blob: 159b8e14519fcbd1b15c4eed491b4d9dcff07a54 [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>
31#include <watchdog.h>
32#include <command.h>
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000033#include <fdtdec.h>
Simon Glassd51004a2012-03-30 21:30:55 +000034#include <malloc.h>
Andreas Bießmann09c2e902011-07-18 20:24:04 +020035#include <version.h>
Wolfgang Denkd9631ec2005-09-30 15:18:23 +020036#ifdef CONFIG_MODEM_SUPPORT
37#include <malloc.h> /* for free() prototype */
38#endif
wdenkc6097192002-11-03 00:24:07 +000039
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020040#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +000041#include <hush.h>
42#endif
43
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000044#ifdef CONFIG_OF_CONTROL
45#include <fdtdec.h>
46#endif
47
wdenkbdccc4f2003-08-05 17:43:17 +000048#include <post.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000049#include <linux/ctype.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000050#include <menu.h>
wdenkbdccc4f2003-08-05 17:43:17 +000051
Wolfgang Denkd87080b2006-03-31 18:32:53 +020052DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denkd87080b2006-03-31 18:32:53 +020053
Heiko Schocherfad63402007-07-13 09:54:17 +020054/*
55 * Board-specific Platform code can reimplement show_boot_progress () if needed
56 */
57void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050058void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020059
wdenkc6097192002-11-03 00:24:07 +000060#define MAX_DELAY_STOP_STR 32
61
wdenkc6097192002-11-03 00:24:07 +000062#undef DEBUG_PARSER
63
John Schmoller6475b9f2010-03-12 09:49:23 -060064char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000065
Stefan Roese3ca91222006-07-27 16:11:19 +020066static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-12-22 09:40:45 -050067static const char erase_seq[] = "\b \b"; /* erase sequence */
68static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000069
70#ifdef CONFIG_BOOT_RETRY_TIME
71static uint64_t endtime = 0; /* must be set, default is instant timeout */
72static int retry_time = -1; /* -1 so can call readline before main_loop */
73#endif
74
75#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
76
77#ifndef CONFIG_BOOT_RETRY_MIN
78#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
79#endif
80
81#ifdef CONFIG_MODEM_SUPPORT
82int do_mdm_init = 0;
83extern void mdm_init(void); /* defined in board.c */
84#endif
85
86/***************************************************************************
87 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000088 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000089 */
Joe Hershbergerd5145442013-02-08 10:28:00 +000090#if defined(CONFIG_BOOTDELAY)
wdenkc6097192002-11-03 00:24:07 +000091# if defined(CONFIG_AUTOBOOT_KEYED)
Simon Glass063ae002013-05-15 06:23:55 +000092static int abortboot_keyed(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000093{
94 int abort = 0;
95 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +020096 struct {
wdenkc6097192002-11-03 00:24:07 +000097 char* str;
98 u_int len;
99 int retry;
100 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200101 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000102 { str: getenv ("bootdelaykey"), retry: 1 },
103 { str: getenv ("bootdelaykey2"), retry: 1 },
104 { str: getenv ("bootstopkey"), retry: 0 },
105 { str: getenv ("bootstopkey2"), retry: 0 },
106 };
107
108 char presskey [MAX_DELAY_STOP_STR];
109 u_int presskey_len = 0;
110 u_int presskey_max = 0;
111 u_int i;
112
Dirk Eibacha5aae0a2012-04-26 01:49:33 +0000113#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
114 if (bootdelay == 0)
115 return 0;
116#endif
117
wdenkc6097192002-11-03 00:24:07 +0000118# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200119 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000120# endif
121
122# ifdef CONFIG_AUTOBOOT_DELAY_STR
123 if (delaykey[0].str == NULL)
124 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
125# endif
126# ifdef CONFIG_AUTOBOOT_DELAY_STR2
127 if (delaykey[1].str == NULL)
128 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
129# endif
130# ifdef CONFIG_AUTOBOOT_STOP_STR
131 if (delaykey[2].str == NULL)
132 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
133# endif
134# ifdef CONFIG_AUTOBOOT_STOP_STR2
135 if (delaykey[3].str == NULL)
136 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
137# endif
138
139 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
140 delaykey[i].len = delaykey[i].str == NULL ?
141 0 : strlen (delaykey[i].str);
142 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
143 MAX_DELAY_STOP_STR : delaykey[i].len;
144
145 presskey_max = presskey_max > delaykey[i].len ?
146 presskey_max : delaykey[i].len;
147
148# if DEBUG_BOOTKEYS
149 printf("%s key:<%s>\n",
150 delaykey[i].retry ? "delay" : "stop",
151 delaykey[i].str ? delaykey[i].str : "NULL");
152# endif
153 }
154
155 /* In order to keep up with incoming data, check timeout only
156 * when catch up.
157 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100158 do {
159 if (tstc()) {
160 if (presskey_len < presskey_max) {
161 presskey [presskey_len ++] = getc();
162 }
163 else {
164 for (i = 0; i < presskey_max - 1; i ++)
165 presskey [i] = presskey [i + 1];
166
167 presskey [i] = getc();
168 }
169 }
170
wdenkc6097192002-11-03 00:24:07 +0000171 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
172 if (delaykey[i].len > 0 &&
173 presskey_len >= delaykey[i].len &&
174 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000175 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000176 delaykey[i].len) == 0) {
177# if DEBUG_BOOTKEYS
178 printf("got %skey\n",
179 delaykey[i].retry ? "delay" : "stop");
180# endif
181
182# ifdef CONFIG_BOOT_RETRY_TIME
183 /* don't retry auto boot */
184 if (! delaykey[i].retry)
185 retry_time = -1;
186# endif
187 abort = 1;
188 }
189 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100190 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000191
wdenkc6097192002-11-03 00:24:07 +0000192# if DEBUG_BOOTKEYS
193 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200194 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000195# endif
196
dzu8cb81432003-10-24 13:14:45 +0000197#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200198 if (abort)
199 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000200#endif
201
wdenkc6097192002-11-03 00:24:07 +0000202 return abort;
203}
204
205# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
206
wdenkc7de8292002-11-19 11:04:11 +0000207#ifdef CONFIG_MENUKEY
208static int menukey = 0;
209#endif
210
Simon Glass063ae002013-05-15 06:23:55 +0000211static int abortboot_normal(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000212{
213 int abort = 0;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000214 unsigned long ts;
wdenkc6097192002-11-03 00:24:07 +0000215
wdenkc7de8292002-11-19 11:04:11 +0000216#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200217 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000218#else
Joe Hershberger93d72122012-08-17 10:53:12 +0000219 if (bootdelay >= 0)
220 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000221#endif
wdenkc6097192002-11-03 00:24:07 +0000222
223#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000224 /*
225 * Check if key already pressed
226 * Don't check if bootdelay < 0
227 */
wdenkc6097192002-11-03 00:24:07 +0000228 if (bootdelay >= 0) {
229 if (tstc()) { /* we got a key press */
230 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000231 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200232 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000233 }
wdenk8bde7f72003-06-27 21:31:46 +0000234 }
wdenkc6097192002-11-03 00:24:07 +0000235#endif
236
wdenkf72da342003-10-10 10:05:42 +0000237 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000238 --bootdelay;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000239 /* delay 1000 ms */
240 ts = get_timer(0);
241 do {
wdenkc6097192002-11-03 00:24:07 +0000242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000245# ifdef CONFIG_MENUKEY
246 menukey = getc();
247# else
wdenkc6097192002-11-03 00:24:07 +0000248 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000249# endif
wdenkc6097192002-11-03 00:24:07 +0000250 break;
251 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200252 udelay(10000);
Jim Linb2f3e0e2013-01-24 01:05:55 +0000253 } while (!abort && get_timer(ts) < 1000);
wdenkc6097192002-11-03 00:24:07 +0000254
Ladislav Michlada4d402007-04-25 16:01:26 +0200255 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000256 }
257
Ladislav Michlada4d402007-04-25 16:01:26 +0200258 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000259
wdenkf72da342003-10-10 10:05:42 +0000260#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200261 if (abort)
262 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000263#endif
264
wdenkc6097192002-11-03 00:24:07 +0000265 return abort;
266}
267# endif /* CONFIG_AUTOBOOT_KEYED */
Simon Glass063ae002013-05-15 06:23:55 +0000268
269static int abortboot(int bootdelay)
270{
271#ifdef CONFIG_AUTOBOOT_KEYED
272 return abortboot_keyed(bootdelay);
273#else
274 return abortboot_normal(bootdelay);
275#endif
276}
Joe Hershbergerd5145442013-02-08 10:28:00 +0000277#endif /* CONFIG_BOOTDELAY */
wdenkc6097192002-11-03 00:24:07 +0000278
Doug Anderson67e1ea22012-10-25 16:31:09 +0000279/*
280 * Runs the given boot command securely. Specifically:
281 * - Doesn't run the command with the shell (run_command or parse_string_outer),
282 * since that's a lot of code surface that an attacker might exploit.
283 * Because of this, we don't do any argument parsing--the secure boot command
284 * has to be a full-fledged u-boot command.
285 * - Doesn't check for keypresses before booting, since that could be a
286 * security hole; also disables Ctrl-C.
287 * - Doesn't allow the command to return.
288 *
289 * Upon any failures, this function will drop into an infinite loop after
290 * printing the error message to console.
291 */
292
Joe Hershbergerd5145442013-02-08 10:28:00 +0000293#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
Doug Anderson67e1ea22012-10-25 16:31:09 +0000294static void secure_boot_cmd(char *cmd)
295{
296 cmd_tbl_t *cmdtp;
297 int rc;
298
299 if (!cmd) {
300 printf("## Error: Secure boot command not specified\n");
301 goto err;
302 }
303
304 /* Disable Ctrl-C just in case some command is used that checks it. */
305 disable_ctrlc(1);
306
307 /* Find the command directly. */
308 cmdtp = find_cmd(cmd);
309 if (!cmdtp) {
310 printf("## Error: \"%s\" not defined\n", cmd);
311 goto err;
312 }
313
314 /* Run the command, forcing no flags and faking argc and argv. */
315 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
316
317 /* Shouldn't ever return from boot command. */
318 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
319
320err:
321 /*
322 * Not a whole lot to do here. Rebooting won't help much, since we'll
323 * just end up right back here. Just loop.
324 */
325 hang();
326}
327
Simon Glassfcabc242012-10-25 16:31:11 +0000328static void process_fdt_options(const void *blob)
329{
330 ulong addr;
331
332 /* Add an env variable to point to a kernel payload, if available */
333 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
334 if (addr)
335 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
336
337 /* Add an env variable to point to a root disk, if available */
338 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
339 if (addr)
340 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
341}
Doug Anderson67e1ea22012-10-25 16:31:09 +0000342#endif /* CONFIG_OF_CONTROL */
343
Simon Glassbc2b4c22013-05-15 06:23:56 +0000344#ifdef CONFIG_BOOTDELAY
345static void process_boot_delay(void)
wdenkc6097192002-11-03 00:24:07 +0000346{
Simon Glassbc2b4c22013-05-15 06:23:56 +0000347#ifdef CONFIG_OF_CONTROL
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000348 char *env;
349#endif
wdenkc6097192002-11-03 00:24:07 +0000350 char *s;
351 int bootdelay;
wdenkbdccc4f2003-08-05 17:43:17 +0000352#ifdef CONFIG_BOOTCOUNT_LIMIT
353 unsigned long bootcount = 0;
354 unsigned long bootlimit = 0;
355 char *bcs;
356 char bcs_set[16];
357#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000358
wdenkbdccc4f2003-08-05 17:43:17 +0000359#ifdef CONFIG_BOOTCOUNT_LIMIT
360 bootcount = bootcount_load();
361 bootcount++;
362 bootcount_store (bootcount);
363 sprintf (bcs_set, "%lu", bootcount);
364 setenv ("bootcount", bcs_set);
365 bcs = getenv ("bootlimit");
366 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
367#endif /* CONFIG_BOOTCOUNT_LIMIT */
368
wdenkc6097192002-11-03 00:24:07 +0000369 s = getenv ("bootdelay");
370 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
371
wdenka6c7ad22002-12-03 21:28:10 +0000372 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000373
Heiko Schocher317d6c52012-01-16 21:13:35 +0000374#if defined(CONFIG_MENU_SHOW)
375 bootdelay = menu_show(bootdelay);
376#endif
wdenkc6097192002-11-03 00:24:07 +0000377# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000378 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000379# endif /* CONFIG_BOOT_RETRY_TIME */
380
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100381#ifdef CONFIG_POST
382 if (gd->flags & GD_FLG_POSTFAIL) {
383 s = getenv("failbootcmd");
384 }
385 else
386#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000387#ifdef CONFIG_BOOTCOUNT_LIMIT
388 if (bootlimit && (bootcount > bootlimit)) {
389 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
390 (unsigned)bootlimit);
391 s = getenv ("altbootcmd");
392 }
393 else
394#endif /* CONFIG_BOOTCOUNT_LIMIT */
395 s = getenv ("bootcmd");
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000396#ifdef CONFIG_OF_CONTROL
397 /* Allow the fdt to override the boot command */
398 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
399 if (env)
400 s = env;
Doug Anderson67e1ea22012-10-25 16:31:09 +0000401
Simon Glassfcabc242012-10-25 16:31:11 +0000402 process_fdt_options(gd->fdt_blob);
403
Doug Anderson67e1ea22012-10-25 16:31:09 +0000404 /*
405 * If the bootsecure option was chosen, use secure_boot_cmd().
406 * Always use 'env' in this case, since bootsecure requres that the
407 * bootcmd was specified in the FDT too.
408 */
409 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
410 secure_boot_cmd(env);
411
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000412#endif /* CONFIG_OF_CONTROL */
wdenka6c7ad22002-12-03 21:28:10 +0000413
414 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
415
Joe Hershberger93d72122012-08-17 10:53:12 +0000416 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
Simon Glassbc2b4c22013-05-15 06:23:56 +0000417#ifdef CONFIG_AUTOBOOT_KEYED
wdenkc6097192002-11-03 00:24:07 +0000418 int prev = disable_ctrlc(1); /* disable Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000419#endif
wdenkc6097192002-11-03 00:24:07 +0000420
Simon Glass3a8a02b2012-03-30 21:30:56 +0000421 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000422
Simon Glassbc2b4c22013-05-15 06:23:56 +0000423#ifdef CONFIG_AUTOBOOT_KEYED
wdenkc6097192002-11-03 00:24:07 +0000424 disable_ctrlc(prev); /* restore Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000425#endif
wdenkc6097192002-11-03 00:24:07 +0000426 }
wdenkc7de8292002-11-19 11:04:11 +0000427
Simon Glassbc2b4c22013-05-15 06:23:56 +0000428#ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000429 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000430 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000431 if (s)
Simon Glass3a8a02b2012-03-30 21:30:56 +0000432 run_command_list(s, -1, 0);
wdenkc7de8292002-11-19 11:04:11 +0000433 }
434#endif /* CONFIG_MENUKEY */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000435}
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200436#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000437
Simon Glassbc2b4c22013-05-15 06:23:56 +0000438void main_loop(void)
439{
440#ifndef CONFIG_SYS_HUSH_PARSER
441 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
442 int len;
443 int rc = 1;
444 int flag;
445#endif
446#ifdef CONFIG_PREBOOT
447 char *p;
448#endif
449
450 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
451
452#ifdef CONFIG_MODEM_SUPPORT
453 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
454 if (do_mdm_init) {
455 char *str = strdup(getenv("mdm_cmd"));
456 setenv("preboot", str); /* set or delete definition */
457 if (str != NULL)
458 free(str);
459 mdm_init(); /* wait for modem connection */
460 }
461#endif /* CONFIG_MODEM_SUPPORT */
462
463#ifdef CONFIG_VERSION_VARIABLE
464 {
465 setenv("ver", version_string); /* set version variable */
466 }
467#endif /* CONFIG_VERSION_VARIABLE */
468
469#ifdef CONFIG_SYS_HUSH_PARSER
470 u_boot_hush_start();
471#endif
472
473#if defined(CONFIG_HUSH_INIT_VAR)
474 hush_init_var();
475#endif
476
477#ifdef CONFIG_PREBOOT
478 p = getenv("preboot");
479 if (p != NULL) {
480# ifdef CONFIG_AUTOBOOT_KEYED
481 int prev = disable_ctrlc(1); /* disable Control C checking */
482# endif
483
484 run_command_list(p, -1, 0);
485
486# ifdef CONFIG_AUTOBOOT_KEYED
487 disable_ctrlc(prev); /* restore Control C checking */
488# endif
489 }
490#endif /* CONFIG_PREBOOT */
491
492#if defined(CONFIG_UPDATE_TFTP)
493 update_tftp(0UL);
494#endif /* CONFIG_UPDATE_TFTP */
495
496#ifdef CONFIG_BOOTDELAY
497 process_boot_delay();
498#endif
wdenkc6097192002-11-03 00:24:07 +0000499 /*
500 * Main Loop for Monitor Command Processing
501 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200502#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000503 parse_file_outer();
504 /* This point is never reached */
505 for (;;);
506#else
507 for (;;) {
508#ifdef CONFIG_BOOT_RETRY_TIME
509 if (rc >= 0) {
510 /* Saw enough of a valid command to
511 * restart the timeout.
512 */
513 reset_cmd_timeout();
514 }
515#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200516 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000517
518 flag = 0; /* assume no special flags for now */
519 if (len > 0)
520 strcpy (lastcommand, console_buffer);
521 else if (len == 0)
522 flag |= CMD_FLAG_REPEAT;
523#ifdef CONFIG_BOOT_RETRY_TIME
524 else if (len == -2) {
525 /* -2 means timed out, retry autoboot
526 */
wdenk4b9206e2004-03-23 22:14:11 +0000527 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000528# ifdef CONFIG_RESET_TO_RETRY
529 /* Reinit board to run initialization code again */
530 do_reset (NULL, 0, 0, NULL);
531# else
532 return; /* retry autoboot */
533# endif
534 }
535#endif
536
537 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000538 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000539 else
Simon Glass53071532012-02-14 19:59:21 +0000540 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000541
542 if (rc <= 0) {
543 /* invalid command or not repeatable, forget it */
544 lastcommand[0] = 0;
545 }
546 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200547#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000548}
549
wdenk6dd652f2003-06-19 23:40:20 +0000550#ifdef CONFIG_BOOT_RETRY_TIME
551/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200552 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000553 */
554void init_cmd_timeout(void)
555{
556 char *s = getenv ("bootretry");
557
558 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000559 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000560 else
561 retry_time = CONFIG_BOOT_RETRY_TIME;
562
563 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
564 retry_time = CONFIG_BOOT_RETRY_MIN;
565}
566
wdenkc6097192002-11-03 00:24:07 +0000567/***************************************************************************
568 * reset command line timeout to retry_time seconds
569 */
wdenkc6097192002-11-03 00:24:07 +0000570void reset_cmd_timeout(void)
571{
572 endtime = endtick(retry_time);
573}
574#endif
575
Wolfgang Denk501090a2006-07-21 11:33:45 +0200576#ifdef CONFIG_CMDLINE_EDITING
577
578/*
579 * cmdline-editing related codes from vivi.
580 * Author: Janghoon Lyu <nandy@mizi.com>
581 */
582
Wolfgang Denk501090a2006-07-21 11:33:45 +0200583#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700584 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200585 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200586
587#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200588#define CTL_BACKSPACE ('\b')
589#define DEL ((char)255)
590#define DEL7 ((char)127)
591#define CREAD_HIST_CHAR ('!')
592
593#define getcmd_putch(ch) putc(ch)
594#define getcmd_getch() getc()
595#define getcmd_cbeep() getcmd_putch('\a')
596
597#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500598#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200599
Kim Phillips199adb62012-10-29 13:34:32 +0000600static int hist_max;
601static int hist_add_idx;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200602static int hist_cur = -1;
Kim Phillips199adb62012-10-29 13:34:32 +0000603static unsigned hist_num;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200604
Kim Phillips199adb62012-10-29 13:34:32 +0000605static char *hist_list[HIST_MAX];
606static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200607
608#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
609
610static void hist_init(void)
611{
612 int i;
613
614 hist_max = 0;
615 hist_add_idx = 0;
616 hist_cur = -1;
617 hist_num = 0;
618
619 for (i = 0; i < HIST_MAX; i++) {
620 hist_list[i] = hist_lines[i];
621 hist_list[i][0] = '\0';
622 }
623}
624
625static void cread_add_to_hist(char *line)
626{
627 strcpy(hist_list[hist_add_idx], line);
628
629 if (++hist_add_idx >= HIST_MAX)
630 hist_add_idx = 0;
631
632 if (hist_add_idx > hist_max)
633 hist_max = hist_add_idx;
634
635 hist_num++;
636}
637
638static char* hist_prev(void)
639{
640 char *ret;
641 int old_cur;
642
643 if (hist_cur < 0)
644 return NULL;
645
646 old_cur = hist_cur;
647 if (--hist_cur < 0)
648 hist_cur = hist_max;
649
650 if (hist_cur == hist_add_idx) {
651 hist_cur = old_cur;
652 ret = NULL;
653 } else
654 ret = hist_list[hist_cur];
655
656 return (ret);
657}
658
659static char* hist_next(void)
660{
661 char *ret;
662
663 if (hist_cur < 0)
664 return NULL;
665
666 if (hist_cur == hist_add_idx)
667 return NULL;
668
669 if (++hist_cur > hist_max)
670 hist_cur = 0;
671
672 if (hist_cur == hist_add_idx) {
673 ret = "";
674 } else
675 ret = hist_list[hist_cur];
676
677 return (ret);
678}
679
Stefan Roese3ca91222006-07-27 16:11:19 +0200680#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200681static void cread_print_hist_list(void)
682{
683 int i;
684 unsigned long n;
685
686 n = hist_num - hist_max;
687
688 i = hist_add_idx + 1;
689 while (1) {
690 if (i > hist_max)
691 i = 0;
692 if (i == hist_add_idx)
693 break;
694 printf("%s\n", hist_list[i]);
695 n++;
696 i++;
697 }
698}
Stefan Roese3ca91222006-07-27 16:11:19 +0200699#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200700
701#define BEGINNING_OF_LINE() { \
702 while (num) { \
703 getcmd_putch(CTL_BACKSPACE); \
704 num--; \
705 } \
706}
707
708#define ERASE_TO_EOL() { \
709 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400710 printf("%*s", (int)(eol_num - num), ""); \
711 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200712 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400713 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200714 } \
715}
716
717#define REFRESH_TO_EOL() { \
718 if (num < eol_num) { \
719 wlen = eol_num - num; \
720 putnstr(buf + num, wlen); \
721 num = eol_num; \
722 } \
723}
724
725static void cread_add_char(char ichar, int insert, unsigned long *num,
726 unsigned long *eol_num, char *buf, unsigned long len)
727{
728 unsigned long wlen;
729
730 /* room ??? */
731 if (insert || *num == *eol_num) {
732 if (*eol_num > len - 1) {
733 getcmd_cbeep();
734 return;
735 }
736 (*eol_num)++;
737 }
738
739 if (insert) {
740 wlen = *eol_num - *num;
741 if (wlen > 1) {
742 memmove(&buf[*num+1], &buf[*num], wlen-1);
743 }
744
745 buf[*num] = ichar;
746 putnstr(buf + *num, wlen);
747 (*num)++;
748 while (--wlen) {
749 getcmd_putch(CTL_BACKSPACE);
750 }
751 } else {
752 /* echo the character */
753 wlen = 1;
754 buf[*num] = ichar;
755 putnstr(buf + *num, wlen);
756 (*num)++;
757 }
758}
759
760static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
761 unsigned long *eol_num, char *buf, unsigned long len)
762{
763 while (strsize--) {
764 cread_add_char(*str, insert, num, eol_num, buf, len);
765 str++;
766 }
767}
768
Heiko Schocher9c348312012-01-16 21:13:05 +0000769static int cread_line(const char *const prompt, char *buf, unsigned int *len,
770 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200771{
772 unsigned long num = 0;
773 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200774 unsigned long wlen;
775 char ichar;
776 int insert = 1;
777 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200778 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500779 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000780 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500781
782 if (init_len)
783 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200784
785 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100786#ifdef CONFIG_BOOT_RETRY_TIME
787 while (!tstc()) { /* while no incoming data */
788 if (retry_time >= 0 && get_ticks() > endtime)
789 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200790 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100791 }
792#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000793 if (first && timeout) {
794 uint64_t etime = endtick(timeout);
795
796 while (!tstc()) { /* while no incoming data */
797 if (get_ticks() >= etime)
798 return -2; /* timed out */
799 WATCHDOG_RESET();
800 }
801 first = 0;
802 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100803
Wolfgang Denk501090a2006-07-21 11:33:45 +0200804 ichar = getcmd_getch();
805
806 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200807 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200808 break;
809 }
810
811 /*
812 * handle standard linux xterm esc sequences for arrow key, etc.
813 */
814 if (esc_len != 0) {
815 if (esc_len == 1) {
816 if (ichar == '[') {
817 esc_save[esc_len] = ichar;
818 esc_len = 2;
819 } else {
820 cread_add_str(esc_save, esc_len, insert,
821 &num, &eol_num, buf, *len);
822 esc_len = 0;
823 }
824 continue;
825 }
826
827 switch (ichar) {
828
829 case 'D': /* <- key */
830 ichar = CTL_CH('b');
831 esc_len = 0;
832 break;
833 case 'C': /* -> key */
834 ichar = CTL_CH('f');
835 esc_len = 0;
836 break; /* pass off to ^F handler */
837 case 'H': /* Home key */
838 ichar = CTL_CH('a');
839 esc_len = 0;
840 break; /* pass off to ^A handler */
841 case 'A': /* up arrow */
842 ichar = CTL_CH('p');
843 esc_len = 0;
844 break; /* pass off to ^P handler */
845 case 'B': /* down arrow */
846 ichar = CTL_CH('n');
847 esc_len = 0;
848 break; /* pass off to ^N handler */
849 default:
850 esc_save[esc_len++] = ichar;
851 cread_add_str(esc_save, esc_len, insert,
852 &num, &eol_num, buf, *len);
853 esc_len = 0;
854 continue;
855 }
856 }
857
858 switch (ichar) {
859 case 0x1b:
860 if (esc_len == 0) {
861 esc_save[esc_len] = ichar;
862 esc_len = 1;
863 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200864 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200865 esc_len = 0;
866 }
867 break;
868
869 case CTL_CH('a'):
870 BEGINNING_OF_LINE();
871 break;
872 case CTL_CH('c'): /* ^C - break */
873 *buf = '\0'; /* discard input */
874 return (-1);
875 case CTL_CH('f'):
876 if (num < eol_num) {
877 getcmd_putch(buf[num]);
878 num++;
879 }
880 break;
881 case CTL_CH('b'):
882 if (num) {
883 getcmd_putch(CTL_BACKSPACE);
884 num--;
885 }
886 break;
887 case CTL_CH('d'):
888 if (num < eol_num) {
889 wlen = eol_num - num - 1;
890 if (wlen) {
891 memmove(&buf[num], &buf[num+1], wlen);
892 putnstr(buf + num, wlen);
893 }
894
895 getcmd_putch(' ');
896 do {
897 getcmd_putch(CTL_BACKSPACE);
898 } while (wlen--);
899 eol_num--;
900 }
901 break;
902 case CTL_CH('k'):
903 ERASE_TO_EOL();
904 break;
905 case CTL_CH('e'):
906 REFRESH_TO_EOL();
907 break;
908 case CTL_CH('o'):
909 insert = !insert;
910 break;
911 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100912 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200913 BEGINNING_OF_LINE();
914 ERASE_TO_EOL();
915 break;
916 case DEL:
917 case DEL7:
918 case 8:
919 if (num) {
920 wlen = eol_num - num;
921 num--;
922 memmove(&buf[num], &buf[num+1], wlen);
923 getcmd_putch(CTL_BACKSPACE);
924 putnstr(buf + num, wlen);
925 getcmd_putch(' ');
926 do {
927 getcmd_putch(CTL_BACKSPACE);
928 } while (wlen--);
929 eol_num--;
930 }
931 break;
932 case CTL_CH('p'):
933 case CTL_CH('n'):
934 {
935 char * hline;
936
937 esc_len = 0;
938
939 if (ichar == CTL_CH('p'))
940 hline = hist_prev();
941 else
942 hline = hist_next();
943
944 if (!hline) {
945 getcmd_cbeep();
946 continue;
947 }
948
949 /* nuke the current line */
950 /* first, go home */
951 BEGINNING_OF_LINE();
952
953 /* erase to end of line */
954 ERASE_TO_EOL();
955
956 /* copy new line into place and display */
957 strcpy(buf, hline);
958 eol_num = strlen(buf);
959 REFRESH_TO_EOL();
960 continue;
961 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100962#ifdef CONFIG_AUTO_COMPLETE
963 case '\t': {
964 int num2, col;
965
966 /* do not autocomplete when in the middle */
967 if (num < eol_num) {
968 getcmd_cbeep();
969 break;
970 }
971
972 buf[num] = '\0';
973 col = strlen(prompt) + eol_num;
974 num2 = num;
975 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
976 col = num2 - num;
977 num += col;
978 eol_num += col;
979 }
980 break;
981 }
982#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200983 default:
984 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
985 break;
986 }
987 }
988 *len = eol_num;
989 buf[eol_num] = '\0'; /* lose the newline */
990
991 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
992 cread_add_to_hist(buf);
993 hist_cur = hist_add_idx;
994
Peter Tyserf9239432009-10-25 15:12:53 -0500995 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200996}
997
998#endif /* CONFIG_CMDLINE_EDITING */
999
wdenkc6097192002-11-03 00:24:07 +00001000/****************************************************************************/
1001
1002/*
1003 * Prompt for input and read a line.
1004 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
1005 * time out when time goes past endtime (timebase time in ticks).
1006 * Return: number of read characters
1007 * -1 if break
1008 * -2 if timed out
1009 */
1010int readline (const char *const prompt)
1011{
Peter Tyserecc55002009-10-25 15:12:54 -05001012 /*
1013 * If console_buffer isn't 0-length the user will be prompted to modify
1014 * it instead of entering it from scratch as desired.
1015 */
1016 console_buffer[0] = '\0';
1017
Heiko Schocher9c348312012-01-16 21:13:05 +00001018 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -06001019}
1020
1021
Heiko Schocher9c348312012-01-16 21:13:05 +00001022int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -06001023{
1024 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001025#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -05001026 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +02001027 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001028 static int initted = 0;
1029
James Yang597f6c22008-05-05 10:22:53 -05001030 /*
1031 * History uses a global array which is not
1032 * writable until after relocation to RAM.
1033 * Revert to non-history version if still
1034 * running from flash.
1035 */
1036 if (gd->flags & GD_FLG_RELOC) {
1037 if (!initted) {
1038 hist_init();
1039 initted = 1;
1040 }
1041
Peter Tysere491a712009-10-25 15:12:52 -05001042 if (prompt)
1043 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -05001044
Heiko Schocher9c348312012-01-16 21:13:05 +00001045 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -05001046 return rc < 0 ? rc : len;
1047
1048 } else {
1049#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -06001050 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +00001051 int n = 0; /* buffer index */
1052 int plen = 0; /* prompt length */
1053 int col; /* output column cnt */
1054 char c;
1055
1056 /* print prompt */
1057 if (prompt) {
1058 plen = strlen (prompt);
1059 puts (prompt);
1060 }
1061 col = plen;
1062
1063 for (;;) {
1064#ifdef CONFIG_BOOT_RETRY_TIME
1065 while (!tstc()) { /* while no incoming data */
1066 if (retry_time >= 0 && get_ticks() > endtime)
1067 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +02001068 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001069 }
1070#endif
1071 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1072
1073#ifdef CONFIG_SHOW_ACTIVITY
1074 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +00001075 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +02001076 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001077 }
1078#endif
1079 c = getc();
1080
1081 /*
1082 * Special character handling
1083 */
1084 switch (c) {
1085 case '\r': /* Enter */
1086 case '\n':
1087 *p = '\0';
1088 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -06001089 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001090
wdenk27aa8182004-03-23 22:37:33 +00001091 case '\0': /* nul */
1092 continue;
1093
wdenkc6097192002-11-03 00:24:07 +00001094 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001095 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001096 return (-1);
1097
1098 case 0x15: /* ^U - erase line */
1099 while (col > plen) {
1100 puts (erase_seq);
1101 --col;
1102 }
James Yang6636b622008-01-09 11:17:49 -06001103 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001104 n = 0;
1105 continue;
1106
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001107 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001108 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001109 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001110 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001111 }
1112 continue;
1113
1114 case 0x08: /* ^H - backspace */
1115 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001116 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001117 continue;
1118
1119 default:
1120 /*
1121 * Must be a normal character then
1122 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001123 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001124 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001125#ifdef CONFIG_AUTO_COMPLETE
1126 /* if auto completion triggered just continue */
1127 *p = '\0';
1128 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001129 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001130 continue;
1131 }
1132#endif
wdenkc6097192002-11-03 00:24:07 +00001133 puts (tab_seq+(col&07));
1134 col += 8 - (col&07);
1135 } else {
Simon Glass9a8efc42012-10-30 13:40:18 +00001136 char buf[2];
1137
1138 /*
1139 * Echo input using puts() to force am
1140 * LCD flush if we are using an LCD
1141 */
1142 ++col;
1143 buf[0] = c;
1144 buf[1] = '\0';
1145 puts(buf);
wdenkc6097192002-11-03 00:24:07 +00001146 }
1147 *p++ = c;
1148 ++n;
1149 } else { /* Buffer full */
1150 putc ('\a');
1151 }
1152 }
1153 }
James Yang597f6c22008-05-05 10:22:53 -05001154#ifdef CONFIG_CMDLINE_EDITING
1155 }
1156#endif
wdenkc6097192002-11-03 00:24:07 +00001157}
1158
1159/****************************************************************************/
1160
1161static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1162{
1163 char *s;
1164
1165 if (*np == 0) {
1166 return (p);
1167 }
1168
1169 if (*(--p) == '\t') { /* will retype the whole line */
1170 while (*colp > plen) {
1171 puts (erase_seq);
1172 (*colp)--;
1173 }
1174 for (s=buffer; s<p; ++s) {
1175 if (*s == '\t') {
1176 puts (tab_seq+((*colp) & 07));
1177 *colp += 8 - ((*colp) & 07);
1178 } else {
1179 ++(*colp);
1180 putc (*s);
1181 }
1182 }
1183 } else {
1184 puts (erase_seq);
1185 (*colp)--;
1186 }
1187 (*np)--;
1188 return (p);
1189}
1190
1191/****************************************************************************/
1192
1193int parse_line (char *line, char *argv[])
1194{
1195 int nargs = 0;
1196
1197#ifdef DEBUG_PARSER
1198 printf ("parse_line: \"%s\"\n", line);
1199#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001200 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001201
1202 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001203 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001204 ++line;
wdenkc6097192002-11-03 00:24:07 +00001205
1206 if (*line == '\0') { /* end of line, no more args */
1207 argv[nargs] = NULL;
1208#ifdef DEBUG_PARSER
1209 printf ("parse_line: nargs=%d\n", nargs);
1210#endif
1211 return (nargs);
1212 }
1213
1214 argv[nargs++] = line; /* begin of argument string */
1215
1216 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001217 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001218 ++line;
wdenkc6097192002-11-03 00:24:07 +00001219
1220 if (*line == '\0') { /* end of line, no more args */
1221 argv[nargs] = NULL;
1222#ifdef DEBUG_PARSER
1223 printf ("parse_line: nargs=%d\n", nargs);
1224#endif
1225 return (nargs);
1226 }
1227
1228 *line++ = '\0'; /* terminate current arg */
1229 }
1230
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001231 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001232
1233#ifdef DEBUG_PARSER
1234 printf ("parse_line: nargs=%d\n", nargs);
1235#endif
1236 return (nargs);
1237}
1238
1239/****************************************************************************/
1240
Simon Glass7fed89e2012-02-14 19:59:22 +00001241#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001242static void process_macros (const char *input, char *output)
1243{
1244 char c, prev;
1245 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001246 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001247 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001248 int state = 0; /* 0 = waiting for '$' */
1249
1250 /* 1 = waiting for '(' or '{' */
1251 /* 2 = waiting for ')' or '}' */
1252 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001253#ifdef DEBUG_PARSER
1254 char *output_start = output;
1255
Wolfgang Denk19973b62006-10-28 00:38:39 +02001256 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1257 input);
wdenkc6097192002-11-03 00:24:07 +00001258#endif
1259
Wolfgang Denk19973b62006-10-28 00:38:39 +02001260 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001261
1262 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001263 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001264 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001265
Wolfgang Denk19973b62006-10-28 00:38:39 +02001266 if (state != 3) {
1267 /* remove one level of escape characters */
1268 if ((c == '\\') && (prev != '\\')) {
1269 if (inputcnt-- == 0)
1270 break;
1271 prev = c;
1272 c = *input++;
1273 }
wdenka25f8622003-01-02 23:57:29 +00001274 }
wdenkc6097192002-11-03 00:24:07 +00001275
Wolfgang Denk19973b62006-10-28 00:38:39 +02001276 switch (state) {
1277 case 0: /* Waiting for (unescaped) $ */
1278 if ((c == '\'') && (prev != '\\')) {
1279 state = 3;
1280 break;
1281 }
1282 if ((c == '$') && (prev != '\\')) {
1283 state++;
1284 } else {
wdenkc6097192002-11-03 00:24:07 +00001285 *(output++) = c;
1286 outputcnt--;
1287 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001288 break;
1289 case 1: /* Waiting for ( */
1290 if (c == '(' || c == '{') {
1291 state++;
1292 varname_start = input;
1293 } else {
1294 state = 0;
1295 *(output++) = '$';
1296 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001297
Wolfgang Denk19973b62006-10-28 00:38:39 +02001298 if (outputcnt) {
1299 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001300 outputcnt--;
1301 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001302 }
1303 break;
1304 case 2: /* Waiting for ) */
1305 if (c == ')' || c == '}') {
1306 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001307 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001308 int envcnt = input - varname_start - 1; /* Varname # of chars */
1309
1310 /* Get the varname */
1311 for (i = 0; i < envcnt; i++) {
1312 envname[i] = varname_start[i];
1313 }
1314 envname[i] = 0;
1315
1316 /* Get its value */
1317 envval = getenv (envname);
1318
1319 /* Copy into the line if it exists */
1320 if (envval != NULL)
1321 while ((*envval) && outputcnt) {
1322 *(output++) = *(envval++);
1323 outputcnt--;
1324 }
1325 /* Look for another '$' */
1326 state = 0;
1327 }
1328 break;
1329 case 3: /* Waiting for ' */
1330 if ((c == '\'') && (prev != '\\')) {
1331 state = 0;
1332 } else {
1333 *(output++) = c;
1334 outputcnt--;
1335 }
1336 break;
wdenkc6097192002-11-03 00:24:07 +00001337 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001338 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001339 }
1340
1341 if (outputcnt)
1342 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001343 else
1344 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001345
1346#ifdef DEBUG_PARSER
1347 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001348 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001349#endif
1350}
1351
1352/****************************************************************************
1353 * returns:
1354 * 1 - command executed, repeatable
1355 * 0 - command executed but not repeatable, interrupted commands are
1356 * always considered not repeatable
1357 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001358 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001359 * considered unrecognized)
1360 *
1361 * WARNING:
1362 *
1363 * We must create a temporary copy of the command since the command we get
1364 * may be the result from getenv(), which returns a pointer directly to
1365 * the environment data, which may change magicly when the command we run
1366 * creates or modifies environment variables (like "bootp" does).
1367 */
Simon Glass53071532012-02-14 19:59:21 +00001368static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001369{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001370 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001371 char *token; /* start of token in cmdbuf */
1372 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001373 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001374 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001375 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001376 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001377 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001378 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001379
1380#ifdef DEBUG_PARSER
1381 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1382 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1383 puts ("\"\n");
1384#endif
1385
1386 clear_ctrlc(); /* forget any previous Control C */
1387
1388 if (!cmd || !*cmd) {
1389 return -1; /* empty command */
1390 }
1391
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001392 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001393 puts ("## Command too long!\n");
1394 return -1;
1395 }
1396
1397 strcpy (cmdbuf, cmd);
1398
1399 /* Process separators and check for invalid
1400 * repeatable commands
1401 */
1402
1403#ifdef DEBUG_PARSER
1404 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1405#endif
1406 while (*str) {
1407
1408 /*
1409 * Find separator, or string end
1410 * Allow simple escape of ';' by writing "\;"
1411 */
wdenka25f8622003-01-02 23:57:29 +00001412 for (inquotes = 0, sep = str; *sep; sep++) {
1413 if ((*sep=='\'') &&
1414 (*(sep-1) != '\\'))
1415 inquotes=!inquotes;
1416
1417 if (!inquotes &&
1418 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001419 ( sep != str) && /* past string start */
1420 (*(sep-1) != '\\')) /* and NOT escaped */
1421 break;
1422 }
1423
1424 /*
1425 * Limit the token to data between separators
1426 */
1427 token = str;
1428 if (*sep) {
1429 str = sep + 1; /* start of command for next pass */
1430 *sep = '\0';
1431 }
1432 else
1433 str = sep; /* no more commands for next pass */
1434#ifdef DEBUG_PARSER
1435 printf ("token: \"%s\"\n", token);
1436#endif
1437
1438 /* find macros in this token and replace them */
1439 process_macros (token, finaltoken);
1440
1441 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001442 if ((argc = parse_line (finaltoken, argv)) == 0) {
1443 rc = -1; /* no command at all */
1444 continue;
1445 }
wdenkc6097192002-11-03 00:24:07 +00001446
Richard Genoud34765e82012-12-03 06:28:28 +00001447 if (cmd_process(flag, argc, argv, &repeatable, NULL))
Timo Ketola030fca52012-04-22 23:57:27 +00001448 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001449
1450 /* Did the user stop this? */
1451 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001452 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001453 }
1454
wdenkf07771c2003-05-28 08:06:31 +00001455 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001456}
Simon Glass7fed89e2012-02-14 19:59:22 +00001457#endif
wdenkc6097192002-11-03 00:24:07 +00001458
Simon Glass53071532012-02-14 19:59:21 +00001459/*
1460 * Run a command using the selected parser.
1461 *
1462 * @param cmd Command to run
1463 * @param flag Execution flags (CMD_FLAG_...)
1464 * @return 0 on success, or != 0 on error.
1465 */
1466int run_command(const char *cmd, int flag)
1467{
1468#ifndef CONFIG_SYS_HUSH_PARSER
1469 /*
1470 * builtin_run_command can return 0 or 1 for success, so clean up
1471 * its result.
1472 */
1473 if (builtin_run_command(cmd, flag) == -1)
1474 return 1;
1475
1476 return 0;
1477#else
1478 return parse_string_outer(cmd,
1479 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1480#endif
1481}
1482
Simon Glassd51004a2012-03-30 21:30:55 +00001483#ifndef CONFIG_SYS_HUSH_PARSER
1484/**
1485 * Execute a list of command separated by ; or \n using the built-in parser.
1486 *
1487 * This function cannot take a const char * for the command, since if it
1488 * finds newlines in the string, it replaces them with \0.
1489 *
1490 * @param cmd String containing list of commands
1491 * @param flag Execution flags (CMD_FLAG_...)
1492 * @return 0 on success, or != 0 on error.
1493 */
1494static int builtin_run_command_list(char *cmd, int flag)
1495{
1496 char *line, *next;
1497 int rcode = 0;
1498
1499 /*
1500 * Break into individual lines, and execute each line; terminate on
1501 * error.
1502 */
1503 line = next = cmd;
1504 while (*next) {
1505 if (*next == '\n') {
1506 *next = '\0';
1507 /* run only non-empty commands */
1508 if (*line) {
1509 debug("** exec: \"%s\"\n", line);
1510 if (builtin_run_command(line, 0) < 0) {
1511 rcode = 1;
1512 break;
1513 }
1514 }
1515 line = next + 1;
1516 }
1517 ++next;
1518 }
1519 if (rcode == 0 && *line)
1520 rcode = (builtin_run_command(line, 0) >= 0);
1521
1522 return rcode;
1523}
1524#endif
1525
1526int run_command_list(const char *cmd, int len, int flag)
1527{
1528 int need_buff = 1;
1529 char *buff = (char *)cmd; /* cast away const */
1530 int rcode = 0;
1531
1532 if (len == -1) {
1533 len = strlen(cmd);
1534#ifdef CONFIG_SYS_HUSH_PARSER
1535 /* hush will never change our string */
1536 need_buff = 0;
1537#else
1538 /* the built-in parser will change our string if it sees \n */
1539 need_buff = strchr(cmd, '\n') != NULL;
1540#endif
1541 }
1542 if (need_buff) {
1543 buff = malloc(len + 1);
1544 if (!buff)
1545 return 1;
1546 memcpy(buff, cmd, len);
1547 buff[len] = '\0';
1548 }
1549#ifdef CONFIG_SYS_HUSH_PARSER
1550 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1551#else
1552 /*
1553 * This function will overwrite any \n it sees with a \0, which
1554 * is why it can't work with a const char *. Here we are making
1555 * using of internal knowledge of this function, to avoid always
1556 * doing a malloc() which is actually required only in a case that
1557 * is pretty rare.
1558 */
1559 rcode = builtin_run_command_list(buff, flag);
1560 if (need_buff)
1561 free(buff);
1562#endif
1563
1564 return rcode;
1565}
1566
wdenkc6097192002-11-03 00:24:07 +00001567/****************************************************************************/
1568
Jon Loeligerc3517f92007-07-08 18:10:08 -05001569#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001570int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001571{
1572 int i;
wdenkc6097192002-11-03 00:24:07 +00001573
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001574 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001575 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001576
1577 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001578 char *arg;
1579
1580 if ((arg = getenv (argv[i])) == NULL) {
1581 printf ("## Error: \"%s\" not defined\n", argv[i]);
1582 return 1;
1583 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001584
Simon Glass009dde12012-02-14 19:59:20 +00001585 if (run_command(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001586 return 1;
wdenkc6097192002-11-03 00:24:07 +00001587 }
wdenk3e386912003-04-05 00:53:31 +00001588 return 0;
wdenkc6097192002-11-03 00:24:07 +00001589}
Jon Loeliger90253172007-07-10 11:02:44 -05001590#endif