1 // SPDX-License-Identifier: GPL-2.0+
16 #include <u-boot/sha256.h>
17 #include <bootcount.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #define MAX_DELAY_STOP_STR 32
23 #ifndef DEBUG_BOOTKEYS
24 #define DEBUG_BOOTKEYS 0
26 #define debug_bootkeys(fmt, args...) \
27 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
29 /* Stored value of bootdelay, used by autoboot_command() */
30 static int stored_bootdelay;
32 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
33 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
35 #define AUTOBOOT_STOP_STR_SHA256 ""
38 #if defined(CONFIG_AUTOBOOT_KEYED)
41 * Use a "constant-length" time compare function for this
44 * https://crackstation.net/hashing-security.htm
46 static int slow_equals(u8 *a, u8 *b, int len)
51 for (i = 0; i < len; i++)
58 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
60 * This checks for the user entering a SHA256 hash within a given time.
62 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
63 * @return 0 if autoboot should continue, 1 if it should stop
65 static int passwd_abort_sha256(uint64_t etime)
67 const char *sha_env_str = env_get("bootstopkeysha256");
68 u8 sha_env[SHA256_SUM_LEN];
69 u8 sha[SHA256_SUM_LEN];
70 char presskey[MAX_DELAY_STOP_STR];
71 const char *algo_name = "sha256";
72 u_int presskey_len = 0;
74 int size = sizeof(sha);
77 if (sha_env_str == NULL)
78 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
81 * Generate the binary value from the environment hash value
82 * so that we can compare this value with the computed hash
85 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
87 printf("Hash %s not supported!\n", algo_name);
92 * We don't know how long the stop-string is, so we need to
93 * generate the sha256 hash upon each input character and
94 * compare the value with the one saved in the environment
98 /* Check for input string overflow */
99 if (presskey_len >= MAX_DELAY_STOP_STR)
102 presskey[presskey_len++] = getc();
104 /* Calculate sha256 upon each new char */
105 hash_block(algo_name, (const void *)presskey,
106 presskey_len, sha, &size);
108 /* And check if sha matches saved value in env */
109 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
112 } while (!abort && get_ticks() <= etime);
118 * passwd_abort_key() - check for a key sequence to aborted booting
120 * This checks for the user entering a string within a given time.
122 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
123 * @return 0 if autoboot should continue, 1 if it should stop
125 static int passwd_abort_key(uint64_t etime)
134 { .str = env_get("bootdelaykey"), .retry = 1 },
135 { .str = env_get("bootstopkey"), .retry = 0 },
138 char presskey[MAX_DELAY_STOP_STR];
139 u_int presskey_len = 0;
140 u_int presskey_max = 0;
143 # ifdef CONFIG_AUTOBOOT_DELAY_STR
144 if (delaykey[0].str == NULL)
145 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
147 # ifdef CONFIG_AUTOBOOT_STOP_STR
148 if (delaykey[1].str == NULL)
149 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
152 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
153 delaykey[i].len = delaykey[i].str == NULL ?
154 0 : strlen(delaykey[i].str);
155 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
156 MAX_DELAY_STOP_STR : delaykey[i].len;
158 presskey_max = presskey_max > delaykey[i].len ?
159 presskey_max : delaykey[i].len;
161 debug_bootkeys("%s key:<%s>\n",
162 delaykey[i].retry ? "delay" : "stop",
163 delaykey[i].str ? delaykey[i].str : "NULL");
166 /* In order to keep up with incoming data, check timeout only
171 if (presskey_len < presskey_max) {
172 presskey[presskey_len++] = getc();
174 for (i = 0; i < presskey_max - 1; i++)
175 presskey[i] = presskey[i + 1];
177 presskey[i] = getc();
181 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
182 if (delaykey[i].len > 0 &&
183 presskey_len >= delaykey[i].len &&
184 memcmp(presskey + presskey_len -
185 delaykey[i].len, delaykey[i].str,
186 delaykey[i].len) == 0) {
187 debug_bootkeys("got %skey\n",
188 delaykey[i].retry ? "delay" :
191 /* don't retry auto boot */
192 if (!delaykey[i].retry)
193 bootretry_dont_retry();
197 } while (!abort && get_ticks() <= etime);
202 /***************************************************************************
203 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
204 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
206 static int __abortboot(int bootdelay)
209 uint64_t etime = endtick(bootdelay);
211 # ifdef CONFIG_AUTOBOOT_PROMPT
213 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
214 * To print the bootdelay value upon bootup.
216 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
219 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
220 abort = passwd_abort_sha256(etime);
222 abort = passwd_abort_key(etime);
224 debug_bootkeys("key timeout\n");
229 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
231 #ifdef CONFIG_MENUKEY
235 static int __abortboot(int bootdelay)
240 #ifdef CONFIG_MENUPROMPT
241 printf(CONFIG_MENUPROMPT);
243 printf("Hit any key to stop autoboot: %2d ", bootdelay);
247 * Check if key already pressed
249 if (tstc()) { /* we got a key press */
250 (void) getc(); /* consume input */
252 abort = 1; /* don't auto boot */
255 while ((bootdelay > 0) && (!abort)) {
260 if (tstc()) { /* we got a key press */
261 abort = 1; /* don't auto boot */
262 bootdelay = 0; /* no more delay */
263 # ifdef CONFIG_MENUKEY
266 (void) getc(); /* consume input */
271 } while (!abort && get_timer(ts) < 1000);
273 printf("\b\b\b%2d ", bootdelay);
280 # endif /* CONFIG_AUTOBOOT_KEYED */
282 static int abortboot(int bootdelay)
287 abort = __abortboot(bootdelay);
289 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
290 gd->flags &= ~GD_FLG_SILENT;
295 static void process_fdt_options(const void *blob)
297 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
300 /* Add an env variable to point to a kernel payload, if available */
301 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
303 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
305 /* Add an env variable to point to a root disk, if available */
306 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
308 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
309 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
312 const char *bootdelay_process(void)
319 s = env_get("bootdelay");
320 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
322 #ifdef CONFIG_OF_CONTROL
323 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
327 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
329 #if defined(CONFIG_MENU_SHOW)
330 bootdelay = menu_show(bootdelay);
332 bootretry_init_cmd_timeout();
335 if (gd->flags & GD_FLG_POSTFAIL) {
336 s = env_get("failbootcmd");
338 #endif /* CONFIG_POST */
339 if (bootcount_error())
340 s = env_get("altbootcmd");
342 s = env_get("bootcmd");
344 process_fdt_options(gd->fdt_blob);
345 stored_bootdelay = bootdelay;
350 void autoboot_command(const char *s)
352 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
354 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
355 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
356 int prev = disable_ctrlc(1); /* disable Control C checking */
359 run_command_list(s, -1, 0);
361 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
362 disable_ctrlc(prev); /* restore Control C checking */
366 #ifdef CONFIG_MENUKEY
367 if (menukey == CONFIG_MENUKEY) {
368 s = env_get("menucmd");
370 run_command_list(s, -1, 0);
372 #endif /* CONFIG_MENUKEY */