blob: 45df6656760b9ad9655d50be4bbe7e78a78e03ba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7b6a95a2014-04-10 20:01:28 -06002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass7b6a95a2014-04-10 20:01:28 -06005 */
6
7#include <common.h>
Jeroen Hofsteef682d8c2014-07-13 22:57:58 +02008#include <autoboot.h>
Simon Glass399ed9a2014-04-10 20:01:30 -06009#include <bootretry.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060010#include <cli.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060012#include <fdtdec.h>
Simon Glass6a2e09f2019-07-20 20:51:16 -060013#include <hash.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060014#include <menu.h>
15#include <post.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020016#include <u-boot/sha256.h>
Lukasz Majewski4fc18912018-05-02 16:10:53 +020017#include <bootcount.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060018
19DECLARE_GLOBAL_DATA_PTR;
20
21#define MAX_DELAY_STOP_STR 32
22
23#ifndef DEBUG_BOOTKEYS
24#define DEBUG_BOOTKEYS 0
25#endif
26#define debug_bootkeys(fmt, args...) \
27 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
28
Simon Glass5b47e302014-04-10 20:01:35 -060029/* Stored value of bootdelay, used by autoboot_command() */
30static int stored_bootdelay;
31
Simon Glass760d3fb2019-07-20 20:51:15 -060032#ifdef CONFIG_AUTOBOOT_ENCRYPTION
33#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
34#else
35#define AUTOBOOT_STOP_STR_SHA256 ""
36#endif
37
Stefan Roese0ed2e462015-05-18 14:08:24 +020038#if defined(CONFIG_AUTOBOOT_KEYED)
Stefan Roese0ed2e462015-05-18 14:08:24 +020039
40/*
41 * Use a "constant-length" time compare function for this
42 * hash compare:
43 *
44 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -060045 */
Stefan Roese0ed2e462015-05-18 14:08:24 +020046static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -060047{
Stefan Roese0ed2e462015-05-18 14:08:24 +020048 int diff = 0;
49 int i;
50
51 for (i = 0; i < len; i++)
52 diff |= a[i] ^ b[i];
53
54 return diff == 0;
55}
56
Simon Glassa8cab882019-07-20 20:51:17 -060057/**
58 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
59 *
60 * This checks for the user entering a SHA256 hash within a given time.
61 *
62 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
63 * @return 0 if autoboot should continue, 1 if it should stop
64 */
Simon Glass6a2e09f2019-07-20 20:51:16 -060065static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +020066{
Simon Glass64b723f2017-08-03 12:22:12 -060067 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +020068 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;
Simon Glass7b6a95a2014-04-10 20:01:28 -060073 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +010074 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +020075 int ret;
76
77 if (sha_env_str == NULL)
Simon Glass760d3fb2019-07-20 20:51:15 -060078 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +020079
80 /*
81 * Generate the binary value from the environment hash value
82 * so that we can compare this value with the computed hash
83 * from the user input
84 */
85 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
86 if (ret) {
87 printf("Hash %s not supported!\n", algo_name);
88 return 0;
89 }
90
91 /*
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
95 */
96 do {
97 if (tstc()) {
98 /* Check for input string overflow */
99 if (presskey_len >= MAX_DELAY_STOP_STR)
100 return 0;
101
102 presskey[presskey_len++] = getc();
103
104 /* Calculate sha256 upon each new char */
105 hash_block(algo_name, (const void *)presskey,
106 presskey_len, sha, &size);
107
108 /* And check if sha matches saved value in env */
109 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
110 abort = 1;
111 }
112 } while (!abort && get_ticks() <= etime);
113
114 return abort;
115}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600116
Simon Glassa8cab882019-07-20 20:51:17 -0600117/**
118 * passwd_abort_key() - check for a key sequence to aborted booting
119 *
120 * This checks for the user entering a string within a given time.
121 *
122 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
123 * @return 0 if autoboot should continue, 1 if it should stop
124 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600125static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200126{
127 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600128 struct {
129 char *str;
130 u_int len;
131 int retry;
132 }
133 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600134 { .str = env_get("bootdelaykey"), .retry = 1 },
135 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600136 };
137
138 char presskey[MAX_DELAY_STOP_STR];
139 u_int presskey_len = 0;
140 u_int presskey_max = 0;
141 u_int i;
142
Simon Glass7b6a95a2014-04-10 20:01:28 -0600143# ifdef CONFIG_AUTOBOOT_DELAY_STR
144 if (delaykey[0].str == NULL)
145 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
146# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600147# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200148 if (delaykey[1].str == NULL)
149 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600150# endif
151
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;
157
158 presskey_max = presskey_max > delaykey[i].len ?
159 presskey_max : delaykey[i].len;
160
161 debug_bootkeys("%s key:<%s>\n",
162 delaykey[i].retry ? "delay" : "stop",
163 delaykey[i].str ? delaykey[i].str : "NULL");
164 }
165
166 /* In order to keep up with incoming data, check timeout only
167 * when catch up.
168 */
169 do {
170 if (tstc()) {
171 if (presskey_len < presskey_max) {
172 presskey[presskey_len++] = getc();
173 } else {
174 for (i = 0; i < presskey_max - 1; i++)
175 presskey[i] = presskey[i + 1];
176
177 presskey[i] = getc();
178 }
179 }
180
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" :
189 "stop");
190
Simon Glass7b6a95a2014-04-10 20:01:28 -0600191 /* don't retry auto boot */
192 if (!delaykey[i].retry)
193 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600194 abort = 1;
195 }
196 }
197 } while (!abort && get_ticks() <= etime);
198
Stefan Roese0ed2e462015-05-18 14:08:24 +0200199 return abort;
200}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200201
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
205 */
Masahiro Yamada6d1248a2016-06-27 16:23:02 +0900206static int __abortboot(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200207{
208 int abort;
209 uint64_t etime = endtick(bootdelay);
210
Stefan Roese0ed2e462015-05-18 14:08:24 +0200211# ifdef CONFIG_AUTOBOOT_PROMPT
212 /*
213 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
214 * To print the bootdelay value upon bootup.
215 */
216 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
217# endif
218
Simon Glass6a2e09f2019-07-20 20:51:16 -0600219 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
220 abort = passwd_abort_sha256(etime);
221 else
222 abort = passwd_abort_key(etime);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600223 if (!abort)
224 debug_bootkeys("key timeout\n");
225
Simon Glass7b6a95a2014-04-10 20:01:28 -0600226 return abort;
227}
228
229# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
230
231#ifdef CONFIG_MENUKEY
232static int menukey;
233#endif
234
Masahiro Yamada6d1248a2016-06-27 16:23:02 +0900235static int __abortboot(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600236{
237 int abort = 0;
238 unsigned long ts;
239
240#ifdef CONFIG_MENUPROMPT
241 printf(CONFIG_MENUPROMPT);
242#else
Masahiro Yamada9a333862016-06-27 16:23:04 +0900243 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600244#endif
245
Simon Glass7b6a95a2014-04-10 20:01:28 -0600246 /*
247 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600248 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900249 if (tstc()) { /* we got a key press */
250 (void) getc(); /* consume input */
251 puts("\b\b\b 0");
252 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600253 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600254
255 while ((bootdelay > 0) && (!abort)) {
256 --bootdelay;
257 /* delay 1000 ms */
258 ts = get_timer(0);
259 do {
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
264 menukey = getc();
265# else
266 (void) getc(); /* consume input */
267# endif
268 break;
269 }
270 udelay(10000);
271 } while (!abort && get_timer(ts) < 1000);
272
273 printf("\b\b\b%2d ", bootdelay);
274 }
275
276 putc('\n');
277
Simon Glass7b6a95a2014-04-10 20:01:28 -0600278 return abort;
279}
280# endif /* CONFIG_AUTOBOOT_KEYED */
281
282static int abortboot(int bootdelay)
283{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900284 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900285
Masahiro Yamada9a333862016-06-27 16:23:04 +0900286 if (bootdelay >= 0)
287 abort = __abortboot(bootdelay);
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900288
Simon Glass3a315052019-07-20 20:51:18 -0600289 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900290 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900291
292 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600293}
294
Simon Glass7b6a95a2014-04-10 20:01:28 -0600295static void process_fdt_options(const void *blob)
296{
Stefan Roesec9fa9a52016-01-28 17:34:40 +0100297#if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600298 ulong addr;
299
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);
302 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600303 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600304
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);
307 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600308 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Stefan Roesec9fa9a52016-01-28 17:34:40 +0100309#endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600310}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600311
Simon Glass5b47e302014-04-10 20:01:35 -0600312const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600313{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600314 char *s;
315 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600316
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200317 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600318
Simon Glass64b723f2017-08-03 12:22:12 -0600319 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600320 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
321
322#ifdef CONFIG_OF_CONTROL
323 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
324 bootdelay);
325#endif
326
327 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
328
329#if defined(CONFIG_MENU_SHOW)
330 bootdelay = menu_show(bootdelay);
331#endif
Simon Glass09007c42014-04-10 20:01:31 -0600332 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600333
334#ifdef CONFIG_POST
335 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600336 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600337 } else
338#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200339 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600340 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200341 else
Simon Glass64b723f2017-08-03 12:22:12 -0600342 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600343
344 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600345 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600346
Simon Glass5b47e302014-04-10 20:01:35 -0600347 return s;
348}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600349
Simon Glass5b47e302014-04-10 20:01:35 -0600350void autoboot_command(const char *s)
351{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600352 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
353
Simon Glass5b47e302014-04-10 20:01:35 -0600354 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass7b6a95a2014-04-10 20:01:28 -0600355#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
356 int prev = disable_ctrlc(1); /* disable Control C checking */
357#endif
358
359 run_command_list(s, -1, 0);
360
361#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
362 disable_ctrlc(prev); /* restore Control C checking */
363#endif
364 }
365
366#ifdef CONFIG_MENUKEY
367 if (menukey == CONFIG_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600368 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600369 if (s)
370 run_command_list(s, -1, 0);
371 }
372#endif /* CONFIG_MENUKEY */
373}