751b77d0e986ae918438b1e5c33f54c8ae75ef34
[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 = env_match((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 int env_match(uchar *s1, int i2)
789 {
790         if (s1 == NULL)
791                 return -1;
792
793         while (*s1 == env_get_char(i2++))
794                 if (*s1++ == '=')
795                         return i2;
796
797         if (*s1 == '\0' && env_get_char(i2-1) == '=')
798                 return i2;
799
800         return -1;
801 }
802
803 #ifndef CONFIG_SPL_BUILD
804 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
805                           int argc, char * const argv[])
806 {
807         int all = 0, env_flag = H_INTERACTIVE;
808
809         debug("Initial value for argc=%d\n", argc);
810         while (--argc > 0 && **++argv == '-') {
811                 char *arg = *argv;
812
813                 while (*++arg) {
814                         switch (*arg) {
815                         case 'a':               /* default all */
816                                 all = 1;
817                                 break;
818                         case 'f':               /* force */
819                                 env_flag |= H_FORCE;
820                                 break;
821                         default:
822                                 return cmd_usage(cmdtp);
823                         }
824                 }
825         }
826         debug("Final value for argc=%d\n", argc);
827         if (all && (argc == 0)) {
828                 /* Reset the whole environment */
829                 set_default_env("## Resetting to default environment\n",
830                                 env_flag);
831                 return 0;
832         }
833         if (!all && (argc > 0)) {
834                 /* Reset individual variables */
835                 set_default_vars(argc, argv, env_flag);
836                 return 0;
837         }
838
839         return cmd_usage(cmdtp);
840 }
841
842 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
843                          int argc, char * const argv[])
844 {
845         int env_flag = H_INTERACTIVE;
846         int ret = 0;
847
848         debug("Initial value for argc=%d\n", argc);
849         while (argc > 1 && **(argv + 1) == '-') {
850                 char *arg = *++argv;
851
852                 --argc;
853                 while (*++arg) {
854                         switch (*arg) {
855                         case 'f':               /* force */
856                                 env_flag |= H_FORCE;
857                                 break;
858                         default:
859                                 return CMD_RET_USAGE;
860                         }
861                 }
862         }
863         debug("Final value for argc=%d\n", argc);
864
865         env_id++;
866
867         while (--argc > 0) {
868                 char *name = *++argv;
869
870                 if (!hdelete_r(name, &env_htab, env_flag))
871                         ret = 1;
872         }
873
874         return ret;
875 }
876
877 #ifdef CONFIG_CMD_EXPORTENV
878 /*
879  * env export [-t | -b | -c] [-s size] addr [var ...]
880  *      -t:     export as text format; if size is given, data will be
881  *              padded with '\0' bytes; if not, one terminating '\0'
882  *              will be added (which is included in the "filesize"
883  *              setting so you can for exmple copy this to flash and
884  *              keep the termination).
885  *      -b:     export as binary format (name=value pairs separated by
886  *              '\0', list end marked by double "\0\0")
887  *      -c:     export as checksum protected environment format as
888  *              used for example by "saveenv" command
889  *      -s size:
890  *              size of output buffer
891  *      addr:   memory address where environment gets stored
892  *      var...  List of variable names that get included into the
893  *              export. Without arguments, the whole environment gets
894  *              exported.
895  *
896  * With "-c" and size is NOT given, then the export command will
897  * format the data as currently used for the persistent storage,
898  * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
899  * prepend a valid CRC32 checksum and, in case of redundant
900  * environment, a "current" redundancy flag. If size is given, this
901  * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
902  * checksum and redundancy flag will be inserted.
903  *
904  * With "-b" and "-t", always only the real data (including a
905  * terminating '\0' byte) will be written; here the optional size
906  * argument will be used to make sure not to overflow the user
907  * provided buffer; the command will abort if the size is not
908  * sufficient. Any remaining space will be '\0' padded.
909  *
910  * On successful return, the variable "filesize" will be set.
911  * Note that filesize includes the trailing/terminating '\0' byte(s).
912  *
913  * Usage scenario:  create a text snapshot/backup of the current settings:
914  *
915  *      => env export -t 100000
916  *      => era ${backup_addr} +${filesize}
917  *      => cp.b 100000 ${backup_addr} ${filesize}
918  *
919  * Re-import this snapshot, deleting all other settings:
920  *
921  *      => env import -d -t ${backup_addr}
922  */
923 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
924                          int argc, char * const argv[])
925 {
926         char    buf[32];
927         ulong   addr;
928         char    *ptr, *cmd, *res;
929         size_t  size = 0;
930         ssize_t len;
931         env_t   *envp;
932         char    sep = '\n';
933         int     chk = 0;
934         int     fmt = 0;
935
936         cmd = *argv;
937
938         while (--argc > 0 && **++argv == '-') {
939                 char *arg = *argv;
940                 while (*++arg) {
941                         switch (*arg) {
942                         case 'b':               /* raw binary format */
943                                 if (fmt++)
944                                         goto sep_err;
945                                 sep = '\0';
946                                 break;
947                         case 'c':               /* external checksum format */
948                                 if (fmt++)
949                                         goto sep_err;
950                                 sep = '\0';
951                                 chk = 1;
952                                 break;
953                         case 's':               /* size given */
954                                 if (--argc <= 0)
955                                         return cmd_usage(cmdtp);
956                                 size = simple_strtoul(*++argv, NULL, 16);
957                                 goto NXTARG;
958                         case 't':               /* text format */
959                                 if (fmt++)
960                                         goto sep_err;
961                                 sep = '\n';
962                                 break;
963                         default:
964                                 return CMD_RET_USAGE;
965                         }
966                 }
967 NXTARG:         ;
968         }
969
970         if (argc < 1)
971                 return CMD_RET_USAGE;
972
973         addr = simple_strtoul(argv[0], NULL, 16);
974         ptr = map_sysmem(addr, size);
975
976         if (size)
977                 memset(ptr, '\0', size);
978
979         argc--;
980         argv++;
981
982         if (sep) {              /* export as text file */
983                 len = hexport_r(&env_htab, sep,
984                                 H_MATCH_KEY | H_MATCH_IDENT,
985                                 &ptr, size, argc, argv);
986                 if (len < 0) {
987                         pr_err("## Error: Cannot export environment: errno = %d\n",
988                                errno);
989                         return 1;
990                 }
991                 sprintf(buf, "%zX", (size_t)len);
992                 env_set("filesize", buf);
993
994                 return 0;
995         }
996
997         envp = (env_t *)ptr;
998
999         if (chk)                /* export as checksum protected block */
1000                 res = (char *)envp->data;
1001         else                    /* export as raw binary data */
1002                 res = ptr;
1003
1004         len = hexport_r(&env_htab, '\0',
1005                         H_MATCH_KEY | H_MATCH_IDENT,
1006                         &res, ENV_SIZE, argc, argv);
1007         if (len < 0) {
1008                 pr_err("## Error: Cannot export environment: errno = %d\n",
1009                        errno);
1010                 return 1;
1011         }
1012
1013         if (chk) {
1014                 envp->crc = crc32(0, envp->data,
1015                                 size ? size - offsetof(env_t, data) : ENV_SIZE);
1016 #ifdef CONFIG_ENV_ADDR_REDUND
1017                 envp->flags = ACTIVE_FLAG;
1018 #endif
1019         }
1020         env_set_hex("filesize", len + offsetof(env_t, data));
1021
1022         return 0;
1023
1024 sep_err:
1025         printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1026                cmd);
1027         return 1;
1028 }
1029 #endif
1030
1031 #ifdef CONFIG_CMD_IMPORTENV
1032 /*
1033  * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1034  *      -d:     delete existing environment before importing if no var is
1035  *              passed; if vars are passed, if one var is in the current
1036  *              environment but not in the environment at addr, delete var from
1037  *              current environment;
1038  *              otherwise overwrite / append to existing definitions
1039  *      -t:     assume text format; either "size" must be given or the
1040  *              text data must be '\0' terminated
1041  *      -r:     handle CRLF like LF, that means exported variables with
1042  *              a content which ends with \r won't get imported. Used
1043  *              to import text files created with editors which are using CRLF
1044  *              for line endings. Only effective in addition to -t.
1045  *      -b:     assume binary format ('\0' separated, "\0\0" terminated)
1046  *      -c:     assume checksum protected environment format
1047  *      addr:   memory address to read from
1048  *      size:   length of input data; if missing, proper '\0'
1049  *              termination is mandatory
1050  *              if var is set and size should be missing (i.e. '\0'
1051  *              termination), set size to '-'
1052  *      var...  List of the names of the only variables that get imported from
1053  *              the environment at address 'addr'. Without arguments, the whole
1054  *              environment gets imported.
1055  */
1056 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1057                          int argc, char * const argv[])
1058 {
1059         ulong   addr;
1060         char    *cmd, *ptr;
1061         char    sep = '\n';
1062         int     chk = 0;
1063         int     fmt = 0;
1064         int     del = 0;
1065         int     crlf_is_lf = 0;
1066         int     wl = 0;
1067         size_t  size;
1068
1069         cmd = *argv;
1070
1071         while (--argc > 0 && **++argv == '-') {
1072                 char *arg = *argv;
1073                 while (*++arg) {
1074                         switch (*arg) {
1075                         case 'b':               /* raw binary format */
1076                                 if (fmt++)
1077                                         goto sep_err;
1078                                 sep = '\0';
1079                                 break;
1080                         case 'c':               /* external checksum format */
1081                                 if (fmt++)
1082                                         goto sep_err;
1083                                 sep = '\0';
1084                                 chk = 1;
1085                                 break;
1086                         case 't':               /* text format */
1087                                 if (fmt++)
1088                                         goto sep_err;
1089                                 sep = '\n';
1090                                 break;
1091                         case 'r':               /* handle CRLF like LF */
1092                                 crlf_is_lf = 1;
1093                                 break;
1094                         case 'd':
1095                                 del = 1;
1096                                 break;
1097                         default:
1098                                 return CMD_RET_USAGE;
1099                         }
1100                 }
1101         }
1102
1103         if (argc < 1)
1104                 return CMD_RET_USAGE;
1105
1106         if (!fmt)
1107                 printf("## Warning: defaulting to text format\n");
1108
1109         if (sep != '\n' && crlf_is_lf )
1110                 crlf_is_lf = 0;
1111
1112         addr = simple_strtoul(argv[0], NULL, 16);
1113         ptr = map_sysmem(addr, 0);
1114
1115         if (argc >= 2 && strcmp(argv[1], "-")) {
1116                 size = simple_strtoul(argv[1], NULL, 16);
1117         } else if (chk) {
1118                 puts("## Error: external checksum format must pass size\n");
1119                 return CMD_RET_FAILURE;
1120         } else {
1121                 char *s = ptr;
1122
1123                 size = 0;
1124
1125                 while (size < MAX_ENV_SIZE) {
1126                         if ((*s == sep) && (*(s+1) == '\0'))
1127                                 break;
1128                         ++s;
1129                         ++size;
1130                 }
1131                 if (size == MAX_ENV_SIZE) {
1132                         printf("## Warning: Input data exceeds %d bytes"
1133                                 " - truncated\n", MAX_ENV_SIZE);
1134                 }
1135                 size += 2;
1136                 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1137         }
1138
1139         if (argc > 2)
1140                 wl = 1;
1141
1142         if (chk) {
1143                 uint32_t crc;
1144                 env_t *ep = (env_t *)ptr;
1145
1146                 size -= offsetof(env_t, data);
1147                 memcpy(&crc, &ep->crc, sizeof(crc));
1148
1149                 if (crc32(0, ep->data, size) != crc) {
1150                         puts("## Error: bad CRC, import failed\n");
1151                         return 1;
1152                 }
1153                 ptr = (char *)ep->data;
1154         }
1155
1156         if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1157                        crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1158                 pr_err("## Error: Environment import failed: errno = %d\n",
1159                        errno);
1160                 return 1;
1161         }
1162         gd->flags |= GD_FLG_ENV_READY;
1163
1164         return 0;
1165
1166 sep_err:
1167         printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1168                 cmd);
1169         return 1;
1170 }
1171 #endif
1172
1173 #if defined(CONFIG_CMD_NVEDIT_INFO)
1174 /*
1175  * print_env_info - print environment information
1176  */
1177 static int print_env_info(void)
1178 {
1179         const char *value;
1180
1181         /* print environment validity value */
1182         switch (gd->env_valid) {
1183         case ENV_INVALID:
1184                 value = "invalid";
1185                 break;
1186         case ENV_VALID:
1187                 value = "valid";
1188                 break;
1189         case ENV_REDUND:
1190                 value = "redundant";
1191                 break;
1192         default:
1193                 value = "unknown";
1194                 break;
1195         }
1196         printf("env_valid = %s\n", value);
1197
1198         /* print environment ready flag */
1199         value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1200         printf("env_ready = %s\n", value);
1201
1202         /* print environment using default flag */
1203         value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1204         printf("env_use_default = %s\n", value);
1205
1206         return CMD_RET_SUCCESS;
1207 }
1208
1209 #define ENV_INFO_IS_DEFAULT     BIT(0) /* default environment bit mask */
1210 #define ENV_INFO_IS_PERSISTED   BIT(1) /* environment persistence bit mask */
1211
1212 /*
1213  * env info - display environment information
1214  * env info [-d] - evaluate whether default environment is used
1215  * env info [-p] - evaluate whether environment can be persisted
1216  */
1217 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1218                        int argc, char * const argv[])
1219 {
1220         int eval_flags = 0;
1221         int eval_results = 0;
1222
1223         /* display environment information */
1224         if (argc <= 1)
1225                 return print_env_info();
1226
1227         /* process options */
1228         while (--argc > 0 && **++argv == '-') {
1229                 char *arg = *argv;
1230
1231                 while (*++arg) {
1232                         switch (*arg) {
1233                         case 'd':
1234                                 eval_flags |= ENV_INFO_IS_DEFAULT;
1235                                 break;
1236                         case 'p':
1237                                 eval_flags |= ENV_INFO_IS_PERSISTED;
1238                                 break;
1239                         default:
1240                                 return CMD_RET_USAGE;
1241                         }
1242                 }
1243         }
1244
1245         /* evaluate whether default environment is used */
1246         if (eval_flags & ENV_INFO_IS_DEFAULT) {
1247                 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1248                         printf("Default environment is used\n");
1249                         eval_results |= ENV_INFO_IS_DEFAULT;
1250                 } else {
1251                         printf("Environment was loaded from persistent storage\n");
1252                 }
1253         }
1254
1255         /* evaluate whether environment can be persisted */
1256         if (eval_flags & ENV_INFO_IS_PERSISTED) {
1257 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1258                 printf("Environment can be persisted\n");
1259                 eval_results |= ENV_INFO_IS_PERSISTED;
1260 #else
1261                 printf("Environment cannot be persisted\n");
1262 #endif
1263         }
1264
1265         /* The result of evaluations is combined with AND */
1266         if (eval_flags != eval_results)
1267                 return CMD_RET_FAILURE;
1268
1269         return CMD_RET_SUCCESS;
1270 }
1271 #endif
1272
1273 #if defined(CONFIG_CMD_ENV_EXISTS)
1274 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1275                        char * const argv[])
1276 {
1277         ENTRY e, *ep;
1278
1279         if (argc < 2)
1280                 return CMD_RET_USAGE;
1281
1282         e.key = argv[1];
1283         e.data = NULL;
1284         hsearch_r(e, FIND, &ep, &env_htab, 0);
1285
1286         return (ep == NULL) ? 1 : 0;
1287 }
1288 #endif
1289
1290 /*
1291  * New command line interface: "env" command with subcommands
1292  */
1293 static cmd_tbl_t cmd_env_sub[] = {
1294 #if defined(CONFIG_CMD_ASKENV)
1295         U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1296 #endif
1297         U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1298         U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1299 #if defined(CONFIG_CMD_EDITENV)
1300         U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1301 #endif
1302 #if defined(CONFIG_CMD_ENV_CALLBACK)
1303         U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1304 #endif
1305 #if defined(CONFIG_CMD_ENV_FLAGS)
1306         U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1307 #endif
1308 #if defined(CONFIG_CMD_EXPORTENV)
1309         U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1310 #endif
1311 #if defined(CONFIG_CMD_GREPENV)
1312         U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1313 #endif
1314 #if defined(CONFIG_CMD_IMPORTENV)
1315         U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1316 #endif
1317 #if defined(CONFIG_CMD_NVEDIT_INFO)
1318         U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1319 #endif
1320         U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1321 #if defined(CONFIG_CMD_RUN)
1322         U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1323 #endif
1324 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1325         U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1326 #if defined(CONFIG_CMD_ERASEENV)
1327         U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1328 #endif
1329 #endif
1330         U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1331 #if defined(CONFIG_CMD_ENV_EXISTS)
1332         U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1333 #endif
1334 };
1335
1336 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1337 void env_reloc(void)
1338 {
1339         fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1340 }
1341 #endif
1342
1343 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1344 {
1345         cmd_tbl_t *cp;
1346
1347         if (argc < 2)
1348                 return CMD_RET_USAGE;
1349
1350         /* drop initial "env" arg */
1351         argc--;
1352         argv++;
1353
1354         cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1355
1356         if (cp)
1357                 return cp->cmd(cmdtp, flag, argc, argv);
1358
1359         return CMD_RET_USAGE;
1360 }
1361
1362 #ifdef CONFIG_SYS_LONGHELP
1363 static char env_help_text[] =
1364 #if defined(CONFIG_CMD_ASKENV)
1365         "ask name [message] [size] - ask for environment variable\nenv "
1366 #endif
1367 #if defined(CONFIG_CMD_ENV_CALLBACK)
1368         "callbacks - print callbacks and their associated variables\nenv "
1369 #endif
1370         "default [-f] -a - [forcibly] reset default environment\n"
1371         "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1372         "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1373 #if defined(CONFIG_CMD_EDITENV)
1374         "env edit name - edit environment variable\n"
1375 #endif
1376 #if defined(CONFIG_CMD_ENV_EXISTS)
1377         "env exists name - tests for existence of variable\n"
1378 #endif
1379 #if defined(CONFIG_CMD_EXPORTENV)
1380         "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1381 #endif
1382 #if defined(CONFIG_CMD_ENV_FLAGS)
1383         "env flags - print variables that have non-default flags\n"
1384 #endif
1385 #if defined(CONFIG_CMD_GREPENV)
1386 #ifdef CONFIG_REGEX
1387         "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1388 #else
1389         "env grep [-n | -v | -b] string [...] - search environment\n"
1390 #endif
1391 #endif
1392 #if defined(CONFIG_CMD_IMPORTENV)
1393         "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1394 #endif
1395 #if defined(CONFIG_CMD_NVEDIT_INFO)
1396         "env info - display environment information\n"
1397         "env info [-d] - whether default environment is used\n"
1398         "env info [-p] - whether environment can be persisted\n"
1399 #endif
1400         "env print [-a | name ...] - print environment\n"
1401 #if defined(CONFIG_CMD_NVEDIT_EFI)
1402         "env print -e [name ...] - print UEFI environment\n"
1403 #endif
1404 #if defined(CONFIG_CMD_RUN)
1405         "env run var [...] - run commands in an environment variable\n"
1406 #endif
1407 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1408         "env save - save environment\n"
1409 #if defined(CONFIG_CMD_ERASEENV)
1410         "env erase - erase environment\n"
1411 #endif
1412 #endif
1413 #if defined(CONFIG_CMD_NVEDIT_EFI)
1414         "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1415 #endif
1416         "env set [-f] name [arg ...]\n";
1417 #endif
1418
1419 U_BOOT_CMD(
1420         env, CONFIG_SYS_MAXARGS, 1, do_env,
1421         "environment handling commands", env_help_text
1422 );
1423
1424 /*
1425  * Old command line interface, kept for compatibility
1426  */
1427
1428 #if defined(CONFIG_CMD_EDITENV)
1429 U_BOOT_CMD_COMPLETE(
1430         editenv, 2, 0,  do_env_edit,
1431         "edit environment variable",
1432         "name\n"
1433         "    - edit environment variable 'name'",
1434         var_complete
1435 );
1436 #endif
1437
1438 U_BOOT_CMD_COMPLETE(
1439         printenv, CONFIG_SYS_MAXARGS, 1,        do_env_print,
1440         "print environment variables",
1441         "[-a]\n    - print [all] values of all environment variables\n"
1442 #if defined(CONFIG_CMD_NVEDIT_EFI)
1443         "printenv -e [name ...]\n"
1444         "    - print UEFI variable 'name' or all the variables\n"
1445 #endif
1446         "printenv name ...\n"
1447         "    - print value of environment variable 'name'",
1448         var_complete
1449 );
1450
1451 #ifdef CONFIG_CMD_GREPENV
1452 U_BOOT_CMD_COMPLETE(
1453         grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
1454         "search environment variables",
1455 #ifdef CONFIG_REGEX
1456         "[-e] [-n | -v | -b] string ...\n"
1457 #else
1458         "[-n | -v | -b] string ...\n"
1459 #endif
1460         "    - list environment name=value pairs matching 'string'\n"
1461 #ifdef CONFIG_REGEX
1462         "      \"-e\": enable regular expressions;\n"
1463 #endif
1464         "      \"-n\": search variable names; \"-v\": search values;\n"
1465         "      \"-b\": search both names and values (default)",
1466         var_complete
1467 );
1468 #endif
1469
1470 U_BOOT_CMD_COMPLETE(
1471         setenv, CONFIG_SYS_MAXARGS, 0,  do_env_set,
1472         "set environment variables",
1473 #if defined(CONFIG_CMD_NVEDIT_EFI)
1474         "-e [-nv] name [value ...]\n"
1475         "    - set UEFI variable 'name' to 'value' ...'\n"
1476         "      'nv' option makes the variable non-volatile\n"
1477         "    - delete UEFI variable 'name' if 'value' not specified\n"
1478 #endif
1479         "setenv [-f] name value ...\n"
1480         "    - [forcibly] set environment variable 'name' to 'value ...'\n"
1481         "setenv [-f] name\n"
1482         "    - [forcibly] delete environment variable 'name'",
1483         var_complete
1484 );
1485
1486 #if defined(CONFIG_CMD_ASKENV)
1487
1488 U_BOOT_CMD(
1489         askenv, CONFIG_SYS_MAXARGS,     1,      do_env_ask,
1490         "get environment variables from stdin",
1491         "name [message] [size]\n"
1492         "    - get environment variable 'name' from stdin (max 'size' chars)"
1493 );
1494 #endif
1495
1496 #if defined(CONFIG_CMD_RUN)
1497 U_BOOT_CMD_COMPLETE(
1498         run,    CONFIG_SYS_MAXARGS,     1,      do_run,
1499         "run commands in an environment variable",
1500         "var [...]\n"
1501         "    - run the commands in the environment variable(s) 'var'",
1502         var_complete
1503 );
1504 #endif
1505 #endif /* CONFIG_SPL_BUILD */