nvedit.c 32.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
W
wdenk 已提交
2
/*
3
 * (C) Copyright 2000-2013
W
wdenk 已提交
4 5 6 7
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
 * Andreas Heppel <aheppel@sysgo.de>
K
Kim Phillips 已提交
8 9
 *
 * Copyright 2011 Freescale Semiconductor, Inc.
W
wdenk 已提交
10 11
 */

12
/*
W
wdenk 已提交
13 14
 * Support for persistent environment data
 *
15 16
 * The "environment" is stored on external storage as a list of '\0'
 * 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 19
 * the data part and, in case of redundant environment, a byte of
 * flags.
W
wdenk 已提交
20
 *
21 22 23
 * This linearized representation will also be used before
 * relocation, i. e. as long as we don't have a full C runtime
 * environment. After that, we use a hash table.
W
wdenk 已提交
24 25 26
 */

#include <common.h>
27
#include <cli.h>
W
wdenk 已提交
28
#include <command.h>
29
#include <console.h>
W
wdenk 已提交
30
#include <environment.h>
31 32
#include <search.h>
#include <errno.h>
P
Peter Tyser 已提交
33
#include <malloc.h>
34
#include <mapmem.h>
35
#include <watchdog.h>
W
wdenk 已提交
36 37
#include <linux/stddef.h>
#include <asm/byteorder.h>
38
#include <asm/io.h>
W
wdenk 已提交
39

40 41
DECLARE_GLOBAL_DATA_PTR;

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#if	defined(CONFIG_ENV_IS_IN_EEPROM)	|| \
	defined(CONFIG_ENV_IS_IN_FLASH)		|| \
	defined(CONFIG_ENV_IS_IN_MMC)		|| \
	defined(CONFIG_ENV_IS_IN_FAT)		|| \
	defined(CONFIG_ENV_IS_IN_EXT4)		|| \
	defined(CONFIG_ENV_IS_IN_NAND)		|| \
	defined(CONFIG_ENV_IS_IN_NVRAM)		|| \
	defined(CONFIG_ENV_IS_IN_ONENAND)	|| \
	defined(CONFIG_ENV_IS_IN_SATA)		|| \
	defined(CONFIG_ENV_IS_IN_SPI_FLASH)	|| \
	defined(CONFIG_ENV_IS_IN_REMOTE)	|| \
	defined(CONFIG_ENV_IS_IN_UBI)

#define ENV_IS_IN_DEVICE

#endif

#if	!defined(ENV_IS_IN_DEVICE)		&& \
60
	!defined(CONFIG_ENV_IS_NOWHERE)
61
# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
62
NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
W
wdenk 已提交
63 64
#endif

65 66 67 68
/*
 * Maximum expected input data size for import command
 */
#define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
W
wdenk 已提交
69

H
Heiko Schocher 已提交
70
/*
71
 * This variable is incremented on each do_env_set(), so it can
H
Heiko Schocher 已提交
72 73 74 75 76
 * be used via get_env_id() as an indication, if the environment
 * has changed or not. So it is possible to reread an environment
 * variable only if the environment was changed ... done so for
 * example in NetInitLoop()
 */
H
Heiko Schocher 已提交
77
static int env_id = 1;
W
wdenk 已提交
78

79
int get_env_id(void)
H
Heiko Schocher 已提交
80 81 82
{
	return env_id;
}
W
wdenk 已提交
83

I
Ilya Yanok 已提交
84
#ifndef CONFIG_SPL_BUILD
85
/*
86 87 88
 * Command interface: print one or all environment variables
 *
 * Returns 0 in case of error, or length of printed string
89
 */
90
static int env_print(char *name, int flag)
W
wdenk 已提交
91
{
92
	char *res = NULL;
93
	ssize_t len;
94 95 96 97 98 99

	if (name) {		/* print a single name */
		ENTRY e, *ep;

		e.key = name;
		e.data = NULL;
100
		hsearch_r(e, FIND, &ep, &env_htab, flag);
101 102
		if (ep == NULL)
			return 0;
103
		len = printf("%s=%s\n", ep->key, ep->data);
104 105
		return len;
	}
W
wdenk 已提交
106

107
	/* print whole list */
108
	len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
W
wdenk 已提交
109

110 111 112 113
	if (len > 0) {
		puts(res);
		free(res);
		return len;
W
wdenk 已提交
114 115
	}

116
	/* should never happen */
117
	printf("## Error: cannot export environment\n");
118
	return 0;
119
}
W
wdenk 已提交
120

K
Kim Phillips 已提交
121 122
static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
			char * const argv[])
123 124 125
{
	int i;
	int rcode = 0;
126 127
	int env_flag = H_HIDE_DOT;

128 129 130 131 132
#if defined(CONFIG_CMD_NVEDIT_EFI)
	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
		return do_env_print_efi(cmdtp, flag, --argc, ++argv);
#endif

133 134 135 136 137
	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
		argc--;
		argv++;
		env_flag &= ~H_HIDE_DOT;
	}
W
wdenk 已提交
138

139 140
	if (argc == 1) {
		/* print all env vars */
141
		rcode = env_print(NULL, env_flag);
142
		if (!rcode)
143 144 145 146 147
			return 1;
		printf("\nEnvironment size: %d/%ld bytes\n",
			rcode, (ulong)ENV_SIZE);
		return 0;
	}
W
wdenk 已提交
148

149
	/* print selected env vars */
150
	env_flag &= ~H_HIDE_DOT;
151
	for (i = 1; i < argc; ++i) {
152
		int rc = env_print(argv[i], env_flag);
153 154
		if (!rc) {
			printf("## Error: \"%s\" not defined\n", argv[i]);
155
			++rcode;
W
wdenk 已提交
156 157
		}
	}
158

W
wdenk 已提交
159 160 161
	return rcode;
}

