]> Git Repo - J-u-boot.git/blame - cmd/nvedit.c
cmd: env: add env select command
[J-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, [email protected].
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <[email protected]>
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>
f1f0ae6a 30#include <env.h>
f3998fdc 31#include <env_internal.h>
f7ae49fc 32#include <log.h>
90526e9f 33#include <net.h>
ea882baf
WD
34#include <search.h>
35#include <errno.h>
246c6922 36#include <malloc.h>
0eb25b61 37#include <mapmem.h>
cd93d625 38#include <linux/bitops.h>
3db71108 39#include <u-boot/crc.h>
2a3cb020 40#include <watchdog.h>
a68d3ed0
WD
41#include <linux/stddef.h>
42#include <asm/byteorder.h>
fd37dac9 43#include <asm/io.h>
a68d3ed0 44
d87080b7
WD
45DECLARE_GLOBAL_DATA_PTR;
46
2643c85d
PD
47#if defined(CONFIG_ENV_IS_IN_EEPROM) || \
48 defined(CONFIG_ENV_IS_IN_FLASH) || \
49 defined(CONFIG_ENV_IS_IN_MMC) || \
50 defined(CONFIG_ENV_IS_IN_FAT) || \
51 defined(CONFIG_ENV_IS_IN_EXT4) || \
52 defined(CONFIG_ENV_IS_IN_NAND) || \
53 defined(CONFIG_ENV_IS_IN_NVRAM) || \
54 defined(CONFIG_ENV_IS_IN_ONENAND) || \
55 defined(CONFIG_ENV_IS_IN_SATA) || \
56 defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \
57 defined(CONFIG_ENV_IS_IN_REMOTE) || \
58 defined(CONFIG_ENV_IS_IN_UBI)
59
60#define ENV_IS_IN_DEVICE
61
62#endif
63
64#if !defined(ENV_IS_IN_DEVICE) && \
f3c615b8 65 !defined(CONFIG_ENV_IS_NOWHERE)
7b7341d7 66# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
d1b88cd3 67NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
a68d3ed0
WD
68#endif
69
ea882baf
WD
70/*
71 * Maximum expected input data size for import command
72 */
73#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
a68d3ed0 74
da95427c 75/*
ea882baf 76 * This variable is incremented on each do_env_set(), so it can
f1f0ae6a 77 * be used via env_get_id() as an indication, if the environment
da95427c
HS
78 * has changed or not. So it is possible to reread an environment
79 * variable only if the environment was changed ... done so for
80 * example in NetInitLoop()
81 */
2f70c49e 82static int env_id = 1;
a68d3ed0 83
f1f0ae6a 84int env_get_id(void)
2f70c49e
HS
85{
86 return env_id;
87}
a68d3ed0 88
7ac2fe2d 89#ifndef CONFIG_SPL_BUILD
4c94f6c5 90/*
ea882baf
WD
91 * Command interface: print one or all environment variables
92 *
93 * Returns 0 in case of error, or length of printed string
4c94f6c5 94 */
be11235a 95static int env_print(char *name, int flag)
a68d3ed0 96{
ea882baf 97 char *res = NULL;
22a4a6c5 98 ssize_t len;
ea882baf
WD
99
100 if (name) { /* print a single name */
dd2408ca 101 struct env_entry e, *ep;
ea882baf
WD
102
103 e.key = name;
104 e.data = NULL;
3f0d6807 105 hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
ea882baf
WD
106 if (ep == NULL)
107 return 0;
f3c615b8 108 len = printf("%s=%s\n", ep->key, ep->data);
ea882baf
WD
109 return len;
110 }
a68d3ed0 111
ea882baf 112 /* print whole list */
be11235a 113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
a68d3ed0 114
ea882baf
WD
115 if (len > 0) {
116 puts(res);
117 free(res);
118 return len;
a68d3ed0
WD
119 }
120
ea882baf 121 /* should never happen */
22a4a6c5 122 printf("## Error: cannot export environment\n");
ea882baf 123 return 0;
4c94f6c5 124}
a68d3ed0 125
09140113
SG
126static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
127 char *const argv[])
4c94f6c5
MF
128{
129 int i;
130 int rcode = 0;
be11235a
JH
131 int env_flag = H_HIDE_DOT;
132
49d81fdf
AT
133#if defined(CONFIG_CMD_NVEDIT_EFI)
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
135 return do_env_print_efi(cmdtp, flag, --argc, ++argv);
136#endif
137
be11235a
JH
138 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
139 argc--;
140 argv++;
141 env_flag &= ~H_HIDE_DOT;
142 }
a68d3ed0 143
4c94f6c5
MF
144 if (argc == 1) {
145 /* print all env vars */
be11235a 146 rcode = env_print(NULL, env_flag);
ea882baf 147 if (!rcode)
4c94f6c5
MF
148 return 1;
149 printf("\nEnvironment size: %d/%ld bytes\n",
150 rcode, (ulong)ENV_SIZE);
151 return 0;
152 }
a68d3ed0 153
4c94f6c5 154 /* print selected env vars */
be11235a 155 env_flag &= ~H_HIDE_DOT;
4c94f6c5 156 for (i = 1; i < argc; ++i) {
be11235a 157 int rc = env_print(argv[i], env_flag);
ea882baf
WD
158 if (!rc) {
159 printf("## Error: \"%s\" not defined\n", argv[i]);
4c94f6c5 160 ++rcode;
a68d3ed0
WD
161 }
162 }
4c94f6c5 163
a68d3ed0
WD
164 return rcode;
165}
166
a000b795 167#ifdef CONFIG_CMD_GREPENV
09140113
SG
168static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
169 int argc, char *const argv[])
a000b795 170{
5a31ea04 171 char *res = NULL;
be29df6a 172 int len, grep_how, grep_what;
a000b795
KP
173
174 if (argc < 2)
4c12eeb8 175 return CMD_RET_USAGE;
a000b795 176
be29df6a
WD
177 grep_how = H_MATCH_SUBSTR; /* default: substring search */
178 grep_what = H_MATCH_BOTH; /* default: grep names and values */
d87244d5 179
9a832331
PA
180 while (--argc > 0 && **++argv == '-') {
181 char *arg = *argv;
d87244d5
WD
182 while (*++arg) {
183 switch (*arg) {
be29df6a
WD
184#ifdef CONFIG_REGEX
185 case 'e': /* use regex matching */
186 grep_how = H_MATCH_REGEX;
187 break;
188#endif
d87244d5 189 case 'n': /* grep for name */
be29df6a 190 grep_what = H_MATCH_KEY;
d87244d5
WD
191 break;
192 case 'v': /* grep for value */
be29df6a 193 grep_what = H_MATCH_DATA;
d87244d5
WD
194 break;
195 case 'b': /* grep for both */
be29df6a 196 grep_what = H_MATCH_BOTH;
d87244d5
WD
197 break;
198 case '-':
199 goto DONE;
200 default:
201 return CMD_RET_USAGE;
202 }
203 }
204 }
205
206DONE:
5a31ea04 207 len = hexport_r(&env_htab, '\n',
be29df6a 208 flag | grep_what | grep_how,
5a31ea04 209 &res, 0, argc, argv);
a000b795 210
5a31ea04
WD
211 if (len > 0) {
212 puts(res);
213 free(res);
a000b795
KP
214 }
215
5a31ea04
WD
216 if (len < 2)
217 return 1;
218
219 return 0;
a000b795
KP
220}
221#endif
7ac2fe2d 222#endif /* CONFIG_SPL_BUILD */
a000b795 223
c3f65258
GF
224/*
225 * Set a new environment variable,
226 * or replace or delete an existing one.
2598090b 227 */
09140113 228static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
c3f65258
GF
229{
230 int i, len;
231 char *name, *value, *s;
dd2408ca 232 struct env_entry e, *ep;
c3f65258 233
24ab5a19 234 debug("Initial value for argc=%d\n", argc);
49d81fdf
AT
235
236#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
237 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
238 return do_env_set_efi(NULL, flag, --argc, ++argv);
239#endif
240
24ab5a19
JH
241 while (argc > 1 && **(argv + 1) == '-') {
242 char *arg = *++argv;
243
244 --argc;
245 while (*++arg) {
246 switch (*arg) {
247 case 'f': /* force */
248 env_flag |= H_FORCE;
249 break;
250 default:
251 return CMD_RET_USAGE;
252 }
253 }
254 }
255 debug("Final value for argc=%d\n", argc);
c3f65258 256 name = argv[1];
c3f65258
GF
257
258 if (strchr(name, '=')) {
259 printf("## Error: illegal character '='"
260 "in variable name \"%s\"\n", name);
261 return 1;
262 }
263
264 env_id++;
c3f65258 265
a68d3ed0 266 /* Delete only ? */
d09b1787 267 if (argc < 3 || argv[2] == NULL) {
24ab5a19 268 int rc = hdelete_r(name, &env_htab, env_flag);
ea882baf 269 return !rc;
a68d3ed0
WD
270 }
271
272 /*
ea882baf 273 * Insert / replace new value
a68d3ed0 274 */
f3c615b8 275 for (i = 2, len = 0; i < argc; ++i)
a68d3ed0 276 len += strlen(argv[i]) + 1;
f3c615b8
ML
277
278 value = malloc(len);
279 if (value == NULL) {
ea882baf 280 printf("## Can't malloc %d bytes\n", len);
a68d3ed0
WD
281 return 1;
282 }
f3c615b8 283 for (i = 2, s = value; i < argc; ++i) {
ea882baf 284 char *v = argv[i];
a68d3ed0 285
ea882baf 286 while ((*s++ = *v++) != '\0')
a68d3ed0 287 ;
d09b1787 288 *(s - 1) = ' ';
ea882baf
WD
289 }
290 if (s != value)
291 *--s = '\0';
292
d09b1787
IG
293 e.key = name;
294 e.data = value;
3f0d6807 295 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
ea882baf
WD
296 free(value);
297 if (!ep) {
298 printf("## Error inserting \"%s\" variable, errno=%d\n",
299 name, errno);
300 return 1;
a68d3ed0 301 }
a68d3ed0 302
a68d3ed0
WD
303 return 0;
304}
305
382bee57 306int env_set(const char *varname, const char *varvalue)
a68d3ed0 307{
84b5e802
WD
308 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
309
a7eb1d66
JH
310 /* before import into hashtable */
311 if (!(gd->flags & GD_FLG_ENV_READY))
312 return 1;
313
d09b1787 314 if (varvalue == NULL || varvalue[0] == '\0')
94b467b1 315 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
9ffd451a 316 else
94b467b1 317 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
a68d3ed0
WD
318}
319
d67f10ce
SG
320/**
321 * Set an environment variable to an integer value
322 *
9602286d 323 * @param varname Environment variable to set
d67f10ce
SG
324 * @param value Value to set it to
325 * @return 0 if ok, 1 on error
326 */
018f5303 327int env_set_ulong(const char *varname, ulong value)
d67f10ce
SG
328{
329 /* TODO: this should be unsigned */
330 char *str = simple_itoa(value);
331
382bee57 332 return env_set(varname, str);
d67f10ce
SG
333}
334
335/**
bfc59966 336 * Set an environment variable to an value in hex
d67f10ce 337 *
9602286d 338 * @param varname Environment variable to set
bfc59966 339 * @param value Value to set it to
d67f10ce
SG
340 * @return 0 if ok, 1 on error
341 */
018f5303 342int env_set_hex(const char *varname, ulong value)
d67f10ce
SG
343{
344 char str[17];
345
bfc59966 346 sprintf(str, "%lx", value);
382bee57 347 return env_set(varname, str);
d67f10ce
SG
348}
349
bfebc8c9 350ulong env_get_hex(const char *varname, ulong default_val)
76b8f79c
SG
351{
352 const char *s;
353 ulong value;
354 char *endp;
355
00caae6d 356 s = env_get(varname);
76b8f79c
SG
357 if (s)
358 value = simple_strtoul(s, &endp, 16);
359 if (!s || endp == s)
360 return default_val;
361
362 return value;
363}
364
9925f1db
AK
365int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
366{
fb8977c5 367 string_to_enetaddr(env_get(name), enetaddr);
9925f1db
AK
368 return is_valid_ethaddr(enetaddr);
369}
370
371int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
372{
373 char buf[ARP_HLEN_ASCII + 1];
374
375 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
376 return -EEXIST;
377
378 sprintf(buf, "%pM", enetaddr);
379
380 return env_set(name, buf);
381}
382
7ac2fe2d 383#ifndef CONFIG_SPL_BUILD
09140113
SG
384static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc,
385 char *const argv[])
a68d3ed0 386{
47e26b1b 387 if (argc < 2)
4c12eeb8 388 return CMD_RET_USAGE;
a68d3ed0 389
94b467b1 390 return _do_env_set(flag, argc, argv, H_INTERACTIVE);
a68d3ed0
WD
391}
392
ea882baf 393/*
a68d3ed0
WD
394 * Prompt for environment variable
395 */
c76fe474 396#if defined(CONFIG_CMD_ASKENV)
09140113 397int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
a68d3ed0 398{
6d0f6bcf 399 char message[CONFIG_SYS_CBSIZE];
7d85591d 400 int i, len, pos, size;
a68d3ed0 401 char *local_args[4];
7d85591d 402 char *endptr;
a68d3ed0
WD
403
404 local_args[0] = argv[0];
405 local_args[1] = argv[1];
406 local_args[2] = NULL;
407 local_args[3] = NULL;
408
7d85591d
WD
409 /*
410 * Check the syntax:
411 *
412 * env_ask envname [message1 ...] [size]
413 */
414 if (argc == 1)
4c12eeb8 415 return CMD_RET_USAGE;
a68d3ed0 416
7d85591d
WD
417 /*
418 * We test the last argument if it can be converted
419 * into a decimal number. If yes, we assume it's
420 * the size. Otherwise we echo it as part of the
421 * message.
422 */
423 i = simple_strtoul(argv[argc - 1], &endptr, 10);
424 if (*endptr != '\0') { /* no size */
425 size = CONFIG_SYS_CBSIZE - 1;
426 } else { /* size given */
427 size = i;
428 --argc;
429 }
a68d3ed0 430
7d85591d
WD
431 if (argc <= 2) {
432 sprintf(message, "Please enter '%s': ", argv[1]);
433 } else {
434 /* env_ask envname message1 ... messagen [size] */
bf52fcde 435 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
f3c615b8 436 if (pos)
a68d3ed0 437 message[pos++] = ' ';
f3c615b8 438
c667723f 439 strncpy(message + pos, argv[i], sizeof(message) - pos);
a68d3ed0
WD
440 pos += strlen(argv[i]);
441 }
c667723f
TR
442 if (pos < sizeof(message) - 1) {
443 message[pos++] = ' ';
444 message[pos] = '\0';
445 } else
446 message[CONFIG_SYS_CBSIZE - 1] = '\0';
a68d3ed0
WD
447 }
448
6d0f6bcf
JCPV
449 if (size >= CONFIG_SYS_CBSIZE)
450 size = CONFIG_SYS_CBSIZE - 1;
a68d3ed0
WD
451
452 if (size <= 0)
453 return 1;
454
455 /* prompt for input */
e1bf824d 456 len = cli_readline(message);
a68d3ed0
WD
457
458 if (size < len)
459 console_buffer[size] = '\0';
460
461 len = 2;
462 if (console_buffer[0] != '\0') {
463 local_args[2] = console_buffer;
464 len = 3;
465 }
466
467 /* Continue calling setenv code */
94b467b1 468 return _do_env_set(flag, len, local_args, H_INTERACTIVE);
a68d3ed0 469}
90253178 470#endif
a68d3ed0 471
5e2b3e0c 472#if defined(CONFIG_CMD_ENV_CALLBACK)
cca98fd6
JH
473static int print_static_binding(const char *var_name, const char *callback_name,
474 void *priv)
5e2b3e0c
JH
475{
476 printf("\t%-20s %-20s\n", var_name, callback_name);
477
478 return 0;
479}
480
dd2408ca 481static int print_active_callback(struct env_entry *entry)
5e2b3e0c
JH
482{
483 struct env_clbk_tbl *clbkp;
484 int i;
485 int num_callbacks;
486
487 if (entry->callback == NULL)
488 return 0;
489
490 /* look up the callback in the linker-list */
491 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
492 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
493 i < num_callbacks;
494 i++, clbkp++) {
495#if defined(CONFIG_NEEDS_MANUAL_RELOC)
496 if (entry->callback == clbkp->callback + gd->reloc_off)
497#else
498 if (entry->callback == clbkp->callback)
499#endif
500 break;
501 }
502
503 if (i == num_callbacks)
504 /* this should probably never happen, but just in case... */
505 printf("\t%-20s %p\n", entry->key, entry->callback);
506 else
507 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
508
509 return 0;
510}
511
512/*
513 * Print the callbacks available and what they are bound to
514 */
09140113
SG
515int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc,
516 char *const argv[])
5e2b3e0c
JH
517{
518 struct env_clbk_tbl *clbkp;
519 int i;
520 int num_callbacks;
521
522 /* Print the available callbacks */
523 puts("Available callbacks:\n");
524 puts("\tCallback Name\n");
525 puts("\t-------------\n");
526 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
527 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
528 i < num_callbacks;
529 i++, clbkp++)
530 printf("\t%s\n", clbkp->name);
531 puts("\n");
532
533 /* Print the static bindings that may exist */
534 puts("Static callback bindings:\n");
535 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
536 printf("\t%-20s %-20s\n", "-------------", "-------------");
cca98fd6 537 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
5e2b3e0c
JH
538 puts("\n");
539
540 /* walk through each variable and print the callback if it has one */
541 puts("Active callback bindings:\n");
542 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
543 printf("\t%-20s %-20s\n", "-------------", "-------------");
544 hwalk_r(&env_htab, print_active_callback);
545 return 0;
546}
547#endif
548
fffad71b 549#if defined(CONFIG_CMD_ENV_FLAGS)
cca98fd6
JH
550static int print_static_flags(const char *var_name, const char *flags,
551 void *priv)
fffad71b
JH
552{
553 enum env_flags_vartype type = env_flags_parse_vartype(flags);
267541f7 554 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
fffad71b 555
267541f7
JH
556 printf("\t%-20s %-20s %-20s\n", var_name,
557 env_flags_get_vartype_name(type),
558 env_flags_get_varaccess_name(access));
fffad71b
JH
559
560 return 0;
561}
562
dd2408ca 563static int print_active_flags(struct env_entry *entry)
fffad71b
JH
564{
565 enum env_flags_vartype type;
267541f7 566 enum env_flags_varaccess access;
fffad71b
JH
567
568 if (entry->flags == 0)
569 return 0;
570
571 type = (enum env_flags_vartype)
572 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
267541f7
JH
573 access = env_flags_parse_varaccess_from_binflags(entry->flags);
574 printf("\t%-20s %-20s %-20s\n", entry->key,
575 env_flags_get_vartype_name(type),
576 env_flags_get_varaccess_name(access));
fffad71b
JH
577
578 return 0;
579}
580
581/*
582 * Print the flags available and what variables have flags
583 */
09140113 584int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
fffad71b
JH
585{
586 /* Print the available variable types */
587 printf("Available variable type flags (position %d):\n",
588 ENV_FLAGS_VARTYPE_LOC);
589 puts("\tFlag\tVariable Type Name\n");
590 puts("\t----\t------------------\n");
591 env_flags_print_vartypes();
592 puts("\n");
593
267541f7
JH
594 /* Print the available variable access types */
595 printf("Available variable access flags (position %d):\n",
596 ENV_FLAGS_VARACCESS_LOC);
597 puts("\tFlag\tVariable Access Name\n");
598 puts("\t----\t--------------------\n");
599 env_flags_print_varaccess();
600 puts("\n");
601
fffad71b
JH
602 /* Print the static flags that may exist */
603 puts("Static flags:\n");
267541f7
JH
604 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
605 "Variable Access");
606 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
607 "---------------");
cca98fd6 608 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
fffad71b
JH
609 puts("\n");
610
611 /* walk through each variable and print the flags if non-default */
612 puts("Active flags:\n");
267541f7
JH
613 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
614 "Variable Access");
615 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
616 "---------------");
fffad71b
JH
617 hwalk_r(&env_htab, print_active_flags);
618 return 0;
619}
620#endif
621
ea882baf 622/*
246c6922
PT
623 * Interactively edit an environment variable
624 */
625#if defined(CONFIG_CMD_EDITENV)
09140113
SG
626static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc,
627 char *const argv[])
246c6922
PT
628{
629 char buffer[CONFIG_SYS_CBSIZE];
630 char *init_val;
246c6922 631
47e26b1b 632 if (argc < 2)
4c12eeb8 633 return CMD_RET_USAGE;
246c6922 634
94b467b1
JH
635 /* before import into hashtable */
636 if (!(gd->flags & GD_FLG_ENV_READY))
637 return 1;
638
246c6922 639 /* Set read buffer to initial value or empty sting */
00caae6d 640 init_val = env_get(argv[1]);
246c6922 641 if (init_val)
5d49b4cd 642 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
246c6922
PT
643 else
644 buffer[0] = '\0';
645
e1bf824d 646 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
18a3cce9 647 return 1;
246c6922 648
94b467b1
JH
649 if (buffer[0] == '\0') {
650 const char * const _argv[3] = { "setenv", argv[1], NULL };
651
652 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
653 } else {
654 const char * const _argv[4] = { "setenv", argv[1], buffer,
655 NULL };
656
657 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
658 }
246c6922
PT
659}
660#endif /* CONFIG_CMD_EDITENV */
7ac2fe2d 661#endif /* CONFIG_SPL_BUILD */
246c6922 662
ea882baf 663/*
a68d3ed0
WD
664 * Look up variable from environment,
665 * return address of storage for that variable,
666 * or NULL if not found
667 */
00caae6d 668char *env_get(const char *name)
a68d3ed0 669{
d09b1787 670 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
dd2408ca 671 struct env_entry e, *ep;
a68d3ed0 672
91a76751 673 WATCHDOG_RESET();
2a3cb020 674
d09b1787
IG
675 e.key = name;
676 e.data = NULL;
3f0d6807 677 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
91a76751 678
f3c615b8 679 return ep ? ep->data : NULL;
a68d3ed0
WD
680 }
681
ea882baf 682 /* restricted capabilities before import */
00caae6d 683 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
ea882baf 684 return (char *)(gd->env_buf);
91a76751 685
ea882baf 686 return NULL;
a68d3ed0
WD
687}
688
1ac2cb97
PC
689/*
690 * Like env_get, but prints an error if envvar isn't defined in the
691 * environment. It always returns what env_get does, so it can be used in
692 * place of env_get without changing error handling otherwise.
693 */
694char *from_env(const char *envvar)
695{
696 char *ret;
697
698 ret = env_get(envvar);
699
700 if (!ret)
701 printf("missing environment variable: %s\n", envvar);
702
703 return ret;
704}
705
ea882baf
WD
706/*
707 * Look up variable from environment for restricted C runtime env.
708 */
00caae6d 709int env_get_f(const char *name, char *buf, unsigned len)
a68d3ed0 710{
87c7fb39 711 int i, nxt, c;
a68d3ed0 712
d09b1787 713 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
a68d3ed0
WD
714 int val, n;
715
87c7fb39
SG
716 for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
717 if (c < 0)
718 return c;
f3c615b8
ML
719 if (nxt >= CONFIG_ENV_SIZE)
720 return -1;
a68d3ed0 721 }
f3c615b8 722
b9ca02c2 723 val = env_match((uchar *)name, i);
f3c615b8 724 if (val < 0)
a68d3ed0 725 continue;
9ed4a958 726
a68d3ed0 727 /* found; copy out */
f3c615b8 728 for (n = 0; n < len; ++n, ++buf) {
87c7fb39
SG
729 c = env_get_char(val++);
730 if (c < 0)
731 return c;
732 *buf = c;
d09b1787 733 if (*buf == '\0')
9ed4a958
WD
734 return n;
735 }
736
737 if (n)
738 *--buf = '\0';
739
82919517
HS
740 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
741 len, name);
9ed4a958
WD
742
743 return n;
a68d3ed0 744 }
d09b1787 745
f3c615b8 746 return -1;
a68d3ed0
WD
747}
748
4a9b4131
SG
749/**
750 * Decode the integer value of an environment variable and return it.
751 *
919d25c9 752 * @param name Name of environment variable
4a9b4131
SG
753 * @param base Number base to use (normally 10, or 16 for hex)
754 * @param default_val Default value to return if the variable is not
755 * found
756 * @return the decoded value, or default_val if not found
757 */
bfebc8c9 758ulong env_get_ulong(const char *name, int base, ulong default_val)
4a9b4131
SG
759{
760 /*
00caae6d 761 * We can use env_get() here, even before relocation, since the
4a9b4131
SG
762 * environment variable value is an integer and thus short.
763 */
00caae6d 764 const char *str = env_get(name);
4a9b4131
SG
765
766 return str ? simple_strtoul(str, NULL, base) : default_val;
767}
768
7ac2fe2d 769#ifndef CONFIG_SPL_BUILD
2643c85d 770#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
09140113
SG
771static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc,
772 char *const argv[])
a68d3ed0 773{
01510091 774 return env_save() ? 1 : 0;
a68d3ed0 775}
8bde7f77 776
ba69dc26 777U_BOOT_CMD(
ea882baf 778 saveenv, 1, 0, do_env_save,
2fb2604d 779 "save environment variables to persistent storage",
a89c33db 780 ""
ba69dc26 781);
cd121bdb
FW
782
783#if defined(CONFIG_CMD_ERASEENV)
09140113
SG
784static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc,
785 char *const argv[])
cd121bdb
FW
786{
787 return env_erase() ? 1 : 0;
788}
789
790U_BOOT_CMD(
791 eraseenv, 1, 0, do_env_erase,
792 "erase environment variables from persistent storage",
793 ""
794);
795#endif
a68d3ed0 796#endif
0115dd3a
PD
797
798#if defined(CONFIG_CMD_NVEDIT_LOAD)
799static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
800 char *const argv[])
801{
802 return env_reload() ? 1 : 0;
803}
804#endif
a97d22eb
PD
805
806#if defined(CONFIG_CMD_NVEDIT_SELECT)
807static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
808 char *const argv[])
809{
810 return env_select(argv[1]) ? 1 : 0;
811}
812#endif
813
7ac2fe2d 814#endif /* CONFIG_SPL_BUILD */
a68d3ed0 815
b9ca02c2 816int env_match(uchar *s1, int i2)
a68d3ed0 817{
586197df
JH
818 if (s1 == NULL)
819 return -1;
820
a68d3ed0
WD
821 while (*s1 == env_get_char(i2++))
822 if (*s1++ == '=')
f3c615b8 823 return i2;
d09b1787 824
a68d3ed0 825 if (*s1 == '\0' && env_get_char(i2-1) == '=')
f3c615b8 826 return i2;
d09b1787 827
f3c615b8 828 return -1;
a68d3ed0 829}
8bde7f77 830
7ac2fe2d 831#ifndef CONFIG_SPL_BUILD
09140113
SG
832static int do_env_default(struct cmd_tbl *cmdtp, int flag,
833 int argc, char *const argv[])
ea882baf 834{
5a04264e 835 int all = 0, env_flag = H_INTERACTIVE;
f3c615b8 836
b64b7c3d
GF
837 debug("Initial value for argc=%d\n", argc);
838 while (--argc > 0 && **++argv == '-') {
839 char *arg = *argv;
840
841 while (*++arg) {
842 switch (*arg) {
843 case 'a': /* default all */
844 all = 1;
845 break;
846 case 'f': /* force */
30091494 847 env_flag |= H_FORCE;
b64b7c3d
GF
848 break;
849 default:
850 return cmd_usage(cmdtp);
851 }
852 }
853 }
854 debug("Final value for argc=%d\n", argc);
855 if (all && (argc == 0)) {
856 /* Reset the whole environment */
0ac7d722 857 env_set_default("## Resetting to default environment\n",
c5d548a9 858 env_flag);
b64b7c3d
GF
859 return 0;
860 }
861 if (!all && (argc > 0)) {
862 /* Reset individual variables */
0b9d8a05 863 env_set_default_vars(argc, argv, env_flag);
b64b7c3d
GF
864 return 0;
865 }
866
867 return cmd_usage(cmdtp);
ea882baf
WD
868}
869
09140113
SG
870static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
871 int argc, char *const argv[])
ea882baf 872{
9d8d661d
JH
873 int env_flag = H_INTERACTIVE;
874 int ret = 0;
875
876 debug("Initial value for argc=%d\n", argc);
877 while (argc > 1 && **(argv + 1) == '-') {
878 char *arg = *++argv;
879
880 --argc;
881 while (*++arg) {
882 switch (*arg) {
883 case 'f': /* force */
884 env_flag |= H_FORCE;
885 break;
886 default:
887 return CMD_RET_USAGE;
888 }
889 }
890 }
891 debug("Final value for argc=%d\n", argc);
892
893 env_id++;
894
895 while (--argc > 0) {
896 char *name = *++argv;
897
898 if (!hdelete_r(name, &env_htab, env_flag))
899 ret = 1;
900 }
901
902 return ret;
ea882baf
WD
903}
904
0c79cda0 905#ifdef CONFIG_CMD_EXPORTENV
ea882baf 906/*
37f2fe74 907 * env export [-t | -b | -c] [-s size] addr [var ...]
ea882baf
WD
908 * -t: export as text format; if size is given, data will be
909 * padded with '\0' bytes; if not, one terminating '\0'
910 * will be added (which is included in the "filesize"
911 * setting so you can for exmple copy this to flash and
912 * keep the termination).
913 * -b: export as binary format (name=value pairs separated by
914 * '\0', list end marked by double "\0\0")
915 * -c: export as checksum protected environment format as
916 * used for example by "saveenv" command
37f2fe74
WD
917 * -s size:
918 * size of output buffer
ea882baf 919 * addr: memory address where environment gets stored
37f2fe74
WD
920 * var... List of variable names that get included into the
921 * export. Without arguments, the whole environment gets
922 * exported.
ea882baf
WD
923 *
924 * With "-c" and size is NOT given, then the export command will
925 * format the data as currently used for the persistent storage,
926 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
fc0b5948 927 * prepend a valid CRC32 checksum and, in case of redundant
ea882baf
WD
928 * environment, a "current" redundancy flag. If size is given, this
929 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
930 * checksum and redundancy flag will be inserted.
931 *
932 * With "-b" and "-t", always only the real data (including a
933 * terminating '\0' byte) will be written; here the optional size
934 * argument will be used to make sure not to overflow the user
935 * provided buffer; the command will abort if the size is not
fc0b5948 936 * sufficient. Any remaining space will be '\0' padded.
ea882baf
WD
937 *
938 * On successful return, the variable "filesize" will be set.
939 * Note that filesize includes the trailing/terminating '\0' byte(s).
940 *
fc0b5948 941 * Usage scenario: create a text snapshot/backup of the current settings:
ea882baf
WD
942 *
943 * => env export -t 100000
944 * => era ${backup_addr} +${filesize}
945 * => cp.b 100000 ${backup_addr} ${filesize}
946 *
947 * Re-import this snapshot, deleting all other settings:
948 *
949 * => env import -d -t ${backup_addr}
950 */
09140113
SG
951static int do_env_export(struct cmd_tbl *cmdtp, int flag,
952 int argc, char *const argv[])
ea882baf
WD
953{
954 char buf[32];
fd37dac9
SG
955 ulong addr;
956 char *ptr, *cmd, *res;
37f2fe74 957 size_t size = 0;
ea882baf
WD
958 ssize_t len;
959 env_t *envp;
960 char sep = '\n';
961 int chk = 0;
962 int fmt = 0;
963
964 cmd = *argv;
965
966 while (--argc > 0 && **++argv == '-') {
967 char *arg = *argv;
968 while (*++arg) {
969 switch (*arg) {
970 case 'b': /* raw binary format */
971 if (fmt++)
972 goto sep_err;
973 sep = '\0';
974 break;
975 case 'c': /* external checksum format */
976 if (fmt++)
977 goto sep_err;
978 sep = '\0';
979 chk = 1;
980 break;
37f2fe74
WD
981 case 's': /* size given */
982 if (--argc <= 0)
983 return cmd_usage(cmdtp);
984 size = simple_strtoul(*++argv, NULL, 16);
985 goto NXTARG;
ea882baf
WD
986 case 't': /* text format */
987 if (fmt++)
988 goto sep_err;
989 sep = '\n';
990 break;
991 default:
4c12eeb8 992 return CMD_RET_USAGE;
ea882baf
WD
993 }
994 }
37f2fe74 995NXTARG: ;
ea882baf
WD
996 }
997
f3c615b8 998 if (argc < 1)
4c12eeb8 999 return CMD_RET_USAGE;
8bde7f77 1000
fd37dac9
SG
1001 addr = simple_strtoul(argv[0], NULL, 16);
1002 ptr = map_sysmem(addr, size);
ea882baf 1003
37f2fe74 1004 if (size)
fd37dac9 1005 memset(ptr, '\0', size);
37f2fe74
WD
1006
1007 argc--;
1008 argv++;
ea882baf
WD
1009
1010 if (sep) { /* export as text file */
ea009d47
WD
1011 len = hexport_r(&env_htab, sep,
1012 H_MATCH_KEY | H_MATCH_IDENT,
fd37dac9 1013 &ptr, size, argc, argv);
ea882baf 1014 if (len < 0) {
6c90f623
QS
1015 pr_err("## Error: Cannot export environment: errno = %d\n",
1016 errno);
ea882baf
WD
1017 return 1;
1018 }
8c3aff52 1019 sprintf(buf, "%zX", (size_t)len);
382bee57 1020 env_set("filesize", buf);
ea882baf
WD
1021
1022 return 0;
1023 }
1024
fd37dac9 1025 envp = (env_t *)ptr;
ea882baf
WD
1026
1027 if (chk) /* export as checksum protected block */
1028 res = (char *)envp->data;
1029 else /* export as raw binary data */
fd37dac9 1030 res = ptr;
ea882baf 1031
ea009d47
WD
1032 len = hexport_r(&env_htab, '\0',
1033 H_MATCH_KEY | H_MATCH_IDENT,
1034 &res, ENV_SIZE, argc, argv);
ea882baf 1035 if (len < 0) {
6c90f623
QS
1036 pr_err("## Error: Cannot export environment: errno = %d\n",
1037 errno);
ea882baf
WD
1038 return 1;
1039 }
1040
1041 if (chk) {
d71b029d
NS
1042 envp->crc = crc32(0, envp->data,
1043 size ? size - offsetof(env_t, data) : ENV_SIZE);
ea882baf 1044#ifdef CONFIG_ENV_ADDR_REDUND
d3716dd6 1045 envp->flags = ENV_REDUND_ACTIVE;
ea882baf
WD
1046#endif
1047 }
018f5303 1048 env_set_hex("filesize", len + offsetof(env_t, data));
ea882baf
WD
1049
1050 return 0;
1051
1052sep_err:
6c90f623
QS
1053 printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1054 cmd);
ea882baf
WD
1055 return 1;
1056}
0c79cda0 1057#endif
ea882baf 1058
0c79cda0 1059#ifdef CONFIG_CMD_IMPORTENV
ea882baf 1060/*
eaf73472
QS
1061 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
1062 * -d: delete existing environment before importing if no var is
1063 * passed; if vars are passed, if one var is in the current
1064 * environment but not in the environment at addr, delete var from
1065 * current environment;
fc0b5948 1066 * otherwise overwrite / append to existing definitions
ea882baf
WD
1067 * -t: assume text format; either "size" must be given or the
1068 * text data must be '\0' terminated
ecd1446f
AH
1069 * -r: handle CRLF like LF, that means exported variables with
1070 * a content which ends with \r won't get imported. Used
1071 * to import text files created with editors which are using CRLF
1072 * for line endings. Only effective in addition to -t.
ea882baf
WD
1073 * -b: assume binary format ('\0' separated, "\0\0" terminated)
1074 * -c: assume checksum protected environment format
1075 * addr: memory address to read from
1076 * size: length of input data; if missing, proper '\0'
1077 * termination is mandatory
eaf73472
QS
1078 * if var is set and size should be missing (i.e. '\0'
1079 * termination), set size to '-'
1080 * var... List of the names of the only variables that get imported from
1081 * the environment at address 'addr'. Without arguments, the whole
1082 * environment gets imported.
ea882baf 1083 */
09140113
SG
1084static int do_env_import(struct cmd_tbl *cmdtp, int flag,
1085 int argc, char *const argv[])
ea882baf 1086{
fd37dac9
SG
1087 ulong addr;
1088 char *cmd, *ptr;
ea882baf
WD
1089 char sep = '\n';
1090 int chk = 0;
1091 int fmt = 0;
1092 int del = 0;
ecd1446f 1093 int crlf_is_lf = 0;
eaf73472 1094 int wl = 0;
ea882baf
WD
1095 size_t size;
1096
1097 cmd = *argv;
1098
1099 while (--argc > 0 && **++argv == '-') {
1100 char *arg = *argv;
1101 while (*++arg) {
1102 switch (*arg) {
1103 case 'b': /* raw binary format */
1104 if (fmt++)
1105 goto sep_err;
1106 sep = '\0';
1107 break;
1108 case 'c': /* external checksum format */
1109 if (fmt++)
1110 goto sep_err;
1111 sep = '\0';
1112 chk = 1;
1113 break;
1114 case 't': /* text format */
1115 if (fmt++)
1116 goto sep_err;
1117 sep = '\n';
1118 break;
ecd1446f
AH
1119 case 'r': /* handle CRLF like LF */
1120 crlf_is_lf = 1;
1121 break;
ea882baf
WD
1122 case 'd':
1123 del = 1;
1124 break;
1125 default:
4c12eeb8 1126 return CMD_RET_USAGE;
ea882baf
WD
1127 }
1128 }
1129 }
1130
f3c615b8 1131 if (argc < 1)
4c12eeb8 1132 return CMD_RET_USAGE;
ea882baf
WD
1133
1134 if (!fmt)
1135 printf("## Warning: defaulting to text format\n");
1136
ecd1446f
AH
1137 if (sep != '\n' && crlf_is_lf )
1138 crlf_is_lf = 0;
1139
fd37dac9
SG
1140 addr = simple_strtoul(argv[0], NULL, 16);
1141 ptr = map_sysmem(addr, 0);
ea882baf 1142
eaf73472 1143 if (argc >= 2 && strcmp(argv[1], "-")) {
ea882baf 1144 size = simple_strtoul(argv[1], NULL, 16);
eaf73472 1145 } else if (chk) {
3775dcd9
TR
1146 puts("## Error: external checksum format must pass size\n");
1147 return CMD_RET_FAILURE;
ea882baf 1148 } else {
fd37dac9 1149 char *s = ptr;
ea882baf
WD
1150
1151 size = 0;
1152
1153 while (size < MAX_ENV_SIZE) {
1154 if ((*s == sep) && (*(s+1) == '\0'))
1155 break;
1156 ++s;
1157 ++size;
1158 }
1159 if (size == MAX_ENV_SIZE) {
1160 printf("## Warning: Input data exceeds %d bytes"
1161 " - truncated\n", MAX_ENV_SIZE);
1162 }
d3f80c77 1163 size += 2;
79afc88d 1164 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
ea882baf
WD
1165 }
1166
eaf73472
QS
1167 if (argc > 2)
1168 wl = 1;
1169
ea882baf
WD
1170 if (chk) {
1171 uint32_t crc;
fd37dac9 1172 env_t *ep = (env_t *)ptr;
ea882baf
WD
1173
1174 size -= offsetof(env_t, data);
1175 memcpy(&crc, &ep->crc, sizeof(crc));
1176
1177 if (crc32(0, ep->data, size) != crc) {
1178 puts("## Error: bad CRC, import failed\n");
1179 return 1;
1180 }
fd37dac9 1181 ptr = (char *)ep->data;
ea882baf
WD
1182 }
1183
eaf73472
QS
1184 if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1185 crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
6c90f623
QS
1186 pr_err("## Error: Environment import failed: errno = %d\n",
1187 errno);
ea882baf
WD
1188 return 1;
1189 }
1190 gd->flags |= GD_FLG_ENV_READY;
1191
1192 return 0;
1193
1194sep_err:
1195 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1196 cmd);
1197 return 1;
1198}
0c79cda0 1199#endif
ea882baf 1200
8e92120b
LR
1201#if defined(CONFIG_CMD_NVEDIT_INFO)
1202/*
1203 * print_env_info - print environment information
1204 */
1205static int print_env_info(void)
1206{
1207 const char *value;
1208
1209 /* print environment validity value */
1210 switch (gd->env_valid) {
1211 case ENV_INVALID:
1212 value = "invalid";
1213 break;
1214 case ENV_VALID:
1215 value = "valid";
1216 break;
1217 case ENV_REDUND:
1218 value = "redundant";
1219 break;
1220 default:
1221 value = "unknown";
1222 break;
1223 }
1224 printf("env_valid = %s\n", value);
1225
1226 /* print environment ready flag */
1227 value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
1228 printf("env_ready = %s\n", value);
1229
1230 /* print environment using default flag */
1231 value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
1232 printf("env_use_default = %s\n", value);
1233
1234 return CMD_RET_SUCCESS;
1235}
1236
1237#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
1238#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
1239
1240/*
1241 * env info - display environment information
1242 * env info [-d] - evaluate whether default environment is used
1243 * env info [-p] - evaluate whether environment can be persisted
6718ebd0
PD
1244 * Add [-q] - quiet mode, use only for command result, for test by example:
1245 * test env info -p -d -q
8e92120b 1246 */
09140113
SG
1247static int do_env_info(struct cmd_tbl *cmdtp, int flag,
1248 int argc, char *const argv[])
8e92120b
LR
1249{
1250 int eval_flags = 0;
1251 int eval_results = 0;
6718ebd0 1252 bool quiet = false;
2f96b323
PD
1253#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1254 enum env_location loc;
1255#endif
8e92120b
LR
1256
1257 /* display environment information */
1258 if (argc <= 1)
1259 return print_env_info();
1260
1261 /* process options */
1262 while (--argc > 0 && **++argv == '-') {
1263 char *arg = *argv;
1264
1265 while (*++arg) {
1266 switch (*arg) {
1267 case 'd':
1268 eval_flags |= ENV_INFO_IS_DEFAULT;
1269 break;
1270 case 'p':
1271 eval_flags |= ENV_INFO_IS_PERSISTED;
1272 break;
6718ebd0
PD
1273 case 'q':
1274 quiet = true;
1275 break;
8e92120b
LR
1276 default:
1277 return CMD_RET_USAGE;
1278 }
1279 }
1280 }
1281
1282 /* evaluate whether default environment is used */
1283 if (eval_flags & ENV_INFO_IS_DEFAULT) {
1284 if (gd->flags & GD_FLG_ENV_DEFAULT) {
6718ebd0
PD
1285 if (!quiet)
1286 printf("Default environment is used\n");
8e92120b
LR
1287 eval_results |= ENV_INFO_IS_DEFAULT;
1288 } else {
6718ebd0
PD
1289 if (!quiet)
1290 printf("Environment was loaded from persistent storage\n");
8e92120b
LR
1291 }
1292 }
1293
1294 /* evaluate whether environment can be persisted */
1295 if (eval_flags & ENV_INFO_IS_PERSISTED) {
8b5206d9 1296#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
2f96b323
PD
1297 loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
1298 if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
1299 if (!quiet)
1300 printf("Environment can be persisted\n");
1301 eval_results |= ENV_INFO_IS_PERSISTED;
1302 } else {
1303 if (!quiet)
1304 printf("Environment cannot be persisted\n");
1305 }
8e92120b 1306#else
6718ebd0
PD
1307 if (!quiet)
1308 printf("Environment cannot be persisted\n");
8e92120b
LR
1309#endif
1310 }
1311
1312 /* The result of evaluations is combined with AND */
1313 if (eval_flags != eval_results)
1314 return CMD_RET_FAILURE;
1315
1316 return CMD_RET_SUCCESS;
1317}
1318#endif
1319
88733e2c 1320#if defined(CONFIG_CMD_ENV_EXISTS)
09140113
SG
1321static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
1322 char *const argv[])
88733e2c 1323{
dd2408ca 1324 struct env_entry e, *ep;
88733e2c
AR
1325
1326 if (argc < 2)
1327 return CMD_RET_USAGE;
1328
1329 e.key = argv[1];
1330 e.data = NULL;
3f0d6807 1331 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
88733e2c
AR
1332
1333 return (ep == NULL) ? 1 : 0;
1334}
1335#endif
1336
ea882baf
WD
1337/*
1338 * New command line interface: "env" command with subcommands
1339 */
09140113 1340static struct cmd_tbl cmd_env_sub[] = {
ea882baf
WD
1341#if defined(CONFIG_CMD_ASKENV)
1342 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1343#endif
1344 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
9d8d661d 1345 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
ea882baf
WD
1346#if defined(CONFIG_CMD_EDITENV)
1347 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1348#endif
5e2b3e0c
JH
1349#if defined(CONFIG_CMD_ENV_CALLBACK)
1350 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1351#endif
fffad71b
JH
1352#if defined(CONFIG_CMD_ENV_FLAGS)
1353 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1354#endif
0c79cda0 1355#if defined(CONFIG_CMD_EXPORTENV)
ea882baf 1356 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
0c79cda0 1357#endif
a000b795
KP
1358#if defined(CONFIG_CMD_GREPENV)
1359 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1360#endif
0c79cda0 1361#if defined(CONFIG_CMD_IMPORTENV)
ea882baf 1362 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
8e92120b
LR
1363#endif
1364#if defined(CONFIG_CMD_NVEDIT_INFO)
6718ebd0 1365 U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
0115dd3a
PD
1366#endif
1367#if defined(CONFIG_CMD_NVEDIT_LOAD)
1368 U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
0c79cda0 1369#endif
ea882baf
WD
1370 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1371#if defined(CONFIG_CMD_RUN)
1372 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1373#endif
2643c85d 1374#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
ea882baf 1375 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
cd121bdb
FW
1376#if defined(CONFIG_CMD_ERASEENV)
1377 U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
1378#endif
a97d22eb
PD
1379#endif
1380#if defined(CONFIG_CMD_NVEDIT_SELECT)
1381 U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
ea882baf
WD
1382#endif
1383 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
88733e2c
AR
1384#if defined(CONFIG_CMD_ENV_EXISTS)
1385 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1386#endif
ea882baf
WD
1387};
1388
2e5167cc 1389#if defined(CONFIG_NEEDS_MANUAL_RELOC)
60f7da1f
HS
1390void env_reloc(void)
1391{
1392 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1393}
1394#endif
1395
09140113 1396static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
ea882baf 1397{
09140113 1398 struct cmd_tbl *cp;
ea882baf 1399
5904da02 1400 if (argc < 2)
4c12eeb8 1401 return CMD_RET_USAGE;
5904da02 1402
ea882baf
WD
1403 /* drop initial "env" arg */
1404 argc--;
1405 argv++;
1406
1407 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1408
1409 if (cp)
1410 return cp->cmd(cmdtp, flag, argc, argv);
1411
4c12eeb8 1412 return CMD_RET_USAGE;
ea882baf
WD
1413}
1414
088f1b19
KP
1415#ifdef CONFIG_SYS_LONGHELP
1416static char env_help_text[] =
ea882baf
WD
1417#if defined(CONFIG_CMD_ASKENV)
1418 "ask name [message] [size] - ask for environment variable\nenv "
5e2b3e0c
JH
1419#endif
1420#if defined(CONFIG_CMD_ENV_CALLBACK)
1421 "callbacks - print callbacks and their associated variables\nenv "
ea882baf 1422#endif
b64b7c3d
GF
1423 "default [-f] -a - [forcibly] reset default environment\n"
1424 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
9d8d661d 1425 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
ea882baf
WD
1426#if defined(CONFIG_CMD_EDITENV)
1427 "env edit name - edit environment variable\n"
1428#endif
88733e2c
AR
1429#if defined(CONFIG_CMD_ENV_EXISTS)
1430 "env exists name - tests for existence of variable\n"
1431#endif
4796bc4b 1432#if defined(CONFIG_CMD_EXPORTENV)
37f2fe74 1433 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
4796bc4b 1434#endif
fffad71b
JH
1435#if defined(CONFIG_CMD_ENV_FLAGS)
1436 "env flags - print variables that have non-default flags\n"
1437#endif
a000b795 1438#if defined(CONFIG_CMD_GREPENV)
be29df6a
WD
1439#ifdef CONFIG_REGEX
1440 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1441#else
d87244d5 1442 "env grep [-n | -v | -b] string [...] - search environment\n"
a000b795 1443#endif
be29df6a 1444#endif
4796bc4b 1445#if defined(CONFIG_CMD_IMPORTENV)
eaf73472 1446 "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
8e92120b
LR
1447#endif
1448#if defined(CONFIG_CMD_NVEDIT_INFO)
1449 "env info - display environment information\n"
6718ebd0
PD
1450 "env info [-d] [-p] [-q] - evaluate environment information\n"
1451 " \"-d\": default environment is used\n"
1452 " \"-p\": environment can be persisted\n"
1453 " \"-q\": quiet output\n"
4796bc4b 1454#endif
be11235a 1455 "env print [-a | name ...] - print environment\n"
49d81fdf 1456#if defined(CONFIG_CMD_NVEDIT_EFI)
c70f4481 1457 "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
49d81fdf 1458#endif
ea882baf
WD
1459#if defined(CONFIG_CMD_RUN)
1460 "env run var [...] - run commands in an environment variable\n"
1461#endif
2643c85d 1462#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
ea882baf 1463 "env save - save environment\n"
cd121bdb
FW
1464#if defined(CONFIG_CMD_ERASEENV)
1465 "env erase - erase environment\n"
1466#endif
49d81fdf 1467#endif
0115dd3a
PD
1468#if defined(CONFIG_CMD_NVEDIT_LOAD)
1469 "env load - load environment\n"
a97d22eb
PD
1470#endif
1471#if defined(CONFIG_CMD_NVEDIT_SELECT)
1472 "env select [target] - select environment target\n"
0115dd3a 1473#endif
49d81fdf 1474#if defined(CONFIG_CMD_NVEDIT_EFI)
e50e2878 1475 "env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n"
051aa89f 1476 " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
d798a9b5 1477#endif
088f1b19
KP
1478 "env set [-f] name [arg ...]\n";
1479#endif
1480
1481U_BOOT_CMD(
1482 env, CONFIG_SYS_MAXARGS, 1, do_env,
1483 "environment handling commands", env_help_text
ea882baf
WD
1484);
1485
1486/*
1487 * Old command line interface, kept for compatibility
1488 */
8bde7f77 1489
246c6922 1490#if defined(CONFIG_CMD_EDITENV)
722b061b 1491U_BOOT_CMD_COMPLETE(
ea882baf 1492 editenv, 2, 0, do_env_edit,
246c6922
PT
1493 "edit environment variable",
1494 "name\n"
722b061b
MF
1495 " - edit environment variable 'name'",
1496 var_complete
246c6922
PT
1497);
1498#endif
1499
722b061b 1500U_BOOT_CMD_COMPLETE(
ea882baf 1501 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
2fb2604d 1502 "print environment variables",
be11235a 1503 "[-a]\n - print [all] values of all environment variables\n"
49d81fdf 1504#if defined(CONFIG_CMD_NVEDIT_EFI)
c70f4481 1505 "printenv -e [-guid guid][-n] [name ...]\n"
49d81fdf 1506 " - print UEFI variable 'name' or all the variables\n"
c70f4481 1507 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
051aa89f 1508 " \"-n\": suppress dumping variable's value\n"
49d81fdf 1509#endif
8bde7f77 1510 "printenv name ...\n"
722b061b
MF
1511 " - print value of environment variable 'name'",
1512 var_complete
8bde7f77
WD
1513);
1514
a000b795
KP
1515#ifdef CONFIG_CMD_GREPENV
1516U_BOOT_CMD_COMPLETE(
1517 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1518 "search environment variables",
be29df6a
WD
1519#ifdef CONFIG_REGEX
1520 "[-e] [-n | -v | -b] string ...\n"
1521#else
d87244d5 1522 "[-n | -v | -b] string ...\n"
be29df6a 1523#endif
d87244d5 1524 " - list environment name=value pairs matching 'string'\n"
be29df6a
WD
1525#ifdef CONFIG_REGEX
1526 " \"-e\": enable regular expressions;\n"
1527#endif
d87244d5
WD
1528 " \"-n\": search variable names; \"-v\": search values;\n"
1529 " \"-b\": search both names and values (default)",
a000b795
KP
1530 var_complete
1531);
1532#endif
1533
722b061b 1534U_BOOT_CMD_COMPLETE(
ea882baf 1535 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
2fb2604d 1536 "set environment variables",
49d81fdf 1537#if defined(CONFIG_CMD_NVEDIT_EFI)
e50e2878 1538 "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
051aa89f 1539 " [-i addr,size name], or [name [value ...]]\n"
49d81fdf 1540 " - set UEFI variable 'name' to 'value' ...'\n"
c70f4481 1541 " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
051aa89f
AT
1542 " \"-nv\": set non-volatile attribute\n"
1543 " \"-bs\": set boot-service attribute\n"
1544 " \"-rt\": set runtime attribute\n"
e50e2878 1545 " \"-at\": set time-based authentication attribute\n"
051aa89f
AT
1546 " \"-a\": append-write\n"
1547 " \"-i addr,size\": use <addr,size> as variable's value\n"
1548 " \"-v\": verbose message\n"
49d81fdf
AT
1549 " - delete UEFI variable 'name' if 'value' not specified\n"
1550#endif
1551 "setenv [-f] name value ...\n"
24ab5a19
JH
1552 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1553 "setenv [-f] name\n"
1554 " - [forcibly] delete environment variable 'name'",
722b061b 1555 var_complete
8bde7f77
WD
1556);
1557
c76fe474 1558#if defined(CONFIG_CMD_ASKENV)
8bde7f77 1559
0d498393 1560U_BOOT_CMD(
ea882baf 1561 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
2fb2604d 1562 "get environment variables from stdin",
8bde7f77 1563 "name [message] [size]\n"
7d85591d 1564 " - get environment variable 'name' from stdin (max 'size' chars)"
8bde7f77 1565);
90253178 1566#endif
8bde7f77 1567
c76fe474 1568#if defined(CONFIG_CMD_RUN)
722b061b 1569U_BOOT_CMD_COMPLETE(
6d0f6bcf 1570 run, CONFIG_SYS_MAXARGS, 1, do_run,
2fb2604d 1571 "run commands in an environment variable",
8bde7f77 1572 "var [...]\n"
722b061b
MF
1573 " - run the commands in the environment variable(s) 'var'",
1574 var_complete
8bde7f77 1575);
90253178 1576#endif
7ac2fe2d 1577#endif /* CONFIG_SPL_BUILD */
This page took 0.79882 seconds and 4 git commands to generate.