]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/main.c
[new uImage] Add fit_parse_conf() and fit_parse_subimage() routines
[people/ms/u-boot.git] / common / main.c
CommitLineData
c609719b
WD
1/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
501090aa
WD
5 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
c609719b
WD
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307 USA
26 */
27
a6c7ad2f
WD
28/* #define DEBUG */
29
c609719b
WD
30#include <common.h>
31#include <watchdog.h>
32#include <command.h>
d9631ecf
WD
33#ifdef CONFIG_MODEM_SUPPORT
34#include <malloc.h> /* for free() prototype */
35#endif
c609719b
WD
36
37#ifdef CFG_HUSH_PARSER
38#include <hush.h>
39#endif
40
bdccc4fe
WD
41#include <post.h>
42
d87080b7
WD
43#ifdef CONFIG_SILENT_CONSOLE
44DECLARE_GLOBAL_DATA_PTR;
45#endif
46
fad63407
HS
47/*
48 * Board-specific Platform code can reimplement show_boot_progress () if needed
49 */
50void inline __show_boot_progress (int val) {}
51void inline show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
52
8bde7f77
WD
53#if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
54extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
55#endif
56
57extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
58
59
c609719b
WD
60#define MAX_DELAY_STOP_STR 32
61
c609719b
WD
62#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
63static int abortboot(int);
64#endif
65
66#undef DEBUG_PARSER
67
68char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
69
3ca9122f
SR
70#ifndef CONFIG_CMDLINE_EDITING
71static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
c609719b
WD
72static char erase_seq[] = "\b \b"; /* erase sequence */
73static char tab_seq[] = " "; /* used to expand TABs */
3ca9122f 74#endif /* CONFIG_CMDLINE_EDITING */
c609719b
WD
75
76#ifdef CONFIG_BOOT_RETRY_TIME
77static uint64_t endtime = 0; /* must be set, default is instant timeout */
78static int retry_time = -1; /* -1 so can call readline before main_loop */
79#endif
80
81#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
82
83#ifndef CONFIG_BOOT_RETRY_MIN
84#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
85#endif
86
87#ifdef CONFIG_MODEM_SUPPORT
88int do_mdm_init = 0;
89extern void mdm_init(void); /* defined in board.c */
90#endif
91
92/***************************************************************************
93 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
94 * returns: 0 - no key string, allow autoboot
95 * 1 - got key string, abort
96 */
97#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
98# if defined(CONFIG_AUTOBOOT_KEYED)
99static __inline__ int abortboot(int bootdelay)
100{
101 int abort = 0;
102 uint64_t etime = endtick(bootdelay);
19973b6a 103 struct {
c609719b
WD
104 char* str;
105 u_int len;
106 int retry;
107 }
19973b6a 108 delaykey [] = {
c609719b
WD
109 { str: getenv ("bootdelaykey"), retry: 1 },
110 { str: getenv ("bootdelaykey2"), retry: 1 },
111 { str: getenv ("bootstopkey"), retry: 0 },
112 { str: getenv ("bootstopkey2"), retry: 0 },
113 };
114
115 char presskey [MAX_DELAY_STOP_STR];
116 u_int presskey_len = 0;
117 u_int presskey_max = 0;
118 u_int i;
119
120# ifdef CONFIG_AUTOBOOT_PROMPT
ada4d400 121 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
c609719b
WD
122# endif
123
124# ifdef CONFIG_AUTOBOOT_DELAY_STR
125 if (delaykey[0].str == NULL)
126 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
127# endif
128# ifdef CONFIG_AUTOBOOT_DELAY_STR2
129 if (delaykey[1].str == NULL)
130 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
131# endif
132# ifdef CONFIG_AUTOBOOT_STOP_STR
133 if (delaykey[2].str == NULL)
134 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
135# endif
136# ifdef CONFIG_AUTOBOOT_STOP_STR2
137 if (delaykey[3].str == NULL)
138 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
139# endif
140
141 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
142 delaykey[i].len = delaykey[i].str == NULL ?
143 0 : strlen (delaykey[i].str);
144 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
145 MAX_DELAY_STOP_STR : delaykey[i].len;
146
147 presskey_max = presskey_max > delaykey[i].len ?
148 presskey_max : delaykey[i].len;
149
150# if DEBUG_BOOTKEYS
151 printf("%s key:<%s>\n",
152 delaykey[i].retry ? "delay" : "stop",
153 delaykey[i].str ? delaykey[i].str : "NULL");
154# endif
155 }
156
157 /* In order to keep up with incoming data, check timeout only
158 * when catch up.
159 */
160 while (!abort && get_ticks() <= etime) {
161 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
162 if (delaykey[i].len > 0 &&
163 presskey_len >= delaykey[i].len &&
164 memcmp (presskey + presskey_len - delaykey[i].len,
8bde7f77 165 delaykey[i].str,
c609719b
WD
166 delaykey[i].len) == 0) {
167# if DEBUG_BOOTKEYS
168 printf("got %skey\n",
169 delaykey[i].retry ? "delay" : "stop");
170# endif
171
172# ifdef CONFIG_BOOT_RETRY_TIME
173 /* don't retry auto boot */
174 if (! delaykey[i].retry)
175 retry_time = -1;
176# endif
177 abort = 1;
178 }
179 }
180
181 if (tstc()) {
182 if (presskey_len < presskey_max) {
183 presskey [presskey_len ++] = getc();
184 }
185 else {
186 for (i = 0; i < presskey_max - 1; i ++)
187 presskey [i] = presskey [i + 1];
188
189 presskey [i] = getc();
190 }
191 }
192 }
193# if DEBUG_BOOTKEYS
194 if (!abort)
ada4d400 195 puts("key timeout\n");
c609719b
WD
196# endif
197
8cb8143e 198#ifdef CONFIG_SILENT_CONSOLE
4ec5bd55
LM
199 if (abort)
200 gd->flags &= ~GD_FLG_SILENT;
8cb8143e 201#endif
202
c609719b
WD
203 return abort;
204}
205
206# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
207
c7de829c
WD
208#ifdef CONFIG_MENUKEY
209static int menukey = 0;
210#endif
211
c609719b
WD
212static __inline__ int abortboot(int bootdelay)
213{
214 int abort = 0;
215
c7de829c
WD
216#ifdef CONFIG_MENUPROMPT
217 printf(CONFIG_MENUPROMPT, bootdelay);
218#else
c609719b 219 printf("Hit any key to stop autoboot: %2d ", bootdelay);
c7de829c 220#endif
c609719b
WD
221
222#if defined CONFIG_ZERO_BOOTDELAY_CHECK
8bde7f77
WD
223 /*
224 * Check if key already pressed
225 * Don't check if bootdelay < 0
226 */
c609719b
WD
227 if (bootdelay >= 0) {
228 if (tstc()) { /* we got a key press */
229 (void) getc(); /* consume input */
4b9206ed 230 puts ("\b\b\b 0");
4ec5bd55 231 abort = 1; /* don't auto boot */
c609719b 232 }
8bde7f77 233 }
c609719b
WD
234#endif
235
f72da340 236 while ((bootdelay > 0) && (!abort)) {
c609719b
WD
237 int i;
238
239 --bootdelay;
240 /* delay 100 * 10ms */
241 for (i=0; !abort && i<100; ++i) {
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
c7de829c
WD
245# ifdef CONFIG_MENUKEY
246 menukey = getc();
247# else
c609719b 248 (void) getc(); /* consume input */
c7de829c 249# endif
c609719b
WD
250 break;
251 }
ada4d400 252 udelay(10000);
c609719b
WD
253 }
254
ada4d400 255 printf("\b\b\b%2d ", bootdelay);
c609719b
WD
256 }
257
ada4d400 258 putc('\n');
c609719b 259
f72da340 260#ifdef CONFIG_SILENT_CONSOLE
4ec5bd55
LM
261 if (abort)
262 gd->flags &= ~GD_FLG_SILENT;
f72da340
WD
263#endif
264
c609719b
WD
265 return abort;
266}
267# endif /* CONFIG_AUTOBOOT_KEYED */
268#endif /* CONFIG_BOOTDELAY >= 0 */
269
270/****************************************************************************/
271
272void main_loop (void)
273{
274#ifndef CFG_HUSH_PARSER
275 static char lastcommand[CFG_CBSIZE] = { 0, };
276 int len;
277 int rc = 1;
278 int flag;
279#endif
280
281#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
282 char *s;
283 int bootdelay;
284#endif
285#ifdef CONFIG_PREBOOT
286 char *p;
287#endif
bdccc4fe
WD
288#ifdef CONFIG_BOOTCOUNT_LIMIT
289 unsigned long bootcount = 0;
290 unsigned long bootlimit = 0;
291 char *bcs;
292 char bcs_set[16];
293#endif /* CONFIG_BOOTCOUNT_LIMIT */
c609719b
WD
294
295#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
296 ulong bmp = 0; /* default bitmap */
297 extern int trab_vfd (ulong bitmap);
298
299#ifdef CONFIG_MODEM_SUPPORT
300 if (do_mdm_init)
301 bmp = 1; /* alternate bitmap */
302#endif
303 trab_vfd (bmp);
304#endif /* CONFIG_VFD && VFD_TEST_LOGO */
305
bdccc4fe
WD
306#ifdef CONFIG_BOOTCOUNT_LIMIT
307 bootcount = bootcount_load();
308 bootcount++;
309 bootcount_store (bootcount);
310 sprintf (bcs_set, "%lu", bootcount);
311 setenv ("bootcount", bcs_set);
312 bcs = getenv ("bootlimit");
313 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
314#endif /* CONFIG_BOOTCOUNT_LIMIT */
315
c609719b
WD
316#ifdef CONFIG_MODEM_SUPPORT
317 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
318 if (do_mdm_init) {
77ddac94 319 char *str = strdup(getenv("mdm_cmd"));
c609719b
WD
320 setenv ("preboot", str); /* set or delete definition */
321 if (str != NULL)
322 free (str);
323 mdm_init(); /* wait for modem connection */
324 }
325#endif /* CONFIG_MODEM_SUPPORT */
326
0587597c
SR
327#ifdef CONFIG_VERSION_VARIABLE
328 {
329 extern char version_string[];
0587597c 330
155cb010 331 setenv ("ver", version_string); /* set version variable */
0587597c
SR
332 }
333#endif /* CONFIG_VERSION_VARIABLE */
334
c609719b
WD
335#ifdef CFG_HUSH_PARSER
336 u_boot_hush_start ();
337#endif
338
04a85b3b
WD
339#ifdef CONFIG_AUTO_COMPLETE
340 install_auto_complete();
341#endif
342
c609719b
WD
343#ifdef CONFIG_PREBOOT
344 if ((p = getenv ("preboot")) != NULL) {
345# ifdef CONFIG_AUTOBOOT_KEYED
346 int prev = disable_ctrlc(1); /* disable Control C checking */
347# endif
348
349# ifndef CFG_HUSH_PARSER
350 run_command (p, 0);
351# else
352 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
353 FLAG_EXIT_FROM_LOOP);
354# endif
355
356# ifdef CONFIG_AUTOBOOT_KEYED
357 disable_ctrlc(prev); /* restore Control C checking */
358# endif
359 }
360#endif /* CONFIG_PREBOOT */
361
362#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
363 s = getenv ("bootdelay");
364 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
365
a6c7ad2f 366 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
c609719b
WD
367
368# ifdef CONFIG_BOOT_RETRY_TIME
6dd652fa 369 init_cmd_timeout ();
c609719b
WD
370# endif /* CONFIG_BOOT_RETRY_TIME */
371
bdccc4fe
WD
372#ifdef CONFIG_BOOTCOUNT_LIMIT
373 if (bootlimit && (bootcount > bootlimit)) {
374 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
375 (unsigned)bootlimit);
376 s = getenv ("altbootcmd");
377 }
378 else
379#endif /* CONFIG_BOOTCOUNT_LIMIT */
380 s = getenv ("bootcmd");
a6c7ad2f
WD
381
382 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
383
c609719b
WD
384 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
385# ifdef CONFIG_AUTOBOOT_KEYED
386 int prev = disable_ctrlc(1); /* disable Control C checking */
387# endif
388
389# ifndef CFG_HUSH_PARSER
390 run_command (s, 0);
391# else
392 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
393 FLAG_EXIT_FROM_LOOP);
394# endif
395
396# ifdef CONFIG_AUTOBOOT_KEYED
397 disable_ctrlc(prev); /* restore Control C checking */
398# endif
399 }
c7de829c
WD
400
401# ifdef CONFIG_MENUKEY
a6c7ad2f 402 if (menukey == CONFIG_MENUKEY) {
c7de829c 403 s = getenv("menucmd");
a6c7ad2f 404 if (s) {
c7de829c 405# ifndef CFG_HUSH_PARSER
6225c5db 406 run_command (s, 0);
c7de829c
WD
407# else
408 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
409 FLAG_EXIT_FROM_LOOP);
410# endif
411 }
412 }
413#endif /* CONFIG_MENUKEY */
c609719b
WD
414#endif /* CONFIG_BOOTDELAY */
415
c7de829c
WD
416#ifdef CONFIG_AMIGAONEG3SE
417 {
418 extern void video_banner(void);
419 video_banner();
420 }
421#endif
422
c609719b
WD
423 /*
424 * Main Loop for Monitor Command Processing
425 */
426#ifdef CFG_HUSH_PARSER
427 parse_file_outer();
428 /* This point is never reached */
429 for (;;);
430#else
431 for (;;) {
432#ifdef CONFIG_BOOT_RETRY_TIME
433 if (rc >= 0) {
434 /* Saw enough of a valid command to
435 * restart the timeout.
436 */
437 reset_cmd_timeout();
438 }
439#endif
440 len = readline (CFG_PROMPT);
441
442 flag = 0; /* assume no special flags for now */
443 if (len > 0)
444 strcpy (lastcommand, console_buffer);
445 else if (len == 0)
446 flag |= CMD_FLAG_REPEAT;
447#ifdef CONFIG_BOOT_RETRY_TIME
448 else if (len == -2) {
449 /* -2 means timed out, retry autoboot
450 */
4b9206ed 451 puts ("\nTimed out waiting for command\n");
c609719b
WD
452# ifdef CONFIG_RESET_TO_RETRY
453 /* Reinit board to run initialization code again */
454 do_reset (NULL, 0, 0, NULL);
455# else
456 return; /* retry autoboot */
457# endif
458 }
459#endif
460
461 if (len == -1)
4b9206ed 462 puts ("<INTERRUPT>\n");
c609719b
WD
463 else
464 rc = run_command (lastcommand, flag);
465
466 if (rc <= 0) {
467 /* invalid command or not repeatable, forget it */
468 lastcommand[0] = 0;
469 }
470 }
471#endif /*CFG_HUSH_PARSER*/
472}
473
6dd652fa
WD
474#ifdef CONFIG_BOOT_RETRY_TIME
475/***************************************************************************
19973b6a 476 * initialize command line timeout
6dd652fa
WD
477 */
478void init_cmd_timeout(void)
479{
480 char *s = getenv ("bootretry");
481
482 if (s != NULL)
b028f715 483 retry_time = (int)simple_strtol(s, NULL, 10);
6dd652fa
WD
484 else
485 retry_time = CONFIG_BOOT_RETRY_TIME;
486
487 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
488 retry_time = CONFIG_BOOT_RETRY_MIN;
489}
490
c609719b
WD
491/***************************************************************************
492 * reset command line timeout to retry_time seconds
493 */
c609719b
WD
494void reset_cmd_timeout(void)
495{
496 endtime = endtick(retry_time);
497}
498#endif
499
501090aa
WD
500#ifdef CONFIG_CMDLINE_EDITING
501
502/*
503 * cmdline-editing related codes from vivi.
504 * Author: Janghoon Lyu <nandy@mizi.com>
505 */
506
501090aa
WD
507#define putnstr(str,n) do { \
508 printf ("%.*s", n, str); \
509 } while (0)
501090aa
WD
510
511#define CTL_CH(c) ((c) - 'a' + 1)
512
513#define MAX_CMDBUF_SIZE 256
514
515#define CTL_BACKSPACE ('\b')
516#define DEL ((char)255)
517#define DEL7 ((char)127)
518#define CREAD_HIST_CHAR ('!')
519
520#define getcmd_putch(ch) putc(ch)
521#define getcmd_getch() getc()
522#define getcmd_cbeep() getcmd_putch('\a')
523
524#define HIST_MAX 20
525#define HIST_SIZE MAX_CMDBUF_SIZE
526
527static int hist_max = 0;
528static int hist_add_idx = 0;
529static int hist_cur = -1;
530unsigned hist_num = 0;
531
532char* hist_list[HIST_MAX];
533char hist_lines[HIST_MAX][HIST_SIZE];
534
535#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
536
537static void hist_init(void)
538{
539 int i;
540
541 hist_max = 0;
542 hist_add_idx = 0;
543 hist_cur = -1;
544 hist_num = 0;
545
546 for (i = 0; i < HIST_MAX; i++) {
547 hist_list[i] = hist_lines[i];
548 hist_list[i][0] = '\0';
549 }
550}
551
552static void cread_add_to_hist(char *line)
553{
554 strcpy(hist_list[hist_add_idx], line);
555
556 if (++hist_add_idx >= HIST_MAX)
557 hist_add_idx = 0;
558
559 if (hist_add_idx > hist_max)
560 hist_max = hist_add_idx;
561
562 hist_num++;
563}
564
565static char* hist_prev(void)
566{
567 char *ret;
568 int old_cur;
569
570 if (hist_cur < 0)
571 return NULL;
572
573 old_cur = hist_cur;
574 if (--hist_cur < 0)
575 hist_cur = hist_max;
576
577 if (hist_cur == hist_add_idx) {
578 hist_cur = old_cur;
579 ret = NULL;
580 } else
581 ret = hist_list[hist_cur];
582
583 return (ret);
584}
585
586static char* hist_next(void)
587{
588 char *ret;
589
590 if (hist_cur < 0)
591 return NULL;
592
593 if (hist_cur == hist_add_idx)
594 return NULL;
595
596 if (++hist_cur > hist_max)
597 hist_cur = 0;
598
599 if (hist_cur == hist_add_idx) {
600 ret = "";
601 } else
602 ret = hist_list[hist_cur];
603
604 return (ret);
605}
606
3ca9122f 607#ifndef CONFIG_CMDLINE_EDITING
501090aa
WD
608static void cread_print_hist_list(void)
609{
610 int i;
611 unsigned long n;
612
613 n = hist_num - hist_max;
614
615 i = hist_add_idx + 1;
616 while (1) {
617 if (i > hist_max)
618 i = 0;
619 if (i == hist_add_idx)
620 break;
621 printf("%s\n", hist_list[i]);
622 n++;
623 i++;
624 }
625}
3ca9122f 626#endif /* CONFIG_CMDLINE_EDITING */
501090aa
WD
627
628#define BEGINNING_OF_LINE() { \
629 while (num) { \
630 getcmd_putch(CTL_BACKSPACE); \
631 num--; \
632 } \
633}
634
635#define ERASE_TO_EOL() { \
636 if (num < eol_num) { \
637 int tmp; \
638 for (tmp = num; tmp < eol_num; tmp++) \
639 getcmd_putch(' '); \
640 while (tmp-- > num) \
641 getcmd_putch(CTL_BACKSPACE); \
642 eol_num = num; \
643 } \
644}
645
646#define REFRESH_TO_EOL() { \
647 if (num < eol_num) { \
648 wlen = eol_num - num; \
649 putnstr(buf + num, wlen); \
650 num = eol_num; \
651 } \
652}
653
654static void cread_add_char(char ichar, int insert, unsigned long *num,
655 unsigned long *eol_num, char *buf, unsigned long len)
656{
657 unsigned long wlen;
658
659 /* room ??? */
660 if (insert || *num == *eol_num) {
661 if (*eol_num > len - 1) {
662 getcmd_cbeep();
663 return;
664 }
665 (*eol_num)++;
666 }
667
668 if (insert) {
669 wlen = *eol_num - *num;
670 if (wlen > 1) {
671 memmove(&buf[*num+1], &buf[*num], wlen-1);
672 }
673
674 buf[*num] = ichar;
675 putnstr(buf + *num, wlen);
676 (*num)++;
677 while (--wlen) {
678 getcmd_putch(CTL_BACKSPACE);
679 }
680 } else {
681 /* echo the character */
682 wlen = 1;
683 buf[*num] = ichar;
684 putnstr(buf + *num, wlen);
685 (*num)++;
686 }
687}
688
689static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
690 unsigned long *eol_num, char *buf, unsigned long len)
691{
692 while (strsize--) {
693 cread_add_char(*str, insert, num, eol_num, buf, len);
694 str++;
695 }
696}
697
23d0baf9 698static int cread_line(const char *const prompt, char *buf, unsigned int *len)
501090aa
WD
699{
700 unsigned long num = 0;
701 unsigned long eol_num = 0;
702 unsigned long rlen;
703 unsigned long wlen;
704 char ichar;
705 int insert = 1;
706 int esc_len = 0;
707 int rc = 0;
708 char esc_save[8];
709
710 while (1) {
711 rlen = 1;
00ac50e3
AE
712#ifdef CONFIG_BOOT_RETRY_TIME
713 while (!tstc()) { /* while no incoming data */
714 if (retry_time >= 0 && get_ticks() > endtime)
715 return (-2); /* timed out */
716 }
717#endif
718
501090aa
WD
719 ichar = getcmd_getch();
720
721 if ((ichar == '\n') || (ichar == '\r')) {
dd9f06f0 722 putc('\n');
501090aa
WD
723 break;
724 }
725
726 /*
727 * handle standard linux xterm esc sequences for arrow key, etc.
728 */
729 if (esc_len != 0) {
730 if (esc_len == 1) {
731 if (ichar == '[') {
732 esc_save[esc_len] = ichar;
733 esc_len = 2;
734 } else {
735 cread_add_str(esc_save, esc_len, insert,
736 &num, &eol_num, buf, *len);
737 esc_len = 0;
738 }
739 continue;
740 }
741
742 switch (ichar) {
743
744 case 'D': /* <- key */
745 ichar = CTL_CH('b');
746 esc_len = 0;
747 break;
748 case 'C': /* -> key */
749 ichar = CTL_CH('f');
750 esc_len = 0;
751 break; /* pass off to ^F handler */
752 case 'H': /* Home key */
753 ichar = CTL_CH('a');
754 esc_len = 0;
755 break; /* pass off to ^A handler */
756 case 'A': /* up arrow */
757 ichar = CTL_CH('p');
758 esc_len = 0;
759 break; /* pass off to ^P handler */
760 case 'B': /* down arrow */
761 ichar = CTL_CH('n');
762 esc_len = 0;
763 break; /* pass off to ^N handler */
764 default:
765 esc_save[esc_len++] = ichar;
766 cread_add_str(esc_save, esc_len, insert,
767 &num, &eol_num, buf, *len);
768 esc_len = 0;
769 continue;
770 }
771 }
772
773 switch (ichar) {
774 case 0x1b:
775 if (esc_len == 0) {
776 esc_save[esc_len] = ichar;
777 esc_len = 1;
778 } else {
dd9f06f0 779 puts("impossible condition #876\n");
501090aa
WD
780 esc_len = 0;
781 }
782 break;
783
784 case CTL_CH('a'):
785 BEGINNING_OF_LINE();
786 break;
787 case CTL_CH('c'): /* ^C - break */
788 *buf = '\0'; /* discard input */
789 return (-1);
790 case CTL_CH('f'):
791 if (num < eol_num) {
792 getcmd_putch(buf[num]);
793 num++;
794 }
795 break;
796 case CTL_CH('b'):
797 if (num) {
798 getcmd_putch(CTL_BACKSPACE);
799 num--;
800 }
801 break;
802 case CTL_CH('d'):
803 if (num < eol_num) {
804 wlen = eol_num - num - 1;
805 if (wlen) {
806 memmove(&buf[num], &buf[num+1], wlen);
807 putnstr(buf + num, wlen);
808 }
809
810 getcmd_putch(' ');
811 do {
812 getcmd_putch(CTL_BACKSPACE);
813 } while (wlen--);
814 eol_num--;
815 }
816 break;
817 case CTL_CH('k'):
818 ERASE_TO_EOL();
819 break;
820 case CTL_CH('e'):
821 REFRESH_TO_EOL();
822 break;
823 case CTL_CH('o'):
824 insert = !insert;
825 break;
826 case CTL_CH('x'):
23d0baf9 827 case CTL_CH('u'):
501090aa
WD
828 BEGINNING_OF_LINE();
829 ERASE_TO_EOL();
830 break;
831 case DEL:
832 case DEL7:
833 case 8:
834 if (num) {
835 wlen = eol_num - num;
836 num--;
837 memmove(&buf[num], &buf[num+1], wlen);
838 getcmd_putch(CTL_BACKSPACE);
839 putnstr(buf + num, wlen);
840 getcmd_putch(' ');
841 do {
842 getcmd_putch(CTL_BACKSPACE);
843 } while (wlen--);
844 eol_num--;
845 }
846 break;
847 case CTL_CH('p'):
848 case CTL_CH('n'):
849 {
850 char * hline;
851
852 esc_len = 0;
853
854 if (ichar == CTL_CH('p'))
855 hline = hist_prev();
856 else
857 hline = hist_next();
858
859 if (!hline) {
860 getcmd_cbeep();
861 continue;
862 }
863
864 /* nuke the current line */
865 /* first, go home */
866 BEGINNING_OF_LINE();
867
868 /* erase to end of line */
869 ERASE_TO_EOL();
870
871 /* copy new line into place and display */
872 strcpy(buf, hline);
873 eol_num = strlen(buf);
874 REFRESH_TO_EOL();
875 continue;
876 }
23d0baf9
JCPV
877#ifdef CONFIG_AUTO_COMPLETE
878 case '\t': {
879 int num2, col;
880
881 /* do not autocomplete when in the middle */
882 if (num < eol_num) {
883 getcmd_cbeep();
884 break;
885 }
886
887 buf[num] = '\0';
888 col = strlen(prompt) + eol_num;
889 num2 = num;
890 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
891 col = num2 - num;
892 num += col;
893 eol_num += col;
894 }
895 break;
896 }
897#endif
501090aa
WD
898 default:
899 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
900 break;
901 }
902 }
903 *len = eol_num;
904 buf[eol_num] = '\0'; /* lose the newline */
905
906 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
907 cread_add_to_hist(buf);
908 hist_cur = hist_add_idx;
909
910 return (rc);
911}
912
913#endif /* CONFIG_CMDLINE_EDITING */
914
c609719b
WD
915/****************************************************************************/
916
917/*
918 * Prompt for input and read a line.
919 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
920 * time out when time goes past endtime (timebase time in ticks).
921 * Return: number of read characters
922 * -1 if break
923 * -2 if timed out
924 */
925int readline (const char *const prompt)
926{
6636b62a
JY
927 return readline_into_buffer(prompt, console_buffer);
928}
929
930
931int readline_into_buffer (const char *const prompt, char * buffer)
932{
933 char *p = buffer;
501090aa 934#ifdef CONFIG_CMDLINE_EDITING
501090aa 935 unsigned int len=MAX_CMDBUF_SIZE;
d8f961bb 936 int rc;
501090aa
WD
937 static int initted = 0;
938
939 if (!initted) {
940 hist_init();
941 initted = 1;
942 }
943
dd9f06f0 944 puts (prompt);
501090aa 945
23d0baf9 946 rc = cread_line(prompt, p, &len);
d8f961bb 947 return rc < 0 ? rc : len;
501090aa 948#else
0ec59524 949 char * p_buf = p;
c609719b
WD
950 int n = 0; /* buffer index */
951 int plen = 0; /* prompt length */
952 int col; /* output column cnt */
953 char c;
954
955 /* print prompt */
956 if (prompt) {
957 plen = strlen (prompt);
958 puts (prompt);
959 }
960 col = plen;
961
962 for (;;) {
963#ifdef CONFIG_BOOT_RETRY_TIME
964 while (!tstc()) { /* while no incoming data */
965 if (retry_time >= 0 && get_ticks() > endtime)
966 return (-2); /* timed out */
967 }
968#endif
969 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
970
971#ifdef CONFIG_SHOW_ACTIVITY
972 while (!tstc()) {
973 extern void show_activity(int arg);
974 show_activity(0);
975 }
976#endif
977 c = getc();
978
979 /*
980 * Special character handling
981 */
982 switch (c) {
983 case '\r': /* Enter */
984 case '\n':
985 *p = '\0';
986 puts ("\r\n");
6636b62a 987 return (p - p_buf);
c609719b 988
27aa8186
WD
989 case '\0': /* nul */
990 continue;
991
c609719b 992 case 0x03: /* ^C - break */
6636b62a 993 p_buf[0] = '\0'; /* discard input */
c609719b
WD
994 return (-1);
995
996 case 0x15: /* ^U - erase line */
997 while (col > plen) {
998 puts (erase_seq);
999 --col;
1000 }
6636b62a 1001 p = p_buf;
c609719b
WD
1002 n = 0;
1003 continue;
1004
1636d1c8 1005 case 0x17: /* ^W - erase word */
6636b62a 1006 p=delete_char(p_buf, p, &col, &n, plen);
c609719b 1007 while ((n > 0) && (*p != ' ')) {
6636b62a 1008 p=delete_char(p_buf, p, &col, &n, plen);
c609719b
WD
1009 }
1010 continue;
1011
1012 case 0x08: /* ^H - backspace */
1013 case 0x7F: /* DEL - backspace */
6636b62a 1014 p=delete_char(p_buf, p, &col, &n, plen);
c609719b
WD
1015 continue;
1016
1017 default:
1018 /*
1019 * Must be a normal character then
1020 */
1021 if (n < CFG_CBSIZE-2) {
1022 if (c == '\t') { /* expand TABs */
04a85b3b
WD
1023#ifdef CONFIG_AUTO_COMPLETE
1024 /* if auto completion triggered just continue */
1025 *p = '\0';
1026 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
6636b62a 1027 p = p_buf + n; /* reset */
04a85b3b
WD
1028 continue;
1029 }
1030#endif
c609719b
WD
1031 puts (tab_seq+(col&07));
1032 col += 8 - (col&07);
1033 } else {
1034 ++col; /* echo input */
1035 putc (c);
1036 }
1037 *p++ = c;
1038 ++n;
1039 } else { /* Buffer full */
1040 putc ('\a');
1041 }
1042 }
1043 }
501090aa 1044#endif /* CONFIG_CMDLINE_EDITING */
c609719b
WD
1045}
1046
1047/****************************************************************************/
1048
3ca9122f 1049#ifndef CONFIG_CMDLINE_EDITING
c609719b
WD
1050static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1051{
1052 char *s;
1053
1054 if (*np == 0) {
1055 return (p);
1056 }
1057
1058 if (*(--p) == '\t') { /* will retype the whole line */
1059 while (*colp > plen) {
1060 puts (erase_seq);
1061 (*colp)--;
1062 }
1063 for (s=buffer; s<p; ++s) {
1064 if (*s == '\t') {
1065 puts (tab_seq+((*colp) & 07));
1066 *colp += 8 - ((*colp) & 07);
1067 } else {
1068 ++(*colp);
1069 putc (*s);
1070 }
1071 }
1072 } else {
1073 puts (erase_seq);
1074 (*colp)--;
1075 }
1076 (*np)--;
1077 return (p);
1078}
3ca9122f 1079#endif /* CONFIG_CMDLINE_EDITING */
c609719b
WD
1080
1081/****************************************************************************/
1082
1083int parse_line (char *line, char *argv[])
1084{
1085 int nargs = 0;
1086
1087#ifdef DEBUG_PARSER
1088 printf ("parse_line: \"%s\"\n", line);
1089#endif
1090 while (nargs < CFG_MAXARGS) {
1091
1092 /* skip any white space */
1093 while ((*line == ' ') || (*line == '\t')) {
1094 ++line;
1095 }
1096
1097 if (*line == '\0') { /* end of line, no more args */
1098 argv[nargs] = NULL;
1099#ifdef DEBUG_PARSER
1100 printf ("parse_line: nargs=%d\n", nargs);
1101#endif
1102 return (nargs);
1103 }
1104
1105 argv[nargs++] = line; /* begin of argument string */
1106
1107 /* find end of string */
1108 while (*line && (*line != ' ') && (*line != '\t')) {
1109 ++line;
1110 }
1111
1112 if (*line == '\0') { /* end of line, no more args */
1113 argv[nargs] = NULL;
1114#ifdef DEBUG_PARSER
1115 printf ("parse_line: nargs=%d\n", nargs);
1116#endif
1117 return (nargs);
1118 }
1119
1120 *line++ = '\0'; /* terminate current arg */
1121 }
1122
1123 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1124
1125#ifdef DEBUG_PARSER
1126 printf ("parse_line: nargs=%d\n", nargs);
1127#endif
1128 return (nargs);
1129}
1130
1131/****************************************************************************/
1132
1133static void process_macros (const char *input, char *output)
1134{
1135 char c, prev;
1136 const char *varname_start = NULL;
19973b6a 1137 int inputcnt = strlen (input);
c609719b 1138 int outputcnt = CFG_CBSIZE;
19973b6a
WD
1139 int state = 0; /* 0 = waiting for '$' */
1140
1141 /* 1 = waiting for '(' or '{' */
1142 /* 2 = waiting for ')' or '}' */
1143 /* 3 = waiting for ''' */
c609719b
WD
1144#ifdef DEBUG_PARSER
1145 char *output_start = output;
1146
19973b6a
WD
1147 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1148 input);
c609719b
WD
1149#endif
1150
19973b6a 1151 prev = '\0'; /* previous character */
c609719b
WD
1152
1153 while (inputcnt && outputcnt) {
8bde7f77 1154 c = *input++;
19973b6a
WD
1155 inputcnt--;
1156
1157 if (state != 3) {
1158 /* remove one level of escape characters */
1159 if ((c == '\\') && (prev != '\\')) {
1160 if (inputcnt-- == 0)
1161 break;
1162 prev = c;
1163 c = *input++;
1164 }
c609719b 1165 }
19973b6a
WD
1166
1167 switch (state) {
1168 case 0: /* Waiting for (unescaped) $ */
1169 if ((c == '\'') && (prev != '\\')) {
1170 state = 3;
1171 break;
1172 }
1173 if ((c == '$') && (prev != '\\')) {
1174 state++;
1175 } else {
c609719b
WD
1176 *(output++) = c;
1177 outputcnt--;
1178 }
19973b6a
WD
1179 break;
1180 case 1: /* Waiting for ( */
1181 if (c == '(' || c == '{') {
1182 state++;
1183 varname_start = input;
1184 } else {
1185 state = 0;
1186 *(output++) = '$';
1187 outputcnt--;
c609719b 1188
19973b6a
WD
1189 if (outputcnt) {
1190 *(output++) = c;
c609719b
WD
1191 outputcnt--;
1192 }
19973b6a
WD
1193 }
1194 break;
1195 case 2: /* Waiting for ) */
1196 if (c == ')' || c == '}') {
1197 int i;
1198 char envname[CFG_CBSIZE], *envval;
1199 int envcnt = input - varname_start - 1; /* Varname # of chars */
1200
1201 /* Get the varname */
1202 for (i = 0; i < envcnt; i++) {
1203 envname[i] = varname_start[i];
1204 }
1205 envname[i] = 0;
1206
1207 /* Get its value */
1208 envval = getenv (envname);
1209
1210 /* Copy into the line if it exists */
1211 if (envval != NULL)
1212 while ((*envval) && outputcnt) {
1213 *(output++) = *(envval++);
1214 outputcnt--;
1215 }
1216 /* Look for another '$' */
1217 state = 0;
1218 }
1219 break;
1220 case 3: /* Waiting for ' */
1221 if ((c == '\'') && (prev != '\\')) {
1222 state = 0;
1223 } else {
1224 *(output++) = c;
1225 outputcnt--;
1226 }
1227 break;
a25f862b 1228 }
19973b6a 1229 prev = c;
c609719b
WD
1230 }
1231
1232 if (outputcnt)
1233 *output = 0;
9160b96f
BS
1234 else
1235 *(output - 1) = 0;
c609719b
WD
1236
1237#ifdef DEBUG_PARSER
1238 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
19973b6a 1239 strlen (output_start), output_start);
c609719b
WD
1240#endif
1241}
1242
1243/****************************************************************************
1244 * returns:
1245 * 1 - command executed, repeatable
1246 * 0 - command executed but not repeatable, interrupted commands are
1247 * always considered not repeatable
1248 * -1 - not executed (unrecognized, bootd recursion or too many args)
1249 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1250 * considered unrecognized)
1251 *
1252 * WARNING:
1253 *
1254 * We must create a temporary copy of the command since the command we get
1255 * may be the result from getenv(), which returns a pointer directly to
1256 * the environment data, which may change magicly when the command we run
1257 * creates or modifies environment variables (like "bootp" does).
1258 */
1259
1260int run_command (const char *cmd, int flag)
1261{
1262 cmd_tbl_t *cmdtp;
1263 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1264 char *token; /* start of token in cmdbuf */
1265 char *sep; /* end of token (separator) in cmdbuf */
1266 char finaltoken[CFG_CBSIZE];
1267 char *str = cmdbuf;
1268 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
f07771cc 1269 int argc, inquotes;
c609719b 1270 int repeatable = 1;
f07771cc 1271 int rc = 0;
c609719b
WD
1272
1273#ifdef DEBUG_PARSER
1274 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1275 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1276 puts ("\"\n");
1277#endif
1278
1279 clear_ctrlc(); /* forget any previous Control C */
1280
1281 if (!cmd || !*cmd) {
1282 return -1; /* empty command */
1283 }
1284
1285 if (strlen(cmd) >= CFG_CBSIZE) {
1286 puts ("## Command too long!\n");
1287 return -1;
1288 }
1289
1290 strcpy (cmdbuf, cmd);
1291
1292 /* Process separators and check for invalid
1293 * repeatable commands
1294 */
1295
1296#ifdef DEBUG_PARSER
1297 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1298#endif
1299 while (*str) {
1300
1301 /*
1302 * Find separator, or string end
1303 * Allow simple escape of ';' by writing "\;"
1304 */
a25f862b
WD
1305 for (inquotes = 0, sep = str; *sep; sep++) {
1306 if ((*sep=='\'') &&
1307 (*(sep-1) != '\\'))
1308 inquotes=!inquotes;
1309
1310 if (!inquotes &&
1311 (*sep == ';') && /* separator */
c609719b
WD
1312 ( sep != str) && /* past string start */
1313 (*(sep-1) != '\\')) /* and NOT escaped */
1314 break;
1315 }
1316
1317 /*
1318 * Limit the token to data between separators
1319 */
1320 token = str;
1321 if (*sep) {
1322 str = sep + 1; /* start of command for next pass */
1323 *sep = '\0';
1324 }
1325 else
1326 str = sep; /* no more commands for next pass */
1327#ifdef DEBUG_PARSER
1328 printf ("token: \"%s\"\n", token);
1329#endif
1330
1331 /* find macros in this token and replace them */
1332 process_macros (token, finaltoken);
1333
1334 /* Extract arguments */
1264b405
WD
1335 if ((argc = parse_line (finaltoken, argv)) == 0) {
1336 rc = -1; /* no command at all */
1337 continue;
1338 }
c609719b
WD
1339
1340 /* Look up command in command table */
1341 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1342 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
f07771cc
WD
1343 rc = -1; /* give up after bad command */
1344 continue;
c609719b
WD
1345 }
1346
1347 /* found - check max args */
1348 if (argc > cmdtp->maxargs) {
1349 printf ("Usage:\n%s\n", cmdtp->usage);
f07771cc
WD
1350 rc = -1;
1351 continue;
c609719b
WD
1352 }
1353
c3517f91 1354#if defined(CONFIG_CMD_BOOTD)
c609719b
WD
1355 /* avoid "bootd" recursion */
1356 if (cmdtp->cmd == do_bootd) {
1357#ifdef DEBUG_PARSER
1358 printf ("[%s]\n", finaltoken);
1359#endif
1360 if (flag & CMD_FLAG_BOOTD) {
4b9206ed 1361 puts ("'bootd' recursion detected\n");
f07771cc
WD
1362 rc = -1;
1363 continue;
1264b405 1364 } else {
c609719b 1365 flag |= CMD_FLAG_BOOTD;
1264b405 1366 }
c609719b 1367 }
90253178 1368#endif
c609719b
WD
1369
1370 /* OK - call function to do the command */
1371 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
f07771cc 1372 rc = -1;
c609719b
WD
1373 }
1374
1375 repeatable &= cmdtp->repeatable;
1376
1377 /* Did the user stop this? */
1378 if (had_ctrlc ())
5afb2020 1379 return -1; /* if stopped then not repeatable */
c609719b
WD
1380 }
1381
f07771cc 1382 return rc ? rc : repeatable;
c609719b
WD
1383}
1384
1385/****************************************************************************/
1386
c3517f91 1387#if defined(CONFIG_CMD_RUN)
c609719b
WD
1388int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1389{
1390 int i;
c609719b
WD
1391
1392 if (argc < 2) {
1393 printf ("Usage:\n%s\n", cmdtp->usage);
1394 return 1;
1395 }
1396
1397 for (i=1; i<argc; ++i) {
3e38691e
WD
1398 char *arg;
1399
1400 if ((arg = getenv (argv[i])) == NULL) {
1401 printf ("## Error: \"%s\" not defined\n", argv[i]);
1402 return 1;
1403 }
c609719b 1404#ifndef CFG_HUSH_PARSER
3e38691e
WD
1405 if (run_command (arg, flag) == -1)
1406 return 1;
c609719b 1407#else
3e38691e 1408 if (parse_string_outer(arg,
7aa78614 1409 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
3e38691e 1410 return 1;
c609719b
WD
1411#endif
1412 }
3e38691e 1413 return 0;
c609719b 1414}
90253178 1415#endif