3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <cmd_nvedit.h>
28 #include <cmd_bootm.h>
30 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
31 #include <cmd_boot.h> /* for do_reset() prototype */
34 #ifdef CFG_HUSH_PARSER
38 #define MAX_DELAY_STOP_STR 32
40 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
41 static int parse_line (char *, char *[]);
42 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
43 static int abortboot(int);
48 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
50 static char erase_seq[] = "\b \b"; /* erase sequence */
51 static char tab_seq[] = " "; /* used to expand TABs */
53 #ifdef CONFIG_BOOT_RETRY_TIME
54 static uint64_t endtime = 0; /* must be set, default is instant timeout */
55 static int retry_time = -1; /* -1 so can call readline before main_loop */
58 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
60 #ifndef CONFIG_BOOT_RETRY_MIN
61 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
64 #ifdef CONFIG_MODEM_SUPPORT
66 extern void mdm_init(void); /* defined in board.c */
69 /***************************************************************************
70 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
71 * returns: 0 - no key string, allow autoboot
72 * 1 - got key string, abort
74 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
75 # if defined(CONFIG_AUTOBOOT_KEYED)
76 static __inline__ int abortboot(int bootdelay)
79 uint64_t etime = endtick(bootdelay);
88 { str: getenv ("bootdelaykey"), retry: 1 },
89 { str: getenv ("bootdelaykey2"), retry: 1 },
90 { str: getenv ("bootstopkey"), retry: 0 },
91 { str: getenv ("bootstopkey2"), retry: 0 },
94 char presskey [MAX_DELAY_STOP_STR];
95 u_int presskey_len = 0;
96 u_int presskey_max = 0;
99 # ifdef CONFIG_AUTOBOOT_PROMPT
100 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
103 # ifdef CONFIG_AUTOBOOT_DELAY_STR
104 if (delaykey[0].str == NULL)
105 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
107 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
108 if (delaykey[1].str == NULL)
109 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
111 # ifdef CONFIG_AUTOBOOT_STOP_STR
112 if (delaykey[2].str == NULL)
113 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
115 # ifdef CONFIG_AUTOBOOT_STOP_STR2
116 if (delaykey[3].str == NULL)
117 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
120 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
121 delaykey[i].len = delaykey[i].str == NULL ?
122 0 : strlen (delaykey[i].str);
123 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
124 MAX_DELAY_STOP_STR : delaykey[i].len;
126 presskey_max = presskey_max > delaykey[i].len ?
127 presskey_max : delaykey[i].len;
130 printf("%s key:<%s>\n",
131 delaykey[i].retry ? "delay" : "stop",
132 delaykey[i].str ? delaykey[i].str : "NULL");
136 /* In order to keep up with incoming data, check timeout only
139 while (!abort && get_ticks() <= etime) {
140 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
141 if (delaykey[i].len > 0 &&
142 presskey_len >= delaykey[i].len &&
143 memcmp (presskey + presskey_len - delaykey[i].len,
145 delaykey[i].len) == 0) {
147 printf("got %skey\n",
148 delaykey[i].retry ? "delay" : "stop");
151 # ifdef CONFIG_BOOT_RETRY_TIME
152 /* don't retry auto boot */
153 if (! delaykey[i].retry)
161 if (presskey_len < presskey_max) {
162 presskey [presskey_len ++] = getc();
165 for (i = 0; i < presskey_max - 1; i ++)
166 presskey [i] = presskey [i + 1];
168 presskey [i] = getc();
174 printf("key timeout\n");
180 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
182 static __inline__ int abortboot(int bootdelay)
186 printf("Hit any key to stop autoboot: %2d ", bootdelay);
188 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
190 * Check if key already pressed
191 * Don't check if bootdelay < 0
193 if (bootdelay >= 0) {
194 if (tstc()) { /* we got a key press */
195 (void) getc(); /* consume input */
196 printf ("\b\b\b 0\n");
197 return 1; /* don't auto boot */
202 while (bootdelay > 0) {
206 /* delay 100 * 10ms */
207 for (i=0; !abort && i<100; ++i) {
208 if (tstc()) { /* we got a key press */
209 abort = 1; /* don't auto boot */
210 bootdelay = 0; /* no more delay */
211 (void) getc(); /* consume input */
217 printf ("\b\b\b%2d ", bootdelay);
224 # endif /* CONFIG_AUTOBOOT_KEYED */
225 #endif /* CONFIG_BOOTDELAY >= 0 */
227 /****************************************************************************/
229 void main_loop (void)
231 #ifndef CFG_HUSH_PARSER
232 static char lastcommand[CFG_CBSIZE] = { 0, };
238 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
242 #ifdef CONFIG_PREBOOT
246 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
247 ulong bmp = 0; /* default bitmap */
248 extern int trab_vfd (ulong bitmap);
250 #ifdef CONFIG_MODEM_SUPPORT
252 bmp = 1; /* alternate bitmap */
255 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
257 #ifdef CONFIG_MODEM_SUPPORT
258 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
260 uchar *str = strdup(getenv("mdm_cmd"));
261 setenv ("preboot", str); /* set or delete definition */
264 mdm_init(); /* wait for modem connection */
266 #endif /* CONFIG_MODEM_SUPPORT */
268 #ifdef CFG_HUSH_PARSER
269 u_boot_hush_start ();
272 #ifdef CONFIG_PREBOOT
273 if ((p = getenv ("preboot")) != NULL) {
274 # ifdef CONFIG_AUTOBOOT_KEYED
275 int prev = disable_ctrlc(1); /* disable Control C checking */
278 # ifndef CFG_HUSH_PARSER
281 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
282 FLAG_EXIT_FROM_LOOP);
285 # ifdef CONFIG_AUTOBOOT_KEYED
286 disable_ctrlc(prev); /* restore Control C checking */
289 #endif /* CONFIG_PREBOOT */
291 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
292 s = getenv ("bootdelay");
293 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
296 printf ("### main_loop entered:\n\n");
299 # ifdef CONFIG_BOOT_RETRY_TIME
300 s = getenv ("bootretry");
302 retry_time = (int)simple_strtoul(s, NULL, 10);
304 retry_time = CONFIG_BOOT_RETRY_TIME;
305 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
306 retry_time = CONFIG_BOOT_RETRY_MIN;
307 # endif /* CONFIG_BOOT_RETRY_TIME */
309 s = getenv ("bootcmd");
310 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
311 # ifdef CONFIG_AUTOBOOT_KEYED
312 int prev = disable_ctrlc(1); /* disable Control C checking */
315 # ifndef CFG_HUSH_PARSER
318 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
319 FLAG_EXIT_FROM_LOOP);
322 # ifdef CONFIG_AUTOBOOT_KEYED
323 disable_ctrlc(prev); /* restore Control C checking */
326 #endif /* CONFIG_BOOTDELAY */
329 * Main Loop for Monitor Command Processing
331 #ifdef CFG_HUSH_PARSER
333 /* This point is never reached */
337 #ifdef CONFIG_BOOT_RETRY_TIME
339 /* Saw enough of a valid command to
340 * restart the timeout.
345 len = readline (CFG_PROMPT);
347 flag = 0; /* assume no special flags for now */
349 strcpy (lastcommand, console_buffer);
351 flag |= CMD_FLAG_REPEAT;
352 #ifdef CONFIG_BOOT_RETRY_TIME
353 else if (len == -2) {
354 /* -2 means timed out, retry autoboot
356 printf("\nTimed out waiting for command\n");
357 # ifdef CONFIG_RESET_TO_RETRY
358 /* Reinit board to run initialization code again */
359 do_reset (NULL, 0, 0, NULL);
361 return; /* retry autoboot */
367 printf ("<INTERRUPT>\n");
369 rc = run_command (lastcommand, flag);
372 /* invalid command or not repeatable, forget it */
376 #endif /*CFG_HUSH_PARSER*/
379 /***************************************************************************
380 * reset command line timeout to retry_time seconds
382 #ifdef CONFIG_BOOT_RETRY_TIME
383 void reset_cmd_timeout(void)
385 endtime = endtick(retry_time);
389 /****************************************************************************/
392 * Prompt for input and read a line.
393 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
394 * time out when time goes past endtime (timebase time in ticks).
395 * Return: number of read characters
399 int readline (const char *const prompt)
401 char *p = console_buffer;
402 int n = 0; /* buffer index */
403 int plen = 0; /* prompt length */
404 int col; /* output column cnt */
409 plen = strlen (prompt);
415 #ifdef CONFIG_BOOT_RETRY_TIME
416 while (!tstc()) { /* while no incoming data */
417 if (retry_time >= 0 && get_ticks() > endtime)
418 return (-2); /* timed out */
421 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
423 #ifdef CONFIG_SHOW_ACTIVITY
425 extern void show_activity(int arg);
432 * Special character handling
435 case '\r': /* Enter */
439 return (p - console_buffer);
441 case 0x03: /* ^C - break */
442 console_buffer[0] = '\0'; /* discard input */
445 case 0x15: /* ^U - erase line */
454 case 0x17: /* ^W - erase word */
455 p=delete_char(console_buffer, p, &col, &n, plen);
456 while ((n > 0) && (*p != ' ')) {
457 p=delete_char(console_buffer, p, &col, &n, plen);
461 case 0x08: /* ^H - backspace */
462 case 0x7F: /* DEL - backspace */
463 p=delete_char(console_buffer, p, &col, &n, plen);
468 * Must be a normal character then
470 if (n < CFG_CBSIZE-2) {
471 if (c == '\t') { /* expand TABs */
472 puts (tab_seq+(col&07));
475 ++col; /* echo input */
480 } else { /* Buffer full */
487 /****************************************************************************/
489 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
497 if (*(--p) == '\t') { /* will retype the whole line */
498 while (*colp > plen) {
502 for (s=buffer; s<p; ++s) {
504 puts (tab_seq+((*colp) & 07));
505 *colp += 8 - ((*colp) & 07);
519 /****************************************************************************/
521 int parse_line (char *line, char *argv[])
526 printf ("parse_line: \"%s\"\n", line);
528 while (nargs < CFG_MAXARGS) {
530 /* skip any white space */
531 while ((*line == ' ') || (*line == '\t')) {
535 if (*line == '\0') { /* end of line, no more args */
538 printf ("parse_line: nargs=%d\n", nargs);
543 argv[nargs++] = line; /* begin of argument string */
545 /* find end of string */
546 while (*line && (*line != ' ') && (*line != '\t')) {
550 if (*line == '\0') { /* end of line, no more args */
553 printf ("parse_line: nargs=%d\n", nargs);
558 *line++ = '\0'; /* terminate current arg */
561 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
564 printf ("parse_line: nargs=%d\n", nargs);
569 /****************************************************************************/
571 static void process_macros (const char *input, char *output)
574 const char *varname_start = NULL;
575 int inputcnt = strlen (input);
576 int outputcnt = CFG_CBSIZE;
577 int state = 0; /* 0 = waiting for '$' */
578 /* 1 = waiting for '(' */
579 /* 2 = waiting for ')' */
582 char *output_start = output;
584 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
587 prev = '\0'; /* previous character */
589 while (inputcnt && outputcnt) {
593 /* remove one level of escape characters */
594 if ((c == '\\') && (prev != '\\')) {
602 case 0: /* Waiting for (unescaped) $ */
603 if ((c == '$') && (prev != '\\')) {
610 case 1: /* Waiting for ( */
613 varname_start = input;
625 case 2: /* Waiting for ) */
628 char envname[CFG_CBSIZE], *envval;
629 int envcnt = input-varname_start-1; /* Varname # of chars */
631 /* Get the varname */
632 for (i = 0; i < envcnt; i++) {
633 envname[i] = varname_start[i];
638 envval = getenv (envname);
640 /* Copy into the line if it exists */
642 while ((*envval) && outputcnt) {
643 *(output++) = *(envval++);
646 /* Look for another '$' */
659 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
660 strlen(output_start), output_start);
664 /****************************************************************************
666 * 1 - command executed, repeatable
667 * 0 - command executed but not repeatable, interrupted commands are
668 * always considered not repeatable
669 * -1 - not executed (unrecognized, bootd recursion or too many args)
670 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
671 * considered unrecognized)
675 * We must create a temporary copy of the command since the command we get
676 * may be the result from getenv(), which returns a pointer directly to
677 * the environment data, which may change magicly when the command we run
678 * creates or modifies environment variables (like "bootp" does).
681 int run_command (const char *cmd, int flag)
684 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
685 char *token; /* start of token in cmdbuf */
686 char *sep; /* end of token (separator) in cmdbuf */
687 char finaltoken[CFG_CBSIZE];
689 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
694 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
695 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
699 clear_ctrlc(); /* forget any previous Control C */
702 return -1; /* empty command */
705 if (strlen(cmd) >= CFG_CBSIZE) {
706 puts ("## Command too long!\n");
710 strcpy (cmdbuf, cmd);
712 /* Process separators and check for invalid
713 * repeatable commands
717 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
722 * Find separator, or string end
723 * Allow simple escape of ';' by writing "\;"
725 for (sep = str; *sep; sep++) {
726 if ((*sep == ';') && /* separator */
727 ( sep != str) && /* past string start */
728 (*(sep-1) != '\\')) /* and NOT escaped */
733 * Limit the token to data between separators
737 str = sep + 1; /* start of command for next pass */
741 str = sep; /* no more commands for next pass */
743 printf ("token: \"%s\"\n", token);
746 /* find macros in this token and replace them */
747 process_macros (token, finaltoken);
749 /* Extract arguments */
750 argc = parse_line (finaltoken, argv);
752 /* Look up command in command table */
753 if ((cmdtp = find_cmd(argv[0])) == NULL) {
754 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
755 return -1; /* give up after bad command */
758 /* found - check max args */
759 if (argc > cmdtp->maxargs) {
760 printf ("Usage:\n%s\n", cmdtp->usage);
764 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
765 /* avoid "bootd" recursion */
766 if (cmdtp->cmd == do_bootd) {
768 printf ("[%s]\n", finaltoken);
770 if (flag & CMD_FLAG_BOOTD) {
771 printf ("'bootd' recursion detected\n");
775 flag |= CMD_FLAG_BOOTD;
777 #endif /* CFG_CMD_BOOTD */
779 /* OK - call function to do the command */
780 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
784 repeatable &= cmdtp->repeatable;
786 /* Did the user stop this? */
788 return 0; /* if stopped then not repeatable */
794 /****************************************************************************/
796 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
797 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
803 printf ("Usage:\n%s\n", cmdtp->usage);
807 for (i=1; i<argc; ++i) {
808 #ifndef CFG_HUSH_PARSER
809 if (run_command (getenv (argv[i]), flag) != -1) ++rcode;
811 if (parse_string_outer(getenv (argv[i]),
812 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) == 0) ++rcode;
815 return ((rcode == i) ? 0 : 1);