1 // SPDX-License-Identifier: GPL-2.0+
22 #include <asm/global_data.h>
23 #include <linux/delay.h>
24 #include <u-boot/sha256.h>
25 #include <bootcount.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 #define MAX_DELAY_STOP_STR 64
31 #ifndef DEBUG_BOOTKEYS
32 #define DEBUG_BOOTKEYS 0
34 #define debug_bootkeys(fmt, args...) \
35 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
37 /* Stored value of bootdelay, used by autoboot_command() */
38 static int stored_bootdelay;
41 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
42 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
44 #define AUTOBOOT_STOP_STR_SHA256 ""
47 #ifdef CONFIG_USE_AUTOBOOT_MENUKEY
48 #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
50 #define AUTOBOOT_MENUKEY 0
54 * Use a "constant-length" time compare function for this
57 * https://crackstation.net/hashing-security.htm
59 static int slow_equals(u8 *a, u8 *b, int len)
64 for (i = 0; i < len; i++)
71 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
73 * This checks for the user entering a SHA256 hash within a given time.
75 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
76 * @return 0 if autoboot should continue, 1 if it should stop
78 static int passwd_abort_sha256(uint64_t etime)
80 const char *sha_env_str = env_get("bootstopkeysha256");
81 u8 sha_env[SHA256_SUM_LEN];
85 const char *algo_name = "sha256";
86 u_int presskey_len = 0;
88 int size = sizeof(sha);
91 if (sha_env_str == NULL)
92 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
94 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
95 c = strstr(sha_env_str, ":");
96 if (c && (c - sha_env_str < MAX_DELAY_STOP_STR)) {
97 /* preload presskey with salt */
98 memcpy(presskey, sha_env_str, c - sha_env_str);
99 presskey_len = c - sha_env_str;
103 * Generate the binary value from the environment hash value
104 * so that we can compare this value with the computed hash
105 * from the user input
107 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
109 printf("Hash %s not supported!\n", algo_name);
113 sha = malloc_cache_aligned(SHA256_SUM_LEN);
114 size = SHA256_SUM_LEN;
116 * We don't know how long the stop-string is, so we need to
117 * generate the sha256 hash upon each input character and
118 * compare the value with the one saved in the environment
122 /* Check for input string overflow */
123 if (presskey_len >= MAX_DELAY_STOP_STR) {
129 presskey[presskey_len++] = getchar();
131 /* Calculate sha256 upon each new char */
132 hash_block(algo_name, (const void *)presskey,
133 presskey_len, sha, &size);
135 /* And check if sha matches saved value in env */
136 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
139 } while (!abort && get_ticks() <= etime);
147 * passwd_abort_key() - check for a key sequence to aborted booting
149 * This checks for the user entering a string within a given time.
151 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
152 * @return 0 if autoboot should continue, 1 if it should stop
154 static int passwd_abort_key(uint64_t etime)
163 { .str = env_get("bootdelaykey"), .retry = 1 },
164 { .str = env_get("bootstopkey"), .retry = 0 },
167 char presskey[MAX_DELAY_STOP_STR];
168 int presskey_len = 0;
169 int presskey_max = 0;
172 # ifdef CONFIG_AUTOBOOT_DELAY_STR
173 if (delaykey[0].str == NULL)
174 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
176 # ifdef CONFIG_AUTOBOOT_STOP_STR
177 if (delaykey[1].str == NULL)
178 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
181 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
182 delaykey[i].len = delaykey[i].str == NULL ?
183 0 : strlen(delaykey[i].str);
184 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
185 MAX_DELAY_STOP_STR : delaykey[i].len;
187 presskey_max = presskey_max > delaykey[i].len ?
188 presskey_max : delaykey[i].len;
190 debug_bootkeys("%s key:<%s>\n",
191 delaykey[i].retry ? "delay" : "stop",
192 delaykey[i].str ? delaykey[i].str : "NULL");
195 /* In order to keep up with incoming data, check timeout only
200 if (presskey_len < presskey_max) {
201 presskey[presskey_len++] = getchar();
203 for (i = 0; i < presskey_max - 1; i++)
204 presskey[i] = presskey[i + 1];
206 presskey[i] = getchar();
210 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
211 if (delaykey[i].len > 0 &&
212 presskey_len >= delaykey[i].len &&
213 memcmp(presskey + presskey_len -
214 delaykey[i].len, delaykey[i].str,
215 delaykey[i].len) == 0) {
216 debug_bootkeys("got %skey\n",
217 delaykey[i].retry ? "delay" :
220 /* don't retry auto boot */
221 if (!delaykey[i].retry)
222 bootretry_dont_retry();
226 } while (!abort && get_ticks() <= etime);
231 /***************************************************************************
232 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
233 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
235 static int abortboot_key_sequence(int bootdelay)
238 uint64_t etime = endtick(bootdelay);
240 # ifdef CONFIG_AUTOBOOT_PROMPT
242 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
243 * To print the bootdelay value upon bootup.
245 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
248 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
249 abort = passwd_abort_sha256(etime);
251 abort = passwd_abort_key(etime);
253 debug_bootkeys("key timeout\n");
258 static int abortboot_single_key(int bootdelay)
263 printf("Hit any key to stop autoboot: %2d ", bootdelay);
266 * Check if key already pressed
268 if (tstc()) { /* we got a key press */
269 getchar(); /* consume input */
271 abort = 1; /* don't auto boot */
274 while ((bootdelay > 0) && (!abort)) {
279 if (tstc()) { /* we got a key press */
282 abort = 1; /* don't auto boot */
283 bootdelay = 0; /* no more delay */
284 key = getchar();/* consume input */
285 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
290 } while (!abort && get_timer(ts) < 1000);
292 printf("\b\b\b%2d ", bootdelay);
300 static int abortboot(int bootdelay)
304 if (bootdelay >= 0) {
305 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
306 abort = abortboot_key_sequence(bootdelay);
308 abort = abortboot_single_key(bootdelay);
311 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
312 gd->flags &= ~GD_FLG_SILENT;
317 static void process_fdt_options(const void *blob)
319 #ifdef CONFIG_SYS_TEXT_BASE
322 /* Add an env variable to point to a kernel payload, if available */
323 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
325 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
327 /* Add an env variable to point to a root disk, if available */
328 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
330 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
331 #endif /* CONFIG_SYS_TEXT_BASE */
334 const char *bootdelay_process(void)
341 s = env_get("bootdelay");
342 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
344 if (IS_ENABLED(CONFIG_OF_CONTROL))
345 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
348 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
350 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
351 bootdelay = menu_show(bootdelay);
352 bootretry_init_cmd_timeout();
355 if (gd->flags & GD_FLG_POSTFAIL) {
356 s = env_get("failbootcmd");
358 #endif /* CONFIG_POST */
359 if (bootcount_error())
360 s = env_get("altbootcmd");
362 s = env_get("bootcmd");
364 if (IS_ENABLED(CONFIG_OF_CONTROL))
365 process_fdt_options(gd->fdt_blob);
366 stored_bootdelay = bootdelay;
371 void autoboot_command(const char *s)
373 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
375 if (s && (stored_bootdelay == -2 ||
376 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
380 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
381 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
383 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
385 run_command_list(s, -1, 0);
388 disable_ctrlc(prev); /* restore Ctrl-C checking */
391 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
392 menukey == AUTOBOOT_MENUKEY) {
393 s = env_get("menucmd");
395 run_command_list(s, -1, 0);