xref: /openbmc/u-boot/common/main.c (revision 54841ab50c20d6fa6c9cc3eb826989da3a22d934)
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) {}
515e2c08c3SEmil 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)
54*54841ab5SWolfgang Denk extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);		/* for do_reset() prototype */
558bde7f77Swdenk #endif
568bde7f77Swdenk 
57*54841ab5SWolfgang Denk extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char * const 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 
716475b9f9SJohn Schmoller char        console_buffer[CONFIG_SYS_CBSIZE + 1];	/* 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 
308bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
309bdccc4feSwdenk 	bootcount = bootcount_load();
310bdccc4feSwdenk 	bootcount++;
311bdccc4feSwdenk 	bootcount_store (bootcount);
312bdccc4feSwdenk 	sprintf (bcs_set, "%lu", bootcount);
313bdccc4feSwdenk 	setenv ("bootcount", bcs_set);
314bdccc4feSwdenk 	bcs = getenv ("bootlimit");
315bdccc4feSwdenk 	bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
316bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
317bdccc4feSwdenk 
318c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
319c609719bSwdenk 	debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
320c609719bSwdenk 	if (do_mdm_init) {
32177ddac94SWolfgang Denk 		char *str = strdup(getenv("mdm_cmd"));
322c609719bSwdenk 		setenv ("preboot", str);  /* set or delete definition */
323c609719bSwdenk 		if (str != NULL)
324c609719bSwdenk 			free (str);
325c609719bSwdenk 		mdm_init(); /* wait for modem connection */
326c609719bSwdenk 	}
327c609719bSwdenk #endif  /* CONFIG_MODEM_SUPPORT */
328c609719bSwdenk 
3290587597cSstroese #ifdef CONFIG_VERSION_VARIABLE
3300587597cSstroese 	{
3310587597cSstroese 		extern char version_string[];
3320587597cSstroese 
3330587597cSstroese 		setenv ("ver", version_string);  /* set version variable */
3340587597cSstroese 	}
3350587597cSstroese #endif /* CONFIG_VERSION_VARIABLE */
3360587597cSstroese 
3376d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
338c609719bSwdenk 	u_boot_hush_start ();
339c609719bSwdenk #endif
340c609719bSwdenk 
34181473f67SHeiko Schocher #if defined(CONFIG_HUSH_INIT_VAR)
34281473f67SHeiko Schocher 	hush_init_var ();
34381473f67SHeiko Schocher #endif
34481473f67SHeiko Schocher 
34504a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
34604a85b3bSwdenk 	install_auto_complete();
34704a85b3bSwdenk #endif
34804a85b3bSwdenk 
349c609719bSwdenk #ifdef CONFIG_PREBOOT
350c609719bSwdenk 	if ((p = getenv ("preboot")) != NULL) {
351c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
352c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
353c609719bSwdenk # endif
354c609719bSwdenk 
3556d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # ifndef CONFIG_SYS_HUSH_PARSER
356c609719bSwdenk 		run_command (p, 0);
357c609719bSwdenk # else
358c609719bSwdenk 		parse_string_outer(p, FLAG_PARSE_SEMICOLON |
359c609719bSwdenk 				    FLAG_EXIT_FROM_LOOP);
360c609719bSwdenk # endif
361c609719bSwdenk 
362c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
363c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
364c609719bSwdenk # endif
365c609719bSwdenk 	}
366c609719bSwdenk #endif /* CONFIG_PREBOOT */
367c609719bSwdenk 
368143cd21fSWolfgang Denk #if defined(CONFIG_UPDATE_TFTP)
369143cd21fSWolfgang Denk 	update_tftp ();
370143cd21fSWolfgang Denk #endif /* CONFIG_UPDATE_TFTP */
371143cd21fSWolfgang Denk 
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 */
431c7de829cSwdenk 
432c609719bSwdenk 	/*
433c609719bSwdenk 	 * Main Loop for Monitor Command Processing
434c609719bSwdenk 	 */
4356d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
436c609719bSwdenk 	parse_file_outer();
437c609719bSwdenk 	/* This point is never reached */
438c609719bSwdenk 	for (;;);
439c609719bSwdenk #else
440c609719bSwdenk 	for (;;) {
441c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
442c609719bSwdenk 		if (rc >= 0) {
443c609719bSwdenk 			/* Saw enough of a valid command to
444c609719bSwdenk 			 * restart the timeout.
445c609719bSwdenk 			 */
446c609719bSwdenk 			reset_cmd_timeout();
447c609719bSwdenk 		}
448c609719bSwdenk #endif
4496d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		len = readline (CONFIG_SYS_PROMPT);
450c609719bSwdenk 
451c609719bSwdenk 		flag = 0;	/* assume no special flags for now */
452c609719bSwdenk 		if (len > 0)
453c609719bSwdenk 			strcpy (lastcommand, console_buffer);
454c609719bSwdenk 		else if (len == 0)
455c609719bSwdenk 			flag |= CMD_FLAG_REPEAT;
456c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
457c609719bSwdenk 		else if (len == -2) {
458c609719bSwdenk 			/* -2 means timed out, retry autoboot
459c609719bSwdenk 			 */
4604b9206edSwdenk 			puts ("\nTimed out waiting for command\n");
461c609719bSwdenk # ifdef CONFIG_RESET_TO_RETRY
462c609719bSwdenk 			/* Reinit board to run initialization code again */
463c609719bSwdenk 			do_reset (NULL, 0, 0, NULL);
464c609719bSwdenk # else
465c609719bSwdenk 			return;		/* retry autoboot */
466c609719bSwdenk # endif
467c609719bSwdenk 		}
468c609719bSwdenk #endif
469c609719bSwdenk 
470c609719bSwdenk 		if (len == -1)
4714b9206edSwdenk 			puts ("<INTERRUPT>\n");
472c609719bSwdenk 		else
473c609719bSwdenk 			rc = run_command (lastcommand, flag);
474c609719bSwdenk 
475c609719bSwdenk 		if (rc <= 0) {
476c609719bSwdenk 			/* invalid command or not repeatable, forget it */
477c609719bSwdenk 			lastcommand[0] = 0;
478c609719bSwdenk 		}
479c609719bSwdenk 	}
4806d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /*CONFIG_SYS_HUSH_PARSER*/
481c609719bSwdenk }
482c609719bSwdenk 
4836dd652faSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
4846dd652faSwdenk /***************************************************************************
48519973b6aSWolfgang Denk  * initialize command line timeout
4866dd652faSwdenk  */
4876dd652faSwdenk void init_cmd_timeout(void)
4886dd652faSwdenk {
4896dd652faSwdenk 	char *s = getenv ("bootretry");
4906dd652faSwdenk 
4916dd652faSwdenk 	if (s != NULL)
492b028f715Swdenk 		retry_time = (int)simple_strtol(s, NULL, 10);
4936dd652faSwdenk 	else
4946dd652faSwdenk 		retry_time =  CONFIG_BOOT_RETRY_TIME;
4956dd652faSwdenk 
4966dd652faSwdenk 	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
4976dd652faSwdenk 		retry_time = CONFIG_BOOT_RETRY_MIN;
4986dd652faSwdenk }
4996dd652faSwdenk 
500c609719bSwdenk /***************************************************************************
501c609719bSwdenk  * reset command line timeout to retry_time seconds
502c609719bSwdenk  */
503c609719bSwdenk void reset_cmd_timeout(void)
504c609719bSwdenk {
505c609719bSwdenk 	endtime = endtick(retry_time);
506c609719bSwdenk }
507c609719bSwdenk #endif
508c609719bSwdenk 
509501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
510501090aaSWolfgang Denk 
511501090aaSWolfgang Denk /*
512501090aaSWolfgang Denk  * cmdline-editing related codes from vivi.
513501090aaSWolfgang Denk  * Author: Janghoon Lyu <nandy@mizi.com>
514501090aaSWolfgang Denk  */
515501090aaSWolfgang Denk 
516501090aaSWolfgang Denk #define putnstr(str,n)	do {			\
517dc4b0b38SAndrew Klossner 		printf ("%.*s", (int)n, str);	\
518501090aaSWolfgang Denk 	} while (0)
519501090aaSWolfgang Denk 
520501090aaSWolfgang Denk #define CTL_CH(c)		((c) - 'a' + 1)
521501090aaSWolfgang Denk 
522d6112950SJohn Schmoller #define MAX_CMDBUF_SIZE		CONFIG_SYS_CBSIZE
523501090aaSWolfgang Denk 
524501090aaSWolfgang Denk #define CTL_BACKSPACE		('\b')
525501090aaSWolfgang Denk #define DEL			((char)255)
526501090aaSWolfgang Denk #define DEL7			((char)127)
527501090aaSWolfgang Denk #define CREAD_HIST_CHAR		('!')
528501090aaSWolfgang Denk 
529501090aaSWolfgang Denk #define getcmd_putch(ch)	putc(ch)
530501090aaSWolfgang Denk #define getcmd_getch()		getc()
531501090aaSWolfgang Denk #define getcmd_cbeep()		getcmd_putch('\a')
532501090aaSWolfgang Denk 
533501090aaSWolfgang Denk #define HIST_MAX		20
534501090aaSWolfgang Denk #define HIST_SIZE		MAX_CMDBUF_SIZE
535501090aaSWolfgang Denk 
536501090aaSWolfgang Denk static int hist_max = 0;
537501090aaSWolfgang Denk static int hist_add_idx = 0;
538501090aaSWolfgang Denk static int hist_cur = -1;
539501090aaSWolfgang Denk unsigned hist_num = 0;
540501090aaSWolfgang Denk 
541501090aaSWolfgang Denk char* hist_list[HIST_MAX];
5426475b9f9SJohn Schmoller char hist_lines[HIST_MAX][HIST_SIZE + 1];	 /* Save room for NULL */
543501090aaSWolfgang Denk 
544501090aaSWolfgang Denk #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
545501090aaSWolfgang Denk 
546501090aaSWolfgang Denk static void hist_init(void)
547501090aaSWolfgang Denk {
548501090aaSWolfgang Denk 	int i;
549501090aaSWolfgang Denk 
550501090aaSWolfgang Denk 	hist_max = 0;
551501090aaSWolfgang Denk 	hist_add_idx = 0;
552501090aaSWolfgang Denk 	hist_cur = -1;
553501090aaSWolfgang Denk 	hist_num = 0;
554501090aaSWolfgang Denk 
555501090aaSWolfgang Denk 	for (i = 0; i < HIST_MAX; i++) {
556501090aaSWolfgang Denk 		hist_list[i] = hist_lines[i];
557501090aaSWolfgang Denk 		hist_list[i][0] = '\0';
558501090aaSWolfgang Denk 	}
559501090aaSWolfgang Denk }
560501090aaSWolfgang Denk 
561501090aaSWolfgang Denk static void cread_add_to_hist(char *line)
562501090aaSWolfgang Denk {
563501090aaSWolfgang Denk 	strcpy(hist_list[hist_add_idx], line);
564501090aaSWolfgang Denk 
565501090aaSWolfgang Denk 	if (++hist_add_idx >= HIST_MAX)
566501090aaSWolfgang Denk 		hist_add_idx = 0;
567501090aaSWolfgang Denk 
568501090aaSWolfgang Denk 	if (hist_add_idx > hist_max)
569501090aaSWolfgang Denk 		hist_max = hist_add_idx;
570501090aaSWolfgang Denk 
571501090aaSWolfgang Denk 	hist_num++;
572501090aaSWolfgang Denk }
573501090aaSWolfgang Denk 
574501090aaSWolfgang Denk static char* hist_prev(void)
575501090aaSWolfgang Denk {
576501090aaSWolfgang Denk 	char *ret;
577501090aaSWolfgang Denk 	int old_cur;
578501090aaSWolfgang Denk 
579501090aaSWolfgang Denk 	if (hist_cur < 0)
580501090aaSWolfgang Denk 		return NULL;
581501090aaSWolfgang Denk 
582501090aaSWolfgang Denk 	old_cur = hist_cur;
583501090aaSWolfgang Denk 	if (--hist_cur < 0)
584501090aaSWolfgang Denk 		hist_cur = hist_max;
585501090aaSWolfgang Denk 
586501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
587501090aaSWolfgang Denk 		hist_cur = old_cur;
588501090aaSWolfgang Denk 		ret = NULL;
589501090aaSWolfgang Denk 	} else
590501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
591501090aaSWolfgang Denk 
592501090aaSWolfgang Denk 	return (ret);
593501090aaSWolfgang Denk }
594501090aaSWolfgang Denk 
595501090aaSWolfgang Denk static char* hist_next(void)
596501090aaSWolfgang Denk {
597501090aaSWolfgang Denk 	char *ret;
598501090aaSWolfgang Denk 
599501090aaSWolfgang Denk 	if (hist_cur < 0)
600501090aaSWolfgang Denk 		return NULL;
601501090aaSWolfgang Denk 
602501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx)
603501090aaSWolfgang Denk 		return NULL;
604501090aaSWolfgang Denk 
605501090aaSWolfgang Denk 	if (++hist_cur > hist_max)
606501090aaSWolfgang Denk 		hist_cur = 0;
607501090aaSWolfgang Denk 
608501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
609501090aaSWolfgang Denk 		ret = "";
610501090aaSWolfgang Denk 	} else
611501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
612501090aaSWolfgang Denk 
613501090aaSWolfgang Denk 	return (ret);
614501090aaSWolfgang Denk }
615501090aaSWolfgang Denk 
6163ca9122fSStefan Roese #ifndef CONFIG_CMDLINE_EDITING
617501090aaSWolfgang Denk static void cread_print_hist_list(void)
618501090aaSWolfgang Denk {
619501090aaSWolfgang Denk 	int i;
620501090aaSWolfgang Denk 	unsigned long n;
621501090aaSWolfgang Denk 
622501090aaSWolfgang Denk 	n = hist_num - hist_max;
623501090aaSWolfgang Denk 
624501090aaSWolfgang Denk 	i = hist_add_idx + 1;
625501090aaSWolfgang Denk 	while (1) {
626501090aaSWolfgang Denk 		if (i > hist_max)
627501090aaSWolfgang Denk 			i = 0;
628501090aaSWolfgang Denk 		if (i == hist_add_idx)
629501090aaSWolfgang Denk 			break;
630501090aaSWolfgang Denk 		printf("%s\n", hist_list[i]);
631501090aaSWolfgang Denk 		n++;
632501090aaSWolfgang Denk 		i++;
633501090aaSWolfgang Denk 	}
634501090aaSWolfgang Denk }
6353ca9122fSStefan Roese #endif /* CONFIG_CMDLINE_EDITING */
636501090aaSWolfgang Denk 
637501090aaSWolfgang Denk #define BEGINNING_OF_LINE() {			\
638501090aaSWolfgang Denk 	while (num) {				\
639501090aaSWolfgang Denk 		getcmd_putch(CTL_BACKSPACE);	\
640501090aaSWolfgang Denk 		num--;				\
641501090aaSWolfgang Denk 	}					\
642501090aaSWolfgang Denk }
643501090aaSWolfgang Denk 
644501090aaSWolfgang Denk #define ERASE_TO_EOL() {				\
645501090aaSWolfgang Denk 	if (num < eol_num) {				\
646501090aaSWolfgang Denk 		int tmp;				\
647501090aaSWolfgang Denk 		for (tmp = num; tmp < eol_num; tmp++)	\
648501090aaSWolfgang Denk 			getcmd_putch(' ');		\
649501090aaSWolfgang Denk 		while (tmp-- > num)			\
650501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);	\
651501090aaSWolfgang Denk 		eol_num = num;				\
652501090aaSWolfgang Denk 	}						\
653501090aaSWolfgang Denk }
654501090aaSWolfgang Denk 
655501090aaSWolfgang Denk #define REFRESH_TO_EOL() {			\
656501090aaSWolfgang Denk 	if (num < eol_num) {			\
657501090aaSWolfgang Denk 		wlen = eol_num - num;		\
658501090aaSWolfgang Denk 		putnstr(buf + num, wlen);	\
659501090aaSWolfgang Denk 		num = eol_num;			\
660501090aaSWolfgang Denk 	}					\
661501090aaSWolfgang Denk }
662501090aaSWolfgang Denk 
663501090aaSWolfgang Denk static void cread_add_char(char ichar, int insert, unsigned long *num,
664501090aaSWolfgang Denk 	       unsigned long *eol_num, char *buf, unsigned long len)
665501090aaSWolfgang Denk {
666501090aaSWolfgang Denk 	unsigned long wlen;
667501090aaSWolfgang Denk 
668501090aaSWolfgang Denk 	/* room ??? */
669501090aaSWolfgang Denk 	if (insert || *num == *eol_num) {
670501090aaSWolfgang Denk 		if (*eol_num > len - 1) {
671501090aaSWolfgang Denk 			getcmd_cbeep();
672501090aaSWolfgang Denk 			return;
673501090aaSWolfgang Denk 		}
674501090aaSWolfgang Denk 		(*eol_num)++;
675501090aaSWolfgang Denk 	}
676501090aaSWolfgang Denk 
677501090aaSWolfgang Denk 	if (insert) {
678501090aaSWolfgang Denk 		wlen = *eol_num - *num;
679501090aaSWolfgang Denk 		if (wlen > 1) {
680501090aaSWolfgang Denk 			memmove(&buf[*num+1], &buf[*num], wlen-1);
681501090aaSWolfgang Denk 		}
682501090aaSWolfgang Denk 
683501090aaSWolfgang Denk 		buf[*num] = ichar;
684501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
685501090aaSWolfgang Denk 		(*num)++;
686501090aaSWolfgang Denk 		while (--wlen) {
687501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);
688501090aaSWolfgang Denk 		}
689501090aaSWolfgang Denk 	} else {
690501090aaSWolfgang Denk 		/* echo the character */
691501090aaSWolfgang Denk 		wlen = 1;
692501090aaSWolfgang Denk 		buf[*num] = ichar;
693501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
694501090aaSWolfgang Denk 		(*num)++;
695501090aaSWolfgang Denk 	}
696501090aaSWolfgang Denk }
697501090aaSWolfgang Denk 
698501090aaSWolfgang Denk static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
699501090aaSWolfgang Denk 	      unsigned long *eol_num, char *buf, unsigned long len)
700501090aaSWolfgang Denk {
701501090aaSWolfgang Denk 	while (strsize--) {
702501090aaSWolfgang Denk 		cread_add_char(*str, insert, num, eol_num, buf, len);
703501090aaSWolfgang Denk 		str++;
704501090aaSWolfgang Denk 	}
705501090aaSWolfgang Denk }
706501090aaSWolfgang Denk 
70723d0baf9SJean-Christophe PLAGNIOL-VILLARD static int cread_line(const char *const prompt, char *buf, unsigned int *len)
708501090aaSWolfgang Denk {
709501090aaSWolfgang Denk 	unsigned long num = 0;
710501090aaSWolfgang Denk 	unsigned long eol_num = 0;
711501090aaSWolfgang Denk 	unsigned long wlen;
712501090aaSWolfgang Denk 	char ichar;
713501090aaSWolfgang Denk 	int insert = 1;
714501090aaSWolfgang Denk 	int esc_len = 0;
715501090aaSWolfgang Denk 	char esc_save[8];
716ecc5500eSPeter Tyser 	int init_len = strlen(buf);
717ecc5500eSPeter Tyser 
718ecc5500eSPeter Tyser 	if (init_len)
719ecc5500eSPeter Tyser 		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
720501090aaSWolfgang Denk 
721501090aaSWolfgang Denk 	while (1) {
72200ac50e3SAndreas Engel #ifdef CONFIG_BOOT_RETRY_TIME
72300ac50e3SAndreas Engel 		while (!tstc()) {	/* while no incoming data */
72400ac50e3SAndreas Engel 			if (retry_time >= 0 && get_ticks() > endtime)
72500ac50e3SAndreas Engel 				return (-2);	/* timed out */
72630dc165aSJens Scharsig 			WATCHDOG_RESET();
72700ac50e3SAndreas Engel 		}
72800ac50e3SAndreas Engel #endif
72900ac50e3SAndreas Engel 
730501090aaSWolfgang Denk 		ichar = getcmd_getch();
731501090aaSWolfgang Denk 
732501090aaSWolfgang Denk 		if ((ichar == '\n') || (ichar == '\r')) {
733dd9f06f0SWolfgang Denk 			putc('\n');
734501090aaSWolfgang Denk 			break;
735501090aaSWolfgang Denk 		}
736501090aaSWolfgang Denk 
737501090aaSWolfgang Denk 		/*
738501090aaSWolfgang Denk 		 * handle standard linux xterm esc sequences for arrow key, etc.
739501090aaSWolfgang Denk 		 */
740501090aaSWolfgang Denk 		if (esc_len != 0) {
741501090aaSWolfgang Denk 			if (esc_len == 1) {
742501090aaSWolfgang Denk 				if (ichar == '[') {
743501090aaSWolfgang Denk 					esc_save[esc_len] = ichar;
744501090aaSWolfgang Denk 					esc_len = 2;
745501090aaSWolfgang Denk 				} else {
746501090aaSWolfgang Denk 					cread_add_str(esc_save, esc_len, insert,
747501090aaSWolfgang Denk 						      &num, &eol_num, buf, *len);
748501090aaSWolfgang Denk 					esc_len = 0;
749501090aaSWolfgang Denk 				}
750501090aaSWolfgang Denk 				continue;
751501090aaSWolfgang Denk 			}
752501090aaSWolfgang Denk 
753501090aaSWolfgang Denk 			switch (ichar) {
754501090aaSWolfgang Denk 
755501090aaSWolfgang Denk 			case 'D':	/* <- key */
756501090aaSWolfgang Denk 				ichar = CTL_CH('b');
757501090aaSWolfgang Denk 				esc_len = 0;
758501090aaSWolfgang Denk 				break;
759501090aaSWolfgang Denk 			case 'C':	/* -> key */
760501090aaSWolfgang Denk 				ichar = CTL_CH('f');
761501090aaSWolfgang Denk 				esc_len = 0;
762501090aaSWolfgang Denk 				break;	/* pass off to ^F handler */
763501090aaSWolfgang Denk 			case 'H':	/* Home key */
764501090aaSWolfgang Denk 				ichar = CTL_CH('a');
765501090aaSWolfgang Denk 				esc_len = 0;
766501090aaSWolfgang Denk 				break;	/* pass off to ^A handler */
767501090aaSWolfgang Denk 			case 'A':	/* up arrow */
768501090aaSWolfgang Denk 				ichar = CTL_CH('p');
769501090aaSWolfgang Denk 				esc_len = 0;
770501090aaSWolfgang Denk 				break;	/* pass off to ^P handler */
771501090aaSWolfgang Denk 			case 'B':	/* down arrow */
772501090aaSWolfgang Denk 				ichar = CTL_CH('n');
773501090aaSWolfgang Denk 				esc_len = 0;
774501090aaSWolfgang Denk 				break;	/* pass off to ^N handler */
775501090aaSWolfgang Denk 			default:
776501090aaSWolfgang Denk 				esc_save[esc_len++] = ichar;
777501090aaSWolfgang Denk 				cread_add_str(esc_save, esc_len, insert,
778501090aaSWolfgang Denk 					      &num, &eol_num, buf, *len);
779501090aaSWolfgang Denk 				esc_len = 0;
780501090aaSWolfgang Denk 				continue;
781501090aaSWolfgang Denk 			}
782501090aaSWolfgang Denk 		}
783501090aaSWolfgang Denk 
784501090aaSWolfgang Denk 		switch (ichar) {
785501090aaSWolfgang Denk 		case 0x1b:
786501090aaSWolfgang Denk 			if (esc_len == 0) {
787501090aaSWolfgang Denk 				esc_save[esc_len] = ichar;
788501090aaSWolfgang Denk 				esc_len = 1;
789501090aaSWolfgang Denk 			} else {
790dd9f06f0SWolfgang Denk 				puts("impossible condition #876\n");
791501090aaSWolfgang Denk 				esc_len = 0;
792501090aaSWolfgang Denk 			}
793501090aaSWolfgang Denk 			break;
794501090aaSWolfgang Denk 
795501090aaSWolfgang Denk 		case CTL_CH('a'):
796501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
797501090aaSWolfgang Denk 			break;
798501090aaSWolfgang Denk 		case CTL_CH('c'):	/* ^C - break */
799501090aaSWolfgang Denk 			*buf = '\0';	/* discard input */
800501090aaSWolfgang Denk 			return (-1);
801501090aaSWolfgang Denk 		case CTL_CH('f'):
802501090aaSWolfgang Denk 			if (num < eol_num) {
803501090aaSWolfgang Denk 				getcmd_putch(buf[num]);
804501090aaSWolfgang Denk 				num++;
805501090aaSWolfgang Denk 			}
806501090aaSWolfgang Denk 			break;
807501090aaSWolfgang Denk 		case CTL_CH('b'):
808501090aaSWolfgang Denk 			if (num) {
809501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
810501090aaSWolfgang Denk 				num--;
811501090aaSWolfgang Denk 			}
812501090aaSWolfgang Denk 			break;
813501090aaSWolfgang Denk 		case CTL_CH('d'):
814501090aaSWolfgang Denk 			if (num < eol_num) {
815501090aaSWolfgang Denk 				wlen = eol_num - num - 1;
816501090aaSWolfgang Denk 				if (wlen) {
817501090aaSWolfgang Denk 					memmove(&buf[num], &buf[num+1], wlen);
818501090aaSWolfgang Denk 					putnstr(buf + num, wlen);
819501090aaSWolfgang Denk 				}
820501090aaSWolfgang Denk 
821501090aaSWolfgang Denk 				getcmd_putch(' ');
822501090aaSWolfgang Denk 				do {
823501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
824501090aaSWolfgang Denk 				} while (wlen--);
825501090aaSWolfgang Denk 				eol_num--;
826501090aaSWolfgang Denk 			}
827501090aaSWolfgang Denk 			break;
828501090aaSWolfgang Denk 		case CTL_CH('k'):
829501090aaSWolfgang Denk 			ERASE_TO_EOL();
830501090aaSWolfgang Denk 			break;
831501090aaSWolfgang Denk 		case CTL_CH('e'):
832501090aaSWolfgang Denk 			REFRESH_TO_EOL();
833501090aaSWolfgang Denk 			break;
834501090aaSWolfgang Denk 		case CTL_CH('o'):
835501090aaSWolfgang Denk 			insert = !insert;
836501090aaSWolfgang Denk 			break;
837501090aaSWolfgang Denk 		case CTL_CH('x'):
83823d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case CTL_CH('u'):
839501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
840501090aaSWolfgang Denk 			ERASE_TO_EOL();
841501090aaSWolfgang Denk 			break;
842501090aaSWolfgang Denk 		case DEL:
843501090aaSWolfgang Denk 		case DEL7:
844501090aaSWolfgang Denk 		case 8:
845501090aaSWolfgang Denk 			if (num) {
846501090aaSWolfgang Denk 				wlen = eol_num - num;
847501090aaSWolfgang Denk 				num--;
848501090aaSWolfgang Denk 				memmove(&buf[num], &buf[num+1], wlen);
849501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
850501090aaSWolfgang Denk 				putnstr(buf + num, wlen);
851501090aaSWolfgang Denk 				getcmd_putch(' ');
852501090aaSWolfgang Denk 				do {
853501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
854501090aaSWolfgang Denk 				} while (wlen--);
855501090aaSWolfgang Denk 				eol_num--;
856501090aaSWolfgang Denk 			}
857501090aaSWolfgang Denk 			break;
858501090aaSWolfgang Denk 		case CTL_CH('p'):
859501090aaSWolfgang Denk 		case CTL_CH('n'):
860501090aaSWolfgang Denk 		{
861501090aaSWolfgang Denk 			char * hline;
862501090aaSWolfgang Denk 
863501090aaSWolfgang Denk 			esc_len = 0;
864501090aaSWolfgang Denk 
865501090aaSWolfgang Denk 			if (ichar == CTL_CH('p'))
866501090aaSWolfgang Denk 				hline = hist_prev();
867501090aaSWolfgang Denk 			else
868501090aaSWolfgang Denk 				hline = hist_next();
869501090aaSWolfgang Denk 
870501090aaSWolfgang Denk 			if (!hline) {
871501090aaSWolfgang Denk 				getcmd_cbeep();
872501090aaSWolfgang Denk 				continue;
873501090aaSWolfgang Denk 			}
874501090aaSWolfgang Denk 
875501090aaSWolfgang Denk 			/* nuke the current line */
876501090aaSWolfgang Denk 			/* first, go home */
877501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
878501090aaSWolfgang Denk 
879501090aaSWolfgang Denk 			/* erase to end of line */
880501090aaSWolfgang Denk 			ERASE_TO_EOL();
881501090aaSWolfgang Denk 
882501090aaSWolfgang Denk 			/* copy new line into place and display */
883501090aaSWolfgang Denk 			strcpy(buf, hline);
884501090aaSWolfgang Denk 			eol_num = strlen(buf);
885501090aaSWolfgang Denk 			REFRESH_TO_EOL();
886501090aaSWolfgang Denk 			continue;
887501090aaSWolfgang Denk 		}
88823d0baf9SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_AUTO_COMPLETE
88923d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case '\t': {
89023d0baf9SJean-Christophe PLAGNIOL-VILLARD 			int num2, col;
89123d0baf9SJean-Christophe PLAGNIOL-VILLARD 
89223d0baf9SJean-Christophe PLAGNIOL-VILLARD 			/* do not autocomplete when in the middle */
89323d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (num < eol_num) {
89423d0baf9SJean-Christophe PLAGNIOL-VILLARD 				getcmd_cbeep();
89523d0baf9SJean-Christophe PLAGNIOL-VILLARD 				break;
89623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
89723d0baf9SJean-Christophe PLAGNIOL-VILLARD 
89823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			buf[num] = '\0';
89923d0baf9SJean-Christophe PLAGNIOL-VILLARD 			col = strlen(prompt) + eol_num;
90023d0baf9SJean-Christophe PLAGNIOL-VILLARD 			num2 = num;
90123d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
90223d0baf9SJean-Christophe PLAGNIOL-VILLARD 				col = num2 - num;
90323d0baf9SJean-Christophe PLAGNIOL-VILLARD 				num += col;
90423d0baf9SJean-Christophe PLAGNIOL-VILLARD 				eol_num += col;
90523d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
90623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			break;
90723d0baf9SJean-Christophe PLAGNIOL-VILLARD 		}
90823d0baf9SJean-Christophe PLAGNIOL-VILLARD #endif
909501090aaSWolfgang Denk 		default:
910501090aaSWolfgang Denk 			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
911501090aaSWolfgang Denk 			break;
912501090aaSWolfgang Denk 		}
913501090aaSWolfgang Denk 	}
914501090aaSWolfgang Denk 	*len = eol_num;
915501090aaSWolfgang Denk 	buf[eol_num] = '\0';	/* lose the newline */
916501090aaSWolfgang Denk 
917501090aaSWolfgang Denk 	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
918501090aaSWolfgang Denk 		cread_add_to_hist(buf);
919501090aaSWolfgang Denk 	hist_cur = hist_add_idx;
920501090aaSWolfgang Denk 
921f9239438SPeter Tyser 	return 0;
922501090aaSWolfgang Denk }
923501090aaSWolfgang Denk 
924501090aaSWolfgang Denk #endif /* CONFIG_CMDLINE_EDITING */
925501090aaSWolfgang Denk 
926c609719bSwdenk /****************************************************************************/
927c609719bSwdenk 
928c609719bSwdenk /*
929c609719bSwdenk  * Prompt for input and read a line.
930c609719bSwdenk  * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
931c609719bSwdenk  * time out when time goes past endtime (timebase time in ticks).
932c609719bSwdenk  * Return:	number of read characters
933c609719bSwdenk  *		-1 if break
934c609719bSwdenk  *		-2 if timed out
935c609719bSwdenk  */
936c609719bSwdenk int readline (const char *const prompt)
937c609719bSwdenk {
938ecc5500eSPeter Tyser 	/*
939ecc5500eSPeter Tyser 	 * If console_buffer isn't 0-length the user will be prompted to modify
940ecc5500eSPeter Tyser 	 * it instead of entering it from scratch as desired.
941ecc5500eSPeter Tyser 	 */
942ecc5500eSPeter Tyser 	console_buffer[0] = '\0';
943ecc5500eSPeter Tyser 
9446636b62aSJames Yang 	return readline_into_buffer(prompt, console_buffer);
9456636b62aSJames Yang }
9466636b62aSJames Yang 
9476636b62aSJames Yang 
9486636b62aSJames Yang int readline_into_buffer (const char *const prompt, char * buffer)
9496636b62aSJames Yang {
9506636b62aSJames Yang 	char *p = buffer;
951501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
952501090aaSWolfgang Denk 	unsigned int len=MAX_CMDBUF_SIZE;
953d8f961bbSStefan Roese 	int rc;
954501090aaSWolfgang Denk 	static int initted = 0;
955501090aaSWolfgang Denk 
956597f6c26SJames Yang 	/*
957597f6c26SJames Yang 	 * History uses a global array which is not
958597f6c26SJames Yang 	 * writable until after relocation to RAM.
959597f6c26SJames Yang 	 * Revert to non-history version if still
960597f6c26SJames Yang 	 * running from flash.
961597f6c26SJames Yang 	 */
962597f6c26SJames Yang 	if (gd->flags & GD_FLG_RELOC) {
963597f6c26SJames Yang 		if (!initted) {
964597f6c26SJames Yang 			hist_init();
965597f6c26SJames Yang 			initted = 1;
966597f6c26SJames Yang 		}
967597f6c26SJames Yang 
968e491a71eSPeter Tyser 		if (prompt)
969dd9f06f0SWolfgang Denk 			puts (prompt);
970501090aaSWolfgang Denk 
97123d0baf9SJean-Christophe PLAGNIOL-VILLARD 		rc = cread_line(prompt, p, &len);
972d8f961bbSStefan Roese 		return rc < 0 ? rc : len;
973597f6c26SJames Yang 
974597f6c26SJames Yang 	} else {
975597f6c26SJames Yang #endif	/* CONFIG_CMDLINE_EDITING */
9760ec59524SKumar Gala 	char * p_buf = p;
977c609719bSwdenk 	int	n = 0;				/* buffer index		*/
978c609719bSwdenk 	int	plen = 0;			/* prompt length	*/
979c609719bSwdenk 	int	col;				/* output column cnt	*/
980c609719bSwdenk 	char	c;
981c609719bSwdenk 
982c609719bSwdenk 	/* print prompt */
983c609719bSwdenk 	if (prompt) {
984c609719bSwdenk 		plen = strlen (prompt);
985c609719bSwdenk 		puts (prompt);
986c609719bSwdenk 	}
987c609719bSwdenk 	col = plen;
988c609719bSwdenk 
989c609719bSwdenk 	for (;;) {
990c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
991c609719bSwdenk 		while (!tstc()) {	/* while no incoming data */
992c609719bSwdenk 			if (retry_time >= 0 && get_ticks() > endtime)
993c609719bSwdenk 				return (-2);	/* timed out */
99430dc165aSJens Scharsig 			WATCHDOG_RESET();
995c609719bSwdenk 		}
996c609719bSwdenk #endif
997c609719bSwdenk 		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
998c609719bSwdenk 
999c609719bSwdenk #ifdef CONFIG_SHOW_ACTIVITY
1000c609719bSwdenk 		while (!tstc()) {
1001c609719bSwdenk 			extern void show_activity(int arg);
1002c609719bSwdenk 			show_activity(0);
100330dc165aSJens Scharsig 			WATCHDOG_RESET();
1004c609719bSwdenk 		}
1005c609719bSwdenk #endif
1006c609719bSwdenk 		c = getc();
1007c609719bSwdenk 
1008c609719bSwdenk 		/*
1009c609719bSwdenk 		 * Special character handling
1010c609719bSwdenk 		 */
1011c609719bSwdenk 		switch (c) {
1012c609719bSwdenk 		case '\r':				/* Enter		*/
1013c609719bSwdenk 		case '\n':
1014c609719bSwdenk 			*p = '\0';
1015c609719bSwdenk 			puts ("\r\n");
10166636b62aSJames Yang 			return (p - p_buf);
1017c609719bSwdenk 
101827aa8186Swdenk 		case '\0':				/* nul			*/
101927aa8186Swdenk 			continue;
102027aa8186Swdenk 
1021c609719bSwdenk 		case 0x03:				/* ^C - break		*/
10226636b62aSJames Yang 			p_buf[0] = '\0';	/* discard input */
1023c609719bSwdenk 			return (-1);
1024c609719bSwdenk 
1025c609719bSwdenk 		case 0x15:				/* ^U - erase line	*/
1026c609719bSwdenk 			while (col > plen) {
1027c609719bSwdenk 				puts (erase_seq);
1028c609719bSwdenk 				--col;
1029c609719bSwdenk 			}
10306636b62aSJames Yang 			p = p_buf;
1031c609719bSwdenk 			n = 0;
1032c609719bSwdenk 			continue;
1033c609719bSwdenk 
1034c609719bSwdenk 		case 0x17:				/* ^W - erase word	*/
10356636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1036c609719bSwdenk 			while ((n > 0) && (*p != ' ')) {
10376636b62aSJames Yang 				p=delete_char(p_buf, p, &col, &n, plen);
1038c609719bSwdenk 			}
1039c609719bSwdenk 			continue;
1040c609719bSwdenk 
1041c609719bSwdenk 		case 0x08:				/* ^H  - backspace	*/
1042c609719bSwdenk 		case 0x7F:				/* DEL - backspace	*/
10436636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1044c609719bSwdenk 			continue;
1045c609719bSwdenk 
1046c609719bSwdenk 		default:
1047c609719bSwdenk 			/*
1048c609719bSwdenk 			 * Must be a normal character then
1049c609719bSwdenk 			 */
10506d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 			if (n < CONFIG_SYS_CBSIZE-2) {
1051c609719bSwdenk 				if (c == '\t') {	/* expand TABs		*/
105204a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
105304a85b3bSwdenk 					/* if auto completion triggered just continue */
105404a85b3bSwdenk 					*p = '\0';
105504a85b3bSwdenk 					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
10566636b62aSJames Yang 						p = p_buf + n;	/* reset */
105704a85b3bSwdenk 						continue;
105804a85b3bSwdenk 					}
105904a85b3bSwdenk #endif
1060c609719bSwdenk 					puts (tab_seq+(col&07));
1061c609719bSwdenk 					col += 8 - (col&07);
1062c609719bSwdenk 				} else {
1063c609719bSwdenk 					++col;		/* echo input		*/
1064c609719bSwdenk 					putc (c);
1065c609719bSwdenk 				}
1066c609719bSwdenk 				*p++ = c;
1067c609719bSwdenk 				++n;
1068c609719bSwdenk 			} else {			/* Buffer full		*/
1069c609719bSwdenk 				putc ('\a');
1070c609719bSwdenk 			}
1071c609719bSwdenk 		}
1072c609719bSwdenk 	}
1073597f6c26SJames Yang #ifdef CONFIG_CMDLINE_EDITING
1074597f6c26SJames Yang 	}
1075597f6c26SJames Yang #endif
1076c609719bSwdenk }
1077c609719bSwdenk 
1078c609719bSwdenk /****************************************************************************/
1079c609719bSwdenk 
1080c609719bSwdenk static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1081c609719bSwdenk {
1082c609719bSwdenk 	char *s;
1083c609719bSwdenk 
1084c609719bSwdenk 	if (*np == 0) {
1085c609719bSwdenk 		return (p);
1086c609719bSwdenk 	}
1087c609719bSwdenk 
1088c609719bSwdenk 	if (*(--p) == '\t') {			/* will retype the whole line	*/
1089c609719bSwdenk 		while (*colp > plen) {
1090c609719bSwdenk 			puts (erase_seq);
1091c609719bSwdenk 			(*colp)--;
1092c609719bSwdenk 		}
1093c609719bSwdenk 		for (s=buffer; s<p; ++s) {
1094c609719bSwdenk 			if (*s == '\t') {
1095c609719bSwdenk 				puts (tab_seq+((*colp) & 07));
1096c609719bSwdenk 				*colp += 8 - ((*colp) & 07);
1097c609719bSwdenk 			} else {
1098c609719bSwdenk 				++(*colp);
1099c609719bSwdenk 				putc (*s);
1100c609719bSwdenk 			}
1101c609719bSwdenk 		}
1102c609719bSwdenk 	} else {
1103c609719bSwdenk 		puts (erase_seq);
1104c609719bSwdenk 		(*colp)--;
1105c609719bSwdenk 	}
1106c609719bSwdenk 	(*np)--;
1107c609719bSwdenk 	return (p);
1108c609719bSwdenk }
1109c609719bSwdenk 
1110c609719bSwdenk /****************************************************************************/
1111c609719bSwdenk 
1112c609719bSwdenk int parse_line (char *line, char *argv[])
1113c609719bSwdenk {
1114c609719bSwdenk 	int nargs = 0;
1115c609719bSwdenk 
1116c609719bSwdenk #ifdef DEBUG_PARSER
1117c609719bSwdenk 	printf ("parse_line: \"%s\"\n", line);
1118c609719bSwdenk #endif
11196d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	while (nargs < CONFIG_SYS_MAXARGS) {
1120c609719bSwdenk 
1121c609719bSwdenk 		/* skip any white space */
1122c609719bSwdenk 		while ((*line == ' ') || (*line == '\t')) {
1123c609719bSwdenk 			++line;
1124c609719bSwdenk 		}
1125c609719bSwdenk 
1126c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1127c609719bSwdenk 			argv[nargs] = NULL;
1128c609719bSwdenk #ifdef DEBUG_PARSER
1129c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1130c609719bSwdenk #endif
1131c609719bSwdenk 			return (nargs);
1132c609719bSwdenk 		}
1133c609719bSwdenk 
1134c609719bSwdenk 		argv[nargs++] = line;	/* begin of argument string	*/
1135c609719bSwdenk 
1136c609719bSwdenk 		/* find end of string */
1137c609719bSwdenk 		while (*line && (*line != ' ') && (*line != '\t')) {
1138c609719bSwdenk 			++line;
1139c609719bSwdenk 		}
1140c609719bSwdenk 
1141c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1142c609719bSwdenk 			argv[nargs] = NULL;
1143c609719bSwdenk #ifdef DEBUG_PARSER
1144c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1145c609719bSwdenk #endif
1146c609719bSwdenk 			return (nargs);
1147c609719bSwdenk 		}
1148c609719bSwdenk 
1149c609719bSwdenk 		*line++ = '\0';		/* terminate current arg	 */
1150c609719bSwdenk 	}
1151c609719bSwdenk 
11526d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1153c609719bSwdenk 
1154c609719bSwdenk #ifdef DEBUG_PARSER
1155c609719bSwdenk 	printf ("parse_line: nargs=%d\n", nargs);
1156c609719bSwdenk #endif
1157c609719bSwdenk 	return (nargs);
1158c609719bSwdenk }
1159c609719bSwdenk 
1160c609719bSwdenk /****************************************************************************/
1161c609719bSwdenk 
1162c609719bSwdenk static void process_macros (const char *input, char *output)
1163c609719bSwdenk {
1164c609719bSwdenk 	char c, prev;
1165c609719bSwdenk 	const char *varname_start = NULL;
1166c609719bSwdenk 	int inputcnt = strlen (input);
11676d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	int outputcnt = CONFIG_SYS_CBSIZE;
1168c609719bSwdenk 	int state = 0;		/* 0 = waiting for '$'  */
116919973b6aSWolfgang Denk 
11705cf91d6bSwdenk 	/* 1 = waiting for '(' or '{' */
11715cf91d6bSwdenk 	/* 2 = waiting for ')' or '}' */
1172a25f862bSwdenk 	/* 3 = waiting for '''  */
1173c609719bSwdenk #ifdef DEBUG_PARSER
1174c609719bSwdenk 	char *output_start = output;
1175c609719bSwdenk 
117619973b6aSWolfgang Denk 	printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
117719973b6aSWolfgang Denk 		input);
1178c609719bSwdenk #endif
1179c609719bSwdenk 
1180c609719bSwdenk 	prev = '\0';		/* previous character   */
1181c609719bSwdenk 
1182c609719bSwdenk 	while (inputcnt && outputcnt) {
1183c609719bSwdenk 		c = *input++;
1184c609719bSwdenk 		inputcnt--;
1185c609719bSwdenk 
1186a25f862bSwdenk 		if (state != 3) {
1187c609719bSwdenk 			/* remove one level of escape characters */
1188c609719bSwdenk 			if ((c == '\\') && (prev != '\\')) {
1189c609719bSwdenk 				if (inputcnt-- == 0)
1190c609719bSwdenk 					break;
1191c609719bSwdenk 				prev = c;
1192c609719bSwdenk 				c = *input++;
1193c609719bSwdenk 			}
1194a25f862bSwdenk 		}
1195c609719bSwdenk 
1196c609719bSwdenk 		switch (state) {
1197c609719bSwdenk 		case 0:	/* Waiting for (unescaped) $    */
1198a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1199a25f862bSwdenk 				state = 3;
1200a25f862bSwdenk 				break;
1201a25f862bSwdenk 			}
1202c609719bSwdenk 			if ((c == '$') && (prev != '\\')) {
1203c609719bSwdenk 				state++;
1204c609719bSwdenk 			} else {
1205c609719bSwdenk 				*(output++) = c;
1206c609719bSwdenk 				outputcnt--;
1207c609719bSwdenk 			}
1208c609719bSwdenk 			break;
1209c609719bSwdenk 		case 1:	/* Waiting for (        */
12105cf91d6bSwdenk 			if (c == '(' || c == '{') {
1211c609719bSwdenk 				state++;
1212c609719bSwdenk 				varname_start = input;
1213c609719bSwdenk 			} else {
1214c609719bSwdenk 				state = 0;
1215c609719bSwdenk 				*(output++) = '$';
1216c609719bSwdenk 				outputcnt--;
1217c609719bSwdenk 
1218c609719bSwdenk 				if (outputcnt) {
1219c609719bSwdenk 					*(output++) = c;
1220c609719bSwdenk 					outputcnt--;
1221c609719bSwdenk 				}
1222c609719bSwdenk 			}
1223c609719bSwdenk 			break;
1224c609719bSwdenk 		case 2:	/* Waiting for )        */
12255cf91d6bSwdenk 			if (c == ')' || c == '}') {
1226c609719bSwdenk 				int i;
12276d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 				char envname[CONFIG_SYS_CBSIZE], *envval;
1228c609719bSwdenk 				int envcnt = input - varname_start - 1;	/* Varname # of chars */
1229c609719bSwdenk 
1230c609719bSwdenk 				/* Get the varname */
1231c609719bSwdenk 				for (i = 0; i < envcnt; i++) {
1232c609719bSwdenk 					envname[i] = varname_start[i];
1233c609719bSwdenk 				}
1234c609719bSwdenk 				envname[i] = 0;
1235c609719bSwdenk 
1236c609719bSwdenk 				/* Get its value */
1237c609719bSwdenk 				envval = getenv (envname);
1238c609719bSwdenk 
1239c609719bSwdenk 				/* Copy into the line if it exists */
1240c609719bSwdenk 				if (envval != NULL)
1241c609719bSwdenk 					while ((*envval) && outputcnt) {
1242c609719bSwdenk 						*(output++) = *(envval++);
1243c609719bSwdenk 						outputcnt--;
1244c609719bSwdenk 					}
1245c609719bSwdenk 				/* Look for another '$' */
1246c609719bSwdenk 				state = 0;
1247c609719bSwdenk 			}
1248c609719bSwdenk 			break;
1249a25f862bSwdenk 		case 3:	/* Waiting for '        */
1250a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1251a25f862bSwdenk 				state = 0;
1252a25f862bSwdenk 			} else {
1253a25f862bSwdenk 				*(output++) = c;
1254a25f862bSwdenk 				outputcnt--;
1255c609719bSwdenk 			}
1256a25f862bSwdenk 			break;
1257a25f862bSwdenk 		}
1258c609719bSwdenk 		prev = c;
1259c609719bSwdenk 	}
1260c609719bSwdenk 
1261c609719bSwdenk 	if (outputcnt)
1262c609719bSwdenk 		*output = 0;
12639160b96fSBartlomiej Sieka 	else
12649160b96fSBartlomiej Sieka 		*(output - 1) = 0;
1265c609719bSwdenk 
1266c609719bSwdenk #ifdef DEBUG_PARSER
1267c609719bSwdenk 	printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1268c609719bSwdenk 		strlen (output_start), output_start);
1269c609719bSwdenk #endif
1270c609719bSwdenk }
1271c609719bSwdenk 
1272c609719bSwdenk /****************************************************************************
1273c609719bSwdenk  * returns:
1274c609719bSwdenk  *	1  - command executed, repeatable
1275c609719bSwdenk  *	0  - command executed but not repeatable, interrupted commands are
1276c609719bSwdenk  *	     always considered not repeatable
1277c609719bSwdenk  *	-1 - not executed (unrecognized, bootd recursion or too many args)
12786d0f6bcfSJean-Christophe PLAGNIOL-VILLARD  *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1279c609719bSwdenk  *           considered unrecognized)
1280c609719bSwdenk  *
1281c609719bSwdenk  * WARNING:
1282c609719bSwdenk  *
1283c609719bSwdenk  * We must create a temporary copy of the command since the command we get
1284c609719bSwdenk  * may be the result from getenv(), which returns a pointer directly to
1285c609719bSwdenk  * the environment data, which may change magicly when the command we run
1286c609719bSwdenk  * creates or modifies environment variables (like "bootp" does).
1287c609719bSwdenk  */
1288c609719bSwdenk 
1289c609719bSwdenk int run_command (const char *cmd, int flag)
1290c609719bSwdenk {
1291c609719bSwdenk 	cmd_tbl_t *cmdtp;
12926d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
1293c609719bSwdenk 	char *token;			/* start of token in cmdbuf	*/
1294c609719bSwdenk 	char *sep;			/* end of token (separator) in cmdbuf */
12956d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char finaltoken[CONFIG_SYS_CBSIZE];
1296c609719bSwdenk 	char *str = cmdbuf;
12976d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
1298f07771ccSwdenk 	int argc, inquotes;
1299c609719bSwdenk 	int repeatable = 1;
1300f07771ccSwdenk 	int rc = 0;
1301c609719bSwdenk 
1302c609719bSwdenk #ifdef DEBUG_PARSER
1303c609719bSwdenk 	printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1304c609719bSwdenk 	puts (cmd ? cmd : "NULL");	/* use puts - string may be loooong */
1305c609719bSwdenk 	puts ("\"\n");
1306c609719bSwdenk #endif
1307c609719bSwdenk 
1308c609719bSwdenk 	clear_ctrlc();		/* forget any previous Control C */
1309c609719bSwdenk 
1310c609719bSwdenk 	if (!cmd || !*cmd) {
1311c609719bSwdenk 		return -1;	/* empty command */
1312c609719bSwdenk 	}
1313c609719bSwdenk 
13146d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1315c609719bSwdenk 		puts ("## Command too long!\n");
1316c609719bSwdenk 		return -1;
1317c609719bSwdenk 	}
1318c609719bSwdenk 
1319c609719bSwdenk 	strcpy (cmdbuf, cmd);
1320c609719bSwdenk 
1321c609719bSwdenk 	/* Process separators and check for invalid
1322c609719bSwdenk 	 * repeatable commands
1323c609719bSwdenk 	 */
1324c609719bSwdenk 
1325c609719bSwdenk #ifdef DEBUG_PARSER
1326c609719bSwdenk 	printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1327c609719bSwdenk #endif
1328c609719bSwdenk 	while (*str) {
1329c609719bSwdenk 
1330c609719bSwdenk 		/*
1331c609719bSwdenk 		 * Find separator, or string end
1332c609719bSwdenk 		 * Allow simple escape of ';' by writing "\;"
1333c609719bSwdenk 		 */
1334a25f862bSwdenk 		for (inquotes = 0, sep = str; *sep; sep++) {
1335a25f862bSwdenk 			if ((*sep=='\'') &&
1336a25f862bSwdenk 			    (*(sep-1) != '\\'))
1337a25f862bSwdenk 				inquotes=!inquotes;
1338a25f862bSwdenk 
1339a25f862bSwdenk 			if (!inquotes &&
1340a25f862bSwdenk 			    (*sep == ';') &&	/* separator		*/
1341c609719bSwdenk 			    ( sep != str) &&	/* past string start	*/
1342c609719bSwdenk 			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
1343c609719bSwdenk 				break;
1344c609719bSwdenk 		}
1345c609719bSwdenk 
1346c609719bSwdenk 		/*
1347c609719bSwdenk 		 * Limit the token to data between separators
1348c609719bSwdenk 		 */
1349c609719bSwdenk 		token = str;
1350c609719bSwdenk 		if (*sep) {
1351c609719bSwdenk 			str = sep + 1;	/* start of command for next pass */
1352c609719bSwdenk 			*sep = '\0';
1353c609719bSwdenk 		}
1354c609719bSwdenk 		else
1355c609719bSwdenk 			str = sep;	/* no more commands for next pass */
1356c609719bSwdenk #ifdef DEBUG_PARSER
1357c609719bSwdenk 		printf ("token: \"%s\"\n", token);
1358c609719bSwdenk #endif
1359c609719bSwdenk 
1360c609719bSwdenk 		/* find macros in this token and replace them */
1361c609719bSwdenk 		process_macros (token, finaltoken);
1362c609719bSwdenk 
1363c609719bSwdenk 		/* Extract arguments */
13641264b405SWolfgang Denk 		if ((argc = parse_line (finaltoken, argv)) == 0) {
13651264b405SWolfgang Denk 			rc = -1;	/* no command at all */
13661264b405SWolfgang Denk 			continue;
13671264b405SWolfgang Denk 		}
1368c609719bSwdenk 
1369c609719bSwdenk 		/* Look up command in command table */
1370c609719bSwdenk 		if ((cmdtp = find_cmd(argv[0])) == NULL) {
1371c609719bSwdenk 			printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1372f07771ccSwdenk 			rc = -1;	/* give up after bad command */
1373f07771ccSwdenk 			continue;
1374c609719bSwdenk 		}
1375c609719bSwdenk 
1376c609719bSwdenk 		/* found - check max args */
1377c609719bSwdenk 		if (argc > cmdtp->maxargs) {
137862c3ae7cSPeter Tyser 			cmd_usage(cmdtp);
1379f07771ccSwdenk 			rc = -1;
1380f07771ccSwdenk 			continue;
1381c609719bSwdenk 		}
1382c609719bSwdenk 
1383c3517f91SJon Loeliger #if defined(CONFIG_CMD_BOOTD)
1384c609719bSwdenk 		/* avoid "bootd" recursion */
1385c609719bSwdenk 		if (cmdtp->cmd == do_bootd) {
1386c609719bSwdenk #ifdef DEBUG_PARSER
1387c609719bSwdenk 			printf ("[%s]\n", finaltoken);
1388c609719bSwdenk #endif
1389c609719bSwdenk 			if (flag & CMD_FLAG_BOOTD) {
13904b9206edSwdenk 				puts ("'bootd' recursion detected\n");
1391f07771ccSwdenk 				rc = -1;
1392f07771ccSwdenk 				continue;
13931264b405SWolfgang Denk 			} else {
1394c609719bSwdenk 				flag |= CMD_FLAG_BOOTD;
1395c609719bSwdenk 			}
13961264b405SWolfgang Denk 		}
139790253178SJon Loeliger #endif
1398c609719bSwdenk 
1399c609719bSwdenk 		/* OK - call function to do the command */
1400c609719bSwdenk 		if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1401f07771ccSwdenk 			rc = -1;
1402c609719bSwdenk 		}
1403c609719bSwdenk 
1404c609719bSwdenk 		repeatable &= cmdtp->repeatable;
1405c609719bSwdenk 
1406c609719bSwdenk 		/* Did the user stop this? */
1407c609719bSwdenk 		if (had_ctrlc ())
14085afb2020SDetlev Zundel 			return -1;	/* if stopped then not repeatable */
1409c609719bSwdenk 	}
1410c609719bSwdenk 
1411f07771ccSwdenk 	return rc ? rc : repeatable;
1412c609719bSwdenk }
1413c609719bSwdenk 
1414c609719bSwdenk /****************************************************************************/
1415c609719bSwdenk 
1416c3517f91SJon Loeliger #if defined(CONFIG_CMD_RUN)
1417*54841ab5SWolfgang Denk int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1418c609719bSwdenk {
1419c609719bSwdenk 	int i;
1420c609719bSwdenk 
1421c609719bSwdenk 	if (argc < 2) {
142262c3ae7cSPeter Tyser 		cmd_usage(cmdtp);
1423c609719bSwdenk 		return 1;
1424c609719bSwdenk 	}
1425c609719bSwdenk 
1426c609719bSwdenk 	for (i=1; i<argc; ++i) {
14273e38691eSwdenk 		char *arg;
14283e38691eSwdenk 
14293e38691eSwdenk 		if ((arg = getenv (argv[i])) == NULL) {
14303e38691eSwdenk 			printf ("## Error: \"%s\" not defined\n", argv[i]);
14313e38691eSwdenk 			return 1;
14323e38691eSwdenk 		}
14336d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER
14343e38691eSwdenk 		if (run_command (arg, flag) == -1)
14353e38691eSwdenk 			return 1;
1436c609719bSwdenk #else
14373e38691eSwdenk 		if (parse_string_outer(arg,
14387aa78614Swdenk 		    FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
14393e38691eSwdenk 			return 1;
1440c609719bSwdenk #endif
1441c609719bSwdenk 	}
14423e38691eSwdenk 	return 0;
1443c609719bSwdenk }
144490253178SJon Loeliger #endif
1445