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
35 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
36 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
39 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
42 #define MAX_DELAY_STOP_STR 32
44 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
45 static int parse_line (char *, char *[]);
46 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
47 static int abortboot(int);
52 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
54 static char erase_seq[] = "\b \b"; /* erase sequence */
55 static char tab_seq[] = " "; /* used to expand TABs */
57 #ifdef CONFIG_BOOT_RETRY_TIME
58 static uint64_t endtime = 0; /* must be set, default is instant timeout */
59 static int retry_time = -1; /* -1 so can call readline before main_loop */
62 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
64 #ifndef CONFIG_BOOT_RETRY_MIN
65 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
68 #ifdef CONFIG_MODEM_SUPPORT
70 extern void mdm_init(void); /* defined in board.c */
73 /***************************************************************************
74 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
75 * returns: 0 - no key string, allow autoboot
76 * 1 - got key string, abort
78 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
79 # if defined(CONFIG_AUTOBOOT_KEYED)
80 static __inline__ int abortboot(int bootdelay)
83 uint64_t etime = endtick(bootdelay);
92 { str: getenv ("bootdelaykey"), retry: 1 },
93 { str: getenv ("bootdelaykey2"), retry: 1 },
94 { str: getenv ("bootstopkey"), retry: 0 },
95 { str: getenv ("bootstopkey2"), retry: 0 },
98 char presskey [MAX_DELAY_STOP_STR];
99 u_int presskey_len = 0;
100 u_int presskey_max = 0;
103 # ifdef CONFIG_AUTOBOOT_PROMPT
104 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
107 # ifdef CONFIG_AUTOBOOT_DELAY_STR
108 if (delaykey[0].str == NULL)
109 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
111 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
112 if (delaykey[1].str == NULL)
113 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
115 # ifdef CONFIG_AUTOBOOT_STOP_STR
116 if (delaykey[2].str == NULL)
117 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
119 # ifdef CONFIG_AUTOBOOT_STOP_STR2
120 if (delaykey[3].str == NULL)
121 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
124 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
125 delaykey[i].len = delaykey[i].str == NULL ?
126 0 : strlen (delaykey[i].str);
127 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
128 MAX_DELAY_STOP_STR : delaykey[i].len;
130 presskey_max = presskey_max > delaykey[i].len ?
131 presskey_max : delaykey[i].len;
134 printf("%s key:<%s>\n",
135 delaykey[i].retry ? "delay" : "stop",
136 delaykey[i].str ? delaykey[i].str : "NULL");
140 /* In order to keep up with incoming data, check timeout only
143 while (!abort && get_ticks() <= etime) {
144 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
145 if (delaykey[i].len > 0 &&
146 presskey_len >= delaykey[i].len &&
147 memcmp (presskey + presskey_len - delaykey[i].len,
149 delaykey[i].len) == 0) {
151 printf("got %skey\n",
152 delaykey[i].retry ? "delay" : "stop");
155 # ifdef CONFIG_BOOT_RETRY_TIME
156 /* don't retry auto boot */
157 if (! delaykey[i].retry)
165 if (presskey_len < presskey_max) {
166 presskey [presskey_len ++] = getc();
169 for (i = 0; i < presskey_max - 1; i ++)
170 presskey [i] = presskey [i + 1];
172 presskey [i] = getc();
178 printf("key timeout\n");
184 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
186 #ifdef CONFIG_MENUKEY
187 static int menukey = 0;
190 static __inline__ int abortboot(int bootdelay)
194 #ifdef CONFIG_MENUPROMPT
195 printf(CONFIG_MENUPROMPT, bootdelay);
197 printf("Hit any key to stop autoboot: %2d ", bootdelay);
200 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
202 * Check if key already pressed
203 * Don't check if bootdelay < 0
205 if (bootdelay >= 0) {
206 if (tstc()) { /* we got a key press */
207 (void) getc(); /* consume input */
208 printf ("\b\b\b 0\n");
209 return 1; /* don't auto boot */
214 while (bootdelay > 0) {
218 /* delay 100 * 10ms */
219 for (i=0; !abort && i<100; ++i) {
220 if (tstc()) { /* we got a key press */
221 abort = 1; /* don't auto boot */
222 bootdelay = 0; /* no more delay */
223 # ifdef CONFIG_MENUKEY
226 (void) getc(); /* consume input */
233 printf ("\b\b\b%2d ", bootdelay);
240 # endif /* CONFIG_AUTOBOOT_KEYED */
241 #endif /* CONFIG_BOOTDELAY >= 0 */
243 /****************************************************************************/
245 void main_loop (void)
247 #ifndef CFG_HUSH_PARSER
248 static char lastcommand[CFG_CBSIZE] = { 0, };
254 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
258 #ifdef CONFIG_PREBOOT
262 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
263 ulong bmp = 0; /* default bitmap */
264 extern int trab_vfd (ulong bitmap);
266 #ifdef CONFIG_MODEM_SUPPORT
268 bmp = 1; /* alternate bitmap */
271 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
273 #ifdef CONFIG_MODEM_SUPPORT
274 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
276 uchar *str = strdup(getenv("mdm_cmd"));
277 setenv ("preboot", str); /* set or delete definition */
280 mdm_init(); /* wait for modem connection */
282 #endif /* CONFIG_MODEM_SUPPORT */
284 #ifdef CONFIG_VERSION_VARIABLE
286 extern char version_string[];
287 char *str = getenv("ver");
290 setenv ("ver", version_string); /* set version variable */
292 #endif /* CONFIG_VERSION_VARIABLE */
294 #ifdef CFG_HUSH_PARSER
295 u_boot_hush_start ();
298 #ifdef CONFIG_PREBOOT
299 if ((p = getenv ("preboot")) != NULL) {
300 # ifdef CONFIG_AUTOBOOT_KEYED
301 int prev = disable_ctrlc(1); /* disable Control C checking */
304 # ifndef CFG_HUSH_PARSER
307 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
308 FLAG_EXIT_FROM_LOOP);
311 # ifdef CONFIG_AUTOBOOT_KEYED
312 disable_ctrlc(prev); /* restore Control C checking */
315 #endif /* CONFIG_PREBOOT */
317 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
318 s = getenv ("bootdelay");
319 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
321 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
323 # ifdef CONFIG_BOOT_RETRY_TIME
325 # endif /* CONFIG_BOOT_RETRY_TIME */
327 s = getenv ("bootcmd");
329 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
331 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
332 # ifdef CONFIG_AUTOBOOT_KEYED
333 int prev = disable_ctrlc(1); /* disable Control C checking */
336 # ifndef CFG_HUSH_PARSER
339 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
340 FLAG_EXIT_FROM_LOOP);
343 # ifdef CONFIG_AUTOBOOT_KEYED
344 disable_ctrlc(prev); /* restore Control C checking */
348 # ifdef CONFIG_MENUKEY
349 if (menukey == CONFIG_MENUKEY) {
350 s = getenv("menucmd");
352 # ifndef CFG_HUSH_PARSER
353 run_command (s, bd, 0);
355 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
356 FLAG_EXIT_FROM_LOOP);
360 #endif /* CONFIG_MENUKEY */
361 #endif /* CONFIG_BOOTDELAY */
363 #ifdef CONFIG_AMIGAONEG3SE
365 extern void video_banner(void);
371 * Main Loop for Monitor Command Processing
373 #ifdef CFG_HUSH_PARSER
375 /* This point is never reached */
379 #ifdef CONFIG_BOOT_RETRY_TIME
381 /* Saw enough of a valid command to
382 * restart the timeout.
387 len = readline (CFG_PROMPT);
389 flag = 0; /* assume no special flags for now */
391 strcpy (lastcommand, console_buffer);
393 flag |= CMD_FLAG_REPEAT;
394 #ifdef CONFIG_BOOT_RETRY_TIME
395 else if (len == -2) {
396 /* -2 means timed out, retry autoboot
398 printf("\nTimed out waiting for command\n");
399 # ifdef CONFIG_RESET_TO_RETRY
400 /* Reinit board to run initialization code again */
401 do_reset (NULL, 0, 0, NULL);
403 return; /* retry autoboot */
409 printf ("<INTERRUPT>\n");
411 rc = run_command (lastcommand, flag);
414 /* invalid command or not repeatable, forget it */
418 #endif /*CFG_HUSH_PARSER*/
421 #ifdef CONFIG_BOOT_RETRY_TIME
422 /***************************************************************************
423 * initialise command line timeout
425 void init_cmd_timeout(void)
427 char *s = getenv ("bootretry");
430 retry_time = (int)simple_strtoul(s, NULL, 10);
432 retry_time = CONFIG_BOOT_RETRY_TIME;
434 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
435 retry_time = CONFIG_BOOT_RETRY_MIN;
438 /***************************************************************************
439 * reset command line timeout to retry_time seconds
441 void reset_cmd_timeout(void)
443 endtime = endtick(retry_time);
447 /****************************************************************************/
450 * Prompt for input and read a line.
451 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
452 * time out when time goes past endtime (timebase time in ticks).
453 * Return: number of read characters
457 int readline (const char *const prompt)
459 char *p = console_buffer;
460 int n = 0; /* buffer index */
461 int plen = 0; /* prompt length */
462 int col; /* output column cnt */
467 plen = strlen (prompt);
473 #ifdef CONFIG_BOOT_RETRY_TIME
474 while (!tstc()) { /* while no incoming data */
475 if (retry_time >= 0 && get_ticks() > endtime)
476 return (-2); /* timed out */
479 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
481 #ifdef CONFIG_SHOW_ACTIVITY
483 extern void show_activity(int arg);
490 * Special character handling
493 case '\r': /* Enter */
497 return (p - console_buffer);
499 case 0x03: /* ^C - break */
500 console_buffer[0] = '\0'; /* discard input */
503 case 0x15: /* ^U - erase line */
512 case 0x17: /* ^W - erase word */
513 p=delete_char(console_buffer, p, &col, &n, plen);
514 while ((n > 0) && (*p != ' ')) {
515 p=delete_char(console_buffer, p, &col, &n, plen);
519 case 0x08: /* ^H - backspace */
520 case 0x7F: /* DEL - backspace */
521 p=delete_char(console_buffer, p, &col, &n, plen);
526 * Must be a normal character then
528 if (n < CFG_CBSIZE-2) {
529 if (c == '\t') { /* expand TABs */
530 puts (tab_seq+(col&07));
533 ++col; /* echo input */
538 } else { /* Buffer full */
545 /****************************************************************************/
547 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
555 if (*(--p) == '\t') { /* will retype the whole line */
556 while (*colp > plen) {
560 for (s=buffer; s<p; ++s) {
562 puts (tab_seq+((*colp) & 07));
563 *colp += 8 - ((*colp) & 07);
577 /****************************************************************************/
579 int parse_line (char *line, char *argv[])
584 printf ("parse_line: \"%s\"\n", line);
586 while (nargs < CFG_MAXARGS) {
588 /* skip any white space */
589 while ((*line == ' ') || (*line == '\t')) {
593 if (*line == '\0') { /* end of line, no more args */
596 printf ("parse_line: nargs=%d\n", nargs);
601 argv[nargs++] = line; /* begin of argument string */
603 /* find end of string */
604 while (*line && (*line != ' ') && (*line != '\t')) {
608 if (*line == '\0') { /* end of line, no more args */
611 printf ("parse_line: nargs=%d\n", nargs);
616 *line++ = '\0'; /* terminate current arg */
619 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
622 printf ("parse_line: nargs=%d\n", nargs);
627 /****************************************************************************/
629 static void process_macros (const char *input, char *output)
632 const char *varname_start = NULL;
633 int inputcnt = strlen (input);
634 int outputcnt = CFG_CBSIZE;
635 int state = 0; /* 0 = waiting for '$' */
636 /* 1 = waiting for '(' */
637 /* 2 = waiting for ')' */
638 /* 3 = waiting for ''' */
640 char *output_start = output;
642 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
645 prev = '\0'; /* previous character */
647 while (inputcnt && outputcnt) {
652 /* remove one level of escape characters */
653 if ((c == '\\') && (prev != '\\')) {
662 case 0: /* Waiting for (unescaped) $ */
663 if ((c == '\'') && (prev != '\\')) {
667 if ((c == '$') && (prev != '\\')) {
674 case 1: /* Waiting for ( */
677 varname_start = input;
689 case 2: /* Waiting for ) */
692 char envname[CFG_CBSIZE], *envval;
693 int envcnt = input-varname_start-1; /* Varname # of chars */
695 /* Get the varname */
696 for (i = 0; i < envcnt; i++) {
697 envname[i] = varname_start[i];
702 envval = getenv (envname);
704 /* Copy into the line if it exists */
706 while ((*envval) && outputcnt) {
707 *(output++) = *(envval++);
710 /* Look for another '$' */
714 case 3: /* Waiting for ' */
715 if ((c == '\'') && (prev != '\\')) {
730 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
731 strlen(output_start), output_start);
735 /****************************************************************************
737 * 1 - command executed, repeatable
738 * 0 - command executed but not repeatable, interrupted commands are
739 * always considered not repeatable
740 * -1 - not executed (unrecognized, bootd recursion or too many args)
741 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
742 * considered unrecognized)
746 * We must create a temporary copy of the command since the command we get
747 * may be the result from getenv(), which returns a pointer directly to
748 * the environment data, which may change magicly when the command we run
749 * creates or modifies environment variables (like "bootp" does).
752 int run_command (const char *cmd, int flag)
755 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
756 char *token; /* start of token in cmdbuf */
757 char *sep; /* end of token (separator) in cmdbuf */
758 char finaltoken[CFG_CBSIZE];
760 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
766 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
767 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
771 clear_ctrlc(); /* forget any previous Control C */
774 return -1; /* empty command */
777 if (strlen(cmd) >= CFG_CBSIZE) {
778 puts ("## Command too long!\n");
782 strcpy (cmdbuf, cmd);
784 /* Process separators and check for invalid
785 * repeatable commands
789 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
794 * Find separator, or string end
795 * Allow simple escape of ';' by writing "\;"
797 for (inquotes = 0, sep = str; *sep; sep++) {
803 (*sep == ';') && /* separator */
804 ( sep != str) && /* past string start */
805 (*(sep-1) != '\\')) /* and NOT escaped */
810 * Limit the token to data between separators
814 str = sep + 1; /* start of command for next pass */
818 str = sep; /* no more commands for next pass */
820 printf ("token: \"%s\"\n", token);
823 /* find macros in this token and replace them */
824 process_macros (token, finaltoken);
826 /* Extract arguments */
827 argc = parse_line (finaltoken, argv);
829 /* Look up command in command table */
830 if ((cmdtp = find_cmd(argv[0])) == NULL) {
831 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
832 rc = -1; /* give up after bad command */
836 /* found - check max args */
837 if (argc > cmdtp->maxargs) {
838 printf ("Usage:\n%s\n", cmdtp->usage);
843 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
844 /* avoid "bootd" recursion */
845 if (cmdtp->cmd == do_bootd) {
847 printf ("[%s]\n", finaltoken);
849 if (flag & CMD_FLAG_BOOTD) {
850 printf ("'bootd' recursion detected\n");
855 flag |= CMD_FLAG_BOOTD;
857 #endif /* CFG_CMD_BOOTD */
859 /* OK - call function to do the command */
860 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
864 repeatable &= cmdtp->repeatable;
866 /* Did the user stop this? */
868 return 0; /* if stopped then not repeatable */
871 return rc ? rc : repeatable;
874 /****************************************************************************/
876 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
877 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
882 printf ("Usage:\n%s\n", cmdtp->usage);
886 for (i=1; i<argc; ++i) {
889 if ((arg = getenv (argv[i])) == NULL) {
890 printf ("## Error: \"%s\" not defined\n", argv[i]);
893 #ifndef CFG_HUSH_PARSER
894 if (run_command (arg, flag) == -1)
897 if (parse_string_outer(arg,
898 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
904 #endif /* CFG_CMD_RUN */