blob: 6257fbd23e6d0703213bcc59b14a487bf52f4707 [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>
49#if (CONFIG_COMMANDS & CFG_CMD_NET)
50#include <net.h>
51#endif
52
Wolfgang Denk6405a152006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54
wdenk86765902003-12-06 23:55:10 +000055#if !defined(CFG_ENV_IS_IN_NVRAM) && \
56 !defined(CFG_ENV_IS_IN_EEPROM) && \
57 !defined(CFG_ENV_IS_IN_FLASH) && \
58 !defined(CFG_ENV_IS_IN_DATAFLASH) && \
wdenk79b59372004-06-09 14:58:14 +000059 !defined(CFG_ENV_IS_IN_NAND) && \
wdenk86765902003-12-06 23:55:10 +000060 !defined(CFG_ENV_IS_NOWHERE)
61# error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE}
wdenka68d3ed2002-10-11 08:38:32 +000062#endif
63
64#define XMK_STR(x) #x
65#define MK_STR(x) XMK_STR(x)
66
67/************************************************************************
68************************************************************************/
69
70/* Function that returns a character from the environment */
71extern uchar (*env_get_char)(int);
72
73/* Function that returns a pointer to a value from the environment */
74/* (Only memory version supported / needed). */
75extern uchar *env_get_addr(int);
76
77/* Function that updates CRC of the enironment */
78extern void env_crc_update (void);
79
80/************************************************************************
81************************************************************************/
82
83static int envmatch (uchar *, int);
84
85/*
86 * Table with supported baudrates (defined in config_xyz.h)
87 */
88static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
89#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
90
91
92/************************************************************************
93 * Command interface: print one or all environment variables
94 */
95
96int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
97{
98 int i, j, k, nxt;
99 int rcode = 0;
100
101 if (argc == 1) { /* Print all env variables */
102 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
103 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
104 ;
105 for (k=i; k<nxt; ++k)
106 putc(env_get_char(k));
107 putc ('\n');
108
109 if (ctrlc()) {
110 puts ("\n ** Abort\n");
111 return 1;
112 }
113 }
114
115 printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE);
116
117 return 0;
118 }
119
120 for (i=1; i<argc; ++i) { /* print single env variables */
121 char *name = argv[i];
122
123 k = -1;
124
125 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
126
127 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
128 ;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200129 k = envmatch((uchar *)name, j);
wdenka68d3ed2002-10-11 08:38:32 +0000130 if (k < 0) {
131 continue;
132 }
133 puts (name);
134 putc ('=');
135 while (k < nxt)
136 putc(env_get_char(k++));
137 putc ('\n');
138 break;
139 }
140 if (k < 0) {
141 printf ("## Error: \"%s\" not defined\n", name);
142 rcode ++;
143 }
144 }
145 return rcode;
146}
147
148/************************************************************************
149 * Set a new environment variable,
150 * or replace or delete an existing one.
151 *
152 * This function will ONLY work with a in-RAM copy of the environment
153 */
154
155int _do_setenv (int flag, int argc, char *argv[])
156{
wdenka68d3ed2002-10-11 08:38:32 +0000157 int i, len, oldval;
158 int console = -1;
159 uchar *env, *nxt = NULL;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200160 char *name;
wdenka68d3ed2002-10-11 08:38:32 +0000161 bd_t *bd = gd->bd;
162
163 uchar *env_data = env_get_addr(0);
164
165 if (!env_data) /* need copy in RAM */
166 return 1;
167
168 name = argv[1];
169
170 /*
171 * search if variable with this name already exists
172 */
173 oldval = -1;
174 for (env=env_data; *env; env=nxt+1) {
175 for (nxt=env; *nxt; ++nxt)
176 ;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200177 if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
wdenka68d3ed2002-10-11 08:38:32 +0000178 break;
179 }
180
181 /*
182 * Delete any existing definition
183 */
184 if (oldval >= 0) {
185#ifndef CONFIG_ENV_OVERWRITE
186
187 /*
stroesef8105cf2003-04-04 15:44:49 +0000188 * Ethernet Address and serial# can be set only once,
189 * ver is readonly.
wdenka68d3ed2002-10-11 08:38:32 +0000190 */
191 if ( (strcmp (name, "serial#") == 0) ||
192 ((strcmp (name, "ethaddr") == 0)
193#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200194 && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
wdenka68d3ed2002-10-11 08:38:32 +0000195#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
196 ) ) {
197 printf ("Can't overwrite \"%s\"\n", name);
198 return 1;
199 }
200#endif
201
202 /* Check for console redirection */
203 if (strcmp(name,"stdin") == 0) {
204 console = stdin;
205 } else if (strcmp(name,"stdout") == 0) {
206 console = stdout;
207 } else if (strcmp(name,"stderr") == 0) {
208 console = stderr;
209 }
210
211 if (console != -1) {
212 if (argc < 3) { /* Cannot delete it! */
213 printf("Can't delete \"%s\"\n", name);
214 return 1;
215 }
216
217 /* Try assigning specified device */
218 if (console_assign (console, argv[2]) < 0)
219 return 1;
wdenk7ac16102004-08-01 22:48:16 +0000220
221#ifdef CONFIG_SERIAL_MULTI
222 if (serial_assign (argv[2]) < 0)
223 return 1;
224#endif
wdenka68d3ed2002-10-11 08:38:32 +0000225 }
226
227 /*
228 * Switch to new baudrate if new baudrate is supported
229 */
230 if (strcmp(argv[1],"baudrate") == 0) {
231 int baudrate = simple_strtoul(argv[2], NULL, 10);
232 int i;
233 for (i=0; i<N_BAUDRATES; ++i) {
234 if (baudrate == baudrate_table[i])
235 break;
236 }
237 if (i == N_BAUDRATES) {
238 printf ("## Baudrate %d bps not supported\n",
239 baudrate);
240 return 1;
241 }
242 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
243 baudrate);
244 udelay(50000);
245 gd->baudrate = baudrate;
wdenke7f34c62003-01-11 09:48:40 +0000246#ifdef CONFIG_PPC
247 gd->bd->bi_baudrate = baudrate;
248#endif
249
wdenka68d3ed2002-10-11 08:38:32 +0000250 serial_setbrg ();
251 udelay(50000);
252 for (;;) {
253 if (getc() == '\r')
254 break;
255 }
256 }
257
258 if (*++nxt == '\0') {
259 if (env > env_data) {
260 env--;
261 } else {
262 *env = '\0';
263 }
264 } else {
265 for (;;) {
266 *env = *nxt++;
267 if ((*env == '\0') && (*nxt == '\0'))
268 break;
269 ++env;
270 }
271 }
272 *++env = '\0';
273 }
274
275#ifdef CONFIG_NET_MULTI
276 if (strncmp(name, "eth", 3) == 0) {
277 char *end;
278 int num = simple_strtoul(name+3, &end, 10);
279
280 if (strcmp(end, "addr") == 0) {
281 eth_set_enetaddr(num, argv[2]);
282 }
283 }
284#endif
285
286
287 /* Delete only ? */
288 if ((argc < 3) || argv[2] == NULL) {
289 env_crc_update ();
290 return 0;
291 }
292
293 /*
294 * Append new definition at the end
295 */
296 for (env=env_data; *env || *(env+1); ++env)
297 ;
298 if (env > env_data)
299 ++env;
300 /*
301 * Overflow when:
302 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
303 */
304 len = strlen(name) + 2;
305 /* add '=' for first arg, ' ' for all others */
306 for (i=2; i<argc; ++i) {
307 len += strlen(argv[i]) + 1;
308 }
309 if (len > (&env_data[ENV_SIZE]-env)) {
310 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
311 return 1;
312 }
313 while ((*env = *name++) != '\0')
314 env++;
315 for (i=2; i<argc; ++i) {
316 char *val = argv[i];
317
318 *env = (i==2) ? '=' : ' ';
319 while ((*++env = *val++) != '\0')
320 ;
321 }
322
323 /* end is marked with double '\0' */
324 *++env = '\0';
325
326 /* Update CRC */
327 env_crc_update ();
328
329 /*
330 * Some variables should be updated when the corresponding
331 * entry in the enviornment is changed
332 */
333
334 if (strcmp(argv[1],"ethaddr") == 0) {
335 char *s = argv[2]; /* always use only one arg */
336 char *e;
337 for (i=0; i<6; ++i) {
338 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
339 if (s) s = (*e) ? e+1 : e;
340 }
341#ifdef CONFIG_NET_MULTI
342 eth_set_enetaddr(0, argv[2]);
343#endif
344 return 0;
345 }
346
347 if (strcmp(argv[1],"ipaddr") == 0) {
348 char *s = argv[2]; /* always use only one arg */
349 char *e;
350 unsigned long addr;
351 bd->bi_ip_addr = 0;
352 for (addr=0, i=0; i<4; ++i) {
353 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
354 addr <<= 8;
355 addr |= (val & 0xFF);
356 if (s) s = (*e) ? e+1 : e;
357 }
358 bd->bi_ip_addr = htonl(addr);
359 return 0;
360 }
361 if (strcmp(argv[1],"loadaddr") == 0) {
362 load_addr = simple_strtoul(argv[2], NULL, 16);
363 return 0;
364 }
365#if (CONFIG_COMMANDS & CFG_CMD_NET)
366 if (strcmp(argv[1],"bootfile") == 0) {
367 copy_filename (BootFile, argv[2], sizeof(BootFile));
368 return 0;
369 }
370#endif /* CFG_CMD_NET */
wdenk452cfd62002-11-19 11:04:11 +0000371
stroesef8105cf2003-04-04 15:44:49 +0000372#ifdef CONFIG_AMIGAONEG3SE
wdenk452cfd62002-11-19 11:04:11 +0000373 if (strcmp(argv[1], "vga_fg_color") == 0 ||
374 strcmp(argv[1], "vga_bg_color") == 0 ) {
375 extern void video_set_color(unsigned char attr);
376 extern unsigned char video_get_attr(void);
377
378 video_set_color(video_get_attr());
379 return 0;
380 }
381#endif /* CONFIG_AMIGAONEG3SE */
382
wdenka68d3ed2002-10-11 08:38:32 +0000383 return 0;
384}
385
386void setenv (char *varname, char *varvalue)
387{
388 char *argv[4] = { "setenv", varname, varvalue, NULL };
389 _do_setenv (0, 3, argv);
390}
391
392int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
393{
394 if (argc < 2) {
395 printf ("Usage:\n%s\n", cmdtp->usage);
396 return 1;
397 }
398
399 return _do_setenv (flag, argc, argv);
400}
401
402/************************************************************************
403 * Prompt for environment variable
404 */
405
406#if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
407int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
408{
409 extern char console_buffer[CFG_CBSIZE];
410 char message[CFG_CBSIZE];
411 int size = CFG_CBSIZE - 1;
412 int len;
413 char *local_args[4];
414
415 local_args[0] = argv[0];
416 local_args[1] = argv[1];
417 local_args[2] = NULL;
418 local_args[3] = NULL;
419
420 if (argc < 2) {
421 printf ("Usage:\n%s\n", cmdtp->usage);
422 return 1;
423 }
424 /* Check the syntax */
425 switch (argc) {
426 case 1:
427 printf ("Usage:\n%s\n", cmdtp->usage);
428 return 1;
429
430 case 2: /* askenv envname */
431 sprintf (message, "Please enter '%s':", argv[1]);
432 break;
433
434 case 3: /* askenv envname size */
435 sprintf (message, "Please enter '%s':", argv[1]);
436 size = simple_strtoul (argv[2], NULL, 10);
437 break;
438
439 default: /* askenv envname message1 ... messagen size */
440 {
441 int i;
442 int pos = 0;
443
444 for (i = 2; i < argc - 1; i++) {
445 if (pos) {
446 message[pos++] = ' ';
447 }
448 strcpy (message+pos, argv[i]);
449 pos += strlen(argv[i]);
450 }
451 message[pos] = '\0';
452 size = simple_strtoul (argv[argc - 1], NULL, 10);
453 }
454 break;
455 }
456
457 if (size >= CFG_CBSIZE)
458 size = CFG_CBSIZE - 1;
459
460 if (size <= 0)
461 return 1;
462
463 /* prompt for input */
464 len = readline (message);
465
466 if (size < len)
467 console_buffer[size] = '\0';
468
469 len = 2;
470 if (console_buffer[0] != '\0') {
471 local_args[2] = console_buffer;
472 len = 3;
473 }
474
475 /* Continue calling setenv code */
476 return _do_setenv (flag, len, local_args);
477}
478#endif /* CFG_CMD_ASKENV */
479
480/************************************************************************
481 * Look up variable from environment,
482 * return address of storage for that variable,
483 * or NULL if not found
484 */
485
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200486char *getenv (char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000487{
488 int i, nxt;
489
wdenk2a3cb022002-11-05 21:01:48 +0000490 WATCHDOG_RESET();
491
wdenka68d3ed2002-10-11 08:38:32 +0000492 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
493 int val;
494
495 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
496 if (nxt >= CFG_ENV_SIZE) {
497 return (NULL);
498 }
499 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200500 if ((val=envmatch((uchar *)name, i)) < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000501 continue;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200502 return ((char *)env_get_addr(val));
wdenka68d3ed2002-10-11 08:38:32 +0000503 }
504
505 return (NULL);
506}
507
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200508int getenv_r (char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000509{
510 int i, nxt;
511
512 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
513 int val, n;
514
515 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
516 if (nxt >= CFG_ENV_SIZE) {
517 return (-1);
518 }
519 }
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200520 if ((val=envmatch((uchar *)name, i)) < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000521 continue;
522 /* found; copy out */
523 n = 0;
524 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
525 ;
526 if (len == n)
527 *buf = '\0';
528 return (n);
529 }
530 return (-1);
531}
532
533#if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
534 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
Markus Klotzbuecherbe8ebab2006-03-08 00:04:04 +0100535 (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
536 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \
537 (CFG_CMD_ENV|CFG_CMD_NAND))
wdenka68d3ed2002-10-11 08:38:32 +0000538int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
539{
540 extern char * env_name_spec;
541
542 printf ("Saving Environment to %s...\n", env_name_spec);
543
544 return (saveenv() ? 1 : 0);
545}
wdenk57b2d802003-06-27 21:31:46 +0000546
547
wdenka68d3ed2002-10-11 08:38:32 +0000548#endif
549
550
551/************************************************************************
552 * Match a name / name=value pair
553 *
554 * s1 is either a simple 'name', or a 'name=value' pair.
555 * i2 is the environment index for a 'name2=value2' pair.
556 * If the names match, return the index for the value2, else NULL.
557 */
558
559static int
560envmatch (uchar *s1, int i2)
561{
562
563 while (*s1 == env_get_char(i2++))
564 if (*s1++ == '=')
565 return(i2);
566 if (*s1 == '\0' && env_get_char(i2-1) == '=')
567 return(i2);
568 return(-1);
569}
wdenk57b2d802003-06-27 21:31:46 +0000570
571
572/**************************************************/
573
wdenkf287a242003-07-01 21:06:45 +0000574U_BOOT_CMD(
575 printenv, CFG_MAXARGS, 1, do_printenv,
wdenk57b2d802003-06-27 21:31:46 +0000576 "printenv- print environment variables\n",
577 "\n - print values of all environment variables\n"
578 "printenv name ...\n"
579 " - print value of environment variable 'name'\n"
580);
581
wdenkf287a242003-07-01 21:06:45 +0000582U_BOOT_CMD(
583 setenv, CFG_MAXARGS, 0, do_setenv,
wdenk57b2d802003-06-27 21:31:46 +0000584 "setenv - set environment variables\n",
585 "name value ...\n"
586 " - set environment variable 'name' to 'value ...'\n"
587 "setenv name\n"
588 " - delete environment variable 'name'\n"
589);
590
wdenk57bfdd42004-09-29 22:55:14 +0000591#if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
592 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
Markus Klotzbuecherbe8ebab2006-03-08 00:04:04 +0100593 (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
594 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \
595 (CFG_CMD_ENV|CFG_CMD_NAND))
wdenkf287a242003-07-01 21:06:45 +0000596U_BOOT_CMD(
597 saveenv, 1, 0, do_saveenv,
wdenk57b2d802003-06-27 21:31:46 +0000598 "saveenv - save environment variables to persistent storage\n",
599 NULL
600);
601
602#endif /* CFG_CMD_ENV */
603
604#if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
605
wdenkf287a242003-07-01 21:06:45 +0000606U_BOOT_CMD(
607 askenv, CFG_MAXARGS, 1, do_askenv,
wdenk57b2d802003-06-27 21:31:46 +0000608 "askenv - get environment variables from stdin\n",
609 "name [message] [size]\n"
610 " - get environment variable 'name' from stdin (max 'size' chars)\n"
611 "askenv name\n"
612 " - get environment variable 'name' from stdin\n"
613 "askenv name size\n"
614 " - get environment variable 'name' from stdin (max 'size' chars)\n"
615 "askenv name [message] size\n"
616 " - display 'message' string and get environment variable 'name'"
617 "from stdin (max 'size' chars)\n"
618);
619#endif /* CFG_CMD_ASKENV */
620
621#if (CONFIG_COMMANDS & CFG_CMD_RUN)
622int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenkf287a242003-07-01 21:06:45 +0000623U_BOOT_CMD(
624 run, CFG_MAXARGS, 1, do_run,
wdenk57b2d802003-06-27 21:31:46 +0000625 "run - run commands in an environment variable\n",
626 "var [...]\n"
627 " - run the commands in the environment variable(s) 'var'\n"
628);
629#endif /* CFG_CMD_RUN */