xref: /openbmc/u-boot/common/main.c (revision 5e2c08c3ac90808e9be64856916bfd6df984823c)
1c609719bSwdenk /*
2c609719bSwdenk  * (C) Copyright 2000
3c609719bSwdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4c609719bSwdenk  *
5501090aaSWolfgang Denk  * Add to readline cmdline-editing by
6501090aaSWolfgang Denk  * (C) Copyright 2005
7501090aaSWolfgang Denk  * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8501090aaSWolfgang Denk  *
9c609719bSwdenk  * See file CREDITS for list of people who contributed to this
10c609719bSwdenk  * project.
11c609719bSwdenk  *
12c609719bSwdenk  * This program is free software; you can redistribute it and/or
13c609719bSwdenk  * modify it under the terms of the GNU General Public License as
14c609719bSwdenk  * published by the Free Software Foundation; either version 2 of
15c609719bSwdenk  * the License, or (at your option) any later version.
16c609719bSwdenk  *
17c609719bSwdenk  * This program is distributed in the hope that it will be useful,
18c609719bSwdenk  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c609719bSwdenk  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c609719bSwdenk  * GNU General Public License for more details.
21c609719bSwdenk  *
22c609719bSwdenk  * You should have received a copy of the GNU General Public License
23c609719bSwdenk  * along with this program; if not, write to the Free Software
24c609719bSwdenk  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25c609719bSwdenk  * MA 02111-1307 USA
26c609719bSwdenk  */
27c609719bSwdenk 
28a6c7ad2fSwdenk /* #define	DEBUG	*/
29a6c7ad2fSwdenk 
30c609719bSwdenk #include <common.h>
31c609719bSwdenk #include <watchdog.h>
32c609719bSwdenk #include <command.h>
33d9631ecfSWolfgang Denk #ifdef CONFIG_MODEM_SUPPORT
34d9631ecfSWolfgang Denk #include <malloc.h>		/* for free() prototype */
35d9631ecfSWolfgang Denk #endif
36c609719bSwdenk 
376d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
38c609719bSwdenk #include <hush.h>
39c609719bSwdenk #endif
40c609719bSwdenk 
41bdccc4feSwdenk #include <post.h>
42bdccc4feSwdenk 
43597f6c26SJames Yang #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
44d87080b7SWolfgang Denk DECLARE_GLOBAL_DATA_PTR;
45d87080b7SWolfgang Denk #endif
46d87080b7SWolfgang Denk 
47fad63407SHeiko Schocher /*
48fad63407SHeiko Schocher  * Board-specific Platform code can reimplement show_boot_progress () if needed
49fad63407SHeiko Schocher  */
50fad63407SHeiko Schocher void inline __show_boot_progress (int val) {}
51*5e2c08c3SEmil Medve void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
52fad63407SHeiko Schocher 
538bde7f77Swdenk #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
548bde7f77Swdenk extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);		/* for do_reset() prototype */
558bde7f77Swdenk #endif
568bde7f77Swdenk 
578bde7f77Swdenk extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
588bde7f77Swdenk 
594bae9090SBartlomiej Sieka #if defined(CONFIG_UPDATE_TFTP)
604bae9090SBartlomiej Sieka void update_tftp (void);
614bae9090SBartlomiej Sieka #endif /* CONFIG_UPDATE_TFTP */
628bde7f77Swdenk 
63c609719bSwdenk #define MAX_DELAY_STOP_STR 32
64c609719bSwdenk 
65c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
66c609719bSwdenk static int abortboot(int);
67c609719bSwdenk #endif
68c609719bSwdenk 
69c609719bSwdenk #undef DEBUG_PARSER
70c609719bSwdenk 
716d0f6bcfSJean-Christophe PLAGNIOL-VILLARD char        console_buffer[CONFIG_SYS_CBSIZE];		/* console I/O buffer	*/
72c609719bSwdenk 
733ca9122fSStefan Roese static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
74c609719bSwdenk static char erase_seq[] = "\b \b";		/* erase sequence	*/
75c609719bSwdenk static char   tab_seq[] = "        ";		/* used to expand TABs	*/
76c609719bSwdenk 
77c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
78c609719bSwdenk static uint64_t endtime = 0;  /* must be set, default is instant timeout */
79c609719bSwdenk static int      retry_time = -1; /* -1 so can call readline before main_loop */
80c609719bSwdenk #endif
81c609719bSwdenk 
82c609719bSwdenk #define	endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
83c609719bSwdenk 
84c609719bSwdenk #ifndef CONFIG_BOOT_RETRY_MIN
85c609719bSwdenk #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
86c609719bSwdenk #endif
87c609719bSwdenk 
88c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
89c609719bSwdenk int do_mdm_init = 0;
90c609719bSwdenk extern void mdm_init(void); /* defined in board.c */
91c609719bSwdenk #endif
92c609719bSwdenk 
93c609719bSwdenk /***************************************************************************
94c609719bSwdenk  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
95c609719bSwdenk  * returns: 0 -  no key string, allow autoboot
96c609719bSwdenk  *          1 - got key string, abort
97c609719bSwdenk  */
98c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
99c609719bSwdenk # if defined(CONFIG_AUTOBOOT_KEYED)
100c609719bSwdenk static __inline__ int abortboot(int bootdelay)
101c609719bSwdenk {
102c609719bSwdenk 	int abort = 0;
103c609719bSwdenk 	uint64_t etime = endtick(bootdelay);
10419973b6aSWolfgang Denk 	struct {
105c609719bSwdenk 		char* str;
106c609719bSwdenk 		u_int len;
107c609719bSwdenk 		int retry;
108c609719bSwdenk 	}
10919973b6aSWolfgang Denk 	delaykey [] = {
110c609719bSwdenk 		{ str: getenv ("bootdelaykey"),  retry: 1 },
111c609719bSwdenk 		{ str: getenv ("bootdelaykey2"), retry: 1 },
112c609719bSwdenk 		{ str: getenv ("bootstopkey"),   retry: 0 },
113c609719bSwdenk 		{ str: getenv ("bootstopkey2"),  retry: 0 },
114c609719bSwdenk 	};
115c609719bSwdenk 
116c609719bSwdenk 	char presskey [MAX_DELAY_STOP_STR];
117c609719bSwdenk 	u_int presskey_len = 0;
118c609719bSwdenk 	u_int presskey_max = 0;
119c609719bSwdenk 	u_int i;
120c609719bSwdenk 
121c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_PROMPT
122f2302d44SStefan Roese 	printf(CONFIG_AUTOBOOT_PROMPT);
123c609719bSwdenk #  endif
124c609719bSwdenk 
125c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR
126c609719bSwdenk 	if (delaykey[0].str == NULL)
127c609719bSwdenk 		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
128c609719bSwdenk #  endif
129c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR2
130c609719bSwdenk 	if (delaykey[1].str == NULL)
131c609719bSwdenk 		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
132c609719bSwdenk #  endif
133c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR
134c609719bSwdenk 	if (delaykey[2].str == NULL)
135c609719bSwdenk 		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
136c609719bSwdenk #  endif
137c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR2
138c609719bSwdenk 	if (delaykey[3].str == NULL)
139c609719bSwdenk 		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
140c609719bSwdenk #  endif
141c609719bSwdenk 
142c609719bSwdenk 	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
143c609719bSwdenk 		delaykey[i].len = delaykey[i].str == NULL ?
144c609719bSwdenk 				    0 : strlen (delaykey[i].str);
145c609719bSwdenk 		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
146c609719bSwdenk 				    MAX_DELAY_STOP_STR : delaykey[i].len;
147c609719bSwdenk 
148c609719bSwdenk 		presskey_max = presskey_max > delaykey[i].len ?
149c609719bSwdenk 				    presskey_max : delaykey[i].len;
150c609719bSwdenk 
151c609719bSwdenk #  if DEBUG_BOOTKEYS
152c609719bSwdenk 		printf("%s key:<%s>\n",
153c609719bSwdenk 		       delaykey[i].retry ? "delay" : "stop",
154c609719bSwdenk 		       delaykey[i].str ? delaykey[i].str : "NULL");
155c609719bSwdenk #  endif
156c609719bSwdenk 	}
157c609719bSwdenk 
158c609719bSwdenk 	/* In order to keep up with incoming data, check timeout only
159c609719bSwdenk 	 * when catch up.
160c609719bSwdenk 	 */
161c3284b03SPeter Korsgaard 	do {
162c3284b03SPeter Korsgaard 		if (tstc()) {
163c3284b03SPeter Korsgaard 			if (presskey_len < presskey_max) {
164c3284b03SPeter Korsgaard 				presskey [presskey_len ++] = getc();
165c3284b03SPeter Korsgaard 			}
166c3284b03SPeter Korsgaard 			else {
167c3284b03SPeter Korsgaard 				for (i = 0; i < presskey_max - 1; i ++)
168c3284b03SPeter Korsgaard 					presskey [i] = presskey [i + 1];
169c3284b03SPeter Korsgaard 
170c3284b03SPeter Korsgaard 				presskey [i] = getc();
171c3284b03SPeter Korsgaard 			}
172c3284b03SPeter Korsgaard 		}
173c3284b03SPeter Korsgaard 
174c609719bSwdenk 		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
175c609719bSwdenk 			if (delaykey[i].len > 0 &&
176c609719bSwdenk 			    presskey_len >= delaykey[i].len &&
177c609719bSwdenk 			    memcmp (presskey + presskey_len - delaykey[i].len,
178c609719bSwdenk 				    delaykey[i].str,
179c609719bSwdenk 				    delaykey[i].len) == 0) {
180c609719bSwdenk #  if DEBUG_BOOTKEYS
181c609719bSwdenk 				printf("got %skey\n",
182c609719bSwdenk 				       delaykey[i].retry ? "delay" : "stop");
183c609719bSwdenk #  endif
184c609719bSwdenk 
185c609719bSwdenk #  ifdef CONFIG_BOOT_RETRY_TIME
186c609719bSwdenk 				/* don't retry auto boot */
187c609719bSwdenk 				if (! delaykey[i].retry)
188c609719bSwdenk 					retry_time = -1;
189c609719bSwdenk #  endif
190c609719bSwdenk 				abort = 1;
191c609719bSwdenk 			}
192c609719bSwdenk 		}
193c3284b03SPeter Korsgaard 	} while (!abort && get_ticks() <= etime);
194c609719bSwdenk 
195c609719bSwdenk #  if DEBUG_BOOTKEYS
196c609719bSwdenk 	if (!abort)
1974b9206edSwdenk 		puts("key timeout\n");
198c609719bSwdenk #  endif
199c609719bSwdenk 
2008cb8143eSdzu #ifdef CONFIG_SILENT_CONSOLE
2014ec5bd55SLadislav Michl 	if (abort)
2024ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
2038cb8143eSdzu #endif
2048cb8143eSdzu 
205c609719bSwdenk 	return abort;
206c609719bSwdenk }
207c609719bSwdenk 
208c609719bSwdenk # else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
209c609719bSwdenk 
210c7de829cSwdenk #ifdef CONFIG_MENUKEY
211c7de829cSwdenk static int menukey = 0;
212c7de829cSwdenk #endif
213c7de829cSwdenk 
214c609719bSwdenk static __inline__ int abortboot(int bootdelay)
215c609719bSwdenk {
216c609719bSwdenk 	int abort = 0;
217c609719bSwdenk 
218c7de829cSwdenk #ifdef CONFIG_MENUPROMPT
219f2302d44SStefan Roese 	printf(CONFIG_MENUPROMPT);
220c7de829cSwdenk #else
221c609719bSwdenk 	printf("Hit any key to stop autoboot: %2d ", bootdelay);
222c7de829cSwdenk #endif
223c609719bSwdenk 
224c609719bSwdenk #if defined CONFIG_ZERO_BOOTDELAY_CHECK
225c609719bSwdenk 	/*
226c609719bSwdenk 	 * Check if key already pressed
227c609719bSwdenk 	 * Don't check if bootdelay < 0
228c609719bSwdenk 	 */
229c609719bSwdenk 	if (bootdelay >= 0) {
230c609719bSwdenk 		if (tstc()) {	/* we got a key press	*/
231c609719bSwdenk 			(void) getc();  /* consume input	*/
2324b9206edSwdenk 			puts ("\b\b\b 0");
233f72da340Swdenk 			abort = 1;	/* don't auto boot	*/
234c609719bSwdenk 		}
235c609719bSwdenk 	}
236c609719bSwdenk #endif
237c609719bSwdenk 
238f72da340Swdenk 	while ((bootdelay > 0) && (!abort)) {
239c609719bSwdenk 		int i;
240c609719bSwdenk 
241c609719bSwdenk 		--bootdelay;
242c609719bSwdenk 		/* delay 100 * 10ms */
243c609719bSwdenk 		for (i=0; !abort && i<100; ++i) {
244c609719bSwdenk 			if (tstc()) {	/* we got a key press	*/
245c609719bSwdenk 				abort  = 1;	/* don't auto boot	*/
246c609719bSwdenk 				bootdelay = 0;	/* no more delay	*/
247c7de829cSwdenk # ifdef CONFIG_MENUKEY
248c7de829cSwdenk 				menukey = getc();
249c7de829cSwdenk # else
250c609719bSwdenk 				(void) getc();  /* consume input	*/
251c7de829cSwdenk # endif
252c609719bSwdenk 				break;
253c609719bSwdenk 			}
254c609719bSwdenk 			udelay(10000);
255c609719bSwdenk 		}
256c609719bSwdenk 
257c609719bSwdenk 		printf("\b\b\b%2d ", bootdelay);
258c609719bSwdenk 	}
259c609719bSwdenk 
260c609719bSwdenk 	putc('\n');
261c609719bSwdenk 
262f72da340Swdenk #ifdef CONFIG_SILENT_CONSOLE
2634ec5bd55SLadislav Michl 	if (abort)
2644ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
265f72da340Swdenk #endif
266f72da340Swdenk 
267c609719bSwdenk 	return abort;
268c609719bSwdenk }
269c609719bSwdenk # endif	/* CONFIG_AUTOBOOT_KEYED */
270c609719bSwdenk #endif	/* CONFIG_BOOTDELAY >= 0  */
271c609719bSwdenk 
272c609719bSwdenk /****************************************************************************/
273c609719bSwdenk 
274c609719bSwdenk void main_loop (void)
275c609719bSwdenk {
2766d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER
2776d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
278c609719bSwdenk 	int len;
279c609719bSwdenk 	int rc = 1;
280c609719bSwdenk 	int flag;
281c609719bSwdenk #endif
282c609719bSwdenk 
283c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
284c609719bSwdenk 	char *s;
285c609719bSwdenk 	int bootdelay;
286c609719bSwdenk #endif
287c609719bSwdenk #ifdef CONFIG_PREBOOT
288c609719bSwdenk 	char *p;
289c609719bSwdenk #endif
290bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
291bdccc4feSwdenk 	unsigned long bootcount = 0;
292bdccc4feSwdenk 	unsigned long bootlimit = 0;
293bdccc4feSwdenk 	char *bcs;
294bdccc4feSwdenk 	char bcs_set[16];
295bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
296c609719bSwdenk 
297c609719bSwdenk #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
298c609719bSwdenk 	ulong bmp = 0;		/* default bitmap */
299c609719bSwdenk 	extern int trab_vfd (ulong bitmap);
300c609719bSwdenk 
301c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
302c609719bSwdenk 	if (do_mdm_init)
303c609719bSwdenk 		bmp = 1;	/* alternate bitmap */
304c609719bSwdenk #endif
305c609719bSwdenk 	trab_vfd (bmp);
306c609719bSwdenk #endif	/* CONFIG_VFD && VFD_TEST_LOGO */
307c609719bSwdenk 
3084bae9090SBartlomiej Sieka #if defined(CONFIG_UPDATE_TFTP)
3094bae9090SBartlomiej Sieka 	update_tftp ();
3104bae9090SBartlomiej Sieka #endif /* CONFIG_UPDATE_TFTP */
3114bae9090SBartlomiej Sieka 
312bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
313bdccc4feSwdenk 	bootcount = bootcount_load();
314bdccc4feSwdenk 	bootcount++;
315bdccc4feSwdenk 	bootcount_store (bootcount);
316bdccc4feSwdenk 	sprintf (bcs_set, "%lu", bootcount);
317bdccc4feSwdenk 	setenv ("bootcount", bcs_set);
318bdccc4feSwdenk 	bcs = getenv ("bootlimit");
319bdccc4feSwdenk 	bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
320bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
321bdccc4feSwdenk 
322c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
323c609719bSwdenk 	debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
324c609719bSwdenk 	if (do_mdm_init) {
32577ddac94SWolfgang Denk 		char *str = strdup(getenv("mdm_cmd"));
326c609719bSwdenk 		setenv ("preboot", str);  /* set or delete definition */
327c609719bSwdenk 		if (str != NULL)
328c609719bSwdenk 			free (str);
329c609719bSwdenk 		mdm_init(); /* wait for modem connection */
330c609719bSwdenk 	}
331c609719bSwdenk #endif  /* CONFIG_MODEM_SUPPORT */
332c609719bSwdenk 
3330587597cSstroese #ifdef CONFIG_VERSION_VARIABLE
3340587597cSstroese 	{
3350587597cSstroese 		extern char version_string[];
3360587597cSstroese 
3370587597cSstroese 		setenv ("ver", version_string);  /* set version variable */
3380587597cSstroese 	}
3390587597cSstroese #endif /* CONFIG_VERSION_VARIABLE */
3400587597cSstroese 
3416d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
342c609719bSwdenk 	u_boot_hush_start ();
343c609719bSwdenk #endif
344c609719bSwdenk 
34581473f67SHeiko Schocher #if defined(CONFIG_HUSH_INIT_VAR)
34681473f67SHeiko Schocher 	hush_init_var ();
34781473f67SHeiko Schocher #endif
34881473f67SHeiko Schocher 
34904a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
35004a85b3bSwdenk 	install_auto_complete();
35104a85b3bSwdenk #endif
35204a85b3bSwdenk 
353c609719bSwdenk #ifdef CONFIG_PREBOOT
354c609719bSwdenk 	if ((p = getenv ("preboot")) != NULL) {
355c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
356c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
357c609719bSwdenk # endif
358c609719bSwdenk 
3596d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER
360c609719bSwdenk 		run_command (p, 0);
361c609719bSwdenk # else
362c609719bSwdenk 		parse_string_outer(p, FLAG_PARSE_SEMICOLON |
363c609719bSwdenk 				    FLAG_EXIT_FROM_LOOP);
364c609719bSwdenk # endif
365c609719bSwdenk 
366c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
367c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
368c609719bSwdenk # endif
369c609719bSwdenk 	}
370c609719bSwdenk #endif /* CONFIG_PREBOOT */
371c609719bSwdenk 
372c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
373c609719bSwdenk 	s = getenv ("bootdelay");
374c609719bSwdenk 	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
375c609719bSwdenk 
376a6c7ad2fSwdenk 	debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
377c609719bSwdenk 
378c609719bSwdenk # ifdef CONFIG_BOOT_RETRY_TIME
3796dd652faSwdenk 	init_cmd_timeout ();
380c609719bSwdenk # endif	/* CONFIG_BOOT_RETRY_TIME */
381c609719bSwdenk 
382b428f6a8SYuri Tikhonov #ifdef CONFIG_POST
383b428f6a8SYuri Tikhonov 	if (gd->flags & GD_FLG_POSTFAIL) {
384b428f6a8SYuri Tikhonov 		s = getenv("failbootcmd");
385b428f6a8SYuri Tikhonov 	}
386b428f6a8SYuri Tikhonov 	else
387b428f6a8SYuri Tikhonov #endif /* CONFIG_POST */
388bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
389bdccc4feSwdenk 	if (bootlimit && (bootcount > bootlimit)) {
390bdccc4feSwdenk 		printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
391bdccc4feSwdenk 		        (unsigned)bootlimit);
392bdccc4feSwdenk 		s = getenv ("altbootcmd");
393bdccc4feSwdenk 	}
394bdccc4feSwdenk 	else
395bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
396c609719bSwdenk 		s = getenv ("bootcmd");
397a6c7ad2fSwdenk 
398a6c7ad2fSwdenk 	debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
399a6c7ad2fSwdenk 
400c609719bSwdenk 	if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
401c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
402c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
403c609719bSwdenk # endif
404c609719bSwdenk 
4056d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER
406c609719bSwdenk 		run_command (s, 0);
407c609719bSwdenk # else
408c609719bSwdenk 		parse_string_outer(s, FLAG_PARSE_SEMICOLON |
409c609719bSwdenk 				    FLAG_EXIT_FROM_LOOP);
410c609719bSwdenk # endif
411c609719bSwdenk 
412c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
413c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
414c609719bSwdenk # endif
415c609719bSwdenk 	}
416c7de829cSwdenk 
417c7de829cSwdenk # ifdef CONFIG_MENUKEY
418a6c7ad2fSwdenk 	if (menukey == CONFIG_MENUKEY) {
419c7de829cSwdenk 	    s = getenv("menucmd");
420a6c7ad2fSwdenk 	    if (s) {
4216d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER
4226225c5dbSwdenk 		run_command (s, 0);
423c7de829cSwdenk # else
424c7de829cSwdenk 		parse_string_outer(s, FLAG_PARSE_SEMICOLON |
425c7de829cSwdenk 				    FLAG_EXIT_FROM_LOOP);
426c7de829cSwdenk # endif
427c7de829cSwdenk 	    }
428c7de829cSwdenk 	}
429c7de829cSwdenk #endif /* CONFIG_MENUKEY */
430c609719bSwdenk #endif	/* CONFIG_BOOTDELAY */
431c609719bSwdenk 
432c7de829cSwdenk #ifdef CONFIG_AMIGAONEG3SE
433c7de829cSwdenk 	{
434c7de829cSwdenk 	    extern void video_banner(void);
435c7de829cSwdenk 	    video_banner();
436c7de829cSwdenk 	}
437c7de829cSwdenk #endif
438c7de829cSwdenk 
439c609719bSwdenk 	/*
440c609719bSwdenk 	 * Main Loop for Monitor Command Processing
441c609719bSwdenk 	 */
4426d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
443c609719bSwdenk 	parse_file_outer();
444c609719bSwdenk 	/* This point is never reached */
445c609719bSwdenk 	for (;;);
446c609719bSwdenk #else
447c609719bSwdenk 	for (;;) {
448c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
449c609719bSwdenk 		if (rc >= 0) {
450c609719bSwdenk 			/* Saw enough of a valid command to
451c609719bSwdenk 			 * restart the timeout.
452c609719bSwdenk 			 */
453c609719bSwdenk 			reset_cmd_timeout();
454c609719bSwdenk 		}
455c609719bSwdenk #endif
4566d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		len = readline (CONFIG_SYS_PROMPT);
457c609719bSwdenk 
458c609719bSwdenk 		flag = 0;	/* assume no special flags for now */
459c609719bSwdenk 		if (len > 0)
460c609719bSwdenk 			strcpy (lastcommand, console_buffer);
461c609719bSwdenk 		else if (len == 0)
462c609719bSwdenk 			flag |= CMD_FLAG_REPEAT;
463c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
464c609719bSwdenk 		else if (len == -2) {
465c609719bSwdenk 			/* -2 means timed out, retry autoboot
466c609719bSwdenk 			 */
4674b9206edSwdenk 			puts ("\nTimed out waiting for command\n");
468c609719bSwdenk # ifdef CONFIG_RESET_TO_RETRY
469c609719bSwdenk 			/* Reinit board to run initialization code again */
470c609719bSwdenk 			do_reset (NULL, 0, 0, NULL);
471c609719bSwdenk # else
472c609719bSwdenk 			return;		/* retry autoboot */
473c609719bSwdenk # endif
474c609719bSwdenk 		}
475c609719bSwdenk #endif
476c609719bSwdenk 
477c609719bSwdenk 		if (len == -1)
4784b9206edSwdenk 			puts ("<INTERRUPT>\n");
479c609719bSwdenk 		else
480c609719bSwdenk 			rc = run_command (lastcommand, flag);
481c609719bSwdenk 
482c609719bSwdenk 		if (rc <= 0) {
483c609719bSwdenk 			/* invalid command or not repeatable, forget it */
484c609719bSwdenk 			lastcommand[0] = 0;
485c609719bSwdenk 		}
486c609719bSwdenk 	}
4876d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /*CONFIG_SYS_HUSH_PARSER*/
488c609719bSwdenk }
489c609719bSwdenk 
4906dd652faSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
4916dd652faSwdenk /***************************************************************************
49219973b6aSWolfgang Denk  * initialize command line timeout
4936dd652faSwdenk  */
4946dd652faSwdenk void init_cmd_timeout(void)
4956dd652faSwdenk {
4966dd652faSwdenk 	char *s = getenv ("bootretry");
4976dd652faSwdenk 
4986dd652faSwdenk 	if (s != NULL)
499b028f715Swdenk 		retry_time = (int)simple_strtol(s, NULL, 10);
5006dd652faSwdenk 	else
5016dd652faSwdenk 		retry_time =  CONFIG_BOOT_RETRY_TIME;
5026dd652faSwdenk 
5036dd652faSwdenk 	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
5046dd652faSwdenk 		retry_time = CONFIG_BOOT_RETRY_MIN;
5056dd652faSwdenk }
5066dd652faSwdenk 
507c609719bSwdenk /***************************************************************************
508c609719bSwdenk  * reset command line timeout to retry_time seconds
509c609719bSwdenk  */
510c609719bSwdenk void reset_cmd_timeout(void)
511c609719bSwdenk {
512c609719bSwdenk 	endtime = endtick(retry_time);
513c609719bSwdenk }
514c609719bSwdenk #endif
515c609719bSwdenk 
516501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
517501090aaSWolfgang Denk 
518501090aaSWolfgang Denk /*
519501090aaSWolfgang Denk  * cmdline-editing related codes from vivi.
520501090aaSWolfgang Denk  * Author: Janghoon Lyu <nandy@mizi.com>
521501090aaSWolfgang Denk  */
522501090aaSWolfgang Denk 
523501090aaSWolfgang Denk #define putnstr(str,n)	do {			\
524dc4b0b38SAndrew Klossner 		printf ("%.*s", (int)n, str);	\
525501090aaSWolfgang Denk 	} while (0)
526501090aaSWolfgang Denk 
527501090aaSWolfgang Denk #define CTL_CH(c)		((c) - 'a' + 1)
528501090aaSWolfgang Denk 
529501090aaSWolfgang Denk #define MAX_CMDBUF_SIZE		256
530501090aaSWolfgang Denk 
531501090aaSWolfgang Denk #define CTL_BACKSPACE		('\b')
532501090aaSWolfgang Denk #define DEL			((char)255)
533501090aaSWolfgang Denk #define DEL7			((char)127)
534501090aaSWolfgang Denk #define CREAD_HIST_CHAR		('!')
535501090aaSWolfgang Denk 
536501090aaSWolfgang Denk #define getcmd_putch(ch)	putc(ch)
537501090aaSWolfgang Denk #define getcmd_getch()		getc()
538501090aaSWolfgang Denk #define getcmd_cbeep()		getcmd_putch('\a')
539501090aaSWolfgang Denk 
540501090aaSWolfgang Denk #define HIST_MAX		20
541501090aaSWolfgang Denk #define HIST_SIZE		MAX_CMDBUF_SIZE
542501090aaSWolfgang Denk 
543501090aaSWolfgang Denk static int hist_max = 0;
544501090aaSWolfgang Denk static int hist_add_idx = 0;
545501090aaSWolfgang Denk static int hist_cur = -1;
546501090aaSWolfgang Denk unsigned hist_num = 0;
547501090aaSWolfgang Denk 
548501090aaSWolfgang Denk char* hist_list[HIST_MAX];
549501090aaSWolfgang Denk char hist_lines[HIST_MAX][HIST_SIZE];
550501090aaSWolfgang Denk 
551501090aaSWolfgang Denk #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
552501090aaSWolfgang Denk 
553501090aaSWolfgang Denk static void hist_init(void)
554501090aaSWolfgang Denk {
555501090aaSWolfgang Denk 	int i;
556501090aaSWolfgang Denk 
557501090aaSWolfgang Denk 	hist_max = 0;
558501090aaSWolfgang Denk 	hist_add_idx = 0;
559501090aaSWolfgang Denk 	hist_cur = -1;
560501090aaSWolfgang Denk 	hist_num = 0;
561501090aaSWolfgang Denk 
562501090aaSWolfgang Denk 	for (i = 0; i < HIST_MAX; i++) {
563501090aaSWolfgang Denk 		hist_list[i] = hist_lines[i];
564501090aaSWolfgang Denk 		hist_list[i][0] = '\0';
565501090aaSWolfgang Denk 	}
566501090aaSWolfgang Denk }
567501090aaSWolfgang Denk 
568501090aaSWolfgang Denk static void cread_add_to_hist(char *line)
569501090aaSWolfgang Denk {
570501090aaSWolfgang Denk 	strcpy(hist_list[hist_add_idx], line);
571501090aaSWolfgang Denk 
572501090aaSWolfgang Denk 	if (++hist_add_idx >= HIST_MAX)
573501090aaSWolfgang Denk 		hist_add_idx = 0;
574501090aaSWolfgang Denk 
575501090aaSWolfgang Denk 	if (hist_add_idx > hist_max)
576501090aaSWolfgang Denk 		hist_max = hist_add_idx;
577501090aaSWolfgang Denk 
578501090aaSWolfgang Denk 	hist_num++;
579501090aaSWolfgang Denk }
580501090aaSWolfgang Denk 
581501090aaSWolfgang Denk static char* hist_prev(void)
582501090aaSWolfgang Denk {
583501090aaSWolfgang Denk 	char *ret;
584501090aaSWolfgang Denk 	int old_cur;
585501090aaSWolfgang Denk 
586501090aaSWolfgang Denk 	if (hist_cur < 0)
587501090aaSWolfgang Denk 		return NULL;
588501090aaSWolfgang Denk 
589501090aaSWolfgang Denk 	old_cur = hist_cur;
590501090aaSWolfgang Denk 	if (--hist_cur < 0)
591501090aaSWolfgang Denk 		hist_cur = hist_max;
592501090aaSWolfgang Denk 
593501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
594501090aaSWolfgang Denk 		hist_cur = old_cur;
595501090aaSWolfgang Denk 		ret = NULL;
596501090aaSWolfgang Denk 	} else
597501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
598501090aaSWolfgang Denk 
599501090aaSWolfgang Denk 	return (ret);
600501090aaSWolfgang Denk }
601501090aaSWolfgang Denk 
602501090aaSWolfgang Denk static char* hist_next(void)
603501090aaSWolfgang Denk {
604501090aaSWolfgang Denk 	char *ret;
605501090aaSWolfgang Denk 
606501090aaSWolfgang Denk 	if (hist_cur < 0)
607501090aaSWolfgang Denk 		return NULL;
608501090aaSWolfgang Denk 
609501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx)
610501090aaSWolfgang Denk 		return NULL;
611501090aaSWolfgang Denk 
612501090aaSWolfgang Denk 	if (++hist_cur > hist_max)
613501090aaSWolfgang Denk 		hist_cur = 0;
614501090aaSWolfgang Denk 
615501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
616501090aaSWolfgang Denk 		ret = "";
617501090aaSWolfgang Denk 	} else
618501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
619501090aaSWolfgang Denk 
620501090aaSWolfgang Denk 	return (ret);
621501090aaSWolfgang Denk }
622501090aaSWolfgang Denk 
6233ca9122fSStefan Roese #ifndef CONFIG_CMDLINE_EDITING
624501090aaSWolfgang Denk static void cread_print_hist_list(void)
625501090aaSWolfgang Denk {
626501090aaSWolfgang Denk 	int i;
627501090aaSWolfgang Denk 	unsigned long n;
628501090aaSWolfgang Denk 
629501090aaSWolfgang Denk 	n = hist_num - hist_max;
630501090aaSWolfgang Denk 
631501090aaSWolfgang Denk 	i = hist_add_idx + 1;
632501090aaSWolfgang Denk 	while (1) {
633501090aaSWolfgang Denk 		if (i > hist_max)
634501090aaSWolfgang Denk 			i = 0;
635501090aaSWolfgang Denk 		if (i == hist_add_idx)
636501090aaSWolfgang Denk 			break;
637501090aaSWolfgang Denk 		printf("%s\n", hist_list[i]);
638501090aaSWolfgang Denk 		n++;
639501090aaSWolfgang Denk 		i++;
640501090aaSWolfgang Denk 	}
641501090aaSWolfgang Denk }
6423ca9122fSStefan Roese #endif /* CONFIG_CMDLINE_EDITING */
643501090aaSWolfgang Denk 
644501090aaSWolfgang Denk #define BEGINNING_OF_LINE() {			\
645501090aaSWolfgang Denk 	while (num) {				\
646501090aaSWolfgang Denk 		getcmd_putch(CTL_BACKSPACE);	\
647501090aaSWolfgang Denk 		num--;				\
648501090aaSWolfgang Denk 	}					\
649501090aaSWolfgang Denk }
650501090aaSWolfgang Denk 
651501090aaSWolfgang Denk #define ERASE_TO_EOL() {				\
652501090aaSWolfgang Denk 	if (num < eol_num) {				\
653501090aaSWolfgang Denk 		int tmp;				\
654501090aaSWolfgang Denk 		for (tmp = num; tmp < eol_num; tmp++)	\
655501090aaSWolfgang Denk 			getcmd_putch(' ');		\
656501090aaSWolfgang Denk 		while (tmp-- > num)			\
657501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);	\
658501090aaSWolfgang Denk 		eol_num = num;				\
659501090aaSWolfgang Denk 	}						\
660501090aaSWolfgang Denk }
661501090aaSWolfgang Denk 
662501090aaSWolfgang Denk #define REFRESH_TO_EOL() {			\
663501090aaSWolfgang Denk 	if (num < eol_num) {			\
664501090aaSWolfgang Denk 		wlen = eol_num - num;		\
665501090aaSWolfgang Denk 		putnstr(buf + num, wlen);	\
666501090aaSWolfgang Denk 		num = eol_num;			\
667501090aaSWolfgang Denk 	}					\
668501090aaSWolfgang Denk }
669501090aaSWolfgang Denk 
670501090aaSWolfgang Denk static void cread_add_char(char ichar, int insert, unsigned long *num,
671501090aaSWolfgang Denk 	       unsigned long *eol_num, char *buf, unsigned long len)
672501090aaSWolfgang Denk {
673501090aaSWolfgang Denk 	unsigned long wlen;
674501090aaSWolfgang Denk 
675501090aaSWolfgang Denk 	/* room ??? */
676501090aaSWolfgang Denk 	if (insert || *num == *eol_num) {
677501090aaSWolfgang Denk 		if (*eol_num > len - 1) {
678501090aaSWolfgang Denk 			getcmd_cbeep();
679501090aaSWolfgang Denk 			return;
680501090aaSWolfgang Denk 		}
681501090aaSWolfgang Denk 		(*eol_num)++;
682501090aaSWolfgang Denk 	}
683501090aaSWolfgang Denk 
684501090aaSWolfgang Denk 	if (insert) {
685501090aaSWolfgang Denk 		wlen = *eol_num - *num;
686501090aaSWolfgang Denk 		if (wlen > 1) {
687501090aaSWolfgang Denk 			memmove(&buf[*num+1], &buf[*num], wlen-1);
688501090aaSWolfgang Denk 		}
689501090aaSWolfgang Denk 
690501090aaSWolfgang Denk 		buf[*num] = ichar;
691501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
692501090aaSWolfgang Denk 		(*num)++;
693501090aaSWolfgang Denk 		while (--wlen) {
694501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);
695501090aaSWolfgang Denk 		}
696501090aaSWolfgang Denk 	} else {
697501090aaSWolfgang Denk 		/* echo the character */
698501090aaSWolfgang Denk 		wlen = 1;
699501090aaSWolfgang Denk 		buf[*num] = ichar;
700501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
701501090aaSWolfgang Denk 		(*num)++;
702501090aaSWolfgang Denk 	}
703501090aaSWolfgang Denk }
704501090aaSWolfgang Denk 
705501090aaSWolfgang Denk static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
706501090aaSWolfgang Denk 	      unsigned long *eol_num, char *buf, unsigned long len)
707501090aaSWolfgang Denk {
708501090aaSWolfgang Denk 	while (strsize--) {
709501090aaSWolfgang Denk 		cread_add_char(*str, insert, num, eol_num, buf, len);
710501090aaSWolfgang Denk 		str++;
711501090aaSWolfgang Denk 	}
712501090aaSWolfgang Denk }
713501090aaSWolfgang Denk 
71423d0baf9SJean-Christophe PLAGNIOL-VILLARD static int cread_line(const char *const prompt, char *buf, unsigned int *len)
715501090aaSWolfgang Denk {
716501090aaSWolfgang Denk 	unsigned long num = 0;
717501090aaSWolfgang Denk 	unsigned long eol_num = 0;
718501090aaSWolfgang Denk 	unsigned long rlen;
719501090aaSWolfgang Denk 	unsigned long wlen;
720501090aaSWolfgang Denk 	char ichar;
721501090aaSWolfgang Denk 	int insert = 1;
722501090aaSWolfgang Denk 	int esc_len = 0;
723501090aaSWolfgang Denk 	int rc = 0;
724501090aaSWolfgang Denk 	char esc_save[8];
725501090aaSWolfgang Denk 
726501090aaSWolfgang Denk 	while (1) {
727501090aaSWolfgang Denk 		rlen = 1;
72800ac50e3SAndreas Engel #ifdef CONFIG_BOOT_RETRY_TIME
72900ac50e3SAndreas Engel 		while (!tstc()) {	/* while no incoming data */
73000ac50e3SAndreas Engel 			if (retry_time >= 0 && get_ticks() > endtime)
73100ac50e3SAndreas Engel 				return (-2);	/* timed out */
73200ac50e3SAndreas Engel 		}
73300ac50e3SAndreas Engel #endif
73400ac50e3SAndreas Engel 
735501090aaSWolfgang Denk 		ichar = getcmd_getch();
736501090aaSWolfgang Denk 
737501090aaSWolfgang Denk 		if ((ichar == '\n') || (ichar == '\r')) {
738dd9f06f0SWolfgang Denk 			putc('\n');
739501090aaSWolfgang Denk 			break;
740501090aaSWolfgang Denk 		}
741501090aaSWolfgang Denk 
742501090aaSWolfgang Denk 		/*
743501090aaSWolfgang Denk 		 * handle standard linux xterm esc sequences for arrow key, etc.
744501090aaSWolfgang Denk 		 */
745501090aaSWolfgang Denk 		if (esc_len != 0) {
746501090aaSWolfgang Denk 			if (esc_len == 1) {
747501090aaSWolfgang Denk 				if (ichar == '[') {
748501090aaSWolfgang Denk 					esc_save[esc_len] = ichar;
749501090aaSWolfgang Denk 					esc_len = 2;
750501090aaSWolfgang Denk 				} else {
751501090aaSWolfgang Denk 					cread_add_str(esc_save, esc_len, insert,
752501090aaSWolfgang Denk 						      &num, &eol_num, buf, *len);
753501090aaSWolfgang Denk 					esc_len = 0;
754501090aaSWolfgang Denk 				}
755501090aaSWolfgang Denk 				continue;
756501090aaSWolfgang Denk 			}
757501090aaSWolfgang Denk 
758501090aaSWolfgang Denk 			switch (ichar) {
759501090aaSWolfgang Denk 
760501090aaSWolfgang Denk 			case 'D':	/* <- key */
761501090aaSWolfgang Denk 				ichar = CTL_CH('b');
762501090aaSWolfgang Denk 				esc_len = 0;
763501090aaSWolfgang Denk 				break;
764501090aaSWolfgang Denk 			case 'C':	/* -> key */
765501090aaSWolfgang Denk 				ichar = CTL_CH('f');
766501090aaSWolfgang Denk 				esc_len = 0;
767501090aaSWolfgang Denk 				break;	/* pass off to ^F handler */
768501090aaSWolfgang Denk 			case 'H':	/* Home key */
769501090aaSWolfgang Denk 				ichar = CTL_CH('a');
770501090aaSWolfgang Denk 				esc_len = 0;
771501090aaSWolfgang Denk 				break;	/* pass off to ^A handler */
772501090aaSWolfgang Denk 			case 'A':	/* up arrow */
773501090aaSWolfgang Denk 				ichar = CTL_CH('p');
774501090aaSWolfgang Denk 				esc_len = 0;
775501090aaSWolfgang Denk 				break;	/* pass off to ^P handler */
776501090aaSWolfgang Denk 			case 'B':	/* down arrow */
777501090aaSWolfgang Denk 				ichar = CTL_CH('n');
778501090aaSWolfgang Denk 				esc_len = 0;
779501090aaSWolfgang Denk 				break;	/* pass off to ^N handler */
780501090aaSWolfgang Denk 			default:
781501090aaSWolfgang Denk 				esc_save[esc_len++] = ichar;
782501090aaSWolfgang Denk 				cread_add_str(esc_save, esc_len, insert,
783501090aaSWolfgang Denk 					      &num, &eol_num, buf, *len);
784501090aaSWolfgang Denk 				esc_len = 0;
785501090aaSWolfgang Denk 				continue;
786501090aaSWolfgang Denk 			}
787501090aaSWolfgang Denk 		}
788501090aaSWolfgang Denk 
789501090aaSWolfgang Denk 		switch (ichar) {
790501090aaSWolfgang Denk 		case 0x1b:
791501090aaSWolfgang Denk 			if (esc_len == 0) {
792501090aaSWolfgang Denk 				esc_save[esc_len] = ichar;
793501090aaSWolfgang Denk 				esc_len = 1;
794501090aaSWolfgang Denk 			} else {
795dd9f06f0SWolfgang Denk 				puts("impossible condition #876\n");
796501090aaSWolfgang Denk 				esc_len = 0;
797501090aaSWolfgang Denk 			}
798501090aaSWolfgang Denk 			break;
799501090aaSWolfgang Denk 
800501090aaSWolfgang Denk 		case CTL_CH('a'):
801501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
802501090aaSWolfgang Denk 			break;
803501090aaSWolfgang Denk 		case CTL_CH('c'):	/* ^C - break */
804501090aaSWolfgang Denk 			*buf = '\0';	/* discard input */
805501090aaSWolfgang Denk 			return (-1);
806501090aaSWolfgang Denk 		case CTL_CH('f'):
807501090aaSWolfgang Denk 			if (num < eol_num) {
808501090aaSWolfgang Denk 				getcmd_putch(buf[num]);
809501090aaSWolfgang Denk 				num++;
810501090aaSWolfgang Denk 			}
811501090aaSWolfgang Denk 			break;
812501090aaSWolfgang Denk 		case CTL_CH('b'):
813501090aaSWolfgang Denk 			if (num) {
814501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
815501090aaSWolfgang Denk 				num--;
816501090aaSWolfgang Denk 			}
817501090aaSWolfgang Denk 			break;
818501090aaSWolfgang Denk 		case CTL_CH('d'):
819501090aaSWolfgang Denk 			if (num < eol_num) {
820501090aaSWolfgang Denk 				wlen = eol_num - num - 1;
821501090aaSWolfgang Denk 				if (wlen) {
822501090aaSWolfgang Denk 					memmove(&buf[num], &buf[num+1], wlen);
823501090aaSWolfgang Denk 					putnstr(buf + num, wlen);
824501090aaSWolfgang Denk 				}
825501090aaSWolfgang Denk 
826501090aaSWolfgang Denk 				getcmd_putch(' ');
827501090aaSWolfgang Denk 				do {
828501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
829501090aaSWolfgang Denk 				} while (wlen--);
830501090aaSWolfgang Denk 				eol_num--;
831501090aaSWolfgang Denk 			}
832501090aaSWolfgang Denk 			break;
833501090aaSWolfgang Denk 		case CTL_CH('k'):
834501090aaSWolfgang Denk 			ERASE_TO_EOL();
835501090aaSWolfgang Denk 			break;
836501090aaSWolfgang Denk 		case CTL_CH('e'):
837501090aaSWolfgang Denk 			REFRESH_TO_EOL();
838501090aaSWolfgang Denk 			break;
839501090aaSWolfgang Denk 		case CTL_CH('o'):
840501090aaSWolfgang Denk 			insert = !insert;
841501090aaSWolfgang Denk 			break;
842501090aaSWolfgang Denk 		case CTL_CH('x'):
84323d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case CTL_CH('u'):
844501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
845501090aaSWolfgang Denk 			ERASE_TO_EOL();
846501090aaSWolfgang Denk 			break;
847501090aaSWolfgang Denk 		case DEL:
848501090aaSWolfgang Denk 		case DEL7:
849501090aaSWolfgang Denk 		case 8:
850501090aaSWolfgang Denk 			if (num) {
851501090aaSWolfgang Denk 				wlen = eol_num - num;
852501090aaSWolfgang Denk 				num--;
853501090aaSWolfgang Denk 				memmove(&buf[num], &buf[num+1], wlen);
854501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
855501090aaSWolfgang Denk 				putnstr(buf + num, wlen);
856501090aaSWolfgang Denk 				getcmd_putch(' ');
857501090aaSWolfgang Denk 				do {
858501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
859501090aaSWolfgang Denk 				} while (wlen--);
860501090aaSWolfgang Denk 				eol_num--;
861501090aaSWolfgang Denk 			}
862501090aaSWolfgang Denk 			break;
863501090aaSWolfgang Denk 		case CTL_CH('p'):
864501090aaSWolfgang Denk 		case CTL_CH('n'):
865501090aaSWolfgang Denk 		{
866501090aaSWolfgang Denk 			char * hline;
867501090aaSWolfgang Denk 
868501090aaSWolfgang Denk 			esc_len = 0;
869501090aaSWolfgang Denk 
870501090aaSWolfgang Denk 			if (ichar == CTL_CH('p'))
871501090aaSWolfgang Denk 				hline = hist_prev();
872501090aaSWolfgang Denk 			else
873501090aaSWolfgang Denk 				hline = hist_next();
874501090aaSWolfgang Denk 
875501090aaSWolfgang Denk 			if (!hline) {
876501090aaSWolfgang Denk 				getcmd_cbeep();
877501090aaSWolfgang Denk 				continue;
878501090aaSWolfgang Denk 			}
879501090aaSWolfgang Denk 
880501090aaSWolfgang Denk 			/* nuke the current line */
881501090aaSWolfgang Denk 			/* first, go home */
882501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
883501090aaSWolfgang Denk 
884501090aaSWolfgang Denk 			/* erase to end of line */
885501090aaSWolfgang Denk 			ERASE_TO_EOL();
886501090aaSWolfgang Denk 
887501090aaSWolfgang Denk 			/* copy new line into place and display */
888501090aaSWolfgang Denk 			strcpy(buf, hline);
889501090aaSWolfgang Denk 			eol_num = strlen(buf);
890501090aaSWolfgang Denk 			REFRESH_TO_EOL();
891501090aaSWolfgang Denk 			continue;
892501090aaSWolfgang Denk 		}
89323d0baf9SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_AUTO_COMPLETE
89423d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case '\t': {
89523d0baf9SJean-Christophe PLAGNIOL-VILLARD 			int num2, col;
89623d0baf9SJean-Christophe PLAGNIOL-VILLARD 
89723d0baf9SJean-Christophe PLAGNIOL-VILLARD 			/* do not autocomplete when in the middle */
89823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (num < eol_num) {
89923d0baf9SJean-Christophe PLAGNIOL-VILLARD 				getcmd_cbeep();
90023d0baf9SJean-Christophe PLAGNIOL-VILLARD 				break;
90123d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
90223d0baf9SJean-Christophe PLAGNIOL-VILLARD 
90323d0baf9SJean-Christophe PLAGNIOL-VILLARD 			buf[num] = '\0';
90423d0baf9SJean-Christophe PLAGNIOL-VILLARD 			col = strlen(prompt) + eol_num;
90523d0baf9SJean-Christophe PLAGNIOL-VILLARD 			num2 = num;
90623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
90723d0baf9SJean-Christophe PLAGNIOL-VILLARD 				col = num2 - num;
90823d0baf9SJean-Christophe PLAGNIOL-VILLARD 				num += col;
90923d0baf9SJean-Christophe PLAGNIOL-VILLARD 				eol_num += col;
91023d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
91123d0baf9SJean-Christophe PLAGNIOL-VILLARD 			break;
91223d0baf9SJean-Christophe PLAGNIOL-VILLARD 		}
91323d0baf9SJean-Christophe PLAGNIOL-VILLARD #endif
914501090aaSWolfgang Denk 		default:
915501090aaSWolfgang Denk 			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
916501090aaSWolfgang Denk 			break;
917501090aaSWolfgang Denk 		}
918501090aaSWolfgang Denk 	}
919501090aaSWolfgang Denk 	*len = eol_num;
920501090aaSWolfgang Denk 	buf[eol_num] = '\0';	/* lose the newline */
921501090aaSWolfgang Denk 
922501090aaSWolfgang Denk 	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
923501090aaSWolfgang Denk 		cread_add_to_hist(buf);
924501090aaSWolfgang Denk 	hist_cur = hist_add_idx;
925501090aaSWolfgang Denk 
926501090aaSWolfgang Denk 	return (rc);
927501090aaSWolfgang Denk }
928501090aaSWolfgang Denk 
929501090aaSWolfgang Denk #endif /* CONFIG_CMDLINE_EDITING */
930501090aaSWolfgang Denk 
931c609719bSwdenk /****************************************************************************/
932c609719bSwdenk 
933c609719bSwdenk /*
934c609719bSwdenk  * Prompt for input and read a line.
935c609719bSwdenk  * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
936c609719bSwdenk  * time out when time goes past endtime (timebase time in ticks).
937c609719bSwdenk  * Return:	number of read characters
938c609719bSwdenk  *		-1 if break
939c609719bSwdenk  *		-2 if timed out
940c609719bSwdenk  */
941c609719bSwdenk int readline (const char *const prompt)
942c609719bSwdenk {
9436636b62aSJames Yang 	return readline_into_buffer(prompt, console_buffer);
9446636b62aSJames Yang }
9456636b62aSJames Yang 
9466636b62aSJames Yang 
9476636b62aSJames Yang int readline_into_buffer (const char *const prompt, char * buffer)
9486636b62aSJames Yang {
9496636b62aSJames Yang 	char *p = buffer;
950501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
951501090aaSWolfgang Denk 	unsigned int len=MAX_CMDBUF_SIZE;
952d8f961bbSStefan Roese 	int rc;
953501090aaSWolfgang Denk 	static int initted = 0;
954501090aaSWolfgang Denk 
955597f6c26SJames Yang 	/*
956597f6c26SJames Yang 	 * History uses a global array which is not
957597f6c26SJames Yang 	 * writable until after relocation to RAM.
958597f6c26SJames Yang 	 * Revert to non-history version if still
959597f6c26SJames Yang 	 * running from flash.
960597f6c26SJames Yang 	 */
961597f6c26SJames Yang 	if (gd->flags & GD_FLG_RELOC) {
962597f6c26SJames Yang 		if (!initted) {
963597f6c26SJames Yang 			hist_init();
964597f6c26SJames Yang 			initted = 1;
965597f6c26SJames Yang 		}
966597f6c26SJames Yang 
967dd9f06f0SWolfgang Denk 		puts (prompt);
968501090aaSWolfgang Denk 
96923d0baf9SJean-Christophe PLAGNIOL-VILLARD 		rc = cread_line(prompt, p, &len);
970d8f961bbSStefan Roese 		return rc < 0 ? rc : len;
971597f6c26SJames Yang 
972597f6c26SJames Yang 	} else {
973597f6c26SJames Yang #endif	/* CONFIG_CMDLINE_EDITING */
9740ec59524SKumar Gala 	char * p_buf = p;
975c609719bSwdenk 	int	n = 0;				/* buffer index		*/
976c609719bSwdenk 	int	plen = 0;			/* prompt length	*/
977c609719bSwdenk 	int	col;				/* output column cnt	*/
978c609719bSwdenk 	char	c;
979c609719bSwdenk 
980c609719bSwdenk 	/* print prompt */
981c609719bSwdenk 	if (prompt) {
982c609719bSwdenk 		plen = strlen (prompt);
983c609719bSwdenk 		puts (prompt);
984c609719bSwdenk 	}
985c609719bSwdenk 	col = plen;
986c609719bSwdenk 
987c609719bSwdenk 	for (;;) {
988c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
989c609719bSwdenk 		while (!tstc()) {	/* while no incoming data */
990c609719bSwdenk 			if (retry_time >= 0 && get_ticks() > endtime)
991c609719bSwdenk 				return (-2);	/* timed out */
992c609719bSwdenk 		}
993c609719bSwdenk #endif
994c609719bSwdenk 		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
995c609719bSwdenk 
996c609719bSwdenk #ifdef CONFIG_SHOW_ACTIVITY
997c609719bSwdenk 		while (!tstc()) {
998c609719bSwdenk 			extern void show_activity(int arg);
999c609719bSwdenk 			show_activity(0);
1000c609719bSwdenk 		}
1001c609719bSwdenk #endif
1002c609719bSwdenk 		c = getc();
1003c609719bSwdenk 
1004c609719bSwdenk 		/*
1005c609719bSwdenk 		 * Special character handling
1006c609719bSwdenk 		 */
1007c609719bSwdenk 		switch (c) {
1008c609719bSwdenk 		case '\r':				/* Enter		*/
1009c609719bSwdenk 		case '\n':
1010c609719bSwdenk 			*p = '\0';
1011c609719bSwdenk 			puts ("\r\n");
10126636b62aSJames Yang 			return (p - p_buf);
1013c609719bSwdenk 
101427aa8186Swdenk 		case '\0':				/* nul			*/
101527aa8186Swdenk 			continue;
101627aa8186Swdenk 
1017c609719bSwdenk 		case 0x03:				/* ^C - break		*/
10186636b62aSJames Yang 			p_buf[0] = '\0';	/* discard input */
1019c609719bSwdenk 			return (-1);
1020c609719bSwdenk 
1021c609719bSwdenk 		case 0x15:				/* ^U - erase line	*/
1022c609719bSwdenk 			while (col > plen) {
1023c609719bSwdenk 				puts (erase_seq);
1024c609719bSwdenk 				--col;
1025c609719bSwdenk 			}
10266636b62aSJames Yang 			p = p_buf;
1027c609719bSwdenk 			n = 0;
1028c609719bSwdenk 			continue;
1029c609719bSwdenk 
1030c609719bSwdenk 		case 0x17:				/* ^W - erase word	*/
10316636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1032c609719bSwdenk 			while ((n > 0) && (*p != ' ')) {
10336636b62aSJames Yang 				p=delete_char(p_buf, p, &col, &n, plen);
1034c609719bSwdenk 			}
1035c609719bSwdenk 			continue;
1036c609719bSwdenk 
1037c609719bSwdenk 		case 0x08:				/* ^H  - backspace	*/
1038c609719bSwdenk 		case 0x7F:				/* DEL - backspace	*/
10396636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1040c609719bSwdenk 			continue;
1041c609719bSwdenk 
1042c609719bSwdenk 		default:
1043c609719bSwdenk 			/*
1044c609719bSwdenk 			 * Must be a normal character then
1045c609719bSwdenk 			 */
10466d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 			if (n < CONFIG_SYS_CBSIZE-2) {
1047c609719bSwdenk 				if (c == '\t') {	/* expand TABs		*/
104804a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
104904a85b3bSwdenk 					/* if auto completion triggered just continue */
105004a85b3bSwdenk 					*p = '\0';
105104a85b3bSwdenk 					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
10526636b62aSJames Yang 						p = p_buf + n;	/* reset */
105304a85b3bSwdenk 						continue;
105404a85b3bSwdenk 					}
105504a85b3bSwdenk #endif
1056c609719bSwdenk 					puts (tab_seq+(col&07));
1057c609719bSwdenk 					col += 8 - (col&07);
1058c609719bSwdenk 				} else {
1059c609719bSwdenk 					++col;		/* echo input		*/
1060c609719bSwdenk 					putc (c);
1061c609719bSwdenk 				}
1062c609719bSwdenk 				*p++ = c;
1063c609719bSwdenk 				++n;
1064c609719bSwdenk 			} else {			/* Buffer full		*/
1065c609719bSwdenk 				putc ('\a');
1066c609719bSwdenk 			}
1067c609719bSwdenk 		}
1068c609719bSwdenk 	}
1069597f6c26SJames Yang #ifdef CONFIG_CMDLINE_EDITING
1070597f6c26SJames Yang 	}
1071597f6c26SJames Yang #endif
1072c609719bSwdenk }
1073c609719bSwdenk 
1074c609719bSwdenk /****************************************************************************/
1075c609719bSwdenk 
1076c609719bSwdenk static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1077c609719bSwdenk {
1078c609719bSwdenk 	char *s;
1079c609719bSwdenk 
1080c609719bSwdenk 	if (*np == 0) {
1081c609719bSwdenk 		return (p);
1082c609719bSwdenk 	}
1083c609719bSwdenk 
1084c609719bSwdenk 	if (*(--p) == '\t') {			/* will retype the whole line	*/
1085c609719bSwdenk 		while (*colp > plen) {
1086c609719bSwdenk 			puts (erase_seq);
1087c609719bSwdenk 			(*colp)--;
1088c609719bSwdenk 		}
1089c609719bSwdenk 		for (s=buffer; s<p; ++s) {
1090c609719bSwdenk 			if (*s == '\t') {
1091c609719bSwdenk 				puts (tab_seq+((*colp) & 07));
1092c609719bSwdenk 				*colp += 8 - ((*colp) & 07);
1093c609719bSwdenk 			} else {
1094c609719bSwdenk 				++(*colp);
1095c609719bSwdenk 				putc (*s);
1096c609719bSwdenk 			}
1097c609719bSwdenk 		}
1098c609719bSwdenk 	} else {
1099c609719bSwdenk 		puts (erase_seq);
1100c609719bSwdenk 		(*colp)--;
1101c609719bSwdenk 	}
1102c609719bSwdenk 	(*np)--;
1103c609719bSwdenk 	return (p);
1104c609719bSwdenk }
1105c609719bSwdenk 
1106c609719bSwdenk /****************************************************************************/
1107c609719bSwdenk 
1108c609719bSwdenk int parse_line (char *line, char *argv[])
1109c609719bSwdenk {
1110c609719bSwdenk 	int nargs = 0;
1111c609719bSwdenk 
1112c609719bSwdenk #ifdef DEBUG_PARSER
1113c609719bSwdenk 	printf ("parse_line: \"%s\"\n", line);
1114c609719bSwdenk #endif
11156d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	while (nargs < CONFIG_SYS_MAXARGS) {
1116c609719bSwdenk 
1117c609719bSwdenk 		/* skip any white space */
1118c609719bSwdenk 		while ((*line == ' ') || (*line == '\t')) {
1119c609719bSwdenk 			++line;
1120c609719bSwdenk 		}
1121c609719bSwdenk 
1122c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1123c609719bSwdenk 			argv[nargs] = NULL;
1124c609719bSwdenk #ifdef DEBUG_PARSER
1125c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1126c609719bSwdenk #endif
1127c609719bSwdenk 			return (nargs);
1128c609719bSwdenk 		}
1129c609719bSwdenk 
1130c609719bSwdenk 		argv[nargs++] = line;	/* begin of argument string	*/
1131c609719bSwdenk 
1132c609719bSwdenk 		/* find end of string */
1133c609719bSwdenk 		while (*line && (*line != ' ') && (*line != '\t')) {
1134c609719bSwdenk 			++line;
1135c609719bSwdenk 		}
1136c609719bSwdenk 
1137c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1138c609719bSwdenk 			argv[nargs] = NULL;
1139c609719bSwdenk #ifdef DEBUG_PARSER
1140c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1141c609719bSwdenk #endif
1142c609719bSwdenk 			return (nargs);
1143c609719bSwdenk 		}
1144c609719bSwdenk 
1145c609719bSwdenk 		*line++ = '\0';		/* terminate current arg	 */
1146c609719bSwdenk 	}
1147c609719bSwdenk 
11486d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1149c609719bSwdenk 
1150c609719bSwdenk #ifdef DEBUG_PARSER
1151c609719bSwdenk 	printf ("parse_line: nargs=%d\n", nargs);
1152c609719bSwdenk #endif
1153c609719bSwdenk 	return (nargs);
1154c609719bSwdenk }
1155c609719bSwdenk 
1156c609719bSwdenk /****************************************************************************/
1157c609719bSwdenk 
1158c609719bSwdenk static void process_macros (const char *input, char *output)
1159c609719bSwdenk {
1160c609719bSwdenk 	char c, prev;
1161c609719bSwdenk 	const char *varname_start = NULL;
1162c609719bSwdenk 	int inputcnt = strlen (input);
11636d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	int outputcnt = CONFIG_SYS_CBSIZE;
1164c609719bSwdenk 	int state = 0;		/* 0 = waiting for '$'  */
116519973b6aSWolfgang Denk 
11665cf91d6bSwdenk 	/* 1 = waiting for '(' or '{' */
11675cf91d6bSwdenk 	/* 2 = waiting for ')' or '}' */
1168a25f862bSwdenk 	/* 3 = waiting for '''  */
1169c609719bSwdenk #ifdef DEBUG_PARSER
1170c609719bSwdenk 	char *output_start = output;
1171c609719bSwdenk 
117219973b6aSWolfgang Denk 	printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
117319973b6aSWolfgang Denk 		input);
1174c609719bSwdenk #endif
1175c609719bSwdenk 
1176c609719bSwdenk 	prev = '\0';		/* previous character   */
1177c609719bSwdenk 
1178c609719bSwdenk 	while (inputcnt && outputcnt) {
1179c609719bSwdenk 		c = *input++;
1180c609719bSwdenk 		inputcnt--;
1181c609719bSwdenk 
1182a25f862bSwdenk 		if (state != 3) {
1183c609719bSwdenk 			/* remove one level of escape characters */
1184c609719bSwdenk 			if ((c == '\\') && (prev != '\\')) {
1185c609719bSwdenk 				if (inputcnt-- == 0)
1186c609719bSwdenk 					break;
1187c609719bSwdenk 				prev = c;
1188c609719bSwdenk 				c = *input++;
1189c609719bSwdenk 			}
1190a25f862bSwdenk 		}
1191c609719bSwdenk 
1192c609719bSwdenk 		switch (state) {
1193c609719bSwdenk 		case 0:	/* Waiting for (unescaped) $    */
1194a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1195a25f862bSwdenk 				state = 3;
1196a25f862bSwdenk 				break;
1197a25f862bSwdenk 			}
1198c609719bSwdenk 			if ((c == '$') && (prev != '\\')) {
1199c609719bSwdenk 				state++;
1200c609719bSwdenk 			} else {
1201c609719bSwdenk 				*(output++) = c;
1202c609719bSwdenk 				outputcnt--;
1203c609719bSwdenk 			}
1204c609719bSwdenk 			break;
1205c609719bSwdenk 		case 1:	/* Waiting for (        */
12065cf91d6bSwdenk 			if (c == '(' || c == '{') {
1207c609719bSwdenk 				state++;
1208c609719bSwdenk 				varname_start = input;
1209c609719bSwdenk 			} else {
1210c609719bSwdenk 				state = 0;
1211c609719bSwdenk 				*(output++) = '$';
1212c609719bSwdenk 				outputcnt--;
1213c609719bSwdenk 
1214c609719bSwdenk 				if (outputcnt) {
1215c609719bSwdenk 					*(output++) = c;
1216c609719bSwdenk 					outputcnt--;
1217c609719bSwdenk 				}
1218c609719bSwdenk 			}
1219c609719bSwdenk 			break;
1220c609719bSwdenk 		case 2:	/* Waiting for )        */
12215cf91d6bSwdenk 			if (c == ')' || c == '}') {
1222c609719bSwdenk 				int i;
12236d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 				char envname[CONFIG_SYS_CBSIZE], *envval;
1224c609719bSwdenk 				int envcnt = input - varname_start - 1;	/* Varname # of chars */
1225c609719bSwdenk 
1226c609719bSwdenk 				/* Get the varname */
1227c609719bSwdenk 				for (i = 0; i < envcnt; i++) {
1228c609719bSwdenk 					envname[i] = varname_start[i];
1229c609719bSwdenk 				}
1230c609719bSwdenk 				envname[i] = 0;
1231c609719bSwdenk 
1232c609719bSwdenk 				/* Get its value */
1233c609719bSwdenk 				envval = getenv (envname);
1234c609719bSwdenk 
1235c609719bSwdenk 				/* Copy into the line if it exists */
1236c609719bSwdenk 				if (envval != NULL)
1237c609719bSwdenk 					while ((*envval) && outputcnt) {
1238c609719bSwdenk 						*(output++) = *(envval++);
1239c609719bSwdenk 						outputcnt--;
1240c609719bSwdenk 					}
1241c609719bSwdenk 				/* Look for another '$' */
1242c609719bSwdenk 				state = 0;
1243c609719bSwdenk 			}
1244c609719bSwdenk 			break;
1245a25f862bSwdenk 		case 3:	/* Waiting for '        */
1246a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1247a25f862bSwdenk 				state = 0;
1248a25f862bSwdenk 			} else {
1249a25f862bSwdenk 				*(output++) = c;
1250a25f862bSwdenk 				outputcnt--;
1251c609719bSwdenk 			}
1252a25f862bSwdenk 			break;
1253a25f862bSwdenk 		}
1254c609719bSwdenk 		prev = c;
1255c609719bSwdenk 	}
1256c609719bSwdenk 
1257c609719bSwdenk 	if (outputcnt)
1258c609719bSwdenk 		*output = 0;
12599160b96fSBartlomiej Sieka 	else
12609160b96fSBartlomiej Sieka 		*(output - 1) = 0;
1261c609719bSwdenk 
1262c609719bSwdenk #ifdef DEBUG_PARSER
1263c609719bSwdenk 	printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1264c609719bSwdenk 		strlen (output_start), output_start);
1265c609719bSwdenk #endif
1266c609719bSwdenk }
1267c609719bSwdenk 
1268c609719bSwdenk /****************************************************************************
1269c609719bSwdenk  * returns:
1270c609719bSwdenk  *	1  - command executed, repeatable
1271c609719bSwdenk  *	0  - command executed but not repeatable, interrupted commands are
1272c609719bSwdenk  *	     always considered not repeatable
1273c609719bSwdenk  *	-1 - not executed (unrecognized, bootd recursion or too many args)
12746d0f6bcfSJean-Christophe PLAGNIOL-VILLARD  *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1275c609719bSwdenk  *           considered unrecognized)
1276c609719bSwdenk  *
1277c609719bSwdenk  * WARNING:
1278c609719bSwdenk  *
1279c609719bSwdenk  * We must create a temporary copy of the command since the command we get
1280c609719bSwdenk  * may be the result from getenv(), which returns a pointer directly to
1281c609719bSwdenk  * the environment data, which may change magicly when the command we run
1282c609719bSwdenk  * creates or modifies environment variables (like "bootp" does).
1283c609719bSwdenk  */
1284c609719bSwdenk 
1285c609719bSwdenk int run_command (const char *cmd, int flag)
1286c609719bSwdenk {
1287c609719bSwdenk 	cmd_tbl_t *cmdtp;
12886d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
1289c609719bSwdenk 	char *token;			/* start of token in cmdbuf	*/
1290c609719bSwdenk 	char *sep;			/* end of token (separator) in cmdbuf */
12916d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char finaltoken[CONFIG_SYS_CBSIZE];
1292c609719bSwdenk 	char *str = cmdbuf;
12936d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
1294f07771ccSwdenk 	int argc, inquotes;
1295c609719bSwdenk 	int repeatable = 1;
1296f07771ccSwdenk 	int rc = 0;
1297c609719bSwdenk 
1298c609719bSwdenk #ifdef DEBUG_PARSER
1299c609719bSwdenk 	printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1300c609719bSwdenk 	puts (cmd ? cmd : "NULL");	/* use puts - string may be loooong */
1301c609719bSwdenk 	puts ("\"\n");
1302c609719bSwdenk #endif
1303c609719bSwdenk 
1304c609719bSwdenk 	clear_ctrlc();		/* forget any previous Control C */
1305c609719bSwdenk 
1306c609719bSwdenk 	if (!cmd || !*cmd) {
1307c609719bSwdenk 		return -1;	/* empty command */
1308c609719bSwdenk 	}
1309c609719bSwdenk 
13106d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1311c609719bSwdenk 		puts ("## Command too long!\n");
1312c609719bSwdenk 		return -1;
1313c609719bSwdenk 	}
1314c609719bSwdenk 
1315c609719bSwdenk 	strcpy (cmdbuf, cmd);
1316c609719bSwdenk 
1317c609719bSwdenk 	/* Process separators and check for invalid
1318c609719bSwdenk 	 * repeatable commands
1319c609719bSwdenk 	 */
1320c609719bSwdenk 
1321c609719bSwdenk #ifdef DEBUG_PARSER
1322c609719bSwdenk 	printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1323c609719bSwdenk #endif
1324c609719bSwdenk 	while (*str) {
1325c609719bSwdenk 
1326c609719bSwdenk 		/*
1327c609719bSwdenk 		 * Find separator, or string end
1328c609719bSwdenk 		 * Allow simple escape of ';' by writing "\;"
1329c609719bSwdenk 		 */
1330a25f862bSwdenk 		for (inquotes = 0, sep = str; *sep; sep++) {
1331a25f862bSwdenk 			if ((*sep=='\'') &&
1332a25f862bSwdenk 			    (*(sep-1) != '\\'))
1333a25f862bSwdenk 				inquotes=!inquotes;
1334a25f862bSwdenk 
1335a25f862bSwdenk 			if (!inquotes &&
1336a25f862bSwdenk 			    (*sep == ';') &&	/* separator		*/
1337c609719bSwdenk 			    ( sep != str) &&	/* past string start	*/
1338c609719bSwdenk 			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
1339c609719bSwdenk 				break;
1340c609719bSwdenk 		}
1341c609719bSwdenk 
1342c609719bSwdenk 		/*
1343c609719bSwdenk 		 * Limit the token to data between separators
1344c609719bSwdenk 		 */
1345c609719bSwdenk 		token = str;
1346c609719bSwdenk 		if (*sep) {
1347c609719bSwdenk 			str = sep + 1;	/* start of command for next pass */
1348c609719bSwdenk 			*sep = '\0';
1349c609719bSwdenk 		}
1350c609719bSwdenk 		else
1351c609719bSwdenk 			str = sep;	/* no more commands for next pass */
1352c609719bSwdenk #ifdef DEBUG_PARSER
1353c609719bSwdenk 		printf ("token: \"%s\"\n", token);
1354c609719bSwdenk #endif
1355c609719bSwdenk 
1356c609719bSwdenk 		/* find macros in this token and replace them */
1357c609719bSwdenk 		process_macros (token, finaltoken);
1358c609719bSwdenk 
1359c609719bSwdenk 		/* Extract arguments */
13601264b405SWolfgang Denk 		if ((argc = parse_line (finaltoken, argv)) == 0) {
13611264b405SWolfgang Denk 			rc = -1;	/* no command at all */
13621264b405SWolfgang Denk 			continue;
13631264b405SWolfgang Denk 		}
1364c609719bSwdenk 
1365c609719bSwdenk 		/* Look up command in command table */
1366c609719bSwdenk 		if ((cmdtp = find_cmd(argv[0])) == NULL) {
1367c609719bSwdenk 			printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1368f07771ccSwdenk 			rc = -1;	/* give up after bad command */
1369f07771ccSwdenk 			continue;
1370c609719bSwdenk 		}
1371c609719bSwdenk 
1372c609719bSwdenk 		/* found - check max args */
1373c609719bSwdenk 		if (argc > cmdtp->maxargs) {
137462c3ae7cSPeter Tyser 			cmd_usage(cmdtp);
1375f07771ccSwdenk 			rc = -1;
1376f07771ccSwdenk 			continue;
1377c609719bSwdenk 		}
1378c609719bSwdenk 
1379c3517f91SJon Loeliger #if defined(CONFIG_CMD_BOOTD)
1380c609719bSwdenk 		/* avoid "bootd" recursion */
1381c609719bSwdenk 		if (cmdtp->cmd == do_bootd) {
1382c609719bSwdenk #ifdef DEBUG_PARSER
1383c609719bSwdenk 			printf ("[%s]\n", finaltoken);
1384c609719bSwdenk #endif
1385c609719bSwdenk 			if (flag & CMD_FLAG_BOOTD) {
13864b9206edSwdenk 				puts ("'bootd' recursion detected\n");
1387f07771ccSwdenk 				rc = -1;
1388f07771ccSwdenk 				continue;
13891264b405SWolfgang Denk 			} else {
1390c609719bSwdenk 				flag |= CMD_FLAG_BOOTD;
1391c609719bSwdenk 			}
13921264b405SWolfgang Denk 		}
139390253178SJon Loeliger #endif
1394c609719bSwdenk 
1395c609719bSwdenk 		/* OK - call function to do the command */
1396c609719bSwdenk 		if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1397f07771ccSwdenk 			rc = -1;
1398c609719bSwdenk 		}
1399c609719bSwdenk 
1400c609719bSwdenk 		repeatable &= cmdtp->repeatable;
1401c609719bSwdenk 
1402c609719bSwdenk 		/* Did the user stop this? */
1403c609719bSwdenk 		if (had_ctrlc ())
14045afb2020SDetlev Zundel 			return -1;	/* if stopped then not repeatable */
1405c609719bSwdenk 	}
1406c609719bSwdenk 
1407f07771ccSwdenk 	return rc ? rc : repeatable;
1408c609719bSwdenk }
1409c609719bSwdenk 
1410c609719bSwdenk /****************************************************************************/
1411c609719bSwdenk 
1412c3517f91SJon Loeliger #if defined(CONFIG_CMD_RUN)
1413c609719bSwdenk int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1414c609719bSwdenk {
1415c609719bSwdenk 	int i;
1416c609719bSwdenk 
1417c609719bSwdenk 	if (argc < 2) {
141862c3ae7cSPeter Tyser 		cmd_usage(cmdtp);
1419c609719bSwdenk 		return 1;
1420c609719bSwdenk 	}
1421c609719bSwdenk 
1422c609719bSwdenk 	for (i=1; i<argc; ++i) {
14233e38691eSwdenk 		char *arg;
14243e38691eSwdenk 
14253e38691eSwdenk 		if ((arg = getenv (argv[i])) == NULL) {
14263e38691eSwdenk 			printf ("## Error: \"%s\" not defined\n", argv[i]);
14273e38691eSwdenk 			return 1;
14283e38691eSwdenk 		}
14296d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER
14303e38691eSwdenk 		if (run_command (arg, flag) == -1)
14313e38691eSwdenk 			return 1;
1432c609719bSwdenk #else
14333e38691eSwdenk 		if (parse_string_outer(arg,
14347aa78614Swdenk 		    FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
14353e38691eSwdenk 			return 1;
1436c609719bSwdenk #endif
1437c609719bSwdenk 	}
14383e38691eSwdenk 	return 0;
1439c609719bSwdenk }
144090253178SJon Loeliger #endif
1441