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 #if defined(CONFIG_UPDATE_TFTP)
61 int update_tftp (ulong addr);
62 #endif /* CONFIG_UPDATE_TFTP */
64 #define MAX_DELAY_STOP_STR 32
68 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
70 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
71 static const char erase_seq[] = "\b \b"; /* erase sequence */
72 static const char tab_seq[] = " "; /* used to expand TABs */
74 #ifdef CONFIG_BOOT_RETRY_TIME
75 static uint64_t endtime = 0; /* must be set, default is instant timeout */
76 static int retry_time = -1; /* -1 so can call readline before main_loop */
79 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
81 #ifndef CONFIG_BOOT_RETRY_MIN
82 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
85 #ifdef CONFIG_MODEM_SUPPORT
87 extern void mdm_init(void); /* defined in board.c */
90 /***************************************************************************
91 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
92 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
94 #if defined(CONFIG_BOOTDELAY)
95 # if defined(CONFIG_AUTOBOOT_KEYED)
99 int abortboot(int bootdelay)
102 uint64_t etime = endtick(bootdelay);
109 { str: getenv ("bootdelaykey"), retry: 1 },
110 { str: getenv ("bootdelaykey2"), retry: 1 },
111 { str: getenv ("bootstopkey"), retry: 0 },
112 { str: getenv ("bootstopkey2"), retry: 0 },
115 char presskey [MAX_DELAY_STOP_STR];
116 u_int presskey_len = 0;
117 u_int presskey_max = 0;
120 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
125 # ifdef CONFIG_AUTOBOOT_PROMPT
126 printf(CONFIG_AUTOBOOT_PROMPT);
129 # ifdef CONFIG_AUTOBOOT_DELAY_STR
130 if (delaykey[0].str == NULL)
131 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
133 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
134 if (delaykey[1].str == NULL)
135 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
137 # ifdef CONFIG_AUTOBOOT_STOP_STR
138 if (delaykey[2].str == NULL)
139 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
141 # ifdef CONFIG_AUTOBOOT_STOP_STR2
142 if (delaykey[3].str == NULL)
143 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
146 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
147 delaykey[i].len = delaykey[i].str == NULL ?
148 0 : strlen (delaykey[i].str);
149 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
150 MAX_DELAY_STOP_STR : delaykey[i].len;
152 presskey_max = presskey_max > delaykey[i].len ?
153 presskey_max : delaykey[i].len;
156 printf("%s key:<%s>\n",
157 delaykey[i].retry ? "delay" : "stop",
158 delaykey[i].str ? delaykey[i].str : "NULL");
162 /* In order to keep up with incoming data, check timeout only
167 if (presskey_len < presskey_max) {
168 presskey [presskey_len ++] = getc();
171 for (i = 0; i < presskey_max - 1; i ++)
172 presskey [i] = presskey [i + 1];
174 presskey [i] = getc();
178 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
179 if (delaykey[i].len > 0 &&
180 presskey_len >= delaykey[i].len &&
181 memcmp (presskey + presskey_len - delaykey[i].len,
183 delaykey[i].len) == 0) {
185 printf("got %skey\n",
186 delaykey[i].retry ? "delay" : "stop");
189 # ifdef CONFIG_BOOT_RETRY_TIME
190 /* don't retry auto boot */
191 if (! delaykey[i].retry)
197 } while (!abort && get_ticks() <= etime);
201 puts("key timeout\n");
204 #ifdef CONFIG_SILENT_CONSOLE
206 gd->flags &= ~GD_FLG_SILENT;
212 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
214 #ifdef CONFIG_MENUKEY
215 static int menukey = 0;
221 int abortboot(int bootdelay)
226 #ifdef CONFIG_MENUPROMPT
227 printf(CONFIG_MENUPROMPT);
230 printf("Hit any key to stop autoboot: %2d ", bootdelay);
233 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
235 * Check if key already pressed
236 * Don't check if bootdelay < 0
238 if (bootdelay >= 0) {
239 if (tstc()) { /* we got a key press */
240 (void) getc(); /* consume input */
242 abort = 1; /* don't auto boot */
247 while ((bootdelay > 0) && (!abort)) {
252 if (tstc()) { /* we got a key press */
253 abort = 1; /* don't auto boot */
254 bootdelay = 0; /* no more delay */
255 # ifdef CONFIG_MENUKEY
258 (void) getc(); /* consume input */
263 } while (!abort && get_timer(ts) < 1000);
265 printf("\b\b\b%2d ", bootdelay);
270 #ifdef CONFIG_SILENT_CONSOLE
272 gd->flags &= ~GD_FLG_SILENT;
277 # endif /* CONFIG_AUTOBOOT_KEYED */
278 #endif /* CONFIG_BOOTDELAY */
281 * Runs the given boot command securely. Specifically:
282 * - Doesn't run the command with the shell (run_command or parse_string_outer),
283 * since that's a lot of code surface that an attacker might exploit.
284 * Because of this, we don't do any argument parsing--the secure boot command
285 * has to be a full-fledged u-boot command.
286 * - Doesn't check for keypresses before booting, since that could be a
287 * security hole; also disables Ctrl-C.
288 * - Doesn't allow the command to return.
290 * Upon any failures, this function will drop into an infinite loop after
291 * printing the error message to console.
294 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
295 static void secure_boot_cmd(char *cmd)
301 printf("## Error: Secure boot command not specified\n");
305 /* Disable Ctrl-C just in case some command is used that checks it. */
308 /* Find the command directly. */
309 cmdtp = find_cmd(cmd);
311 printf("## Error: \"%s\" not defined\n", cmd);
315 /* Run the command, forcing no flags and faking argc and argv. */
316 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
318 /* Shouldn't ever return from boot command. */
319 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
323 * Not a whole lot to do here. Rebooting won't help much, since we'll
324 * just end up right back here. Just loop.
329 static void process_fdt_options(const void *blob)
333 /* Add an env variable to point to a kernel payload, if available */
334 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
336 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
338 /* Add an env variable to point to a root disk, if available */
339 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
341 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
343 #endif /* CONFIG_OF_CONTROL */
346 /****************************************************************************/
348 void main_loop (void)
350 #ifndef CONFIG_SYS_HUSH_PARSER
351 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
356 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
359 #if defined(CONFIG_BOOTDELAY)
363 #ifdef CONFIG_PREBOOT
366 #ifdef CONFIG_BOOTCOUNT_LIMIT
367 unsigned long bootcount = 0;
368 unsigned long bootlimit = 0;
371 #endif /* CONFIG_BOOTCOUNT_LIMIT */
373 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
375 #ifdef CONFIG_BOOTCOUNT_LIMIT
376 bootcount = bootcount_load();
378 bootcount_store (bootcount);
379 sprintf (bcs_set, "%lu", bootcount);
380 setenv ("bootcount", bcs_set);
381 bcs = getenv ("bootlimit");
382 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
383 #endif /* CONFIG_BOOTCOUNT_LIMIT */
385 #ifdef CONFIG_MODEM_SUPPORT
386 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
388 char *str = strdup(getenv("mdm_cmd"));
389 setenv ("preboot", str); /* set or delete definition */
392 mdm_init(); /* wait for modem connection */
394 #endif /* CONFIG_MODEM_SUPPORT */
396 #ifdef CONFIG_VERSION_VARIABLE
398 setenv ("ver", version_string); /* set version variable */
400 #endif /* CONFIG_VERSION_VARIABLE */
402 #ifdef CONFIG_SYS_HUSH_PARSER
403 u_boot_hush_start ();
406 #if defined(CONFIG_HUSH_INIT_VAR)
410 #ifdef CONFIG_PREBOOT
411 if ((p = getenv ("preboot")) != NULL) {
412 # ifdef CONFIG_AUTOBOOT_KEYED
413 int prev = disable_ctrlc(1); /* disable Control C checking */
416 run_command_list(p, -1, 0);
418 # ifdef CONFIG_AUTOBOOT_KEYED
419 disable_ctrlc(prev); /* restore Control C checking */
422 #endif /* CONFIG_PREBOOT */
424 #if defined(CONFIG_UPDATE_TFTP)
426 #endif /* CONFIG_UPDATE_TFTP */
428 #if defined(CONFIG_BOOTDELAY)
429 s = getenv ("bootdelay");
430 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
432 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
434 #if defined(CONFIG_MENU_SHOW)
435 bootdelay = menu_show(bootdelay);
437 # ifdef CONFIG_BOOT_RETRY_TIME
439 # endif /* CONFIG_BOOT_RETRY_TIME */
442 if (gd->flags & GD_FLG_POSTFAIL) {
443 s = getenv("failbootcmd");
446 #endif /* CONFIG_POST */
447 #ifdef CONFIG_BOOTCOUNT_LIMIT
448 if (bootlimit && (bootcount > bootlimit)) {
449 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
450 (unsigned)bootlimit);
451 s = getenv ("altbootcmd");
454 #endif /* CONFIG_BOOTCOUNT_LIMIT */
455 s = getenv ("bootcmd");
456 #ifdef CONFIG_OF_CONTROL
457 /* Allow the fdt to override the boot command */
458 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
462 process_fdt_options(gd->fdt_blob);
465 * If the bootsecure option was chosen, use secure_boot_cmd().
466 * Always use 'env' in this case, since bootsecure requres that the
467 * bootcmd was specified in the FDT too.
469 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
470 secure_boot_cmd(env);
472 #endif /* CONFIG_OF_CONTROL */
474 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
476 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
477 # ifdef CONFIG_AUTOBOOT_KEYED
478 int prev = disable_ctrlc(1); /* disable Control C checking */
481 run_command_list(s, -1, 0);
483 # ifdef CONFIG_AUTOBOOT_KEYED
484 disable_ctrlc(prev); /* restore Control C checking */
488 # ifdef CONFIG_MENUKEY
489 if (menukey == CONFIG_MENUKEY) {
490 s = getenv("menucmd");
492 run_command_list(s, -1, 0);
494 #endif /* CONFIG_MENUKEY */
495 #endif /* CONFIG_BOOTDELAY */
498 * Main Loop for Monitor Command Processing
500 #ifdef CONFIG_SYS_HUSH_PARSER
502 /* This point is never reached */
506 #ifdef CONFIG_BOOT_RETRY_TIME
508 /* Saw enough of a valid command to
509 * restart the timeout.
514 len = readline (CONFIG_SYS_PROMPT);
516 flag = 0; /* assume no special flags for now */
518 strcpy (lastcommand, console_buffer);
520 flag |= CMD_FLAG_REPEAT;
521 #ifdef CONFIG_BOOT_RETRY_TIME
522 else if (len == -2) {
523 /* -2 means timed out, retry autoboot
525 puts ("\nTimed out waiting for command\n");
526 # ifdef CONFIG_RESET_TO_RETRY
527 /* Reinit board to run initialization code again */
528 do_reset (NULL, 0, 0, NULL);
530 return; /* retry autoboot */
536 puts ("<INTERRUPT>\n");
538 rc = run_command(lastcommand, flag);
541 /* invalid command or not repeatable, forget it */
545 #endif /*CONFIG_SYS_HUSH_PARSER*/
548 #ifdef CONFIG_BOOT_RETRY_TIME
549 /***************************************************************************
550 * initialize command line timeout
552 void init_cmd_timeout(void)
554 char *s = getenv ("bootretry");
557 retry_time = (int)simple_strtol(s, NULL, 10);
559 retry_time = CONFIG_BOOT_RETRY_TIME;
561 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
562 retry_time = CONFIG_BOOT_RETRY_MIN;
565 /***************************************************************************
566 * reset command line timeout to retry_time seconds
568 void reset_cmd_timeout(void)
570 endtime = endtick(retry_time);
574 #ifdef CONFIG_CMDLINE_EDITING
577 * cmdline-editing related codes from vivi.
578 * Author: Janghoon Lyu <nandy@mizi.com>
581 #define putnstr(str,n) do { \
582 printf ("%.*s", (int)n, str); \
585 #define CTL_CH(c) ((c) - 'a' + 1)
586 #define CTL_BACKSPACE ('\b')
587 #define DEL ((char)255)
588 #define DEL7 ((char)127)
589 #define CREAD_HIST_CHAR ('!')
591 #define getcmd_putch(ch) putc(ch)
592 #define getcmd_getch() getc()
593 #define getcmd_cbeep() getcmd_putch('\a')
596 #define HIST_SIZE CONFIG_SYS_CBSIZE
599 static int hist_add_idx;
600 static int hist_cur = -1;
601 static unsigned hist_num;
603 static char *hist_list[HIST_MAX];
604 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
606 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
608 static void hist_init(void)
617 for (i = 0; i < HIST_MAX; i++) {
618 hist_list[i] = hist_lines[i];
619 hist_list[i][0] = '\0';
623 static void cread_add_to_hist(char *line)
625 strcpy(hist_list[hist_add_idx], line);
627 if (++hist_add_idx >= HIST_MAX)
630 if (hist_add_idx > hist_max)
631 hist_max = hist_add_idx;
636 static char* hist_prev(void)
648 if (hist_cur == hist_add_idx) {
652 ret = hist_list[hist_cur];
657 static char* hist_next(void)
664 if (hist_cur == hist_add_idx)
667 if (++hist_cur > hist_max)
670 if (hist_cur == hist_add_idx) {
673 ret = hist_list[hist_cur];
678 #ifndef CONFIG_CMDLINE_EDITING
679 static void cread_print_hist_list(void)
684 n = hist_num - hist_max;
686 i = hist_add_idx + 1;
690 if (i == hist_add_idx)
692 printf("%s\n", hist_list[i]);
697 #endif /* CONFIG_CMDLINE_EDITING */
699 #define BEGINNING_OF_LINE() { \
701 getcmd_putch(CTL_BACKSPACE); \
706 #define ERASE_TO_EOL() { \
707 if (num < eol_num) { \
708 printf("%*s", (int)(eol_num - num), ""); \
710 getcmd_putch(CTL_BACKSPACE); \
711 } while (--eol_num > num); \
715 #define REFRESH_TO_EOL() { \
716 if (num < eol_num) { \
717 wlen = eol_num - num; \
718 putnstr(buf + num, wlen); \
723 static void cread_add_char(char ichar, int insert, unsigned long *num,
724 unsigned long *eol_num, char *buf, unsigned long len)
729 if (insert || *num == *eol_num) {
730 if (*eol_num > len - 1) {
738 wlen = *eol_num - *num;
740 memmove(&buf[*num+1], &buf[*num], wlen-1);
744 putnstr(buf + *num, wlen);
747 getcmd_putch(CTL_BACKSPACE);
750 /* echo the character */
753 putnstr(buf + *num, wlen);
758 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
759 unsigned long *eol_num, char *buf, unsigned long len)
762 cread_add_char(*str, insert, num, eol_num, buf, len);
767 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
770 unsigned long num = 0;
771 unsigned long eol_num = 0;
777 int init_len = strlen(buf);
781 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
784 #ifdef CONFIG_BOOT_RETRY_TIME
785 while (!tstc()) { /* while no incoming data */
786 if (retry_time >= 0 && get_ticks() > endtime)
787 return (-2); /* timed out */
791 if (first && timeout) {
792 uint64_t etime = endtick(timeout);
794 while (!tstc()) { /* while no incoming data */
795 if (get_ticks() >= etime)
796 return -2; /* timed out */
802 ichar = getcmd_getch();
804 if ((ichar == '\n') || (ichar == '\r')) {
810 * handle standard linux xterm esc sequences for arrow key, etc.
815 esc_save[esc_len] = ichar;
818 cread_add_str(esc_save, esc_len, insert,
819 &num, &eol_num, buf, *len);
827 case 'D': /* <- key */
831 case 'C': /* -> key */
834 break; /* pass off to ^F handler */
835 case 'H': /* Home key */
838 break; /* pass off to ^A handler */
839 case 'A': /* up arrow */
842 break; /* pass off to ^P handler */
843 case 'B': /* down arrow */
846 break; /* pass off to ^N handler */
848 esc_save[esc_len++] = ichar;
849 cread_add_str(esc_save, esc_len, insert,
850 &num, &eol_num, buf, *len);
859 esc_save[esc_len] = ichar;
862 puts("impossible condition #876\n");
870 case CTL_CH('c'): /* ^C - break */
871 *buf = '\0'; /* discard input */
875 getcmd_putch(buf[num]);
881 getcmd_putch(CTL_BACKSPACE);
887 wlen = eol_num - num - 1;
889 memmove(&buf[num], &buf[num+1], wlen);
890 putnstr(buf + num, wlen);
895 getcmd_putch(CTL_BACKSPACE);
918 wlen = eol_num - num;
920 memmove(&buf[num], &buf[num+1], wlen);
921 getcmd_putch(CTL_BACKSPACE);
922 putnstr(buf + num, wlen);
925 getcmd_putch(CTL_BACKSPACE);
937 if (ichar == CTL_CH('p'))
947 /* nuke the current line */
951 /* erase to end of line */
954 /* copy new line into place and display */
956 eol_num = strlen(buf);
960 #ifdef CONFIG_AUTO_COMPLETE
964 /* do not autocomplete when in the middle */
971 col = strlen(prompt) + eol_num;
973 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
982 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
987 buf[eol_num] = '\0'; /* lose the newline */
989 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
990 cread_add_to_hist(buf);
991 hist_cur = hist_add_idx;
996 #endif /* CONFIG_CMDLINE_EDITING */
998 /****************************************************************************/
1001 * Prompt for input and read a line.
1002 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
1003 * time out when time goes past endtime (timebase time in ticks).
1004 * Return: number of read characters
1008 int readline (const char *const prompt)
1011 * If console_buffer isn't 0-length the user will be prompted to modify
1012 * it instead of entering it from scratch as desired.
1014 console_buffer[0] = '\0';
1016 return readline_into_buffer(prompt, console_buffer, 0);
1020 int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
1023 #ifdef CONFIG_CMDLINE_EDITING
1024 unsigned int len = CONFIG_SYS_CBSIZE;
1026 static int initted = 0;
1029 * History uses a global array which is not
1030 * writable until after relocation to RAM.
1031 * Revert to non-history version if still
1032 * running from flash.
1034 if (gd->flags & GD_FLG_RELOC) {
1043 rc = cread_line(prompt, p, &len, timeout);
1044 return rc < 0 ? rc : len;
1047 #endif /* CONFIG_CMDLINE_EDITING */
1049 int n = 0; /* buffer index */
1050 int plen = 0; /* prompt length */
1051 int col; /* output column cnt */
1056 plen = strlen (prompt);
1062 #ifdef CONFIG_BOOT_RETRY_TIME
1063 while (!tstc()) { /* while no incoming data */
1064 if (retry_time >= 0 && get_ticks() > endtime)
1065 return (-2); /* timed out */
1069 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1071 #ifdef CONFIG_SHOW_ACTIVITY
1080 * Special character handling
1083 case '\r': /* Enter */
1089 case '\0': /* nul */
1092 case 0x03: /* ^C - break */
1093 p_buf[0] = '\0'; /* discard input */
1096 case 0x15: /* ^U - erase line */
1097 while (col > plen) {
1105 case 0x17: /* ^W - erase word */
1106 p=delete_char(p_buf, p, &col, &n, plen);
1107 while ((n > 0) && (*p != ' ')) {
1108 p=delete_char(p_buf, p, &col, &n, plen);
1112 case 0x08: /* ^H - backspace */
1113 case 0x7F: /* DEL - backspace */
1114 p=delete_char(p_buf, p, &col, &n, plen);
1119 * Must be a normal character then
1121 if (n < CONFIG_SYS_CBSIZE-2) {
1122 if (c == '\t') { /* expand TABs */
1123 #ifdef CONFIG_AUTO_COMPLETE
1124 /* if auto completion triggered just continue */
1126 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1127 p = p_buf + n; /* reset */
1131 puts (tab_seq+(col&07));
1132 col += 8 - (col&07);
1137 * Echo input using puts() to force am
1138 * LCD flush if we are using an LCD
1147 } else { /* Buffer full */
1152 #ifdef CONFIG_CMDLINE_EDITING
1157 /****************************************************************************/
1159 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1167 if (*(--p) == '\t') { /* will retype the whole line */
1168 while (*colp > plen) {
1172 for (s=buffer; s<p; ++s) {
1174 puts (tab_seq+((*colp) & 07));
1175 *colp += 8 - ((*colp) & 07);
1189 /****************************************************************************/
1191 int parse_line (char *line, char *argv[])
1196 printf ("parse_line: \"%s\"\n", line);
1198 while (nargs < CONFIG_SYS_MAXARGS) {
1200 /* skip any white space */
1201 while (isblank(*line))
1204 if (*line == '\0') { /* end of line, no more args */
1207 printf ("parse_line: nargs=%d\n", nargs);
1212 argv[nargs++] = line; /* begin of argument string */
1214 /* find end of string */
1215 while (*line && !isblank(*line))
1218 if (*line == '\0') { /* end of line, no more args */
1221 printf ("parse_line: nargs=%d\n", nargs);
1226 *line++ = '\0'; /* terminate current arg */
1229 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1232 printf ("parse_line: nargs=%d\n", nargs);
1237 /****************************************************************************/
1239 #ifndef CONFIG_SYS_HUSH_PARSER
1240 static void process_macros (const char *input, char *output)
1243 const char *varname_start = NULL;
1244 int inputcnt = strlen (input);
1245 int outputcnt = CONFIG_SYS_CBSIZE;
1246 int state = 0; /* 0 = waiting for '$' */
1248 /* 1 = waiting for '(' or '{' */
1249 /* 2 = waiting for ')' or '}' */
1250 /* 3 = waiting for ''' */
1252 char *output_start = output;
1254 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1258 prev = '\0'; /* previous character */
1260 while (inputcnt && outputcnt) {
1265 /* remove one level of escape characters */
1266 if ((c == '\\') && (prev != '\\')) {
1267 if (inputcnt-- == 0)
1275 case 0: /* Waiting for (unescaped) $ */
1276 if ((c == '\'') && (prev != '\\')) {
1280 if ((c == '$') && (prev != '\\')) {
1287 case 1: /* Waiting for ( */
1288 if (c == '(' || c == '{') {
1290 varname_start = input;
1302 case 2: /* Waiting for ) */
1303 if (c == ')' || c == '}') {
1305 char envname[CONFIG_SYS_CBSIZE], *envval;
1306 int envcnt = input - varname_start - 1; /* Varname # of chars */
1308 /* Get the varname */
1309 for (i = 0; i < envcnt; i++) {
1310 envname[i] = varname_start[i];
1315 envval = getenv (envname);
1317 /* Copy into the line if it exists */
1319 while ((*envval) && outputcnt) {
1320 *(output++) = *(envval++);
1323 /* Look for another '$' */
1327 case 3: /* Waiting for ' */
1328 if ((c == '\'') && (prev != '\\')) {
1345 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1346 strlen (output_start), output_start);
1350 /****************************************************************************
1352 * 1 - command executed, repeatable
1353 * 0 - command executed but not repeatable, interrupted commands are
1354 * always considered not repeatable
1355 * -1 - not executed (unrecognized, bootd recursion or too many args)
1356 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1357 * considered unrecognized)
1361 * We must create a temporary copy of the command since the command we get
1362 * may be the result from getenv(), which returns a pointer directly to
1363 * the environment data, which may change magicly when the command we run
1364 * creates or modifies environment variables (like "bootp" does).
1366 static int builtin_run_command(const char *cmd, int flag)
1368 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1369 char *token; /* start of token in cmdbuf */
1370 char *sep; /* end of token (separator) in cmdbuf */
1371 char finaltoken[CONFIG_SYS_CBSIZE];
1373 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1379 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1380 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1384 clear_ctrlc(); /* forget any previous Control C */
1386 if (!cmd || !*cmd) {
1387 return -1; /* empty command */
1390 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1391 puts ("## Command too long!\n");
1395 strcpy (cmdbuf, cmd);
1397 /* Process separators and check for invalid
1398 * repeatable commands
1402 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1407 * Find separator, or string end
1408 * Allow simple escape of ';' by writing "\;"
1410 for (inquotes = 0, sep = str; *sep; sep++) {
1416 (*sep == ';') && /* separator */
1417 ( sep != str) && /* past string start */
1418 (*(sep-1) != '\\')) /* and NOT escaped */
1423 * Limit the token to data between separators
1427 str = sep + 1; /* start of command for next pass */
1431 str = sep; /* no more commands for next pass */
1433 printf ("token: \"%s\"\n", token);
1436 /* find macros in this token and replace them */
1437 process_macros (token, finaltoken);
1439 /* Extract arguments */
1440 if ((argc = parse_line (finaltoken, argv)) == 0) {
1441 rc = -1; /* no command at all */
1445 if (cmd_process(flag, argc, argv, &repeatable, NULL))
1448 /* Did the user stop this? */
1450 return -1; /* if stopped then not repeatable */
1453 return rc ? rc : repeatable;
1458 * Run a command using the selected parser.
1460 * @param cmd Command to run
1461 * @param flag Execution flags (CMD_FLAG_...)
1462 * @return 0 on success, or != 0 on error.
1464 int run_command(const char *cmd, int flag)
1466 #ifndef CONFIG_SYS_HUSH_PARSER
1468 * builtin_run_command can return 0 or 1 for success, so clean up
1471 if (builtin_run_command(cmd, flag) == -1)
1476 return parse_string_outer(cmd,
1477 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1481 #ifndef CONFIG_SYS_HUSH_PARSER
1483 * Execute a list of command separated by ; or \n using the built-in parser.
1485 * This function cannot take a const char * for the command, since if it
1486 * finds newlines in the string, it replaces them with \0.
1488 * @param cmd String containing list of commands
1489 * @param flag Execution flags (CMD_FLAG_...)
1490 * @return 0 on success, or != 0 on error.
1492 static int builtin_run_command_list(char *cmd, int flag)
1498 * Break into individual lines, and execute each line; terminate on
1503 if (*next == '\n') {
1505 /* run only non-empty commands */
1507 debug("** exec: \"%s\"\n", line);
1508 if (builtin_run_command(line, 0) < 0) {
1517 if (rcode == 0 && *line)
1518 rcode = (builtin_run_command(line, 0) >= 0);
1524 int run_command_list(const char *cmd, int len, int flag)
1527 char *buff = (char *)cmd; /* cast away const */
1532 #ifdef CONFIG_SYS_HUSH_PARSER
1533 /* hush will never change our string */
1536 /* the built-in parser will change our string if it sees \n */
1537 need_buff = strchr(cmd, '\n') != NULL;
1541 buff = malloc(len + 1);
1544 memcpy(buff, cmd, len);
1547 #ifdef CONFIG_SYS_HUSH_PARSER
1548 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1551 * This function will overwrite any \n it sees with a \0, which
1552 * is why it can't work with a const char *. Here we are making
1553 * using of internal knowledge of this function, to avoid always
1554 * doing a malloc() which is actually required only in a case that
1557 rcode = builtin_run_command_list(buff, flag);
1565 /****************************************************************************/
1567 #if defined(CONFIG_CMD_RUN)
1568 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1573 return CMD_RET_USAGE;
1575 for (i=1; i<argc; ++i) {
1578 if ((arg = getenv (argv[i])) == NULL) {
1579 printf ("## Error: \"%s\" not defined\n", argv[i]);
1583 if (run_command(arg, flag) != 0)