blob: 6d78716a266a0e766b523954257c27591225cd68 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass66ded172014-04-10 20:01:28 -06002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass66ded172014-04-10 20:01:28 -06005 */
6
7#include <common.h>
Jeroen Hofstee39e12302014-07-13 22:57:58 +02008#include <autoboot.h>
Simon Glass0098e172014-04-10 20:01:30 -06009#include <bootretry.h>
Simon Glass66ded172014-04-10 20:01:28 -060010#include <cli.h>
Simon Glass288b29e2019-11-14 12:57:43 -070011#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060013#include <env.h>
Simon Glass66ded172014-04-10 20:01:28 -060014#include <fdtdec.h>
Simon Glasse8c78052019-07-20 20:51:16 -060015#include <hash.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <malloc.h>
Heiko Schocherecaae802019-07-29 07:23:19 +020018#include <memalign.h>
Simon Glass66ded172014-04-10 20:01:28 -060019#include <menu.h>
20#include <post.h>
Simon Glass10453152019-11-14 12:57:30 -070021#include <time.h>
Simon Glassc05ed002020-05-10 11:40:11 -060022#include <linux/delay.h>
Stefan Roese8f0b1e22015-05-18 14:08:24 +020023#include <u-boot/sha256.h>
Lukasz Majewskibc8c4402018-05-02 16:10:53 +020024#include <bootcount.h>
Simon Glass66ded172014-04-10 20:01:28 -060025
26DECLARE_GLOBAL_DATA_PTR;
27
28#define MAX_DELAY_STOP_STR 32
29
30#ifndef DEBUG_BOOTKEYS
31#define DEBUG_BOOTKEYS 0
32#endif
33#define debug_bootkeys(fmt, args...) \
34 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
35
Simon Glassaffb2152014-04-10 20:01:35 -060036/* Stored value of bootdelay, used by autoboot_command() */
37static int stored_bootdelay;
Simon Glassd915ad22019-07-20 20:51:23 -060038static int menukey;
Simon Glassaffb2152014-04-10 20:01:35 -060039
Simon Glass0c4bd312019-07-20 20:51:15 -060040#ifdef CONFIG_AUTOBOOT_ENCRYPTION
41#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
42#else
43#define AUTOBOOT_STOP_STR_SHA256 ""
44#endif
45
Simon Glassd915ad22019-07-20 20:51:23 -060046#ifdef CONFIG_USE_AUTOBOOT_MENUKEY
47#define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
48#else
49#define AUTOBOOT_MENUKEY 0
50#endif
51
Stefan Roese8f0b1e22015-05-18 14:08:24 +020052/*
53 * Use a "constant-length" time compare function for this
54 * hash compare:
55 *
56 * https://crackstation.net/hashing-security.htm
Simon Glass66ded172014-04-10 20:01:28 -060057 */
Stefan Roese8f0b1e22015-05-18 14:08:24 +020058static int slow_equals(u8 *a, u8 *b, int len)
59{
60 int diff = 0;
61 int i;
62
63 for (i = 0; i < len; i++)
64 diff |= a[i] ^ b[i];
65
66 return diff == 0;
67}
68
Simon Glass88fa4be2019-07-20 20:51:17 -060069/**
70 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
71 *
72 * This checks for the user entering a SHA256 hash within a given time.
73 *
74 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
75 * @return 0 if autoboot should continue, 1 if it should stop
76 */
Simon Glasse8c78052019-07-20 20:51:16 -060077static int passwd_abort_sha256(uint64_t etime)
Stefan Roese8f0b1e22015-05-18 14:08:24 +020078{
Simon Glass00caae62017-08-03 12:22:12 -060079 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese8f0b1e22015-05-18 14:08:24 +020080 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocherecaae802019-07-29 07:23:19 +020081 u8 *sha;
82 char *presskey;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020083 const char *algo_name = "sha256";
84 u_int presskey_len = 0;
85 int abort = 0;
Martin Etnestad2d06fd82018-01-12 09:04:38 +010086 int size = sizeof(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +020087 int ret;
88
89 if (sha_env_str == NULL)
Simon Glass0c4bd312019-07-20 20:51:15 -060090 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020091
92 /*
93 * Generate the binary value from the environment hash value
94 * so that we can compare this value with the computed hash
95 * from the user input
96 */
97 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
98 if (ret) {
99 printf("Hash %s not supported!\n", algo_name);
100 return 0;
101 }
102
Heiko Schocherecaae802019-07-29 07:23:19 +0200103 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
104 sha = malloc_cache_aligned(SHA256_SUM_LEN);
105 size = SHA256_SUM_LEN;
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200106 /*
107 * We don't know how long the stop-string is, so we need to
108 * generate the sha256 hash upon each input character and
109 * compare the value with the one saved in the environment
110 */
111 do {
112 if (tstc()) {
113 /* Check for input string overflow */
Heiko Schocherecaae802019-07-29 07:23:19 +0200114 if (presskey_len >= MAX_DELAY_STOP_STR) {
115 free(presskey);
116 free(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200117 return 0;
Heiko Schocherecaae802019-07-29 07:23:19 +0200118 }
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200119
120 presskey[presskey_len++] = getc();
121
122 /* Calculate sha256 upon each new char */
123 hash_block(algo_name, (const void *)presskey,
124 presskey_len, sha, &size);
125
126 /* And check if sha matches saved value in env */
127 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
128 abort = 1;
129 }
130 } while (!abort && get_ticks() <= etime);
131
Heiko Schocherecaae802019-07-29 07:23:19 +0200132 free(presskey);
133 free(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200134 return abort;
135}
Simon Glasse8c78052019-07-20 20:51:16 -0600136
Simon Glass88fa4be2019-07-20 20:51:17 -0600137/**
138 * passwd_abort_key() - check for a key sequence to aborted booting
139 *
140 * This checks for the user entering a string within a given time.
141 *
142 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
143 * @return 0 if autoboot should continue, 1 if it should stop
144 */
Simon Glasse8c78052019-07-20 20:51:16 -0600145static int passwd_abort_key(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600146{
147 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600148 struct {
149 char *str;
150 u_int len;
151 int retry;
152 }
153 delaykey[] = {
Simon Glass00caae62017-08-03 12:22:12 -0600154 { .str = env_get("bootdelaykey"), .retry = 1 },
155 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600156 };
157
158 char presskey[MAX_DELAY_STOP_STR];
159 u_int presskey_len = 0;
160 u_int presskey_max = 0;
161 u_int i;
162
Simon Glass66ded172014-04-10 20:01:28 -0600163# ifdef CONFIG_AUTOBOOT_DELAY_STR
164 if (delaykey[0].str == NULL)
165 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
166# endif
Simon Glass66ded172014-04-10 20:01:28 -0600167# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200168 if (delaykey[1].str == NULL)
169 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600170# endif
171
172 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
173 delaykey[i].len = delaykey[i].str == NULL ?
174 0 : strlen(delaykey[i].str);
175 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
176 MAX_DELAY_STOP_STR : delaykey[i].len;
177
178 presskey_max = presskey_max > delaykey[i].len ?
179 presskey_max : delaykey[i].len;
180
181 debug_bootkeys("%s key:<%s>\n",
182 delaykey[i].retry ? "delay" : "stop",
183 delaykey[i].str ? delaykey[i].str : "NULL");
184 }
185
186 /* In order to keep up with incoming data, check timeout only
187 * when catch up.
188 */
189 do {
190 if (tstc()) {
191 if (presskey_len < presskey_max) {
192 presskey[presskey_len++] = getc();
193 } else {
194 for (i = 0; i < presskey_max - 1; i++)
195 presskey[i] = presskey[i + 1];
196
197 presskey[i] = getc();
198 }
199 }
200
201 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
202 if (delaykey[i].len > 0 &&
203 presskey_len >= delaykey[i].len &&
204 memcmp(presskey + presskey_len -
205 delaykey[i].len, delaykey[i].str,
206 delaykey[i].len) == 0) {
207 debug_bootkeys("got %skey\n",
208 delaykey[i].retry ? "delay" :
209 "stop");
210
Simon Glass66ded172014-04-10 20:01:28 -0600211 /* don't retry auto boot */
212 if (!delaykey[i].retry)
213 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600214 abort = 1;
215 }
216 }
217 } while (!abort && get_ticks() <= etime);
218
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200219 return abort;
220}
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200221
222/***************************************************************************
223 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
224 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
225 */
Simon Glasse79e4b22019-07-20 20:51:19 -0600226static int abortboot_key_sequence(int bootdelay)
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200227{
228 int abort;
229 uint64_t etime = endtick(bootdelay);
230
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200231# ifdef CONFIG_AUTOBOOT_PROMPT
232 /*
233 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
234 * To print the bootdelay value upon bootup.
235 */
236 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
237# endif
238
Simon Glasse8c78052019-07-20 20:51:16 -0600239 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
240 abort = passwd_abort_sha256(etime);
241 else
242 abort = passwd_abort_key(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600243 if (!abort)
244 debug_bootkeys("key timeout\n");
245
Simon Glass66ded172014-04-10 20:01:28 -0600246 return abort;
247}
248
Simon Glasse79e4b22019-07-20 20:51:19 -0600249static int abortboot_single_key(int bootdelay)
Simon Glass66ded172014-04-10 20:01:28 -0600250{
251 int abort = 0;
252 unsigned long ts;
253
Masahiro Yamada46327392016-06-27 16:23:04 +0900254 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600255
Simon Glass66ded172014-04-10 20:01:28 -0600256 /*
257 * Check if key already pressed
Simon Glass66ded172014-04-10 20:01:28 -0600258 */
Masahiro Yamada46327392016-06-27 16:23:04 +0900259 if (tstc()) { /* we got a key press */
260 (void) getc(); /* consume input */
261 puts("\b\b\b 0");
262 abort = 1; /* don't auto boot */
Simon Glass66ded172014-04-10 20:01:28 -0600263 }
Simon Glass66ded172014-04-10 20:01:28 -0600264
265 while ((bootdelay > 0) && (!abort)) {
266 --bootdelay;
267 /* delay 1000 ms */
268 ts = get_timer(0);
269 do {
270 if (tstc()) { /* we got a key press */
Simon Glassd915ad22019-07-20 20:51:23 -0600271 int key;
272
Simon Glass66ded172014-04-10 20:01:28 -0600273 abort = 1; /* don't auto boot */
274 bootdelay = 0; /* no more delay */
Simon Glassd915ad22019-07-20 20:51:23 -0600275 key = getc(); /* consume input */
276 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
277 menukey = key;
Simon Glass66ded172014-04-10 20:01:28 -0600278 break;
279 }
280 udelay(10000);
281 } while (!abort && get_timer(ts) < 1000);
282
283 printf("\b\b\b%2d ", bootdelay);
284 }
285
286 putc('\n');
287
Simon Glass66ded172014-04-10 20:01:28 -0600288 return abort;
289}
Simon Glass66ded172014-04-10 20:01:28 -0600290
291static int abortboot(int bootdelay)
292{
Masahiro Yamada46327392016-06-27 16:23:04 +0900293 int abort = 0;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900294
Simon Glasse79e4b22019-07-20 20:51:19 -0600295 if (bootdelay >= 0) {
296 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
297 abort = abortboot_key_sequence(bootdelay);
298 else
299 abort = abortboot_single_key(bootdelay);
300 }
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900301
Simon Glass42b4d142019-07-20 20:51:18 -0600302 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900303 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900304
305 return abort;
Simon Glass66ded172014-04-10 20:01:28 -0600306}
307
Simon Glass66ded172014-04-10 20:01:28 -0600308static void process_fdt_options(const void *blob)
309{
Simon Glass5fa3fd22019-07-20 20:51:27 -0600310#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass66ded172014-04-10 20:01:28 -0600311 ulong addr;
312
313 /* Add an env variable to point to a kernel payload, if available */
314 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
315 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600316 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600317
318 /* Add an env variable to point to a root disk, if available */
319 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
320 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600321 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass5fa3fd22019-07-20 20:51:27 -0600322#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glassaffb2152014-04-10 20:01:35 -0600323}
Simon Glass66ded172014-04-10 20:01:28 -0600324
Simon Glassaffb2152014-04-10 20:01:35 -0600325const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600326{
Simon Glass66ded172014-04-10 20:01:28 -0600327 char *s;
328 int bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600329
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200330 bootcount_inc();
Simon Glass66ded172014-04-10 20:01:28 -0600331
Simon Glass00caae62017-08-03 12:22:12 -0600332 s = env_get("bootdelay");
Simon Glass66ded172014-04-10 20:01:28 -0600333 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
334
Simon Glass5fa3fd22019-07-20 20:51:27 -0600335 if (IS_ENABLED(CONFIG_OF_CONTROL))
336 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
337 bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600338
339 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
340
Simon Glass5fa3fd22019-07-20 20:51:27 -0600341 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
342 bootdelay = menu_show(bootdelay);
Simon Glassb26440f2014-04-10 20:01:31 -0600343 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600344
345#ifdef CONFIG_POST
346 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass00caae62017-08-03 12:22:12 -0600347 s = env_get("failbootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600348 } else
349#endif /* CONFIG_POST */
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200350 if (bootcount_error())
Simon Glass00caae62017-08-03 12:22:12 -0600351 s = env_get("altbootcmd");
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200352 else
Simon Glass00caae62017-08-03 12:22:12 -0600353 s = env_get("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600354
Simon Glass5fa3fd22019-07-20 20:51:27 -0600355 if (IS_ENABLED(CONFIG_OF_CONTROL))
356 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600357 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600358
Simon Glassaffb2152014-04-10 20:01:35 -0600359 return s;
360}
Simon Glass66ded172014-04-10 20:01:28 -0600361
Simon Glassaffb2152014-04-10 20:01:35 -0600362void autoboot_command(const char *s)
363{
Simon Glass66ded172014-04-10 20:01:28 -0600364 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
365
Simon Glassaffb2152014-04-10 20:01:35 -0600366 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass5ec35ff2019-07-20 20:51:28 -0600367 bool lock;
368 int prev;
369
370 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
371 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
372 if (lock)
373 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass66ded172014-04-10 20:01:28 -0600374
375 run_command_list(s, -1, 0);
376
Simon Glass5ec35ff2019-07-20 20:51:28 -0600377 if (lock)
378 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass66ded172014-04-10 20:01:28 -0600379 }
380
Simon Glassd915ad22019-07-20 20:51:23 -0600381 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
382 menukey == AUTOBOOT_MENUKEY) {
Simon Glass00caae62017-08-03 12:22:12 -0600383 s = env_get("menucmd");
Simon Glass66ded172014-04-10 20:01:28 -0600384 if (s)
385 run_command_list(s, -1, 0);
386 }
Simon Glass66ded172014-04-10 20:01:28 -0600387}