blob: b025fd99a0bf6fbc9d3c70b34b7898aef04878df [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
Joel Peshkin652b5042020-11-21 17:18:59 -080028#define MAX_DELAY_STOP_STR 64
Simon Glass66ded172014-04-10 20:01:28 -060029
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;
Joel Peshkin652b5042020-11-21 17:18:59 -080083 char *c;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020084 const char *algo_name = "sha256";
85 u_int presskey_len = 0;
86 int abort = 0;
Martin Etnestad2d06fd82018-01-12 09:04:38 +010087 int size = sizeof(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +020088 int ret;
89
90 if (sha_env_str == NULL)
Simon Glass0c4bd312019-07-20 20:51:15 -060091 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020092
Joel Peshkin652b5042020-11-21 17:18:59 -080093 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
94 c = strstr(sha_env_str, ":");
95 if (c && (c - sha_env_str < MAX_DELAY_STOP_STR)) {
96 /* preload presskey with salt */
97 memcpy(presskey, sha_env_str, c - sha_env_str);
98 presskey_len = c - sha_env_str;
99 sha_env_str = c + 1;
100 }
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200101 /*
102 * Generate the binary value from the environment hash value
103 * so that we can compare this value with the computed hash
104 * from the user input
105 */
106 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
107 if (ret) {
108 printf("Hash %s not supported!\n", algo_name);
109 return 0;
110 }
111
Heiko Schocherecaae802019-07-29 07:23:19 +0200112 sha = malloc_cache_aligned(SHA256_SUM_LEN);
113 size = SHA256_SUM_LEN;
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200114 /*
115 * We don't know how long the stop-string is, so we need to
116 * generate the sha256 hash upon each input character and
117 * compare the value with the one saved in the environment
118 */
119 do {
120 if (tstc()) {
121 /* Check for input string overflow */
Heiko Schocherecaae802019-07-29 07:23:19 +0200122 if (presskey_len >= MAX_DELAY_STOP_STR) {
123 free(presskey);
124 free(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200125 return 0;
Heiko Schocherecaae802019-07-29 07:23:19 +0200126 }
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200127
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200128 presskey[presskey_len++] = getchar();
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200129
130 /* Calculate sha256 upon each new char */
131 hash_block(algo_name, (const void *)presskey,
132 presskey_len, sha, &size);
133
134 /* And check if sha matches saved value in env */
135 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
136 abort = 1;
137 }
138 } while (!abort && get_ticks() <= etime);
139
Heiko Schocherecaae802019-07-29 07:23:19 +0200140 free(presskey);
141 free(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200142 return abort;
143}
Simon Glasse8c78052019-07-20 20:51:16 -0600144
Simon Glass88fa4be2019-07-20 20:51:17 -0600145/**
146 * passwd_abort_key() - check for a key sequence to aborted booting
147 *
148 * This checks for the user entering a string within a given time.
149 *
150 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
151 * @return 0 if autoboot should continue, 1 if it should stop
152 */
Simon Glasse8c78052019-07-20 20:51:16 -0600153static int passwd_abort_key(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600154{
155 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600156 struct {
157 char *str;
158 u_int len;
159 int retry;
160 }
161 delaykey[] = {
Simon Glass00caae62017-08-03 12:22:12 -0600162 { .str = env_get("bootdelaykey"), .retry = 1 },
163 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600164 };
165
166 char presskey[MAX_DELAY_STOP_STR];
Yuezhang.Mo@sony.come088f0c2021-01-15 03:11:49 +0000167 int presskey_len = 0;
168 int presskey_max = 0;
169 int i;
Simon Glass66ded172014-04-10 20:01:28 -0600170
Simon Glass66ded172014-04-10 20:01:28 -0600171# ifdef CONFIG_AUTOBOOT_DELAY_STR
172 if (delaykey[0].str == NULL)
173 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
174# endif
Simon Glass66ded172014-04-10 20:01:28 -0600175# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200176 if (delaykey[1].str == NULL)
177 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600178# endif
179
180 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
181 delaykey[i].len = delaykey[i].str == NULL ?
182 0 : strlen(delaykey[i].str);
183 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
184 MAX_DELAY_STOP_STR : delaykey[i].len;
185
186 presskey_max = presskey_max > delaykey[i].len ?
187 presskey_max : delaykey[i].len;
188
189 debug_bootkeys("%s key:<%s>\n",
190 delaykey[i].retry ? "delay" : "stop",
191 delaykey[i].str ? delaykey[i].str : "NULL");
192 }
193
194 /* In order to keep up with incoming data, check timeout only
195 * when catch up.
196 */
197 do {
198 if (tstc()) {
199 if (presskey_len < presskey_max) {
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200200 presskey[presskey_len++] = getchar();
Simon Glass66ded172014-04-10 20:01:28 -0600201 } else {
202 for (i = 0; i < presskey_max - 1; i++)
203 presskey[i] = presskey[i + 1];
204
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200205 presskey[i] = getchar();
Simon Glass66ded172014-04-10 20:01:28 -0600206 }
207 }
208
209 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
210 if (delaykey[i].len > 0 &&
211 presskey_len >= delaykey[i].len &&
212 memcmp(presskey + presskey_len -
213 delaykey[i].len, delaykey[i].str,
214 delaykey[i].len) == 0) {
215 debug_bootkeys("got %skey\n",
216 delaykey[i].retry ? "delay" :
217 "stop");
218
Simon Glass66ded172014-04-10 20:01:28 -0600219 /* don't retry auto boot */
220 if (!delaykey[i].retry)
221 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600222 abort = 1;
223 }
224 }
225 } while (!abort && get_ticks() <= etime);
226
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200227 return abort;
228}
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200229
230/***************************************************************************
231 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
232 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
233 */
Simon Glasse79e4b22019-07-20 20:51:19 -0600234static int abortboot_key_sequence(int bootdelay)
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200235{
236 int abort;
237 uint64_t etime = endtick(bootdelay);
238
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200239# ifdef CONFIG_AUTOBOOT_PROMPT
240 /*
241 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
242 * To print the bootdelay value upon bootup.
243 */
244 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
245# endif
246
Simon Glasse8c78052019-07-20 20:51:16 -0600247 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
248 abort = passwd_abort_sha256(etime);
249 else
250 abort = passwd_abort_key(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600251 if (!abort)
252 debug_bootkeys("key timeout\n");
253
Simon Glass66ded172014-04-10 20:01:28 -0600254 return abort;
255}
256
Simon Glasse79e4b22019-07-20 20:51:19 -0600257static int abortboot_single_key(int bootdelay)
Simon Glass66ded172014-04-10 20:01:28 -0600258{
259 int abort = 0;
260 unsigned long ts;
261
Masahiro Yamada46327392016-06-27 16:23:04 +0900262 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600263
Simon Glass66ded172014-04-10 20:01:28 -0600264 /*
265 * Check if key already pressed
Simon Glass66ded172014-04-10 20:01:28 -0600266 */
Masahiro Yamada46327392016-06-27 16:23:04 +0900267 if (tstc()) { /* we got a key press */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200268 getchar(); /* consume input */
Masahiro Yamada46327392016-06-27 16:23:04 +0900269 puts("\b\b\b 0");
270 abort = 1; /* don't auto boot */
Simon Glass66ded172014-04-10 20:01:28 -0600271 }
Simon Glass66ded172014-04-10 20:01:28 -0600272
273 while ((bootdelay > 0) && (!abort)) {
274 --bootdelay;
275 /* delay 1000 ms */
276 ts = get_timer(0);
277 do {
278 if (tstc()) { /* we got a key press */
Simon Glassd915ad22019-07-20 20:51:23 -0600279 int key;
280
Simon Glass66ded172014-04-10 20:01:28 -0600281 abort = 1; /* don't auto boot */
282 bootdelay = 0; /* no more delay */
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200283 key = getchar();/* consume input */
Simon Glassd915ad22019-07-20 20:51:23 -0600284 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
285 menukey = key;
Simon Glass66ded172014-04-10 20:01:28 -0600286 break;
287 }
288 udelay(10000);
289 } while (!abort && get_timer(ts) < 1000);
290
291 printf("\b\b\b%2d ", bootdelay);
292 }
293
294 putc('\n');
295
Simon Glass66ded172014-04-10 20:01:28 -0600296 return abort;
297}
Simon Glass66ded172014-04-10 20:01:28 -0600298
299static int abortboot(int bootdelay)
300{
Masahiro Yamada46327392016-06-27 16:23:04 +0900301 int abort = 0;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900302
Simon Glasse79e4b22019-07-20 20:51:19 -0600303 if (bootdelay >= 0) {
304 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
305 abort = abortboot_key_sequence(bootdelay);
306 else
307 abort = abortboot_single_key(bootdelay);
308 }
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900309
Simon Glass42b4d142019-07-20 20:51:18 -0600310 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900311 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900312
313 return abort;
Simon Glass66ded172014-04-10 20:01:28 -0600314}
315
Simon Glass66ded172014-04-10 20:01:28 -0600316static void process_fdt_options(const void *blob)
317{
Simon Glass5fa3fd22019-07-20 20:51:27 -0600318#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass66ded172014-04-10 20:01:28 -0600319 ulong addr;
320
321 /* Add an env variable to point to a kernel payload, if available */
322 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
323 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600324 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600325
326 /* Add an env variable to point to a root disk, if available */
327 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
328 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600329 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass5fa3fd22019-07-20 20:51:27 -0600330#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glassaffb2152014-04-10 20:01:35 -0600331}
Simon Glass66ded172014-04-10 20:01:28 -0600332
Simon Glassaffb2152014-04-10 20:01:35 -0600333const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600334{
Simon Glass66ded172014-04-10 20:01:28 -0600335 char *s;
336 int bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600337
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200338 bootcount_inc();
Simon Glass66ded172014-04-10 20:01:28 -0600339
Simon Glass00caae62017-08-03 12:22:12 -0600340 s = env_get("bootdelay");
Simon Glass66ded172014-04-10 20:01:28 -0600341 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
342
Simon Glass5fa3fd22019-07-20 20:51:27 -0600343 if (IS_ENABLED(CONFIG_OF_CONTROL))
344 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
345 bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600346
347 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
348
Simon Glass5fa3fd22019-07-20 20:51:27 -0600349 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
350 bootdelay = menu_show(bootdelay);
Simon Glassb26440f2014-04-10 20:01:31 -0600351 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600352
353#ifdef CONFIG_POST
354 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass00caae62017-08-03 12:22:12 -0600355 s = env_get("failbootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600356 } else
357#endif /* CONFIG_POST */
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200358 if (bootcount_error())
Simon Glass00caae62017-08-03 12:22:12 -0600359 s = env_get("altbootcmd");
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200360 else
Simon Glass00caae62017-08-03 12:22:12 -0600361 s = env_get("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600362
Simon Glass5fa3fd22019-07-20 20:51:27 -0600363 if (IS_ENABLED(CONFIG_OF_CONTROL))
364 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600365 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600366
Simon Glassaffb2152014-04-10 20:01:35 -0600367 return s;
368}
Simon Glass66ded172014-04-10 20:01:28 -0600369
Simon Glassaffb2152014-04-10 20:01:35 -0600370void autoboot_command(const char *s)
371{
Simon Glass66ded172014-04-10 20:01:28 -0600372 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
373
Heiko Schocher6ebe6b32020-10-07 08:06:54 +0200374 if (s && (stored_bootdelay == -2 ||
375 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
Simon Glass5ec35ff2019-07-20 20:51:28 -0600376 bool lock;
377 int prev;
378
379 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
380 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
381 if (lock)
382 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass66ded172014-04-10 20:01:28 -0600383
384 run_command_list(s, -1, 0);
385
Simon Glass5ec35ff2019-07-20 20:51:28 -0600386 if (lock)
387 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass66ded172014-04-10 20:01:28 -0600388 }
389
Simon Glassd915ad22019-07-20 20:51:23 -0600390 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
391 menukey == AUTOBOOT_MENUKEY) {
Simon Glass00caae62017-08-03 12:22:12 -0600392 s = env_get("menucmd");
Simon Glass66ded172014-04-10 20:01:28 -0600393 if (s)
394 run_command_list(s, -1, 0);
395 }
Simon Glass66ded172014-04-10 20:01:28 -0600396}