1 // SPDX-License-Identifier: GPL-2.0+
22 #include <u-boot/sha256.h>
23 #include <bootcount.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 #define MAX_DELAY_STOP_STR 32
29 #ifndef DEBUG_BOOTKEYS
30 #define DEBUG_BOOTKEYS 0
32 #define debug_bootkeys(fmt, args...) \
33 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
35 /* Stored value of bootdelay, used by autoboot_command() */
36 static int stored_bootdelay;
39 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
40 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
42 #define AUTOBOOT_STOP_STR_SHA256 ""
45 #ifdef CONFIG_USE_AUTOBOOT_MENUKEY
46 #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
48 #define AUTOBOOT_MENUKEY 0
52 * Use a "constant-length" time compare function for this
55 * https://crackstation.net/hashing-security.htm
57 static int slow_equals(u8 *a, u8 *b, int len)
62 for (i = 0; i < len; i++)
69 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
71 * This checks for the user entering a SHA256 hash within a given time.
73 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
74 * @return 0 if autoboot should continue, 1 if it should stop
76 static int passwd_abort_sha256(uint64_t etime)
78 const char *sha_env_str = env_get("bootstopkeysha256");
79 u8 sha_env[SHA256_SUM_LEN];
82 const char *algo_name = "sha256";
83 u_int presskey_len = 0;
85 int size = sizeof(sha);
88 if (sha_env_str == NULL)
89 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
92 * Generate the binary value from the environment hash value
93 * so that we can compare this value with the computed hash
96 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
98 printf("Hash %s not supported!\n", algo_name);
102 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
103 sha = malloc_cache_aligned(SHA256_SUM_LEN);
104 size = SHA256_SUM_LEN;
106 * We don't know how long the stop-string is, so we need to
107 * generate the sha256 hash upon each input character and
108 * compare the value with the one saved in the environment
112 /* Check for input string overflow */
113 if (presskey_len >= MAX_DELAY_STOP_STR) {
119 presskey[presskey_len++] = getc();
121 /* Calculate sha256 upon each new char */
122 hash_block(algo_name, (const void *)presskey,
123 presskey_len, sha, &size);
125 /* And check if sha matches saved value in env */
126 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
129 } while (!abort && get_ticks() <= etime);
137 * passwd_abort_key() - check for a key sequence to aborted booting
139 * This checks for the user entering a string within a given time.
141 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
142 * @return 0 if autoboot should continue, 1 if it should stop
144 static int passwd_abort_key(uint64_t etime)
153 { .str = env_get("bootdelaykey"), .retry = 1 },
154 { .str = env_get("bootstopkey"), .retry = 0 },
157 char presskey[MAX_DELAY_STOP_STR];
158 u_int presskey_len = 0;
159 u_int presskey_max = 0;
162 # ifdef CONFIG_AUTOBOOT_DELAY_STR
163 if (delaykey[0].str == NULL)
164 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
166 # ifdef CONFIG_AUTOBOOT_STOP_STR
167 if (delaykey[1].str == NULL)
168 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
171 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
172 delaykey[i].len = delaykey[i].str == NULL ?
173 0 : strlen(delaykey[i].str);
174 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
175 MAX_DELAY_STOP_STR : delaykey[i].len;
177 presskey_max = presskey_max > delaykey[i].len ?
178 presskey_max : delaykey[i].len;
180 debug_bootkeys("%s key:<%s>\n",
181 delaykey[i].retry ? "delay" : "stop",
182 delaykey[i].str ? delaykey[i].str : "NULL");
185 /* In order to keep up with incoming data, check timeout only
190 if (presskey_len < presskey_max) {
191 presskey[presskey_len++] = getc();
193 for (i = 0; i < presskey_max - 1; i++)
194 presskey[i] = presskey[i + 1];
196 presskey[i] = getc();
200 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
201 if (delaykey[i].len > 0 &&
202 presskey_len >= delaykey[i].len &&
203 memcmp(presskey + presskey_len -
204 delaykey[i].len, delaykey[i].str,
205 delaykey[i].len) == 0) {
206 debug_bootkeys("got %skey\n",
207 delaykey[i].retry ? "delay" :
210 /* don't retry auto boot */
211 if (!delaykey[i].retry)
212 bootretry_dont_retry();
216 } while (!abort && get_ticks() <= etime);
221 /***************************************************************************
222 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
223 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
225 static int abortboot_key_sequence(int bootdelay)
228 uint64_t etime = endtick(bootdelay);
230 # ifdef CONFIG_AUTOBOOT_PROMPT
232 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
233 * To print the bootdelay value upon bootup.
235 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
238 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
239 abort = passwd_abort_sha256(etime);
241 abort = passwd_abort_key(etime);
243 debug_bootkeys("key timeout\n");
248 static int abortboot_single_key(int bootdelay)
253 printf("Hit any key to stop autoboot: %2d ", bootdelay);
256 * Check if key already pressed
258 if (tstc()) { /* we got a key press */
259 (void) getc(); /* consume input */
261 abort = 1; /* don't auto boot */
264 while ((bootdelay > 0) && (!abort)) {
269 if (tstc()) { /* we got a key press */
272 abort = 1; /* don't auto boot */
273 bootdelay = 0; /* no more delay */
274 key = getc(); /* consume input */
275 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
280 } while (!abort && get_timer(ts) < 1000);
282 printf("\b\b\b%2d ", bootdelay);
290 static int abortboot(int bootdelay)
294 if (bootdelay >= 0) {
295 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
296 abort = abortboot_key_sequence(bootdelay);
298 abort = abortboot_single_key(bootdelay);
301 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
302 gd->flags &= ~GD_FLG_SILENT;
307 static void process_fdt_options(const void *blob)
309 #ifdef CONFIG_SYS_TEXT_BASE
312 /* Add an env variable to point to a kernel payload, if available */
313 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
315 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
317 /* Add an env variable to point to a root disk, if available */
318 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
320 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
321 #endif /* CONFIG_SYS_TEXT_BASE */
324 const char *bootdelay_process(void)
331 s = env_get("bootdelay");
332 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
334 if (IS_ENABLED(CONFIG_OF_CONTROL))
335 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
338 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
340 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
341 bootdelay = menu_show(bootdelay);
342 bootretry_init_cmd_timeout();
345 if (gd->flags & GD_FLG_POSTFAIL) {
346 s = env_get("failbootcmd");
348 #endif /* CONFIG_POST */
349 if (bootcount_error())
350 s = env_get("altbootcmd");
352 s = env_get("bootcmd");
354 if (IS_ENABLED(CONFIG_OF_CONTROL))
355 process_fdt_options(gd->fdt_blob);
356 stored_bootdelay = bootdelay;
361 void autoboot_command(const char *s)
363 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
365 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
369 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
370 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
372 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
374 run_command_list(s, -1, 0);
377 disable_ctrlc(prev); /* restore Ctrl-C checking */
380 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
381 menukey == AUTOBOOT_MENUKEY) {
382 s = env_get("menucmd");
384 run_command_list(s, -1, 0);