K
Kim Phillips 已提交
162
#ifdef CONFIG_CMD_GREPENV
163 164
static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
		       int argc, char * const argv[])
K
Kim Phillips 已提交
165
{
166
	char *res = NULL;
167
	int len, grep_how, grep_what;
K
Kim Phillips 已提交
168 169

	if (argc < 2)
170
		return CMD_RET_USAGE;
K
Kim Phillips 已提交
171

172 173
	grep_how  = H_MATCH_SUBSTR;	/* default: substring search	*/
	grep_what = H_MATCH_BOTH;	/* default: grep names and values */
174

P
Pierre Aubert 已提交
175 176
	while (--argc > 0 && **++argv == '-') {
		char *arg = *argv;
177 178
		while (*++arg) {
			switch (*arg) {
179 180 181 182 183
#ifdef CONFIG_REGEX
			case 'e':		/* use regex matching */
				grep_how  = H_MATCH_REGEX;
				break;
#endif
184
			case 'n':		/* grep for name */
185
				grep_what = H_MATCH_KEY;
186 187
				break;
			case 'v':		/* grep for value */
188
				grep_what = H_MATCH_DATA;
189 190
				break;
			case 'b':		/* grep for both */
191
				grep_what = H_MATCH_BOTH;
192 193 194 195 196 197 198 199 200 201
				break;
			case '-':
				goto DONE;
			default:
				return CMD_RET_USAGE;
			}
		}
	}

DONE:
202
	len = hexport_r(&env_htab, '\n',
203
			flag | grep_what | grep_how,
204
			&res, 0, argc, argv);
K
Kim Phillips 已提交
205

206 207 208
	if (len > 0) {
		puts(res);
		free(res);
K
Kim Phillips 已提交
209 210
	}

211 212 213 214
	if (len < 2)
		return 1;

	return 0;
K
Kim Phillips 已提交
215 216
}
#endif
I
Ilya Yanok 已提交
217
#endif /* CONFIG_SPL_BUILD */
K
Kim Phillips 已提交
218

219 220 221
/*
 * Set a new environment variable,
 * or replace or delete an existing one.
222
 */
223
static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
224 225 226 227 228
{
	int   i, len;
	char  *name, *value, *s;
	ENTRY e, *ep;

J
Joe Hershberger 已提交
229
	debug("Initial value for argc=%d\n", argc);
230 231 232 233 234 235

#if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI)
	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
		return do_env_set_efi(NULL, flag, --argc, ++argv);
#endif

J
Joe Hershberger 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	while (argc > 1 && **(argv + 1) == '-') {
		char *arg = *++argv;

		--argc;
		while (*++arg) {
			switch (*arg) {
			case 'f':		/* force */
				env_flag |= H_FORCE;
				break;
			default:
				return CMD_RET_USAGE;
			}
		}
	}
	debug("Final value for argc=%d\n", argc);
251 252 253 254 255 256 257 258 259 260
	name = argv[1];

	if (strchr(name, '=')) {
		printf("## Error: illegal character '='"
		       "in variable name \"%s\"\n", name);
		return 1;
	}

	env_id++;

W
wdenk 已提交
261
	/* Delete only ? */
262
	if (argc < 3 || argv[2] == NULL) {
J
Joe Hershberger 已提交
263
		int rc = hdelete_r(name, &env_htab, env_flag);
264
		return !rc;
W
wdenk 已提交
265 266 267
	}

	/*
268
	 * Insert / replace new value
W
wdenk 已提交
269
	 */
270
	for (i = 2, len = 0; i < argc; ++i)
W
wdenk 已提交
271
		len += strlen(argv[i]) + 1;
272 273 274

	value = malloc(len);
	if (value == NULL) {
275
		printf("## Can't malloc %d bytes\n", len);
W
wdenk 已提交
276 277
		return 1;
	}
278
	for (i = 2, s = value; i < argc; ++i) {
279
		char *v = argv[i];
W
wdenk 已提交
280

281
		while ((*s++ = *v++) != '\0')
W
wdenk 已提交
282
			;
283
		*(s - 1) = ' ';
284 285 286 287
	}
	if (s != value)
		*--s = '\0';

288 289
	e.key	= name;
	e.data	= value;
J
Joe Hershberger 已提交
290
	hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
291 292 293 294 295
	free(value);
	if (!ep) {
		printf("## Error inserting \"%s\" variable, errno=%d\n",
			name, errno);
		return 1;
W
wdenk 已提交
296 297 298 299 300
	}

	return 0;
}

S
Simon Glass 已提交
301
int env_set(const char *varname, const char *varvalue)
W
wdenk 已提交
302
{
303 304
	const char * const argv[4] = { "setenv", varname, varvalue, NULL };

305 306 307 308
	/* before import into hashtable */
	if (!(gd->flags & GD_FLG_ENV_READY))
		return 1;

309
	if (varvalue == NULL || varvalue[0] == '\0')
310
		return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
311
	else
312
		return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
W
wdenk 已提交
313 314
}

S
Simon Glass 已提交
315 316 317
/**
 * Set an environment variable to an integer value
 *
318
 * @param varname	Environment variable to set
S
Simon Glass 已提交
319 320 321
 * @param value		Value to set it to
 * @return 0 if ok, 1 on error
 */
322
int env_set_ulong(const char *varname, ulong value)
S
Simon Glass 已提交
323 324 325 326
{
	/* TODO: this should be unsigned */
	char *str = simple_itoa(value);

S
Simon Glass 已提交
327
	return env_set(varname, str);
S
Simon Glass 已提交
328 329 330
}

/**
331
 * Set an environment variable to an value in hex
S
Simon Glass 已提交
332
 *
333
 * @param varname	Environment variable to set
334
 * @param value		Value to set it to
S
Simon Glass 已提交
335 336
 * @return 0 if ok, 1 on error
 */
