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 (CONFIG_COMMANDS & CFG_CMD_NET)
53 #if !defined(CFG_ENV_IS_IN_NVRAM) && \
54 !defined(CFG_ENV_IS_IN_EEPROM) && \
55 !defined(CFG_ENV_IS_IN_FLASH) && \
56 !defined(CFG_ENV_IS_IN_DATAFLASH) && \
57 !defined(CFG_ENV_IS_IN_NAND) && \
58 !defined(CFG_ENV_IS_NOWHERE)
59 # error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE}
63 #define MK_STR(x) XMK_STR(x)
65 /************************************************************************
66 ************************************************************************/
68 /* Function that returns a character from the environment */
69 extern uchar (*env_get_char)(int);
71 /* Function that returns a pointer to a value from the environment */
72 /* (Only memory version supported / needed). */
73 extern uchar *env_get_addr(int);
75 /* Function that updates CRC of the enironment */
76 extern void env_crc_update (void);
78 /************************************************************************
79 ************************************************************************/
81 static int envmatch (uchar *, int);
84 * Table with supported baudrates (defined in config_xyz.h)
86 static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
87 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
90 /************************************************************************
91 * Command interface: print one or all environment variables
94 int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
99 if (argc == 1) { /* Print all env variables */
100 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
101 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
103 for (k=i; k<nxt; ++k)
104 putc(env_get_char(k));
108 puts ("\n ** Abort\n");
113 printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE);
118 for (i=1; i<argc; ++i) { /* print single env variables */
119 char *name = argv[i];
123 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
125 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
127 k = envmatch(name, j);
134 putc(env_get_char(k++));
139 printf ("## Error: \"%s\" not defined\n", name);
146 /************************************************************************
147 * Set a new environment variable,
148 * or replace or delete an existing one.
150 * This function will ONLY work with a in-RAM copy of the environment
153 int _do_setenv (int flag, int argc, char *argv[])
155 DECLARE_GLOBAL_DATA_PTR;
159 uchar *env, *nxt = NULL;
163 uchar *env_data = env_get_addr(0);
165 if (!env_data) /* need copy in RAM */
171 * search if variable with this name already exists
174 for (env=env_data; *env; env=nxt+1) {
175 for (nxt=env; *nxt; ++nxt)
177 if ((oldval = envmatch(name, env-env_data)) >= 0)
182 * Delete any existing definition
185 #ifndef CONFIG_ENV_OVERWRITE
188 * Ethernet Address and serial# can be set only once,
191 if ( (strcmp (name, "serial#") == 0) ||
192 ((strcmp (name, "ethaddr") == 0)
193 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
194 && (strcmp (env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
195 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
197 printf ("Can't overwrite \"%s\"\n", name);
202 /* Check for console redirection */
203 if (strcmp(name,"stdin") == 0) {
205 } else if (strcmp(name,"stdout") == 0) {
207 } else if (strcmp(name,"stderr") == 0) {
212 if (argc < 3) { /* Cannot delete it! */
213 printf("Can't delete \"%s\"\n", name);
217 /* Try assigning specified device */
218 if (console_assign (console, argv[2]) < 0)
221 #ifdef CONFIG_SERIAL_MULTI
222 if (serial_assign (argv[2]) < 0)
228 * Switch to new baudrate if new baudrate is supported
230 if (strcmp(argv[1],"baudrate") == 0) {
231 int baudrate = simple_strtoul(argv[2], NULL, 10);
233 for (i=0; i<N_BAUDRATES; ++i) {
234 if (baudrate == baudrate_table[i])
237 if (i == N_BAUDRATES) {
238 printf ("## Baudrate %d bps not supported\n",
242 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
245 gd->baudrate = baudrate;
247 gd->bd->bi_baudrate = baudrate;
258 if (*++nxt == '\0') {
259 if (env > env_data) {
267 if ((*env == '\0') && (*nxt == '\0'))
275 #ifdef CONFIG_NET_MULTI
276 if (strncmp(name, "eth", 3) == 0) {
278 int num = simple_strtoul(name+3, &end, 10);
280 if (strcmp(end, "addr") == 0) {
281 eth_set_enetaddr(num, argv[2]);
288 if ((argc < 3) || argv[2] == NULL) {
294 * Append new definition at the end
296 for (env=env_data; *env || *(env+1); ++env)
302 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
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;
309 if (len > (&env_data[ENV_SIZE]-env)) {
310 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
313 while ((*env = *name++) != '\0')
315 for (i=2; i<argc; ++i) {
318 *env = (i==2) ? '=' : ' ';
319 while ((*++env = *val++) != '\0')
323 /* end is marked with double '\0' */
330 * Some variables should be updated when the corresponding
331 * entry in the enviornment is changed
334 if (strcmp(argv[1],"ethaddr") == 0) {
335 char *s = argv[2]; /* always use only one arg */
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;
341 #ifdef CONFIG_NET_MULTI
342 eth_set_enetaddr(0, argv[2]);
347 if (strcmp(argv[1],"ipaddr") == 0) {
348 char *s = argv[2]; /* always use only one arg */
352 for (addr=0, i=0; i<4; ++i) {
353 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
355 addr |= (val & 0xFF);
356 if (s) s = (*e) ? e+1 : e;
358 bd->bi_ip_addr = htonl(addr);
361 if (strcmp(argv[1],"loadaddr") == 0) {
362 load_addr = simple_strtoul(argv[2], NULL, 16);
365 #if (CONFIG_COMMANDS & CFG_CMD_NET)
366 if (strcmp(argv[1],"bootfile") == 0) {
367 copy_filename (BootFile, argv[2], sizeof(BootFile));
370 #endif /* CFG_CMD_NET */
372 #ifdef CONFIG_AMIGAONEG3SE
373 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);
378 video_set_color(video_get_attr());
381 #endif /* CONFIG_AMIGAONEG3SE */
386 void setenv (char *varname, char *varvalue)
388 char *argv[4] = { "setenv", varname, varvalue, NULL };
389 _do_setenv (0, 3, argv);
392 int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
395 printf ("Usage:\n%s\n", cmdtp->usage);
399 return _do_setenv (flag, argc, argv);
402 /************************************************************************
403 * Prompt for environment variable
406 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
407 int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
409 extern char console_buffer[CFG_CBSIZE];
410 char message[CFG_CBSIZE];
411 int size = CFG_CBSIZE - 1;
415 local_args[0] = argv[0];
416 local_args[1] = argv[1];
417 local_args[2] = NULL;
418 local_args[3] = NULL;
421 printf ("Usage:\n%s\n", cmdtp->usage);
424 /* Check the syntax */
427 printf ("Usage:\n%s\n", cmdtp->usage);
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 >= CFG_CBSIZE)
458 size = CFG_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);
478 #endif /* CFG_CMD_ASKENV */
480 /************************************************************************
481 * Look up variable from environment,
482 * return address of storage for that variable,
483 * or NULL if not found
486 char *getenv (uchar *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 >= CFG_ENV_SIZE) {
500 if ((val=envmatch(name, i)) < 0)
502 return (env_get_addr(val));
508 int getenv_r (uchar *name, uchar *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 >= CFG_ENV_SIZE) {
520 if ((val=envmatch(name, i)) < 0)
522 /* found; copy out */
524 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
533 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
534 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
535 (CFG_CMD_ENV|CFG_CMD_FLASH))
536 int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
538 extern char * env_name_spec;
540 printf ("Saving Environment to %s...\n", env_name_spec);
542 return (saveenv() ? 1 : 0);
549 /************************************************************************
550 * Match a name / name=value pair
552 * s1 is either a simple 'name', or a 'name=value' pair.
553 * i2 is the environment index for a 'name2=value2' pair.
554 * If the names match, return the index for the value2, else NULL.
558 envmatch (uchar *s1, int i2)
561 while (*s1 == env_get_char(i2++))
564 if (*s1 == '\0' && env_get_char(i2-1) == '=')
570 /**************************************************/
573 printenv, CFG_MAXARGS, 1, do_printenv,
574 "printenv- print environment variables\n",
575 "\n - print values of all environment variables\n"
576 "printenv name ...\n"
577 " - print value of environment variable 'name'\n"
581 setenv, CFG_MAXARGS, 0, do_setenv,
582 "setenv - set environment variables\n",
584 " - set environment variable 'name' to 'value ...'\n"
586 " - delete environment variable 'name'\n"
589 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
590 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
591 (CFG_CMD_ENV|CFG_CMD_FLASH))
593 saveenv, 1, 0, do_saveenv,
594 "saveenv - save environment variables to persistent storage\n",
598 #endif /* CFG_CMD_ENV */
600 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
603 askenv, CFG_MAXARGS, 1, do_askenv,
604 "askenv - get environment variables from stdin\n",
605 "name [message] [size]\n"
606 " - get environment variable 'name' from stdin (max 'size' chars)\n"
608 " - get environment variable 'name' from stdin\n"
610 " - get environment variable 'name' from stdin (max 'size' chars)\n"
611 "askenv name [message] size\n"
612 " - display 'message' string and get environment variable 'name'"
613 "from stdin (max 'size' chars)\n"
615 #endif /* CFG_CMD_ASKENV */
617 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
618 int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
620 run, CFG_MAXARGS, 1, do_run,
621 "run - run commands in an environment variable\n",
623 " - run the commands in the environment variable(s) 'var'\n"
625 #endif /* CFG_CMD_RUN */