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){
61 #ifdef CONFIG_AUTOBOOT_STOP_CHAR
66 #ifdef CONFIG_SILENT_CONSOLE
67 if(gd->flags & GD_FLG_SILENT){
68 /* Restore serial console */
69 console_assign(stdout, "serial");
70 console_assign(stderr, "serial");
75 #ifdef CONFIG_MENUPROMPT
76 printf(CONFIG_MENUPROMPT, bootdelay);
78 printf("Hit any key to stop autoboot: %d ", bootdelay);
81 while((bootdelay > 0) && (!abort)){
86 /* delay 100 * 10ms */
87 for(i = 0; !abort && i < 100; ++i){
89 /* we got a key press */
91 #ifdef CONFIG_AUTOBOOT_STOP_CHAR
93 if (stopc == CONFIG_AUTOBOOT_STOP_CHAR) {
100 /* don't auto boot */
107 #endif /* CONFIG_AUTOBOOT_STOP_CHAR */
112 printf("\b\b%d ", bootdelay);
118 #ifdef CONFIG_SILENT_CONSOLE
120 /* permanently enable normal console output */
121 gd->flags &= ~(GD_FLG_SILENT);
122 } else if(gd->flags & GD_FLG_SILENT){
123 /* Restore silent console */
124 console_assign(stdout, "nulldev");
125 console_assign(stderr, "nulldev");
131 #endif /* CONFIG_BOOTDELAY >= 0 */
133 /****************************************************************************/
135 void main_loop(void){
136 #ifndef CFG_HUSH_PARSER
137 static char lastcommand[CFG_CBSIZE] = { 0, };
144 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
147 #endif /* defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) */
149 #ifdef CFG_HUSH_PARSER
153 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
154 // get boot delay (seconds)
155 s = getenv("bootdelay");
156 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
159 s = getenv("bootcmd");
161 #if !defined(CONFIG_BOOTCOMMAND)
162 #error "CONFIG_BOOTCOMMAND not defined!"
166 setenv("bootcmd", CONFIG_BOOTCOMMAND);
169 s = getenv("bootcmd");
171 // are we going to run web failsafe mode, U-Boot console, U-Boot netconsole or just boot command?
172 if(reset_button_status()){
174 #ifdef CONFIG_SILENT_CONSOLE
175 if(gd->flags & GD_FLG_SILENT){
176 /* Restore serial console */
177 console_assign(stdout, "serial");
178 console_assign(stderr, "serial");
181 /* enable normal console output */
182 gd->flags &= ~(GD_FLG_SILENT);
188 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",
189 CONFIG_DELAY_TO_AUTORUN_HTTPD,
190 CONFIG_DELAY_TO_AUTORUN_CONSOLE,
191 CONFIG_DELAY_TO_AUTORUN_NETCONSOLE);
193 printf("Reset button is pressed for: %2d ", counter);
195 while(reset_button_status()){
197 // LED ON and wait 0,15s
201 // LED OFF and wait 0,85s
207 // how long the button is pressed?
208 printf("\b\b\b%2d ", counter);
210 if(!reset_button_status()){
214 if(counter >= CONFIG_MAX_BUTTON_PRESSING){
223 // run web failsafe mode
224 if(counter >= CONFIG_DELAY_TO_AUTORUN_HTTPD && counter < CONFIG_DELAY_TO_AUTORUN_CONSOLE){
225 printf("\n\nButton was pressed for %d sec...\nHTTP server is starting for firmware update...\n\n", counter);
228 } else if(counter >= CONFIG_DELAY_TO_AUTORUN_CONSOLE && counter < CONFIG_DELAY_TO_AUTORUN_NETCONSOLE){
229 printf("\n\nButton was pressed for %d sec...\nStarting U-Boot console...\n\n", counter);
231 } else if(counter >= CONFIG_DELAY_TO_AUTORUN_NETCONSOLE){
232 printf("\n\nButton was pressed for %d sec...\nStarting U-Boot netconsole...\n\n", counter);
234 run_command("startnc", 0);
236 printf("\n\n## Error: button wasn't pressed long enough!\nContinuing normal boot...\n\n");
240 printf("\n\n## Error: button wasn't pressed long enough!\nContinuing normal boot...\n\n");
245 if(bootdelay >= 0 && s && !abortboot(bootdelay)){
248 #ifndef CFG_HUSH_PARSER
251 parse_string_outer(s, FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
254 // something goes wrong!
255 printf("\n## Error: failed to execute 'bootcmd'!\nHTTP server is starting for firmware update...\n\n");
258 #endif /* CONFIG_BOOTDELAY */
261 * Main Loop for Monitor Command Processing
263 #ifdef CFG_HUSH_PARSER
265 /* This point is never reached */
269 len = readline(CFG_PROMPT);
271 flag = 0; /* assume no special flags for now */
273 strcpy(lastcommand, console_buffer);
275 flag |= CMD_FLAG_REPEAT;
279 puts("<INTERRUPT>\n");
281 rc = run_command(lastcommand, flag);
285 /* invalid command or not repeatable, forget it */
289 #endif /* CFG_HUSH_PARSER */
292 /****************************************************************************/
295 * Prompt for input and read a line.
296 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
297 * time out when time goes past endtime (timebase time in ticks).
298 * Return: number of read characters
302 int readline(const char * const prompt){
303 char *p = console_buffer;
304 int n = 0; /* buffer index */
305 int plen = 0; /* prompt length */
306 int col; /* output column cnt */
311 plen = strlen(prompt);
320 * Special character handling
323 case '\r': /* Enter */
327 return(p - console_buffer);
332 case 0x03: /* ^C - break */
333 console_buffer[0] = '\0'; /* discard input */
336 case 0x15: /* ^U - erase line */
345 case 0x17: /* ^W - erase word */
346 p = delete_char(console_buffer, p, &col, &n, plen);
347 while((n > 0) && (*p != ' ')){
348 p = delete_char(console_buffer, p, &col, &n, plen);
352 case 0x08: /* ^H - backspace */
353 case 0x7F: /* DEL - backspace */
354 p = delete_char(console_buffer, p, &col, &n, plen);
359 * Must be a normal character then
361 if(n < CFG_CBSIZE - 2){
362 if(c == '\t'){ /* expand TABs */
363 puts(tab_seq + (col & 07));
364 col += 8 - (col & 07);
366 ++col; /* echo input */
371 } else { /* Buffer full */
378 /****************************************************************************/
380 static char * delete_char(char *buffer, char *p, int *colp, int *np, int plen){
387 if(*(--p) == '\t'){ /* will retype the whole line */
392 for(s = buffer; s < p; ++s){
394 puts(tab_seq + ((*colp) & 07));
395 *colp += 8 - ((*colp) & 07);
409 /****************************************************************************/
411 int parse_line(char *line, char *argv[]){
414 while(nargs < CFG_MAXARGS){
416 /* skip any white space */
417 while((*line == ' ') || (*line == '\t')){
421 if(*line == '\0'){ /* end of line, no more args */
426 argv[nargs++] = line; /* begin of argument string */
428 /* find end of string */
429 while(*line && (*line != ' ') && (*line != '\t')){
433 if(*line == '\0'){ /* end of line, no more args */
438 *line++ = '\0'; /* terminate current arg */
441 printf("## Error: too many args (max. %d)\n", CFG_MAXARGS);
446 /****************************************************************************/
448 static void process_macros(const char *input, char *output){
450 const char *varname_start = NULL;
451 int inputcnt = strlen(input);
452 int outputcnt = CFG_CBSIZE;
453 int state = 0; /* 0 = waiting for '$' */
454 /* 1 = waiting for '(' or '{' */
455 /* 2 = waiting for ')' or '}' */
456 /* 3 = waiting for ''' */
458 prev = '\0'; /* previous character */
460 while(inputcnt && outputcnt){
465 /* remove one level of escape characters */
466 if((c == '\\') && (prev != '\\')){
477 case 0: /* Waiting for (unescaped) $ */
478 if((c == '\'') && (prev != '\\')){
482 if((c == '$') && (prev != '\\')){
489 case 1: /* Waiting for ( */
490 if(c == '(' || c == '{'){
492 varname_start = input;
504 case 2: /* Waiting for ) */
505 if(c == ')' || c == '}'){
507 char envname[CFG_CBSIZE], *envval;
508 int envcnt = input - varname_start - 1; /* Varname # of chars */
510 /* Get the varname */
511 for(i = 0; i < envcnt; i++){
512 envname[i] = varname_start[i];
517 envval = getenv(envname);
519 /* Copy into the line if it exists */
521 while((*envval) && outputcnt){
522 *(output++) = *(envval++);
526 /* Look for another '$' */
530 case 3: /* Waiting for ' */
531 if((c == '\'') && (prev != '\\')){
547 /****************************************************************************
549 * 1 - command executed, repeatable
550 * 0 - command executed but not repeatable, interrupted commands are
551 * always considered not repeatable
552 * -1 - not executed (unrecognized, bootd recursion or too many args)
553 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
554 * considered unrecognized)
558 * We must create a temporary copy of the command since the command we get
559 * may be the result from getenv(), which returns a pointer directly to
560 * the environment data, which may change magicly when the command we run
561 * creates or modifies environment variables (like "bootp" does).
564 int run_command(const char *cmd, int flag){
566 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
567 char *token; /* start of token in cmdbuf */
568 char *sep; /* end of token (separator) in cmdbuf */
569 char finaltoken[CFG_CBSIZE];
571 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
576 clear_ctrlc(); /* forget any previous Control C */
579 return(-1); /* empty command */
582 if(strlen(cmd) >= CFG_CBSIZE){
583 puts("## Error: command too long!\n");
589 /* Process separators and check for invalid
590 * repeatable commands
595 * Find separator, or string end
596 * Allow simple escape of ';' by writing "\;"
598 for(inquotes = 0, sep = str; *sep; sep++){
599 if((*sep == '\'') && (*(sep - 1) != '\\')){
600 inquotes = !inquotes;
603 if(!inquotes && (*sep == ';') && (sep != str) && (*(sep - 1) != '\\')){
609 * Limit the token to data between separators
613 str = sep + 1; /* start of command for next pass */
616 str = sep; /* no more commands for next pass */
619 /* find macros in this token and replace them */
620 process_macros(token, finaltoken);
622 /* Extract arguments */
623 if((argc = parse_line(finaltoken, argv)) == 0){
624 rc = -1; /* no command at all */
628 /* Look up command in command table */
629 if((cmdtp = find_cmd(argv[0])) == NULL){
630 printf("## Error: unknown command '%s' - try 'help'\n\n", argv[0]);
631 rc = -1; /* give up after bad command */
635 /* found - check max args */
636 if(argc > cmdtp->maxargs){
638 if(cmdtp->help != NULL){
639 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
641 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
644 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
650 /* OK - call function to do the command */
651 if((cmdtp->cmd)(cmdtp, flag, argc, argv) != 0){
655 repeatable &= cmdtp->repeatable;
657 /* Did the user stop this? */
658 if(had_ctrlc()){ /* if stopped then not repeatable */
663 return(rc ? rc : repeatable);
666 /****************************************************************************/
668 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
669 int do_run(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]){
674 if(cmdtp->help != NULL){
675 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
677 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
680 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
685 for(i=1; i<argc; ++i){
688 if((arg = getenv(argv[i])) == NULL){
689 printf("## Error: \"%s\" not defined\n", argv[i]);
692 #ifndef CFG_HUSH_PARSER
693 if(run_command(arg, flag) == -1){
697 if (parse_string_outer(arg, FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0){
700 #endif /* CFG_HUSH_PARSER */
705 #endif /* CFG_CMD_RUN */