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: static inline const char *
68: ub_envmatch(const char *csName, const char *e)
69: {
70: while (*csName == *e++)
71: if (*csName++ == '=')
72: return e;
73: if (!*csName && *(e - 1) == '=')
74: return e;
75:
76: return NULL;
77: }
78:
79: int
80: ub_load(const char *csSec)
81: {
82: const char *str;
83: size_t siz;
84:
85: FTRACE(4);
86:
87: str = cfg_getAttribute(&cfg, csSec, "size");
88: siz = strtol(str, NULL, 0);
89: if (!siz)
90: return -1;
91:
92: env = e_malloc(siz);
93: if (!env) {
94: ELIBERR(elwix);
95: return -1;
96: }
97:
98: return ub_flash_io(csSec, O_RDONLY);
99: }
100:
101: void
102: ub_unload()
103: {
104: FTRACE(4);
105:
106: if (env)
107: e_free(env);
108: }
109:
110: const char*
111: ub_getenv(const char *csSec, const char *csName)
112: {
113: char *e, *nxt;
114: size_t dlen;
115: const char *str = NULL;
116:
117: FTRACE(3);
118:
119: str = cfg_getAttribute(&cfg, csSec, "size");
120: if (!str)
121: return NULL;
122: dlen = strtol(str, NULL, 0);
123: if (!dlen)
124: return NULL;
125: else
126: dlen--;
127:
128: for (e = env->env_data; *e; e = nxt + 1) {
129: for (nxt = e; *nxt; nxt++)
130: if (nxt >= env->env_data + dlen) {
131: printf("Error:: environment not terminated\n");
132: return NULL;
133: }
134:
135: str = ub_envmatch(csName, e);
136: if (str)
137: break;
138: }
139:
140: return str;
141: }
142:
143: int
144: ub_setenv(const char *csSec, const char *csName, const char *csValue)
145: {
146: char *e, *nxt;
147: size_t dlen, len;
148: const char *str, *old = NULL;
149:
150: FTRACE(3);
151:
152: str = cfg_getAttribute(&cfg, csSec, "size");
153: if (!str)
154: return -1;
155: dlen = strtol(str, NULL, 0);
156: if (!dlen)
157: return -1;
158: else
159: dlen--;
160:
161: for (e = env->env_data; *e; e = nxt + 1) {
162: for (nxt = e; *nxt; nxt++)
163: if (nxt >= env->env_data + dlen) {
164: printf("Error:: environment not terminated\n");
165: return -1;
166: }
167:
168: old = ub_envmatch(csName, e);
169: if (old)
170: break;
171: }
172:
173: /* Delete any existing definition */
174: if (old) {
175: if (!*++nxt)
176: *e = 0;
177: else
178: while (42) {
179: *e = *nxt++;
180: if (!*e && !*nxt)
181: break;
182: e++;
183: }
184: *++e = 0;
185: }
186:
187: if (csValue) {
188: /* Append new definition at the end */
189: for (e = env->env_data; *e || *(e + 1); e++);
190: if (e > env->env_data)
191: e++;
192: /* "name" + "=" + "val" +"\0\0" > u-boot-env size */
193: len = strlen(csName) + 2; /* add '=' for first arg, ' ' for all others */
194: len += strlen(csValue) + 1;
195: if (len > env->env_data + dlen - e) {
196: printf("Error:: Environment overflow!\n");
197: return -1;
198: }
199:
200: /* av pair */
201: while ((*e = *csName++))
202: e++;
203: *e = '=';
204: while ((*++e = *csValue++));
205:
206: /* end is marked with double '\0' */
207: *++e = 0;
208: }
209:
210: if (ub_flash_io(csSec, O_RDWR)) {
211: printf("Error:: Can't write environment to flash!\n");
212: return -1;
213: }
214:
215: return 0;
216: }
217:
218: int
219: ub_env(const char *csSec)
220: {
221: char *e, *nxt;
222: size_t dlen;
223: const char *str;
224:
225: FTRACE(3);
226:
227: str = cfg_getAttribute(&cfg, csSec, "size");
228: if (!str)
229: return -1;
230: dlen = strtol(str, NULL, 0);
231: if (!dlen)
232: return -1;
233: else
234: dlen--;
235:
236: for (e = env->env_data; *e; e = nxt + 1) {
237: for (nxt = e; *nxt; nxt++)
238: if (nxt >= env->env_data + dlen) {
239: printf("Error:: environment not terminated\n");
240: return -1;
241: }
242:
243: printf("%s\n", e);
244: }
245:
246: return 0;
247: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>