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
48 #ifdef CONFIG_OF_LIBFDT
49 #include <fdt_support.h>
50 #endif /* CONFIG_OF_LIBFDT */
53 #include <linux/ctype.h>
56 DECLARE_GLOBAL_DATA_PTR;
59 * Board-specific Platform code can reimplement show_boot_progress () if needed
61 void inline __show_boot_progress (int val) {}
62 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
64 #if defined(CONFIG_UPDATE_TFTP)
65 int update_tftp (ulong addr);
66 #endif /* CONFIG_UPDATE_TFTP */
68 #define MAX_DELAY_STOP_STR 32
72 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
74 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
75 static const char erase_seq[] = "\b \b"; /* erase sequence */
76 static const char tab_seq[] = " "; /* used to expand TABs */
78 #ifdef CONFIG_BOOT_RETRY_TIME
79 static uint64_t endtime = 0; /* must be set, default is instant timeout */
80 static int retry_time = -1; /* -1 so can call readline before main_loop */
83 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
85 #ifndef CONFIG_BOOT_RETRY_MIN
86 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
89 #ifdef CONFIG_MODEM_SUPPORT
91 extern void mdm_init(void); /* defined in board.c */
94 /***************************************************************************
95 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
96 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
98 #if defined(CONFIG_BOOTDELAY)
99 # if defined(CONFIG_AUTOBOOT_KEYED)
103 int abortboot(int bootdelay)
106 uint64_t etime = endtick(bootdelay);
113 { str: getenv ("bootdelaykey"), retry: 1 },
114 { str: getenv ("bootdelaykey2"), retry: 1 },
115 { str: getenv ("bootstopkey"), retry: 0 },
116 { str: getenv ("bootstopkey2"), retry: 0 },
119 char presskey [MAX_DELAY_STOP_STR];
120 u_int presskey_len = 0;
121 u_int presskey_max = 0;
124 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
129 # ifdef CONFIG_AUTOBOOT_PROMPT
130 printf(CONFIG_AUTOBOOT_PROMPT);
133 # ifdef CONFIG_AUTOBOOT_DELAY_STR
134 if (delaykey[0].str == NULL)
135 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
137 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
138 if (delaykey[1].str == NULL)
139 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
141 # ifdef CONFIG_AUTOBOOT_STOP_STR
142 if (delaykey[2].str == NULL)
143 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
145 # ifdef CONFIG_AUTOBOOT_STOP_STR2
146 if (delaykey[3].str == NULL)
147 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
150 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
151 delaykey[i].len = delaykey[i].str == NULL ?
152 0 : strlen (delaykey[i].str);
153 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
154 MAX_DELAY_STOP_STR : delaykey[i].len;
156 presskey_max = presskey_max > delaykey[i].len ?
157 presskey_max : delaykey[i].len;
160 printf("%s key:<%s>\n",
161 delaykey[i].retry ? "delay" : "stop",
162 delaykey[i].str ? delaykey[i].str : "NULL");
166 /* In order to keep up with incoming data, check timeout only
171 if (presskey_len < presskey_max) {
172 presskey [presskey_len ++] = getc();
175 for (i = 0; i < presskey_max - 1; i ++)
176 presskey [i] = presskey [i + 1];
178 presskey [i] = getc();
182 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
183 if (delaykey[i].len > 0 &&
184 presskey_len >= delaykey[i].len &&
185 memcmp (presskey + presskey_len - delaykey[i].len,
187 delaykey[i].len) == 0) {
189 printf("got %skey\n",
190 delaykey[i].retry ? "delay" : "stop");
193 # ifdef CONFIG_BOOT_RETRY_TIME
194 /* don't retry auto boot */
195 if (! delaykey[i].retry)
201 } while (!abort && get_ticks() <= etime);
205 puts("key timeout\n");
208 #ifdef CONFIG_SILENT_CONSOLE
210 gd->flags &= ~GD_FLG_SILENT;
216 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
218 #ifdef CONFIG_MENUKEY
219 static int menukey = 0;
225 int abortboot(int bootdelay)
230 #ifdef CONFIG_MENUPROMPT
231 printf(CONFIG_MENUPROMPT);
234 printf("Hit any key to stop autoboot: %2d ", bootdelay);
237 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
239 * Check if key already pressed
240 * Don't check if bootdelay < 0
242 if (bootdelay >= 0) {
243 if (tstc()) { /* we got a key press */
244 (void) getc(); /* consume input */
246 abort = 1; /* don't auto boot */
251 while ((bootdelay > 0) && (!abort)) {
256 if (tstc()) { /* we got a key press */
257 abort = 1; /* don't auto boot */
258 bootdelay = 0; /* no more delay */
259 # ifdef CONFIG_MENUKEY
262 (void) getc(); /* consume input */
267 } while (!abort && get_timer(ts) < 1000);
269 printf("\b\b\b%2d ", bootdelay);
274 #ifdef CONFIG_SILENT_CONSOLE
276 gd->flags &= ~GD_FLG_SILENT;
281 # endif /* CONFIG_AUTOBOOT_KEYED */
282 #endif /* CONFIG_BOOTDELAY */
285 * Runs the given boot command securely. Specifically:
286 * - Doesn't run the command with the shell (run_command or parse_string_outer),
287 * since that's a lot of code surface that an attacker might exploit.
288 * Because of this, we don't do any argument parsing--the secure boot command
289 * has to be a full-fledged u-boot command.
290 * - Doesn't check for keypresses before booting, since that could be a
291 * security hole; also disables Ctrl-C.
292 * - Doesn't allow the command to return.
294 * Upon any failures, this function will drop into an infinite loop after
295 * printing the error message to console.
298 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
299 static void secure_boot_cmd(char *cmd)
305 printf("## Error: Secure boot command not specified\n");
309 /* Disable Ctrl-C just in case some command is used that checks it. */
312 /* Find the command directly. */
313 cmdtp = find_cmd(cmd);
315 printf("## Error: \"%s\" not defined\n", cmd);
319 /* Run the command, forcing no flags and faking argc and argv. */
320 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
322 /* Shouldn't ever return from boot command. */
323 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
327 * Not a whole lot to do here. Rebooting won't help much, since we'll
328 * just end up right back here. Just loop.
333 static void process_fdt_options(const void *blob)
337 /* Add an env variable to point to a kernel payload, if available */
338 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
340 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
342 /* Add an env variable to point to a root disk, if available */
343 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
345 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
347 #endif /* CONFIG_OF_CONTROL */
350 /****************************************************************************/
352 void main_loop (void)
354 #ifndef CONFIG_SYS_HUSH_PARSER
355 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
360 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
363 #if defined(CONFIG_BOOTDELAY)
367 #ifdef CONFIG_PREBOOT
370 #ifdef CONFIG_BOOTCOUNT_LIMIT
371 unsigned long bootcount = 0;
372 unsigned long bootlimit = 0;
375 #endif /* CONFIG_BOOTCOUNT_LIMIT */
377 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
379 #if defined CONFIG_OF_CONTROL
380 set_working_fdt_addr((void *)gd->fdt_blob);
381 #endif /* CONFIG_OF_CONTROL */
383 #ifdef CONFIG_BOOTCOUNT_LIMIT
384 bootcount = bootcount_load();
386 bootcount_store (bootcount);
387 sprintf (bcs_set, "%lu", bootcount);
388 setenv ("bootcount", bcs_set);
389 bcs = getenv ("bootlimit");
390 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
391 #endif /* CONFIG_BOOTCOUNT_LIMIT */
393 #ifdef CONFIG_MODEM_SUPPORT
394 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
396 char *str = strdup(getenv("mdm_cmd"));
397 setenv ("preboot", str); /* set or delete definition */
400 mdm_init(); /* wait for modem connection */
402 #endif /* CONFIG_MODEM_SUPPORT */
404 #ifdef CONFIG_VERSION_VARIABLE
406 setenv ("ver", version_string); /* set version variable */
408 #endif /* CONFIG_VERSION_VARIABLE */
410 #ifdef CONFIG_SYS_HUSH_PARSER
411 u_boot_hush_start ();
414 #if defined(CONFIG_HUSH_INIT_VAR)
418 #ifdef CONFIG_PREBOOT
419 if ((p = getenv ("preboot")) != NULL) {
420 # ifdef CONFIG_AUTOBOOT_KEYED
421 int prev = disable_ctrlc(1); /* disable Control C checking */
424 run_command_list(p, -1, 0);
426 # ifdef CONFIG_AUTOBOOT_KEYED
427 disable_ctrlc(prev); /* restore Control C checking */
430 #endif /* CONFIG_PREBOOT */
432 #if defined(CONFIG_UPDATE_TFTP)
434 #endif /* CONFIG_UPDATE_TFTP */
436 #if defined(CONFIG_BOOTDELAY)
437 s = getenv ("bootdelay");
438 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
440 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
442 #if defined(CONFIG_MENU_SHOW)
443 bootdelay = menu_show(bootdelay);
445 # ifdef CONFIG_BOOT_RETRY_TIME
447 # endif /* CONFIG_BOOT_RETRY_TIME */
450 if (gd->flags & GD_FLG_POSTFAIL) {
451 s = getenv("failbootcmd");
454 #endif /* CONFIG_POST */
455 #ifdef CONFIG_BOOTCOUNT_LIMIT
456 if (bootlimit && (bootcount > bootlimit)) {
457 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
458 (unsigned)bootlimit);
459 s = getenv ("altbootcmd");
462 #endif /* CONFIG_BOOTCOUNT_LIMIT */
463 s = getenv ("bootcmd");
464 #ifdef CONFIG_OF_CONTROL
465 /* Allow the fdt to override the boot command */
466 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
470 process_fdt_options(gd->fdt_blob);
473 * If the bootsecure option was chosen, use secure_boot_cmd().
474 * Always use 'env' in this case, since bootsecure requres that the
475 * bootcmd was specified in the FDT too.
477 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
478 secure_boot_cmd(env);
480 #endif /* CONFIG_OF_CONTROL */
482 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
484 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
485 # ifdef CONFIG_AUTOBOOT_KEYED
486 int prev = disable_ctrlc(1); /* disable Control C checking */
489 run_command_list(s, -1, 0);
491 # ifdef CONFIG_AUTOBOOT_KEYED
492 disable_ctrlc(prev); /* restore Control C checking */
496 # ifdef CONFIG_MENUKEY
497 if (menukey == CONFIG_MENUKEY) {
498 s = getenv("menucmd");
500 run_command_list(s, -1, 0);
502 #endif /* CONFIG_MENUKEY */
503 #endif /* CONFIG_BOOTDELAY */
506 * Main Loop for Monitor Command Processing
508 #ifdef CONFIG_SYS_HUSH_PARSER
510 /* This point is never reached */
514 #ifdef CONFIG_BOOT_RETRY_TIME
516 /* Saw enough of a valid command to
517 * restart the timeout.
522 len = readline (CONFIG_SYS_PROMPT);
524 flag = 0; /* assume no special flags for now */
526 strcpy (lastcommand, console_buffer);
528 flag |= CMD_FLAG_REPEAT;
529 #ifdef CONFIG_BOOT_RETRY_TIME
530 else if (len == -2) {
531 /* -2 means timed out, retry autoboot
533 puts ("\nTimed out waiting for command\n");
534 # ifdef CONFIG_RESET_TO_RETRY
535 /* Reinit board to run initialization code again */
536 do_reset (NULL, 0, 0, NULL);
538 return; /* retry autoboot */
544 puts ("<INTERRUPT>\n");
546 rc = run_command(lastcommand, flag);
549 /* invalid command or not repeatable, forget it */
553 #endif /*CONFIG_SYS_HUSH_PARSER*/
556 #ifdef CONFIG_BOOT_RETRY_TIME
557 /***************************************************************************
558 * initialize command line timeout
560 void init_cmd_timeout(void)
562 char *s = getenv ("bootretry");
565 retry_time = (int)simple_strtol(s, NULL, 10);
567 retry_time = CONFIG_BOOT_RETRY_TIME;
569 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
570 retry_time = CONFIG_BOOT_RETRY_MIN;
573 /***************************************************************************
574 * reset command line timeout to retry_time seconds
576 void reset_cmd_timeout(void)
578 endtime = endtick(retry_time);
582 #ifdef CONFIG_CMDLINE_EDITING
585 * cmdline-editing related codes from vivi.
586 * Author: Janghoon Lyu <nandy@mizi.com>
589 #define putnstr(str,n) do { \
590 printf ("%.*s", (int)n, str); \
593 #define CTL_CH(c) ((c) - 'a' + 1)
594 #define CTL_BACKSPACE ('\b')
595 #define DEL ((char)255)
596 #define DEL7 ((char)127)
597 #define CREAD_HIST_CHAR ('!')
599 #define getcmd_putch(ch) putc(ch)
600 #define getcmd_getch() getc()
601 #define getcmd_cbeep() getcmd_putch('\a')
604 #define HIST_SIZE CONFIG_SYS_CBSIZE
607 static int hist_add_idx;
608 static int hist_cur = -1;
609 static unsigned hist_num;
611 static char *hist_list[HIST_MAX];
612 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
614 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
616 static void hist_init(void)
625 for (i = 0; i < HIST_MAX; i++) {
626 hist_list[i] = hist_lines[i];
627 hist_list[i][0] = '\0';
631 static void cread_add_to_hist(char *line)
633 strcpy(hist_list[hist_add_idx], line);
635 if (++hist_add_idx >= HIST_MAX)
638 if (hist_add_idx > hist_max)
639 hist_max = hist_add_idx;
644 static char* hist_prev(void)
656 if (hist_cur == hist_add_idx) {
660 ret = hist_list[hist_cur];
665 static char* hist_next(void)
672 if (hist_cur == hist_add_idx)
675 if (++hist_cur > hist_max)
678 if (hist_cur == hist_add_idx) {
681 ret = hist_list[hist_cur];
686 #ifndef CONFIG_CMDLINE_EDITING
687 static void cread_print_hist_list(void)
692 n = hist_num - hist_max;
694 i = hist_add_idx + 1;
698 if (i == hist_add_idx)
700 printf("%s\n", hist_list[i]);
705 #endif /* CONFIG_CMDLINE_EDITING */
707 #define BEGINNING_OF_LINE() { \
709 getcmd_putch(CTL_BACKSPACE); \
714 #define ERASE_TO_EOL() { \
715 if (num < eol_num) { \
716 printf("%*s", (int)(eol_num - num), ""); \
718 getcmd_putch(CTL_BACKSPACE); \
719 } while (--eol_num > num); \
723 #define REFRESH_TO_EOL() { \
724 if (num < eol_num) { \
725 wlen = eol_num - num; \
726 putnstr(buf + num, wlen); \
731 static void cread_add_char(char ichar, int insert, unsigned long *num,
732 unsigned long *eol_num, char *buf, unsigned long len)
737 if (insert || *num == *eol_num) {
738 if (*eol_num > len - 1) {
746 wlen = *eol_num - *num;
748 memmove(&buf[*num+1], &buf[*num], wlen-1);
752 putnstr(buf + *num, wlen);
755 getcmd_putch(CTL_BACKSPACE);
758 /* echo the character */
761 putnstr(buf + *num, wlen);
766 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
767 unsigned long *eol_num, char *buf, unsigned long len)
770 cread_add_char(*str, insert, num, eol_num, buf, len);
775 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
778 unsigned long num = 0;
779 unsigned long eol_num = 0;
785 int init_len = strlen(buf);
789 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
792 #ifdef CONFIG_BOOT_RETRY_TIME
793 while (!tstc()) { /* while no incoming data */
794 if (retry_time >= 0 && get_ticks() > endtime)
795 return (-2); /* timed out */
799 if (first && timeout) {
800 uint64_t etime = endtick(timeout);
802 while (!tstc()) { /* while no incoming data */
803 if (get_ticks() >= etime)
804 return -2; /* timed out */
810 ichar = getcmd_getch();
812 if ((ichar == '\n') || (ichar == '\r')) {
818 * handle standard linux xterm esc sequences for arrow key, etc.
823 esc_save[esc_len] = ichar;
826 cread_add_str(esc_save, esc_len, insert,
827 &num, &eol_num, buf, *len);
835 case 'D': /* <- key */
839 case 'C': /* -> key */
842 break; /* pass off to ^F handler */
843 case 'H': /* Home key */
846 break; /* pass off to ^A handler */
847 case 'A': /* up arrow */
850 break; /* pass off to ^P handler */
851 case 'B': /* down arrow */
854 break; /* pass off to ^N handler */
856 esc_save[esc_len++] = ichar;
857 cread_add_str(esc_save, esc_len, insert,
858 &num, &eol_num, buf, *len);
867 esc_save[esc_len] = ichar;
870 puts("impossible condition #876\n");
878 case CTL_CH('c'): /* ^C - break */
879 *buf = '\0'; /* discard input */
883 getcmd_putch(buf[num]);
889 getcmd_putch(CTL_BACKSPACE);
895 wlen = eol_num - num - 1;
897 memmove(&buf[num], &buf[num+1], wlen);
898 putnstr(buf + num, wlen);
903 getcmd_putch(CTL_BACKSPACE);
926 wlen = eol_num - num;
928 memmove(&buf[num], &buf[num+1], wlen);
929 getcmd_putch(CTL_BACKSPACE);
930 putnstr(buf + num, wlen);
933 getcmd_putch(CTL_BACKSPACE);
945 if (ichar == CTL_CH('p'))
955 /* nuke the current line */
959 /* erase to end of line */
962 /* copy new line into place and display */
964 eol_num = strlen(buf);
968 #ifdef CONFIG_AUTO_COMPLETE
972 /* do not autocomplete when in the middle */
979 col = strlen(prompt) + eol_num;
981 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
990 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
995 buf[eol_num] = '\0'; /* lose the newline */
997 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
998 cread_add_to_hist(buf);
999 hist_cur = hist_add_idx;
1004 #endif /* CONFIG_CMDLINE_EDITING */
1006 /****************************************************************************/
1009 * Prompt for input and read a line.
1010 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
1011 * time out when time goes past endtime (timebase time in ticks).
1012 * Return: number of read characters
1016 int readline (const char *const prompt)
1019 * If console_buffer isn't 0-length the user will be prompted to modify
1020 * it instead of entering it from scratch as desired.
1022 console_buffer[0] = '\0';
1024 return readline_into_buffer(prompt, console_buffer, 0);
1028 int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
1031 #ifdef CONFIG_CMDLINE_EDITING
1032 unsigned int len = CONFIG_SYS_CBSIZE;
1034 static int initted = 0;
1037 * History uses a global array which is not
1038 * writable until after relocation to RAM.
1039 * Revert to non-history version if still
1040 * running from flash.
1042 if (gd->flags & GD_FLG_RELOC) {
1051 rc = cread_line(prompt, p, &len, timeout);
1052 return rc < 0 ? rc : len;
1055 #endif /* CONFIG_CMDLINE_EDITING */
1057 int n = 0; /* buffer index */
1058 int plen = 0; /* prompt length */
1059 int col; /* output column cnt */
1064 plen = strlen (prompt);
1070 #ifdef CONFIG_BOOT_RETRY_TIME
1071 while (!tstc()) { /* while no incoming data */
1072 if (retry_time >= 0 && get_ticks() > endtime)
1073 return (-2); /* timed out */
1077 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1079 #ifdef CONFIG_SHOW_ACTIVITY
1088 * Special character handling
1091 case '\r': /* Enter */
1097 case '\0': /* nul */
1100 case 0x03: /* ^C - break */
1101 p_buf[0] = '\0'; /* discard input */
1104 case 0x15: /* ^U - erase line */
1105 while (col > plen) {
1113 case 0x17: /* ^W - erase word */
1114 p=delete_char(p_buf, p, &col, &n, plen);
1115 while ((n > 0) && (*p != ' ')) {
1116 p=delete_char(p_buf, p, &col, &n, plen);
1120 case 0x08: /* ^H - backspace */
1121 case 0x7F: /* DEL - backspace */
1122 p=delete_char(p_buf, p, &col, &n, plen);
1127 * Must be a normal character then
1129 if (n < CONFIG_SYS_CBSIZE-2) {
1130 if (c == '\t') { /* expand TABs */
1131 #ifdef CONFIG_AUTO_COMPLETE
1132 /* if auto completion triggered just continue */
1134 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1135 p = p_buf + n; /* reset */
1139 puts (tab_seq+(col&07));
1140 col += 8 - (col&07);
1145 * Echo input using puts() to force am
1146 * LCD flush if we are using an LCD
1155 } else { /* Buffer full */
1160 #ifdef CONFIG_CMDLINE_EDITING
1165 /****************************************************************************/
1167 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1175 if (*(--p) == '\t') { /* will retype the whole line */
1176 while (*colp > plen) {
1180 for (s=buffer; s<p; ++s) {
1182 puts (tab_seq+((*colp) & 07));
1183 *colp += 8 - ((*colp) & 07);
1197 /****************************************************************************/
1199 int parse_line (char *line, char *argv[])
1204 printf ("parse_line: \"%s\"\n", line);
1206 while (nargs < CONFIG_SYS_MAXARGS) {
1208 /* skip any white space */
1209 while (isblank(*line))
1212 if (*line == '\0') { /* end of line, no more args */
1215 printf ("parse_line: nargs=%d\n", nargs);
1220 argv[nargs++] = line; /* begin of argument string */
1222 /* find end of string */
1223 while (*line && !isblank(*line))
1226 if (*line == '\0') { /* end of line, no more args */
1229 printf ("parse_line: nargs=%d\n", nargs);
1234 *line++ = '\0'; /* terminate current arg */
1237 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1240 printf ("parse_line: nargs=%d\n", nargs);
1245 /****************************************************************************/
1247 #ifndef CONFIG_SYS_HUSH_PARSER
1248 static void process_macros (const char *input, char *output)
1251 const char *varname_start = NULL;
1252 int inputcnt = strlen (input);
1253 int outputcnt = CONFIG_SYS_CBSIZE;
1254 int state = 0; /* 0 = waiting for '$' */
1256 /* 1 = waiting for '(' or '{' */
1257 /* 2 = waiting for ')' or '}' */
1258 /* 3 = waiting for ''' */
1260 char *output_start = output;
1262 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1266 prev = '\0'; /* previous character */
1268 while (inputcnt && outputcnt) {
1273 /* remove one level of escape characters */
1274 if ((c == '\\') && (prev != '\\')) {
1275 if (inputcnt-- == 0)
1283 case 0: /* Waiting for (unescaped) $ */
1284 if ((c == '\'') && (prev != '\\')) {
1288 if ((c == '$') && (prev != '\\')) {
1295 case 1: /* Waiting for ( */
1296 if (c == '(' || c == '{') {
1298 varname_start = input;
1310 case 2: /* Waiting for ) */
1311 if (c == ')' || c == '}') {
1313 char envname[CONFIG_SYS_CBSIZE], *envval;
1314 int envcnt = input - varname_start - 1; /* Varname # of chars */
1316 /* Get the varname */
1317 for (i = 0; i < envcnt; i++) {
1318 envname[i] = varname_start[i];
1323 envval = getenv (envname);
1325 /* Copy into the line if it exists */
1327 while ((*envval) && outputcnt) {
1328 *(output++) = *(envval++);
1331 /* Look for another '$' */
1335 case 3: /* Waiting for ' */
1336 if ((c == '\'') && (prev != '\\')) {
1353 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1354 strlen (output_start), output_start);
1358 /****************************************************************************
1360 * 1 - command executed, repeatable
1361 * 0 - command executed but not repeatable, interrupted commands are
1362 * always considered not repeatable
1363 * -1 - not executed (unrecognized, bootd recursion or too many args)
1364 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1365 * considered unrecognized)
1369 * We must create a temporary copy of the command since the command we get
1370 * may be the result from getenv(), which returns a pointer directly to
1371 * the environment data, which may change magicly when the command we run
1372 * creates or modifies environment variables (like "bootp" does).
1374 static int builtin_run_command(const char *cmd, int flag)
1376 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1377 char *token; /* start of token in cmdbuf */
1378 char *sep; /* end of token (separator) in cmdbuf */
1379 char finaltoken[CONFIG_SYS_CBSIZE];
1381 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1387 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1388 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1392 clear_ctrlc(); /* forget any previous Control C */
1394 if (!cmd || !*cmd) {
1395 return -1; /* empty command */
1398 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1399 puts ("## Command too long!\n");
1403 strcpy (cmdbuf, cmd);
1405 /* Process separators and check for invalid
1406 * repeatable commands
1410 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1415 * Find separator, or string end
1416 * Allow simple escape of ';' by writing "\;"
1418 for (inquotes = 0, sep = str; *sep; sep++) {
1424 (*sep == ';') && /* separator */
1425 ( sep != str) && /* past string start */
1426 (*(sep-1) != '\\')) /* and NOT escaped */
1431 * Limit the token to data between separators
1435 str = sep + 1; /* start of command for next pass */
1439 str = sep; /* no more commands for next pass */
1441 printf ("token: \"%s\"\n", token);
1444 /* find macros in this token and replace them */
1445 process_macros (token, finaltoken);
1447 /* Extract arguments */
1448 if ((argc = parse_line (finaltoken, argv)) == 0) {
1449 rc = -1; /* no command at all */
1453 if (cmd_process(flag, argc, argv, &repeatable, NULL))
1456 /* Did the user stop this? */
1458 return -1; /* if stopped then not repeatable */
1461 return rc ? rc : repeatable;
1466 * Run a command using the selected parser.
1468 * @param cmd Command to run
1469 * @param flag Execution flags (CMD_FLAG_...)
1470 * @return 0 on success, or != 0 on error.
1472 int run_command(const char *cmd, int flag)
1474 #ifndef CONFIG_SYS_HUSH_PARSER
1476 * builtin_run_command can return 0 or 1 for success, so clean up
1479 if (builtin_run_command(cmd, flag) == -1)
1484 return parse_string_outer(cmd,
1485 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1489 #ifndef CONFIG_SYS_HUSH_PARSER
1491 * Execute a list of command separated by ; or \n using the built-in parser.
1493 * This function cannot take a const char * for the command, since if it
1494 * finds newlines in the string, it replaces them with \0.
1496 * @param cmd String containing list of commands
1497 * @param flag Execution flags (CMD_FLAG_...)
1498 * @return 0 on success, or != 0 on error.
1500 static int builtin_run_command_list(char *cmd, int flag)
1506 * Break into individual lines, and execute each line; terminate on
1511 if (*next == '\n') {
1513 /* run only non-empty commands */
1515 debug("** exec: \"%s\"\n", line);
1516 if (builtin_run_command(line, 0) < 0) {
1525 if (rcode == 0 && *line)
1526 rcode = (builtin_run_command(line, 0) >= 0);
1532 int run_command_list(const char *cmd, int len, int flag)
1535 char *buff = (char *)cmd; /* cast away const */
1540 #ifdef CONFIG_SYS_HUSH_PARSER
1541 /* hush will never change our string */
1544 /* the built-in parser will change our string if it sees \n */
1545 need_buff = strchr(cmd, '\n') != NULL;
1549 buff = malloc(len + 1);
1552 memcpy(buff, cmd, len);
1555 #ifdef CONFIG_SYS_HUSH_PARSER
1556 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1559 * This function will overwrite any \n it sees with a \0, which
1560 * is why it can't work with a const char *. Here we are making
1561 * using of internal knowledge of this function, to avoid always
1562 * doing a malloc() which is actually required only in a case that
1565 rcode = builtin_run_command_list(buff, flag);
1573 /****************************************************************************/
1575 #if defined(CONFIG_CMD_RUN)
1576 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1581 return CMD_RET_USAGE;
1583 for (i=1; i<argc; ++i) {
1586 if ((arg = getenv (argv[i])) == NULL) {
1587 printf ("## Error: \"%s\" not defined\n", argv[i]);
1591 if (run_command(arg, flag) != 0)