1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2013
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Copyright 2011 Freescale Semiconductor, Inc.
13 * Support for persistent environment data
15 * The "environment" is stored on external storage as a list of '\0'
16 * terminated "name=value" strings. The end of the list is marked by
17 * a double '\0'. The environment is preceded by a 32 bit CRC over
18 * the data part and, in case of redundant environment, a byte of
21 * This linearized representation will also be used before
22 * relocation, i. e. as long as we don't have a full C runtime
23 * environment. After that, we use a hash table.
31 #include <env_internal.h>
37 #include <u-boot/crc.h>
39 #include <linux/stddef.h>
40 #include <asm/byteorder.h>
43 DECLARE_GLOBAL_DATA_PTR;
45 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
46 defined(CONFIG_ENV_IS_IN_FLASH) || \
47 defined(CONFIG_ENV_IS_IN_MMC) || \
48 defined(CONFIG_ENV_IS_IN_FAT) || \
49 defined(CONFIG_ENV_IS_IN_EXT4) || \
50 defined(CONFIG_ENV_IS_IN_NAND) || \
51 defined(CONFIG_ENV_IS_IN_NVRAM) || \
52 defined(CONFIG_ENV_IS_IN_ONENAND) || \
53 defined(CONFIG_ENV_IS_IN_SATA) || \
54 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
55 defined(CONFIG_ENV_IS_IN_REMOTE) || \
56 defined(CONFIG_ENV_IS_IN_UBI)
58 #define ENV_IS_IN_DEVICE
62 #if !defined(ENV_IS_IN_DEVICE) && \
63 !defined(CONFIG_ENV_IS_NOWHERE)
64 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
65 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
69 * Maximum expected input data size for import command
71 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
74 * This variable is incremented on each do_env_set(), so it can
75 * be used via env_get_id() as an indication, if the environment
76 * has changed or not. So it is possible to reread an environment
77 * variable only if the environment was changed ... done so for
78 * example in NetInitLoop()
80 static int env_id = 1;
87 #ifndef CONFIG_SPL_BUILD
89 * Command interface: print one or all environment variables
91 * Returns 0 in case of error, or length of printed string
93 static int env_print(char *name, int flag)
98 if (name) { /* print a single name */
99 struct env_entry e, *ep;
103 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
106 len = printf("%s=%s\n", ep->key, ep->data);
110 /* print whole list */
111 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
119 /* should never happen */
120 printf("## Error: cannot export environment\n");
124 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
129 int env_flag = H_HIDE_DOT;
131 #if defined(CONFIG_CMD_NVEDIT_EFI)
132 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
133 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
136 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
139 env_flag &= ~H_HIDE_DOT;
143 /* print all env vars */
144 rcode = env_print(NULL, env_flag);
147 printf("\nEnvironment size: %d/%ld bytes\n",
148 rcode, (ulong)ENV_SIZE);
152 /* print selected env vars */
153 env_flag &= ~H_HIDE_DOT;
154 for (i = 1; i < argc; ++i) {
155 int rc = env_print(argv[i], env_flag);
157 printf("## Error: \"%s\" not defined\n", argv[i]);
165 #ifdef CONFIG_CMD_GREPENV
166 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
167 int argc, char *const argv[])
170 int len, grep_how, grep_what;
173 return CMD_RET_USAGE;
175 grep_how = H_MATCH_SUBSTR; /* default: substring search */
176 grep_what = H_MATCH_BOTH; /* default: grep names and values */
178 while (--argc > 0 && **++argv == '-') {
183 case 'e': /* use regex matching */
184 grep_how = H_MATCH_REGEX;
187 case 'n': /* grep for name */
188 grep_what = H_MATCH_KEY;
190 case 'v': /* grep for value */
191 grep_what = H_MATCH_DATA;
193 case 'b': /* grep for both */
194 grep_what = H_MATCH_BOTH;
199 return CMD_RET_USAGE;
205 len = hexport_r(&env_htab, '\n',
206 flag | grep_what | grep_how,
207 &res, 0, argc, argv);
220 #endif /* CONFIG_SPL_BUILD */
223 * Set a new environment variable,
224 * or replace or delete an existing one.
226 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
229 char *name, *value, *s;
230 struct env_entry e, *ep;
232 debug("Initial value for argc=%d\n", argc);
234 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
235 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
236 return do_env_set_efi(NULL, flag, --argc, ++argv);
239 while (argc > 1 && **(argv + 1) == '-') {
245 case 'f': /* force */
249 return CMD_RET_USAGE;
253 debug("Final value for argc=%d\n", argc);
256 if (strchr(name, '=')) {
257 printf("## Error: illegal character '='"
258 "in variable name \"%s\"\n", name);
265 if (argc < 3 || argv[2] == NULL) {
266 int rc = hdelete_r(name, &env_htab, env_flag);
271 * Insert / replace new value
273 for (i = 2, len = 0; i < argc; ++i)
274 len += strlen(argv[i]) + 1;
278 printf("## Can't malloc %d bytes\n", len);
281 for (i = 2, s = value; i < argc; ++i) {
284 while ((*s++ = *v++) != '\0')
293 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
296 printf("## Error inserting \"%s\" variable, errno=%d\n",
304 int env_set(const char *varname, const char *varvalue)
306 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
308 /* before import into hashtable */
309 if (!(gd->flags & GD_FLG_ENV_READY))
312 if (varvalue == NULL || varvalue[0] == '\0')
313 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
315 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
319 * Set an environment variable to an integer value
321 * @param varname Environment variable to set
322 * @param value Value to set it to
323 * @return 0 if ok, 1 on error
325 int env_set_ulong(const char *varname, ulong value)
327 /* TODO: this should be unsigned */
328 char *str = simple_itoa(value);
330 return env_set(varname, str);
334 * Set an environment variable to an value in hex
336 * @param varname Environment variable to set
337 * @param value Value to set it to
338 * @return 0 if ok, 1 on error
340 int env_set_hex(const char *varname, ulong value)
344 sprintf(str, "%lx", value);
345 return env_set(varname, str);
348 ulong env_get_hex(const char *varname, ulong default_val)
354 s = env_get(varname);
356 value = simple_strtoul(s, &endp, 16);
363 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
365 string_to_enetaddr(env_get(name), enetaddr);
366 return is_valid_ethaddr(enetaddr);
369 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
371 char buf[ARP_HLEN_ASCII + 1];
373 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
376 sprintf(buf, "%pM", enetaddr);
378 return env_set(name, buf);
381 #ifndef CONFIG_SPL_BUILD
382 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
386 return CMD_RET_USAGE;
388 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
392 * Prompt for environment variable
394 #if defined(CONFIG_CMD_ASKENV)
395 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
397 char message[CONFIG_SYS_CBSIZE];
398 int i, len, pos, size;
402 local_args[0] = argv[0];
403 local_args[1] = argv[1];
404 local_args[2] = NULL;
405 local_args[3] = NULL;
410 * env_ask envname [message1 ...] [size]
413 return CMD_RET_USAGE;
416 * We test the last argument if it can be converted
417 * into a decimal number. If yes, we assume it's
418 * the size. Otherwise we echo it as part of the
421 i = simple_strtoul(argv[argc - 1], &endptr, 10);
422 if (*endptr != '\0') { /* no size */
423 size = CONFIG_SYS_CBSIZE - 1;
424 } else { /* size given */
430 sprintf(message, "Please enter '%s': ", argv[1]);
432 /* env_ask envname message1 ... messagen [size] */
433 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
435 message[pos++] = ' ';
437 strncpy(message + pos, argv[i], sizeof(message) - pos);
438 pos += strlen(argv[i]);
440 if (pos < sizeof(message) - 1) {
441 message[pos++] = ' ';
444 message[CONFIG_SYS_CBSIZE - 1] = '\0';
447 if (size >= CONFIG_SYS_CBSIZE)
448 size = CONFIG_SYS_CBSIZE - 1;
453 /* prompt for input */
454 len = cli_readline(message);
457 console_buffer[size] = '\0';
460 if (console_buffer[0] != '\0') {
461 local_args[2] = console_buffer;
465 /* Continue calling setenv code */
466 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
470 #if defined(CONFIG_CMD_ENV_CALLBACK)
471 static int print_static_binding(const char *var_name, const char *callback_name,
474 printf("\t%-20s %-20s\n", var_name, callback_name);
479 static int print_active_callback(struct env_entry *entry)
481 struct env_clbk_tbl *clbkp;
485 if (entry->callback == NULL)
488 /* look up the callback in the linker-list */
489 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
490 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
493 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
494 if (entry->callback == clbkp->callback + gd->reloc_off)
496 if (entry->callback == clbkp->callback)
501 if (i == num_callbacks)
502 /* this should probably never happen, but just in case... */
503 printf("\t%-20s %p\n", entry->key, entry->callback);
505 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
511 * Print the callbacks available and what they are bound to
513 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
516 struct env_clbk_tbl *clbkp;
520 /* Print the available callbacks */
521 puts("Available callbacks:\n");
522 puts("\tCallback Name\n");
523 puts("\t-------------\n");
524 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
525 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
528 printf("\t%s\n", clbkp->name);
531 /* Print the static bindings that may exist */
532 puts("Static callback bindings:\n");
533 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
534 printf("\t%-20s %-20s\n", "-------------", "-------------");
535 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
538 /* walk through each variable and print the callback if it has one */
539 puts("Active callback bindings:\n");
540 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
541 printf("\t%-20s %-20s\n", "-------------", "-------------");
542 hwalk_r(&env_htab, print_active_callback);
547 #if defined(CONFIG_CMD_ENV_FLAGS)
548 static int print_static_flags(const char *var_name, const char *flags,
551 enum env_flags_vartype type = env_flags_parse_vartype(flags);
552 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
554 printf("\t%-20s %-20s %-20s\n", var_name,
555 env_flags_get_vartype_name(type),
556 env_flags_get_varaccess_name(access));
561 static int print_active_flags(struct env_entry *entry)
563 enum env_flags_vartype type;
564 enum env_flags_varaccess access;
566 if (entry->flags == 0)
569 type = (enum env_flags_vartype)
570 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
571 access = env_flags_parse_varaccess_from_binflags(entry->flags);
572 printf("\t%-20s %-20s %-20s\n", entry->key,
573 env_flags_get_vartype_name(type),
574 env_flags_get_varaccess_name(access));
580 * Print the flags available and what variables have flags
582 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
584 /* Print the available variable types */
585 printf("Available variable type flags (position %d):\n",
586 ENV_FLAGS_VARTYPE_LOC);
587 puts("\tFlag\tVariable Type Name\n");
588 puts("\t----\t------------------\n");
589 env_flags_print_vartypes();
592 /* Print the available variable access types */
593 printf("Available variable access flags (position %d):\n",
594 ENV_FLAGS_VARACCESS_LOC);
595 puts("\tFlag\tVariable Access Name\n");
596 puts("\t----\t--------------------\n");
597 env_flags_print_varaccess();
600 /* Print the static flags that may exist */
601 puts("Static flags:\n");
602 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
604 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
606 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
609 /* walk through each variable and print the flags if non-default */
610 puts("Active flags:\n");
611 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
613 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
615 hwalk_r(&env_htab, print_active_flags);
621 * Interactively edit an environment variable
623 #if defined(CONFIG_CMD_EDITENV)
624 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
627 char buffer[CONFIG_SYS_CBSIZE];
631 return CMD_RET_USAGE;
633 /* before import into hashtable */
634 if (!(gd->flags & GD_FLG_ENV_READY))
637 /* Set read buffer to initial value or empty sting */
638 init_val = env_get(argv[1]);
640 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
644 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
647 if (buffer[0] == '\0') {
648 const char * const _argv[3] = { "setenv", argv[1], NULL };
650 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
652 const char * const _argv[4] = { "setenv", argv[1], buffer,
655 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
658 #endif /* CONFIG_CMD_EDITENV */
659 #endif /* CONFIG_SPL_BUILD */
662 * Look up variable from environment,
663 * return address of storage for that variable,
664 * or NULL if not found
666 char *env_get(const char *name)
668 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
669 struct env_entry e, *ep;
675 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
677 return ep ? ep->data : NULL;
680 /* restricted capabilities before import */
681 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
682 return (char *)(gd->env_buf);
688 * Like env_get, but prints an error if envvar isn't defined in the
689 * environment. It always returns what env_get does, so it can be used in
690 * place of env_get without changing error handling otherwise.
692 char *from_env(const char *envvar)
696 ret = env_get(envvar);
699 printf("missing environment variable: %s\n", envvar);
705 * Look up variable from environment for restricted C runtime env.
707 int env_get_f(const char *name, char *buf, unsigned len)
711 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
714 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
717 if (nxt >= CONFIG_ENV_SIZE)
721 val = env_match((uchar *)name, i);
725 /* found; copy out */
726 for (n = 0; n < len; ++n, ++buf) {
727 c = env_get_char(val++);
738 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
748 * Decode the integer value of an environment variable and return it.
750 * @param name Name of environment variable
751 * @param base Number base to use (normally 10, or 16 for hex)
752 * @param default_val Default value to return if the variable is not
754 * @return the decoded value, or default_val if not found
756 ulong env_get_ulong(const char *name, int base, ulong default_val)
759 * We can use env_get() here, even before relocation, since the
760 * environment variable value is an integer and thus short.
762 const char *str = env_get(name);
764 return str ? simple_strtoul(str, NULL, base) : default_val;
767 #ifndef CONFIG_SPL_BUILD
768 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
769 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
772 return env_save() ? 1 : 0;
776 saveenv, 1, 0, do_env_save,
777 "save environment variables to persistent storage",
781 #if defined(CONFIG_CMD_ERASEENV)
782 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
785 return env_erase() ? 1 : 0;
789 eraseenv, 1, 0, do_env_erase,
790 "erase environment variables from persistent storage",
795 #endif /* CONFIG_SPL_BUILD */
797 int env_match(uchar *s1, int i2)
802 while (*s1 == env_get_char(i2++))
806 if (*s1 == '\0' && env_get_char(i2-1) == '=')
812 #ifndef CONFIG_SPL_BUILD
813 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
814 int argc, char *const argv[])
816 int all = 0, env_flag = H_INTERACTIVE;
818 debug("Initial value for argc=%d\n", argc);
819 while (--argc > 0 && **++argv == '-') {
824 case 'a': /* default all */
827 case 'f': /* force */
831 return cmd_usage(cmdtp);
835 debug("Final value for argc=%d\n", argc);
836 if (all && (argc == 0)) {
837 /* Reset the whole environment */
838 env_set_default("## Resetting to default environment\n",
842 if (!all && (argc > 0)) {
843 /* Reset individual variables */
844 env_set_default_vars(argc, argv, env_flag);
848 return cmd_usage(cmdtp);
851 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
852 int argc, char *const argv[])
854 int env_flag = H_INTERACTIVE;
857 debug("Initial value for argc=%d\n", argc);
858 while (argc > 1 && **(argv + 1) == '-') {
864 case 'f': /* force */
868 return CMD_RET_USAGE;
872 debug("Final value for argc=%d\n", argc);
877 char *name = *++argv;
879 if (!hdelete_r(name, &env_htab, env_flag))
886 #ifdef CONFIG_CMD_EXPORTENV
888 * env export [-t | -b | -c] [-s size] addr [var ...]
889 * -t: export as text format; if size is given, data will be
890 * padded with '\0' bytes; if not, one terminating '\0'
891 * will be added (which is included in the "filesize"
892 * setting so you can for exmple copy this to flash and
893 * keep the termination).
894 * -b: export as binary format (name=value pairs separated by
895 * '\0', list end marked by double "\0\0")
896 * -c: export as checksum protected environment format as
897 * used for example by "saveenv" command
899 * size of output buffer
900 * addr: memory address where environment gets stored
901 * var... List of variable names that get included into the
902 * export. Without arguments, the whole environment gets
905 * With "-c" and size is NOT given, then the export command will
906 * format the data as currently used for the persistent storage,
907 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
908 * prepend a valid CRC32 checksum and, in case of redundant
909 * environment, a "current" redundancy flag. If size is given, this
910 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
911 * checksum and redundancy flag will be inserted.
913 * With "-b" and "-t", always only the real data (including a
914 * terminating '\0' byte) will be written; here the optional size
915 * argument will be used to make sure not to overflow the user
916 * provided buffer; the command will abort if the size is not
917 * sufficient. Any remaining space will be '\0' padded.
919 * On successful return, the variable "filesize" will be set.
920 * Note that filesize includes the trailing/terminating '\0' byte(s).
922 * Usage scenario: create a text snapshot/backup of the current settings:
924 * => env export -t 100000
925 * => era ${backup_addr} +${filesize}
926 * => cp.b 100000 ${backup_addr} ${filesize}
928 * Re-import this snapshot, deleting all other settings:
930 * => env import -d -t ${backup_addr}
932 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
933 int argc, char *const argv[])
937 char *ptr, *cmd, *res;
947 while (--argc > 0 && **++argv == '-') {
951 case 'b': /* raw binary format */
956 case 'c': /* external checksum format */
962 case 's': /* size given */
964 return cmd_usage(cmdtp);
965 size = simple_strtoul(*++argv, NULL, 16);
967 case 't': /* text format */
973 return CMD_RET_USAGE;
980 return CMD_RET_USAGE;
982 addr = simple_strtoul(argv[0], NULL, 16);
983 ptr = map_sysmem(addr, size);
986 memset(ptr, '\0', size);
991 if (sep) { /* export as text file */
992 len = hexport_r(&env_htab, sep,
993 H_MATCH_KEY | H_MATCH_IDENT,
994 &ptr, size, argc, argv);
996 pr_err("## Error: Cannot export environment: errno = %d\n",
1000 sprintf(buf, "%zX", (size_t)len);
1001 env_set("filesize", buf);
1006 envp = (env_t *)ptr;
1008 if (chk) /* export as checksum protected block */
1009 res = (char *)envp->data;
1010 else /* export as raw binary data */
1013 len = hexport_r(&env_htab, '\0',
1014 H_MATCH_KEY | H_MATCH_IDENT,
1015 &res, ENV_SIZE, argc, argv);
1017 pr_err("## Error: Cannot export environment: errno = %d\n",
1023 envp->crc = crc32(0, envp->data,
1024 size ? size - offsetof(env_t, data) : ENV_SIZE);
1025 #ifdef CONFIG_ENV_ADDR_REDUND
1026 envp->flags = ENV_REDUND_ACTIVE;
1029 env_set_hex("filesize", len + offsetof(env_t, data));
1034 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1040 #ifdef CONFIG_CMD_IMPORTENV
1042 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1043 * -d: delete existing environment before importing if no var is
1044 * passed; if vars are passed, if one var is in the current
1045 * environment but not in the environment at addr, delete var from
1046 * current environment;
1047 * otherwise overwrite / append to existing definitions
1048 * -t: assume text format; either "size" must be given or the
1049 * text data must be '\0' terminated
1050 * -r: handle CRLF like LF, that means exported variables with
1051 * a content which ends with \r won't get imported. Used
1052 * to import text files created with editors which are using CRLF
1053 * for line endings. Only effective in addition to -t.
1054 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1055 * -c: assume checksum protected environment format
1056 * addr: memory address to read from
1057 * size: length of input data; if missing, proper '\0'
1058 * termination is mandatory
1059 * if var is set and size should be missing (i.e. '\0'
1060 * termination), set size to '-'
1061 * var... List of the names of the only variables that get imported from
1062 * the environment at address 'addr'. Without arguments, the whole
1063 * environment gets imported.
1065 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1066 int argc, char *const argv[])
1080 while (--argc > 0 && **++argv == '-') {
1084 case 'b': /* raw binary format */
1089 case 'c': /* external checksum format */
1095 case 't': /* text format */
1100 case 'r': /* handle CRLF like LF */
1107 return CMD_RET_USAGE;
1113 return CMD_RET_USAGE;
1116 printf("## Warning: defaulting to text format\n");
1118 if (sep != '\n' && crlf_is_lf )
1121 addr = simple_strtoul(argv[0], NULL, 16);
1122 ptr = map_sysmem(addr, 0);
1124 if (argc >= 2 && strcmp(argv[1], "-")) {
1125 size = simple_strtoul(argv[1], NULL, 16);
1127 puts("## Error: external checksum format must pass size\n");
1128 return CMD_RET_FAILURE;
1134 while (size < MAX_ENV_SIZE) {
1135 if ((*s == sep) && (*(s+1) == '\0'))
1140 if (size == MAX_ENV_SIZE) {
1141 printf("## Warning: Input data exceeds %d bytes"
1142 " - truncated\n", MAX_ENV_SIZE);
1145 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1153 env_t *ep = (env_t *)ptr;
1155 size -= offsetof(env_t, data);
1156 memcpy(&crc, &ep->crc, sizeof(crc));
1158 if (crc32(0, ep->data, size) != crc) {
1159 puts("## Error: bad CRC, import failed\n");
1162 ptr = (char *)ep->data;
1165 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1166 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1167 pr_err("## Error: Environment import failed: errno = %d\n",
1171 gd->flags |= GD_FLG_ENV_READY;
1176 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1182 #if defined(CONFIG_CMD_NVEDIT_INFO)
1184 * print_env_info - print environment information
1186 static int print_env_info(void)
1190 /* print environment validity value */
1191 switch (gd->env_valid) {
1199 value = "redundant";
1205 printf("env_valid = %s\n", value);
1207 /* print environment ready flag */
1208 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1209 printf("env_ready = %s\n", value);
1211 /* print environment using default flag */
1212 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1213 printf("env_use_default = %s\n", value);
1215 return CMD_RET_SUCCESS;
1218 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1219 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1222 * env info - display environment information
1223 * env info [-d] - evaluate whether default environment is used
1224 * env info [-p] - evaluate whether environment can be persisted
1226 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1227 int argc, char *const argv[])
1230 int eval_results = 0;
1232 /* display environment information */
1234 return print_env_info();
1236 /* process options */
1237 while (--argc > 0 && **++argv == '-') {
1243 eval_flags |= ENV_INFO_IS_DEFAULT;
1246 eval_flags |= ENV_INFO_IS_PERSISTED;
1249 return CMD_RET_USAGE;
1254 /* evaluate whether default environment is used */
1255 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1256 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1257 printf("Default environment is used\n");
1258 eval_results |= ENV_INFO_IS_DEFAULT;
1260 printf("Environment was loaded from persistent storage\n");
1264 /* evaluate whether environment can be persisted */
1265 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1266 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1267 printf("Environment can be persisted\n");
1268 eval_results |= ENV_INFO_IS_PERSISTED;
1270 printf("Environment cannot be persisted\n");
1274 /* The result of evaluations is combined with AND */
1275 if (eval_flags != eval_results)
1276 return CMD_RET_FAILURE;
1278 return CMD_RET_SUCCESS;
1282 #if defined(CONFIG_CMD_ENV_EXISTS)
1283 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1286 struct env_entry e, *ep;
1289 return CMD_RET_USAGE;
1293 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1295 return (ep == NULL) ? 1 : 0;
1300 * New command line interface: "env" command with subcommands
1302 static struct cmd_tbl cmd_env_sub[] = {
1303 #if defined(CONFIG_CMD_ASKENV)
1304 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1306 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1307 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1308 #if defined(CONFIG_CMD_EDITENV)
1309 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1311 #if defined(CONFIG_CMD_ENV_CALLBACK)
1312 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1314 #if defined(CONFIG_CMD_ENV_FLAGS)
1315 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1317 #if defined(CONFIG_CMD_EXPORTENV)
1318 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1320 #if defined(CONFIG_CMD_GREPENV)
1321 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1323 #if defined(CONFIG_CMD_IMPORTENV)
1324 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1326 #if defined(CONFIG_CMD_NVEDIT_INFO)
1327 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1329 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1330 #if defined(CONFIG_CMD_RUN)
1331 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1333 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1334 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1335 #if defined(CONFIG_CMD_ERASEENV)
1336 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1339 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1340 #if defined(CONFIG_CMD_ENV_EXISTS)
1341 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1345 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1346 void env_reloc(void)
1348 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1352 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1357 return CMD_RET_USAGE;
1359 /* drop initial "env" arg */
1363 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1366 return cp->cmd(cmdtp, flag, argc, argv);
1368 return CMD_RET_USAGE;
1371 #ifdef CONFIG_SYS_LONGHELP
1372 static char env_help_text[] =
1373 #if defined(CONFIG_CMD_ASKENV)
1374 "ask name [message] [size] - ask for environment variable\nenv "
1376 #if defined(CONFIG_CMD_ENV_CALLBACK)
1377 "callbacks - print callbacks and their associated variables\nenv "
1379 "default [-f] -a - [forcibly] reset default environment\n"
1380 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1381 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1382 #if defined(CONFIG_CMD_EDITENV)
1383 "env edit name - edit environment variable\n"
1385 #if defined(CONFIG_CMD_ENV_EXISTS)
1386 "env exists name - tests for existence of variable\n"
1388 #if defined(CONFIG_CMD_EXPORTENV)
1389 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1391 #if defined(CONFIG_CMD_ENV_FLAGS)
1392 "env flags - print variables that have non-default flags\n"
1394 #if defined(CONFIG_CMD_GREPENV)
1396 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1398 "env grep [-n | -v | -b] string [...] - search environment\n"
1401 #if defined(CONFIG_CMD_IMPORTENV)
1402 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1404 #if defined(CONFIG_CMD_NVEDIT_INFO)
1405 "env info - display environment information\n"
1406 "env info [-d] - whether default environment is used\n"
1407 "env info [-p] - whether environment can be persisted\n"
1409 "env print [-a | name ...] - print environment\n"
1410 #if defined(CONFIG_CMD_NVEDIT_EFI)
1411 "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
1413 #if defined(CONFIG_CMD_RUN)
1414 "env run var [...] - run commands in an environment variable\n"
1416 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1417 "env save - save environment\n"
1418 #if defined(CONFIG_CMD_ERASEENV)
1419 "env erase - erase environment\n"
1422 #if defined(CONFIG_CMD_NVEDIT_EFI)
1423 "env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n"
1424 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1426 "env set [-f] name [arg ...]\n";
1430 env, CONFIG_SYS_MAXARGS, 1, do_env,
1431 "environment handling commands", env_help_text
1435 * Old command line interface, kept for compatibility
1438 #if defined(CONFIG_CMD_EDITENV)
1439 U_BOOT_CMD_COMPLETE(
1440 editenv, 2, 0, do_env_edit,
1441 "edit environment variable",
1443 " - edit environment variable 'name'",
1448 U_BOOT_CMD_COMPLETE(
1449 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1450 "print environment variables",
1451 "[-a]\n - print [all] values of all environment variables\n"
1452 #if defined(CONFIG_CMD_NVEDIT_EFI)
1453 "printenv -e [-guid guid|-all][-n] [name ...]\n"
1454 " - print UEFI variable 'name' or all the variables\n"
1455 " \"-n\": suppress dumping variable's value\n"
1457 "printenv name ...\n"
1458 " - print value of environment variable 'name'",
1462 #ifdef CONFIG_CMD_GREPENV
1463 U_BOOT_CMD_COMPLETE(
1464 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1465 "search environment variables",
1467 "[-e] [-n | -v | -b] string ...\n"
1469 "[-n | -v | -b] string ...\n"
1471 " - list environment name=value pairs matching 'string'\n"
1473 " \"-e\": enable regular expressions;\n"
1475 " \"-n\": search variable names; \"-v\": search values;\n"
1476 " \"-b\": search both names and values (default)",
1481 U_BOOT_CMD_COMPLETE(
1482 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1483 "set environment variables",
1484 #if defined(CONFIG_CMD_NVEDIT_EFI)
1485 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1486 " [-i addr,size name], or [name [value ...]]\n"
1487 " - set UEFI variable 'name' to 'value' ...'\n"
1488 " \"-guid\": set vendor guid\n"
1489 " \"-nv\": set non-volatile attribute\n"
1490 " \"-bs\": set boot-service attribute\n"
1491 " \"-rt\": set runtime attribute\n"
1492 " \"-at\": set time-based authentication attribute\n"
1493 " \"-a\": append-write\n"
1494 " \"-i addr,size\": use <addr,size> as variable's value\n"
1495 " \"-v\": verbose message\n"
1496 " - delete UEFI variable 'name' if 'value' not specified\n"
1498 "setenv [-f] name value ...\n"
1499 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1500 "setenv [-f] name\n"
1501 " - [forcibly] delete environment variable 'name'",
1505 #if defined(CONFIG_CMD_ASKENV)
1508 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1509 "get environment variables from stdin",
1510 "name [message] [size]\n"
1511 " - get environment variable 'name' from stdin (max 'size' chars)"
1515 #if defined(CONFIG_CMD_RUN)
1516 U_BOOT_CMD_COMPLETE(
1517 run, CONFIG_SYS_MAXARGS, 1, do_run,
1518 "run commands in an environment variable",
1520 " - run the commands in the environment variable(s) 'var'",
1524 #endif /* CONFIG_SPL_BUILD */