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,
29 #ifdef CONFIG_MODEM_SUPPORT
30 #include <malloc.h> /* for free() prototype */
33 #ifdef CFG_HUSH_PARSER
39 #ifdef CONFIG_SILENT_CONSOLE
40 DECLARE_GLOBAL_DATA_PTR;
43 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
44 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
47 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50 #define MAX_DELAY_STOP_STR 32
52 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
53 static int parse_line (char *, char *[]);
54 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
55 static int abortboot(int);
60 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
62 static char erase_seq[] = "\b \b"; /* erase sequence */
63 static char tab_seq[] = " "; /* used to expand TABs */
65 #ifdef CONFIG_BOOT_RETRY_TIME
66 static uint64_t endtime = 0; /* must be set, default is instant timeout */
67 static int retry_time = -1; /* -1 so can call readline before main_loop */
70 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
72 #ifndef CONFIG_BOOT_RETRY_MIN
73 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
76 #ifdef CONFIG_MODEM_SUPPORT
78 extern void mdm_init(void); /* defined in board.c */
81 /***************************************************************************
82 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
83 * returns: 0 - no key string, allow autoboot
84 * 1 - got key string, abort
86 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
87 # if defined(CONFIG_AUTOBOOT_KEYED)
88 static __inline__ int abortboot(int bootdelay)
91 uint64_t etime = endtick(bootdelay);
100 { str: getenv ("bootdelaykey"), retry: 1 },
101 { str: getenv ("bootdelaykey2"), retry: 1 },
102 { str: getenv ("bootstopkey"), retry: 0 },
103 { str: getenv ("bootstopkey2"), retry: 0 },
106 char presskey [MAX_DELAY_STOP_STR];
107 u_int presskey_len = 0;
108 u_int presskey_max = 0;
111 #ifdef CONFIG_SILENT_CONSOLE
112 if (gd->flags & GD_FLG_SILENT) {
113 /* Restore serial console */
114 console_assign (stdout, "serial");
115 console_assign (stderr, "serial");
119 # ifdef CONFIG_AUTOBOOT_PROMPT
120 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
123 # ifdef CONFIG_AUTOBOOT_DELAY_STR
124 if (delaykey[0].str == NULL)
125 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
127 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
128 if (delaykey[1].str == NULL)
129 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
131 # ifdef CONFIG_AUTOBOOT_STOP_STR
132 if (delaykey[2].str == NULL)
133 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
135 # ifdef CONFIG_AUTOBOOT_STOP_STR2
136 if (delaykey[3].str == NULL)
137 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
140 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
141 delaykey[i].len = delaykey[i].str == NULL ?
142 0 : strlen (delaykey[i].str);
143 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
144 MAX_DELAY_STOP_STR : delaykey[i].len;
146 presskey_max = presskey_max > delaykey[i].len ?
147 presskey_max : delaykey[i].len;
150 printf("%s key:<%s>\n",
151 delaykey[i].retry ? "delay" : "stop",
152 delaykey[i].str ? delaykey[i].str : "NULL");
156 /* In order to keep up with incoming data, check timeout only
159 while (!abort && get_ticks() <= etime) {
160 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
161 if (delaykey[i].len > 0 &&
162 presskey_len >= delaykey[i].len &&
163 memcmp (presskey + presskey_len - delaykey[i].len,
165 delaykey[i].len) == 0) {
167 printf("got %skey\n",
168 delaykey[i].retry ? "delay" : "stop");
171 # ifdef CONFIG_BOOT_RETRY_TIME
172 /* don't retry auto boot */
173 if (! delaykey[i].retry)
181 if (presskey_len < presskey_max) {
182 presskey [presskey_len ++] = getc();
185 for (i = 0; i < presskey_max - 1; i ++)
186 presskey [i] = presskey [i + 1];
188 presskey [i] = getc();
194 puts ("key timeout\n");
197 #ifdef CONFIG_SILENT_CONSOLE
199 /* permanently enable normal console output */
200 gd->flags &= ~(GD_FLG_SILENT);
201 } else if (gd->flags & GD_FLG_SILENT) {
202 /* Restore silent console */
203 console_assign (stdout, "nulldev");
204 console_assign (stderr, "nulldev");
211 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
213 #ifdef CONFIG_MENUKEY
214 static int menukey = 0;
217 static __inline__ int abortboot(int bootdelay)
221 #ifdef CONFIG_SILENT_CONSOLE
222 if (gd->flags & GD_FLG_SILENT) {
223 /* Restore serial console */
224 console_assign (stdout, "serial");
225 console_assign (stderr, "serial");
229 #ifdef CONFIG_MENUPROMPT
230 printf(CONFIG_MENUPROMPT, bootdelay);
232 printf("Hit any key to stop autoboot: %2d ", bootdelay);
235 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
237 * Check if key already pressed
238 * Don't check if bootdelay < 0
240 if (bootdelay >= 0) {
241 if (tstc()) { /* we got a key press */
242 (void) getc(); /* consume input */
244 abort = 1; /* don't auto boot */
249 while ((bootdelay > 0) && (!abort)) {
253 /* delay 100 * 10ms */
254 for (i=0; !abort && i<100; ++i) {
255 if (tstc()) { /* we got a key press */
256 abort = 1; /* don't auto boot */
257 bootdelay = 0; /* no more delay */
258 # ifdef CONFIG_MENUKEY
261 (void) getc(); /* consume input */
268 printf ("\b\b\b%2d ", bootdelay);
273 #ifdef CONFIG_SILENT_CONSOLE
275 /* permanently enable normal console output */
276 gd->flags &= ~(GD_FLG_SILENT);
277 } else if (gd->flags & GD_FLG_SILENT) {
278 /* Restore silent console */
279 console_assign (stdout, "nulldev");
280 console_assign (stderr, "nulldev");
286 # endif /* CONFIG_AUTOBOOT_KEYED */
287 #endif /* CONFIG_BOOTDELAY >= 0 */
289 /****************************************************************************/
291 void main_loop (void)
293 #ifndef CFG_HUSH_PARSER
294 static char lastcommand[CFG_CBSIZE] = { 0, };
300 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
304 #ifdef CONFIG_PREBOOT
307 #ifdef CONFIG_BOOTCOUNT_LIMIT
308 unsigned long bootcount = 0;
309 unsigned long bootlimit = 0;
312 #endif /* CONFIG_BOOTCOUNT_LIMIT */
314 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
315 ulong bmp = 0; /* default bitmap */
316 extern int trab_vfd (ulong bitmap);
318 #ifdef CONFIG_MODEM_SUPPORT
320 bmp = 1; /* alternate bitmap */
323 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
325 #ifdef CONFIG_BOOTCOUNT_LIMIT
326 bootcount = bootcount_load();
328 bootcount_store (bootcount);
329 sprintf (bcs_set, "%lu", bootcount);
330 setenv ("bootcount", bcs_set);
331 bcs = getenv ("bootlimit");
332 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
333 #endif /* CONFIG_BOOTCOUNT_LIMIT */
335 #ifdef CONFIG_MODEM_SUPPORT
336 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
338 char *str = strdup(getenv("mdm_cmd"));
339 setenv ("preboot", str); /* set or delete definition */
342 mdm_init(); /* wait for modem connection */
344 #endif /* CONFIG_MODEM_SUPPORT */
346 #ifdef CONFIG_VERSION_VARIABLE
348 extern char version_string[];
350 setenv ("ver", version_string); /* set version variable */
352 #endif /* CONFIG_VERSION_VARIABLE */
354 #ifdef CFG_HUSH_PARSER
355 u_boot_hush_start ();
358 #ifdef CONFIG_AUTO_COMPLETE
359 install_auto_complete();
362 #ifdef CONFIG_PREBOOT
363 if ((p = getenv ("preboot")) != NULL) {
364 # ifdef CONFIG_AUTOBOOT_KEYED
365 int prev = disable_ctrlc(1); /* disable Control C checking */
368 # ifndef CFG_HUSH_PARSER
371 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
372 FLAG_EXIT_FROM_LOOP);
375 # ifdef CONFIG_AUTOBOOT_KEYED
376 disable_ctrlc(prev); /* restore Control C checking */
379 #endif /* CONFIG_PREBOOT */
381 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
382 s = getenv ("bootdelay");
383 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
385 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
387 # ifdef CONFIG_BOOT_RETRY_TIME
389 # endif /* CONFIG_BOOT_RETRY_TIME */
391 #ifdef CONFIG_BOOTCOUNT_LIMIT
392 if (bootlimit && (bootcount > bootlimit)) {
393 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
394 (unsigned)bootlimit);
395 s = getenv ("altbootcmd");
398 #endif /* CONFIG_BOOTCOUNT_LIMIT */
399 s = getenv ("bootcmd");
401 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
403 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
404 # ifdef CONFIG_AUTOBOOT_KEYED
405 int prev = disable_ctrlc(1); /* disable Control C checking */
408 # ifndef CFG_HUSH_PARSER
411 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
412 FLAG_EXIT_FROM_LOOP);
415 # ifdef CONFIG_AUTOBOOT_KEYED
416 disable_ctrlc(prev); /* restore Control C checking */
420 # ifdef CONFIG_MENUKEY
421 if (menukey == CONFIG_MENUKEY) {
422 s = getenv("menucmd");
424 # ifndef CFG_HUSH_PARSER
427 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
428 FLAG_EXIT_FROM_LOOP);
432 #endif /* CONFIG_MENUKEY */
433 #endif /* CONFIG_BOOTDELAY */
435 #ifdef CONFIG_AMIGAONEG3SE
437 extern void video_banner(void);
443 * Main Loop for Monitor Command Processing
445 #ifdef CFG_HUSH_PARSER
447 /* This point is never reached */
451 #ifdef CONFIG_BOOT_RETRY_TIME
453 /* Saw enough of a valid command to
454 * restart the timeout.
459 len = readline (CFG_PROMPT);
461 flag = 0; /* assume no special flags for now */
463 strcpy (lastcommand, console_buffer);
465 flag |= CMD_FLAG_REPEAT;
466 #ifdef CONFIG_BOOT_RETRY_TIME
467 else if (len == -2) {
468 /* -2 means timed out, retry autoboot
470 puts ("\nTimed out waiting for command\n");
471 # ifdef CONFIG_RESET_TO_RETRY
472 /* Reinit board to run initialization code again */
473 do_reset (NULL, 0, 0, NULL);
475 return; /* retry autoboot */
481 puts ("<INTERRUPT>\n");
483 rc = run_command (lastcommand, flag);
486 /* invalid command or not repeatable, forget it */
490 #endif /*CFG_HUSH_PARSER*/
493 #ifdef CONFIG_BOOT_RETRY_TIME
494 /***************************************************************************
495 * initialise command line timeout
497 void init_cmd_timeout(void)
499 char *s = getenv ("bootretry");
502 retry_time = (int)simple_strtol(s, NULL, 10);
504 retry_time = CONFIG_BOOT_RETRY_TIME;
506 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
507 retry_time = CONFIG_BOOT_RETRY_MIN;
510 /***************************************************************************
511 * reset command line timeout to retry_time seconds
513 void reset_cmd_timeout(void)
515 endtime = endtick(retry_time);
519 /****************************************************************************/
522 * Prompt for input and read a line.
523 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
524 * time out when time goes past endtime (timebase time in ticks).
525 * Return: number of read characters
529 int readline (const char *const prompt)
531 char *p = console_buffer;
532 int n = 0; /* buffer index */
533 int plen = 0; /* prompt length */
534 int col; /* output column cnt */
539 plen = strlen (prompt);
545 #ifdef CONFIG_BOOT_RETRY_TIME
546 while (!tstc()) { /* while no incoming data */
547 if (retry_time >= 0 && get_ticks() > endtime)
548 return (-2); /* timed out */
551 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
553 #ifdef CONFIG_SHOW_ACTIVITY
555 extern void show_activity(int arg);
562 * Special character handling
565 case '\r': /* Enter */
569 return (p - console_buffer);
574 case 0x03: /* ^C - break */
575 console_buffer[0] = '\0'; /* discard input */
578 case 0x15: /* ^U - erase line */
587 case 0x17: /* ^W - erase word */
588 p=delete_char(console_buffer, p, &col, &n, plen);
589 while ((n > 0) && (*p != ' ')) {
590 p=delete_char(console_buffer, p, &col, &n, plen);
594 case 0x08: /* ^H - backspace */
595 case 0x7F: /* DEL - backspace */
596 p=delete_char(console_buffer, p, &col, &n, plen);
601 * Must be a normal character then
603 if (n < CFG_CBSIZE-2) {
604 if (c == '\t') { /* expand TABs */
605 #ifdef CONFIG_AUTO_COMPLETE
606 /* if auto completion triggered just continue */
608 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
609 p = console_buffer + n; /* reset */
613 puts (tab_seq+(col&07));
616 ++col; /* echo input */
621 } else { /* Buffer full */
628 /****************************************************************************/
630 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
638 if (*(--p) == '\t') { /* will retype the whole line */
639 while (*colp > plen) {
643 for (s=buffer; s<p; ++s) {
645 puts (tab_seq+((*colp) & 07));
646 *colp += 8 - ((*colp) & 07);
660 /****************************************************************************/
662 int parse_line (char *line, char *argv[])
667 printf ("parse_line: \"%s\"\n", line);
669 while (nargs < CFG_MAXARGS) {
671 /* skip any white space */
672 while ((*line == ' ') || (*line == '\t')) {
676 if (*line == '\0') { /* end of line, no more args */
679 printf ("parse_line: nargs=%d\n", nargs);
684 argv[nargs++] = line; /* begin of argument string */
686 /* find end of string */
687 while (*line && (*line != ' ') && (*line != '\t')) {
691 if (*line == '\0') { /* end of line, no more args */
694 printf ("parse_line: nargs=%d\n", nargs);
699 *line++ = '\0'; /* terminate current arg */
702 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
705 printf ("parse_line: nargs=%d\n", nargs);
710 /****************************************************************************/
712 static void process_macros (const char *input, char *output)
715 const char *varname_start = NULL;
716 int inputcnt = strlen (input);
717 int outputcnt = CFG_CBSIZE;
718 int state = 0; /* 0 = waiting for '$' */
719 /* 1 = waiting for '(' or '{' */
720 /* 2 = waiting for ')' or '}' */
721 /* 3 = waiting for ''' */
723 char *output_start = output;
725 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
728 prev = '\0'; /* previous character */
730 while (inputcnt && outputcnt) {
735 /* remove one level of escape characters */
736 if ((c == '\\') && (prev != '\\')) {
745 case 0: /* Waiting for (unescaped) $ */
746 if ((c == '\'') && (prev != '\\')) {
750 if ((c == '$') && (prev != '\\')) {
757 case 1: /* Waiting for ( */
758 if (c == '(' || c == '{') {
760 varname_start = input;
772 case 2: /* Waiting for ) */
773 if (c == ')' || c == '}') {
775 char envname[CFG_CBSIZE], *envval;
776 int envcnt = input-varname_start-1; /* Varname # of chars */
778 /* Get the varname */
779 for (i = 0; i < envcnt; i++) {
780 envname[i] = varname_start[i];
785 envval = getenv (envname);
787 /* Copy into the line if it exists */
789 while ((*envval) && outputcnt) {
790 *(output++) = *(envval++);
793 /* Look for another '$' */
797 case 3: /* Waiting for ' */
798 if ((c == '\'') && (prev != '\\')) {
813 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
814 strlen(output_start), output_start);
818 /****************************************************************************
820 * 1 - command executed, repeatable
821 * 0 - command executed but not repeatable, interrupted commands are
822 * always considered not repeatable
823 * -1 - not executed (unrecognized, bootd recursion or too many args)
824 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
825 * considered unrecognized)
829 * We must create a temporary copy of the command since the command we get
830 * may be the result from getenv(), which returns a pointer directly to
831 * the environment data, which may change magicly when the command we run
832 * creates or modifies environment variables (like "bootp" does).
835 int run_command (const char *cmd, int flag)
838 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
839 char *token; /* start of token in cmdbuf */
840 char *sep; /* end of token (separator) in cmdbuf */
841 char finaltoken[CFG_CBSIZE];
843 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
849 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
850 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
854 clear_ctrlc(); /* forget any previous Control C */
857 return -1; /* empty command */
860 if (strlen(cmd) >= CFG_CBSIZE) {
861 puts ("## Command too long!\n");
865 strcpy (cmdbuf, cmd);
867 /* Process separators and check for invalid
868 * repeatable commands
872 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
877 * Find separator, or string end
878 * Allow simple escape of ';' by writing "\;"
880 for (inquotes = 0, sep = str; *sep; sep++) {
886 (*sep == ';') && /* separator */
887 ( sep != str) && /* past string start */
888 (*(sep-1) != '\\')) /* and NOT escaped */
893 * Limit the token to data between separators
897 str = sep + 1; /* start of command for next pass */
901 str = sep; /* no more commands for next pass */
903 printf ("token: \"%s\"\n", token);
906 /* find macros in this token and replace them */
907 process_macros (token, finaltoken);
909 /* Extract arguments */
910 if ((argc = parse_line (finaltoken, argv)) == 0) {
911 rc = -1; /* no command at all */
915 /* Look up command in command table */
916 if ((cmdtp = find_cmd(argv[0])) == NULL) {
917 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
918 rc = -1; /* give up after bad command */
922 /* found - check max args */
923 if (argc > cmdtp->maxargs) {
924 printf ("Usage:\n%s\n", cmdtp->usage);
929 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
930 /* avoid "bootd" recursion */
931 if (cmdtp->cmd == do_bootd) {
933 printf ("[%s]\n", finaltoken);
935 if (flag & CMD_FLAG_BOOTD) {
936 puts ("'bootd' recursion detected\n");
940 flag |= CMD_FLAG_BOOTD;
943 #endif /* CFG_CMD_BOOTD */
945 /* OK - call function to do the command */
946 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
950 repeatable &= cmdtp->repeatable;
952 /* Did the user stop this? */
954 return 0; /* if stopped then not repeatable */
957 return rc ? rc : repeatable;
960 /****************************************************************************/
962 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
963 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
968 printf ("Usage:\n%s\n", cmdtp->usage);
972 for (i=1; i<argc; ++i) {
975 if ((arg = getenv (argv[i])) == NULL) {
976 printf ("## Error: \"%s\" not defined\n", argv[i]);
979 #ifndef CFG_HUSH_PARSER
980 if (run_command (arg, flag) == -1)
983 if (parse_string_outer(arg,
984 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
990 #endif /* CFG_CMD_RUN */