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 #ifdef CONFIG_MENUKEY
183 static int menukey = 0;
186 static __inline__ int abortboot(int bootdelay)
190 #ifdef CONFIG_MENUPROMPT
191 printf(CONFIG_MENUPROMPT, bootdelay);
193 printf("Hit any key to stop autoboot: %2d ", bootdelay);
196 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
198 * Check if key already pressed
199 * Don't check if bootdelay < 0
201 if (bootdelay >= 0) {
202 if (tstc()) { /* we got a key press */
203 (void) getc(); /* consume input */
204 printf ("\b\b\b 0\n");
205 return 1; /* don't auto boot */
210 while (bootdelay > 0) {
214 /* delay 100 * 10ms */
215 for (i=0; !abort && i<100; ++i) {
216 if (tstc()) { /* we got a key press */
217 abort = 1; /* don't auto boot */
218 bootdelay = 0; /* no more delay */
219 # ifdef CONFIG_MENUKEY
222 (void) getc(); /* consume input */
229 printf ("\b\b\b%2d ", bootdelay);
236 # endif /* CONFIG_AUTOBOOT_KEYED */
237 #endif /* CONFIG_BOOTDELAY >= 0 */
239 /****************************************************************************/
241 void main_loop (void)
243 #ifndef CFG_HUSH_PARSER
244 static char lastcommand[CFG_CBSIZE] = { 0, };
250 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
254 #ifdef CONFIG_PREBOOT
258 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
259 ulong bmp = 0; /* default bitmap */
260 extern int trab_vfd (ulong bitmap);
262 #ifdef CONFIG_MODEM_SUPPORT
264 bmp = 1; /* alternate bitmap */
267 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
269 #ifdef CONFIG_MODEM_SUPPORT
270 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
272 uchar *str = strdup(getenv("mdm_cmd"));
273 setenv ("preboot", str); /* set or delete definition */
276 mdm_init(); /* wait for modem connection */
278 #endif /* CONFIG_MODEM_SUPPORT */
280 #ifdef CFG_HUSH_PARSER
281 u_boot_hush_start ();
284 #ifdef CONFIG_PREBOOT
285 if ((p = getenv ("preboot")) != NULL) {
286 # ifdef CONFIG_AUTOBOOT_KEYED
287 int prev = disable_ctrlc(1); /* disable Control C checking */
290 # ifndef CFG_HUSH_PARSER
293 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
294 FLAG_EXIT_FROM_LOOP);
297 # ifdef CONFIG_AUTOBOOT_KEYED
298 disable_ctrlc(prev); /* restore Control C checking */
301 #endif /* CONFIG_PREBOOT */
303 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
304 s = getenv ("bootdelay");
305 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
308 printf ("### main_loop entered:\n\n");
311 # ifdef CONFIG_BOOT_RETRY_TIME
312 s = getenv ("bootretry");
314 retry_time = (int)simple_strtoul(s, NULL, 10);
316 retry_time = CONFIG_BOOT_RETRY_TIME;
317 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
318 retry_time = CONFIG_BOOT_RETRY_MIN;
319 # endif /* CONFIG_BOOT_RETRY_TIME */
321 s = getenv ("bootcmd");
322 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
323 # ifdef CONFIG_AUTOBOOT_KEYED
324 int prev = disable_ctrlc(1); /* disable Control C checking */
327 # ifndef CFG_HUSH_PARSER
330 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
331 FLAG_EXIT_FROM_LOOP);
334 # ifdef CONFIG_AUTOBOOT_KEYED
335 disable_ctrlc(prev); /* restore Control C checking */
339 # ifdef CONFIG_MENUKEY
340 if (menukey == CONFIG_MENUKEY)
342 s = getenv("menucmd");
345 # ifndef CFG_HUSH_PARSER
346 run_command (s, bd, 0);
348 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
349 FLAG_EXIT_FROM_LOOP);
353 #endif /* CONFIG_MENUKEY */
354 #endif /* CONFIG_BOOTDELAY */
356 #ifdef CONFIG_AMIGAONEG3SE
358 extern void video_banner(void);
364 * Main Loop for Monitor Command Processing
366 #ifdef CFG_HUSH_PARSER
368 /* This point is never reached */
372 #ifdef CONFIG_BOOT_RETRY_TIME
374 /* Saw enough of a valid command to
375 * restart the timeout.
380 len = readline (CFG_PROMPT);
382 flag = 0; /* assume no special flags for now */
384 strcpy (lastcommand, console_buffer);
386 flag |= CMD_FLAG_REPEAT;
387 #ifdef CONFIG_BOOT_RETRY_TIME
388 else if (len == -2) {
389 /* -2 means timed out, retry autoboot
391 printf("\nTimed out waiting for command\n");
392 # ifdef CONFIG_RESET_TO_RETRY
393 /* Reinit board to run initialization code again */
394 do_reset (NULL, 0, 0, NULL);
396 return; /* retry autoboot */
402 printf ("<INTERRUPT>\n");
404 rc = run_command (lastcommand, flag);
407 /* invalid command or not repeatable, forget it */
411 #endif /*CFG_HUSH_PARSER*/
414 /***************************************************************************
415 * reset command line timeout to retry_time seconds
417 #ifdef CONFIG_BOOT_RETRY_TIME
418 void reset_cmd_timeout(void)
420 endtime = endtick(retry_time);
424 /****************************************************************************/
427 * Prompt for input and read a line.
428 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
429 * time out when time goes past endtime (timebase time in ticks).
430 * Return: number of read characters
434 int readline (const char *const prompt)
436 char *p = console_buffer;
437 int n = 0; /* buffer index */
438 int plen = 0; /* prompt length */
439 int col; /* output column cnt */
444 plen = strlen (prompt);
450 #ifdef CONFIG_BOOT_RETRY_TIME
451 while (!tstc()) { /* while no incoming data */
452 if (retry_time >= 0 && get_ticks() > endtime)
453 return (-2); /* timed out */
456 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
458 #ifdef CONFIG_SHOW_ACTIVITY
460 extern void show_activity(int arg);
467 * Special character handling
470 case '\r': /* Enter */
474 return (p - console_buffer);
476 case 0x03: /* ^C - break */
477 console_buffer[0] = '\0'; /* discard input */
480 case 0x15: /* ^U - erase line */
489 case 0x17: /* ^W - erase word */
490 p=delete_char(console_buffer, p, &col, &n, plen);
491 while ((n > 0) && (*p != ' ')) {
492 p=delete_char(console_buffer, p, &col, &n, plen);
496 case 0x08: /* ^H - backspace */
497 case 0x7F: /* DEL - backspace */
498 p=delete_char(console_buffer, p, &col, &n, plen);
503 * Must be a normal character then
505 if (n < CFG_CBSIZE-2) {
506 if (c == '\t') { /* expand TABs */
507 puts (tab_seq+(col&07));
510 ++col; /* echo input */
515 } else { /* Buffer full */
522 /****************************************************************************/
524 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
532 if (*(--p) == '\t') { /* will retype the whole line */
533 while (*colp > plen) {
537 for (s=buffer; s<p; ++s) {
539 puts (tab_seq+((*colp) & 07));
540 *colp += 8 - ((*colp) & 07);
554 /****************************************************************************/
556 int parse_line (char *line, char *argv[])
561 printf ("parse_line: \"%s\"\n", line);
563 while (nargs < CFG_MAXARGS) {
565 /* skip any white space */
566 while ((*line == ' ') || (*line == '\t')) {
570 if (*line == '\0') { /* end of line, no more args */
573 printf ("parse_line: nargs=%d\n", nargs);
578 argv[nargs++] = line; /* begin of argument string */
580 /* find end of string */
581 while (*line && (*line != ' ') && (*line != '\t')) {
585 if (*line == '\0') { /* end of line, no more args */
588 printf ("parse_line: nargs=%d\n", nargs);
593 *line++ = '\0'; /* terminate current arg */
596 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
599 printf ("parse_line: nargs=%d\n", nargs);
604 /****************************************************************************/
606 static void process_macros (const char *input, char *output)
609 const char *varname_start = NULL;
610 int inputcnt = strlen (input);
611 int outputcnt = CFG_CBSIZE;
612 int state = 0; /* 0 = waiting for '$' */
613 /* 1 = waiting for '(' */
614 /* 2 = waiting for ')' */
617 char *output_start = output;
619 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
622 prev = '\0'; /* previous character */
624 while (inputcnt && outputcnt) {
628 /* remove one level of escape characters */
629 if ((c == '\\') && (prev != '\\')) {
637 case 0: /* Waiting for (unescaped) $ */
638 if ((c == '$') && (prev != '\\')) {
645 case 1: /* Waiting for ( */
648 varname_start = input;
660 case 2: /* Waiting for ) */
663 char envname[CFG_CBSIZE], *envval;
664 int envcnt = input-varname_start-1; /* Varname # of chars */
666 /* Get the varname */
667 for (i = 0; i < envcnt; i++) {
668 envname[i] = varname_start[i];
673 envval = getenv (envname);
675 /* Copy into the line if it exists */
677 while ((*envval) && outputcnt) {
678 *(output++) = *(envval++);
681 /* Look for another '$' */
694 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
695 strlen(output_start), output_start);
699 /****************************************************************************
701 * 1 - command executed, repeatable
702 * 0 - command executed but not repeatable, interrupted commands are
703 * always considered not repeatable
704 * -1 - not executed (unrecognized, bootd recursion or too many args)
705 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
706 * considered unrecognized)
710 * We must create a temporary copy of the command since the command we get
711 * may be the result from getenv(), which returns a pointer directly to
712 * the environment data, which may change magicly when the command we run
713 * creates or modifies environment variables (like "bootp" does).
716 int run_command (const char *cmd, int flag)
719 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
720 char *token; /* start of token in cmdbuf */
721 char *sep; /* end of token (separator) in cmdbuf */
722 char finaltoken[CFG_CBSIZE];
724 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
729 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
730 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
734 clear_ctrlc(); /* forget any previous Control C */
737 return -1; /* empty command */
740 if (strlen(cmd) >= CFG_CBSIZE) {
741 puts ("## Command too long!\n");
745 strcpy (cmdbuf, cmd);
747 /* Process separators and check for invalid
748 * repeatable commands
752 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
757 * Find separator, or string end
758 * Allow simple escape of ';' by writing "\;"
760 for (sep = str; *sep; sep++) {
761 if ((*sep == ';') && /* separator */
762 ( sep != str) && /* past string start */
763 (*(sep-1) != '\\')) /* and NOT escaped */
768 * Limit the token to data between separators
772 str = sep + 1; /* start of command for next pass */
776 str = sep; /* no more commands for next pass */
778 printf ("token: \"%s\"\n", token);
781 /* find macros in this token and replace them */
782 process_macros (token, finaltoken);
784 /* Extract arguments */
785 argc = parse_line (finaltoken, argv);
787 /* Look up command in command table */
788 if ((cmdtp = find_cmd(argv[0])) == NULL) {
789 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
790 return -1; /* give up after bad command */
793 /* found - check max args */
794 if (argc > cmdtp->maxargs) {
795 printf ("Usage:\n%s\n", cmdtp->usage);
799 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
800 /* avoid "bootd" recursion */
801 if (cmdtp->cmd == do_bootd) {
803 printf ("[%s]\n", finaltoken);
805 if (flag & CMD_FLAG_BOOTD) {
806 printf ("'bootd' recursion detected\n");
810 flag |= CMD_FLAG_BOOTD;
812 #endif /* CFG_CMD_BOOTD */
814 /* OK - call function to do the command */
815 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
819 repeatable &= cmdtp->repeatable;
821 /* Did the user stop this? */
823 return 0; /* if stopped then not repeatable */
829 /****************************************************************************/
831 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
832 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
838 printf ("Usage:\n%s\n", cmdtp->usage);
842 for (i=1; i<argc; ++i) {
843 #ifndef CFG_HUSH_PARSER
844 if (run_command (getenv (argv[i]), flag) != -1) ++rcode;
846 if (parse_string_outer(getenv (argv[i]),
847 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) == 0) ++rcode;
850 return ((rcode == i) ? 0 : 1);