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,
26 /**************************************************************************
28 * Support for persistent environment data
30 * The "environment" is stored as a list of '\0' terminated
31 * "name=value" strings. The end of the list is marked by a double
32 * '\0'. New entries are always added at the end. Deleting an entry
33 * shifts the remaining entries to the front. Replacing an entry is a
34 * combination of deleting the old value and adding the new one.
36 * The environment is preceeded by a 32 bit CRC over the data part.
38 **************************************************************************
42 #include <environment.h>
44 #include <linux/stddef.h>
45 #include <asm/byteorder.h>
46 #if (CONFIG_COMMANDS & CFG_CMD_NET)
50 DECLARE_GLOBAL_DATA_PTR;
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]))
88 /************************************************************************
89 * Command interface: print one or all environment variables
91 int do_printenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
95 if(argc == 1){ /* Print all env variables */
96 for(i = 0; env_get_char(i) != '\0'; i = nxt + 1){
98 for(nxt = i; env_get_char(nxt) != '\0'; ++nxt);
100 for(k = i; k < nxt; ++k){
101 putc(env_get_char(k));
112 #if defined(CFG_ENV_IS_IN_NVRAM) || \
113 defined(CFG_ENV_IS_IN_EEPROM) || \
114 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
115 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
116 printf("\nEnvironment size: %d/%d bytes\n\n", i, ENV_SIZE);
118 printf("\nEnvironment size: %d bytes\n\n", i);
123 for(i = 1; i < argc; ++i){ /* print single env variables */
124 char *name = argv[i];
128 for(j = 0; env_get_char(j) != '\0'; j = nxt + 1){
130 for(nxt = j; env_get_char(nxt) != '\0'; ++nxt);
132 k = envmatch((uchar *)name, j);
142 putc(env_get_char(k++));
149 printf("## Error: \"%s\" not defined\n", name);
156 /************************************************************************
157 * Set a new environment variable,
158 * or replace or delete an existing one.
160 * This function will ONLY work with a in-RAM copy of the environment
162 int _do_setenv(int flag, int argc, char *argv[]){
165 uchar *env, *nxt = NULL;
169 uchar *env_data = env_get_addr(0);
171 if(!env_data){ /* need copy in RAM */
178 * search if variable with this name already exists
182 for(env = env_data; *env; env = nxt + 1){
183 for(nxt = env; *nxt; ++nxt);
185 if((oldval = envmatch((uchar *)name, env - env_data)) >= 0){
191 * Delete any existing definition
194 /* Check for console redirection */
195 if(strcmp(name, "stdin") == 0){
197 } else if(strcmp(name, "stdout") == 0){
199 } else if(strcmp(name, "stderr") == 0){
204 if(argc < 3){ /* Cannot delete it! */
205 printf("## Error: can't delete \"%s\"\n", name);
209 /* Try assigning specified device */
210 if(console_assign(console, argv[2]) < 0){
214 #ifdef CONFIG_SERIAL_MULTI
215 if(serial_assign(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);
228 for(i = 0; i < N_BAUDRATES; ++i){
229 if(baudrate == baudrate_table[i]){
234 if(i == N_BAUDRATES){
235 printf("## Error: baudrate %d bps is not supported,\n", baudrate);
236 printf(" choose one from the list:\n\n");
237 for(i = 0; i < N_BAUDRATES; ++i){
238 printf("- %7d bps%s\n", baudrate_table[i], baudrate_table[i] == gd->baudrate ? " [current]" : "");
244 printf("Switch baudrate to %d bps and press ENTER...\n", baudrate);
247 gd->baudrate = baudrate;
270 if((*env == '\0') && (*nxt == '\0')){
280 #ifdef CONFIG_NET_MULTI
281 if(strncmp(name, "eth", 3) == 0){
283 int num = simple_strtoul(name + 3, &end, 10);
285 if(strcmp(end, "addr") == 0){
286 eth_set_enetaddr(num, argv[2]);
292 if((argc < 3) || argv[2] == NULL){
298 * Append new definition at the end
300 for(env = env_data; *env || *(env + 1); ++env);
307 * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
309 len = strlen(name) + 2;
311 /* add '=' for first arg, ' ' for all others */
312 for(i = 2; i < argc; ++i){
313 len += strlen(argv[i]) + 1;
316 if(len > (&env_data[ENV_SIZE] - env)){
317 printf("## Error: environment overflow, \"%s\" deleted\n", name);
321 while((*env = *name++) != '\0'){
325 for(i = 2; i < argc; ++i){
328 *env = (i == 2) ? '=' : ' ';
329 while((*++env = *val++) != '\0');
332 /* end is marked with double '\0' */
339 * Some variables should be updated when the corresponding
340 * entry in the enviornment is changed
342 if(strcmp(argv[1], "ethaddr") == 0){
343 char *s = argv[2]; /* always use only one arg */
346 for(i = 0; i < 6; ++i){
347 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
349 s = (*e) ? e + 1 : e;
352 #ifdef CONFIG_NET_MULTI
353 eth_set_enetaddr(0, argv[2]);
358 if(strcmp(argv[1], "ipaddr") == 0){
359 char *s = argv[2]; /* always use only one arg */
365 for(addr = 0, i = 0; i < 4; ++i){
366 ulong val = s ? simple_strtoul(s, &e, 10) : 0;
368 addr |= (val & 0xFF);
370 s = (*e) ? e + 1 : e;
374 bd->bi_ip_addr = htonl(addr);
378 if(strcmp(argv[1], "loadaddr") == 0){
379 load_addr = simple_strtoul(argv[2], NULL, 16);
383 #if (CONFIG_COMMANDS & CFG_CMD_NET)
384 if(strcmp(argv[1], "bootfile") == 0){
385 copy_filename(BootFile, argv[2], sizeof(BootFile));
388 #endif /* CFG_CMD_NET */
393 void setenv(char *varname, char *varvalue){
394 char *argv[4] = { "setenv", varname, varvalue, NULL };
395 _do_setenv(0, 3, argv);
398 int do_setenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
400 print_cmd_help(cmdtp);
404 return(_do_setenv(flag, argc, argv));
407 /************************************************************************
408 * Prompt for environment variable
411 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
412 int do_askenv( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
413 extern char console_buffer[CFG_CBSIZE];
414 char message[CFG_CBSIZE];
415 int size = CFG_CBSIZE - 1;
419 local_args[0] = argv[0];
420 local_args[1] = argv[1];
421 local_args[2] = NULL;
422 local_args[3] = NULL;
425 print_cmd_help(cmdtp);
428 /* Check the syntax */
431 print_cmd_help(cmdtp);
434 case 2: /* askenv envname */
435 sprintf(message, "Please enter '%s':", argv[1]);
438 case 3: /* askenv envname size */
439 sprintf(message, "Please enter '%s':", argv[1]);
440 size = simple_strtoul(argv[2], NULL, 10);
443 default: /* askenv envname message1 ... messagen size */
448 for(i = 2; i < argc - 1; i++){
450 message[pos++] = ' ';
452 strcpy(message+pos, argv[i]);
453 pos += strlen(argv[i]);
456 size = simple_strtoul(argv[argc - 1], NULL, 10);
461 if(size >= CFG_CBSIZE){
462 size = CFG_CBSIZE - 1;
469 /* prompt for input */
470 len = readline(message);
473 console_buffer[size] = '\0';
477 if(console_buffer[0] != '\0'){
478 local_args[2] = console_buffer;
482 /* Continue calling setenv code */
483 return(_do_setenv(flag, len, local_args));
485 #endif /* CFG_CMD_ASKENV */
487 /************************************************************************
488 * Look up variable from environment,
489 * return address of storage for that variable,
490 * or NULL if not found
492 char *getenv(char *name){
495 for(i = 0; env_get_char(i) != '\0'; i = nxt + 1){
498 for(nxt = i; env_get_char(nxt) != '\0'; ++nxt){
499 if(nxt >= CFG_ENV_SIZE){
503 if((val = envmatch((uchar *)name, i)) < 0){
506 return((char *)env_get_addr(val));
512 int getenv_r(char *name, char *buf, unsigned len){
515 for(i = 0; env_get_char(i) != '\0'; i = nxt + 1){
518 for(nxt = i; env_get_char(nxt) != '\0'; ++nxt){
519 if(nxt >= CFG_ENV_SIZE){
523 if((val = envmatch((uchar *)name, i)) < 0){
526 /* found; copy out */
528 while((len > n++) && (*buf++ = env_get_char(val++)) != '\0');
540 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
541 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
542 (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
543 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \
544 (CFG_CMD_ENV|CFG_CMD_NAND))
545 int do_saveenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
546 extern char * env_name_spec;
548 printf("Saving environment to %s...\n\n", env_name_spec);
550 return(saveenv() ? 1 : 0);
555 /************************************************************************
556 * Match a name / name=value pair
558 * s1 is either a simple 'name', or a 'name=value' pair.
559 * i2 is the environment index for a 'name2=value2' pair.
560 * If the names match, return the index for the value2, else NULL.
562 static int envmatch(uchar *s1, int i2){
564 while(*s1 == env_get_char(i2++)){
570 if(*s1 == '\0' && env_get_char(i2 - 1) == '='){
577 /**************************************************/
578 U_BOOT_CMD(printenv, CFG_MAXARGS, 1, do_printenv, "print environment variables\n", "[name]\n\t- print values of all environment variables or of one with name 'name'\n");
580 U_BOOT_CMD(setenv, CFG_MAXARGS, 0, do_setenv, "set environment variables\n",
582 "\t- set environment variable 'name' to 'value ...'\n"
584 "\t- delete environment variable 'name'\n");
586 #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
587 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
588 (CFG_CMD_ENV|CFG_CMD_FLASH)) || \
589 ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \
590 (CFG_CMD_ENV|CFG_CMD_NAND))
591 U_BOOT_CMD(saveenv, 1, 0, do_saveenv, "save environment variables to FLASH\n", NULL);
593 #endif /* CFG_CMD_ENV */
595 #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
598 askenv, CFG_MAXARGS, 1, do_askenv,
599 "get environment variables from stdin\n",
600 "name [message] [size]\n"
601 " - get environment variable 'name' from stdin (max 'size' chars)\n"
603 " - get environment variable 'name' from stdin\n"
605 " - get environment variable 'name' from stdin (max 'size' chars)\n"
606 "askenv name [message] size\n"
607 " - display 'message' string and get environment variable 'name'"
608 "from stdin (max 'size' chars)\n"
610 #endif /* CFG_CMD_ASKENV */
612 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
613 int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
614 U_BOOT_CMD(run, CFG_MAXARGS, 1, do_run, "run commands in an environment variable\n", "var [...]\n\t- run the commands in the environment variable(s) 'var'\n");
615 #endif /* CFG_CMD_RUN */