blob: 08d49df220a1bf23057eef8bd022cef0d600d424 [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);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000270 }
271
272 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200273 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000274 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000275 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000276 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000277
278 value = malloc(len);
279 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200280 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000281 return 1;
282 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000283 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200284 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000285
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200286 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000287 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000288 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000289 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200290 if (s != value)
291 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000292
Igor Grinbergde832562011-11-07 01:13:59 +0000293 e.key = name;
294 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600295 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200296 free(value);
297 if (!ep) {
298 printf("## Error inserting \"%s\" variable, errno=%d\n",
299 name, errno);
300 return 1;
301 }
wdenka68d3ed2002-10-11 08:38:32 +0000302
wdenka68d3ed2002-10-11 08:38:32 +0000303 return 0;
304}
305
Simon Glass6a38e412017-08-03 12:22:09 -0600306int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000307{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200308 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
309
Joe Hershberger3902c602013-04-08 10:32:50 +0000310 /* before import into hashtable */
311 if (!(gd->flags & GD_FLG_ENV_READY))
312 return 1;
313
Igor Grinbergde832562011-11-07 01:13:59 +0000314 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500315 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200316 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500317 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000318}
319
Simon Glassb8e926e2011-10-24 17:59:59 +0000320/**
321 * Set an environment variable to an integer value
322 *
Simon Glassdf372d62013-05-07 06:11:45 +0000323 * @param varname Environment variable to set
Simon Glassb8e926e2011-10-24 17:59:59 +0000324 * @param value Value to set it to
325 * @return 0 if ok, 1 on error
326 */
Simon Glass4d949a22017-08-03 12:22:10 -0600327int env_set_ulong(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000328{
329 /* TODO: this should be unsigned */
330 char *str = simple_itoa(value);
331
Simon Glass6a38e412017-08-03 12:22:09 -0600332 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000333}
334
335/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000336 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000337 *
Simon Glassdf372d62013-05-07 06:11:45 +0000338 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000339 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000340 * @return 0 if ok, 1 on error
341 */
Simon Glass4d949a22017-08-03 12:22:10 -0600342int env_set_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000343{
344 char str[17];
345
Simon Glass044dc9e2013-02-24 17:33:21 +0000346 sprintf(str, "%lx", value);
Simon Glass6a38e412017-08-03 12:22:09 -0600347 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000348}
349
Simon Glass22c34c22017-08-03 12:22:13 -0600350ulong env_get_hex(const char *varname, ulong default_val)
Simon Glassabbd5122013-04-20 08:42:43 +0000351{
352 const char *s;
353 ulong value;
354 char *endp;
355
Simon Glass64b723f2017-08-03 12:22:12 -0600356 s = env_get(varname);
Simon Glassabbd5122013-04-20 08:42:43 +0000357 if (s)
358 value = simple_strtoul(s, &endp, 16);
359 if (!s || endp == s)
360 return default_val;
361
362 return value;
363}
364
Alex Kiernan9c215492018-04-01 09:22:38 +0000365int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
366{
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500367 string_to_enetaddr(env_get(name), enetaddr);
Alex Kiernan9c215492018-04-01 09:22:38 +0000368 return is_valid_ethaddr(enetaddr);
369}
370
371int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
372{
373 char buf[ARP_HLEN_ASCII + 1];
374
375 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
376 return -EEXIST;
377
378 sprintf(buf, "%pM", enetaddr);
379
380 return env_set(name, buf);
381}
382
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000383#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600384static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
385 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000386{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200387 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000388 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000389
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500390 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000391}
392
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200393/*
wdenka68d3ed2002-10-11 08:38:32 +0000394 * Prompt for environment variable
395 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500396#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600397int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000398{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200399 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000400 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000401 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000402 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000403
404 local_args[0] = argv[0];
405 local_args[1] = argv[1];
406 local_args[2] = NULL;
407 local_args[3] = NULL;
408
Wolfgang Denk586895d2013-02-20 04:53:16 +0000409 /*
410 * Check the syntax:
411 *
412 * env_ask envname [message1 ...] [size]
413 */
414 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000415 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000416
Wolfgang Denk586895d2013-02-20 04:53:16 +0000417 /*
418 * We test the last argument if it can be converted
419 * into a decimal number. If yes, we assume it's
420 * the size. Otherwise we echo it as part of the
421 * message.
422 */
423 i = simple_strtoul(argv[argc - 1], &endptr, 10);
424 if (*endptr != '\0') { /* no size */
425 size = CONFIG_SYS_CBSIZE - 1;
426 } else { /* size given */
427 size = i;
428 --argc;
429 }
wdenka68d3ed2002-10-11 08:38:32 +0000430
Wolfgang Denk586895d2013-02-20 04:53:16 +0000431 if (argc <= 2) {
432 sprintf(message, "Please enter '%s': ", argv[1]);
433 } else {
434 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400435 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000436 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000437 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000438
Tom Rini4beb2352017-09-26 19:37:11 -0400439 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000440 pos += strlen(argv[i]);
441 }
Tom Rini4beb2352017-09-26 19:37:11 -0400442 if (pos < sizeof(message) - 1) {
443 message[pos++] = ' ';
444 message[pos] = '\0';
445 } else
446 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000447 }
448
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200449 if (size >= CONFIG_SYS_CBSIZE)
450 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000451
452 if (size <= 0)
453 return 1;
454
455 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600456 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000457
458 if (size < len)
459 console_buffer[size] = '\0';
460
461 len = 2;
462 if (console_buffer[0] != '\0') {
463 local_args[2] = console_buffer;
464 len = 3;
465 }
466
467 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500468 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000469}
Jon Loeligerd704d912007-07-10 11:02:44 -0500470#endif
wdenka68d3ed2002-10-11 08:38:32 +0000471
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600472#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500473static int print_static_binding(const char *var_name, const char *callback_name,
474 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600475{
476 printf("\t%-20s %-20s\n", var_name, callback_name);
477
478 return 0;
479}
480
Simon Glass1a236862019-08-02 09:44:18 -0600481static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600482{
483 struct env_clbk_tbl *clbkp;
484 int i;
485 int num_callbacks;
486
487 if (entry->callback == NULL)
488 return 0;
489
490 /* look up the callback in the linker-list */
491 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
492 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
493 i < num_callbacks;
494 i++, clbkp++) {
495#if defined(CONFIG_NEEDS_MANUAL_RELOC)
496 if (entry->callback == clbkp->callback + gd->reloc_off)
497#else
498 if (entry->callback == clbkp->callback)
499#endif
500 break;
501 }
502
503 if (i == num_callbacks)
504 /* this should probably never happen, but just in case... */
505 printf("\t%-20s %p\n", entry->key, entry->callback);
506 else
507 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
508
509 return 0;
510}
511
512/*
513 * Print the callbacks available and what they are bound to
514 */
Simon Glassed38aef2020-05-10 11:40:03 -0600515int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
516 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600517{
518 struct env_clbk_tbl *clbkp;
519 int i;
520 int num_callbacks;
521
522 /* Print the available callbacks */
523 puts("Available callbacks:\n");
524 puts("\tCallback Name\n");
525 puts("\t-------------\n");
526 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
527 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
528 i < num_callbacks;
529 i++, clbkp++)
530 printf("\t%s\n", clbkp->name);
531 puts("\n");
532
533 /* Print the static bindings that may exist */
534 puts("Static callback bindings:\n");
535 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
536 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500537 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600538 puts("\n");
539
540 /* walk through each variable and print the callback if it has one */
541 puts("Active callback bindings:\n");
542 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
543 printf("\t%-20s %-20s\n", "-------------", "-------------");
544 hwalk_r(&env_htab, print_active_callback);
545 return 0;
546}
547#endif
548
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600549#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500550static int print_static_flags(const char *var_name, const char *flags,
551 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600552{
553 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600554 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600555
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600556 printf("\t%-20s %-20s %-20s\n", var_name,
557 env_flags_get_vartype_name(type),
558 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600559
560 return 0;
561}
562
Simon Glass1a236862019-08-02 09:44:18 -0600563static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600564{
565 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600566 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600567
568 if (entry->flags == 0)
569 return 0;
570
571 type = (enum env_flags_vartype)
572 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600573 access = env_flags_parse_varaccess_from_binflags(entry->flags);
574 printf("\t%-20s %-20s %-20s\n", entry->key,
575 env_flags_get_vartype_name(type),
576 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600577
578 return 0;
579}
580
581/*
582 * Print the flags available and what variables have flags
583 */
Simon Glassed38aef2020-05-10 11:40:03 -0600584int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600585{
586 /* Print the available variable types */
587 printf("Available variable type flags (position %d):\n",
588 ENV_FLAGS_VARTYPE_LOC);
589 puts("\tFlag\tVariable Type Name\n");
590 puts("\t----\t------------------\n");
591 env_flags_print_vartypes();
592 puts("\n");
593
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600594 /* Print the available variable access types */
595 printf("Available variable access flags (position %d):\n",
596 ENV_FLAGS_VARACCESS_LOC);
597 puts("\tFlag\tVariable Access Name\n");
598 puts("\t----\t--------------------\n");
599 env_flags_print_varaccess();
600 puts("\n");
601
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600602 /* Print the static flags that may exist */
603 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600604 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
605 "Variable Access");
606 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
607 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500608 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600609 puts("\n");
610
611 /* walk through each variable and print the flags if non-default */
612 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600613 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
614 "Variable Access");
615 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
616 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600617 hwalk_r(&env_htab, print_active_flags);
618 return 0;
619}
620#endif
621
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200622/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500623 * Interactively edit an environment variable
624 */
625#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600626static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
627 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500628{
629 char buffer[CONFIG_SYS_CBSIZE];
630 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500631
Wolfgang Denk3b683112010-07-17 01:06:04 +0200632 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000633 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500634
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500635 /* before import into hashtable */
636 if (!(gd->flags & GD_FLG_ENV_READY))
637 return 1;
638
Peter Tyser0deafa22009-10-25 15:12:56 -0500639 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600640 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500641 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800642 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500643 else
644 buffer[0] = '\0';
645
Simon Glassbe6aafc2014-04-10 20:01:27 -0600646 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000647 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500648
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500649 if (buffer[0] == '\0') {
650 const char * const _argv[3] = { "setenv", argv[1], NULL };
651
652 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
653 } else {
654 const char * const _argv[4] = { "setenv", argv[1], buffer,
655 NULL };
656
657 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
658 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500659}
660#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000661#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500662
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200663/*
wdenka68d3ed2002-10-11 08:38:32 +0000664 * Look up variable from environment,
665 * return address of storage for that variable,
666 * or NULL if not found
667 */
Simon Glass64b723f2017-08-03 12:22:12 -0600668char *env_get(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000669{
Igor Grinbergde832562011-11-07 01:13:59 +0000670 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Simon Glass1a236862019-08-02 09:44:18 -0600671 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000672
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200673 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000674
Igor Grinbergde832562011-11-07 01:13:59 +0000675 e.key = name;
676 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600677 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200678
Macpaul Linff2a9f32011-04-26 16:16:45 +0000679 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000680 }
681
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200682 /* restricted capabilities before import */
Simon Glass64b723f2017-08-03 12:22:12 -0600683 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200684 return (char *)(gd->env_buf);
685
686 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000687}
688
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200689/*
Patrice Chotardceed4b42019-11-25 09:07:36 +0100690 * Like env_get, but prints an error if envvar isn't defined in the
691 * environment. It always returns what env_get does, so it can be used in
692 * place of env_get without changing error handling otherwise.
693 */
694char *from_env(const char *envvar)
695{
696 char *ret;
697
698 ret = env_get(envvar);
699
700 if (!ret)
701 printf("missing environment variable: %s\n", envvar);
702
703 return ret;
704}
705
706/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200707 * Look up variable from environment for restricted C runtime env.
708 */
Simon Glass64b723f2017-08-03 12:22:12 -0600709int env_get_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000710{
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100711 int i, nxt, c;
wdenka68d3ed2002-10-11 08:38:32 +0000712
Igor Grinbergde832562011-11-07 01:13:59 +0000713 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000714 int val, n;
715
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100716 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
717 if (c < 0)
718 return c;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000719 if (nxt >= CONFIG_ENV_SIZE)
720 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000721 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000722
Simon Glass909ff4d2019-08-01 09:46:45 -0600723 val = env_match((uchar *)name, i);
Macpaul Linff2a9f32011-04-26 16:16:45 +0000724 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000725 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200726
wdenka68d3ed2002-10-11 08:38:32 +0000727 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000728 for (n = 0; n < len; ++n, ++buf) {
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100729 c = env_get_char(val++);
730 if (c < 0)
731 return c;
732 *buf = c;
Igor Grinbergde832562011-11-07 01:13:59 +0000733 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200734 return n;
735 }
736
737 if (n)
738 *--buf = '\0';
739
Heinrich Schuchardtcf80bd72019-01-06 11:52:06 +0100740 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
741 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200742
743 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000744 }
Igor Grinbergde832562011-11-07 01:13:59 +0000745
Macpaul Linff2a9f32011-04-26 16:16:45 +0000746 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000747}
748
Simon Glassbf690f82011-10-14 13:25:18 +0000749/**
750 * Decode the integer value of an environment variable and return it.
751 *
Shyam Sainiefe757c2018-06-07 19:47:19 +0530752 * @param name Name of environment variable
Simon Glassbf690f82011-10-14 13:25:18 +0000753 * @param base Number base to use (normally 10, or 16 for hex)
754 * @param default_val Default value to return if the variable is not
755 * found
756 * @return the decoded value, or default_val if not found
757 */
Simon Glass22c34c22017-08-03 12:22:13 -0600758ulong env_get_ulong(const char *name, int base, ulong default_val)
Simon Glassbf690f82011-10-14 13:25:18 +0000759{
760 /*
Simon Glass64b723f2017-08-03 12:22:12 -0600761 * We can use env_get() here, even before relocation, since the
Simon Glassbf690f82011-10-14 13:25:18 +0000762 * environment variable value is an integer and thus short.
763 */
Simon Glass64b723f2017-08-03 12:22:12 -0600764 const char *str = env_get(name);
Simon Glassbf690f82011-10-14 13:25:18 +0000765
766 return str ? simple_strtoul(str, NULL, base) : default_val;
767}
768
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000769#ifndef CONFIG_SPL_BUILD
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100770#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Simon Glassed38aef2020-05-10 11:40:03 -0600771static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
772 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000773{
Simon Glassd49b8892017-08-03 12:22:08 -0600774 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000775}
wdenk57b2d802003-06-27 21:31:46 +0000776
Mike Frysinger61583932008-12-30 02:59:25 -0500777U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200778 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600779 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200780 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500781);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200782
783#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600784static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
785 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200786{
787 return env_erase() ? 1 : 0;
788}
789
790U_BOOT_CMD(
791 eraseenv, 1, 0, do_env_erase,
792 "erase environment variables from persistent storage",
793 ""
794);
795#endif
wdenka68d3ed2002-10-11 08:38:32 +0000796#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000797#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000798
Simon Glass909ff4d2019-08-01 09:46:45 -0600799int env_match(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000800{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000801 if (s1 == NULL)
802 return -1;
803
wdenka68d3ed2002-10-11 08:38:32 +0000804 while (*s1 == env_get_char(i2++))
805 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000806 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000807
wdenka68d3ed2002-10-11 08:38:32 +0000808 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000809 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000810
Macpaul Linff2a9f32011-04-26 16:16:45 +0000811 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000812}
wdenk57b2d802003-06-27 21:31:46 +0000813
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000814#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600815static int do_env_default(struct cmd_tbl *cmdtp, int flag,
816 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200817{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300818 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000819
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000820 debug("Initial value for argc=%d\n", argc);
821 while (--argc > 0 && **++argv == '-') {
822 char *arg = *argv;
823
824 while (*++arg) {
825 switch (*arg) {
826 case 'a': /* default all */
827 all = 1;
828 break;
829 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300830 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000831 break;
832 default:
833 return cmd_usage(cmdtp);
834 }
835 }
836 }
837 debug("Final value for argc=%d\n", argc);
838 if (all && (argc == 0)) {
839 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600840 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300841 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000842 return 0;
843 }
844 if (!all && (argc > 0)) {
845 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600846 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000847 return 0;
848 }
849
850 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200851}
852
Simon Glassed38aef2020-05-10 11:40:03 -0600853static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
854 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200855{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600856 int env_flag = H_INTERACTIVE;
857 int ret = 0;
858
859 debug("Initial value for argc=%d\n", argc);
860 while (argc > 1 && **(argv + 1) == '-') {
861 char *arg = *++argv;
862
863 --argc;
864 while (*++arg) {
865 switch (*arg) {
866 case 'f': /* force */
867 env_flag |= H_FORCE;
868 break;
869 default:
870 return CMD_RET_USAGE;
871 }
872 }
873 }
874 debug("Final value for argc=%d\n", argc);
875
876 env_id++;
877
878 while (--argc > 0) {
879 char *name = *++argv;
880
881 if (!hdelete_r(name, &env_htab, env_flag))
882 ret = 1;
883 }
884
885 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200886}
887
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500888#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200889/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100890 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200891 * -t: export as text format; if size is given, data will be
892 * padded with '\0' bytes; if not, one terminating '\0'
893 * will be added (which is included in the "filesize"
894 * setting so you can for exmple copy this to flash and
895 * keep the termination).
896 * -b: export as binary format (name=value pairs separated by
897 * '\0', list end marked by double "\0\0")
898 * -c: export as checksum protected environment format as
899 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100900 * -s size:
901 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200902 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100903 * var... List of variable names that get included into the
904 * export. Without arguments, the whole environment gets
905 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200906 *
907 * With "-c" and size is NOT given, then the export command will
908 * format the data as currently used for the persistent storage,
909 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400910 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200911 * environment, a "current" redundancy flag. If size is given, this
912 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
913 * checksum and redundancy flag will be inserted.
914 *
915 * With "-b" and "-t", always only the real data (including a
916 * terminating '\0' byte) will be written; here the optional size
917 * argument will be used to make sure not to overflow the user
918 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400919 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200920 *
921 * On successful return, the variable "filesize" will be set.
922 * Note that filesize includes the trailing/terminating '\0' byte(s).
923 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400924 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200925 *
926 * => env export -t 100000
927 * => era ${backup_addr} +${filesize}
928 * => cp.b 100000 ${backup_addr} ${filesize}
929 *
930 * Re-import this snapshot, deleting all other settings:
931 *
932 * => env import -d -t ${backup_addr}
933 */
Simon Glassed38aef2020-05-10 11:40:03 -0600934static int do_env_export(struct cmd_tbl *cmdtp, int flag,
935 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200936{
937 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600938 ulong addr;
939 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100940 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200941 ssize_t len;
942 env_t *envp;
943 char sep = '\n';
944 int chk = 0;
945 int fmt = 0;
946
947 cmd = *argv;
948
949 while (--argc > 0 && **++argv == '-') {
950 char *arg = *argv;
951 while (*++arg) {
952 switch (*arg) {
953 case 'b': /* raw binary format */
954 if (fmt++)
955 goto sep_err;
956 sep = '\0';
957 break;
958 case 'c': /* external checksum format */
959 if (fmt++)
960 goto sep_err;
961 sep = '\0';
962 chk = 1;
963 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100964 case 's': /* size given */
965 if (--argc <= 0)
966 return cmd_usage(cmdtp);
967 size = simple_strtoul(*++argv, NULL, 16);
968 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200969 case 't': /* text format */
970 if (fmt++)
971 goto sep_err;
972 sep = '\n';
973 break;
974 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000975 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200976 }
977 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100978NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200979 }
980
Macpaul Linff2a9f32011-04-26 16:16:45 +0000981 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000982 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000983
Simon Glassa7459262013-10-25 23:01:31 -0600984 addr = simple_strtoul(argv[0], NULL, 16);
985 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200986
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100987 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600988 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100989
990 argc--;
991 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200992
993 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000994 len = hexport_r(&env_htab, sep,
995 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600996 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200997 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200998 pr_err("## Error: Cannot export environment: errno = %d\n",
999 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001000 return 1;
1001 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +01001002 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -06001003 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001004
1005 return 0;
1006 }
1007
Simon Glassa7459262013-10-25 23:01:31 -06001008 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001009
1010 if (chk) /* export as checksum protected block */
1011 res = (char *)envp->data;
1012 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -06001013 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001014
Wolfgang Denk565af272013-03-23 23:50:28 +00001015 len = hexport_r(&env_htab, '\0',
1016 H_MATCH_KEY | H_MATCH_IDENT,
1017 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001018 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001019 pr_err("## Error: Cannot export environment: errno = %d\n",
1020 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001021 return 1;
1022 }
1023
1024 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +00001025 envp->crc = crc32(0, envp->data,
1026 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001027#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -06001028 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001029#endif
1030 }
Simon Glass4d949a22017-08-03 12:22:10 -06001031 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001032
1033 return 0;
1034
1035sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001036 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1037 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001038 return 1;
1039}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001040#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001041
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001042#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001043/*
Quentin Schulz0312b942018-07-09 19:16:29 +02001044 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1045 * -d: delete existing environment before importing if no var is
1046 * passed; if vars are passed, if one var is in the current
1047 * environment but not in the environment at addr, delete var from
1048 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04001049 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001050 * -t: assume text format; either "size" must be given or the
1051 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +02001052 * -r: handle CRLF like LF, that means exported variables with
1053 * a content which ends with \r won't get imported. Used
1054 * to import text files created with editors which are using CRLF
1055 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001056 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1057 * -c: assume checksum protected environment format
1058 * addr: memory address to read from
1059 * size: length of input data; if missing, proper '\0'
1060 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +02001061 * if var is set and size should be missing (i.e. '\0'
1062 * termination), set size to '-'
1063 * var... List of the names of the only variables that get imported from
1064 * the environment at address 'addr'. Without arguments, the whole
1065 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001066 */
Simon Glassed38aef2020-05-10 11:40:03 -06001067static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1068 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001069{
Simon Glassa7459262013-10-25 23:01:31 -06001070 ulong addr;
1071 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001072 char sep = '\n';
1073 int chk = 0;
1074 int fmt = 0;
1075 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +02001076 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +02001077 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001078 size_t size;
1079
1080 cmd = *argv;
1081
1082 while (--argc > 0 && **++argv == '-') {
1083 char *arg = *argv;
1084 while (*++arg) {
1085 switch (*arg) {
1086 case 'b': /* raw binary format */
1087 if (fmt++)
1088 goto sep_err;
1089 sep = '\0';
1090 break;
1091 case 'c': /* external checksum format */
1092 if (fmt++)
1093 goto sep_err;
1094 sep = '\0';
1095 chk = 1;
1096 break;
1097 case 't': /* text format */
1098 if (fmt++)
1099 goto sep_err;
1100 sep = '\n';
1101 break;
Alexander Holler3e12be72014-07-14 17:49:55 +02001102 case 'r': /* handle CRLF like LF */
1103 crlf_is_lf = 1;
1104 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001105 case 'd':
1106 del = 1;
1107 break;
1108 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001109 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001110 }
1111 }
1112 }
1113
Macpaul Linff2a9f32011-04-26 16:16:45 +00001114 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001115 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001116
1117 if (!fmt)
1118 printf("## Warning: defaulting to text format\n");
1119
Alexander Holler3e12be72014-07-14 17:49:55 +02001120 if (sep != '\n' && crlf_is_lf )
1121 crlf_is_lf = 0;
1122
Simon Glassa7459262013-10-25 23:01:31 -06001123 addr = simple_strtoul(argv[0], NULL, 16);
1124 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001125
Quentin Schulz0312b942018-07-09 19:16:29 +02001126 if (argc >= 2 && strcmp(argv[1], "-")) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001127 size = simple_strtoul(argv[1], NULL, 16);
Quentin Schulz0312b942018-07-09 19:16:29 +02001128 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -05001129 puts("## Error: external checksum format must pass size\n");
1130 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001131 } else {
Simon Glassa7459262013-10-25 23:01:31 -06001132 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001133
1134 size = 0;
1135
1136 while (size < MAX_ENV_SIZE) {
1137 if ((*s == sep) && (*(s+1) == '\0'))
1138 break;
1139 ++s;
1140 ++size;
1141 }
1142 if (size == MAX_ENV_SIZE) {
1143 printf("## Warning: Input data exceeds %d bytes"
1144 " - truncated\n", MAX_ENV_SIZE);
1145 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001146 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001147 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001148 }
1149
Quentin Schulz0312b942018-07-09 19:16:29 +02001150 if (argc > 2)
1151 wl = 1;
1152
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001153 if (chk) {
1154 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -06001155 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001156
1157 size -= offsetof(env_t, data);
1158 memcpy(&crc, &ep->crc, sizeof(crc));
1159
1160 if (crc32(0, ep->data, size) != crc) {
1161 puts("## Error: bad CRC, import failed\n");
1162 return 1;
1163 }
Simon Glassa7459262013-10-25 23:01:31 -06001164 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001165 }
1166
Quentin Schulz0312b942018-07-09 19:16:29 +02001167 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1168 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001169 pr_err("## Error: Environment import failed: errno = %d\n",
1170 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001171 return 1;
1172 }
1173 gd->flags |= GD_FLG_ENV_READY;
1174
1175 return 0;
1176
1177sep_err:
1178 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1179 cmd);
1180 return 1;
1181}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001182#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001183
Leo Ruan7450b872019-05-24 17:20:19 +02001184#if defined(CONFIG_CMD_NVEDIT_INFO)
1185/*
1186 * print_env_info - print environment information
1187 */
1188static int print_env_info(void)
1189{
1190 const char *value;
1191
1192 /* print environment validity value */
1193 switch (gd->env_valid) {
1194 case ENV_INVALID:
1195 value = "invalid";
1196 break;
1197 case ENV_VALID:
1198 value = "valid";
1199 break;
1200 case ENV_REDUND:
1201 value = "redundant";
1202 break;
1203 default:
1204 value = "unknown";
1205 break;
1206 }
1207 printf("env_valid = %s\n", value);
1208
1209 /* print environment ready flag */
1210 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1211 printf("env_ready = %s\n", value);
1212
1213 /* print environment using default flag */
1214 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1215 printf("env_use_default = %s\n", value);
1216
1217 return CMD_RET_SUCCESS;
1218}
1219
1220#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1221#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1222
1223/*
1224 * env info - display environment information
1225 * env info [-d] - evaluate whether default environment is used
1226 * env info [-p] - evaluate whether environment can be persisted
1227 */
Simon Glassed38aef2020-05-10 11:40:03 -06001228static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1229 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +02001230{
1231 int eval_flags = 0;
1232 int eval_results = 0;
1233
1234 /* display environment information */
1235 if (argc <= 1)
1236 return print_env_info();
1237
1238 /* process options */
1239 while (--argc > 0 && **++argv == '-') {
1240 char *arg = *argv;
1241
1242 while (*++arg) {
1243 switch (*arg) {
1244 case 'd':
1245 eval_flags |= ENV_INFO_IS_DEFAULT;
1246 break;
1247 case 'p':
1248 eval_flags |= ENV_INFO_IS_PERSISTED;
1249 break;
1250 default:
1251 return CMD_RET_USAGE;
1252 }
1253 }
1254 }
1255
1256 /* evaluate whether default environment is used */
1257 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1258 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1259 printf("Default environment is used\n");
1260 eval_results |= ENV_INFO_IS_DEFAULT;
1261 } else {
1262 printf("Environment was loaded from persistent storage\n");
1263 }
1264 }
1265
1266 /* evaluate whether environment can be persisted */
1267 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1268#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1269 printf("Environment can be persisted\n");
1270 eval_results |= ENV_INFO_IS_PERSISTED;
1271#else
1272 printf("Environment cannot be persisted\n");
1273#endif
1274 }
1275
1276 /* The result of evaluations is combined with AND */
1277 if (eval_flags != eval_results)
1278 return CMD_RET_FAILURE;
1279
1280 return CMD_RET_SUCCESS;
1281}
1282#endif
1283
Andrew Ruder94463402013-10-22 19:07:34 -05001284#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001285static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1286 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001287{
Simon Glass1a236862019-08-02 09:44:18 -06001288 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001289
1290 if (argc < 2)
1291 return CMD_RET_USAGE;
1292
1293 e.key = argv[1];
1294 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001295 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001296
1297 return (ep == NULL) ? 1 : 0;
1298}
1299#endif
1300
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001301/*
1302 * New command line interface: "env" command with subcommands
1303 */
Simon Glassed38aef2020-05-10 11:40:03 -06001304static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001305#if defined(CONFIG_CMD_ASKENV)
1306 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1307#endif
1308 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001309 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001310#if defined(CONFIG_CMD_EDITENV)
1311 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1312#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001313#if defined(CONFIG_CMD_ENV_CALLBACK)
1314 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1315#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001316#if defined(CONFIG_CMD_ENV_FLAGS)
1317 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1318#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001319#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001320 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001321#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001322#if defined(CONFIG_CMD_GREPENV)
1323 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1324#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001325#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001326 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001327#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001328#if defined(CONFIG_CMD_NVEDIT_INFO)
1329 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1330#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001331 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1332#if defined(CONFIG_CMD_RUN)
1333 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1334#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001335#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001336 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001337#if defined(CONFIG_CMD_ERASEENV)
1338 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1339#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001340#endif
1341 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001342#if defined(CONFIG_CMD_ENV_EXISTS)
1343 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1344#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001345};
1346
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001347#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001348void env_reloc(void)
1349{
1350 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1351}
1352#endif
1353
Simon Glassed38aef2020-05-10 11:40:03 -06001354static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001355{
Simon Glassed38aef2020-05-10 11:40:03 -06001356 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001357
Thomas Weber4d282652010-11-24 13:07:52 +01001358 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001359 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001360
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001361 /* drop initial "env" arg */
1362 argc--;
1363 argv++;
1364
1365 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1366
1367 if (cp)
1368 return cp->cmd(cmdtp, flag, argc, argv);
1369
Simon Glassa06dfc72011-12-10 08:44:01 +00001370 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001371}
1372
Kim Phillipsdc00a682012-10-29 13:34:31 +00001373#ifdef CONFIG_SYS_LONGHELP
1374static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001375#if defined(CONFIG_CMD_ASKENV)
1376 "ask name [message] [size] - ask for environment variable\nenv "
1377#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001378#if defined(CONFIG_CMD_ENV_CALLBACK)
1379 "callbacks - print callbacks and their associated variables\nenv "
1380#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001381 "default [-f] -a - [forcibly] reset default environment\n"
1382 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001383 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001384#if defined(CONFIG_CMD_EDITENV)
1385 "env edit name - edit environment variable\n"
1386#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001387#if defined(CONFIG_CMD_ENV_EXISTS)
1388 "env exists name - tests for existence of variable\n"
1389#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001390#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001391 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001392#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001393#if defined(CONFIG_CMD_ENV_FLAGS)
1394 "env flags - print variables that have non-default flags\n"
1395#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001396#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001397#ifdef CONFIG_REGEX
1398 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1399#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001400 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001401#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001402#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001403#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001404 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001405#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001406#if defined(CONFIG_CMD_NVEDIT_INFO)
1407 "env info - display environment information\n"
1408 "env info [-d] - whether default environment is used\n"
1409 "env info [-p] - whether environment can be persisted\n"
1410#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001411 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001412#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001413 "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001414#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001415#if defined(CONFIG_CMD_RUN)
1416 "env run var [...] - run commands in an environment variable\n"
1417#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001418#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001419 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001420#if defined(CONFIG_CMD_ERASEENV)
1421 "env erase - erase environment\n"
1422#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001423#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001424#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001425 "env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001426 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001427#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001428 "env set [-f] name [arg ...]\n";
1429#endif
1430
1431U_BOOT_CMD(
1432 env, CONFIG_SYS_MAXARGS, 1, do_env,
1433 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001434);
1435
1436/*
1437 * Old command line interface, kept for compatibility
1438 */
wdenk57b2d802003-06-27 21:31:46 +00001439
Peter Tyser0deafa22009-10-25 15:12:56 -05001440#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001441U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001442 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001443 "edit environment variable",
1444 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001445 " - edit environment variable 'name'",
1446 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001447);
1448#endif
1449
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001450U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001451 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001452 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001453 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001454#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001455 "printenv -e [-guid guid|-all][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001456 " - print UEFI variable 'name' or all the variables\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001457 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001458#endif
wdenk57b2d802003-06-27 21:31:46 +00001459 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001460 " - print value of environment variable 'name'",
1461 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001462);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001463
1464#ifdef CONFIG_CMD_GREPENV
1465U_BOOT_CMD_COMPLETE(
1466 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1467 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001468#ifdef CONFIG_REGEX
1469 "[-e] [-n | -v | -b] string ...\n"
1470#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001471 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001472#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001473 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001474#ifdef CONFIG_REGEX
1475 " \"-e\": enable regular expressions;\n"
1476#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001477 " \"-n\": search variable names; \"-v\": search values;\n"
1478 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001479 var_complete
1480);
1481#endif
wdenk57b2d802003-06-27 21:31:46 +00001482
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001483U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001484 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001485 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001486#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001487 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001488 " [-i addr,size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001489 " - set UEFI variable 'name' to 'value' ...'\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001490 " \"-guid\": set vendor guid\n"
1491 " \"-nv\": set non-volatile attribute\n"
1492 " \"-bs\": set boot-service attribute\n"
1493 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001494 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001495 " \"-a\": append-write\n"
1496 " \"-i addr,size\": use <addr,size> as variable's value\n"
1497 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001498 " - delete UEFI variable 'name' if 'value' not specified\n"
1499#endif
1500 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001501 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1502 "setenv [-f] name\n"
1503 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001504 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001505);
1506
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001507#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001508
wdenkf287a242003-07-01 21:06:45 +00001509U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001510 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001511 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001512 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001513 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001514);
Jon Loeligerd704d912007-07-10 11:02:44 -05001515#endif
wdenk57b2d802003-06-27 21:31:46 +00001516
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001517#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001518U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001519 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001520 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001521 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001522 " - run the commands in the environment variable(s) 'var'",
1523 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001524);
Jon Loeligerd704d912007-07-10 11:02:44 -05001525#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001526#endif /* CONFIG_SPL_BUILD */