blob: 7a91736a00fc200007aec2fa27ac528885177e44 [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>
Heiko Schocher2ef3a952019-07-29 07:23:19 +020014#include <memalign.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060015#include <menu.h>
16#include <post.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020017#include <u-boot/sha256.h>
Lukasz Majewski4fc18912018-05-02 16:10:53 +020018#include <bootcount.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060019
20DECLARE_GLOBAL_DATA_PTR;
21
22#define MAX_DELAY_STOP_STR 32
23
24#ifndef DEBUG_BOOTKEYS
25#define DEBUG_BOOTKEYS 0
26#endif
27#define debug_bootkeys(fmt, args...) \
28 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
29
Simon Glass5b47e302014-04-10 20:01:35 -060030/* Stored value of bootdelay, used by autoboot_command() */
31static int stored_bootdelay;
Simon Glass9d983852019-07-20 20:51:23 -060032static int menukey;
Simon Glass5b47e302014-04-10 20:01:35 -060033
Simon Glass760d3fb2019-07-20 20:51:15 -060034#ifdef CONFIG_AUTOBOOT_ENCRYPTION
35#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
36#else
37#define AUTOBOOT_STOP_STR_SHA256 ""
38#endif
39
Simon Glass9d983852019-07-20 20:51:23 -060040#ifdef CONFIG_USE_AUTOBOOT_MENUKEY
41#define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
42#else
43#define AUTOBOOT_MENUKEY 0
44#endif
45
Stefan Roese0ed2e462015-05-18 14:08:24 +020046/*
47 * Use a "constant-length" time compare function for this
48 * hash compare:
49 *
50 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -060051 */
Stefan Roese0ed2e462015-05-18 14:08:24 +020052static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -060053{
Stefan Roese0ed2e462015-05-18 14:08:24 +020054 int diff = 0;
55 int i;
56
57 for (i = 0; i < len; i++)
58 diff |= a[i] ^ b[i];
59
60 return diff == 0;
61}
62
Simon Glassa8cab882019-07-20 20:51:17 -060063/**
64 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
65 *
66 * This checks for the user entering a SHA256 hash within a given time.
67 *
68 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
69 * @return 0 if autoboot should continue, 1 if it should stop
70 */
Simon Glass6a2e09f2019-07-20 20:51:16 -060071static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +020072{
Simon Glass64b723f2017-08-03 12:22:12 -060073 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +020074 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +020075 u8 *sha;
76 char *presskey;
Stefan Roese0ed2e462015-05-18 14:08:24 +020077 const char *algo_name = "sha256";
78 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -060079 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +010080 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +020081 int ret;
82
83 if (sha_env_str == NULL)
Simon Glass760d3fb2019-07-20 20:51:15 -060084 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +020085
86 /*
87 * Generate the binary value from the environment hash value
88 * so that we can compare this value with the computed hash
89 * from the user input
90 */
91 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
92 if (ret) {
93 printf("Hash %s not supported!\n", algo_name);
94 return 0;
95 }
96
Heiko Schocher2ef3a952019-07-29 07:23:19 +020097 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
98 sha = malloc_cache_aligned(SHA256_SUM_LEN);
99 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200100 /*
101 * We don't know how long the stop-string is, so we need to
102 * generate the sha256 hash upon each input character and
103 * compare the value with the one saved in the environment
104 */
105 do {
106 if (tstc()) {
107 /* Check for input string overflow */
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200108 if (presskey_len >= MAX_DELAY_STOP_STR) {
109 free(presskey);
110 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200111 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200112 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200113
114 presskey[presskey_len++] = getc();
115
116 /* Calculate sha256 upon each new char */
117 hash_block(algo_name, (const void *)presskey,
118 presskey_len, sha, &size);
119
120 /* And check if sha matches saved value in env */
121 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
122 abort = 1;
123 }
124 } while (!abort && get_ticks() <= etime);
125
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200126 free(presskey);
127 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200128 return abort;
129}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600130
Simon Glassa8cab882019-07-20 20:51:17 -0600131/**
132 * passwd_abort_key() - check for a key sequence to aborted booting
133 *
134 * This checks for the user entering a string within a given time.
135 *
136 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
137 * @return 0 if autoboot should continue, 1 if it should stop
138 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600139static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200140{
141 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600142 struct {
143 char *str;
144 u_int len;
145 int retry;
146 }
147 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600148 { .str = env_get("bootdelaykey"), .retry = 1 },
149 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600150 };
151
152 char presskey[MAX_DELAY_STOP_STR];
153 u_int presskey_len = 0;
154 u_int presskey_max = 0;
155 u_int i;
156
Simon Glass7b6a95a2014-04-10 20:01:28 -0600157# ifdef CONFIG_AUTOBOOT_DELAY_STR
158 if (delaykey[0].str == NULL)
159 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
160# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600161# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200162 if (delaykey[1].str == NULL)
163 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600164# endif
165
166 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
167 delaykey[i].len = delaykey[i].str == NULL ?
168 0 : strlen(delaykey[i].str);
169 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
170 MAX_DELAY_STOP_STR : delaykey[i].len;
171
172 presskey_max = presskey_max > delaykey[i].len ?
173 presskey_max : delaykey[i].len;
174
175 debug_bootkeys("%s key:<%s>\n",
176 delaykey[i].retry ? "delay" : "stop",
177 delaykey[i].str ? delaykey[i].str : "NULL");
178 }
179
180 /* In order to keep up with incoming data, check timeout only
181 * when catch up.
182 */
183 do {
184 if (tstc()) {
185 if (presskey_len < presskey_max) {
186 presskey[presskey_len++] = getc();
187 } else {
188 for (i = 0; i < presskey_max - 1; i++)
189 presskey[i] = presskey[i + 1];
190
191 presskey[i] = getc();
192 }
193 }
194
195 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
196 if (delaykey[i].len > 0 &&
197 presskey_len >= delaykey[i].len &&
198 memcmp(presskey + presskey_len -
199 delaykey[i].len, delaykey[i].str,
200 delaykey[i].len) == 0) {
201 debug_bootkeys("got %skey\n",
202 delaykey[i].retry ? "delay" :
203 "stop");
204
Simon Glass7b6a95a2014-04-10 20:01:28 -0600205 /* don't retry auto boot */
206 if (!delaykey[i].retry)
207 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600208 abort = 1;
209 }
210 }
211 } while (!abort && get_ticks() <= etime);
212
Stefan Roese0ed2e462015-05-18 14:08:24 +0200213 return abort;
214}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200215
216/***************************************************************************
217 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
218 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
219 */
Simon Glass6d99d132019-07-20 20:51:19 -0600220static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200221{
222 int abort;
223 uint64_t etime = endtick(bootdelay);
224
Stefan Roese0ed2e462015-05-18 14:08:24 +0200225# ifdef CONFIG_AUTOBOOT_PROMPT
226 /*
227 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
228 * To print the bootdelay value upon bootup.
229 */
230 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
231# endif
232
Simon Glass6a2e09f2019-07-20 20:51:16 -0600233 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
234 abort = passwd_abort_sha256(etime);
235 else
236 abort = passwd_abort_key(etime);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600237 if (!abort)
238 debug_bootkeys("key timeout\n");
239
Simon Glass7b6a95a2014-04-10 20:01:28 -0600240 return abort;
241}
242
Simon Glass6d99d132019-07-20 20:51:19 -0600243static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600244{
245 int abort = 0;
246 unsigned long ts;
247
Masahiro Yamada9a333862016-06-27 16:23:04 +0900248 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600249
Simon Glass7b6a95a2014-04-10 20:01:28 -0600250 /*
251 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600252 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900253 if (tstc()) { /* we got a key press */
254 (void) getc(); /* consume input */
255 puts("\b\b\b 0");
256 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600257 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600258
259 while ((bootdelay > 0) && (!abort)) {
260 --bootdelay;
261 /* delay 1000 ms */
262 ts = get_timer(0);
263 do {
264 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600265 int key;
266
Simon Glass7b6a95a2014-04-10 20:01:28 -0600267 abort = 1; /* don't auto boot */
268 bootdelay = 0; /* no more delay */
Simon Glass9d983852019-07-20 20:51:23 -0600269 key = getc(); /* consume input */
270 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
271 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600272 break;
273 }
274 udelay(10000);
275 } while (!abort && get_timer(ts) < 1000);
276
277 printf("\b\b\b%2d ", bootdelay);
278 }
279
280 putc('\n');
281
Simon Glass7b6a95a2014-04-10 20:01:28 -0600282 return abort;
283}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600284
285static int abortboot(int bootdelay)
286{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900287 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900288
Simon Glass6d99d132019-07-20 20:51:19 -0600289 if (bootdelay >= 0) {
290 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
291 abort = abortboot_key_sequence(bootdelay);
292 else
293 abort = abortboot_single_key(bootdelay);
294 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900295
Simon Glass3a315052019-07-20 20:51:18 -0600296 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900297 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900298
299 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600300}
301
Simon Glass7b6a95a2014-04-10 20:01:28 -0600302static void process_fdt_options(const void *blob)
303{
Simon Glassb97ace52019-07-20 20:51:27 -0600304#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600305 ulong addr;
306
307 /* Add an env variable to point to a kernel payload, if available */
308 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
309 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600310 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600311
312 /* Add an env variable to point to a root disk, if available */
313 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
314 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600315 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glassb97ace52019-07-20 20:51:27 -0600316#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600317}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600318
Simon Glass5b47e302014-04-10 20:01:35 -0600319const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600320{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600321 char *s;
322 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600323
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200324 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600325
Simon Glass64b723f2017-08-03 12:22:12 -0600326 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600327 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
328
Simon Glassb97ace52019-07-20 20:51:27 -0600329 if (IS_ENABLED(CONFIG_OF_CONTROL))
330 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
331 bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600332
333 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
334
Simon Glassb97ace52019-07-20 20:51:27 -0600335 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
336 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600337 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600338
339#ifdef CONFIG_POST
340 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600341 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600342 } else
343#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200344 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600345 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200346 else
Simon Glass64b723f2017-08-03 12:22:12 -0600347 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600348
Simon Glassb97ace52019-07-20 20:51:27 -0600349 if (IS_ENABLED(CONFIG_OF_CONTROL))
350 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600351 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600352
Simon Glass5b47e302014-04-10 20:01:35 -0600353 return s;
354}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600355
Simon Glass5b47e302014-04-10 20:01:35 -0600356void autoboot_command(const char *s)
357{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600358 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
359
Simon Glass5b47e302014-04-10 20:01:35 -0600360 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600361 bool lock;
362 int prev;
363
364 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
365 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
366 if (lock)
367 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600368
369 run_command_list(s, -1, 0);
370
Simon Glass313fcbd2019-07-20 20:51:28 -0600371 if (lock)
372 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600373 }
374
Simon Glass9d983852019-07-20 20:51:23 -0600375 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
376 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600377 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600378 if (s)
379 run_command_list(s, -1, 0);
380 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600381}