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