337
int env_set_hex(const char *varname, ulong value)
S
Simon Glass 已提交
338 339 340
{
	char str[17];

341
	sprintf(str, "%lx", value);
S
Simon Glass 已提交
342
	return env_set(varname, str);
S
Simon Glass 已提交
343 344
}

345
ulong env_get_hex(const char *varname, ulong default_val)
346 347 348 349 350
{
	const char *s;
	ulong value;
	char *endp;

351
	s = env_get(varname);
352 353 354 355 356 357 358 359
	if (s)
		value = simple_strtoul(s, &endp, 16);
	if (!s || endp == s)
		return default_val;

	return value;
}

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
{
	char *end;
	int i;

	for (i = 0; i < 6; ++i) {
		enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
		if (addr)
			addr = (*end) ? end + 1 : end;
	}
}

int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
{
	eth_parse_enetaddr(env_get(name), enetaddr);
	return is_valid_ethaddr(enetaddr);
}

int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
{
	char buf[ARP_HLEN_ASCII + 1];

	if (eth_env_get_enetaddr(name, (uint8_t *)buf))
		return -EEXIST;

	sprintf(buf, "%pM", enetaddr);

	return env_set(name, buf);
}

I
Ilya Yanok 已提交
390
#ifndef CONFIG_SPL_BUILD
K
Kim Phillips 已提交
391
static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
W
wdenk 已提交
392
{
393
	if (argc < 2)
394
		return CMD_RET_USAGE;
W
wdenk 已提交
395

396
	return _do_env_set(flag, argc, argv, H_INTERACTIVE);
W
wdenk 已提交
397 398
}

399
/*
W
wdenk 已提交
400 401
 * Prompt for environment variable
 */
402
#if defined(CONFIG_CMD_ASKENV)
403
int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
W
wdenk 已提交
404
{
405
	char message[CONFIG_SYS_CBSIZE];
W
Wolfgang Denk 已提交
406
	int i, len, pos, size;
W
wdenk 已提交
407
	char *local_args[4];
W
Wolfgang Denk 已提交
408
	char *endptr;
W
wdenk 已提交
409 410 411 412 413 414

	local_args[0] = argv[0];
	local_args[1] = argv[1];
	local_args[2] = NULL;
	local_args[3] = NULL;

W
Wolfgang Denk 已提交
415 416 417 418 419 420
	/*
	 * Check the syntax:
	 *
	 * env_ask envname [message1 ...] [size]
	 */
	if (argc == 1)
421
		return CMD_RET_USAGE;
W
wdenk 已提交
422

W
Wolfgang Denk 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435
	/*
	 * We test the last argument if it can be converted
	 * into a decimal number.  If yes, we assume it's
	 * the size.  Otherwise we echo it as part of the
	 * message.
	 */
	i = simple_strtoul(argv[argc - 1], &endptr, 10);
	if (*endptr != '\0') {			/* no size */
		size = CONFIG_SYS_CBSIZE - 1;
	} else {				/* size given */
		size = i;
		--argc;
	}
W
wdenk 已提交
436

W
Wolfgang Denk 已提交
437 438 439 440
	if (argc <= 2) {
		sprintf(message, "Please enter '%s': ", argv[1]);
	} else {
		/* env_ask envname message1 ... messagen [size] */
441
		for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) {
442
			if (pos)
W
wdenk 已提交
443
				message[pos++] = ' ';
444

445
			strncpy(message + pos, argv[i], sizeof(message) - pos);
W
wdenk 已提交
446 447
			pos += strlen(argv[i]);
		}
448 449 450 451 452
		if (pos < sizeof(message) - 1) {
			message[pos++] = ' ';
			message[pos] = '\0';
		} else
			message[CONFIG_SYS_CBSIZE - 1] = '\0';
W
wdenk 已提交
453 454
	}

455 456
	if (size >= CONFIG_SYS_CBSIZE)
		size = CONFIG_SYS_CBSIZE - 1;
W
wdenk 已提交
457 458 459 460 461

	if (size <= 0)
		return 1;

	/* prompt for input */
462
	len = cli_readline(message);
W
wdenk 已提交
463 464 465 466 467 468 469 470 471 472 473

	if (size < len)
		console_buffer[size] = '\0';

	len = 2;
	if (console_buffer[0] != '\0') {
		local_args[2] = console_buffer;
		len = 3;
	}

	/* Continue calling setenv code */
474
	return _do_env_set(flag, len, local_args, H_INTERACTIVE);
W
wdenk 已提交
475
}
476
#endif
W
wdenk 已提交
477

478
#if defined(CONFIG_CMD_ENV_CALLBACK)
479 480
static int print_static_binding(const char *var_name, const char *callback_name,
				void *priv)
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	printf("\t%-20s %-20s\n", var_name, callback_name);

	return 0;
}

static int print_active_callback(ENTRY *entry)
{
	struct env_clbk_tbl *clbkp;
	int i;
	int num_callbacks;

	if (entry->callback == NULL)
		return 0;

	/* look up the callback in the linker-list */
	num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
	for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
	     i < num_callbacks;
	     i++, clbkp++) {
#if defined(CONFIG_NEEDS_MANUAL_RELOC)
		if (entry->callback == clbkp->callback + gd->reloc_off)
#else
		if (entry->callback == clbkp->callback)
#endif
			break;
	}

	if (i == num_callbacks)
		/* this should probably never happen, but just in case... */
		printf("\t%-20s %p\n", entry->key, entry->callback);
	else
		printf("\t%-20s %-20s\n", entry->key, clbkp->name);

	return 0;
}

/*
 * Print the callbacks available and what they are bound to
 */
int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	struct env_clbk_tbl *clbkp;
	int i;
	int num_callbacks;

	/* Print the available callbacks */
	puts("Available callbacks:\n");
	puts("\tCallback Name\n");
	puts("\t-------------\n");
	num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
	for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
	     i < num_callbacks;
	     i++, clbkp++)
		printf("\t%s\n", clbkp->name);
	puts("\n");

	/* Print the static bindings that may exist */
	puts("Static callback bindings:\n");
	printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
	printf("\t%-20s %-20s\n", "-------------", "-------------");
