blob: 8faac2005dfeced5ced8dd5e442b00e51a1b54d1 [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 Glass313112a2019-08-01 09:46:46 -060012#include <env.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060013#include <fdtdec.h>
Simon Glass6a2e09f2019-07-20 20:51:16 -060014#include <hash.h>
Heiko Schocher2ef3a952019-07-29 07:23:19 +020015#include <memalign.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060016#include <menu.h>
17#include <post.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070018#include <time.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020019#include <u-boot/sha256.h>
Lukasz Majewski4fc18912018-05-02 16:10:53 +020020#include <bootcount.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060021
22DECLARE_GLOBAL_DATA_PTR;
23
24#define MAX_DELAY_STOP_STR 32
25
26#ifndef DEBUG_BOOTKEYS
27#define DEBUG_BOOTKEYS 0
28#endif
29#define debug_bootkeys(fmt, args...) \
30 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
31
Simon Glass5b47e302014-04-10 20:01:35 -060032/* Stored value of bootdelay, used by autoboot_command() */
33static int stored_bootdelay;
Simon Glass9d983852019-07-20 20:51:23 -060034static int menukey;
Simon Glass5b47e302014-04-10 20:01:35 -060035
Simon Glass760d3fb2019-07-20 20:51:15 -060036#ifdef CONFIG_AUTOBOOT_ENCRYPTION
37#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
38#else
39#define AUTOBOOT_STOP_STR_SHA256 ""
40#endif
41
Simon Glass9d983852019-07-20 20:51:23 -060042#ifdef CONFIG_USE_AUTOBOOT_MENUKEY
43#define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
44#else
45#define AUTOBOOT_MENUKEY 0
46#endif
47
Stefan Roese0ed2e462015-05-18 14:08:24 +020048/*
49 * Use a "constant-length" time compare function for this
50 * hash compare:
51 *
52 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -060053 */
Stefan Roese0ed2e462015-05-18 14:08:24 +020054static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -060055{
Stefan Roese0ed2e462015-05-18 14:08:24 +020056 int diff = 0;
57 int i;
58
59 for (i = 0; i < len; i++)
60 diff |= a[i] ^ b[i];
61
62 return diff == 0;
63}
64
Simon Glassa8cab882019-07-20 20:51:17 -060065/**
66 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
67 *
68 * This checks for the user entering a SHA256 hash within a given time.
69 *
70 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
71 * @return 0 if autoboot should continue, 1 if it should stop
72 */
Simon Glass6a2e09f2019-07-20 20:51:16 -060073static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +020074{
Simon Glass64b723f2017-08-03 12:22:12 -060075 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +020076 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +020077 u8 *sha;
78 char *presskey;
Stefan Roese0ed2e462015-05-18 14:08:24 +020079 const char *algo_name = "sha256";
80 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -060081 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +010082 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +020083 int ret;
84
85 if (sha_env_str == NULL)
Simon Glass760d3fb2019-07-20 20:51:15 -060086 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +020087
88 /*
89 * Generate the binary value from the environment hash value
90 * so that we can compare this value with the computed hash
91 * from the user input
92 */
93 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
94 if (ret) {
95 printf("Hash %s not supported!\n", algo_name);
96 return 0;
97 }
98
Heiko Schocher2ef3a952019-07-29 07:23:19 +020099 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
100 sha = malloc_cache_aligned(SHA256_SUM_LEN);
101 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200102 /*
103 * We don't know how long the stop-string is, so we need to
104 * generate the sha256 hash upon each input character and
105 * compare the value with the one saved in the environment
106 */
107 do {
108 if (tstc()) {
109 /* Check for input string overflow */
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200110 if (presskey_len >= MAX_DELAY_STOP_STR) {
111 free(presskey);
112 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200113 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200114 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200115
116 presskey[presskey_len++] = getc();
117
118 /* Calculate sha256 upon each new char */
119 hash_block(algo_name, (const void *)presskey,
120 presskey_len, sha, &size);
121
122 /* And check if sha matches saved value in env */
123 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
124 abort = 1;
125 }
126 } while (!abort && get_ticks() <= etime);
127
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200128 free(presskey);
129 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200130 return abort;
131}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600132
Simon Glassa8cab882019-07-20 20:51:17 -0600133/**
134 * passwd_abort_key() - check for a key sequence to aborted booting
135 *
136 * This checks for the user entering a string within a given time.
137 *
138 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
139 * @return 0 if autoboot should continue, 1 if it should stop
140 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600141static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200142{
143 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600144 struct {
145 char *str;
146 u_int len;
147 int retry;
148 }
149 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600150 { .str = env_get("bootdelaykey"), .retry = 1 },
151 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600152 };
153
154 char presskey[MAX_DELAY_STOP_STR];
155 u_int presskey_len = 0;
156 u_int presskey_max = 0;
157 u_int i;
158
Simon Glass7b6a95a2014-04-10 20:01:28 -0600159# ifdef CONFIG_AUTOBOOT_DELAY_STR
160 if (delaykey[0].str == NULL)
161 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
162# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600163# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200164 if (delaykey[1].str == NULL)
165 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600166# endif
167
168 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
169 delaykey[i].len = delaykey[i].str == NULL ?
170 0 : strlen(delaykey[i].str);
171 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
172 MAX_DELAY_STOP_STR : delaykey[i].len;
173
174 presskey_max = presskey_max > delaykey[i].len ?
175 presskey_max : delaykey[i].len;
176
177 debug_bootkeys("%s key:<%s>\n",
178 delaykey[i].retry ? "delay" : "stop",
179 delaykey[i].str ? delaykey[i].str : "NULL");
180 }
181
182 /* In order to keep up with incoming data, check timeout only
183 * when catch up.
184 */
185 do {
186 if (tstc()) {
187 if (presskey_len < presskey_max) {
188 presskey[presskey_len++] = getc();
189 } else {
190 for (i = 0; i < presskey_max - 1; i++)
191 presskey[i] = presskey[i + 1];
192
193 presskey[i] = getc();
194 }
195 }
196
197 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
198 if (delaykey[i].len > 0 &&
199 presskey_len >= delaykey[i].len &&
200 memcmp(presskey + presskey_len -
201 delaykey[i].len, delaykey[i].str,
202 delaykey[i].len) == 0) {
203 debug_bootkeys("got %skey\n",
204 delaykey[i].retry ? "delay" :
205 "stop");
206
Simon Glass7b6a95a2014-04-10 20:01:28 -0600207 /* don't retry auto boot */
208 if (!delaykey[i].retry)
209 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600210 abort = 1;
211 }
212 }
213 } while (!abort && get_ticks() <= etime);
214
Stefan Roese0ed2e462015-05-18 14:08:24 +0200215 return abort;
216}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200217
218/***************************************************************************
219 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
220 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
221 */
Simon Glass6d99d132019-07-20 20:51:19 -0600222static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200223{
224 int abort;
225 uint64_t etime = endtick(bootdelay);
226
Stefan Roese0ed2e462015-05-18 14:08:24 +0200227# ifdef CONFIG_AUTOBOOT_PROMPT
228 /*
229 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
230 * To print the bootdelay value upon bootup.
231 */
232 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
233# endif
234
Simon Glass6a2e09f2019-07-20 20:51:16 -0600235 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
236 abort = passwd_abort_sha256(etime);
237 else
238 abort = passwd_abort_key(etime);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600239 if (!abort)
240 debug_bootkeys("key timeout\n");
241
Simon Glass7b6a95a2014-04-10 20:01:28 -0600242 return abort;
243}
244
Simon Glass6d99d132019-07-20 20:51:19 -0600245static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600246{
247 int abort = 0;
248 unsigned long ts;
249
Masahiro Yamada9a333862016-06-27 16:23:04 +0900250 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600251
Simon Glass7b6a95a2014-04-10 20:01:28 -0600252 /*
253 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600254 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900255 if (tstc()) { /* we got a key press */
256 (void) getc(); /* consume input */
257 puts("\b\b\b 0");
258 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600259 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600260
261 while ((bootdelay > 0) && (!abort)) {
262 --bootdelay;
263 /* delay 1000 ms */
264 ts = get_timer(0);
265 do {
266 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600267 int key;
268
Simon Glass7b6a95a2014-04-10 20:01:28 -0600269 abort = 1; /* don't auto boot */
270 bootdelay = 0; /* no more delay */
Simon Glass9d983852019-07-20 20:51:23 -0600271 key = getc(); /* consume input */
272 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
273 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600274 break;
275 }
276 udelay(10000);
277 } while (!abort && get_timer(ts) < 1000);
278
279 printf("\b\b\b%2d ", bootdelay);
280 }
281
282 putc('\n');
283
Simon Glass7b6a95a2014-04-10 20:01:28 -0600284 return abort;
285}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600286
287static int abortboot(int bootdelay)
288{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900289 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900290
Simon Glass6d99d132019-07-20 20:51:19 -0600291 if (bootdelay >= 0) {
292 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
293 abort = abortboot_key_sequence(bootdelay);
294 else
295 abort = abortboot_single_key(bootdelay);
296 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900297
Simon Glass3a315052019-07-20 20:51:18 -0600298 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900299 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900300
301 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600302}
303
Simon Glass7b6a95a2014-04-10 20:01:28 -0600304static void process_fdt_options(const void *blob)
305{
Simon Glassb97ace52019-07-20 20:51:27 -0600306#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600307 ulong addr;
308
309 /* Add an env variable to point to a kernel payload, if available */
310 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
311 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600312 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600313
314 /* Add an env variable to point to a root disk, if available */
315 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
316 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600317 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glassb97ace52019-07-20 20:51:27 -0600318#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600319}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600320
Simon Glass5b47e302014-04-10 20:01:35 -0600321const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600322{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600323 char *s;
324 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600325
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200326 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600327
Simon Glass64b723f2017-08-03 12:22:12 -0600328 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600329 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
330
Simon Glassb97ace52019-07-20 20:51:27 -0600331 if (IS_ENABLED(CONFIG_OF_CONTROL))
332 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
333 bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600334
335 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
336
Simon Glassb97ace52019-07-20 20:51:27 -0600337 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
338 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600339 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600340
341#ifdef CONFIG_POST
342 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600343 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600344 } else
345#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200346 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600347 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200348 else
Simon Glass64b723f2017-08-03 12:22:12 -0600349 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600350
Simon Glassb97ace52019-07-20 20:51:27 -0600351 if (IS_ENABLED(CONFIG_OF_CONTROL))
352 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600353 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600354
Simon Glass5b47e302014-04-10 20:01:35 -0600355 return s;
356}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600357
Simon Glass5b47e302014-04-10 20:01:35 -0600358void autoboot_command(const char *s)
359{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600360 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
361
Simon Glass5b47e302014-04-10 20:01:35 -0600362 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600363 bool lock;
364 int prev;
365
366 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
367 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
368 if (lock)
369 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600370
371 run_command_list(s, -1, 0);
372
Simon Glass313fcbd2019-07-20 20:51:28 -0600373 if (lock)
374 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600375 }
376
Simon Glass9d983852019-07-20 20:51:23 -0600377 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
378 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600379 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600380 if (s)
381 run_command_list(s, -1, 0);
382 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600383}