blob: 46b1e60f0ad92372409fe738902fd2c18ceb3f9a [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>
wdenka68d3ed2002-10-11 08:38:32 +000030#include <environment.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020031#include <search.h>
32#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050033#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050034#include <mapmem.h>
wdenk2a3cb022002-11-05 21:01:48 +000035#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000036#include <linux/stddef.h>
37#include <asm/byteorder.h>
Simon Glassa7459262013-10-25 23:01:31 -060038#include <asm/io.h>
wdenka68d3ed2002-10-11 08:38:32 +000039
Wolfgang Denk6405a152006-03-31 18:32:53 +020040DECLARE_GLOBAL_DATA_PTR;
41
Patrick Delaunaybd5f5812019-02-18 10:58:16 +010042#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
43 defined(CONFIG_ENV_IS_IN_FLASH) || \
44 defined(CONFIG_ENV_IS_IN_MMC) || \
45 defined(CONFIG_ENV_IS_IN_FAT) || \
46 defined(CONFIG_ENV_IS_IN_EXT4) || \
47 defined(CONFIG_ENV_IS_IN_NAND) || \
48 defined(CONFIG_ENV_IS_IN_NVRAM) || \
49 defined(CONFIG_ENV_IS_IN_ONENAND) || \
50 defined(CONFIG_ENV_IS_IN_SATA) || \
51 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
52 defined(CONFIG_ENV_IS_IN_REMOTE) || \
53 defined(CONFIG_ENV_IS_IN_UBI)
54
55#define ENV_IS_IN_DEVICE
56
57#endif
58
59#if !defined(ENV_IS_IN_DEVICE) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000060 !defined(CONFIG_ENV_IS_NOWHERE)
Tuomas Tynkkynenbfef2972017-10-10 21:59:40 +030061# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
Lothar Waßmannbdad0fa2017-06-08 14:16:14 +020062NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000063#endif
64
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020065/*
66 * Maximum expected input data size for import command
67 */
68#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000069
wdenka68d3ed2002-10-11 08:38:32 +000070/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071 * This variable is incremented on each do_env_set(), so it can
Heiko Schocher3b195ff2009-04-28 08:36:11 +020072 * be used via get_env_id() as an indication, if the environment
73 * has changed or not. So it is possible to reread an environment
74 * variable only if the environment was changed ... done so for
75 * example in NetInitLoop()
76 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010077static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000078
Macpaul Linff2a9f32011-04-26 16:16:45 +000079int get_env_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010080{
81 return env_id;
82}
wdenka68d3ed2002-10-11 08:38:32 +000083
Ilya Yanokf7a2c552012-09-18 00:22:50 +000084#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040085/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086 * Command interface: print one or all environment variables
87 *
88 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040089 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060090static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000091{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020092 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000093 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000094
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020095 if (name) { /* print a single name */
96 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000097
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 e.key = name;
99 e.data = NULL;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600100 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101 if (ep == NULL)
102 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000103 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200104 return len;
105 }
wdenka68d3ed2002-10-11 08:38:32 +0000106
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200107 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600108 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000109
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 if (len > 0) {
111 puts(res);
112 free(res);
113 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000114 }
115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000117 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200118 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400119}
wdenka68d3ed2002-10-11 08:38:32 +0000120
Kim Phillipsdc00a682012-10-29 13:34:31 +0000121static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
122 char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400123{
124 int i;
125 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600126 int env_flag = H_HIDE_DOT;
127
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900128#if defined(CONFIG_CMD_NVEDIT_EFI)
129 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
130 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
131#endif
132
Joe Hershberger79a905e2012-12-11 22:16:23 -0600133 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
134 argc--;
135 argv++;
136 env_flag &= ~H_HIDE_DOT;
137 }
wdenka68d3ed2002-10-11 08:38:32 +0000138
Mike Frysinger87944922009-05-24 02:26:19 -0400139 if (argc == 1) {
140 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600141 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200142 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400143 return 1;
144 printf("\nEnvironment size: %d/%ld bytes\n",
145 rcode, (ulong)ENV_SIZE);
146 return 0;
147 }
wdenka68d3ed2002-10-11 08:38:32 +0000148
Mike Frysinger87944922009-05-24 02:26:19 -0400149 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600150 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400151 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600152 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200153 if (!rc) {
154 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400155 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000156 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000157 }
158
159 return rcode;
160}
161
162#ifdef CONFIG_CMD_GREPENV
Igor Grinbergde832562011-11-07 01:13:59 +0000163static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
164 int argc, char * const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000165{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000166 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000167 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000168
169 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000170 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000171
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000172 grep_how = H_MATCH_SUBSTR; /* default: substring search */
173 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000174
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200175 while (--argc > 0 && **++argv == '-') {
176 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000177 while (*++arg) {
178 switch (*arg) {
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000179#ifdef CONFIG_REGEX
180 case 'e': /* use regex matching */
181 grep_how = H_MATCH_REGEX;
182 break;
183#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000184 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000185 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000186 break;
187 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000188 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000189 break;
190 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000191 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000192 break;
193 case '-':
194 goto DONE;
195 default:
196 return CMD_RET_USAGE;
197 }
198 }
199 }
200
201DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000202 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000203 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000204 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000205
Wolfgang Denke27fc122013-03-23 23:50:29 +0000206 if (len > 0) {
207 puts(res);
208 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000209 }
Mike Frysinger87944922009-05-24 02:26:19 -0400210
Wolfgang Denke27fc122013-03-23 23:50:29 +0000211 if (len < 2)
212 return 1;
213
214 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000215}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000216#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000217#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000218
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200219/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000220 * Set a new environment variable,
221 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600222 */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500223static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
Gerlando Falautodc88d592012-08-24 00:11:37 +0000224{
225 int i, len;
226 char *name, *value, *s;
227 ENTRY e, *ep;
228
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600229 debug("Initial value for argc=%d\n", argc);
AKASHI Takahiro117e68a2019-02-25 15:54:36 +0900230
231#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
232 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
233 return do_env_set_efi(NULL, flag, --argc, ++argv);
234#endif
235
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600236 while (argc > 1 && **(argv + 1) == '-') {
237 char *arg = *++argv;
238
239 --argc;
240 while (*++arg) {
241 switch (*arg) {
242 case 'f': /* force */
243 env_flag |= H_FORCE;
244 break;
245 default:
246 return CMD_RET_USAGE;
247 }
248 }
249 }
250 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000251 name = argv[1];
Gerlando Falautodc88d592012-08-24 00:11:37 +0000252
253 if (strchr(name, '=')) {
254 printf("## Error: illegal character '='"
255 "in variable name \"%s\"\n", name);
256 return 1;
257 }
258
259 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000260
wdenka68d3ed2002-10-11 08:38:32 +0000261 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000262 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600263 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200264 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000265 }
266
267 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200268 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000269 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000270 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000271 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000272
273 value = malloc(len);
274 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200275 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000276 return 1;
277 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000278 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200279 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000280
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200281 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000282 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000283 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000284 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200285 if (s != value)
286 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000287
Igor Grinbergde832562011-11-07 01:13:59 +0000288 e.key = name;
289 e.data = value;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600290 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200291 free(value);
292 if (!ep) {
293 printf("## Error inserting \"%s\" variable, errno=%d\n",
294 name, errno);
295 return 1;
296 }
wdenka68d3ed2002-10-11 08:38:32 +0000297
wdenka68d3ed2002-10-11 08:38:32 +0000298 return 0;
299}
300
Simon Glass6a38e412017-08-03 12:22:09 -0600301int env_set(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000302{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200303 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
304
Joe Hershberger3902c602013-04-08 10:32:50 +0000305 /* before import into hashtable */
306 if (!(gd->flags & GD_FLG_ENV_READY))
307 return 1;
308
Igor Grinbergde832562011-11-07 01:13:59 +0000309 if (varvalue == NULL || varvalue[0] == '\0')
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500310 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200311 else
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500312 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
wdenka68d3ed2002-10-11 08:38:32 +0000313}
314
Simon Glassb8e926e2011-10-24 17:59:59 +0000315/**
316 * Set an environment variable to an integer value
317 *
Simon Glassdf372d62013-05-07 06:11:45 +0000318 * @param varname Environment variable to set
Simon Glassb8e926e2011-10-24 17:59:59 +0000319 * @param value Value to set it to
320 * @return 0 if ok, 1 on error
321 */
Simon Glass4d949a22017-08-03 12:22:10 -0600322int env_set_ulong(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000323{
324 /* TODO: this should be unsigned */
325 char *str = simple_itoa(value);
326
Simon Glass6a38e412017-08-03 12:22:09 -0600327 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000328}
329
330/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000331 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000332 *
Simon Glassdf372d62013-05-07 06:11:45 +0000333 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000334 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000335 * @return 0 if ok, 1 on error
336 */
Simon Glass4d949a22017-08-03 12:22:10 -0600337int env_set_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000338{
339 char str[17];
340
Simon Glass044dc9e2013-02-24 17:33:21 +0000341 sprintf(str, "%lx", value);
Simon Glass6a38e412017-08-03 12:22:09 -0600342 return env_set(varname, str);
Simon Glassb8e926e2011-10-24 17:59:59 +0000343}
344
Simon Glass22c34c22017-08-03 12:22:13 -0600345ulong env_get_hex(const char *varname, ulong default_val)
Simon Glassabbd5122013-04-20 08:42:43 +0000346{
347 const char *s;
348 ulong value;
349 char *endp;
350
Simon Glass64b723f2017-08-03 12:22:12 -0600351 s = env_get(varname);
Simon Glassabbd5122013-04-20 08:42:43 +0000352 if (s)
353 value = simple_strtoul(s, &endp, 16);
354 if (!s || endp == s)
355 return default_val;
356
357 return value;
358}
359
Alex Kiernan9c215492018-04-01 09:22:38 +0000360void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
361{
362 char *end;
363 int i;
364
365 for (i = 0; i < 6; ++i) {
366 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
367 if (addr)
368 addr = (*end) ? end + 1 : end;
369 }
370}
371
372int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
373{
374 eth_parse_enetaddr(env_get(name), enetaddr);
375 return is_valid_ethaddr(enetaddr);
376}
377
378int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
379{
380 char buf[ARP_HLEN_ASCII + 1];
381
382 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
383 return -EEXIST;
384
385 sprintf(buf, "%pM", enetaddr);
386
387 return env_set(name, buf);
388}
389
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000390#ifndef CONFIG_SPL_BUILD
Kim Phillipsdc00a682012-10-29 13:34:31 +0000391static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000392{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200393 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000394 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000395
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500396 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000397}
398
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200399/*
wdenka68d3ed2002-10-11 08:38:32 +0000400 * Prompt for environment variable
401 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500402#if defined(CONFIG_CMD_ASKENV)
Macpaul Linff2a9f32011-04-26 16:16:45 +0000403int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000404{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200405 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000406 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000407 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000408 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000409
410 local_args[0] = argv[0];
411 local_args[1] = argv[1];
412 local_args[2] = NULL;
413 local_args[3] = NULL;
414
Wolfgang Denk586895d2013-02-20 04:53:16 +0000415 /*
416 * Check the syntax:
417 *
418 * env_ask envname [message1 ...] [size]
419 */
420 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000421 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000422
Wolfgang Denk586895d2013-02-20 04:53:16 +0000423 /*
424 * We test the last argument if it can be converted
425 * into a decimal number. If yes, we assume it's
426 * the size. Otherwise we echo it as part of the
427 * message.
428 */
429 i = simple_strtoul(argv[argc - 1], &endptr, 10);
430 if (*endptr != '\0') { /* no size */
431 size = CONFIG_SYS_CBSIZE - 1;
432 } else { /* size given */
433 size = i;
434 --argc;
435 }
wdenka68d3ed2002-10-11 08:38:32 +0000436
Wolfgang Denk586895d2013-02-20 04:53:16 +0000437 if (argc <= 2) {
438 sprintf(message, "Please enter '%s': ", argv[1]);
439 } else {
440 /* env_ask envname message1 ... messagen [size] */
Tom Rinia29829e2017-10-07 11:27:59 -0400441 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000442 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000443 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000444
Tom Rini4beb2352017-09-26 19:37:11 -0400445 strncpy(message + pos, argv[i], sizeof(message) - pos);
wdenka68d3ed2002-10-11 08:38:32 +0000446 pos += strlen(argv[i]);
447 }
Tom Rini4beb2352017-09-26 19:37:11 -0400448 if (pos < sizeof(message) - 1) {
449 message[pos++] = ' ';
450 message[pos] = '\0';
451 } else
452 message[CONFIG_SYS_CBSIZE - 1] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000453 }
454
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200455 if (size >= CONFIG_SYS_CBSIZE)
456 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000457
458 if (size <= 0)
459 return 1;
460
461 /* prompt for input */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600462 len = cli_readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000463
464 if (size < len)
465 console_buffer[size] = '\0';
466
467 len = 2;
468 if (console_buffer[0] != '\0') {
469 local_args[2] = console_buffer;
470 len = 3;
471 }
472
473 /* Continue calling setenv code */
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500474 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
wdenka68d3ed2002-10-11 08:38:32 +0000475}
Jon Loeligerd704d912007-07-10 11:02:44 -0500476#endif
wdenka68d3ed2002-10-11 08:38:32 +0000477
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600478#if defined(CONFIG_CMD_ENV_CALLBACK)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500479static int print_static_binding(const char *var_name, const char *callback_name,
480 void *priv)
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600481{
482 printf("\t%-20s %-20s\n", var_name, callback_name);
483
484 return 0;
485}
486
487static int print_active_callback(ENTRY *entry)
488{
489 struct env_clbk_tbl *clbkp;
490 int i;
491 int num_callbacks;
492
493 if (entry->callback == NULL)
494 return 0;
495
496 /* look up the callback in the linker-list */
497 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
498 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
499 i < num_callbacks;
500 i++, clbkp++) {
501#if defined(CONFIG_NEEDS_MANUAL_RELOC)
502 if (entry->callback == clbkp->callback + gd->reloc_off)
503#else
504 if (entry->callback == clbkp->callback)
505#endif
506 break;
507 }
508
509 if (i == num_callbacks)
510 /* this should probably never happen, but just in case... */
511 printf("\t%-20s %p\n", entry->key, entry->callback);
512 else
513 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
514
515 return 0;
516}
517
518/*
519 * Print the callbacks available and what they are bound to
520 */
521int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
522{
523 struct env_clbk_tbl *clbkp;
524 int i;
525 int num_callbacks;
526
527 /* Print the available callbacks */
528 puts("Available callbacks:\n");
529 puts("\tCallback Name\n");
530 puts("\t-------------\n");
531 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
532 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
533 i < num_callbacks;
534 i++, clbkp++)
535 printf("\t%s\n", clbkp->name);
536 puts("\n");
537
538 /* Print the static bindings that may exist */
539 puts("Static callback bindings:\n");
540 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
541 printf("\t%-20s %-20s\n", "-------------", "-------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500542 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600543 puts("\n");
544
545 /* walk through each variable and print the callback if it has one */
546 puts("Active callback bindings:\n");
547 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
548 printf("\t%-20s %-20s\n", "-------------", "-------------");
549 hwalk_r(&env_htab, print_active_callback);
550 return 0;
551}
552#endif
553
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600554#if defined(CONFIG_CMD_ENV_FLAGS)
Joe Hershbergerd741f562015-05-20 14:27:19 -0500555static int print_static_flags(const char *var_name, const char *flags,
556 void *priv)
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600557{
558 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600559 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600560
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600561 printf("\t%-20s %-20s %-20s\n", var_name,
562 env_flags_get_vartype_name(type),
563 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600564
565 return 0;
566}
567
568static int print_active_flags(ENTRY *entry)
569{
570 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600571 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600572
573 if (entry->flags == 0)
574 return 0;
575
576 type = (enum env_flags_vartype)
577 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600578 access = env_flags_parse_varaccess_from_binflags(entry->flags);
579 printf("\t%-20s %-20s %-20s\n", entry->key,
580 env_flags_get_vartype_name(type),
581 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600582
583 return 0;
584}
585
586/*
587 * Print the flags available and what variables have flags
588 */
589int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
590{
591 /* Print the available variable types */
592 printf("Available variable type flags (position %d):\n",
593 ENV_FLAGS_VARTYPE_LOC);
594 puts("\tFlag\tVariable Type Name\n");
595 puts("\t----\t------------------\n");
596 env_flags_print_vartypes();
597 puts("\n");
598
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600599 /* Print the available variable access types */
600 printf("Available variable access flags (position %d):\n",
601 ENV_FLAGS_VARACCESS_LOC);
602 puts("\tFlag\tVariable Access Name\n");
603 puts("\t----\t--------------------\n");
604 env_flags_print_varaccess();
605 puts("\n");
606
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600607 /* Print the static flags that may exist */
608 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600609 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
610 "Variable Access");
611 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
612 "---------------");
Joe Hershbergerd741f562015-05-20 14:27:19 -0500613 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600614 puts("\n");
615
616 /* walk through each variable and print the flags if non-default */
617 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600618 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
619 "Variable Access");
620 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
621 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600622 hwalk_r(&env_htab, print_active_flags);
623 return 0;
624}
625#endif
626
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200627/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500628 * Interactively edit an environment variable
629 */
630#if defined(CONFIG_CMD_EDITENV)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000631static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
632 char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500633{
634 char buffer[CONFIG_SYS_CBSIZE];
635 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500636
Wolfgang Denk3b683112010-07-17 01:06:04 +0200637 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000638 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500639
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500640 /* before import into hashtable */
641 if (!(gd->flags & GD_FLG_ENV_READY))
642 return 1;
643
Peter Tyser0deafa22009-10-25 15:12:56 -0500644 /* Set read buffer to initial value or empty sting */
Simon Glass64b723f2017-08-03 12:22:12 -0600645 init_val = env_get(argv[1]);
Peter Tyser0deafa22009-10-25 15:12:56 -0500646 if (init_val)
Peng Fan46c99d42015-12-23 12:08:09 +0800647 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500648 else
649 buffer[0] = '\0';
650
Simon Glassbe6aafc2014-04-10 20:01:27 -0600651 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000652 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500653
Joe Hershbergerdfc382a2015-05-20 14:27:21 -0500654 if (buffer[0] == '\0') {
655 const char * const _argv[3] = { "setenv", argv[1], NULL };
656
657 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
658 } else {
659 const char * const _argv[4] = { "setenv", argv[1], buffer,
660 NULL };
661
662 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
663 }
Peter Tyser0deafa22009-10-25 15:12:56 -0500664}
665#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000666#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500667
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200668/*
wdenka68d3ed2002-10-11 08:38:32 +0000669 * Look up variable from environment,
670 * return address of storage for that variable,
671 * or NULL if not found
672 */
Simon Glass64b723f2017-08-03 12:22:12 -0600673char *env_get(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000674{
Igor Grinbergde832562011-11-07 01:13:59 +0000675 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200676 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000677
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200678 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000679
Igor Grinbergde832562011-11-07 01:13:59 +0000680 e.key = name;
681 e.data = NULL;
Joe Hershbergera46f7702012-12-11 22:16:19 -0600682 hsearch_r(e, FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200683
Macpaul Linff2a9f32011-04-26 16:16:45 +0000684 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000685 }
686
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200687 /* restricted capabilities before import */
Simon Glass64b723f2017-08-03 12:22:12 -0600688 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200689 return (char *)(gd->env_buf);
690
691 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000692}
693
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200694/*
695 * Look up variable from environment for restricted C runtime env.
696 */
Simon Glass64b723f2017-08-03 12:22:12 -0600697int env_get_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000698{
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100699 int i, nxt, c;
wdenka68d3ed2002-10-11 08:38:32 +0000700
Igor Grinbergde832562011-11-07 01:13:59 +0000701 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000702 int val, n;
703
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100704 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
705 if (c < 0)
706 return c;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000707 if (nxt >= CONFIG_ENV_SIZE)
708 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000709 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000710
711 val = envmatch((uchar *)name, i);
712 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000713 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200714
wdenka68d3ed2002-10-11 08:38:32 +0000715 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000716 for (n = 0; n < len; ++n, ++buf) {
Simon Goldschmidt5a90fb72018-01-31 07:56:48 +0100717 c = env_get_char(val++);
718 if (c < 0)
719 return c;
720 *buf = c;
Igor Grinbergde832562011-11-07 01:13:59 +0000721 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200722 return n;
723 }
724
725 if (n)
726 *--buf = '\0';
727
Heinrich Schuchardtcf80bd72019-01-06 11:52:06 +0100728 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
729 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200730
731 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000732 }
Igor Grinbergde832562011-11-07 01:13:59 +0000733
Macpaul Linff2a9f32011-04-26 16:16:45 +0000734 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000735}
736
Simon Glassbf690f82011-10-14 13:25:18 +0000737/**
738 * Decode the integer value of an environment variable and return it.
739 *
Shyam Sainiefe757c2018-06-07 19:47:19 +0530740 * @param name Name of environment variable
Simon Glassbf690f82011-10-14 13:25:18 +0000741 * @param base Number base to use (normally 10, or 16 for hex)
742 * @param default_val Default value to return if the variable is not
743 * found
744 * @return the decoded value, or default_val if not found
745 */
Simon Glass22c34c22017-08-03 12:22:13 -0600746ulong env_get_ulong(const char *name, int base, ulong default_val)
Simon Glassbf690f82011-10-14 13:25:18 +0000747{
748 /*
Simon Glass64b723f2017-08-03 12:22:12 -0600749 * We can use env_get() here, even before relocation, since the
Simon Glassbf690f82011-10-14 13:25:18 +0000750 * environment variable value is an integer and thus short.
751 */
Simon Glass64b723f2017-08-03 12:22:12 -0600752 const char *str = env_get(name);
Simon Glassbf690f82011-10-14 13:25:18 +0000753
754 return str ? simple_strtoul(str, NULL, base) : default_val;
755}
756
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000757#ifndef CONFIG_SPL_BUILD
Patrick Delaunaybd5f5812019-02-18 10:58:16 +0100758#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000759static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
760 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000761{
Simon Glassd49b8892017-08-03 12:22:08 -0600762 return env_save() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000763}
wdenk57b2d802003-06-27 21:31:46 +0000764
Mike Frysinger61583932008-12-30 02:59:25 -0500765U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200766 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600767 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200768 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500769);
Frank Wunderlich33afa932019-06-29 11:36:19 +0200770
771#if defined(CONFIG_CMD_ERASEENV)
772static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
773 char * const argv[])
774{
775 return env_erase() ? 1 : 0;
776}
777
778U_BOOT_CMD(
779 eraseenv, 1, 0, do_env_erase,
780 "erase environment variables from persistent storage",
781 ""
782);
783#endif
wdenka68d3ed2002-10-11 08:38:32 +0000784#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000785#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000786
787
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200788/*
wdenka68d3ed2002-10-11 08:38:32 +0000789 * Match a name / name=value pair
790 *
791 * s1 is either a simple 'name', or a 'name=value' pair.
792 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergde832562011-11-07 01:13:59 +0000793 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000794 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000795int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000796{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000797 if (s1 == NULL)
798 return -1;
799
wdenka68d3ed2002-10-11 08:38:32 +0000800 while (*s1 == env_get_char(i2++))
801 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000802 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000803
wdenka68d3ed2002-10-11 08:38:32 +0000804 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000805 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000806
Macpaul Linff2a9f32011-04-26 16:16:45 +0000807 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000808}
wdenk57b2d802003-06-27 21:31:46 +0000809
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000810#ifndef CONFIG_SPL_BUILD
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300811static int do_env_default(cmd_tbl_t *cmdtp, int flag,
Igor Grinbergde832562011-11-07 01:13:59 +0000812 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200813{
Yaniv Levinsky16e34242018-06-24 19:16:56 +0300814 int all = 0, env_flag = H_INTERACTIVE;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000815
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000816 debug("Initial value for argc=%d\n", argc);
817 while (--argc > 0 && **++argv == '-') {
818 char *arg = *argv;
819
820 while (*++arg) {
821 switch (*arg) {
822 case 'a': /* default all */
823 all = 1;
824 break;
825 case 'f': /* force */
Yaniv Levinsky72e10ca2018-06-24 19:16:54 +0300826 env_flag |= H_FORCE;
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000827 break;
828 default:
829 return cmd_usage(cmdtp);
830 }
831 }
832 }
833 debug("Final value for argc=%d\n", argc);
834 if (all && (argc == 0)) {
835 /* Reset the whole environment */
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300836 set_default_env("## Resetting to default environment\n",
837 env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000838 return 0;
839 }
840 if (!all && (argc > 0)) {
841 /* Reset individual variables */
Yaniv Levinsky23840d12018-06-24 19:16:55 +0300842 set_default_vars(argc, argv, env_flag);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000843 return 0;
844 }
845
846 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200847}
848
Igor Grinbergde832562011-11-07 01:13:59 +0000849static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
850 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200851{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600852 int env_flag = H_INTERACTIVE;
853 int ret = 0;
854
855 debug("Initial value for argc=%d\n", argc);
856 while (argc > 1 && **(argv + 1) == '-') {
857 char *arg = *++argv;
858
859 --argc;
860 while (*++arg) {
861 switch (*arg) {
862 case 'f': /* force */
863 env_flag |= H_FORCE;
864 break;
865 default:
866 return CMD_RET_USAGE;
867 }
868 }
869 }
870 debug("Final value for argc=%d\n", argc);
871
872 env_id++;
873
874 while (--argc > 0) {
875 char *name = *++argv;
876
877 if (!hdelete_r(name, &env_htab, env_flag))
878 ret = 1;
879 }
880
881 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200882}
883
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500884#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200885/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100886 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200887 * -t: export as text format; if size is given, data will be
888 * padded with '\0' bytes; if not, one terminating '\0'
889 * will be added (which is included in the "filesize"
890 * setting so you can for exmple copy this to flash and
891 * keep the termination).
892 * -b: export as binary format (name=value pairs separated by
893 * '\0', list end marked by double "\0\0")
894 * -c: export as checksum protected environment format as
895 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100896 * -s size:
897 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200898 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100899 * var... List of variable names that get included into the
900 * export. Without arguments, the whole environment gets
901 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200902 *
903 * With "-c" and size is NOT given, then the export command will
904 * format the data as currently used for the persistent storage,
905 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400906 * prepend a valid CRC32 checksum and, in case of redundant
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200907 * environment, a "current" redundancy flag. If size is given, this
908 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
909 * checksum and redundancy flag will be inserted.
910 *
911 * With "-b" and "-t", always only the real data (including a
912 * terminating '\0' byte) will be written; here the optional size
913 * argument will be used to make sure not to overflow the user
914 * provided buffer; the command will abort if the size is not
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400915 * sufficient. Any remaining space will be '\0' padded.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200916 *
917 * On successful return, the variable "filesize" will be set.
918 * Note that filesize includes the trailing/terminating '\0' byte(s).
919 *
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400920 * Usage scenario: create a text snapshot/backup of the current settings:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200921 *
922 * => env export -t 100000
923 * => era ${backup_addr} +${filesize}
924 * => cp.b 100000 ${backup_addr} ${filesize}
925 *
926 * Re-import this snapshot, deleting all other settings:
927 *
928 * => env import -d -t ${backup_addr}
929 */
Igor Grinbergde832562011-11-07 01:13:59 +0000930static int do_env_export(cmd_tbl_t *cmdtp, int flag,
931 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200932{
933 char buf[32];
Simon Glassa7459262013-10-25 23:01:31 -0600934 ulong addr;
935 char *ptr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100936 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200937 ssize_t len;
938 env_t *envp;
939 char sep = '\n';
940 int chk = 0;
941 int fmt = 0;
942
943 cmd = *argv;
944
945 while (--argc > 0 && **++argv == '-') {
946 char *arg = *argv;
947 while (*++arg) {
948 switch (*arg) {
949 case 'b': /* raw binary format */
950 if (fmt++)
951 goto sep_err;
952 sep = '\0';
953 break;
954 case 'c': /* external checksum format */
955 if (fmt++)
956 goto sep_err;
957 sep = '\0';
958 chk = 1;
959 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100960 case 's': /* size given */
961 if (--argc <= 0)
962 return cmd_usage(cmdtp);
963 size = simple_strtoul(*++argv, NULL, 16);
964 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200965 case 't': /* text format */
966 if (fmt++)
967 goto sep_err;
968 sep = '\n';
969 break;
970 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000971 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200972 }
973 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100974NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200975 }
976
Macpaul Linff2a9f32011-04-26 16:16:45 +0000977 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000978 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000979
Simon Glassa7459262013-10-25 23:01:31 -0600980 addr = simple_strtoul(argv[0], NULL, 16);
981 ptr = map_sysmem(addr, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200982
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100983 if (size)
Simon Glassa7459262013-10-25 23:01:31 -0600984 memset(ptr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100985
986 argc--;
987 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200988
989 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000990 len = hexport_r(&env_htab, sep,
991 H_MATCH_KEY | H_MATCH_IDENT,
Simon Glassa7459262013-10-25 23:01:31 -0600992 &ptr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200993 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200994 pr_err("## Error: Cannot export environment: errno = %d\n",
995 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200996 return 1;
997 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100998 sprintf(buf, "%zX", (size_t)len);
Simon Glass6a38e412017-08-03 12:22:09 -0600999 env_set("filesize", buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001000
1001 return 0;
1002 }
1003
Simon Glassa7459262013-10-25 23:01:31 -06001004 envp = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001005
1006 if (chk) /* export as checksum protected block */
1007 res = (char *)envp->data;
1008 else /* export as raw binary data */
Simon Glassa7459262013-10-25 23:01:31 -06001009 res = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001010
Wolfgang Denk565af272013-03-23 23:50:28 +00001011 len = hexport_r(&env_htab, '\0',
1012 H_MATCH_KEY | H_MATCH_IDENT,
1013 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001014 if (len < 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001015 pr_err("## Error: Cannot export environment: errno = %d\n",
1016 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001017 return 1;
1018 }
1019
1020 if (chk) {
Neil Stainton960a5fd2018-09-12 11:02:51 +00001021 envp->crc = crc32(0, envp->data,
1022 size ? size - offsetof(env_t, data) : ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001023#ifdef CONFIG_ENV_ADDR_REDUND
1024 envp->flags = ACTIVE_FLAG;
1025#endif
1026 }
Simon Glass4d949a22017-08-03 12:22:10 -06001027 env_set_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001028
1029 return 0;
1030
1031sep_err:
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001032 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1033 cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001034 return 1;
1035}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001036#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001037
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001038#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001039/*
Quentin Schulz0312b942018-07-09 19:16:29 +02001040 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1041 * -d: delete existing environment before importing if no var is
1042 * passed; if vars are passed, if one var is in the current
1043 * environment but not in the environment at addr, delete var from
1044 * current environment;
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04001045 * otherwise overwrite / append to existing definitions
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001046 * -t: assume text format; either "size" must be given or the
1047 * text data must be '\0' terminated
Alexander Holler3e12be72014-07-14 17:49:55 +02001048 * -r: handle CRLF like LF, that means exported variables with
1049 * a content which ends with \r won't get imported. Used
1050 * to import text files created with editors which are using CRLF
1051 * for line endings. Only effective in addition to -t.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001052 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1053 * -c: assume checksum protected environment format
1054 * addr: memory address to read from
1055 * size: length of input data; if missing, proper '\0'
1056 * termination is mandatory
Quentin Schulz0312b942018-07-09 19:16:29 +02001057 * if var is set and size should be missing (i.e. '\0'
1058 * termination), set size to '-'
1059 * var... List of the names of the only variables that get imported from
1060 * the environment at address 'addr'. Without arguments, the whole
1061 * environment gets imported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001062 */
Igor Grinbergde832562011-11-07 01:13:59 +00001063static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1064 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001065{
Simon Glassa7459262013-10-25 23:01:31 -06001066 ulong addr;
1067 char *cmd, *ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001068 char sep = '\n';
1069 int chk = 0;
1070 int fmt = 0;
1071 int del = 0;
Alexander Holler3e12be72014-07-14 17:49:55 +02001072 int crlf_is_lf = 0;
Quentin Schulz0312b942018-07-09 19:16:29 +02001073 int wl = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001074 size_t size;
1075
1076 cmd = *argv;
1077
1078 while (--argc > 0 && **++argv == '-') {
1079 char *arg = *argv;
1080 while (*++arg) {
1081 switch (*arg) {
1082 case 'b': /* raw binary format */
1083 if (fmt++)
1084 goto sep_err;
1085 sep = '\0';
1086 break;
1087 case 'c': /* external checksum format */
1088 if (fmt++)
1089 goto sep_err;
1090 sep = '\0';
1091 chk = 1;
1092 break;
1093 case 't': /* text format */
1094 if (fmt++)
1095 goto sep_err;
1096 sep = '\n';
1097 break;
Alexander Holler3e12be72014-07-14 17:49:55 +02001098 case 'r': /* handle CRLF like LF */
1099 crlf_is_lf = 1;
1100 break;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001101 case 'd':
1102 del = 1;
1103 break;
1104 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001105 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001106 }
1107 }
1108 }
1109
Macpaul Linff2a9f32011-04-26 16:16:45 +00001110 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001111 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001112
1113 if (!fmt)
1114 printf("## Warning: defaulting to text format\n");
1115
Alexander Holler3e12be72014-07-14 17:49:55 +02001116 if (sep != '\n' && crlf_is_lf )
1117 crlf_is_lf = 0;
1118
Simon Glassa7459262013-10-25 23:01:31 -06001119 addr = simple_strtoul(argv[0], NULL, 16);
1120 ptr = map_sysmem(addr, 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001121
Quentin Schulz0312b942018-07-09 19:16:29 +02001122 if (argc >= 2 && strcmp(argv[1], "-")) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001123 size = simple_strtoul(argv[1], NULL, 16);
Quentin Schulz0312b942018-07-09 19:16:29 +02001124 } else if (chk) {
Tom Rini0c44c492014-03-04 15:52:35 -05001125 puts("## Error: external checksum format must pass size\n");
1126 return CMD_RET_FAILURE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001127 } else {
Simon Glassa7459262013-10-25 23:01:31 -06001128 char *s = ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001129
1130 size = 0;
1131
1132 while (size < MAX_ENV_SIZE) {
1133 if ((*s == sep) && (*(s+1) == '\0'))
1134 break;
1135 ++s;
1136 ++size;
1137 }
1138 if (size == MAX_ENV_SIZE) {
1139 printf("## Warning: Input data exceeds %d bytes"
1140 " - truncated\n", MAX_ENV_SIZE);
1141 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001142 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001143 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001144 }
1145
Quentin Schulz0312b942018-07-09 19:16:29 +02001146 if (argc > 2)
1147 wl = 1;
1148
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001149 if (chk) {
1150 uint32_t crc;
Simon Glassa7459262013-10-25 23:01:31 -06001151 env_t *ep = (env_t *)ptr;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001152
1153 size -= offsetof(env_t, data);
1154 memcpy(&crc, &ep->crc, sizeof(crc));
1155
1156 if (crc32(0, ep->data, size) != crc) {
1157 puts("## Error: bad CRC, import failed\n");
1158 return 1;
1159 }
Simon Glassa7459262013-10-25 23:01:31 -06001160 ptr = (char *)ep->data;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001161 }
1162
Quentin Schulz0312b942018-07-09 19:16:29 +02001163 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1164 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +02001165 pr_err("## Error: Environment import failed: errno = %d\n",
1166 errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001167 return 1;
1168 }
1169 gd->flags |= GD_FLG_ENV_READY;
1170
1171 return 0;
1172
1173sep_err:
1174 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1175 cmd);
1176 return 1;
1177}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001178#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001179
Leo Ruan7450b872019-05-24 17:20:19 +02001180#if defined(CONFIG_CMD_NVEDIT_INFO)
1181/*
1182 * print_env_info - print environment information
1183 */
1184static int print_env_info(void)
1185{
1186 const char *value;
1187
1188 /* print environment validity value */
1189 switch (gd->env_valid) {
1190 case ENV_INVALID:
1191 value = "invalid";
1192 break;
1193 case ENV_VALID:
1194 value = "valid";
1195 break;
1196 case ENV_REDUND:
1197 value = "redundant";
1198 break;
1199 default:
1200 value = "unknown";
1201 break;
1202 }
1203 printf("env_valid = %s\n", value);
1204
1205 /* print environment ready flag */
1206 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1207 printf("env_ready = %s\n", value);
1208
1209 /* print environment using default flag */
1210 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1211 printf("env_use_default = %s\n", value);
1212
1213 return CMD_RET_SUCCESS;
1214}
1215
1216#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1217#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1218
1219/*
1220 * env info - display environment information
1221 * env info [-d] - evaluate whether default environment is used
1222 * env info [-p] - evaluate whether environment can be persisted
1223 */
1224static int do_env_info(cmd_tbl_t *cmdtp, int flag,
1225 int argc, char * const argv[])
1226{
1227 int eval_flags = 0;
1228 int eval_results = 0;
1229
1230 /* display environment information */
1231 if (argc <= 1)
1232 return print_env_info();
1233
1234 /* process options */
1235 while (--argc > 0 && **++argv == '-') {
1236 char *arg = *argv;
1237
1238 while (*++arg) {
1239 switch (*arg) {
1240 case 'd':
1241 eval_flags |= ENV_INFO_IS_DEFAULT;
1242 break;
1243 case 'p':
1244 eval_flags |= ENV_INFO_IS_PERSISTED;
1245 break;
1246 default:
1247 return CMD_RET_USAGE;
1248 }
1249 }
1250 }
1251
1252 /* evaluate whether default environment is used */
1253 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1254 if (gd->flags & GD_FLG_ENV_DEFAULT) {
1255 printf("Default environment is used\n");
1256 eval_results |= ENV_INFO_IS_DEFAULT;
1257 } else {
1258 printf("Environment was loaded from persistent storage\n");
1259 }
1260 }
1261
1262 /* evaluate whether environment can be persisted */
1263 if (eval_flags & ENV_INFO_IS_PERSISTED) {
1264#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1265 printf("Environment can be persisted\n");
1266 eval_results |= ENV_INFO_IS_PERSISTED;
1267#else
1268 printf("Environment cannot be persisted\n");
1269#endif
1270 }
1271
1272 /* The result of evaluations is combined with AND */
1273 if (eval_flags != eval_results)
1274 return CMD_RET_FAILURE;
1275
1276 return CMD_RET_SUCCESS;
1277}
1278#endif
1279
Andrew Ruder94463402013-10-22 19:07:34 -05001280#if defined(CONFIG_CMD_ENV_EXISTS)
1281static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1282 char * const argv[])
1283{
1284 ENTRY e, *ep;
1285
1286 if (argc < 2)
1287 return CMD_RET_USAGE;
1288
1289 e.key = argv[1];
1290 e.data = NULL;
1291 hsearch_r(e, FIND, &ep, &env_htab, 0);
1292
1293 return (ep == NULL) ? 1 : 0;
1294}
1295#endif
1296
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001297/*
1298 * New command line interface: "env" command with subcommands
1299 */
1300static cmd_tbl_t cmd_env_sub[] = {
1301#if defined(CONFIG_CMD_ASKENV)
1302 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1303#endif
1304 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001305 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001306#if defined(CONFIG_CMD_EDITENV)
1307 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1308#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001309#if defined(CONFIG_CMD_ENV_CALLBACK)
1310 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1311#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001312#if defined(CONFIG_CMD_ENV_FLAGS)
1313 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1314#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001315#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001316 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001317#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001318#if defined(CONFIG_CMD_GREPENV)
1319 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1320#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001321#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001322 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001323#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001324#if defined(CONFIG_CMD_NVEDIT_INFO)
1325 U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
1326#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001327 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1328#if defined(CONFIG_CMD_RUN)
1329 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1330#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001331#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001332 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
Frank Wunderlich33afa932019-06-29 11:36:19 +02001333#if defined(CONFIG_CMD_ERASEENV)
1334 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1335#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001336#endif
1337 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001338#if defined(CONFIG_CMD_ENV_EXISTS)
1339 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1340#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001341};
1342
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001343#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001344void env_reloc(void)
1345{
1346 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1347}
1348#endif
1349
Macpaul Linff2a9f32011-04-26 16:16:45 +00001350static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001351{
1352 cmd_tbl_t *cp;
1353
Thomas Weber4d282652010-11-24 13:07:52 +01001354 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001355 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001356
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001357 /* drop initial "env" arg */
1358 argc--;
1359 argv++;
1360
1361 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1362
1363 if (cp)
1364 return cp->cmd(cmdtp, flag, argc, argv);
1365
Simon Glassa06dfc72011-12-10 08:44:01 +00001366 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001367}
1368
Kim Phillipsdc00a682012-10-29 13:34:31 +00001369#ifdef CONFIG_SYS_LONGHELP
1370static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001371#if defined(CONFIG_CMD_ASKENV)
1372 "ask name [message] [size] - ask for environment variable\nenv "
1373#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001374#if defined(CONFIG_CMD_ENV_CALLBACK)
1375 "callbacks - print callbacks and their associated variables\nenv "
1376#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001377 "default [-f] -a - [forcibly] reset default environment\n"
1378 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001379 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001380#if defined(CONFIG_CMD_EDITENV)
1381 "env edit name - edit environment variable\n"
1382#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001383#if defined(CONFIG_CMD_ENV_EXISTS)
1384 "env exists name - tests for existence of variable\n"
1385#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001386#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001387 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001388#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001389#if defined(CONFIG_CMD_ENV_FLAGS)
1390 "env flags - print variables that have non-default flags\n"
1391#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001392#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001393#ifdef CONFIG_REGEX
1394 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1395#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001396 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001397#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001398#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001399#if defined(CONFIG_CMD_IMPORTENV)
Quentin Schulz0312b942018-07-09 19:16:29 +02001400 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001401#endif
Leo Ruan7450b872019-05-24 17:20:19 +02001402#if defined(CONFIG_CMD_NVEDIT_INFO)
1403 "env info - display environment information\n"
1404 "env info [-d] - whether default environment is used\n"
1405 "env info [-p] - whether environment can be persisted\n"
1406#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001407 "env print [-a | name ...] - print environment\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001408#if defined(CONFIG_CMD_NVEDIT_EFI)
1409 "env print -e [name ...] - print UEFI environment\n"
1410#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001411#if defined(CONFIG_CMD_RUN)
1412 "env run var [...] - run commands in an environment variable\n"
1413#endif
Patrick Delaunaybd5f5812019-02-18 10:58:16 +01001414#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001415 "env save - save environment\n"
Frank Wunderlich33afa932019-06-29 11:36:19 +02001416#if defined(CONFIG_CMD_ERASEENV)
1417 "env erase - erase environment\n"
1418#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001419#endif
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001420#if defined(CONFIG_CMD_NVEDIT_EFI)
1421 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1422#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001423 "env set [-f] name [arg ...]\n";
1424#endif
1425
1426U_BOOT_CMD(
1427 env, CONFIG_SYS_MAXARGS, 1, do_env,
1428 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001429);
1430
1431/*
1432 * Old command line interface, kept for compatibility
1433 */
wdenk57b2d802003-06-27 21:31:46 +00001434
Peter Tyser0deafa22009-10-25 15:12:56 -05001435#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001436U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001437 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001438 "edit environment variable",
1439 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001440 " - edit environment variable 'name'",
1441 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001442);
1443#endif
1444
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001445U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001446 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001447 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001448 "[-a]\n - print [all] values of all environment variables\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001449#if defined(CONFIG_CMD_NVEDIT_EFI)
1450 "printenv -e [name ...]\n"
1451 " - print UEFI variable 'name' or all the variables\n"
1452#endif
wdenk57b2d802003-06-27 21:31:46 +00001453 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001454 " - print value of environment variable 'name'",
1455 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001456);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001457
1458#ifdef CONFIG_CMD_GREPENV
1459U_BOOT_CMD_COMPLETE(
1460 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1461 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001462#ifdef CONFIG_REGEX
1463 "[-e] [-n | -v | -b] string ...\n"
1464#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001465 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001466#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001467 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001468#ifdef CONFIG_REGEX
1469 " \"-e\": enable regular expressions;\n"
1470#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001471 " \"-n\": search variable names; \"-v\": search values;\n"
1472 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001473 var_complete
1474);
1475#endif
wdenk57b2d802003-06-27 21:31:46 +00001476
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001477U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001478 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001479 "set environment variables",
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001480#if defined(CONFIG_CMD_NVEDIT_EFI)
AKASHI Takahiro26105762019-06-04 15:52:11 +09001481 "-e [-nv] name [value ...]\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001482 " - set UEFI variable 'name' to 'value' ...'\n"
AKASHI Takahiro26105762019-06-04 15:52:11 +09001483 " 'nv' option makes the variable non-volatile\n"
AKASHI Takahiro117e68a2019-02-25 15:54:36 +09001484 " - delete UEFI variable 'name' if 'value' not specified\n"
1485#endif
1486 "setenv [-f] name value ...\n"
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001487 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1488 "setenv [-f] name\n"
1489 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001490 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001491);
1492
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001493#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001494
wdenkf287a242003-07-01 21:06:45 +00001495U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001496 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001497 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001498 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001499 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001500);
Jon Loeligerd704d912007-07-10 11:02:44 -05001501#endif
wdenk57b2d802003-06-27 21:31:46 +00001502
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001503#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001504U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001505 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001506 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001507 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001508 " - run the commands in the environment variable(s) 'var'",
1509 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001510);
Jon Loeligerd704d912007-07-10 11:02:44 -05001511#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001512#endif /* CONFIG_SPL_BUILD */