3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
15 DECLARE_GLOBAL_DATA_PTR;
17 #define MAX_DELAY_STOP_STR 32
19 #ifndef DEBUG_BOOTKEYS
20 #define DEBUG_BOOTKEYS 0
22 #define debug_bootkeys(fmt, args...) \
23 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
25 /***************************************************************************
26 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
27 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
29 # if defined(CONFIG_AUTOBOOT_KEYED)
30 static int abortboot_keyed(int bootdelay)
33 uint64_t etime = endtick(bootdelay);
40 { str: getenv("bootdelaykey"), retry: 1 },
41 { str: getenv("bootdelaykey2"), retry: 1 },
42 { str: getenv("bootstopkey"), retry: 0 },
43 { str: getenv("bootstopkey2"), retry: 0 },
46 char presskey[MAX_DELAY_STOP_STR];
47 u_int presskey_len = 0;
48 u_int presskey_max = 0;
51 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
56 # ifdef CONFIG_AUTOBOOT_PROMPT
57 printf(CONFIG_AUTOBOOT_PROMPT);
60 # ifdef CONFIG_AUTOBOOT_DELAY_STR
61 if (delaykey[0].str == NULL)
62 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
64 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
65 if (delaykey[1].str == NULL)
66 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
68 # ifdef CONFIG_AUTOBOOT_STOP_STR
69 if (delaykey[2].str == NULL)
70 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
72 # ifdef CONFIG_AUTOBOOT_STOP_STR2
73 if (delaykey[3].str == NULL)
74 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
77 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
78 delaykey[i].len = delaykey[i].str == NULL ?
79 0 : strlen(delaykey[i].str);
80 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
81 MAX_DELAY_STOP_STR : delaykey[i].len;
83 presskey_max = presskey_max > delaykey[i].len ?
84 presskey_max : delaykey[i].len;
86 debug_bootkeys("%s key:<%s>\n",
87 delaykey[i].retry ? "delay" : "stop",
88 delaykey[i].str ? delaykey[i].str : "NULL");
91 /* In order to keep up with incoming data, check timeout only
96 if (presskey_len < presskey_max) {
97 presskey[presskey_len++] = getc();
99 for (i = 0; i < presskey_max - 1; i++)
100 presskey[i] = presskey[i + 1];
102 presskey[i] = getc();
106 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
107 if (delaykey[i].len > 0 &&
108 presskey_len >= delaykey[i].len &&
109 memcmp(presskey + presskey_len -
110 delaykey[i].len, delaykey[i].str,
111 delaykey[i].len) == 0) {
112 debug_bootkeys("got %skey\n",
113 delaykey[i].retry ? "delay" :
116 /* don't retry auto boot */
117 if (!delaykey[i].retry)
118 bootretry_dont_retry();
122 } while (!abort && get_ticks() <= etime);
125 debug_bootkeys("key timeout\n");
127 #ifdef CONFIG_SILENT_CONSOLE
129 gd->flags &= ~GD_FLG_SILENT;
135 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
137 #ifdef CONFIG_MENUKEY
141 static int abortboot_normal(int bootdelay)
146 #ifdef CONFIG_MENUPROMPT
147 printf(CONFIG_MENUPROMPT);
150 printf("Hit any key to stop autoboot: %2d ", bootdelay);
153 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
155 * Check if key already pressed
156 * Don't check if bootdelay < 0
158 if (bootdelay >= 0) {
159 if (tstc()) { /* we got a key press */
160 (void) getc(); /* consume input */
162 abort = 1; /* don't auto boot */
167 while ((bootdelay > 0) && (!abort)) {
172 if (tstc()) { /* we got a key press */
173 abort = 1; /* don't auto boot */
174 bootdelay = 0; /* no more delay */
175 # ifdef CONFIG_MENUKEY
178 (void) getc(); /* consume input */
183 } while (!abort && get_timer(ts) < 1000);
185 printf("\b\b\b%2d ", bootdelay);
190 #ifdef CONFIG_SILENT_CONSOLE
192 gd->flags &= ~GD_FLG_SILENT;
197 # endif /* CONFIG_AUTOBOOT_KEYED */
199 static int abortboot(int bootdelay)
201 #ifdef CONFIG_AUTOBOOT_KEYED
202 return abortboot_keyed(bootdelay);
204 return abortboot_normal(bootdelay);
209 * Runs the given boot command securely. Specifically:
210 * - Doesn't run the command with the shell (run_command or parse_string_outer),
211 * since that's a lot of code surface that an attacker might exploit.
212 * Because of this, we don't do any argument parsing--the secure boot command
213 * has to be a full-fledged u-boot command.
214 * - Doesn't check for keypresses before booting, since that could be a
215 * security hole; also disables Ctrl-C.
216 * - Doesn't allow the command to return.
218 * Upon any failures, this function will drop into an infinite loop after
219 * printing the error message to console.
222 #if defined(CONFIG_OF_CONTROL)
223 static void secure_boot_cmd(char *cmd)
229 printf("## Error: Secure boot command not specified\n");
233 /* Disable Ctrl-C just in case some command is used that checks it. */
236 /* Find the command directly. */
237 cmdtp = find_cmd(cmd);
239 printf("## Error: \"%s\" not defined\n", cmd);
243 /* Run the command, forcing no flags and faking argc and argv. */
244 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
246 /* Shouldn't ever return from boot command. */
247 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
251 * Not a whole lot to do here. Rebooting won't help much, since we'll
252 * just end up right back here. Just loop.
257 static void process_fdt_options(const void *blob)
261 /* Add an env variable to point to a kernel payload, if available */
262 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
264 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
266 /* Add an env variable to point to a root disk, if available */
267 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
269 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
271 #endif /* CONFIG_OF_CONTROL */
273 void bootdelay_process(void)
275 #ifdef CONFIG_OF_CONTROL
280 #ifdef CONFIG_BOOTCOUNT_LIMIT
281 unsigned long bootcount = 0;
282 unsigned long bootlimit = 0;
283 #endif /* CONFIG_BOOTCOUNT_LIMIT */
285 #ifdef CONFIG_BOOTCOUNT_LIMIT
286 bootcount = bootcount_load();
288 bootcount_store(bootcount);
289 setenv_ulong("bootcount", bootcount);
290 bootlimit = getenv_ulong("bootlimit", 10, 0);
291 #endif /* CONFIG_BOOTCOUNT_LIMIT */
293 s = getenv("bootdelay");
294 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
296 #ifdef CONFIG_OF_CONTROL
297 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
301 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
303 #if defined(CONFIG_MENU_SHOW)
304 bootdelay = menu_show(bootdelay);
306 bootretry_init_cmd_timeout();
309 if (gd->flags & GD_FLG_POSTFAIL) {
310 s = getenv("failbootcmd");
312 #endif /* CONFIG_POST */
313 #ifdef CONFIG_BOOTCOUNT_LIMIT
314 if (bootlimit && (bootcount > bootlimit)) {
315 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
316 (unsigned)bootlimit);
317 s = getenv("altbootcmd");
319 #endif /* CONFIG_BOOTCOUNT_LIMIT */
320 s = getenv("bootcmd");
321 #ifdef CONFIG_OF_CONTROL
322 /* Allow the fdt to override the boot command */
323 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
327 process_fdt_options(gd->fdt_blob);
330 * If the bootsecure option was chosen, use secure_boot_cmd().
331 * Always use 'env' in this case, since bootsecure requres that the
332 * bootcmd was specified in the FDT too.
334 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
335 secure_boot_cmd(env);
337 #endif /* CONFIG_OF_CONTROL */
339 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
341 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
342 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
343 int prev = disable_ctrlc(1); /* disable Control C checking */
346 run_command_list(s, -1, 0);
348 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
349 disable_ctrlc(prev); /* restore Control C checking */
353 #ifdef CONFIG_MENUKEY
354 if (menukey == CONFIG_MENUKEY) {
355 s = getenv("menucmd");
357 run_command_list(s, -1, 0);
359 #endif /* CONFIG_MENUKEY */