blob: 1cb0bc1460b951c146078d007cd8a4d62db30e53 [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>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050034#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050035#include <mapmem.h>
wdenk2a3cb022002-11-05 21:01:48 +000036#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000037#include <linux/stddef.h>
38#include <asm/byteorder.h>
Simon Glassa7459262013-10-25 23:01:31 -060039#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000040
Wolfgang Denk6405a152006-03-31 18:32:53 +020041DECLARE_GLOBAL_DATA_PTR;
42
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010043#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
44 defined(CONFIG_ENV_IS_IN_FLASH) || \
45 defined(CONFIG_ENV_IS_IN_MMC) || \
46 defined(CONFIG_ENV_IS_IN_FAT) || \
47 defined(CONFIG_ENV_IS_IN_EXT4) || \
48 defined(CONFIG_ENV_IS_IN_NAND) || \
49 defined(CONFIG_ENV_IS_IN_NVRAM) || \
50 defined(CONFIG_ENV_IS_IN_ONENAND) || \
51 defined(CONFIG_ENV_IS_IN_SATA) || \
52 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
53 defined(CONFIG_ENV_IS_IN_REMOTE) || \
54 defined(CONFIG_ENV_IS_IN_UBI)
55
56#define ENV_IS_IN_DEVICE
57
58#endif
59
60#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000061 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030062# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020063NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000064#endif
65
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020066/*
67 * Maximum expected input data size for import command
68 */
69#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000070
wdenka68d3ed2002-10-11 08:38:32 +000071/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020072 * This variable is incremented on each do_env_set(), so it can
Simon Glass2d85a752019-08-01 09:46:41 -060073 * be used via env_get_id() as an indication, if the environment
Heiko Schocher3b195ff2009-04-28 08:36:11 +020074 * has changed or not. So it is possible to reread an environment
75 * variable only if the environment was changed ... done so for
76 * example in NetInitLoop()
77 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010078static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000079
Simon Glass2d85a752019-08-01 09:46:41 -060080int env_get_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010081{
82 return env_id;
83}
wdenka68d3ed2002-10-11 08:38:32 +000084
Ilya Yanokf7a2c552012-09-18 00:22:50 +000085#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040086/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020087 * Command interface: print one or all environment variables
88 *
89 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040090 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060091static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000092{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020093 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000094 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000095
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020096 if (name) { /* print a single name */
Simon Glass1a236862019-08-02 09:44:18 -060097 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000098
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020099 e.key = name;
100 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600101 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200102 if (ep == NULL)
103 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000104 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200105 return len;
106 }
wdenka68d3ed2002-10-11 08:38:32 +0000107
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200108 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600109 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000110
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200111 if (len > 0) {
112 puts(res);
113 free(res);
114 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000115 }
116
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200117 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000118 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200119 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400120}
wdenka68d3ed2002-10-11 08:38:32 +0000121
Kim Phillipsdc00a682012-10-29 13:34:31 +0000122static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
123 char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400124{
125 int i;
126 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600127 int env_flag = H_HIDE_DOT;
128
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900129#if defined(CONFIG_CMD_NVEDIT_EFI)
130 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
131 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
132#endif
133
Joe Hershberger79a905e2012-12-11 22:16:23 -0600134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
135 argc--;
136 argv++;
137 env_flag &= ~H_HIDE_DOT;
138 }
wdenka68d3ed2002-10-11 08:38:32 +0000139
Mike Frysinger87944922009-05-24 02:26:19 -0400140 if (argc == 1) {
141 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600142 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200143 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400144 return 1;
145 printf("\nEnvironment size: %d/%ld bytes\n",
146 rcode, (ulong)ENV_SIZE);
147 return 0;
148 }
wdenka68d3ed2002-10-11 08:38:32 +0000149
Mike Frysinger87944922009-05-24 02:26:19 -0400150 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600151 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400152 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600153 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200154 if (!rc) {
155 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400156 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000157 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000158 }
159
160 return rcode;
161}
162
163#ifdef CONFIG_CMD_GREPENV
Igor Grinbergde832562011-11-07 01:13:59 +0000164static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165 int argc, char * const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000166{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000167 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000168 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000169
170 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000171 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000172
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000173 grep_how = H_MATCH_SUBSTR; /* default: substring search */
174 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000175
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200176 while (--argc > 0 && **++argv == '-') {
177 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000178 while (*++arg) {
179 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000180#ifdef CONFIG_REGEX
181 case 'e': /* use regex matching */
182 grep_how = H_MATCH_REGEX;
183 break;
184#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000185 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000186 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000187 break;
188 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000189 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000190 break;
191 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000192 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000193 break;
194 case '-':
195 goto DONE;
196 default:
197 return CMD_RET_USAGE;
198 }
199 }
200 }
201
202DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000203 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000204 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000205 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000206
Wolfgang Denke27fc122013-03-23 23:50:29 +0000207 if (len > 0) {
208 puts(res);
209 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000210 }
Mike Frysinger87944922009-05-24 02:26:19 -0400211
Wolfgang Denke27fc122013-03-23 23:50:29 +0000212 if (len < 2)
213 return 1;
214
215 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000216}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000217#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000218#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000219
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200220/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000221 * Set a new environment variable,
222 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600223 */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500224static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000225{
226 int i, len;
227 char *name, *value, *s;
Simon Glass1a236862019-08-02 09:44:18 -0600228 struct env_entry e, *ep;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000229
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600230 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900231
232#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
233 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
234 return do_env_set_efi(NULL, flag, --argc, ++argv);
235#endif
236
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600237 while (argc > 1 && **(argv + 1) == '-') {
238 char *arg = *++argv;
239
240 --argc;
241 while (*++arg) {
242 switch (*arg) {
243 case 'f': /* force */
244 env_flag |= H_FORCE;
245 break;
246 default:
247 return CMD_RET_USAGE;
248 }
249 }
250 }
251 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000252 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000253
254 if (strchr(name, '=')) {
255 printf("## Error: illegal character '='"
256 "in variable name \"%s\"\n", name);
257 return 1;
258 }
259
260 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000261
wdenka68d3ed2002-10-11 08:38:32 +0000262 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000263 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600264 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200265 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000266 }
267
268 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000270 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000271 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000272 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000273
274 value = malloc(len);
275 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200276 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000277 return 1;
278 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000279 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200280 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000281
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200282 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000283 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000284 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000285 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200286 if (s != value)
287 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000288
Igor Grinbergde832562011-11-07 01:13:59 +0000289 e.key = name;
290 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600291 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200292 free(value);
293 if (!ep) {
294 printf("## Error inserting \"%s\" variable, errno=%d\n",
295 name, errno);
296 return 1;
297 }
wdenka68d3ed2002-10-11 08:38:32 +0000298
wdenka68d3ed2002-10-11 08:38:32 +0000299 return 0;
300}
301
Simon Glass6a38e412017-08-03 12:22:09 -0600302int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000303{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200304 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
305
Joe Hershberger3902c602013-04-08 10:32:50 +0000306 /* before import into hashtable */
307 if (!(gd->flags & GD_FLG_ENV_READY))
308 return 1;
309
Igor Grinbergde832562011-11-07 01:13:59 +0000310 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500311 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200312 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500313 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000314}
315
Simon Glassb8e926e2011-10-24 17:59:59 +0000316/**
317 * Set an environment variable to an integer value
318 *
Simon Glassdf372d62013-05-07 06:11:45 +0000319 * @param varname Environment variable to set
Simon Glassb8e926e2011-10-24 17:59:59 +0000320 * @param value Value to set it to
321 * @return 0 if ok, 1 on error
322 */
Simon Glass4d949a22017-08-03 12:22:10 -0600323int env_set_ulong(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000324{
325 /* TODO: this should be unsigned */
326 char *str = simple_itoa(value);
327
Simon Glass6a38e412017-08-03 12:22:09 -0600328 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000329}
330
331/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000332 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000333 *
Simon Glassdf372d62013-05-07 06:11:45 +0000334 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000335 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000336 * @return 0 if ok, 1 on error
337 */
Simon Glass4d949a22017-08-03 12:22:10 -0600338int env_set_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000339{
340 char str[17];
341
Simon Glass044dc9e2013-02-24 17:33:21 +0000342 sprintf(str, "%lx", value);
Simon Glass6a38e412017-08-03 12:22:09 -0600343 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000344}
345
Simon Glass22c34c22017-08-03 12:22:13 -0600346ulong env_get_hex(const char *varname, ulong default_val)
Simon Glassabbd5122013-04-20 08:42:43 +0000347{
348 const char *s;
349 ulong value;
350 char *endp;
351
Simon Glass64b723f2017-08-03 12:22:12 -0600352 s = env_get(varname);
Simon Glassabbd5122013-04-20 08:42:43 +0000353 if (s)
354 value = simple_strtoul(s, &endp, 16);
355 if (!s || endp == s)
356 return default_val;
357
358 return value;
359}
360
Alex Kiernan9c215492018-04-01 09:22:38 +0000361int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
362{
363 eth_parse_enetaddr(env_get(name), enetaddr);
364 return is_valid_ethaddr(enetaddr);
365}
366
367int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
368{
369 char buf[ARP_HLEN_ASCII + 1];
370
371 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
372 return -EEXIST;
373
374 sprintf(buf, "%pM", enetaddr);
375
376 return env_set(name, buf);
377}
378
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000379#ifndef CONFIG_SPL_BUILD
Kim Phillipsdc00a682012-10-29 13:34:31 +0000380static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000381{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200382 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000383 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000384
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500385 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000386}
387
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200388/*
wdenka68d3ed2002-10-11 08:38:32 +0000389 * Prompt for environment variable
390 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500391#if defined(CONFIG_CMD_ASKENV)
Macpaul Linff2a9f32011-04-26 16:16:45 +0000392int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000393{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200394 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000395 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000396 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000397 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000398
399 local_args[0] = argv[0];
400 local_args[1] = argv[1];
401 local_args[2] = NULL;
402 local_args[3] = NULL;
403
Wolfgang Denk586895d2013-02-20 04:53:16 +0000404 /*
405 * Check the syntax:
406 *
407 * env_ask envname [message1 ...] [size]
408 */
409 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000410 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000411
Wolfgang Denk586895d2013-02-20 04:53:16 +0000412 /*
413 * We test the last argument if it can be converted
414 * into a decimal number. If yes, we assume it's
415 * the size. Otherwise we echo it as part of the
416 * message.
417 */
418 i = simple_strtoul(argv[argc - 1], &endptr, 10);
419 if (*endptr != '\0') { /* no size */
420 size = CONFIG_SYS_CBSIZE - 1;
421 } else { /* size given */
422 size = i;
423 --argc;
424 }
wdenka68d3ed2002-10-11 08:38:32 +0000425
Wolfgang Denk586895d2013-02-20 04:53:16 +0000426 if (argc <= 2) {
427 sprintf(message, "Please enter '%s': ", argv[1]);
428 } else {
429 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400430 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000431 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000432 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000433
Tom Rini4beb2352017-09-26 19:37:11 -0400434 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000435 pos += strlen(argv[i]);
436 }
Tom Rini4beb2352017-09-26 19:37:11 -0400437 if (pos < sizeof(message) - 1) {
438 message[pos++] = ' ';
439 message[pos] = '\0';
440 } else
441 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000442 }
443
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200444 if (size >= CONFIG_SYS_CBSIZE)
445 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000446
447 if (size <= 0)
448 return 1;
449
450 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600451 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000452
453 if (size < len)
454 console_buffer[size] = '\0';
455
456 len = 2;
457 if (console_buffer[0] != '\0') {
458 local_args[2] = console_buffer;
459 len = 3;
460 }
461
462 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500463 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000464}
Jon Loeligerd704d912007-07-10 11:02:44 -0500465#endif
wdenka68d3ed2002-10-11 08:38:32 +0000466
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600467#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500468static int print_static_binding(const char *var_name, const char *callback_name,
469 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600470{
471 printf("\t%-20s %-20s\n", var_name, callback_name);
472
473 return 0;
474}
475
Simon Glass1a236862019-08-02 09:44:18 -0600476static int print_active_callback(struct env_entry *entry)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600477{
478 struct env_clbk_tbl *clbkp;
479 int i;
480 int num_callbacks;
481
482 if (entry->callback == NULL)
483 return 0;
484
485 /* look up the callback in the linker-list */
486 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
487 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
488 i < num_callbacks;
489 i++, clbkp++) {
490#if defined(CONFIG_NEEDS_MANUAL_RELOC)
491 if (entry->callback == clbkp->callback + gd->reloc_off)
492#else
493 if (entry->callback == clbkp->callback)
494#endif
495 break;
496 }
497
498 if (i == num_callbacks)
499 /* this should probably never happen, but just in case... */
500 printf("\t%-20s %p\n", entry->key, entry->callback);
501 else
502 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
503
504 return 0;
505}
506
507/*
508 * Print the callbacks available and what they are bound to
509 */
510int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
511{
512 struct env_clbk_tbl *clbkp;
513 int i;
514 int num_callbacks;
515
516 /* Print the available callbacks */
517 puts("Available callbacks:\n");
518 puts("\tCallback Name\n");
519 puts("\t-------------\n");
520 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
521 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
522 i < num_callbacks;
523 i++, clbkp++)
524 printf("\t%s\n", clbkp->name);
525 puts("\n");
526
527 /* Print the static bindings that may exist */
528 puts("Static callback bindings:\n");
529 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
530 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500531 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600532 puts("\n");
533
534 /* walk through each variable and print the callback if it has one */
535 puts("Active callback bindings:\n");
536 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
537 printf("\t%-20s %-20s\n", "-------------", "-------------");
538 hwalk_r(&env_htab, print_active_callback);
539 return 0;
540}
541#endif
542
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600543#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500544static int print_static_flags(const char *var_name, const char *flags,
545 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600546{
547 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600548 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600549
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600550 printf("\t%-20s %-20s %-20s\n", var_name,
551 env_flags_get_vartype_name(type),
552 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600553
554 return 0;
555}
556
Simon Glass1a236862019-08-02 09:44:18 -0600557static int print_active_flags(struct env_entry *entry)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600558{
559 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600560 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600561
562 if (entry->flags == 0)
563 return 0;
564
565 type = (enum env_flags_vartype)
566 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600567 access = env_flags_parse_varaccess_from_binflags(entry->flags);
568 printf("\t%-20s %-20s %-20s\n", entry->key,
569 env_flags_get_vartype_name(type),
570 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600571
572 return 0;
573}
574
575/*
576 * Print the flags available and what variables have flags
577 */
578int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
579{
580 /* Print the available variable types */
581 printf("Available variable type flags (position %d):\n",
582 ENV_FLAGS_VARTYPE_LOC);
583 puts("\tFlag\tVariable Type Name\n");
584 puts("\t----\t------------------\n");
585 env_flags_print_vartypes();
586 puts("\n");
587
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600588 /* Print the available variable access types */
589 printf("Available variable access flags (position %d):\n",
590 ENV_FLAGS_VARACCESS_LOC);
591 puts("\tFlag\tVariable Access Name\n");
592 puts("\t----\t--------------------\n");
593 env_flags_print_varaccess();
594 puts("\n");
595
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600596 /* Print the static flags that may exist */
597 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600598 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
599 "Variable Access");
600 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
601 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500602 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600603 puts("\n");
604
605 /* walk through each variable and print the flags if non-default */
606 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600607 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
608 "Variable Access");
609 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
610 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600611 hwalk_r(&env_htab, print_active_flags);
612 return 0;
613}
614#endif
615
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200616/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500617 * Interactively edit an environment variable
618 */
619#if defined(CONFIG_CMD_EDITENV)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000620static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
621 char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500622{
623 char buffer[CONFIG_SYS_CBSIZE];
624 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500625
Wolfgang Denk3b683112010-07-17 01:06:04 +0200626 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000627 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500628
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500629 /* before import into hashtable */
630 if (!(gd->flags & GD_FLG_ENV_READY))
631 return 1;
632
Peter Tyser0deafa22009-10-25 15:12:56 -0500633 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600634 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500635 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800636 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500637 else
638 buffer[0] = '\0';
639
Simon Glassbe6aafc2014-04-10 20:01:27 -0600640 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000641 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500642
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500643 if (buffer[0] == '\0') {
644 const char * const _argv[3] = { "setenv", argv[1], NULL };
645
646 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
647 } else {
648 const char * const _argv[4] = { "setenv", argv[1], buffer,
649 NULL };
650
651 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
652 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500653}
654#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000655#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500656
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200657/*
wdenka68d3ed2002-10-11 08:38:32 +0000658 * Look up variable from environment,
659 * return address of storage for that variable,
660 * or NULL if not found
661 */
Simon Glass64b723f2017-08-03 12:22:12 -0600662char *env_get(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000663{
Igor Grinbergde832562011-11-07 01:13:59 +0000664 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Simon Glass1a236862019-08-02 09:44:18 -0600665 struct env_entry e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000666
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200667 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000668
Igor Grinbergde832562011-11-07 01:13:59 +0000669 e.key = name;
670 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600671 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200672
Macpaul Linff2a9f32011-04-26 16:16:45 +0000673 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000674 }
675
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200676 /* restricted capabilities before import */
Simon Glass64b723f2017-08-03 12:22:12 -0600677 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200678 return (char *)(gd->env_buf);
679
680 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000681}
682
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200683/*
684 * Look up variable from environment for restricted C runtime env.
685 */
Simon Glass64b723f2017-08-03 12:22:12 -0600686int env_get_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000687{
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100688 int i, nxt, c;
wdenka68d3ed2002-10-11 08:38:32 +0000689
Igor Grinbergde832562011-11-07 01:13:59 +0000690 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000691 int val, n;
692
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100693 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
694 if (c < 0)
695 return c;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000696 if (nxt >= CONFIG_ENV_SIZE)
697 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000698 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000699
Simon Glass909ff4d2019-08-01 09:46:45 -0600700 val = env_match((uchar *)name, i);
Macpaul Linff2a9f32011-04-26 16:16:45 +0000701 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000702 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200703
wdenka68d3ed2002-10-11 08:38:32 +0000704 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000705 for (n = 0; n < len; ++n, ++buf) {
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100706 c = env_get_char(val++);
707 if (c < 0)
708 return c;
709 *buf = c;
Igor Grinbergde832562011-11-07 01:13:59 +0000710 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200711 return n;
712 }
713
714 if (n)
715 *--buf = '\0';
716
Heinrich Schuchardtcf80bd72019-01-06 11:52:06 +0100717 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
718 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200719
720 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000721 }
Igor Grinbergde832562011-11-07 01:13:59 +0000722
Macpaul Linff2a9f32011-04-26 16:16:45 +0000723 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000724}
725
Simon Glassbf690f82011-10-14 13:25:18 +0000726/**
727 * Decode the integer value of an environment variable and return it.
728 *
Shyam Sainiefe757c2018-06-07 19:47:19 +0530729 * @param name Name of environment variable
Simon Glassbf690f82011-10-14 13:25:18 +0000730 * @param base Number base to use (normally 10, or 16 for hex)
731 * @param default_val Default value to return if the variable is not
732 * found
733 * @return the decoded value, or default_val if not found
734 */
Simon Glass22c34c22017-08-03 12:22:13 -0600735ulong env_get_ulong(const char *name, int base, ulong default_val)
Simon Glassbf690f82011-10-14 13:25:18 +0000736{
737 /*
Simon Glass64b723f2017-08-03 12:22:12 -0600738 * We can use env_get() here, even before relocation, since the
Simon Glassbf690f82011-10-14 13:25:18 +0000739 * environment variable value is an integer and thus short.
740 */
Simon Glass64b723f2017-08-03 12:22:12 -0600741 const char *str = env_get(name);
Simon Glassbf690f82011-10-14 13:25:18 +0000742
743 return str ? simple_strtoul(str, NULL, base) : default_val;
744}
745
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000746#ifndef CONFIG_SPL_BUILD
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100747#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000748static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
749 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000750{
Simon Glassd49b8892017-08-03 12:22:08 -0600751 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000752}
wdenk57b2d802003-06-27 21:31:46 +0000753
Mike Frysinger61583932008-12-30 02:59:25 -0500754U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200755 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600756 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200757 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500758);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200759
760#if defined(CONFIG_CMD_ERASEENV)
761static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
762 char * const argv[])
763{
764 return env_erase() ? 1 : 0;
765}
766
767U_BOOT_CMD(
768 eraseenv, 1, 0, do_env_erase,
769 "erase environment variables from persistent storage",
770 ""
771);
772#endif
wdenka68d3ed2002-10-11 08:38:32 +0000773#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000774#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000775
Simon Glass909ff4d2019-08-01 09:46:45 -0600776int env_match(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000777{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000778 if (s1 == NULL)
779 return -1;
780
wdenka68d3ed2002-10-11 08:38:32 +0000781 while (*s1 == env_get_char(i2++))
782 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000783 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000784
wdenka68d3ed2002-10-11 08:38:32 +0000785 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000786 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000787
Macpaul Linff2a9f32011-04-26 16:16:45 +0000788 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000789}
wdenk57b2d802003-06-27 21:31:46 +0000790
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000791#ifndef CONFIG_SPL_BUILD
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300792static int do_env_default(cmd_tbl_t *cmdtp, int flag,
Igor Grinbergde832562011-11-07 01:13:59 +0000793 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200794{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300795 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000796
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000797 debug("Initial value for argc=%d\n", argc);
798 while (--argc > 0 && **++argv == '-') {
799 char *arg = *argv;
800
801 while (*++arg) {
802 switch (*arg) {
803 case 'a': /* default all */
804 all = 1;
805 break;
806 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300807 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000808 break;
809 default:
810 return cmd_usage(cmdtp);
811 }
812 }
813 }
814 debug("Final value for argc=%d\n", argc);
815 if (all && (argc == 0)) {
816 /* Reset the whole environment */
Simon Glass97385862019-08-01 09:47:00 -0600817 env_set_default("## Resetting to default environment\n",
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300818 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000819 return 0;
820 }
821 if (!all && (argc > 0)) {
822 /* Reset individual variables */
Simon Glasseec97962019-08-01 09:46:56 -0600823 env_set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000824 return 0;
825 }
826
827 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200828}
829
Igor Grinbergde832562011-11-07 01:13:59 +0000830static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
831 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200832{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600833 int env_flag = H_INTERACTIVE;
834 int ret = 0;
835
836 debug("Initial value for argc=%d\n", argc);
837 while (argc > 1 && **(argv + 1) == '-') {
838 char *arg = *++argv;
839
840 --argc;
841 while (*++arg) {
842 switch (*arg) {
843 case 'f': /* force */
844 env_flag |= H_FORCE;
845 break;
846 default:
847 return CMD_RET_USAGE;
848 }
849 }
850 }
851 debug("Final value for argc=%d\n", argc);
852
853 env_id++;
854
855 while (--argc > 0) {
856 char *name = *++argv;
857
858 if (!hdelete_r(name, &env_htab, env_flag))
859 ret = 1;
860 }
861
862 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200863}
864
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500865#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200866/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100867 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200868 * -t: export as text format; if size is given, data will be
869 * padded with '\0' bytes; if not, one terminating '\0'
870 * will be added (which is included in the "filesize"
871 * setting so you can for exmple copy this to flash and
872 * keep the termination).
873 * -b: export as binary format (name=value pairs separated by
874 * '\0', list end marked by double "\0\0")
875 * -c: export as checksum protected environment format as
876 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100877 * -s size:
878 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200879 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100880 * var... List of variable names that get included into the
881 * export. Without arguments, the whole environment gets
882 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200883 *
884 * With "-c" and size is NOT given, then the export command will
885 * format the data as currently used for the persistent storage,
886 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400887 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200888 * environment, a "current" redundancy flag. If size is given, this
889 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
890 * checksum and redundancy flag will be inserted.
891 *
892 * With "-b" and "-t", always only the real data (including a
893 * terminating '\0' byte) will be written; here the optional size
894 * argument will be used to make sure not to overflow the user
895 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400896 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200897 *
898 * On successful return, the variable "filesize" will be set.
899 * Note that filesize includes the trailing/terminating '\0' byte(s).
900 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400901 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200902 *
903 * => env export -t 100000
904 * => era ${backup_addr} +${filesize}
905 * => cp.b 100000 ${backup_addr} ${filesize}
906 *
907 * Re-import this snapshot, deleting all other settings:
908 *
909 * => env import -d -t ${backup_addr}
910 */
Igor Grinbergde832562011-11-07 01:13:59 +0000911static int do_env_export(cmd_tbl_t *cmdtp, int flag,
912 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200913{
914 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600915 ulong addr;
916 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100917 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200918 ssize_t len;
919 env_t *envp;
920 char sep = '\n';
921 int chk = 0;
922 int fmt = 0;
923
924 cmd = *argv;
925
926 while (--argc > 0 && **++argv == '-') {
927 char *arg = *argv;
928 while (*++arg) {
929 switch (*arg) {
930 case 'b': /* raw binary format */
931 if (fmt++)
932 goto sep_err;
933 sep = '\0';
934 break;
935 case 'c': /* external checksum format */
936 if (fmt++)
937 goto sep_err;
938 sep = '\0';
939 chk = 1;
940 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100941 case 's': /* size given */
942 if (--argc <= 0)
943 return cmd_usage(cmdtp);
944 size = simple_strtoul(*++argv, NULL, 16);
945 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200946 case 't': /* text format */
947 if (fmt++)
948 goto sep_err;
949 sep = '\n';
950 break;
951 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000952 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200953 }
954 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100955NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200956 }
957
Macpaul Linff2a9f32011-04-26 16:16:45 +0000958 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000959 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000960
Simon Glassa7459262013-10-25 23:01:31 -0600961 addr = simple_strtoul(argv[0], NULL, 16);
962 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200963
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100964 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600965 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100966
967 argc--;
968 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200969
970 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000971 len = hexport_r(&env_htab, sep,
972 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600973 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200974 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200975 pr_err("## Error: Cannot export environment: errno = %d\n",
976 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200977 return 1;
978 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100979 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600980 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200981
982 return 0;
983 }
984
Simon Glassa7459262013-10-25 23:01:31 -0600985 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200986
987 if (chk) /* export as checksum protected block */
988 res = (char *)envp->data;
989 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -0600990 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200991
Wolfgang Denk565af272013-03-23 23:50:28 +0000992 len = hexport_r(&env_htab, '\0',
993 H_MATCH_KEY | H_MATCH_IDENT,
994 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200995 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200996 pr_err("## Error: Cannot export environment: errno = %d\n",
997 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200998 return 1;
999 }
1000
1001 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +00001002 envp->crc = crc32(0, envp->data,
1003 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001004#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4686d8f2019-08-01 09:47:08 -06001005 envp->flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001006#endif
1007 }
Simon Glass4d949a22017-08-03 12:22:10 -06001008 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001009
1010 return 0;
1011
1012sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001013 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1014 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001015 return 1;
1016}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001017#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001018
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001019#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001020/*
Quentin Schulz0312b942018-07-09 19:16:29 +02001021 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1022 * -d: delete existing environment before importing if no var is
1023 * passed; if vars are passed, if one var is in the current
1024 * environment but not in the environment at addr, delete var from
1025 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04001026 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001027 * -t: assume text format; either "size" must be given or the
1028 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +02001029 * -r: handle CRLF like LF, that means exported variables with
1030 * a content which ends with \r won't get imported. Used
1031 * to import text files created with editors which are using CRLF
1032 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001033 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1034 * -c: assume checksum protected environment format
1035 * addr: memory address to read from
1036 * size: length of input data; if missing, proper '\0'
1037 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +02001038 * if var is set and size should be missing (i.e. '\0'
1039 * termination), set size to '-'
1040 * var... List of the names of the only variables that get imported from
1041 * the environment at address 'addr'. Without arguments, the whole
1042 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001043 */
Igor Grinbergde832562011-11-07 01:13:59 +00001044static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1045 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001046{
Simon Glassa7459262013-10-25 23:01:31 -06001047 ulong addr;
1048 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001049 char sep = '\n';
1050 int chk = 0;
1051 int fmt = 0;
1052 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +02001053 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +02001054 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001055 size_t size;
1056
1057 cmd = *argv;
1058
1059 while (--argc > 0 && **++argv == '-') {
1060 char *arg = *argv;
1061 while (*++arg) {
1062 switch (*arg) {
1063 case 'b': /* raw binary format */
1064 if (fmt++)
1065 goto sep_err;
1066 sep = '\0';
1067 break;
1068 case 'c': /* external checksum format */
1069 if (fmt++)
1070 goto sep_err;
1071 sep = '\0';
1072 chk = 1;
1073 break;
1074 case 't': /* text format */
1075 if (fmt++)
1076 goto sep_err;
1077 sep = '\n';
1078 break;
Alexander Holler3e12be72014-07-14 17:49:55 +02001079 case 'r': /* handle CRLF like LF */
1080 crlf_is_lf = 1;
1081 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001082 case 'd':
1083 del = 1;
1084 break;
1085 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001086 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001087 }
1088 }
1089 }
1090
Macpaul Linff2a9f32011-04-26 16:16:45 +00001091 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001092 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001093
1094 if (!fmt)
1095 printf("## Warning: defaulting to text format\n");
1096
Alexander Holler3e12be72014-07-14 17:49:55 +02001097 if (sep != '\n' && crlf_is_lf )
1098 crlf_is_lf = 0;
1099
Simon Glassa7459262013-10-25 23:01:31 -06001100 addr = simple_strtoul(argv[0], NULL, 16);
1101 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001102
Quentin Schulz0312b942018-07-09 19:16:29 +02001103 if (argc >= 2 && strcmp(argv[1], "-")) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001104 size = simple_strtoul(argv[1], NULL, 16);
Quentin Schulz0312b942018-07-09 19:16:29 +02001105 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -05001106 puts("## Error: external checksum format must pass size\n");
1107 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001108 } else {
Simon Glassa7459262013-10-25 23:01:31 -06001109 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001110
1111 size = 0;
1112
1113 while (size < MAX_ENV_SIZE) {
1114 if ((*s == sep) && (*(s+1) == '\0'))
1115 break;
1116 ++s;
1117 ++size;
1118 }
1119 if (size == MAX_ENV_SIZE) {
1120 printf("## Warning: Input data exceeds %d bytes"
1121 " - truncated\n", MAX_ENV_SIZE);
1122 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001123 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001124 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001125 }
1126
Quentin Schulz0312b942018-07-09 19:16:29 +02001127 if (argc > 2)
1128 wl = 1;
1129
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001130 if (chk) {
1131 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -06001132 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001133
1134 size -= offsetof(env_t, data);
1135 memcpy(&crc, &ep->crc, sizeof(crc));
1136
1137 if (crc32(0, ep->data, size) != crc) {
1138 puts("## Error: bad CRC, import failed\n");
1139 return 1;
1140 }
Simon Glassa7459262013-10-25 23:01:31 -06001141 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001142 }
1143
Quentin Schulz0312b942018-07-09 19:16:29 +02001144 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1145 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001146 pr_err("## Error: Environment import failed: errno = %d\n",
1147 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001148 return 1;
1149 }
1150 gd->flags |= GD_FLG_ENV_READY;
1151
1152 return 0;
1153
1154sep_err:
1155 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1156 cmd);
1157 return 1;
1158}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001159#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001160
Leo Ruan7450b872019-05-24 17:20:19 +02001161#if defined(CONFIG_CMD_NVEDIT_INFO)
1162/*
1163 * print_env_info - print environment information
1164 */
1165static int print_env_info(void)
1166{
1167 const char *value;
1168
1169 /* print environment validity value */
1170 switch (gd->env_valid) {
1171 case ENV_INVALID:
1172 value = "invalid";
1173 break;
1174 case ENV_VALID:
1175 value = "valid";
1176 break;
1177 case ENV_REDUND:
1178 value = "redundant";
1179 break;
1180 default:
1181 value = "unknown";
1182 break;
1183 }
1184 printf("env_valid = %s\n", value);
1185
1186 /* print environment ready flag */
1187 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1188 printf("env_ready = %s\n", value);
1189
1190 /* print environment using default flag */
1191 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1192 printf("env_use_default = %s\n", value);
1193
1194 return CMD_RET_SUCCESS;
1195}
1196
1197#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1198#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1199
1200/*
1201 * env info - display environment information
1202 * env info [-d] - evaluate whether default environment is used
1203 * env info [-p] - evaluate whether environment can be persisted
1204 */
1205static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1206 int argc, char * const argv[])
1207{
1208 int eval_flags = 0;
1209 int eval_results = 0;
1210
1211 /* display environment information */
1212 if (argc <= 1)
1213 return print_env_info();
1214
1215 /* process options */
1216 while (--argc > 0 && **++argv == '-') {
1217 char *arg = *argv;
1218
1219 while (*++arg) {
1220 switch (*arg) {
1221 case 'd':
1222 eval_flags |= ENV_INFO_IS_DEFAULT;
1223 break;
1224 case 'p':
1225 eval_flags |= ENV_INFO_IS_PERSISTED;
1226 break;
1227 default:
1228 return CMD_RET_USAGE;
1229 }
1230 }
1231 }
1232
1233 /* evaluate whether default environment is used */
1234 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1235 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1236 printf("Default environment is used\n");
1237 eval_results |= ENV_INFO_IS_DEFAULT;
1238 } else {
1239 printf("Environment was loaded from persistent storage\n");
1240 }
1241 }
1242
1243 /* evaluate whether environment can be persisted */
1244 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1245#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1246 printf("Environment can be persisted\n");
1247 eval_results |= ENV_INFO_IS_PERSISTED;
1248#else
1249 printf("Environment cannot be persisted\n");
1250#endif
1251 }
1252
1253 /* The result of evaluations is combined with AND */
1254 if (eval_flags != eval_results)
1255 return CMD_RET_FAILURE;
1256
1257 return CMD_RET_SUCCESS;
1258}
1259#endif
1260
Andrew Ruder94463402013-10-22 19:07:34 -05001261#if defined(CONFIG_CMD_ENV_EXISTS)
1262static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1263 char * const argv[])
1264{
Simon Glass1a236862019-08-02 09:44:18 -06001265 struct env_entry e, *ep;
Andrew Ruder94463402013-10-22 19:07:34 -05001266
1267 if (argc < 2)
1268 return CMD_RET_USAGE;
1269
1270 e.key = argv[1];
1271 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -06001272 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Andrew Ruder94463402013-10-22 19:07:34 -05001273
1274 return (ep == NULL) ? 1 : 0;
1275}
1276#endif
1277
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001278/*
1279 * New command line interface: "env" command with subcommands
1280 */
1281static cmd_tbl_t cmd_env_sub[] = {
1282#if defined(CONFIG_CMD_ASKENV)
1283 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1284#endif
1285 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001286 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001287#if defined(CONFIG_CMD_EDITENV)
1288 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1289#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001290#if defined(CONFIG_CMD_ENV_CALLBACK)
1291 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1292#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001293#if defined(CONFIG_CMD_ENV_FLAGS)
1294 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1295#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001296#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001297 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001298#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001299#if defined(CONFIG_CMD_GREPENV)
1300 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1301#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001302#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001303 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001304#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001305#if defined(CONFIG_CMD_NVEDIT_INFO)
1306 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1307#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001308 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1309#if defined(CONFIG_CMD_RUN)
1310 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1311#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001312#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001313 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001314#if defined(CONFIG_CMD_ERASEENV)
1315 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1316#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001317#endif
1318 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001319#if defined(CONFIG_CMD_ENV_EXISTS)
1320 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1321#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001322};
1323
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001324#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001325void env_reloc(void)
1326{
1327 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1328}
1329#endif
1330
Macpaul Linff2a9f32011-04-26 16:16:45 +00001331static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001332{
1333 cmd_tbl_t *cp;
1334
Thomas Weber4d282652010-11-24 13:07:52 +01001335 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001336 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001337
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001338 /* drop initial "env" arg */
1339 argc--;
1340 argv++;
1341
1342 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1343
1344 if (cp)
1345 return cp->cmd(cmdtp, flag, argc, argv);
1346
Simon Glassa06dfc72011-12-10 08:44:01 +00001347 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001348}
1349
Kim Phillipsdc00a682012-10-29 13:34:31 +00001350#ifdef CONFIG_SYS_LONGHELP
1351static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001352#if defined(CONFIG_CMD_ASKENV)
1353 "ask name [message] [size] - ask for environment variable\nenv "
1354#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001355#if defined(CONFIG_CMD_ENV_CALLBACK)
1356 "callbacks - print callbacks and their associated variables\nenv "
1357#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001358 "default [-f] -a - [forcibly] reset default environment\n"
1359 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001360 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001361#if defined(CONFIG_CMD_EDITENV)
1362 "env edit name - edit environment variable\n"
1363#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001364#if defined(CONFIG_CMD_ENV_EXISTS)
1365 "env exists name - tests for existence of variable\n"
1366#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001367#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001368 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001369#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001370#if defined(CONFIG_CMD_ENV_FLAGS)
1371 "env flags - print variables that have non-default flags\n"
1372#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001373#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001374#ifdef CONFIG_REGEX
1375 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1376#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001377 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001378#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001379#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001380#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001381 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001382#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001383#if defined(CONFIG_CMD_NVEDIT_INFO)
1384 "env info - display environment information\n"
1385 "env info [-d] - whether default environment is used\n"
1386 "env info [-p] - whether environment can be persisted\n"
1387#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001388 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001389#if defined(CONFIG_CMD_NVEDIT_EFI)
1390 "env print -e [name ...] - print UEFI environment\n"
1391#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001392#if defined(CONFIG_CMD_RUN)
1393 "env run var [...] - run commands in an environment variable\n"
1394#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001395#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001396 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001397#if defined(CONFIG_CMD_ERASEENV)
1398 "env erase - erase environment\n"
1399#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001400#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001401#if defined(CONFIG_CMD_NVEDIT_EFI)
1402 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1403#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001404 "env set [-f] name [arg ...]\n";
1405#endif
1406
1407U_BOOT_CMD(
1408 env, CONFIG_SYS_MAXARGS, 1, do_env,
1409 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001410);
1411
1412/*
1413 * Old command line interface, kept for compatibility
1414 */
wdenk57b2d802003-06-27 21:31:46 +00001415
Peter Tyser0deafa22009-10-25 15:12:56 -05001416#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001417U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001418 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001419 "edit environment variable",
1420 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001421 " - edit environment variable 'name'",
1422 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001423);
1424#endif
1425
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001426U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001427 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001428 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001429 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001430#if defined(CONFIG_CMD_NVEDIT_EFI)
1431 "printenv -e [name ...]\n"
1432 " - print UEFI variable 'name' or all the variables\n"
1433#endif
wdenk57b2d802003-06-27 21:31:46 +00001434 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001435 " - print value of environment variable 'name'",
1436 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001437);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001438
1439#ifdef CONFIG_CMD_GREPENV
1440U_BOOT_CMD_COMPLETE(
1441 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1442 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001443#ifdef CONFIG_REGEX
1444 "[-e] [-n | -v | -b] string ...\n"
1445#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001446 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001447#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001448 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001449#ifdef CONFIG_REGEX
1450 " \"-e\": enable regular expressions;\n"
1451#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001452 " \"-n\": search variable names; \"-v\": search values;\n"
1453 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001454 var_complete
1455);
1456#endif
wdenk57b2d802003-06-27 21:31:46 +00001457
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001458U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001459 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001460 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001461#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro26105762019-06-04 15:52:11 +09001462 "-e [-nv] name [value ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001463 " - set UEFI variable 'name' to 'value' ...'\n"
AKASHI Takahiro26105762019-06-04 15:52:11 +09001464 " 'nv' option makes the variable non-volatile\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001465 " - delete UEFI variable 'name' if 'value' not specified\n"
1466#endif
1467 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001468 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1469 "setenv [-f] name\n"
1470 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001471 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001472);
1473
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001474#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001475
wdenkf287a242003-07-01 21:06:45 +00001476U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001477 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001478 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001479 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001480 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001481);
Jon Loeligerd704d912007-07-10 11:02:44 -05001482#endif
wdenk57b2d802003-06-27 21:31:46 +00001483
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001484#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001485U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001486 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001487 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001488 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001489 " - run the commands in the environment variable(s) 'var'",
1490 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001491);
Jon Loeligerd704d912007-07-10 11:02:44 -05001492#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001493#endif /* CONFIG_SPL_BUILD */