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