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