1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
22 #include <asm/global_data.h>
23 #include <linux/delay.h>
24 #include <u-boot/sha256.h>
25 #include <bootcount.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 #define DELAY_STOP_STR_MAX_LENGTH 64
32 #ifndef DEBUG_BOOTKEYS
33 #define DEBUG_BOOTKEYS 0
35 #define debug_bootkeys(fmt, args...) \
36 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
38 /* Stored value of bootdelay, used by autoboot_command() */
39 static int stored_bootdelay;
42 #if !defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT)
43 #define CONFIG_AUTOBOOT_STOP_STR_CRYPT ""
45 #if !defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
46 #define CONFIG_AUTOBOOT_STOP_STR_SHA256 ""
49 #ifdef CONFIG_AUTOBOOT_USE_MENUKEY
50 #define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY
52 #define AUTOBOOT_MENUKEY 0
56 * passwd_abort_crypt() - check for a crypt-style hashed key sequence to abort booting
58 * This checks for the user entering a password within a given time.
60 * The entered password is hashed via one of the crypt-style hash methods
61 * and compared to the pre-defined value from either
62 * the environment variable "bootstopkeycrypt"
64 * the config value CONFIG_AUTOBOOT_STOP_STR_CRYPT
66 * In case the config value CONFIG_AUTOBOOT_NEVER_TIMEOUT has been enabled
67 * this function never times out if the user presses the <Enter> key
68 * before starting to enter the password.
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_crypt(uint64_t etime)
75 const char *crypt_env_str = env_get("bootstopkeycrypt");
76 char presskey[DELAY_STOP_STR_MAX_LENGTH];
77 u_int presskey_len = 0;
79 int never_timeout = 0;
82 if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str)
83 crypt_env_str = CONFIG_AUTOBOOT_STOP_STR_CRYPT;
88 /* We expect the stop-string to be newline-terminated */
91 /* Check for input string overflow */
92 if (presskey_len >= sizeof(presskey))
95 presskey[presskey_len] = getchar();
97 if ((presskey[presskey_len] == '\r') ||
98 (presskey[presskey_len] == '\n')) {
99 if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) &&
104 presskey[presskey_len] = '\0';
105 err = crypt_compare(crypt_env_str, presskey,
109 "crypt_compare() failed with: %s\n",
111 /* you had one chance */
117 } while (never_timeout || get_ticks() <= etime);
123 * Use a "constant-length" time compare function for this
126 * https://crackstation.net/hashing-security.htm
128 static int slow_equals(u8 *a, u8 *b, int len)
133 for (i = 0; i < len; i++)
140 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
142 * This checks for the user entering a SHA256 hash within a given time.
144 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
145 * @return 0 if autoboot should continue, 1 if it should stop
147 static int passwd_abort_sha256(uint64_t etime)
149 const char *sha_env_str = env_get("bootstopkeysha256");
150 u8 sha_env[SHA256_SUM_LEN];
154 const char *algo_name = "sha256";
155 u_int presskey_len = 0;
157 int size = sizeof(sha);
160 if (sha_env_str == NULL)
161 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
163 presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH);
164 c = strstr(sha_env_str, ":");
165 if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
166 /* preload presskey with salt */
167 memcpy(presskey, sha_env_str, c - sha_env_str);
168 presskey_len = c - sha_env_str;
172 * Generate the binary value from the environment hash value
173 * so that we can compare this value with the computed hash
174 * from the user input
176 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
178 printf("Hash %s not supported!\n", algo_name);
182 sha = malloc_cache_aligned(SHA256_SUM_LEN);
183 size = SHA256_SUM_LEN;
185 * We don't know how long the stop-string is, so we need to
186 * generate the sha256 hash upon each input character and
187 * compare the value with the one saved in the environment
191 /* Check for input string overflow */
192 if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
198 presskey[presskey_len++] = getchar();
200 /* Calculate sha256 upon each new char */
201 hash_block(algo_name, (const void *)presskey,
202 presskey_len, sha, &size);
204 /* And check if sha matches saved value in env */
205 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
208 } while (!abort && get_ticks() <= etime);
216 * passwd_abort_key() - check for a key sequence to aborted booting
218 * This checks for the user entering a string within a given time.
220 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
221 * @return 0 if autoboot should continue, 1 if it should stop
223 static int passwd_abort_key(uint64_t etime)
232 { .str = env_get("bootdelaykey"), .retry = 1 },
233 { .str = env_get("bootstopkey"), .retry = 0 },
236 char presskey[DELAY_STOP_STR_MAX_LENGTH];
237 int presskey_len = 0;
238 int presskey_max = 0;
241 # ifdef CONFIG_AUTOBOOT_DELAY_STR
242 if (delaykey[0].str == NULL)
243 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
245 # ifdef CONFIG_AUTOBOOT_STOP_STR
246 if (delaykey[1].str == NULL)
247 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
250 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
251 delaykey[i].len = delaykey[i].str == NULL ?
252 0 : strlen(delaykey[i].str);
253 delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
254 DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
256 presskey_max = presskey_max > delaykey[i].len ?
257 presskey_max : delaykey[i].len;
259 debug_bootkeys("%s key:<%s>\n",
260 delaykey[i].retry ? "delay" : "stop",
261 delaykey[i].str ? delaykey[i].str : "NULL");
264 /* In order to keep up with incoming data, check timeout only
269 if (presskey_len < presskey_max) {
270 presskey[presskey_len++] = getchar();
272 for (i = 0; i < presskey_max - 1; i++)
273 presskey[i] = presskey[i + 1];
275 presskey[i] = getchar();
279 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
280 if (delaykey[i].len > 0 &&
281 presskey_len >= delaykey[i].len &&
282 memcmp(presskey + presskey_len -
283 delaykey[i].len, delaykey[i].str,
284 delaykey[i].len) == 0) {
285 debug_bootkeys("got %skey\n",
286 delaykey[i].retry ? "delay" :
289 /* don't retry auto boot */
290 if (!delaykey[i].retry)
291 bootretry_dont_retry();
295 } while (!abort && get_ticks() <= etime);
301 * flush_stdin() - drops all pending characters from stdin
303 static void flush_stdin(void)
310 * fallback_to_sha256() - check whether we should fall back to sha256
313 * This checks for the environment variable `bootstopusesha256` in case
314 * sha256-fallback has been enabled via the config setting
315 * `AUTOBOOT_SHA256_FALLBACK`.
317 * @return `false` if we must not fall-back, `true` if plain sha256 should be tried
319 static bool fallback_to_sha256(void)
321 if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
322 return env_get_yesno("bootstopusesha256") == 1;
323 else if (IS_ENABLED(CONFIG_CRYPT_PW))
329 /***************************************************************************
330 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
331 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
333 static int abortboot_key_sequence(int bootdelay)
336 uint64_t etime = endtick(bootdelay);
338 if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
340 # ifdef CONFIG_AUTOBOOT_PROMPT
342 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
343 * To print the bootdelay value upon bootup.
345 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
348 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
349 if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
350 abort = passwd_abort_crypt(etime);
352 abort = passwd_abort_sha256(etime);
354 abort = passwd_abort_key(etime);
357 debug_bootkeys("key timeout\n");
362 static int abortboot_single_key(int bootdelay)
367 printf("Hit any key to stop autoboot: %2d ", bootdelay);
370 * Check if key already pressed
372 if (tstc()) { /* we got a key press */
373 getchar(); /* consume input */
375 abort = 1; /* don't auto boot */
378 while ((bootdelay > 0) && (!abort)) {
383 if (tstc()) { /* we got a key press */
386 abort = 1; /* don't auto boot */
387 bootdelay = 0; /* no more delay */
388 key = getchar();/* consume input */
389 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
394 } while (!abort && get_timer(ts) < 1000);
396 printf("\b\b\b%2d ", bootdelay);
404 static int abortboot(int bootdelay)
408 if (bootdelay >= 0) {
409 if (autoboot_keyed())
410 abort = abortboot_key_sequence(bootdelay);
412 abort = abortboot_single_key(bootdelay);
415 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
416 gd->flags &= ~GD_FLG_SILENT;
421 static void process_fdt_options(const void *blob)
423 #ifdef CONFIG_SYS_TEXT_BASE
426 /* Add an env variable to point to a kernel payload, if available */
427 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
429 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
431 /* Add an env variable to point to a root disk, if available */
432 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
434 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
435 #endif /* CONFIG_SYS_TEXT_BASE */
438 const char *bootdelay_process(void)
445 s = env_get("bootdelay");
446 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
448 if (IS_ENABLED(CONFIG_OF_CONTROL))
449 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
452 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
454 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
455 bootdelay = menu_show(bootdelay);
456 bootretry_init_cmd_timeout();
459 if (gd->flags & GD_FLG_POSTFAIL) {
460 s = env_get("failbootcmd");
462 #endif /* CONFIG_POST */
463 if (bootcount_error())
464 s = env_get("altbootcmd");
466 s = env_get("bootcmd");
468 if (IS_ENABLED(CONFIG_OF_CONTROL))
469 process_fdt_options(gd->fdt_blob);
470 stored_bootdelay = bootdelay;
475 void autoboot_command(const char *s)
477 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
479 if (s && (stored_bootdelay == -2 ||
480 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
484 lock = autoboot_keyed() &&
485 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
487 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
489 run_command_list(s, -1, 0);
492 disable_ctrlc(prev); /* restore Ctrl-C checking */
495 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
496 menukey == AUTOBOOT_MENUKEY) {
497 s = env_get("menucmd");
499 run_command_list(s, -1, 0);