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_SPI_FLASH) || \
55 defined(CONFIG_ENV_IS_IN_REMOTE) || \
56 defined(CONFIG_ENV_IS_IN_UBI)
58 #define ENV_IS_IN_DEVICE
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
69 * Maximum expected input data size for import command
71 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
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()
80 static int env_id = 1;
87 #ifndef CONFIG_SPL_BUILD
89 * Command interface: print one or all environment variables
91 * Returns 0 in case of error, or length of printed string
93 static int env_print(char *name, int flag)
98 if (name) { /* print a single name */
99 struct env_entry e, *ep;
103 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
106 len = printf("%s=%s\n", ep->key, ep->data);
110 /* print whole list */
111 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
119 /* should never happen */
120 printf("## Error: cannot export environment\n");
124 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
129 int env_flag = H_HIDE_DOT;
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);
136 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
139 env_flag &= ~H_HIDE_DOT;
143 /* print all env vars */
144 rcode = env_print(NULL, env_flag);
147 printf("\nEnvironment size: %d/%ld bytes\n",
148 rcode, (ulong)ENV_SIZE);
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);
157 printf("## Error: \"%s\" not defined\n", argv[i]);
165 #ifdef CONFIG_CMD_GREPENV
166 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
167 int argc, char *const argv[])
170 int len, grep_how, grep_what;
173 return CMD_RET_USAGE;
175 grep_how = H_MATCH_SUBSTR; /* default: substring search */
176 grep_what = H_MATCH_BOTH; /* default: grep names and values */
178 while (--argc > 0 && **++argv == '-') {
183 case 'e': /* use regex matching */
184 grep_how = H_MATCH_REGEX;
187 case 'n': /* grep for name */
188 grep_what = H_MATCH_KEY;
190 case 'v': /* grep for value */
191 grep_what = H_MATCH_DATA;
193 case 'b': /* grep for both */
194 grep_what = H_MATCH_BOTH;
199 return CMD_RET_USAGE;
205 len = hexport_r(&env_htab, '\n',
206 flag | grep_what | grep_how,
207 &res, 0, argc, argv);
220 #endif /* CONFIG_SPL_BUILD */
223 * Set a new environment variable,
224 * or replace or delete an existing one.
226 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
229 char *name, *value, *s;
230 struct env_entry e, *ep;
232 debug("Initial value for argc=%d\n", argc);
234 #if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
235 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
236 return do_env_set_efi(NULL, flag, --argc, ++argv);
239 while (argc > 1 && **(argv + 1) == '-') {
245 case 'f': /* force */
249 return CMD_RET_USAGE;
253 debug("Final value for argc=%d\n", argc);
256 if (strchr(name, '=')) {
257 printf("## Error: illegal character '='"
258 "in variable name \"%s\"\n", name);
265 if (argc < 3 || argv[2] == NULL) {
266 int rc = hdelete_r(name, &env_htab, env_flag);
268 /* If the variable didn't exist, don't report an error */
269 return rc && rc != -ENOENT ? 1 : 0;
273 * Insert / replace new value
275 for (i = 2, len = 0; i < argc; ++i)
276 len += strlen(argv[i]) + 1;
280 printf("## Can't malloc %d bytes\n", len);
283 for (i = 2, s = value; i < argc; ++i) {
286 while ((*s++ = *v++) != '\0')
295 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
298 printf("## Error inserting \"%s\" variable, errno=%d\n",
306 int env_set(const char *varname, const char *varvalue)
308 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
310 /* before import into hashtable */
311 if (!(gd->flags & GD_FLG_ENV_READY))
314 if (varvalue == NULL || varvalue[0] == '\0')
315 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
317 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
320 #ifndef CONFIG_SPL_BUILD
321 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
325 return CMD_RET_USAGE;
327 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
331 * Prompt for environment variable
333 #if defined(CONFIG_CMD_ASKENV)
334 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
336 char message[CONFIG_SYS_CBSIZE];
337 int i, len, pos, size;
341 local_args[0] = argv[0];
342 local_args[1] = argv[1];
343 local_args[2] = NULL;
344 local_args[3] = NULL;
349 * env_ask envname [message1 ...] [size]
352 return CMD_RET_USAGE;
355 * We test the last argument if it can be converted
356 * into a decimal number. If yes, we assume it's
357 * the size. Otherwise we echo it as part of the
360 i = dectoul(argv[argc - 1], &endptr);
361 if (*endptr != '\0') { /* no size */
362 size = CONFIG_SYS_CBSIZE - 1;
363 } else { /* size given */
369 sprintf(message, "Please enter '%s': ", argv[1]);
371 /* env_ask envname message1 ... messagen [size] */
372 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
374 message[pos++] = ' ';
376 strncpy(message + pos, argv[i], sizeof(message) - pos);
377 pos += strlen(argv[i]);
379 if (pos < sizeof(message) - 1) {
380 message[pos++] = ' ';
383 message[CONFIG_SYS_CBSIZE - 1] = '\0';
386 if (size >= CONFIG_SYS_CBSIZE)
387 size = CONFIG_SYS_CBSIZE - 1;
392 /* prompt for input */
393 len = cli_readline(message);
396 console_buffer[size] = '\0';
399 if (console_buffer[0] != '\0') {
400 local_args[2] = console_buffer;
404 /* Continue calling setenv code */
405 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
409 #if defined(CONFIG_CMD_ENV_CALLBACK)
410 static int print_static_binding(const char *var_name, const char *callback_name,
413 printf("\t%-20s %-20s\n", var_name, callback_name);
418 static int print_active_callback(struct env_entry *entry)
420 struct env_clbk_tbl *clbkp;
424 if (entry->callback == NULL)
427 /* look up the callback in the linker-list */
428 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
429 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
432 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
433 if (entry->callback == clbkp->callback + gd->reloc_off)
435 if (entry->callback == clbkp->callback)
440 if (i == num_callbacks)
441 /* this should probably never happen, but just in case... */
442 printf("\t%-20s %p\n", entry->key, entry->callback);
444 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
450 * Print the callbacks available and what they are bound to
452 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
455 struct env_clbk_tbl *clbkp;
459 /* Print the available callbacks */
460 puts("Available callbacks:\n");
461 puts("\tCallback Name\n");
462 puts("\t-------------\n");
463 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
464 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
467 printf("\t%s\n", clbkp->name);
470 /* Print the static bindings that may exist */
471 puts("Static callback bindings:\n");
472 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
473 printf("\t%-20s %-20s\n", "-------------", "-------------");
474 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
477 /* walk through each variable and print the callback if it has one */
478 puts("Active callback bindings:\n");
479 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
480 printf("\t%-20s %-20s\n", "-------------", "-------------");
481 hwalk_r(&env_htab, print_active_callback);
486 #if defined(CONFIG_CMD_ENV_FLAGS)
487 static int print_static_flags(const char *var_name, const char *flags,
490 enum env_flags_vartype type = env_flags_parse_vartype(flags);
491 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
493 printf("\t%-20s %-20s %-20s\n", var_name,
494 env_flags_get_vartype_name(type),
495 env_flags_get_varaccess_name(access));
500 static int print_active_flags(struct env_entry *entry)
502 enum env_flags_vartype type;
503 enum env_flags_varaccess access;
505 if (entry->flags == 0)
508 type = (enum env_flags_vartype)
509 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
510 access = env_flags_parse_varaccess_from_binflags(entry->flags);
511 printf("\t%-20s %-20s %-20s\n", entry->key,
512 env_flags_get_vartype_name(type),
513 env_flags_get_varaccess_name(access));
519 * Print the flags available and what variables have flags
521 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
523 /* Print the available variable types */
524 printf("Available variable type flags (position %d):\n",
525 ENV_FLAGS_VARTYPE_LOC);
526 puts("\tFlag\tVariable Type Name\n");
527 puts("\t----\t------------------\n");
528 env_flags_print_vartypes();
531 /* Print the available variable access types */
532 printf("Available variable access flags (position %d):\n",
533 ENV_FLAGS_VARACCESS_LOC);
534 puts("\tFlag\tVariable Access Name\n");
535 puts("\t----\t--------------------\n");
536 env_flags_print_varaccess();
539 /* Print the static flags that may exist */
540 puts("Static flags:\n");
541 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
543 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
545 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
548 /* walk through each variable and print the flags if non-default */
549 puts("Active flags:\n");
550 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
552 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
554 hwalk_r(&env_htab, print_active_flags);
560 * Interactively edit an environment variable
562 #if defined(CONFIG_CMD_EDITENV)
563 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
566 char buffer[CONFIG_SYS_CBSIZE];
570 return CMD_RET_USAGE;
572 /* before import into hashtable */
573 if (!(gd->flags & GD_FLG_ENV_READY))
576 /* Set read buffer to initial value or empty sting */
577 init_val = env_get(argv[1]);
579 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
583 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
586 if (buffer[0] == '\0') {
587 const char * const _argv[3] = { "setenv", argv[1], NULL };
589 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
591 const char * const _argv[4] = { "setenv", argv[1], buffer,
594 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
597 #endif /* CONFIG_CMD_EDITENV */
599 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
600 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
603 return env_save() ? 1 : 0;
607 saveenv, 1, 0, do_env_save,
608 "save environment variables to persistent storage",
612 #if defined(CONFIG_CMD_ERASEENV)
613 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
616 return env_erase() ? 1 : 0;
620 eraseenv, 1, 0, do_env_erase,
621 "erase environment variables from persistent storage",
627 #if defined(CONFIG_CMD_NVEDIT_LOAD)
628 static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
631 return env_reload() ? 1 : 0;
635 #if defined(CONFIG_CMD_NVEDIT_SELECT)
636 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
639 return env_select(argv[1]) ? 1 : 0;
643 #endif /* CONFIG_SPL_BUILD */
645 #ifndef CONFIG_SPL_BUILD
646 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
647 int argc, char *const argv[])
649 int all = 0, env_flag = H_INTERACTIVE;
651 debug("Initial value for argc=%d\n", argc);
652 while (--argc > 0 && **++argv == '-') {
657 case 'a': /* default all */
660 case 'f': /* force */
664 return cmd_usage(cmdtp);
668 debug("Final value for argc=%d\n", argc);
669 if (all && (argc == 0)) {
670 /* Reset the whole environment */
671 env_set_default("## Resetting to default environment\n",
675 if (!all && (argc > 0)) {
676 /* Reset individual variables */
677 env_set_default_vars(argc, argv, env_flag);
681 return cmd_usage(cmdtp);
684 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
685 int argc, char *const argv[])
687 int env_flag = H_INTERACTIVE;
690 debug("Initial value for argc=%d\n", argc);
691 while (argc > 1 && **(argv + 1) == '-') {
697 case 'f': /* force */
701 return CMD_RET_USAGE;
705 debug("Final value for argc=%d\n", argc);
710 char *name = *++argv;
712 if (hdelete_r(name, &env_htab, env_flag))
719 #ifdef CONFIG_CMD_EXPORTENV
721 * env export [-t | -b | -c] [-s size] addr [var ...]
722 * -t: export as text format; if size is given, data will be
723 * padded with '\0' bytes; if not, one terminating '\0'
724 * will be added (which is included in the "filesize"
725 * setting so you can for exmple copy this to flash and
726 * keep the termination).
727 * -b: export as binary format (name=value pairs separated by
728 * '\0', list end marked by double "\0\0")
729 * -c: export as checksum protected environment format as
730 * used for example by "saveenv" command
732 * size of output buffer
733 * addr: memory address where environment gets stored
734 * var... List of variable names that get included into the
735 * export. Without arguments, the whole environment gets
738 * With "-c" and size is NOT given, then the export command will
739 * format the data as currently used for the persistent storage,
740 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
741 * prepend a valid CRC32 checksum and, in case of redundant
742 * environment, a "current" redundancy flag. If size is given, this
743 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
744 * checksum and redundancy flag will be inserted.
746 * With "-b" and "-t", always only the real data (including a
747 * terminating '\0' byte) will be written; here the optional size
748 * argument will be used to make sure not to overflow the user
749 * provided buffer; the command will abort if the size is not
750 * sufficient. Any remaining space will be '\0' padded.
752 * On successful return, the variable "filesize" will be set.
753 * Note that filesize includes the trailing/terminating '\0' byte(s).
755 * Usage scenario: create a text snapshot/backup of the current settings:
757 * => env export -t 100000
758 * => era ${backup_addr} +${filesize}
759 * => cp.b 100000 ${backup_addr} ${filesize}
761 * Re-import this snapshot, deleting all other settings:
763 * => env import -d -t ${backup_addr}
765 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
766 int argc, char *const argv[])
770 char *ptr, *cmd, *res;
780 while (--argc > 0 && **++argv == '-') {
784 case 'b': /* raw binary format */
789 case 'c': /* external checksum format */
795 case 's': /* size given */
797 return cmd_usage(cmdtp);
798 size = hextoul(*++argv, NULL);
800 case 't': /* text format */
806 return CMD_RET_USAGE;
813 return CMD_RET_USAGE;
815 addr = hextoul(argv[0], NULL);
816 ptr = map_sysmem(addr, size);
819 memset(ptr, '\0', size);
824 if (sep) { /* export as text file */
825 len = hexport_r(&env_htab, sep,
826 H_MATCH_KEY | H_MATCH_IDENT,
827 &ptr, size, argc, argv);
829 pr_err("## Error: Cannot export environment: errno = %d\n",
833 sprintf(buf, "%zX", (size_t)len);
834 env_set("filesize", buf);
841 if (chk) /* export as checksum protected block */
842 res = (char *)envp->data;
843 else /* export as raw binary data */
846 len = hexport_r(&env_htab, '\0',
847 H_MATCH_KEY | H_MATCH_IDENT,
848 &res, ENV_SIZE, argc, argv);
850 pr_err("## Error: Cannot export environment: errno = %d\n",
856 envp->crc = crc32(0, envp->data,
857 size ? size - offsetof(env_t, data) : ENV_SIZE);
858 #ifdef CONFIG_ENV_ADDR_REDUND
859 envp->flags = ENV_REDUND_ACTIVE;
862 env_set_hex("filesize", len + offsetof(env_t, data));
867 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
873 #ifdef CONFIG_CMD_IMPORTENV
875 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
876 * -d: delete existing environment before importing if no var is
877 * passed; if vars are passed, if one var is in the current
878 * environment but not in the environment at addr, delete var from
879 * current environment;
880 * otherwise overwrite / append to existing definitions
881 * -t: assume text format; either "size" must be given or the
882 * text data must be '\0' terminated
883 * -r: handle CRLF like LF, that means exported variables with
884 * a content which ends with \r won't get imported. Used
885 * to import text files created with editors which are using CRLF
886 * for line endings. Only effective in addition to -t.
887 * -b: assume binary format ('\0' separated, "\0\0" terminated)
888 * -c: assume checksum protected environment format
889 * addr: memory address to read from
890 * size: length of input data; if missing, proper '\0'
891 * termination is mandatory
892 * if var is set and size should be missing (i.e. '\0'
893 * termination), set size to '-'
894 * var... List of the names of the only variables that get imported from
895 * the environment at address 'addr'. Without arguments, the whole
896 * environment gets imported.
898 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
899 int argc, char *const argv[])
913 while (--argc > 0 && **++argv == '-') {
917 case 'b': /* raw binary format */
922 case 'c': /* external checksum format */
928 case 't': /* text format */
933 case 'r': /* handle CRLF like LF */
940 return CMD_RET_USAGE;
946 return CMD_RET_USAGE;
949 printf("## Warning: defaulting to text format\n");
951 if (sep != '\n' && crlf_is_lf )
954 addr = hextoul(argv[0], NULL);
955 ptr = map_sysmem(addr, 0);
957 if (argc >= 2 && strcmp(argv[1], "-")) {
958 size = hextoul(argv[1], NULL);
960 puts("## Error: external checksum format must pass size\n");
961 return CMD_RET_FAILURE;
967 while (size < MAX_ENV_SIZE) {
968 if ((*s == sep) && (*(s+1) == '\0'))
973 if (size == MAX_ENV_SIZE) {
974 printf("## Warning: Input data exceeds %d bytes"
975 " - truncated\n", MAX_ENV_SIZE);
978 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
986 env_t *ep = (env_t *)ptr;
988 if (size <= offsetof(env_t, data)) {
989 printf("## Error: Invalid size 0x%zX\n", size);
993 size -= offsetof(env_t, data);
994 memcpy(&crc, &ep->crc, sizeof(crc));
996 if (crc32(0, ep->data, size) != crc) {
997 puts("## Error: bad CRC, import failed\n");
1000 ptr = (char *)ep->data;
1003 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1004 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1005 pr_err("## Error: Environment import failed: errno = %d\n",
1009 gd->flags |= GD_FLG_ENV_READY;
1014 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1020 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1021 static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
1022 int argc, char *const argv[])
1025 char *from = argv[2];
1026 char *default_value = NULL;
1029 if (argc < 3 || argc > 4) {
1030 return CMD_RET_USAGE;
1034 default_value = argv[3];
1037 if (env_get(from) == NULL && default_value == NULL) {
1038 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1040 return CMD_RET_FAILURE;
1043 if (env_get(from) == NULL) {
1044 ret = env_set(to, default_value);
1047 ret = env_set(to, env_get(from));
1051 return CMD_RET_SUCCESS;
1054 return CMD_RET_FAILURE;
1059 #if defined(CONFIG_CMD_NVEDIT_INFO)
1061 * print_env_info - print environment information
1063 static int print_env_info(void)
1067 /* print environment validity value */
1068 switch (gd->env_valid) {
1076 value = "redundant";
1082 printf("env_valid = %s\n", value);
1084 /* print environment ready flag */
1085 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1086 printf("env_ready = %s\n", value);
1088 /* print environment using default flag */
1089 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1090 printf("env_use_default = %s\n", value);
1092 return CMD_RET_SUCCESS;
1095 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1096 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1099 * env info - display environment information
1100 * env info [-d] - evaluate whether default environment is used
1101 * env info [-p] - evaluate whether environment can be persisted
1102 * Add [-q] - quiet mode, use only for command result, for test by example:
1103 * test env info -p -d -q
1105 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1106 int argc, char *const argv[])
1109 int eval_results = 0;
1111 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1112 enum env_location loc;
1115 /* display environment information */
1117 return print_env_info();
1119 /* process options */
1120 while (--argc > 0 && **++argv == '-') {
1126 eval_flags |= ENV_INFO_IS_DEFAULT;
1129 eval_flags |= ENV_INFO_IS_PERSISTED;
1135 return CMD_RET_USAGE;
1140 /* evaluate whether default environment is used */
1141 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1142 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1144 printf("Default environment is used\n");
1145 eval_results |= ENV_INFO_IS_DEFAULT;
1148 printf("Environment was loaded from persistent storage\n");
1152 /* evaluate whether environment can be persisted */
1153 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1154 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1155 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1156 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1158 printf("Environment can be persisted\n");
1159 eval_results |= ENV_INFO_IS_PERSISTED;
1162 printf("Environment cannot be persisted\n");
1166 printf("Environment cannot be persisted\n");
1170 /* The result of evaluations is combined with AND */
1171 if (eval_flags != eval_results)
1172 return CMD_RET_FAILURE;
1174 return CMD_RET_SUCCESS;
1178 #if defined(CONFIG_CMD_ENV_EXISTS)
1179 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1182 struct env_entry e, *ep;
1185 return CMD_RET_USAGE;
1189 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1191 return (ep == NULL) ? 1 : 0;
1196 * New command line interface: "env" command with subcommands
1198 static struct cmd_tbl cmd_env_sub[] = {
1199 #if defined(CONFIG_CMD_ASKENV)
1200 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1202 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1203 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1204 #if defined(CONFIG_CMD_EDITENV)
1205 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1207 #if defined(CONFIG_CMD_ENV_CALLBACK)
1208 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1210 #if defined(CONFIG_CMD_ENV_FLAGS)
1211 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1213 #if defined(CONFIG_CMD_EXPORTENV)
1214 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1216 #if defined(CONFIG_CMD_GREPENV)
1217 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1219 #if defined(CONFIG_CMD_IMPORTENV)
1220 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1222 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1223 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1225 #if defined(CONFIG_CMD_NVEDIT_INFO)
1226 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
1228 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1229 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1231 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1232 #if defined(CONFIG_CMD_RUN)
1233 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1235 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1236 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1237 #if defined(CONFIG_CMD_ERASEENV)
1238 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1241 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1242 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1244 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1245 #if defined(CONFIG_CMD_ENV_EXISTS)
1246 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1250 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1251 void env_reloc(void)
1253 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1257 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1262 return CMD_RET_USAGE;
1264 /* drop initial "env" arg */
1268 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1271 return cp->cmd(cmdtp, flag, argc, argv);
1273 return CMD_RET_USAGE;
1276 #ifdef CONFIG_SYS_LONGHELP
1277 static char env_help_text[] =
1278 #if defined(CONFIG_CMD_ASKENV)
1279 "ask name [message] [size] - ask for environment variable\nenv "
1281 #if defined(CONFIG_CMD_ENV_CALLBACK)
1282 "callbacks - print callbacks and their associated variables\nenv "
1284 "default [-f] -a - [forcibly] reset default environment\n"
1285 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1286 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1287 #if defined(CONFIG_CMD_EDITENV)
1288 "env edit name - edit environment variable\n"
1290 #if defined(CONFIG_CMD_ENV_EXISTS)
1291 "env exists name - tests for existence of variable\n"
1293 #if defined(CONFIG_CMD_EXPORTENV)
1294 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1296 #if defined(CONFIG_CMD_ENV_FLAGS)
1297 "env flags - print variables that have non-default flags\n"
1299 #if defined(CONFIG_CMD_GREPENV)
1301 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1303 "env grep [-n | -v | -b] string [...] - search environment\n"
1306 #if defined(CONFIG_CMD_IMPORTENV)
1307 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1309 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1310 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1312 #if defined(CONFIG_CMD_NVEDIT_INFO)
1313 "env info - display environment information\n"
1314 "env info [-d] [-p] [-q] - evaluate environment information\n"
1315 " \"-d\": default environment is used\n"
1316 " \"-p\": environment can be persisted\n"
1317 " \"-q\": quiet output\n"
1319 "env print [-a | name ...] - print environment\n"
1320 #if defined(CONFIG_CMD_NVEDIT_EFI)
1321 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
1323 #if defined(CONFIG_CMD_RUN)
1324 "env run var [...] - run commands in an environment variable\n"
1326 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1327 "env save - save environment\n"
1328 #if defined(CONFIG_CMD_ERASEENV)
1329 "env erase - erase environment\n"
1332 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1333 "env load - load environment\n"
1335 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1336 "env select [target] - select environment target\n"
1338 #if defined(CONFIG_CMD_NVEDIT_EFI)
1339 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
1340 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1342 "env set [-f] name [arg ...]\n";
1346 env, CONFIG_SYS_MAXARGS, 1, do_env,
1347 "environment handling commands", env_help_text
1351 * Old command line interface, kept for compatibility
1354 #if defined(CONFIG_CMD_EDITENV)
1355 U_BOOT_CMD_COMPLETE(
1356 editenv, 2, 0, do_env_edit,
1357 "edit environment variable",
1359 " - edit environment variable 'name'",
1364 U_BOOT_CMD_COMPLETE(
1365 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1366 "print environment variables",
1367 "[-a]\n - print [all] values of all environment variables\n"
1368 #if defined(CONFIG_CMD_NVEDIT_EFI)
1369 "printenv -e [-guid guid][-n] [name ...]\n"
1370 " - print UEFI variable 'name' or all the variables\n"
1371 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1372 " \"-n\": suppress dumping variable's value\n"
1374 "printenv name ...\n"
1375 " - print value of environment variable 'name'",
1379 #ifdef CONFIG_CMD_GREPENV
1380 U_BOOT_CMD_COMPLETE(
1381 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1382 "search environment variables",
1384 "[-e] [-n | -v | -b] string ...\n"
1386 "[-n | -v | -b] string ...\n"
1388 " - list environment name=value pairs matching 'string'\n"
1390 " \"-e\": enable regular expressions;\n"
1392 " \"-n\": search variable names; \"-v\": search values;\n"
1393 " \"-b\": search both names and values (default)",
1398 U_BOOT_CMD_COMPLETE(
1399 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1400 "set environment variables",
1401 #if defined(CONFIG_CMD_NVEDIT_EFI)
1402 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1403 " [-i addr:size name], or [name [value ...]]\n"
1404 " - set UEFI variable 'name' to 'value' ...'\n"
1405 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1406 " \"-nv\": set non-volatile attribute\n"
1407 " \"-bs\": set boot-service attribute\n"
1408 " \"-rt\": set runtime attribute\n"
1409 " \"-at\": set time-based authentication attribute\n"
1410 " \"-a\": append-write\n"
1411 " \"-i addr,size\": use <addr,size> as variable's value\n"
1412 " \"-v\": verbose message\n"
1413 " - delete UEFI variable 'name' if 'value' not specified\n"
1415 "setenv [-f] name value ...\n"
1416 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1417 "setenv [-f] name\n"
1418 " - [forcibly] delete environment variable 'name'",
1422 #if defined(CONFIG_CMD_ASKENV)
1425 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1426 "get environment variables from stdin",
1427 "name [message] [size]\n"
1428 " - get environment variable 'name' from stdin (max 'size' chars)"
1432 #if defined(CONFIG_CMD_RUN)
1433 U_BOOT_CMD_COMPLETE(
1434 run, CONFIG_SYS_MAXARGS, 1, do_run,
1435 "run commands in an environment variable",
1437 " - run the commands in the environment variable(s) 'var'",
1441 #endif /* CONFIG_SPL_BUILD */