Merge branch '2019-10-28-azure-ci-support'
[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 <env_internal.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                 struct env_entry e, *ep;
98
99                 e.key = name;
100                 e.data = NULL;
101                 hsearch_r(e, ENV_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         struct env_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, ENV_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 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
362 {
363         eth_parse_enetaddr(env_get(name), enetaddr);
364         return is_valid_ethaddr(enetaddr);
365 }
366
367 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
368 {
369         char buf[ARP_HLEN_ASCII + 1];
370
371         if (eth_env_get_enetaddr(name, (uint8_t *)buf))
372                 return -EEXIST;
373
374         sprintf(buf, "%pM", enetaddr);
375
376         return env_set(name, buf);
377 }
378
379 #ifndef CONFIG_SPL_BUILD
380 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
381 {
382         if (argc < 2)
383                 return CMD_RET_USAGE;
384
385         return _do_env_set(flag, argc, argv, H_INTERACTIVE);
386 }
387
388 /*
389  * Prompt for environment variable
390  */
391 #if defined(CONFIG_CMD_ASKENV)
392 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
393 {
394         char message[CONFIG_SYS_CBSIZE];
395         int i, len, pos, size;
396         char *local_args[4];
397         char *endptr;
398
399         local_args[0] = argv[0];
400         local_args[1] = argv[1];
401         local_args[2] = NULL;
402         local_args[3] = NULL;
403
404         /*
405          * Check the syntax:
406          *
407          * env_ask envname [message1 ...] [size]
408          */
409         if (argc == 1)
410                 return CMD_RET_USAGE;
411
412         /*
413          * We test the last argument if it can be converted
414          * into a decimal number.  If yes, we assume it's
415          * the size.  Otherwise we echo it as part of the
416          * message.
417          */
418         i = simple_strtoul(argv[argc - 1], &endptr, 10);
419         if (*endptr != '\0') {                  /* no size */
420                 size = CONFIG_SYS_CBSIZE - 1;
421         } else {                                /* size given */
422                 size = i;
423                 --argc;
424         }
425
426         if (argc <= 2) {
427                 sprintf(message, "Please enter '%s': ", argv[1]);
428         } else {
429                 /* env_ask envname message1 ... messagen [size] */
430                 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
431                         if (pos)
432                                 message[pos++] = ' ';
433
434                         strncpy(message + pos, argv[i], sizeof(message) - pos);
435                         pos += strlen(argv[i]);
436                 }
437                 if (pos < sizeof(message) - 1) {
438                         message[pos++] = ' ';
439                         message[pos] = '\0';
440                 } else
441                         message[CONFIG_SYS_CBSIZE - 1] = '\0';
442         }
443
444         if (size >= CONFIG_SYS_CBSIZE)
445                 size = CONFIG_SYS_CBSIZE - 1;
446
447         if (size <= 0)
448                 return 1;
449
450         /* prompt for input */
451         len = cli_readline(message);
452
453         if (size < len)
454                 console_buffer[size] = '\0';
455
456         len = 2;
457         if (console_buffer[0] != '\0') {
458                 local_args[2] = console_buffer;
459                 len = 3;
460         }
461
462         /* Continue calling setenv code */
463         return _do_env_set(flag, len, local_args, H_INTERACTIVE);
464 }
465 #endif
466
467 #if defined(CONFIG_CMD_ENV_CALLBACK)
468 static int print_static_binding(const char *var_name, const char *callback_name,
469                                 void *priv)
470 {
471         printf("\t%-20s %-20s\n", var_name, callback_name);
472
473         return 0;
474 }
475
476 static int print_active_callback(struct env_entry *entry)
477 {
478         struct env_clbk_tbl *clbkp;
479         int i;
480         int num_callbacks;
481
482         if (entry->callback == NULL)
483                 return 0;
484
485         /* look up the callback in the linker-list */
486         num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
487         for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
488              i < num_callbacks;
489              i++, clbkp++) {
490 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
491                 if (entry->callback == clbkp->callback + gd->reloc_off)
492 #else
493                 if (entry->callback == clbkp->callback)
494 #endif
495                         break;
496         }
497
498         if (i == num_callbacks)
499                 /* this should probably never happen, but just in case... */
500                 printf("\t%-20s %p\n", entry->key, entry->callback);
501         else
502                 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
503
504         return 0;
505 }
506
507 /*
508  * Print the callbacks available and what they are bound to
509  */
510 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
511 {
512         struct env_clbk_tbl *clbkp;
513         int i;
514         int num_callbacks;
515
516         /* Print the available callbacks */
517         puts("Available callbacks:\n");
518         puts("\tCallback Name\n");
519         puts("\t-------------\n");
520         num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
521         for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
522              i < num_callbacks;
523              i++, clbkp++)
524                 printf("\t%s\n", clbkp->name);
525         puts("\n");
526
527         /* Print the static bindings that may exist */
528         puts("Static callback bindings:\n");
529         printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
530         printf("\t%-20s %-20s\n", "-------------", "-------------");
531         env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
532         puts("\n");
533
534         /* walk through each variable and print the callback if it has one */
535         puts("Active callback bindings:\n");
536         printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
537         printf("\t%-20s %-20s\n", "-------------", "-------------");
538         hwalk_r(&env_htab, print_active_callback);
539         return 0;
540 }
541 #endif
542
543 #if defined(CONFIG_CMD_ENV_FLAGS)
544 static int print_static_flags(const char *var_name, const char *flags,
545                               void *priv)
546 {
547         enum env_flags_vartype type = env_flags_parse_vartype(flags);
548         enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
549
550         printf("\t%-20s %-20s %-20s\n", var_name,
551                 env_flags_get_vartype_name(type),
552                 env_flags_get_varaccess_name(access));
553
554         return 0;
555 }
556
557 static int print_active_flags(struct env_entry *entry)
558 {
559         enum env_flags_vartype type;
560         enum env_flags_varaccess access;
561
562         if (entry->flags == 0)
563                 return 0;
564
565         type = (enum env_flags_vartype)
566                 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
567         access = env_flags_parse_varaccess_from_binflags(entry->flags);
568         printf("\t%-20s %-20s %-20s\n", entry->key,
569                 env_flags_get_vartype_name(type),
570                 env_flags_get_varaccess_name(access));
571
572         return 0;
573 }
574
575 /*
576  * Print the flags available and what variables have flags
577  */
578 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
579 {
580         /* Print the available variable types */
581         printf("Available variable type flags (position %d):\n",
582                 ENV_FLAGS_VARTYPE_LOC);
583         puts("\tFlag\tVariable Type Name\n");
584         puts("\t----\t------------------\n");
585         env_flags_print_vartypes();
586         puts("\n");
587
588         /* Print the available variable access types */
589         printf("Available variable access flags (position %d):\n",
590                 ENV_FLAGS_VARACCESS_LOC);
591         puts("\tFlag\tVariable Access Name\n");
592         puts("\t----\t--------------------\n");
593         env_flags_print_varaccess();
594         puts("\n");
595
596         /* Print the static flags that may exist */
597         puts("Static flags:\n");
598         printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
599                 "Variable Access");
600         printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
601                 "---------------");
602         env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
603         puts("\n");
604
605         /* walk through each variable and print the flags if non-default */
606         puts("Active flags:\n");
607         printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
608                 "Variable Access");
609         printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
610                 "---------------");
611         hwalk_r(&env_htab, print_active_flags);
612         return 0;
613 }
614 #endif
615
616 /*
617  * Interactively edit an environment variable
618  */
619 #if defined(CONFIG_CMD_EDITENV)
620 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
621                        char * const argv[])
622 {
623         char buffer[CONFIG_SYS_CBSIZE];
624         char *init_val;
625
626         if (argc < 2)
627                 return CMD_RET_USAGE;
628
629         /* before import into hashtable */
630         if (!(gd->flags & GD_FLG_ENV_READY))
631                 return 1;
632
633         /* Set read buffer to initial value or empty sting */
634         init_val = env_get(argv[1]);
635         if (init_val)
636                 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
637         else
638                 buffer[0] = '\0';
639
640         if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
641                 return 1;
642
643         if (buffer[0] == '\0') {
644                 const char * const _argv[3] = { "setenv", argv[1], NULL };
645
646                 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
647         } else {
648                 const char * const _argv[4] = { "setenv", argv[1], buffer,
649                         NULL };
650
651                 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
652         }
653 }
654 #endif /* CONFIG_CMD_EDITENV */
655 #endif /* CONFIG_SPL_BUILD */
656
657 /*
658  * Look up variable from environment,
659  * return address of storage for that variable,
660  * or NULL if not found
661  */
662 char *env_get(const char *name)
663 {
664         if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
665                 struct env_entry e, *ep;
666
667                 WATCHDOG_RESET();
668
669                 e.key   = name;
670                 e.data  = NULL;
671                 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
672
673                 return ep ? ep->data : NULL;
674         }
675
676         /* restricted capabilities before import */
677         if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
678                 return (char *)(gd->env_buf);
679
680         return NULL;
681 }
682
683 /*
684  * Look up variable from environment for restricted C runtime env.
685  */
686 int env_get_f(const char *name, char *buf, unsigned len)
687 {
688         int i, nxt, c;
689
690         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
691                 int val, n;
692
693                 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
694                         if (c < 0)
695                                 return c;
696                         if (nxt >= CONFIG_ENV_SIZE)
697                                 return -1;
698                 }
699
700                 val = env_match((uchar *)name, i);
701                 if (val < 0)
702                         continue;
703
704                 /* found; copy out */
705                 for (n = 0; n < len; ++n, ++buf) {
706                         c = env_get_char(val++);
707                         if (c < 0)
708                                 return c;
709                         *buf = c;
710                         if (*buf == '\0')
711                                 return n;
712                 }
713
714                 if (n)
715                         *--buf = '\0';
716
717                 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
718                        len, name);
719
720                 return n;
721         }
722
723         return -1;
724 }
725
726 /**
727  * Decode the integer value of an environment variable and return it.
728  *
729  * @param name          Name of environment variable
730  * @param base          Number base to use (normally 10, or 16 for hex)
731  * @param default_val   Default value to return if the variable is not
732  *                      found
733  * @return the decoded value, or default_val if not found
734  */
735 ulong env_get_ulong(const char *name, int base, ulong default_val)
736 {
737         /*
738          * We can use env_get() here, even before relocation, since the
739          * environment variable value is an integer and thus short.
740          */
741         const char *str = env_get(name);
742
743         return str ? simple_strtoul(str, NULL, base) : default_val;
744 }
745
746 #ifndef CONFIG_SPL_BUILD
747 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
748 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
749                        char * const argv[])
750 {
751         return env_save() ? 1 : 0;
752 }
753
754 U_BOOT_CMD(
755         saveenv, 1, 0,  do_env_save,
756         "save environment variables to persistent storage",
757         ""
758 );
759
760 #if defined(CONFIG_CMD_ERASEENV)
761 static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
762                         char * const argv[])
763 {
764         return env_erase() ? 1 : 0;
765 }
766
767 U_BOOT_CMD(
768         eraseenv, 1, 0, do_env_erase,
769         "erase environment variables from persistent storage",
770         ""
771 );
772 #endif
773 #endif
774 #endif /* CONFIG_SPL_BUILD */
775
776 int env_match(uchar *s1, int i2)
777 {
778         if (s1 == NULL)
779                 return -1;
780
781         while (*s1 == env_get_char(i2++))
782                 if (*s1++ == '=')
783                         return i2;
784
785         if (*s1 == '\0' && env_get_char(i2-1) == '=')
786                 return i2;
787
788         return -1;
789 }
790
791 #ifndef CONFIG_SPL_BUILD
792 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
793                           int argc, char * const argv[])
794 {
795         int all = 0, env_flag = H_INTERACTIVE;
796
797         debug("Initial value for argc=%d\n", argc);
798         while (--argc > 0 && **++argv == '-') {
799                 char *arg = *argv;
800
801                 while (*++arg) {
802                         switch (*arg) {
803                         case 'a':               /* default all */
804                                 all = 1;
805                                 break;
806                         case 'f':               /* force */
807                                 env_flag |= H_FORCE;
808                                 break;
809                         default:
810                                 return cmd_usage(cmdtp);
811                         }
812                 }
813         }
814         debug("Final value for argc=%d\n", argc);
815         if (all && (argc == 0)) {
816                 /* Reset the whole environment */
817                 env_set_default("## Resetting to default environment\n",
818                                 env_flag);
819                 return 0;
820         }
821         if (!all && (argc > 0)) {
822                 /* Reset individual variables */
823                 env_set_default_vars(argc, argv, env_flag);
824                 return 0;
825         }
826
827         return cmd_usage(cmdtp);
828 }
829
830 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
831                          int argc, char * const argv[])
832 {
833         int env_flag = H_INTERACTIVE;
834         int ret = 0;
835
836         debug("Initial value for argc=%d\n", argc);
837         while (argc > 1 && **(argv + 1) == '-') {
838                 char *arg = *++argv;
839
840                 --argc;
841                 while (*++arg) {
842                         switch (*arg) {
843                         case 'f':               /* force */
844                                 env_flag |= H_FORCE;
845                                 break;
846                         default:
847                                 return CMD_RET_USAGE;
848                         }
849                 }
850         }
851         debug("Final value for argc=%d\n", argc);
852
853         env_id++;
854
855         while (--argc > 0) {
856                 char *name = *++argv;
857
858                 if (!hdelete_r(name, &env_htab, env_flag))
859                         ret = 1;
860         }
861
862         return ret;
863 }
864
865 #ifdef CONFIG_CMD_EXPORTENV
866 /*
867  * env export [-t | -b | -c] [-s size] addr [var ...]
868  *      -t:     export as text format; if size is given, data will be
869  *              padded with '\0' bytes; if not, one terminating '\0'
870  *              will be added (which is included in the "filesize"
871  *              setting so you can for exmple copy this to flash and
872  *              keep the termination).
873  *      -b:     export as binary format (name=value pairs separated by
874  *              '\0', list end marked by double "\0\0")
875  *      -c:     export as checksum protected environment format as
876  *              used for example by "saveenv" command
877  *      -s size:
878  *              size of output buffer
879  *      addr:   memory address where environment gets stored
880  *      var...  List of variable names that get included into the
881  *              export. Without arguments, the whole environment gets
882  *              exported.
883  *
884  * With "-c" and size is NOT given, then the export command will
885  * format the data as currently used for the persistent storage,
886  * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
887  * prepend a valid CRC32 checksum and, in case of redundant
888  * environment, a "current" redundancy flag. If size is given, this
889  * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
890  * checksum and redundancy flag will be inserted.
891  *
892  * With "-b" and "-t", always only the real data (including a
893  * terminating '\0' byte) will be written; here the optional size
894  * argument will be used to make sure not to overflow the user
895  * provided buffer; the command will abort if the size is not
896  * sufficient. Any remaining space will be '\0' padded.
897  *
898  * On successful return, the variable "filesize" will be set.
899  * Note that filesize includes the trailing/terminating '\0' byte(s).
900  *
901  * Usage scenario:  create a text snapshot/backup of the current settings:
902  *
903  *      => env export -t 100000
904  *      => era ${backup_addr} +${filesize}
905  *      => cp.b 100000 ${backup_addr} ${filesize}
906  *
907  * Re-import this snapshot, deleting all other settings:
908  *
909  *      => env import -d -t ${backup_addr}
910  */
911 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
912                          int argc, char * const argv[])
913 {
914         char    buf[32];
915         ulong   addr;
916         char    *ptr, *cmd, *res;
917         size_t  size = 0;
918         ssize_t len;
919         env_t   *envp;
920         char    sep = '\n';
921         int     chk = 0;
922         int     fmt = 0;
923
924         cmd = *argv;
925
926         while (--argc > 0 && **++argv == '-') {
927                 char *arg = *argv;
928                 while (*++arg) {
929                         switch (*arg) {
930                         case 'b':               /* raw binary format */
931                                 if (fmt++)
932                                         goto sep_err;
933                                 sep = '\0';
934                                 break;
935                         case 'c':               /* external checksum format */
936                                 if (fmt++)
937                                         goto sep_err;
938                                 sep = '\0';
939                                 chk = 1;
940                                 break;
941                         case 's':               /* size given */
942                                 if (--argc <= 0)
943                                         return cmd_usage(cmdtp);
944                                 size = simple_strtoul(*++argv, NULL, 16);
945                                 goto NXTARG;
946                         case 't':               /* text format */
947                                 if (fmt++)
948                                         goto sep_err;
949                                 sep = '\n';
950                                 break;
951                         default:
952                                 return CMD_RET_USAGE;
953                         }
954                 }
955 NXTARG:         ;
956         }
957
958         if (argc < 1)
959                 return CMD_RET_USAGE;
960
961         addr = simple_strtoul(argv[0], NULL, 16);
962         ptr = map_sysmem(addr, size);
963
964         if (size)
965                 memset(ptr, '\0', size);
966
967         argc--;
968         argv++;
969
970         if (sep) {              /* export as text file */
971                 len = hexport_r(&env_htab, sep,
972                                 H_MATCH_KEY | H_MATCH_IDENT,
973                                 &ptr, size, argc, argv);
974                 if (len < 0) {
975                         pr_err("## Error: Cannot export environment: errno = %d\n",
976                                errno);
977                         return 1;
978                 }
979                 sprintf(buf, "%zX", (size_t)len);
980                 env_set("filesize", buf);
981
982                 return 0;
983         }
984
985         envp = (env_t *)ptr;
986
987         if (chk)                /* export as checksum protected block */
988                 res = (char *)envp->data;
989         else                    /* export as raw binary data */
990                 res = ptr;
991
992         len = hexport_r(&env_htab, '\0',
993                         H_MATCH_KEY | H_MATCH_IDENT,
994                         &res, ENV_SIZE, argc, argv);
995         if (len < 0) {
996                 pr_err("## Error: Cannot export environment: errno = %d\n",
997                        errno);
998                 return 1;
999         }
1000
1001         if (chk) {
1002                 envp->crc = crc32(0, envp->data,
1003                                 size ? size - offsetof(env_t, data) : ENV_SIZE);
1004 #ifdef CONFIG_ENV_ADDR_REDUND
1005                 envp->flags = ENV_REDUND_ACTIVE;
1006 #endif
1007         }
1008         env_set_hex("filesize", len + offsetof(env_t, data));
1009
1010         return 0;
1011
1012 sep_err:
1013         printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1014                cmd);
1015         return 1;
1016 }
1017 #endif
1018
1019 #ifdef CONFIG_CMD_IMPORTENV
1020 /*
1021  * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1022  *      -d:     delete existing environment before importing if no var is
1023  *              passed; if vars are passed, if one var is in the current
1024  *              environment but not in the environment at addr, delete var from
1025  *              current environment;
1026  *              otherwise overwrite / append to existing definitions
1027  *      -t:     assume text format; either "size" must be given or the
1028  *              text data must be '\0' terminated
1029  *      -r:     handle CRLF like LF, that means exported variables with
1030  *              a content which ends with \r won't get imported. Used
1031  *              to import text files created with editors which are using CRLF
1032  *              for line endings. Only effective in addition to -t.
1033  *      -b:     assume binary format ('\0' separated, "\0\0" terminated)
1034  *      -c:     assume checksum protected environment format
1035  *      addr:   memory address to read from
1036  *      size:   length of input data; if missing, proper '\0'
1037  *              termination is mandatory
1038  *              if var is set and size should be missing (i.e. '\0'
1039  *              termination), set size to '-'
1040  *      var...  List of the names of the only variables that get imported from
1041  *              the environment at address 'addr'. Without arguments, the whole
1042  *              environment gets imported.
1043  */
1044 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1045                          int argc, char * const argv[])
1046 {
1047         ulong   addr;
1048         char    *cmd, *ptr;
1049         char    sep = '\n';
1050         int     chk = 0;
1051         int     fmt = 0;
1052         int     del = 0;
1053         int     crlf_is_lf = 0;
1054         int     wl = 0;
1055         size_t  size;
1056
1057         cmd = *argv;
1058
1059         while (--argc > 0 && **++argv == '-') {
1060                 char *arg = *argv;
1061                 while (*++arg) {
1062                         switch (*arg) {
1063                         case 'b':               /* raw binary format */
1064                                 if (fmt++)
1065                                         goto sep_err;
1066                                 sep = '\0';
1067                                 break;
1068                         case 'c':               /* external checksum format */
1069                                 if (fmt++)
1070                                         goto sep_err;
1071                                 sep = '\0';
1072                                 chk = 1;
1073                                 break;
1074                         case 't':               /* text format */
1075                                 if (fmt++)
1076                                         goto sep_err;
1077                                 sep = '\n';
1078                                 break;
1079                         case 'r':               /* handle CRLF like LF */
1080                                 crlf_is_lf = 1;
1081                                 break;
1082                         case 'd':
1083                                 del = 1;
1084                                 break;
1085                         default:
1086                                 return CMD_RET_USAGE;
1087                         }
1088                 }
1089         }
1090
1091         if (argc < 1)
1092                 return CMD_RET_USAGE;
1093
1094         if (!fmt)
1095                 printf("## Warning: defaulting to text format\n");
1096
1097         if (sep != '\n' && crlf_is_lf )
1098                 crlf_is_lf = 0;
1099
1100         addr = simple_strtoul(argv[0], NULL, 16);
1101         ptr = map_sysmem(addr, 0);
1102
1103         if (argc >= 2 && strcmp(argv[1], "-")) {
1104                 size = simple_strtoul(argv[1], NULL, 16);
1105         } else if (chk) {
1106                 puts("## Error: external checksum format must pass size\n");
1107                 return CMD_RET_FAILURE;
1108         } else {
1109                 char *s = ptr;
1110
1111                 size = 0;
1112
1113                 while (size < MAX_ENV_SIZE) {
1114                         if ((*s == sep) && (*(s+1) == '\0'))
1115                                 break;
1116                         ++s;
1117                         ++size;
1118                 }
1119                 if (size == MAX_ENV_SIZE) {
1120                         printf("## Warning: Input data exceeds %d bytes"
1121                                 " - truncated\n", MAX_ENV_SIZE);
1122                 }
1123                 size += 2;
1124                 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1125         }
1126
1127         if (argc > 2)
1128                 wl = 1;
1129
1130         if (chk) {
1131                 uint32_t crc;
1132                 env_t *ep = (env_t *)ptr;
1133
1134                 size -= offsetof(env_t, data);
1135                 memcpy(&crc, &ep->crc, sizeof(crc));
1136
1137                 if (crc32(0, ep->data, size) != crc) {
1138                         puts("## Error: bad CRC, import failed\n");
1139                         return 1;
1140                 }
1141                 ptr = (char *)ep->data;
1142         }
1143
1144         if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1145                        crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1146                 pr_err("## Error: Environment import failed: errno = %d\n",
1147                        errno);
1148                 return 1;
1149         }
1150         gd->flags |= GD_FLG_ENV_READY;
1151
1152         return 0;
1153
1154 sep_err:
1155         printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1156                 cmd);
1157         return 1;
1158 }
1159 #endif
1160
1161 #if defined(CONFIG_CMD_NVEDIT_INFO)
1162 /*
1163  * print_env_info - print environment information
1164  */
1165 static int print_env_info(void)
1166 {
1167         const char *value;
1168
1169         /* print environment validity value */
1170         switch (gd->env_valid) {
1171         case ENV_INVALID:
1172                 value = "invalid";
1173                 break;
1174         case ENV_VALID:
1175                 value = "valid";
1176                 break;
1177         case ENV_REDUND:
1178                 value = "redundant";
1179                 break;
1180         default:
1181                 value = "unknown";
1182                 break;
1183         }
1184         printf("env_valid = %s\n", value);
1185
1186         /* print environment ready flag */
1187         value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1188         printf("env_ready = %s\n", value);
1189
1190         /* print environment using default flag */
1191         value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1192         printf("env_use_default = %s\n", value);
1193
1194         return CMD_RET_SUCCESS;
1195 }
1196
1197 #define ENV_INFO_IS_DEFAULT     BIT(0) /* default environment bit mask */
1198 #define ENV_INFO_IS_PERSISTED   BIT(1) /* environment persistence bit mask */
1199
1200 /*
1201  * env info - display environment information
1202  * env info [-d] - evaluate whether default environment is used
1203  * env info [-p] - evaluate whether environment can be persisted
1204  */
1205 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1206                        int argc, char * const argv[])
1207 {
1208         int eval_flags = 0;
1209         int eval_results = 0;
1210
1211         /* display environment information */
1212         if (argc <= 1)
1213                 return print_env_info();
1214
1215         /* process options */
1216         while (--argc > 0 && **++argv == '-') {
1217                 char *arg = *argv;
1218
1219                 while (*++arg) {
1220                         switch (*arg) {
1221                         case 'd':
1222                                 eval_flags |= ENV_INFO_IS_DEFAULT;
1223                                 break;
1224                         case 'p':
1225                                 eval_flags |= ENV_INFO_IS_PERSISTED;
1226                                 break;
1227                         default:
1228                                 return CMD_RET_USAGE;
1229                         }
1230                 }
1231         }
1232
1233         /* evaluate whether default environment is used */
1234         if (eval_flags & ENV_INFO_IS_DEFAULT) {
1235                 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1236                         printf("Default environment is used\n");
1237                         eval_results |= ENV_INFO_IS_DEFAULT;
1238                 } else {
1239                         printf("Environment was loaded from persistent storage\n");
1240                 }
1241         }
1242
1243         /* evaluate whether environment can be persisted */
1244         if (eval_flags & ENV_INFO_IS_PERSISTED) {
1245 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1246                 printf("Environment can be persisted\n");
1247                 eval_results |= ENV_INFO_IS_PERSISTED;
1248 #else
1249                 printf("Environment cannot be persisted\n");
1250 #endif
1251         }
1252
1253         /* The result of evaluations is combined with AND */
1254         if (eval_flags != eval_results)
1255                 return CMD_RET_FAILURE;
1256
1257         return CMD_RET_SUCCESS;
1258 }
1259 #endif
1260
1261 #if defined(CONFIG_CMD_ENV_EXISTS)
1262 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1263                        char * const argv[])
1264 {
1265         struct env_entry e, *ep;
1266
1267         if (argc < 2)
1268                 return CMD_RET_USAGE;
1269
1270         e.key = argv[1];
1271         e.data = NULL;
1272         hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1273
1274         return (ep == NULL) ? 1 : 0;
1275 }
1276 #endif
1277
1278 /*
1279  * New command line interface: "env" command with subcommands
1280  */
1281 static cmd_tbl_t cmd_env_sub[] = {
1282 #if defined(CONFIG_CMD_ASKENV)
1283         U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1284 #endif
1285         U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1286         U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1287 #if defined(CONFIG_CMD_EDITENV)
1288         U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1289 #endif
1290 #if defined(CONFIG_CMD_ENV_CALLBACK)
1291         U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1292 #endif
1293 #if defined(CONFIG_CMD_ENV_FLAGS)
1294         U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1295 #endif
1296 #if defined(CONFIG_CMD_EXPORTENV)
1297         U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1298 #endif
1299 #if defined(CONFIG_CMD_GREPENV)
1300         U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1301 #endif
1302 #if defined(CONFIG_CMD_IMPORTENV)
1303         U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1304 #endif
1305 #if defined(CONFIG_CMD_NVEDIT_INFO)
1306         U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1307 #endif
1308         U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1309 #if defined(CONFIG_CMD_RUN)
1310         U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1311 #endif
1312 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1313         U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1314 #if defined(CONFIG_CMD_ERASEENV)
1315         U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1316 #endif
1317 #endif
1318         U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1319 #if defined(CONFIG_CMD_ENV_EXISTS)
1320         U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1321 #endif
1322 };
1323
1324 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1325 void env_reloc(void)
1326 {
1327         fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1328 }
1329 #endif
1330
1331 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1332 {
1333         cmd_tbl_t *cp;
1334
1335         if (argc < 2)
1336                 return CMD_RET_USAGE;
1337
1338         /* drop initial "env" arg */
1339         argc--;
1340         argv++;
1341
1342         cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1343
1344         if (cp)
1345                 return cp->cmd(cmdtp, flag, argc, argv);
1346
1347         return CMD_RET_USAGE;
1348 }
1349
1350 #ifdef CONFIG_SYS_LONGHELP
1351 static char env_help_text[] =
1352 #if defined(CONFIG_CMD_ASKENV)
1353         "ask name [message] [size] - ask for environment variable\nenv "
1354 #endif
1355 #if defined(CONFIG_CMD_ENV_CALLBACK)
1356         "callbacks - print callbacks and their associated variables\nenv "
1357 #endif
1358         "default [-f] -a - [forcibly] reset default environment\n"
1359         "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1360         "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1361 #if defined(CONFIG_CMD_EDITENV)
1362         "env edit name - edit environment variable\n"
1363 #endif
1364 #if defined(CONFIG_CMD_ENV_EXISTS)
1365         "env exists name - tests for existence of variable\n"
1366 #endif
1367 #if defined(CONFIG_CMD_EXPORTENV)
1368         "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1369 #endif
1370 #if defined(CONFIG_CMD_ENV_FLAGS)
1371         "env flags - print variables that have non-default flags\n"
1372 #endif
1373 #if defined(CONFIG_CMD_GREPENV)
1374 #ifdef CONFIG_REGEX
1375         "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1376 #else
1377         "env grep [-n | -v | -b] string [...] - search environment\n"
1378 #endif
1379 #endif
1380 #if defined(CONFIG_CMD_IMPORTENV)
1381         "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1382 #endif
1383 #if defined(CONFIG_CMD_NVEDIT_INFO)
1384         "env info - display environment information\n"
1385         "env info [-d] - whether default environment is used\n"
1386         "env info [-p] - whether environment can be persisted\n"
1387 #endif
1388         "env print [-a | name ...] - print environment\n"
1389 #if defined(CONFIG_CMD_NVEDIT_EFI)
1390         "env print -e [name ...] - print UEFI environment\n"
1391 #endif
1392 #if defined(CONFIG_CMD_RUN)
1393         "env run var [...] - run commands in an environment variable\n"
1394 #endif
1395 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1396         "env save - save environment\n"
1397 #if defined(CONFIG_CMD_ERASEENV)
1398         "env erase - erase environment\n"
1399 #endif
1400 #endif
1401 #if defined(CONFIG_CMD_NVEDIT_EFI)
1402         "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1403 #endif
1404         "env set [-f] name [arg ...]\n";
1405 #endif
1406
1407 U_BOOT_CMD(
1408         env, CONFIG_SYS_MAXARGS, 1, do_env,
1409         "environment handling commands", env_help_text
1410 );
1411
1412 /*
1413  * Old command line interface, kept for compatibility
1414  */
1415
1416 #if defined(CONFIG_CMD_EDITENV)
1417 U_BOOT_CMD_COMPLETE(
1418         editenv, 2, 0,  do_env_edit,
1419         "edit environment variable",
1420         "name\n"
1421         "    - edit environment variable 'name'",
1422         var_complete
1423 );
1424 #endif
1425
1426 U_BOOT_CMD_COMPLETE(
1427         printenv, CONFIG_SYS_MAXARGS, 1,        do_env_print,
1428         "print environment variables",
1429         "[-a]\n    - print [all] values of all environment variables\n"
1430 #if defined(CONFIG_CMD_NVEDIT_EFI)
1431         "printenv -e [name ...]\n"
1432         "    - print UEFI variable 'name' or all the variables\n"
1433 #endif
1434         "printenv name ...\n"
1435         "    - print value of environment variable 'name'",
1436         var_complete
1437 );
1438
1439 #ifdef CONFIG_CMD_GREPENV
1440 U_BOOT_CMD_COMPLETE(
1441         grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
1442         "search environment variables",
1443 #ifdef CONFIG_REGEX
1444         "[-e] [-n | -v | -b] string ...\n"
1445 #else
1446         "[-n | -v | -b] string ...\n"
1447 #endif
1448         "    - list environment name=value pairs matching 'string'\n"
1449 #ifdef CONFIG_REGEX
1450         "      \"-e\": enable regular expressions;\n"
1451 #endif
1452         "      \"-n\": search variable names; \"-v\": search values;\n"
1453         "      \"-b\": search both names and values (default)",
1454         var_complete
1455 );
1456 #endif
1457
1458 U_BOOT_CMD_COMPLETE(
1459         setenv, CONFIG_SYS_MAXARGS, 0,  do_env_set,
1460         "set environment variables",
1461 #if defined(CONFIG_CMD_NVEDIT_EFI)
1462         "-e [-nv] name [value ...]\n"
1463         "    - set UEFI variable 'name' to 'value' ...'\n"
1464         "      'nv' option makes the variable non-volatile\n"
1465         "    - delete UEFI variable 'name' if 'value' not specified\n"
1466 #endif
1467         "setenv [-f] name value ...\n"
1468         "    - [forcibly] set environment variable 'name' to 'value ...'\n"
1469         "setenv [-f] name\n"
1470         "    - [forcibly] delete environment variable 'name'",
1471         var_complete
1472 );
1473
1474 #if defined(CONFIG_CMD_ASKENV)
1475
1476 U_BOOT_CMD(
1477         askenv, CONFIG_SYS_MAXARGS,     1,      do_env_ask,
1478         "get environment variables from stdin",
1479         "name [message] [size]\n"
1480         "    - get environment variable 'name' from stdin (max 'size' chars)"
1481 );
1482 #endif
1483
1484 #if defined(CONFIG_CMD_RUN)
1485 U_BOOT_CMD_COMPLETE(
1486         run,    CONFIG_SYS_MAXARGS,     1,      do_run,
1487         "run commands in an environment variable",
1488         "var [...]\n"
1489         "    - run the commands in the environment variable(s) 'var'",
1490         var_complete
1491 );
1492 #endif
1493 #endif /* CONFIG_SPL_BUILD */