xref: /openbmc/u-boot/common/main.c (revision c8a2079e49ee15dde10278865425ec9c36215f31)
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>
3309c2e90cSAndreas Bießmann #include <version.h>
34d9631ecfSWolfgang Denk #ifdef CONFIG_MODEM_SUPPORT
35d9631ecfSWolfgang Denk #include <malloc.h>		/* for free() prototype */
36d9631ecfSWolfgang Denk #endif
37c609719bSwdenk 
386d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
39c609719bSwdenk #include <hush.h>
40c609719bSwdenk #endif
41c609719bSwdenk 
42bdccc4feSwdenk #include <post.h>
43bdccc4feSwdenk 
44597f6c26SJames Yang #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
45d87080b7SWolfgang Denk DECLARE_GLOBAL_DATA_PTR;
46d87080b7SWolfgang Denk #endif
47d87080b7SWolfgang Denk 
48fad63407SHeiko Schocher /*
49fad63407SHeiko Schocher  * Board-specific Platform code can reimplement show_boot_progress () if needed
50fad63407SHeiko Schocher  */
51fad63407SHeiko Schocher void inline __show_boot_progress (int val) {}
525e2c08c3SEmil Medve void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
53fad63407SHeiko Schocher 
544bae9090SBartlomiej Sieka #if defined(CONFIG_UPDATE_TFTP)
558d6b7320SAndreas Pretzsch int update_tftp (ulong addr);
564bae9090SBartlomiej Sieka #endif /* CONFIG_UPDATE_TFTP */
578bde7f77Swdenk 
58c609719bSwdenk #define MAX_DELAY_STOP_STR 32
59c609719bSwdenk 
60c609719bSwdenk #undef DEBUG_PARSER
61c609719bSwdenk 
626475b9f9SJohn Schmoller char        console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
63c609719bSwdenk 
643ca9122fSStefan Roese static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
6582359aecSMike Frysinger static const char erase_seq[] = "\b \b";		/* erase sequence	*/
6682359aecSMike Frysinger static const char   tab_seq[] = "        ";		/* used to expand TABs	*/
67c609719bSwdenk 
68c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
69c609719bSwdenk static uint64_t endtime = 0;  /* must be set, default is instant timeout */
70c609719bSwdenk static int      retry_time = -1; /* -1 so can call readline before main_loop */
71c609719bSwdenk #endif
72c609719bSwdenk 
73c609719bSwdenk #define	endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
74c609719bSwdenk 
75c609719bSwdenk #ifndef CONFIG_BOOT_RETRY_MIN
76c609719bSwdenk #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
77c609719bSwdenk #endif
78c609719bSwdenk 
79c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
80c609719bSwdenk int do_mdm_init = 0;
81c609719bSwdenk extern void mdm_init(void); /* defined in board.c */
82c609719bSwdenk #endif
83c609719bSwdenk 
84c609719bSwdenk /***************************************************************************
85c609719bSwdenk  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
86*c8a2079eSJason Hobbs  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
87c609719bSwdenk  */
88c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
89c609719bSwdenk # if defined(CONFIG_AUTOBOOT_KEYED)
90b41bc5a8SJason Hobbs #ifndef CONFIG_MENU
91b41bc5a8SJason Hobbs static inline
92b41bc5a8SJason Hobbs #endif
93b41bc5a8SJason Hobbs int abortboot(int bootdelay)
94c609719bSwdenk {
95c609719bSwdenk 	int abort = 0;
96c609719bSwdenk 	uint64_t etime = endtick(bootdelay);
9719973b6aSWolfgang Denk 	struct {
98c609719bSwdenk 		char* str;
99c609719bSwdenk 		u_int len;
100c609719bSwdenk 		int retry;
101c609719bSwdenk 	}
10219973b6aSWolfgang Denk 	delaykey [] = {
103c609719bSwdenk 		{ str: getenv ("bootdelaykey"),  retry: 1 },
104c609719bSwdenk 		{ str: getenv ("bootdelaykey2"), retry: 1 },
105c609719bSwdenk 		{ str: getenv ("bootstopkey"),   retry: 0 },
106c609719bSwdenk 		{ str: getenv ("bootstopkey2"),  retry: 0 },
107c609719bSwdenk 	};
108c609719bSwdenk 
109c609719bSwdenk 	char presskey [MAX_DELAY_STOP_STR];
110c609719bSwdenk 	u_int presskey_len = 0;
111c609719bSwdenk 	u_int presskey_max = 0;
112c609719bSwdenk 	u_int i;
113c609719bSwdenk 
114c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_PROMPT
115f2302d44SStefan Roese 	printf(CONFIG_AUTOBOOT_PROMPT);
116c609719bSwdenk #  endif
117c609719bSwdenk 
118c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR
119c609719bSwdenk 	if (delaykey[0].str == NULL)
120c609719bSwdenk 		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
121c609719bSwdenk #  endif
122c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR2
123c609719bSwdenk 	if (delaykey[1].str == NULL)
124c609719bSwdenk 		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
125c609719bSwdenk #  endif
126c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR
127c609719bSwdenk 	if (delaykey[2].str == NULL)
128c609719bSwdenk 		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
129c609719bSwdenk #  endif
130c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR2
131c609719bSwdenk 	if (delaykey[3].str == NULL)
132c609719bSwdenk 		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
133c609719bSwdenk #  endif
134c609719bSwdenk 
135c609719bSwdenk 	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
136c609719bSwdenk 		delaykey[i].len = delaykey[i].str == NULL ?
137c609719bSwdenk 				    0 : strlen (delaykey[i].str);
138c609719bSwdenk 		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
139c609719bSwdenk 				    MAX_DELAY_STOP_STR : delaykey[i].len;
140c609719bSwdenk 
141c609719bSwdenk 		presskey_max = presskey_max > delaykey[i].len ?
142c609719bSwdenk 				    presskey_max : delaykey[i].len;
143c609719bSwdenk 
144c609719bSwdenk #  if DEBUG_BOOTKEYS
145c609719bSwdenk 		printf("%s key:<%s>\n",
146c609719bSwdenk 		       delaykey[i].retry ? "delay" : "stop",
147c609719bSwdenk 		       delaykey[i].str ? delaykey[i].str : "NULL");
148c609719bSwdenk #  endif
149c609719bSwdenk 	}
150c609719bSwdenk 
151c609719bSwdenk 	/* In order to keep up with incoming data, check timeout only
152c609719bSwdenk 	 * when catch up.
153c609719bSwdenk 	 */
154c3284b03SPeter Korsgaard 	do {
155c3284b03SPeter Korsgaard 		if (tstc()) {
156c3284b03SPeter Korsgaard 			if (presskey_len < presskey_max) {
157c3284b03SPeter Korsgaard 				presskey [presskey_len ++] = getc();
158c3284b03SPeter Korsgaard 			}
159c3284b03SPeter Korsgaard 			else {
160c3284b03SPeter Korsgaard 				for (i = 0; i < presskey_max - 1; i ++)
161c3284b03SPeter Korsgaard 					presskey [i] = presskey [i + 1];
162c3284b03SPeter Korsgaard 
163c3284b03SPeter Korsgaard 				presskey [i] = getc();
164c3284b03SPeter Korsgaard 			}
165c3284b03SPeter Korsgaard 		}
166c3284b03SPeter Korsgaard 
167c609719bSwdenk 		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
168c609719bSwdenk 			if (delaykey[i].len > 0 &&
169c609719bSwdenk 			    presskey_len >= delaykey[i].len &&
170c609719bSwdenk 			    memcmp (presskey + presskey_len - delaykey[i].len,
171c609719bSwdenk 				    delaykey[i].str,
172c609719bSwdenk 				    delaykey[i].len) == 0) {
173c609719bSwdenk #  if DEBUG_BOOTKEYS
174c609719bSwdenk 				printf("got %skey\n",
175c609719bSwdenk 				       delaykey[i].retry ? "delay" : "stop");
176c609719bSwdenk #  endif
177c609719bSwdenk 
178c609719bSwdenk #  ifdef CONFIG_BOOT_RETRY_TIME
179c609719bSwdenk 				/* don't retry auto boot */
180c609719bSwdenk 				if (! delaykey[i].retry)
181c609719bSwdenk 					retry_time = -1;
182c609719bSwdenk #  endif
183c609719bSwdenk 				abort = 1;
184c609719bSwdenk 			}
185c609719bSwdenk 		}
186c3284b03SPeter Korsgaard 	} while (!abort && get_ticks() <= etime);
187c609719bSwdenk 
188c609719bSwdenk #  if DEBUG_BOOTKEYS
189c609719bSwdenk 	if (!abort)
1904b9206edSwdenk 		puts("key timeout\n");
191c609719bSwdenk #  endif
192c609719bSwdenk 
1938cb8143eSdzu #ifdef CONFIG_SILENT_CONSOLE
1944ec5bd55SLadislav Michl 	if (abort)
1954ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
1968cb8143eSdzu #endif
1978cb8143eSdzu 
198c609719bSwdenk 	return abort;
199c609719bSwdenk }
200c609719bSwdenk 
201c609719bSwdenk # else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
202c609719bSwdenk 
203c7de829cSwdenk #ifdef CONFIG_MENUKEY
204c7de829cSwdenk static int menukey = 0;
205c7de829cSwdenk #endif
206c7de829cSwdenk 
207b41bc5a8SJason Hobbs #ifndef CONFIG_MENU
208b41bc5a8SJason Hobbs static inline
209b41bc5a8SJason Hobbs #endif
210b41bc5a8SJason Hobbs int abortboot(int bootdelay)
211c609719bSwdenk {
212c609719bSwdenk 	int abort = 0;
213c609719bSwdenk 
214c7de829cSwdenk #ifdef CONFIG_MENUPROMPT
215f2302d44SStefan Roese 	printf(CONFIG_MENUPROMPT);
216c7de829cSwdenk #else
217c609719bSwdenk 	printf("Hit any key to stop autoboot: %2d ", bootdelay);
218c7de829cSwdenk #endif
219c609719bSwdenk 
220c609719bSwdenk #if defined CONFIG_ZERO_BOOTDELAY_CHECK
221c609719bSwdenk 	/*
222c609719bSwdenk 	 * Check if key already pressed
223c609719bSwdenk 	 * Don't check if bootdelay < 0
224c609719bSwdenk 	 */
225c609719bSwdenk 	if (bootdelay >= 0) {
226c609719bSwdenk 		if (tstc()) {	/* we got a key press	*/
227c609719bSwdenk 			(void) getc();  /* consume input	*/
2284b9206edSwdenk 			puts ("\b\b\b 0");
229f72da340Swdenk 			abort = 1;	/* don't auto boot	*/
230c609719bSwdenk 		}
231c609719bSwdenk 	}
232c609719bSwdenk #endif
233c609719bSwdenk 
234f72da340Swdenk 	while ((bootdelay > 0) && (!abort)) {
235c609719bSwdenk 		int i;
236c609719bSwdenk 
237c609719bSwdenk 		--bootdelay;
238c609719bSwdenk 		/* delay 100 * 10ms */
239c609719bSwdenk 		for (i=0; !abort && i<100; ++i) {
240c609719bSwdenk 			if (tstc()) {	/* we got a key press	*/
241c609719bSwdenk 				abort  = 1;	/* don't auto boot	*/
242c609719bSwdenk 				bootdelay = 0;	/* no more delay	*/
243c7de829cSwdenk # ifdef CONFIG_MENUKEY
244c7de829cSwdenk 				menukey = getc();
245c7de829cSwdenk # else
246c609719bSwdenk 				(void) getc();  /* consume input	*/
247c7de829cSwdenk # endif
248c609719bSwdenk 				break;
249c609719bSwdenk 			}
250c609719bSwdenk 			udelay(10000);
251c609719bSwdenk 		}
252c609719bSwdenk 
253c609719bSwdenk 		printf("\b\b\b%2d ", bootdelay);
254c609719bSwdenk 	}
255c609719bSwdenk 
256c609719bSwdenk 	putc('\n');
257c609719bSwdenk 
258f72da340Swdenk #ifdef CONFIG_SILENT_CONSOLE
2594ec5bd55SLadislav Michl 	if (abort)
2604ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
261f72da340Swdenk #endif
262f72da340Swdenk 
263c609719bSwdenk 	return abort;
264c609719bSwdenk }
265c609719bSwdenk # endif	/* CONFIG_AUTOBOOT_KEYED */
266c609719bSwdenk #endif	/* CONFIG_BOOTDELAY >= 0  */
267c609719bSwdenk 
268*c8a2079eSJason Hobbs /*
269*c8a2079eSJason Hobbs  * Return 0 on success, or != 0 on error.
270*c8a2079eSJason Hobbs  */
271*c8a2079eSJason Hobbs static inline
272*c8a2079eSJason Hobbs int run_command2(const char *cmd, int flag)
273*c8a2079eSJason Hobbs {
274*c8a2079eSJason Hobbs #ifndef CONFIG_SYS_HUSH_PARSER
275*c8a2079eSJason Hobbs 	/*
276*c8a2079eSJason Hobbs 	 * run_command can return 0 or 1 for success, so clean up its result.
277*c8a2079eSJason Hobbs 	 */
278*c8a2079eSJason Hobbs 	if (run_command(cmd, flag) == -1)
279*c8a2079eSJason Hobbs 		return 1;
280*c8a2079eSJason Hobbs 
281*c8a2079eSJason Hobbs 	return 0;
282*c8a2079eSJason Hobbs #else
283*c8a2079eSJason Hobbs 	return parse_string_outer(cmd,
284*c8a2079eSJason Hobbs 			FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
285*c8a2079eSJason Hobbs #endif
286*c8a2079eSJason Hobbs }
287*c8a2079eSJason Hobbs 
288c609719bSwdenk /****************************************************************************/
289c609719bSwdenk 
290c609719bSwdenk void main_loop (void)
291c609719bSwdenk {
2926d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_HUSH_PARSER
2936d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
294c609719bSwdenk 	int len;
295c609719bSwdenk 	int rc = 1;
296c609719bSwdenk 	int flag;
297c609719bSwdenk #endif
298c609719bSwdenk 
299c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
300c609719bSwdenk 	char *s;
301c609719bSwdenk 	int bootdelay;
302c609719bSwdenk #endif
303c609719bSwdenk #ifdef CONFIG_PREBOOT
304c609719bSwdenk 	char *p;
305c609719bSwdenk #endif
306bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
307bdccc4feSwdenk 	unsigned long bootcount = 0;
308bdccc4feSwdenk 	unsigned long bootlimit = 0;
309bdccc4feSwdenk 	char *bcs;
310bdccc4feSwdenk 	char bcs_set[16];
311bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
312c609719bSwdenk 
313bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
314bdccc4feSwdenk 	bootcount = bootcount_load();
315bdccc4feSwdenk 	bootcount++;
316bdccc4feSwdenk 	bootcount_store (bootcount);
317bdccc4feSwdenk 	sprintf (bcs_set, "%lu", bootcount);
318bdccc4feSwdenk 	setenv ("bootcount", bcs_set);
319bdccc4feSwdenk 	bcs = getenv ("bootlimit");
320bdccc4feSwdenk 	bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
321bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
322bdccc4feSwdenk 
323c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
324c609719bSwdenk 	debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
325c609719bSwdenk 	if (do_mdm_init) {
32677ddac94SWolfgang Denk 		char *str = strdup(getenv("mdm_cmd"));
327c609719bSwdenk 		setenv ("preboot", str);  /* set or delete definition */
328c609719bSwdenk 		if (str != NULL)
329c609719bSwdenk 			free (str);
330c609719bSwdenk 		mdm_init(); /* wait for modem connection */
331c609719bSwdenk 	}
332c609719bSwdenk #endif  /* CONFIG_MODEM_SUPPORT */
333c609719bSwdenk 
3340587597cSstroese #ifdef CONFIG_VERSION_VARIABLE
3350587597cSstroese 	{
3360587597cSstroese 		setenv ("ver", version_string);  /* set version variable */
3370587597cSstroese 	}
3380587597cSstroese #endif /* CONFIG_VERSION_VARIABLE */
3390587597cSstroese 
3406d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
341c609719bSwdenk 	u_boot_hush_start ();
342c609719bSwdenk #endif
343c609719bSwdenk 
34481473f67SHeiko Schocher #if defined(CONFIG_HUSH_INIT_VAR)
34581473f67SHeiko Schocher 	hush_init_var ();
34681473f67SHeiko Schocher #endif
34781473f67SHeiko Schocher 
348c609719bSwdenk #ifdef CONFIG_PREBOOT
349c609719bSwdenk 	if ((p = getenv ("preboot")) != NULL) {
350c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
351c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
352c609719bSwdenk # endif
353c609719bSwdenk 
354*c8a2079eSJason Hobbs 		run_command2(p, 0);
355c609719bSwdenk 
356c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
357c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
358c609719bSwdenk # endif
359c609719bSwdenk 	}
360c609719bSwdenk #endif /* CONFIG_PREBOOT */
361c609719bSwdenk 
362143cd21fSWolfgang Denk #if defined(CONFIG_UPDATE_TFTP)
3638d6b7320SAndreas Pretzsch 	update_tftp (0UL);
364143cd21fSWolfgang Denk #endif /* CONFIG_UPDATE_TFTP */
365143cd21fSWolfgang Denk 
366c609719bSwdenk #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
367c609719bSwdenk 	s = getenv ("bootdelay");
368c609719bSwdenk 	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
369c609719bSwdenk 
370a6c7ad2fSwdenk 	debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
371c609719bSwdenk 
372c609719bSwdenk # ifdef CONFIG_BOOT_RETRY_TIME
3736dd652faSwdenk 	init_cmd_timeout ();
374c609719bSwdenk # endif	/* CONFIG_BOOT_RETRY_TIME */
375c609719bSwdenk 
376b428f6a8SYuri Tikhonov #ifdef CONFIG_POST
377b428f6a8SYuri Tikhonov 	if (gd->flags & GD_FLG_POSTFAIL) {
378b428f6a8SYuri Tikhonov 		s = getenv("failbootcmd");
379b428f6a8SYuri Tikhonov 	}
380b428f6a8SYuri Tikhonov 	else
381b428f6a8SYuri Tikhonov #endif /* CONFIG_POST */
382bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
383bdccc4feSwdenk 	if (bootlimit && (bootcount > bootlimit)) {
384bdccc4feSwdenk 		printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
385bdccc4feSwdenk 		        (unsigned)bootlimit);
386bdccc4feSwdenk 		s = getenv ("altbootcmd");
387bdccc4feSwdenk 	}
388bdccc4feSwdenk 	else
389bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
390c609719bSwdenk 		s = getenv ("bootcmd");
391a6c7ad2fSwdenk 
392a6c7ad2fSwdenk 	debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
393a6c7ad2fSwdenk 
394c609719bSwdenk 	if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
395c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
396c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
397c609719bSwdenk # endif
398c609719bSwdenk 
399*c8a2079eSJason Hobbs 		run_command2(s, 0);
400c609719bSwdenk 
401c609719bSwdenk # ifdef CONFIG_AUTOBOOT_KEYED
402c609719bSwdenk 		disable_ctrlc(prev);	/* restore Control C checking */
403c609719bSwdenk # endif
404c609719bSwdenk 	}
405c7de829cSwdenk 
406c7de829cSwdenk # ifdef CONFIG_MENUKEY
407a6c7ad2fSwdenk 	if (menukey == CONFIG_MENUKEY) {
408c7de829cSwdenk 		s = getenv("menucmd");
409*c8a2079eSJason Hobbs 		if (s)
410*c8a2079eSJason Hobbs 			run_command2(s, 0);
411c7de829cSwdenk 	}
412c7de829cSwdenk #endif /* CONFIG_MENUKEY */
413c609719bSwdenk #endif /* CONFIG_BOOTDELAY */
414c7de829cSwdenk 
415c609719bSwdenk 	/*
416c609719bSwdenk 	 * Main Loop for Monitor Command Processing
417c609719bSwdenk 	 */
4186d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
419c609719bSwdenk 	parse_file_outer();
420c609719bSwdenk 	/* This point is never reached */
421c609719bSwdenk 	for (;;);
422c609719bSwdenk #else
423c609719bSwdenk 	for (;;) {
424c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
425c609719bSwdenk 		if (rc >= 0) {
426c609719bSwdenk 			/* Saw enough of a valid command to
427c609719bSwdenk 			 * restart the timeout.
428c609719bSwdenk 			 */
429c609719bSwdenk 			reset_cmd_timeout();
430c609719bSwdenk 		}
431c609719bSwdenk #endif
4326d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		len = readline (CONFIG_SYS_PROMPT);
433c609719bSwdenk 
434c609719bSwdenk 		flag = 0;	/* assume no special flags for now */
435c609719bSwdenk 		if (len > 0)
436c609719bSwdenk 			strcpy (lastcommand, console_buffer);
437c609719bSwdenk 		else if (len == 0)
438c609719bSwdenk 			flag |= CMD_FLAG_REPEAT;
439c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
440c609719bSwdenk 		else if (len == -2) {
441c609719bSwdenk 			/* -2 means timed out, retry autoboot
442c609719bSwdenk 			 */
4434b9206edSwdenk 			puts ("\nTimed out waiting for command\n");
444c609719bSwdenk # ifdef CONFIG_RESET_TO_RETRY
445c609719bSwdenk 			/* Reinit board to run initialization code again */
446c609719bSwdenk 			do_reset (NULL, 0, 0, NULL);
447c609719bSwdenk # else
448c609719bSwdenk 			return;		/* retry autoboot */
449c609719bSwdenk # endif
450c609719bSwdenk 		}
451c609719bSwdenk #endif
452c609719bSwdenk 
453c609719bSwdenk 		if (len == -1)
4544b9206edSwdenk 			puts ("<INTERRUPT>\n");
455c609719bSwdenk 		else
456c609719bSwdenk 			rc = run_command (lastcommand, flag);
457c609719bSwdenk 
458c609719bSwdenk 		if (rc <= 0) {
459c609719bSwdenk 			/* invalid command or not repeatable, forget it */
460c609719bSwdenk 			lastcommand[0] = 0;
461c609719bSwdenk 		}
462c609719bSwdenk 	}
4636d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /*CONFIG_SYS_HUSH_PARSER*/
464c609719bSwdenk }
465c609719bSwdenk 
4666dd652faSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
4676dd652faSwdenk /***************************************************************************
46819973b6aSWolfgang Denk  * initialize command line timeout
4696dd652faSwdenk  */
4706dd652faSwdenk void init_cmd_timeout(void)
4716dd652faSwdenk {
4726dd652faSwdenk 	char *s = getenv ("bootretry");
4736dd652faSwdenk 
4746dd652faSwdenk 	if (s != NULL)
475b028f715Swdenk 		retry_time = (int)simple_strtol(s, NULL, 10);
4766dd652faSwdenk 	else
4776dd652faSwdenk 		retry_time =  CONFIG_BOOT_RETRY_TIME;
4786dd652faSwdenk 
4796dd652faSwdenk 	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
4806dd652faSwdenk 		retry_time = CONFIG_BOOT_RETRY_MIN;
4816dd652faSwdenk }
4826dd652faSwdenk 
483c609719bSwdenk /***************************************************************************
484c609719bSwdenk  * reset command line timeout to retry_time seconds
485c609719bSwdenk  */
486c609719bSwdenk void reset_cmd_timeout(void)
487c609719bSwdenk {
488c609719bSwdenk 	endtime = endtick(retry_time);
489c609719bSwdenk }
490c609719bSwdenk #endif
491c609719bSwdenk 
492501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
493501090aaSWolfgang Denk 
494501090aaSWolfgang Denk /*
495501090aaSWolfgang Denk  * cmdline-editing related codes from vivi.
496501090aaSWolfgang Denk  * Author: Janghoon Lyu <nandy@mizi.com>
497501090aaSWolfgang Denk  */
498501090aaSWolfgang Denk 
499501090aaSWolfgang Denk #define putnstr(str,n)	do {			\
500dc4b0b38SAndrew Klossner 		printf ("%.*s", (int)n, str);	\
501501090aaSWolfgang Denk 	} while (0)
502501090aaSWolfgang Denk 
503501090aaSWolfgang Denk #define CTL_CH(c)		((c) - 'a' + 1)
504501090aaSWolfgang Denk #define CTL_BACKSPACE		('\b')
505501090aaSWolfgang Denk #define DEL			((char)255)
506501090aaSWolfgang Denk #define DEL7			((char)127)
507501090aaSWolfgang Denk #define CREAD_HIST_CHAR		('!')
508501090aaSWolfgang Denk 
509501090aaSWolfgang Denk #define getcmd_putch(ch)	putc(ch)
510501090aaSWolfgang Denk #define getcmd_getch()		getc()
511501090aaSWolfgang Denk #define getcmd_cbeep()		getcmd_putch('\a')
512501090aaSWolfgang Denk 
513501090aaSWolfgang Denk #define HIST_MAX		20
5148804ae3bSPeter Tyser #define HIST_SIZE		CONFIG_SYS_CBSIZE
515501090aaSWolfgang Denk 
516501090aaSWolfgang Denk static int hist_max = 0;
517501090aaSWolfgang Denk static int hist_add_idx = 0;
518501090aaSWolfgang Denk static int hist_cur = -1;
519501090aaSWolfgang Denk unsigned hist_num = 0;
520501090aaSWolfgang Denk 
521501090aaSWolfgang Denk char* hist_list[HIST_MAX];
5226475b9f9SJohn Schmoller char hist_lines[HIST_MAX][HIST_SIZE + 1];	 /* Save room for NULL */
523501090aaSWolfgang Denk 
524501090aaSWolfgang Denk #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
525501090aaSWolfgang Denk 
526501090aaSWolfgang Denk static void hist_init(void)
527501090aaSWolfgang Denk {
528501090aaSWolfgang Denk 	int i;
529501090aaSWolfgang Denk 
530501090aaSWolfgang Denk 	hist_max = 0;
531501090aaSWolfgang Denk 	hist_add_idx = 0;
532501090aaSWolfgang Denk 	hist_cur = -1;
533501090aaSWolfgang Denk 	hist_num = 0;
534501090aaSWolfgang Denk 
535501090aaSWolfgang Denk 	for (i = 0; i < HIST_MAX; i++) {
536501090aaSWolfgang Denk 		hist_list[i] = hist_lines[i];
537501090aaSWolfgang Denk 		hist_list[i][0] = '\0';
538501090aaSWolfgang Denk 	}
539501090aaSWolfgang Denk }
540501090aaSWolfgang Denk 
541501090aaSWolfgang Denk static void cread_add_to_hist(char *line)
542501090aaSWolfgang Denk {
543501090aaSWolfgang Denk 	strcpy(hist_list[hist_add_idx], line);
544501090aaSWolfgang Denk 
545501090aaSWolfgang Denk 	if (++hist_add_idx >= HIST_MAX)
546501090aaSWolfgang Denk 		hist_add_idx = 0;
547501090aaSWolfgang Denk 
548501090aaSWolfgang Denk 	if (hist_add_idx > hist_max)
549501090aaSWolfgang Denk 		hist_max = hist_add_idx;
550501090aaSWolfgang Denk 
551501090aaSWolfgang Denk 	hist_num++;
552501090aaSWolfgang Denk }
553501090aaSWolfgang Denk 
554501090aaSWolfgang Denk static char* hist_prev(void)
555501090aaSWolfgang Denk {
556501090aaSWolfgang Denk 	char *ret;
557501090aaSWolfgang Denk 	int old_cur;
558501090aaSWolfgang Denk 
559501090aaSWolfgang Denk 	if (hist_cur < 0)
560501090aaSWolfgang Denk 		return NULL;
561501090aaSWolfgang Denk 
562501090aaSWolfgang Denk 	old_cur = hist_cur;
563501090aaSWolfgang Denk 	if (--hist_cur < 0)
564501090aaSWolfgang Denk 		hist_cur = hist_max;
565501090aaSWolfgang Denk 
566501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
567501090aaSWolfgang Denk 		hist_cur = old_cur;
568501090aaSWolfgang Denk 		ret = NULL;
569501090aaSWolfgang Denk 	} else
570501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
571501090aaSWolfgang Denk 
572501090aaSWolfgang Denk 	return (ret);
573501090aaSWolfgang Denk }
574501090aaSWolfgang Denk 
575501090aaSWolfgang Denk static char* hist_next(void)
576501090aaSWolfgang Denk {
577501090aaSWolfgang Denk 	char *ret;
578501090aaSWolfgang Denk 
579501090aaSWolfgang Denk 	if (hist_cur < 0)
580501090aaSWolfgang Denk 		return NULL;
581501090aaSWolfgang Denk 
582501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx)
583501090aaSWolfgang Denk 		return NULL;
584501090aaSWolfgang Denk 
585501090aaSWolfgang Denk 	if (++hist_cur > hist_max)
586501090aaSWolfgang Denk 		hist_cur = 0;
587501090aaSWolfgang Denk 
588501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
589501090aaSWolfgang Denk 		ret = "";
590501090aaSWolfgang Denk 	} else
591501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
592501090aaSWolfgang Denk 
593501090aaSWolfgang Denk 	return (ret);
594501090aaSWolfgang Denk }
595501090aaSWolfgang Denk 
5963ca9122fSStefan Roese #ifndef CONFIG_CMDLINE_EDITING
597501090aaSWolfgang Denk static void cread_print_hist_list(void)
598501090aaSWolfgang Denk {
599501090aaSWolfgang Denk 	int i;
600501090aaSWolfgang Denk 	unsigned long n;
601501090aaSWolfgang Denk 
602501090aaSWolfgang Denk 	n = hist_num - hist_max;
603501090aaSWolfgang Denk 
604501090aaSWolfgang Denk 	i = hist_add_idx + 1;
605501090aaSWolfgang Denk 	while (1) {
606501090aaSWolfgang Denk 		if (i > hist_max)
607501090aaSWolfgang Denk 			i = 0;
608501090aaSWolfgang Denk 		if (i == hist_add_idx)
609501090aaSWolfgang Denk 			break;
610501090aaSWolfgang Denk 		printf("%s\n", hist_list[i]);
611501090aaSWolfgang Denk 		n++;
612501090aaSWolfgang Denk 		i++;
613501090aaSWolfgang Denk 	}
614501090aaSWolfgang Denk }
6153ca9122fSStefan Roese #endif /* CONFIG_CMDLINE_EDITING */
616501090aaSWolfgang Denk 
617501090aaSWolfgang Denk #define BEGINNING_OF_LINE() {			\
618501090aaSWolfgang Denk 	while (num) {				\
619501090aaSWolfgang Denk 		getcmd_putch(CTL_BACKSPACE);	\
620501090aaSWolfgang Denk 		num--;				\
621501090aaSWolfgang Denk 	}					\
622501090aaSWolfgang Denk }
623501090aaSWolfgang Denk 
624501090aaSWolfgang Denk #define ERASE_TO_EOL() {				\
625501090aaSWolfgang Denk 	if (num < eol_num) {				\
6268faba489SMike Frysinger 		printf("%*s", (int)(eol_num - num), ""); \
6278faba489SMike Frysinger 		do {					\
628501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);	\
6298faba489SMike Frysinger 		} while (--eol_num > num);		\
630501090aaSWolfgang Denk 	}						\
631501090aaSWolfgang Denk }
632501090aaSWolfgang Denk 
633501090aaSWolfgang Denk #define REFRESH_TO_EOL() {			\
634501090aaSWolfgang Denk 	if (num < eol_num) {			\
635501090aaSWolfgang Denk 		wlen = eol_num - num;		\
636501090aaSWolfgang Denk 		putnstr(buf + num, wlen);	\
637501090aaSWolfgang Denk 		num = eol_num;			\
638501090aaSWolfgang Denk 	}					\
639501090aaSWolfgang Denk }
640501090aaSWolfgang Denk 
641501090aaSWolfgang Denk static void cread_add_char(char ichar, int insert, unsigned long *num,
642501090aaSWolfgang Denk 	       unsigned long *eol_num, char *buf, unsigned long len)
643501090aaSWolfgang Denk {
644501090aaSWolfgang Denk 	unsigned long wlen;
645501090aaSWolfgang Denk 
646501090aaSWolfgang Denk 	/* room ??? */
647501090aaSWolfgang Denk 	if (insert || *num == *eol_num) {
648501090aaSWolfgang Denk 		if (*eol_num > len - 1) {
649501090aaSWolfgang Denk 			getcmd_cbeep();
650501090aaSWolfgang Denk 			return;
651501090aaSWolfgang Denk 		}
652501090aaSWolfgang Denk 		(*eol_num)++;
653501090aaSWolfgang Denk 	}
654501090aaSWolfgang Denk 
655501090aaSWolfgang Denk 	if (insert) {
656501090aaSWolfgang Denk 		wlen = *eol_num - *num;
657501090aaSWolfgang Denk 		if (wlen > 1) {
658501090aaSWolfgang Denk 			memmove(&buf[*num+1], &buf[*num], wlen-1);
659501090aaSWolfgang Denk 		}
660501090aaSWolfgang Denk 
661501090aaSWolfgang Denk 		buf[*num] = ichar;
662501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
663501090aaSWolfgang Denk 		(*num)++;
664501090aaSWolfgang Denk 		while (--wlen) {
665501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);
666501090aaSWolfgang Denk 		}
667501090aaSWolfgang Denk 	} else {
668501090aaSWolfgang Denk 		/* echo the character */
669501090aaSWolfgang Denk 		wlen = 1;
670501090aaSWolfgang Denk 		buf[*num] = ichar;
671501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
672501090aaSWolfgang Denk 		(*num)++;
673501090aaSWolfgang Denk 	}
674501090aaSWolfgang Denk }
675501090aaSWolfgang Denk 
676501090aaSWolfgang Denk static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
677501090aaSWolfgang Denk 	      unsigned long *eol_num, char *buf, unsigned long len)
678501090aaSWolfgang Denk {
679501090aaSWolfgang Denk 	while (strsize--) {
680501090aaSWolfgang Denk 		cread_add_char(*str, insert, num, eol_num, buf, len);
681501090aaSWolfgang Denk 		str++;
682501090aaSWolfgang Denk 	}
683501090aaSWolfgang Denk }
684501090aaSWolfgang Denk 
68523d0baf9SJean-Christophe PLAGNIOL-VILLARD static int cread_line(const char *const prompt, char *buf, unsigned int *len)
686501090aaSWolfgang Denk {
687501090aaSWolfgang Denk 	unsigned long num = 0;
688501090aaSWolfgang Denk 	unsigned long eol_num = 0;
689501090aaSWolfgang Denk 	unsigned long wlen;
690501090aaSWolfgang Denk 	char ichar;
691501090aaSWolfgang Denk 	int insert = 1;
692501090aaSWolfgang Denk 	int esc_len = 0;
693501090aaSWolfgang Denk 	char esc_save[8];
694ecc5500eSPeter Tyser 	int init_len = strlen(buf);
695ecc5500eSPeter Tyser 
696ecc5500eSPeter Tyser 	if (init_len)
697ecc5500eSPeter Tyser 		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
698501090aaSWolfgang Denk 
699501090aaSWolfgang Denk 	while (1) {
70000ac50e3SAndreas Engel #ifdef CONFIG_BOOT_RETRY_TIME
70100ac50e3SAndreas Engel 		while (!tstc()) {	/* while no incoming data */
70200ac50e3SAndreas Engel 			if (retry_time >= 0 && get_ticks() > endtime)
70300ac50e3SAndreas Engel 				return (-2);	/* timed out */
70430dc165aSJens Scharsig 			WATCHDOG_RESET();
70500ac50e3SAndreas Engel 		}
70600ac50e3SAndreas Engel #endif
70700ac50e3SAndreas Engel 
708501090aaSWolfgang Denk 		ichar = getcmd_getch();
709501090aaSWolfgang Denk 
710501090aaSWolfgang Denk 		if ((ichar == '\n') || (ichar == '\r')) {
711dd9f06f0SWolfgang Denk 			putc('\n');
712501090aaSWolfgang Denk 			break;
713501090aaSWolfgang Denk 		}
714501090aaSWolfgang Denk 
715501090aaSWolfgang Denk 		/*
716501090aaSWolfgang Denk 		 * handle standard linux xterm esc sequences for arrow key, etc.
717501090aaSWolfgang Denk 		 */
718501090aaSWolfgang Denk 		if (esc_len != 0) {
719501090aaSWolfgang Denk 			if (esc_len == 1) {
720501090aaSWolfgang Denk 				if (ichar == '[') {
721501090aaSWolfgang Denk 					esc_save[esc_len] = ichar;
722501090aaSWolfgang Denk 					esc_len = 2;
723501090aaSWolfgang Denk 				} else {
724501090aaSWolfgang Denk 					cread_add_str(esc_save, esc_len, insert,
725501090aaSWolfgang Denk 						      &num, &eol_num, buf, *len);
726501090aaSWolfgang Denk 					esc_len = 0;
727501090aaSWolfgang Denk 				}
728501090aaSWolfgang Denk 				continue;
729501090aaSWolfgang Denk 			}
730501090aaSWolfgang Denk 
731501090aaSWolfgang Denk 			switch (ichar) {
732501090aaSWolfgang Denk 
733501090aaSWolfgang Denk 			case 'D':	/* <- key */
734501090aaSWolfgang Denk 				ichar = CTL_CH('b');
735501090aaSWolfgang Denk 				esc_len = 0;
736501090aaSWolfgang Denk 				break;
737501090aaSWolfgang Denk 			case 'C':	/* -> key */
738501090aaSWolfgang Denk 				ichar = CTL_CH('f');
739501090aaSWolfgang Denk 				esc_len = 0;
740501090aaSWolfgang Denk 				break;	/* pass off to ^F handler */
741501090aaSWolfgang Denk 			case 'H':	/* Home key */
742501090aaSWolfgang Denk 				ichar = CTL_CH('a');
743501090aaSWolfgang Denk 				esc_len = 0;
744501090aaSWolfgang Denk 				break;	/* pass off to ^A handler */
745501090aaSWolfgang Denk 			case 'A':	/* up arrow */
746501090aaSWolfgang Denk 				ichar = CTL_CH('p');
747501090aaSWolfgang Denk 				esc_len = 0;
748501090aaSWolfgang Denk 				break;	/* pass off to ^P handler */
749501090aaSWolfgang Denk 			case 'B':	/* down arrow */
750501090aaSWolfgang Denk 				ichar = CTL_CH('n');
751501090aaSWolfgang Denk 				esc_len = 0;
752501090aaSWolfgang Denk 				break;	/* pass off to ^N handler */
753501090aaSWolfgang Denk 			default:
754501090aaSWolfgang Denk 				esc_save[esc_len++] = ichar;
755501090aaSWolfgang Denk 				cread_add_str(esc_save, esc_len, insert,
756501090aaSWolfgang Denk 					      &num, &eol_num, buf, *len);
757501090aaSWolfgang Denk 				esc_len = 0;
758501090aaSWolfgang Denk 				continue;
759501090aaSWolfgang Denk 			}
760501090aaSWolfgang Denk 		}
761501090aaSWolfgang Denk 
762501090aaSWolfgang Denk 		switch (ichar) {
763501090aaSWolfgang Denk 		case 0x1b:
764501090aaSWolfgang Denk 			if (esc_len == 0) {
765501090aaSWolfgang Denk 				esc_save[esc_len] = ichar;
766501090aaSWolfgang Denk 				esc_len = 1;
767501090aaSWolfgang Denk 			} else {
768dd9f06f0SWolfgang Denk 				puts("impossible condition #876\n");
769501090aaSWolfgang Denk 				esc_len = 0;
770501090aaSWolfgang Denk 			}
771501090aaSWolfgang Denk 			break;
772501090aaSWolfgang Denk 
773501090aaSWolfgang Denk 		case CTL_CH('a'):
774501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
775501090aaSWolfgang Denk 			break;
776501090aaSWolfgang Denk 		case CTL_CH('c'):	/* ^C - break */
777501090aaSWolfgang Denk 			*buf = '\0';	/* discard input */
778501090aaSWolfgang Denk 			return (-1);
779501090aaSWolfgang Denk 		case CTL_CH('f'):
780501090aaSWolfgang Denk 			if (num < eol_num) {
781501090aaSWolfgang Denk 				getcmd_putch(buf[num]);
782501090aaSWolfgang Denk 				num++;
783501090aaSWolfgang Denk 			}
784501090aaSWolfgang Denk 			break;
785501090aaSWolfgang Denk 		case CTL_CH('b'):
786501090aaSWolfgang Denk 			if (num) {
787501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
788501090aaSWolfgang Denk 				num--;
789501090aaSWolfgang Denk 			}
790501090aaSWolfgang Denk 			break;
791501090aaSWolfgang Denk 		case CTL_CH('d'):
792501090aaSWolfgang Denk 			if (num < eol_num) {
793501090aaSWolfgang Denk 				wlen = eol_num - num - 1;
794501090aaSWolfgang Denk 				if (wlen) {
795501090aaSWolfgang Denk 					memmove(&buf[num], &buf[num+1], wlen);
796501090aaSWolfgang Denk 					putnstr(buf + num, wlen);
797501090aaSWolfgang Denk 				}
798501090aaSWolfgang Denk 
799501090aaSWolfgang Denk 				getcmd_putch(' ');
800501090aaSWolfgang Denk 				do {
801501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
802501090aaSWolfgang Denk 				} while (wlen--);
803501090aaSWolfgang Denk 				eol_num--;
804501090aaSWolfgang Denk 			}
805501090aaSWolfgang Denk 			break;
806501090aaSWolfgang Denk 		case CTL_CH('k'):
807501090aaSWolfgang Denk 			ERASE_TO_EOL();
808501090aaSWolfgang Denk 			break;
809501090aaSWolfgang Denk 		case CTL_CH('e'):
810501090aaSWolfgang Denk 			REFRESH_TO_EOL();
811501090aaSWolfgang Denk 			break;
812501090aaSWolfgang Denk 		case CTL_CH('o'):
813501090aaSWolfgang Denk 			insert = !insert;
814501090aaSWolfgang Denk 			break;
815501090aaSWolfgang Denk 		case CTL_CH('x'):
81623d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case CTL_CH('u'):
817501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
818501090aaSWolfgang Denk 			ERASE_TO_EOL();
819501090aaSWolfgang Denk 			break;
820501090aaSWolfgang Denk 		case DEL:
821501090aaSWolfgang Denk 		case DEL7:
822501090aaSWolfgang Denk 		case 8:
823501090aaSWolfgang Denk 			if (num) {
824501090aaSWolfgang Denk 				wlen = eol_num - num;
825501090aaSWolfgang Denk 				num--;
826501090aaSWolfgang Denk 				memmove(&buf[num], &buf[num+1], wlen);
827501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
828501090aaSWolfgang Denk 				putnstr(buf + num, wlen);
829501090aaSWolfgang Denk 				getcmd_putch(' ');
830501090aaSWolfgang Denk 				do {
831501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
832501090aaSWolfgang Denk 				} while (wlen--);
833501090aaSWolfgang Denk 				eol_num--;
834501090aaSWolfgang Denk 			}
835501090aaSWolfgang Denk 			break;
836501090aaSWolfgang Denk 		case CTL_CH('p'):
837501090aaSWolfgang Denk 		case CTL_CH('n'):
838501090aaSWolfgang Denk 		{
839501090aaSWolfgang Denk 			char * hline;
840501090aaSWolfgang Denk 
841501090aaSWolfgang Denk 			esc_len = 0;
842501090aaSWolfgang Denk 
843501090aaSWolfgang Denk 			if (ichar == CTL_CH('p'))
844501090aaSWolfgang Denk 				hline = hist_prev();
845501090aaSWolfgang Denk 			else
846501090aaSWolfgang Denk 				hline = hist_next();
847501090aaSWolfgang Denk 
848501090aaSWolfgang Denk 			if (!hline) {
849501090aaSWolfgang Denk 				getcmd_cbeep();
850501090aaSWolfgang Denk 				continue;
851501090aaSWolfgang Denk 			}
852501090aaSWolfgang Denk 
853501090aaSWolfgang Denk 			/* nuke the current line */
854501090aaSWolfgang Denk 			/* first, go home */
855501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
856501090aaSWolfgang Denk 
857501090aaSWolfgang Denk 			/* erase to end of line */
858501090aaSWolfgang Denk 			ERASE_TO_EOL();
859501090aaSWolfgang Denk 
860501090aaSWolfgang Denk 			/* copy new line into place and display */
861501090aaSWolfgang Denk 			strcpy(buf, hline);
862501090aaSWolfgang Denk 			eol_num = strlen(buf);
863501090aaSWolfgang Denk 			REFRESH_TO_EOL();
864501090aaSWolfgang Denk 			continue;
865501090aaSWolfgang Denk 		}
86623d0baf9SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_AUTO_COMPLETE
86723d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case '\t': {
86823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			int num2, col;
86923d0baf9SJean-Christophe PLAGNIOL-VILLARD 
87023d0baf9SJean-Christophe PLAGNIOL-VILLARD 			/* do not autocomplete when in the middle */
87123d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (num < eol_num) {
87223d0baf9SJean-Christophe PLAGNIOL-VILLARD 				getcmd_cbeep();
87323d0baf9SJean-Christophe PLAGNIOL-VILLARD 				break;
87423d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
87523d0baf9SJean-Christophe PLAGNIOL-VILLARD 
87623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			buf[num] = '\0';
87723d0baf9SJean-Christophe PLAGNIOL-VILLARD 			col = strlen(prompt) + eol_num;
87823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			num2 = num;
87923d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
88023d0baf9SJean-Christophe PLAGNIOL-VILLARD 				col = num2 - num;
88123d0baf9SJean-Christophe PLAGNIOL-VILLARD 				num += col;
88223d0baf9SJean-Christophe PLAGNIOL-VILLARD 				eol_num += col;
88323d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
88423d0baf9SJean-Christophe PLAGNIOL-VILLARD 			break;
88523d0baf9SJean-Christophe PLAGNIOL-VILLARD 		}
88623d0baf9SJean-Christophe PLAGNIOL-VILLARD #endif
887501090aaSWolfgang Denk 		default:
888501090aaSWolfgang Denk 			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
889501090aaSWolfgang Denk 			break;
890501090aaSWolfgang Denk 		}
891501090aaSWolfgang Denk 	}
892501090aaSWolfgang Denk 	*len = eol_num;
893501090aaSWolfgang Denk 	buf[eol_num] = '\0';	/* lose the newline */
894501090aaSWolfgang Denk 
895501090aaSWolfgang Denk 	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
896501090aaSWolfgang Denk 		cread_add_to_hist(buf);
897501090aaSWolfgang Denk 	hist_cur = hist_add_idx;
898501090aaSWolfgang Denk 
899f9239438SPeter Tyser 	return 0;
900501090aaSWolfgang Denk }
901501090aaSWolfgang Denk 
902501090aaSWolfgang Denk #endif /* CONFIG_CMDLINE_EDITING */
903501090aaSWolfgang Denk 
904c609719bSwdenk /****************************************************************************/
905c609719bSwdenk 
906c609719bSwdenk /*
907c609719bSwdenk  * Prompt for input and read a line.
908c609719bSwdenk  * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
909c609719bSwdenk  * time out when time goes past endtime (timebase time in ticks).
910c609719bSwdenk  * Return:	number of read characters
911c609719bSwdenk  *		-1 if break
912c609719bSwdenk  *		-2 if timed out
913c609719bSwdenk  */
914c609719bSwdenk int readline (const char *const prompt)
915c609719bSwdenk {
916ecc5500eSPeter Tyser 	/*
917ecc5500eSPeter Tyser 	 * If console_buffer isn't 0-length the user will be prompted to modify
918ecc5500eSPeter Tyser 	 * it instead of entering it from scratch as desired.
919ecc5500eSPeter Tyser 	 */
920ecc5500eSPeter Tyser 	console_buffer[0] = '\0';
921ecc5500eSPeter Tyser 
9226636b62aSJames Yang 	return readline_into_buffer(prompt, console_buffer);
9236636b62aSJames Yang }
9246636b62aSJames Yang 
9256636b62aSJames Yang 
9266636b62aSJames Yang int readline_into_buffer (const char *const prompt, char * buffer)
9276636b62aSJames Yang {
9286636b62aSJames Yang 	char *p = buffer;
929501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
9308804ae3bSPeter Tyser 	unsigned int len = CONFIG_SYS_CBSIZE;
931d8f961bbSStefan Roese 	int rc;
932501090aaSWolfgang Denk 	static int initted = 0;
933501090aaSWolfgang Denk 
934597f6c26SJames Yang 	/*
935597f6c26SJames Yang 	 * History uses a global array which is not
936597f6c26SJames Yang 	 * writable until after relocation to RAM.
937597f6c26SJames Yang 	 * Revert to non-history version if still
938597f6c26SJames Yang 	 * running from flash.
939597f6c26SJames Yang 	 */
940597f6c26SJames Yang 	if (gd->flags & GD_FLG_RELOC) {
941597f6c26SJames Yang 		if (!initted) {
942597f6c26SJames Yang 			hist_init();
943597f6c26SJames Yang 			initted = 1;
944597f6c26SJames Yang 		}
945597f6c26SJames Yang 
946e491a71eSPeter Tyser 		if (prompt)
947dd9f06f0SWolfgang Denk 			puts (prompt);
948501090aaSWolfgang Denk 
94923d0baf9SJean-Christophe PLAGNIOL-VILLARD 		rc = cread_line(prompt, p, &len);
950d8f961bbSStefan Roese 		return rc < 0 ? rc : len;
951597f6c26SJames Yang 
952597f6c26SJames Yang 	} else {
953597f6c26SJames Yang #endif	/* CONFIG_CMDLINE_EDITING */
9540ec59524SKumar Gala 	char * p_buf = p;
955c609719bSwdenk 	int	n = 0;				/* buffer index		*/
956c609719bSwdenk 	int	plen = 0;			/* prompt length	*/
957c609719bSwdenk 	int	col;				/* output column cnt	*/
958c609719bSwdenk 	char	c;
959c609719bSwdenk 
960c609719bSwdenk 	/* print prompt */
961c609719bSwdenk 	if (prompt) {
962c609719bSwdenk 		plen = strlen (prompt);
963c609719bSwdenk 		puts (prompt);
964c609719bSwdenk 	}
965c609719bSwdenk 	col = plen;
966c609719bSwdenk 
967c609719bSwdenk 	for (;;) {
968c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
969c609719bSwdenk 		while (!tstc()) {	/* while no incoming data */
970c609719bSwdenk 			if (retry_time >= 0 && get_ticks() > endtime)
971c609719bSwdenk 				return (-2);	/* timed out */
97230dc165aSJens Scharsig 			WATCHDOG_RESET();
973c609719bSwdenk 		}
974c609719bSwdenk #endif
975c609719bSwdenk 		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
976c609719bSwdenk 
977c609719bSwdenk #ifdef CONFIG_SHOW_ACTIVITY
978c609719bSwdenk 		while (!tstc()) {
979c609719bSwdenk 			extern void show_activity(int arg);
980c609719bSwdenk 			show_activity(0);
98130dc165aSJens Scharsig 			WATCHDOG_RESET();
982c609719bSwdenk 		}
983c609719bSwdenk #endif
984c609719bSwdenk 		c = getc();
985c609719bSwdenk 
986c609719bSwdenk 		/*
987c609719bSwdenk 		 * Special character handling
988c609719bSwdenk 		 */
989c609719bSwdenk 		switch (c) {
990c609719bSwdenk 		case '\r':				/* Enter		*/
991c609719bSwdenk 		case '\n':
992c609719bSwdenk 			*p = '\0';
993c609719bSwdenk 			puts ("\r\n");
9946636b62aSJames Yang 			return (p - p_buf);
995c609719bSwdenk 
99627aa8186Swdenk 		case '\0':				/* nul			*/
99727aa8186Swdenk 			continue;
99827aa8186Swdenk 
999c609719bSwdenk 		case 0x03:				/* ^C - break		*/
10006636b62aSJames Yang 			p_buf[0] = '\0';	/* discard input */
1001c609719bSwdenk 			return (-1);
1002c609719bSwdenk 
1003c609719bSwdenk 		case 0x15:				/* ^U - erase line	*/
1004c609719bSwdenk 			while (col > plen) {
1005c609719bSwdenk 				puts (erase_seq);
1006c609719bSwdenk 				--col;
1007c609719bSwdenk 			}
10086636b62aSJames Yang 			p = p_buf;
1009c609719bSwdenk 			n = 0;
1010c609719bSwdenk 			continue;
1011c609719bSwdenk 
1012c609719bSwdenk 		case 0x17:				/* ^W - erase word	*/
10136636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1014c609719bSwdenk 			while ((n > 0) && (*p != ' ')) {
10156636b62aSJames Yang 				p=delete_char(p_buf, p, &col, &n, plen);
1016c609719bSwdenk 			}
1017c609719bSwdenk 			continue;
1018c609719bSwdenk 
1019c609719bSwdenk 		case 0x08:				/* ^H  - backspace	*/
1020c609719bSwdenk 		case 0x7F:				/* DEL - backspace	*/
10216636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1022c609719bSwdenk 			continue;
1023c609719bSwdenk 
1024c609719bSwdenk 		default:
1025c609719bSwdenk 			/*
1026c609719bSwdenk 			 * Must be a normal character then
1027c609719bSwdenk 			 */
10286d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 			if (n < CONFIG_SYS_CBSIZE-2) {
1029c609719bSwdenk 				if (c == '\t') {	/* expand TABs		*/
103004a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
103104a85b3bSwdenk 					/* if auto completion triggered just continue */
103204a85b3bSwdenk 					*p = '\0';
103304a85b3bSwdenk 					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
10346636b62aSJames Yang 						p = p_buf + n;	/* reset */
103504a85b3bSwdenk 						continue;
103604a85b3bSwdenk 					}
103704a85b3bSwdenk #endif
1038c609719bSwdenk 					puts (tab_seq+(col&07));
1039c609719bSwdenk 					col += 8 - (col&07);
1040c609719bSwdenk 				} else {
1041c609719bSwdenk 					++col;		/* echo input		*/
1042c609719bSwdenk 					putc (c);
1043c609719bSwdenk 				}
1044c609719bSwdenk 				*p++ = c;
1045c609719bSwdenk 				++n;
1046c609719bSwdenk 			} else {			/* Buffer full		*/
1047c609719bSwdenk 				putc ('\a');
1048c609719bSwdenk 			}
1049c609719bSwdenk 		}
1050c609719bSwdenk 	}
1051597f6c26SJames Yang #ifdef CONFIG_CMDLINE_EDITING
1052597f6c26SJames Yang 	}
1053597f6c26SJames Yang #endif
1054c609719bSwdenk }
1055c609719bSwdenk 
1056c609719bSwdenk /****************************************************************************/
1057c609719bSwdenk 
1058c609719bSwdenk static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1059c609719bSwdenk {
1060c609719bSwdenk 	char *s;
1061c609719bSwdenk 
1062c609719bSwdenk 	if (*np == 0) {
1063c609719bSwdenk 		return (p);
1064c609719bSwdenk 	}
1065c609719bSwdenk 
1066c609719bSwdenk 	if (*(--p) == '\t') {			/* will retype the whole line	*/
1067c609719bSwdenk 		while (*colp > plen) {
1068c609719bSwdenk 			puts (erase_seq);
1069c609719bSwdenk 			(*colp)--;
1070c609719bSwdenk 		}
1071c609719bSwdenk 		for (s=buffer; s<p; ++s) {
1072c609719bSwdenk 			if (*s == '\t') {
1073c609719bSwdenk 				puts (tab_seq+((*colp) & 07));
1074c609719bSwdenk 				*colp += 8 - ((*colp) & 07);
1075c609719bSwdenk 			} else {
1076c609719bSwdenk 				++(*colp);
1077c609719bSwdenk 				putc (*s);
1078c609719bSwdenk 			}
1079c609719bSwdenk 		}
1080c609719bSwdenk 	} else {
1081c609719bSwdenk 		puts (erase_seq);
1082c609719bSwdenk 		(*colp)--;
1083c609719bSwdenk 	}
1084c609719bSwdenk 	(*np)--;
1085c609719bSwdenk 	return (p);
1086c609719bSwdenk }
1087c609719bSwdenk 
1088c609719bSwdenk /****************************************************************************/
1089c609719bSwdenk 
1090c609719bSwdenk int parse_line (char *line, char *argv[])
1091c609719bSwdenk {
1092c609719bSwdenk 	int nargs = 0;
1093c609719bSwdenk 
1094c609719bSwdenk #ifdef DEBUG_PARSER
1095c609719bSwdenk 	printf ("parse_line: \"%s\"\n", line);
1096c609719bSwdenk #endif
10976d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	while (nargs < CONFIG_SYS_MAXARGS) {
1098c609719bSwdenk 
1099c609719bSwdenk 		/* skip any white space */
1100c609719bSwdenk 		while ((*line == ' ') || (*line == '\t')) {
1101c609719bSwdenk 			++line;
1102c609719bSwdenk 		}
1103c609719bSwdenk 
1104c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1105c609719bSwdenk 			argv[nargs] = NULL;
1106c609719bSwdenk #ifdef DEBUG_PARSER
1107c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1108c609719bSwdenk #endif
1109c609719bSwdenk 			return (nargs);
1110c609719bSwdenk 		}
1111c609719bSwdenk 
1112c609719bSwdenk 		argv[nargs++] = line;	/* begin of argument string	*/
1113c609719bSwdenk 
1114c609719bSwdenk 		/* find end of string */
1115c609719bSwdenk 		while (*line && (*line != ' ') && (*line != '\t')) {
1116c609719bSwdenk 			++line;
1117c609719bSwdenk 		}
1118c609719bSwdenk 
1119c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1120c609719bSwdenk 			argv[nargs] = NULL;
1121c609719bSwdenk #ifdef DEBUG_PARSER
1122c609719bSwdenk 		printf ("parse_line: nargs=%d\n", nargs);
1123c609719bSwdenk #endif
1124c609719bSwdenk 			return (nargs);
1125c609719bSwdenk 		}
1126c609719bSwdenk 
1127c609719bSwdenk 		*line++ = '\0';		/* terminate current arg	 */
1128c609719bSwdenk 	}
1129c609719bSwdenk 
11306d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1131c609719bSwdenk 
1132c609719bSwdenk #ifdef DEBUG_PARSER
1133c609719bSwdenk 	printf ("parse_line: nargs=%d\n", nargs);
1134c609719bSwdenk #endif
1135c609719bSwdenk 	return (nargs);
1136c609719bSwdenk }
1137c609719bSwdenk 
1138c609719bSwdenk /****************************************************************************/
1139c609719bSwdenk 
1140c609719bSwdenk static void process_macros (const char *input, char *output)
1141c609719bSwdenk {
1142c609719bSwdenk 	char c, prev;
1143c609719bSwdenk 	const char *varname_start = NULL;
1144c609719bSwdenk 	int inputcnt = strlen (input);
11456d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	int outputcnt = CONFIG_SYS_CBSIZE;
1146c609719bSwdenk 	int state = 0;		/* 0 = waiting for '$'  */
114719973b6aSWolfgang Denk 
11485cf91d6bSwdenk 	/* 1 = waiting for '(' or '{' */
11495cf91d6bSwdenk 	/* 2 = waiting for ')' or '}' */
1150a25f862bSwdenk 	/* 3 = waiting for '''  */
1151c609719bSwdenk #ifdef DEBUG_PARSER
1152c609719bSwdenk 	char *output_start = output;
1153c609719bSwdenk 
115419973b6aSWolfgang Denk 	printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
115519973b6aSWolfgang Denk 		input);
1156c609719bSwdenk #endif
1157c609719bSwdenk 
1158c609719bSwdenk 	prev = '\0';		/* previous character   */
1159c609719bSwdenk 
1160c609719bSwdenk 	while (inputcnt && outputcnt) {
1161c609719bSwdenk 		c = *input++;
1162c609719bSwdenk 		inputcnt--;
1163c609719bSwdenk 
1164a25f862bSwdenk 		if (state != 3) {
1165c609719bSwdenk 			/* remove one level of escape characters */
1166c609719bSwdenk 			if ((c == '\\') && (prev != '\\')) {
1167c609719bSwdenk 				if (inputcnt-- == 0)
1168c609719bSwdenk 					break;
1169c609719bSwdenk 				prev = c;
1170c609719bSwdenk 				c = *input++;
1171c609719bSwdenk 			}
1172a25f862bSwdenk 		}
1173c609719bSwdenk 
1174c609719bSwdenk 		switch (state) {
1175c609719bSwdenk 		case 0:	/* Waiting for (unescaped) $    */
1176a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1177a25f862bSwdenk 				state = 3;
1178a25f862bSwdenk 				break;
1179a25f862bSwdenk 			}
1180c609719bSwdenk 			if ((c == '$') && (prev != '\\')) {
1181c609719bSwdenk 				state++;
1182c609719bSwdenk 			} else {
1183c609719bSwdenk 				*(output++) = c;
1184c609719bSwdenk 				outputcnt--;
1185c609719bSwdenk 			}
1186c609719bSwdenk 			break;
1187c609719bSwdenk 		case 1:	/* Waiting for (        */
11885cf91d6bSwdenk 			if (c == '(' || c == '{') {
1189c609719bSwdenk 				state++;
1190c609719bSwdenk 				varname_start = input;
1191c609719bSwdenk 			} else {
1192c609719bSwdenk 				state = 0;
1193c609719bSwdenk 				*(output++) = '$';
1194c609719bSwdenk 				outputcnt--;
1195c609719bSwdenk 
1196c609719bSwdenk 				if (outputcnt) {
1197c609719bSwdenk 					*(output++) = c;
1198c609719bSwdenk 					outputcnt--;
1199c609719bSwdenk 				}
1200c609719bSwdenk 			}
1201c609719bSwdenk 			break;
1202c609719bSwdenk 		case 2:	/* Waiting for )        */
12035cf91d6bSwdenk 			if (c == ')' || c == '}') {
1204c609719bSwdenk 				int i;
12056d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 				char envname[CONFIG_SYS_CBSIZE], *envval;
1206c609719bSwdenk 				int envcnt = input - varname_start - 1;	/* Varname # of chars */
1207c609719bSwdenk 
1208c609719bSwdenk 				/* Get the varname */
1209c609719bSwdenk 				for (i = 0; i < envcnt; i++) {
1210c609719bSwdenk 					envname[i] = varname_start[i];
1211c609719bSwdenk 				}
1212c609719bSwdenk 				envname[i] = 0;
1213c609719bSwdenk 
1214c609719bSwdenk 				/* Get its value */
1215c609719bSwdenk 				envval = getenv (envname);
1216c609719bSwdenk 
1217c609719bSwdenk 				/* Copy into the line if it exists */
1218c609719bSwdenk 				if (envval != NULL)
1219c609719bSwdenk 					while ((*envval) && outputcnt) {
1220c609719bSwdenk 						*(output++) = *(envval++);
1221c609719bSwdenk 						outputcnt--;
1222c609719bSwdenk 					}
1223c609719bSwdenk 				/* Look for another '$' */
1224c609719bSwdenk 				state = 0;
1225c609719bSwdenk 			}
1226c609719bSwdenk 			break;
1227a25f862bSwdenk 		case 3:	/* Waiting for '        */
1228a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1229a25f862bSwdenk 				state = 0;
1230a25f862bSwdenk 			} else {
1231a25f862bSwdenk 				*(output++) = c;
1232a25f862bSwdenk 				outputcnt--;
1233c609719bSwdenk 			}
1234a25f862bSwdenk 			break;
1235a25f862bSwdenk 		}
1236c609719bSwdenk 		prev = c;
1237c609719bSwdenk 	}
1238c609719bSwdenk 
1239c609719bSwdenk 	if (outputcnt)
1240c609719bSwdenk 		*output = 0;
12419160b96fSBartlomiej Sieka 	else
12429160b96fSBartlomiej Sieka 		*(output - 1) = 0;
1243c609719bSwdenk 
1244c609719bSwdenk #ifdef DEBUG_PARSER
1245c609719bSwdenk 	printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1246c609719bSwdenk 		strlen (output_start), output_start);
1247c609719bSwdenk #endif
1248c609719bSwdenk }
1249c609719bSwdenk 
1250c609719bSwdenk /****************************************************************************
1251c609719bSwdenk  * returns:
1252c609719bSwdenk  *	1  - command executed, repeatable
1253c609719bSwdenk  *	0  - command executed but not repeatable, interrupted commands are
1254c609719bSwdenk  *	     always considered not repeatable
1255c609719bSwdenk  *	-1 - not executed (unrecognized, bootd recursion or too many args)
12566d0f6bcfSJean-Christophe PLAGNIOL-VILLARD  *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1257c609719bSwdenk  *           considered unrecognized)
1258c609719bSwdenk  *
1259c609719bSwdenk  * WARNING:
1260c609719bSwdenk  *
1261c609719bSwdenk  * We must create a temporary copy of the command since the command we get
1262c609719bSwdenk  * may be the result from getenv(), which returns a pointer directly to
1263c609719bSwdenk  * the environment data, which may change magicly when the command we run
1264c609719bSwdenk  * creates or modifies environment variables (like "bootp" does).
1265c609719bSwdenk  */
1266c609719bSwdenk 
1267c609719bSwdenk int run_command (const char *cmd, int flag)
1268c609719bSwdenk {
1269c609719bSwdenk 	cmd_tbl_t *cmdtp;
12706d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
1271c609719bSwdenk 	char *token;			/* start of token in cmdbuf	*/
1272c609719bSwdenk 	char *sep;			/* end of token (separator) in cmdbuf */
12736d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char finaltoken[CONFIG_SYS_CBSIZE];
1274c609719bSwdenk 	char *str = cmdbuf;
12756d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
1276f07771ccSwdenk 	int argc, inquotes;
1277c609719bSwdenk 	int repeatable = 1;
1278f07771ccSwdenk 	int rc = 0;
1279c609719bSwdenk 
1280c609719bSwdenk #ifdef DEBUG_PARSER
1281c609719bSwdenk 	printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1282c609719bSwdenk 	puts (cmd ? cmd : "NULL");	/* use puts - string may be loooong */
1283c609719bSwdenk 	puts ("\"\n");
1284c609719bSwdenk #endif
1285c609719bSwdenk 
1286c609719bSwdenk 	clear_ctrlc();		/* forget any previous Control C */
1287c609719bSwdenk 
1288c609719bSwdenk 	if (!cmd || !*cmd) {
1289c609719bSwdenk 		return -1;	/* empty command */
1290c609719bSwdenk 	}
1291c609719bSwdenk 
12926d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1293c609719bSwdenk 		puts ("## Command too long!\n");
1294c609719bSwdenk 		return -1;
1295c609719bSwdenk 	}
1296c609719bSwdenk 
1297c609719bSwdenk 	strcpy (cmdbuf, cmd);
1298c609719bSwdenk 
1299c609719bSwdenk 	/* Process separators and check for invalid
1300c609719bSwdenk 	 * repeatable commands
1301c609719bSwdenk 	 */
1302c609719bSwdenk 
1303c609719bSwdenk #ifdef DEBUG_PARSER
1304c609719bSwdenk 	printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1305c609719bSwdenk #endif
1306c609719bSwdenk 	while (*str) {
1307c609719bSwdenk 
1308c609719bSwdenk 		/*
1309c609719bSwdenk 		 * Find separator, or string end
1310c609719bSwdenk 		 * Allow simple escape of ';' by writing "\;"
1311c609719bSwdenk 		 */
1312a25f862bSwdenk 		for (inquotes = 0, sep = str; *sep; sep++) {
1313a25f862bSwdenk 			if ((*sep=='\'') &&
1314a25f862bSwdenk 			    (*(sep-1) != '\\'))
1315a25f862bSwdenk 				inquotes=!inquotes;
1316a25f862bSwdenk 
1317a25f862bSwdenk 			if (!inquotes &&
1318a25f862bSwdenk 			    (*sep == ';') &&	/* separator		*/
1319c609719bSwdenk 			    ( sep != str) &&	/* past string start	*/
1320c609719bSwdenk 			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
1321c609719bSwdenk 				break;
1322c609719bSwdenk 		}
1323c609719bSwdenk 
1324c609719bSwdenk 		/*
1325c609719bSwdenk 		 * Limit the token to data between separators
1326c609719bSwdenk 		 */
1327c609719bSwdenk 		token = str;
1328c609719bSwdenk 		if (*sep) {
1329c609719bSwdenk 			str = sep + 1;	/* start of command for next pass */
1330c609719bSwdenk 			*sep = '\0';
1331c609719bSwdenk 		}
1332c609719bSwdenk 		else
1333c609719bSwdenk 			str = sep;	/* no more commands for next pass */
1334c609719bSwdenk #ifdef DEBUG_PARSER
1335c609719bSwdenk 		printf ("token: \"%s\"\n", token);
1336c609719bSwdenk #endif
1337c609719bSwdenk 
1338c609719bSwdenk 		/* find macros in this token and replace them */
1339c609719bSwdenk 		process_macros (token, finaltoken);
1340c609719bSwdenk 
1341c609719bSwdenk 		/* Extract arguments */
13421264b405SWolfgang Denk 		if ((argc = parse_line (finaltoken, argv)) == 0) {
13431264b405SWolfgang Denk 			rc = -1;	/* no command at all */
13441264b405SWolfgang Denk 			continue;
13451264b405SWolfgang Denk 		}
1346c609719bSwdenk 
1347c609719bSwdenk 		/* Look up command in command table */
1348c609719bSwdenk 		if ((cmdtp = find_cmd(argv[0])) == NULL) {
1349c609719bSwdenk 			printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1350f07771ccSwdenk 			rc = -1;	/* give up after bad command */
1351f07771ccSwdenk 			continue;
1352c609719bSwdenk 		}
1353c609719bSwdenk 
1354c609719bSwdenk 		/* found - check max args */
1355c609719bSwdenk 		if (argc > cmdtp->maxargs) {
135662c3ae7cSPeter Tyser 			cmd_usage(cmdtp);
1357f07771ccSwdenk 			rc = -1;
1358f07771ccSwdenk 			continue;
1359c609719bSwdenk 		}
1360c609719bSwdenk 
1361c3517f91SJon Loeliger #if defined(CONFIG_CMD_BOOTD)
1362c609719bSwdenk 		/* avoid "bootd" recursion */
1363c609719bSwdenk 		if (cmdtp->cmd == do_bootd) {
1364c609719bSwdenk #ifdef DEBUG_PARSER
1365c609719bSwdenk 			printf ("[%s]\n", finaltoken);
1366c609719bSwdenk #endif
1367c609719bSwdenk 			if (flag & CMD_FLAG_BOOTD) {
13684b9206edSwdenk 				puts ("'bootd' recursion detected\n");
1369f07771ccSwdenk 				rc = -1;
1370f07771ccSwdenk 				continue;
13711264b405SWolfgang Denk 			} else {
1372c609719bSwdenk 				flag |= CMD_FLAG_BOOTD;
1373c609719bSwdenk 			}
13741264b405SWolfgang Denk 		}
137590253178SJon Loeliger #endif
1376c609719bSwdenk 
1377c609719bSwdenk 		/* OK - call function to do the command */
1378c609719bSwdenk 		if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1379f07771ccSwdenk 			rc = -1;
1380c609719bSwdenk 		}
1381c609719bSwdenk 
1382c609719bSwdenk 		repeatable &= cmdtp->repeatable;
1383c609719bSwdenk 
1384c609719bSwdenk 		/* Did the user stop this? */
1385c609719bSwdenk 		if (had_ctrlc ())
13865afb2020SDetlev Zundel 			return -1;	/* if stopped then not repeatable */
1387c609719bSwdenk 	}
1388c609719bSwdenk 
1389f07771ccSwdenk 	return rc ? rc : repeatable;
1390c609719bSwdenk }
1391c609719bSwdenk 
1392c609719bSwdenk /****************************************************************************/
1393c609719bSwdenk 
1394c3517f91SJon Loeliger #if defined(CONFIG_CMD_RUN)
139554841ab5SWolfgang Denk int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1396c609719bSwdenk {
1397c609719bSwdenk 	int i;
1398c609719bSwdenk 
139947e26b1bSWolfgang Denk 	if (argc < 2)
140047e26b1bSWolfgang Denk 		return cmd_usage(cmdtp);
1401c609719bSwdenk 
1402c609719bSwdenk 	for (i=1; i<argc; ++i) {
14033e38691eSwdenk 		char *arg;
14043e38691eSwdenk 
14053e38691eSwdenk 		if ((arg = getenv (argv[i])) == NULL) {
14063e38691eSwdenk 			printf ("## Error: \"%s\" not defined\n", argv[i]);
14073e38691eSwdenk 			return 1;
14083e38691eSwdenk 		}
1409*c8a2079eSJason Hobbs 
1410*c8a2079eSJason Hobbs 		if (run_command2(arg, flag) != 0)
14113e38691eSwdenk 			return 1;
1412c609719bSwdenk 	}
14133e38691eSwdenk 	return 0;
1414c609719bSwdenk }
141590253178SJon Loeliger #endif
1416