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