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 # ifdef CONFIG_BOOT_RETRY_TIME
117 /* don't retry auto boot */
118 if (!delaykey
[i
].retry
)
119 bootretry_dont_retry();
124 } while (!abort
&& get_ticks() <= etime
);
127 debug_bootkeys("key timeout\n");
129 #ifdef CONFIG_SILENT_CONSOLE
131 gd
->flags
&= ~GD_FLG_SILENT
;
137 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
139 #ifdef CONFIG_MENUKEY
143 static int abortboot_normal(int bootdelay
)
148 #ifdef CONFIG_MENUPROMPT
149 printf(CONFIG_MENUPROMPT
);
152 printf("Hit any key to stop autoboot: %2d ", bootdelay
);
155 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
157 * Check if key already pressed
158 * Don't check if bootdelay < 0
160 if (bootdelay
>= 0) {
161 if (tstc()) { /* we got a key press */
162 (void) getc(); /* consume input */
164 abort
= 1; /* don't auto boot */
169 while ((bootdelay
> 0) && (!abort
)) {
174 if (tstc()) { /* we got a key press */
175 abort
= 1; /* don't auto boot */
176 bootdelay
= 0; /* no more delay */
177 # ifdef CONFIG_MENUKEY
180 (void) getc(); /* consume input */
185 } while (!abort
&& get_timer(ts
) < 1000);
187 printf("\b\b\b%2d ", bootdelay
);
192 #ifdef CONFIG_SILENT_CONSOLE
194 gd
->flags
&= ~GD_FLG_SILENT
;
199 # endif /* CONFIG_AUTOBOOT_KEYED */
201 static int abortboot(int bootdelay
)
203 #ifdef CONFIG_AUTOBOOT_KEYED
204 return abortboot_keyed(bootdelay
);
206 return abortboot_normal(bootdelay
);
211 * Runs the given boot command securely. Specifically:
212 * - Doesn't run the command with the shell (run_command or parse_string_outer),
213 * since that's a lot of code surface that an attacker might exploit.
214 * Because of this, we don't do any argument parsing--the secure boot command
215 * has to be a full-fledged u-boot command.
216 * - Doesn't check for keypresses before booting, since that could be a
217 * security hole; also disables Ctrl-C.
218 * - Doesn't allow the command to return.
220 * Upon any failures, this function will drop into an infinite loop after
221 * printing the error message to console.
224 #if defined(CONFIG_OF_CONTROL)
225 static void secure_boot_cmd(char *cmd
)
231 printf("## Error: Secure boot command not specified\n");
235 /* Disable Ctrl-C just in case some command is used that checks it. */
238 /* Find the command directly. */
239 cmdtp
= find_cmd(cmd
);
241 printf("## Error: \"%s\" not defined\n", cmd
);
245 /* Run the command, forcing no flags and faking argc and argv. */
246 rc
= (cmdtp
->cmd
)(cmdtp
, 0, 1, &cmd
);
248 /* Shouldn't ever return from boot command. */
249 printf("## Error: \"%s\" returned (code %d)\n", cmd
, rc
);
253 * Not a whole lot to do here. Rebooting won't help much, since we'll
254 * just end up right back here. Just loop.
259 static void process_fdt_options(const void *blob
)
263 /* Add an env variable to point to a kernel payload, if available */
264 addr
= fdtdec_get_config_int(gd
->fdt_blob
, "kernel-offset", 0);
266 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE
+ addr
));
268 /* Add an env variable to point to a root disk, if available */
269 addr
= fdtdec_get_config_int(gd
->fdt_blob
, "rootdisk-offset", 0);
271 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE
+ addr
));
273 #endif /* CONFIG_OF_CONTROL */
275 void bootdelay_process(void)
277 #ifdef CONFIG_OF_CONTROL
282 #ifdef CONFIG_BOOTCOUNT_LIMIT
283 unsigned long bootcount
= 0;
284 unsigned long bootlimit
= 0;
285 #endif /* CONFIG_BOOTCOUNT_LIMIT */
287 #ifdef CONFIG_BOOTCOUNT_LIMIT
288 bootcount
= bootcount_load();
290 bootcount_store(bootcount
);
291 setenv_ulong("bootcount", bootcount
);
292 bootlimit
= getenv_ulong("bootlimit", 10, 0);
293 #endif /* CONFIG_BOOTCOUNT_LIMIT */
295 s
= getenv("bootdelay");
296 bootdelay
= s
? (int)simple_strtol(s
, NULL
, 10) : CONFIG_BOOTDELAY
;
298 #ifdef CONFIG_OF_CONTROL
299 bootdelay
= fdtdec_get_config_int(gd
->fdt_blob
, "bootdelay",
303 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
305 #if defined(CONFIG_MENU_SHOW)
306 bootdelay
= menu_show(bootdelay
);
308 # ifdef CONFIG_BOOT_RETRY_TIME
310 # endif /* CONFIG_BOOT_RETRY_TIME */
313 if (gd
->flags
& GD_FLG_POSTFAIL
) {
314 s
= getenv("failbootcmd");
316 #endif /* CONFIG_POST */
317 #ifdef CONFIG_BOOTCOUNT_LIMIT
318 if (bootlimit
&& (bootcount
> bootlimit
)) {
319 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
320 (unsigned)bootlimit
);
321 s
= getenv("altbootcmd");
323 #endif /* CONFIG_BOOTCOUNT_LIMIT */
324 s
= getenv("bootcmd");
325 #ifdef CONFIG_OF_CONTROL
326 /* Allow the fdt to override the boot command */
327 env
= fdtdec_get_config_string(gd
->fdt_blob
, "bootcmd");
331 process_fdt_options(gd
->fdt_blob
);
334 * If the bootsecure option was chosen, use secure_boot_cmd().
335 * Always use 'env' in this case, since bootsecure requres that the
336 * bootcmd was specified in the FDT too.
338 if (fdtdec_get_config_int(gd
->fdt_blob
, "bootsecure", 0))
339 secure_boot_cmd(env
);
341 #endif /* CONFIG_OF_CONTROL */
343 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: "<UNDEFINED>");
345 if (bootdelay
!= -1 && s
&& !abortboot(bootdelay
)) {
346 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
347 int prev
= disable_ctrlc(1); /* disable Control C checking */
350 run_command_list(s
, -1, 0);
352 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
353 disable_ctrlc(prev
); /* restore Control C checking */
357 #ifdef CONFIG_MENUKEY
358 if (menukey
== CONFIG_MENUKEY
) {
359 s
= getenv("menucmd");
361 run_command_list(s
, -1, 0);
363 #endif /* CONFIG_MENUKEY */