Annotation of embedtools/src/ub_env.c, revision 1.1.2.12
1.1.2.1 misho 1: #include "global.h"
2: #include "ub_env.h"
3:
4:
1.1.2.4 misho 5: env_t *env;
6:
1.1.2.3 misho 7: static int
1.1.2.5 misho 8: ub_flash_io(const char *csSec, int mode)
1.1.2.3 misho 9: {
1.1.2.7 misho 10: int f, l, rlen;
1.1.2.5 misho 11: const char *str;
1.1.2.12! misho 12: size_t siz, esiz;
1.1.2.7 misho 13: ait_val_t v;
1.1.2.12! misho 14: u_int crc;
1.1.2.3 misho 15:
16: FTRACE(4);
17:
1.1.2.12! misho 18: str = cfg_getAttribute(&cfg, csSec, "drive_size");
1.1.2.6 misho 19: siz = strtol(str, NULL, 0);
20: if (!siz)
21: return -1;
1.1.2.12! misho 22: str = cfg_getAttribute(&cfg, csSec, "env_size");
! 23: if (!str)
! 24: esiz = siz - sizeof env->env_crc;
! 25: else
! 26: esiz = strtol(str, NULL, 0);
! 27: str = cfg_getAttribute(&cfg, csSec, "drive_name");
1.1.2.5 misho 28: if (!str) {
29: printf("Error:: drive not found!\n");
30: return -1;
31: }
32:
1.1.2.7 misho 33: cfg_loadAttribute(&cfg, "ube", "lock", &v, UBE_LOCK);
1.1.2.8 misho 34: l = open(AIT_GET_STR(&v), O_CREAT | O_EXCL | O_WRONLY, 0644);
1.1.2.7 misho 35: if (l == -1) {
1.1.2.8 misho 36: printf("Error:: Locked u-boot-env map %s\n", AIT_GET_STR(&v));
1.1.2.7 misho 37: AIT_FREE_VAL(&v);
38: return -1;
39: }
40:
1.1.2.5 misho 41: f = open(str, mode);
1.1.2.3 misho 42: if (f == -1) {
1.1.2.5 misho 43: printf("Error:: Can't access u-boot-env device %s\n", str);
1.1.2.7 misho 44: close(l);
45: unlink(AIT_GET_STR(&v));
46: AIT_FREE_VAL(&v);
1.1.2.3 misho 47: return -1;
48: }
49:
50: if (mode & O_RDWR) {
1.1.2.6 misho 51: rlen = write(f, env, siz);
52: if (rlen != siz)
53: printf("Error:: written %d bytes != %d\n", rlen, siz);
54: else
55: VERB(3) printf("Written %d bytes\n", rlen);
56: lseek(f, 0, SEEK_SET);
1.1.2.3 misho 57: }
58:
1.1.2.6 misho 59: rlen = read(f, env, siz);
60: if (rlen != siz)
61: printf("Error:: readed %d bytes != %d\n", rlen, siz);
62: else
63: VERB(3) printf("Readed %d bytes\n", rlen);
64:
1.1.2.12! misho 65: crc = crc32(0, (u_char*) env->env_data, esiz);
! 66: if (crc != env->env_crc)
! 67: VERB(1) printf("Warning:: Flash crc32 0x%x != 0x%x - "
! 68: "Bad CRC, using default environment\n", env->env_crc, crc);
! 69:
1.1.2.3 misho 70: close(f);
1.1.2.7 misho 71: close(l);
72: unlink(AIT_GET_STR(&v));
73: AIT_FREE_VAL(&v);
74: return 0;
1.1.2.3 misho 75: }
76:
1.1.2.10 misho 77: static inline const char *
78: ub_envmatch(const char *csName, const char *e)
79: {
80: while (*csName == *e++)
81: if (*csName++ == '=')
82: return e;
83: if (!*csName && *(e - 1) == '=')
84: return e;
85:
86: return NULL;
87: }
88:
1.1.2.4 misho 89: int
90: ub_load(const char *csSec)
91: {
92: const char *str;
93: size_t siz;
94:
1.1.2.5 misho 95: FTRACE(4);
96:
1.1.2.12! misho 97: str = cfg_getAttribute(&cfg, csSec, "drive_size");
1.1.2.4 misho 98: siz = strtol(str, NULL, 0);
99: if (!siz)
100: return -1;
101:
102: env = e_malloc(siz);
103: if (!env) {
104: ELIBERR(elwix);
105: return -1;
106: }
107:
1.1.2.7 misho 108: return ub_flash_io(csSec, O_RDONLY);
1.1.2.4 misho 109: }
110:
111: void
112: ub_unload()
113: {
1.1.2.5 misho 114: FTRACE(4);
115:
1.1.2.4 misho 116: if (env)
117: e_free(env);
118: }
119:
1.1.2.2 misho 120: const char*
1.1.2.5 misho 121: ub_getenv(const char *csSec, const char *csName)
1.1.2.2 misho 122: {
1.1.2.10 misho 123: char *e, *nxt;
124: size_t dlen;
1.1.2.2 misho 125: const char *str = NULL;
126:
1.1.2.3 misho 127: FTRACE(3);
128:
1.1.2.12! misho 129: str = cfg_getAttribute(&cfg, csSec, "env_size");
! 130: if (!str) {
! 131: str = cfg_getAttribute(&cfg, csSec, "drive_size");
! 132: if (!str)
! 133: return -1;
! 134: }
1.1.2.10 misho 135: dlen = strtol(str, NULL, 0);
136: if (!dlen)
137: return NULL;
138: else
139: dlen--;
140:
141: for (e = env->env_data; *e; e = nxt + 1) {
142: for (nxt = e; *nxt; nxt++)
143: if (nxt >= env->env_data + dlen) {
144: printf("Error:: environment not terminated\n");
145: return NULL;
146: }
147:
148: str = ub_envmatch(csName, e);
149: if (str)
150: break;
151: }
152:
1.1.2.2 misho 153: return str;
154: }
155:
156: int
1.1.2.5 misho 157: ub_setenv(const char *csSec, const char *csName, const char *csValue)
1.1.2.2 misho 158: {
1.1.2.11 misho 159: char *e, *nxt;
160: size_t dlen, len;
161: const char *str, *old = NULL;
162:
1.1.2.3 misho 163: FTRACE(3);
164:
1.1.2.12! misho 165: str = cfg_getAttribute(&cfg, csSec, "env_size");
! 166: if (!str) {
! 167: str = cfg_getAttribute(&cfg, csSec, "drive_size");
! 168: if (!str)
! 169: return -1;
! 170: }
1.1.2.11 misho 171: dlen = strtol(str, NULL, 0);
172: if (!dlen)
173: return -1;
174: else
175: dlen--;
176:
177: for (e = env->env_data; *e; e = nxt + 1) {
178: for (nxt = e; *nxt; nxt++)
179: if (nxt >= env->env_data + dlen) {
180: printf("Error:: environment not terminated\n");
181: return -1;
182: }
183:
184: old = ub_envmatch(csName, e);
185: if (old)
186: break;
187: }
188:
189: /* Delete any existing definition */
190: if (old) {
191: if (!*++nxt)
192: *e = 0;
193: else
194: while (42) {
195: *e = *nxt++;
196: if (!*e && !*nxt)
197: break;
198: e++;
199: }
200: *++e = 0;
201: }
202:
203: if (csValue) {
204: /* Append new definition at the end */
205: for (e = env->env_data; *e || *(e + 1); e++);
206: if (e > env->env_data)
207: e++;
208: /* "name" + "=" + "val" +"\0\0" > u-boot-env size */
209: len = strlen(csName) + 2; /* add '=' for first arg, ' ' for all others */
210: len += strlen(csValue) + 1;
211: if (len > env->env_data + dlen - e) {
212: printf("Error:: Environment overflow!\n");
213: return -1;
214: }
215:
216: /* av pair */
217: while ((*e = *csName++))
218: e++;
219: *e = '=';
220: while ((*++e = *csValue++));
221:
222: /* end is marked with double '\0' */
223: *++e = 0;
224: }
225:
226: if (ub_flash_io(csSec, O_RDWR)) {
227: printf("Error:: Can't write environment to flash!\n");
228: return -1;
229: }
230:
1.1.2.2 misho 231: return 0;
232: }
1.1.2.9 misho 233:
234: int
235: ub_env(const char *csSec)
236: {
237: char *e, *nxt;
238: size_t dlen;
239: const char *str;
240:
241: FTRACE(3);
242:
1.1.2.12! misho 243: str = cfg_getAttribute(&cfg, csSec, "env_size");
! 244: if (!str) {
! 245: str = cfg_getAttribute(&cfg, csSec, "drive_size");
! 246: if (!str)
! 247: return -1;
! 248: }
1.1.2.9 misho 249: dlen = strtol(str, NULL, 0);
250: if (!dlen)
251: return -1;
252: else
253: dlen--;
254:
255: for (e = env->env_data; *e; e = nxt + 1) {
256: for (nxt = e; *nxt; nxt++)
257: if (nxt >= env->env_data + dlen) {
258: printf("Error:: environment not terminated\n");
259: return -1;
260: }
261:
262: printf("%s\n", e);
263: }
264:
265: return 0;
266: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>