3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <bootretry.h>
16 #include <u-boot/sha256.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 #if defined(CONFIG_AUTOBOOT_KEYED)
32 #if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
35 * Use a "constant-length" time compare function for this
38 * https://crackstation.net/hashing-security.htm
40 static int slow_equals(u8 *a, u8 *b, int len)
45 for (i = 0; i < len; i++)
51 static int passwd_abort(uint64_t etime)
53 const char *sha_env_str = getenv("bootstopkeysha256");
54 u8 sha_env[SHA256_SUM_LEN];
55 u8 sha[SHA256_SUM_LEN];
56 char presskey[MAX_DELAY_STOP_STR];
57 const char *algo_name = "sha256";
58 u_int presskey_len = 0;
63 if (sha_env_str == NULL)
64 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
67 * Generate the binary value from the environment hash value
68 * so that we can compare this value with the computed hash
71 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
73 printf("Hash %s not supported!\n", algo_name);
78 * We don't know how long the stop-string is, so we need to
79 * generate the sha256 hash upon each input character and
80 * compare the value with the one saved in the environment
84 /* Check for input string overflow */
85 if (presskey_len >= MAX_DELAY_STOP_STR)
88 presskey[presskey_len++] = getc();
90 /* Calculate sha256 upon each new char */
91 hash_block(algo_name, (const void *)presskey,
92 presskey_len, sha, &size);
94 /* And check if sha matches saved value in env */
95 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
98 } while (!abort && get_ticks() <= etime);
103 static int passwd_abort(uint64_t etime)
112 { .str = getenv("bootdelaykey"), .retry = 1 },
113 { .str = getenv("bootstopkey"), .retry = 0 },
116 char presskey[MAX_DELAY_STOP_STR];
117 u_int presskey_len = 0;
118 u_int presskey_max = 0;
121 # ifdef CONFIG_AUTOBOOT_DELAY_STR
122 if (delaykey[0].str == NULL)
123 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
125 # ifdef CONFIG_AUTOBOOT_STOP_STR
126 if (delaykey[1].str == NULL)
127 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
130 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
131 delaykey[i].len = delaykey[i].str == NULL ?
132 0 : strlen(delaykey[i].str);
133 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
134 MAX_DELAY_STOP_STR : delaykey[i].len;
136 presskey_max = presskey_max > delaykey[i].len ?
137 presskey_max : delaykey[i].len;
139 debug_bootkeys("%s key:<%s>\n",
140 delaykey[i].retry ? "delay" : "stop",
141 delaykey[i].str ? delaykey[i].str : "NULL");
144 /* In order to keep up with incoming data, check timeout only
149 if (presskey_len < presskey_max) {
150 presskey[presskey_len++] = getc();
152 for (i = 0; i < presskey_max - 1; i++)
153 presskey[i] = presskey[i + 1];
155 presskey[i] = getc();
159 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
160 if (delaykey[i].len > 0 &&
161 presskey_len >= delaykey[i].len &&
162 memcmp(presskey + presskey_len -
163 delaykey[i].len, delaykey[i].str,
164 delaykey[i].len) == 0) {
165 debug_bootkeys("got %skey\n",
166 delaykey[i].retry ? "delay" :
169 /* don't retry auto boot */
170 if (!delaykey[i].retry)
171 bootretry_dont_retry();
175 } while (!abort && get_ticks() <= etime);
181 /***************************************************************************
182 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
183 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
185 static int abortboot_keyed(int bootdelay)
188 uint64_t etime = endtick(bootdelay);
190 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
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 abort = passwd_abort(etime);
205 debug_bootkeys("key timeout\n");
207 #ifdef CONFIG_SILENT_CONSOLE
209 gd->flags &= ~GD_FLG_SILENT;
215 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
217 #ifdef CONFIG_MENUKEY
221 static int abortboot_normal(int bootdelay)
226 #ifdef CONFIG_MENUPROMPT
227 printf(CONFIG_MENUPROMPT);
230 printf("Hit any key to stop autoboot: %2d ", bootdelay);
233 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
235 * Check if key already pressed
236 * Don't check if bootdelay < 0
238 if (bootdelay >= 0) {
239 if (tstc()) { /* we got a key press */
240 (void) getc(); /* consume input */
242 abort = 1; /* don't auto boot */
247 while ((bootdelay > 0) && (!abort)) {
252 if (tstc()) { /* we got a key press */
253 abort = 1; /* don't auto boot */
254 bootdelay = 0; /* no more delay */
255 # ifdef CONFIG_MENUKEY
258 (void) getc(); /* consume input */
263 } while (!abort && get_timer(ts) < 1000);
265 printf("\b\b\b%2d ", bootdelay);
270 #ifdef CONFIG_SILENT_CONSOLE
272 gd->flags &= ~GD_FLG_SILENT;
277 # endif /* CONFIG_AUTOBOOT_KEYED */
279 static int abortboot(int bootdelay)
281 #ifdef CONFIG_AUTOBOOT_KEYED
282 return abortboot_keyed(bootdelay);
284 return abortboot_normal(bootdelay);
288 static void process_fdt_options(const void *blob)
290 #if defined(CONFIG_OF_CONTROL)
293 /* Add an env variable to point to a kernel payload, if available */
294 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
296 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
298 /* Add an env variable to point to a root disk, if available */
299 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
301 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
302 #endif /* CONFIG_OF_CONTROL */
305 const char *bootdelay_process(void)
309 #ifdef CONFIG_BOOTCOUNT_LIMIT
310 unsigned long bootcount = 0;
311 unsigned long bootlimit = 0;
312 #endif /* CONFIG_BOOTCOUNT_LIMIT */
314 #ifdef CONFIG_BOOTCOUNT_LIMIT
315 bootcount = bootcount_load();
317 bootcount_store(bootcount);
318 setenv_ulong("bootcount", bootcount);
319 bootlimit = getenv_ulong("bootlimit", 10, 0);
320 #endif /* CONFIG_BOOTCOUNT_LIMIT */
322 s = getenv("bootdelay");
323 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
325 #ifdef CONFIG_OF_CONTROL
326 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
330 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
332 #if defined(CONFIG_MENU_SHOW)
333 bootdelay = menu_show(bootdelay);
335 bootretry_init_cmd_timeout();
338 if (gd->flags & GD_FLG_POSTFAIL) {
339 s = getenv("failbootcmd");
341 #endif /* CONFIG_POST */
342 #ifdef CONFIG_BOOTCOUNT_LIMIT
343 if (bootlimit && (bootcount > bootlimit)) {
344 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
345 (unsigned)bootlimit);
346 s = getenv("altbootcmd");
348 #endif /* CONFIG_BOOTCOUNT_LIMIT */
349 s = getenv("bootcmd");
351 process_fdt_options(gd->fdt_blob);
352 stored_bootdelay = bootdelay;
357 void autoboot_command(const char *s)
359 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
361 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
362 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
363 int prev = disable_ctrlc(1); /* disable Control C checking */
366 run_command_list(s, -1, 0);
368 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
369 disable_ctrlc(prev); /* restore Control C checking */
373 #ifdef CONFIG_MENUKEY
374 if (menukey == CONFIG_MENUKEY) {
375 s = getenv("menucmd");
377 run_command_list(s, -1, 0);
379 #endif /* CONFIG_MENUKEY */