1 /* 2 * (C) Copyright 2000-2013 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 * Copyright 2011 Freescale Semiconductor, Inc. 9 * 10 * SPDX-License-Identifier: GPL-2.0+ 11 */ 12 13 /* 14 * Support for persistent environment data 15 * 16 * The "environment" is stored on external storage as a list of '\0' 17 * terminated "name=value" strings. The end of the list is marked by 18 * a double '\0'. The environment is preceded by a 32 bit CRC over 19 * the data part and, in case of redundant environment, a byte of 20 * flags. 21 * 22 * This linearized representation will also be used before 23 * relocation, i. e. as long as we don't have a full C runtime 24 * environment. After that, we use a hash table. 25 */ 26 27 #include <common.h> 28 #include <cli.h> 29 #include <command.h> 30 #include <console.h> 31 #include <environment.h> 32 #include <search.h> 33 #include <errno.h> 34 #include <malloc.h> 35 #include <mapmem.h> 36 #include <watchdog.h> 37 #include <linux/stddef.h> 38 #include <asm/byteorder.h> 39 #include <asm/io.h> 40 41 DECLARE_GLOBAL_DATA_PTR; 42 43 #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \ 44 !defined(CONFIG_ENV_IS_IN_FLASH) && \ 45 !defined(CONFIG_ENV_IS_IN_MMC) && \ 46 !defined(CONFIG_ENV_IS_IN_FAT) && \ 47 !defined(CONFIG_ENV_IS_IN_EXT4) && \ 48 !defined(CONFIG_ENV_IS_IN_NAND) && \ 49 !defined(CONFIG_ENV_IS_IN_NVRAM) && \ 50 !defined(CONFIG_ENV_IS_IN_ONENAND) && \ 51 !defined(CONFIG_ENV_IS_IN_SATA) && \ 52 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ 53 !defined(CONFIG_ENV_IS_IN_REMOTE) && \ 54 !defined(CONFIG_ENV_IS_IN_UBI) && \ 55 !defined(CONFIG_ENV_IS_IN_BLK_DEV) && \ 56 !defined(CONFIG_ENV_IS_NOWHERE) 57 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\ 58 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI|BLK_DEV} or CONFIG_ENV_IS_NOWHERE 59 #endif 60 61 /* 62 * Maximum expected input data size for import command 63 */ 64 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ 65 66 /* 67 * This variable is incremented on each do_env_set(), so it can 68 * be used via get_env_id() as an indication, if the environment 69 * has changed or not. So it is possible to reread an environment 70 * variable only if the environment was changed ... done so for 71 * example in NetInitLoop() 72 */ 73 static int env_id = 1; 74 75 int get_env_id(void) 76 { 77 return env_id; 78 } 79 80 #ifndef CONFIG_SPL_BUILD 81 /* 82 * Command interface: print one or all environment variables 83 * 84 * Returns 0 in case of error, or length of printed string 85 */ 86 static int env_print(char *name, int flag) 87 { 88 char *res = NULL; 89 ssize_t len; 90 91 if (name) { /* print a single name */ 92 ENTRY e, *ep; 93 94 e.key = name; 95 e.data = NULL; 96 hsearch_r(e, FIND, &ep, &env_htab, flag); 97 if (ep == NULL) 98 return 0; 99 len = printf("%s=%s\n", ep->key, ep->data); 100 return len; 101 } 102 103 /* print whole list */ 104 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); 105 106 if (len > 0) { 107 puts(res); 108 free(res); 109 return len; 110 } 111 112 /* should never happen */ 113 printf("## Error: cannot export environment\n"); 114 return 0; 115 } 116 117 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, 118 char * const argv[]) 119 { 120 int i; 121 int rcode = 0; 122 int env_flag = H_HIDE_DOT; 123 124 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') { 125 argc--; 126 argv++; 127 env_flag &= ~H_HIDE_DOT; 128 } 129 130 if (argc == 1) { 131 /* print all env vars */ 132 rcode = env_print(NULL, env_flag); 133 if (!rcode) 134 return 1; 135 printf("\nEnvironment size: %d/%ld bytes\n", 136 rcode, (ulong)ENV_SIZE); 137 return 0; 138 } 139 140 /* print selected env vars */ 141 env_flag &= ~H_HIDE_DOT; 142 for (i = 1; i < argc; ++i) { 143 int rc = env_print(argv[i], env_flag); 144 if (!rc) { 145 printf("## Error: \"%s\" not defined\n", argv[i]); 146 ++rcode; 147 } 148 } 149 150 return rcode; 151 } 152 153 #ifdef CONFIG_CMD_GREPENV 154 static int do_env_grep(cmd_tbl_t *cmdtp, int flag, 155 int argc, char * const argv[]) 156 { 157 char *res = NULL; 158 int len, grep_how, grep_what; 159 160 if (argc < 2) 161 return CMD_RET_USAGE; 162 163 grep_how = H_MATCH_SUBSTR; /* default: substring search */ 164 grep_what = H_MATCH_BOTH; /* default: grep names and values */ 165 166 while (--argc > 0 && **++argv == '-') { 167 char *arg = *argv; 168 while (*++arg) { 169 switch (*arg) { 170 #ifdef CONFIG_REGEX 171 case 'e': /* use regex matching */ 172 grep_how = H_MATCH_REGEX; 173 break; 174 #endif 175 case 'n': /* grep for name */ 176 grep_what = H_MATCH_KEY; 177 break; 178 case 'v': /* grep for value */ 179 grep_what = H_MATCH_DATA; 180 break; 181 case 'b': /* grep for both */ 182 grep_what = H_MATCH_BOTH; 183 break; 184 case '-': 185 goto DONE; 186 default: 187 return CMD_RET_USAGE; 188 } 189 } 190 } 191 192 DONE: 193 len = hexport_r(&env_htab, '\n', 194 flag | grep_what | grep_how, 195 &res, 0, argc, argv); 196 197 if (len > 0) { 198 puts(res); 199 free(res); 200 } 201 202 if (len < 2) 203 return 1; 204 205 return 0; 206 } 207 #endif 208 #endif /* CONFIG_SPL_BUILD */ 209 210 /* 211 * Set a new environment variable, 212 * or replace or delete an existing one. 213 */ 214 static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) 215 { 216 int i, len; 217 char *name, *value, *s; 218 ENTRY e, *ep; 219 220 debug("Initial value for argc=%d\n", argc); 221 while (argc > 1 && **(argv + 1) == '-') { 222 char *arg = *++argv; 223 224 --argc; 225 while (*++arg) { 226 switch (*arg) { 227 case 'f': /* force */ 228 env_flag |= H_FORCE; 229 break; 230 default: 231 return CMD_RET_USAGE; 232 } 233 } 234 } 235 debug("Final value for argc=%d\n", argc); 236 name = argv[1]; 237 238 if (strchr(name, '=')) { 239 printf("## Error: illegal character '='" 240 "in variable name \"%s\"\n", name); 241 return 1; 242 } 243 244 env_id++; 245 246 /* Delete only ? */ 247 if (argc < 3 || argv[2] == NULL) { 248 int rc = hdelete_r(name, &env_htab, env_flag); 249 return !rc; 250 } 251 252 /* 253 * Insert / replace new value 254 */ 255 for (i = 2, len = 0; i < argc; ++i) 256 len += strlen(argv[i]) + 1; 257 258 value = malloc(len); 259 if (value == NULL) { 260 printf("## Can't malloc %d bytes\n", len); 261 return 1; 262 } 263 for (i = 2, s = value; i < argc; ++i) { 264 char *v = argv[i]; 265 266 while ((*s++ = *v++) != '\0') 267 ; 268 *(s - 1) = ' '; 269 } 270 if (s != value) 271 *--s = '\0'; 272 273 e.key = name; 274 e.data = value; 275 hsearch_r(e, ENTER, &ep, &env_htab, env_flag); 276 free(value); 277 if (!ep) { 278 printf("## Error inserting \"%s\" variable, errno=%d\n", 279 name, errno); 280 return 1; 281 } 282 283 return 0; 284 } 285 286 int env_set(const char *varname, const char *varvalue) 287 { 288 const char * const argv[4] = { "setenv", varname, varvalue, NULL }; 289 290 /* before import into hashtable */ 291 if (!(gd->flags & GD_FLG_ENV_READY)) 292 return 1; 293 294 if (varvalue == NULL || varvalue[0] == '\0') 295 return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); 296 else 297 return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); 298 } 299 300 static int env_append(const char *varname, const char *varvalue) 301 { 302 int len = 0; 303 char *oldvalue, *newvalue; 304 305 debug("%s: varvalue = %s\n", __func__, varvalue); 306 307 /* before import into hashtable */ 308 if (!(gd->flags & GD_FLG_ENV_READY) || !varname) 309 return 1; 310 311 if (env_exist(varname, varvalue)) 312 return 0; 313 314 debug("%s: reall append: %s\n", __func__, varvalue); 315 316 if (varvalue) 317 len += strlen(varvalue); 318 319 oldvalue = env_get(varname); 320 if (oldvalue) 321 len += strlen(oldvalue); 322 323 newvalue = malloc(len + 2); 324 if (!newvalue) { 325 printf("Error: malloc in %s failed!\n", __func__); 326 return 1; 327 } 328 329 *newvalue = '\0'; 330 331 if (oldvalue) { 332 strcpy(newvalue, oldvalue); 333 strcat(newvalue, " "); 334 } 335 336 if (varvalue) 337 strcat(newvalue, varvalue); 338 339 debug("%s: newvalue: %s\n", __func__, newvalue); 340 env_set(varname, newvalue); 341 free(newvalue); 342 343 return 0; 344 } 345 346 static int env_replace(const char *varname, const char *substr, 347 const char *replacement) 348 { 349 char *oldvalue, *newvalue, *dst, *sub; 350 int substr_len, replace_len, oldvalue_len, len; 351 352 /* before import into hashtable */ 353 if (!(gd->flags & GD_FLG_ENV_READY) || !varname) 354 return 1; 355 356 oldvalue = env_get(varname); 357 if (!oldvalue) 358 return 1; 359 360 sub = strstr(oldvalue, substr); 361 if (!sub) 362 return 1; 363 364 oldvalue_len = strlen(oldvalue) + 1; 365 substr_len = strlen(substr); 366 replace_len = strlen(replacement); 367 368 if (replace_len >= substr_len) 369 len = oldvalue_len + replace_len - substr_len; 370 else 371 len = oldvalue_len + substr_len - replace_len; 372 373 newvalue = malloc(len); 374 if (!newvalue) { 375 printf("Error: malloc in %s failed!\n", __func__); 376 return 1; 377 } 378 379 *newvalue = '\0'; 380 381 /* 382 * Orignal string is splited like format: [str1.. substr str2..] 383 */ 384 385 /* str1.. */ 386 dst = newvalue; 387 dst = strncat(dst, oldvalue, sub - oldvalue); 388 389 /* substr */ 390 dst += sub - oldvalue; 391 dst = strncat(dst, replacement, replace_len); 392 393 /* str2.. */ 394 dst += replace_len; 395 len = oldvalue_len - substr_len - (sub - oldvalue); 396 dst = strncat(dst, sub + substr_len, len); 397 398 env_set(varname, newvalue); 399 free(newvalue); 400 401 return 0; 402 } 403 404 #define ARGS_ITEM_NUM 50 405 406 int env_update_filter(const char *varname, const char *varvalue, 407 const char *ignore) 408 { 409 /* 'a_' means "varargs_'; 'v_' means 'varvalue_' */ 410 char *varargs; 411 char *a_title, *v_title; 412 char *a_string_tok, *a_item_tok = NULL; 413 char *v_string_tok, *v_item_tok = NULL; 414 char *a_item, *a_items[ARGS_ITEM_NUM] = { NULL }; 415 char *v_item, *v_items[ARGS_ITEM_NUM] = { NULL }; 416 bool match = false; 417 int i = 0, j = 0; 418 419 /* Before import into hashtable */ 420 if (!(gd->flags & GD_FLG_ENV_READY) || !varname) 421 return 1; 422 423 /* If varname doesn't exist, create it and set varvalue */ 424 varargs = env_get(varname); 425 if (!varargs) { 426 env_set(varname, varvalue); 427 if (ignore && strstr(varvalue, ignore)) 428 env_delete(varname, ignore, 0); 429 return 0; 430 } 431 432 /* Malloc a temporary varargs for strtok */ 433 a_string_tok = strdup(varargs); 434 if (!a_string_tok) { 435 printf("Error: strdup in failed, line=%d\n", __LINE__); 436 return 1; 437 } 438 439 /* Malloc a temporary varvalue for strtok */ 440 v_string_tok = strdup(varvalue); 441 if (!v_string_tok) { 442 free(a_string_tok); 443 printf("Error: strdup in failed, line=%d\n", __LINE__); 444 return 1; 445 } 446 447 /* Splite varargs into items containing "=" by the blank */ 448 a_item = strtok(a_string_tok, " "); 449 while (a_item && i < ARGS_ITEM_NUM) { 450 debug("%s: [a_item %d]: %s\n", __func__, i, a_item); 451 if (strstr(a_item, "=")) 452 a_items[i++] = a_item; 453 a_item = strtok(NULL, " "); 454 } 455 456 /* 457 * Splite varvalue into items containing "=" by the blank. 458 * parse varvalue title, eg: "bootmode=emmc", title is "bootmode" 459 */ 460 v_item = strtok(v_string_tok, " "); 461 while (v_item && j < ARGS_ITEM_NUM) { 462 debug("%s: <v_item %d>: %s ", __func__, j, v_item); 463 464 /* filter ignore string */ 465 if (ignore && strstr(v_item, ignore)) { 466 v_item = strtok(NULL, " "); 467 debug("...ignore\n"); 468 continue; 469 } 470 471 if (strstr(v_item, "=")) { 472 debug("\n"); 473 v_items[j++] = v_item; 474 } else { 475 debug("... do append\n"); 476 env_append(varname, v_item); 477 } 478 479 v_item = strtok(NULL, " "); 480 } 481 482 /* For every v_item, search its title */ 483 for (j = 0; j < ARGS_ITEM_NUM && v_items[j]; j++) { 484 v_item = v_items[j]; 485 /* Malloc a temporary a_item for strtok */ 486 v_item_tok = strdup(v_item); 487 if (!v_item_tok) { 488 printf("Error: strdup in failed, line=%d\n", __LINE__); 489 free(a_string_tok); 490 free(v_string_tok); 491 return 1; 492 } 493 v_title = strtok(v_item_tok, "="); 494 debug("%s: <v_title>: %s\n", __func__, v_title); 495 496 /* For every a_item, search its title */ 497 for (i = 0; i < ARGS_ITEM_NUM && a_items[i]; i++) { 498 a_item = a_items[i]; 499 /* Malloc a temporary a_item for strtok */ 500 a_item_tok = strdup(a_item); 501 if (!a_item_tok) { 502 printf("Error: strdup in failed, line=%d\n", __LINE__); 503 free(a_string_tok); 504 free(v_string_tok); 505 free(v_item_tok); 506 return 1; 507 } 508 509 a_title = strtok(a_item_tok, "="); 510 debug("%s: [a_title]: %s\n", __func__, a_title); 511 if (!strcmp(a_title, v_title)) { 512 /* Find! replace it */ 513 env_replace(varname, a_item, v_item); 514 free(a_item_tok); 515 match = true; 516 break; 517 } 518 free(a_item_tok); 519 } 520 521 /* Not find, just append */ 522 if (!match) { 523 debug("%s: append '%s' to the '%s' end\n", 524 __func__, v_item, varname); 525 env_append(varname, v_item); 526 } 527 match = false; 528 free(v_item_tok); 529 } 530 531 free(v_string_tok); 532 free(a_string_tok); 533 534 return 0; 535 } 536 537 int env_update(const char *varname, const char *varvalue) 538 { 539 return env_update_filter(varname, varvalue, NULL); 540 } 541 542 char *env_exist(const char *varname, const char *varvalue) 543 { 544 char *buf, *ptr = NULL; 545 char *oldvalue, *p; 546 int len; 547 548 /* before import into hashtable */ 549 if (!(gd->flags & GD_FLG_ENV_READY) || !varname) 550 return NULL; 551 552 len = strlen(varvalue) + 8; /* extra 8 byte is enough*/ 553 buf = calloc(1, len); 554 if (!buf) 555 return NULL; 556 557 oldvalue = env_get(varname); 558 if (oldvalue) { 559 /* Match middle one ? */ 560 snprintf(buf, len, " %s ", varvalue); 561 p = strstr(oldvalue, buf); 562 if (p) { 563 debug("%s: '%s' is already exist in '%s'(middle)\n", 564 __func__, varvalue, varname); 565 ptr = (p + 1); 566 goto out; 567 } else { 568 debug("%s: not find in middle one\n", __func__); 569 } 570 571 /* Match last one ? */ 572 snprintf(buf, len, " %s", varvalue); 573 p = strstr(oldvalue, buf); 574 if (p) { 575 if (*(p + strlen(varvalue) + 1) == '\0') { 576 debug("%s: '%s' is already exist in '%s'(last)\n", 577 __func__, varvalue, varname); 578 ptr = (p + 1); 579 goto out; 580 } 581 } else { 582 debug("%s: not find in last one\n", __func__); 583 } 584 585 /* Match first one ? */ 586 snprintf(buf, len, "%s ", varvalue); 587 p = strstr(oldvalue, buf); 588 if (p) { 589 len = strstr(p, " ") - oldvalue; 590 if (len == strlen(varvalue)) { 591 debug("%s: '%s' is already exist in '%s'(first)\n", 592 __func__, varvalue, varname); 593 ptr = p; 594 goto out; 595 } 596 } else { 597 debug("%s: not find in first one\n", __func__); 598 } 599 } 600 out: 601 free(buf); 602 603 return ptr; 604 } 605 606 int env_delete(const char *varname, const char *varvalue, int complete_match) 607 { 608 const char *str; 609 char *value, *start; 610 611 /* before import into hashtable */ 612 if (!(gd->flags & GD_FLG_ENV_READY) || !varname) 613 return 1; 614 615 value = env_get(varname); 616 if (!value) 617 return 0; 618 619 start = complete_match ? 620 env_exist(varname, varvalue) : strstr(value, varvalue); 621 if (!start) 622 return 0; 623 624 /* varvalue is not the last property */ 625 str = strstr(start, " "); 626 if (str) { 627 /* Terminate, so cmdline can be dest for strcat() */ 628 *start = '\0'; 629 /* +1 to skip white space */ 630 strcat((char *)value, (str + 1)); 631 /* varvalue is the last property */ 632 } else { 633 /* skip white space */ 634 *(start - 1) = '\0'; 635 } 636 637 return 0; 638 } 639 640 /** 641 * Set an environment variable to an integer value 642 * 643 * @param varname Environment variable to set 644 * @param value Value to set it to 645 * @return 0 if ok, 1 on error 646 */ 647 int env_set_ulong(const char *varname, ulong value) 648 { 649 /* TODO: this should be unsigned */ 650 char *str = simple_itoa(value); 651 652 return env_set(varname, str); 653 } 654 655 /** 656 * Set an environment variable to an value in hex 657 * 658 * @param varname Environment variable to set 659 * @param value Value to set it to 660 * @return 0 if ok, 1 on error 661 */ 662 int env_set_hex(const char *varname, ulong value) 663 { 664 char str[19]; 665 666 sprintf(str, "0x%lx", value); 667 return env_set(varname, str); 668 } 669 670 ulong env_get_hex(const char *varname, ulong default_val) 671 { 672 const char *s; 673 ulong value; 674 char *endp; 675 676 s = env_get(varname); 677 if (s) 678 value = simple_strtoul(s, &endp, 16); 679 if (!s || endp == s) 680 return default_val; 681 682 return value; 683 } 684 685 #ifndef CONFIG_SPL_BUILD 686 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 687 { 688 if (argc < 2) 689 return CMD_RET_USAGE; 690 691 return _do_env_set(flag, argc, argv, H_INTERACTIVE); 692 } 693 694 /* 695 * Prompt for environment variable 696 */ 697 #if defined(CONFIG_CMD_ASKENV) 698 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 699 { 700 char message[CONFIG_SYS_CBSIZE]; 701 int i, len, pos, size; 702 char *local_args[4]; 703 char *endptr; 704 705 local_args[0] = argv[0]; 706 local_args[1] = argv[1]; 707 local_args[2] = NULL; 708 local_args[3] = NULL; 709 710 /* 711 * Check the syntax: 712 * 713 * env_ask envname [message1 ...] [size] 714 */ 715 if (argc == 1) 716 return CMD_RET_USAGE; 717 718 /* 719 * We test the last argument if it can be converted 720 * into a decimal number. If yes, we assume it's 721 * the size. Otherwise we echo it as part of the 722 * message. 723 */ 724 i = simple_strtoul(argv[argc - 1], &endptr, 10); 725 if (*endptr != '\0') { /* no size */ 726 size = CONFIG_SYS_CBSIZE - 1; 727 } else { /* size given */ 728 size = i; 729 --argc; 730 } 731 732 if (argc <= 2) { 733 sprintf(message, "Please enter '%s': ", argv[1]); 734 } else { 735 /* env_ask envname message1 ... messagen [size] */ 736 for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) { 737 if (pos) 738 message[pos++] = ' '; 739 740 strncpy(message + pos, argv[i], sizeof(message) - pos); 741 pos += strlen(argv[i]); 742 } 743 if (pos < sizeof(message) - 1) { 744 message[pos++] = ' '; 745 message[pos] = '\0'; 746 } else 747 message[CONFIG_SYS_CBSIZE - 1] = '\0'; 748 } 749 750 if (size >= CONFIG_SYS_CBSIZE) 751 size = CONFIG_SYS_CBSIZE - 1; 752 753 if (size <= 0) 754 return 1; 755 756 /* prompt for input */ 757 len = cli_readline(message); 758 759 if (size < len) 760 console_buffer[size] = '\0'; 761 762 len = 2; 763 if (console_buffer[0] != '\0') { 764 local_args[2] = console_buffer; 765 len = 3; 766 } 767 768 /* Continue calling setenv code */ 769 return _do_env_set(flag, len, local_args, H_INTERACTIVE); 770 } 771 #endif 772 773 #if defined(CONFIG_CMD_ENV_CALLBACK) 774 static int print_static_binding(const char *var_name, const char *callback_name, 775 void *priv) 776 { 777 printf("\t%-20s %-20s\n", var_name, callback_name); 778 779 return 0; 780 } 781 782 static int print_active_callback(ENTRY *entry) 783 { 784 struct env_clbk_tbl *clbkp; 785 int i; 786 int num_callbacks; 787 788 if (entry->callback == NULL) 789 return 0; 790 791 /* look up the callback in the linker-list */ 792 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); 793 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); 794 i < num_callbacks; 795 i++, clbkp++) { 796 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 797 if (entry->callback == clbkp->callback + gd->reloc_off) 798 #else 799 if (entry->callback == clbkp->callback) 800 #endif 801 break; 802 } 803 804 if (i == num_callbacks) 805 /* this should probably never happen, but just in case... */ 806 printf("\t%-20s %p\n", entry->key, entry->callback); 807 else 808 printf("\t%-20s %-20s\n", entry->key, clbkp->name); 809 810 return 0; 811 } 812 813 /* 814 * Print the callbacks available and what they are bound to 815 */ 816 int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 817 { 818 struct env_clbk_tbl *clbkp; 819 int i; 820 int num_callbacks; 821 822 /* Print the available callbacks */ 823 puts("Available callbacks:\n"); 824 puts("\tCallback Name\n"); 825 puts("\t-------------\n"); 826 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); 827 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); 828 i < num_callbacks; 829 i++, clbkp++) 830 printf("\t%s\n", clbkp->name); 831 puts("\n"); 832 833 /* Print the static bindings that may exist */ 834 puts("Static callback bindings:\n"); 835 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); 836 printf("\t%-20s %-20s\n", "-------------", "-------------"); 837 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL); 838 puts("\n"); 839 840 /* walk through each variable and print the callback if it has one */ 841 puts("Active callback bindings:\n"); 842 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); 843 printf("\t%-20s %-20s\n", "-------------", "-------------"); 844 hwalk_r(&env_htab, print_active_callback); 845 return 0; 846 } 847 #endif 848 849 #if defined(CONFIG_CMD_ENV_FLAGS) 850 static int print_static_flags(const char *var_name, const char *flags, 851 void *priv) 852 { 853 enum env_flags_vartype type = env_flags_parse_vartype(flags); 854 enum env_flags_varaccess access = env_flags_parse_varaccess(flags); 855 856 printf("\t%-20s %-20s %-20s\n", var_name, 857 env_flags_get_vartype_name(type), 858 env_flags_get_varaccess_name(access)); 859 860 return 0; 861 } 862 863 static int print_active_flags(ENTRY *entry) 864 { 865 enum env_flags_vartype type; 866 enum env_flags_varaccess access; 867 868 if (entry->flags == 0) 869 return 0; 870 871 type = (enum env_flags_vartype) 872 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); 873 access = env_flags_parse_varaccess_from_binflags(entry->flags); 874 printf("\t%-20s %-20s %-20s\n", entry->key, 875 env_flags_get_vartype_name(type), 876 env_flags_get_varaccess_name(access)); 877 878 return 0; 879 } 880 881 /* 882 * Print the flags available and what variables have flags 883 */ 884 int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 885 { 886 /* Print the available variable types */ 887 printf("Available variable type flags (position %d):\n", 888 ENV_FLAGS_VARTYPE_LOC); 889 puts("\tFlag\tVariable Type Name\n"); 890 puts("\t----\t------------------\n"); 891 env_flags_print_vartypes(); 892 puts("\n"); 893 894 /* Print the available variable access types */ 895 printf("Available variable access flags (position %d):\n", 896 ENV_FLAGS_VARACCESS_LOC); 897 puts("\tFlag\tVariable Access Name\n"); 898 puts("\t----\t--------------------\n"); 899 env_flags_print_varaccess(); 900 puts("\n"); 901 902 /* Print the static flags that may exist */ 903 puts("Static flags:\n"); 904 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", 905 "Variable Access"); 906 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", 907 "---------------"); 908 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL); 909 puts("\n"); 910 911 /* walk through each variable and print the flags if non-default */ 912 puts("Active flags:\n"); 913 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", 914 "Variable Access"); 915 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", 916 "---------------"); 917 hwalk_r(&env_htab, print_active_flags); 918 return 0; 919 } 920 #endif 921 922 /* 923 * Interactively edit an environment variable 924 */ 925 #if defined(CONFIG_CMD_EDITENV) 926 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, 927 char * const argv[]) 928 { 929 char buffer[CONFIG_SYS_CBSIZE]; 930 char *init_val; 931 932 if (argc < 2) 933 return CMD_RET_USAGE; 934 935 /* before import into hashtable */ 936 if (!(gd->flags & GD_FLG_ENV_READY)) 937 return 1; 938 939 /* Set read buffer to initial value or empty sting */ 940 init_val = env_get(argv[1]); 941 if (init_val) 942 snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val); 943 else 944 buffer[0] = '\0'; 945 946 if (cli_readline_into_buffer("edit: ", buffer, 0) < 0) 947 return 1; 948 949 if (buffer[0] == '\0') { 950 const char * const _argv[3] = { "setenv", argv[1], NULL }; 951 952 return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE); 953 } else { 954 const char * const _argv[4] = { "setenv", argv[1], buffer, 955 NULL }; 956 957 return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE); 958 } 959 } 960 #endif /* CONFIG_CMD_EDITENV */ 961 #endif /* CONFIG_SPL_BUILD */ 962 963 /* 964 * Look up variable from environment, 965 * return address of storage for that variable, 966 * or NULL if not found 967 */ 968 char *env_get(const char *name) 969 { 970 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ 971 ENTRY e, *ep; 972 973 WATCHDOG_RESET(); 974 975 e.key = name; 976 e.data = NULL; 977 hsearch_r(e, FIND, &ep, &env_htab, 0); 978 979 return ep ? ep->data : NULL; 980 } 981 982 /* restricted capabilities before import */ 983 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) 984 return (char *)(gd->env_buf); 985 986 return NULL; 987 } 988 989 /* 990 * Look up variable from environment for restricted C runtime env. 991 */ 992 int env_get_f(const char *name, char *buf, unsigned len) 993 { 994 int i, nxt; 995 996 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { 997 int val, n; 998 999 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) { 1000 if (nxt >= CONFIG_ENV_SIZE) 1001 return -1; 1002 } 1003 1004 val = envmatch((uchar *)name, i); 1005 if (val < 0) 1006 continue; 1007 1008 /* found; copy out */ 1009 for (n = 0; n < len; ++n, ++buf) { 1010 *buf = env_get_char(val++); 1011 if (*buf == '\0') 1012 return n; 1013 } 1014 1015 if (n) 1016 *--buf = '\0'; 1017 1018 printf("env_buf [%d bytes] too small for value of \"%s\"\n", 1019 len, name); 1020 1021 return n; 1022 } 1023 1024 return -1; 1025 } 1026 1027 /** 1028 * Decode the integer value of an environment variable and return it. 1029 * 1030 * @param name Name of environemnt variable 1031 * @param base Number base to use (normally 10, or 16 for hex) 1032 * @param default_val Default value to return if the variable is not 1033 * found 1034 * @return the decoded value, or default_val if not found 1035 */ 1036 ulong env_get_ulong(const char *name, int base, ulong default_val) 1037 { 1038 /* 1039 * We can use env_get() here, even before relocation, since the 1040 * environment variable value is an integer and thus short. 1041 */ 1042 const char *str = env_get(name); 1043 1044 return str ? simple_strtoul(str, NULL, base) : default_val; 1045 } 1046 1047 #ifndef CONFIG_SPL_BUILD 1048 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) 1049 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, 1050 char * const argv[]) 1051 { 1052 struct env_driver *env = env_driver_lookup_default(); 1053 1054 printf("Saving Environment to %s...\n", env->name); 1055 1056 return env_save() ? 1 : 0; 1057 } 1058 1059 U_BOOT_CMD( 1060 saveenv, 1, 0, do_env_save, 1061 "save environment variables to persistent storage", 1062 "" 1063 ); 1064 #endif 1065 #endif /* CONFIG_SPL_BUILD */ 1066 1067 1068 /* 1069 * Match a name / name=value pair 1070 * 1071 * s1 is either a simple 'name', or a 'name=value' pair. 1072 * i2 is the environment index for a 'name2=value2' pair. 1073 * If the names match, return the index for the value2, else -1. 1074 */ 1075 int envmatch(uchar *s1, int i2) 1076 { 1077 if (s1 == NULL) 1078 return -1; 1079 1080 while (*s1 == env_get_char(i2++)) 1081 if (*s1++ == '=') 1082 return i2; 1083 1084 if (*s1 == '\0' && env_get_char(i2-1) == '=') 1085 return i2; 1086 1087 return -1; 1088 } 1089 1090 #ifndef CONFIG_SPL_BUILD 1091 static int do_env_default(cmd_tbl_t *cmdtp, int __flag, 1092 int argc, char * const argv[]) 1093 { 1094 int all = 0, flag = 0; 1095 1096 debug("Initial value for argc=%d\n", argc); 1097 while (--argc > 0 && **++argv == '-') { 1098 char *arg = *argv; 1099 1100 while (*++arg) { 1101 switch (*arg) { 1102 case 'a': /* default all */ 1103 all = 1; 1104 break; 1105 case 'f': /* force */ 1106 flag |= H_FORCE; 1107 break; 1108 default: 1109 return cmd_usage(cmdtp); 1110 } 1111 } 1112 } 1113 debug("Final value for argc=%d\n", argc); 1114 if (all && (argc == 0)) { 1115 /* Reset the whole environment */ 1116 set_default_env("## Resetting to default environment\n"); 1117 return 0; 1118 } 1119 if (!all && (argc > 0)) { 1120 /* Reset individual variables */ 1121 set_default_vars(argc, argv); 1122 return 0; 1123 } 1124 1125 return cmd_usage(cmdtp); 1126 } 1127 1128 static int do_env_delete(cmd_tbl_t *cmdtp, int flag, 1129 int argc, char * const argv[]) 1130 { 1131 int env_flag = H_INTERACTIVE; 1132 int ret = 0; 1133 1134 debug("Initial value for argc=%d\n", argc); 1135 while (argc > 1 && **(argv + 1) == '-') { 1136 char *arg = *++argv; 1137 1138 --argc; 1139 while (*++arg) { 1140 switch (*arg) { 1141 case 'f': /* force */ 1142 env_flag |= H_FORCE; 1143 break; 1144 default: 1145 return CMD_RET_USAGE; 1146 } 1147 } 1148 } 1149 debug("Final value for argc=%d\n", argc); 1150 1151 env_id++; 1152 1153 while (--argc > 0) { 1154 char *name = *++argv; 1155 1156 if (!hdelete_r(name, &env_htab, env_flag)) 1157 ret = 1; 1158 } 1159 1160 return ret; 1161 } 1162 1163 static int do_env_update(cmd_tbl_t *cmdtp, int flag, 1164 int argc, char *const argv[]) 1165 { 1166 if (argc != 3) 1167 return CMD_RET_USAGE; 1168 1169 return env_update(argv[1], argv[2]); 1170 } 1171 1172 #ifdef CONFIG_CMD_EXPORTENV 1173 /* 1174 * env export [-t | -b | -c] [-s size] addr [var ...] 1175 * -t: export as text format; if size is given, data will be 1176 * padded with '\0' bytes; if not, one terminating '\0' 1177 * will be added (which is included in the "filesize" 1178 * setting so you can for exmple copy this to flash and 1179 * keep the termination). 1180 * -b: export as binary format (name=value pairs separated by 1181 * '\0', list end marked by double "\0\0") 1182 * -c: export as checksum protected environment format as 1183 * used for example by "saveenv" command 1184 * -s size: 1185 * size of output buffer 1186 * addr: memory address where environment gets stored 1187 * var... List of variable names that get included into the 1188 * export. Without arguments, the whole environment gets 1189 * exported. 1190 * 1191 * With "-c" and size is NOT given, then the export command will 1192 * format the data as currently used for the persistent storage, 1193 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and 1194 * prepend a valid CRC32 checksum and, in case of redundant 1195 * environment, a "current" redundancy flag. If size is given, this 1196 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 1197 * checksum and redundancy flag will be inserted. 1198 * 1199 * With "-b" and "-t", always only the real data (including a 1200 * terminating '\0' byte) will be written; here the optional size 1201 * argument will be used to make sure not to overflow the user 1202 * provided buffer; the command will abort if the size is not 1203 * sufficient. Any remaining space will be '\0' padded. 1204 * 1205 * On successful return, the variable "filesize" will be set. 1206 * Note that filesize includes the trailing/terminating '\0' byte(s). 1207 * 1208 * Usage scenario: create a text snapshot/backup of the current settings: 1209 * 1210 * => env export -t 100000 1211 * => era ${backup_addr} +${filesize} 1212 * => cp.b 100000 ${backup_addr} ${filesize} 1213 * 1214 * Re-import this snapshot, deleting all other settings: 1215 * 1216 * => env import -d -t ${backup_addr} 1217 */ 1218 static int do_env_export(cmd_tbl_t *cmdtp, int flag, 1219 int argc, char * const argv[]) 1220 { 1221 char buf[32]; 1222 ulong addr; 1223 char *ptr, *cmd, *res; 1224 size_t size = 0; 1225 ssize_t len; 1226 env_t *envp; 1227 char sep = '\n'; 1228 int chk = 0; 1229 int fmt = 0; 1230 1231 cmd = *argv; 1232 1233 while (--argc > 0 && **++argv == '-') { 1234 char *arg = *argv; 1235 while (*++arg) { 1236 switch (*arg) { 1237 case 'b': /* raw binary format */ 1238 if (fmt++) 1239 goto sep_err; 1240 sep = '\0'; 1241 break; 1242 case 'c': /* external checksum format */ 1243 if (fmt++) 1244 goto sep_err; 1245 sep = '\0'; 1246 chk = 1; 1247 break; 1248 case 's': /* size given */ 1249 if (--argc <= 0) 1250 return cmd_usage(cmdtp); 1251 size = simple_strtoul(*++argv, NULL, 16); 1252 goto NXTARG; 1253 case 't': /* text format */ 1254 if (fmt++) 1255 goto sep_err; 1256 sep = '\n'; 1257 break; 1258 default: 1259 return CMD_RET_USAGE; 1260 } 1261 } 1262 NXTARG: ; 1263 } 1264 1265 if (argc < 1) 1266 return CMD_RET_USAGE; 1267 1268 addr = simple_strtoul(argv[0], NULL, 16); 1269 ptr = map_sysmem(addr, size); 1270 1271 if (size) 1272 memset(ptr, '\0', size); 1273 1274 argc--; 1275 argv++; 1276 1277 if (sep) { /* export as text file */ 1278 len = hexport_r(&env_htab, sep, 1279 H_MATCH_KEY | H_MATCH_IDENT, 1280 &ptr, size, argc, argv); 1281 if (len < 0) { 1282 pr_err("Cannot export environment: errno = %d\n", errno); 1283 return 1; 1284 } 1285 sprintf(buf, "%zX", (size_t)len); 1286 env_set("filesize", buf); 1287 1288 return 0; 1289 } 1290 1291 envp = (env_t *)ptr; 1292 1293 if (chk) /* export as checksum protected block */ 1294 res = (char *)envp->data; 1295 else /* export as raw binary data */ 1296 res = ptr; 1297 1298 len = hexport_r(&env_htab, '\0', 1299 H_MATCH_KEY | H_MATCH_IDENT, 1300 &res, ENV_SIZE, argc, argv); 1301 if (len < 0) { 1302 pr_err("Cannot export environment: errno = %d\n", errno); 1303 return 1; 1304 } 1305 1306 if (chk) { 1307 envp->crc = crc32(0, envp->data, ENV_SIZE); 1308 #ifdef CONFIG_ENV_ADDR_REDUND 1309 envp->flags = ACTIVE_FLAG; 1310 #endif 1311 } 1312 env_set_hex("filesize", len + offsetof(env_t, data)); 1313 1314 return 0; 1315 1316 sep_err: 1317 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd); 1318 return 1; 1319 } 1320 #endif 1321 1322 #ifdef CONFIG_CMD_IMPORTENV 1323 /* 1324 * env import [-d] [-t [-r] | -b | -c] addr [size] 1325 * -d: delete existing environment before importing; 1326 * otherwise overwrite / append to existing definitions 1327 * -t: assume text format; either "size" must be given or the 1328 * text data must be '\0' terminated 1329 * -r: handle CRLF like LF, that means exported variables with 1330 * a content which ends with \r won't get imported. Used 1331 * to import text files created with editors which are using CRLF 1332 * for line endings. Only effective in addition to -t. 1333 * -b: assume binary format ('\0' separated, "\0\0" terminated) 1334 * -c: assume checksum protected environment format 1335 * addr: memory address to read from 1336 * size: length of input data; if missing, proper '\0' 1337 * termination is mandatory 1338 */ 1339 static int do_env_import(cmd_tbl_t *cmdtp, int flag, 1340 int argc, char * const argv[]) 1341 { 1342 ulong addr; 1343 char *cmd, *ptr; 1344 char sep = '\n'; 1345 int chk = 0; 1346 int fmt = 0; 1347 int del = 0; 1348 int crlf_is_lf = 0; 1349 size_t size; 1350 1351 cmd = *argv; 1352 1353 while (--argc > 0 && **++argv == '-') { 1354 char *arg = *argv; 1355 while (*++arg) { 1356 switch (*arg) { 1357 case 'b': /* raw binary format */ 1358 if (fmt++) 1359 goto sep_err; 1360 sep = '\0'; 1361 break; 1362 case 'c': /* external checksum format */ 1363 if (fmt++) 1364 goto sep_err; 1365 sep = '\0'; 1366 chk = 1; 1367 break; 1368 case 't': /* text format */ 1369 if (fmt++) 1370 goto sep_err; 1371 sep = '\n'; 1372 break; 1373 case 'r': /* handle CRLF like LF */ 1374 crlf_is_lf = 1; 1375 break; 1376 case 'd': 1377 del = 1; 1378 break; 1379 default: 1380 return CMD_RET_USAGE; 1381 } 1382 } 1383 } 1384 1385 if (argc < 1) 1386 return CMD_RET_USAGE; 1387 1388 if (!fmt) 1389 printf("## Warning: defaulting to text format\n"); 1390 1391 if (sep != '\n' && crlf_is_lf ) 1392 crlf_is_lf = 0; 1393 1394 addr = simple_strtoul(argv[0], NULL, 16); 1395 ptr = map_sysmem(addr, 0); 1396 1397 if (argc == 2) { 1398 size = simple_strtoul(argv[1], NULL, 16); 1399 } else if (argc == 1 && chk) { 1400 puts("## Error: external checksum format must pass size\n"); 1401 return CMD_RET_FAILURE; 1402 } else { 1403 char *s = ptr; 1404 1405 size = 0; 1406 1407 while (size < MAX_ENV_SIZE) { 1408 if ((*s == sep) && (*(s+1) == '\0')) 1409 break; 1410 ++s; 1411 ++size; 1412 } 1413 if (size == MAX_ENV_SIZE) { 1414 printf("## Warning: Input data exceeds %d bytes" 1415 " - truncated\n", MAX_ENV_SIZE); 1416 } 1417 size += 2; 1418 printf("## Info: input data size = %zu = 0x%zX\n", size, size); 1419 } 1420 1421 if (chk) { 1422 uint32_t crc; 1423 env_t *ep = (env_t *)ptr; 1424 1425 size -= offsetof(env_t, data); 1426 memcpy(&crc, &ep->crc, sizeof(crc)); 1427 1428 if (crc32(0, ep->data, size) != crc) { 1429 puts("## Error: bad CRC, import failed\n"); 1430 return 1; 1431 } 1432 ptr = (char *)ep->data; 1433 } 1434 1435 if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, 1436 crlf_is_lf, 0, NULL) == 0) { 1437 pr_err("Environment import failed: errno = %d\n", errno); 1438 return 1; 1439 } 1440 gd->flags |= GD_FLG_ENV_READY; 1441 1442 return 0; 1443 1444 sep_err: 1445 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", 1446 cmd); 1447 return 1; 1448 } 1449 #endif 1450 1451 #if defined(CONFIG_CMD_ENV_EXISTS) 1452 static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc, 1453 char * const argv[]) 1454 { 1455 ENTRY e, *ep; 1456 1457 if (argc < 2) 1458 return CMD_RET_USAGE; 1459 1460 e.key = argv[1]; 1461 e.data = NULL; 1462 hsearch_r(e, FIND, &ep, &env_htab, 0); 1463 1464 return (ep == NULL) ? 1 : 0; 1465 } 1466 #endif 1467 1468 /* 1469 * New command line interface: "env" command with subcommands 1470 */ 1471 static cmd_tbl_t cmd_env_sub[] = { 1472 #if defined(CONFIG_CMD_ASKENV) 1473 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), 1474 #endif 1475 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), 1476 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), 1477 U_BOOT_CMD_MKENT(update, 3, 0, do_env_update, "", ""), 1478 #if defined(CONFIG_CMD_EDITENV) 1479 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), 1480 #endif 1481 #if defined(CONFIG_CMD_ENV_CALLBACK) 1482 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), 1483 #endif 1484 #if defined(CONFIG_CMD_ENV_FLAGS) 1485 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), 1486 #endif 1487 #if defined(CONFIG_CMD_EXPORTENV) 1488 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), 1489 #endif 1490 #if defined(CONFIG_CMD_GREPENV) 1491 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), 1492 #endif 1493 #if defined(CONFIG_CMD_IMPORTENV) 1494 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), 1495 #endif 1496 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), 1497 #if defined(CONFIG_CMD_RUN) 1498 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), 1499 #endif 1500 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) 1501 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), 1502 #endif 1503 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), 1504 #if defined(CONFIG_CMD_ENV_EXISTS) 1505 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""), 1506 #endif 1507 }; 1508 1509 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 1510 void env_reloc(void) 1511 { 1512 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); 1513 } 1514 #endif 1515 1516 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 1517 { 1518 cmd_tbl_t *cp; 1519 1520 if (argc < 2) 1521 return CMD_RET_USAGE; 1522 1523 /* drop initial "env" arg */ 1524 argc--; 1525 argv++; 1526 1527 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); 1528 1529 if (cp) 1530 return cp->cmd(cmdtp, flag, argc, argv); 1531 1532 return CMD_RET_USAGE; 1533 } 1534 1535 #ifdef CONFIG_SYS_LONGHELP 1536 static char env_help_text[] = 1537 #if defined(CONFIG_CMD_ASKENV) 1538 "ask name [message] [size] - ask for environment variable\nenv " 1539 #endif 1540 #if defined(CONFIG_CMD_ENV_CALLBACK) 1541 "callbacks - print callbacks and their associated variables\nenv " 1542 #endif 1543 "default [-f] -a - [forcibly] reset default environment\n" 1544 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" 1545 "env delete [-f] var [...] - [forcibly] delete variable(s)\n" 1546 "env update [name] [value] - add/append/replace variable(s)\n" 1547 #if defined(CONFIG_CMD_EDITENV) 1548 "env edit name - edit environment variable\n" 1549 #endif 1550 #if defined(CONFIG_CMD_ENV_EXISTS) 1551 "env exists name - tests for existence of variable\n" 1552 #endif 1553 #if defined(CONFIG_CMD_EXPORTENV) 1554 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" 1555 #endif 1556 #if defined(CONFIG_CMD_ENV_FLAGS) 1557 "env flags - print variables that have non-default flags\n" 1558 #endif 1559 #if defined(CONFIG_CMD_GREPENV) 1560 #ifdef CONFIG_REGEX 1561 "env grep [-e] [-n | -v | -b] string [...] - search environment\n" 1562 #else 1563 "env grep [-n | -v | -b] string [...] - search environment\n" 1564 #endif 1565 #endif 1566 #if defined(CONFIG_CMD_IMPORTENV) 1567 "env import [-d] [-t [-r] | -b | -c] addr [size] - import environment\n" 1568 #endif 1569 "env print [-a | name ...] - print environment\n" 1570 #if defined(CONFIG_CMD_RUN) 1571 "env run var [...] - run commands in an environment variable\n" 1572 #endif 1573 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) 1574 "env save - save environment\n" 1575 #endif 1576 "env set [-f] name [arg ...]\n"; 1577 #endif 1578 1579 U_BOOT_CMD( 1580 env, CONFIG_SYS_MAXARGS, 1, do_env, 1581 "environment handling commands", env_help_text 1582 ); 1583 1584 /* 1585 * Old command line interface, kept for compatibility 1586 */ 1587 1588 #if defined(CONFIG_CMD_EDITENV) 1589 U_BOOT_CMD_COMPLETE( 1590 editenv, 2, 0, do_env_edit, 1591 "edit environment variable", 1592 "name\n" 1593 " - edit environment variable 'name'", 1594 var_complete 1595 ); 1596 #endif 1597 1598 U_BOOT_CMD_COMPLETE( 1599 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, 1600 "print environment variables", 1601 "[-a]\n - print [all] values of all environment variables\n" 1602 "printenv name ...\n" 1603 " - print value of environment variable 'name'", 1604 var_complete 1605 ); 1606 1607 #ifdef CONFIG_CMD_GREPENV 1608 U_BOOT_CMD_COMPLETE( 1609 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, 1610 "search environment variables", 1611 #ifdef CONFIG_REGEX 1612 "[-e] [-n | -v | -b] string ...\n" 1613 #else 1614 "[-n | -v | -b] string ...\n" 1615 #endif 1616 " - list environment name=value pairs matching 'string'\n" 1617 #ifdef CONFIG_REGEX 1618 " \"-e\": enable regular expressions;\n" 1619 #endif 1620 " \"-n\": search variable names; \"-v\": search values;\n" 1621 " \"-b\": search both names and values (default)", 1622 var_complete 1623 ); 1624 #endif 1625 1626 U_BOOT_CMD_COMPLETE( 1627 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, 1628 "set environment variables", 1629 "[-f] name value ...\n" 1630 " - [forcibly] set environment variable 'name' to 'value ...'\n" 1631 "setenv [-f] name\n" 1632 " - [forcibly] delete environment variable 'name'", 1633 var_complete 1634 ); 1635 1636 #if defined(CONFIG_CMD_ASKENV) 1637 1638 U_BOOT_CMD( 1639 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, 1640 "get environment variables from stdin", 1641 "name [message] [size]\n" 1642 " - get environment variable 'name' from stdin (max 'size' chars)" 1643 ); 1644 #endif 1645 1646 #if defined(CONFIG_CMD_RUN) 1647 U_BOOT_CMD_COMPLETE( 1648 run, CONFIG_SYS_MAXARGS, 1, do_run, 1649 "run commands in an environment variable", 1650 "var [...]\n" 1651 " - run the commands in the environment variable(s) 'var'", 1652 var_complete 1653 ); 1654 #endif 1655 #endif /* CONFIG_SPL_BUILD */ 1656