542
	env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
543 544 545 546 547 548 549 550 551 552 553
	puts("\n");

	/* walk through each variable and print the callback if it has one */
	puts("Active callback bindings:\n");
	printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
	printf("\t%-20s %-20s\n", "-------------", "-------------");
	hwalk_r(&env_htab, print_active_callback);
	return 0;
}
#endif

554
#if defined(CONFIG_CMD_ENV_FLAGS)
555 556
static int print_static_flags(const char *var_name, const char *flags,
			      void *priv)
557 558
{
	enum env_flags_vartype type = env_flags_parse_vartype(flags);
559
	enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
560

561 562 563
	printf("\t%-20s %-20s %-20s\n", var_name,
		env_flags_get_vartype_name(type),
		env_flags_get_varaccess_name(access));
564 565 566 567 568 569 570

	return 0;
}

static int print_active_flags(ENTRY *entry)
{
	enum env_flags_vartype type;
571
	enum env_flags_varaccess access;
572 573 574 575 576 577

	if (entry->flags == 0)
		return 0;

	type = (enum env_flags_vartype)
		(entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
578 579 580 581
	access = env_flags_parse_varaccess_from_binflags(entry->flags);
	printf("\t%-20s %-20s %-20s\n", entry->key,
		env_flags_get_vartype_name(type),
		env_flags_get_varaccess_name(access));
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

	return 0;
}

/*
 * Print the flags available and what variables have flags
 */
int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	/* Print the available variable types */
	printf("Available variable type flags (position %d):\n",
		ENV_FLAGS_VARTYPE_LOC);
	puts("\tFlag\tVariable Type Name\n");
	puts("\t----\t------------------\n");
	env_flags_print_vartypes();
	puts("\n");

599 600 601 602 603 604 605 606
	/* Print the available variable access types */
	printf("Available variable access flags (position %d):\n",
		ENV_FLAGS_VARACCESS_LOC);
	puts("\tFlag\tVariable Access Name\n");
	puts("\t----\t--------------------\n");
	env_flags_print_varaccess();
	puts("\n");

607 608
	/* Print the static flags that may exist */
	puts("Static flags:\n");
609 610 611 612
	printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
		"Variable Access");
	printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
		"---------------");
613
	env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
614 615 616 617
	puts("\n");

	/* walk through each variable and print the flags if non-default */
	puts("Active flags:\n");
618 619 620 621
	printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
		"Variable Access");
	printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
		"---------------");
622 623 624 625 626
	hwalk_r(&env_htab, print_active_flags);
	return 0;
}
#endif

627
/*
P
Peter Tyser 已提交
628 629 630
 * Interactively edit an environment variable
 */
#if defined(CONFIG_CMD_EDITENV)
K
Kim Phillips 已提交
631 632
static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
		       char * const argv[])
P
Peter Tyser 已提交
633 634 635 636
{
	char buffer[CONFIG_SYS_CBSIZE];
	char *init_val;

637
	if (argc < 2)
638
		return CMD_RET_USAGE;
P
Peter Tyser 已提交
639

640 641 642 643
	/* before import into hashtable */
	if (!(gd->flags & GD_FLG_ENV_READY))
		return 1;

P
Peter Tyser 已提交
644
	/* Set read buffer to initial value or empty sting */
645
	init_val = env_get(argv[1]);
P
Peter Tyser 已提交
646
	if (init_val)
647
		snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
P
Peter Tyser 已提交
648 649 650
	else
		buffer[0] = '\0';

651
	if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
652
		return 1;
P
Peter Tyser 已提交
653

654 655 656 657 658 659 660 661 662 663
	if (buffer[0] == '\0') {
		const char * const _argv[3] = { "setenv", argv[1], NULL };

		return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
	} else {
		const char * const _argv[4] = { "setenv", argv[1], buffer,
			NULL };

		return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
	}
P
Peter Tyser 已提交
664 665
}
#endif /* CONFIG_CMD_EDITENV */
I
Ilya Yanok 已提交
666
#endif /* CONFIG_SPL_BUILD */
P
Peter Tyser 已提交
667

668
/*
W
wdenk 已提交
669 670 671 672
 * Look up variable from environment,
 * return address of storage for that variable,
 * or NULL if not found
 */
673
char *env_get(const char *name)
W
wdenk 已提交
674
{
675
	if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
676
		ENTRY e, *ep;
W
wdenk 已提交
677

678
		WATCHDOG_RESET();
679

680 681
		e.key	= name;
		e.data	= NULL;
J
Joe Hershberger 已提交
682
		hsearch_r(e, FIND, &ep, &env_htab, 0);
683

684
		return ep ? ep->data : NULL;
W
wdenk 已提交
685 686
	}

687
	/* restricted capabilities before import */
688
	if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
689
		return (char *)(gd->env_buf);
690

691
	return NULL;
W
wdenk 已提交
692 693
}

694 695 696
/*
 * Look up variable from environment for restricted C runtime env.
 */
697
int env_get_f(const char *name, char *buf, unsigned len)
W
wdenk 已提交
698
{
699
	int i, nxt, c;
W
wdenk 已提交
700

701
	for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
W
wdenk 已提交
702 703
		int val, n;

704 705 706
		for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) {
			if (c < 0)
				return c;
707 708
			if (nxt >= CONFIG_ENV_SIZE)
				return -1;
W
wdenk 已提交
709
		}
710 711 712

		val = envmatch((uchar *)name, i);
		if (val < 0)
W
wdenk 已提交
713
			continue;
714

W
wdenk 已提交
715
		/* found; copy out */
