blob: daf1ad37f9be25477e63e058061395922c55b12b [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>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020033#include <search.h>
34#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050035#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050036#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060037#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060038#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060039#include <linux/printk.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070040#include <u-boot/crc.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
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020047/*
48 * Maximum expected input data size for import command
49 */
50#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000051
wdenka68d3ed2002-10-11 08:38:32 +000052/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020053 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060054 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020055 * has changed or not. So it is possible to reread an environment
56 * variable only if the environment was changed ... done so for
57 * example in NetInitLoop()
58 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010059static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000060
Simon Glass2d85a752019-08-01 09:46:41 -060061int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010062{
63 return env_id;
64}
wdenka68d3ed2002-10-11 08:38:32 +000065
Ilya Yanokf7a2c552012-09-18 00:22:50 +000066#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040067/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020068 * Command interface: print one or all environment variables
69 *
70 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040071 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060072static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000073{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020074 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000075 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000076
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020077 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -060078 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000079
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020080 e.key = name;
81 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -060082 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020083 if (ep == NULL)
84 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +000085 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086 return len;
87 }
wdenka68d3ed2002-10-11 08:38:32 +000088
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020089 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -060090 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +000091
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020092 if (len > 0) {
93 puts(res);
94 free(res);
95 return len;
wdenka68d3ed2002-10-11 08:38:32 +000096 }
97
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +000099 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200100 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400101}
wdenka68d3ed2002-10-11 08:38:32 +0000102
Simon Glassed38aef2020-05-10 11:40:03 -0600103static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
104 char *const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400105{
106 int i;
107 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600108 int env_flag = H_HIDE_DOT;
109
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900110#if defined(CONFIG_CMD_NVEDIT_EFI)
111 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
112 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
113#endif
114
Joe Hershberger79a905e2012-12-11 22:16:23 -0600115 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
116 argc--;
117 argv++;
118 env_flag &= ~H_HIDE_DOT;
119 }
wdenka68d3ed2002-10-11 08:38:32 +0000120
Mike Frysinger87944922009-05-24 02:26:19 -0400121 if (argc == 1) {
122 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600123 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200124 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400125 return 1;
126 printf("\nEnvironment size: %d/%ld bytes\n",
127 rcode, (ulong)ENV_SIZE);
128 return 0;
129 }
wdenka68d3ed2002-10-11 08:38:32 +0000130
Mike Frysinger87944922009-05-24 02:26:19 -0400131 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600132 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400133 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600134 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200135 if (!rc) {
136 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400137 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000138 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000139 }
140
141 return rcode;
142}
143
144#ifdef CONFIG_CMD_GREPENV
Simon Glassed38aef2020-05-10 11:40:03 -0600145static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
146 int argc, char *const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000147{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000148 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000149 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000150
151 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000152 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000153
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000154 grep_how = H_MATCH_SUBSTR; /* default: substring search */
155 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000156
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200157 while (--argc > 0 && **++argv == '-') {
158 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000159 while (*++arg) {
160 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000161#ifdef CONFIG_REGEX
162 case 'e': /* use regex matching */
163 grep_how = H_MATCH_REGEX;
164 break;
165#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000166 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000167 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000168 break;
169 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000170 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000171 break;
172 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000173 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000174 break;
175 case '-':
176 goto DONE;
177 default:
178 return CMD_RET_USAGE;
179 }
180 }
181 }
182
183DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000184 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000185 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000186 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000187
Wolfgang Denke27fc122013-03-23 23:50:29 +0000188 if (len > 0) {
189 puts(res);
190 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000191 }
Mike Frysinger87944922009-05-24 02:26:19 -0400192
Wolfgang Denke27fc122013-03-23 23:50:29 +0000193 if (len < 2)
194 return 1;
195
196 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000197}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000198#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000199#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000200
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200201/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000202 * Set a new environment variable,
203 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600204 */
Simon Glassed38aef2020-05-10 11:40:03 -0600205static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000206{
207 int i, len;
208 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600209 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000210
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600211 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900212
Simon Glass70f58222023-02-05 15:36:36 -0700213#if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900214 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
215 return do_env_set_efi(NULL, flag, --argc, ++argv);
216#endif
217
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600218 while (argc > 1 && **(argv + 1) == '-') {
219 char *arg = *++argv;
220
221 --argc;
222 while (*++arg) {
223 switch (*arg) {
224 case 'f': /* force */
225 env_flag |= H_FORCE;
226 break;
227 default:
228 return CMD_RET_USAGE;
229 }
230 }
231 }
232 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000233 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000234
235 if (strchr(name, '=')) {
236 printf("## Error: illegal character '='"
237 "in variable name \"%s\"\n", name);
238 return 1;
239 }
240
241 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000242
wdenka68d3ed2002-10-11 08:38:32 +0000243 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000244 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600245 int rc = hdelete_r(name, &env_htab, env_flag);
Simon Glass783ef3f2020-11-05 10:33:37 -0700246
247 /* If the variable didn't exist, don't report an error */
248 return rc && rc != -ENOENT ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000249 }
250
251 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200252 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000253 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000254 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000255 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000256
257 value = malloc(len);
258 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200259 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000260 return 1;
261 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000262 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200263 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000264
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200265 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000266 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000267 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000268 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 if (s != value)
270 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000271
Igor Grinbergde832562011-11-07 01:13:59 +0000272 e.key = name;
273 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600274 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200275 free(value);
276 if (!ep) {
277 printf("## Error inserting \"%s\" variable, errno=%d\n",
278 name, errno);
279 return 1;
280 }
wdenka68d3ed2002-10-11 08:38:32 +0000281
wdenka68d3ed2002-10-11 08:38:32 +0000282 return 0;
283}
284
Simon Glass6a38e412017-08-03 12:22:09 -0600285int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000286{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200287 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
288
Joe Hershberger3902c602013-04-08 10:32:50 +0000289 /* before import into hashtable */
290 if (!(gd->flags & GD_FLG_ENV_READY))
291 return 1;
292
Igor Grinbergde832562011-11-07 01:13:59 +0000293 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500294 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200295 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500296 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000297}
298
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000299#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600300static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
301 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000302{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200303 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000304 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000305
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500306 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000307}
308
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200309/*
wdenka68d3ed2002-10-11 08:38:32 +0000310 * Prompt for environment variable
311 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500312#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600313int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000314{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200315 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000316 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000317 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000318 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000319
320 local_args[0] = argv[0];
321 local_args[1] = argv[1];
322 local_args[2] = NULL;
323 local_args[3] = NULL;
324
Wolfgang Denk586895d2013-02-20 04:53:16 +0000325 /*
326 * Check the syntax:
327 *
328 * env_ask envname [message1 ...] [size]
329 */
330 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000331 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000332
Wolfgang Denk586895d2013-02-20 04:53:16 +0000333 /*
334 * We test the last argument if it can be converted
335 * into a decimal number. If yes, we assume it's
336 * the size. Otherwise we echo it as part of the
337 * message.
338 */
Simon Glassff9b9032021-07-24 09:03:30 -0600339 i = dectoul(argv[argc - 1], &endptr);
Wolfgang Denk586895d2013-02-20 04:53:16 +0000340 if (*endptr != '\0') { /* no size */
341 size = CONFIG_SYS_CBSIZE - 1;
342 } else { /* size given */
343 size = i;
344 --argc;
345 }
wdenka68d3ed2002-10-11 08:38:32 +0000346
Wolfgang Denk586895d2013-02-20 04:53:16 +0000347 if (argc <= 2) {
348 sprintf(message, "Please enter '%s': ", argv[1]);
349 } else {
350 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400351 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000352 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000353 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000354
Tom Rini4beb2352017-09-26 19:37:11 -0400355 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000356 pos += strlen(argv[i]);
357 }
Tom Rini4beb2352017-09-26 19:37:11 -0400358 if (pos < sizeof(message) - 1) {
359 message[pos++] = ' ';
360 message[pos] = '\0';
361 } else
362 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000363 }
364
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200365 if (size >= CONFIG_SYS_CBSIZE)
366 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000367
368 if (size <= 0)
369 return 1;
370
371 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600372 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000373
374 if (size < len)
375 console_buffer[size] = '\0';
376
377 len = 2;
378 if (console_buffer[0] != '\0') {
379 local_args[2] = console_buffer;
380 len = 3;
381 }
382
383 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500384 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000385}
Jon Loeligerd704d912007-07-10 11:02:44 -0500386#endif
wdenka68d3ed2002-10-11 08:38:32 +0000387
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600388#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500389static int print_static_binding(const char *var_name, const char *callback_name,
390 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600391{
392 printf("\t%-20s %-20s\n", var_name, callback_name);
393
394 return 0;
395}
396
Simon Glass1a236862019-08-02 09:44:18 -0600397static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600398{
399 struct env_clbk_tbl *clbkp;
400 int i;
401 int num_callbacks;
402
403 if (entry->callback == NULL)
404 return 0;
405
406 /* look up the callback in the linker-list */
407 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
408 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
409 i < num_callbacks;
410 i++, clbkp++) {
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600411 if (entry->callback == clbkp->callback)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600412 break;
413 }
414
415 if (i == num_callbacks)
416 /* this should probably never happen, but just in case... */
417 printf("\t%-20s %p\n", entry->key, entry->callback);
418 else
419 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
420
421 return 0;
422}
423
424/*
425 * Print the callbacks available and what they are bound to
426 */
Simon Glassed38aef2020-05-10 11:40:03 -0600427int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
428 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600429{
430 struct env_clbk_tbl *clbkp;
431 int i;
432 int num_callbacks;
433
434 /* Print the available callbacks */
435 puts("Available callbacks:\n");
436 puts("\tCallback Name\n");
437 puts("\t-------------\n");
438 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
439 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
440 i < num_callbacks;
441 i++, clbkp++)
442 printf("\t%s\n", clbkp->name);
443 puts("\n");
444
445 /* Print the static bindings that may exist */
446 puts("Static callback bindings:\n");
447 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
448 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500449 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600450 puts("\n");
451
452 /* walk through each variable and print the callback if it has one */
453 puts("Active callback bindings:\n");
454 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
455 printf("\t%-20s %-20s\n", "-------------", "-------------");
456 hwalk_r(&env_htab, print_active_callback);
457 return 0;
458}
459#endif
460
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600461#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500462static int print_static_flags(const char *var_name, const char *flags,
463 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600464{
465 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600466 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600467
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600468 printf("\t%-20s %-20s %-20s\n", var_name,
469 env_flags_get_vartype_name(type),
470 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600471
472 return 0;
473}
474
Simon Glass1a236862019-08-02 09:44:18 -0600475static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600476{
477 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600478 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600479
480 if (entry->flags == 0)
481 return 0;
482
483 type = (enum env_flags_vartype)
484 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600485 access = env_flags_parse_varaccess_from_binflags(entry->flags);
486 printf("\t%-20s %-20s %-20s\n", entry->key,
487 env_flags_get_vartype_name(type),
488 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600489
490 return 0;
491}
492
493/*
494 * Print the flags available and what variables have flags
495 */
Simon Glassed38aef2020-05-10 11:40:03 -0600496int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600497{
498 /* Print the available variable types */
499 printf("Available variable type flags (position %d):\n",
500 ENV_FLAGS_VARTYPE_LOC);
501 puts("\tFlag\tVariable Type Name\n");
502 puts("\t----\t------------------\n");
503 env_flags_print_vartypes();
504 puts("\n");
505
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600506 /* Print the available variable access types */
507 printf("Available variable access flags (position %d):\n",
508 ENV_FLAGS_VARACCESS_LOC);
509 puts("\tFlag\tVariable Access Name\n");
510 puts("\t----\t--------------------\n");
511 env_flags_print_varaccess();
512 puts("\n");
513
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600514 /* Print the static flags that may exist */
515 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600516 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
517 "Variable Access");
518 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
519 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500520 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600521 puts("\n");
522
523 /* walk through each variable and print the flags if non-default */
524 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600525 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
526 "Variable Access");
527 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
528 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600529 hwalk_r(&env_htab, print_active_flags);
530 return 0;
531}
532#endif
533
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200534/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500535 * Interactively edit an environment variable
536 */
537#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600538static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
539 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500540{
541 char buffer[CONFIG_SYS_CBSIZE];
542 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500543
Wolfgang Denk3b683112010-07-17 01:06:04 +0200544 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000545 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500546
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500547 /* before import into hashtable */
548 if (!(gd->flags & GD_FLG_ENV_READY))
549 return 1;
550
Peter Tyser0deafa22009-10-25 15:12:56 -0500551 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600552 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500553 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800554 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500555 else
556 buffer[0] = '\0';
557
Simon Glassbe6aafc2014-04-10 20:01:27 -0600558 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000559 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500560
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500561 if (buffer[0] == '\0') {
562 const char * const _argv[3] = { "setenv", argv[1], NULL };
563
564 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
565 } else {
566 const char * const _argv[4] = { "setenv", argv[1], buffer,
567 NULL };
568
569 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
570 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500571}
572#endif /* CONFIG_CMD_EDITENV */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200573
Troy Kisky91229722023-03-13 14:31:24 -0700574#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Simon Glassed38aef2020-05-10 11:40:03 -0600575static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
576 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000577{
Simon Glassd49b8892017-08-03 12:22:08 -0600578 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000579}
wdenk57b2d802003-06-27 21:31:46 +0000580
Mike Frysinger61583932008-12-30 02:59:25 -0500581U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200582 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600583 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200584 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500585);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200586
587#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600588static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
589 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200590{
591 return env_erase() ? 1 : 0;
592}
593
594U_BOOT_CMD(
595 eraseenv, 1, 0, do_env_erase,
596 "erase environment variables from persistent storage",
597 ""
598);
599#endif
wdenka68d3ed2002-10-11 08:38:32 +0000600#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200601
602#if defined(CONFIG_CMD_NVEDIT_LOAD)
603static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
604 char *const argv[])
605{
606 return env_reload() ? 1 : 0;
607}
608#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200609
610#if defined(CONFIG_CMD_NVEDIT_SELECT)
611static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
612 char *const argv[])
613{
614 return env_select(argv[1]) ? 1 : 0;
615}
616#endif
617
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000618#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000619
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000620#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600621static int do_env_default(struct cmd_tbl *cmdtp, int flag,
622 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200623{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300624 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000625
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000626 debug("Initial value for argc=%d\n", argc);
627 while (--argc > 0 && **++argv == '-') {
628 char *arg = *argv;
629
630 while (*++arg) {
631 switch (*arg) {
632 case 'a': /* default all */
633 all = 1;
634 break;
635 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300636 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000637 break;
638 default:
639 return cmd_usage(cmdtp);
640 }
641 }
642 }
643 debug("Final value for argc=%d\n", argc);
644 if (all && (argc == 0)) {
645 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600646 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300647 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000648 return 0;
649 }
650 if (!all && (argc > 0)) {
651 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600652 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000653 return 0;
654 }
655
656 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200657}
658
Simon Glassed38aef2020-05-10 11:40:03 -0600659static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
660 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200661{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600662 int env_flag = H_INTERACTIVE;
663 int ret = 0;
664
665 debug("Initial value for argc=%d\n", argc);
666 while (argc > 1 && **(argv + 1) == '-') {
667 char *arg = *++argv;
668
669 --argc;
670 while (*++arg) {
671 switch (*arg) {
672 case 'f': /* force */
673 env_flag |= H_FORCE;
674 break;
675 default:
676 return CMD_RET_USAGE;
677 }
678 }
679 }
680 debug("Final value for argc=%d\n", argc);
681
682 env_id++;
683
684 while (--argc > 0) {
685 char *name = *++argv;
686
Simon Glass783ef3f2020-11-05 10:33:37 -0700687 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger8335ce72012-12-11 22:16:36 -0600688 ret = 1;
689 }
690
691 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200692}
693
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500694#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200695/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100696 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200697 * -t: export as text format; if size is given, data will be
698 * padded with '\0' bytes; if not, one terminating '\0'
699 * will be added (which is included in the "filesize"
700 * setting so you can for exmple copy this to flash and
701 * keep the termination).
702 * -b: export as binary format (name=value pairs separated by
703 * '\0', list end marked by double "\0\0")
704 * -c: export as checksum protected environment format as
705 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100706 * -s size:
707 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200708 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100709 * var... List of variable names that get included into the
710 * export. Without arguments, the whole environment gets
711 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200712 *
713 * With "-c" and size is NOT given, then the export command will
714 * format the data as currently used for the persistent storage,
715 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400716 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200717 * environment, a "current" redundancy flag. If size is given, this
718 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
719 * checksum and redundancy flag will be inserted.
720 *
721 * With "-b" and "-t", always only the real data (including a
722 * terminating '\0' byte) will be written; here the optional size
723 * argument will be used to make sure not to overflow the user
724 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400725 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200726 *
727 * On successful return, the variable "filesize" will be set.
728 * Note that filesize includes the trailing/terminating '\0' byte(s).
729 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400730 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200731 *
732 * => env export -t 100000
733 * => era ${backup_addr} +${filesize}
734 * => cp.b 100000 ${backup_addr} ${filesize}
735 *
736 * Re-import this snapshot, deleting all other settings:
737 *
738 * => env import -d -t ${backup_addr}
739 */
Simon Glassed38aef2020-05-10 11:40:03 -0600740static int do_env_export(struct cmd_tbl *cmdtp, int flag,
741 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200742{
743 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600744 ulong addr;
745 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100746 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200747 ssize_t len;
748 env_t *envp;
749 char sep = '\n';
750 int chk = 0;
751 int fmt = 0;
752
753 cmd = *argv;
754
755 while (--argc > 0 && **++argv == '-') {
756 char *arg = *argv;
757 while (*++arg) {
758 switch (*arg) {
759 case 'b': /* raw binary format */
760 if (fmt++)
761 goto sep_err;
762 sep = '\0';
763 break;
764 case 'c': /* external checksum format */
765 if (fmt++)
766 goto sep_err;
767 sep = '\0';
768 chk = 1;
769 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100770 case 's': /* size given */
771 if (--argc <= 0)
772 return cmd_usage(cmdtp);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600773 size = hextoul(*++argv, NULL);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100774 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200775 case 't': /* text format */
776 if (fmt++)
777 goto sep_err;
778 sep = '\n';
779 break;
780 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000781 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200782 }
783 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100784NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200785 }
786
Macpaul Linff2a9f32011-04-26 16:16:45 +0000787 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000788 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000789
Simon Glass3ff49ec2021-07-24 09:03:29 -0600790 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600791 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200792
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100793 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600794 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100795
796 argc--;
797 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200798
799 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000800 len = hexport_r(&env_htab, sep,
801 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600802 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200803 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200804 pr_err("## Error: Cannot export environment: errno = %d\n",
805 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200806 return 1;
807 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100808 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600809 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200810
811 return 0;
812 }
813
Simon Glassa7459262013-10-25 23:01:31 -0600814 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200815
816 if (chk) /* export as checksum protected block */
817 res = (char *)envp->data;
818 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -0600819 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200820
Wolfgang Denk565af272013-03-23 23:50:28 +0000821 len = hexport_r(&env_htab, '\0',
822 H_MATCH_KEY | H_MATCH_IDENT,
823 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200824 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200825 pr_err("## Error: Cannot export environment: errno = %d\n",
826 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200827 return 1;
828 }
829
830 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +0000831 envp->crc = crc32(0, envp->data,
832 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200833#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -0600834 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200835#endif
836 }
Simon Glass4d949a22017-08-03 12:22:10 -0600837 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200838
839 return 0;
840
841sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200842 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
843 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200844 return 1;
845}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500846#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200847
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500848#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200849/*
Quentin Schulz0312b942018-07-09 19:16:29 +0200850 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
851 * -d: delete existing environment before importing if no var is
852 * passed; if vars are passed, if one var is in the current
853 * environment but not in the environment at addr, delete var from
854 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400855 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200856 * -t: assume text format; either "size" must be given or the
857 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +0200858 * -r: handle CRLF like LF, that means exported variables with
859 * a content which ends with \r won't get imported. Used
860 * to import text files created with editors which are using CRLF
861 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200862 * -b: assume binary format ('\0' separated, "\0\0" terminated)
863 * -c: assume checksum protected environment format
864 * addr: memory address to read from
865 * size: length of input data; if missing, proper '\0'
866 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +0200867 * if var is set and size should be missing (i.e. '\0'
868 * termination), set size to '-'
869 * var... List of the names of the only variables that get imported from
870 * the environment at address 'addr'. Without arguments, the whole
871 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200872 */
Simon Glassed38aef2020-05-10 11:40:03 -0600873static int do_env_import(struct cmd_tbl *cmdtp, int flag,
874 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200875{
Simon Glassa7459262013-10-25 23:01:31 -0600876 ulong addr;
877 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200878 char sep = '\n';
879 int chk = 0;
880 int fmt = 0;
881 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +0200882 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +0200883 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200884 size_t size;
885
886 cmd = *argv;
887
888 while (--argc > 0 && **++argv == '-') {
889 char *arg = *argv;
890 while (*++arg) {
891 switch (*arg) {
892 case 'b': /* raw binary format */
893 if (fmt++)
894 goto sep_err;
895 sep = '\0';
896 break;
897 case 'c': /* external checksum format */
898 if (fmt++)
899 goto sep_err;
900 sep = '\0';
901 chk = 1;
902 break;
903 case 't': /* text format */
904 if (fmt++)
905 goto sep_err;
906 sep = '\n';
907 break;
Alexander Holler3e12be72014-07-14 17:49:55 +0200908 case 'r': /* handle CRLF like LF */
909 crlf_is_lf = 1;
910 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200911 case 'd':
912 del = 1;
913 break;
914 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000915 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200916 }
917 }
918 }
919
Macpaul Linff2a9f32011-04-26 16:16:45 +0000920 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000921 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200922
923 if (!fmt)
924 printf("## Warning: defaulting to text format\n");
925
Alexander Holler3e12be72014-07-14 17:49:55 +0200926 if (sep != '\n' && crlf_is_lf )
927 crlf_is_lf = 0;
928
Simon Glass3ff49ec2021-07-24 09:03:29 -0600929 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600930 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200931
Quentin Schulz0312b942018-07-09 19:16:29 +0200932 if (argc >= 2 && strcmp(argv[1], "-")) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600933 size = hextoul(argv[1], NULL);
Quentin Schulz0312b942018-07-09 19:16:29 +0200934 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -0500935 puts("## Error: external checksum format must pass size\n");
936 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200937 } else {
Simon Glassa7459262013-10-25 23:01:31 -0600938 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200939
940 size = 0;
941
942 while (size < MAX_ENV_SIZE) {
943 if ((*s == sep) && (*(s+1) == '\0'))
944 break;
945 ++s;
946 ++size;
947 }
948 if (size == MAX_ENV_SIZE) {
949 printf("## Warning: Input data exceeds %d bytes"
950 " - truncated\n", MAX_ENV_SIZE);
951 }
Horst Kronstorfer295da642011-12-16 23:33:10 +0000952 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +0000953 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200954 }
955
Quentin Schulz0312b942018-07-09 19:16:29 +0200956 if (argc > 2)
957 wl = 1;
958
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200959 if (chk) {
960 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -0600961 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200962
Pedro Aguilar834b5522020-08-31 11:01:41 +0200963 if (size <= offsetof(env_t, data)) {
964 printf("## Error: Invalid size 0x%zX\n", size);
965 return 1;
966 }
967
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200968 size -= offsetof(env_t, data);
969 memcpy(&crc, &ep->crc, sizeof(crc));
970
971 if (crc32(0, ep->data, size) != crc) {
972 puts("## Error: bad CRC, import failed\n");
973 return 1;
974 }
Simon Glassa7459262013-10-25 23:01:31 -0600975 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200976 }
977
Quentin Schulz0312b942018-07-09 19:16:29 +0200978 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
979 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200980 pr_err("## Error: Environment import failed: errno = %d\n",
981 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200982 return 1;
983 }
984 gd->flags |= GD_FLG_ENV_READY;
985
986 return 0;
987
988sep_err:
989 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
990 cmd);
991 return 1;
992}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500993#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200994
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -0500995#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
996static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
997 int argc, char *const argv[])
998{
999 char *to = argv[1];
1000 char *from = argv[2];
1001 char *default_value = NULL;
1002 int ret = 0;
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +01001003 char *val;
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001004
1005 if (argc < 3 || argc > 4) {
1006 return CMD_RET_USAGE;
1007 }
1008
1009 if (argc == 4) {
1010 default_value = argv[3];
1011 }
1012
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +01001013 val = env_get(from) ?: default_value;
1014 if (!val) {
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001015 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1016
1017 return CMD_RET_FAILURE;
1018 }
1019
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +01001020 ret = env_set(to, val);
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001021
1022 if (ret == 0) {
1023 return CMD_RET_SUCCESS;
1024 }
1025 else {
1026 return CMD_RET_FAILURE;
1027 }
1028}
1029#endif
1030
Leo Ruan7450b872019-05-24 17:20:19 +02001031#if defined(CONFIG_CMD_NVEDIT_INFO)
1032/*
1033 * print_env_info - print environment information
1034 */
1035static int print_env_info(void)
1036{
1037 const char *value;
1038
1039 /* print environment validity value */
1040 switch (gd->env_valid) {
1041 case ENV_INVALID:
1042 value = "invalid";
1043 break;
1044 case ENV_VALID:
1045 value = "valid";
1046 break;
1047 case ENV_REDUND:
1048 value = "redundant";
1049 break;
1050 default:
1051 value = "unknown";
1052 break;
1053 }
1054 printf("env_valid = %s\n", value);
1055
1056 /* print environment ready flag */
1057 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1058 printf("env_ready = %s\n", value);
1059
1060 /* print environment using default flag */
1061 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1062 printf("env_use_default = %s\n", value);
1063
1064 return CMD_RET_SUCCESS;
1065}
1066
1067#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1068#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1069
1070/*
1071 * env info - display environment information
1072 * env info [-d] - evaluate whether default environment is used
1073 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001074 * Add [-q] - quiet mode, use only for command result, for test by example:
1075 * test env info -p -d -q
Leo Ruan7450b872019-05-24 17:20:19 +02001076 */
Simon Glassed38aef2020-05-10 11:40:03 -06001077static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1078 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +02001079{
1080 int eval_flags = 0;
1081 int eval_results = 0;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001082 bool quiet = false;
Troy Kisky91229722023-03-13 14:31:24 -07001083#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001084 enum env_location loc;
1085#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001086
1087 /* display environment information */
1088 if (argc <= 1)
1089 return print_env_info();
1090
1091 /* process options */
1092 while (--argc > 0 && **++argv == '-') {
1093 char *arg = *argv;
1094
1095 while (*++arg) {
1096 switch (*arg) {
1097 case 'd':
1098 eval_flags |= ENV_INFO_IS_DEFAULT;
1099 break;
1100 case 'p':
1101 eval_flags |= ENV_INFO_IS_PERSISTED;
1102 break;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001103 case 'q':
1104 quiet = true;
1105 break;
Leo Ruan7450b872019-05-24 17:20:19 +02001106 default:
1107 return CMD_RET_USAGE;
1108 }
1109 }
1110 }
1111
1112 /* evaluate whether default environment is used */
1113 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1114 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001115 if (!quiet)
1116 printf("Default environment is used\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001117 eval_results |= ENV_INFO_IS_DEFAULT;
1118 } else {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001119 if (!quiet)
1120 printf("Environment was loaded from persistent storage\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001121 }
1122 }
1123
1124 /* evaluate whether environment can be persisted */
1125 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Troy Kisky91229722023-03-13 14:31:24 -07001126#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001127 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1128 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1129 if (!quiet)
1130 printf("Environment can be persisted\n");
1131 eval_results |= ENV_INFO_IS_PERSISTED;
1132 } else {
1133 if (!quiet)
1134 printf("Environment cannot be persisted\n");
1135 }
Leo Ruan7450b872019-05-24 17:20:19 +02001136#else
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001137 if (!quiet)
1138 printf("Environment cannot be persisted\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001139#endif
1140 }
1141
1142 /* The result of evaluations is combined with AND */
1143 if (eval_flags != eval_results)
1144 return CMD_RET_FAILURE;
1145
1146 return CMD_RET_SUCCESS;
1147}
1148#endif
1149
Andrew Ruder94463402013-10-22 19:07:34 -05001150#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001151static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1152 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001153{
Simon Glass1a236862019-08-02 09:44:18 -06001154 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001155
1156 if (argc < 2)
1157 return CMD_RET_USAGE;
1158
1159 e.key = argv[1];
1160 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001161 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001162
1163 return (ep == NULL) ? 1 : 0;
1164}
1165#endif
1166
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001167/*
1168 * New command line interface: "env" command with subcommands
1169 */
Simon Glassed38aef2020-05-10 11:40:03 -06001170static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001171#if defined(CONFIG_CMD_ASKENV)
1172 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1173#endif
1174 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001175 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001176#if defined(CONFIG_CMD_EDITENV)
1177 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1178#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001179#if defined(CONFIG_CMD_ENV_CALLBACK)
1180 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1181#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001182#if defined(CONFIG_CMD_ENV_FLAGS)
1183 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1184#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001185#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001186 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001187#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001188#if defined(CONFIG_CMD_GREPENV)
1189 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1190#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001191#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001192 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001193#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001194#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1195 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1196#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001197#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001198 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan7450b872019-05-24 17:20:19 +02001199#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001200#if defined(CONFIG_CMD_NVEDIT_LOAD)
1201 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1202#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001203 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1204#if defined(CONFIG_CMD_RUN)
1205 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1206#endif
Troy Kisky91229722023-03-13 14:31:24 -07001207#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001208 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001209#if defined(CONFIG_CMD_ERASEENV)
1210 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1211#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001212#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001213#if defined(CONFIG_CMD_NVEDIT_SELECT)
1214 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1215#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001216 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001217#if defined(CONFIG_CMD_ENV_EXISTS)
1218 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1219#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001220};
1221
Simon Glassed38aef2020-05-10 11:40:03 -06001222static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001223{
Simon Glassed38aef2020-05-10 11:40:03 -06001224 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001225
Thomas Weber4d282652010-11-24 13:07:52 +01001226 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001227 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001228
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001229 /* drop initial "env" arg */
1230 argc--;
1231 argv++;
1232
1233 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1234
1235 if (cp)
1236 return cp->cmd(cmdtp, flag, argc, argv);
1237
Simon Glassa06dfc72011-12-10 08:44:01 +00001238 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001239}
1240
Tom Rini03f146c2023-10-07 15:13:08 -04001241U_BOOT_LONGHELP(env,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001242#if defined(CONFIG_CMD_ASKENV)
1243 "ask name [message] [size] - ask for environment variable\nenv "
1244#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001245#if defined(CONFIG_CMD_ENV_CALLBACK)
1246 "callbacks - print callbacks and their associated variables\nenv "
1247#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001248 "default [-f] -a - [forcibly] reset default environment\n"
1249 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001250 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001251#if defined(CONFIG_CMD_EDITENV)
1252 "env edit name - edit environment variable\n"
1253#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001254#if defined(CONFIG_CMD_ENV_EXISTS)
1255 "env exists name - tests for existence of variable\n"
1256#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001257#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001258 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001259#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001260#if defined(CONFIG_CMD_ENV_FLAGS)
1261 "env flags - print variables that have non-default flags\n"
1262#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001263#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001264#ifdef CONFIG_REGEX
1265 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1266#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001267 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001268#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001269#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001270#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001271 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001272#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001273#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1274 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1275#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001276#if defined(CONFIG_CMD_NVEDIT_INFO)
1277 "env info - display environment information\n"
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001278 "env info [-d] [-p] [-q] - evaluate environment information\n"
1279 " \"-d\": default environment is used\n"
1280 " \"-p\": environment can be persisted\n"
1281 " \"-q\": quiet output\n"
Leo Ruan7450b872019-05-24 17:20:19 +02001282#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001283 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001284#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001285 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001286#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001287#if defined(CONFIG_CMD_RUN)
1288 "env run var [...] - run commands in an environment variable\n"
1289#endif
Troy Kisky91229722023-03-13 14:31:24 -07001290#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001291 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001292#if defined(CONFIG_CMD_ERASEENV)
1293 "env erase - erase environment\n"
1294#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001295#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001296#if defined(CONFIG_CMD_NVEDIT_LOAD)
1297 "env load - load environment\n"
1298#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001299#if defined(CONFIG_CMD_NVEDIT_SELECT)
1300 "env select [target] - select environment target\n"
1301#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001302#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001303 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001304 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001305#endif
Tom Rini03f146c2023-10-07 15:13:08 -04001306 "env set [-f] name [arg ...]\n");
Kim Phillipsdc00a682012-10-29 13:34:31 +00001307
1308U_BOOT_CMD(
1309 env, CONFIG_SYS_MAXARGS, 1, do_env,
1310 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001311);
1312
1313/*
1314 * Old command line interface, kept for compatibility
1315 */
wdenk57b2d802003-06-27 21:31:46 +00001316
Peter Tyser0deafa22009-10-25 15:12:56 -05001317#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001318U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001319 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001320 "edit environment variable",
1321 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001322 " - edit environment variable 'name'",
1323 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001324);
1325#endif
1326
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001327U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001328 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001329 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001330 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001331#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001332 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001333 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001334 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001335 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001336#endif
wdenk57b2d802003-06-27 21:31:46 +00001337 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001338 " - print value of environment variable 'name'",
1339 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001340);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001341
1342#ifdef CONFIG_CMD_GREPENV
1343U_BOOT_CMD_COMPLETE(
1344 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1345 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001346#ifdef CONFIG_REGEX
1347 "[-e] [-n | -v | -b] string ...\n"
1348#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001349 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001350#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001351 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001352#ifdef CONFIG_REGEX
1353 " \"-e\": enable regular expressions;\n"
1354#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001355 " \"-n\": search variable names; \"-v\": search values;\n"
1356 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001357 var_complete
1358);
1359#endif
wdenk57b2d802003-06-27 21:31:46 +00001360
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001361U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001362 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001363 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001364#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001365 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001366 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001367 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001368 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001369 " \"-nv\": set non-volatile attribute\n"
1370 " \"-bs\": set boot-service attribute\n"
1371 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001372 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001373 " \"-a\": append-write\n"
1374 " \"-i addr,size\": use <addr,size> as variable's value\n"
1375 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001376 " - delete UEFI variable 'name' if 'value' not specified\n"
1377#endif
1378 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001379 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1380 "setenv [-f] name\n"
1381 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001382 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001383);
1384
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001385#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001386
wdenkf287a242003-07-01 21:06:45 +00001387U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001388 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001389 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001390 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001391 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001392);
Jon Loeligerd704d912007-07-10 11:02:44 -05001393#endif
wdenk57b2d802003-06-27 21:31:46 +00001394
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001395#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001396U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001397 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001398 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001399 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001400 " - run the commands in the environment variable(s) 'var'",
1401 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001402);
Jon Loeligerd704d912007-07-10 11:02:44 -05001403#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001404#endif /* CONFIG_SPL_BUILD */