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