]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/nvedit.c
Merge tag 'u-boot-stm32-20190606' of https://github.com/pchotard/u-boot
[thirdparty/u-boot.git] / cmd / nvedit.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
a68d3ed0 2/*
ea009d47 3 * (C) Copyright 2000-2013
a68d3ed0
WD
4 * 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>
a000b795
KP
8 *
9 * Copyright 2011 Freescale Semiconductor, Inc.
a68d3ed0
WD
10 */
11
ea882baf 12/*
a68d3ed0
WD
13 * Support for persistent environment data
14 *
ea882baf
WD
15 * 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
fc0b5948 17 * a double '\0'. The environment is preceded by a 32 bit CRC over
ea882baf
WD
18 * the data part and, in case of redundant environment, a byte of
19 * flags.
a68d3ed0 20 *
ea882baf
WD
21 * 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.
a68d3ed0
WD
24 */
25
26#include <common.h>
18d66533 27#include <cli.h>
a68d3ed0 28#include <command.h>
24b852a7 29#include <console.h>
a68d3ed0 30#include <environment.h>
ea882baf
WD
31#include <search.h>
32#include <errno.h>
246c6922 33#include <malloc.h>
0eb25b61 34#include <mapmem.h>
2a3cb020 35#include <watchdog.h>
a68d3ed0
WD
36#include <linux/stddef.h>
37#include <asm/byteorder.h>
fd37dac9 38#include <asm/io.h>
a68d3ed0 39
d87080b7
WD
40DECLARE_GLOBAL_DATA_PTR;
41
2643c85d
PD
42#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) && \
f3c615b8 60 !defined(CONFIG_ENV_IS_NOWHERE)
7b7341d7 61# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
d1b88cd3 62NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
a68d3ed0
WD
63#endif
64
ea882baf
WD
65/*
66 * Maximum expected input data size for import command
67 */
68#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
a68d3ed0 69
da95427c 70/*
ea882baf 71 * This variable is incremented on each do_env_set(), so it can
da95427c
HS
72 * 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 */
2f70c49e 77static int env_id = 1;
a68d3ed0 78
f3c615b8 79int get_env_id(void)
2f70c49e
HS
80{
81 return env_id;
82}
a68d3ed0 83
7ac2fe2d 84#ifndef CONFIG_SPL_BUILD
4c94f6c5 85/*
ea882baf
WD
86 * Command interface: print one or all environment variables
87 *
88 * Returns 0 in case of error, or length of printed string
4c94f6c5 89 */
be11235a 90static int env_print(char *name, int flag)
a68d3ed0 91{
ea882baf 92 char *res = NULL;
22a4a6c5 93 ssize_t len;
ea882baf
WD
94
95 if (name) { /* print a single name */
96 ENTRY e, *ep;
97
98 e.key = name;
99 e.data = NULL;
be11235a 100 hsearch_r(e, FIND, &ep, &env_htab, flag);
ea882baf
WD
101 if (ep == NULL)
102 return 0;
f3c615b8 103 len = printf("%s=%s\n", ep->key, ep->data);
ea882baf
WD
104 return len;
105 }
a68d3ed0 106
ea882baf 107 /* print whole list */
be11235a 108 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
a68d3ed0 109
ea882baf
WD
110 if (len > 0) {
111 puts(res);
112 free(res);
113 return len;
a68d3ed0
WD
114 }
115
ea882baf 116 /* should never happen */
22a4a6c5 117 printf("## Error: cannot export environment\n");
ea882baf 118 return 0;
4c94f6c5 119}
a68d3ed0 120
088f1b19
KP
121static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
122 char * const argv[])
4c94f6c5
MF
123{
124 int i;
125 int rcode = 0;
be11235a
JH
126 int env_flag = H_HIDE_DOT;
127
49d81fdf
AT
128#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
be11235a
JH
133 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
134 argc--;
135 argv++;
136 env_flag &= ~H_HIDE_DOT;
137 }
a68d3ed0 138
4c94f6c5
MF
139 if (argc == 1) {
140 /* print all env vars */
be11235a 141 rcode = env_print(NULL, env_flag);
ea882baf 142 if (!rcode)
4c94f6c5
MF
143 return 1;
144 printf("\nEnvironment size: %d/%ld bytes\n",
145 rcode, (ulong)ENV_SIZE);
146 return 0;
147 }
a68d3ed0 148
4c94f6c5 149 /* print selected env vars */
be11235a 150 env_flag &= ~H_HIDE_DOT;
4c94f6c5 151 for (i = 1; i < argc; ++i) {
be11235a 152 int rc = env_print(argv[i], env_flag);
ea882baf
WD
153 if (!rc) {
154 printf("## Error: \"%s\" not defined\n", argv[i]);
4c94f6c5 155 ++rcode;
a68d3ed0
WD
156 }
157 }
4c94f6c5 158
a68d3ed0
WD
159 return rcode;
160}
161
a000b795 162#ifdef CONFIG_CMD_GREPENV
d09b1787
IG
163static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
164 int argc, char * const argv[])
a000b795 165{
5a31ea04 166 char *res = NULL;
be29df6a 167 int len, grep_how, grep_what;
a000b795
KP
168
169 if (argc < 2)
4c12eeb8 170 return CMD_RET_USAGE;
a000b795 171
be29df6a
WD
172 grep_how = H_MATCH_SUBSTR; /* default: substring search */
173 grep_what = H_MATCH_BOTH; /* default: grep names and values */
d87244d5 174
9a832331
PA
175 while (--argc > 0 && **++argv == '-') {
176 char *arg = *argv;
d87244d5
WD
177 while (*++arg) {
178 switch (*arg) {
be29df6a
WD
179#ifdef CONFIG_REGEX
180 case 'e': /* use regex matching */
181 grep_how = H_MATCH_REGEX;
182 break;
183#endif
d87244d5 184 case 'n': /* grep for name */
be29df6a 185 grep_what = H_MATCH_KEY;
d87244d5
WD
186 break;
187 case 'v': /* grep for value */
be29df6a 188 grep_what = H_MATCH_DATA;
d87244d5
WD
189 break;
190 case 'b': /* grep for both */
be29df6a 191 grep_what = H_MATCH_BOTH;
d87244d5
WD
192 break;
193 case '-':
194 goto DONE;
195 default:
196 return CMD_RET_USAGE;
197 }
198 }
199 }
200
201DONE:
5a31ea04 202 len = hexport_r(&env_htab, '\n',
be29df6a 203 flag | grep_what | grep_how,
5a31ea04 204 &res, 0, argc, argv);
a000b795 205
5a31ea04
WD
206 if (len > 0) {
207 puts(res);
208 free(res);
a000b795
KP
209 }
210
5a31ea04
WD
211 if (len < 2)
212 return 1;
213
214 return 0;
a000b795
KP
215}
216#endif
7ac2fe2d 217#endif /* CONFIG_SPL_BUILD */
a000b795 218
c3f65258
GF
219/*
220 * Set a new environment variable,
221 * or replace or delete an existing one.
2598090b 222 */
94b467b1 223static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
c3f65258
GF
224{
225 int i, len;
226 char *name, *value, *s;
227 ENTRY e, *ep;
228
24ab5a19 229 debug("Initial value for argc=%d\n", argc);
49d81fdf
AT
230
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
24ab5a19
JH
236 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);
c3f65258 251 name = argv[1];
c3f65258
GF
252
253 if (strchr(name, '=')) {
254 printf("## Error: illegal character '='"
255 "in variable name \"%s\"\n", name);
256 return 1;
257 }
258
259 env_id++;
c3f65258 260
a68d3ed0 261 /* Delete only ? */
d09b1787 262 if (argc < 3 || argv[2] == NULL) {
24ab5a19 263 int rc = hdelete_r(name, &env_htab, env_flag);
ea882baf 264 return !rc;
a68d3ed0
WD
265 }
266
267 /*
ea882baf 268 * Insert / replace new value
a68d3ed0 269 */
f3c615b8 270 for (i = 2, len = 0; i < argc; ++i)
a68d3ed0 271 len += strlen(argv[i]) + 1;
f3c615b8
ML
272
273 value = malloc(len);
274 if (value == NULL) {
ea882baf 275 printf("## Can't malloc %d bytes\n", len);
a68d3ed0
WD
276 return 1;
277 }
f3c615b8 278 for (i = 2, s = value; i < argc; ++i) {
ea882baf 279 char *v = argv[i];
a68d3ed0 280
ea882baf 281 while ((*s++ = *v++) != '\0')
a68d3ed0 282 ;
d09b1787 283 *(s - 1) = ' ';
ea882baf
WD
284 }
285 if (s != value)
286 *--s = '\0';
287
d09b1787
IG
288 e.key = name;
289 e.data = value;
24ab5a19 290 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
ea882baf
WD
291 free(value);
292 if (!ep) {
293 printf("## Error inserting \"%s\" variable, errno=%d\n",
294 name, errno);
295 return 1;
a68d3ed0 296 }
a68d3ed0 297
a68d3ed0
WD
298 return 0;
299}
300
382bee57 301int env_set(const char *varname, const char *varvalue)
a68d3ed0 302{
84b5e802
WD
303 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
304
a7eb1d66
JH
305 /* before import into hashtable */
306 if (!(gd->flags & GD_FLG_ENV_READY))
307 return 1;
308
d09b1787 309 if (varvalue == NULL || varvalue[0] == '\0')
94b467b1 310 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
9ffd451a 311 else
94b467b1 312 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
a68d3ed0
WD
313}
314
d67f10ce
SG
315/**
316 * Set an environment variable to an integer value
317 *
9602286d 318 * @param varname Environment variable to set
d67f10ce
SG
319 * @param value Value to set it to
320 * @return 0 if ok, 1 on error
321 */
018f5303 322int env_set_ulong(const char *varname, ulong value)
d67f10ce
SG
323{
324 /* TODO: this should be unsigned */
325 char *str = simple_itoa(value);
326
382bee57 327 return env_set(varname, str);
d67f10ce
SG
328}
329
330/**
bfc59966 331 * Set an environment variable to an value in hex
d67f10ce 332 *
9602286d 333 * @param varname Environment variable to set
bfc59966 334 * @param value Value to set it to
d67f10ce
SG
335 * @return 0 if ok, 1 on error
336 */
018f5303 337int env_set_hex(const char *varname, ulong value)
d67f10ce
SG
338{
339 char str[17];
340
bfc59966 341 sprintf(str, "%lx", value);
382bee57 342 return env_set(varname, str);
d67f10ce
SG
343}
344
bfebc8c9 345ulong env_get_hex(const char *varname, ulong default_val)
76b8f79c
SG
346{
347 const char *s;
348 ulong value;
349 char *endp;
350
00caae6d 351 s = env_get(varname);
76b8f79c
SG
352 if (s)
353 value = simple_strtoul(s, &endp, 16);
354 if (!s || endp == s)
355 return default_val;
356
357 return value;
358}
359
9925f1db
AK
360void 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
7ac2fe2d 390#ifndef CONFIG_SPL_BUILD
088f1b19 391static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a68d3ed0 392{
47e26b1b 393 if (argc < 2)
4c12eeb8 394 return CMD_RET_USAGE;
a68d3ed0 395
94b467b1 396 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
a68d3ed0
WD
397}
398
ea882baf 399/*
a68d3ed0
WD
400 * Prompt for environment variable
401 */
c76fe474 402#if defined(CONFIG_CMD_ASKENV)
f3c615b8 403int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a68d3ed0 404{
6d0f6bcf 405 char message[CONFIG_SYS_CBSIZE];
7d85591d 406 int i, len, pos, size;
a68d3ed0 407 char *local_args[4];
7d85591d 408 char *endptr;
a68d3ed0
WD
409
410 local_args[0] = argv[0];
411 local_args[1] = argv[1];
412 local_args[2] = NULL;
413 local_args[3] = NULL;
414
7d85591d
WD
415 /*
416 * Check the syntax:
417 *
418 * env_ask envname [message1 ...] [size]
419 */
420 if (argc == 1)
4c12eeb8 421 return CMD_RET_USAGE;
a68d3ed0 422
7d85591d
WD
423 /*
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 }
a68d3ed0 436
7d85591d
WD
437 if (argc <= 2) {
438 sprintf(message, "Please enter '%s': ", argv[1]);
439 } else {
440 /* env_ask envname message1 ... messagen [size] */
bf52fcde 441 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
f3c615b8 442 if (pos)
a68d3ed0 443 message[pos++] = ' ';
f3c615b8 444
c667723f 445 strncpy(message + pos, argv[i], sizeof(message) - pos);
a68d3ed0
WD
446 pos += strlen(argv[i]);
447 }
c667723f
TR
448 if (pos < sizeof(message) - 1) {
449 message[pos++] = ' ';
450 message[pos] = '\0';
451 } else
452 message[CONFIG_SYS_CBSIZE - 1] = '\0';
a68d3ed0
WD
453 }
454
6d0f6bcf
JCPV
455 if (size >= CONFIG_SYS_CBSIZE)
456 size = CONFIG_SYS_CBSIZE - 1;
a68d3ed0
WD
457
458 if (size <= 0)
459 return 1;
460
461 /* prompt for input */
e1bf824d 462 len = cli_readline(message);
a68d3ed0
WD
463
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 */
94b467b1 474 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
a68d3ed0 475}
90253178 476#endif
a68d3ed0 477
5e2b3e0c 478#if defined(CONFIG_CMD_ENV_CALLBACK)
cca98fd6
JH
479static int print_static_binding(const char *var_name, const char *callback_name,
480 void *priv)
5e2b3e0c
JH
481{
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", "-------------", "-------------");
cca98fd6 542 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
5e2b3e0c
JH
543 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
fffad71b 554#if defined(CONFIG_CMD_ENV_FLAGS)
cca98fd6
JH
555static int print_static_flags(const char *var_name, const char *flags,
556 void *priv)
fffad71b
JH
557{
558 enum env_flags_vartype type = env_flags_parse_vartype(flags);
267541f7 559 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
fffad71b 560
267541f7
JH
561 printf("\t%-20s %-20s %-20s\n", var_name,
562 env_flags_get_vartype_name(type),
563 env_flags_get_varaccess_name(access));
fffad71b
JH
564
565 return 0;
566}
567
568static int print_active_flags(ENTRY *entry)
569{
570 enum env_flags_vartype type;
267541f7 571 enum env_flags_varaccess access;
fffad71b
JH
572
573 if (entry->flags == 0)
574 return 0;
575
576 type = (enum env_flags_vartype)
577 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
267541f7
JH
578 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));
fffad71b
JH
582
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
267541f7
JH
599 /* 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
fffad71b
JH
607 /* Print the static flags that may exist */
608 puts("Static flags:\n");
267541f7
JH
609 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
610 "Variable Access");
611 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
612 "---------------");
cca98fd6 613 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
fffad71b
JH
614 puts("\n");
615
616 /* walk through each variable and print the flags if non-default */
617 puts("Active flags:\n");
267541f7
JH
618 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
619 "Variable Access");
620 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
621 "---------------");
fffad71b
JH
622 hwalk_r(&env_htab, print_active_flags);
623 return 0;
624}
625#endif
626
ea882baf 627/*
246c6922
PT
628 * Interactively edit an environment variable
629 */
630#if defined(CONFIG_CMD_EDITENV)
088f1b19
KP
631static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
632 char * const argv[])
246c6922
PT
633{
634 char buffer[CONFIG_SYS_CBSIZE];
635 char *init_val;
246c6922 636
47e26b1b 637 if (argc < 2)
4c12eeb8 638 return CMD_RET_USAGE;
246c6922 639
94b467b1
JH
640 /* before import into hashtable */
641 if (!(gd->flags & GD_FLG_ENV_READY))
642 return 1;
643
246c6922 644 /* Set read buffer to initial value or empty sting */
00caae6d 645 init_val = env_get(argv[1]);
246c6922 646 if (init_val)
5d49b4cd 647 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
246c6922
PT
648 else
649 buffer[0] = '\0';
650
e1bf824d 651 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
18a3cce9 652 return 1;
246c6922 653
94b467b1
JH
654 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 }
246c6922
PT
664}
665#endif /* CONFIG_CMD_EDITENV */
7ac2fe2d 666#endif /* CONFIG_SPL_BUILD */
246c6922 667
ea882baf 668/*
a68d3ed0
WD
669 * Look up variable from environment,
670 * return address of storage for that variable,
671 * or NULL if not found
672 */
00caae6d 673char *env_get(const char *name)
a68d3ed0 674{
d09b1787 675 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
ea882baf 676 ENTRY e, *ep;
a68d3ed0 677
91a76751 678 WATCHDOG_RESET();
2a3cb020 679
d09b1787
IG
680 e.key = name;
681 e.data = NULL;
c4e0057f 682 hsearch_r(e, FIND, &ep, &env_htab, 0);
91a76751 683
f3c615b8 684 return ep ? ep->data : NULL;
a68d3ed0
WD
685 }
686
ea882baf 687 /* restricted capabilities before import */
00caae6d 688 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
ea882baf 689 return (char *)(gd->env_buf);
91a76751 690
ea882baf 691 return NULL;
a68d3ed0
WD
692}
693
ea882baf
WD
694/*
695 * Look up variable from environment for restricted C runtime env.
696 */
00caae6d 697int env_get_f(const char *name, char *buf, unsigned len)
a68d3ed0 698{
87c7fb39 699 int i, nxt, c;
a68d3ed0 700
d09b1787 701 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
a68d3ed0
WD
702 int val, n;
703
87c7fb39
SG
704 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
705 if (c < 0)
706 return c;
f3c615b8
ML
707 if (nxt >= CONFIG_ENV_SIZE)
708 return -1;
a68d3ed0 709 }
f3c615b8
ML
710
711 val = envmatch((uchar *)name, i);
712 if (val < 0)
a68d3ed0 713 continue;
9ed4a958 714
a68d3ed0 715 /* found; copy out */
f3c615b8 716 for (n = 0; n < len; ++n, ++buf) {
87c7fb39
SG
717 c = env_get_char(val++);
718 if (c < 0)
719 return c;
720 *buf = c;
d09b1787 721 if (*buf == '\0')
9ed4a958
WD
722 return n;
723 }
724
725 if (n)
726 *--buf = '\0';
727
82919517
HS
728 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
729 len, name);
9ed4a958
WD
730
731 return n;
a68d3ed0 732 }
d09b1787 733
f3c615b8 734 return -1;
a68d3ed0
WD
735}
736
4a9b4131
SG
737/**
738 * Decode the integer value of an environment variable and return it.
739 *
919d25c9 740 * @param name Name of environment variable
4a9b4131
SG
741 * @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 */
bfebc8c9 746ulong env_get_ulong(const char *name, int base, ulong default_val)
4a9b4131
SG
747{
748 /*
00caae6d 749 * We can use env_get() here, even before relocation, since the
4a9b4131
SG
750 * environment variable value is an integer and thus short.
751 */
00caae6d 752 const char *str = env_get(name);
4a9b4131
SG
753
754 return str ? simple_strtoul(str, NULL, base) : default_val;
755}
756
7ac2fe2d 757#ifndef CONFIG_SPL_BUILD
2643c85d 758#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
088f1b19
KP
759static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
760 char * const argv[])
a68d3ed0 761{
01510091 762 return env_save() ? 1 : 0;
a68d3ed0 763}
8bde7f77 764
ba69dc26 765U_BOOT_CMD(
ea882baf 766 saveenv, 1, 0, do_env_save,
2fb2604d 767 "save environment variables to persistent storage",
a89c33db 768 ""
ba69dc26 769);
a68d3ed0 770#endif
7ac2fe2d 771#endif /* CONFIG_SPL_BUILD */
a68d3ed0
WD
772
773
ea882baf 774/*
a68d3ed0
WD
775 * Match a name / name=value pair
776 *
777 * s1 is either a simple 'name', or a 'name=value' pair.
778 * i2 is the environment index for a 'name2=value2' pair.
d09b1787 779 * If the names match, return the index for the value2, else -1.
a68d3ed0 780 */
f3c615b8 781int envmatch(uchar *s1, int i2)
a68d3ed0 782{
586197df
JH
783 if (s1 == NULL)
784 return -1;
785
a68d3ed0
WD
786 while (*s1 == env_get_char(i2++))
787 if (*s1++ == '=')
f3c615b8 788 return i2;
d09b1787 789
a68d3ed0 790 if (*s1 == '\0' && env_get_char(i2-1) == '=')
f3c615b8 791 return i2;
d09b1787 792
f3c615b8 793 return -1;
a68d3ed0 794}
8bde7f77 795
7ac2fe2d 796#ifndef CONFIG_SPL_BUILD
30091494 797static int do_env_default(cmd_tbl_t *cmdtp, int flag,
d09b1787 798 int argc, char * const argv[])
ea882baf 799{
5a04264e 800 int all = 0, env_flag = H_INTERACTIVE;
f3c615b8 801
b64b7c3d
GF
802 debug("Initial value for argc=%d\n", argc);
803 while (--argc > 0 && **++argv == '-') {
804 char *arg = *argv;
805
806 while (*++arg) {
807 switch (*arg) {
808 case 'a': /* default all */
809 all = 1;
810 break;
811 case 'f': /* force */
30091494 812 env_flag |= H_FORCE;
b64b7c3d
GF
813 break;
814 default:
815 return cmd_usage(cmdtp);
816 }
817 }
818 }
819 debug("Final value for argc=%d\n", argc);
820 if (all && (argc == 0)) {
821 /* Reset the whole environment */
c5d548a9
YL
822 set_default_env("## Resetting to default environment\n",
823 env_flag);
b64b7c3d
GF
824 return 0;
825 }
826 if (!all && (argc > 0)) {
827 /* Reset individual variables */
477f8116 828 set_default_vars(argc, argv, env_flag);
b64b7c3d
GF
829 return 0;
830 }
831
832 return cmd_usage(cmdtp);
ea882baf
WD
833}
834
d09b1787
IG
835static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
836 int argc, char * const argv[])
ea882baf 837{
9d8d661d
JH
838 int env_flag = H_INTERACTIVE;
839 int ret = 0;
840
841 debug("Initial value for argc=%d\n", argc);
842 while (argc > 1 && **(argv + 1) == '-') {
843 char *arg = *++argv;
844
845 --argc;
846 while (*++arg) {
847 switch (*arg) {
848 case 'f': /* force */
849 env_flag |= H_FORCE;
850 break;
851 default:
852 return CMD_RET_USAGE;
853 }
854 }
855 }
856 debug("Final value for argc=%d\n", argc);
857
858 env_id++;
859
860 while (--argc > 0) {
861 char *name = *++argv;
862
863 if (!hdelete_r(name, &env_htab, env_flag))
864 ret = 1;
865 }
866
867 return ret;
ea882baf
WD
868}
869
0c79cda0 870#ifdef CONFIG_CMD_EXPORTENV
ea882baf 871/*
37f2fe74 872 * env export [-t | -b | -c] [-s size] addr [var ...]
ea882baf
WD
873 * -t: export as text format; if size is given, data will be
874 * padded with '\0' bytes; if not, one terminating '\0'
875 * will be added (which is included in the "filesize"
876 * setting so you can for exmple copy this to flash and
877 * keep the termination).
878 * -b: export as binary format (name=value pairs separated by
879 * '\0', list end marked by double "\0\0")
880 * -c: export as checksum protected environment format as
881 * used for example by "saveenv" command
37f2fe74
WD
882 * -s size:
883 * size of output buffer
ea882baf 884 * addr: memory address where environment gets stored
37f2fe74
WD
885 * var... List of variable names that get included into the
886 * export. Without arguments, the whole environment gets
887 * exported.
ea882baf
WD
888 *
889 * With "-c" and size is NOT given, then the export command will
890 * format the data as currently used for the persistent storage,
891 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
fc0b5948 892 * prepend a valid CRC32 checksum and, in case of redundant
ea882baf
WD
893 * environment, a "current" redundancy flag. If size is given, this
894 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
895 * checksum and redundancy flag will be inserted.
896 *
897 * With "-b" and "-t", always only the real data (including a
898 * terminating '\0' byte) will be written; here the optional size
899 * argument will be used to make sure not to overflow the user
900 * provided buffer; the command will abort if the size is not
fc0b5948 901 * sufficient. Any remaining space will be '\0' padded.
ea882baf
WD
902 *
903 * On successful return, the variable "filesize" will be set.
904 * Note that filesize includes the trailing/terminating '\0' byte(s).
905 *
fc0b5948 906 * Usage scenario: create a text snapshot/backup of the current settings:
ea882baf
WD
907 *
908 * => env export -t 100000
909 * => era ${backup_addr} +${filesize}
910 * => cp.b 100000 ${backup_addr} ${filesize}
911 *
912 * Re-import this snapshot, deleting all other settings:
913 *
914 * => env import -d -t ${backup_addr}
915 */
d09b1787
IG
916static int do_env_export(cmd_tbl_t *cmdtp, int flag,
917 int argc, char * const argv[])
ea882baf
WD
918{
919 char buf[32];
fd37dac9
SG
920 ulong addr;
921 char *ptr, *cmd, *res;
37f2fe74 922 size_t size = 0;
ea882baf
WD
923 ssize_t len;
924 env_t *envp;
925 char sep = '\n';
926 int chk = 0;
927 int fmt = 0;
928
929 cmd = *argv;
930
931 while (--argc > 0 && **++argv == '-') {
932 char *arg = *argv;
933 while (*++arg) {
934 switch (*arg) {
935 case 'b': /* raw binary format */
936 if (fmt++)
937 goto sep_err;
938 sep = '\0';
939 break;
940 case 'c': /* external checksum format */
941 if (fmt++)
942 goto sep_err;
943 sep = '\0';
944 chk = 1;
945 break;
37f2fe74
WD
946 case 's': /* size given */
947 if (--argc <= 0)
948 return cmd_usage(cmdtp);
949 size = simple_strtoul(*++argv, NULL, 16);
950 goto NXTARG;
ea882baf
WD
951 case 't': /* text format */
952 if (fmt++)
953 goto sep_err;
954 sep = '\n';
955 break;
956 default:
4c12eeb8 957 return CMD_RET_USAGE;
ea882baf
WD
958 }
959 }
37f2fe74 960NXTARG: ;
ea882baf
WD
961 }
962
f3c615b8 963 if (argc < 1)
4c12eeb8 964 return CMD_RET_USAGE;
8bde7f77 965
fd37dac9
SG
966 addr = simple_strtoul(argv[0], NULL, 16);
967 ptr = map_sysmem(addr, size);
ea882baf 968
37f2fe74 969 if (size)
fd37dac9 970 memset(ptr, '\0', size);
37f2fe74
WD
971
972 argc--;
973 argv++;
ea882baf
WD
974
975 if (sep) { /* export as text file */
ea009d47
WD
976 len = hexport_r(&env_htab, sep,
977 H_MATCH_KEY | H_MATCH_IDENT,
fd37dac9 978 &ptr, size, argc, argv);
ea882baf 979 if (len < 0) {
6c90f623
QS
980 pr_err("## Error: Cannot export environment: errno = %d\n",
981 errno);
ea882baf
WD
982 return 1;
983 }
8c3aff52 984 sprintf(buf, "%zX", (size_t)len);
382bee57 985 env_set("filesize", buf);
ea882baf
WD
986
987 return 0;
988 }
989
fd37dac9 990 envp = (env_t *)ptr;
ea882baf
WD
991
992 if (chk) /* export as checksum protected block */
993 res = (char *)envp->data;
994 else /* export as raw binary data */
fd37dac9 995 res = ptr;
ea882baf 996
ea009d47
WD
997 len = hexport_r(&env_htab, '\0',
998 H_MATCH_KEY | H_MATCH_IDENT,
999 &res, ENV_SIZE, argc, argv);
ea882baf 1000 if (len < 0) {
6c90f623
QS
1001 pr_err("## Error: Cannot export environment: errno = %d\n",
1002 errno);
ea882baf
WD
1003 return 1;
1004 }
1005
1006 if (chk) {
d71b029d
NS
1007 envp->crc = crc32(0, envp->data,
1008 size ? size - offsetof(env_t, data) : ENV_SIZE);
ea882baf
WD
1009#ifdef CONFIG_ENV_ADDR_REDUND
1010 envp->flags = ACTIVE_FLAG;
1011#endif
1012 }
018f5303 1013 env_set_hex("filesize", len + offsetof(env_t, data));
ea882baf
WD
1014
1015 return 0;
1016
1017sep_err:
6c90f623
QS
1018 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1019 cmd);
ea882baf
WD
1020 return 1;
1021}
0c79cda0 1022#endif
ea882baf 1023
0c79cda0 1024#ifdef CONFIG_CMD_IMPORTENV
ea882baf 1025/*
eaf73472
QS
1026 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1027 * -d: delete existing environment before importing if no var is
1028 * passed; if vars are passed, if one var is in the current
1029 * environment but not in the environment at addr, delete var from
1030 * current environment;
fc0b5948 1031 * otherwise overwrite / append to existing definitions
ea882baf
WD
1032 * -t: assume text format; either "size" must be given or the
1033 * text data must be '\0' terminated
ecd1446f
AH
1034 * -r: handle CRLF like LF, that means exported variables with
1035 * a content which ends with \r won't get imported. Used
1036 * to import text files created with editors which are using CRLF
1037 * for line endings. Only effective in addition to -t.
ea882baf
WD
1038 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1039 * -c: assume checksum protected environment format
1040 * addr: memory address to read from
1041 * size: length of input data; if missing, proper '\0'
1042 * termination is mandatory
eaf73472
QS
1043 * if var is set and size should be missing (i.e. '\0'
1044 * termination), set size to '-'
1045 * var... List of the names of the only variables that get imported from
1046 * the environment at address 'addr'. Without arguments, the whole
1047 * environment gets imported.
ea882baf 1048 */
d09b1787
IG
1049static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1050 int argc, char * const argv[])
ea882baf 1051{
fd37dac9
SG
1052 ulong addr;
1053 char *cmd, *ptr;
ea882baf
WD
1054 char sep = '\n';
1055 int chk = 0;
1056 int fmt = 0;
1057 int del = 0;
ecd1446f 1058 int crlf_is_lf = 0;
eaf73472 1059 int wl = 0;
ea882baf
WD
1060 size_t size;
1061
1062 cmd = *argv;
1063
1064 while (--argc > 0 && **++argv == '-') {
1065 char *arg = *argv;
1066 while (*++arg) {
1067 switch (*arg) {
1068 case 'b': /* raw binary format */
1069 if (fmt++)
1070 goto sep_err;
1071 sep = '\0';
1072 break;
1073 case 'c': /* external checksum format */
1074 if (fmt++)
1075 goto sep_err;
1076 sep = '\0';
1077 chk = 1;
1078 break;
1079 case 't': /* text format */
1080 if (fmt++)
1081 goto sep_err;
1082 sep = '\n';
1083 break;
ecd1446f
AH
1084 case 'r': /* handle CRLF like LF */
1085 crlf_is_lf = 1;
1086 break;
ea882baf
WD
1087 case 'd':
1088 del = 1;
1089 break;
1090 default:
4c12eeb8 1091 return CMD_RET_USAGE;
ea882baf
WD
1092 }
1093 }
1094 }
1095
f3c615b8 1096 if (argc < 1)
4c12eeb8 1097 return CMD_RET_USAGE;
ea882baf
WD
1098
1099 if (!fmt)
1100 printf("## Warning: defaulting to text format\n");
1101
ecd1446f
AH
1102 if (sep != '\n' && crlf_is_lf )
1103 crlf_is_lf = 0;
1104
fd37dac9
SG
1105 addr = simple_strtoul(argv[0], NULL, 16);
1106 ptr = map_sysmem(addr, 0);
ea882baf 1107
eaf73472 1108 if (argc >= 2 && strcmp(argv[1], "-")) {
ea882baf 1109 size = simple_strtoul(argv[1], NULL, 16);
eaf73472 1110 } else if (chk) {
3775dcd9
TR
1111 puts("## Error: external checksum format must pass size\n");
1112 return CMD_RET_FAILURE;
ea882baf 1113 } else {
fd37dac9 1114 char *s = ptr;
ea882baf
WD
1115
1116 size = 0;
1117
1118 while (size < MAX_ENV_SIZE) {
1119 if ((*s == sep) && (*(s+1) == '\0'))
1120 break;
1121 ++s;
1122 ++size;
1123 }
1124 if (size == MAX_ENV_SIZE) {
1125 printf("## Warning: Input data exceeds %d bytes"
1126 " - truncated\n", MAX_ENV_SIZE);
1127 }
d3f80c77 1128 size += 2;
79afc88d 1129 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
ea882baf
WD
1130 }
1131
eaf73472
QS
1132 if (argc > 2)
1133 wl = 1;
1134
ea882baf
WD
1135 if (chk) {
1136 uint32_t crc;
fd37dac9 1137 env_t *ep = (env_t *)ptr;
ea882baf
WD
1138
1139 size -= offsetof(env_t, data);
1140 memcpy(&crc, &ep->crc, sizeof(crc));
1141
1142 if (crc32(0, ep->data, size) != crc) {
1143 puts("## Error: bad CRC, import failed\n");
1144 return 1;
1145 }
fd37dac9 1146 ptr = (char *)ep->data;
ea882baf
WD
1147 }
1148
eaf73472
QS
1149 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1150 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
6c90f623
QS
1151 pr_err("## Error: Environment import failed: errno = %d\n",
1152 errno);
ea882baf
WD
1153 return 1;
1154 }
1155 gd->flags |= GD_FLG_ENV_READY;
1156
1157 return 0;
1158
1159sep_err:
1160 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1161 cmd);
1162 return 1;
1163}
0c79cda0 1164#endif
ea882baf 1165
88733e2c
AR
1166#if defined(CONFIG_CMD_ENV_EXISTS)
1167static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1168 char * const argv[])
1169{
1170 ENTRY e, *ep;
1171
1172 if (argc < 2)
1173 return CMD_RET_USAGE;
1174
1175 e.key = argv[1];
1176 e.data = NULL;
1177 hsearch_r(e, FIND, &ep, &env_htab, 0);
1178
1179 return (ep == NULL) ? 1 : 0;
1180}
1181#endif
1182
ea882baf
WD
1183/*
1184 * New command line interface: "env" command with subcommands
1185 */
1186static cmd_tbl_t cmd_env_sub[] = {
1187#if defined(CONFIG_CMD_ASKENV)
1188 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1189#endif
1190 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
9d8d661d 1191 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
ea882baf
WD
1192#if defined(CONFIG_CMD_EDITENV)
1193 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1194#endif
5e2b3e0c
JH
1195#if defined(CONFIG_CMD_ENV_CALLBACK)
1196 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1197#endif
fffad71b
JH
1198#if defined(CONFIG_CMD_ENV_FLAGS)
1199 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1200#endif
0c79cda0 1201#if defined(CONFIG_CMD_EXPORTENV)
ea882baf 1202 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
0c79cda0 1203#endif
a000b795
KP
1204#if defined(CONFIG_CMD_GREPENV)
1205 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1206#endif
0c79cda0 1207#if defined(CONFIG_CMD_IMPORTENV)
ea882baf 1208 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
0c79cda0 1209#endif
ea882baf
WD
1210 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1211#if defined(CONFIG_CMD_RUN)
1212 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1213#endif
2643c85d 1214#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
ea882baf
WD
1215 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1216#endif
1217 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
88733e2c
AR
1218#if defined(CONFIG_CMD_ENV_EXISTS)
1219 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1220#endif
ea882baf
WD
1221};
1222
2e5167cc 1223#if defined(CONFIG_NEEDS_MANUAL_RELOC)
60f7da1f
HS
1224void env_reloc(void)
1225{
1226 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1227}
1228#endif
1229
f3c615b8 1230static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea882baf
WD
1231{
1232 cmd_tbl_t *cp;
1233
5904da02 1234 if (argc < 2)
4c12eeb8 1235 return CMD_RET_USAGE;
5904da02 1236
ea882baf
WD
1237 /* drop initial "env" arg */
1238 argc--;
1239 argv++;
1240
1241 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1242
1243 if (cp)
1244 return cp->cmd(cmdtp, flag, argc, argv);
1245
4c12eeb8 1246 return CMD_RET_USAGE;
ea882baf
WD
1247}
1248
088f1b19
KP
1249#ifdef CONFIG_SYS_LONGHELP
1250static char env_help_text[] =
ea882baf
WD
1251#if defined(CONFIG_CMD_ASKENV)
1252 "ask name [message] [size] - ask for environment variable\nenv "
5e2b3e0c
JH
1253#endif
1254#if defined(CONFIG_CMD_ENV_CALLBACK)
1255 "callbacks - print callbacks and their associated variables\nenv "
ea882baf 1256#endif
b64b7c3d
GF
1257 "default [-f] -a - [forcibly] reset default environment\n"
1258 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
9d8d661d 1259 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
ea882baf
WD
1260#if defined(CONFIG_CMD_EDITENV)
1261 "env edit name - edit environment variable\n"
1262#endif
88733e2c
AR
1263#if defined(CONFIG_CMD_ENV_EXISTS)
1264 "env exists name - tests for existence of variable\n"
1265#endif
4796bc4b 1266#if defined(CONFIG_CMD_EXPORTENV)
37f2fe74 1267 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
4796bc4b 1268#endif
fffad71b
JH
1269#if defined(CONFIG_CMD_ENV_FLAGS)
1270 "env flags - print variables that have non-default flags\n"
1271#endif
a000b795 1272#if defined(CONFIG_CMD_GREPENV)
be29df6a
WD
1273#ifdef CONFIG_REGEX
1274 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1275#else
d87244d5 1276 "env grep [-n | -v | -b] string [...] - search environment\n"
a000b795 1277#endif
be29df6a 1278#endif
4796bc4b 1279#if defined(CONFIG_CMD_IMPORTENV)
eaf73472 1280 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
4796bc4b 1281#endif
be11235a 1282 "env print [-a | name ...] - print environment\n"
49d81fdf
AT
1283#if defined(CONFIG_CMD_NVEDIT_EFI)
1284 "env print -e [name ...] - print UEFI environment\n"
1285#endif
ea882baf
WD
1286#if defined(CONFIG_CMD_RUN)
1287 "env run var [...] - run commands in an environment variable\n"
1288#endif
2643c85d 1289#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
ea882baf 1290 "env save - save environment\n"
49d81fdf
AT
1291#endif
1292#if defined(CONFIG_CMD_NVEDIT_EFI)
1293 "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
d798a9b5 1294#endif
088f1b19
KP
1295 "env set [-f] name [arg ...]\n";
1296#endif
1297
1298U_BOOT_CMD(
1299 env, CONFIG_SYS_MAXARGS, 1, do_env,
1300 "environment handling commands", env_help_text
ea882baf
WD
1301);
1302
1303/*
1304 * Old command line interface, kept for compatibility
1305 */
8bde7f77 1306
246c6922 1307#if defined(CONFIG_CMD_EDITENV)
722b061b 1308U_BOOT_CMD_COMPLETE(
ea882baf 1309 editenv, 2, 0, do_env_edit,
246c6922
PT
1310 "edit environment variable",
1311 "name\n"
722b061b
MF
1312 " - edit environment variable 'name'",
1313 var_complete
246c6922
PT
1314);
1315#endif
1316
722b061b 1317U_BOOT_CMD_COMPLETE(
ea882baf 1318 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
2fb2604d 1319 "print environment variables",
be11235a 1320 "[-a]\n - print [all] values of all environment variables\n"
49d81fdf
AT
1321#if defined(CONFIG_CMD_NVEDIT_EFI)
1322 "printenv -e [name ...]\n"
1323 " - print UEFI variable 'name' or all the variables\n"
1324#endif
8bde7f77 1325 "printenv name ...\n"
722b061b
MF
1326 " - print value of environment variable 'name'",
1327 var_complete
8bde7f77
WD
1328);
1329
a000b795
KP
1330#ifdef CONFIG_CMD_GREPENV
1331U_BOOT_CMD_COMPLETE(
1332 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1333 "search environment variables",
be29df6a
WD
1334#ifdef CONFIG_REGEX
1335 "[-e] [-n | -v | -b] string ...\n"
1336#else
d87244d5 1337 "[-n | -v | -b] string ...\n"
be29df6a 1338#endif
d87244d5 1339 " - list environment name=value pairs matching 'string'\n"
be29df6a
WD
1340#ifdef CONFIG_REGEX
1341 " \"-e\": enable regular expressions;\n"
1342#endif
d87244d5
WD
1343 " \"-n\": search variable names; \"-v\": search values;\n"
1344 " \"-b\": search both names and values (default)",
a000b795
KP
1345 var_complete
1346);
1347#endif
1348
722b061b 1349U_BOOT_CMD_COMPLETE(
ea882baf 1350 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
2fb2604d 1351 "set environment variables",
49d81fdf 1352#if defined(CONFIG_CMD_NVEDIT_EFI)
4b27a761 1353 "-e [-nv] name [value ...]\n"
49d81fdf 1354 " - set UEFI variable 'name' to 'value' ...'\n"
4b27a761 1355 " 'nv' option makes the variable non-volatile\n"
49d81fdf
AT
1356 " - delete UEFI variable 'name' if 'value' not specified\n"
1357#endif
1358 "setenv [-f] name value ...\n"
24ab5a19
JH
1359 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1360 "setenv [-f] name\n"
1361 " - [forcibly] delete environment variable 'name'",
722b061b 1362 var_complete
8bde7f77
WD
1363);
1364
c76fe474 1365#if defined(CONFIG_CMD_ASKENV)
8bde7f77 1366
0d498393 1367U_BOOT_CMD(
ea882baf 1368 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
2fb2604d 1369 "get environment variables from stdin",
8bde7f77 1370 "name [message] [size]\n"
7d85591d 1371 " - get environment variable 'name' from stdin (max 'size' chars)"
8bde7f77 1372);
90253178 1373#endif
8bde7f77 1374
c76fe474 1375#if defined(CONFIG_CMD_RUN)
722b061b 1376U_BOOT_CMD_COMPLETE(
6d0f6bcf 1377 run, CONFIG_SYS_MAXARGS, 1, do_run,
2fb2604d 1378 "run commands in an environment variable",
8bde7f77 1379 "var [...]\n"
722b061b
MF
1380 " - run the commands in the environment variable(s) 'var'",
1381 var_complete
8bde7f77 1382);
90253178 1383#endif
7ac2fe2d 1384#endif /* CONFIG_SPL_BUILD */