1c609719bSwdenk /* 2c609719bSwdenk * (C) Copyright 2000 3c609719bSwdenk * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4c609719bSwdenk * 5501090aaSWolfgang Denk * Add to readline cmdline-editing by 6501090aaSWolfgang Denk * (C) Copyright 2005 7501090aaSWolfgang Denk * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> 8501090aaSWolfgang Denk * 9c609719bSwdenk * See file CREDITS for list of people who contributed to this 10c609719bSwdenk * project. 11c609719bSwdenk * 12c609719bSwdenk * This program is free software; you can redistribute it and/or 13c609719bSwdenk * modify it under the terms of the GNU General Public License as 14c609719bSwdenk * published by the Free Software Foundation; either version 2 of 15c609719bSwdenk * the License, or (at your option) any later version. 16c609719bSwdenk * 17c609719bSwdenk * This program is distributed in the hope that it will be useful, 18c609719bSwdenk * but WITHOUT ANY WARRANTY; without even the implied warranty of 19c609719bSwdenk * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20c609719bSwdenk * GNU General Public License for more details. 21c609719bSwdenk * 22c609719bSwdenk * You should have received a copy of the GNU General Public License 23c609719bSwdenk * along with this program; if not, write to the Free Software 24c609719bSwdenk * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 25c609719bSwdenk * MA 02111-1307 USA 26c609719bSwdenk */ 27c609719bSwdenk 28a6c7ad2fSwdenk /* #define DEBUG */ 29a6c7ad2fSwdenk 30c609719bSwdenk #include <common.h> 31c609719bSwdenk #include <watchdog.h> 32c609719bSwdenk #include <command.h> 33*09c2e90cSAndreas Bießmann #include <version.h> 34d9631ecfSWolfgang Denk #ifdef CONFIG_MODEM_SUPPORT 35d9631ecfSWolfgang Denk #include <malloc.h> /* for free() prototype */ 36d9631ecfSWolfgang Denk #endif 37c609719bSwdenk 386d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER 39c609719bSwdenk #include <hush.h> 40c609719bSwdenk #endif 41c609719bSwdenk 42bdccc4feSwdenk #include <post.h> 43bdccc4feSwdenk 44597f6c26SJames Yang #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING) 45d87080b7SWolfgang Denk DECLARE_GLOBAL_DATA_PTR; 46d87080b7SWolfgang Denk #endif 47d87080b7SWolfgang Denk 48fad63407SHeiko Schocher /* 49fad63407SHeiko Schocher * Board-specific Platform code can reimplement show_boot_progress () if needed 50fad63407SHeiko Schocher */ 51fad63407SHeiko Schocher void inline __show_boot_progress (int val) {} 525e2c08c3SEmil Medve void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress"))); 53fad63407SHeiko Schocher 544bae9090SBartlomiej Sieka #if defined(CONFIG_UPDATE_TFTP) 558d6b7320SAndreas Pretzsch int update_tftp (ulong addr); 564bae9090SBartlomiej Sieka #endif /* CONFIG_UPDATE_TFTP */ 578bde7f77Swdenk 58c609719bSwdenk #define MAX_DELAY_STOP_STR 32 59c609719bSwdenk 60c609719bSwdenk #undef DEBUG_PARSER 61c609719bSwdenk 626475b9f9SJohn Schmoller char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ 63c609719bSwdenk 643ca9122fSStefan Roese static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen); 6582359aecSMike Frysinger static const char erase_seq[] = "\b \b"; /* erase sequence */ 6682359aecSMike Frysinger static const char tab_seq[] = " "; /* used to expand TABs */ 67c609719bSwdenk 68c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME 69c609719bSwdenk static uint64_t endtime = 0; /* must be set, default is instant timeout */ 70c609719bSwdenk static int retry_time = -1; /* -1 so can call readline before main_loop */ 71c609719bSwdenk #endif 72c609719bSwdenk 73c609719bSwdenk #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) 74c609719bSwdenk 75c609719bSwdenk #ifndef CONFIG_BOOT_RETRY_MIN 76c609719bSwdenk #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME 77c609719bSwdenk #endif 78c609719bSwdenk 79c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT 80c609719bSwdenk int do_mdm_init = 0; 81c609719bSwdenk extern void mdm_init(void); /* defined in board.c */ 82c609719bSwdenk #endif 83c609719bSwdenk 84c609719bSwdenk /*************************************************************************** 85c609719bSwdenk * Watch for 'delay' seconds for autoboot stop or autoboot delay string. 86c609719bSwdenk * returns: 0 - no key string, allow autoboot 87c609719bSwdenk * 1 - got key string, abort 88c609719bSwdenk */ 89c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) 90c609719bSwdenk # if defined(CONFIG_AUTOBOOT_KEYED) 913c766dccSJason Hobbs static inline int abortboot(int bootdelay) 92c609719bSwdenk { 93c609719bSwdenk int abort = 0; 94c609719bSwdenk uint64_t etime = endtick(bootdelay); 9519973b6aSWolfgang Denk struct { 96c609719bSwdenk char* str; 97c609719bSwdenk u_int len; 98c609719bSwdenk int retry; 99c609719bSwdenk } 10019973b6aSWolfgang Denk delaykey [] = { 101c609719bSwdenk { str: getenv ("bootdelaykey"), retry: 1 }, 102c609719bSwdenk { str: getenv ("bootdelaykey2"), retry: 1 }, 103c609719bSwdenk { str: getenv ("bootstopkey"), retry: 0 }, 104c609719bSwdenk { str: getenv ("bootstopkey2"), retry: 0 }, 105c609719bSwdenk }; 106c609719bSwdenk 107c609719bSwdenk char presskey [MAX_DELAY_STOP_STR]; 108c609719bSwdenk u_int presskey_len = 0; 109c609719bSwdenk u_int presskey_max = 0; 110c609719bSwdenk u_int i; 111c609719bSwdenk 112c609719bSwdenk # ifdef CONFIG_AUTOBOOT_PROMPT 113f2302d44SStefan Roese printf(CONFIG_AUTOBOOT_PROMPT); 114c609719bSwdenk # endif 115c609719bSwdenk 116c609719bSwdenk # ifdef CONFIG_AUTOBOOT_DELAY_STR 117c609719bSwdenk if (delaykey[0].str == NULL) 118c609719bSwdenk delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; 119c609719bSwdenk # endif 120c609719bSwdenk # ifdef CONFIG_AUTOBOOT_DELAY_STR2 121c609719bSwdenk if (delaykey[1].str == NULL) 122c609719bSwdenk delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2; 123c609719bSwdenk # endif 124c609719bSwdenk # ifdef CONFIG_AUTOBOOT_STOP_STR 125c609719bSwdenk if (delaykey[2].str == NULL) 126c609719bSwdenk delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR; 127c609719bSwdenk # endif 128c609719bSwdenk # ifdef CONFIG_AUTOBOOT_STOP_STR2 129c609719bSwdenk if (delaykey[3].str == NULL) 130c609719bSwdenk delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2; 131c609719bSwdenk # endif 132c609719bSwdenk 133c609719bSwdenk for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { 134c609719bSwdenk delaykey[i].len = delaykey[i].str == NULL ? 135c609719bSwdenk 0 : strlen (delaykey[i].str); 136c609719bSwdenk delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ? 137c609719bSwdenk MAX_DELAY_STOP_STR : delaykey[i].len; 138c609719bSwdenk 139c609719bSwdenk presskey_max = presskey_max > delaykey[i].len ? 140c609719bSwdenk presskey_max : delaykey[i].len; 141c609719bSwdenk 142c609719bSwdenk # if DEBUG_BOOTKEYS 143c609719bSwdenk printf("%s key:<%s>\n", 144c609719bSwdenk delaykey[i].retry ? "delay" : "stop", 145c609719bSwdenk delaykey[i].str ? delaykey[i].str : "NULL"); 146c609719bSwdenk # endif 147c609719bSwdenk } 148c609719bSwdenk 149c609719bSwdenk /* In order to keep up with incoming data, check timeout only 150c609719bSwdenk * when catch up. 151c609719bSwdenk */ 152c3284b03SPeter Korsgaard do { 153c3284b03SPeter Korsgaard if (tstc()) { 154c3284b03SPeter Korsgaard if (presskey_len < presskey_max) { 155c3284b03SPeter Korsgaard presskey [presskey_len ++] = getc(); 156c3284b03SPeter Korsgaard } 157c3284b03SPeter Korsgaard else { 158c3284b03SPeter Korsgaard for (i = 0; i < presskey_max - 1; i ++) 159c3284b03SPeter Korsgaard presskey [i] = presskey [i + 1]; 160c3284b03SPeter Korsgaard 161c3284b03SPeter Korsgaard presskey [i] = getc(); 162c3284b03SPeter Korsgaard } 163c3284b03SPeter Korsgaard } 164c3284b03SPeter Korsgaard 165c609719bSwdenk for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { 166c609719bSwdenk if (delaykey[i].len > 0 && 167c609719bSwdenk presskey_len >= delaykey[i].len && 168c609719bSwdenk memcmp (presskey + presskey_len - delaykey[i].len, 169c609719bSwdenk delaykey[i].str, 170c609719bSwdenk delaykey[i].len) == 0) { 171c609719bSwdenk # if DEBUG_BOOTKEYS 172c609719bSwdenk printf("got %skey\n", 173c609719bSwdenk delaykey[i].retry ? "delay" : "stop"); 174c609719bSwdenk # endif 175c609719bSwdenk 176c609719bSwdenk # ifdef CONFIG_BOOT_RETRY_TIME 177c609719bSwdenk /* don't retry auto boot */ 178c609719bSwdenk if (! delaykey[i].retry) 179c609719bSwdenk retry_time = -1; 180c609719bSwdenk # endif 181c609719bSwdenk abort = 1; 182c609719bSwdenk } 183c609719bSwdenk } 184c3284b03SPeter Korsgaard } while (!abort && get_ticks() <= etime); 185c609719bSwdenk 186c609719bSwdenk # if DEBUG_BOOTKEYS 187c609719bSwdenk if (!abort) 1884b9206edSwdenk puts("key timeout\n"); 189c609719bSwdenk # endif 190c609719bSwdenk 1918cb8143eSdzu #ifdef CONFIG_SILENT_CONSOLE 1924ec5bd55SLadislav Michl if (abort) 1934ec5bd55SLadislav Michl gd->flags &= ~GD_FLG_SILENT; 1948cb8143eSdzu #endif 1958cb8143eSdzu 196c609719bSwdenk return abort; 197c609719bSwdenk } 198c609719bSwdenk 199c609719bSwdenk # else /* !defined(CONFIG_AUTOBOOT_KEYED) */ 200c609719bSwdenk 201c7de829cSwdenk #ifdef CONFIG_MENUKEY 202c7de829cSwdenk static int menukey = 0; 203c7de829cSwdenk #endif 204c7de829cSwdenk 2053c766dccSJason Hobbs static inline int abortboot(int bootdelay) 206c609719bSwdenk { 207c609719bSwdenk int abort = 0; 208c609719bSwdenk 209c7de829cSwdenk #ifdef CONFIG_MENUPROMPT 210f2302d44SStefan Roese printf(CONFIG_MENUPROMPT); 211c7de829cSwdenk #else 212c609719bSwdenk printf("Hit any key to stop autoboot: %2d ", bootdelay); 213c7de829cSwdenk #endif 214c609719bSwdenk 215c609719bSwdenk #if defined CONFIG_ZERO_BOOTDELAY_CHECK 216c609719bSwdenk /* 217c609719bSwdenk * Check if key already pressed 218c609719bSwdenk * Don't check if bootdelay < 0 219c609719bSwdenk */ 220c609719bSwdenk if (bootdelay >= 0) { 221c609719bSwdenk if (tstc()) { /* we got a key press */ 222c609719bSwdenk (void) getc(); /* consume input */ 2234b9206edSwdenk puts ("\b\b\b 0"); 224f72da340Swdenk abort = 1; /* don't auto boot */ 225c609719bSwdenk } 226c609719bSwdenk } 227c609719bSwdenk #endif 228c609719bSwdenk 229f72da340Swdenk while ((bootdelay > 0) && (!abort)) { 230c609719bSwdenk int i; 231c609719bSwdenk 232c609719bSwdenk --bootdelay; 233c609719bSwdenk /* delay 100 * 10ms */ 234c609719bSwdenk for (i=0; !abort && i<100; ++i) { 235c609719bSwdenk if (tstc()) { /* we got a key press */ 236c609719bSwdenk abort = 1; /* don't auto boot */ 237c609719bSwdenk bootdelay = 0; /* no more delay */ 238c7de829cSwdenk # ifdef CONFIG_MENUKEY 239c7de829cSwdenk menukey = getc(); 240c7de829cSwdenk # else 241c609719bSwdenk (void) getc(); /* consume input */ 242c7de829cSwdenk # endif 243c609719bSwdenk break; 244c609719bSwdenk } 245c609719bSwdenk udelay(10000); 246c609719bSwdenk } 247c609719bSwdenk 248c609719bSwdenk printf("\b\b\b%2d ", bootdelay); 249c609719bSwdenk } 250c609719bSwdenk 251c609719bSwdenk putc('\n'); 252c609719bSwdenk 253f72da340Swdenk #ifdef CONFIG_SILENT_CONSOLE 2544ec5bd55SLadislav Michl if (abort) 2554ec5bd55SLadislav Michl gd->flags &= ~GD_FLG_SILENT; 256f72da340Swdenk #endif 257f72da340Swdenk 258c609719bSwdenk return abort; 259c609719bSwdenk } 260c609719bSwdenk # endif /* CONFIG_AUTOBOOT_KEYED */ 261c609719bSwdenk #endif /* CONFIG_BOOTDELAY >= 0 */ 262c609719bSwdenk 263c609719bSwdenk /****************************************************************************/ 264c609719bSwdenk 265c609719bSwdenk void main_loop (void) 266c609719bSwdenk { 2676d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER 2686d0f6bcfSJean-Christophe PLAGNIOL-VILLARD static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, }; 269c609719bSwdenk int len; 270c609719bSwdenk int rc = 1; 271c609719bSwdenk int flag; 272c609719bSwdenk #endif 273c609719bSwdenk 274c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) 275c609719bSwdenk char *s; 276c609719bSwdenk int bootdelay; 277c609719bSwdenk #endif 278c609719bSwdenk #ifdef CONFIG_PREBOOT 279c609719bSwdenk char *p; 280c609719bSwdenk #endif 281bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT 282bdccc4feSwdenk unsigned long bootcount = 0; 283bdccc4feSwdenk unsigned long bootlimit = 0; 284bdccc4feSwdenk char *bcs; 285bdccc4feSwdenk char bcs_set[16]; 286bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */ 287c609719bSwdenk 288bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT 289bdccc4feSwdenk bootcount = bootcount_load(); 290bdccc4feSwdenk bootcount++; 291bdccc4feSwdenk bootcount_store (bootcount); 292bdccc4feSwdenk sprintf (bcs_set, "%lu", bootcount); 293bdccc4feSwdenk setenv ("bootcount", bcs_set); 294bdccc4feSwdenk bcs = getenv ("bootlimit"); 295bdccc4feSwdenk bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0; 296bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */ 297bdccc4feSwdenk 298c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT 299c609719bSwdenk debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init); 300c609719bSwdenk if (do_mdm_init) { 30177ddac94SWolfgang Denk char *str = strdup(getenv("mdm_cmd")); 302c609719bSwdenk setenv ("preboot", str); /* set or delete definition */ 303c609719bSwdenk if (str != NULL) 304c609719bSwdenk free (str); 305c609719bSwdenk mdm_init(); /* wait for modem connection */ 306c609719bSwdenk } 307c609719bSwdenk #endif /* CONFIG_MODEM_SUPPORT */ 308c609719bSwdenk 3090587597cSstroese #ifdef CONFIG_VERSION_VARIABLE 3100587597cSstroese { 3110587597cSstroese setenv ("ver", version_string); /* set version variable */ 3120587597cSstroese } 3130587597cSstroese #endif /* CONFIG_VERSION_VARIABLE */ 3140587597cSstroese 3156d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER 316c609719bSwdenk u_boot_hush_start (); 317c609719bSwdenk #endif 318c609719bSwdenk 31981473f67SHeiko Schocher #if defined(CONFIG_HUSH_INIT_VAR) 32081473f67SHeiko Schocher hush_init_var (); 32181473f67SHeiko Schocher #endif 32281473f67SHeiko Schocher 323c609719bSwdenk #ifdef CONFIG_PREBOOT 324c609719bSwdenk if ((p = getenv ("preboot")) != NULL) { 325c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED 326c609719bSwdenk int prev = disable_ctrlc(1); /* disable Control C checking */ 327c609719bSwdenk # endif 328c609719bSwdenk 3296d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER 330c609719bSwdenk run_command (p, 0); 331c609719bSwdenk # else 332c609719bSwdenk parse_string_outer(p, FLAG_PARSE_SEMICOLON | 333c609719bSwdenk FLAG_EXIT_FROM_LOOP); 334c609719bSwdenk # endif 335c609719bSwdenk 336c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED 337c609719bSwdenk disable_ctrlc(prev); /* restore Control C checking */ 338c609719bSwdenk # endif 339c609719bSwdenk } 340c609719bSwdenk #endif /* CONFIG_PREBOOT */ 341c609719bSwdenk 342143cd21fSWolfgang Denk #if defined(CONFIG_UPDATE_TFTP) 3438d6b7320SAndreas Pretzsch update_tftp (0UL); 344143cd21fSWolfgang Denk #endif /* CONFIG_UPDATE_TFTP */ 345143cd21fSWolfgang Denk 346c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) 347c609719bSwdenk s = getenv ("bootdelay"); 348c609719bSwdenk bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; 349c609719bSwdenk 350a6c7ad2fSwdenk debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay); 351c609719bSwdenk 352c609719bSwdenk # ifdef CONFIG_BOOT_RETRY_TIME 3536dd652faSwdenk init_cmd_timeout (); 354c609719bSwdenk # endif /* CONFIG_BOOT_RETRY_TIME */ 355c609719bSwdenk 356b428f6a8SYuri Tikhonov #ifdef CONFIG_POST 357b428f6a8SYuri Tikhonov if (gd->flags & GD_FLG_POSTFAIL) { 358b428f6a8SYuri Tikhonov s = getenv("failbootcmd"); 359b428f6a8SYuri Tikhonov } 360b428f6a8SYuri Tikhonov else 361b428f6a8SYuri Tikhonov #endif /* CONFIG_POST */ 362bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT 363bdccc4feSwdenk if (bootlimit && (bootcount > bootlimit)) { 364bdccc4feSwdenk printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n", 365bdccc4feSwdenk (unsigned)bootlimit); 366bdccc4feSwdenk s = getenv ("altbootcmd"); 367bdccc4feSwdenk } 368bdccc4feSwdenk else 369bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */ 370c609719bSwdenk s = getenv ("bootcmd"); 371a6c7ad2fSwdenk 372a6c7ad2fSwdenk debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); 373a6c7ad2fSwdenk 374c609719bSwdenk if (bootdelay >= 0 && s && !abortboot (bootdelay)) { 375c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED 376c609719bSwdenk int prev = disable_ctrlc(1); /* disable Control C checking */ 377c609719bSwdenk # endif 378c609719bSwdenk 3796d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER 380c609719bSwdenk run_command (s, 0); 381c609719bSwdenk # else 382c609719bSwdenk parse_string_outer(s, FLAG_PARSE_SEMICOLON | 383c609719bSwdenk FLAG_EXIT_FROM_LOOP); 384c609719bSwdenk # endif 385c609719bSwdenk 386c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED 387c609719bSwdenk disable_ctrlc(prev); /* restore Control C checking */ 388c609719bSwdenk # endif 389c609719bSwdenk } 390c7de829cSwdenk 391c7de829cSwdenk # ifdef CONFIG_MENUKEY 392a6c7ad2fSwdenk if (menukey == CONFIG_MENUKEY) { 393c7de829cSwdenk s = getenv("menucmd"); 394a6c7ad2fSwdenk if (s) { 3956d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER 3966225c5dbSwdenk run_command(s, 0); 397c7de829cSwdenk # else 398c7de829cSwdenk parse_string_outer(s, FLAG_PARSE_SEMICOLON | 399c7de829cSwdenk FLAG_EXIT_FROM_LOOP); 400c7de829cSwdenk # endif 401c7de829cSwdenk } 402c7de829cSwdenk } 403c7de829cSwdenk #endif /* CONFIG_MENUKEY */ 404c609719bSwdenk #endif /* CONFIG_BOOTDELAY */ 405c7de829cSwdenk 406c609719bSwdenk /* 407c609719bSwdenk * Main Loop for Monitor Command Processing 408c609719bSwdenk */ 4096d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER 410c609719bSwdenk parse_file_outer(); 411c609719bSwdenk /* This point is never reached */ 412c609719bSwdenk for (;;); 413c609719bSwdenk #else 414c609719bSwdenk for (;;) { 415c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME 416c609719bSwdenk if (rc >= 0) { 417c609719bSwdenk /* Saw enough of a valid command to 418c609719bSwdenk * restart the timeout. 419c609719bSwdenk */ 420c609719bSwdenk reset_cmd_timeout(); 421c609719bSwdenk } 422c609719bSwdenk #endif 4236d0f6bcfSJean-Christophe PLAGNIOL-VILLARD len = readline (CONFIG_SYS_PROMPT); 424c609719bSwdenk 425c609719bSwdenk flag = 0; /* assume no special flags for now */ 426c609719bSwdenk if (len > 0) 427c609719bSwdenk strcpy (lastcommand, console_buffer); 428c609719bSwdenk else if (len == 0) 429c609719bSwdenk flag |= CMD_FLAG_REPEAT; 430c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME 431c609719bSwdenk else if (len == -2) { 432c609719bSwdenk /* -2 means timed out, retry autoboot 433c609719bSwdenk */ 4344b9206edSwdenk puts ("\nTimed out waiting for command\n"); 435c609719bSwdenk # ifdef CONFIG_RESET_TO_RETRY 436c609719bSwdenk /* Reinit board to run initialization code again */ 437c609719bSwdenk do_reset (NULL, 0, 0, NULL); 438c609719bSwdenk # else 439c609719bSwdenk return; /* retry autoboot */ 440c609719bSwdenk # endif 441c609719bSwdenk } 442c609719bSwdenk #endif 443c609719bSwdenk 444c609719bSwdenk if (len == -1) 4454b9206edSwdenk puts ("<INTERRUPT>\n"); 446c609719bSwdenk else 447c609719bSwdenk rc = run_command (lastcommand, flag); 448c609719bSwdenk 449c609719bSwdenk if (rc <= 0) { 450c609719bSwdenk /* invalid command or not repeatable, forget it */ 451c609719bSwdenk lastcommand[0] = 0; 452c609719bSwdenk } 453c609719bSwdenk } 4546d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /*CONFIG_SYS_HUSH_PARSER*/ 455c609719bSwdenk } 456c609719bSwdenk 4576dd652faSwdenk #ifdef CONFIG_BOOT_RETRY_TIME 4586dd652faSwdenk /*************************************************************************** 45919973b6aSWolfgang Denk * initialize command line timeout 4606dd652faSwdenk */ 4616dd652faSwdenk void init_cmd_timeout(void) 4626dd652faSwdenk { 4636dd652faSwdenk char *s = getenv ("bootretry"); 4646dd652faSwdenk 4656dd652faSwdenk if (s != NULL) 466b028f715Swdenk retry_time = (int)simple_strtol(s, NULL, 10); 4676dd652faSwdenk else 4686dd652faSwdenk retry_time = CONFIG_BOOT_RETRY_TIME; 4696dd652faSwdenk 4706dd652faSwdenk if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) 4716dd652faSwdenk retry_time = CONFIG_BOOT_RETRY_MIN; 4726dd652faSwdenk } 4736dd652faSwdenk 474c609719bSwdenk /*************************************************************************** 475c609719bSwdenk * reset command line timeout to retry_time seconds 476c609719bSwdenk */ 477c609719bSwdenk void reset_cmd_timeout(void) 478c609719bSwdenk { 479c609719bSwdenk endtime = endtick(retry_time); 480c609719bSwdenk } 481c609719bSwdenk #endif 482c609719bSwdenk 483501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING 484501090aaSWolfgang Denk 485501090aaSWolfgang Denk /* 486501090aaSWolfgang Denk * cmdline-editing related codes from vivi. 487501090aaSWolfgang Denk * Author: Janghoon Lyu <nandy@mizi.com> 488501090aaSWolfgang Denk */ 489501090aaSWolfgang Denk 490501090aaSWolfgang Denk #define putnstr(str,n) do { \ 491dc4b0b38SAndrew Klossner printf ("%.*s", (int)n, str); \ 492501090aaSWolfgang Denk } while (0) 493501090aaSWolfgang Denk 494501090aaSWolfgang Denk #define CTL_CH(c) ((c) - 'a' + 1) 495501090aaSWolfgang Denk #define CTL_BACKSPACE ('\b') 496501090aaSWolfgang Denk #define DEL ((char)255) 497501090aaSWolfgang Denk #define DEL7 ((char)127) 498501090aaSWolfgang Denk #define CREAD_HIST_CHAR ('!') 499501090aaSWolfgang Denk 500501090aaSWolfgang Denk #define getcmd_putch(ch) putc(ch) 501501090aaSWolfgang Denk #define getcmd_getch() getc() 502501090aaSWolfgang Denk #define getcmd_cbeep() getcmd_putch('\a') 503501090aaSWolfgang Denk 504501090aaSWolfgang Denk #define HIST_MAX 20 5058804ae3bSPeter Tyser #define HIST_SIZE CONFIG_SYS_CBSIZE 506501090aaSWolfgang Denk 507501090aaSWolfgang Denk static int hist_max = 0; 508501090aaSWolfgang Denk static int hist_add_idx = 0; 509501090aaSWolfgang Denk static int hist_cur = -1; 510501090aaSWolfgang Denk unsigned hist_num = 0; 511501090aaSWolfgang Denk 512501090aaSWolfgang Denk char* hist_list[HIST_MAX]; 5136475b9f9SJohn Schmoller char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ 514501090aaSWolfgang Denk 515501090aaSWolfgang Denk #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) 516501090aaSWolfgang Denk 517501090aaSWolfgang Denk static void hist_init(void) 518501090aaSWolfgang Denk { 519501090aaSWolfgang Denk int i; 520501090aaSWolfgang Denk 521501090aaSWolfgang Denk hist_max = 0; 522501090aaSWolfgang Denk hist_add_idx = 0; 523501090aaSWolfgang Denk hist_cur = -1; 524501090aaSWolfgang Denk hist_num = 0; 525501090aaSWolfgang Denk 526501090aaSWolfgang Denk for (i = 0; i < HIST_MAX; i++) { 527501090aaSWolfgang Denk hist_list[i] = hist_lines[i]; 528501090aaSWolfgang Denk hist_list[i][0] = '\0'; 529501090aaSWolfgang Denk } 530501090aaSWolfgang Denk } 531501090aaSWolfgang Denk 532501090aaSWolfgang Denk static void cread_add_to_hist(char *line) 533501090aaSWolfgang Denk { 534501090aaSWolfgang Denk strcpy(hist_list[hist_add_idx], line); 535501090aaSWolfgang Denk 536501090aaSWolfgang Denk if (++hist_add_idx >= HIST_MAX) 537501090aaSWolfgang Denk hist_add_idx = 0; 538501090aaSWolfgang Denk 539501090aaSWolfgang Denk if (hist_add_idx > hist_max) 540501090aaSWolfgang Denk hist_max = hist_add_idx; 541501090aaSWolfgang Denk 542501090aaSWolfgang Denk hist_num++; 543501090aaSWolfgang Denk } 544501090aaSWolfgang Denk 545501090aaSWolfgang Denk static char* hist_prev(void) 546501090aaSWolfgang Denk { 547501090aaSWolfgang Denk char *ret; 548501090aaSWolfgang Denk int old_cur; 549501090aaSWolfgang Denk 550501090aaSWolfgang Denk if (hist_cur < 0) 551501090aaSWolfgang Denk return NULL; 552501090aaSWolfgang Denk 553501090aaSWolfgang Denk old_cur = hist_cur; 554501090aaSWolfgang Denk if (--hist_cur < 0) 555501090aaSWolfgang Denk hist_cur = hist_max; 556501090aaSWolfgang Denk 557501090aaSWolfgang Denk if (hist_cur == hist_add_idx) { 558501090aaSWolfgang Denk hist_cur = old_cur; 559501090aaSWolfgang Denk ret = NULL; 560501090aaSWolfgang Denk } else 561501090aaSWolfgang Denk ret = hist_list[hist_cur]; 562501090aaSWolfgang Denk 563501090aaSWolfgang Denk return (ret); 564501090aaSWolfgang Denk } 565501090aaSWolfgang Denk 566501090aaSWolfgang Denk static char* hist_next(void) 567501090aaSWolfgang Denk { 568501090aaSWolfgang Denk char *ret; 569501090aaSWolfgang Denk 570501090aaSWolfgang Denk if (hist_cur < 0) 571501090aaSWolfgang Denk return NULL; 572501090aaSWolfgang Denk 573501090aaSWolfgang Denk if (hist_cur == hist_add_idx) 574501090aaSWolfgang Denk return NULL; 575501090aaSWolfgang Denk 576501090aaSWolfgang Denk if (++hist_cur > hist_max) 577501090aaSWolfgang Denk hist_cur = 0; 578501090aaSWolfgang Denk 579501090aaSWolfgang Denk if (hist_cur == hist_add_idx) { 580501090aaSWolfgang Denk ret = ""; 581501090aaSWolfgang Denk } else 582501090aaSWolfgang Denk ret = hist_list[hist_cur]; 583501090aaSWolfgang Denk 584501090aaSWolfgang Denk return (ret); 585501090aaSWolfgang Denk } 586501090aaSWolfgang Denk 5873ca9122fSStefan Roese #ifndef CONFIG_CMDLINE_EDITING 588501090aaSWolfgang Denk static void cread_print_hist_list(void) 589501090aaSWolfgang Denk { 590501090aaSWolfgang Denk int i; 591501090aaSWolfgang Denk unsigned long n; 592501090aaSWolfgang Denk 593501090aaSWolfgang Denk n = hist_num - hist_max; 594501090aaSWolfgang Denk 595501090aaSWolfgang Denk i = hist_add_idx + 1; 596501090aaSWolfgang Denk while (1) { 597501090aaSWolfgang Denk if (i > hist_max) 598501090aaSWolfgang Denk i = 0; 599501090aaSWolfgang Denk if (i == hist_add_idx) 600501090aaSWolfgang Denk break; 601501090aaSWolfgang Denk printf("%s\n", hist_list[i]); 602501090aaSWolfgang Denk n++; 603501090aaSWolfgang Denk i++; 604501090aaSWolfgang Denk } 605501090aaSWolfgang Denk } 6063ca9122fSStefan Roese #endif /* CONFIG_CMDLINE_EDITING */ 607501090aaSWolfgang Denk 608501090aaSWolfgang Denk #define BEGINNING_OF_LINE() { \ 609501090aaSWolfgang Denk while (num) { \ 610501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); \ 611501090aaSWolfgang Denk num--; \ 612501090aaSWolfgang Denk } \ 613501090aaSWolfgang Denk } 614501090aaSWolfgang Denk 615501090aaSWolfgang Denk #define ERASE_TO_EOL() { \ 616501090aaSWolfgang Denk if (num < eol_num) { \ 6178faba489SMike Frysinger printf("%*s", (int)(eol_num - num), ""); \ 6188faba489SMike Frysinger do { \ 619501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); \ 6208faba489SMike Frysinger } while (--eol_num > num); \ 621501090aaSWolfgang Denk } \ 622501090aaSWolfgang Denk } 623501090aaSWolfgang Denk 624501090aaSWolfgang Denk #define REFRESH_TO_EOL() { \ 625501090aaSWolfgang Denk if (num < eol_num) { \ 626501090aaSWolfgang Denk wlen = eol_num - num; \ 627501090aaSWolfgang Denk putnstr(buf + num, wlen); \ 628501090aaSWolfgang Denk num = eol_num; \ 629501090aaSWolfgang Denk } \ 630501090aaSWolfgang Denk } 631501090aaSWolfgang Denk 632501090aaSWolfgang Denk static void cread_add_char(char ichar, int insert, unsigned long *num, 633501090aaSWolfgang Denk unsigned long *eol_num, char *buf, unsigned long len) 634501090aaSWolfgang Denk { 635501090aaSWolfgang Denk unsigned long wlen; 636501090aaSWolfgang Denk 637501090aaSWolfgang Denk /* room ??? */ 638501090aaSWolfgang Denk if (insert || *num == *eol_num) { 639501090aaSWolfgang Denk if (*eol_num > len - 1) { 640501090aaSWolfgang Denk getcmd_cbeep(); 641501090aaSWolfgang Denk return; 642501090aaSWolfgang Denk } 643501090aaSWolfgang Denk (*eol_num)++; 644501090aaSWolfgang Denk } 645501090aaSWolfgang Denk 646501090aaSWolfgang Denk if (insert) { 647501090aaSWolfgang Denk wlen = *eol_num - *num; 648501090aaSWolfgang Denk if (wlen > 1) { 649501090aaSWolfgang Denk memmove(&buf[*num+1], &buf[*num], wlen-1); 650501090aaSWolfgang Denk } 651501090aaSWolfgang Denk 652501090aaSWolfgang Denk buf[*num] = ichar; 653501090aaSWolfgang Denk putnstr(buf + *num, wlen); 654501090aaSWolfgang Denk (*num)++; 655501090aaSWolfgang Denk while (--wlen) { 656501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); 657501090aaSWolfgang Denk } 658501090aaSWolfgang Denk } else { 659501090aaSWolfgang Denk /* echo the character */ 660501090aaSWolfgang Denk wlen = 1; 661501090aaSWolfgang Denk buf[*num] = ichar; 662501090aaSWolfgang Denk putnstr(buf + *num, wlen); 663501090aaSWolfgang Denk (*num)++; 664501090aaSWolfgang Denk } 665501090aaSWolfgang Denk } 666501090aaSWolfgang Denk 667501090aaSWolfgang Denk static void cread_add_str(char *str, int strsize, int insert, unsigned long *num, 668501090aaSWolfgang Denk unsigned long *eol_num, char *buf, unsigned long len) 669501090aaSWolfgang Denk { 670501090aaSWolfgang Denk while (strsize--) { 671501090aaSWolfgang Denk cread_add_char(*str, insert, num, eol_num, buf, len); 672501090aaSWolfgang Denk str++; 673501090aaSWolfgang Denk } 674501090aaSWolfgang Denk } 675501090aaSWolfgang Denk 67623d0baf9SJean-Christophe PLAGNIOL-VILLARD static int cread_line(const char *const prompt, char *buf, unsigned int *len) 677501090aaSWolfgang Denk { 678501090aaSWolfgang Denk unsigned long num = 0; 679501090aaSWolfgang Denk unsigned long eol_num = 0; 680501090aaSWolfgang Denk unsigned long wlen; 681501090aaSWolfgang Denk char ichar; 682501090aaSWolfgang Denk int insert = 1; 683501090aaSWolfgang Denk int esc_len = 0; 684501090aaSWolfgang Denk char esc_save[8]; 685ecc5500eSPeter Tyser int init_len = strlen(buf); 686ecc5500eSPeter Tyser 687ecc5500eSPeter Tyser if (init_len) 688ecc5500eSPeter Tyser cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); 689501090aaSWolfgang Denk 690501090aaSWolfgang Denk while (1) { 69100ac50e3SAndreas Engel #ifdef CONFIG_BOOT_RETRY_TIME 69200ac50e3SAndreas Engel while (!tstc()) { /* while no incoming data */ 69300ac50e3SAndreas Engel if (retry_time >= 0 && get_ticks() > endtime) 69400ac50e3SAndreas Engel return (-2); /* timed out */ 69530dc165aSJens Scharsig WATCHDOG_RESET(); 69600ac50e3SAndreas Engel } 69700ac50e3SAndreas Engel #endif 69800ac50e3SAndreas Engel 699501090aaSWolfgang Denk ichar = getcmd_getch(); 700501090aaSWolfgang Denk 701501090aaSWolfgang Denk if ((ichar == '\n') || (ichar == '\r')) { 702dd9f06f0SWolfgang Denk putc('\n'); 703501090aaSWolfgang Denk break; 704501090aaSWolfgang Denk } 705501090aaSWolfgang Denk 706501090aaSWolfgang Denk /* 707501090aaSWolfgang Denk * handle standard linux xterm esc sequences for arrow key, etc. 708501090aaSWolfgang Denk */ 709501090aaSWolfgang Denk if (esc_len != 0) { 710501090aaSWolfgang Denk if (esc_len == 1) { 711501090aaSWolfgang Denk if (ichar == '[') { 712501090aaSWolfgang Denk esc_save[esc_len] = ichar; 713501090aaSWolfgang Denk esc_len = 2; 714501090aaSWolfgang Denk } else { 715501090aaSWolfgang Denk cread_add_str(esc_save, esc_len, insert, 716501090aaSWolfgang Denk &num, &eol_num, buf, *len); 717501090aaSWolfgang Denk esc_len = 0; 718501090aaSWolfgang Denk } 719501090aaSWolfgang Denk continue; 720501090aaSWolfgang Denk } 721501090aaSWolfgang Denk 722501090aaSWolfgang Denk switch (ichar) { 723501090aaSWolfgang Denk 724501090aaSWolfgang Denk case 'D': /* <- key */ 725501090aaSWolfgang Denk ichar = CTL_CH('b'); 726501090aaSWolfgang Denk esc_len = 0; 727501090aaSWolfgang Denk break; 728501090aaSWolfgang Denk case 'C': /* -> key */ 729501090aaSWolfgang Denk ichar = CTL_CH('f'); 730501090aaSWolfgang Denk esc_len = 0; 731501090aaSWolfgang Denk break; /* pass off to ^F handler */ 732501090aaSWolfgang Denk case 'H': /* Home key */ 733501090aaSWolfgang Denk ichar = CTL_CH('a'); 734501090aaSWolfgang Denk esc_len = 0; 735501090aaSWolfgang Denk break; /* pass off to ^A handler */ 736501090aaSWolfgang Denk case 'A': /* up arrow */ 737501090aaSWolfgang Denk ichar = CTL_CH('p'); 738501090aaSWolfgang Denk esc_len = 0; 739501090aaSWolfgang Denk break; /* pass off to ^P handler */ 740501090aaSWolfgang Denk case 'B': /* down arrow */ 741501090aaSWolfgang Denk ichar = CTL_CH('n'); 742501090aaSWolfgang Denk esc_len = 0; 743501090aaSWolfgang Denk break; /* pass off to ^N handler */ 744501090aaSWolfgang Denk default: 745501090aaSWolfgang Denk esc_save[esc_len++] = ichar; 746501090aaSWolfgang Denk cread_add_str(esc_save, esc_len, insert, 747501090aaSWolfgang Denk &num, &eol_num, buf, *len); 748501090aaSWolfgang Denk esc_len = 0; 749501090aaSWolfgang Denk continue; 750501090aaSWolfgang Denk } 751501090aaSWolfgang Denk } 752501090aaSWolfgang Denk 753501090aaSWolfgang Denk switch (ichar) { 754501090aaSWolfgang Denk case 0x1b: 755501090aaSWolfgang Denk if (esc_len == 0) { 756501090aaSWolfgang Denk esc_save[esc_len] = ichar; 757501090aaSWolfgang Denk esc_len = 1; 758501090aaSWolfgang Denk } else { 759dd9f06f0SWolfgang Denk puts("impossible condition #876\n"); 760501090aaSWolfgang Denk esc_len = 0; 761501090aaSWolfgang Denk } 762501090aaSWolfgang Denk break; 763501090aaSWolfgang Denk 764501090aaSWolfgang Denk case CTL_CH('a'): 765501090aaSWolfgang Denk BEGINNING_OF_LINE(); 766501090aaSWolfgang Denk break; 767501090aaSWolfgang Denk case CTL_CH('c'): /* ^C - break */ 768501090aaSWolfgang Denk *buf = '\0'; /* discard input */ 769501090aaSWolfgang Denk return (-1); 770501090aaSWolfgang Denk case CTL_CH('f'): 771501090aaSWolfgang Denk if (num < eol_num) { 772501090aaSWolfgang Denk getcmd_putch(buf[num]); 773501090aaSWolfgang Denk num++; 774501090aaSWolfgang Denk } 775501090aaSWolfgang Denk break; 776501090aaSWolfgang Denk case CTL_CH('b'): 777501090aaSWolfgang Denk if (num) { 778501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); 779501090aaSWolfgang Denk num--; 780501090aaSWolfgang Denk } 781501090aaSWolfgang Denk break; 782501090aaSWolfgang Denk case CTL_CH('d'): 783501090aaSWolfgang Denk if (num < eol_num) { 784501090aaSWolfgang Denk wlen = eol_num - num - 1; 785501090aaSWolfgang Denk if (wlen) { 786501090aaSWolfgang Denk memmove(&buf[num], &buf[num+1], wlen); 787501090aaSWolfgang Denk putnstr(buf + num, wlen); 788501090aaSWolfgang Denk } 789501090aaSWolfgang Denk 790501090aaSWolfgang Denk getcmd_putch(' '); 791501090aaSWolfgang Denk do { 792501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); 793501090aaSWolfgang Denk } while (wlen--); 794501090aaSWolfgang Denk eol_num--; 795501090aaSWolfgang Denk } 796501090aaSWolfgang Denk break; 797501090aaSWolfgang Denk case CTL_CH('k'): 798501090aaSWolfgang Denk ERASE_TO_EOL(); 799501090aaSWolfgang Denk break; 800501090aaSWolfgang Denk case CTL_CH('e'): 801501090aaSWolfgang Denk REFRESH_TO_EOL(); 802501090aaSWolfgang Denk break; 803501090aaSWolfgang Denk case CTL_CH('o'): 804501090aaSWolfgang Denk insert = !insert; 805501090aaSWolfgang Denk break; 806501090aaSWolfgang Denk case CTL_CH('x'): 80723d0baf9SJean-Christophe PLAGNIOL-VILLARD case CTL_CH('u'): 808501090aaSWolfgang Denk BEGINNING_OF_LINE(); 809501090aaSWolfgang Denk ERASE_TO_EOL(); 810501090aaSWolfgang Denk break; 811501090aaSWolfgang Denk case DEL: 812501090aaSWolfgang Denk case DEL7: 813501090aaSWolfgang Denk case 8: 814501090aaSWolfgang Denk if (num) { 815501090aaSWolfgang Denk wlen = eol_num - num; 816501090aaSWolfgang Denk num--; 817501090aaSWolfgang Denk memmove(&buf[num], &buf[num+1], wlen); 818501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); 819501090aaSWolfgang Denk putnstr(buf + num, wlen); 820501090aaSWolfgang Denk getcmd_putch(' '); 821501090aaSWolfgang Denk do { 822501090aaSWolfgang Denk getcmd_putch(CTL_BACKSPACE); 823501090aaSWolfgang Denk } while (wlen--); 824501090aaSWolfgang Denk eol_num--; 825501090aaSWolfgang Denk } 826501090aaSWolfgang Denk break; 827501090aaSWolfgang Denk case CTL_CH('p'): 828501090aaSWolfgang Denk case CTL_CH('n'): 829501090aaSWolfgang Denk { 830501090aaSWolfgang Denk char * hline; 831501090aaSWolfgang Denk 832501090aaSWolfgang Denk esc_len = 0; 833501090aaSWolfgang Denk 834501090aaSWolfgang Denk if (ichar == CTL_CH('p')) 835501090aaSWolfgang Denk hline = hist_prev(); 836501090aaSWolfgang Denk else 837501090aaSWolfgang Denk hline = hist_next(); 838501090aaSWolfgang Denk 839501090aaSWolfgang Denk if (!hline) { 840501090aaSWolfgang Denk getcmd_cbeep(); 841501090aaSWolfgang Denk continue; 842501090aaSWolfgang Denk } 843501090aaSWolfgang Denk 844501090aaSWolfgang Denk /* nuke the current line */ 845501090aaSWolfgang Denk /* first, go home */ 846501090aaSWolfgang Denk BEGINNING_OF_LINE(); 847501090aaSWolfgang Denk 848501090aaSWolfgang Denk /* erase to end of line */ 849501090aaSWolfgang Denk ERASE_TO_EOL(); 850501090aaSWolfgang Denk 851501090aaSWolfgang Denk /* copy new line into place and display */ 852501090aaSWolfgang Denk strcpy(buf, hline); 853501090aaSWolfgang Denk eol_num = strlen(buf); 854501090aaSWolfgang Denk REFRESH_TO_EOL(); 855501090aaSWolfgang Denk continue; 856501090aaSWolfgang Denk } 85723d0baf9SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_AUTO_COMPLETE 85823d0baf9SJean-Christophe PLAGNIOL-VILLARD case '\t': { 85923d0baf9SJean-Christophe PLAGNIOL-VILLARD int num2, col; 86023d0baf9SJean-Christophe PLAGNIOL-VILLARD 86123d0baf9SJean-Christophe PLAGNIOL-VILLARD /* do not autocomplete when in the middle */ 86223d0baf9SJean-Christophe PLAGNIOL-VILLARD if (num < eol_num) { 86323d0baf9SJean-Christophe PLAGNIOL-VILLARD getcmd_cbeep(); 86423d0baf9SJean-Christophe PLAGNIOL-VILLARD break; 86523d0baf9SJean-Christophe PLAGNIOL-VILLARD } 86623d0baf9SJean-Christophe PLAGNIOL-VILLARD 86723d0baf9SJean-Christophe PLAGNIOL-VILLARD buf[num] = '\0'; 86823d0baf9SJean-Christophe PLAGNIOL-VILLARD col = strlen(prompt) + eol_num; 86923d0baf9SJean-Christophe PLAGNIOL-VILLARD num2 = num; 87023d0baf9SJean-Christophe PLAGNIOL-VILLARD if (cmd_auto_complete(prompt, buf, &num2, &col)) { 87123d0baf9SJean-Christophe PLAGNIOL-VILLARD col = num2 - num; 87223d0baf9SJean-Christophe PLAGNIOL-VILLARD num += col; 87323d0baf9SJean-Christophe PLAGNIOL-VILLARD eol_num += col; 87423d0baf9SJean-Christophe PLAGNIOL-VILLARD } 87523d0baf9SJean-Christophe PLAGNIOL-VILLARD break; 87623d0baf9SJean-Christophe PLAGNIOL-VILLARD } 87723d0baf9SJean-Christophe PLAGNIOL-VILLARD #endif 878501090aaSWolfgang Denk default: 879501090aaSWolfgang Denk cread_add_char(ichar, insert, &num, &eol_num, buf, *len); 880501090aaSWolfgang Denk break; 881501090aaSWolfgang Denk } 882501090aaSWolfgang Denk } 883501090aaSWolfgang Denk *len = eol_num; 884501090aaSWolfgang Denk buf[eol_num] = '\0'; /* lose the newline */ 885501090aaSWolfgang Denk 886501090aaSWolfgang Denk if (buf[0] && buf[0] != CREAD_HIST_CHAR) 887501090aaSWolfgang Denk cread_add_to_hist(buf); 888501090aaSWolfgang Denk hist_cur = hist_add_idx; 889501090aaSWolfgang Denk 890f9239438SPeter Tyser return 0; 891501090aaSWolfgang Denk } 892501090aaSWolfgang Denk 893501090aaSWolfgang Denk #endif /* CONFIG_CMDLINE_EDITING */ 894501090aaSWolfgang Denk 895c609719bSwdenk /****************************************************************************/ 896c609719bSwdenk 897c609719bSwdenk /* 898c609719bSwdenk * Prompt for input and read a line. 899c609719bSwdenk * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, 900c609719bSwdenk * time out when time goes past endtime (timebase time in ticks). 901c609719bSwdenk * Return: number of read characters 902c609719bSwdenk * -1 if break 903c609719bSwdenk * -2 if timed out 904c609719bSwdenk */ 905c609719bSwdenk int readline (const char *const prompt) 906c609719bSwdenk { 907ecc5500eSPeter Tyser /* 908ecc5500eSPeter Tyser * If console_buffer isn't 0-length the user will be prompted to modify 909ecc5500eSPeter Tyser * it instead of entering it from scratch as desired. 910ecc5500eSPeter Tyser */ 911ecc5500eSPeter Tyser console_buffer[0] = '\0'; 912ecc5500eSPeter Tyser 9136636b62aSJames Yang return readline_into_buffer(prompt, console_buffer); 9146636b62aSJames Yang } 9156636b62aSJames Yang 9166636b62aSJames Yang 9176636b62aSJames Yang int readline_into_buffer (const char *const prompt, char * buffer) 9186636b62aSJames Yang { 9196636b62aSJames Yang char *p = buffer; 920501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING 9218804ae3bSPeter Tyser unsigned int len = CONFIG_SYS_CBSIZE; 922d8f961bbSStefan Roese int rc; 923501090aaSWolfgang Denk static int initted = 0; 924501090aaSWolfgang Denk 925597f6c26SJames Yang /* 926597f6c26SJames Yang * History uses a global array which is not 927597f6c26SJames Yang * writable until after relocation to RAM. 928597f6c26SJames Yang * Revert to non-history version if still 929597f6c26SJames Yang * running from flash. 930597f6c26SJames Yang */ 931597f6c26SJames Yang if (gd->flags & GD_FLG_RELOC) { 932597f6c26SJames Yang if (!initted) { 933597f6c26SJames Yang hist_init(); 934597f6c26SJames Yang initted = 1; 935597f6c26SJames Yang } 936597f6c26SJames Yang 937e491a71eSPeter Tyser if (prompt) 938dd9f06f0SWolfgang Denk puts (prompt); 939501090aaSWolfgang Denk 94023d0baf9SJean-Christophe PLAGNIOL-VILLARD rc = cread_line(prompt, p, &len); 941d8f961bbSStefan Roese return rc < 0 ? rc : len; 942597f6c26SJames Yang 943597f6c26SJames Yang } else { 944597f6c26SJames Yang #endif /* CONFIG_CMDLINE_EDITING */ 9450ec59524SKumar Gala char * p_buf = p; 946c609719bSwdenk int n = 0; /* buffer index */ 947c609719bSwdenk int plen = 0; /* prompt length */ 948c609719bSwdenk int col; /* output column cnt */ 949c609719bSwdenk char c; 950c609719bSwdenk 951c609719bSwdenk /* print prompt */ 952c609719bSwdenk if (prompt) { 953c609719bSwdenk plen = strlen (prompt); 954c609719bSwdenk puts (prompt); 955c609719bSwdenk } 956c609719bSwdenk col = plen; 957c609719bSwdenk 958c609719bSwdenk for (;;) { 959c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME 960c609719bSwdenk while (!tstc()) { /* while no incoming data */ 961c609719bSwdenk if (retry_time >= 0 && get_ticks() > endtime) 962c609719bSwdenk return (-2); /* timed out */ 96330dc165aSJens Scharsig WATCHDOG_RESET(); 964c609719bSwdenk } 965c609719bSwdenk #endif 966c609719bSwdenk WATCHDOG_RESET(); /* Trigger watchdog, if needed */ 967c609719bSwdenk 968c609719bSwdenk #ifdef CONFIG_SHOW_ACTIVITY 969c609719bSwdenk while (!tstc()) { 970c609719bSwdenk extern void show_activity(int arg); 971c609719bSwdenk show_activity(0); 97230dc165aSJens Scharsig WATCHDOG_RESET(); 973c609719bSwdenk } 974c609719bSwdenk #endif 975c609719bSwdenk c = getc(); 976c609719bSwdenk 977c609719bSwdenk /* 978c609719bSwdenk * Special character handling 979c609719bSwdenk */ 980c609719bSwdenk switch (c) { 981c609719bSwdenk case '\r': /* Enter */ 982c609719bSwdenk case '\n': 983c609719bSwdenk *p = '\0'; 984c609719bSwdenk puts ("\r\n"); 9856636b62aSJames Yang return (p - p_buf); 986c609719bSwdenk 98727aa8186Swdenk case '\0': /* nul */ 98827aa8186Swdenk continue; 98927aa8186Swdenk 990c609719bSwdenk case 0x03: /* ^C - break */ 9916636b62aSJames Yang p_buf[0] = '\0'; /* discard input */ 992c609719bSwdenk return (-1); 993c609719bSwdenk 994c609719bSwdenk case 0x15: /* ^U - erase line */ 995c609719bSwdenk while (col > plen) { 996c609719bSwdenk puts (erase_seq); 997c609719bSwdenk --col; 998c609719bSwdenk } 9996636b62aSJames Yang p = p_buf; 1000c609719bSwdenk n = 0; 1001c609719bSwdenk continue; 1002c609719bSwdenk 1003c609719bSwdenk case 0x17: /* ^W - erase word */ 10046636b62aSJames Yang p=delete_char(p_buf, p, &col, &n, plen); 1005c609719bSwdenk while ((n > 0) && (*p != ' ')) { 10066636b62aSJames Yang p=delete_char(p_buf, p, &col, &n, plen); 1007c609719bSwdenk } 1008c609719bSwdenk continue; 1009c609719bSwdenk 1010c609719bSwdenk case 0x08: /* ^H - backspace */ 1011c609719bSwdenk case 0x7F: /* DEL - backspace */ 10126636b62aSJames Yang p=delete_char(p_buf, p, &col, &n, plen); 1013c609719bSwdenk continue; 1014c609719bSwdenk 1015c609719bSwdenk default: 1016c609719bSwdenk /* 1017c609719bSwdenk * Must be a normal character then 1018c609719bSwdenk */ 10196d0f6bcfSJean-Christophe PLAGNIOL-VILLARD if (n < CONFIG_SYS_CBSIZE-2) { 1020c609719bSwdenk if (c == '\t') { /* expand TABs */ 102104a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE 102204a85b3bSwdenk /* if auto completion triggered just continue */ 102304a85b3bSwdenk *p = '\0'; 102404a85b3bSwdenk if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { 10256636b62aSJames Yang p = p_buf + n; /* reset */ 102604a85b3bSwdenk continue; 102704a85b3bSwdenk } 102804a85b3bSwdenk #endif 1029c609719bSwdenk puts (tab_seq+(col&07)); 1030c609719bSwdenk col += 8 - (col&07); 1031c609719bSwdenk } else { 1032c609719bSwdenk ++col; /* echo input */ 1033c609719bSwdenk putc (c); 1034c609719bSwdenk } 1035c609719bSwdenk *p++ = c; 1036c609719bSwdenk ++n; 1037c609719bSwdenk } else { /* Buffer full */ 1038c609719bSwdenk putc ('\a'); 1039c609719bSwdenk } 1040c609719bSwdenk } 1041c609719bSwdenk } 1042597f6c26SJames Yang #ifdef CONFIG_CMDLINE_EDITING 1043597f6c26SJames Yang } 1044597f6c26SJames Yang #endif 1045c609719bSwdenk } 1046c609719bSwdenk 1047c609719bSwdenk /****************************************************************************/ 1048c609719bSwdenk 1049c609719bSwdenk static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen) 1050c609719bSwdenk { 1051c609719bSwdenk char *s; 1052c609719bSwdenk 1053c609719bSwdenk if (*np == 0) { 1054c609719bSwdenk return (p); 1055c609719bSwdenk } 1056c609719bSwdenk 1057c609719bSwdenk if (*(--p) == '\t') { /* will retype the whole line */ 1058c609719bSwdenk while (*colp > plen) { 1059c609719bSwdenk puts (erase_seq); 1060c609719bSwdenk (*colp)--; 1061c609719bSwdenk } 1062c609719bSwdenk for (s=buffer; s<p; ++s) { 1063c609719bSwdenk if (*s == '\t') { 1064c609719bSwdenk puts (tab_seq+((*colp) & 07)); 1065c609719bSwdenk *colp += 8 - ((*colp) & 07); 1066c609719bSwdenk } else { 1067c609719bSwdenk ++(*colp); 1068c609719bSwdenk putc (*s); 1069c609719bSwdenk } 1070c609719bSwdenk } 1071c609719bSwdenk } else { 1072c609719bSwdenk puts (erase_seq); 1073c609719bSwdenk (*colp)--; 1074c609719bSwdenk } 1075c609719bSwdenk (*np)--; 1076c609719bSwdenk return (p); 1077c609719bSwdenk } 1078c609719bSwdenk 1079c609719bSwdenk /****************************************************************************/ 1080c609719bSwdenk 1081c609719bSwdenk int parse_line (char *line, char *argv[]) 1082c609719bSwdenk { 1083c609719bSwdenk int nargs = 0; 1084c609719bSwdenk 1085c609719bSwdenk #ifdef DEBUG_PARSER 1086c609719bSwdenk printf ("parse_line: \"%s\"\n", line); 1087c609719bSwdenk #endif 10886d0f6bcfSJean-Christophe PLAGNIOL-VILLARD while (nargs < CONFIG_SYS_MAXARGS) { 1089c609719bSwdenk 1090c609719bSwdenk /* skip any white space */ 1091c609719bSwdenk while ((*line == ' ') || (*line == '\t')) { 1092c609719bSwdenk ++line; 1093c609719bSwdenk } 1094c609719bSwdenk 1095c609719bSwdenk if (*line == '\0') { /* end of line, no more args */ 1096c609719bSwdenk argv[nargs] = NULL; 1097c609719bSwdenk #ifdef DEBUG_PARSER 1098c609719bSwdenk printf ("parse_line: nargs=%d\n", nargs); 1099c609719bSwdenk #endif 1100c609719bSwdenk return (nargs); 1101c609719bSwdenk } 1102c609719bSwdenk 1103c609719bSwdenk argv[nargs++] = line; /* begin of argument string */ 1104c609719bSwdenk 1105c609719bSwdenk /* find end of string */ 1106c609719bSwdenk while (*line && (*line != ' ') && (*line != '\t')) { 1107c609719bSwdenk ++line; 1108c609719bSwdenk } 1109c609719bSwdenk 1110c609719bSwdenk if (*line == '\0') { /* end of line, no more args */ 1111c609719bSwdenk argv[nargs] = NULL; 1112c609719bSwdenk #ifdef DEBUG_PARSER 1113c609719bSwdenk printf ("parse_line: nargs=%d\n", nargs); 1114c609719bSwdenk #endif 1115c609719bSwdenk return (nargs); 1116c609719bSwdenk } 1117c609719bSwdenk 1118c609719bSwdenk *line++ = '\0'; /* terminate current arg */ 1119c609719bSwdenk } 1120c609719bSwdenk 11216d0f6bcfSJean-Christophe PLAGNIOL-VILLARD printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS); 1122c609719bSwdenk 1123c609719bSwdenk #ifdef DEBUG_PARSER 1124c609719bSwdenk printf ("parse_line: nargs=%d\n", nargs); 1125c609719bSwdenk #endif 1126c609719bSwdenk return (nargs); 1127c609719bSwdenk } 1128c609719bSwdenk 1129c609719bSwdenk /****************************************************************************/ 1130c609719bSwdenk 1131c609719bSwdenk static void process_macros (const char *input, char *output) 1132c609719bSwdenk { 1133c609719bSwdenk char c, prev; 1134c609719bSwdenk const char *varname_start = NULL; 1135c609719bSwdenk int inputcnt = strlen (input); 11366d0f6bcfSJean-Christophe PLAGNIOL-VILLARD int outputcnt = CONFIG_SYS_CBSIZE; 1137c609719bSwdenk int state = 0; /* 0 = waiting for '$' */ 113819973b6aSWolfgang Denk 11395cf91d6bSwdenk /* 1 = waiting for '(' or '{' */ 11405cf91d6bSwdenk /* 2 = waiting for ')' or '}' */ 1141a25f862bSwdenk /* 3 = waiting for ''' */ 1142c609719bSwdenk #ifdef DEBUG_PARSER 1143c609719bSwdenk char *output_start = output; 1144c609719bSwdenk 114519973b6aSWolfgang Denk printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input), 114619973b6aSWolfgang Denk input); 1147c609719bSwdenk #endif 1148c609719bSwdenk 1149c609719bSwdenk prev = '\0'; /* previous character */ 1150c609719bSwdenk 1151c609719bSwdenk while (inputcnt && outputcnt) { 1152c609719bSwdenk c = *input++; 1153c609719bSwdenk inputcnt--; 1154c609719bSwdenk 1155a25f862bSwdenk if (state != 3) { 1156c609719bSwdenk /* remove one level of escape characters */ 1157c609719bSwdenk if ((c == '\\') && (prev != '\\')) { 1158c609719bSwdenk if (inputcnt-- == 0) 1159c609719bSwdenk break; 1160c609719bSwdenk prev = c; 1161c609719bSwdenk c = *input++; 1162c609719bSwdenk } 1163a25f862bSwdenk } 1164c609719bSwdenk 1165c609719bSwdenk switch (state) { 1166c609719bSwdenk case 0: /* Waiting for (unescaped) $ */ 1167a25f862bSwdenk if ((c == '\'') && (prev != '\\')) { 1168a25f862bSwdenk state = 3; 1169a25f862bSwdenk break; 1170a25f862bSwdenk } 1171c609719bSwdenk if ((c == '$') && (prev != '\\')) { 1172c609719bSwdenk state++; 1173c609719bSwdenk } else { 1174c609719bSwdenk *(output++) = c; 1175c609719bSwdenk outputcnt--; 1176c609719bSwdenk } 1177c609719bSwdenk break; 1178c609719bSwdenk case 1: /* Waiting for ( */ 11795cf91d6bSwdenk if (c == '(' || c == '{') { 1180c609719bSwdenk state++; 1181c609719bSwdenk varname_start = input; 1182c609719bSwdenk } else { 1183c609719bSwdenk state = 0; 1184c609719bSwdenk *(output++) = '$'; 1185c609719bSwdenk outputcnt--; 1186c609719bSwdenk 1187c609719bSwdenk if (outputcnt) { 1188c609719bSwdenk *(output++) = c; 1189c609719bSwdenk outputcnt--; 1190c609719bSwdenk } 1191c609719bSwdenk } 1192c609719bSwdenk break; 1193c609719bSwdenk case 2: /* Waiting for ) */ 11945cf91d6bSwdenk if (c == ')' || c == '}') { 1195c609719bSwdenk int i; 11966d0f6bcfSJean-Christophe PLAGNIOL-VILLARD char envname[CONFIG_SYS_CBSIZE], *envval; 1197c609719bSwdenk int envcnt = input - varname_start - 1; /* Varname # of chars */ 1198c609719bSwdenk 1199c609719bSwdenk /* Get the varname */ 1200c609719bSwdenk for (i = 0; i < envcnt; i++) { 1201c609719bSwdenk envname[i] = varname_start[i]; 1202c609719bSwdenk } 1203c609719bSwdenk envname[i] = 0; 1204c609719bSwdenk 1205c609719bSwdenk /* Get its value */ 1206c609719bSwdenk envval = getenv (envname); 1207c609719bSwdenk 1208c609719bSwdenk /* Copy into the line if it exists */ 1209c609719bSwdenk if (envval != NULL) 1210c609719bSwdenk while ((*envval) && outputcnt) { 1211c609719bSwdenk *(output++) = *(envval++); 1212c609719bSwdenk outputcnt--; 1213c609719bSwdenk } 1214c609719bSwdenk /* Look for another '$' */ 1215c609719bSwdenk state = 0; 1216c609719bSwdenk } 1217c609719bSwdenk break; 1218a25f862bSwdenk case 3: /* Waiting for ' */ 1219a25f862bSwdenk if ((c == '\'') && (prev != '\\')) { 1220a25f862bSwdenk state = 0; 1221a25f862bSwdenk } else { 1222a25f862bSwdenk *(output++) = c; 1223a25f862bSwdenk outputcnt--; 1224c609719bSwdenk } 1225a25f862bSwdenk break; 1226a25f862bSwdenk } 1227c609719bSwdenk prev = c; 1228c609719bSwdenk } 1229c609719bSwdenk 1230c609719bSwdenk if (outputcnt) 1231c609719bSwdenk *output = 0; 12329160b96fSBartlomiej Sieka else 12339160b96fSBartlomiej Sieka *(output - 1) = 0; 1234c609719bSwdenk 1235c609719bSwdenk #ifdef DEBUG_PARSER 1236c609719bSwdenk printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", 1237c609719bSwdenk strlen (output_start), output_start); 1238c609719bSwdenk #endif 1239c609719bSwdenk } 1240c609719bSwdenk 1241c609719bSwdenk /**************************************************************************** 1242c609719bSwdenk * returns: 1243c609719bSwdenk * 1 - command executed, repeatable 1244c609719bSwdenk * 0 - command executed but not repeatable, interrupted commands are 1245c609719bSwdenk * always considered not repeatable 1246c609719bSwdenk * -1 - not executed (unrecognized, bootd recursion or too many args) 12476d0f6bcfSJean-Christophe PLAGNIOL-VILLARD * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is 1248c609719bSwdenk * considered unrecognized) 1249c609719bSwdenk * 1250c609719bSwdenk * WARNING: 1251c609719bSwdenk * 1252c609719bSwdenk * We must create a temporary copy of the command since the command we get 1253c609719bSwdenk * may be the result from getenv(), which returns a pointer directly to 1254c609719bSwdenk * the environment data, which may change magicly when the command we run 1255c609719bSwdenk * creates or modifies environment variables (like "bootp" does). 1256c609719bSwdenk */ 1257c609719bSwdenk 1258c609719bSwdenk int run_command (const char *cmd, int flag) 1259c609719bSwdenk { 1260c609719bSwdenk cmd_tbl_t *cmdtp; 12616d0f6bcfSJean-Christophe PLAGNIOL-VILLARD char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */ 1262c609719bSwdenk char *token; /* start of token in cmdbuf */ 1263c609719bSwdenk char *sep; /* end of token (separator) in cmdbuf */ 12646d0f6bcfSJean-Christophe PLAGNIOL-VILLARD char finaltoken[CONFIG_SYS_CBSIZE]; 1265c609719bSwdenk char *str = cmdbuf; 12666d0f6bcfSJean-Christophe PLAGNIOL-VILLARD char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ 1267f07771ccSwdenk int argc, inquotes; 1268c609719bSwdenk int repeatable = 1; 1269f07771ccSwdenk int rc = 0; 1270c609719bSwdenk 1271c609719bSwdenk #ifdef DEBUG_PARSER 1272c609719bSwdenk printf ("[RUN_COMMAND] cmd[%p]=\"", cmd); 1273c609719bSwdenk puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */ 1274c609719bSwdenk puts ("\"\n"); 1275c609719bSwdenk #endif 1276c609719bSwdenk 1277c609719bSwdenk clear_ctrlc(); /* forget any previous Control C */ 1278c609719bSwdenk 1279c609719bSwdenk if (!cmd || !*cmd) { 1280c609719bSwdenk return -1; /* empty command */ 1281c609719bSwdenk } 1282c609719bSwdenk 12836d0f6bcfSJean-Christophe PLAGNIOL-VILLARD if (strlen(cmd) >= CONFIG_SYS_CBSIZE) { 1284c609719bSwdenk puts ("## Command too long!\n"); 1285c609719bSwdenk return -1; 1286c609719bSwdenk } 1287c609719bSwdenk 1288c609719bSwdenk strcpy (cmdbuf, cmd); 1289c609719bSwdenk 1290c609719bSwdenk /* Process separators and check for invalid 1291c609719bSwdenk * repeatable commands 1292c609719bSwdenk */ 1293c609719bSwdenk 1294c609719bSwdenk #ifdef DEBUG_PARSER 1295c609719bSwdenk printf ("[PROCESS_SEPARATORS] %s\n", cmd); 1296c609719bSwdenk #endif 1297c609719bSwdenk while (*str) { 1298c609719bSwdenk 1299c609719bSwdenk /* 1300c609719bSwdenk * Find separator, or string end 1301c609719bSwdenk * Allow simple escape of ';' by writing "\;" 1302c609719bSwdenk */ 1303a25f862bSwdenk for (inquotes = 0, sep = str; *sep; sep++) { 1304a25f862bSwdenk if ((*sep=='\'') && 1305a25f862bSwdenk (*(sep-1) != '\\')) 1306a25f862bSwdenk inquotes=!inquotes; 1307a25f862bSwdenk 1308a25f862bSwdenk if (!inquotes && 1309a25f862bSwdenk (*sep == ';') && /* separator */ 1310c609719bSwdenk ( sep != str) && /* past string start */ 1311c609719bSwdenk (*(sep-1) != '\\')) /* and NOT escaped */ 1312c609719bSwdenk break; 1313c609719bSwdenk } 1314c609719bSwdenk 1315c609719bSwdenk /* 1316c609719bSwdenk * Limit the token to data between separators 1317c609719bSwdenk */ 1318c609719bSwdenk token = str; 1319c609719bSwdenk if (*sep) { 1320c609719bSwdenk str = sep + 1; /* start of command for next pass */ 1321c609719bSwdenk *sep = '\0'; 1322c609719bSwdenk } 1323c609719bSwdenk else 1324c609719bSwdenk str = sep; /* no more commands for next pass */ 1325c609719bSwdenk #ifdef DEBUG_PARSER 1326c609719bSwdenk printf ("token: \"%s\"\n", token); 1327c609719bSwdenk #endif 1328c609719bSwdenk 1329c609719bSwdenk /* find macros in this token and replace them */ 1330c609719bSwdenk process_macros (token, finaltoken); 1331c609719bSwdenk 1332c609719bSwdenk /* Extract arguments */ 13331264b405SWolfgang Denk if ((argc = parse_line (finaltoken, argv)) == 0) { 13341264b405SWolfgang Denk rc = -1; /* no command at all */ 13351264b405SWolfgang Denk continue; 13361264b405SWolfgang Denk } 1337c609719bSwdenk 1338c609719bSwdenk /* Look up command in command table */ 1339c609719bSwdenk if ((cmdtp = find_cmd(argv[0])) == NULL) { 1340c609719bSwdenk printf ("Unknown command '%s' - try 'help'\n", argv[0]); 1341f07771ccSwdenk rc = -1; /* give up after bad command */ 1342f07771ccSwdenk continue; 1343c609719bSwdenk } 1344c609719bSwdenk 1345c609719bSwdenk /* found - check max args */ 1346c609719bSwdenk if (argc > cmdtp->maxargs) { 134762c3ae7cSPeter Tyser cmd_usage(cmdtp); 1348f07771ccSwdenk rc = -1; 1349f07771ccSwdenk continue; 1350c609719bSwdenk } 1351c609719bSwdenk 1352c3517f91SJon Loeliger #if defined(CONFIG_CMD_BOOTD) 1353c609719bSwdenk /* avoid "bootd" recursion */ 1354c609719bSwdenk if (cmdtp->cmd == do_bootd) { 1355c609719bSwdenk #ifdef DEBUG_PARSER 1356c609719bSwdenk printf ("[%s]\n", finaltoken); 1357c609719bSwdenk #endif 1358c609719bSwdenk if (flag & CMD_FLAG_BOOTD) { 13594b9206edSwdenk puts ("'bootd' recursion detected\n"); 1360f07771ccSwdenk rc = -1; 1361f07771ccSwdenk continue; 13621264b405SWolfgang Denk } else { 1363c609719bSwdenk flag |= CMD_FLAG_BOOTD; 1364c609719bSwdenk } 13651264b405SWolfgang Denk } 136690253178SJon Loeliger #endif 1367c609719bSwdenk 1368c609719bSwdenk /* OK - call function to do the command */ 1369c609719bSwdenk if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) { 1370f07771ccSwdenk rc = -1; 1371c609719bSwdenk } 1372c609719bSwdenk 1373c609719bSwdenk repeatable &= cmdtp->repeatable; 1374c609719bSwdenk 1375c609719bSwdenk /* Did the user stop this? */ 1376c609719bSwdenk if (had_ctrlc ()) 13775afb2020SDetlev Zundel return -1; /* if stopped then not repeatable */ 1378c609719bSwdenk } 1379c609719bSwdenk 1380f07771ccSwdenk return rc ? rc : repeatable; 1381c609719bSwdenk } 1382c609719bSwdenk 1383c609719bSwdenk /****************************************************************************/ 1384c609719bSwdenk 1385c3517f91SJon Loeliger #if defined(CONFIG_CMD_RUN) 138654841ab5SWolfgang Denk int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) 1387c609719bSwdenk { 1388c609719bSwdenk int i; 1389c609719bSwdenk 139047e26b1bSWolfgang Denk if (argc < 2) 139147e26b1bSWolfgang Denk return cmd_usage(cmdtp); 1392c609719bSwdenk 1393c609719bSwdenk for (i=1; i<argc; ++i) { 13943e38691eSwdenk char *arg; 13953e38691eSwdenk 13963e38691eSwdenk if ((arg = getenv (argv[i])) == NULL) { 13973e38691eSwdenk printf ("## Error: \"%s\" not defined\n", argv[i]); 13983e38691eSwdenk return 1; 13993e38691eSwdenk } 14006d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER 14013e38691eSwdenk if (run_command (arg, flag) == -1) 14023e38691eSwdenk return 1; 1403c609719bSwdenk #else 14043e38691eSwdenk if (parse_string_outer(arg, 14057aa78614Swdenk FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0) 14063e38691eSwdenk return 1; 1407c609719bSwdenk #endif 1408c609719bSwdenk } 14093e38691eSwdenk return 0; 1410c609719bSwdenk } 141190253178SJon Loeliger #endif 1412