]>
Commit | Line | Data |
---|---|---|
05706fdd | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
05706fdd WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
6 | * Andreas Heppel <[email protected]> | |
ea882baf | 7 | * |
3765b3e7 | 8 | * SPDX-License-Identifier: GPL-2.0+ |
05706fdd WD |
9 | */ |
10 | ||
11 | #include <common.h> | |
12 | #include <command.h> | |
13 | #include <environment.h> | |
05706fdd | 14 | #include <linux/stddef.h> |
ea882baf WD |
15 | #include <search.h> |
16 | #include <errno.h> | |
05706fdd WD |
17 | #include <malloc.h> |
18 | ||
d87080b7 WD |
19 | DECLARE_GLOBAL_DATA_PTR; |
20 | ||
05706fdd WD |
21 | /************************************************************************ |
22 | * Default settings to be used when no valid environment is found | |
23 | */ | |
ddd8418f | 24 | #include <env_default.h> |
05706fdd | 25 | |
c5983592 | 26 | struct hsearch_data env_htab = { |
2598090b | 27 | .change_ok = env_flags_validate, |
c5983592 | 28 | }; |
2eb1573f | 29 | |
0b7df656 | 30 | __weak uchar env_get_char_spec(int index) |
bf95df44 IG |
31 | { |
32 | return *((uchar *)(gd->env_addr + index)); | |
33 | } | |
bf95df44 | 34 | |
27aafe98 | 35 | static uchar env_get_char_init(int index) |
05706fdd | 36 | { |
05706fdd WD |
37 | /* if crc was bad, use the default environment */ |
38 | if (gd->env_valid) | |
27aafe98 | 39 | return env_get_char_spec(index); |
ea882baf | 40 | else |
27aafe98 | 41 | return default_environment[index]; |
05706fdd WD |
42 | } |
43 | ||
27aafe98 | 44 | uchar env_get_char_memory(int index) |
05706fdd | 45 | { |
ea882baf | 46 | return *env_get_addr(index); |
05706fdd WD |
47 | } |
48 | ||
27aafe98 | 49 | uchar env_get_char(int index) |
b502611b | 50 | { |
b502611b JT |
51 | /* if relocated to RAM */ |
52 | if (gd->flags & GD_FLG_RELOC) | |
27aafe98 | 53 | return env_get_char_memory(index); |
b502611b | 54 | else |
27aafe98 | 55 | return env_get_char_init(index); |
b502611b JT |
56 | } |
57 | ||
27aafe98 | 58 | const uchar *env_get_addr(int index) |
05706fdd | 59 | { |
ea882baf WD |
60 | if (gd->env_valid) |
61 | return (uchar *)(gd->env_addr + index); | |
62 | else | |
63 | return &default_environment[index]; | |
05706fdd WD |
64 | } |
65 | ||
ec8a252c JH |
66 | /* |
67 | * Read an environment variable as a boolean | |
68 | * Return -1 if variable does not exist (default to true) | |
69 | */ | |
70 | int getenv_yesno(const char *var) | |
71 | { | |
72 | char *s = getenv(var); | |
73 | ||
74 | if (s == NULL) | |
75 | return -1; | |
76 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? | |
77 | 1 : 0; | |
78 | } | |
79 | ||
267541f7 JH |
80 | /* |
81 | * Look up the variable from the default environment | |
82 | */ | |
83 | char *getenv_default(const char *name) | |
84 | { | |
85 | char *ret_val; | |
86 | unsigned long really_valid = gd->env_valid; | |
87 | unsigned long real_gd_flags = gd->flags; | |
88 | ||
89 | /* Pretend that the image is bad. */ | |
90 | gd->flags &= ~GD_FLG_ENV_READY; | |
91 | gd->env_valid = 0; | |
92 | ret_val = getenv(name); | |
93 | gd->env_valid = really_valid; | |
94 | gd->flags = real_gd_flags; | |
95 | return ret_val; | |
96 | } | |
97 | ||
ea882baf | 98 | void set_default_env(const char *s) |
5bb12dbd | 99 | { |
c4e0057f JH |
100 | int flags = 0; |
101 | ||
5bb12dbd | 102 | if (sizeof(default_environment) > ENV_SIZE) { |
ea882baf | 103 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
104 | return; |
105 | } | |
106 | ||
ea882baf WD |
107 | if (s) { |
108 | if (*s == '!') { | |
109 | printf("*** Warning - %s, " | |
110 | "using default environment\n\n", | |
27aafe98 | 111 | s + 1); |
ea882baf | 112 | } else { |
c4e0057f | 113 | flags = H_INTERACTIVE; |
ea882baf WD |
114 | puts(s); |
115 | } | |
116 | } else { | |
117 | puts("Using default environment\n\n"); | |
118 | } | |
119 | ||
2eb1573f | 120 | if (himport_r(&env_htab, (char *)default_environment, |
ecd1446f | 121 | sizeof(default_environment), '\0', flags, 0, |
c4e0057f | 122 | 0, NULL) == 0) |
ea882baf | 123 | error("Environment import failed: errno = %d\n", errno); |
27aafe98 | 124 | |
ea882baf | 125 | gd->flags |= GD_FLG_ENV_READY; |
340b0e3b | 126 | gd->flags |= GD_FLG_ENV_DEFAULT; |
5bb12dbd HW |
127 | } |
128 | ||
b64b7c3d GF |
129 | |
130 | /* [re]set individual variables to their value in the default environment */ | |
131 | int set_default_vars(int nvars, char * const vars[]) | |
132 | { | |
133 | /* | |
134 | * Special use-case: import from default environment | |
135 | * (and use \0 as a separator) | |
136 | */ | |
137 | return himport_r(&env_htab, (const char *)default_environment, | |
c4e0057f | 138 | sizeof(default_environment), '\0', |
ecd1446f | 139 | H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars); |
b64b7c3d GF |
140 | } |
141 | ||
a4223b74 | 142 | #ifdef CONFIG_ENV_AES |
b80c0b99 | 143 | #include <uboot_aes.h> |
a4223b74 MV |
144 | /** |
145 | * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment | |
146 | * | |
147 | * This function shall return 16-byte array containing AES-128 key used | |
62a3b7dd | 148 | * to encrypt and decrypt the environment. This function must be overridden |
a4223b74 MV |
149 | * by the implementer as otherwise the environment encryption will not |
150 | * work. | |
151 | */ | |
152 | __weak uint8_t *env_aes_cbc_get_key(void) | |
153 | { | |
154 | return NULL; | |
155 | } | |
156 | ||
157 | static int env_aes_cbc_crypt(env_t *env, const int enc) | |
158 | { | |
159 | unsigned char *data = env->data; | |
160 | uint8_t *key; | |
161 | uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; | |
162 | uint32_t aes_blocks; | |
163 | ||
164 | key = env_aes_cbc_get_key(); | |
165 | if (!key) | |
166 | return -EINVAL; | |
167 | ||
168 | /* First we expand the key. */ | |
169 | aes_expand_key(key, key_exp); | |
170 | ||
171 | /* Calculate the number of AES blocks to encrypt. */ | |
172 | aes_blocks = ENV_SIZE / AES_KEY_LENGTH; | |
173 | ||
174 | if (enc) | |
175 | aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks); | |
176 | else | |
177 | aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | #else | |
182 | static inline int env_aes_cbc_crypt(env_t *env, const int enc) | |
183 | { | |
184 | return 0; | |
185 | } | |
186 | #endif | |
187 | ||
ea882baf WD |
188 | /* |
189 | * Check if CRC is valid and (if yes) import the environment. | |
190 | * Note that "buf" may or may not be aligned. | |
191 | */ | |
192 | int env_import(const char *buf, int check) | |
05706fdd | 193 | { |
ea882baf | 194 | env_t *ep = (env_t *)buf; |
a4223b74 | 195 | int ret; |
05706fdd | 196 | |
ea882baf WD |
197 | if (check) { |
198 | uint32_t crc; | |
199 | ||
200 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
201 | ||
202 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
203 | set_default_env("!bad CRC"); | |
204 | return 0; | |
205 | } | |
206 | } | |
207 | ||
a4223b74 MV |
208 | /* Decrypt the env if desired. */ |
209 | ret = env_aes_cbc_crypt(ep, 0); | |
210 | if (ret) { | |
211 | error("Failed to decrypt env!\n"); | |
212 | set_default_env("!import failed"); | |
213 | return ret; | |
214 | } | |
215 | ||
ecd1446f | 216 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, |
c4e0057f | 217 | 0, NULL)) { |
ea882baf WD |
218 | gd->flags |= GD_FLG_ENV_READY; |
219 | return 1; | |
220 | } | |
05706fdd | 221 | |
ea882baf WD |
222 | error("Cannot import environment: errno = %d\n", errno); |
223 | ||
224 | set_default_env("!import failed"); | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
76768f5f FA |
229 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
230 | static unsigned char env_flags; | |
231 | ||
232 | int env_import_redund(const char *buf1, const char *buf2) | |
233 | { | |
234 | int crc1_ok, crc2_ok; | |
235 | env_t *ep, *tmp_env1, *tmp_env2; | |
236 | ||
237 | tmp_env1 = (env_t *)buf1; | |
238 | tmp_env2 = (env_t *)buf2; | |
239 | ||
240 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == | |
241 | tmp_env1->crc; | |
242 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == | |
243 | tmp_env2->crc; | |
244 | ||
245 | if (!crc1_ok && !crc2_ok) { | |
246 | set_default_env("!bad CRC"); | |
247 | return 0; | |
248 | } else if (crc1_ok && !crc2_ok) { | |
249 | gd->env_valid = 1; | |
250 | } else if (!crc1_ok && crc2_ok) { | |
251 | gd->env_valid = 2; | |
252 | } else { | |
253 | /* both ok - check serial */ | |
254 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
255 | gd->env_valid = 2; | |
256 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) | |
257 | gd->env_valid = 1; | |
258 | else if (tmp_env1->flags > tmp_env2->flags) | |
259 | gd->env_valid = 1; | |
260 | else if (tmp_env2->flags > tmp_env1->flags) | |
261 | gd->env_valid = 2; | |
262 | else /* flags are equal - almost impossible */ | |
263 | gd->env_valid = 1; | |
264 | } | |
265 | ||
266 | if (gd->env_valid == 1) | |
267 | ep = tmp_env1; | |
268 | else | |
269 | ep = tmp_env2; | |
270 | ||
271 | env_flags = ep->flags; | |
272 | return env_import((char *)ep, 0); | |
273 | } | |
274 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
275 | ||
fc0b5948 | 276 | /* Export the environment and generate CRC for it. */ |
7ce1526e MV |
277 | int env_export(env_t *env_out) |
278 | { | |
279 | char *res; | |
280 | ssize_t len; | |
a4223b74 | 281 | int ret; |
7ce1526e MV |
282 | |
283 | res = (char *)env_out->data; | |
284 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); | |
285 | if (len < 0) { | |
286 | error("Cannot export environment: errno = %d\n", errno); | |
287 | return 1; | |
288 | } | |
a4223b74 MV |
289 | |
290 | /* Encrypt the env if desired. */ | |
291 | ret = env_aes_cbc_crypt(env_out, 1); | |
292 | if (ret) | |
293 | return ret; | |
294 | ||
7ce1526e MV |
295 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
296 | ||
76768f5f FA |
297 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
298 | env_out->flags = ++env_flags; /* increase the serial */ | |
299 | #endif | |
300 | ||
7ce1526e MV |
301 | return 0; |
302 | } | |
303 | ||
27aafe98 | 304 | void env_relocate(void) |
ea882baf | 305 | { |
2e5167cc | 306 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f | 307 | env_reloc(); |
7afcf3a5 | 308 | env_htab.change_ok += gd->reloc_off; |
60f7da1f | 309 | #endif |
05706fdd | 310 | if (gd->env_valid == 0) { |
7ac2fe2d IY |
311 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
312 | /* Environment not changable */ | |
ea882baf | 313 | set_default_env(NULL); |
05706fdd | 314 | #else |
770605e4 | 315 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
ea882baf | 316 | set_default_env("!bad CRC"); |
d259079d | 317 | #endif |
ea882baf | 318 | } else { |
27aafe98 | 319 | env_relocate_spec(); |
05706fdd | 320 | } |
05706fdd | 321 | } |
04a85b3b | 322 | |
7ac2fe2d | 323 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
04a85b3b WD |
324 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
325 | { | |
560d424b MF |
326 | ENTRY *match; |
327 | int found, idx; | |
04a85b3b | 328 | |
560d424b | 329 | idx = 0; |
04a85b3b WD |
330 | found = 0; |
331 | cmdv[0] = NULL; | |
332 | ||
560d424b MF |
333 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
334 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 335 | |
560d424b | 336 | if (found >= maxv - 2 || bufsz < vallen) |
04a85b3b | 337 | break; |
560d424b | 338 | |
04a85b3b | 339 | cmdv[found++] = buf; |
560d424b MF |
340 | memcpy(buf, match->key, vallen); |
341 | buf += vallen; | |
342 | bufsz -= vallen; | |
04a85b3b WD |
343 | } |
344 | ||
560d424b MF |
345 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
346 | ||
347 | if (idx) | |
348 | cmdv[found++] = "..."; | |
27aafe98 | 349 | |
04a85b3b WD |
350 | cmdv[found] = NULL; |
351 | return found; | |
352 | } | |
353 | #endif |