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 #include <cmd_nvedit.h>
30 #include <cmd_bootm.h>
32 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
33 #include <cmd_boot.h> /* for do_reset() prototype */
36 #ifdef CFG_HUSH_PARSER
40 #define MAX_DELAY_STOP_STR 32
42 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
43 static int parse_line (char *, char *[]);
44 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
45 static int abortboot(int);
50 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
52 static char erase_seq[] = "\b \b"; /* erase sequence */
53 static char tab_seq[] = " "; /* used to expand TABs */
55 #ifdef CONFIG_BOOT_RETRY_TIME
56 static uint64_t endtime = 0; /* must be set, default is instant timeout */
57 static int retry_time = -1; /* -1 so can call readline before main_loop */
60 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
62 #ifndef CONFIG_BOOT_RETRY_MIN
63 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
66 #ifdef CONFIG_MODEM_SUPPORT
68 extern void mdm_init(void); /* defined in board.c */
71 /***************************************************************************
72 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
73 * returns: 0 - no key string, allow autoboot
74 * 1 - got key string, abort
76 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
77 # if defined(CONFIG_AUTOBOOT_KEYED)
78 static __inline__ int abortboot(int bootdelay)
81 uint64_t etime = endtick(bootdelay);
90 { str: getenv ("bootdelaykey"), retry: 1 },
91 { str: getenv ("bootdelaykey2"), retry: 1 },
92 { str: getenv ("bootstopkey"), retry: 0 },
93 { str: getenv ("bootstopkey2"), retry: 0 },
96 char presskey [MAX_DELAY_STOP_STR];
97 u_int presskey_len = 0;
98 u_int presskey_max = 0;
101 # ifdef CONFIG_AUTOBOOT_PROMPT
102 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
105 # ifdef CONFIG_AUTOBOOT_DELAY_STR
106 if (delaykey[0].str == NULL)
107 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
109 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
110 if (delaykey[1].str == NULL)
111 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
113 # ifdef CONFIG_AUTOBOOT_STOP_STR
114 if (delaykey[2].str == NULL)
115 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
117 # ifdef CONFIG_AUTOBOOT_STOP_STR2
118 if (delaykey[3].str == NULL)
119 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
122 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
123 delaykey[i].len = delaykey[i].str == NULL ?
124 0 : strlen (delaykey[i].str);
125 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
126 MAX_DELAY_STOP_STR : delaykey[i].len;
128 presskey_max = presskey_max > delaykey[i].len ?
129 presskey_max : delaykey[i].len;
132 printf("%s key:<%s>\n",
133 delaykey[i].retry ? "delay" : "stop",
134 delaykey[i].str ? delaykey[i].str : "NULL");
138 /* In order to keep up with incoming data, check timeout only
141 while (!abort && get_ticks() <= etime) {
142 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
143 if (delaykey[i].len > 0 &&
144 presskey_len >= delaykey[i].len &&
145 memcmp (presskey + presskey_len - delaykey[i].len,
147 delaykey[i].len) == 0) {
149 printf("got %skey\n",
150 delaykey[i].retry ? "delay" : "stop");
153 # ifdef CONFIG_BOOT_RETRY_TIME
154 /* don't retry auto boot */
155 if (! delaykey[i].retry)
163 if (presskey_len < presskey_max) {
164 presskey [presskey_len ++] = getc();
167 for (i = 0; i < presskey_max - 1; i ++)
168 presskey [i] = presskey [i + 1];
170 presskey [i] = getc();
176 printf("key timeout\n");
182 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
184 #ifdef CONFIG_MENUKEY
185 static int menukey = 0;
188 static __inline__ int abortboot(int bootdelay)
192 #ifdef CONFIG_MENUPROMPT
193 printf(CONFIG_MENUPROMPT, bootdelay);
195 printf("Hit any key to stop autoboot: %2d ", bootdelay);
198 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
200 * Check if key already pressed
201 * Don't check if bootdelay < 0
203 if (bootdelay >= 0) {
204 if (tstc()) { /* we got a key press */
205 (void) getc(); /* consume input */
206 printf ("\b\b\b 0\n");
207 return 1; /* don't auto boot */
212 while (bootdelay > 0) {
216 /* delay 100 * 10ms */
217 for (i=0; !abort && i<100; ++i) {
218 if (tstc()) { /* we got a key press */
219 abort = 1; /* don't auto boot */
220 bootdelay = 0; /* no more delay */
221 # ifdef CONFIG_MENUKEY
224 (void) getc(); /* consume input */
231 printf ("\b\b\b%2d ", bootdelay);
238 # endif /* CONFIG_AUTOBOOT_KEYED */
239 #endif /* CONFIG_BOOTDELAY >= 0 */
241 /****************************************************************************/
243 void main_loop (void)
245 #ifndef CFG_HUSH_PARSER
246 static char lastcommand[CFG_CBSIZE] = { 0, };
252 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
256 #ifdef CONFIG_PREBOOT
260 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
261 ulong bmp = 0; /* default bitmap */
262 extern int trab_vfd (ulong bitmap);
264 #ifdef CONFIG_MODEM_SUPPORT
266 bmp = 1; /* alternate bitmap */
269 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
271 #ifdef CONFIG_MODEM_SUPPORT
272 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
274 uchar *str = strdup(getenv("mdm_cmd"));
275 setenv ("preboot", str); /* set or delete definition */
278 mdm_init(); /* wait for modem connection */
280 #endif /* CONFIG_MODEM_SUPPORT */
282 #ifdef CONFIG_VERSION_VARIABLE
284 extern char version_string[];
285 char *str = getenv("ver");
288 setenv ("ver", version_string); /* set version variable */
290 #endif /* CONFIG_VERSION_VARIABLE */
292 #ifdef CFG_HUSH_PARSER
293 u_boot_hush_start ();
296 #ifdef CONFIG_PREBOOT
297 if ((p = getenv ("preboot")) != NULL) {
298 # ifdef CONFIG_AUTOBOOT_KEYED
299 int prev = disable_ctrlc(1); /* disable Control C checking */
302 # ifndef CFG_HUSH_PARSER
305 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
306 FLAG_EXIT_FROM_LOOP);
309 # ifdef CONFIG_AUTOBOOT_KEYED
310 disable_ctrlc(prev); /* restore Control C checking */
313 #endif /* CONFIG_PREBOOT */
315 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
316 s = getenv ("bootdelay");
317 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
319 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
321 # ifdef CONFIG_BOOT_RETRY_TIME
323 # endif /* CONFIG_BOOT_RETRY_TIME */
325 s = getenv ("bootcmd");
327 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
329 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
330 # ifdef CONFIG_AUTOBOOT_KEYED
331 int prev = disable_ctrlc(1); /* disable Control C checking */
334 # ifndef CFG_HUSH_PARSER
337 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
338 FLAG_EXIT_FROM_LOOP);
341 # ifdef CONFIG_AUTOBOOT_KEYED
342 disable_ctrlc(prev); /* restore Control C checking */
346 # ifdef CONFIG_MENUKEY
347 if (menukey == CONFIG_MENUKEY) {
348 s = getenv("menucmd");
350 # ifndef CFG_HUSH_PARSER
351 run_command (s, bd, 0);
353 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
354 FLAG_EXIT_FROM_LOOP);
358 #endif /* CONFIG_MENUKEY */
359 #endif /* CONFIG_BOOTDELAY */
361 #ifdef CONFIG_AMIGAONEG3SE
363 extern void video_banner(void);
369 * Main Loop for Monitor Command Processing
371 #ifdef CFG_HUSH_PARSER
373 /* This point is never reached */
377 #ifdef CONFIG_BOOT_RETRY_TIME
379 /* Saw enough of a valid command to
380 * restart the timeout.
385 len = readline (CFG_PROMPT);
387 flag = 0; /* assume no special flags for now */
389 strcpy (lastcommand, console_buffer);
391 flag |= CMD_FLAG_REPEAT;
392 #ifdef CONFIG_BOOT_RETRY_TIME
393 else if (len == -2) {
394 /* -2 means timed out, retry autoboot
396 printf("\nTimed out waiting for command\n");
397 # ifdef CONFIG_RESET_TO_RETRY
398 /* Reinit board to run initialization code again */
399 do_reset (NULL, 0, 0, NULL);
401 return; /* retry autoboot */
407 printf ("<INTERRUPT>\n");
409 rc = run_command (lastcommand, flag);
412 /* invalid command or not repeatable, forget it */
416 #endif /*CFG_HUSH_PARSER*/
419 #ifdef CONFIG_BOOT_RETRY_TIME
420 /***************************************************************************
421 * initialise command line timeout
423 void init_cmd_timeout(void)
425 char *s = getenv ("bootretry");
428 retry_time = (int)simple_strtoul(s, NULL, 10);
430 retry_time = CONFIG_BOOT_RETRY_TIME;
432 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
433 retry_time = CONFIG_BOOT_RETRY_MIN;
436 /***************************************************************************
437 * reset command line timeout to retry_time seconds
439 void reset_cmd_timeout(void)
441 endtime = endtick(retry_time);
445 /****************************************************************************/
448 * Prompt for input and read a line.
449 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
450 * time out when time goes past endtime (timebase time in ticks).
451 * Return: number of read characters
455 int readline (const char *const prompt)
457 char *p = console_buffer;
458 int n = 0; /* buffer index */
459 int plen = 0; /* prompt length */
460 int col; /* output column cnt */
465 plen = strlen (prompt);
471 #ifdef CONFIG_BOOT_RETRY_TIME
472 while (!tstc()) { /* while no incoming data */
473 if (retry_time >= 0 && get_ticks() > endtime)
474 return (-2); /* timed out */
477 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
479 #ifdef CONFIG_SHOW_ACTIVITY
481 extern void show_activity(int arg);
488 * Special character handling
491 case '\r': /* Enter */
495 return (p - console_buffer);
497 case 0x03: /* ^C - break */
498 console_buffer[0] = '\0'; /* discard input */
501 case 0x15: /* ^U - erase line */
510 case 0x17: /* ^W - erase word */
511 p=delete_char(console_buffer, p, &col, &n, plen);
512 while ((n > 0) && (*p != ' ')) {
513 p=delete_char(console_buffer, p, &col, &n, plen);
517 case 0x08: /* ^H - backspace */
518 case 0x7F: /* DEL - backspace */
519 p=delete_char(console_buffer, p, &col, &n, plen);
524 * Must be a normal character then
526 if (n < CFG_CBSIZE-2) {
527 if (c == '\t') { /* expand TABs */
528 puts (tab_seq+(col&07));
531 ++col; /* echo input */
536 } else { /* Buffer full */
543 /****************************************************************************/
545 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
553 if (*(--p) == '\t') { /* will retype the whole line */
554 while (*colp > plen) {
558 for (s=buffer; s<p; ++s) {
560 puts (tab_seq+((*colp) & 07));
561 *colp += 8 - ((*colp) & 07);
575 /****************************************************************************/
577 int parse_line (char *line, char *argv[])
582 printf ("parse_line: \"%s\"\n", line);
584 while (nargs < CFG_MAXARGS) {
586 /* skip any white space */
587 while ((*line == ' ') || (*line == '\t')) {
591 if (*line == '\0') { /* end of line, no more args */
594 printf ("parse_line: nargs=%d\n", nargs);
599 argv[nargs++] = line; /* begin of argument string */
601 /* find end of string */
602 while (*line && (*line != ' ') && (*line != '\t')) {
606 if (*line == '\0') { /* end of line, no more args */
609 printf ("parse_line: nargs=%d\n", nargs);
614 *line++ = '\0'; /* terminate current arg */
617 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
620 printf ("parse_line: nargs=%d\n", nargs);
625 /****************************************************************************/
627 static void process_macros (const char *input, char *output)
630 const char *varname_start = NULL;
631 int inputcnt = strlen (input);
632 int outputcnt = CFG_CBSIZE;
633 int state = 0; /* 0 = waiting for '$' */
634 /* 1 = waiting for '(' */
635 /* 2 = waiting for ')' */
636 /* 3 = waiting for ''' */
638 char *output_start = output;
640 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
643 prev = '\0'; /* previous character */
645 while (inputcnt && outputcnt) {
650 /* remove one level of escape characters */
651 if ((c == '\\') && (prev != '\\')) {
660 case 0: /* Waiting for (unescaped) $ */
661 if ((c == '\'') && (prev != '\\')) {
665 if ((c == '$') && (prev != '\\')) {
672 case 1: /* Waiting for ( */
675 varname_start = input;
687 case 2: /* Waiting for ) */
690 char envname[CFG_CBSIZE], *envval;
691 int envcnt = input-varname_start-1; /* Varname # of chars */
693 /* Get the varname */
694 for (i = 0; i < envcnt; i++) {
695 envname[i] = varname_start[i];
700 envval = getenv (envname);
702 /* Copy into the line if it exists */
704 while ((*envval) && outputcnt) {
705 *(output++) = *(envval++);
708 /* Look for another '$' */
712 case 3: /* Waiting for ' */
713 if ((c == '\'') && (prev != '\\')) {
728 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
729 strlen(output_start), output_start);
733 /****************************************************************************
735 * 1 - command executed, repeatable
736 * 0 - command executed but not repeatable, interrupted commands are
737 * always considered not repeatable
738 * -1 - not executed (unrecognized, bootd recursion or too many args)
739 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
740 * considered unrecognized)
744 * We must create a temporary copy of the command since the command we get
745 * may be the result from getenv(), which returns a pointer directly to
746 * the environment data, which may change magicly when the command we run
747 * creates or modifies environment variables (like "bootp" does).
750 int run_command (const char *cmd, int flag)
753 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
754 char *token; /* start of token in cmdbuf */
755 char *sep; /* end of token (separator) in cmdbuf */
756 char finaltoken[CFG_CBSIZE];
758 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
764 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
765 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
769 clear_ctrlc(); /* forget any previous Control C */
772 return -1; /* empty command */
775 if (strlen(cmd) >= CFG_CBSIZE) {
776 puts ("## Command too long!\n");
780 strcpy (cmdbuf, cmd);
782 /* Process separators and check for invalid
783 * repeatable commands
787 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
792 * Find separator, or string end
793 * Allow simple escape of ';' by writing "\;"
795 for (inquotes = 0, sep = str; *sep; sep++) {
801 (*sep == ';') && /* separator */
802 ( sep != str) && /* past string start */
803 (*(sep-1) != '\\')) /* and NOT escaped */
808 * Limit the token to data between separators
812 str = sep + 1; /* start of command for next pass */
816 str = sep; /* no more commands for next pass */
818 printf ("token: \"%s\"\n", token);
821 /* find macros in this token and replace them */
822 process_macros (token, finaltoken);
824 /* Extract arguments */
825 argc = parse_line (finaltoken, argv);
827 /* Look up command in command table */
828 if ((cmdtp = find_cmd(argv[0])) == NULL) {
829 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
830 rc = -1; /* give up after bad command */
834 /* found - check max args */
835 if (argc > cmdtp->maxargs) {
836 printf ("Usage:\n%s\n", cmdtp->usage);
841 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
842 /* avoid "bootd" recursion */
843 if (cmdtp->cmd == do_bootd) {
845 printf ("[%s]\n", finaltoken);
847 if (flag & CMD_FLAG_BOOTD) {
848 printf ("'bootd' recursion detected\n");
853 flag |= CMD_FLAG_BOOTD;
855 #endif /* CFG_CMD_BOOTD */
857 /* OK - call function to do the command */
858 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
862 repeatable &= cmdtp->repeatable;
864 /* Did the user stop this? */
866 return 0; /* if stopped then not repeatable */
869 return rc ? rc : repeatable;
872 /****************************************************************************/
874 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
875 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
880 printf ("Usage:\n%s\n", cmdtp->usage);
884 for (i=1; i<argc; ++i) {
887 if ((arg = getenv (argv[i])) == NULL) {
888 printf ("## Error: \"%s\" not defined\n", argv[i]);
891 #ifndef CFG_HUSH_PARSER
892 if (run_command (arg, flag) == -1)
895 if (parse_string_outer(arg,
896 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
902 #endif /* CFG_CMD_RUN */