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 CFG_HUSH_PARSER
33 #ifdef CONFIG_SILENT_CONSOLE
34 DECLARE_GLOBAL_DATA_PTR;
37 extern int reset_button_status(void);
38 extern void all_led_on(void);
39 extern void all_led_off(void);
40 extern int NetLoopHttpd(void);
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);
50 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
51 static char erase_seq[] = "\b \b"; /* erase sequence */
52 static char tab_seq[] = " "; /* used to expand TABs */
54 /***************************************************************************
55 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
56 * returns: 0 - no key string, allow autoboot
57 * 1 - got key string, abort
59 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
60 static __inline__ int abortboot(int bootdelay){
64 #ifdef CONFIG_SILENT_CONSOLE
65 if(gd->flags & GD_FLG_SILENT){
66 /* Restore serial console */
67 console_assign(stdout, "serial");
68 console_assign(stderr, "serial");
73 #ifdef CONFIG_MENUPROMPT
74 printf(CONFIG_MENUPROMPT, bootdelay);
76 printf("Hit any key to stop autoboot: %d ", bootdelay);
79 while((bootdelay > 0) && (!abort)){
84 /* delay 100 * 10ms */
85 for(i = 0; !abort && i < 100; ++i){
86 /* we got a key press */
89 #ifdef CONFIG_AUTOBOOT_STOP_CHAR
90 if (stopc == CONFIG_AUTOBOOT_STOP_CHAR) {
103 printf("\b\b%d ", bootdelay);
109 #ifdef CONFIG_SILENT_CONSOLE
111 /* permanently enable normal console output */
112 gd->flags &= ~(GD_FLG_SILENT);
113 } else if(gd->flags & GD_FLG_SILENT){
114 /* Restore silent console */
115 console_assign(stdout, "nulldev");
116 console_assign(stderr, "nulldev");
122 #endif /* CONFIG_BOOTDELAY >= 0 */
124 /****************************************************************************/
126 void main_loop(void){
127 #ifndef CFG_HUSH_PARSER
128 static char lastcommand[CFG_CBSIZE] = { 0, };
135 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
138 #endif /* defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) */
140 #ifdef CFG_HUSH_PARSER
144 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
145 // get boot delay (seconds)
146 s = getenv("bootdelay");
147 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
150 s = getenv("bootcmd");
152 #if !defined(CONFIG_BOOTCOMMAND)
153 #error "CONFIG_BOOTCOMMAND not defined!"
157 setenv("bootcmd", CONFIG_BOOTCOMMAND);
160 s = getenv("bootcmd");
162 // are we going to run web failsafe mode, U-Boot console, U-Boot netconsole or just boot command?
163 if(reset_button_status()){
165 #ifdef CONFIG_SILENT_CONSOLE
166 if(gd->flags & GD_FLG_SILENT){
167 /* Restore serial console */
168 console_assign(stdout, "serial");
169 console_assign(stderr, "serial");
172 /* enable normal console output */
173 gd->flags &= ~(GD_FLG_SILENT);
179 printf("Press reset button for at least:\n- %d sec. to run web failsafe mode\n- %d sec. to run U-Boot console\n- %d sec. to run U-Boot netconsole\n\n",
180 CONFIG_DELAY_TO_AUTORUN_HTTPD,
181 CONFIG_DELAY_TO_AUTORUN_CONSOLE,
182 CONFIG_DELAY_TO_AUTORUN_NETCONSOLE);
184 printf("Reset button is pressed for: %2d ", counter);
186 while(reset_button_status()){
188 // LED ON and wait 0,15s
192 // LED OFF and wait 0,85s
198 // how long the button is pressed?
199 printf("\b\b\b%2d ", counter);
201 if(!reset_button_status()){
205 if(counter >= CONFIG_MAX_BUTTON_PRESSING){
214 // run web failsafe mode
215 if(counter >= CONFIG_DELAY_TO_AUTORUN_HTTPD && counter < CONFIG_DELAY_TO_AUTORUN_CONSOLE){
216 printf("\n\nButton was pressed for %d sec...\nHTTP server is starting for firmware update...\n\n", counter);
219 } else if(counter >= CONFIG_DELAY_TO_AUTORUN_CONSOLE && counter < CONFIG_DELAY_TO_AUTORUN_NETCONSOLE){
220 printf("\n\nButton was pressed for %d sec...\nStarting U-Boot console...\n\n", counter);
222 } else if(counter >= CONFIG_DELAY_TO_AUTORUN_NETCONSOLE){
223 printf("\n\nButton was pressed for %d sec...\nStarting U-Boot netconsole...\n\n", counter);
225 run_command("startnc", 0);
227 printf("\n\n## Error: button wasn't pressed long enough!\nContinuing normal boot...\n\n");
231 printf("\n\n## Error: button wasn't pressed long enough!\nContinuing normal boot...\n\n");
236 if(bootdelay >= 0 && s && !abortboot(bootdelay)){
239 #ifndef CFG_HUSH_PARSER
242 parse_string_outer(s, FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
245 // something goes wrong!
246 printf("\n## Error: failed to execute 'bootcmd'!\nHTTP server is starting for firmware update...\n\n");
249 #endif /* CONFIG_BOOTDELAY */
252 * Main Loop for Monitor Command Processing
254 #ifdef CFG_HUSH_PARSER
256 /* This point is never reached */
260 len = readline(CFG_PROMPT);
262 flag = 0; /* assume no special flags for now */
264 strcpy(lastcommand, console_buffer);
266 flag |= CMD_FLAG_REPEAT;
270 puts("<INTERRUPT>\n");
272 rc = run_command(lastcommand, flag);
276 /* invalid command or not repeatable, forget it */
280 #endif /* CFG_HUSH_PARSER */
283 /****************************************************************************/
286 * Prompt for input and read a line.
287 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
288 * time out when time goes past endtime (timebase time in ticks).
289 * Return: number of read characters
293 int readline(const char * const prompt){
294 char *p = console_buffer;
295 int n = 0; /* buffer index */
296 int plen = 0; /* prompt length */
297 int col; /* output column cnt */
302 plen = strlen(prompt);
311 * Special character handling
314 case '\r': /* Enter */
318 return(p - console_buffer);
323 case 0x03: /* ^C - break */
324 console_buffer[0] = '\0'; /* discard input */
327 case 0x15: /* ^U - erase line */
336 case 0x17: /* ^W - erase word */
337 p = delete_char(console_buffer, p, &col, &n, plen);
338 while((n > 0) && (*p != ' ')){
339 p = delete_char(console_buffer, p, &col, &n, plen);
343 case 0x08: /* ^H - backspace */
344 case 0x7F: /* DEL - backspace */
345 p = delete_char(console_buffer, p, &col, &n, plen);
350 * Must be a normal character then
352 if(n < CFG_CBSIZE - 2){
353 if(c == '\t'){ /* expand TABs */
354 puts(tab_seq + (col & 07));
355 col += 8 - (col & 07);
357 ++col; /* echo input */
362 } else { /* Buffer full */
369 /****************************************************************************/
371 static char * delete_char(char *buffer, char *p, int *colp, int *np, int plen){
378 if(*(--p) == '\t'){ /* will retype the whole line */
383 for(s = buffer; s < p; ++s){
385 puts(tab_seq + ((*colp) & 07));
386 *colp += 8 - ((*colp) & 07);
400 /****************************************************************************/
402 int parse_line(char *line, char *argv[]){
405 while(nargs < CFG_MAXARGS){
407 /* skip any white space */
408 while((*line == ' ') || (*line == '\t')){
412 if(*line == '\0'){ /* end of line, no more args */
417 argv[nargs++] = line; /* begin of argument string */
419 /* find end of string */
420 while(*line && (*line != ' ') && (*line != '\t')){
424 if(*line == '\0'){ /* end of line, no more args */
429 *line++ = '\0'; /* terminate current arg */
432 printf("## Error: too many args (max. %d)\n", CFG_MAXARGS);
437 /****************************************************************************/
439 static void process_macros(const char *input, char *output){
441 const char *varname_start = NULL;
442 int inputcnt = strlen(input);
443 int outputcnt = CFG_CBSIZE;
444 int state = 0; /* 0 = waiting for '$' */
445 /* 1 = waiting for '(' or '{' */
446 /* 2 = waiting for ')' or '}' */
447 /* 3 = waiting for ''' */
449 prev = '\0'; /* previous character */
451 while(inputcnt && outputcnt){
456 /* remove one level of escape characters */
457 if((c == '\\') && (prev != '\\')){
468 case 0: /* Waiting for (unescaped) $ */
469 if((c == '\'') && (prev != '\\')){
473 if((c == '$') && (prev != '\\')){
480 case 1: /* Waiting for ( */
481 if(c == '(' || c == '{'){
483 varname_start = input;
495 case 2: /* Waiting for ) */
496 if(c == ')' || c == '}'){
498 char envname[CFG_CBSIZE], *envval;
499 int envcnt = input - varname_start - 1; /* Varname # of chars */
501 /* Get the varname */
502 for(i = 0; i < envcnt; i++){
503 envname[i] = varname_start[i];
508 envval = getenv(envname);
510 /* Copy into the line if it exists */
512 while((*envval) && outputcnt){
513 *(output++) = *(envval++);
517 /* Look for another '$' */
521 case 3: /* Waiting for ' */
522 if((c == '\'') && (prev != '\\')){
538 /****************************************************************************
540 * 1 - command executed, repeatable
541 * 0 - command executed but not repeatable, interrupted commands are
542 * always considered not repeatable
543 * -1 - not executed (unrecognized, bootd recursion or too many args)
544 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
545 * considered unrecognized)
549 * We must create a temporary copy of the command since the command we get
550 * may be the result from getenv(), which returns a pointer directly to
551 * the environment data, which may change magicly when the command we run
552 * creates or modifies environment variables (like "bootp" does).
555 int run_command(const char *cmd, int flag){
557 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
558 char *token; /* start of token in cmdbuf */
559 char *sep; /* end of token (separator) in cmdbuf */
560 char finaltoken[CFG_CBSIZE];
562 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
567 clear_ctrlc(); /* forget any previous Control C */
570 return(-1); /* empty command */
573 if(strlen(cmd) >= CFG_CBSIZE){
574 puts("## Error: command too long!\n");
580 /* Process separators and check for invalid
581 * repeatable commands
586 * Find separator, or string end
587 * Allow simple escape of ';' by writing "\;"
589 for(inquotes = 0, sep = str; *sep; sep++){
590 if((*sep == '\'') && (*(sep - 1) != '\\')){
591 inquotes = !inquotes;
594 if(!inquotes && (*sep == ';') && (sep != str) && (*(sep - 1) != '\\')){
600 * Limit the token to data between separators
604 str = sep + 1; /* start of command for next pass */
607 str = sep; /* no more commands for next pass */
610 /* find macros in this token and replace them */
611 process_macros(token, finaltoken);
613 /* Extract arguments */
614 if((argc = parse_line(finaltoken, argv)) == 0){
615 rc = -1; /* no command at all */
619 /* Look up command in command table */
620 if((cmdtp = find_cmd(argv[0])) == NULL){
621 printf("## Error: unknown command '%s' - try 'help'\n\n", argv[0]);
622 rc = -1; /* give up after bad command */
626 /* found - check max args */
627 if(argc > cmdtp->maxargs){
628 print_cmd_help(cmdtp);
633 /* OK - call function to do the command */
634 if((cmdtp->cmd)(cmdtp, flag, argc, argv) != 0){
638 repeatable &= cmdtp->repeatable;
640 /* Did the user stop this? */
641 if(had_ctrlc()){ /* if stopped then not repeatable */
646 return(rc ? rc : repeatable);
649 /****************************************************************************/
651 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
652 int do_run(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]){
656 print_cmd_help(cmdtp);
660 for(i=1; i<argc; ++i){
663 if((arg = getenv(argv[i])) == NULL){
664 printf("## Error: \"%s\" not defined\n", argv[i]);
667 #ifndef CFG_HUSH_PARSER
668 if(run_command(arg, flag) == -1){
672 if (parse_string_outer(arg, FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0){
675 #endif /* CFG_HUSH_PARSER */
680 #endif /* CFG_CMD_RUN */