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_SILENT_CONSOLE
30 DECLARE_GLOBAL_DATA_PTR;
33 extern int reset_button_status(void);
34 extern void all_led_on(void);
35 extern void all_led_off(void);
36 extern int NetLoopHttpd(void);
38 #define MAX_DELAY_STOP_STR 32
40 static char *delete_char(char *buffer, char *p, int *colp, int *np, int plen);
41 static int parse_line(char *, char *[]);
42 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
43 static int abortboot(int);
46 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
47 static char erase_seq[] = "\b \b"; /* erase sequence */
48 static char tab_seq[] = " "; /* used to expand TABs */
50 /***************************************************************************
51 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
52 * returns: 0 - no key string, allow autoboot
53 * 1 - got key string, abort
55 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
56 static __inline__ int abortboot(int bootdelay){
59 #ifdef CONFIG_SILENT_CONSOLE
60 if(gd->flags & GD_FLG_SILENT){
61 /* Restore serial console */
62 console_assign(stdout, "serial");
63 console_assign(stderr, "serial");
67 #ifdef CONFIG_MENUPROMPT
68 printf(CONFIG_MENUPROMPT, bootdelay);
70 printf("Hit any key to stop autoboot: %d ", bootdelay);
73 while((bootdelay > 0) && (!abort)){
78 /* delay 100 * 10ms */
79 for(i = 0; !abort && i < 100; ++i){
81 /* we got a key press */
94 printf("\b\b%d ", bootdelay);
99 #ifdef CONFIG_SILENT_CONSOLE
101 /* permanently enable normal console output */
102 gd->flags &= ~(GD_FLG_SILENT);
103 } else if(gd->flags & GD_FLG_SILENT){
104 /* Restore silent console */
105 console_assign(stdout, "nulldev");
106 console_assign(stderr, "nulldev");
112 #endif /* CONFIG_BOOTDELAY >= 0 */
114 /****************************************************************************/
116 void main_loop(void){
117 static char lastcommand[CFG_CBSIZE] = { 0, };
123 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
127 // get boot delay (seconds)
128 s = getenv("bootdelay");
129 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
132 s = getenv("bootcmd");
134 #if !defined(CONFIG_BOOTCOMMAND)
135 #error "CONFIG_BOOTCOMMAND not defined!"
139 setenv("bootcmd", CONFIG_BOOTCOMMAND);
142 s = getenv("bootcmd");
144 // are we going to run web failsafe mode, U-Boot console, U-Boot netconsole or just boot command?
145 if(reset_button_status()){
150 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",
151 CONFIG_DELAY_TO_AUTORUN_HTTPD,
152 CONFIG_DELAY_TO_AUTORUN_CONSOLE,
153 CONFIG_DELAY_TO_AUTORUN_NETCONSOLE);
155 printf("Reset button is pressed for: %2d ", counter);
157 while(reset_button_status()){
159 // LED ON and wait 0,15s
163 // LED OFF and wait 0,85s
169 // how long the button is pressed?
170 printf("\b\b\b%2d ", counter);
172 if(!reset_button_status()){
176 if(counter >= CONFIG_MAX_BUTTON_PRESSING){
185 // run web failsafe mode
186 if(counter >= CONFIG_DELAY_TO_AUTORUN_HTTPD && counter < CONFIG_DELAY_TO_AUTORUN_CONSOLE){
187 printf("\n\nButton was pressed for %d sec...\nHTTP server is starting for firmware update...\n\n", counter);
190 } else if(counter >= CONFIG_DELAY_TO_AUTORUN_CONSOLE && counter < CONFIG_DELAY_TO_AUTORUN_NETCONSOLE){
191 printf("\n\nButton was pressed for %d sec...\nStarting U-Boot console...\n\n", counter);
193 } else if(counter >= CONFIG_DELAY_TO_AUTORUN_NETCONSOLE){
194 printf("\n\nButton was pressed for %d sec...\nStarting U-Boot netconsole...\n\n", counter);
196 run_command("startnc", 0);
198 printf("\n\n## Error: button wasn't pressed long enough!\nContinuing normal boot...\n\n");
202 printf("\n\n## Error: button wasn't pressed long enough!\nContinuing normal boot...\n\n");
207 if(bootdelay >= 0 && s && !abortboot(bootdelay)){
212 // something goes wrong!
213 printf("\n## Error: failed to execute 'bootcmd'!\nHTTP server is starting for firmware update...\n\n");
216 #endif /* CONFIG_BOOTDELAY */
219 * Main Loop for Monitor Command Processing
222 len = readline(CFG_PROMPT);
224 flag = 0; /* assume no special flags for now */
226 strcpy(lastcommand, console_buffer);
228 flag |= CMD_FLAG_REPEAT;
232 puts("<INTERRUPT>\n");
234 rc = run_command(lastcommand, flag);
238 /* invalid command or not repeatable, forget it */
244 /****************************************************************************/
247 * Prompt for input and read a line.
248 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
249 * time out when time goes past endtime (timebase time in ticks).
250 * Return: number of read characters
254 int readline(const char * const prompt){
255 char *p = console_buffer;
256 int n = 0; /* buffer index */
257 int plen = 0; /* prompt length */
258 int col; /* output column cnt */
263 plen = strlen(prompt);
272 * Special character handling
275 case '\r': /* Enter */
279 return(p - console_buffer);
284 case 0x03: /* ^C - break */
285 console_buffer[0] = '\0'; /* discard input */
288 case 0x15: /* ^U - erase line */
297 case 0x17: /* ^W - erase word */
298 p = delete_char(console_buffer, p, &col, &n, plen);
299 while((n > 0) && (*p != ' ')){
300 p = delete_char(console_buffer, p, &col, &n, plen);
304 case 0x08: /* ^H - backspace */
305 case 0x7F: /* DEL - backspace */
306 p = delete_char(console_buffer, p, &col, &n, plen);
311 * Must be a normal character then
313 if(n < CFG_CBSIZE - 2){
314 if(c == '\t'){ /* expand TABs */
315 puts(tab_seq + (col & 07));
316 col += 8 - (col & 07);
318 ++col; /* echo input */
323 } else { /* Buffer full */
330 /****************************************************************************/
332 static char * delete_char(char *buffer, char *p, int *colp, int *np, int plen){
339 if(*(--p) == '\t'){ /* will retype the whole line */
344 for(s = buffer; s < p; ++s){
346 puts(tab_seq + ((*colp) & 07));
347 *colp += 8 - ((*colp) & 07);
361 /****************************************************************************/
363 int parse_line(char *line, char *argv[]){
366 while(nargs < CFG_MAXARGS){
368 /* skip any white space */
369 while((*line == ' ') || (*line == '\t')){
373 if(*line == '\0'){ /* end of line, no more args */
378 argv[nargs++] = line; /* begin of argument string */
380 /* find end of string */
381 while(*line && (*line != ' ') && (*line != '\t')){
385 if(*line == '\0'){ /* end of line, no more args */
390 *line++ = '\0'; /* terminate current arg */
393 printf("## Error: too many args (max. %d)\n", CFG_MAXARGS);
398 /****************************************************************************/
400 static void process_macros(const char *input, char *output){
402 const char *varname_start = NULL;
403 int inputcnt = strlen(input);
404 int outputcnt = CFG_CBSIZE;
405 int state = 0; /* 0 = waiting for '$' */
406 /* 1 = waiting for '(' or '{' */
407 /* 2 = waiting for ')' or '}' */
408 /* 3 = waiting for ''' */
410 prev = '\0'; /* previous character */
412 while(inputcnt && outputcnt){
417 /* remove one level of escape characters */
418 if((c == '\\') && (prev != '\\')){
429 case 0: /* Waiting for (unescaped) $ */
430 if((c == '\'') && (prev != '\\')){
434 if((c == '$') && (prev != '\\')){
441 case 1: /* Waiting for ( */
442 if(c == '(' || c == '{'){
444 varname_start = input;
456 case 2: /* Waiting for ) */
457 if(c == ')' || c == '}'){
459 char envname[CFG_CBSIZE], *envval;
460 int envcnt = input - varname_start - 1; /* Varname # of chars */
462 /* Get the varname */
463 for(i = 0; i < envcnt; i++){
464 envname[i] = varname_start[i];
469 envval = getenv(envname);
471 /* Copy into the line if it exists */
473 while((*envval) && outputcnt){
474 *(output++) = *(envval++);
478 /* Look for another '$' */
482 case 3: /* Waiting for ' */
483 if((c == '\'') && (prev != '\\')){
499 /****************************************************************************
501 * 1 - command executed, repeatable
502 * 0 - command executed but not repeatable, interrupted commands are
503 * always considered not repeatable
504 * -1 - not executed (unrecognized, bootd recursion or too many args)
505 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
506 * considered unrecognized)
510 * We must create a temporary copy of the command since the command we get
511 * may be the result from getenv(), which returns a pointer directly to
512 * the environment data, which may change magicly when the command we run
513 * creates or modifies environment variables (like "bootp" does).
516 int run_command(const char *cmd, int flag){
518 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
519 char *token; /* start of token in cmdbuf */
520 char *sep; /* end of token (separator) in cmdbuf */
521 char finaltoken[CFG_CBSIZE];
523 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
528 clear_ctrlc(); /* forget any previous Control C */
531 return(-1); /* empty command */
534 if(strlen(cmd) >= CFG_CBSIZE){
535 puts("## Error: command too long!\n");
541 /* Process separators and check for invalid
542 * repeatable commands
547 * Find separator, or string end
548 * Allow simple escape of ';' by writing "\;"
550 for(inquotes = 0, sep = str; *sep; sep++){
551 if((*sep == '\'') && (*(sep - 1) != '\\')){
552 inquotes = !inquotes;
555 if(!inquotes && (*sep == ';') && (sep != str) && (*(sep - 1) != '\\')){
561 * Limit the token to data between separators
565 str = sep + 1; /* start of command for next pass */
568 str = sep; /* no more commands for next pass */
571 /* find macros in this token and replace them */
572 process_macros(token, finaltoken);
574 /* Extract arguments */
575 if((argc = parse_line(finaltoken, argv)) == 0){
576 rc = -1; /* no command at all */
580 /* Look up command in command table */
581 if((cmdtp = find_cmd(argv[0])) == NULL){
582 printf("## Error: unknown command '%s' - try 'help'\n\n", argv[0]);
583 rc = -1; /* give up after bad command */
587 /* found - check max args */
588 if(argc > cmdtp->maxargs){
590 if(cmdtp->help != NULL){
591 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
593 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
596 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
602 /* OK - call function to do the command */
603 if((cmdtp->cmd)(cmdtp, flag, argc, argv) != 0){
607 repeatable &= cmdtp->repeatable;
609 /* Did the user stop this? */
610 if(had_ctrlc()){ /* if stopped then not repeatable */
615 return(rc ? rc : repeatable);
618 /****************************************************************************/
620 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
621 int do_run(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]){
626 if(cmdtp->help != NULL){
627 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
629 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
632 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
637 for(i=1; i<argc; ++i){
640 if((arg = getenv(argv[i])) == NULL){
641 printf("## Error: \"%s\" not defined\n", argv[i]);
645 if(run_command(arg, flag) == -1){
652 #endif /* CFG_CMD_RUN */