1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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 ""
39 * Use a "constant-length" time compare function for this
42 * https://crackstation.net/hashing-security.htm
44 static int slow_equals(u8 *a, u8 *b, int len)
49 for (i = 0; i < len; i++)
56 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
58 * This checks for the user entering a SHA256 hash within a given time.
60 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
61 * @return 0 if autoboot should continue, 1 if it should stop
63 static int passwd_abort_sha256(uint64_t etime)
65 const char *sha_env_str = env_get("bootstopkeysha256");
66 u8 sha_env[SHA256_SUM_LEN];
67 u8 sha[SHA256_SUM_LEN];
68 char presskey[MAX_DELAY_STOP_STR];
69 const char *algo_name = "sha256";
70 u_int presskey_len = 0;
72 int size = sizeof(sha);
75 if (sha_env_str == NULL)
76 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
79 * Generate the binary value from the environment hash value
80 * so that we can compare this value with the computed hash
83 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
85 printf("Hash %s not supported!\n", algo_name);
90 * We don't know how long the stop-string is, so we need to
91 * generate the sha256 hash upon each input character and
92 * compare the value with the one saved in the environment
96 /* Check for input string overflow */
97 if (presskey_len >= MAX_DELAY_STOP_STR)
100 presskey[presskey_len++] = getc();
102 /* Calculate sha256 upon each new char */
103 hash_block(algo_name, (const void *)presskey,
104 presskey_len, sha, &size);
106 /* And check if sha matches saved value in env */
107 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
110 } while (!abort && get_ticks() <= etime);
116 * passwd_abort_key() - check for a key sequence to aborted booting
118 * This checks for the user entering a string within a given time.
120 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
121 * @return 0 if autoboot should continue, 1 if it should stop
123 static int passwd_abort_key(uint64_t etime)
132 { .str = env_get("bootdelaykey"), .retry = 1 },
133 { .str = env_get("bootstopkey"), .retry = 0 },
136 char presskey[MAX_DELAY_STOP_STR];
137 u_int presskey_len = 0;
138 u_int presskey_max = 0;
141 # ifdef CONFIG_AUTOBOOT_DELAY_STR
142 if (delaykey[0].str == NULL)
143 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
145 # ifdef CONFIG_AUTOBOOT_STOP_STR
146 if (delaykey[1].str == NULL)
147 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
150 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
151 delaykey[i].len = delaykey[i].str == NULL ?
152 0 : strlen(delaykey[i].str);
153 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
154 MAX_DELAY_STOP_STR : delaykey[i].len;
156 presskey_max = presskey_max > delaykey[i].len ?
157 presskey_max : delaykey[i].len;
159 debug_bootkeys("%s key:<%s>\n",
160 delaykey[i].retry ? "delay" : "stop",
161 delaykey[i].str ? delaykey[i].str : "NULL");
164 /* In order to keep up with incoming data, check timeout only
169 if (presskey_len < presskey_max) {
170 presskey[presskey_len++] = getc();
172 for (i = 0; i < presskey_max - 1; i++)
173 presskey[i] = presskey[i + 1];
175 presskey[i] = getc();
179 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
180 if (delaykey[i].len > 0 &&
181 presskey_len >= delaykey[i].len &&
182 memcmp(presskey + presskey_len -
183 delaykey[i].len, delaykey[i].str,
184 delaykey[i].len) == 0) {
185 debug_bootkeys("got %skey\n",
186 delaykey[i].retry ? "delay" :
189 /* don't retry auto boot */
190 if (!delaykey[i].retry)
191 bootretry_dont_retry();
195 } while (!abort && get_ticks() <= etime);
200 /***************************************************************************
201 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
202 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
204 static int abortboot_key_sequence(int bootdelay)
207 uint64_t etime = endtick(bootdelay);
209 # ifdef CONFIG_AUTOBOOT_PROMPT
211 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
212 * To print the bootdelay value upon bootup.
214 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
217 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
218 abort = passwd_abort_sha256(etime);
220 abort = passwd_abort_key(etime);
222 debug_bootkeys("key timeout\n");
227 #ifdef CONFIG_MENUKEY
231 static int abortboot_single_key(int bootdelay)
236 #ifdef CONFIG_MENUPROMPT
237 printf(CONFIG_MENUPROMPT);
239 printf("Hit any key to stop autoboot: %2d ", bootdelay);
243 * Check if key already pressed
245 if (tstc()) { /* we got a key press */
246 (void) getc(); /* consume input */
248 abort = 1; /* don't auto boot */
251 while ((bootdelay > 0) && (!abort)) {
256 if (tstc()) { /* we got a key press */
257 abort = 1; /* don't auto boot */
258 bootdelay = 0; /* no more delay */
259 # ifdef CONFIG_MENUKEY
262 (void) getc(); /* consume input */
267 } while (!abort && get_timer(ts) < 1000);
269 printf("\b\b\b%2d ", bootdelay);
277 static int abortboot(int bootdelay)
281 if (bootdelay >= 0) {
282 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
283 abort = abortboot_key_sequence(bootdelay);
285 abort = abortboot_single_key(bootdelay);
288 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
289 gd->flags &= ~GD_FLG_SILENT;
294 static void process_fdt_options(const void *blob)
296 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
299 /* Add an env variable to point to a kernel payload, if available */
300 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
302 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
304 /* Add an env variable to point to a root disk, if available */
305 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
307 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
308 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
311 const char *bootdelay_process(void)
318 s = env_get("bootdelay");
319 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
321 #ifdef CONFIG_OF_CONTROL
322 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
326 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
328 #if defined(CONFIG_MENU_SHOW)
329 bootdelay = menu_show(bootdelay);
331 bootretry_init_cmd_timeout();
334 if (gd->flags & GD_FLG_POSTFAIL) {
335 s = env_get("failbootcmd");
337 #endif /* CONFIG_POST */
338 if (bootcount_error())
339 s = env_get("altbootcmd");
341 s = env_get("bootcmd");
343 process_fdt_options(gd->fdt_blob);
344 stored_bootdelay = bootdelay;
349 void autoboot_command(const char *s)
351 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
353 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
354 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
355 int prev = disable_ctrlc(1); /* disable Control C checking */
358 run_command_list(s, -1, 0);
360 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
361 disable_ctrlc(prev); /* restore Control C checking */
365 #ifdef CONFIG_MENUKEY
366 if (menukey == CONFIG_MENUKEY) {
367 s = env_get("menucmd");
369 run_command_list(s, -1, 0);
371 #endif /* CONFIG_MENUKEY */