blob: 53e6b57b60eada06ad974915c1ea418bb56dfd4b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenka68d3ed2002-10-11 08:38:32 +00002/*
Wolfgang Denk565af272013-03-23 23:50:28 +00003 * (C) Copyright 2000-2013
wdenka68d3ed2002-10-11 08:38:32 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
Kim Phillipsf0c9d532011-04-05 07:15:14 +00008 *
9 * Copyright 2011 Freescale Semiconductor, Inc.
wdenka68d3ed2002-10-11 08:38:32 +000010 */
11
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020012/*
wdenka68d3ed2002-10-11 08:38:32 +000013 * Support for persistent environment data
14 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020015 * The "environment" is stored on external storage as a list of '\0'
16 * terminated "name=value" strings. The end of the list is marked by
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -040017 * a double '\0'. The environment is preceded by a 32 bit CRC over
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020018 * the data part and, in case of redundant environment, a byte of
19 * flags.
wdenka68d3ed2002-10-11 08:38:32 +000020 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020021 * This linearized representation will also be used before
22 * relocation, i. e. as long as we don't have a full C runtime
23 * environment. After that, we use a hash table.
wdenka68d3ed2002-10-11 08:38:32 +000024 */
25
26#include <common.h>
Simon Glassdec3c012014-04-10 20:01:25 -060027#include <cli.h>
wdenka68d3ed2002-10-11 08:38:32 +000028#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070029#include <console.h>
Simon Glass2d85a752019-08-01 09:46:41 -060030#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060031#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -060032#include <log.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020033#include <search.h>
34#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050035#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050036#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060037#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060038#include <linux/bitops.h>
Simon Glass48b6c6b2019-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 Glassa7459262013-10-25 23:01:31 -060042#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000043
Wolfgang Denk6405a152006-03-31 18:32:53 +020044DECLARE_GLOBAL_DATA_PTR;
45
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010046#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
47 defined(CONFIG_ENV_IS_IN_FLASH) || \
48 defined(CONFIG_ENV_IS_IN_MMC) || \
49 defined(CONFIG_ENV_IS_IN_FAT) || \
50 defined(CONFIG_ENV_IS_IN_EXT4) || \
51 defined(CONFIG_ENV_IS_IN_NAND) || \
52 defined(CONFIG_ENV_IS_IN_NVRAM) || \
53 defined(CONFIG_ENV_IS_IN_ONENAND) || \
54 defined(CONFIG_ENV_IS_IN_SATA) || \
55 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
56 defined(CONFIG_ENV_IS_IN_REMOTE) || \
57 defined(CONFIG_ENV_IS_IN_UBI)
58
59#define ENV_IS_IN_DEVICE
60
61#endif
62
63#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000064 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030065# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020066NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000067#endif
68
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020069/*
70 * Maximum expected input data size for import command
71 */
72#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000073
wdenka68d3ed2002-10-11 08:38:32 +000074/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020075 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060076 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020077 * has changed or not. So it is possible to reread an environment
78 * variable only if the environment was changed ... done so for
79 * example in NetInitLoop()
80 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010081static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000082
Simon Glass2d85a752019-08-01 09:46:41 -060083int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010084{
85 return env_id;
86}
wdenka68d3ed2002-10-11 08:38:32 +000087
Ilya Yanokf7a2c552012-09-18 00:22:50 +000088#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040089/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020090 * Command interface: print one or all environment variables
91 *
92 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040093 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060094static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000095{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020096 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000097 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000098
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020099 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -0600100 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000101
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200102 e.key = name;
103 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600104 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200105 if (ep == NULL)
106 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000107 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200108 return len;
109 }
wdenka68d3ed2002-10-11 08:38:32 +0000110
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200111 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600112 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000113
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200114 if (len > 0) {
115 puts(res);
116 free(res);
117 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000118 }
119
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200120 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000121 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200122 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400123}
wdenka68d3ed2002-10-11 08:38:32 +0000124
Simon Glassed38aef2020-05-10 11:40:03 -0600125static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
126 char *const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400127{
128 int i;
129 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600130 int env_flag = H_HIDE_DOT;
131
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900132#if defined(CONFIG_CMD_NVEDIT_EFI)
133 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
134 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
135#endif
136
Joe Hershberger79a905e2012-12-11 22:16:23 -0600137 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
138 argc--;
139 argv++;
140 env_flag &= ~H_HIDE_DOT;
141 }
wdenka68d3ed2002-10-11 08:38:32 +0000142
Mike Frysinger87944922009-05-24 02:26:19 -0400143 if (argc == 1) {
144 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600145 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200146 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400147 return 1;
148 printf("\nEnvironment size: %d/%ld bytes\n",
149 rcode, (ulong)ENV_SIZE);
150 return 0;
151 }
wdenka68d3ed2002-10-11 08:38:32 +0000152
Mike Frysinger87944922009-05-24 02:26:19 -0400153 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600154 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400155 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600156 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200157 if (!rc) {
158 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400159 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000160 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000161 }
162
163 return rcode;
164}
165
166#ifdef CONFIG_CMD_GREPENV
Simon Glassed38aef2020-05-10 11:40:03 -0600167static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
168 int argc, char *const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000169{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000170 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000171 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000172
173 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000174 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000175
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000176 grep_how = H_MATCH_SUBSTR; /* default: substring search */
177 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000178
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200179 while (--argc > 0 && **++argv == '-') {
180 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000181 while (*++arg) {
182 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000183#ifdef CONFIG_REGEX
184 case 'e': /* use regex matching */
185 grep_how = H_MATCH_REGEX;
186 break;
187#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000188 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000189 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000190 break;
191 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000192 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000193 break;
194 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000195 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000196 break;
197 case '-':
198 goto DONE;
199 default:
200 return CMD_RET_USAGE;
201 }
202 }
203 }
204
205DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000206 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000207 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000208 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000209
Wolfgang Denke27fc122013-03-23 23:50:29 +0000210 if (len > 0) {
211 puts(res);
212 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000213 }
Mike Frysinger87944922009-05-24 02:26:19 -0400214
Wolfgang Denke27fc122013-03-23 23:50:29 +0000215 if (len < 2)
216 return 1;
217
218 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000219}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000220#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000221#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000222
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200223/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000224 * Set a new environment variable,
225 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600226 */
Simon Glassed38aef2020-05-10 11:40:03 -0600227static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000228{
229 int i, len;
230 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600231 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000232
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600233 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900234
235#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
236 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
237 return do_env_set_efi(NULL, flag, --argc, ++argv);
238#endif
239
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600240 while (argc > 1 && **(argv + 1) == '-') {
241 char *arg = *++argv;
242
243 --argc;
244 while (*++arg) {
245 switch (*arg) {
246 case 'f': /* force */
247 env_flag |= H_FORCE;
248 break;
249 default:
250 return CMD_RET_USAGE;
251 }
252 }
253 }
254 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000255 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000256
257 if (strchr(name, '=')) {
258 printf("## Error: illegal character '='"
259 "in variable name \"%s\"\n", name);
260 return 1;
261 }
262
263 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000264
wdenka68d3ed2002-10-11 08:38:32 +0000265 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000266 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600267 int rc = hdelete_r(name, &env_htab, env_flag);
Simon Glass783ef3f2020-11-05 10:33:37 -0700268
269 /* If the variable didn't exist, don't report an error */
270 return rc && rc != -ENOENT ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000271 }
272
273 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200274 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000275 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000276 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000277 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000278
279 value = malloc(len);
280 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200281 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000282 return 1;
283 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000284 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200285 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000286
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200287 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000288 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000289 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000290 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200291 if (s != value)
292 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000293
Igor Grinbergde832562011-11-07 01:13:59 +0000294 e.key = name;
295 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600296 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200297 free(value);
298 if (!ep) {
299 printf("## Error inserting \"%s\" variable, errno=%d\n",
300 name, errno);
301 return 1;
302 }
wdenka68d3ed2002-10-11 08:38:32 +0000303
wdenka68d3ed2002-10-11 08:38:32 +0000304 return 0;
305}
306
Simon Glass6a38e412017-08-03 12:22:09 -0600307int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000308{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200309 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
310
Joe Hershberger3902c602013-04-08 10:32:50 +0000311 /* before import into hashtable */
312 if (!(gd->flags & GD_FLG_ENV_READY))
313 return 1;
314
Igor Grinbergde832562011-11-07 01:13:59 +0000315 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500316 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200317 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500318 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000319}
320
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000321#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600322static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
323 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000324{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200325 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000326 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000327
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500328 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000329}
330
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200331/*
wdenka68d3ed2002-10-11 08:38:32 +0000332 * Prompt for environment variable
333 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500334#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600335int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000336{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200337 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000338 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000339 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000340 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000341
342 local_args[0] = argv[0];
343 local_args[1] = argv[1];
344 local_args[2] = NULL;
345 local_args[3] = NULL;
346
Wolfgang Denk586895d2013-02-20 04:53:16 +0000347 /*
348 * Check the syntax:
349 *
350 * env_ask envname [message1 ...] [size]
351 */
352 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000353 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000354
Wolfgang Denk586895d2013-02-20 04:53:16 +0000355 /*
356 * We test the last argument if it can be converted
357 * into a decimal number. If yes, we assume it's
358 * the size. Otherwise we echo it as part of the
359 * message.
360 */
Simon Glassff9b9032021-07-24 09:03:30 -0600361 i = dectoul(argv[argc - 1], &endptr);
Wolfgang Denk586895d2013-02-20 04:53:16 +0000362 if (*endptr != '\0') { /* no size */
363 size = CONFIG_SYS_CBSIZE - 1;
364 } else { /* size given */
365 size = i;
366 --argc;
367 }
wdenka68d3ed2002-10-11 08:38:32 +0000368
Wolfgang Denk586895d2013-02-20 04:53:16 +0000369 if (argc <= 2) {
370 sprintf(message, "Please enter '%s': ", argv[1]);
371 } else {
372 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400373 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000374 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000375 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000376
Tom Rini4beb2352017-09-26 19:37:11 -0400377 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000378 pos += strlen(argv[i]);
379 }
Tom Rini4beb2352017-09-26 19:37:11 -0400380 if (pos < sizeof(message) - 1) {
381 message[pos++] = ' ';
382 message[pos] = '\0';
383 } else
384 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000385 }
386
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200387 if (size >= CONFIG_SYS_CBSIZE)
388 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000389
390 if (size <= 0)
391 return 1;
392
393 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600394 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000395
396 if (size < len)
397 console_buffer[size] = '\0';
398
399 len = 2;
400 if (console_buffer[0] != '\0') {
401 local_args[2] = console_buffer;
402 len = 3;
403 }
404
405 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500406 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000407}
Jon Loeligerd704d912007-07-10 11:02:44 -0500408#endif
wdenka68d3ed2002-10-11 08:38:32 +0000409
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600410#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500411static int print_static_binding(const char *var_name, const char *callback_name,
412 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600413{
414 printf("\t%-20s %-20s\n", var_name, callback_name);
415
416 return 0;
417}
418
Simon Glass1a236862019-08-02 09:44:18 -0600419static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600420{
421 struct env_clbk_tbl *clbkp;
422 int i;
423 int num_callbacks;
424
425 if (entry->callback == NULL)
426 return 0;
427
428 /* look up the callback in the linker-list */
429 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
430 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
431 i < num_callbacks;
432 i++, clbkp++) {
433#if defined(CONFIG_NEEDS_MANUAL_RELOC)
434 if (entry->callback == clbkp->callback + gd->reloc_off)
435#else
436 if (entry->callback == clbkp->callback)
437#endif
438 break;
439 }
440
441 if (i == num_callbacks)
442 /* this should probably never happen, but just in case... */
443 printf("\t%-20s %p\n", entry->key, entry->callback);
444 else
445 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
446
447 return 0;
448}
449
450/*
451 * Print the callbacks available and what they are bound to
452 */
Simon Glassed38aef2020-05-10 11:40:03 -0600453int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
454 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600455{
456 struct env_clbk_tbl *clbkp;
457 int i;
458 int num_callbacks;
459
460 /* Print the available callbacks */
461 puts("Available callbacks:\n");
462 puts("\tCallback Name\n");
463 puts("\t-------------\n");
464 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
465 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
466 i < num_callbacks;
467 i++, clbkp++)
468 printf("\t%s\n", clbkp->name);
469 puts("\n");
470
471 /* Print the static bindings that may exist */
472 puts("Static callback bindings:\n");
473 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
474 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500475 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600476 puts("\n");
477
478 /* walk through each variable and print the callback if it has one */
479 puts("Active callback bindings:\n");
480 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
481 printf("\t%-20s %-20s\n", "-------------", "-------------");
482 hwalk_r(&env_htab, print_active_callback);
483 return 0;
484}
485#endif
486
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600487#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500488static int print_static_flags(const char *var_name, const char *flags,
489 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600490{
491 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600492 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600493
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600494 printf("\t%-20s %-20s %-20s\n", var_name,
495 env_flags_get_vartype_name(type),
496 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600497
498 return 0;
499}
500
Simon Glass1a236862019-08-02 09:44:18 -0600501static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600502{
503 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600504 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600505
506 if (entry->flags == 0)
507 return 0;
508
509 type = (enum env_flags_vartype)
510 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600511 access = env_flags_parse_varaccess_from_binflags(entry->flags);
512 printf("\t%-20s %-20s %-20s\n", entry->key,
513 env_flags_get_vartype_name(type),
514 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600515
516 return 0;
517}
518
519/*
520 * Print the flags available and what variables have flags
521 */
Simon Glassed38aef2020-05-10 11:40:03 -0600522int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600523{
524 /* Print the available variable types */
525 printf("Available variable type flags (position %d):\n",
526 ENV_FLAGS_VARTYPE_LOC);
527 puts("\tFlag\tVariable Type Name\n");
528 puts("\t----\t------------------\n");
529 env_flags_print_vartypes();
530 puts("\n");
531
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600532 /* Print the available variable access types */
533 printf("Available variable access flags (position %d):\n",
534 ENV_FLAGS_VARACCESS_LOC);
535 puts("\tFlag\tVariable Access Name\n");
536 puts("\t----\t--------------------\n");
537 env_flags_print_varaccess();
538 puts("\n");
539
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600540 /* Print the static flags that may exist */
541 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600542 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
543 "Variable Access");
544 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
545 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500546 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600547 puts("\n");
548
549 /* walk through each variable and print the flags if non-default */
550 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600551 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
552 "Variable Access");
553 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
554 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600555 hwalk_r(&env_htab, print_active_flags);
556 return 0;
557}
558#endif
559
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200560/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500561 * Interactively edit an environment variable
562 */
563#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600564static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
565 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500566{
567 char buffer[CONFIG_SYS_CBSIZE];
568 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500569
Wolfgang Denk3b683112010-07-17 01:06:04 +0200570 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000571 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500572
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500573 /* before import into hashtable */
574 if (!(gd->flags & GD_FLG_ENV_READY))
575 return 1;
576
Peter Tyser0deafa22009-10-25 15:12:56 -0500577 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600578 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500579 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800580 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500581 else
582 buffer[0] = '\0';
583
Simon Glassbe6aafc2014-04-10 20:01:27 -0600584 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000585 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500586
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500587 if (buffer[0] == '\0') {
588 const char * const _argv[3] = { "setenv", argv[1], NULL };
589
590 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
591 } else {
592 const char * const _argv[4] = { "setenv", argv[1], buffer,
593 NULL };
594
595 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
596 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500597}
598#endif /* CONFIG_CMD_EDITENV */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200599
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100600#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Simon Glassed38aef2020-05-10 11:40:03 -0600601static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
602 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000603{
Simon Glassd49b8892017-08-03 12:22:08 -0600604 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000605}
wdenk57b2d802003-06-27 21:31:46 +0000606
Mike Frysinger61583932008-12-30 02:59:25 -0500607U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200608 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600609 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200610 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500611);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200612
613#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600614static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
615 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200616{
617 return env_erase() ? 1 : 0;
618}
619
620U_BOOT_CMD(
621 eraseenv, 1, 0, do_env_erase,
622 "erase environment variables from persistent storage",
623 ""
624);
625#endif
wdenka68d3ed2002-10-11 08:38:32 +0000626#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200627
628#if defined(CONFIG_CMD_NVEDIT_LOAD)
629static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
630 char *const argv[])
631{
632 return env_reload() ? 1 : 0;
633}
634#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200635
636#if defined(CONFIG_CMD_NVEDIT_SELECT)
637static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
638 char *const argv[])
639{
640 return env_select(argv[1]) ? 1 : 0;
641}
642#endif
643
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000644#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000645
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000646#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600647static int do_env_default(struct cmd_tbl *cmdtp, int flag,
648 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200649{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300650 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000651
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000652 debug("Initial value for argc=%d\n", argc);
653 while (--argc > 0 && **++argv == '-') {
654 char *arg = *argv;
655
656 while (*++arg) {
657 switch (*arg) {
658 case 'a': /* default all */
659 all = 1;
660 break;
661 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300662 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000663 break;
664 default:
665 return cmd_usage(cmdtp);
666 }
667 }
668 }
669 debug("Final value for argc=%d\n", argc);
670 if (all && (argc == 0)) {
671 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600672 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300673 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000674 return 0;
675 }
676 if (!all && (argc > 0)) {
677 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600678 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000679 return 0;
680 }
681
682 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200683}
684
Simon Glassed38aef2020-05-10 11:40:03 -0600685static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
686 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200687{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600688 int env_flag = H_INTERACTIVE;
689 int ret = 0;
690
691 debug("Initial value for argc=%d\n", argc);
692 while (argc > 1 && **(argv + 1) == '-') {
693 char *arg = *++argv;
694
695 --argc;
696 while (*++arg) {
697 switch (*arg) {
698 case 'f': /* force */
699 env_flag |= H_FORCE;
700 break;
701 default:
702 return CMD_RET_USAGE;
703 }
704 }
705 }
706 debug("Final value for argc=%d\n", argc);
707
708 env_id++;
709
710 while (--argc > 0) {
711 char *name = *++argv;
712
Simon Glass783ef3f2020-11-05 10:33:37 -0700713 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger8335ce72012-12-11 22:16:36 -0600714 ret = 1;
715 }
716
717 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200718}
719
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500720#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200721/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100722 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200723 * -t: export as text format; if size is given, data will be
724 * padded with '\0' bytes; if not, one terminating '\0'
725 * will be added (which is included in the "filesize"
726 * setting so you can for exmple copy this to flash and
727 * keep the termination).
728 * -b: export as binary format (name=value pairs separated by
729 * '\0', list end marked by double "\0\0")
730 * -c: export as checksum protected environment format as
731 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100732 * -s size:
733 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200734 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100735 * var... List of variable names that get included into the
736 * export. Without arguments, the whole environment gets
737 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200738 *
739 * With "-c" and size is NOT given, then the export command will
740 * format the data as currently used for the persistent storage,
741 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400742 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200743 * environment, a "current" redundancy flag. If size is given, this
744 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
745 * checksum and redundancy flag will be inserted.
746 *
747 * With "-b" and "-t", always only the real data (including a
748 * terminating '\0' byte) will be written; here the optional size
749 * argument will be used to make sure not to overflow the user
750 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400751 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200752 *
753 * On successful return, the variable "filesize" will be set.
754 * Note that filesize includes the trailing/terminating '\0' byte(s).
755 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400756 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200757 *
758 * => env export -t 100000
759 * => era ${backup_addr} +${filesize}
760 * => cp.b 100000 ${backup_addr} ${filesize}
761 *
762 * Re-import this snapshot, deleting all other settings:
763 *
764 * => env import -d -t ${backup_addr}
765 */
Simon Glassed38aef2020-05-10 11:40:03 -0600766static int do_env_export(struct cmd_tbl *cmdtp, int flag,
767 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200768{
769 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600770 ulong addr;
771 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100772 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200773 ssize_t len;
774 env_t *envp;
775 char sep = '\n';
776 int chk = 0;
777 int fmt = 0;
778
779 cmd = *argv;
780
781 while (--argc > 0 && **++argv == '-') {
782 char *arg = *argv;
783 while (*++arg) {
784 switch (*arg) {
785 case 'b': /* raw binary format */
786 if (fmt++)
787 goto sep_err;
788 sep = '\0';
789 break;
790 case 'c': /* external checksum format */
791 if (fmt++)
792 goto sep_err;
793 sep = '\0';
794 chk = 1;
795 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100796 case 's': /* size given */
797 if (--argc <= 0)
798 return cmd_usage(cmdtp);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600799 size = hextoul(*++argv, NULL);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100800 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200801 case 't': /* text format */
802 if (fmt++)
803 goto sep_err;
804 sep = '\n';
805 break;
806 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000807 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200808 }
809 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100810NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200811 }
812
Macpaul Linff2a9f32011-04-26 16:16:45 +0000813 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000814 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000815
Simon Glass3ff49ec2021-07-24 09:03:29 -0600816 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600817 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200818
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100819 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600820 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100821
822 argc--;
823 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200824
825 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000826 len = hexport_r(&env_htab, sep,
827 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600828 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200829 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200830 pr_err("## Error: Cannot export environment: errno = %d\n",
831 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200832 return 1;
833 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100834 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600835 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200836
837 return 0;
838 }
839
Simon Glassa7459262013-10-25 23:01:31 -0600840 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200841
842 if (chk) /* export as checksum protected block */
843 res = (char *)envp->data;
844 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -0600845 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200846
Wolfgang Denk565af272013-03-23 23:50:28 +0000847 len = hexport_r(&env_htab, '\0',
848 H_MATCH_KEY | H_MATCH_IDENT,
849 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200850 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200851 pr_err("## Error: Cannot export environment: errno = %d\n",
852 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200853 return 1;
854 }
855
856 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +0000857 envp->crc = crc32(0, envp->data,
858 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200859#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -0600860 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200861#endif
862 }
Simon Glass4d949a22017-08-03 12:22:10 -0600863 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200864
865 return 0;
866
867sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200868 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
869 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200870 return 1;
871}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500872#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200873
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500874#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200875/*
Quentin Schulz0312b942018-07-09 19:16:29 +0200876 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
877 * -d: delete existing environment before importing if no var is
878 * passed; if vars are passed, if one var is in the current
879 * environment but not in the environment at addr, delete var from
880 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400881 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200882 * -t: assume text format; either "size" must be given or the
883 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +0200884 * -r: handle CRLF like LF, that means exported variables with
885 * a content which ends with \r won't get imported. Used
886 * to import text files created with editors which are using CRLF
887 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200888 * -b: assume binary format ('\0' separated, "\0\0" terminated)
889 * -c: assume checksum protected environment format
890 * addr: memory address to read from
891 * size: length of input data; if missing, proper '\0'
892 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +0200893 * if var is set and size should be missing (i.e. '\0'
894 * termination), set size to '-'
895 * var... List of the names of the only variables that get imported from
896 * the environment at address 'addr'. Without arguments, the whole
897 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200898 */
Simon Glassed38aef2020-05-10 11:40:03 -0600899static int do_env_import(struct cmd_tbl *cmdtp, int flag,
900 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200901{
Simon Glassa7459262013-10-25 23:01:31 -0600902 ulong addr;
903 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200904 char sep = '\n';
905 int chk = 0;
906 int fmt = 0;
907 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +0200908 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +0200909 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200910 size_t size;
911
912 cmd = *argv;
913
914 while (--argc > 0 && **++argv == '-') {
915 char *arg = *argv;
916 while (*++arg) {
917 switch (*arg) {
918 case 'b': /* raw binary format */
919 if (fmt++)
920 goto sep_err;
921 sep = '\0';
922 break;
923 case 'c': /* external checksum format */
924 if (fmt++)
925 goto sep_err;
926 sep = '\0';
927 chk = 1;
928 break;
929 case 't': /* text format */
930 if (fmt++)
931 goto sep_err;
932 sep = '\n';
933 break;
Alexander Holler3e12be72014-07-14 17:49:55 +0200934 case 'r': /* handle CRLF like LF */
935 crlf_is_lf = 1;
936 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200937 case 'd':
938 del = 1;
939 break;
940 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000941 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200942 }
943 }
944 }
945
Macpaul Linff2a9f32011-04-26 16:16:45 +0000946 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000947 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200948
949 if (!fmt)
950 printf("## Warning: defaulting to text format\n");
951
Alexander Holler3e12be72014-07-14 17:49:55 +0200952 if (sep != '\n' && crlf_is_lf )
953 crlf_is_lf = 0;
954
Simon Glass3ff49ec2021-07-24 09:03:29 -0600955 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600956 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200957
Quentin Schulz0312b942018-07-09 19:16:29 +0200958 if (argc >= 2 && strcmp(argv[1], "-")) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600959 size = hextoul(argv[1], NULL);
Quentin Schulz0312b942018-07-09 19:16:29 +0200960 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -0500961 puts("## Error: external checksum format must pass size\n");
962 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200963 } else {
Simon Glassa7459262013-10-25 23:01:31 -0600964 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200965
966 size = 0;
967
968 while (size < MAX_ENV_SIZE) {
969 if ((*s == sep) && (*(s+1) == '\0'))
970 break;
971 ++s;
972 ++size;
973 }
974 if (size == MAX_ENV_SIZE) {
975 printf("## Warning: Input data exceeds %d bytes"
976 " - truncated\n", MAX_ENV_SIZE);
977 }
Horst Kronstorfer295da642011-12-16 23:33:10 +0000978 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +0000979 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200980 }
981
Quentin Schulz0312b942018-07-09 19:16:29 +0200982 if (argc > 2)
983 wl = 1;
984
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200985 if (chk) {
986 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -0600987 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200988
Pedro Aguilar834b5522020-08-31 11:01:41 +0200989 if (size <= offsetof(env_t, data)) {
990 printf("## Error: Invalid size 0x%zX\n", size);
991 return 1;
992 }
993
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200994 size -= offsetof(env_t, data);
995 memcpy(&crc, &ep->crc, sizeof(crc));
996
997 if (crc32(0, ep->data, size) != crc) {
998 puts("## Error: bad CRC, import failed\n");
999 return 1;
1000 }
Simon Glassa7459262013-10-25 23:01:31 -06001001 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001002 }
1003
Quentin Schulz0312b942018-07-09 19:16:29 +02001004 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1005 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001006 pr_err("## Error: Environment import failed: errno = %d\n",
1007 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001008 return 1;
1009 }
1010 gd->flags |= GD_FLG_ENV_READY;
1011
1012 return 0;
1013
1014sep_err:
1015 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1016 cmd);
1017 return 1;
1018}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001019#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001020
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001021#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1022static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
1023 int argc, char *const argv[])
1024{
1025 char *to = argv[1];
1026 char *from = argv[2];
1027 char *default_value = NULL;
1028 int ret = 0;
1029
1030 if (argc < 3 || argc > 4) {
1031 return CMD_RET_USAGE;
1032 }
1033
1034 if (argc == 4) {
1035 default_value = argv[3];
1036 }
1037
1038 if (env_get(from) == NULL && default_value == NULL) {
1039 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1040
1041 return CMD_RET_FAILURE;
1042 }
1043
1044 if (env_get(from) == NULL) {
1045 ret = env_set(to, default_value);
1046 }
1047 else {
1048 ret = env_set(to, env_get(from));
1049 }
1050
1051 if (ret == 0) {
1052 return CMD_RET_SUCCESS;
1053 }
1054 else {
1055 return CMD_RET_FAILURE;
1056 }
1057}
1058#endif
1059
Leo Ruan7450b872019-05-24 17:20:19 +02001060#if defined(CONFIG_CMD_NVEDIT_INFO)
1061/*
1062 * print_env_info - print environment information
1063 */
1064static int print_env_info(void)
1065{
1066 const char *value;
1067
1068 /* print environment validity value */
1069 switch (gd->env_valid) {
1070 case ENV_INVALID:
1071 value = "invalid";
1072 break;
1073 case ENV_VALID:
1074 value = "valid";
1075 break;
1076 case ENV_REDUND:
1077 value = "redundant";
1078 break;
1079 default:
1080 value = "unknown";
1081 break;
1082 }
1083 printf("env_valid = %s\n", value);
1084
1085 /* print environment ready flag */
1086 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1087 printf("env_ready = %s\n", value);
1088
1089 /* print environment using default flag */
1090 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1091 printf("env_use_default = %s\n", value);
1092
1093 return CMD_RET_SUCCESS;
1094}
1095
1096#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1097#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1098
1099/*
1100 * env info - display environment information
1101 * env info [-d] - evaluate whether default environment is used
1102 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001103 * Add [-q] - quiet mode, use only for command result, for test by example:
1104 * test env info -p -d -q
Leo Ruan7450b872019-05-24 17:20:19 +02001105 */
Simon Glassed38aef2020-05-10 11:40:03 -06001106static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1107 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +02001108{
1109 int eval_flags = 0;
1110 int eval_results = 0;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001111 bool quiet = false;
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001112#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1113 enum env_location loc;
1114#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001115
1116 /* display environment information */
1117 if (argc <= 1)
1118 return print_env_info();
1119
1120 /* process options */
1121 while (--argc > 0 && **++argv == '-') {
1122 char *arg = *argv;
1123
1124 while (*++arg) {
1125 switch (*arg) {
1126 case 'd':
1127 eval_flags |= ENV_INFO_IS_DEFAULT;
1128 break;
1129 case 'p':
1130 eval_flags |= ENV_INFO_IS_PERSISTED;
1131 break;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001132 case 'q':
1133 quiet = true;
1134 break;
Leo Ruan7450b872019-05-24 17:20:19 +02001135 default:
1136 return CMD_RET_USAGE;
1137 }
1138 }
1139 }
1140
1141 /* evaluate whether default environment is used */
1142 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1143 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001144 if (!quiet)
1145 printf("Default environment is used\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001146 eval_results |= ENV_INFO_IS_DEFAULT;
1147 } else {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001148 if (!quiet)
1149 printf("Environment was loaded from persistent storage\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001150 }
1151 }
1152
1153 /* evaluate whether environment can be persisted */
1154 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Patrick Delaunay4b876be2020-07-02 17:43:44 +02001155#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001156 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1157 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1158 if (!quiet)
1159 printf("Environment can be persisted\n");
1160 eval_results |= ENV_INFO_IS_PERSISTED;
1161 } else {
1162 if (!quiet)
1163 printf("Environment cannot be persisted\n");
1164 }
Leo Ruan7450b872019-05-24 17:20:19 +02001165#else
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001166 if (!quiet)
1167 printf("Environment cannot be persisted\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001168#endif
1169 }
1170
1171 /* The result of evaluations is combined with AND */
1172 if (eval_flags != eval_results)
1173 return CMD_RET_FAILURE;
1174
1175 return CMD_RET_SUCCESS;
1176}
1177#endif
1178
Andrew Ruder94463402013-10-22 19:07:34 -05001179#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001180static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1181 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001182{
Simon Glass1a236862019-08-02 09:44:18 -06001183 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001184
1185 if (argc < 2)
1186 return CMD_RET_USAGE;
1187
1188 e.key = argv[1];
1189 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001190 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001191
1192 return (ep == NULL) ? 1 : 0;
1193}
1194#endif
1195
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001196/*
1197 * New command line interface: "env" command with subcommands
1198 */
Simon Glassed38aef2020-05-10 11:40:03 -06001199static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001200#if defined(CONFIG_CMD_ASKENV)
1201 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1202#endif
1203 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001204 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001205#if defined(CONFIG_CMD_EDITENV)
1206 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1207#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001208#if defined(CONFIG_CMD_ENV_CALLBACK)
1209 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1210#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001211#if defined(CONFIG_CMD_ENV_FLAGS)
1212 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1213#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001214#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001215 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001216#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001217#if defined(CONFIG_CMD_GREPENV)
1218 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1219#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001220#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001221 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001222#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001223#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1224 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1225#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001226#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001227 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan7450b872019-05-24 17:20:19 +02001228#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001229#if defined(CONFIG_CMD_NVEDIT_LOAD)
1230 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1231#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001232 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1233#if defined(CONFIG_CMD_RUN)
1234 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1235#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001236#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001237 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001238#if defined(CONFIG_CMD_ERASEENV)
1239 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1240#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001241#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001242#if defined(CONFIG_CMD_NVEDIT_SELECT)
1243 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1244#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001245 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001246#if defined(CONFIG_CMD_ENV_EXISTS)
1247 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1248#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001249};
1250
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001251#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001252void env_reloc(void)
1253{
1254 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1255}
1256#endif
1257
Simon Glassed38aef2020-05-10 11:40:03 -06001258static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001259{
Simon Glassed38aef2020-05-10 11:40:03 -06001260 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001261
Thomas Weber4d282652010-11-24 13:07:52 +01001262 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001263 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001264
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001265 /* drop initial "env" arg */
1266 argc--;
1267 argv++;
1268
1269 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1270
1271 if (cp)
1272 return cp->cmd(cmdtp, flag, argc, argv);
1273
Simon Glassa06dfc72011-12-10 08:44:01 +00001274 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001275}
1276
Kim Phillipsdc00a682012-10-29 13:34:31 +00001277#ifdef CONFIG_SYS_LONGHELP
1278static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001279#if defined(CONFIG_CMD_ASKENV)
1280 "ask name [message] [size] - ask for environment variable\nenv "
1281#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001282#if defined(CONFIG_CMD_ENV_CALLBACK)
1283 "callbacks - print callbacks and their associated variables\nenv "
1284#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001285 "default [-f] -a - [forcibly] reset default environment\n"
1286 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001287 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001288#if defined(CONFIG_CMD_EDITENV)
1289 "env edit name - edit environment variable\n"
1290#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001291#if defined(CONFIG_CMD_ENV_EXISTS)
1292 "env exists name - tests for existence of variable\n"
1293#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001294#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001295 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001296#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001297#if defined(CONFIG_CMD_ENV_FLAGS)
1298 "env flags - print variables that have non-default flags\n"
1299#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001300#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001301#ifdef CONFIG_REGEX
1302 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1303#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001304 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001305#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001306#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001307#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001308 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001309#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001310#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1311 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1312#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001313#if defined(CONFIG_CMD_NVEDIT_INFO)
1314 "env info - display environment information\n"
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001315 "env info [-d] [-p] [-q] - evaluate environment information\n"
1316 " \"-d\": default environment is used\n"
1317 " \"-p\": environment can be persisted\n"
1318 " \"-q\": quiet output\n"
Leo Ruan7450b872019-05-24 17:20:19 +02001319#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001320 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001321#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001322 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001323#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001324#if defined(CONFIG_CMD_RUN)
1325 "env run var [...] - run commands in an environment variable\n"
1326#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001327#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001328 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001329#if defined(CONFIG_CMD_ERASEENV)
1330 "env erase - erase environment\n"
1331#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001332#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001333#if defined(CONFIG_CMD_NVEDIT_LOAD)
1334 "env load - load environment\n"
1335#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001336#if defined(CONFIG_CMD_NVEDIT_SELECT)
1337 "env select [target] - select environment target\n"
1338#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001339#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001340 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001341 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001342#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001343 "env set [-f] name [arg ...]\n";
1344#endif
1345
1346U_BOOT_CMD(
1347 env, CONFIG_SYS_MAXARGS, 1, do_env,
1348 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001349);
1350
1351/*
1352 * Old command line interface, kept for compatibility
1353 */
wdenk57b2d802003-06-27 21:31:46 +00001354
Peter Tyser0deafa22009-10-25 15:12:56 -05001355#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001356U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001357 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001358 "edit environment variable",
1359 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001360 " - edit environment variable 'name'",
1361 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001362);
1363#endif
1364
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001365U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001366 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001367 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001368 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001369#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001370 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001371 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001372 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001373 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001374#endif
wdenk57b2d802003-06-27 21:31:46 +00001375 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001376 " - print value of environment variable 'name'",
1377 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001378);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001379
1380#ifdef CONFIG_CMD_GREPENV
1381U_BOOT_CMD_COMPLETE(
1382 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1383 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001384#ifdef CONFIG_REGEX
1385 "[-e] [-n | -v | -b] string ...\n"
1386#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001387 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001388#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001389 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001390#ifdef CONFIG_REGEX
1391 " \"-e\": enable regular expressions;\n"
1392#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001393 " \"-n\": search variable names; \"-v\": search values;\n"
1394 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001395 var_complete
1396);
1397#endif
wdenk57b2d802003-06-27 21:31:46 +00001398
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001399U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001400 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001401 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001402#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001403 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001404 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001405 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001406 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001407 " \"-nv\": set non-volatile attribute\n"
1408 " \"-bs\": set boot-service attribute\n"
1409 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001410 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001411 " \"-a\": append-write\n"
1412 " \"-i addr,size\": use <addr,size> as variable's value\n"
1413 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001414 " - delete UEFI variable 'name' if 'value' not specified\n"
1415#endif
1416 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001417 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1418 "setenv [-f] name\n"
1419 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001420 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001421);
1422
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001423#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001424
wdenkf287a242003-07-01 21:06:45 +00001425U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001426 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001427 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001428 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001429 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001430);
Jon Loeligerd704d912007-07-10 11:02:44 -05001431#endif
wdenk57b2d802003-06-27 21:31:46 +00001432
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001433#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001434U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001435 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001436 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001437 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001438 " - run the commands in the environment variable(s) 'var'",
1439 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001440);
Jon Loeligerd704d912007-07-10 11:02:44 -05001441#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001442#endif /* CONFIG_SPL_BUILD */