3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
14 DECLARE_GLOBAL_DATA_PTR;
16 #define MAX_DELAY_STOP_STR 32
18 #ifndef DEBUG_BOOTKEYS
19 #define DEBUG_BOOTKEYS 0
21 #define debug_bootkeys(fmt, args...) \
22 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
24 /***************************************************************************
25 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
26 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
28 # if defined(CONFIG_AUTOBOOT_KEYED)
29 static int abortboot_keyed(int bootdelay)
32 uint64_t etime = endtick(bootdelay);
39 { str: getenv("bootdelaykey"), retry: 1 },
40 { str: getenv("bootdelaykey2"), retry: 1 },
41 { str: getenv("bootstopkey"), retry: 0 },
42 { str: getenv("bootstopkey2"), retry: 0 },
45 char presskey[MAX_DELAY_STOP_STR];
46 u_int presskey_len = 0;
47 u_int presskey_max = 0;
50 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
55 # ifdef CONFIG_AUTOBOOT_PROMPT
56 printf(CONFIG_AUTOBOOT_PROMPT);
59 # ifdef CONFIG_AUTOBOOT_DELAY_STR
60 if (delaykey[0].str == NULL)
61 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
63 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
64 if (delaykey[1].str == NULL)
65 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
67 # ifdef CONFIG_AUTOBOOT_STOP_STR
68 if (delaykey[2].str == NULL)
69 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
71 # ifdef CONFIG_AUTOBOOT_STOP_STR2
72 if (delaykey[3].str == NULL)
73 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
76 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
77 delaykey[i].len = delaykey[i].str == NULL ?
78 0 : strlen(delaykey[i].str);
79 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
80 MAX_DELAY_STOP_STR : delaykey[i].len;
82 presskey_max = presskey_max > delaykey[i].len ?
83 presskey_max : delaykey[i].len;
85 debug_bootkeys("%s key:<%s>\n",
86 delaykey[i].retry ? "delay" : "stop",
87 delaykey[i].str ? delaykey[i].str : "NULL");
90 /* In order to keep up with incoming data, check timeout only
95 if (presskey_len < presskey_max) {
96 presskey[presskey_len++] = getc();
98 for (i = 0; i < presskey_max - 1; i++)
99 presskey[i] = presskey[i + 1];
101 presskey[i] = getc();
105 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
106 if (delaykey[i].len > 0 &&
107 presskey_len >= delaykey[i].len &&
108 memcmp(presskey + presskey_len -
109 delaykey[i].len, delaykey[i].str,
110 delaykey[i].len) == 0) {
111 debug_bootkeys("got %skey\n",
112 delaykey[i].retry ? "delay" :
115 # ifdef CONFIG_BOOT_RETRY_TIME
116 /* don't retry auto boot */
117 if (!delaykey[i].retry)
118 bootretry_dont_retry();
123 } while (!abort && get_ticks() <= etime);
126 debug_bootkeys("key timeout\n");
128 #ifdef CONFIG_SILENT_CONSOLE
130 gd->flags &= ~GD_FLG_SILENT;
136 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
138 #ifdef CONFIG_MENUKEY
142 static int abortboot_normal(int bootdelay)
147 #ifdef CONFIG_MENUPROMPT
148 printf(CONFIG_MENUPROMPT);
151 printf("Hit any key to stop autoboot: %2d ", bootdelay);
154 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
156 * Check if key already pressed
157 * Don't check if bootdelay < 0
159 if (bootdelay >= 0) {
160 if (tstc()) { /* we got a key press */
161 (void) getc(); /* consume input */
163 abort = 1; /* don't auto boot */
168 while ((bootdelay > 0) && (!abort)) {
173 if (tstc()) { /* we got a key press */
174 abort = 1; /* don't auto boot */
175 bootdelay = 0; /* no more delay */
176 # ifdef CONFIG_MENUKEY
179 (void) getc(); /* consume input */
184 } while (!abort && get_timer(ts) < 1000);
186 printf("\b\b\b%2d ", bootdelay);
191 #ifdef CONFIG_SILENT_CONSOLE
193 gd->flags &= ~GD_FLG_SILENT;
198 # endif /* CONFIG_AUTOBOOT_KEYED */
200 static int abortboot(int bootdelay)
202 #ifdef CONFIG_AUTOBOOT_KEYED
203 return abortboot_keyed(bootdelay);
205 return abortboot_normal(bootdelay);
210 * Runs the given boot command securely. Specifically:
211 * - Doesn't run the command with the shell (run_command or parse_string_outer),
212 * since that's a lot of code surface that an attacker might exploit.
213 * Because of this, we don't do any argument parsing--the secure boot command
214 * has to be a full-fledged u-boot command.
215 * - Doesn't check for keypresses before booting, since that could be a
216 * security hole; also disables Ctrl-C.
217 * - Doesn't allow the command to return.
219 * Upon any failures, this function will drop into an infinite loop after
220 * printing the error message to console.
223 #if defined(CONFIG_OF_CONTROL)
224 static void secure_boot_cmd(char *cmd)
230 printf("## Error: Secure boot command not specified\n");
234 /* Disable Ctrl-C just in case some command is used that checks it. */
237 /* Find the command directly. */
238 cmdtp = find_cmd(cmd);
240 printf("## Error: \"%s\" not defined\n", cmd);
244 /* Run the command, forcing no flags and faking argc and argv. */
245 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
247 /* Shouldn't ever return from boot command. */
248 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
252 * Not a whole lot to do here. Rebooting won't help much, since we'll
253 * just end up right back here. Just loop.
258 static void process_fdt_options(const void *blob)
262 /* Add an env variable to point to a kernel payload, if available */
263 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
265 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
267 /* Add an env variable to point to a root disk, if available */
268 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
270 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
272 #endif /* CONFIG_OF_CONTROL */
274 void bootdelay_process(void)
276 #ifdef CONFIG_OF_CONTROL
281 #ifdef CONFIG_BOOTCOUNT_LIMIT
282 unsigned long bootcount = 0;
283 unsigned long bootlimit = 0;
284 #endif /* CONFIG_BOOTCOUNT_LIMIT */
286 #ifdef CONFIG_BOOTCOUNT_LIMIT
287 bootcount = bootcount_load();
289 bootcount_store(bootcount);
290 setenv_ulong("bootcount", bootcount);
291 bootlimit = getenv_ulong("bootlimit", 10, 0);
292 #endif /* CONFIG_BOOTCOUNT_LIMIT */
294 s = getenv("bootdelay");
295 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
297 #ifdef CONFIG_OF_CONTROL
298 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
302 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
304 #if defined(CONFIG_MENU_SHOW)
305 bootdelay = menu_show(bootdelay);
307 # ifdef CONFIG_BOOT_RETRY_TIME
309 # endif /* CONFIG_BOOT_RETRY_TIME */
312 if (gd->flags & GD_FLG_POSTFAIL) {
313 s = getenv("failbootcmd");
315 #endif /* CONFIG_POST */
316 #ifdef CONFIG_BOOTCOUNT_LIMIT
317 if (bootlimit && (bootcount > bootlimit)) {
318 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
319 (unsigned)bootlimit);
320 s = getenv("altbootcmd");
322 #endif /* CONFIG_BOOTCOUNT_LIMIT */
323 s = getenv("bootcmd");
324 #ifdef CONFIG_OF_CONTROL
325 /* Allow the fdt to override the boot command */
326 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
330 process_fdt_options(gd->fdt_blob);
333 * If the bootsecure option was chosen, use secure_boot_cmd().
334 * Always use 'env' in this case, since bootsecure requres that the
335 * bootcmd was specified in the FDT too.
337 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
338 secure_boot_cmd(env);
340 #endif /* CONFIG_OF_CONTROL */
342 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
344 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
345 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
346 int prev = disable_ctrlc(1); /* disable Control C checking */
349 run_command_list(s, -1, 0);
351 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
352 disable_ctrlc(prev); /* restore Control C checking */
356 #ifdef CONFIG_MENUKEY
357 if (menukey == CONFIG_MENUKEY) {
358 s = getenv("menucmd");
360 run_command_list(s, -1, 0);
362 #endif /* CONFIG_MENUKEY */