env: Move get_env_id() to env.h
[oweals/u-boot.git] / cmd / nvedit.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2013
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Andreas Heppel <aheppel@sysgo.de>
8  *
9  * Copyright 2011 Freescale Semiconductor, Inc.
10  */
11
12 /*
13  * Support for persistent environment data
14  *
15  * The "environment" is stored on external storage as a list of '\0'
16  * terminated "name=value" strings. The end of the list is marked by
17  * a double '\0'. The environment is preceded by a 32 bit CRC over
18  * the data part and, in case of redundant environment, a byte of
19  * flags.
20  *
21  * This linearized representation will also be used before
22  * relocation, i. e. as long as we don't have a full C runtime
23  * environment. After that, we use a hash table.
24  */
25
26 #include <common.h>
27 #include <cli.h>
28 #include <command.h>
29 #include <console.h>
30 #include <env.h>
31 #include <environment.h>
32 #include <search.h>
33 #include <errno.h>
34 #include <malloc.h>
35 #include <mapmem.h>
36 #include <watchdog.h>
37 #include <linux/stddef.h>
38 #include <asm/byteorder.h>
39 #include <asm/io.h>
40
41 DECLARE_GLOBAL_DATA_PTR;
42
43 #if     defined(CONFIG_ENV_IS_IN_EEPROM)        || \
44         defined(CONFIG_ENV_IS_IN_FLASH)         || \
45         defined(CONFIG_ENV_IS_IN_MMC)           || \
46         defined(CONFIG_ENV_IS_IN_FAT)           || \
47         defined(CONFIG_ENV_IS_IN_EXT4)          || \
48         defined(CONFIG_ENV_IS_IN_NAND)          || \
49         defined(CONFIG_ENV_IS_IN_NVRAM)         || \
50         defined(CONFIG_ENV_IS_IN_ONENAND)       || \
51         defined(CONFIG_ENV_IS_IN_SATA)          || \
52         defined(CONFIG_ENV_IS_IN_SPI_FLASH)     || \
53         defined(CONFIG_ENV_IS_IN_REMOTE)        || \
54         defined(CONFIG_ENV_IS_IN_UBI)
55
56 #define ENV_IS_IN_DEVICE
57
58 #endif
59
60 #if     !defined(ENV_IS_IN_DEVICE)              && \
61         !defined(CONFIG_ENV_IS_NOWHERE)
62 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
63 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
64 #endif
65
66 /*
67  * Maximum expected input data size for import command
68  */
69 #define MAX_ENV_SIZE    (1 << 20)       /* 1 MiB */
70
71 /*
72  * This variable is incremented on each do_env_set(), so it can
73  * be used via env_get_id() as an indication, if the environment
74  * has changed or not. So it is possible to reread an environment
75  * variable only if the environment was changed ... done so for
76  * example in NetInitLoop()
77  */
78 static int env_id = 1;
79
80 int env_get_id(void)
81 {
82         return env_id;
83 }
84
85 #ifndef CONFIG_SPL_BUILD
86 /*
87  * Command interface: print one or all environment variables
88  *
89  * Returns 0 in case of error, or length of printed string
90  */
91 static int env_print(char *name, int flag)
92 {
93         char *res = NULL;
94         ssize_t len;
95
96         if (name) {             /* print a single name */
97                 ENTRY e, *ep;
98
99                 e.key = name;
100                 e.data = NULL;
101                 hsearch_r(e, FIND, &ep, &env_htab, flag);
102                 if (ep == NULL)
103                         return 0;
104                 len = printf("%s=%s\n", ep->key, ep->data);
105                 return len;
106         }
107
108         /* print whole list */
109         len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
110
111         if (len > 0) {
112                 puts(res);
113                 free(res);
114                 return len;
115         }
116
117         /* should never happen */
118         printf("## Error: cannot export environment\n");
119         return 0;
120 }
121
122 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
123                         char * const argv[])
124 {
125         int i;
126         int rcode = 0;
127         int env_flag = H_HIDE_DOT;
128
129 #if defined(CONFIG_CMD_NVEDIT_EFI)
130         if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
131                 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
132 #endif
133
134         if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
135                 argc--;
136                 argv++;
137                 env_flag &= ~H_HIDE_DOT;
138         }
139
140         if (argc == 1) {
141                 /* print all env vars */
142                 rcode = env_print(NULL, env_flag);
143                 if (!rcode)
144                         return 1;
145                 printf("\nEnvironment size: %d/%ld bytes\n",
146                         rcode, (ulong)ENV_SIZE);
147                 return 0;
148         }
149
150         /* print selected env vars */
151         env_flag &= ~H_HIDE_DOT;
152         for (i = 1; i < argc; ++i) {
153                 int rc = env_print(argv[i], env_flag);
154                 if (!rc) {
155                         printf("## Error: \"%s\" not defined\n", argv[i]);
156                         ++rcode;
157                 }
158         }
159
160         return rcode;
161 }
162
163 #ifdef CONFIG_CMD_GREPENV
164 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165                        int argc, char * const argv[])
166 {
167         char *res = NULL;
168         int len, grep_how, grep_what;
169
170         if (argc < 2)
171                 return CMD_RET_USAGE;
172
173         grep_how  = H_MATCH_SUBSTR;     /* default: substring search    */
174         grep_what = H_MATCH_BOTH;       /* default: grep names and values */
175
176         while (--argc > 0 && **++argv == '-') {
177                 char *arg = *argv;
178                 while (*++arg) {
179                         switch (*arg) {
180 #ifdef CONFIG_REGEX
181                         case 'e':               /* use regex matching */
182                                 grep_how  = H_MATCH_REGEX;
183                                 break;
184 #endif
185                         case 'n':               /* grep for name */
186                                 grep_what = H_MATCH_KEY;
187                                 break;
188                         case 'v':               /* grep for value */
189                                 grep_what = H_MATCH_DATA;
190                                 break;
191                         case 'b':               /* grep for both */
192                                 grep_what = H_MATCH_BOTH;
193                                 break;
194                         case '-':
195                                 goto DONE;
196                         default:
197                                 return CMD_RET_USAGE;
198                         }
199                 }
200         }
201
202 DONE:
203         len = hexport_r(&env_htab, '\n',
204                         flag | grep_what | grep_how,
205                         &res, 0, argc, argv);
206
207         if (len > 0) {
208                 puts(res);
209                 free(res);
210         }
211
212         if (len < 2)
213                 return 1;
214
215         return 0;
216 }
217 #endif
218 #endif /* CONFIG_SPL_BUILD */
219
220 /*
221  * Set a new environment variable,
222  * or replace or delete an existing one.
223  */
224 static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
225 {
226         int   i, len;
227         char  *name, *value, *s;
228         ENTRY e, *ep;
229
230         debug("Initial value for argc=%d\n", argc);
231
232 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
233         if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
234                 return do_env_set_efi(NULL, flag, --argc, ++argv);
235 #endif
236
237         while (argc > 1 && **(argv + 1) == '-') {
238                 char *arg = *++argv;
239
240                 --argc;
241                 while (*++arg) {
242                         switch (*arg) {
243                         case 'f':               /* force */
244                                 env_flag |= H_FORCE;
245                                 break;
246                         default:
247                                 return CMD_RET_USAGE;
248                         }
249                 }
250         }
251         debug("Final value for argc=%d\n", argc);
252         name = argv[1];
253
254         if (strchr(name, '=')) {
255                 printf("## Error: illegal character '='"
256                        "in variable name \"%s\"\n", name);
257                 return 1;
258         }
259
260         env_id++;
261
262         /* Delete only ? */
263         if (argc < 3 || argv[2] == NULL) {
264                 int rc = hdelete_r(name, &env_htab, env_flag);
265                 return !rc;
266         }
267
268         /*
269          * Insert / replace new value
270          */
271         for (i = 2, len = 0; i < argc; ++i)
272                 len += strlen(argv[i]) + 1;
273
274         value = malloc(len);
275         if (value == NULL) {
276                 printf("## Can't malloc %d bytes\n", len);
277                 return 1;
278         }
279         for (i = 2, s = value; i < argc; ++i) {
280                 char *v = argv[i];
281
282                 while ((*s++ = *v++) != '\0')
283                         ;
284                 *(s - 1) = ' ';
285         }
286         if (s != value)
287                 *--s = '\0';
288
289         e.key   = name;
290         e.data  = value;
291         hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
292         free(value);
293         if (!ep) {
294                 printf("## Error inserting \"%s\" variable, errno=%d\n",
295                         name, errno);
296                 return 1;
297         }
298
299         return 0;
300 }
301
302 int env_set(const char *varname, const char *varvalue)
303 {
304         const char * const argv[4] = { "setenv", varname, varvalue, NULL };
305
306         /* before import into hashtable */
307         if (!(gd->flags & GD_FLG_ENV_READY))
308                 return 1;
309
310         if (varvalue == NULL || varvalue[0] == '\0')
311                 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
312         else
313                 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
314 }
315
316 /**
317  * Set an environment variable to an integer value
318  *
319  * @param varname       Environment variable to set
320  * @param value         Value to set it to
321  * @return 0 if ok, 1 on error
322  */
323 int env_set_ulong(const char *varname, ulong value)
324 {
325         /* TODO: this should be unsigned */
326         char *str = simple_itoa(value);
327
328         return env_set(varname, str);
329 }
330
331 /**
332  * Set an environment variable to an value in hex
333  *
334  * @param varname       Environment variable to set
335  * @param value         Value to set it to
336  * @return 0 if ok, 1 on error
337  */
338 int env_set_hex(const char *varname, ulong value)
339 {
340         char str[17];
341
342         sprintf(str, "%lx", value);
343         return env_set(varname, str);
344 }
345
346 ulong env_get_hex(const char *varname, ulong default_val)
347 {
348         const char *s;
349         ulong value;
350         char *endp;
351
352         s = env_get(varname);
353         if (s)
354                 value = simple_strtoul(s, &endp, 16);
355         if (!s || endp == s)
356                 return default_val;
357
358         return value;
359 }
360
361 void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
362 {
363         char *end;
364         int i;
365
366         for (i = 0; i < 6; ++i) {
367                 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
368                 if (addr)
369                         addr = (*end) ? end + 1 : end;
370         }
371 }
372
373 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
374 {
375         eth_parse_enetaddr(env_get(name), enetaddr);
376         return is_valid_ethaddr(enetaddr);
377 }
378
379 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
380 {
381         char buf[ARP_HLEN_ASCII + 1];
382
383         if (eth_env_get_enetaddr(name, (uint8_t *)buf))
384                 return -EEXIST;
385
386         sprintf(buf, "%pM", enetaddr);
387
388         return env_set(name, buf);
389 }
390
391 #ifndef CONFIG_SPL_BUILD
392 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
393 {
394         if (argc < 2)
395                 return CMD_RET_USAGE;
396
397         return _do_env_set(flag, argc, argv, H_INTERACTIVE);
398 }
399
400 /*
401  * Prompt for environment variable
402  */
403 #if defined(CONFIG_CMD_ASKENV)
404 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
405 {
406         char message[CONFIG_SYS_CBSIZE];
407         int i, len, pos, size;
408         char *local_args[4];
409         char *endptr;
410
411         local_args[0] = argv[0];
412         local_args[1] = argv[1];
413         local_args[2] = NULL;
414         local_args[3] = NULL;
415
416         /*
417          * Check the syntax:
418          *
419          * env_ask envname [message1 ...] [size]
420          */
421         if (argc == 1)
422                 return CMD_RET_USAGE;
423
424         /*
425          * We test the last argument if it can be converted
426          * into a decimal number.  If yes, we assume it's
427          * the size.  Otherwise we echo it as part of the
428          * message.
429          */
430         i = simple_strtoul(argv[argc - 1], &endptr, 10);
431         if (*endptr != '\0') {                  /* no size */
432                 size = CONFIG_SYS_CBSIZE - 1;
433         } else {                                /* size given */
434                 size = i;
435                 --argc;
436         }
437
438         if (argc <= 2) {
439                 sprintf(message, "Please enter '%s': ", argv[1]);
440         } else {
441                 /* env_ask envname message1 ... messagen [size] */
442                 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
443                         if (pos)
444                                 message[pos++] = ' ';
445
446                         strncpy(message + pos, argv[i], sizeof(message) - pos);
447                         pos += strlen(argv[i]);
448                 }
449                 if (pos < sizeof(message) - 1) {
450                         message[pos++] = ' ';
451                         message[pos] = '\0';
452                 } else
453                         message[CONFIG_SYS_CBSIZE - 1] = '\0';
454         }
455
456         if (size >= CONFIG_SYS_CBSIZE)
457                 size = CONFIG_SYS_CBSIZE - 1;
458
459         if (size <= 0)
460                 return 1;
461
462         /* prompt for input */
463         len = cli_readline(message);
464
465         if (size < len)
466                 console_buffer[size] = '\0';
467
468         len = 2;
469         if (console_buffer[0] != '\0') {
470                 local_args[2] = console_buffer;
471                 len = 3;
472         }
473
474         /* Continue calling setenv code */
475         return _do_env_set(flag, len, local_args, H_INTERACTIVE);
476 }
477 #endif
478
479 #if defined(CONFIG_CMD_ENV_CALLBACK)
480 static int print_static_binding(const char *var_name, const char *callback_name,
481                                 void *priv)
482 {
483         printf("\t%-20s %-20s\n", var_name, callback_name);
484
485         return 0;
486 }
487
488 static int print_active_callback(ENTRY *entry)
489 {
490         struct env_clbk_tbl *clbkp;
491         int i;
492         int num_callbacks;
493
494         if (entry->callback == NULL)
495                 return 0;
496
497         /* look up the callback in the linker-list */
498         num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
499         for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
500              i < num_callbacks;
501              i++, clbkp++) {
502 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
503                 if (entry->callback == clbkp->callback + gd->reloc_off)
504 #else
505                 if (entry->callback == clbkp->callback)
506 #endif
507                         break;
508         }
509
510         if (i == num_callbacks)
511                 /* this should probably never happen, but just in case... */
512                 printf("\t%-20s %p\n", entry->key, entry->callback);
513         else
514                 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
515
516         return 0;
517 }
518
519 /*
520  * Print the callbacks available and what they are bound to
521  */
522 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
523 {
524         struct env_clbk_tbl *clbkp;
525         int i;
526         int num_callbacks;
527
528         /* Print the available callbacks */
529         puts("Available callbacks:\n");
530         puts("\tCallback Name\n");
531         puts("\t-------------\n");
532         num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
533         for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
534              i < num_callbacks;
535              i++, clbkp++)
536                 printf("\t%s\n", clbkp->name);
537         puts("\n");
538
539         /* Print the static bindings that may exist */
540         puts("Static callback bindings:\n");
541         printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
542         printf("\t%-20s %-20s\n", "-------------", "-------------");
543         env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
544         puts("\n");
545
546         /* walk through each variable and print the callback if it has one */
547         puts("Active callback bindings:\n");
548         printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
549         printf("\t%-20s %-20s\n", "-------------", "-------------");
550         hwalk_r(&env_htab, print_active_callback);
551         return 0;
552 }
553 #endif
554
555 #if defined(CONFIG_CMD_ENV_FLAGS)
556 static int print_static_flags(const char *var_name, const char *flags,
557                               void *priv)
558 {
559         enum env_flags_vartype type = env_flags_parse_vartype(flags);
560         enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
561
562         printf("\t%-20s %-20s %-20s\n", var_name,
563                 env_flags_get_vartype_name(type),
564                 env_flags_get_varaccess_name(access));
565
566         return 0;
567 }
568
569 static int print_active_flags(ENTRY *entry)
570 {
571         enum env_flags_vartype type;
572         enum env_flags_varaccess access;
573
574         if (entry->flags == 0)
575                 return 0;
576
577         type = (enum env_flags_vartype)
578                 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
579         access = env_flags_parse_varaccess_from_binflags(entry->flags);
580         printf("\t%-20s %-20s %-20s\n", entry->key,
581                 env_flags_get_vartype_name(type),
582                 env_flags_get_varaccess_name(access));
583
584         return 0;
585 }
586
587 /*
588  * Print the flags available and what variables have flags
589  */
590 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
591 {
592         /* Print the available variable types */
593         printf("Available variable type flags (position %d):\n",
594                 ENV_FLAGS_VARTYPE_LOC);
595         puts("\tFlag\tVariable Type Name\n");
596         puts("\t----\t------------------\n");
597         env_flags_print_vartypes();
598         puts("\n");
599
600         /* Print the available variable access types */
601         printf("Available variable access flags (position %d):\n",
602                 ENV_FLAGS_VARACCESS_LOC);
603         puts("\tFlag\tVariable Access Name\n");
604         puts("\t----\t--------------------\n");
605         env_flags_print_varaccess();
606         puts("\n");
607
608         /* Print the static flags that may exist */
609         puts("Static flags:\n");
610         printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
611                 "Variable Access");
612         printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
613                 "---------------");
614         env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
615         puts("\n");
616
617         /* walk through each variable and print the flags if non-default */
618         puts("Active flags:\n");
619         printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
620                 "Variable Access");
621         printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
622                 "---------------");
623         hwalk_r(&env_htab, print_active_flags);
624         return 0;
625 }
626 #endif
627
628 /*
629  * Interactively edit an environment variable
630  */
631 #if defined(CONFIG_CMD_EDITENV)
632 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
633                        char * const argv[])
634 {
635         char buffer[CONFIG_SYS_CBSIZE];
636         char *init_val;
637
638         if (argc < 2)
639                 return CMD_RET_USAGE;
640
641         /* before import into hashtable */
642         if (!(gd->flags & GD_FLG_ENV_READY))
643                 return 1;
644
645         /* Set read buffer to initial value or empty sting */
646         init_val = env_get(argv[1]);
647         if (init_val)
648                 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
649         else
650                 buffer[0] = '\0';
651
652         if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
653                 return 1;
654
655         if (buffer[0] == '\0') {
656                 const char * const _argv[3] = { "setenv", argv[1], NULL };
657
658                 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
659         } else {
660                 const char * const _argv[4] = { "setenv", argv[1], buffer,
661                         NULL };
662
663                 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
664         }
665 }
666 #endif /* CONFIG_CMD_EDITENV */
667 #endif /* CONFIG_SPL_BUILD */
668
669 /*
670  * Look up variable from environment,
671  * return address of storage for that variable,
672  * or NULL if not found
673  */
674 char *env_get(const char *name)
675 {
676         if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
677                 ENTRY e, *ep;
678
679                 WATCHDOG_RESET();
680
681                 e.key   = name;
682                 e.data  = NULL;
683                 hsearch_r(e, FIND, &ep, &env_htab, 0);
684
685                 return ep ? ep->data : NULL;
686         }
687
688         /* restricted capabilities before import */
689         if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
690                 return (char *)(gd->env_buf);
691
692         return NULL;
693 }
694
695 /*
696  * Look up variable from environment for restricted C runtime env.
697  */
698 int env_get_f(const char *name, char *buf, unsigned len)
699 {
700         int i, nxt, c;
701
702         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
703                 int val, n;
704
705                 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
706                         if (c < 0)
707                                 return c;
708                         if (nxt >= CONFIG_ENV_SIZE)
709                                 return -1;
710                 }
711
712                 val = envmatch((uchar *)name, i);
713                 if (val < 0)
714                         continue;
715
716                 /* found; copy out */
717                 for (n = 0; n < len; ++n, ++buf) {
718                         c = env_get_char(val++);
719                         if (c < 0)
720                                 return c;
721                         *buf = c;
722                         if (*buf == '\0')
723                                 return n;
724                 }
725
726                 if (n)
727                         *--buf = '\0';
728
729                 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
730                        len, name);
731
732                 return n;
733         }
734
735         return -1;
736 }
737
738 /**
739  * Decode the integer value of an environment variable and return it.
740  *
741  * @param name          Name of environment variable
742  * @param base          Number base to use (normally 10, or 16 for hex)
743  * @param default_val   Default value to return if the variable is not
744  *                      found
745  * @return the decoded value, or default_val if not found
746  */
747 ulong env_get_ulong(const char *name, int base, ulong default_val)
748 {
749         /*
750          * We can use env_get() here, even before relocation, since the
751          * environment variable value is an integer and thus short.
752          */
753         const char *str = env_get(name);
754
755         return str ? simple_strtoul(str, NULL, base) : default_val;
756 }
757
758 #ifndef CONFIG_SPL_BUILD
759 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
760 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
761                        char * const argv[])
762 {
763         return env_save() ? 1 : 0;
764 }
765
766 U_BOOT_CMD(
767         saveenv, 1, 0,  do_env_save,
768         "save environment variables to persistent storage",
769         ""
770 );
771
772 #if defined(CONFIG_CMD_ERASEENV)
773 static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
774                         char * const argv[])
775 {
776         return env_erase() ? 1 : 0;
777 }
778
779 U_BOOT_CMD(
780         eraseenv, 1, 0, do_env_erase,
781         "erase environment variables from persistent storage",
782         ""
783 );
784 #endif
785 #endif
786 #endif /* CONFIG_SPL_BUILD */
787
788
789 /*
790  * Match a name / name=value pair
791  *
792  * s1 is either a simple 'name', or a 'name=value' pair.
793  * i2 is the environment index for a 'name2=value2' pair.
794  * If the names match, return the index for the value2, else -1.
795  */
796 int envmatch(uchar *s1, int i2)
797 {
798         if (s1 == NULL)
799                 return -1;
800
801         while (*s1 == env_get_char(i2++))
802                 if (*s1++ == '=')
803                         return i2;
804
805         if (*s1 == '\0' && env_get_char(i2-1) == '=')
806                 return i2;
807
808         return -1;
809 }
810
811 #ifndef CONFIG_SPL_BUILD
812 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
813                           int argc, char * const argv[])
814 {
815         int all = 0, env_flag = H_INTERACTIVE;
816
817         debug("Initial value for argc=%d\n", argc);
818         while (--argc > 0 && **++argv == '-') {
819                 char *arg = *argv;
820
821                 while (*++arg) {
822                         switch (*arg) {
823                         case 'a':               /* default all */
824                                 all = 1;
825                                 break;
826                         case 'f':               /* force */
827                                 env_flag |= H_FORCE;
828                                 break;
829                         default:
830                                 return cmd_usage(cmdtp);
831                         }
832                 }
833         }
834         debug("Final value for argc=%d\n", argc);
835         if (all && (argc == 0)) {
836                 /* Reset the whole environment */
837                 set_default_env("## Resetting to default environment\n",
838                                 env_flag);
839                 return 0;
840         }
841         if (!all && (argc > 0)) {
842                 /* Reset individual variables */
843                 set_default_vars(argc, argv, env_flag);
844                 return 0;
845         }
846
847         return cmd_usage(cmdtp);
848 }
849
850 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
851                          int argc, char * const argv[])
852 {
853         int env_flag = H_INTERACTIVE;
854         int ret = 0;
855
856         debug("Initial value for argc=%d\n", argc);
857         while (argc > 1 && **(argv + 1) == '-') {
858                 char *arg = *++argv;
859
860                 --argc;
861                 while (*++arg) {
862                         switch (*arg) {
863                         case 'f':               /* force */
864                                 env_flag |= H_FORCE;
865                                 break;
866                         default:
867                                 return CMD_RET_USAGE;
868                         }
869                 }
870         }
871         debug("Final value for argc=%d\n", argc);
872
873         env_id++;
874
875         while (--argc > 0) {
876                 char *name = *++argv;
877
878                 if (!hdelete_r(name, &env_htab, env_flag))
879                         ret = 1;
880         }
881
882         return ret;
883 }
884
885 #ifdef CONFIG_CMD_EXPORTENV
886 /*
887  * env export [-t | -b | -c] [-s size] addr [var ...]
888  *      -t:     export as text format; if size is given, data will be
889  *              padded with '\0' bytes; if not, one terminating '\0'
890  *              will be added (which is included in the "filesize"
891  *              setting so you can for exmple copy this to flash and
892  *              keep the termination).
893  *      -b:     export as binary format (name=value pairs separated by
894  *              '\0', list end marked by double "\0\0")
895  *      -c:     export as checksum protected environment format as
896  *              used for example by "saveenv" command
897  *      -s size:
898  *              size of output buffer
899  *      addr:   memory address where environment gets stored
900  *      var...  List of variable names that get included into the
901  *              export. Without arguments, the whole environment gets
902  *              exported.
903  *
904  * With "-c" and size is NOT given, then the export command will
905  * format the data as currently used for the persistent storage,
906  * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
907  * prepend a valid CRC32 checksum and, in case of redundant
908  * environment, a "current" redundancy flag. If size is given, this
909  * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
910  * checksum and redundancy flag will be inserted.
911  *
912  * With "-b" and "-t", always only the real data (including a
913  * terminating '\0' byte) will be written; here the optional size
914  * argument will be used to make sure not to overflow the user
915  * provided buffer; the command will abort if the size is not
916  * sufficient. Any remaining space will be '\0' padded.
917  *
918  * On successful return, the variable "filesize" will be set.
919  * Note that filesize includes the trailing/terminating '\0' byte(s).
920  *
921  * Usage scenario:  create a text snapshot/backup of the current settings:
922  *
923  *      => env export -t 100000
924  *      => era ${backup_addr} +${filesize}
925  *      => cp.b 100000 ${backup_addr} ${filesize}
926  *
927  * Re-import this snapshot, deleting all other settings:
928  *
929  *      => env import -d -t ${backup_addr}
930  */
931 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
932                          int argc, char * const argv[])
933 {
934         char    buf[32];
935         ulong   addr;
936         char    *ptr, *cmd, *res;
937         size_t  size = 0;
938         ssize_t len;
939         env_t   *envp;
940         char    sep = '\n';
941         int     chk = 0;
942         int     fmt = 0;
943
944         cmd = *argv;
945
946         while (--argc > 0 && **++argv == '-') {
947                 char *arg = *argv;
948                 while (*++arg) {
949                         switch (*arg) {
950                         case 'b':               /* raw binary format */
951                                 if (fmt++)
952                                         goto sep_err;
953                                 sep = '\0';
954                                 break;
955                         case 'c':               /* external checksum format */
956                                 if (fmt++)
957                                         goto sep_err;
958                                 sep = '\0';
959                                 chk = 1;
960                                 break;
961                         case 's':               /* size given */
962                                 if (--argc <= 0)
963                                         return cmd_usage(cmdtp);
964                                 size = simple_strtoul(*++argv, NULL, 16);
965                                 goto NXTARG;
966                         case 't':               /* text format */
967                                 if (fmt++)
968                                         goto sep_err;
969                                 sep = '\n';
970                                 break;
971                         default:
972                                 return CMD_RET_USAGE;
973                         }
974                 }
975 NXTARG:         ;
976         }
977
978         if (argc < 1)
979                 return CMD_RET_USAGE;
980
981         addr = simple_strtoul(argv[0], NULL, 16);
982         ptr = map_sysmem(addr, size);
983
984         if (size)
985                 memset(ptr, '\0', size);
986
987         argc--;
988         argv++;
989
990         if (sep) {              /* export as text file */
991                 len = hexport_r(&env_htab, sep,
992                                 H_MATCH_KEY | H_MATCH_IDENT,
993                                 &ptr, size, argc, argv);
994                 if (len < 0) {
995                         pr_err("## Error: Cannot export environment: errno = %d\n",
996                                errno);
997                         return 1;
998                 }
999                 sprintf(buf, "%zX", (size_t)len);
1000                 env_set("filesize", buf);
1001
1002                 return 0;
1003         }
1004
1005         envp = (env_t *)ptr;
1006
1007         if (chk)                /* export as checksum protected block */
1008                 res = (char *)envp->data;
1009         else                    /* export as raw binary data */
1010                 res = ptr;
1011
1012         len = hexport_r(&env_htab, '\0',
1013                         H_MATCH_KEY | H_MATCH_IDENT,
1014                         &res, ENV_SIZE, argc, argv);
1015         if (len < 0) {
1016                 pr_err("## Error: Cannot export environment: errno = %d\n",
1017                        errno);
1018                 return 1;
1019         }
1020
1021         if (chk) {
1022                 envp->crc = crc32(0, envp->data,
1023                                 size ? size - offsetof(env_t, data) : ENV_SIZE);
1024 #ifdef CONFIG_ENV_ADDR_REDUND
1025                 envp->flags = ACTIVE_FLAG;
1026 #endif
1027         }
1028         env_set_hex("filesize", len + offsetof(env_t, data));
1029
1030         return 0;
1031
1032 sep_err:
1033         printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1034                cmd);
1035         return 1;
1036 }
1037 #endif
1038
1039 #ifdef CONFIG_CMD_IMPORTENV
1040 /*
1041  * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1042  *      -d:     delete existing environment before importing if no var is
1043  *              passed; if vars are passed, if one var is in the current
1044  *              environment but not in the environment at addr, delete var from
1045  *              current environment;
1046  *              otherwise overwrite / append to existing definitions
1047  *      -t:     assume text format; either "size" must be given or the
1048  *              text data must be '\0' terminated
1049  *      -r:     handle CRLF like LF, that means exported variables with
1050  *              a content which ends with \r won't get imported. Used
1051  *              to import text files created with editors which are using CRLF
1052  *              for line endings. Only effective in addition to -t.
1053  *      -b:     assume binary format ('\0' separated, "\0\0" terminated)
1054  *      -c:     assume checksum protected environment format
1055  *      addr:   memory address to read from
1056  *      size:   length of input data; if missing, proper '\0'
1057  *              termination is mandatory
1058  *              if var is set and size should be missing (i.e. '\0'
1059  *              termination), set size to '-'
1060  *      var...  List of the names of the only variables that get imported from
1061  *              the environment at address 'addr'. Without arguments, the whole
1062  *              environment gets imported.
1063  */
1064 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1065                          int argc, char * const argv[])
1066 {
1067         ulong   addr;
1068         char    *cmd, *ptr;
1069         char    sep = '\n';
1070         int     chk = 0;
1071         int     fmt = 0;
1072         int     del = 0;
1073         int     crlf_is_lf = 0;
1074         int     wl = 0;
1075         size_t  size;
1076
1077         cmd = *argv;
1078
1079         while (--argc > 0 && **++argv == '-') {
1080                 char *arg = *argv;
1081                 while (*++arg) {
1082                         switch (*arg) {
1083                         case 'b':               /* raw binary format */
1084                                 if (fmt++)
1085                                         goto sep_err;
1086                                 sep = '\0';
1087                                 break;
1088                         case 'c':               /* external checksum format */
1089                                 if (fmt++)
1090                                         goto sep_err;
1091                                 sep = '\0';
1092                                 chk = 1;
1093                                 break;
1094                         case 't':               /* text format */
1095                                 if (fmt++)
1096                                         goto sep_err;
1097                                 sep = '\n';
1098                                 break;
1099                         case 'r':               /* handle CRLF like LF */
1100                                 crlf_is_lf = 1;
1101                                 break;
1102                         case 'd':
1103                                 del = 1;
1104                                 break;
1105                         default:
1106                                 return CMD_RET_USAGE;
1107                         }
1108                 }
1109         }
1110
1111         if (argc < 1)
1112                 return CMD_RET_USAGE;
1113
1114         if (!fmt)
1115                 printf("## Warning: defaulting to text format\n");
1116
1117         if (sep != '\n' && crlf_is_lf )
1118                 crlf_is_lf = 0;
1119
1120         addr = simple_strtoul(argv[0], NULL, 16);
1121         ptr = map_sysmem(addr, 0);
1122
1123         if (argc >= 2 && strcmp(argv[1], "-")) {
1124                 size = simple_strtoul(argv[1], NULL, 16);
1125         } else if (chk) {
1126                 puts("## Error: external checksum format must pass size\n");
1127                 return CMD_RET_FAILURE;
1128         } else {
1129                 char *s = ptr;
1130
1131                 size = 0;
1132
1133                 while (size < MAX_ENV_SIZE) {
1134                         if ((*s == sep) && (*(s+1) == '\0'))
1135                                 break;
1136                         ++s;
1137                         ++size;
1138                 }
1139                 if (size == MAX_ENV_SIZE) {
1140                         printf("## Warning: Input data exceeds %d bytes"
1141                                 " - truncated\n", MAX_ENV_SIZE);
1142                 }
1143                 size += 2;
1144                 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1145         }
1146
1147         if (argc > 2)
1148                 wl = 1;
1149
1150         if (chk) {
1151                 uint32_t crc;
1152                 env_t *ep = (env_t *)ptr;
1153
1154                 size -= offsetof(env_t, data);
1155                 memcpy(&crc, &ep->crc, sizeof(crc));
1156
1157                 if (crc32(0, ep->data, size) != crc) {
1158                         puts("## Error: bad CRC, import failed\n");
1159                         return 1;
1160                 }
1161                 ptr = (char *)ep->data;
1162         }
1163
1164         if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1165                        crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1166                 pr_err("## Error: Environment import failed: errno = %d\n",
1167                        errno);
1168                 return 1;
1169         }
1170         gd->flags |= GD_FLG_ENV_READY;
1171
1172         return 0;
1173
1174 sep_err:
1175         printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1176                 cmd);
1177         return 1;
1178 }
1179 #endif
1180
1181 #if defined(CONFIG_CMD_NVEDIT_INFO)
1182 /*
1183  * print_env_info - print environment information
1184  */
1185 static int print_env_info(void)
1186 {
1187         const char *value;
1188
1189         /* print environment validity value */
1190         switch (gd->env_valid) {
1191         case ENV_INVALID:
1192                 value = "invalid";
1193                 break;
1194         case ENV_VALID:
1195                 value = "valid";
1196                 break;
1197         case ENV_REDUND:
1198                 value = "redundant";
1199                 break;
1200         default:
1201                 value = "unknown";
1202                 break;
1203         }
1204         printf("env_valid = %s\n", value);
1205
1206         /* print environment ready flag */
1207         value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1208         printf("env_ready = %s\n", value);
1209
1210         /* print environment using default flag */
1211         value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1212         printf("env_use_default = %s\n", value);
1213
1214         return CMD_RET_SUCCESS;
1215 }
1216
1217 #define ENV_INFO_IS_DEFAULT     BIT(0) /* default environment bit mask */
1218 #define ENV_INFO_IS_PERSISTED   BIT(1) /* environment persistence bit mask */
1219
1220 /*
1221  * env info - display environment information
1222  * env info [-d] - evaluate whether default environment is used
1223  * env info [-p] - evaluate whether environment can be persisted
1224  */
1225 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1226                        int argc, char * const argv[])
1227 {
1228         int eval_flags = 0;
1229         int eval_results = 0;
1230
1231         /* display environment information */
1232         if (argc <= 1)
1233                 return print_env_info();
1234
1235         /* process options */
1236         while (--argc > 0 && **++argv == '-') {
1237                 char *arg = *argv;
1238
1239                 while (*++arg) {
1240                         switch (*arg) {
1241                         case 'd':
1242                                 eval_flags |= ENV_INFO_IS_DEFAULT;
1243                                 break;
1244                         case 'p':
1245                                 eval_flags |= ENV_INFO_IS_PERSISTED;
1246                                 break;
1247                         default:
1248                                 return CMD_RET_USAGE;
1249                         }
1250                 }
1251         }
1252
1253         /* evaluate whether default environment is used */
1254         if (eval_flags & ENV_INFO_IS_DEFAULT) {
1255                 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1256                         printf("Default environment is used\n");
1257                         eval_results |= ENV_INFO_IS_DEFAULT;
1258                 } else {
1259                         printf("Environment was loaded from persistent storage\n");
1260                 }
1261         }
1262
1263         /* evaluate whether environment can be persisted */
1264         if (eval_flags & ENV_INFO_IS_PERSISTED) {
1265 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1266                 printf("Environment can be persisted\n");
1267                 eval_results |= ENV_INFO_IS_PERSISTED;
1268 #else
1269                 printf("Environment cannot be persisted\n");
1270 #endif
1271         }
1272
1273         /* The result of evaluations is combined with AND */
1274         if (eval_flags != eval_results)
1275                 return CMD_RET_FAILURE;
1276
1277         return CMD_RET_SUCCESS;
1278 }
1279 #endif
1280
1281 #if defined(CONFIG_CMD_ENV_EXISTS)
1282 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1283                        char * const argv[])
1284 {
1285         ENTRY e, *ep;
1286
1287         if (argc < 2)
1288                 return CMD_RET_USAGE;
1289
1290         e.key = argv[1];
1291         e.data = NULL;
1292         hsearch_r(e, FIND, &ep, &env_htab, 0);
1293
1294         return (ep == NULL) ? 1 : 0;
1295 }
1296 #endif
1297
1298 /*
1299  * New command line interface: "env" command with subcommands
1300  */
1301 static cmd_tbl_t cmd_env_sub[] = {
1302 #if defined(CONFIG_CMD_ASKENV)
1303         U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1304 #endif
1305         U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1306         U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1307 #if defined(CONFIG_CMD_EDITENV)
1308         U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1309 #endif
1310 #if defined(CONFIG_CMD_ENV_CALLBACK)
1311         U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1312 #endif
1313 #if defined(CONFIG_CMD_ENV_FLAGS)
1314         U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1315 #endif
1316 #if defined(CONFIG_CMD_EXPORTENV)
1317         U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1318 #endif
1319 #if defined(CONFIG_CMD_GREPENV)
1320         U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1321 #endif
1322 #if defined(CONFIG_CMD_IMPORTENV)
1323         U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1324 #endif
1325 #if defined(CONFIG_CMD_NVEDIT_INFO)
1326         U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1327 #endif
1328         U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1329 #if defined(CONFIG_CMD_RUN)
1330         U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1331 #endif
1332 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1333         U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1334 #if defined(CONFIG_CMD_ERASEENV)
1335         U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1336 #endif
1337 #endif
1338         U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1339 #if defined(CONFIG_CMD_ENV_EXISTS)
1340         U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1341 #endif
1342 };
1343
1344 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1345 void env_reloc(void)
1346 {
1347         fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1348 }
1349 #endif
1350
1351 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1352 {
1353         cmd_tbl_t *cp;
1354
1355         if (argc < 2)
1356                 return CMD_RET_USAGE;
1357
1358         /* drop initial "env" arg */
1359         argc--;
1360         argv++;
1361
1362         cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1363
1364         if (cp)
1365                 return cp->cmd(cmdtp, flag, argc, argv);
1366
1367         return CMD_RET_USAGE;
1368 }
1369
1370 #ifdef CONFIG_SYS_LONGHELP
1371 static char env_help_text[] =
1372 #if defined(CONFIG_CMD_ASKENV)
1373         "ask name [message] [size] - ask for environment variable\nenv "
1374 #endif
1375 #if defined(CONFIG_CMD_ENV_CALLBACK)
1376         "callbacks - print callbacks and their associated variables\nenv "
1377 #endif
1378         "default [-f] -a - [forcibly] reset default environment\n"
1379         "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1380         "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1381 #if defined(CONFIG_CMD_EDITENV)
1382         "env edit name - edit environment variable\n"
1383 #endif
1384 #if defined(CONFIG_CMD_ENV_EXISTS)
1385         "env exists name - tests for existence of variable\n"
1386 #endif
1387 #if defined(CONFIG_CMD_EXPORTENV)
1388         "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1389 #endif
1390 #if defined(CONFIG_CMD_ENV_FLAGS)
1391         "env flags - print variables that have non-default flags\n"
1392 #endif
1393 #if defined(CONFIG_CMD_GREPENV)
1394 #ifdef CONFIG_REGEX
1395         "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1396 #else
1397         "env grep [-n | -v | -b] string [...] - search environment\n"
1398 #endif
1399 #endif
1400 #if defined(CONFIG_CMD_IMPORTENV)
1401         "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1402 #endif
1403 #if defined(CONFIG_CMD_NVEDIT_INFO)
1404         "env info - display environment information\n"
1405         "env info [-d] - whether default environment is used\n"
1406         "env info [-p] - whether environment can be persisted\n"
1407 #endif
1408         "env print [-a | name ...] - print environment\n"
1409 #if defined(CONFIG_CMD_NVEDIT_EFI)
1410         "env print -e [name ...] - print UEFI environment\n"
1411 #endif
1412 #if defined(CONFIG_CMD_RUN)
1413         "env run var [...] - run commands in an environment variable\n"
1414 #endif
1415 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1416         "env save - save environment\n"
1417 #if defined(CONFIG_CMD_ERASEENV)
1418         "env erase - erase environment\n"
1419 #endif
1420 #endif
1421 #if defined(CONFIG_CMD_NVEDIT_EFI)
1422         "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1423 #endif
1424         "env set [-f] name [arg ...]\n";
1425 #endif
1426
1427 U_BOOT_CMD(
1428         env, CONFIG_SYS_MAXARGS, 1, do_env,
1429         "environment handling commands", env_help_text
1430 );
1431
1432 /*
1433  * Old command line interface, kept for compatibility
1434  */
1435
1436 #if defined(CONFIG_CMD_EDITENV)
1437 U_BOOT_CMD_COMPLETE(
1438         editenv, 2, 0,  do_env_edit,
1439         "edit environment variable",
1440         "name\n"
1441         "    - edit environment variable 'name'",
1442         var_complete
1443 );
1444 #endif
1445
1446 U_BOOT_CMD_COMPLETE(
1447         printenv, CONFIG_SYS_MAXARGS, 1,        do_env_print,
1448         "print environment variables",
1449         "[-a]\n    - print [all] values of all environment variables\n"
1450 #if defined(CONFIG_CMD_NVEDIT_EFI)
1451         "printenv -e [name ...]\n"
1452         "    - print UEFI variable 'name' or all the variables\n"
1453 #endif
1454         "printenv name ...\n"
1455         "    - print value of environment variable 'name'",
1456         var_complete
1457 );
1458
1459 #ifdef CONFIG_CMD_GREPENV
1460 U_BOOT_CMD_COMPLETE(
1461         grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
1462         "search environment variables",
1463 #ifdef CONFIG_REGEX
1464         "[-e] [-n | -v | -b] string ...\n"
1465 #else
1466         "[-n | -v | -b] string ...\n"
1467 #endif
1468         "    - list environment name=value pairs matching 'string'\n"
1469 #ifdef CONFIG_REGEX
1470         "      \"-e\": enable regular expressions;\n"
1471 #endif
1472         "      \"-n\": search variable names; \"-v\": search values;\n"
1473         "      \"-b\": search both names and values (default)",
1474         var_complete
1475 );
1476 #endif
1477
1478 U_BOOT_CMD_COMPLETE(
1479         setenv, CONFIG_SYS_MAXARGS, 0,  do_env_set,
1480         "set environment variables",
1481 #if defined(CONFIG_CMD_NVEDIT_EFI)
1482         "-e [-nv] name [value ...]\n"
1483         "    - set UEFI variable 'name' to 'value' ...'\n"
1484         "      'nv' option makes the variable non-volatile\n"
1485         "    - delete UEFI variable 'name' if 'value' not specified\n"
1486 #endif
1487         "setenv [-f] name value ...\n"
1488         "    - [forcibly] set environment variable 'name' to 'value ...'\n"
1489         "setenv [-f] name\n"
1490         "    - [forcibly] delete environment variable 'name'",
1491         var_complete
1492 );
1493
1494 #if defined(CONFIG_CMD_ASKENV)
1495
1496 U_BOOT_CMD(
1497         askenv, CONFIG_SYS_MAXARGS,     1,      do_env_ask,
1498         "get environment variables from stdin",
1499         "name [message] [size]\n"
1500         "    - get environment variable 'name' from stdin (max 'size' chars)"
1501 );
1502 #endif
1503
1504 #if defined(CONFIG_CMD_RUN)
1505 U_BOOT_CMD_COMPLETE(
1506         run,    CONFIG_SYS_MAXARGS,     1,      do_run,
1507         "run commands in an environment variable",
1508         "var [...]\n"
1509         "    - run the commands in the environment variable(s) 'var'",
1510         var_complete
1511 );
1512 #endif
1513 #endif /* CONFIG_SPL_BUILD */