3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Add to readline cmdline-editing by
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #ifdef CONFIG_MODEM_SUPPORT
37 #include <malloc.h> /* for free() prototype */
40 #ifdef CONFIG_SYS_HUSH_PARSER
44 #ifdef CONFIG_OF_CONTROL
49 #include <linux/ctype.h>
52 DECLARE_GLOBAL_DATA_PTR
;
55 * Board-specific Platform code can reimplement show_boot_progress () if needed
57 void inline __show_boot_progress (int val
) {}
58 void show_boot_progress (int val
) __attribute__((weak
, alias("__show_boot_progress")));
60 #define MAX_DELAY_STOP_STR 32
64 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
66 static char * delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
);
67 static const char erase_seq
[] = "\b \b"; /* erase sequence */
68 static const char tab_seq
[] = " "; /* used to expand TABs */
70 #ifdef CONFIG_BOOT_RETRY_TIME
71 static uint64_t endtime
= 0; /* must be set, default is instant timeout */
72 static int retry_time
= -1; /* -1 so can call readline before main_loop */
75 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
77 #ifndef CONFIG_BOOT_RETRY_MIN
78 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
81 #ifdef CONFIG_MODEM_SUPPORT
83 extern void mdm_init(void); /* defined in board.c */
86 /***************************************************************************
87 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
88 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
90 #if defined(CONFIG_BOOTDELAY)
91 # if defined(CONFIG_AUTOBOOT_KEYED)
92 static int abortboot_keyed(int bootdelay
)
95 uint64_t etime
= endtick(bootdelay
);
102 { str
: getenv ("bootdelaykey"), retry
: 1 },
103 { str
: getenv ("bootdelaykey2"), retry
: 1 },
104 { str
: getenv ("bootstopkey"), retry
: 0 },
105 { str
: getenv ("bootstopkey2"), retry
: 0 },
108 char presskey
[MAX_DELAY_STOP_STR
];
109 u_int presskey_len
= 0;
110 u_int presskey_max
= 0;
113 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
118 # ifdef CONFIG_AUTOBOOT_PROMPT
119 printf(CONFIG_AUTOBOOT_PROMPT
);
122 # ifdef CONFIG_AUTOBOOT_DELAY_STR
123 if (delaykey
[0].str
== NULL
)
124 delaykey
[0].str
= CONFIG_AUTOBOOT_DELAY_STR
;
126 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
127 if (delaykey
[1].str
== NULL
)
128 delaykey
[1].str
= CONFIG_AUTOBOOT_DELAY_STR2
;
130 # ifdef CONFIG_AUTOBOOT_STOP_STR
131 if (delaykey
[2].str
== NULL
)
132 delaykey
[2].str
= CONFIG_AUTOBOOT_STOP_STR
;
134 # ifdef CONFIG_AUTOBOOT_STOP_STR2
135 if (delaykey
[3].str
== NULL
)
136 delaykey
[3].str
= CONFIG_AUTOBOOT_STOP_STR2
;
139 for (i
= 0; i
< sizeof(delaykey
) / sizeof(delaykey
[0]); i
++) {
140 delaykey
[i
].len
= delaykey
[i
].str
== NULL
?
141 0 : strlen (delaykey
[i
].str
);
142 delaykey
[i
].len
= delaykey
[i
].len
> MAX_DELAY_STOP_STR
?
143 MAX_DELAY_STOP_STR
: delaykey
[i
].len
;
145 presskey_max
= presskey_max
> delaykey
[i
].len
?
146 presskey_max
: delaykey
[i
].len
;
149 printf("%s key:<%s>\n",
150 delaykey
[i
].retry
? "delay" : "stop",
151 delaykey
[i
].str
? delaykey
[i
].str
: "NULL");
155 /* In order to keep up with incoming data, check timeout only
160 if (presskey_len
< presskey_max
) {
161 presskey
[presskey_len
++] = getc();
164 for (i
= 0; i
< presskey_max
- 1; i
++)
165 presskey
[i
] = presskey
[i
+ 1];
167 presskey
[i
] = getc();
171 for (i
= 0; i
< sizeof(delaykey
) / sizeof(delaykey
[0]); i
++) {
172 if (delaykey
[i
].len
> 0 &&
173 presskey_len
>= delaykey
[i
].len
&&
174 memcmp (presskey
+ presskey_len
- delaykey
[i
].len
,
176 delaykey
[i
].len
) == 0) {
178 printf("got %skey\n",
179 delaykey
[i
].retry
? "delay" : "stop");
182 # ifdef CONFIG_BOOT_RETRY_TIME
183 /* don't retry auto boot */
184 if (! delaykey
[i
].retry
)
190 } while (!abort
&& get_ticks() <= etime
);
194 puts("key timeout\n");
197 #ifdef CONFIG_SILENT_CONSOLE
199 gd
->flags
&= ~GD_FLG_SILENT
;
205 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
207 #ifdef CONFIG_MENUKEY
208 static int menukey
= 0;
211 static int abortboot_normal(int bootdelay
)
216 #ifdef CONFIG_MENUPROMPT
217 printf(CONFIG_MENUPROMPT
);
220 printf("Hit any key to stop autoboot: %2d ", bootdelay
);
223 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
225 * Check if key already pressed
226 * Don't check if bootdelay < 0
228 if (bootdelay
>= 0) {
229 if (tstc()) { /* we got a key press */
230 (void) getc(); /* consume input */
232 abort
= 1; /* don't auto boot */
237 while ((bootdelay
> 0) && (!abort
)) {
242 if (tstc()) { /* we got a key press */
243 abort
= 1; /* don't auto boot */
244 bootdelay
= 0; /* no more delay */
245 # ifdef CONFIG_MENUKEY
248 (void) getc(); /* consume input */
253 } while (!abort
&& get_timer(ts
) < 1000);
255 printf("\b\b\b%2d ", bootdelay
);
260 #ifdef CONFIG_SILENT_CONSOLE
262 gd
->flags
&= ~GD_FLG_SILENT
;
267 # endif /* CONFIG_AUTOBOOT_KEYED */
269 static int abortboot(int bootdelay
)
271 #ifdef CONFIG_AUTOBOOT_KEYED
272 return abortboot_keyed(bootdelay
);
274 return abortboot_normal(bootdelay
);
277 #endif /* CONFIG_BOOTDELAY */
280 * Runs the given boot command securely. Specifically:
281 * - Doesn't run the command with the shell (run_command or parse_string_outer),
282 * since that's a lot of code surface that an attacker might exploit.
283 * Because of this, we don't do any argument parsing--the secure boot command
284 * has to be a full-fledged u-boot command.
285 * - Doesn't check for keypresses before booting, since that could be a
286 * security hole; also disables Ctrl-C.
287 * - Doesn't allow the command to return.
289 * Upon any failures, this function will drop into an infinite loop after
290 * printing the error message to console.
293 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
294 static void secure_boot_cmd(char *cmd
)
300 printf("## Error: Secure boot command not specified\n");
304 /* Disable Ctrl-C just in case some command is used that checks it. */
307 /* Find the command directly. */
308 cmdtp
= find_cmd(cmd
);
310 printf("## Error: \"%s\" not defined\n", cmd
);
314 /* Run the command, forcing no flags and faking argc and argv. */
315 rc
= (cmdtp
->cmd
)(cmdtp
, 0, 1, &cmd
);
317 /* Shouldn't ever return from boot command. */
318 printf("## Error: \"%s\" returned (code %d)\n", cmd
, rc
);
322 * Not a whole lot to do here. Rebooting won't help much, since we'll
323 * just end up right back here. Just loop.
328 static void process_fdt_options(const void *blob
)
332 /* Add an env variable to point to a kernel payload, if available */
333 addr
= fdtdec_get_config_int(gd
->fdt_blob
, "kernel-offset", 0);
335 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE
+ addr
));
337 /* Add an env variable to point to a root disk, if available */
338 addr
= fdtdec_get_config_int(gd
->fdt_blob
, "rootdisk-offset", 0);
340 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE
+ addr
));
342 #endif /* CONFIG_OF_CONTROL */
345 /****************************************************************************/
347 void main_loop (void)
349 #ifndef CONFIG_SYS_HUSH_PARSER
350 static char lastcommand
[CONFIG_SYS_CBSIZE
] = { 0, };
355 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
358 #if defined(CONFIG_BOOTDELAY)
362 #ifdef CONFIG_PREBOOT
365 #ifdef CONFIG_BOOTCOUNT_LIMIT
366 unsigned long bootcount
= 0;
367 unsigned long bootlimit
= 0;
370 #endif /* CONFIG_BOOTCOUNT_LIMIT */
372 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP
, "main_loop");
374 #ifdef CONFIG_BOOTCOUNT_LIMIT
375 bootcount
= bootcount_load();
377 bootcount_store (bootcount
);
378 sprintf (bcs_set
, "%lu", bootcount
);
379 setenv ("bootcount", bcs_set
);
380 bcs
= getenv ("bootlimit");
381 bootlimit
= bcs
? simple_strtoul (bcs
, NULL
, 10) : 0;
382 #endif /* CONFIG_BOOTCOUNT_LIMIT */
384 #ifdef CONFIG_MODEM_SUPPORT
385 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init
);
387 char *str
= strdup(getenv("mdm_cmd"));
388 setenv ("preboot", str
); /* set or delete definition */
391 mdm_init(); /* wait for modem connection */
393 #endif /* CONFIG_MODEM_SUPPORT */
395 #ifdef CONFIG_VERSION_VARIABLE
397 setenv ("ver", version_string
); /* set version variable */
399 #endif /* CONFIG_VERSION_VARIABLE */
401 #ifdef CONFIG_SYS_HUSH_PARSER
402 u_boot_hush_start ();
405 #if defined(CONFIG_HUSH_INIT_VAR)
409 #ifdef CONFIG_PREBOOT
410 if ((p
= getenv ("preboot")) != NULL
) {
411 # ifdef CONFIG_AUTOBOOT_KEYED
412 int prev
= disable_ctrlc(1); /* disable Control C checking */
415 run_command_list(p
, -1, 0);
417 # ifdef CONFIG_AUTOBOOT_KEYED
418 disable_ctrlc(prev
); /* restore Control C checking */
421 #endif /* CONFIG_PREBOOT */
423 #if defined(CONFIG_UPDATE_TFTP)
425 #endif /* CONFIG_UPDATE_TFTP */
427 #if defined(CONFIG_BOOTDELAY)
428 s
= getenv ("bootdelay");
429 bootdelay
= s
? (int)simple_strtol(s
, NULL
, 10) : CONFIG_BOOTDELAY
;
431 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
433 #if defined(CONFIG_MENU_SHOW)
434 bootdelay
= menu_show(bootdelay
);
436 # ifdef CONFIG_BOOT_RETRY_TIME
438 # endif /* CONFIG_BOOT_RETRY_TIME */
441 if (gd
->flags
& GD_FLG_POSTFAIL
) {
442 s
= getenv("failbootcmd");
445 #endif /* CONFIG_POST */
446 #ifdef CONFIG_BOOTCOUNT_LIMIT
447 if (bootlimit
&& (bootcount
> bootlimit
)) {
448 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
449 (unsigned)bootlimit
);
450 s
= getenv ("altbootcmd");
453 #endif /* CONFIG_BOOTCOUNT_LIMIT */
454 s
= getenv ("bootcmd");
455 #ifdef CONFIG_OF_CONTROL
456 /* Allow the fdt to override the boot command */
457 env
= fdtdec_get_config_string(gd
->fdt_blob
, "bootcmd");
461 process_fdt_options(gd
->fdt_blob
);
464 * If the bootsecure option was chosen, use secure_boot_cmd().
465 * Always use 'env' in this case, since bootsecure requres that the
466 * bootcmd was specified in the FDT too.
468 if (fdtdec_get_config_int(gd
->fdt_blob
, "bootsecure", 0))
469 secure_boot_cmd(env
);
471 #endif /* CONFIG_OF_CONTROL */
473 debug ("### main_loop: bootcmd=\"%s\"\n", s
? s
: "<UNDEFINED>");
475 if (bootdelay
!= -1 && s
&& !abortboot(bootdelay
)) {
476 # ifdef CONFIG_AUTOBOOT_KEYED
477 int prev
= disable_ctrlc(1); /* disable Control C checking */
480 run_command_list(s
, -1, 0);
482 # ifdef CONFIG_AUTOBOOT_KEYED
483 disable_ctrlc(prev
); /* restore Control C checking */
487 # ifdef CONFIG_MENUKEY
488 if (menukey
== CONFIG_MENUKEY
) {
489 s
= getenv("menucmd");
491 run_command_list(s
, -1, 0);
493 #endif /* CONFIG_MENUKEY */
494 #endif /* CONFIG_BOOTDELAY */
497 * Main Loop for Monitor Command Processing
499 #ifdef CONFIG_SYS_HUSH_PARSER
501 /* This point is never reached */
505 #ifdef CONFIG_BOOT_RETRY_TIME
507 /* Saw enough of a valid command to
508 * restart the timeout.
513 len
= readline (CONFIG_SYS_PROMPT
);
515 flag
= 0; /* assume no special flags for now */
517 strcpy (lastcommand
, console_buffer
);
519 flag
|= CMD_FLAG_REPEAT
;
520 #ifdef CONFIG_BOOT_RETRY_TIME
521 else if (len
== -2) {
522 /* -2 means timed out, retry autoboot
524 puts ("\nTimed out waiting for command\n");
525 # ifdef CONFIG_RESET_TO_RETRY
526 /* Reinit board to run initialization code again */
527 do_reset (NULL
, 0, 0, NULL
);
529 return; /* retry autoboot */
535 puts ("<INTERRUPT>\n");
537 rc
= run_command(lastcommand
, flag
);
540 /* invalid command or not repeatable, forget it */
544 #endif /*CONFIG_SYS_HUSH_PARSER*/
547 #ifdef CONFIG_BOOT_RETRY_TIME
548 /***************************************************************************
549 * initialize command line timeout
551 void init_cmd_timeout(void)
553 char *s
= getenv ("bootretry");
556 retry_time
= (int)simple_strtol(s
, NULL
, 10);
558 retry_time
= CONFIG_BOOT_RETRY_TIME
;
560 if (retry_time
>= 0 && retry_time
< CONFIG_BOOT_RETRY_MIN
)
561 retry_time
= CONFIG_BOOT_RETRY_MIN
;
564 /***************************************************************************
565 * reset command line timeout to retry_time seconds
567 void reset_cmd_timeout(void)
569 endtime
= endtick(retry_time
);
573 #ifdef CONFIG_CMDLINE_EDITING
576 * cmdline-editing related codes from vivi.
577 * Author: Janghoon Lyu <nandy@mizi.com>
580 #define putnstr(str,n) do { \
581 printf ("%.*s", (int)n, str); \
584 #define CTL_CH(c) ((c) - 'a' + 1)
585 #define CTL_BACKSPACE ('\b')
586 #define DEL ((char)255)
587 #define DEL7 ((char)127)
588 #define CREAD_HIST_CHAR ('!')
590 #define getcmd_putch(ch) putc(ch)
591 #define getcmd_getch() getc()
592 #define getcmd_cbeep() getcmd_putch('\a')
595 #define HIST_SIZE CONFIG_SYS_CBSIZE
598 static int hist_add_idx
;
599 static int hist_cur
= -1;
600 static unsigned hist_num
;
602 static char *hist_list
[HIST_MAX
];
603 static char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
605 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
607 static void hist_init(void)
616 for (i
= 0; i
< HIST_MAX
; i
++) {
617 hist_list
[i
] = hist_lines
[i
];
618 hist_list
[i
][0] = '\0';
622 static void cread_add_to_hist(char *line
)
624 strcpy(hist_list
[hist_add_idx
], line
);
626 if (++hist_add_idx
>= HIST_MAX
)
629 if (hist_add_idx
> hist_max
)
630 hist_max
= hist_add_idx
;
635 static char* hist_prev(void)
647 if (hist_cur
== hist_add_idx
) {
651 ret
= hist_list
[hist_cur
];
656 static char* hist_next(void)
663 if (hist_cur
== hist_add_idx
)
666 if (++hist_cur
> hist_max
)
669 if (hist_cur
== hist_add_idx
) {
672 ret
= hist_list
[hist_cur
];
677 #ifndef CONFIG_CMDLINE_EDITING
678 static void cread_print_hist_list(void)
683 n
= hist_num
- hist_max
;
685 i
= hist_add_idx
+ 1;
689 if (i
== hist_add_idx
)
691 printf("%s\n", hist_list
[i
]);
696 #endif /* CONFIG_CMDLINE_EDITING */
698 #define BEGINNING_OF_LINE() { \
700 getcmd_putch(CTL_BACKSPACE); \
705 #define ERASE_TO_EOL() { \
706 if (num < eol_num) { \
707 printf("%*s", (int)(eol_num - num), ""); \
709 getcmd_putch(CTL_BACKSPACE); \
710 } while (--eol_num > num); \
714 #define REFRESH_TO_EOL() { \
715 if (num < eol_num) { \
716 wlen = eol_num - num; \
717 putnstr(buf + num, wlen); \
722 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
723 unsigned long *eol_num
, char *buf
, unsigned long len
)
728 if (insert
|| *num
== *eol_num
) {
729 if (*eol_num
> len
- 1) {
737 wlen
= *eol_num
- *num
;
739 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
743 putnstr(buf
+ *num
, wlen
);
746 getcmd_putch(CTL_BACKSPACE
);
749 /* echo the character */
752 putnstr(buf
+ *num
, wlen
);
757 static void cread_add_str(char *str
, int strsize
, int insert
, unsigned long *num
,
758 unsigned long *eol_num
, char *buf
, unsigned long len
)
761 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
766 static int cread_line(const char *const prompt
, char *buf
, unsigned int *len
,
769 unsigned long num
= 0;
770 unsigned long eol_num
= 0;
776 int init_len
= strlen(buf
);
780 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
783 #ifdef CONFIG_BOOT_RETRY_TIME
784 while (!tstc()) { /* while no incoming data */
785 if (retry_time
>= 0 && get_ticks() > endtime
)
786 return (-2); /* timed out */
790 if (first
&& timeout
) {
791 uint64_t etime
= endtick(timeout
);
793 while (!tstc()) { /* while no incoming data */
794 if (get_ticks() >= etime
)
795 return -2; /* timed out */
801 ichar
= getcmd_getch();
803 if ((ichar
== '\n') || (ichar
== '\r')) {
809 * handle standard linux xterm esc sequences for arrow key, etc.
814 esc_save
[esc_len
] = ichar
;
817 cread_add_str(esc_save
, esc_len
, insert
,
818 &num
, &eol_num
, buf
, *len
);
826 case 'D': /* <- key */
830 case 'C': /* -> key */
833 break; /* pass off to ^F handler */
834 case 'H': /* Home key */
837 break; /* pass off to ^A handler */
838 case 'A': /* up arrow */
841 break; /* pass off to ^P handler */
842 case 'B': /* down arrow */
845 break; /* pass off to ^N handler */
847 esc_save
[esc_len
++] = ichar
;
848 cread_add_str(esc_save
, esc_len
, insert
,
849 &num
, &eol_num
, buf
, *len
);
858 esc_save
[esc_len
] = ichar
;
861 puts("impossible condition #876\n");
869 case CTL_CH('c'): /* ^C - break */
870 *buf
= '\0'; /* discard input */
874 getcmd_putch(buf
[num
]);
880 getcmd_putch(CTL_BACKSPACE
);
886 wlen
= eol_num
- num
- 1;
888 memmove(&buf
[num
], &buf
[num
+1], wlen
);
889 putnstr(buf
+ num
, wlen
);
894 getcmd_putch(CTL_BACKSPACE
);
917 wlen
= eol_num
- num
;
919 memmove(&buf
[num
], &buf
[num
+1], wlen
);
920 getcmd_putch(CTL_BACKSPACE
);
921 putnstr(buf
+ num
, wlen
);
924 getcmd_putch(CTL_BACKSPACE
);
936 if (ichar
== CTL_CH('p'))
946 /* nuke the current line */
950 /* erase to end of line */
953 /* copy new line into place and display */
955 eol_num
= strlen(buf
);
959 #ifdef CONFIG_AUTO_COMPLETE
963 /* do not autocomplete when in the middle */
970 col
= strlen(prompt
) + eol_num
;
972 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
981 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
, *len
);
986 buf
[eol_num
] = '\0'; /* lose the newline */
988 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
989 cread_add_to_hist(buf
);
990 hist_cur
= hist_add_idx
;
995 #endif /* CONFIG_CMDLINE_EDITING */
997 /****************************************************************************/
1000 * Prompt for input and read a line.
1001 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
1002 * time out when time goes past endtime (timebase time in ticks).
1003 * Return: number of read characters
1007 int readline (const char *const prompt
)
1010 * If console_buffer isn't 0-length the user will be prompted to modify
1011 * it instead of entering it from scratch as desired.
1013 console_buffer
[0] = '\0';
1015 return readline_into_buffer(prompt
, console_buffer
, 0);
1019 int readline_into_buffer(const char *const prompt
, char *buffer
, int timeout
)
1022 #ifdef CONFIG_CMDLINE_EDITING
1023 unsigned int len
= CONFIG_SYS_CBSIZE
;
1025 static int initted
= 0;
1028 * History uses a global array which is not
1029 * writable until after relocation to RAM.
1030 * Revert to non-history version if still
1031 * running from flash.
1033 if (gd
->flags
& GD_FLG_RELOC
) {
1042 rc
= cread_line(prompt
, p
, &len
, timeout
);
1043 return rc
< 0 ? rc
: len
;
1046 #endif /* CONFIG_CMDLINE_EDITING */
1048 int n
= 0; /* buffer index */
1049 int plen
= 0; /* prompt length */
1050 int col
; /* output column cnt */
1055 plen
= strlen (prompt
);
1061 #ifdef CONFIG_BOOT_RETRY_TIME
1062 while (!tstc()) { /* while no incoming data */
1063 if (retry_time
>= 0 && get_ticks() > endtime
)
1064 return (-2); /* timed out */
1068 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1070 #ifdef CONFIG_SHOW_ACTIVITY
1079 * Special character handling
1082 case '\r': /* Enter */
1088 case '\0': /* nul */
1091 case 0x03: /* ^C - break */
1092 p_buf
[0] = '\0'; /* discard input */
1095 case 0x15: /* ^U - erase line */
1096 while (col
> plen
) {
1104 case 0x17: /* ^W - erase word */
1105 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1106 while ((n
> 0) && (*p
!= ' ')) {
1107 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1111 case 0x08: /* ^H - backspace */
1112 case 0x7F: /* DEL - backspace */
1113 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1118 * Must be a normal character then
1120 if (n
< CONFIG_SYS_CBSIZE
-2) {
1121 if (c
== '\t') { /* expand TABs */
1122 #ifdef CONFIG_AUTO_COMPLETE
1123 /* if auto completion triggered just continue */
1125 if (cmd_auto_complete(prompt
, console_buffer
, &n
, &col
)) {
1126 p
= p_buf
+ n
; /* reset */
1130 puts (tab_seq
+(col
&07));
1131 col
+= 8 - (col
&07);
1136 * Echo input using puts() to force am
1137 * LCD flush if we are using an LCD
1146 } else { /* Buffer full */
1151 #ifdef CONFIG_CMDLINE_EDITING
1156 /****************************************************************************/
1158 static char * delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
1166 if (*(--p
) == '\t') { /* will retype the whole line */
1167 while (*colp
> plen
) {
1171 for (s
=buffer
; s
<p
; ++s
) {
1173 puts (tab_seq
+((*colp
) & 07));
1174 *colp
+= 8 - ((*colp
) & 07);
1188 /****************************************************************************/
1190 int parse_line (char *line
, char *argv
[])
1195 printf ("parse_line: \"%s\"\n", line
);
1197 while (nargs
< CONFIG_SYS_MAXARGS
) {
1199 /* skip any white space */
1200 while (isblank(*line
))
1203 if (*line
== '\0') { /* end of line, no more args */
1206 printf ("parse_line: nargs=%d\n", nargs
);
1211 argv
[nargs
++] = line
; /* begin of argument string */
1213 /* find end of string */
1214 while (*line
&& !isblank(*line
))
1217 if (*line
== '\0') { /* end of line, no more args */
1220 printf ("parse_line: nargs=%d\n", nargs
);
1225 *line
++ = '\0'; /* terminate current arg */
1228 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS
);
1231 printf ("parse_line: nargs=%d\n", nargs
);
1236 /****************************************************************************/
1238 #ifndef CONFIG_SYS_HUSH_PARSER
1239 static void process_macros (const char *input
, char *output
)
1242 const char *varname_start
= NULL
;
1243 int inputcnt
= strlen (input
);
1244 int outputcnt
= CONFIG_SYS_CBSIZE
;
1245 int state
= 0; /* 0 = waiting for '$' */
1247 /* 1 = waiting for '(' or '{' */
1248 /* 2 = waiting for ')' or '}' */
1249 /* 3 = waiting for ''' */
1251 char *output_start
= output
;
1253 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input
),
1257 prev
= '\0'; /* previous character */
1259 while (inputcnt
&& outputcnt
) {
1264 /* remove one level of escape characters */
1265 if ((c
== '\\') && (prev
!= '\\')) {
1266 if (inputcnt
-- == 0)
1274 case 0: /* Waiting for (unescaped) $ */
1275 if ((c
== '\'') && (prev
!= '\\')) {
1279 if ((c
== '$') && (prev
!= '\\')) {
1286 case 1: /* Waiting for ( */
1287 if (c
== '(' || c
== '{') {
1289 varname_start
= input
;
1301 case 2: /* Waiting for ) */
1302 if (c
== ')' || c
== '}') {
1304 char envname
[CONFIG_SYS_CBSIZE
], *envval
;
1305 int envcnt
= input
- varname_start
- 1; /* Varname # of chars */
1307 /* Get the varname */
1308 for (i
= 0; i
< envcnt
; i
++) {
1309 envname
[i
] = varname_start
[i
];
1314 envval
= getenv (envname
);
1316 /* Copy into the line if it exists */
1318 while ((*envval
) && outputcnt
) {
1319 *(output
++) = *(envval
++);
1322 /* Look for another '$' */
1326 case 3: /* Waiting for ' */
1327 if ((c
== '\'') && (prev
!= '\\')) {
1344 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1345 strlen (output_start
), output_start
);
1349 /****************************************************************************
1351 * 1 - command executed, repeatable
1352 * 0 - command executed but not repeatable, interrupted commands are
1353 * always considered not repeatable
1354 * -1 - not executed (unrecognized, bootd recursion or too many args)
1355 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1356 * considered unrecognized)
1360 * We must create a temporary copy of the command since the command we get
1361 * may be the result from getenv(), which returns a pointer directly to
1362 * the environment data, which may change magicly when the command we run
1363 * creates or modifies environment variables (like "bootp" does).
1365 static int builtin_run_command(const char *cmd
, int flag
)
1367 char cmdbuf
[CONFIG_SYS_CBSIZE
]; /* working copy of cmd */
1368 char *token
; /* start of token in cmdbuf */
1369 char *sep
; /* end of token (separator) in cmdbuf */
1370 char finaltoken
[CONFIG_SYS_CBSIZE
];
1372 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
1378 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd
);
1379 puts (cmd
? cmd
: "NULL"); /* use puts - string may be loooong */
1383 clear_ctrlc(); /* forget any previous Control C */
1385 if (!cmd
|| !*cmd
) {
1386 return -1; /* empty command */
1389 if (strlen(cmd
) >= CONFIG_SYS_CBSIZE
) {
1390 puts ("## Command too long!\n");
1394 strcpy (cmdbuf
, cmd
);
1396 /* Process separators and check for invalid
1397 * repeatable commands
1401 printf ("[PROCESS_SEPARATORS] %s\n", cmd
);
1406 * Find separator, or string end
1407 * Allow simple escape of ';' by writing "\;"
1409 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
1415 (*sep
== ';') && /* separator */
1416 ( sep
!= str
) && /* past string start */
1417 (*(sep
-1) != '\\')) /* and NOT escaped */
1422 * Limit the token to data between separators
1426 str
= sep
+ 1; /* start of command for next pass */
1430 str
= sep
; /* no more commands for next pass */
1432 printf ("token: \"%s\"\n", token
);
1435 /* find macros in this token and replace them */
1436 process_macros (token
, finaltoken
);
1438 /* Extract arguments */
1439 if ((argc
= parse_line (finaltoken
, argv
)) == 0) {
1440 rc
= -1; /* no command at all */
1444 if (cmd_process(flag
, argc
, argv
, &repeatable
, NULL
))
1447 /* Did the user stop this? */
1449 return -1; /* if stopped then not repeatable */
1452 return rc
? rc
: repeatable
;
1457 * Run a command using the selected parser.
1459 * @param cmd Command to run
1460 * @param flag Execution flags (CMD_FLAG_...)
1461 * @return 0 on success, or != 0 on error.
1463 int run_command(const char *cmd
, int flag
)
1465 #ifndef CONFIG_SYS_HUSH_PARSER
1467 * builtin_run_command can return 0 or 1 for success, so clean up
1470 if (builtin_run_command(cmd
, flag
) == -1)
1475 return parse_string_outer(cmd
,
1476 FLAG_PARSE_SEMICOLON
| FLAG_EXIT_FROM_LOOP
);
1480 #ifndef CONFIG_SYS_HUSH_PARSER
1482 * Execute a list of command separated by ; or \n using the built-in parser.
1484 * This function cannot take a const char * for the command, since if it
1485 * finds newlines in the string, it replaces them with \0.
1487 * @param cmd String containing list of commands
1488 * @param flag Execution flags (CMD_FLAG_...)
1489 * @return 0 on success, or != 0 on error.
1491 static int builtin_run_command_list(char *cmd
, int flag
)
1497 * Break into individual lines, and execute each line; terminate on
1502 if (*next
== '\n') {
1504 /* run only non-empty commands */
1506 debug("** exec: \"%s\"\n", line
);
1507 if (builtin_run_command(line
, 0) < 0) {
1516 if (rcode
== 0 && *line
)
1517 rcode
= (builtin_run_command(line
, 0) >= 0);
1523 int run_command_list(const char *cmd
, int len
, int flag
)
1526 char *buff
= (char *)cmd
; /* cast away const */
1531 #ifdef CONFIG_SYS_HUSH_PARSER
1532 /* hush will never change our string */
1535 /* the built-in parser will change our string if it sees \n */
1536 need_buff
= strchr(cmd
, '\n') != NULL
;
1540 buff
= malloc(len
+ 1);
1543 memcpy(buff
, cmd
, len
);
1546 #ifdef CONFIG_SYS_HUSH_PARSER
1547 rcode
= parse_string_outer(buff
, FLAG_PARSE_SEMICOLON
);
1550 * This function will overwrite any \n it sees with a \0, which
1551 * is why it can't work with a const char *. Here we are making
1552 * using of internal knowledge of this function, to avoid always
1553 * doing a malloc() which is actually required only in a case that
1556 rcode
= builtin_run_command_list(buff
, flag
);
1564 /****************************************************************************/
1566 #if defined(CONFIG_CMD_RUN)
1567 int do_run (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
1572 return CMD_RET_USAGE
;
1574 for (i
=1; i
<argc
; ++i
) {
1577 if ((arg
= getenv (argv
[i
])) == NULL
) {
1578 printf ("## Error: \"%s\" not defined\n", argv
[i
]);
1582 if (run_command(arg
, flag
) != 0)