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);
273 * Insert / replace new value
275 for (i = 2, len = 0; i < argc; ++i)
276 len += strlen(argv[i]) + 1;
280 printf("## Can't malloc %d bytes\n", len);
283 for (i = 2, s = value; i < argc; ++i) {
286 while ((*s++ = *v++) != '\0')
295 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
298 printf("## Error inserting \"%s\" variable, errno=%d\n",
306 int env_set(const char *varname, const char *varvalue)
308 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
310 /* before import into hashtable */
311 if (!(gd->flags & GD_FLG_ENV_READY))
314 if (varvalue == NULL || varvalue[0] == '\0')
315 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
317 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
321 * Set an environment variable to an integer value
323 * @param varname Environment variable to set
324 * @param value Value to set it to
325 * @return 0 if ok, 1 on error
327 int env_set_ulong(const char *varname, ulong value)
329 /* TODO: this should be unsigned */
330 char *str = simple_itoa(value);
332 return env_set(varname, str);
336 * Set an environment variable to an value in hex
338 * @param varname Environment variable to set
339 * @param value Value to set it to
340 * @return 0 if ok, 1 on error
342 int env_set_hex(const char *varname, ulong value)
346 sprintf(str, "%lx", value);
347 return env_set(varname, str);
350 ulong env_get_hex(const char *varname, ulong default_val)
356 s = env_get(varname);
358 value = simple_strtoul(s, &endp, 16);
365 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
367 string_to_enetaddr(env_get(name), enetaddr);
368 return is_valid_ethaddr(enetaddr);
371 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
373 char buf[ARP_HLEN_ASCII + 1];
375 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
378 sprintf(buf, "%pM", enetaddr);
380 return env_set(name, buf);
383 #ifndef CONFIG_SPL_BUILD
384 static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
388 return CMD_RET_USAGE;
390 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
394 * Prompt for environment variable
396 #if defined(CONFIG_CMD_ASKENV)
397 int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
399 char message[CONFIG_SYS_CBSIZE];
400 int i, len, pos, size;
404 local_args[0] = argv[0];
405 local_args[1] = argv[1];
406 local_args[2] = NULL;
407 local_args[3] = NULL;
412 * env_ask envname [message1 ...] [size]
415 return CMD_RET_USAGE;
418 * We test the last argument if it can be converted
419 * into a decimal number. If yes, we assume it's
420 * the size. Otherwise we echo it as part of the
423 i = simple_strtoul(argv[argc - 1], &endptr, 10);
424 if (*endptr != '\0') { /* no size */
425 size = CONFIG_SYS_CBSIZE - 1;
426 } else { /* size given */
432 sprintf(message, "Please enter '%s': ", argv[1]);
434 /* env_ask envname message1 ... messagen [size] */
435 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
437 message[pos++] = ' ';
439 strncpy(message + pos, argv[i], sizeof(message) - pos);
440 pos += strlen(argv[i]);
442 if (pos < sizeof(message) - 1) {
443 message[pos++] = ' ';
446 message[CONFIG_SYS_CBSIZE - 1] = '\0';
449 if (size >= CONFIG_SYS_CBSIZE)
450 size = CONFIG_SYS_CBSIZE - 1;
455 /* prompt for input */
456 len = cli_readline(message);
459 console_buffer[size] = '\0';
462 if (console_buffer[0] != '\0') {
463 local_args[2] = console_buffer;
467 /* Continue calling setenv code */
468 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
472 #if defined(CONFIG_CMD_ENV_CALLBACK)
473 static int print_static_binding(const char *var_name, const char *callback_name,
476 printf("\t%-20s %-20s\n", var_name, callback_name);
481 static int print_active_callback(struct env_entry *entry)
483 struct env_clbk_tbl *clbkp;
487 if (entry->callback == NULL)
490 /* look up the callback in the linker-list */
491 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
492 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
495 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
496 if (entry->callback == clbkp->callback + gd->reloc_off)
498 if (entry->callback == clbkp->callback)
503 if (i == num_callbacks)
504 /* this should probably never happen, but just in case... */
505 printf("\t%-20s %p\n", entry->key, entry->callback);
507 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
513 * Print the callbacks available and what they are bound to
515 int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
518 struct env_clbk_tbl *clbkp;
522 /* Print the available callbacks */
523 puts("Available callbacks:\n");
524 puts("\tCallback Name\n");
525 puts("\t-------------\n");
526 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
527 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
530 printf("\t%s\n", clbkp->name);
533 /* Print the static bindings that may exist */
534 puts("Static callback bindings:\n");
535 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
536 printf("\t%-20s %-20s\n", "-------------", "-------------");
537 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
540 /* walk through each variable and print the callback if it has one */
541 puts("Active callback bindings:\n");
542 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
543 printf("\t%-20s %-20s\n", "-------------", "-------------");
544 hwalk_r(&env_htab, print_active_callback);
549 #if defined(CONFIG_CMD_ENV_FLAGS)
550 static int print_static_flags(const char *var_name, const char *flags,
553 enum env_flags_vartype type = env_flags_parse_vartype(flags);
554 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
556 printf("\t%-20s %-20s %-20s\n", var_name,
557 env_flags_get_vartype_name(type),
558 env_flags_get_varaccess_name(access));
563 static int print_active_flags(struct env_entry *entry)
565 enum env_flags_vartype type;
566 enum env_flags_varaccess access;
568 if (entry->flags == 0)
571 type = (enum env_flags_vartype)
572 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
573 access = env_flags_parse_varaccess_from_binflags(entry->flags);
574 printf("\t%-20s %-20s %-20s\n", entry->key,
575 env_flags_get_vartype_name(type),
576 env_flags_get_varaccess_name(access));
582 * Print the flags available and what variables have flags
584 int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
586 /* Print the available variable types */
587 printf("Available variable type flags (position %d):\n",
588 ENV_FLAGS_VARTYPE_LOC);
589 puts("\tFlag\tVariable Type Name\n");
590 puts("\t----\t------------------\n");
591 env_flags_print_vartypes();
594 /* Print the available variable access types */
595 printf("Available variable access flags (position %d):\n",
596 ENV_FLAGS_VARACCESS_LOC);
597 puts("\tFlag\tVariable Access Name\n");
598 puts("\t----\t--------------------\n");
599 env_flags_print_varaccess();
602 /* Print the static flags that may exist */
603 puts("Static flags:\n");
604 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
606 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
608 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
611 /* walk through each variable and print the flags if non-default */
612 puts("Active flags:\n");
613 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
615 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
617 hwalk_r(&env_htab, print_active_flags);
623 * Interactively edit an environment variable
625 #if defined(CONFIG_CMD_EDITENV)
626 static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
629 char buffer[CONFIG_SYS_CBSIZE];
633 return CMD_RET_USAGE;
635 /* before import into hashtable */
636 if (!(gd->flags & GD_FLG_ENV_READY))
639 /* Set read buffer to initial value or empty sting */
640 init_val = env_get(argv[1]);
642 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
646 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
649 if (buffer[0] == '\0') {
650 const char * const _argv[3] = { "setenv", argv[1], NULL };
652 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
654 const char * const _argv[4] = { "setenv", argv[1], buffer,
657 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
660 #endif /* CONFIG_CMD_EDITENV */
661 #endif /* CONFIG_SPL_BUILD */
664 * Look up variable from environment,
665 * return address of storage for that variable,
666 * or NULL if not found
668 char *env_get(const char *name)
670 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
671 struct env_entry e, *ep;
677 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
679 return ep ? ep->data : NULL;
682 /* restricted capabilities before import */
683 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
684 return (char *)(gd->env_buf);
690 * Like env_get, but prints an error if envvar isn't defined in the
691 * environment. It always returns what env_get does, so it can be used in
692 * place of env_get without changing error handling otherwise.
694 char *from_env(const char *envvar)
698 ret = env_get(envvar);
701 printf("missing environment variable: %s\n", envvar);
707 * Look up variable from environment for restricted C runtime env.
709 int env_get_f(const char *name, char *buf, unsigned len)
713 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
716 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
719 if (nxt >= CONFIG_ENV_SIZE)
723 val = env_match((uchar *)name, i);
727 /* found; copy out */
728 for (n = 0; n < len; ++n, ++buf) {
729 c = env_get_char(val++);
740 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
750 * Decode the integer value of an environment variable and return it.
752 * @param name Name of environment variable
753 * @param base Number base to use (normally 10, or 16 for hex)
754 * @param default_val Default value to return if the variable is not
756 * @return the decoded value, or default_val if not found
758 ulong env_get_ulong(const char *name, int base, ulong default_val)
761 * We can use env_get() here, even before relocation, since the
762 * environment variable value is an integer and thus short.
764 const char *str = env_get(name);
766 return str ? simple_strtoul(str, NULL, base) : default_val;
769 #ifndef CONFIG_SPL_BUILD
770 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
771 static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
774 return env_save() ? 1 : 0;
778 saveenv, 1, 0, do_env_save,
779 "save environment variables to persistent storage",
783 #if defined(CONFIG_CMD_ERASEENV)
784 static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
787 return env_erase() ? 1 : 0;
791 eraseenv, 1, 0, do_env_erase,
792 "erase environment variables from persistent storage",
798 #if defined(CONFIG_CMD_NVEDIT_LOAD)
799 static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
802 return env_reload() ? 1 : 0;
806 #if defined(CONFIG_CMD_NVEDIT_SELECT)
807 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
810 return env_select(argv[1]) ? 1 : 0;
814 #endif /* CONFIG_SPL_BUILD */
816 int env_match(uchar *s1, int i2)
821 while (*s1 == env_get_char(i2++))
825 if (*s1 == '\0' && env_get_char(i2-1) == '=')
831 #ifndef CONFIG_SPL_BUILD
832 static int do_env_default(struct cmd_tbl *cmdtp, int flag,
833 int argc, char *const argv[])
835 int all = 0, env_flag = H_INTERACTIVE;
837 debug("Initial value for argc=%d\n", argc);
838 while (--argc > 0 && **++argv == '-') {
843 case 'a': /* default all */
846 case 'f': /* force */
850 return cmd_usage(cmdtp);
854 debug("Final value for argc=%d\n", argc);
855 if (all && (argc == 0)) {
856 /* Reset the whole environment */
857 env_set_default("## Resetting to default environment\n",
861 if (!all && (argc > 0)) {
862 /* Reset individual variables */
863 env_set_default_vars(argc, argv, env_flag);
867 return cmd_usage(cmdtp);
870 static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
871 int argc, char *const argv[])
873 int env_flag = H_INTERACTIVE;
876 debug("Initial value for argc=%d\n", argc);
877 while (argc > 1 && **(argv + 1) == '-') {
883 case 'f': /* force */
887 return CMD_RET_USAGE;
891 debug("Final value for argc=%d\n", argc);
896 char *name = *++argv;
898 if (!hdelete_r(name, &env_htab, env_flag))
905 #ifdef CONFIG_CMD_EXPORTENV
907 * env export [-t | -b | -c] [-s size] addr [var ...]
908 * -t: export as text format; if size is given, data will be
909 * padded with '\0' bytes; if not, one terminating '\0'
910 * will be added (which is included in the "filesize"
911 * setting so you can for exmple copy this to flash and
912 * keep the termination).
913 * -b: export as binary format (name=value pairs separated by
914 * '\0', list end marked by double "\0\0")
915 * -c: export as checksum protected environment format as
916 * used for example by "saveenv" command
918 * size of output buffer
919 * addr: memory address where environment gets stored
920 * var... List of variable names that get included into the
921 * export. Without arguments, the whole environment gets
924 * With "-c" and size is NOT given, then the export command will
925 * format the data as currently used for the persistent storage,
926 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
927 * prepend a valid CRC32 checksum and, in case of redundant
928 * environment, a "current" redundancy flag. If size is given, this
929 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
930 * checksum and redundancy flag will be inserted.
932 * With "-b" and "-t", always only the real data (including a
933 * terminating '\0' byte) will be written; here the optional size
934 * argument will be used to make sure not to overflow the user
935 * provided buffer; the command will abort if the size is not
936 * sufficient. Any remaining space will be '\0' padded.
938 * On successful return, the variable "filesize" will be set.
939 * Note that filesize includes the trailing/terminating '\0' byte(s).
941 * Usage scenario: create a text snapshot/backup of the current settings:
943 * => env export -t 100000
944 * => era ${backup_addr} +${filesize}
945 * => cp.b 100000 ${backup_addr} ${filesize}
947 * Re-import this snapshot, deleting all other settings:
949 * => env import -d -t ${backup_addr}
951 static int do_env_export(struct cmd_tbl *cmdtp, int flag,
952 int argc, char *const argv[])
956 char *ptr, *cmd, *res;
966 while (--argc > 0 && **++argv == '-') {
970 case 'b': /* raw binary format */
975 case 'c': /* external checksum format */
981 case 's': /* size given */
983 return cmd_usage(cmdtp);
984 size = simple_strtoul(*++argv, NULL, 16);
986 case 't': /* text format */
992 return CMD_RET_USAGE;
999 return CMD_RET_USAGE;
1001 addr = simple_strtoul(argv[0], NULL, 16);
1002 ptr = map_sysmem(addr, size);
1005 memset(ptr, '\0', size);
1010 if (sep) { /* export as text file */
1011 len = hexport_r(&env_htab, sep,
1012 H_MATCH_KEY | H_MATCH_IDENT,
1013 &ptr, size, argc, argv);
1015 pr_err("## Error: Cannot export environment: errno = %d\n",
1019 sprintf(buf, "%zX", (size_t)len);
1020 env_set("filesize", buf);
1025 envp = (env_t *)ptr;
1027 if (chk) /* export as checksum protected block */
1028 res = (char *)envp->data;
1029 else /* export as raw binary data */
1032 len = hexport_r(&env_htab, '\0',
1033 H_MATCH_KEY | H_MATCH_IDENT,
1034 &res, ENV_SIZE, argc, argv);
1036 pr_err("## Error: Cannot export environment: errno = %d\n",
1042 envp->crc = crc32(0, envp->data,
1043 size ? size - offsetof(env_t, data) : ENV_SIZE);
1044 #ifdef CONFIG_ENV_ADDR_REDUND
1045 envp->flags = ENV_REDUND_ACTIVE;
1048 env_set_hex("filesize", len + offsetof(env_t, data));
1053 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1059 #ifdef CONFIG_CMD_IMPORTENV
1061 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1062 * -d: delete existing environment before importing if no var is
1063 * passed; if vars are passed, if one var is in the current
1064 * environment but not in the environment at addr, delete var from
1065 * current environment;
1066 * otherwise overwrite / append to existing definitions
1067 * -t: assume text format; either "size" must be given or the
1068 * text data must be '\0' terminated
1069 * -r: handle CRLF like LF, that means exported variables with
1070 * a content which ends with \r won't get imported. Used
1071 * to import text files created with editors which are using CRLF
1072 * for line endings. Only effective in addition to -t.
1073 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1074 * -c: assume checksum protected environment format
1075 * addr: memory address to read from
1076 * size: length of input data; if missing, proper '\0'
1077 * termination is mandatory
1078 * if var is set and size should be missing (i.e. '\0'
1079 * termination), set size to '-'
1080 * var... List of the names of the only variables that get imported from
1081 * the environment at address 'addr'. Without arguments, the whole
1082 * environment gets imported.
1084 static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1085 int argc, char *const argv[])
1099 while (--argc > 0 && **++argv == '-') {
1103 case 'b': /* raw binary format */
1108 case 'c': /* external checksum format */
1114 case 't': /* text format */
1119 case 'r': /* handle CRLF like LF */
1126 return CMD_RET_USAGE;
1132 return CMD_RET_USAGE;
1135 printf("## Warning: defaulting to text format\n");
1137 if (sep != '\n' && crlf_is_lf )
1140 addr = simple_strtoul(argv[0], NULL, 16);
1141 ptr = map_sysmem(addr, 0);
1143 if (argc >= 2 && strcmp(argv[1], "-")) {
1144 size = simple_strtoul(argv[1], NULL, 16);
1146 puts("## Error: external checksum format must pass size\n");
1147 return CMD_RET_FAILURE;
1153 while (size < MAX_ENV_SIZE) {
1154 if ((*s == sep) && (*(s+1) == '\0'))
1159 if (size == MAX_ENV_SIZE) {
1160 printf("## Warning: Input data exceeds %d bytes"
1161 " - truncated\n", MAX_ENV_SIZE);
1164 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1172 env_t *ep = (env_t *)ptr;
1174 if (size <= offsetof(env_t, data)) {
1175 printf("## Error: Invalid size 0x%zX\n", size);
1179 size -= offsetof(env_t, data);
1180 memcpy(&crc, &ep->crc, sizeof(crc));
1182 if (crc32(0, ep->data, size) != crc) {
1183 puts("## Error: bad CRC, import failed\n");
1186 ptr = (char *)ep->data;
1189 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1190 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1191 pr_err("## Error: Environment import failed: errno = %d\n",
1195 gd->flags |= GD_FLG_ENV_READY;
1200 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1206 #if defined(CONFIG_CMD_NVEDIT_INFO)
1208 * print_env_info - print environment information
1210 static int print_env_info(void)
1214 /* print environment validity value */
1215 switch (gd->env_valid) {
1223 value = "redundant";
1229 printf("env_valid = %s\n", value);
1231 /* print environment ready flag */
1232 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1233 printf("env_ready = %s\n", value);
1235 /* print environment using default flag */
1236 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1237 printf("env_use_default = %s\n", value);
1239 return CMD_RET_SUCCESS;
1242 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1243 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1246 * env info - display environment information
1247 * env info [-d] - evaluate whether default environment is used
1248 * env info [-p] - evaluate whether environment can be persisted
1249 * Add [-q] - quiet mode, use only for command result, for test by example:
1250 * test env info -p -d -q
1252 static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1253 int argc, char *const argv[])
1256 int eval_results = 0;
1258 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1259 enum env_location loc;
1262 /* display environment information */
1264 return print_env_info();
1266 /* process options */
1267 while (--argc > 0 && **++argv == '-') {
1273 eval_flags |= ENV_INFO_IS_DEFAULT;
1276 eval_flags |= ENV_INFO_IS_PERSISTED;
1282 return CMD_RET_USAGE;
1287 /* evaluate whether default environment is used */
1288 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1289 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1291 printf("Default environment is used\n");
1292 eval_results |= ENV_INFO_IS_DEFAULT;
1295 printf("Environment was loaded from persistent storage\n");
1299 /* evaluate whether environment can be persisted */
1300 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1301 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1302 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1303 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1305 printf("Environment can be persisted\n");
1306 eval_results |= ENV_INFO_IS_PERSISTED;
1309 printf("Environment cannot be persisted\n");
1313 printf("Environment cannot be persisted\n");
1317 /* The result of evaluations is combined with AND */
1318 if (eval_flags != eval_results)
1319 return CMD_RET_FAILURE;
1321 return CMD_RET_SUCCESS;
1325 #if defined(CONFIG_CMD_ENV_EXISTS)
1326 static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1329 struct env_entry e, *ep;
1332 return CMD_RET_USAGE;
1336 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1338 return (ep == NULL) ? 1 : 0;
1343 * New command line interface: "env" command with subcommands
1345 static struct cmd_tbl cmd_env_sub[] = {
1346 #if defined(CONFIG_CMD_ASKENV)
1347 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1349 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1350 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1351 #if defined(CONFIG_CMD_EDITENV)
1352 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1354 #if defined(CONFIG_CMD_ENV_CALLBACK)
1355 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1357 #if defined(CONFIG_CMD_ENV_FLAGS)
1358 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1360 #if defined(CONFIG_CMD_EXPORTENV)
1361 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1363 #if defined(CONFIG_CMD_GREPENV)
1364 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1366 #if defined(CONFIG_CMD_IMPORTENV)
1367 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1369 #if defined(CONFIG_CMD_NVEDIT_INFO)
1370 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
1372 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1373 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1375 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1376 #if defined(CONFIG_CMD_RUN)
1377 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1379 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1380 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1381 #if defined(CONFIG_CMD_ERASEENV)
1382 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1385 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1386 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1388 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1389 #if defined(CONFIG_CMD_ENV_EXISTS)
1390 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1394 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1395 void env_reloc(void)
1397 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1401 static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
1406 return CMD_RET_USAGE;
1408 /* drop initial "env" arg */
1412 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1415 return cp->cmd(cmdtp, flag, argc, argv);
1417 return CMD_RET_USAGE;
1420 #ifdef CONFIG_SYS_LONGHELP
1421 static char env_help_text[] =
1422 #if defined(CONFIG_CMD_ASKENV)
1423 "ask name [message] [size] - ask for environment variable\nenv "
1425 #if defined(CONFIG_CMD_ENV_CALLBACK)
1426 "callbacks - print callbacks and their associated variables\nenv "
1428 "default [-f] -a - [forcibly] reset default environment\n"
1429 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1430 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1431 #if defined(CONFIG_CMD_EDITENV)
1432 "env edit name - edit environment variable\n"
1434 #if defined(CONFIG_CMD_ENV_EXISTS)
1435 "env exists name - tests for existence of variable\n"
1437 #if defined(CONFIG_CMD_EXPORTENV)
1438 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1440 #if defined(CONFIG_CMD_ENV_FLAGS)
1441 "env flags - print variables that have non-default flags\n"
1443 #if defined(CONFIG_CMD_GREPENV)
1445 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1447 "env grep [-n | -v | -b] string [...] - search environment\n"
1450 #if defined(CONFIG_CMD_IMPORTENV)
1451 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1453 #if defined(CONFIG_CMD_NVEDIT_INFO)
1454 "env info - display environment information\n"
1455 "env info [-d] [-p] [-q] - evaluate environment information\n"
1456 " \"-d\": default environment is used\n"
1457 " \"-p\": environment can be persisted\n"
1458 " \"-q\": quiet output\n"
1460 "env print [-a | name ...] - print environment\n"
1461 #if defined(CONFIG_CMD_NVEDIT_EFI)
1462 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
1464 #if defined(CONFIG_CMD_RUN)
1465 "env run var [...] - run commands in an environment variable\n"
1467 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1468 "env save - save environment\n"
1469 #if defined(CONFIG_CMD_ERASEENV)
1470 "env erase - erase environment\n"
1473 #if defined(CONFIG_CMD_NVEDIT_LOAD)
1474 "env load - load environment\n"
1476 #if defined(CONFIG_CMD_NVEDIT_SELECT)
1477 "env select [target] - select environment target\n"
1479 #if defined(CONFIG_CMD_NVEDIT_EFI)
1480 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
1481 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
1483 "env set [-f] name [arg ...]\n";
1487 env, CONFIG_SYS_MAXARGS, 1, do_env,
1488 "environment handling commands", env_help_text
1492 * Old command line interface, kept for compatibility
1495 #if defined(CONFIG_CMD_EDITENV)
1496 U_BOOT_CMD_COMPLETE(
1497 editenv, 2, 0, do_env_edit,
1498 "edit environment variable",
1500 " - edit environment variable 'name'",
1505 U_BOOT_CMD_COMPLETE(
1506 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1507 "print environment variables",
1508 "[-a]\n - print [all] values of all environment variables\n"
1509 #if defined(CONFIG_CMD_NVEDIT_EFI)
1510 "printenv -e [-guid guid][-n] [name ...]\n"
1511 " - print UEFI variable 'name' or all the variables\n"
1512 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1513 " \"-n\": suppress dumping variable's value\n"
1515 "printenv name ...\n"
1516 " - print value of environment variable 'name'",
1520 #ifdef CONFIG_CMD_GREPENV
1521 U_BOOT_CMD_COMPLETE(
1522 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1523 "search environment variables",
1525 "[-e] [-n | -v | -b] string ...\n"
1527 "[-n | -v | -b] string ...\n"
1529 " - list environment name=value pairs matching 'string'\n"
1531 " \"-e\": enable regular expressions;\n"
1533 " \"-n\": search variable names; \"-v\": search values;\n"
1534 " \"-b\": search both names and values (default)",
1539 U_BOOT_CMD_COMPLETE(
1540 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1541 "set environment variables",
1542 #if defined(CONFIG_CMD_NVEDIT_EFI)
1543 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
1544 " [-i addr:size name], or [name [value ...]]\n"
1545 " - set UEFI variable 'name' to 'value' ...'\n"
1546 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
1547 " \"-nv\": set non-volatile attribute\n"
1548 " \"-bs\": set boot-service attribute\n"
1549 " \"-rt\": set runtime attribute\n"
1550 " \"-at\": set time-based authentication attribute\n"
1551 " \"-a\": append-write\n"
1552 " \"-i addr,size\": use <addr,size> as variable's value\n"
1553 " \"-v\": verbose message\n"
1554 " - delete UEFI variable 'name' if 'value' not specified\n"
1556 "setenv [-f] name value ...\n"
1557 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1558 "setenv [-f] name\n"
1559 " - [forcibly] delete environment variable 'name'",
1563 #if defined(CONFIG_CMD_ASKENV)
1566 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1567 "get environment variables from stdin",
1568 "name [message] [size]\n"
1569 " - get environment variable 'name' from stdin (max 'size' chars)"
1573 #if defined(CONFIG_CMD_RUN)
1574 U_BOOT_CMD_COMPLETE(
1575 run, CONFIG_SYS_MAXARGS, 1, do_run,
1576 "run commands in an environment variable",
1578 " - run the commands in the environment variable(s) 'var'",
1582 #endif /* CONFIG_SPL_BUILD */