2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 /**************************************************************************
29 * Support for persistent environment data
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.
37 * The environment is preceeded by a 32 bit CRC over the data part.
39 **************************************************************************
44 #include <environment.h>
47 #include <linux/stddef.h>
48 #include <asm/byteorder.h>
49 #if defined(CONFIG_CMD_NET)
53 DECLARE_GLOBAL_DATA_PTR;
55 #if !defined(CONFIG_ENV_IS_IN_NVRAM) && \
56 !defined(CONFIG_ENV_IS_IN_EEPROM) && \
57 !defined(CONFIG_ENV_IS_IN_FLASH) && \
58 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
59 !defined(CONFIG_ENV_IS_IN_NAND) && \
60 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
61 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
62 !defined(CONFIG_ENV_IS_NOWHERE)
63 # error Define one of CONFIG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|ONENAND|SPI_FLASH|NOWHERE}
67 #define MK_STR(x) XMK_STR(x)
69 /************************************************************************
70 ************************************************************************/
73 * Table with supported baudrates (defined in config_xyz.h)
75 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
76 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
78 static int env_id = 1;
84 /************************************************************************
85 * Command interface: print one or all environment variables
88 int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
93 if (argc == 1) { /* Print all env variables */
94 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
95 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
98 putc(env_get_char(k));
102 puts ("\n ** Abort\n");
107 printf("\nEnvironment size: %d/%ld bytes\n",
113 for (i=1; i<argc; ++i) { /* print single env variables */
114 char *name = argv[i];
118 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
120 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
122 k = envmatch((uchar *)name, j);
129 putc(env_get_char(k++));
134 printf ("## Error: \"%s\" not defined\n", name);
141 /************************************************************************
142 * Set a new environment variable,
143 * or replace or delete an existing one.
145 * This function will ONLY work with a in-RAM copy of the environment
148 int _do_setenv (int flag, int argc, char *argv[])
152 uchar *env, *nxt = NULL;
156 uchar *env_data = env_get_addr(0);
158 if (!env_data) /* need copy in RAM */
163 if (strchr(name, '=')) {
164 printf ("## Error: illegal character '=' in variable name \"%s\"\n", name);
170 * search if variable with this name already exists
173 for (env=env_data; *env; env=nxt+1) {
174 for (nxt=env; *nxt; ++nxt)
176 if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
181 * Delete any existing definition
184 #ifndef CONFIG_ENV_OVERWRITE
187 * Ethernet Address and serial# can be set only once,
191 #ifdef CONFIG_HAS_UID
192 /* Allow serial# forced overwrite with 0xdeaf4add flag */
193 ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) ||
195 (strcmp (name, "serial#") == 0) ||
197 ((strcmp (name, "ethaddr") == 0)
198 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
199 && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
200 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
202 printf ("Can't overwrite \"%s\"\n", name);
207 /* Check for console redirection */
208 if (strcmp(name,"stdin") == 0) {
210 } else if (strcmp(name,"stdout") == 0) {
212 } else if (strcmp(name,"stderr") == 0) {
217 if (argc < 3) { /* Cannot delete it! */
218 printf("Can't delete \"%s\"\n", name);
222 #ifdef CONFIG_CONSOLE_MUX
223 i = iomux_doenv(console, argv[2]);
227 /* Try assigning specified device */
228 if (console_assign (console, argv[2]) < 0)
231 #ifdef CONFIG_SERIAL_MULTI
232 if (serial_assign (argv[2]) < 0)
235 #endif /* CONFIG_CONSOLE_MUX */
239 * Switch to new baudrate if new baudrate is supported
241 if (strcmp(argv[1],"baudrate") == 0) {
242 int baudrate = simple_strtoul(argv[2], NULL, 10);
244 for (i=0; i<N_BAUDRATES; ++i) {
245 if (baudrate == baudrate_table[i])
248 if (i == N_BAUDRATES) {
249 printf ("## Baudrate %d bps not supported\n",
253 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
256 gd->baudrate = baudrate;
257 #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
258 gd->bd->bi_baudrate = baudrate;
269 if (*++nxt == '\0') {
270 if (env > env_data) {
278 if ((*env == '\0') && (*nxt == '\0'))
287 if ((argc < 3) || argv[2] == NULL) {
293 * Append new definition at the end
295 for (env=env_data; *env || *(env+1); ++env)
301 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
303 len = strlen(name) + 2;
304 /* add '=' for first arg, ' ' for all others */
305 for (i=2; i<argc; ++i) {
306 len += strlen(argv[i]) + 1;
308 if (len > (&env_data[ENV_SIZE]-env)) {
309 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
312 while ((*env = *name++) != '\0')
314 for (i=2; i<argc; ++i) {
317 *env = (i==2) ? '=' : ' ';
318 while ((*++env = *val++) != '\0')
322 /* end is marked with double '\0' */
329 * Some variables should be updated when the corresponding
330 * entry in the enviornment is changed
333 if (strcmp(argv[1],"ethaddr") == 0)
336 if (strcmp(argv[1],"ipaddr") == 0) {
337 char *s = argv[2]; /* always use only one arg */
341 for (addr=0, i=0; i<4; ++i) {
342 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
344 addr |= (val & 0xFF);
345 if (s) s = (*e) ? e+1 : e;
347 bd->bi_ip_addr = htonl(addr);
350 if (strcmp(argv[1],"loadaddr") == 0) {
351 load_addr = simple_strtoul(argv[2], NULL, 16);
354 #if defined(CONFIG_CMD_NET)
355 if (strcmp(argv[1],"bootfile") == 0) {
356 copy_filename (BootFile, argv[2], sizeof(BootFile));
361 #ifdef CONFIG_AMIGAONEG3SE
362 if (strcmp(argv[1], "vga_fg_color") == 0 ||
363 strcmp(argv[1], "vga_bg_color") == 0 ) {
364 extern void video_set_color(unsigned char attr);
365 extern unsigned char video_get_attr(void);
367 video_set_color(video_get_attr());
370 #endif /* CONFIG_AMIGAONEG3SE */
375 int setenv (char *varname, char *varvalue)
377 char *argv[4] = { "setenv", varname, varvalue, NULL };
378 if (varvalue == NULL)
379 return _do_setenv (0, 2, argv);
381 return _do_setenv (0, 3, argv);
384 #ifdef CONFIG_HAS_UID
385 void forceenv (char *varname, char *varvalue)
387 char *argv[4] = { "forceenv", varname, varvalue, NULL };
388 _do_setenv (0xdeaf4add, 3, argv);
392 int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
399 return _do_setenv (flag, argc, argv);
402 /************************************************************************
403 * Prompt for environment variable
406 #if defined(CONFIG_CMD_ASKENV)
407 int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
409 extern char console_buffer[CONFIG_SYS_CBSIZE];
410 char message[CONFIG_SYS_CBSIZE];
411 int size = CONFIG_SYS_CBSIZE - 1;
415 local_args[0] = argv[0];
416 local_args[1] = argv[1];
417 local_args[2] = NULL;
418 local_args[3] = NULL;
424 /* Check the syntax */
430 case 2: /* askenv envname */
431 sprintf (message, "Please enter '%s':", argv[1]);
434 case 3: /* askenv envname size */
435 sprintf (message, "Please enter '%s':", argv[1]);
436 size = simple_strtoul (argv[2], NULL, 10);
439 default: /* askenv envname message1 ... messagen size */
444 for (i = 2; i < argc - 1; i++) {
446 message[pos++] = ' ';
448 strcpy (message+pos, argv[i]);
449 pos += strlen(argv[i]);
452 size = simple_strtoul (argv[argc - 1], NULL, 10);
457 if (size >= CONFIG_SYS_CBSIZE)
458 size = CONFIG_SYS_CBSIZE - 1;
463 /* prompt for input */
464 len = readline (message);
467 console_buffer[size] = '\0';
470 if (console_buffer[0] != '\0') {
471 local_args[2] = console_buffer;
475 /* Continue calling setenv code */
476 return _do_setenv (flag, len, local_args);
480 /************************************************************************
481 * Look up variable from environment,
482 * return address of storage for that variable,
483 * or NULL if not found
486 char *getenv (char *name)
492 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
495 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
496 if (nxt >= CONFIG_ENV_SIZE) {
500 if ((val=envmatch((uchar *)name, i)) < 0)
502 return ((char *)env_get_addr(val));
508 int getenv_r (char *name, char *buf, unsigned len)
512 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
515 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
516 if (nxt >= CONFIG_ENV_SIZE) {
520 if ((val=envmatch((uchar *)name, i)) < 0)
522 /* found; copy out */
524 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
533 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
535 int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
537 extern char * env_name_spec;
539 printf ("Saving Environment to %s...\n", env_name_spec);
541 return (saveenv() ? 1 : 0);
545 saveenv, 1, 0, do_saveenv,
546 "save environment variables to persistent storage",
553 /************************************************************************
554 * Match a name / name=value pair
556 * s1 is either a simple 'name', or a 'name=value' pair.
557 * i2 is the environment index for a 'name2=value2' pair.
558 * If the names match, return the index for the value2, else NULL.
561 int envmatch (uchar *s1, int i2)
564 while (*s1 == env_get_char(i2++))
567 if (*s1 == '\0' && env_get_char(i2-1) == '=')
573 /**************************************************/
576 printenv, CONFIG_SYS_MAXARGS, 1, do_printenv,
577 "print environment variables",
578 "\n - print values of all environment variables\n"
579 "printenv name ...\n"
580 " - print value of environment variable 'name'\n"
584 setenv, CONFIG_SYS_MAXARGS, 0, do_setenv,
585 "set environment variables",
587 " - set environment variable 'name' to 'value ...'\n"
589 " - delete environment variable 'name'\n"
592 #if defined(CONFIG_CMD_ASKENV)
595 askenv, CONFIG_SYS_MAXARGS, 1, do_askenv,
596 "get environment variables from stdin",
597 "name [message] [size]\n"
598 " - get environment variable 'name' from stdin (max 'size' chars)\n"
600 " - get environment variable 'name' from stdin\n"
602 " - get environment variable 'name' from stdin (max 'size' chars)\n"
603 "askenv name [message] size\n"
604 " - display 'message' string and get environment variable 'name'"
605 "from stdin (max 'size' chars)\n"
609 #if defined(CONFIG_CMD_RUN)
610 int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
612 run, CONFIG_SYS_MAXARGS, 1, do_run,
613 "run commands in an environment variable",
615 " - run the commands in the environment variable(s) 'var'\n"