1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
19 #include <u-boot/sha256.h>
20 #include <bootcount.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #define MAX_DELAY_STOP_STR 32
26 #ifndef DEBUG_BOOTKEYS
27 #define DEBUG_BOOTKEYS 0
29 #define debug_bootkeys(fmt, args...) \
30 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
32 /* Stored value of bootdelay, used by autoboot_command() */
33 static int stored_bootdelay;
36 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
37 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
39 #define AUTOBOOT_STOP_STR_SHA256 ""
42 #ifdef CONFIG_USE_AUTOBOOT_MENUKEY
43 #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
45 #define AUTOBOOT_MENUKEY 0
49 * Use a "constant-length" time compare function for this
52 * https://crackstation.net/hashing-security.htm
54 static int slow_equals(u8 *a, u8 *b, int len)
59 for (i = 0; i < len; i++)
66 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
68 * This checks for the user entering a SHA256 hash within a given time.
70 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
71 * @return 0 if autoboot should continue, 1 if it should stop
73 static int passwd_abort_sha256(uint64_t etime)
75 const char *sha_env_str = env_get("bootstopkeysha256");
76 u8 sha_env[SHA256_SUM_LEN];
79 const char *algo_name = "sha256";
80 u_int presskey_len = 0;
82 int size = sizeof(sha);
85 if (sha_env_str == NULL)
86 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
89 * Generate the binary value from the environment hash value
90 * so that we can compare this value with the computed hash
93 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
95 printf("Hash %s not supported!\n", algo_name);
99 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
100 sha = malloc_cache_aligned(SHA256_SUM_LEN);
101 size = SHA256_SUM_LEN;
103 * We don't know how long the stop-string is, so we need to
104 * generate the sha256 hash upon each input character and
105 * compare the value with the one saved in the environment
109 /* Check for input string overflow */
110 if (presskey_len >= MAX_DELAY_STOP_STR) {
116 presskey[presskey_len++] = getc();
118 /* Calculate sha256 upon each new char */
119 hash_block(algo_name, (const void *)presskey,
120 presskey_len, sha, &size);
122 /* And check if sha matches saved value in env */
123 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
126 } while (!abort && get_ticks() <= etime);
134 * passwd_abort_key() - check for a key sequence to aborted booting
136 * This checks for the user entering a string within a given time.
138 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
139 * @return 0 if autoboot should continue, 1 if it should stop
141 static int passwd_abort_key(uint64_t etime)
150 { .str = env_get("bootdelaykey"), .retry = 1 },
151 { .str = env_get("bootstopkey"), .retry = 0 },
154 char presskey[MAX_DELAY_STOP_STR];
155 u_int presskey_len = 0;
156 u_int presskey_max = 0;
159 # ifdef CONFIG_AUTOBOOT_DELAY_STR
160 if (delaykey[0].str == NULL)
161 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
163 # ifdef CONFIG_AUTOBOOT_STOP_STR
164 if (delaykey[1].str == NULL)
165 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
168 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
169 delaykey[i].len = delaykey[i].str == NULL ?
170 0 : strlen(delaykey[i].str);
171 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
172 MAX_DELAY_STOP_STR : delaykey[i].len;
174 presskey_max = presskey_max > delaykey[i].len ?
175 presskey_max : delaykey[i].len;
177 debug_bootkeys("%s key:<%s>\n",
178 delaykey[i].retry ? "delay" : "stop",
179 delaykey[i].str ? delaykey[i].str : "NULL");
182 /* In order to keep up with incoming data, check timeout only
187 if (presskey_len < presskey_max) {
188 presskey[presskey_len++] = getc();
190 for (i = 0; i < presskey_max - 1; i++)
191 presskey[i] = presskey[i + 1];
193 presskey[i] = getc();
197 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
198 if (delaykey[i].len > 0 &&
199 presskey_len >= delaykey[i].len &&
200 memcmp(presskey + presskey_len -
201 delaykey[i].len, delaykey[i].str,
202 delaykey[i].len) == 0) {
203 debug_bootkeys("got %skey\n",
204 delaykey[i].retry ? "delay" :
207 /* don't retry auto boot */
208 if (!delaykey[i].retry)
209 bootretry_dont_retry();
213 } while (!abort && get_ticks() <= etime);
218 /***************************************************************************
219 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
220 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
222 static int abortboot_key_sequence(int bootdelay)
225 uint64_t etime = endtick(bootdelay);
227 # ifdef CONFIG_AUTOBOOT_PROMPT
229 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
230 * To print the bootdelay value upon bootup.
232 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
235 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
236 abort = passwd_abort_sha256(etime);
238 abort = passwd_abort_key(etime);
240 debug_bootkeys("key timeout\n");
245 static int abortboot_single_key(int bootdelay)
250 printf("Hit any key to stop autoboot: %2d ", bootdelay);
253 * Check if key already pressed
255 if (tstc()) { /* we got a key press */
256 (void) getc(); /* consume input */
258 abort = 1; /* don't auto boot */
261 while ((bootdelay > 0) && (!abort)) {
266 if (tstc()) { /* we got a key press */
269 abort = 1; /* don't auto boot */
270 bootdelay = 0; /* no more delay */
271 key = getc(); /* consume input */
272 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
277 } while (!abort && get_timer(ts) < 1000);
279 printf("\b\b\b%2d ", bootdelay);
287 static int abortboot(int bootdelay)
291 if (bootdelay >= 0) {
292 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
293 abort = abortboot_key_sequence(bootdelay);
295 abort = abortboot_single_key(bootdelay);
298 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
299 gd->flags &= ~GD_FLG_SILENT;
304 static void process_fdt_options(const void *blob)
306 #ifdef CONFIG_SYS_TEXT_BASE
309 /* Add an env variable to point to a kernel payload, if available */
310 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
312 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
314 /* Add an env variable to point to a root disk, if available */
315 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
317 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
318 #endif /* CONFIG_SYS_TEXT_BASE */
321 const char *bootdelay_process(void)
328 s = env_get("bootdelay");
329 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
331 if (IS_ENABLED(CONFIG_OF_CONTROL))
332 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
335 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
337 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
338 bootdelay = menu_show(bootdelay);
339 bootretry_init_cmd_timeout();
342 if (gd->flags & GD_FLG_POSTFAIL) {
343 s = env_get("failbootcmd");
345 #endif /* CONFIG_POST */
346 if (bootcount_error())
347 s = env_get("altbootcmd");
349 s = env_get("bootcmd");
351 if (IS_ENABLED(CONFIG_OF_CONTROL))
352 process_fdt_options(gd->fdt_blob);
353 stored_bootdelay = bootdelay;
358 void autoboot_command(const char *s)
360 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
362 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
366 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
367 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
369 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
371 run_command_list(s, -1, 0);
374 disable_ctrlc(prev); /* restore Ctrl-C checking */
377 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
378 menukey == AUTOBOOT_MENUKEY) {
379 s = env_get("menucmd");
381 run_command_list(s, -1, 0);