1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <u-boot/sha256.h>
16 #include <bootcount.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #define MAX_DELAY_STOP_STR 32
22 #ifndef DEBUG_BOOTKEYS
23 #define DEBUG_BOOTKEYS 0
25 #define debug_bootkeys(fmt, args...) \
26 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
28 /* Stored value of bootdelay, used by autoboot_command() */
29 static int stored_bootdelay;
31 #ifdef CONFIG_AUTOBOOT_ENCRYPTION
32 #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
34 #define AUTOBOOT_STOP_STR_SHA256 ""
37 #if defined(CONFIG_AUTOBOOT_KEYED)
38 #if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
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(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(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);
187 /***************************************************************************
188 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
189 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
191 static int __abortboot(int bootdelay)
194 uint64_t etime = endtick(bootdelay);
196 # ifdef CONFIG_AUTOBOOT_PROMPT
198 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
199 * To print the bootdelay value upon bootup.
201 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
204 abort = passwd_abort(etime);
206 debug_bootkeys("key timeout\n");
211 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
213 #ifdef CONFIG_MENUKEY
217 static int __abortboot(int bootdelay)
222 #ifdef CONFIG_MENUPROMPT
223 printf(CONFIG_MENUPROMPT);
225 printf("Hit any key to stop autoboot: %2d ", bootdelay);
229 * Check if key already pressed
231 if (tstc()) { /* we got a key press */
232 (void) getc(); /* consume input */
234 abort = 1; /* don't auto boot */
237 while ((bootdelay > 0) && (!abort)) {
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
245 # ifdef CONFIG_MENUKEY
248 (void) getc(); /* consume input */
253 } while (!abort && get_timer(ts) < 1000);
255 printf("\b\b\b%2d ", bootdelay);
262 # endif /* CONFIG_AUTOBOOT_KEYED */
264 static int abortboot(int bootdelay)
269 abort = __abortboot(bootdelay);
271 #ifdef CONFIG_SILENT_CONSOLE
273 gd->flags &= ~GD_FLG_SILENT;
279 static void process_fdt_options(const void *blob)
281 #if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
284 /* Add an env variable to point to a kernel payload, if available */
285 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
287 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
289 /* Add an env variable to point to a root disk, if available */
290 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
292 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
293 #endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
296 const char *bootdelay_process(void)
303 s = env_get("bootdelay");
304 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
306 #ifdef CONFIG_OF_CONTROL
307 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
311 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
313 #if defined(CONFIG_MENU_SHOW)
314 bootdelay = menu_show(bootdelay);
316 bootretry_init_cmd_timeout();
319 if (gd->flags & GD_FLG_POSTFAIL) {
320 s = env_get("failbootcmd");
322 #endif /* CONFIG_POST */
323 if (bootcount_error())
324 s = env_get("altbootcmd");
326 s = env_get("bootcmd");
328 process_fdt_options(gd->fdt_blob);
329 stored_bootdelay = bootdelay;
334 void autoboot_command(const char *s)
336 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
338 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
339 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
340 int prev = disable_ctrlc(1); /* disable Control C checking */
343 run_command_list(s, -1, 0);
345 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
346 disable_ctrlc(prev); /* restore Control C checking */
350 #ifdef CONFIG_MENUKEY
351 if (menukey == CONFIG_MENUKEY) {
352 s = env_get("menucmd");
354 run_command_list(s, -1, 0);
356 #endif /* CONFIG_MENUKEY */