]>
Commit | Line | Data |
---|---|---|
a68d3ed0 | 1 | /* |
ea009d47 | 2 | * (C) Copyright 2000-2013 |
a68d3ed0 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]> | |
a000b795 KP |
7 | * |
8 | * Copyright 2011 Freescale Semiconductor, Inc. | |
9 | * | |
3765b3e7 | 10 | * SPDX-License-Identifier: GPL-2.0+ |
a68d3ed0 WD |
11 | */ |
12 | ||
ea882baf | 13 | /* |
a68d3ed0 WD |
14 | * Support for persistent environment data |
15 | * | |
ea882baf WD |
16 | * The "environment" is stored on external storage as a list of '\0' |
17 | * terminated "name=value" strings. The end of the list is marked by | |
18 | * a double '\0'. The environment is preceeded by a 32 bit CRC over | |
19 | * the data part and, in case of redundant environment, a byte of | |
20 | * flags. | |
a68d3ed0 | 21 | * |
ea882baf WD |
22 | * This linearized representation will also be used before |
23 | * relocation, i. e. as long as we don't have a full C runtime | |
24 | * environment. After that, we use a hash table. | |
a68d3ed0 WD |
25 | */ |
26 | ||
27 | #include <common.h> | |
28 | #include <command.h> | |
29 | #include <environment.h> | |
ea882baf WD |
30 | #include <search.h> |
31 | #include <errno.h> | |
246c6922 | 32 | #include <malloc.h> |
2a3cb020 | 33 | #include <watchdog.h> |
a68d3ed0 WD |
34 | #include <linux/stddef.h> |
35 | #include <asm/byteorder.h> | |
a68d3ed0 | 36 | |
d87080b7 WD |
37 | DECLARE_GLOBAL_DATA_PTR; |
38 | ||
f3c615b8 ML |
39 | #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \ |
40 | !defined(CONFIG_ENV_IS_IN_FLASH) && \ | |
41 | !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \ | |
f3c615b8 | 42 | !defined(CONFIG_ENV_IS_IN_MMC) && \ |
57210c7c | 43 | !defined(CONFIG_ENV_IS_IN_FAT) && \ |
f3c615b8 ML |
44 | !defined(CONFIG_ENV_IS_IN_NAND) && \ |
45 | !defined(CONFIG_ENV_IS_IN_NVRAM) && \ | |
46 | !defined(CONFIG_ENV_IS_IN_ONENAND) && \ | |
47 | !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ | |
0a85a9e7 | 48 | !defined(CONFIG_ENV_IS_IN_REMOTE) && \ |
2b74433f | 49 | !defined(CONFIG_ENV_IS_IN_UBI) && \ |
f3c615b8 | 50 | !defined(CONFIG_ENV_IS_NOWHERE) |
75eb82ec | 51 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\ |
2b74433f | 52 | SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE |
a68d3ed0 WD |
53 | #endif |
54 | ||
ea882baf WD |
55 | /* |
56 | * Maximum expected input data size for import command | |
57 | */ | |
58 | #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ | |
a68d3ed0 | 59 | |
da95427c | 60 | /* |
ea882baf | 61 | * This variable is incremented on each do_env_set(), so it can |
da95427c HS |
62 | * be used via get_env_id() as an indication, if the environment |
63 | * has changed or not. So it is possible to reread an environment | |
64 | * variable only if the environment was changed ... done so for | |
65 | * example in NetInitLoop() | |
66 | */ | |
2f70c49e | 67 | static int env_id = 1; |
a68d3ed0 | 68 | |
f3c615b8 | 69 | int get_env_id(void) |
2f70c49e HS |
70 | { |
71 | return env_id; | |
72 | } | |
a68d3ed0 | 73 | |
7ac2fe2d | 74 | #ifndef CONFIG_SPL_BUILD |
4c94f6c5 | 75 | /* |
ea882baf WD |
76 | * Command interface: print one or all environment variables |
77 | * | |
78 | * Returns 0 in case of error, or length of printed string | |
4c94f6c5 | 79 | */ |
be11235a | 80 | static int env_print(char *name, int flag) |
a68d3ed0 | 81 | { |
ea882baf | 82 | char *res = NULL; |
22a4a6c5 | 83 | ssize_t len; |
ea882baf WD |
84 | |
85 | if (name) { /* print a single name */ | |
86 | ENTRY e, *ep; | |
87 | ||
88 | e.key = name; | |
89 | e.data = NULL; | |
be11235a | 90 | hsearch_r(e, FIND, &ep, &env_htab, flag); |
ea882baf WD |
91 | if (ep == NULL) |
92 | return 0; | |
f3c615b8 | 93 | len = printf("%s=%s\n", ep->key, ep->data); |
ea882baf WD |
94 | return len; |
95 | } | |
a68d3ed0 | 96 | |
ea882baf | 97 | /* print whole list */ |
be11235a | 98 | len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); |
a68d3ed0 | 99 | |
ea882baf WD |
100 | if (len > 0) { |
101 | puts(res); | |
102 | free(res); | |
103 | return len; | |
a68d3ed0 WD |
104 | } |
105 | ||
ea882baf | 106 | /* should never happen */ |
22a4a6c5 | 107 | printf("## Error: cannot export environment\n"); |
ea882baf | 108 | return 0; |
4c94f6c5 | 109 | } |
a68d3ed0 | 110 | |
088f1b19 KP |
111 | static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, |
112 | char * const argv[]) | |
4c94f6c5 MF |
113 | { |
114 | int i; | |
115 | int rcode = 0; | |
be11235a JH |
116 | int env_flag = H_HIDE_DOT; |
117 | ||
118 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') { | |
119 | argc--; | |
120 | argv++; | |
121 | env_flag &= ~H_HIDE_DOT; | |
122 | } | |
a68d3ed0 | 123 | |
4c94f6c5 MF |
124 | if (argc == 1) { |
125 | /* print all env vars */ | |
be11235a | 126 | rcode = env_print(NULL, env_flag); |
ea882baf | 127 | if (!rcode) |
4c94f6c5 MF |
128 | return 1; |
129 | printf("\nEnvironment size: %d/%ld bytes\n", | |
130 | rcode, (ulong)ENV_SIZE); | |
131 | return 0; | |
132 | } | |
a68d3ed0 | 133 | |
4c94f6c5 | 134 | /* print selected env vars */ |
be11235a | 135 | env_flag &= ~H_HIDE_DOT; |
4c94f6c5 | 136 | for (i = 1; i < argc; ++i) { |
be11235a | 137 | int rc = env_print(argv[i], env_flag); |
ea882baf WD |
138 | if (!rc) { |
139 | printf("## Error: \"%s\" not defined\n", argv[i]); | |
4c94f6c5 | 140 | ++rcode; |
a68d3ed0 WD |
141 | } |
142 | } | |
4c94f6c5 | 143 | |
a68d3ed0 WD |
144 | return rcode; |
145 | } | |
146 | ||
a000b795 | 147 | #ifdef CONFIG_CMD_GREPENV |
d09b1787 IG |
148 | static int do_env_grep(cmd_tbl_t *cmdtp, int flag, |
149 | int argc, char * const argv[]) | |
a000b795 | 150 | { |
5a31ea04 | 151 | char *res = NULL; |
be29df6a | 152 | int len, grep_how, grep_what; |
a000b795 KP |
153 | |
154 | if (argc < 2) | |
4c12eeb8 | 155 | return CMD_RET_USAGE; |
a000b795 | 156 | |
be29df6a WD |
157 | grep_how = H_MATCH_SUBSTR; /* default: substring search */ |
158 | grep_what = H_MATCH_BOTH; /* default: grep names and values */ | |
d87244d5 | 159 | |
9a832331 PA |
160 | while (--argc > 0 && **++argv == '-') { |
161 | char *arg = *argv; | |
d87244d5 WD |
162 | while (*++arg) { |
163 | switch (*arg) { | |
be29df6a WD |
164 | #ifdef CONFIG_REGEX |
165 | case 'e': /* use regex matching */ | |
166 | grep_how = H_MATCH_REGEX; | |
167 | break; | |
168 | #endif | |
d87244d5 | 169 | case 'n': /* grep for name */ |
be29df6a | 170 | grep_what = H_MATCH_KEY; |
d87244d5 WD |
171 | break; |
172 | case 'v': /* grep for value */ | |
be29df6a | 173 | grep_what = H_MATCH_DATA; |
d87244d5 WD |
174 | break; |
175 | case 'b': /* grep for both */ | |
be29df6a | 176 | grep_what = H_MATCH_BOTH; |
d87244d5 WD |
177 | break; |
178 | case '-': | |
179 | goto DONE; | |
180 | default: | |
181 | return CMD_RET_USAGE; | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | DONE: | |
5a31ea04 | 187 | len = hexport_r(&env_htab, '\n', |
be29df6a | 188 | flag | grep_what | grep_how, |
5a31ea04 | 189 | &res, 0, argc, argv); |
a000b795 | 190 | |
5a31ea04 WD |
191 | if (len > 0) { |
192 | puts(res); | |
193 | free(res); | |
a000b795 KP |
194 | } |
195 | ||
5a31ea04 WD |
196 | if (len < 2) |
197 | return 1; | |
198 | ||
199 | return 0; | |
a000b795 KP |
200 | } |
201 | #endif | |
7ac2fe2d | 202 | #endif /* CONFIG_SPL_BUILD */ |
a000b795 | 203 | |
c3f65258 GF |
204 | /* |
205 | * Set a new environment variable, | |
206 | * or replace or delete an existing one. | |
2598090b | 207 | */ |
088f1b19 | 208 | static int _do_env_set(int flag, int argc, char * const argv[]) |
c3f65258 GF |
209 | { |
210 | int i, len; | |
211 | char *name, *value, *s; | |
212 | ENTRY e, *ep; | |
24ab5a19 | 213 | int env_flag = H_INTERACTIVE; |
c3f65258 | 214 | |
24ab5a19 JH |
215 | debug("Initial value for argc=%d\n", argc); |
216 | while (argc > 1 && **(argv + 1) == '-') { | |
217 | char *arg = *++argv; | |
218 | ||
219 | --argc; | |
220 | while (*++arg) { | |
221 | switch (*arg) { | |
222 | case 'f': /* force */ | |
223 | env_flag |= H_FORCE; | |
224 | break; | |
225 | default: | |
226 | return CMD_RET_USAGE; | |
227 | } | |
228 | } | |
229 | } | |
230 | debug("Final value for argc=%d\n", argc); | |
c3f65258 GF |
231 | name = argv[1]; |
232 | value = argv[2]; | |
233 | ||
234 | if (strchr(name, '=')) { | |
235 | printf("## Error: illegal character '='" | |
236 | "in variable name \"%s\"\n", name); | |
237 | return 1; | |
238 | } | |
239 | ||
240 | env_id++; | |
c3f65258 | 241 | |
a68d3ed0 | 242 | /* Delete only ? */ |
d09b1787 | 243 | if (argc < 3 || argv[2] == NULL) { |
24ab5a19 | 244 | int rc = hdelete_r(name, &env_htab, env_flag); |
ea882baf | 245 | return !rc; |
a68d3ed0 WD |
246 | } |
247 | ||
248 | /* | |
ea882baf | 249 | * Insert / replace new value |
a68d3ed0 | 250 | */ |
f3c615b8 | 251 | for (i = 2, len = 0; i < argc; ++i) |
a68d3ed0 | 252 | len += strlen(argv[i]) + 1; |
f3c615b8 ML |
253 | |
254 | value = malloc(len); | |
255 | if (value == NULL) { | |
ea882baf | 256 | printf("## Can't malloc %d bytes\n", len); |
a68d3ed0 WD |
257 | return 1; |
258 | } | |
f3c615b8 | 259 | for (i = 2, s = value; i < argc; ++i) { |
ea882baf | 260 | char *v = argv[i]; |
a68d3ed0 | 261 | |
ea882baf | 262 | while ((*s++ = *v++) != '\0') |
a68d3ed0 | 263 | ; |
d09b1787 | 264 | *(s - 1) = ' '; |
ea882baf WD |
265 | } |
266 | if (s != value) | |
267 | *--s = '\0'; | |
268 | ||
d09b1787 IG |
269 | e.key = name; |
270 | e.data = value; | |
24ab5a19 | 271 | hsearch_r(e, ENTER, &ep, &env_htab, env_flag); |
ea882baf WD |
272 | free(value); |
273 | if (!ep) { | |
274 | printf("## Error inserting \"%s\" variable, errno=%d\n", | |
275 | name, errno); | |
276 | return 1; | |
a68d3ed0 | 277 | } |
a68d3ed0 | 278 | |
a68d3ed0 WD |
279 | return 0; |
280 | } | |
281 | ||
84b5e802 | 282 | int setenv(const char *varname, const char *varvalue) |
a68d3ed0 | 283 | { |
84b5e802 WD |
284 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
285 | ||
a7eb1d66 JH |
286 | /* before import into hashtable */ |
287 | if (!(gd->flags & GD_FLG_ENV_READY)) | |
288 | return 1; | |
289 | ||
d09b1787 | 290 | if (varvalue == NULL || varvalue[0] == '\0') |
84b5e802 | 291 | return _do_env_set(0, 2, (char * const *)argv); |
9ffd451a | 292 | else |
84b5e802 | 293 | return _do_env_set(0, 3, (char * const *)argv); |
a68d3ed0 WD |
294 | } |
295 | ||
d67f10ce SG |
296 | /** |
297 | * Set an environment variable to an integer value | |
298 | * | |
9602286d | 299 | * @param varname Environment variable to set |
d67f10ce SG |
300 | * @param value Value to set it to |
301 | * @return 0 if ok, 1 on error | |
302 | */ | |
303 | int setenv_ulong(const char *varname, ulong value) | |
304 | { | |
305 | /* TODO: this should be unsigned */ | |
306 | char *str = simple_itoa(value); | |
307 | ||
308 | return setenv(varname, str); | |
309 | } | |
310 | ||
311 | /** | |
bfc59966 | 312 | * Set an environment variable to an value in hex |
d67f10ce | 313 | * |
9602286d | 314 | * @param varname Environment variable to set |
bfc59966 | 315 | * @param value Value to set it to |
d67f10ce SG |
316 | * @return 0 if ok, 1 on error |
317 | */ | |
bfc59966 | 318 | int setenv_hex(const char *varname, ulong value) |
d67f10ce SG |
319 | { |
320 | char str[17]; | |
321 | ||
bfc59966 | 322 | sprintf(str, "%lx", value); |
d67f10ce SG |
323 | return setenv(varname, str); |
324 | } | |
325 | ||
76b8f79c SG |
326 | ulong getenv_hex(const char *varname, ulong default_val) |
327 | { | |
328 | const char *s; | |
329 | ulong value; | |
330 | char *endp; | |
331 | ||
332 | s = getenv(varname); | |
333 | if (s) | |
334 | value = simple_strtoul(s, &endp, 16); | |
335 | if (!s || endp == s) | |
336 | return default_val; | |
337 | ||
338 | return value; | |
339 | } | |
340 | ||
7ac2fe2d | 341 | #ifndef CONFIG_SPL_BUILD |
088f1b19 | 342 | static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
a68d3ed0 | 343 | { |
47e26b1b | 344 | if (argc < 2) |
4c12eeb8 | 345 | return CMD_RET_USAGE; |
a68d3ed0 | 346 | |
ea882baf | 347 | return _do_env_set(flag, argc, argv); |
a68d3ed0 WD |
348 | } |
349 | ||
ea882baf | 350 | /* |
a68d3ed0 WD |
351 | * Prompt for environment variable |
352 | */ | |
c76fe474 | 353 | #if defined(CONFIG_CMD_ASKENV) |
f3c615b8 | 354 | int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
a68d3ed0 | 355 | { |
6d0f6bcf | 356 | char message[CONFIG_SYS_CBSIZE]; |
7d85591d | 357 | int i, len, pos, size; |
a68d3ed0 | 358 | char *local_args[4]; |
7d85591d | 359 | char *endptr; |
a68d3ed0 WD |
360 | |
361 | local_args[0] = argv[0]; | |
362 | local_args[1] = argv[1]; | |
363 | local_args[2] = NULL; | |
364 | local_args[3] = NULL; | |
365 | ||
7d85591d WD |
366 | /* |
367 | * Check the syntax: | |
368 | * | |
369 | * env_ask envname [message1 ...] [size] | |
370 | */ | |
371 | if (argc == 1) | |
4c12eeb8 | 372 | return CMD_RET_USAGE; |
a68d3ed0 | 373 | |
7d85591d WD |
374 | /* |
375 | * We test the last argument if it can be converted | |
376 | * into a decimal number. If yes, we assume it's | |
377 | * the size. Otherwise we echo it as part of the | |
378 | * message. | |
379 | */ | |
380 | i = simple_strtoul(argv[argc - 1], &endptr, 10); | |
381 | if (*endptr != '\0') { /* no size */ | |
382 | size = CONFIG_SYS_CBSIZE - 1; | |
383 | } else { /* size given */ | |
384 | size = i; | |
385 | --argc; | |
386 | } | |
a68d3ed0 | 387 | |
7d85591d WD |
388 | if (argc <= 2) { |
389 | sprintf(message, "Please enter '%s': ", argv[1]); | |
390 | } else { | |
391 | /* env_ask envname message1 ... messagen [size] */ | |
392 | for (i = 2, pos = 0; i < argc; i++) { | |
f3c615b8 | 393 | if (pos) |
a68d3ed0 | 394 | message[pos++] = ' '; |
f3c615b8 | 395 | |
d09b1787 | 396 | strcpy(message + pos, argv[i]); |
a68d3ed0 WD |
397 | pos += strlen(argv[i]); |
398 | } | |
7d85591d | 399 | message[pos++] = ' '; |
a68d3ed0 | 400 | message[pos] = '\0'; |
a68d3ed0 WD |
401 | } |
402 | ||
6d0f6bcf JCPV |
403 | if (size >= CONFIG_SYS_CBSIZE) |
404 | size = CONFIG_SYS_CBSIZE - 1; | |
a68d3ed0 WD |
405 | |
406 | if (size <= 0) | |
407 | return 1; | |
408 | ||
409 | /* prompt for input */ | |
ea882baf | 410 | len = readline(message); |
a68d3ed0 WD |
411 | |
412 | if (size < len) | |
413 | console_buffer[size] = '\0'; | |
414 | ||
415 | len = 2; | |
416 | if (console_buffer[0] != '\0') { | |
417 | local_args[2] = console_buffer; | |
418 | len = 3; | |
419 | } | |
420 | ||
421 | /* Continue calling setenv code */ | |
ea882baf | 422 | return _do_env_set(flag, len, local_args); |
a68d3ed0 | 423 | } |
90253178 | 424 | #endif |
a68d3ed0 | 425 | |
5e2b3e0c JH |
426 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
427 | static int print_static_binding(const char *var_name, const char *callback_name) | |
428 | { | |
429 | printf("\t%-20s %-20s\n", var_name, callback_name); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int print_active_callback(ENTRY *entry) | |
435 | { | |
436 | struct env_clbk_tbl *clbkp; | |
437 | int i; | |
438 | int num_callbacks; | |
439 | ||
440 | if (entry->callback == NULL) | |
441 | return 0; | |
442 | ||
443 | /* look up the callback in the linker-list */ | |
444 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); | |
445 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); | |
446 | i < num_callbacks; | |
447 | i++, clbkp++) { | |
448 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) | |
449 | if (entry->callback == clbkp->callback + gd->reloc_off) | |
450 | #else | |
451 | if (entry->callback == clbkp->callback) | |
452 | #endif | |
453 | break; | |
454 | } | |
455 | ||
456 | if (i == num_callbacks) | |
457 | /* this should probably never happen, but just in case... */ | |
458 | printf("\t%-20s %p\n", entry->key, entry->callback); | |
459 | else | |
460 | printf("\t%-20s %-20s\n", entry->key, clbkp->name); | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | /* | |
466 | * Print the callbacks available and what they are bound to | |
467 | */ | |
468 | int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
469 | { | |
470 | struct env_clbk_tbl *clbkp; | |
471 | int i; | |
472 | int num_callbacks; | |
473 | ||
474 | /* Print the available callbacks */ | |
475 | puts("Available callbacks:\n"); | |
476 | puts("\tCallback Name\n"); | |
477 | puts("\t-------------\n"); | |
478 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); | |
479 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); | |
480 | i < num_callbacks; | |
481 | i++, clbkp++) | |
482 | printf("\t%s\n", clbkp->name); | |
483 | puts("\n"); | |
484 | ||
485 | /* Print the static bindings that may exist */ | |
486 | puts("Static callback bindings:\n"); | |
487 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); | |
488 | printf("\t%-20s %-20s\n", "-------------", "-------------"); | |
489 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding); | |
490 | puts("\n"); | |
491 | ||
492 | /* walk through each variable and print the callback if it has one */ | |
493 | puts("Active callback bindings:\n"); | |
494 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); | |
495 | printf("\t%-20s %-20s\n", "-------------", "-------------"); | |
496 | hwalk_r(&env_htab, print_active_callback); | |
497 | return 0; | |
498 | } | |
499 | #endif | |
500 | ||
fffad71b JH |
501 | #if defined(CONFIG_CMD_ENV_FLAGS) |
502 | static int print_static_flags(const char *var_name, const char *flags) | |
503 | { | |
504 | enum env_flags_vartype type = env_flags_parse_vartype(flags); | |
267541f7 | 505 | enum env_flags_varaccess access = env_flags_parse_varaccess(flags); |
fffad71b | 506 | |
267541f7 JH |
507 | printf("\t%-20s %-20s %-20s\n", var_name, |
508 | env_flags_get_vartype_name(type), | |
509 | env_flags_get_varaccess_name(access)); | |
fffad71b JH |
510 | |
511 | return 0; | |
512 | } | |
513 | ||
514 | static int print_active_flags(ENTRY *entry) | |
515 | { | |
516 | enum env_flags_vartype type; | |
267541f7 | 517 | enum env_flags_varaccess access; |
fffad71b JH |
518 | |
519 | if (entry->flags == 0) | |
520 | return 0; | |
521 | ||
522 | type = (enum env_flags_vartype) | |
523 | (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); | |
267541f7 JH |
524 | access = env_flags_parse_varaccess_from_binflags(entry->flags); |
525 | printf("\t%-20s %-20s %-20s\n", entry->key, | |
526 | env_flags_get_vartype_name(type), | |
527 | env_flags_get_varaccess_name(access)); | |
fffad71b JH |
528 | |
529 | return 0; | |
530 | } | |
531 | ||
532 | /* | |
533 | * Print the flags available and what variables have flags | |
534 | */ | |
535 | int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
536 | { | |
537 | /* Print the available variable types */ | |
538 | printf("Available variable type flags (position %d):\n", | |
539 | ENV_FLAGS_VARTYPE_LOC); | |
540 | puts("\tFlag\tVariable Type Name\n"); | |
541 | puts("\t----\t------------------\n"); | |
542 | env_flags_print_vartypes(); | |
543 | puts("\n"); | |
544 | ||
267541f7 JH |
545 | /* Print the available variable access types */ |
546 | printf("Available variable access flags (position %d):\n", | |
547 | ENV_FLAGS_VARACCESS_LOC); | |
548 | puts("\tFlag\tVariable Access Name\n"); | |
549 | puts("\t----\t--------------------\n"); | |
550 | env_flags_print_varaccess(); | |
551 | puts("\n"); | |
552 | ||
fffad71b JH |
553 | /* Print the static flags that may exist */ |
554 | puts("Static flags:\n"); | |
267541f7 JH |
555 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
556 | "Variable Access"); | |
557 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", | |
558 | "---------------"); | |
fffad71b JH |
559 | env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags); |
560 | puts("\n"); | |
561 | ||
562 | /* walk through each variable and print the flags if non-default */ | |
563 | puts("Active flags:\n"); | |
267541f7 JH |
564 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
565 | "Variable Access"); | |
566 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", | |
567 | "---------------"); | |
fffad71b JH |
568 | hwalk_r(&env_htab, print_active_flags); |
569 | return 0; | |
570 | } | |
571 | #endif | |
572 | ||
ea882baf | 573 | /* |
246c6922 PT |
574 | * Interactively edit an environment variable |
575 | */ | |
576 | #if defined(CONFIG_CMD_EDITENV) | |
088f1b19 KP |
577 | static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, |
578 | char * const argv[]) | |
246c6922 PT |
579 | { |
580 | char buffer[CONFIG_SYS_CBSIZE]; | |
581 | char *init_val; | |
246c6922 | 582 | |
47e26b1b | 583 | if (argc < 2) |
4c12eeb8 | 584 | return CMD_RET_USAGE; |
246c6922 PT |
585 | |
586 | /* Set read buffer to initial value or empty sting */ | |
587 | init_val = getenv(argv[1]); | |
588 | if (init_val) | |
7fcd9bbd | 589 | sprintf(buffer, "%s", init_val); |
246c6922 PT |
590 | else |
591 | buffer[0] = '\0'; | |
592 | ||
18a3cce9 JH |
593 | if (readline_into_buffer("edit: ", buffer, 0) < 0) |
594 | return 1; | |
246c6922 PT |
595 | |
596 | return setenv(argv[1], buffer); | |
597 | } | |
598 | #endif /* CONFIG_CMD_EDITENV */ | |
7ac2fe2d | 599 | #endif /* CONFIG_SPL_BUILD */ |
246c6922 | 600 | |
ea882baf | 601 | /* |
a68d3ed0 WD |
602 | * Look up variable from environment, |
603 | * return address of storage for that variable, | |
604 | * or NULL if not found | |
605 | */ | |
84b5e802 | 606 | char *getenv(const char *name) |
a68d3ed0 | 607 | { |
d09b1787 | 608 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
ea882baf | 609 | ENTRY e, *ep; |
a68d3ed0 | 610 | |
91a76751 | 611 | WATCHDOG_RESET(); |
2a3cb020 | 612 | |
d09b1787 IG |
613 | e.key = name; |
614 | e.data = NULL; | |
c4e0057f | 615 | hsearch_r(e, FIND, &ep, &env_htab, 0); |
91a76751 | 616 | |
f3c615b8 | 617 | return ep ? ep->data : NULL; |
a68d3ed0 WD |
618 | } |
619 | ||
ea882baf | 620 | /* restricted capabilities before import */ |
ea882baf WD |
621 | if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) |
622 | return (char *)(gd->env_buf); | |
91a76751 | 623 | |
ea882baf | 624 | return NULL; |
a68d3ed0 WD |
625 | } |
626 | ||
ea882baf WD |
627 | /* |
628 | * Look up variable from environment for restricted C runtime env. | |
629 | */ | |
84b5e802 | 630 | int getenv_f(const char *name, char *buf, unsigned len) |
a68d3ed0 WD |
631 | { |
632 | int i, nxt; | |
633 | ||
d09b1787 | 634 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
a68d3ed0 WD |
635 | int val, n; |
636 | ||
f3c615b8 ML |
637 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) { |
638 | if (nxt >= CONFIG_ENV_SIZE) | |
639 | return -1; | |
a68d3ed0 | 640 | } |
f3c615b8 ML |
641 | |
642 | val = envmatch((uchar *)name, i); | |
643 | if (val < 0) | |
a68d3ed0 | 644 | continue; |
9ed4a958 | 645 | |
a68d3ed0 | 646 | /* found; copy out */ |
f3c615b8 | 647 | for (n = 0; n < len; ++n, ++buf) { |
d09b1787 IG |
648 | *buf = env_get_char(val++); |
649 | if (*buf == '\0') | |
9ed4a958 WD |
650 | return n; |
651 | } | |
652 | ||
653 | if (n) | |
654 | *--buf = '\0'; | |
655 | ||
a02a884b WD |
656 | printf("env_buf [%d bytes] too small for value of \"%s\"\n", |
657 | len, name); | |
9ed4a958 WD |
658 | |
659 | return n; | |
a68d3ed0 | 660 | } |
d09b1787 | 661 | |
f3c615b8 | 662 | return -1; |
a68d3ed0 WD |
663 | } |
664 | ||
4a9b4131 SG |
665 | /** |
666 | * Decode the integer value of an environment variable and return it. | |
667 | * | |
668 | * @param name Name of environemnt variable | |
669 | * @param base Number base to use (normally 10, or 16 for hex) | |
670 | * @param default_val Default value to return if the variable is not | |
671 | * found | |
672 | * @return the decoded value, or default_val if not found | |
673 | */ | |
674 | ulong getenv_ulong(const char *name, int base, ulong default_val) | |
675 | { | |
676 | /* | |
677 | * We can use getenv() here, even before relocation, since the | |
678 | * environment variable value is an integer and thus short. | |
679 | */ | |
680 | const char *str = getenv(name); | |
681 | ||
682 | return str ? simple_strtoul(str, NULL, base) : default_val; | |
683 | } | |
684 | ||
7ac2fe2d | 685 | #ifndef CONFIG_SPL_BUILD |
bdab39d3 | 686 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
088f1b19 KP |
687 | static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, |
688 | char * const argv[]) | |
a68d3ed0 | 689 | { |
f3c615b8 | 690 | printf("Saving Environment to %s...\n", env_name_spec); |
a68d3ed0 | 691 | |
f3c615b8 | 692 | return saveenv() ? 1 : 0; |
a68d3ed0 | 693 | } |
8bde7f77 | 694 | |
ba69dc26 | 695 | U_BOOT_CMD( |
ea882baf | 696 | saveenv, 1, 0, do_env_save, |
2fb2604d | 697 | "save environment variables to persistent storage", |
a89c33db | 698 | "" |
ba69dc26 | 699 | ); |
a68d3ed0 | 700 | #endif |
7ac2fe2d | 701 | #endif /* CONFIG_SPL_BUILD */ |
a68d3ed0 WD |
702 | |
703 | ||
ea882baf | 704 | /* |
a68d3ed0 WD |
705 | * Match a name / name=value pair |
706 | * | |
707 | * s1 is either a simple 'name', or a 'name=value' pair. | |
708 | * i2 is the environment index for a 'name2=value2' pair. | |
d09b1787 | 709 | * If the names match, return the index for the value2, else -1. |
a68d3ed0 | 710 | */ |
f3c615b8 | 711 | int envmatch(uchar *s1, int i2) |
a68d3ed0 | 712 | { |
586197df JH |
713 | if (s1 == NULL) |
714 | return -1; | |
715 | ||
a68d3ed0 WD |
716 | while (*s1 == env_get_char(i2++)) |
717 | if (*s1++ == '=') | |
f3c615b8 | 718 | return i2; |
d09b1787 | 719 | |
a68d3ed0 | 720 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
f3c615b8 | 721 | return i2; |
d09b1787 | 722 | |
f3c615b8 | 723 | return -1; |
a68d3ed0 | 724 | } |
8bde7f77 | 725 | |
7ac2fe2d | 726 | #ifndef CONFIG_SPL_BUILD |
b64b7c3d | 727 | static int do_env_default(cmd_tbl_t *cmdtp, int __flag, |
d09b1787 | 728 | int argc, char * const argv[]) |
ea882baf | 729 | { |
b64b7c3d | 730 | int all = 0, flag = 0; |
f3c615b8 | 731 | |
b64b7c3d GF |
732 | debug("Initial value for argc=%d\n", argc); |
733 | while (--argc > 0 && **++argv == '-') { | |
734 | char *arg = *argv; | |
735 | ||
736 | while (*++arg) { | |
737 | switch (*arg) { | |
738 | case 'a': /* default all */ | |
739 | all = 1; | |
740 | break; | |
741 | case 'f': /* force */ | |
742 | flag |= H_FORCE; | |
743 | break; | |
744 | default: | |
745 | return cmd_usage(cmdtp); | |
746 | } | |
747 | } | |
748 | } | |
749 | debug("Final value for argc=%d\n", argc); | |
750 | if (all && (argc == 0)) { | |
751 | /* Reset the whole environment */ | |
752 | set_default_env("## Resetting to default environment\n"); | |
753 | return 0; | |
754 | } | |
755 | if (!all && (argc > 0)) { | |
756 | /* Reset individual variables */ | |
757 | set_default_vars(argc, argv); | |
758 | return 0; | |
759 | } | |
760 | ||
761 | return cmd_usage(cmdtp); | |
ea882baf WD |
762 | } |
763 | ||
d09b1787 IG |
764 | static int do_env_delete(cmd_tbl_t *cmdtp, int flag, |
765 | int argc, char * const argv[]) | |
ea882baf | 766 | { |
9d8d661d JH |
767 | int env_flag = H_INTERACTIVE; |
768 | int ret = 0; | |
769 | ||
770 | debug("Initial value for argc=%d\n", argc); | |
771 | while (argc > 1 && **(argv + 1) == '-') { | |
772 | char *arg = *++argv; | |
773 | ||
774 | --argc; | |
775 | while (*++arg) { | |
776 | switch (*arg) { | |
777 | case 'f': /* force */ | |
778 | env_flag |= H_FORCE; | |
779 | break; | |
780 | default: | |
781 | return CMD_RET_USAGE; | |
782 | } | |
783 | } | |
784 | } | |
785 | debug("Final value for argc=%d\n", argc); | |
786 | ||
787 | env_id++; | |
788 | ||
789 | while (--argc > 0) { | |
790 | char *name = *++argv; | |
791 | ||
792 | if (!hdelete_r(name, &env_htab, env_flag)) | |
793 | ret = 1; | |
794 | } | |
795 | ||
796 | return ret; | |
ea882baf WD |
797 | } |
798 | ||
0c79cda0 | 799 | #ifdef CONFIG_CMD_EXPORTENV |
ea882baf | 800 | /* |
37f2fe74 | 801 | * env export [-t | -b | -c] [-s size] addr [var ...] |
ea882baf WD |
802 | * -t: export as text format; if size is given, data will be |
803 | * padded with '\0' bytes; if not, one terminating '\0' | |
804 | * will be added (which is included in the "filesize" | |
805 | * setting so you can for exmple copy this to flash and | |
806 | * keep the termination). | |
807 | * -b: export as binary format (name=value pairs separated by | |
808 | * '\0', list end marked by double "\0\0") | |
809 | * -c: export as checksum protected environment format as | |
810 | * used for example by "saveenv" command | |
37f2fe74 WD |
811 | * -s size: |
812 | * size of output buffer | |
ea882baf | 813 | * addr: memory address where environment gets stored |
37f2fe74 WD |
814 | * var... List of variable names that get included into the |
815 | * export. Without arguments, the whole environment gets | |
816 | * exported. | |
ea882baf WD |
817 | * |
818 | * With "-c" and size is NOT given, then the export command will | |
819 | * format the data as currently used for the persistent storage, | |
820 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and | |
821 | * prepend a valid CRC32 checksum and, in case of resundant | |
822 | * environment, a "current" redundancy flag. If size is given, this | |
823 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 | |
824 | * checksum and redundancy flag will be inserted. | |
825 | * | |
826 | * With "-b" and "-t", always only the real data (including a | |
827 | * terminating '\0' byte) will be written; here the optional size | |
828 | * argument will be used to make sure not to overflow the user | |
829 | * provided buffer; the command will abort if the size is not | |
830 | * sufficient. Any remainign space will be '\0' padded. | |
831 | * | |
832 | * On successful return, the variable "filesize" will be set. | |
833 | * Note that filesize includes the trailing/terminating '\0' byte(s). | |
834 | * | |
835 | * Usage szenario: create a text snapshot/backup of the current settings: | |
836 | * | |
837 | * => env export -t 100000 | |
838 | * => era ${backup_addr} +${filesize} | |
839 | * => cp.b 100000 ${backup_addr} ${filesize} | |
840 | * | |
841 | * Re-import this snapshot, deleting all other settings: | |
842 | * | |
843 | * => env import -d -t ${backup_addr} | |
844 | */ | |
d09b1787 IG |
845 | static int do_env_export(cmd_tbl_t *cmdtp, int flag, |
846 | int argc, char * const argv[]) | |
ea882baf WD |
847 | { |
848 | char buf[32]; | |
849 | char *addr, *cmd, *res; | |
37f2fe74 | 850 | size_t size = 0; |
ea882baf WD |
851 | ssize_t len; |
852 | env_t *envp; | |
853 | char sep = '\n'; | |
854 | int chk = 0; | |
855 | int fmt = 0; | |
856 | ||
857 | cmd = *argv; | |
858 | ||
859 | while (--argc > 0 && **++argv == '-') { | |
860 | char *arg = *argv; | |
861 | while (*++arg) { | |
862 | switch (*arg) { | |
863 | case 'b': /* raw binary format */ | |
864 | if (fmt++) | |
865 | goto sep_err; | |
866 | sep = '\0'; | |
867 | break; | |
868 | case 'c': /* external checksum format */ | |
869 | if (fmt++) | |
870 | goto sep_err; | |
871 | sep = '\0'; | |
872 | chk = 1; | |
873 | break; | |
37f2fe74 WD |
874 | case 's': /* size given */ |
875 | if (--argc <= 0) | |
876 | return cmd_usage(cmdtp); | |
877 | size = simple_strtoul(*++argv, NULL, 16); | |
878 | goto NXTARG; | |
ea882baf WD |
879 | case 't': /* text format */ |
880 | if (fmt++) | |
881 | goto sep_err; | |
882 | sep = '\n'; | |
883 | break; | |
884 | default: | |
4c12eeb8 | 885 | return CMD_RET_USAGE; |
ea882baf WD |
886 | } |
887 | } | |
37f2fe74 | 888 | NXTARG: ; |
ea882baf WD |
889 | } |
890 | ||
f3c615b8 | 891 | if (argc < 1) |
4c12eeb8 | 892 | return CMD_RET_USAGE; |
8bde7f77 | 893 | |
ea882baf WD |
894 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
895 | ||
37f2fe74 | 896 | if (size) |
ea882baf | 897 | memset(addr, '\0', size); |
37f2fe74 WD |
898 | |
899 | argc--; | |
900 | argv++; | |
ea882baf WD |
901 | |
902 | if (sep) { /* export as text file */ | |
ea009d47 WD |
903 | len = hexport_r(&env_htab, sep, |
904 | H_MATCH_KEY | H_MATCH_IDENT, | |
905 | &addr, size, argc, argv); | |
ea882baf | 906 | if (len < 0) { |
d09b1787 | 907 | error("Cannot export environment: errno = %d\n", errno); |
ea882baf WD |
908 | return 1; |
909 | } | |
8c3aff52 | 910 | sprintf(buf, "%zX", (size_t)len); |
ea882baf WD |
911 | setenv("filesize", buf); |
912 | ||
913 | return 0; | |
914 | } | |
915 | ||
916 | envp = (env_t *)addr; | |
917 | ||
918 | if (chk) /* export as checksum protected block */ | |
919 | res = (char *)envp->data; | |
920 | else /* export as raw binary data */ | |
921 | res = addr; | |
922 | ||
ea009d47 WD |
923 | len = hexport_r(&env_htab, '\0', |
924 | H_MATCH_KEY | H_MATCH_IDENT, | |
925 | &res, ENV_SIZE, argc, argv); | |
ea882baf | 926 | if (len < 0) { |
d09b1787 | 927 | error("Cannot export environment: errno = %d\n", errno); |
ea882baf WD |
928 | return 1; |
929 | } | |
930 | ||
931 | if (chk) { | |
d09b1787 | 932 | envp->crc = crc32(0, envp->data, ENV_SIZE); |
ea882baf WD |
933 | #ifdef CONFIG_ENV_ADDR_REDUND |
934 | envp->flags = ACTIVE_FLAG; | |
935 | #endif | |
936 | } | |
41ef372c | 937 | setenv_hex("filesize", len + offsetof(env_t, data)); |
ea882baf WD |
938 | |
939 | return 0; | |
940 | ||
941 | sep_err: | |
d09b1787 | 942 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd); |
ea882baf WD |
943 | return 1; |
944 | } | |
0c79cda0 | 945 | #endif |
ea882baf | 946 | |
0c79cda0 | 947 | #ifdef CONFIG_CMD_IMPORTENV |
ea882baf WD |
948 | /* |
949 | * env import [-d] [-t | -b | -c] addr [size] | |
950 | * -d: delete existing environment before importing; | |
951 | * otherwise overwrite / append to existion definitions | |
952 | * -t: assume text format; either "size" must be given or the | |
953 | * text data must be '\0' terminated | |
954 | * -b: assume binary format ('\0' separated, "\0\0" terminated) | |
955 | * -c: assume checksum protected environment format | |
956 | * addr: memory address to read from | |
957 | * size: length of input data; if missing, proper '\0' | |
958 | * termination is mandatory | |
959 | */ | |
d09b1787 IG |
960 | static int do_env_import(cmd_tbl_t *cmdtp, int flag, |
961 | int argc, char * const argv[]) | |
ea882baf WD |
962 | { |
963 | char *cmd, *addr; | |
964 | char sep = '\n'; | |
965 | int chk = 0; | |
966 | int fmt = 0; | |
967 | int del = 0; | |
968 | size_t size; | |
969 | ||
970 | cmd = *argv; | |
971 | ||
972 | while (--argc > 0 && **++argv == '-') { | |
973 | char *arg = *argv; | |
974 | while (*++arg) { | |
975 | switch (*arg) { | |
976 | case 'b': /* raw binary format */ | |
977 | if (fmt++) | |
978 | goto sep_err; | |
979 | sep = '\0'; | |
980 | break; | |
981 | case 'c': /* external checksum format */ | |
982 | if (fmt++) | |
983 | goto sep_err; | |
984 | sep = '\0'; | |
985 | chk = 1; | |
986 | break; | |
987 | case 't': /* text format */ | |
988 | if (fmt++) | |
989 | goto sep_err; | |
990 | sep = '\n'; | |
991 | break; | |
992 | case 'd': | |
993 | del = 1; | |
994 | break; | |
995 | default: | |
4c12eeb8 | 996 | return CMD_RET_USAGE; |
ea882baf WD |
997 | } |
998 | } | |
999 | } | |
1000 | ||
f3c615b8 | 1001 | if (argc < 1) |
4c12eeb8 | 1002 | return CMD_RET_USAGE; |
ea882baf WD |
1003 | |
1004 | if (!fmt) | |
1005 | printf("## Warning: defaulting to text format\n"); | |
1006 | ||
1007 | addr = (char *)simple_strtoul(argv[0], NULL, 16); | |
1008 | ||
1009 | if (argc == 2) { | |
1010 | size = simple_strtoul(argv[1], NULL, 16); | |
1011 | } else { | |
1012 | char *s = addr; | |
1013 | ||
1014 | size = 0; | |
1015 | ||
1016 | while (size < MAX_ENV_SIZE) { | |
1017 | if ((*s == sep) && (*(s+1) == '\0')) | |
1018 | break; | |
1019 | ++s; | |
1020 | ++size; | |
1021 | } | |
1022 | if (size == MAX_ENV_SIZE) { | |
1023 | printf("## Warning: Input data exceeds %d bytes" | |
1024 | " - truncated\n", MAX_ENV_SIZE); | |
1025 | } | |
d3f80c77 | 1026 | size += 2; |
79afc88d | 1027 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
ea882baf WD |
1028 | } |
1029 | ||
1030 | if (chk) { | |
1031 | uint32_t crc; | |
1032 | env_t *ep = (env_t *)addr; | |
1033 | ||
1034 | size -= offsetof(env_t, data); | |
1035 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
1036 | ||
1037 | if (crc32(0, ep->data, size) != crc) { | |
1038 | puts("## Error: bad CRC, import failed\n"); | |
1039 | return 1; | |
1040 | } | |
1041 | addr = (char *)ep->data; | |
1042 | } | |
1043 | ||
348b1f1c | 1044 | if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR, |
c4e0057f | 1045 | 0, NULL) == 0) { |
ea882baf WD |
1046 | error("Environment import failed: errno = %d\n", errno); |
1047 | return 1; | |
1048 | } | |
1049 | gd->flags |= GD_FLG_ENV_READY; | |
1050 | ||
1051 | return 0; | |
1052 | ||
1053 | sep_err: | |
1054 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", | |
1055 | cmd); | |
1056 | return 1; | |
1057 | } | |
0c79cda0 | 1058 | #endif |
ea882baf | 1059 | |
88733e2c AR |
1060 | #if defined(CONFIG_CMD_ENV_EXISTS) |
1061 | static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc, | |
1062 | char * const argv[]) | |
1063 | { | |
1064 | ENTRY e, *ep; | |
1065 | ||
1066 | if (argc < 2) | |
1067 | return CMD_RET_USAGE; | |
1068 | ||
1069 | e.key = argv[1]; | |
1070 | e.data = NULL; | |
1071 | hsearch_r(e, FIND, &ep, &env_htab, 0); | |
1072 | ||
1073 | return (ep == NULL) ? 1 : 0; | |
1074 | } | |
1075 | #endif | |
1076 | ||
ea882baf WD |
1077 | /* |
1078 | * New command line interface: "env" command with subcommands | |
1079 | */ | |
1080 | static cmd_tbl_t cmd_env_sub[] = { | |
1081 | #if defined(CONFIG_CMD_ASKENV) | |
1082 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), | |
1083 | #endif | |
1084 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), | |
9d8d661d | 1085 | U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), |
ea882baf WD |
1086 | #if defined(CONFIG_CMD_EDITENV) |
1087 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), | |
1088 | #endif | |
5e2b3e0c JH |
1089 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
1090 | U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), | |
1091 | #endif | |
fffad71b JH |
1092 | #if defined(CONFIG_CMD_ENV_FLAGS) |
1093 | U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), | |
1094 | #endif | |
0c79cda0 | 1095 | #if defined(CONFIG_CMD_EXPORTENV) |
ea882baf | 1096 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
0c79cda0 | 1097 | #endif |
a000b795 KP |
1098 | #if defined(CONFIG_CMD_GREPENV) |
1099 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), | |
1100 | #endif | |
0c79cda0 | 1101 | #if defined(CONFIG_CMD_IMPORTENV) |
ea882baf | 1102 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
0c79cda0 | 1103 | #endif |
ea882baf WD |
1104 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
1105 | #if defined(CONFIG_CMD_RUN) | |
1106 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), | |
1107 | #endif | |
1108 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) | |
1109 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), | |
1110 | #endif | |
1111 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), | |
88733e2c AR |
1112 | #if defined(CONFIG_CMD_ENV_EXISTS) |
1113 | U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""), | |
1114 | #endif | |
ea882baf WD |
1115 | }; |
1116 | ||
2e5167cc | 1117 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f HS |
1118 | void env_reloc(void) |
1119 | { | |
1120 | fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); | |
1121 | } | |
1122 | #endif | |
1123 | ||
f3c615b8 | 1124 | static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
ea882baf WD |
1125 | { |
1126 | cmd_tbl_t *cp; | |
1127 | ||
5904da02 | 1128 | if (argc < 2) |
4c12eeb8 | 1129 | return CMD_RET_USAGE; |
5904da02 | 1130 | |
ea882baf WD |
1131 | /* drop initial "env" arg */ |
1132 | argc--; | |
1133 | argv++; | |
1134 | ||
1135 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); | |
1136 | ||
1137 | if (cp) | |
1138 | return cp->cmd(cmdtp, flag, argc, argv); | |
1139 | ||
4c12eeb8 | 1140 | return CMD_RET_USAGE; |
ea882baf WD |
1141 | } |
1142 | ||
088f1b19 KP |
1143 | #ifdef CONFIG_SYS_LONGHELP |
1144 | static char env_help_text[] = | |
ea882baf WD |
1145 | #if defined(CONFIG_CMD_ASKENV) |
1146 | "ask name [message] [size] - ask for environment variable\nenv " | |
5e2b3e0c JH |
1147 | #endif |
1148 | #if defined(CONFIG_CMD_ENV_CALLBACK) | |
1149 | "callbacks - print callbacks and their associated variables\nenv " | |
ea882baf | 1150 | #endif |
b64b7c3d GF |
1151 | "default [-f] -a - [forcibly] reset default environment\n" |
1152 | "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" | |
9d8d661d | 1153 | "env delete [-f] var [...] - [forcibly] delete variable(s)\n" |
ea882baf WD |
1154 | #if defined(CONFIG_CMD_EDITENV) |
1155 | "env edit name - edit environment variable\n" | |
1156 | #endif | |
88733e2c AR |
1157 | #if defined(CONFIG_CMD_ENV_EXISTS) |
1158 | "env exists name - tests for existence of variable\n" | |
1159 | #endif | |
4796bc4b | 1160 | #if defined(CONFIG_CMD_EXPORTENV) |
37f2fe74 | 1161 | "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" |
4796bc4b | 1162 | #endif |
fffad71b JH |
1163 | #if defined(CONFIG_CMD_ENV_FLAGS) |
1164 | "env flags - print variables that have non-default flags\n" | |
1165 | #endif | |
a000b795 | 1166 | #if defined(CONFIG_CMD_GREPENV) |
be29df6a WD |
1167 | #ifdef CONFIG_REGEX |
1168 | "env grep [-e] [-n | -v | -b] string [...] - search environment\n" | |
1169 | #else | |
d87244d5 | 1170 | "env grep [-n | -v | -b] string [...] - search environment\n" |
a000b795 | 1171 | #endif |
be29df6a | 1172 | #endif |
4796bc4b | 1173 | #if defined(CONFIG_CMD_IMPORTENV) |
a000b795 | 1174 | "env import [-d] [-t | -b | -c] addr [size] - import environment\n" |
4796bc4b | 1175 | #endif |
be11235a | 1176 | "env print [-a | name ...] - print environment\n" |
ea882baf WD |
1177 | #if defined(CONFIG_CMD_RUN) |
1178 | "env run var [...] - run commands in an environment variable\n" | |
1179 | #endif | |
d798a9b5 | 1180 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
ea882baf | 1181 | "env save - save environment\n" |
d798a9b5 | 1182 | #endif |
088f1b19 KP |
1183 | "env set [-f] name [arg ...]\n"; |
1184 | #endif | |
1185 | ||
1186 | U_BOOT_CMD( | |
1187 | env, CONFIG_SYS_MAXARGS, 1, do_env, | |
1188 | "environment handling commands", env_help_text | |
ea882baf WD |
1189 | ); |
1190 | ||
1191 | /* | |
1192 | * Old command line interface, kept for compatibility | |
1193 | */ | |
8bde7f77 | 1194 | |
246c6922 | 1195 | #if defined(CONFIG_CMD_EDITENV) |
722b061b | 1196 | U_BOOT_CMD_COMPLETE( |
ea882baf | 1197 | editenv, 2, 0, do_env_edit, |
246c6922 PT |
1198 | "edit environment variable", |
1199 | "name\n" | |
722b061b MF |
1200 | " - edit environment variable 'name'", |
1201 | var_complete | |
246c6922 PT |
1202 | ); |
1203 | #endif | |
1204 | ||
722b061b | 1205 | U_BOOT_CMD_COMPLETE( |
ea882baf | 1206 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
2fb2604d | 1207 | "print environment variables", |
be11235a | 1208 | "[-a]\n - print [all] values of all environment variables\n" |
8bde7f77 | 1209 | "printenv name ...\n" |
722b061b MF |
1210 | " - print value of environment variable 'name'", |
1211 | var_complete | |
8bde7f77 WD |
1212 | ); |
1213 | ||
a000b795 KP |
1214 | #ifdef CONFIG_CMD_GREPENV |
1215 | U_BOOT_CMD_COMPLETE( | |
1216 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, | |
1217 | "search environment variables", | |
be29df6a WD |
1218 | #ifdef CONFIG_REGEX |
1219 | "[-e] [-n | -v | -b] string ...\n" | |
1220 | #else | |
d87244d5 | 1221 | "[-n | -v | -b] string ...\n" |
be29df6a | 1222 | #endif |
d87244d5 | 1223 | " - list environment name=value pairs matching 'string'\n" |
be29df6a WD |
1224 | #ifdef CONFIG_REGEX |
1225 | " \"-e\": enable regular expressions;\n" | |
1226 | #endif | |
d87244d5 WD |
1227 | " \"-n\": search variable names; \"-v\": search values;\n" |
1228 | " \"-b\": search both names and values (default)", | |
a000b795 KP |
1229 | var_complete |
1230 | ); | |
1231 | #endif | |
1232 | ||
722b061b | 1233 | U_BOOT_CMD_COMPLETE( |
ea882baf | 1234 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
2fb2604d | 1235 | "set environment variables", |
24ab5a19 JH |
1236 | "[-f] name value ...\n" |
1237 | " - [forcibly] set environment variable 'name' to 'value ...'\n" | |
1238 | "setenv [-f] name\n" | |
1239 | " - [forcibly] delete environment variable 'name'", | |
722b061b | 1240 | var_complete |
8bde7f77 WD |
1241 | ); |
1242 | ||
c76fe474 | 1243 | #if defined(CONFIG_CMD_ASKENV) |
8bde7f77 | 1244 | |
0d498393 | 1245 | U_BOOT_CMD( |
ea882baf | 1246 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
2fb2604d | 1247 | "get environment variables from stdin", |
8bde7f77 | 1248 | "name [message] [size]\n" |
7d85591d | 1249 | " - get environment variable 'name' from stdin (max 'size' chars)" |
8bde7f77 | 1250 | ); |
90253178 | 1251 | #endif |
8bde7f77 | 1252 | |
c76fe474 | 1253 | #if defined(CONFIG_CMD_RUN) |
722b061b | 1254 | U_BOOT_CMD_COMPLETE( |
6d0f6bcf | 1255 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
2fb2604d | 1256 | "run commands in an environment variable", |
8bde7f77 | 1257 | "var [...]\n" |
722b061b MF |
1258 | " - run the commands in the environment variable(s) 'var'", |
1259 | var_complete | |
8bde7f77 | 1260 | ); |
90253178 | 1261 | #endif |
7ac2fe2d | 1262 | #endif /* CONFIG_SPL_BUILD */ |