Blame view

cmd/nvedit.c 30.1 KB
a68d3ed0a   wdenk   Initial revision
1
  /*
ea009d474   Wolfgang Denk   hashtable: prepar...
2
   * (C) Copyright 2000-2013
a68d3ed0a   wdenk   Initial revision
3
4
5
6
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   * Andreas Heppel <aheppel@sysgo.de>
a000b7950   Kim Phillips   common: add a gre...
7
8
9
   *
   * Copyright 2011 Freescale Semiconductor, Inc.
   *
3765b3e7b   Wolfgang Denk   Coding Style clea...
10
   * SPDX-License-Identifier:	GPL-2.0+
a68d3ed0a   wdenk   Initial revision
11
   */
ea882baf9   Wolfgang Denk   New implementatio...
12
  /*
a68d3ed0a   wdenk   Initial revision
13
14
   * Support for persistent environment data
   *
ea882baf9   Wolfgang Denk   New implementatio...
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
fc0b5948e   Robert P. J. Day   Various, accumula...
17
   * a double '\0'. The environment is preceded by a 32 bit CRC over
ea882baf9   Wolfgang Denk   New implementatio...
18
19
   * the data part and, in case of redundant environment, a byte of
   * flags.
a68d3ed0a   wdenk   Initial revision
20
   *
ea882baf9   Wolfgang Denk   New implementatio...
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.
a68d3ed0a   wdenk   Initial revision
24
25
26
   */
  
  #include <common.h>
18d66533a   Simon Glass   move CLI prototyp...
27
  #include <cli.h>
a68d3ed0a   wdenk   Initial revision
28
  #include <command.h>
24b852a7a   Simon Glass   Move console defi...
29
  #include <console.h>
a68d3ed0a   wdenk   Initial revision
30
  #include <environment.h>
ea882baf9   Wolfgang Denk   New implementatio...
31
32
  #include <search.h>
  #include <errno.h>
246c69225   Peter Tyser   Add 'editenv' com...
33
  #include <malloc.h>
0eb25b619   Joe Hershberger   common: Make sure...
34
  #include <mapmem.h>
2a3cb0207   wdenk   Quick & Dirty fix...
35
  #include <watchdog.h>
a68d3ed0a   wdenk   Initial revision
36
37
  #include <linux/stddef.h>
  #include <asm/byteorder.h>
fd37dac9e   Simon Glass   sandbox: Support ...
38
  #include <asm/io.h>
a68d3ed0a   wdenk   Initial revision
39

d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
40
  DECLARE_GLOBAL_DATA_PTR;
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
41
42
43
  #if	!defined(CONFIG_ENV_IS_IN_EEPROM)	&& \
  	!defined(CONFIG_ENV_IS_IN_FLASH)	&& \
  	!defined(CONFIG_ENV_IS_IN_DATAFLASH)	&& \
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
44
  	!defined(CONFIG_ENV_IS_IN_MMC)		&& \
57210c7cc   Maximilian Schwerin   Add support for l...
45
  	!defined(CONFIG_ENV_IS_IN_FAT)		&& \
fd1000b9c   Stuart Longland   common: Add suppo...
46
  	!defined(CONFIG_ENV_IS_IN_EXT4)		&& \
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
47
48
49
  	!defined(CONFIG_ENV_IS_IN_NAND)		&& \
  	!defined(CONFIG_ENV_IS_IN_NVRAM)	&& \
  	!defined(CONFIG_ENV_IS_IN_ONENAND)	&& \
125d193c4   Peng Fan   common: env: supp...
50
  	!defined(CONFIG_ENV_IS_IN_SATA)		&& \
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
51
  	!defined(CONFIG_ENV_IS_IN_SPI_FLASH)	&& \
0a85a9e70   Liu Gang   powerpc/corenet_d...
52
  	!defined(CONFIG_ENV_IS_IN_REMOTE)	&& \
2b74433f3   Joe Hershberger   env: Add support ...
53
  	!defined(CONFIG_ENV_IS_IN_UBI)		&& \
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
54
  	!defined(CONFIG_ENV_IS_NOWHERE)
75eb82ec7   unsik Kim   mflash: Initial m...
55
  # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
125d193c4   Peng Fan   common: env: supp...
56
  SATA|SPI_FLASH|NVRAM|MMC|FAT|EXT4|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
a68d3ed0a   wdenk   Initial revision
57
  #endif
ea882baf9   Wolfgang Denk   New implementatio...
58
59
60
61
  /*
   * Maximum expected input data size for import command
   */
  #define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
a68d3ed0a   wdenk   Initial revision
62

a68d3ed0a   wdenk   Initial revision
63
  /*
ea882baf9   Wolfgang Denk   New implementatio...
64
   * This variable is incremented on each do_env_set(), so it can
da95427ce   Heiko Schocher   netloop: updates ...
65
66
67
68
69
   * 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()
   */
2f70c49e5   Heiko Schocher   netloop: speed up...
70
  static int env_id = 1;
a68d3ed0a   wdenk   Initial revision
71

f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
72
  int get_env_id(void)
2f70c49e5   Heiko Schocher   netloop: speed up...
73
74
75
  {
  	return env_id;
  }
a68d3ed0a   wdenk   Initial revision
76

7ac2fe2da   Ilya Yanok   OMAP: networking ...
77
  #ifndef CONFIG_SPL_BUILD
4c94f6c54   Mike Frysinger   nvedit: speed up ...
78
  /*
ea882baf9   Wolfgang Denk   New implementatio...
79
80
81
   * Command interface: print one or all environment variables
   *
   * Returns 0 in case of error, or length of printed string
4c94f6c54   Mike Frysinger   nvedit: speed up ...
82
   */
be11235ab   Joe Hershberger   env: Hide '.' var...
83
  static int env_print(char *name, int flag)