716
		for (n = 0; n < len; ++n, ++buf) {
717 718 719 720
			c = env_get_char(val++);
			if (c < 0)
				return c;
			*buf = c;
721
			if (*buf == '\0')
722 723 724 725 726 727
				return n;
		}

		if (n)
			*--buf = '\0';

728 729
		printf("env_buf [%u bytes] too small for value of \"%s\"\n",
		       len, name);
730 731

		return n;
W
wdenk 已提交
732
	}
733

734
	return -1;
W
wdenk 已提交
735 736
}

737 738 739
/**
 * Decode the integer value of an environment variable and return it.
 *
S
Shyam Saini 已提交
740
 * @param name		Name of environment variable
741 742 743 744 745
 * @param base		Number base to use (normally 10, or 16 for hex)
 * @param default_val	Default value to return if the variable is not
 *			found
 * @return the decoded value, or default_val if not found
 */
746
ulong env_get_ulong(const char *name, int base, ulong default_val)
747 748
{
	/*
749
	 * We can use env_get() here, even before relocation, since the
750 751
	 * environment variable value is an integer and thus short.
	 */
752
	const char *str = env_get(name);
753 754 755 756

	return str ? simple_strtoul(str, NULL, base) : default_val;
}

I
Ilya Yanok 已提交
757
#ifndef CONFIG_SPL_BUILD
758
#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
K
Kim Phillips 已提交
759 760
static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
		       char * const argv[])
W
wdenk 已提交
761
{
762
	return env_save() ? 1 : 0;
W
wdenk 已提交
763
}
W
wdenk 已提交
764

M
Mike Frysinger 已提交
765
U_BOOT_CMD(
766
	saveenv, 1, 0,	do_env_save,
P
Peter Tyser 已提交
767
	"save environment variables to persistent storage",
W
Wolfgang Denk 已提交
768
	""
M
Mike Frysinger 已提交
769
);
W
wdenk 已提交
770
#endif
I
Ilya Yanok 已提交
771
#endif /* CONFIG_SPL_BUILD */
W
wdenk 已提交
772 773


774
/*
W
wdenk 已提交
775 776 777 778
 * Match a name / name=value pair
 *
 * s1 is either a simple 'name', or a 'name=value' pair.
 * i2 is the environment index for a 'name2=value2' pair.
779
 * If the names match, return the index for the value2, else -1.
W
wdenk 已提交
780
 */
781
int envmatch(uchar *s1, int i2)
W
wdenk 已提交
782
{
783 784 785
	if (s1 == NULL)
		return -1;

W
wdenk 已提交
786 787
	while (*s1 == env_get_char(i2++))
		if (*s1++ == '=')
788
			return i2;
789

W
wdenk 已提交
790
	if (*s1 == '\0' && env_get_char(i2-1) == '=')
791
		return i2;
792

793
	return -1;
W
wdenk 已提交
794
}
W
wdenk 已提交
795

I
Ilya Yanok 已提交
796
#ifndef CONFIG_SPL_BUILD
797
static int do_env_default(cmd_tbl_t *cmdtp, int flag,
798
			  int argc, char * const argv[])
799
{
800
	int all = 0, env_flag = H_INTERACTIVE;
801

802 803 804 805 806 807 808 809 810 811
	debug("Initial value for argc=%d\n", argc);
	while (--argc > 0 && **++argv == '-') {
		char *arg = *argv;

		while (*++arg) {
			switch (*arg) {
			case 'a':		/* default all */
				all = 1;
				break;
			case 'f':		/* force */
812
				env_flag |= H_FORCE;
813 814 815 816 817 818 819 820 821
				break;
			default:
				return cmd_usage(cmdtp);
			}
		}
	}
	debug("Final value for argc=%d\n", argc);
	if (all && (argc == 0)) {
		/* Reset the whole environment */
822 823
		set_default_env("## Resetting to default environment\n",
				env_flag);
824 825 826 827
		return 0;
	}
	if (!all && (argc > 0)) {
		/* Reset individual variables */
828
		set_default_vars(argc, argv, env_flag);
829 830 831 832
		return 0;
	}

	return cmd_usage(cmdtp);
833 834
}

835 836
static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
			 int argc, char * const argv[])
837
{
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	int env_flag = H_INTERACTIVE;
	int ret = 0;

	debug("Initial value for argc=%d\n", argc);
	while (argc > 1 && **(argv + 1) == '-') {
		char *arg = *++argv;

		--argc;
		while (*++arg) {
			switch (*arg) {
			case 'f':		/* force */
				env_flag |= H_FORCE;
				break;
			default:
				return CMD_RET_USAGE;
			}
		}
	}
	debug("Final value for argc=%d\n", argc);

	env_id++;

	while (--argc > 0) {
		char *name = *++argv;

		if (!hdelete_r(name, &env_htab, env_flag))
			ret = 1;
	}

	return ret;
868 869
}

870
#ifdef CONFIG_CMD_EXPORTENV
871
/*
872
 * env export [-t | -b | -c] [-s size] addr [var ...]
873 874 875 876 877 878 879 880 881
 *	-t:	export as text format; if size is given, data will be
 *		padded with '\0' bytes; if not, one terminating '\0'
 *		will be added (which is included in the "filesize"
 *		setting so you can for exmple copy this to flash and
 *		keep the termination).
 *	-b:	export as binary format (name=value pairs separated by
 *		'\0', list end marked by double "\0\0")
 *	-c:	export as checksum protected environment format as
 *		used for example by "saveenv" command
882 883
 *	-s size:
 *		size of output buffer
884
 *	addr:	memory address where environment gets stored
885 886 887
 *	var...	List of variable names that get included into the
 *		export. Without arguments, the whole environment gets
 *		exported.
888 889 890 891
 *
 * With "-c" and size is NOT given, then the export command will
 * format the data as currently used for the persistent storage,
 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
892
 * prepend a valid CRC32 checksum and, in case of redundant
893 894 895 896 897 898 899 900
 * environment, a "current" redundancy flag. If size is given, this
 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
 * checksum and redundancy flag will be inserted.
 *
 * With "-b" and "-t", always only the real data (including a
 * terminating '\0' byte) will be written; here the optional size
 * argument will be used to make sure not to overflow the user
 * provided buffer; the command will abort if the size is not
901
 * sufficient. Any remaining space will be '\0' padded.
902 903 904 905
 *
 * On successful return, the variable "filesize" will be set.
 * Note that filesize includes the trailing/terminating '\0' byte(s).
 *
906
 * Usage scenario:  create a text snapshot/backup of the current settings:
907 908 909 910 911 912 913 914 915
 *
 *	=> env export -t 100000
 *	=> era ${backup_addr} +${filesize}
 *	=> cp.b 100000 ${backup_addr} ${filesize}
 *
 * Re-import this snapshot, deleting all other settings:
 *
 *	=> env import -d -t ${backup_addr}
 */
