1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2013
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
9 * Copyright 2011 Freescale Semiconductor, Inc.
13 * Support for persistent environment data
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
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.
31 #include <env_internal.h>
37 #include <asm/global_data.h>
38 #include <linux/bitops.h>
39 #include <u-boot/crc.h>
40 #include <linux/stddef.h>
41 #include <asm/byteorder.h>
44 DECLARE_GLOBAL_DATA_PTR;
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)
59 #define ENV_IS_IN_DEVICE
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
70 * Maximum expected input data size for import command
72 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
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()
81 static int env_id = 1;
88 #ifndef CONFIG_SPL_BUILD
90 * Command interface: print one or all environment variables
92 * Returns 0 in case of error, or length of printed string
94 static int env_print(char *name, int flag)
99 if (name) { /* print a single name */
100 struct env_entry e, *ep;
104 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
107 len = printf("%s=%s\n", ep->key, ep->data);
111 /* print whole list */
112 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
120 /* should never happen */
121 printf("## Error: cannot export environment\n");
125 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
130 int env_flag = H_HIDE_DOT;
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);
137 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
140 env_flag &= ~H_HIDE_DOT;
144 /* print all env vars */
145 rcode = env_print(NULL, env_flag);
148 printf("\nEnvironment size: %d/%ld bytes\n",
149 rcode, (ulong)ENV_SIZE);
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);
158 printf("## Error: \"%s\" not defined\n", argv[i]);
166 #ifdef CONFIG_CMD_GREPENV
167 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
168 int argc, char *const argv[])
171 int len, grep_how, grep_what;
174 return CMD_RET_USAGE;
176 grep_how = H_MATCH_SUBSTR; /* default: substring search */
177 grep_what = H_MATCH_BOTH; /* default: grep names and values */
179 while (--argc > 0 && **++argv == '-') {
184 case 'e': /* use regex matching */
185 grep_how = H_MATCH_REGEX;
188 case 'n': /* grep for name */
189 grep_what = H_MATCH_KEY;
191 case 'v': /* grep for value */
192 grep_what = H_MATCH_DATA;
194 case 'b': /* grep for both */
195 grep_what = H_MATCH_BOTH;
200 return CMD_RET_USAGE;
206 len = hexport_r(&env_htab, '\n',
207 flag | grep_what | grep_how,
208 &res, 0, argc, argv);
221 #endif /* CONFIG_SPL_BUILD */
224 * Set a new environment variable,
225 * or replace or delete an existing one.
227 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
230 char *name, *value, *s;
231 struct env_entry e, *ep;
233 debug("Initial value for argc=%d\n", argc);
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);
240 while (argc > 1 && **(argv + 1) == '-') {
246 case 'f': /* force */
250 return CMD_RET_USAGE;
254 debug("Final value for argc=%d\n", argc);
257 if (strchr(name, '=')) {
258 printf("## Error: illegal character '='"
259 "in variable name \"%s\"\n", name);
266 if (argc < 3 || argv[2] == NULL) {
267 int rc = hdelete_r(name, &env_htab, env_flag);
269 /* If the variable didn't exist, don't report an error */
270 return rc && rc != -ENOENT ? 1 : 0;
274 * Insert / replace new value
276 for (i = 2, len = 0; i < argc; ++i)
277 len += strlen(argv[i]) + 1;
281 printf("## Can't malloc %d bytes\n", len);
284 for (i = 2, s = value; i < argc; ++i) {
287 while ((*s++ = *v++) != '\0')
296 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
299 printf("## Error inserting \"%s\" variable, errno=%d\n",
307 int env_set(const char *varname, const char *varvalue)
309 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
311 /* before import into hashtable */
312 if (!(gd->flags & GD_FLG_ENV_READY))
315 if (varvalue == NULL || varvalue[0] == '\0')
316 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
318 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
321 #ifndef CONFIG_SPL_BUILD
322 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
326 return CMD_RET_USAGE;
328 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
332 * Prompt for environment variable
334 #if defined(CONFIG_CMD_ASKENV)
335 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
337 char message[CONFIG_SYS_CBSIZE];
338 int i, len, pos, size;
342 local_args[0] = argv[0];
343 local_args[1] = argv[1];
344 local_args[2] = NULL;
345 local_args[3] = NULL;
350 * env_ask envname [message1 ...] [size]
353 return CMD_RET_USAGE;
356 * We test the last argument if it can be converted
357 * into a decimal number. If yes, we assume it's
358 * the size. Otherwise we echo it as part of the
361 i = dectoul(argv[argc - 1], &endptr);
362 if (*endptr != '\0') { /* no size */
363 size = CONFIG_SYS_CBSIZE - 1;
364 } else { /* size given */
370 sprintf(message, "Please enter '%s': ", argv[1]);
372 /* env_ask envname message1 ... messagen [size] */
373 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
375 message[pos++] = ' ';
377 strncpy(message + pos, argv[i], sizeof(message) - pos);
378 pos += strlen(argv[i]);
380 if (pos < sizeof(message) - 1) {
381 message[pos++] = ' ';
384 message[CONFIG_SYS_CBSIZE - 1] = '\0';
387 if (size >= CONFIG_SYS_CBSIZE)
388 size = CONFIG_SYS_CBSIZE - 1;
393 /* prompt for input */
394 len = cli_readline(message);
397 console_buffer[size] = '\0';
400 if (console_buffer[0] != '\0') {
401 local_args[2] = console_buffer;
405 /* Continue calling setenv code */
406 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
410 #if defined(CONFIG_CMD_ENV_CALLBACK)
411 static int print_static_binding(const char *var_name, const char *callback_name,
414 printf("\t%-20s %-20s\n", var_name, callback_name);
419 static int print_active_callback(struct env_entry *entry)
421 struct env_clbk_tbl *clbkp;
425 if (entry->callback == NULL)
428 /* look up the callback in the linker-list */
429 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
430 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
433 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
434 if (entry->callback == clbkp->callback + gd->reloc_off)
436 if (entry->callback == clbkp->callback)
441 if (i == num_callbacks)
442 /* this should probably never happen, but just in case... */
443 printf("\t%-20s %p\n", entry->key, entry->callback);
445 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
451 * Print the callbacks available and what they are bound to
453 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
456 struct env_clbk_tbl *clbkp;
460 /* Print the available callbacks */
461 puts("Available callbacks:\n");
462 puts("\tCallback Name\n");
463 puts("\t-------------\n");
464 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
465 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
468 printf("\t%s\n", clbkp->name);
471 /* Print the static bindings that may exist */
472 puts("Static callback bindings:\n");
473 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
474 printf("\t%-20s %-20s\n", "-------------", "-------------");
475 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
478 /* walk through each variable and print the callback if it has one */
479 puts("Active callback bindings:\n");
480 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
481 printf("\t%-20s %-20s\n", "-------------", "-------------");
482 hwalk_r(&env_htab, print_active_callback);
487 #if defined(CONFIG_CMD_ENV_FLAGS)
488 static int print_static_flags(const char *var_name, const char *flags,
491 enum env_flags_vartype type = env_flags_parse_vartype(flags);
492 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
494 printf("\t%-20s %-20s %-20s\n", var_name,
495 env_flags_get_vartype_name(type),
496 env_flags_get_varaccess_name(access));
501 static int print_active_flags(struct env_entry *entry)
503 enum env_flags_vartype type;
504 enum env_flags_varaccess access;
506 if (entry->flags == 0)
509 type = (enum env_flags_vartype)
510 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
511 access = env_flags_parse_varaccess_from_binflags(entry->flags);
512 printf("\t%-20s %-20s %-20s\n", entry->key,
513 env_flags_get_vartype_name(type),
514 env_flags_get_varaccess_name(access));
520 * Print the flags available and what variables have flags
522 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
524 /* Print the available variable types */
525 printf("Available variable type flags (position %d):\n",
526 ENV_FLAGS_VARTYPE_LOC);
527 puts("\tFlag\tVariable Type Name\n");
528 puts("\t----\t------------------\n");
529 env_flags_print_vartypes();
532 /* Print the available variable access types */
533 printf("Available variable access flags (position %d):\n",
534 ENV_FLAGS_VARACCESS_LOC);
535 puts("\tFlag\tVariable Access Name\n");
536 puts("\t----\t--------------------\n");
537 env_flags_print_varaccess();
540 /* Print the static flags that may exist */
541 puts("Static flags:\n");
542 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
544 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
546 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
549 /* walk through each variable and print the flags if non-default */
550 puts("Active flags:\n");
551 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
553 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
555 hwalk_r(&env_htab, print_active_flags);
561 * Interactively edit an environment variable
563 #if defined(CONFIG_CMD_EDITENV)
564 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
567 char buffer[CONFIG_SYS_CBSIZE];
571 return CMD_RET_USAGE;
573 /* before import into hashtable */
574 if (!(gd->flags & GD_FLG_ENV_READY))
577 /* Set read buffer to initial value or empty sting */
578 init_val = env_get(argv[1]);
580 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
584 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
587 if (buffer[0] == '\0') {
588 const char * const _argv[3] = { "setenv", argv[1], NULL };
590 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
592 const char * const _argv[4] = { "setenv", argv[1], buffer,
595 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
598 #endif /* CONFIG_CMD_EDITENV */
600 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
601 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
604 return env_save() ? 1 : 0;
608 saveenv, 1, 0, do_env_save,
609 "save environment variables to persistent storage",
613 #if defined(CONFIG_CMD_ERASEENV)
614 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
617 return env_erase() ? 1 : 0;
621 eraseenv, 1, 0, do_env_erase,
622 "erase environment variables from persistent storage",
628 #if defined(CONFIG_CMD_NVEDIT_LOAD)
629 static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
632 return env_reload() ? 1 : 0;
636 #if defined(CONFIG_CMD_NVEDIT_SELECT)
637 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
640 return env_select(argv[1]) ? 1 : 0;
644 #endif /* CONFIG_SPL_BUILD */
646 #ifndef CONFIG_SPL_BUILD
647 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
648 int argc, char *const argv[])
650 int all = 0, env_flag = H_INTERACTIVE;
652 debug("Initial value for argc=%d\n", argc);
653 while (--argc > 0 && **++argv == '-') {
658 case 'a': /* default all */
661 case 'f': /* force */
665 return cmd_usage(cmdtp);
669 debug("Final value for argc=%d\n", argc);
670 if (all && (argc == 0)) {
671 /* Reset the whole environment */
672 env_set_default("## Resetting to default environment\n",
676 if (!all && (argc > 0)) {
677 /* Reset individual variables */
678 env_set_default_vars(argc, argv, env_flag);
682 return cmd_usage(cmdtp);
685 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
686 int argc, char *const argv[])
688 int env_flag = H_INTERACTIVE;
691 debug("Initial value for argc=%d\n", argc);
692 while (argc > 1 && **(argv + 1) == '-') {
698 case 'f': /* force */
702 return CMD_RET_USAGE;
706 debug("Final value for argc=%d\n", argc);
711 char *name = *++argv;
713 if (hdelete_r(name, &env_htab, env_flag))
720 #ifdef CONFIG_CMD_EXPORTENV
722 * env export [-t | -b | -c] [-s size] addr [var ...]
723 * -t: export as text format; if size is given, data will be
724 * padded with '\0' bytes; if not, one terminating '\0'
725 * will be added (which is included in the "filesize"
726 * setting so you can for exmple copy this to flash and
727 * keep the termination).
728 * -b: export as binary format (name=value pairs separated by
729 * '\0', list end marked by double "\0\0")
730 * -c: export as checksum protected environment format as
731 * used for example by "saveenv" command
733 * size of output buffer
734 * addr: memory address where environment gets stored
735 * var... List of variable names that get included into the
736 * export. Without arguments, the whole environment gets
739 * With "-c" and size is NOT given, then the export command will
740 * format the data as currently used for the persistent storage,
741 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
742 * prepend a valid CRC32 checksum and, in case of redundant
743 * environment, a "current" redundancy flag. If size is given, this
744 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
745 * checksum and redundancy flag will be inserted.
747 * With "-b" and "-t", always only the real data (including a
748 * terminating '\0' byte) will be written; here the optional size
749 * argument will be used to make sure not to overflow the user
750 * provided buffer; the command will abort if the size is not
751 * sufficient. Any remaining space will be '\0' padded.
753 * On successful return, the variable "filesize" will be set.
754 * Note that filesize includes the trailing/terminating '\0' byte(s).
756 * Usage scenario: create a text snapshot/backup of the current settings:
758 * => env export -t 100000
759 * => era ${backup_addr} +${filesize}
760 * => cp.b 100000 ${backup_addr} ${filesize}
762 * Re-import this snapshot, deleting all other settings:
764 * => env import -d -t ${backup_addr}
766 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
767 int argc, char *const argv[])
771 char *ptr, *cmd, *res;
781 while (--argc > 0 && **++argv == '-') {
785 case 'b': /* raw binary format */
790 case 'c': /* external checksum format */
796 case 's': /* size given */
798 return cmd_usage(cmdtp);
799 size = hextoul(*++argv, NULL);
801 case 't': /* text format */
807 return CMD_RET_USAGE;
814 return CMD_RET_USAGE;
816 addr = hextoul(argv[0], NULL);
817 ptr = map_sysmem(addr, size);
820 memset(ptr, '\0', size);
825 if (sep) { /* export as text file */
826 len = hexport_r(&env_htab, sep,
827 H_MATCH_KEY | H_MATCH_IDENT,
828 &ptr, size, argc, argv);
830 pr_err("## Error: Cannot export environment: errno = %d\n",
834 sprintf(buf, "%zX", (size_t)len);
835 env_set("filesize", buf);
842 if (chk) /* export as checksum protected block */
843 res = (char *)envp->data;
844 else /* export as raw binary data */
847 len = hexport_r(&env_htab, '\0',
848 H_MATCH_KEY | H_MATCH_IDENT,
849 &res, ENV_SIZE, argc, argv);
851 pr_err("## Error: Cannot export environment: errno = %d\n",
857 envp->crc = crc32(0, envp->data,
858 size ? size - offsetof(env_t, data) : ENV_SIZE);
859 #ifdef CONFIG_ENV_ADDR_REDUND
860 envp->flags = ENV_REDUND_ACTIVE;
863 env_set_hex("filesize", len + offsetof(env_t, data));
868 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
874 #ifdef CONFIG_CMD_IMPORTENV
876 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
877 * -d: delete existing environment before importing if no var is
878 * passed; if vars are passed, if one var is in the current
879 * environment but not in the environment at addr, delete var from
880 * current environment;
881 * otherwise overwrite / append to existing definitions
882 * -t: assume text format; either "size" must be given or the
883 * text data must be '\0' terminated
884 * -r: handle CRLF like LF, that means exported variables with
885 * a content which ends with \r won't get imported. Used
886 * to import text files created with editors which are using CRLF
887 * for line endings. Only effective in addition to -t.
888 * -b: assume binary format ('\0' separated, "\0\0" terminated)
889 * -c: assume checksum protected environment format
890 * addr: memory address to read from
891 * size: length of input data; if missing, proper '\0'
892 * termination is mandatory
893 * if var is set and size should be missing (i.e. '\0'
894 * termination), set size to '-'
895 * var... List of the names of the only variables that get imported from
896 * the environment at address 'addr'. Without arguments, the whole
897 * environment gets imported.
899 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
900 int argc, char *const argv[])
914 while (--argc > 0 && **++argv == '-') {
918 case 'b': /* raw binary format */
923 case 'c': /* external checksum format */
929 case 't': /* text format */
934 case 'r': /* handle CRLF like LF */
941 return CMD_RET_USAGE;
947 return CMD_RET_USAGE;
950 printf("## Warning: defaulting to text format\n");
952 if (sep != '\n' && crlf_is_lf )
955 addr = hextoul(argv[0], NULL);
956 ptr = map_sysmem(addr, 0);
958 if (argc >= 2 && strcmp(argv[1], "-")) {
959 size = hextoul(argv[1], NULL);
961 puts("## Error: external checksum format must pass size\n");
962 return CMD_RET_FAILURE;
968 while (size < MAX_ENV_SIZE) {
969 if ((*s == sep) && (*(s+1) == '\0'))
974 if (size == MAX_ENV_SIZE) {
975 printf("## Warning: Input data exceeds %d bytes"
976 " - truncated\n", MAX_ENV_SIZE);
979 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
987 env_t *ep = (env_t *)ptr;
989 if (size <= offsetof(env_t, data)) {
990 printf("## Error: Invalid size 0x%zX\n", size);
994 size -= offsetof(env_t, data);
995 memcpy(&crc, &ep->crc, sizeof(crc));
997 if (crc32(0, ep->data, size) != crc) {
998 puts("## Error: bad CRC, import failed\n");
1001 ptr = (char *)ep->data;
1004 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1005 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1006 pr_err("## Error: Environment import failed: errno = %d\n",
1010 gd->flags |= GD_FLG_ENV_READY;
1015 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1021 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1022 static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
1023 int argc, char *const argv[])
1026 char *from = argv[2];
1027 char *default_value = NULL;
1030 if (argc < 3 || argc > 4) {
1031 return CMD_RET_USAGE;
1035 default_value = argv[3];
1038 if (env_get(from) == NULL && default_value == NULL) {
1039 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1041 return CMD_RET_FAILURE;
1044 if (env_get(from) == NULL) {
1045 ret = env_set(to, default_value);
1048 ret = env_set(to, env_get(from));
1052 return CMD_RET_SUCCESS;
1055 return CMD_RET_FAILURE;
1060 #if defined(CONFIG_CMD_NVEDIT_INFO)
1062 * print_env_info - print environment information
1064 static int print_env_info(void)
1068 /* print environment validity value */
1069 switch (gd->env_valid) {
1077 value = "redundant";
1083 printf("env_valid = %s\n", value);
1085 /* print environment ready flag */
1086 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1087 printf("env_ready = %s\n", value);
1089 /* print environment using default flag */
1090 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1091 printf("env_use_default = %s\n", value);
1093 return CMD_RET_SUCCESS;
1096 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1097 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1100 * env info - display environment information
1101 * env info [-d] - evaluate whether default environment is used
1102 * env info [-p] - evaluate whether environment can be persisted
1103 * Add [-q] - quiet mode, use only for command result, for test by example:
1104 * test env info -p -d -q
1106 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1107 int argc, char *const argv[])
1110 int eval_results = 0;
1112 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1113 enum env_location loc;
1116 /* display environment information */
1118 return print_env_info();
1120 /* process options */
1121 while (--argc > 0 && **++argv == '-') {
1127 eval_flags |= ENV_INFO_IS_DEFAULT;
1130 eval_flags |= ENV_INFO_IS_PERSISTED;
1136 return CMD_RET_USAGE;
1141 /* evaluate whether default environment is used */
1142 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1143 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1145 printf("Default environment is used\n");
1146 eval_results |= ENV_INFO_IS_DEFAULT;
1149 printf("Environment was loaded from persistent storage\n");
1153 /* evaluate whether environment can be persisted */
1154 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1155 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1156 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1157 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1159 printf("Environment can be persisted\n");
1160 eval_results |= ENV_INFO_IS_PERSISTED;
1163 printf("Environment cannot be persisted\n");
1167 printf("Environment cannot be persisted\n");
1171 /* The result of evaluations is combined with AND */
1172 if (eval_flags != eval_results)
1173 return CMD_RET_FAILURE;
1175 return CMD_RET_SUCCESS;
1179 #if defined(CONFIG_CMD_ENV_EXISTS)
1180 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1183 struct env_entry e, *ep;
1186 return CMD_RET_USAGE;
1190 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1192 return (ep == NULL) ? 1 : 0;
1197 * New command line interface: "env" command with subcommands
1199 static struct cmd_tbl cmd_env_sub[] = {
1200 #if defined(CONFIG_CMD_ASKENV)
1201 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1203 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1204 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1205 #if defined(CONFIG_CMD_EDITENV)
1206 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1208 #if defined(CONFIG_CMD_ENV_CALLBACK)
1209 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1211 #if defined(CONFIG_CMD_ENV_FLAGS)
1212 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1214 #if defined(CONFIG_CMD_EXPORTENV)
1215 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1217 #if defined(CONFIG_CMD_GREPENV)
1218 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1220 #if defined(CONFIG_CMD_IMPORTENV)
1221 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1223 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1224 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1226 #if defined(CONFIG_CMD_NVEDIT_INFO)
1227 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
1229 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1230 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1232 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1233 #if defined(CONFIG_CMD_RUN)
1234 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1236 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1237 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1238 #if defined(CONFIG_CMD_ERASEENV)
1239 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1242 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1243 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1245 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1246 #if defined(CONFIG_CMD_ENV_EXISTS)
1247 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1251 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1252 void env_reloc(void)
1254 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1258 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1263 return CMD_RET_USAGE;
1265 /* drop initial "env" arg */
1269 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1272 return cp->cmd(cmdtp, flag, argc, argv);
1274 return CMD_RET_USAGE;
1277 #ifdef CONFIG_SYS_LONGHELP
1278 static char env_help_text[] =
1279 #if defined(CONFIG_CMD_ASKENV)
1280 "ask name [message] [size] - ask for environment variable\nenv "
1282 #if defined(CONFIG_CMD_ENV_CALLBACK)
1283 "callbacks - print callbacks and their associated variables\nenv "
1285 "default [-f] -a - [forcibly] reset default environment\n"
1286 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1287 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1288 #if defined(CONFIG_CMD_EDITENV)
1289 "env edit name - edit environment variable\n"
1291 #if defined(CONFIG_CMD_ENV_EXISTS)
1292 "env exists name - tests for existence of variable\n"
1294 #if defined(CONFIG_CMD_EXPORTENV)
1295 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1297 #if defined(CONFIG_CMD_ENV_FLAGS)
1298 "env flags - print variables that have non-default flags\n"
1300 #if defined(CONFIG_CMD_GREPENV)
1302 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1304 "env grep [-n | -v | -b] string [...] - search environment\n"
1307 #if defined(CONFIG_CMD_IMPORTENV)
1308 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1310 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1311 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1313 #if defined(CONFIG_CMD_NVEDIT_INFO)
1314 "env info - display environment information\n"
1315 "env info [-d] [-p] [-q] - evaluate environment information\n"
1316 " \"-d\": default environment is used\n"
1317 " \"-p\": environment can be persisted\n"
1318 " \"-q\": quiet output\n"
1320 "env print [-a | name ...] - print environment\n"
1321 #if defined(CONFIG_CMD_NVEDIT_EFI)
1322 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
1324 #if defined(CONFIG_CMD_RUN)
1325 "env run var [...] - run commands in an environment variable\n"
1327 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1328 "env save - save environment\n"
1329 #if defined(CONFIG_CMD_ERASEENV)
1330 "env erase - erase environment\n"
1333 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1334 "env load - load environment\n"
1336 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1337 "env select [target] - select environment target\n"
1339 #if defined(CONFIG_CMD_NVEDIT_EFI)
1340 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
1341 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1343 "env set [-f] name [arg ...]\n";
1347 env, CONFIG_SYS_MAXARGS, 1, do_env,
1348 "environment handling commands", env_help_text
1352 * Old command line interface, kept for compatibility
1355 #if defined(CONFIG_CMD_EDITENV)
1356 U_BOOT_CMD_COMPLETE(
1357 editenv, 2, 0, do_env_edit,
1358 "edit environment variable",
1360 " - edit environment variable 'name'",
1365 U_BOOT_CMD_COMPLETE(
1366 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1367 "print environment variables",
1368 "[-a]\n - print [all] values of all environment variables\n"
1369 #if defined(CONFIG_CMD_NVEDIT_EFI)
1370 "printenv -e [-guid guid][-n] [name ...]\n"
1371 " - print UEFI variable 'name' or all the variables\n"
1372 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1373 " \"-n\": suppress dumping variable's value\n"
1375 "printenv name ...\n"
1376 " - print value of environment variable 'name'",
1380 #ifdef CONFIG_CMD_GREPENV
1381 U_BOOT_CMD_COMPLETE(
1382 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1383 "search environment variables",
1385 "[-e] [-n | -v | -b] string ...\n"
1387 "[-n | -v | -b] string ...\n"
1389 " - list environment name=value pairs matching 'string'\n"
1391 " \"-e\": enable regular expressions;\n"
1393 " \"-n\": search variable names; \"-v\": search values;\n"
1394 " \"-b\": search both names and values (default)",
1399 U_BOOT_CMD_COMPLETE(
1400 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1401 "set environment variables",
1402 #if defined(CONFIG_CMD_NVEDIT_EFI)
1403 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1404 " [-i addr:size name], or [name [value ...]]\n"
1405 " - set UEFI variable 'name' to 'value' ...'\n"
1406 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1407 " \"-nv\": set non-volatile attribute\n"
1408 " \"-bs\": set boot-service attribute\n"
1409 " \"-rt\": set runtime attribute\n"
1410 " \"-at\": set time-based authentication attribute\n"
1411 " \"-a\": append-write\n"
1412 " \"-i addr,size\": use <addr,size> as variable's value\n"
1413 " \"-v\": verbose message\n"
1414 " - delete UEFI variable 'name' if 'value' not specified\n"
1416 "setenv [-f] name value ...\n"
1417 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1418 "setenv [-f] name\n"
1419 " - [forcibly] delete environment variable 'name'",
1423 #if defined(CONFIG_CMD_ASKENV)
1426 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1427 "get environment variables from stdin",
1428 "name [message] [size]\n"
1429 " - get environment variable 'name' from stdin (max 'size' chars)"
1433 #if defined(CONFIG_CMD_RUN)
1434 U_BOOT_CMD_COMPLETE(
1435 run, CONFIG_SYS_MAXARGS, 1, do_run,
1436 "run commands in an environment variable",
1438 " - run the commands in the environment variable(s) 'var'",
1442 #endif /* CONFIG_SPL_BUILD */