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