1 /* 2 * (C) Copyright 2000 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * Add to readline cmdline-editing by 6 * (C) Copyright 2005 7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> 8 * 9 * See file CREDITS for list of people who contributed to this 10 * project. 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License as 14 * published by the Free Software Foundation; either version 2 of 15 * the License, or (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 25 * MA 02111-1307 USA 26 */ 27 28 /* #define DEBUG */ 29 30 #include <common.h> 31 #include <watchdog.h> 32 #include <command.h> 33 #include <version.h> 34 #ifdef CONFIG_MODEM_SUPPORT 35 #include <malloc.h> /* for free() prototype */ 36 #endif 37 38 #ifdef CONFIG_SYS_HUSH_PARSER 39 #include <hush.h> 40 #endif 41 42 #include <post.h> 43 #include <linux/ctype.h> 44 #include <menu.h> 45 46 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING) 47 DECLARE_GLOBAL_DATA_PTR; 48 #endif 49 50 /* 51 * Board-specific Platform code can reimplement show_boot_progress () if needed 52 */ 53 void inline __show_boot_progress (int val) {} 54 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress"))); 55 56 #if defined(CONFIG_UPDATE_TFTP) 57 int update_tftp (ulong addr); 58 #endif /* CONFIG_UPDATE_TFTP */ 59 60 #define MAX_DELAY_STOP_STR 32 61 62 #undef DEBUG_PARSER 63 64 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ 65 66 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen); 67 static const char erase_seq[] = "\b \b"; /* erase sequence */ 68 static const char tab_seq[] = " "; /* used to expand TABs */ 69 70 #ifdef CONFIG_BOOT_RETRY_TIME 71 static uint64_t endtime = 0; /* must be set, default is instant timeout */ 72 static int retry_time = -1; /* -1 so can call readline before main_loop */ 73 #endif 74 75 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) 76 77 #ifndef CONFIG_BOOT_RETRY_MIN 78 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME 79 #endif 80 81 #ifdef CONFIG_MODEM_SUPPORT 82 int do_mdm_init = 0; 83 extern void mdm_init(void); /* defined in board.c */ 84 #endif 85 86 /*************************************************************************** 87 * Watch for 'delay' seconds for autoboot stop or autoboot delay string. 88 * returns: 0 - no key string, allow autoboot 1 - got key string, abort 89 */ 90 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) 91 # if defined(CONFIG_AUTOBOOT_KEYED) 92 #ifndef CONFIG_MENU 93 static inline 94 #endif 95 int abortboot(int bootdelay) 96 { 97 int abort = 0; 98 uint64_t etime = endtick(bootdelay); 99 struct { 100 char* str; 101 u_int len; 102 int retry; 103 } 104 delaykey [] = { 105 { str: getenv ("bootdelaykey"), retry: 1 }, 106 { str: getenv ("bootdelaykey2"), retry: 1 }, 107 { str: getenv ("bootstopkey"), retry: 0 }, 108 { str: getenv ("bootstopkey2"), retry: 0 }, 109 }; 110 111 char presskey [MAX_DELAY_STOP_STR]; 112 u_int presskey_len = 0; 113 u_int presskey_max = 0; 114 u_int i; 115 116 # ifdef CONFIG_AUTOBOOT_PROMPT 117 printf(CONFIG_AUTOBOOT_PROMPT); 118 # endif 119 120 # ifdef CONFIG_AUTOBOOT_DELAY_STR 121 if (delaykey[0].str == NULL) 122 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; 123 # endif 124 # ifdef CONFIG_AUTOBOOT_DELAY_STR2 125 if (delaykey[1].str == NULL) 126 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2; 127 # endif 128 # ifdef CONFIG_AUTOBOOT_STOP_STR 129 if (delaykey[2].str == NULL) 130 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR; 131 # endif 132 # ifdef CONFIG_AUTOBOOT_STOP_STR2 133 if (delaykey[3].str == NULL) 134 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2; 135 # endif 136 137 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { 138 delaykey[i].len = delaykey[i].str == NULL ? 139 0 : strlen (delaykey[i].str); 140 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ? 141 MAX_DELAY_STOP_STR : delaykey[i].len; 142 143 presskey_max = presskey_max > delaykey[i].len ? 144 presskey_max : delaykey[i].len; 145 146 # if DEBUG_BOOTKEYS 147 printf("%s key:<%s>\n", 148 delaykey[i].retry ? "delay" : "stop", 149 delaykey[i].str ? delaykey[i].str : "NULL"); 150 # endif 151 } 152 153 /* In order to keep up with incoming data, check timeout only 154 * when catch up. 155 */ 156 do { 157 if (tstc()) { 158 if (presskey_len < presskey_max) { 159 presskey [presskey_len ++] = getc(); 160 } 161 else { 162 for (i = 0; i < presskey_max - 1; i ++) 163 presskey [i] = presskey [i + 1]; 164 165 presskey [i] = getc(); 166 } 167 } 168 169 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { 170 if (delaykey[i].len > 0 && 171 presskey_len >= delaykey[i].len && 172 memcmp (presskey + presskey_len - delaykey[i].len, 173 delaykey[i].str, 174 delaykey[i].len) == 0) { 175 # if DEBUG_BOOTKEYS 176 printf("got %skey\n", 177 delaykey[i].retry ? "delay" : "stop"); 178 # endif 179 180 # ifdef CONFIG_BOOT_RETRY_TIME 181 /* don't retry auto boot */ 182 if (! delaykey[i].retry) 183 retry_time = -1; 184 # endif 185 abort = 1; 186 } 187 } 188 } while (!abort && get_ticks() <= etime); 189 190 # if DEBUG_BOOTKEYS 191 if (!abort) 192 puts("key timeout\n"); 193 # endif 194 195 #ifdef CONFIG_SILENT_CONSOLE 196 if (abort) 197 gd->flags &= ~GD_FLG_SILENT; 198 #endif 199 200 return abort; 201 } 202 203 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */ 204 205 #ifdef CONFIG_MENUKEY 206 static int menukey = 0; 207 #endif 208 209 #ifndef CONFIG_MENU 210 static inline 211 #endif 212 int abortboot(int bootdelay) 213 { 214 int abort = 0; 215 216 #ifdef CONFIG_MENUPROMPT 217 printf(CONFIG_MENUPROMPT); 218 #else 219 printf("Hit any key to stop autoboot: %2d ", bootdelay); 220 #endif 221 222 #if defined CONFIG_ZERO_BOOTDELAY_CHECK 223 /* 224 * Check if key already pressed 225 * Don't check if bootdelay < 0 226 */ 227 if (bootdelay >= 0) { 228 if (tstc()) { /* we got a key press */ 229 (void) getc(); /* consume input */ 230 puts ("\b\b\b 0"); 231 abort = 1; /* don't auto boot */ 232 } 233 } 234 #endif 235 236 while ((bootdelay > 0) && (!abort)) { 237 int i; 238 239 --bootdelay; 240 /* delay 100 * 10ms */ 241 for (i=0; !abort && i<100; ++i) { 242 if (tstc()) { /* we got a key press */ 243 abort = 1; /* don't auto boot */ 244 bootdelay = 0; /* no more delay */ 245 # ifdef CONFIG_MENUKEY 246 menukey = getc(); 247 # else 248 (void) getc(); /* consume input */ 249 # endif 250 break; 251 } 252 udelay(10000); 253 } 254 255 printf("\b\b\b%2d ", bootdelay); 256 } 257 258 putc('\n'); 259 260 #ifdef CONFIG_SILENT_CONSOLE 261 if (abort) 262 gd->flags &= ~GD_FLG_SILENT; 263 #endif 264 265 return abort; 266 } 267 # endif /* CONFIG_AUTOBOOT_KEYED */ 268 #endif /* CONFIG_BOOTDELAY >= 0 */ 269 270 /* 271 * Return 0 on success, or != 0 on error. 272 */ 273 #ifndef CONFIG_CMD_PXE 274 static inline 275 #endif 276 int run_command2(const char *cmd, int flag) 277 { 278 #ifndef CONFIG_SYS_HUSH_PARSER 279 /* 280 * run_command can return 0 or 1 for success, so clean up its result. 281 */ 282 if (run_command(cmd, flag) == -1) 283 return 1; 284 285 return 0; 286 #else 287 return parse_string_outer(cmd, 288 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP); 289 #endif 290 } 291 292 /****************************************************************************/ 293 294 void main_loop (void) 295 { 296 #ifndef CONFIG_SYS_HUSH_PARSER 297 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, }; 298 int len; 299 int rc = 1; 300 int flag; 301 #endif 302 303 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) 304 char *s; 305 int bootdelay; 306 #endif 307 #ifdef CONFIG_PREBOOT 308 char *p; 309 #endif 310 #ifdef CONFIG_BOOTCOUNT_LIMIT 311 unsigned long bootcount = 0; 312 unsigned long bootlimit = 0; 313 char *bcs; 314 char bcs_set[16]; 315 #endif /* CONFIG_BOOTCOUNT_LIMIT */ 316 317 #ifdef CONFIG_BOOTCOUNT_LIMIT 318 bootcount = bootcount_load(); 319 bootcount++; 320 bootcount_store (bootcount); 321 sprintf (bcs_set, "%lu", bootcount); 322 setenv ("bootcount", bcs_set); 323 bcs = getenv ("bootlimit"); 324 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0; 325 #endif /* CONFIG_BOOTCOUNT_LIMIT */ 326 327 #ifdef CONFIG_MODEM_SUPPORT 328 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init); 329 if (do_mdm_init) { 330 char *str = strdup(getenv("mdm_cmd")); 331 setenv ("preboot", str); /* set or delete definition */ 332 if (str != NULL) 333 free (str); 334 mdm_init(); /* wait for modem connection */ 335 } 336 #endif /* CONFIG_MODEM_SUPPORT */ 337 338 #ifdef CONFIG_VERSION_VARIABLE 339 { 340 setenv ("ver", version_string); /* set version variable */ 341 } 342 #endif /* CONFIG_VERSION_VARIABLE */ 343 344 #ifdef CONFIG_SYS_HUSH_PARSER 345 u_boot_hush_start (); 346 #endif 347 348 #if defined(CONFIG_HUSH_INIT_VAR) 349 hush_init_var (); 350 #endif 351 352 #ifdef CONFIG_PREBOOT 353 if ((p = getenv ("preboot")) != NULL) { 354 # ifdef CONFIG_AUTOBOOT_KEYED 355 int prev = disable_ctrlc(1); /* disable Control C checking */ 356 # endif 357 358 run_command2(p, 0); 359 360 # ifdef CONFIG_AUTOBOOT_KEYED 361 disable_ctrlc(prev); /* restore Control C checking */ 362 # endif 363 } 364 #endif /* CONFIG_PREBOOT */ 365 366 #if defined(CONFIG_UPDATE_TFTP) 367 update_tftp (0UL); 368 #endif /* CONFIG_UPDATE_TFTP */ 369 370 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) 371 s = getenv ("bootdelay"); 372 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; 373 374 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay); 375 376 #if defined(CONFIG_MENU_SHOW) 377 bootdelay = menu_show(bootdelay); 378 #endif 379 # ifdef CONFIG_BOOT_RETRY_TIME 380 init_cmd_timeout (); 381 # endif /* CONFIG_BOOT_RETRY_TIME */ 382 383 #ifdef CONFIG_POST 384 if (gd->flags & GD_FLG_POSTFAIL) { 385 s = getenv("failbootcmd"); 386 } 387 else 388 #endif /* CONFIG_POST */ 389 #ifdef CONFIG_BOOTCOUNT_LIMIT 390 if (bootlimit && (bootcount > bootlimit)) { 391 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n", 392 (unsigned)bootlimit); 393 s = getenv ("altbootcmd"); 394 } 395 else 396 #endif /* CONFIG_BOOTCOUNT_LIMIT */ 397 s = getenv ("bootcmd"); 398 399 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); 400 401 if (bootdelay >= 0 && s && !abortboot (bootdelay)) { 402 # ifdef CONFIG_AUTOBOOT_KEYED 403 int prev = disable_ctrlc(1); /* disable Control C checking */ 404 # endif 405 406 run_command2(s, 0); 407 408 # ifdef CONFIG_AUTOBOOT_KEYED 409 disable_ctrlc(prev); /* restore Control C checking */ 410 # endif 411 } 412 413 # ifdef CONFIG_MENUKEY 414 if (menukey == CONFIG_MENUKEY) { 415 s = getenv("menucmd"); 416 if (s) 417 run_command2(s, 0); 418 } 419 #endif /* CONFIG_MENUKEY */ 420 #endif /* CONFIG_BOOTDELAY */ 421 422 /* 423 * Main Loop for Monitor Command Processing 424 */ 425 #ifdef CONFIG_SYS_HUSH_PARSER 426 parse_file_outer(); 427 /* This point is never reached */ 428 for (;;); 429 #else 430 for (;;) { 431 #ifdef CONFIG_BOOT_RETRY_TIME 432 if (rc >= 0) { 433 /* Saw enough of a valid command to 434 * restart the timeout. 435 */ 436 reset_cmd_timeout(); 437 } 438 #endif 439 len = readline (CONFIG_SYS_PROMPT); 440 441 flag = 0; /* assume no special flags for now */ 442 if (len > 0) 443 strcpy (lastcommand, console_buffer); 444 else if (len == 0) 445 flag |= CMD_FLAG_REPEAT; 446 #ifdef CONFIG_BOOT_RETRY_TIME 447 else if (len == -2) { 448 /* -2 means timed out, retry autoboot 449 */ 450 puts ("\nTimed out waiting for command\n"); 451 # ifdef CONFIG_RESET_TO_RETRY 452 /* Reinit board to run initialization code again */ 453 do_reset (NULL, 0, 0, NULL); 454 # else 455 return; /* retry autoboot */ 456 # endif 457 } 458 #endif 459 460 if (len == -1) 461 puts ("<INTERRUPT>\n"); 462 else 463 rc = run_command (lastcommand, flag); 464 465 if (rc <= 0) { 466 /* invalid command or not repeatable, forget it */ 467 lastcommand[0] = 0; 468 } 469 } 470 #endif /*CONFIG_SYS_HUSH_PARSER*/ 471 } 472 473 #ifdef CONFIG_BOOT_RETRY_TIME 474 /*************************************************************************** 475 * initialize command line timeout 476 */ 477 void init_cmd_timeout(void) 478 { 479 char *s = getenv ("bootretry"); 480 481 if (s != NULL) 482 retry_time = (int)simple_strtol(s, NULL, 10); 483 else 484 retry_time = CONFIG_BOOT_RETRY_TIME; 485 486 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) 487 retry_time = CONFIG_BOOT_RETRY_MIN; 488 } 489 490 /*************************************************************************** 491 * reset command line timeout to retry_time seconds 492 */ 493 void reset_cmd_timeout(void) 494 { 495 endtime = endtick(retry_time); 496 } 497 #endif 498 499 #ifdef CONFIG_CMDLINE_EDITING 500 501 /* 502 * cmdline-editing related codes from vivi. 503 * Author: Janghoon Lyu <nandy@mizi.com> 504 */ 505 506 #define putnstr(str,n) do { \ 507 printf ("%.*s", (int)n, str); \ 508 } while (0) 509 510 #define CTL_CH(c) ((c) - 'a' + 1) 511 #define CTL_BACKSPACE ('\b') 512 #define DEL ((char)255) 513 #define DEL7 ((char)127) 514 #define CREAD_HIST_CHAR ('!') 515 516 #define getcmd_putch(ch) putc(ch) 517 #define getcmd_getch() getc() 518 #define getcmd_cbeep() getcmd_putch('\a') 519 520 #define HIST_MAX 20 521 #define HIST_SIZE CONFIG_SYS_CBSIZE 522 523 static int hist_max = 0; 524 static int hist_add_idx = 0; 525 static int hist_cur = -1; 526 unsigned hist_num = 0; 527 528 char* hist_list[HIST_MAX]; 529 char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ 530 531 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) 532 533 static void hist_init(void) 534 { 535 int i; 536 537 hist_max = 0; 538 hist_add_idx = 0; 539 hist_cur = -1; 540 hist_num = 0; 541 542 for (i = 0; i < HIST_MAX; i++) { 543 hist_list[i] = hist_lines[i]; 544 hist_list[i][0] = '\0'; 545 } 546 } 547 548 static void cread_add_to_hist(char *line) 549 { 550 strcpy(hist_list[hist_add_idx], line); 551 552 if (++hist_add_idx >= HIST_MAX) 553 hist_add_idx = 0; 554 555 if (hist_add_idx > hist_max) 556 hist_max = hist_add_idx; 557 558 hist_num++; 559 } 560 561 static char* hist_prev(void) 562 { 563 char *ret; 564 int old_cur; 565 566 if (hist_cur < 0) 567 return NULL; 568 569 old_cur = hist_cur; 570 if (--hist_cur < 0) 571 hist_cur = hist_max; 572 573 if (hist_cur == hist_add_idx) { 574 hist_cur = old_cur; 575 ret = NULL; 576 } else 577 ret = hist_list[hist_cur]; 578 579 return (ret); 580 } 581 582 static char* hist_next(void) 583 { 584 char *ret; 585 586 if (hist_cur < 0) 587 return NULL; 588 589 if (hist_cur == hist_add_idx) 590 return NULL; 591 592 if (++hist_cur > hist_max) 593 hist_cur = 0; 594 595 if (hist_cur == hist_add_idx) { 596 ret = ""; 597 } else 598 ret = hist_list[hist_cur]; 599 600 return (ret); 601 } 602 603 #ifndef CONFIG_CMDLINE_EDITING 604 static void cread_print_hist_list(void) 605 { 606 int i; 607 unsigned long n; 608 609 n = hist_num - hist_max; 610 611 i = hist_add_idx + 1; 612 while (1) { 613 if (i > hist_max) 614 i = 0; 615 if (i == hist_add_idx) 616 break; 617 printf("%s\n", hist_list[i]); 618 n++; 619 i++; 620 } 621 } 622 #endif /* CONFIG_CMDLINE_EDITING */ 623 624 #define BEGINNING_OF_LINE() { \ 625 while (num) { \ 626 getcmd_putch(CTL_BACKSPACE); \ 627 num--; \ 628 } \ 629 } 630 631 #define ERASE_TO_EOL() { \ 632 if (num < eol_num) { \ 633 printf("%*s", (int)(eol_num - num), ""); \ 634 do { \ 635 getcmd_putch(CTL_BACKSPACE); \ 636 } while (--eol_num > num); \ 637 } \ 638 } 639 640 #define REFRESH_TO_EOL() { \ 641 if (num < eol_num) { \ 642 wlen = eol_num - num; \ 643 putnstr(buf + num, wlen); \ 644 num = eol_num; \ 645 } \ 646 } 647 648 static void cread_add_char(char ichar, int insert, unsigned long *num, 649 unsigned long *eol_num, char *buf, unsigned long len) 650 { 651 unsigned long wlen; 652 653 /* room ??? */ 654 if (insert || *num == *eol_num) { 655 if (*eol_num > len - 1) { 656 getcmd_cbeep(); 657 return; 658 } 659 (*eol_num)++; 660 } 661 662 if (insert) { 663 wlen = *eol_num - *num; 664 if (wlen > 1) { 665 memmove(&buf[*num+1], &buf[*num], wlen-1); 666 } 667 668 buf[*num] = ichar; 669 putnstr(buf + *num, wlen); 670 (*num)++; 671 while (--wlen) { 672 getcmd_putch(CTL_BACKSPACE); 673 } 674 } else { 675 /* echo the character */ 676 wlen = 1; 677 buf[*num] = ichar; 678 putnstr(buf + *num, wlen); 679 (*num)++; 680 } 681 } 682 683 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num, 684 unsigned long *eol_num, char *buf, unsigned long len) 685 { 686 while (strsize--) { 687 cread_add_char(*str, insert, num, eol_num, buf, len); 688 str++; 689 } 690 } 691 692 static int cread_line(const char *const prompt, char *buf, unsigned int *len, 693 int timeout) 694 { 695 unsigned long num = 0; 696 unsigned long eol_num = 0; 697 unsigned long wlen; 698 char ichar; 699 int insert = 1; 700 int esc_len = 0; 701 char esc_save[8]; 702 int init_len = strlen(buf); 703 int first = 1; 704 705 if (init_len) 706 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); 707 708 while (1) { 709 #ifdef CONFIG_BOOT_RETRY_TIME 710 while (!tstc()) { /* while no incoming data */ 711 if (retry_time >= 0 && get_ticks() > endtime) 712 return (-2); /* timed out */ 713 WATCHDOG_RESET(); 714 } 715 #endif 716 if (first && timeout) { 717 uint64_t etime = endtick(timeout); 718 719 while (!tstc()) { /* while no incoming data */ 720 if (get_ticks() >= etime) 721 return -2; /* timed out */ 722 WATCHDOG_RESET(); 723 } 724 first = 0; 725 } 726 727 ichar = getcmd_getch(); 728 729 if ((ichar == '\n') || (ichar == '\r')) { 730 putc('\n'); 731 break; 732 } 733 734 /* 735 * handle standard linux xterm esc sequences for arrow key, etc. 736 */ 737 if (esc_len != 0) { 738 if (esc_len == 1) { 739 if (ichar == '[') { 740 esc_save[esc_len] = ichar; 741 esc_len = 2; 742 } else { 743 cread_add_str(esc_save, esc_len, insert, 744 &num, &eol_num, buf, *len); 745 esc_len = 0; 746 } 747 continue; 748 } 749 750 switch (ichar) { 751 752 case 'D': /* <- key */ 753 ichar = CTL_CH('b'); 754 esc_len = 0; 755 break; 756 case 'C': /* -> key */ 757 ichar = CTL_CH('f'); 758 esc_len = 0; 759 break; /* pass off to ^F handler */ 760 case 'H': /* Home key */ 761 ichar = CTL_CH('a'); 762 esc_len = 0; 763 break; /* pass off to ^A handler */ 764 case 'A': /* up arrow */ 765 ichar = CTL_CH('p'); 766 esc_len = 0; 767 break; /* pass off to ^P handler */ 768 case 'B': /* down arrow */ 769 ichar = CTL_CH('n'); 770 esc_len = 0; 771 break; /* pass off to ^N handler */ 772 default: 773 esc_save[esc_len++] = ichar; 774 cread_add_str(esc_save, esc_len, insert, 775 &num, &eol_num, buf, *len); 776 esc_len = 0; 777 continue; 778 } 779 } 780 781 switch (ichar) { 782 case 0x1b: 783 if (esc_len == 0) { 784 esc_save[esc_len] = ichar; 785 esc_len = 1; 786 } else { 787 puts("impossible condition #876\n"); 788 esc_len = 0; 789 } 790 break; 791 792 case CTL_CH('a'): 793 BEGINNING_OF_LINE(); 794 break; 795 case CTL_CH('c'): /* ^C - break */ 796 *buf = '\0'; /* discard input */ 797 return (-1); 798 case CTL_CH('f'): 799 if (num < eol_num) { 800 getcmd_putch(buf[num]); 801 num++; 802 } 803 break; 804 case CTL_CH('b'): 805 if (num) { 806 getcmd_putch(CTL_BACKSPACE); 807 num--; 808 } 809 break; 810 case CTL_CH('d'): 811 if (num < eol_num) { 812 wlen = eol_num - num - 1; 813 if (wlen) { 814 memmove(&buf[num], &buf[num+1], wlen); 815 putnstr(buf + num, wlen); 816 } 817 818 getcmd_putch(' '); 819 do { 820 getcmd_putch(CTL_BACKSPACE); 821 } while (wlen--); 822 eol_num--; 823 } 824 break; 825 case CTL_CH('k'): 826 ERASE_TO_EOL(); 827 break; 828 case CTL_CH('e'): 829 REFRESH_TO_EOL(); 830 break; 831 case CTL_CH('o'): 832 insert = !insert; 833 break; 834 case CTL_CH('x'): 835 case CTL_CH('u'): 836 BEGINNING_OF_LINE(); 837 ERASE_TO_EOL(); 838 break; 839 case DEL: 840 case DEL7: 841 case 8: 842 if (num) { 843 wlen = eol_num - num; 844 num--; 845 memmove(&buf[num], &buf[num+1], wlen); 846 getcmd_putch(CTL_BACKSPACE); 847 putnstr(buf + num, wlen); 848 getcmd_putch(' '); 849 do { 850 getcmd_putch(CTL_BACKSPACE); 851 } while (wlen--); 852 eol_num--; 853 } 854 break; 855 case CTL_CH('p'): 856 case CTL_CH('n'): 857 { 858 char * hline; 859 860 esc_len = 0; 861 862 if (ichar == CTL_CH('p')) 863 hline = hist_prev(); 864 else 865 hline = hist_next(); 866 867 if (!hline) { 868 getcmd_cbeep(); 869 continue; 870 } 871 872 /* nuke the current line */ 873 /* first, go home */ 874 BEGINNING_OF_LINE(); 875 876 /* erase to end of line */ 877 ERASE_TO_EOL(); 878 879 /* copy new line into place and display */ 880 strcpy(buf, hline); 881 eol_num = strlen(buf); 882 REFRESH_TO_EOL(); 883 continue; 884 } 885 #ifdef CONFIG_AUTO_COMPLETE 886 case '\t': { 887 int num2, col; 888 889 /* do not autocomplete when in the middle */ 890 if (num < eol_num) { 891 getcmd_cbeep(); 892 break; 893 } 894 895 buf[num] = '\0'; 896 col = strlen(prompt) + eol_num; 897 num2 = num; 898 if (cmd_auto_complete(prompt, buf, &num2, &col)) { 899 col = num2 - num; 900 num += col; 901 eol_num += col; 902 } 903 break; 904 } 905 #endif 906 default: 907 cread_add_char(ichar, insert, &num, &eol_num, buf, *len); 908 break; 909 } 910 } 911 *len = eol_num; 912 buf[eol_num] = '\0'; /* lose the newline */ 913 914 if (buf[0] && buf[0] != CREAD_HIST_CHAR) 915 cread_add_to_hist(buf); 916 hist_cur = hist_add_idx; 917 918 return 0; 919 } 920 921 #endif /* CONFIG_CMDLINE_EDITING */ 922 923 /****************************************************************************/ 924 925 /* 926 * Prompt for input and read a line. 927 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, 928 * time out when time goes past endtime (timebase time in ticks). 929 * Return: number of read characters 930 * -1 if break 931 * -2 if timed out 932 */ 933 int readline (const char *const prompt) 934 { 935 /* 936 * If console_buffer isn't 0-length the user will be prompted to modify 937 * it instead of entering it from scratch as desired. 938 */ 939 console_buffer[0] = '\0'; 940 941 return readline_into_buffer(prompt, console_buffer, 0); 942 } 943 944 945 int readline_into_buffer(const char *const prompt, char *buffer, int timeout) 946 { 947 char *p = buffer; 948 #ifdef CONFIG_CMDLINE_EDITING 949 unsigned int len = CONFIG_SYS_CBSIZE; 950 int rc; 951 static int initted = 0; 952 953 /* 954 * History uses a global array which is not 955 * writable until after relocation to RAM. 956 * Revert to non-history version if still 957 * running from flash. 958 */ 959 if (gd->flags & GD_FLG_RELOC) { 960 if (!initted) { 961 hist_init(); 962 initted = 1; 963 } 964 965 if (prompt) 966 puts (prompt); 967 968 rc = cread_line(prompt, p, &len, timeout); 969 return rc < 0 ? rc : len; 970 971 } else { 972 #endif /* CONFIG_CMDLINE_EDITING */ 973 char * p_buf = p; 974 int n = 0; /* buffer index */ 975 int plen = 0; /* prompt length */ 976 int col; /* output column cnt */ 977 char c; 978 979 /* print prompt */ 980 if (prompt) { 981 plen = strlen (prompt); 982 puts (prompt); 983 } 984 col = plen; 985 986 for (;;) { 987 #ifdef CONFIG_BOOT_RETRY_TIME 988 while (!tstc()) { /* while no incoming data */ 989 if (retry_time >= 0 && get_ticks() > endtime) 990 return (-2); /* timed out */ 991 WATCHDOG_RESET(); 992 } 993 #endif 994 WATCHDOG_RESET(); /* Trigger watchdog, if needed */ 995 996 #ifdef CONFIG_SHOW_ACTIVITY 997 while (!tstc()) { 998 extern void show_activity(int arg); 999 show_activity(0); 1000 WATCHDOG_RESET(); 1001 } 1002 #endif 1003 c = getc(); 1004 1005 /* 1006 * Special character handling 1007 */ 1008 switch (c) { 1009 case '\r': /* Enter */ 1010 case '\n': 1011 *p = '\0'; 1012 puts ("\r\n"); 1013 return (p - p_buf); 1014 1015 case '\0': /* nul */ 1016 continue; 1017 1018 case 0x03: /* ^C - break */ 1019 p_buf[0] = '\0'; /* discard input */ 1020 return (-1); 1021 1022 case 0x15: /* ^U - erase line */ 1023 while (col > plen) { 1024 puts (erase_seq); 1025 --col; 1026 } 1027 p = p_buf; 1028 n = 0; 1029 continue; 1030 1031 case 0x17: /* ^W - erase word */ 1032 p=delete_char(p_buf, p, &col, &n, plen); 1033 while ((n > 0) && (*p != ' ')) { 1034 p=delete_char(p_buf, p, &col, &n, plen); 1035 } 1036 continue; 1037 1038 case 0x08: /* ^H - backspace */ 1039 case 0x7F: /* DEL - backspace */ 1040 p=delete_char(p_buf, p, &col, &n, plen); 1041 continue; 1042 1043 default: 1044 /* 1045 * Must be a normal character then 1046 */ 1047 if (n < CONFIG_SYS_CBSIZE-2) { 1048 if (c == '\t') { /* expand TABs */ 1049 #ifdef CONFIG_AUTO_COMPLETE 1050 /* if auto completion triggered just continue */ 1051 *p = '\0'; 1052 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { 1053 p = p_buf + n; /* reset */ 1054 continue; 1055 } 1056 #endif 1057 puts (tab_seq+(col&07)); 1058 col += 8 - (col&07); 1059 } else { 1060 ++col; /* echo input */ 1061 putc (c); 1062 } 1063 *p++ = c; 1064 ++n; 1065 } else { /* Buffer full */ 1066 putc ('\a'); 1067 } 1068 } 1069 } 1070 #ifdef CONFIG_CMDLINE_EDITING 1071 } 1072 #endif 1073 } 1074 1075 /****************************************************************************/ 1076 1077 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen) 1078 { 1079 char *s; 1080 1081 if (*np == 0) { 1082 return (p); 1083 } 1084 1085 if (*(--p) == '\t') { /* will retype the whole line */ 1086 while (*colp > plen) { 1087 puts (erase_seq); 1088 (*colp)--; 1089 } 1090 for (s=buffer; s<p; ++s) { 1091 if (*s == '\t') { 1092 puts (tab_seq+((*colp) & 07)); 1093 *colp += 8 - ((*colp) & 07); 1094 } else { 1095 ++(*colp); 1096 putc (*s); 1097 } 1098 } 1099 } else { 1100 puts (erase_seq); 1101 (*colp)--; 1102 } 1103 (*np)--; 1104 return (p); 1105 } 1106 1107 /****************************************************************************/ 1108 1109 int parse_line (char *line, char *argv[]) 1110 { 1111 int nargs = 0; 1112 1113 #ifdef DEBUG_PARSER 1114 printf ("parse_line: \"%s\"\n", line); 1115 #endif 1116 while (nargs < CONFIG_SYS_MAXARGS) { 1117 1118 /* skip any white space */ 1119 while (isblank(*line)) 1120 ++line; 1121 1122 if (*line == '\0') { /* end of line, no more args */ 1123 argv[nargs] = NULL; 1124 #ifdef DEBUG_PARSER 1125 printf ("parse_line: nargs=%d\n", nargs); 1126 #endif 1127 return (nargs); 1128 } 1129 1130 argv[nargs++] = line; /* begin of argument string */ 1131 1132 /* find end of string */ 1133 while (*line && !isblank(*line)) 1134 ++line; 1135 1136 if (*line == '\0') { /* end of line, no more args */ 1137 argv[nargs] = NULL; 1138 #ifdef DEBUG_PARSER 1139 printf ("parse_line: nargs=%d\n", nargs); 1140 #endif 1141 return (nargs); 1142 } 1143 1144 *line++ = '\0'; /* terminate current arg */ 1145 } 1146 1147 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS); 1148 1149 #ifdef DEBUG_PARSER 1150 printf ("parse_line: nargs=%d\n", nargs); 1151 #endif 1152 return (nargs); 1153 } 1154 1155 /****************************************************************************/ 1156 1157 static void process_macros (const char *input, char *output) 1158 { 1159 char c, prev; 1160 const char *varname_start = NULL; 1161 int inputcnt = strlen (input); 1162 int outputcnt = CONFIG_SYS_CBSIZE; 1163 int state = 0; /* 0 = waiting for '$' */ 1164 1165 /* 1 = waiting for '(' or '{' */ 1166 /* 2 = waiting for ')' or '}' */ 1167 /* 3 = waiting for ''' */ 1168 #ifdef DEBUG_PARSER 1169 char *output_start = output; 1170 1171 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input), 1172 input); 1173 #endif 1174 1175 prev = '\0'; /* previous character */ 1176 1177 while (inputcnt && outputcnt) { 1178 c = *input++; 1179 inputcnt--; 1180 1181 if (state != 3) { 1182 /* remove one level of escape characters */ 1183 if ((c == '\\') && (prev != '\\')) { 1184 if (inputcnt-- == 0) 1185 break; 1186 prev = c; 1187 c = *input++; 1188 } 1189 } 1190 1191 switch (state) { 1192 case 0: /* Waiting for (unescaped) $ */ 1193 if ((c == '\'') && (prev != '\\')) { 1194 state = 3; 1195 break; 1196 } 1197 if ((c == '$') && (prev != '\\')) { 1198 state++; 1199 } else { 1200 *(output++) = c; 1201 outputcnt--; 1202 } 1203 break; 1204 case 1: /* Waiting for ( */ 1205 if (c == '(' || c == '{') { 1206 state++; 1207 varname_start = input; 1208 } else { 1209 state = 0; 1210 *(output++) = '$'; 1211 outputcnt--; 1212 1213 if (outputcnt) { 1214 *(output++) = c; 1215 outputcnt--; 1216 } 1217 } 1218 break; 1219 case 2: /* Waiting for ) */ 1220 if (c == ')' || c == '}') { 1221 int i; 1222 char envname[CONFIG_SYS_CBSIZE], *envval; 1223 int envcnt = input - varname_start - 1; /* Varname # of chars */ 1224 1225 /* Get the varname */ 1226 for (i = 0; i < envcnt; i++) { 1227 envname[i] = varname_start[i]; 1228 } 1229 envname[i] = 0; 1230 1231 /* Get its value */ 1232 envval = getenv (envname); 1233 1234 /* Copy into the line if it exists */ 1235 if (envval != NULL) 1236 while ((*envval) && outputcnt) { 1237 *(output++) = *(envval++); 1238 outputcnt--; 1239 } 1240 /* Look for another '$' */ 1241 state = 0; 1242 } 1243 break; 1244 case 3: /* Waiting for ' */ 1245 if ((c == '\'') && (prev != '\\')) { 1246 state = 0; 1247 } else { 1248 *(output++) = c; 1249 outputcnt--; 1250 } 1251 break; 1252 } 1253 prev = c; 1254 } 1255 1256 if (outputcnt) 1257 *output = 0; 1258 else 1259 *(output - 1) = 0; 1260 1261 #ifdef DEBUG_PARSER 1262 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", 1263 strlen (output_start), output_start); 1264 #endif 1265 } 1266 1267 /**************************************************************************** 1268 * returns: 1269 * 1 - command executed, repeatable 1270 * 0 - command executed but not repeatable, interrupted commands are 1271 * always considered not repeatable 1272 * -1 - not executed (unrecognized, bootd recursion or too many args) 1273 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is 1274 * considered unrecognized) 1275 * 1276 * WARNING: 1277 * 1278 * We must create a temporary copy of the command since the command we get 1279 * may be the result from getenv(), which returns a pointer directly to 1280 * the environment data, which may change magicly when the command we run 1281 * creates or modifies environment variables (like "bootp" does). 1282 */ 1283 1284 int run_command (const char *cmd, int flag) 1285 { 1286 cmd_tbl_t *cmdtp; 1287 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */ 1288 char *token; /* start of token in cmdbuf */ 1289 char *sep; /* end of token (separator) in cmdbuf */ 1290 char finaltoken[CONFIG_SYS_CBSIZE]; 1291 char *str = cmdbuf; 1292 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ 1293 int argc, inquotes; 1294 int repeatable = 1; 1295 int rc = 0; 1296 1297 #ifdef DEBUG_PARSER 1298 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd); 1299 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */ 1300 puts ("\"\n"); 1301 #endif 1302 1303 clear_ctrlc(); /* forget any previous Control C */ 1304 1305 if (!cmd || !*cmd) { 1306 return -1; /* empty command */ 1307 } 1308 1309 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) { 1310 puts ("## Command too long!\n"); 1311 return -1; 1312 } 1313 1314 strcpy (cmdbuf, cmd); 1315 1316 /* Process separators and check for invalid 1317 * repeatable commands 1318 */ 1319 1320 #ifdef DEBUG_PARSER 1321 printf ("[PROCESS_SEPARATORS] %s\n", cmd); 1322 #endif 1323 while (*str) { 1324 1325 /* 1326 * Find separator, or string end 1327 * Allow simple escape of ';' by writing "\;" 1328 */ 1329 for (inquotes = 0, sep = str; *sep; sep++) { 1330 if ((*sep=='\'') && 1331 (*(sep-1) != '\\')) 1332 inquotes=!inquotes; 1333 1334 if (!inquotes && 1335 (*sep == ';') && /* separator */ 1336 ( sep != str) && /* past string start */ 1337 (*(sep-1) != '\\')) /* and NOT escaped */ 1338 break; 1339 } 1340 1341 /* 1342 * Limit the token to data between separators 1343 */ 1344 token = str; 1345 if (*sep) { 1346 str = sep + 1; /* start of command for next pass */ 1347 *sep = '\0'; 1348 } 1349 else 1350 str = sep; /* no more commands for next pass */ 1351 #ifdef DEBUG_PARSER 1352 printf ("token: \"%s\"\n", token); 1353 #endif 1354 1355 /* find macros in this token and replace them */ 1356 process_macros (token, finaltoken); 1357 1358 /* Extract arguments */ 1359 if ((argc = parse_line (finaltoken, argv)) == 0) { 1360 rc = -1; /* no command at all */ 1361 continue; 1362 } 1363 1364 /* Look up command in command table */ 1365 if ((cmdtp = find_cmd(argv[0])) == NULL) { 1366 printf ("Unknown command '%s' - try 'help'\n", argv[0]); 1367 rc = -1; /* give up after bad command */ 1368 continue; 1369 } 1370 1371 /* found - check max args */ 1372 if (argc > cmdtp->maxargs) { 1373 cmd_usage(cmdtp); 1374 rc = -1; 1375 continue; 1376 } 1377 1378 #if defined(CONFIG_CMD_BOOTD) 1379 /* avoid "bootd" recursion */ 1380 if (cmdtp->cmd == do_bootd) { 1381 #ifdef DEBUG_PARSER 1382 printf ("[%s]\n", finaltoken); 1383 #endif 1384 if (flag & CMD_FLAG_BOOTD) { 1385 puts ("'bootd' recursion detected\n"); 1386 rc = -1; 1387 continue; 1388 } else { 1389 flag |= CMD_FLAG_BOOTD; 1390 } 1391 } 1392 #endif 1393 1394 /* OK - call function to do the command */ 1395 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) { 1396 rc = -1; 1397 } 1398 1399 repeatable &= cmdtp->repeatable; 1400 1401 /* Did the user stop this? */ 1402 if (had_ctrlc ()) 1403 return -1; /* if stopped then not repeatable */ 1404 } 1405 1406 return rc ? rc : repeatable; 1407 } 1408 1409 /****************************************************************************/ 1410 1411 #if defined(CONFIG_CMD_RUN) 1412 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) 1413 { 1414 int i; 1415 1416 if (argc < 2) 1417 return cmd_usage(cmdtp); 1418 1419 for (i=1; i<argc; ++i) { 1420 char *arg; 1421 1422 if ((arg = getenv (argv[i])) == NULL) { 1423 printf ("## Error: \"%s\" not defined\n", argv[i]); 1424 return 1; 1425 } 1426 1427 if (run_command2(arg, flag) != 0) 1428 return 1; 1429 } 1430 return 0; 1431 } 1432 #endif 1433