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 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
40 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
43 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
46 #define MAX_DELAY_STOP_STR 32
48 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
49 static int parse_line (char *, char *[]);
50 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
51 static int abortboot(int);
56 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
58 static char erase_seq[] = "\b \b"; /* erase sequence */
59 static char tab_seq[] = " "; /* used to expand TABs */
61 #ifdef CONFIG_BOOT_RETRY_TIME
62 static uint64_t endtime = 0; /* must be set, default is instant timeout */
63 static int retry_time = -1; /* -1 so can call readline before main_loop */
66 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
68 #ifndef CONFIG_BOOT_RETRY_MIN
69 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
72 #ifdef CONFIG_MODEM_SUPPORT
74 extern void mdm_init(void); /* defined in board.c */
77 /***************************************************************************
78 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
79 * returns: 0 - no key string, allow autoboot
80 * 1 - got key string, abort
82 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
83 # if defined(CONFIG_AUTOBOOT_KEYED)
84 static __inline__ int abortboot(int bootdelay)
87 uint64_t etime = endtick(bootdelay);
96 { str: getenv ("bootdelaykey"), retry: 1 },
97 { str: getenv ("bootdelaykey2"), retry: 1 },
98 { str: getenv ("bootstopkey"), retry: 0 },
99 { str: getenv ("bootstopkey2"), retry: 0 },
102 char presskey [MAX_DELAY_STOP_STR];
103 u_int presskey_len = 0;
104 u_int presskey_max = 0;
107 #ifdef CONFIG_SILENT_CONSOLE
109 DECLARE_GLOBAL_DATA_PTR;
111 if (gd->flags & GD_FLG_SILENT) {
112 /* Restore serial console */
113 console_assign (stdout, "serial");
114 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 DECLARE_GLOBAL_DATA_PTR;
202 /* permanently enable normal console output */
203 gd->flags &= ~(GD_FLG_SILENT);
204 } else if (gd->flags & GD_FLG_SILENT) {
205 /* Restore silent console */
206 console_assign (stdout, "nulldev");
207 console_assign (stderr, "nulldev");
215 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
217 #ifdef CONFIG_MENUKEY
218 static int menukey = 0;
221 static __inline__ int abortboot(int bootdelay)
225 #ifdef CONFIG_SILENT_CONSOLE
227 DECLARE_GLOBAL_DATA_PTR;
229 if (gd->flags & GD_FLG_SILENT) {
230 /* Restore serial console */
231 console_assign (stdout, "serial");
232 console_assign (stderr, "serial");
237 #ifdef CONFIG_MENUPROMPT
238 printf(CONFIG_MENUPROMPT, bootdelay);
240 printf("Hit any key to stop autoboot: %2d ", bootdelay);
243 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
245 * Check if key already pressed
246 * Don't check if bootdelay < 0
248 if (bootdelay >= 0) {
249 if (tstc()) { /* we got a key press */
250 (void) getc(); /* consume input */
252 abort = 1; /* don't auto boot */
257 while ((bootdelay > 0) && (!abort)) {
261 /* delay 100 * 10ms */
262 for (i=0; !abort && i<100; ++i) {
263 if (tstc()) { /* we got a key press */
264 abort = 1; /* don't auto boot */
265 bootdelay = 0; /* no more delay */
266 # ifdef CONFIG_MENUKEY
269 (void) getc(); /* consume input */
276 printf ("\b\b\b%2d ", bootdelay);
281 #ifdef CONFIG_SILENT_CONSOLE
283 DECLARE_GLOBAL_DATA_PTR;
286 /* permanently enable normal console output */
287 gd->flags &= ~(GD_FLG_SILENT);
288 } else if (gd->flags & GD_FLG_SILENT) {
289 /* Restore silent console */
290 console_assign (stdout, "nulldev");
291 console_assign (stderr, "nulldev");
298 # endif /* CONFIG_AUTOBOOT_KEYED */
299 #endif /* CONFIG_BOOTDELAY >= 0 */
301 /****************************************************************************/
303 void main_loop (void)
305 #ifndef CFG_HUSH_PARSER
306 static char lastcommand[CFG_CBSIZE] = { 0, };
312 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
316 #ifdef CONFIG_PREBOOT
319 #ifdef CONFIG_BOOTCOUNT_LIMIT
320 unsigned long bootcount = 0;
321 unsigned long bootlimit = 0;
324 #endif /* CONFIG_BOOTCOUNT_LIMIT */
326 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
327 ulong bmp = 0; /* default bitmap */
328 extern int trab_vfd (ulong bitmap);
330 #ifdef CONFIG_MODEM_SUPPORT
332 bmp = 1; /* alternate bitmap */
335 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
337 #ifdef CONFIG_BOOTCOUNT_LIMIT
338 bootcount = bootcount_load();
340 bootcount_store (bootcount);
341 sprintf (bcs_set, "%lu", bootcount);
342 setenv ("bootcount", bcs_set);
343 bcs = getenv ("bootlimit");
344 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
345 #endif /* CONFIG_BOOTCOUNT_LIMIT */
347 #ifdef CONFIG_MODEM_SUPPORT
348 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
350 uchar *str = strdup(getenv("mdm_cmd"));
351 setenv ("preboot", str); /* set or delete definition */
354 mdm_init(); /* wait for modem connection */
356 #endif /* CONFIG_MODEM_SUPPORT */
358 #ifdef CONFIG_VERSION_VARIABLE
360 extern char version_string[];
362 setenv ("ver", version_string); /* set version variable */
364 #endif /* CONFIG_VERSION_VARIABLE */
366 #ifdef CFG_HUSH_PARSER
367 u_boot_hush_start ();
370 #ifdef CONFIG_AUTO_COMPLETE
371 install_auto_complete();
374 #ifdef CONFIG_PREBOOT
375 if ((p = getenv ("preboot")) != NULL) {
376 # ifdef CONFIG_AUTOBOOT_KEYED
377 int prev = disable_ctrlc(1); /* disable Control C checking */
380 # ifndef CFG_HUSH_PARSER
383 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
384 FLAG_EXIT_FROM_LOOP);
387 # ifdef CONFIG_AUTOBOOT_KEYED
388 disable_ctrlc(prev); /* restore Control C checking */
391 #endif /* CONFIG_PREBOOT */
393 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
394 s = getenv ("bootdelay");
395 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
397 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
399 # ifdef CONFIG_BOOT_RETRY_TIME
401 # endif /* CONFIG_BOOT_RETRY_TIME */
403 #ifdef CONFIG_BOOTCOUNT_LIMIT
404 if (bootlimit && (bootcount > bootlimit)) {
405 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
406 (unsigned)bootlimit);
407 s = getenv ("altbootcmd");
410 #endif /* CONFIG_BOOTCOUNT_LIMIT */
411 s = getenv ("bootcmd");
413 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
415 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
416 # ifdef CONFIG_AUTOBOOT_KEYED
417 int prev = disable_ctrlc(1); /* disable Control C checking */
420 # ifndef CFG_HUSH_PARSER
423 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
424 FLAG_EXIT_FROM_LOOP);
427 # ifdef CONFIG_AUTOBOOT_KEYED
428 disable_ctrlc(prev); /* restore Control C checking */
432 # ifdef CONFIG_MENUKEY
433 if (menukey == CONFIG_MENUKEY) {
434 s = getenv("menucmd");
436 # ifndef CFG_HUSH_PARSER
439 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
440 FLAG_EXIT_FROM_LOOP);
444 #endif /* CONFIG_MENUKEY */
445 #endif /* CONFIG_BOOTDELAY */
447 #ifdef CONFIG_AMIGAONEG3SE
449 extern void video_banner(void);
455 * Main Loop for Monitor Command Processing
457 #ifdef CFG_HUSH_PARSER
459 /* This point is never reached */
463 #ifdef CONFIG_BOOT_RETRY_TIME
465 /* Saw enough of a valid command to
466 * restart the timeout.
471 len = readline (CFG_PROMPT);
473 flag = 0; /* assume no special flags for now */
475 strcpy (lastcommand, console_buffer);
477 flag |= CMD_FLAG_REPEAT;
478 #ifdef CONFIG_BOOT_RETRY_TIME
479 else if (len == -2) {
480 /* -2 means timed out, retry autoboot
482 puts ("\nTimed out waiting for command\n");
483 # ifdef CONFIG_RESET_TO_RETRY
484 /* Reinit board to run initialization code again */
485 do_reset (NULL, 0, 0, NULL);
487 return; /* retry autoboot */
493 puts ("<INTERRUPT>\n");
495 rc = run_command (lastcommand, flag);
498 /* invalid command or not repeatable, forget it */
502 #endif /*CFG_HUSH_PARSER*/
505 #ifdef CONFIG_BOOT_RETRY_TIME
506 /***************************************************************************
507 * initialise command line timeout
509 void init_cmd_timeout(void)
511 char *s = getenv ("bootretry");
514 retry_time = (int)simple_strtol(s, NULL, 10);
516 retry_time = CONFIG_BOOT_RETRY_TIME;
518 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
519 retry_time = CONFIG_BOOT_RETRY_MIN;
522 /***************************************************************************
523 * reset command line timeout to retry_time seconds
525 void reset_cmd_timeout(void)
527 endtime = endtick(retry_time);
531 /****************************************************************************/
534 * Prompt for input and read a line.
535 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
536 * time out when time goes past endtime (timebase time in ticks).
537 * Return: number of read characters
541 int readline (const char *const prompt)
543 char *p = console_buffer;
544 int n = 0; /* buffer index */
545 int plen = 0; /* prompt length */
546 int col; /* output column cnt */
551 plen = strlen (prompt);
557 #ifdef CONFIG_BOOT_RETRY_TIME
558 while (!tstc()) { /* while no incoming data */
559 if (retry_time >= 0 && get_ticks() > endtime)
560 return (-2); /* timed out */
563 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
565 #ifdef CONFIG_SHOW_ACTIVITY
567 extern void show_activity(int arg);
574 * Special character handling
577 case '\r': /* Enter */
581 return (p - console_buffer);
586 case 0x03: /* ^C - break */
587 console_buffer[0] = '\0'; /* discard input */
590 case 0x15: /* ^U - erase line */
599 case 0x17: /* ^W - erase word */
600 p=delete_char(console_buffer, p, &col, &n, plen);
601 while ((n > 0) && (*p != ' ')) {
602 p=delete_char(console_buffer, p, &col, &n, plen);
606 case 0x08: /* ^H - backspace */
607 case 0x7F: /* DEL - backspace */
608 p=delete_char(console_buffer, p, &col, &n, plen);
613 * Must be a normal character then
615 if (n < CFG_CBSIZE-2) {
616 if (c == '\t') { /* expand TABs */
617 #ifdef CONFIG_AUTO_COMPLETE
618 /* if auto completion triggered just continue */
620 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
621 p = console_buffer + n; /* reset */
625 puts (tab_seq+(col&07));
628 ++col; /* echo input */
633 } else { /* Buffer full */
640 /****************************************************************************/
642 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
650 if (*(--p) == '\t') { /* will retype the whole line */
651 while (*colp > plen) {
655 for (s=buffer; s<p; ++s) {
657 puts (tab_seq+((*colp) & 07));
658 *colp += 8 - ((*colp) & 07);
672 /****************************************************************************/
674 int parse_line (char *line, char *argv[])
679 printf ("parse_line: \"%s\"\n", line);
681 while (nargs < CFG_MAXARGS) {
683 /* skip any white space */
684 while ((*line == ' ') || (*line == '\t')) {
688 if (*line == '\0') { /* end of line, no more args */
691 printf ("parse_line: nargs=%d\n", nargs);
696 argv[nargs++] = line; /* begin of argument string */
698 /* find end of string */
699 while (*line && (*line != ' ') && (*line != '\t')) {
703 if (*line == '\0') { /* end of line, no more args */
706 printf ("parse_line: nargs=%d\n", nargs);
711 *line++ = '\0'; /* terminate current arg */
714 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
717 printf ("parse_line: nargs=%d\n", nargs);
722 /****************************************************************************/
724 static void process_macros (const char *input, char *output)
727 const char *varname_start = NULL;
728 int inputcnt = strlen (input);
729 int outputcnt = CFG_CBSIZE;
730 int state = 0; /* 0 = waiting for '$' */
731 /* 1 = waiting for '(' or '{' */
732 /* 2 = waiting for ')' or '}' */
733 /* 3 = waiting for ''' */
735 char *output_start = output;
737 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
740 prev = '\0'; /* previous character */
742 while (inputcnt && outputcnt) {
747 /* remove one level of escape characters */
748 if ((c == '\\') && (prev != '\\')) {
757 case 0: /* Waiting for (unescaped) $ */
758 if ((c == '\'') && (prev != '\\')) {
762 if ((c == '$') && (prev != '\\')) {
769 case 1: /* Waiting for ( */
770 if (c == '(' || c == '{') {
772 varname_start = input;
784 case 2: /* Waiting for ) */
785 if (c == ')' || c == '}') {
787 char envname[CFG_CBSIZE], *envval;
788 int envcnt = input-varname_start-1; /* Varname # of chars */
790 /* Get the varname */
791 for (i = 0; i < envcnt; i++) {
792 envname[i] = varname_start[i];
797 envval = getenv (envname);
799 /* Copy into the line if it exists */
801 while ((*envval) && outputcnt) {
802 *(output++) = *(envval++);
805 /* Look for another '$' */
809 case 3: /* Waiting for ' */
810 if ((c == '\'') && (prev != '\\')) {
825 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
826 strlen(output_start), output_start);
830 /****************************************************************************
832 * 1 - command executed, repeatable
833 * 0 - command executed but not repeatable, interrupted commands are
834 * always considered not repeatable
835 * -1 - not executed (unrecognized, bootd recursion or too many args)
836 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
837 * considered unrecognized)
841 * We must create a temporary copy of the command since the command we get
842 * may be the result from getenv(), which returns a pointer directly to
843 * the environment data, which may change magicly when the command we run
844 * creates or modifies environment variables (like "bootp" does).
847 int run_command (const char *cmd, int flag)
850 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
851 char *token; /* start of token in cmdbuf */
852 char *sep; /* end of token (separator) in cmdbuf */
853 char finaltoken[CFG_CBSIZE];
855 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
861 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
862 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
866 clear_ctrlc(); /* forget any previous Control C */
869 return -1; /* empty command */
872 if (strlen(cmd) >= CFG_CBSIZE) {
873 puts ("## Command too long!\n");
877 strcpy (cmdbuf, cmd);
879 /* Process separators and check for invalid
880 * repeatable commands
884 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
889 * Find separator, or string end
890 * Allow simple escape of ';' by writing "\;"
892 for (inquotes = 0, sep = str; *sep; sep++) {
898 (*sep == ';') && /* separator */
899 ( sep != str) && /* past string start */
900 (*(sep-1) != '\\')) /* and NOT escaped */
905 * Limit the token to data between separators
909 str = sep + 1; /* start of command for next pass */
913 str = sep; /* no more commands for next pass */
915 printf ("token: \"%s\"\n", token);
918 /* find macros in this token and replace them */
919 process_macros (token, finaltoken);
921 /* Extract arguments */
922 argc = parse_line (finaltoken, argv);
924 /* Look up command in command table */
925 if ((cmdtp = find_cmd(argv[0])) == NULL) {
926 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
927 rc = -1; /* give up after bad command */
931 /* found - check max args */
932 if (argc > cmdtp->maxargs) {
933 printf ("Usage:\n%s\n", cmdtp->usage);
938 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
939 /* avoid "bootd" recursion */
940 if (cmdtp->cmd == do_bootd) {
942 printf ("[%s]\n", finaltoken);
944 if (flag & CMD_FLAG_BOOTD) {
945 puts ("'bootd' recursion detected\n");
950 flag |= CMD_FLAG_BOOTD;
952 #endif /* CFG_CMD_BOOTD */
954 /* OK - call function to do the command */
955 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
959 repeatable &= cmdtp->repeatable;
961 /* Did the user stop this? */
963 return 0; /* if stopped then not repeatable */
966 return rc ? rc : repeatable;
969 /****************************************************************************/
971 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
972 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
977 printf ("Usage:\n%s\n", cmdtp->usage);
981 for (i=1; i<argc; ++i) {
984 if ((arg = getenv (argv[i])) == NULL) {
985 printf ("## Error: \"%s\" not defined\n", argv[i]);
988 #ifndef CFG_HUSH_PARSER
989 if (run_command (arg, flag) == -1)
992 if (parse_string_outer(arg,
993 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
999 #endif /* CFG_CMD_RUN */