blob: a80fb7c3441cda6ef3318dad72cf000126502cdf [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 Denk4c9ca782006-07-21 11:33:45 +02005 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +000010 */
11
wdenk9dd2b882002-12-03 21:28:10 +000012/* #define DEBUG */
13
wdenkc6097192002-11-03 00:24:07 +000014#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <command.h>
Che-Liang Chiou0b5e5a32012-10-25 16:31:07 +000016#include <fdtdec.h>
Simon Glass299f0be2014-04-10 20:01:24 -060017#include <cli_hush.h>
Simon Glassb2b687f2013-05-15 06:23:59 +000018#include <malloc.h>
19#include <menu.h>
wdenk0a658552003-08-05 17:43:17 +000020#include <post.h>
Simon Glassb2b687f2013-05-15 06:23:59 +000021#include <version.h>
22#include <watchdog.h>
Jason Hobbscafa1aa2011-08-23 11:06:54 +000023#include <linux/ctype.h>
wdenk0a658552003-08-05 17:43:17 +000024
Wolfgang Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6405a152006-03-31 18:32:53 +020026
Heiko Schocher8a8ec532007-07-13 09:54:17 +020027/*
28 * Board-specific Platform code can reimplement show_boot_progress () if needed
29 */
30void inline __show_boot_progress (int val) {}
Emil Medveace7cc82009-05-12 13:48:32 -050031void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocher8a8ec532007-07-13 09:54:17 +020032
wdenkc6097192002-11-03 00:24:07 +000033#define MAX_DELAY_STOP_STR 32
34
Simon Glassa1a16a82013-05-15 06:24:00 +000035#define DEBUG_PARSER 0 /* set to 1 to debug */
36
37#define debug_parser(fmt, args...) \
38 debug_cond(DEBUG_PARSER, fmt, ##args)
39
Simon Glasse717e642013-05-15 06:24:01 +000040#ifndef DEBUG_BOOTKEYS
41#define DEBUG_BOOTKEYS 0
42#endif
43#define debug_bootkeys(fmt, args...) \
44 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
wdenkc6097192002-11-03 00:24:07 +000045
John Schmollerc01e7202010-03-12 09:49:23 -060046char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000047
Stefan Roese59ff5a52006-07-27 16:11:19 +020048static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysingerd5c319c2010-12-22 09:40:45 -050049static const char erase_seq[] = "\b \b"; /* erase sequence */
50static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000051
52#ifdef CONFIG_BOOT_RETRY_TIME
53static uint64_t endtime = 0; /* must be set, default is instant timeout */
54static int retry_time = -1; /* -1 so can call readline before main_loop */
55#endif
56
57#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
58
59#ifndef CONFIG_BOOT_RETRY_MIN
60#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
61#endif
62
63#ifdef CONFIG_MODEM_SUPPORT
64int do_mdm_init = 0;
65extern void mdm_init(void); /* defined in board.c */
66#endif
67
68/***************************************************************************
69 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsa56febb2011-08-31 05:37:24 +000070 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000071 */
Joe Hershberger66eb8fc2013-02-08 10:28:00 +000072#if defined(CONFIG_BOOTDELAY)
wdenkc6097192002-11-03 00:24:07 +000073# if defined(CONFIG_AUTOBOOT_KEYED)
Simon Glasse71c1e62013-05-15 06:23:55 +000074static int abortboot_keyed(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000075{
76 int abort = 0;
77 uint64_t etime = endtick(bootdelay);
Wolfgang Denk9c82c682006-10-28 00:38:39 +020078 struct {
wdenkc6097192002-11-03 00:24:07 +000079 char* str;
80 u_int len;
81 int retry;
82 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +020083 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +000084 { str: getenv ("bootdelaykey"), retry: 1 },
85 { str: getenv ("bootdelaykey2"), retry: 1 },
86 { str: getenv ("bootstopkey"), retry: 0 },
87 { str: getenv ("bootstopkey2"), retry: 0 },
88 };
89
90 char presskey [MAX_DELAY_STOP_STR];
91 u_int presskey_len = 0;
92 u_int presskey_max = 0;
93 u_int i;
94
Dirk Eibachc82b4ea2012-04-26 01:49:33 +000095#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
96 if (bootdelay == 0)
97 return 0;
98#endif
99
wdenkc6097192002-11-03 00:24:07 +0000100# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roese37628252008-08-06 14:05:38 +0200101 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000102# endif
103
104# ifdef CONFIG_AUTOBOOT_DELAY_STR
105 if (delaykey[0].str == NULL)
106 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
107# endif
108# ifdef CONFIG_AUTOBOOT_DELAY_STR2
109 if (delaykey[1].str == NULL)
110 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
111# endif
112# ifdef CONFIG_AUTOBOOT_STOP_STR
113 if (delaykey[2].str == NULL)
114 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
115# endif
116# ifdef CONFIG_AUTOBOOT_STOP_STR2
117 if (delaykey[3].str == NULL)
118 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
119# endif
120
121 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
122 delaykey[i].len = delaykey[i].str == NULL ?
123 0 : strlen (delaykey[i].str);
124 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
125 MAX_DELAY_STOP_STR : delaykey[i].len;
126
127 presskey_max = presskey_max > delaykey[i].len ?
128 presskey_max : delaykey[i].len;
129
Simon Glasse717e642013-05-15 06:24:01 +0000130 debug_bootkeys("%s key:<%s>\n",
131 delaykey[i].retry ? "delay" : "stop",
132 delaykey[i].str ? delaykey[i].str : "NULL");
wdenkc6097192002-11-03 00:24:07 +0000133 }
134
135 /* In order to keep up with incoming data, check timeout only
136 * when catch up.
137 */
Peter Korsgaard6499fd62008-12-10 16:24:16 +0100138 do {
139 if (tstc()) {
140 if (presskey_len < presskey_max) {
141 presskey [presskey_len ++] = getc();
142 }
143 else {
144 for (i = 0; i < presskey_max - 1; i ++)
145 presskey [i] = presskey [i + 1];
146
147 presskey [i] = getc();
148 }
149 }
150
wdenkc6097192002-11-03 00:24:07 +0000151 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
152 if (delaykey[i].len > 0 &&
153 presskey_len >= delaykey[i].len &&
154 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk57b2d802003-06-27 21:31:46 +0000155 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000156 delaykey[i].len) == 0) {
Simon Glasse717e642013-05-15 06:24:01 +0000157 debug_bootkeys("got %skey\n",
158 delaykey[i].retry ? "delay" :
159 "stop");
wdenkc6097192002-11-03 00:24:07 +0000160
161# ifdef CONFIG_BOOT_RETRY_TIME
162 /* don't retry auto boot */
163 if (! delaykey[i].retry)
164 retry_time = -1;
165# endif
166 abort = 1;
167 }
168 }
Peter Korsgaard6499fd62008-12-10 16:24:16 +0100169 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000170
wdenkc6097192002-11-03 00:24:07 +0000171 if (!abort)
Simon Glasse717e642013-05-15 06:24:01 +0000172 debug_bootkeys("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000173
dzu6605c842003-10-24 13:14:45 +0000174#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200175 if (abort)
176 gd->flags &= ~GD_FLG_SILENT;
dzu6605c842003-10-24 13:14:45 +0000177#endif
178
wdenkc6097192002-11-03 00:24:07 +0000179 return abort;
180}
181
182# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
183
wdenk452cfd62002-11-19 11:04:11 +0000184#ifdef CONFIG_MENUKEY
185static int menukey = 0;
186#endif
187
Simon Glasse71c1e62013-05-15 06:23:55 +0000188static int abortboot_normal(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000189{
190 int abort = 0;
Jim Lin56014a42013-01-24 01:05:55 +0000191 unsigned long ts;
wdenkc6097192002-11-03 00:24:07 +0000192
wdenk452cfd62002-11-19 11:04:11 +0000193#ifdef CONFIG_MENUPROMPT
Stefan Roese37628252008-08-06 14:05:38 +0200194 printf(CONFIG_MENUPROMPT);
wdenk452cfd62002-11-19 11:04:11 +0000195#else
Joe Hershberger96ccaf32012-08-17 10:53:12 +0000196 if (bootdelay >= 0)
197 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenk452cfd62002-11-19 11:04:11 +0000198#endif
wdenkc6097192002-11-03 00:24:07 +0000199
200#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk57b2d802003-06-27 21:31:46 +0000201 /*
202 * Check if key already pressed
203 * Don't check if bootdelay < 0
204 */
wdenkc6097192002-11-03 00:24:07 +0000205 if (bootdelay >= 0) {
206 if (tstc()) { /* we got a key press */
207 (void) getc(); /* consume input */
wdenk42c05472004-03-23 22:14:11 +0000208 puts ("\b\b\b 0");
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200209 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000210 }
wdenk57b2d802003-06-27 21:31:46 +0000211 }
wdenkc6097192002-11-03 00:24:07 +0000212#endif
213
wdenk808532a2003-10-10 10:05:42 +0000214 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000215 --bootdelay;
Jim Lin56014a42013-01-24 01:05:55 +0000216 /* delay 1000 ms */
217 ts = get_timer(0);
218 do {
wdenkc6097192002-11-03 00:24:07 +0000219 if (tstc()) { /* we got a key press */
220 abort = 1; /* don't auto boot */
221 bootdelay = 0; /* no more delay */
wdenk452cfd62002-11-19 11:04:11 +0000222# ifdef CONFIG_MENUKEY
223 menukey = getc();
224# else
wdenkc6097192002-11-03 00:24:07 +0000225 (void) getc(); /* consume input */
wdenk452cfd62002-11-19 11:04:11 +0000226# endif
wdenkc6097192002-11-03 00:24:07 +0000227 break;
228 }
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200229 udelay(10000);
Jim Lin56014a42013-01-24 01:05:55 +0000230 } while (!abort && get_timer(ts) < 1000);
wdenkc6097192002-11-03 00:24:07 +0000231
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200232 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000233 }
234
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200235 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000236
wdenk808532a2003-10-10 10:05:42 +0000237#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200238 if (abort)
239 gd->flags &= ~GD_FLG_SILENT;
wdenk808532a2003-10-10 10:05:42 +0000240#endif
241
wdenkc6097192002-11-03 00:24:07 +0000242 return abort;
243}
244# endif /* CONFIG_AUTOBOOT_KEYED */
Simon Glasse71c1e62013-05-15 06:23:55 +0000245
246static int abortboot(int bootdelay)
247{
248#ifdef CONFIG_AUTOBOOT_KEYED
249 return abortboot_keyed(bootdelay);
250#else
251 return abortboot_normal(bootdelay);
252#endif
253}
Joe Hershberger66eb8fc2013-02-08 10:28:00 +0000254#endif /* CONFIG_BOOTDELAY */
wdenkc6097192002-11-03 00:24:07 +0000255
Doug Anderson07b81142012-10-25 16:31:09 +0000256/*
257 * Runs the given boot command securely. Specifically:
258 * - Doesn't run the command with the shell (run_command or parse_string_outer),
259 * since that's a lot of code surface that an attacker might exploit.
260 * Because of this, we don't do any argument parsing--the secure boot command
261 * has to be a full-fledged u-boot command.
262 * - Doesn't check for keypresses before booting, since that could be a
263 * security hole; also disables Ctrl-C.
264 * - Doesn't allow the command to return.
265 *
266 * Upon any failures, this function will drop into an infinite loop after
267 * printing the error message to console.
268 */
269
Joe Hershberger66eb8fc2013-02-08 10:28:00 +0000270#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
Doug Anderson07b81142012-10-25 16:31:09 +0000271static void secure_boot_cmd(char *cmd)
272{
273 cmd_tbl_t *cmdtp;
274 int rc;
275
276 if (!cmd) {
277 printf("## Error: Secure boot command not specified\n");
278 goto err;
279 }
280
281 /* Disable Ctrl-C just in case some command is used that checks it. */
282 disable_ctrlc(1);
283
284 /* Find the command directly. */
285 cmdtp = find_cmd(cmd);
286 if (!cmdtp) {
287 printf("## Error: \"%s\" not defined\n", cmd);
288 goto err;
289 }
290
291 /* Run the command, forcing no flags and faking argc and argv. */
292 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
293
294 /* Shouldn't ever return from boot command. */
295 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
296
297err:
298 /*
299 * Not a whole lot to do here. Rebooting won't help much, since we'll
300 * just end up right back here. Just loop.
301 */
302 hang();
303}
304
Simon Glass057f6802012-10-25 16:31:11 +0000305static void process_fdt_options(const void *blob)
306{
307 ulong addr;
308
309 /* Add an env variable to point to a kernel payload, if available */
310 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
311 if (addr)
312 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
313
314 /* Add an env variable to point to a root disk, if available */
315 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
316 if (addr)
317 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
318}
Doug Anderson07b81142012-10-25 16:31:09 +0000319#endif /* CONFIG_OF_CONTROL */
320
Simon Glassde6f0c42013-05-15 06:23:56 +0000321#ifdef CONFIG_BOOTDELAY
322static void process_boot_delay(void)
wdenkc6097192002-11-03 00:24:07 +0000323{
Simon Glassde6f0c42013-05-15 06:23:56 +0000324#ifdef CONFIG_OF_CONTROL
Che-Liang Chiou0b5e5a32012-10-25 16:31:07 +0000325 char *env;
326#endif
wdenkc6097192002-11-03 00:24:07 +0000327 char *s;
328 int bootdelay;
wdenk0a658552003-08-05 17:43:17 +0000329#ifdef CONFIG_BOOTCOUNT_LIMIT
330 unsigned long bootcount = 0;
331 unsigned long bootlimit = 0;
wdenk0a658552003-08-05 17:43:17 +0000332#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000333
wdenk0a658552003-08-05 17:43:17 +0000334#ifdef CONFIG_BOOTCOUNT_LIMIT
335 bootcount = bootcount_load();
336 bootcount++;
337 bootcount_store (bootcount);
Simon Glass962cf722013-05-15 06:23:57 +0000338 setenv_ulong("bootcount", bootcount);
339 bootlimit = getenv_ulong("bootlimit", 10, 0);
wdenk0a658552003-08-05 17:43:17 +0000340#endif /* CONFIG_BOOTCOUNT_LIMIT */
341
wdenkc6097192002-11-03 00:24:07 +0000342 s = getenv ("bootdelay");
343 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
344
Stephen Warren12474462013-05-14 08:02:56 +0000345#ifdef CONFIG_OF_CONTROL
346 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
347 bootdelay);
348#endif
349
wdenk9dd2b882002-12-03 21:28:10 +0000350 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000351
Heiko Schocherb4c1df82012-01-16 21:13:35 +0000352#if defined(CONFIG_MENU_SHOW)
353 bootdelay = menu_show(bootdelay);
354#endif
wdenkc6097192002-11-03 00:24:07 +0000355# ifdef CONFIG_BOOT_RETRY_TIME
wdenkb00ec162003-06-19 23:40:20 +0000356 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000357# endif /* CONFIG_BOOT_RETRY_TIME */
358
Yuri Tikhonovd773efb2008-02-04 14:11:03 +0100359#ifdef CONFIG_POST
360 if (gd->flags & GD_FLG_POSTFAIL) {
361 s = getenv("failbootcmd");
362 }
363 else
364#endif /* CONFIG_POST */
wdenk0a658552003-08-05 17:43:17 +0000365#ifdef CONFIG_BOOTCOUNT_LIMIT
366 if (bootlimit && (bootcount > bootlimit)) {
367 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200368 (unsigned)bootlimit);
wdenk0a658552003-08-05 17:43:17 +0000369 s = getenv ("altbootcmd");
370 }
371 else
372#endif /* CONFIG_BOOTCOUNT_LIMIT */
373 s = getenv ("bootcmd");
Che-Liang Chiou0b5e5a32012-10-25 16:31:07 +0000374#ifdef CONFIG_OF_CONTROL
375 /* Allow the fdt to override the boot command */
376 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
377 if (env)
378 s = env;
Doug Anderson07b81142012-10-25 16:31:09 +0000379
Simon Glass057f6802012-10-25 16:31:11 +0000380 process_fdt_options(gd->fdt_blob);
381
Doug Anderson07b81142012-10-25 16:31:09 +0000382 /*
383 * If the bootsecure option was chosen, use secure_boot_cmd().
384 * Always use 'env' in this case, since bootsecure requres that the
385 * bootcmd was specified in the FDT too.
386 */
387 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
388 secure_boot_cmd(env);
389
Che-Liang Chiou0b5e5a32012-10-25 16:31:07 +0000390#endif /* CONFIG_OF_CONTROL */
wdenk9dd2b882002-12-03 21:28:10 +0000391
392 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
393
Joe Hershberger96ccaf32012-08-17 10:53:12 +0000394 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
Mark Langsdorfc3012262013-09-10 15:20:23 -0500395#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
wdenkc6097192002-11-03 00:24:07 +0000396 int prev = disable_ctrlc(1); /* disable Control C checking */
Simon Glassde6f0c42013-05-15 06:23:56 +0000397#endif
wdenkc6097192002-11-03 00:24:07 +0000398
Simon Glassd0eb9402012-03-30 21:30:56 +0000399 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000400
Mark Langsdorfc3012262013-09-10 15:20:23 -0500401#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
wdenkc6097192002-11-03 00:24:07 +0000402 disable_ctrlc(prev); /* restore Control C checking */
Simon Glassde6f0c42013-05-15 06:23:56 +0000403#endif
wdenkc6097192002-11-03 00:24:07 +0000404 }
wdenk452cfd62002-11-19 11:04:11 +0000405
Simon Glassde6f0c42013-05-15 06:23:56 +0000406#ifdef CONFIG_MENUKEY
wdenk9dd2b882002-12-03 21:28:10 +0000407 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs3b403b72011-06-23 08:27:30 +0000408 s = getenv("menucmd");
Jason Hobbsa56febb2011-08-31 05:37:24 +0000409 if (s)
Simon Glassd0eb9402012-03-30 21:30:56 +0000410 run_command_list(s, -1, 0);
wdenk452cfd62002-11-19 11:04:11 +0000411 }
412#endif /* CONFIG_MENUKEY */
Simon Glassde6f0c42013-05-15 06:23:56 +0000413}
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200414#endif /* CONFIG_BOOTDELAY */
wdenk452cfd62002-11-19 11:04:11 +0000415
Simon Glassde6f0c42013-05-15 06:23:56 +0000416void main_loop(void)
417{
418#ifndef CONFIG_SYS_HUSH_PARSER
419 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
420 int len;
421 int rc = 1;
422 int flag;
423#endif
424#ifdef CONFIG_PREBOOT
425 char *p;
426#endif
427
428 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
429
Simon Glassf5b1c6d2014-03-22 17:14:51 -0600430#ifndef CONFIG_SYS_GENERIC_BOARD
431 puts("Warning: Your board does not use generic board. Please read\n");
432 puts("doc/README.generic-board and take action. Boards not\n");
433 puts("upgraded by the late 2014 may break or be removed.\n");
434#endif
435
Simon Glassde6f0c42013-05-15 06:23:56 +0000436#ifdef CONFIG_MODEM_SUPPORT
437 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
438 if (do_mdm_init) {
439 char *str = strdup(getenv("mdm_cmd"));
440 setenv("preboot", str); /* set or delete definition */
441 if (str != NULL)
442 free(str);
443 mdm_init(); /* wait for modem connection */
444 }
445#endif /* CONFIG_MODEM_SUPPORT */
446
447#ifdef CONFIG_VERSION_VARIABLE
448 {
449 setenv("ver", version_string); /* set version variable */
450 }
451#endif /* CONFIG_VERSION_VARIABLE */
452
453#ifdef CONFIG_SYS_HUSH_PARSER
454 u_boot_hush_start();
455#endif
456
457#if defined(CONFIG_HUSH_INIT_VAR)
458 hush_init_var();
459#endif
460
461#ifdef CONFIG_PREBOOT
462 p = getenv("preboot");
463 if (p != NULL) {
464# ifdef CONFIG_AUTOBOOT_KEYED
465 int prev = disable_ctrlc(1); /* disable Control C checking */
466# endif
467
468 run_command_list(p, -1, 0);
469
470# ifdef CONFIG_AUTOBOOT_KEYED
471 disable_ctrlc(prev); /* restore Control C checking */
472# endif
473 }
474#endif /* CONFIG_PREBOOT */
475
476#if defined(CONFIG_UPDATE_TFTP)
477 update_tftp(0UL);
478#endif /* CONFIG_UPDATE_TFTP */
479
480#ifdef CONFIG_BOOTDELAY
481 process_boot_delay();
482#endif
wdenkc6097192002-11-03 00:24:07 +0000483 /*
484 * Main Loop for Monitor Command Processing
485 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200486#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000487 parse_file_outer();
488 /* This point is never reached */
489 for (;;);
490#else
491 for (;;) {
492#ifdef CONFIG_BOOT_RETRY_TIME
493 if (rc >= 0) {
494 /* Saw enough of a valid command to
495 * restart the timeout.
496 */
497 reset_cmd_timeout();
498 }
499#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200500 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000501
502 flag = 0; /* assume no special flags for now */
503 if (len > 0)
504 strcpy (lastcommand, console_buffer);
505 else if (len == 0)
506 flag |= CMD_FLAG_REPEAT;
507#ifdef CONFIG_BOOT_RETRY_TIME
508 else if (len == -2) {
509 /* -2 means timed out, retry autoboot
510 */
wdenk42c05472004-03-23 22:14:11 +0000511 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000512# ifdef CONFIG_RESET_TO_RETRY
513 /* Reinit board to run initialization code again */
514 do_reset (NULL, 0, 0, NULL);
515# else
516 return; /* retry autoboot */
517# endif
518 }
519#endif
520
521 if (len == -1)
wdenk42c05472004-03-23 22:14:11 +0000522 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000523 else
Simon Glassbf8c5b02012-02-14 19:59:21 +0000524 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000525
526 if (rc <= 0) {
527 /* invalid command or not repeatable, forget it */
528 lastcommand[0] = 0;
529 }
530 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200531#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000532}
533
wdenkb00ec162003-06-19 23:40:20 +0000534#ifdef CONFIG_BOOT_RETRY_TIME
535/***************************************************************************
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200536 * initialize command line timeout
wdenkb00ec162003-06-19 23:40:20 +0000537 */
538void init_cmd_timeout(void)
539{
540 char *s = getenv ("bootretry");
541
542 if (s != NULL)
wdenk444f22b2003-12-07 21:39:28 +0000543 retry_time = (int)simple_strtol(s, NULL, 10);
wdenkb00ec162003-06-19 23:40:20 +0000544 else
545 retry_time = CONFIG_BOOT_RETRY_TIME;
546
547 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
548 retry_time = CONFIG_BOOT_RETRY_MIN;
549}
550
wdenkc6097192002-11-03 00:24:07 +0000551/***************************************************************************
552 * reset command line timeout to retry_time seconds
553 */
wdenkc6097192002-11-03 00:24:07 +0000554void reset_cmd_timeout(void)
555{
556 endtime = endtick(retry_time);
557}
558#endif
559
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200560#ifdef CONFIG_CMDLINE_EDITING
561
562/*
563 * cmdline-editing related codes from vivi.
564 * Author: Janghoon Lyu <nandy@mizi.com>
565 */
566
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200567#define putnstr(str,n) do { \
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700568 printf ("%.*s", (int)n, str); \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200569 } while (0)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200570
571#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200572#define CTL_BACKSPACE ('\b')
573#define DEL ((char)255)
574#define DEL7 ((char)127)
575#define CREAD_HIST_CHAR ('!')
576
577#define getcmd_putch(ch) putc(ch)
578#define getcmd_getch() getc()
579#define getcmd_cbeep() getcmd_putch('\a')
580
581#define HIST_MAX 20
Peter Tyser7e0e5ca2010-09-29 13:30:56 -0500582#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200583
Kim Phillipsb052b602012-10-29 13:34:32 +0000584static int hist_max;
585static int hist_add_idx;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200586static int hist_cur = -1;
Kim Phillipsb052b602012-10-29 13:34:32 +0000587static unsigned hist_num;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200588
Kim Phillipsb052b602012-10-29 13:34:32 +0000589static char *hist_list[HIST_MAX];
590static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200591
592#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
593
594static void hist_init(void)
595{
596 int i;
597
598 hist_max = 0;
599 hist_add_idx = 0;
600 hist_cur = -1;
601 hist_num = 0;
602
603 for (i = 0; i < HIST_MAX; i++) {
604 hist_list[i] = hist_lines[i];
605 hist_list[i][0] = '\0';
606 }
607}
608
609static void cread_add_to_hist(char *line)
610{
611 strcpy(hist_list[hist_add_idx], line);
612
613 if (++hist_add_idx >= HIST_MAX)
614 hist_add_idx = 0;
615
616 if (hist_add_idx > hist_max)
617 hist_max = hist_add_idx;
618
619 hist_num++;
620}
621
622static char* hist_prev(void)
623{
624 char *ret;
625 int old_cur;
626
627 if (hist_cur < 0)
628 return NULL;
629
630 old_cur = hist_cur;
631 if (--hist_cur < 0)
632 hist_cur = hist_max;
633
634 if (hist_cur == hist_add_idx) {
635 hist_cur = old_cur;
636 ret = NULL;
637 } else
638 ret = hist_list[hist_cur];
639
640 return (ret);
641}
642
643static char* hist_next(void)
644{
645 char *ret;
646
647 if (hist_cur < 0)
648 return NULL;
649
650 if (hist_cur == hist_add_idx)
651 return NULL;
652
653 if (++hist_cur > hist_max)
654 hist_cur = 0;
655
656 if (hist_cur == hist_add_idx) {
657 ret = "";
658 } else
659 ret = hist_list[hist_cur];
660
661 return (ret);
662}
663
Stefan Roese59ff5a52006-07-27 16:11:19 +0200664#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200665static void cread_print_hist_list(void)
666{
667 int i;
668 unsigned long n;
669
670 n = hist_num - hist_max;
671
672 i = hist_add_idx + 1;
673 while (1) {
674 if (i > hist_max)
675 i = 0;
676 if (i == hist_add_idx)
677 break;
678 printf("%s\n", hist_list[i]);
679 n++;
680 i++;
681 }
682}
Stefan Roese59ff5a52006-07-27 16:11:19 +0200683#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200684
685#define BEGINNING_OF_LINE() { \
686 while (num) { \
687 getcmd_putch(CTL_BACKSPACE); \
688 num--; \
689 } \
690}
691
692#define ERASE_TO_EOL() { \
693 if (num < eol_num) { \
Mike Frysinger3952bea2010-07-23 05:28:15 -0400694 printf("%*s", (int)(eol_num - num), ""); \
695 do { \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200696 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger3952bea2010-07-23 05:28:15 -0400697 } while (--eol_num > num); \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200698 } \
699}
700
701#define REFRESH_TO_EOL() { \
702 if (num < eol_num) { \
703 wlen = eol_num - num; \
704 putnstr(buf + num, wlen); \
705 num = eol_num; \
706 } \
707}
708
709static void cread_add_char(char ichar, int insert, unsigned long *num,
710 unsigned long *eol_num, char *buf, unsigned long len)
711{
712 unsigned long wlen;
713
714 /* room ??? */
715 if (insert || *num == *eol_num) {
716 if (*eol_num > len - 1) {
717 getcmd_cbeep();
718 return;
719 }
720 (*eol_num)++;
721 }
722
723 if (insert) {
724 wlen = *eol_num - *num;
725 if (wlen > 1) {
726 memmove(&buf[*num+1], &buf[*num], wlen-1);
727 }
728
729 buf[*num] = ichar;
730 putnstr(buf + *num, wlen);
731 (*num)++;
732 while (--wlen) {
733 getcmd_putch(CTL_BACKSPACE);
734 }
735 } else {
736 /* echo the character */
737 wlen = 1;
738 buf[*num] = ichar;
739 putnstr(buf + *num, wlen);
740 (*num)++;
741 }
742}
743
744static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
745 unsigned long *eol_num, char *buf, unsigned long len)
746{
747 while (strsize--) {
748 cread_add_char(*str, insert, num, eol_num, buf, len);
749 str++;
750 }
751}
752
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000753static int cread_line(const char *const prompt, char *buf, unsigned int *len,
754 int timeout)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200755{
756 unsigned long num = 0;
757 unsigned long eol_num = 0;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200758 unsigned long wlen;
759 char ichar;
760 int insert = 1;
761 int esc_len = 0;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200762 char esc_save[8];
Peter Tyser8ad15a92009-10-25 15:12:54 -0500763 int init_len = strlen(buf);
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000764 int first = 1;
Peter Tyser8ad15a92009-10-25 15:12:54 -0500765
766 if (init_len)
767 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200768
769 while (1) {
Andreas Engel3a0ba102008-01-09 17:10:56 +0100770#ifdef CONFIG_BOOT_RETRY_TIME
771 while (!tstc()) { /* while no incoming data */
772 if (retry_time >= 0 && get_ticks() > endtime)
773 return (-2); /* timed out */
Jens Scharsig11247552010-04-09 19:02:38 +0200774 WATCHDOG_RESET();
Andreas Engel3a0ba102008-01-09 17:10:56 +0100775 }
776#endif
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000777 if (first && timeout) {
778 uint64_t etime = endtick(timeout);
779
780 while (!tstc()) { /* while no incoming data */
781 if (get_ticks() >= etime)
782 return -2; /* timed out */
783 WATCHDOG_RESET();
784 }
785 first = 0;
786 }
Andreas Engel3a0ba102008-01-09 17:10:56 +0100787
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200788 ichar = getcmd_getch();
789
790 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200791 putc('\n');
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200792 break;
793 }
794
795 /*
796 * handle standard linux xterm esc sequences for arrow key, etc.
797 */
798 if (esc_len != 0) {
799 if (esc_len == 1) {
800 if (ichar == '[') {
801 esc_save[esc_len] = ichar;
802 esc_len = 2;
803 } else {
804 cread_add_str(esc_save, esc_len, insert,
805 &num, &eol_num, buf, *len);
806 esc_len = 0;
807 }
808 continue;
809 }
810
811 switch (ichar) {
812
813 case 'D': /* <- key */
814 ichar = CTL_CH('b');
815 esc_len = 0;
816 break;
817 case 'C': /* -> key */
818 ichar = CTL_CH('f');
819 esc_len = 0;
820 break; /* pass off to ^F handler */
821 case 'H': /* Home key */
822 ichar = CTL_CH('a');
823 esc_len = 0;
824 break; /* pass off to ^A handler */
825 case 'A': /* up arrow */
826 ichar = CTL_CH('p');
827 esc_len = 0;
828 break; /* pass off to ^P handler */
829 case 'B': /* down arrow */
830 ichar = CTL_CH('n');
831 esc_len = 0;
832 break; /* pass off to ^N handler */
833 default:
834 esc_save[esc_len++] = ichar;
835 cread_add_str(esc_save, esc_len, insert,
836 &num, &eol_num, buf, *len);
837 esc_len = 0;
838 continue;
839 }
840 }
841
842 switch (ichar) {
843 case 0x1b:
844 if (esc_len == 0) {
845 esc_save[esc_len] = ichar;
846 esc_len = 1;
847 } else {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200848 puts("impossible condition #876\n");
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200849 esc_len = 0;
850 }
851 break;
852
853 case CTL_CH('a'):
854 BEGINNING_OF_LINE();
855 break;
856 case CTL_CH('c'): /* ^C - break */
857 *buf = '\0'; /* discard input */
858 return (-1);
859 case CTL_CH('f'):
860 if (num < eol_num) {
861 getcmd_putch(buf[num]);
862 num++;
863 }
864 break;
865 case CTL_CH('b'):
866 if (num) {
867 getcmd_putch(CTL_BACKSPACE);
868 num--;
869 }
870 break;
871 case CTL_CH('d'):
872 if (num < eol_num) {
873 wlen = eol_num - num - 1;
874 if (wlen) {
875 memmove(&buf[num], &buf[num+1], wlen);
876 putnstr(buf + num, wlen);
877 }
878
879 getcmd_putch(' ');
880 do {
881 getcmd_putch(CTL_BACKSPACE);
882 } while (wlen--);
883 eol_num--;
884 }
885 break;
886 case CTL_CH('k'):
887 ERASE_TO_EOL();
888 break;
889 case CTL_CH('e'):
890 REFRESH_TO_EOL();
891 break;
892 case CTL_CH('o'):
893 insert = !insert;
894 break;
895 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100896 case CTL_CH('u'):
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200897 BEGINNING_OF_LINE();
898 ERASE_TO_EOL();
899 break;
900 case DEL:
901 case DEL7:
902 case 8:
903 if (num) {
904 wlen = eol_num - num;
905 num--;
906 memmove(&buf[num], &buf[num+1], wlen);
907 getcmd_putch(CTL_BACKSPACE);
908 putnstr(buf + num, wlen);
909 getcmd_putch(' ');
910 do {
911 getcmd_putch(CTL_BACKSPACE);
912 } while (wlen--);
913 eol_num--;
914 }
915 break;
916 case CTL_CH('p'):
917 case CTL_CH('n'):
918 {
919 char * hline;
920
921 esc_len = 0;
922
923 if (ichar == CTL_CH('p'))
924 hline = hist_prev();
925 else
926 hline = hist_next();
927
928 if (!hline) {
929 getcmd_cbeep();
930 continue;
931 }
932
933 /* nuke the current line */
934 /* first, go home */
935 BEGINNING_OF_LINE();
936
937 /* erase to end of line */
938 ERASE_TO_EOL();
939
940 /* copy new line into place and display */
941 strcpy(buf, hline);
942 eol_num = strlen(buf);
943 REFRESH_TO_EOL();
944 continue;
945 }
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100946#ifdef CONFIG_AUTO_COMPLETE
947 case '\t': {
948 int num2, col;
949
950 /* do not autocomplete when in the middle */
951 if (num < eol_num) {
952 getcmd_cbeep();
953 break;
954 }
955
956 buf[num] = '\0';
957 col = strlen(prompt) + eol_num;
958 num2 = num;
959 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
960 col = num2 - num;
961 num += col;
962 eol_num += col;
963 }
964 break;
965 }
966#endif
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200967 default:
968 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
969 break;
970 }
971 }
972 *len = eol_num;
973 buf[eol_num] = '\0'; /* lose the newline */
974
975 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
976 cread_add_to_hist(buf);
977 hist_cur = hist_add_idx;
978
Peter Tyser03e46c42009-10-25 15:12:53 -0500979 return 0;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200980}
981
982#endif /* CONFIG_CMDLINE_EDITING */
983
wdenkc6097192002-11-03 00:24:07 +0000984/****************************************************************************/
985
986/*
987 * Prompt for input and read a line.
988 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
989 * time out when time goes past endtime (timebase time in ticks).
990 * Return: number of read characters
991 * -1 if break
992 * -2 if timed out
993 */
994int readline (const char *const prompt)
995{
Peter Tyser8ad15a92009-10-25 15:12:54 -0500996 /*
997 * If console_buffer isn't 0-length the user will be prompted to modify
998 * it instead of entering it from scratch as desired.
999 */
1000 console_buffer[0] = '\0';
1001
Heiko Schocher0eab4d52012-01-16 21:13:05 +00001002 return readline_into_buffer(prompt, console_buffer, 0);
James Yang20ffa972008-01-09 11:17:49 -06001003}
1004
1005
Heiko Schocher0eab4d52012-01-16 21:13:05 +00001006int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang20ffa972008-01-09 11:17:49 -06001007{
1008 char *p = buffer;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +02001009#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser7e0e5ca2010-09-29 13:30:56 -05001010 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesedc5ff702006-08-07 15:08:44 +02001011 int rc;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +02001012 static int initted = 0;
1013
James Yangb83983f2008-05-05 10:22:53 -05001014 /*
1015 * History uses a global array which is not
1016 * writable until after relocation to RAM.
1017 * Revert to non-history version if still
1018 * running from flash.
1019 */
1020 if (gd->flags & GD_FLG_RELOC) {
1021 if (!initted) {
1022 hist_init();
1023 initted = 1;
1024 }
1025
Peter Tyserc144e012009-10-25 15:12:52 -05001026 if (prompt)
1027 puts (prompt);
James Yangb83983f2008-05-05 10:22:53 -05001028
Heiko Schocher0eab4d52012-01-16 21:13:05 +00001029 rc = cread_line(prompt, p, &len, timeout);
James Yangb83983f2008-05-05 10:22:53 -05001030 return rc < 0 ? rc : len;
1031
1032 } else {
1033#endif /* CONFIG_CMDLINE_EDITING */
Kumar Galae225b172008-01-10 02:22:05 -06001034 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +00001035 int n = 0; /* buffer index */
1036 int plen = 0; /* prompt length */
1037 int col; /* output column cnt */
1038 char c;
1039
1040 /* print prompt */
1041 if (prompt) {
1042 plen = strlen (prompt);
1043 puts (prompt);
1044 }
1045 col = plen;
1046
1047 for (;;) {
1048#ifdef CONFIG_BOOT_RETRY_TIME
1049 while (!tstc()) { /* while no incoming data */
1050 if (retry_time >= 0 && get_ticks() > endtime)
1051 return (-2); /* timed out */
Jens Scharsig11247552010-04-09 19:02:38 +02001052 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001053 }
1054#endif
1055 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1056
1057#ifdef CONFIG_SHOW_ACTIVITY
1058 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +00001059 show_activity(0);
Jens Scharsig11247552010-04-09 19:02:38 +02001060 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001061 }
1062#endif
1063 c = getc();
1064
1065 /*
1066 * Special character handling
1067 */
1068 switch (c) {
Simon Glasse30a5f72013-05-15 06:23:58 +00001069 case '\r': /* Enter */
wdenkc6097192002-11-03 00:24:07 +00001070 case '\n':
1071 *p = '\0';
1072 puts ("\r\n");
Simon Glasse30a5f72013-05-15 06:23:58 +00001073 return p - p_buf;
wdenkc6097192002-11-03 00:24:07 +00001074
Simon Glasse30a5f72013-05-15 06:23:58 +00001075 case '\0': /* nul */
wdenk8d4d1f62004-03-23 22:37:33 +00001076 continue;
1077
Simon Glasse30a5f72013-05-15 06:23:58 +00001078 case 0x03: /* ^C - break */
James Yang20ffa972008-01-09 11:17:49 -06001079 p_buf[0] = '\0'; /* discard input */
Simon Glasse30a5f72013-05-15 06:23:58 +00001080 return -1;
wdenkc6097192002-11-03 00:24:07 +00001081
Simon Glasse30a5f72013-05-15 06:23:58 +00001082 case 0x15: /* ^U - erase line */
wdenkc6097192002-11-03 00:24:07 +00001083 while (col > plen) {
1084 puts (erase_seq);
1085 --col;
1086 }
James Yang20ffa972008-01-09 11:17:49 -06001087 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001088 n = 0;
1089 continue;
1090
Simon Glasse30a5f72013-05-15 06:23:58 +00001091 case 0x17: /* ^W - erase word */
James Yang20ffa972008-01-09 11:17:49 -06001092 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001093 while ((n > 0) && (*p != ' ')) {
James Yang20ffa972008-01-09 11:17:49 -06001094 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001095 }
1096 continue;
1097
Simon Glasse30a5f72013-05-15 06:23:58 +00001098 case 0x08: /* ^H - backspace */
1099 case 0x7F: /* DEL - backspace */
James Yang20ffa972008-01-09 11:17:49 -06001100 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001101 continue;
1102
1103 default:
1104 /*
1105 * Must be a normal character then
1106 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001107 if (n < CONFIG_SYS_CBSIZE-2) {
Simon Glasse30a5f72013-05-15 06:23:58 +00001108 if (c == '\t') { /* expand TABs */
wdenk3902d702004-04-15 18:22:41 +00001109#ifdef CONFIG_AUTO_COMPLETE
1110 /* if auto completion triggered just continue */
1111 *p = '\0';
1112 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang20ffa972008-01-09 11:17:49 -06001113 p = p_buf + n; /* reset */
wdenk3902d702004-04-15 18:22:41 +00001114 continue;
1115 }
1116#endif
wdenkc6097192002-11-03 00:24:07 +00001117 puts (tab_seq+(col&07));
1118 col += 8 - (col&07);
1119 } else {
Simon Glassef45ffd2012-10-30 13:40:18 +00001120 char buf[2];
1121
1122 /*
Simon Glasse30a5f72013-05-15 06:23:58 +00001123 * Echo input using puts() to force an
Simon Glassef45ffd2012-10-30 13:40:18 +00001124 * LCD flush if we are using an LCD
1125 */
1126 ++col;
1127 buf[0] = c;
1128 buf[1] = '\0';
1129 puts(buf);
wdenkc6097192002-11-03 00:24:07 +00001130 }
1131 *p++ = c;
1132 ++n;
1133 } else { /* Buffer full */
1134 putc ('\a');
1135 }
1136 }
1137 }
James Yangb83983f2008-05-05 10:22:53 -05001138#ifdef CONFIG_CMDLINE_EDITING
1139 }
1140#endif
wdenkc6097192002-11-03 00:24:07 +00001141}
1142
1143/****************************************************************************/
1144
1145static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1146{
1147 char *s;
1148
1149 if (*np == 0) {
1150 return (p);
1151 }
1152
1153 if (*(--p) == '\t') { /* will retype the whole line */
1154 while (*colp > plen) {
1155 puts (erase_seq);
1156 (*colp)--;
1157 }
1158 for (s=buffer; s<p; ++s) {
1159 if (*s == '\t') {
1160 puts (tab_seq+((*colp) & 07));
1161 *colp += 8 - ((*colp) & 07);
1162 } else {
1163 ++(*colp);
1164 putc (*s);
1165 }
1166 }
1167 } else {
1168 puts (erase_seq);
1169 (*colp)--;
1170 }
1171 (*np)--;
1172 return (p);
1173}
1174
1175/****************************************************************************/
1176
1177int parse_line (char *line, char *argv[])
1178{
1179 int nargs = 0;
1180
Simon Glassa1a16a82013-05-15 06:24:00 +00001181 debug_parser("parse_line: \"%s\"\n", line);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001182 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001183
1184 /* skip any white space */
Jason Hobbscafa1aa2011-08-23 11:06:54 +00001185 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001186 ++line;
wdenkc6097192002-11-03 00:24:07 +00001187
1188 if (*line == '\0') { /* end of line, no more args */
1189 argv[nargs] = NULL;
Simon Glassa1a16a82013-05-15 06:24:00 +00001190 debug_parser("parse_line: nargs=%d\n", nargs);
1191 return nargs;
wdenkc6097192002-11-03 00:24:07 +00001192 }
1193
1194 argv[nargs++] = line; /* begin of argument string */
1195
1196 /* find end of string */
Jason Hobbscafa1aa2011-08-23 11:06:54 +00001197 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001198 ++line;
wdenkc6097192002-11-03 00:24:07 +00001199
1200 if (*line == '\0') { /* end of line, no more args */
1201 argv[nargs] = NULL;
Simon Glassa1a16a82013-05-15 06:24:00 +00001202 debug_parser("parse_line: nargs=%d\n", nargs);
1203 return nargs;
wdenkc6097192002-11-03 00:24:07 +00001204 }
1205
1206 *line++ = '\0'; /* terminate current arg */
1207 }
1208
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001209 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001210
Simon Glassa1a16a82013-05-15 06:24:00 +00001211 debug_parser("parse_line: nargs=%d\n", nargs);
wdenkc6097192002-11-03 00:24:07 +00001212 return (nargs);
1213}
1214
1215/****************************************************************************/
1216
Simon Glass82c21322012-02-14 19:59:22 +00001217#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001218static void process_macros (const char *input, char *output)
1219{
1220 char c, prev;
1221 const char *varname_start = NULL;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001222 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001223 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001224 int state = 0; /* 0 = waiting for '$' */
1225
1226 /* 1 = waiting for '(' or '{' */
1227 /* 2 = waiting for ')' or '}' */
1228 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001229 char *output_start = output;
1230
Simon Glassa1a16a82013-05-15 06:24:00 +00001231 debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
1232 input);
wdenkc6097192002-11-03 00:24:07 +00001233
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001234 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001235
1236 while (inputcnt && outputcnt) {
wdenk57b2d802003-06-27 21:31:46 +00001237 c = *input++;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001238 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001239
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001240 if (state != 3) {
1241 /* remove one level of escape characters */
1242 if ((c == '\\') && (prev != '\\')) {
1243 if (inputcnt-- == 0)
1244 break;
1245 prev = c;
1246 c = *input++;
1247 }
wdenkc6097192002-11-03 00:24:07 +00001248 }
wdenkc6097192002-11-03 00:24:07 +00001249
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001250 switch (state) {
1251 case 0: /* Waiting for (unescaped) $ */
1252 if ((c == '\'') && (prev != '\\')) {
1253 state = 3;
1254 break;
1255 }
1256 if ((c == '$') && (prev != '\\')) {
1257 state++;
1258 } else {
wdenkc6097192002-11-03 00:24:07 +00001259 *(output++) = c;
1260 outputcnt--;
1261 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001262 break;
1263 case 1: /* Waiting for ( */
1264 if (c == '(' || c == '{') {
1265 state++;
1266 varname_start = input;
1267 } else {
1268 state = 0;
1269 *(output++) = '$';
1270 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001271
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001272 if (outputcnt) {
1273 *(output++) = c;
1274 outputcnt--;
1275 }
wdenkc6097192002-11-03 00:24:07 +00001276 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001277 break;
1278 case 2: /* Waiting for ) */
1279 if (c == ')' || c == '}') {
1280 int i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001281 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001282 int envcnt = input - varname_start - 1; /* Varname # of chars */
wdenkc6097192002-11-03 00:24:07 +00001283
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001284 /* Get the varname */
1285 for (i = 0; i < envcnt; i++) {
1286 envname[i] = varname_start[i];
wdenkc6097192002-11-03 00:24:07 +00001287 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001288 envname[i] = 0;
1289
1290 /* Get its value */
1291 envval = getenv (envname);
1292
1293 /* Copy into the line if it exists */
1294 if (envval != NULL)
1295 while ((*envval) && outputcnt) {
1296 *(output++) = *(envval++);
1297 outputcnt--;
1298 }
1299 /* Look for another '$' */
1300 state = 0;
1301 }
1302 break;
1303 case 3: /* Waiting for ' */
1304 if ((c == '\'') && (prev != '\\')) {
1305 state = 0;
1306 } else {
1307 *(output++) = c;
1308 outputcnt--;
1309 }
1310 break;
wdenkc6097192002-11-03 00:24:07 +00001311 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001312 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001313 }
1314
1315 if (outputcnt)
1316 *output = 0;
Bartlomiej Siekacadc8e82007-05-27 17:04:18 +02001317 else
1318 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001319
Simon Glassa1a16a82013-05-15 06:24:00 +00001320 debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
1321 strlen(output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001322}
1323
1324/****************************************************************************
1325 * returns:
1326 * 1 - command executed, repeatable
1327 * 0 - command executed but not repeatable, interrupted commands are
1328 * always considered not repeatable
1329 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001330 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001331 * considered unrecognized)
1332 *
1333 * WARNING:
1334 *
1335 * We must create a temporary copy of the command since the command we get
1336 * may be the result from getenv(), which returns a pointer directly to
1337 * the environment data, which may change magicly when the command we run
1338 * creates or modifies environment variables (like "bootp" does).
1339 */
Simon Glassbf8c5b02012-02-14 19:59:21 +00001340static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001341{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001342 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001343 char *token; /* start of token in cmdbuf */
1344 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001345 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001346 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001347 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf4688a22003-05-28 08:06:31 +00001348 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001349 int repeatable = 1;
wdenkf4688a22003-05-28 08:06:31 +00001350 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001351
Simon Glassa1a16a82013-05-15 06:24:00 +00001352 debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
1353 if (DEBUG_PARSER) {
1354 /* use puts - string may be loooong */
1355 puts(cmd ? cmd : "NULL");
1356 puts("\"\n");
1357 }
wdenkc6097192002-11-03 00:24:07 +00001358 clear_ctrlc(); /* forget any previous Control C */
1359
1360 if (!cmd || !*cmd) {
1361 return -1; /* empty command */
1362 }
1363
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001364 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001365 puts ("## Command too long!\n");
1366 return -1;
1367 }
1368
1369 strcpy (cmdbuf, cmd);
1370
1371 /* Process separators and check for invalid
1372 * repeatable commands
1373 */
1374
Simon Glassa1a16a82013-05-15 06:24:00 +00001375 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
wdenkc6097192002-11-03 00:24:07 +00001376 while (*str) {
1377
1378 /*
1379 * Find separator, or string end
1380 * Allow simple escape of ';' by writing "\;"
1381 */
wdenk654868c2003-01-02 23:57:29 +00001382 for (inquotes = 0, sep = str; *sep; sep++) {
1383 if ((*sep=='\'') &&
1384 (*(sep-1) != '\\'))
1385 inquotes=!inquotes;
1386
1387 if (!inquotes &&
1388 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001389 ( sep != str) && /* past string start */
1390 (*(sep-1) != '\\')) /* and NOT escaped */
1391 break;
1392 }
1393
1394 /*
1395 * Limit the token to data between separators
1396 */
1397 token = str;
1398 if (*sep) {
1399 str = sep + 1; /* start of command for next pass */
1400 *sep = '\0';
1401 }
1402 else
1403 str = sep; /* no more commands for next pass */
Simon Glassa1a16a82013-05-15 06:24:00 +00001404 debug_parser("token: \"%s\"\n", token);
wdenkc6097192002-11-03 00:24:07 +00001405
1406 /* find macros in this token and replace them */
1407 process_macros (token, finaltoken);
1408
1409 /* Extract arguments */
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001410 if ((argc = parse_line (finaltoken, argv)) == 0) {
1411 rc = -1; /* no command at all */
1412 continue;
1413 }
wdenkc6097192002-11-03 00:24:07 +00001414
Richard Genoud94ff0672012-12-03 06:28:28 +00001415 if (cmd_process(flag, argc, argv, &repeatable, NULL))
Timo Ketola9ba54722012-04-22 23:57:27 +00001416 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001417
1418 /* Did the user stop this? */
1419 if (had_ctrlc ())
Detlev Zundeleeab5a12007-05-23 18:47:48 +02001420 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001421 }
1422
wdenkf4688a22003-05-28 08:06:31 +00001423 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001424}
Simon Glass82c21322012-02-14 19:59:22 +00001425#endif
wdenkc6097192002-11-03 00:24:07 +00001426
Simon Glassbf8c5b02012-02-14 19:59:21 +00001427/*
1428 * Run a command using the selected parser.
1429 *
1430 * @param cmd Command to run
1431 * @param flag Execution flags (CMD_FLAG_...)
1432 * @return 0 on success, or != 0 on error.
1433 */
1434int run_command(const char *cmd, int flag)
1435{
1436#ifndef CONFIG_SYS_HUSH_PARSER
1437 /*
1438 * builtin_run_command can return 0 or 1 for success, so clean up
1439 * its result.
1440 */
1441 if (builtin_run_command(cmd, flag) == -1)
1442 return 1;
1443
1444 return 0;
1445#else
1446 return parse_string_outer(cmd,
1447 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1448#endif
1449}
1450
Simon Glassa6642e02012-03-30 21:30:55 +00001451#ifndef CONFIG_SYS_HUSH_PARSER
1452/**
1453 * Execute a list of command separated by ; or \n using the built-in parser.
1454 *
1455 * This function cannot take a const char * for the command, since if it
1456 * finds newlines in the string, it replaces them with \0.
1457 *
1458 * @param cmd String containing list of commands
1459 * @param flag Execution flags (CMD_FLAG_...)
1460 * @return 0 on success, or != 0 on error.
1461 */
1462static int builtin_run_command_list(char *cmd, int flag)
1463{
1464 char *line, *next;
1465 int rcode = 0;
1466
1467 /*
1468 * Break into individual lines, and execute each line; terminate on
1469 * error.
1470 */
1471 line = next = cmd;
1472 while (*next) {
1473 if (*next == '\n') {
1474 *next = '\0';
1475 /* run only non-empty commands */
1476 if (*line) {
1477 debug("** exec: \"%s\"\n", line);
1478 if (builtin_run_command(line, 0) < 0) {
1479 rcode = 1;
1480 break;
1481 }
1482 }
1483 line = next + 1;
1484 }
1485 ++next;
1486 }
1487 if (rcode == 0 && *line)
1488 rcode = (builtin_run_command(line, 0) >= 0);
1489
1490 return rcode;
1491}
1492#endif
1493
1494int run_command_list(const char *cmd, int len, int flag)
1495{
1496 int need_buff = 1;
1497 char *buff = (char *)cmd; /* cast away const */
1498 int rcode = 0;
1499
1500 if (len == -1) {
1501 len = strlen(cmd);
1502#ifdef CONFIG_SYS_HUSH_PARSER
1503 /* hush will never change our string */
1504 need_buff = 0;
1505#else
1506 /* the built-in parser will change our string if it sees \n */
1507 need_buff = strchr(cmd, '\n') != NULL;
1508#endif
1509 }
1510 if (need_buff) {
1511 buff = malloc(len + 1);
1512 if (!buff)
1513 return 1;
1514 memcpy(buff, cmd, len);
1515 buff[len] = '\0';
1516 }
1517#ifdef CONFIG_SYS_HUSH_PARSER
1518 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1519#else
1520 /*
1521 * This function will overwrite any \n it sees with a \0, which
1522 * is why it can't work with a const char *. Here we are making
1523 * using of internal knowledge of this function, to avoid always
1524 * doing a malloc() which is actually required only in a case that
1525 * is pretty rare.
1526 */
1527 rcode = builtin_run_command_list(buff, flag);
1528 if (need_buff)
1529 free(buff);
1530#endif
1531
1532 return rcode;
1533}
1534
wdenkc6097192002-11-03 00:24:07 +00001535/****************************************************************************/
1536
Jon Loeliger052fc842007-07-08 18:10:08 -05001537#if defined(CONFIG_CMD_RUN)
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001538int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001539{
1540 int i;
wdenkc6097192002-11-03 00:24:07 +00001541
Wolfgang Denk3b683112010-07-17 01:06:04 +02001542 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001543 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001544
1545 for (i=1; i<argc; ++i) {
wdenkb02744a2003-04-05 00:53:31 +00001546 char *arg;
1547
1548 if ((arg = getenv (argv[i])) == NULL) {
1549 printf ("## Error: \"%s\" not defined\n", argv[i]);
1550 return 1;
1551 }
Jason Hobbsa56febb2011-08-31 05:37:24 +00001552
Simon Glass4c39b1d2013-10-25 23:01:32 -06001553 if (run_command_list(arg, -1, flag) != 0)
wdenkb02744a2003-04-05 00:53:31 +00001554 return 1;
wdenkc6097192002-11-03 00:24:07 +00001555 }
wdenkb02744a2003-04-05 00:53:31 +00001556 return 0;
wdenkc6097192002-11-03 00:24:07 +00001557}
Jon Loeligerd704d912007-07-10 11:02:44 -05001558#endif