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.
30 #include <environment.h>
36 #include <linux/stddef.h>
37 #include <asm/byteorder.h>
40 DECLARE_GLOBAL_DATA_PTR;
42 #if defined(CONFIG_ENV_IS_IN_EEPROM) || \
43 defined(CONFIG_ENV_IS_IN_FLASH) || \
44 defined(CONFIG_ENV_IS_IN_MMC) || \
45 defined(CONFIG_ENV_IS_IN_FAT) || \
46 defined(CONFIG_ENV_IS_IN_EXT4) || \
47 defined(CONFIG_ENV_IS_IN_NAND) || \
48 defined(CONFIG_ENV_IS_IN_NVRAM) || \
49 defined(CONFIG_ENV_IS_IN_ONENAND) || \
50 defined(CONFIG_ENV_IS_IN_SATA) || \
51 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
52 defined(CONFIG_ENV_IS_IN_REMOTE) || \
53 defined(CONFIG_ENV_IS_IN_UBI)
55 #define ENV_IS_IN_DEVICE
59 #if !defined(ENV_IS_IN_DEVICE) && \
60 !defined(CONFIG_ENV_IS_NOWHERE)
61 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
62 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
66 * Maximum expected input data size for import command
68 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
71 * This variable is incremented on each do_env_set(), so it can
72 * be used via get_env_id() as an indication, if the environment
73 * has changed or not. So it is possible to reread an environment
74 * variable only if the environment was changed ... done so for
75 * example in NetInitLoop()
77 static int env_id = 1;
84 #ifndef CONFIG_SPL_BUILD
86 * Command interface: print one or all environment variables
88 * Returns 0 in case of error, or length of printed string
90 static int env_print(char *name, int flag)
95 if (name) { /* print a single name */
100 hsearch_r(e, FIND, &ep, &env_htab, flag);
103 len = printf("%s=%s\n", ep->key, ep->data);
107 /* print whole list */
108 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
116 /* should never happen */
117 printf("## Error: cannot export environment\n");
121 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
126 int env_flag = H_HIDE_DOT;
128 #if defined(CONFIG_CMD_NVEDIT_EFI)
129 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
130 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
133 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
136 env_flag &= ~H_HIDE_DOT;
140 /* print all env vars */
141 rcode = env_print(NULL, env_flag);
144 printf("\nEnvironment size: %d/%ld bytes\n",
145 rcode, (ulong)ENV_SIZE);
149 /* print selected env vars */
150 env_flag &= ~H_HIDE_DOT;
151 for (i = 1; i < argc; ++i) {
152 int rc = env_print(argv[i], env_flag);
154 printf("## Error: \"%s\" not defined\n", argv[i]);
162 #ifdef CONFIG_CMD_GREPENV
163 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
164 int argc, char * const argv[])
167 int len, grep_how, grep_what;
170 return CMD_RET_USAGE;
172 grep_how = H_MATCH_SUBSTR; /* default: substring search */
173 grep_what = H_MATCH_BOTH; /* default: grep names and values */
175 while (--argc > 0 && **++argv == '-') {
180 case 'e': /* use regex matching */
181 grep_how = H_MATCH_REGEX;
184 case 'n': /* grep for name */
185 grep_what = H_MATCH_KEY;
187 case 'v': /* grep for value */
188 grep_what = H_MATCH_DATA;
190 case 'b': /* grep for both */
191 grep_what = H_MATCH_BOTH;
196 return CMD_RET_USAGE;
202 len = hexport_r(&env_htab, '\n',
203 flag | grep_what | grep_how,
204 &res, 0, argc, argv);
217 #endif /* CONFIG_SPL_BUILD */
220 * Set a new environment variable,
221 * or replace or delete an existing one.
223 static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
226 char *name, *value, *s;
229 debug("Initial value for argc=%d\n", argc);
231 #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
232 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
233 return do_env_set_efi(NULL, flag, --argc, ++argv);
236 while (argc > 1 && **(argv + 1) == '-') {
242 case 'f': /* force */
246 return CMD_RET_USAGE;
250 debug("Final value for argc=%d\n", argc);
253 if (strchr(name, '=')) {
254 printf("## Error: illegal character '='"
255 "in variable name \"%s\"\n", name);
262 if (argc < 3 || argv[2] == NULL) {
263 int rc = hdelete_r(name, &env_htab, env_flag);
268 * Insert / replace new value
270 for (i = 2, len = 0; i < argc; ++i)
271 len += strlen(argv[i]) + 1;
275 printf("## Can't malloc %d bytes\n", len);
278 for (i = 2, s = value; i < argc; ++i) {
281 while ((*s++ = *v++) != '\0')
290 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
293 printf("## Error inserting \"%s\" variable, errno=%d\n",
301 int env_set(const char *varname, const char *varvalue)
303 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
305 /* before import into hashtable */
306 if (!(gd->flags & GD_FLG_ENV_READY))
309 if (varvalue == NULL || varvalue[0] == '\0')
310 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
312 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
316 * Set an environment variable to an integer value
318 * @param varname Environment variable to set
319 * @param value Value to set it to
320 * @return 0 if ok, 1 on error
322 int env_set_ulong(const char *varname, ulong value)
324 /* TODO: this should be unsigned */
325 char *str = simple_itoa(value);
327 return env_set(varname, str);
331 * Set an environment variable to an value in hex
333 * @param varname Environment variable to set
334 * @param value Value to set it to
335 * @return 0 if ok, 1 on error
337 int env_set_hex(const char *varname, ulong value)
341 sprintf(str, "%lx", value);
342 return env_set(varname, str);
345 ulong env_get_hex(const char *varname, ulong default_val)
351 s = env_get(varname);
353 value = simple_strtoul(s, &endp, 16);
360 void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
365 for (i = 0; i < 6; ++i) {
366 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
368 addr = (*end) ? end + 1 : end;
372 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
374 eth_parse_enetaddr(env_get(name), enetaddr);
375 return is_valid_ethaddr(enetaddr);
378 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
380 char buf[ARP_HLEN_ASCII + 1];
382 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
385 sprintf(buf, "%pM", enetaddr);
387 return env_set(name, buf);
390 #ifndef CONFIG_SPL_BUILD
391 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
394 return CMD_RET_USAGE;
396 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
400 * Prompt for environment variable
402 #if defined(CONFIG_CMD_ASKENV)
403 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
405 char message[CONFIG_SYS_CBSIZE];
406 int i, len, pos, size;
410 local_args[0] = argv[0];
411 local_args[1] = argv[1];
412 local_args[2] = NULL;
413 local_args[3] = NULL;
418 * env_ask envname [message1 ...] [size]
421 return CMD_RET_USAGE;
424 * We test the last argument if it can be converted
425 * into a decimal number. If yes, we assume it's
426 * the size. Otherwise we echo it as part of the
429 i = simple_strtoul(argv[argc - 1], &endptr, 10);
430 if (*endptr != '\0') { /* no size */
431 size = CONFIG_SYS_CBSIZE - 1;
432 } else { /* size given */
438 sprintf(message, "Please enter '%s': ", argv[1]);
440 /* env_ask envname message1 ... messagen [size] */
441 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
443 message[pos++] = ' ';
445 strncpy(message + pos, argv[i], sizeof(message) - pos);
446 pos += strlen(argv[i]);
448 if (pos < sizeof(message) - 1) {
449 message[pos++] = ' ';
452 message[CONFIG_SYS_CBSIZE - 1] = '\0';
455 if (size >= CONFIG_SYS_CBSIZE)
456 size = CONFIG_SYS_CBSIZE - 1;
461 /* prompt for input */
462 len = cli_readline(message);
465 console_buffer[size] = '\0';
468 if (console_buffer[0] != '\0') {
469 local_args[2] = console_buffer;
473 /* Continue calling setenv code */
474 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
478 #if defined(CONFIG_CMD_ENV_CALLBACK)
479 static int print_static_binding(const char *var_name, const char *callback_name,
482 printf("\t%-20s %-20s\n", var_name, callback_name);
487 static int print_active_callback(ENTRY *entry)
489 struct env_clbk_tbl *clbkp;
493 if (entry->callback == NULL)
496 /* look up the callback in the linker-list */
497 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
498 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
501 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
502 if (entry->callback == clbkp->callback + gd->reloc_off)
504 if (entry->callback == clbkp->callback)
509 if (i == num_callbacks)
510 /* this should probably never happen, but just in case... */
511 printf("\t%-20s %p\n", entry->key, entry->callback);
513 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
519 * Print the callbacks available and what they are bound to
521 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
523 struct env_clbk_tbl *clbkp;
527 /* Print the available callbacks */
528 puts("Available callbacks:\n");
529 puts("\tCallback Name\n");
530 puts("\t-------------\n");
531 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
532 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
535 printf("\t%s\n", clbkp->name);
538 /* Print the static bindings that may exist */
539 puts("Static callback bindings:\n");
540 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
541 printf("\t%-20s %-20s\n", "-------------", "-------------");
542 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
545 /* walk through each variable and print the callback if it has one */
546 puts("Active callback bindings:\n");
547 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
548 printf("\t%-20s %-20s\n", "-------------", "-------------");
549 hwalk_r(&env_htab, print_active_callback);
554 #if defined(CONFIG_CMD_ENV_FLAGS)
555 static int print_static_flags(const char *var_name, const char *flags,
558 enum env_flags_vartype type = env_flags_parse_vartype(flags);
559 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
561 printf("\t%-20s %-20s %-20s\n", var_name,
562 env_flags_get_vartype_name(type),
563 env_flags_get_varaccess_name(access));
568 static int print_active_flags(ENTRY *entry)
570 enum env_flags_vartype type;
571 enum env_flags_varaccess access;
573 if (entry->flags == 0)
576 type = (enum env_flags_vartype)
577 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
578 access = env_flags_parse_varaccess_from_binflags(entry->flags);
579 printf("\t%-20s %-20s %-20s\n", entry->key,
580 env_flags_get_vartype_name(type),
581 env_flags_get_varaccess_name(access));
587 * Print the flags available and what variables have flags
589 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
591 /* Print the available variable types */
592 printf("Available variable type flags (position %d):\n",
593 ENV_FLAGS_VARTYPE_LOC);
594 puts("\tFlag\tVariable Type Name\n");
595 puts("\t----\t------------------\n");
596 env_flags_print_vartypes();
599 /* Print the available variable access types */
600 printf("Available variable access flags (position %d):\n",
601 ENV_FLAGS_VARACCESS_LOC);
602 puts("\tFlag\tVariable Access Name\n");
603 puts("\t----\t--------------------\n");
604 env_flags_print_varaccess();
607 /* Print the static flags that may exist */
608 puts("Static flags:\n");
609 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
611 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
613 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
616 /* walk through each variable and print the flags if non-default */
617 puts("Active flags:\n");
618 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
620 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
622 hwalk_r(&env_htab, print_active_flags);
628 * Interactively edit an environment variable
630 #if defined(CONFIG_CMD_EDITENV)
631 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
634 char buffer[CONFIG_SYS_CBSIZE];
638 return CMD_RET_USAGE;
640 /* before import into hashtable */
641 if (!(gd->flags & GD_FLG_ENV_READY))
644 /* Set read buffer to initial value or empty sting */
645 init_val = env_get(argv[1]);
647 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
651 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
654 if (buffer[0] == '\0') {
655 const char * const _argv[3] = { "setenv", argv[1], NULL };
657 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
659 const char * const _argv[4] = { "setenv", argv[1], buffer,
662 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
665 #endif /* CONFIG_CMD_EDITENV */
666 #endif /* CONFIG_SPL_BUILD */
669 * Look up variable from environment,
670 * return address of storage for that variable,
671 * or NULL if not found
673 char *env_get(const char *name)
675 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
682 hsearch_r(e, FIND, &ep, &env_htab, 0);
684 return ep ? ep->data : NULL;
687 /* restricted capabilities before import */
688 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
689 return (char *)(gd->env_buf);
695 * Look up variable from environment for restricted C runtime env.
697 int env_get_f(const char *name, char *buf, unsigned len)
701 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
704 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
707 if (nxt >= CONFIG_ENV_SIZE)
711 val = envmatch((uchar *)name, i);
715 /* found; copy out */
716 for (n = 0; n < len; ++n, ++buf) {
717 c = env_get_char(val++);
728 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
738 * Decode the integer value of an environment variable and return it.
740 * @param name Name of environment variable
741 * @param base Number base to use (normally 10, or 16 for hex)
742 * @param default_val Default value to return if the variable is not
744 * @return the decoded value, or default_val if not found
746 ulong env_get_ulong(const char *name, int base, ulong default_val)
749 * We can use env_get() here, even before relocation, since the
750 * environment variable value is an integer and thus short.
752 const char *str = env_get(name);
754 return str ? simple_strtoul(str, NULL, base) : default_val;
757 #ifndef CONFIG_SPL_BUILD
758 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
759 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
762 return env_save() ? 1 : 0;
766 saveenv, 1, 0, do_env_save,
767 "save environment variables to persistent storage",
771 #endif /* CONFIG_SPL_BUILD */
775 * Match a name / name=value pair
777 * s1 is either a simple 'name', or a 'name=value' pair.
778 * i2 is the environment index for a 'name2=value2' pair.
779 * If the names match, return the index for the value2, else -1.
781 int envmatch(uchar *s1, int i2)
786 while (*s1 == env_get_char(i2++))
790 if (*s1 == '\0' && env_get_char(i2-1) == '=')
796 #ifndef CONFIG_SPL_BUILD
797 static int do_env_default(cmd_tbl_t *cmdtp, int flag,
798 int argc, char * const argv[])
800 int all = 0, env_flag = H_INTERACTIVE;
802 debug("Initial value for argc=%d\n", argc);
803 while (--argc > 0 && **++argv == '-') {
808 case 'a': /* default all */
811 case 'f': /* force */
815 return cmd_usage(cmdtp);
819 debug("Final value for argc=%d\n", argc);
820 if (all && (argc == 0)) {
821 /* Reset the whole environment */
822 set_default_env("## Resetting to default environment\n",
826 if (!all && (argc > 0)) {
827 /* Reset individual variables */
828 set_default_vars(argc, argv, env_flag);
832 return cmd_usage(cmdtp);
835 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
836 int argc, char * const argv[])
838 int env_flag = H_INTERACTIVE;
841 debug("Initial value for argc=%d\n", argc);
842 while (argc > 1 && **(argv + 1) == '-') {
848 case 'f': /* force */
852 return CMD_RET_USAGE;
856 debug("Final value for argc=%d\n", argc);
861 char *name = *++argv;
863 if (!hdelete_r(name, &env_htab, env_flag))
870 #ifdef CONFIG_CMD_EXPORTENV
872 * env export [-t | -b | -c] [-s size] addr [var ...]
873 * -t: export as text format; if size is given, data will be
874 * padded with '\0' bytes; if not, one terminating '\0'
875 * will be added (which is included in the "filesize"
876 * setting so you can for exmple copy this to flash and
877 * keep the termination).
878 * -b: export as binary format (name=value pairs separated by
879 * '\0', list end marked by double "\0\0")
880 * -c: export as checksum protected environment format as
881 * used for example by "saveenv" command
883 * size of output buffer
884 * addr: memory address where environment gets stored
885 * var... List of variable names that get included into the
886 * export. Without arguments, the whole environment gets
889 * With "-c" and size is NOT given, then the export command will
890 * format the data as currently used for the persistent storage,
891 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
892 * prepend a valid CRC32 checksum and, in case of redundant
893 * environment, a "current" redundancy flag. If size is given, this
894 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
895 * checksum and redundancy flag will be inserted.
897 * With "-b" and "-t", always only the real data (including a
898 * terminating '\0' byte) will be written; here the optional size
899 * argument will be used to make sure not to overflow the user
900 * provided buffer; the command will abort if the size is not
901 * sufficient. Any remaining space will be '\0' padded.
903 * On successful return, the variable "filesize" will be set.
904 * Note that filesize includes the trailing/terminating '\0' byte(s).
906 * Usage scenario: create a text snapshot/backup of the current settings:
908 * => env export -t 100000
909 * => era ${backup_addr} +${filesize}
910 * => cp.b 100000 ${backup_addr} ${filesize}
912 * Re-import this snapshot, deleting all other settings:
914 * => env import -d -t ${backup_addr}
916 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
917 int argc, char * const argv[])
921 char *ptr, *cmd, *res;
931 while (--argc > 0 && **++argv == '-') {
935 case 'b': /* raw binary format */
940 case 'c': /* external checksum format */
946 case 's': /* size given */
948 return cmd_usage(cmdtp);
949 size = simple_strtoul(*++argv, NULL, 16);
951 case 't': /* text format */
957 return CMD_RET_USAGE;
964 return CMD_RET_USAGE;
966 addr = simple_strtoul(argv[0], NULL, 16);
967 ptr = map_sysmem(addr, size);
970 memset(ptr, '\0', size);
975 if (sep) { /* export as text file */
976 len = hexport_r(&env_htab, sep,
977 H_MATCH_KEY | H_MATCH_IDENT,
978 &ptr, size, argc, argv);
980 pr_err("## Error: Cannot export environment: errno = %d\n",
984 sprintf(buf, "%zX", (size_t)len);
985 env_set("filesize", buf);
992 if (chk) /* export as checksum protected block */
993 res = (char *)envp->data;
994 else /* export as raw binary data */
997 len = hexport_r(&env_htab, '\0',
998 H_MATCH_KEY | H_MATCH_IDENT,
999 &res, ENV_SIZE, argc, argv);
1001 pr_err("## Error: Cannot export environment: errno = %d\n",
1007 envp->crc = crc32(0, envp->data,
1008 size ? size - offsetof(env_t, data) : ENV_SIZE);
1009 #ifdef CONFIG_ENV_ADDR_REDUND
1010 envp->flags = ACTIVE_FLAG;
1013 env_set_hex("filesize", len + offsetof(env_t, data));
1018 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1024 #ifdef CONFIG_CMD_IMPORTENV
1026 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1027 * -d: delete existing environment before importing if no var is
1028 * passed; if vars are passed, if one var is in the current
1029 * environment but not in the environment at addr, delete var from
1030 * current environment;
1031 * otherwise overwrite / append to existing definitions
1032 * -t: assume text format; either "size" must be given or the
1033 * text data must be '\0' terminated
1034 * -r: handle CRLF like LF, that means exported variables with
1035 * a content which ends with \r won't get imported. Used
1036 * to import text files created with editors which are using CRLF
1037 * for line endings. Only effective in addition to -t.
1038 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1039 * -c: assume checksum protected environment format
1040 * addr: memory address to read from
1041 * size: length of input data; if missing, proper '\0'
1042 * termination is mandatory
1043 * if var is set and size should be missing (i.e. '\0'
1044 * termination), set size to '-'
1045 * var... List of the names of the only variables that get imported from
1046 * the environment at address 'addr'. Without arguments, the whole
1047 * environment gets imported.
1049 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1050 int argc, char * const argv[])
1064 while (--argc > 0 && **++argv == '-') {
1068 case 'b': /* raw binary format */
1073 case 'c': /* external checksum format */
1079 case 't': /* text format */
1084 case 'r': /* handle CRLF like LF */
1091 return CMD_RET_USAGE;
1097 return CMD_RET_USAGE;
1100 printf("## Warning: defaulting to text format\n");
1102 if (sep != '\n' && crlf_is_lf )
1105 addr = simple_strtoul(argv[0], NULL, 16);
1106 ptr = map_sysmem(addr, 0);
1108 if (argc >= 2 && strcmp(argv[1], "-")) {
1109 size = simple_strtoul(argv[1], NULL, 16);
1111 puts("## Error: external checksum format must pass size\n");
1112 return CMD_RET_FAILURE;
1118 while (size < MAX_ENV_SIZE) {
1119 if ((*s == sep) && (*(s+1) == '\0'))
1124 if (size == MAX_ENV_SIZE) {
1125 printf("## Warning: Input data exceeds %d bytes"
1126 " - truncated\n", MAX_ENV_SIZE);
1129 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1137 env_t *ep = (env_t *)ptr;
1139 size -= offsetof(env_t, data);
1140 memcpy(&crc, &ep->crc, sizeof(crc));
1142 if (crc32(0, ep->data, size) != crc) {
1143 puts("## Error: bad CRC, import failed\n");
1146 ptr = (char *)ep->data;
1149 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1150 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1151 pr_err("## Error: Environment import failed: errno = %d\n",
1155 gd->flags |= GD_FLG_ENV_READY;
1160 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1166 #if defined(CONFIG_CMD_ENV_EXISTS)
1167 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1168 char * const argv[])
1173 return CMD_RET_USAGE;
1177 hsearch_r(e, FIND, &ep, &env_htab, 0);
1179 return (ep == NULL) ? 1 : 0;
1184 * New command line interface: "env" command with subcommands
1186 static cmd_tbl_t cmd_env_sub[] = {
1187 #if defined(CONFIG_CMD_ASKENV)
1188 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1190 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1191 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1192 #if defined(CONFIG_CMD_EDITENV)
1193 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1195 #if defined(CONFIG_CMD_ENV_CALLBACK)
1196 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1198 #if defined(CONFIG_CMD_ENV_FLAGS)
1199 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1201 #if defined(CONFIG_CMD_EXPORTENV)
1202 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1204 #if defined(CONFIG_CMD_GREPENV)
1205 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1207 #if defined(CONFIG_CMD_IMPORTENV)
1208 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1210 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1211 #if defined(CONFIG_CMD_RUN)
1212 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1214 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1215 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1217 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1218 #if defined(CONFIG_CMD_ENV_EXISTS)
1219 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1223 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1224 void env_reloc(void)
1226 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1230 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1235 return CMD_RET_USAGE;
1237 /* drop initial "env" arg */
1241 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1244 return cp->cmd(cmdtp, flag, argc, argv);
1246 return CMD_RET_USAGE;
1249 #ifdef CONFIG_SYS_LONGHELP
1250 static char env_help_text[] =
1251 #if defined(CONFIG_CMD_ASKENV)
1252 "ask name [message] [size] - ask for environment variable\nenv "
1254 #if defined(CONFIG_CMD_ENV_CALLBACK)
1255 "callbacks - print callbacks and their associated variables\nenv "
1257 "default [-f] -a - [forcibly] reset default environment\n"
1258 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1259 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1260 #if defined(CONFIG_CMD_EDITENV)
1261 "env edit name - edit environment variable\n"
1263 #if defined(CONFIG_CMD_ENV_EXISTS)
1264 "env exists name - tests for existence of variable\n"
1266 #if defined(CONFIG_CMD_EXPORTENV)
1267 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1269 #if defined(CONFIG_CMD_ENV_FLAGS)
1270 "env flags - print variables that have non-default flags\n"
1272 #if defined(CONFIG_CMD_GREPENV)
1274 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1276 "env grep [-n | -v | -b] string [...] - search environment\n"
1279 #if defined(CONFIG_CMD_IMPORTENV)
1280 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1282 "env print [-a | name ...] - print environment\n"
1283 #if defined(CONFIG_CMD_NVEDIT_EFI)
1284 "env print -e [name ...] - print UEFI environment\n"
1286 #if defined(CONFIG_CMD_RUN)
1287 "env run var [...] - run commands in an environment variable\n"
1289 #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1290 "env save - save environment\n"
1292 #if defined(CONFIG_CMD_NVEDIT_EFI)
1293 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1295 "env set [-f] name [arg ...]\n";
1299 env, CONFIG_SYS_MAXARGS, 1, do_env,
1300 "environment handling commands", env_help_text
1304 * Old command line interface, kept for compatibility
1307 #if defined(CONFIG_CMD_EDITENV)
1308 U_BOOT_CMD_COMPLETE(
1309 editenv, 2, 0, do_env_edit,
1310 "edit environment variable",
1312 " - edit environment variable 'name'",
1317 U_BOOT_CMD_COMPLETE(
1318 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1319 "print environment variables",
1320 "[-a]\n - print [all] values of all environment variables\n"
1321 #if defined(CONFIG_CMD_NVEDIT_EFI)
1322 "printenv -e [name ...]\n"
1323 " - print UEFI variable 'name' or all the variables\n"
1325 "printenv name ...\n"
1326 " - print value of environment variable 'name'",
1330 #ifdef CONFIG_CMD_GREPENV
1331 U_BOOT_CMD_COMPLETE(
1332 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1333 "search environment variables",
1335 "[-e] [-n | -v | -b] string ...\n"
1337 "[-n | -v | -b] string ...\n"
1339 " - list environment name=value pairs matching 'string'\n"
1341 " \"-e\": enable regular expressions;\n"
1343 " \"-n\": search variable names; \"-v\": search values;\n"
1344 " \"-b\": search both names and values (default)",
1349 U_BOOT_CMD_COMPLETE(
1350 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1351 "set environment variables",
1352 #if defined(CONFIG_CMD_NVEDIT_EFI)
1353 "-e [-nv] name [value ...]\n"
1354 " - set UEFI variable 'name' to 'value' ...'\n"
1355 " 'nv' option makes the variable non-volatile\n"
1356 " - delete UEFI variable 'name' if 'value' not specified\n"
1358 "setenv [-f] name value ...\n"
1359 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1360 "setenv [-f] name\n"
1361 " - [forcibly] delete environment variable 'name'",
1365 #if defined(CONFIG_CMD_ASKENV)
1368 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1369 "get environment variables from stdin",
1370 "name [message] [size]\n"
1371 " - get environment variable 'name' from stdin (max 'size' chars)"
1375 #if defined(CONFIG_CMD_RUN)
1376 U_BOOT_CMD_COMPLETE(
1377 run, CONFIG_SYS_MAXARGS, 1, do_run,
1378 "run commands in an environment variable",
1380 " - run the commands in the environment variable(s) 'var'",
1384 #endif /* CONFIG_SPL_BUILD */