blob: 88b0adb6b70dfe0734589a70dc9f35eff357d43f [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 Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
wdenka6c7ad22002-12-03 21:28:10 +00008/* #define DEBUG */
9
wdenkc6097192002-11-03 00:24:07 +000010#include <common.h>
Simon Glass18d66532014-04-10 20:01:25 -060011#include <cli.h>
wdenkc6097192002-11-03 00:24:07 +000012#include <command.h>
Che-Liang Chiou224b72e2012-10-25 16:31:07 +000013#include <fdtdec.h>
Simon Glasseca86fa2014-04-10 20:01:24 -060014#include <cli_hush.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000015#include <malloc.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000016#include <menu.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000017#include <post.h>
18#include <version.h>
wdenkbdccc4f2003-08-05 17:43:17 +000019
Wolfgang Denkd87080b2006-03-31 18:32:53 +020020DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denkd87080b2006-03-31 18:32:53 +020021
Heiko Schocherfad63402007-07-13 09:54:17 +020022/*
23 * Board-specific Platform code can reimplement show_boot_progress () if needed
24 */
25void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050026void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020027
wdenkc6097192002-11-03 00:24:07 +000028#define MAX_DELAY_STOP_STR 32
29
Simon Glassd34d1862013-05-15 06:24:01 +000030#ifndef DEBUG_BOOTKEYS
31#define DEBUG_BOOTKEYS 0
32#endif
33#define debug_bootkeys(fmt, args...) \
34 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
wdenkc6097192002-11-03 00:24:07 +000035
wdenkc6097192002-11-03 00:24:07 +000036#ifdef CONFIG_MODEM_SUPPORT
37int do_mdm_init = 0;
38extern void mdm_init(void); /* defined in board.c */
39#endif
40
41/***************************************************************************
42 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsc8a20792011-08-31 05:37:24 +000043 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000044 */
Joe Hershbergerd5145442013-02-08 10:28:00 +000045#if defined(CONFIG_BOOTDELAY)
wdenkc6097192002-11-03 00:24:07 +000046# if defined(CONFIG_AUTOBOOT_KEYED)
Simon Glass063ae002013-05-15 06:23:55 +000047static int abortboot_keyed(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +000048{
49 int abort = 0;
50 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +020051 struct {
wdenkc6097192002-11-03 00:24:07 +000052 char* str;
53 u_int len;
54 int retry;
55 }
Wolfgang Denk19973b62006-10-28 00:38:39 +020056 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +000057 { str: getenv ("bootdelaykey"), retry: 1 },
58 { str: getenv ("bootdelaykey2"), retry: 1 },
59 { str: getenv ("bootstopkey"), retry: 0 },
60 { str: getenv ("bootstopkey2"), retry: 0 },
61 };
62
63 char presskey [MAX_DELAY_STOP_STR];
64 u_int presskey_len = 0;
65 u_int presskey_max = 0;
66 u_int i;
67
Dirk Eibacha5aae0a2012-04-26 01:49:33 +000068#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
69 if (bootdelay == 0)
70 return 0;
71#endif
72
wdenkc6097192002-11-03 00:24:07 +000073# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +020074 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +000075# endif
76
77# ifdef CONFIG_AUTOBOOT_DELAY_STR
78 if (delaykey[0].str == NULL)
79 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
80# endif
81# ifdef CONFIG_AUTOBOOT_DELAY_STR2
82 if (delaykey[1].str == NULL)
83 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
84# endif
85# ifdef CONFIG_AUTOBOOT_STOP_STR
86 if (delaykey[2].str == NULL)
87 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
88# endif
89# ifdef CONFIG_AUTOBOOT_STOP_STR2
90 if (delaykey[3].str == NULL)
91 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
92# endif
93
94 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
95 delaykey[i].len = delaykey[i].str == NULL ?
96 0 : strlen (delaykey[i].str);
97 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
98 MAX_DELAY_STOP_STR : delaykey[i].len;
99
100 presskey_max = presskey_max > delaykey[i].len ?
101 presskey_max : delaykey[i].len;
102
Simon Glassd34d1862013-05-15 06:24:01 +0000103 debug_bootkeys("%s key:<%s>\n",
104 delaykey[i].retry ? "delay" : "stop",
105 delaykey[i].str ? delaykey[i].str : "NULL");
wdenkc6097192002-11-03 00:24:07 +0000106 }
107
108 /* In order to keep up with incoming data, check timeout only
109 * when catch up.
110 */
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100111 do {
112 if (tstc()) {
113 if (presskey_len < presskey_max) {
114 presskey [presskey_len ++] = getc();
115 }
116 else {
117 for (i = 0; i < presskey_max - 1; i ++)
118 presskey [i] = presskey [i + 1];
119
120 presskey [i] = getc();
121 }
122 }
123
wdenkc6097192002-11-03 00:24:07 +0000124 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
125 if (delaykey[i].len > 0 &&
126 presskey_len >= delaykey[i].len &&
127 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000128 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000129 delaykey[i].len) == 0) {
Simon Glassd34d1862013-05-15 06:24:01 +0000130 debug_bootkeys("got %skey\n",
131 delaykey[i].retry ? "delay" :
132 "stop");
wdenkc6097192002-11-03 00:24:07 +0000133
134# ifdef CONFIG_BOOT_RETRY_TIME
135 /* don't retry auto boot */
136 if (! delaykey[i].retry)
Simon Glass6493ccc2014-04-10 20:01:26 -0600137 bootretry_dont_retry();
wdenkc6097192002-11-03 00:24:07 +0000138# endif
139 abort = 1;
140 }
141 }
Peter Korsgaardc3284b02008-12-10 16:24:16 +0100142 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000143
wdenkc6097192002-11-03 00:24:07 +0000144 if (!abort)
Simon Glassd34d1862013-05-15 06:24:01 +0000145 debug_bootkeys("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000146
dzu8cb81432003-10-24 13:14:45 +0000147#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200148 if (abort)
149 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000150#endif
151
wdenkc6097192002-11-03 00:24:07 +0000152 return abort;
153}
154
155# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
156
wdenkc7de8292002-11-19 11:04:11 +0000157#ifdef CONFIG_MENUKEY
158static int menukey = 0;
159#endif
160
Simon Glass063ae002013-05-15 06:23:55 +0000161static int abortboot_normal(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000162{
163 int abort = 0;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000164 unsigned long ts;
wdenkc6097192002-11-03 00:24:07 +0000165
wdenkc7de8292002-11-19 11:04:11 +0000166#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200167 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000168#else
Joe Hershberger93d72122012-08-17 10:53:12 +0000169 if (bootdelay >= 0)
170 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000171#endif
wdenkc6097192002-11-03 00:24:07 +0000172
173#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000174 /*
175 * Check if key already pressed
176 * Don't check if bootdelay < 0
177 */
wdenkc6097192002-11-03 00:24:07 +0000178 if (bootdelay >= 0) {
179 if (tstc()) { /* we got a key press */
180 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000181 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200182 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000183 }
wdenk8bde7f72003-06-27 21:31:46 +0000184 }
wdenkc6097192002-11-03 00:24:07 +0000185#endif
186
wdenkf72da342003-10-10 10:05:42 +0000187 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000188 --bootdelay;
Jim Linb2f3e0e2013-01-24 01:05:55 +0000189 /* delay 1000 ms */
190 ts = get_timer(0);
191 do {
wdenkc6097192002-11-03 00:24:07 +0000192 if (tstc()) { /* we got a key press */
193 abort = 1; /* don't auto boot */
194 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000195# ifdef CONFIG_MENUKEY
196 menukey = getc();
197# else
wdenkc6097192002-11-03 00:24:07 +0000198 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000199# endif
wdenkc6097192002-11-03 00:24:07 +0000200 break;
201 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200202 udelay(10000);
Jim Linb2f3e0e2013-01-24 01:05:55 +0000203 } while (!abort && get_timer(ts) < 1000);
wdenkc6097192002-11-03 00:24:07 +0000204
Ladislav Michlada4d402007-04-25 16:01:26 +0200205 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000206 }
207
Ladislav Michlada4d402007-04-25 16:01:26 +0200208 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000209
wdenkf72da342003-10-10 10:05:42 +0000210#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200211 if (abort)
212 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000213#endif
214
wdenkc6097192002-11-03 00:24:07 +0000215 return abort;
216}
217# endif /* CONFIG_AUTOBOOT_KEYED */
Simon Glass063ae002013-05-15 06:23:55 +0000218
219static int abortboot(int bootdelay)
220{
221#ifdef CONFIG_AUTOBOOT_KEYED
222 return abortboot_keyed(bootdelay);
223#else
224 return abortboot_normal(bootdelay);
225#endif
226}
Joe Hershbergerd5145442013-02-08 10:28:00 +0000227#endif /* CONFIG_BOOTDELAY */
wdenkc6097192002-11-03 00:24:07 +0000228
Doug Anderson67e1ea22012-10-25 16:31:09 +0000229/*
230 * Runs the given boot command securely. Specifically:
231 * - Doesn't run the command with the shell (run_command or parse_string_outer),
232 * since that's a lot of code surface that an attacker might exploit.
233 * Because of this, we don't do any argument parsing--the secure boot command
234 * has to be a full-fledged u-boot command.
235 * - Doesn't check for keypresses before booting, since that could be a
236 * security hole; also disables Ctrl-C.
237 * - Doesn't allow the command to return.
238 *
239 * Upon any failures, this function will drop into an infinite loop after
240 * printing the error message to console.
241 */
242
Joe Hershbergerd5145442013-02-08 10:28:00 +0000243#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
Doug Anderson67e1ea22012-10-25 16:31:09 +0000244static void secure_boot_cmd(char *cmd)
245{
246 cmd_tbl_t *cmdtp;
247 int rc;
248
249 if (!cmd) {
250 printf("## Error: Secure boot command not specified\n");
251 goto err;
252 }
253
254 /* Disable Ctrl-C just in case some command is used that checks it. */
255 disable_ctrlc(1);
256
257 /* Find the command directly. */
258 cmdtp = find_cmd(cmd);
259 if (!cmdtp) {
260 printf("## Error: \"%s\" not defined\n", cmd);
261 goto err;
262 }
263
264 /* Run the command, forcing no flags and faking argc and argv. */
265 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
266
267 /* Shouldn't ever return from boot command. */
268 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
269
270err:
271 /*
272 * Not a whole lot to do here. Rebooting won't help much, since we'll
273 * just end up right back here. Just loop.
274 */
275 hang();
276}
277
Simon Glassfcabc242012-10-25 16:31:11 +0000278static void process_fdt_options(const void *blob)
279{
280 ulong addr;
281
282 /* Add an env variable to point to a kernel payload, if available */
283 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
284 if (addr)
285 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
286
287 /* Add an env variable to point to a root disk, if available */
288 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
289 if (addr)
290 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
291}
Doug Anderson67e1ea22012-10-25 16:31:09 +0000292#endif /* CONFIG_OF_CONTROL */
293
Simon Glassbc2b4c22013-05-15 06:23:56 +0000294#ifdef CONFIG_BOOTDELAY
295static void process_boot_delay(void)
wdenkc6097192002-11-03 00:24:07 +0000296{
Simon Glassbc2b4c22013-05-15 06:23:56 +0000297#ifdef CONFIG_OF_CONTROL
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000298 char *env;
299#endif
wdenkc6097192002-11-03 00:24:07 +0000300 char *s;
301 int bootdelay;
wdenkbdccc4f2003-08-05 17:43:17 +0000302#ifdef CONFIG_BOOTCOUNT_LIMIT
303 unsigned long bootcount = 0;
304 unsigned long bootlimit = 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000305#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000306
wdenkbdccc4f2003-08-05 17:43:17 +0000307#ifdef CONFIG_BOOTCOUNT_LIMIT
308 bootcount = bootcount_load();
309 bootcount++;
310 bootcount_store (bootcount);
Simon Glassf2abca82013-05-15 06:23:57 +0000311 setenv_ulong("bootcount", bootcount);
312 bootlimit = getenv_ulong("bootlimit", 10, 0);
wdenkbdccc4f2003-08-05 17:43:17 +0000313#endif /* CONFIG_BOOTCOUNT_LIMIT */
314
wdenkc6097192002-11-03 00:24:07 +0000315 s = getenv ("bootdelay");
316 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
317
Stephen Warren99bd5442013-05-14 08:02:56 +0000318#ifdef CONFIG_OF_CONTROL
319 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
320 bootdelay);
321#endif
322
wdenka6c7ad22002-12-03 21:28:10 +0000323 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000324
Heiko Schocher317d6c52012-01-16 21:13:35 +0000325#if defined(CONFIG_MENU_SHOW)
326 bootdelay = menu_show(bootdelay);
327#endif
wdenkc6097192002-11-03 00:24:07 +0000328# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000329 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000330# endif /* CONFIG_BOOT_RETRY_TIME */
331
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100332#ifdef CONFIG_POST
333 if (gd->flags & GD_FLG_POSTFAIL) {
334 s = getenv("failbootcmd");
335 }
336 else
337#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000338#ifdef CONFIG_BOOTCOUNT_LIMIT
339 if (bootlimit && (bootcount > bootlimit)) {
340 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
Wolfgang Denk93e14592013-10-04 17:43:24 +0200341 (unsigned)bootlimit);
wdenkbdccc4f2003-08-05 17:43:17 +0000342 s = getenv ("altbootcmd");
343 }
344 else
345#endif /* CONFIG_BOOTCOUNT_LIMIT */
346 s = getenv ("bootcmd");
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000347#ifdef CONFIG_OF_CONTROL
348 /* Allow the fdt to override the boot command */
349 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
350 if (env)
351 s = env;
Doug Anderson67e1ea22012-10-25 16:31:09 +0000352
Simon Glassfcabc242012-10-25 16:31:11 +0000353 process_fdt_options(gd->fdt_blob);
354
Doug Anderson67e1ea22012-10-25 16:31:09 +0000355 /*
356 * If the bootsecure option was chosen, use secure_boot_cmd().
357 * Always use 'env' in this case, since bootsecure requres that the
358 * bootcmd was specified in the FDT too.
359 */
360 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
361 secure_boot_cmd(env);
362
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000363#endif /* CONFIG_OF_CONTROL */
wdenka6c7ad22002-12-03 21:28:10 +0000364
365 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
366
Joe Hershberger93d72122012-08-17 10:53:12 +0000367 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
Mark Langsdorf00ddacc2013-09-10 15:20:23 -0500368#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
wdenkc6097192002-11-03 00:24:07 +0000369 int prev = disable_ctrlc(1); /* disable Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000370#endif
wdenkc6097192002-11-03 00:24:07 +0000371
Simon Glass3a8a02b2012-03-30 21:30:56 +0000372 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000373
Mark Langsdorf00ddacc2013-09-10 15:20:23 -0500374#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
wdenkc6097192002-11-03 00:24:07 +0000375 disable_ctrlc(prev); /* restore Control C checking */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000376#endif
wdenkc6097192002-11-03 00:24:07 +0000377 }
wdenkc7de8292002-11-19 11:04:11 +0000378
Simon Glassbc2b4c22013-05-15 06:23:56 +0000379#ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000380 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000381 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000382 if (s)
Simon Glass3a8a02b2012-03-30 21:30:56 +0000383 run_command_list(s, -1, 0);
wdenkc7de8292002-11-19 11:04:11 +0000384 }
385#endif /* CONFIG_MENUKEY */
Simon Glassbc2b4c22013-05-15 06:23:56 +0000386}
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200387#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000388
Simon Glassbc2b4c22013-05-15 06:23:56 +0000389void main_loop(void)
390{
Simon Glassbc2b4c22013-05-15 06:23:56 +0000391#ifdef CONFIG_PREBOOT
392 char *p;
393#endif
394
395 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
396
Simon Glass0f605c12014-03-22 17:14:51 -0600397#ifndef CONFIG_SYS_GENERIC_BOARD
398 puts("Warning: Your board does not use generic board. Please read\n");
399 puts("doc/README.generic-board and take action. Boards not\n");
400 puts("upgraded by the late 2014 may break or be removed.\n");
401#endif
402
Simon Glassbc2b4c22013-05-15 06:23:56 +0000403#ifdef CONFIG_MODEM_SUPPORT
404 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
405 if (do_mdm_init) {
406 char *str = strdup(getenv("mdm_cmd"));
407 setenv("preboot", str); /* set or delete definition */
408 if (str != NULL)
409 free(str);
410 mdm_init(); /* wait for modem connection */
411 }
412#endif /* CONFIG_MODEM_SUPPORT */
413
414#ifdef CONFIG_VERSION_VARIABLE
415 {
416 setenv("ver", version_string); /* set version variable */
417 }
418#endif /* CONFIG_VERSION_VARIABLE */
419
420#ifdef CONFIG_SYS_HUSH_PARSER
421 u_boot_hush_start();
422#endif
423
424#if defined(CONFIG_HUSH_INIT_VAR)
425 hush_init_var();
426#endif
427
428#ifdef CONFIG_PREBOOT
429 p = getenv("preboot");
430 if (p != NULL) {
431# ifdef CONFIG_AUTOBOOT_KEYED
432 int prev = disable_ctrlc(1); /* disable Control C checking */
433# endif
434
435 run_command_list(p, -1, 0);
436
437# ifdef CONFIG_AUTOBOOT_KEYED
438 disable_ctrlc(prev); /* restore Control C checking */
439# endif
440 }
441#endif /* CONFIG_PREBOOT */
442
443#if defined(CONFIG_UPDATE_TFTP)
444 update_tftp(0UL);
445#endif /* CONFIG_UPDATE_TFTP */
446
447#ifdef CONFIG_BOOTDELAY
448 process_boot_delay();
449#endif
wdenkc6097192002-11-03 00:24:07 +0000450 /*
451 * Main Loop for Monitor Command Processing
452 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200453#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000454 parse_file_outer();
455 /* This point is never reached */
456 for (;;);
457#else
Simon Glass6493ccc2014-04-10 20:01:26 -0600458 cli_loop();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200459#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000460}
461
wdenkc6097192002-11-03 00:24:07 +0000462/****************************************************************************/
463
464/*
Simon Glass53071532012-02-14 19:59:21 +0000465 * Run a command using the selected parser.
466 *
467 * @param cmd Command to run
468 * @param flag Execution flags (CMD_FLAG_...)
469 * @return 0 on success, or != 0 on error.
470 */
471int run_command(const char *cmd, int flag)
472{
473#ifndef CONFIG_SYS_HUSH_PARSER
474 /*
Simon Glass6493ccc2014-04-10 20:01:26 -0600475 * cli_run_command can return 0 or 1 for success, so clean up
Simon Glass53071532012-02-14 19:59:21 +0000476 * its result.
477 */
Simon Glass6493ccc2014-04-10 20:01:26 -0600478 if (cli_simple_run_command(cmd, flag) == -1)
Simon Glass53071532012-02-14 19:59:21 +0000479 return 1;
480
481 return 0;
482#else
483 return parse_string_outer(cmd,
484 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
485#endif
486}
487
Simon Glassd51004a2012-03-30 21:30:55 +0000488int run_command_list(const char *cmd, int len, int flag)
489{
490 int need_buff = 1;
491 char *buff = (char *)cmd; /* cast away const */
492 int rcode = 0;
493
494 if (len == -1) {
495 len = strlen(cmd);
496#ifdef CONFIG_SYS_HUSH_PARSER
497 /* hush will never change our string */
498 need_buff = 0;
499#else
500 /* the built-in parser will change our string if it sees \n */
501 need_buff = strchr(cmd, '\n') != NULL;
502#endif
503 }
504 if (need_buff) {
505 buff = malloc(len + 1);
506 if (!buff)
507 return 1;
508 memcpy(buff, cmd, len);
509 buff[len] = '\0';
510 }
511#ifdef CONFIG_SYS_HUSH_PARSER
512 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
513#else
514 /*
515 * This function will overwrite any \n it sees with a \0, which
516 * is why it can't work with a const char *. Here we are making
517 * using of internal knowledge of this function, to avoid always
518 * doing a malloc() which is actually required only in a case that
519 * is pretty rare.
520 */
Simon Glass6493ccc2014-04-10 20:01:26 -0600521 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassd51004a2012-03-30 21:30:55 +0000522 if (need_buff)
523 free(buff);
524#endif
525
526 return rcode;
527}
528
wdenkc6097192002-11-03 00:24:07 +0000529/****************************************************************************/
530
Jon Loeligerc3517f92007-07-08 18:10:08 -0500531#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200532int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000533{
534 int i;
wdenkc6097192002-11-03 00:24:07 +0000535
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200536 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000537 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +0000538
539 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +0000540 char *arg;
541
542 if ((arg = getenv (argv[i])) == NULL) {
543 printf ("## Error: \"%s\" not defined\n", argv[i]);
544 return 1;
545 }
Jason Hobbsc8a20792011-08-31 05:37:24 +0000546
Simon Glass1992dbf2013-10-25 23:01:32 -0600547 if (run_command_list(arg, -1, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +0000548 return 1;
wdenkc6097192002-11-03 00:24:07 +0000549 }
wdenk3e386912003-04-05 00:53:31 +0000550 return 0;
wdenkc6097192002-11-03 00:24:07 +0000551}
Jon Loeliger90253172007-07-10 11:02:44 -0500552#endif