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 ""
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++)
57 static int passwd_abort_sha256(uint64_t etime)
59 const char *sha_env_str = env_get("bootstopkeysha256");
60 u8 sha_env[SHA256_SUM_LEN];
61 u8 sha[SHA256_SUM_LEN];
62 char presskey[MAX_DELAY_STOP_STR];
63 const char *algo_name = "sha256";
64 u_int presskey_len = 0;
66 int size = sizeof(sha);
69 if (sha_env_str == NULL)
70 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
73 * Generate the binary value from the environment hash value
74 * so that we can compare this value with the computed hash
77 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
79 printf("Hash %s not supported!\n", algo_name);
84 * We don't know how long the stop-string is, so we need to
85 * generate the sha256 hash upon each input character and
86 * compare the value with the one saved in the environment
90 /* Check for input string overflow */
91 if (presskey_len >= MAX_DELAY_STOP_STR)
94 presskey[presskey_len++] = getc();
96 /* Calculate sha256 upon each new char */
97 hash_block(algo_name, (const void *)presskey,
98 presskey_len, sha, &size);
100 /* And check if sha matches saved value in env */
101 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
104 } while (!abort && get_ticks() <= etime);
109 static int passwd_abort_key(uint64_t etime)
118 { .str = env_get("bootdelaykey"), .retry = 1 },
119 { .str = env_get("bootstopkey"), .retry = 0 },
122 char presskey[MAX_DELAY_STOP_STR];
123 u_int presskey_len = 0;
124 u_int presskey_max = 0;
127 # ifdef CONFIG_AUTOBOOT_DELAY_STR
128 if (delaykey[0].str == NULL)
129 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
131 # ifdef CONFIG_AUTOBOOT_STOP_STR
132 if (delaykey[1].str == NULL)
133 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
136 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
137 delaykey[i].len = delaykey[i].str == NULL ?
138 0 : strlen(delaykey[i].str);
139 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
140 MAX_DELAY_STOP_STR : delaykey[i].len;
142 presskey_max = presskey_max > delaykey[i].len ?
143 presskey_max : delaykey[i].len;
145 debug_bootkeys("%s key:<%s>\n",
146 delaykey[i].retry ? "delay" : "stop",
147 delaykey[i].str ? delaykey[i].str : "NULL");
150 /* In order to keep up with incoming data, check timeout only
155 if (presskey_len < presskey_max) {
156 presskey[presskey_len++] = getc();
158 for (i = 0; i < presskey_max - 1; i++)
159 presskey[i] = presskey[i + 1];
161 presskey[i] = getc();
165 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
166 if (delaykey[i].len > 0 &&
167 presskey_len >= delaykey[i].len &&
168 memcmp(presskey + presskey_len -
169 delaykey[i].len, delaykey[i].str,
170 delaykey[i].len) == 0) {
171 debug_bootkeys("got %skey\n",
172 delaykey[i].retry ? "delay" :
175 /* don't retry auto boot */
176 if (!delaykey[i].retry)
177 bootretry_dont_retry();
181 } while (!abort && get_ticks() <= etime);
186 /***************************************************************************
187 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
188 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
190 static int __abortboot(int bootdelay)
193 uint64_t etime = endtick(bootdelay);
195 # ifdef CONFIG_AUTOBOOT_PROMPT
197 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
198 * To print the bootdelay value upon bootup.
200 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
203 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
204 abort = passwd_abort_sha256(etime);
206 abort = passwd_abort_key(etime);
208 debug_bootkeys("key timeout\n");
213 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
215 #ifdef CONFIG_MENUKEY
219 static int __abortboot(int bootdelay)
224 #ifdef CONFIG_MENUPROMPT
225 printf(CONFIG_MENUPROMPT);
227 printf("Hit any key to stop autoboot: %2d ", bootdelay);
231 * Check if key already pressed
233 if (tstc()) { /* we got a key press */
234 (void) getc(); /* consume input */
236 abort = 1; /* don't auto boot */
239 while ((bootdelay > 0) && (!abort)) {
244 if (tstc()) { /* we got a key press */
245 abort = 1; /* don't auto boot */
246 bootdelay = 0; /* no more delay */
247 # ifdef CONFIG_MENUKEY
250 (void) getc(); /* consume input */
255 } while (!abort && get_timer(ts) < 1000);
257 printf("\b\b\b%2d ", bootdelay);
264 # endif /* CONFIG_AUTOBOOT_KEYED */
266 static int abortboot(int bootdelay)
271 abort = __abortboot(bootdelay);
273 #ifdef CONFIG_SILENT_CONSOLE
275 gd->flags &= ~GD_FLG_SILENT;
281 static void process_fdt_options(const void *blob)
283 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
286 /* Add an env variable to point to a kernel payload, if available */
287 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
289 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
291 /* Add an env variable to point to a root disk, if available */
292 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
294 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
295 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
298 const char *bootdelay_process(void)
305 s = env_get("bootdelay");
306 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
308 #ifdef CONFIG_OF_CONTROL
309 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
313 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
315 #if defined(CONFIG_MENU_SHOW)
316 bootdelay = menu_show(bootdelay);
318 bootretry_init_cmd_timeout();
321 if (gd->flags & GD_FLG_POSTFAIL) {
322 s = env_get("failbootcmd");
324 #endif /* CONFIG_POST */
325 if (bootcount_error())
326 s = env_get("altbootcmd");
328 s = env_get("bootcmd");
330 process_fdt_options(gd->fdt_blob);
331 stored_bootdelay = bootdelay;
336 void autoboot_command(const char *s)
338 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
340 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
341 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
342 int prev = disable_ctrlc(1); /* disable Control C checking */
345 run_command_list(s, -1, 0);
347 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
348 disable_ctrlc(prev); /* restore Control C checking */
352 #ifdef CONFIG_MENUKEY
353 if (menukey == CONFIG_MENUKEY) {
354 s = env_get("menucmd");
356 run_command_list(s, -1, 0);
358 #endif /* CONFIG_MENUKEY */