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: const char *str = NULL;
71:
72: while (*csName == *e++)
73: if (*csName++ == '=')
74: return e;
75: if (!*csName && *(e - 1) == '=')
76: return e;
77:
78: return NULL;
79: }
80:
81: int
82: ub_load(const char *csSec)
83: {
84: const char *str;
85: size_t siz;
86:
87: FTRACE(4);
88:
89: str = cfg_getAttribute(&cfg, csSec, "size");
90: siz = strtol(str, NULL, 0);
91: if (!siz)
92: return -1;
93:
94: env = e_malloc(siz);
95: if (!env) {
96: ELIBERR(elwix);
97: return -1;
98: }
99:
100: return ub_flash_io(csSec, O_RDONLY);
101: }
102:
103: void
104: ub_unload()
105: {
106: FTRACE(4);
107:
108: if (env)
109: e_free(env);
110: }
111:
112: const char*
113: ub_getenv(const char *csSec, const char *csName)
114: {
115: char *e, *nxt;
116: size_t dlen;
117: const char *str = NULL;
118:
119:
120: FTRACE(3);
121:
122: str = cfg_getAttribute(&cfg, csSec, "size");
123: dlen = strtol(str, NULL, 0);
124: if (!dlen)
125: return NULL;
126: else
127: dlen--;
128:
129: for (e = env->env_data; *e; e = nxt + 1) {
130: for (nxt = e; *nxt; nxt++)
131: if (nxt >= env->env_data + dlen) {
132: printf("Error:: environment not terminated\n");
133: return NULL;
134: }
135:
136: str = ub_envmatch(csName, e);
137: if (str)
138: break;
139: }
140:
141: return str;
142: }
143:
144: int
145: ub_setenv(const char *csSec, const char *csName, const char *csValue)
146: {
147: FTRACE(3);
148:
149: return 0;
150: }
151:
152: int
153: ub_env(const char *csSec)
154: {
155: char *e, *nxt;
156: size_t dlen;
157: const char *str;
158:
159: FTRACE(3);
160:
161: str = cfg_getAttribute(&cfg, csSec, "size");
162: dlen = strtol(str, NULL, 0);
163: if (!dlen)
164: return -1;
165: else
166: dlen--;
167:
168: for (e = env->env_data; *e; e = nxt + 1) {
169: for (nxt = e; *nxt; nxt++)
170: if (nxt >= env->env_data + dlen) {
171: printf("Error:: environment not terminated\n");
172: return -1;
173: }
174:
175: printf("%s\n", e);
176: }
177:
178: return 0;
179: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>