a68d3ed0a   wdenk   Initial revision
84
  {
ea882baf9   Wolfgang Denk   New implementatio...
85
  	char *res = NULL;
22a4a6c5c   Maxime Larocque   printenv: Correct...
86
  	ssize_t len;
ea882baf9   Wolfgang Denk   New implementatio...
87
88
89
90
91
92
  
  	if (name) {		/* print a single name */
  		ENTRY e, *ep;
  
  		e.key = name;
  		e.data = NULL;
be11235ab   Joe Hershberger   env: Hide '.' var...
93
  		hsearch_r(e, FIND, &ep, &env_htab, flag);
ea882baf9   Wolfgang Denk   New implementatio...
94
95
  		if (ep == NULL)
  			return 0;
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
96
97
  		len = printf("%s=%s
  ", ep->key, ep->data);
ea882baf9   Wolfgang Denk   New implementatio...
98
99
  		return len;
  	}
a68d3ed0a   wdenk   Initial revision
100

ea882baf9   Wolfgang Denk   New implementatio...
101
  	/* print whole list */
be11235ab   Joe Hershberger   env: Hide '.' var...
102
103
  	len = hexport_r(&env_htab, '
  ', flag, &res, 0, 0, NULL);
a68d3ed0a   wdenk   Initial revision
104

ea882baf9   Wolfgang Denk   New implementatio...
105
106
107
108
  	if (len > 0) {
  		puts(res);
  		free(res);
  		return len;
a68d3ed0a   wdenk   Initial revision
109
  	}
ea882baf9   Wolfgang Denk   New implementatio...
110
  	/* should never happen */
22a4a6c5c   Maxime Larocque   printenv: Correct...
111
112
  	printf("## Error: cannot export environment
  ");
ea882baf9   Wolfgang Denk   New implementatio...
113
  	return 0;
4c94f6c54   Mike Frysinger   nvedit: speed up ...
114
  }
a68d3ed0a   wdenk   Initial revision
115

088f1b199   Kim Phillips   common/cmd_*.c: s...
116
117
  static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
  			char * const argv[])
4c94f6c54   Mike Frysinger   nvedit: speed up ...
118
119
120
  {
  	int i;
  	int rcode = 0;
be11235ab   Joe Hershberger   env: Hide '.' var...
121
122
123
124
125
126
127
  	int env_flag = H_HIDE_DOT;
  
  	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
  		argc--;
  		argv++;
  		env_flag &= ~H_HIDE_DOT;
  	}
a68d3ed0a   wdenk   Initial revision
128

4c94f6c54   Mike Frysinger   nvedit: speed up ...
129
130
  	if (argc == 1) {
  		/* print all env vars */
be11235ab   Joe Hershberger   env: Hide '.' var...
131
  		rcode = env_print(NULL, env_flag);
ea882baf9   Wolfgang Denk   New implementatio...
132
  		if (!rcode)
4c94f6c54   Mike Frysinger   nvedit: speed up ...
133
134
135
136
137
138
139
  			return 1;
  		printf("
  Environment size: %d/%ld bytes
  ",
  			rcode, (ulong)ENV_SIZE);
  		return 0;
  	}
a68d3ed0a   wdenk   Initial revision
140

4c94f6c54   Mike Frysinger   nvedit: speed up ...
141
  	/* print selected env vars */
be11235ab   Joe Hershberger   env: Hide '.' var...
142
  	env_flag &= ~H_HIDE_DOT;
4c94f6c54   Mike Frysinger   nvedit: speed up ...
143
  	for (i = 1; i < argc; ++i) {
be11235ab   Joe Hershberger   env: Hide '.' var...
144
  		int rc = env_print(argv[i], env_flag);
ea882baf9   Wolfgang Denk   New implementatio...
145
146
147
  		if (!rc) {
  			printf("## Error: \"%s\" not defined
  ", argv[i]);
4c94f6c54   Mike Frysinger   nvedit: speed up ...
148
  			++rcode;
a68d3ed0a   wdenk   Initial revision
149
150
  		}
  	}
4c94f6c54   Mike Frysinger   nvedit: speed up ...
151

a68d3ed0a   wdenk   Initial revision
152
153
  	return rcode;
  }
a000b7950   Kim Phillips   common: add a gre...
154
  #ifdef CONFIG_CMD_GREPENV
d09b1787a   Igor Grinberg   env: clean cmd_nv...
155
156
  static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
  		       int argc, char * const argv[])
a000b7950   Kim Phillips   common: add a gre...
157
  {
5a31ea04c   Wolfgang Denk   "env grep" - reim...
158
  	char *res = NULL;
be29df6a1   Wolfgang Denk   "env grep" - add ...
159
  	int len, grep_how, grep_what;
a000b7950   Kim Phillips   common: add a gre...
160
161
  
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
162
  		return CMD_RET_USAGE;
a000b7950   Kim Phillips   common: add a gre...
163

be29df6a1   Wolfgang Denk   "env grep" - add ...
164
165
  	grep_how  = H_MATCH_SUBSTR;	/* default: substring search	*/
  	grep_what = H_MATCH_BOTH;	/* default: grep names and values */
d87244d5a   Wolfgang Denk   "env grep" - add ...
166

9a8323311   Pierre Aubert   env: fix the env ...
167
168
  	while (--argc > 0 && **++argv == '-') {
  		char *arg = *argv;
d87244d5a   Wolfgang Denk   "env grep" - add ...
169
170
  		while (*++arg) {
  			switch (*arg) {
be29df6a1   Wolfgang Denk   "env grep" - add ...
171
172
173
174
175
  #ifdef CONFIG_REGEX
  			case 'e':		/* use regex matching */
  				grep_how  = H_MATCH_REGEX;
  				break;
  #endif
d87244d5a   Wolfgang Denk   "env grep" - add ...
176
  			case 'n':		/* grep for name */
be29df6a1   Wolfgang Denk   "env grep" - add ...
177
  				grep_what = H_MATCH_KEY;
d87244d5a   Wolfgang Denk   "env grep" - add ...
178
179
  				break;
  			case 'v':		/* grep for value */
be29df6a1   Wolfgang Denk   "env grep" - add ...
180
  				grep_what = H_MATCH_DATA;
d87244d5a   Wolfgang Denk   "env grep" - add ...
181
182
  				break;
  			case 'b':		/* grep for both */
be29df6a1   Wolfgang Denk   "env grep" - add ...
183
  				grep_what = H_MATCH_BOTH;
d87244d5a   Wolfgang Denk   "env grep" - add ...
184
185
186
187
188
189
190
191
192
193
  				break;
  			case '-':
  				goto DONE;
  			default:
  				return CMD_RET_USAGE;
  			}
  		}
  	}
  
  DONE:
5a31ea04c   Wolfgang Denk   "env grep" - reim...
194
195
  	len = hexport_r(&env_htab, '
  ',
be29df6a1   Wolfgang Denk   "env grep" - add ...
196
  			flag | grep_what | grep_how,
5a31ea04c   Wolfgang Denk   "env grep" - reim...
197
  			&res, 0, argc, argv);
a000b7950   Kim Phillips   common: add a gre...
198

5a31ea04c   Wolfgang Denk   "env grep" - reim...
199
200
201
  	if (len > 0) {
  		puts(res);
  		free(res);
a000b7950   Kim Phillips   common: add a gre...
202
  	}
5a31ea04c   Wolfgang Denk   "env grep" - reim...
203
204
205
206
  	if (len < 2)
  		return 1;
  
  	return 0;
a000b7950   Kim Phillips   common: add a gre...
207
208
  }
  #endif
7ac2fe2da   Ilya Yanok   OMAP: networking ...
209
  #endif /* CONFIG_SPL_BUILD */
a000b7950   Kim Phillips   common: add a gre...
210

ea882baf9   Wolfgang Denk   New implementatio...
211
  /*
c3f652585   Gerlando Falauto   env: unify logic ...
212
213
   * Set a new environment variable,
   * or replace or delete an existing one.
2598090b7   Joe Hershberger   env: Add environm...
214
   */
94b467b14   Joe Hershberger   env: Distinguish ...
215
  static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
c3f652585   Gerlando Falauto   env: unify logic ...
216
217
218
219
  {
  	int   i, len;
  	char  *name, *value, *s;
  	ENTRY e, *ep;
24ab5a191   Joe Hershberger   env: Add setenv f...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  	debug("Initial value for argc=%d
  ", 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
  ", argc);
c3f652585   Gerlando Falauto   env: unify logic ...
238
239
240
241
242
243
244
245
246
247
248
  	name = argv[1];
  	value = argv[2];
  
  	if (strchr(name, '=')) {
  		printf("## Error: illegal character '='"
  		       "in variable name \"%s\"
  ", name);
  		return 1;
  	}
  
  	env_id++;
c3f652585   Gerlando Falauto   env: unify logic ...
249

a68d3ed0a   wdenk   Initial revision
250
  	/* Delete only ? */
d09b1787a   Igor Grinberg   env: clean cmd_nv...
251
  	if (argc < 3 || argv[2] == NULL) {
24ab5a191   Joe Hershberger   env: Add setenv f...
252
  		int rc = hdelete_r(name, &env_htab, env_flag);
ea882baf9   Wolfgang Denk   New implementatio...
253
  		return !rc;
a68d3ed0a   wdenk   Initial revision
254
255
256
  	}
  
  	/*
ea882baf9   Wolfgang Denk   New implementatio...
257
  	 * Insert / replace new value
a68d3ed0a   wdenk   Initial revision
258
  	 */
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
259
  	for (i = 2, len = 0; i < argc; ++i)
a68d3ed0a   wdenk   Initial revision
260
  		len += strlen(argv[i]) + 1;
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
261
262
263
  
  	value = malloc(len);
  	if (value == NULL) {
ea882baf9   Wolfgang Denk   New implementatio...
264
265
  		printf("## Can't malloc %d bytes
  ", len);
a68d3ed0a   wdenk   Initial revision
266
267
  		return 1;
  	}
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
268
  	for (i = 2, s = value; i < argc; ++i) {
ea882baf9   Wolfgang Denk   New implementatio...
269
  		char *v = argv[i];
a68d3ed0a   wdenk   Initial revision
270

ea882baf9   Wolfgang Denk   New implementatio...
271
  		while ((*s++ = *v++) != '\0')
a68d3ed0a   wdenk   Initial revision
272
  			;
d09b1787a   Igor Grinberg   env: clean cmd_nv...
273
  		*(s - 1) = ' ';
ea882baf9   Wolfgang Denk   New implementatio...
274
275
276
  	}
  	if (s != value)
  		*--s = '\0';
d09b1787a   Igor Grinberg   env: clean cmd_nv...
277
278
  	e.key	= name;
  	e.data	= value;
24ab5a191   Joe Hershberger   env: Add setenv f...
279
  	hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
ea882baf9   Wolfgang Denk   New implementatio...
280
281
282
283
284
285
  	free(value);
  	if (!ep) {
  		printf("## Error inserting \"%s\" variable, errno=%d
  ",
  			name, errno);
  		return 1;
a68d3ed0a   wdenk   Initial revision
286
  	}
a68d3ed0a   wdenk   Initial revision
287

a68d3ed0a   wdenk   Initial revision
288
289
  	return 0;
  }
84b5e8022   Wolfgang Denk   Constify getenv()...
290
  int setenv(const char *varname, const char *varvalue)
a68d3ed0a   wdenk   Initial revision
291
  {
84b5e8022   Wolfgang Denk   Constify getenv()...
292
  	const char * const argv[4] = { "setenv", varname, varvalue, NULL };
a7eb1d66c   Joe Hershberger   mtd: Make mtdpart...
293
294
295
  	/* before import into hashtable */
  	if (!(gd->flags & GD_FLG_ENV_READY))
  		return 1;
d09b1787a   Igor Grinberg   env: clean cmd_nv...
296
  	if (varvalue == NULL || varvalue[0] == '\0')
94b467b14   Joe Hershberger   env: Distinguish ...
297
  		return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
9ffd451af   Jeffrey Mann   [patch] setenv(.....
298
  	else
94b467b14   Joe Hershberger   env: Distinguish ...
299
  		return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
a68d3ed0a   wdenk   Initial revision
300
  }
d67f10ce0   Simon Glass   Add setenv_ulong(...
301
302
303
  /**
   * Set an environment variable to an integer value
   *
9602286d3   Simon Glass   env: Fix minor co...
304
   * @param varname	Environment variable to set
d67f10ce0   Simon Glass   Add setenv_ulong(...
305
306
307
308
309
310
311
312
313
314
315
316
   * @param value		Value to set it to
   * @return 0 if ok, 1 on error
   */
  int setenv_ulong(const char *varname, ulong value)
  {
  	/* TODO: this should be unsigned */
  	char *str = simple_itoa(value);
  
  	return setenv(varname, str);
  }
  
  /**
bfc599664   Simon Glass   Update set_workin...
317
   * Set an environment variable to an value in hex
d67f10ce0   Simon Glass   Add setenv_ulong(...
318
   *
9602286d3   Simon Glass   env: Fix minor co...
319
   * @param varname	Environment variable to set
bfc599664   Simon Glass   Update set_workin...
320
   * @param value		Value to set it to
d67f10ce0   Simon Glass   Add setenv_ulong(...
321
322
   * @return 0 if ok, 1 on error
   */
bfc599664   Simon Glass   Update set_workin...
323
  int setenv_hex(const char *varname, ulong value)
d67f10ce0   Simon Glass   Add setenv_ulong(...
324
325
  {
  	char str[17];
bfc599664   Simon Glass   Update set_workin...
326
  	sprintf(str, "%lx", value);
d67f10ce0   Simon Glass   Add setenv_ulong(...
327
328
  	return setenv(varname, str);
  }
76b8f79c2   Simon Glass   Add getenv_hex() ...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  ulong getenv_hex(const char *varname, ulong default_val)
  {
  	const char *s;
  	ulong value;
  	char *endp;
  
  	s = getenv(varname);
  	if (s)
  		value = simple_strtoul(s, &endp, 16);
  	if (!s || endp == s)
  		return default_val;
  
  	return value;
  }
7ac2fe2da   Ilya Yanok   OMAP: networking ...
343
  #ifndef CONFIG_SPL_BUILD
088f1b199   Kim Phillips   common/cmd_*.c: s...
344
  static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a68d3ed0a   wdenk   Initial revision
345
  {
47e26b1bf   Wolfgang Denk   cmd_usage(): simp...
346
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
347
  		return CMD_RET_USAGE;
a68d3ed0a   wdenk   Initial revision
348

94b467b14   Joe Hershberger   env: Distinguish ...
349
  	return _do_env_set(flag, argc, argv, H_INTERACTIVE);
a68d3ed0a   wdenk   Initial revision
350
  }
ea882baf9   Wolfgang Denk   New implementatio...
351
  /*
a68d3ed0a   wdenk   Initial revision
352
353
   * Prompt for environment variable
   */
c76fe4742   Jon Loeliger   common/cmd_[i-n]*...
354
  #if defined(CONFIG_CMD_ASKENV)
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
355
  int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a68d3ed0a   wdenk   Initial revision
356
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
357
  	char message[CONFIG_SYS_CBSIZE];
7d85591dd   Wolfgang Denk   env: fix "env ask...
358
  	int i, len, pos, size;
a68d3ed0a   wdenk   Initial revision
359
  	char *local_args[4];
7d85591dd   Wolfgang Denk   env: fix "env ask...
360
  	char *endptr;
a68d3ed0a   wdenk   Initial revision
361
362
363
364
365
  
  	local_args[0] = argv[0];
  	local_args[1] = argv[1];
  	local_args[2] = NULL;
  	local_args[3] = NULL;
7d85591dd   Wolfgang Denk   env: fix "env ask...
366
367
368
369
370
371
  	/*
  	 * Check the syntax:
  	 *
  	 * env_ask envname [message1 ...] [size]
  	 */
  	if (argc == 1)
4c12eeb8b   Simon Glass   Convert cmd_usage...
372
  		return CMD_RET_USAGE;
a68d3ed0a   wdenk   Initial revision
373

7d85591dd   Wolfgang Denk   env: fix "env ask...
374
375
376
377
378
379
380
381
382
383
384
385
386
  	/*
  	 * 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;
  	}
a68d3ed0a   wdenk   Initial revision
387

7d85591dd   Wolfgang Denk   env: fix "env ask...
388
389
390
391
392
  	if (argc <= 2) {
  		sprintf(message, "Please enter '%s': ", argv[1]);
  	} else {
  		/* env_ask envname message1 ... messagen [size] */
  		for (i = 2, pos = 0; i < argc; i++) {
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
393
  			if (pos)
a68d3ed0a   wdenk   Initial revision
394
  				message[pos++] = ' ';
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
395

d09b1787a   Igor Grinberg   env: clean cmd_nv...
396
  			strcpy(message + pos, argv[i]);
a68d3ed0a   wdenk   Initial revision
397
398
  			pos += strlen(argv[i]);
  		}
7d85591dd   Wolfgang Denk   env: fix "env ask...
399
  		message[pos++] = ' ';
a68d3ed0a   wdenk   Initial revision
400
  		message[pos] = '\0';
a68d3ed0a   wdenk   Initial revision
401
  	}
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
402
403
  	if (size >= CONFIG_SYS_CBSIZE)
  		size = CONFIG_SYS_CBSIZE - 1;
a68d3ed0a   wdenk   Initial revision
404
405
406
407
408
  
  	if (size <= 0)
  		return 1;
  
  	/* prompt for input */
e1bf824df   Simon Glass   Add cli_ prefix t...
409
  	len = cli_readline(message);
a68d3ed0a   wdenk   Initial revision
410
411
412
413
414
415
416
417
418
419
420
  
  	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 */
94b467b14   Joe Hershberger   env: Distinguish ...
421
  	return _do_env_set(flag, len, local_args, H_INTERACTIVE);
a68d3ed0a   wdenk   Initial revision
422
  }
902531788   Jon Loeliger   common/: Remove l...
423
  #endif
a68d3ed0a   wdenk   Initial revision
424

5e2b3e0c5   Joe Hershberger   env: Add a comman...
425
  #if defined(CONFIG_CMD_ENV_CALLBACK)
cca98fd6a   Joe Hershberger   env: Allow env_at...
426
427
  static int print_static_binding(const char *var_name, const char *callback_name,
  				void *priv)
5e2b3e0c5   Joe Hershberger   env: Add a comman...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
  {
  	printf("\t%-20s %-20s
  ", 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
  ", entry->key, entry->callback);
  	else
  		printf("\t%-20s %-20s
  ", 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:
  ");
  	puts("\tCallback Name
  ");
  	puts("\t-------------
  ");
  	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
  ", clbkp->name);
  	puts("
  ");
  
  	/* Print the static bindings that may exist */
  	puts("Static callback bindings:
  ");
  	printf("\t%-20s %-20s
  ", "Variable Name", "Callback Name");
  	printf("\t%-20s %-20s
  ", "-------------", "-------------");
cca98fd6a   Joe Hershberger   env: Allow env_at...
500
  	env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
5e2b3e0c5   Joe Hershberger   env: Add a comman...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  	puts("
  ");
  
  	/* walk through each variable and print the callback if it has one */
  	puts("Active callback bindings:
  ");
  	printf("\t%-20s %-20s
  ", "Variable Name", "Callback Name");
  	printf("\t%-20s %-20s
  ", "-------------", "-------------");
  	hwalk_r(&env_htab, print_active_callback);
  	return 0;
  }
  #endif
fffad71bc   Joe Hershberger   env: Add a comman...
515
  #if defined(CONFIG_CMD_ENV_FLAGS)
cca98fd6a   Joe Hershberger   env: Allow env_at...
516
517
  static int print_static_flags(const char *var_name, const char *flags,
  			      void *priv)
fffad71bc   Joe Hershberger   env: Add a comman...
518
519
  {
  	enum env_flags_vartype type = env_flags_parse_vartype(flags);
267541f77   Joe Hershberger   env: Add support ...
520
  	enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
fffad71bc   Joe Hershberger   env: Add a comman...
521

267541f77   Joe Hershberger   env: Add support ...
522
523
524
525
  	printf("\t%-20s %-20s %-20s
  ", var_name,
  		env_flags_get_vartype_name(type),
  		env_flags_get_varaccess_name(access));
fffad71bc   Joe Hershberger   env: Add a comman...
526
527
528
529
530
531
532
  
  	return 0;
  }
  
  static int print_active_flags(ENTRY *entry)
  {
  	enum env_flags_vartype type;
267541f77   Joe Hershberger   env: Add support ...
533
  	enum env_flags_varaccess access;
fffad71bc   Joe Hershberger   env: Add a comman...
534
535
536
537
538
539
  
  	if (entry->flags == 0)
  		return 0;
  
  	type = (enum env_flags_vartype)
  		(entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
267541f77   Joe Hershberger   env: Add support ...
540
541
542
543
544
  	access = env_flags_parse_varaccess_from_binflags(entry->flags);
  	printf("\t%-20s %-20s %-20s
  ", entry->key,
  		env_flags_get_vartype_name(type),
  		env_flags_get_varaccess_name(access));
fffad71bc   Joe Hershberger   env: Add a comman...
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  
  	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):
  ",
  		ENV_FLAGS_VARTYPE_LOC);
  	puts("\tFlag\tVariable Type Name
  ");
  	puts("\t----\t------------------
  ");
  	env_flags_print_vartypes();
  	puts("
  ");
267541f77   Joe Hershberger   env: Add support ...
565
566
567
568
569
570
571
572
573
574
575
  	/* Print the available variable access types */
  	printf("Available variable access flags (position %d):
  ",
  		ENV_FLAGS_VARACCESS_LOC);
  	puts("\tFlag\tVariable Access Name
  ");
  	puts("\t----\t--------------------
  ");
  	env_flags_print_varaccess();
  	puts("
  ");
fffad71bc   Joe Hershberger   env: Add a comman...
576
577
578
  	/* Print the static flags that may exist */
  	puts("Static flags:
  ");
267541f77   Joe Hershberger   env: Add support ...
579
580
581
582
583
584
  	printf("\t%-20s %-20s %-20s
  ", "Variable Name", "Variable Type",
  		"Variable Access");
  	printf("\t%-20s %-20s %-20s
  ", "-------------", "-------------",
  		"---------------");
cca98fd6a   Joe Hershberger   env: Allow env_at...
585
  	env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
fffad71bc   Joe Hershberger   env: Add a comman...
586
587
588
589
590
591
  	puts("
  ");
  
  	/* walk through each variable and print the flags if non-default */
  	puts("Active flags:
  ");
267541f77   Joe Hershberger   env: Add support ...
592
593
594
595
596
597
  	printf("\t%-20s %-20s %-20s
  ", "Variable Name", "Variable Type",
  		"Variable Access");
  	printf("\t%-20s %-20s %-20s
  ", "-------------", "-------------",
  		"---------------");
fffad71bc   Joe Hershberger   env: Add a comman...
598
599
600
601
  	hwalk_r(&env_htab, print_active_flags);
  	return 0;
  }
  #endif
ea882baf9   Wolfgang Denk   New implementatio...
602
  /*
246c69225   Peter Tyser   Add 'editenv' com...
603
604
605
   * Interactively edit an environment variable
   */
  #if defined(CONFIG_CMD_EDITENV)
088f1b199   Kim Phillips   common/cmd_*.c: s...
606
607
  static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
  		       char * const argv[])
246c69225   Peter Tyser   Add 'editenv' com...
608
609
610
  {
  	char buffer[CONFIG_SYS_CBSIZE];
  	char *init_val;
246c69225   Peter Tyser   Add 'editenv' com...
611

47e26b1bf   Wolfgang Denk   cmd_usage(): simp...
612
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
613
  		return CMD_RET_USAGE;
246c69225   Peter Tyser   Add 'editenv' com...
614

94b467b14   Joe Hershberger   env: Distinguish ...
615
616
617
  	/* before import into hashtable */
  	if (!(gd->flags & GD_FLG_ENV_READY))
  		return 1;
246c69225   Peter Tyser   Add 'editenv' com...
618
619
620
  	/* Set read buffer to initial value or empty sting */
  	init_val = getenv(argv[1]);
  	if (init_val)
5d49b4cdf   Peng Fan   common: nvedit: u...
621
  		snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
246c69225   Peter Tyser   Add 'editenv' com...
622
623
  	else
  		buffer[0] = '\0';
e1bf824df   Simon Glass   Add cli_ prefix t...
624
  	if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
18a3cce9f   Joe Hershberger   env: Avoid clobbe...
625
  		return 1;
246c69225   Peter Tyser   Add 'editenv' com...
626

94b467b14   Joe Hershberger   env: Distinguish ...
627
628
629
630
631
632
633
634
635
636
  	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);
  	}
246c69225   Peter Tyser   Add 'editenv' com...
637
638
  }
  #endif /* CONFIG_CMD_EDITENV */
7ac2fe2da   Ilya Yanok   OMAP: networking ...
639
  #endif /* CONFIG_SPL_BUILD */
246c69225   Peter Tyser   Add 'editenv' com...
640

ea882baf9   Wolfgang Denk   New implementatio...
641
  /*
a68d3ed0a   wdenk   Initial revision
642
643
644
645
   * Look up variable from environment,
   * return address of storage for that variable,
   * or NULL if not found
   */
84b5e8022   Wolfgang Denk   Constify getenv()...
646
  char *getenv(const char *name)
a68d3ed0a   wdenk   Initial revision
647
  {
d09b1787a   Igor Grinberg   env: clean cmd_nv...
648
  	if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
ea882baf9   Wolfgang Denk   New implementatio...
649
  		ENTRY e, *ep;
a68d3ed0a   wdenk   Initial revision
650

91a76751a   Wolfgang Denk   Make getenv() wor...
651
  		WATCHDOG_RESET();
2a3cb0207   wdenk   Quick & Dirty fix...
652

d09b1787a   Igor Grinberg   env: clean cmd_nv...
653
654
  		e.key	= name;
  		e.data	= NULL;
c4e0057fa   Joe Hershberger   env: Refactor do_...
655
  		hsearch_r(e, FIND, &ep, &env_htab, 0);
91a76751a   Wolfgang Denk   Make getenv() wor...
656

f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
657
  		return ep ? ep->data : NULL;
a68d3ed0a   wdenk   Initial revision
658
  	}
ea882baf9   Wolfgang Denk   New implementatio...
659
  	/* restricted capabilities before import */
ea882baf9   Wolfgang Denk   New implementatio...
660
661
  	if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
  		return (char *)(gd->env_buf);
91a76751a   Wolfgang Denk   Make getenv() wor...
662

ea882baf9   Wolfgang Denk   New implementatio...
663
  	return NULL;
a68d3ed0a   wdenk   Initial revision
664
  }
ea882baf9   Wolfgang Denk   New implementatio...
665
666
667
  /*
   * Look up variable from environment for restricted C runtime env.
   */
84b5e8022   Wolfgang Denk   Constify getenv()...
668
  int getenv_f(const char *name, char *buf, unsigned len)
a68d3ed0a   wdenk   Initial revision
669
670
  {
  	int i, nxt;
d09b1787a   Igor Grinberg   env: clean cmd_nv...
671
  	for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
a68d3ed0a   wdenk   Initial revision
672
  		int val, n;
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
673
674
675
  		for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
  			if (nxt >= CONFIG_ENV_SIZE)
  				return -1;
a68d3ed0a   wdenk   Initial revision
676
  		}
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
677
678
679
  
  		val = envmatch((uchar *)name, i);
  		if (val < 0)
a68d3ed0a   wdenk   Initial revision
680
  			continue;
9ed4a9582   Wolfgang Denk   getenv_f(): fix h...
681

a68d3ed0a   wdenk   Initial revision
682
  		/* found; copy out */
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
683
  		for (n = 0; n < len; ++n, ++buf) {
d09b1787a   Igor Grinberg   env: clean cmd_nv...
684
685
  			*buf = env_get_char(val++);
  			if (*buf == '\0')
9ed4a9582   Wolfgang Denk   getenv_f(): fix h...
686
687
688
689
690
  				return n;
  		}
  
  		if (n)
  			*--buf = '\0';
a02a884b9   Wolfgang Denk   cmd_nvedit.c: mak...
691
692
693
  		printf("env_buf [%d bytes] too small for value of \"%s\"
  ",
  			len, name);
9ed4a9582   Wolfgang Denk   getenv_f(): fix h...
694
695
  
  		return n;
a68d3ed0a   wdenk   Initial revision
696
  	}
d09b1787a   Igor Grinberg   env: clean cmd_nv...
697

f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
698
  	return -1;
a68d3ed0a   wdenk   Initial revision
699
  }
4a9b41310   Simon Glass   Add getenv_ulong(...
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  /**
   * Decode the integer value of an environment variable and return it.
   *
   * @param name		Name of environemnt variable
   * @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
   */
  ulong getenv_ulong(const char *name, int base, ulong default_val)
  {
  	/*
  	 * We can use getenv() here, even before relocation, since the
  	 * environment variable value is an integer and thus short.
  	 */
  	const char *str = getenv(name);
  
  	return str ? simple_strtoul(str, NULL, base) : default_val;
  }
7ac2fe2da   Ilya Yanok   OMAP: networking ...
719
  #ifndef CONFIG_SPL_BUILD
bdab39d35   Mike Frysinger   rename CONFIG_CMD...
720
  #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
088f1b199   Kim Phillips   common/cmd_*.c: s...
721
722
  static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
  		       char * const argv[])
a68d3ed0a   wdenk   Initial revision
723
  {
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
724
725
  	printf("Saving Environment to %s...
  ", env_name_spec);
a68d3ed0a   wdenk   Initial revision
726

f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
727
  	return saveenv() ? 1 : 0;
a68d3ed0a   wdenk   Initial revision
728
  }
8bde7f776   wdenk   * Code cleanup:
729

ba69dc26a   Mike Frysinger   saveenv: standard...
730
  U_BOOT_CMD(
ea882baf9   Wolfgang Denk   New implementatio...
731
  	saveenv, 1, 0,	do_env_save,
2fb2604d5   Peter Tyser   Command usage cle...
732
  	"save environment variables to persistent storage",
a89c33db9   Wolfgang Denk   General help mess...
733
  	""
ba69dc26a   Mike Frysinger   saveenv: standard...
734
  );
a68d3ed0a   wdenk   Initial revision
735
  #endif
7ac2fe2da   Ilya Yanok   OMAP: networking ...
736
  #endif /* CONFIG_SPL_BUILD */
a68d3ed0a   wdenk   Initial revision
737

ea882baf9   Wolfgang Denk   New implementatio...
738
  /*
a68d3ed0a   wdenk   Initial revision
739
740
741
742
   * 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.
d09b1787a   Igor Grinberg   env: clean cmd_nv...
743
   * If the names match, return the index for the value2, else -1.
a68d3ed0a   wdenk   Initial revision
744
   */
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
745
  int envmatch(uchar *s1, int i2)
a68d3ed0a   wdenk   Initial revision
746
  {
586197dfe   Joe Hershberger   env: Check for NU...
747
748
  	if (s1 == NULL)
  		return -1;
a68d3ed0a   wdenk   Initial revision
749
750
  	while (*s1 == env_get_char(i2++))
  		if (*s1++ == '=')
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
751
  			return i2;
d09b1787a   Igor Grinberg   env: clean cmd_nv...
752

a68d3ed0a   wdenk   Initial revision
753
  	if (*s1 == '\0' && env_get_char(i2-1) == '=')
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
754
  		return i2;
d09b1787a   Igor Grinberg   env: clean cmd_nv...
755

f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
756
  	return -1;
a68d3ed0a   wdenk   Initial revision
757
  }
8bde7f776   wdenk   * Code cleanup:
758

7ac2fe2da   Ilya Yanok   OMAP: networking ...
759
  #ifndef CONFIG_SPL_BUILD
b64b7c3df   Gerlando Falauto   env: make "env de...
760
  static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
d09b1787a   Igor Grinberg   env: clean cmd_nv...
761
  			  int argc, char * const argv[])
ea882baf9   Wolfgang Denk   New implementatio...
762
  {
b64b7c3df   Gerlando Falauto   env: make "env de...
763
  	int all = 0, flag = 0;
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
764

b64b7c3df   Gerlando Falauto   env: make "env de...
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
  	debug("Initial value for argc=%d
  ", argc);
  	while (--argc > 0 && **++argv == '-') {
  		char *arg = *argv;
  
  		while (*++arg) {
  			switch (*arg) {
  			case 'a':		/* default all */
  				all = 1;
  				break;
  			case 'f':		/* force */
  				flag |= H_FORCE;
  				break;
  			default:
  				return cmd_usage(cmdtp);
  			}
  		}
  	}
  	debug("Final value for argc=%d
  ", argc);
  	if (all && (argc == 0)) {
  		/* Reset the whole environment */
  		set_default_env("## Resetting to default environment
  ");
  		return 0;
  	}
  	if (!all && (argc > 0)) {
  		/* Reset individual variables */
  		set_default_vars(argc, argv);
  		return 0;
  	}
  
  	return cmd_usage(cmdtp);
ea882baf9   Wolfgang Denk   New implementatio...
798
  }
d09b1787a   Igor Grinberg   env: clean cmd_nv...
799
800
  static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
  			 int argc, char * const argv[])
ea882baf9   Wolfgang Denk   New implementatio...
801
  {
9d8d661d7   Joe Hershberger   env: Implement th...
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
  	int env_flag = H_INTERACTIVE;
  	int ret = 0;
  
  	debug("Initial value for argc=%d
  ", 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
  ", argc);
  
  	env_id++;
  
  	while (--argc > 0) {
  		char *name = *++argv;
  
  		if (!hdelete_r(name, &env_htab, env_flag))
  			ret = 1;
  	}
  
  	return ret;
ea882baf9   Wolfgang Denk   New implementatio...
834
  }
0c79cda01   Mike Frysinger   env: make import/...
835
  #ifdef CONFIG_CMD_EXPORTENV
ea882baf9   Wolfgang Denk   New implementatio...
836
  /*
37f2fe747   Wolfgang Denk   env: allow to exp...
837
   * env export [-t | -b | -c] [-s size] addr [var ...]
ea882baf9   Wolfgang Denk   New implementatio...
838
839
840
841
842
843
844
845
846
   *	-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
37f2fe747   Wolfgang Denk   env: allow to exp...
847
848
   *	-s size:
   *		size of output buffer
ea882baf9   Wolfgang Denk   New implementatio...
849
   *	addr:	memory address where environment gets stored
37f2fe747   Wolfgang Denk   env: allow to exp...
850
851
852
   *	var...	List of variable names that get included into the
   *		export. Without arguments, the whole environment gets
   *		exported.
ea882baf9   Wolfgang Denk   New implementatio...
853
854
855
856
   *
   * 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
fc0b5948e   Robert P. J. Day   Various, accumula...
857
   * prepend a valid CRC32 checksum and, in case of redundant
ea882baf9   Wolfgang Denk   New implementatio...
858
859
860
861
862
863
864
865
   * 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
fc0b5948e   Robert P. J. Day   Various, accumula...
866
   * sufficient. Any remaining space will be '\0' padded.
ea882baf9   Wolfgang Denk   New implementatio...
867
868
869
870
   *
   * On successful return, the variable "filesize" will be set.
   * Note that filesize includes the trailing/terminating '\0' byte(s).
   *
fc0b5948e   Robert P. J. Day   Various, accumula...
871
   * Usage scenario:  create a text snapshot/backup of the current settings:
ea882baf9   Wolfgang Denk   New implementatio...
872
873
874
875
876
877
878
879
880
   *
   *	=> 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}
   */
d09b1787a   Igor Grinberg   env: clean cmd_nv...
881
882
  static int do_env_export(cmd_tbl_t *cmdtp, int flag,
  			 int argc, char * const argv[])
ea882baf9   Wolfgang Denk   New implementatio...
883
884
  {
  	char	buf[32];
fd37dac9e   Simon Glass   sandbox: Support ...
885
886
  	ulong	addr;
  	char	*ptr, *cmd, *res;
37f2fe747   Wolfgang Denk   env: allow to exp...
887
  	size_t	size = 0;
ea882baf9   Wolfgang Denk   New implementatio...
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
  	ssize_t	len;
  	env_t	*envp;
  	char	sep = '
  ';
  	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;
37f2fe747   Wolfgang Denk   env: allow to exp...
912
913
914
915
916
  			case 's':		/* size given */
  				if (--argc <= 0)
  					return cmd_usage(cmdtp);
  				size = simple_strtoul(*++argv, NULL, 16);
  				goto NXTARG;
ea882baf9   Wolfgang Denk   New implementatio...
917
918
919
920
921
922
923
  			case 't':		/* text format */
  				if (fmt++)
  					goto sep_err;
  				sep = '
  ';
  				break;
  			default:
4c12eeb8b   Simon Glass   Convert cmd_usage...
924
  				return CMD_RET_USAGE;
ea882baf9   Wolfgang Denk   New implementatio...
925
926
  			}
  		}
37f2fe747   Wolfgang Denk   env: allow to exp...
927
  NXTARG:		;
ea882baf9   Wolfgang Denk   New implementatio...
928
  	}
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
929
  	if (argc < 1)
4c12eeb8b   Simon Glass   Convert cmd_usage...
930
  		return CMD_RET_USAGE;
8bde7f776   wdenk   * Code cleanup:
931

fd37dac9e   Simon Glass   sandbox: Support ...
932
933
  	addr = simple_strtoul(argv[0], NULL, 16);
  	ptr = map_sysmem(addr, size);
ea882baf9   Wolfgang Denk   New implementatio...
934

37f2fe747   Wolfgang Denk   env: allow to exp...
935
  	if (size)
fd37dac9e   Simon Glass   sandbox: Support ...
936
  		memset(ptr, '\0', size);
37f2fe747   Wolfgang Denk   env: allow to exp...
937
938
939
  
  	argc--;
  	argv++;
ea882baf9   Wolfgang Denk   New implementatio...
940
941
  
  	if (sep) {		/* export as text file */
ea009d474   Wolfgang Denk   hashtable: prepar...
942
943
  		len = hexport_r(&env_htab, sep,
  				H_MATCH_KEY | H_MATCH_IDENT,
fd37dac9e   Simon Glass   sandbox: Support ...
944
  				&ptr, size, argc, argv);
ea882baf9   Wolfgang Denk   New implementatio...
945
  		if (len < 0) {
d09b1787a   Igor Grinberg   env: clean cmd_nv...
946
947
  			error("Cannot export environment: errno = %d
  ", errno);
ea882baf9   Wolfgang Denk   New implementatio...
948
949
  			return 1;
  		}
8c3aff525   Andreas Bießmann   cmd_nvedit: use e...
950
  		sprintf(buf, "%zX", (size_t)len);
ea882baf9   Wolfgang Denk   New implementatio...
951
952
953
954
  		setenv("filesize", buf);
  
  		return 0;
  	}
fd37dac9e   Simon Glass   sandbox: Support ...
955
  	envp = (env_t *)ptr;
ea882baf9   Wolfgang Denk   New implementatio...
956
957
958
959
  
  	if (chk)		/* export as checksum protected block */
  		res = (char *)envp->data;
  	else			/* export as raw binary data */
fd37dac9e   Simon Glass   sandbox: Support ...
960
  		res = ptr;
ea882baf9   Wolfgang Denk   New implementatio...
961

ea009d474   Wolfgang Denk   hashtable: prepar...
962
963
964
  	len = hexport_r(&env_htab, '\0',
  			H_MATCH_KEY | H_MATCH_IDENT,
  			&res, ENV_SIZE, argc, argv);
ea882baf9   Wolfgang Denk   New implementatio...
965
  	if (len < 0) {
d09b1787a   Igor Grinberg   env: clean cmd_nv...
966
967
  		error("Cannot export environment: errno = %d
  ", errno);
ea882baf9   Wolfgang Denk   New implementatio...
968
969
970
971
  		return 1;
  	}
  
  	if (chk) {
d09b1787a   Igor Grinberg   env: clean cmd_nv...
972
  		envp->crc = crc32(0, envp->data, ENV_SIZE);
ea882baf9   Wolfgang Denk   New implementatio...
973
974
975
976
  #ifdef CONFIG_ENV_ADDR_REDUND
  		envp->flags = ACTIVE_FLAG;
  #endif
  	}
41ef372c1   Simon Glass   common: Use new n...
977
  	setenv_hex("filesize", len + offsetof(env_t, data));
ea882baf9   Wolfgang Denk   New implementatio...
978
979
980
981
  
  	return 0;
  
  sep_err:
d09b1787a   Igor Grinberg   env: clean cmd_nv...
982
983
  	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed
  ",	cmd);
ea882baf9   Wolfgang Denk   New implementatio...
984
985
  	return 1;
  }
0c79cda01   Mike Frysinger   env: make import/...
986
  #endif
ea882baf9   Wolfgang Denk   New implementatio...
987

0c79cda01   Mike Frysinger   env: make import/...
988
  #ifdef CONFIG_CMD_IMPORTENV
ea882baf9   Wolfgang Denk   New implementatio...
989
  /*
ecd1446fe   Alexander Holler   Add option -r to ...
990
   * env import [-d] [-t [-r] | -b | -c] addr [size]
ea882baf9   Wolfgang Denk   New implementatio...
991
   *	-d:	delete existing environment before importing;
fc0b5948e   Robert P. J. Day   Various, accumula...
992
   *		otherwise overwrite / append to existing definitions
ea882baf9   Wolfgang Denk   New implementatio...
993
994
   *	-t:	assume text format; either "size" must be given or the
   *		text data must be '\0' terminated
ecd1446fe   Alexander Holler   Add option -r to ...
995
996
997
998
   *	-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.
ea882baf9   Wolfgang Denk   New implementatio...
999
1000
1001
1002
1003
1004
   *	-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
   */
d09b1787a   Igor Grinberg   env: clean cmd_nv...
1005
1006
  static int do_env_import(cmd_tbl_t *cmdtp, int flag,
  			 int argc, char * const argv[])
ea882baf9   Wolfgang Denk   New implementatio...
1007
  {
fd37dac9e   Simon Glass   sandbox: Support ...
1008
1009
  	ulong	addr;
  	char	*cmd, *ptr;
ea882baf9   Wolfgang Denk   New implementatio...
1010
1011
1012
1013
1014
  	char	sep = '
  ';
  	int	chk = 0;
  	int	fmt = 0;
  	int	del = 0;
ecd1446fe   Alexander Holler   Add option -r to ...
1015
  	int	crlf_is_lf = 0;
ea882baf9   Wolfgang Denk   New implementatio...
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
  	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 = '
  ';
  				break;
ecd1446fe   Alexander Holler   Add option -r to ...
1041
1042
1043
  			case 'r':		/* handle CRLF like LF */
  				crlf_is_lf = 1;
  				break;
ea882baf9   Wolfgang Denk   New implementatio...
1044
1045
1046
1047
  			case 'd':
  				del = 1;
  				break;
  			default:
4c12eeb8b   Simon Glass   Convert cmd_usage...
1048
  				return CMD_RET_USAGE;
ea882baf9   Wolfgang Denk   New implementatio...
1049
1050
1051
  			}
  		}
  	}
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
1052
  	if (argc < 1)
4c12eeb8b   Simon Glass   Convert cmd_usage...
1053
  		return CMD_RET_USAGE;
ea882baf9   Wolfgang Denk   New implementatio...
1054
1055
1056
1057
  
  	if (!fmt)
  		printf("## Warning: defaulting to text format
  ");
ecd1446fe   Alexander Holler   Add option -r to ...
1058
1059
1060
  	if (sep != '
  ' && crlf_is_lf )
  		crlf_is_lf = 0;
fd37dac9e   Simon Glass   sandbox: Support ...
1061
1062
  	addr = simple_strtoul(argv[0], NULL, 16);
  	ptr = map_sysmem(addr, 0);
ea882baf9   Wolfgang Denk   New implementatio...
1063
1064
1065
  
  	if (argc == 2) {
  		size = simple_strtoul(argv[1], NULL, 16);
3775dcd9c   Tom Rini   cmd_nvedit: Make ...
1066
1067
1068
1069
  	} else if (argc == 1 && chk) {
  		puts("## Error: external checksum format must pass size
  ");
  		return CMD_RET_FAILURE;
ea882baf9   Wolfgang Denk   New implementatio...
1070
  	} else {
fd37dac9e   Simon Glass   sandbox: Support ...
1071
  		char *s = ptr;
ea882baf9   Wolfgang Denk   New implementatio...
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
  
  		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
  ", MAX_ENV_SIZE);
  		}
d3f80c77c   Horst Kronstorfer   common/cmd_nvedit...
1086
  		size += 2;
79afc88d2   Simon Glass   Fix warnings in c...
1087
1088
  		printf("## Info: input data size = %zu = 0x%zX
  ", size, size);
ea882baf9   Wolfgang Denk   New implementatio...
1089
1090
1091
1092
  	}
  
  	if (chk) {
  		uint32_t crc;
fd37dac9e   Simon Glass   sandbox: Support ...
1093
  		env_t *ep = (env_t *)ptr;
ea882baf9   Wolfgang Denk   New implementatio...
1094
1095
1096
1097
1098
1099
1100
1101
1102
  
  		size -= offsetof(env_t, data);
  		memcpy(&crc, &ep->crc, sizeof(crc));
  
  		if (crc32(0, ep->data, size) != crc) {
  			puts("## Error: bad CRC, import failed
  ");
  			return 1;
  		}
fd37dac9e   Simon Glass   sandbox: Support ...
1103
  		ptr = (char *)ep->data;
ea882baf9   Wolfgang Denk   New implementatio...
1104
  	}
ecd1446fe   Alexander Holler   Add option -r to ...
1105
1106
  	if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
  			crlf_is_lf, 0, NULL) == 0) {
ea882baf9   Wolfgang Denk   New implementatio...
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  		error("Environment import failed: errno = %d
  ", errno);
  		return 1;
  	}
  	gd->flags |= GD_FLG_ENV_READY;
  
  	return 0;
  
  sep_err:
  	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed
  ",
  		cmd);
  	return 1;
  }
0c79cda01   Mike Frysinger   env: make import/...
1121
  #endif
ea882baf9   Wolfgang Denk   New implementatio...
1122

88733e2c6   Andrew Ruder   cmd_nvedit.c: Add...
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
  #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
ea882baf9   Wolfgang Denk   New implementatio...
1139
1140
1141
1142
1143
1144
1145
1146
  /*
   * 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, "", ""),
9d8d661d7   Joe Hershberger   env: Implement th...
1147
  	U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
ea882baf9   Wolfgang Denk   New implementatio...
1148
1149
1150
  #if defined(CONFIG_CMD_EDITENV)
  	U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  #endif
5e2b3e0c5   Joe Hershberger   env: Add a comman...
1151
1152
1153
  #if defined(CONFIG_CMD_ENV_CALLBACK)
  	U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
  #endif
fffad71bc   Joe Hershberger   env: Add a comman...
1154
1155
1156
  #if defined(CONFIG_CMD_ENV_FLAGS)
  	U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
  #endif
0c79cda01   Mike Frysinger   env: make import/...
1157
  #if defined(CONFIG_CMD_EXPORTENV)
ea882baf9   Wolfgang Denk   New implementatio...
1158
  	U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
0c79cda01   Mike Frysinger   env: make import/...
1159
  #endif
a000b7950   Kim Phillips   common: add a gre...
1160
1161
1162
  #if defined(CONFIG_CMD_GREPENV)
  	U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  #endif
0c79cda01   Mike Frysinger   env: make import/...
1163
  #if defined(CONFIG_CMD_IMPORTENV)
ea882baf9   Wolfgang Denk   New implementatio...
1164
  	U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
0c79cda01   Mike Frysinger   env: make import/...
1165
  #endif
ea882baf9   Wolfgang Denk   New implementatio...
1166
1167
1168
1169
1170
1171
1172
1173
  	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
  #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  	U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  #endif
  	U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
88733e2c6   Andrew Ruder   cmd_nvedit.c: Add...
1174
1175
1176
  #if defined(CONFIG_CMD_ENV_EXISTS)
  	U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
  #endif
ea882baf9   Wolfgang Denk   New implementatio...
1177
  };
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
1178
  #if defined(CONFIG_NEEDS_MANUAL_RELOC)
60f7da1f4   Heiko Schocher   env: fix cmd_env_...
1179
1180
1181
1182
1183
  void env_reloc(void)
  {
  	fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  }
  #endif
f3c615b8a   Macpaul Lin   cmd_nvedit.c: cle...
1184
  static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea882baf9   Wolfgang Denk   New implementatio...
1185
1186
  {
  	cmd_tbl_t *cp;
5904da021   Thomas Weber   Common/cmd_nvedit...
1187
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
1188
  		return CMD_RET_USAGE;
5904da021   Thomas Weber   Common/cmd_nvedit...
1189

ea882baf9   Wolfgang Denk   New implementatio...
1190
1191
1192
1193
1194
1195
1196
1197
  	/* 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);
4c12eeb8b   Simon Glass   Convert cmd_usage...
1198
  	return CMD_RET_USAGE;
ea882baf9   Wolfgang Denk   New implementatio...
1199
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
1200
1201
  #ifdef CONFIG_SYS_LONGHELP
  static char env_help_text[] =
ea882baf9   Wolfgang Denk   New implementatio...
1202
1203
1204
1205
  #if defined(CONFIG_CMD_ASKENV)
  	"ask name [message] [size] - ask for environment variable
  env "
  #endif
5e2b3e0c5   Joe Hershberger   env: Add a comman...
1206
1207
1208
1209
  #if defined(CONFIG_CMD_ENV_CALLBACK)
  	"callbacks - print callbacks and their associated variables
  env "
  #endif
b64b7c3df   Gerlando Falauto   env: make "env de...
1210
1211
1212
1213
  	"default [-f] -a - [forcibly] reset default environment
  "
  	"env default [-f] var [...] - [forcibly] reset variable(s) to their default values
  "
9d8d661d7   Joe Hershberger   env: Implement th...
1214
1215
  	"env delete [-f] var [...] - [forcibly] delete variable(s)
  "
ea882baf9   Wolfgang Denk   New implementatio...
1216
1217
1218
1219
  #if defined(CONFIG_CMD_EDITENV)
  	"env edit name - edit environment variable
  "
  #endif
88733e2c6   Andrew Ruder   cmd_nvedit.c: Add...
1220
1221
1222
1223
  #if defined(CONFIG_CMD_ENV_EXISTS)
  	"env exists name - tests for existence of variable
  "
  #endif
4796bc4bb   Benoît Thébaudeau   env import/export...
1224
  #if defined(CONFIG_CMD_EXPORTENV)
37f2fe747   Wolfgang Denk   env: allow to exp...
1225
1226
  	"env export [-t | -b | -c] [-s size] addr [var ...] - export environment
  "
4796bc4bb   Benoît Thébaudeau   env import/export...
1227
  #endif
fffad71bc   Joe Hershberger   env: Add a comman...
1228
1229
1230
1231
  #if defined(CONFIG_CMD_ENV_FLAGS)
  	"env flags - print variables that have non-default flags
  "
  #endif
a000b7950   Kim Phillips   common: add a gre...
1232
  #if defined(CONFIG_CMD_GREPENV)
be29df6a1   Wolfgang Denk   "env grep" - add ...
1233
1234
1235
1236
  #ifdef CONFIG_REGEX
  	"env grep [-e] [-n | -v | -b] string [...] - search environment
  "
  #else
d87244d5a   Wolfgang Denk   "env grep" - add ...
1237
1238
  	"env grep [-n | -v | -b] string [...] - search environment
  "
a000b7950   Kim Phillips   common: add a gre...
1239
  #endif
be29df6a1   Wolfgang Denk   "env grep" - add ...
1240
  #endif
4796bc4bb   Benoît Thébaudeau   env import/export...
1241
  #if defined(CONFIG_CMD_IMPORTENV)
ecd1446fe   Alexander Holler   Add option -r to ...
1242
1243
  	"env import [-d] [-t [-r] | -b | -c] addr [size] - import environment
  "
4796bc4bb   Benoît Thébaudeau   env import/export...
1244
  #endif
be11235ab   Joe Hershberger   env: Hide '.' var...
1245
1246
  	"env print [-a | name ...] - print environment
  "
ea882baf9   Wolfgang Denk   New implementatio...
1247
1248
1249
1250
  #if defined(CONFIG_CMD_RUN)
  	"env run var [...] - run commands in an environment variable
  "
  #endif
d798a9b5d   Horst Kronstorfer   common/cmd_nvedit...
1251
  #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
ea882baf9   Wolfgang Denk   New implementatio...
1252
1253
  	"env save - save environment
  "
d798a9b5d   Horst Kronstorfer   common/cmd_nvedit...
1254
  #endif
088f1b199   Kim Phillips   common/cmd_*.c: s...
1255
1256
1257
1258
1259
1260
1261
  	"env set [-f] name [arg ...]
  ";
  #endif
  
  U_BOOT_CMD(
  	env, CONFIG_SYS_MAXARGS, 1, do_env,
  	"environment handling commands", env_help_text
ea882baf9   Wolfgang Denk   New implementatio...
1262
1263
1264
1265
1266
  );
  
  /*
   * Old command line interface, kept for compatibility
   */
8bde7f776   wdenk   * Code cleanup:
1267

246c69225   Peter Tyser   Add 'editenv' com...
1268
  #if defined(CONFIG_CMD_EDITENV)
722b061b6   Mike Frysinger   autocomplete: rem...
1269
  U_BOOT_CMD_COMPLETE(
ea882baf9   Wolfgang Denk   New implementatio...
1270
  	editenv, 2, 0,	do_env_edit,
246c69225   Peter Tyser   Add 'editenv' com...
1271
1272
1273
  	"edit environment variable",
  	"name
  "
722b061b6   Mike Frysinger   autocomplete: rem...
1274
1275
  	"    - edit environment variable 'name'",
  	var_complete
246c69225   Peter Tyser   Add 'editenv' com...
1276
1277
  );
  #endif
722b061b6   Mike Frysinger   autocomplete: rem...
1278
  U_BOOT_CMD_COMPLETE(
ea882baf9   Wolfgang Denk   New implementatio...
1279
  	printenv, CONFIG_SYS_MAXARGS, 1,	do_env_print,
2fb2604d5   Peter Tyser   Command usage cle...
1280
  	"print environment variables",
be11235ab   Joe Hershberger   env: Hide '.' var...
1281
1282
1283
  	"[-a]
      - print [all] values of all environment variables
  "
8bde7f776   wdenk   * Code cleanup:
1284
1285
  	"printenv name ...
  "
722b061b6   Mike Frysinger   autocomplete: rem...
1286
1287
  	"    - print value of environment variable 'name'",
  	var_complete
8bde7f776   wdenk   * Code cleanup:
1288
  );
a000b7950   Kim Phillips   common: add a gre...
1289
1290
1291
1292
  #ifdef CONFIG_CMD_GREPENV
  U_BOOT_CMD_COMPLETE(
  	grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
  	"search environment variables",
be29df6a1   Wolfgang Denk   "env grep" - add ...
1293
1294
1295
1296
  #ifdef CONFIG_REGEX
  	"[-e] [-n | -v | -b] string ...
  "
  #else
d87244d5a   Wolfgang Denk   "env grep" - add ...
1297
1298
  	"[-n | -v | -b] string ...
  "
be29df6a1   Wolfgang Denk   "env grep" - add ...
1299
  #endif
d87244d5a   Wolfgang Denk   "env grep" - add ...
1300
1301
  	"    - list environment name=value pairs matching 'string'
  "
be29df6a1   Wolfgang Denk   "env grep" - add ...
1302
1303
1304
1305
  #ifdef CONFIG_REGEX
  	"      \"-e\": enable regular expressions;
  "
  #endif
d87244d5a   Wolfgang Denk   "env grep" - add ...
1306
1307
1308
  	"      \"-n\": search variable names; \"-v\": search values;
  "
  	"      \"-b\": search both names and values (default)",
a000b7950   Kim Phillips   common: add a gre...
1309
1310
1311
  	var_complete
  );
  #endif
722b061b6   Mike Frysinger   autocomplete: rem...
1312
  U_BOOT_CMD_COMPLETE(
ea882baf9   Wolfgang Denk   New implementatio...
1313
  	setenv, CONFIG_SYS_MAXARGS, 0,	do_env_set,
2fb2604d5   Peter Tyser   Command usage cle...
1314
  	"set environment variables",
24ab5a191   Joe Hershberger   env: Add setenv f...
1315
1316
1317
1318
1319
1320
1321
  	"[-f] name value ...
  "
  	"    - [forcibly] set environment variable 'name' to 'value ...'
  "
  	"setenv [-f] name
  "
  	"    - [forcibly] delete environment variable 'name'",
722b061b6   Mike Frysinger   autocomplete: rem...
1322
  	var_complete
8bde7f776   wdenk   * Code cleanup:
1323
  );
c76fe4742   Jon Loeliger   common/cmd_[i-n]*...
1324
  #if defined(CONFIG_CMD_ASKENV)
8bde7f776   wdenk   * Code cleanup:
1325

0d4983930   wdenk   Patch by Kenneth ...
1326
  U_BOOT_CMD(
ea882baf9   Wolfgang Denk   New implementatio...
1327
  	askenv,	CONFIG_SYS_MAXARGS,	1,	do_env_ask,
2fb2604d5   Peter Tyser   Command usage cle...
1328
  	"get environment variables from stdin",
8bde7f776   wdenk   * Code cleanup:
1329
1330
  	"name [message] [size]
  "
7d85591dd   Wolfgang Denk   env: fix "env ask...
1331
  	"    - get environment variable 'name' from stdin (max 'size' chars)"
8bde7f776   wdenk   * Code cleanup:
1332
  );
902531788   Jon Loeliger   common/: Remove l...
1333
  #endif
8bde7f776   wdenk   * Code cleanup:
1334

c76fe4742   Jon Loeliger   common/cmd_[i-n]*...
1335
  #if defined(CONFIG_CMD_RUN)
722b061b6   Mike Frysinger   autocomplete: rem...
1336
  U_BOOT_CMD_COMPLETE(
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1337
  	run,	CONFIG_SYS_MAXARGS,	1,	do_run,
2fb2604d5   Peter Tyser   Command usage cle...
1338
  	"run commands in an environment variable",
8bde7f776   wdenk   * Code cleanup:
1339
1340
  	"var [...]
  "
722b061b6   Mike Frysinger   autocomplete: rem...
1341
1342
  	"    - run the commands in the environment variable(s) 'var'",
  	var_complete
8bde7f776   wdenk   * Code cleanup:
1343
  );
902531788   Jon Loeliger   common/: Remove l...
1344
  #endif
7ac2fe2da   Ilya Yanok   OMAP: networking ...
1345
  #endif /* CONFIG_SPL_BUILD */