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