blob: d0d2eca904729089802a3a62b4e7922bf4f6e450 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060032#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060033#include <net.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020034#include <search.h>
35#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050036#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050037#include <mapmem.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060038#include <linux/bitops.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070039#include <u-boot/crc.h>
wdenk2a3cb022002-11-05 21:01:48 +000040#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000041#include <linux/stddef.h>
42#include <asm/byteorder.h>
Simon Glassa7459262013-10-25 23:01:31 -060043#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000044
Wolfgang Denk6405a152006-03-31 18:32:53 +020045DECLARE_GLOBAL_DATA_PTR;
46
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010047#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
48 defined(CONFIG_ENV_IS_IN_FLASH) || \
49 defined(CONFIG_ENV_IS_IN_MMC) || \
50 defined(CONFIG_ENV_IS_IN_FAT) || \
51 defined(CONFIG_ENV_IS_IN_EXT4) || \
52 defined(CONFIG_ENV_IS_IN_NAND) || \
53 defined(CONFIG_ENV_IS_IN_NVRAM) || \
54 defined(CONFIG_ENV_IS_IN_ONENAND) || \
55 defined(CONFIG_ENV_IS_IN_SATA) || \
56 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
57 defined(CONFIG_ENV_IS_IN_REMOTE) || \
58 defined(CONFIG_ENV_IS_IN_UBI)
59
60#define ENV_IS_IN_DEVICE
61
62#endif
63
64#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000065 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030066# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020067NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000068#endif
69
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020070/*
71 * Maximum expected input data size for import command
72 */
73#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000074
wdenka68d3ed2002-10-11 08:38:32 +000075/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020076 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060077 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020078 * has changed or not. So it is possible to reread an environment
79 * variable only if the environment was changed ... done so for
80 * example in NetInitLoop()
81 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010082static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000083
Simon Glass2d85a752019-08-01 09:46:41 -060084int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010085{
86 return env_id;
87}
wdenka68d3ed2002-10-11 08:38:32 +000088
Ilya Yanokf7a2c552012-09-18 00:22:50 +000089#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040090/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020091 * Command interface: print one or all environment variables
92 *
93 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040094 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060095static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000096{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020097 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000098 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000099
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200100 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -0600101 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000102
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200103 e.key = name;
104 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600105 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200106 if (ep == NULL)
107 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000108 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200109 return len;
110 }
wdenka68d3ed2002-10-11 08:38:32 +0000111
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200112 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000114
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200115 if (len > 0) {
116 puts(res);
117 free(res);
118 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000119 }
120
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200121 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000122 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200123 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400124}
wdenka68d3ed2002-10-11 08:38:32 +0000125
Simon Glassed38aef2020-05-10 11:40:03 -0600126static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
127 char *const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400128{
129 int i;
130 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600131 int env_flag = H_HIDE_DOT;
132
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900133#if defined(CONFIG_CMD_NVEDIT_EFI)
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
135 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
136#endif
137
Joe Hershberger79a905e2012-12-11 22:16:23 -0600138 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
139 argc--;
140 argv++;
141 env_flag &= ~H_HIDE_DOT;
142 }
wdenka68d3ed2002-10-11 08:38:32 +0000143
Mike Frysinger87944922009-05-24 02:26:19 -0400144 if (argc == 1) {
145 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600146 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200147 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400148 return 1;
149 printf("\nEnvironment size: %d/%ld bytes\n",
150 rcode, (ulong)ENV_SIZE);
151 return 0;
152 }
wdenka68d3ed2002-10-11 08:38:32 +0000153
Mike Frysinger87944922009-05-24 02:26:19 -0400154 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600155 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400156 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600157 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200158 if (!rc) {
159 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400160 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000161 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000162 }
163
164 return rcode;
165}
166
167#ifdef CONFIG_CMD_GREPENV
Simon Glassed38aef2020-05-10 11:40:03 -0600168static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
169 int argc, char *const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000170{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000171 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000172 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000173
174 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000175 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000176
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000177 grep_how = H_MATCH_SUBSTR; /* default: substring search */
178 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000179
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200180 while (--argc > 0 && **++argv == '-') {
181 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000182 while (*++arg) {
183 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000184#ifdef CONFIG_REGEX
185 case 'e': /* use regex matching */
186 grep_how = H_MATCH_REGEX;
187 break;
188#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000189 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000190 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000191 break;
192 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000193 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000194 break;
195 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000196 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000197 break;
198 case '-':
199 goto DONE;
200 default:
201 return CMD_RET_USAGE;
202 }
203 }
204 }
205
206DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000207 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000208 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000209 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000210
Wolfgang Denke27fc122013-03-23 23:50:29 +0000211 if (len > 0) {
212 puts(res);
213 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000214 }
Mike Frysinger87944922009-05-24 02:26:19 -0400215
Wolfgang Denke27fc122013-03-23 23:50:29 +0000216 if (len < 2)
217 return 1;
218
219 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000220}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000221#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000222#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000223
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200224/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000225 * Set a new environment variable,
226 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600227 */
Simon Glassed38aef2020-05-10 11:40:03 -0600228static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000229{
230 int i, len;
231 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600232 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000233
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600234 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900235
236#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
237 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
238 return do_env_set_efi(NULL, flag, --argc, ++argv);
239#endif
240
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600241 while (argc > 1 && **(argv + 1) == '-') {
242 char *arg = *++argv;
243
244 --argc;
245 while (*++arg) {
246 switch (*arg) {
247 case 'f': /* force */
248 env_flag |= H_FORCE;
249 break;
250 default:
251 return CMD_RET_USAGE;
252 }
253 }
254 }
255 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000256 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000257
258 if (strchr(name, '=')) {
259 printf("## Error: illegal character '='"
260 "in variable name \"%s\"\n", name);
261 return 1;
262 }
263
264 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000265
wdenka68d3ed2002-10-11 08:38:32 +0000266 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000267 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600268 int rc = hdelete_r(name, &env_htab, env_flag);
Simon Glass783ef3f2020-11-05 10:33:37 -0700269
270 /* If the variable didn't exist, don't report an error */
271 return rc && rc != -ENOENT ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000272 }
273
274 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200275 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000276 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000277 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000278 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000279
280 value = malloc(len);
281 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200282 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000283 return 1;
284 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000285 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200286 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000287
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200288 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000289 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000290 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000291 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200292 if (s != value)
293 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000294
Igor Grinbergde832562011-11-07 01:13:59 +0000295 e.key = name;
296 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600297 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200298 free(value);
299 if (!ep) {
300 printf("## Error inserting \"%s\" variable, errno=%d\n",
301 name, errno);
302 return 1;
303 }
wdenka68d3ed2002-10-11 08:38:32 +0000304
wdenka68d3ed2002-10-11 08:38:32 +0000305 return 0;
306}
307
Simon Glass6a38e412017-08-03 12:22:09 -0600308int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000309{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200310 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
311
Joe Hershberger3902c602013-04-08 10:32:50 +0000312 /* before import into hashtable */
313 if (!(gd->flags & GD_FLG_ENV_READY))
314 return 1;
315
Igor Grinbergde832562011-11-07 01:13:59 +0000316 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500317 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200318 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500319 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000320}
321
Simon Glassb8e926e2011-10-24 17:59:59 +0000322/**
323 * Set an environment variable to an integer value
324 *
Simon Glassdf372d62013-05-07 06:11:45 +0000325 * @param varname Environment variable to set
Simon Glassb8e926e2011-10-24 17:59:59 +0000326 * @param value Value to set it to
327 * @return 0 if ok, 1 on error
328 */
Simon Glass4d949a22017-08-03 12:22:10 -0600329int env_set_ulong(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000330{
331 /* TODO: this should be unsigned */
332 char *str = simple_itoa(value);
333
Simon Glass6a38e412017-08-03 12:22:09 -0600334 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000335}
336
337/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000338 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000339 *
Simon Glassdf372d62013-05-07 06:11:45 +0000340 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000341 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000342 * @return 0 if ok, 1 on error
343 */
Simon Glass4d949a22017-08-03 12:22:10 -0600344int env_set_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000345{
346 char str[17];
347
Simon Glass044dc9e2013-02-24 17:33:21 +0000348 sprintf(str, "%lx", value);
Simon Glass6a38e412017-08-03 12:22:09 -0600349 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000350}
351
Simon Glass22c34c22017-08-03 12:22:13 -0600352ulong env_get_hex(const char *varname, ulong default_val)
Simon Glassabbd5122013-04-20 08:42:43 +0000353{
354 const char *s;
355 ulong value;
356 char *endp;
357
Simon Glass64b723f2017-08-03 12:22:12 -0600358 s = env_get(varname);
Simon Glassabbd5122013-04-20 08:42:43 +0000359 if (s)
360 value = simple_strtoul(s, &endp, 16);
361 if (!s || endp == s)
362 return default_val;
363
364 return value;
365}
366
Alex Kiernan9c215492018-04-01 09:22:38 +0000367int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
368{
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500369 string_to_enetaddr(env_get(name), enetaddr);
Alex Kiernan9c215492018-04-01 09:22:38 +0000370 return is_valid_ethaddr(enetaddr);
371}
372
373int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
374{
375 char buf[ARP_HLEN_ASCII + 1];
376
377 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
378 return -EEXIST;
379
380 sprintf(buf, "%pM", enetaddr);
381
382 return env_set(name, buf);
383}
384
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000385#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600386static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
387 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000388{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200389 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000390 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000391
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500392 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000393}
394
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200395/*
wdenka68d3ed2002-10-11 08:38:32 +0000396 * Prompt for environment variable
397 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500398#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600399int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000400{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200401 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000402 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000403 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000404 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000405
406 local_args[0] = argv[0];
407 local_args[1] = argv[1];
408 local_args[2] = NULL;
409 local_args[3] = NULL;
410
Wolfgang Denk586895d2013-02-20 04:53:16 +0000411 /*
412 * Check the syntax:
413 *
414 * env_ask envname [message1 ...] [size]
415 */
416 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000417 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000418
Wolfgang Denk586895d2013-02-20 04:53:16 +0000419 /*
420 * We test the last argument if it can be converted
421 * into a decimal number. If yes, we assume it's
422 * the size. Otherwise we echo it as part of the
423 * message.
424 */
425 i = simple_strtoul(argv[argc - 1], &endptr, 10);
426 if (*endptr != '\0') { /* no size */
427 size = CONFIG_SYS_CBSIZE - 1;
428 } else { /* size given */
429 size = i;
430 --argc;
431 }
wdenka68d3ed2002-10-11 08:38:32 +0000432
Wolfgang Denk586895d2013-02-20 04:53:16 +0000433 if (argc <= 2) {
434 sprintf(message, "Please enter '%s': ", argv[1]);
435 } else {
436 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400437 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000438 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000439 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000440
Tom Rini4beb2352017-09-26 19:37:11 -0400441 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000442 pos += strlen(argv[i]);
443 }
Tom Rini4beb2352017-09-26 19:37:11 -0400444 if (pos < sizeof(message) - 1) {
445 message[pos++] = ' ';
446 message[pos] = '\0';
447 } else
448 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000449 }
450
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200451 if (size >= CONFIG_SYS_CBSIZE)
452 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000453
454 if (size <= 0)
455 return 1;
456
457 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600458 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000459
460 if (size < len)
461 console_buffer[size] = '\0';
462
463 len = 2;
464 if (console_buffer[0] != '\0') {
465 local_args[2] = console_buffer;
466 len = 3;
467 }
468
469 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500470 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000471}
Jon Loeligerd704d912007-07-10 11:02:44 -0500472#endif
wdenka68d3ed2002-10-11 08:38:32 +0000473
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600474#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500475static int print_static_binding(const char *var_name, const char *callback_name,
476 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600477{
478 printf("\t%-20s %-20s\n", var_name, callback_name);
479
480 return 0;
481}
482
Simon Glass1a236862019-08-02 09:44:18 -0600483static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600484{
485 struct env_clbk_tbl *clbkp;
486 int i;
487 int num_callbacks;
488
489 if (entry->callback == NULL)
490 return 0;
491
492 /* look up the callback in the linker-list */
493 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
494 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
495 i < num_callbacks;
496 i++, clbkp++) {
497#if defined(CONFIG_NEEDS_MANUAL_RELOC)
498 if (entry->callback == clbkp->callback + gd->reloc_off)
499#else
500 if (entry->callback == clbkp->callback)
501#endif
502 break;
503 }
504
505 if (i == num_callbacks)
506 /* this should probably never happen, but just in case... */
507 printf("\t%-20s %p\n", entry->key, entry->callback);
508 else
509 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
510
511 return 0;
512}
513
514/*
515 * Print the callbacks available and what they are bound to
516 */
Simon Glassed38aef2020-05-10 11:40:03 -0600517int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
518 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600519{
520 struct env_clbk_tbl *clbkp;
521 int i;
522 int num_callbacks;
523
524 /* Print the available callbacks */
525 puts("Available callbacks:\n");
526 puts("\tCallback Name\n");
527 puts("\t-------------\n");
528 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
529 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
530 i < num_callbacks;
531 i++, clbkp++)
532 printf("\t%s\n", clbkp->name);
533 puts("\n");
534
535 /* Print the static bindings that may exist */
536 puts("Static callback bindings:\n");
537 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
538 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500539 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600540 puts("\n");
541
542 /* walk through each variable and print the callback if it has one */
543 puts("Active callback bindings:\n");
544 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
545 printf("\t%-20s %-20s\n", "-------------", "-------------");
546 hwalk_r(&env_htab, print_active_callback);
547 return 0;
548}
549#endif
550
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600551#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500552static int print_static_flags(const char *var_name, const char *flags,
553 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600554{
555 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600556 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600557
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600558 printf("\t%-20s %-20s %-20s\n", var_name,
559 env_flags_get_vartype_name(type),
560 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600561
562 return 0;
563}
564
Simon Glass1a236862019-08-02 09:44:18 -0600565static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600566{
567 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600568 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600569
570 if (entry->flags == 0)
571 return 0;
572
573 type = (enum env_flags_vartype)
574 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600575 access = env_flags_parse_varaccess_from_binflags(entry->flags);
576 printf("\t%-20s %-20s %-20s\n", entry->key,
577 env_flags_get_vartype_name(type),
578 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600579
580 return 0;
581}
582
583/*
584 * Print the flags available and what variables have flags
585 */
Simon Glassed38aef2020-05-10 11:40:03 -0600586int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600587{
588 /* Print the available variable types */
589 printf("Available variable type flags (position %d):\n",
590 ENV_FLAGS_VARTYPE_LOC);
591 puts("\tFlag\tVariable Type Name\n");
592 puts("\t----\t------------------\n");
593 env_flags_print_vartypes();
594 puts("\n");
595
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600596 /* Print the available variable access types */
597 printf("Available variable access flags (position %d):\n",
598 ENV_FLAGS_VARACCESS_LOC);
599 puts("\tFlag\tVariable Access Name\n");
600 puts("\t----\t--------------------\n");
601 env_flags_print_varaccess();
602 puts("\n");
603
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600604 /* Print the static flags that may exist */
605 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600606 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
607 "Variable Access");
608 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
609 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500610 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600611 puts("\n");
612
613 /* walk through each variable and print the flags if non-default */
614 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600615 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
616 "Variable Access");
617 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
618 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600619 hwalk_r(&env_htab, print_active_flags);
620 return 0;
621}
622#endif
623
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200624/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500625 * Interactively edit an environment variable
626 */
627#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600628static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
629 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500630{
631 char buffer[CONFIG_SYS_CBSIZE];
632 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500633
Wolfgang Denk3b683112010-07-17 01:06:04 +0200634 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000635 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500636
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500637 /* before import into hashtable */
638 if (!(gd->flags & GD_FLG_ENV_READY))
639 return 1;
640
Peter Tyser0deafa22009-10-25 15:12:56 -0500641 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600642 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500643 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800644 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500645 else
646 buffer[0] = '\0';
647
Simon Glassbe6aafc2014-04-10 20:01:27 -0600648 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000649 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500650
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500651 if (buffer[0] == '\0') {
652 const char * const _argv[3] = { "setenv", argv[1], NULL };
653
654 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
655 } else {
656 const char * const _argv[4] = { "setenv", argv[1], buffer,
657 NULL };
658
659 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
660 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500661}
662#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000663#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500664
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200665/*
wdenka68d3ed2002-10-11 08:38:32 +0000666 * Look up variable from environment,
667 * return address of storage for that variable,
668 * or NULL if not found
669 */
Simon Glass64b723f2017-08-03 12:22:12 -0600670char *env_get(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000671{
Igor Grinbergde832562011-11-07 01:13:59 +0000672 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Simon Glass1a236862019-08-02 09:44:18 -0600673 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000674
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200675 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000676
Igor Grinbergde832562011-11-07 01:13:59 +0000677 e.key = name;
678 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600679 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200680
Macpaul Linff2a9f32011-04-26 16:16:45 +0000681 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000682 }
683
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200684 /* restricted capabilities before import */
Simon Glass64b723f2017-08-03 12:22:12 -0600685 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200686 return (char *)(gd->env_buf);
687
688 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000689}
690
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200691/*
Patrice Chotardceed4b42019-11-25 09:07:36 +0100692 * Like env_get, but prints an error if envvar isn't defined in the
693 * environment. It always returns what env_get does, so it can be used in
694 * place of env_get without changing error handling otherwise.
695 */
696char *from_env(const char *envvar)
697{
698 char *ret;
699
700 ret = env_get(envvar);
701
702 if (!ret)
703 printf("missing environment variable: %s\n", envvar);
704
705 return ret;
706}
707
708/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200709 * Look up variable from environment for restricted C runtime env.
710 */
Simon Glass64b723f2017-08-03 12:22:12 -0600711int env_get_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000712{
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100713 int i, nxt, c;
wdenka68d3ed2002-10-11 08:38:32 +0000714
Igor Grinbergde832562011-11-07 01:13:59 +0000715 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000716 int val, n;
717
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100718 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
719 if (c < 0)
720 return c;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000721 if (nxt >= CONFIG_ENV_SIZE)
722 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000723 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000724
Simon Glass909ff4d2019-08-01 09:46:45 -0600725 val = env_match((uchar *)name, i);
Macpaul Linff2a9f32011-04-26 16:16:45 +0000726 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000727 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200728
wdenka68d3ed2002-10-11 08:38:32 +0000729 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000730 for (n = 0; n < len; ++n, ++buf) {
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100731 c = env_get_char(val++);
732 if (c < 0)
733 return c;
734 *buf = c;
Igor Grinbergde832562011-11-07 01:13:59 +0000735 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200736 return n;
737 }
738
739 if (n)
740 *--buf = '\0';
741
Heinrich Schuchardtcf80bd72019-01-06 11:52:06 +0100742 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
743 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200744
745 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000746 }
Igor Grinbergde832562011-11-07 01:13:59 +0000747
Macpaul Linff2a9f32011-04-26 16:16:45 +0000748 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000749}
750
Simon Glassbf690f82011-10-14 13:25:18 +0000751/**
752 * Decode the integer value of an environment variable and return it.
753 *
Shyam Sainiefe757c2018-06-07 19:47:19 +0530754 * @param name Name of environment variable
Simon Glassbf690f82011-10-14 13:25:18 +0000755 * @param base Number base to use (normally 10, or 16 for hex)
756 * @param default_val Default value to return if the variable is not
757 * found
758 * @return the decoded value, or default_val if not found
759 */
Simon Glass22c34c22017-08-03 12:22:13 -0600760ulong env_get_ulong(const char *name, int base, ulong default_val)
Simon Glassbf690f82011-10-14 13:25:18 +0000761{
762 /*
Simon Glass64b723f2017-08-03 12:22:12 -0600763 * We can use env_get() here, even before relocation, since the
Simon Glassbf690f82011-10-14 13:25:18 +0000764 * environment variable value is an integer and thus short.
765 */
Simon Glass64b723f2017-08-03 12:22:12 -0600766 const char *str = env_get(name);
Simon Glassbf690f82011-10-14 13:25:18 +0000767
768 return str ? simple_strtoul(str, NULL, base) : default_val;
769}
770
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000771#ifndef CONFIG_SPL_BUILD
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100772#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Simon Glassed38aef2020-05-10 11:40:03 -0600773static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
774 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000775{
Simon Glassd49b8892017-08-03 12:22:08 -0600776 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000777}
wdenk57b2d802003-06-27 21:31:46 +0000778
Mike Frysinger61583932008-12-30 02:59:25 -0500779U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200780 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600781 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200782 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500783);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200784
785#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600786static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
787 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200788{
789 return env_erase() ? 1 : 0;
790}
791
792U_BOOT_CMD(
793 eraseenv, 1, 0, do_env_erase,
794 "erase environment variables from persistent storage",
795 ""
796);
797#endif
wdenka68d3ed2002-10-11 08:38:32 +0000798#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200799
800#if defined(CONFIG_CMD_NVEDIT_LOAD)
801static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
802 char *const argv[])
803{
804 return env_reload() ? 1 : 0;
805}
806#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200807
808#if defined(CONFIG_CMD_NVEDIT_SELECT)
809static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
810 char *const argv[])
811{
812 return env_select(argv[1]) ? 1 : 0;
813}
814#endif
815
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000816#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000817
Simon Glass909ff4d2019-08-01 09:46:45 -0600818int env_match(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000819{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000820 if (s1 == NULL)
821 return -1;
822
wdenka68d3ed2002-10-11 08:38:32 +0000823 while (*s1 == env_get_char(i2++))
824 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000825 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000826
wdenka68d3ed2002-10-11 08:38:32 +0000827 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000828 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000829
Macpaul Linff2a9f32011-04-26 16:16:45 +0000830 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000831}
wdenk57b2d802003-06-27 21:31:46 +0000832
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000833#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600834static int do_env_default(struct cmd_tbl *cmdtp, int flag,
835 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200836{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300837 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000838
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000839 debug("Initial value for argc=%d\n", argc);
840 while (--argc > 0 && **++argv == '-') {
841 char *arg = *argv;
842
843 while (*++arg) {
844 switch (*arg) {
845 case 'a': /* default all */
846 all = 1;
847 break;
848 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300849 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000850 break;
851 default:
852 return cmd_usage(cmdtp);
853 }
854 }
855 }
856 debug("Final value for argc=%d\n", argc);
857 if (all && (argc == 0)) {
858 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600859 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300860 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000861 return 0;
862 }
863 if (!all && (argc > 0)) {
864 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600865 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000866 return 0;
867 }
868
869 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200870}
871
Simon Glassed38aef2020-05-10 11:40:03 -0600872static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
873 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200874{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600875 int env_flag = H_INTERACTIVE;
876 int ret = 0;
877
878 debug("Initial value for argc=%d\n", argc);
879 while (argc > 1 && **(argv + 1) == '-') {
880 char *arg = *++argv;
881
882 --argc;
883 while (*++arg) {
884 switch (*arg) {
885 case 'f': /* force */
886 env_flag |= H_FORCE;
887 break;
888 default:
889 return CMD_RET_USAGE;
890 }
891 }
892 }
893 debug("Final value for argc=%d\n", argc);
894
895 env_id++;
896
897 while (--argc > 0) {
898 char *name = *++argv;
899
Simon Glass783ef3f2020-11-05 10:33:37 -0700900 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger8335ce72012-12-11 22:16:36 -0600901 ret = 1;
902 }
903
904 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200905}
906
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500907#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200908/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100909 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200910 * -t: export as text format; if size is given, data will be
911 * padded with '\0' bytes; if not, one terminating '\0'
912 * will be added (which is included in the "filesize"
913 * setting so you can for exmple copy this to flash and
914 * keep the termination).
915 * -b: export as binary format (name=value pairs separated by
916 * '\0', list end marked by double "\0\0")
917 * -c: export as checksum protected environment format as
918 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100919 * -s size:
920 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200921 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100922 * var... List of variable names that get included into the
923 * export. Without arguments, the whole environment gets
924 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200925 *
926 * With "-c" and size is NOT given, then the export command will
927 * format the data as currently used for the persistent storage,
928 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400929 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200930 * environment, a "current" redundancy flag. If size is given, this
931 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
932 * checksum and redundancy flag will be inserted.
933 *
934 * With "-b" and "-t", always only the real data (including a
935 * terminating '\0' byte) will be written; here the optional size
936 * argument will be used to make sure not to overflow the user
937 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400938 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200939 *
940 * On successful return, the variable "filesize" will be set.
941 * Note that filesize includes the trailing/terminating '\0' byte(s).
942 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400943 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200944 *
945 * => env export -t 100000
946 * => era ${backup_addr} +${filesize}
947 * => cp.b 100000 ${backup_addr} ${filesize}
948 *
949 * Re-import this snapshot, deleting all other settings:
950 *
951 * => env import -d -t ${backup_addr}
952 */
Simon Glassed38aef2020-05-10 11:40:03 -0600953static int do_env_export(struct cmd_tbl *cmdtp, int flag,
954 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200955{
956 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600957 ulong addr;
958 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100959 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200960 ssize_t len;
961 env_t *envp;
962 char sep = '\n';
963 int chk = 0;
964 int fmt = 0;
965
966 cmd = *argv;
967
968 while (--argc > 0 && **++argv == '-') {
969 char *arg = *argv;
970 while (*++arg) {
971 switch (*arg) {
972 case 'b': /* raw binary format */
973 if (fmt++)
974 goto sep_err;
975 sep = '\0';
976 break;
977 case 'c': /* external checksum format */
978 if (fmt++)
979 goto sep_err;
980 sep = '\0';
981 chk = 1;
982 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100983 case 's': /* size given */
984 if (--argc <= 0)
985 return cmd_usage(cmdtp);
986 size = simple_strtoul(*++argv, NULL, 16);
987 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200988 case 't': /* text format */
989 if (fmt++)
990 goto sep_err;
991 sep = '\n';
992 break;
993 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000994 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200995 }
996 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100997NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200998 }
999
Macpaul Linff2a9f32011-04-26 16:16:45 +00001000 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001001 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +00001002
Simon Glassa7459262013-10-25 23:01:31 -06001003 addr = simple_strtoul(argv[0], NULL, 16);
1004 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001005
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001006 if (size)
Simon Glassa7459262013-10-25 23:01:31 -06001007 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001008
1009 argc--;
1010 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001011
1012 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +00001013 len = hexport_r(&env_htab, sep,
1014 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -06001015 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001016 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001017 pr_err("## Error: Cannot export environment: errno = %d\n",
1018 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001019 return 1;
1020 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +01001021 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -06001022 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001023
1024 return 0;
1025 }
1026
Simon Glassa7459262013-10-25 23:01:31 -06001027 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001028
1029 if (chk) /* export as checksum protected block */
1030 res = (char *)envp->data;
1031 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -06001032 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001033
Wolfgang Denk565af272013-03-23 23:50:28 +00001034 len = hexport_r(&env_htab, '\0',
1035 H_MATCH_KEY | H_MATCH_IDENT,
1036 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001037 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001038 pr_err("## Error: Cannot export environment: errno = %d\n",
1039 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001040 return 1;
1041 }
1042
1043 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +00001044 envp->crc = crc32(0, envp->data,
1045 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001046#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -06001047 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001048#endif
1049 }
Simon Glass4d949a22017-08-03 12:22:10 -06001050 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001051
1052 return 0;
1053
1054sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001055 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1056 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001057 return 1;
1058}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001059#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001060
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001061#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001062/*
Quentin Schulz0312b942018-07-09 19:16:29 +02001063 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1064 * -d: delete existing environment before importing if no var is
1065 * passed; if vars are passed, if one var is in the current
1066 * environment but not in the environment at addr, delete var from
1067 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04001068 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001069 * -t: assume text format; either "size" must be given or the
1070 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +02001071 * -r: handle CRLF like LF, that means exported variables with
1072 * a content which ends with \r won't get imported. Used
1073 * to import text files created with editors which are using CRLF
1074 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001075 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1076 * -c: assume checksum protected environment format
1077 * addr: memory address to read from
1078 * size: length of input data; if missing, proper '\0'
1079 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +02001080 * if var is set and size should be missing (i.e. '\0'
1081 * termination), set size to '-'
1082 * var... List of the names of the only variables that get imported from
1083 * the environment at address 'addr'. Without arguments, the whole
1084 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001085 */
Simon Glassed38aef2020-05-10 11:40:03 -06001086static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1087 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001088{
Simon Glassa7459262013-10-25 23:01:31 -06001089 ulong addr;
1090 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001091 char sep = '\n';
1092 int chk = 0;
1093 int fmt = 0;
1094 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +02001095 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +02001096 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001097 size_t size;
1098
1099 cmd = *argv;
1100
1101 while (--argc > 0 && **++argv == '-') {
1102 char *arg = *argv;
1103 while (*++arg) {
1104 switch (*arg) {
1105 case 'b': /* raw binary format */
1106 if (fmt++)
1107 goto sep_err;
1108 sep = '\0';
1109 break;
1110 case 'c': /* external checksum format */
1111 if (fmt++)
1112 goto sep_err;
1113 sep = '\0';
1114 chk = 1;
1115 break;
1116 case 't': /* text format */
1117 if (fmt++)
1118 goto sep_err;
1119 sep = '\n';
1120 break;
Alexander Holler3e12be72014-07-14 17:49:55 +02001121 case 'r': /* handle CRLF like LF */
1122 crlf_is_lf = 1;
1123 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001124 case 'd':
1125 del = 1;
1126 break;
1127 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001128 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001129 }
1130 }
1131 }
1132
Macpaul Linff2a9f32011-04-26 16:16:45 +00001133 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001134 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001135
1136 if (!fmt)
1137 printf("## Warning: defaulting to text format\n");
1138
Alexander Holler3e12be72014-07-14 17:49:55 +02001139 if (sep != '\n' && crlf_is_lf )
1140 crlf_is_lf = 0;
1141
Simon Glassa7459262013-10-25 23:01:31 -06001142 addr = simple_strtoul(argv[0], NULL, 16);
1143 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001144
Quentin Schulz0312b942018-07-09 19:16:29 +02001145 if (argc >= 2 && strcmp(argv[1], "-")) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001146 size = simple_strtoul(argv[1], NULL, 16);
Quentin Schulz0312b942018-07-09 19:16:29 +02001147 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -05001148 puts("## Error: external checksum format must pass size\n");
1149 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001150 } else {
Simon Glassa7459262013-10-25 23:01:31 -06001151 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001152
1153 size = 0;
1154
1155 while (size < MAX_ENV_SIZE) {
1156 if ((*s == sep) && (*(s+1) == '\0'))
1157 break;
1158 ++s;
1159 ++size;
1160 }
1161 if (size == MAX_ENV_SIZE) {
1162 printf("## Warning: Input data exceeds %d bytes"
1163 " - truncated\n", MAX_ENV_SIZE);
1164 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001165 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001166 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001167 }
1168
Quentin Schulz0312b942018-07-09 19:16:29 +02001169 if (argc > 2)
1170 wl = 1;
1171
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001172 if (chk) {
1173 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -06001174 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001175
Pedro Aguilar834b5522020-08-31 11:01:41 +02001176 if (size <= offsetof(env_t, data)) {
1177 printf("## Error: Invalid size 0x%zX\n", size);
1178 return 1;
1179 }
1180
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001181 size -= offsetof(env_t, data);
1182 memcpy(&crc, &ep->crc, sizeof(crc));
1183
1184 if (crc32(0, ep->data, size) != crc) {
1185 puts("## Error: bad CRC, import failed\n");
1186 return 1;
1187 }
Simon Glassa7459262013-10-25 23:01:31 -06001188 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001189 }
1190
Quentin Schulz0312b942018-07-09 19:16:29 +02001191 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1192 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001193 pr_err("## Error: Environment import failed: errno = %d\n",
1194 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001195 return 1;
1196 }
1197 gd->flags |= GD_FLG_ENV_READY;
1198
1199 return 0;
1200
1201sep_err:
1202 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1203 cmd);
1204 return 1;
1205}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001206#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001207
Leo Ruan7450b872019-05-24 17:20:19 +02001208#if defined(CONFIG_CMD_NVEDIT_INFO)
1209/*
1210 * print_env_info - print environment information
1211 */
1212static int print_env_info(void)
1213{
1214 const char *value;
1215
1216 /* print environment validity value */
1217 switch (gd->env_valid) {
1218 case ENV_INVALID:
1219 value = "invalid";
1220 break;
1221 case ENV_VALID:
1222 value = "valid";
1223 break;
1224 case ENV_REDUND:
1225 value = "redundant";
1226 break;
1227 default:
1228 value = "unknown";
1229 break;
1230 }
1231 printf("env_valid = %s\n", value);
1232
1233 /* print environment ready flag */
1234 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1235 printf("env_ready = %s\n", value);
1236
1237 /* print environment using default flag */
1238 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1239 printf("env_use_default = %s\n", value);
1240
1241 return CMD_RET_SUCCESS;
1242}
1243
1244#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1245#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1246
1247/*
1248 * env info - display environment information
1249 * env info [-d] - evaluate whether default environment is used
1250 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001251 * Add [-q] - quiet mode, use only for command result, for test by example:
1252 * test env info -p -d -q
Leo Ruan7450b872019-05-24 17:20:19 +02001253 */
Simon Glassed38aef2020-05-10 11:40:03 -06001254static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1255 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +02001256{
1257 int eval_flags = 0;
1258 int eval_results = 0;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001259 bool quiet = false;
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001260#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1261 enum env_location loc;
1262#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001263
1264 /* display environment information */
1265 if (argc <= 1)
1266 return print_env_info();
1267
1268 /* process options */
1269 while (--argc > 0 && **++argv == '-') {
1270 char *arg = *argv;
1271
1272 while (*++arg) {
1273 switch (*arg) {
1274 case 'd':
1275 eval_flags |= ENV_INFO_IS_DEFAULT;
1276 break;
1277 case 'p':
1278 eval_flags |= ENV_INFO_IS_PERSISTED;
1279 break;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001280 case 'q':
1281 quiet = true;
1282 break;
Leo Ruan7450b872019-05-24 17:20:19 +02001283 default:
1284 return CMD_RET_USAGE;
1285 }
1286 }
1287 }
1288
1289 /* evaluate whether default environment is used */
1290 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1291 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001292 if (!quiet)
1293 printf("Default environment is used\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001294 eval_results |= ENV_INFO_IS_DEFAULT;
1295 } else {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001296 if (!quiet)
1297 printf("Environment was loaded from persistent storage\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001298 }
1299 }
1300
1301 /* evaluate whether environment can be persisted */
1302 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Patrick Delaunay4b876be2020-07-02 17:43:44 +02001303#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001304 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1305 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1306 if (!quiet)
1307 printf("Environment can be persisted\n");
1308 eval_results |= ENV_INFO_IS_PERSISTED;
1309 } else {
1310 if (!quiet)
1311 printf("Environment cannot be persisted\n");
1312 }
Leo Ruan7450b872019-05-24 17:20:19 +02001313#else
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001314 if (!quiet)
1315 printf("Environment cannot be persisted\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001316#endif
1317 }
1318
1319 /* The result of evaluations is combined with AND */
1320 if (eval_flags != eval_results)
1321 return CMD_RET_FAILURE;
1322
1323 return CMD_RET_SUCCESS;
1324}
1325#endif
1326
Andrew Ruder94463402013-10-22 19:07:34 -05001327#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001328static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1329 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001330{
Simon Glass1a236862019-08-02 09:44:18 -06001331 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001332
1333 if (argc < 2)
1334 return CMD_RET_USAGE;
1335
1336 e.key = argv[1];
1337 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001338 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001339
1340 return (ep == NULL) ? 1 : 0;
1341}
1342#endif
1343
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001344/*
1345 * New command line interface: "env" command with subcommands
1346 */
Simon Glassed38aef2020-05-10 11:40:03 -06001347static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001348#if defined(CONFIG_CMD_ASKENV)
1349 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1350#endif
1351 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001352 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001353#if defined(CONFIG_CMD_EDITENV)
1354 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1355#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001356#if defined(CONFIG_CMD_ENV_CALLBACK)
1357 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1358#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001359#if defined(CONFIG_CMD_ENV_FLAGS)
1360 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1361#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001362#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001363 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001364#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001365#if defined(CONFIG_CMD_GREPENV)
1366 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1367#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001368#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001369 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001370#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001371#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001372 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan7450b872019-05-24 17:20:19 +02001373#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001374#if defined(CONFIG_CMD_NVEDIT_LOAD)
1375 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1376#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001377 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1378#if defined(CONFIG_CMD_RUN)
1379 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1380#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001381#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001382 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001383#if defined(CONFIG_CMD_ERASEENV)
1384 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1385#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001386#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001387#if defined(CONFIG_CMD_NVEDIT_SELECT)
1388 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1389#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001390 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001391#if defined(CONFIG_CMD_ENV_EXISTS)
1392 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1393#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001394};
1395
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001396#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001397void env_reloc(void)
1398{
1399 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1400}
1401#endif
1402
Simon Glassed38aef2020-05-10 11:40:03 -06001403static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001404{
Simon Glassed38aef2020-05-10 11:40:03 -06001405 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001406
Thomas Weber4d282652010-11-24 13:07:52 +01001407 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001408 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001409
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001410 /* drop initial "env" arg */
1411 argc--;
1412 argv++;
1413
1414 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1415
1416 if (cp)
1417 return cp->cmd(cmdtp, flag, argc, argv);
1418
Simon Glassa06dfc72011-12-10 08:44:01 +00001419 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001420}
1421
Kim Phillipsdc00a682012-10-29 13:34:31 +00001422#ifdef CONFIG_SYS_LONGHELP
1423static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001424#if defined(CONFIG_CMD_ASKENV)
1425 "ask name [message] [size] - ask for environment variable\nenv "
1426#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001427#if defined(CONFIG_CMD_ENV_CALLBACK)
1428 "callbacks - print callbacks and their associated variables\nenv "
1429#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001430 "default [-f] -a - [forcibly] reset default environment\n"
1431 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001432 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001433#if defined(CONFIG_CMD_EDITENV)
1434 "env edit name - edit environment variable\n"
1435#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001436#if defined(CONFIG_CMD_ENV_EXISTS)
1437 "env exists name - tests for existence of variable\n"
1438#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001439#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001440 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001441#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001442#if defined(CONFIG_CMD_ENV_FLAGS)
1443 "env flags - print variables that have non-default flags\n"
1444#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001445#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001446#ifdef CONFIG_REGEX
1447 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1448#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001449 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001450#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001451#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001452#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001453 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001454#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001455#if defined(CONFIG_CMD_NVEDIT_INFO)
1456 "env info - display environment information\n"
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001457 "env info [-d] [-p] [-q] - evaluate environment information\n"
1458 " \"-d\": default environment is used\n"
1459 " \"-p\": environment can be persisted\n"
1460 " \"-q\": quiet output\n"
Leo Ruan7450b872019-05-24 17:20:19 +02001461#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001462 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001463#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001464 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001465#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001466#if defined(CONFIG_CMD_RUN)
1467 "env run var [...] - run commands in an environment variable\n"
1468#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001469#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001470 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001471#if defined(CONFIG_CMD_ERASEENV)
1472 "env erase - erase environment\n"
1473#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001474#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001475#if defined(CONFIG_CMD_NVEDIT_LOAD)
1476 "env load - load environment\n"
1477#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001478#if defined(CONFIG_CMD_NVEDIT_SELECT)
1479 "env select [target] - select environment target\n"
1480#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001481#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001482 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001483 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001484#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001485 "env set [-f] name [arg ...]\n";
1486#endif
1487
1488U_BOOT_CMD(
1489 env, CONFIG_SYS_MAXARGS, 1, do_env,
1490 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001491);
1492
1493/*
1494 * Old command line interface, kept for compatibility
1495 */
wdenk57b2d802003-06-27 21:31:46 +00001496
Peter Tyser0deafa22009-10-25 15:12:56 -05001497#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001498U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001499 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001500 "edit environment variable",
1501 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001502 " - edit environment variable 'name'",
1503 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001504);
1505#endif
1506
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001507U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001508 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001509 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001510 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001511#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001512 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001513 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001514 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001515 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001516#endif
wdenk57b2d802003-06-27 21:31:46 +00001517 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001518 " - print value of environment variable 'name'",
1519 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001520);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001521
1522#ifdef CONFIG_CMD_GREPENV
1523U_BOOT_CMD_COMPLETE(
1524 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1525 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001526#ifdef CONFIG_REGEX
1527 "[-e] [-n | -v | -b] string ...\n"
1528#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001529 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001530#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001531 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001532#ifdef CONFIG_REGEX
1533 " \"-e\": enable regular expressions;\n"
1534#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001535 " \"-n\": search variable names; \"-v\": search values;\n"
1536 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001537 var_complete
1538);
1539#endif
wdenk57b2d802003-06-27 21:31:46 +00001540
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001541U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001542 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001543 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001544#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001545 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001546 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001547 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001548 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001549 " \"-nv\": set non-volatile attribute\n"
1550 " \"-bs\": set boot-service attribute\n"
1551 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001552 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001553 " \"-a\": append-write\n"
1554 " \"-i addr,size\": use <addr,size> as variable's value\n"
1555 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001556 " - delete UEFI variable 'name' if 'value' not specified\n"
1557#endif
1558 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001559 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1560 "setenv [-f] name\n"
1561 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001562 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001563);
1564
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001565#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001566
wdenkf287a242003-07-01 21:06:45 +00001567U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001568 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001569 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001570 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001571 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001572);
Jon Loeligerd704d912007-07-10 11:02:44 -05001573#endif
wdenk57b2d802003-06-27 21:31:46 +00001574
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001575#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001576U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001577 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001578 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001579 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001580 " - run the commands in the environment variable(s) 'var'",
1581 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001582);
Jon Loeligerd704d912007-07-10 11:02:44 -05001583#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001584#endif /* CONFIG_SPL_BUILD */