blob: 98a687bcabbdd3fc4d29cc55169489aba4661902 [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
Tom Rini5c1444f2024-04-27 08:10:59 -060026#include <config.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
Ilya Yanokf7a2c552012-09-18 00:22:50 +000052#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040053/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020054 * Command interface: print one or all environment variables
55 *
56 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040057 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060058static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000059{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020060 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000061 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000062
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020063 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -060064 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000065
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020066 e.key = name;
67 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -060068 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020069 if (ep == NULL)
70 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +000071 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020072 return len;
73 }
wdenka68d3ed2002-10-11 08:38:32 +000074
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020075 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -060076 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +000077
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020078 if (len > 0) {
79 puts(res);
80 free(res);
81 return len;
wdenka68d3ed2002-10-11 08:38:32 +000082 }
83
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020084 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +000085 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -040087}
wdenka68d3ed2002-10-11 08:38:32 +000088
Simon Glassed38aef2020-05-10 11:40:03 -060089static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
90 char *const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -040091{
92 int i;
93 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -060094 int env_flag = H_HIDE_DOT;
95
AKASHI Takahiro117e68a2019-02-25 15:54:36 +090096#if defined(CONFIG_CMD_NVEDIT_EFI)
97 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
98 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
99#endif
100
Joe Hershberger79a905e2012-12-11 22:16:23 -0600101 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
102 argc--;
103 argv++;
104 env_flag &= ~H_HIDE_DOT;
105 }
wdenka68d3ed2002-10-11 08:38:32 +0000106
Mike Frysinger87944922009-05-24 02:26:19 -0400107 if (argc == 1) {
108 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600109 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400111 return 1;
112 printf("\nEnvironment size: %d/%ld bytes\n",
113 rcode, (ulong)ENV_SIZE);
114 return 0;
115 }
wdenka68d3ed2002-10-11 08:38:32 +0000116
Mike Frysinger87944922009-05-24 02:26:19 -0400117 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600118 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400119 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600120 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200121 if (!rc) {
122 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400123 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000124 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000125 }
126
127 return rcode;
128}
129
130#ifdef CONFIG_CMD_GREPENV
Simon Glassed38aef2020-05-10 11:40:03 -0600131static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
132 int argc, char *const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000133{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000134 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000135 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000136
137 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000138 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000139
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000140 grep_how = H_MATCH_SUBSTR; /* default: substring search */
141 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000142
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200143 while (--argc > 0 && **++argv == '-') {
144 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000145 while (*++arg) {
146 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000147#ifdef CONFIG_REGEX
148 case 'e': /* use regex matching */
149 grep_how = H_MATCH_REGEX;
150 break;
151#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000152 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000153 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000154 break;
155 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000156 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000157 break;
158 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000159 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000160 break;
161 case '-':
162 goto DONE;
163 default:
164 return CMD_RET_USAGE;
165 }
166 }
167 }
168
169DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000170 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000171 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000172 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000173
Wolfgang Denke27fc122013-03-23 23:50:29 +0000174 if (len > 0) {
175 puts(res);
176 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000177 }
Mike Frysinger87944922009-05-24 02:26:19 -0400178
Wolfgang Denke27fc122013-03-23 23:50:29 +0000179 if (len < 2)
180 return 1;
181
182 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000183}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000184#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000185#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000186
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000187#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600188static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
189 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000190{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200191 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000192 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000193
Tom Rinib8ca93f2023-10-26 14:31:16 -0400194 return env_do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000195}
196
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200197/*
wdenka68d3ed2002-10-11 08:38:32 +0000198 * Prompt for environment variable
199 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500200#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600201int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000202{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200203 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000204 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000205 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000206 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000207
208 local_args[0] = argv[0];
209 local_args[1] = argv[1];
210 local_args[2] = NULL;
211 local_args[3] = NULL;
212
Wolfgang Denk586895d2013-02-20 04:53:16 +0000213 /*
214 * Check the syntax:
215 *
216 * env_ask envname [message1 ...] [size]
217 */
218 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000219 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000220
Wolfgang Denk586895d2013-02-20 04:53:16 +0000221 /*
222 * We test the last argument if it can be converted
223 * into a decimal number. If yes, we assume it's
224 * the size. Otherwise we echo it as part of the
225 * message.
226 */
Simon Glassff9b9032021-07-24 09:03:30 -0600227 i = dectoul(argv[argc - 1], &endptr);
Wolfgang Denk586895d2013-02-20 04:53:16 +0000228 if (*endptr != '\0') { /* no size */
229 size = CONFIG_SYS_CBSIZE - 1;
230 } else { /* size given */
231 size = i;
232 --argc;
233 }
wdenka68d3ed2002-10-11 08:38:32 +0000234
Wolfgang Denk586895d2013-02-20 04:53:16 +0000235 if (argc <= 2) {
236 sprintf(message, "Please enter '%s': ", argv[1]);
237 } else {
238 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400239 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000240 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000241 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000242
Tom Rini4beb2352017-09-26 19:37:11 -0400243 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000244 pos += strlen(argv[i]);
245 }
Tom Rini4beb2352017-09-26 19:37:11 -0400246 if (pos < sizeof(message) - 1) {
247 message[pos++] = ' ';
248 message[pos] = '\0';
249 } else
250 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000251 }
252
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200253 if (size >= CONFIG_SYS_CBSIZE)
254 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000255
256 if (size <= 0)
257 return 1;
258
259 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600260 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000261
262 if (size < len)
263 console_buffer[size] = '\0';
264
265 len = 2;
266 if (console_buffer[0] != '\0') {
267 local_args[2] = console_buffer;
268 len = 3;
269 }
270
271 /* Continue calling setenv code */
Tom Rinib8ca93f2023-10-26 14:31:16 -0400272 return env_do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000273}
Jon Loeligerd704d912007-07-10 11:02:44 -0500274#endif
wdenka68d3ed2002-10-11 08:38:32 +0000275
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600276#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500277static int print_static_binding(const char *var_name, const char *callback_name,
278 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600279{
280 printf("\t%-20s %-20s\n", var_name, callback_name);
281
282 return 0;
283}
284
Simon Glass1a236862019-08-02 09:44:18 -0600285static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600286{
287 struct env_clbk_tbl *clbkp;
288 int i;
289 int num_callbacks;
290
291 if (entry->callback == NULL)
292 return 0;
293
294 /* look up the callback in the linker-list */
295 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
296 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
297 i < num_callbacks;
298 i++, clbkp++) {
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600299 if (entry->callback == clbkp->callback)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600300 break;
301 }
302
303 if (i == num_callbacks)
304 /* this should probably never happen, but just in case... */
305 printf("\t%-20s %p\n", entry->key, entry->callback);
306 else
307 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
308
309 return 0;
310}
311
312/*
313 * Print the callbacks available and what they are bound to
314 */
Simon Glassed38aef2020-05-10 11:40:03 -0600315int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
316 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600317{
318 struct env_clbk_tbl *clbkp;
319 int i;
320 int num_callbacks;
321
322 /* Print the available callbacks */
323 puts("Available callbacks:\n");
324 puts("\tCallback Name\n");
325 puts("\t-------------\n");
326 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
327 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
328 i < num_callbacks;
329 i++, clbkp++)
330 printf("\t%s\n", clbkp->name);
331 puts("\n");
332
333 /* Print the static bindings that may exist */
334 puts("Static callback bindings:\n");
335 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
336 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500337 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600338 puts("\n");
339
340 /* walk through each variable and print the callback if it has one */
341 puts("Active callback bindings:\n");
342 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
343 printf("\t%-20s %-20s\n", "-------------", "-------------");
344 hwalk_r(&env_htab, print_active_callback);
345 return 0;
346}
347#endif
348
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600349#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500350static int print_static_flags(const char *var_name, const char *flags,
351 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600352{
353 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600354 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600355
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600356 printf("\t%-20s %-20s %-20s\n", var_name,
357 env_flags_get_vartype_name(type),
358 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600359
360 return 0;
361}
362
Simon Glass1a236862019-08-02 09:44:18 -0600363static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600364{
365 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600366 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600367
368 if (entry->flags == 0)
369 return 0;
370
371 type = (enum env_flags_vartype)
372 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600373 access = env_flags_parse_varaccess_from_binflags(entry->flags);
374 printf("\t%-20s %-20s %-20s\n", entry->key,
375 env_flags_get_vartype_name(type),
376 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600377
378 return 0;
379}
380
381/*
382 * Print the flags available and what variables have flags
383 */
Simon Glassed38aef2020-05-10 11:40:03 -0600384int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600385{
386 /* Print the available variable types */
387 printf("Available variable type flags (position %d):\n",
388 ENV_FLAGS_VARTYPE_LOC);
389 puts("\tFlag\tVariable Type Name\n");
390 puts("\t----\t------------------\n");
391 env_flags_print_vartypes();
392 puts("\n");
393
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600394 /* Print the available variable access types */
395 printf("Available variable access flags (position %d):\n",
396 ENV_FLAGS_VARACCESS_LOC);
397 puts("\tFlag\tVariable Access Name\n");
398 puts("\t----\t--------------------\n");
399 env_flags_print_varaccess();
400 puts("\n");
401
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600402 /* Print the static flags that may exist */
403 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600404 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
405 "Variable Access");
406 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
407 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500408 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600409 puts("\n");
410
411 /* walk through each variable and print the flags if non-default */
412 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600413 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
414 "Variable Access");
415 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
416 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600417 hwalk_r(&env_htab, print_active_flags);
418 return 0;
419}
420#endif
421
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200422/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500423 * Interactively edit an environment variable
424 */
425#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600426static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
427 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500428{
429 char buffer[CONFIG_SYS_CBSIZE];
430 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500431
Wolfgang Denk3b683112010-07-17 01:06:04 +0200432 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000433 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500434
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500435 /* before import into hashtable */
436 if (!(gd->flags & GD_FLG_ENV_READY))
437 return 1;
438
Peter Tyser0deafa22009-10-25 15:12:56 -0500439 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600440 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500441 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800442 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500443 else
444 buffer[0] = '\0';
445
Simon Glassbe6aafc2014-04-10 20:01:27 -0600446 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000447 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500448
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500449 if (buffer[0] == '\0') {
450 const char * const _argv[3] = { "setenv", argv[1], NULL };
451
Tom Rinib8ca93f2023-10-26 14:31:16 -0400452 return env_do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500453 } else {
454 const char * const _argv[4] = { "setenv", argv[1], buffer,
455 NULL };
456
Tom Rinib8ca93f2023-10-26 14:31:16 -0400457 return env_do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500458 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500459}
460#endif /* CONFIG_CMD_EDITENV */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200461
Troy Kisky91229722023-03-13 14:31:24 -0700462#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Simon Glassed38aef2020-05-10 11:40:03 -0600463static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
464 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000465{
Simon Glassd49b8892017-08-03 12:22:08 -0600466 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000467}
wdenk57b2d802003-06-27 21:31:46 +0000468
Mike Frysinger61583932008-12-30 02:59:25 -0500469U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200470 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600471 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200472 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500473);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200474
475#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600476static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
477 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200478{
479 return env_erase() ? 1 : 0;
480}
481
482U_BOOT_CMD(
483 eraseenv, 1, 0, do_env_erase,
484 "erase environment variables from persistent storage",
485 ""
486);
487#endif
wdenka68d3ed2002-10-11 08:38:32 +0000488#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200489
490#if defined(CONFIG_CMD_NVEDIT_LOAD)
491static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
492 char *const argv[])
493{
494 return env_reload() ? 1 : 0;
495}
496#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200497
498#if defined(CONFIG_CMD_NVEDIT_SELECT)
499static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
500 char *const argv[])
501{
502 return env_select(argv[1]) ? 1 : 0;
503}
504#endif
505
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000506#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000507
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000508#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600509static int do_env_default(struct cmd_tbl *cmdtp, int flag,
510 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200511{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300512 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000513
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000514 debug("Initial value for argc=%d\n", argc);
515 while (--argc > 0 && **++argv == '-') {
516 char *arg = *argv;
517
518 while (*++arg) {
519 switch (*arg) {
520 case 'a': /* default all */
521 all = 1;
522 break;
523 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300524 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000525 break;
526 default:
527 return cmd_usage(cmdtp);
528 }
529 }
530 }
531 debug("Final value for argc=%d\n", argc);
532 if (all && (argc == 0)) {
533 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600534 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300535 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000536 return 0;
537 }
538 if (!all && (argc > 0)) {
539 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600540 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000541 return 0;
542 }
543
544 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200545}
546
Simon Glassed38aef2020-05-10 11:40:03 -0600547static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
548 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200549{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600550 int env_flag = H_INTERACTIVE;
551 int ret = 0;
552
553 debug("Initial value for argc=%d\n", argc);
554 while (argc > 1 && **(argv + 1) == '-') {
555 char *arg = *++argv;
556
557 --argc;
558 while (*++arg) {
559 switch (*arg) {
560 case 'f': /* force */
561 env_flag |= H_FORCE;
562 break;
563 default:
564 return CMD_RET_USAGE;
565 }
566 }
567 }
568 debug("Final value for argc=%d\n", argc);
569
Tom Rinib8ca93f2023-10-26 14:31:16 -0400570 env_inc_id();
Joe Hershberger8335ce72012-12-11 22:16:36 -0600571
572 while (--argc > 0) {
573 char *name = *++argv;
574
Simon Glass783ef3f2020-11-05 10:33:37 -0700575 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger8335ce72012-12-11 22:16:36 -0600576 ret = 1;
577 }
578
579 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200580}
581
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500582#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200583/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100584 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200585 * -t: export as text format; if size is given, data will be
586 * padded with '\0' bytes; if not, one terminating '\0'
587 * will be added (which is included in the "filesize"
588 * setting so you can for exmple copy this to flash and
589 * keep the termination).
590 * -b: export as binary format (name=value pairs separated by
591 * '\0', list end marked by double "\0\0")
592 * -c: export as checksum protected environment format as
593 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100594 * -s size:
595 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200596 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100597 * var... List of variable names that get included into the
598 * export. Without arguments, the whole environment gets
599 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200600 *
601 * With "-c" and size is NOT given, then the export command will
602 * format the data as currently used for the persistent storage,
603 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400604 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200605 * environment, a "current" redundancy flag. If size is given, this
606 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
607 * checksum and redundancy flag will be inserted.
608 *
609 * With "-b" and "-t", always only the real data (including a
610 * terminating '\0' byte) will be written; here the optional size
611 * argument will be used to make sure not to overflow the user
612 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400613 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200614 *
615 * On successful return, the variable "filesize" will be set.
616 * Note that filesize includes the trailing/terminating '\0' byte(s).
617 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400618 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200619 *
620 * => env export -t 100000
621 * => era ${backup_addr} +${filesize}
622 * => cp.b 100000 ${backup_addr} ${filesize}
623 *
624 * Re-import this snapshot, deleting all other settings:
625 *
626 * => env import -d -t ${backup_addr}
627 */
Simon Glassed38aef2020-05-10 11:40:03 -0600628static int do_env_export(struct cmd_tbl *cmdtp, int flag,
629 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200630{
631 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600632 ulong addr;
633 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100634 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200635 ssize_t len;
636 env_t *envp;
637 char sep = '\n';
638 int chk = 0;
639 int fmt = 0;
640
641 cmd = *argv;
642
643 while (--argc > 0 && **++argv == '-') {
644 char *arg = *argv;
645 while (*++arg) {
646 switch (*arg) {
647 case 'b': /* raw binary format */
648 if (fmt++)
649 goto sep_err;
650 sep = '\0';
651 break;
652 case 'c': /* external checksum format */
653 if (fmt++)
654 goto sep_err;
655 sep = '\0';
656 chk = 1;
657 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100658 case 's': /* size given */
659 if (--argc <= 0)
660 return cmd_usage(cmdtp);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600661 size = hextoul(*++argv, NULL);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100662 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200663 case 't': /* text format */
664 if (fmt++)
665 goto sep_err;
666 sep = '\n';
667 break;
668 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000669 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200670 }
671 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100672NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200673 }
674
Macpaul Linff2a9f32011-04-26 16:16:45 +0000675 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000676 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000677
Simon Glass3ff49ec2021-07-24 09:03:29 -0600678 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600679 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200680
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100681 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600682 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100683
684 argc--;
685 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200686
687 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000688 len = hexport_r(&env_htab, sep,
689 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600690 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200691 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200692 pr_err("## Error: Cannot export environment: errno = %d\n",
693 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200694 return 1;
695 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100696 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600697 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200698
699 return 0;
700 }
701
Simon Glassa7459262013-10-25 23:01:31 -0600702 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200703
704 if (chk) /* export as checksum protected block */
705 res = (char *)envp->data;
706 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -0600707 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200708
Wolfgang Denk565af272013-03-23 23:50:28 +0000709 len = hexport_r(&env_htab, '\0',
710 H_MATCH_KEY | H_MATCH_IDENT,
711 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200712 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200713 pr_err("## Error: Cannot export environment: errno = %d\n",
714 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200715 return 1;
716 }
717
718 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +0000719 envp->crc = crc32(0, envp->data,
720 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200721#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -0600722 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200723#endif
724 }
Simon Glass4d949a22017-08-03 12:22:10 -0600725 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200726
727 return 0;
728
729sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200730 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
731 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200732 return 1;
733}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500734#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200735
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500736#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200737/*
Quentin Schulz0312b942018-07-09 19:16:29 +0200738 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
739 * -d: delete existing environment before importing if no var is
740 * passed; if vars are passed, if one var is in the current
741 * environment but not in the environment at addr, delete var from
742 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400743 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200744 * -t: assume text format; either "size" must be given or the
745 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +0200746 * -r: handle CRLF like LF, that means exported variables with
747 * a content which ends with \r won't get imported. Used
748 * to import text files created with editors which are using CRLF
749 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200750 * -b: assume binary format ('\0' separated, "\0\0" terminated)
751 * -c: assume checksum protected environment format
752 * addr: memory address to read from
753 * size: length of input data; if missing, proper '\0'
754 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +0200755 * if var is set and size should be missing (i.e. '\0'
756 * termination), set size to '-'
757 * var... List of the names of the only variables that get imported from
758 * the environment at address 'addr'. Without arguments, the whole
759 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200760 */
Simon Glassed38aef2020-05-10 11:40:03 -0600761static int do_env_import(struct cmd_tbl *cmdtp, int flag,
762 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200763{
Simon Glassa7459262013-10-25 23:01:31 -0600764 ulong addr;
765 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200766 char sep = '\n';
767 int chk = 0;
768 int fmt = 0;
769 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +0200770 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +0200771 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200772 size_t size;
773
774 cmd = *argv;
775
776 while (--argc > 0 && **++argv == '-') {
777 char *arg = *argv;
778 while (*++arg) {
779 switch (*arg) {
780 case 'b': /* raw binary format */
781 if (fmt++)
782 goto sep_err;
783 sep = '\0';
784 break;
785 case 'c': /* external checksum format */
786 if (fmt++)
787 goto sep_err;
788 sep = '\0';
789 chk = 1;
790 break;
791 case 't': /* text format */
792 if (fmt++)
793 goto sep_err;
794 sep = '\n';
795 break;
Alexander Holler3e12be72014-07-14 17:49:55 +0200796 case 'r': /* handle CRLF like LF */
797 crlf_is_lf = 1;
798 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200799 case 'd':
800 del = 1;
801 break;
802 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000803 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200804 }
805 }
806 }
807
Macpaul Linff2a9f32011-04-26 16:16:45 +0000808 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000809 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200810
811 if (!fmt)
812 printf("## Warning: defaulting to text format\n");
813
Alexander Holler3e12be72014-07-14 17:49:55 +0200814 if (sep != '\n' && crlf_is_lf )
815 crlf_is_lf = 0;
816
Simon Glass3ff49ec2021-07-24 09:03:29 -0600817 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600818 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200819
Quentin Schulz0312b942018-07-09 19:16:29 +0200820 if (argc >= 2 && strcmp(argv[1], "-")) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600821 size = hextoul(argv[1], NULL);
Quentin Schulz0312b942018-07-09 19:16:29 +0200822 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -0500823 puts("## Error: external checksum format must pass size\n");
824 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200825 } else {
Simon Glassa7459262013-10-25 23:01:31 -0600826 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200827
828 size = 0;
829
830 while (size < MAX_ENV_SIZE) {
831 if ((*s == sep) && (*(s+1) == '\0'))
832 break;
833 ++s;
834 ++size;
835 }
836 if (size == MAX_ENV_SIZE) {
837 printf("## Warning: Input data exceeds %d bytes"
838 " - truncated\n", MAX_ENV_SIZE);
839 }
Horst Kronstorfer295da642011-12-16 23:33:10 +0000840 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +0000841 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200842 }
843
Quentin Schulz0312b942018-07-09 19:16:29 +0200844 if (argc > 2)
845 wl = 1;
846
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200847 if (chk) {
848 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -0600849 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200850
Pedro Aguilar834b5522020-08-31 11:01:41 +0200851 if (size <= offsetof(env_t, data)) {
852 printf("## Error: Invalid size 0x%zX\n", size);
853 return 1;
854 }
855
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200856 size -= offsetof(env_t, data);
857 memcpy(&crc, &ep->crc, sizeof(crc));
858
859 if (crc32(0, ep->data, size) != crc) {
860 puts("## Error: bad CRC, import failed\n");
861 return 1;
862 }
Simon Glassa7459262013-10-25 23:01:31 -0600863 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200864 }
865
Quentin Schulz0312b942018-07-09 19:16:29 +0200866 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
867 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200868 pr_err("## Error: Environment import failed: errno = %d\n",
869 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200870 return 1;
871 }
872 gd->flags |= GD_FLG_ENV_READY;
873
874 return 0;
875
876sep_err:
877 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
878 cmd);
879 return 1;
880}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500881#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200882
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -0500883#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
884static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
885 int argc, char *const argv[])
886{
887 char *to = argv[1];
888 char *from = argv[2];
889 char *default_value = NULL;
890 int ret = 0;
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +0100891 char *val;
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -0500892
893 if (argc < 3 || argc > 4) {
894 return CMD_RET_USAGE;
895 }
896
897 if (argc == 4) {
898 default_value = argv[3];
899 }
900
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +0100901 val = env_get(from) ?: default_value;
902 if (!val) {
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -0500903 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
904
905 return CMD_RET_FAILURE;
906 }
907
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +0100908 ret = env_set(to, val);
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -0500909
910 if (ret == 0) {
911 return CMD_RET_SUCCESS;
912 }
913 else {
914 return CMD_RET_FAILURE;
915 }
916}
917#endif
918
Leo Ruan7450b872019-05-24 17:20:19 +0200919#if defined(CONFIG_CMD_NVEDIT_INFO)
920/*
921 * print_env_info - print environment information
922 */
923static int print_env_info(void)
924{
925 const char *value;
926
927 /* print environment validity value */
928 switch (gd->env_valid) {
929 case ENV_INVALID:
930 value = "invalid";
931 break;
932 case ENV_VALID:
933 value = "valid";
934 break;
935 case ENV_REDUND:
936 value = "redundant";
937 break;
938 default:
939 value = "unknown";
940 break;
941 }
942 printf("env_valid = %s\n", value);
943
944 /* print environment ready flag */
945 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
946 printf("env_ready = %s\n", value);
947
948 /* print environment using default flag */
949 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
950 printf("env_use_default = %s\n", value);
951
952 return CMD_RET_SUCCESS;
953}
954
955#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
956#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
957
958/*
959 * env info - display environment information
960 * env info [-d] - evaluate whether default environment is used
961 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay109ec8f2020-06-19 14:03:34 +0200962 * Add [-q] - quiet mode, use only for command result, for test by example:
963 * test env info -p -d -q
Leo Ruan7450b872019-05-24 17:20:19 +0200964 */
Simon Glassed38aef2020-05-10 11:40:03 -0600965static int do_env_info(struct cmd_tbl *cmdtp, int flag,
966 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +0200967{
968 int eval_flags = 0;
969 int eval_results = 0;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +0200970 bool quiet = false;
Troy Kisky91229722023-03-13 14:31:24 -0700971#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +0200972 enum env_location loc;
973#endif
Leo Ruan7450b872019-05-24 17:20:19 +0200974
975 /* display environment information */
976 if (argc <= 1)
977 return print_env_info();
978
979 /* process options */
980 while (--argc > 0 && **++argv == '-') {
981 char *arg = *argv;
982
983 while (*++arg) {
984 switch (*arg) {
985 case 'd':
986 eval_flags |= ENV_INFO_IS_DEFAULT;
987 break;
988 case 'p':
989 eval_flags |= ENV_INFO_IS_PERSISTED;
990 break;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +0200991 case 'q':
992 quiet = true;
993 break;
Leo Ruan7450b872019-05-24 17:20:19 +0200994 default:
995 return CMD_RET_USAGE;
996 }
997 }
998 }
999
1000 /* evaluate whether default environment is used */
1001 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1002 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001003 if (!quiet)
1004 printf("Default environment is used\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001005 eval_results |= ENV_INFO_IS_DEFAULT;
1006 } else {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001007 if (!quiet)
1008 printf("Environment was loaded from persistent storage\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001009 }
1010 }
1011
1012 /* evaluate whether environment can be persisted */
1013 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Troy Kisky91229722023-03-13 14:31:24 -07001014#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001015 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1016 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1017 if (!quiet)
1018 printf("Environment can be persisted\n");
1019 eval_results |= ENV_INFO_IS_PERSISTED;
1020 } else {
1021 if (!quiet)
1022 printf("Environment cannot be persisted\n");
1023 }
Leo Ruan7450b872019-05-24 17:20:19 +02001024#else
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001025 if (!quiet)
1026 printf("Environment cannot be persisted\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001027#endif
1028 }
1029
1030 /* The result of evaluations is combined with AND */
1031 if (eval_flags != eval_results)
1032 return CMD_RET_FAILURE;
1033
1034 return CMD_RET_SUCCESS;
1035}
1036#endif
1037
Andrew Ruder94463402013-10-22 19:07:34 -05001038#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001039static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1040 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001041{
Simon Glass1a236862019-08-02 09:44:18 -06001042 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001043
1044 if (argc < 2)
1045 return CMD_RET_USAGE;
1046
1047 e.key = argv[1];
1048 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001049 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001050
1051 return (ep == NULL) ? 1 : 0;
1052}
1053#endif
1054
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001055/*
1056 * New command line interface: "env" command with subcommands
1057 */
Simon Glassed38aef2020-05-10 11:40:03 -06001058static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001059#if defined(CONFIG_CMD_ASKENV)
1060 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1061#endif
1062 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001063 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001064#if defined(CONFIG_CMD_EDITENV)
1065 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1066#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001067#if defined(CONFIG_CMD_ENV_CALLBACK)
1068 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1069#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001070#if defined(CONFIG_CMD_ENV_FLAGS)
1071 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1072#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001073#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001074 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001075#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001076#if defined(CONFIG_CMD_GREPENV)
1077 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1078#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001079#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001080 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001081#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001082#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1083 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1084#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001085#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001086 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan7450b872019-05-24 17:20:19 +02001087#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001088#if defined(CONFIG_CMD_NVEDIT_LOAD)
1089 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1090#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001091 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1092#if defined(CONFIG_CMD_RUN)
1093 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1094#endif
Troy Kisky91229722023-03-13 14:31:24 -07001095#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001096 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001097#if defined(CONFIG_CMD_ERASEENV)
1098 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1099#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001100#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001101#if defined(CONFIG_CMD_NVEDIT_SELECT)
1102 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1103#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001104 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001105#if defined(CONFIG_CMD_ENV_EXISTS)
1106 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1107#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001108};
1109
Simon Glassed38aef2020-05-10 11:40:03 -06001110static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001111{
Simon Glassed38aef2020-05-10 11:40:03 -06001112 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001113
Thomas Weber4d282652010-11-24 13:07:52 +01001114 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001115 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001116
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001117 /* drop initial "env" arg */
1118 argc--;
1119 argv++;
1120
1121 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1122
1123 if (cp)
1124 return cp->cmd(cmdtp, flag, argc, argv);
1125
Simon Glassa06dfc72011-12-10 08:44:01 +00001126 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001127}
1128
Tom Rini03f146c2023-10-07 15:13:08 -04001129U_BOOT_LONGHELP(env,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001130#if defined(CONFIG_CMD_ASKENV)
1131 "ask name [message] [size] - ask for environment variable\nenv "
1132#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001133#if defined(CONFIG_CMD_ENV_CALLBACK)
1134 "callbacks - print callbacks and their associated variables\nenv "
1135#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001136 "default [-f] -a - [forcibly] reset default environment\n"
1137 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001138 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001139#if defined(CONFIG_CMD_EDITENV)
1140 "env edit name - edit environment variable\n"
1141#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001142#if defined(CONFIG_CMD_ENV_EXISTS)
1143 "env exists name - tests for existence of variable\n"
1144#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001145#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001146 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001147#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001148#if defined(CONFIG_CMD_ENV_FLAGS)
1149 "env flags - print variables that have non-default flags\n"
1150#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001151#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001152#ifdef CONFIG_REGEX
1153 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1154#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001155 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001156#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001157#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001158#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001159 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001160#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001161#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1162 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1163#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001164#if defined(CONFIG_CMD_NVEDIT_INFO)
1165 "env info - display environment information\n"
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001166 "env info [-d] [-p] [-q] - evaluate environment information\n"
1167 " \"-d\": default environment is used\n"
1168 " \"-p\": environment can be persisted\n"
1169 " \"-q\": quiet output\n"
Leo Ruan7450b872019-05-24 17:20:19 +02001170#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001171 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001172#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001173 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001174#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001175#if defined(CONFIG_CMD_RUN)
1176 "env run var [...] - run commands in an environment variable\n"
1177#endif
Troy Kisky91229722023-03-13 14:31:24 -07001178#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001179 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001180#if defined(CONFIG_CMD_ERASEENV)
1181 "env erase - erase environment\n"
1182#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001183#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001184#if defined(CONFIG_CMD_NVEDIT_LOAD)
1185 "env load - load environment\n"
1186#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001187#if defined(CONFIG_CMD_NVEDIT_SELECT)
1188 "env select [target] - select environment target\n"
1189#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001190#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001191 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001192 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001193#endif
Tom Rini03f146c2023-10-07 15:13:08 -04001194 "env set [-f] name [arg ...]\n");
Kim Phillipsdc00a682012-10-29 13:34:31 +00001195
1196U_BOOT_CMD(
1197 env, CONFIG_SYS_MAXARGS, 1, do_env,
1198 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001199);
1200
1201/*
1202 * Old command line interface, kept for compatibility
1203 */
wdenk57b2d802003-06-27 21:31:46 +00001204
Peter Tyser0deafa22009-10-25 15:12:56 -05001205#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001206U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001207 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001208 "edit environment variable",
1209 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001210 " - edit environment variable 'name'",
1211 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001212);
1213#endif
1214
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001215U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001216 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001217 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001218 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001219#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001220 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001221 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001222 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001223 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001224#endif
wdenk57b2d802003-06-27 21:31:46 +00001225 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001226 " - print value of environment variable 'name'",
1227 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001228);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001229
1230#ifdef CONFIG_CMD_GREPENV
1231U_BOOT_CMD_COMPLETE(
1232 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1233 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001234#ifdef CONFIG_REGEX
1235 "[-e] [-n | -v | -b] string ...\n"
1236#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001237 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001238#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001239 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001240#ifdef CONFIG_REGEX
1241 " \"-e\": enable regular expressions;\n"
1242#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001243 " \"-n\": search variable names; \"-v\": search values;\n"
1244 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001245 var_complete
1246);
1247#endif
wdenk57b2d802003-06-27 21:31:46 +00001248
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001249U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001250 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001251 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001252#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001253 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001254 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001255 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001256 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001257 " \"-nv\": set non-volatile attribute\n"
1258 " \"-bs\": set boot-service attribute\n"
1259 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001260 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001261 " \"-a\": append-write\n"
1262 " \"-i addr,size\": use <addr,size> as variable's value\n"
1263 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001264 " - delete UEFI variable 'name' if 'value' not specified\n"
1265#endif
1266 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001267 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1268 "setenv [-f] name\n"
1269 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001270 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001271);
1272
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001273#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001274
wdenkf287a242003-07-01 21:06:45 +00001275U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001276 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001277 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001278 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001279 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001280);
Jon Loeligerd704d912007-07-10 11:02:44 -05001281#endif
wdenk57b2d802003-06-27 21:31:46 +00001282
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001283#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001284U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001285 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001286 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001287 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001288 " - run the commands in the environment variable(s) 'var'",
1289 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001290);
Jon Loeligerd704d912007-07-10 11:02:44 -05001291#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001292#endif /* CONFIG_SPL_BUILD */