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