916 917
static int do_env_export(cmd_tbl_t *cmdtp, int flag,
			 int argc, char * const argv[])
918 919
{
	char	buf[32];
920 921
	ulong	addr;
	char	*ptr, *cmd, *res;
922
	size_t	size = 0;
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	ssize_t	len;
	env_t	*envp;
	char	sep = '\n';
	int	chk = 0;
	int	fmt = 0;

	cmd = *argv;

	while (--argc > 0 && **++argv == '-') {
		char *arg = *argv;
		while (*++arg) {
			switch (*arg) {
			case 'b':		/* raw binary format */
				if (fmt++)
					goto sep_err;
				sep = '\0';
				break;
			case 'c':		/* external checksum format */
				if (fmt++)
					goto sep_err;
				sep = '\0';
				chk = 1;
				break;
946 947 948 949 950
			case 's':		/* size given */
				if (--argc <= 0)
					return cmd_usage(cmdtp);
				size = simple_strtoul(*++argv, NULL, 16);
				goto NXTARG;
951 952 953 954 955 956
			case 't':		/* text format */
				if (fmt++)
					goto sep_err;
				sep = '\n';
				break;
			default:
957
				return CMD_RET_USAGE;
958 959
			}
		}
960
NXTARG:		;
961 962
	}

963
	if (argc < 1)
964
		return CMD_RET_USAGE;
W
wdenk 已提交
965

966 967
	addr = simple_strtoul(argv[0], NULL, 16);
	ptr = map_sysmem(addr, size);
968

969
	if (size)
970
		memset(ptr, '\0', size);
971 972 973

	argc--;
	argv++;
974 975

	if (sep) {		/* export as text file */
976 977
		len = hexport_r(&env_htab, sep,
				H_MATCH_KEY | H_MATCH_IDENT,
978
				&ptr, size, argc, argv);
979
		if (len < 0) {
980 981
			pr_err("## Error: Cannot export environment: errno = %d\n",
			       errno);
982 983
			return 1;
		}
984
		sprintf(buf, "%zX", (size_t)len);
S
Simon Glass 已提交
985
		env_set("filesize", buf);
986 987 988 989

		return 0;
	}

990
	envp = (env_t *)ptr;
991 992 993 994

	if (chk)		/* export as checksum protected block */
		res = (char *)envp->data;
	else			/* export as raw binary data */
995
		res = ptr;
996

997 998 999
	len = hexport_r(&env_htab, '\0',
			H_MATCH_KEY | H_MATCH_IDENT,
			&res, ENV_SIZE, argc, argv);
1000
	if (len < 0) {
1001 1002
		pr_err("## Error: Cannot export environment: errno = %d\n",
		       errno);
1003 1004 1005 1006
		return 1;
	}

	if (chk) {
1007 1008
		envp->crc = crc32(0, envp->data,
				size ? size - offsetof(env_t, data) : ENV_SIZE);
1009 1010 1011 1012
#ifdef CONFIG_ENV_ADDR_REDUND
		envp->flags = ACTIVE_FLAG;
#endif
	}
1013
	env_set_hex("filesize", len + offsetof(env_t, data));
1014 1015 1016 1017

	return 0;

sep_err:
1018 1019
	printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
	       cmd);
1020 1021
	return 1;
}
1022
#endif
1023

1024
#ifdef CONFIG_CMD_IMPORTENV
1025
/*
1026 1027 1028 1029 1030
 * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
 *	-d:	delete existing environment before importing if no var is
 *		passed; if vars are passed, if one var is in the current
 *		environment but not in the environment at addr, delete var from
 *		current environment;
1031
 *		otherwise overwrite / append to existing definitions
1032 1033
 *	-t:	assume text format; either "size" must be given or the
 *		text data must be '\0' terminated
1034 1035 1036 1037
 *	-r:	handle CRLF like LF, that means exported variables with
 *		a content which ends with \r won't get imported. Used
 *		to import text files created with editors which are using CRLF
 *		for line endings. Only effective in addition to -t.
1038 1039 1040 1041 1042
 *	-b:	assume binary format ('\0' separated, "\0\0" terminated)
 *	-c:	assume checksum protected environment format
 *	addr:	memory address to read from
 *	size:	length of input data; if missing, proper '\0'
 *		termination is mandatory
1043 1044 1045 1046 1047
 *		if var is set and size should be missing (i.e. '\0'
 *		termination), set size to '-'
 *	var...	List of the names of the only variables that get imported from
 *		the environment at address 'addr'. Without arguments, the whole
 *		environment gets imported.
1048
 */
1049 1050
static int do_env_import(cmd_tbl_t *cmdtp, int flag,
			 int argc, char * const argv[])
