3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
20 DECLARE_GLOBAL_DATA_PTR;
23 * Board-specific Platform code can reimplement show_boot_progress () if needed
25 void inline __show_boot_progress (int val) {}
26 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
28 #define MAX_DELAY_STOP_STR 32
30 #ifndef DEBUG_BOOTKEYS
31 #define DEBUG_BOOTKEYS 0
33 #define debug_bootkeys(fmt, args...) \
34 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
36 #ifdef CONFIG_MODEM_SUPPORT
38 extern void mdm_init(void); /* defined in board.c */
41 /***************************************************************************
42 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
43 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
45 #if defined(CONFIG_BOOTDELAY)
46 # if defined(CONFIG_AUTOBOOT_KEYED)
47 static int abortboot_keyed(int bootdelay)
50 uint64_t etime = endtick(bootdelay);
57 { str: getenv ("bootdelaykey"), retry: 1 },
58 { str: getenv ("bootdelaykey2"), retry: 1 },
59 { str: getenv ("bootstopkey"), retry: 0 },
60 { str: getenv ("bootstopkey2"), retry: 0 },
63 char presskey [MAX_DELAY_STOP_STR];
64 u_int presskey_len = 0;
65 u_int presskey_max = 0;
68 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
73 # ifdef CONFIG_AUTOBOOT_PROMPT
74 printf(CONFIG_AUTOBOOT_PROMPT);
77 # ifdef CONFIG_AUTOBOOT_DELAY_STR
78 if (delaykey[0].str == NULL)
79 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
81 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
82 if (delaykey[1].str == NULL)
83 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
85 # ifdef CONFIG_AUTOBOOT_STOP_STR
86 if (delaykey[2].str == NULL)
87 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
89 # ifdef CONFIG_AUTOBOOT_STOP_STR2
90 if (delaykey[3].str == NULL)
91 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
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;
100 presskey_max = presskey_max > delaykey[i].len ?
101 presskey_max : delaykey[i].len;
103 debug_bootkeys("%s key:<%s>\n",
104 delaykey[i].retry ? "delay" : "stop",
105 delaykey[i].str ? delaykey[i].str : "NULL");
108 /* In order to keep up with incoming data, check timeout only
113 if (presskey_len < presskey_max) {
114 presskey [presskey_len ++] = getc();
117 for (i = 0; i < presskey_max - 1; i ++)
118 presskey [i] = presskey [i + 1];
120 presskey [i] = getc();
124 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,
129 delaykey[i].len) == 0) {
130 debug_bootkeys("got %skey\n",
131 delaykey[i].retry ? "delay" :
134 # ifdef CONFIG_BOOT_RETRY_TIME
135 /* don't retry auto boot */
136 if (! delaykey[i].retry)
137 bootretry_dont_retry();
142 } while (!abort && get_ticks() <= etime);
145 debug_bootkeys("key timeout\n");
147 #ifdef CONFIG_SILENT_CONSOLE
149 gd->flags &= ~GD_FLG_SILENT;
155 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
157 #ifdef CONFIG_MENUKEY
158 static int menukey = 0;
161 static int abortboot_normal(int bootdelay)
166 #ifdef CONFIG_MENUPROMPT
167 printf(CONFIG_MENUPROMPT);
170 printf("Hit any key to stop autoboot: %2d ", bootdelay);
173 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
175 * Check if key already pressed
176 * Don't check if bootdelay < 0
178 if (bootdelay >= 0) {
179 if (tstc()) { /* we got a key press */
180 (void) getc(); /* consume input */
182 abort = 1; /* don't auto boot */
187 while ((bootdelay > 0) && (!abort)) {
192 if (tstc()) { /* we got a key press */
193 abort = 1; /* don't auto boot */
194 bootdelay = 0; /* no more delay */
195 # ifdef CONFIG_MENUKEY
198 (void) getc(); /* consume input */
203 } while (!abort && get_timer(ts) < 1000);
205 printf("\b\b\b%2d ", bootdelay);
210 #ifdef CONFIG_SILENT_CONSOLE
212 gd->flags &= ~GD_FLG_SILENT;
217 # endif /* CONFIG_AUTOBOOT_KEYED */
219 static int abortboot(int bootdelay)
221 #ifdef CONFIG_AUTOBOOT_KEYED
222 return abortboot_keyed(bootdelay);
224 return abortboot_normal(bootdelay);
227 #endif /* CONFIG_BOOTDELAY */
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.
239 * Upon any failures, this function will drop into an infinite loop after
240 * printing the error message to console.
243 #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
244 static void secure_boot_cmd(char *cmd)
250 printf("## Error: Secure boot command not specified\n");
254 /* Disable Ctrl-C just in case some command is used that checks it. */
257 /* Find the command directly. */
258 cmdtp = find_cmd(cmd);
260 printf("## Error: \"%s\" not defined\n", cmd);
264 /* Run the command, forcing no flags and faking argc and argv. */
265 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
267 /* Shouldn't ever return from boot command. */
268 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
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.
278 static void process_fdt_options(const void *blob)
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);
285 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
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);
290 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
292 #endif /* CONFIG_OF_CONTROL */
294 #ifdef CONFIG_BOOTDELAY
295 static void process_boot_delay(void)
297 #ifdef CONFIG_OF_CONTROL
302 #ifdef CONFIG_BOOTCOUNT_LIMIT
303 unsigned long bootcount = 0;
304 unsigned long bootlimit = 0;
305 #endif /* CONFIG_BOOTCOUNT_LIMIT */
307 #ifdef CONFIG_BOOTCOUNT_LIMIT
308 bootcount = bootcount_load();
310 bootcount_store (bootcount);
311 setenv_ulong("bootcount", bootcount);
312 bootlimit = getenv_ulong("bootlimit", 10, 0);
313 #endif /* CONFIG_BOOTCOUNT_LIMIT */
315 s = getenv ("bootdelay");
316 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
318 #ifdef CONFIG_OF_CONTROL
319 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
323 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
325 #if defined(CONFIG_MENU_SHOW)
326 bootdelay = menu_show(bootdelay);
328 # ifdef CONFIG_BOOT_RETRY_TIME
330 # endif /* CONFIG_BOOT_RETRY_TIME */
333 if (gd->flags & GD_FLG_POSTFAIL) {
334 s = getenv("failbootcmd");
337 #endif /* CONFIG_POST */
338 #ifdef CONFIG_BOOTCOUNT_LIMIT
339 if (bootlimit && (bootcount > bootlimit)) {
340 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
341 (unsigned)bootlimit);
342 s = getenv ("altbootcmd");
345 #endif /* CONFIG_BOOTCOUNT_LIMIT */
346 s = getenv ("bootcmd");
347 #ifdef CONFIG_OF_CONTROL
348 /* Allow the fdt to override the boot command */
349 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
353 process_fdt_options(gd->fdt_blob);
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.
360 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
361 secure_boot_cmd(env);
363 #endif /* CONFIG_OF_CONTROL */
365 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
367 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
368 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
369 int prev = disable_ctrlc(1); /* disable Control C checking */
372 run_command_list(s, -1, 0);
374 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
375 disable_ctrlc(prev); /* restore Control C checking */
379 #ifdef CONFIG_MENUKEY
380 if (menukey == CONFIG_MENUKEY) {
381 s = getenv("menucmd");
383 run_command_list(s, -1, 0);
385 #endif /* CONFIG_MENUKEY */
387 #endif /* CONFIG_BOOTDELAY */
391 #ifdef CONFIG_PREBOOT
395 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
397 #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");
403 #ifdef CONFIG_MODEM_SUPPORT
404 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
406 char *str = strdup(getenv("mdm_cmd"));
407 setenv("preboot", str); /* set or delete definition */
410 mdm_init(); /* wait for modem connection */
412 #endif /* CONFIG_MODEM_SUPPORT */
414 #ifdef CONFIG_VERSION_VARIABLE
416 setenv("ver", version_string); /* set version variable */
418 #endif /* CONFIG_VERSION_VARIABLE */
420 #ifdef CONFIG_SYS_HUSH_PARSER
424 #if defined(CONFIG_HUSH_INIT_VAR)
428 #ifdef CONFIG_PREBOOT
429 p = getenv("preboot");
431 # ifdef CONFIG_AUTOBOOT_KEYED
432 int prev = disable_ctrlc(1); /* disable Control C checking */
435 run_command_list(p, -1, 0);
437 # ifdef CONFIG_AUTOBOOT_KEYED
438 disable_ctrlc(prev); /* restore Control C checking */
441 #endif /* CONFIG_PREBOOT */
443 #if defined(CONFIG_UPDATE_TFTP)
445 #endif /* CONFIG_UPDATE_TFTP */
447 #ifdef CONFIG_BOOTDELAY
448 process_boot_delay();
451 * Main Loop for Monitor Command Processing
453 #ifdef CONFIG_SYS_HUSH_PARSER
455 /* This point is never reached */
459 #endif /*CONFIG_SYS_HUSH_PARSER*/
462 /****************************************************************************/
465 * Run a command using the selected parser.
467 * @param cmd Command to run
468 * @param flag Execution flags (CMD_FLAG_...)
469 * @return 0 on success, or != 0 on error.
471 int run_command(const char *cmd, int flag)
473 #ifndef CONFIG_SYS_HUSH_PARSER
475 * cli_run_command can return 0 or 1 for success, so clean up
478 if (cli_simple_run_command(cmd, flag) == -1)
483 return parse_string_outer(cmd,
484 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
488 int run_command_list(const char *cmd, int len, int flag)
491 char *buff = (char *)cmd; /* cast away const */
496 #ifdef CONFIG_SYS_HUSH_PARSER
497 /* hush will never change our string */
500 /* the built-in parser will change our string if it sees \n */
501 need_buff = strchr(cmd, '\n') != NULL;
505 buff = malloc(len + 1);
508 memcpy(buff, cmd, len);
511 #ifdef CONFIG_SYS_HUSH_PARSER
512 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
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
521 rcode = cli_simple_run_command_list(buff, flag);
529 /****************************************************************************/
531 #if defined(CONFIG_CMD_RUN)
532 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
537 return CMD_RET_USAGE;
539 for (i=1; i<argc; ++i) {
542 if ((arg = getenv (argv[i])) == NULL) {
543 printf ("## Error: \"%s\" not defined\n", argv[i]);
547 if (run_command_list(arg, -1, flag) != 0)