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