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>
36 #include <u-boot/crc.h>
38 #include <linux/stddef.h>
39 #include <asm/byteorder.h>
42 DECLARE_GLOBAL_DATA_PTR;
44 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
45 defined(CONFIG_ENV_IS_IN_FLASH) || \
46 defined(CONFIG_ENV_IS_IN_MMC) || \
47 defined(CONFIG_ENV_IS_IN_FAT) || \
48 defined(CONFIG_ENV_IS_IN_EXT4) || \
49 defined(CONFIG_ENV_IS_IN_NAND) || \
50 defined(CONFIG_ENV_IS_IN_NVRAM) || \
51 defined(CONFIG_ENV_IS_IN_ONENAND) || \
52 defined(CONFIG_ENV_IS_IN_SATA) || \
53 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
54 defined(CONFIG_ENV_IS_IN_REMOTE) || \
55 defined(CONFIG_ENV_IS_IN_UBI)
57 #define ENV_IS_IN_DEVICE
61 #if !defined(ENV_IS_IN_DEVICE) && \
62 !defined(CONFIG_ENV_IS_NOWHERE)
63 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
64 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
68 * Maximum expected input data size for import command
70 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
73 * This variable is incremented on each do_env_set(), so it can
74 * be used via env_get_id() as an indication, if the environment
75 * has changed or not. So it is possible to reread an environment
76 * variable only if the environment was changed ... done so for
77 * example in NetInitLoop()
79 static int env_id = 1;
86 #ifndef CONFIG_SPL_BUILD
88 * Command interface: print one or all environment variables
90 * Returns 0 in case of error, or length of printed string
92 static int env_print(char *name, int flag)
97 if (name) { /* print a single name */
98 struct env_entry e, *ep;
102 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
105 len = printf("%s=%s\n", ep->key, ep->data);
109 /* print whole list */
110 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
118 /* should never happen */
119 printf("## Error: cannot export environment\n");
123 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
128 int env_flag = H_HIDE_DOT;
130 #if defined(CONFIG_CMD_NVEDIT_EFI)
131 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
132 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
135 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
138 env_flag &= ~H_HIDE_DOT;
142 /* print all env vars */
143 rcode = env_print(NULL, env_flag);
146 printf("\nEnvironment size: %d/%ld bytes\n",
147 rcode, (ulong)ENV_SIZE);
151 /* print selected env vars */
152 env_flag &= ~H_HIDE_DOT;
153 for (i = 1; i < argc; ++i) {
154 int rc = env_print(argv[i], env_flag);
156 printf("## Error: \"%s\" not defined\n", argv[i]);
164 #ifdef CONFIG_CMD_GREPENV
165 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
166 int argc, char * const argv[])
169 int len, grep_how, grep_what;
172 return CMD_RET_USAGE;
174 grep_how = H_MATCH_SUBSTR; /* default: substring search */
175 grep_what = H_MATCH_BOTH; /* default: grep names and values */
177 while (--argc > 0 && **++argv == '-') {
182 case 'e': /* use regex matching */
183 grep_how = H_MATCH_REGEX;
186 case 'n': /* grep for name */
187 grep_what = H_MATCH_KEY;
189 case 'v': /* grep for value */
190 grep_what = H_MATCH_DATA;
192 case 'b': /* grep for both */
193 grep_what = H_MATCH_BOTH;
198 return CMD_RET_USAGE;
204 len = hexport_r(&env_htab, '\n',
205 flag | grep_what | grep_how,
206 &res, 0, argc, argv);
219 #endif /* CONFIG_SPL_BUILD */
222 * Set a new environment variable,
223 * or replace or delete an existing one.
225 static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
228 char *name, *value, *s;
229 struct env_entry e, *ep;
231 debug("Initial value for argc=%d\n", argc);
233 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
234 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
235 return do_env_set_efi(NULL, flag, --argc, ++argv);
238 while (argc > 1 && **(argv + 1) == '-') {
244 case 'f': /* force */
248 return CMD_RET_USAGE;
252 debug("Final value for argc=%d\n", argc);
255 if (strchr(name, '=')) {
256 printf("## Error: illegal character '='"
257 "in variable name \"%s\"\n", name);
264 if (argc < 3 || argv[2] == NULL) {
265 int rc = hdelete_r(name, &env_htab, env_flag);
270 * Insert / replace new value
272 for (i = 2, len = 0; i < argc; ++i)
273 len += strlen(argv[i]) + 1;
277 printf("## Can't malloc %d bytes\n", len);
280 for (i = 2, s = value; i < argc; ++i) {
283 while ((*s++ = *v++) != '\0')
292 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
295 printf("## Error inserting \"%s\" variable, errno=%d\n",
303 int env_set(const char *varname, const char *varvalue)
305 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
307 /* before import into hashtable */
308 if (!(gd->flags & GD_FLG_ENV_READY))
311 if (varvalue == NULL || varvalue[0] == '\0')
312 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
314 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
318 * Set an environment variable to an integer value
320 * @param varname Environment variable to set
321 * @param value Value to set it to
322 * @return 0 if ok, 1 on error
324 int env_set_ulong(const char *varname, ulong value)
326 /* TODO: this should be unsigned */
327 char *str = simple_itoa(value);
329 return env_set(varname, str);
333 * Set an environment variable to an value in hex
335 * @param varname Environment variable to set
336 * @param value Value to set it to
337 * @return 0 if ok, 1 on error
339 int env_set_hex(const char *varname, ulong value)
343 sprintf(str, "%lx", value);
344 return env_set(varname, str);
347 ulong env_get_hex(const char *varname, ulong default_val)
353 s = env_get(varname);
355 value = simple_strtoul(s, &endp, 16);
362 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
364 string_to_enetaddr(env_get(name), enetaddr);
365 return is_valid_ethaddr(enetaddr);
368 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
370 char buf[ARP_HLEN_ASCII + 1];
372 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
375 sprintf(buf, "%pM", enetaddr);
377 return env_set(name, buf);
380 #ifndef CONFIG_SPL_BUILD
381 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
384 return CMD_RET_USAGE;
386 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
390 * Prompt for environment variable
392 #if defined(CONFIG_CMD_ASKENV)
393 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
395 char message[CONFIG_SYS_CBSIZE];
396 int i, len, pos, size;
400 local_args[0] = argv[0];
401 local_args[1] = argv[1];
402 local_args[2] = NULL;
403 local_args[3] = NULL;
408 * env_ask envname [message1 ...] [size]
411 return CMD_RET_USAGE;
414 * We test the last argument if it can be converted
415 * into a decimal number. If yes, we assume it's
416 * the size. Otherwise we echo it as part of the
419 i = simple_strtoul(argv[argc - 1], &endptr, 10);
420 if (*endptr != '\0') { /* no size */
421 size = CONFIG_SYS_CBSIZE - 1;
422 } else { /* size given */
428 sprintf(message, "Please enter '%s': ", argv[1]);
430 /* env_ask envname message1 ... messagen [size] */
431 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
433 message[pos++] = ' ';
435 strncpy(message + pos, argv[i], sizeof(message) - pos);
436 pos += strlen(argv[i]);
438 if (pos < sizeof(message) - 1) {
439 message[pos++] = ' ';
442 message[CONFIG_SYS_CBSIZE - 1] = '\0';
445 if (size >= CONFIG_SYS_CBSIZE)
446 size = CONFIG_SYS_CBSIZE - 1;
451 /* prompt for input */
452 len = cli_readline(message);
455 console_buffer[size] = '\0';
458 if (console_buffer[0] != '\0') {
459 local_args[2] = console_buffer;
463 /* Continue calling setenv code */
464 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
468 #if defined(CONFIG_CMD_ENV_CALLBACK)
469 static int print_static_binding(const char *var_name, const char *callback_name,
472 printf("\t%-20s %-20s\n", var_name, callback_name);
477 static int print_active_callback(struct env_entry *entry)
479 struct env_clbk_tbl *clbkp;
483 if (entry->callback == NULL)
486 /* look up the callback in the linker-list */
487 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
488 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
491 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
492 if (entry->callback == clbkp->callback + gd->reloc_off)
494 if (entry->callback == clbkp->callback)
499 if (i == num_callbacks)
500 /* this should probably never happen, but just in case... */
501 printf("\t%-20s %p\n", entry->key, entry->callback);
503 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
509 * Print the callbacks available and what they are bound to
511 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
513 struct env_clbk_tbl *clbkp;
517 /* Print the available callbacks */
518 puts("Available callbacks:\n");
519 puts("\tCallback Name\n");
520 puts("\t-------------\n");
521 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
522 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
525 printf("\t%s\n", clbkp->name);
528 /* Print the static bindings that may exist */
529 puts("Static callback bindings:\n");
530 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
531 printf("\t%-20s %-20s\n", "-------------", "-------------");
532 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
535 /* walk through each variable and print the callback if it has one */
536 puts("Active callback bindings:\n");
537 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
538 printf("\t%-20s %-20s\n", "-------------", "-------------");
539 hwalk_r(&env_htab, print_active_callback);
544 #if defined(CONFIG_CMD_ENV_FLAGS)
545 static int print_static_flags(const char *var_name, const char *flags,
548 enum env_flags_vartype type = env_flags_parse_vartype(flags);
549 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
551 printf("\t%-20s %-20s %-20s\n", var_name,
552 env_flags_get_vartype_name(type),
553 env_flags_get_varaccess_name(access));
558 static int print_active_flags(struct env_entry *entry)
560 enum env_flags_vartype type;
561 enum env_flags_varaccess access;
563 if (entry->flags == 0)
566 type = (enum env_flags_vartype)
567 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
568 access = env_flags_parse_varaccess_from_binflags(entry->flags);
569 printf("\t%-20s %-20s %-20s\n", entry->key,
570 env_flags_get_vartype_name(type),
571 env_flags_get_varaccess_name(access));
577 * Print the flags available and what variables have flags
579 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
581 /* Print the available variable types */
582 printf("Available variable type flags (position %d):\n",
583 ENV_FLAGS_VARTYPE_LOC);
584 puts("\tFlag\tVariable Type Name\n");
585 puts("\t----\t------------------\n");
586 env_flags_print_vartypes();
589 /* Print the available variable access types */
590 printf("Available variable access flags (position %d):\n",
591 ENV_FLAGS_VARACCESS_LOC);
592 puts("\tFlag\tVariable Access Name\n");
593 puts("\t----\t--------------------\n");
594 env_flags_print_varaccess();
597 /* Print the static flags that may exist */
598 puts("Static flags:\n");
599 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
601 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
603 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
606 /* walk through each variable and print the flags if non-default */
607 puts("Active flags:\n");
608 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
610 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
612 hwalk_r(&env_htab, print_active_flags);
618 * Interactively edit an environment variable
620 #if defined(CONFIG_CMD_EDITENV)
621 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
624 char buffer[CONFIG_SYS_CBSIZE];
628 return CMD_RET_USAGE;
630 /* before import into hashtable */
631 if (!(gd->flags & GD_FLG_ENV_READY))
634 /* Set read buffer to initial value or empty sting */
635 init_val = env_get(argv[1]);
637 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
641 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
644 if (buffer[0] == '\0') {
645 const char * const _argv[3] = { "setenv", argv[1], NULL };
647 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
649 const char * const _argv[4] = { "setenv", argv[1], buffer,
652 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
655 #endif /* CONFIG_CMD_EDITENV */
656 #endif /* CONFIG_SPL_BUILD */
659 * Look up variable from environment,
660 * return address of storage for that variable,
661 * or NULL if not found
663 char *env_get(const char *name)
665 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
666 struct env_entry e, *ep;
672 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
674 return ep ? ep->data : NULL;
677 /* restricted capabilities before import */
678 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
679 return (char *)(gd->env_buf);
685 * Like env_get, but prints an error if envvar isn't defined in the
686 * environment. It always returns what env_get does, so it can be used in
687 * place of env_get without changing error handling otherwise.
689 char *from_env(const char *envvar)
693 ret = env_get(envvar);
696 printf("missing environment variable: %s\n", envvar);
702 * Look up variable from environment for restricted C runtime env.
704 int env_get_f(const char *name, char *buf, unsigned len)
708 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
711 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
714 if (nxt >= CONFIG_ENV_SIZE)
718 val = env_match((uchar *)name, i);
722 /* found; copy out */
723 for (n = 0; n < len; ++n, ++buf) {
724 c = env_get_char(val++);
735 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
745 * Decode the integer value of an environment variable and return it.
747 * @param name Name of environment variable
748 * @param base Number base to use (normally 10, or 16 for hex)
749 * @param default_val Default value to return if the variable is not
751 * @return the decoded value, or default_val if not found
753 ulong env_get_ulong(const char *name, int base, ulong default_val)
756 * We can use env_get() here, even before relocation, since the
757 * environment variable value is an integer and thus short.
759 const char *str = env_get(name);
761 return str ? simple_strtoul(str, NULL, base) : default_val;
764 #ifndef CONFIG_SPL_BUILD
765 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
766 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
769 return env_save() ? 1 : 0;
773 saveenv, 1, 0, do_env_save,
774 "save environment variables to persistent storage",
778 #if defined(CONFIG_CMD_ERASEENV)
779 static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
782 return env_erase() ? 1 : 0;
786 eraseenv, 1, 0, do_env_erase,
787 "erase environment variables from persistent storage",
792 #endif /* CONFIG_SPL_BUILD */
794 int env_match(uchar *s1, int i2)
799 while (*s1 == env_get_char(i2++))
803 if (*s1 == '\0' && env_get_char(i2-1) == '=')
809 #ifndef CONFIG_SPL_BUILD
810 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
811 int argc, char * const argv[])
813 int all = 0, env_flag = H_INTERACTIVE;
815 debug("Initial value for argc=%d\n", argc);
816 while (--argc > 0 && **++argv == '-') {
821 case 'a': /* default all */
824 case 'f': /* force */
828 return cmd_usage(cmdtp);
832 debug("Final value for argc=%d\n", argc);
833 if (all && (argc == 0)) {
834 /* Reset the whole environment */
835 env_set_default("## Resetting to default environment\n",
839 if (!all && (argc > 0)) {
840 /* Reset individual variables */
841 env_set_default_vars(argc, argv, env_flag);
845 return cmd_usage(cmdtp);
848 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
849 int argc, char * const argv[])
851 int env_flag = H_INTERACTIVE;
854 debug("Initial value for argc=%d\n", argc);
855 while (argc > 1 && **(argv + 1) == '-') {
861 case 'f': /* force */
865 return CMD_RET_USAGE;
869 debug("Final value for argc=%d\n", argc);
874 char *name = *++argv;
876 if (!hdelete_r(name, &env_htab, env_flag))
883 #ifdef CONFIG_CMD_EXPORTENV
885 * env export [-t | -b | -c] [-s size] addr [var ...]
886 * -t: export as text format; if size is given, data will be
887 * padded with '\0' bytes; if not, one terminating '\0'
888 * will be added (which is included in the "filesize"
889 * setting so you can for exmple copy this to flash and
890 * keep the termination).
891 * -b: export as binary format (name=value pairs separated by
892 * '\0', list end marked by double "\0\0")
893 * -c: export as checksum protected environment format as
894 * used for example by "saveenv" command
896 * size of output buffer
897 * addr: memory address where environment gets stored
898 * var... List of variable names that get included into the
899 * export. Without arguments, the whole environment gets
902 * With "-c" and size is NOT given, then the export command will
903 * format the data as currently used for the persistent storage,
904 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
905 * prepend a valid CRC32 checksum and, in case of redundant
906 * environment, a "current" redundancy flag. If size is given, this
907 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
908 * checksum and redundancy flag will be inserted.
910 * With "-b" and "-t", always only the real data (including a
911 * terminating '\0' byte) will be written; here the optional size
912 * argument will be used to make sure not to overflow the user
913 * provided buffer; the command will abort if the size is not
914 * sufficient. Any remaining space will be '\0' padded.
916 * On successful return, the variable "filesize" will be set.
917 * Note that filesize includes the trailing/terminating '\0' byte(s).
919 * Usage scenario: create a text snapshot/backup of the current settings:
921 * => env export -t 100000
922 * => era ${backup_addr} +${filesize}
923 * => cp.b 100000 ${backup_addr} ${filesize}
925 * Re-import this snapshot, deleting all other settings:
927 * => env import -d -t ${backup_addr}
929 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
930 int argc, char * const argv[])
934 char *ptr, *cmd, *res;
944 while (--argc > 0 && **++argv == '-') {
948 case 'b': /* raw binary format */
953 case 'c': /* external checksum format */
959 case 's': /* size given */
961 return cmd_usage(cmdtp);
962 size = simple_strtoul(*++argv, NULL, 16);
964 case 't': /* text format */
970 return CMD_RET_USAGE;
977 return CMD_RET_USAGE;
979 addr = simple_strtoul(argv[0], NULL, 16);
980 ptr = map_sysmem(addr, size);
983 memset(ptr, '\0', size);
988 if (sep) { /* export as text file */
989 len = hexport_r(&env_htab, sep,
990 H_MATCH_KEY | H_MATCH_IDENT,
991 &ptr, size, argc, argv);
993 pr_err("## Error: Cannot export environment: errno = %d\n",
997 sprintf(buf, "%zX", (size_t)len);
998 env_set("filesize", buf);
1003 envp = (env_t *)ptr;
1005 if (chk) /* export as checksum protected block */
1006 res = (char *)envp->data;
1007 else /* export as raw binary data */
1010 len = hexport_r(&env_htab, '\0',
1011 H_MATCH_KEY | H_MATCH_IDENT,
1012 &res, ENV_SIZE, argc, argv);
1014 pr_err("## Error: Cannot export environment: errno = %d\n",
1020 envp->crc = crc32(0, envp->data,
1021 size ? size - offsetof(env_t, data) : ENV_SIZE);
1022 #ifdef CONFIG_ENV_ADDR_REDUND
1023 envp->flags = ENV_REDUND_ACTIVE;
1026 env_set_hex("filesize", len + offsetof(env_t, data));
1031 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1037 #ifdef CONFIG_CMD_IMPORTENV
1039 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1040 * -d: delete existing environment before importing if no var is
1041 * passed; if vars are passed, if one var is in the current
1042 * environment but not in the environment at addr, delete var from
1043 * current environment;
1044 * otherwise overwrite / append to existing definitions
1045 * -t: assume text format; either "size" must be given or the
1046 * text data must be '\0' terminated
1047 * -r: handle CRLF like LF, that means exported variables with
1048 * a content which ends with \r won't get imported. Used
1049 * to import text files created with editors which are using CRLF
1050 * for line endings. Only effective in addition to -t.
1051 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1052 * -c: assume checksum protected environment format
1053 * addr: memory address to read from
1054 * size: length of input data; if missing, proper '\0'
1055 * termination is mandatory
1056 * if var is set and size should be missing (i.e. '\0'
1057 * termination), set size to '-'
1058 * var... List of the names of the only variables that get imported from
1059 * the environment at address 'addr'. Without arguments, the whole
1060 * environment gets imported.
1062 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1063 int argc, char * const argv[])
1077 while (--argc > 0 && **++argv == '-') {
1081 case 'b': /* raw binary format */
1086 case 'c': /* external checksum format */
1092 case 't': /* text format */
1097 case 'r': /* handle CRLF like LF */
1104 return CMD_RET_USAGE;
1110 return CMD_RET_USAGE;
1113 printf("## Warning: defaulting to text format\n");
1115 if (sep != '\n' && crlf_is_lf )
1118 addr = simple_strtoul(argv[0], NULL, 16);
1119 ptr = map_sysmem(addr, 0);
1121 if (argc >= 2 && strcmp(argv[1], "-")) {
1122 size = simple_strtoul(argv[1], NULL, 16);
1124 puts("## Error: external checksum format must pass size\n");
1125 return CMD_RET_FAILURE;
1131 while (size < MAX_ENV_SIZE) {
1132 if ((*s == sep) && (*(s+1) == '\0'))
1137 if (size == MAX_ENV_SIZE) {
1138 printf("## Warning: Input data exceeds %d bytes"
1139 " - truncated\n", MAX_ENV_SIZE);
1142 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1150 env_t *ep = (env_t *)ptr;
1152 size -= offsetof(env_t, data);
1153 memcpy(&crc, &ep->crc, sizeof(crc));
1155 if (crc32(0, ep->data, size) != crc) {
1156 puts("## Error: bad CRC, import failed\n");
1159 ptr = (char *)ep->data;
1162 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1163 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1164 pr_err("## Error: Environment import failed: errno = %d\n",
1168 gd->flags |= GD_FLG_ENV_READY;
1173 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1179 #if defined(CONFIG_CMD_NVEDIT_INFO)
1181 * print_env_info - print environment information
1183 static int print_env_info(void)
1187 /* print environment validity value */
1188 switch (gd->env_valid) {
1196 value = "redundant";
1202 printf("env_valid = %s\n", value);
1204 /* print environment ready flag */
1205 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1206 printf("env_ready = %s\n", value);
1208 /* print environment using default flag */
1209 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1210 printf("env_use_default = %s\n", value);
1212 return CMD_RET_SUCCESS;
1215 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1216 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1219 * env info - display environment information
1220 * env info [-d] - evaluate whether default environment is used
1221 * env info [-p] - evaluate whether environment can be persisted
1223 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1224 int argc, char * const argv[])
1227 int eval_results = 0;
1229 /* display environment information */
1231 return print_env_info();
1233 /* process options */
1234 while (--argc > 0 && **++argv == '-') {
1240 eval_flags |= ENV_INFO_IS_DEFAULT;
1243 eval_flags |= ENV_INFO_IS_PERSISTED;
1246 return CMD_RET_USAGE;
1251 /* evaluate whether default environment is used */
1252 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1253 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1254 printf("Default environment is used\n");
1255 eval_results |= ENV_INFO_IS_DEFAULT;
1257 printf("Environment was loaded from persistent storage\n");
1261 /* evaluate whether environment can be persisted */
1262 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1263 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1264 printf("Environment can be persisted\n");
1265 eval_results |= ENV_INFO_IS_PERSISTED;
1267 printf("Environment cannot be persisted\n");
1271 /* The result of evaluations is combined with AND */
1272 if (eval_flags != eval_results)
1273 return CMD_RET_FAILURE;
1275 return CMD_RET_SUCCESS;
1279 #if defined(CONFIG_CMD_ENV_EXISTS)
1280 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1281 char * const argv[])
1283 struct env_entry e, *ep;
1286 return CMD_RET_USAGE;
1290 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1292 return (ep == NULL) ? 1 : 0;
1297 * New command line interface: "env" command with subcommands
1299 static cmd_tbl_t cmd_env_sub[] = {
1300 #if defined(CONFIG_CMD_ASKENV)
1301 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1303 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1304 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1305 #if defined(CONFIG_CMD_EDITENV)
1306 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1308 #if defined(CONFIG_CMD_ENV_CALLBACK)
1309 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1311 #if defined(CONFIG_CMD_ENV_FLAGS)
1312 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1314 #if defined(CONFIG_CMD_EXPORTENV)
1315 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1317 #if defined(CONFIG_CMD_GREPENV)
1318 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1320 #if defined(CONFIG_CMD_IMPORTENV)
1321 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1323 #if defined(CONFIG_CMD_NVEDIT_INFO)
1324 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1326 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1327 #if defined(CONFIG_CMD_RUN)
1328 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1330 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1331 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1332 #if defined(CONFIG_CMD_ERASEENV)
1333 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1336 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1337 #if defined(CONFIG_CMD_ENV_EXISTS)
1338 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1342 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1343 void env_reloc(void)
1345 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1349 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1354 return CMD_RET_USAGE;
1356 /* drop initial "env" arg */
1360 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1363 return cp->cmd(cmdtp, flag, argc, argv);
1365 return CMD_RET_USAGE;
1368 #ifdef CONFIG_SYS_LONGHELP
1369 static char env_help_text[] =
1370 #if defined(CONFIG_CMD_ASKENV)
1371 "ask name [message] [size] - ask for environment variable\nenv "
1373 #if defined(CONFIG_CMD_ENV_CALLBACK)
1374 "callbacks - print callbacks and their associated variables\nenv "
1376 "default [-f] -a - [forcibly] reset default environment\n"
1377 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1378 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1379 #if defined(CONFIG_CMD_EDITENV)
1380 "env edit name - edit environment variable\n"
1382 #if defined(CONFIG_CMD_ENV_EXISTS)
1383 "env exists name - tests for existence of variable\n"
1385 #if defined(CONFIG_CMD_EXPORTENV)
1386 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1388 #if defined(CONFIG_CMD_ENV_FLAGS)
1389 "env flags - print variables that have non-default flags\n"
1391 #if defined(CONFIG_CMD_GREPENV)
1393 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1395 "env grep [-n | -v | -b] string [...] - search environment\n"
1398 #if defined(CONFIG_CMD_IMPORTENV)
1399 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1401 #if defined(CONFIG_CMD_NVEDIT_INFO)
1402 "env info - display environment information\n"
1403 "env info [-d] - whether default environment is used\n"
1404 "env info [-p] - whether environment can be persisted\n"
1406 "env print [-a | name ...] - print environment\n"
1407 #if defined(CONFIG_CMD_NVEDIT_EFI)
1408 "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
1410 #if defined(CONFIG_CMD_RUN)
1411 "env run var [...] - run commands in an environment variable\n"
1413 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1414 "env save - save environment\n"
1415 #if defined(CONFIG_CMD_ERASEENV)
1416 "env erase - erase environment\n"
1419 #if defined(CONFIG_CMD_NVEDIT_EFI)
1420 "env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n"
1421 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1423 "env set [-f] name [arg ...]\n";
1427 env, CONFIG_SYS_MAXARGS, 1, do_env,
1428 "environment handling commands", env_help_text
1432 * Old command line interface, kept for compatibility
1435 #if defined(CONFIG_CMD_EDITENV)
1436 U_BOOT_CMD_COMPLETE(
1437 editenv, 2, 0, do_env_edit,
1438 "edit environment variable",
1440 " - edit environment variable 'name'",
1445 U_BOOT_CMD_COMPLETE(
1446 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1447 "print environment variables",
1448 "[-a]\n - print [all] values of all environment variables\n"
1449 #if defined(CONFIG_CMD_NVEDIT_EFI)
1450 "printenv -e [-guid guid|-all][-n] [name ...]\n"
1451 " - print UEFI variable 'name' or all the variables\n"
1452 " \"-n\": suppress dumping variable's value\n"
1454 "printenv name ...\n"
1455 " - print value of environment variable 'name'",
1459 #ifdef CONFIG_CMD_GREPENV
1460 U_BOOT_CMD_COMPLETE(
1461 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1462 "search environment variables",
1464 "[-e] [-n | -v | -b] string ...\n"
1466 "[-n | -v | -b] string ...\n"
1468 " - list environment name=value pairs matching 'string'\n"
1470 " \"-e\": enable regular expressions;\n"
1472 " \"-n\": search variable names; \"-v\": search values;\n"
1473 " \"-b\": search both names and values (default)",
1478 U_BOOT_CMD_COMPLETE(
1479 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1480 "set environment variables",
1481 #if defined(CONFIG_CMD_NVEDIT_EFI)
1482 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1483 " [-i addr,size name], or [name [value ...]]\n"
1484 " - set UEFI variable 'name' to 'value' ...'\n"
1485 " \"-guid\": set vendor guid\n"
1486 " \"-nv\": set non-volatile attribute\n"
1487 " \"-bs\": set boot-service attribute\n"
1488 " \"-rt\": set runtime attribute\n"
1489 " \"-at\": set time-based authentication attribute\n"
1490 " \"-a\": append-write\n"
1491 " \"-i addr,size\": use <addr,size> as variable's value\n"
1492 " \"-v\": verbose message\n"
1493 " - delete UEFI variable 'name' if 'value' not specified\n"
1495 "setenv [-f] name value ...\n"
1496 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1497 "setenv [-f] name\n"
1498 " - [forcibly] delete environment variable 'name'",
1502 #if defined(CONFIG_CMD_ASKENV)
1505 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1506 "get environment variables from stdin",
1507 "name [message] [size]\n"
1508 " - get environment variable 'name' from stdin (max 'size' chars)"
1512 #if defined(CONFIG_CMD_RUN)
1513 U_BOOT_CMD_COMPLETE(
1514 run, CONFIG_SYS_MAXARGS, 1, do_run,
1515 "run commands in an environment variable",
1517 " - run the commands in the environment variable(s) 'var'",
1521 #endif /* CONFIG_SPL_BUILD */