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>
45 #include <cmd_nvedit.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) && !defined(CFG_ENV_IS_IN_EEPROM) && !defined(CFG_ENV_IS_IN_FLASH) && !defined(CFG_ENV_IS_NOWHERE)
53 # error Define one of CFG_ENV_IS_IN_NVRAM, CFG_ENV_IS_IN_EEPROM, CFG_ENV_IS_IN_FLASH, CFG_ENV_IS_NOWHERE
57 #define MK_STR(x) XMK_STR(x)
59 /************************************************************************
60 ************************************************************************/
62 /* Function that returns a character from the environment */
63 extern uchar (*env_get_char)(int);
65 /* Function that returns a pointer to a value from the environment */
66 /* (Only memory version supported / needed). */
67 extern uchar *env_get_addr(int);
69 /* Function that updates CRC of the enironment */
70 extern void env_crc_update (void);
72 /************************************************************************
73 ************************************************************************/
75 static int envmatch (uchar *, int);
78 * Table with supported baudrates (defined in config_xyz.h)
80 static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
81 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
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/%d bytes\n", i, ENV_SIZE);
112 for (i=1; i<argc; ++i) { /* print single env variables */
113 char *name = argv[i];
117 for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
119 for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
121 k = envmatch(name, j);
128 putc(env_get_char(k++));
133 printf ("## Error: \"%s\" not defined\n", name);
140 /************************************************************************
141 * Set a new environment variable,
142 * or replace or delete an existing one.
144 * This function will ONLY work with a in-RAM copy of the environment
147 int _do_setenv (int flag, int argc, char *argv[])
149 DECLARE_GLOBAL_DATA_PTR;
153 uchar *env, *nxt = NULL;
157 uchar *env_data = env_get_addr(0);
159 if (!env_data) /* need copy in RAM */
165 * search if variable with this name already exists
168 for (env=env_data; *env; env=nxt+1) {
169 for (nxt=env; *nxt; ++nxt)
171 if ((oldval = envmatch(name, env-env_data)) >= 0)
176 * Delete any existing definition
179 #ifndef CONFIG_ENV_OVERWRITE
182 * Ethernet Address and serial# can be set only once
184 if ( (strcmp (name, "serial#") == 0) ||
185 ((strcmp (name, "ethaddr") == 0)
186 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
187 && (strcmp (env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
188 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
190 printf ("Can't overwrite \"%s\"\n", name);
195 /* Check for console redirection */
196 if (strcmp(name,"stdin") == 0) {
198 } else if (strcmp(name,"stdout") == 0) {
200 } else if (strcmp(name,"stderr") == 0) {
205 if (argc < 3) { /* Cannot delete it! */
206 printf("Can't delete \"%s\"\n", name);
210 /* Try assigning specified device */
211 if (console_assign (console, argv[2]) < 0)
216 * Switch to new baudrate if new baudrate is supported
218 if (strcmp(argv[1],"baudrate") == 0) {
219 int baudrate = simple_strtoul(argv[2], NULL, 10);
221 for (i=0; i<N_BAUDRATES; ++i) {
222 if (baudrate == baudrate_table[i])
225 if (i == N_BAUDRATES) {
226 printf ("## Baudrate %d bps not supported\n",
230 printf ("## Switch baudrate to %d bps and press ENTER ...\n",
233 gd->baudrate = baudrate;
242 if (*++nxt == '\0') {
243 if (env > env_data) {
251 if ((*env == '\0') && (*nxt == '\0'))
259 #ifdef CONFIG_NET_MULTI
260 if (strncmp(name, "eth", 3) == 0) {
262 int num = simple_strtoul(name+3, &end, 10);
264 if (strcmp(end, "addr") == 0) {
265 eth_set_enetaddr(num, argv[2]);
272 if ((argc < 3) || argv[2] == NULL) {
278 * Append new definition at the end
280 for (env=env_data; *env || *(env+1); ++env)
286 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
288 len = strlen(name) + 2;
289 /* add '=' for first arg, ' ' for all others */
290 for (i=2; i<argc; ++i) {
291 len += strlen(argv[i]) + 1;
293 if (len > (&env_data[ENV_SIZE]-env)) {
294 printf ("## Error: environment overflow, \"%s\" deleted\n", name);
297 while ((*env = *name++) != '\0')
299 for (i=2; i<argc; ++i) {
302 *env = (i==2) ? '=' : ' ';
303 while ((*++env = *val++) != '\0')
307 /* end is marked with double '\0' */
314 * Some variables should be updated when the corresponding
315 * entry in the enviornment is changed
318 if (strcmp(argv[1],"ethaddr") == 0) {
319 char *s = argv[2]; /* always use only one arg */
321 for (i=0; i<6; ++i) {
322 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
323 if (s) s = (*e) ? e+1 : e;
325 #ifdef CONFIG_NET_MULTI
326 eth_set_enetaddr(0, argv[2]);
331 if (strcmp(argv[1],"ipaddr") == 0) {
332 char *s = argv[2]; /* always use only one arg */
336 for (addr=0, i=0; i<4; ++i) {
337 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
339 addr |= (val & 0xFF);
340 if (s) s = (*e) ? e+1 : e;
342 bd->bi_ip_addr = htonl(addr);
345 if (strcmp(argv[1],"loadaddr") == 0) {
346 load_addr = simple_strtoul(argv[2], NULL, 16);
349 #if (CONFIG_COMMANDS & CFG_CMD_NET)
350 if (strcmp(argv[1],"bootfile") == 0) {
351 copy_filename (BootFile, argv[2], sizeof(BootFile));
354 #endif /* CFG_CMD_NET */
358 void setenv (char *varname, char *varvalue)
360 char *argv[4] = { "setenv", varname, varvalue, NULL };
361 _do_setenv (0, 3, argv);
364 int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
367 printf ("Usage:\n%s\n", cmdtp->usage);
371 return _do_setenv (flag, argc, argv);
374 /************************************************************************
375 * Prompt for environment variable
378 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
379 int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
381 extern char console_buffer[CFG_CBSIZE];
382 char message[CFG_CBSIZE];
383 int size = CFG_CBSIZE - 1;
387 local_args[0] = argv[0];
388 local_args[1] = argv[1];
389 local_args[2] = NULL;
390 local_args[3] = NULL;
393 printf ("Usage:\n%s\n", cmdtp->usage);
396 /* Check the syntax */
399 printf ("Usage:\n%s\n", cmdtp->usage);
402 case 2: /* askenv envname */
403 sprintf (message, "Please enter '%s':", argv[1]);
406 case 3: /* askenv envname size */
407 sprintf (message, "Please enter '%s':", argv[1]);
408 size = simple_strtoul (argv[2], NULL, 10);
411 default: /* askenv envname message1 ... messagen size */
416 for (i = 2; i < argc - 1; i++) {
418 message[pos++] = ' ';
420 strcpy (message+pos, argv[i]);
421 pos += strlen(argv[i]);
424 size = simple_strtoul (argv[argc - 1], NULL, 10);
429 if (size >= CFG_CBSIZE)
430 size = CFG_CBSIZE - 1;
435 /* prompt for input */
436 len = readline (message);
439 console_buffer[size] = '\0';
442 if (console_buffer[0] != '\0') {
443 local_args[2] = console_buffer;
447 /* Continue calling setenv code */
448 return _do_setenv (flag, len, local_args);
450 #endif /* CFG_CMD_ASKENV */
452 /************************************************************************
453 * Look up variable from environment,
454 * return address of storage for that variable,
455 * or NULL if not found
458 char *getenv (uchar *name)
462 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
465 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
466 if (nxt >= CFG_ENV_SIZE) {
470 if ((val=envmatch(name, i)) < 0)
472 return (env_get_addr(val));
478 int getenv_r (uchar *name, uchar *buf, unsigned len)
482 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
485 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
486 if (nxt >= CFG_ENV_SIZE) {
490 if ((val=envmatch(name, i)) < 0)
492 /* found; copy out */
494 while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
503 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
504 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
505 (CFG_CMD_ENV|CFG_CMD_FLASH))
506 int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
508 extern char * env_name_spec;
510 printf ("Saving Environment to %s...\n", env_name_spec);
512 return (saveenv() ? 1 : 0);
517 /************************************************************************
518 * Match a name / name=value pair
520 * s1 is either a simple 'name', or a 'name=value' pair.
521 * i2 is the environment index for a 'name2=value2' pair.
522 * If the names match, return the index for the value2, else NULL.
526 envmatch (uchar *s1, int i2)
529 while (*s1 == env_get_char(i2++))
532 if (*s1 == '\0' && env_get_char(i2-1) == '=')