Annotation of embedtools/src/ub_env.c, revision 1.1.2.11
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:
7:
1.1.2.3 misho 8: static int
1.1.2.5 misho 9: ub_flash_io(const char *csSec, int mode)
1.1.2.3 misho 10: {
1.1.2.7 misho 11: int f, l, rlen;
1.1.2.5 misho 12: const char *str;
1.1.2.6 misho 13: size_t siz;
1.1.2.7 misho 14: ait_val_t v;
1.1.2.3 misho 15:
16: FTRACE(4);
17:
1.1.2.6 misho 18: str = cfg_getAttribute(&cfg, csSec, "size");
19: siz = strtol(str, NULL, 0);
20: if (!siz)
21: return -1;
1.1.2.5 misho 22: str = cfg_getAttribute(&cfg, csSec, "drive");
23: if (!str) {
24: printf("Error:: drive not found!\n");
25: return -1;
26: }
27:
1.1.2.7 misho 28: cfg_loadAttribute(&cfg, "ube", "lock", &v, UBE_LOCK);
1.1.2.8 misho 29: l = open(AIT_GET_STR(&v), O_CREAT | O_EXCL | O_WRONLY, 0644);
1.1.2.7 misho 30: if (l == -1) {
1.1.2.8 misho 31: printf("Error:: Locked u-boot-env map %s\n", AIT_GET_STR(&v));
1.1.2.7 misho 32: AIT_FREE_VAL(&v);
33: return -1;
34: }
35:
1.1.2.5 misho 36: f = open(str, mode);
1.1.2.3 misho 37: if (f == -1) {
1.1.2.5 misho 38: printf("Error:: Can't access u-boot-env device %s\n", str);
1.1.2.7 misho 39: close(l);
40: unlink(AIT_GET_STR(&v));
41: AIT_FREE_VAL(&v);
1.1.2.3 misho 42: return -1;
43: }
44:
45: if (mode & O_RDWR) {
1.1.2.6 misho 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);
1.1.2.3 misho 52: }
53:
1.1.2.6 misho 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:
1.1.2.3 misho 60: close(f);
1.1.2.7 misho 61: close(l);
62: unlink(AIT_GET_STR(&v));
63: AIT_FREE_VAL(&v);
64: return 0;
1.1.2.3 misho 65: }
66:
1.1.2.10 misho 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:
1.1.2.4 misho 79: int
80: ub_load(const char *csSec)
81: {
82: const char *str;
83: size_t siz;
84:
1.1.2.5 misho 85: FTRACE(4);
86:
1.1.2.4 misho 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:
1.1.2.7 misho 98: return ub_flash_io(csSec, O_RDONLY);
1.1.2.4 misho 99: }
100:
101: void
102: ub_unload()
103: {
1.1.2.5 misho 104: FTRACE(4);
105:
1.1.2.4 misho 106: if (env)
107: e_free(env);
108: }
109:
1.1.2.2 misho 110: const char*
1.1.2.5 misho 111: ub_getenv(const char *csSec, const char *csName)
1.1.2.2 misho 112: {
1.1.2.10 misho 113: char *e, *nxt;
114: size_t dlen;
1.1.2.2 misho 115: const char *str = NULL;
116:
1.1.2.3 misho 117: FTRACE(3);
118:
1.1.2.10 misho 119: str = cfg_getAttribute(&cfg, csSec, "size");
1.1.2.11! misho 120: if (!str)
! 121: return NULL;
1.1.2.10 misho 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:
1.1.2.2 misho 140: return str;
141: }
142:
143: int
1.1.2.5 misho 144: ub_setenv(const char *csSec, const char *csName, const char *csValue)
1.1.2.2 misho 145: {
1.1.2.11! misho 146: char *e, *nxt;
! 147: size_t dlen, len;
! 148: const char *str, *old = NULL;
! 149:
1.1.2.3 misho 150: FTRACE(3);
151:
1.1.2.11! misho 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:
1.1.2.2 misho 215: return 0;
216: }
1.1.2.9 misho 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");
1.1.2.11! misho 228: if (!str)
! 229: return -1;
1.1.2.9 misho 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>