blob: 81d94cd193c6dd6c03d4c843cc6cae2a1284575a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenka68d3ed2002-10-11 08:38:32 +00002/*
Wolfgang Denk565af272013-03-23 23:50:28 +00003 * (C) Copyright 2000-2013
wdenka68d3ed2002-10-11 08:38:32 +00004 * 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 Phillipsf0c9d532011-04-05 07:15:14 +00008 *
9 * Copyright 2011 Freescale Semiconductor, Inc.
wdenka68d3ed2002-10-11 08:38:32 +000010 */
11
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020012/*
wdenka68d3ed2002-10-11 08:38:32 +000013 * Support for persistent environment data
14 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020015 * 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. Dayc5b1e5d2016-09-07 14:27:59 -040017 * a double '\0'. The environment is preceded by a 32 bit CRC over
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020018 * the data part and, in case of redundant environment, a byte of
19 * flags.
wdenka68d3ed2002-10-11 08:38:32 +000020 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020021 * 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.
wdenka68d3ed2002-10-11 08:38:32 +000024 */
25
26#include <common.h>
Simon Glassdec3c012014-04-10 20:01:25 -060027#include <cli.h>
wdenka68d3ed2002-10-11 08:38:32 +000028#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070029#include <console.h>
Simon Glass2d85a752019-08-01 09:46:41 -060030#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060031#include <env_internal.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050034#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050035#include <mapmem.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070036#include <u-boot/crc.h>
wdenk2a3cb022002-11-05 21:01:48 +000037#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000038#include <linux/stddef.h>
39#include <asm/byteorder.h>
Simon Glassa7459262013-10-25 23:01:31 -060040#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000041
Wolfgang Denk6405a152006-03-31 18:32:53 +020042DECLARE_GLOBAL_DATA_PTR;
43
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010044#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
45 defined(CONFIG_ENV_IS_IN_FLASH) || \
46 defined(CONFIG_ENV_IS_IN_MMC) || \
47 defined(CONFIG_ENV_IS_IN_FAT) || \
48 defined(CONFIG_ENV_IS_IN_EXT4) || \
49 defined(CONFIG_ENV_IS_IN_NAND) || \
50 defined(CONFIG_ENV_IS_IN_NVRAM) || \
51 defined(CONFIG_ENV_IS_IN_ONENAND) || \
52 defined(CONFIG_ENV_IS_IN_SATA) || \
53 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
54 defined(CONFIG_ENV_IS_IN_REMOTE) || \
55 defined(CONFIG_ENV_IS_IN_UBI)
56
57#define ENV_IS_IN_DEVICE
58
59#endif
60
61#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000062 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030063# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020064NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000065#endif
66
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020067/*
68 * Maximum expected input data size for import command
69 */
70#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000071
wdenka68d3ed2002-10-11 08:38:32 +000072/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060074 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020075 * has changed or not. So it is possible to reread an environment
76 * variable only if the environment was changed ... done so for
77 * example in NetInitLoop()
78 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010079static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000080
Simon Glass2d85a752019-08-01 09:46:41 -060081int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010082{
83 return env_id;
84}
wdenka68d3ed2002-10-11 08:38:32 +000085
Ilya Yanokf7a2c552012-09-18 00:22:50 +000086#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040087/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088 * Command interface: print one or all environment variables
89 *
90 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040091 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060092static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000093{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020094 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000095 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000096
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020097 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -060098 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000099
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200100 e.key = name;
101 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600102 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200103 if (ep == NULL)
104 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000105 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200106 return len;
107 }
wdenka68d3ed2002-10-11 08:38:32 +0000108
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200109 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600110 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000111
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200112 if (len > 0) {
113 puts(res);
114 free(res);
115 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000116 }
117
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200118 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000119 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200120 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400121}
wdenka68d3ed2002-10-11 08:38:32 +0000122
Kim Phillipsdc00a682012-10-29 13:34:31 +0000123static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
124 char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400125{
126 int i;
127 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600128 int env_flag = H_HIDE_DOT;
129
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900130#if defined(CONFIG_CMD_NVEDIT_EFI)
131 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
132 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
133#endif
134
Joe Hershberger79a905e2012-12-11 22:16:23 -0600135 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
136 argc--;
137 argv++;
138 env_flag &= ~H_HIDE_DOT;
139 }
wdenka68d3ed2002-10-11 08:38:32 +0000140
Mike Frysinger87944922009-05-24 02:26:19 -0400141 if (argc == 1) {
142 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600143 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200144 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400145 return 1;
146 printf("\nEnvironment size: %d/%ld bytes\n",
147 rcode, (ulong)ENV_SIZE);
148 return 0;
149 }
wdenka68d3ed2002-10-11 08:38:32 +0000150
Mike Frysinger87944922009-05-24 02:26:19 -0400151 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600152 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400153 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600154 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200155 if (!rc) {
156 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400157 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000158 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000159 }
160
161 return rcode;
162}
163
164#ifdef CONFIG_CMD_GREPENV
Igor Grinbergde832562011-11-07 01:13:59 +0000165static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
166 int argc, char * const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000167{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000168 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000169 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000170
171 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000172 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000173
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000174 grep_how = H_MATCH_SUBSTR; /* default: substring search */
175 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000176
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200177 while (--argc > 0 && **++argv == '-') {
178 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000179 while (*++arg) {
180 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000181#ifdef CONFIG_REGEX
182 case 'e': /* use regex matching */
183 grep_how = H_MATCH_REGEX;
184 break;
185#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000186 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000187 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000188 break;
189 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000190 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000191 break;
192 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000193 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000194 break;
195 case '-':
196 goto DONE;
197 default:
198 return CMD_RET_USAGE;
199 }
200 }
201 }
202
203DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000204 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000205 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000206 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000207
Wolfgang Denke27fc122013-03-23 23:50:29 +0000208 if (len > 0) {
209 puts(res);
210 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000211 }
Mike Frysinger87944922009-05-24 02:26:19 -0400212
Wolfgang Denke27fc122013-03-23 23:50:29 +0000213 if (len < 2)
214 return 1;
215
216 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000217}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000218#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000219#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000220
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200221/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000222 * Set a new environment variable,
223 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600224 */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500225static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000226{
227 int i, len;
228 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600229 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000230
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600231 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900232
233#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
234 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
235 return do_env_set_efi(NULL, flag, --argc, ++argv);
236#endif
237
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600238 while (argc > 1 && **(argv + 1) == '-') {
239 char *arg = *++argv;
240
241 --argc;
242 while (*++arg) {
243 switch (*arg) {
244 case 'f': /* force */
245 env_flag |= H_FORCE;
246 break;
247 default:
248 return CMD_RET_USAGE;
249 }
250 }
251 }
252 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000253 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000254
255 if (strchr(name, '=')) {
256 printf("## Error: illegal character '='"
257 "in variable name \"%s\"\n", name);
258 return 1;
259 }
260
261 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000262
wdenka68d3ed2002-10-11 08:38:32 +0000263 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000264 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600265 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200266 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000267 }
268
269 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200270 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000271 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000272 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000273 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000274
275 value = malloc(len);
276 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200277 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000278 return 1;
279 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000280 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200281 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000282
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200283 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000284 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000285 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000286 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200287 if (s != value)
288 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000289
Igor Grinbergde832562011-11-07 01:13:59 +0000290 e.key = name;
291 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600292 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200293 free(value);
294 if (!ep) {
295 printf("## Error inserting \"%s\" variable, errno=%d\n",
296 name, errno);
297 return 1;
298 }
wdenka68d3ed2002-10-11 08:38:32 +0000299
wdenka68d3ed2002-10-11 08:38:32 +0000300 return 0;
301}
302
Simon Glass6a38e412017-08-03 12:22:09 -0600303int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000304{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200305 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
306
Joe Hershberger3902c602013-04-08 10:32:50 +0000307 /* before import into hashtable */
308 if (!(gd->flags & GD_FLG_ENV_READY))
309 return 1;
310
Igor Grinbergde832562011-11-07 01:13:59 +0000311 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500312 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200313 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500314 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000315}
316
Simon Glassb8e926e2011-10-24 17:59:59 +0000317/**
318 * Set an environment variable to an integer value
319 *
Simon Glassdf372d62013-05-07 06:11:45 +0000320 * @param varname Environment variable to set
Simon Glassb8e926e2011-10-24 17:59:59 +0000321 * @param value Value to set it to
322 * @return 0 if ok, 1 on error
323 */
Simon Glass4d949a22017-08-03 12:22:10 -0600324int env_set_ulong(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000325{
326 /* TODO: this should be unsigned */
327 char *str = simple_itoa(value);
328
Simon Glass6a38e412017-08-03 12:22:09 -0600329 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000330}
331
332/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000333 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000334 *
Simon Glassdf372d62013-05-07 06:11:45 +0000335 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000336 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000337 * @return 0 if ok, 1 on error
338 */
Simon Glass4d949a22017-08-03 12:22:10 -0600339int env_set_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000340{
341 char str[17];
342
Simon Glass044dc9e2013-02-24 17:33:21 +0000343 sprintf(str, "%lx", value);
Simon Glass6a38e412017-08-03 12:22:09 -0600344 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000345}
346
Simon Glass22c34c22017-08-03 12:22:13 -0600347ulong env_get_hex(const char *varname, ulong default_val)
Simon Glassabbd5122013-04-20 08:42:43 +0000348{
349 const char *s;
350 ulong value;
351 char *endp;
352
Simon Glass64b723f2017-08-03 12:22:12 -0600353 s = env_get(varname);
Simon Glassabbd5122013-04-20 08:42:43 +0000354 if (s)
355 value = simple_strtoul(s, &endp, 16);
356 if (!s || endp == s)
357 return default_val;
358
359 return value;
360}
361
Alex Kiernan9c215492018-04-01 09:22:38 +0000362int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
363{
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500364 string_to_enetaddr(env_get(name), enetaddr);
Alex Kiernan9c215492018-04-01 09:22:38 +0000365 return is_valid_ethaddr(enetaddr);
366}
367
368int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
369{
370 char buf[ARP_HLEN_ASCII + 1];
371
372 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
373 return -EEXIST;
374
375 sprintf(buf, "%pM", enetaddr);
376
377 return env_set(name, buf);
378}
379
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000380#ifndef CONFIG_SPL_BUILD
Kim Phillipsdc00a682012-10-29 13:34:31 +0000381static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000382{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200383 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000384 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000385
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500386 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000387}
388
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200389/*
wdenka68d3ed2002-10-11 08:38:32 +0000390 * Prompt for environment variable
391 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500392#if defined(CONFIG_CMD_ASKENV)
Macpaul Linff2a9f32011-04-26 16:16:45 +0000393int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000394{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200395 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000396 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000397 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000398 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000399
400 local_args[0] = argv[0];
401 local_args[1] = argv[1];
402 local_args[2] = NULL;
403 local_args[3] = NULL;
404
Wolfgang Denk586895d2013-02-20 04:53:16 +0000405 /*
406 * Check the syntax:
407 *
408 * env_ask envname [message1 ...] [size]
409 */
410 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000411 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000412
Wolfgang Denk586895d2013-02-20 04:53:16 +0000413 /*
414 * We test the last argument if it can be converted
415 * into a decimal number. If yes, we assume it's
416 * the size. Otherwise we echo it as part of the
417 * message.
418 */
419 i = simple_strtoul(argv[argc - 1], &endptr, 10);
420 if (*endptr != '\0') { /* no size */
421 size = CONFIG_SYS_CBSIZE - 1;
422 } else { /* size given */
423 size = i;
424 --argc;
425 }
wdenka68d3ed2002-10-11 08:38:32 +0000426
Wolfgang Denk586895d2013-02-20 04:53:16 +0000427 if (argc <= 2) {
428 sprintf(message, "Please enter '%s': ", argv[1]);
429 } else {
430 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400431 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000432 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000433 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000434
Tom Rini4beb2352017-09-26 19:37:11 -0400435 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000436 pos += strlen(argv[i]);
437 }
Tom Rini4beb2352017-09-26 19:37:11 -0400438 if (pos < sizeof(message) - 1) {
439 message[pos++] = ' ';
440 message[pos] = '\0';
441 } else
442 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000443 }
444
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200445 if (size >= CONFIG_SYS_CBSIZE)
446 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000447
448 if (size <= 0)
449 return 1;
450
451 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600452 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000453
454 if (size < len)
455 console_buffer[size] = '\0';
456
457 len = 2;
458 if (console_buffer[0] != '\0') {
459 local_args[2] = console_buffer;
460 len = 3;
461 }
462
463 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500464 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000465}
Jon Loeligerd704d912007-07-10 11:02:44 -0500466#endif
wdenka68d3ed2002-10-11 08:38:32 +0000467
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600468#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500469static int print_static_binding(const char *var_name, const char *callback_name,
470 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600471{
472 printf("\t%-20s %-20s\n", var_name, callback_name);
473
474 return 0;
475}
476
Simon Glass1a236862019-08-02 09:44:18 -0600477static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600478{
479 struct env_clbk_tbl *clbkp;
480 int i;
481 int num_callbacks;
482
483 if (entry->callback == NULL)
484 return 0;
485
486 /* look up the callback in the linker-list */
487 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
488 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
489 i < num_callbacks;
490 i++, clbkp++) {
491#if defined(CONFIG_NEEDS_MANUAL_RELOC)
492 if (entry->callback == clbkp->callback + gd->reloc_off)
493#else
494 if (entry->callback == clbkp->callback)
495#endif
496 break;
497 }
498
499 if (i == num_callbacks)
500 /* this should probably never happen, but just in case... */
501 printf("\t%-20s %p\n", entry->key, entry->callback);
502 else
503 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
504
505 return 0;
506}
507
508/*
509 * Print the callbacks available and what they are bound to
510 */
511int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
512{
513 struct env_clbk_tbl *clbkp;
514 int i;
515 int num_callbacks;
516
517 /* Print the available callbacks */
518 puts("Available callbacks:\n");
519 puts("\tCallback Name\n");
520 puts("\t-------------\n");
521 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
522 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
523 i < num_callbacks;
524 i++, clbkp++)
525 printf("\t%s\n", clbkp->name);
526 puts("\n");
527
528 /* Print the static bindings that may exist */
529 puts("Static callback bindings:\n");
530 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
531 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500532 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600533 puts("\n");
534
535 /* walk through each variable and print the callback if it has one */
536 puts("Active callback bindings:\n");
537 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
538 printf("\t%-20s %-20s\n", "-------------", "-------------");
539 hwalk_r(&env_htab, print_active_callback);
540 return 0;
541}
542#endif
543
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600544#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500545static int print_static_flags(const char *var_name, const char *flags,
546 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600547{
548 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600549 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600550
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600551 printf("\t%-20s %-20s %-20s\n", var_name,
552 env_flags_get_vartype_name(type),
553 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600554
555 return 0;
556}
557
Simon Glass1a236862019-08-02 09:44:18 -0600558static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600559{
560 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600561 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600562
563 if (entry->flags == 0)
564 return 0;
565
566 type = (enum env_flags_vartype)
567 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600568 access = env_flags_parse_varaccess_from_binflags(entry->flags);
569 printf("\t%-20s %-20s %-20s\n", entry->key,
570 env_flags_get_vartype_name(type),
571 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600572
573 return 0;
574}
575
576/*
577 * Print the flags available and what variables have flags
578 */
579int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
580{
581 /* Print the available variable types */
582 printf("Available variable type flags (position %d):\n",
583 ENV_FLAGS_VARTYPE_LOC);
584 puts("\tFlag\tVariable Type Name\n");
585 puts("\t----\t------------------\n");
586 env_flags_print_vartypes();
587 puts("\n");
588
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600589 /* Print the available variable access types */
590 printf("Available variable access flags (position %d):\n",
591 ENV_FLAGS_VARACCESS_LOC);
592 puts("\tFlag\tVariable Access Name\n");
593 puts("\t----\t--------------------\n");
594 env_flags_print_varaccess();
595 puts("\n");
596
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600597 /* Print the static flags that may exist */
598 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600599 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
600 "Variable Access");
601 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
602 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500603 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600604 puts("\n");
605
606 /* walk through each variable and print the flags if non-default */
607 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600608 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
609 "Variable Access");
610 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
611 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600612 hwalk_r(&env_htab, print_active_flags);
613 return 0;
614}
615#endif
616
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200617/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500618 * Interactively edit an environment variable
619 */
620#if defined(CONFIG_CMD_EDITENV)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000621static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
622 char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500623{
624 char buffer[CONFIG_SYS_CBSIZE];
625 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500626
Wolfgang Denk3b683112010-07-17 01:06:04 +0200627 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000628 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500629
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500630 /* before import into hashtable */
631 if (!(gd->flags & GD_FLG_ENV_READY))
632 return 1;
633
Peter Tyser0deafa22009-10-25 15:12:56 -0500634 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600635 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500636 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800637 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500638 else
639 buffer[0] = '\0';
640
Simon Glassbe6aafc2014-04-10 20:01:27 -0600641 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000642 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500643
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500644 if (buffer[0] == '\0') {
645 const char * const _argv[3] = { "setenv", argv[1], NULL };
646
647 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
648 } else {
649 const char * const _argv[4] = { "setenv", argv[1], buffer,
650 NULL };
651
652 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
653 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500654}
655#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000656#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500657
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200658/*
wdenka68d3ed2002-10-11 08:38:32 +0000659 * Look up variable from environment,
660 * return address of storage for that variable,
661 * or NULL if not found
662 */
Simon Glass64b723f2017-08-03 12:22:12 -0600663char *env_get(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000664{
Igor Grinbergde832562011-11-07 01:13:59 +0000665 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Simon Glass1a236862019-08-02 09:44:18 -0600666 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000667
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200668 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000669
Igor Grinbergde832562011-11-07 01:13:59 +0000670 e.key = name;
671 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600672 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200673
Macpaul Linff2a9f32011-04-26 16:16:45 +0000674 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000675 }
676
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200677 /* restricted capabilities before import */
Simon Glass64b723f2017-08-03 12:22:12 -0600678 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200679 return (char *)(gd->env_buf);
680
681 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000682}
683
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200684/*
Patrice Chotardceed4b42019-11-25 09:07:36 +0100685 * Like env_get, but prints an error if envvar isn't defined in the
686 * environment. It always returns what env_get does, so it can be used in
687 * place of env_get without changing error handling otherwise.
688 */
689char *from_env(const char *envvar)
690{
691 char *ret;
692
693 ret = env_get(envvar);
694
695 if (!ret)
696 printf("missing environment variable: %s\n", envvar);
697
698 return ret;
699}
700
701/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200702 * Look up variable from environment for restricted C runtime env.
703 */
Simon Glass64b723f2017-08-03 12:22:12 -0600704int env_get_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000705{
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100706 int i, nxt, c;
wdenka68d3ed2002-10-11 08:38:32 +0000707
Igor Grinbergde832562011-11-07 01:13:59 +0000708 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000709 int val, n;
710
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100711 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
712 if (c < 0)
713 return c;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000714 if (nxt >= CONFIG_ENV_SIZE)
715 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000716 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000717
Simon Glass909ff4d2019-08-01 09:46:45 -0600718 val = env_match((uchar *)name, i);
Macpaul Linff2a9f32011-04-26 16:16:45 +0000719 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000720 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200721
wdenka68d3ed2002-10-11 08:38:32 +0000722 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000723 for (n = 0; n < len; ++n, ++buf) {
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100724 c = env_get_char(val++);
725 if (c < 0)
726 return c;
727 *buf = c;
Igor Grinbergde832562011-11-07 01:13:59 +0000728 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200729 return n;
730 }
731
732 if (n)
733 *--buf = '\0';
734
Heinrich Schuchardtcf80bd72019-01-06 11:52:06 +0100735 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
736 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200737
738 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000739 }
Igor Grinbergde832562011-11-07 01:13:59 +0000740
Macpaul Linff2a9f32011-04-26 16:16:45 +0000741 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000742}
743
Simon Glassbf690f82011-10-14 13:25:18 +0000744/**
745 * Decode the integer value of an environment variable and return it.
746 *
Shyam Sainiefe757c2018-06-07 19:47:19 +0530747 * @param name Name of environment variable
Simon Glassbf690f82011-10-14 13:25:18 +0000748 * @param base Number base to use (normally 10, or 16 for hex)
749 * @param default_val Default value to return if the variable is not
750 * found
751 * @return the decoded value, or default_val if not found
752 */
Simon Glass22c34c22017-08-03 12:22:13 -0600753ulong env_get_ulong(const char *name, int base, ulong default_val)
Simon Glassbf690f82011-10-14 13:25:18 +0000754{
755 /*
Simon Glass64b723f2017-08-03 12:22:12 -0600756 * We can use env_get() here, even before relocation, since the
Simon Glassbf690f82011-10-14 13:25:18 +0000757 * environment variable value is an integer and thus short.
758 */
Simon Glass64b723f2017-08-03 12:22:12 -0600759 const char *str = env_get(name);
Simon Glassbf690f82011-10-14 13:25:18 +0000760
761 return str ? simple_strtoul(str, NULL, base) : default_val;
762}
763
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000764#ifndef CONFIG_SPL_BUILD
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100765#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000766static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
767 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000768{
Simon Glassd49b8892017-08-03 12:22:08 -0600769 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000770}
wdenk57b2d802003-06-27 21:31:46 +0000771
Mike Frysinger61583932008-12-30 02:59:25 -0500772U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200773 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600774 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200775 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500776);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200777
778#if defined(CONFIG_CMD_ERASEENV)
779static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
780 char * const argv[])
781{
782 return env_erase() ? 1 : 0;
783}
784
785U_BOOT_CMD(
786 eraseenv, 1, 0, do_env_erase,
787 "erase environment variables from persistent storage",
788 ""
789);
790#endif
wdenka68d3ed2002-10-11 08:38:32 +0000791#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000792#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000793
Simon Glass909ff4d2019-08-01 09:46:45 -0600794int env_match(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000795{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000796 if (s1 == NULL)
797 return -1;
798
wdenka68d3ed2002-10-11 08:38:32 +0000799 while (*s1 == env_get_char(i2++))
800 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000801 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000802
wdenka68d3ed2002-10-11 08:38:32 +0000803 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000804 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000805
Macpaul Linff2a9f32011-04-26 16:16:45 +0000806 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000807}
wdenk57b2d802003-06-27 21:31:46 +0000808
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000809#ifndef CONFIG_SPL_BUILD
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300810static int do_env_default(cmd_tbl_t *cmdtp, int flag,
Igor Grinbergde832562011-11-07 01:13:59 +0000811 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200812{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300813 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000814
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000815 debug("Initial value for argc=%d\n", argc);
816 while (--argc > 0 && **++argv == '-') {
817 char *arg = *argv;
818
819 while (*++arg) {
820 switch (*arg) {
821 case 'a': /* default all */
822 all = 1;
823 break;
824 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300825 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000826 break;
827 default:
828 return cmd_usage(cmdtp);
829 }
830 }
831 }
832 debug("Final value for argc=%d\n", argc);
833 if (all && (argc == 0)) {
834 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600835 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300836 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000837 return 0;
838 }
839 if (!all && (argc > 0)) {
840 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600841 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000842 return 0;
843 }
844
845 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200846}
847
Igor Grinbergde832562011-11-07 01:13:59 +0000848static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
849 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200850{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600851 int env_flag = H_INTERACTIVE;
852 int ret = 0;
853
854 debug("Initial value for argc=%d\n", argc);
855 while (argc > 1 && **(argv + 1) == '-') {
856 char *arg = *++argv;
857
858 --argc;
859 while (*++arg) {
860 switch (*arg) {
861 case 'f': /* force */
862 env_flag |= H_FORCE;
863 break;
864 default:
865 return CMD_RET_USAGE;
866 }
867 }
868 }
869 debug("Final value for argc=%d\n", argc);
870
871 env_id++;
872
873 while (--argc > 0) {
874 char *name = *++argv;
875
876 if (!hdelete_r(name, &env_htab, env_flag))
877 ret = 1;
878 }
879
880 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200881}
882
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500883#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200884/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100885 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200886 * -t: export as text format; if size is given, data will be
887 * padded with '\0' bytes; if not, one terminating '\0'
888 * will be added (which is included in the "filesize"
889 * setting so you can for exmple copy this to flash and
890 * keep the termination).
891 * -b: export as binary format (name=value pairs separated by
892 * '\0', list end marked by double "\0\0")
893 * -c: export as checksum protected environment format as
894 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100895 * -s size:
896 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200897 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100898 * var... List of variable names that get included into the
899 * export. Without arguments, the whole environment gets
900 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200901 *
902 * With "-c" and size is NOT given, then the export command will
903 * format the data as currently used for the persistent storage,
904 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400905 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200906 * environment, a "current" redundancy flag. If size is given, this
907 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
908 * checksum and redundancy flag will be inserted.
909 *
910 * With "-b" and "-t", always only the real data (including a
911 * terminating '\0' byte) will be written; here the optional size
912 * argument will be used to make sure not to overflow the user
913 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400914 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200915 *
916 * On successful return, the variable "filesize" will be set.
917 * Note that filesize includes the trailing/terminating '\0' byte(s).
918 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400919 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200920 *
921 * => env export -t 100000
922 * => era ${backup_addr} +${filesize}
923 * => cp.b 100000 ${backup_addr} ${filesize}
924 *
925 * Re-import this snapshot, deleting all other settings:
926 *
927 * => env import -d -t ${backup_addr}
928 */
Igor Grinbergde832562011-11-07 01:13:59 +0000929static int do_env_export(cmd_tbl_t *cmdtp, int flag,
930 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200931{
932 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600933 ulong addr;
934 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100935 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200936 ssize_t len;
937 env_t *envp;
938 char sep = '\n';
939 int chk = 0;
940 int fmt = 0;
941
942 cmd = *argv;
943
944 while (--argc > 0 && **++argv == '-') {
945 char *arg = *argv;
946 while (*++arg) {
947 switch (*arg) {
948 case 'b': /* raw binary format */
949 if (fmt++)
950 goto sep_err;
951 sep = '\0';
952 break;
953 case 'c': /* external checksum format */
954 if (fmt++)
955 goto sep_err;
956 sep = '\0';
957 chk = 1;
958 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100959 case 's': /* size given */
960 if (--argc <= 0)
961 return cmd_usage(cmdtp);
962 size = simple_strtoul(*++argv, NULL, 16);
963 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200964 case 't': /* text format */
965 if (fmt++)
966 goto sep_err;
967 sep = '\n';
968 break;
969 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000970 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200971 }
972 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100973NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200974 }
975
Macpaul Linff2a9f32011-04-26 16:16:45 +0000976 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000977 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000978
Simon Glassa7459262013-10-25 23:01:31 -0600979 addr = simple_strtoul(argv[0], NULL, 16);
980 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200981
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100982 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600983 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100984
985 argc--;
986 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200987
988 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000989 len = hexport_r(&env_htab, sep,
990 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600991 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200992 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200993 pr_err("## Error: Cannot export environment: errno = %d\n",
994 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200995 return 1;
996 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100997 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600998 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200999
1000 return 0;
1001 }
1002
Simon Glassa7459262013-10-25 23:01:31 -06001003 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001004
1005 if (chk) /* export as checksum protected block */
1006 res = (char *)envp->data;
1007 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -06001008 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001009
Wolfgang Denk565af272013-03-23 23:50:28 +00001010 len = hexport_r(&env_htab, '\0',
1011 H_MATCH_KEY | H_MATCH_IDENT,
1012 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001013 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001014 pr_err("## Error: Cannot export environment: errno = %d\n",
1015 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001016 return 1;
1017 }
1018
1019 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +00001020 envp->crc = crc32(0, envp->data,
1021 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001022#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -06001023 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001024#endif
1025 }
Simon Glass4d949a22017-08-03 12:22:10 -06001026 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001027
1028 return 0;
1029
1030sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001031 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1032 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001033 return 1;
1034}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001035#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001036
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001037#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001038/*
Quentin Schulz0312b942018-07-09 19:16:29 +02001039 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1040 * -d: delete existing environment before importing if no var is
1041 * passed; if vars are passed, if one var is in the current
1042 * environment but not in the environment at addr, delete var from
1043 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04001044 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001045 * -t: assume text format; either "size" must be given or the
1046 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +02001047 * -r: handle CRLF like LF, that means exported variables with
1048 * a content which ends with \r won't get imported. Used
1049 * to import text files created with editors which are using CRLF
1050 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001051 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1052 * -c: assume checksum protected environment format
1053 * addr: memory address to read from
1054 * size: length of input data; if missing, proper '\0'
1055 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +02001056 * if var is set and size should be missing (i.e. '\0'
1057 * termination), set size to '-'
1058 * var... List of the names of the only variables that get imported from
1059 * the environment at address 'addr'. Without arguments, the whole
1060 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001061 */
Igor Grinbergde832562011-11-07 01:13:59 +00001062static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1063 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001064{
Simon Glassa7459262013-10-25 23:01:31 -06001065 ulong addr;
1066 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001067 char sep = '\n';
1068 int chk = 0;
1069 int fmt = 0;
1070 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +02001071 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +02001072 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001073 size_t size;
1074
1075 cmd = *argv;
1076
1077 while (--argc > 0 && **++argv == '-') {
1078 char *arg = *argv;
1079 while (*++arg) {
1080 switch (*arg) {
1081 case 'b': /* raw binary format */
1082 if (fmt++)
1083 goto sep_err;
1084 sep = '\0';
1085 break;
1086 case 'c': /* external checksum format */
1087 if (fmt++)
1088 goto sep_err;
1089 sep = '\0';
1090 chk = 1;
1091 break;
1092 case 't': /* text format */
1093 if (fmt++)
1094 goto sep_err;
1095 sep = '\n';
1096 break;
Alexander Holler3e12be72014-07-14 17:49:55 +02001097 case 'r': /* handle CRLF like LF */
1098 crlf_is_lf = 1;
1099 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001100 case 'd':
1101 del = 1;
1102 break;
1103 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001104 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001105 }
1106 }
1107 }
1108
Macpaul Linff2a9f32011-04-26 16:16:45 +00001109 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001110 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001111
1112 if (!fmt)
1113 printf("## Warning: defaulting to text format\n");
1114
Alexander Holler3e12be72014-07-14 17:49:55 +02001115 if (sep != '\n' && crlf_is_lf )
1116 crlf_is_lf = 0;
1117
Simon Glassa7459262013-10-25 23:01:31 -06001118 addr = simple_strtoul(argv[0], NULL, 16);
1119 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001120
Quentin Schulz0312b942018-07-09 19:16:29 +02001121 if (argc >= 2 && strcmp(argv[1], "-")) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001122 size = simple_strtoul(argv[1], NULL, 16);
Quentin Schulz0312b942018-07-09 19:16:29 +02001123 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -05001124 puts("## Error: external checksum format must pass size\n");
1125 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001126 } else {
Simon Glassa7459262013-10-25 23:01:31 -06001127 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001128
1129 size = 0;
1130
1131 while (size < MAX_ENV_SIZE) {
1132 if ((*s == sep) && (*(s+1) == '\0'))
1133 break;
1134 ++s;
1135 ++size;
1136 }
1137 if (size == MAX_ENV_SIZE) {
1138 printf("## Warning: Input data exceeds %d bytes"
1139 " - truncated\n", MAX_ENV_SIZE);
1140 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001141 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001142 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001143 }
1144
Quentin Schulz0312b942018-07-09 19:16:29 +02001145 if (argc > 2)
1146 wl = 1;
1147
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001148 if (chk) {
1149 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -06001150 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001151
1152 size -= offsetof(env_t, data);
1153 memcpy(&crc, &ep->crc, sizeof(crc));
1154
1155 if (crc32(0, ep->data, size) != crc) {
1156 puts("## Error: bad CRC, import failed\n");
1157 return 1;
1158 }
Simon Glassa7459262013-10-25 23:01:31 -06001159 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001160 }
1161
Quentin Schulz0312b942018-07-09 19:16:29 +02001162 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1163 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001164 pr_err("## Error: Environment import failed: errno = %d\n",
1165 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001166 return 1;
1167 }
1168 gd->flags |= GD_FLG_ENV_READY;
1169
1170 return 0;
1171
1172sep_err:
1173 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1174 cmd);
1175 return 1;
1176}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001177#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001178
Leo Ruan7450b872019-05-24 17:20:19 +02001179#if defined(CONFIG_CMD_NVEDIT_INFO)
1180/*
1181 * print_env_info - print environment information
1182 */
1183static int print_env_info(void)
1184{
1185 const char *value;
1186
1187 /* print environment validity value */
1188 switch (gd->env_valid) {
1189 case ENV_INVALID:
1190 value = "invalid";
1191 break;
1192 case ENV_VALID:
1193 value = "valid";
1194 break;
1195 case ENV_REDUND:
1196 value = "redundant";
1197 break;
1198 default:
1199 value = "unknown";
1200 break;
1201 }
1202 printf("env_valid = %s\n", value);
1203
1204 /* print environment ready flag */
1205 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1206 printf("env_ready = %s\n", value);
1207
1208 /* print environment using default flag */
1209 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1210 printf("env_use_default = %s\n", value);
1211
1212 return CMD_RET_SUCCESS;
1213}
1214
1215#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1216#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1217
1218/*
1219 * env info - display environment information
1220 * env info [-d] - evaluate whether default environment is used
1221 * env info [-p] - evaluate whether environment can be persisted
1222 */
1223static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1224 int argc, char * const argv[])
1225{
1226 int eval_flags = 0;
1227 int eval_results = 0;
1228
1229 /* display environment information */
1230 if (argc <= 1)
1231 return print_env_info();
1232
1233 /* process options */
1234 while (--argc > 0 && **++argv == '-') {
1235 char *arg = *argv;
1236
1237 while (*++arg) {
1238 switch (*arg) {
1239 case 'd':
1240 eval_flags |= ENV_INFO_IS_DEFAULT;
1241 break;
1242 case 'p':
1243 eval_flags |= ENV_INFO_IS_PERSISTED;
1244 break;
1245 default:
1246 return CMD_RET_USAGE;
1247 }
1248 }
1249 }
1250
1251 /* evaluate whether default environment is used */
1252 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1253 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1254 printf("Default environment is used\n");
1255 eval_results |= ENV_INFO_IS_DEFAULT;
1256 } else {
1257 printf("Environment was loaded from persistent storage\n");
1258 }
1259 }
1260
1261 /* evaluate whether environment can be persisted */
1262 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1263#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1264 printf("Environment can be persisted\n");
1265 eval_results |= ENV_INFO_IS_PERSISTED;
1266#else
1267 printf("Environment cannot be persisted\n");
1268#endif
1269 }
1270
1271 /* The result of evaluations is combined with AND */
1272 if (eval_flags != eval_results)
1273 return CMD_RET_FAILURE;
1274
1275 return CMD_RET_SUCCESS;
1276}
1277#endif
1278
Andrew Ruder94463402013-10-22 19:07:34 -05001279#if defined(CONFIG_CMD_ENV_EXISTS)
1280static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1281 char * const argv[])
1282{
Simon Glass1a236862019-08-02 09:44:18 -06001283 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001284
1285 if (argc < 2)
1286 return CMD_RET_USAGE;
1287
1288 e.key = argv[1];
1289 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001290 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001291
1292 return (ep == NULL) ? 1 : 0;
1293}
1294#endif
1295
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001296/*
1297 * New command line interface: "env" command with subcommands
1298 */
1299static cmd_tbl_t cmd_env_sub[] = {
1300#if defined(CONFIG_CMD_ASKENV)
1301 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1302#endif
1303 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001304 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001305#if defined(CONFIG_CMD_EDITENV)
1306 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1307#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001308#if defined(CONFIG_CMD_ENV_CALLBACK)
1309 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1310#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001311#if defined(CONFIG_CMD_ENV_FLAGS)
1312 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1313#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001314#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001315 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001316#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001317#if defined(CONFIG_CMD_GREPENV)
1318 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1319#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001320#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001321 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001322#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001323#if defined(CONFIG_CMD_NVEDIT_INFO)
1324 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1325#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001326 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1327#if defined(CONFIG_CMD_RUN)
1328 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1329#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001330#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001331 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001332#if defined(CONFIG_CMD_ERASEENV)
1333 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1334#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001335#endif
1336 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001337#if defined(CONFIG_CMD_ENV_EXISTS)
1338 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1339#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001340};
1341
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001342#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001343void env_reloc(void)
1344{
1345 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1346}
1347#endif
1348
Macpaul Linff2a9f32011-04-26 16:16:45 +00001349static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001350{
1351 cmd_tbl_t *cp;
1352
Thomas Weber4d282652010-11-24 13:07:52 +01001353 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001354 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001355
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001356 /* drop initial "env" arg */
1357 argc--;
1358 argv++;
1359
1360 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1361
1362 if (cp)
1363 return cp->cmd(cmdtp, flag, argc, argv);
1364
Simon Glassa06dfc72011-12-10 08:44:01 +00001365 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001366}
1367
Kim Phillipsdc00a682012-10-29 13:34:31 +00001368#ifdef CONFIG_SYS_LONGHELP
1369static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001370#if defined(CONFIG_CMD_ASKENV)
1371 "ask name [message] [size] - ask for environment variable\nenv "
1372#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001373#if defined(CONFIG_CMD_ENV_CALLBACK)
1374 "callbacks - print callbacks and their associated variables\nenv "
1375#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001376 "default [-f] -a - [forcibly] reset default environment\n"
1377 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001378 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001379#if defined(CONFIG_CMD_EDITENV)
1380 "env edit name - edit environment variable\n"
1381#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001382#if defined(CONFIG_CMD_ENV_EXISTS)
1383 "env exists name - tests for existence of variable\n"
1384#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001385#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001386 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001387#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001388#if defined(CONFIG_CMD_ENV_FLAGS)
1389 "env flags - print variables that have non-default flags\n"
1390#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001391#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001392#ifdef CONFIG_REGEX
1393 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1394#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001395 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001396#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001397#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001398#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001399 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001400#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001401#if defined(CONFIG_CMD_NVEDIT_INFO)
1402 "env info - display environment information\n"
1403 "env info [-d] - whether default environment is used\n"
1404 "env info [-p] - whether environment can be persisted\n"
1405#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001406 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001407#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001408 "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001409#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001410#if defined(CONFIG_CMD_RUN)
1411 "env run var [...] - run commands in an environment variable\n"
1412#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001413#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001414 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001415#if defined(CONFIG_CMD_ERASEENV)
1416 "env erase - erase environment\n"
1417#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001418#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001419#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001420 "env set -e [-nv][-bs][-rt][-a][-i addr,size][-v] name [arg ...]\n"
1421 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001422#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001423 "env set [-f] name [arg ...]\n";
1424#endif
1425
1426U_BOOT_CMD(
1427 env, CONFIG_SYS_MAXARGS, 1, do_env,
1428 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001429);
1430
1431/*
1432 * Old command line interface, kept for compatibility
1433 */
wdenk57b2d802003-06-27 21:31:46 +00001434
Peter Tyser0deafa22009-10-25 15:12:56 -05001435#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001436U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001437 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001438 "edit environment variable",
1439 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001440 " - edit environment variable 'name'",
1441 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001442);
1443#endif
1444
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001445U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001446 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001447 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001448 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001449#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001450 "printenv -e [-guid guid|-all][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001451 " - print UEFI variable 'name' or all the variables\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001452 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001453#endif
wdenk57b2d802003-06-27 21:31:46 +00001454 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001455 " - print value of environment variable 'name'",
1456 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001457);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001458
1459#ifdef CONFIG_CMD_GREPENV
1460U_BOOT_CMD_COMPLETE(
1461 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1462 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001463#ifdef CONFIG_REGEX
1464 "[-e] [-n | -v | -b] string ...\n"
1465#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001466 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001467#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001468 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001469#ifdef CONFIG_REGEX
1470 " \"-e\": enable regular expressions;\n"
1471#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001472 " \"-n\": search variable names; \"-v\": search values;\n"
1473 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001474 var_complete
1475);
1476#endif
wdenk57b2d802003-06-27 21:31:46 +00001477
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001478U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001479 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001480 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001481#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001482 "-e [-guid guid][-nv][-bs][-rt][-a][-v]\n"
1483 " [-i addr,size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001484 " - set UEFI variable 'name' to 'value' ...'\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001485 " \"-guid\": set vendor guid\n"
1486 " \"-nv\": set non-volatile attribute\n"
1487 " \"-bs\": set boot-service attribute\n"
1488 " \"-rt\": set runtime attribute\n"
1489 " \"-a\": append-write\n"
1490 " \"-i addr,size\": use <addr,size> as variable's value\n"
1491 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001492 " - delete UEFI variable 'name' if 'value' not specified\n"
1493#endif
1494 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001495 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1496 "setenv [-f] name\n"
1497 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001498 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001499);
1500
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001501#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001502
wdenkf287a242003-07-01 21:06:45 +00001503U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001504 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001505 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001506 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001507 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001508);
Jon Loeligerd704d912007-07-10 11:02:44 -05001509#endif
wdenk57b2d802003-06-27 21:31:46 +00001510
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001511#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001512U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001513 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001514 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001515 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001516 " - run the commands in the environment variable(s) 'var'",
1517 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001518);
Jon Loeligerd704d912007-07-10 11:02:44 -05001519#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001520#endif /* CONFIG_SPL_BUILD */