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 32
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];
83 const char *algo_name = "sha256";
84 u_int presskey_len = 0;
86 int size = sizeof(sha);
89 if (sha_env_str == NULL)
90 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
93 * Generate the binary value from the environment hash value
94 * so that we can compare this value with the computed hash
97 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
99 printf("Hash %s not supported!\n", algo_name);
103 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
104 sha = malloc_cache_aligned(SHA256_SUM_LEN);
105 size = SHA256_SUM_LEN;
107 * We don't know how long the stop-string is, so we need to
108 * generate the sha256 hash upon each input character and
109 * compare the value with the one saved in the environment
113 /* Check for input string overflow */
114 if (presskey_len >= MAX_DELAY_STOP_STR) {
120 presskey[presskey_len++] = getc();
122 /* Calculate sha256 upon each new char */
123 hash_block(algo_name, (const void *)presskey,
124 presskey_len, sha, &size);
126 /* And check if sha matches saved value in env */
127 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
130 } while (!abort && get_ticks() <= etime);
138 * passwd_abort_key() - check for a key sequence to aborted booting
140 * This checks for the user entering a string within a given time.
142 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
143 * @return 0 if autoboot should continue, 1 if it should stop
145 static int passwd_abort_key(uint64_t etime)
154 { .str = env_get("bootdelaykey"), .retry = 1 },
155 { .str = env_get("bootstopkey"), .retry = 0 },
158 char presskey[MAX_DELAY_STOP_STR];
159 u_int presskey_len = 0;
160 u_int presskey_max = 0;
163 # ifdef CONFIG_AUTOBOOT_DELAY_STR
164 if (delaykey[0].str == NULL)
165 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
167 # ifdef CONFIG_AUTOBOOT_STOP_STR
168 if (delaykey[1].str == NULL)
169 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
172 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
173 delaykey[i].len = delaykey[i].str == NULL ?
174 0 : strlen(delaykey[i].str);
175 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
176 MAX_DELAY_STOP_STR : delaykey[i].len;
178 presskey_max = presskey_max > delaykey[i].len ?
179 presskey_max : delaykey[i].len;
181 debug_bootkeys("%s key:<%s>\n",
182 delaykey[i].retry ? "delay" : "stop",
183 delaykey[i].str ? delaykey[i].str : "NULL");
186 /* In order to keep up with incoming data, check timeout only
191 if (presskey_len < presskey_max) {
192 presskey[presskey_len++] = getc();
194 for (i = 0; i < presskey_max - 1; i++)
195 presskey[i] = presskey[i + 1];
197 presskey[i] = getc();
201 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
202 if (delaykey[i].len > 0 &&
203 presskey_len >= delaykey[i].len &&
204 memcmp(presskey + presskey_len -
205 delaykey[i].len, delaykey[i].str,
206 delaykey[i].len) == 0) {
207 debug_bootkeys("got %skey\n",
208 delaykey[i].retry ? "delay" :
211 /* don't retry auto boot */
212 if (!delaykey[i].retry)
213 bootretry_dont_retry();
217 } while (!abort && get_ticks() <= etime);
222 /***************************************************************************
223 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
224 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
226 static int abortboot_key_sequence(int bootdelay)
229 uint64_t etime = endtick(bootdelay);
231 # ifdef CONFIG_AUTOBOOT_PROMPT
233 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
234 * To print the bootdelay value upon bootup.
236 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
239 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
240 abort = passwd_abort_sha256(etime);
242 abort = passwd_abort_key(etime);
244 debug_bootkeys("key timeout\n");
249 static int abortboot_single_key(int bootdelay)
254 printf("Hit any key to stop autoboot: %2d ", bootdelay);
257 * Check if key already pressed
259 if (tstc()) { /* we got a key press */
260 (void) getc(); /* consume input */
262 abort = 1; /* don't auto boot */
265 while ((bootdelay > 0) && (!abort)) {
270 if (tstc()) { /* we got a key press */
273 abort = 1; /* don't auto boot */
274 bootdelay = 0; /* no more delay */
275 key = getc(); /* consume input */
276 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
281 } while (!abort && get_timer(ts) < 1000);
283 printf("\b\b\b%2d ", bootdelay);
291 static int abortboot(int bootdelay)
295 if (bootdelay >= 0) {
296 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
297 abort = abortboot_key_sequence(bootdelay);
299 abort = abortboot_single_key(bootdelay);
302 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
303 gd->flags &= ~GD_FLG_SILENT;
308 static void process_fdt_options(const void *blob)
310 #ifdef CONFIG_SYS_TEXT_BASE
313 /* Add an env variable to point to a kernel payload, if available */
314 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
316 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
318 /* Add an env variable to point to a root disk, if available */
319 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
321 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
322 #endif /* CONFIG_SYS_TEXT_BASE */
325 const char *bootdelay_process(void)
332 s = env_get("bootdelay");
333 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
335 if (IS_ENABLED(CONFIG_OF_CONTROL))
336 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
339 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
341 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
342 bootdelay = menu_show(bootdelay);
343 bootretry_init_cmd_timeout();
346 if (gd->flags & GD_FLG_POSTFAIL) {
347 s = env_get("failbootcmd");
349 #endif /* CONFIG_POST */
350 if (bootcount_error())
351 s = env_get("altbootcmd");
353 s = env_get("bootcmd");
355 if (IS_ENABLED(CONFIG_OF_CONTROL))
356 process_fdt_options(gd->fdt_blob);
357 stored_bootdelay = bootdelay;
362 void autoboot_command(const char *s)
364 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
366 if (s && (stored_bootdelay == -2 ||
367 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
371 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
372 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
374 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
376 run_command_list(s, -1, 0);
379 disable_ctrlc(prev); /* restore Ctrl-C checking */
382 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
383 menukey == AUTOBOOT_MENUKEY) {
384 s = env_get("menucmd");
386 run_command_list(s, -1, 0);