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;
47 * Maximum expected input data size for import command
49 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
52 * This variable is incremented on each do_env_set(), so it can
53 * be used via env_get_id() as an indication, if the environment
54 * has changed or not. So it is possible to reread an environment
55 * variable only if the environment was changed ... done so for
56 * example in NetInitLoop()
58 static int env_id = 1;
65 #ifndef CONFIG_SPL_BUILD
67 * Command interface: print one or all environment variables
69 * Returns 0 in case of error, or length of printed string
71 static int env_print(char *name, int flag)
76 if (name) { /* print a single name */
77 struct env_entry e, *ep;
81 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
84 len = printf("%s=%s\n", ep->key, ep->data);
88 /* print whole list */
89 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
97 /* should never happen */
98 printf("## Error: cannot export environment\n");
102 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
107 int env_flag = H_HIDE_DOT;
109 #if defined(CONFIG_CMD_NVEDIT_EFI)
110 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
111 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
114 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
117 env_flag &= ~H_HIDE_DOT;
121 /* print all env vars */
122 rcode = env_print(NULL, env_flag);
125 printf("\nEnvironment size: %d/%ld bytes\n",
126 rcode, (ulong)ENV_SIZE);
130 /* print selected env vars */
131 env_flag &= ~H_HIDE_DOT;
132 for (i = 1; i < argc; ++i) {
133 int rc = env_print(argv[i], env_flag);
135 printf("## Error: \"%s\" not defined\n", argv[i]);
143 #ifdef CONFIG_CMD_GREPENV
144 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
145 int argc, char *const argv[])
148 int len, grep_how, grep_what;
151 return CMD_RET_USAGE;
153 grep_how = H_MATCH_SUBSTR; /* default: substring search */
154 grep_what = H_MATCH_BOTH; /* default: grep names and values */
156 while (--argc > 0 && **++argv == '-') {
161 case 'e': /* use regex matching */
162 grep_how = H_MATCH_REGEX;
165 case 'n': /* grep for name */
166 grep_what = H_MATCH_KEY;
168 case 'v': /* grep for value */
169 grep_what = H_MATCH_DATA;
171 case 'b': /* grep for both */
172 grep_what = H_MATCH_BOTH;
177 return CMD_RET_USAGE;
183 len = hexport_r(&env_htab, '\n',
184 flag | grep_what | grep_how,
185 &res, 0, argc, argv);
198 #endif /* CONFIG_SPL_BUILD */
201 * Set a new environment variable,
202 * or replace or delete an existing one.
204 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
207 char *name, *value, *s;
208 struct env_entry e, *ep;
210 debug("Initial value for argc=%d\n", argc);
212 #if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
213 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
214 return do_env_set_efi(NULL, flag, --argc, ++argv);
217 while (argc > 1 && **(argv + 1) == '-') {
223 case 'f': /* force */
227 return CMD_RET_USAGE;
231 debug("Final value for argc=%d\n", argc);
234 if (strchr(name, '=')) {
235 printf("## Error: illegal character '='"
236 "in variable name \"%s\"\n", name);
243 if (argc < 3 || argv[2] == NULL) {
244 int rc = hdelete_r(name, &env_htab, env_flag);
246 /* If the variable didn't exist, don't report an error */
247 return rc && rc != -ENOENT ? 1 : 0;
251 * Insert / replace new value
253 for (i = 2, len = 0; i < argc; ++i)
254 len += strlen(argv[i]) + 1;
258 printf("## Can't malloc %d bytes\n", len);
261 for (i = 2, s = value; i < argc; ++i) {
264 while ((*s++ = *v++) != '\0')
273 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
276 printf("## Error inserting \"%s\" variable, errno=%d\n",
284 int env_set(const char *varname, const char *varvalue)
286 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
288 /* before import into hashtable */
289 if (!(gd->flags & GD_FLG_ENV_READY))
292 if (varvalue == NULL || varvalue[0] == '\0')
293 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
295 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
298 #ifndef CONFIG_SPL_BUILD
299 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
303 return CMD_RET_USAGE;
305 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
309 * Prompt for environment variable
311 #if defined(CONFIG_CMD_ASKENV)
312 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
314 char message[CONFIG_SYS_CBSIZE];
315 int i, len, pos, size;
319 local_args[0] = argv[0];
320 local_args[1] = argv[1];
321 local_args[2] = NULL;
322 local_args[3] = NULL;
327 * env_ask envname [message1 ...] [size]
330 return CMD_RET_USAGE;
333 * We test the last argument if it can be converted
334 * into a decimal number. If yes, we assume it's
335 * the size. Otherwise we echo it as part of the
338 i = dectoul(argv[argc - 1], &endptr);
339 if (*endptr != '\0') { /* no size */
340 size = CONFIG_SYS_CBSIZE - 1;
341 } else { /* size given */
347 sprintf(message, "Please enter '%s': ", argv[1]);
349 /* env_ask envname message1 ... messagen [size] */
350 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
352 message[pos++] = ' ';
354 strncpy(message + pos, argv[i], sizeof(message) - pos);
355 pos += strlen(argv[i]);
357 if (pos < sizeof(message) - 1) {
358 message[pos++] = ' ';
361 message[CONFIG_SYS_CBSIZE - 1] = '\0';
364 if (size >= CONFIG_SYS_CBSIZE)
365 size = CONFIG_SYS_CBSIZE - 1;
370 /* prompt for input */
371 len = cli_readline(message);
374 console_buffer[size] = '\0';
377 if (console_buffer[0] != '\0') {
378 local_args[2] = console_buffer;
382 /* Continue calling setenv code */
383 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
387 #if defined(CONFIG_CMD_ENV_CALLBACK)
388 static int print_static_binding(const char *var_name, const char *callback_name,
391 printf("\t%-20s %-20s\n", var_name, callback_name);
396 static int print_active_callback(struct env_entry *entry)
398 struct env_clbk_tbl *clbkp;
402 if (entry->callback == NULL)
405 /* look up the callback in the linker-list */
406 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
407 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
410 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
411 if (entry->callback == clbkp->callback + gd->reloc_off)
413 if (entry->callback == clbkp->callback)
418 if (i == num_callbacks)
419 /* this should probably never happen, but just in case... */
420 printf("\t%-20s %p\n", entry->key, entry->callback);
422 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
428 * Print the callbacks available and what they are bound to
430 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
433 struct env_clbk_tbl *clbkp;
437 /* Print the available callbacks */
438 puts("Available callbacks:\n");
439 puts("\tCallback Name\n");
440 puts("\t-------------\n");
441 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
442 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
445 printf("\t%s\n", clbkp->name);
448 /* Print the static bindings that may exist */
449 puts("Static callback bindings:\n");
450 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
451 printf("\t%-20s %-20s\n", "-------------", "-------------");
452 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
455 /* walk through each variable and print the callback if it has one */
456 puts("Active callback bindings:\n");
457 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
458 printf("\t%-20s %-20s\n", "-------------", "-------------");
459 hwalk_r(&env_htab, print_active_callback);
464 #if defined(CONFIG_CMD_ENV_FLAGS)
465 static int print_static_flags(const char *var_name, const char *flags,
468 enum env_flags_vartype type = env_flags_parse_vartype(flags);
469 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
471 printf("\t%-20s %-20s %-20s\n", var_name,
472 env_flags_get_vartype_name(type),
473 env_flags_get_varaccess_name(access));
478 static int print_active_flags(struct env_entry *entry)
480 enum env_flags_vartype type;
481 enum env_flags_varaccess access;
483 if (entry->flags == 0)
486 type = (enum env_flags_vartype)
487 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
488 access = env_flags_parse_varaccess_from_binflags(entry->flags);
489 printf("\t%-20s %-20s %-20s\n", entry->key,
490 env_flags_get_vartype_name(type),
491 env_flags_get_varaccess_name(access));
497 * Print the flags available and what variables have flags
499 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
501 /* Print the available variable types */
502 printf("Available variable type flags (position %d):\n",
503 ENV_FLAGS_VARTYPE_LOC);
504 puts("\tFlag\tVariable Type Name\n");
505 puts("\t----\t------------------\n");
506 env_flags_print_vartypes();
509 /* Print the available variable access types */
510 printf("Available variable access flags (position %d):\n",
511 ENV_FLAGS_VARACCESS_LOC);
512 puts("\tFlag\tVariable Access Name\n");
513 puts("\t----\t--------------------\n");
514 env_flags_print_varaccess();
517 /* Print the static flags that may exist */
518 puts("Static flags:\n");
519 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
521 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
523 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
526 /* walk through each variable and print the flags if non-default */
527 puts("Active flags:\n");
528 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
530 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
532 hwalk_r(&env_htab, print_active_flags);
538 * Interactively edit an environment variable
540 #if defined(CONFIG_CMD_EDITENV)
541 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
544 char buffer[CONFIG_SYS_CBSIZE];
548 return CMD_RET_USAGE;
550 /* before import into hashtable */
551 if (!(gd->flags & GD_FLG_ENV_READY))
554 /* Set read buffer to initial value or empty sting */
555 init_val = env_get(argv[1]);
557 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
561 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
564 if (buffer[0] == '\0') {
565 const char * const _argv[3] = { "setenv", argv[1], NULL };
567 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
569 const char * const _argv[4] = { "setenv", argv[1], buffer,
572 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
575 #endif /* CONFIG_CMD_EDITENV */
577 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
578 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
581 return env_save() ? 1 : 0;
585 saveenv, 1, 0, do_env_save,
586 "save environment variables to persistent storage",
590 #if defined(CONFIG_CMD_ERASEENV)
591 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
594 return env_erase() ? 1 : 0;
598 eraseenv, 1, 0, do_env_erase,
599 "erase environment variables from persistent storage",
605 #if defined(CONFIG_CMD_NVEDIT_LOAD)
606 static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
609 return env_reload() ? 1 : 0;
613 #if defined(CONFIG_CMD_NVEDIT_SELECT)
614 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
617 return env_select(argv[1]) ? 1 : 0;
621 #endif /* CONFIG_SPL_BUILD */
623 #ifndef CONFIG_SPL_BUILD
624 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
625 int argc, char *const argv[])
627 int all = 0, env_flag = H_INTERACTIVE;
629 debug("Initial value for argc=%d\n", argc);
630 while (--argc > 0 && **++argv == '-') {
635 case 'a': /* default all */
638 case 'f': /* force */
642 return cmd_usage(cmdtp);
646 debug("Final value for argc=%d\n", argc);
647 if (all && (argc == 0)) {
648 /* Reset the whole environment */
649 env_set_default("## Resetting to default environment\n",
653 if (!all && (argc > 0)) {
654 /* Reset individual variables */
655 env_set_default_vars(argc, argv, env_flag);
659 return cmd_usage(cmdtp);
662 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
663 int argc, char *const argv[])
665 int env_flag = H_INTERACTIVE;
668 debug("Initial value for argc=%d\n", argc);
669 while (argc > 1 && **(argv + 1) == '-') {
675 case 'f': /* force */
679 return CMD_RET_USAGE;
683 debug("Final value for argc=%d\n", argc);
688 char *name = *++argv;
690 if (hdelete_r(name, &env_htab, env_flag))
697 #ifdef CONFIG_CMD_EXPORTENV
699 * env export [-t | -b | -c] [-s size] addr [var ...]
700 * -t: export as text format; if size is given, data will be
701 * padded with '\0' bytes; if not, one terminating '\0'
702 * will be added (which is included in the "filesize"
703 * setting so you can for exmple copy this to flash and
704 * keep the termination).
705 * -b: export as binary format (name=value pairs separated by
706 * '\0', list end marked by double "\0\0")
707 * -c: export as checksum protected environment format as
708 * used for example by "saveenv" command
710 * size of output buffer
711 * addr: memory address where environment gets stored
712 * var... List of variable names that get included into the
713 * export. Without arguments, the whole environment gets
716 * With "-c" and size is NOT given, then the export command will
717 * format the data as currently used for the persistent storage,
718 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
719 * prepend a valid CRC32 checksum and, in case of redundant
720 * environment, a "current" redundancy flag. If size is given, this
721 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
722 * checksum and redundancy flag will be inserted.
724 * With "-b" and "-t", always only the real data (including a
725 * terminating '\0' byte) will be written; here the optional size
726 * argument will be used to make sure not to overflow the user
727 * provided buffer; the command will abort if the size is not
728 * sufficient. Any remaining space will be '\0' padded.
730 * On successful return, the variable "filesize" will be set.
731 * Note that filesize includes the trailing/terminating '\0' byte(s).
733 * Usage scenario: create a text snapshot/backup of the current settings:
735 * => env export -t 100000
736 * => era ${backup_addr} +${filesize}
737 * => cp.b 100000 ${backup_addr} ${filesize}
739 * Re-import this snapshot, deleting all other settings:
741 * => env import -d -t ${backup_addr}
743 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
744 int argc, char *const argv[])
748 char *ptr, *cmd, *res;
758 while (--argc > 0 && **++argv == '-') {
762 case 'b': /* raw binary format */
767 case 'c': /* external checksum format */
773 case 's': /* size given */
775 return cmd_usage(cmdtp);
776 size = hextoul(*++argv, NULL);
778 case 't': /* text format */
784 return CMD_RET_USAGE;
791 return CMD_RET_USAGE;
793 addr = hextoul(argv[0], NULL);
794 ptr = map_sysmem(addr, size);
797 memset(ptr, '\0', size);
802 if (sep) { /* export as text file */
803 len = hexport_r(&env_htab, sep,
804 H_MATCH_KEY | H_MATCH_IDENT,
805 &ptr, size, argc, argv);
807 pr_err("## Error: Cannot export environment: errno = %d\n",
811 sprintf(buf, "%zX", (size_t)len);
812 env_set("filesize", buf);
819 if (chk) /* export as checksum protected block */
820 res = (char *)envp->data;
821 else /* export as raw binary data */
824 len = hexport_r(&env_htab, '\0',
825 H_MATCH_KEY | H_MATCH_IDENT,
826 &res, ENV_SIZE, argc, argv);
828 pr_err("## Error: Cannot export environment: errno = %d\n",
834 envp->crc = crc32(0, envp->data,
835 size ? size - offsetof(env_t, data) : ENV_SIZE);
836 #ifdef CONFIG_ENV_ADDR_REDUND
837 envp->flags = ENV_REDUND_ACTIVE;
840 env_set_hex("filesize", len + offsetof(env_t, data));
845 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
851 #ifdef CONFIG_CMD_IMPORTENV
853 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
854 * -d: delete existing environment before importing if no var is
855 * passed; if vars are passed, if one var is in the current
856 * environment but not in the environment at addr, delete var from
857 * current environment;
858 * otherwise overwrite / append to existing definitions
859 * -t: assume text format; either "size" must be given or the
860 * text data must be '\0' terminated
861 * -r: handle CRLF like LF, that means exported variables with
862 * a content which ends with \r won't get imported. Used
863 * to import text files created with editors which are using CRLF
864 * for line endings. Only effective in addition to -t.
865 * -b: assume binary format ('\0' separated, "\0\0" terminated)
866 * -c: assume checksum protected environment format
867 * addr: memory address to read from
868 * size: length of input data; if missing, proper '\0'
869 * termination is mandatory
870 * if var is set and size should be missing (i.e. '\0'
871 * termination), set size to '-'
872 * var... List of the names of the only variables that get imported from
873 * the environment at address 'addr'. Without arguments, the whole
874 * environment gets imported.
876 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
877 int argc, char *const argv[])
891 while (--argc > 0 && **++argv == '-') {
895 case 'b': /* raw binary format */
900 case 'c': /* external checksum format */
906 case 't': /* text format */
911 case 'r': /* handle CRLF like LF */
918 return CMD_RET_USAGE;
924 return CMD_RET_USAGE;
927 printf("## Warning: defaulting to text format\n");
929 if (sep != '\n' && crlf_is_lf )
932 addr = hextoul(argv[0], NULL);
933 ptr = map_sysmem(addr, 0);
935 if (argc >= 2 && strcmp(argv[1], "-")) {
936 size = hextoul(argv[1], NULL);
938 puts("## Error: external checksum format must pass size\n");
939 return CMD_RET_FAILURE;
945 while (size < MAX_ENV_SIZE) {
946 if ((*s == sep) && (*(s+1) == '\0'))
951 if (size == MAX_ENV_SIZE) {
952 printf("## Warning: Input data exceeds %d bytes"
953 " - truncated\n", MAX_ENV_SIZE);
956 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
964 env_t *ep = (env_t *)ptr;
966 if (size <= offsetof(env_t, data)) {
967 printf("## Error: Invalid size 0x%zX\n", size);
971 size -= offsetof(env_t, data);
972 memcpy(&crc, &ep->crc, sizeof(crc));
974 if (crc32(0, ep->data, size) != crc) {
975 puts("## Error: bad CRC, import failed\n");
978 ptr = (char *)ep->data;
981 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
982 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
983 pr_err("## Error: Environment import failed: errno = %d\n",
987 gd->flags |= GD_FLG_ENV_READY;
992 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
998 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
999 static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
1000 int argc, char *const argv[])
1003 char *from = argv[2];
1004 char *default_value = NULL;
1008 if (argc < 3 || argc > 4) {
1009 return CMD_RET_USAGE;
1013 default_value = argv[3];
1016 val = env_get(from) ?: default_value;
1018 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1020 return CMD_RET_FAILURE;
1023 ret = env_set(to, val);
1026 return CMD_RET_SUCCESS;
1029 return CMD_RET_FAILURE;
1034 #if defined(CONFIG_CMD_NVEDIT_INFO)
1036 * print_env_info - print environment information
1038 static int print_env_info(void)
1042 /* print environment validity value */
1043 switch (gd->env_valid) {
1051 value = "redundant";
1057 printf("env_valid = %s\n", value);
1059 /* print environment ready flag */
1060 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1061 printf("env_ready = %s\n", value);
1063 /* print environment using default flag */
1064 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1065 printf("env_use_default = %s\n", value);
1067 return CMD_RET_SUCCESS;
1070 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1071 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1074 * env info - display environment information
1075 * env info [-d] - evaluate whether default environment is used
1076 * env info [-p] - evaluate whether environment can be persisted
1077 * Add [-q] - quiet mode, use only for command result, for test by example:
1078 * test env info -p -d -q
1080 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1081 int argc, char *const argv[])
1084 int eval_results = 0;
1086 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1087 enum env_location loc;
1090 /* display environment information */
1092 return print_env_info();
1094 /* process options */
1095 while (--argc > 0 && **++argv == '-') {
1101 eval_flags |= ENV_INFO_IS_DEFAULT;
1104 eval_flags |= ENV_INFO_IS_PERSISTED;
1110 return CMD_RET_USAGE;
1115 /* evaluate whether default environment is used */
1116 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1117 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1119 printf("Default environment is used\n");
1120 eval_results |= ENV_INFO_IS_DEFAULT;
1123 printf("Environment was loaded from persistent storage\n");
1127 /* evaluate whether environment can be persisted */
1128 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1129 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1130 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1131 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1133 printf("Environment can be persisted\n");
1134 eval_results |= ENV_INFO_IS_PERSISTED;
1137 printf("Environment cannot be persisted\n");
1141 printf("Environment cannot be persisted\n");
1145 /* The result of evaluations is combined with AND */
1146 if (eval_flags != eval_results)
1147 return CMD_RET_FAILURE;
1149 return CMD_RET_SUCCESS;
1153 #if defined(CONFIG_CMD_ENV_EXISTS)
1154 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1157 struct env_entry e, *ep;
1160 return CMD_RET_USAGE;
1164 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1166 return (ep == NULL) ? 1 : 0;
1171 * New command line interface: "env" command with subcommands
1173 static struct cmd_tbl cmd_env_sub[] = {
1174 #if defined(CONFIG_CMD_ASKENV)
1175 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1177 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1178 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1179 #if defined(CONFIG_CMD_EDITENV)
1180 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1182 #if defined(CONFIG_CMD_ENV_CALLBACK)
1183 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1185 #if defined(CONFIG_CMD_ENV_FLAGS)
1186 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1188 #if defined(CONFIG_CMD_EXPORTENV)
1189 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1191 #if defined(CONFIG_CMD_GREPENV)
1192 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1194 #if defined(CONFIG_CMD_IMPORTENV)
1195 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1197 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1198 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1200 #if defined(CONFIG_CMD_NVEDIT_INFO)
1201 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
1203 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1204 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1206 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1207 #if defined(CONFIG_CMD_RUN)
1208 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1210 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1211 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1212 #if defined(CONFIG_CMD_ERASEENV)
1213 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1216 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1217 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1219 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1220 #if defined(CONFIG_CMD_ENV_EXISTS)
1221 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1225 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1226 void env_reloc(void)
1228 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1232 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1237 return CMD_RET_USAGE;
1239 /* drop initial "env" arg */
1243 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1246 return cp->cmd(cmdtp, flag, argc, argv);
1248 return CMD_RET_USAGE;
1251 #ifdef CONFIG_SYS_LONGHELP
1252 static char env_help_text[] =
1253 #if defined(CONFIG_CMD_ASKENV)
1254 "ask name [message] [size] - ask for environment variable\nenv "
1256 #if defined(CONFIG_CMD_ENV_CALLBACK)
1257 "callbacks - print callbacks and their associated variables\nenv "
1259 "default [-f] -a - [forcibly] reset default environment\n"
1260 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1261 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1262 #if defined(CONFIG_CMD_EDITENV)
1263 "env edit name - edit environment variable\n"
1265 #if defined(CONFIG_CMD_ENV_EXISTS)
1266 "env exists name - tests for existence of variable\n"
1268 #if defined(CONFIG_CMD_EXPORTENV)
1269 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1271 #if defined(CONFIG_CMD_ENV_FLAGS)
1272 "env flags - print variables that have non-default flags\n"
1274 #if defined(CONFIG_CMD_GREPENV)
1276 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1278 "env grep [-n | -v | -b] string [...] - search environment\n"
1281 #if defined(CONFIG_CMD_IMPORTENV)
1282 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1284 #if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1285 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1287 #if defined(CONFIG_CMD_NVEDIT_INFO)
1288 "env info - display environment information\n"
1289 "env info [-d] [-p] [-q] - evaluate environment information\n"
1290 " \"-d\": default environment is used\n"
1291 " \"-p\": environment can be persisted\n"
1292 " \"-q\": quiet output\n"
1294 "env print [-a | name ...] - print environment\n"
1295 #if defined(CONFIG_CMD_NVEDIT_EFI)
1296 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
1298 #if defined(CONFIG_CMD_RUN)
1299 "env run var [...] - run commands in an environment variable\n"
1301 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
1302 "env save - save environment\n"
1303 #if defined(CONFIG_CMD_ERASEENV)
1304 "env erase - erase environment\n"
1307 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1308 "env load - load environment\n"
1310 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1311 "env select [target] - select environment target\n"
1313 #if defined(CONFIG_CMD_NVEDIT_EFI)
1314 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
1315 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1317 "env set [-f] name [arg ...]\n";
1321 env, CONFIG_SYS_MAXARGS, 1, do_env,
1322 "environment handling commands", env_help_text
1326 * Old command line interface, kept for compatibility
1329 #if defined(CONFIG_CMD_EDITENV)
1330 U_BOOT_CMD_COMPLETE(
1331 editenv, 2, 0, do_env_edit,
1332 "edit environment variable",
1334 " - edit environment variable 'name'",
1339 U_BOOT_CMD_COMPLETE(
1340 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1341 "print environment variables",
1342 "[-a]\n - print [all] values of all environment variables\n"
1343 #if defined(CONFIG_CMD_NVEDIT_EFI)
1344 "printenv -e [-guid guid][-n] [name ...]\n"
1345 " - print UEFI variable 'name' or all the variables\n"
1346 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1347 " \"-n\": suppress dumping variable's value\n"
1349 "printenv name ...\n"
1350 " - print value of environment variable 'name'",
1354 #ifdef CONFIG_CMD_GREPENV
1355 U_BOOT_CMD_COMPLETE(
1356 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1357 "search environment variables",
1359 "[-e] [-n | -v | -b] string ...\n"
1361 "[-n | -v | -b] string ...\n"
1363 " - list environment name=value pairs matching 'string'\n"
1365 " \"-e\": enable regular expressions;\n"
1367 " \"-n\": search variable names; \"-v\": search values;\n"
1368 " \"-b\": search both names and values (default)",
1373 U_BOOT_CMD_COMPLETE(
1374 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1375 "set environment variables",
1376 #if defined(CONFIG_CMD_NVEDIT_EFI)
1377 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1378 " [-i addr:size name], or [name [value ...]]\n"
1379 " - set UEFI variable 'name' to 'value' ...'\n"
1380 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1381 " \"-nv\": set non-volatile attribute\n"
1382 " \"-bs\": set boot-service attribute\n"
1383 " \"-rt\": set runtime attribute\n"
1384 " \"-at\": set time-based authentication attribute\n"
1385 " \"-a\": append-write\n"
1386 " \"-i addr,size\": use <addr,size> as variable's value\n"
1387 " \"-v\": verbose message\n"
1388 " - delete UEFI variable 'name' if 'value' not specified\n"
1390 "setenv [-f] name value ...\n"
1391 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1392 "setenv [-f] name\n"
1393 " - [forcibly] delete environment variable 'name'",
1397 #if defined(CONFIG_CMD_ASKENV)
1400 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1401 "get environment variables from stdin",
1402 "name [message] [size]\n"
1403 " - get environment variable 'name' from stdin (max 'size' chars)"
1407 #if defined(CONFIG_CMD_RUN)
1408 U_BOOT_CMD_COMPLETE(
1409 run, CONFIG_SYS_MAXARGS, 1, do_run,
1410 "run commands in an environment variable",
1412 " - run the commands in the environment variable(s) 'var'",
1416 #endif /* CONFIG_SPL_BUILD */