Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 2 | /* |
Wolfgang Denk | ea009d4 | 2013-03-23 23:50:28 +0000 | [diff] [blame] | 3 | * (C) Copyright 2000-2013 |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Andreas Heppel <aheppel@sysgo.de> |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 8 | * |
| 9 | * Copyright 2011 Freescale Semiconductor, Inc. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 12 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 13 | * Support for persistent environment data |
| 14 | * |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 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 |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 17 | * a double '\0'. The environment is preceded by a 32 bit CRC over |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 18 | * the data part and, in case of redundant environment, a byte of |
| 19 | * flags. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 20 | * |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 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. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Tom Rini | 03de305 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 26 | #include <config.h> |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 27 | #include <cli.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 28 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 29 | #include <console.h> |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 30 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 31 | #include <env_internal.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 32 | #include <log.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 33 | #include <search.h> |
| 34 | #include <errno.h> |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 35 | #include <malloc.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 36 | #include <mapmem.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 37 | #include <asm/global_data.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 38 | #include <linux/bitops.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 39 | #include <linux/printk.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 40 | #include <u-boot/crc.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 41 | #include <linux/stddef.h> |
| 42 | #include <asm/byteorder.h> |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 43 | #include <asm/io.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 44 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 47 | /* |
| 48 | * Maximum expected input data size for import command |
| 49 | */ |
| 50 | #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 51 | |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 52 | #ifndef CONFIG_XPL_BUILD |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 53 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 54 | * Command interface: print one or all environment variables |
| 55 | * |
| 56 | * Returns 0 in case of error, or length of printed string |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 57 | */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 58 | static int env_print(char *name, int flag) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 59 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 60 | char *res = NULL; |
Maxime Larocque | 22a4a6c | 2012-09-28 05:00:13 +0000 | [diff] [blame] | 61 | ssize_t len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 62 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 63 | if (name) { /* print a single name */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 64 | struct env_entry e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 65 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 66 | e.key = name; |
| 67 | e.data = NULL; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 68 | hsearch_r(e, ENV_FIND, &ep, &env_htab, flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | if (ep == NULL) |
| 70 | return 0; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 71 | len = printf("%s=%s\n", ep->key, ep->data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 72 | return len; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 73 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 74 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | /* print whole list */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 76 | len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 77 | |
| 78 | if (len > 0) { |
| 79 | puts(res); |
| 80 | free(res); |
| 81 | return len; |
| 82 | } |
| 83 | |
| 84 | /* should never happen */ |
Maxime Larocque | 22a4a6c | 2012-09-28 05:00:13 +0000 | [diff] [blame] | 85 | printf("## Error: cannot export environment\n"); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 86 | return 0; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 89 | static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc, |
| 90 | char *const argv[]) |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 91 | { |
| 92 | int i; |
| 93 | int rcode = 0; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 94 | int env_flag = H_HIDE_DOT; |
| 95 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 96 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
| 97 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') |
| 98 | return do_env_print_efi(cmdtp, flag, --argc, ++argv); |
| 99 | #endif |
| 100 | |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 101 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') { |
| 102 | argc--; |
| 103 | argv++; |
| 104 | env_flag &= ~H_HIDE_DOT; |
| 105 | } |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 106 | |
| 107 | if (argc == 1) { |
| 108 | /* print all env vars */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 109 | rcode = env_print(NULL, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 110 | if (!rcode) |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 111 | return 1; |
| 112 | printf("\nEnvironment size: %d/%ld bytes\n", |
| 113 | rcode, (ulong)ENV_SIZE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 117 | /* print selected env vars */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 118 | env_flag &= ~H_HIDE_DOT; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 119 | for (i = 1; i < argc; ++i) { |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 120 | int rc = env_print(argv[i], env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 121 | if (!rc) { |
| 122 | printf("## Error: \"%s\" not defined\n", argv[i]); |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 123 | ++rcode; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 126 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 127 | return rcode; |
| 128 | } |
| 129 | |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_CMD_GREPENV |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 131 | static int do_env_grep(struct cmd_tbl *cmdtp, int flag, |
| 132 | int argc, char *const argv[]) |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 133 | { |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 134 | char *res = NULL; |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 135 | int len, grep_how, grep_what; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 136 | |
| 137 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 138 | return CMD_RET_USAGE; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 139 | |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 140 | grep_how = H_MATCH_SUBSTR; /* default: substring search */ |
| 141 | grep_what = H_MATCH_BOTH; /* default: grep names and values */ |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 142 | |
Pierre Aubert | 9a83233 | 2013-10-08 14:20:27 +0200 | [diff] [blame] | 143 | while (--argc > 0 && **++argv == '-') { |
| 144 | char *arg = *argv; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 145 | while (*++arg) { |
| 146 | switch (*arg) { |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 147 | #ifdef CONFIG_REGEX |
| 148 | case 'e': /* use regex matching */ |
| 149 | grep_how = H_MATCH_REGEX; |
| 150 | break; |
| 151 | #endif |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 152 | case 'n': /* grep for name */ |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 153 | grep_what = H_MATCH_KEY; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 154 | break; |
| 155 | case 'v': /* grep for value */ |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 156 | grep_what = H_MATCH_DATA; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 157 | break; |
| 158 | case 'b': /* grep for both */ |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 159 | grep_what = H_MATCH_BOTH; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 160 | break; |
| 161 | case '-': |
| 162 | goto DONE; |
| 163 | default: |
| 164 | return CMD_RET_USAGE; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | DONE: |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 170 | len = hexport_r(&env_htab, '\n', |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 171 | flag | grep_what | grep_how, |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 172 | &res, 0, argc, argv); |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 173 | |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 174 | if (len > 0) { |
| 175 | puts(res); |
| 176 | free(res); |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 179 | if (len < 2) |
| 180 | return 1; |
| 181 | |
| 182 | return 0; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 183 | } |
| 184 | #endif |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 185 | #endif /* CONFIG_XPL_BUILD */ |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 186 | |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 187 | #ifndef CONFIG_XPL_BUILD |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 188 | static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc, |
| 189 | char *const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 190 | { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 191 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 192 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 193 | |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 194 | return env_do_env_set(flag, argc, argv, H_INTERACTIVE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 198 | * Prompt for environment variable |
| 199 | */ |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 200 | #if defined(CONFIG_CMD_ASKENV) |
Andy Shevchenko | e7dad2d | 2024-10-21 16:56:33 +0300 | [diff] [blame] | 201 | static int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 202 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 203 | char message[CONFIG_SYS_CBSIZE]; |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 204 | int i, len, pos, size; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 205 | char *local_args[4]; |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 206 | char *endptr; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 207 | |
| 208 | local_args[0] = argv[0]; |
| 209 | local_args[1] = argv[1]; |
| 210 | local_args[2] = NULL; |
| 211 | local_args[3] = NULL; |
| 212 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 213 | /* |
| 214 | * Check the syntax: |
| 215 | * |
| 216 | * env_ask envname [message1 ...] [size] |
| 217 | */ |
| 218 | if (argc == 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 219 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 220 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 221 | /* |
| 222 | * We test the last argument if it can be converted |
| 223 | * into a decimal number. If yes, we assume it's |
| 224 | * the size. Otherwise we echo it as part of the |
| 225 | * message. |
| 226 | */ |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 227 | i = dectoul(argv[argc - 1], &endptr); |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 228 | if (*endptr != '\0') { /* no size */ |
| 229 | size = CONFIG_SYS_CBSIZE - 1; |
| 230 | } else { /* size given */ |
| 231 | size = i; |
| 232 | --argc; |
| 233 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 234 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 235 | if (argc <= 2) { |
| 236 | sprintf(message, "Please enter '%s': ", argv[1]); |
| 237 | } else { |
| 238 | /* env_ask envname message1 ... messagen [size] */ |
Tom Rini | bf52fcd | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 239 | for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) { |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 240 | if (pos) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 241 | message[pos++] = ' '; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 242 | |
Tom Rini | c667723 | 2017-09-26 19:37:11 -0400 | [diff] [blame] | 243 | strncpy(message + pos, argv[i], sizeof(message) - pos); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 244 | pos += strlen(argv[i]); |
| 245 | } |
Tom Rini | c667723 | 2017-09-26 19:37:11 -0400 | [diff] [blame] | 246 | if (pos < sizeof(message) - 1) { |
| 247 | message[pos++] = ' '; |
| 248 | message[pos] = '\0'; |
| 249 | } else |
| 250 | message[CONFIG_SYS_CBSIZE - 1] = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 253 | if (size >= CONFIG_SYS_CBSIZE) |
| 254 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 255 | |
| 256 | if (size <= 0) |
| 257 | return 1; |
| 258 | |
| 259 | /* prompt for input */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 260 | len = cli_readline(message); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 261 | |
| 262 | if (size < len) |
| 263 | console_buffer[size] = '\0'; |
| 264 | |
| 265 | len = 2; |
| 266 | if (console_buffer[0] != '\0') { |
| 267 | local_args[2] = console_buffer; |
| 268 | len = 3; |
| 269 | } |
| 270 | |
| 271 | /* Continue calling setenv code */ |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 272 | return env_do_env_set(flag, len, local_args, H_INTERACTIVE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 273 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 274 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 275 | |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 276 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 277 | static int print_static_binding(const char *var_name, const char *callback_name, |
| 278 | void *priv) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 279 | { |
| 280 | printf("\t%-20s %-20s\n", var_name, callback_name); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 285 | static int print_active_callback(struct env_entry *entry) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 286 | { |
| 287 | struct env_clbk_tbl *clbkp; |
| 288 | int i; |
| 289 | int num_callbacks; |
| 290 | |
| 291 | if (entry->callback == NULL) |
| 292 | return 0; |
| 293 | |
| 294 | /* look up the callback in the linker-list */ |
| 295 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 296 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 297 | i < num_callbacks; |
| 298 | i++, clbkp++) { |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 299 | if (entry->callback == clbkp->callback) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | |
| 303 | if (i == num_callbacks) |
| 304 | /* this should probably never happen, but just in case... */ |
| 305 | printf("\t%-20s %p\n", entry->key, entry->callback); |
| 306 | else |
| 307 | printf("\t%-20s %-20s\n", entry->key, clbkp->name); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Print the callbacks available and what they are bound to |
| 314 | */ |
Andy Shevchenko | e7dad2d | 2024-10-21 16:56:33 +0300 | [diff] [blame] | 315 | static int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc, |
| 316 | char *const argv[]) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 317 | { |
| 318 | struct env_clbk_tbl *clbkp; |
| 319 | int i; |
| 320 | int num_callbacks; |
| 321 | |
| 322 | /* Print the available callbacks */ |
| 323 | puts("Available callbacks:\n"); |
| 324 | puts("\tCallback Name\n"); |
| 325 | puts("\t-------------\n"); |
| 326 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 327 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 328 | i < num_callbacks; |
| 329 | i++, clbkp++) |
| 330 | printf("\t%s\n", clbkp->name); |
| 331 | puts("\n"); |
| 332 | |
| 333 | /* Print the static bindings that may exist */ |
| 334 | puts("Static callback bindings:\n"); |
| 335 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 336 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 337 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL); |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 338 | puts("\n"); |
| 339 | |
| 340 | /* walk through each variable and print the callback if it has one */ |
| 341 | puts("Active callback bindings:\n"); |
| 342 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 343 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
| 344 | hwalk_r(&env_htab, print_active_callback); |
| 345 | return 0; |
| 346 | } |
| 347 | #endif |
| 348 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 349 | #if defined(CONFIG_CMD_ENV_FLAGS) |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 350 | static int print_static_flags(const char *var_name, const char *flags, |
| 351 | void *priv) |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 352 | { |
| 353 | enum env_flags_vartype type = env_flags_parse_vartype(flags); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 354 | enum env_flags_varaccess access = env_flags_parse_varaccess(flags); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 355 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 356 | printf("\t%-20s %-20s %-20s\n", var_name, |
| 357 | env_flags_get_vartype_name(type), |
| 358 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 363 | static int print_active_flags(struct env_entry *entry) |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 364 | { |
| 365 | enum env_flags_vartype type; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 366 | enum env_flags_varaccess access; |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 367 | |
| 368 | if (entry->flags == 0) |
| 369 | return 0; |
| 370 | |
| 371 | type = (enum env_flags_vartype) |
| 372 | (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 373 | access = env_flags_parse_varaccess_from_binflags(entry->flags); |
| 374 | printf("\t%-20s %-20s %-20s\n", entry->key, |
| 375 | env_flags_get_vartype_name(type), |
| 376 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Print the flags available and what variables have flags |
| 383 | */ |
Andy Shevchenko | e7dad2d | 2024-10-21 16:56:33 +0300 | [diff] [blame] | 384 | static int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 385 | { |
| 386 | /* Print the available variable types */ |
| 387 | printf("Available variable type flags (position %d):\n", |
| 388 | ENV_FLAGS_VARTYPE_LOC); |
| 389 | puts("\tFlag\tVariable Type Name\n"); |
| 390 | puts("\t----\t------------------\n"); |
| 391 | env_flags_print_vartypes(); |
| 392 | puts("\n"); |
| 393 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 394 | /* Print the available variable access types */ |
| 395 | printf("Available variable access flags (position %d):\n", |
| 396 | ENV_FLAGS_VARACCESS_LOC); |
| 397 | puts("\tFlag\tVariable Access Name\n"); |
| 398 | puts("\t----\t--------------------\n"); |
| 399 | env_flags_print_varaccess(); |
| 400 | puts("\n"); |
| 401 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 402 | /* Print the static flags that may exist */ |
| 403 | puts("Static flags:\n"); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 404 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 405 | "Variable Access"); |
| 406 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 407 | "---------------"); |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 408 | env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 409 | puts("\n"); |
| 410 | |
| 411 | /* walk through each variable and print the flags if non-default */ |
| 412 | puts("Active flags:\n"); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 413 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 414 | "Variable Access"); |
| 415 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 416 | "---------------"); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 417 | hwalk_r(&env_htab, print_active_flags); |
| 418 | return 0; |
| 419 | } |
| 420 | #endif |
| 421 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 422 | /* |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 423 | * Interactively edit an environment variable |
| 424 | */ |
| 425 | #if defined(CONFIG_CMD_EDITENV) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 426 | static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc, |
| 427 | char *const argv[]) |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 428 | { |
| 429 | char buffer[CONFIG_SYS_CBSIZE]; |
| 430 | char *init_val; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 431 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 432 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 433 | return CMD_RET_USAGE; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 434 | |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 435 | /* before import into hashtable */ |
| 436 | if (!(gd->flags & GD_FLG_ENV_READY)) |
| 437 | return 1; |
| 438 | |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 439 | /* Set read buffer to initial value or empty sting */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 440 | init_val = env_get(argv[1]); |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 441 | if (init_val) |
Peng Fan | 5d49b4c | 2015-12-23 12:08:09 +0800 | [diff] [blame] | 442 | snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val); |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 443 | else |
| 444 | buffer[0] = '\0'; |
| 445 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 446 | if (cli_readline_into_buffer("edit: ", buffer, 0) < 0) |
Joe Hershberger | 18a3cce | 2013-02-08 10:12:34 +0000 | [diff] [blame] | 447 | return 1; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 448 | |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 449 | if (buffer[0] == '\0') { |
| 450 | const char * const _argv[3] = { "setenv", argv[1], NULL }; |
| 451 | |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 452 | return env_do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE); |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 453 | } else { |
| 454 | const char * const _argv[4] = { "setenv", argv[1], buffer, |
| 455 | NULL }; |
| 456 | |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 457 | return env_do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE); |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 458 | } |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 459 | } |
| 460 | #endif /* CONFIG_CMD_EDITENV */ |
| 461 | |
Troy Kisky | 289aa6a | 2023-03-13 14:31:24 -0700 | [diff] [blame] | 462 | #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 463 | static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc, |
| 464 | char *const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 465 | { |
Simon Glass | 0151009 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 466 | return env_save() ? 1 : 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 467 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 468 | |
Mike Frysinger | ba69dc2 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 469 | U_BOOT_CMD( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 470 | saveenv, 1, 0, do_env_save, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 471 | "save environment variables to persistent storage", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 472 | "" |
Mike Frysinger | ba69dc2 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 473 | ); |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 474 | |
| 475 | #if defined(CONFIG_CMD_ERASEENV) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 476 | static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc, |
| 477 | char *const argv[]) |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 478 | { |
| 479 | return env_erase() ? 1 : 0; |
| 480 | } |
| 481 | |
| 482 | U_BOOT_CMD( |
| 483 | eraseenv, 1, 0, do_env_erase, |
| 484 | "erase environment variables from persistent storage", |
| 485 | "" |
| 486 | ); |
| 487 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 488 | #endif |
Patrick Delaunay | 0115dd3 | 2020-07-28 11:51:20 +0200 | [diff] [blame] | 489 | |
| 490 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
| 491 | static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 492 | char *const argv[]) |
| 493 | { |
| 494 | return env_reload() ? 1 : 0; |
| 495 | } |
| 496 | #endif |
Patrick Delaunay | a97d22e | 2020-07-28 11:51:21 +0200 | [diff] [blame] | 497 | |
| 498 | #if defined(CONFIG_CMD_NVEDIT_SELECT) |
| 499 | static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc, |
| 500 | char *const argv[]) |
| 501 | { |
| 502 | return env_select(argv[1]) ? 1 : 0; |
| 503 | } |
| 504 | #endif |
| 505 | |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 506 | #endif /* CONFIG_XPL_BUILD */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 507 | |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 508 | #ifndef CONFIG_XPL_BUILD |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 509 | static int do_env_default(struct cmd_tbl *cmdtp, int flag, |
| 510 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 511 | { |
Yaniv Levinsky | 5a04264 | 2018-06-24 19:16:56 +0300 | [diff] [blame] | 512 | int all = 0, env_flag = H_INTERACTIVE; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 513 | |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 514 | debug("Initial value for argc=%d\n", argc); |
| 515 | while (--argc > 0 && **++argv == '-') { |
| 516 | char *arg = *argv; |
| 517 | |
| 518 | while (*++arg) { |
| 519 | switch (*arg) { |
| 520 | case 'a': /* default all */ |
| 521 | all = 1; |
| 522 | break; |
| 523 | case 'f': /* force */ |
Yaniv Levinsky | 3009149 | 2018-06-24 19:16:54 +0300 | [diff] [blame] | 524 | env_flag |= H_FORCE; |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 525 | break; |
Rasmus Villemoes | 4d28fcd | 2024-10-30 22:34:02 +0100 | [diff] [blame] | 526 | case 'k': |
| 527 | env_flag |= H_NOCLEAR; |
| 528 | break; |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 529 | default: |
| 530 | return cmd_usage(cmdtp); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | debug("Final value for argc=%d\n", argc); |
| 535 | if (all && (argc == 0)) { |
| 536 | /* Reset the whole environment */ |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 537 | env_set_default("## Resetting to default environment\n", |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 538 | env_flag); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | if (!all && (argc > 0)) { |
| 542 | /* Reset individual variables */ |
Simon Glass | 0b9d8a0 | 2019-08-01 09:46:56 -0600 | [diff] [blame] | 543 | env_set_default_vars(argc, argv, env_flag); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | return cmd_usage(cmdtp); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 548 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 549 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 550 | static int do_env_delete(struct cmd_tbl *cmdtp, int flag, |
| 551 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 552 | { |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 553 | int env_flag = H_INTERACTIVE; |
| 554 | int ret = 0; |
| 555 | |
| 556 | debug("Initial value for argc=%d\n", argc); |
| 557 | while (argc > 1 && **(argv + 1) == '-') { |
| 558 | char *arg = *++argv; |
| 559 | |
| 560 | --argc; |
| 561 | while (*++arg) { |
| 562 | switch (*arg) { |
| 563 | case 'f': /* force */ |
| 564 | env_flag |= H_FORCE; |
| 565 | break; |
| 566 | default: |
| 567 | return CMD_RET_USAGE; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | debug("Final value for argc=%d\n", argc); |
| 572 | |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 573 | env_inc_id(); |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 574 | |
| 575 | while (--argc > 0) { |
| 576 | char *name = *++argv; |
| 577 | |
Simon Glass | 96434a7 | 2020-11-05 10:33:37 -0700 | [diff] [blame] | 578 | if (hdelete_r(name, &env_htab, env_flag)) |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 579 | ret = 1; |
| 580 | } |
| 581 | |
| 582 | return ret; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 585 | #ifdef CONFIG_CMD_EXPORTENV |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 586 | /* |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 587 | * env export [-t | -b | -c] [-s size] addr [var ...] |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 588 | * -t: export as text format; if size is given, data will be |
| 589 | * padded with '\0' bytes; if not, one terminating '\0' |
| 590 | * will be added (which is included in the "filesize" |
| 591 | * setting so you can for exmple copy this to flash and |
| 592 | * keep the termination). |
| 593 | * -b: export as binary format (name=value pairs separated by |
| 594 | * '\0', list end marked by double "\0\0") |
| 595 | * -c: export as checksum protected environment format as |
| 596 | * used for example by "saveenv" command |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 597 | * -s size: |
| 598 | * size of output buffer |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 599 | * addr: memory address where environment gets stored |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 600 | * var... List of variable names that get included into the |
| 601 | * export. Without arguments, the whole environment gets |
| 602 | * exported. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 603 | * |
| 604 | * With "-c" and size is NOT given, then the export command will |
| 605 | * format the data as currently used for the persistent storage, |
| 606 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 607 | * prepend a valid CRC32 checksum and, in case of redundant |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 608 | * environment, a "current" redundancy flag. If size is given, this |
| 609 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 |
| 610 | * checksum and redundancy flag will be inserted. |
| 611 | * |
| 612 | * With "-b" and "-t", always only the real data (including a |
| 613 | * terminating '\0' byte) will be written; here the optional size |
| 614 | * argument will be used to make sure not to overflow the user |
| 615 | * provided buffer; the command will abort if the size is not |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 616 | * sufficient. Any remaining space will be '\0' padded. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 617 | * |
| 618 | * On successful return, the variable "filesize" will be set. |
| 619 | * Note that filesize includes the trailing/terminating '\0' byte(s). |
| 620 | * |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 621 | * Usage scenario: create a text snapshot/backup of the current settings: |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 622 | * |
| 623 | * => env export -t 100000 |
| 624 | * => era ${backup_addr} +${filesize} |
| 625 | * => cp.b 100000 ${backup_addr} ${filesize} |
| 626 | * |
| 627 | * Re-import this snapshot, deleting all other settings: |
| 628 | * |
| 629 | * => env import -d -t ${backup_addr} |
| 630 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 631 | static int do_env_export(struct cmd_tbl *cmdtp, int flag, |
| 632 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 633 | { |
| 634 | char buf[32]; |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 635 | ulong addr; |
| 636 | char *ptr, *cmd, *res; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 637 | size_t size = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 638 | ssize_t len; |
| 639 | env_t *envp; |
| 640 | char sep = '\n'; |
| 641 | int chk = 0; |
| 642 | int fmt = 0; |
| 643 | |
| 644 | cmd = *argv; |
| 645 | |
| 646 | while (--argc > 0 && **++argv == '-') { |
| 647 | char *arg = *argv; |
| 648 | while (*++arg) { |
| 649 | switch (*arg) { |
| 650 | case 'b': /* raw binary format */ |
| 651 | if (fmt++) |
| 652 | goto sep_err; |
| 653 | sep = '\0'; |
| 654 | break; |
| 655 | case 'c': /* external checksum format */ |
| 656 | if (fmt++) |
| 657 | goto sep_err; |
| 658 | sep = '\0'; |
| 659 | chk = 1; |
| 660 | break; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 661 | case 's': /* size given */ |
| 662 | if (--argc <= 0) |
| 663 | return cmd_usage(cmdtp); |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 664 | size = hextoul(*++argv, NULL); |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 665 | goto NXTARG; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 666 | case 't': /* text format */ |
| 667 | if (fmt++) |
| 668 | goto sep_err; |
| 669 | sep = '\n'; |
| 670 | break; |
| 671 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 672 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 673 | } |
| 674 | } |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 675 | NXTARG: ; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 678 | if (argc < 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 679 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 680 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 681 | addr = hextoul(argv[0], NULL); |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 682 | ptr = map_sysmem(addr, size); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 683 | |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 684 | if (size) |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 685 | memset(ptr, '\0', size); |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 686 | |
| 687 | argc--; |
| 688 | argv++; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 689 | |
| 690 | if (sep) { /* export as text file */ |
Wolfgang Denk | ea009d4 | 2013-03-23 23:50:28 +0000 | [diff] [blame] | 691 | len = hexport_r(&env_htab, sep, |
| 692 | H_MATCH_KEY | H_MATCH_IDENT, |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 693 | &ptr, size, argc, argv); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 694 | if (len < 0) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 695 | pr_err("## Error: Cannot export environment: errno = %d\n", |
| 696 | errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 697 | return 1; |
| 698 | } |
Andreas Bießmann | 8c3aff5 | 2011-02-09 15:10:29 +0100 | [diff] [blame] | 699 | sprintf(buf, "%zX", (size_t)len); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 700 | env_set("filesize", buf); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 705 | envp = (env_t *)ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 706 | |
| 707 | if (chk) /* export as checksum protected block */ |
| 708 | res = (char *)envp->data; |
| 709 | else /* export as raw binary data */ |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 710 | res = ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 711 | |
Wolfgang Denk | ea009d4 | 2013-03-23 23:50:28 +0000 | [diff] [blame] | 712 | len = hexport_r(&env_htab, '\0', |
| 713 | H_MATCH_KEY | H_MATCH_IDENT, |
| 714 | &res, ENV_SIZE, argc, argv); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 715 | if (len < 0) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 716 | pr_err("## Error: Cannot export environment: errno = %d\n", |
| 717 | errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | if (chk) { |
Neil Stainton | d71b029 | 2018-09-12 11:02:51 +0000 | [diff] [blame] | 722 | envp->crc = crc32(0, envp->data, |
| 723 | size ? size - offsetof(env_t, data) : ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 724 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 725 | envp->flags = ENV_REDUND_ACTIVE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 726 | #endif |
| 727 | } |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 728 | env_set_hex("filesize", len + offsetof(env_t, data)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 729 | |
| 730 | return 0; |
| 731 | |
| 732 | sep_err: |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 733 | printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 734 | cmd); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 735 | return 1; |
| 736 | } |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 737 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 738 | |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 739 | #ifdef CONFIG_CMD_IMPORTENV |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 740 | /* |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 741 | * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] |
| 742 | * -d: delete existing environment before importing if no var is |
| 743 | * passed; if vars are passed, if one var is in the current |
| 744 | * environment but not in the environment at addr, delete var from |
| 745 | * current environment; |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 746 | * otherwise overwrite / append to existing definitions |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 747 | * -t: assume text format; either "size" must be given or the |
| 748 | * text data must be '\0' terminated |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 749 | * -r: handle CRLF like LF, that means exported variables with |
| 750 | * a content which ends with \r won't get imported. Used |
| 751 | * to import text files created with editors which are using CRLF |
| 752 | * for line endings. Only effective in addition to -t. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 753 | * -b: assume binary format ('\0' separated, "\0\0" terminated) |
| 754 | * -c: assume checksum protected environment format |
| 755 | * addr: memory address to read from |
| 756 | * size: length of input data; if missing, proper '\0' |
| 757 | * termination is mandatory |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 758 | * if var is set and size should be missing (i.e. '\0' |
| 759 | * termination), set size to '-' |
| 760 | * var... List of the names of the only variables that get imported from |
| 761 | * the environment at address 'addr'. Without arguments, the whole |
| 762 | * environment gets imported. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 763 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 764 | static int do_env_import(struct cmd_tbl *cmdtp, int flag, |
| 765 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 766 | { |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 767 | ulong addr; |
| 768 | char *cmd, *ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 769 | char sep = '\n'; |
| 770 | int chk = 0; |
| 771 | int fmt = 0; |
| 772 | int del = 0; |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 773 | int crlf_is_lf = 0; |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 774 | int wl = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 775 | size_t size; |
| 776 | |
| 777 | cmd = *argv; |
| 778 | |
| 779 | while (--argc > 0 && **++argv == '-') { |
| 780 | char *arg = *argv; |
| 781 | while (*++arg) { |
| 782 | switch (*arg) { |
| 783 | case 'b': /* raw binary format */ |
| 784 | if (fmt++) |
| 785 | goto sep_err; |
| 786 | sep = '\0'; |
| 787 | break; |
| 788 | case 'c': /* external checksum format */ |
| 789 | if (fmt++) |
| 790 | goto sep_err; |
| 791 | sep = '\0'; |
| 792 | chk = 1; |
| 793 | break; |
| 794 | case 't': /* text format */ |
| 795 | if (fmt++) |
| 796 | goto sep_err; |
| 797 | sep = '\n'; |
| 798 | break; |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 799 | case 'r': /* handle CRLF like LF */ |
| 800 | crlf_is_lf = 1; |
| 801 | break; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 802 | case 'd': |
| 803 | del = 1; |
| 804 | break; |
| 805 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 806 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 811 | if (argc < 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 812 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 813 | |
| 814 | if (!fmt) |
| 815 | printf("## Warning: defaulting to text format\n"); |
| 816 | |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 817 | if (sep != '\n' && crlf_is_lf ) |
| 818 | crlf_is_lf = 0; |
| 819 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 820 | addr = hextoul(argv[0], NULL); |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 821 | ptr = map_sysmem(addr, 0); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 822 | |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 823 | if (argc >= 2 && strcmp(argv[1], "-")) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 824 | size = hextoul(argv[1], NULL); |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 825 | } else if (chk) { |
Tom Rini | 3775dcd | 2014-03-04 15:52:35 -0500 | [diff] [blame] | 826 | puts("## Error: external checksum format must pass size\n"); |
| 827 | return CMD_RET_FAILURE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 828 | } else { |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 829 | char *s = ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 830 | |
| 831 | size = 0; |
| 832 | |
| 833 | while (size < MAX_ENV_SIZE) { |
| 834 | if ((*s == sep) && (*(s+1) == '\0')) |
| 835 | break; |
| 836 | ++s; |
| 837 | ++size; |
| 838 | } |
| 839 | if (size == MAX_ENV_SIZE) { |
| 840 | printf("## Warning: Input data exceeds %d bytes" |
| 841 | " - truncated\n", MAX_ENV_SIZE); |
| 842 | } |
Horst Kronstorfer | d3f80c7 | 2011-12-16 23:33:10 +0000 | [diff] [blame] | 843 | size += 2; |
Simon Glass | 79afc88 | 2011-11-04 06:42:36 +0000 | [diff] [blame] | 844 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 847 | if (argc > 2) |
| 848 | wl = 1; |
| 849 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 850 | if (chk) { |
| 851 | uint32_t crc; |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 852 | env_t *ep = (env_t *)ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 853 | |
Pedro Aguilar | 142775a | 2020-08-31 11:01:41 +0200 | [diff] [blame] | 854 | if (size <= offsetof(env_t, data)) { |
| 855 | printf("## Error: Invalid size 0x%zX\n", size); |
| 856 | return 1; |
| 857 | } |
| 858 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 859 | size -= offsetof(env_t, data); |
| 860 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 861 | |
| 862 | if (crc32(0, ep->data, size) != crc) { |
| 863 | puts("## Error: bad CRC, import failed\n"); |
| 864 | return 1; |
| 865 | } |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 866 | ptr = (char *)ep->data; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 867 | } |
| 868 | |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 869 | if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, |
| 870 | crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 871 | pr_err("## Error: Environment import failed: errno = %d\n", |
| 872 | errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 873 | return 1; |
| 874 | } |
| 875 | gd->flags |= GD_FLG_ENV_READY; |
| 876 | |
| 877 | return 0; |
| 878 | |
| 879 | sep_err: |
| 880 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 881 | cmd); |
| 882 | return 1; |
| 883 | } |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 884 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 885 | |
Samuel Dionne-Riel | ec57bd7 | 2021-12-20 18:31:56 -0500 | [diff] [blame] | 886 | #if defined(CONFIG_CMD_NVEDIT_INDIRECT) |
| 887 | static int do_env_indirect(struct cmd_tbl *cmdtp, int flag, |
| 888 | int argc, char *const argv[]) |
| 889 | { |
| 890 | char *to = argv[1]; |
| 891 | char *from = argv[2]; |
| 892 | char *default_value = NULL; |
| 893 | int ret = 0; |
Rasmus Villemoes | 732b082 | 2023-03-06 14:27:21 +0100 | [diff] [blame] | 894 | char *val; |
Samuel Dionne-Riel | ec57bd7 | 2021-12-20 18:31:56 -0500 | [diff] [blame] | 895 | |
| 896 | if (argc < 3 || argc > 4) { |
| 897 | return CMD_RET_USAGE; |
| 898 | } |
| 899 | |
| 900 | if (argc == 4) { |
| 901 | default_value = argv[3]; |
| 902 | } |
| 903 | |
Rasmus Villemoes | 732b082 | 2023-03-06 14:27:21 +0100 | [diff] [blame] | 904 | val = env_get(from) ?: default_value; |
| 905 | if (!val) { |
Samuel Dionne-Riel | ec57bd7 | 2021-12-20 18:31:56 -0500 | [diff] [blame] | 906 | printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from); |
| 907 | |
| 908 | return CMD_RET_FAILURE; |
| 909 | } |
| 910 | |
Rasmus Villemoes | 732b082 | 2023-03-06 14:27:21 +0100 | [diff] [blame] | 911 | ret = env_set(to, val); |
Samuel Dionne-Riel | ec57bd7 | 2021-12-20 18:31:56 -0500 | [diff] [blame] | 912 | |
| 913 | if (ret == 0) { |
| 914 | return CMD_RET_SUCCESS; |
| 915 | } |
| 916 | else { |
| 917 | return CMD_RET_FAILURE; |
| 918 | } |
| 919 | } |
| 920 | #endif |
| 921 | |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 922 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
| 923 | /* |
| 924 | * print_env_info - print environment information |
| 925 | */ |
| 926 | static int print_env_info(void) |
| 927 | { |
| 928 | const char *value; |
| 929 | |
| 930 | /* print environment validity value */ |
| 931 | switch (gd->env_valid) { |
| 932 | case ENV_INVALID: |
| 933 | value = "invalid"; |
| 934 | break; |
| 935 | case ENV_VALID: |
| 936 | value = "valid"; |
| 937 | break; |
| 938 | case ENV_REDUND: |
| 939 | value = "redundant"; |
| 940 | break; |
| 941 | default: |
| 942 | value = "unknown"; |
| 943 | break; |
| 944 | } |
| 945 | printf("env_valid = %s\n", value); |
| 946 | |
| 947 | /* print environment ready flag */ |
| 948 | value = gd->flags & GD_FLG_ENV_READY ? "true" : "false"; |
| 949 | printf("env_ready = %s\n", value); |
| 950 | |
| 951 | /* print environment using default flag */ |
| 952 | value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false"; |
| 953 | printf("env_use_default = %s\n", value); |
| 954 | |
| 955 | return CMD_RET_SUCCESS; |
| 956 | } |
| 957 | |
| 958 | #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */ |
| 959 | #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */ |
| 960 | |
| 961 | /* |
| 962 | * env info - display environment information |
| 963 | * env info [-d] - evaluate whether default environment is used |
| 964 | * env info [-p] - evaluate whether environment can be persisted |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 965 | * Add [-q] - quiet mode, use only for command result, for test by example: |
| 966 | * test env info -p -d -q |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 967 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 968 | static int do_env_info(struct cmd_tbl *cmdtp, int flag, |
| 969 | int argc, char *const argv[]) |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 970 | { |
| 971 | int eval_flags = 0; |
| 972 | int eval_results = 0; |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 973 | bool quiet = false; |
Troy Kisky | 289aa6a | 2023-03-13 14:31:24 -0700 | [diff] [blame] | 974 | #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT) |
Patrick Delaunay | 2f96b32 | 2020-06-19 14:03:35 +0200 | [diff] [blame] | 975 | enum env_location loc; |
| 976 | #endif |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 977 | |
| 978 | /* display environment information */ |
| 979 | if (argc <= 1) |
| 980 | return print_env_info(); |
| 981 | |
| 982 | /* process options */ |
| 983 | while (--argc > 0 && **++argv == '-') { |
| 984 | char *arg = *argv; |
| 985 | |
| 986 | while (*++arg) { |
| 987 | switch (*arg) { |
| 988 | case 'd': |
| 989 | eval_flags |= ENV_INFO_IS_DEFAULT; |
| 990 | break; |
| 991 | case 'p': |
| 992 | eval_flags |= ENV_INFO_IS_PERSISTED; |
| 993 | break; |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 994 | case 'q': |
| 995 | quiet = true; |
| 996 | break; |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 997 | default: |
| 998 | return CMD_RET_USAGE; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* evaluate whether default environment is used */ |
| 1004 | if (eval_flags & ENV_INFO_IS_DEFAULT) { |
| 1005 | if (gd->flags & GD_FLG_ENV_DEFAULT) { |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1006 | if (!quiet) |
| 1007 | printf("Default environment is used\n"); |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1008 | eval_results |= ENV_INFO_IS_DEFAULT; |
| 1009 | } else { |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1010 | if (!quiet) |
| 1011 | printf("Environment was loaded from persistent storage\n"); |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /* evaluate whether environment can be persisted */ |
| 1016 | if (eval_flags & ENV_INFO_IS_PERSISTED) { |
Troy Kisky | 289aa6a | 2023-03-13 14:31:24 -0700 | [diff] [blame] | 1017 | #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT) |
Patrick Delaunay | 2f96b32 | 2020-06-19 14:03:35 +0200 | [diff] [blame] | 1018 | loc = env_get_location(ENVOP_SAVE, gd->env_load_prio); |
| 1019 | if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) { |
| 1020 | if (!quiet) |
| 1021 | printf("Environment can be persisted\n"); |
| 1022 | eval_results |= ENV_INFO_IS_PERSISTED; |
| 1023 | } else { |
| 1024 | if (!quiet) |
| 1025 | printf("Environment cannot be persisted\n"); |
| 1026 | } |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1027 | #else |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1028 | if (!quiet) |
| 1029 | printf("Environment cannot be persisted\n"); |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1030 | #endif |
| 1031 | } |
| 1032 | |
| 1033 | /* The result of evaluations is combined with AND */ |
| 1034 | if (eval_flags != eval_results) |
| 1035 | return CMD_RET_FAILURE; |
| 1036 | |
| 1037 | return CMD_RET_SUCCESS; |
| 1038 | } |
| 1039 | #endif |
| 1040 | |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1041 | #if defined(CONFIG_CMD_ENV_EXISTS) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1042 | static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1043 | char *const argv[]) |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1044 | { |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 1045 | struct env_entry e, *ep; |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1046 | |
| 1047 | if (argc < 2) |
| 1048 | return CMD_RET_USAGE; |
| 1049 | |
| 1050 | e.key = argv[1]; |
| 1051 | e.data = NULL; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 1052 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1053 | |
| 1054 | return (ep == NULL) ? 1 : 0; |
| 1055 | } |
| 1056 | #endif |
| 1057 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1058 | /* |
| 1059 | * New command line interface: "env" command with subcommands |
| 1060 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1061 | static struct cmd_tbl cmd_env_sub[] = { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1062 | #if defined(CONFIG_CMD_ASKENV) |
| 1063 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), |
| 1064 | #endif |
| 1065 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1066 | U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1067 | #if defined(CONFIG_CMD_EDITENV) |
| 1068 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), |
| 1069 | #endif |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1070 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1071 | U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), |
| 1072 | #endif |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1073 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1074 | U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), |
| 1075 | #endif |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1076 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1077 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1078 | #endif |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1079 | #if defined(CONFIG_CMD_GREPENV) |
| 1080 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), |
| 1081 | #endif |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1082 | #if defined(CONFIG_CMD_IMPORTENV) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1083 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1084 | #endif |
Samuel Dionne-Riel | ec57bd7 | 2021-12-20 18:31:56 -0500 | [diff] [blame] | 1085 | #if defined(CONFIG_CMD_NVEDIT_INDIRECT) |
| 1086 | U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""), |
| 1087 | #endif |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1088 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1089 | U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""), |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1090 | #endif |
Patrick Delaunay | 0115dd3 | 2020-07-28 11:51:20 +0200 | [diff] [blame] | 1091 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
| 1092 | U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""), |
| 1093 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1094 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
| 1095 | #if defined(CONFIG_CMD_RUN) |
| 1096 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), |
| 1097 | #endif |
Troy Kisky | 289aa6a | 2023-03-13 14:31:24 -0700 | [diff] [blame] | 1098 | #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1099 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 1100 | #if defined(CONFIG_CMD_ERASEENV) |
| 1101 | U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""), |
| 1102 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1103 | #endif |
Patrick Delaunay | a97d22e | 2020-07-28 11:51:21 +0200 | [diff] [blame] | 1104 | #if defined(CONFIG_CMD_NVEDIT_SELECT) |
| 1105 | U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""), |
| 1106 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1107 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1108 | #if defined(CONFIG_CMD_ENV_EXISTS) |
| 1109 | U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""), |
| 1110 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1111 | }; |
| 1112 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1113 | static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1114 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1115 | struct cmd_tbl *cp; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1116 | |
Thomas Weber | 5904da0 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1117 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1118 | return CMD_RET_USAGE; |
Thomas Weber | 5904da0 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1119 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1120 | /* drop initial "env" arg */ |
| 1121 | argc--; |
| 1122 | argv++; |
| 1123 | |
| 1124 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1125 | |
| 1126 | if (cp) |
| 1127 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1128 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1129 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 1132 | U_BOOT_LONGHELP(env, |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1133 | #if defined(CONFIG_CMD_ASKENV) |
| 1134 | "ask name [message] [size] - ask for environment variable\nenv " |
| 1135 | #endif |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1136 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1137 | "callbacks - print callbacks and their associated variables\nenv " |
| 1138 | #endif |
Rasmus Villemoes | 4d28fcd | 2024-10-30 22:34:02 +0100 | [diff] [blame] | 1139 | "default [-k] [-f] -a - [forcibly] reset default environment\n" |
| 1140 | "env default [-k] [-f] var [...] - [forcibly] reset variable(s) to their default values\n" |
| 1141 | " \"-k\": keep variables not defined in default environment\n" |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1142 | "env delete [-f] var [...] - [forcibly] delete variable(s)\n" |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1143 | #if defined(CONFIG_CMD_EDITENV) |
| 1144 | "env edit name - edit environment variable\n" |
| 1145 | #endif |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1146 | #if defined(CONFIG_CMD_ENV_EXISTS) |
| 1147 | "env exists name - tests for existence of variable\n" |
| 1148 | #endif |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1149 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 1150 | "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1151 | #endif |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1152 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1153 | "env flags - print variables that have non-default flags\n" |
| 1154 | #endif |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1155 | #if defined(CONFIG_CMD_GREPENV) |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1156 | #ifdef CONFIG_REGEX |
| 1157 | "env grep [-e] [-n | -v | -b] string [...] - search environment\n" |
| 1158 | #else |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1159 | "env grep [-n | -v | -b] string [...] - search environment\n" |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1160 | #endif |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1161 | #endif |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1162 | #if defined(CONFIG_CMD_IMPORTENV) |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1163 | "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n" |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1164 | #endif |
Samuel Dionne-Riel | ec57bd7 | 2021-12-20 18:31:56 -0500 | [diff] [blame] | 1165 | #if defined(CONFIG_CMD_NVEDIT_INDIRECT) |
| 1166 | "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n" |
| 1167 | #endif |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1168 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
| 1169 | "env info - display environment information\n" |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1170 | "env info [-d] [-p] [-q] - evaluate environment information\n" |
| 1171 | " \"-d\": default environment is used\n" |
| 1172 | " \"-p\": environment can be persisted\n" |
| 1173 | " \"-q\": quiet output\n" |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1174 | #endif |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1175 | "env print [-a | name ...] - print environment\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1176 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1177 | "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1178 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1179 | #if defined(CONFIG_CMD_RUN) |
| 1180 | "env run var [...] - run commands in an environment variable\n" |
| 1181 | #endif |
Troy Kisky | 289aa6a | 2023-03-13 14:31:24 -0700 | [diff] [blame] | 1182 | #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1183 | "env save - save environment\n" |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 1184 | #if defined(CONFIG_CMD_ERASEENV) |
| 1185 | "env erase - erase environment\n" |
| 1186 | #endif |
Horst Kronstorfer | d798a9b | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 1187 | #endif |
Patrick Delaunay | 0115dd3 | 2020-07-28 11:51:20 +0200 | [diff] [blame] | 1188 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
| 1189 | "env load - load environment\n" |
| 1190 | #endif |
Patrick Delaunay | a97d22e | 2020-07-28 11:51:21 +0200 | [diff] [blame] | 1191 | #if defined(CONFIG_CMD_NVEDIT_SELECT) |
| 1192 | "env select [target] - select environment target\n" |
| 1193 | #endif |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1194 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
Maxim Uvarov | 8f0ac53 | 2020-08-28 22:20:10 +0300 | [diff] [blame] | 1195 | "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1196 | " - set UEFI variable; unset if '-i' or 'arg' not specified\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1197 | #endif |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 1198 | "env set [-f] name [arg ...]\n"); |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1199 | |
| 1200 | U_BOOT_CMD( |
| 1201 | env, CONFIG_SYS_MAXARGS, 1, do_env, |
| 1202 | "environment handling commands", env_help_text |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1203 | ); |
| 1204 | |
| 1205 | /* |
| 1206 | * Old command line interface, kept for compatibility |
| 1207 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1208 | |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1209 | #if defined(CONFIG_CMD_EDITENV) |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1210 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1211 | editenv, 2, 0, do_env_edit, |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1212 | "edit environment variable", |
| 1213 | "name\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1214 | " - edit environment variable 'name'", |
| 1215 | var_complete |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1216 | ); |
| 1217 | #endif |
| 1218 | |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1219 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1220 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1221 | "print environment variables", |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1222 | "[-a]\n - print [all] values of all environment variables\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1223 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1224 | "printenv -e [-guid guid][-n] [name ...]\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1225 | " - print UEFI variable 'name' or all the variables\n" |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1226 | " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1227 | " \"-n\": suppress dumping variable's value\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1228 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1229 | "printenv name ...\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1230 | " - print value of environment variable 'name'", |
| 1231 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1232 | ); |
| 1233 | |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1234 | #ifdef CONFIG_CMD_GREPENV |
| 1235 | U_BOOT_CMD_COMPLETE( |
| 1236 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, |
| 1237 | "search environment variables", |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1238 | #ifdef CONFIG_REGEX |
| 1239 | "[-e] [-n | -v | -b] string ...\n" |
| 1240 | #else |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1241 | "[-n | -v | -b] string ...\n" |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1242 | #endif |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1243 | " - list environment name=value pairs matching 'string'\n" |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1244 | #ifdef CONFIG_REGEX |
| 1245 | " \"-e\": enable regular expressions;\n" |
| 1246 | #endif |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1247 | " \"-n\": search variable names; \"-v\": search values;\n" |
| 1248 | " \"-b\": search both names and values (default)", |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1249 | var_complete |
| 1250 | ); |
| 1251 | #endif |
| 1252 | |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1253 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1254 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1255 | "set environment variables", |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1256 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 1257 | "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n" |
Maxim Uvarov | 8f0ac53 | 2020-08-28 22:20:10 +0300 | [diff] [blame] | 1258 | " [-i addr:size name], or [name [value ...]]\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1259 | " - set UEFI variable 'name' to 'value' ...'\n" |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1260 | " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1261 | " \"-nv\": set non-volatile attribute\n" |
| 1262 | " \"-bs\": set boot-service attribute\n" |
| 1263 | " \"-rt\": set runtime attribute\n" |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 1264 | " \"-at\": set time-based authentication attribute\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1265 | " \"-a\": append-write\n" |
| 1266 | " \"-i addr,size\": use <addr,size> as variable's value\n" |
| 1267 | " \"-v\": verbose message\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1268 | " - delete UEFI variable 'name' if 'value' not specified\n" |
| 1269 | #endif |
| 1270 | "setenv [-f] name value ...\n" |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 1271 | " - [forcibly] set environment variable 'name' to 'value ...'\n" |
| 1272 | "setenv [-f] name\n" |
| 1273 | " - [forcibly] delete environment variable 'name'", |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1274 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1275 | ); |
| 1276 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1277 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1278 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1279 | U_BOOT_CMD( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1280 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1281 | "get environment variables from stdin", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1282 | "name [message] [size]\n" |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 1283 | " - get environment variable 'name' from stdin (max 'size' chars)" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1284 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1285 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1286 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1287 | #if defined(CONFIG_CMD_RUN) |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1288 | U_BOOT_CMD_COMPLETE( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1289 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1290 | "run commands in an environment variable", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1291 | "var [...]\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1292 | " - run the commands in the environment variable(s) 'var'", |
| 1293 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1294 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1295 | #endif |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 1296 | #endif /* CONFIG_XPL_BUILD */ |