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((uchar
*)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((uchar
*)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 ((char *)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
) {
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) {
334 char *s
= argv
[2]; /* always use only one arg */
336 for (i
=0; i
<6; ++i
) {
337 bd
->bi_enetaddr
[i
] = s
? simple_strtoul(s
, &e
, 16) : 0;
338 if (s
) s
= (*e
) ? e
+1 : e
;
340 #ifdef CONFIG_NET_MULTI
341 eth_set_enetaddr(0, argv
[2]);
346 if (strcmp(argv
[1],"ipaddr") == 0) {
347 char *s
= argv
[2]; /* always use only one arg */
351 for (addr
=0, i
=0; i
<4; ++i
) {
352 ulong val
= s
? simple_strtoul(s
, &e
, 10) : 0;
354 addr
|= (val
& 0xFF);
355 if (s
) s
= (*e
) ? e
+1 : e
;
357 bd
->bi_ip_addr
= htonl(addr
);
360 if (strcmp(argv
[1],"loadaddr") == 0) {
361 load_addr
= simple_strtoul(argv
[2], NULL
, 16);
364 #if (CONFIG_COMMANDS & CFG_CMD_NET)
365 if (strcmp(argv
[1],"bootfile") == 0) {
366 copy_filename (BootFile
, argv
[2], sizeof(BootFile
));
369 #endif /* CFG_CMD_NET */
371 #ifdef CONFIG_AMIGAONEG3SE
372 if (strcmp(argv
[1], "vga_fg_color") == 0 ||
373 strcmp(argv
[1], "vga_bg_color") == 0 ) {
374 extern void video_set_color(unsigned char attr
);
375 extern unsigned char video_get_attr(void);
377 video_set_color(video_get_attr());
380 #endif /* CONFIG_AMIGAONEG3SE */
385 void setenv (char *varname
, char *varvalue
)
387 char *argv
[4] = { "setenv", varname
, varvalue
, NULL
};
388 _do_setenv (0, 3, argv
);
391 int do_setenv ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
394 printf ("Usage:\n%s\n", cmdtp
->usage
);
398 return _do_setenv (flag
, argc
, argv
);
401 /************************************************************************
402 * Prompt for environment variable
405 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
406 int do_askenv ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
408 extern char console_buffer
[CFG_CBSIZE
];
409 char message
[CFG_CBSIZE
];
410 int size
= CFG_CBSIZE
- 1;
414 local_args
[0] = argv
[0];
415 local_args
[1] = argv
[1];
416 local_args
[2] = NULL
;
417 local_args
[3] = NULL
;
420 printf ("Usage:\n%s\n", cmdtp
->usage
);
423 /* Check the syntax */
426 printf ("Usage:\n%s\n", cmdtp
->usage
);
429 case 2: /* askenv envname */
430 sprintf (message
, "Please enter '%s':", argv
[1]);
433 case 3: /* askenv envname size */
434 sprintf (message
, "Please enter '%s':", argv
[1]);
435 size
= simple_strtoul (argv
[2], NULL
, 10);
438 default: /* askenv envname message1 ... messagen size */
443 for (i
= 2; i
< argc
- 1; i
++) {
445 message
[pos
++] = ' ';
447 strcpy (message
+pos
, argv
[i
]);
448 pos
+= strlen(argv
[i
]);
451 size
= simple_strtoul (argv
[argc
- 1], NULL
, 10);
456 if (size
>= CFG_CBSIZE
)
457 size
= CFG_CBSIZE
- 1;
462 /* prompt for input */
463 len
= readline (message
);
466 console_buffer
[size
] = '\0';
469 if (console_buffer
[0] != '\0') {
470 local_args
[2] = console_buffer
;
474 /* Continue calling setenv code */
475 return _do_setenv (flag
, len
, local_args
);
477 #endif /* CFG_CMD_ASKENV */
479 /************************************************************************
480 * Look up variable from environment,
481 * return address of storage for that variable,
482 * or NULL if not found
485 char *getenv (char *name
)
491 for (i
=0; env_get_char(i
) != '\0'; i
=nxt
+1) {
494 for (nxt
=i
; env_get_char(nxt
) != '\0'; ++nxt
) {
495 if (nxt
>= CFG_ENV_SIZE
) {
499 if ((val
=envmatch((uchar
*)name
, i
)) < 0)
501 return ((char *)env_get_addr(val
));
507 int getenv_r (char *name
, char *buf
, unsigned len
)
511 for (i
=0; env_get_char(i
) != '\0'; i
=nxt
+1) {
514 for (nxt
=i
; env_get_char(nxt
) != '\0'; ++nxt
) {
515 if (nxt
>= CFG_ENV_SIZE
) {
519 if ((val
=envmatch((uchar
*)name
, i
)) < 0)
521 /* found; copy out */
523 while ((len
> n
++) && (*buf
++ = env_get_char(val
++)) != '\0')
532 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || defined(CFG_ENV_IS_IN_NAND) || \
533 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
534 (CFG_CMD_ENV|CFG_CMD_FLASH))
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);
548 /************************************************************************
549 * Match a name / name=value pair
551 * s1 is either a simple 'name', or a 'name=value' pair.
552 * i2 is the environment index for a 'name2=value2' pair.
553 * If the names match, return the index for the value2, else NULL.
557 envmatch (uchar
*s1
, int i2
)
560 while (*s1
== env_get_char(i2
++))
563 if (*s1
== '\0' && env_get_char(i2
-1) == '=')
569 /**************************************************/
572 printenv
, CFG_MAXARGS
, 1, do_printenv
,
573 "printenv- print environment variables\n",
574 "\n - print values of all environment variables\n"
575 "printenv name ...\n"
576 " - print value of environment variable 'name'\n"
580 setenv
, CFG_MAXARGS
, 0, do_setenv
,
581 "setenv - set environment variables\n",
583 " - set environment variable 'name' to 'value ...'\n"
585 " - delete environment variable 'name'\n"
588 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || defined(CFG_ENV_IS_IN_NAND) || \
589 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
590 (CFG_CMD_ENV|CFG_CMD_FLASH))
592 saveenv
, 1, 0, do_saveenv
,
593 "saveenv - save environment variables to persistent storage\n",
597 #endif /* CFG_CMD_ENV */
599 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
602 askenv
, CFG_MAXARGS
, 1, do_askenv
,
603 "askenv - get environment variables from stdin\n",
604 "name [message] [size]\n"
605 " - get environment variable 'name' from stdin (max 'size' chars)\n"
607 " - get environment variable 'name' from stdin\n"
609 " - get environment variable 'name' from stdin (max 'size' chars)\n"
610 "askenv name [message] size\n"
611 " - display 'message' string and get environment variable 'name'"
612 "from stdin (max 'size' chars)\n"
614 #endif /* CFG_CMD_ASKENV */
616 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
617 int do_run (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[]);
619 run
, CFG_MAXARGS
, 1, do_run
,
620 "run - run commands in an environment variable\n",
622 " - run the commands in the environment variable(s) 'var'\n"
624 #endif /* CFG_CMD_RUN */