1: #include "global.h"
2: #include "ub_env.h"
3:
4:
5: env_t *env;
6:
7:
8: static int
9: ub_flash_io(const char *csSec, int mode)
10: {
11: int f, l, rlen;
12: const char *str;
13: size_t siz;
14: ait_val_t v;
15:
16: FTRACE(4);
17:
18: str = cfg_getAttribute(&cfg, csSec, "size");
19: siz = strtol(str, NULL, 0);
20: if (!siz)
21: return -1;
22: str = cfg_getAttribute(&cfg, csSec, "drive");
23: if (!str) {
24: printf("Error:: drive not found!\n");
25: return -1;
26: }
27:
28: cfg_loadAttribute(&cfg, "ube", "lock", &v, UBE_LOCK);
29: l = open(AIT_GET_STR(&v), O_CREAT | O_EXCL | O_WRONLY, 0644);
30: if (l == -1) {
31: printf("Error:: Locked u-boot-env map %s\n", AIT_GET_STR(&v));
32: AIT_FREE_VAL(&v);
33: return -1;
34: }
35:
36: f = open(str, mode);
37: if (f == -1) {
38: printf("Error:: Can't access u-boot-env device %s\n", str);
39: close(l);
40: unlink(AIT_GET_STR(&v));
41: AIT_FREE_VAL(&v);
42: return -1;
43: }
44:
45: if (mode & O_RDWR) {
46: rlen = write(f, env, siz);
47: if (rlen != siz)
48: printf("Error:: written %d bytes != %d\n", rlen, siz);
49: else
50: VERB(3) printf("Written %d bytes\n", rlen);
51: lseek(f, 0, SEEK_SET);
52: }
53:
54: rlen = read(f, env, siz);
55: if (rlen != siz)
56: printf("Error:: readed %d bytes != %d\n", rlen, siz);
57: else
58: VERB(3) printf("Readed %d bytes\n", rlen);
59:
60: close(f);
61: close(l);
62: unlink(AIT_GET_STR(&v));
63: AIT_FREE_VAL(&v);
64: return 0;
65: }
66:
67: int
68: ub_load(const char *csSec)
69: {
70: const char *str;
71: size_t siz;
72:
73: FTRACE(4);
74:
75: str = cfg_getAttribute(&cfg, csSec, "size");
76: siz = strtol(str, NULL, 0);
77: if (!siz)
78: return -1;
79:
80: env = e_malloc(siz);
81: if (!env) {
82: ELIBERR(elwix);
83: return -1;
84: }
85:
86: return ub_flash_io(csSec, O_RDONLY);
87: }
88:
89: void
90: ub_unload()
91: {
92: FTRACE(4);
93:
94: if (env)
95: e_free(env);
96: }
97:
98: const char*
99: ub_getenv(const char *csSec, const char *csName)
100: {
101: const char *str = NULL;
102:
103: FTRACE(3);
104:
105: return str;
106: }
107:
108: int
109: ub_setenv(const char *csSec, const char *csName, const char *csValue)
110: {
111: FTRACE(3);
112:
113: return 0;
114: }
115:
116: int
117: ub_env(const char *csSec)
118: {
119: char *e, *nxt;
120: size_t dlen;
121: const char *str;
122:
123: FTRACE(3);
124:
125: str = cfg_getAttribute(&cfg, csSec, "size");
126: dlen = strtol(str, NULL, 0);
127: if (!dlen)
128: return -1;
129: else
130: dlen--;
131:
132: for (e = env->env_data; *e; e = nxt + 1) {
133: for (nxt = e; *nxt; nxt++)
134: if (nxt >= env->env_data + dlen) {
135: printf("Error:: environment not terminated\n");
136: return -1;
137: }
138:
139: printf("%s\n", e);
140: }
141:
142: return 0;
143: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>