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,
31 #ifdef CFG_HUSH_PARSER
37 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
38 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
41 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
44 #define MAX_DELAY_STOP_STR 32
46 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
47 static int parse_line (char *, char *[]);
48 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
49 static int abortboot(int);
54 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
56 static char erase_seq[] = "\b \b"; /* erase sequence */
57 static char tab_seq[] = " "; /* used to expand TABs */
59 #ifdef CONFIG_BOOT_RETRY_TIME
60 static uint64_t endtime = 0; /* must be set, default is instant timeout */
61 static int retry_time = -1; /* -1 so can call readline before main_loop */
64 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
66 #ifndef CONFIG_BOOT_RETRY_MIN
67 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
70 #ifdef CONFIG_MODEM_SUPPORT
72 extern void mdm_init(void); /* defined in board.c */
75 /***************************************************************************
76 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
77 * returns: 0 - no key string, allow autoboot
78 * 1 - got key string, abort
80 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
81 # if defined(CONFIG_AUTOBOOT_KEYED)
82 static __inline__ int abortboot(int bootdelay)
85 uint64_t etime = endtick(bootdelay);
94 { str: getenv ("bootdelaykey"), retry: 1 },
95 { str: getenv ("bootdelaykey2"), retry: 1 },
96 { str: getenv ("bootstopkey"), retry: 0 },
97 { str: getenv ("bootstopkey2"), retry: 0 },
100 char presskey [MAX_DELAY_STOP_STR];
101 u_int presskey_len = 0;
102 u_int presskey_max = 0;
105 # ifdef CONFIG_AUTOBOOT_PROMPT
106 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
109 # ifdef CONFIG_AUTOBOOT_DELAY_STR
110 if (delaykey[0].str == NULL)
111 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
113 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
114 if (delaykey[1].str == NULL)
115 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
117 # ifdef CONFIG_AUTOBOOT_STOP_STR
118 if (delaykey[2].str == NULL)
119 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
121 # ifdef CONFIG_AUTOBOOT_STOP_STR2
122 if (delaykey[3].str == NULL)
123 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
126 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
127 delaykey[i].len = delaykey[i].str == NULL ?
128 0 : strlen (delaykey[i].str);
129 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
130 MAX_DELAY_STOP_STR : delaykey[i].len;
132 presskey_max = presskey_max > delaykey[i].len ?
133 presskey_max : delaykey[i].len;
136 printf("%s key:<%s>\n",
137 delaykey[i].retry ? "delay" : "stop",
138 delaykey[i].str ? delaykey[i].str : "NULL");
142 /* In order to keep up with incoming data, check timeout only
145 while (!abort && get_ticks() <= etime) {
146 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
147 if (delaykey[i].len > 0 &&
148 presskey_len >= delaykey[i].len &&
149 memcmp (presskey + presskey_len - delaykey[i].len,
151 delaykey[i].len) == 0) {
153 printf("got %skey\n",
154 delaykey[i].retry ? "delay" : "stop");
157 # ifdef CONFIG_BOOT_RETRY_TIME
158 /* don't retry auto boot */
159 if (! delaykey[i].retry)
167 if (presskey_len < presskey_max) {
168 presskey [presskey_len ++] = getc();
171 for (i = 0; i < presskey_max - 1; i ++)
172 presskey [i] = presskey [i + 1];
174 presskey [i] = getc();
180 printf("key timeout\n");
186 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
188 #ifdef CONFIG_MENUKEY
189 static int menukey = 0;
192 static __inline__ int abortboot(int bootdelay)
196 #ifdef CONFIG_SILENT_CONSOLE
198 DECLARE_GLOBAL_DATA_PTR;
200 if (gd->flags & GD_FLG_SILENT) {
201 /* Restore serial console */
202 console_assign (stdout, "serial");
203 console_assign (stderr, "serial");
208 #ifdef CONFIG_MENUPROMPT
209 printf(CONFIG_MENUPROMPT, bootdelay);
211 printf("Hit any key to stop autoboot: %2d ", bootdelay);
214 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
216 * Check if key already pressed
217 * Don't check if bootdelay < 0
219 if (bootdelay >= 0) {
220 if (tstc()) { /* we got a key press */
221 (void) getc(); /* consume input */
223 abort = 1; /* don't auto boot */
228 while ((bootdelay > 0) && (!abort)) {
232 /* delay 100 * 10ms */
233 for (i=0; !abort && i<100; ++i) {
234 if (tstc()) { /* we got a key press */
235 abort = 1; /* don't auto boot */
236 bootdelay = 0; /* no more delay */
237 # ifdef CONFIG_MENUKEY
240 (void) getc(); /* consume input */
247 printf ("\b\b\b%2d ", bootdelay);
252 #ifdef CONFIG_SILENT_CONSOLE
254 DECLARE_GLOBAL_DATA_PTR;
257 /* permanently enable normal console output */
258 gd->flags &= ~(GD_FLG_SILENT);
259 } else if (gd->flags & GD_FLG_SILENT) {
260 /* Restore silent console */
261 console_assign (stdout, "nulldev");
262 console_assign (stderr, "nulldev");
269 # endif /* CONFIG_AUTOBOOT_KEYED */
270 #endif /* CONFIG_BOOTDELAY >= 0 */
272 /****************************************************************************/
274 void main_loop (void)
276 #ifndef CFG_HUSH_PARSER
277 static char lastcommand[CFG_CBSIZE] = { 0, };
283 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
287 #ifdef CONFIG_PREBOOT
290 #ifdef CONFIG_BOOTCOUNT_LIMIT
291 unsigned long bootcount = 0;
292 unsigned long bootlimit = 0;
295 #endif /* CONFIG_BOOTCOUNT_LIMIT */
297 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
298 ulong bmp = 0; /* default bitmap */
299 extern int trab_vfd (ulong bitmap);
301 #ifdef CONFIG_MODEM_SUPPORT
303 bmp = 1; /* alternate bitmap */
306 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
308 #ifdef CONFIG_BOOTCOUNT_LIMIT
309 bootcount = bootcount_load();
311 bootcount_store (bootcount);
312 sprintf (bcs_set, "%lu", bootcount);
313 setenv ("bootcount", bcs_set);
314 bcs = getenv ("bootlimit");
315 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
316 #endif /* CONFIG_BOOTCOUNT_LIMIT */
318 #ifdef CONFIG_MODEM_SUPPORT
319 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
321 uchar *str = strdup(getenv("mdm_cmd"));
322 setenv ("preboot", str); /* set or delete definition */
325 mdm_init(); /* wait for modem connection */
327 #endif /* CONFIG_MODEM_SUPPORT */
329 #ifdef CONFIG_VERSION_VARIABLE
331 extern char version_string[];
333 setenv ("ver", version_string); /* set version variable */
335 #endif /* CONFIG_VERSION_VARIABLE */
337 #ifdef CFG_HUSH_PARSER
338 u_boot_hush_start ();
341 #ifdef CONFIG_PREBOOT
342 if ((p = getenv ("preboot")) != NULL) {
343 # ifdef CONFIG_AUTOBOOT_KEYED
344 int prev = disable_ctrlc(1); /* disable Control C checking */
347 # ifndef CFG_HUSH_PARSER
350 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
351 FLAG_EXIT_FROM_LOOP);
354 # ifdef CONFIG_AUTOBOOT_KEYED
355 disable_ctrlc(prev); /* restore Control C checking */
358 #endif /* CONFIG_PREBOOT */
360 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
361 s = getenv ("bootdelay");
362 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
364 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
366 # ifdef CONFIG_BOOT_RETRY_TIME
368 # endif /* CONFIG_BOOT_RETRY_TIME */
370 #ifdef CONFIG_BOOTCOUNT_LIMIT
371 if (bootlimit && (bootcount > bootlimit)) {
372 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
373 (unsigned)bootlimit);
374 s = getenv ("altbootcmd");
377 #endif /* CONFIG_BOOTCOUNT_LIMIT */
378 s = getenv ("bootcmd");
380 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
382 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
383 # ifdef CONFIG_AUTOBOOT_KEYED
384 int prev = disable_ctrlc(1); /* disable Control C checking */
387 # ifndef CFG_HUSH_PARSER
390 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
391 FLAG_EXIT_FROM_LOOP);
394 # ifdef CONFIG_AUTOBOOT_KEYED
395 disable_ctrlc(prev); /* restore Control C checking */
399 # ifdef CONFIG_MENUKEY
400 if (menukey == CONFIG_MENUKEY) {
401 s = getenv("menucmd");
403 # ifndef CFG_HUSH_PARSER
404 run_command (s, bd, 0);
406 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
407 FLAG_EXIT_FROM_LOOP);
411 #endif /* CONFIG_MENUKEY */
412 #endif /* CONFIG_BOOTDELAY */
414 #ifdef CONFIG_AMIGAONEG3SE
416 extern void video_banner(void);
422 * Main Loop for Monitor Command Processing
424 #ifdef CFG_HUSH_PARSER
426 /* This point is never reached */
430 #ifdef CONFIG_BOOT_RETRY_TIME
432 /* Saw enough of a valid command to
433 * restart the timeout.
438 len = readline (CFG_PROMPT);
440 flag = 0; /* assume no special flags for now */
442 strcpy (lastcommand, console_buffer);
444 flag |= CMD_FLAG_REPEAT;
445 #ifdef CONFIG_BOOT_RETRY_TIME
446 else if (len == -2) {
447 /* -2 means timed out, retry autoboot
449 printf("\nTimed out waiting for command\n");
450 # ifdef CONFIG_RESET_TO_RETRY
451 /* Reinit board to run initialization code again */
452 do_reset (NULL, 0, 0, NULL);
454 return; /* retry autoboot */
460 printf ("<INTERRUPT>\n");
462 rc = run_command (lastcommand, flag);
465 /* invalid command or not repeatable, forget it */
469 #endif /*CFG_HUSH_PARSER*/
472 #ifdef CONFIG_BOOT_RETRY_TIME
473 /***************************************************************************
474 * initialise command line timeout
476 void init_cmd_timeout(void)
478 char *s = getenv ("bootretry");
481 retry_time = (int)simple_strtoul(s, NULL, 10);
483 retry_time = CONFIG_BOOT_RETRY_TIME;
485 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
486 retry_time = CONFIG_BOOT_RETRY_MIN;
489 /***************************************************************************
490 * reset command line timeout to retry_time seconds
492 void reset_cmd_timeout(void)
494 endtime = endtick(retry_time);
498 /****************************************************************************/
501 * Prompt for input and read a line.
502 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
503 * time out when time goes past endtime (timebase time in ticks).
504 * Return: number of read characters
508 int readline (const char *const prompt)
510 char *p = console_buffer;
511 int n = 0; /* buffer index */
512 int plen = 0; /* prompt length */
513 int col; /* output column cnt */
518 plen = strlen (prompt);
524 #ifdef CONFIG_BOOT_RETRY_TIME
525 while (!tstc()) { /* while no incoming data */
526 if (retry_time >= 0 && get_ticks() > endtime)
527 return (-2); /* timed out */
530 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
532 #ifdef CONFIG_SHOW_ACTIVITY
534 extern void show_activity(int arg);
541 * Special character handling
544 case '\r': /* Enter */
548 return (p - console_buffer);
550 case 0x03: /* ^C - break */
551 console_buffer[0] = '\0'; /* discard input */
554 case 0x15: /* ^U - erase line */
563 case 0x17: /* ^W - erase word */
564 p=delete_char(console_buffer, p, &col, &n, plen);
565 while ((n > 0) && (*p != ' ')) {
566 p=delete_char(console_buffer, p, &col, &n, plen);
570 case 0x08: /* ^H - backspace */
571 case 0x7F: /* DEL - backspace */
572 p=delete_char(console_buffer, p, &col, &n, plen);
577 * Must be a normal character then
579 if (n < CFG_CBSIZE-2) {
580 if (c == '\t') { /* expand TABs */
581 puts (tab_seq+(col&07));
584 ++col; /* echo input */
589 } else { /* Buffer full */
596 /****************************************************************************/
598 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
606 if (*(--p) == '\t') { /* will retype the whole line */
607 while (*colp > plen) {
611 for (s=buffer; s<p; ++s) {
613 puts (tab_seq+((*colp) & 07));
614 *colp += 8 - ((*colp) & 07);
628 /****************************************************************************/
630 int parse_line (char *line, char *argv[])
635 printf ("parse_line: \"%s\"\n", line);
637 while (nargs < CFG_MAXARGS) {
639 /* skip any white space */
640 while ((*line == ' ') || (*line == '\t')) {
644 if (*line == '\0') { /* end of line, no more args */
647 printf ("parse_line: nargs=%d\n", nargs);
652 argv[nargs++] = line; /* begin of argument string */
654 /* find end of string */
655 while (*line && (*line != ' ') && (*line != '\t')) {
659 if (*line == '\0') { /* end of line, no more args */
662 printf ("parse_line: nargs=%d\n", nargs);
667 *line++ = '\0'; /* terminate current arg */
670 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
673 printf ("parse_line: nargs=%d\n", nargs);
678 /****************************************************************************/
680 static void process_macros (const char *input, char *output)
683 const char *varname_start = NULL;
684 int inputcnt = strlen (input);
685 int outputcnt = CFG_CBSIZE;
686 int state = 0; /* 0 = waiting for '$' */
687 /* 1 = waiting for '(' */
688 /* 2 = waiting for ')' */
689 /* 3 = waiting for ''' */
691 char *output_start = output;
693 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
696 prev = '\0'; /* previous character */
698 while (inputcnt && outputcnt) {
703 /* remove one level of escape characters */
704 if ((c == '\\') && (prev != '\\')) {
713 case 0: /* Waiting for (unescaped) $ */
714 if ((c == '\'') && (prev != '\\')) {
718 if ((c == '$') && (prev != '\\')) {
725 case 1: /* Waiting for ( */
728 varname_start = input;
740 case 2: /* Waiting for ) */
743 char envname[CFG_CBSIZE], *envval;
744 int envcnt = input-varname_start-1; /* Varname # of chars */
746 /* Get the varname */
747 for (i = 0; i < envcnt; i++) {
748 envname[i] = varname_start[i];
753 envval = getenv (envname);
755 /* Copy into the line if it exists */
757 while ((*envval) && outputcnt) {
758 *(output++) = *(envval++);
761 /* Look for another '$' */
765 case 3: /* Waiting for ' */
766 if ((c == '\'') && (prev != '\\')) {
781 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
782 strlen(output_start), output_start);
786 /****************************************************************************
788 * 1 - command executed, repeatable
789 * 0 - command executed but not repeatable, interrupted commands are
790 * always considered not repeatable
791 * -1 - not executed (unrecognized, bootd recursion or too many args)
792 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
793 * considered unrecognized)
797 * We must create a temporary copy of the command since the command we get
798 * may be the result from getenv(), which returns a pointer directly to
799 * the environment data, which may change magicly when the command we run
800 * creates or modifies environment variables (like "bootp" does).
803 int run_command (const char *cmd, int flag)
806 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
807 char *token; /* start of token in cmdbuf */
808 char *sep; /* end of token (separator) in cmdbuf */
809 char finaltoken[CFG_CBSIZE];
811 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
817 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
818 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
822 clear_ctrlc(); /* forget any previous Control C */
825 return -1; /* empty command */
828 if (strlen(cmd) >= CFG_CBSIZE) {
829 puts ("## Command too long!\n");
833 strcpy (cmdbuf, cmd);
835 /* Process separators and check for invalid
836 * repeatable commands
840 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
845 * Find separator, or string end
846 * Allow simple escape of ';' by writing "\;"
848 for (inquotes = 0, sep = str; *sep; sep++) {
854 (*sep == ';') && /* separator */
855 ( sep != str) && /* past string start */
856 (*(sep-1) != '\\')) /* and NOT escaped */
861 * Limit the token to data between separators
865 str = sep + 1; /* start of command for next pass */
869 str = sep; /* no more commands for next pass */
871 printf ("token: \"%s\"\n", token);
874 /* find macros in this token and replace them */
875 process_macros (token, finaltoken);
877 /* Extract arguments */
878 argc = parse_line (finaltoken, argv);
880 /* Look up command in command table */
881 if ((cmdtp = find_cmd(argv[0])) == NULL) {
882 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
883 rc = -1; /* give up after bad command */
887 /* found - check max args */
888 if (argc > cmdtp->maxargs) {
889 printf ("Usage:\n%s\n", cmdtp->usage);
894 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
895 /* avoid "bootd" recursion */
896 if (cmdtp->cmd == do_bootd) {
898 printf ("[%s]\n", finaltoken);
900 if (flag & CMD_FLAG_BOOTD) {
901 printf ("'bootd' recursion detected\n");
906 flag |= CMD_FLAG_BOOTD;
908 #endif /* CFG_CMD_BOOTD */
910 /* OK - call function to do the command */
911 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
915 repeatable &= cmdtp->repeatable;
917 /* Did the user stop this? */
919 return 0; /* if stopped then not repeatable */
922 return rc ? rc : repeatable;
925 /****************************************************************************/
927 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
928 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
933 printf ("Usage:\n%s\n", cmdtp->usage);
937 for (i=1; i<argc; ++i) {
940 if ((arg = getenv (argv[i])) == NULL) {
941 printf ("## Error: \"%s\" not defined\n", argv[i]);
944 #ifndef CFG_HUSH_PARSER
945 if (run_command (arg, flag) == -1)
948 if (parse_string_outer(arg,
949 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
955 #endif /* CFG_CMD_RUN */