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 | |
| 26 | #include <common.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> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 33 | #include <net.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 34 | #include <search.h> |
| 35 | #include <errno.h> |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 36 | #include <malloc.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 37 | #include <mapmem.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 38 | #include <linux/bitops.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 39 | #include <u-boot/crc.h> |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 40 | #include <watchdog.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 | |
Patrick Delaunay | 2643c85 | 2019-02-18 10:58:16 +0100 | [diff] [blame] | 47 | #if defined(CONFIG_ENV_IS_IN_EEPROM) || \ |
| 48 | defined(CONFIG_ENV_IS_IN_FLASH) || \ |
| 49 | defined(CONFIG_ENV_IS_IN_MMC) || \ |
| 50 | defined(CONFIG_ENV_IS_IN_FAT) || \ |
| 51 | defined(CONFIG_ENV_IS_IN_EXT4) || \ |
| 52 | defined(CONFIG_ENV_IS_IN_NAND) || \ |
| 53 | defined(CONFIG_ENV_IS_IN_NVRAM) || \ |
| 54 | defined(CONFIG_ENV_IS_IN_ONENAND) || \ |
| 55 | defined(CONFIG_ENV_IS_IN_SATA) || \ |
| 56 | defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \ |
| 57 | defined(CONFIG_ENV_IS_IN_REMOTE) || \ |
| 58 | defined(CONFIG_ENV_IS_IN_UBI) |
| 59 | |
| 60 | #define ENV_IS_IN_DEVICE |
| 61 | |
| 62 | #endif |
| 63 | |
| 64 | #if !defined(ENV_IS_IN_DEVICE) && \ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 65 | !defined(CONFIG_ENV_IS_NOWHERE) |
Tuomas Tynkkynen | 7b7341d | 2017-10-10 21:59:40 +0300 | [diff] [blame] | 66 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\ |
Lothar Waßmann | d1b88cd | 2017-06-08 14:16:14 +0200 | [diff] [blame] | 67 | NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Maximum expected input data size for import command |
| 72 | */ |
| 73 | #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 74 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 75 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 76 | * This variable is incremented on each do_env_set(), so it can |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 77 | * be used via env_get_id() as an indication, if the environment |
Heiko Schocher | da95427 | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 78 | * has changed or not. So it is possible to reread an environment |
| 79 | * variable only if the environment was changed ... done so for |
| 80 | * example in NetInitLoop() |
| 81 | */ |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 82 | static int env_id = 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 83 | |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 84 | int env_get_id(void) |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 85 | { |
| 86 | return env_id; |
| 87 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 88 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 89 | #ifndef CONFIG_SPL_BUILD |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 90 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 91 | * Command interface: print one or all environment variables |
| 92 | * |
| 93 | * Returns 0 in case of error, or length of printed string |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 94 | */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 95 | static int env_print(char *name, int flag) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 96 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 97 | char *res = NULL; |
Maxime Larocque | 22a4a6c | 2012-09-28 05:00:13 +0000 | [diff] [blame] | 98 | ssize_t len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 99 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 100 | if (name) { /* print a single name */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 101 | struct env_entry e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 102 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | e.key = name; |
| 104 | e.data = NULL; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 105 | hsearch_r(e, ENV_FIND, &ep, &env_htab, flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 106 | if (ep == NULL) |
| 107 | return 0; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 108 | len = printf("%s=%s\n", ep->key, ep->data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 109 | return len; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 110 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 111 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 112 | /* print whole list */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 113 | len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | |
| 115 | if (len > 0) { |
| 116 | puts(res); |
| 117 | free(res); |
| 118 | return len; |
| 119 | } |
| 120 | |
| 121 | /* should never happen */ |
Maxime Larocque | 22a4a6c | 2012-09-28 05:00:13 +0000 | [diff] [blame] | 122 | printf("## Error: cannot export environment\n"); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 123 | return 0; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 126 | static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc, |
| 127 | char *const argv[]) |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 128 | { |
| 129 | int i; |
| 130 | int rcode = 0; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 131 | int env_flag = H_HIDE_DOT; |
| 132 | |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 133 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
| 134 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') |
| 135 | return do_env_print_efi(cmdtp, flag, --argc, ++argv); |
| 136 | #endif |
| 137 | |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 138 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') { |
| 139 | argc--; |
| 140 | argv++; |
| 141 | env_flag &= ~H_HIDE_DOT; |
| 142 | } |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 143 | |
| 144 | if (argc == 1) { |
| 145 | /* print all env vars */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 146 | rcode = env_print(NULL, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 147 | if (!rcode) |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 148 | return 1; |
| 149 | printf("\nEnvironment size: %d/%ld bytes\n", |
| 150 | rcode, (ulong)ENV_SIZE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 154 | /* print selected env vars */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 155 | env_flag &= ~H_HIDE_DOT; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 156 | for (i = 1; i < argc; ++i) { |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 157 | int rc = env_print(argv[i], env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 158 | if (!rc) { |
| 159 | printf("## Error: \"%s\" not defined\n", argv[i]); |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 160 | ++rcode; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 163 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 164 | return rcode; |
| 165 | } |
| 166 | |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 167 | #ifdef CONFIG_CMD_GREPENV |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 168 | static int do_env_grep(struct cmd_tbl *cmdtp, int flag, |
| 169 | int argc, char *const argv[]) |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 170 | { |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 171 | char *res = NULL; |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 172 | int len, grep_how, grep_what; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 173 | |
| 174 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 175 | return CMD_RET_USAGE; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 176 | |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 177 | grep_how = H_MATCH_SUBSTR; /* default: substring search */ |
| 178 | grep_what = H_MATCH_BOTH; /* default: grep names and values */ |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 179 | |
Pierre Aubert | 9a83233 | 2013-10-08 14:20:27 +0200 | [diff] [blame] | 180 | while (--argc > 0 && **++argv == '-') { |
| 181 | char *arg = *argv; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 182 | while (*++arg) { |
| 183 | switch (*arg) { |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 184 | #ifdef CONFIG_REGEX |
| 185 | case 'e': /* use regex matching */ |
| 186 | grep_how = H_MATCH_REGEX; |
| 187 | break; |
| 188 | #endif |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 189 | case 'n': /* grep for name */ |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 190 | grep_what = H_MATCH_KEY; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 191 | break; |
| 192 | case 'v': /* grep for value */ |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 193 | grep_what = H_MATCH_DATA; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 194 | break; |
| 195 | case 'b': /* grep for both */ |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 196 | grep_what = H_MATCH_BOTH; |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 197 | break; |
| 198 | case '-': |
| 199 | goto DONE; |
| 200 | default: |
| 201 | return CMD_RET_USAGE; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | DONE: |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 207 | len = hexport_r(&env_htab, '\n', |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 208 | flag | grep_what | grep_how, |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 209 | &res, 0, argc, argv); |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 210 | |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 211 | if (len > 0) { |
| 212 | puts(res); |
| 213 | free(res); |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Wolfgang Denk | 5a31ea0 | 2013-03-23 23:50:29 +0000 | [diff] [blame] | 216 | if (len < 2) |
| 217 | return 1; |
| 218 | |
| 219 | return 0; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 220 | } |
| 221 | #endif |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 222 | #endif /* CONFIG_SPL_BUILD */ |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 223 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 224 | /* |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 225 | * Set a new environment variable, |
| 226 | * or replace or delete an existing one. |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 227 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 228 | static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 229 | { |
| 230 | int i, len; |
| 231 | char *name, *value, *s; |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 232 | struct env_entry e, *ep; |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 233 | |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 234 | debug("Initial value for argc=%d\n", argc); |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 235 | |
| 236 | #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI) |
| 237 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') |
| 238 | return do_env_set_efi(NULL, flag, --argc, ++argv); |
| 239 | #endif |
| 240 | |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 241 | while (argc > 1 && **(argv + 1) == '-') { |
| 242 | char *arg = *++argv; |
| 243 | |
| 244 | --argc; |
| 245 | while (*++arg) { |
| 246 | switch (*arg) { |
| 247 | case 'f': /* force */ |
| 248 | env_flag |= H_FORCE; |
| 249 | break; |
| 250 | default: |
| 251 | return CMD_RET_USAGE; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | debug("Final value for argc=%d\n", argc); |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 256 | name = argv[1]; |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 257 | |
| 258 | if (strchr(name, '=')) { |
| 259 | printf("## Error: illegal character '='" |
| 260 | "in variable name \"%s\"\n", name); |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | env_id++; |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 265 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 266 | /* Delete only ? */ |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 267 | if (argc < 3 || argv[2] == NULL) { |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 268 | int rc = hdelete_r(name, &env_htab, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 269 | return !rc; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 273 | * Insert / replace new value |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 274 | */ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 275 | for (i = 2, len = 0; i < argc; ++i) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 276 | len += strlen(argv[i]) + 1; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 277 | |
| 278 | value = malloc(len); |
| 279 | if (value == NULL) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 280 | printf("## Can't malloc %d bytes\n", len); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 281 | return 1; |
| 282 | } |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 283 | for (i = 2, s = value; i < argc; ++i) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 284 | char *v = argv[i]; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 285 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 286 | while ((*s++ = *v++) != '\0') |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 287 | ; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 288 | *(s - 1) = ' '; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 289 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 290 | if (s != value) |
| 291 | *--s = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 292 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 293 | e.key = name; |
| 294 | e.data = value; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 295 | hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 296 | free(value); |
| 297 | if (!ep) { |
| 298 | printf("## Error inserting \"%s\" variable, errno=%d\n", |
| 299 | name, errno); |
| 300 | return 1; |
| 301 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 302 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 306 | int env_set(const char *varname, const char *varvalue) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 307 | { |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 308 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
| 309 | |
Joe Hershberger | a7eb1d6 | 2013-04-08 10:32:50 +0000 | [diff] [blame] | 310 | /* before import into hashtable */ |
| 311 | if (!(gd->flags & GD_FLG_ENV_READY)) |
| 312 | return 1; |
| 313 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 314 | if (varvalue == NULL || varvalue[0] == '\0') |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 315 | return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); |
Jeffrey Mann | 9ffd451 | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 316 | else |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 317 | return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 320 | /** |
| 321 | * Set an environment variable to an integer value |
| 322 | * |
Simon Glass | 9602286 | 2013-05-07 06:11:45 +0000 | [diff] [blame] | 323 | * @param varname Environment variable to set |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 324 | * @param value Value to set it to |
| 325 | * @return 0 if ok, 1 on error |
| 326 | */ |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 327 | int env_set_ulong(const char *varname, ulong value) |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 328 | { |
| 329 | /* TODO: this should be unsigned */ |
| 330 | char *str = simple_itoa(value); |
| 331 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 332 | return env_set(varname, str); |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 336 | * Set an environment variable to an value in hex |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 337 | * |
Simon Glass | 9602286 | 2013-05-07 06:11:45 +0000 | [diff] [blame] | 338 | * @param varname Environment variable to set |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 339 | * @param value Value to set it to |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 340 | * @return 0 if ok, 1 on error |
| 341 | */ |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 342 | int env_set_hex(const char *varname, ulong value) |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 343 | { |
| 344 | char str[17]; |
| 345 | |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 346 | sprintf(str, "%lx", value); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 347 | return env_set(varname, str); |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 350 | ulong env_get_hex(const char *varname, ulong default_val) |
Simon Glass | 76b8f79 | 2013-04-20 08:42:43 +0000 | [diff] [blame] | 351 | { |
| 352 | const char *s; |
| 353 | ulong value; |
| 354 | char *endp; |
| 355 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 356 | s = env_get(varname); |
Simon Glass | 76b8f79 | 2013-04-20 08:42:43 +0000 | [diff] [blame] | 357 | if (s) |
| 358 | value = simple_strtoul(s, &endp, 16); |
| 359 | if (!s || endp == s) |
| 360 | return default_val; |
| 361 | |
| 362 | return value; |
| 363 | } |
| 364 | |
Alex Kiernan | 9925f1d | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 365 | int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) |
| 366 | { |
Joe Hershberger | fb8977c | 2019-09-13 19:21:16 -0500 | [diff] [blame] | 367 | string_to_enetaddr(env_get(name), enetaddr); |
Alex Kiernan | 9925f1d | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 368 | return is_valid_ethaddr(enetaddr); |
| 369 | } |
| 370 | |
| 371 | int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) |
| 372 | { |
| 373 | char buf[ARP_HLEN_ASCII + 1]; |
| 374 | |
| 375 | if (eth_env_get_enetaddr(name, (uint8_t *)buf)) |
| 376 | return -EEXIST; |
| 377 | |
| 378 | sprintf(buf, "%pM", enetaddr); |
| 379 | |
| 380 | return env_set(name, buf); |
| 381 | } |
| 382 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 383 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 384 | static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc, |
| 385 | char *const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 386 | { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 387 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 388 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 389 | |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 390 | return _do_env_set(flag, argc, argv, H_INTERACTIVE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 393 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 394 | * Prompt for environment variable |
| 395 | */ |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 396 | #if defined(CONFIG_CMD_ASKENV) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 397 | 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] | 398 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 399 | char message[CONFIG_SYS_CBSIZE]; |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 400 | int i, len, pos, size; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 401 | char *local_args[4]; |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 402 | char *endptr; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 403 | |
| 404 | local_args[0] = argv[0]; |
| 405 | local_args[1] = argv[1]; |
| 406 | local_args[2] = NULL; |
| 407 | local_args[3] = NULL; |
| 408 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 409 | /* |
| 410 | * Check the syntax: |
| 411 | * |
| 412 | * env_ask envname [message1 ...] [size] |
| 413 | */ |
| 414 | if (argc == 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 415 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 416 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 417 | /* |
| 418 | * We test the last argument if it can be converted |
| 419 | * into a decimal number. If yes, we assume it's |
| 420 | * the size. Otherwise we echo it as part of the |
| 421 | * message. |
| 422 | */ |
| 423 | i = simple_strtoul(argv[argc - 1], &endptr, 10); |
| 424 | if (*endptr != '\0') { /* no size */ |
| 425 | size = CONFIG_SYS_CBSIZE - 1; |
| 426 | } else { /* size given */ |
| 427 | size = i; |
| 428 | --argc; |
| 429 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 430 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 431 | if (argc <= 2) { |
| 432 | sprintf(message, "Please enter '%s': ", argv[1]); |
| 433 | } else { |
| 434 | /* env_ask envname message1 ... messagen [size] */ |
Tom Rini | bf52fcd | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 435 | for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) { |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 436 | if (pos) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 437 | message[pos++] = ' '; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 438 | |
Tom Rini | c667723 | 2017-09-26 19:37:11 -0400 | [diff] [blame] | 439 | strncpy(message + pos, argv[i], sizeof(message) - pos); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 440 | pos += strlen(argv[i]); |
| 441 | } |
Tom Rini | c667723 | 2017-09-26 19:37:11 -0400 | [diff] [blame] | 442 | if (pos < sizeof(message) - 1) { |
| 443 | message[pos++] = ' '; |
| 444 | message[pos] = '\0'; |
| 445 | } else |
| 446 | message[CONFIG_SYS_CBSIZE - 1] = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 449 | if (size >= CONFIG_SYS_CBSIZE) |
| 450 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 451 | |
| 452 | if (size <= 0) |
| 453 | return 1; |
| 454 | |
| 455 | /* prompt for input */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 456 | len = cli_readline(message); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 457 | |
| 458 | if (size < len) |
| 459 | console_buffer[size] = '\0'; |
| 460 | |
| 461 | len = 2; |
| 462 | if (console_buffer[0] != '\0') { |
| 463 | local_args[2] = console_buffer; |
| 464 | len = 3; |
| 465 | } |
| 466 | |
| 467 | /* Continue calling setenv code */ |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 468 | return _do_env_set(flag, len, local_args, H_INTERACTIVE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 469 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 470 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 471 | |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 472 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 473 | static int print_static_binding(const char *var_name, const char *callback_name, |
| 474 | void *priv) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 475 | { |
| 476 | printf("\t%-20s %-20s\n", var_name, callback_name); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 481 | static int print_active_callback(struct env_entry *entry) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 482 | { |
| 483 | struct env_clbk_tbl *clbkp; |
| 484 | int i; |
| 485 | int num_callbacks; |
| 486 | |
| 487 | if (entry->callback == NULL) |
| 488 | return 0; |
| 489 | |
| 490 | /* look up the callback in the linker-list */ |
| 491 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 492 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 493 | i < num_callbacks; |
| 494 | i++, clbkp++) { |
| 495 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 496 | if (entry->callback == clbkp->callback + gd->reloc_off) |
| 497 | #else |
| 498 | if (entry->callback == clbkp->callback) |
| 499 | #endif |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | if (i == num_callbacks) |
| 504 | /* this should probably never happen, but just in case... */ |
| 505 | printf("\t%-20s %p\n", entry->key, entry->callback); |
| 506 | else |
| 507 | printf("\t%-20s %-20s\n", entry->key, clbkp->name); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Print the callbacks available and what they are bound to |
| 514 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 515 | int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc, |
| 516 | char *const argv[]) |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 517 | { |
| 518 | struct env_clbk_tbl *clbkp; |
| 519 | int i; |
| 520 | int num_callbacks; |
| 521 | |
| 522 | /* Print the available callbacks */ |
| 523 | puts("Available callbacks:\n"); |
| 524 | puts("\tCallback Name\n"); |
| 525 | puts("\t-------------\n"); |
| 526 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 527 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 528 | i < num_callbacks; |
| 529 | i++, clbkp++) |
| 530 | printf("\t%s\n", clbkp->name); |
| 531 | puts("\n"); |
| 532 | |
| 533 | /* Print the static bindings that may exist */ |
| 534 | puts("Static callback bindings:\n"); |
| 535 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 536 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 537 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL); |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 538 | puts("\n"); |
| 539 | |
| 540 | /* walk through each variable and print the callback if it has one */ |
| 541 | puts("Active callback bindings:\n"); |
| 542 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 543 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
| 544 | hwalk_r(&env_htab, print_active_callback); |
| 545 | return 0; |
| 546 | } |
| 547 | #endif |
| 548 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 549 | #if defined(CONFIG_CMD_ENV_FLAGS) |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 550 | static int print_static_flags(const char *var_name, const char *flags, |
| 551 | void *priv) |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 552 | { |
| 553 | enum env_flags_vartype type = env_flags_parse_vartype(flags); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 554 | enum env_flags_varaccess access = env_flags_parse_varaccess(flags); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 555 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 556 | printf("\t%-20s %-20s %-20s\n", var_name, |
| 557 | env_flags_get_vartype_name(type), |
| 558 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 563 | static int print_active_flags(struct env_entry *entry) |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 564 | { |
| 565 | enum env_flags_vartype type; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 566 | enum env_flags_varaccess access; |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 567 | |
| 568 | if (entry->flags == 0) |
| 569 | return 0; |
| 570 | |
| 571 | type = (enum env_flags_vartype) |
| 572 | (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 573 | access = env_flags_parse_varaccess_from_binflags(entry->flags); |
| 574 | printf("\t%-20s %-20s %-20s\n", entry->key, |
| 575 | env_flags_get_vartype_name(type), |
| 576 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Print the flags available and what variables have flags |
| 583 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 584 | 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] | 585 | { |
| 586 | /* Print the available variable types */ |
| 587 | printf("Available variable type flags (position %d):\n", |
| 588 | ENV_FLAGS_VARTYPE_LOC); |
| 589 | puts("\tFlag\tVariable Type Name\n"); |
| 590 | puts("\t----\t------------------\n"); |
| 591 | env_flags_print_vartypes(); |
| 592 | puts("\n"); |
| 593 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 594 | /* Print the available variable access types */ |
| 595 | printf("Available variable access flags (position %d):\n", |
| 596 | ENV_FLAGS_VARACCESS_LOC); |
| 597 | puts("\tFlag\tVariable Access Name\n"); |
| 598 | puts("\t----\t--------------------\n"); |
| 599 | env_flags_print_varaccess(); |
| 600 | puts("\n"); |
| 601 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 602 | /* Print the static flags that may exist */ |
| 603 | puts("Static flags:\n"); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 604 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 605 | "Variable Access"); |
| 606 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 607 | "---------------"); |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 608 | env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 609 | puts("\n"); |
| 610 | |
| 611 | /* walk through each variable and print the flags if non-default */ |
| 612 | puts("Active flags:\n"); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 613 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 614 | "Variable Access"); |
| 615 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 616 | "---------------"); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 617 | hwalk_r(&env_htab, print_active_flags); |
| 618 | return 0; |
| 619 | } |
| 620 | #endif |
| 621 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 622 | /* |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 623 | * Interactively edit an environment variable |
| 624 | */ |
| 625 | #if defined(CONFIG_CMD_EDITENV) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 626 | static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc, |
| 627 | char *const argv[]) |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 628 | { |
| 629 | char buffer[CONFIG_SYS_CBSIZE]; |
| 630 | char *init_val; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 631 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 632 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 633 | return CMD_RET_USAGE; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 634 | |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 635 | /* before import into hashtable */ |
| 636 | if (!(gd->flags & GD_FLG_ENV_READY)) |
| 637 | return 1; |
| 638 | |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 639 | /* Set read buffer to initial value or empty sting */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 640 | init_val = env_get(argv[1]); |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 641 | if (init_val) |
Peng Fan | 5d49b4c | 2015-12-23 12:08:09 +0800 | [diff] [blame] | 642 | snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val); |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 643 | else |
| 644 | buffer[0] = '\0'; |
| 645 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 646 | if (cli_readline_into_buffer("edit: ", buffer, 0) < 0) |
Joe Hershberger | 18a3cce | 2013-02-08 10:12:34 +0000 | [diff] [blame] | 647 | return 1; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 648 | |
Joe Hershberger | 94b467b | 2015-05-20 14:27:21 -0500 | [diff] [blame] | 649 | if (buffer[0] == '\0') { |
| 650 | const char * const _argv[3] = { "setenv", argv[1], NULL }; |
| 651 | |
| 652 | return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE); |
| 653 | } else { |
| 654 | const char * const _argv[4] = { "setenv", argv[1], buffer, |
| 655 | NULL }; |
| 656 | |
| 657 | return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE); |
| 658 | } |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 659 | } |
| 660 | #endif /* CONFIG_CMD_EDITENV */ |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 661 | #endif /* CONFIG_SPL_BUILD */ |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 662 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 663 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 664 | * Look up variable from environment, |
| 665 | * return address of storage for that variable, |
| 666 | * or NULL if not found |
| 667 | */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 668 | char *env_get(const char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 669 | { |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 670 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 671 | struct env_entry e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 672 | |
Wolfgang Denk | 91a7675 | 2010-07-24 20:22:02 +0200 | [diff] [blame] | 673 | WATCHDOG_RESET(); |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 674 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 675 | e.key = name; |
| 676 | e.data = NULL; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 677 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 678 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 679 | return ep ? ep->data : NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 682 | /* restricted capabilities before import */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 683 | if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 684 | return (char *)(gd->env_buf); |
| 685 | |
| 686 | return NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 689 | /* |
Patrice Chotard | 1ac2cb9 | 2019-11-25 09:07:36 +0100 | [diff] [blame] | 690 | * Like env_get, but prints an error if envvar isn't defined in the |
| 691 | * environment. It always returns what env_get does, so it can be used in |
| 692 | * place of env_get without changing error handling otherwise. |
| 693 | */ |
| 694 | char *from_env(const char *envvar) |
| 695 | { |
| 696 | char *ret; |
| 697 | |
| 698 | ret = env_get(envvar); |
| 699 | |
| 700 | if (!ret) |
| 701 | printf("missing environment variable: %s\n", envvar); |
| 702 | |
| 703 | return ret; |
| 704 | } |
| 705 | |
| 706 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 707 | * Look up variable from environment for restricted C runtime env. |
| 708 | */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 709 | int env_get_f(const char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 710 | { |
Simon Goldschmidt | 87c7fb3 | 2018-01-31 07:56:48 +0100 | [diff] [blame] | 711 | int i, nxt, c; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 712 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 713 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 714 | int val, n; |
| 715 | |
Simon Goldschmidt | 87c7fb3 | 2018-01-31 07:56:48 +0100 | [diff] [blame] | 716 | for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) { |
| 717 | if (c < 0) |
| 718 | return c; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 719 | if (nxt >= CONFIG_ENV_SIZE) |
| 720 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 721 | } |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 722 | |
Simon Glass | b9ca02c | 2019-08-01 09:46:45 -0600 | [diff] [blame] | 723 | val = env_match((uchar *)name, i); |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 724 | if (val < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 725 | continue; |
Wolfgang Denk | 9ed4a95 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 726 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 727 | /* found; copy out */ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 728 | for (n = 0; n < len; ++n, ++buf) { |
Simon Goldschmidt | 87c7fb3 | 2018-01-31 07:56:48 +0100 | [diff] [blame] | 729 | c = env_get_char(val++); |
| 730 | if (c < 0) |
| 731 | return c; |
| 732 | *buf = c; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 733 | if (*buf == '\0') |
Wolfgang Denk | 9ed4a95 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 734 | return n; |
| 735 | } |
| 736 | |
| 737 | if (n) |
| 738 | *--buf = '\0'; |
| 739 | |
Heinrich Schuchardt | 8291951 | 2019-01-06 11:52:06 +0100 | [diff] [blame] | 740 | printf("env_buf [%u bytes] too small for value of \"%s\"\n", |
| 741 | len, name); |
Wolfgang Denk | 9ed4a95 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 742 | |
| 743 | return n; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 744 | } |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 745 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 746 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Simon Glass | 4a9b413 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 749 | /** |
| 750 | * Decode the integer value of an environment variable and return it. |
| 751 | * |
Shyam Saini | 919d25c | 2018-06-07 19:47:19 +0530 | [diff] [blame] | 752 | * @param name Name of environment variable |
Simon Glass | 4a9b413 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 753 | * @param base Number base to use (normally 10, or 16 for hex) |
| 754 | * @param default_val Default value to return if the variable is not |
| 755 | * found |
| 756 | * @return the decoded value, or default_val if not found |
| 757 | */ |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 758 | ulong env_get_ulong(const char *name, int base, ulong default_val) |
Simon Glass | 4a9b413 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 759 | { |
| 760 | /* |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 761 | * We can use env_get() here, even before relocation, since the |
Simon Glass | 4a9b413 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 762 | * environment variable value is an integer and thus short. |
| 763 | */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 764 | const char *str = env_get(name); |
Simon Glass | 4a9b413 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 765 | |
| 766 | return str ? simple_strtoul(str, NULL, base) : default_val; |
| 767 | } |
| 768 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 769 | #ifndef CONFIG_SPL_BUILD |
Patrick Delaunay | 2643c85 | 2019-02-18 10:58:16 +0100 | [diff] [blame] | 770 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 771 | static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc, |
| 772 | char *const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 773 | { |
Simon Glass | 0151009 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 774 | return env_save() ? 1 : 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 775 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 776 | |
Mike Frysinger | ba69dc2 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 777 | U_BOOT_CMD( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 778 | saveenv, 1, 0, do_env_save, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 779 | "save environment variables to persistent storage", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 780 | "" |
Mike Frysinger | ba69dc2 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 781 | ); |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 782 | |
| 783 | #if defined(CONFIG_CMD_ERASEENV) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 784 | static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc, |
| 785 | char *const argv[]) |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 786 | { |
| 787 | return env_erase() ? 1 : 0; |
| 788 | } |
| 789 | |
| 790 | U_BOOT_CMD( |
| 791 | eraseenv, 1, 0, do_env_erase, |
| 792 | "erase environment variables from persistent storage", |
| 793 | "" |
| 794 | ); |
| 795 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 796 | #endif |
Patrick Delaunay | 0115dd3 | 2020-07-28 11:51:20 +0200 | [diff] [blame] | 797 | |
| 798 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
| 799 | static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 800 | char *const argv[]) |
| 801 | { |
| 802 | return env_reload() ? 1 : 0; |
| 803 | } |
| 804 | #endif |
Patrick Delaunay | a97d22e | 2020-07-28 11:51:21 +0200 | [diff] [blame] | 805 | |
| 806 | #if defined(CONFIG_CMD_NVEDIT_SELECT) |
| 807 | static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc, |
| 808 | char *const argv[]) |
| 809 | { |
| 810 | return env_select(argv[1]) ? 1 : 0; |
| 811 | } |
| 812 | #endif |
| 813 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 814 | #endif /* CONFIG_SPL_BUILD */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 815 | |
Simon Glass | b9ca02c | 2019-08-01 09:46:45 -0600 | [diff] [blame] | 816 | int env_match(uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 817 | { |
Joe Hershberger | 586197d | 2012-10-03 09:38:50 +0000 | [diff] [blame] | 818 | if (s1 == NULL) |
| 819 | return -1; |
| 820 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 821 | while (*s1 == env_get_char(i2++)) |
| 822 | if (*s1++ == '=') |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 823 | return i2; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 824 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 825 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 826 | return i2; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 827 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 828 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 829 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 830 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 831 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 832 | static int do_env_default(struct cmd_tbl *cmdtp, int flag, |
| 833 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 834 | { |
Yaniv Levinsky | 5a04264 | 2018-06-24 19:16:56 +0300 | [diff] [blame] | 835 | int all = 0, env_flag = H_INTERACTIVE; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 836 | |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 837 | debug("Initial value for argc=%d\n", argc); |
| 838 | while (--argc > 0 && **++argv == '-') { |
| 839 | char *arg = *argv; |
| 840 | |
| 841 | while (*++arg) { |
| 842 | switch (*arg) { |
| 843 | case 'a': /* default all */ |
| 844 | all = 1; |
| 845 | break; |
| 846 | case 'f': /* force */ |
Yaniv Levinsky | 3009149 | 2018-06-24 19:16:54 +0300 | [diff] [blame] | 847 | env_flag |= H_FORCE; |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 848 | break; |
| 849 | default: |
| 850 | return cmd_usage(cmdtp); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | debug("Final value for argc=%d\n", argc); |
| 855 | if (all && (argc == 0)) { |
| 856 | /* Reset the whole environment */ |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 857 | env_set_default("## Resetting to default environment\n", |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 858 | env_flag); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 859 | return 0; |
| 860 | } |
| 861 | if (!all && (argc > 0)) { |
| 862 | /* Reset individual variables */ |
Simon Glass | 0b9d8a0 | 2019-08-01 09:46:56 -0600 | [diff] [blame] | 863 | env_set_default_vars(argc, argv, env_flag); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | return cmd_usage(cmdtp); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 868 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 869 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 870 | static int do_env_delete(struct cmd_tbl *cmdtp, int flag, |
| 871 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 872 | { |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 873 | int env_flag = H_INTERACTIVE; |
| 874 | int ret = 0; |
| 875 | |
| 876 | debug("Initial value for argc=%d\n", argc); |
| 877 | while (argc > 1 && **(argv + 1) == '-') { |
| 878 | char *arg = *++argv; |
| 879 | |
| 880 | --argc; |
| 881 | while (*++arg) { |
| 882 | switch (*arg) { |
| 883 | case 'f': /* force */ |
| 884 | env_flag |= H_FORCE; |
| 885 | break; |
| 886 | default: |
| 887 | return CMD_RET_USAGE; |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | debug("Final value for argc=%d\n", argc); |
| 892 | |
| 893 | env_id++; |
| 894 | |
| 895 | while (--argc > 0) { |
| 896 | char *name = *++argv; |
| 897 | |
| 898 | if (!hdelete_r(name, &env_htab, env_flag)) |
| 899 | ret = 1; |
| 900 | } |
| 901 | |
| 902 | return ret; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 905 | #ifdef CONFIG_CMD_EXPORTENV |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 906 | /* |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 907 | * env export [-t | -b | -c] [-s size] addr [var ...] |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 908 | * -t: export as text format; if size is given, data will be |
| 909 | * padded with '\0' bytes; if not, one terminating '\0' |
| 910 | * will be added (which is included in the "filesize" |
| 911 | * setting so you can for exmple copy this to flash and |
| 912 | * keep the termination). |
| 913 | * -b: export as binary format (name=value pairs separated by |
| 914 | * '\0', list end marked by double "\0\0") |
| 915 | * -c: export as checksum protected environment format as |
| 916 | * used for example by "saveenv" command |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 917 | * -s size: |
| 918 | * size of output buffer |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 919 | * addr: memory address where environment gets stored |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 920 | * var... List of variable names that get included into the |
| 921 | * export. Without arguments, the whole environment gets |
| 922 | * exported. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 923 | * |
| 924 | * With "-c" and size is NOT given, then the export command will |
| 925 | * format the data as currently used for the persistent storage, |
| 926 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 927 | * prepend a valid CRC32 checksum and, in case of redundant |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 928 | * environment, a "current" redundancy flag. If size is given, this |
| 929 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 |
| 930 | * checksum and redundancy flag will be inserted. |
| 931 | * |
| 932 | * With "-b" and "-t", always only the real data (including a |
| 933 | * terminating '\0' byte) will be written; here the optional size |
| 934 | * argument will be used to make sure not to overflow the user |
| 935 | * provided buffer; the command will abort if the size is not |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 936 | * sufficient. Any remaining space will be '\0' padded. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 937 | * |
| 938 | * On successful return, the variable "filesize" will be set. |
| 939 | * Note that filesize includes the trailing/terminating '\0' byte(s). |
| 940 | * |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 941 | * Usage scenario: create a text snapshot/backup of the current settings: |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 942 | * |
| 943 | * => env export -t 100000 |
| 944 | * => era ${backup_addr} +${filesize} |
| 945 | * => cp.b 100000 ${backup_addr} ${filesize} |
| 946 | * |
| 947 | * Re-import this snapshot, deleting all other settings: |
| 948 | * |
| 949 | * => env import -d -t ${backup_addr} |
| 950 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 951 | static int do_env_export(struct cmd_tbl *cmdtp, int flag, |
| 952 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 953 | { |
| 954 | char buf[32]; |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 955 | ulong addr; |
| 956 | char *ptr, *cmd, *res; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 957 | size_t size = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 958 | ssize_t len; |
| 959 | env_t *envp; |
| 960 | char sep = '\n'; |
| 961 | int chk = 0; |
| 962 | int fmt = 0; |
| 963 | |
| 964 | cmd = *argv; |
| 965 | |
| 966 | while (--argc > 0 && **++argv == '-') { |
| 967 | char *arg = *argv; |
| 968 | while (*++arg) { |
| 969 | switch (*arg) { |
| 970 | case 'b': /* raw binary format */ |
| 971 | if (fmt++) |
| 972 | goto sep_err; |
| 973 | sep = '\0'; |
| 974 | break; |
| 975 | case 'c': /* external checksum format */ |
| 976 | if (fmt++) |
| 977 | goto sep_err; |
| 978 | sep = '\0'; |
| 979 | chk = 1; |
| 980 | break; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 981 | case 's': /* size given */ |
| 982 | if (--argc <= 0) |
| 983 | return cmd_usage(cmdtp); |
| 984 | size = simple_strtoul(*++argv, NULL, 16); |
| 985 | goto NXTARG; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 986 | case 't': /* text format */ |
| 987 | if (fmt++) |
| 988 | goto sep_err; |
| 989 | sep = '\n'; |
| 990 | break; |
| 991 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 992 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 993 | } |
| 994 | } |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 995 | NXTARG: ; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 998 | if (argc < 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 999 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1000 | |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1001 | addr = simple_strtoul(argv[0], NULL, 16); |
| 1002 | ptr = map_sysmem(addr, size); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1003 | |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 1004 | if (size) |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1005 | memset(ptr, '\0', size); |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 1006 | |
| 1007 | argc--; |
| 1008 | argv++; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1009 | |
| 1010 | if (sep) { /* export as text file */ |
Wolfgang Denk | ea009d4 | 2013-03-23 23:50:28 +0000 | [diff] [blame] | 1011 | len = hexport_r(&env_htab, sep, |
| 1012 | H_MATCH_KEY | H_MATCH_IDENT, |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1013 | &ptr, size, argc, argv); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1014 | if (len < 0) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 1015 | pr_err("## Error: Cannot export environment: errno = %d\n", |
| 1016 | errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1017 | return 1; |
| 1018 | } |
Andreas Bießmann | 8c3aff5 | 2011-02-09 15:10:29 +0100 | [diff] [blame] | 1019 | sprintf(buf, "%zX", (size_t)len); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 1020 | env_set("filesize", buf); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1025 | envp = (env_t *)ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1026 | |
| 1027 | if (chk) /* export as checksum protected block */ |
| 1028 | res = (char *)envp->data; |
| 1029 | else /* export as raw binary data */ |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1030 | res = ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1031 | |
Wolfgang Denk | ea009d4 | 2013-03-23 23:50:28 +0000 | [diff] [blame] | 1032 | len = hexport_r(&env_htab, '\0', |
| 1033 | H_MATCH_KEY | H_MATCH_IDENT, |
| 1034 | &res, ENV_SIZE, argc, argv); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1035 | if (len < 0) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 1036 | pr_err("## Error: Cannot export environment: errno = %d\n", |
| 1037 | errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | if (chk) { |
Neil Stainton | d71b029 | 2018-09-12 11:02:51 +0000 | [diff] [blame] | 1042 | envp->crc = crc32(0, envp->data, |
| 1043 | size ? size - offsetof(env_t, data) : ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1044 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1045 | envp->flags = ENV_REDUND_ACTIVE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1046 | #endif |
| 1047 | } |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 1048 | env_set_hex("filesize", len + offsetof(env_t, data)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1049 | |
| 1050 | return 0; |
| 1051 | |
| 1052 | sep_err: |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 1053 | printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 1054 | cmd); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1055 | return 1; |
| 1056 | } |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1057 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1058 | |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1059 | #ifdef CONFIG_CMD_IMPORTENV |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1060 | /* |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1061 | * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] |
| 1062 | * -d: delete existing environment before importing if no var is |
| 1063 | * passed; if vars are passed, if one var is in the current |
| 1064 | * environment but not in the environment at addr, delete var from |
| 1065 | * current environment; |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 1066 | * otherwise overwrite / append to existing definitions |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1067 | * -t: assume text format; either "size" must be given or the |
| 1068 | * text data must be '\0' terminated |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 1069 | * -r: handle CRLF like LF, that means exported variables with |
| 1070 | * a content which ends with \r won't get imported. Used |
| 1071 | * to import text files created with editors which are using CRLF |
| 1072 | * for line endings. Only effective in addition to -t. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1073 | * -b: assume binary format ('\0' separated, "\0\0" terminated) |
| 1074 | * -c: assume checksum protected environment format |
| 1075 | * addr: memory address to read from |
| 1076 | * size: length of input data; if missing, proper '\0' |
| 1077 | * termination is mandatory |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1078 | * if var is set and size should be missing (i.e. '\0' |
| 1079 | * termination), set size to '-' |
| 1080 | * var... List of the names of the only variables that get imported from |
| 1081 | * the environment at address 'addr'. Without arguments, the whole |
| 1082 | * environment gets imported. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1083 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1084 | static int do_env_import(struct cmd_tbl *cmdtp, int flag, |
| 1085 | int argc, char *const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1086 | { |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1087 | ulong addr; |
| 1088 | char *cmd, *ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1089 | char sep = '\n'; |
| 1090 | int chk = 0; |
| 1091 | int fmt = 0; |
| 1092 | int del = 0; |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 1093 | int crlf_is_lf = 0; |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1094 | int wl = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1095 | size_t size; |
| 1096 | |
| 1097 | cmd = *argv; |
| 1098 | |
| 1099 | while (--argc > 0 && **++argv == '-') { |
| 1100 | char *arg = *argv; |
| 1101 | while (*++arg) { |
| 1102 | switch (*arg) { |
| 1103 | case 'b': /* raw binary format */ |
| 1104 | if (fmt++) |
| 1105 | goto sep_err; |
| 1106 | sep = '\0'; |
| 1107 | break; |
| 1108 | case 'c': /* external checksum format */ |
| 1109 | if (fmt++) |
| 1110 | goto sep_err; |
| 1111 | sep = '\0'; |
| 1112 | chk = 1; |
| 1113 | break; |
| 1114 | case 't': /* text format */ |
| 1115 | if (fmt++) |
| 1116 | goto sep_err; |
| 1117 | sep = '\n'; |
| 1118 | break; |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 1119 | case 'r': /* handle CRLF like LF */ |
| 1120 | crlf_is_lf = 1; |
| 1121 | break; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1122 | case 'd': |
| 1123 | del = 1; |
| 1124 | break; |
| 1125 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1126 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 1131 | if (argc < 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1132 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1133 | |
| 1134 | if (!fmt) |
| 1135 | printf("## Warning: defaulting to text format\n"); |
| 1136 | |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 1137 | if (sep != '\n' && crlf_is_lf ) |
| 1138 | crlf_is_lf = 0; |
| 1139 | |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1140 | addr = simple_strtoul(argv[0], NULL, 16); |
| 1141 | ptr = map_sysmem(addr, 0); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1142 | |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1143 | if (argc >= 2 && strcmp(argv[1], "-")) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1144 | size = simple_strtoul(argv[1], NULL, 16); |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1145 | } else if (chk) { |
Tom Rini | 3775dcd | 2014-03-04 15:52:35 -0500 | [diff] [blame] | 1146 | puts("## Error: external checksum format must pass size\n"); |
| 1147 | return CMD_RET_FAILURE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1148 | } else { |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1149 | char *s = ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1150 | |
| 1151 | size = 0; |
| 1152 | |
| 1153 | while (size < MAX_ENV_SIZE) { |
| 1154 | if ((*s == sep) && (*(s+1) == '\0')) |
| 1155 | break; |
| 1156 | ++s; |
| 1157 | ++size; |
| 1158 | } |
| 1159 | if (size == MAX_ENV_SIZE) { |
| 1160 | printf("## Warning: Input data exceeds %d bytes" |
| 1161 | " - truncated\n", MAX_ENV_SIZE); |
| 1162 | } |
Horst Kronstorfer | d3f80c7 | 2011-12-16 23:33:10 +0000 | [diff] [blame] | 1163 | size += 2; |
Simon Glass | 79afc88 | 2011-11-04 06:42:36 +0000 | [diff] [blame] | 1164 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1167 | if (argc > 2) |
| 1168 | wl = 1; |
| 1169 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1170 | if (chk) { |
| 1171 | uint32_t crc; |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1172 | env_t *ep = (env_t *)ptr; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1173 | |
| 1174 | size -= offsetof(env_t, data); |
| 1175 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 1176 | |
| 1177 | if (crc32(0, ep->data, size) != crc) { |
| 1178 | puts("## Error: bad CRC, import failed\n"); |
| 1179 | return 1; |
| 1180 | } |
Simon Glass | fd37dac | 2013-10-25 23:01:31 -0600 | [diff] [blame] | 1181 | ptr = (char *)ep->data; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1184 | if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, |
| 1185 | crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 1186 | pr_err("## Error: Environment import failed: errno = %d\n", |
| 1187 | errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1188 | return 1; |
| 1189 | } |
| 1190 | gd->flags |= GD_FLG_ENV_READY; |
| 1191 | |
| 1192 | return 0; |
| 1193 | |
| 1194 | sep_err: |
| 1195 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 1196 | cmd); |
| 1197 | return 1; |
| 1198 | } |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1199 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1200 | |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1201 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
| 1202 | /* |
| 1203 | * print_env_info - print environment information |
| 1204 | */ |
| 1205 | static int print_env_info(void) |
| 1206 | { |
| 1207 | const char *value; |
| 1208 | |
| 1209 | /* print environment validity value */ |
| 1210 | switch (gd->env_valid) { |
| 1211 | case ENV_INVALID: |
| 1212 | value = "invalid"; |
| 1213 | break; |
| 1214 | case ENV_VALID: |
| 1215 | value = "valid"; |
| 1216 | break; |
| 1217 | case ENV_REDUND: |
| 1218 | value = "redundant"; |
| 1219 | break; |
| 1220 | default: |
| 1221 | value = "unknown"; |
| 1222 | break; |
| 1223 | } |
| 1224 | printf("env_valid = %s\n", value); |
| 1225 | |
| 1226 | /* print environment ready flag */ |
| 1227 | value = gd->flags & GD_FLG_ENV_READY ? "true" : "false"; |
| 1228 | printf("env_ready = %s\n", value); |
| 1229 | |
| 1230 | /* print environment using default flag */ |
| 1231 | value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false"; |
| 1232 | printf("env_use_default = %s\n", value); |
| 1233 | |
| 1234 | return CMD_RET_SUCCESS; |
| 1235 | } |
| 1236 | |
| 1237 | #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */ |
| 1238 | #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */ |
| 1239 | |
| 1240 | /* |
| 1241 | * env info - display environment information |
| 1242 | * env info [-d] - evaluate whether default environment is used |
| 1243 | * env info [-p] - evaluate whether environment can be persisted |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1244 | * Add [-q] - quiet mode, use only for command result, for test by example: |
| 1245 | * test env info -p -d -q |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1246 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1247 | static int do_env_info(struct cmd_tbl *cmdtp, int flag, |
| 1248 | int argc, char *const argv[]) |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1249 | { |
| 1250 | int eval_flags = 0; |
| 1251 | int eval_results = 0; |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1252 | bool quiet = false; |
Patrick Delaunay | 2f96b32 | 2020-06-19 14:03:35 +0200 | [diff] [blame] | 1253 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
| 1254 | enum env_location loc; |
| 1255 | #endif |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1256 | |
| 1257 | /* display environment information */ |
| 1258 | if (argc <= 1) |
| 1259 | return print_env_info(); |
| 1260 | |
| 1261 | /* process options */ |
| 1262 | while (--argc > 0 && **++argv == '-') { |
| 1263 | char *arg = *argv; |
| 1264 | |
| 1265 | while (*++arg) { |
| 1266 | switch (*arg) { |
| 1267 | case 'd': |
| 1268 | eval_flags |= ENV_INFO_IS_DEFAULT; |
| 1269 | break; |
| 1270 | case 'p': |
| 1271 | eval_flags |= ENV_INFO_IS_PERSISTED; |
| 1272 | break; |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1273 | case 'q': |
| 1274 | quiet = true; |
| 1275 | break; |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1276 | default: |
| 1277 | return CMD_RET_USAGE; |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* evaluate whether default environment is used */ |
| 1283 | if (eval_flags & ENV_INFO_IS_DEFAULT) { |
| 1284 | if (gd->flags & GD_FLG_ENV_DEFAULT) { |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1285 | if (!quiet) |
| 1286 | printf("Default environment is used\n"); |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1287 | eval_results |= ENV_INFO_IS_DEFAULT; |
| 1288 | } else { |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1289 | if (!quiet) |
| 1290 | printf("Environment was loaded from persistent storage\n"); |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /* evaluate whether environment can be persisted */ |
| 1295 | if (eval_flags & ENV_INFO_IS_PERSISTED) { |
Patrick Delaunay | 8b5206d | 2020-07-02 17:43:44 +0200 | [diff] [blame] | 1296 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
Patrick Delaunay | 2f96b32 | 2020-06-19 14:03:35 +0200 | [diff] [blame] | 1297 | loc = env_get_location(ENVOP_SAVE, gd->env_load_prio); |
| 1298 | if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) { |
| 1299 | if (!quiet) |
| 1300 | printf("Environment can be persisted\n"); |
| 1301 | eval_results |= ENV_INFO_IS_PERSISTED; |
| 1302 | } else { |
| 1303 | if (!quiet) |
| 1304 | printf("Environment cannot be persisted\n"); |
| 1305 | } |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1306 | #else |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1307 | if (!quiet) |
| 1308 | printf("Environment cannot be persisted\n"); |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1309 | #endif |
| 1310 | } |
| 1311 | |
| 1312 | /* The result of evaluations is combined with AND */ |
| 1313 | if (eval_flags != eval_results) |
| 1314 | return CMD_RET_FAILURE; |
| 1315 | |
| 1316 | return CMD_RET_SUCCESS; |
| 1317 | } |
| 1318 | #endif |
| 1319 | |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1320 | #if defined(CONFIG_CMD_ENV_EXISTS) |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1321 | static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1322 | char *const argv[]) |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1323 | { |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 1324 | struct env_entry e, *ep; |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1325 | |
| 1326 | if (argc < 2) |
| 1327 | return CMD_RET_USAGE; |
| 1328 | |
| 1329 | e.key = argv[1]; |
| 1330 | e.data = NULL; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 1331 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1332 | |
| 1333 | return (ep == NULL) ? 1 : 0; |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1337 | /* |
| 1338 | * New command line interface: "env" command with subcommands |
| 1339 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1340 | static struct cmd_tbl cmd_env_sub[] = { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1341 | #if defined(CONFIG_CMD_ASKENV) |
| 1342 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), |
| 1343 | #endif |
| 1344 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1345 | U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1346 | #if defined(CONFIG_CMD_EDITENV) |
| 1347 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), |
| 1348 | #endif |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1349 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1350 | U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), |
| 1351 | #endif |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1352 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1353 | U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), |
| 1354 | #endif |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1355 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1356 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1357 | #endif |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1358 | #if defined(CONFIG_CMD_GREPENV) |
| 1359 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), |
| 1360 | #endif |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1361 | #if defined(CONFIG_CMD_IMPORTENV) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1362 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1363 | #endif |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1364 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1365 | U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""), |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1366 | #endif |
Patrick Delaunay | 0115dd3 | 2020-07-28 11:51:20 +0200 | [diff] [blame] | 1367 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
| 1368 | U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""), |
| 1369 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1370 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
| 1371 | #if defined(CONFIG_CMD_RUN) |
| 1372 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), |
| 1373 | #endif |
Patrick Delaunay | 2643c85 | 2019-02-18 10:58:16 +0100 | [diff] [blame] | 1374 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1375 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 1376 | #if defined(CONFIG_CMD_ERASEENV) |
| 1377 | U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""), |
| 1378 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1379 | #endif |
Patrick Delaunay | a97d22e | 2020-07-28 11:51:21 +0200 | [diff] [blame] | 1380 | #if defined(CONFIG_CMD_NVEDIT_SELECT) |
| 1381 | U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""), |
| 1382 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1383 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1384 | #if defined(CONFIG_CMD_ENV_EXISTS) |
| 1385 | U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""), |
| 1386 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1387 | }; |
| 1388 | |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 1389 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 60f7da1 | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 1390 | void env_reloc(void) |
| 1391 | { |
| 1392 | fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1393 | } |
| 1394 | #endif |
| 1395 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1396 | 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] | 1397 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1398 | struct cmd_tbl *cp; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1399 | |
Thomas Weber | 5904da0 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1400 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1401 | return CMD_RET_USAGE; |
Thomas Weber | 5904da0 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1402 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1403 | /* drop initial "env" arg */ |
| 1404 | argc--; |
| 1405 | argv++; |
| 1406 | |
| 1407 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1408 | |
| 1409 | if (cp) |
| 1410 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1411 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1412 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1415 | #ifdef CONFIG_SYS_LONGHELP |
| 1416 | static char env_help_text[] = |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1417 | #if defined(CONFIG_CMD_ASKENV) |
| 1418 | "ask name [message] [size] - ask for environment variable\nenv " |
| 1419 | #endif |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1420 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1421 | "callbacks - print callbacks and their associated variables\nenv " |
| 1422 | #endif |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 1423 | "default [-f] -a - [forcibly] reset default environment\n" |
| 1424 | "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1425 | "env delete [-f] var [...] - [forcibly] delete variable(s)\n" |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1426 | #if defined(CONFIG_CMD_EDITENV) |
| 1427 | "env edit name - edit environment variable\n" |
| 1428 | #endif |
Andrew Ruder | 88733e2 | 2013-10-22 19:07:34 -0500 | [diff] [blame] | 1429 | #if defined(CONFIG_CMD_ENV_EXISTS) |
| 1430 | "env exists name - tests for existence of variable\n" |
| 1431 | #endif |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1432 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 1433 | "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] | 1434 | #endif |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1435 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1436 | "env flags - print variables that have non-default flags\n" |
| 1437 | #endif |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1438 | #if defined(CONFIG_CMD_GREPENV) |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1439 | #ifdef CONFIG_REGEX |
| 1440 | "env grep [-e] [-n | -v | -b] string [...] - search environment\n" |
| 1441 | #else |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1442 | "env grep [-n | -v | -b] string [...] - search environment\n" |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1443 | #endif |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1444 | #endif |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1445 | #if defined(CONFIG_CMD_IMPORTENV) |
Quentin Schulz | eaf7347 | 2018-07-09 19:16:29 +0200 | [diff] [blame] | 1446 | "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] | 1447 | #endif |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1448 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
| 1449 | "env info - display environment information\n" |
Patrick Delaunay | 6718ebd | 2020-06-19 14:03:34 +0200 | [diff] [blame] | 1450 | "env info [-d] [-p] [-q] - evaluate environment information\n" |
| 1451 | " \"-d\": default environment is used\n" |
| 1452 | " \"-p\": environment can be persisted\n" |
| 1453 | " \"-q\": quiet output\n" |
Leo Ruan | 8e92120 | 2019-05-24 17:20:19 +0200 | [diff] [blame] | 1454 | #endif |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1455 | "env print [-a | name ...] - print environment\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1456 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1457 | "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1458 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1459 | #if defined(CONFIG_CMD_RUN) |
| 1460 | "env run var [...] - run commands in an environment variable\n" |
| 1461 | #endif |
Patrick Delaunay | 2643c85 | 2019-02-18 10:58:16 +0100 | [diff] [blame] | 1462 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1463 | "env save - save environment\n" |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 1464 | #if defined(CONFIG_CMD_ERASEENV) |
| 1465 | "env erase - erase environment\n" |
| 1466 | #endif |
Horst Kronstorfer | d798a9b | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 1467 | #endif |
Patrick Delaunay | 0115dd3 | 2020-07-28 11:51:20 +0200 | [diff] [blame] | 1468 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
| 1469 | "env load - load environment\n" |
| 1470 | #endif |
Patrick Delaunay | a97d22e | 2020-07-28 11:51:21 +0200 | [diff] [blame] | 1471 | #if defined(CONFIG_CMD_NVEDIT_SELECT) |
| 1472 | "env select [target] - select environment target\n" |
| 1473 | #endif |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1474 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 1475 | "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] | 1476 | " - set UEFI variable; unset if '-i' or 'arg' not specified\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1477 | #endif |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1478 | "env set [-f] name [arg ...]\n"; |
| 1479 | #endif |
| 1480 | |
| 1481 | U_BOOT_CMD( |
| 1482 | env, CONFIG_SYS_MAXARGS, 1, do_env, |
| 1483 | "environment handling commands", env_help_text |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1484 | ); |
| 1485 | |
| 1486 | /* |
| 1487 | * Old command line interface, kept for compatibility |
| 1488 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1489 | |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1490 | #if defined(CONFIG_CMD_EDITENV) |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1491 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1492 | editenv, 2, 0, do_env_edit, |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1493 | "edit environment variable", |
| 1494 | "name\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1495 | " - edit environment variable 'name'", |
| 1496 | var_complete |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1497 | ); |
| 1498 | #endif |
| 1499 | |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1500 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1501 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1502 | "print environment variables", |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1503 | "[-a]\n - print [all] values of all environment variables\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1504 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1505 | "printenv -e [-guid guid][-n] [name ...]\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1506 | " - print UEFI variable 'name' or all the variables\n" |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1507 | " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1508 | " \"-n\": suppress dumping variable's value\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1509 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1510 | "printenv name ...\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1511 | " - print value of environment variable 'name'", |
| 1512 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1513 | ); |
| 1514 | |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1515 | #ifdef CONFIG_CMD_GREPENV |
| 1516 | U_BOOT_CMD_COMPLETE( |
| 1517 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, |
| 1518 | "search environment variables", |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1519 | #ifdef CONFIG_REGEX |
| 1520 | "[-e] [-n | -v | -b] string ...\n" |
| 1521 | #else |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1522 | "[-n | -v | -b] string ...\n" |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1523 | #endif |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1524 | " - list environment name=value pairs matching 'string'\n" |
Wolfgang Denk | be29df6 | 2013-03-23 23:50:32 +0000 | [diff] [blame] | 1525 | #ifdef CONFIG_REGEX |
| 1526 | " \"-e\": enable regular expressions;\n" |
| 1527 | #endif |
Wolfgang Denk | d87244d | 2013-03-23 23:50:30 +0000 | [diff] [blame] | 1528 | " \"-n\": search variable names; \"-v\": search values;\n" |
| 1529 | " \"-b\": search both names and values (default)", |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1530 | var_complete |
| 1531 | ); |
| 1532 | #endif |
| 1533 | |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1534 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1535 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1536 | "set environment variables", |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1537 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 1538 | "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1539 | " [-i addr,size name], or [name [value ...]]\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1540 | " - set UEFI variable 'name' to 'value' ...'\n" |
Heinrich Schuchardt | c70f448 | 2020-07-15 18:00:56 +0200 | [diff] [blame] | 1541 | " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1542 | " \"-nv\": set non-volatile attribute\n" |
| 1543 | " \"-bs\": set boot-service attribute\n" |
| 1544 | " \"-rt\": set runtime attribute\n" |
AKASHI Takahiro | e50e287 | 2020-04-14 11:51:47 +0900 | [diff] [blame] | 1545 | " \"-at\": set time-based authentication attribute\n" |
AKASHI Takahiro | 051aa89 | 2019-10-24 15:17:13 +0900 | [diff] [blame] | 1546 | " \"-a\": append-write\n" |
| 1547 | " \"-i addr,size\": use <addr,size> as variable's value\n" |
| 1548 | " \"-v\": verbose message\n" |
AKASHI Takahiro | 49d81fd | 2019-02-25 15:54:36 +0900 | [diff] [blame] | 1549 | " - delete UEFI variable 'name' if 'value' not specified\n" |
| 1550 | #endif |
| 1551 | "setenv [-f] name value ...\n" |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 1552 | " - [forcibly] set environment variable 'name' to 'value ...'\n" |
| 1553 | "setenv [-f] name\n" |
| 1554 | " - [forcibly] delete environment variable 'name'", |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1555 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1556 | ); |
| 1557 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1558 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1559 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1560 | U_BOOT_CMD( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1561 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1562 | "get environment variables from stdin", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1563 | "name [message] [size]\n" |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 1564 | " - get environment variable 'name' from stdin (max 'size' chars)" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1565 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1566 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1567 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1568 | #if defined(CONFIG_CMD_RUN) |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1569 | U_BOOT_CMD_COMPLETE( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1570 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1571 | "run commands in an environment variable", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1572 | "var [...]\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1573 | " - run the commands in the environment variable(s) 'var'", |
| 1574 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1575 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1576 | #endif |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 1577 | #endif /* CONFIG_SPL_BUILD */ |