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>
38 #include <u-boot/crc.h>
40 #include <linux/stddef.h>
41 #include <asm/byteorder.h>
44 DECLARE_GLOBAL_DATA_PTR;
46 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
47 defined(CONFIG_ENV_IS_IN_FLASH) || \
48 defined(CONFIG_ENV_IS_IN_MMC) || \
49 defined(CONFIG_ENV_IS_IN_FAT) || \
50 defined(CONFIG_ENV_IS_IN_EXT4) || \
51 defined(CONFIG_ENV_IS_IN_NAND) || \
52 defined(CONFIG_ENV_IS_IN_NVRAM) || \
53 defined(CONFIG_ENV_IS_IN_ONENAND) || \
54 defined(CONFIG_ENV_IS_IN_SATA) || \
55 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
56 defined(CONFIG_ENV_IS_IN_REMOTE) || \
57 defined(CONFIG_ENV_IS_IN_UBI)
59 #define ENV_IS_IN_DEVICE
63 #if !defined(ENV_IS_IN_DEVICE) && \
64 !defined(CONFIG_ENV_IS_NOWHERE)
65 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
66 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
70 * Maximum expected input data size for import command
72 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
75 * This variable is incremented on each do_env_set(), so it can
76 * be used via env_get_id() as an indication, if the environment
77 * has changed or not. So it is possible to reread an environment
78 * variable only if the environment was changed ... done so for
79 * example in NetInitLoop()
81 static int env_id = 1;
88 #ifndef CONFIG_SPL_BUILD
90 * Command interface: print one or all environment variables
92 * Returns 0 in case of error, or length of printed string
94 static int env_print(char *name, int flag)
99 if (name) { /* print a single name */
100 struct env_entry e, *ep;
104 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
107 len = printf("%s=%s\n", ep->key, ep->data);
111 /* print whole list */
112 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
120 /* should never happen */
121 printf("## Error: cannot export environment\n");
125 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
130 int env_flag = H_HIDE_DOT;
132 #if defined(CONFIG_CMD_NVEDIT_EFI)
133 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
134 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
137 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
140 env_flag &= ~H_HIDE_DOT;
144 /* print all env vars */
145 rcode = env_print(NULL, env_flag);
148 printf("\nEnvironment size: %d/%ld bytes\n",
149 rcode, (ulong)ENV_SIZE);
153 /* print selected env vars */
154 env_flag &= ~H_HIDE_DOT;
155 for (i = 1; i < argc; ++i) {
156 int rc = env_print(argv[i], env_flag);
158 printf("## Error: \"%s\" not defined\n", argv[i]);
166 #ifdef CONFIG_CMD_GREPENV
167 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
168 int argc, char *const argv[])
171 int len, grep_how, grep_what;
174 return CMD_RET_USAGE;
176 grep_how = H_MATCH_SUBSTR; /* default: substring search */
177 grep_what = H_MATCH_BOTH; /* default: grep names and values */
179 while (--argc > 0 && **++argv == '-') {
184 case 'e': /* use regex matching */
185 grep_how = H_MATCH_REGEX;
188 case 'n': /* grep for name */
189 grep_what = H_MATCH_KEY;
191 case 'v': /* grep for value */
192 grep_what = H_MATCH_DATA;
194 case 'b': /* grep for both */
195 grep_what = H_MATCH_BOTH;
200 return CMD_RET_USAGE;
206 len = hexport_r(&env_htab, '\n',
207 flag | grep_what | grep_how,
208 &res, 0, argc, argv);
221 #endif /* CONFIG_SPL_BUILD */
224 * Set a new environment variable,
225 * or replace or delete an existing one.
227 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
230 char *name, *value, *s;
231 struct env_entry e, *ep;
233 debug("Initial value for argc=%d\n", argc);
235 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
236 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
237 return do_env_set_efi(NULL, flag, --argc, ++argv);
240 while (argc > 1 && **(argv + 1) == '-') {
246 case 'f': /* force */
250 return CMD_RET_USAGE;
254 debug("Final value for argc=%d\n", argc);
257 if (strchr(name, '=')) {
258 printf("## Error: illegal character '='"
259 "in variable name \"%s\"\n", name);
266 if (argc < 3 || argv[2] == NULL) {
267 int rc = hdelete_r(name, &env_htab, env_flag);
272 * Insert / replace new value
274 for (i = 2, len = 0; i < argc; ++i)
275 len += strlen(argv[i]) + 1;
279 printf("## Can't malloc %d bytes\n", len);
282 for (i = 2, s = value; i < argc; ++i) {
285 while ((*s++ = *v++) != '\0')
294 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
297 printf("## Error inserting \"%s\" variable, errno=%d\n",
305 int env_set(const char *varname, const char *varvalue)
307 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
309 /* before import into hashtable */
310 if (!(gd->flags & GD_FLG_ENV_READY))
313 if (varvalue == NULL || varvalue[0] == '\0')
314 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
316 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
320 * Set an environment variable to an integer value
322 * @param varname Environment variable to set
323 * @param value Value to set it to
324 * @return 0 if ok, 1 on error
326 int env_set_ulong(const char *varname, ulong value)
328 /* TODO: this should be unsigned */
329 char *str = simple_itoa(value);
331 return env_set(varname, str);
335 * Set an environment variable to an value in hex
337 * @param varname Environment variable to set
338 * @param value Value to set it to
339 * @return 0 if ok, 1 on error
341 int env_set_hex(const char *varname, ulong value)
345 sprintf(str, "%lx", value);
346 return env_set(varname, str);
349 ulong env_get_hex(const char *varname, ulong default_val)
355 s = env_get(varname);
357 value = simple_strtoul(s, &endp, 16);
364 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
366 string_to_enetaddr(env_get(name), enetaddr);
367 return is_valid_ethaddr(enetaddr);
370 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
372 char buf[ARP_HLEN_ASCII + 1];
374 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
377 sprintf(buf, "%pM", enetaddr);
379 return env_set(name, buf);
382 #ifndef CONFIG_SPL_BUILD
383 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
387 return CMD_RET_USAGE;
389 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
393 * Prompt for environment variable
395 #if defined(CONFIG_CMD_ASKENV)
396 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
398 char message[CONFIG_SYS_CBSIZE];
399 int i, len, pos, size;
403 local_args[0] = argv[0];
404 local_args[1] = argv[1];
405 local_args[2] = NULL;
406 local_args[3] = NULL;
411 * env_ask envname [message1 ...] [size]
414 return CMD_RET_USAGE;
417 * We test the last argument if it can be converted
418 * into a decimal number. If yes, we assume it's
419 * the size. Otherwise we echo it as part of the
422 i = simple_strtoul(argv[argc - 1], &endptr, 10);
423 if (*endptr != '\0') { /* no size */
424 size = CONFIG_SYS_CBSIZE - 1;
425 } else { /* size given */
431 sprintf(message, "Please enter '%s': ", argv[1]);
433 /* env_ask envname message1 ... messagen [size] */
434 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
436 message[pos++] = ' ';
438 strncpy(message + pos, argv[i], sizeof(message) - pos);
439 pos += strlen(argv[i]);
441 if (pos < sizeof(message) - 1) {
442 message[pos++] = ' ';
445 message[CONFIG_SYS_CBSIZE - 1] = '\0';
448 if (size >= CONFIG_SYS_CBSIZE)
449 size = CONFIG_SYS_CBSIZE - 1;
454 /* prompt for input */
455 len = cli_readline(message);
458 console_buffer[size] = '\0';
461 if (console_buffer[0] != '\0') {
462 local_args[2] = console_buffer;
466 /* Continue calling setenv code */
467 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
471 #if defined(CONFIG_CMD_ENV_CALLBACK)
472 static int print_static_binding(const char *var_name, const char *callback_name,
475 printf("\t%-20s %-20s\n", var_name, callback_name);
480 static int print_active_callback(struct env_entry *entry)
482 struct env_clbk_tbl *clbkp;
486 if (entry->callback == NULL)
489 /* look up the callback in the linker-list */
490 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
491 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
494 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
495 if (entry->callback == clbkp->callback + gd->reloc_off)
497 if (entry->callback == clbkp->callback)
502 if (i == num_callbacks)
503 /* this should probably never happen, but just in case... */
504 printf("\t%-20s %p\n", entry->key, entry->callback);
506 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
512 * Print the callbacks available and what they are bound to
514 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
517 struct env_clbk_tbl *clbkp;
521 /* Print the available callbacks */
522 puts("Available callbacks:\n");
523 puts("\tCallback Name\n");
524 puts("\t-------------\n");
525 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
526 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
529 printf("\t%s\n", clbkp->name);
532 /* Print the static bindings that may exist */
533 puts("Static callback bindings:\n");
534 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
535 printf("\t%-20s %-20s\n", "-------------", "-------------");
536 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
539 /* walk through each variable and print the callback if it has one */
540 puts("Active callback bindings:\n");
541 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
542 printf("\t%-20s %-20s\n", "-------------", "-------------");
543 hwalk_r(&env_htab, print_active_callback);
548 #if defined(CONFIG_CMD_ENV_FLAGS)
549 static int print_static_flags(const char *var_name, const char *flags,
552 enum env_flags_vartype type = env_flags_parse_vartype(flags);
553 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
555 printf("\t%-20s %-20s %-20s\n", var_name,
556 env_flags_get_vartype_name(type),
557 env_flags_get_varaccess_name(access));
562 static int print_active_flags(struct env_entry *entry)
564 enum env_flags_vartype type;
565 enum env_flags_varaccess access;
567 if (entry->flags == 0)
570 type = (enum env_flags_vartype)
571 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
572 access = env_flags_parse_varaccess_from_binflags(entry->flags);
573 printf("\t%-20s %-20s %-20s\n", entry->key,
574 env_flags_get_vartype_name(type),
575 env_flags_get_varaccess_name(access));
581 * Print the flags available and what variables have flags
583 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
585 /* Print the available variable types */
586 printf("Available variable type flags (position %d):\n",
587 ENV_FLAGS_VARTYPE_LOC);
588 puts("\tFlag\tVariable Type Name\n");
589 puts("\t----\t------------------\n");
590 env_flags_print_vartypes();
593 /* Print the available variable access types */
594 printf("Available variable access flags (position %d):\n",
595 ENV_FLAGS_VARACCESS_LOC);
596 puts("\tFlag\tVariable Access Name\n");
597 puts("\t----\t--------------------\n");
598 env_flags_print_varaccess();
601 /* Print the static flags that may exist */
602 puts("Static flags:\n");
603 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
605 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
607 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
610 /* walk through each variable and print the flags if non-default */
611 puts("Active flags:\n");
612 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
614 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
616 hwalk_r(&env_htab, print_active_flags);
622 * Interactively edit an environment variable
624 #if defined(CONFIG_CMD_EDITENV)
625 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
628 char buffer[CONFIG_SYS_CBSIZE];
632 return CMD_RET_USAGE;
634 /* before import into hashtable */
635 if (!(gd->flags & GD_FLG_ENV_READY))
638 /* Set read buffer to initial value or empty sting */
639 init_val = env_get(argv[1]);
641 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
645 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
648 if (buffer[0] == '\0') {
649 const char * const _argv[3] = { "setenv", argv[1], NULL };
651 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
653 const char * const _argv[4] = { "setenv", argv[1], buffer,
656 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
659 #endif /* CONFIG_CMD_EDITENV */
660 #endif /* CONFIG_SPL_BUILD */
663 * Look up variable from environment,
664 * return address of storage for that variable,
665 * or NULL if not found
667 char *env_get(const char *name)
669 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
670 struct env_entry e, *ep;
676 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
678 return ep ? ep->data : NULL;
681 /* restricted capabilities before import */
682 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
683 return (char *)(gd->env_buf);
689 * Like env_get, but prints an error if envvar isn't defined in the
690 * environment. It always returns what env_get does, so it can be used in
691 * place of env_get without changing error handling otherwise.
693 char *from_env(const char *envvar)
697 ret = env_get(envvar);
700 printf("missing environment variable: %s\n", envvar);
706 * Look up variable from environment for restricted C runtime env.
708 int env_get_f(const char *name, char *buf, unsigned len)
712 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
715 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
718 if (nxt >= CONFIG_ENV_SIZE)
722 val = env_match((uchar *)name, i);
726 /* found; copy out */
727 for (n = 0; n < len; ++n, ++buf) {
728 c = env_get_char(val++);
739 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
749 * Decode the integer value of an environment variable and return it.
751 * @param name Name of environment variable
752 * @param base Number base to use (normally 10, or 16 for hex)
753 * @param default_val Default value to return if the variable is not
755 * @return the decoded value, or default_val if not found
757 ulong env_get_ulong(const char *name, int base, ulong default_val)
760 * We can use env_get() here, even before relocation, since the
761 * environment variable value is an integer and thus short.
763 const char *str = env_get(name);
765 return str ? simple_strtoul(str, NULL, base) : default_val;
768 #ifndef CONFIG_SPL_BUILD
769 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
770 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
773 return env_save() ? 1 : 0;
777 saveenv, 1, 0, do_env_save,
778 "save environment variables to persistent storage",
782 #if defined(CONFIG_CMD_ERASEENV)
783 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
786 return env_erase() ? 1 : 0;
790 eraseenv, 1, 0, do_env_erase,
791 "erase environment variables from persistent storage",
796 #endif /* CONFIG_SPL_BUILD */
798 int env_match(uchar *s1, int i2)
803 while (*s1 == env_get_char(i2++))
807 if (*s1 == '\0' && env_get_char(i2-1) == '=')
813 #ifndef CONFIG_SPL_BUILD
814 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
815 int argc, char *const argv[])
817 int all = 0, env_flag = H_INTERACTIVE;
819 debug("Initial value for argc=%d\n", argc);
820 while (--argc > 0 && **++argv == '-') {
825 case 'a': /* default all */
828 case 'f': /* force */
832 return cmd_usage(cmdtp);
836 debug("Final value for argc=%d\n", argc);
837 if (all && (argc == 0)) {
838 /* Reset the whole environment */
839 env_set_default("## Resetting to default environment\n",
843 if (!all && (argc > 0)) {
844 /* Reset individual variables */
845 env_set_default_vars(argc, argv, env_flag);
849 return cmd_usage(cmdtp);
852 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
853 int argc, char *const argv[])
855 int env_flag = H_INTERACTIVE;
858 debug("Initial value for argc=%d\n", argc);
859 while (argc > 1 && **(argv + 1) == '-') {
865 case 'f': /* force */
869 return CMD_RET_USAGE;
873 debug("Final value for argc=%d\n", argc);
878 char *name = *++argv;
880 if (!hdelete_r(name, &env_htab, env_flag))
887 #ifdef CONFIG_CMD_EXPORTENV
889 * env export [-t | -b | -c] [-s size] addr [var ...]
890 * -t: export as text format; if size is given, data will be
891 * padded with '\0' bytes; if not, one terminating '\0'
892 * will be added (which is included in the "filesize"
893 * setting so you can for exmple copy this to flash and
894 * keep the termination).
895 * -b: export as binary format (name=value pairs separated by
896 * '\0', list end marked by double "\0\0")
897 * -c: export as checksum protected environment format as
898 * used for example by "saveenv" command
900 * size of output buffer
901 * addr: memory address where environment gets stored
902 * var... List of variable names that get included into the
903 * export. Without arguments, the whole environment gets
906 * With "-c" and size is NOT given, then the export command will
907 * format the data as currently used for the persistent storage,
908 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
909 * prepend a valid CRC32 checksum and, in case of redundant
910 * environment, a "current" redundancy flag. If size is given, this
911 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
912 * checksum and redundancy flag will be inserted.
914 * With "-b" and "-t", always only the real data (including a
915 * terminating '\0' byte) will be written; here the optional size
916 * argument will be used to make sure not to overflow the user
917 * provided buffer; the command will abort if the size is not
918 * sufficient. Any remaining space will be '\0' padded.
920 * On successful return, the variable "filesize" will be set.
921 * Note that filesize includes the trailing/terminating '\0' byte(s).
923 * Usage scenario: create a text snapshot/backup of the current settings:
925 * => env export -t 100000
926 * => era ${backup_addr} +${filesize}
927 * => cp.b 100000 ${backup_addr} ${filesize}
929 * Re-import this snapshot, deleting all other settings:
931 * => env import -d -t ${backup_addr}
933 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
934 int argc, char *const argv[])
938 char *ptr, *cmd, *res;
948 while (--argc > 0 && **++argv == '-') {
952 case 'b': /* raw binary format */
957 case 'c': /* external checksum format */
963 case 's': /* size given */
965 return cmd_usage(cmdtp);
966 size = simple_strtoul(*++argv, NULL, 16);
968 case 't': /* text format */
974 return CMD_RET_USAGE;
981 return CMD_RET_USAGE;
983 addr = simple_strtoul(argv[0], NULL, 16);
984 ptr = map_sysmem(addr, size);
987 memset(ptr, '\0', size);
992 if (sep) { /* export as text file */
993 len = hexport_r(&env_htab, sep,
994 H_MATCH_KEY | H_MATCH_IDENT,
995 &ptr, size, argc, argv);
997 pr_err("## Error: Cannot export environment: errno = %d\n",
1001 sprintf(buf, "%zX", (size_t)len);
1002 env_set("filesize", buf);
1007 envp = (env_t *)ptr;
1009 if (chk) /* export as checksum protected block */
1010 res = (char *)envp->data;
1011 else /* export as raw binary data */
1014 len = hexport_r(&env_htab, '\0',
1015 H_MATCH_KEY | H_MATCH_IDENT,
1016 &res, ENV_SIZE, argc, argv);
1018 pr_err("## Error: Cannot export environment: errno = %d\n",
1024 envp->crc = crc32(0, envp->data,
1025 size ? size - offsetof(env_t, data) : ENV_SIZE);
1026 #ifdef CONFIG_ENV_ADDR_REDUND
1027 envp->flags = ENV_REDUND_ACTIVE;
1030 env_set_hex("filesize", len + offsetof(env_t, data));
1035 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1041 #ifdef CONFIG_CMD_IMPORTENV
1043 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1044 * -d: delete existing environment before importing if no var is
1045 * passed; if vars are passed, if one var is in the current
1046 * environment but not in the environment at addr, delete var from
1047 * current environment;
1048 * otherwise overwrite / append to existing definitions
1049 * -t: assume text format; either "size" must be given or the
1050 * text data must be '\0' terminated
1051 * -r: handle CRLF like LF, that means exported variables with
1052 * a content which ends with \r won't get imported. Used
1053 * to import text files created with editors which are using CRLF
1054 * for line endings. Only effective in addition to -t.
1055 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1056 * -c: assume checksum protected environment format
1057 * addr: memory address to read from
1058 * size: length of input data; if missing, proper '\0'
1059 * termination is mandatory
1060 * if var is set and size should be missing (i.e. '\0'
1061 * termination), set size to '-'
1062 * var... List of the names of the only variables that get imported from
1063 * the environment at address 'addr'. Without arguments, the whole
1064 * environment gets imported.
1066 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1067 int argc, char *const argv[])
1081 while (--argc > 0 && **++argv == '-') {
1085 case 'b': /* raw binary format */
1090 case 'c': /* external checksum format */
1096 case 't': /* text format */
1101 case 'r': /* handle CRLF like LF */
1108 return CMD_RET_USAGE;
1114 return CMD_RET_USAGE;
1117 printf("## Warning: defaulting to text format\n");
1119 if (sep != '\n' && crlf_is_lf )
1122 addr = simple_strtoul(argv[0], NULL, 16);
1123 ptr = map_sysmem(addr, 0);
1125 if (argc >= 2 && strcmp(argv[1], "-")) {
1126 size = simple_strtoul(argv[1], NULL, 16);
1128 puts("## Error: external checksum format must pass size\n");
1129 return CMD_RET_FAILURE;
1135 while (size < MAX_ENV_SIZE) {
1136 if ((*s == sep) && (*(s+1) == '\0'))
1141 if (size == MAX_ENV_SIZE) {
1142 printf("## Warning: Input data exceeds %d bytes"
1143 " - truncated\n", MAX_ENV_SIZE);
1146 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1154 env_t *ep = (env_t *)ptr;
1156 size -= offsetof(env_t, data);
1157 memcpy(&crc, &ep->crc, sizeof(crc));
1159 if (crc32(0, ep->data, size) != crc) {
1160 puts("## Error: bad CRC, import failed\n");
1163 ptr = (char *)ep->data;
1166 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1167 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1168 pr_err("## Error: Environment import failed: errno = %d\n",
1172 gd->flags |= GD_FLG_ENV_READY;
1177 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1183 #if defined(CONFIG_CMD_NVEDIT_INFO)
1185 * print_env_info - print environment information
1187 static int print_env_info(void)
1191 /* print environment validity value */
1192 switch (gd->env_valid) {
1200 value = "redundant";
1206 printf("env_valid = %s\n", value);
1208 /* print environment ready flag */
1209 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1210 printf("env_ready = %s\n", value);
1212 /* print environment using default flag */
1213 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1214 printf("env_use_default = %s\n", value);
1216 return CMD_RET_SUCCESS;
1219 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1220 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1223 * env info - display environment information
1224 * env info [-d] - evaluate whether default environment is used
1225 * env info [-p] - evaluate whether environment can be persisted
1227 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1228 int argc, char *const argv[])
1231 int eval_results = 0;
1233 /* display environment information */
1235 return print_env_info();
1237 /* process options */
1238 while (--argc > 0 && **++argv == '-') {
1244 eval_flags |= ENV_INFO_IS_DEFAULT;
1247 eval_flags |= ENV_INFO_IS_PERSISTED;
1250 return CMD_RET_USAGE;
1255 /* evaluate whether default environment is used */
1256 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1257 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1258 printf("Default environment is used\n");
1259 eval_results |= ENV_INFO_IS_DEFAULT;
1261 printf("Environment was loaded from persistent storage\n");
1265 /* evaluate whether environment can be persisted */
1266 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1267 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1268 printf("Environment can be persisted\n");
1269 eval_results |= ENV_INFO_IS_PERSISTED;
1271 printf("Environment cannot be persisted\n");
1275 /* The result of evaluations is combined with AND */
1276 if (eval_flags != eval_results)
1277 return CMD_RET_FAILURE;
1279 return CMD_RET_SUCCESS;
1283 #if defined(CONFIG_CMD_ENV_EXISTS)
1284 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1287 struct env_entry e, *ep;
1290 return CMD_RET_USAGE;
1294 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1296 return (ep == NULL) ? 1 : 0;
1301 * New command line interface: "env" command with subcommands
1303 static struct cmd_tbl cmd_env_sub[] = {
1304 #if defined(CONFIG_CMD_ASKENV)
1305 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1307 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1308 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1309 #if defined(CONFIG_CMD_EDITENV)
1310 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1312 #if defined(CONFIG_CMD_ENV_CALLBACK)
1313 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1315 #if defined(CONFIG_CMD_ENV_FLAGS)
1316 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1318 #if defined(CONFIG_CMD_EXPORTENV)
1319 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1321 #if defined(CONFIG_CMD_GREPENV)
1322 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1324 #if defined(CONFIG_CMD_IMPORTENV)
1325 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1327 #if defined(CONFIG_CMD_NVEDIT_INFO)
1328 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1330 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1331 #if defined(CONFIG_CMD_RUN)
1332 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1334 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1335 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1336 #if defined(CONFIG_CMD_ERASEENV)
1337 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1340 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1341 #if defined(CONFIG_CMD_ENV_EXISTS)
1342 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1346 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1347 void env_reloc(void)
1349 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1353 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1358 return CMD_RET_USAGE;
1360 /* drop initial "env" arg */
1364 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1367 return cp->cmd(cmdtp, flag, argc, argv);
1369 return CMD_RET_USAGE;
1372 #ifdef CONFIG_SYS_LONGHELP
1373 static char env_help_text[] =
1374 #if defined(CONFIG_CMD_ASKENV)
1375 "ask name [message] [size] - ask for environment variable\nenv "
1377 #if defined(CONFIG_CMD_ENV_CALLBACK)
1378 "callbacks - print callbacks and their associated variables\nenv "
1380 "default [-f] -a - [forcibly] reset default environment\n"
1381 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1382 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1383 #if defined(CONFIG_CMD_EDITENV)
1384 "env edit name - edit environment variable\n"
1386 #if defined(CONFIG_CMD_ENV_EXISTS)
1387 "env exists name - tests for existence of variable\n"
1389 #if defined(CONFIG_CMD_EXPORTENV)
1390 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1392 #if defined(CONFIG_CMD_ENV_FLAGS)
1393 "env flags - print variables that have non-default flags\n"
1395 #if defined(CONFIG_CMD_GREPENV)
1397 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1399 "env grep [-n | -v | -b] string [...] - search environment\n"
1402 #if defined(CONFIG_CMD_IMPORTENV)
1403 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1405 #if defined(CONFIG_CMD_NVEDIT_INFO)
1406 "env info - display environment information\n"
1407 "env info [-d] - whether default environment is used\n"
1408 "env info [-p] - whether environment can be persisted\n"
1410 "env print [-a | name ...] - print environment\n"
1411 #if defined(CONFIG_CMD_NVEDIT_EFI)
1412 "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
1414 #if defined(CONFIG_CMD_RUN)
1415 "env run var [...] - run commands in an environment variable\n"
1417 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1418 "env save - save environment\n"
1419 #if defined(CONFIG_CMD_ERASEENV)
1420 "env erase - erase environment\n"
1423 #if defined(CONFIG_CMD_NVEDIT_EFI)
1424 "env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n"
1425 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1427 "env set [-f] name [arg ...]\n";
1431 env, CONFIG_SYS_MAXARGS, 1, do_env,
1432 "environment handling commands", env_help_text
1436 * Old command line interface, kept for compatibility
1439 #if defined(CONFIG_CMD_EDITENV)
1440 U_BOOT_CMD_COMPLETE(
1441 editenv, 2, 0, do_env_edit,
1442 "edit environment variable",
1444 " - edit environment variable 'name'",
1449 U_BOOT_CMD_COMPLETE(
1450 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1451 "print environment variables",
1452 "[-a]\n - print [all] values of all environment variables\n"
1453 #if defined(CONFIG_CMD_NVEDIT_EFI)
1454 "printenv -e [-guid guid|-all][-n] [name ...]\n"
1455 " - print UEFI variable 'name' or all the variables\n"
1456 " \"-n\": suppress dumping variable's value\n"
1458 "printenv name ...\n"
1459 " - print value of environment variable 'name'",
1463 #ifdef CONFIG_CMD_GREPENV
1464 U_BOOT_CMD_COMPLETE(
1465 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1466 "search environment variables",
1468 "[-e] [-n | -v | -b] string ...\n"
1470 "[-n | -v | -b] string ...\n"
1472 " - list environment name=value pairs matching 'string'\n"
1474 " \"-e\": enable regular expressions;\n"
1476 " \"-n\": search variable names; \"-v\": search values;\n"
1477 " \"-b\": search both names and values (default)",
1482 U_BOOT_CMD_COMPLETE(
1483 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1484 "set environment variables",
1485 #if defined(CONFIG_CMD_NVEDIT_EFI)
1486 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1487 " [-i addr,size name], or [name [value ...]]\n"
1488 " - set UEFI variable 'name' to 'value' ...'\n"
1489 " \"-guid\": set vendor guid\n"
1490 " \"-nv\": set non-volatile attribute\n"
1491 " \"-bs\": set boot-service attribute\n"
1492 " \"-rt\": set runtime attribute\n"
1493 " \"-at\": set time-based authentication attribute\n"
1494 " \"-a\": append-write\n"
1495 " \"-i addr,size\": use <addr,size> as variable's value\n"
1496 " \"-v\": verbose message\n"
1497 " - delete UEFI variable 'name' if 'value' not specified\n"
1499 "setenv [-f] name value ...\n"
1500 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1501 "setenv [-f] name\n"
1502 " - [forcibly] delete environment variable 'name'",
1506 #if defined(CONFIG_CMD_ASKENV)
1509 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1510 "get environment variables from stdin",
1511 "name [message] [size]\n"
1512 " - get environment variable 'name' from stdin (max 'size' chars)"
1516 #if defined(CONFIG_CMD_RUN)
1517 U_BOOT_CMD_COMPLETE(
1518 run, CONFIG_SYS_MAXARGS, 1, do_run,
1519 "run commands in an environment variable",
1521 " - run the commands in the environment variable(s) 'var'",
1525 #endif /* CONFIG_SPL_BUILD */