blob: f6b9dc212f1b62f96d94d4c85095c749c5df5535 [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 *
wdenkc6097192002-11-03 00:24:07 +00009 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
wdenka6c7ad22002-12-03 21:28:10 +000028/* #define DEBUG */
29
wdenkc6097192002-11-03 00:24:07 +000030#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000031#include <command.h>
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000032#include <fdtdec.h>
wdenkc6097192002-11-03 00:24:07 +000033#include <hush.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000034#include <malloc.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000035#include <menu.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000036#include <post.h>
37#include <version.h>
38#include <watchdog.h>
39#include <linux/ctype.h>
wdenkbdccc4f2003-08-05 17:43:17 +000040
Wolfgang Denkd87080b2006-03-31 18:32:53 +020041DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denkd87080b2006-03-31 18:32:53 +020042
Heiko Schocherfad63402007-07-13 09:54:17 +020043/*
44 * Board-specific Platform code can reimplement show_boot_progress () if needed
45 */
46void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050047void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020048
wdenkc6097192002-11-03 00:24:07 +000049#define MAX_DELAY_STOP_STR 32
50
Simon Glass3e408872013-05-15 06:24:00 +000051#define DEBUG_PARSER 0 /* set to 1 to debug */
52
53#define debug_parser(fmt, args...) \
54 debug_cond(DEBUG_PARSER, fmt, ##args)
55
Simon Glassd34d1862013-05-15 06:24:01 +000056#ifndef DEBUG_BOOTKEYS
57#define DEBUG_BOOTKEYS 0
58#endif
59#define debug_bootkeys(fmt, args...) \
60 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
wdenkc6097192002-11-03 00:24:07 +000061
John Schmoller6475b9f2010-03-12 09:49:23 -060062char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000063
Stefan Roese3ca91222006-07-27 16:11:19 +020064static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050065static const char erase_seq[] = "\b \b"; /* erase sequence */
66static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000067
68#ifdef CONFIG_BOOT_RETRY_TIME
69static uint64_t endtime = 0; /* must be set, default is instant timeout */
70static int retry_time = -1; /* -1 so can call readline before main_loop */
71#endif
72
73#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
74
75#ifndef CONFIG_BOOT_RETRY_MIN
76#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
77#endif
78
79#ifdef CONFIG_MODEM_SUPPORT
80int do_mdm_init = 0;
81extern void mdm_init(void); /* defined in board.c */
82#endif
83
84/***************************************************************************
85 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000086 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000087 */
Joe Hershbergerd5145442013-02-08 10:28:00 +000088#if defined(CONFIG_BOOTDELAY)
wdenkc6097192002-11-03 00:24:07 +000089# if defined(CONFIG_AUTOBOOT_KEYED)
Simon Glass063ae002013-05-15 06:23:55 +000090static int abortboot_keyed(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000091{
92 int abort = 0;
93 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +020094 struct {
wdenkc6097192002-11-03 00:24:07 +000095 char* str;
96 u_int len;
97 int retry;
98 }
Wolfgang Denk19973b62006-10-28 00:38:39 +020099 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000100 { str: getenv ("bootdelaykey"), retry: 1 },
101 { str: getenv ("bootdelaykey2"), retry: 1 },
102 { str: getenv ("bootstopkey"), retry: 0 },
103 { str: getenv ("bootstopkey2"), retry: 0 },
104 };
105
106 char presskey [MAX_DELAY_STOP_STR];
107 u_int presskey_len = 0;
108 u_int presskey_max = 0;
109 u_int i;
110
Dirk Eibacha5aae0a2012-04-26 01:49:33 +0000111#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
112 if (bootdelay == 0)
113 return 0;
114#endif
115
wdenkc6097192002-11-03 00:24:07 +0000116# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200117 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000118# endif
119
120# ifdef CONFIG_AUTOBOOT_DELAY_STR
121 if (delaykey[0].str == NULL)
122 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
123# endif
124# ifdef CONFIG_AUTOBOOT_DELAY_STR2
125 if (delaykey[1].str == NULL)
126 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
127# endif
128# ifdef CONFIG_AUTOBOOT_STOP_STR
129 if (delaykey[2].str == NULL)
130 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
131# endif
132# ifdef CONFIG_AUTOBOOT_STOP_STR2
133 if (delaykey[3].str == NULL)
134 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
135# endif
136
137 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
138 delaykey[i].len = delaykey[i].str == NULL ?
139 0 : strlen (delaykey[i].str);
140 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
141 MAX_DELAY_STOP_STR : delaykey[i].len;
142
143 presskey_max = presskey_max > delaykey[i].len ?
144 presskey_max : delaykey[i].len;
145
Simon Glassd34d1862013-05-15 06:24:01 +0000146 debug_bootkeys("%s key:<%s>\n",
147 delaykey[i].retry ? "delay" : "stop",
148 delaykey[i].str ? delaykey[i].str : "NULL");
wdenkc6097192002-11-03 00:24:07 +0000149 }
150
151 /* In order to keep up with incoming data, check timeout only
152 * when catch up.
153 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100154 do {
155 if (tstc()) {
156 if (presskey_len < presskey_max) {
157 presskey [presskey_len ++] = getc();
158 }
159 else {
160 for (i = 0; i < presskey_max - 1; i ++)
161 presskey [i] = presskey [i + 1];
162
163 presskey [i] = getc();
164 }
165 }
166
wdenkc6097192002-11-03 00:24:07 +0000167 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
168 if (delaykey[i].len > 0 &&
169 presskey_len >= delaykey[i].len &&
170 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000171 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000172 delaykey[i].len) == 0) {
Simon Glassd34d1862013-05-15 06:24:01 +0000173 debug_bootkeys("got %skey\n",
174 delaykey[i].retry ? "delay" :
175 "stop");
wdenkc6097192002-11-03 00:24:07 +0000176
177# ifdef CONFIG_BOOT_RETRY_TIME
178 /* don't retry auto boot */
179 if (! delaykey[i].retry)
180 retry_time = -1;
181# endif
182 abort = 1;
183 }
184 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100185 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000186
wdenkc6097192002-11-03 00:24:07 +0000187 if (!abort)
Simon Glassd34d1862013-05-15 06:24:01 +0000188 debug_bootkeys("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000189
dzu8cb81432003-10-24 13:14:45 +0000190#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200191 if (abort)
192 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000193#endif
194
wdenkc6097192002-11-03 00:24:07 +0000195 return abort;
196}
197
198# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
199
wdenkc7de8292002-11-19 11:04:11 +0000200#ifdef CONFIG_MENUKEY
201static int menukey = 0;
202#endif
203
Simon Glass063ae002013-05-15 06:23:55 +0000204static int abortboot_normal(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000205{
206 int abort = 0;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000207 unsigned long ts;
wdenkc6097192002-11-03 00:24:07 +0000208
wdenkc7de8292002-11-19 11:04:11 +0000209#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200210 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000211#else
Joe Hershberger93d72122012-08-17 10:53:12 +0000212 if (bootdelay >= 0)
213 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000214#endif
wdenkc6097192002-11-03 00:24:07 +0000215
216#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000217 /*
218 * Check if key already pressed
219 * Don't check if bootdelay < 0
220 */
wdenkc6097192002-11-03 00:24:07 +0000221 if (bootdelay >= 0) {
222 if (tstc()) { /* we got a key press */
223 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000224 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200225 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000226 }
wdenk8bde7f72003-06-27 21:31:46 +0000227 }
wdenkc6097192002-11-03 00:24:07 +0000228#endif
229
wdenkf72da342003-10-10 10:05:42 +0000230 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000231 --bootdelay;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000232 /* delay 1000 ms */
233 ts = get_timer(0);
234 do {
wdenkc6097192002-11-03 00:24:07 +0000235 if (tstc()) { /* we got a key press */
236 abort = 1; /* don't auto boot */
237 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000238# ifdef CONFIG_MENUKEY
239 menukey = getc();
240# else
wdenkc6097192002-11-03 00:24:07 +0000241 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000242# endif
wdenkc6097192002-11-03 00:24:07 +0000243 break;
244 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200245 udelay(10000);
Jim Linb2f3e0e2013-01-24 01:05:55 +0000246 } while (!abort && get_timer(ts) < 1000);
wdenkc6097192002-11-03 00:24:07 +0000247
Ladislav Michlada4d402007-04-25 16:01:26 +0200248 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000249 }
250
Ladislav Michlada4d402007-04-25 16:01:26 +0200251 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000252
wdenkf72da342003-10-10 10:05:42 +0000253#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200254 if (abort)
255 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000256#endif
257
wdenkc6097192002-11-03 00:24:07 +0000258 return abort;
259}
260# endif /* CONFIG_AUTOBOOT_KEYED */
Simon Glass063ae002013-05-15 06:23:55 +0000261
262static int abortboot(int bootdelay)
263{
264#ifdef CONFIG_AUTOBOOT_KEYED
265 return abortboot_keyed(bootdelay);
266#else
267 return abortboot_normal(bootdelay);
268#endif
269}
Joe Hershbergerd5145442013-02-08 10:28:00 +0000270#endif /* CONFIG_BOOTDELAY */
wdenkc6097192002-11-03 00:24:07 +0000271
Doug Anderson67e1ea22012-10-25 16:31:09 +0000272/*
273 * Runs the given boot command securely. Specifically:
274 * - Doesn't run the command with the shell (run_command or parse_string_outer),
275 * since that's a lot of code surface that an attacker might exploit.
276 * Because of this, we don't do any argument parsing--the secure boot command
277 * has to be a full-fledged u-boot command.
278 * - Doesn't check for keypresses before booting, since that could be a
279 * security hole; also disables Ctrl-C.
280 * - Doesn't allow the command to return.
281 *
282 * Upon any failures, this function will drop into an infinite loop after
283 * printing the error message to console.
284 */
285
Joe Hershbergerd5145442013-02-08 10:28:00 +0000286#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
Doug Anderson67e1ea22012-10-25 16:31:09 +0000287static void secure_boot_cmd(char *cmd)
288{
289 cmd_tbl_t *cmdtp;
290 int rc;
291
292 if (!cmd) {
293 printf("## Error: Secure boot command not specified\n");
294 goto err;
295 }
296
297 /* Disable Ctrl-C just in case some command is used that checks it. */
298 disable_ctrlc(1);
299
300 /* Find the command directly. */
301 cmdtp = find_cmd(cmd);
302 if (!cmdtp) {
303 printf("## Error: \"%s\" not defined\n", cmd);
304 goto err;
305 }
306
307 /* Run the command, forcing no flags and faking argc and argv. */
308 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
309
310 /* Shouldn't ever return from boot command. */
311 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
312
313err:
314 /*
315 * Not a whole lot to do here. Rebooting won't help much, since we'll
316 * just end up right back here. Just loop.
317 */
318 hang();
319}
320
Simon Glassfcabc242012-10-25 16:31:11 +0000321static void process_fdt_options(const void *blob)
322{
323 ulong addr;
324
325 /* Add an env variable to point to a kernel payload, if available */
326 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
327 if (addr)
328 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
329
330 /* Add an env variable to point to a root disk, if available */
331 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
332 if (addr)
333 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
334}
Doug Anderson67e1ea22012-10-25 16:31:09 +0000335#endif /* CONFIG_OF_CONTROL */
336
Simon Glassbc2b4c22013-05-15 06:23:56 +0000337#ifdef CONFIG_BOOTDELAY
338static void process_boot_delay(void)
wdenkc6097192002-11-03 00:24:07 +0000339{
Simon Glassbc2b4c22013-05-15 06:23:56 +0000340#ifdef CONFIG_OF_CONTROL
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000341 char *env;
342#endif
wdenkc6097192002-11-03 00:24:07 +0000343 char *s;
344 int bootdelay;
wdenkbdccc4f2003-08-05 17:43:17 +0000345#ifdef CONFIG_BOOTCOUNT_LIMIT
346 unsigned long bootcount = 0;
347 unsigned long bootlimit = 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000348#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000349
wdenkbdccc4f2003-08-05 17:43:17 +0000350#ifdef CONFIG_BOOTCOUNT_LIMIT
351 bootcount = bootcount_load();
352 bootcount++;
353 bootcount_store (bootcount);
Simon Glassf2abca82013-05-15 06:23:57 +0000354 setenv_ulong("bootcount", bootcount);
355 bootlimit = getenv_ulong("bootlimit", 10, 0);
wdenkbdccc4f2003-08-05 17:43:17 +0000356#endif /* CONFIG_BOOTCOUNT_LIMIT */
357
wdenkc6097192002-11-03 00:24:07 +0000358 s = getenv ("bootdelay");
359 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
360
wdenka6c7ad22002-12-03 21:28:10 +0000361 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000362
Heiko Schocher317d6c52012-01-16 21:13:35 +0000363#if defined(CONFIG_MENU_SHOW)
364 bootdelay = menu_show(bootdelay);
365#endif
wdenkc6097192002-11-03 00:24:07 +0000366# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000367 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000368# endif /* CONFIG_BOOT_RETRY_TIME */
369
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100370#ifdef CONFIG_POST
371 if (gd->flags & GD_FLG_POSTFAIL) {
372 s = getenv("failbootcmd");
373 }
374 else
375#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000376#ifdef CONFIG_BOOTCOUNT_LIMIT
377 if (bootlimit && (bootcount > bootlimit)) {
378 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
379 (unsigned)bootlimit);
380 s = getenv ("altbootcmd");
381 }
382 else
383#endif /* CONFIG_BOOTCOUNT_LIMIT */
384 s = getenv ("bootcmd");
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000385#ifdef CONFIG_OF_CONTROL
386 /* Allow the fdt to override the boot command */
387 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
388 if (env)
389 s = env;
Doug Anderson67e1ea22012-10-25 16:31:09 +0000390
Simon Glassfcabc242012-10-25 16:31:11 +0000391 process_fdt_options(gd->fdt_blob);
392
Doug Anderson67e1ea22012-10-25 16:31:09 +0000393 /*
394 * If the bootsecure option was chosen, use secure_boot_cmd().
395 * Always use 'env' in this case, since bootsecure requres that the
396 * bootcmd was specified in the FDT too.
397 */
398 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
399 secure_boot_cmd(env);
400
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000401#endif /* CONFIG_OF_CONTROL */
wdenka6c7ad22002-12-03 21:28:10 +0000402
403 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
404
Joe Hershberger93d72122012-08-17 10:53:12 +0000405 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
Simon Glassbc2b4c22013-05-15 06:23:56 +0000406#ifdef CONFIG_AUTOBOOT_KEYED
wdenkc6097192002-11-03 00:24:07 +0000407 int prev = disable_ctrlc(1); /* disable Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000408#endif
wdenkc6097192002-11-03 00:24:07 +0000409
Simon Glass3a8a02b2012-03-30 21:30:56 +0000410 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000411
Simon Glassbc2b4c22013-05-15 06:23:56 +0000412#ifdef CONFIG_AUTOBOOT_KEYED
wdenkc6097192002-11-03 00:24:07 +0000413 disable_ctrlc(prev); /* restore Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000414#endif
wdenkc6097192002-11-03 00:24:07 +0000415 }
wdenkc7de8292002-11-19 11:04:11 +0000416
Simon Glassbc2b4c22013-05-15 06:23:56 +0000417#ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000418 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000419 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000420 if (s)
Simon Glass3a8a02b2012-03-30 21:30:56 +0000421 run_command_list(s, -1, 0);
wdenkc7de8292002-11-19 11:04:11 +0000422 }
423#endif /* CONFIG_MENUKEY */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000424}
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200425#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000426
Simon Glassbc2b4c22013-05-15 06:23:56 +0000427void main_loop(void)
428{
429#ifndef CONFIG_SYS_HUSH_PARSER
430 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
431 int len;
432 int rc = 1;
433 int flag;
434#endif
435#ifdef CONFIG_PREBOOT
436 char *p;
437#endif
438
439 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
440
441#ifdef CONFIG_MODEM_SUPPORT
442 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
443 if (do_mdm_init) {
444 char *str = strdup(getenv("mdm_cmd"));
445 setenv("preboot", str); /* set or delete definition */
446 if (str != NULL)
447 free(str);
448 mdm_init(); /* wait for modem connection */
449 }
450#endif /* CONFIG_MODEM_SUPPORT */
451
452#ifdef CONFIG_VERSION_VARIABLE
453 {
454 setenv("ver", version_string); /* set version variable */
455 }
456#endif /* CONFIG_VERSION_VARIABLE */
457
458#ifdef CONFIG_SYS_HUSH_PARSER
459 u_boot_hush_start();
460#endif
461
462#if defined(CONFIG_HUSH_INIT_VAR)
463 hush_init_var();
464#endif
465
466#ifdef CONFIG_PREBOOT
467 p = getenv("preboot");
468 if (p != NULL) {
469# ifdef CONFIG_AUTOBOOT_KEYED
470 int prev = disable_ctrlc(1); /* disable Control C checking */
471# endif
472
473 run_command_list(p, -1, 0);
474
475# ifdef CONFIG_AUTOBOOT_KEYED
476 disable_ctrlc(prev); /* restore Control C checking */
477# endif
478 }
479#endif /* CONFIG_PREBOOT */
480
481#if defined(CONFIG_UPDATE_TFTP)
482 update_tftp(0UL);
483#endif /* CONFIG_UPDATE_TFTP */
484
485#ifdef CONFIG_BOOTDELAY
486 process_boot_delay();
487#endif
wdenkc6097192002-11-03 00:24:07 +0000488 /*
489 * Main Loop for Monitor Command Processing
490 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200491#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000492 parse_file_outer();
493 /* This point is never reached */
494 for (;;);
495#else
496 for (;;) {
497#ifdef CONFIG_BOOT_RETRY_TIME
498 if (rc >= 0) {
499 /* Saw enough of a valid command to
500 * restart the timeout.
501 */
502 reset_cmd_timeout();
503 }
504#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200505 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000506
507 flag = 0; /* assume no special flags for now */
508 if (len > 0)
509 strcpy (lastcommand, console_buffer);
510 else if (len == 0)
511 flag |= CMD_FLAG_REPEAT;
512#ifdef CONFIG_BOOT_RETRY_TIME
513 else if (len == -2) {
514 /* -2 means timed out, retry autoboot
515 */
wdenk4b9206e2004-03-23 22:14:11 +0000516 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000517# ifdef CONFIG_RESET_TO_RETRY
518 /* Reinit board to run initialization code again */
519 do_reset (NULL, 0, 0, NULL);
520# else
521 return; /* retry autoboot */
522# endif
523 }
524#endif
525
526 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000527 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000528 else
Simon Glass53071532012-02-14 19:59:21 +0000529 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000530
531 if (rc <= 0) {
532 /* invalid command or not repeatable, forget it */
533 lastcommand[0] = 0;
534 }
535 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200536#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000537}
538
wdenk6dd652f2003-06-19 23:40:20 +0000539#ifdef CONFIG_BOOT_RETRY_TIME
540/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200541 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000542 */
543void init_cmd_timeout(void)
544{
545 char *s = getenv ("bootretry");
546
547 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000548 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000549 else
550 retry_time = CONFIG_BOOT_RETRY_TIME;
551
552 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
553 retry_time = CONFIG_BOOT_RETRY_MIN;
554}
555
wdenkc6097192002-11-03 00:24:07 +0000556/***************************************************************************
557 * reset command line timeout to retry_time seconds
558 */
wdenkc6097192002-11-03 00:24:07 +0000559void reset_cmd_timeout(void)
560{
561 endtime = endtick(retry_time);
562}
563#endif
564
Wolfgang Denk501090a2006-07-21 11:33:45 +0200565#ifdef CONFIG_CMDLINE_EDITING
566
567/*
568 * cmdline-editing related codes from vivi.
569 * Author: Janghoon Lyu <nandy@mizi.com>
570 */
571
Wolfgang Denk501090a2006-07-21 11:33:45 +0200572#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700573 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200574 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200575
576#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200577#define CTL_BACKSPACE ('\b')
578#define DEL ((char)255)
579#define DEL7 ((char)127)
580#define CREAD_HIST_CHAR ('!')
581
582#define getcmd_putch(ch) putc(ch)
583#define getcmd_getch() getc()
584#define getcmd_cbeep() getcmd_putch('\a')
585
586#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500587#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200588
Kim Phillips199adb62012-10-29 13:34:32 +0000589static int hist_max;
590static int hist_add_idx;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200591static int hist_cur = -1;
Kim Phillips199adb62012-10-29 13:34:32 +0000592static unsigned hist_num;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200593
Kim Phillips199adb62012-10-29 13:34:32 +0000594static char *hist_list[HIST_MAX];
595static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200596
597#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
598
599static void hist_init(void)
600{
601 int i;
602
603 hist_max = 0;
604 hist_add_idx = 0;
605 hist_cur = -1;
606 hist_num = 0;
607
608 for (i = 0; i < HIST_MAX; i++) {
609 hist_list[i] = hist_lines[i];
610 hist_list[i][0] = '\0';
611 }
612}
613
614static void cread_add_to_hist(char *line)
615{
616 strcpy(hist_list[hist_add_idx], line);
617
618 if (++hist_add_idx >= HIST_MAX)
619 hist_add_idx = 0;
620
621 if (hist_add_idx > hist_max)
622 hist_max = hist_add_idx;
623
624 hist_num++;
625}
626
627static char* hist_prev(void)
628{
629 char *ret;
630 int old_cur;
631
632 if (hist_cur < 0)
633 return NULL;
634
635 old_cur = hist_cur;
636 if (--hist_cur < 0)
637 hist_cur = hist_max;
638
639 if (hist_cur == hist_add_idx) {
640 hist_cur = old_cur;
641 ret = NULL;
642 } else
643 ret = hist_list[hist_cur];
644
645 return (ret);
646}
647
648static char* hist_next(void)
649{
650 char *ret;
651
652 if (hist_cur < 0)
653 return NULL;
654
655 if (hist_cur == hist_add_idx)
656 return NULL;
657
658 if (++hist_cur > hist_max)
659 hist_cur = 0;
660
661 if (hist_cur == hist_add_idx) {
662 ret = "";
663 } else
664 ret = hist_list[hist_cur];
665
666 return (ret);
667}
668
Stefan Roese3ca91222006-07-27 16:11:19 +0200669#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200670static void cread_print_hist_list(void)
671{
672 int i;
673 unsigned long n;
674
675 n = hist_num - hist_max;
676
677 i = hist_add_idx + 1;
678 while (1) {
679 if (i > hist_max)
680 i = 0;
681 if (i == hist_add_idx)
682 break;
683 printf("%s\n", hist_list[i]);
684 n++;
685 i++;
686 }
687}
Stefan Roese3ca91222006-07-27 16:11:19 +0200688#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200689
690#define BEGINNING_OF_LINE() { \
691 while (num) { \
692 getcmd_putch(CTL_BACKSPACE); \
693 num--; \
694 } \
695}
696
697#define ERASE_TO_EOL() { \
698 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400699 printf("%*s", (int)(eol_num - num), ""); \
700 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200701 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400702 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200703 } \
704}
705
706#define REFRESH_TO_EOL() { \
707 if (num < eol_num) { \
708 wlen = eol_num - num; \
709 putnstr(buf + num, wlen); \
710 num = eol_num; \
711 } \
712}
713
714static void cread_add_char(char ichar, int insert, unsigned long *num,
715 unsigned long *eol_num, char *buf, unsigned long len)
716{
717 unsigned long wlen;
718
719 /* room ??? */
720 if (insert || *num == *eol_num) {
721 if (*eol_num > len - 1) {
722 getcmd_cbeep();
723 return;
724 }
725 (*eol_num)++;
726 }
727
728 if (insert) {
729 wlen = *eol_num - *num;
730 if (wlen > 1) {
731 memmove(&buf[*num+1], &buf[*num], wlen-1);
732 }
733
734 buf[*num] = ichar;
735 putnstr(buf + *num, wlen);
736 (*num)++;
737 while (--wlen) {
738 getcmd_putch(CTL_BACKSPACE);
739 }
740 } else {
741 /* echo the character */
742 wlen = 1;
743 buf[*num] = ichar;
744 putnstr(buf + *num, wlen);
745 (*num)++;
746 }
747}
748
749static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
750 unsigned long *eol_num, char *buf, unsigned long len)
751{
752 while (strsize--) {
753 cread_add_char(*str, insert, num, eol_num, buf, len);
754 str++;
755 }
756}
757
Heiko Schocher9c348312012-01-16 21:13:05 +0000758static int cread_line(const char *const prompt, char *buf, unsigned int *len,
759 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200760{
761 unsigned long num = 0;
762 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200763 unsigned long wlen;
764 char ichar;
765 int insert = 1;
766 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200767 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500768 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000769 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500770
771 if (init_len)
772 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200773
774 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100775#ifdef CONFIG_BOOT_RETRY_TIME
776 while (!tstc()) { /* while no incoming data */
777 if (retry_time >= 0 && get_ticks() > endtime)
778 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200779 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100780 }
781#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000782 if (first && timeout) {
783 uint64_t etime = endtick(timeout);
784
785 while (!tstc()) { /* while no incoming data */
786 if (get_ticks() >= etime)
787 return -2; /* timed out */
788 WATCHDOG_RESET();
789 }
790 first = 0;
791 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100792
Wolfgang Denk501090a2006-07-21 11:33:45 +0200793 ichar = getcmd_getch();
794
795 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200796 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200797 break;
798 }
799
800 /*
801 * handle standard linux xterm esc sequences for arrow key, etc.
802 */
803 if (esc_len != 0) {
804 if (esc_len == 1) {
805 if (ichar == '[') {
806 esc_save[esc_len] = ichar;
807 esc_len = 2;
808 } else {
809 cread_add_str(esc_save, esc_len, insert,
810 &num, &eol_num, buf, *len);
811 esc_len = 0;
812 }
813 continue;
814 }
815
816 switch (ichar) {
817
818 case 'D': /* <- key */
819 ichar = CTL_CH('b');
820 esc_len = 0;
821 break;
822 case 'C': /* -> key */
823 ichar = CTL_CH('f');
824 esc_len = 0;
825 break; /* pass off to ^F handler */
826 case 'H': /* Home key */
827 ichar = CTL_CH('a');
828 esc_len = 0;
829 break; /* pass off to ^A handler */
830 case 'A': /* up arrow */
831 ichar = CTL_CH('p');
832 esc_len = 0;
833 break; /* pass off to ^P handler */
834 case 'B': /* down arrow */
835 ichar = CTL_CH('n');
836 esc_len = 0;
837 break; /* pass off to ^N handler */
838 default:
839 esc_save[esc_len++] = ichar;
840 cread_add_str(esc_save, esc_len, insert,
841 &num, &eol_num, buf, *len);
842 esc_len = 0;
843 continue;
844 }
845 }
846
847 switch (ichar) {
848 case 0x1b:
849 if (esc_len == 0) {
850 esc_save[esc_len] = ichar;
851 esc_len = 1;
852 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200853 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200854 esc_len = 0;
855 }
856 break;
857
858 case CTL_CH('a'):
859 BEGINNING_OF_LINE();
860 break;
861 case CTL_CH('c'): /* ^C - break */
862 *buf = '\0'; /* discard input */
863 return (-1);
864 case CTL_CH('f'):
865 if (num < eol_num) {
866 getcmd_putch(buf[num]);
867 num++;
868 }
869 break;
870 case CTL_CH('b'):
871 if (num) {
872 getcmd_putch(CTL_BACKSPACE);
873 num--;
874 }
875 break;
876 case CTL_CH('d'):
877 if (num < eol_num) {
878 wlen = eol_num - num - 1;
879 if (wlen) {
880 memmove(&buf[num], &buf[num+1], wlen);
881 putnstr(buf + num, wlen);
882 }
883
884 getcmd_putch(' ');
885 do {
886 getcmd_putch(CTL_BACKSPACE);
887 } while (wlen--);
888 eol_num--;
889 }
890 break;
891 case CTL_CH('k'):
892 ERASE_TO_EOL();
893 break;
894 case CTL_CH('e'):
895 REFRESH_TO_EOL();
896 break;
897 case CTL_CH('o'):
898 insert = !insert;
899 break;
900 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100901 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200902 BEGINNING_OF_LINE();
903 ERASE_TO_EOL();
904 break;
905 case DEL:
906 case DEL7:
907 case 8:
908 if (num) {
909 wlen = eol_num - num;
910 num--;
911 memmove(&buf[num], &buf[num+1], wlen);
912 getcmd_putch(CTL_BACKSPACE);
913 putnstr(buf + num, wlen);
914 getcmd_putch(' ');
915 do {
916 getcmd_putch(CTL_BACKSPACE);
917 } while (wlen--);
918 eol_num--;
919 }
920 break;
921 case CTL_CH('p'):
922 case CTL_CH('n'):
923 {
924 char * hline;
925
926 esc_len = 0;
927
928 if (ichar == CTL_CH('p'))
929 hline = hist_prev();
930 else
931 hline = hist_next();
932
933 if (!hline) {
934 getcmd_cbeep();
935 continue;
936 }
937
938 /* nuke the current line */
939 /* first, go home */
940 BEGINNING_OF_LINE();
941
942 /* erase to end of line */
943 ERASE_TO_EOL();
944
945 /* copy new line into place and display */
946 strcpy(buf, hline);
947 eol_num = strlen(buf);
948 REFRESH_TO_EOL();
949 continue;
950 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100951#ifdef CONFIG_AUTO_COMPLETE
952 case '\t': {
953 int num2, col;
954
955 /* do not autocomplete when in the middle */
956 if (num < eol_num) {
957 getcmd_cbeep();
958 break;
959 }
960
961 buf[num] = '\0';
962 col = strlen(prompt) + eol_num;
963 num2 = num;
964 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
965 col = num2 - num;
966 num += col;
967 eol_num += col;
968 }
969 break;
970 }
971#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200972 default:
973 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
974 break;
975 }
976 }
977 *len = eol_num;
978 buf[eol_num] = '\0'; /* lose the newline */
979
980 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
981 cread_add_to_hist(buf);
982 hist_cur = hist_add_idx;
983
Peter Tyserf9239432009-10-25 15:12:53 -0500984 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200985}
986
987#endif /* CONFIG_CMDLINE_EDITING */
988
wdenkc6097192002-11-03 00:24:07 +0000989/****************************************************************************/
990
991/*
992 * Prompt for input and read a line.
993 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
994 * time out when time goes past endtime (timebase time in ticks).
995 * Return: number of read characters
996 * -1 if break
997 * -2 if timed out
998 */
999int readline (const char *const prompt)
1000{
Peter Tyserecc55002009-10-25 15:12:54 -05001001 /*
1002 * If console_buffer isn't 0-length the user will be prompted to modify
1003 * it instead of entering it from scratch as desired.
1004 */
1005 console_buffer[0] = '\0';
1006
Heiko Schocher9c348312012-01-16 21:13:05 +00001007 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -06001008}
1009
1010
Heiko Schocher9c348312012-01-16 21:13:05 +00001011int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -06001012{
1013 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001014#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -05001015 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +02001016 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001017 static int initted = 0;
1018
James Yang597f6c22008-05-05 10:22:53 -05001019 /*
1020 * History uses a global array which is not
1021 * writable until after relocation to RAM.
1022 * Revert to non-history version if still
1023 * running from flash.
1024 */
1025 if (gd->flags & GD_FLG_RELOC) {
1026 if (!initted) {
1027 hist_init();
1028 initted = 1;
1029 }
1030
Peter Tysere491a712009-10-25 15:12:52 -05001031 if (prompt)
1032 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -05001033
Heiko Schocher9c348312012-01-16 21:13:05 +00001034 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -05001035 return rc < 0 ? rc : len;
1036
1037 } else {
1038#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -06001039 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +00001040 int n = 0; /* buffer index */
1041 int plen = 0; /* prompt length */
1042 int col; /* output column cnt */
1043 char c;
1044
1045 /* print prompt */
1046 if (prompt) {
1047 plen = strlen (prompt);
1048 puts (prompt);
1049 }
1050 col = plen;
1051
1052 for (;;) {
1053#ifdef CONFIG_BOOT_RETRY_TIME
1054 while (!tstc()) { /* while no incoming data */
1055 if (retry_time >= 0 && get_ticks() > endtime)
1056 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +02001057 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001058 }
1059#endif
1060 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1061
1062#ifdef CONFIG_SHOW_ACTIVITY
1063 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +00001064 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +02001065 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001066 }
1067#endif
1068 c = getc();
1069
1070 /*
1071 * Special character handling
1072 */
1073 switch (c) {
Simon Glass49333812013-05-15 06:23:58 +00001074 case '\r': /* Enter */
wdenkc6097192002-11-03 00:24:07 +00001075 case '\n':
1076 *p = '\0';
1077 puts ("\r\n");
Simon Glass49333812013-05-15 06:23:58 +00001078 return p - p_buf;
wdenkc6097192002-11-03 00:24:07 +00001079
Simon Glass49333812013-05-15 06:23:58 +00001080 case '\0': /* nul */
wdenk27aa8182004-03-23 22:37:33 +00001081 continue;
1082
Simon Glass49333812013-05-15 06:23:58 +00001083 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001084 p_buf[0] = '\0'; /* discard input */
Simon Glass49333812013-05-15 06:23:58 +00001085 return -1;
wdenkc6097192002-11-03 00:24:07 +00001086
Simon Glass49333812013-05-15 06:23:58 +00001087 case 0x15: /* ^U - erase line */
wdenkc6097192002-11-03 00:24:07 +00001088 while (col > plen) {
1089 puts (erase_seq);
1090 --col;
1091 }
James Yang6636b622008-01-09 11:17:49 -06001092 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001093 n = 0;
1094 continue;
1095
Simon Glass49333812013-05-15 06:23:58 +00001096 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001097 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001098 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001099 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001100 }
1101 continue;
1102
Simon Glass49333812013-05-15 06:23:58 +00001103 case 0x08: /* ^H - backspace */
1104 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001105 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001106 continue;
1107
1108 default:
1109 /*
1110 * Must be a normal character then
1111 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001112 if (n < CONFIG_SYS_CBSIZE-2) {
Simon Glass49333812013-05-15 06:23:58 +00001113 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001114#ifdef CONFIG_AUTO_COMPLETE
1115 /* if auto completion triggered just continue */
1116 *p = '\0';
1117 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001118 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001119 continue;
1120 }
1121#endif
wdenkc6097192002-11-03 00:24:07 +00001122 puts (tab_seq+(col&07));
1123 col += 8 - (col&07);
1124 } else {
Simon Glass9a8efc42012-10-30 13:40:18 +00001125 char buf[2];
1126
1127 /*
Simon Glass49333812013-05-15 06:23:58 +00001128 * Echo input using puts() to force an
Simon Glass9a8efc42012-10-30 13:40:18 +00001129 * LCD flush if we are using an LCD
1130 */
1131 ++col;
1132 buf[0] = c;
1133 buf[1] = '\0';
1134 puts(buf);
wdenkc6097192002-11-03 00:24:07 +00001135 }
1136 *p++ = c;
1137 ++n;
1138 } else { /* Buffer full */
1139 putc ('\a');
1140 }
1141 }
1142 }
James Yang597f6c22008-05-05 10:22:53 -05001143#ifdef CONFIG_CMDLINE_EDITING
1144 }
1145#endif
wdenkc6097192002-11-03 00:24:07 +00001146}
1147
1148/****************************************************************************/
1149
1150static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1151{
1152 char *s;
1153
1154 if (*np == 0) {
1155 return (p);
1156 }
1157
1158 if (*(--p) == '\t') { /* will retype the whole line */
1159 while (*colp > plen) {
1160 puts (erase_seq);
1161 (*colp)--;
1162 }
1163 for (s=buffer; s<p; ++s) {
1164 if (*s == '\t') {
1165 puts (tab_seq+((*colp) & 07));
1166 *colp += 8 - ((*colp) & 07);
1167 } else {
1168 ++(*colp);
1169 putc (*s);
1170 }
1171 }
1172 } else {
1173 puts (erase_seq);
1174 (*colp)--;
1175 }
1176 (*np)--;
1177 return (p);
1178}
1179
1180/****************************************************************************/
1181
1182int parse_line (char *line, char *argv[])
1183{
1184 int nargs = 0;
1185
Simon Glass3e408872013-05-15 06:24:00 +00001186 debug_parser("parse_line: \"%s\"\n", line);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001187 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001188
1189 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001190 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001191 ++line;
wdenkc6097192002-11-03 00:24:07 +00001192
1193 if (*line == '\0') { /* end of line, no more args */
1194 argv[nargs] = NULL;
Simon Glass3e408872013-05-15 06:24:00 +00001195 debug_parser("parse_line: nargs=%d\n", nargs);
1196 return nargs;
wdenkc6097192002-11-03 00:24:07 +00001197 }
1198
1199 argv[nargs++] = line; /* begin of argument string */
1200
1201 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001202 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001203 ++line;
wdenkc6097192002-11-03 00:24:07 +00001204
1205 if (*line == '\0') { /* end of line, no more args */
1206 argv[nargs] = NULL;
Simon Glass3e408872013-05-15 06:24:00 +00001207 debug_parser("parse_line: nargs=%d\n", nargs);
1208 return nargs;
wdenkc6097192002-11-03 00:24:07 +00001209 }
1210
1211 *line++ = '\0'; /* terminate current arg */
1212 }
1213
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001214 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001215
Simon Glass3e408872013-05-15 06:24:00 +00001216 debug_parser("parse_line: nargs=%d\n", nargs);
wdenkc6097192002-11-03 00:24:07 +00001217 return (nargs);
1218}
1219
1220/****************************************************************************/
1221
Simon Glass7fed89e2012-02-14 19:59:22 +00001222#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001223static void process_macros (const char *input, char *output)
1224{
1225 char c, prev;
1226 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001227 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001228 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001229 int state = 0; /* 0 = waiting for '$' */
1230
1231 /* 1 = waiting for '(' or '{' */
1232 /* 2 = waiting for ')' or '}' */
1233 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001234 char *output_start = output;
1235
Simon Glass3e408872013-05-15 06:24:00 +00001236 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
1237 input);
wdenkc6097192002-11-03 00:24:07 +00001238
Wolfgang Denk19973b62006-10-28 00:38:39 +02001239 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001240
1241 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001242 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001243 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001244
Wolfgang Denk19973b62006-10-28 00:38:39 +02001245 if (state != 3) {
1246 /* remove one level of escape characters */
1247 if ((c == '\\') && (prev != '\\')) {
1248 if (inputcnt-- == 0)
1249 break;
1250 prev = c;
1251 c = *input++;
1252 }
wdenka25f8622003-01-02 23:57:29 +00001253 }
wdenkc6097192002-11-03 00:24:07 +00001254
Wolfgang Denk19973b62006-10-28 00:38:39 +02001255 switch (state) {
1256 case 0: /* Waiting for (unescaped) $ */
1257 if ((c == '\'') && (prev != '\\')) {
1258 state = 3;
1259 break;
1260 }
1261 if ((c == '$') && (prev != '\\')) {
1262 state++;
1263 } else {
wdenkc6097192002-11-03 00:24:07 +00001264 *(output++) = c;
1265 outputcnt--;
1266 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001267 break;
1268 case 1: /* Waiting for ( */
1269 if (c == '(' || c == '{') {
1270 state++;
1271 varname_start = input;
1272 } else {
1273 state = 0;
1274 *(output++) = '$';
1275 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001276
Wolfgang Denk19973b62006-10-28 00:38:39 +02001277 if (outputcnt) {
1278 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001279 outputcnt--;
1280 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001281 }
1282 break;
1283 case 2: /* Waiting for ) */
1284 if (c == ')' || c == '}') {
1285 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001286 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001287 int envcnt = input - varname_start - 1; /* Varname # of chars */
1288
1289 /* Get the varname */
1290 for (i = 0; i < envcnt; i++) {
1291 envname[i] = varname_start[i];
1292 }
1293 envname[i] = 0;
1294
1295 /* Get its value */
1296 envval = getenv (envname);
1297
1298 /* Copy into the line if it exists */
1299 if (envval != NULL)
1300 while ((*envval) && outputcnt) {
1301 *(output++) = *(envval++);
1302 outputcnt--;
1303 }
1304 /* Look for another '$' */
1305 state = 0;
1306 }
1307 break;
1308 case 3: /* Waiting for ' */
1309 if ((c == '\'') && (prev != '\\')) {
1310 state = 0;
1311 } else {
1312 *(output++) = c;
1313 outputcnt--;
1314 }
1315 break;
wdenkc6097192002-11-03 00:24:07 +00001316 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001317 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001318 }
1319
1320 if (outputcnt)
1321 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001322 else
1323 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001324
Simon Glass3e408872013-05-15 06:24:00 +00001325 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
1326 strlen(output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001327}
1328
1329/****************************************************************************
1330 * returns:
1331 * 1 - command executed, repeatable
1332 * 0 - command executed but not repeatable, interrupted commands are
1333 * always considered not repeatable
1334 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001335 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001336 * considered unrecognized)
1337 *
1338 * WARNING:
1339 *
1340 * We must create a temporary copy of the command since the command we get
1341 * may be the result from getenv(), which returns a pointer directly to
1342 * the environment data, which may change magicly when the command we run
1343 * creates or modifies environment variables (like "bootp" does).
1344 */
Simon Glass53071532012-02-14 19:59:21 +00001345static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001346{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001347 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001348 char *token; /* start of token in cmdbuf */
1349 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001350 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001351 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001352 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001353 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001354 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001355 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001356
Simon Glass3e408872013-05-15 06:24:00 +00001357 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
1358 if (DEBUG_PARSER) {
1359 /* use puts - string may be loooong */
1360 puts(cmd ? cmd : "NULL");
1361 puts("\"\n");
1362 }
wdenkc6097192002-11-03 00:24:07 +00001363 clear_ctrlc(); /* forget any previous Control C */
1364
1365 if (!cmd || !*cmd) {
1366 return -1; /* empty command */
1367 }
1368
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001369 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001370 puts ("## Command too long!\n");
1371 return -1;
1372 }
1373
1374 strcpy (cmdbuf, cmd);
1375
1376 /* Process separators and check for invalid
1377 * repeatable commands
1378 */
1379
Simon Glass3e408872013-05-15 06:24:00 +00001380 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
wdenkc6097192002-11-03 00:24:07 +00001381 while (*str) {
1382
1383 /*
1384 * Find separator, or string end
1385 * Allow simple escape of ';' by writing "\;"
1386 */
wdenka25f8622003-01-02 23:57:29 +00001387 for (inquotes = 0, sep = str; *sep; sep++) {
1388 if ((*sep=='\'') &&
1389 (*(sep-1) != '\\'))
1390 inquotes=!inquotes;
1391
1392 if (!inquotes &&
1393 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001394 ( sep != str) && /* past string start */
1395 (*(sep-1) != '\\')) /* and NOT escaped */
1396 break;
1397 }
1398
1399 /*
1400 * Limit the token to data between separators
1401 */
1402 token = str;
1403 if (*sep) {
1404 str = sep + 1; /* start of command for next pass */
1405 *sep = '\0';
1406 }
1407 else
1408 str = sep; /* no more commands for next pass */
Simon Glass3e408872013-05-15 06:24:00 +00001409 debug_parser("token: \"%s\"\n", token);
wdenkc6097192002-11-03 00:24:07 +00001410
1411 /* find macros in this token and replace them */
1412 process_macros (token, finaltoken);
1413
1414 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001415 if ((argc = parse_line (finaltoken, argv)) == 0) {
1416 rc = -1; /* no command at all */
1417 continue;
1418 }
wdenkc6097192002-11-03 00:24:07 +00001419
Richard Genoud34765e82012-12-03 06:28:28 +00001420 if (cmd_process(flag, argc, argv, &repeatable, NULL))
Timo Ketola030fca52012-04-22 23:57:27 +00001421 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001422
1423 /* Did the user stop this? */
1424 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001425 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001426 }
1427
wdenkf07771c2003-05-28 08:06:31 +00001428 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001429}
Simon Glass7fed89e2012-02-14 19:59:22 +00001430#endif
wdenkc6097192002-11-03 00:24:07 +00001431
Simon Glass53071532012-02-14 19:59:21 +00001432/*
1433 * Run a command using the selected parser.
1434 *
1435 * @param cmd Command to run
1436 * @param flag Execution flags (CMD_FLAG_...)
1437 * @return 0 on success, or != 0 on error.
1438 */
1439int run_command(const char *cmd, int flag)
1440{
1441#ifndef CONFIG_SYS_HUSH_PARSER
1442 /*
1443 * builtin_run_command can return 0 or 1 for success, so clean up
1444 * its result.
1445 */
1446 if (builtin_run_command(cmd, flag) == -1)
1447 return 1;
1448
1449 return 0;
1450#else
1451 return parse_string_outer(cmd,
1452 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1453#endif
1454}
1455
Simon Glassd51004a2012-03-30 21:30:55 +00001456#ifndef CONFIG_SYS_HUSH_PARSER
1457/**
1458 * Execute a list of command separated by ; or \n using the built-in parser.
1459 *
1460 * This function cannot take a const char * for the command, since if it
1461 * finds newlines in the string, it replaces them with \0.
1462 *
1463 * @param cmd String containing list of commands
1464 * @param flag Execution flags (CMD_FLAG_...)
1465 * @return 0 on success, or != 0 on error.
1466 */
1467static int builtin_run_command_list(char *cmd, int flag)
1468{
1469 char *line, *next;
1470 int rcode = 0;
1471
1472 /*
1473 * Break into individual lines, and execute each line; terminate on
1474 * error.
1475 */
1476 line = next = cmd;
1477 while (*next) {
1478 if (*next == '\n') {
1479 *next = '\0';
1480 /* run only non-empty commands */
1481 if (*line) {
1482 debug("** exec: \"%s\"\n", line);
1483 if (builtin_run_command(line, 0) < 0) {
1484 rcode = 1;
1485 break;
1486 }
1487 }
1488 line = next + 1;
1489 }
1490 ++next;
1491 }
1492 if (rcode == 0 && *line)
1493 rcode = (builtin_run_command(line, 0) >= 0);
1494
1495 return rcode;
1496}
1497#endif
1498
1499int run_command_list(const char *cmd, int len, int flag)
1500{
1501 int need_buff = 1;
1502 char *buff = (char *)cmd; /* cast away const */
1503 int rcode = 0;
1504
1505 if (len == -1) {
1506 len = strlen(cmd);
1507#ifdef CONFIG_SYS_HUSH_PARSER
1508 /* hush will never change our string */
1509 need_buff = 0;
1510#else
1511 /* the built-in parser will change our string if it sees \n */
1512 need_buff = strchr(cmd, '\n') != NULL;
1513#endif
1514 }
1515 if (need_buff) {
1516 buff = malloc(len + 1);
1517 if (!buff)
1518 return 1;
1519 memcpy(buff, cmd, len);
1520 buff[len] = '\0';
1521 }
1522#ifdef CONFIG_SYS_HUSH_PARSER
1523 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1524#else
1525 /*
1526 * This function will overwrite any \n it sees with a \0, which
1527 * is why it can't work with a const char *. Here we are making
1528 * using of internal knowledge of this function, to avoid always
1529 * doing a malloc() which is actually required only in a case that
1530 * is pretty rare.
1531 */
1532 rcode = builtin_run_command_list(buff, flag);
1533 if (need_buff)
1534 free(buff);
1535#endif
1536
1537 return rcode;
1538}
1539
wdenkc6097192002-11-03 00:24:07 +00001540/****************************************************************************/
1541
Jon Loeligerc3517f92007-07-08 18:10:08 -05001542#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001543int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001544{
1545 int i;
wdenkc6097192002-11-03 00:24:07 +00001546
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001547 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001548 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001549
1550 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001551 char *arg;
1552
1553 if ((arg = getenv (argv[i])) == NULL) {
1554 printf ("## Error: \"%s\" not defined\n", argv[i]);
1555 return 1;
1556 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001557
Simon Glass009dde12012-02-14 19:59:20 +00001558 if (run_command(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001559 return 1;
wdenkc6097192002-11-03 00:24:07 +00001560 }
wdenk3e386912003-04-05 00:53:31 +00001561 return 0;
wdenkc6097192002-11-03 00:24:07 +00001562}
Jon Loeliger90253172007-07-10 11:02:44 -05001563#endif