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_SILENT_CONSOLE
107 DECLARE_GLOBAL_DATA_PTR;
109 if (gd->flags & GD_FLG_SILENT) {
110 /* Restore serial console */
111 console_assign (stdout, "serial");
112 console_assign (stderr, "serial");
117 # ifdef CONFIG_AUTOBOOT_PROMPT
118 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
121 # ifdef CONFIG_AUTOBOOT_DELAY_STR
122 if (delaykey[0].str == NULL)
123 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
125 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
126 if (delaykey[1].str == NULL)
127 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
129 # ifdef CONFIG_AUTOBOOT_STOP_STR
130 if (delaykey[2].str == NULL)
131 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
133 # ifdef CONFIG_AUTOBOOT_STOP_STR2
134 if (delaykey[3].str == NULL)
135 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
138 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
139 delaykey[i].len = delaykey[i].str == NULL ?
140 0 : strlen (delaykey[i].str);
141 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
142 MAX_DELAY_STOP_STR : delaykey[i].len;
144 presskey_max = presskey_max > delaykey[i].len ?
145 presskey_max : delaykey[i].len;
148 printf("%s key:<%s>\n",
149 delaykey[i].retry ? "delay" : "stop",
150 delaykey[i].str ? delaykey[i].str : "NULL");
154 /* In order to keep up with incoming data, check timeout only
157 while (!abort && get_ticks() <= etime) {
158 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
159 if (delaykey[i].len > 0 &&
160 presskey_len >= delaykey[i].len &&
161 memcmp (presskey + presskey_len - delaykey[i].len,
163 delaykey[i].len) == 0) {
165 printf("got %skey\n",
166 delaykey[i].retry ? "delay" : "stop");
169 # ifdef CONFIG_BOOT_RETRY_TIME
170 /* don't retry auto boot */
171 if (! delaykey[i].retry)
179 if (presskey_len < presskey_max) {
180 presskey [presskey_len ++] = getc();
183 for (i = 0; i < presskey_max - 1; i ++)
184 presskey [i] = presskey [i + 1];
186 presskey [i] = getc();
192 puts ("key timeout\n");
195 #ifdef CONFIG_SILENT_CONSOLE
197 DECLARE_GLOBAL_DATA_PTR;
200 /* permanently enable normal console output */
201 gd->flags &= ~(GD_FLG_SILENT);
202 } else if (gd->flags & GD_FLG_SILENT) {
203 /* Restore silent console */
204 console_assign (stdout, "nulldev");
205 console_assign (stderr, "nulldev");
213 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
215 #ifdef CONFIG_MENUKEY
216 static int menukey = 0;
219 static __inline__ int abortboot(int bootdelay)
223 #ifdef CONFIG_SILENT_CONSOLE
225 DECLARE_GLOBAL_DATA_PTR;
227 if (gd->flags & GD_FLG_SILENT) {
228 /* Restore serial console */
229 console_assign (stdout, "serial");
230 console_assign (stderr, "serial");
235 #ifdef CONFIG_MENUPROMPT
236 printf(CONFIG_MENUPROMPT, bootdelay);
238 printf("Hit any key to stop autoboot: %2d ", bootdelay);
241 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
243 * Check if key already pressed
244 * Don't check if bootdelay < 0
246 if (bootdelay >= 0) {
247 if (tstc()) { /* we got a key press */
248 (void) getc(); /* consume input */
250 abort = 1; /* don't auto boot */
255 while ((bootdelay > 0) && (!abort)) {
259 /* delay 100 * 10ms */
260 for (i=0; !abort && i<100; ++i) {
261 if (tstc()) { /* we got a key press */
262 abort = 1; /* don't auto boot */
263 bootdelay = 0; /* no more delay */
264 # ifdef CONFIG_MENUKEY
267 (void) getc(); /* consume input */
274 printf ("\b\b\b%2d ", bootdelay);
279 #ifdef CONFIG_SILENT_CONSOLE
281 DECLARE_GLOBAL_DATA_PTR;
284 /* permanently enable normal console output */
285 gd->flags &= ~(GD_FLG_SILENT);
286 } else if (gd->flags & GD_FLG_SILENT) {
287 /* Restore silent console */
288 console_assign (stdout, "nulldev");
289 console_assign (stderr, "nulldev");
296 # endif /* CONFIG_AUTOBOOT_KEYED */
297 #endif /* CONFIG_BOOTDELAY >= 0 */
299 /****************************************************************************/
301 void main_loop (void)
303 #ifndef CFG_HUSH_PARSER
304 static char lastcommand[CFG_CBSIZE] = { 0, };
310 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
314 #ifdef CONFIG_PREBOOT
317 #ifdef CONFIG_BOOTCOUNT_LIMIT
318 unsigned long bootcount = 0;
319 unsigned long bootlimit = 0;
322 #endif /* CONFIG_BOOTCOUNT_LIMIT */
324 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
325 ulong bmp = 0; /* default bitmap */
326 extern int trab_vfd (ulong bitmap);
328 #ifdef CONFIG_MODEM_SUPPORT
330 bmp = 1; /* alternate bitmap */
333 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
335 #ifdef CONFIG_BOOTCOUNT_LIMIT
336 bootcount = bootcount_load();
338 bootcount_store (bootcount);
339 sprintf (bcs_set, "%lu", bootcount);
340 setenv ("bootcount", bcs_set);
341 bcs = getenv ("bootlimit");
342 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
343 #endif /* CONFIG_BOOTCOUNT_LIMIT */
345 #ifdef CONFIG_MODEM_SUPPORT
346 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
348 uchar *str = strdup(getenv("mdm_cmd"));
349 setenv ("preboot", str); /* set or delete definition */
352 mdm_init(); /* wait for modem connection */
354 #endif /* CONFIG_MODEM_SUPPORT */
356 #ifdef CONFIG_VERSION_VARIABLE
358 extern char version_string[];
360 setenv ("ver", version_string); /* set version variable */
362 #endif /* CONFIG_VERSION_VARIABLE */
364 #ifdef CFG_HUSH_PARSER
365 u_boot_hush_start ();
368 #ifdef CONFIG_PREBOOT
369 if ((p = getenv ("preboot")) != NULL) {
370 # ifdef CONFIG_AUTOBOOT_KEYED
371 int prev = disable_ctrlc(1); /* disable Control C checking */
374 # ifndef CFG_HUSH_PARSER
377 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
378 FLAG_EXIT_FROM_LOOP);
381 # ifdef CONFIG_AUTOBOOT_KEYED
382 disable_ctrlc(prev); /* restore Control C checking */
385 #endif /* CONFIG_PREBOOT */
387 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
388 s = getenv ("bootdelay");
389 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
391 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
393 # ifdef CONFIG_BOOT_RETRY_TIME
395 # endif /* CONFIG_BOOT_RETRY_TIME */
397 #ifdef CONFIG_BOOTCOUNT_LIMIT
398 if (bootlimit && (bootcount > bootlimit)) {
399 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
400 (unsigned)bootlimit);
401 s = getenv ("altbootcmd");
404 #endif /* CONFIG_BOOTCOUNT_LIMIT */
405 s = getenv ("bootcmd");
407 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
409 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
410 # ifdef CONFIG_AUTOBOOT_KEYED
411 int prev = disable_ctrlc(1); /* disable Control C checking */
414 # ifndef CFG_HUSH_PARSER
417 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
418 FLAG_EXIT_FROM_LOOP);
421 # ifdef CONFIG_AUTOBOOT_KEYED
422 disable_ctrlc(prev); /* restore Control C checking */
426 # ifdef CONFIG_MENUKEY
427 if (menukey == CONFIG_MENUKEY) {
428 s = getenv("menucmd");
430 # ifndef CFG_HUSH_PARSER
431 run_command (s, bd, 0);
433 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
434 FLAG_EXIT_FROM_LOOP);
438 #endif /* CONFIG_MENUKEY */
439 #endif /* CONFIG_BOOTDELAY */
441 #ifdef CONFIG_AMIGAONEG3SE
443 extern void video_banner(void);
449 * Main Loop for Monitor Command Processing
451 #ifdef CFG_HUSH_PARSER
453 /* This point is never reached */
457 #ifdef CONFIG_BOOT_RETRY_TIME
459 /* Saw enough of a valid command to
460 * restart the timeout.
465 len = readline (CFG_PROMPT);
467 flag = 0; /* assume no special flags for now */
469 strcpy (lastcommand, console_buffer);
471 flag |= CMD_FLAG_REPEAT;
472 #ifdef CONFIG_BOOT_RETRY_TIME
473 else if (len == -2) {
474 /* -2 means timed out, retry autoboot
476 puts ("\nTimed out waiting for command\n");
477 # ifdef CONFIG_RESET_TO_RETRY
478 /* Reinit board to run initialization code again */
479 do_reset (NULL, 0, 0, NULL);
481 return; /* retry autoboot */
487 puts ("<INTERRUPT>\n");
489 rc = run_command (lastcommand, flag);
492 /* invalid command or not repeatable, forget it */
496 #endif /*CFG_HUSH_PARSER*/
499 #ifdef CONFIG_BOOT_RETRY_TIME
500 /***************************************************************************
501 * initialise command line timeout
503 void init_cmd_timeout(void)
505 char *s = getenv ("bootretry");
508 retry_time = (int)simple_strtol(s, NULL, 10);
510 retry_time = CONFIG_BOOT_RETRY_TIME;
512 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
513 retry_time = CONFIG_BOOT_RETRY_MIN;
516 /***************************************************************************
517 * reset command line timeout to retry_time seconds
519 void reset_cmd_timeout(void)
521 endtime = endtick(retry_time);
525 /****************************************************************************/
528 * Prompt for input and read a line.
529 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
530 * time out when time goes past endtime (timebase time in ticks).
531 * Return: number of read characters
535 int readline (const char *const prompt)
537 char *p = console_buffer;
538 int n = 0; /* buffer index */
539 int plen = 0; /* prompt length */
540 int col; /* output column cnt */
545 plen = strlen (prompt);
551 #ifdef CONFIG_BOOT_RETRY_TIME
552 while (!tstc()) { /* while no incoming data */
553 if (retry_time >= 0 && get_ticks() > endtime)
554 return (-2); /* timed out */
557 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
559 #ifdef CONFIG_SHOW_ACTIVITY
561 extern void show_activity(int arg);
568 * Special character handling
571 case '\r': /* Enter */
575 return (p - console_buffer);
580 case 0x03: /* ^C - break */
581 console_buffer[0] = '\0'; /* discard input */
584 case 0x15: /* ^U - erase line */
593 case 0x17: /* ^W - erase word */
594 p=delete_char(console_buffer, p, &col, &n, plen);
595 while ((n > 0) && (*p != ' ')) {
596 p=delete_char(console_buffer, p, &col, &n, plen);
600 case 0x08: /* ^H - backspace */
601 case 0x7F: /* DEL - backspace */
602 p=delete_char(console_buffer, p, &col, &n, plen);
607 * Must be a normal character then
609 if (n < CFG_CBSIZE-2) {
610 if (c == '\t') { /* expand TABs */
611 puts (tab_seq+(col&07));
614 ++col; /* echo input */
619 } else { /* Buffer full */
626 /****************************************************************************/
628 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
636 if (*(--p) == '\t') { /* will retype the whole line */
637 while (*colp > plen) {
641 for (s=buffer; s<p; ++s) {
643 puts (tab_seq+((*colp) & 07));
644 *colp += 8 - ((*colp) & 07);
658 /****************************************************************************/
660 int parse_line (char *line, char *argv[])
665 printf ("parse_line: \"%s\"\n", line);
667 while (nargs < CFG_MAXARGS) {
669 /* skip any white space */
670 while ((*line == ' ') || (*line == '\t')) {
674 if (*line == '\0') { /* end of line, no more args */
677 printf ("parse_line: nargs=%d\n", nargs);
682 argv[nargs++] = line; /* begin of argument string */
684 /* find end of string */
685 while (*line && (*line != ' ') && (*line != '\t')) {
689 if (*line == '\0') { /* end of line, no more args */
692 printf ("parse_line: nargs=%d\n", nargs);
697 *line++ = '\0'; /* terminate current arg */
700 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
703 printf ("parse_line: nargs=%d\n", nargs);
708 /****************************************************************************/
710 static void process_macros (const char *input, char *output)
713 const char *varname_start = NULL;
714 int inputcnt = strlen (input);
715 int outputcnt = CFG_CBSIZE;
716 int state = 0; /* 0 = waiting for '$' */
717 /* 1 = waiting for '(' */
718 /* 2 = waiting for ')' */
719 /* 3 = waiting for ''' */
721 char *output_start = output;
723 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
726 prev = '\0'; /* previous character */
728 while (inputcnt && outputcnt) {
733 /* remove one level of escape characters */
734 if ((c == '\\') && (prev != '\\')) {
743 case 0: /* Waiting for (unescaped) $ */
744 if ((c == '\'') && (prev != '\\')) {
748 if ((c == '$') && (prev != '\\')) {
755 case 1: /* Waiting for ( */
758 varname_start = input;
770 case 2: /* Waiting for ) */
773 char envname[CFG_CBSIZE], *envval;
774 int envcnt = input-varname_start-1; /* Varname # of chars */
776 /* Get the varname */
777 for (i = 0; i < envcnt; i++) {
778 envname[i] = varname_start[i];
783 envval = getenv (envname);
785 /* Copy into the line if it exists */
787 while ((*envval) && outputcnt) {
788 *(output++) = *(envval++);
791 /* Look for another '$' */
795 case 3: /* Waiting for ' */
796 if ((c == '\'') && (prev != '\\')) {
811 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
812 strlen(output_start), output_start);
816 /****************************************************************************
818 * 1 - command executed, repeatable
819 * 0 - command executed but not repeatable, interrupted commands are
820 * always considered not repeatable
821 * -1 - not executed (unrecognized, bootd recursion or too many args)
822 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
823 * considered unrecognized)
827 * We must create a temporary copy of the command since the command we get
828 * may be the result from getenv(), which returns a pointer directly to
829 * the environment data, which may change magicly when the command we run
830 * creates or modifies environment variables (like "bootp" does).
833 int run_command (const char *cmd, int flag)
836 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
837 char *token; /* start of token in cmdbuf */
838 char *sep; /* end of token (separator) in cmdbuf */
839 char finaltoken[CFG_CBSIZE];
841 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
847 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
848 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
852 clear_ctrlc(); /* forget any previous Control C */
855 return -1; /* empty command */
858 if (strlen(cmd) >= CFG_CBSIZE) {
859 puts ("## Command too long!\n");
863 strcpy (cmdbuf, cmd);
865 /* Process separators and check for invalid
866 * repeatable commands
870 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
875 * Find separator, or string end
876 * Allow simple escape of ';' by writing "\;"
878 for (inquotes = 0, sep = str; *sep; sep++) {
884 (*sep == ';') && /* separator */
885 ( sep != str) && /* past string start */
886 (*(sep-1) != '\\')) /* and NOT escaped */
891 * Limit the token to data between separators
895 str = sep + 1; /* start of command for next pass */
899 str = sep; /* no more commands for next pass */
901 printf ("token: \"%s\"\n", token);
904 /* find macros in this token and replace them */
905 process_macros (token, finaltoken);
907 /* Extract arguments */
908 argc = parse_line (finaltoken, argv);
910 /* Look up command in command table */
911 if ((cmdtp = find_cmd(argv[0])) == NULL) {
912 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
913 rc = -1; /* give up after bad command */
917 /* found - check max args */
918 if (argc > cmdtp->maxargs) {
919 printf ("Usage:\n%s\n", cmdtp->usage);
924 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
925 /* avoid "bootd" recursion */
926 if (cmdtp->cmd == do_bootd) {
928 printf ("[%s]\n", finaltoken);
930 if (flag & CMD_FLAG_BOOTD) {
931 puts ("'bootd' recursion detected\n");
936 flag |= CMD_FLAG_BOOTD;
938 #endif /* CFG_CMD_BOOTD */
940 /* OK - call function to do the command */
941 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
945 repeatable &= cmdtp->repeatable;
947 /* Did the user stop this? */
949 return 0; /* if stopped then not repeatable */
952 return rc ? rc : repeatable;
955 /****************************************************************************/
957 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
958 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
963 printf ("Usage:\n%s\n", cmdtp->usage);
967 for (i=1; i<argc; ++i) {
970 if ((arg = getenv (argv[i])) == NULL) {
971 printf ("## Error: \"%s\" not defined\n", argv[i]);
974 #ifndef CFG_HUSH_PARSER
975 if (run_command (arg, flag) == -1)
978 if (parse_string_outer(arg,
979 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
985 #endif /* CFG_CMD_RUN */