wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | /************************************************************************** |
| 28 | * |
| 29 | * Support for persistent environment data |
| 30 | * |
| 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. |
| 36 | * |
| 37 | * The environment is preceeded by a 32 bit CRC over the data part. |
| 38 | * |
| 39 | ************************************************************************** |
| 40 | */ |
| 41 | |
| 42 | #include <common.h> |
| 43 | #include <command.h> |
| 44 | #include <environment.h> |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 45 | #include <watchdog.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 46 | #include <serial.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 47 | #include <linux/stddef.h> |
| 48 | #include <asm/byteorder.h> |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 49 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 50 | #include <net.h> |
| 51 | #endif |
| 52 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 55 | #if !defined(CFG_ENV_IS_IN_NVRAM) && \ |
| 56 | !defined(CFG_ENV_IS_IN_EEPROM) && \ |
| 57 | !defined(CFG_ENV_IS_IN_FLASH) && \ |
| 58 | !defined(CFG_ENV_IS_IN_DATAFLASH) && \ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 59 | !defined(CFG_ENV_IS_IN_NAND) && \ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 60 | !defined(CFG_ENV_IS_IN_ONENAND) && \ |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 61 | !defined(CFG_ENV_IS_NOWHERE) |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 62 | # error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|ONENAND|NOWHERE} |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | #define XMK_STR(x) #x |
| 66 | #define MK_STR(x) XMK_STR(x) |
| 67 | |
| 68 | /************************************************************************ |
| 69 | ************************************************************************/ |
| 70 | |
| 71 | /* Function that returns a character from the environment */ |
| 72 | extern uchar (*env_get_char)(int); |
| 73 | |
| 74 | /* Function that returns a pointer to a value from the environment */ |
| 75 | /* (Only memory version supported / needed). */ |
| 76 | extern uchar *env_get_addr(int); |
| 77 | |
| 78 | /* Function that updates CRC of the enironment */ |
| 79 | extern void env_crc_update (void); |
| 80 | |
| 81 | /************************************************************************ |
| 82 | ************************************************************************/ |
| 83 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 84 | /* |
| 85 | * Table with supported baudrates (defined in config_xyz.h) |
| 86 | */ |
| 87 | static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE; |
| 88 | #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0])) |
| 89 | |
| 90 | |
| 91 | /************************************************************************ |
| 92 | * Command interface: print one or all environment variables |
| 93 | */ |
| 94 | |
| 95 | int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 96 | { |
| 97 | int i, j, k, nxt; |
| 98 | int rcode = 0; |
| 99 | |
| 100 | if (argc == 1) { /* Print all env variables */ |
| 101 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 102 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) |
| 103 | ; |
| 104 | for (k=i; k<nxt; ++k) |
| 105 | putc(env_get_char(k)); |
| 106 | putc ('\n'); |
| 107 | |
| 108 | if (ctrlc()) { |
| 109 | puts ("\n ** Abort\n"); |
| 110 | return 1; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | for (i=1; i<argc; ++i) { /* print single env variables */ |
| 120 | char *name = argv[i]; |
| 121 | |
| 122 | k = -1; |
| 123 | |
| 124 | for (j=0; env_get_char(j) != '\0'; j=nxt+1) { |
| 125 | |
| 126 | for (nxt=j; env_get_char(nxt) != '\0'; ++nxt) |
| 127 | ; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 128 | k = envmatch((uchar *)name, j); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 129 | if (k < 0) { |
| 130 | continue; |
| 131 | } |
| 132 | puts (name); |
| 133 | putc ('='); |
| 134 | while (k < nxt) |
| 135 | putc(env_get_char(k++)); |
| 136 | putc ('\n'); |
| 137 | break; |
| 138 | } |
| 139 | if (k < 0) { |
| 140 | printf ("## Error: \"%s\" not defined\n", name); |
| 141 | rcode ++; |
| 142 | } |
| 143 | } |
| 144 | return rcode; |
| 145 | } |
| 146 | |
| 147 | /************************************************************************ |
| 148 | * Set a new environment variable, |
| 149 | * or replace or delete an existing one. |
| 150 | * |
| 151 | * This function will ONLY work with a in-RAM copy of the environment |
| 152 | */ |
| 153 | |
| 154 | int _do_setenv (int flag, int argc, char *argv[]) |
| 155 | { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 156 | int i, len, oldval; |
| 157 | int console = -1; |
| 158 | uchar *env, *nxt = NULL; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 159 | char *name; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 160 | bd_t *bd = gd->bd; |
| 161 | |
| 162 | uchar *env_data = env_get_addr(0); |
| 163 | |
| 164 | if (!env_data) /* need copy in RAM */ |
| 165 | return 1; |
| 166 | |
| 167 | name = argv[1]; |
| 168 | |
Wolfgang Denk | 471a7be | 2006-10-28 01:14:32 +0200 | [diff] [blame] | 169 | if (strchr(name, '=')) { |
| 170 | printf ("## Error: illegal character '=' in variable name \"%s\"\n", name); |
| 171 | return 1; |
| 172 | } |
| 173 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 174 | /* |
| 175 | * search if variable with this name already exists |
| 176 | */ |
| 177 | oldval = -1; |
| 178 | for (env=env_data; *env; env=nxt+1) { |
| 179 | for (nxt=env; *nxt; ++nxt) |
| 180 | ; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 181 | if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Delete any existing definition |
| 187 | */ |
| 188 | if (oldval >= 0) { |
| 189 | #ifndef CONFIG_ENV_OVERWRITE |
| 190 | |
| 191 | /* |
stroese | 0587597 | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 192 | * Ethernet Address and serial# can be set only once, |
| 193 | * ver is readonly. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 194 | */ |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 195 | #ifdef CONFIG_HAS_UID |
| 196 | /* Allow serial# forced overwrite with 0xdeaf4add flag */ |
| 197 | if ( ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) || |
| 198 | #else |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 199 | if ( (strcmp (name, "serial#") == 0) || |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 200 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 201 | ((strcmp (name, "ethaddr") == 0) |
| 202 | #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 203 | && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 204 | #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ |
| 205 | ) ) { |
| 206 | printf ("Can't overwrite \"%s\"\n", name); |
| 207 | return 1; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | /* Check for console redirection */ |
| 212 | if (strcmp(name,"stdin") == 0) { |
| 213 | console = stdin; |
| 214 | } else if (strcmp(name,"stdout") == 0) { |
| 215 | console = stdout; |
| 216 | } else if (strcmp(name,"stderr") == 0) { |
| 217 | console = stderr; |
| 218 | } |
| 219 | |
| 220 | if (console != -1) { |
| 221 | if (argc < 3) { /* Cannot delete it! */ |
| 222 | printf("Can't delete \"%s\"\n", name); |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | /* Try assigning specified device */ |
| 227 | if (console_assign (console, argv[2]) < 0) |
| 228 | return 1; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 229 | |
| 230 | #ifdef CONFIG_SERIAL_MULTI |
| 231 | if (serial_assign (argv[2]) < 0) |
| 232 | return 1; |
| 233 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Switch to new baudrate if new baudrate is supported |
| 238 | */ |
| 239 | if (strcmp(argv[1],"baudrate") == 0) { |
| 240 | int baudrate = simple_strtoul(argv[2], NULL, 10); |
| 241 | int i; |
| 242 | for (i=0; i<N_BAUDRATES; ++i) { |
| 243 | if (baudrate == baudrate_table[i]) |
| 244 | break; |
| 245 | } |
| 246 | if (i == N_BAUDRATES) { |
| 247 | printf ("## Baudrate %d bps not supported\n", |
| 248 | baudrate); |
| 249 | return 1; |
| 250 | } |
| 251 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 252 | baudrate); |
| 253 | udelay(50000); |
| 254 | gd->baudrate = baudrate; |
Bartlomiej Sieka | c84bad0 | 2006-12-20 00:29:43 +0100 | [diff] [blame] | 255 | #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2) |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 256 | gd->bd->bi_baudrate = baudrate; |
| 257 | #endif |
| 258 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 259 | serial_setbrg (); |
| 260 | udelay(50000); |
| 261 | for (;;) { |
| 262 | if (getc() == '\r') |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (*++nxt == '\0') { |
| 268 | if (env > env_data) { |
| 269 | env--; |
| 270 | } else { |
| 271 | *env = '\0'; |
| 272 | } |
| 273 | } else { |
| 274 | for (;;) { |
| 275 | *env = *nxt++; |
| 276 | if ((*env == '\0') && (*nxt == '\0')) |
| 277 | break; |
| 278 | ++env; |
| 279 | } |
| 280 | } |
| 281 | *++env = '\0'; |
| 282 | } |
| 283 | |
| 284 | #ifdef CONFIG_NET_MULTI |
| 285 | if (strncmp(name, "eth", 3) == 0) { |
| 286 | char *end; |
| 287 | int num = simple_strtoul(name+3, &end, 10); |
| 288 | |
| 289 | if (strcmp(end, "addr") == 0) { |
| 290 | eth_set_enetaddr(num, argv[2]); |
| 291 | } |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | |
| 296 | /* Delete only ? */ |
| 297 | if ((argc < 3) || argv[2] == NULL) { |
| 298 | env_crc_update (); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Append new definition at the end |
| 304 | */ |
| 305 | for (env=env_data; *env || *(env+1); ++env) |
| 306 | ; |
| 307 | if (env > env_data) |
| 308 | ++env; |
| 309 | /* |
| 310 | * Overflow when: |
| 311 | * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data) |
| 312 | */ |
| 313 | len = strlen(name) + 2; |
| 314 | /* add '=' for first arg, ' ' for all others */ |
| 315 | for (i=2; i<argc; ++i) { |
| 316 | len += strlen(argv[i]) + 1; |
| 317 | } |
| 318 | if (len > (&env_data[ENV_SIZE]-env)) { |
| 319 | printf ("## Error: environment overflow, \"%s\" deleted\n", name); |
| 320 | return 1; |
| 321 | } |
| 322 | while ((*env = *name++) != '\0') |
| 323 | env++; |
| 324 | for (i=2; i<argc; ++i) { |
| 325 | char *val = argv[i]; |
| 326 | |
| 327 | *env = (i==2) ? '=' : ' '; |
| 328 | while ((*++env = *val++) != '\0') |
| 329 | ; |
| 330 | } |
| 331 | |
| 332 | /* end is marked with double '\0' */ |
| 333 | *++env = '\0'; |
| 334 | |
| 335 | /* Update CRC */ |
| 336 | env_crc_update (); |
| 337 | |
| 338 | /* |
| 339 | * Some variables should be updated when the corresponding |
| 340 | * entry in the enviornment is changed |
| 341 | */ |
| 342 | |
| 343 | if (strcmp(argv[1],"ethaddr") == 0) { |
| 344 | char *s = argv[2]; /* always use only one arg */ |
| 345 | char *e; |
| 346 | for (i=0; i<6; ++i) { |
| 347 | bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0; |
| 348 | if (s) s = (*e) ? e+1 : e; |
| 349 | } |
| 350 | #ifdef CONFIG_NET_MULTI |
| 351 | eth_set_enetaddr(0, argv[2]); |
| 352 | #endif |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | if (strcmp(argv[1],"ipaddr") == 0) { |
| 357 | char *s = argv[2]; /* always use only one arg */ |
| 358 | char *e; |
| 359 | unsigned long addr; |
| 360 | bd->bi_ip_addr = 0; |
| 361 | for (addr=0, i=0; i<4; ++i) { |
| 362 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 363 | addr <<= 8; |
| 364 | addr |= (val & 0xFF); |
| 365 | if (s) s = (*e) ? e+1 : e; |
| 366 | } |
| 367 | bd->bi_ip_addr = htonl(addr); |
| 368 | return 0; |
| 369 | } |
| 370 | if (strcmp(argv[1],"loadaddr") == 0) { |
| 371 | load_addr = simple_strtoul(argv[2], NULL, 16); |
| 372 | return 0; |
| 373 | } |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 374 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 375 | if (strcmp(argv[1],"bootfile") == 0) { |
| 376 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 377 | return 0; |
| 378 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 379 | #endif |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 380 | |
stroese | 0587597 | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 381 | #ifdef CONFIG_AMIGAONEG3SE |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 382 | if (strcmp(argv[1], "vga_fg_color") == 0 || |
| 383 | strcmp(argv[1], "vga_bg_color") == 0 ) { |
| 384 | extern void video_set_color(unsigned char attr); |
| 385 | extern unsigned char video_get_attr(void); |
| 386 | |
| 387 | video_set_color(video_get_attr()); |
| 388 | return 0; |
| 389 | } |
| 390 | #endif /* CONFIG_AMIGAONEG3SE */ |
| 391 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | void setenv (char *varname, char *varvalue) |
| 396 | { |
| 397 | char *argv[4] = { "setenv", varname, varvalue, NULL }; |
Jeffrey Mann | 9ffd451 | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 398 | if (varvalue == NULL) |
| 399 | _do_setenv (0, 2, argv); |
| 400 | else |
| 401 | _do_setenv (0, 3, argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 404 | #ifdef CONFIG_HAS_UID |
| 405 | void forceenv (char *varname, char *varvalue) |
| 406 | { |
| 407 | char *argv[4] = { "forceenv", varname, varvalue, NULL }; |
| 408 | _do_setenv (0xdeaf4add, 3, argv); |
| 409 | } |
| 410 | #endif |
| 411 | |
| 412 | int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 413 | { |
| 414 | if (argc < 2) { |
| 415 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | return _do_setenv (flag, argc, argv); |
| 420 | } |
| 421 | |
| 422 | /************************************************************************ |
| 423 | * Prompt for environment variable |
| 424 | */ |
| 425 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 426 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 427 | int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 428 | { |
| 429 | extern char console_buffer[CFG_CBSIZE]; |
| 430 | char message[CFG_CBSIZE]; |
| 431 | int size = CFG_CBSIZE - 1; |
| 432 | int len; |
| 433 | char *local_args[4]; |
| 434 | |
| 435 | local_args[0] = argv[0]; |
| 436 | local_args[1] = argv[1]; |
| 437 | local_args[2] = NULL; |
| 438 | local_args[3] = NULL; |
| 439 | |
| 440 | if (argc < 2) { |
| 441 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 442 | return 1; |
| 443 | } |
| 444 | /* Check the syntax */ |
| 445 | switch (argc) { |
| 446 | case 1: |
| 447 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 448 | return 1; |
| 449 | |
| 450 | case 2: /* askenv envname */ |
| 451 | sprintf (message, "Please enter '%s':", argv[1]); |
| 452 | break; |
| 453 | |
| 454 | case 3: /* askenv envname size */ |
| 455 | sprintf (message, "Please enter '%s':", argv[1]); |
| 456 | size = simple_strtoul (argv[2], NULL, 10); |
| 457 | break; |
| 458 | |
| 459 | default: /* askenv envname message1 ... messagen size */ |
| 460 | { |
| 461 | int i; |
| 462 | int pos = 0; |
| 463 | |
| 464 | for (i = 2; i < argc - 1; i++) { |
| 465 | if (pos) { |
| 466 | message[pos++] = ' '; |
| 467 | } |
| 468 | strcpy (message+pos, argv[i]); |
| 469 | pos += strlen(argv[i]); |
| 470 | } |
| 471 | message[pos] = '\0'; |
| 472 | size = simple_strtoul (argv[argc - 1], NULL, 10); |
| 473 | } |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (size >= CFG_CBSIZE) |
| 478 | size = CFG_CBSIZE - 1; |
| 479 | |
| 480 | if (size <= 0) |
| 481 | return 1; |
| 482 | |
| 483 | /* prompt for input */ |
| 484 | len = readline (message); |
| 485 | |
| 486 | if (size < len) |
| 487 | console_buffer[size] = '\0'; |
| 488 | |
| 489 | len = 2; |
| 490 | if (console_buffer[0] != '\0') { |
| 491 | local_args[2] = console_buffer; |
| 492 | len = 3; |
| 493 | } |
| 494 | |
| 495 | /* Continue calling setenv code */ |
| 496 | return _do_setenv (flag, len, local_args); |
| 497 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 498 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 499 | |
| 500 | /************************************************************************ |
| 501 | * Look up variable from environment, |
| 502 | * return address of storage for that variable, |
| 503 | * or NULL if not found |
| 504 | */ |
| 505 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 506 | char *getenv (char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 507 | { |
| 508 | int i, nxt; |
| 509 | |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 510 | WATCHDOG_RESET(); |
| 511 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 512 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 513 | int val; |
| 514 | |
| 515 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
| 516 | if (nxt >= CFG_ENV_SIZE) { |
| 517 | return (NULL); |
| 518 | } |
| 519 | } |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 520 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 521 | continue; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 522 | return ((char *)env_get_addr(val)); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return (NULL); |
| 526 | } |
| 527 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 528 | int getenv_r (char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 529 | { |
| 530 | int i, nxt; |
| 531 | |
| 532 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 533 | int val, n; |
| 534 | |
| 535 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
| 536 | if (nxt >= CFG_ENV_SIZE) { |
| 537 | return (-1); |
| 538 | } |
| 539 | } |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 540 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 541 | continue; |
| 542 | /* found; copy out */ |
| 543 | n = 0; |
| 544 | while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0') |
| 545 | ; |
| 546 | if (len == n) |
| 547 | *buf = '\0'; |
| 548 | return (n); |
| 549 | } |
| 550 | return (-1); |
| 551 | } |
| 552 | |
Jean-Christophe PLAGNIOL-VILLARD | 00b48a4 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 553 | #if ((defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) \ |
Jon Loeliger | 65c450b | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 554 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 555 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \ |
Jean-Christophe PLAGNIOL-VILLARD | 00b48a4 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 556 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND))) \ |
| 557 | && !defined(CFG_ENV_IS_NOWHERE)) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 558 | int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 559 | { |
| 560 | extern char * env_name_spec; |
| 561 | |
| 562 | printf ("Saving Environment to %s...\n", env_name_spec); |
| 563 | |
| 564 | return (saveenv() ? 1 : 0); |
| 565 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 566 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 567 | #endif |
| 568 | |
| 569 | |
| 570 | /************************************************************************ |
| 571 | * Match a name / name=value pair |
| 572 | * |
| 573 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 574 | * i2 is the environment index for a 'name2=value2' pair. |
| 575 | * If the names match, return the index for the value2, else NULL. |
| 576 | */ |
| 577 | |
Rafal Jaworowski | 26a4179 | 2008-01-09 18:05:27 +0100 | [diff] [blame] | 578 | int envmatch (uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 579 | { |
| 580 | |
| 581 | while (*s1 == env_get_char(i2++)) |
| 582 | if (*s1++ == '=') |
| 583 | return(i2); |
| 584 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
| 585 | return(i2); |
| 586 | return(-1); |
| 587 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 588 | |
| 589 | |
| 590 | /**************************************************/ |
| 591 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 592 | U_BOOT_CMD( |
| 593 | printenv, CFG_MAXARGS, 1, do_printenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 594 | "printenv- print environment variables\n", |
| 595 | "\n - print values of all environment variables\n" |
| 596 | "printenv name ...\n" |
| 597 | " - print value of environment variable 'name'\n" |
| 598 | ); |
| 599 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 600 | U_BOOT_CMD( |
| 601 | setenv, CFG_MAXARGS, 0, do_setenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 602 | "setenv - set environment variables\n", |
| 603 | "name value ...\n" |
| 604 | " - set environment variable 'name' to 'value ...'\n" |
| 605 | "setenv name\n" |
| 606 | " - delete environment variable 'name'\n" |
| 607 | ); |
| 608 | |
Jean-Christophe PLAGNIOL-VILLARD | 00b48a4 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 609 | #if ((defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) \ |
Jon Loeliger | 65c450b | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 610 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 611 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \ |
Jean-Christophe PLAGNIOL-VILLARD | 00b48a4 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 612 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND))) \ |
| 613 | && !defined(CFG_ENV_IS_NOWHERE)) |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 614 | U_BOOT_CMD( |
| 615 | saveenv, 1, 0, do_saveenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 616 | "saveenv - save environment variables to persistent storage\n", |
| 617 | NULL |
| 618 | ); |
| 619 | |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 620 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 621 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 622 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 623 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 624 | U_BOOT_CMD( |
| 625 | askenv, CFG_MAXARGS, 1, do_askenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 626 | "askenv - get environment variables from stdin\n", |
| 627 | "name [message] [size]\n" |
| 628 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 629 | "askenv name\n" |
| 630 | " - get environment variable 'name' from stdin\n" |
| 631 | "askenv name size\n" |
| 632 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 633 | "askenv name [message] size\n" |
| 634 | " - display 'message' string and get environment variable 'name'" |
| 635 | "from stdin (max 'size' chars)\n" |
| 636 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 637 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 638 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 639 | #if defined(CONFIG_CMD_RUN) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 640 | int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 641 | U_BOOT_CMD( |
| 642 | run, CFG_MAXARGS, 1, do_run, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 643 | "run - run commands in an environment variable\n", |
| 644 | "var [...]\n" |
| 645 | " - run the commands in the environment variable(s) 'var'\n" |
| 646 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 647 | #endif |