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 <linux/bitops.h>
39 #include <u-boot/crc.h>
41 #include <linux/stddef.h>
42 #include <asm/byteorder.h>
45 DECLARE_GLOBAL_DATA_PTR;
47 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
48 defined(CONFIG_ENV_IS_IN_FLASH) || \
49 defined(CONFIG_ENV_IS_IN_MMC) || \
50 defined(CONFIG_ENV_IS_IN_FAT) || \
51 defined(CONFIG_ENV_IS_IN_EXT4) || \
52 defined(CONFIG_ENV_IS_IN_NAND) || \
53 defined(CONFIG_ENV_IS_IN_NVRAM) || \
54 defined(CONFIG_ENV_IS_IN_ONENAND) || \
55 defined(CONFIG_ENV_IS_IN_SATA) || \
56 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
57 defined(CONFIG_ENV_IS_IN_REMOTE) || \
58 defined(CONFIG_ENV_IS_IN_UBI)
60 #define ENV_IS_IN_DEVICE
64 #if !defined(ENV_IS_IN_DEVICE) && \
65 !defined(CONFIG_ENV_IS_NOWHERE)
66 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
67 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
71 * Maximum expected input data size for import command
73 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
76 * This variable is incremented on each do_env_set(), so it can
77 * be used via env_get_id() as an indication, if the environment
78 * has changed or not. So it is possible to reread an environment
79 * variable only if the environment was changed ... done so for
80 * example in NetInitLoop()
82 static int env_id = 1;
89 #ifndef CONFIG_SPL_BUILD
91 * Command interface: print one or all environment variables
93 * Returns 0 in case of error, or length of printed string
95 static int env_print(char *name, int flag)
100 if (name) { /* print a single name */
101 struct env_entry e, *ep;
105 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
108 len = printf("%s=%s\n", ep->key, ep->data);
112 /* print whole list */
113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
121 /* should never happen */
122 printf("## Error: cannot export environment\n");
126 static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
131 int env_flag = H_HIDE_DOT;
133 #if defined(CONFIG_CMD_NVEDIT_EFI)
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
135 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
138 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
141 env_flag &= ~H_HIDE_DOT;
145 /* print all env vars */
146 rcode = env_print(NULL, env_flag);
149 printf("\nEnvironment size: %d/%ld bytes\n",
150 rcode, (ulong)ENV_SIZE);
154 /* print selected env vars */
155 env_flag &= ~H_HIDE_DOT;
156 for (i = 1; i < argc; ++i) {
157 int rc = env_print(argv[i], env_flag);
159 printf("## Error: \"%s\" not defined\n", argv[i]);
167 #ifdef CONFIG_CMD_GREPENV
168 static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
169 int argc, char *const argv[])
172 int len, grep_how, grep_what;
175 return CMD_RET_USAGE;
177 grep_how = H_MATCH_SUBSTR; /* default: substring search */
178 grep_what = H_MATCH_BOTH; /* default: grep names and values */
180 while (--argc > 0 && **++argv == '-') {
185 case 'e': /* use regex matching */
186 grep_how = H_MATCH_REGEX;
189 case 'n': /* grep for name */
190 grep_what = H_MATCH_KEY;
192 case 'v': /* grep for value */
193 grep_what = H_MATCH_DATA;
195 case 'b': /* grep for both */
196 grep_what = H_MATCH_BOTH;
201 return CMD_RET_USAGE;
207 len = hexport_r(&env_htab, '\n',
208 flag | grep_what | grep_how,
209 &res, 0, argc, argv);
222 #endif /* CONFIG_SPL_BUILD */
225 * Set a new environment variable,
226 * or replace or delete an existing one.
228 static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
231 char *name, *value, *s;
232 struct env_entry e, *ep;
234 debug("Initial value for argc=%d\n", argc);
236 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
237 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
238 return do_env_set_efi(NULL, flag, --argc, ++argv);
241 while (argc > 1 && **(argv + 1) == '-') {
247 case 'f': /* force */
251 return CMD_RET_USAGE;
255 debug("Final value for argc=%d\n", argc);
258 if (strchr(name, '=')) {
259 printf("## Error: illegal character '='"
260 "in variable name \"%s\"\n", name);
267 if (argc < 3 || argv[2] == NULL) {
268 int rc = hdelete_r(name, &env_htab, env_flag);
270 /* If the variable didn't exist, don't report an error */
271 return rc && rc != -ENOENT ? 1 : 0;
275 * Insert / replace new value
277 for (i = 2, len = 0; i < argc; ++i)
278 len += strlen(argv[i]) + 1;
282 printf("## Can't malloc %d bytes\n", len);
285 for (i = 2, s = value; i < argc; ++i) {
288 while ((*s++ = *v++) != '\0')
297 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
300 printf("## Error inserting \"%s\" variable, errno=%d\n",
308 int env_set(const char *varname, const char *varvalue)
310 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
312 /* before import into hashtable */
313 if (!(gd->flags & GD_FLG_ENV_READY))
316 if (varvalue == NULL || varvalue[0] == '\0')
317 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
319 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
323 * Set an environment variable to an integer value
325 * @param varname Environment variable to set
326 * @param value Value to set it to
327 * @return 0 if ok, 1 on error
329 int env_set_ulong(const char *varname, ulong value)
331 /* TODO: this should be unsigned */
332 char *str = simple_itoa(value);
334 return env_set(varname, str);
338 * Set an environment variable to an value in hex
340 * @param varname Environment variable to set
341 * @param value Value to set it to
342 * @return 0 if ok, 1 on error
344 int env_set_hex(const char *varname, ulong value)
348 sprintf(str, "%lx", value);
349 return env_set(varname, str);
352 ulong env_get_hex(const char *varname, ulong default_val)
358 s = env_get(varname);
360 value = simple_strtoul(s, &endp, 16);
367 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
369 string_to_enetaddr(env_get(name), enetaddr);
370 return is_valid_ethaddr(enetaddr);
373 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
375 char buf[ARP_HLEN_ASCII + 1];
377 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
380 sprintf(buf, "%pM", enetaddr);
382 return env_set(name, buf);
385 #ifndef CONFIG_SPL_BUILD
386 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
390 return CMD_RET_USAGE;
392 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
396 * Prompt for environment variable
398 #if defined(CONFIG_CMD_ASKENV)
399 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
401 char message[CONFIG_SYS_CBSIZE];
402 int i, len, pos, size;
406 local_args[0] = argv[0];
407 local_args[1] = argv[1];
408 local_args[2] = NULL;
409 local_args[3] = NULL;
414 * env_ask envname [message1 ...] [size]
417 return CMD_RET_USAGE;
420 * We test the last argument if it can be converted
421 * into a decimal number. If yes, we assume it's
422 * the size. Otherwise we echo it as part of the
425 i = simple_strtoul(argv[argc - 1], &endptr, 10);
426 if (*endptr != '\0') { /* no size */
427 size = CONFIG_SYS_CBSIZE - 1;
428 } else { /* size given */
434 sprintf(message, "Please enter '%s': ", argv[1]);
436 /* env_ask envname message1 ... messagen [size] */
437 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
439 message[pos++] = ' ';
441 strncpy(message + pos, argv[i], sizeof(message) - pos);
442 pos += strlen(argv[i]);
444 if (pos < sizeof(message) - 1) {
445 message[pos++] = ' ';
448 message[CONFIG_SYS_CBSIZE - 1] = '\0';
451 if (size >= CONFIG_SYS_CBSIZE)
452 size = CONFIG_SYS_CBSIZE - 1;
457 /* prompt for input */
458 len = cli_readline(message);
461 console_buffer[size] = '\0';
464 if (console_buffer[0] != '\0') {
465 local_args[2] = console_buffer;
469 /* Continue calling setenv code */
470 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
474 #if defined(CONFIG_CMD_ENV_CALLBACK)
475 static int print_static_binding(const char *var_name, const char *callback_name,
478 printf("\t%-20s %-20s\n", var_name, callback_name);
483 static int print_active_callback(struct env_entry *entry)
485 struct env_clbk_tbl *clbkp;
489 if (entry->callback == NULL)
492 /* look up the callback in the linker-list */
493 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
494 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
497 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
498 if (entry->callback == clbkp->callback + gd->reloc_off)
500 if (entry->callback == clbkp->callback)
505 if (i == num_callbacks)
506 /* this should probably never happen, but just in case... */
507 printf("\t%-20s %p\n", entry->key, entry->callback);
509 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
515 * Print the callbacks available and what they are bound to
517 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
520 struct env_clbk_tbl *clbkp;
524 /* Print the available callbacks */
525 puts("Available callbacks:\n");
526 puts("\tCallback Name\n");
527 puts("\t-------------\n");
528 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
529 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
532 printf("\t%s\n", clbkp->name);
535 /* Print the static bindings that may exist */
536 puts("Static callback bindings:\n");
537 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
538 printf("\t%-20s %-20s\n", "-------------", "-------------");
539 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
542 /* walk through each variable and print the callback if it has one */
543 puts("Active callback bindings:\n");
544 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
545 printf("\t%-20s %-20s\n", "-------------", "-------------");
546 hwalk_r(&env_htab, print_active_callback);
551 #if defined(CONFIG_CMD_ENV_FLAGS)
552 static int print_static_flags(const char *var_name, const char *flags,
555 enum env_flags_vartype type = env_flags_parse_vartype(flags);
556 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
558 printf("\t%-20s %-20s %-20s\n", var_name,
559 env_flags_get_vartype_name(type),
560 env_flags_get_varaccess_name(access));
565 static int print_active_flags(struct env_entry *entry)
567 enum env_flags_vartype type;
568 enum env_flags_varaccess access;
570 if (entry->flags == 0)
573 type = (enum env_flags_vartype)
574 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
575 access = env_flags_parse_varaccess_from_binflags(entry->flags);
576 printf("\t%-20s %-20s %-20s\n", entry->key,
577 env_flags_get_vartype_name(type),
578 env_flags_get_varaccess_name(access));
584 * Print the flags available and what variables have flags
586 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
588 /* Print the available variable types */
589 printf("Available variable type flags (position %d):\n",
590 ENV_FLAGS_VARTYPE_LOC);
591 puts("\tFlag\tVariable Type Name\n");
592 puts("\t----\t------------------\n");
593 env_flags_print_vartypes();
596 /* Print the available variable access types */
597 printf("Available variable access flags (position %d):\n",
598 ENV_FLAGS_VARACCESS_LOC);
599 puts("\tFlag\tVariable Access Name\n");
600 puts("\t----\t--------------------\n");
601 env_flags_print_varaccess();
604 /* Print the static flags that may exist */
605 puts("Static flags:\n");
606 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
608 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
610 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
613 /* walk through each variable and print the flags if non-default */
614 puts("Active flags:\n");
615 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
617 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
619 hwalk_r(&env_htab, print_active_flags);
625 * Interactively edit an environment variable
627 #if defined(CONFIG_CMD_EDITENV)
628 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
631 char buffer[CONFIG_SYS_CBSIZE];
635 return CMD_RET_USAGE;
637 /* before import into hashtable */
638 if (!(gd->flags & GD_FLG_ENV_READY))
641 /* Set read buffer to initial value or empty sting */
642 init_val = env_get(argv[1]);
644 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
648 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
651 if (buffer[0] == '\0') {
652 const char * const _argv[3] = { "setenv", argv[1], NULL };
654 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
656 const char * const _argv[4] = { "setenv", argv[1], buffer,
659 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
662 #endif /* CONFIG_CMD_EDITENV */
663 #endif /* CONFIG_SPL_BUILD */
666 * Look up variable from environment,
667 * return address of storage for that variable,
668 * or NULL if not found
670 char *env_get(const char *name)
672 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
673 struct env_entry e, *ep;
679 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
681 return ep ? ep->data : NULL;
684 /* restricted capabilities before import */
685 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
686 return (char *)(gd->env_buf);
692 * Like env_get, but prints an error if envvar isn't defined in the
693 * environment. It always returns what env_get does, so it can be used in
694 * place of env_get without changing error handling otherwise.
696 char *from_env(const char *envvar)
700 ret = env_get(envvar);
703 printf("missing environment variable: %s\n", envvar);
709 * Look up variable from environment for restricted C runtime env.
711 int env_get_f(const char *name, char *buf, unsigned len)
715 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
718 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
721 if (nxt >= CONFIG_ENV_SIZE)
725 val = env_match((uchar *)name, i);
729 /* found; copy out */
730 for (n = 0; n < len; ++n, ++buf) {
731 c = env_get_char(val++);
742 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
752 * Decode the integer value of an environment variable and return it.
754 * @param name Name of environment variable
755 * @param base Number base to use (normally 10, or 16 for hex)
756 * @param default_val Default value to return if the variable is not
758 * @return the decoded value, or default_val if not found
760 ulong env_get_ulong(const char *name, int base, ulong default_val)
763 * We can use env_get() here, even before relocation, since the
764 * environment variable value is an integer and thus short.
766 const char *str = env_get(name);
768 return str ? simple_strtoul(str, NULL, base) : default_val;
771 #ifndef CONFIG_SPL_BUILD
772 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
773 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
776 return env_save() ? 1 : 0;
780 saveenv, 1, 0, do_env_save,
781 "save environment variables to persistent storage",
785 #if defined(CONFIG_CMD_ERASEENV)
786 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
789 return env_erase() ? 1 : 0;
793 eraseenv, 1, 0, do_env_erase,
794 "erase environment variables from persistent storage",
800 #if defined(CONFIG_CMD_NVEDIT_LOAD)
801 static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
804 return env_reload() ? 1 : 0;
808 #if defined(CONFIG_CMD_NVEDIT_SELECT)
809 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
812 return env_select(argv[1]) ? 1 : 0;
816 #endif /* CONFIG_SPL_BUILD */
818 int env_match(uchar *s1, int i2)
823 while (*s1 == env_get_char(i2++))
827 if (*s1 == '\0' && env_get_char(i2-1) == '=')
833 #ifndef CONFIG_SPL_BUILD
834 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
835 int argc, char *const argv[])
837 int all = 0, env_flag = H_INTERACTIVE;
839 debug("Initial value for argc=%d\n", argc);
840 while (--argc > 0 && **++argv == '-') {
845 case 'a': /* default all */
848 case 'f': /* force */
852 return cmd_usage(cmdtp);
856 debug("Final value for argc=%d\n", argc);
857 if (all && (argc == 0)) {
858 /* Reset the whole environment */
859 env_set_default("## Resetting to default environment\n",
863 if (!all && (argc > 0)) {
864 /* Reset individual variables */
865 env_set_default_vars(argc, argv, env_flag);
869 return cmd_usage(cmdtp);
872 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
873 int argc, char *const argv[])
875 int env_flag = H_INTERACTIVE;
878 debug("Initial value for argc=%d\n", argc);
879 while (argc > 1 && **(argv + 1) == '-') {
885 case 'f': /* force */
889 return CMD_RET_USAGE;
893 debug("Final value for argc=%d\n", argc);
898 char *name = *++argv;
900 if (hdelete_r(name, &env_htab, env_flag))
907 #ifdef CONFIG_CMD_EXPORTENV
909 * env export [-t | -b | -c] [-s size] addr [var ...]
910 * -t: export as text format; if size is given, data will be
911 * padded with '\0' bytes; if not, one terminating '\0'
912 * will be added (which is included in the "filesize"
913 * setting so you can for exmple copy this to flash and
914 * keep the termination).
915 * -b: export as binary format (name=value pairs separated by
916 * '\0', list end marked by double "\0\0")
917 * -c: export as checksum protected environment format as
918 * used for example by "saveenv" command
920 * size of output buffer
921 * addr: memory address where environment gets stored
922 * var... List of variable names that get included into the
923 * export. Without arguments, the whole environment gets
926 * With "-c" and size is NOT given, then the export command will
927 * format the data as currently used for the persistent storage,
928 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
929 * prepend a valid CRC32 checksum and, in case of redundant
930 * environment, a "current" redundancy flag. If size is given, this
931 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
932 * checksum and redundancy flag will be inserted.
934 * With "-b" and "-t", always only the real data (including a
935 * terminating '\0' byte) will be written; here the optional size
936 * argument will be used to make sure not to overflow the user
937 * provided buffer; the command will abort if the size is not
938 * sufficient. Any remaining space will be '\0' padded.
940 * On successful return, the variable "filesize" will be set.
941 * Note that filesize includes the trailing/terminating '\0' byte(s).
943 * Usage scenario: create a text snapshot/backup of the current settings:
945 * => env export -t 100000
946 * => era ${backup_addr} +${filesize}
947 * => cp.b 100000 ${backup_addr} ${filesize}
949 * Re-import this snapshot, deleting all other settings:
951 * => env import -d -t ${backup_addr}
953 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
954 int argc, char *const argv[])
958 char *ptr, *cmd, *res;
968 while (--argc > 0 && **++argv == '-') {
972 case 'b': /* raw binary format */
977 case 'c': /* external checksum format */
983 case 's': /* size given */
985 return cmd_usage(cmdtp);
986 size = simple_strtoul(*++argv, NULL, 16);
988 case 't': /* text format */
994 return CMD_RET_USAGE;
1001 return CMD_RET_USAGE;
1003 addr = simple_strtoul(argv[0], NULL, 16);
1004 ptr = map_sysmem(addr, size);
1007 memset(ptr, '\0', size);
1012 if (sep) { /* export as text file */
1013 len = hexport_r(&env_htab, sep,
1014 H_MATCH_KEY | H_MATCH_IDENT,
1015 &ptr, size, argc, argv);
1017 pr_err("## Error: Cannot export environment: errno = %d\n",
1021 sprintf(buf, "%zX", (size_t)len);
1022 env_set("filesize", buf);
1027 envp = (env_t *)ptr;
1029 if (chk) /* export as checksum protected block */
1030 res = (char *)envp->data;
1031 else /* export as raw binary data */
1034 len = hexport_r(&env_htab, '\0',
1035 H_MATCH_KEY | H_MATCH_IDENT,
1036 &res, ENV_SIZE, argc, argv);
1038 pr_err("## Error: Cannot export environment: errno = %d\n",
1044 envp->crc = crc32(0, envp->data,
1045 size ? size - offsetof(env_t, data) : ENV_SIZE);
1046 #ifdef CONFIG_ENV_ADDR_REDUND
1047 envp->flags = ENV_REDUND_ACTIVE;
1050 env_set_hex("filesize", len + offsetof(env_t, data));
1055 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1061 #ifdef CONFIG_CMD_IMPORTENV
1063 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1064 * -d: delete existing environment before importing if no var is
1065 * passed; if vars are passed, if one var is in the current
1066 * environment but not in the environment at addr, delete var from
1067 * current environment;
1068 * otherwise overwrite / append to existing definitions
1069 * -t: assume text format; either "size" must be given or the
1070 * text data must be '\0' terminated
1071 * -r: handle CRLF like LF, that means exported variables with
1072 * a content which ends with \r won't get imported. Used
1073 * to import text files created with editors which are using CRLF
1074 * for line endings. Only effective in addition to -t.
1075 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1076 * -c: assume checksum protected environment format
1077 * addr: memory address to read from
1078 * size: length of input data; if missing, proper '\0'
1079 * termination is mandatory
1080 * if var is set and size should be missing (i.e. '\0'
1081 * termination), set size to '-'
1082 * var... List of the names of the only variables that get imported from
1083 * the environment at address 'addr'. Without arguments, the whole
1084 * environment gets imported.
1086 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1087 int argc, char *const argv[])
1101 while (--argc > 0 && **++argv == '-') {
1105 case 'b': /* raw binary format */
1110 case 'c': /* external checksum format */
1116 case 't': /* text format */
1121 case 'r': /* handle CRLF like LF */
1128 return CMD_RET_USAGE;
1134 return CMD_RET_USAGE;
1137 printf("## Warning: defaulting to text format\n");
1139 if (sep != '\n' && crlf_is_lf )
1142 addr = simple_strtoul(argv[0], NULL, 16);
1143 ptr = map_sysmem(addr, 0);
1145 if (argc >= 2 && strcmp(argv[1], "-")) {
1146 size = simple_strtoul(argv[1], NULL, 16);
1148 puts("## Error: external checksum format must pass size\n");
1149 return CMD_RET_FAILURE;
1155 while (size < MAX_ENV_SIZE) {
1156 if ((*s == sep) && (*(s+1) == '\0'))
1161 if (size == MAX_ENV_SIZE) {
1162 printf("## Warning: Input data exceeds %d bytes"
1163 " - truncated\n", MAX_ENV_SIZE);
1166 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1174 env_t *ep = (env_t *)ptr;
1176 if (size <= offsetof(env_t, data)) {
1177 printf("## Error: Invalid size 0x%zX\n", size);
1181 size -= offsetof(env_t, data);
1182 memcpy(&crc, &ep->crc, sizeof(crc));
1184 if (crc32(0, ep->data, size) != crc) {
1185 puts("## Error: bad CRC, import failed\n");
1188 ptr = (char *)ep->data;
1191 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1192 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1193 pr_err("## Error: Environment import failed: errno = %d\n",
1197 gd->flags |= GD_FLG_ENV_READY;
1202 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1208 #if defined(CONFIG_CMD_NVEDIT_INFO)
1210 * print_env_info - print environment information
1212 static int print_env_info(void)
1216 /* print environment validity value */
1217 switch (gd->env_valid) {
1225 value = "redundant";
1231 printf("env_valid = %s\n", value);
1233 /* print environment ready flag */
1234 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1235 printf("env_ready = %s\n", value);
1237 /* print environment using default flag */
1238 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1239 printf("env_use_default = %s\n", value);
1241 return CMD_RET_SUCCESS;
1244 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1245 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1248 * env info - display environment information
1249 * env info [-d] - evaluate whether default environment is used
1250 * env info [-p] - evaluate whether environment can be persisted
1251 * Add [-q] - quiet mode, use only for command result, for test by example:
1252 * test env info -p -d -q
1254 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1255 int argc, char *const argv[])
1258 int eval_results = 0;
1260 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1261 enum env_location loc;
1264 /* display environment information */
1266 return print_env_info();
1268 /* process options */
1269 while (--argc > 0 && **++argv == '-') {
1275 eval_flags |= ENV_INFO_IS_DEFAULT;
1278 eval_flags |= ENV_INFO_IS_PERSISTED;
1284 return CMD_RET_USAGE;
1289 /* evaluate whether default environment is used */
1290 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1291 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1293 printf("Default environment is used\n");
1294 eval_results |= ENV_INFO_IS_DEFAULT;
1297 printf("Environment was loaded from persistent storage\n");
1301 /* evaluate whether environment can be persisted */
1302 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1303 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1304 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1305 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1307 printf("Environment can be persisted\n");
1308 eval_results |= ENV_INFO_IS_PERSISTED;
1311 printf("Environment cannot be persisted\n");
1315 printf("Environment cannot be persisted\n");
1319 /* The result of evaluations is combined with AND */
1320 if (eval_flags != eval_results)
1321 return CMD_RET_FAILURE;
1323 return CMD_RET_SUCCESS;
1327 #if defined(CONFIG_CMD_ENV_EXISTS)
1328 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1331 struct env_entry e, *ep;
1334 return CMD_RET_USAGE;
1338 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1340 return (ep == NULL) ? 1 : 0;
1345 * New command line interface: "env" command with subcommands
1347 static struct cmd_tbl cmd_env_sub[] = {
1348 #if defined(CONFIG_CMD_ASKENV)
1349 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1351 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1352 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1353 #if defined(CONFIG_CMD_EDITENV)
1354 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1356 #if defined(CONFIG_CMD_ENV_CALLBACK)
1357 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1359 #if defined(CONFIG_CMD_ENV_FLAGS)
1360 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1362 #if defined(CONFIG_CMD_EXPORTENV)
1363 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1365 #if defined(CONFIG_CMD_GREPENV)
1366 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1368 #if defined(CONFIG_CMD_IMPORTENV)
1369 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1371 #if defined(CONFIG_CMD_NVEDIT_INFO)
1372 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
1374 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1375 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1377 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1378 #if defined(CONFIG_CMD_RUN)
1379 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1381 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1382 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1383 #if defined(CONFIG_CMD_ERASEENV)
1384 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1387 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1388 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1390 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1391 #if defined(CONFIG_CMD_ENV_EXISTS)
1392 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1396 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1397 void env_reloc(void)
1399 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1403 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1408 return CMD_RET_USAGE;
1410 /* drop initial "env" arg */
1414 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1417 return cp->cmd(cmdtp, flag, argc, argv);
1419 return CMD_RET_USAGE;
1422 #ifdef CONFIG_SYS_LONGHELP
1423 static char env_help_text[] =
1424 #if defined(CONFIG_CMD_ASKENV)
1425 "ask name [message] [size] - ask for environment variable\nenv "
1427 #if defined(CONFIG_CMD_ENV_CALLBACK)
1428 "callbacks - print callbacks and their associated variables\nenv "
1430 "default [-f] -a - [forcibly] reset default environment\n"
1431 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1432 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1433 #if defined(CONFIG_CMD_EDITENV)
1434 "env edit name - edit environment variable\n"
1436 #if defined(CONFIG_CMD_ENV_EXISTS)
1437 "env exists name - tests for existence of variable\n"
1439 #if defined(CONFIG_CMD_EXPORTENV)
1440 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1442 #if defined(CONFIG_CMD_ENV_FLAGS)
1443 "env flags - print variables that have non-default flags\n"
1445 #if defined(CONFIG_CMD_GREPENV)
1447 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1449 "env grep [-n | -v | -b] string [...] - search environment\n"
1452 #if defined(CONFIG_CMD_IMPORTENV)
1453 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1455 #if defined(CONFIG_CMD_NVEDIT_INFO)
1456 "env info - display environment information\n"
1457 "env info [-d] [-p] [-q] - evaluate environment information\n"
1458 " \"-d\": default environment is used\n"
1459 " \"-p\": environment can be persisted\n"
1460 " \"-q\": quiet output\n"
1462 "env print [-a | name ...] - print environment\n"
1463 #if defined(CONFIG_CMD_NVEDIT_EFI)
1464 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
1466 #if defined(CONFIG_CMD_RUN)
1467 "env run var [...] - run commands in an environment variable\n"
1469 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1470 "env save - save environment\n"
1471 #if defined(CONFIG_CMD_ERASEENV)
1472 "env erase - erase environment\n"
1475 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1476 "env load - load environment\n"
1478 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1479 "env select [target] - select environment target\n"
1481 #if defined(CONFIG_CMD_NVEDIT_EFI)
1482 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
1483 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1485 "env set [-f] name [arg ...]\n";
1489 env, CONFIG_SYS_MAXARGS, 1, do_env,
1490 "environment handling commands", env_help_text
1494 * Old command line interface, kept for compatibility
1497 #if defined(CONFIG_CMD_EDITENV)
1498 U_BOOT_CMD_COMPLETE(
1499 editenv, 2, 0, do_env_edit,
1500 "edit environment variable",
1502 " - edit environment variable 'name'",
1507 U_BOOT_CMD_COMPLETE(
1508 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1509 "print environment variables",
1510 "[-a]\n - print [all] values of all environment variables\n"
1511 #if defined(CONFIG_CMD_NVEDIT_EFI)
1512 "printenv -e [-guid guid][-n] [name ...]\n"
1513 " - print UEFI variable 'name' or all the variables\n"
1514 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1515 " \"-n\": suppress dumping variable's value\n"
1517 "printenv name ...\n"
1518 " - print value of environment variable 'name'",
1522 #ifdef CONFIG_CMD_GREPENV
1523 U_BOOT_CMD_COMPLETE(
1524 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1525 "search environment variables",
1527 "[-e] [-n | -v | -b] string ...\n"
1529 "[-n | -v | -b] string ...\n"
1531 " - list environment name=value pairs matching 'string'\n"
1533 " \"-e\": enable regular expressions;\n"
1535 " \"-n\": search variable names; \"-v\": search values;\n"
1536 " \"-b\": search both names and values (default)",
1541 U_BOOT_CMD_COMPLETE(
1542 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1543 "set environment variables",
1544 #if defined(CONFIG_CMD_NVEDIT_EFI)
1545 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1546 " [-i addr:size name], or [name [value ...]]\n"
1547 " - set UEFI variable 'name' to 'value' ...'\n"
1548 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1549 " \"-nv\": set non-volatile attribute\n"
1550 " \"-bs\": set boot-service attribute\n"
1551 " \"-rt\": set runtime attribute\n"
1552 " \"-at\": set time-based authentication attribute\n"
1553 " \"-a\": append-write\n"
1554 " \"-i addr,size\": use <addr,size> as variable's value\n"
1555 " \"-v\": verbose message\n"
1556 " - delete UEFI variable 'name' if 'value' not specified\n"
1558 "setenv [-f] name value ...\n"
1559 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1560 "setenv [-f] name\n"
1561 " - [forcibly] delete environment variable 'name'",
1565 #if defined(CONFIG_CMD_ASKENV)
1568 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1569 "get environment variables from stdin",
1570 "name [message] [size]\n"
1571 " - get environment variable 'name' from stdin (max 'size' chars)"
1575 #if defined(CONFIG_CMD_RUN)
1576 U_BOOT_CMD_COMPLETE(
1577 run, CONFIG_SYS_MAXARGS, 1, do_run,
1578 "run commands in an environment variable",
1580 " - run the commands in the environment variable(s) 'var'",
1584 #endif /* CONFIG_SPL_BUILD */