blob: 12eae0627bb882f0e2ee19c18854f2462b389507 [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) || \
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010054 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
55 defined(CONFIG_ENV_IS_IN_REMOTE) || \
56 defined(CONFIG_ENV_IS_IN_UBI)
57
58#define ENV_IS_IN_DEVICE
59
60#endif
61
62#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000063 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030064# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020065NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000066#endif
67
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020068/*
69 * Maximum expected input data size for import command
70 */
71#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000072
wdenka68d3ed2002-10-11 08:38:32 +000073/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020074 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060075 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020076 * has changed or not. So it is possible to reread an environment
77 * variable only if the environment was changed ... done so for
78 * example in NetInitLoop()
79 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010080static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000081
Simon Glass2d85a752019-08-01 09:46:41 -060082int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010083{
84 return env_id;
85}
wdenka68d3ed2002-10-11 08:38:32 +000086
Ilya Yanokf7a2c552012-09-18 00:22:50 +000087#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040088/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020089 * Command interface: print one or all environment variables
90 *
91 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040092 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060093static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000094{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020095 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000096 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000097
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -060099 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000100
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101 e.key = name;
102 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600103 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200104 if (ep == NULL)
105 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000106 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200107 return len;
108 }
wdenka68d3ed2002-10-11 08:38:32 +0000109
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600111 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000112
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200113 if (len > 0) {
114 puts(res);
115 free(res);
116 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000117 }
118
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200119 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000120 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200121 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400122}
wdenka68d3ed2002-10-11 08:38:32 +0000123
Simon Glassed38aef2020-05-10 11:40:03 -0600124static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
125 char *const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400126{
127 int i;
128 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600129 int env_flag = H_HIDE_DOT;
130
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900131#if defined(CONFIG_CMD_NVEDIT_EFI)
132 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
133 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
134#endif
135
Joe Hershberger79a905e2012-12-11 22:16:23 -0600136 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
137 argc--;
138 argv++;
139 env_flag &= ~H_HIDE_DOT;
140 }
wdenka68d3ed2002-10-11 08:38:32 +0000141
Mike Frysinger87944922009-05-24 02:26:19 -0400142 if (argc == 1) {
143 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600144 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200145 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400146 return 1;
147 printf("\nEnvironment size: %d/%ld bytes\n",
148 rcode, (ulong)ENV_SIZE);
149 return 0;
150 }
wdenka68d3ed2002-10-11 08:38:32 +0000151
Mike Frysinger87944922009-05-24 02:26:19 -0400152 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600153 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400154 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600155 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200156 if (!rc) {
157 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400158 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000159 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000160 }
161
162 return rcode;
163}
164
165#ifdef CONFIG_CMD_GREPENV
Simon Glassed38aef2020-05-10 11:40:03 -0600166static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
167 int argc, char *const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000168{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000169 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000170 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000171
172 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000173 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000174
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000175 grep_how = H_MATCH_SUBSTR; /* default: substring search */
176 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000177
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200178 while (--argc > 0 && **++argv == '-') {
179 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000180 while (*++arg) {
181 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000182#ifdef CONFIG_REGEX
183 case 'e': /* use regex matching */
184 grep_how = H_MATCH_REGEX;
185 break;
186#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000187 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000188 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000189 break;
190 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000191 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000192 break;
193 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000194 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000195 break;
196 case '-':
197 goto DONE;
198 default:
199 return CMD_RET_USAGE;
200 }
201 }
202 }
203
204DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000205 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000206 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000207 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000208
Wolfgang Denke27fc122013-03-23 23:50:29 +0000209 if (len > 0) {
210 puts(res);
211 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000212 }
Mike Frysinger87944922009-05-24 02:26:19 -0400213
Wolfgang Denke27fc122013-03-23 23:50:29 +0000214 if (len < 2)
215 return 1;
216
217 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000218}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000219#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000220#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000221
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200222/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000223 * Set a new environment variable,
224 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600225 */
Simon Glassed38aef2020-05-10 11:40:03 -0600226static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000227{
228 int i, len;
229 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600230 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000231
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600232 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900233
Simon Glass70f58222023-02-05 15:36:36 -0700234#if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900235 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
236 return do_env_set_efi(NULL, flag, --argc, ++argv);
237#endif
238
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600239 while (argc > 1 && **(argv + 1) == '-') {
240 char *arg = *++argv;
241
242 --argc;
243 while (*++arg) {
244 switch (*arg) {
245 case 'f': /* force */
246 env_flag |= H_FORCE;
247 break;
248 default:
249 return CMD_RET_USAGE;
250 }
251 }
252 }
253 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000254 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000255
256 if (strchr(name, '=')) {
257 printf("## Error: illegal character '='"
258 "in variable name \"%s\"\n", name);
259 return 1;
260 }
261
262 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000263
wdenka68d3ed2002-10-11 08:38:32 +0000264 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000265 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600266 int rc = hdelete_r(name, &env_htab, env_flag);
Simon Glass783ef3f2020-11-05 10:33:37 -0700267
268 /* If the variable didn't exist, don't report an error */
269 return rc && rc != -ENOENT ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000270 }
271
272 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200273 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000274 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000275 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000276 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000277
278 value = malloc(len);
279 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200280 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000281 return 1;
282 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000283 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200284 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000285
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200286 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000287 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000288 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000289 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200290 if (s != value)
291 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000292
Igor Grinbergde832562011-11-07 01:13:59 +0000293 e.key = name;
294 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600295 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200296 free(value);
297 if (!ep) {
298 printf("## Error inserting \"%s\" variable, errno=%d\n",
299 name, errno);
300 return 1;
301 }
wdenka68d3ed2002-10-11 08:38:32 +0000302
wdenka68d3ed2002-10-11 08:38:32 +0000303 return 0;
304}
305
Simon Glass6a38e412017-08-03 12:22:09 -0600306int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000307{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200308 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
309
Joe Hershberger3902c602013-04-08 10:32:50 +0000310 /* before import into hashtable */
311 if (!(gd->flags & GD_FLG_ENV_READY))
312 return 1;
313
Igor Grinbergde832562011-11-07 01:13:59 +0000314 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500315 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200316 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500317 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000318}
319
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000320#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600321static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
322 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000323{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200324 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000325 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000326
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500327 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000328}
329
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200330/*
wdenka68d3ed2002-10-11 08:38:32 +0000331 * Prompt for environment variable
332 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500333#if defined(CONFIG_CMD_ASKENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600334int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000335{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200336 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000337 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000338 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000339 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000340
341 local_args[0] = argv[0];
342 local_args[1] = argv[1];
343 local_args[2] = NULL;
344 local_args[3] = NULL;
345
Wolfgang Denk586895d2013-02-20 04:53:16 +0000346 /*
347 * Check the syntax:
348 *
349 * env_ask envname [message1 ...] [size]
350 */
351 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000352 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000353
Wolfgang Denk586895d2013-02-20 04:53:16 +0000354 /*
355 * We test the last argument if it can be converted
356 * into a decimal number. If yes, we assume it's
357 * the size. Otherwise we echo it as part of the
358 * message.
359 */
Simon Glassff9b9032021-07-24 09:03:30 -0600360 i = dectoul(argv[argc - 1], &endptr);
Wolfgang Denk586895d2013-02-20 04:53:16 +0000361 if (*endptr != '\0') { /* no size */
362 size = CONFIG_SYS_CBSIZE - 1;
363 } else { /* size given */
364 size = i;
365 --argc;
366 }
wdenka68d3ed2002-10-11 08:38:32 +0000367
Wolfgang Denk586895d2013-02-20 04:53:16 +0000368 if (argc <= 2) {
369 sprintf(message, "Please enter '%s': ", argv[1]);
370 } else {
371 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400372 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000373 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000374 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000375
Tom Rini4beb2352017-09-26 19:37:11 -0400376 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000377 pos += strlen(argv[i]);
378 }
Tom Rini4beb2352017-09-26 19:37:11 -0400379 if (pos < sizeof(message) - 1) {
380 message[pos++] = ' ';
381 message[pos] = '\0';
382 } else
383 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000384 }
385
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200386 if (size >= CONFIG_SYS_CBSIZE)
387 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000388
389 if (size <= 0)
390 return 1;
391
392 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600393 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000394
395 if (size < len)
396 console_buffer[size] = '\0';
397
398 len = 2;
399 if (console_buffer[0] != '\0') {
400 local_args[2] = console_buffer;
401 len = 3;
402 }
403
404 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500405 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000406}
Jon Loeligerd704d912007-07-10 11:02:44 -0500407#endif
wdenka68d3ed2002-10-11 08:38:32 +0000408
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600409#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500410static int print_static_binding(const char *var_name, const char *callback_name,
411 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600412{
413 printf("\t%-20s %-20s\n", var_name, callback_name);
414
415 return 0;
416}
417
Simon Glass1a236862019-08-02 09:44:18 -0600418static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600419{
420 struct env_clbk_tbl *clbkp;
421 int i;
422 int num_callbacks;
423
424 if (entry->callback == NULL)
425 return 0;
426
427 /* look up the callback in the linker-list */
428 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
429 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
430 i < num_callbacks;
431 i++, clbkp++) {
432#if defined(CONFIG_NEEDS_MANUAL_RELOC)
433 if (entry->callback == clbkp->callback + gd->reloc_off)
434#else
435 if (entry->callback == clbkp->callback)
436#endif
437 break;
438 }
439
440 if (i == num_callbacks)
441 /* this should probably never happen, but just in case... */
442 printf("\t%-20s %p\n", entry->key, entry->callback);
443 else
444 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
445
446 return 0;
447}
448
449/*
450 * Print the callbacks available and what they are bound to
451 */
Simon Glassed38aef2020-05-10 11:40:03 -0600452int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
453 char *const argv[])
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600454{
455 struct env_clbk_tbl *clbkp;
456 int i;
457 int num_callbacks;
458
459 /* Print the available callbacks */
460 puts("Available callbacks:\n");
461 puts("\tCallback Name\n");
462 puts("\t-------------\n");
463 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
464 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
465 i < num_callbacks;
466 i++, clbkp++)
467 printf("\t%s\n", clbkp->name);
468 puts("\n");
469
470 /* Print the static bindings that may exist */
471 puts("Static callback bindings:\n");
472 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
473 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500474 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600475 puts("\n");
476
477 /* walk through each variable and print the callback if it has one */
478 puts("Active callback bindings:\n");
479 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
480 printf("\t%-20s %-20s\n", "-------------", "-------------");
481 hwalk_r(&env_htab, print_active_callback);
482 return 0;
483}
484#endif
485
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600486#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500487static int print_static_flags(const char *var_name, const char *flags,
488 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600489{
490 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600491 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600492
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600493 printf("\t%-20s %-20s %-20s\n", var_name,
494 env_flags_get_vartype_name(type),
495 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600496
497 return 0;
498}
499
Simon Glass1a236862019-08-02 09:44:18 -0600500static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600501{
502 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600503 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600504
505 if (entry->flags == 0)
506 return 0;
507
508 type = (enum env_flags_vartype)
509 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600510 access = env_flags_parse_varaccess_from_binflags(entry->flags);
511 printf("\t%-20s %-20s %-20s\n", entry->key,
512 env_flags_get_vartype_name(type),
513 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600514
515 return 0;
516}
517
518/*
519 * Print the flags available and what variables have flags
520 */
Simon Glassed38aef2020-05-10 11:40:03 -0600521int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600522{
523 /* Print the available variable types */
524 printf("Available variable type flags (position %d):\n",
525 ENV_FLAGS_VARTYPE_LOC);
526 puts("\tFlag\tVariable Type Name\n");
527 puts("\t----\t------------------\n");
528 env_flags_print_vartypes();
529 puts("\n");
530
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600531 /* Print the available variable access types */
532 printf("Available variable access flags (position %d):\n",
533 ENV_FLAGS_VARACCESS_LOC);
534 puts("\tFlag\tVariable Access Name\n");
535 puts("\t----\t--------------------\n");
536 env_flags_print_varaccess();
537 puts("\n");
538
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600539 /* Print the static flags that may exist */
540 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600541 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
542 "Variable Access");
543 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
544 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500545 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600546 puts("\n");
547
548 /* walk through each variable and print the flags if non-default */
549 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600550 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
551 "Variable Access");
552 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
553 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600554 hwalk_r(&env_htab, print_active_flags);
555 return 0;
556}
557#endif
558
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200559/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500560 * Interactively edit an environment variable
561 */
562#if defined(CONFIG_CMD_EDITENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600563static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
564 char *const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500565{
566 char buffer[CONFIG_SYS_CBSIZE];
567 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500568
Wolfgang Denk3b683112010-07-17 01:06:04 +0200569 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000570 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500571
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500572 /* before import into hashtable */
573 if (!(gd->flags & GD_FLG_ENV_READY))
574 return 1;
575
Peter Tyser0deafa22009-10-25 15:12:56 -0500576 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600577 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500578 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800579 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500580 else
581 buffer[0] = '\0';
582
Simon Glassbe6aafc2014-04-10 20:01:27 -0600583 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000584 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500585
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500586 if (buffer[0] == '\0') {
587 const char * const _argv[3] = { "setenv", argv[1], NULL };
588
589 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
590 } else {
591 const char * const _argv[4] = { "setenv", argv[1], buffer,
592 NULL };
593
594 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
595 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500596}
597#endif /* CONFIG_CMD_EDITENV */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200598
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100599#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Simon Glassed38aef2020-05-10 11:40:03 -0600600static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
601 char *const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000602{
Simon Glassd49b8892017-08-03 12:22:08 -0600603 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000604}
wdenk57b2d802003-06-27 21:31:46 +0000605
Mike Frysinger61583932008-12-30 02:59:25 -0500606U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200607 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600608 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200609 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500610);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200611
612#if defined(CONFIG_CMD_ERASEENV)
Simon Glassed38aef2020-05-10 11:40:03 -0600613static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
614 char *const argv[])
Frank Wunderlich33afa932019-06-29 11:36:19 +0200615{
616 return env_erase() ? 1 : 0;
617}
618
619U_BOOT_CMD(
620 eraseenv, 1, 0, do_env_erase,
621 "erase environment variables from persistent storage",
622 ""
623);
624#endif
wdenka68d3ed2002-10-11 08:38:32 +0000625#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200626
627#if defined(CONFIG_CMD_NVEDIT_LOAD)
628static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
629 char *const argv[])
630{
631 return env_reload() ? 1 : 0;
632}
633#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200634
635#if defined(CONFIG_CMD_NVEDIT_SELECT)
636static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
637 char *const argv[])
638{
639 return env_select(argv[1]) ? 1 : 0;
640}
641#endif
642
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000643#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000644
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000645#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600646static int do_env_default(struct cmd_tbl *cmdtp, int flag,
647 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200648{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300649 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000650
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000651 debug("Initial value for argc=%d\n", argc);
652 while (--argc > 0 && **++argv == '-') {
653 char *arg = *argv;
654
655 while (*++arg) {
656 switch (*arg) {
657 case 'a': /* default all */
658 all = 1;
659 break;
660 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300661 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000662 break;
663 default:
664 return cmd_usage(cmdtp);
665 }
666 }
667 }
668 debug("Final value for argc=%d\n", argc);
669 if (all && (argc == 0)) {
670 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600671 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300672 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000673 return 0;
674 }
675 if (!all && (argc > 0)) {
676 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600677 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000678 return 0;
679 }
680
681 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200682}
683
Simon Glassed38aef2020-05-10 11:40:03 -0600684static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
685 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200686{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600687 int env_flag = H_INTERACTIVE;
688 int ret = 0;
689
690 debug("Initial value for argc=%d\n", argc);
691 while (argc > 1 && **(argv + 1) == '-') {
692 char *arg = *++argv;
693
694 --argc;
695 while (*++arg) {
696 switch (*arg) {
697 case 'f': /* force */
698 env_flag |= H_FORCE;
699 break;
700 default:
701 return CMD_RET_USAGE;
702 }
703 }
704 }
705 debug("Final value for argc=%d\n", argc);
706
707 env_id++;
708
709 while (--argc > 0) {
710 char *name = *++argv;
711
Simon Glass783ef3f2020-11-05 10:33:37 -0700712 if (hdelete_r(name, &env_htab, env_flag))
Joe Hershberger8335ce72012-12-11 22:16:36 -0600713 ret = 1;
714 }
715
716 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200717}
718
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500719#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200720/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100721 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200722 * -t: export as text format; if size is given, data will be
723 * padded with '\0' bytes; if not, one terminating '\0'
724 * will be added (which is included in the "filesize"
725 * setting so you can for exmple copy this to flash and
726 * keep the termination).
727 * -b: export as binary format (name=value pairs separated by
728 * '\0', list end marked by double "\0\0")
729 * -c: export as checksum protected environment format as
730 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100731 * -s size:
732 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200733 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100734 * var... List of variable names that get included into the
735 * export. Without arguments, the whole environment gets
736 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200737 *
738 * With "-c" and size is NOT given, then the export command will
739 * format the data as currently used for the persistent storage,
740 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400741 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200742 * environment, a "current" redundancy flag. If size is given, this
743 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
744 * checksum and redundancy flag will be inserted.
745 *
746 * With "-b" and "-t", always only the real data (including a
747 * terminating '\0' byte) will be written; here the optional size
748 * argument will be used to make sure not to overflow the user
749 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400750 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200751 *
752 * On successful return, the variable "filesize" will be set.
753 * Note that filesize includes the trailing/terminating '\0' byte(s).
754 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400755 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200756 *
757 * => env export -t 100000
758 * => era ${backup_addr} +${filesize}
759 * => cp.b 100000 ${backup_addr} ${filesize}
760 *
761 * Re-import this snapshot, deleting all other settings:
762 *
763 * => env import -d -t ${backup_addr}
764 */
Simon Glassed38aef2020-05-10 11:40:03 -0600765static int do_env_export(struct cmd_tbl *cmdtp, int flag,
766 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200767{
768 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600769 ulong addr;
770 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100771 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200772 ssize_t len;
773 env_t *envp;
774 char sep = '\n';
775 int chk = 0;
776 int fmt = 0;
777
778 cmd = *argv;
779
780 while (--argc > 0 && **++argv == '-') {
781 char *arg = *argv;
782 while (*++arg) {
783 switch (*arg) {
784 case 'b': /* raw binary format */
785 if (fmt++)
786 goto sep_err;
787 sep = '\0';
788 break;
789 case 'c': /* external checksum format */
790 if (fmt++)
791 goto sep_err;
792 sep = '\0';
793 chk = 1;
794 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100795 case 's': /* size given */
796 if (--argc <= 0)
797 return cmd_usage(cmdtp);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600798 size = hextoul(*++argv, NULL);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100799 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200800 case 't': /* text format */
801 if (fmt++)
802 goto sep_err;
803 sep = '\n';
804 break;
805 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000806 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200807 }
808 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100809NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200810 }
811
Macpaul Linff2a9f32011-04-26 16:16:45 +0000812 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000813 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000814
Simon Glass3ff49ec2021-07-24 09:03:29 -0600815 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600816 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200817
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100818 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600819 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100820
821 argc--;
822 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200823
824 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000825 len = hexport_r(&env_htab, sep,
826 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600827 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200828 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200829 pr_err("## Error: Cannot export environment: errno = %d\n",
830 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200831 return 1;
832 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100833 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600834 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200835
836 return 0;
837 }
838
Simon Glassa7459262013-10-25 23:01:31 -0600839 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200840
841 if (chk) /* export as checksum protected block */
842 res = (char *)envp->data;
843 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -0600844 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200845
Wolfgang Denk565af272013-03-23 23:50:28 +0000846 len = hexport_r(&env_htab, '\0',
847 H_MATCH_KEY | H_MATCH_IDENT,
848 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200849 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200850 pr_err("## Error: Cannot export environment: errno = %d\n",
851 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200852 return 1;
853 }
854
855 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +0000856 envp->crc = crc32(0, envp->data,
857 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200858#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -0600859 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200860#endif
861 }
Simon Glass4d949a22017-08-03 12:22:10 -0600862 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200863
864 return 0;
865
866sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200867 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
868 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200869 return 1;
870}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500871#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200872
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500873#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200874/*
Quentin Schulz0312b942018-07-09 19:16:29 +0200875 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
876 * -d: delete existing environment before importing if no var is
877 * passed; if vars are passed, if one var is in the current
878 * environment but not in the environment at addr, delete var from
879 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400880 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200881 * -t: assume text format; either "size" must be given or the
882 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +0200883 * -r: handle CRLF like LF, that means exported variables with
884 * a content which ends with \r won't get imported. Used
885 * to import text files created with editors which are using CRLF
886 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200887 * -b: assume binary format ('\0' separated, "\0\0" terminated)
888 * -c: assume checksum protected environment format
889 * addr: memory address to read from
890 * size: length of input data; if missing, proper '\0'
891 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +0200892 * if var is set and size should be missing (i.e. '\0'
893 * termination), set size to '-'
894 * var... List of the names of the only variables that get imported from
895 * the environment at address 'addr'. Without arguments, the whole
896 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200897 */
Simon Glassed38aef2020-05-10 11:40:03 -0600898static int do_env_import(struct cmd_tbl *cmdtp, int flag,
899 int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200900{
Simon Glassa7459262013-10-25 23:01:31 -0600901 ulong addr;
902 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200903 char sep = '\n';
904 int chk = 0;
905 int fmt = 0;
906 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +0200907 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +0200908 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200909 size_t size;
910
911 cmd = *argv;
912
913 while (--argc > 0 && **++argv == '-') {
914 char *arg = *argv;
915 while (*++arg) {
916 switch (*arg) {
917 case 'b': /* raw binary format */
918 if (fmt++)
919 goto sep_err;
920 sep = '\0';
921 break;
922 case 'c': /* external checksum format */
923 if (fmt++)
924 goto sep_err;
925 sep = '\0';
926 chk = 1;
927 break;
928 case 't': /* text format */
929 if (fmt++)
930 goto sep_err;
931 sep = '\n';
932 break;
Alexander Holler3e12be72014-07-14 17:49:55 +0200933 case 'r': /* handle CRLF like LF */
934 crlf_is_lf = 1;
935 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200936 case 'd':
937 del = 1;
938 break;
939 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000940 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200941 }
942 }
943 }
944
Macpaul Linff2a9f32011-04-26 16:16:45 +0000945 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000946 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200947
948 if (!fmt)
949 printf("## Warning: defaulting to text format\n");
950
Alexander Holler3e12be72014-07-14 17:49:55 +0200951 if (sep != '\n' && crlf_is_lf )
952 crlf_is_lf = 0;
953
Simon Glass3ff49ec2021-07-24 09:03:29 -0600954 addr = hextoul(argv[0], NULL);
Simon Glassa7459262013-10-25 23:01:31 -0600955 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200956
Quentin Schulz0312b942018-07-09 19:16:29 +0200957 if (argc >= 2 && strcmp(argv[1], "-")) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600958 size = hextoul(argv[1], NULL);
Quentin Schulz0312b942018-07-09 19:16:29 +0200959 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -0500960 puts("## Error: external checksum format must pass size\n");
961 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200962 } else {
Simon Glassa7459262013-10-25 23:01:31 -0600963 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200964
965 size = 0;
966
967 while (size < MAX_ENV_SIZE) {
968 if ((*s == sep) && (*(s+1) == '\0'))
969 break;
970 ++s;
971 ++size;
972 }
973 if (size == MAX_ENV_SIZE) {
974 printf("## Warning: Input data exceeds %d bytes"
975 " - truncated\n", MAX_ENV_SIZE);
976 }
Horst Kronstorfer295da642011-12-16 23:33:10 +0000977 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +0000978 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200979 }
980
Quentin Schulz0312b942018-07-09 19:16:29 +0200981 if (argc > 2)
982 wl = 1;
983
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200984 if (chk) {
985 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -0600986 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200987
Pedro Aguilar834b5522020-08-31 11:01:41 +0200988 if (size <= offsetof(env_t, data)) {
989 printf("## Error: Invalid size 0x%zX\n", size);
990 return 1;
991 }
992
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200993 size -= offsetof(env_t, data);
994 memcpy(&crc, &ep->crc, sizeof(crc));
995
996 if (crc32(0, ep->data, size) != crc) {
997 puts("## Error: bad CRC, import failed\n");
998 return 1;
999 }
Simon Glassa7459262013-10-25 23:01:31 -06001000 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001001 }
1002
Quentin Schulz0312b942018-07-09 19:16:29 +02001003 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1004 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001005 pr_err("## Error: Environment import failed: errno = %d\n",
1006 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001007 return 1;
1008 }
1009 gd->flags |= GD_FLG_ENV_READY;
1010
1011 return 0;
1012
1013sep_err:
1014 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1015 cmd);
1016 return 1;
1017}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001018#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001019
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001020#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1021static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
1022 int argc, char *const argv[])
1023{
1024 char *to = argv[1];
1025 char *from = argv[2];
1026 char *default_value = NULL;
1027 int ret = 0;
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +01001028 char *val;
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001029
1030 if (argc < 3 || argc > 4) {
1031 return CMD_RET_USAGE;
1032 }
1033
1034 if (argc == 4) {
1035 default_value = argv[3];
1036 }
1037
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +01001038 val = env_get(from) ?: default_value;
1039 if (!val) {
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001040 printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
1041
1042 return CMD_RET_FAILURE;
1043 }
1044
Rasmus Villemoesd54ccb42023-03-06 14:27:21 +01001045 ret = env_set(to, val);
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001046
1047 if (ret == 0) {
1048 return CMD_RET_SUCCESS;
1049 }
1050 else {
1051 return CMD_RET_FAILURE;
1052 }
1053}
1054#endif
1055
Leo Ruan7450b872019-05-24 17:20:19 +02001056#if defined(CONFIG_CMD_NVEDIT_INFO)
1057/*
1058 * print_env_info - print environment information
1059 */
1060static int print_env_info(void)
1061{
1062 const char *value;
1063
1064 /* print environment validity value */
1065 switch (gd->env_valid) {
1066 case ENV_INVALID:
1067 value = "invalid";
1068 break;
1069 case ENV_VALID:
1070 value = "valid";
1071 break;
1072 case ENV_REDUND:
1073 value = "redundant";
1074 break;
1075 default:
1076 value = "unknown";
1077 break;
1078 }
1079 printf("env_valid = %s\n", value);
1080
1081 /* print environment ready flag */
1082 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1083 printf("env_ready = %s\n", value);
1084
1085 /* print environment using default flag */
1086 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1087 printf("env_use_default = %s\n", value);
1088
1089 return CMD_RET_SUCCESS;
1090}
1091
1092#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1093#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1094
1095/*
1096 * env info - display environment information
1097 * env info [-d] - evaluate whether default environment is used
1098 * env info [-p] - evaluate whether environment can be persisted
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001099 * Add [-q] - quiet mode, use only for command result, for test by example:
1100 * test env info -p -d -q
Leo Ruan7450b872019-05-24 17:20:19 +02001101 */
Simon Glassed38aef2020-05-10 11:40:03 -06001102static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1103 int argc, char *const argv[])
Leo Ruan7450b872019-05-24 17:20:19 +02001104{
1105 int eval_flags = 0;
1106 int eval_results = 0;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001107 bool quiet = false;
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001108#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1109 enum env_location loc;
1110#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001111
1112 /* display environment information */
1113 if (argc <= 1)
1114 return print_env_info();
1115
1116 /* process options */
1117 while (--argc > 0 && **++argv == '-') {
1118 char *arg = *argv;
1119
1120 while (*++arg) {
1121 switch (*arg) {
1122 case 'd':
1123 eval_flags |= ENV_INFO_IS_DEFAULT;
1124 break;
1125 case 'p':
1126 eval_flags |= ENV_INFO_IS_PERSISTED;
1127 break;
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001128 case 'q':
1129 quiet = true;
1130 break;
Leo Ruan7450b872019-05-24 17:20:19 +02001131 default:
1132 return CMD_RET_USAGE;
1133 }
1134 }
1135 }
1136
1137 /* evaluate whether default environment is used */
1138 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1139 if (gd->flags & GD_FLG_ENV_DEFAULT) {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001140 if (!quiet)
1141 printf("Default environment is used\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001142 eval_results |= ENV_INFO_IS_DEFAULT;
1143 } else {
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001144 if (!quiet)
1145 printf("Environment was loaded from persistent storage\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001146 }
1147 }
1148
1149 /* evaluate whether environment can be persisted */
1150 if (eval_flags & ENV_INFO_IS_PERSISTED) {
Patrick Delaunay4b876be2020-07-02 17:43:44 +02001151#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Patrick Delaunayc149a3b2020-06-19 14:03:35 +02001152 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1153 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1154 if (!quiet)
1155 printf("Environment can be persisted\n");
1156 eval_results |= ENV_INFO_IS_PERSISTED;
1157 } else {
1158 if (!quiet)
1159 printf("Environment cannot be persisted\n");
1160 }
Leo Ruan7450b872019-05-24 17:20:19 +02001161#else
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001162 if (!quiet)
1163 printf("Environment cannot be persisted\n");
Leo Ruan7450b872019-05-24 17:20:19 +02001164#endif
1165 }
1166
1167 /* The result of evaluations is combined with AND */
1168 if (eval_flags != eval_results)
1169 return CMD_RET_FAILURE;
1170
1171 return CMD_RET_SUCCESS;
1172}
1173#endif
1174
Andrew Ruder94463402013-10-22 19:07:34 -05001175#if defined(CONFIG_CMD_ENV_EXISTS)
Simon Glassed38aef2020-05-10 11:40:03 -06001176static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1177 char *const argv[])
Andrew Ruder94463402013-10-22 19:07:34 -05001178{
Simon Glass1a236862019-08-02 09:44:18 -06001179 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001180
1181 if (argc < 2)
1182 return CMD_RET_USAGE;
1183
1184 e.key = argv[1];
1185 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001186 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001187
1188 return (ep == NULL) ? 1 : 0;
1189}
1190#endif
1191
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001192/*
1193 * New command line interface: "env" command with subcommands
1194 */
Simon Glassed38aef2020-05-10 11:40:03 -06001195static struct cmd_tbl cmd_env_sub[] = {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001196#if defined(CONFIG_CMD_ASKENV)
1197 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1198#endif
1199 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001200 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001201#if defined(CONFIG_CMD_EDITENV)
1202 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1203#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001204#if defined(CONFIG_CMD_ENV_CALLBACK)
1205 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1206#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001207#if defined(CONFIG_CMD_ENV_FLAGS)
1208 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1209#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001210#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001211 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001212#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001213#if defined(CONFIG_CMD_GREPENV)
1214 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1215#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001216#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001217 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001218#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001219#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1220 U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
1221#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001222#if defined(CONFIG_CMD_NVEDIT_INFO)
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001223 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
Leo Ruan7450b872019-05-24 17:20:19 +02001224#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001225#if defined(CONFIG_CMD_NVEDIT_LOAD)
1226 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
1227#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001228 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1229#if defined(CONFIG_CMD_RUN)
1230 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1231#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001232#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001233 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001234#if defined(CONFIG_CMD_ERASEENV)
1235 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1236#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001237#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001238#if defined(CONFIG_CMD_NVEDIT_SELECT)
1239 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
1240#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001241 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001242#if defined(CONFIG_CMD_ENV_EXISTS)
1243 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1244#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001245};
1246
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001247#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001248void env_reloc(void)
1249{
1250 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1251}
1252#endif
1253
Simon Glassed38aef2020-05-10 11:40:03 -06001254static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001255{
Simon Glassed38aef2020-05-10 11:40:03 -06001256 struct cmd_tbl *cp;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001257
Thomas Weber4d282652010-11-24 13:07:52 +01001258 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001259 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001260
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001261 /* drop initial "env" arg */
1262 argc--;
1263 argv++;
1264
1265 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1266
1267 if (cp)
1268 return cp->cmd(cmdtp, flag, argc, argv);
1269
Simon Glassa06dfc72011-12-10 08:44:01 +00001270 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001271}
1272
Kim Phillipsdc00a682012-10-29 13:34:31 +00001273#ifdef CONFIG_SYS_LONGHELP
1274static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001275#if defined(CONFIG_CMD_ASKENV)
1276 "ask name [message] [size] - ask for environment variable\nenv "
1277#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001278#if defined(CONFIG_CMD_ENV_CALLBACK)
1279 "callbacks - print callbacks and their associated variables\nenv "
1280#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001281 "default [-f] -a - [forcibly] reset default environment\n"
1282 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001283 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001284#if defined(CONFIG_CMD_EDITENV)
1285 "env edit name - edit environment variable\n"
1286#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001287#if defined(CONFIG_CMD_ENV_EXISTS)
1288 "env exists name - tests for existence of variable\n"
1289#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001290#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001291 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001292#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001293#if defined(CONFIG_CMD_ENV_FLAGS)
1294 "env flags - print variables that have non-default flags\n"
1295#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001296#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001297#ifdef CONFIG_REGEX
1298 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1299#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001300 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001301#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001302#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001303#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001304 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001305#endif
Samuel Dionne-Riel46127db2021-12-20 18:31:56 -05001306#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
1307 "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
1308#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001309#if defined(CONFIG_CMD_NVEDIT_INFO)
1310 "env info - display environment information\n"
Patrick Delaunay109ec8f2020-06-19 14:03:34 +02001311 "env info [-d] [-p] [-q] - evaluate environment information\n"
1312 " \"-d\": default environment is used\n"
1313 " \"-p\": environment can be persisted\n"
1314 " \"-q\": quiet output\n"
Leo Ruan7450b872019-05-24 17:20:19 +02001315#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001316 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001317#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001318 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001319#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001320#if defined(CONFIG_CMD_RUN)
1321 "env run var [...] - run commands in an environment variable\n"
1322#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001323#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001324 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001325#if defined(CONFIG_CMD_ERASEENV)
1326 "env erase - erase environment\n"
1327#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001328#endif
Patrick Delaunay748e42e2020-07-28 11:51:20 +02001329#if defined(CONFIG_CMD_NVEDIT_LOAD)
1330 "env load - load environment\n"
1331#endif
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +02001332#if defined(CONFIG_CMD_NVEDIT_SELECT)
1333 "env select [target] - select environment target\n"
1334#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001335#if defined(CONFIG_CMD_NVEDIT_EFI)
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001336 "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001337 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001338#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001339 "env set [-f] name [arg ...]\n";
1340#endif
1341
1342U_BOOT_CMD(
1343 env, CONFIG_SYS_MAXARGS, 1, do_env,
1344 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001345);
1346
1347/*
1348 * Old command line interface, kept for compatibility
1349 */
wdenk57b2d802003-06-27 21:31:46 +00001350
Peter Tyser0deafa22009-10-25 15:12:56 -05001351#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001352U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001353 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001354 "edit environment variable",
1355 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001356 " - edit environment variable 'name'",
1357 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001358);
1359#endif
1360
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001361U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001362 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001363 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001364 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001365#if defined(CONFIG_CMD_NVEDIT_EFI)
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001366 "printenv -e [-guid guid][-n] [name ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001367 " - print UEFI variable 'name' or all the variables\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001368 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001369 " \"-n\": suppress dumping variable's value\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001370#endif
wdenk57b2d802003-06-27 21:31:46 +00001371 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001372 " - print value of environment variable 'name'",
1373 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001374);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001375
1376#ifdef CONFIG_CMD_GREPENV
1377U_BOOT_CMD_COMPLETE(
1378 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1379 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001380#ifdef CONFIG_REGEX
1381 "[-e] [-n | -v | -b] string ...\n"
1382#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001383 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001384#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001385 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001386#ifdef CONFIG_REGEX
1387 " \"-e\": enable regular expressions;\n"
1388#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001389 " \"-n\": search variable names; \"-v\": search values;\n"
1390 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001391 var_complete
1392);
1393#endif
wdenk57b2d802003-06-27 21:31:46 +00001394
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001395U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001396 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001397 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001398#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001399 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
Maxim Uvarovd9858d62020-08-28 22:20:10 +03001400 " [-i addr:size name], or [name [value ...]]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001401 " - set UEFI variable 'name' to 'value' ...'\n"
Heinrich Schuchardt7efbf7e2020-07-15 18:00:56 +02001402 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001403 " \"-nv\": set non-volatile attribute\n"
1404 " \"-bs\": set boot-service attribute\n"
1405 " \"-rt\": set runtime attribute\n"
AKASHI Takahirobd7defa2020-04-14 11:51:47 +09001406 " \"-at\": set time-based authentication attribute\n"
AKASHI Takahiro2e952122019-10-24 15:17:13 +09001407 " \"-a\": append-write\n"
1408 " \"-i addr,size\": use <addr,size> as variable's value\n"
1409 " \"-v\": verbose message\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001410 " - delete UEFI variable 'name' if 'value' not specified\n"
1411#endif
1412 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001413 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1414 "setenv [-f] name\n"
1415 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001416 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001417);
1418
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001419#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001420
wdenkf287a242003-07-01 21:06:45 +00001421U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001422 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001423 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001424 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001425 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001426);
Jon Loeligerd704d912007-07-10 11:02:44 -05001427#endif
wdenk57b2d802003-06-27 21:31:46 +00001428
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001429#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001430U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001431 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001432 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001433 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001434 " - run the commands in the environment variable(s) 'var'",
1435 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001436);
Jon Loeligerd704d912007-07-10 11:02:44 -05001437#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001438#endif /* CONFIG_SPL_BUILD */