blob: ae37fee46d984370a2ae994432ccaf52333acf33 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk501090a2006-07-21 11:33:45 +02005 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +000010 */
11
wdenka6c7ad22002-12-03 21:28:10 +000012/* #define DEBUG */
13
wdenkc6097192002-11-03 00:24:07 +000014#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <command.h>
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000016#include <fdtdec.h>
wdenkc6097192002-11-03 00:24:07 +000017#include <hush.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000018#include <malloc.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000019#include <menu.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000020#include <post.h>
21#include <version.h>
22#include <watchdog.h>
23#include <linux/ctype.h>
wdenkbdccc4f2003-08-05 17:43:17 +000024
Wolfgang Denkd87080b2006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denkd87080b2006-03-31 18:32:53 +020026
Heiko Schocherfad63402007-07-13 09:54:17 +020027/*
28 * Board-specific Platform code can reimplement show_boot_progress () if needed
29 */
30void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050031void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020032
wdenkc6097192002-11-03 00:24:07 +000033#define MAX_DELAY_STOP_STR 32
34
Simon Glass3e408872013-05-15 06:24:00 +000035#define DEBUG_PARSER 0 /* set to 1 to debug */
36
37#define debug_parser(fmt, args...) \
38 debug_cond(DEBUG_PARSER, fmt, ##args)
39
Simon Glassd34d1862013-05-15 06:24:01 +000040#ifndef DEBUG_BOOTKEYS
41#define DEBUG_BOOTKEYS 0
42#endif
43#define debug_bootkeys(fmt, args...) \
44 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
wdenkc6097192002-11-03 00:24:07 +000045
John Schmoller6475b9f2010-03-12 09:49:23 -060046char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000047
Stefan Roese3ca91222006-07-27 16:11:19 +020048static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050049static const char erase_seq[] = "\b \b"; /* erase sequence */
50static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000051
52#ifdef CONFIG_BOOT_RETRY_TIME
53static uint64_t endtime = 0; /* must be set, default is instant timeout */
54static int retry_time = -1; /* -1 so can call readline before main_loop */
55#endif
56
57#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
58
59#ifndef CONFIG_BOOT_RETRY_MIN
60#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
61#endif
62
63#ifdef CONFIG_MODEM_SUPPORT
64int do_mdm_init = 0;
65extern void mdm_init(void); /* defined in board.c */
66#endif
67
68/***************************************************************************
69 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000070 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000071 */
Joe Hershbergerd5145442013-02-08 10:28:00 +000072#if defined(CONFIG_BOOTDELAY)
wdenkc6097192002-11-03 00:24:07 +000073# if defined(CONFIG_AUTOBOOT_KEYED)
Simon Glass063ae002013-05-15 06:23:55 +000074static int abortboot_keyed(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000075{
76 int abort = 0;
77 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +020078 struct {
wdenkc6097192002-11-03 00:24:07 +000079 char* str;
80 u_int len;
81 int retry;
82 }
Wolfgang Denk19973b62006-10-28 00:38:39 +020083 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +000084 { str: getenv ("bootdelaykey"), retry: 1 },
85 { str: getenv ("bootdelaykey2"), retry: 1 },
86 { str: getenv ("bootstopkey"), retry: 0 },
87 { str: getenv ("bootstopkey2"), retry: 0 },
88 };
89
90 char presskey [MAX_DELAY_STOP_STR];
91 u_int presskey_len = 0;
92 u_int presskey_max = 0;
93 u_int i;
94
Dirk Eibacha5aae0a2012-04-26 01:49:33 +000095#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
96 if (bootdelay == 0)
97 return 0;
98#endif
99
wdenkc6097192002-11-03 00:24:07 +0000100# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200101 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000102# endif
103
104# ifdef CONFIG_AUTOBOOT_DELAY_STR
105 if (delaykey[0].str == NULL)
106 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
107# endif
108# ifdef CONFIG_AUTOBOOT_DELAY_STR2
109 if (delaykey[1].str == NULL)
110 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
111# endif
112# ifdef CONFIG_AUTOBOOT_STOP_STR
113 if (delaykey[2].str == NULL)
114 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
115# endif
116# ifdef CONFIG_AUTOBOOT_STOP_STR2
117 if (delaykey[3].str == NULL)
118 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
119# endif
120
121 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
122 delaykey[i].len = delaykey[i].str == NULL ?
123 0 : strlen (delaykey[i].str);
124 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
125 MAX_DELAY_STOP_STR : delaykey[i].len;
126
127 presskey_max = presskey_max > delaykey[i].len ?
128 presskey_max : delaykey[i].len;
129
Simon Glassd34d1862013-05-15 06:24:01 +0000130 debug_bootkeys("%s key:<%s>\n",
131 delaykey[i].retry ? "delay" : "stop",
132 delaykey[i].str ? delaykey[i].str : "NULL");
wdenkc6097192002-11-03 00:24:07 +0000133 }
134
135 /* In order to keep up with incoming data, check timeout only
136 * when catch up.
137 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100138 do {
139 if (tstc()) {
140 if (presskey_len < presskey_max) {
141 presskey [presskey_len ++] = getc();
142 }
143 else {
144 for (i = 0; i < presskey_max - 1; i ++)
145 presskey [i] = presskey [i + 1];
146
147 presskey [i] = getc();
148 }
149 }
150
wdenkc6097192002-11-03 00:24:07 +0000151 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
152 if (delaykey[i].len > 0 &&
153 presskey_len >= delaykey[i].len &&
154 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000155 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000156 delaykey[i].len) == 0) {
Simon Glassd34d1862013-05-15 06:24:01 +0000157 debug_bootkeys("got %skey\n",
158 delaykey[i].retry ? "delay" :
159 "stop");
wdenkc6097192002-11-03 00:24:07 +0000160
161# ifdef CONFIG_BOOT_RETRY_TIME
162 /* don't retry auto boot */
163 if (! delaykey[i].retry)
164 retry_time = -1;
165# endif
166 abort = 1;
167 }
168 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100169 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000170
wdenkc6097192002-11-03 00:24:07 +0000171 if (!abort)
Simon Glassd34d1862013-05-15 06:24:01 +0000172 debug_bootkeys("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000173
dzu8cb81432003-10-24 13:14:45 +0000174#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200175 if (abort)
176 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000177#endif
178
wdenkc6097192002-11-03 00:24:07 +0000179 return abort;
180}
181
182# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
183
wdenkc7de8292002-11-19 11:04:11 +0000184#ifdef CONFIG_MENUKEY
185static int menukey = 0;
186#endif
187
Simon Glass063ae002013-05-15 06:23:55 +0000188static int abortboot_normal(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000189{
190 int abort = 0;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000191 unsigned long ts;
wdenkc6097192002-11-03 00:24:07 +0000192
wdenkc7de8292002-11-19 11:04:11 +0000193#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200194 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000195#else
Joe Hershberger93d72122012-08-17 10:53:12 +0000196 if (bootdelay >= 0)
197 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000198#endif
wdenkc6097192002-11-03 00:24:07 +0000199
200#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000201 /*
202 * Check if key already pressed
203 * Don't check if bootdelay < 0
204 */
wdenkc6097192002-11-03 00:24:07 +0000205 if (bootdelay >= 0) {
206 if (tstc()) { /* we got a key press */
207 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000208 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200209 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000210 }
wdenk8bde7f72003-06-27 21:31:46 +0000211 }
wdenkc6097192002-11-03 00:24:07 +0000212#endif
213
wdenkf72da342003-10-10 10:05:42 +0000214 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000215 --bootdelay;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000216 /* delay 1000 ms */
217 ts = get_timer(0);
218 do {
wdenkc6097192002-11-03 00:24:07 +0000219 if (tstc()) { /* we got a key press */
220 abort = 1; /* don't auto boot */
221 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000222# ifdef CONFIG_MENUKEY
223 menukey = getc();
224# else
wdenkc6097192002-11-03 00:24:07 +0000225 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000226# endif
wdenkc6097192002-11-03 00:24:07 +0000227 break;
228 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200229 udelay(10000);
Jim Linb2f3e0e2013-01-24 01:05:55 +0000230 } while (!abort && get_timer(ts) < 1000);
wdenkc6097192002-11-03 00:24:07 +0000231
Ladislav Michlada4d402007-04-25 16:01:26 +0200232 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000233 }
234
Ladislav Michlada4d402007-04-25 16:01:26 +0200235 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000236
wdenkf72da342003-10-10 10:05:42 +0000237#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200238 if (abort)
239 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000240#endif
241
wdenkc6097192002-11-03 00:24:07 +0000242 return abort;
243}
244# endif /* CONFIG_AUTOBOOT_KEYED */
Simon Glass063ae002013-05-15 06:23:55 +0000245
246static int abortboot(int bootdelay)
247{
248#ifdef CONFIG_AUTOBOOT_KEYED
249 return abortboot_keyed(bootdelay);
250#else
251 return abortboot_normal(bootdelay);
252#endif
253}
Joe Hershbergerd5145442013-02-08 10:28:00 +0000254#endif /* CONFIG_BOOTDELAY */
wdenkc6097192002-11-03 00:24:07 +0000255
Doug Anderson67e1ea22012-10-25 16:31:09 +0000256/*
257 * Runs the given boot command securely. Specifically:
258 * - Doesn't run the command with the shell (run_command or parse_string_outer),
259 * since that's a lot of code surface that an attacker might exploit.
260 * Because of this, we don't do any argument parsing--the secure boot command
261 * has to be a full-fledged u-boot command.
262 * - Doesn't check for keypresses before booting, since that could be a
263 * security hole; also disables Ctrl-C.
264 * - Doesn't allow the command to return.
265 *
266 * Upon any failures, this function will drop into an infinite loop after
267 * printing the error message to console.
268 */
269
Joe Hershbergerd5145442013-02-08 10:28:00 +0000270#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
Doug Anderson67e1ea22012-10-25 16:31:09 +0000271static void secure_boot_cmd(char *cmd)
272{
273 cmd_tbl_t *cmdtp;
274 int rc;
275
276 if (!cmd) {
277 printf("## Error: Secure boot command not specified\n");
278 goto err;
279 }
280
281 /* Disable Ctrl-C just in case some command is used that checks it. */
282 disable_ctrlc(1);
283
284 /* Find the command directly. */
285 cmdtp = find_cmd(cmd);
286 if (!cmdtp) {
287 printf("## Error: \"%s\" not defined\n", cmd);
288 goto err;
289 }
290
291 /* Run the command, forcing no flags and faking argc and argv. */
292 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
293
294 /* Shouldn't ever return from boot command. */
295 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
296
297err:
298 /*
299 * Not a whole lot to do here. Rebooting won't help much, since we'll
300 * just end up right back here. Just loop.
301 */
302 hang();
303}
304
Simon Glassfcabc242012-10-25 16:31:11 +0000305static void process_fdt_options(const void *blob)
306{
307 ulong addr;
308
309 /* Add an env variable to point to a kernel payload, if available */
310 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
311 if (addr)
312 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
313
314 /* Add an env variable to point to a root disk, if available */
315 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
316 if (addr)
317 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
318}
Doug Anderson67e1ea22012-10-25 16:31:09 +0000319#endif /* CONFIG_OF_CONTROL */
320
Simon Glassbc2b4c22013-05-15 06:23:56 +0000321#ifdef CONFIG_BOOTDELAY
322static void process_boot_delay(void)
wdenkc6097192002-11-03 00:24:07 +0000323{
Simon Glassbc2b4c22013-05-15 06:23:56 +0000324#ifdef CONFIG_OF_CONTROL
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000325 char *env;
326#endif
wdenkc6097192002-11-03 00:24:07 +0000327 char *s;
328 int bootdelay;
wdenkbdccc4f2003-08-05 17:43:17 +0000329#ifdef CONFIG_BOOTCOUNT_LIMIT
330 unsigned long bootcount = 0;
331 unsigned long bootlimit = 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000332#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000333
wdenkbdccc4f2003-08-05 17:43:17 +0000334#ifdef CONFIG_BOOTCOUNT_LIMIT
335 bootcount = bootcount_load();
336 bootcount++;
337 bootcount_store (bootcount);
Simon Glassf2abca82013-05-15 06:23:57 +0000338 setenv_ulong("bootcount", bootcount);
339 bootlimit = getenv_ulong("bootlimit", 10, 0);
wdenkbdccc4f2003-08-05 17:43:17 +0000340#endif /* CONFIG_BOOTCOUNT_LIMIT */
341
wdenkc6097192002-11-03 00:24:07 +0000342 s = getenv ("bootdelay");
343 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
344
Stephen Warren99bd5442013-05-14 08:02:56 +0000345#ifdef CONFIG_OF_CONTROL
346 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
347 bootdelay);
348#endif
349
wdenka6c7ad22002-12-03 21:28:10 +0000350 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000351
Heiko Schocher317d6c52012-01-16 21:13:35 +0000352#if defined(CONFIG_MENU_SHOW)
353 bootdelay = menu_show(bootdelay);
354#endif
wdenkc6097192002-11-03 00:24:07 +0000355# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000356 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000357# endif /* CONFIG_BOOT_RETRY_TIME */
358
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100359#ifdef CONFIG_POST
360 if (gd->flags & GD_FLG_POSTFAIL) {
361 s = getenv("failbootcmd");
362 }
363 else
364#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000365#ifdef CONFIG_BOOTCOUNT_LIMIT
366 if (bootlimit && (bootcount > bootlimit)) {
367 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
368 (unsigned)bootlimit);
369 s = getenv ("altbootcmd");
370 }
371 else
372#endif /* CONFIG_BOOTCOUNT_LIMIT */
373 s = getenv ("bootcmd");
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000374#ifdef CONFIG_OF_CONTROL
375 /* Allow the fdt to override the boot command */
376 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
377 if (env)
378 s = env;
Doug Anderson67e1ea22012-10-25 16:31:09 +0000379
Simon Glassfcabc242012-10-25 16:31:11 +0000380 process_fdt_options(gd->fdt_blob);
381
Doug Anderson67e1ea22012-10-25 16:31:09 +0000382 /*
383 * If the bootsecure option was chosen, use secure_boot_cmd().
384 * Always use 'env' in this case, since bootsecure requres that the
385 * bootcmd was specified in the FDT too.
386 */
387 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
388 secure_boot_cmd(env);
389
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000390#endif /* CONFIG_OF_CONTROL */
wdenka6c7ad22002-12-03 21:28:10 +0000391
392 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
393
Joe Hershberger93d72122012-08-17 10:53:12 +0000394 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
Simon Glassbc2b4c22013-05-15 06:23:56 +0000395#ifdef CONFIG_AUTOBOOT_KEYED
wdenkc6097192002-11-03 00:24:07 +0000396 int prev = disable_ctrlc(1); /* disable Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000397#endif
wdenkc6097192002-11-03 00:24:07 +0000398
Simon Glass3a8a02b2012-03-30 21:30:56 +0000399 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000400
Simon Glassbc2b4c22013-05-15 06:23:56 +0000401#ifdef CONFIG_AUTOBOOT_KEYED
wdenkc6097192002-11-03 00:24:07 +0000402 disable_ctrlc(prev); /* restore Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000403#endif
wdenkc6097192002-11-03 00:24:07 +0000404 }
wdenkc7de8292002-11-19 11:04:11 +0000405
Simon Glassbc2b4c22013-05-15 06:23:56 +0000406#ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000407 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000408 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000409 if (s)
Simon Glass3a8a02b2012-03-30 21:30:56 +0000410 run_command_list(s, -1, 0);
wdenkc7de8292002-11-19 11:04:11 +0000411 }
412#endif /* CONFIG_MENUKEY */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000413}
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200414#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000415
Simon Glassbc2b4c22013-05-15 06:23:56 +0000416void main_loop(void)
417{
418#ifndef CONFIG_SYS_HUSH_PARSER
419 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
420 int len;
421 int rc = 1;
422 int flag;
423#endif
424#ifdef CONFIG_PREBOOT
425 char *p;
426#endif
427
428 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
429
430#ifdef CONFIG_MODEM_SUPPORT
431 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
432 if (do_mdm_init) {
433 char *str = strdup(getenv("mdm_cmd"));
434 setenv("preboot", str); /* set or delete definition */
435 if (str != NULL)
436 free(str);
437 mdm_init(); /* wait for modem connection */
438 }
439#endif /* CONFIG_MODEM_SUPPORT */
440
441#ifdef CONFIG_VERSION_VARIABLE
442 {
443 setenv("ver", version_string); /* set version variable */
444 }
445#endif /* CONFIG_VERSION_VARIABLE */
446
447#ifdef CONFIG_SYS_HUSH_PARSER
448 u_boot_hush_start();
449#endif
450
451#if defined(CONFIG_HUSH_INIT_VAR)
452 hush_init_var();
453#endif
454
455#ifdef CONFIG_PREBOOT
456 p = getenv("preboot");
457 if (p != NULL) {
458# ifdef CONFIG_AUTOBOOT_KEYED
459 int prev = disable_ctrlc(1); /* disable Control C checking */
460# endif
461
462 run_command_list(p, -1, 0);
463
464# ifdef CONFIG_AUTOBOOT_KEYED
465 disable_ctrlc(prev); /* restore Control C checking */
466# endif
467 }
468#endif /* CONFIG_PREBOOT */
469
470#if defined(CONFIG_UPDATE_TFTP)
471 update_tftp(0UL);
472#endif /* CONFIG_UPDATE_TFTP */
473
474#ifdef CONFIG_BOOTDELAY
475 process_boot_delay();
476#endif
wdenkc6097192002-11-03 00:24:07 +0000477 /*
478 * Main Loop for Monitor Command Processing
479 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200480#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000481 parse_file_outer();
482 /* This point is never reached */
483 for (;;);
484#else
485 for (;;) {
486#ifdef CONFIG_BOOT_RETRY_TIME
487 if (rc >= 0) {
488 /* Saw enough of a valid command to
489 * restart the timeout.
490 */
491 reset_cmd_timeout();
492 }
493#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200494 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000495
496 flag = 0; /* assume no special flags for now */
497 if (len > 0)
498 strcpy (lastcommand, console_buffer);
499 else if (len == 0)
500 flag |= CMD_FLAG_REPEAT;
501#ifdef CONFIG_BOOT_RETRY_TIME
502 else if (len == -2) {
503 /* -2 means timed out, retry autoboot
504 */
wdenk4b9206e2004-03-23 22:14:11 +0000505 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000506# ifdef CONFIG_RESET_TO_RETRY
507 /* Reinit board to run initialization code again */
508 do_reset (NULL, 0, 0, NULL);
509# else
510 return; /* retry autoboot */
511# endif
512 }
513#endif
514
515 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000516 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000517 else
Simon Glass53071532012-02-14 19:59:21 +0000518 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000519
520 if (rc <= 0) {
521 /* invalid command or not repeatable, forget it */
522 lastcommand[0] = 0;
523 }
524 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200525#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000526}
527
wdenk6dd652f2003-06-19 23:40:20 +0000528#ifdef CONFIG_BOOT_RETRY_TIME
529/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200530 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000531 */
532void init_cmd_timeout(void)
533{
534 char *s = getenv ("bootretry");
535
536 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000537 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000538 else
539 retry_time = CONFIG_BOOT_RETRY_TIME;
540
541 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
542 retry_time = CONFIG_BOOT_RETRY_MIN;
543}
544
wdenkc6097192002-11-03 00:24:07 +0000545/***************************************************************************
546 * reset command line timeout to retry_time seconds
547 */
wdenkc6097192002-11-03 00:24:07 +0000548void reset_cmd_timeout(void)
549{
550 endtime = endtick(retry_time);
551}
552#endif
553
Wolfgang Denk501090a2006-07-21 11:33:45 +0200554#ifdef CONFIG_CMDLINE_EDITING
555
556/*
557 * cmdline-editing related codes from vivi.
558 * Author: Janghoon Lyu <nandy@mizi.com>
559 */
560
Wolfgang Denk501090a2006-07-21 11:33:45 +0200561#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700562 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200563 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200564
565#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200566#define CTL_BACKSPACE ('\b')
567#define DEL ((char)255)
568#define DEL7 ((char)127)
569#define CREAD_HIST_CHAR ('!')
570
571#define getcmd_putch(ch) putc(ch)
572#define getcmd_getch() getc()
573#define getcmd_cbeep() getcmd_putch('\a')
574
575#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500576#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200577
Kim Phillips199adb62012-10-29 13:34:32 +0000578static int hist_max;
579static int hist_add_idx;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200580static int hist_cur = -1;
Kim Phillips199adb62012-10-29 13:34:32 +0000581static unsigned hist_num;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200582
Kim Phillips199adb62012-10-29 13:34:32 +0000583static char *hist_list[HIST_MAX];
584static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200585
586#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
587
588static void hist_init(void)
589{
590 int i;
591
592 hist_max = 0;
593 hist_add_idx = 0;
594 hist_cur = -1;
595 hist_num = 0;
596
597 for (i = 0; i < HIST_MAX; i++) {
598 hist_list[i] = hist_lines[i];
599 hist_list[i][0] = '\0';
600 }
601}
602
603static void cread_add_to_hist(char *line)
604{
605 strcpy(hist_list[hist_add_idx], line);
606
607 if (++hist_add_idx >= HIST_MAX)
608 hist_add_idx = 0;
609
610 if (hist_add_idx > hist_max)
611 hist_max = hist_add_idx;
612
613 hist_num++;
614}
615
616static char* hist_prev(void)
617{
618 char *ret;
619 int old_cur;
620
621 if (hist_cur < 0)
622 return NULL;
623
624 old_cur = hist_cur;
625 if (--hist_cur < 0)
626 hist_cur = hist_max;
627
628 if (hist_cur == hist_add_idx) {
629 hist_cur = old_cur;
630 ret = NULL;
631 } else
632 ret = hist_list[hist_cur];
633
634 return (ret);
635}
636
637static char* hist_next(void)
638{
639 char *ret;
640
641 if (hist_cur < 0)
642 return NULL;
643
644 if (hist_cur == hist_add_idx)
645 return NULL;
646
647 if (++hist_cur > hist_max)
648 hist_cur = 0;
649
650 if (hist_cur == hist_add_idx) {
651 ret = "";
652 } else
653 ret = hist_list[hist_cur];
654
655 return (ret);
656}
657
Stefan Roese3ca91222006-07-27 16:11:19 +0200658#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200659static void cread_print_hist_list(void)
660{
661 int i;
662 unsigned long n;
663
664 n = hist_num - hist_max;
665
666 i = hist_add_idx + 1;
667 while (1) {
668 if (i > hist_max)
669 i = 0;
670 if (i == hist_add_idx)
671 break;
672 printf("%s\n", hist_list[i]);
673 n++;
674 i++;
675 }
676}
Stefan Roese3ca91222006-07-27 16:11:19 +0200677#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200678
679#define BEGINNING_OF_LINE() { \
680 while (num) { \
681 getcmd_putch(CTL_BACKSPACE); \
682 num--; \
683 } \
684}
685
686#define ERASE_TO_EOL() { \
687 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400688 printf("%*s", (int)(eol_num - num), ""); \
689 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200690 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400691 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200692 } \
693}
694
695#define REFRESH_TO_EOL() { \
696 if (num < eol_num) { \
697 wlen = eol_num - num; \
698 putnstr(buf + num, wlen); \
699 num = eol_num; \
700 } \
701}
702
703static void cread_add_char(char ichar, int insert, unsigned long *num,
704 unsigned long *eol_num, char *buf, unsigned long len)
705{
706 unsigned long wlen;
707
708 /* room ??? */
709 if (insert || *num == *eol_num) {
710 if (*eol_num > len - 1) {
711 getcmd_cbeep();
712 return;
713 }
714 (*eol_num)++;
715 }
716
717 if (insert) {
718 wlen = *eol_num - *num;
719 if (wlen > 1) {
720 memmove(&buf[*num+1], &buf[*num], wlen-1);
721 }
722
723 buf[*num] = ichar;
724 putnstr(buf + *num, wlen);
725 (*num)++;
726 while (--wlen) {
727 getcmd_putch(CTL_BACKSPACE);
728 }
729 } else {
730 /* echo the character */
731 wlen = 1;
732 buf[*num] = ichar;
733 putnstr(buf + *num, wlen);
734 (*num)++;
735 }
736}
737
738static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
739 unsigned long *eol_num, char *buf, unsigned long len)
740{
741 while (strsize--) {
742 cread_add_char(*str, insert, num, eol_num, buf, len);
743 str++;
744 }
745}
746
Heiko Schocher9c348312012-01-16 21:13:05 +0000747static int cread_line(const char *const prompt, char *buf, unsigned int *len,
748 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200749{
750 unsigned long num = 0;
751 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200752 unsigned long wlen;
753 char ichar;
754 int insert = 1;
755 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200756 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500757 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000758 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500759
760 if (init_len)
761 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200762
763 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100764#ifdef CONFIG_BOOT_RETRY_TIME
765 while (!tstc()) { /* while no incoming data */
766 if (retry_time >= 0 && get_ticks() > endtime)
767 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200768 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100769 }
770#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000771 if (first && timeout) {
772 uint64_t etime = endtick(timeout);
773
774 while (!tstc()) { /* while no incoming data */
775 if (get_ticks() >= etime)
776 return -2; /* timed out */
777 WATCHDOG_RESET();
778 }
779 first = 0;
780 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100781
Wolfgang Denk501090a2006-07-21 11:33:45 +0200782 ichar = getcmd_getch();
783
784 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200785 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200786 break;
787 }
788
789 /*
790 * handle standard linux xterm esc sequences for arrow key, etc.
791 */
792 if (esc_len != 0) {
793 if (esc_len == 1) {
794 if (ichar == '[') {
795 esc_save[esc_len] = ichar;
796 esc_len = 2;
797 } else {
798 cread_add_str(esc_save, esc_len, insert,
799 &num, &eol_num, buf, *len);
800 esc_len = 0;
801 }
802 continue;
803 }
804
805 switch (ichar) {
806
807 case 'D': /* <- key */
808 ichar = CTL_CH('b');
809 esc_len = 0;
810 break;
811 case 'C': /* -> key */
812 ichar = CTL_CH('f');
813 esc_len = 0;
814 break; /* pass off to ^F handler */
815 case 'H': /* Home key */
816 ichar = CTL_CH('a');
817 esc_len = 0;
818 break; /* pass off to ^A handler */
819 case 'A': /* up arrow */
820 ichar = CTL_CH('p');
821 esc_len = 0;
822 break; /* pass off to ^P handler */
823 case 'B': /* down arrow */
824 ichar = CTL_CH('n');
825 esc_len = 0;
826 break; /* pass off to ^N handler */
827 default:
828 esc_save[esc_len++] = ichar;
829 cread_add_str(esc_save, esc_len, insert,
830 &num, &eol_num, buf, *len);
831 esc_len = 0;
832 continue;
833 }
834 }
835
836 switch (ichar) {
837 case 0x1b:
838 if (esc_len == 0) {
839 esc_save[esc_len] = ichar;
840 esc_len = 1;
841 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200842 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200843 esc_len = 0;
844 }
845 break;
846
847 case CTL_CH('a'):
848 BEGINNING_OF_LINE();
849 break;
850 case CTL_CH('c'): /* ^C - break */
851 *buf = '\0'; /* discard input */
852 return (-1);
853 case CTL_CH('f'):
854 if (num < eol_num) {
855 getcmd_putch(buf[num]);
856 num++;
857 }
858 break;
859 case CTL_CH('b'):
860 if (num) {
861 getcmd_putch(CTL_BACKSPACE);
862 num--;
863 }
864 break;
865 case CTL_CH('d'):
866 if (num < eol_num) {
867 wlen = eol_num - num - 1;
868 if (wlen) {
869 memmove(&buf[num], &buf[num+1], wlen);
870 putnstr(buf + num, wlen);
871 }
872
873 getcmd_putch(' ');
874 do {
875 getcmd_putch(CTL_BACKSPACE);
876 } while (wlen--);
877 eol_num--;
878 }
879 break;
880 case CTL_CH('k'):
881 ERASE_TO_EOL();
882 break;
883 case CTL_CH('e'):
884 REFRESH_TO_EOL();
885 break;
886 case CTL_CH('o'):
887 insert = !insert;
888 break;
889 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100890 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200891 BEGINNING_OF_LINE();
892 ERASE_TO_EOL();
893 break;
894 case DEL:
895 case DEL7:
896 case 8:
897 if (num) {
898 wlen = eol_num - num;
899 num--;
900 memmove(&buf[num], &buf[num+1], wlen);
901 getcmd_putch(CTL_BACKSPACE);
902 putnstr(buf + num, wlen);
903 getcmd_putch(' ');
904 do {
905 getcmd_putch(CTL_BACKSPACE);
906 } while (wlen--);
907 eol_num--;
908 }
909 break;
910 case CTL_CH('p'):
911 case CTL_CH('n'):
912 {
913 char * hline;
914
915 esc_len = 0;
916
917 if (ichar == CTL_CH('p'))
918 hline = hist_prev();
919 else
920 hline = hist_next();
921
922 if (!hline) {
923 getcmd_cbeep();
924 continue;
925 }
926
927 /* nuke the current line */
928 /* first, go home */
929 BEGINNING_OF_LINE();
930
931 /* erase to end of line */
932 ERASE_TO_EOL();
933
934 /* copy new line into place and display */
935 strcpy(buf, hline);
936 eol_num = strlen(buf);
937 REFRESH_TO_EOL();
938 continue;
939 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100940#ifdef CONFIG_AUTO_COMPLETE
941 case '\t': {
942 int num2, col;
943
944 /* do not autocomplete when in the middle */
945 if (num < eol_num) {
946 getcmd_cbeep();
947 break;
948 }
949
950 buf[num] = '\0';
951 col = strlen(prompt) + eol_num;
952 num2 = num;
953 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
954 col = num2 - num;
955 num += col;
956 eol_num += col;
957 }
958 break;
959 }
960#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200961 default:
962 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
963 break;
964 }
965 }
966 *len = eol_num;
967 buf[eol_num] = '\0'; /* lose the newline */
968
969 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
970 cread_add_to_hist(buf);
971 hist_cur = hist_add_idx;
972
Peter Tyserf9239432009-10-25 15:12:53 -0500973 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200974}
975
976#endif /* CONFIG_CMDLINE_EDITING */
977
wdenkc6097192002-11-03 00:24:07 +0000978/****************************************************************************/
979
980/*
981 * Prompt for input and read a line.
982 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
983 * time out when time goes past endtime (timebase time in ticks).
984 * Return: number of read characters
985 * -1 if break
986 * -2 if timed out
987 */
988int readline (const char *const prompt)
989{
Peter Tyserecc55002009-10-25 15:12:54 -0500990 /*
991 * If console_buffer isn't 0-length the user will be prompted to modify
992 * it instead of entering it from scratch as desired.
993 */
994 console_buffer[0] = '\0';
995
Heiko Schocher9c348312012-01-16 21:13:05 +0000996 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -0600997}
998
999
Heiko Schocher9c348312012-01-16 21:13:05 +00001000int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -06001001{
1002 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001003#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -05001004 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +02001005 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001006 static int initted = 0;
1007
James Yang597f6c22008-05-05 10:22:53 -05001008 /*
1009 * History uses a global array which is not
1010 * writable until after relocation to RAM.
1011 * Revert to non-history version if still
1012 * running from flash.
1013 */
1014 if (gd->flags & GD_FLG_RELOC) {
1015 if (!initted) {
1016 hist_init();
1017 initted = 1;
1018 }
1019
Peter Tysere491a712009-10-25 15:12:52 -05001020 if (prompt)
1021 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -05001022
Heiko Schocher9c348312012-01-16 21:13:05 +00001023 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -05001024 return rc < 0 ? rc : len;
1025
1026 } else {
1027#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -06001028 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +00001029 int n = 0; /* buffer index */
1030 int plen = 0; /* prompt length */
1031 int col; /* output column cnt */
1032 char c;
1033
1034 /* print prompt */
1035 if (prompt) {
1036 plen = strlen (prompt);
1037 puts (prompt);
1038 }
1039 col = plen;
1040
1041 for (;;) {
1042#ifdef CONFIG_BOOT_RETRY_TIME
1043 while (!tstc()) { /* while no incoming data */
1044 if (retry_time >= 0 && get_ticks() > endtime)
1045 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +02001046 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001047 }
1048#endif
1049 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1050
1051#ifdef CONFIG_SHOW_ACTIVITY
1052 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +00001053 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +02001054 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001055 }
1056#endif
1057 c = getc();
1058
1059 /*
1060 * Special character handling
1061 */
1062 switch (c) {
Simon Glass49333812013-05-15 06:23:58 +00001063 case '\r': /* Enter */
wdenkc6097192002-11-03 00:24:07 +00001064 case '\n':
1065 *p = '\0';
1066 puts ("\r\n");
Simon Glass49333812013-05-15 06:23:58 +00001067 return p - p_buf;
wdenkc6097192002-11-03 00:24:07 +00001068
Simon Glass49333812013-05-15 06:23:58 +00001069 case '\0': /* nul */
wdenk27aa8182004-03-23 22:37:33 +00001070 continue;
1071
Simon Glass49333812013-05-15 06:23:58 +00001072 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001073 p_buf[0] = '\0'; /* discard input */
Simon Glass49333812013-05-15 06:23:58 +00001074 return -1;
wdenkc6097192002-11-03 00:24:07 +00001075
Simon Glass49333812013-05-15 06:23:58 +00001076 case 0x15: /* ^U - erase line */
wdenkc6097192002-11-03 00:24:07 +00001077 while (col > plen) {
1078 puts (erase_seq);
1079 --col;
1080 }
James Yang6636b622008-01-09 11:17:49 -06001081 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001082 n = 0;
1083 continue;
1084
Simon Glass49333812013-05-15 06:23:58 +00001085 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001086 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001087 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001088 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001089 }
1090 continue;
1091
Simon Glass49333812013-05-15 06:23:58 +00001092 case 0x08: /* ^H - backspace */
1093 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001094 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001095 continue;
1096
1097 default:
1098 /*
1099 * Must be a normal character then
1100 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001101 if (n < CONFIG_SYS_CBSIZE-2) {
Simon Glass49333812013-05-15 06:23:58 +00001102 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001103#ifdef CONFIG_AUTO_COMPLETE
1104 /* if auto completion triggered just continue */
1105 *p = '\0';
1106 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001107 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001108 continue;
1109 }
1110#endif
wdenkc6097192002-11-03 00:24:07 +00001111 puts (tab_seq+(col&07));
1112 col += 8 - (col&07);
1113 } else {
Simon Glass9a8efc42012-10-30 13:40:18 +00001114 char buf[2];
1115
1116 /*
Simon Glass49333812013-05-15 06:23:58 +00001117 * Echo input using puts() to force an
Simon Glass9a8efc42012-10-30 13:40:18 +00001118 * LCD flush if we are using an LCD
1119 */
1120 ++col;
1121 buf[0] = c;
1122 buf[1] = '\0';
1123 puts(buf);
wdenkc6097192002-11-03 00:24:07 +00001124 }
1125 *p++ = c;
1126 ++n;
1127 } else { /* Buffer full */
1128 putc ('\a');
1129 }
1130 }
1131 }
James Yang597f6c22008-05-05 10:22:53 -05001132#ifdef CONFIG_CMDLINE_EDITING
1133 }
1134#endif
wdenkc6097192002-11-03 00:24:07 +00001135}
1136
1137/****************************************************************************/
1138
1139static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1140{
1141 char *s;
1142
1143 if (*np == 0) {
1144 return (p);
1145 }
1146
1147 if (*(--p) == '\t') { /* will retype the whole line */
1148 while (*colp > plen) {
1149 puts (erase_seq);
1150 (*colp)--;
1151 }
1152 for (s=buffer; s<p; ++s) {
1153 if (*s == '\t') {
1154 puts (tab_seq+((*colp) & 07));
1155 *colp += 8 - ((*colp) & 07);
1156 } else {
1157 ++(*colp);
1158 putc (*s);
1159 }
1160 }
1161 } else {
1162 puts (erase_seq);
1163 (*colp)--;
1164 }
1165 (*np)--;
1166 return (p);
1167}
1168
1169/****************************************************************************/
1170
1171int parse_line (char *line, char *argv[])
1172{
1173 int nargs = 0;
1174
Simon Glass3e408872013-05-15 06:24:00 +00001175 debug_parser("parse_line: \"%s\"\n", line);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001176 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001177
1178 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001179 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001180 ++line;
wdenkc6097192002-11-03 00:24:07 +00001181
1182 if (*line == '\0') { /* end of line, no more args */
1183 argv[nargs] = NULL;
Simon Glass3e408872013-05-15 06:24:00 +00001184 debug_parser("parse_line: nargs=%d\n", nargs);
1185 return nargs;
wdenkc6097192002-11-03 00:24:07 +00001186 }
1187
1188 argv[nargs++] = line; /* begin of argument string */
1189
1190 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001191 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001192 ++line;
wdenkc6097192002-11-03 00:24:07 +00001193
1194 if (*line == '\0') { /* end of line, no more args */
1195 argv[nargs] = NULL;
Simon Glass3e408872013-05-15 06:24:00 +00001196 debug_parser("parse_line: nargs=%d\n", nargs);
1197 return nargs;
wdenkc6097192002-11-03 00:24:07 +00001198 }
1199
1200 *line++ = '\0'; /* terminate current arg */
1201 }
1202
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001203 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001204
Simon Glass3e408872013-05-15 06:24:00 +00001205 debug_parser("parse_line: nargs=%d\n", nargs);
wdenkc6097192002-11-03 00:24:07 +00001206 return (nargs);
1207}
1208
1209/****************************************************************************/
1210
Simon Glass7fed89e2012-02-14 19:59:22 +00001211#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001212static void process_macros (const char *input, char *output)
1213{
1214 char c, prev;
1215 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001216 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001217 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001218 int state = 0; /* 0 = waiting for '$' */
1219
1220 /* 1 = waiting for '(' or '{' */
1221 /* 2 = waiting for ')' or '}' */
1222 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001223 char *output_start = output;
1224
Simon Glass3e408872013-05-15 06:24:00 +00001225 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
1226 input);
wdenkc6097192002-11-03 00:24:07 +00001227
Wolfgang Denk19973b62006-10-28 00:38:39 +02001228 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001229
1230 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001231 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001232 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001233
Wolfgang Denk19973b62006-10-28 00:38:39 +02001234 if (state != 3) {
1235 /* remove one level of escape characters */
1236 if ((c == '\\') && (prev != '\\')) {
1237 if (inputcnt-- == 0)
1238 break;
1239 prev = c;
1240 c = *input++;
1241 }
wdenka25f8622003-01-02 23:57:29 +00001242 }
wdenkc6097192002-11-03 00:24:07 +00001243
Wolfgang Denk19973b62006-10-28 00:38:39 +02001244 switch (state) {
1245 case 0: /* Waiting for (unescaped) $ */
1246 if ((c == '\'') && (prev != '\\')) {
1247 state = 3;
1248 break;
1249 }
1250 if ((c == '$') && (prev != '\\')) {
1251 state++;
1252 } else {
wdenkc6097192002-11-03 00:24:07 +00001253 *(output++) = c;
1254 outputcnt--;
1255 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001256 break;
1257 case 1: /* Waiting for ( */
1258 if (c == '(' || c == '{') {
1259 state++;
1260 varname_start = input;
1261 } else {
1262 state = 0;
1263 *(output++) = '$';
1264 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001265
Wolfgang Denk19973b62006-10-28 00:38:39 +02001266 if (outputcnt) {
1267 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001268 outputcnt--;
1269 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001270 }
1271 break;
1272 case 2: /* Waiting for ) */
1273 if (c == ')' || c == '}') {
1274 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001275 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001276 int envcnt = input - varname_start - 1; /* Varname # of chars */
1277
1278 /* Get the varname */
1279 for (i = 0; i < envcnt; i++) {
1280 envname[i] = varname_start[i];
1281 }
1282 envname[i] = 0;
1283
1284 /* Get its value */
1285 envval = getenv (envname);
1286
1287 /* Copy into the line if it exists */
1288 if (envval != NULL)
1289 while ((*envval) && outputcnt) {
1290 *(output++) = *(envval++);
1291 outputcnt--;
1292 }
1293 /* Look for another '$' */
1294 state = 0;
1295 }
1296 break;
1297 case 3: /* Waiting for ' */
1298 if ((c == '\'') && (prev != '\\')) {
1299 state = 0;
1300 } else {
1301 *(output++) = c;
1302 outputcnt--;
1303 }
1304 break;
wdenkc6097192002-11-03 00:24:07 +00001305 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001306 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001307 }
1308
1309 if (outputcnt)
1310 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001311 else
1312 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001313
Simon Glass3e408872013-05-15 06:24:00 +00001314 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
1315 strlen(output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001316}
1317
1318/****************************************************************************
1319 * returns:
1320 * 1 - command executed, repeatable
1321 * 0 - command executed but not repeatable, interrupted commands are
1322 * always considered not repeatable
1323 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001324 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001325 * considered unrecognized)
1326 *
1327 * WARNING:
1328 *
1329 * We must create a temporary copy of the command since the command we get
1330 * may be the result from getenv(), which returns a pointer directly to
1331 * the environment data, which may change magicly when the command we run
1332 * creates or modifies environment variables (like "bootp" does).
1333 */
Simon Glass53071532012-02-14 19:59:21 +00001334static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001335{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001336 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001337 char *token; /* start of token in cmdbuf */
1338 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001339 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001340 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001341 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001342 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001343 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001344 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001345
Simon Glass3e408872013-05-15 06:24:00 +00001346 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
1347 if (DEBUG_PARSER) {
1348 /* use puts - string may be loooong */
1349 puts(cmd ? cmd : "NULL");
1350 puts("\"\n");
1351 }
wdenkc6097192002-11-03 00:24:07 +00001352 clear_ctrlc(); /* forget any previous Control C */
1353
1354 if (!cmd || !*cmd) {
1355 return -1; /* empty command */
1356 }
1357
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001358 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001359 puts ("## Command too long!\n");
1360 return -1;
1361 }
1362
1363 strcpy (cmdbuf, cmd);
1364
1365 /* Process separators and check for invalid
1366 * repeatable commands
1367 */
1368
Simon Glass3e408872013-05-15 06:24:00 +00001369 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
wdenkc6097192002-11-03 00:24:07 +00001370 while (*str) {
1371
1372 /*
1373 * Find separator, or string end
1374 * Allow simple escape of ';' by writing "\;"
1375 */
wdenka25f8622003-01-02 23:57:29 +00001376 for (inquotes = 0, sep = str; *sep; sep++) {
1377 if ((*sep=='\'') &&
1378 (*(sep-1) != '\\'))
1379 inquotes=!inquotes;
1380
1381 if (!inquotes &&
1382 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001383 ( sep != str) && /* past string start */
1384 (*(sep-1) != '\\')) /* and NOT escaped */
1385 break;
1386 }
1387
1388 /*
1389 * Limit the token to data between separators
1390 */
1391 token = str;
1392 if (*sep) {
1393 str = sep + 1; /* start of command for next pass */
1394 *sep = '\0';
1395 }
1396 else
1397 str = sep; /* no more commands for next pass */
Simon Glass3e408872013-05-15 06:24:00 +00001398 debug_parser("token: \"%s\"\n", token);
wdenkc6097192002-11-03 00:24:07 +00001399
1400 /* find macros in this token and replace them */
1401 process_macros (token, finaltoken);
1402
1403 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001404 if ((argc = parse_line (finaltoken, argv)) == 0) {
1405 rc = -1; /* no command at all */
1406 continue;
1407 }
wdenkc6097192002-11-03 00:24:07 +00001408
Richard Genoud34765e82012-12-03 06:28:28 +00001409 if (cmd_process(flag, argc, argv, &repeatable, NULL))
Timo Ketola030fca52012-04-22 23:57:27 +00001410 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001411
1412 /* Did the user stop this? */
1413 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001414 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001415 }
1416
wdenkf07771c2003-05-28 08:06:31 +00001417 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001418}
Simon Glass7fed89e2012-02-14 19:59:22 +00001419#endif
wdenkc6097192002-11-03 00:24:07 +00001420
Simon Glass53071532012-02-14 19:59:21 +00001421/*
1422 * Run a command using the selected parser.
1423 *
1424 * @param cmd Command to run
1425 * @param flag Execution flags (CMD_FLAG_...)
1426 * @return 0 on success, or != 0 on error.
1427 */
1428int run_command(const char *cmd, int flag)
1429{
1430#ifndef CONFIG_SYS_HUSH_PARSER
1431 /*
1432 * builtin_run_command can return 0 or 1 for success, so clean up
1433 * its result.
1434 */
1435 if (builtin_run_command(cmd, flag) == -1)
1436 return 1;
1437
1438 return 0;
1439#else
1440 return parse_string_outer(cmd,
1441 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1442#endif
1443}
1444
Simon Glassd51004a2012-03-30 21:30:55 +00001445#ifndef CONFIG_SYS_HUSH_PARSER
1446/**
1447 * Execute a list of command separated by ; or \n using the built-in parser.
1448 *
1449 * This function cannot take a const char * for the command, since if it
1450 * finds newlines in the string, it replaces them with \0.
1451 *
1452 * @param cmd String containing list of commands
1453 * @param flag Execution flags (CMD_FLAG_...)
1454 * @return 0 on success, or != 0 on error.
1455 */
1456static int builtin_run_command_list(char *cmd, int flag)
1457{
1458 char *line, *next;
1459 int rcode = 0;
1460
1461 /*
1462 * Break into individual lines, and execute each line; terminate on
1463 * error.
1464 */
1465 line = next = cmd;
1466 while (*next) {
1467 if (*next == '\n') {
1468 *next = '\0';
1469 /* run only non-empty commands */
1470 if (*line) {
1471 debug("** exec: \"%s\"\n", line);
1472 if (builtin_run_command(line, 0) < 0) {
1473 rcode = 1;
1474 break;
1475 }
1476 }
1477 line = next + 1;
1478 }
1479 ++next;
1480 }
1481 if (rcode == 0 && *line)
1482 rcode = (builtin_run_command(line, 0) >= 0);
1483
1484 return rcode;
1485}
1486#endif
1487
1488int run_command_list(const char *cmd, int len, int flag)
1489{
1490 int need_buff = 1;
1491 char *buff = (char *)cmd; /* cast away const */
1492 int rcode = 0;
1493
1494 if (len == -1) {
1495 len = strlen(cmd);
1496#ifdef CONFIG_SYS_HUSH_PARSER
1497 /* hush will never change our string */
1498 need_buff = 0;
1499#else
1500 /* the built-in parser will change our string if it sees \n */
1501 need_buff = strchr(cmd, '\n') != NULL;
1502#endif
1503 }
1504 if (need_buff) {
1505 buff = malloc(len + 1);
1506 if (!buff)
1507 return 1;
1508 memcpy(buff, cmd, len);
1509 buff[len] = '\0';
1510 }
1511#ifdef CONFIG_SYS_HUSH_PARSER
1512 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1513#else
1514 /*
1515 * This function will overwrite any \n it sees with a \0, which
1516 * is why it can't work with a const char *. Here we are making
1517 * using of internal knowledge of this function, to avoid always
1518 * doing a malloc() which is actually required only in a case that
1519 * is pretty rare.
1520 */
1521 rcode = builtin_run_command_list(buff, flag);
1522 if (need_buff)
1523 free(buff);
1524#endif
1525
1526 return rcode;
1527}
1528
wdenkc6097192002-11-03 00:24:07 +00001529/****************************************************************************/
1530
Jon Loeligerc3517f92007-07-08 18:10:08 -05001531#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001532int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001533{
1534 int i;
wdenkc6097192002-11-03 00:24:07 +00001535
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001536 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001537 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001538
1539 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001540 char *arg;
1541
1542 if ((arg = getenv (argv[i])) == NULL) {
1543 printf ("## Error: \"%s\" not defined\n", argv[i]);
1544 return 1;
1545 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001546
Simon Glass009dde12012-02-14 19:59:20 +00001547 if (run_command(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001548 return 1;
wdenkc6097192002-11-03 00:24:07 +00001549 }
wdenk3e386912003-04-05 00:53:31 +00001550 return 0;
wdenkc6097192002-11-03 00:24:07 +00001551}
Jon Loeliger90253172007-07-10 11:02:44 -05001552#endif