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>
46 #include <linux/stddef.h>
47 #include <asm/byteorder.h>
48 #if (CONFIG_COMMANDS & CFG_CMD_NET)
52 #if !defined(CFG_ENV_IS_IN_NVRAM) && \
53 !defined(CFG_ENV_IS_IN_EEPROM) && \
54 !defined(CFG_ENV_IS_IN_FLASH) && \
55 !defined(CFG_ENV_IS_IN_DATAFLASH) && \
56 !defined(CFG_ENV_IS_IN_NAND) && \
57 !defined(CFG_ENV_IS_NOWHERE)
58 # error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE}
62 #define MK_STR(x) XMK_STR(x)
64 /************************************************************************
65 ************************************************************************/
67 /* Function that returns a character from the environment */
68 extern uchar (*env_get_char)(int);
70 /* Function that returns a pointer to a value from the environment */
71 /* (Only memory version supported / needed). */
72 extern uchar *env_get_addr(int);
74 /* Function that updates CRC of the enironment */
75 extern void env_crc_update (void);
77 /************************************************************************
78 ************************************************************************/
80 static int envmatch (uchar *, int);
83 * Table with supported baudrates (defined in config_xyz.h)
85 static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
86 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
89 /************************************************************************
90 * Command interface: print one or all environment variables
93 int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
98 if (argc == 1) { /* Print all env variables */
99 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
100 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
102 for (k=i; k<nxt; ++k)
103 putc(env_get_char(k));
107 puts ("\n ** Abort\n");
112 printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE);
117 for (i=1; i<argc; ++i) { /* print single env variables */
118 char *name = argv[i];
122 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
124 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
126 k = envmatch(name, j);
133 putc(env_get_char(k++));
138 printf ("## Error: \"%s\" not defined\n", name);
145 /************************************************************************
146 * Set a new environment variable,
147 * or replace or delete an existing one.
149 * This function will ONLY work with a in-RAM copy of the environment
152 int _do_setenv (int flag, int argc, char *argv[])
154 DECLARE_GLOBAL_DATA_PTR;
158 uchar *env, *nxt = NULL;
162 uchar *env_data = env_get_addr(0);
164 if (!env_data) /* need copy in RAM */
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(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,
190 if ( (strcmp (name, "serial#") == 0) ||
191 ((strcmp (name, "ethaddr") == 0)
192 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
193 && (strcmp (env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
194 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
196 printf ("Can't overwrite \"%s\"\n", name);
201 /* Check for console redirection */
202 if (strcmp(name,"stdin") == 0) {
204 } else if (strcmp(name,"stdout") == 0) {
206 } else if (strcmp(name,"stderr") == 0) {
211 if (argc < 3) { /* Cannot delete it! */
212 printf("Can't delete \"%s\"\n", name);
216 /* Try assigning specified device */
217 if (console_assign (console, argv[2]) < 0)
222 * Switch to new baudrate if new baudrate is supported
224 if (strcmp(argv[1],"baudrate") == 0) {
225 int baudrate = simple_strtoul(argv[2], NULL, 10);
227 for (i=0; i<N_BAUDRATES; ++i) {
228 if (baudrate == baudrate_table[i])
231 if (i == N_BAUDRATES) {
232 printf ("## Baudrate %d bps not supported\n",
236 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
239 gd->baudrate = baudrate;
241 gd->bd->bi_baudrate = baudrate;
252 if (*++nxt == '\0') {
253 if (env > env_data) {
261 if ((*env == '\0') && (*nxt == '\0'))
269 #ifdef CONFIG_NET_MULTI
270 if (strncmp(name, "eth", 3) == 0) {
272 int num = simple_strtoul(name+3, &end, 10);
274 if (strcmp(end, "addr") == 0) {
275 eth_set_enetaddr(num, argv[2]);
282 if ((argc < 3) || argv[2] == NULL) {
288 * Append new definition at the end
290 for (env=env_data; *env || *(env+1); ++env)
296 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
298 len = strlen(name) + 2;
299 /* add '=' for first arg, ' ' for all others */
300 for (i=2; i<argc; ++i) {
301 len += strlen(argv[i]) + 1;
303 if (len > (&env_data[ENV_SIZE]-env)) {
304 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
307 while ((*env = *name++) != '\0')
309 for (i=2; i<argc; ++i) {
312 *env = (i==2) ? '=' : ' ';
313 while ((*++env = *val++) != '\0')
317 /* end is marked with double '\0' */
324 * Some variables should be updated when the corresponding
325 * entry in the enviornment is changed
328 if (strcmp(argv[1],"ethaddr") == 0) {
329 char *s = argv[2]; /* always use only one arg */
331 for (i=0; i<6; ++i) {
332 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
333 if (s) s = (*e) ? e+1 : e;
335 #ifdef CONFIG_NET_MULTI
336 eth_set_enetaddr(0, argv[2]);
341 if (strcmp(argv[1],"ipaddr") == 0) {
342 char *s = argv[2]; /* always use only one arg */
346 for (addr=0, i=0; i<4; ++i) {
347 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
349 addr |= (val & 0xFF);
350 if (s) s = (*e) ? e+1 : e;
352 bd->bi_ip_addr = htonl(addr);
355 if (strcmp(argv[1],"loadaddr") == 0) {
356 load_addr = simple_strtoul(argv[2], NULL, 16);
359 #if (CONFIG_COMMANDS & CFG_CMD_NET)
360 if (strcmp(argv[1],"bootfile") == 0) {
361 copy_filename (BootFile, argv[2], sizeof(BootFile));
364 #endif /* CFG_CMD_NET */
366 #ifdef CONFIG_AMIGAONEG3SE
367 if (strcmp(argv[1], "vga_fg_color") == 0 ||
368 strcmp(argv[1], "vga_bg_color") == 0 ) {
369 extern void video_set_color(unsigned char attr);
370 extern unsigned char video_get_attr(void);
372 video_set_color(video_get_attr());
375 #endif /* CONFIG_AMIGAONEG3SE */
380 void setenv (char *varname, char *varvalue)
382 char *argv[4] = { "setenv", varname, varvalue, NULL };
383 _do_setenv (0, 3, argv);
386 int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
389 printf ("Usage:\n%s\n", cmdtp->usage);
393 return _do_setenv (flag, argc, argv);
396 /************************************************************************
397 * Prompt for environment variable
400 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
401 int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
403 extern char console_buffer[CFG_CBSIZE];
404 char message[CFG_CBSIZE];
405 int size = CFG_CBSIZE - 1;
409 local_args[0] = argv[0];
410 local_args[1] = argv[1];
411 local_args[2] = NULL;
412 local_args[3] = NULL;
415 printf ("Usage:\n%s\n", cmdtp->usage);
418 /* Check the syntax */
421 printf ("Usage:\n%s\n", cmdtp->usage);
424 case 2: /* askenv envname */
425 sprintf (message, "Please enter '%s':", argv[1]);
428 case 3: /* askenv envname size */
429 sprintf (message, "Please enter '%s':", argv[1]);
430 size = simple_strtoul (argv[2], NULL, 10);
433 default: /* askenv envname message1 ... messagen size */
438 for (i = 2; i < argc - 1; i++) {
440 message[pos++] = ' ';
442 strcpy (message+pos, argv[i]);
443 pos += strlen(argv[i]);
446 size = simple_strtoul (argv[argc - 1], NULL, 10);
451 if (size >= CFG_CBSIZE)
452 size = CFG_CBSIZE - 1;
457 /* prompt for input */
458 len = readline (message);
461 console_buffer[size] = '\0';
464 if (console_buffer[0] != '\0') {
465 local_args[2] = console_buffer;
469 /* Continue calling setenv code */
470 return _do_setenv (flag, len, local_args);
472 #endif /* CFG_CMD_ASKENV */
474 /************************************************************************
475 * Look up variable from environment,
476 * return address of storage for that variable,
477 * or NULL if not found
480 char *getenv (uchar *name)
486 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
489 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
490 if (nxt >= CFG_ENV_SIZE) {
494 if ((val=envmatch(name, i)) < 0)
496 return (env_get_addr(val));
502 int getenv_r (uchar *name, uchar *buf, unsigned len)
506 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
509 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
510 if (nxt >= CFG_ENV_SIZE) {
514 if ((val=envmatch(name, i)) < 0)
516 /* found; copy out */
518 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
527 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
528 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
529 (CFG_CMD_ENV|CFG_CMD_FLASH))
530 int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
532 extern char * env_name_spec;
534 printf ("Saving Environment to %s...\n", env_name_spec);
536 return (saveenv() ? 1 : 0);
543 /************************************************************************
544 * Match a name / name=value pair
546 * s1 is either a simple 'name', or a 'name=value' pair.
547 * i2 is the environment index for a 'name2=value2' pair.
548 * If the names match, return the index for the value2, else NULL.
552 envmatch (uchar *s1, int i2)
555 while (*s1 == env_get_char(i2++))
558 if (*s1 == '\0' && env_get_char(i2-1) == '=')
564 /**************************************************/
567 printenv, CFG_MAXARGS, 1, do_printenv,
568 "printenv- print environment variables\n",
569 "\n - print values of all environment variables\n"
570 "printenv name ...\n"
571 " - print value of environment variable 'name'\n"
575 setenv, CFG_MAXARGS, 0, do_setenv,
576 "setenv - set environment variables\n",
578 " - set environment variable 'name' to 'value ...'\n"
580 " - delete environment variable 'name'\n"
583 #if ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
586 saveenv, 1, 0, do_saveenv,
587 "saveenv - save environment variables to persistent storage\n",
591 #endif /* CFG_CMD_ENV */
593 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
596 askenv, CFG_MAXARGS, 1, do_askenv,
597 "askenv - get environment variables from stdin\n",
598 "name [message] [size]\n"
599 " - get environment variable 'name' from stdin (max 'size' chars)\n"
601 " - get environment variable 'name' from stdin\n"
603 " - get environment variable 'name' from stdin (max 'size' chars)\n"
604 "askenv name [message] size\n"
605 " - display 'message' string and get environment variable 'name'"
606 "from stdin (max 'size' chars)\n"
608 #endif /* CFG_CMD_ASKENV */
610 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
611 int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
613 run, CFG_MAXARGS, 1, do_run,
614 "run - run commands in an environment variable\n",
616 " - run the commands in the environment variable(s) 'var'\n"
618 #endif /* CFG_CMD_RUN */