]>
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 | ||
52f24238 | 10 | #include <bootstage.h> |
05706fdd | 11 | #include <command.h> |
3f989e7b | 12 | #include <env.h> |
f3998fdc | 13 | #include <env_internal.h> |
f7ae49fc | 14 | #include <log.h> |
8bef79bf | 15 | #include <sort.h> |
401d1c4f | 16 | #include <asm/global_data.h> |
1e94b46f | 17 | #include <linux/printk.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 | 38 | /* |
d9721925 TR |
39 | * This variable is incremented each time we set an environment variable so we |
40 | * can be check via env_get_id() to see if the environment has changed or not. | |
41 | * This makes it possible to reread an environment variable only if the | |
42 | * environment was changed, typically used by networking code. | |
f2315664 | 43 | */ |
d9721925 TR |
44 | static int env_id = 1; |
45 | ||
46 | int env_get_id(void) | |
47 | { | |
48 | return env_id; | |
49 | } | |
50 | ||
51 | void env_inc_id(void) | |
52 | { | |
53 | env_id++; | |
54 | } | |
55 | ||
56 | int env_do_env_set(int flag, int argc, char *const argv[], int env_flag) | |
57 | { | |
58 | int i, len; | |
59 | char *name, *value, *s; | |
60 | struct env_entry e, *ep; | |
61 | ||
62 | debug("Initial value for argc=%d\n", argc); | |
63 | ||
64 | #if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI) | |
65 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') | |
66 | return do_env_set_efi(NULL, flag, --argc, ++argv); | |
67 | #endif | |
68 | ||
69 | while (argc > 1 && **(argv + 1) == '-') { | |
70 | char *arg = *++argv; | |
71 | ||
72 | --argc; | |
73 | while (*++arg) { | |
74 | switch (*arg) { | |
75 | case 'f': /* force */ | |
76 | env_flag |= H_FORCE; | |
77 | break; | |
78 | default: | |
79 | return CMD_RET_USAGE; | |
80 | } | |
81 | } | |
82 | } | |
83 | debug("Final value for argc=%d\n", argc); | |
84 | name = argv[1]; | |
85 | ||
86 | if (strchr(name, '=')) { | |
3c3270b5 | 87 | printf("## Error: illegal character '=' " |
d9721925 TR |
88 | "in variable name \"%s\"\n", name); |
89 | return 1; | |
90 | } | |
91 | ||
92 | env_inc_id(); | |
93 | ||
94 | /* Delete only ? */ | |
95 | if (argc < 3 || argv[2] == NULL) { | |
96 | int rc = hdelete_r(name, &env_htab, env_flag); | |
97 | ||
98 | /* If the variable didn't exist, don't report an error */ | |
99 | return rc && rc != -ENOENT ? 1 : 0; | |
100 | } | |
101 | ||
102 | /* | |
103 | * Insert / replace new value | |
104 | */ | |
105 | for (i = 2, len = 0; i < argc; ++i) | |
106 | len += strlen(argv[i]) + 1; | |
107 | ||
108 | value = malloc(len); | |
109 | if (value == NULL) { | |
110 | printf("## Can't malloc %d bytes\n", len); | |
111 | return 1; | |
112 | } | |
113 | for (i = 2, s = value; i < argc; ++i) { | |
114 | char *v = argv[i]; | |
115 | ||
116 | while ((*s++ = *v++) != '\0') | |
117 | ; | |
118 | *(s - 1) = ' '; | |
119 | } | |
120 | if (s != value) | |
121 | *--s = '\0'; | |
122 | ||
123 | e.key = name; | |
124 | e.data = value; | |
125 | hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag); | |
126 | free(value); | |
127 | if (!ep) { | |
128 | printf("## Error inserting \"%s\" variable, errno=%d\n", | |
129 | name, errno); | |
130 | return 1; | |
131 | } | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | int env_set(const char *varname, const char *varvalue) | |
137 | { | |
138 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; | |
139 | ||
140 | /* before import into hashtable */ | |
141 | if (!(gd->flags & GD_FLG_ENV_READY)) | |
142 | return 1; | |
143 | ||
144 | if (varvalue == NULL || varvalue[0] == '\0') | |
145 | return env_do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); | |
146 | else | |
147 | return env_do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); | |
148 | } | |
f2315664 MB |
149 | |
150 | /** | |
151 | * Set an environment variable to an integer value | |
152 | * | |
153 | * @param varname Environment variable to set | |
154 | * @param value Value to set it to | |
185f812c | 155 | * Return: 0 if ok, 1 on error |
f2315664 MB |
156 | */ |
157 | int env_set_ulong(const char *varname, ulong value) | |
158 | { | |
159 | /* TODO: this should be unsigned */ | |
160 | char *str = simple_itoa(value); | |
161 | ||
162 | return env_set(varname, str); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Set an environment variable to an value in hex | |
167 | * | |
168 | * @param varname Environment variable to set | |
169 | * @param value Value to set it to | |
185f812c | 170 | * Return: 0 if ok, 1 on error |
f2315664 MB |
171 | */ |
172 | int env_set_hex(const char *varname, ulong value) | |
173 | { | |
174 | char str[17]; | |
175 | ||
176 | sprintf(str, "%lx", value); | |
177 | return env_set(varname, str); | |
178 | } | |
179 | ||
180 | ulong env_get_hex(const char *varname, ulong default_val) | |
181 | { | |
182 | const char *s; | |
183 | ulong value; | |
184 | char *endp; | |
185 | ||
186 | s = env_get(varname); | |
187 | if (s) | |
188 | value = hextoul(s, &endp); | |
189 | if (!s || endp == s) | |
190 | return default_val; | |
191 | ||
192 | return value; | |
193 | } | |
194 | ||
195 | int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) | |
196 | { | |
197 | string_to_enetaddr(env_get(name), enetaddr); | |
198 | return is_valid_ethaddr(enetaddr); | |
199 | } | |
200 | ||
201 | int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) | |
202 | { | |
203 | char buf[ARP_HLEN_ASCII + 1]; | |
204 | ||
205 | if (eth_env_get_enetaddr(name, (uint8_t *)buf)) | |
206 | return -EEXIST; | |
207 | ||
208 | sprintf(buf, "%pM", enetaddr); | |
209 | ||
210 | return env_set(name, buf); | |
211 | } | |
212 | ||
213 | /* | |
214 | * Look up variable from environment, | |
215 | * return address of storage for that variable, | |
216 | * or NULL if not found | |
217 | */ | |
218 | char *env_get(const char *name) | |
219 | { | |
220 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ | |
221 | struct env_entry e, *ep; | |
222 | ||
29caf930 | 223 | schedule(); |
f2315664 MB |
224 | |
225 | e.key = name; | |
226 | e.data = NULL; | |
227 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); | |
228 | ||
229 | return ep ? ep->data : NULL; | |
230 | } | |
231 | ||
232 | /* restricted capabilities before import */ | |
e8459c12 | 233 | if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0) |
f2315664 MB |
234 | return (char *)(gd->env_buf); |
235 | ||
236 | return NULL; | |
237 | } | |
238 | ||
239 | /* | |
240 | * Like env_get, but prints an error if envvar isn't defined in the | |
241 | * environment. It always returns what env_get does, so it can be used in | |
242 | * place of env_get without changing error handling otherwise. | |
243 | */ | |
244 | char *from_env(const char *envvar) | |
245 | { | |
246 | char *ret; | |
247 | ||
248 | ret = env_get(envvar); | |
249 | ||
250 | if (!ret) | |
251 | printf("missing environment variable: %s\n", envvar); | |
252 | ||
253 | return ret; | |
254 | } | |
255 | ||
4e7c8b2a MB |
256 | static int env_get_from_linear(const char *env, const char *name, char *buf, |
257 | unsigned len) | |
f2315664 | 258 | { |
4e7c8b2a | 259 | const char *p, *end; |
f2315664 MB |
260 | size_t name_len; |
261 | ||
262 | if (name == NULL || *name == '\0') | |
263 | return -1; | |
264 | ||
265 | name_len = strlen(name); | |
266 | ||
f2315664 MB |
267 | for (p = env; *p != '\0'; p = end + 1) { |
268 | const char *value; | |
269 | unsigned res; | |
270 | ||
271 | for (end = p; *end != '\0'; ++end) | |
272 | if (end - env >= CONFIG_ENV_SIZE) | |
273 | return -1; | |
274 | ||
275 | if (strncmp(name, p, name_len) || p[name_len] != '=') | |
276 | continue; | |
277 | value = &p[name_len + 1]; | |
278 | ||
279 | res = end - value; | |
280 | memcpy(buf, value, min(len, res + 1)); | |
281 | ||
282 | if (len <= res) { | |
283 | buf[len - 1] = '\0'; | |
284 | printf("env_buf [%u bytes] too small for value of \"%s\"\n", | |
285 | len, name); | |
286 | } | |
287 | ||
288 | return res; | |
289 | } | |
290 | ||
291 | return -1; | |
292 | } | |
293 | ||
4e7c8b2a MB |
294 | /* |
295 | * Look up variable from environment for restricted C runtime env. | |
296 | */ | |
297 | int env_get_f(const char *name, char *buf, unsigned len) | |
298 | { | |
299 | const char *env; | |
300 | ||
301 | if (gd->env_valid == ENV_INVALID) | |
302 | env = default_environment; | |
303 | else | |
304 | env = (const char *)gd->env_addr; | |
305 | ||
306 | return env_get_from_linear(env, name, buf, len); | |
307 | } | |
308 | ||
f2315664 MB |
309 | /** |
310 | * Decode the integer value of an environment variable and return it. | |
311 | * | |
312 | * @param name Name of environment variable | |
313 | * @param base Number base to use (normally 10, or 16 for hex) | |
314 | * @param default_val Default value to return if the variable is not | |
315 | * found | |
185f812c | 316 | * Return: the decoded value, or default_val if not found |
f2315664 MB |
317 | */ |
318 | ulong env_get_ulong(const char *name, int base, ulong default_val) | |
319 | { | |
320 | /* | |
321 | * We can use env_get() here, even before relocation, since the | |
322 | * environment variable value is an integer and thus short. | |
323 | */ | |
324 | const char *str = env_get(name); | |
325 | ||
326 | return str ? simple_strtoul(str, NULL, base) : default_val; | |
327 | } | |
328 | ||
ec8a252c JH |
329 | /* |
330 | * Read an environment variable as a boolean | |
331 | * Return -1 if variable does not exist (default to true) | |
332 | */ | |
bfebc8c9 | 333 | int env_get_yesno(const char *var) |
ec8a252c | 334 | { |
00caae6d | 335 | char *s = env_get(var); |
ec8a252c JH |
336 | |
337 | if (s == NULL) | |
338 | return -1; | |
339 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? | |
340 | 1 : 0; | |
341 | } | |
342 | ||
78398652 SG |
343 | bool env_get_autostart(void) |
344 | { | |
345 | return env_get_yesno("autostart") == 1; | |
346 | } | |
347 | ||
267541f7 JH |
348 | /* |
349 | * Look up the variable from the default environment | |
350 | */ | |
723806cc | 351 | char *env_get_default(const char *name) |
267541f7 | 352 | { |
87d3c472 QS |
353 | int ret; |
354 | ||
355 | ret = env_get_default_into(name, (char *)(gd->env_buf), | |
356 | sizeof(gd->env_buf)); | |
357 | if (ret >= 0) | |
4e7c8b2a MB |
358 | return (char *)(gd->env_buf); |
359 | ||
360 | return NULL; | |
267541f7 JH |
361 | } |
362 | ||
23c1ab92 QS |
363 | /* |
364 | * Look up the variable from the default environment and store its value in buf | |
365 | */ | |
366 | int env_get_default_into(const char *name, char *buf, unsigned int len) | |
367 | { | |
368 | return env_get_from_linear(default_environment, name, buf, len); | |
369 | } | |
370 | ||
0ac7d722 | 371 | void env_set_default(const char *s, int flags) |
5bb12dbd | 372 | { |
ea882baf | 373 | if (s) { |
c5d548a9 | 374 | if ((flags & H_INTERACTIVE) == 0) { |
ea882baf | 375 | printf("*** Warning - %s, " |
c5d548a9 | 376 | "using default environment\n\n", s); |
ea882baf WD |
377 | } else { |
378 | puts(s); | |
379 | } | |
380 | } else { | |
58ae9990 | 381 | debug("Using default environment\n"); |
ea882baf WD |
382 | } |
383 | ||
ef9bef2b | 384 | flags |= H_DEFAULT; |
c5cbbe35 | 385 | if (himport_r(&env_htab, default_environment, |
ecd1446f | 386 | sizeof(default_environment), '\0', flags, 0, |
c9db4c54 | 387 | 0, NULL) == 0) { |
6c90f623 QS |
388 | pr_err("## Error: Environment import failed: errno = %d\n", |
389 | errno); | |
c9db4c54 MB |
390 | return; |
391 | } | |
27aafe98 | 392 | |
ea882baf | 393 | gd->flags |= GD_FLG_ENV_READY; |
340b0e3b | 394 | gd->flags |= GD_FLG_ENV_DEFAULT; |
5bb12dbd HW |
395 | } |
396 | ||
b64b7c3d | 397 | /* [re]set individual variables to their value in the default environment */ |
0b9d8a05 | 398 | int env_set_default_vars(int nvars, char * const vars[], int flags) |
b64b7c3d GF |
399 | { |
400 | /* | |
401 | * Special use-case: import from default environment | |
402 | * (and use \0 as a separator) | |
403 | */ | |
2a521d01 RM |
404 | |
405 | /* | |
406 | * When vars are passed remove variables that are not in | |
407 | * the default environment. | |
408 | */ | |
409 | if (!nvars) | |
410 | flags |= H_NOCLEAR; | |
411 | ||
412 | flags |= H_DEFAULT; | |
c5cbbe35 | 413 | return himport_r(&env_htab, default_environment, |
c4e0057f | 414 | sizeof(default_environment), '\0', |
477f8116 | 415 | flags, 0, nvars, vars); |
b64b7c3d GF |
416 | } |
417 | ||
ea882baf WD |
418 | /* |
419 | * Check if CRC is valid and (if yes) import the environment. | |
420 | * Note that "buf" may or may not be aligned. | |
421 | */ | |
890feeca | 422 | int env_import(const char *buf, int check, int flags) |
05706fdd | 423 | { |
ea882baf | 424 | env_t *ep = (env_t *)buf; |
05706fdd | 425 | |
ea882baf WD |
426 | if (check) { |
427 | uint32_t crc; | |
428 | ||
429 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
430 | ||
431 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
0ac7d722 | 432 | env_set_default("bad CRC", 0); |
49d04c58 | 433 | return -ENOMSG; /* needed for env_load() */ |
ea882baf WD |
434 | } |
435 | } | |
436 | ||
890feeca | 437 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0, |
c4e0057f | 438 | 0, NULL)) { |
ea882baf | 439 | gd->flags |= GD_FLG_ENV_READY; |
42a1820b | 440 | return 0; |
ea882baf | 441 | } |
05706fdd | 442 | |
9b643e31 | 443 | pr_err("Cannot import environment: errno = %d\n", errno); |
ea882baf | 444 | |
0ac7d722 | 445 | env_set_default("import failed", 0); |
ea882baf | 446 | |
42a1820b | 447 | return -EIO; |
ea882baf WD |
448 | } |
449 | ||
76768f5f FA |
450 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
451 | static unsigned char env_flags; | |
452 | ||
1229533a HS |
453 | int env_check_redund(const char *buf1, int buf1_read_fail, |
454 | const char *buf2, int buf2_read_fail) | |
76768f5f | 455 | { |
b9c3052f | 456 | int crc1_ok = 0, crc2_ok = 0; |
1229533a | 457 | env_t *tmp_env1, *tmp_env2; |
76768f5f FA |
458 | |
459 | tmp_env1 = (env_t *)buf1; | |
460 | tmp_env2 = (env_t *)buf2; | |
461 | ||
31f044bd SG |
462 | if (buf1_read_fail && buf2_read_fail) { |
463 | puts("*** Error - No Valid Environment Area found\n"); | |
b9c3052f | 464 | return -EIO; |
31f044bd SG |
465 | } else if (buf1_read_fail || buf2_read_fail) { |
466 | puts("*** Warning - some problems detected "); | |
467 | puts("reading environment; recovered successfully\n"); | |
468 | } | |
469 | ||
b9c3052f BM |
470 | if (!buf1_read_fail) |
471 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == | |
472 | tmp_env1->crc; | |
473 | if (!buf2_read_fail) | |
474 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == | |
475 | tmp_env2->crc; | |
76768f5f FA |
476 | |
477 | if (!crc1_ok && !crc2_ok) { | |
4dc5e262 | 478 | gd->env_valid = ENV_INVALID; |
49d04c58 | 479 | return -ENOMSG; /* needed for env_load() */ |
76768f5f | 480 | } else if (crc1_ok && !crc2_ok) { |
2d7cb5b4 | 481 | gd->env_valid = ENV_VALID; |
76768f5f | 482 | } else if (!crc1_ok && crc2_ok) { |
2d7cb5b4 | 483 | gd->env_valid = ENV_REDUND; |
76768f5f FA |
484 | } else { |
485 | /* both ok - check serial */ | |
486 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
2d7cb5b4 | 487 | gd->env_valid = ENV_REDUND; |
76768f5f | 488 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
2d7cb5b4 | 489 | gd->env_valid = ENV_VALID; |
76768f5f | 490 | else if (tmp_env1->flags > tmp_env2->flags) |
2d7cb5b4 | 491 | gd->env_valid = ENV_VALID; |
76768f5f | 492 | else if (tmp_env2->flags > tmp_env1->flags) |
2d7cb5b4 | 493 | gd->env_valid = ENV_REDUND; |
76768f5f | 494 | else /* flags are equal - almost impossible */ |
2d7cb5b4 | 495 | gd->env_valid = ENV_VALID; |
76768f5f FA |
496 | } |
497 | ||
1229533a HS |
498 | return 0; |
499 | } | |
500 | ||
501 | int env_import_redund(const char *buf1, int buf1_read_fail, | |
502 | const char *buf2, int buf2_read_fail, | |
503 | int flags) | |
504 | { | |
505 | env_t *ep; | |
506 | int ret; | |
507 | ||
508 | ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail); | |
509 | ||
510 | if (ret == -EIO) { | |
511 | env_set_default("bad env area", 0); | |
512 | return -EIO; | |
1229533a HS |
513 | } else if (ret == -ENOMSG) { |
514 | env_set_default("bad CRC", 0); | |
515 | return -ENOMSG; | |
516 | } | |
517 | ||
2d7cb5b4 | 518 | if (gd->env_valid == ENV_VALID) |
1229533a | 519 | ep = (env_t *)buf1; |
76768f5f | 520 | else |
1229533a | 521 | ep = (env_t *)buf2; |
76768f5f FA |
522 | |
523 | env_flags = ep->flags; | |
1229533a | 524 | |
890feeca | 525 | return env_import((char *)ep, 0, flags); |
76768f5f FA |
526 | } |
527 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
528 | ||
fc0b5948 | 529 | /* Export the environment and generate CRC for it. */ |
7ce1526e MV |
530 | int env_export(env_t *env_out) |
531 | { | |
532 | char *res; | |
533 | ssize_t len; | |
534 | ||
535 | res = (char *)env_out->data; | |
536 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); | |
537 | if (len < 0) { | |
9b643e31 | 538 | pr_err("Cannot export environment: errno = %d\n", errno); |
7ce1526e MV |
539 | return 1; |
540 | } | |
a4223b74 | 541 | |
7ce1526e MV |
542 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
543 | ||
76768f5f FA |
544 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
545 | env_out->flags = ++env_flags; /* increase the serial */ | |
546 | #endif | |
547 | ||
7ce1526e MV |
548 | return 0; |
549 | } | |
550 | ||
27aafe98 | 551 | void env_relocate(void) |
ea882baf | 552 | { |
2d7cb5b4 | 553 | if (gd->env_valid == ENV_INVALID) { |
7ac2fe2d IY |
554 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
555 | /* Environment not changable */ | |
0ac7d722 | 556 | env_set_default(NULL, 0); |
05706fdd | 557 | #else |
770605e4 | 558 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
0ac7d722 | 559 | env_set_default("bad CRC", 0); |
d259079d | 560 | #endif |
ea882baf | 561 | } else { |
310fb14b | 562 | env_load(); |
05706fdd | 563 | } |
05706fdd | 564 | } |
04a85b3b | 565 | |
03dcf17d BB |
566 | #ifdef CONFIG_AUTO_COMPLETE |
567 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf, | |
568 | bool dollar_comp) | |
04a85b3b | 569 | { |
dd2408ca | 570 | struct env_entry *match; |
560d424b | 571 | int found, idx; |
04a85b3b | 572 | |
03dcf17d BB |
573 | if (dollar_comp) { |
574 | /* | |
575 | * When doing $ completion, the first character should | |
576 | * obviously be a '$'. | |
577 | */ | |
578 | if (var[0] != '$') | |
579 | return 0; | |
580 | ||
581 | var++; | |
582 | ||
583 | /* | |
584 | * The second one, if present, should be a '{', as some | |
585 | * configuration of the u-boot shell expand ${var} but not | |
586 | * $var. | |
587 | */ | |
588 | if (var[0] == '{') | |
589 | var++; | |
590 | else if (var[0] != '\0') | |
591 | return 0; | |
592 | } | |
593 | ||
560d424b | 594 | idx = 0; |
04a85b3b WD |
595 | found = 0; |
596 | cmdv[0] = NULL; | |
597 | ||
560d424b MF |
598 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
599 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 600 | |
03dcf17d BB |
601 | if (found >= maxv - 2 || |
602 | bufsz < vallen + (dollar_comp ? 3 : 0)) | |
04a85b3b | 603 | break; |
560d424b | 604 | |
04a85b3b | 605 | cmdv[found++] = buf; |
03dcf17d BB |
606 | |
607 | /* Add the '${' prefix to each var when doing $ completion. */ | |
608 | if (dollar_comp) { | |
609 | strcpy(buf, "${"); | |
610 | buf += 2; | |
611 | bufsz -= 3; | |
612 | } | |
613 | ||
560d424b MF |
614 | memcpy(buf, match->key, vallen); |
615 | buf += vallen; | |
616 | bufsz -= vallen; | |
03dcf17d BB |
617 | |
618 | if (dollar_comp) { | |
619 | /* | |
620 | * This one is a bit odd: vallen already contains the | |
621 | * '\0' character but we need to add the '}' suffix, | |
622 | * hence the buf - 1 here. strcpy() will add the '\0' | |
623 | * character just after '}'. buf is then incremented | |
624 | * to account for the extra '}' we just added. | |
625 | */ | |
626 | strcpy(buf - 1, "}"); | |
627 | buf++; | |
628 | } | |
04a85b3b WD |
629 | } |
630 | ||
560d424b MF |
631 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
632 | ||
633 | if (idx) | |
03dcf17d | 634 | cmdv[found++] = dollar_comp ? "${...}" : "..."; |
27aafe98 | 635 | |
04a85b3b WD |
636 | cmdv[found] = NULL; |
637 | return found; | |
638 | } | |
639 | #endif | |
95fd9772 RV |
640 | |
641 | #ifdef CONFIG_ENV_IMPORT_FDT | |
642 | void env_import_fdt(void) | |
643 | { | |
644 | const char *path; | |
645 | struct ofprop prop; | |
646 | ofnode node; | |
647 | int res; | |
648 | ||
649 | path = env_get("env_fdt_path"); | |
650 | if (!path || !path[0]) | |
651 | return; | |
652 | ||
653 | node = ofnode_path(path); | |
654 | if (!ofnode_valid(node)) { | |
655 | printf("Warning: device tree node '%s' not found\n", path); | |
656 | return; | |
657 | } | |
658 | ||
4b1f5714 | 659 | for (res = ofnode_first_property(node, &prop); |
95fd9772 | 660 | !res; |
4b1f5714 | 661 | res = ofnode_next_property(&prop)) { |
95fd9772 RV |
662 | const char *name, *val; |
663 | ||
92432246 | 664 | val = ofprop_get_property(&prop, &name, NULL); |
95fd9772 RV |
665 | env_set(name, val); |
666 | } | |
667 | } | |
668 | #endif |