1051
{
1052 1053
	ulong	addr;
	char	*cmd, *ptr;
1054 1055 1056 1057
	char	sep = '\n';
	int	chk = 0;
	int	fmt = 0;
	int	del = 0;
1058
	int	crlf_is_lf = 0;
1059
	int	wl = 0;
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	size_t	size;

	cmd = *argv;

	while (--argc > 0 && **++argv == '-') {
		char *arg = *argv;
		while (*++arg) {
			switch (*arg) {
			case 'b':		/* raw binary format */
				if (fmt++)
					goto sep_err;
				sep = '\0';
				break;
			case 'c':		/* external checksum format */
				if (fmt++)
					goto sep_err;
				sep = '\0';
				chk = 1;
				break;
			case 't':		/* text format */
				if (fmt++)
					goto sep_err;
				sep = '\n';
				break;
1084 1085 1086
			case 'r':		/* handle CRLF like LF */
				crlf_is_lf = 1;
				break;
1087 1088 1089 1090
			case 'd':
				del = 1;
				break;
			default:
1091
				return CMD_RET_USAGE;
1092 1093 1094 1095
			}
		}
	}

1096
	if (argc < 1)
1097
		return CMD_RET_USAGE;
1098 1099 1100 1101

	if (!fmt)
		printf("## Warning: defaulting to text format\n");

1102 1103 1104
	if (sep != '\n' && crlf_is_lf )
		crlf_is_lf = 0;

1105 1106
	addr = simple_strtoul(argv[0], NULL, 16);
	ptr = map_sysmem(addr, 0);
1107

1108
	if (argc >= 2 && strcmp(argv[1], "-")) {
1109
		size = simple_strtoul(argv[1], NULL, 16);
1110
	} else if (chk) {
1111 1112
		puts("## Error: external checksum format must pass size\n");
		return CMD_RET_FAILURE;
1113
	} else {
1114
		char *s = ptr;
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

		size = 0;

		while (size < MAX_ENV_SIZE) {
			if ((*s == sep) && (*(s+1) == '\0'))
				break;
			++s;
			++size;
		}
		if (size == MAX_ENV_SIZE) {
			printf("## Warning: Input data exceeds %d bytes"
				" - truncated\n", MAX_ENV_SIZE);
		}
1128
		size += 2;
S
Simon Glass 已提交
1129
		printf("## Info: input data size = %zu = 0x%zX\n", size, size);
1130 1131
	}

1132 1133 1134
	if (argc > 2)
		wl = 1;

1135 1136
	if (chk) {
		uint32_t crc;
1137
		env_t *ep = (env_t *)ptr;
1138 1139 1140 1141 1142 1143 1144 1145

		size -= offsetof(env_t, data);
		memcpy(&crc, &ep->crc, sizeof(crc));

		if (crc32(0, ep->data, size) != crc) {
			puts("## Error: bad CRC, import failed\n");
			return 1;
		}
1146
		ptr = (char *)ep->data;
1147 1148
	}

1149 1150
	if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
		       crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
1151 1152
		pr_err("## Error: Environment import failed: errno = %d\n",
		       errno);
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		return 1;
	}
	gd->flags |= GD_FLG_ENV_READY;

	return 0;

sep_err:
	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
		cmd);
	return 1;
}
1164
#endif
1165

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
#if defined(CONFIG_CMD_ENV_EXISTS)
static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
		       char * const argv[])
{
	ENTRY e, *ep;

	if (argc < 2)
		return CMD_RET_USAGE;

	e.key = argv[1];
	e.data = NULL;
	hsearch_r(e, FIND, &ep, &env_htab, 0);

	return (ep == NULL) ? 1 : 0;
}
#endif

1183 1184 1185 1186 1187 1188 1189 1190
/*
 * New command line interface: "env" command with subcommands
 */
static cmd_tbl_t cmd_env_sub[] = {
#if defined(CONFIG_CMD_ASKENV)
	U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
#endif
	U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1191
	U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1192 1193 1194
#if defined(CONFIG_CMD_EDITENV)
	U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
#endif
1195 1196 1197
#if defined(CONFIG_CMD_ENV_CALLBACK)
	U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
#endif
1198 1199 1200
#if defined(CONFIG_CMD_ENV_FLAGS)
	U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
#endif
1201
#if defined(CONFIG_CMD_EXPORTENV)
1202
	U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1203
#endif
K
Kim Phillips 已提交
1204 1205 1206
#if defined(CONFIG_CMD_GREPENV)
	U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
#endif
1207
#if defined(CONFIG_CMD_IMPORTENV)
1208
	U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1209
#endif
1210 1211 1212 1213
	U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
#if defined(CONFIG_CMD_RUN)
	U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
#endif
1214
#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1215 1216 1217
	U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
#endif
	U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1218 1219 1220
#if defined(CONFIG_CMD_ENV_EXISTS)
	U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
#endif
1221 1222
};

1223
#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1224 1225 1226 1227 1228 1229
void env_reloc(void)
{
	fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
}
#endif

1230
static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1231 1232 1233
{
	cmd_tbl_t *cp;

1234
	if (argc < 2)
1235
		return CMD_RET_USAGE;
1236

1237 1238 1239 1240 1241 1242 1243 1244 1245
	/* drop initial "env" arg */
	argc--;
	argv++;

	cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));

	if (cp)
		return cp->cmd(cmdtp, flag, argc, argv);

1246
	return CMD_RET_USAGE;
1247 1248
}

K
Kim Phillips 已提交
1249 1250
#ifdef CONFIG_SYS_LONGHELP
static char env_help_text[] =
1251 1252
#if defined(CONFIG_CMD_ASKENV)
	"ask name [message] [size] - ask for environment variable\nenv "
1253 1254 1255
#endif
#if defined(CONFIG_CMD_ENV_CALLBACK)
	"callbacks - print callbacks and their associated variables\nenv "
1256
#endif
1257 1258
	"default [-f] -a - [forcibly] reset default environment\n"
	"env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1259
	"env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1260 1261 1262
#if defined(CONFIG_CMD_EDITENV)
	"env edit name - edit environment variable\n"
