xref: /rk3399_rockchip-uboot/cmd/nvedit.c (revision 6222c401243b38707fd88ccf5e4c35678c183f8d)
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 /**
521  * Set an environment variable to an integer value
522  *
523  * @param varname	Environment variable to set
524  * @param value		Value to set it to
525  * @return 0 if ok, 1 on error
526  */
527 int env_set_ulong(const char *varname, ulong value)
528 {
529 	/* TODO: this should be unsigned */
530 	char *str = simple_itoa(value);
531 
532 	return env_set(varname, str);
533 }
534 
535 /**
536  * Set an environment variable to an value in hex
537  *
538  * @param varname	Environment variable to set
539  * @param value		Value to set it to
540  * @return 0 if ok, 1 on error
541  */
542 int env_set_hex(const char *varname, ulong value)
543 {
544 	char str[17];
545 
546 	sprintf(str, "%lx", value);
547 	return env_set(varname, str);
548 }
549 
550 ulong env_get_hex(const char *varname, ulong default_val)
551 {
552 	const char *s;
553 	ulong value;
554 	char *endp;
555 
556 	s = env_get(varname);
557 	if (s)
558 		value = simple_strtoul(s, &endp, 16);
559 	if (!s || endp == s)
560 		return default_val;
561 
562 	return value;
563 }
564 
565 #ifndef CONFIG_SPL_BUILD
566 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
567 {
568 	if (argc < 2)
569 		return CMD_RET_USAGE;
570 
571 	return _do_env_set(flag, argc, argv, H_INTERACTIVE);
572 }
573 
574 /*
575  * Prompt for environment variable
576  */
577 #if defined(CONFIG_CMD_ASKENV)
578 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
579 {
580 	char message[CONFIG_SYS_CBSIZE];
581 	int i, len, pos, size;
582 	char *local_args[4];
583 	char *endptr;
584 
585 	local_args[0] = argv[0];
586 	local_args[1] = argv[1];
587 	local_args[2] = NULL;
588 	local_args[3] = NULL;
589 
590 	/*
591 	 * Check the syntax:
592 	 *
593 	 * env_ask envname [message1 ...] [size]
594 	 */
595 	if (argc == 1)
596 		return CMD_RET_USAGE;
597 
598 	/*
599 	 * We test the last argument if it can be converted
600 	 * into a decimal number.  If yes, we assume it's
601 	 * the size.  Otherwise we echo it as part of the
602 	 * message.
603 	 */
604 	i = simple_strtoul(argv[argc - 1], &endptr, 10);
605 	if (*endptr != '\0') {			/* no size */
606 		size = CONFIG_SYS_CBSIZE - 1;
607 	} else {				/* size given */
608 		size = i;
609 		--argc;
610 	}
611 
612 	if (argc <= 2) {
613 		sprintf(message, "Please enter '%s': ", argv[1]);
614 	} else {
615 		/* env_ask envname message1 ... messagen [size] */
616 		for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
617 			if (pos)
618 				message[pos++] = ' ';
619 
620 			strncpy(message + pos, argv[i], sizeof(message) - pos);
621 			pos += strlen(argv[i]);
622 		}
623 		if (pos < sizeof(message) - 1) {
624 			message[pos++] = ' ';
625 			message[pos] = '\0';
626 		} else
627 			message[CONFIG_SYS_CBSIZE - 1] = '\0';
628 	}
629 
630 	if (size >= CONFIG_SYS_CBSIZE)
631 		size = CONFIG_SYS_CBSIZE - 1;
632 
633 	if (size <= 0)
634 		return 1;
635 
636 	/* prompt for input */
637 	len = cli_readline(message);
638 
639 	if (size < len)
640 		console_buffer[size] = '\0';
641 
642 	len = 2;
643 	if (console_buffer[0] != '\0') {
644 		local_args[2] = console_buffer;
645 		len = 3;
646 	}
647 
648 	/* Continue calling setenv code */
649 	return _do_env_set(flag, len, local_args, H_INTERACTIVE);
650 }
651 #endif
652 
653 #if defined(CONFIG_CMD_ENV_CALLBACK)
654 static int print_static_binding(const char *var_name, const char *callback_name,
655 				void *priv)
656 {
657 	printf("\t%-20s %-20s\n", var_name, callback_name);
658 
659 	return 0;
660 }
661 
662 static int print_active_callback(ENTRY *entry)
663 {
664 	struct env_clbk_tbl *clbkp;
665 	int i;
666 	int num_callbacks;
667 
668 	if (entry->callback == NULL)
669 		return 0;
670 
671 	/* look up the callback in the linker-list */
672 	num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
673 	for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
674 	     i < num_callbacks;
675 	     i++, clbkp++) {
676 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
677 		if (entry->callback == clbkp->callback + gd->reloc_off)
678 #else
679 		if (entry->callback == clbkp->callback)
680 #endif
681 			break;
682 	}
683 
684 	if (i == num_callbacks)
685 		/* this should probably never happen, but just in case... */
686 		printf("\t%-20s %p\n", entry->key, entry->callback);
687 	else
688 		printf("\t%-20s %-20s\n", entry->key, clbkp->name);
689 
690 	return 0;
691 }
692 
693 /*
694  * Print the callbacks available and what they are bound to
695  */
696 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
697 {
698 	struct env_clbk_tbl *clbkp;
699 	int i;
700 	int num_callbacks;
701 
702 	/* Print the available callbacks */
703 	puts("Available callbacks:\n");
704 	puts("\tCallback Name\n");
705 	puts("\t-------------\n");
706 	num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
707 	for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
708 	     i < num_callbacks;
709 	     i++, clbkp++)
710 		printf("\t%s\n", clbkp->name);
711 	puts("\n");
712 
713 	/* Print the static bindings that may exist */
714 	puts("Static callback bindings:\n");
715 	printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
716 	printf("\t%-20s %-20s\n", "-------------", "-------------");
717 	env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
718 	puts("\n");
719 
720 	/* walk through each variable and print the callback if it has one */
721 	puts("Active callback bindings:\n");
722 	printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
723 	printf("\t%-20s %-20s\n", "-------------", "-------------");
724 	hwalk_r(&env_htab, print_active_callback);
725 	return 0;
726 }
727 #endif
728 
729 #if defined(CONFIG_CMD_ENV_FLAGS)
730 static int print_static_flags(const char *var_name, const char *flags,
731 			      void *priv)
732 {
733 	enum env_flags_vartype type = env_flags_parse_vartype(flags);
734 	enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
735 
736 	printf("\t%-20s %-20s %-20s\n", var_name,
737 		env_flags_get_vartype_name(type),
738 		env_flags_get_varaccess_name(access));
739 
740 	return 0;
741 }
742 
743 static int print_active_flags(ENTRY *entry)
744 {
745 	enum env_flags_vartype type;
746 	enum env_flags_varaccess access;
747 
748 	if (entry->flags == 0)
749 		return 0;
750 
751 	type = (enum env_flags_vartype)
752 		(entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
753 	access = env_flags_parse_varaccess_from_binflags(entry->flags);
754 	printf("\t%-20s %-20s %-20s\n", entry->key,
755 		env_flags_get_vartype_name(type),
756 		env_flags_get_varaccess_name(access));
757 
758 	return 0;
759 }
760 
761 /*
762  * Print the flags available and what variables have flags
763  */
764 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
765 {
766 	/* Print the available variable types */
767 	printf("Available variable type flags (position %d):\n",
768 		ENV_FLAGS_VARTYPE_LOC);
769 	puts("\tFlag\tVariable Type Name\n");
770 	puts("\t----\t------------------\n");
771 	env_flags_print_vartypes();
772 	puts("\n");
773 
774 	/* Print the available variable access types */
775 	printf("Available variable access flags (position %d):\n",
776 		ENV_FLAGS_VARACCESS_LOC);
777 	puts("\tFlag\tVariable Access Name\n");
778 	puts("\t----\t--------------------\n");
779 	env_flags_print_varaccess();
780 	puts("\n");
781 
782 	/* Print the static flags that may exist */
783 	puts("Static flags:\n");
784 	printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
785 		"Variable Access");
786 	printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
787 		"---------------");
788 	env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
789 	puts("\n");
790 
791 	/* walk through each variable and print the flags if non-default */
792 	puts("Active flags:\n");
793 	printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
794 		"Variable Access");
795 	printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
796 		"---------------");
797 	hwalk_r(&env_htab, print_active_flags);
798 	return 0;
799 }
800 #endif
801 
802 /*
803  * Interactively edit an environment variable
804  */
805 #if defined(CONFIG_CMD_EDITENV)
806 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
807 		       char * const argv[])
808 {
809 	char buffer[CONFIG_SYS_CBSIZE];
810 	char *init_val;
811 
812 	if (argc < 2)
813 		return CMD_RET_USAGE;
814 
815 	/* before import into hashtable */
816 	if (!(gd->flags & GD_FLG_ENV_READY))
817 		return 1;
818 
819 	/* Set read buffer to initial value or empty sting */
820 	init_val = env_get(argv[1]);
821 	if (init_val)
822 		snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
823 	else
824 		buffer[0] = '\0';
825 
826 	if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
827 		return 1;
828 
829 	if (buffer[0] == '\0') {
830 		const char * const _argv[3] = { "setenv", argv[1], NULL };
831 
832 		return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
833 	} else {
834 		const char * const _argv[4] = { "setenv", argv[1], buffer,
835 			NULL };
836 
837 		return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
838 	}
839 }
840 #endif /* CONFIG_CMD_EDITENV */
841 #endif /* CONFIG_SPL_BUILD */
842 
843 /*
844  * Look up variable from environment,
845  * return address of storage for that variable,
846  * or NULL if not found
847  */
848 char *env_get(const char *name)
849 {
850 	if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
851 		ENTRY e, *ep;
852 
853 		WATCHDOG_RESET();
854 
855 		e.key	= name;
856 		e.data	= NULL;
857 		hsearch_r(e, FIND, &ep, &env_htab, 0);
858 
859 		return ep ? ep->data : NULL;
860 	}
861 
862 	/* restricted capabilities before import */
863 	if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
864 		return (char *)(gd->env_buf);
865 
866 	return NULL;
867 }
868 
869 /*
870  * Look up variable from environment for restricted C runtime env.
871  */
872 int env_get_f(const char *name, char *buf, unsigned len)
873 {
874 	int i, nxt;
875 
876 	for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
877 		int val, n;
878 
879 		for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
880 			if (nxt >= CONFIG_ENV_SIZE)
881 				return -1;
882 		}
883 
884 		val = envmatch((uchar *)name, i);
885 		if (val < 0)
886 			continue;
887 
888 		/* found; copy out */
889 		for (n = 0; n < len; ++n, ++buf) {
890 			*buf = env_get_char(val++);
891 			if (*buf == '\0')
892 				return n;
893 		}
894 
895 		if (n)
896 			*--buf = '\0';
897 
898 		printf("env_buf [%d bytes] too small for value of \"%s\"\n",
899 			len, name);
900 
901 		return n;
902 	}
903 
904 	return -1;
905 }
906 
907 /**
908  * Decode the integer value of an environment variable and return it.
909  *
910  * @param name		Name of environemnt variable
911  * @param base		Number base to use (normally 10, or 16 for hex)
912  * @param default_val	Default value to return if the variable is not
913  *			found
914  * @return the decoded value, or default_val if not found
915  */
916 ulong env_get_ulong(const char *name, int base, ulong default_val)
917 {
918 	/*
919 	 * We can use env_get() here, even before relocation, since the
920 	 * environment variable value is an integer and thus short.
921 	 */
922 	const char *str = env_get(name);
923 
924 	return str ? simple_strtoul(str, NULL, base) : default_val;
925 }
926 
927 #ifndef CONFIG_SPL_BUILD
928 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
929 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
930 		       char * const argv[])
931 {
932 	struct env_driver *env = env_driver_lookup_default();
933 
934 	printf("Saving Environment to %s...\n", env->name);
935 
936 	return env_save() ? 1 : 0;
937 }
938 
939 U_BOOT_CMD(
940 	saveenv, 1, 0,	do_env_save,
941 	"save environment variables to persistent storage",
942 	""
943 );
944 #endif
945 #endif /* CONFIG_SPL_BUILD */
946 
947 
948 /*
949  * Match a name / name=value pair
950  *
951  * s1 is either a simple 'name', or a 'name=value' pair.
952  * i2 is the environment index for a 'name2=value2' pair.
953  * If the names match, return the index for the value2, else -1.
954  */
955 int envmatch(uchar *s1, int i2)
956 {
957 	if (s1 == NULL)
958 		return -1;
959 
960 	while (*s1 == env_get_char(i2++))
961 		if (*s1++ == '=')
962 			return i2;
963 
964 	if (*s1 == '\0' && env_get_char(i2-1) == '=')
965 		return i2;
966 
967 	return -1;
968 }
969 
970 #ifndef CONFIG_SPL_BUILD
971 static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
972 			  int argc, char * const argv[])
973 {
974 	int all = 0, flag = 0;
975 
976 	debug("Initial value for argc=%d\n", argc);
977 	while (--argc > 0 && **++argv == '-') {
978 		char *arg = *argv;
979 
980 		while (*++arg) {
981 			switch (*arg) {
982 			case 'a':		/* default all */
983 				all = 1;
984 				break;
985 			case 'f':		/* force */
986 				flag |= H_FORCE;
987 				break;
988 			default:
989 				return cmd_usage(cmdtp);
990 			}
991 		}
992 	}
993 	debug("Final value for argc=%d\n", argc);
994 	if (all && (argc == 0)) {
995 		/* Reset the whole environment */
996 		set_default_env("## Resetting to default environment\n");
997 		return 0;
998 	}
999 	if (!all && (argc > 0)) {
1000 		/* Reset individual variables */
1001 		set_default_vars(argc, argv);
1002 		return 0;
1003 	}
1004 
1005 	return cmd_usage(cmdtp);
1006 }
1007 
1008 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
1009 			 int argc, char * const argv[])
1010 {
1011 	int env_flag = H_INTERACTIVE;
1012 	int ret = 0;
1013 
1014 	debug("Initial value for argc=%d\n", argc);
1015 	while (argc > 1 && **(argv + 1) == '-') {
1016 		char *arg = *++argv;
1017 
1018 		--argc;
1019 		while (*++arg) {
1020 			switch (*arg) {
1021 			case 'f':		/* force */
1022 				env_flag |= H_FORCE;
1023 				break;
1024 			default:
1025 				return CMD_RET_USAGE;
1026 			}
1027 		}
1028 	}
1029 	debug("Final value for argc=%d\n", argc);
1030 
1031 	env_id++;
1032 
1033 	while (--argc > 0) {
1034 		char *name = *++argv;
1035 
1036 		if (!hdelete_r(name, &env_htab, env_flag))
1037 			ret = 1;
1038 	}
1039 
1040 	return ret;
1041 }
1042 
1043 #ifdef CONFIG_CMD_EXPORTENV
1044 /*
1045  * env export [-t | -b | -c] [-s size] addr [var ...]
1046  *	-t:	export as text format; if size is given, data will be
1047  *		padded with '\0' bytes; if not, one terminating '\0'
1048  *		will be added (which is included in the "filesize"
1049  *		setting so you can for exmple copy this to flash and
1050  *		keep the termination).
1051  *	-b:	export as binary format (name=value pairs separated by
1052  *		'\0', list end marked by double "\0\0")
1053  *	-c:	export as checksum protected environment format as
1054  *		used for example by "saveenv" command
1055  *	-s size:
1056  *		size of output buffer
1057  *	addr:	memory address where environment gets stored
1058  *	var...	List of variable names that get included into the
1059  *		export. Without arguments, the whole environment gets
1060  *		exported.
1061  *
1062  * With "-c" and size is NOT given, then the export command will
1063  * format the data as currently used for the persistent storage,
1064  * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
1065  * prepend a valid CRC32 checksum and, in case of redundant
1066  * environment, a "current" redundancy flag. If size is given, this
1067  * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
1068  * checksum and redundancy flag will be inserted.
1069  *
1070  * With "-b" and "-t", always only the real data (including a
1071  * terminating '\0' byte) will be written; here the optional size
1072  * argument will be used to make sure not to overflow the user
1073  * provided buffer; the command will abort if the size is not
1074  * sufficient. Any remaining space will be '\0' padded.
1075  *
1076  * On successful return, the variable "filesize" will be set.
1077  * Note that filesize includes the trailing/terminating '\0' byte(s).
1078  *
1079  * Usage scenario:  create a text snapshot/backup of the current settings:
1080  *
1081  *	=> env export -t 100000
1082  *	=> era ${backup_addr} +${filesize}
1083  *	=> cp.b 100000 ${backup_addr} ${filesize}
1084  *
1085  * Re-import this snapshot, deleting all other settings:
1086  *
1087  *	=> env import -d -t ${backup_addr}
1088  */
1089 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
1090 			 int argc, char * const argv[])
1091 {
1092 	char	buf[32];
1093 	ulong	addr;
1094 	char	*ptr, *cmd, *res;
1095 	size_t	size = 0;
1096 	ssize_t	len;
1097 	env_t	*envp;
1098 	char	sep = '\n';
1099 	int	chk = 0;
1100 	int	fmt = 0;
1101 
1102 	cmd = *argv;
1103 
1104 	while (--argc > 0 && **++argv == '-') {
1105 		char *arg = *argv;
1106 		while (*++arg) {
1107 			switch (*arg) {
1108 			case 'b':		/* raw binary format */
1109 				if (fmt++)
1110 					goto sep_err;
1111 				sep = '\0';
1112 				break;
1113 			case 'c':		/* external checksum format */
1114 				if (fmt++)
1115 					goto sep_err;
1116 				sep = '\0';
1117 				chk = 1;
1118 				break;
1119 			case 's':		/* size given */
1120 				if (--argc <= 0)
1121 					return cmd_usage(cmdtp);
1122 				size = simple_strtoul(*++argv, NULL, 16);
1123 				goto NXTARG;
1124 			case 't':		/* text format */
1125 				if (fmt++)
1126 					goto sep_err;
1127 				sep = '\n';
1128 				break;
1129 			default:
1130 				return CMD_RET_USAGE;
1131 			}
1132 		}
1133 NXTARG:		;
1134 	}
1135 
1136 	if (argc < 1)
1137 		return CMD_RET_USAGE;
1138 
1139 	addr = simple_strtoul(argv[0], NULL, 16);
1140 	ptr = map_sysmem(addr, size);
1141 
1142 	if (size)
1143 		memset(ptr, '\0', size);
1144 
1145 	argc--;
1146 	argv++;
1147 
1148 	if (sep) {		/* export as text file */
1149 		len = hexport_r(&env_htab, sep,
1150 				H_MATCH_KEY | H_MATCH_IDENT,
1151 				&ptr, size, argc, argv);
1152 		if (len < 0) {
1153 			pr_err("Cannot export environment: errno = %d\n", errno);
1154 			return 1;
1155 		}
1156 		sprintf(buf, "%zX", (size_t)len);
1157 		env_set("filesize", buf);
1158 
1159 		return 0;
1160 	}
1161 
1162 	envp = (env_t *)ptr;
1163 
1164 	if (chk)		/* export as checksum protected block */
1165 		res = (char *)envp->data;
1166 	else			/* export as raw binary data */
1167 		res = ptr;
1168 
1169 	len = hexport_r(&env_htab, '\0',
1170 			H_MATCH_KEY | H_MATCH_IDENT,
1171 			&res, ENV_SIZE, argc, argv);
1172 	if (len < 0) {
1173 		pr_err("Cannot export environment: errno = %d\n", errno);
1174 		return 1;
1175 	}
1176 
1177 	if (chk) {
1178 		envp->crc = crc32(0, envp->data, ENV_SIZE);
1179 #ifdef CONFIG_ENV_ADDR_REDUND
1180 		envp->flags = ACTIVE_FLAG;
1181 #endif
1182 	}
1183 	env_set_hex("filesize", len + offsetof(env_t, data));
1184 
1185 	return 0;
1186 
1187 sep_err:
1188 	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",	cmd);
1189 	return 1;
1190 }
1191 #endif
1192 
1193 #ifdef CONFIG_CMD_IMPORTENV
1194 /*
1195  * env import [-d] [-t [-r] | -b | -c] addr [size]
1196  *	-d:	delete existing environment before importing;
1197  *		otherwise overwrite / append to existing definitions
1198  *	-t:	assume text format; either "size" must be given or the
1199  *		text data must be '\0' terminated
1200  *	-r:	handle CRLF like LF, that means exported variables with
1201  *		a content which ends with \r won't get imported. Used
1202  *		to import text files created with editors which are using CRLF
1203  *		for line endings. Only effective in addition to -t.
1204  *	-b:	assume binary format ('\0' separated, "\0\0" terminated)
1205  *	-c:	assume checksum protected environment format
1206  *	addr:	memory address to read from
1207  *	size:	length of input data; if missing, proper '\0'
1208  *		termination is mandatory
1209  */
1210 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
1211 			 int argc, char * const argv[])
1212 {
1213 	ulong	addr;
1214 	char	*cmd, *ptr;
1215 	char	sep = '\n';
1216 	int	chk = 0;
1217 	int	fmt = 0;
1218 	int	del = 0;
1219 	int	crlf_is_lf = 0;
1220 	size_t	size;
1221 
1222 	cmd = *argv;
1223 
1224 	while (--argc > 0 && **++argv == '-') {
1225 		char *arg = *argv;
1226 		while (*++arg) {
1227 			switch (*arg) {
1228 			case 'b':		/* raw binary format */
1229 				if (fmt++)
1230 					goto sep_err;
1231 				sep = '\0';
1232 				break;
1233 			case 'c':		/* external checksum format */
1234 				if (fmt++)
1235 					goto sep_err;
1236 				sep = '\0';
1237 				chk = 1;
1238 				break;
1239 			case 't':		/* text format */
1240 				if (fmt++)
1241 					goto sep_err;
1242 				sep = '\n';
1243 				break;
1244 			case 'r':		/* handle CRLF like LF */
1245 				crlf_is_lf = 1;
1246 				break;
1247 			case 'd':
1248 				del = 1;
1249 				break;
1250 			default:
1251 				return CMD_RET_USAGE;
1252 			}
1253 		}
1254 	}
1255 
1256 	if (argc < 1)
1257 		return CMD_RET_USAGE;
1258 
1259 	if (!fmt)
1260 		printf("## Warning: defaulting to text format\n");
1261 
1262 	if (sep != '\n' && crlf_is_lf )
1263 		crlf_is_lf = 0;
1264 
1265 	addr = simple_strtoul(argv[0], NULL, 16);
1266 	ptr = map_sysmem(addr, 0);
1267 
1268 	if (argc == 2) {
1269 		size = simple_strtoul(argv[1], NULL, 16);
1270 	} else if (argc == 1 && chk) {
1271 		puts("## Error: external checksum format must pass size\n");
1272 		return CMD_RET_FAILURE;
1273 	} else {
1274 		char *s = ptr;
1275 
1276 		size = 0;
1277 
1278 		while (size < MAX_ENV_SIZE) {
1279 			if ((*s == sep) && (*(s+1) == '\0'))
1280 				break;
1281 			++s;
1282 			++size;
1283 		}
1284 		if (size == MAX_ENV_SIZE) {
1285 			printf("## Warning: Input data exceeds %d bytes"
1286 				" - truncated\n", MAX_ENV_SIZE);
1287 		}
1288 		size += 2;
1289 		printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1290 	}
1291 
1292 	if (chk) {
1293 		uint32_t crc;
1294 		env_t *ep = (env_t *)ptr;
1295 
1296 		size -= offsetof(env_t, data);
1297 		memcpy(&crc, &ep->crc, sizeof(crc));
1298 
1299 		if (crc32(0, ep->data, size) != crc) {
1300 			puts("## Error: bad CRC, import failed\n");
1301 			return 1;
1302 		}
1303 		ptr = (char *)ep->data;
1304 	}
1305 
1306 	if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
1307 			crlf_is_lf, 0, NULL) == 0) {
1308 		pr_err("Environment import failed: errno = %d\n", errno);
1309 		return 1;
1310 	}
1311 	gd->flags |= GD_FLG_ENV_READY;
1312 
1313 	return 0;
1314 
1315 sep_err:
1316 	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1317 		cmd);
1318 	return 1;
1319 }
1320 #endif
1321 
1322 #if defined(CONFIG_CMD_ENV_EXISTS)
1323 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1324 		       char * const argv[])
1325 {
1326 	ENTRY e, *ep;
1327 
1328 	if (argc < 2)
1329 		return CMD_RET_USAGE;
1330 
1331 	e.key = argv[1];
1332 	e.data = NULL;
1333 	hsearch_r(e, FIND, &ep, &env_htab, 0);
1334 
1335 	return (ep == NULL) ? 1 : 0;
1336 }
1337 #endif
1338 
1339 /*
1340  * New command line interface: "env" command with subcommands
1341  */
1342 static cmd_tbl_t cmd_env_sub[] = {
1343 #if defined(CONFIG_CMD_ASKENV)
1344 	U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1345 #endif
1346 	U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1347 	U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1348 #if defined(CONFIG_CMD_EDITENV)
1349 	U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1350 #endif
1351 #if defined(CONFIG_CMD_ENV_CALLBACK)
1352 	U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1353 #endif
1354 #if defined(CONFIG_CMD_ENV_FLAGS)
1355 	U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1356 #endif
1357 #if defined(CONFIG_CMD_EXPORTENV)
1358 	U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1359 #endif
1360 #if defined(CONFIG_CMD_GREPENV)
1361 	U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1362 #endif
1363 #if defined(CONFIG_CMD_IMPORTENV)
1364 	U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1365 #endif
1366 	U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1367 #if defined(CONFIG_CMD_RUN)
1368 	U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1369 #endif
1370 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1371 	U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1372 #endif
1373 	U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1374 #if defined(CONFIG_CMD_ENV_EXISTS)
1375 	U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1376 #endif
1377 };
1378 
1379 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1380 void env_reloc(void)
1381 {
1382 	fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1383 }
1384 #endif
1385 
1386 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1387 {
1388 	cmd_tbl_t *cp;
1389 
1390 	if (argc < 2)
1391 		return CMD_RET_USAGE;
1392 
1393 	/* drop initial "env" arg */
1394 	argc--;
1395 	argv++;
1396 
1397 	cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1398 
1399 	if (cp)
1400 		return cp->cmd(cmdtp, flag, argc, argv);
1401 
1402 	return CMD_RET_USAGE;
1403 }
1404 
1405 #ifdef CONFIG_SYS_LONGHELP
1406 static char env_help_text[] =
1407 #if defined(CONFIG_CMD_ASKENV)
1408 	"ask name [message] [size] - ask for environment variable\nenv "
1409 #endif
1410 #if defined(CONFIG_CMD_ENV_CALLBACK)
1411 	"callbacks - print callbacks and their associated variables\nenv "
1412 #endif
1413 	"default [-f] -a - [forcibly] reset default environment\n"
1414 	"env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1415 	"env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1416 #if defined(CONFIG_CMD_EDITENV)
1417 	"env edit name - edit environment variable\n"
1418 #endif
1419 #if defined(CONFIG_CMD_ENV_EXISTS)
1420 	"env exists name - tests for existence of variable\n"
1421 #endif
1422 #if defined(CONFIG_CMD_EXPORTENV)
1423 	"env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1424 #endif
1425 #if defined(CONFIG_CMD_ENV_FLAGS)
1426 	"env flags - print variables that have non-default flags\n"
1427 #endif
1428 #if defined(CONFIG_CMD_GREPENV)
1429 #ifdef CONFIG_REGEX
1430 	"env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1431 #else
1432 	"env grep [-n | -v | -b] string [...] - search environment\n"
1433 #endif
1434 #endif
1435 #if defined(CONFIG_CMD_IMPORTENV)
1436 	"env import [-d] [-t [-r] | -b | -c] addr [size] - import environment\n"
1437 #endif
1438 	"env print [-a | name ...] - print environment\n"
1439 #if defined(CONFIG_CMD_RUN)
1440 	"env run var [...] - run commands in an environment variable\n"
1441 #endif
1442 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1443 	"env save - save environment\n"
1444 #endif
1445 	"env set [-f] name [arg ...]\n";
1446 #endif
1447 
1448 U_BOOT_CMD(
1449 	env, CONFIG_SYS_MAXARGS, 1, do_env,
1450 	"environment handling commands", env_help_text
1451 );
1452 
1453 /*
1454  * Old command line interface, kept for compatibility
1455  */
1456 
1457 #if defined(CONFIG_CMD_EDITENV)
1458 U_BOOT_CMD_COMPLETE(
1459 	editenv, 2, 0,	do_env_edit,
1460 	"edit environment variable",
1461 	"name\n"
1462 	"    - edit environment variable 'name'",
1463 	var_complete
1464 );
1465 #endif
1466 
1467 U_BOOT_CMD_COMPLETE(
1468 	printenv, CONFIG_SYS_MAXARGS, 1,	do_env_print,
1469 	"print environment variables",
1470 	"[-a]\n    - print [all] values of all environment variables\n"
1471 	"printenv name ...\n"
1472 	"    - print value of environment variable 'name'",
1473 	var_complete
1474 );
1475 
1476 #ifdef CONFIG_CMD_GREPENV
1477 U_BOOT_CMD_COMPLETE(
1478 	grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
1479 	"search environment variables",
1480 #ifdef CONFIG_REGEX
1481 	"[-e] [-n | -v | -b] string ...\n"
1482 #else
1483 	"[-n | -v | -b] string ...\n"
1484 #endif
1485 	"    - list environment name=value pairs matching 'string'\n"
1486 #ifdef CONFIG_REGEX
1487 	"      \"-e\": enable regular expressions;\n"
1488 #endif
1489 	"      \"-n\": search variable names; \"-v\": search values;\n"
1490 	"      \"-b\": search both names and values (default)",
1491 	var_complete
1492 );
1493 #endif
1494 
1495 U_BOOT_CMD_COMPLETE(
1496 	setenv, CONFIG_SYS_MAXARGS, 0,	do_env_set,
1497 	"set environment variables",
1498 	"[-f] name value ...\n"
1499 	"    - [forcibly] set environment variable 'name' to 'value ...'\n"
1500 	"setenv [-f] name\n"
1501 	"    - [forcibly] delete environment variable 'name'",
1502 	var_complete
1503 );
1504 
1505 #if defined(CONFIG_CMD_ASKENV)
1506 
1507 U_BOOT_CMD(
1508 	askenv,	CONFIG_SYS_MAXARGS,	1,	do_env_ask,
1509 	"get environment variables from stdin",
1510 	"name [message] [size]\n"
1511 	"    - get environment variable 'name' from stdin (max 'size' chars)"
1512 );
1513 #endif
1514 
1515 #if defined(CONFIG_CMD_RUN)
1516 U_BOOT_CMD_COMPLETE(
1517 	run,	CONFIG_SYS_MAXARGS,	1,	do_run,
1518 	"run commands in an environment variable",
1519 	"var [...]\n"
1520 	"    - run the commands in the environment variable(s) 'var'",
1521 	var_complete
1522 );
1523 #endif
1524 #endif /* CONFIG_SPL_BUILD */
1525