1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2013
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
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 <linux/printk.h>
40 #include <u-boot/crc.h>
41 #include <linux/stddef.h>
42 #include <asm/byteorder.h>
45 DECLARE_GLOBAL_DATA_PTR;
48 * Maximum expected input data size for import command
50 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
53 * This variable is incremented on each do_env_set(), so it can
54 * be used via env_get_id() as an indication, if the environment
55 * has changed or not. So it is possible to reread an environment
56 * variable only if the environment was changed ... done so for
57 * example in NetInitLoop()
59 static int env_id = 1;
66 #ifndef CONFIG_SPL_BUILD
68 * Command interface: print one or all environment variables
70 * Returns 0 in case of error, or length of printed string
72 static int env_print(char *name, int flag)
77 if (name) { /* print a single name */
78 struct env_entry e, *ep;
82 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
85 len = printf("%s=%s\n", ep->key, ep->data);
89 /* print whole list */
90 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
98 /* should never happen */
99 printf("## Error: cannot export environment\n");
103 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
108 int env_flag = H_HIDE_DOT;
110 #if defined(CONFIG_CMD_NVEDIT_EFI)
111 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
112 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
115 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
118 env_flag &= ~H_HIDE_DOT;
122 /* print all env vars */
123 rcode = env_print(NULL, env_flag);
126 printf("\nEnvironment size: %d/%ld bytes\n",
127 rcode, (ulong)ENV_SIZE);
131 /* print selected env vars */
132 env_flag &= ~H_HIDE_DOT;
133 for (i = 1; i < argc; ++i) {
134 int rc = env_print(argv[i], env_flag);
136 printf("## Error: \"%s\" not defined\n", argv[i]);
144 #ifdef CONFIG_CMD_GREPENV
145 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
146 int argc, char *const argv[])
149 int len, grep_how, grep_what;
152 return CMD_RET_USAGE;
154 grep_how = H_MATCH_SUBSTR; /* default: substring search */
155 grep_what = H_MATCH_BOTH; /* default: grep names and values */
157 while (--argc > 0 && **++argv == '-') {
162 case 'e': /* use regex matching */
163 grep_how = H_MATCH_REGEX;
166 case 'n': /* grep for name */
167 grep_what = H_MATCH_KEY;
169 case 'v': /* grep for value */
170 grep_what = H_MATCH_DATA;
172 case 'b': /* grep for both */
173 grep_what = H_MATCH_BOTH;
178 return CMD_RET_USAGE;
184 len = hexport_r(&env_htab, '\n',
185 flag | grep_what | grep_how,
186 &res, 0, argc, argv);
199 #endif /* CONFIG_SPL_BUILD */
202 * Set a new environment variable,
203 * or replace or delete an existing one.
205 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
208 char *name, *value, *s;
209 struct env_entry e, *ep;
211 debug("Initial value for argc=%d\n", argc);
213 #if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
214 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
215 return do_env_set_efi(NULL, flag, --argc, ++argv);
218 while (argc > 1 && **(argv + 1) == '-') {
224 case 'f': /* force */
228 return CMD_RET_USAGE;
232 debug("Final value for argc=%d\n", argc);
235 if (strchr(name, '=')) {
236 printf("## Error: illegal character '='"
237 "in variable name \"%s\"\n", name);
244 if (argc < 3 || argv[2] == NULL) {
245 int rc = hdelete_r(name, &env_htab, env_flag);
247 /* If the variable didn't exist, don't report an error */
248 return rc && rc != -ENOENT ? 1 : 0;
252 * Insert / replace new value
254 for (i = 2, len = 0; i < argc; ++i)
255 len += strlen(argv[i]) + 1;
259 printf("## Can't malloc %d bytes\n", len);
262 for (i = 2, s = value; i < argc; ++i) {
265 while ((*s++ = *v++) != '\0')
274 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
277 printf("## Error inserting \"%s\" variable, errno=%d\n",
285 int env_set(const char *varname, const char *varvalue)
287 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
289 /* before import into hashtable */
290 if (!(gd->flags & GD_FLG_ENV_READY))
293 if (varvalue == NULL || varvalue[0] == '\0')
294 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
296 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
299 #ifndef CONFIG_SPL_BUILD
300 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
304 return CMD_RET_USAGE;
306 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
310 * Prompt for environment variable
312 #if defined(CONFIG_CMD_ASKENV)
313 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
315 char message[CONFIG_SYS_CBSIZE];
316 int i, len, pos, size;
320 local_args[0] = argv[0];
321 local_args[1] = argv[1];
322 local_args[2] = NULL;
323 local_args[3] = NULL;
328 * env_ask envname [message1 ...] [size]
331 return CMD_RET_USAGE;
334 * We test the last argument if it can be converted
335 * into a decimal number. If yes, we assume it's
336 * the size. Otherwise we echo it as part of the
339 i = dectoul(argv[argc - 1], &endptr);
340 if (*endptr != '\0') { /* no size */
341 size = CONFIG_SYS_CBSIZE - 1;
342 } else { /* size given */
348 sprintf(message, "Please enter '%s': ", argv[1]);
350 /* env_ask envname message1 ... messagen [size] */
351 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
353 message[pos++] = ' ';
355 strncpy(message + pos, argv[i], sizeof(message) - pos);
356 pos += strlen(argv[i]);
358 if (pos < sizeof(message) - 1) {
359 message[pos++] = ' ';
362 message[CONFIG_SYS_CBSIZE - 1] = '\0';
365 if (size >= CONFIG_SYS_CBSIZE)
366 size = CONFIG_SYS_CBSIZE - 1;
371 /* prompt for input */
372 len = cli_readline(message);
375 console_buffer[size] = '\0';
378 if (console_buffer[0] != '\0') {
379 local_args[2] = console_buffer;
383 /* Continue calling setenv code */
384 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
388 #if defined(CONFIG_CMD_ENV_CALLBACK)
389 static int print_static_binding(const char *var_name, const char *callback_name,
392 printf("\t%-20s %-20s\n", var_name, callback_name);
397 static int print_active_callback(struct env_entry *entry)
399 struct env_clbk_tbl *clbkp;
403 if (entry->callback == NULL)
406 /* look up the callback in the linker-list */
407 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
408 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
411 if (entry->callback == clbkp->callback)
415 if (i == num_callbacks)
416 /* this should probably never happen, but just in case... */
417 printf("\t%-20s %p\n", entry->key, entry->callback);
419 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
425 * Print the callbacks available and what they are bound to
427 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
430 struct env_clbk_tbl *clbkp;
434 /* Print the available callbacks */
435 puts("Available callbacks:\n");
436 puts("\tCallback Name\n");
437 puts("\t-------------\n");
438 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
439 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
442 printf("\t%s\n", clbkp->name);
445 /* Print the static bindings that may exist */
446 puts("Static callback bindings:\n");
447 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
448 printf("\t%-20s %-20s\n", "-------------", "-------------");
449 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
452 /* walk through each variable and print the callback if it has one */
453 puts("Active callback bindings:\n");
454 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
455 printf("\t%-20s %-20s\n", "-------------", "-------------");
456 hwalk_r(&env_htab, print_active_callback);
461 #if defined(CONFIG_CMD_ENV_FLAGS)
462 static int print_static_flags(const char *var_name, const char *flags,
465 enum env_flags_vartype type = env_flags_parse_vartype(flags);
466 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
468 printf("\t%-20s %-20s %-20s\n", var_name,
469 env_flags_get_vartype_name(type),
470 env_flags_get_varaccess_name(access));
475 static int print_active_flags(struct env_entry *entry)
477 enum env_flags_vartype type;
478 enum env_flags_varaccess access;
480 if (entry->flags == 0)
483 type = (enum env_flags_vartype)
484 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
485 access = env_flags_parse_varaccess_from_binflags(entry->flags);
486 printf("\t%-20s %-20s %-20s\n", entry->key,
487 env_flags_get_vartype_name(type),
488 env_flags_get_varaccess_name(access));
494 * Print the flags available and what variables have flags
496 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
498 /* Print the available variable types */
499 printf("Available variable type flags (position %d):\n",
500 ENV_FLAGS_VARTYPE_LOC);
501 puts("\tFlag\tVariable Type Name\n");
502 puts("\t----\t------------------\n");
503 env_flags_print_vartypes();
506 /* Print the available variable access types */
507 printf("Available variable access flags (position %d):\n",
508 ENV_FLAGS_VARACCESS_LOC);
509 puts("\tFlag\tVariable Access Name\n");
510 puts("\t----\t--------------------\n");
511 env_flags_print_varaccess();
514 /* Print the static flags that may exist */
515 puts("Static flags:\n");
516 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
518 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
520 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
523 /* walk through each variable and print the flags if non-default */
524 puts("Active flags:\n");
525 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
527 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
529 hwalk_r(&env_htab, print_active_flags);
535 * Interactively edit an environment variable
537 #if defined(CONFIG_CMD_EDITENV)
538 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
541 char buffer[CONFIG_SYS_CBSIZE];
545 return CMD_RET_USAGE;
547 /* before import into hashtable */
548 if (!(gd->flags & GD_FLG_ENV_READY))
551 /* Set read buffer to initial value or empty sting */
552 init_val = env_get(argv[1]);
554 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
558 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
561 if (buffer[0] == '\0') {
562 const char * const _argv[3] = { "setenv", argv[1], NULL };
564 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
566 const char * const _argv[4] = { "setenv", argv[1], buffer,
569 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
572 #endif /* CONFIG_CMD_EDITENV */
574 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
575 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
578 return env_save() ? 1 : 0;
582 saveenv, 1, 0, do_env_save,
583 "save environment variables to persistent storage",
587 #if defined(CONFIG_CMD_ERASEENV)
588 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
591 return env_erase() ? 1 : 0;
595 eraseenv, 1, 0, do_env_erase,
596 "erase environment variables from persistent storage",
602 #if defined(CONFIG_CMD_NVEDIT_LOAD)
603 static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
606 return env_reload() ? 1 : 0;
610 #if defined(CONFIG_CMD_NVEDIT_SELECT)
611 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
614 return env_select(argv[1]) ? 1 : 0;
618 #endif /* CONFIG_SPL_BUILD */
620 #ifndef CONFIG_SPL_BUILD
621 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
622 int argc, char *const argv[])
624 int all = 0, env_flag = H_INTERACTIVE;
626 debug("Initial value for argc=%d\n", argc);
627 while (--argc > 0 && **++argv == '-') {
632 case 'a': /* default all */
635 case 'f': /* force */
639 return cmd_usage(cmdtp);
643 debug("Final value for argc=%d\n", argc);
644 if (all && (argc == 0)) {
645 /* Reset the whole environment */
646 env_set_default("## Resetting to default environment\n",
650 if (!all && (argc > 0)) {
651 /* Reset individual variables */
652 env_set_default_vars(argc, argv, env_flag);
656 return cmd_usage(cmdtp);
659 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
660 int argc, char *const argv[])
662 int env_flag = H_INTERACTIVE;
665 debug("Initial value for argc=%d\n", argc);
666 while (argc > 1 && **(argv + 1) == '-') {
672 case 'f': /* force */
676 return CMD_RET_USAGE;
680 debug("Final value for argc=%d\n", argc);
685 char *name = *++argv;
687 if (hdelete_r(name, &env_htab, env_flag))
694 #ifdef CONFIG_CMD_EXPORTENV
696 * env export [-t | -b | -c] [-s size] addr [var ...]
697 * -t: export as text format; if size is given, data will be
698 * padded with '\0' bytes; if not, one terminating '\0'
699 * will be added (which is included in the "filesize"
700 * setting so you can for exmple copy this to flash and
701 * keep the termination).
702 * -b: export as binary format (name=value pairs separated by
703 * '\0', list end marked by double "\0\0")
704 * -c: export as checksum protected environment format as
705 * used for example by "saveenv" command
707 * size of output buffer
708 * addr: memory address where environment gets stored
709 * var... List of variable names that get included into the
710 * export. Without arguments, the whole environment gets
713 * With "-c" and size is NOT given, then the export command will
714 * format the data as currently used for the persistent storage,
715 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
716 * prepend a valid CRC32 checksum and, in case of redundant
717 * environment, a "current" redundancy flag. If size is given, this
718 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
719 * checksum and redundancy flag will be inserted.
721 * With "-b" and "-t", always only the real data (including a
722 * terminating '\0' byte) will be written; here the optional size
723 * argument will be used to make sure not to overflow the user
724 * provided buffer; the command will abort if the size is not
725 * sufficient. Any remaining space will be '\0' padded.
727 * On successful return, the variable "filesize" will be set.
728 * Note that filesize includes the trailing/terminating '\0' byte(s).
730 * Usage scenario: create a text snapshot/backup of the current settings:
732 * => env export -t 100000
733 * => era ${backup_addr} +${filesize}
734 * => cp.b 100000 ${backup_addr} ${filesize}
736 * Re-import this snapshot, deleting all other settings:
738 * => env import -d -t ${backup_addr}
740 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
741 int argc, char *const argv[])
745 char *ptr, *cmd, *res;
755 while (--argc > 0 && **++argv == '-') {
759 case 'b': /* raw binary format */
764 case 'c': /* external checksum format */
770 case 's': /* size given */
772 return cmd_usage(cmdtp);
773 size = hextoul(*++argv, NULL);
775 case 't': /* text format */
781 return CMD_RET_USAGE;
788 return CMD_RET_USAGE;
790 addr = hextoul(argv[0], NULL);
791 ptr = map_sysmem(addr, size);
794 memset(ptr, '\0', size);
799 if (sep) { /* export as text file */
800 len = hexport_r(&env_htab, sep,
801 H_MATCH_KEY | H_MATCH_IDENT,
802 &ptr, size, argc, argv);
804 pr_err("## Error: Cannot export environment: errno = %d\n",
808 sprintf(buf, "%zX", (size_t)len);
809 env_set("filesize", buf);
816 if (chk) /* export as checksum protected block */
817 res = (char *)envp->data;
818 else /* export as raw binary data */
821 len = hexport_r(&env_htab, '\0',
822 H_MATCH_KEY | H_MATCH_IDENT,
823 &res, ENV_SIZE, argc, argv);
825 pr_err("## Error: Cannot export environment: errno = %d\n",
831 envp->crc = crc32(0, envp->data,
832 size ? size - offsetof(env_t, data) : ENV_SIZE);
833 #ifdef CONFIG_ENV_ADDR_REDUND
834 envp->flags = ENV_REDUND_ACTIVE;
837 env_set_hex("filesize", len + offsetof(env_t, data));
842 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
848 #ifdef CONFIG_CMD_IMPORTENV
850 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
851 * -d: delete existing environment before importing if no var is
852 * passed; if vars are passed, if one var is in the current
853 * environment but not in the environment at addr, delete var from
854 * current environment;
855 * otherwise overwrite / append to existing definitions
856 * -t: assume text format; either "size" must be given or the
857 * text data must be '\0' terminated
858 * -r: handle CRLF like LF, that means exported variables with
859 * a content which ends with \r won't get imported. Used
860 * to import text files created with editors which are using CRLF
861 * for line endings. Only effective in addition to -t.
862 * -b: assume binary format ('\0' separated, "\0\0" terminated)
863 * -c: assume checksum protected environment format
864 * addr: memory address to read from
865 * size: length of input data; if missing, proper '\0'
866 * termination is mandatory
867 * if var is set and size should be missing (i.e. '\0'
868 * termination), set size to '-'
869 * var... List of the names of the only variables that get imported from
870 * the environment at address 'addr'. Without arguments, the whole
871 * environment gets imported.
873 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
874 int argc, char *const argv[])
888 while (--argc > 0 && **++argv == '-') {
892 case 'b': /* raw binary format */
897 case 'c': /* external checksum format */
903 case 't': /* text format */
908 case 'r': /* handle CRLF like LF */
915 return CMD_RET_USAGE;
921 return CMD_RET_USAGE;
924 printf("## Warning: defaulting to text format\n");
926 if (sep != '\n' && crlf_is_lf )
929 addr = hextoul(argv[0], NULL);
930 ptr = map_sysmem(addr, 0);
932 if (argc >= 2 && strcmp(argv[1], "-")) {
933 size = hextoul(argv[1], NULL);
935 puts("## Error: external checksum format must pass size\n");
936 return CMD_RET_FAILURE;
942 while (size < MAX_ENV_SIZE) {
943 if ((*s == sep) && (*(s+1) == '\0'))
948 if (size == MAX_ENV_SIZE) {
949 printf("## Warning: Input data exceeds %d bytes"
950 " - truncated\n", MAX_ENV_SIZE);
953 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
961 env_t *ep = (env_t *)ptr;
963 if (size <= offsetof(env_t, data)) {
964 printf("## Error: Invalid size 0x%zX\n", size);
968 size -= offsetof(env_t, data);
969 memcpy(&crc, &ep->crc, sizeof(crc));
971 if (crc32(0, ep->data, size) != crc) {
972 puts("## Error: bad CRC, import failed\n");
975 ptr = (char *)ep->data;
978 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
979 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
980 pr_err("## Error: Environment import failed: errno = %d\n",
984 gd->flags |= GD_FLG_ENV_READY;
989 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
995 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
996 static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
997 int argc, char *const argv[])
1000 char *from = argv[2];
1001 char *default_value = NULL;
1005 if (argc < 3 || argc > 4) {
1006 return CMD_RET_USAGE;
1010 default_value = argv[3];
1013 val = env_get(from) ?: default_value;
1015 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1017 return CMD_RET_FAILURE;
1020 ret = env_set(to, val);
1023 return CMD_RET_SUCCESS;
1026 return CMD_RET_FAILURE;
1031 #if defined(CONFIG_CMD_NVEDIT_INFO)
1033 * print_env_info - print environment information
1035 static int print_env_info(void)
1039 /* print environment validity value */
1040 switch (gd->env_valid) {
1048 value = "redundant";
1054 printf("env_valid = %s\n", value);
1056 /* print environment ready flag */
1057 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1058 printf("env_ready = %s\n", value);
1060 /* print environment using default flag */
1061 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1062 printf("env_use_default = %s\n", value);
1064 return CMD_RET_SUCCESS;
1067 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1068 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1071 * env info - display environment information
1072 * env info [-d] - evaluate whether default environment is used
1073 * env info [-p] - evaluate whether environment can be persisted
1074 * Add [-q] - quiet mode, use only for command result, for test by example:
1075 * test env info -p -d -q
1077 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1078 int argc, char *const argv[])
1081 int eval_results = 0;
1083 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1084 enum env_location loc;
1087 /* display environment information */
1089 return print_env_info();
1091 /* process options */
1092 while (--argc > 0 && **++argv == '-') {
1098 eval_flags |= ENV_INFO_IS_DEFAULT;
1101 eval_flags |= ENV_INFO_IS_PERSISTED;
1107 return CMD_RET_USAGE;
1112 /* evaluate whether default environment is used */
1113 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1114 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1116 printf("Default environment is used\n");
1117 eval_results |= ENV_INFO_IS_DEFAULT;
1120 printf("Environment was loaded from persistent storage\n");
1124 /* evaluate whether environment can be persisted */
1125 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1126 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1127 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1128 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1130 printf("Environment can be persisted\n");
1131 eval_results |= ENV_INFO_IS_PERSISTED;
1134 printf("Environment cannot be persisted\n");
1138 printf("Environment cannot be persisted\n");
1142 /* The result of evaluations is combined with AND */
1143 if (eval_flags != eval_results)
1144 return CMD_RET_FAILURE;
1146 return CMD_RET_SUCCESS;
1150 #if defined(CONFIG_CMD_ENV_EXISTS)
1151 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1154 struct env_entry e, *ep;
1157 return CMD_RET_USAGE;
1161 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1163 return (ep == NULL) ? 1 : 0;
1168 * New command line interface: "env" command with subcommands
1170 static struct cmd_tbl cmd_env_sub[] = {
1171 #if defined(CONFIG_CMD_ASKENV)
1172 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1174 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1175 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1176 #if defined(CONFIG_CMD_EDITENV)
1177 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1179 #if defined(CONFIG_CMD_ENV_CALLBACK)
1180 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1182 #if defined(CONFIG_CMD_ENV_FLAGS)
1183 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1185 #if defined(CONFIG_CMD_EXPORTENV)
1186 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1188 #if defined(CONFIG_CMD_GREPENV)
1189 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1191 #if defined(CONFIG_CMD_IMPORTENV)
1192 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1194 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1195 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1197 #if defined(CONFIG_CMD_NVEDIT_INFO)
1198 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
1200 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1201 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1203 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1204 #if defined(CONFIG_CMD_RUN)
1205 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1207 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1208 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1209 #if defined(CONFIG_CMD_ERASEENV)
1210 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1213 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1214 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1216 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1217 #if defined(CONFIG_CMD_ENV_EXISTS)
1218 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1222 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1227 return CMD_RET_USAGE;
1229 /* drop initial "env" arg */
1233 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1236 return cp->cmd(cmdtp, flag, argc, argv);
1238 return CMD_RET_USAGE;
1241 U_BOOT_LONGHELP(env,
1242 #if defined(CONFIG_CMD_ASKENV)
1243 "ask name [message] [size] - ask for environment variable\nenv "
1245 #if defined(CONFIG_CMD_ENV_CALLBACK)
1246 "callbacks - print callbacks and their associated variables\nenv "
1248 "default [-f] -a - [forcibly] reset default environment\n"
1249 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1250 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1251 #if defined(CONFIG_CMD_EDITENV)
1252 "env edit name - edit environment variable\n"
1254 #if defined(CONFIG_CMD_ENV_EXISTS)
1255 "env exists name - tests for existence of variable\n"
1257 #if defined(CONFIG_CMD_EXPORTENV)
1258 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1260 #if defined(CONFIG_CMD_ENV_FLAGS)
1261 "env flags - print variables that have non-default flags\n"
1263 #if defined(CONFIG_CMD_GREPENV)
1265 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1267 "env grep [-n | -v | -b] string [...] - search environment\n"
1270 #if defined(CONFIG_CMD_IMPORTENV)
1271 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1273 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1274 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1276 #if defined(CONFIG_CMD_NVEDIT_INFO)
1277 "env info - display environment information\n"
1278 "env info [-d] [-p] [-q] - evaluate environment information\n"
1279 " \"-d\": default environment is used\n"
1280 " \"-p\": environment can be persisted\n"
1281 " \"-q\": quiet output\n"
1283 "env print [-a | name ...] - print environment\n"
1284 #if defined(CONFIG_CMD_NVEDIT_EFI)
1285 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
1287 #if defined(CONFIG_CMD_RUN)
1288 "env run var [...] - run commands in an environment variable\n"
1290 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1291 "env save - save environment\n"
1292 #if defined(CONFIG_CMD_ERASEENV)
1293 "env erase - erase environment\n"
1296 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1297 "env load - load environment\n"
1299 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1300 "env select [target] - select environment target\n"
1302 #if defined(CONFIG_CMD_NVEDIT_EFI)
1303 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
1304 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1306 "env set [-f] name [arg ...]\n");
1309 env, CONFIG_SYS_MAXARGS, 1, do_env,
1310 "environment handling commands", env_help_text
1314 * Old command line interface, kept for compatibility
1317 #if defined(CONFIG_CMD_EDITENV)
1318 U_BOOT_CMD_COMPLETE(
1319 editenv, 2, 0, do_env_edit,
1320 "edit environment variable",
1322 " - edit environment variable 'name'",
1327 U_BOOT_CMD_COMPLETE(
1328 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1329 "print environment variables",
1330 "[-a]\n - print [all] values of all environment variables\n"
1331 #if defined(CONFIG_CMD_NVEDIT_EFI)
1332 "printenv -e [-guid guid][-n] [name ...]\n"
1333 " - print UEFI variable 'name' or all the variables\n"
1334 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1335 " \"-n\": suppress dumping variable's value\n"
1337 "printenv name ...\n"
1338 " - print value of environment variable 'name'",
1342 #ifdef CONFIG_CMD_GREPENV
1343 U_BOOT_CMD_COMPLETE(
1344 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1345 "search environment variables",
1347 "[-e] [-n | -v | -b] string ...\n"
1349 "[-n | -v | -b] string ...\n"
1351 " - list environment name=value pairs matching 'string'\n"
1353 " \"-e\": enable regular expressions;\n"
1355 " \"-n\": search variable names; \"-v\": search values;\n"
1356 " \"-b\": search both names and values (default)",
1361 U_BOOT_CMD_COMPLETE(
1362 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1363 "set environment variables",
1364 #if defined(CONFIG_CMD_NVEDIT_EFI)
1365 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1366 " [-i addr:size name], or [name [value ...]]\n"
1367 " - set UEFI variable 'name' to 'value' ...'\n"
1368 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1369 " \"-nv\": set non-volatile attribute\n"
1370 " \"-bs\": set boot-service attribute\n"
1371 " \"-rt\": set runtime attribute\n"
1372 " \"-at\": set time-based authentication attribute\n"
1373 " \"-a\": append-write\n"
1374 " \"-i addr,size\": use <addr,size> as variable's value\n"
1375 " \"-v\": verbose message\n"
1376 " - delete UEFI variable 'name' if 'value' not specified\n"
1378 "setenv [-f] name value ...\n"
1379 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1380 "setenv [-f] name\n"
1381 " - [forcibly] delete environment variable 'name'",
1385 #if defined(CONFIG_CMD_ASKENV)
1388 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1389 "get environment variables from stdin",
1390 "name [message] [size]\n"
1391 " - get environment variable 'name' from stdin (max 'size' chars)"
1395 #if defined(CONFIG_CMD_RUN)
1396 U_BOOT_CMD_COMPLETE(
1397 run, CONFIG_SYS_MAXARGS, 1, do_run,
1398 "run commands in an environment variable",
1400 " - run the commands in the environment variable(s) 'var'",
1404 #endif /* CONFIG_SPL_BUILD */