blob: 9f8d5319596d2a171f55035683c1eca61af21d11 [file] [log] [blame]
wdenka68d3ed2002-10-11 08:38:32 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27/**************************************************************************
28 *
29 * Support for persistent environment data
30 *
31 * The "environment" is stored as a list of '\0' terminated
32 * "name=value" strings. The end of the list is marked by a double
33 * '\0'. New entries are always added at the end. Deleting an entry
34 * shifts the remaining entries to the front. Replacing an entry is a
35 * combination of deleting the old value and adding the new one.
36 *
37 * The environment is preceeded by a 32 bit CRC over the data part.
38 *
39 **************************************************************************
40 */
41
42#include <common.h>
43#include <command.h>
44#include <environment.h>
wdenk2a3cb022002-11-05 21:01:48 +000045#include <watchdog.h>
wdenk7ac16102004-08-01 22:48:16 +000046#include <serial.h>
wdenka68d3ed2002-10-11 08:38:32 +000047#include <linux/stddef.h>
48#include <asm/byteorder.h>
Jon Loeligerd76b5c12007-07-08 18:02:23 -050049#if defined(CONFIG_CMD_NET)
wdenka68d3ed2002-10-11 08:38:32 +000050#include <net.h>
51#endif
52
Wolfgang Denk6405a152006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54
unsik Kimf0b1bdd2009-02-25 11:31:24 +090055#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
Jean-Christophe PLAGNIOL-VILLARD53db4cd2008-09-10 22:48:04 +020056 !defined(CONFIG_ENV_IS_IN_FLASH) && \
Jean-Christophe PLAGNIOL-VILLARD2b14d2b2008-09-10 22:47:58 +020057 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090058 !defined(CONFIG_ENV_IS_IN_MG_DISK) && \
Jean-Christophe PLAGNIOL-VILLARDdda84dd2008-09-10 22:47:58 +020059 !defined(CONFIG_ENV_IS_IN_NAND) && \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090060 !defined(CONFIG_ENV_IS_IN_NVRAM) && \
Jean-Christophe PLAGNIOL-VILLARD75989162008-09-10 22:47:59 +020061 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
Jean-Christophe PLAGNIOL-VILLARD4539b1c2008-09-10 22:48:00 +020062 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
Jean-Christophe PLAGNIOL-VILLARD68a87562008-09-10 22:48:00 +020063 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kimf0b1bdd2009-02-25 11:31:24 +090064# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
65SPI_FLASH|MG_DISK|NVRAM|NOWHERE}
wdenka68d3ed2002-10-11 08:38:32 +000066#endif
67
68#define XMK_STR(x) #x
69#define MK_STR(x) XMK_STR(x)
70
71/************************************************************************
72************************************************************************/
73
wdenka68d3ed2002-10-11 08:38:32 +000074/*
75 * Table with supported baudrates (defined in config_xyz.h)
76 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
wdenka68d3ed2002-10-11 08:38:32 +000078#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
79
Heiko Schocher3b195ff2009-04-28 08:36:11 +020080/*
81 * This variable is incremented on each do_setenv (), so it can
82 * be used via get_env_id() as an indication, if the environment
83 * has changed or not. So it is possible to reread an environment
84 * variable only if the environment was changed ... done so for
85 * example in NetInitLoop()
86 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010087static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000088
Heiko Schocher0c303fa2009-02-10 09:38:52 +010089int get_env_id (void)
90{
91 return env_id;
92}
wdenka68d3ed2002-10-11 08:38:32 +000093/************************************************************************
94 * Command interface: print one or all environment variables
95 */
96
Mike Frysinger87944922009-05-24 02:26:19 -040097/*
98 * state 0: finish printing this string and return (matched!)
99 * state 1: no matching to be done; print everything
100 * state 2: continue searching for matched name
101 */
102static int printenv(char *name, int state)
wdenka68d3ed2002-10-11 08:38:32 +0000103{
Mike Frysinger87944922009-05-24 02:26:19 -0400104 int i, j;
105 char c, buf[17];
wdenka68d3ed2002-10-11 08:38:32 +0000106
Mike Frysinger87944922009-05-24 02:26:19 -0400107 i = 0;
108 buf[16] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000109
Mike Frysinger87944922009-05-24 02:26:19 -0400110 while (state && env_get_char(i) != '\0') {
111 if (state == 2 && envmatch((uchar *)name, i) >= 0)
112 state = 0;
113
114 j = 0;
115 do {
116 buf[j++] = c = env_get_char(i++);
117 if (j == sizeof(buf) - 1) {
118 if (state <= 1)
119 puts(buf);
120 j = 0;
wdenka68d3ed2002-10-11 08:38:32 +0000121 }
Mike Frysinger87944922009-05-24 02:26:19 -0400122 } while (c != '\0');
wdenka68d3ed2002-10-11 08:38:32 +0000123
Mike Frysinger87944922009-05-24 02:26:19 -0400124 if (state <= 1) {
125 if (j)
126 puts(buf);
127 putc('\n');
128 }
wdenka68d3ed2002-10-11 08:38:32 +0000129
Mike Frysinger87944922009-05-24 02:26:19 -0400130 if (ctrlc())
131 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000132 }
133
Mike Frysinger87944922009-05-24 02:26:19 -0400134 if (state == 0)
135 i = 0;
136 return i;
137}
wdenka68d3ed2002-10-11 08:38:32 +0000138
Mike Frysinger87944922009-05-24 02:26:19 -0400139int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
140{
141 int i;
142 int rcode = 0;
wdenka68d3ed2002-10-11 08:38:32 +0000143
Mike Frysinger87944922009-05-24 02:26:19 -0400144 if (argc == 1) {
145 /* print all env vars */
146 rcode = printenv(NULL, 1);
147 if (rcode < 0)
148 return 1;
149 printf("\nEnvironment size: %d/%ld bytes\n",
150 rcode, (ulong)ENV_SIZE);
151 return 0;
152 }
wdenka68d3ed2002-10-11 08:38:32 +0000153
Mike Frysinger87944922009-05-24 02:26:19 -0400154 /* print selected env vars */
155 for (i = 1; i < argc; ++i) {
156 char *name = argv[i];
157 if (printenv(name, 2)) {
158 printf("## Error: \"%s\" not defined\n", name);
159 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000160 }
161 }
Mike Frysinger87944922009-05-24 02:26:19 -0400162
wdenka68d3ed2002-10-11 08:38:32 +0000163 return rcode;
164}
165
166/************************************************************************
167 * Set a new environment variable,
168 * or replace or delete an existing one.
169 *
170 * This function will ONLY work with a in-RAM copy of the environment
171 */
172
173int _do_setenv (int flag, int argc, char *argv[])
174{
wdenka68d3ed2002-10-11 08:38:32 +0000175 int i, len, oldval;
176 int console = -1;
177 uchar *env, *nxt = NULL;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200178 char *name;
wdenka68d3ed2002-10-11 08:38:32 +0000179 bd_t *bd = gd->bd;
180
181 uchar *env_data = env_get_addr(0);
182
183 if (!env_data) /* need copy in RAM */
184 return 1;
185
186 name = argv[1];
187
Wolfgang Denkbc50f8c2006-10-28 01:14:32 +0200188 if (strchr(name, '=')) {
189 printf ("## Error: illegal character '=' in variable name \"%s\"\n", name);
190 return 1;
191 }
192
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100193 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000194 /*
195 * search if variable with this name already exists
196 */
197 oldval = -1;
198 for (env=env_data; *env; env=nxt+1) {
199 for (nxt=env; *nxt; ++nxt)
200 ;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200201 if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
wdenka68d3ed2002-10-11 08:38:32 +0000202 break;
203 }
204
Alessandro Rubini5d4d06c2009-10-08 14:29:14 +0200205 /* Check for console redirection */
206 if (strcmp(name,"stdin") == 0) {
207 console = stdin;
208 } else if (strcmp(name,"stdout") == 0) {
209 console = stdout;
210 } else if (strcmp(name,"stderr") == 0) {
211 console = stderr;
212 }
213
214 if (console != -1) {
215 if (argc < 3) { /* Cannot delete it! */
216 printf("Can't delete \"%s\"\n", name);
217 return 1;
218 }
219
220#ifdef CONFIG_CONSOLE_MUX
221 i = iomux_doenv(console, argv[2]);
222 if (i)
223 return i;
224#else
225 /* Try assigning specified device */
226 if (console_assign (console, argv[2]) < 0)
227 return 1;
228
229#ifdef CONFIG_SERIAL_MULTI
230 if (serial_assign (argv[2]) < 0)
231 return 1;
232#endif
233#endif /* CONFIG_CONSOLE_MUX */
234 }
235
wdenka68d3ed2002-10-11 08:38:32 +0000236 /*
237 * Delete any existing definition
238 */
239 if (oldval >= 0) {
240#ifndef CONFIG_ENV_OVERWRITE
241
242 /*
stroesef8105cf2003-04-04 15:44:49 +0000243 * Ethernet Address and serial# can be set only once,
244 * ver is readonly.
wdenka68d3ed2002-10-11 08:38:32 +0000245 */
Steven A. Falco2a1d4e42008-06-12 13:24:42 -0400246 if (
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200247#ifdef CONFIG_HAS_UID
248 /* Allow serial# forced overwrite with 0xdeaf4add flag */
Steven A. Falco2a1d4e42008-06-12 13:24:42 -0400249 ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) ||
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200250#else
Steven A. Falco2a1d4e42008-06-12 13:24:42 -0400251 (strcmp (name, "serial#") == 0) ||
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200252#endif
wdenka68d3ed2002-10-11 08:38:32 +0000253 ((strcmp (name, "ethaddr") == 0)
254#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200255 && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
wdenka68d3ed2002-10-11 08:38:32 +0000256#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
257 ) ) {
258 printf ("Can't overwrite \"%s\"\n", name);
259 return 1;
260 }
wdenk7ac16102004-08-01 22:48:16 +0000261#endif
wdenka68d3ed2002-10-11 08:38:32 +0000262
263 /*
264 * Switch to new baudrate if new baudrate is supported
265 */
266 if (strcmp(argv[1],"baudrate") == 0) {
267 int baudrate = simple_strtoul(argv[2], NULL, 10);
268 int i;
269 for (i=0; i<N_BAUDRATES; ++i) {
270 if (baudrate == baudrate_table[i])
271 break;
272 }
273 if (i == N_BAUDRATES) {
274 printf ("## Baudrate %d bps not supported\n",
275 baudrate);
276 return 1;
277 }
278 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
279 baudrate);
280 udelay(50000);
281 gd->baudrate = baudrate;
Bartlomiej Sieka23d43282006-12-20 00:29:43 +0100282#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
wdenke7f34c62003-01-11 09:48:40 +0000283 gd->bd->bi_baudrate = baudrate;
284#endif
285
wdenka68d3ed2002-10-11 08:38:32 +0000286 serial_setbrg ();
287 udelay(50000);
288 for (;;) {
289 if (getc() == '\r')
290 break;
291 }
292 }
293
294 if (*++nxt == '\0') {
295 if (env > env_data) {
296 env--;
297 } else {
298 *env = '\0';
299 }
300 } else {
301 for (;;) {
302 *env = *nxt++;
303 if ((*env == '\0') && (*nxt == '\0'))
304 break;
305 ++env;
306 }
307 }
308 *++env = '\0';
309 }
310
wdenka68d3ed2002-10-11 08:38:32 +0000311 /* Delete only ? */
312 if ((argc < 3) || argv[2] == NULL) {
313 env_crc_update ();
314 return 0;
315 }
316
317 /*
318 * Append new definition at the end
319 */
320 for (env=env_data; *env || *(env+1); ++env)
321 ;
322 if (env > env_data)
323 ++env;
324 /*
325 * Overflow when:
326 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
327 */
328 len = strlen(name) + 2;
329 /* add '=' for first arg, ' ' for all others */
330 for (i=2; i<argc; ++i) {
331 len += strlen(argv[i]) + 1;
332 }
333 if (len > (&env_data[ENV_SIZE]-env)) {
334 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
335 return 1;
336 }
337 while ((*env = *name++) != '\0')
338 env++;
339 for (i=2; i<argc; ++i) {
340 char *val = argv[i];
341
342 *env = (i==2) ? '=' : ' ';
343 while ((*++env = *val++) != '\0')
344 ;
345 }
346
347 /* end is marked with double '\0' */
348 *++env = '\0';
349
350 /* Update CRC */
351 env_crc_update ();
352
353 /*
354 * Some variables should be updated when the corresponding
355 * entry in the enviornment is changed
356 */
357
Mike Frysingerc3c533c2009-02-11 18:52:38 -0500358 if (strcmp(argv[1],"ethaddr") == 0)
wdenka68d3ed2002-10-11 08:38:32 +0000359 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000360
361 if (strcmp(argv[1],"ipaddr") == 0) {
362 char *s = argv[2]; /* always use only one arg */
363 char *e;
364 unsigned long addr;
365 bd->bi_ip_addr = 0;
366 for (addr=0, i=0; i<4; ++i) {
367 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
368 addr <<= 8;
369 addr |= (val & 0xFF);
370 if (s) s = (*e) ? e+1 : e;
371 }
372 bd->bi_ip_addr = htonl(addr);
373 return 0;
374 }
375 if (strcmp(argv[1],"loadaddr") == 0) {
376 load_addr = simple_strtoul(argv[2], NULL, 16);
377 return 0;
378 }
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500379#if defined(CONFIG_CMD_NET)
wdenka68d3ed2002-10-11 08:38:32 +0000380 if (strcmp(argv[1],"bootfile") == 0) {
381 copy_filename (BootFile, argv[2], sizeof(BootFile));
382 return 0;
383 }
Jon Loeligerd704d912007-07-10 11:02:44 -0500384#endif
wdenk452cfd62002-11-19 11:04:11 +0000385
stroesef8105cf2003-04-04 15:44:49 +0000386#ifdef CONFIG_AMIGAONEG3SE
wdenk452cfd62002-11-19 11:04:11 +0000387 if (strcmp(argv[1], "vga_fg_color") == 0 ||
388 strcmp(argv[1], "vga_bg_color") == 0 ) {
389 extern void video_set_color(unsigned char attr);
390 extern unsigned char video_get_attr(void);
391
392 video_set_color(video_get_attr());
393 return 0;
394 }
395#endif /* CONFIG_AMIGAONEG3SE */
396
wdenka68d3ed2002-10-11 08:38:32 +0000397 return 0;
398}
399
Steven A. Falco1ce6f992008-06-12 13:22:12 -0400400int setenv (char *varname, char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000401{
402 char *argv[4] = { "setenv", varname, varvalue, NULL };
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200403 if (varvalue == NULL)
Steven A. Falco1ce6f992008-06-12 13:22:12 -0400404 return _do_setenv (0, 2, argv);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200405 else
Steven A. Falco1ce6f992008-06-12 13:22:12 -0400406 return _do_setenv (0, 3, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000407}
408
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200409#ifdef CONFIG_HAS_UID
410void forceenv (char *varname, char *varvalue)
411{
412 char *argv[4] = { "forceenv", varname, varvalue, NULL };
413 _do_setenv (0xdeaf4add, 3, argv);
414}
415#endif
416
417int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000418{
419 if (argc < 2) {
Peter Tyserddb3af92009-01-27 18:03:10 -0600420 cmd_usage(cmdtp);
wdenka68d3ed2002-10-11 08:38:32 +0000421 return 1;
422 }
423
424 return _do_setenv (flag, argc, argv);
425}
426
427/************************************************************************
428 * Prompt for environment variable
429 */
430
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500431#if defined(CONFIG_CMD_ASKENV)
wdenka68d3ed2002-10-11 08:38:32 +0000432int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
433{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200434 extern char console_buffer[CONFIG_SYS_CBSIZE];
435 char message[CONFIG_SYS_CBSIZE];
436 int size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000437 int len;
438 char *local_args[4];
439
440 local_args[0] = argv[0];
441 local_args[1] = argv[1];
442 local_args[2] = NULL;
443 local_args[3] = NULL;
444
445 if (argc < 2) {
Peter Tyserddb3af92009-01-27 18:03:10 -0600446 cmd_usage(cmdtp);
wdenka68d3ed2002-10-11 08:38:32 +0000447 return 1;
448 }
449 /* Check the syntax */
450 switch (argc) {
451 case 1:
Peter Tyserddb3af92009-01-27 18:03:10 -0600452 cmd_usage(cmdtp);
wdenka68d3ed2002-10-11 08:38:32 +0000453 return 1;
454
455 case 2: /* askenv envname */
456 sprintf (message, "Please enter '%s':", argv[1]);
457 break;
458
459 case 3: /* askenv envname size */
460 sprintf (message, "Please enter '%s':", argv[1]);
461 size = simple_strtoul (argv[2], NULL, 10);
462 break;
463
464 default: /* askenv envname message1 ... messagen size */
465 {
466 int i;
467 int pos = 0;
468
469 for (i = 2; i < argc - 1; i++) {
470 if (pos) {
471 message[pos++] = ' ';
472 }
473 strcpy (message+pos, argv[i]);
474 pos += strlen(argv[i]);
475 }
476 message[pos] = '\0';
477 size = simple_strtoul (argv[argc - 1], NULL, 10);
478 }
479 break;
480 }
481
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200482 if (size >= CONFIG_SYS_CBSIZE)
483 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000484
485 if (size <= 0)
486 return 1;
487
488 /* prompt for input */
489 len = readline (message);
490
491 if (size < len)
492 console_buffer[size] = '\0';
493
494 len = 2;
495 if (console_buffer[0] != '\0') {
496 local_args[2] = console_buffer;
497 len = 3;
498 }
499
500 /* Continue calling setenv code */
501 return _do_setenv (flag, len, local_args);
502}
Jon Loeligerd704d912007-07-10 11:02:44 -0500503#endif
wdenka68d3ed2002-10-11 08:38:32 +0000504
505/************************************************************************
506 * Look up variable from environment,
507 * return address of storage for that variable,
508 * or NULL if not found
509 */
510
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200511char *getenv (char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000512{
513 int i, nxt;
514
wdenk2a3cb022002-11-05 21:01:48 +0000515 WATCHDOG_RESET();
516
wdenka68d3ed2002-10-11 08:38:32 +0000517 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
518 int val;
519
520 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200521 if (nxt >= CONFIG_ENV_SIZE) {
wdenka68d3ed2002-10-11 08:38:32 +0000522 return (NULL);
523 }
524 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200525 if ((val=envmatch((uchar *)name, i)) < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000526 continue;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200527 return ((char *)env_get_addr(val));
wdenka68d3ed2002-10-11 08:38:32 +0000528 }
529
530 return (NULL);
531}
532
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200533int getenv_r (char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000534{
535 int i, nxt;
536
537 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
538 int val, n;
539
540 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200541 if (nxt >= CONFIG_ENV_SIZE) {
wdenka68d3ed2002-10-11 08:38:32 +0000542 return (-1);
543 }
544 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200545 if ((val=envmatch((uchar *)name, i)) < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000546 continue;
547 /* found; copy out */
548 n = 0;
549 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
550 ;
551 if (len == n)
552 *buf = '\0';
553 return (n);
554 }
555 return (-1);
556}
557
Mike Frysinger78dcaf42009-01-28 19:08:14 -0500558#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Mike Frysinger61583932008-12-30 02:59:25 -0500559
wdenka68d3ed2002-10-11 08:38:32 +0000560int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
561{
562 extern char * env_name_spec;
563
564 printf ("Saving Environment to %s...\n", env_name_spec);
565
566 return (saveenv() ? 1 : 0);
567}
wdenk57b2d802003-06-27 21:31:46 +0000568
Mike Frysinger61583932008-12-30 02:59:25 -0500569U_BOOT_CMD(
570 saveenv, 1, 0, do_saveenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600571 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200572 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500573);
574
wdenka68d3ed2002-10-11 08:38:32 +0000575#endif
576
577
578/************************************************************************
579 * Match a name / name=value pair
580 *
581 * s1 is either a simple 'name', or a 'name=value' pair.
582 * i2 is the environment index for a 'name2=value2' pair.
583 * If the names match, return the index for the value2, else NULL.
584 */
585
Rafal Jaworowski75b54422008-01-09 18:05:27 +0100586int envmatch (uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000587{
588
589 while (*s1 == env_get_char(i2++))
590 if (*s1++ == '=')
591 return(i2);
592 if (*s1 == '\0' && env_get_char(i2-1) == '=')
593 return(i2);
594 return(-1);
595}
wdenk57b2d802003-06-27 21:31:46 +0000596
597
598/**************************************************/
599
wdenkf287a242003-07-01 21:06:45 +0000600U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200601 printenv, CONFIG_SYS_MAXARGS, 1, do_printenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600602 "print environment variables",
wdenk57b2d802003-06-27 21:31:46 +0000603 "\n - print values of all environment variables\n"
604 "printenv name ...\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200605 " - print value of environment variable 'name'"
wdenk57b2d802003-06-27 21:31:46 +0000606);
607
wdenkf287a242003-07-01 21:06:45 +0000608U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200609 setenv, CONFIG_SYS_MAXARGS, 0, do_setenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600610 "set environment variables",
wdenk57b2d802003-06-27 21:31:46 +0000611 "name value ...\n"
612 " - set environment variable 'name' to 'value ...'\n"
613 "setenv name\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200614 " - delete environment variable 'name'"
wdenk57b2d802003-06-27 21:31:46 +0000615);
616
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500617#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +0000618
wdenkf287a242003-07-01 21:06:45 +0000619U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200620 askenv, CONFIG_SYS_MAXARGS, 1, do_askenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600621 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +0000622 "name [message] [size]\n"
623 " - get environment variable 'name' from stdin (max 'size' chars)\n"
624 "askenv name\n"
625 " - get environment variable 'name' from stdin\n"
626 "askenv name size\n"
627 " - get environment variable 'name' from stdin (max 'size' chars)\n"
628 "askenv name [message] size\n"
629 " - display 'message' string and get environment variable 'name'"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200630 "from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +0000631);
Jon Loeligerd704d912007-07-10 11:02:44 -0500632#endif
wdenk57b2d802003-06-27 21:31:46 +0000633
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500634#if defined(CONFIG_CMD_RUN)
wdenk57b2d802003-06-27 21:31:46 +0000635int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenkf287a242003-07-01 21:06:45 +0000636U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200637 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600638 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +0000639 "var [...]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200640 " - run the commands in the environment variable(s) 'var'"
wdenk57b2d802003-06-27 21:31:46 +0000641);
Jon Loeligerd704d912007-07-10 11:02:44 -0500642#endif