blob: 94a1b4abebac941858909b7e89cfd838ee40f5b1 [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 Glassadaaa482019-11-14 12:57:43 -070011#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060013#include <env.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060014#include <fdtdec.h>
Simon Glass6a2e09f2019-07-20 20:51:16 -060015#include <hash.h>
Heiko Schocher2ef3a952019-07-29 07:23:19 +020016#include <memalign.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060017#include <menu.h>
18#include <post.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070019#include <time.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020020#include <u-boot/sha256.h>
Lukasz Majewski4fc18912018-05-02 16:10:53 +020021#include <bootcount.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060022
23DECLARE_GLOBAL_DATA_PTR;
24
25#define MAX_DELAY_STOP_STR 32
26
27#ifndef DEBUG_BOOTKEYS
28#define DEBUG_BOOTKEYS 0
29#endif
30#define debug_bootkeys(fmt, args...) \
31 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
32
Simon Glass5b47e302014-04-10 20:01:35 -060033/* Stored value of bootdelay, used by autoboot_command() */
34static int stored_bootdelay;
Simon Glass9d983852019-07-20 20:51:23 -060035static int menukey;
Simon Glass5b47e302014-04-10 20:01:35 -060036
Simon Glass760d3fb2019-07-20 20:51:15 -060037#ifdef CONFIG_AUTOBOOT_ENCRYPTION
38#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
39#else
40#define AUTOBOOT_STOP_STR_SHA256 ""
41#endif
42
Simon Glass9d983852019-07-20 20:51:23 -060043#ifdef CONFIG_USE_AUTOBOOT_MENUKEY
44#define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
45#else
46#define AUTOBOOT_MENUKEY 0
47#endif
48
Stefan Roese0ed2e462015-05-18 14:08:24 +020049/*
50 * Use a "constant-length" time compare function for this
51 * hash compare:
52 *
53 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -060054 */
Stefan Roese0ed2e462015-05-18 14:08:24 +020055static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -060056{
Stefan Roese0ed2e462015-05-18 14:08:24 +020057 int diff = 0;
58 int i;
59
60 for (i = 0; i < len; i++)
61 diff |= a[i] ^ b[i];
62
63 return diff == 0;
64}
65
Simon Glassa8cab882019-07-20 20:51:17 -060066/**
67 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
68 *
69 * This checks for the user entering a SHA256 hash within a given time.
70 *
71 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
72 * @return 0 if autoboot should continue, 1 if it should stop
73 */
Simon Glass6a2e09f2019-07-20 20:51:16 -060074static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +020075{
Simon Glass64b723f2017-08-03 12:22:12 -060076 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +020077 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +020078 u8 *sha;
79 char *presskey;
Stefan Roese0ed2e462015-05-18 14:08:24 +020080 const char *algo_name = "sha256";
81 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -060082 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +010083 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +020084 int ret;
85
86 if (sha_env_str == NULL)
Simon Glass760d3fb2019-07-20 20:51:15 -060087 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +020088
89 /*
90 * Generate the binary value from the environment hash value
91 * so that we can compare this value with the computed hash
92 * from the user input
93 */
94 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
95 if (ret) {
96 printf("Hash %s not supported!\n", algo_name);
97 return 0;
98 }
99
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200100 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
101 sha = malloc_cache_aligned(SHA256_SUM_LEN);
102 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200103 /*
104 * We don't know how long the stop-string is, so we need to
105 * generate the sha256 hash upon each input character and
106 * compare the value with the one saved in the environment
107 */
108 do {
109 if (tstc()) {
110 /* Check for input string overflow */
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200111 if (presskey_len >= MAX_DELAY_STOP_STR) {
112 free(presskey);
113 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200114 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200115 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200116
117 presskey[presskey_len++] = getc();
118
119 /* Calculate sha256 upon each new char */
120 hash_block(algo_name, (const void *)presskey,
121 presskey_len, sha, &size);
122
123 /* And check if sha matches saved value in env */
124 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
125 abort = 1;
126 }
127 } while (!abort && get_ticks() <= etime);
128
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200129 free(presskey);
130 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200131 return abort;
132}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600133
Simon Glassa8cab882019-07-20 20:51:17 -0600134/**
135 * passwd_abort_key() - check for a key sequence to aborted booting
136 *
137 * This checks for the user entering a string within a given time.
138 *
139 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
140 * @return 0 if autoboot should continue, 1 if it should stop
141 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600142static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200143{
144 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600145 struct {
146 char *str;
147 u_int len;
148 int retry;
149 }
150 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600151 { .str = env_get("bootdelaykey"), .retry = 1 },
152 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600153 };
154
155 char presskey[MAX_DELAY_STOP_STR];
156 u_int presskey_len = 0;
157 u_int presskey_max = 0;
158 u_int i;
159
Simon Glass7b6a95a2014-04-10 20:01:28 -0600160# ifdef CONFIG_AUTOBOOT_DELAY_STR
161 if (delaykey[0].str == NULL)
162 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
163# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600164# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200165 if (delaykey[1].str == NULL)
166 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600167# endif
168
169 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
170 delaykey[i].len = delaykey[i].str == NULL ?
171 0 : strlen(delaykey[i].str);
172 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
173 MAX_DELAY_STOP_STR : delaykey[i].len;
174
175 presskey_max = presskey_max > delaykey[i].len ?
176 presskey_max : delaykey[i].len;
177
178 debug_bootkeys("%s key:<%s>\n",
179 delaykey[i].retry ? "delay" : "stop",
180 delaykey[i].str ? delaykey[i].str : "NULL");
181 }
182
183 /* In order to keep up with incoming data, check timeout only
184 * when catch up.
185 */
186 do {
187 if (tstc()) {
188 if (presskey_len < presskey_max) {
189 presskey[presskey_len++] = getc();
190 } else {
191 for (i = 0; i < presskey_max - 1; i++)
192 presskey[i] = presskey[i + 1];
193
194 presskey[i] = getc();
195 }
196 }
197
198 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
199 if (delaykey[i].len > 0 &&
200 presskey_len >= delaykey[i].len &&
201 memcmp(presskey + presskey_len -
202 delaykey[i].len, delaykey[i].str,
203 delaykey[i].len) == 0) {
204 debug_bootkeys("got %skey\n",
205 delaykey[i].retry ? "delay" :
206 "stop");
207
Simon Glass7b6a95a2014-04-10 20:01:28 -0600208 /* don't retry auto boot */
209 if (!delaykey[i].retry)
210 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600211 abort = 1;
212 }
213 }
214 } while (!abort && get_ticks() <= etime);
215
Stefan Roese0ed2e462015-05-18 14:08:24 +0200216 return abort;
217}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200218
219/***************************************************************************
220 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
221 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
222 */
Simon Glass6d99d132019-07-20 20:51:19 -0600223static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200224{
225 int abort;
226 uint64_t etime = endtick(bootdelay);
227
Stefan Roese0ed2e462015-05-18 14:08:24 +0200228# ifdef CONFIG_AUTOBOOT_PROMPT
229 /*
230 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
231 * To print the bootdelay value upon bootup.
232 */
233 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
234# endif
235
Simon Glass6a2e09f2019-07-20 20:51:16 -0600236 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
237 abort = passwd_abort_sha256(etime);
238 else
239 abort = passwd_abort_key(etime);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600240 if (!abort)
241 debug_bootkeys("key timeout\n");
242
Simon Glass7b6a95a2014-04-10 20:01:28 -0600243 return abort;
244}
245
Simon Glass6d99d132019-07-20 20:51:19 -0600246static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600247{
248 int abort = 0;
249 unsigned long ts;
250
Masahiro Yamada9a333862016-06-27 16:23:04 +0900251 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600252
Simon Glass7b6a95a2014-04-10 20:01:28 -0600253 /*
254 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600255 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900256 if (tstc()) { /* we got a key press */
257 (void) getc(); /* consume input */
258 puts("\b\b\b 0");
259 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600260 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600261
262 while ((bootdelay > 0) && (!abort)) {
263 --bootdelay;
264 /* delay 1000 ms */
265 ts = get_timer(0);
266 do {
267 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600268 int key;
269
Simon Glass7b6a95a2014-04-10 20:01:28 -0600270 abort = 1; /* don't auto boot */
271 bootdelay = 0; /* no more delay */
Simon Glass9d983852019-07-20 20:51:23 -0600272 key = getc(); /* consume input */
273 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
274 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600275 break;
276 }
277 udelay(10000);
278 } while (!abort && get_timer(ts) < 1000);
279
280 printf("\b\b\b%2d ", bootdelay);
281 }
282
283 putc('\n');
284
Simon Glass7b6a95a2014-04-10 20:01:28 -0600285 return abort;
286}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600287
288static int abortboot(int bootdelay)
289{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900290 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900291
Simon Glass6d99d132019-07-20 20:51:19 -0600292 if (bootdelay >= 0) {
293 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
294 abort = abortboot_key_sequence(bootdelay);
295 else
296 abort = abortboot_single_key(bootdelay);
297 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900298
Simon Glass3a315052019-07-20 20:51:18 -0600299 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900300 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900301
302 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600303}
304
Simon Glass7b6a95a2014-04-10 20:01:28 -0600305static void process_fdt_options(const void *blob)
306{
Simon Glassb97ace52019-07-20 20:51:27 -0600307#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600308 ulong addr;
309
310 /* Add an env variable to point to a kernel payload, if available */
311 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
312 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600313 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600314
315 /* Add an env variable to point to a root disk, if available */
316 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
317 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600318 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glassb97ace52019-07-20 20:51:27 -0600319#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600320}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600321
Simon Glass5b47e302014-04-10 20:01:35 -0600322const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600323{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600324 char *s;
325 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600326
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200327 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600328
Simon Glass64b723f2017-08-03 12:22:12 -0600329 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600330 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
331
Simon Glassb97ace52019-07-20 20:51:27 -0600332 if (IS_ENABLED(CONFIG_OF_CONTROL))
333 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
334 bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600335
336 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
337
Simon Glassb97ace52019-07-20 20:51:27 -0600338 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
339 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600340 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600341
342#ifdef CONFIG_POST
343 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600344 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600345 } else
346#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200347 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600348 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200349 else
Simon Glass64b723f2017-08-03 12:22:12 -0600350 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600351
Simon Glassb97ace52019-07-20 20:51:27 -0600352 if (IS_ENABLED(CONFIG_OF_CONTROL))
353 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600354 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600355
Simon Glass5b47e302014-04-10 20:01:35 -0600356 return s;
357}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600358
Simon Glass5b47e302014-04-10 20:01:35 -0600359void autoboot_command(const char *s)
360{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600361 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
362
Simon Glass5b47e302014-04-10 20:01:35 -0600363 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600364 bool lock;
365 int prev;
366
367 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
368 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
369 if (lock)
370 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600371
372 run_command_list(s, -1, 0);
373
Simon Glass313fcbd2019-07-20 20:51:28 -0600374 if (lock)
375 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600376 }
377
Simon Glass9d983852019-07-20 20:51:23 -0600378 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
379 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600380 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600381 if (s)
382 run_command_list(s, -1, 0);
383 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600384}