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> |
| 45 | #include <cmd_nvedit.h> |
| 46 | #include <linux/stddef.h> |
| 47 | #include <asm/byteorder.h> |
| 48 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 49 | #include <net.h> |
| 50 | #endif |
| 51 | |
| 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 |
| 54 | #endif |
| 55 | |
| 56 | #define XMK_STR(x) #x |
| 57 | #define MK_STR(x) XMK_STR(x) |
| 58 | |
| 59 | /************************************************************************ |
| 60 | ************************************************************************/ |
| 61 | |
| 62 | /* Function that returns a character from the environment */ |
| 63 | extern uchar (*env_get_char)(int); |
| 64 | |
| 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); |
| 68 | |
| 69 | /* Function that updates CRC of the enironment */ |
| 70 | extern void env_crc_update (void); |
| 71 | |
| 72 | /************************************************************************ |
| 73 | ************************************************************************/ |
| 74 | |
| 75 | static int envmatch (uchar *, int); |
| 76 | |
| 77 | /* |
| 78 | * Table with supported baudrates (defined in config_xyz.h) |
| 79 | */ |
| 80 | static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE; |
| 81 | #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0])) |
| 82 | |
| 83 | |
| 84 | /************************************************************************ |
| 85 | * Command interface: print one or all environment variables |
| 86 | */ |
| 87 | |
| 88 | int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 89 | { |
| 90 | int i, j, k, nxt; |
| 91 | int rcode = 0; |
| 92 | |
| 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) |
| 96 | ; |
| 97 | for (k=i; k<nxt; ++k) |
| 98 | putc(env_get_char(k)); |
| 99 | putc ('\n'); |
| 100 | |
| 101 | if (ctrlc()) { |
| 102 | puts ("\n ** Abort\n"); |
| 103 | return 1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | for (i=1; i<argc; ++i) { /* print single env variables */ |
| 113 | char *name = argv[i]; |
| 114 | |
| 115 | k = -1; |
| 116 | |
| 117 | for (j=0; env_get_char(j) != '\0'; j=nxt+1) { |
| 118 | |
| 119 | for (nxt=j; env_get_char(nxt) != '\0'; ++nxt) |
| 120 | ; |
| 121 | k = envmatch(name, j); |
| 122 | if (k < 0) { |
| 123 | continue; |
| 124 | } |
| 125 | puts (name); |
| 126 | putc ('='); |
| 127 | while (k < nxt) |
| 128 | putc(env_get_char(k++)); |
| 129 | putc ('\n'); |
| 130 | break; |
| 131 | } |
| 132 | if (k < 0) { |
| 133 | printf ("## Error: \"%s\" not defined\n", name); |
| 134 | rcode ++; |
| 135 | } |
| 136 | } |
| 137 | return rcode; |
| 138 | } |
| 139 | |
| 140 | /************************************************************************ |
| 141 | * Set a new environment variable, |
| 142 | * or replace or delete an existing one. |
| 143 | * |
| 144 | * This function will ONLY work with a in-RAM copy of the environment |
| 145 | */ |
| 146 | |
| 147 | int _do_setenv (int flag, int argc, char *argv[]) |
| 148 | { |
| 149 | DECLARE_GLOBAL_DATA_PTR; |
| 150 | |
| 151 | int i, len, oldval; |
| 152 | int console = -1; |
| 153 | uchar *env, *nxt = NULL; |
| 154 | uchar *name; |
| 155 | bd_t *bd = gd->bd; |
| 156 | |
| 157 | uchar *env_data = env_get_addr(0); |
| 158 | |
| 159 | if (!env_data) /* need copy in RAM */ |
| 160 | return 1; |
| 161 | |
| 162 | name = argv[1]; |
| 163 | |
| 164 | /* |
| 165 | * search if variable with this name already exists |
| 166 | */ |
| 167 | oldval = -1; |
| 168 | for (env=env_data; *env; env=nxt+1) { |
| 169 | for (nxt=env; *nxt; ++nxt) |
| 170 | ; |
| 171 | if ((oldval = envmatch(name, env-env_data)) >= 0) |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Delete any existing definition |
| 177 | */ |
| 178 | if (oldval >= 0) { |
| 179 | #ifndef CONFIG_ENV_OVERWRITE |
| 180 | |
| 181 | /* |
| 182 | * Ethernet Address and serial# can be set only once |
| 183 | */ |
| 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 */ |
| 189 | ) ) { |
| 190 | printf ("Can't overwrite \"%s\"\n", name); |
| 191 | return 1; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | /* Check for console redirection */ |
| 196 | if (strcmp(name,"stdin") == 0) { |
| 197 | console = stdin; |
| 198 | } else if (strcmp(name,"stdout") == 0) { |
| 199 | console = stdout; |
| 200 | } else if (strcmp(name,"stderr") == 0) { |
| 201 | console = stderr; |
| 202 | } |
| 203 | |
| 204 | if (console != -1) { |
| 205 | if (argc < 3) { /* Cannot delete it! */ |
| 206 | printf("Can't delete \"%s\"\n", name); |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | /* Try assigning specified device */ |
| 211 | if (console_assign (console, argv[2]) < 0) |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Switch to new baudrate if new baudrate is supported |
| 217 | */ |
| 218 | if (strcmp(argv[1],"baudrate") == 0) { |
| 219 | int baudrate = simple_strtoul(argv[2], NULL, 10); |
| 220 | int i; |
| 221 | for (i=0; i<N_BAUDRATES; ++i) { |
| 222 | if (baudrate == baudrate_table[i]) |
| 223 | break; |
| 224 | } |
| 225 | if (i == N_BAUDRATES) { |
| 226 | printf ("## Baudrate %d bps not supported\n", |
| 227 | baudrate); |
| 228 | return 1; |
| 229 | } |
| 230 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 231 | baudrate); |
| 232 | udelay(50000); |
| 233 | gd->baudrate = baudrate; |
| 234 | serial_setbrg (); |
| 235 | udelay(50000); |
| 236 | for (;;) { |
| 237 | if (getc() == '\r') |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (*++nxt == '\0') { |
| 243 | if (env > env_data) { |
| 244 | env--; |
| 245 | } else { |
| 246 | *env = '\0'; |
| 247 | } |
| 248 | } else { |
| 249 | for (;;) { |
| 250 | *env = *nxt++; |
| 251 | if ((*env == '\0') && (*nxt == '\0')) |
| 252 | break; |
| 253 | ++env; |
| 254 | } |
| 255 | } |
| 256 | *++env = '\0'; |
| 257 | } |
| 258 | |
| 259 | #ifdef CONFIG_NET_MULTI |
| 260 | if (strncmp(name, "eth", 3) == 0) { |
| 261 | char *end; |
| 262 | int num = simple_strtoul(name+3, &end, 10); |
| 263 | |
| 264 | if (strcmp(end, "addr") == 0) { |
| 265 | eth_set_enetaddr(num, argv[2]); |
| 266 | } |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | |
| 271 | /* Delete only ? */ |
| 272 | if ((argc < 3) || argv[2] == NULL) { |
| 273 | env_crc_update (); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Append new definition at the end |
| 279 | */ |
| 280 | for (env=env_data; *env || *(env+1); ++env) |
| 281 | ; |
| 282 | if (env > env_data) |
| 283 | ++env; |
| 284 | /* |
| 285 | * Overflow when: |
| 286 | * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data) |
| 287 | */ |
| 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; |
| 292 | } |
| 293 | if (len > (&env_data[ENV_SIZE]-env)) { |
| 294 | printf ("## Error: environment overflow, \"%s\" deleted\n", name); |
| 295 | return 1; |
| 296 | } |
| 297 | while ((*env = *name++) != '\0') |
| 298 | env++; |
| 299 | for (i=2; i<argc; ++i) { |
| 300 | char *val = argv[i]; |
| 301 | |
| 302 | *env = (i==2) ? '=' : ' '; |
| 303 | while ((*++env = *val++) != '\0') |
| 304 | ; |
| 305 | } |
| 306 | |
| 307 | /* end is marked with double '\0' */ |
| 308 | *++env = '\0'; |
| 309 | |
| 310 | /* Update CRC */ |
| 311 | env_crc_update (); |
| 312 | |
| 313 | /* |
| 314 | * Some variables should be updated when the corresponding |
| 315 | * entry in the enviornment is changed |
| 316 | */ |
| 317 | |
| 318 | if (strcmp(argv[1],"ethaddr") == 0) { |
| 319 | char *s = argv[2]; /* always use only one arg */ |
| 320 | char *e; |
| 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; |
| 324 | } |
| 325 | #ifdef CONFIG_NET_MULTI |
| 326 | eth_set_enetaddr(0, argv[2]); |
| 327 | #endif |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | if (strcmp(argv[1],"ipaddr") == 0) { |
| 332 | char *s = argv[2]; /* always use only one arg */ |
| 333 | char *e; |
| 334 | unsigned long addr; |
| 335 | bd->bi_ip_addr = 0; |
| 336 | for (addr=0, i=0; i<4; ++i) { |
| 337 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 338 | addr <<= 8; |
| 339 | addr |= (val & 0xFF); |
| 340 | if (s) s = (*e) ? e+1 : e; |
| 341 | } |
| 342 | bd->bi_ip_addr = htonl(addr); |
| 343 | return 0; |
| 344 | } |
| 345 | if (strcmp(argv[1],"loadaddr") == 0) { |
| 346 | load_addr = simple_strtoul(argv[2], NULL, 16); |
| 347 | return 0; |
| 348 | } |
| 349 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 350 | if (strcmp(argv[1],"bootfile") == 0) { |
| 351 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 352 | return 0; |
| 353 | } |
| 354 | #endif /* CFG_CMD_NET */ |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | void setenv (char *varname, char *varvalue) |
| 359 | { |
| 360 | char *argv[4] = { "setenv", varname, varvalue, NULL }; |
| 361 | _do_setenv (0, 3, argv); |
| 362 | } |
| 363 | |
| 364 | int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 365 | { |
| 366 | if (argc < 2) { |
| 367 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | return _do_setenv (flag, argc, argv); |
| 372 | } |
| 373 | |
| 374 | /************************************************************************ |
| 375 | * Prompt for environment variable |
| 376 | */ |
| 377 | |
| 378 | #if (CONFIG_COMMANDS & CFG_CMD_ASKENV) |
| 379 | int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 380 | { |
| 381 | extern char console_buffer[CFG_CBSIZE]; |
| 382 | char message[CFG_CBSIZE]; |
| 383 | int size = CFG_CBSIZE - 1; |
| 384 | int len; |
| 385 | char *local_args[4]; |
| 386 | |
| 387 | local_args[0] = argv[0]; |
| 388 | local_args[1] = argv[1]; |
| 389 | local_args[2] = NULL; |
| 390 | local_args[3] = NULL; |
| 391 | |
| 392 | if (argc < 2) { |
| 393 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 394 | return 1; |
| 395 | } |
| 396 | /* Check the syntax */ |
| 397 | switch (argc) { |
| 398 | case 1: |
| 399 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 400 | return 1; |
| 401 | |
| 402 | case 2: /* askenv envname */ |
| 403 | sprintf (message, "Please enter '%s':", argv[1]); |
| 404 | break; |
| 405 | |
| 406 | case 3: /* askenv envname size */ |
| 407 | sprintf (message, "Please enter '%s':", argv[1]); |
| 408 | size = simple_strtoul (argv[2], NULL, 10); |
| 409 | break; |
| 410 | |
| 411 | default: /* askenv envname message1 ... messagen size */ |
| 412 | { |
| 413 | int i; |
| 414 | int pos = 0; |
| 415 | |
| 416 | for (i = 2; i < argc - 1; i++) { |
| 417 | if (pos) { |
| 418 | message[pos++] = ' '; |
| 419 | } |
| 420 | strcpy (message+pos, argv[i]); |
| 421 | pos += strlen(argv[i]); |
| 422 | } |
| 423 | message[pos] = '\0'; |
| 424 | size = simple_strtoul (argv[argc - 1], NULL, 10); |
| 425 | } |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | if (size >= CFG_CBSIZE) |
| 430 | size = CFG_CBSIZE - 1; |
| 431 | |
| 432 | if (size <= 0) |
| 433 | return 1; |
| 434 | |
| 435 | /* prompt for input */ |
| 436 | len = readline (message); |
| 437 | |
| 438 | if (size < len) |
| 439 | console_buffer[size] = '\0'; |
| 440 | |
| 441 | len = 2; |
| 442 | if (console_buffer[0] != '\0') { |
| 443 | local_args[2] = console_buffer; |
| 444 | len = 3; |
| 445 | } |
| 446 | |
| 447 | /* Continue calling setenv code */ |
| 448 | return _do_setenv (flag, len, local_args); |
| 449 | } |
| 450 | #endif /* CFG_CMD_ASKENV */ |
| 451 | |
| 452 | /************************************************************************ |
| 453 | * Look up variable from environment, |
| 454 | * return address of storage for that variable, |
| 455 | * or NULL if not found |
| 456 | */ |
| 457 | |
| 458 | char *getenv (uchar *name) |
| 459 | { |
| 460 | int i, nxt; |
| 461 | |
| 462 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 463 | int val; |
| 464 | |
| 465 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
| 466 | if (nxt >= CFG_ENV_SIZE) { |
| 467 | return (NULL); |
| 468 | } |
| 469 | } |
| 470 | if ((val=envmatch(name, i)) < 0) |
| 471 | continue; |
| 472 | return (env_get_addr(val)); |
| 473 | } |
| 474 | |
| 475 | return (NULL); |
| 476 | } |
| 477 | |
| 478 | int getenv_r (uchar *name, uchar *buf, unsigned len) |
| 479 | { |
| 480 | int i, nxt; |
| 481 | |
| 482 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 483 | int val, n; |
| 484 | |
| 485 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
| 486 | if (nxt >= CFG_ENV_SIZE) { |
| 487 | return (-1); |
| 488 | } |
| 489 | } |
| 490 | if ((val=envmatch(name, i)) < 0) |
| 491 | continue; |
| 492 | /* found; copy out */ |
| 493 | n = 0; |
| 494 | while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0') |
| 495 | ; |
| 496 | if (len == n) |
| 497 | *buf = '\0'; |
| 498 | return (n); |
| 499 | } |
| 500 | return (-1); |
| 501 | } |
| 502 | |
| 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[]) |
| 507 | { |
| 508 | extern char * env_name_spec; |
| 509 | |
| 510 | printf ("Saving Environment to %s...\n", env_name_spec); |
| 511 | |
| 512 | return (saveenv() ? 1 : 0); |
| 513 | } |
| 514 | #endif |
| 515 | |
| 516 | |
| 517 | /************************************************************************ |
| 518 | * Match a name / name=value pair |
| 519 | * |
| 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. |
| 523 | */ |
| 524 | |
| 525 | static int |
| 526 | envmatch (uchar *s1, int i2) |
| 527 | { |
| 528 | |
| 529 | while (*s1 == env_get_char(i2++)) |
| 530 | if (*s1++ == '=') |
| 531 | return(i2); |
| 532 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
| 533 | return(i2); |
| 534 | return(-1); |
| 535 | } |