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 = env_match((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 */
788 int env_match(uchar *s1, int i2)
793 while (*s1 == env_get_char(i2++))
797 if (*s1 == '\0' && env_get_char(i2-1) == '=')
803 #ifndef CONFIG_SPL_BUILD
804 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
805 int argc, char * const argv[])
807 int all = 0, env_flag = H_INTERACTIVE;
809 debug("Initial value for argc=%d\n", argc);
810 while (--argc > 0 && **++argv == '-') {
815 case 'a': /* default all */
818 case 'f': /* force */
822 return cmd_usage(cmdtp);
826 debug("Final value for argc=%d\n", argc);
827 if (all && (argc == 0)) {
828 /* Reset the whole environment */
829 set_default_env("## Resetting to default environment\n",
833 if (!all && (argc > 0)) {
834 /* Reset individual variables */
835 set_default_vars(argc, argv, env_flag);
839 return cmd_usage(cmdtp);
842 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
843 int argc, char * const argv[])
845 int env_flag = H_INTERACTIVE;
848 debug("Initial value for argc=%d\n", argc);
849 while (argc > 1 && **(argv + 1) == '-') {
855 case 'f': /* force */
859 return CMD_RET_USAGE;
863 debug("Final value for argc=%d\n", argc);
868 char *name = *++argv;
870 if (!hdelete_r(name, &env_htab, env_flag))
877 #ifdef CONFIG_CMD_EXPORTENV
879 * env export [-t | -b | -c] [-s size] addr [var ...]
880 * -t: export as text format; if size is given, data will be
881 * padded with '\0' bytes; if not, one terminating '\0'
882 * will be added (which is included in the "filesize"
883 * setting so you can for exmple copy this to flash and
884 * keep the termination).
885 * -b: export as binary format (name=value pairs separated by
886 * '\0', list end marked by double "\0\0")
887 * -c: export as checksum protected environment format as
888 * used for example by "saveenv" command
890 * size of output buffer
891 * addr: memory address where environment gets stored
892 * var... List of variable names that get included into the
893 * export. Without arguments, the whole environment gets
896 * With "-c" and size is NOT given, then the export command will
897 * format the data as currently used for the persistent storage,
898 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
899 * prepend a valid CRC32 checksum and, in case of redundant
900 * environment, a "current" redundancy flag. If size is given, this
901 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
902 * checksum and redundancy flag will be inserted.
904 * With "-b" and "-t", always only the real data (including a
905 * terminating '\0' byte) will be written; here the optional size
906 * argument will be used to make sure not to overflow the user
907 * provided buffer; the command will abort if the size is not
908 * sufficient. Any remaining space will be '\0' padded.
910 * On successful return, the variable "filesize" will be set.
911 * Note that filesize includes the trailing/terminating '\0' byte(s).
913 * Usage scenario: create a text snapshot/backup of the current settings:
915 * => env export -t 100000
916 * => era ${backup_addr} +${filesize}
917 * => cp.b 100000 ${backup_addr} ${filesize}
919 * Re-import this snapshot, deleting all other settings:
921 * => env import -d -t ${backup_addr}
923 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
924 int argc, char * const argv[])
928 char *ptr, *cmd, *res;
938 while (--argc > 0 && **++argv == '-') {
942 case 'b': /* raw binary format */
947 case 'c': /* external checksum format */
953 case 's': /* size given */
955 return cmd_usage(cmdtp);
956 size = simple_strtoul(*++argv, NULL, 16);
958 case 't': /* text format */
964 return CMD_RET_USAGE;
971 return CMD_RET_USAGE;
973 addr = simple_strtoul(argv[0], NULL, 16);
974 ptr = map_sysmem(addr, size);
977 memset(ptr, '\0', size);
982 if (sep) { /* export as text file */
983 len = hexport_r(&env_htab, sep,
984 H_MATCH_KEY | H_MATCH_IDENT,
985 &ptr, size, argc, argv);
987 pr_err("## Error: Cannot export environment: errno = %d\n",
991 sprintf(buf, "%zX", (size_t)len);
992 env_set("filesize", buf);
999 if (chk) /* export as checksum protected block */
1000 res = (char *)envp->data;
1001 else /* export as raw binary data */
1004 len = hexport_r(&env_htab, '\0',
1005 H_MATCH_KEY | H_MATCH_IDENT,
1006 &res, ENV_SIZE, argc, argv);
1008 pr_err("## Error: Cannot export environment: errno = %d\n",
1014 envp->crc = crc32(0, envp->data,
1015 size ? size - offsetof(env_t, data) : ENV_SIZE);
1016 #ifdef CONFIG_ENV_ADDR_REDUND
1017 envp->flags = ACTIVE_FLAG;
1020 env_set_hex("filesize", len + offsetof(env_t, data));
1025 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1031 #ifdef CONFIG_CMD_IMPORTENV
1033 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1034 * -d: delete existing environment before importing if no var is
1035 * passed; if vars are passed, if one var is in the current
1036 * environment but not in the environment at addr, delete var from
1037 * current environment;
1038 * otherwise overwrite / append to existing definitions
1039 * -t: assume text format; either "size" must be given or the
1040 * text data must be '\0' terminated
1041 * -r: handle CRLF like LF, that means exported variables with
1042 * a content which ends with \r won't get imported. Used
1043 * to import text files created with editors which are using CRLF
1044 * for line endings. Only effective in addition to -t.
1045 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1046 * -c: assume checksum protected environment format
1047 * addr: memory address to read from
1048 * size: length of input data; if missing, proper '\0'
1049 * termination is mandatory
1050 * if var is set and size should be missing (i.e. '\0'
1051 * termination), set size to '-'
1052 * var... List of the names of the only variables that get imported from
1053 * the environment at address 'addr'. Without arguments, the whole
1054 * environment gets imported.
1056 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1057 int argc, char * const argv[])
1071 while (--argc > 0 && **++argv == '-') {
1075 case 'b': /* raw binary format */
1080 case 'c': /* external checksum format */
1086 case 't': /* text format */
1091 case 'r': /* handle CRLF like LF */
1098 return CMD_RET_USAGE;
1104 return CMD_RET_USAGE;
1107 printf("## Warning: defaulting to text format\n");
1109 if (sep != '\n' && crlf_is_lf )
1112 addr = simple_strtoul(argv[0], NULL, 16);
1113 ptr = map_sysmem(addr, 0);
1115 if (argc >= 2 && strcmp(argv[1], "-")) {
1116 size = simple_strtoul(argv[1], NULL, 16);
1118 puts("## Error: external checksum format must pass size\n");
1119 return CMD_RET_FAILURE;
1125 while (size < MAX_ENV_SIZE) {
1126 if ((*s == sep) && (*(s+1) == '\0'))
1131 if (size == MAX_ENV_SIZE) {
1132 printf("## Warning: Input data exceeds %d bytes"
1133 " - truncated\n", MAX_ENV_SIZE);
1136 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1144 env_t *ep = (env_t *)ptr;
1146 size -= offsetof(env_t, data);
1147 memcpy(&crc, &ep->crc, sizeof(crc));
1149 if (crc32(0, ep->data, size) != crc) {
1150 puts("## Error: bad CRC, import failed\n");
1153 ptr = (char *)ep->data;
1156 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1157 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1158 pr_err("## Error: Environment import failed: errno = %d\n",
1162 gd->flags |= GD_FLG_ENV_READY;
1167 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1173 #if defined(CONFIG_CMD_NVEDIT_INFO)
1175 * print_env_info - print environment information
1177 static int print_env_info(void)
1181 /* print environment validity value */
1182 switch (gd->env_valid) {
1190 value = "redundant";
1196 printf("env_valid = %s\n", value);
1198 /* print environment ready flag */
1199 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1200 printf("env_ready = %s\n", value);
1202 /* print environment using default flag */
1203 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1204 printf("env_use_default = %s\n", value);
1206 return CMD_RET_SUCCESS;
1209 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1210 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1213 * env info - display environment information
1214 * env info [-d] - evaluate whether default environment is used
1215 * env info [-p] - evaluate whether environment can be persisted
1217 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1218 int argc, char * const argv[])
1221 int eval_results = 0;
1223 /* display environment information */
1225 return print_env_info();
1227 /* process options */
1228 while (--argc > 0 && **++argv == '-') {
1234 eval_flags |= ENV_INFO_IS_DEFAULT;
1237 eval_flags |= ENV_INFO_IS_PERSISTED;
1240 return CMD_RET_USAGE;
1245 /* evaluate whether default environment is used */
1246 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1247 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1248 printf("Default environment is used\n");
1249 eval_results |= ENV_INFO_IS_DEFAULT;
1251 printf("Environment was loaded from persistent storage\n");
1255 /* evaluate whether environment can be persisted */
1256 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1257 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1258 printf("Environment can be persisted\n");
1259 eval_results |= ENV_INFO_IS_PERSISTED;
1261 printf("Environment cannot be persisted\n");
1265 /* The result of evaluations is combined with AND */
1266 if (eval_flags != eval_results)
1267 return CMD_RET_FAILURE;
1269 return CMD_RET_SUCCESS;
1273 #if defined(CONFIG_CMD_ENV_EXISTS)
1274 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1275 char * const argv[])
1280 return CMD_RET_USAGE;
1284 hsearch_r(e, FIND, &ep, &env_htab, 0);
1286 return (ep == NULL) ? 1 : 0;
1291 * New command line interface: "env" command with subcommands
1293 static cmd_tbl_t cmd_env_sub[] = {
1294 #if defined(CONFIG_CMD_ASKENV)
1295 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1297 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1298 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1299 #if defined(CONFIG_CMD_EDITENV)
1300 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1302 #if defined(CONFIG_CMD_ENV_CALLBACK)
1303 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1305 #if defined(CONFIG_CMD_ENV_FLAGS)
1306 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1308 #if defined(CONFIG_CMD_EXPORTENV)
1309 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1311 #if defined(CONFIG_CMD_GREPENV)
1312 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1314 #if defined(CONFIG_CMD_IMPORTENV)
1315 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1317 #if defined(CONFIG_CMD_NVEDIT_INFO)
1318 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1320 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1321 #if defined(CONFIG_CMD_RUN)
1322 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1324 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1325 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1326 #if defined(CONFIG_CMD_ERASEENV)
1327 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1330 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1331 #if defined(CONFIG_CMD_ENV_EXISTS)
1332 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1336 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1337 void env_reloc(void)
1339 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1343 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1348 return CMD_RET_USAGE;
1350 /* drop initial "env" arg */
1354 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1357 return cp->cmd(cmdtp, flag, argc, argv);
1359 return CMD_RET_USAGE;
1362 #ifdef CONFIG_SYS_LONGHELP
1363 static char env_help_text[] =
1364 #if defined(CONFIG_CMD_ASKENV)
1365 "ask name [message] [size] - ask for environment variable\nenv "
1367 #if defined(CONFIG_CMD_ENV_CALLBACK)
1368 "callbacks - print callbacks and their associated variables\nenv "
1370 "default [-f] -a - [forcibly] reset default environment\n"
1371 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1372 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1373 #if defined(CONFIG_CMD_EDITENV)
1374 "env edit name - edit environment variable\n"
1376 #if defined(CONFIG_CMD_ENV_EXISTS)
1377 "env exists name - tests for existence of variable\n"
1379 #if defined(CONFIG_CMD_EXPORTENV)
1380 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1382 #if defined(CONFIG_CMD_ENV_FLAGS)
1383 "env flags - print variables that have non-default flags\n"
1385 #if defined(CONFIG_CMD_GREPENV)
1387 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1389 "env grep [-n | -v | -b] string [...] - search environment\n"
1392 #if defined(CONFIG_CMD_IMPORTENV)
1393 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1395 #if defined(CONFIG_CMD_NVEDIT_INFO)
1396 "env info - display environment information\n"
1397 "env info [-d] - whether default environment is used\n"
1398 "env info [-p] - whether environment can be persisted\n"
1400 "env print [-a | name ...] - print environment\n"
1401 #if defined(CONFIG_CMD_NVEDIT_EFI)
1402 "env print -e [name ...] - print UEFI environment\n"
1404 #if defined(CONFIG_CMD_RUN)
1405 "env run var [...] - run commands in an environment variable\n"
1407 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1408 "env save - save environment\n"
1409 #if defined(CONFIG_CMD_ERASEENV)
1410 "env erase - erase environment\n"
1413 #if defined(CONFIG_CMD_NVEDIT_EFI)
1414 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1416 "env set [-f] name [arg ...]\n";
1420 env, CONFIG_SYS_MAXARGS, 1, do_env,
1421 "environment handling commands", env_help_text
1425 * Old command line interface, kept for compatibility
1428 #if defined(CONFIG_CMD_EDITENV)
1429 U_BOOT_CMD_COMPLETE(
1430 editenv, 2, 0, do_env_edit,
1431 "edit environment variable",
1433 " - edit environment variable 'name'",
1438 U_BOOT_CMD_COMPLETE(
1439 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1440 "print environment variables",
1441 "[-a]\n - print [all] values of all environment variables\n"
1442 #if defined(CONFIG_CMD_NVEDIT_EFI)
1443 "printenv -e [name ...]\n"
1444 " - print UEFI variable 'name' or all the variables\n"
1446 "printenv name ...\n"
1447 " - print value of environment variable 'name'",
1451 #ifdef CONFIG_CMD_GREPENV
1452 U_BOOT_CMD_COMPLETE(
1453 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1454 "search environment variables",
1456 "[-e] [-n | -v | -b] string ...\n"
1458 "[-n | -v | -b] string ...\n"
1460 " - list environment name=value pairs matching 'string'\n"
1462 " \"-e\": enable regular expressions;\n"
1464 " \"-n\": search variable names; \"-v\": search values;\n"
1465 " \"-b\": search both names and values (default)",
1470 U_BOOT_CMD_COMPLETE(
1471 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1472 "set environment variables",
1473 #if defined(CONFIG_CMD_NVEDIT_EFI)
1474 "-e [-nv] name [value ...]\n"
1475 " - set UEFI variable 'name' to 'value' ...'\n"
1476 " 'nv' option makes the variable non-volatile\n"
1477 " - delete UEFI variable 'name' if 'value' not specified\n"
1479 "setenv [-f] name value ...\n"
1480 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1481 "setenv [-f] name\n"
1482 " - [forcibly] delete environment variable 'name'",
1486 #if defined(CONFIG_CMD_ASKENV)
1489 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1490 "get environment variables from stdin",
1491 "name [message] [size]\n"
1492 " - get environment variable 'name' from stdin (max 'size' chars)"
1496 #if defined(CONFIG_CMD_RUN)
1497 U_BOOT_CMD_COMPLETE(
1498 run, CONFIG_SYS_MAXARGS, 1, do_run,
1499 "run commands in an environment variable",
1501 " - run the commands in the environment variable(s) 'var'",
1505 #endif /* CONFIG_SPL_BUILD */