blob: ddc715b4f917895cc381cc8ad537593ba6743c36 [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 Glass3ba929a2020-10-30 21:38:53 -060038#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060039#include <linux/bitops.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070040#include <u-boot/crc.h>
wdenk2a3cb022002-11-05 21:01:48 +000041#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000042#include <linux/stddef.h>
43#include <asm/byteorder.h>
Simon Glassa7459262013-10-25 23:01:31 -060044#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000045
Wolfgang Denk6405a152006-03-31 18:32:53 +020046DECLARE_GLOBAL_DATA_PTR;
47
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010048#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
49 defined(CONFIG_ENV_IS_IN_FLASH) || \
50 defined(CONFIG_ENV_IS_IN_MMC) || \
51 defined(CONFIG_ENV_IS_IN_FAT) || \
52 defined(CONFIG_ENV_IS_IN_EXT4) || \
53 defined(CONFIG_ENV_IS_IN_NAND) || \
54 defined(CONFIG_ENV_IS_IN_NVRAM) || \
55 defined(CONFIG_ENV_IS_IN_ONENAND) || \
56 defined(CONFIG_ENV_IS_IN_SATA) || \
57 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
58 defined(CONFIG_ENV_IS_IN_REMOTE) || \
59 defined(CONFIG_ENV_IS_IN_UBI)
60
61#define ENV_IS_IN_DEVICE
62
63#endif
64
65#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000066 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030067# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020068NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000069#endif
70
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071/*
72 * Maximum expected input data size for import command
73 */
74#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000075
wdenka68d3ed2002-10-11 08:38:32 +000076/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020077 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060078 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020079 * has changed or not. So it is possible to reread an environment
80 * variable only if the environment was changed ... done so for
81 * example in NetInitLoop()
82 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010083static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000084
Simon Glass2d85a752019-08-01 09:46:41 -060085int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010086{
87 return env_id;
88}
wdenka68d3ed2002-10-11 08:38:32 +000089
Ilya Yanokf7a2c552012-09-18 00:22:50 +000090#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040091/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020092 * Command interface: print one or all environment variables
93 *
94 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040095 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060096static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000097{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000099 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +0000100
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -0600102 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000103
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200104 e.key = name;
105 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600106 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200107 if (ep == NULL)
108 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000109 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 return len;
111 }
wdenka68d3ed2002-10-11 08:38:32 +0000112
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200113 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600114 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 if (len > 0) {
117 puts(res);
118 free(res);
119 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000120 }
121
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200122 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000123 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200124 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400125}
wdenka68d3ed2002-10-11 08:38:32 +0000126
Simon Glassed38aef2020-05-10 11:40:03 -0600127static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
128 char *const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400129{
130 int i;
131 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600132 int env_flag = H_HIDE_DOT;
133
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900134#if defined(CONFIG_CMD_NVEDIT_EFI)
135 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
136 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
137#endif
138
Joe Hershberger79a905e2012-12-11 22:16:23 -0600139 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
140 argc--;
141 argv++;
142 env_flag &= ~H_HIDE_DOT;
143 }
wdenka68d3ed2002-10-11 08:38:32 +0000144
Mike Frysinger87944922009-05-24 02:26:19 -0400145 if (argc == 1) {
146 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600147 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200148 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400149 return 1;
150 printf("\nEnvironment size: %d/%ld bytes\n",
151 rcode, (ulong)ENV_SIZE);
152 return 0;
153 }
wdenka68d3ed2002-10-11 08:38:32 +0000154
Mike Frysinger87944922009-05-24 02:26:19 -0400155 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600156 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400157 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600158 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200159 if (!rc) {
160 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400161 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000162 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000163 }
164
165 return rcode;
166}
167
168#ifdef CONFIG_CMD_GREPENV
Simon Glassed38aef2020-05-10 11:40:03 -0600169static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
170 int argc, char *const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000171{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000172 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000173 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000174
175 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000176 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000177
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000178 grep_how = H_MATCH_SUBSTR; /* default: substring search */
179 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000180
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200181 while (--argc > 0 && **++argv == '-') {
182 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000183 while (*++arg) {
184 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000185#ifdef CONFIG_REGEX
186 case 'e': /* use regex matching */
187 grep_how = H_MATCH_REGEX;
188 break;
189#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000190 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000191 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000192 break;
193 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000194 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000195 break;
196 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000197 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000198 break;
199 case '-':
200 goto DONE;
201 default:
202 return CMD_RET_USAGE;
203 }
204 }
205 }
206
207DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000208 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000209 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000210 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000211
Wolfgang Denke27fc122013-03-23 23:50:29 +0000212 if (len > 0) {
213 puts(res);
214 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000215 }
Mike Frysinger87944922009-05-24 02:26:19 -0400216
Wolfgang Denke27fc122013-03-23 23:50:29 +0000217 if (len < 2)
218 return 1;
219
220 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000221}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000222#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000223#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000224
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200225/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000226 * Set a new environment variable,
227 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600228 */
Simon Glassed38aef2020-05-10 11:40:03 -0600229static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000230{
231 int i, len;
232 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600233 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000234
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600235 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900236
237#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
238 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
239 return do_env_set_efi(NULL, flag, --argc, ++argv);
240#endif
241
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600242 while (argc > 1 && **(argv + 1) == '-') {
243 char *arg = *++argv;
244
245 --argc;
246 while (*++arg) {
247 switch (*arg) {
248 case 'f': /* force */
249 env_flag |= H_FORCE;
250 break;
251 default:
252 return CMD_RET_USAGE;
253 }
254 }
255 }
256 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000257 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000258
259 if (strchr(name, '=')) {
260 printf("## Error: illegal character '='"
261 "in variable name \"%s\"\n", name);
262 return 1;
263 }
264
265 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000266
wdenka68d3ed2002-10-11 08:38:32 +0000267 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000268 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600269 int rc = hdelete_r(name, &env_htab, env_flag);
Simon Glass783ef3f2020-11-05 10:33:37 -0700270
271 /* If the variable didn't exist, don't report an error */
272 return rc && rc != -ENOENT ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000273 }
274
275 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200276 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000277 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000278 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000279 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000280
281 value = malloc(len);
282 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200283 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000284 return 1;
285 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000286 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200287 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000288
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200289 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000290 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000291 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000292 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200293 if (s != value)
294 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000295
Igor Grinbergde832562011-11-07 01:13:59 +0000296 e.key = name;
297 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600298 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200299 free(value);
300 if (!ep) {
301 printf("## Error inserting \"%s\" variable, errno=%d\n",
302 name, errno);
303 return 1;
304 }
wdenka68d3ed2002-10-11 08:38:32 +0000305
wdenka68d3ed2002-10-11 08:38:32 +0000306 return 0;
307}
308
Simon Glass6a38e412017-08-03 12:22:09 -0600309int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000310{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200311 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
312
Joe Hershberger3902c602013-04-08 10:32:50 +0000313 /* before import into hashtable */
314 if (!(gd->flags & GD_FLG_ENV_READY))
315 return 1;
316
Igor Grinbergde832562011-11-07 01:13:59 +0000317 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500318 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200319 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500320 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000321}
322
Simon Glassb8e926e2011-10-24 17:59:59 +0000323/**
324 * Set an environment variable to an integer value
325 *
Simon Glassdf372d62013-05-07 06:11:45 +0000326 * @param varname Environment variable to set
Simon Glassb8e926e2011-10-24 17:59:59 +0000327 * @param value Value to set it to
328 * @return 0 if ok, 1 on error
329 */
Simon Glass4d949a22017-08-03 12:22:10 -0600330int env_set_ulong(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000331{
332 /* TODO: this should be unsigned */
333 char *str = simple_itoa(value);
334
Simon Glass6a38e412017-08-03 12:22:09 -0600335 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000336}
337
338/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000339 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000340 *
Simon Glassdf372d62013-05-07 06:11:45 +0000341 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000342 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000343 * @return 0 if ok, 1 on error
344 */
Simon Glass4d949a22017-08-03 12:22:10 -0600345int env_set_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000346{
347 char str[17];
348
Simon Glass044dc9e2013-02-24 17:33:21 +0000349 sprintf(str, "%lx", value);
Simon Glass6a38e412017-08-03 12:22:09 -0600350 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000351}
352
Simon Glass22c34c22017-08-03 12:22:13 -0600353ulong env_get_hex(const char *varname, ulong default_val)
Simon Glassabbd5122013-04-20 08:42:43 +0000354{
355 const char *s;
356 ulong value;
357 char *endp;
358
Simon Glass64b723f2017-08-03 12:22:12 -0600359 s = env_get(varname);
Simon Glassabbd5122013-04-20 08:42:43 +0000360 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600361 value = hextoul(s, &endp);
Simon Glassabbd5122013-04-20 08:42:43 +0000362 if (!s || endp == s)
363 return default_val;
364
365 return value;
366}
367
Alex Kiernan9c215492018-04-01 09:22:38 +0000368int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
369{
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500370 string_to_enetaddr(env_get(name), enetaddr);
Alex Kiernan9c215492018-04-01 09:22:38 +0000371 return is_valid_ethaddr(enetaddr);
372}
373
374int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
375{
376 char buf[ARP_HLEN_ASCII + 1];
377
378 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
379 return -EEXIST;
380
381 sprintf(buf, "%pM", enetaddr);
382
383 return env_set(name, buf);
384}
385
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000386#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600387static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
388 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000389{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200390 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000391 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000392
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500393 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000394}
395
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200396/*
wdenka68d3ed2002-10-11 08:38:32 +0000397 * Prompt for environment variable
398 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500399#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600400int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000401{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200402 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000403 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000404 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000405 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000406
407 local_args[0] = argv[0];
408 local_args[1] = argv[1];
409 local_args[2] = NULL;
410 local_args[3] = NULL;
411
Wolfgang Denk586895d2013-02-20 04:53:16 +0000412 /*
413 * Check the syntax:
414 *
415 * env_ask envname [message1 ...] [size]
416 */
417 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000418 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000419
Wolfgang Denk586895d2013-02-20 04:53:16 +0000420 /*
421 * We test the last argument if it can be converted
422 * into a decimal number. If yes, we assume it's
423 * the size. Otherwise we echo it as part of the
424 * message.
425 */
Simon Glassff9b9032021-07-24 09:03:30 -0600426 i = dectoul(argv[argc - 1], &endptr);
Wolfgang Denk586895d2013-02-20 04:53:16 +0000427 if (*endptr != '\0') { /* no size */
428 size = CONFIG_SYS_CBSIZE - 1;
429 } else { /* size given */
430 size = i;
431 --argc;
432 }
wdenka68d3ed2002-10-11 08:38:32 +0000433
Wolfgang Denk586895d2013-02-20 04:53:16 +0000434 if (argc <= 2) {
435 sprintf(message, "Please enter '%s': ", argv[1]);
436 } else {
437 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400438 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000439 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000440 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000441
Tom Rini4beb2352017-09-26 19:37:11 -0400442 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000443 pos += strlen(argv[i]);
444 }
Tom Rini4beb2352017-09-26 19:37:11 -0400445 if (pos < sizeof(message) - 1) {
446 message[pos++] = ' ';
447 message[pos] = '\0';
448 } else
449 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000450 }
451
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200452 if (size >= CONFIG_SYS_CBSIZE)
453 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000454
455 if (size <= 0)
456 return 1;
457
458 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600459 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000460
461 if (size < len)
462 console_buffer[size] = '\0';
463
464 len = 2;
465 if (console_buffer[0] != '\0') {
466 local_args[2] = console_buffer;
467 len = 3;
468 }
469
470 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500471 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000472}
Jon Loeligerd704d912007-07-10 11:02:44 -0500473#endif
wdenka68d3ed2002-10-11 08:38:32 +0000474
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600475#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500476static int print_static_binding(const char *var_name, const char *callback_name,
477 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600478{
479 printf("\t%-20s %-20s\n", var_name, callback_name);
480
481 return 0;
482}
483
Simon Glass1a236862019-08-02 09:44:18 -0600484static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600485{
486 struct env_clbk_tbl *clbkp;
487 int i;
488 int num_callbacks;
489
490 if (entry->callback == NULL)
491 return 0;
492
493 /* look up the callback in the linker-list */
494 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
495 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
496 i < num_callbacks;
497 i++, clbkp++) {
498#if defined(CONFIG_NEEDS_MANUAL_RELOC)
499 if (entry->callback == clbkp->callback + gd->reloc_off)
500#else
501 if (entry->callback == clbkp->callback)
502#endif
503 break;
504 }
505
506 if (i == num_callbacks)
507 /* this should probably never happen, but just in case... */
508 printf("\t%-20s %p\n", entry->key, entry->callback);
509 else
510 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
511
512 return 0;
513}
514
515/*
516 * Print the callbacks available and what they are bound to
517 */
Simon Glassed38aef2020-05-10 11:40:03 -0600518int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
519 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600520{
521 struct env_clbk_tbl *clbkp;
522 int i;
523 int num_callbacks;
524
525 /* Print the available callbacks */
526 puts("Available callbacks:\n");
527 puts("\tCallback Name\n");
528 puts("\t-------------\n");
529 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
530 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
531 i < num_callbacks;
532 i++, clbkp++)
533 printf("\t%s\n", clbkp->name);
534 puts("\n");
535
536 /* Print the static bindings that may exist */
537 puts("Static callback bindings:\n");
538 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
539 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500540 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600541 puts("\n");
542
543 /* walk through each variable and print the callback if it has one */
544 puts("Active callback bindings:\n");
545 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
546 printf("\t%-20s %-20s\n", "-------------", "-------------");
547 hwalk_r(&env_htab, print_active_callback);
548 return 0;
549}
550#endif
551
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600552#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500553static int print_static_flags(const char *var_name, const char *flags,
554 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600555{
556 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600557 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600558
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600559 printf("\t%-20s %-20s %-20s\n", var_name,
560 env_flags_get_vartype_name(type),
561 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600562
563 return 0;
564}
565
Simon Glass1a236862019-08-02 09:44:18 -0600566static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600567{
568 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600569 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600570
571 if (entry->flags == 0)
572 return 0;
573
574 type = (enum env_flags_vartype)
575 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600576 access = env_flags_parse_varaccess_from_binflags(entry->flags);
577 printf("\t%-20s %-20s %-20s\n", entry->key,
578 env_flags_get_vartype_name(type),
579 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600580
581 return 0;
582}
583
584/*
585 * Print the flags available and what variables have flags
586 */
Simon Glassed38aef2020-05-10 11:40:03 -0600587int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600588{
589 /* Print the available variable types */
590 printf("Available variable type flags (position %d):\n",
591 ENV_FLAGS_VARTYPE_LOC);
592 puts("\tFlag\tVariable Type Name\n");
593 puts("\t----\t------------------\n");
594 env_flags_print_vartypes();
595 puts("\n");
596
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600597 /* Print the available variable access types */
598 printf("Available variable access flags (position %d):\n",
599 ENV_FLAGS_VARACCESS_LOC);
600 puts("\tFlag\tVariable Access Name\n");
601 puts("\t----\t--------------------\n");
602 env_flags_print_varaccess();
603 puts("\n");
604
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600605 /* Print the static flags that may exist */
606 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600607 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
608 "Variable Access");
609 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
610 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500611 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600612 puts("\n");
613
614 /* walk through each variable and print the flags if non-default */
615 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600616 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
617 "Variable Access");
618 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
619 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600620 hwalk_r(&env_htab, print_active_flags);
621 return 0;
622}
623#endif
624
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200625/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500626 * Interactively edit an environment variable
627 */
628#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600629static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
630 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500631{
632 char buffer[CONFIG_SYS_CBSIZE];
633 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500634
Wolfgang Denk3b683112010-07-17 01:06:04 +0200635 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000636 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500637
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500638 /* before import into hashtable */
639 if (!(gd->flags & GD_FLG_ENV_READY))
640 return 1;
641
Peter Tyser0deafa22009-10-25 15:12:56 -0500642 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600643 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500644 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800645 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500646 else
647 buffer[0] = '\0';
648
Simon Glassbe6aafc2014-04-10 20:01:27 -0600649 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000650 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500651
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500652 if (buffer[0] == '\0') {
653 const char * const _argv[3] = { "setenv", argv[1], NULL };
654
655 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
656 } else {
657 const char * const _argv[4] = { "setenv", argv[1], buffer,
658 NULL };
659
660 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
661 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500662}
663#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000664#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500665
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200666/*
wdenka68d3ed2002-10-11 08:38:32 +0000667 * Look up variable from environment,
668 * return address of storage for that variable,
669 * or NULL if not found
670 */
Simon Glass64b723f2017-08-03 12:22:12 -0600671char *env_get(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000672{
Igor Grinbergde832562011-11-07 01:13:59 +0000673 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Simon Glass1a236862019-08-02 09:44:18 -0600674 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000675
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200676 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000677
Igor Grinbergde832562011-11-07 01:13:59 +0000678 e.key = name;
679 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600680 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200681
Macpaul Linff2a9f32011-04-26 16:16:45 +0000682 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000683 }
684
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200685 /* restricted capabilities before import */
Simon Glass64b723f2017-08-03 12:22:12 -0600686 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200687 return (char *)(gd->env_buf);
688
689 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000690}
691
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200692/*
Patrice Chotardceed4b42019-11-25 09:07:36 +0100693 * Like env_get, but prints an error if envvar isn't defined in the
694 * environment. It always returns what env_get does, so it can be used in
695 * place of env_get without changing error handling otherwise.
696 */
697char *from_env(const char *envvar)
698{
699 char *ret;
700
701 ret = env_get(envvar);
702
703 if (!ret)
704 printf("missing environment variable: %s\n", envvar);
705
706 return ret;
707}
708
709/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200710 * Look up variable from environment for restricted C runtime env.
711 */
Simon Glass64b723f2017-08-03 12:22:12 -0600712int env_get_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000713{
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100714 int i, nxt, c;
wdenka68d3ed2002-10-11 08:38:32 +0000715
Igor Grinbergde832562011-11-07 01:13:59 +0000716 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000717 int val, n;
718
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100719 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
720 if (c < 0)
721 return c;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000722 if (nxt >= CONFIG_ENV_SIZE)
723 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000724 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000725
Simon Glass909ff4d2019-08-01 09:46:45 -0600726 val = env_match((uchar *)name, i);
Macpaul Linff2a9f32011-04-26 16:16:45 +0000727 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000728 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200729
wdenka68d3ed2002-10-11 08:38:32 +0000730 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000731 for (n = 0; n < len; ++n, ++buf) {
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100732 c = env_get_char(val++);
733 if (c < 0)
734 return c;
735 *buf = c;
Igor Grinbergde832562011-11-07 01:13:59 +0000736 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200737 return n;
738 }
739
740 if (n)
741 *--buf = '\0';
742
Heinrich Schuchardtcf80bd72019-01-06 11:52:06 +0100743 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
744 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200745
746 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000747 }
Igor Grinbergde832562011-11-07 01:13:59 +0000748
Macpaul Linff2a9f32011-04-26 16:16:45 +0000749 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000750}
751
Simon Glassbf690f82011-10-14 13:25:18 +0000752/**
753 * Decode the integer value of an environment variable and return it.
754 *
Shyam Sainiefe757c2018-06-07 19:47:19 +0530755 * @param name Name of environment variable
Simon Glassbf690f82011-10-14 13:25:18 +0000756 * @param base Number base to use (normally 10, or 16 for hex)
757 * @param default_val Default value to return if the variable is not
758 * found
759 * @return the decoded value, or default_val if not found
760 */
Simon Glass22c34c22017-08-03 12:22:13 -0600761ulong env_get_ulong(const char *name, int base, ulong default_val)
Simon Glassbf690f82011-10-14 13:25:18 +0000762{
763 /*
Simon Glass64b723f2017-08-03 12:22:12 -0600764 * We can use env_get() here, even before relocation, since the
Simon Glassbf690f82011-10-14 13:25:18 +0000765 * environment variable value is an integer and thus short.
766 */
Simon Glass64b723f2017-08-03 12:22:12 -0600767 const char *str = env_get(name);
Simon Glassbf690f82011-10-14 13:25:18 +0000768
769 return str ? simple_strtoul(str, NULL, base) : default_val;
770}
771
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000772#ifndef CONFIG_SPL_BUILD
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100773#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Simon Glassed38aef2020-05-10 11:40:03 -0600774static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
775 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000776{
Simon Glassd49b8892017-08-03 12:22:08 -0600777 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000778}
wdenk57b2d802003-06-27 21:31:46 +0000779
Mike Frysinger61583932008-12-30 02:59:25 -0500780U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200781 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600782 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200783 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500784);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200785
786#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600787static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
788 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200789{
790 return env_erase() ? 1 : 0;
791}
792
793U_BOOT_CMD(
794 eraseenv, 1, 0, do_env_erase,
795 "erase environment variables from persistent storage",
796 ""
797);
798#endif
wdenka68d3ed2002-10-11 08:38:32 +0000799#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200800
801#if defined(CONFIG_CMD_NVEDIT_LOAD)
802static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
803 char *const argv[])
804{
805 return env_reload() ? 1 : 0;
806}
807#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200808
809#if defined(CONFIG_CMD_NVEDIT_SELECT)
810static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
811 char *const argv[])
812{
813 return env_select(argv[1]) ? 1 : 0;
814}
815#endif
816
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000817#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000818
Simon Glass909ff4d2019-08-01 09:46:45 -0600819int env_match(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000820{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000821 if (s1 == NULL)
822 return -1;
823
wdenka68d3ed2002-10-11 08:38:32 +0000824 while (*s1 == env_get_char(i2++))
825 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000826 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000827
wdenka68d3ed2002-10-11 08:38:32 +0000828 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000829 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000830
Macpaul Linff2a9f32011-04-26 16:16:45 +0000831 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000832}
wdenk57b2d802003-06-27 21:31:46 +0000833
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000834#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600835static int do_env_default(struct cmd_tbl *cmdtp, int flag,
836 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200837{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300838 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000839
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000840 debug("Initial value for argc=%d\n", argc);
841 while (--argc > 0 && **++argv == '-') {
842 char *arg = *argv;
843
844 while (*++arg) {
845 switch (*arg) {
846 case 'a': /* default all */
847 all = 1;
848 break;
849 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300850 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000851 break;
852 default:
853 return cmd_usage(cmdtp);
854 }
855 }
856 }
857 debug("Final value for argc=%d\n", argc);
858 if (all && (argc == 0)) {
859 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600860 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300861 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000862 return 0;
863 }
864 if (!all && (argc > 0)) {
865 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600866 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000867 return 0;
868 }
869
870 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200871}
872
Simon Glassed38aef2020-05-10 11:40:03 -0600873static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
874 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200875{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600876 int env_flag = H_INTERACTIVE;
877 int ret = 0;
878
879 debug("Initial value for argc=%d\n", argc);
880 while (argc > 1 && **(argv + 1) == '-') {
881 char *arg = *++argv;
882
883 --argc;
884 while (*++arg) {
885 switch (*arg) {
886 case 'f': /* force */
887 env_flag |= H_FORCE;
888 break;
889 default:
890 return CMD_RET_USAGE;
891 }
892 }
893 }
894 debug("Final value for argc=%d\n", argc);
895
896 env_id++;
897
898 while (--argc > 0) {
899 char *name = *++argv;
900
Simon Glass783ef3f2020-11-05 10:33:37 -0700901 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger8335ce72012-12-11 22:16:36 -0600902 ret = 1;
903 }
904
905 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200906}
907
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500908#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200909/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100910 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200911 * -t: export as text format; if size is given, data will be
912 * padded with '\0' bytes; if not, one terminating '\0'
913 * will be added (which is included in the "filesize"
914 * setting so you can for exmple copy this to flash and
915 * keep the termination).
916 * -b: export as binary format (name=value pairs separated by
917 * '\0', list end marked by double "\0\0")
918 * -c: export as checksum protected environment format as
919 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100920 * -s size:
921 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200922 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100923 * var... List of variable names that get included into the
924 * export. Without arguments, the whole environment gets
925 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200926 *
927 * With "-c" and size is NOT given, then the export command will
928 * format the data as currently used for the persistent storage,
929 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400930 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200931 * environment, a "current" redundancy flag. If size is given, this
932 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
933 * checksum and redundancy flag will be inserted.
934 *
935 * With "-b" and "-t", always only the real data (including a
936 * terminating '\0' byte) will be written; here the optional size
937 * argument will be used to make sure not to overflow the user
938 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400939 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200940 *
941 * On successful return, the variable "filesize" will be set.
942 * Note that filesize includes the trailing/terminating '\0' byte(s).
943 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400944 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200945 *
946 * => env export -t 100000
947 * => era ${backup_addr} +${filesize}
948 * => cp.b 100000 ${backup_addr} ${filesize}
949 *
950 * Re-import this snapshot, deleting all other settings:
951 *
952 * => env import -d -t ${backup_addr}
953 */
Simon Glassed38aef2020-05-10 11:40:03 -0600954static int do_env_export(struct cmd_tbl *cmdtp, int flag,
955 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200956{
957 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600958 ulong addr;
959 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100960 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200961 ssize_t len;
962 env_t *envp;
963 char sep = '\n';
964 int chk = 0;
965 int fmt = 0;
966
967 cmd = *argv;
968
969 while (--argc > 0 && **++argv == '-') {
970 char *arg = *argv;
971 while (*++arg) {
972 switch (*arg) {
973 case 'b': /* raw binary format */
974 if (fmt++)
975 goto sep_err;
976 sep = '\0';
977 break;
978 case 'c': /* external checksum format */
979 if (fmt++)
980 goto sep_err;
981 sep = '\0';
982 chk = 1;
983 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100984 case 's': /* size given */
985 if (--argc <= 0)
986 return cmd_usage(cmdtp);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600987 size = hextoul(*++argv, NULL);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100988 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200989 case 't': /* text format */
990 if (fmt++)
991 goto sep_err;
992 sep = '\n';
993 break;
994 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000995 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200996 }
997 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100998NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200999 }
1000
Macpaul Linff2a9f32011-04-26 16:16:45 +00001001 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001002 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +00001003
Simon Glass3ff49ec2021-07-24 09:03:29 -06001004 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -06001005 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001006
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001007 if (size)
Simon Glassa7459262013-10-25 23:01:31 -06001008 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001009
1010 argc--;
1011 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001012
1013 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +00001014 len = hexport_r(&env_htab, sep,
1015 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -06001016 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001017 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001018 pr_err("## Error: Cannot export environment: errno = %d\n",
1019 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001020 return 1;
1021 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +01001022 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -06001023 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001024
1025 return 0;
1026 }
1027
Simon Glassa7459262013-10-25 23:01:31 -06001028 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001029
1030 if (chk) /* export as checksum protected block */
1031 res = (char *)envp->data;
1032 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -06001033 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001034
Wolfgang Denk565af272013-03-23 23:50:28 +00001035 len = hexport_r(&env_htab, '\0',
1036 H_MATCH_KEY | H_MATCH_IDENT,
1037 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001038 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001039 pr_err("## Error: Cannot export environment: errno = %d\n",
1040 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001041 return 1;
1042 }
1043
1044 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +00001045 envp->crc = crc32(0, envp->data,
1046 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001047#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -06001048 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001049#endif
1050 }
Simon Glass4d949a22017-08-03 12:22:10 -06001051 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001052
1053 return 0;
1054
1055sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001056 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1057 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001058 return 1;
1059}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001060#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001061
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001062#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001063/*
Quentin Schulz0312b942018-07-09 19:16:29 +02001064 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1065 * -d: delete existing environment before importing if no var is
1066 * passed; if vars are passed, if one var is in the current
1067 * environment but not in the environment at addr, delete var from
1068 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04001069 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001070 * -t: assume text format; either "size" must be given or the
1071 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +02001072 * -r: handle CRLF like LF, that means exported variables with
1073 * a content which ends with \r won't get imported. Used
1074 * to import text files created with editors which are using CRLF
1075 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001076 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1077 * -c: assume checksum protected environment format
1078 * addr: memory address to read from
1079 * size: length of input data; if missing, proper '\0'
1080 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +02001081 * if var is set and size should be missing (i.e. '\0'
1082 * termination), set size to '-'
1083 * var... List of the names of the only variables that get imported from
1084 * the environment at address 'addr'. Without arguments, the whole
1085 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001086 */
Simon Glassed38aef2020-05-10 11:40:03 -06001087static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1088 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001089{
Simon Glassa7459262013-10-25 23:01:31 -06001090 ulong addr;
1091 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001092 char sep = '\n';
1093 int chk = 0;
1094 int fmt = 0;
1095 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +02001096 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +02001097 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001098 size_t size;
1099
1100 cmd = *argv;
1101
1102 while (--argc > 0 && **++argv == '-') {
1103 char *arg = *argv;
1104 while (*++arg) {
1105 switch (*arg) {
1106 case 'b': /* raw binary format */
1107 if (fmt++)
1108 goto sep_err;
1109 sep = '\0';
1110 break;
1111 case 'c': /* external checksum format */
1112 if (fmt++)
1113 goto sep_err;
1114 sep = '\0';
1115 chk = 1;
1116 break;
1117 case 't': /* text format */
1118 if (fmt++)
1119 goto sep_err;
1120 sep = '\n';
1121 break;
Alexander Holler3e12be72014-07-14 17:49:55 +02001122 case 'r': /* handle CRLF like LF */
1123 crlf_is_lf = 1;
1124 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001125 case 'd':
1126 del = 1;
1127 break;
1128 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001129 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001130 }
1131 }
1132 }
1133
Macpaul Linff2a9f32011-04-26 16:16:45 +00001134 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001135 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001136
1137 if (!fmt)
1138 printf("## Warning: defaulting to text format\n");
1139
Alexander Holler3e12be72014-07-14 17:49:55 +02001140 if (sep != '\n' && crlf_is_lf )
1141 crlf_is_lf = 0;
1142
Simon Glass3ff49ec2021-07-24 09:03:29 -06001143 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -06001144 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001145
Quentin Schulz0312b942018-07-09 19:16:29 +02001146 if (argc >= 2 && strcmp(argv[1], "-")) {
Simon Glass3ff49ec2021-07-24 09:03:29 -06001147 size = hextoul(argv[1], NULL);
Quentin Schulz0312b942018-07-09 19:16:29 +02001148 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -05001149 puts("## Error: external checksum format must pass size\n");
1150 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001151 } else {
Simon Glassa7459262013-10-25 23:01:31 -06001152 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001153
1154 size = 0;
1155
1156 while (size < MAX_ENV_SIZE) {
1157 if ((*s == sep) && (*(s+1) == '\0'))
1158 break;
1159 ++s;
1160 ++size;
1161 }
1162 if (size == MAX_ENV_SIZE) {
1163 printf("## Warning: Input data exceeds %d bytes"
1164 " - truncated\n", MAX_ENV_SIZE);
1165 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001166 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001167 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001168 }
1169
Quentin Schulz0312b942018-07-09 19:16:29 +02001170 if (argc > 2)
1171 wl = 1;
1172
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001173 if (chk) {
1174 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -06001175 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001176
Pedro Aguilar834b5522020-08-31 11:01:41 +02001177 if (size <= offsetof(env_t, data)) {
1178 printf("## Error: Invalid size 0x%zX\n", size);
1179 return 1;
1180 }
1181
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001182 size -= offsetof(env_t, data);
1183 memcpy(&crc, &ep->crc, sizeof(crc));
1184
1185 if (crc32(0, ep->data, size) != crc) {
1186 puts("## Error: bad CRC, import failed\n");
1187 return 1;
1188 }
Simon Glassa7459262013-10-25 23:01:31 -06001189 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001190 }
1191
Quentin Schulz0312b942018-07-09 19:16:29 +02001192 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1193 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001194 pr_err("## Error: Environment import failed: errno = %d\n",
1195 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001196 return 1;
1197 }
1198 gd->flags |= GD_FLG_ENV_READY;
1199
1200 return 0;
1201
1202sep_err:
1203 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1204 cmd);
1205 return 1;
1206}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001207#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001208
Leo Ruan7450b872019-05-24 17:20:19 +02001209#if defined(CONFIG_CMD_NVEDIT_INFO)
1210/*
1211 * print_env_info - print environment information
1212 */
1213static int print_env_info(void)
1214{
1215 const char *value;
1216
1217 /* print environment validity value */
1218 switch (gd->env_valid) {
1219 case ENV_INVALID:
1220 value = "invalid";
1221 break;
1222 case ENV_VALID:
1223 value = "valid";
1224 break;
1225 case ENV_REDUND:
1226 value = "redundant";
1227 break;
1228 default:
1229 value = "unknown";
1230 break;
1231 }
1232 printf("env_valid = %s\n", value);
1233
1234 /* print environment ready flag */
1235 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1236 printf("env_ready = %s\n", value);
1237
1238 /* print environment using default flag */
1239 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1240 printf("env_use_default = %s\n", value);
1241
1242 return CMD_RET_SUCCESS;
1243}
1244
1245#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1246#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1247
1248/*
1249 * env info - display environment information
1250 * env info [-d] - evaluate whether default environment is used
1251 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001252 * Add [-q] - quiet mode, use only for command result, for test by example:
1253 * test env info -p -d -q
Leo Ruan7450b872019-05-24 17:20:19 +02001254 */
Simon Glassed38aef2020-05-10 11:40:03 -06001255static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1256 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +02001257{
1258 int eval_flags = 0;
1259 int eval_results = 0;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001260 bool quiet = false;
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001261#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1262 enum env_location loc;
1263#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001264
1265 /* display environment information */
1266 if (argc <= 1)
1267 return print_env_info();
1268
1269 /* process options */
1270 while (--argc > 0 && **++argv == '-') {
1271 char *arg = *argv;
1272
1273 while (*++arg) {
1274 switch (*arg) {
1275 case 'd':
1276 eval_flags |= ENV_INFO_IS_DEFAULT;
1277 break;
1278 case 'p':
1279 eval_flags |= ENV_INFO_IS_PERSISTED;
1280 break;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001281 case 'q':
1282 quiet = true;
1283 break;
Leo Ruan7450b872019-05-24 17:20:19 +02001284 default:
1285 return CMD_RET_USAGE;
1286 }
1287 }
1288 }
1289
1290 /* evaluate whether default environment is used */
1291 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1292 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001293 if (!quiet)
1294 printf("Default environment is used\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001295 eval_results |= ENV_INFO_IS_DEFAULT;
1296 } else {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001297 if (!quiet)
1298 printf("Environment was loaded from persistent storage\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001299 }
1300 }
1301
1302 /* evaluate whether environment can be persisted */
1303 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Patrick Delaunay4b876be2020-07-02 17:43:44 +02001304#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001305 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1306 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1307 if (!quiet)
1308 printf("Environment can be persisted\n");
1309 eval_results |= ENV_INFO_IS_PERSISTED;
1310 } else {
1311 if (!quiet)
1312 printf("Environment cannot be persisted\n");
1313 }
Leo Ruan7450b872019-05-24 17:20:19 +02001314#else
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001315 if (!quiet)
1316 printf("Environment cannot be persisted\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001317#endif
1318 }
1319
1320 /* The result of evaluations is combined with AND */
1321 if (eval_flags != eval_results)
1322 return CMD_RET_FAILURE;
1323
1324 return CMD_RET_SUCCESS;
1325}
1326#endif
1327
Andrew Ruder94463402013-10-22 19:07:34 -05001328#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001329static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1330 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001331{
Simon Glass1a236862019-08-02 09:44:18 -06001332 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001333
1334 if (argc < 2)
1335 return CMD_RET_USAGE;
1336
1337 e.key = argv[1];
1338 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001339 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001340
1341 return (ep == NULL) ? 1 : 0;
1342}
1343#endif
1344
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001345/*
1346 * New command line interface: "env" command with subcommands
1347 */
Simon Glassed38aef2020-05-10 11:40:03 -06001348static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001349#if defined(CONFIG_CMD_ASKENV)
1350 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1351#endif
1352 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001353 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001354#if defined(CONFIG_CMD_EDITENV)
1355 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1356#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001357#if defined(CONFIG_CMD_ENV_CALLBACK)
1358 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1359#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001360#if defined(CONFIG_CMD_ENV_FLAGS)
1361 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1362#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001363#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001364 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001365#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001366#if defined(CONFIG_CMD_GREPENV)
1367 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1368#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001369#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001370 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001371#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001372#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001373 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan7450b872019-05-24 17:20:19 +02001374#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001375#if defined(CONFIG_CMD_NVEDIT_LOAD)
1376 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1377#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001378 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1379#if defined(CONFIG_CMD_RUN)
1380 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1381#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001382#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001383 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001384#if defined(CONFIG_CMD_ERASEENV)
1385 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1386#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001387#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001388#if defined(CONFIG_CMD_NVEDIT_SELECT)
1389 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1390#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001391 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001392#if defined(CONFIG_CMD_ENV_EXISTS)
1393 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1394#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001395};
1396
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001397#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001398void env_reloc(void)
1399{
1400 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1401}
1402#endif
1403
Simon Glassed38aef2020-05-10 11:40:03 -06001404static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001405{
Simon Glassed38aef2020-05-10 11:40:03 -06001406 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001407
Thomas Weber4d282652010-11-24 13:07:52 +01001408 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001409 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001410
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001411 /* drop initial "env" arg */
1412 argc--;
1413 argv++;
1414
1415 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1416
1417 if (cp)
1418 return cp->cmd(cmdtp, flag, argc, argv);
1419
Simon Glassa06dfc72011-12-10 08:44:01 +00001420 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001421}
1422
Kim Phillipsdc00a682012-10-29 13:34:31 +00001423#ifdef CONFIG_SYS_LONGHELP
1424static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001425#if defined(CONFIG_CMD_ASKENV)
1426 "ask name [message] [size] - ask for environment variable\nenv "
1427#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001428#if defined(CONFIG_CMD_ENV_CALLBACK)
1429 "callbacks - print callbacks and their associated variables\nenv "
1430#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001431 "default [-f] -a - [forcibly] reset default environment\n"
1432 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001433 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001434#if defined(CONFIG_CMD_EDITENV)
1435 "env edit name - edit environment variable\n"
1436#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001437#if defined(CONFIG_CMD_ENV_EXISTS)
1438 "env exists name - tests for existence of variable\n"
1439#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001440#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001441 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001442#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001443#if defined(CONFIG_CMD_ENV_FLAGS)
1444 "env flags - print variables that have non-default flags\n"
1445#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001446#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001447#ifdef CONFIG_REGEX
1448 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1449#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001450 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001451#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001452#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001453#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001454 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001455#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001456#if defined(CONFIG_CMD_NVEDIT_INFO)
1457 "env info - display environment information\n"
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001458 "env info [-d] [-p] [-q] - evaluate environment information\n"
1459 " \"-d\": default environment is used\n"
1460 " \"-p\": environment can be persisted\n"
1461 " \"-q\": quiet output\n"
Leo Ruan7450b872019-05-24 17:20:19 +02001462#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001463 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001464#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001465 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001466#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001467#if defined(CONFIG_CMD_RUN)
1468 "env run var [...] - run commands in an environment variable\n"
1469#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001470#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001471 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001472#if defined(CONFIG_CMD_ERASEENV)
1473 "env erase - erase environment\n"
1474#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001475#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001476#if defined(CONFIG_CMD_NVEDIT_LOAD)
1477 "env load - load environment\n"
1478#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001479#if defined(CONFIG_CMD_NVEDIT_SELECT)
1480 "env select [target] - select environment target\n"
1481#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001482#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001483 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001484 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001485#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001486 "env set [-f] name [arg ...]\n";
1487#endif
1488
1489U_BOOT_CMD(
1490 env, CONFIG_SYS_MAXARGS, 1, do_env,
1491 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001492);
1493
1494/*
1495 * Old command line interface, kept for compatibility
1496 */
wdenk57b2d802003-06-27 21:31:46 +00001497
Peter Tyser0deafa22009-10-25 15:12:56 -05001498#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001499U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001500 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001501 "edit environment variable",
1502 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001503 " - edit environment variable 'name'",
1504 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001505);
1506#endif
1507
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001508U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001509 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001510 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001511 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001512#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001513 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001514 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001515 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001516 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001517#endif
wdenk57b2d802003-06-27 21:31:46 +00001518 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001519 " - print value of environment variable 'name'",
1520 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001521);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001522
1523#ifdef CONFIG_CMD_GREPENV
1524U_BOOT_CMD_COMPLETE(
1525 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1526 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001527#ifdef CONFIG_REGEX
1528 "[-e] [-n | -v | -b] string ...\n"
1529#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001530 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001531#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001532 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001533#ifdef CONFIG_REGEX
1534 " \"-e\": enable regular expressions;\n"
1535#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001536 " \"-n\": search variable names; \"-v\": search values;\n"
1537 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001538 var_complete
1539);
1540#endif
wdenk57b2d802003-06-27 21:31:46 +00001541
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001542U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001543 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001544 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001545#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001546 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001547 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001548 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001549 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001550 " \"-nv\": set non-volatile attribute\n"
1551 " \"-bs\": set boot-service attribute\n"
1552 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001553 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001554 " \"-a\": append-write\n"
1555 " \"-i addr,size\": use <addr,size> as variable's value\n"
1556 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001557 " - delete UEFI variable 'name' if 'value' not specified\n"
1558#endif
1559 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001560 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1561 "setenv [-f] name\n"
1562 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001563 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001564);
1565
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001566#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001567
wdenkf287a242003-07-01 21:06:45 +00001568U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001569 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001570 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001571 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001572 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001573);
Jon Loeligerd704d912007-07-10 11:02:44 -05001574#endif
wdenk57b2d802003-06-27 21:31:46 +00001575
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001576#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001577U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001578 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001579 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001580 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001581 " - run the commands in the environment variable(s) 'var'",
1582 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001583);
Jon Loeligerd704d912007-07-10 11:02:44 -05001584#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001585#endif /* CONFIG_SPL_BUILD */