/* * (C) Copyright 2000-2002 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH * Andreas Heppel * See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */ /************************************************************************** * * Support for persistent environment data * * The "environment" is stored as a list of '\0' terminated * "name=value" strings. The end of the list is marked by a double * '\0'. New entries are always added at the end. Deleting an entry * shifts the remaining entries to the front. Replacing an entry is a * combination of deleting the old value and adding the new one. * * The environment is preceeded by a 32 bit CRC over the data part. * ************************************************************************** */ #include #include #include #include #include #include #include #include #if (CONFIG_COMMANDS & CFG_CMD_NET) #include #endif DECLARE_GLOBAL_DATA_PTR; #if !defined(CFG_ENV_IS_IN_NVRAM) && \ !defined(CFG_ENV_IS_IN_EEPROM) && \ !defined(CFG_ENV_IS_IN_FLASH) && \ !defined(CFG_ENV_IS_IN_DATAFLASH) && \ !defined(CFG_ENV_IS_IN_NAND) && \ !defined(CFG_ENV_IS_NOWHERE) # error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|NOWHERE|NAND} #endif #define XMK_STR(x) #x #define MK_STR(x) XMK_STR(x) /************************************************************************ ************************************************************************/ /* Function that returns a character from the environment */ extern uchar (*env_get_char)(int); /* Function that returns a pointer to a value from the environment */ /* (Only memory version supported / needed). */ extern uchar *env_get_addr(int); /* Function that updates CRC of the enironment */ extern void env_crc_update (void); /************************************************************************ ************************************************************************/ static int envmatch (uchar *, int); /* * Table with supported baudrates (defined in config_xyz.h) */ static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE; #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0])) #if 0 /************************************************************************ * Command interface: print one or all environment variables */ int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { int i, j, k, nxt; int rcode = 0; if (argc == 1) { /* Print all env variables */ for (i=0; env_get_char(i) != '\0'; i=nxt+1) { for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) ; for (k=i; k /* * Command interface: print one or all environment variables * * Returns 0 in case of error, or length of printed string */ static int env_print(char *name) { char *res = NULL; size_t len; if (name) { /* print a single name */ ENTRY e, *ep; e.key = name; e.data = NULL; hsearch_r(e, FIND, &ep, &env_htab); if (ep == NULL) return 0; if(!strncmp(name,"otp.sec.",8)){ uchar *buf=NULL; len = strlen(ep->data); buf = malloc(len); if(buf){ memset(buf,0x00, len); otp_sec_decode(ep->data, buf); len = printf("%s=%s\n",ep->key, buf); free(buf); return len; }else{ printf("Malloc mem failed!\n"); return 0; } } len = printf("%s=%s\n", ep->key, ep->data); return len; } /* print whole list */ len = hexport_r(&env_htab, '\n', &res, 0, 0, NULL); if (len > 0) { puts(res); free(res); return len; } /* should never happen */ return 0; } int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { int i; int rcode = 0; if (argc == 1) { /* print all env vars */ rcode = env_print(NULL); if (!rcode) return 1; printf("\nEnvironment size: %d/%ld bytes\n", rcode, (ulong)ENV_SIZE); return 0; } /* print selected env vars */ for (i = 1; i < argc; ++i) { int rc = env_print(argv[i]); if (!rc) { printf("## Error: \"%s\" not defined\n", argv[i]); ++rcode; } } return rcode; } /*============*/ /*======rockpzhang======*/ /************************************************************************ * Set a new environment variable, * or replace or delete an existing one. * * This function will ONLY work with a in-RAM copy of the environment */ #if 0 int _do_setenv (int flag, int argc, char *argv[]) { int i, len, oldval; int console = -1; uchar *env, *nxt = NULL; char *name; bd_t *bd = gd->bd; uchar *env_data = env_get_addr(0); if (!env_data) /* need copy in RAM */ return 1; name = argv[1]; /* * search if variable with this name already exists */ oldval = -1; for (env=env_data; *env; env=nxt+1) { for (nxt=env; *nxt; ++nxt) ; if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0) break; } /* * Delete any existing definition */ if (oldval >= 0) { #ifndef CONFIG_ENV_OVERWRITE /* * Ethernet Address and serial# can be set only once, * ver is readonly. */ if ( (strcmp (name, "serial#") == 0) || ((strcmp (name, "ethaddr") == 0) #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0) #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ ) ) { printf ("Can't overwrite \"%s\"\n", name); return 1; } #endif /* Check for console redirection */ if (strcmp(name,"stdin") == 0) { console = stdin; } else if (strcmp(name,"stdout") == 0) { console = stdout; } else if (strcmp(name,"stderr") == 0) { console = stderr; } if (console != -1) { if (argc < 3) { /* Cannot delete it! */ printf("Can't delete \"%s\"\n", name); return 1; } /* Try assigning specified device */ if (console_assign (console, argv[2]) < 0) return 1; #ifdef CONFIG_SERIAL_MULTI if (serial_assign (argv[2]) < 0) return 1; #endif } /* * Switch to new baudrate if new baudrate is supported */ if (strcmp(argv[1],"baudrate") == 0) { int baudrate = simple_strtoul(argv[2], NULL, 10); int i; for (i=0; ibaudrate = baudrate; #ifdef CONFIG_PPC gd->bd->bi_baudrate = baudrate; #endif serial_setbrg (); udelay(50000); for (;;) { if (getc() == '\r') break; } } if (*++nxt == '\0') { if (env > env_data) { env--; } else { *env = '\0'; } } else { for (;;) { *env = *nxt++; if ((*env == '\0') && (*nxt == '\0')) break; ++env; } } *++env = '\0'; } #ifdef CONFIG_NET_MULTI if (strncmp(name, "eth", 3) == 0) { char *end; int num = simple_strtoul(name+3, &end, 10); if (strcmp(end, "addr") == 0) { eth_set_enetaddr(num, argv[2]); } } #endif /* Delete only ? */ if ((argc < 3) || argv[2] == NULL) { env_crc_update (); return 0; } /* * Append new definition at the end */ for (env=env_data; *env || *(env+1); ++env) ; if (env > env_data) ++env; /* * Overflow when: * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data) */ len = strlen(name) + 2; /* add '=' for first arg, ' ' for all others */ for (i=2; i (&env_data[ENV_SIZE]-env)) { printf ("## Error: environment overflow, \"%s\" deleted\n", name); return 1; } while ((*env = *name++) != '\0') env++; for (i=2; ibi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0; if (s) s = (*e) ? e+1 : e; } #ifdef CONFIG_NET_MULTI eth_set_enetaddr(0, argv[2]); #endif return 0; } if (strcmp(argv[1],"ipaddr") == 0) { char *s = argv[2]; /* always use only one arg */ char *e; unsigned long addr; bd->bi_ip_addr = 0; for (addr=0, i=0; i<4; ++i) { ulong val = s ? simple_strtoul(s, &e, 10) : 0; addr <<= 8; addr |= (val & 0xFF); if (s) s = (*e) ? e+1 : e; } bd->bi_ip_addr = htonl(addr); return 0; } if (strcmp(argv[1],"loadaddr") == 0) { load_addr = simple_strtoul(argv[2], NULL, 16); return 0; } #if (CONFIG_COMMANDS & CFG_CMD_NET) if (strcmp(argv[1],"bootfile") == 0) { copy_filename (BootFile, argv[2], sizeof(BootFile)); return 0; } #endif /* CFG_CMD_NET */ #ifdef CONFIG_AMIGAONEG3SE if (strcmp(argv[1], "vga_fg_color") == 0 || strcmp(argv[1], "vga_bg_color") == 0 ) { extern void video_set_color(unsigned char attr); extern unsigned char video_get_attr(void); video_set_color(video_get_attr()); return 0; } #endif /* CONFIG_AMIGAONEG3SE */ return 0; } void setenv (char *varname, char *varvalue) { char *argv[4] = { "setenv", varname, varvalue, NULL }; _do_setenv (0, 3, argv); } #endif #if 0 int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { if (argc < 2) { printf ("Usage:\n%s\n", cmdtp->usage); return 1; } return _do_setenv (flag, argc, argv); } #endif #include /* * This variable is incremented on each do_env_set(), so it can * be used via get_env_id() as an indication, if the environment * has changed or not. So it is possible to reread an environment * variable only if the environment was changed ... done so for * example in NetInitLoop() */ static int env_id = 1; /* * Set a new environment variable, * or replace or delete an existing one. */ int _do_env_set(int flag, int argc, char * const argv[]) { bd_t *bd = gd->bd; int i, len; int console = -1; char *name, *value, *s; ENTRY e, *ep; int ret; size_t xlen; uchar *base64val=NULL; name = argv[1]; if (strchr(name, '=')) { printf("## Error: illegal character '=' in variable name" "\"%s\"\n", name); return 1; } env_id++; /* * search if variable with this name already exists */ e.key = name; e.data = NULL; hsearch_r(e, FIND, &ep, &env_htab); /* Check for console redirection */ if (strcmp(name, "stdin") == 0) console = stdin; else if (strcmp(name, "stdout") == 0) console = stdout; else if (strcmp(name, "stderr") == 0) console = stderr; if (console != -1) { if (argc < 3) { /* Cannot delete it! */ printf("Can't delete \"%s\"\n", name); return 1; } #ifdef CONFIG_CONSOLE_MUX i = iomux_doenv(console, argv[2]); if (i) return i; #else /* Try assigning specified device */ if (console_assign(console, argv[2]) < 0) return 1; #ifdef CONFIG_SERIAL_MULTI if (serial_assign(argv[2]) < 0) return 1; #endif #endif /* CONFIG_CONSOLE_MUX */ } /* * Some variables like "ethaddr" and "serial#" can be set only * once and cannot be deleted; also, "ver" is readonly. */ if (ep) { /* variable exists */ #ifndef CONFIG_ENV_OVERWRITE if (strcmp(name, "serial#") == 0 || (strcmp(name, "ethaddr") == 0 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) && strcmp(ep->data, MK_STR(CONFIG_ETHADDR)) != 0 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ )) { printf("Can't overwrite \"%s\"\n", name); return 1; } #endif if(strncmp(name, "otp.", 4)==0){ printf("OTP can't overwrite \"%s\"\n", name); return 1; } /* * Switch to new baudrate if new baudrate is supported */ if (strcmp(name, "baudrate") == 0) { int baudrate = simple_strtoul(argv[2], NULL, 10); int i; for (i = 0; i < N_BAUDRATES; ++i) { if (baudrate == baudrate_table[i]) break; } if (i == N_BAUDRATES) { printf("## Baudrate %d bps not supported\n", baudrate); return 1; } printf("## Switch baudrate to %d bps and" "press ENTER ...\n", baudrate); udelay(50000); gd->baudrate = baudrate; #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2) gd->bd->bi_baudrate = baudrate; #endif serial_setbrg(); udelay(50000); while (getc() != '\r') ; } } /* Delete only ? */ if (argc < 3 || argv[2] == NULL) { int rc = hdelete_r(name, &env_htab); return !rc; } /* * Insert / replace new value */ for (i = 2, len = 0; i < argc; ++i) len += strlen(argv[i]) + 1; value = malloc(len); if (value == NULL) { printf("## Can't malloc %d bytes\n", len); return 1; } for (i = 2, s = value; i < argc; ++i) { char *v = argv[i]; while ((*s++ = *v++) != '\0') ; *(s - 1) = ' '; } if (s != value) *--s = '\0'; e.key = name; e.data = value; if(strncmp(name,"otp.sec.",8)==0){ xlen = ((len + 15) / 16) * 16; xlen = (xlen << 3) / 6 +4; base64val = malloc(xlen); if(!base64val){ free(value); printf("## Can't malloc %d bytes\n", xlen); return 1; } memset(base64val, 0x00, xlen); ret = otp_sec_encode(value, len, base64val, &xlen); if(ret){ printf("## Warning otp secure encode failed ##\n"); free(value); free(base64val); return 1; }else{ e.data = base64val; } } hsearch_r(e, ENTER, &ep, &env_htab); free(value); if(base64val) free(base64val); if (!ep) { printf("## Error inserting \"%s\" variable, errno=%d\n", name, errno); return 1; } /* * Some variables should be updated when the corresponding * entry in the environment is changed */ if (strcmp(name, "ipaddr") == 0) { char *s = argv[2]; /* always use only one arg */ char *e; unsigned long addr; bd->bi_ip_addr = 0; for (addr = 0, i = 0; i < 4; ++i) { ulong val = s ? simple_strtoul(s, &e, 10) : 0; addr <<= 8; addr |= val & 0xFF; if (s) s = *e ? e + 1 : e; } bd->bi_ip_addr = htonl(addr); return 0; } else if (strcmp(argv[1], "loadaddr") == 0) { load_addr = simple_strtoul(argv[2], NULL, 16); return 0; } #if (CONFIG_COMMANDS & CFG_CMD_NET) else if (strcmp(argv[1], "bootfile") == 0) { copy_filename(BootFile, argv[2], sizeof(BootFile)); return 0; } #endif return 0; } int setenv(char *varname, char *varvalue) { const char * const argv[4] = { "setenv", varname, varvalue, NULL }; if (varvalue == NULL || varvalue[0] == '\0') return _do_env_set(0, 2, (char * const *)argv); else return _do_env_set(0, 3, (char * const *)argv); } int do_setenv(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { if (argc < 2) return 1; return _do_env_set(flag, argc, argv); } /*============*/ /************************************************************************ * Prompt for environment variable */ #if (CONFIG_COMMANDS & CFG_CMD_ASKENV) int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { extern char console_buffer[CFG_CBSIZE]; char message[CFG_CBSIZE]; int size = CFG_CBSIZE - 1; int len; char *local_args[4]; local_args[0] = argv[0]; local_args[1] = argv[1]; local_args[2] = NULL; local_args[3] = NULL; if (argc < 2) { printf ("Usage:\n%s\n", cmdtp->usage); return 1; } /* Check the syntax */ switch (argc) { case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1; case 2: /* askenv envname */ sprintf (message, "Please enter '%s':", argv[1]); break; case 3: /* askenv envname size */ sprintf (message, "Please enter '%s':", argv[1]); size = simple_strtoul (argv[2], NULL, 10); break; default: /* askenv envname message1 ... messagen size */ { int i; int pos = 0; for (i = 2; i < argc - 1; i++) { if (pos) { message[pos++] = ' '; } strcpy (message+pos, argv[i]); pos += strlen(argv[i]); } message[pos] = '\0'; size = simple_strtoul (argv[argc - 1], NULL, 10); } break; } if (size >= CFG_CBSIZE) size = CFG_CBSIZE - 1; if (size <= 0) return 1; /* prompt for input */ len = readline (message); if (size < len) console_buffer[size] = '\0'; len = 2; if (console_buffer[0] != '\0') { local_args[2] = console_buffer; len = 3; } /* Continue calling setenv code */ return _do_setenv (flag, len, local_args); } #endif /* CFG_CMD_ASKENV */ /************************************************************************ * Look up variable from environment, * return address of storage for that variable, * or NULL if not found */ #if 0 char *getenv (char *name) { int i, nxt; WATCHDOG_RESET(); for (i=0; env_get_char(i) != '\0'; i=nxt+1) { int val; for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { if (nxt >= CFG_ENV_SIZE) { return (NULL); } } if ((val=envmatch((uchar *)name, i)) < 0) continue; return ((char *)env_get_addr(val)); } return (NULL); } #endif /* * Look up variable from environment for restricted C runtime env. */ int getenv_f(const char *name, char *buf, unsigned len) { int i, nxt; for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { int val, n; for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) { if (nxt >= CFG_ENV_SIZE) return -1; } val = envmatch((uchar *)name, i); if (val < 0) continue; /* found; copy out */ for (n = 0; n < len; ++n, ++buf) { *buf = env_get_char(val++); if (*buf == '\0') return n; } if (n) *--buf = '\0'; printf("env_buf [%d bytes] too small for value of \"%s\"\n", len, name); return n; } return -1; } char *getenv(char *name) { if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ ENTRY e, *ep; WATCHDOG_RESET(); e.key = name; e.data = NULL; hsearch_r(e, FIND, &ep, &env_htab); return ep ? ep->data : NULL; } /* restricted capabilities before import */ if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) return (char *)(gd->env_buf); return NULL; } int getenv_r (char *name, char *buf, unsigned len) { int i, nxt; for (i=0; env_get_char(i) != '\0'; i=nxt+1) { int val, n; for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { if (nxt >= CFG_ENV_SIZE) { return (-1); } } if ((val=envmatch((uchar *)name, i)) < 0) continue; /* found; copy out */ n = 0; while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0') ; if (len == n) *buf = '\0'; return (n); } return (-1); } #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \ ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \ (CFG_CMD_ENV|CFG_CMD_FLASH)) || \ ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_NAND)) == \ (CFG_CMD_ENV|CFG_CMD_NAND)) int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { extern char * env_name_spec; printf ("Saving Environment to %s...\n", env_name_spec); return (saveenv() ? 1 : 0); } #endif /************************************************************************ * Match a name / name=value pair * * s1 is either a simple 'name', or a 'name=value' pair. * i2 is the environment index for a 'name2=value2' pair. * If the names match, return the index for the value2, else NULL. */ static int envmatch (uchar *s1, int i2) { while (*s1 == env_get_char(i2++)) if (*s1++ == '=') return(i2); if (*s1 == '\0' && env_get_char(i2-1) == '=') return(i2); return(-1); } /**************************************************/ U_BOOT_CMD( printenv, CFG_MAXARGS, 1, do_printenv, "printenv- print environment variables\n", "\n - print values of all environment variables\n" "printenv name ...\n" " - print value of environment variable 'name'\n" ); U_BOOT_CMD( setenv, CFG_MAXARGS, 0, do_setenv, "setenv - set environment variables\n", "name value ...\n" " - set environment variable 'name' to 'value ...'\n" "setenv name\n" " - delete environment variable 'name'\n" ); #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \ ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \ (CFG_CMD_ENV|CFG_CMD_FLASH)) U_BOOT_CMD( saveenv, 1, 0, do_saveenv, "saveenv - save environment variables to persistent storage\n", NULL ); #endif /* CFG_CMD_ENV */ #if (CONFIG_COMMANDS & CFG_CMD_ASKENV) U_BOOT_CMD( askenv, CFG_MAXARGS, 1, do_askenv, "askenv - get environment variables from stdin\n", "name [message] [size]\n" " - get environment variable 'name' from stdin (max 'size' chars)\n" "askenv name\n" " - get environment variable 'name' from stdin\n" "askenv name size\n" " - get environment variable 'name' from stdin (max 'size' chars)\n" "askenv name [message] size\n" " - display 'message' string and get environment variable 'name'" "from stdin (max 'size' chars)\n" ); #endif /* CFG_CMD_ASKENV */ #if (CONFIG_COMMANDS & CFG_CMD_RUN) int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); U_BOOT_CMD( run, CFG_MAXARGS, 1, do_run, "run - run commands in an environment variable\n", "var [...]\n" " - run the commands in the environment variable(s) 'var'\n" ); #endif /* CFG_CMD_RUN */