blob: c941b95fbcf8333edd5c0643332669bff845a0f5 [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>
Peter Tyser0deafa22009-10-25 15:12:56 -050045#if defined(CONFIG_CMD_EDITENV)
46#include <malloc.h>
47#endif
wdenk2a3cb022002-11-05 21:01:48 +000048#include <watchdog.h>
wdenk7ac16102004-08-01 22:48:16 +000049#include <serial.h>
wdenka68d3ed2002-10-11 08:38:32 +000050#include <linux/stddef.h>
51#include <asm/byteorder.h>
Jon Loeligerd76b5c12007-07-08 18:02:23 -050052#if defined(CONFIG_CMD_NET)
wdenka68d3ed2002-10-11 08:38:32 +000053#include <net.h>
54#endif
55
Wolfgang Denk6405a152006-03-31 18:32:53 +020056DECLARE_GLOBAL_DATA_PTR;
57
unsik Kimf0b1bdd2009-02-25 11:31:24 +090058#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
Jean-Christophe PLAGNIOL-VILLARD53db4cd2008-09-10 22:48:04 +020059 !defined(CONFIG_ENV_IS_IN_FLASH) && \
Jean-Christophe PLAGNIOL-VILLARD2b14d2b2008-09-10 22:47:58 +020060 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090061 !defined(CONFIG_ENV_IS_IN_MG_DISK) && \
Terry Lvb4eceac2010-05-17 10:57:01 +080062 !defined(CONFIG_ENV_IS_IN_MMC) && \
Jean-Christophe PLAGNIOL-VILLARDdda84dd2008-09-10 22:47:58 +020063 !defined(CONFIG_ENV_IS_IN_NAND) && \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090064 !defined(CONFIG_ENV_IS_IN_NVRAM) && \
Jean-Christophe PLAGNIOL-VILLARD75989162008-09-10 22:47:59 +020065 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
Jean-Christophe PLAGNIOL-VILLARD4539b1c2008-09-10 22:48:00 +020066 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
Jean-Christophe PLAGNIOL-VILLARD68a87562008-09-10 22:48:00 +020067 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kimf0b1bdd2009-02-25 11:31:24 +090068# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Terry Lvb4eceac2010-05-17 10:57:01 +080069SPI_FLASH|MG_DISK|NVRAM|MMC|NOWHERE}
wdenka68d3ed2002-10-11 08:38:32 +000070#endif
71
72#define XMK_STR(x) #x
73#define MK_STR(x) XMK_STR(x)
74
75/************************************************************************
76************************************************************************/
77
wdenka68d3ed2002-10-11 08:38:32 +000078/*
79 * Table with supported baudrates (defined in config_xyz.h)
80 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
wdenka68d3ed2002-10-11 08:38:32 +000082#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
83
Heiko Schocher3b195ff2009-04-28 08:36:11 +020084/*
85 * This variable is incremented on each do_setenv (), so it can
86 * be used via get_env_id() as an indication, if the environment
87 * has changed or not. So it is possible to reread an environment
88 * variable only if the environment was changed ... done so for
89 * example in NetInitLoop()
90 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010091static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000092
Heiko Schocher0c303fa2009-02-10 09:38:52 +010093int get_env_id (void)
94{
95 return env_id;
96}
wdenka68d3ed2002-10-11 08:38:32 +000097/************************************************************************
98 * Command interface: print one or all environment variables
99 */
100
Mike Frysinger87944922009-05-24 02:26:19 -0400101/*
102 * state 0: finish printing this string and return (matched!)
103 * state 1: no matching to be done; print everything
104 * state 2: continue searching for matched name
105 */
106static int printenv(char *name, int state)
wdenka68d3ed2002-10-11 08:38:32 +0000107{
Mike Frysinger87944922009-05-24 02:26:19 -0400108 int i, j;
109 char c, buf[17];
wdenka68d3ed2002-10-11 08:38:32 +0000110
Mike Frysinger87944922009-05-24 02:26:19 -0400111 i = 0;
112 buf[16] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000113
Mike Frysinger87944922009-05-24 02:26:19 -0400114 while (state && env_get_char(i) != '\0') {
115 if (state == 2 && envmatch((uchar *)name, i) >= 0)
116 state = 0;
117
118 j = 0;
119 do {
120 buf[j++] = c = env_get_char(i++);
121 if (j == sizeof(buf) - 1) {
122 if (state <= 1)
123 puts(buf);
124 j = 0;
wdenka68d3ed2002-10-11 08:38:32 +0000125 }
Mike Frysinger87944922009-05-24 02:26:19 -0400126 } while (c != '\0');
wdenka68d3ed2002-10-11 08:38:32 +0000127
Mike Frysinger87944922009-05-24 02:26:19 -0400128 if (state <= 1) {
129 if (j)
130 puts(buf);
131 putc('\n');
132 }
wdenka68d3ed2002-10-11 08:38:32 +0000133
Mike Frysinger87944922009-05-24 02:26:19 -0400134 if (ctrlc())
135 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000136 }
137
Mike Frysinger87944922009-05-24 02:26:19 -0400138 if (state == 0)
139 i = 0;
140 return i;
141}
wdenka68d3ed2002-10-11 08:38:32 +0000142
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200143int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400144{
145 int i;
146 int rcode = 0;
wdenka68d3ed2002-10-11 08:38:32 +0000147
Mike Frysinger87944922009-05-24 02:26:19 -0400148 if (argc == 1) {
149 /* print all env vars */
150 rcode = printenv(NULL, 1);
151 if (rcode < 0)
152 return 1;
153 printf("\nEnvironment size: %d/%ld bytes\n",
154 rcode, (ulong)ENV_SIZE);
155 return 0;
156 }
wdenka68d3ed2002-10-11 08:38:32 +0000157
Mike Frysinger87944922009-05-24 02:26:19 -0400158 /* print selected env vars */
159 for (i = 1; i < argc; ++i) {
160 char *name = argv[i];
161 if (printenv(name, 2)) {
162 printf("## Error: \"%s\" not defined\n", name);
163 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000164 }
165 }
Mike Frysinger87944922009-05-24 02:26:19 -0400166
wdenka68d3ed2002-10-11 08:38:32 +0000167 return rcode;
168}
169
170/************************************************************************
171 * Set a new environment variable,
172 * or replace or delete an existing one.
173 *
174 * This function will ONLY work with a in-RAM copy of the environment
175 */
176
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200177int _do_setenv (int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000178{
wdenka68d3ed2002-10-11 08:38:32 +0000179 int i, len, oldval;
180 int console = -1;
181 uchar *env, *nxt = NULL;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200182 char *name;
wdenka68d3ed2002-10-11 08:38:32 +0000183 bd_t *bd = gd->bd;
184
185 uchar *env_data = env_get_addr(0);
186
187 if (!env_data) /* need copy in RAM */
188 return 1;
189
190 name = argv[1];
191
Wolfgang Denkbc50f8c2006-10-28 01:14:32 +0200192 if (strchr(name, '=')) {
193 printf ("## Error: illegal character '=' in variable name \"%s\"\n", name);
194 return 1;
195 }
196
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100197 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000198 /*
199 * search if variable with this name already exists
200 */
201 oldval = -1;
202 for (env=env_data; *env; env=nxt+1) {
203 for (nxt=env; *nxt; ++nxt)
204 ;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200205 if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
wdenka68d3ed2002-10-11 08:38:32 +0000206 break;
207 }
208
Alessandro Rubini5d4d06c2009-10-08 14:29:14 +0200209 /* Check for console redirection */
210 if (strcmp(name,"stdin") == 0) {
211 console = stdin;
212 } else if (strcmp(name,"stdout") == 0) {
213 console = stdout;
214 } else if (strcmp(name,"stderr") == 0) {
215 console = stderr;
216 }
217
218 if (console != -1) {
219 if (argc < 3) { /* Cannot delete it! */
220 printf("Can't delete \"%s\"\n", name);
221 return 1;
222 }
223
224#ifdef CONFIG_CONSOLE_MUX
225 i = iomux_doenv(console, argv[2]);
226 if (i)
227 return i;
228#else
229 /* Try assigning specified device */
230 if (console_assign (console, argv[2]) < 0)
231 return 1;
232
233#ifdef CONFIG_SERIAL_MULTI
234 if (serial_assign (argv[2]) < 0)
235 return 1;
236#endif
237#endif /* CONFIG_CONSOLE_MUX */
238 }
239
wdenka68d3ed2002-10-11 08:38:32 +0000240 /*
241 * Delete any existing definition
242 */
243 if (oldval >= 0) {
244#ifndef CONFIG_ENV_OVERWRITE
245
246 /*
stroesef8105cf2003-04-04 15:44:49 +0000247 * Ethernet Address and serial# can be set only once,
248 * ver is readonly.
wdenka68d3ed2002-10-11 08:38:32 +0000249 */
Steven A. Falco2a1d4e42008-06-12 13:24:42 -0400250 if (
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200251#ifdef CONFIG_HAS_UID
252 /* Allow serial# forced overwrite with 0xdeaf4add flag */
Steven A. Falco2a1d4e42008-06-12 13:24:42 -0400253 ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) ||
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200254#else
Steven A. Falco2a1d4e42008-06-12 13:24:42 -0400255 (strcmp (name, "serial#") == 0) ||
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200256#endif
wdenka68d3ed2002-10-11 08:38:32 +0000257 ((strcmp (name, "ethaddr") == 0)
258#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200259 && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
wdenka68d3ed2002-10-11 08:38:32 +0000260#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
261 ) ) {
262 printf ("Can't overwrite \"%s\"\n", name);
263 return 1;
264 }
wdenk7ac16102004-08-01 22:48:16 +0000265#endif
wdenka68d3ed2002-10-11 08:38:32 +0000266
267 /*
268 * Switch to new baudrate if new baudrate is supported
269 */
270 if (strcmp(argv[1],"baudrate") == 0) {
271 int baudrate = simple_strtoul(argv[2], NULL, 10);
272 int i;
273 for (i=0; i<N_BAUDRATES; ++i) {
274 if (baudrate == baudrate_table[i])
275 break;
276 }
277 if (i == N_BAUDRATES) {
278 printf ("## Baudrate %d bps not supported\n",
279 baudrate);
280 return 1;
281 }
282 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
283 baudrate);
284 udelay(50000);
285 gd->baudrate = baudrate;
Bartlomiej Sieka23d43282006-12-20 00:29:43 +0100286#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
wdenke7f34c62003-01-11 09:48:40 +0000287 gd->bd->bi_baudrate = baudrate;
288#endif
289
wdenka68d3ed2002-10-11 08:38:32 +0000290 serial_setbrg ();
291 udelay(50000);
292 for (;;) {
293 if (getc() == '\r')
294 break;
295 }
296 }
297
298 if (*++nxt == '\0') {
299 if (env > env_data) {
300 env--;
301 } else {
302 *env = '\0';
303 }
304 } else {
305 for (;;) {
306 *env = *nxt++;
307 if ((*env == '\0') && (*nxt == '\0'))
308 break;
309 ++env;
310 }
311 }
312 *++env = '\0';
313 }
314
wdenka68d3ed2002-10-11 08:38:32 +0000315 /* Delete only ? */
316 if ((argc < 3) || argv[2] == NULL) {
317 env_crc_update ();
318 return 0;
319 }
320
321 /*
322 * Append new definition at the end
323 */
324 for (env=env_data; *env || *(env+1); ++env)
325 ;
326 if (env > env_data)
327 ++env;
328 /*
329 * Overflow when:
330 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
331 */
332 len = strlen(name) + 2;
333 /* add '=' for first arg, ' ' for all others */
334 for (i=2; i<argc; ++i) {
335 len += strlen(argv[i]) + 1;
336 }
337 if (len > (&env_data[ENV_SIZE]-env)) {
338 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
339 return 1;
340 }
341 while ((*env = *name++) != '\0')
342 env++;
343 for (i=2; i<argc; ++i) {
344 char *val = argv[i];
345
346 *env = (i==2) ? '=' : ' ';
347 while ((*++env = *val++) != '\0')
348 ;
349 }
350
351 /* end is marked with double '\0' */
352 *++env = '\0';
353
354 /* Update CRC */
355 env_crc_update ();
356
357 /*
358 * Some variables should be updated when the corresponding
359 * entry in the enviornment is changed
360 */
361
Mike Frysingerc3c533c2009-02-11 18:52:38 -0500362 if (strcmp(argv[1],"ethaddr") == 0)
wdenka68d3ed2002-10-11 08:38:32 +0000363 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000364
365 if (strcmp(argv[1],"ipaddr") == 0) {
366 char *s = argv[2]; /* always use only one arg */
367 char *e;
368 unsigned long addr;
369 bd->bi_ip_addr = 0;
370 for (addr=0, i=0; i<4; ++i) {
371 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
372 addr <<= 8;
373 addr |= (val & 0xFF);
374 if (s) s = (*e) ? e+1 : e;
375 }
376 bd->bi_ip_addr = htonl(addr);
377 return 0;
378 }
379 if (strcmp(argv[1],"loadaddr") == 0) {
380 load_addr = simple_strtoul(argv[2], NULL, 16);
381 return 0;
382 }
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500383#if defined(CONFIG_CMD_NET)
wdenka68d3ed2002-10-11 08:38:32 +0000384 if (strcmp(argv[1],"bootfile") == 0) {
385 copy_filename (BootFile, argv[2], sizeof(BootFile));
386 return 0;
387 }
Jon Loeligerd704d912007-07-10 11:02:44 -0500388#endif
wdenka68d3ed2002-10-11 08:38:32 +0000389 return 0;
390}
391
Steven A. Falco1ce6f992008-06-12 13:22:12 -0400392int setenv (char *varname, char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000393{
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200394 char * const argv[4] = { "setenv", varname, varvalue, NULL };
Peter Tyser9fb48fd2009-10-25 15:12:55 -0500395 if ((varvalue == NULL) || (varvalue[0] == '\0'))
Steven A. Falco1ce6f992008-06-12 13:22:12 -0400396 return _do_setenv (0, 2, argv);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200397 else
Steven A. Falco1ce6f992008-06-12 13:22:12 -0400398 return _do_setenv (0, 3, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000399}
400
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200401#ifdef CONFIG_HAS_UID
402void forceenv (char *varname, char *varvalue)
403{
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200404 char * const argv[4] = { "forceenv", varname, varvalue, NULL };
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200405 _do_setenv (0xdeaf4add, 3, argv);
406}
407#endif
408
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200409int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000410{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200411 if (argc < 2)
412 return cmd_usage(cmdtp);
wdenka68d3ed2002-10-11 08:38:32 +0000413
414 return _do_setenv (flag, argc, argv);
415}
416
417/************************************************************************
418 * Prompt for environment variable
419 */
420
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500421#if defined(CONFIG_CMD_ASKENV)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200422int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000423{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200424 extern char console_buffer[CONFIG_SYS_CBSIZE];
425 char message[CONFIG_SYS_CBSIZE];
426 int size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000427 int len;
428 char *local_args[4];
429
430 local_args[0] = argv[0];
431 local_args[1] = argv[1];
432 local_args[2] = NULL;
433 local_args[3] = NULL;
434
Wolfgang Denk3b683112010-07-17 01:06:04 +0200435 if (argc < 2)
436 return cmd_usage(cmdtp);
437
wdenka68d3ed2002-10-11 08:38:32 +0000438 /* Check the syntax */
439 switch (argc) {
440 case 1:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200441 return cmd_usage(cmdtp);
wdenka68d3ed2002-10-11 08:38:32 +0000442
443 case 2: /* askenv envname */
444 sprintf (message, "Please enter '%s':", argv[1]);
445 break;
446
447 case 3: /* askenv envname size */
448 sprintf (message, "Please enter '%s':", argv[1]);
449 size = simple_strtoul (argv[2], NULL, 10);
450 break;
451
452 default: /* askenv envname message1 ... messagen size */
453 {
454 int i;
455 int pos = 0;
456
457 for (i = 2; i < argc - 1; i++) {
458 if (pos) {
459 message[pos++] = ' ';
460 }
461 strcpy (message+pos, argv[i]);
462 pos += strlen(argv[i]);
463 }
464 message[pos] = '\0';
465 size = simple_strtoul (argv[argc - 1], NULL, 10);
466 }
467 break;
468 }
469
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200470 if (size >= CONFIG_SYS_CBSIZE)
471 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000472
473 if (size <= 0)
474 return 1;
475
476 /* prompt for input */
477 len = readline (message);
478
479 if (size < len)
480 console_buffer[size] = '\0';
481
482 len = 2;
483 if (console_buffer[0] != '\0') {
484 local_args[2] = console_buffer;
485 len = 3;
486 }
487
488 /* Continue calling setenv code */
489 return _do_setenv (flag, len, local_args);
490}
Jon Loeligerd704d912007-07-10 11:02:44 -0500491#endif
wdenka68d3ed2002-10-11 08:38:32 +0000492
493/************************************************************************
Peter Tyser0deafa22009-10-25 15:12:56 -0500494 * Interactively edit an environment variable
495 */
496#if defined(CONFIG_CMD_EDITENV)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200497int do_editenv(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500498{
499 char buffer[CONFIG_SYS_CBSIZE];
500 char *init_val;
501 int len;
502
Wolfgang Denk3b683112010-07-17 01:06:04 +0200503 if (argc < 2)
504 return cmd_usage(cmdtp);
Peter Tyser0deafa22009-10-25 15:12:56 -0500505
506 /* Set read buffer to initial value or empty sting */
507 init_val = getenv(argv[1]);
508 if (init_val)
509 len = sprintf(buffer, "%s", init_val);
510 else
511 buffer[0] = '\0';
512
513 readline_into_buffer("edit: ", buffer);
514
515 return setenv(argv[1], buffer);
516}
517#endif /* CONFIG_CMD_EDITENV */
518
519/************************************************************************
wdenka68d3ed2002-10-11 08:38:32 +0000520 * Look up variable from environment,
521 * return address of storage for that variable,
522 * or NULL if not found
523 */
524
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200525char *getenv (char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000526{
527 int i, nxt;
528
wdenk2a3cb022002-11-05 21:01:48 +0000529 WATCHDOG_RESET();
530
wdenka68d3ed2002-10-11 08:38:32 +0000531 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
532 int val;
533
534 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200535 if (nxt >= CONFIG_ENV_SIZE) {
wdenka68d3ed2002-10-11 08:38:32 +0000536 return (NULL);
537 }
538 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200539 if ((val=envmatch((uchar *)name, i)) < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000540 continue;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200541 return ((char *)env_get_addr(val));
wdenka68d3ed2002-10-11 08:38:32 +0000542 }
543
544 return (NULL);
545}
546
Wolfgang Denk76af2782010-07-24 21:55:43 +0200547int getenv_f(char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000548{
549 int i, nxt;
550
551 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
552 int val, n;
553
554 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200555 if (nxt >= CONFIG_ENV_SIZE) {
wdenka68d3ed2002-10-11 08:38:32 +0000556 return (-1);
557 }
558 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200559 if ((val=envmatch((uchar *)name, i)) < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000560 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200561
wdenka68d3ed2002-10-11 08:38:32 +0000562 /* found; copy out */
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200563 for (n=0; n<len; ++n, ++buf) {
564 if ((*buf = env_get_char(val++)) == '\0')
565 return n;
566 }
567
568 if (n)
569 *--buf = '\0';
570
571 printf("env_buf too small [%d]\n", len);
572
573 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000574 }
575 return (-1);
576}
577
Mike Frysinger78dcaf42009-01-28 19:08:14 -0500578#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Mike Frysinger61583932008-12-30 02:59:25 -0500579
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200580int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000581{
582 extern char * env_name_spec;
583
584 printf ("Saving Environment to %s...\n", env_name_spec);
585
586 return (saveenv() ? 1 : 0);
587}
wdenk57b2d802003-06-27 21:31:46 +0000588
Mike Frysinger61583932008-12-30 02:59:25 -0500589U_BOOT_CMD(
590 saveenv, 1, 0, do_saveenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600591 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200592 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500593);
594
wdenka68d3ed2002-10-11 08:38:32 +0000595#endif
596
597
598/************************************************************************
599 * Match a name / name=value pair
600 *
601 * s1 is either a simple 'name', or a 'name=value' pair.
602 * i2 is the environment index for a 'name2=value2' pair.
603 * If the names match, return the index for the value2, else NULL.
604 */
605
Rafal Jaworowski75b54422008-01-09 18:05:27 +0100606int envmatch (uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000607{
608
609 while (*s1 == env_get_char(i2++))
610 if (*s1++ == '=')
611 return(i2);
612 if (*s1 == '\0' && env_get_char(i2-1) == '=')
613 return(i2);
614 return(-1);
615}
wdenk57b2d802003-06-27 21:31:46 +0000616
617
618/**************************************************/
619
Peter Tyser0deafa22009-10-25 15:12:56 -0500620#if defined(CONFIG_CMD_EDITENV)
621U_BOOT_CMD(
622 editenv, 2, 0, do_editenv,
623 "edit environment variable",
624 "name\n"
625 " - edit environment variable 'name'"
626);
627#endif
628
wdenkf287a242003-07-01 21:06:45 +0000629U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200630 printenv, CONFIG_SYS_MAXARGS, 1, do_printenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600631 "print environment variables",
wdenk57b2d802003-06-27 21:31:46 +0000632 "\n - print values of all environment variables\n"
633 "printenv name ...\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200634 " - print value of environment variable 'name'"
wdenk57b2d802003-06-27 21:31:46 +0000635);
636
wdenkf287a242003-07-01 21:06:45 +0000637U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200638 setenv, CONFIG_SYS_MAXARGS, 0, do_setenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600639 "set environment variables",
wdenk57b2d802003-06-27 21:31:46 +0000640 "name value ...\n"
641 " - set environment variable 'name' to 'value ...'\n"
642 "setenv name\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200643 " - delete environment variable 'name'"
wdenk57b2d802003-06-27 21:31:46 +0000644);
645
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500646#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +0000647
wdenkf287a242003-07-01 21:06:45 +0000648U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200649 askenv, CONFIG_SYS_MAXARGS, 1, do_askenv,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600650 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +0000651 "name [message] [size]\n"
652 " - get environment variable 'name' from stdin (max 'size' chars)\n"
653 "askenv name\n"
654 " - get environment variable 'name' from stdin\n"
655 "askenv name size\n"
656 " - get environment variable 'name' from stdin (max 'size' chars)\n"
657 "askenv name [message] size\n"
658 " - display 'message' string and get environment variable 'name'"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200659 "from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +0000660);
Jon Loeligerd704d912007-07-10 11:02:44 -0500661#endif
wdenk57b2d802003-06-27 21:31:46 +0000662
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500663#if defined(CONFIG_CMD_RUN)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200664int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenkf287a242003-07-01 21:06:45 +0000665U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200666 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600667 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +0000668 "var [...]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200669 " - run the commands in the environment variable(s) 'var'"
wdenk57b2d802003-06-27 21:31:46 +0000670);
Jon Loeligerd704d912007-07-10 11:02:44 -0500671#endif