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_MENUPROMPT
197 printf(CONFIG_MENUPROMPT, bootdelay);
199 printf("Hit any key to stop autoboot: %2d ", bootdelay);
202 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
204 * Check if key already pressed
205 * Don't check if bootdelay < 0
207 if (bootdelay >= 0) {
208 if (tstc()) { /* we got a key press */
209 (void) getc(); /* consume input */
210 printf ("\b\b\b 0\n");
211 return 1; /* don't auto boot */
216 while (bootdelay > 0) {
220 /* delay 100 * 10ms */
221 for (i=0; !abort && i<100; ++i) {
222 if (tstc()) { /* we got a key press */
223 abort = 1; /* don't auto boot */
224 bootdelay = 0; /* no more delay */
225 # ifdef CONFIG_MENUKEY
228 (void) getc(); /* consume input */
235 printf ("\b\b\b%2d ", bootdelay);
242 # endif /* CONFIG_AUTOBOOT_KEYED */
243 #endif /* CONFIG_BOOTDELAY >= 0 */
245 /****************************************************************************/
247 void main_loop (void)
249 #ifndef CFG_HUSH_PARSER
250 static char lastcommand[CFG_CBSIZE] = { 0, };
256 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
260 #ifdef CONFIG_PREBOOT
263 #ifdef CONFIG_BOOTCOUNT_LIMIT
264 unsigned long bootcount = 0;
265 unsigned long bootlimit = 0;
268 #endif /* CONFIG_BOOTCOUNT_LIMIT */
270 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
271 ulong bmp = 0; /* default bitmap */
272 extern int trab_vfd (ulong bitmap);
274 #ifdef CONFIG_MODEM_SUPPORT
276 bmp = 1; /* alternate bitmap */
279 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
281 #ifdef CONFIG_BOOTCOUNT_LIMIT
282 bootcount = bootcount_load();
284 bootcount_store (bootcount);
285 sprintf (bcs_set, "%lu", bootcount);
286 setenv ("bootcount", bcs_set);
287 bcs = getenv ("bootlimit");
288 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
289 #endif /* CONFIG_BOOTCOUNT_LIMIT */
291 #ifdef CONFIG_MODEM_SUPPORT
292 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
294 uchar *str = strdup(getenv("mdm_cmd"));
295 setenv ("preboot", str); /* set or delete definition */
298 mdm_init(); /* wait for modem connection */
300 #endif /* CONFIG_MODEM_SUPPORT */
302 #ifdef CONFIG_VERSION_VARIABLE
304 extern char version_string[];
306 setenv ("ver", version_string); /* set version variable */
308 #endif /* CONFIG_VERSION_VARIABLE */
310 #ifdef CFG_HUSH_PARSER
311 u_boot_hush_start ();
314 #ifdef CONFIG_PREBOOT
315 if ((p = getenv ("preboot")) != NULL) {
316 # ifdef CONFIG_AUTOBOOT_KEYED
317 int prev = disable_ctrlc(1); /* disable Control C checking */
320 # ifndef CFG_HUSH_PARSER
323 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
324 FLAG_EXIT_FROM_LOOP);
327 # ifdef CONFIG_AUTOBOOT_KEYED
328 disable_ctrlc(prev); /* restore Control C checking */
331 #endif /* CONFIG_PREBOOT */
333 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
334 s = getenv ("bootdelay");
335 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
337 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
339 # ifdef CONFIG_BOOT_RETRY_TIME
341 # endif /* CONFIG_BOOT_RETRY_TIME */
343 #ifdef CONFIG_BOOTCOUNT_LIMIT
344 if (bootlimit && (bootcount > bootlimit)) {
345 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
346 (unsigned)bootlimit);
347 s = getenv ("altbootcmd");
350 #endif /* CONFIG_BOOTCOUNT_LIMIT */
351 s = getenv ("bootcmd");
353 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
355 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
356 # ifdef CONFIG_AUTOBOOT_KEYED
357 int prev = disable_ctrlc(1); /* disable Control C checking */
360 # ifndef CFG_HUSH_PARSER
363 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
364 FLAG_EXIT_FROM_LOOP);
367 # ifdef CONFIG_AUTOBOOT_KEYED
368 disable_ctrlc(prev); /* restore Control C checking */
372 # ifdef CONFIG_MENUKEY
373 if (menukey == CONFIG_MENUKEY) {
374 s = getenv("menucmd");
376 # ifndef CFG_HUSH_PARSER
377 run_command (s, bd, 0);
379 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
380 FLAG_EXIT_FROM_LOOP);
384 #endif /* CONFIG_MENUKEY */
385 #endif /* CONFIG_BOOTDELAY */
387 #ifdef CONFIG_AMIGAONEG3SE
389 extern void video_banner(void);
395 * Main Loop for Monitor Command Processing
397 #ifdef CFG_HUSH_PARSER
399 /* This point is never reached */
403 #ifdef CONFIG_BOOT_RETRY_TIME
405 /* Saw enough of a valid command to
406 * restart the timeout.
411 len = readline (CFG_PROMPT);
413 flag = 0; /* assume no special flags for now */
415 strcpy (lastcommand, console_buffer);
417 flag |= CMD_FLAG_REPEAT;
418 #ifdef CONFIG_BOOT_RETRY_TIME
419 else if (len == -2) {
420 /* -2 means timed out, retry autoboot
422 printf("\nTimed out waiting for command\n");
423 # ifdef CONFIG_RESET_TO_RETRY
424 /* Reinit board to run initialization code again */
425 do_reset (NULL, 0, 0, NULL);
427 return; /* retry autoboot */
433 printf ("<INTERRUPT>\n");
435 rc = run_command (lastcommand, flag);
438 /* invalid command or not repeatable, forget it */
442 #endif /*CFG_HUSH_PARSER*/
445 #ifdef CONFIG_BOOT_RETRY_TIME
446 /***************************************************************************
447 * initialise command line timeout
449 void init_cmd_timeout(void)
451 char *s = getenv ("bootretry");
454 retry_time = (int)simple_strtoul(s, NULL, 10);
456 retry_time = CONFIG_BOOT_RETRY_TIME;
458 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
459 retry_time = CONFIG_BOOT_RETRY_MIN;
462 /***************************************************************************
463 * reset command line timeout to retry_time seconds
465 void reset_cmd_timeout(void)
467 endtime = endtick(retry_time);
471 /****************************************************************************/
474 * Prompt for input and read a line.
475 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
476 * time out when time goes past endtime (timebase time in ticks).
477 * Return: number of read characters
481 int readline (const char *const prompt)
483 char *p = console_buffer;
484 int n = 0; /* buffer index */
485 int plen = 0; /* prompt length */
486 int col; /* output column cnt */
491 plen = strlen (prompt);
497 #ifdef CONFIG_BOOT_RETRY_TIME
498 while (!tstc()) { /* while no incoming data */
499 if (retry_time >= 0 && get_ticks() > endtime)
500 return (-2); /* timed out */
503 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
505 #ifdef CONFIG_SHOW_ACTIVITY
507 extern void show_activity(int arg);
514 * Special character handling
517 case '\r': /* Enter */
521 return (p - console_buffer);
523 case 0x03: /* ^C - break */
524 console_buffer[0] = '\0'; /* discard input */
527 case 0x15: /* ^U - erase line */
536 case 0x17: /* ^W - erase word */
537 p=delete_char(console_buffer, p, &col, &n, plen);
538 while ((n > 0) && (*p != ' ')) {
539 p=delete_char(console_buffer, p, &col, &n, plen);
543 case 0x08: /* ^H - backspace */
544 case 0x7F: /* DEL - backspace */
545 p=delete_char(console_buffer, p, &col, &n, plen);
550 * Must be a normal character then
552 if (n < CFG_CBSIZE-2) {
553 if (c == '\t') { /* expand TABs */
554 puts (tab_seq+(col&07));
557 ++col; /* echo input */
562 } else { /* Buffer full */
569 /****************************************************************************/
571 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
579 if (*(--p) == '\t') { /* will retype the whole line */
580 while (*colp > plen) {
584 for (s=buffer; s<p; ++s) {
586 puts (tab_seq+((*colp) & 07));
587 *colp += 8 - ((*colp) & 07);
601 /****************************************************************************/
603 int parse_line (char *line, char *argv[])
608 printf ("parse_line: \"%s\"\n", line);
610 while (nargs < CFG_MAXARGS) {
612 /* skip any white space */
613 while ((*line == ' ') || (*line == '\t')) {
617 if (*line == '\0') { /* end of line, no more args */
620 printf ("parse_line: nargs=%d\n", nargs);
625 argv[nargs++] = line; /* begin of argument string */
627 /* find end of string */
628 while (*line && (*line != ' ') && (*line != '\t')) {
632 if (*line == '\0') { /* end of line, no more args */
635 printf ("parse_line: nargs=%d\n", nargs);
640 *line++ = '\0'; /* terminate current arg */
643 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
646 printf ("parse_line: nargs=%d\n", nargs);
651 /****************************************************************************/
653 static void process_macros (const char *input, char *output)
656 const char *varname_start = NULL;
657 int inputcnt = strlen (input);
658 int outputcnt = CFG_CBSIZE;
659 int state = 0; /* 0 = waiting for '$' */
660 /* 1 = waiting for '(' */
661 /* 2 = waiting for ')' */
662 /* 3 = waiting for ''' */
664 char *output_start = output;
666 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
669 prev = '\0'; /* previous character */
671 while (inputcnt && outputcnt) {
676 /* remove one level of escape characters */
677 if ((c == '\\') && (prev != '\\')) {
686 case 0: /* Waiting for (unescaped) $ */
687 if ((c == '\'') && (prev != '\\')) {
691 if ((c == '$') && (prev != '\\')) {
698 case 1: /* Waiting for ( */
701 varname_start = input;
713 case 2: /* Waiting for ) */
716 char envname[CFG_CBSIZE], *envval;
717 int envcnt = input-varname_start-1; /* Varname # of chars */
719 /* Get the varname */
720 for (i = 0; i < envcnt; i++) {
721 envname[i] = varname_start[i];
726 envval = getenv (envname);
728 /* Copy into the line if it exists */
730 while ((*envval) && outputcnt) {
731 *(output++) = *(envval++);
734 /* Look for another '$' */
738 case 3: /* Waiting for ' */
739 if ((c == '\'') && (prev != '\\')) {
754 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
755 strlen(output_start), output_start);
759 /****************************************************************************
761 * 1 - command executed, repeatable
762 * 0 - command executed but not repeatable, interrupted commands are
763 * always considered not repeatable
764 * -1 - not executed (unrecognized, bootd recursion or too many args)
765 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
766 * considered unrecognized)
770 * We must create a temporary copy of the command since the command we get
771 * may be the result from getenv(), which returns a pointer directly to
772 * the environment data, which may change magicly when the command we run
773 * creates or modifies environment variables (like "bootp" does).
776 int run_command (const char *cmd, int flag)
779 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
780 char *token; /* start of token in cmdbuf */
781 char *sep; /* end of token (separator) in cmdbuf */
782 char finaltoken[CFG_CBSIZE];
784 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
790 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
791 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
795 clear_ctrlc(); /* forget any previous Control C */
798 return -1; /* empty command */
801 if (strlen(cmd) >= CFG_CBSIZE) {
802 puts ("## Command too long!\n");
806 strcpy (cmdbuf, cmd);
808 /* Process separators and check for invalid
809 * repeatable commands
813 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
818 * Find separator, or string end
819 * Allow simple escape of ';' by writing "\;"
821 for (inquotes = 0, sep = str; *sep; sep++) {
827 (*sep == ';') && /* separator */
828 ( sep != str) && /* past string start */
829 (*(sep-1) != '\\')) /* and NOT escaped */
834 * Limit the token to data between separators
838 str = sep + 1; /* start of command for next pass */
842 str = sep; /* no more commands for next pass */
844 printf ("token: \"%s\"\n", token);
847 /* find macros in this token and replace them */
848 process_macros (token, finaltoken);
850 /* Extract arguments */
851 argc = parse_line (finaltoken, argv);
853 /* Look up command in command table */
854 if ((cmdtp = find_cmd(argv[0])) == NULL) {
855 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
856 rc = -1; /* give up after bad command */
860 /* found - check max args */
861 if (argc > cmdtp->maxargs) {
862 printf ("Usage:\n%s\n", cmdtp->usage);
867 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
868 /* avoid "bootd" recursion */
869 if (cmdtp->cmd == do_bootd) {
871 printf ("[%s]\n", finaltoken);
873 if (flag & CMD_FLAG_BOOTD) {
874 printf ("'bootd' recursion detected\n");
879 flag |= CMD_FLAG_BOOTD;
881 #endif /* CFG_CMD_BOOTD */
883 /* OK - call function to do the command */
884 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
888 repeatable &= cmdtp->repeatable;
890 /* Did the user stop this? */
892 return 0; /* if stopped then not repeatable */
895 return rc ? rc : repeatable;
898 /****************************************************************************/
900 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
901 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
906 printf ("Usage:\n%s\n", cmdtp->usage);
910 for (i=1; i<argc; ++i) {
913 if ((arg = getenv (argv[i])) == NULL) {
914 printf ("## Error: \"%s\" not defined\n", argv[i]);
917 #ifndef CFG_HUSH_PARSER
918 if (run_command (arg, flag) == -1)
921 if (parse_string_outer(arg,
922 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
928 #endif /* CFG_CMD_RUN */