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 MAX_DELAY_STOP_STR 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 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
67 * @return 0 if autoboot should continue, 1 if it should stop
69 static int passwd_abort_crypt(uint64_t etime)
71 const char *crypt_env_str = env_get("bootstopkeycrypt");
72 char presskey[MAX_DELAY_STOP_STR];
73 u_int presskey_len = 0;
77 if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str)
78 crypt_env_str = CONFIG_AUTOBOOT_STOP_STR_CRYPT;
83 /* We expect the stop-string to be newline-terminated */
86 /* Check for input string overflow */
87 if (presskey_len >= sizeof(presskey))
90 presskey[presskey_len] = getchar();
92 if ((presskey[presskey_len] == '\r') ||
93 (presskey[presskey_len] == '\n')) {
94 presskey[presskey_len] = '\0';
95 err = crypt_compare(crypt_env_str, presskey,
99 "crypt_compare() failed with: %s\n",
101 /* you had one chance */
107 } while (get_ticks() <= etime);
113 * Use a "constant-length" time compare function for this
116 * https://crackstation.net/hashing-security.htm
118 static int slow_equals(u8 *a, u8 *b, int len)
123 for (i = 0; i < len; i++)
130 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
132 * This checks for the user entering a SHA256 hash within a given time.
134 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
135 * @return 0 if autoboot should continue, 1 if it should stop
137 static int passwd_abort_sha256(uint64_t etime)
139 const char *sha_env_str = env_get("bootstopkeysha256");
140 u8 sha_env[SHA256_SUM_LEN];
144 const char *algo_name = "sha256";
145 u_int presskey_len = 0;
147 int size = sizeof(sha);
150 if (sha_env_str == NULL)
151 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
153 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
154 c = strstr(sha_env_str, ":");
155 if (c && (c - sha_env_str < MAX_DELAY_STOP_STR)) {
156 /* preload presskey with salt */
157 memcpy(presskey, sha_env_str, c - sha_env_str);
158 presskey_len = c - sha_env_str;
162 * Generate the binary value from the environment hash value
163 * so that we can compare this value with the computed hash
164 * from the user input
166 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
168 printf("Hash %s not supported!\n", algo_name);
172 sha = malloc_cache_aligned(SHA256_SUM_LEN);
173 size = SHA256_SUM_LEN;
175 * We don't know how long the stop-string is, so we need to
176 * generate the sha256 hash upon each input character and
177 * compare the value with the one saved in the environment
181 /* Check for input string overflow */
182 if (presskey_len >= MAX_DELAY_STOP_STR) {
188 presskey[presskey_len++] = getchar();
190 /* Calculate sha256 upon each new char */
191 hash_block(algo_name, (const void *)presskey,
192 presskey_len, sha, &size);
194 /* And check if sha matches saved value in env */
195 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
198 } while (!abort && get_ticks() <= etime);
206 * passwd_abort_key() - check for a key sequence to aborted booting
208 * This checks for the user entering a string within a given time.
210 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
211 * @return 0 if autoboot should continue, 1 if it should stop
213 static int passwd_abort_key(uint64_t etime)
222 { .str = env_get("bootdelaykey"), .retry = 1 },
223 { .str = env_get("bootstopkey"), .retry = 0 },
226 char presskey[MAX_DELAY_STOP_STR];
227 int presskey_len = 0;
228 int presskey_max = 0;
231 # ifdef CONFIG_AUTOBOOT_DELAY_STR
232 if (delaykey[0].str == NULL)
233 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
235 # ifdef CONFIG_AUTOBOOT_STOP_STR
236 if (delaykey[1].str == NULL)
237 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
240 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
241 delaykey[i].len = delaykey[i].str == NULL ?
242 0 : strlen(delaykey[i].str);
243 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
244 MAX_DELAY_STOP_STR : delaykey[i].len;
246 presskey_max = presskey_max > delaykey[i].len ?
247 presskey_max : delaykey[i].len;
249 debug_bootkeys("%s key:<%s>\n",
250 delaykey[i].retry ? "delay" : "stop",
251 delaykey[i].str ? delaykey[i].str : "NULL");
254 /* In order to keep up with incoming data, check timeout only
259 if (presskey_len < presskey_max) {
260 presskey[presskey_len++] = getchar();
262 for (i = 0; i < presskey_max - 1; i++)
263 presskey[i] = presskey[i + 1];
265 presskey[i] = getchar();
269 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
270 if (delaykey[i].len > 0 &&
271 presskey_len >= delaykey[i].len &&
272 memcmp(presskey + presskey_len -
273 delaykey[i].len, delaykey[i].str,
274 delaykey[i].len) == 0) {
275 debug_bootkeys("got %skey\n",
276 delaykey[i].retry ? "delay" :
279 /* don't retry auto boot */
280 if (!delaykey[i].retry)
281 bootretry_dont_retry();
285 } while (!abort && get_ticks() <= etime);
290 /***************************************************************************
291 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
292 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
294 static int abortboot_key_sequence(int bootdelay)
297 uint64_t etime = endtick(bootdelay);
299 # ifdef CONFIG_AUTOBOOT_PROMPT
301 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
302 * To print the bootdelay value upon bootup.
304 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
307 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
308 if (IS_ENABLED(CONFIG_CRYPT_PW))
309 abort = passwd_abort_crypt(etime);
311 abort = passwd_abort_sha256(etime);
313 abort = passwd_abort_key(etime);
316 debug_bootkeys("key timeout\n");
321 static int abortboot_single_key(int bootdelay)
326 printf("Hit any key to stop autoboot: %2d ", bootdelay);
329 * Check if key already pressed
331 if (tstc()) { /* we got a key press */
332 getchar(); /* consume input */
334 abort = 1; /* don't auto boot */
337 while ((bootdelay > 0) && (!abort)) {
342 if (tstc()) { /* we got a key press */
345 abort = 1; /* don't auto boot */
346 bootdelay = 0; /* no more delay */
347 key = getchar();/* consume input */
348 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
353 } while (!abort && get_timer(ts) < 1000);
355 printf("\b\b\b%2d ", bootdelay);
363 static int abortboot(int bootdelay)
367 if (bootdelay >= 0) {
368 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
369 abort = abortboot_key_sequence(bootdelay);
371 abort = abortboot_single_key(bootdelay);
374 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
375 gd->flags &= ~GD_FLG_SILENT;
380 static void process_fdt_options(const void *blob)
382 #ifdef CONFIG_SYS_TEXT_BASE
385 /* Add an env variable to point to a kernel payload, if available */
386 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
388 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
390 /* Add an env variable to point to a root disk, if available */
391 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
393 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
394 #endif /* CONFIG_SYS_TEXT_BASE */
397 const char *bootdelay_process(void)
404 s = env_get("bootdelay");
405 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
407 if (IS_ENABLED(CONFIG_OF_CONTROL))
408 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
411 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
413 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
414 bootdelay = menu_show(bootdelay);
415 bootretry_init_cmd_timeout();
418 if (gd->flags & GD_FLG_POSTFAIL) {
419 s = env_get("failbootcmd");
421 #endif /* CONFIG_POST */
422 if (bootcount_error())
423 s = env_get("altbootcmd");
425 s = env_get("bootcmd");
427 if (IS_ENABLED(CONFIG_OF_CONTROL))
428 process_fdt_options(gd->fdt_blob);
429 stored_bootdelay = bootdelay;
434 void autoboot_command(const char *s)
436 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
438 if (s && (stored_bootdelay == -2 ||
439 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
443 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
444 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
446 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
448 run_command_list(s, -1, 0);
451 disable_ctrlc(prev); /* restore Ctrl-C checking */
454 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
455 menukey == AUTOBOOT_MENUKEY) {
456 s = env_get("menucmd");
458 run_command_list(s, -1, 0);