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,
30 #ifdef CFG_HUSH_PARSER
36 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
37 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
40 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
43 #define MAX_DELAY_STOP_STR 32
45 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
46 static int parse_line (char *, char *[]);
47 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
48 static int abortboot(int);
53 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
55 static char erase_seq[] = "\b \b"; /* erase sequence */
56 static char tab_seq[] = " "; /* used to expand TABs */
58 #ifdef CONFIG_BOOT_RETRY_TIME
59 static uint64_t endtime = 0; /* must be set, default is instant timeout */
60 static int retry_time = -1; /* -1 so can call readline before main_loop */
63 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
65 #ifndef CONFIG_BOOT_RETRY_MIN
66 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
69 #ifdef CONFIG_MODEM_SUPPORT
71 extern void mdm_init(void); /* defined in board.c */
74 /***************************************************************************
75 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
76 * returns: 0 - no key string, allow autoboot
77 * 1 - got key string, abort
79 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
80 # if defined(CONFIG_AUTOBOOT_KEYED)
81 static __inline__ int abortboot(int bootdelay)
84 uint64_t etime = endtick(bootdelay);
93 { str: getenv ("bootdelaykey"), retry: 1 },
94 { str: getenv ("bootdelaykey2"), retry: 1 },
95 { str: getenv ("bootstopkey"), retry: 0 },
96 { str: getenv ("bootstopkey2"), retry: 0 },
99 char presskey [MAX_DELAY_STOP_STR];
100 u_int presskey_len = 0;
101 u_int presskey_max = 0;
104 #ifdef CONFIG_SILENT_CONSOLE
106 DECLARE_GLOBAL_DATA_PTR;
108 if (gd->flags & GD_FLG_SILENT) {
109 /* Restore serial console */
110 console_assign (stdout, "serial");
111 console_assign (stderr, "serial");
116 # ifdef CONFIG_AUTOBOOT_PROMPT
117 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
120 # ifdef CONFIG_AUTOBOOT_DELAY_STR
121 if (delaykey[0].str == NULL)
122 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
124 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
125 if (delaykey[1].str == NULL)
126 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
128 # ifdef CONFIG_AUTOBOOT_STOP_STR
129 if (delaykey[2].str == NULL)
130 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
132 # ifdef CONFIG_AUTOBOOT_STOP_STR2
133 if (delaykey[3].str == NULL)
134 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
137 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
138 delaykey[i].len = delaykey[i].str == NULL ?
139 0 : strlen (delaykey[i].str);
140 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
141 MAX_DELAY_STOP_STR : delaykey[i].len;
143 presskey_max = presskey_max > delaykey[i].len ?
144 presskey_max : delaykey[i].len;
147 printf("%s key:<%s>\n",
148 delaykey[i].retry ? "delay" : "stop",
149 delaykey[i].str ? delaykey[i].str : "NULL");
153 /* In order to keep up with incoming data, check timeout only
156 while (!abort && get_ticks() <= etime) {
157 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
158 if (delaykey[i].len > 0 &&
159 presskey_len >= delaykey[i].len &&
160 memcmp (presskey + presskey_len - delaykey[i].len,
162 delaykey[i].len) == 0) {
164 printf("got %skey\n",
165 delaykey[i].retry ? "delay" : "stop");
168 # ifdef CONFIG_BOOT_RETRY_TIME
169 /* don't retry auto boot */
170 if (! delaykey[i].retry)
178 if (presskey_len < presskey_max) {
179 presskey [presskey_len ++] = getc();
182 for (i = 0; i < presskey_max - 1; i ++)
183 presskey [i] = presskey [i + 1];
185 presskey [i] = getc();
191 puts ("key timeout\n");
194 #ifdef CONFIG_SILENT_CONSOLE
196 DECLARE_GLOBAL_DATA_PTR;
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");
212 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
214 #ifdef CONFIG_MENUKEY
215 static int menukey = 0;
218 static __inline__ int abortboot(int bootdelay)
222 #ifdef CONFIG_SILENT_CONSOLE
224 DECLARE_GLOBAL_DATA_PTR;
226 if (gd->flags & GD_FLG_SILENT) {
227 /* Restore serial console */
228 console_assign (stdout, "serial");
229 console_assign (stderr, "serial");
234 #ifdef CONFIG_MENUPROMPT
235 printf(CONFIG_MENUPROMPT, bootdelay);
237 printf("Hit any key to stop autoboot: %2d ", bootdelay);
240 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
242 * Check if key already pressed
243 * Don't check if bootdelay < 0
245 if (bootdelay >= 0) {
246 if (tstc()) { /* we got a key press */
247 (void) getc(); /* consume input */
249 abort = 1; /* don't auto boot */
254 while ((bootdelay > 0) && (!abort)) {
258 /* delay 100 * 10ms */
259 for (i=0; !abort && i<100; ++i) {
260 if (tstc()) { /* we got a key press */
261 abort = 1; /* don't auto boot */
262 bootdelay = 0; /* no more delay */
263 # ifdef CONFIG_MENUKEY
266 (void) getc(); /* consume input */
273 printf ("\b\b\b%2d ", bootdelay);
278 #ifdef CONFIG_SILENT_CONSOLE
280 DECLARE_GLOBAL_DATA_PTR;
283 /* permanently enable normal console output */
284 gd->flags &= ~(GD_FLG_SILENT);
285 } else if (gd->flags & GD_FLG_SILENT) {
286 /* Restore silent console */
287 console_assign (stdout, "nulldev");
288 console_assign (stderr, "nulldev");
295 # endif /* CONFIG_AUTOBOOT_KEYED */
296 #endif /* CONFIG_BOOTDELAY >= 0 */
298 /****************************************************************************/
300 void main_loop (void)
302 #ifndef CFG_HUSH_PARSER
303 static char lastcommand[CFG_CBSIZE] = { 0, };
309 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
313 #ifdef CONFIG_PREBOOT
316 #ifdef CONFIG_BOOTCOUNT_LIMIT
317 unsigned long bootcount = 0;
318 unsigned long bootlimit = 0;
321 #endif /* CONFIG_BOOTCOUNT_LIMIT */
323 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
324 ulong bmp = 0; /* default bitmap */
325 extern int trab_vfd (ulong bitmap);
327 #ifdef CONFIG_MODEM_SUPPORT
329 bmp = 1; /* alternate bitmap */
332 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
334 #ifdef CONFIG_BOOTCOUNT_LIMIT
335 bootcount = bootcount_load();
337 bootcount_store (bootcount);
338 sprintf (bcs_set, "%lu", bootcount);
339 setenv ("bootcount", bcs_set);
340 bcs = getenv ("bootlimit");
341 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
342 #endif /* CONFIG_BOOTCOUNT_LIMIT */
344 #ifdef CONFIG_MODEM_SUPPORT
345 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
347 uchar *str = strdup(getenv("mdm_cmd"));
348 setenv ("preboot", str); /* set or delete definition */
351 mdm_init(); /* wait for modem connection */
353 #endif /* CONFIG_MODEM_SUPPORT */
355 #ifdef CONFIG_VERSION_VARIABLE
357 extern char version_string[];
359 setenv ("ver", version_string); /* set version variable */
361 #endif /* CONFIG_VERSION_VARIABLE */
363 #ifdef CFG_HUSH_PARSER
364 u_boot_hush_start ();
367 #ifdef CONFIG_AUTO_COMPLETE
368 install_auto_complete();
371 #ifdef CONFIG_PREBOOT
372 if ((p = getenv ("preboot")) != NULL) {
373 # ifdef CONFIG_AUTOBOOT_KEYED
374 int prev = disable_ctrlc(1); /* disable Control C checking */
377 # ifndef CFG_HUSH_PARSER
380 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
381 FLAG_EXIT_FROM_LOOP);
384 # ifdef CONFIG_AUTOBOOT_KEYED
385 disable_ctrlc(prev); /* restore Control C checking */
388 #endif /* CONFIG_PREBOOT */
390 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
391 s = getenv ("bootdelay");
392 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
394 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
396 # ifdef CONFIG_BOOT_RETRY_TIME
398 # endif /* CONFIG_BOOT_RETRY_TIME */
400 #ifdef CONFIG_BOOTCOUNT_LIMIT
401 if (bootlimit && (bootcount > bootlimit)) {
402 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
403 (unsigned)bootlimit);
404 s = getenv ("altbootcmd");
407 #endif /* CONFIG_BOOTCOUNT_LIMIT */
408 s = getenv ("bootcmd");
410 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
412 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
413 # ifdef CONFIG_AUTOBOOT_KEYED
414 int prev = disable_ctrlc(1); /* disable Control C checking */
417 # ifndef CFG_HUSH_PARSER
420 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
421 FLAG_EXIT_FROM_LOOP);
424 # ifdef CONFIG_AUTOBOOT_KEYED
425 disable_ctrlc(prev); /* restore Control C checking */
429 # ifdef CONFIG_MENUKEY
430 if (menukey == CONFIG_MENUKEY) {
431 s = getenv("menucmd");
433 # ifndef CFG_HUSH_PARSER
436 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
437 FLAG_EXIT_FROM_LOOP);
441 #endif /* CONFIG_MENUKEY */
442 #endif /* CONFIG_BOOTDELAY */
444 #ifdef CONFIG_AMIGAONEG3SE
446 extern void video_banner(void);
452 * Main Loop for Monitor Command Processing
454 #ifdef CFG_HUSH_PARSER
456 /* This point is never reached */
460 #ifdef CONFIG_BOOT_RETRY_TIME
462 /* Saw enough of a valid command to
463 * restart the timeout.
468 len = readline (CFG_PROMPT);
470 flag = 0; /* assume no special flags for now */
472 strcpy (lastcommand, console_buffer);
474 flag |= CMD_FLAG_REPEAT;
475 #ifdef CONFIG_BOOT_RETRY_TIME
476 else if (len == -2) {
477 /* -2 means timed out, retry autoboot
479 puts ("\nTimed out waiting for command\n");
480 # ifdef CONFIG_RESET_TO_RETRY
481 /* Reinit board to run initialization code again */
482 do_reset (NULL, 0, 0, NULL);
484 return; /* retry autoboot */
490 puts ("<INTERRUPT>\n");
492 rc = run_command (lastcommand, flag);
495 /* invalid command or not repeatable, forget it */
499 #endif /*CFG_HUSH_PARSER*/
502 #ifdef CONFIG_BOOT_RETRY_TIME
503 /***************************************************************************
504 * initialise command line timeout
506 void init_cmd_timeout(void)
508 char *s = getenv ("bootretry");
511 retry_time = (int)simple_strtol(s, NULL, 10);
513 retry_time = CONFIG_BOOT_RETRY_TIME;
515 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
516 retry_time = CONFIG_BOOT_RETRY_MIN;
519 /***************************************************************************
520 * reset command line timeout to retry_time seconds
522 void reset_cmd_timeout(void)
524 endtime = endtick(retry_time);
528 /****************************************************************************/
531 * Prompt for input and read a line.
532 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
533 * time out when time goes past endtime (timebase time in ticks).
534 * Return: number of read characters
538 int readline (const char *const prompt)
540 char *p = console_buffer;
541 int n = 0; /* buffer index */
542 int plen = 0; /* prompt length */
543 int col; /* output column cnt */
548 plen = strlen (prompt);
554 #ifdef CONFIG_BOOT_RETRY_TIME
555 while (!tstc()) { /* while no incoming data */
556 if (retry_time >= 0 && get_ticks() > endtime)
557 return (-2); /* timed out */
560 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
562 #ifdef CONFIG_SHOW_ACTIVITY
564 extern void show_activity(int arg);
571 * Special character handling
574 case '\r': /* Enter */
578 return (p - console_buffer);
583 case 0x03: /* ^C - break */
584 console_buffer[0] = '\0'; /* discard input */
587 case 0x15: /* ^U - erase line */
596 case 0x17: /* ^W - erase word */
597 p=delete_char(console_buffer, p, &col, &n, plen);
598 while ((n > 0) && (*p != ' ')) {
599 p=delete_char(console_buffer, p, &col, &n, plen);
603 case 0x08: /* ^H - backspace */
604 case 0x7F: /* DEL - backspace */
605 p=delete_char(console_buffer, p, &col, &n, plen);
610 * Must be a normal character then
612 if (n < CFG_CBSIZE-2) {
613 if (c == '\t') { /* expand TABs */
614 #ifdef CONFIG_AUTO_COMPLETE
615 /* if auto completion triggered just continue */
617 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
618 p = console_buffer + n; /* reset */
622 puts (tab_seq+(col&07));
625 ++col; /* echo input */
630 } else { /* Buffer full */
637 /****************************************************************************/
639 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
647 if (*(--p) == '\t') { /* will retype the whole line */
648 while (*colp > plen) {
652 for (s=buffer; s<p; ++s) {
654 puts (tab_seq+((*colp) & 07));
655 *colp += 8 - ((*colp) & 07);
669 /****************************************************************************/
671 int parse_line (char *line, char *argv[])
676 printf ("parse_line: \"%s\"\n", line);
678 while (nargs < CFG_MAXARGS) {
680 /* skip any white space */
681 while ((*line == ' ') || (*line == '\t')) {
685 if (*line == '\0') { /* end of line, no more args */
688 printf ("parse_line: nargs=%d\n", nargs);
693 argv[nargs++] = line; /* begin of argument string */
695 /* find end of string */
696 while (*line && (*line != ' ') && (*line != '\t')) {
700 if (*line == '\0') { /* end of line, no more args */
703 printf ("parse_line: nargs=%d\n", nargs);
708 *line++ = '\0'; /* terminate current arg */
711 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
714 printf ("parse_line: nargs=%d\n", nargs);
719 /****************************************************************************/
721 static void process_macros (const char *input, char *output)
724 const char *varname_start = NULL;
725 int inputcnt = strlen (input);
726 int outputcnt = CFG_CBSIZE;
727 int state = 0; /* 0 = waiting for '$' */
728 /* 1 = waiting for '(' or '{' */
729 /* 2 = waiting for ')' or '}' */
730 /* 3 = waiting for ''' */
732 char *output_start = output;
734 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
737 prev = '\0'; /* previous character */
739 while (inputcnt && outputcnt) {
744 /* remove one level of escape characters */
745 if ((c == '\\') && (prev != '\\')) {
754 case 0: /* Waiting for (unescaped) $ */
755 if ((c == '\'') && (prev != '\\')) {
759 if ((c == '$') && (prev != '\\')) {
766 case 1: /* Waiting for ( */
767 if (c == '(' || c == '{') {
769 varname_start = input;
781 case 2: /* Waiting for ) */
782 if (c == ')' || c == '}') {
784 char envname[CFG_CBSIZE], *envval;
785 int envcnt = input-varname_start-1; /* Varname # of chars */
787 /* Get the varname */
788 for (i = 0; i < envcnt; i++) {
789 envname[i] = varname_start[i];
794 envval = getenv (envname);
796 /* Copy into the line if it exists */
798 while ((*envval) && outputcnt) {
799 *(output++) = *(envval++);
802 /* Look for another '$' */
806 case 3: /* Waiting for ' */
807 if ((c == '\'') && (prev != '\\')) {
822 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
823 strlen(output_start), output_start);
827 /****************************************************************************
829 * 1 - command executed, repeatable
830 * 0 - command executed but not repeatable, interrupted commands are
831 * always considered not repeatable
832 * -1 - not executed (unrecognized, bootd recursion or too many args)
833 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
834 * considered unrecognized)
838 * We must create a temporary copy of the command since the command we get
839 * may be the result from getenv(), which returns a pointer directly to
840 * the environment data, which may change magicly when the command we run
841 * creates or modifies environment variables (like "bootp" does).
844 int run_command (const char *cmd, int flag)
847 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
848 char *token; /* start of token in cmdbuf */
849 char *sep; /* end of token (separator) in cmdbuf */
850 char finaltoken[CFG_CBSIZE];
852 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
858 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
859 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
863 clear_ctrlc(); /* forget any previous Control C */
866 return -1; /* empty command */
869 if (strlen(cmd) >= CFG_CBSIZE) {
870 puts ("## Command too long!\n");
874 strcpy (cmdbuf, cmd);
876 /* Process separators and check for invalid
877 * repeatable commands
881 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
886 * Find separator, or string end
887 * Allow simple escape of ';' by writing "\;"
889 for (inquotes = 0, sep = str; *sep; sep++) {
895 (*sep == ';') && /* separator */
896 ( sep != str) && /* past string start */
897 (*(sep-1) != '\\')) /* and NOT escaped */
902 * Limit the token to data between separators
906 str = sep + 1; /* start of command for next pass */
910 str = sep; /* no more commands for next pass */
912 printf ("token: \"%s\"\n", token);
915 /* find macros in this token and replace them */
916 process_macros (token, finaltoken);
918 /* Extract arguments */
919 argc = parse_line (finaltoken, argv);
921 /* Look up command in command table */
922 if ((cmdtp = find_cmd(argv[0])) == NULL) {
923 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
924 rc = -1; /* give up after bad command */
928 /* found - check max args */
929 if (argc > cmdtp->maxargs) {
930 printf ("Usage:\n%s\n", cmdtp->usage);
935 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
936 /* avoid "bootd" recursion */
937 if (cmdtp->cmd == do_bootd) {
939 printf ("[%s]\n", finaltoken);
941 if (flag & CMD_FLAG_BOOTD) {
942 puts ("'bootd' recursion detected\n");
947 flag |= CMD_FLAG_BOOTD;
949 #endif /* CFG_CMD_BOOTD */
951 /* OK - call function to do the command */
952 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
956 repeatable &= cmdtp->repeatable;
958 /* Did the user stop this? */
960 return 0; /* if stopped then not repeatable */
963 return rc ? rc : repeatable;
966 /****************************************************************************/
968 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
969 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
974 printf ("Usage:\n%s\n", cmdtp->usage);
978 for (i=1; i<argc; ++i) {
981 if ((arg = getenv (argv[i])) == NULL) {
982 printf ("## Error: \"%s\" not defined\n", argv[i]);
985 #ifndef CFG_HUSH_PARSER
986 if (run_command (arg, flag) == -1)
989 if (parse_string_outer(arg,
990 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
996 #endif /* CFG_CMD_RUN */