blob: 9e4ee4b0177b9136dc86801f906c50355901e241 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenka68d3ed2002-10-11 08:38:32 +00002/*
Wolfgang Denkea009d42013-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 Phillipsa000b792011-04-05 07:15:14 +00008 *
9 * Copyright 2011 Freescale Semiconductor, Inc.
wdenka68d3ed2002-10-11 08:38:32 +000010 */
11
Wolfgang Denkea882ba2010-06-20 23:33:59 +020012/*
wdenka68d3ed2002-10-11 08:38:32 +000013 * Support for persistent environment data
14 *
Wolfgang Denkea882ba2010-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. Dayfc0b5942016-09-07 14:27:59 -040017 * a double '\0'. The environment is preceded by a 32 bit CRC over
Wolfgang Denkea882ba2010-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 Denkea882ba2010-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 Glass18d66532014-04-10 20:01:25 -060027#include <cli.h>
wdenka68d3ed2002-10-11 08:38:32 +000028#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070029#include <console.h>
Simon Glassf1f0ae62019-08-01 09:46:41 -060030#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060031#include <env_internal.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060032#include <log.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020033#include <search.h>
34#include <errno.h>
Peter Tyser246c6922009-10-25 15:12:56 -050035#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050036#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060037#include <asm/global_data.h>
Simon Glasscd93d622020-05-10 11:40:13 -060038#include <linux/bitops.h>
Simon Glass3db71102019-11-14 12:57:16 -070039#include <u-boot/crc.h>
wdenka68d3ed2002-10-11 08:38:32 +000040#include <linux/stddef.h>
41#include <asm/byteorder.h>
Simon Glassfd37dac2013-10-25 23:01:31 -060042#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000043
Wolfgang Denkd87080b2006-03-31 18:32:53 +020044DECLARE_GLOBAL_DATA_PTR;
45
Wolfgang Denkea882ba2010-06-20 23:33:59 +020046/*
47 * Maximum expected input data size for import command
48 */
49#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000050
wdenka68d3ed2002-10-11 08:38:32 +000051/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020052 * This variable is incremented on each do_env_set(), so it can
Simon Glassf1f0ae62019-08-01 09:46:41 -060053 * be used via env_get_id() as an indication, if the environment
Heiko Schocherda954272009-04-28 08:36:11 +020054 * has changed or not. So it is possible to reread an environment
55 * variable only if the environment was changed ... done so for
56 * example in NetInitLoop()
57 */
Heiko Schocher2f70c492009-02-10 09:38:52 +010058static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000059
Simon Glassf1f0ae62019-08-01 09:46:41 -060060int env_get_id(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +010061{
62 return env_id;
63}
wdenka68d3ed2002-10-11 08:38:32 +000064
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000065#ifndef CONFIG_SPL_BUILD
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040066/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020067 * Command interface: print one or all environment variables
68 *
69 * Returns 0 in case of error, or length of printed string
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040070 */
Joe Hershbergerbe112352012-12-11 22:16:23 -060071static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000072{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020073 char *res = NULL;
Maxime Larocque22a4a6c2012-09-28 05:00:13 +000074 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000075
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076 if (name) { /* print a single name */
Simon Glassdd2408c2019-08-02 09:44:18 -060077 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000078
Wolfgang Denkea882ba2010-06-20 23:33:59 +020079 e.key = name;
80 e.data = NULL;
Simon Glass3f0d6802019-08-01 09:47:09 -060081 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020082 if (ep == NULL)
83 return 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +000084 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020085 return len;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040086 }
wdenka68d3ed2002-10-11 08:38:32 +000087
Wolfgang Denkea882ba2010-06-20 23:33:59 +020088 /* print whole list */
Joe Hershbergerbe112352012-12-11 22:16:23 -060089 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020090
91 if (len > 0) {
92 puts(res);
93 free(res);
94 return len;
95 }
96
97 /* should never happen */
Maxime Larocque22a4a6c2012-09-28 05:00:13 +000098 printf("## Error: cannot export environment\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +020099 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400100}
101
Simon Glass09140112020-05-10 11:40:03 -0600102static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
103 char *const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400104{
105 int i;
106 int rcode = 0;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600107 int env_flag = H_HIDE_DOT;
108
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900109#if defined(CONFIG_CMD_NVEDIT_EFI)
110 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
111 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
112#endif
113
Joe Hershbergerbe112352012-12-11 22:16:23 -0600114 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
115 argc--;
116 argv++;
117 env_flag &= ~H_HIDE_DOT;
118 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400119
120 if (argc == 1) {
121 /* print all env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600122 rcode = env_print(NULL, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200123 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400124 return 1;
125 printf("\nEnvironment size: %d/%ld bytes\n",
126 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000127 return 0;
128 }
129
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400130 /* print selected env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600131 env_flag &= ~H_HIDE_DOT;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400132 for (i = 1; i < argc; ++i) {
Joe Hershbergerbe112352012-12-11 22:16:23 -0600133 int rc = env_print(argv[i], env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200134 if (!rc) {
135 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400136 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000137 }
138 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400139
wdenka68d3ed2002-10-11 08:38:32 +0000140 return rcode;
141}
142
Kim Phillipsa000b792011-04-05 07:15:14 +0000143#ifdef CONFIG_CMD_GREPENV
Simon Glass09140112020-05-10 11:40:03 -0600144static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
145 int argc, char *const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000146{
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000147 char *res = NULL;
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000148 int len, grep_how, grep_what;
Kim Phillipsa000b792011-04-05 07:15:14 +0000149
150 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000151 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000152
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000153 grep_how = H_MATCH_SUBSTR; /* default: substring search */
154 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000155
Pierre Aubert9a832332013-10-08 14:20:27 +0200156 while (--argc > 0 && **++argv == '-') {
157 char *arg = *argv;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000158 while (*++arg) {
159 switch (*arg) {
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000160#ifdef CONFIG_REGEX
161 case 'e': /* use regex matching */
162 grep_how = H_MATCH_REGEX;
163 break;
164#endif
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000165 case 'n': /* grep for name */
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000166 grep_what = H_MATCH_KEY;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000167 break;
168 case 'v': /* grep for value */
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000169 grep_what = H_MATCH_DATA;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000170 break;
171 case 'b': /* grep for both */
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000172 grep_what = H_MATCH_BOTH;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000173 break;
174 case '-':
175 goto DONE;
176 default:
177 return CMD_RET_USAGE;
178 }
179 }
180 }
181
182DONE:
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000183 len = hexport_r(&env_htab, '\n',
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000184 flag | grep_what | grep_how,
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000185 &res, 0, argc, argv);
Kim Phillipsa000b792011-04-05 07:15:14 +0000186
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000187 if (len > 0) {
188 puts(res);
189 free(res);
Kim Phillipsa000b792011-04-05 07:15:14 +0000190 }
191
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000192 if (len < 2)
193 return 1;
194
195 return 0;
Kim Phillipsa000b792011-04-05 07:15:14 +0000196}
197#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000198#endif /* CONFIG_SPL_BUILD */
Kim Phillipsa000b792011-04-05 07:15:14 +0000199
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200200/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000201 * Set a new environment variable,
202 * or replace or delete an existing one.
Joe Hershberger25980902012-12-11 22:16:31 -0600203 */
Simon Glass09140112020-05-10 11:40:03 -0600204static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000205{
206 int i, len;
207 char *name, *value, *s;
Simon Glassdd2408c2019-08-02 09:44:18 -0600208 struct env_entry e, *ep;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000209
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600210 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900211
Simon Glassddeac152023-02-05 15:36:36 -0700212#if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +0900213 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
214 return do_env_set_efi(NULL, flag, --argc, ++argv);
215#endif
216
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600217 while (argc > 1 && **(argv + 1) == '-') {
218 char *arg = *++argv;
219
220 --argc;
221 while (*++arg) {
222 switch (*arg) {
223 case 'f': /* force */
224 env_flag |= H_FORCE;
225 break;
226 default:
227 return CMD_RET_USAGE;
228 }
229 }
230 }
231 debug("Final value for argc=%d\n", argc);
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000232 name = argv[1];
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000233
234 if (strchr(name, '=')) {
235 printf("## Error: illegal character '='"
236 "in variable name \"%s\"\n", name);
237 return 1;
238 }
239
240 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000241
wdenka68d3ed2002-10-11 08:38:32 +0000242 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000243 if (argc < 3 || argv[2] == NULL) {
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600244 int rc = hdelete_r(name, &env_htab, env_flag);
Simon Glass96434a72020-11-05 10:33:37 -0700245
246 /* If the variable didn't exist, don't report an error */
247 return rc && rc != -ENOENT ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000248 }
249
250 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200251 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000252 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000253 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000254 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000255
256 value = malloc(len);
257 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200258 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000259 return 1;
260 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000261 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200262 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000263
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000265 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000266 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000267 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200268 if (s != value)
269 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000270
Igor Grinbergd09b1782011-11-07 01:13:59 +0000271 e.key = name;
272 e.data = value;
Simon Glass3f0d6802019-08-01 09:47:09 -0600273 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200274 free(value);
275 if (!ep) {
276 printf("## Error inserting \"%s\" variable, errno=%d\n",
277 name, errno);
278 return 1;
279 }
wdenka68d3ed2002-10-11 08:38:32 +0000280
wdenka68d3ed2002-10-11 08:38:32 +0000281 return 0;
282}
283
Simon Glass382bee52017-08-03 12:22:09 -0600284int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000285{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200286 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
287
Joe Hershbergera7eb1d62013-04-08 10:32:50 +0000288 /* before import into hashtable */
289 if (!(gd->flags & GD_FLG_ENV_READY))
290 return 1;
291
Igor Grinbergd09b1782011-11-07 01:13:59 +0000292 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershberger94b467b2015-05-20 14:27:21 -0500293 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200294 else
Joe Hershberger94b467b2015-05-20 14:27:21 -0500295 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000296}
297
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000298#ifndef CONFIG_SPL_BUILD
Simon Glass09140112020-05-10 11:40:03 -0600299static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
300 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000301{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200302 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000303 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000304
Joe Hershberger94b467b2015-05-20 14:27:21 -0500305 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000306}
307
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200308/*
wdenka68d3ed2002-10-11 08:38:32 +0000309 * Prompt for environment variable
310 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500311#if defined(CONFIG_CMD_ASKENV)
Simon Glass09140112020-05-10 11:40:03 -0600312int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000313{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200314 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000315 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000316 char *local_args[4];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000317 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000318
319 local_args[0] = argv[0];
320 local_args[1] = argv[1];
321 local_args[2] = NULL;
322 local_args[3] = NULL;
323
Wolfgang Denk7d855912013-02-20 04:53:16 +0000324 /*
325 * Check the syntax:
326 *
327 * env_ask envname [message1 ...] [size]
328 */
329 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000330 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000331
Wolfgang Denk7d855912013-02-20 04:53:16 +0000332 /*
333 * We test the last argument if it can be converted
334 * into a decimal number. If yes, we assume it's
335 * the size. Otherwise we echo it as part of the
336 * message.
337 */
Simon Glass0b1284e2021-07-24 09:03:30 -0600338 i = dectoul(argv[argc - 1], &endptr);
Wolfgang Denk7d855912013-02-20 04:53:16 +0000339 if (*endptr != '\0') { /* no size */
340 size = CONFIG_SYS_CBSIZE - 1;
341 } else { /* size given */
342 size = i;
343 --argc;
344 }
wdenka68d3ed2002-10-11 08:38:32 +0000345
Wolfgang Denk7d855912013-02-20 04:53:16 +0000346 if (argc <= 2) {
347 sprintf(message, "Please enter '%s': ", argv[1]);
348 } else {
349 /* env_ask envname message1 ... messagen [size] */
Tom Rinibf52fcd2017-10-07 11:27:59 -0400350 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linf3c615b2011-04-26 16:16:45 +0000351 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000352 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000353
Tom Rinic6677232017-09-26 19:37:11 -0400354 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000355 pos += strlen(argv[i]);
356 }
Tom Rinic6677232017-09-26 19:37:11 -0400357 if (pos < sizeof(message) - 1) {
358 message[pos++] = ' ';
359 message[pos] = '\0';
360 } else
361 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000362 }
363
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200364 if (size >= CONFIG_SYS_CBSIZE)
365 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000366
367 if (size <= 0)
368 return 1;
369
370 /* prompt for input */
Simon Glasse1bf8242014-04-10 20:01:27 -0600371 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000372
373 if (size < len)
374 console_buffer[size] = '\0';
375
376 len = 2;
377 if (console_buffer[0] != '\0') {
378 local_args[2] = console_buffer;
379 len = 3;
380 }
381
382 /* Continue calling setenv code */
Joe Hershberger94b467b2015-05-20 14:27:21 -0500383 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000384}
Jon Loeliger90253172007-07-10 11:02:44 -0500385#endif
wdenka68d3ed2002-10-11 08:38:32 +0000386
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600387#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500388static int print_static_binding(const char *var_name, const char *callback_name,
389 void *priv)
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600390{
391 printf("\t%-20s %-20s\n", var_name, callback_name);
392
393 return 0;
394}
395
Simon Glassdd2408c2019-08-02 09:44:18 -0600396static int print_active_callback(struct env_entry *entry)
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600397{
398 struct env_clbk_tbl *clbkp;
399 int i;
400 int num_callbacks;
401
402 if (entry->callback == NULL)
403 return 0;
404
405 /* look up the callback in the linker-list */
406 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
407 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
408 i < num_callbacks;
409 i++, clbkp++) {
410#if defined(CONFIG_NEEDS_MANUAL_RELOC)
411 if (entry->callback == clbkp->callback + gd->reloc_off)
412#else
413 if (entry->callback == clbkp->callback)
414#endif
415 break;
416 }
417
418 if (i == num_callbacks)
419 /* this should probably never happen, but just in case... */
420 printf("\t%-20s %p\n", entry->key, entry->callback);
421 else
422 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
423
424 return 0;
425}
426
427/*
428 * Print the callbacks available and what they are bound to
429 */
Simon Glass09140112020-05-10 11:40:03 -0600430int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
431 char *const argv[])
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600432{
433 struct env_clbk_tbl *clbkp;
434 int i;
435 int num_callbacks;
436
437 /* Print the available callbacks */
438 puts("Available callbacks:\n");
439 puts("\tCallback Name\n");
440 puts("\t-------------\n");
441 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
442 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
443 i < num_callbacks;
444 i++, clbkp++)
445 printf("\t%s\n", clbkp->name);
446 puts("\n");
447
448 /* Print the static bindings that may exist */
449 puts("Static callback bindings:\n");
450 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
451 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500452 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600453 puts("\n");
454
455 /* walk through each variable and print the callback if it has one */
456 puts("Active callback bindings:\n");
457 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
458 printf("\t%-20s %-20s\n", "-------------", "-------------");
459 hwalk_r(&env_htab, print_active_callback);
460 return 0;
461}
462#endif
463
Joe Hershbergerfffad712012-12-11 22:16:33 -0600464#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500465static int print_static_flags(const char *var_name, const char *flags,
466 void *priv)
Joe Hershbergerfffad712012-12-11 22:16:33 -0600467{
468 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600469 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600470
Joe Hershberger267541f2012-12-11 22:16:34 -0600471 printf("\t%-20s %-20s %-20s\n", var_name,
472 env_flags_get_vartype_name(type),
473 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600474
475 return 0;
476}
477
Simon Glassdd2408c2019-08-02 09:44:18 -0600478static int print_active_flags(struct env_entry *entry)
Joe Hershbergerfffad712012-12-11 22:16:33 -0600479{
480 enum env_flags_vartype type;
Joe Hershberger267541f2012-12-11 22:16:34 -0600481 enum env_flags_varaccess access;
Joe Hershbergerfffad712012-12-11 22:16:33 -0600482
483 if (entry->flags == 0)
484 return 0;
485
486 type = (enum env_flags_vartype)
487 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger267541f2012-12-11 22:16:34 -0600488 access = env_flags_parse_varaccess_from_binflags(entry->flags);
489 printf("\t%-20s %-20s %-20s\n", entry->key,
490 env_flags_get_vartype_name(type),
491 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600492
493 return 0;
494}
495
496/*
497 * Print the flags available and what variables have flags
498 */
Simon Glass09140112020-05-10 11:40:03 -0600499int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergerfffad712012-12-11 22:16:33 -0600500{
501 /* Print the available variable types */
502 printf("Available variable type flags (position %d):\n",
503 ENV_FLAGS_VARTYPE_LOC);
504 puts("\tFlag\tVariable Type Name\n");
505 puts("\t----\t------------------\n");
506 env_flags_print_vartypes();
507 puts("\n");
508
Joe Hershberger267541f2012-12-11 22:16:34 -0600509 /* Print the available variable access types */
510 printf("Available variable access flags (position %d):\n",
511 ENV_FLAGS_VARACCESS_LOC);
512 puts("\tFlag\tVariable Access Name\n");
513 puts("\t----\t--------------------\n");
514 env_flags_print_varaccess();
515 puts("\n");
516
Joe Hershbergerfffad712012-12-11 22:16:33 -0600517 /* Print the static flags that may exist */
518 puts("Static flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600519 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
520 "Variable Access");
521 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
522 "---------------");
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500523 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600524 puts("\n");
525
526 /* walk through each variable and print the flags if non-default */
527 puts("Active flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600528 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
529 "Variable Access");
530 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
531 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600532 hwalk_r(&env_htab, print_active_flags);
533 return 0;
534}
535#endif
536
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200537/*
Peter Tyser246c6922009-10-25 15:12:56 -0500538 * Interactively edit an environment variable
539 */
540#if defined(CONFIG_CMD_EDITENV)
Simon Glass09140112020-05-10 11:40:03 -0600541static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
542 char *const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500543{
544 char buffer[CONFIG_SYS_CBSIZE];
545 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500546
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200547 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000548 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500549
Joe Hershberger94b467b2015-05-20 14:27:21 -0500550 /* before import into hashtable */
551 if (!(gd->flags & GD_FLG_ENV_READY))
552 return 1;
553
Peter Tyser246c6922009-10-25 15:12:56 -0500554 /* Set read buffer to initial value or empty sting */
Simon Glass00caae62017-08-03 12:22:12 -0600555 init_val = env_get(argv[1]);
Peter Tyser246c6922009-10-25 15:12:56 -0500556 if (init_val)
Peng Fan5d49b4c2015-12-23 12:08:09 +0800557 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500558 else
559 buffer[0] = '\0';
560
Simon Glasse1bf8242014-04-10 20:01:27 -0600561 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger18a3cce2013-02-08 10:12:34 +0000562 return 1;
Peter Tyser246c6922009-10-25 15:12:56 -0500563
Joe Hershberger94b467b2015-05-20 14:27:21 -0500564 if (buffer[0] == '\0') {
565 const char * const _argv[3] = { "setenv", argv[1], NULL };
566
567 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
568 } else {
569 const char * const _argv[4] = { "setenv", argv[1], buffer,
570 NULL };
571
572 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
573 }
Peter Tyser246c6922009-10-25 15:12:56 -0500574}
575#endif /* CONFIG_CMD_EDITENV */
576
Troy Kisky289aa6a2023-03-13 14:31:24 -0700577#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Simon Glass09140112020-05-10 11:40:03 -0600578static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
579 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000580{
Simon Glass01510092017-08-03 12:22:08 -0600581 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000582}
wdenk8bde7f72003-06-27 21:31:46 +0000583
Mike Frysingerba69dc22008-12-30 02:59:25 -0500584U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200585 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600586 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200587 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500588);
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200589
590#if defined(CONFIG_CMD_ERASEENV)
Simon Glass09140112020-05-10 11:40:03 -0600591static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
592 char *const argv[])
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200593{
594 return env_erase() ? 1 : 0;
595}
596
597U_BOOT_CMD(
598 eraseenv, 1, 0, do_env_erase,
599 "erase environment variables from persistent storage",
600 ""
601);
602#endif
wdenka68d3ed2002-10-11 08:38:32 +0000603#endif
Patrick Delaunay0115dd32020-07-28 11:51:20 +0200604
605#if defined(CONFIG_CMD_NVEDIT_LOAD)
606static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
607 char *const argv[])
608{
609 return env_reload() ? 1 : 0;
610}
611#endif
Patrick Delaunaya97d22e2020-07-28 11:51:21 +0200612
613#if defined(CONFIG_CMD_NVEDIT_SELECT)
614static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
615 char *const argv[])
616{
617 return env_select(argv[1]) ? 1 : 0;
618}
619#endif
620
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000621#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000622
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000623#ifndef CONFIG_SPL_BUILD
Simon Glass09140112020-05-10 11:40:03 -0600624static int do_env_default(struct cmd_tbl *cmdtp, int flag,
625 int argc, char *const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200626{
Yaniv Levinsky5a042642018-06-24 19:16:56 +0300627 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000628
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000629 debug("Initial value for argc=%d\n", argc);
630 while (--argc > 0 && **++argv == '-') {
631 char *arg = *argv;
632
633 while (*++arg) {
634 switch (*arg) {
635 case 'a': /* default all */
636 all = 1;
637 break;
638 case 'f': /* force */
Yaniv Levinsky30091492018-06-24 19:16:54 +0300639 env_flag |= H_FORCE;
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000640 break;
641 default:
642 return cmd_usage(cmdtp);
643 }
644 }
645 }
646 debug("Final value for argc=%d\n", argc);
647 if (all && (argc == 0)) {
648 /* Reset the whole environment */
Simon Glass0ac7d722019-08-01 09:47:00 -0600649 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300650 env_flag);
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000651 return 0;
652 }
653 if (!all && (argc > 0)) {
654 /* Reset individual variables */
Simon Glass0b9d8a02019-08-01 09:46:56 -0600655 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000656 return 0;
657 }
658
659 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200660}
wdenk8bde7f72003-06-27 21:31:46 +0000661
Simon Glass09140112020-05-10 11:40:03 -0600662static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
663 int argc, char *const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200664{
Joe Hershberger9d8d6612012-12-11 22:16:36 -0600665 int env_flag = H_INTERACTIVE;
666 int ret = 0;
667
668 debug("Initial value for argc=%d\n", argc);
669 while (argc > 1 && **(argv + 1) == '-') {
670 char *arg = *++argv;
671
672 --argc;
673 while (*++arg) {
674 switch (*arg) {
675 case 'f': /* force */
676 env_flag |= H_FORCE;
677 break;
678 default:
679 return CMD_RET_USAGE;
680 }
681 }
682 }
683 debug("Final value for argc=%d\n", argc);
684
685 env_id++;
686
687 while (--argc > 0) {
688 char *name = *++argv;
689
Simon Glass96434a72020-11-05 10:33:37 -0700690 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger9d8d6612012-12-11 22:16:36 -0600691 ret = 1;
692 }
693
694 return ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200695}
696
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500697#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200698/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100699 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200700 * -t: export as text format; if size is given, data will be
701 * padded with '\0' bytes; if not, one terminating '\0'
702 * will be added (which is included in the "filesize"
703 * setting so you can for exmple copy this to flash and
704 * keep the termination).
705 * -b: export as binary format (name=value pairs separated by
706 * '\0', list end marked by double "\0\0")
707 * -c: export as checksum protected environment format as
708 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100709 * -s size:
710 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200711 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100712 * var... List of variable names that get included into the
713 * export. Without arguments, the whole environment gets
714 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200715 *
716 * With "-c" and size is NOT given, then the export command will
717 * format the data as currently used for the persistent storage,
718 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -0400719 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200720 * environment, a "current" redundancy flag. If size is given, this
721 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
722 * checksum and redundancy flag will be inserted.
723 *
724 * With "-b" and "-t", always only the real data (including a
725 * terminating '\0' byte) will be written; here the optional size
726 * argument will be used to make sure not to overflow the user
727 * provided buffer; the command will abort if the size is not
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -0400728 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200729 *
730 * On successful return, the variable "filesize" will be set.
731 * Note that filesize includes the trailing/terminating '\0' byte(s).
732 *
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -0400733 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200734 *
735 * => env export -t 100000
736 * => era ${backup_addr} +${filesize}
737 * => cp.b 100000 ${backup_addr} ${filesize}
738 *
739 * Re-import this snapshot, deleting all other settings:
740 *
741 * => env import -d -t ${backup_addr}
742 */
Simon Glass09140112020-05-10 11:40:03 -0600743static int do_env_export(struct cmd_tbl *cmdtp, int flag,
744 int argc, char *const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200745{
746 char buf[32];
Simon Glassfd37dac2013-10-25 23:01:31 -0600747 ulong addr;
748 char *ptr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100749 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200750 ssize_t len;
751 env_t *envp;
752 char sep = '\n';
753 int chk = 0;
754 int fmt = 0;
755
756 cmd = *argv;
757
758 while (--argc > 0 && **++argv == '-') {
759 char *arg = *argv;
760 while (*++arg) {
761 switch (*arg) {
762 case 'b': /* raw binary format */
763 if (fmt++)
764 goto sep_err;
765 sep = '\0';
766 break;
767 case 'c': /* external checksum format */
768 if (fmt++)
769 goto sep_err;
770 sep = '\0';
771 chk = 1;
772 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100773 case 's': /* size given */
774 if (--argc <= 0)
775 return cmd_usage(cmdtp);
Simon Glass7e5f4602021-07-24 09:03:29 -0600776 size = hextoul(*++argv, NULL);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100777 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200778 case 't': /* text format */
779 if (fmt++)
780 goto sep_err;
781 sep = '\n';
782 break;
783 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000784 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200785 }
786 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100787NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200788 }
789
Macpaul Linf3c615b2011-04-26 16:16:45 +0000790 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000791 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200792
Simon Glass7e5f4602021-07-24 09:03:29 -0600793 addr = hextoul(argv[0], NULL);
Simon Glassfd37dac2013-10-25 23:01:31 -0600794 ptr = map_sysmem(addr, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200795
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100796 if (size)
Simon Glassfd37dac2013-10-25 23:01:31 -0600797 memset(ptr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100798
799 argc--;
800 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200801
802 if (sep) { /* export as text file */
Wolfgang Denkea009d42013-03-23 23:50:28 +0000803 len = hexport_r(&env_htab, sep,
804 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassfd37dac2013-10-25 23:01:31 -0600805 &ptr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200806 if (len < 0) {
Quentin Schulz6c90f622018-07-09 19:16:25 +0200807 pr_err("## Error: Cannot export environment: errno = %d\n",
808 errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200809 return 1;
810 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100811 sprintf(buf, "%zX", (size_t)len);
Simon Glass382bee52017-08-03 12:22:09 -0600812 env_set("filesize", buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200813
814 return 0;
815 }
816
Simon Glassfd37dac2013-10-25 23:01:31 -0600817 envp = (env_t *)ptr;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200818
819 if (chk) /* export as checksum protected block */
820 res = (char *)envp->data;
821 else /* export as raw binary data */
Simon Glassfd37dac2013-10-25 23:01:31 -0600822 res = ptr;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200823
Wolfgang Denkea009d42013-03-23 23:50:28 +0000824 len = hexport_r(&env_htab, '\0',
825 H_MATCH_KEY | H_MATCH_IDENT,
826 &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200827 if (len < 0) {
Quentin Schulz6c90f622018-07-09 19:16:25 +0200828 pr_err("## Error: Cannot export environment: errno = %d\n",
829 errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200830 return 1;
831 }
832
833 if (chk) {
Neil Staintond71b0292018-09-12 11:02:51 +0000834 envp->crc = crc32(0, envp->data,
835 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200836#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glassd3716dd2019-08-01 09:47:08 -0600837 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200838#endif
839 }
Simon Glass018f5302017-08-03 12:22:10 -0600840 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200841
842 return 0;
843
844sep_err:
Quentin Schulz6c90f622018-07-09 19:16:25 +0200845 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
846 cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200847 return 1;
848}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500849#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200850
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500851#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200852/*
Quentin Schulzeaf73472018-07-09 19:16:29 +0200853 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
854 * -d: delete existing environment before importing if no var is
855 * passed; if vars are passed, if one var is in the current
856 * environment but not in the environment at addr, delete var from
857 * current environment;
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -0400858 * otherwise overwrite / append to existing definitions
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200859 * -t: assume text format; either "size" must be given or the
860 * text data must be '\0' terminated
Alexander Hollerecd14462014-07-14 17:49:55 +0200861 * -r: handle CRLF like LF, that means exported variables with
862 * a content which ends with \r won't get imported. Used
863 * to import text files created with editors which are using CRLF
864 * for line endings. Only effective in addition to -t.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200865 * -b: assume binary format ('\0' separated, "\0\0" terminated)
866 * -c: assume checksum protected environment format
867 * addr: memory address to read from
868 * size: length of input data; if missing, proper '\0'
869 * termination is mandatory
Quentin Schulzeaf73472018-07-09 19:16:29 +0200870 * if var is set and size should be missing (i.e. '\0'
871 * termination), set size to '-'
872 * var... List of the names of the only variables that get imported from
873 * the environment at address 'addr'. Without arguments, the whole
874 * environment gets imported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200875 */
Simon Glass09140112020-05-10 11:40:03 -0600876static int do_env_import(struct cmd_tbl *cmdtp, int flag,
877 int argc, char *const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200878{
Simon Glassfd37dac2013-10-25 23:01:31 -0600879 ulong addr;
880 char *cmd, *ptr;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200881 char sep = '\n';
882 int chk = 0;
883 int fmt = 0;
884 int del = 0;
Alexander Hollerecd14462014-07-14 17:49:55 +0200885 int crlf_is_lf = 0;
Quentin Schulzeaf73472018-07-09 19:16:29 +0200886 int wl = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200887 size_t size;
888
889 cmd = *argv;
890
891 while (--argc > 0 && **++argv == '-') {
892 char *arg = *argv;
893 while (*++arg) {
894 switch (*arg) {
895 case 'b': /* raw binary format */
896 if (fmt++)
897 goto sep_err;
898 sep = '\0';
899 break;
900 case 'c': /* external checksum format */
901 if (fmt++)
902 goto sep_err;
903 sep = '\0';
904 chk = 1;
905 break;
906 case 't': /* text format */
907 if (fmt++)
908 goto sep_err;
909 sep = '\n';
910 break;
Alexander Hollerecd14462014-07-14 17:49:55 +0200911 case 'r': /* handle CRLF like LF */
912 crlf_is_lf = 1;
913 break;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200914 case 'd':
915 del = 1;
916 break;
917 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000918 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200919 }
920 }
921 }
922
Macpaul Linf3c615b2011-04-26 16:16:45 +0000923 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000924 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200925
926 if (!fmt)
927 printf("## Warning: defaulting to text format\n");
928
Alexander Hollerecd14462014-07-14 17:49:55 +0200929 if (sep != '\n' && crlf_is_lf )
930 crlf_is_lf = 0;
931
Simon Glass7e5f4602021-07-24 09:03:29 -0600932 addr = hextoul(argv[0], NULL);
Simon Glassfd37dac2013-10-25 23:01:31 -0600933 ptr = map_sysmem(addr, 0);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200934
Quentin Schulzeaf73472018-07-09 19:16:29 +0200935 if (argc >= 2 && strcmp(argv[1], "-")) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600936 size = hextoul(argv[1], NULL);
Quentin Schulzeaf73472018-07-09 19:16:29 +0200937 } else if (chk) {
Tom Rini3775dcd2014-03-04 15:52:35 -0500938 puts("## Error: external checksum format must pass size\n");
939 return CMD_RET_FAILURE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200940 } else {
Simon Glassfd37dac2013-10-25 23:01:31 -0600941 char *s = ptr;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200942
943 size = 0;
944
945 while (size < MAX_ENV_SIZE) {
946 if ((*s == sep) && (*(s+1) == '\0'))
947 break;
948 ++s;
949 ++size;
950 }
951 if (size == MAX_ENV_SIZE) {
952 printf("## Warning: Input data exceeds %d bytes"
953 " - truncated\n", MAX_ENV_SIZE);
954 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000955 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +0000956 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200957 }
958
Quentin Schulzeaf73472018-07-09 19:16:29 +0200959 if (argc > 2)
960 wl = 1;
961
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200962 if (chk) {
963 uint32_t crc;
Simon Glassfd37dac2013-10-25 23:01:31 -0600964 env_t *ep = (env_t *)ptr;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200965
Pedro Aguilar142775a2020-08-31 11:01:41 +0200966 if (size <= offsetof(env_t, data)) {
967 printf("## Error: Invalid size 0x%zX\n", size);
968 return 1;
969 }
970
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200971 size -= offsetof(env_t, data);
972 memcpy(&crc, &ep->crc, sizeof(crc));
973
974 if (crc32(0, ep->data, size) != crc) {
975 puts("## Error: bad CRC, import failed\n");
976 return 1;
977 }
Simon Glassfd37dac2013-10-25 23:01:31 -0600978 ptr = (char *)ep->data;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200979 }
980
Quentin Schulzeaf73472018-07-09 19:16:29 +0200981 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
982 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz6c90f622018-07-09 19:16:25 +0200983 pr_err("## Error: Environment import failed: errno = %d\n",
984 errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200985 return 1;
986 }
987 gd->flags |= GD_FLG_ENV_READY;
988
989 return 0;
990
991sep_err:
992 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
993 cmd);
994 return 1;
995}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500996#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200997
Samuel Dionne-Rielec57bd72021-12-20 18:31:56 -0500998#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
999static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
1000 int argc, char *const argv[])
1001{
1002 char *to = argv[1];
1003 char *from = argv[2];
1004 char *default_value = NULL;
1005 int ret = 0;
Rasmus Villemoes732b0822023-03-06 14:27:21 +01001006 char *val;
Samuel Dionne-Rielec57bd72021-12-20 18:31:56 -05001007
1008 if (argc < 3 || argc > 4) {
1009 return CMD_RET_USAGE;
1010 }
1011
1012 if (argc == 4) {
1013 default_value = argv[3];
1014 }
1015
Rasmus Villemoes732b0822023-03-06 14:27:21 +01001016 val = env_get(from) ?: default_value;
1017 if (!val) {
Samuel Dionne-Rielec57bd72021-12-20 18:31:56 -05001018 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1019
1020 return CMD_RET_FAILURE;
1021 }
1022
Rasmus Villemoes732b0822023-03-06 14:27:21 +01001023 ret = env_set(to, val);
Samuel Dionne-Rielec57bd72021-12-20 18:31:56 -05001024
1025 if (ret == 0) {
1026 return CMD_RET_SUCCESS;
1027 }
1028 else {
1029 return CMD_RET_FAILURE;
1030 }
1031}
1032#endif
1033
Leo Ruan8e921202019-05-24 17:20:19 +02001034#if defined(CONFIG_CMD_NVEDIT_INFO)
1035/*
1036 * print_env_info - print environment information
1037 */
1038static int print_env_info(void)
1039{
1040 const char *value;
1041
1042 /* print environment validity value */
1043 switch (gd->env_valid) {
1044 case ENV_INVALID:
1045 value = "invalid";
1046 break;
1047 case ENV_VALID:
1048 value = "valid";
1049 break;
1050 case ENV_REDUND:
1051 value = "redundant";
1052 break;
1053 default:
1054 value = "unknown";
1055 break;
1056 }
1057 printf("env_valid = %s\n", value);
1058
1059 /* print environment ready flag */
1060 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1061 printf("env_ready = %s\n", value);
1062
1063 /* print environment using default flag */
1064 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1065 printf("env_use_default = %s\n", value);
1066
1067 return CMD_RET_SUCCESS;
1068}
1069
1070#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1071#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1072
1073/*
1074 * env info - display environment information
1075 * env info [-d] - evaluate whether default environment is used
1076 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001077 * Add [-q] - quiet mode, use only for command result, for test by example:
1078 * test env info -p -d -q
Leo Ruan8e921202019-05-24 17:20:19 +02001079 */
Simon Glass09140112020-05-10 11:40:03 -06001080static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1081 int argc, char *const argv[])
Leo Ruan8e921202019-05-24 17:20:19 +02001082{
1083 int eval_flags = 0;
1084 int eval_results = 0;
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001085 bool quiet = false;
Troy Kisky289aa6a2023-03-13 14:31:24 -07001086#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Patrick Delaunay2f96b322020-06-19 14:03:35 +02001087 enum env_location loc;
1088#endif
Leo Ruan8e921202019-05-24 17:20:19 +02001089
1090 /* display environment information */
1091 if (argc <= 1)
1092 return print_env_info();
1093
1094 /* process options */
1095 while (--argc > 0 && **++argv == '-') {
1096 char *arg = *argv;
1097
1098 while (*++arg) {
1099 switch (*arg) {
1100 case 'd':
1101 eval_flags |= ENV_INFO_IS_DEFAULT;
1102 break;
1103 case 'p':
1104 eval_flags |= ENV_INFO_IS_PERSISTED;
1105 break;
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001106 case 'q':
1107 quiet = true;
1108 break;
Leo Ruan8e921202019-05-24 17:20:19 +02001109 default:
1110 return CMD_RET_USAGE;
1111 }
1112 }
1113 }
1114
1115 /* evaluate whether default environment is used */
1116 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1117 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001118 if (!quiet)
1119 printf("Default environment is used\n");
Leo Ruan8e921202019-05-24 17:20:19 +02001120 eval_results |= ENV_INFO_IS_DEFAULT;
1121 } else {
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001122 if (!quiet)
1123 printf("Environment was loaded from persistent storage\n");
Leo Ruan8e921202019-05-24 17:20:19 +02001124 }
1125 }
1126
1127 /* evaluate whether environment can be persisted */
1128 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Troy Kisky289aa6a2023-03-13 14:31:24 -07001129#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Patrick Delaunay2f96b322020-06-19 14:03:35 +02001130 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1131 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1132 if (!quiet)
1133 printf("Environment can be persisted\n");
1134 eval_results |= ENV_INFO_IS_PERSISTED;
1135 } else {
1136 if (!quiet)
1137 printf("Environment cannot be persisted\n");
1138 }
Leo Ruan8e921202019-05-24 17:20:19 +02001139#else
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001140 if (!quiet)
1141 printf("Environment cannot be persisted\n");
Leo Ruan8e921202019-05-24 17:20:19 +02001142#endif
1143 }
1144
1145 /* The result of evaluations is combined with AND */
1146 if (eval_flags != eval_results)
1147 return CMD_RET_FAILURE;
1148
1149 return CMD_RET_SUCCESS;
1150}
1151#endif
1152
Andrew Ruder88733e22013-10-22 19:07:34 -05001153#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glass09140112020-05-10 11:40:03 -06001154static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1155 char *const argv[])
Andrew Ruder88733e22013-10-22 19:07:34 -05001156{
Simon Glassdd2408c2019-08-02 09:44:18 -06001157 struct env_entry e, *ep;
Andrew Ruder88733e22013-10-22 19:07:34 -05001158
1159 if (argc < 2)
1160 return CMD_RET_USAGE;
1161
1162 e.key = argv[1];
1163 e.data = NULL;
Simon Glass3f0d6802019-08-01 09:47:09 -06001164 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder88733e22013-10-22 19:07:34 -05001165
1166 return (ep == NULL) ? 1 : 0;
1167}
1168#endif
1169
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001170/*
1171 * New command line interface: "env" command with subcommands
1172 */
Simon Glass09140112020-05-10 11:40:03 -06001173static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001174#if defined(CONFIG_CMD_ASKENV)
1175 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1176#endif
1177 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001178 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001179#if defined(CONFIG_CMD_EDITENV)
1180 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1181#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001182#if defined(CONFIG_CMD_ENV_CALLBACK)
1183 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1184#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001185#if defined(CONFIG_CMD_ENV_FLAGS)
1186 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1187#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001188#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001189 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001190#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001191#if defined(CONFIG_CMD_GREPENV)
1192 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1193#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001194#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001195 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001196#endif
Samuel Dionne-Rielec57bd72021-12-20 18:31:56 -05001197#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1198 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1199#endif
Leo Ruan8e921202019-05-24 17:20:19 +02001200#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001201 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan8e921202019-05-24 17:20:19 +02001202#endif
Patrick Delaunay0115dd32020-07-28 11:51:20 +02001203#if defined(CONFIG_CMD_NVEDIT_LOAD)
1204 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1205#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001206 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1207#if defined(CONFIG_CMD_RUN)
1208 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1209#endif
Troy Kisky289aa6a2023-03-13 14:31:24 -07001210#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001211 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlichcd121bd2019-06-29 11:36:19 +02001212#if defined(CONFIG_CMD_ERASEENV)
1213 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1214#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001215#endif
Patrick Delaunaya97d22e2020-07-28 11:51:21 +02001216#if defined(CONFIG_CMD_NVEDIT_SELECT)
1217 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1218#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001219 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder88733e22013-10-22 19:07:34 -05001220#if defined(CONFIG_CMD_ENV_EXISTS)
1221 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1222#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001223};
1224
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001225#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +02001226void env_reloc(void)
1227{
1228 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1229}
1230#endif
1231
Simon Glass09140112020-05-10 11:40:03 -06001232static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001233{
Simon Glass09140112020-05-10 11:40:03 -06001234 struct cmd_tbl *cp;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001235
Thomas Weber5904da02010-11-24 13:07:52 +01001236 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001237 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +01001238
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001239 /* drop initial "env" arg */
1240 argc--;
1241 argv++;
1242
1243 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1244
1245 if (cp)
1246 return cp->cmd(cmdtp, flag, argc, argv);
1247
Simon Glass4c12eeb2011-12-10 08:44:01 +00001248 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001249}
1250
Kim Phillips088f1b12012-10-29 13:34:31 +00001251#ifdef CONFIG_SYS_LONGHELP
1252static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001253#if defined(CONFIG_CMD_ASKENV)
1254 "ask name [message] [size] - ask for environment variable\nenv "
1255#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001256#if defined(CONFIG_CMD_ENV_CALLBACK)
1257 "callbacks - print callbacks and their associated variables\nenv "
1258#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +00001259 "default [-f] -a - [forcibly] reset default environment\n"
1260 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001261 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001262#if defined(CONFIG_CMD_EDITENV)
1263 "env edit name - edit environment variable\n"
1264#endif
Andrew Ruder88733e22013-10-22 19:07:34 -05001265#if defined(CONFIG_CMD_ENV_EXISTS)
1266 "env exists name - tests for existence of variable\n"
1267#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001268#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001269 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001270#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001271#if defined(CONFIG_CMD_ENV_FLAGS)
1272 "env flags - print variables that have non-default flags\n"
1273#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001274#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001275#ifdef CONFIG_REGEX
1276 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1277#else
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001278 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsa000b792011-04-05 07:15:14 +00001279#endif
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001280#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001281#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulzeaf73472018-07-09 19:16:29 +02001282 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001283#endif
Samuel Dionne-Rielec57bd72021-12-20 18:31:56 -05001284#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1285 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1286#endif
Leo Ruan8e921202019-05-24 17:20:19 +02001287#if defined(CONFIG_CMD_NVEDIT_INFO)
1288 "env info - display environment information\n"
Patrick Delaunay6718ebd2020-06-19 14:03:34 +02001289 "env info [-d] [-p] [-q] - evaluate environment information\n"
1290 " \"-d\": default environment is used\n"
1291 " \"-p\": environment can be persisted\n"
1292 " \"-q\": quiet output\n"
Leo Ruan8e921202019-05-24 17:20:19 +02001293#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -06001294 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001295#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardtc70f4482020-07-15 18:00:56 +02001296 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001297#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001298#if defined(CONFIG_CMD_RUN)
1299 "env run var [...] - run commands in an environment variable\n"
1300#endif
Troy Kisky289aa6a2023-03-13 14:31:24 -07001301#if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001302 "env save - save environment\n"
Frank Wunderlichcd121bd2019-06-29 11:36:19 +02001303#if defined(CONFIG_CMD_ERASEENV)
1304 "env erase - erase environment\n"
1305#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001306#endif
Patrick Delaunay0115dd32020-07-28 11:51:20 +02001307#if defined(CONFIG_CMD_NVEDIT_LOAD)
1308 "env load - load environment\n"
1309#endif
Patrick Delaunaya97d22e2020-07-28 11:51:21 +02001310#if defined(CONFIG_CMD_NVEDIT_SELECT)
1311 "env select [target] - select environment target\n"
1312#endif
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001313#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarov8f0ac532020-08-28 22:20:10 +03001314 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro051aa892019-10-24 15:17:13 +09001315 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001316#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001317 "env set [-f] name [arg ...]\n";
1318#endif
1319
1320U_BOOT_CMD(
1321 env, CONFIG_SYS_MAXARGS, 1, do_env,
1322 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001323);
1324
1325/*
1326 * Old command line interface, kept for compatibility
1327 */
wdenk8bde7f72003-06-27 21:31:46 +00001328
Peter Tyser246c6922009-10-25 15:12:56 -05001329#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001330U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001331 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001332 "edit environment variable",
1333 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001334 " - edit environment variable 'name'",
1335 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001336);
1337#endif
1338
Mike Frysinger722b0612010-10-20 03:52:39 -04001339U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001340 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001341 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001342 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001343#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardtc70f4482020-07-15 18:00:56 +02001344 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001345 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardtc70f4482020-07-15 18:00:56 +02001346 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro051aa892019-10-24 15:17:13 +09001347 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001348#endif
wdenk8bde7f72003-06-27 21:31:46 +00001349 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001350 " - print value of environment variable 'name'",
1351 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001352);
1353
Kim Phillipsa000b792011-04-05 07:15:14 +00001354#ifdef CONFIG_CMD_GREPENV
1355U_BOOT_CMD_COMPLETE(
1356 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1357 "search environment variables",
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001358#ifdef CONFIG_REGEX
1359 "[-e] [-n | -v | -b] string ...\n"
1360#else
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001361 "[-n | -v | -b] string ...\n"
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001362#endif
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001363 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001364#ifdef CONFIG_REGEX
1365 " \"-e\": enable regular expressions;\n"
1366#endif
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001367 " \"-n\": search variable names; \"-v\": search values;\n"
1368 " \"-b\": search both names and values (default)",
Kim Phillipsa000b792011-04-05 07:15:14 +00001369 var_complete
1370);
1371#endif
1372
Mike Frysinger722b0612010-10-20 03:52:39 -04001373U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001374 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001375 "set environment variables",
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001376#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiroe50e2872020-04-14 11:51:47 +09001377 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarov8f0ac532020-08-28 22:20:10 +03001378 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001379 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardtc70f4482020-07-15 18:00:56 +02001380 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro051aa892019-10-24 15:17:13 +09001381 " \"-nv\": set non-volatile attribute\n"
1382 " \"-bs\": set boot-service attribute\n"
1383 " \"-rt\": set runtime attribute\n"
AKASHI Takahiroe50e2872020-04-14 11:51:47 +09001384 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro051aa892019-10-24 15:17:13 +09001385 " \"-a\": append-write\n"
1386 " \"-i addr,size\": use <addr,size> as variable's value\n"
1387 " \"-v\": verbose message\n"
AKASHI Takahiro49d81fd2019-02-25 15:54:36 +09001388 " - delete UEFI variable 'name' if 'value' not specified\n"
1389#endif
1390 "setenv [-f] name value ...\n"
Joe Hershberger24ab5a12012-12-11 22:16:35 -06001391 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1392 "setenv [-f] name\n"
1393 " - [forcibly] delete environment variable 'name'",
Mike Frysinger722b0612010-10-20 03:52:39 -04001394 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001395);
1396
Jon Loeligerc76fe472007-07-08 18:02:23 -05001397#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001398
wdenk0d498392003-07-01 21:06:45 +00001399U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001400 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001401 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001402 "name [message] [size]\n"
Wolfgang Denk7d855912013-02-20 04:53:16 +00001403 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001404);
Jon Loeliger90253172007-07-10 11:02:44 -05001405#endif
wdenk8bde7f72003-06-27 21:31:46 +00001406
Jon Loeligerc76fe472007-07-08 18:02:23 -05001407#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001408U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001409 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001410 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001411 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001412 " - run the commands in the environment variable(s) 'var'",
1413 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001414);
Jon Loeliger90253172007-07-10 11:02:44 -05001415#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001416#endif /* CONFIG_SPL_BUILD */