#endif
1263 1264 1265
#if defined(CONFIG_CMD_ENV_EXISTS)
	"env exists name - tests for existence of variable\n"
#endif
1266
#if defined(CONFIG_CMD_EXPORTENV)
1267
	"env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1268
#endif
1269 1270 1271
#if defined(CONFIG_CMD_ENV_FLAGS)
	"env flags - print variables that have non-default flags\n"
#endif
K
Kim Phillips 已提交
1272
#if defined(CONFIG_CMD_GREPENV)
1273 1274 1275
#ifdef CONFIG_REGEX
	"env grep [-e] [-n | -v | -b] string [...] - search environment\n"
#else
1276
	"env grep [-n | -v | -b] string [...] - search environment\n"
K
Kim Phillips 已提交
1277
#endif
1278
#endif
1279
#if defined(CONFIG_CMD_IMPORTENV)
1280
	"env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
1281
#endif
1282
	"env print [-a | name ...] - print environment\n"
1283 1284 1285
#if defined(CONFIG_CMD_NVEDIT_EFI)
	"env print -e [name ...] - print UEFI environment\n"
#endif
1286 1287 1288
#if defined(CONFIG_CMD_RUN)
	"env run var [...] - run commands in an environment variable\n"
#endif
1289
#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
1290
	"env save - save environment\n"
1291 1292 1293
#endif
#if defined(CONFIG_CMD_NVEDIT_EFI)
	"env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
1294
#endif
K
Kim Phillips 已提交
1295 1296 1297 1298 1299 1300
	"env set [-f] name [arg ...]\n";
#endif

U_BOOT_CMD(
	env, CONFIG_SYS_MAXARGS, 1, do_env,
	"environment handling commands", env_help_text
1301 1302 1303 1304 1305
);

/*
 * Old command line interface, kept for compatibility
 */
W
wdenk 已提交
1306

P
Peter Tyser 已提交
1307
#if defined(CONFIG_CMD_EDITENV)
1308
U_BOOT_CMD_COMPLETE(
1309
	editenv, 2, 0,	do_env_edit,
P
Peter Tyser 已提交
1310 1311
	"edit environment variable",
	"name\n"
1312 1313
	"    - edit environment variable 'name'",
	var_complete
P
Peter Tyser 已提交
1314 1315 1316
);
#endif

1317
U_BOOT_CMD_COMPLETE(
1318
	printenv, CONFIG_SYS_MAXARGS, 1,	do_env_print,
P
Peter Tyser 已提交
1319
	"print environment variables",
1320
	"[-a]\n    - print [all] values of all environment variables\n"
1321 1322 1323 1324
#if defined(CONFIG_CMD_NVEDIT_EFI)
	"printenv -e [name ...]\n"
	"    - print UEFI variable 'name' or all the variables\n"
#endif
W
wdenk 已提交
1325
	"printenv name ...\n"
1326 1327
	"    - print value of environment variable 'name'",
	var_complete
W
wdenk 已提交
1328 1329
);

K
Kim Phillips 已提交
1330 1331 1332 1333
#ifdef CONFIG_CMD_GREPENV
U_BOOT_CMD_COMPLETE(
	grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
	"search environment variables",
1334 1335 1336
#ifdef CONFIG_REGEX
	"[-e] [-n | -v | -b] string ...\n"
#else
1337
	"[-n | -v | -b] string ...\n"
1338
#endif
1339
	"    - list environment name=value pairs matching 'string'\n"
1340 1341 1342
#ifdef CONFIG_REGEX
	"      \"-e\": enable regular expressions;\n"
#endif
1343 1344
	"      \"-n\": search variable names; \"-v\": search values;\n"
	"      \"-b\": search both names and values (default)",
K
Kim Phillips 已提交
1345 1346 1347 1348
	var_complete
);
#endif

1349
U_BOOT_CMD_COMPLETE(
1350
	setenv, CONFIG_SYS_MAXARGS, 0,	do_env_set,
P
Peter Tyser 已提交
1351
	"set environment variables",
1352
#if defined(CONFIG_CMD_NVEDIT_EFI)
1353
	"-e [-nv] name [value ...]\n"
1354
	"    - set UEFI variable 'name' to 'value' ...'\n"
1355
	"      'nv' option makes the variable non-volatile\n"
1356 1357 1358
	"    - delete UEFI variable 'name' if 'value' not specified\n"
#endif
	"setenv [-f] name value ...\n"
J
Joe Hershberger 已提交
1359 1360 1361
	"    - [forcibly] set environment variable 'name' to 'value ...'\n"
	"setenv [-f] name\n"
	"    - [forcibly] delete environment variable 'name'",
1362
	var_complete
W
wdenk 已提交
1363 1364
);

1365
#if defined(CONFIG_CMD_ASKENV)
W
wdenk 已提交
1366

W
wdenk 已提交
1367
U_BOOT_CMD(
1368
	askenv,	CONFIG_SYS_MAXARGS,	1,	do_env_ask,
P
Peter Tyser 已提交
1369
	"get environment variables from stdin",
W
wdenk 已提交
1370
	"name [message] [size]\n"
W
Wolfgang Denk 已提交
1371
	"    - get environment variable 'name' from stdin (max 'size' chars)"
W
wdenk 已提交
1372
);
1373
#endif
W
wdenk 已提交
1374

1375
#if defined(CONFIG_CMD_RUN)
1376
U_BOOT_CMD_COMPLETE(
1377
	run,	CONFIG_SYS_MAXARGS,	1,	do_run,
P
Peter Tyser 已提交
1378
	"run commands in an environment variable",
W
wdenk 已提交
1379
	"var [...]\n"
1380 1381
	"    - run the commands in the environment variable(s) 'var'",
	var_complete
W
wdenk 已提交
1382
);
1383
#endif
I
Ilya Yanok 已提交
1384
#endif /* CONFIG_SPL_BUILD */