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