1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
22 #include <linux/delay.h>
23 #include <u-boot/sha256.h>
24 #include <bootcount.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #define MAX_DELAY_STOP_STR 64
30 #ifndef DEBUG_BOOTKEYS
31 #define DEBUG_BOOTKEYS 0
33 #define debug_bootkeys(fmt, args...) \
34 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
36 /* Stored value of bootdelay, used by autoboot_command() */
37 static int stored_bootdelay;
40 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
41 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
43 #define AUTOBOOT_STOP_STR_SHA256 ""
46 #ifdef CONFIG_USE_AUTOBOOT_MENUKEY
47 #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
49 #define AUTOBOOT_MENUKEY 0
53 * Use a "constant-length" time compare function for this
56 * https://crackstation.net/hashing-security.htm
58 static int slow_equals(u8 *a, u8 *b, int len)
63 for (i = 0; i < len; i++)
70 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
72 * This checks for the user entering a SHA256 hash within a given time.
74 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
75 * @return 0 if autoboot should continue, 1 if it should stop
77 static int passwd_abort_sha256(uint64_t etime)
79 const char *sha_env_str = env_get("bootstopkeysha256");
80 u8 sha_env[SHA256_SUM_LEN];
84 const char *algo_name = "sha256";
85 u_int presskey_len = 0;
87 int size = sizeof(sha);
90 if (sha_env_str == NULL)
91 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
93 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
94 c = strstr(sha_env_str, ":");
95 if (c && (c - sha_env_str < MAX_DELAY_STOP_STR)) {
96 /* preload presskey with salt */
97 memcpy(presskey, sha_env_str, c - sha_env_str);
98 presskey_len = c - sha_env_str;
102 * Generate the binary value from the environment hash value
103 * so that we can compare this value with the computed hash
104 * from the user input
106 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
108 printf("Hash %s not supported!\n", algo_name);
112 sha = malloc_cache_aligned(SHA256_SUM_LEN);
113 size = SHA256_SUM_LEN;
115 * We don't know how long the stop-string is, so we need to
116 * generate the sha256 hash upon each input character and
117 * compare the value with the one saved in the environment
121 /* Check for input string overflow */
122 if (presskey_len >= MAX_DELAY_STOP_STR) {
128 presskey[presskey_len++] = getchar();
130 /* Calculate sha256 upon each new char */
131 hash_block(algo_name, (const void *)presskey,
132 presskey_len, sha, &size);
134 /* And check if sha matches saved value in env */
135 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
138 } while (!abort && get_ticks() <= etime);
146 * passwd_abort_key() - check for a key sequence to aborted booting
148 * This checks for the user entering a string within a given time.
150 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
151 * @return 0 if autoboot should continue, 1 if it should stop
153 static int passwd_abort_key(uint64_t etime)
162 { .str = env_get("bootdelaykey"), .retry = 1 },
163 { .str = env_get("bootstopkey"), .retry = 0 },
166 char presskey[MAX_DELAY_STOP_STR];
167 u_int presskey_len = 0;
168 u_int presskey_max = 0;
171 # ifdef CONFIG_AUTOBOOT_DELAY_STR
172 if (delaykey[0].str == NULL)
173 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
175 # ifdef CONFIG_AUTOBOOT_STOP_STR
176 if (delaykey[1].str == NULL)
177 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
180 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
181 delaykey[i].len = delaykey[i].str == NULL ?
182 0 : strlen(delaykey[i].str);
183 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
184 MAX_DELAY_STOP_STR : delaykey[i].len;
186 presskey_max = presskey_max > delaykey[i].len ?
187 presskey_max : delaykey[i].len;
189 debug_bootkeys("%s key:<%s>\n",
190 delaykey[i].retry ? "delay" : "stop",
191 delaykey[i].str ? delaykey[i].str : "NULL");
194 /* In order to keep up with incoming data, check timeout only
199 if (presskey_len < presskey_max) {
200 presskey[presskey_len++] = getchar();
202 for (i = 0; i < presskey_max - 1; i++)
203 presskey[i] = presskey[i + 1];
205 presskey[i] = getchar();
209 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
210 if (delaykey[i].len > 0 &&
211 presskey_len >= delaykey[i].len &&
212 memcmp(presskey + presskey_len -
213 delaykey[i].len, delaykey[i].str,
214 delaykey[i].len) == 0) {
215 debug_bootkeys("got %skey\n",
216 delaykey[i].retry ? "delay" :
219 /* don't retry auto boot */
220 if (!delaykey[i].retry)
221 bootretry_dont_retry();
225 } while (!abort && get_ticks() <= etime);
230 /***************************************************************************
231 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
232 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
234 static int abortboot_key_sequence(int bootdelay)
237 uint64_t etime = endtick(bootdelay);
239 # ifdef CONFIG_AUTOBOOT_PROMPT
241 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
242 * To print the bootdelay value upon bootup.
244 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
247 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
248 abort = passwd_abort_sha256(etime);
250 abort = passwd_abort_key(etime);
252 debug_bootkeys("key timeout\n");
257 static int abortboot_single_key(int bootdelay)
262 printf("Hit any key to stop autoboot: %2d ", bootdelay);
265 * Check if key already pressed
267 if (tstc()) { /* we got a key press */
268 getchar(); /* consume input */
270 abort = 1; /* don't auto boot */
273 while ((bootdelay > 0) && (!abort)) {
278 if (tstc()) { /* we got a key press */
281 abort = 1; /* don't auto boot */
282 bootdelay = 0; /* no more delay */
283 key = getchar();/* consume input */
284 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
289 } while (!abort && get_timer(ts) < 1000);
291 printf("\b\b\b%2d ", bootdelay);
299 static int abortboot(int bootdelay)
303 if (bootdelay >= 0) {
304 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
305 abort = abortboot_key_sequence(bootdelay);
307 abort = abortboot_single_key(bootdelay);
310 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
311 gd->flags &= ~GD_FLG_SILENT;
316 static void process_fdt_options(const void *blob)
318 #ifdef CONFIG_SYS_TEXT_BASE
321 /* Add an env variable to point to a kernel payload, if available */
322 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
324 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
326 /* Add an env variable to point to a root disk, if available */
327 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
329 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
330 #endif /* CONFIG_SYS_TEXT_BASE */
333 const char *bootdelay_process(void)
340 s = env_get("bootdelay");
341 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
343 if (IS_ENABLED(CONFIG_OF_CONTROL))
344 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
347 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
349 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
350 bootdelay = menu_show(bootdelay);
351 bootretry_init_cmd_timeout();
354 if (gd->flags & GD_FLG_POSTFAIL) {
355 s = env_get("failbootcmd");
357 #endif /* CONFIG_POST */
358 if (bootcount_error())
359 s = env_get("altbootcmd");
361 s = env_get("bootcmd");
363 if (IS_ENABLED(CONFIG_OF_CONTROL))
364 process_fdt_options(gd->fdt_blob);
365 stored_bootdelay = bootdelay;
370 void autoboot_command(const char *s)
372 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
374 if (s && (stored_bootdelay == -2 ||
375 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
379 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
380 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
382 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
384 run_command_list(s, -1, 0);
387 disable_ctrlc(prev); /* restore Ctrl-C checking */
390 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
391 menukey == AUTOBOOT_MENUKEY) {
392 s = env_get("menucmd");
394 run_command_list(s, -1, 0);