]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
05706fdd | 2 | /* |
ea882baf | 3 | * (C) Copyright 2000-2010 |
05706fdd WD |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
5 | * | |
6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
7 | * Andreas Heppel <[email protected]> | |
05706fdd WD |
8 | */ |
9 | ||
10 | #include <common.h> | |
52f24238 | 11 | #include <bootstage.h> |
05706fdd | 12 | #include <command.h> |
3f989e7b | 13 | #include <env.h> |
f3998fdc | 14 | #include <env_internal.h> |
f7ae49fc | 15 | #include <log.h> |
8bef79bf | 16 | #include <sort.h> |
401d1c4f | 17 | #include <asm/global_data.h> |
05706fdd | 18 | #include <linux/stddef.h> |
ea882baf WD |
19 | #include <search.h> |
20 | #include <errno.h> | |
05706fdd | 21 | #include <malloc.h> |
3db71108 | 22 | #include <u-boot/crc.h> |
95fd9772 | 23 | #include <dm/ofnode.h> |
f2315664 MB |
24 | #include <net.h> |
25 | #include <watchdog.h> | |
05706fdd | 26 | |
d87080b7 WD |
27 | DECLARE_GLOBAL_DATA_PTR; |
28 | ||
05706fdd WD |
29 | /************************************************************************ |
30 | * Default settings to be used when no valid environment is found | |
31 | */ | |
ddd8418f | 32 | #include <env_default.h> |
05706fdd | 33 | |
c5983592 | 34 | struct hsearch_data env_htab = { |
2598090b | 35 | .change_ok = env_flags_validate, |
c5983592 | 36 | }; |
2eb1573f | 37 | |
f2315664 MB |
38 | /* |
39 | * This env_set() function is defined in cmd/nvedit.c, since it calls | |
40 | * _do_env_set(), whis is a static function in that file. | |
41 | * | |
42 | * int env_set(const char *varname, const char *varvalue); | |
43 | */ | |
44 | ||
45 | /** | |
46 | * Set an environment variable to an integer value | |
47 | * | |
48 | * @param varname Environment variable to set | |
49 | * @param value Value to set it to | |
50 | * @return 0 if ok, 1 on error | |
51 | */ | |
52 | int env_set_ulong(const char *varname, ulong value) | |
53 | { | |
54 | /* TODO: this should be unsigned */ | |
55 | char *str = simple_itoa(value); | |
56 | ||
57 | return env_set(varname, str); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Set an environment variable to an value in hex | |
62 | * | |
63 | * @param varname Environment variable to set | |
64 | * @param value Value to set it to | |
65 | * @return 0 if ok, 1 on error | |
66 | */ | |
67 | int env_set_hex(const char *varname, ulong value) | |
68 | { | |
69 | char str[17]; | |
70 | ||
71 | sprintf(str, "%lx", value); | |
72 | return env_set(varname, str); | |
73 | } | |
74 | ||
75 | ulong env_get_hex(const char *varname, ulong default_val) | |
76 | { | |
77 | const char *s; | |
78 | ulong value; | |
79 | char *endp; | |
80 | ||
81 | s = env_get(varname); | |
82 | if (s) | |
83 | value = hextoul(s, &endp); | |
84 | if (!s || endp == s) | |
85 | return default_val; | |
86 | ||
87 | return value; | |
88 | } | |
89 | ||
90 | int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) | |
91 | { | |
92 | string_to_enetaddr(env_get(name), enetaddr); | |
93 | return is_valid_ethaddr(enetaddr); | |
94 | } | |
95 | ||
96 | int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) | |
97 | { | |
98 | char buf[ARP_HLEN_ASCII + 1]; | |
99 | ||
100 | if (eth_env_get_enetaddr(name, (uint8_t *)buf)) | |
101 | return -EEXIST; | |
102 | ||
103 | sprintf(buf, "%pM", enetaddr); | |
104 | ||
105 | return env_set(name, buf); | |
106 | } | |
107 | ||
108 | /* | |
109 | * Look up variable from environment, | |
110 | * return address of storage for that variable, | |
111 | * or NULL if not found | |
112 | */ | |
113 | char *env_get(const char *name) | |
114 | { | |
115 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ | |
116 | struct env_entry e, *ep; | |
117 | ||
118 | WATCHDOG_RESET(); | |
119 | ||
120 | e.key = name; | |
121 | e.data = NULL; | |
122 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); | |
123 | ||
124 | return ep ? ep->data : NULL; | |
125 | } | |
126 | ||
127 | /* restricted capabilities before import */ | |
128 | if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) | |
129 | return (char *)(gd->env_buf); | |
130 | ||
131 | return NULL; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Like env_get, but prints an error if envvar isn't defined in the | |
136 | * environment. It always returns what env_get does, so it can be used in | |
137 | * place of env_get without changing error handling otherwise. | |
138 | */ | |
139 | char *from_env(const char *envvar) | |
140 | { | |
141 | char *ret; | |
142 | ||
143 | ret = env_get(envvar); | |
144 | ||
145 | if (!ret) | |
146 | printf("missing environment variable: %s\n", envvar); | |
147 | ||
148 | return ret; | |
149 | } | |
150 | ||
151 | /* | |
152 | * Look up variable from environment for restricted C runtime env. | |
153 | */ | |
154 | int env_get_f(const char *name, char *buf, unsigned len) | |
155 | { | |
156 | const char *env, *p, *end; | |
157 | size_t name_len; | |
158 | ||
159 | if (name == NULL || *name == '\0') | |
160 | return -1; | |
161 | ||
162 | name_len = strlen(name); | |
163 | ||
164 | if (gd->env_valid == ENV_INVALID) | |
165 | env = (const char *)default_environment; | |
166 | else | |
167 | env = (const char *)gd->env_addr; | |
168 | ||
169 | for (p = env; *p != '\0'; p = end + 1) { | |
170 | const char *value; | |
171 | unsigned res; | |
172 | ||
173 | for (end = p; *end != '\0'; ++end) | |
174 | if (end - env >= CONFIG_ENV_SIZE) | |
175 | return -1; | |
176 | ||
177 | if (strncmp(name, p, name_len) || p[name_len] != '=') | |
178 | continue; | |
179 | value = &p[name_len + 1]; | |
180 | ||
181 | res = end - value; | |
182 | memcpy(buf, value, min(len, res + 1)); | |
183 | ||
184 | if (len <= res) { | |
185 | buf[len - 1] = '\0'; | |
186 | printf("env_buf [%u bytes] too small for value of \"%s\"\n", | |
187 | len, name); | |
188 | } | |
189 | ||
190 | return res; | |
191 | } | |
192 | ||
193 | return -1; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Decode the integer value of an environment variable and return it. | |
198 | * | |
199 | * @param name Name of environment variable | |
200 | * @param base Number base to use (normally 10, or 16 for hex) | |
201 | * @param default_val Default value to return if the variable is not | |
202 | * found | |
203 | * @return the decoded value, or default_val if not found | |
204 | */ | |
205 | ulong env_get_ulong(const char *name, int base, ulong default_val) | |
206 | { | |
207 | /* | |
208 | * We can use env_get() here, even before relocation, since the | |
209 | * environment variable value is an integer and thus short. | |
210 | */ | |
211 | const char *str = env_get(name); | |
212 | ||
213 | return str ? simple_strtoul(str, NULL, base) : default_val; | |
214 | } | |
215 | ||
ec8a252c JH |
216 | /* |
217 | * Read an environment variable as a boolean | |
218 | * Return -1 if variable does not exist (default to true) | |
219 | */ | |
bfebc8c9 | 220 | int env_get_yesno(const char *var) |
ec8a252c | 221 | { |
00caae6d | 222 | char *s = env_get(var); |
ec8a252c JH |
223 | |
224 | if (s == NULL) | |
225 | return -1; | |
226 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? | |
227 | 1 : 0; | |
228 | } | |
229 | ||
267541f7 JH |
230 | /* |
231 | * Look up the variable from the default environment | |
232 | */ | |
723806cc | 233 | char *env_get_default(const char *name) |
267541f7 JH |
234 | { |
235 | char *ret_val; | |
236 | unsigned long really_valid = gd->env_valid; | |
237 | unsigned long real_gd_flags = gd->flags; | |
238 | ||
239 | /* Pretend that the image is bad. */ | |
240 | gd->flags &= ~GD_FLG_ENV_READY; | |
2d7cb5b4 | 241 | gd->env_valid = ENV_INVALID; |
00caae6d | 242 | ret_val = env_get(name); |
267541f7 JH |
243 | gd->env_valid = really_valid; |
244 | gd->flags = real_gd_flags; | |
245 | return ret_val; | |
246 | } | |
247 | ||
0ac7d722 | 248 | void env_set_default(const char *s, int flags) |
5bb12dbd HW |
249 | { |
250 | if (sizeof(default_environment) > ENV_SIZE) { | |
ea882baf | 251 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
252 | return; |
253 | } | |
254 | ||
ea882baf | 255 | if (s) { |
c5d548a9 | 256 | if ((flags & H_INTERACTIVE) == 0) { |
ea882baf | 257 | printf("*** Warning - %s, " |
c5d548a9 | 258 | "using default environment\n\n", s); |
ea882baf WD |
259 | } else { |
260 | puts(s); | |
261 | } | |
262 | } else { | |
58ae9990 | 263 | debug("Using default environment\n"); |
ea882baf WD |
264 | } |
265 | ||
ef9bef2b | 266 | flags |= H_DEFAULT; |
2eb1573f | 267 | if (himport_r(&env_htab, (char *)default_environment, |
ecd1446f | 268 | sizeof(default_environment), '\0', flags, 0, |
c4e0057f | 269 | 0, NULL) == 0) |
6c90f623 QS |
270 | pr_err("## Error: Environment import failed: errno = %d\n", |
271 | errno); | |
27aafe98 | 272 | |
ea882baf | 273 | gd->flags |= GD_FLG_ENV_READY; |
340b0e3b | 274 | gd->flags |= GD_FLG_ENV_DEFAULT; |
5bb12dbd HW |
275 | } |
276 | ||
b64b7c3d GF |
277 | |
278 | /* [re]set individual variables to their value in the default environment */ | |
0b9d8a05 | 279 | int env_set_default_vars(int nvars, char * const vars[], int flags) |
b64b7c3d GF |
280 | { |
281 | /* | |
282 | * Special use-case: import from default environment | |
283 | * (and use \0 as a separator) | |
284 | */ | |
ef9bef2b | 285 | flags |= H_NOCLEAR | H_DEFAULT; |
b64b7c3d | 286 | return himport_r(&env_htab, (const char *)default_environment, |
c4e0057f | 287 | sizeof(default_environment), '\0', |
477f8116 | 288 | flags, 0, nvars, vars); |
b64b7c3d GF |
289 | } |
290 | ||
ea882baf WD |
291 | /* |
292 | * Check if CRC is valid and (if yes) import the environment. | |
293 | * Note that "buf" may or may not be aligned. | |
294 | */ | |
890feeca | 295 | int env_import(const char *buf, int check, int flags) |
05706fdd | 296 | { |
ea882baf | 297 | env_t *ep = (env_t *)buf; |
05706fdd | 298 | |
ea882baf WD |
299 | if (check) { |
300 | uint32_t crc; | |
301 | ||
302 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
303 | ||
304 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
0ac7d722 | 305 | env_set_default("bad CRC", 0); |
49d04c58 | 306 | return -ENOMSG; /* needed for env_load() */ |
ea882baf WD |
307 | } |
308 | } | |
309 | ||
890feeca | 310 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0, |
c4e0057f | 311 | 0, NULL)) { |
ea882baf | 312 | gd->flags |= GD_FLG_ENV_READY; |
42a1820b | 313 | return 0; |
ea882baf | 314 | } |
05706fdd | 315 | |
9b643e31 | 316 | pr_err("Cannot import environment: errno = %d\n", errno); |
ea882baf | 317 | |
0ac7d722 | 318 | env_set_default("import failed", 0); |
ea882baf | 319 | |
42a1820b | 320 | return -EIO; |
ea882baf WD |
321 | } |
322 | ||
76768f5f FA |
323 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
324 | static unsigned char env_flags; | |
325 | ||
1229533a HS |
326 | int env_check_redund(const char *buf1, int buf1_read_fail, |
327 | const char *buf2, int buf2_read_fail) | |
76768f5f | 328 | { |
b9c3052f | 329 | int crc1_ok = 0, crc2_ok = 0; |
1229533a | 330 | env_t *tmp_env1, *tmp_env2; |
76768f5f FA |
331 | |
332 | tmp_env1 = (env_t *)buf1; | |
333 | tmp_env2 = (env_t *)buf2; | |
334 | ||
31f044bd SG |
335 | if (buf1_read_fail && buf2_read_fail) { |
336 | puts("*** Error - No Valid Environment Area found\n"); | |
b9c3052f | 337 | return -EIO; |
31f044bd SG |
338 | } else if (buf1_read_fail || buf2_read_fail) { |
339 | puts("*** Warning - some problems detected "); | |
340 | puts("reading environment; recovered successfully\n"); | |
341 | } | |
342 | ||
b9c3052f BM |
343 | if (!buf1_read_fail) |
344 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == | |
345 | tmp_env1->crc; | |
346 | if (!buf2_read_fail) | |
347 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == | |
348 | tmp_env2->crc; | |
76768f5f FA |
349 | |
350 | if (!crc1_ok && !crc2_ok) { | |
49d04c58 | 351 | return -ENOMSG; /* needed for env_load() */ |
76768f5f | 352 | } else if (crc1_ok && !crc2_ok) { |
2d7cb5b4 | 353 | gd->env_valid = ENV_VALID; |
76768f5f | 354 | } else if (!crc1_ok && crc2_ok) { |
2d7cb5b4 | 355 | gd->env_valid = ENV_REDUND; |
76768f5f FA |
356 | } else { |
357 | /* both ok - check serial */ | |
358 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
2d7cb5b4 | 359 | gd->env_valid = ENV_REDUND; |
76768f5f | 360 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
2d7cb5b4 | 361 | gd->env_valid = ENV_VALID; |
76768f5f | 362 | else if (tmp_env1->flags > tmp_env2->flags) |
2d7cb5b4 | 363 | gd->env_valid = ENV_VALID; |
76768f5f | 364 | else if (tmp_env2->flags > tmp_env1->flags) |
2d7cb5b4 | 365 | gd->env_valid = ENV_REDUND; |
76768f5f | 366 | else /* flags are equal - almost impossible */ |
2d7cb5b4 | 367 | gd->env_valid = ENV_VALID; |
76768f5f FA |
368 | } |
369 | ||
1229533a HS |
370 | return 0; |
371 | } | |
372 | ||
373 | int env_import_redund(const char *buf1, int buf1_read_fail, | |
374 | const char *buf2, int buf2_read_fail, | |
375 | int flags) | |
376 | { | |
377 | env_t *ep; | |
378 | int ret; | |
379 | ||
380 | ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail); | |
381 | ||
382 | if (ret == -EIO) { | |
383 | env_set_default("bad env area", 0); | |
384 | return -EIO; | |
1229533a HS |
385 | } else if (ret == -ENOMSG) { |
386 | env_set_default("bad CRC", 0); | |
387 | return -ENOMSG; | |
388 | } | |
389 | ||
2d7cb5b4 | 390 | if (gd->env_valid == ENV_VALID) |
1229533a | 391 | ep = (env_t *)buf1; |
76768f5f | 392 | else |
1229533a | 393 | ep = (env_t *)buf2; |
76768f5f FA |
394 | |
395 | env_flags = ep->flags; | |
1229533a | 396 | |
890feeca | 397 | return env_import((char *)ep, 0, flags); |
76768f5f FA |
398 | } |
399 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
400 | ||
fc0b5948 | 401 | /* Export the environment and generate CRC for it. */ |
7ce1526e MV |
402 | int env_export(env_t *env_out) |
403 | { | |
404 | char *res; | |
405 | ssize_t len; | |
406 | ||
407 | res = (char *)env_out->data; | |
408 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); | |
409 | if (len < 0) { | |
9b643e31 | 410 | pr_err("Cannot export environment: errno = %d\n", errno); |
7ce1526e MV |
411 | return 1; |
412 | } | |
a4223b74 | 413 | |
7ce1526e MV |
414 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
415 | ||
76768f5f FA |
416 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
417 | env_out->flags = ++env_flags; /* increase the serial */ | |
418 | #endif | |
419 | ||
7ce1526e MV |
420 | return 0; |
421 | } | |
422 | ||
27aafe98 | 423 | void env_relocate(void) |
ea882baf | 424 | { |
2e5167cc | 425 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f | 426 | env_reloc(); |
7bcdf195 | 427 | env_fix_drivers(); |
d90fc9c3 | 428 | env_htab.change_ok += gd->reloc_off; |
60f7da1f | 429 | #endif |
2d7cb5b4 | 430 | if (gd->env_valid == ENV_INVALID) { |
7ac2fe2d IY |
431 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
432 | /* Environment not changable */ | |
0ac7d722 | 433 | env_set_default(NULL, 0); |
05706fdd | 434 | #else |
770605e4 | 435 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
0ac7d722 | 436 | env_set_default("bad CRC", 0); |
d259079d | 437 | #endif |
ea882baf | 438 | } else { |
310fb14b | 439 | env_load(); |
05706fdd | 440 | } |
05706fdd | 441 | } |
04a85b3b | 442 | |
03dcf17d BB |
443 | #ifdef CONFIG_AUTO_COMPLETE |
444 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf, | |
445 | bool dollar_comp) | |
04a85b3b | 446 | { |
dd2408ca | 447 | struct env_entry *match; |
560d424b | 448 | int found, idx; |
04a85b3b | 449 | |
03dcf17d BB |
450 | if (dollar_comp) { |
451 | /* | |
452 | * When doing $ completion, the first character should | |
453 | * obviously be a '$'. | |
454 | */ | |
455 | if (var[0] != '$') | |
456 | return 0; | |
457 | ||
458 | var++; | |
459 | ||
460 | /* | |
461 | * The second one, if present, should be a '{', as some | |
462 | * configuration of the u-boot shell expand ${var} but not | |
463 | * $var. | |
464 | */ | |
465 | if (var[0] == '{') | |
466 | var++; | |
467 | else if (var[0] != '\0') | |
468 | return 0; | |
469 | } | |
470 | ||
560d424b | 471 | idx = 0; |
04a85b3b WD |
472 | found = 0; |
473 | cmdv[0] = NULL; | |
474 | ||
03dcf17d | 475 | |
560d424b MF |
476 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
477 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 478 | |
03dcf17d BB |
479 | if (found >= maxv - 2 || |
480 | bufsz < vallen + (dollar_comp ? 3 : 0)) | |
04a85b3b | 481 | break; |
560d424b | 482 | |
04a85b3b | 483 | cmdv[found++] = buf; |
03dcf17d BB |
484 | |
485 | /* Add the '${' prefix to each var when doing $ completion. */ | |
486 | if (dollar_comp) { | |
487 | strcpy(buf, "${"); | |
488 | buf += 2; | |
489 | bufsz -= 3; | |
490 | } | |
491 | ||
560d424b MF |
492 | memcpy(buf, match->key, vallen); |
493 | buf += vallen; | |
494 | bufsz -= vallen; | |
03dcf17d BB |
495 | |
496 | if (dollar_comp) { | |
497 | /* | |
498 | * This one is a bit odd: vallen already contains the | |
499 | * '\0' character but we need to add the '}' suffix, | |
500 | * hence the buf - 1 here. strcpy() will add the '\0' | |
501 | * character just after '}'. buf is then incremented | |
502 | * to account for the extra '}' we just added. | |
503 | */ | |
504 | strcpy(buf - 1, "}"); | |
505 | buf++; | |
506 | } | |
04a85b3b WD |
507 | } |
508 | ||
560d424b MF |
509 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
510 | ||
511 | if (idx) | |
03dcf17d | 512 | cmdv[found++] = dollar_comp ? "${...}" : "..."; |
27aafe98 | 513 | |
04a85b3b WD |
514 | cmdv[found] = NULL; |
515 | return found; | |
516 | } | |
517 | #endif | |
95fd9772 RV |
518 | |
519 | #ifdef CONFIG_ENV_IMPORT_FDT | |
520 | void env_import_fdt(void) | |
521 | { | |
522 | const char *path; | |
523 | struct ofprop prop; | |
524 | ofnode node; | |
525 | int res; | |
526 | ||
527 | path = env_get("env_fdt_path"); | |
528 | if (!path || !path[0]) | |
529 | return; | |
530 | ||
531 | node = ofnode_path(path); | |
532 | if (!ofnode_valid(node)) { | |
533 | printf("Warning: device tree node '%s' not found\n", path); | |
534 | return; | |
535 | } | |
536 | ||
537 | for (res = ofnode_get_first_property(node, &prop); | |
538 | !res; | |
539 | res = ofnode_get_next_property(&prop)) { | |
540 | const char *name, *val; | |
541 | ||
542 | val = ofnode_get_property_by_prop(&prop, &name, NULL); | |
543 | env_set(name, val); | |
544 | } | |
545 | } | |
546 | #endif |