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 <environment.h>
37 #include <linux/stddef.h>
38 #include <asm/byteorder.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
44 defined(CONFIG_ENV_IS_IN_FLASH) || \
45 defined(CONFIG_ENV_IS_IN_MMC) || \
46 defined(CONFIG_ENV_IS_IN_FAT) || \
47 defined(CONFIG_ENV_IS_IN_EXT4) || \
48 defined(CONFIG_ENV_IS_IN_NAND) || \
49 defined(CONFIG_ENV_IS_IN_NVRAM) || \
50 defined(CONFIG_ENV_IS_IN_ONENAND) || \
51 defined(CONFIG_ENV_IS_IN_SATA) || \
52 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
53 defined(CONFIG_ENV_IS_IN_REMOTE) || \
54 defined(CONFIG_ENV_IS_IN_UBI)
56 #define ENV_IS_IN_DEVICE
60 #if !defined(ENV_IS_IN_DEVICE) && \
61 !defined(CONFIG_ENV_IS_NOWHERE)
62 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
63 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
67 * Maximum expected input data size for import command
69 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
72 * This variable is incremented on each do_env_set(), so it can
73 * be used via env_get_id() as an indication, if the environment
74 * has changed or not. So it is possible to reread an environment
75 * variable only if the environment was changed ... done so for
76 * example in NetInitLoop()
78 static int env_id = 1;
85 #ifndef CONFIG_SPL_BUILD
87 * Command interface: print one or all environment variables
89 * Returns 0 in case of error, or length of printed string
91 static int env_print(char *name, int flag)
96 if (name) { /* print a single name */
101 hsearch_r(e, FIND, &ep, &env_htab, flag);
104 len = printf("%s=%s\n", ep->key, ep->data);
108 /* print whole list */
109 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
117 /* should never happen */
118 printf("## Error: cannot export environment\n");
122 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
127 int env_flag = H_HIDE_DOT;
129 #if defined(CONFIG_CMD_NVEDIT_EFI)
130 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
131 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
137 env_flag &= ~H_HIDE_DOT;
141 /* print all env vars */
142 rcode = env_print(NULL, env_flag);
145 printf("\nEnvironment size: %d/%ld bytes\n",
146 rcode, (ulong)ENV_SIZE);
150 /* print selected env vars */
151 env_flag &= ~H_HIDE_DOT;
152 for (i = 1; i < argc; ++i) {
153 int rc = env_print(argv[i], env_flag);
155 printf("## Error: \"%s\" not defined\n", argv[i]);
163 #ifdef CONFIG_CMD_GREPENV
164 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165 int argc, char * const argv[])
168 int len, grep_how, grep_what;
171 return CMD_RET_USAGE;
173 grep_how = H_MATCH_SUBSTR; /* default: substring search */
174 grep_what = H_MATCH_BOTH; /* default: grep names and values */
176 while (--argc > 0 && **++argv == '-') {
181 case 'e': /* use regex matching */
182 grep_how = H_MATCH_REGEX;
185 case 'n': /* grep for name */
186 grep_what = H_MATCH_KEY;
188 case 'v': /* grep for value */
189 grep_what = H_MATCH_DATA;
191 case 'b': /* grep for both */
192 grep_what = H_MATCH_BOTH;
197 return CMD_RET_USAGE;
203 len = hexport_r(&env_htab, '\n',
204 flag | grep_what | grep_how,
205 &res, 0, argc, argv);
218 #endif /* CONFIG_SPL_BUILD */
221 * Set a new environment variable,
222 * or replace or delete an existing one.
224 static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
227 char *name, *value, *s;
230 debug("Initial value for argc=%d\n", argc);
232 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
233 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
234 return do_env_set_efi(NULL, flag, --argc, ++argv);
237 while (argc > 1 && **(argv + 1) == '-') {
243 case 'f': /* force */
247 return CMD_RET_USAGE;
251 debug("Final value for argc=%d\n", argc);
254 if (strchr(name, '=')) {
255 printf("## Error: illegal character '='"
256 "in variable name \"%s\"\n", name);
263 if (argc < 3 || argv[2] == NULL) {
264 int rc = hdelete_r(name, &env_htab, env_flag);
269 * Insert / replace new value
271 for (i = 2, len = 0; i < argc; ++i)
272 len += strlen(argv[i]) + 1;
276 printf("## Can't malloc %d bytes\n", len);
279 for (i = 2, s = value; i < argc; ++i) {
282 while ((*s++ = *v++) != '\0')
291 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
294 printf("## Error inserting \"%s\" variable, errno=%d\n",
302 int env_set(const char *varname, const char *varvalue)
304 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
306 /* before import into hashtable */
307 if (!(gd->flags & GD_FLG_ENV_READY))
310 if (varvalue == NULL || varvalue[0] == '\0')
311 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
313 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
317 * Set an environment variable to an integer value
319 * @param varname Environment variable to set
320 * @param value Value to set it to
321 * @return 0 if ok, 1 on error
323 int env_set_ulong(const char *varname, ulong value)
325 /* TODO: this should be unsigned */
326 char *str = simple_itoa(value);
328 return env_set(varname, str);
332 * Set an environment variable to an value in hex
334 * @param varname Environment variable to set
335 * @param value Value to set it to
336 * @return 0 if ok, 1 on error
338 int env_set_hex(const char *varname, ulong value)
342 sprintf(str, "%lx", value);
343 return env_set(varname, str);
346 ulong env_get_hex(const char *varname, ulong default_val)
352 s = env_get(varname);
354 value = simple_strtoul(s, &endp, 16);
361 void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
366 for (i = 0; i < 6; ++i) {
367 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
369 addr = (*end) ? end + 1 : end;
373 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
375 eth_parse_enetaddr(env_get(name), enetaddr);
376 return is_valid_ethaddr(enetaddr);
379 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
381 char buf[ARP_HLEN_ASCII + 1];
383 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
386 sprintf(buf, "%pM", enetaddr);
388 return env_set(name, buf);
391 #ifndef CONFIG_SPL_BUILD
392 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
395 return CMD_RET_USAGE;
397 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
401 * Prompt for environment variable
403 #if defined(CONFIG_CMD_ASKENV)
404 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
406 char message[CONFIG_SYS_CBSIZE];
407 int i, len, pos, size;
411 local_args[0] = argv[0];
412 local_args[1] = argv[1];
413 local_args[2] = NULL;
414 local_args[3] = NULL;
419 * env_ask envname [message1 ...] [size]
422 return CMD_RET_USAGE;
425 * We test the last argument if it can be converted
426 * into a decimal number. If yes, we assume it's
427 * the size. Otherwise we echo it as part of the
430 i = simple_strtoul(argv[argc - 1], &endptr, 10);
431 if (*endptr != '\0') { /* no size */
432 size = CONFIG_SYS_CBSIZE - 1;
433 } else { /* size given */
439 sprintf(message, "Please enter '%s': ", argv[1]);
441 /* env_ask envname message1 ... messagen [size] */
442 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
444 message[pos++] = ' ';
446 strncpy(message + pos, argv[i], sizeof(message) - pos);
447 pos += strlen(argv[i]);
449 if (pos < sizeof(message) - 1) {
450 message[pos++] = ' ';
453 message[CONFIG_SYS_CBSIZE - 1] = '\0';
456 if (size >= CONFIG_SYS_CBSIZE)
457 size = CONFIG_SYS_CBSIZE - 1;
462 /* prompt for input */
463 len = cli_readline(message);
466 console_buffer[size] = '\0';
469 if (console_buffer[0] != '\0') {
470 local_args[2] = console_buffer;
474 /* Continue calling setenv code */
475 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
479 #if defined(CONFIG_CMD_ENV_CALLBACK)
480 static int print_static_binding(const char *var_name, const char *callback_name,
483 printf("\t%-20s %-20s\n", var_name, callback_name);
488 static int print_active_callback(ENTRY *entry)
490 struct env_clbk_tbl *clbkp;
494 if (entry->callback == NULL)
497 /* look up the callback in the linker-list */
498 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
499 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
502 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
503 if (entry->callback == clbkp->callback + gd->reloc_off)
505 if (entry->callback == clbkp->callback)
510 if (i == num_callbacks)
511 /* this should probably never happen, but just in case... */
512 printf("\t%-20s %p\n", entry->key, entry->callback);
514 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
520 * Print the callbacks available and what they are bound to
522 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
524 struct env_clbk_tbl *clbkp;
528 /* Print the available callbacks */
529 puts("Available callbacks:\n");
530 puts("\tCallback Name\n");
531 puts("\t-------------\n");
532 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
533 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
536 printf("\t%s\n", clbkp->name);
539 /* Print the static bindings that may exist */
540 puts("Static callback bindings:\n");
541 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
542 printf("\t%-20s %-20s\n", "-------------", "-------------");
543 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
546 /* walk through each variable and print the callback if it has one */
547 puts("Active callback bindings:\n");
548 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
549 printf("\t%-20s %-20s\n", "-------------", "-------------");
550 hwalk_r(&env_htab, print_active_callback);
555 #if defined(CONFIG_CMD_ENV_FLAGS)
556 static int print_static_flags(const char *var_name, const char *flags,
559 enum env_flags_vartype type = env_flags_parse_vartype(flags);
560 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
562 printf("\t%-20s %-20s %-20s\n", var_name,
563 env_flags_get_vartype_name(type),
564 env_flags_get_varaccess_name(access));
569 static int print_active_flags(ENTRY *entry)
571 enum env_flags_vartype type;
572 enum env_flags_varaccess access;
574 if (entry->flags == 0)
577 type = (enum env_flags_vartype)
578 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
579 access = env_flags_parse_varaccess_from_binflags(entry->flags);
580 printf("\t%-20s %-20s %-20s\n", entry->key,
581 env_flags_get_vartype_name(type),
582 env_flags_get_varaccess_name(access));
588 * Print the flags available and what variables have flags
590 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
592 /* Print the available variable types */
593 printf("Available variable type flags (position %d):\n",
594 ENV_FLAGS_VARTYPE_LOC);
595 puts("\tFlag\tVariable Type Name\n");
596 puts("\t----\t------------------\n");
597 env_flags_print_vartypes();
600 /* Print the available variable access types */
601 printf("Available variable access flags (position %d):\n",
602 ENV_FLAGS_VARACCESS_LOC);
603 puts("\tFlag\tVariable Access Name\n");
604 puts("\t----\t--------------------\n");
605 env_flags_print_varaccess();
608 /* Print the static flags that may exist */
609 puts("Static flags:\n");
610 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
612 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
614 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
617 /* walk through each variable and print the flags if non-default */
618 puts("Active flags:\n");
619 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
621 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
623 hwalk_r(&env_htab, print_active_flags);
629 * Interactively edit an environment variable
631 #if defined(CONFIG_CMD_EDITENV)
632 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
635 char buffer[CONFIG_SYS_CBSIZE];
639 return CMD_RET_USAGE;
641 /* before import into hashtable */
642 if (!(gd->flags & GD_FLG_ENV_READY))
645 /* Set read buffer to initial value or empty sting */
646 init_val = env_get(argv[1]);
648 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
652 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
655 if (buffer[0] == '\0') {
656 const char * const _argv[3] = { "setenv", argv[1], NULL };
658 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
660 const char * const _argv[4] = { "setenv", argv[1], buffer,
663 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
666 #endif /* CONFIG_CMD_EDITENV */
667 #endif /* CONFIG_SPL_BUILD */
670 * Look up variable from environment,
671 * return address of storage for that variable,
672 * or NULL if not found
674 char *env_get(const char *name)
676 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
683 hsearch_r(e, FIND, &ep, &env_htab, 0);
685 return ep ? ep->data : NULL;
688 /* restricted capabilities before import */
689 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
690 return (char *)(gd->env_buf);
696 * Look up variable from environment for restricted C runtime env.
698 int env_get_f(const char *name, char *buf, unsigned len)
702 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
705 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
708 if (nxt >= CONFIG_ENV_SIZE)
712 val = envmatch((uchar *)name, i);
716 /* found; copy out */
717 for (n = 0; n < len; ++n, ++buf) {
718 c = env_get_char(val++);
729 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
739 * Decode the integer value of an environment variable and return it.
741 * @param name Name of environment variable
742 * @param base Number base to use (normally 10, or 16 for hex)
743 * @param default_val Default value to return if the variable is not
745 * @return the decoded value, or default_val if not found
747 ulong env_get_ulong(const char *name, int base, ulong default_val)
750 * We can use env_get() here, even before relocation, since the
751 * environment variable value is an integer and thus short.
753 const char *str = env_get(name);
755 return str ? simple_strtoul(str, NULL, base) : default_val;
758 #ifndef CONFIG_SPL_BUILD
759 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
760 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
763 return env_save() ? 1 : 0;
767 saveenv, 1, 0, do_env_save,
768 "save environment variables to persistent storage",
772 #if defined(CONFIG_CMD_ERASEENV)
773 static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
776 return env_erase() ? 1 : 0;
780 eraseenv, 1, 0, do_env_erase,
781 "erase environment variables from persistent storage",
786 #endif /* CONFIG_SPL_BUILD */
790 * Match a name / name=value pair
792 * s1 is either a simple 'name', or a 'name=value' pair.
793 * i2 is the environment index for a 'name2=value2' pair.
794 * If the names match, return the index for the value2, else -1.
796 int envmatch(uchar *s1, int i2)
801 while (*s1 == env_get_char(i2++))
805 if (*s1 == '\0' && env_get_char(i2-1) == '=')
811 #ifndef CONFIG_SPL_BUILD
812 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
813 int argc, char * const argv[])
815 int all = 0, env_flag = H_INTERACTIVE;
817 debug("Initial value for argc=%d\n", argc);
818 while (--argc > 0 && **++argv == '-') {
823 case 'a': /* default all */
826 case 'f': /* force */
830 return cmd_usage(cmdtp);
834 debug("Final value for argc=%d\n", argc);
835 if (all && (argc == 0)) {
836 /* Reset the whole environment */
837 set_default_env("## Resetting to default environment\n",
841 if (!all && (argc > 0)) {
842 /* Reset individual variables */
843 set_default_vars(argc, argv, env_flag);
847 return cmd_usage(cmdtp);
850 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
851 int argc, char * const argv[])
853 int env_flag = H_INTERACTIVE;
856 debug("Initial value for argc=%d\n", argc);
857 while (argc > 1 && **(argv + 1) == '-') {
863 case 'f': /* force */
867 return CMD_RET_USAGE;
871 debug("Final value for argc=%d\n", argc);
876 char *name = *++argv;
878 if (!hdelete_r(name, &env_htab, env_flag))
885 #ifdef CONFIG_CMD_EXPORTENV
887 * env export [-t | -b | -c] [-s size] addr [var ...]
888 * -t: export as text format; if size is given, data will be
889 * padded with '\0' bytes; if not, one terminating '\0'
890 * will be added (which is included in the "filesize"
891 * setting so you can for exmple copy this to flash and
892 * keep the termination).
893 * -b: export as binary format (name=value pairs separated by
894 * '\0', list end marked by double "\0\0")
895 * -c: export as checksum protected environment format as
896 * used for example by "saveenv" command
898 * size of output buffer
899 * addr: memory address where environment gets stored
900 * var... List of variable names that get included into the
901 * export. Without arguments, the whole environment gets
904 * With "-c" and size is NOT given, then the export command will
905 * format the data as currently used for the persistent storage,
906 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
907 * prepend a valid CRC32 checksum and, in case of redundant
908 * environment, a "current" redundancy flag. If size is given, this
909 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
910 * checksum and redundancy flag will be inserted.
912 * With "-b" and "-t", always only the real data (including a
913 * terminating '\0' byte) will be written; here the optional size
914 * argument will be used to make sure not to overflow the user
915 * provided buffer; the command will abort if the size is not
916 * sufficient. Any remaining space will be '\0' padded.
918 * On successful return, the variable "filesize" will be set.
919 * Note that filesize includes the trailing/terminating '\0' byte(s).
921 * Usage scenario: create a text snapshot/backup of the current settings:
923 * => env export -t 100000
924 * => era ${backup_addr} +${filesize}
925 * => cp.b 100000 ${backup_addr} ${filesize}
927 * Re-import this snapshot, deleting all other settings:
929 * => env import -d -t ${backup_addr}
931 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
932 int argc, char * const argv[])
936 char *ptr, *cmd, *res;
946 while (--argc > 0 && **++argv == '-') {
950 case 'b': /* raw binary format */
955 case 'c': /* external checksum format */
961 case 's': /* size given */
963 return cmd_usage(cmdtp);
964 size = simple_strtoul(*++argv, NULL, 16);
966 case 't': /* text format */
972 return CMD_RET_USAGE;
979 return CMD_RET_USAGE;
981 addr = simple_strtoul(argv[0], NULL, 16);
982 ptr = map_sysmem(addr, size);
985 memset(ptr, '\0', size);
990 if (sep) { /* export as text file */
991 len = hexport_r(&env_htab, sep,
992 H_MATCH_KEY | H_MATCH_IDENT,
993 &ptr, size, argc, argv);
995 pr_err("## Error: Cannot export environment: errno = %d\n",
999 sprintf(buf, "%zX", (size_t)len);
1000 env_set("filesize", buf);
1005 envp = (env_t *)ptr;
1007 if (chk) /* export as checksum protected block */
1008 res = (char *)envp->data;
1009 else /* export as raw binary data */
1012 len = hexport_r(&env_htab, '\0',
1013 H_MATCH_KEY | H_MATCH_IDENT,
1014 &res, ENV_SIZE, argc, argv);
1016 pr_err("## Error: Cannot export environment: errno = %d\n",
1022 envp->crc = crc32(0, envp->data,
1023 size ? size - offsetof(env_t, data) : ENV_SIZE);
1024 #ifdef CONFIG_ENV_ADDR_REDUND
1025 envp->flags = ACTIVE_FLAG;
1028 env_set_hex("filesize", len + offsetof(env_t, data));
1033 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1039 #ifdef CONFIG_CMD_IMPORTENV
1041 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1042 * -d: delete existing environment before importing if no var is
1043 * passed; if vars are passed, if one var is in the current
1044 * environment but not in the environment at addr, delete var from
1045 * current environment;
1046 * otherwise overwrite / append to existing definitions
1047 * -t: assume text format; either "size" must be given or the
1048 * text data must be '\0' terminated
1049 * -r: handle CRLF like LF, that means exported variables with
1050 * a content which ends with \r won't get imported. Used
1051 * to import text files created with editors which are using CRLF
1052 * for line endings. Only effective in addition to -t.
1053 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1054 * -c: assume checksum protected environment format
1055 * addr: memory address to read from
1056 * size: length of input data; if missing, proper '\0'
1057 * termination is mandatory
1058 * if var is set and size should be missing (i.e. '\0'
1059 * termination), set size to '-'
1060 * var... List of the names of the only variables that get imported from
1061 * the environment at address 'addr'. Without arguments, the whole
1062 * environment gets imported.
1064 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1065 int argc, char * const argv[])
1079 while (--argc > 0 && **++argv == '-') {
1083 case 'b': /* raw binary format */
1088 case 'c': /* external checksum format */
1094 case 't': /* text format */
1099 case 'r': /* handle CRLF like LF */
1106 return CMD_RET_USAGE;
1112 return CMD_RET_USAGE;
1115 printf("## Warning: defaulting to text format\n");
1117 if (sep != '\n' && crlf_is_lf )
1120 addr = simple_strtoul(argv[0], NULL, 16);
1121 ptr = map_sysmem(addr, 0);
1123 if (argc >= 2 && strcmp(argv[1], "-")) {
1124 size = simple_strtoul(argv[1], NULL, 16);
1126 puts("## Error: external checksum format must pass size\n");
1127 return CMD_RET_FAILURE;
1133 while (size < MAX_ENV_SIZE) {
1134 if ((*s == sep) && (*(s+1) == '\0'))
1139 if (size == MAX_ENV_SIZE) {
1140 printf("## Warning: Input data exceeds %d bytes"
1141 " - truncated\n", MAX_ENV_SIZE);
1144 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1152 env_t *ep = (env_t *)ptr;
1154 size -= offsetof(env_t, data);
1155 memcpy(&crc, &ep->crc, sizeof(crc));
1157 if (crc32(0, ep->data, size) != crc) {
1158 puts("## Error: bad CRC, import failed\n");
1161 ptr = (char *)ep->data;
1164 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1165 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1166 pr_err("## Error: Environment import failed: errno = %d\n",
1170 gd->flags |= GD_FLG_ENV_READY;
1175 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1181 #if defined(CONFIG_CMD_NVEDIT_INFO)
1183 * print_env_info - print environment information
1185 static int print_env_info(void)
1189 /* print environment validity value */
1190 switch (gd->env_valid) {
1198 value = "redundant";
1204 printf("env_valid = %s\n", value);
1206 /* print environment ready flag */
1207 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1208 printf("env_ready = %s\n", value);
1210 /* print environment using default flag */
1211 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1212 printf("env_use_default = %s\n", value);
1214 return CMD_RET_SUCCESS;
1217 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1218 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1221 * env info - display environment information
1222 * env info [-d] - evaluate whether default environment is used
1223 * env info [-p] - evaluate whether environment can be persisted
1225 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1226 int argc, char * const argv[])
1229 int eval_results = 0;
1231 /* display environment information */
1233 return print_env_info();
1235 /* process options */
1236 while (--argc > 0 && **++argv == '-') {
1242 eval_flags |= ENV_INFO_IS_DEFAULT;
1245 eval_flags |= ENV_INFO_IS_PERSISTED;
1248 return CMD_RET_USAGE;
1253 /* evaluate whether default environment is used */
1254 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1255 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1256 printf("Default environment is used\n");
1257 eval_results |= ENV_INFO_IS_DEFAULT;
1259 printf("Environment was loaded from persistent storage\n");
1263 /* evaluate whether environment can be persisted */
1264 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1265 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1266 printf("Environment can be persisted\n");
1267 eval_results |= ENV_INFO_IS_PERSISTED;
1269 printf("Environment cannot be persisted\n");
1273 /* The result of evaluations is combined with AND */
1274 if (eval_flags != eval_results)
1275 return CMD_RET_FAILURE;
1277 return CMD_RET_SUCCESS;
1281 #if defined(CONFIG_CMD_ENV_EXISTS)
1282 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1283 char * const argv[])
1288 return CMD_RET_USAGE;
1292 hsearch_r(e, FIND, &ep, &env_htab, 0);
1294 return (ep == NULL) ? 1 : 0;
1299 * New command line interface: "env" command with subcommands
1301 static cmd_tbl_t cmd_env_sub[] = {
1302 #if defined(CONFIG_CMD_ASKENV)
1303 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1305 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1306 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1307 #if defined(CONFIG_CMD_EDITENV)
1308 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1310 #if defined(CONFIG_CMD_ENV_CALLBACK)
1311 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1313 #if defined(CONFIG_CMD_ENV_FLAGS)
1314 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1316 #if defined(CONFIG_CMD_EXPORTENV)
1317 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1319 #if defined(CONFIG_CMD_GREPENV)
1320 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1322 #if defined(CONFIG_CMD_IMPORTENV)
1323 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1325 #if defined(CONFIG_CMD_NVEDIT_INFO)
1326 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1328 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1329 #if defined(CONFIG_CMD_RUN)
1330 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1332 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1333 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1334 #if defined(CONFIG_CMD_ERASEENV)
1335 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1338 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1339 #if defined(CONFIG_CMD_ENV_EXISTS)
1340 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1344 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1345 void env_reloc(void)
1347 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1351 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1356 return CMD_RET_USAGE;
1358 /* drop initial "env" arg */
1362 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1365 return cp->cmd(cmdtp, flag, argc, argv);
1367 return CMD_RET_USAGE;
1370 #ifdef CONFIG_SYS_LONGHELP
1371 static char env_help_text[] =
1372 #if defined(CONFIG_CMD_ASKENV)
1373 "ask name [message] [size] - ask for environment variable\nenv "
1375 #if defined(CONFIG_CMD_ENV_CALLBACK)
1376 "callbacks - print callbacks and their associated variables\nenv "
1378 "default [-f] -a - [forcibly] reset default environment\n"
1379 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1380 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1381 #if defined(CONFIG_CMD_EDITENV)
1382 "env edit name - edit environment variable\n"
1384 #if defined(CONFIG_CMD_ENV_EXISTS)
1385 "env exists name - tests for existence of variable\n"
1387 #if defined(CONFIG_CMD_EXPORTENV)
1388 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1390 #if defined(CONFIG_CMD_ENV_FLAGS)
1391 "env flags - print variables that have non-default flags\n"
1393 #if defined(CONFIG_CMD_GREPENV)
1395 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1397 "env grep [-n | -v | -b] string [...] - search environment\n"
1400 #if defined(CONFIG_CMD_IMPORTENV)
1401 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1403 #if defined(CONFIG_CMD_NVEDIT_INFO)
1404 "env info - display environment information\n"
1405 "env info [-d] - whether default environment is used\n"
1406 "env info [-p] - whether environment can be persisted\n"
1408 "env print [-a | name ...] - print environment\n"
1409 #if defined(CONFIG_CMD_NVEDIT_EFI)
1410 "env print -e [name ...] - print UEFI environment\n"
1412 #if defined(CONFIG_CMD_RUN)
1413 "env run var [...] - run commands in an environment variable\n"
1415 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1416 "env save - save environment\n"
1417 #if defined(CONFIG_CMD_ERASEENV)
1418 "env erase - erase environment\n"
1421 #if defined(CONFIG_CMD_NVEDIT_EFI)
1422 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1424 "env set [-f] name [arg ...]\n";
1428 env, CONFIG_SYS_MAXARGS, 1, do_env,
1429 "environment handling commands", env_help_text
1433 * Old command line interface, kept for compatibility
1436 #if defined(CONFIG_CMD_EDITENV)
1437 U_BOOT_CMD_COMPLETE(
1438 editenv, 2, 0, do_env_edit,
1439 "edit environment variable",
1441 " - edit environment variable 'name'",
1446 U_BOOT_CMD_COMPLETE(
1447 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1448 "print environment variables",
1449 "[-a]\n - print [all] values of all environment variables\n"
1450 #if defined(CONFIG_CMD_NVEDIT_EFI)
1451 "printenv -e [name ...]\n"
1452 " - print UEFI variable 'name' or all the variables\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 [-nv] name [value ...]\n"
1483 " - set UEFI variable 'name' to 'value' ...'\n"
1484 " 'nv' option makes the variable non-volatile\n"
1485 " - delete UEFI variable 'name' if 'value' not specified\n"
1487 "setenv [-f] name value ...\n"
1488 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1489 "setenv [-f] name\n"
1490 " - [forcibly] delete environment variable 'name'",
1494 #if defined(CONFIG_CMD_ASKENV)
1497 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1498 "get environment variables from stdin",
1499 "name [message] [size]\n"
1500 " - get environment variable 'name' from stdin (max 'size' chars)"
1504 #if defined(CONFIG_CMD_RUN)
1505 U_BOOT_CMD_COMPLETE(
1506 run, CONFIG_SYS_MAXARGS, 1, do_run,
1507 "run commands in an environment variable",
1509 " - run the commands in the environment variable(s) 'var'",
1513 #endif /* CONFIG_SPL_BUILD */