1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
20 #include <u-boot/sha256.h>
21 #include <bootcount.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #define MAX_DELAY_STOP_STR 32
27 #ifndef DEBUG_BOOTKEYS
28 #define DEBUG_BOOTKEYS 0
30 #define debug_bootkeys(fmt, args...) \
31 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
33 /* Stored value of bootdelay, used by autoboot_command() */
34 static int stored_bootdelay;
37 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
38 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
40 #define AUTOBOOT_STOP_STR_SHA256 ""
43 #ifdef CONFIG_USE_AUTOBOOT_MENUKEY
44 #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
46 #define AUTOBOOT_MENUKEY 0
50 * Use a "constant-length" time compare function for this
53 * https://crackstation.net/hashing-security.htm
55 static int slow_equals(u8 *a, u8 *b, int len)
60 for (i = 0; i < len; i++)
67 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
69 * This checks for the user entering a SHA256 hash within a given time.
71 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
72 * @return 0 if autoboot should continue, 1 if it should stop
74 static int passwd_abort_sha256(uint64_t etime)
76 const char *sha_env_str = env_get("bootstopkeysha256");
77 u8 sha_env[SHA256_SUM_LEN];
80 const char *algo_name = "sha256";
81 u_int presskey_len = 0;
83 int size = sizeof(sha);
86 if (sha_env_str == NULL)
87 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
90 * Generate the binary value from the environment hash value
91 * so that we can compare this value with the computed hash
94 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
96 printf("Hash %s not supported!\n", algo_name);
100 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
101 sha = malloc_cache_aligned(SHA256_SUM_LEN);
102 size = SHA256_SUM_LEN;
104 * We don't know how long the stop-string is, so we need to
105 * generate the sha256 hash upon each input character and
106 * compare the value with the one saved in the environment
110 /* Check for input string overflow */
111 if (presskey_len >= MAX_DELAY_STOP_STR) {
117 presskey[presskey_len++] = getc();
119 /* Calculate sha256 upon each new char */
120 hash_block(algo_name, (const void *)presskey,
121 presskey_len, sha, &size);
123 /* And check if sha matches saved value in env */
124 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
127 } while (!abort && get_ticks() <= etime);
135 * passwd_abort_key() - check for a key sequence to aborted booting
137 * This checks for the user entering a string within a given time.
139 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
140 * @return 0 if autoboot should continue, 1 if it should stop
142 static int passwd_abort_key(uint64_t etime)
151 { .str = env_get("bootdelaykey"), .retry = 1 },
152 { .str = env_get("bootstopkey"), .retry = 0 },
155 char presskey[MAX_DELAY_STOP_STR];
156 u_int presskey_len = 0;
157 u_int presskey_max = 0;
160 # ifdef CONFIG_AUTOBOOT_DELAY_STR
161 if (delaykey[0].str == NULL)
162 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
164 # ifdef CONFIG_AUTOBOOT_STOP_STR
165 if (delaykey[1].str == NULL)
166 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
169 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
170 delaykey[i].len = delaykey[i].str == NULL ?
171 0 : strlen(delaykey[i].str);
172 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
173 MAX_DELAY_STOP_STR : delaykey[i].len;
175 presskey_max = presskey_max > delaykey[i].len ?
176 presskey_max : delaykey[i].len;
178 debug_bootkeys("%s key:<%s>\n",
179 delaykey[i].retry ? "delay" : "stop",
180 delaykey[i].str ? delaykey[i].str : "NULL");
183 /* In order to keep up with incoming data, check timeout only
188 if (presskey_len < presskey_max) {
189 presskey[presskey_len++] = getc();
191 for (i = 0; i < presskey_max - 1; i++)
192 presskey[i] = presskey[i + 1];
194 presskey[i] = getc();
198 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
199 if (delaykey[i].len > 0 &&
200 presskey_len >= delaykey[i].len &&
201 memcmp(presskey + presskey_len -
202 delaykey[i].len, delaykey[i].str,
203 delaykey[i].len) == 0) {
204 debug_bootkeys("got %skey\n",
205 delaykey[i].retry ? "delay" :
208 /* don't retry auto boot */
209 if (!delaykey[i].retry)
210 bootretry_dont_retry();
214 } while (!abort && get_ticks() <= etime);
219 /***************************************************************************
220 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
221 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
223 static int abortboot_key_sequence(int bootdelay)
226 uint64_t etime = endtick(bootdelay);
228 # ifdef CONFIG_AUTOBOOT_PROMPT
230 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
231 * To print the bootdelay value upon bootup.
233 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
236 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
237 abort = passwd_abort_sha256(etime);
239 abort = passwd_abort_key(etime);
241 debug_bootkeys("key timeout\n");
246 static int abortboot_single_key(int bootdelay)
251 printf("Hit any key to stop autoboot: %2d ", bootdelay);
254 * Check if key already pressed
256 if (tstc()) { /* we got a key press */
257 (void) getc(); /* consume input */
259 abort = 1; /* don't auto boot */
262 while ((bootdelay > 0) && (!abort)) {
267 if (tstc()) { /* we got a key press */
270 abort = 1; /* don't auto boot */
271 bootdelay = 0; /* no more delay */
272 key = getc(); /* consume input */
273 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
278 } while (!abort && get_timer(ts) < 1000);
280 printf("\b\b\b%2d ", bootdelay);
288 static int abortboot(int bootdelay)
292 if (bootdelay >= 0) {
293 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
294 abort = abortboot_key_sequence(bootdelay);
296 abort = abortboot_single_key(bootdelay);
299 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
300 gd->flags &= ~GD_FLG_SILENT;
305 static void process_fdt_options(const void *blob)
307 #ifdef CONFIG_SYS_TEXT_BASE
310 /* Add an env variable to point to a kernel payload, if available */
311 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
313 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
315 /* Add an env variable to point to a root disk, if available */
316 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
318 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
319 #endif /* CONFIG_SYS_TEXT_BASE */
322 const char *bootdelay_process(void)
329 s = env_get("bootdelay");
330 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
332 if (IS_ENABLED(CONFIG_OF_CONTROL))
333 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
336 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
338 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
339 bootdelay = menu_show(bootdelay);
340 bootretry_init_cmd_timeout();
343 if (gd->flags & GD_FLG_POSTFAIL) {
344 s = env_get("failbootcmd");
346 #endif /* CONFIG_POST */
347 if (bootcount_error())
348 s = env_get("altbootcmd");
350 s = env_get("bootcmd");
352 if (IS_ENABLED(CONFIG_OF_CONTROL))
353 process_fdt_options(gd->fdt_blob);
354 stored_bootdelay = bootdelay;
359 void autoboot_command(const char *s)
361 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
363 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
367 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
368 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
370 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
372 run_command_list(s, -1, 0);
375 disable_ctrlc(prev); /* restore Ctrl-C checking */
378 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
379 menukey == AUTOBOOT_MENUKEY) {
380 s = env_get("menucmd");
382 run_command_list(s, -1, 0);