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>
37 #include <linux/stddef.h>
38 #include <asm/byteorder.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
44 defined(CONFIG_ENV_IS_IN_FLASH) || \
45 defined(CONFIG_ENV_IS_IN_MMC) || \
46 defined(CONFIG_ENV_IS_IN_FAT) || \
47 defined(CONFIG_ENV_IS_IN_EXT4) || \
48 defined(CONFIG_ENV_IS_IN_NAND) || \
49 defined(CONFIG_ENV_IS_IN_NVRAM) || \
50 defined(CONFIG_ENV_IS_IN_ONENAND) || \
51 defined(CONFIG_ENV_IS_IN_SATA) || \
52 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
53 defined(CONFIG_ENV_IS_IN_REMOTE) || \
54 defined(CONFIG_ENV_IS_IN_UBI)
56 #define ENV_IS_IN_DEVICE
60 #if !defined(ENV_IS_IN_DEVICE) && \
61 !defined(CONFIG_ENV_IS_NOWHERE)
62 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
63 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
67 * Maximum expected input data size for import command
69 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
72 * This variable is incremented on each do_env_set(), so it can
73 * be used via env_get_id() as an indication, if the environment
74 * has changed or not. So it is possible to reread an environment
75 * variable only if the environment was changed ... done so for
76 * example in NetInitLoop()
78 static int env_id = 1;
85 #ifndef CONFIG_SPL_BUILD
87 * Command interface: print one or all environment variables
89 * Returns 0 in case of error, or length of printed string
91 static int env_print(char *name, int flag)
96 if (name) { /* print a single name */
97 struct env_entry e, *ep;
101 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
104 len = printf("%s=%s\n", ep->key, ep->data);
108 /* print whole list */
109 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
117 /* should never happen */
118 printf("## Error: cannot export environment\n");
122 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
127 int env_flag = H_HIDE_DOT;
129 #if defined(CONFIG_CMD_NVEDIT_EFI)
130 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
131 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
137 env_flag &= ~H_HIDE_DOT;
141 /* print all env vars */
142 rcode = env_print(NULL, env_flag);
145 printf("\nEnvironment size: %d/%ld bytes\n",
146 rcode, (ulong)ENV_SIZE);
150 /* print selected env vars */
151 env_flag &= ~H_HIDE_DOT;
152 for (i = 1; i < argc; ++i) {
153 int rc = env_print(argv[i], env_flag);
155 printf("## Error: \"%s\" not defined\n", argv[i]);
163 #ifdef CONFIG_CMD_GREPENV
164 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165 int argc, char * const argv[])
168 int len, grep_how, grep_what;
171 return CMD_RET_USAGE;
173 grep_how = H_MATCH_SUBSTR; /* default: substring search */
174 grep_what = H_MATCH_BOTH; /* default: grep names and values */
176 while (--argc > 0 && **++argv == '-') {
181 case 'e': /* use regex matching */
182 grep_how = H_MATCH_REGEX;
185 case 'n': /* grep for name */
186 grep_what = H_MATCH_KEY;
188 case 'v': /* grep for value */
189 grep_what = H_MATCH_DATA;
191 case 'b': /* grep for both */
192 grep_what = H_MATCH_BOTH;
197 return CMD_RET_USAGE;
203 len = hexport_r(&env_htab, '\n',
204 flag | grep_what | grep_how,
205 &res, 0, argc, argv);
218 #endif /* CONFIG_SPL_BUILD */
221 * Set a new environment variable,
222 * or replace or delete an existing one.
224 static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
227 char *name, *value, *s;
228 struct env_entry e, *ep;
230 debug("Initial value for argc=%d\n", argc);
232 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
233 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
234 return do_env_set_efi(NULL, flag, --argc, ++argv);
237 while (argc > 1 && **(argv + 1) == '-') {
243 case 'f': /* force */
247 return CMD_RET_USAGE;
251 debug("Final value for argc=%d\n", argc);
254 if (strchr(name, '=')) {
255 printf("## Error: illegal character '='"
256 "in variable name \"%s\"\n", name);
263 if (argc < 3 || argv[2] == NULL) {
264 int rc = hdelete_r(name, &env_htab, env_flag);
269 * Insert / replace new value
271 for (i = 2, len = 0; i < argc; ++i)
272 len += strlen(argv[i]) + 1;
276 printf("## Can't malloc %d bytes\n", len);
279 for (i = 2, s = value; i < argc; ++i) {
282 while ((*s++ = *v++) != '\0')
291 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
294 printf("## Error inserting \"%s\" variable, errno=%d\n",
302 int env_set(const char *varname, const char *varvalue)
304 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
306 /* before import into hashtable */
307 if (!(gd->flags & GD_FLG_ENV_READY))
310 if (varvalue == NULL || varvalue[0] == '\0')
311 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
313 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
317 * Set an environment variable to an integer value
319 * @param varname Environment variable to set
320 * @param value Value to set it to
321 * @return 0 if ok, 1 on error
323 int env_set_ulong(const char *varname, ulong value)
325 /* TODO: this should be unsigned */
326 char *str = simple_itoa(value);
328 return env_set(varname, str);
332 * Set an environment variable to an value in hex
334 * @param varname Environment variable to set
335 * @param value Value to set it to
336 * @return 0 if ok, 1 on error
338 int env_set_hex(const char *varname, ulong value)
342 sprintf(str, "%lx", value);
343 return env_set(varname, str);
346 ulong env_get_hex(const char *varname, ulong default_val)
352 s = env_get(varname);
354 value = simple_strtoul(s, &endp, 16);
361 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
363 eth_parse_enetaddr(env_get(name), enetaddr);
364 return is_valid_ethaddr(enetaddr);
367 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
369 char buf[ARP_HLEN_ASCII + 1];
371 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
374 sprintf(buf, "%pM", enetaddr);
376 return env_set(name, buf);
379 #ifndef CONFIG_SPL_BUILD
380 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
383 return CMD_RET_USAGE;
385 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
389 * Prompt for environment variable
391 #if defined(CONFIG_CMD_ASKENV)
392 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
394 char message[CONFIG_SYS_CBSIZE];
395 int i, len, pos, size;
399 local_args[0] = argv[0];
400 local_args[1] = argv[1];
401 local_args[2] = NULL;
402 local_args[3] = NULL;
407 * env_ask envname [message1 ...] [size]
410 return CMD_RET_USAGE;
413 * We test the last argument if it can be converted
414 * into a decimal number. If yes, we assume it's
415 * the size. Otherwise we echo it as part of the
418 i = simple_strtoul(argv[argc - 1], &endptr, 10);
419 if (*endptr != '\0') { /* no size */
420 size = CONFIG_SYS_CBSIZE - 1;
421 } else { /* size given */
427 sprintf(message, "Please enter '%s': ", argv[1]);
429 /* env_ask envname message1 ... messagen [size] */
430 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
432 message[pos++] = ' ';
434 strncpy(message + pos, argv[i], sizeof(message) - pos);
435 pos += strlen(argv[i]);
437 if (pos < sizeof(message) - 1) {
438 message[pos++] = ' ';
441 message[CONFIG_SYS_CBSIZE - 1] = '\0';
444 if (size >= CONFIG_SYS_CBSIZE)
445 size = CONFIG_SYS_CBSIZE - 1;
450 /* prompt for input */
451 len = cli_readline(message);
454 console_buffer[size] = '\0';
457 if (console_buffer[0] != '\0') {
458 local_args[2] = console_buffer;
462 /* Continue calling setenv code */
463 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
467 #if defined(CONFIG_CMD_ENV_CALLBACK)
468 static int print_static_binding(const char *var_name, const char *callback_name,
471 printf("\t%-20s %-20s\n", var_name, callback_name);
476 static int print_active_callback(struct env_entry *entry)
478 struct env_clbk_tbl *clbkp;
482 if (entry->callback == NULL)
485 /* look up the callback in the linker-list */
486 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
487 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
490 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
491 if (entry->callback == clbkp->callback + gd->reloc_off)
493 if (entry->callback == clbkp->callback)
498 if (i == num_callbacks)
499 /* this should probably never happen, but just in case... */
500 printf("\t%-20s %p\n", entry->key, entry->callback);
502 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
508 * Print the callbacks available and what they are bound to
510 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
512 struct env_clbk_tbl *clbkp;
516 /* Print the available callbacks */
517 puts("Available callbacks:\n");
518 puts("\tCallback Name\n");
519 puts("\t-------------\n");
520 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
521 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
524 printf("\t%s\n", clbkp->name);
527 /* Print the static bindings that may exist */
528 puts("Static callback bindings:\n");
529 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
530 printf("\t%-20s %-20s\n", "-------------", "-------------");
531 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
534 /* walk through each variable and print the callback if it has one */
535 puts("Active callback bindings:\n");
536 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
537 printf("\t%-20s %-20s\n", "-------------", "-------------");
538 hwalk_r(&env_htab, print_active_callback);
543 #if defined(CONFIG_CMD_ENV_FLAGS)
544 static int print_static_flags(const char *var_name, const char *flags,
547 enum env_flags_vartype type = env_flags_parse_vartype(flags);
548 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
550 printf("\t%-20s %-20s %-20s\n", var_name,
551 env_flags_get_vartype_name(type),
552 env_flags_get_varaccess_name(access));
557 static int print_active_flags(struct env_entry *entry)
559 enum env_flags_vartype type;
560 enum env_flags_varaccess access;
562 if (entry->flags == 0)
565 type = (enum env_flags_vartype)
566 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
567 access = env_flags_parse_varaccess_from_binflags(entry->flags);
568 printf("\t%-20s %-20s %-20s\n", entry->key,
569 env_flags_get_vartype_name(type),
570 env_flags_get_varaccess_name(access));
576 * Print the flags available and what variables have flags
578 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
580 /* Print the available variable types */
581 printf("Available variable type flags (position %d):\n",
582 ENV_FLAGS_VARTYPE_LOC);
583 puts("\tFlag\tVariable Type Name\n");
584 puts("\t----\t------------------\n");
585 env_flags_print_vartypes();
588 /* Print the available variable access types */
589 printf("Available variable access flags (position %d):\n",
590 ENV_FLAGS_VARACCESS_LOC);
591 puts("\tFlag\tVariable Access Name\n");
592 puts("\t----\t--------------------\n");
593 env_flags_print_varaccess();
596 /* Print the static flags that may exist */
597 puts("Static flags:\n");
598 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
600 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
602 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
605 /* walk through each variable and print the flags if non-default */
606 puts("Active flags:\n");
607 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
609 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
611 hwalk_r(&env_htab, print_active_flags);
617 * Interactively edit an environment variable
619 #if defined(CONFIG_CMD_EDITENV)
620 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
623 char buffer[CONFIG_SYS_CBSIZE];
627 return CMD_RET_USAGE;
629 /* before import into hashtable */
630 if (!(gd->flags & GD_FLG_ENV_READY))
633 /* Set read buffer to initial value or empty sting */
634 init_val = env_get(argv[1]);
636 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
640 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
643 if (buffer[0] == '\0') {
644 const char * const _argv[3] = { "setenv", argv[1], NULL };
646 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
648 const char * const _argv[4] = { "setenv", argv[1], buffer,
651 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
654 #endif /* CONFIG_CMD_EDITENV */
655 #endif /* CONFIG_SPL_BUILD */
658 * Look up variable from environment,
659 * return address of storage for that variable,
660 * or NULL if not found
662 char *env_get(const char *name)
664 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
665 struct env_entry e, *ep;
671 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
673 return ep ? ep->data : NULL;
676 /* restricted capabilities before import */
677 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
678 return (char *)(gd->env_buf);
684 * Look up variable from environment for restricted C runtime env.
686 int env_get_f(const char *name, char *buf, unsigned len)
690 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
693 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
696 if (nxt >= CONFIG_ENV_SIZE)
700 val = env_match((uchar *)name, i);
704 /* found; copy out */
705 for (n = 0; n < len; ++n, ++buf) {
706 c = env_get_char(val++);
717 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
727 * Decode the integer value of an environment variable and return it.
729 * @param name Name of environment variable
730 * @param base Number base to use (normally 10, or 16 for hex)
731 * @param default_val Default value to return if the variable is not
733 * @return the decoded value, or default_val if not found
735 ulong env_get_ulong(const char *name, int base, ulong default_val)
738 * We can use env_get() here, even before relocation, since the
739 * environment variable value is an integer and thus short.
741 const char *str = env_get(name);
743 return str ? simple_strtoul(str, NULL, base) : default_val;
746 #ifndef CONFIG_SPL_BUILD
747 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
748 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
751 return env_save() ? 1 : 0;
755 saveenv, 1, 0, do_env_save,
756 "save environment variables to persistent storage",
760 #if defined(CONFIG_CMD_ERASEENV)
761 static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
764 return env_erase() ? 1 : 0;
768 eraseenv, 1, 0, do_env_erase,
769 "erase environment variables from persistent storage",
774 #endif /* CONFIG_SPL_BUILD */
776 int env_match(uchar *s1, int i2)
781 while (*s1 == env_get_char(i2++))
785 if (*s1 == '\0' && env_get_char(i2-1) == '=')
791 #ifndef CONFIG_SPL_BUILD
792 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
793 int argc, char * const argv[])
795 int all = 0, env_flag = H_INTERACTIVE;
797 debug("Initial value for argc=%d\n", argc);
798 while (--argc > 0 && **++argv == '-') {
803 case 'a': /* default all */
806 case 'f': /* force */
810 return cmd_usage(cmdtp);
814 debug("Final value for argc=%d\n", argc);
815 if (all && (argc == 0)) {
816 /* Reset the whole environment */
817 env_set_default("## Resetting to default environment\n",
821 if (!all && (argc > 0)) {
822 /* Reset individual variables */
823 env_set_default_vars(argc, argv, env_flag);
827 return cmd_usage(cmdtp);
830 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
831 int argc, char * const argv[])
833 int env_flag = H_INTERACTIVE;
836 debug("Initial value for argc=%d\n", argc);
837 while (argc > 1 && **(argv + 1) == '-') {
843 case 'f': /* force */
847 return CMD_RET_USAGE;
851 debug("Final value for argc=%d\n", argc);
856 char *name = *++argv;
858 if (!hdelete_r(name, &env_htab, env_flag))
865 #ifdef CONFIG_CMD_EXPORTENV
867 * env export [-t | -b | -c] [-s size] addr [var ...]
868 * -t: export as text format; if size is given, data will be
869 * padded with '\0' bytes; if not, one terminating '\0'
870 * will be added (which is included in the "filesize"
871 * setting so you can for exmple copy this to flash and
872 * keep the termination).
873 * -b: export as binary format (name=value pairs separated by
874 * '\0', list end marked by double "\0\0")
875 * -c: export as checksum protected environment format as
876 * used for example by "saveenv" command
878 * size of output buffer
879 * addr: memory address where environment gets stored
880 * var... List of variable names that get included into the
881 * export. Without arguments, the whole environment gets
884 * With "-c" and size is NOT given, then the export command will
885 * format the data as currently used for the persistent storage,
886 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
887 * prepend a valid CRC32 checksum and, in case of redundant
888 * environment, a "current" redundancy flag. If size is given, this
889 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
890 * checksum and redundancy flag will be inserted.
892 * With "-b" and "-t", always only the real data (including a
893 * terminating '\0' byte) will be written; here the optional size
894 * argument will be used to make sure not to overflow the user
895 * provided buffer; the command will abort if the size is not
896 * sufficient. Any remaining space will be '\0' padded.
898 * On successful return, the variable "filesize" will be set.
899 * Note that filesize includes the trailing/terminating '\0' byte(s).
901 * Usage scenario: create a text snapshot/backup of the current settings:
903 * => env export -t 100000
904 * => era ${backup_addr} +${filesize}
905 * => cp.b 100000 ${backup_addr} ${filesize}
907 * Re-import this snapshot, deleting all other settings:
909 * => env import -d -t ${backup_addr}
911 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
912 int argc, char * const argv[])
916 char *ptr, *cmd, *res;
926 while (--argc > 0 && **++argv == '-') {
930 case 'b': /* raw binary format */
935 case 'c': /* external checksum format */
941 case 's': /* size given */
943 return cmd_usage(cmdtp);
944 size = simple_strtoul(*++argv, NULL, 16);
946 case 't': /* text format */
952 return CMD_RET_USAGE;
959 return CMD_RET_USAGE;
961 addr = simple_strtoul(argv[0], NULL, 16);
962 ptr = map_sysmem(addr, size);
965 memset(ptr, '\0', size);
970 if (sep) { /* export as text file */
971 len = hexport_r(&env_htab, sep,
972 H_MATCH_KEY | H_MATCH_IDENT,
973 &ptr, size, argc, argv);
975 pr_err("## Error: Cannot export environment: errno = %d\n",
979 sprintf(buf, "%zX", (size_t)len);
980 env_set("filesize", buf);
987 if (chk) /* export as checksum protected block */
988 res = (char *)envp->data;
989 else /* export as raw binary data */
992 len = hexport_r(&env_htab, '\0',
993 H_MATCH_KEY | H_MATCH_IDENT,
994 &res, ENV_SIZE, argc, argv);
996 pr_err("## Error: Cannot export environment: errno = %d\n",
1002 envp->crc = crc32(0, envp->data,
1003 size ? size - offsetof(env_t, data) : ENV_SIZE);
1004 #ifdef CONFIG_ENV_ADDR_REDUND
1005 envp->flags = ENV_REDUND_ACTIVE;
1008 env_set_hex("filesize", len + offsetof(env_t, data));
1013 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1019 #ifdef CONFIG_CMD_IMPORTENV
1021 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1022 * -d: delete existing environment before importing if no var is
1023 * passed; if vars are passed, if one var is in the current
1024 * environment but not in the environment at addr, delete var from
1025 * current environment;
1026 * otherwise overwrite / append to existing definitions
1027 * -t: assume text format; either "size" must be given or the
1028 * text data must be '\0' terminated
1029 * -r: handle CRLF like LF, that means exported variables with
1030 * a content which ends with \r won't get imported. Used
1031 * to import text files created with editors which are using CRLF
1032 * for line endings. Only effective in addition to -t.
1033 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1034 * -c: assume checksum protected environment format
1035 * addr: memory address to read from
1036 * size: length of input data; if missing, proper '\0'
1037 * termination is mandatory
1038 * if var is set and size should be missing (i.e. '\0'
1039 * termination), set size to '-'
1040 * var... List of the names of the only variables that get imported from
1041 * the environment at address 'addr'. Without arguments, the whole
1042 * environment gets imported.
1044 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1045 int argc, char * const argv[])
1059 while (--argc > 0 && **++argv == '-') {
1063 case 'b': /* raw binary format */
1068 case 'c': /* external checksum format */
1074 case 't': /* text format */
1079 case 'r': /* handle CRLF like LF */
1086 return CMD_RET_USAGE;
1092 return CMD_RET_USAGE;
1095 printf("## Warning: defaulting to text format\n");
1097 if (sep != '\n' && crlf_is_lf )
1100 addr = simple_strtoul(argv[0], NULL, 16);
1101 ptr = map_sysmem(addr, 0);
1103 if (argc >= 2 && strcmp(argv[1], "-")) {
1104 size = simple_strtoul(argv[1], NULL, 16);
1106 puts("## Error: external checksum format must pass size\n");
1107 return CMD_RET_FAILURE;
1113 while (size < MAX_ENV_SIZE) {
1114 if ((*s == sep) && (*(s+1) == '\0'))
1119 if (size == MAX_ENV_SIZE) {
1120 printf("## Warning: Input data exceeds %d bytes"
1121 " - truncated\n", MAX_ENV_SIZE);
1124 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1132 env_t *ep = (env_t *)ptr;
1134 size -= offsetof(env_t, data);
1135 memcpy(&crc, &ep->crc, sizeof(crc));
1137 if (crc32(0, ep->data, size) != crc) {
1138 puts("## Error: bad CRC, import failed\n");
1141 ptr = (char *)ep->data;
1144 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1145 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1146 pr_err("## Error: Environment import failed: errno = %d\n",
1150 gd->flags |= GD_FLG_ENV_READY;
1155 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1161 #if defined(CONFIG_CMD_NVEDIT_INFO)
1163 * print_env_info - print environment information
1165 static int print_env_info(void)
1169 /* print environment validity value */
1170 switch (gd->env_valid) {
1178 value = "redundant";
1184 printf("env_valid = %s\n", value);
1186 /* print environment ready flag */
1187 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1188 printf("env_ready = %s\n", value);
1190 /* print environment using default flag */
1191 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1192 printf("env_use_default = %s\n", value);
1194 return CMD_RET_SUCCESS;
1197 #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1198 #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1201 * env info - display environment information
1202 * env info [-d] - evaluate whether default environment is used
1203 * env info [-p] - evaluate whether environment can be persisted
1205 static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1206 int argc, char * const argv[])
1209 int eval_results = 0;
1211 /* display environment information */
1213 return print_env_info();
1215 /* process options */
1216 while (--argc > 0 && **++argv == '-') {
1222 eval_flags |= ENV_INFO_IS_DEFAULT;
1225 eval_flags |= ENV_INFO_IS_PERSISTED;
1228 return CMD_RET_USAGE;
1233 /* evaluate whether default environment is used */
1234 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1235 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1236 printf("Default environment is used\n");
1237 eval_results |= ENV_INFO_IS_DEFAULT;
1239 printf("Environment was loaded from persistent storage\n");
1243 /* evaluate whether environment can be persisted */
1244 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1245 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1246 printf("Environment can be persisted\n");
1247 eval_results |= ENV_INFO_IS_PERSISTED;
1249 printf("Environment cannot be persisted\n");
1253 /* The result of evaluations is combined with AND */
1254 if (eval_flags != eval_results)
1255 return CMD_RET_FAILURE;
1257 return CMD_RET_SUCCESS;
1261 #if defined(CONFIG_CMD_ENV_EXISTS)
1262 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1263 char * const argv[])
1265 struct env_entry e, *ep;
1268 return CMD_RET_USAGE;
1272 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
1274 return (ep == NULL) ? 1 : 0;
1279 * New command line interface: "env" command with subcommands
1281 static cmd_tbl_t cmd_env_sub[] = {
1282 #if defined(CONFIG_CMD_ASKENV)
1283 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1285 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1286 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1287 #if defined(CONFIG_CMD_EDITENV)
1288 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1290 #if defined(CONFIG_CMD_ENV_CALLBACK)
1291 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1293 #if defined(CONFIG_CMD_ENV_FLAGS)
1294 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1296 #if defined(CONFIG_CMD_EXPORTENV)
1297 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1299 #if defined(CONFIG_CMD_GREPENV)
1300 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1302 #if defined(CONFIG_CMD_IMPORTENV)
1303 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1305 #if defined(CONFIG_CMD_NVEDIT_INFO)
1306 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1308 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1309 #if defined(CONFIG_CMD_RUN)
1310 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1312 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1313 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1314 #if defined(CONFIG_CMD_ERASEENV)
1315 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1318 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1319 #if defined(CONFIG_CMD_ENV_EXISTS)
1320 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1324 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1325 void env_reloc(void)
1327 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1331 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1336 return CMD_RET_USAGE;
1338 /* drop initial "env" arg */
1342 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1345 return cp->cmd(cmdtp, flag, argc, argv);
1347 return CMD_RET_USAGE;
1350 #ifdef CONFIG_SYS_LONGHELP
1351 static char env_help_text[] =
1352 #if defined(CONFIG_CMD_ASKENV)
1353 "ask name [message] [size] - ask for environment variable\nenv "
1355 #if defined(CONFIG_CMD_ENV_CALLBACK)
1356 "callbacks - print callbacks and their associated variables\nenv "
1358 "default [-f] -a - [forcibly] reset default environment\n"
1359 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1360 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1361 #if defined(CONFIG_CMD_EDITENV)
1362 "env edit name - edit environment variable\n"
1364 #if defined(CONFIG_CMD_ENV_EXISTS)
1365 "env exists name - tests for existence of variable\n"
1367 #if defined(CONFIG_CMD_EXPORTENV)
1368 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1370 #if defined(CONFIG_CMD_ENV_FLAGS)
1371 "env flags - print variables that have non-default flags\n"
1373 #if defined(CONFIG_CMD_GREPENV)
1375 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1377 "env grep [-n | -v | -b] string [...] - search environment\n"
1380 #if defined(CONFIG_CMD_IMPORTENV)
1381 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1383 #if defined(CONFIG_CMD_NVEDIT_INFO)
1384 "env info - display environment information\n"
1385 "env info [-d] - whether default environment is used\n"
1386 "env info [-p] - whether environment can be persisted\n"
1388 "env print [-a | name ...] - print environment\n"
1389 #if defined(CONFIG_CMD_NVEDIT_EFI)
1390 "env print -e [name ...] - print UEFI environment\n"
1392 #if defined(CONFIG_CMD_RUN)
1393 "env run var [...] - run commands in an environment variable\n"
1395 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1396 "env save - save environment\n"
1397 #if defined(CONFIG_CMD_ERASEENV)
1398 "env erase - erase environment\n"
1401 #if defined(CONFIG_CMD_NVEDIT_EFI)
1402 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1404 "env set [-f] name [arg ...]\n";
1408 env, CONFIG_SYS_MAXARGS, 1, do_env,
1409 "environment handling commands", env_help_text
1413 * Old command line interface, kept for compatibility
1416 #if defined(CONFIG_CMD_EDITENV)
1417 U_BOOT_CMD_COMPLETE(
1418 editenv, 2, 0, do_env_edit,
1419 "edit environment variable",
1421 " - edit environment variable 'name'",
1426 U_BOOT_CMD_COMPLETE(
1427 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1428 "print environment variables",
1429 "[-a]\n - print [all] values of all environment variables\n"
1430 #if defined(CONFIG_CMD_NVEDIT_EFI)
1431 "printenv -e [name ...]\n"
1432 " - print UEFI variable 'name' or all the variables\n"
1434 "printenv name ...\n"
1435 " - print value of environment variable 'name'",
1439 #ifdef CONFIG_CMD_GREPENV
1440 U_BOOT_CMD_COMPLETE(
1441 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1442 "search environment variables",
1444 "[-e] [-n | -v | -b] string ...\n"
1446 "[-n | -v | -b] string ...\n"
1448 " - list environment name=value pairs matching 'string'\n"
1450 " \"-e\": enable regular expressions;\n"
1452 " \"-n\": search variable names; \"-v\": search values;\n"
1453 " \"-b\": search both names and values (default)",
1458 U_BOOT_CMD_COMPLETE(
1459 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1460 "set environment variables",
1461 #if defined(CONFIG_CMD_NVEDIT_EFI)
1462 "-e [-nv] name [value ...]\n"
1463 " - set UEFI variable 'name' to 'value' ...'\n"
1464 " 'nv' option makes the variable non-volatile\n"
1465 " - delete UEFI variable 'name' if 'value' not specified\n"
1467 "setenv [-f] name value ...\n"
1468 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1469 "setenv [-f] name\n"
1470 " - [forcibly] delete environment variable 'name'",
1474 #if defined(CONFIG_CMD_ASKENV)
1477 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1478 "get environment variables from stdin",
1479 "name [message] [size]\n"
1480 " - get environment variable 'name' from stdin (max 'size' chars)"
1484 #if defined(CONFIG_CMD_RUN)
1485 U_BOOT_CMD_COMPLETE(
1486 run, CONFIG_SYS_MAXARGS, 1, do_run,
1487 "run commands in an environment variable",
1489 " - run the commands in the environment variable(s) 'var'",
1493 #endif /* CONFIG_SPL_BUILD */