xref: /openbmc/u-boot/common/main.c (revision d51004a832a9784f4c6af5482d4dace6bfd938c4)
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>
33*d51004a8SSimon Glass #include <malloc.h>
3409c2e90cSAndreas Bießmann #include <version.h>
35d9631ecfSWolfgang Denk #ifdef CONFIG_MODEM_SUPPORT
36d9631ecfSWolfgang Denk #include <malloc.h>		/* for free() prototype */
37d9631ecfSWolfgang Denk #endif
38c609719bSwdenk 
396d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
40c609719bSwdenk #include <hush.h>
41c609719bSwdenk #endif
42c609719bSwdenk 
43bdccc4feSwdenk #include <post.h>
444d91a6ecSJason Hobbs #include <linux/ctype.h>
45317d6c57SHeiko Schocher #include <menu.h>
46bdccc4feSwdenk 
47597f6c26SJames Yang #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
48d87080b7SWolfgang Denk DECLARE_GLOBAL_DATA_PTR;
49d87080b7SWolfgang Denk #endif
50d87080b7SWolfgang Denk 
51fad63407SHeiko Schocher /*
52fad63407SHeiko Schocher  * Board-specific Platform code can reimplement show_boot_progress () if needed
53fad63407SHeiko Schocher  */
54fad63407SHeiko Schocher void inline __show_boot_progress (int val) {}
555e2c08c3SEmil Medve void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
56fad63407SHeiko Schocher 
574bae9090SBartlomiej Sieka #if defined(CONFIG_UPDATE_TFTP)
588d6b7320SAndreas Pretzsch int update_tftp (ulong addr);
594bae9090SBartlomiej Sieka #endif /* CONFIG_UPDATE_TFTP */
608bde7f77Swdenk 
61c609719bSwdenk #define MAX_DELAY_STOP_STR 32
62c609719bSwdenk 
63c609719bSwdenk #undef DEBUG_PARSER
64c609719bSwdenk 
656475b9f9SJohn Schmoller char        console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
66c609719bSwdenk 
673ca9122fSStefan Roese static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
6882359aecSMike Frysinger static const char erase_seq[] = "\b \b";		/* erase sequence	*/
6982359aecSMike Frysinger static const char   tab_seq[] = "        ";		/* used to expand TABs	*/
70c609719bSwdenk 
71c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
72c609719bSwdenk static uint64_t endtime = 0;  /* must be set, default is instant timeout */
73c609719bSwdenk static int      retry_time = -1; /* -1 so can call readline before main_loop */
74c609719bSwdenk #endif
75c609719bSwdenk 
76c609719bSwdenk #define	endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
77c609719bSwdenk 
78c609719bSwdenk #ifndef CONFIG_BOOT_RETRY_MIN
79c609719bSwdenk #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
80c609719bSwdenk #endif
81c609719bSwdenk 
82c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
83c609719bSwdenk int do_mdm_init = 0;
84c609719bSwdenk extern void mdm_init(void); /* defined in board.c */
85c609719bSwdenk #endif
86c609719bSwdenk 
87c609719bSwdenk /***************************************************************************
88c609719bSwdenk  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
89c8a2079eSJason Hobbs  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
90c609719bSwdenk  */
91c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
92c609719bSwdenk # if defined(CONFIG_AUTOBOOT_KEYED)
93b41bc5a8SJason Hobbs #ifndef CONFIG_MENU
94b41bc5a8SJason Hobbs static inline
95b41bc5a8SJason Hobbs #endif
96b41bc5a8SJason Hobbs int abortboot(int bootdelay)
97c609719bSwdenk {
98c609719bSwdenk 	int abort = 0;
99c609719bSwdenk 	uint64_t etime = endtick(bootdelay);
10019973b6aSWolfgang Denk 	struct {
101c609719bSwdenk 		char* str;
102c609719bSwdenk 		u_int len;
103c609719bSwdenk 		int retry;
104c609719bSwdenk 	}
10519973b6aSWolfgang Denk 	delaykey [] = {
106c609719bSwdenk 		{ str: getenv ("bootdelaykey"),  retry: 1 },
107c609719bSwdenk 		{ str: getenv ("bootdelaykey2"), retry: 1 },
108c609719bSwdenk 		{ str: getenv ("bootstopkey"),   retry: 0 },
109c609719bSwdenk 		{ str: getenv ("bootstopkey2"),  retry: 0 },
110c609719bSwdenk 	};
111c609719bSwdenk 
112c609719bSwdenk 	char presskey [MAX_DELAY_STOP_STR];
113c609719bSwdenk 	u_int presskey_len = 0;
114c609719bSwdenk 	u_int presskey_max = 0;
115c609719bSwdenk 	u_int i;
116c609719bSwdenk 
117c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_PROMPT
118f2302d44SStefan Roese 	printf(CONFIG_AUTOBOOT_PROMPT);
119c609719bSwdenk #  endif
120c609719bSwdenk 
121c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR
122c609719bSwdenk 	if (delaykey[0].str == NULL)
123c609719bSwdenk 		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
124c609719bSwdenk #  endif
125c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR2
126c609719bSwdenk 	if (delaykey[1].str == NULL)
127c609719bSwdenk 		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
128c609719bSwdenk #  endif
129c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR
130c609719bSwdenk 	if (delaykey[2].str == NULL)
131c609719bSwdenk 		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
132c609719bSwdenk #  endif
133c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR2
134c609719bSwdenk 	if (delaykey[3].str == NULL)
135c609719bSwdenk 		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
136c609719bSwdenk #  endif
137c609719bSwdenk 
138c609719bSwdenk 	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
139c609719bSwdenk 		delaykey[i].len = delaykey[i].str == NULL ?
140c609719bSwdenk 				    0 : strlen (delaykey[i].str);
141c609719bSwdenk 		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
142c609719bSwdenk 				    MAX_DELAY_STOP_STR : delaykey[i].len;
143c609719bSwdenk 
144c609719bSwdenk 		presskey_max = presskey_max > delaykey[i].len ?
145c609719bSwdenk 				    presskey_max : delaykey[i].len;
146c609719bSwdenk 
147c609719bSwdenk #  if DEBUG_BOOTKEYS
148c609719bSwdenk 		printf("%s key:<%s>\n",
149c609719bSwdenk 		       delaykey[i].retry ? "delay" : "stop",
150c609719bSwdenk 		       delaykey[i].str ? delaykey[i].str : "NULL");
151c609719bSwdenk #  endif
152c609719bSwdenk 	}
153c609719bSwdenk 
154c609719bSwdenk 	/* In order to keep up with incoming data, check timeout only
155c609719bSwdenk 	 * when catch up.
156c609719bSwdenk 	 */
157c3284b03SPeter Korsgaard 	do {
158c3284b03SPeter Korsgaard 		if (tstc()) {
159c3284b03SPeter Korsgaard 			if (presskey_len < presskey_max) {
160c3284b03SPeter Korsgaard 				presskey [presskey_len ++] = getc();
161c3284b03SPeter Korsgaard 			}
162c3284b03SPeter Korsgaard 			else {
163c3284b03SPeter Korsgaard 				for (i = 0; i < presskey_max - 1; i ++)
164c3284b03SPeter Korsgaard 					presskey [i] = presskey [i + 1];
165c3284b03SPeter Korsgaard 
166c3284b03SPeter Korsgaard 				presskey [i] = getc();
167c3284b03SPeter Korsgaard 			}
168c3284b03SPeter Korsgaard 		}
169c3284b03SPeter Korsgaard 
170c609719bSwdenk 		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
171c609719bSwdenk 			if (delaykey[i].len > 0 &&
172c609719bSwdenk 			    presskey_len >= delaykey[i].len &&
173c609719bSwdenk 			    memcmp (presskey + presskey_len - delaykey[i].len,
174c609719bSwdenk 				    delaykey[i].str,
175c609719bSwdenk 				    delaykey[i].len) == 0) {
176c609719bSwdenk #  if DEBUG_BOOTKEYS
177c609719bSwdenk 				printf("got %skey\n",
178c609719bSwdenk 				       delaykey[i].retry ? "delay" : "stop");
179c609719bSwdenk #  endif
180c609719bSwdenk 
181c609719bSwdenk #  ifdef CONFIG_BOOT_RETRY_TIME
182c609719bSwdenk 				/* don't retry auto boot */
183c609719bSwdenk 				if (! delaykey[i].retry)
184c609719bSwdenk 					retry_time = -1;
185c609719bSwdenk #  endif
186c609719bSwdenk 				abort = 1;
187c609719bSwdenk 			}
188c609719bSwdenk 		}
189c3284b03SPeter Korsgaard 	} while (!abort && get_ticks() <= etime);
190c609719bSwdenk 
191c609719bSwdenk #  if DEBUG_BOOTKEYS
192c609719bSwdenk 	if (!abort)
1934b9206edSwdenk 		puts("key timeout\n");
194c609719bSwdenk #  endif
195c609719bSwdenk 
1968cb8143eSdzu #ifdef CONFIG_SILENT_CONSOLE
1974ec5bd55SLadislav Michl 	if (abort)
1984ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
1998cb8143eSdzu #endif
2008cb8143eSdzu 
201c609719bSwdenk 	return abort;
202c609719bSwdenk }
203c609719bSwdenk 
204c609719bSwdenk # else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
205c609719bSwdenk 
206c7de829cSwdenk #ifdef CONFIG_MENUKEY
207c7de829cSwdenk static int menukey = 0;
208c7de829cSwdenk #endif
209c7de829cSwdenk 
210b41bc5a8SJason Hobbs #ifndef CONFIG_MENU
211b41bc5a8SJason Hobbs static inline
212b41bc5a8SJason Hobbs #endif
213b41bc5a8SJason Hobbs int abortboot(int bootdelay)
214c609719bSwdenk {
215c609719bSwdenk 	int abort = 0;
216c609719bSwdenk 
217c7de829cSwdenk #ifdef CONFIG_MENUPROMPT
218f2302d44SStefan Roese 	printf(CONFIG_MENUPROMPT);
219c7de829cSwdenk #else
220c609719bSwdenk 	printf("Hit any key to stop autoboot: %2d ", bootdelay);
221c7de829cSwdenk #endif
222c609719bSwdenk 
223c609719bSwdenk #if defined CONFIG_ZERO_BOOTDELAY_CHECK
224c609719bSwdenk 	/*
225c609719bSwdenk 	 * Check if key already pressed
226c609719bSwdenk 	 * Don't check if bootdelay < 0
227c609719bSwdenk 	 */
228c609719bSwdenk 	if (bootdelay >= 0) {
229c609719bSwdenk 		if (tstc()) {	/* we got a key press	*/
230c609719bSwdenk 			(void) getc();  /* consume input	*/
2314b9206edSwdenk 			puts ("\b\b\b 0");
232f72da340Swdenk 			abort = 1;	/* don't auto boot	*/
233c609719bSwdenk 		}
234c609719bSwdenk 	}
235c609719bSwdenk #endif
236c609719bSwdenk 
237f72da340Swdenk 	while ((bootdelay > 0) && (!abort)) {
238c609719bSwdenk 		int i;
239c609719bSwdenk 
240c609719bSwdenk 		--bootdelay;
241c609719bSwdenk 		/* delay 100 * 10ms */
242c609719bSwdenk 		for (i=0; !abort && i<100; ++i) {
243c609719bSwdenk 			if (tstc()) {	/* we got a key press	*/
244c609719bSwdenk 				abort  = 1;	/* don't auto boot	*/
245c609719bSwdenk 				bootdelay = 0;	/* no more delay	*/
246c7de829cSwdenk # ifdef CONFIG_MENUKEY
247c7de829cSwdenk 				menukey = getc();
248c7de829cSwdenk # else
249c609719bSwdenk 				(void) getc();  /* consume input	*/
250c7de829cSwdenk # endif
251c609719bSwdenk 				break;
252c609719bSwdenk 			}
253c609719bSwdenk 			udelay(10000);
254c609719bSwdenk 		}
255c609719bSwdenk 
256c609719bSwdenk 		printf("\b\b\b%2d ", bootdelay);
257c609719bSwdenk 	}
258c609719bSwdenk 
259c609719bSwdenk 	putc('\n');
260c609719bSwdenk 
261f72da340Swdenk #ifdef CONFIG_SILENT_CONSOLE
2624ec5bd55SLadislav Michl 	if (abort)
2634ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
264f72da340Swdenk #endif
265f72da340Swdenk 
266c609719bSwdenk 	return abort;
267c609719bSwdenk }
268c609719bSwdenk # endif	/* CONFIG_AUTOBOOT_KEYED */
269c609719bSwdenk #endif	/* CONFIG_BOOTDELAY >= 0  */
270c609719bSwdenk 
271c609719bSwdenk /****************************************************************************/
272c609719bSwdenk 
273c609719bSwdenk void main_loop (void)
274c609719bSwdenk {
2756d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER
2766d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
277c609719bSwdenk 	int len;
278c609719bSwdenk 	int rc = 1;
279c609719bSwdenk 	int flag;
280c609719bSwdenk #endif
281c609719bSwdenk 
282c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
283c609719bSwdenk 	char *s;
284c609719bSwdenk 	int bootdelay;
285c609719bSwdenk #endif
286c609719bSwdenk #ifdef CONFIG_PREBOOT
287c609719bSwdenk 	char *p;
288c609719bSwdenk #endif
289bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
290bdccc4feSwdenk 	unsigned long bootcount = 0;
291bdccc4feSwdenk 	unsigned long bootlimit = 0;
292bdccc4feSwdenk 	char *bcs;
293bdccc4feSwdenk 	char bcs_set[16];
294bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
295c609719bSwdenk 
296bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
297bdccc4feSwdenk 	bootcount = bootcount_load();
298bdccc4feSwdenk 	bootcount++;
299bdccc4feSwdenk 	bootcount_store (bootcount);
300bdccc4feSwdenk 	sprintf (bcs_set, "%lu", bootcount);
301bdccc4feSwdenk 	setenv ("bootcount", bcs_set);
302bdccc4feSwdenk 	bcs = getenv ("bootlimit");
303bdccc4feSwdenk 	bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
304bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
305bdccc4feSwdenk 
306c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
307c609719bSwdenk 	debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
308c609719bSwdenk 	if (do_mdm_init) {
30977ddac94SWolfgang Denk 		char *str = strdup(getenv("mdm_cmd"));
310c609719bSwdenk 		setenv ("preboot", str);  /* set or delete definition */
311c609719bSwdenk 		if (str != NULL)
312c609719bSwdenk 			free (str);
313c609719bSwdenk 		mdm_init(); /* wait for modem connection */
314c609719bSwdenk 	}
315c609719bSwdenk #endif  /* CONFIG_MODEM_SUPPORT */
316c609719bSwdenk 
3170587597cSstroese #ifdef CONFIG_VERSION_VARIABLE
3180587597cSstroese 	{
3190587597cSstroese 		setenv ("ver", version_string);  /* set version variable */
3200587597cSstroese 	}
3210587597cSstroese #endif /* CONFIG_VERSION_VARIABLE */
3220587597cSstroese 
3236d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
324c609719bSwdenk 	u_boot_hush_start ();
325c609719bSwdenk #endif
326c609719bSwdenk 
32781473f67SHeiko Schocher #if defined(CONFIG_HUSH_INIT_VAR)
32881473f67SHeiko Schocher 	hush_init_var ();
32981473f67SHeiko Schocher #endif
33081473f67SHeiko Schocher 
331c609719bSwdenk #ifdef CONFIG_PREBOOT
332c609719bSwdenk 	if ((p = getenv ("preboot")) != NULL) {
333c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
334c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
335c609719bSwdenk # endif
336c609719bSwdenk 
337009dde19SSimon Glass 		run_command(p, 0);
338c609719bSwdenk 
339c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
340c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
341c609719bSwdenk # endif
342c609719bSwdenk 	}
343c609719bSwdenk #endif /* CONFIG_PREBOOT */
344c609719bSwdenk 
345143cd21fSWolfgang Denk #if defined(CONFIG_UPDATE_TFTP)
3468d6b7320SAndreas Pretzsch 	update_tftp (0UL);
347143cd21fSWolfgang Denk #endif /* CONFIG_UPDATE_TFTP */
348143cd21fSWolfgang Denk 
349c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
350c609719bSwdenk 	s = getenv ("bootdelay");
351c609719bSwdenk 	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
352c609719bSwdenk 
353a6c7ad2fSwdenk 	debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
354c609719bSwdenk 
355317d6c57SHeiko Schocher #if defined(CONFIG_MENU_SHOW)
356317d6c57SHeiko Schocher 	bootdelay = menu_show(bootdelay);
357317d6c57SHeiko Schocher #endif
358c609719bSwdenk # ifdef CONFIG_BOOT_RETRY_TIME
3596dd652faSwdenk 	init_cmd_timeout ();
360c609719bSwdenk # endif	/* CONFIG_BOOT_RETRY_TIME */
361c609719bSwdenk 
362b428f6a8SYuri Tikhonov #ifdef CONFIG_POST
363b428f6a8SYuri Tikhonov 	if (gd->flags & GD_FLG_POSTFAIL) {
364b428f6a8SYuri Tikhonov 		s = getenv("failbootcmd");
365b428f6a8SYuri Tikhonov 	}
366b428f6a8SYuri Tikhonov 	else
367b428f6a8SYuri Tikhonov #endif /* CONFIG_POST */
368bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
369bdccc4feSwdenk 	if (bootlimit && (bootcount > bootlimit)) {
370bdccc4feSwdenk 		printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
371bdccc4feSwdenk 		        (unsigned)bootlimit);
372bdccc4feSwdenk 		s = getenv ("altbootcmd");
373bdccc4feSwdenk 	}
374bdccc4feSwdenk 	else
375bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
376c609719bSwdenk 		s = getenv ("bootcmd");
377a6c7ad2fSwdenk 
378a6c7ad2fSwdenk 	debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
379a6c7ad2fSwdenk 
380c609719bSwdenk 	if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
381c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
382c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
383c609719bSwdenk # endif
384c609719bSwdenk 
385009dde19SSimon Glass 		run_command(s, 0);
386c609719bSwdenk 
387c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
388c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
389c609719bSwdenk # endif
390c609719bSwdenk 	}
391c7de829cSwdenk 
392c7de829cSwdenk # ifdef CONFIG_MENUKEY
393a6c7ad2fSwdenk 	if (menukey == CONFIG_MENUKEY) {
394c7de829cSwdenk 		s = getenv("menucmd");
395c8a2079eSJason Hobbs 		if (s)
396009dde19SSimon Glass 			run_command(s, 0);
397c7de829cSwdenk 	}
398c7de829cSwdenk #endif /* CONFIG_MENUKEY */
399c609719bSwdenk #endif /* CONFIG_BOOTDELAY */
400c7de829cSwdenk 
401c609719bSwdenk 	/*
402c609719bSwdenk 	 * Main Loop for Monitor Command Processing
403c609719bSwdenk 	 */
4046d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
405c609719bSwdenk 	parse_file_outer();
406c609719bSwdenk 	/* This point is never reached */
407c609719bSwdenk 	for (;;);
408c609719bSwdenk #else
409c609719bSwdenk 	for (;;) {
410c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
411c609719bSwdenk 		if (rc >= 0) {
412c609719bSwdenk 			/* Saw enough of a valid command to
413c609719bSwdenk 			 * restart the timeout.
414c609719bSwdenk 			 */
415c609719bSwdenk 			reset_cmd_timeout();
416c609719bSwdenk 		}
417c609719bSwdenk #endif
4186d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		len = readline (CONFIG_SYS_PROMPT);
419c609719bSwdenk 
420c609719bSwdenk 		flag = 0;	/* assume no special flags for now */
421c609719bSwdenk 		if (len > 0)
422c609719bSwdenk 			strcpy (lastcommand, console_buffer);
423c609719bSwdenk 		else if (len == 0)
424c609719bSwdenk 			flag |= CMD_FLAG_REPEAT;
425c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
426c609719bSwdenk 		else if (len == -2) {
427c609719bSwdenk 			/* -2 means timed out, retry autoboot
428c609719bSwdenk 			 */
4294b9206edSwdenk 			puts ("\nTimed out waiting for command\n");
430c609719bSwdenk # ifdef CONFIG_RESET_TO_RETRY
431c609719bSwdenk 			/* Reinit board to run initialization code again */
432c609719bSwdenk 			do_reset (NULL, 0, 0, NULL);
433c609719bSwdenk # else
434c609719bSwdenk 			return;		/* retry autoboot */
435c609719bSwdenk # endif
436c609719bSwdenk 		}
437c609719bSwdenk #endif
438c609719bSwdenk 
439c609719bSwdenk 		if (len == -1)
4404b9206edSwdenk 			puts ("<INTERRUPT>\n");
441c609719bSwdenk 		else
44253071532SSimon Glass 			rc = run_command(lastcommand, flag);
443c609719bSwdenk 
444c609719bSwdenk 		if (rc <= 0) {
445c609719bSwdenk 			/* invalid command or not repeatable, forget it */
446c609719bSwdenk 			lastcommand[0] = 0;
447c609719bSwdenk 		}
448c609719bSwdenk 	}
4496d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /*CONFIG_SYS_HUSH_PARSER*/
450c609719bSwdenk }
451c609719bSwdenk 
4526dd652faSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
4536dd652faSwdenk /***************************************************************************
45419973b6aSWolfgang Denk  * initialize command line timeout
4556dd652faSwdenk  */
4566dd652faSwdenk void init_cmd_timeout(void)
4576dd652faSwdenk {
4586dd652faSwdenk 	char *s = getenv ("bootretry");
4596dd652faSwdenk 
4606dd652faSwdenk 	if (s != NULL)
461b028f715Swdenk 		retry_time = (int)simple_strtol(s, NULL, 10);
4626dd652faSwdenk 	else
4636dd652faSwdenk 		retry_time =  CONFIG_BOOT_RETRY_TIME;
4646dd652faSwdenk 
4656dd652faSwdenk 	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
4666dd652faSwdenk 		retry_time = CONFIG_BOOT_RETRY_MIN;
4676dd652faSwdenk }
4686dd652faSwdenk 
469c609719bSwdenk /***************************************************************************
470c609719bSwdenk  * reset command line timeout to retry_time seconds
471c609719bSwdenk  */
472c609719bSwdenk void reset_cmd_timeout(void)
473c609719bSwdenk {
474c609719bSwdenk 	endtime = endtick(retry_time);
475c609719bSwdenk }
476c609719bSwdenk #endif
477c609719bSwdenk 
478501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
479501090aaSWolfgang Denk 
480501090aaSWolfgang Denk /*
481501090aaSWolfgang Denk  * cmdline-editing related codes from vivi.
482501090aaSWolfgang Denk  * Author: Janghoon Lyu <nandy@mizi.com>
483501090aaSWolfgang Denk  */
484501090aaSWolfgang Denk 
485501090aaSWolfgang Denk #define putnstr(str,n)	do {			\
486dc4b0b38SAndrew Klossner 		printf ("%.*s", (int)n, str);	\
487501090aaSWolfgang Denk 	} while (0)
488501090aaSWolfgang Denk 
489501090aaSWolfgang Denk #define CTL_CH(c)		((c) - 'a' + 1)
490501090aaSWolfgang Denk #define CTL_BACKSPACE		('\b')
491501090aaSWolfgang Denk #define DEL			((char)255)
492501090aaSWolfgang Denk #define DEL7			((char)127)
493501090aaSWolfgang Denk #define CREAD_HIST_CHAR		('!')
494501090aaSWolfgang Denk 
495501090aaSWolfgang Denk #define getcmd_putch(ch)	putc(ch)
496501090aaSWolfgang Denk #define getcmd_getch()		getc()
497501090aaSWolfgang Denk #define getcmd_cbeep()		getcmd_putch('\a')
498501090aaSWolfgang Denk 
499501090aaSWolfgang Denk #define HIST_MAX		20
5008804ae3bSPeter Tyser #define HIST_SIZE		CONFIG_SYS_CBSIZE
501501090aaSWolfgang Denk 
502501090aaSWolfgang Denk static int hist_max = 0;
503501090aaSWolfgang Denk static int hist_add_idx = 0;
504501090aaSWolfgang Denk static int hist_cur = -1;
505501090aaSWolfgang Denk unsigned hist_num = 0;
506501090aaSWolfgang Denk 
507501090aaSWolfgang Denk char* hist_list[HIST_MAX];
5086475b9f9SJohn Schmoller char hist_lines[HIST_MAX][HIST_SIZE + 1];	 /* Save room for NULL */
509501090aaSWolfgang Denk 
510501090aaSWolfgang Denk #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
511501090aaSWolfgang Denk 
512501090aaSWolfgang Denk static void hist_init(void)
513501090aaSWolfgang Denk {
514501090aaSWolfgang Denk 	int i;
515501090aaSWolfgang Denk 
516501090aaSWolfgang Denk 	hist_max = 0;
517501090aaSWolfgang Denk 	hist_add_idx = 0;
518501090aaSWolfgang Denk 	hist_cur = -1;
519501090aaSWolfgang Denk 	hist_num = 0;
520501090aaSWolfgang Denk 
521501090aaSWolfgang Denk 	for (i = 0; i < HIST_MAX; i++) {
522501090aaSWolfgang Denk 		hist_list[i] = hist_lines[i];
523501090aaSWolfgang Denk 		hist_list[i][0] = '\0';
524501090aaSWolfgang Denk 	}
525501090aaSWolfgang Denk }
526501090aaSWolfgang Denk 
527501090aaSWolfgang Denk static void cread_add_to_hist(char *line)
528501090aaSWolfgang Denk {
529501090aaSWolfgang Denk 	strcpy(hist_list[hist_add_idx], line);
530501090aaSWolfgang Denk 
531501090aaSWolfgang Denk 	if (++hist_add_idx >= HIST_MAX)
532501090aaSWolfgang Denk 		hist_add_idx = 0;
533501090aaSWolfgang Denk 
534501090aaSWolfgang Denk 	if (hist_add_idx > hist_max)
535501090aaSWolfgang Denk 		hist_max = hist_add_idx;
536501090aaSWolfgang Denk 
537501090aaSWolfgang Denk 	hist_num++;
538501090aaSWolfgang Denk }
539501090aaSWolfgang Denk 
540501090aaSWolfgang Denk static char* hist_prev(void)
541501090aaSWolfgang Denk {
542501090aaSWolfgang Denk 	char *ret;
543501090aaSWolfgang Denk 	int old_cur;
544501090aaSWolfgang Denk 
545501090aaSWolfgang Denk 	if (hist_cur < 0)
546501090aaSWolfgang Denk 		return NULL;
547501090aaSWolfgang Denk 
548501090aaSWolfgang Denk 	old_cur = hist_cur;
549501090aaSWolfgang Denk 	if (--hist_cur < 0)
550501090aaSWolfgang Denk 		hist_cur = hist_max;
551501090aaSWolfgang Denk 
552501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
553501090aaSWolfgang Denk 		hist_cur = old_cur;
554501090aaSWolfgang Denk 		ret = NULL;
555501090aaSWolfgang Denk 	} else
556501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
557501090aaSWolfgang Denk 
558501090aaSWolfgang Denk 	return (ret);
559501090aaSWolfgang Denk }
560501090aaSWolfgang Denk 
561501090aaSWolfgang Denk static char* hist_next(void)
562501090aaSWolfgang Denk {
563501090aaSWolfgang Denk 	char *ret;
564501090aaSWolfgang Denk 
565501090aaSWolfgang Denk 	if (hist_cur < 0)
566501090aaSWolfgang Denk 		return NULL;
567501090aaSWolfgang Denk 
568501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx)
569501090aaSWolfgang Denk 		return NULL;
570501090aaSWolfgang Denk 
571501090aaSWolfgang Denk 	if (++hist_cur > hist_max)
572501090aaSWolfgang Denk 		hist_cur = 0;
573501090aaSWolfgang Denk 
574501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
575501090aaSWolfgang Denk 		ret = "";
576501090aaSWolfgang Denk 	} else
577501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
578501090aaSWolfgang Denk 
579501090aaSWolfgang Denk 	return (ret);
580501090aaSWolfgang Denk }
581501090aaSWolfgang Denk 
5823ca9122fSStefan Roese #ifndef CONFIG_CMDLINE_EDITING
583501090aaSWolfgang Denk static void cread_print_hist_list(void)
584501090aaSWolfgang Denk {
585501090aaSWolfgang Denk 	int i;
586501090aaSWolfgang Denk 	unsigned long n;
587501090aaSWolfgang Denk 
588501090aaSWolfgang Denk 	n = hist_num - hist_max;
589501090aaSWolfgang Denk 
590501090aaSWolfgang Denk 	i = hist_add_idx + 1;
591501090aaSWolfgang Denk 	while (1) {
592501090aaSWolfgang Denk 		if (i > hist_max)
593501090aaSWolfgang Denk 			i = 0;
594501090aaSWolfgang Denk 		if (i == hist_add_idx)
595501090aaSWolfgang Denk 			break;
596501090aaSWolfgang Denk 		printf("%s\n", hist_list[i]);
597501090aaSWolfgang Denk 		n++;
598501090aaSWolfgang Denk 		i++;
599501090aaSWolfgang Denk 	}
600501090aaSWolfgang Denk }
6013ca9122fSStefan Roese #endif /* CONFIG_CMDLINE_EDITING */
602501090aaSWolfgang Denk 
603501090aaSWolfgang Denk #define BEGINNING_OF_LINE() {			\
604501090aaSWolfgang Denk 	while (num) {				\
605501090aaSWolfgang Denk 		getcmd_putch(CTL_BACKSPACE);	\
606501090aaSWolfgang Denk 		num--;				\
607501090aaSWolfgang Denk 	}					\
608501090aaSWolfgang Denk }
609501090aaSWolfgang Denk 
610501090aaSWolfgang Denk #define ERASE_TO_EOL() {				\
611501090aaSWolfgang Denk 	if (num < eol_num) {				\
6128faba489SMike Frysinger 		printf("%*s", (int)(eol_num - num), ""); \
6138faba489SMike Frysinger 		do {					\
614501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);	\
6158faba489SMike Frysinger 		} while (--eol_num > num);		\
616501090aaSWolfgang Denk 	}						\
617501090aaSWolfgang Denk }
618501090aaSWolfgang Denk 
619501090aaSWolfgang Denk #define REFRESH_TO_EOL() {			\
620501090aaSWolfgang Denk 	if (num < eol_num) {			\
621501090aaSWolfgang Denk 		wlen = eol_num - num;		\
622501090aaSWolfgang Denk 		putnstr(buf + num, wlen);	\
623501090aaSWolfgang Denk 		num = eol_num;			\
624501090aaSWolfgang Denk 	}					\
625501090aaSWolfgang Denk }
626501090aaSWolfgang Denk 
627501090aaSWolfgang Denk static void cread_add_char(char ichar, int insert, unsigned long *num,
628501090aaSWolfgang Denk 	       unsigned long *eol_num, char *buf, unsigned long len)
629501090aaSWolfgang Denk {
630501090aaSWolfgang Denk 	unsigned long wlen;
631501090aaSWolfgang Denk 
632501090aaSWolfgang Denk 	/* room ??? */
633501090aaSWolfgang Denk 	if (insert || *num == *eol_num) {
634501090aaSWolfgang Denk 		if (*eol_num > len - 1) {
635501090aaSWolfgang Denk 			getcmd_cbeep();
636501090aaSWolfgang Denk 			return;
637501090aaSWolfgang Denk 		}
638501090aaSWolfgang Denk 		(*eol_num)++;
639501090aaSWolfgang Denk 	}
640501090aaSWolfgang Denk 
641501090aaSWolfgang Denk 	if (insert) {
642501090aaSWolfgang Denk 		wlen = *eol_num - *num;
643501090aaSWolfgang Denk 		if (wlen > 1) {
644501090aaSWolfgang Denk 			memmove(&buf[*num+1], &buf[*num], wlen-1);
645501090aaSWolfgang Denk 		}
646501090aaSWolfgang Denk 
647501090aaSWolfgang Denk 		buf[*num] = ichar;
648501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
649501090aaSWolfgang Denk 		(*num)++;
650501090aaSWolfgang Denk 		while (--wlen) {
651501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);
652501090aaSWolfgang Denk 		}
653501090aaSWolfgang Denk 	} else {
654501090aaSWolfgang Denk 		/* echo the character */
655501090aaSWolfgang Denk 		wlen = 1;
656501090aaSWolfgang Denk 		buf[*num] = ichar;
657501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
658501090aaSWolfgang Denk 		(*num)++;
659501090aaSWolfgang Denk 	}
660501090aaSWolfgang Denk }
661501090aaSWolfgang Denk 
662501090aaSWolfgang Denk static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
663501090aaSWolfgang Denk 	      unsigned long *eol_num, char *buf, unsigned long len)
664501090aaSWolfgang Denk {
665501090aaSWolfgang Denk 	while (strsize--) {
666501090aaSWolfgang Denk 		cread_add_char(*str, insert, num, eol_num, buf, len);
667501090aaSWolfgang Denk 		str++;
668501090aaSWolfgang Denk 	}
669501090aaSWolfgang Denk }
670501090aaSWolfgang Denk 
6719c348311SHeiko Schocher static int cread_line(const char *const prompt, char *buf, unsigned int *len,
6729c348311SHeiko Schocher 		int timeout)
673501090aaSWolfgang Denk {
674501090aaSWolfgang Denk 	unsigned long num = 0;
675501090aaSWolfgang Denk 	unsigned long eol_num = 0;
676501090aaSWolfgang Denk 	unsigned long wlen;
677501090aaSWolfgang Denk 	char ichar;
678501090aaSWolfgang Denk 	int insert = 1;
679501090aaSWolfgang Denk 	int esc_len = 0;
680501090aaSWolfgang Denk 	char esc_save[8];
681ecc5500eSPeter Tyser 	int init_len = strlen(buf);
6829c348311SHeiko Schocher 	int first = 1;
683ecc5500eSPeter Tyser 
684ecc5500eSPeter Tyser 	if (init_len)
685ecc5500eSPeter Tyser 		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
686501090aaSWolfgang Denk 
687501090aaSWolfgang Denk 	while (1) {
68800ac50e3SAndreas Engel #ifdef CONFIG_BOOT_RETRY_TIME
68900ac50e3SAndreas Engel 		while (!tstc()) {	/* while no incoming data */
69000ac50e3SAndreas Engel 			if (retry_time >= 0 && get_ticks() > endtime)
69100ac50e3SAndreas Engel 				return (-2);	/* timed out */
69230dc165aSJens Scharsig 			WATCHDOG_RESET();
69300ac50e3SAndreas Engel 		}
69400ac50e3SAndreas Engel #endif
6959c348311SHeiko Schocher 		if (first && timeout) {
6969c348311SHeiko Schocher 			uint64_t etime = endtick(timeout);
6979c348311SHeiko Schocher 
6989c348311SHeiko Schocher 			while (!tstc()) {	/* while no incoming data */
6999c348311SHeiko Schocher 				if (get_ticks() >= etime)
7009c348311SHeiko Schocher 					return -2;	/* timed out */
7019c348311SHeiko Schocher 				WATCHDOG_RESET();
7029c348311SHeiko Schocher 			}
7039c348311SHeiko Schocher 			first = 0;
7049c348311SHeiko Schocher 		}
70500ac50e3SAndreas Engel 
706501090aaSWolfgang Denk 		ichar = getcmd_getch();
707501090aaSWolfgang Denk 
708501090aaSWolfgang Denk 		if ((ichar == '\n') || (ichar == '\r')) {
709dd9f06f0SWolfgang Denk 			putc('\n');
710501090aaSWolfgang Denk 			break;
711501090aaSWolfgang Denk 		}
712501090aaSWolfgang Denk 
713501090aaSWolfgang Denk 		/*
714501090aaSWolfgang Denk 		 * handle standard linux xterm esc sequences for arrow key, etc.
715501090aaSWolfgang Denk 		 */
716501090aaSWolfgang Denk 		if (esc_len != 0) {
717501090aaSWolfgang Denk 			if (esc_len == 1) {
718501090aaSWolfgang Denk 				if (ichar == '[') {
719501090aaSWolfgang Denk 					esc_save[esc_len] = ichar;
720501090aaSWolfgang Denk 					esc_len = 2;
721501090aaSWolfgang Denk 				} else {
722501090aaSWolfgang Denk 					cread_add_str(esc_save, esc_len, insert,
723501090aaSWolfgang Denk 						      &num, &eol_num, buf, *len);
724501090aaSWolfgang Denk 					esc_len = 0;
725501090aaSWolfgang Denk 				}
726501090aaSWolfgang Denk 				continue;
727501090aaSWolfgang Denk 			}
728501090aaSWolfgang Denk 
729501090aaSWolfgang Denk 			switch (ichar) {
730501090aaSWolfgang Denk 
731501090aaSWolfgang Denk 			case 'D':	/* <- key */
732501090aaSWolfgang Denk 				ichar = CTL_CH('b');
733501090aaSWolfgang Denk 				esc_len = 0;
734501090aaSWolfgang Denk 				break;
735501090aaSWolfgang Denk 			case 'C':	/* -> key */
736501090aaSWolfgang Denk 				ichar = CTL_CH('f');
737501090aaSWolfgang Denk 				esc_len = 0;
738501090aaSWolfgang Denk 				break;	/* pass off to ^F handler */
739501090aaSWolfgang Denk 			case 'H':	/* Home key */
740501090aaSWolfgang Denk 				ichar = CTL_CH('a');
741501090aaSWolfgang Denk 				esc_len = 0;
742501090aaSWolfgang Denk 				break;	/* pass off to ^A handler */
743501090aaSWolfgang Denk 			case 'A':	/* up arrow */
744501090aaSWolfgang Denk 				ichar = CTL_CH('p');
745501090aaSWolfgang Denk 				esc_len = 0;
746501090aaSWolfgang Denk 				break;	/* pass off to ^P handler */
747501090aaSWolfgang Denk 			case 'B':	/* down arrow */
748501090aaSWolfgang Denk 				ichar = CTL_CH('n');
749501090aaSWolfgang Denk 				esc_len = 0;
750501090aaSWolfgang Denk 				break;	/* pass off to ^N handler */
751501090aaSWolfgang Denk 			default:
752501090aaSWolfgang Denk 				esc_save[esc_len++] = ichar;
753501090aaSWolfgang Denk 				cread_add_str(esc_save, esc_len, insert,
754501090aaSWolfgang Denk 					      &num, &eol_num, buf, *len);
755501090aaSWolfgang Denk 				esc_len = 0;
756501090aaSWolfgang Denk 				continue;
757501090aaSWolfgang Denk 			}
758501090aaSWolfgang Denk 		}
759501090aaSWolfgang Denk 
760501090aaSWolfgang Denk 		switch (ichar) {
761501090aaSWolfgang Denk 		case 0x1b:
762501090aaSWolfgang Denk 			if (esc_len == 0) {
763501090aaSWolfgang Denk 				esc_save[esc_len] = ichar;
764501090aaSWolfgang Denk 				esc_len = 1;
765501090aaSWolfgang Denk 			} else {
766dd9f06f0SWolfgang Denk 				puts("impossible condition #876\n");
767501090aaSWolfgang Denk 				esc_len = 0;
768501090aaSWolfgang Denk 			}
769501090aaSWolfgang Denk 			break;
770501090aaSWolfgang Denk 
771501090aaSWolfgang Denk 		case CTL_CH('a'):
772501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
773501090aaSWolfgang Denk 			break;
774501090aaSWolfgang Denk 		case CTL_CH('c'):	/* ^C - break */
775501090aaSWolfgang Denk 			*buf = '\0';	/* discard input */
776501090aaSWolfgang Denk 			return (-1);
777501090aaSWolfgang Denk 		case CTL_CH('f'):
778501090aaSWolfgang Denk 			if (num < eol_num) {
779501090aaSWolfgang Denk 				getcmd_putch(buf[num]);
780501090aaSWolfgang Denk 				num++;
781501090aaSWolfgang Denk 			}
782501090aaSWolfgang Denk 			break;
783501090aaSWolfgang Denk 		case CTL_CH('b'):
784501090aaSWolfgang Denk 			if (num) {
785501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
786501090aaSWolfgang Denk 				num--;
787501090aaSWolfgang Denk 			}
788501090aaSWolfgang Denk 			break;
789501090aaSWolfgang Denk 		case CTL_CH('d'):
790501090aaSWolfgang Denk 			if (num < eol_num) {
791501090aaSWolfgang Denk 				wlen = eol_num - num - 1;
792501090aaSWolfgang Denk 				if (wlen) {
793501090aaSWolfgang Denk 					memmove(&buf[num], &buf[num+1], wlen);
794501090aaSWolfgang Denk 					putnstr(buf + num, wlen);
795501090aaSWolfgang Denk 				}
796501090aaSWolfgang Denk 
797501090aaSWolfgang Denk 				getcmd_putch(' ');
798501090aaSWolfgang Denk 				do {
799501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
800501090aaSWolfgang Denk 				} while (wlen--);
801501090aaSWolfgang Denk 				eol_num--;
802501090aaSWolfgang Denk 			}
803501090aaSWolfgang Denk 			break;
804501090aaSWolfgang Denk 		case CTL_CH('k'):
805501090aaSWolfgang Denk 			ERASE_TO_EOL();
806501090aaSWolfgang Denk 			break;
807501090aaSWolfgang Denk 		case CTL_CH('e'):
808501090aaSWolfgang Denk 			REFRESH_TO_EOL();
809501090aaSWolfgang Denk 			break;
810501090aaSWolfgang Denk 		case CTL_CH('o'):
811501090aaSWolfgang Denk 			insert = !insert;
812501090aaSWolfgang Denk 			break;
813501090aaSWolfgang Denk 		case CTL_CH('x'):
81423d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case CTL_CH('u'):
815501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
816501090aaSWolfgang Denk 			ERASE_TO_EOL();
817501090aaSWolfgang Denk 			break;
818501090aaSWolfgang Denk 		case DEL:
819501090aaSWolfgang Denk 		case DEL7:
820501090aaSWolfgang Denk 		case 8:
821501090aaSWolfgang Denk 			if (num) {
822501090aaSWolfgang Denk 				wlen = eol_num - num;
823501090aaSWolfgang Denk 				num--;
824501090aaSWolfgang Denk 				memmove(&buf[num], &buf[num+1], wlen);
825501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
826501090aaSWolfgang Denk 				putnstr(buf + num, wlen);
827501090aaSWolfgang Denk 				getcmd_putch(' ');
828501090aaSWolfgang Denk 				do {
829501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
830501090aaSWolfgang Denk 				} while (wlen--);
831501090aaSWolfgang Denk 				eol_num--;
832501090aaSWolfgang Denk 			}
833501090aaSWolfgang Denk 			break;
834501090aaSWolfgang Denk 		case CTL_CH('p'):
835501090aaSWolfgang Denk 		case CTL_CH('n'):
836501090aaSWolfgang Denk 		{
837501090aaSWolfgang Denk 			char * hline;
838501090aaSWolfgang Denk 
839501090aaSWolfgang Denk 			esc_len = 0;
840501090aaSWolfgang Denk 
841501090aaSWolfgang Denk 			if (ichar == CTL_CH('p'))
842501090aaSWolfgang Denk 				hline = hist_prev();
843501090aaSWolfgang Denk 			else
844501090aaSWolfgang Denk 				hline = hist_next();
845501090aaSWolfgang Denk 
846501090aaSWolfgang Denk 			if (!hline) {
847501090aaSWolfgang Denk 				getcmd_cbeep();
848501090aaSWolfgang Denk 				continue;
849501090aaSWolfgang Denk 			}
850501090aaSWolfgang Denk 
851501090aaSWolfgang Denk 			/* nuke the current line */
852501090aaSWolfgang Denk 			/* first, go home */
853501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
854501090aaSWolfgang Denk 
855501090aaSWolfgang Denk 			/* erase to end of line */
856501090aaSWolfgang Denk 			ERASE_TO_EOL();
857501090aaSWolfgang Denk 
858501090aaSWolfgang Denk 			/* copy new line into place and display */
859501090aaSWolfgang Denk 			strcpy(buf, hline);
860501090aaSWolfgang Denk 			eol_num = strlen(buf);
861501090aaSWolfgang Denk 			REFRESH_TO_EOL();
862501090aaSWolfgang Denk 			continue;
863501090aaSWolfgang Denk 		}
86423d0baf9SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_AUTO_COMPLETE
86523d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case '\t': {
86623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			int num2, col;
86723d0baf9SJean-Christophe PLAGNIOL-VILLARD 
86823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			/* do not autocomplete when in the middle */
86923d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (num < eol_num) {
87023d0baf9SJean-Christophe PLAGNIOL-VILLARD 				getcmd_cbeep();
87123d0baf9SJean-Christophe PLAGNIOL-VILLARD 				break;
87223d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
87323d0baf9SJean-Christophe PLAGNIOL-VILLARD 
87423d0baf9SJean-Christophe PLAGNIOL-VILLARD 			buf[num] = '\0';
87523d0baf9SJean-Christophe PLAGNIOL-VILLARD 			col = strlen(prompt) + eol_num;
87623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			num2 = num;
87723d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
87823d0baf9SJean-Christophe PLAGNIOL-VILLARD 				col = num2 - num;
87923d0baf9SJean-Christophe PLAGNIOL-VILLARD 				num += col;
88023d0baf9SJean-Christophe PLAGNIOL-VILLARD 				eol_num += col;
88123d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
88223d0baf9SJean-Christophe PLAGNIOL-VILLARD 			break;
88323d0baf9SJean-Christophe PLAGNIOL-VILLARD 		}
88423d0baf9SJean-Christophe PLAGNIOL-VILLARD #endif
885501090aaSWolfgang Denk 		default:
886501090aaSWolfgang Denk 			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
887501090aaSWolfgang Denk 			break;
888501090aaSWolfgang Denk 		}
889501090aaSWolfgang Denk 	}
890501090aaSWolfgang Denk 	*len = eol_num;
891501090aaSWolfgang Denk 	buf[eol_num] = '\0';	/* lose the newline */
892501090aaSWolfgang Denk 
893501090aaSWolfgang Denk 	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
894501090aaSWolfgang Denk 		cread_add_to_hist(buf);
895501090aaSWolfgang Denk 	hist_cur = hist_add_idx;
896501090aaSWolfgang Denk 
897f9239438SPeter Tyser 	return 0;
898501090aaSWolfgang Denk }
899501090aaSWolfgang Denk 
900501090aaSWolfgang Denk #endif /* CONFIG_CMDLINE_EDITING */
901501090aaSWolfgang Denk 
902c609719bSwdenk /****************************************************************************/
903c609719bSwdenk 
904c609719bSwdenk /*
905c609719bSwdenk  * Prompt for input and read a line.
906c609719bSwdenk  * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
907c609719bSwdenk  * time out when time goes past endtime (timebase time in ticks).
908c609719bSwdenk  * Return:	number of read characters
909c609719bSwdenk  *		-1 if break
910c609719bSwdenk  *		-2 if timed out
911c609719bSwdenk  */
912c609719bSwdenk int readline (const char *const prompt)
913c609719bSwdenk {
914ecc5500eSPeter Tyser 	/*
915ecc5500eSPeter Tyser 	 * If console_buffer isn't 0-length the user will be prompted to modify
916ecc5500eSPeter Tyser 	 * it instead of entering it from scratch as desired.
917ecc5500eSPeter Tyser 	 */
918ecc5500eSPeter Tyser 	console_buffer[0] = '\0';
919ecc5500eSPeter Tyser 
9209c348311SHeiko Schocher 	return readline_into_buffer(prompt, console_buffer, 0);
9216636b62aSJames Yang }
9226636b62aSJames Yang 
9236636b62aSJames Yang 
9249c348311SHeiko Schocher int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
9256636b62aSJames Yang {
9266636b62aSJames Yang 	char *p = buffer;
927501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
9288804ae3bSPeter Tyser 	unsigned int len = CONFIG_SYS_CBSIZE;
929d8f961bbSStefan Roese 	int rc;
930501090aaSWolfgang Denk 	static int initted = 0;
931501090aaSWolfgang Denk 
932597f6c26SJames Yang 	/*
933597f6c26SJames Yang 	 * History uses a global array which is not
934597f6c26SJames Yang 	 * writable until after relocation to RAM.
935597f6c26SJames Yang 	 * Revert to non-history version if still
936597f6c26SJames Yang 	 * running from flash.
937597f6c26SJames Yang 	 */
938597f6c26SJames Yang 	if (gd->flags & GD_FLG_RELOC) {
939597f6c26SJames Yang 		if (!initted) {
940597f6c26SJames Yang 			hist_init();
941597f6c26SJames Yang 			initted = 1;
942597f6c26SJames Yang 		}
943597f6c26SJames Yang 
944e491a71eSPeter Tyser 		if (prompt)
945dd9f06f0SWolfgang Denk 			puts (prompt);
946501090aaSWolfgang Denk 
9479c348311SHeiko Schocher 		rc = cread_line(prompt, p, &len, timeout);
948d8f961bbSStefan Roese 		return rc < 0 ? rc : len;
949597f6c26SJames Yang 
950597f6c26SJames Yang 	} else {
951597f6c26SJames Yang #endif	/* CONFIG_CMDLINE_EDITING */
9520ec59524SKumar Gala 	char * p_buf = p;
953c609719bSwdenk 	int	n = 0;				/* buffer index		*/
954c609719bSwdenk 	int	plen = 0;			/* prompt length	*/
955c609719bSwdenk 	int	col;				/* output column cnt	*/
956c609719bSwdenk 	char	c;
957c609719bSwdenk 
958c609719bSwdenk 	/* print prompt */
959c609719bSwdenk 	if (prompt) {
960c609719bSwdenk 		plen = strlen (prompt);
961c609719bSwdenk 		puts (prompt);
962c609719bSwdenk 	}
963c609719bSwdenk 	col = plen;
964c609719bSwdenk 
965c609719bSwdenk 	for (;;) {
966c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
967c609719bSwdenk 		while (!tstc()) {	/* while no incoming data */
968c609719bSwdenk 			if (retry_time >= 0 && get_ticks() > endtime)
969c609719bSwdenk 				return (-2);	/* timed out */
97030dc165aSJens Scharsig 			WATCHDOG_RESET();
971c609719bSwdenk 		}
972c609719bSwdenk #endif
973c609719bSwdenk 		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
974c609719bSwdenk 
975c609719bSwdenk #ifdef CONFIG_SHOW_ACTIVITY
976c609719bSwdenk 		while (!tstc()) {
977c609719bSwdenk 			show_activity(0);
97830dc165aSJens Scharsig 			WATCHDOG_RESET();
979c609719bSwdenk 		}
980c609719bSwdenk #endif
981c609719bSwdenk 		c = getc();
982c609719bSwdenk 
983c609719bSwdenk 		/*
984c609719bSwdenk 		 * Special character handling
985c609719bSwdenk 		 */
986c609719bSwdenk 		switch (c) {
987c609719bSwdenk 		case '\r':				/* Enter		*/
988c609719bSwdenk 		case '\n':
989c609719bSwdenk 			*p = '\0';
990c609719bSwdenk 			puts ("\r\n");
9916636b62aSJames Yang 			return (p - p_buf);
992c609719bSwdenk 
99327aa8186Swdenk 		case '\0':				/* nul			*/
99427aa8186Swdenk 			continue;
99527aa8186Swdenk 
996c609719bSwdenk 		case 0x03:				/* ^C - break		*/
9976636b62aSJames Yang 			p_buf[0] = '\0';	/* discard input */
998c609719bSwdenk 			return (-1);
999c609719bSwdenk 
1000c609719bSwdenk 		case 0x15:				/* ^U - erase line	*/
1001c609719bSwdenk 			while (col > plen) {
1002c609719bSwdenk 				puts (erase_seq);
1003c609719bSwdenk 				--col;
1004c609719bSwdenk 			}
10056636b62aSJames Yang 			p = p_buf;
1006c609719bSwdenk 			n = 0;
1007c609719bSwdenk 			continue;
1008c609719bSwdenk 
1009c609719bSwdenk 		case 0x17:				/* ^W - erase word	*/
10106636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1011c609719bSwdenk 			while ((n > 0) && (*p != ' ')) {
10126636b62aSJames Yang 				p=delete_char(p_buf, p, &col, &n, plen);
1013c609719bSwdenk 			}
1014c609719bSwdenk 			continue;
1015c609719bSwdenk 
1016c609719bSwdenk 		case 0x08:				/* ^H  - backspace	*/
1017c609719bSwdenk 		case 0x7F:				/* DEL - backspace	*/
10186636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1019c609719bSwdenk 			continue;
1020c609719bSwdenk 
1021c609719bSwdenk 		default:
1022c609719bSwdenk 			/*
1023c609719bSwdenk 			 * Must be a normal character then
1024c609719bSwdenk 			 */
10256d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 			if (n < CONFIG_SYS_CBSIZE-2) {
1026c609719bSwdenk 				if (c == '\t') {	/* expand TABs		*/
102704a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
102804a85b3bSwdenk 					/* if auto completion triggered just continue */
102904a85b3bSwdenk 					*p = '\0';
103004a85b3bSwdenk 					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
10316636b62aSJames Yang 						p = p_buf + n;	/* reset */
103204a85b3bSwdenk 						continue;
103304a85b3bSwdenk 					}
103404a85b3bSwdenk #endif
1035c609719bSwdenk 					puts (tab_seq+(col&07));
1036c609719bSwdenk 					col += 8 - (col&07);
1037c609719bSwdenk 				} else {
1038c609719bSwdenk 					++col;		/* echo input		*/
1039c609719bSwdenk 					putc (c);
1040c609719bSwdenk 				}
1041c609719bSwdenk 				*p++ = c;
1042c609719bSwdenk 				++n;
1043c609719bSwdenk 			} else {			/* Buffer full		*/
1044c609719bSwdenk 				putc ('\a');
1045c609719bSwdenk 			}
1046c609719bSwdenk 		}
1047c609719bSwdenk 	}
1048597f6c26SJames Yang #ifdef CONFIG_CMDLINE_EDITING
1049597f6c26SJames Yang 	}
1050597f6c26SJames Yang #endif
1051c609719bSwdenk }
1052c609719bSwdenk 
1053c609719bSwdenk /****************************************************************************/
1054c609719bSwdenk 
1055c609719bSwdenk static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1056c609719bSwdenk {
1057c609719bSwdenk 	char *s;
1058c609719bSwdenk 
1059c609719bSwdenk 	if (*np == 0) {
1060c609719bSwdenk 		return (p);
1061c609719bSwdenk 	}
1062c609719bSwdenk 
1063c609719bSwdenk 	if (*(--p) == '\t') {			/* will retype the whole line	*/
1064c609719bSwdenk 		while (*colp > plen) {
1065c609719bSwdenk 			puts (erase_seq);
1066c609719bSwdenk 			(*colp)--;
1067c609719bSwdenk 		}
1068c609719bSwdenk 		for (s=buffer; s<p; ++s) {
1069c609719bSwdenk 			if (*s == '\t') {
1070c609719bSwdenk 				puts (tab_seq+((*colp) & 07));
1071c609719bSwdenk 				*colp += 8 - ((*colp) & 07);
1072c609719bSwdenk 			} else {
1073c609719bSwdenk 				++(*colp);
1074c609719bSwdenk 				putc (*s);
1075c609719bSwdenk 			}
1076c609719bSwdenk 		}
1077c609719bSwdenk 	} else {
1078c609719bSwdenk 		puts (erase_seq);
1079c609719bSwdenk 		(*colp)--;
1080c609719bSwdenk 	}
1081c609719bSwdenk 	(*np)--;
1082c609719bSwdenk 	return (p);
1083c609719bSwdenk }
1084c609719bSwdenk 
1085c609719bSwdenk /****************************************************************************/
1086c609719bSwdenk 
1087c609719bSwdenk int parse_line (char *line, char *argv[])
1088c609719bSwdenk {
1089c609719bSwdenk 	int nargs = 0;
1090c609719bSwdenk 
1091c609719bSwdenk #ifdef DEBUG_PARSER
1092c609719bSwdenk 	printf ("parse_line: \"%s\"\n", line);
1093c609719bSwdenk #endif
10946d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	while (nargs < CONFIG_SYS_MAXARGS) {
1095c609719bSwdenk 
1096c609719bSwdenk 		/* skip any white space */
10974d91a6ecSJason Hobbs 		while (isblank(*line))
1098c609719bSwdenk 			++line;
1099c609719bSwdenk 
1100c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1101c609719bSwdenk 			argv[nargs] = NULL;
1102c609719bSwdenk #ifdef DEBUG_PARSER
1103c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1104c609719bSwdenk #endif
1105c609719bSwdenk 			return (nargs);
1106c609719bSwdenk 		}
1107c609719bSwdenk 
1108c609719bSwdenk 		argv[nargs++] = line;	/* begin of argument string	*/
1109c609719bSwdenk 
1110c609719bSwdenk 		/* find end of string */
11114d91a6ecSJason Hobbs 		while (*line && !isblank(*line))
1112c609719bSwdenk 			++line;
1113c609719bSwdenk 
1114c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1115c609719bSwdenk 			argv[nargs] = NULL;
1116c609719bSwdenk #ifdef DEBUG_PARSER
1117c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1118c609719bSwdenk #endif
1119c609719bSwdenk 			return (nargs);
1120c609719bSwdenk 		}
1121c609719bSwdenk 
1122c609719bSwdenk 		*line++ = '\0';		/* terminate current arg	 */
1123c609719bSwdenk 	}
1124c609719bSwdenk 
11256d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1126c609719bSwdenk 
1127c609719bSwdenk #ifdef DEBUG_PARSER
1128c609719bSwdenk 	printf ("parse_line: nargs=%d\n", nargs);
1129c609719bSwdenk #endif
1130c609719bSwdenk 	return (nargs);
1131c609719bSwdenk }
1132c609719bSwdenk 
1133c609719bSwdenk /****************************************************************************/
1134c609719bSwdenk 
11357fed89e0SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
1136c609719bSwdenk static void process_macros (const char *input, char *output)
1137c609719bSwdenk {
1138c609719bSwdenk 	char c, prev;
1139c609719bSwdenk 	const char *varname_start = NULL;
1140c609719bSwdenk 	int inputcnt = strlen (input);
11416d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	int outputcnt = CONFIG_SYS_CBSIZE;
1142c609719bSwdenk 	int state = 0;		/* 0 = waiting for '$'  */
114319973b6aSWolfgang Denk 
11445cf91d6bSwdenk 	/* 1 = waiting for '(' or '{' */
11455cf91d6bSwdenk 	/* 2 = waiting for ')' or '}' */
1146a25f862bSwdenk 	/* 3 = waiting for '''  */
1147c609719bSwdenk #ifdef DEBUG_PARSER
1148c609719bSwdenk 	char *output_start = output;
1149c609719bSwdenk 
115019973b6aSWolfgang Denk 	printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
115119973b6aSWolfgang Denk 		input);
1152c609719bSwdenk #endif
1153c609719bSwdenk 
1154c609719bSwdenk 	prev = '\0';		/* previous character   */
1155c609719bSwdenk 
1156c609719bSwdenk 	while (inputcnt && outputcnt) {
1157c609719bSwdenk 		c = *input++;
1158c609719bSwdenk 		inputcnt--;
1159c609719bSwdenk 
1160a25f862bSwdenk 		if (state != 3) {
1161c609719bSwdenk 			/* remove one level of escape characters */
1162c609719bSwdenk 			if ((c == '\\') && (prev != '\\')) {
1163c609719bSwdenk 				if (inputcnt-- == 0)
1164c609719bSwdenk 					break;
1165c609719bSwdenk 				prev = c;
1166c609719bSwdenk 				c = *input++;
1167c609719bSwdenk 			}
1168a25f862bSwdenk 		}
1169c609719bSwdenk 
1170c609719bSwdenk 		switch (state) {
1171c609719bSwdenk 		case 0:	/* Waiting for (unescaped) $    */
1172a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1173a25f862bSwdenk 				state = 3;
1174a25f862bSwdenk 				break;
1175a25f862bSwdenk 			}
1176c609719bSwdenk 			if ((c == '$') && (prev != '\\')) {
1177c609719bSwdenk 				state++;
1178c609719bSwdenk 			} else {
1179c609719bSwdenk 				*(output++) = c;
1180c609719bSwdenk 				outputcnt--;
1181c609719bSwdenk 			}
1182c609719bSwdenk 			break;
1183c609719bSwdenk 		case 1:	/* Waiting for (        */
11845cf91d6bSwdenk 			if (c == '(' || c == '{') {
1185c609719bSwdenk 				state++;
1186c609719bSwdenk 				varname_start = input;
1187c609719bSwdenk 			} else {
1188c609719bSwdenk 				state = 0;
1189c609719bSwdenk 				*(output++) = '$';
1190c609719bSwdenk 				outputcnt--;
1191c609719bSwdenk 
1192c609719bSwdenk 				if (outputcnt) {
1193c609719bSwdenk 					*(output++) = c;
1194c609719bSwdenk 					outputcnt--;
1195c609719bSwdenk 				}
1196c609719bSwdenk 			}
1197c609719bSwdenk 			break;
1198c609719bSwdenk 		case 2:	/* Waiting for )        */
11995cf91d6bSwdenk 			if (c == ')' || c == '}') {
1200c609719bSwdenk 				int i;
12016d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 				char envname[CONFIG_SYS_CBSIZE], *envval;
1202c609719bSwdenk 				int envcnt = input - varname_start - 1;	/* Varname # of chars */
1203c609719bSwdenk 
1204c609719bSwdenk 				/* Get the varname */
1205c609719bSwdenk 				for (i = 0; i < envcnt; i++) {
1206c609719bSwdenk 					envname[i] = varname_start[i];
1207c609719bSwdenk 				}
1208c609719bSwdenk 				envname[i] = 0;
1209c609719bSwdenk 
1210c609719bSwdenk 				/* Get its value */
1211c609719bSwdenk 				envval = getenv (envname);
1212c609719bSwdenk 
1213c609719bSwdenk 				/* Copy into the line if it exists */
1214c609719bSwdenk 				if (envval != NULL)
1215c609719bSwdenk 					while ((*envval) && outputcnt) {
1216c609719bSwdenk 						*(output++) = *(envval++);
1217c609719bSwdenk 						outputcnt--;
1218c609719bSwdenk 					}
1219c609719bSwdenk 				/* Look for another '$' */
1220c609719bSwdenk 				state = 0;
1221c609719bSwdenk 			}
1222c609719bSwdenk 			break;
1223a25f862bSwdenk 		case 3:	/* Waiting for '        */
1224a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1225a25f862bSwdenk 				state = 0;
1226a25f862bSwdenk 			} else {
1227a25f862bSwdenk 				*(output++) = c;
1228a25f862bSwdenk 				outputcnt--;
1229c609719bSwdenk 			}
1230a25f862bSwdenk 			break;
1231a25f862bSwdenk 		}
1232c609719bSwdenk 		prev = c;
1233c609719bSwdenk 	}
1234c609719bSwdenk 
1235c609719bSwdenk 	if (outputcnt)
1236c609719bSwdenk 		*output = 0;
12379160b96fSBartlomiej Sieka 	else
12389160b96fSBartlomiej Sieka 		*(output - 1) = 0;
1239c609719bSwdenk 
1240c609719bSwdenk #ifdef DEBUG_PARSER
1241c609719bSwdenk 	printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1242c609719bSwdenk 		strlen (output_start), output_start);
1243c609719bSwdenk #endif
1244c609719bSwdenk }
1245c609719bSwdenk 
1246c609719bSwdenk /****************************************************************************
1247c609719bSwdenk  * returns:
1248c609719bSwdenk  *	1  - command executed, repeatable
1249c609719bSwdenk  *	0  - command executed but not repeatable, interrupted commands are
1250c609719bSwdenk  *	     always considered not repeatable
1251c609719bSwdenk  *	-1 - not executed (unrecognized, bootd recursion or too many args)
12526d0f6bcfSJean-Christophe PLAGNIOL-VILLARD  *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1253c609719bSwdenk  *           considered unrecognized)
1254c609719bSwdenk  *
1255c609719bSwdenk  * WARNING:
1256c609719bSwdenk  *
1257c609719bSwdenk  * We must create a temporary copy of the command since the command we get
1258c609719bSwdenk  * may be the result from getenv(), which returns a pointer directly to
1259c609719bSwdenk  * the environment data, which may change magicly when the command we run
1260c609719bSwdenk  * creates or modifies environment variables (like "bootp" does).
1261c609719bSwdenk  */
126253071532SSimon Glass static int builtin_run_command(const char *cmd, int flag)
1263c609719bSwdenk {
12646d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
1265c609719bSwdenk 	char *token;			/* start of token in cmdbuf	*/
1266c609719bSwdenk 	char *sep;			/* end of token (separator) in cmdbuf */
12676d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char finaltoken[CONFIG_SYS_CBSIZE];
1268c609719bSwdenk 	char *str = cmdbuf;
12696d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
1270f07771ccSwdenk 	int argc, inquotes;
1271c609719bSwdenk 	int repeatable = 1;
1272f07771ccSwdenk 	int rc = 0;
1273c609719bSwdenk 
1274c609719bSwdenk #ifdef DEBUG_PARSER
1275c609719bSwdenk 	printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1276c609719bSwdenk 	puts (cmd ? cmd : "NULL");	/* use puts - string may be loooong */
1277c609719bSwdenk 	puts ("\"\n");
1278c609719bSwdenk #endif
1279c609719bSwdenk 
1280c609719bSwdenk 	clear_ctrlc();		/* forget any previous Control C */
1281c609719bSwdenk 
1282c609719bSwdenk 	if (!cmd || !*cmd) {
1283c609719bSwdenk 		return -1;	/* empty command */
1284c609719bSwdenk 	}
1285c609719bSwdenk 
12866d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1287c609719bSwdenk 		puts ("## Command too long!\n");
1288c609719bSwdenk 		return -1;
1289c609719bSwdenk 	}
1290c609719bSwdenk 
1291c609719bSwdenk 	strcpy (cmdbuf, cmd);
1292c609719bSwdenk 
1293c609719bSwdenk 	/* Process separators and check for invalid
1294c609719bSwdenk 	 * repeatable commands
1295c609719bSwdenk 	 */
1296c609719bSwdenk 
1297c609719bSwdenk #ifdef DEBUG_PARSER
1298c609719bSwdenk 	printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1299c609719bSwdenk #endif
1300c609719bSwdenk 	while (*str) {
1301c609719bSwdenk 
1302c609719bSwdenk 		/*
1303c609719bSwdenk 		 * Find separator, or string end
1304c609719bSwdenk 		 * Allow simple escape of ';' by writing "\;"
1305c609719bSwdenk 		 */
1306a25f862bSwdenk 		for (inquotes = 0, sep = str; *sep; sep++) {
1307a25f862bSwdenk 			if ((*sep=='\'') &&
1308a25f862bSwdenk 			    (*(sep-1) != '\\'))
1309a25f862bSwdenk 				inquotes=!inquotes;
1310a25f862bSwdenk 
1311a25f862bSwdenk 			if (!inquotes &&
1312a25f862bSwdenk 			    (*sep == ';') &&	/* separator		*/
1313c609719bSwdenk 			    ( sep != str) &&	/* past string start	*/
1314c609719bSwdenk 			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
1315c609719bSwdenk 				break;
1316c609719bSwdenk 		}
1317c609719bSwdenk 
1318c609719bSwdenk 		/*
1319c609719bSwdenk 		 * Limit the token to data between separators
1320c609719bSwdenk 		 */
1321c609719bSwdenk 		token = str;
1322c609719bSwdenk 		if (*sep) {
1323c609719bSwdenk 			str = sep + 1;	/* start of command for next pass */
1324c609719bSwdenk 			*sep = '\0';
1325c609719bSwdenk 		}
1326c609719bSwdenk 		else
1327c609719bSwdenk 			str = sep;	/* no more commands for next pass */
1328c609719bSwdenk #ifdef DEBUG_PARSER
1329c609719bSwdenk 		printf ("token: \"%s\"\n", token);
1330c609719bSwdenk #endif
1331c609719bSwdenk 
1332c609719bSwdenk 		/* find macros in this token and replace them */
1333c609719bSwdenk 		process_macros (token, finaltoken);
1334c609719bSwdenk 
1335c609719bSwdenk 		/* Extract arguments */
13361264b405SWolfgang Denk 		if ((argc = parse_line (finaltoken, argv)) == 0) {
13371264b405SWolfgang Denk 			rc = -1;	/* no command at all */
13381264b405SWolfgang Denk 			continue;
13391264b405SWolfgang Denk 		}
1340c609719bSwdenk 
1341030fca52STimo Ketola 		if (cmd_process(flag, argc, argv, &repeatable))
1342030fca52STimo Ketola 			rc = -1;
1343c609719bSwdenk 
1344c609719bSwdenk 		/* Did the user stop this? */
1345c609719bSwdenk 		if (had_ctrlc ())
13465afb2020SDetlev Zundel 			return -1;	/* if stopped then not repeatable */
1347c609719bSwdenk 	}
1348c609719bSwdenk 
1349f07771ccSwdenk 	return rc ? rc : repeatable;
1350c609719bSwdenk }
13517fed89e0SSimon Glass #endif
1352c609719bSwdenk 
135353071532SSimon Glass /*
135453071532SSimon Glass  * Run a command using the selected parser.
135553071532SSimon Glass  *
135653071532SSimon Glass  * @param cmd	Command to run
135753071532SSimon Glass  * @param flag	Execution flags (CMD_FLAG_...)
135853071532SSimon Glass  * @return 0 on success, or != 0 on error.
135953071532SSimon Glass  */
136053071532SSimon Glass int run_command(const char *cmd, int flag)
136153071532SSimon Glass {
136253071532SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
136353071532SSimon Glass 	/*
136453071532SSimon Glass 	 * builtin_run_command can return 0 or 1 for success, so clean up
136553071532SSimon Glass 	 * its result.
136653071532SSimon Glass 	 */
136753071532SSimon Glass 	if (builtin_run_command(cmd, flag) == -1)
136853071532SSimon Glass 		return 1;
136953071532SSimon Glass 
137053071532SSimon Glass 	return 0;
137153071532SSimon Glass #else
137253071532SSimon Glass 	return parse_string_outer(cmd,
137353071532SSimon Glass 			FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
137453071532SSimon Glass #endif
137553071532SSimon Glass }
137653071532SSimon Glass 
1377*d51004a8SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
1378*d51004a8SSimon Glass /**
1379*d51004a8SSimon Glass  * Execute a list of command separated by ; or \n using the built-in parser.
1380*d51004a8SSimon Glass  *
1381*d51004a8SSimon Glass  * This function cannot take a const char * for the command, since if it
1382*d51004a8SSimon Glass  * finds newlines in the string, it replaces them with \0.
1383*d51004a8SSimon Glass  *
1384*d51004a8SSimon Glass  * @param cmd	String containing list of commands
1385*d51004a8SSimon Glass  * @param flag	Execution flags (CMD_FLAG_...)
1386*d51004a8SSimon Glass  * @return 0 on success, or != 0 on error.
1387*d51004a8SSimon Glass  */
1388*d51004a8SSimon Glass static int builtin_run_command_list(char *cmd, int flag)
1389*d51004a8SSimon Glass {
1390*d51004a8SSimon Glass 	char *line, *next;
1391*d51004a8SSimon Glass 	int rcode = 0;
1392*d51004a8SSimon Glass 
1393*d51004a8SSimon Glass 	/*
1394*d51004a8SSimon Glass 	 * Break into individual lines, and execute each line; terminate on
1395*d51004a8SSimon Glass 	 * error.
1396*d51004a8SSimon Glass 	 */
1397*d51004a8SSimon Glass 	line = next = cmd;
1398*d51004a8SSimon Glass 	while (*next) {
1399*d51004a8SSimon Glass 		if (*next == '\n') {
1400*d51004a8SSimon Glass 			*next = '\0';
1401*d51004a8SSimon Glass 			/* run only non-empty commands */
1402*d51004a8SSimon Glass 			if (*line) {
1403*d51004a8SSimon Glass 				debug("** exec: \"%s\"\n", line);
1404*d51004a8SSimon Glass 				if (builtin_run_command(line, 0) < 0) {
1405*d51004a8SSimon Glass 					rcode = 1;
1406*d51004a8SSimon Glass 					break;
1407*d51004a8SSimon Glass 				}
1408*d51004a8SSimon Glass 			}
1409*d51004a8SSimon Glass 			line = next + 1;
1410*d51004a8SSimon Glass 		}
1411*d51004a8SSimon Glass 		++next;
1412*d51004a8SSimon Glass 	}
1413*d51004a8SSimon Glass 	if (rcode == 0 && *line)
1414*d51004a8SSimon Glass 		rcode = (builtin_run_command(line, 0) >= 0);
1415*d51004a8SSimon Glass 
1416*d51004a8SSimon Glass 	return rcode;
1417*d51004a8SSimon Glass }
1418*d51004a8SSimon Glass #endif
1419*d51004a8SSimon Glass 
1420*d51004a8SSimon Glass int run_command_list(const char *cmd, int len, int flag)
1421*d51004a8SSimon Glass {
1422*d51004a8SSimon Glass 	int need_buff = 1;
1423*d51004a8SSimon Glass 	char *buff = (char *)cmd;	/* cast away const */
1424*d51004a8SSimon Glass 	int rcode = 0;
1425*d51004a8SSimon Glass 
1426*d51004a8SSimon Glass 	if (len == -1) {
1427*d51004a8SSimon Glass 		len = strlen(cmd);
1428*d51004a8SSimon Glass #ifdef CONFIG_SYS_HUSH_PARSER
1429*d51004a8SSimon Glass 		/* hush will never change our string */
1430*d51004a8SSimon Glass 		need_buff = 0;
1431*d51004a8SSimon Glass #else
1432*d51004a8SSimon Glass 		/* the built-in parser will change our string if it sees \n */
1433*d51004a8SSimon Glass 		need_buff = strchr(cmd, '\n') != NULL;
1434*d51004a8SSimon Glass #endif
1435*d51004a8SSimon Glass 	}
1436*d51004a8SSimon Glass 	if (need_buff) {
1437*d51004a8SSimon Glass 		buff = malloc(len + 1);
1438*d51004a8SSimon Glass 		if (!buff)
1439*d51004a8SSimon Glass 			return 1;
1440*d51004a8SSimon Glass 		memcpy(buff, cmd, len);
1441*d51004a8SSimon Glass 		buff[len] = '\0';
1442*d51004a8SSimon Glass 	}
1443*d51004a8SSimon Glass #ifdef CONFIG_SYS_HUSH_PARSER
1444*d51004a8SSimon Glass 	rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1445*d51004a8SSimon Glass #else
1446*d51004a8SSimon Glass 	/*
1447*d51004a8SSimon Glass 	 * This function will overwrite any \n it sees with a \0, which
1448*d51004a8SSimon Glass 	 * is why it can't work with a const char *. Here we are making
1449*d51004a8SSimon Glass 	 * using of internal knowledge of this function, to avoid always
1450*d51004a8SSimon Glass 	 * doing a malloc() which is actually required only in a case that
1451*d51004a8SSimon Glass 	 * is pretty rare.
1452*d51004a8SSimon Glass 	 */
1453*d51004a8SSimon Glass 	rcode = builtin_run_command_list(buff, flag);
1454*d51004a8SSimon Glass 	if (need_buff)
1455*d51004a8SSimon Glass 		free(buff);
1456*d51004a8SSimon Glass #endif
1457*d51004a8SSimon Glass 
1458*d51004a8SSimon Glass 	return rcode;
1459*d51004a8SSimon Glass }
1460*d51004a8SSimon Glass 
1461c609719bSwdenk /****************************************************************************/
1462c609719bSwdenk 
1463c3517f91SJon Loeliger #if defined(CONFIG_CMD_RUN)
146454841ab5SWolfgang Denk int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1465c609719bSwdenk {
1466c609719bSwdenk 	int i;
1467c609719bSwdenk 
146847e26b1bSWolfgang Denk 	if (argc < 2)
14694c12eeb8SSimon Glass 		return CMD_RET_USAGE;
1470c609719bSwdenk 
1471c609719bSwdenk 	for (i=1; i<argc; ++i) {
14723e38691eSwdenk 		char *arg;
14733e38691eSwdenk 
14743e38691eSwdenk 		if ((arg = getenv (argv[i])) == NULL) {
14753e38691eSwdenk 			printf ("## Error: \"%s\" not defined\n", argv[i]);
14763e38691eSwdenk 			return 1;
14773e38691eSwdenk 		}
1478c8a2079eSJason Hobbs 
1479009dde19SSimon Glass 		if (run_command(arg, flag) != 0)
14803e38691eSwdenk 			return 1;
1481c609719bSwdenk 	}
14823e38691eSwdenk 	return 0;
1483c609719bSwdenk }
148490253178SJon Loeliger #endif
1485