xref: /openbmc/u-boot/common/main.c (revision eca86fad3d823c3c1e7e78b07752aa6a10e35283)
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  *
91a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
10c609719bSwdenk  */
11c609719bSwdenk 
12a6c7ad2fSwdenk /* #define	DEBUG	*/
13a6c7ad2fSwdenk 
14c609719bSwdenk #include <common.h>
15c609719bSwdenk #include <command.h>
16224b72e6SChe-Liang Chiou #include <fdtdec.h>
17*eca86fadSSimon Glass #include <cli_hush.h>
18fbcdf32aSSimon Glass #include <malloc.h>
19317d6c57SHeiko Schocher #include <menu.h>
20fbcdf32aSSimon Glass #include <post.h>
21fbcdf32aSSimon Glass #include <version.h>
22fbcdf32aSSimon Glass #include <watchdog.h>
23fbcdf32aSSimon Glass #include <linux/ctype.h>
24bdccc4feSwdenk 
25d87080b7SWolfgang Denk DECLARE_GLOBAL_DATA_PTR;
26d87080b7SWolfgang Denk 
27fad63407SHeiko Schocher /*
28fad63407SHeiko Schocher  * Board-specific Platform code can reimplement show_boot_progress () if needed
29fad63407SHeiko Schocher  */
30fad63407SHeiko Schocher void inline __show_boot_progress (int val) {}
315e2c08c3SEmil Medve void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
32fad63407SHeiko Schocher 
33c609719bSwdenk #define MAX_DELAY_STOP_STR 32
34c609719bSwdenk 
353e408873SSimon Glass #define DEBUG_PARSER	0	/* set to 1 to debug */
363e408873SSimon Glass 
373e408873SSimon Glass #define debug_parser(fmt, args...)		\
383e408873SSimon Glass 	debug_cond(DEBUG_PARSER, fmt, ##args)
393e408873SSimon Glass 
40d34d186eSSimon Glass #ifndef DEBUG_BOOTKEYS
41d34d186eSSimon Glass #define DEBUG_BOOTKEYS 0
42d34d186eSSimon Glass #endif
43d34d186eSSimon Glass #define debug_bootkeys(fmt, args...)		\
44d34d186eSSimon Glass 	debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
45c609719bSwdenk 
466475b9f9SJohn Schmoller char        console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
47c609719bSwdenk 
483ca9122fSStefan Roese static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
4982359aecSMike Frysinger static const char erase_seq[] = "\b \b";		/* erase sequence	*/
5082359aecSMike Frysinger static const char   tab_seq[] = "        ";		/* used to expand TABs	*/
51c609719bSwdenk 
52c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
53c609719bSwdenk static uint64_t endtime = 0;  /* must be set, default is instant timeout */
54c609719bSwdenk static int      retry_time = -1; /* -1 so can call readline before main_loop */
55c609719bSwdenk #endif
56c609719bSwdenk 
57c609719bSwdenk #define	endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
58c609719bSwdenk 
59c609719bSwdenk #ifndef CONFIG_BOOT_RETRY_MIN
60c609719bSwdenk #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
61c609719bSwdenk #endif
62c609719bSwdenk 
63c609719bSwdenk #ifdef CONFIG_MODEM_SUPPORT
64c609719bSwdenk int do_mdm_init = 0;
65c609719bSwdenk extern void mdm_init(void); /* defined in board.c */
66c609719bSwdenk #endif
67c609719bSwdenk 
68c609719bSwdenk /***************************************************************************
69c609719bSwdenk  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
70c8a2079eSJason Hobbs  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
71c609719bSwdenk  */
72d514544fSJoe Hershberger #if defined(CONFIG_BOOTDELAY)
73c609719bSwdenk # if defined(CONFIG_AUTOBOOT_KEYED)
74063ae006SSimon Glass static int abortboot_keyed(int bootdelay)
75c609719bSwdenk {
76c609719bSwdenk 	int abort = 0;
77c609719bSwdenk 	uint64_t etime = endtick(bootdelay);
7819973b6aSWolfgang Denk 	struct {
79c609719bSwdenk 		char* str;
80c609719bSwdenk 		u_int len;
81c609719bSwdenk 		int retry;
82c609719bSwdenk 	}
8319973b6aSWolfgang Denk 	delaykey [] = {
84c609719bSwdenk 		{ str: getenv ("bootdelaykey"),  retry: 1 },
85c609719bSwdenk 		{ str: getenv ("bootdelaykey2"), retry: 1 },
86c609719bSwdenk 		{ str: getenv ("bootstopkey"),   retry: 0 },
87c609719bSwdenk 		{ str: getenv ("bootstopkey2"),  retry: 0 },
88c609719bSwdenk 	};
89c609719bSwdenk 
90c609719bSwdenk 	char presskey [MAX_DELAY_STOP_STR];
91c609719bSwdenk 	u_int presskey_len = 0;
92c609719bSwdenk 	u_int presskey_max = 0;
93c609719bSwdenk 	u_int i;
94c609719bSwdenk 
95a5aae0a1SDirk Eibach #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
96a5aae0a1SDirk Eibach 	if (bootdelay == 0)
97a5aae0a1SDirk Eibach 		return 0;
98a5aae0a1SDirk Eibach #endif
99a5aae0a1SDirk Eibach 
100c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_PROMPT
101f2302d44SStefan Roese 	printf(CONFIG_AUTOBOOT_PROMPT);
102c609719bSwdenk #  endif
103c609719bSwdenk 
104c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR
105c609719bSwdenk 	if (delaykey[0].str == NULL)
106c609719bSwdenk 		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
107c609719bSwdenk #  endif
108c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_DELAY_STR2
109c609719bSwdenk 	if (delaykey[1].str == NULL)
110c609719bSwdenk 		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
111c609719bSwdenk #  endif
112c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR
113c609719bSwdenk 	if (delaykey[2].str == NULL)
114c609719bSwdenk 		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
115c609719bSwdenk #  endif
116c609719bSwdenk #  ifdef CONFIG_AUTOBOOT_STOP_STR2
117c609719bSwdenk 	if (delaykey[3].str == NULL)
118c609719bSwdenk 		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
119c609719bSwdenk #  endif
120c609719bSwdenk 
121c609719bSwdenk 	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
122c609719bSwdenk 		delaykey[i].len = delaykey[i].str == NULL ?
123c609719bSwdenk 				    0 : strlen (delaykey[i].str);
124c609719bSwdenk 		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
125c609719bSwdenk 				    MAX_DELAY_STOP_STR : delaykey[i].len;
126c609719bSwdenk 
127c609719bSwdenk 		presskey_max = presskey_max > delaykey[i].len ?
128c609719bSwdenk 				    presskey_max : delaykey[i].len;
129c609719bSwdenk 
130d34d186eSSimon Glass 		debug_bootkeys("%s key:<%s>\n",
131c609719bSwdenk 			       delaykey[i].retry ? "delay" : "stop",
132c609719bSwdenk 			       delaykey[i].str ? delaykey[i].str : "NULL");
133c609719bSwdenk 	}
134c609719bSwdenk 
135c609719bSwdenk 	/* In order to keep up with incoming data, check timeout only
136c609719bSwdenk 	 * when catch up.
137c609719bSwdenk 	 */
138c3284b03SPeter Korsgaard 	do {
139c3284b03SPeter Korsgaard 		if (tstc()) {
140c3284b03SPeter Korsgaard 			if (presskey_len < presskey_max) {
141c3284b03SPeter Korsgaard 				presskey [presskey_len ++] = getc();
142c3284b03SPeter Korsgaard 			}
143c3284b03SPeter Korsgaard 			else {
144c3284b03SPeter Korsgaard 				for (i = 0; i < presskey_max - 1; i ++)
145c3284b03SPeter Korsgaard 					presskey [i] = presskey [i + 1];
146c3284b03SPeter Korsgaard 
147c3284b03SPeter Korsgaard 				presskey [i] = getc();
148c3284b03SPeter Korsgaard 			}
149c3284b03SPeter Korsgaard 		}
150c3284b03SPeter Korsgaard 
151c609719bSwdenk 		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
152c609719bSwdenk 			if (delaykey[i].len > 0 &&
153c609719bSwdenk 			    presskey_len >= delaykey[i].len &&
154c609719bSwdenk 			    memcmp (presskey + presskey_len - delaykey[i].len,
155c609719bSwdenk 				    delaykey[i].str,
156c609719bSwdenk 				    delaykey[i].len) == 0) {
157d34d186eSSimon Glass 				debug_bootkeys("got %skey\n",
158d34d186eSSimon Glass 					       delaykey[i].retry ? "delay" :
159d34d186eSSimon Glass 					       "stop");
160c609719bSwdenk 
161c609719bSwdenk #  ifdef CONFIG_BOOT_RETRY_TIME
162c609719bSwdenk 				/* don't retry auto boot */
163c609719bSwdenk 				if (! delaykey[i].retry)
164c609719bSwdenk 					retry_time = -1;
165c609719bSwdenk #  endif
166c609719bSwdenk 				abort = 1;
167c609719bSwdenk 			}
168c609719bSwdenk 		}
169c3284b03SPeter Korsgaard 	} while (!abort && get_ticks() <= etime);
170c609719bSwdenk 
171c609719bSwdenk 	if (!abort)
172d34d186eSSimon Glass 		debug_bootkeys("key timeout\n");
173c609719bSwdenk 
1748cb8143eSdzu #ifdef CONFIG_SILENT_CONSOLE
1754ec5bd55SLadislav Michl 	if (abort)
1764ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
1778cb8143eSdzu #endif
1788cb8143eSdzu 
179c609719bSwdenk 	return abort;
180c609719bSwdenk }
181c609719bSwdenk 
182c609719bSwdenk # else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
183c609719bSwdenk 
184c7de829cSwdenk #ifdef CONFIG_MENUKEY
185c7de829cSwdenk static int menukey = 0;
186c7de829cSwdenk #endif
187c7de829cSwdenk 
188063ae006SSimon Glass static int abortboot_normal(int bootdelay)
189c609719bSwdenk {
190c609719bSwdenk 	int abort = 0;
191b2f3e0eaSJim Lin 	unsigned long ts;
192c609719bSwdenk 
193c7de829cSwdenk #ifdef CONFIG_MENUPROMPT
194f2302d44SStefan Roese 	printf(CONFIG_MENUPROMPT);
195c7de829cSwdenk #else
19693d7212fSJoe Hershberger 	if (bootdelay >= 0)
197c609719bSwdenk 		printf("Hit any key to stop autoboot: %2d ", bootdelay);
198c7de829cSwdenk #endif
199c609719bSwdenk 
200c609719bSwdenk #if defined CONFIG_ZERO_BOOTDELAY_CHECK
201c609719bSwdenk 	/*
202c609719bSwdenk 	 * Check if key already pressed
203c609719bSwdenk 	 * Don't check if bootdelay < 0
204c609719bSwdenk 	 */
205c609719bSwdenk 	if (bootdelay >= 0) {
206c609719bSwdenk 		if (tstc()) {	/* we got a key press	*/
207c609719bSwdenk 			(void) getc();  /* consume input	*/
2084b9206edSwdenk 			puts ("\b\b\b 0");
209f72da340Swdenk 			abort = 1;	/* don't auto boot	*/
210c609719bSwdenk 		}
211c609719bSwdenk 	}
212c609719bSwdenk #endif
213c609719bSwdenk 
214f72da340Swdenk 	while ((bootdelay > 0) && (!abort)) {
215c609719bSwdenk 		--bootdelay;
216b2f3e0eaSJim Lin 		/* delay 1000 ms */
217b2f3e0eaSJim Lin 		ts = get_timer(0);
218b2f3e0eaSJim Lin 		do {
219c609719bSwdenk 			if (tstc()) {	/* we got a key press	*/
220c609719bSwdenk 				abort  = 1;	/* don't auto boot	*/
221c609719bSwdenk 				bootdelay = 0;	/* no more delay	*/
222c7de829cSwdenk # ifdef CONFIG_MENUKEY
223c7de829cSwdenk 				menukey = getc();
224c7de829cSwdenk # else
225c609719bSwdenk 				(void) getc();  /* consume input	*/
226c7de829cSwdenk # endif
227c609719bSwdenk 				break;
228c609719bSwdenk 			}
229c609719bSwdenk 			udelay(10000);
230b2f3e0eaSJim Lin 		} while (!abort && get_timer(ts) < 1000);
231c609719bSwdenk 
232c609719bSwdenk 		printf("\b\b\b%2d ", bootdelay);
233c609719bSwdenk 	}
234c609719bSwdenk 
235c609719bSwdenk 	putc('\n');
236c609719bSwdenk 
237f72da340Swdenk #ifdef CONFIG_SILENT_CONSOLE
2384ec5bd55SLadislav Michl 	if (abort)
2394ec5bd55SLadislav Michl 		gd->flags &= ~GD_FLG_SILENT;
240f72da340Swdenk #endif
241f72da340Swdenk 
242c609719bSwdenk 	return abort;
243c609719bSwdenk }
244c609719bSwdenk # endif	/* CONFIG_AUTOBOOT_KEYED */
245063ae006SSimon Glass 
246063ae006SSimon Glass static int abortboot(int bootdelay)
247063ae006SSimon Glass {
248063ae006SSimon Glass #ifdef CONFIG_AUTOBOOT_KEYED
249063ae006SSimon Glass 	return abortboot_keyed(bootdelay);
250063ae006SSimon Glass #else
251063ae006SSimon Glass 	return abortboot_normal(bootdelay);
252063ae006SSimon Glass #endif
253063ae006SSimon Glass }
254d514544fSJoe Hershberger #endif	/* CONFIG_BOOTDELAY */
255c609719bSwdenk 
25667e1ea26SDoug Anderson /*
25767e1ea26SDoug Anderson  * Runs the given boot command securely.  Specifically:
25867e1ea26SDoug Anderson  * - Doesn't run the command with the shell (run_command or parse_string_outer),
25967e1ea26SDoug Anderson  *   since that's a lot of code surface that an attacker might exploit.
26067e1ea26SDoug Anderson  *   Because of this, we don't do any argument parsing--the secure boot command
26167e1ea26SDoug Anderson  *   has to be a full-fledged u-boot command.
26267e1ea26SDoug Anderson  * - Doesn't check for keypresses before booting, since that could be a
26367e1ea26SDoug Anderson  *   security hole; also disables Ctrl-C.
26467e1ea26SDoug Anderson  * - Doesn't allow the command to return.
26567e1ea26SDoug Anderson  *
26667e1ea26SDoug Anderson  * Upon any failures, this function will drop into an infinite loop after
26767e1ea26SDoug Anderson  * printing the error message to console.
26867e1ea26SDoug Anderson  */
26967e1ea26SDoug Anderson 
270d514544fSJoe Hershberger #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
27167e1ea26SDoug Anderson static void secure_boot_cmd(char *cmd)
27267e1ea26SDoug Anderson {
27367e1ea26SDoug Anderson 	cmd_tbl_t *cmdtp;
27467e1ea26SDoug Anderson 	int rc;
27567e1ea26SDoug Anderson 
27667e1ea26SDoug Anderson 	if (!cmd) {
27767e1ea26SDoug Anderson 		printf("## Error: Secure boot command not specified\n");
27867e1ea26SDoug Anderson 		goto err;
27967e1ea26SDoug Anderson 	}
28067e1ea26SDoug Anderson 
28167e1ea26SDoug Anderson 	/* Disable Ctrl-C just in case some command is used that checks it. */
28267e1ea26SDoug Anderson 	disable_ctrlc(1);
28367e1ea26SDoug Anderson 
28467e1ea26SDoug Anderson 	/* Find the command directly. */
28567e1ea26SDoug Anderson 	cmdtp = find_cmd(cmd);
28667e1ea26SDoug Anderson 	if (!cmdtp) {
28767e1ea26SDoug Anderson 		printf("## Error: \"%s\" not defined\n", cmd);
28867e1ea26SDoug Anderson 		goto err;
28967e1ea26SDoug Anderson 	}
29067e1ea26SDoug Anderson 
29167e1ea26SDoug Anderson 	/* Run the command, forcing no flags and faking argc and argv. */
29267e1ea26SDoug Anderson 	rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
29367e1ea26SDoug Anderson 
29467e1ea26SDoug Anderson 	/* Shouldn't ever return from boot command. */
29567e1ea26SDoug Anderson 	printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
29667e1ea26SDoug Anderson 
29767e1ea26SDoug Anderson err:
29867e1ea26SDoug Anderson 	/*
29967e1ea26SDoug Anderson 	 * Not a whole lot to do here.  Rebooting won't help much, since we'll
30067e1ea26SDoug Anderson 	 * just end up right back here.  Just loop.
30167e1ea26SDoug Anderson 	 */
30267e1ea26SDoug Anderson 	hang();
30367e1ea26SDoug Anderson }
30467e1ea26SDoug Anderson 
305fcabc24fSSimon Glass static void process_fdt_options(const void *blob)
306fcabc24fSSimon Glass {
307fcabc24fSSimon Glass 	ulong addr;
308fcabc24fSSimon Glass 
309fcabc24fSSimon Glass 	/* Add an env variable to point to a kernel payload, if available */
310fcabc24fSSimon Glass 	addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
311fcabc24fSSimon Glass 	if (addr)
312fcabc24fSSimon Glass 		setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
313fcabc24fSSimon Glass 
314fcabc24fSSimon Glass 	/* Add an env variable to point to a root disk, if available */
315fcabc24fSSimon Glass 	addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
316fcabc24fSSimon Glass 	if (addr)
317fcabc24fSSimon Glass 		setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
318fcabc24fSSimon Glass }
31967e1ea26SDoug Anderson #endif /* CONFIG_OF_CONTROL */
32067e1ea26SDoug Anderson 
321bc2b4c27SSimon Glass #ifdef CONFIG_BOOTDELAY
322bc2b4c27SSimon Glass static void process_boot_delay(void)
323c609719bSwdenk {
324bc2b4c27SSimon Glass #ifdef CONFIG_OF_CONTROL
325224b72e6SChe-Liang Chiou 	char *env;
326224b72e6SChe-Liang Chiou #endif
327c609719bSwdenk 	char *s;
328c609719bSwdenk 	int bootdelay;
329bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
330bdccc4feSwdenk 	unsigned long bootcount = 0;
331bdccc4feSwdenk 	unsigned long bootlimit = 0;
332bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
333c609719bSwdenk 
334bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
335bdccc4feSwdenk 	bootcount = bootcount_load();
336bdccc4feSwdenk 	bootcount++;
337bdccc4feSwdenk 	bootcount_store (bootcount);
338f2abca84SSimon Glass 	setenv_ulong("bootcount", bootcount);
339f2abca84SSimon Glass 	bootlimit = getenv_ulong("bootlimit", 10, 0);
340bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
341bdccc4feSwdenk 
342c609719bSwdenk 	s = getenv ("bootdelay");
343c609719bSwdenk 	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
344c609719bSwdenk 
34599bd544eSStephen Warren #ifdef CONFIG_OF_CONTROL
34699bd544eSStephen Warren 	bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
34799bd544eSStephen Warren 			bootdelay);
34899bd544eSStephen Warren #endif
34999bd544eSStephen Warren 
350a6c7ad2fSwdenk 	debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
351c609719bSwdenk 
352317d6c57SHeiko Schocher #if defined(CONFIG_MENU_SHOW)
353317d6c57SHeiko Schocher 	bootdelay = menu_show(bootdelay);
354317d6c57SHeiko Schocher #endif
355c609719bSwdenk # ifdef CONFIG_BOOT_RETRY_TIME
3566dd652faSwdenk 	init_cmd_timeout ();
357c609719bSwdenk # endif	/* CONFIG_BOOT_RETRY_TIME */
358c609719bSwdenk 
359b428f6a8SYuri Tikhonov #ifdef CONFIG_POST
360b428f6a8SYuri Tikhonov 	if (gd->flags & GD_FLG_POSTFAIL) {
361b428f6a8SYuri Tikhonov 		s = getenv("failbootcmd");
362b428f6a8SYuri Tikhonov 	}
363b428f6a8SYuri Tikhonov 	else
364b428f6a8SYuri Tikhonov #endif /* CONFIG_POST */
365bdccc4feSwdenk #ifdef CONFIG_BOOTCOUNT_LIMIT
366bdccc4feSwdenk 	if (bootlimit && (bootcount > bootlimit)) {
367bdccc4feSwdenk 		printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
368bdccc4feSwdenk 			(unsigned)bootlimit);
369bdccc4feSwdenk 		s = getenv ("altbootcmd");
370bdccc4feSwdenk 	}
371bdccc4feSwdenk 	else
372bdccc4feSwdenk #endif /* CONFIG_BOOTCOUNT_LIMIT */
373c609719bSwdenk 		s = getenv ("bootcmd");
374224b72e6SChe-Liang Chiou #ifdef CONFIG_OF_CONTROL
375224b72e6SChe-Liang Chiou 	/* Allow the fdt to override the boot command */
376224b72e6SChe-Liang Chiou 	env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
377224b72e6SChe-Liang Chiou 	if (env)
378224b72e6SChe-Liang Chiou 		s = env;
37967e1ea26SDoug Anderson 
380fcabc24fSSimon Glass 	process_fdt_options(gd->fdt_blob);
381fcabc24fSSimon Glass 
38267e1ea26SDoug Anderson 	/*
38367e1ea26SDoug Anderson 	 * If the bootsecure option was chosen, use secure_boot_cmd().
38467e1ea26SDoug Anderson 	 * Always use 'env' in this case, since bootsecure requres that the
38567e1ea26SDoug Anderson 	 * bootcmd was specified in the FDT too.
38667e1ea26SDoug Anderson 	 */
38767e1ea26SDoug Anderson 	if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
38867e1ea26SDoug Anderson 		secure_boot_cmd(env);
38967e1ea26SDoug Anderson 
390224b72e6SChe-Liang Chiou #endif /* CONFIG_OF_CONTROL */
391a6c7ad2fSwdenk 
392a6c7ad2fSwdenk 	debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
393a6c7ad2fSwdenk 
39493d7212fSJoe Hershberger 	if (bootdelay != -1 && s && !abortboot(bootdelay)) {
39500ddacc1SMark Langsdorf #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
396c609719bSwdenk 		int prev = disable_ctrlc(1);	/* disable Control C checking */
397c609719bSwdenk #endif
398c609719bSwdenk 
3993a8a02beSSimon Glass 		run_command_list(s, -1, 0);
400c609719bSwdenk 
40100ddacc1SMark Langsdorf #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
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");
409c8a2079eSJason Hobbs 		if (s)
4103a8a02beSSimon Glass 			run_command_list(s, -1, 0);
411c7de829cSwdenk 	}
412c7de829cSwdenk #endif /* CONFIG_MENUKEY */
413bc2b4c27SSimon Glass }
414c609719bSwdenk #endif /* CONFIG_BOOTDELAY */
415c7de829cSwdenk 
416bc2b4c27SSimon Glass void main_loop(void)
417bc2b4c27SSimon Glass {
418bc2b4c27SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
419bc2b4c27SSimon Glass 	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
420bc2b4c27SSimon Glass 	int len;
421bc2b4c27SSimon Glass 	int rc = 1;
422bc2b4c27SSimon Glass 	int flag;
423bc2b4c27SSimon Glass #endif
424bc2b4c27SSimon Glass #ifdef CONFIG_PREBOOT
425bc2b4c27SSimon Glass 	char *p;
426bc2b4c27SSimon Glass #endif
427bc2b4c27SSimon Glass 
428bc2b4c27SSimon Glass 	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
429bc2b4c27SSimon Glass 
4300f605c15SSimon Glass #ifndef CONFIG_SYS_GENERIC_BOARD
4310f605c15SSimon Glass 	puts("Warning: Your board does not use generic board. Please read\n");
4320f605c15SSimon Glass 	puts("doc/README.generic-board and take action. Boards not\n");
4330f605c15SSimon Glass 	puts("upgraded by the late 2014 may break or be removed.\n");
4340f605c15SSimon Glass #endif
4350f605c15SSimon Glass 
436bc2b4c27SSimon Glass #ifdef CONFIG_MODEM_SUPPORT
437bc2b4c27SSimon Glass 	debug("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
438bc2b4c27SSimon Glass 	if (do_mdm_init) {
439bc2b4c27SSimon Glass 		char *str = strdup(getenv("mdm_cmd"));
440bc2b4c27SSimon Glass 		setenv("preboot", str);  /* set or delete definition */
441bc2b4c27SSimon Glass 		if (str != NULL)
442bc2b4c27SSimon Glass 			free(str);
443bc2b4c27SSimon Glass 		mdm_init(); /* wait for modem connection */
444bc2b4c27SSimon Glass 	}
445bc2b4c27SSimon Glass #endif  /* CONFIG_MODEM_SUPPORT */
446bc2b4c27SSimon Glass 
447bc2b4c27SSimon Glass #ifdef CONFIG_VERSION_VARIABLE
448bc2b4c27SSimon Glass 	{
449bc2b4c27SSimon Glass 		setenv("ver", version_string);  /* set version variable */
450bc2b4c27SSimon Glass 	}
451bc2b4c27SSimon Glass #endif /* CONFIG_VERSION_VARIABLE */
452bc2b4c27SSimon Glass 
453bc2b4c27SSimon Glass #ifdef CONFIG_SYS_HUSH_PARSER
454bc2b4c27SSimon Glass 	u_boot_hush_start();
455bc2b4c27SSimon Glass #endif
456bc2b4c27SSimon Glass 
457bc2b4c27SSimon Glass #if defined(CONFIG_HUSH_INIT_VAR)
458bc2b4c27SSimon Glass 	hush_init_var();
459bc2b4c27SSimon Glass #endif
460bc2b4c27SSimon Glass 
461bc2b4c27SSimon Glass #ifdef CONFIG_PREBOOT
462bc2b4c27SSimon Glass 	p = getenv("preboot");
463bc2b4c27SSimon Glass 	if (p != NULL) {
464bc2b4c27SSimon Glass # ifdef CONFIG_AUTOBOOT_KEYED
465bc2b4c27SSimon Glass 		int prev = disable_ctrlc(1);	/* disable Control C checking */
466bc2b4c27SSimon Glass # endif
467bc2b4c27SSimon Glass 
468bc2b4c27SSimon Glass 		run_command_list(p, -1, 0);
469bc2b4c27SSimon Glass 
470bc2b4c27SSimon Glass # ifdef CONFIG_AUTOBOOT_KEYED
471bc2b4c27SSimon Glass 		disable_ctrlc(prev);	/* restore Control C checking */
472bc2b4c27SSimon Glass # endif
473bc2b4c27SSimon Glass 	}
474bc2b4c27SSimon Glass #endif /* CONFIG_PREBOOT */
475bc2b4c27SSimon Glass 
476bc2b4c27SSimon Glass #if defined(CONFIG_UPDATE_TFTP)
477bc2b4c27SSimon Glass 	update_tftp(0UL);
478bc2b4c27SSimon Glass #endif /* CONFIG_UPDATE_TFTP */
479bc2b4c27SSimon Glass 
480bc2b4c27SSimon Glass #ifdef CONFIG_BOOTDELAY
481bc2b4c27SSimon Glass 	process_boot_delay();
482bc2b4c27SSimon Glass #endif
483c609719bSwdenk 	/*
484c609719bSwdenk 	 * Main Loop for Monitor Command Processing
485c609719bSwdenk 	 */
4866d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_HUSH_PARSER
487c609719bSwdenk 	parse_file_outer();
488c609719bSwdenk 	/* This point is never reached */
489c609719bSwdenk 	for (;;);
490c609719bSwdenk #else
491c609719bSwdenk 	for (;;) {
492c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
493c609719bSwdenk 		if (rc >= 0) {
494c609719bSwdenk 			/* Saw enough of a valid command to
495c609719bSwdenk 			 * restart the timeout.
496c609719bSwdenk 			 */
497c609719bSwdenk 			reset_cmd_timeout();
498c609719bSwdenk 		}
499c609719bSwdenk #endif
5006d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		len = readline (CONFIG_SYS_PROMPT);
501c609719bSwdenk 
502c609719bSwdenk 		flag = 0;	/* assume no special flags for now */
503c609719bSwdenk 		if (len > 0)
504c609719bSwdenk 			strcpy (lastcommand, console_buffer);
505c609719bSwdenk 		else if (len == 0)
506c609719bSwdenk 			flag |= CMD_FLAG_REPEAT;
507c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
508c609719bSwdenk 		else if (len == -2) {
509c609719bSwdenk 			/* -2 means timed out, retry autoboot
510c609719bSwdenk 			 */
5114b9206edSwdenk 			puts ("\nTimed out waiting for command\n");
512c609719bSwdenk # ifdef CONFIG_RESET_TO_RETRY
513c609719bSwdenk 			/* Reinit board to run initialization code again */
514c609719bSwdenk 			do_reset (NULL, 0, 0, NULL);
515c609719bSwdenk # else
516c609719bSwdenk 			return;		/* retry autoboot */
517c609719bSwdenk # endif
518c609719bSwdenk 		}
519c609719bSwdenk #endif
520c609719bSwdenk 
521c609719bSwdenk 		if (len == -1)
5224b9206edSwdenk 			puts ("<INTERRUPT>\n");
523c609719bSwdenk 		else
52453071532SSimon Glass 			rc = run_command(lastcommand, flag);
525c609719bSwdenk 
526c609719bSwdenk 		if (rc <= 0) {
527c609719bSwdenk 			/* invalid command or not repeatable, forget it */
528c609719bSwdenk 			lastcommand[0] = 0;
529c609719bSwdenk 		}
530c609719bSwdenk 	}
5316d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /*CONFIG_SYS_HUSH_PARSER*/
532c609719bSwdenk }
533c609719bSwdenk 
5346dd652faSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
5356dd652faSwdenk /***************************************************************************
53619973b6aSWolfgang Denk  * initialize command line timeout
5376dd652faSwdenk  */
5386dd652faSwdenk void init_cmd_timeout(void)
5396dd652faSwdenk {
5406dd652faSwdenk 	char *s = getenv ("bootretry");
5416dd652faSwdenk 
5426dd652faSwdenk 	if (s != NULL)
543b028f715Swdenk 		retry_time = (int)simple_strtol(s, NULL, 10);
5446dd652faSwdenk 	else
5456dd652faSwdenk 		retry_time =  CONFIG_BOOT_RETRY_TIME;
5466dd652faSwdenk 
5476dd652faSwdenk 	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
5486dd652faSwdenk 		retry_time = CONFIG_BOOT_RETRY_MIN;
5496dd652faSwdenk }
5506dd652faSwdenk 
551c609719bSwdenk /***************************************************************************
552c609719bSwdenk  * reset command line timeout to retry_time seconds
553c609719bSwdenk  */
554c609719bSwdenk void reset_cmd_timeout(void)
555c609719bSwdenk {
556c609719bSwdenk 	endtime = endtick(retry_time);
557c609719bSwdenk }
558c609719bSwdenk #endif
559c609719bSwdenk 
560501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
561501090aaSWolfgang Denk 
562501090aaSWolfgang Denk /*
563501090aaSWolfgang Denk  * cmdline-editing related codes from vivi.
564501090aaSWolfgang Denk  * Author: Janghoon Lyu <nandy@mizi.com>
565501090aaSWolfgang Denk  */
566501090aaSWolfgang Denk 
567501090aaSWolfgang Denk #define putnstr(str,n)	do {			\
568dc4b0b38SAndrew Klossner 		printf ("%.*s", (int)n, str);	\
569501090aaSWolfgang Denk 	} while (0)
570501090aaSWolfgang Denk 
571501090aaSWolfgang Denk #define CTL_CH(c)		((c) - 'a' + 1)
572501090aaSWolfgang Denk #define CTL_BACKSPACE		('\b')
573501090aaSWolfgang Denk #define DEL			((char)255)
574501090aaSWolfgang Denk #define DEL7			((char)127)
575501090aaSWolfgang Denk #define CREAD_HIST_CHAR		('!')
576501090aaSWolfgang Denk 
577501090aaSWolfgang Denk #define getcmd_putch(ch)	putc(ch)
578501090aaSWolfgang Denk #define getcmd_getch()		getc()
579501090aaSWolfgang Denk #define getcmd_cbeep()		getcmd_putch('\a')
580501090aaSWolfgang Denk 
581501090aaSWolfgang Denk #define HIST_MAX		20
5828804ae3bSPeter Tyser #define HIST_SIZE		CONFIG_SYS_CBSIZE
583501090aaSWolfgang Denk 
584199adb60SKim Phillips static int hist_max;
585199adb60SKim Phillips static int hist_add_idx;
586501090aaSWolfgang Denk static int hist_cur = -1;
587199adb60SKim Phillips static unsigned hist_num;
588501090aaSWolfgang Denk 
589199adb60SKim Phillips static char *hist_list[HIST_MAX];
590199adb60SKim Phillips static char hist_lines[HIST_MAX][HIST_SIZE + 1];	/* Save room for NULL */
591501090aaSWolfgang Denk 
592501090aaSWolfgang Denk #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
593501090aaSWolfgang Denk 
594501090aaSWolfgang Denk static void hist_init(void)
595501090aaSWolfgang Denk {
596501090aaSWolfgang Denk 	int i;
597501090aaSWolfgang Denk 
598501090aaSWolfgang Denk 	hist_max = 0;
599501090aaSWolfgang Denk 	hist_add_idx = 0;
600501090aaSWolfgang Denk 	hist_cur = -1;
601501090aaSWolfgang Denk 	hist_num = 0;
602501090aaSWolfgang Denk 
603501090aaSWolfgang Denk 	for (i = 0; i < HIST_MAX; i++) {
604501090aaSWolfgang Denk 		hist_list[i] = hist_lines[i];
605501090aaSWolfgang Denk 		hist_list[i][0] = '\0';
606501090aaSWolfgang Denk 	}
607501090aaSWolfgang Denk }
608501090aaSWolfgang Denk 
609501090aaSWolfgang Denk static void cread_add_to_hist(char *line)
610501090aaSWolfgang Denk {
611501090aaSWolfgang Denk 	strcpy(hist_list[hist_add_idx], line);
612501090aaSWolfgang Denk 
613501090aaSWolfgang Denk 	if (++hist_add_idx >= HIST_MAX)
614501090aaSWolfgang Denk 		hist_add_idx = 0;
615501090aaSWolfgang Denk 
616501090aaSWolfgang Denk 	if (hist_add_idx > hist_max)
617501090aaSWolfgang Denk 		hist_max = hist_add_idx;
618501090aaSWolfgang Denk 
619501090aaSWolfgang Denk 	hist_num++;
620501090aaSWolfgang Denk }
621501090aaSWolfgang Denk 
622501090aaSWolfgang Denk static char* hist_prev(void)
623501090aaSWolfgang Denk {
624501090aaSWolfgang Denk 	char *ret;
625501090aaSWolfgang Denk 	int old_cur;
626501090aaSWolfgang Denk 
627501090aaSWolfgang Denk 	if (hist_cur < 0)
628501090aaSWolfgang Denk 		return NULL;
629501090aaSWolfgang Denk 
630501090aaSWolfgang Denk 	old_cur = hist_cur;
631501090aaSWolfgang Denk 	if (--hist_cur < 0)
632501090aaSWolfgang Denk 		hist_cur = hist_max;
633501090aaSWolfgang Denk 
634501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
635501090aaSWolfgang Denk 		hist_cur = old_cur;
636501090aaSWolfgang Denk 		ret = NULL;
637501090aaSWolfgang Denk 	} else
638501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
639501090aaSWolfgang Denk 
640501090aaSWolfgang Denk 	return (ret);
641501090aaSWolfgang Denk }
642501090aaSWolfgang Denk 
643501090aaSWolfgang Denk static char* hist_next(void)
644501090aaSWolfgang Denk {
645501090aaSWolfgang Denk 	char *ret;
646501090aaSWolfgang Denk 
647501090aaSWolfgang Denk 	if (hist_cur < 0)
648501090aaSWolfgang Denk 		return NULL;
649501090aaSWolfgang Denk 
650501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx)
651501090aaSWolfgang Denk 		return NULL;
652501090aaSWolfgang Denk 
653501090aaSWolfgang Denk 	if (++hist_cur > hist_max)
654501090aaSWolfgang Denk 		hist_cur = 0;
655501090aaSWolfgang Denk 
656501090aaSWolfgang Denk 	if (hist_cur == hist_add_idx) {
657501090aaSWolfgang Denk 		ret = "";
658501090aaSWolfgang Denk 	} else
659501090aaSWolfgang Denk 		ret = hist_list[hist_cur];
660501090aaSWolfgang Denk 
661501090aaSWolfgang Denk 	return (ret);
662501090aaSWolfgang Denk }
663501090aaSWolfgang Denk 
6643ca9122fSStefan Roese #ifndef CONFIG_CMDLINE_EDITING
665501090aaSWolfgang Denk static void cread_print_hist_list(void)
666501090aaSWolfgang Denk {
667501090aaSWolfgang Denk 	int i;
668501090aaSWolfgang Denk 	unsigned long n;
669501090aaSWolfgang Denk 
670501090aaSWolfgang Denk 	n = hist_num - hist_max;
671501090aaSWolfgang Denk 
672501090aaSWolfgang Denk 	i = hist_add_idx + 1;
673501090aaSWolfgang Denk 	while (1) {
674501090aaSWolfgang Denk 		if (i > hist_max)
675501090aaSWolfgang Denk 			i = 0;
676501090aaSWolfgang Denk 		if (i == hist_add_idx)
677501090aaSWolfgang Denk 			break;
678501090aaSWolfgang Denk 		printf("%s\n", hist_list[i]);
679501090aaSWolfgang Denk 		n++;
680501090aaSWolfgang Denk 		i++;
681501090aaSWolfgang Denk 	}
682501090aaSWolfgang Denk }
6833ca9122fSStefan Roese #endif /* CONFIG_CMDLINE_EDITING */
684501090aaSWolfgang Denk 
685501090aaSWolfgang Denk #define BEGINNING_OF_LINE() {			\
686501090aaSWolfgang Denk 	while (num) {				\
687501090aaSWolfgang Denk 		getcmd_putch(CTL_BACKSPACE);	\
688501090aaSWolfgang Denk 		num--;				\
689501090aaSWolfgang Denk 	}					\
690501090aaSWolfgang Denk }
691501090aaSWolfgang Denk 
692501090aaSWolfgang Denk #define ERASE_TO_EOL() {				\
693501090aaSWolfgang Denk 	if (num < eol_num) {				\
6948faba489SMike Frysinger 		printf("%*s", (int)(eol_num - num), ""); \
6958faba489SMike Frysinger 		do {					\
696501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);	\
6978faba489SMike Frysinger 		} while (--eol_num > num);		\
698501090aaSWolfgang Denk 	}						\
699501090aaSWolfgang Denk }
700501090aaSWolfgang Denk 
701501090aaSWolfgang Denk #define REFRESH_TO_EOL() {			\
702501090aaSWolfgang Denk 	if (num < eol_num) {			\
703501090aaSWolfgang Denk 		wlen = eol_num - num;		\
704501090aaSWolfgang Denk 		putnstr(buf + num, wlen);	\
705501090aaSWolfgang Denk 		num = eol_num;			\
706501090aaSWolfgang Denk 	}					\
707501090aaSWolfgang Denk }
708501090aaSWolfgang Denk 
709501090aaSWolfgang Denk static void cread_add_char(char ichar, int insert, unsigned long *num,
710501090aaSWolfgang Denk 	       unsigned long *eol_num, char *buf, unsigned long len)
711501090aaSWolfgang Denk {
712501090aaSWolfgang Denk 	unsigned long wlen;
713501090aaSWolfgang Denk 
714501090aaSWolfgang Denk 	/* room ??? */
715501090aaSWolfgang Denk 	if (insert || *num == *eol_num) {
716501090aaSWolfgang Denk 		if (*eol_num > len - 1) {
717501090aaSWolfgang Denk 			getcmd_cbeep();
718501090aaSWolfgang Denk 			return;
719501090aaSWolfgang Denk 		}
720501090aaSWolfgang Denk 		(*eol_num)++;
721501090aaSWolfgang Denk 	}
722501090aaSWolfgang Denk 
723501090aaSWolfgang Denk 	if (insert) {
724501090aaSWolfgang Denk 		wlen = *eol_num - *num;
725501090aaSWolfgang Denk 		if (wlen > 1) {
726501090aaSWolfgang Denk 			memmove(&buf[*num+1], &buf[*num], wlen-1);
727501090aaSWolfgang Denk 		}
728501090aaSWolfgang Denk 
729501090aaSWolfgang Denk 		buf[*num] = ichar;
730501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
731501090aaSWolfgang Denk 		(*num)++;
732501090aaSWolfgang Denk 		while (--wlen) {
733501090aaSWolfgang Denk 			getcmd_putch(CTL_BACKSPACE);
734501090aaSWolfgang Denk 		}
735501090aaSWolfgang Denk 	} else {
736501090aaSWolfgang Denk 		/* echo the character */
737501090aaSWolfgang Denk 		wlen = 1;
738501090aaSWolfgang Denk 		buf[*num] = ichar;
739501090aaSWolfgang Denk 		putnstr(buf + *num, wlen);
740501090aaSWolfgang Denk 		(*num)++;
741501090aaSWolfgang Denk 	}
742501090aaSWolfgang Denk }
743501090aaSWolfgang Denk 
744501090aaSWolfgang Denk static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
745501090aaSWolfgang Denk 	      unsigned long *eol_num, char *buf, unsigned long len)
746501090aaSWolfgang Denk {
747501090aaSWolfgang Denk 	while (strsize--) {
748501090aaSWolfgang Denk 		cread_add_char(*str, insert, num, eol_num, buf, len);
749501090aaSWolfgang Denk 		str++;
750501090aaSWolfgang Denk 	}
751501090aaSWolfgang Denk }
752501090aaSWolfgang Denk 
7539c348311SHeiko Schocher static int cread_line(const char *const prompt, char *buf, unsigned int *len,
7549c348311SHeiko Schocher 		int timeout)
755501090aaSWolfgang Denk {
756501090aaSWolfgang Denk 	unsigned long num = 0;
757501090aaSWolfgang Denk 	unsigned long eol_num = 0;
758501090aaSWolfgang Denk 	unsigned long wlen;
759501090aaSWolfgang Denk 	char ichar;
760501090aaSWolfgang Denk 	int insert = 1;
761501090aaSWolfgang Denk 	int esc_len = 0;
762501090aaSWolfgang Denk 	char esc_save[8];
763ecc5500eSPeter Tyser 	int init_len = strlen(buf);
7649c348311SHeiko Schocher 	int first = 1;
765ecc5500eSPeter Tyser 
766ecc5500eSPeter Tyser 	if (init_len)
767ecc5500eSPeter Tyser 		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
768501090aaSWolfgang Denk 
769501090aaSWolfgang Denk 	while (1) {
77000ac50e3SAndreas Engel #ifdef CONFIG_BOOT_RETRY_TIME
77100ac50e3SAndreas Engel 		while (!tstc()) {	/* while no incoming data */
77200ac50e3SAndreas Engel 			if (retry_time >= 0 && get_ticks() > endtime)
77300ac50e3SAndreas Engel 				return (-2);	/* timed out */
77430dc165aSJens Scharsig 			WATCHDOG_RESET();
77500ac50e3SAndreas Engel 		}
77600ac50e3SAndreas Engel #endif
7779c348311SHeiko Schocher 		if (first && timeout) {
7789c348311SHeiko Schocher 			uint64_t etime = endtick(timeout);
7799c348311SHeiko Schocher 
7809c348311SHeiko Schocher 			while (!tstc()) {	/* while no incoming data */
7819c348311SHeiko Schocher 				if (get_ticks() >= etime)
7829c348311SHeiko Schocher 					return -2;	/* timed out */
7839c348311SHeiko Schocher 				WATCHDOG_RESET();
7849c348311SHeiko Schocher 			}
7859c348311SHeiko Schocher 			first = 0;
7869c348311SHeiko Schocher 		}
78700ac50e3SAndreas Engel 
788501090aaSWolfgang Denk 		ichar = getcmd_getch();
789501090aaSWolfgang Denk 
790501090aaSWolfgang Denk 		if ((ichar == '\n') || (ichar == '\r')) {
791dd9f06f0SWolfgang Denk 			putc('\n');
792501090aaSWolfgang Denk 			break;
793501090aaSWolfgang Denk 		}
794501090aaSWolfgang Denk 
795501090aaSWolfgang Denk 		/*
796501090aaSWolfgang Denk 		 * handle standard linux xterm esc sequences for arrow key, etc.
797501090aaSWolfgang Denk 		 */
798501090aaSWolfgang Denk 		if (esc_len != 0) {
799501090aaSWolfgang Denk 			if (esc_len == 1) {
800501090aaSWolfgang Denk 				if (ichar == '[') {
801501090aaSWolfgang Denk 					esc_save[esc_len] = ichar;
802501090aaSWolfgang Denk 					esc_len = 2;
803501090aaSWolfgang Denk 				} else {
804501090aaSWolfgang Denk 					cread_add_str(esc_save, esc_len, insert,
805501090aaSWolfgang Denk 						      &num, &eol_num, buf, *len);
806501090aaSWolfgang Denk 					esc_len = 0;
807501090aaSWolfgang Denk 				}
808501090aaSWolfgang Denk 				continue;
809501090aaSWolfgang Denk 			}
810501090aaSWolfgang Denk 
811501090aaSWolfgang Denk 			switch (ichar) {
812501090aaSWolfgang Denk 
813501090aaSWolfgang Denk 			case 'D':	/* <- key */
814501090aaSWolfgang Denk 				ichar = CTL_CH('b');
815501090aaSWolfgang Denk 				esc_len = 0;
816501090aaSWolfgang Denk 				break;
817501090aaSWolfgang Denk 			case 'C':	/* -> key */
818501090aaSWolfgang Denk 				ichar = CTL_CH('f');
819501090aaSWolfgang Denk 				esc_len = 0;
820501090aaSWolfgang Denk 				break;	/* pass off to ^F handler */
821501090aaSWolfgang Denk 			case 'H':	/* Home key */
822501090aaSWolfgang Denk 				ichar = CTL_CH('a');
823501090aaSWolfgang Denk 				esc_len = 0;
824501090aaSWolfgang Denk 				break;	/* pass off to ^A handler */
825501090aaSWolfgang Denk 			case 'A':	/* up arrow */
826501090aaSWolfgang Denk 				ichar = CTL_CH('p');
827501090aaSWolfgang Denk 				esc_len = 0;
828501090aaSWolfgang Denk 				break;	/* pass off to ^P handler */
829501090aaSWolfgang Denk 			case 'B':	/* down arrow */
830501090aaSWolfgang Denk 				ichar = CTL_CH('n');
831501090aaSWolfgang Denk 				esc_len = 0;
832501090aaSWolfgang Denk 				break;	/* pass off to ^N handler */
833501090aaSWolfgang Denk 			default:
834501090aaSWolfgang Denk 				esc_save[esc_len++] = ichar;
835501090aaSWolfgang Denk 				cread_add_str(esc_save, esc_len, insert,
836501090aaSWolfgang Denk 					      &num, &eol_num, buf, *len);
837501090aaSWolfgang Denk 				esc_len = 0;
838501090aaSWolfgang Denk 				continue;
839501090aaSWolfgang Denk 			}
840501090aaSWolfgang Denk 		}
841501090aaSWolfgang Denk 
842501090aaSWolfgang Denk 		switch (ichar) {
843501090aaSWolfgang Denk 		case 0x1b:
844501090aaSWolfgang Denk 			if (esc_len == 0) {
845501090aaSWolfgang Denk 				esc_save[esc_len] = ichar;
846501090aaSWolfgang Denk 				esc_len = 1;
847501090aaSWolfgang Denk 			} else {
848dd9f06f0SWolfgang Denk 				puts("impossible condition #876\n");
849501090aaSWolfgang Denk 				esc_len = 0;
850501090aaSWolfgang Denk 			}
851501090aaSWolfgang Denk 			break;
852501090aaSWolfgang Denk 
853501090aaSWolfgang Denk 		case CTL_CH('a'):
854501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
855501090aaSWolfgang Denk 			break;
856501090aaSWolfgang Denk 		case CTL_CH('c'):	/* ^C - break */
857501090aaSWolfgang Denk 			*buf = '\0';	/* discard input */
858501090aaSWolfgang Denk 			return (-1);
859501090aaSWolfgang Denk 		case CTL_CH('f'):
860501090aaSWolfgang Denk 			if (num < eol_num) {
861501090aaSWolfgang Denk 				getcmd_putch(buf[num]);
862501090aaSWolfgang Denk 				num++;
863501090aaSWolfgang Denk 			}
864501090aaSWolfgang Denk 			break;
865501090aaSWolfgang Denk 		case CTL_CH('b'):
866501090aaSWolfgang Denk 			if (num) {
867501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
868501090aaSWolfgang Denk 				num--;
869501090aaSWolfgang Denk 			}
870501090aaSWolfgang Denk 			break;
871501090aaSWolfgang Denk 		case CTL_CH('d'):
872501090aaSWolfgang Denk 			if (num < eol_num) {
873501090aaSWolfgang Denk 				wlen = eol_num - num - 1;
874501090aaSWolfgang Denk 				if (wlen) {
875501090aaSWolfgang Denk 					memmove(&buf[num], &buf[num+1], wlen);
876501090aaSWolfgang Denk 					putnstr(buf + num, wlen);
877501090aaSWolfgang Denk 				}
878501090aaSWolfgang Denk 
879501090aaSWolfgang Denk 				getcmd_putch(' ');
880501090aaSWolfgang Denk 				do {
881501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
882501090aaSWolfgang Denk 				} while (wlen--);
883501090aaSWolfgang Denk 				eol_num--;
884501090aaSWolfgang Denk 			}
885501090aaSWolfgang Denk 			break;
886501090aaSWolfgang Denk 		case CTL_CH('k'):
887501090aaSWolfgang Denk 			ERASE_TO_EOL();
888501090aaSWolfgang Denk 			break;
889501090aaSWolfgang Denk 		case CTL_CH('e'):
890501090aaSWolfgang Denk 			REFRESH_TO_EOL();
891501090aaSWolfgang Denk 			break;
892501090aaSWolfgang Denk 		case CTL_CH('o'):
893501090aaSWolfgang Denk 			insert = !insert;
894501090aaSWolfgang Denk 			break;
895501090aaSWolfgang Denk 		case CTL_CH('x'):
89623d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case CTL_CH('u'):
897501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
898501090aaSWolfgang Denk 			ERASE_TO_EOL();
899501090aaSWolfgang Denk 			break;
900501090aaSWolfgang Denk 		case DEL:
901501090aaSWolfgang Denk 		case DEL7:
902501090aaSWolfgang Denk 		case 8:
903501090aaSWolfgang Denk 			if (num) {
904501090aaSWolfgang Denk 				wlen = eol_num - num;
905501090aaSWolfgang Denk 				num--;
906501090aaSWolfgang Denk 				memmove(&buf[num], &buf[num+1], wlen);
907501090aaSWolfgang Denk 				getcmd_putch(CTL_BACKSPACE);
908501090aaSWolfgang Denk 				putnstr(buf + num, wlen);
909501090aaSWolfgang Denk 				getcmd_putch(' ');
910501090aaSWolfgang Denk 				do {
911501090aaSWolfgang Denk 					getcmd_putch(CTL_BACKSPACE);
912501090aaSWolfgang Denk 				} while (wlen--);
913501090aaSWolfgang Denk 				eol_num--;
914501090aaSWolfgang Denk 			}
915501090aaSWolfgang Denk 			break;
916501090aaSWolfgang Denk 		case CTL_CH('p'):
917501090aaSWolfgang Denk 		case CTL_CH('n'):
918501090aaSWolfgang Denk 		{
919501090aaSWolfgang Denk 			char * hline;
920501090aaSWolfgang Denk 
921501090aaSWolfgang Denk 			esc_len = 0;
922501090aaSWolfgang Denk 
923501090aaSWolfgang Denk 			if (ichar == CTL_CH('p'))
924501090aaSWolfgang Denk 				hline = hist_prev();
925501090aaSWolfgang Denk 			else
926501090aaSWolfgang Denk 				hline = hist_next();
927501090aaSWolfgang Denk 
928501090aaSWolfgang Denk 			if (!hline) {
929501090aaSWolfgang Denk 				getcmd_cbeep();
930501090aaSWolfgang Denk 				continue;
931501090aaSWolfgang Denk 			}
932501090aaSWolfgang Denk 
933501090aaSWolfgang Denk 			/* nuke the current line */
934501090aaSWolfgang Denk 			/* first, go home */
935501090aaSWolfgang Denk 			BEGINNING_OF_LINE();
936501090aaSWolfgang Denk 
937501090aaSWolfgang Denk 			/* erase to end of line */
938501090aaSWolfgang Denk 			ERASE_TO_EOL();
939501090aaSWolfgang Denk 
940501090aaSWolfgang Denk 			/* copy new line into place and display */
941501090aaSWolfgang Denk 			strcpy(buf, hline);
942501090aaSWolfgang Denk 			eol_num = strlen(buf);
943501090aaSWolfgang Denk 			REFRESH_TO_EOL();
944501090aaSWolfgang Denk 			continue;
945501090aaSWolfgang Denk 		}
94623d0baf9SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_AUTO_COMPLETE
94723d0baf9SJean-Christophe PLAGNIOL-VILLARD 		case '\t': {
94823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			int num2, col;
94923d0baf9SJean-Christophe PLAGNIOL-VILLARD 
95023d0baf9SJean-Christophe PLAGNIOL-VILLARD 			/* do not autocomplete when in the middle */
95123d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (num < eol_num) {
95223d0baf9SJean-Christophe PLAGNIOL-VILLARD 				getcmd_cbeep();
95323d0baf9SJean-Christophe PLAGNIOL-VILLARD 				break;
95423d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
95523d0baf9SJean-Christophe PLAGNIOL-VILLARD 
95623d0baf9SJean-Christophe PLAGNIOL-VILLARD 			buf[num] = '\0';
95723d0baf9SJean-Christophe PLAGNIOL-VILLARD 			col = strlen(prompt) + eol_num;
95823d0baf9SJean-Christophe PLAGNIOL-VILLARD 			num2 = num;
95923d0baf9SJean-Christophe PLAGNIOL-VILLARD 			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
96023d0baf9SJean-Christophe PLAGNIOL-VILLARD 				col = num2 - num;
96123d0baf9SJean-Christophe PLAGNIOL-VILLARD 				num += col;
96223d0baf9SJean-Christophe PLAGNIOL-VILLARD 				eol_num += col;
96323d0baf9SJean-Christophe PLAGNIOL-VILLARD 			}
96423d0baf9SJean-Christophe PLAGNIOL-VILLARD 			break;
96523d0baf9SJean-Christophe PLAGNIOL-VILLARD 		}
96623d0baf9SJean-Christophe PLAGNIOL-VILLARD #endif
967501090aaSWolfgang Denk 		default:
968501090aaSWolfgang Denk 			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
969501090aaSWolfgang Denk 			break;
970501090aaSWolfgang Denk 		}
971501090aaSWolfgang Denk 	}
972501090aaSWolfgang Denk 	*len = eol_num;
973501090aaSWolfgang Denk 	buf[eol_num] = '\0';	/* lose the newline */
974501090aaSWolfgang Denk 
975501090aaSWolfgang Denk 	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
976501090aaSWolfgang Denk 		cread_add_to_hist(buf);
977501090aaSWolfgang Denk 	hist_cur = hist_add_idx;
978501090aaSWolfgang Denk 
979f9239438SPeter Tyser 	return 0;
980501090aaSWolfgang Denk }
981501090aaSWolfgang Denk 
982501090aaSWolfgang Denk #endif /* CONFIG_CMDLINE_EDITING */
983501090aaSWolfgang Denk 
984c609719bSwdenk /****************************************************************************/
985c609719bSwdenk 
986c609719bSwdenk /*
987c609719bSwdenk  * Prompt for input and read a line.
988c609719bSwdenk  * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
989c609719bSwdenk  * time out when time goes past endtime (timebase time in ticks).
990c609719bSwdenk  * Return:	number of read characters
991c609719bSwdenk  *		-1 if break
992c609719bSwdenk  *		-2 if timed out
993c609719bSwdenk  */
994c609719bSwdenk int readline (const char *const prompt)
995c609719bSwdenk {
996ecc5500eSPeter Tyser 	/*
997ecc5500eSPeter Tyser 	 * If console_buffer isn't 0-length the user will be prompted to modify
998ecc5500eSPeter Tyser 	 * it instead of entering it from scratch as desired.
999ecc5500eSPeter Tyser 	 */
1000ecc5500eSPeter Tyser 	console_buffer[0] = '\0';
1001ecc5500eSPeter Tyser 
10029c348311SHeiko Schocher 	return readline_into_buffer(prompt, console_buffer, 0);
10036636b62aSJames Yang }
10046636b62aSJames Yang 
10056636b62aSJames Yang 
10069c348311SHeiko Schocher int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
10076636b62aSJames Yang {
10086636b62aSJames Yang 	char *p = buffer;
1009501090aaSWolfgang Denk #ifdef CONFIG_CMDLINE_EDITING
10108804ae3bSPeter Tyser 	unsigned int len = CONFIG_SYS_CBSIZE;
1011d8f961bbSStefan Roese 	int rc;
1012501090aaSWolfgang Denk 	static int initted = 0;
1013501090aaSWolfgang Denk 
1014597f6c26SJames Yang 	/*
1015597f6c26SJames Yang 	 * History uses a global array which is not
1016597f6c26SJames Yang 	 * writable until after relocation to RAM.
1017597f6c26SJames Yang 	 * Revert to non-history version if still
1018597f6c26SJames Yang 	 * running from flash.
1019597f6c26SJames Yang 	 */
1020597f6c26SJames Yang 	if (gd->flags & GD_FLG_RELOC) {
1021597f6c26SJames Yang 		if (!initted) {
1022597f6c26SJames Yang 			hist_init();
1023597f6c26SJames Yang 			initted = 1;
1024597f6c26SJames Yang 		}
1025597f6c26SJames Yang 
1026e491a71eSPeter Tyser 		if (prompt)
1027dd9f06f0SWolfgang Denk 			puts (prompt);
1028501090aaSWolfgang Denk 
10299c348311SHeiko Schocher 		rc = cread_line(prompt, p, &len, timeout);
1030d8f961bbSStefan Roese 		return rc < 0 ? rc : len;
1031597f6c26SJames Yang 
1032597f6c26SJames Yang 	} else {
1033597f6c26SJames Yang #endif	/* CONFIG_CMDLINE_EDITING */
10340ec59524SKumar Gala 	char * p_buf = p;
1035c609719bSwdenk 	int	n = 0;				/* buffer index		*/
1036c609719bSwdenk 	int	plen = 0;			/* prompt length	*/
1037c609719bSwdenk 	int	col;				/* output column cnt	*/
1038c609719bSwdenk 	char	c;
1039c609719bSwdenk 
1040c609719bSwdenk 	/* print prompt */
1041c609719bSwdenk 	if (prompt) {
1042c609719bSwdenk 		plen = strlen (prompt);
1043c609719bSwdenk 		puts (prompt);
1044c609719bSwdenk 	}
1045c609719bSwdenk 	col = plen;
1046c609719bSwdenk 
1047c609719bSwdenk 	for (;;) {
1048c609719bSwdenk #ifdef CONFIG_BOOT_RETRY_TIME
1049c609719bSwdenk 		while (!tstc()) {	/* while no incoming data */
1050c609719bSwdenk 			if (retry_time >= 0 && get_ticks() > endtime)
1051c609719bSwdenk 				return (-2);	/* timed out */
105230dc165aSJens Scharsig 			WATCHDOG_RESET();
1053c609719bSwdenk 		}
1054c609719bSwdenk #endif
1055c609719bSwdenk 		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
1056c609719bSwdenk 
1057c609719bSwdenk #ifdef CONFIG_SHOW_ACTIVITY
1058c609719bSwdenk 		while (!tstc()) {
1059c609719bSwdenk 			show_activity(0);
106030dc165aSJens Scharsig 			WATCHDOG_RESET();
1061c609719bSwdenk 		}
1062c609719bSwdenk #endif
1063c609719bSwdenk 		c = getc();
1064c609719bSwdenk 
1065c609719bSwdenk 		/*
1066c609719bSwdenk 		 * Special character handling
1067c609719bSwdenk 		 */
1068c609719bSwdenk 		switch (c) {
1069c609719bSwdenk 		case '\r':			/* Enter		*/
1070c609719bSwdenk 		case '\n':
1071c609719bSwdenk 			*p = '\0';
1072c609719bSwdenk 			puts ("\r\n");
10734933381aSSimon Glass 			return p - p_buf;
1074c609719bSwdenk 
107527aa8186Swdenk 		case '\0':			/* nul			*/
107627aa8186Swdenk 			continue;
107727aa8186Swdenk 
1078c609719bSwdenk 		case 0x03:			/* ^C - break		*/
10796636b62aSJames Yang 			p_buf[0] = '\0';	/* discard input */
10804933381aSSimon Glass 			return -1;
1081c609719bSwdenk 
1082c609719bSwdenk 		case 0x15:			/* ^U - erase line	*/
1083c609719bSwdenk 			while (col > plen) {
1084c609719bSwdenk 				puts (erase_seq);
1085c609719bSwdenk 				--col;
1086c609719bSwdenk 			}
10876636b62aSJames Yang 			p = p_buf;
1088c609719bSwdenk 			n = 0;
1089c609719bSwdenk 			continue;
1090c609719bSwdenk 
1091c609719bSwdenk 		case 0x17:			/* ^W - erase word	*/
10926636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1093c609719bSwdenk 			while ((n > 0) && (*p != ' ')) {
10946636b62aSJames Yang 				p=delete_char(p_buf, p, &col, &n, plen);
1095c609719bSwdenk 			}
1096c609719bSwdenk 			continue;
1097c609719bSwdenk 
1098c609719bSwdenk 		case 0x08:			/* ^H  - backspace	*/
1099c609719bSwdenk 		case 0x7F:			/* DEL - backspace	*/
11006636b62aSJames Yang 			p=delete_char(p_buf, p, &col, &n, plen);
1101c609719bSwdenk 			continue;
1102c609719bSwdenk 
1103c609719bSwdenk 		default:
1104c609719bSwdenk 			/*
1105c609719bSwdenk 			 * Must be a normal character then
1106c609719bSwdenk 			 */
11076d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 			if (n < CONFIG_SYS_CBSIZE-2) {
1108c609719bSwdenk 				if (c == '\t') {	/* expand TABs */
110904a85b3bSwdenk #ifdef CONFIG_AUTO_COMPLETE
111004a85b3bSwdenk 					/* if auto completion triggered just continue */
111104a85b3bSwdenk 					*p = '\0';
111204a85b3bSwdenk 					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
11136636b62aSJames Yang 						p = p_buf + n;	/* reset */
111404a85b3bSwdenk 						continue;
111504a85b3bSwdenk 					}
111604a85b3bSwdenk #endif
1117c609719bSwdenk 					puts (tab_seq+(col&07));
1118c609719bSwdenk 					col += 8 - (col&07);
1119c609719bSwdenk 				} else {
11209a8efc46SSimon Glass 					char buf[2];
11219a8efc46SSimon Glass 
11229a8efc46SSimon Glass 					/*
11234933381aSSimon Glass 					 * Echo input using puts() to force an
11249a8efc46SSimon Glass 					 * LCD flush if we are using an LCD
11259a8efc46SSimon Glass 					 */
11269a8efc46SSimon Glass 					++col;
11279a8efc46SSimon Glass 					buf[0] = c;
11289a8efc46SSimon Glass 					buf[1] = '\0';
11299a8efc46SSimon Glass 					puts(buf);
1130c609719bSwdenk 				}
1131c609719bSwdenk 				*p++ = c;
1132c609719bSwdenk 				++n;
1133c609719bSwdenk 			} else {			/* Buffer full		*/
1134c609719bSwdenk 				putc ('\a');
1135c609719bSwdenk 			}
1136c609719bSwdenk 		}
1137c609719bSwdenk 	}
1138597f6c26SJames Yang #ifdef CONFIG_CMDLINE_EDITING
1139597f6c26SJames Yang 	}
1140597f6c26SJames Yang #endif
1141c609719bSwdenk }
1142c609719bSwdenk 
1143c609719bSwdenk /****************************************************************************/
1144c609719bSwdenk 
1145c609719bSwdenk static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1146c609719bSwdenk {
1147c609719bSwdenk 	char *s;
1148c609719bSwdenk 
1149c609719bSwdenk 	if (*np == 0) {
1150c609719bSwdenk 		return (p);
1151c609719bSwdenk 	}
1152c609719bSwdenk 
1153c609719bSwdenk 	if (*(--p) == '\t') {			/* will retype the whole line	*/
1154c609719bSwdenk 		while (*colp > plen) {
1155c609719bSwdenk 			puts (erase_seq);
1156c609719bSwdenk 			(*colp)--;
1157c609719bSwdenk 		}
1158c609719bSwdenk 		for (s=buffer; s<p; ++s) {
1159c609719bSwdenk 			if (*s == '\t') {
1160c609719bSwdenk 				puts (tab_seq+((*colp) & 07));
1161c609719bSwdenk 				*colp += 8 - ((*colp) & 07);
1162c609719bSwdenk 			} else {
1163c609719bSwdenk 				++(*colp);
1164c609719bSwdenk 				putc (*s);
1165c609719bSwdenk 			}
1166c609719bSwdenk 		}
1167c609719bSwdenk 	} else {
1168c609719bSwdenk 		puts (erase_seq);
1169c609719bSwdenk 		(*colp)--;
1170c609719bSwdenk 	}
1171c609719bSwdenk 	(*np)--;
1172c609719bSwdenk 	return (p);
1173c609719bSwdenk }
1174c609719bSwdenk 
1175c609719bSwdenk /****************************************************************************/
1176c609719bSwdenk 
1177c609719bSwdenk int parse_line (char *line, char *argv[])
1178c609719bSwdenk {
1179c609719bSwdenk 	int nargs = 0;
1180c609719bSwdenk 
11813e408873SSimon Glass 	debug_parser("parse_line: \"%s\"\n", line);
11826d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	while (nargs < CONFIG_SYS_MAXARGS) {
1183c609719bSwdenk 
1184c609719bSwdenk 		/* skip any white space */
11854d91a6ecSJason Hobbs 		while (isblank(*line))
1186c609719bSwdenk 			++line;
1187c609719bSwdenk 
1188c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1189c609719bSwdenk 			argv[nargs] = NULL;
11903e408873SSimon Glass 			debug_parser("parse_line: nargs=%d\n", nargs);
11913e408873SSimon Glass 			return nargs;
1192c609719bSwdenk 		}
1193c609719bSwdenk 
1194c609719bSwdenk 		argv[nargs++] = line;	/* begin of argument string	*/
1195c609719bSwdenk 
1196c609719bSwdenk 		/* find end of string */
11974d91a6ecSJason Hobbs 		while (*line && !isblank(*line))
1198c609719bSwdenk 			++line;
1199c609719bSwdenk 
1200c609719bSwdenk 		if (*line == '\0') {	/* end of line, no more args	*/
1201c609719bSwdenk 			argv[nargs] = NULL;
12023e408873SSimon Glass 			debug_parser("parse_line: nargs=%d\n", nargs);
12033e408873SSimon Glass 			return nargs;
1204c609719bSwdenk 		}
1205c609719bSwdenk 
1206c609719bSwdenk 		*line++ = '\0';		/* terminate current arg	 */
1207c609719bSwdenk 	}
1208c609719bSwdenk 
12096d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1210c609719bSwdenk 
12113e408873SSimon Glass 	debug_parser("parse_line: nargs=%d\n", nargs);
1212c609719bSwdenk 	return (nargs);
1213c609719bSwdenk }
1214c609719bSwdenk 
1215c609719bSwdenk /****************************************************************************/
1216c609719bSwdenk 
12177fed89e0SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
1218c609719bSwdenk static void process_macros (const char *input, char *output)
1219c609719bSwdenk {
1220c609719bSwdenk 	char c, prev;
1221c609719bSwdenk 	const char *varname_start = NULL;
1222c609719bSwdenk 	int inputcnt = strlen (input);
12236d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	int outputcnt = CONFIG_SYS_CBSIZE;
1224c609719bSwdenk 	int state = 0;		/* 0 = waiting for '$'  */
122519973b6aSWolfgang Denk 
12265cf91d6bSwdenk 	/* 1 = waiting for '(' or '{' */
12275cf91d6bSwdenk 	/* 2 = waiting for ')' or '}' */
1228a25f862bSwdenk 	/* 3 = waiting for '''  */
1229c609719bSwdenk 	char *output_start = output;
1230c609719bSwdenk 
12313e408873SSimon Glass 	debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
123219973b6aSWolfgang Denk 		     input);
1233c609719bSwdenk 
1234c609719bSwdenk 	prev = '\0';		/* previous character   */
1235c609719bSwdenk 
1236c609719bSwdenk 	while (inputcnt && outputcnt) {
1237c609719bSwdenk 		c = *input++;
1238c609719bSwdenk 		inputcnt--;
1239c609719bSwdenk 
1240a25f862bSwdenk 		if (state != 3) {
1241c609719bSwdenk 			/* remove one level of escape characters */
1242c609719bSwdenk 			if ((c == '\\') && (prev != '\\')) {
1243c609719bSwdenk 				if (inputcnt-- == 0)
1244c609719bSwdenk 					break;
1245c609719bSwdenk 				prev = c;
1246c609719bSwdenk 				c = *input++;
1247c609719bSwdenk 			}
1248a25f862bSwdenk 		}
1249c609719bSwdenk 
1250c609719bSwdenk 		switch (state) {
1251c609719bSwdenk 		case 0:	/* Waiting for (unescaped) $    */
1252a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1253a25f862bSwdenk 				state = 3;
1254a25f862bSwdenk 				break;
1255a25f862bSwdenk 			}
1256c609719bSwdenk 			if ((c == '$') && (prev != '\\')) {
1257c609719bSwdenk 				state++;
1258c609719bSwdenk 			} else {
1259c609719bSwdenk 				*(output++) = c;
1260c609719bSwdenk 				outputcnt--;
1261c609719bSwdenk 			}
1262c609719bSwdenk 			break;
1263c609719bSwdenk 		case 1:	/* Waiting for (        */
12645cf91d6bSwdenk 			if (c == '(' || c == '{') {
1265c609719bSwdenk 				state++;
1266c609719bSwdenk 				varname_start = input;
1267c609719bSwdenk 			} else {
1268c609719bSwdenk 				state = 0;
1269c609719bSwdenk 				*(output++) = '$';
1270c609719bSwdenk 				outputcnt--;
1271c609719bSwdenk 
1272c609719bSwdenk 				if (outputcnt) {
1273c609719bSwdenk 					*(output++) = c;
1274c609719bSwdenk 					outputcnt--;
1275c609719bSwdenk 				}
1276c609719bSwdenk 			}
1277c609719bSwdenk 			break;
1278c609719bSwdenk 		case 2:	/* Waiting for )        */
12795cf91d6bSwdenk 			if (c == ')' || c == '}') {
1280c609719bSwdenk 				int i;
12816d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 				char envname[CONFIG_SYS_CBSIZE], *envval;
1282c609719bSwdenk 				int envcnt = input - varname_start - 1;	/* Varname # of chars */
1283c609719bSwdenk 
1284c609719bSwdenk 				/* Get the varname */
1285c609719bSwdenk 				for (i = 0; i < envcnt; i++) {
1286c609719bSwdenk 					envname[i] = varname_start[i];
1287c609719bSwdenk 				}
1288c609719bSwdenk 				envname[i] = 0;
1289c609719bSwdenk 
1290c609719bSwdenk 				/* Get its value */
1291c609719bSwdenk 				envval = getenv (envname);
1292c609719bSwdenk 
1293c609719bSwdenk 				/* Copy into the line if it exists */
1294c609719bSwdenk 				if (envval != NULL)
1295c609719bSwdenk 					while ((*envval) && outputcnt) {
1296c609719bSwdenk 						*(output++) = *(envval++);
1297c609719bSwdenk 						outputcnt--;
1298c609719bSwdenk 					}
1299c609719bSwdenk 				/* Look for another '$' */
1300c609719bSwdenk 				state = 0;
1301c609719bSwdenk 			}
1302c609719bSwdenk 			break;
1303a25f862bSwdenk 		case 3:	/* Waiting for '        */
1304a25f862bSwdenk 			if ((c == '\'') && (prev != '\\')) {
1305a25f862bSwdenk 				state = 0;
1306a25f862bSwdenk 			} else {
1307a25f862bSwdenk 				*(output++) = c;
1308a25f862bSwdenk 				outputcnt--;
1309c609719bSwdenk 			}
1310a25f862bSwdenk 			break;
1311a25f862bSwdenk 		}
1312c609719bSwdenk 		prev = c;
1313c609719bSwdenk 	}
1314c609719bSwdenk 
1315c609719bSwdenk 	if (outputcnt)
1316c609719bSwdenk 		*output = 0;
13179160b96fSBartlomiej Sieka 	else
13189160b96fSBartlomiej Sieka 		*(output - 1) = 0;
1319c609719bSwdenk 
13203e408873SSimon Glass 	debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
1321c609719bSwdenk 		     strlen(output_start), output_start);
1322c609719bSwdenk }
1323c609719bSwdenk 
1324c609719bSwdenk /****************************************************************************
1325c609719bSwdenk  * returns:
1326c609719bSwdenk  *	1  - command executed, repeatable
1327c609719bSwdenk  *	0  - command executed but not repeatable, interrupted commands are
1328c609719bSwdenk  *	     always considered not repeatable
1329c609719bSwdenk  *	-1 - not executed (unrecognized, bootd recursion or too many args)
13306d0f6bcfSJean-Christophe PLAGNIOL-VILLARD  *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1331c609719bSwdenk  *           considered unrecognized)
1332c609719bSwdenk  *
1333c609719bSwdenk  * WARNING:
1334c609719bSwdenk  *
1335c609719bSwdenk  * We must create a temporary copy of the command since the command we get
1336c609719bSwdenk  * may be the result from getenv(), which returns a pointer directly to
1337c609719bSwdenk  * the environment data, which may change magicly when the command we run
1338c609719bSwdenk  * creates or modifies environment variables (like "bootp" does).
1339c609719bSwdenk  */
134053071532SSimon Glass static int builtin_run_command(const char *cmd, int flag)
1341c609719bSwdenk {
13426d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
1343c609719bSwdenk 	char *token;			/* start of token in cmdbuf	*/
1344c609719bSwdenk 	char *sep;			/* end of token (separator) in cmdbuf */
13456d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char finaltoken[CONFIG_SYS_CBSIZE];
1346c609719bSwdenk 	char *str = cmdbuf;
13476d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
1348f07771ccSwdenk 	int argc, inquotes;
1349c609719bSwdenk 	int repeatable = 1;
1350f07771ccSwdenk 	int rc = 0;
1351c609719bSwdenk 
13523e408873SSimon Glass 	debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
13533e408873SSimon Glass 	if (DEBUG_PARSER) {
13543e408873SSimon Glass 		/* use puts - string may be loooong */
13553e408873SSimon Glass 		puts(cmd ? cmd : "NULL");
1356c609719bSwdenk 		puts("\"\n");
13573e408873SSimon Glass 	}
1358c609719bSwdenk 	clear_ctrlc();		/* forget any previous Control C */
1359c609719bSwdenk 
1360c609719bSwdenk 	if (!cmd || !*cmd) {
1361c609719bSwdenk 		return -1;	/* empty command */
1362c609719bSwdenk 	}
1363c609719bSwdenk 
13646d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1365c609719bSwdenk 		puts ("## Command too long!\n");
1366c609719bSwdenk 		return -1;
1367c609719bSwdenk 	}
1368c609719bSwdenk 
1369c609719bSwdenk 	strcpy (cmdbuf, cmd);
1370c609719bSwdenk 
1371c609719bSwdenk 	/* Process separators and check for invalid
1372c609719bSwdenk 	 * repeatable commands
1373c609719bSwdenk 	 */
1374c609719bSwdenk 
13753e408873SSimon Glass 	debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
1376c609719bSwdenk 	while (*str) {
1377c609719bSwdenk 
1378c609719bSwdenk 		/*
1379c609719bSwdenk 		 * Find separator, or string end
1380c609719bSwdenk 		 * Allow simple escape of ';' by writing "\;"
1381c609719bSwdenk 		 */
1382a25f862bSwdenk 		for (inquotes = 0, sep = str; *sep; sep++) {
1383a25f862bSwdenk 			if ((*sep=='\'') &&
1384a25f862bSwdenk 			    (*(sep-1) != '\\'))
1385a25f862bSwdenk 				inquotes=!inquotes;
1386a25f862bSwdenk 
1387a25f862bSwdenk 			if (!inquotes &&
1388a25f862bSwdenk 			    (*sep == ';') &&	/* separator		*/
1389c609719bSwdenk 			    ( sep != str) &&	/* past string start	*/
1390c609719bSwdenk 			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
1391c609719bSwdenk 				break;
1392c609719bSwdenk 		}
1393c609719bSwdenk 
1394c609719bSwdenk 		/*
1395c609719bSwdenk 		 * Limit the token to data between separators
1396c609719bSwdenk 		 */
1397c609719bSwdenk 		token = str;
1398c609719bSwdenk 		if (*sep) {
1399c609719bSwdenk 			str = sep + 1;	/* start of command for next pass */
1400c609719bSwdenk 			*sep = '\0';
1401c609719bSwdenk 		}
1402c609719bSwdenk 		else
1403c609719bSwdenk 			str = sep;	/* no more commands for next pass */
14043e408873SSimon Glass 		debug_parser("token: \"%s\"\n", token);
1405c609719bSwdenk 
1406c609719bSwdenk 		/* find macros in this token and replace them */
1407c609719bSwdenk 		process_macros (token, finaltoken);
1408c609719bSwdenk 
1409c609719bSwdenk 		/* Extract arguments */
14101264b405SWolfgang Denk 		if ((argc = parse_line (finaltoken, argv)) == 0) {
14111264b405SWolfgang Denk 			rc = -1;	/* no command at all */
14121264b405SWolfgang Denk 			continue;
14131264b405SWolfgang Denk 		}
1414c609719bSwdenk 
141534765e88SRichard Genoud 		if (cmd_process(flag, argc, argv, &repeatable, NULL))
1416030fca52STimo Ketola 			rc = -1;
1417c609719bSwdenk 
1418c609719bSwdenk 		/* Did the user stop this? */
1419c609719bSwdenk 		if (had_ctrlc ())
14205afb2020SDetlev Zundel 			return -1;	/* if stopped then not repeatable */
1421c609719bSwdenk 	}
1422c609719bSwdenk 
1423f07771ccSwdenk 	return rc ? rc : repeatable;
1424c609719bSwdenk }
14257fed89e0SSimon Glass #endif
1426c609719bSwdenk 
142753071532SSimon Glass /*
142853071532SSimon Glass  * Run a command using the selected parser.
142953071532SSimon Glass  *
143053071532SSimon Glass  * @param cmd	Command to run
143153071532SSimon Glass  * @param flag	Execution flags (CMD_FLAG_...)
143253071532SSimon Glass  * @return 0 on success, or != 0 on error.
143353071532SSimon Glass  */
143453071532SSimon Glass int run_command(const char *cmd, int flag)
143553071532SSimon Glass {
143653071532SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
143753071532SSimon Glass 	/*
143853071532SSimon Glass 	 * builtin_run_command can return 0 or 1 for success, so clean up
143953071532SSimon Glass 	 * its result.
144053071532SSimon Glass 	 */
144153071532SSimon Glass 	if (builtin_run_command(cmd, flag) == -1)
144253071532SSimon Glass 		return 1;
144353071532SSimon Glass 
144453071532SSimon Glass 	return 0;
144553071532SSimon Glass #else
144653071532SSimon Glass 	return parse_string_outer(cmd,
144753071532SSimon Glass 			FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
144853071532SSimon Glass #endif
144953071532SSimon Glass }
145053071532SSimon Glass 
1451d51004a8SSimon Glass #ifndef CONFIG_SYS_HUSH_PARSER
1452d51004a8SSimon Glass /**
1453d51004a8SSimon Glass  * Execute a list of command separated by ; or \n using the built-in parser.
1454d51004a8SSimon Glass  *
1455d51004a8SSimon Glass  * This function cannot take a const char * for the command, since if it
1456d51004a8SSimon Glass  * finds newlines in the string, it replaces them with \0.
1457d51004a8SSimon Glass  *
1458d51004a8SSimon Glass  * @param cmd	String containing list of commands
1459d51004a8SSimon Glass  * @param flag	Execution flags (CMD_FLAG_...)
1460d51004a8SSimon Glass  * @return 0 on success, or != 0 on error.
1461d51004a8SSimon Glass  */
1462d51004a8SSimon Glass static int builtin_run_command_list(char *cmd, int flag)
1463d51004a8SSimon Glass {
1464d51004a8SSimon Glass 	char *line, *next;
1465d51004a8SSimon Glass 	int rcode = 0;
1466d51004a8SSimon Glass 
1467d51004a8SSimon Glass 	/*
1468d51004a8SSimon Glass 	 * Break into individual lines, and execute each line; terminate on
1469d51004a8SSimon Glass 	 * error.
1470d51004a8SSimon Glass 	 */
1471d51004a8SSimon Glass 	line = next = cmd;
1472d51004a8SSimon Glass 	while (*next) {
1473d51004a8SSimon Glass 		if (*next == '\n') {
1474d51004a8SSimon Glass 			*next = '\0';
1475d51004a8SSimon Glass 			/* run only non-empty commands */
1476d51004a8SSimon Glass 			if (*line) {
1477d51004a8SSimon Glass 				debug("** exec: \"%s\"\n", line);
1478d51004a8SSimon Glass 				if (builtin_run_command(line, 0) < 0) {
1479d51004a8SSimon Glass 					rcode = 1;
1480d51004a8SSimon Glass 					break;
1481d51004a8SSimon Glass 				}
1482d51004a8SSimon Glass 			}
1483d51004a8SSimon Glass 			line = next + 1;
1484d51004a8SSimon Glass 		}
1485d51004a8SSimon Glass 		++next;
1486d51004a8SSimon Glass 	}
1487d51004a8SSimon Glass 	if (rcode == 0 && *line)
1488d51004a8SSimon Glass 		rcode = (builtin_run_command(line, 0) >= 0);
1489d51004a8SSimon Glass 
1490d51004a8SSimon Glass 	return rcode;
1491d51004a8SSimon Glass }
1492d51004a8SSimon Glass #endif
1493d51004a8SSimon Glass 
1494d51004a8SSimon Glass int run_command_list(const char *cmd, int len, int flag)
1495d51004a8SSimon Glass {
1496d51004a8SSimon Glass 	int need_buff = 1;
1497d51004a8SSimon Glass 	char *buff = (char *)cmd;	/* cast away const */
1498d51004a8SSimon Glass 	int rcode = 0;
1499d51004a8SSimon Glass 
1500d51004a8SSimon Glass 	if (len == -1) {
1501d51004a8SSimon Glass 		len = strlen(cmd);
1502d51004a8SSimon Glass #ifdef CONFIG_SYS_HUSH_PARSER
1503d51004a8SSimon Glass 		/* hush will never change our string */
1504d51004a8SSimon Glass 		need_buff = 0;
1505d51004a8SSimon Glass #else
1506d51004a8SSimon Glass 		/* the built-in parser will change our string if it sees \n */
1507d51004a8SSimon Glass 		need_buff = strchr(cmd, '\n') != NULL;
1508d51004a8SSimon Glass #endif
1509d51004a8SSimon Glass 	}
1510d51004a8SSimon Glass 	if (need_buff) {
1511d51004a8SSimon Glass 		buff = malloc(len + 1);
1512d51004a8SSimon Glass 		if (!buff)
1513d51004a8SSimon Glass 			return 1;
1514d51004a8SSimon Glass 		memcpy(buff, cmd, len);
1515d51004a8SSimon Glass 		buff[len] = '\0';
1516d51004a8SSimon Glass 	}
1517d51004a8SSimon Glass #ifdef CONFIG_SYS_HUSH_PARSER
1518d51004a8SSimon Glass 	rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1519d51004a8SSimon Glass #else
1520d51004a8SSimon Glass 	/*
1521d51004a8SSimon Glass 	 * This function will overwrite any \n it sees with a \0, which
1522d51004a8SSimon Glass 	 * is why it can't work with a const char *. Here we are making
1523d51004a8SSimon Glass 	 * using of internal knowledge of this function, to avoid always
1524d51004a8SSimon Glass 	 * doing a malloc() which is actually required only in a case that
1525d51004a8SSimon Glass 	 * is pretty rare.
1526d51004a8SSimon Glass 	 */
1527d51004a8SSimon Glass 	rcode = builtin_run_command_list(buff, flag);
1528d51004a8SSimon Glass 	if (need_buff)
1529d51004a8SSimon Glass 		free(buff);
1530d51004a8SSimon Glass #endif
1531d51004a8SSimon Glass 
1532d51004a8SSimon Glass 	return rcode;
1533d51004a8SSimon Glass }
1534d51004a8SSimon Glass 
1535c609719bSwdenk /****************************************************************************/
1536c609719bSwdenk 
1537c3517f91SJon Loeliger #if defined(CONFIG_CMD_RUN)
153854841ab5SWolfgang Denk int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1539c609719bSwdenk {
1540c609719bSwdenk 	int i;
1541c609719bSwdenk 
154247e26b1bSWolfgang Denk 	if (argc < 2)
15434c12eeb8SSimon Glass 		return CMD_RET_USAGE;
1544c609719bSwdenk 
1545c609719bSwdenk 	for (i=1; i<argc; ++i) {
15463e38691eSwdenk 		char *arg;
15473e38691eSwdenk 
15483e38691eSwdenk 		if ((arg = getenv (argv[i])) == NULL) {
15493e38691eSwdenk 			printf ("## Error: \"%s\" not defined\n", argv[i]);
15503e38691eSwdenk 			return 1;
15513e38691eSwdenk 		}
1552c8a2079eSJason Hobbs 
15531992dbfdSSimon Glass 		if (run_command_list(arg, -1, flag) != 0)
15543e38691eSwdenk 			return 1;
1555c609719bSwdenk 	}
15563e38691eSwdenk 	return 0;
1557c609719bSwdenk }
155890253178SJon Loeliger #endif
1559