blob: 4ea9be6da9e626a91063c483742607d67a84518c [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 Glass336d4612020-02-03 07:36:16 -070016#include <malloc.h>
Heiko Schocherecaae802019-07-29 07:23:19 +020017#include <memalign.h>
Simon Glass66ded172014-04-10 20:01:28 -060018#include <menu.h>
19#include <post.h>
Simon Glass10453152019-11-14 12:57:30 -070020#include <time.h>
Stefan Roese8f0b1e22015-05-18 14:08:24 +020021#include <u-boot/sha256.h>
Lukasz Majewskibc8c4402018-05-02 16:10:53 +020022#include <bootcount.h>
Simon Glass66ded172014-04-10 20:01:28 -060023
24DECLARE_GLOBAL_DATA_PTR;
25
26#define MAX_DELAY_STOP_STR 32
27
28#ifndef DEBUG_BOOTKEYS
29#define DEBUG_BOOTKEYS 0
30#endif
31#define debug_bootkeys(fmt, args...) \
32 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
33
Simon Glassaffb2152014-04-10 20:01:35 -060034/* Stored value of bootdelay, used by autoboot_command() */
35static int stored_bootdelay;
Simon Glassd915ad22019-07-20 20:51:23 -060036static int menukey;
Simon Glassaffb2152014-04-10 20:01:35 -060037
Simon Glass0c4bd312019-07-20 20:51:15 -060038#ifdef CONFIG_AUTOBOOT_ENCRYPTION
39#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
40#else
41#define AUTOBOOT_STOP_STR_SHA256 ""
42#endif
43
Simon Glassd915ad22019-07-20 20:51:23 -060044#ifdef CONFIG_USE_AUTOBOOT_MENUKEY
45#define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
46#else
47#define AUTOBOOT_MENUKEY 0
48#endif
49
Stefan Roese8f0b1e22015-05-18 14:08:24 +020050/*
51 * Use a "constant-length" time compare function for this
52 * hash compare:
53 *
54 * https://crackstation.net/hashing-security.htm
Simon Glass66ded172014-04-10 20:01:28 -060055 */
Stefan Roese8f0b1e22015-05-18 14:08:24 +020056static int slow_equals(u8 *a, u8 *b, int len)
57{
58 int diff = 0;
59 int i;
60
61 for (i = 0; i < len; i++)
62 diff |= a[i] ^ b[i];
63
64 return diff == 0;
65}
66
Simon Glass88fa4be2019-07-20 20:51:17 -060067/**
68 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
69 *
70 * This checks for the user entering a SHA256 hash within a given time.
71 *
72 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
73 * @return 0 if autoboot should continue, 1 if it should stop
74 */
Simon Glasse8c78052019-07-20 20:51:16 -060075static int passwd_abort_sha256(uint64_t etime)
Stefan Roese8f0b1e22015-05-18 14:08:24 +020076{
Simon Glass00caae62017-08-03 12:22:12 -060077 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese8f0b1e22015-05-18 14:08:24 +020078 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocherecaae802019-07-29 07:23:19 +020079 u8 *sha;
80 char *presskey;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020081 const char *algo_name = "sha256";
82 u_int presskey_len = 0;
83 int abort = 0;
Martin Etnestad2d06fd82018-01-12 09:04:38 +010084 int size = sizeof(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +020085 int ret;
86
87 if (sha_env_str == NULL)
Simon Glass0c4bd312019-07-20 20:51:15 -060088 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020089
90 /*
91 * Generate the binary value from the environment hash value
92 * so that we can compare this value with the computed hash
93 * from the user input
94 */
95 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
96 if (ret) {
97 printf("Hash %s not supported!\n", algo_name);
98 return 0;
99 }
100
Heiko Schocherecaae802019-07-29 07:23:19 +0200101 presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
102 sha = malloc_cache_aligned(SHA256_SUM_LEN);
103 size = SHA256_SUM_LEN;
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200104 /*
105 * We don't know how long the stop-string is, so we need to
106 * generate the sha256 hash upon each input character and
107 * compare the value with the one saved in the environment
108 */
109 do {
110 if (tstc()) {
111 /* Check for input string overflow */
Heiko Schocherecaae802019-07-29 07:23:19 +0200112 if (presskey_len >= MAX_DELAY_STOP_STR) {
113 free(presskey);
114 free(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200115 return 0;
Heiko Schocherecaae802019-07-29 07:23:19 +0200116 }
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200117
118 presskey[presskey_len++] = getc();
119
120 /* Calculate sha256 upon each new char */
121 hash_block(algo_name, (const void *)presskey,
122 presskey_len, sha, &size);
123
124 /* And check if sha matches saved value in env */
125 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
126 abort = 1;
127 }
128 } while (!abort && get_ticks() <= etime);
129
Heiko Schocherecaae802019-07-29 07:23:19 +0200130 free(presskey);
131 free(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200132 return abort;
133}
Simon Glasse8c78052019-07-20 20:51:16 -0600134
Simon Glass88fa4be2019-07-20 20:51:17 -0600135/**
136 * passwd_abort_key() - check for a key sequence to aborted booting
137 *
138 * This checks for the user entering a string within a given time.
139 *
140 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
141 * @return 0 if autoboot should continue, 1 if it should stop
142 */
Simon Glasse8c78052019-07-20 20:51:16 -0600143static int passwd_abort_key(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600144{
145 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600146 struct {
147 char *str;
148 u_int len;
149 int retry;
150 }
151 delaykey[] = {
Simon Glass00caae62017-08-03 12:22:12 -0600152 { .str = env_get("bootdelaykey"), .retry = 1 },
153 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600154 };
155
156 char presskey[MAX_DELAY_STOP_STR];
157 u_int presskey_len = 0;
158 u_int presskey_max = 0;
159 u_int i;
160
Simon Glass66ded172014-04-10 20:01:28 -0600161# ifdef CONFIG_AUTOBOOT_DELAY_STR
162 if (delaykey[0].str == NULL)
163 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
164# endif
Simon Glass66ded172014-04-10 20:01:28 -0600165# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200166 if (delaykey[1].str == NULL)
167 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600168# endif
169
170 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
171 delaykey[i].len = delaykey[i].str == NULL ?
172 0 : strlen(delaykey[i].str);
173 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
174 MAX_DELAY_STOP_STR : delaykey[i].len;
175
176 presskey_max = presskey_max > delaykey[i].len ?
177 presskey_max : delaykey[i].len;
178
179 debug_bootkeys("%s key:<%s>\n",
180 delaykey[i].retry ? "delay" : "stop",
181 delaykey[i].str ? delaykey[i].str : "NULL");
182 }
183
184 /* In order to keep up with incoming data, check timeout only
185 * when catch up.
186 */
187 do {
188 if (tstc()) {
189 if (presskey_len < presskey_max) {
190 presskey[presskey_len++] = getc();
191 } else {
192 for (i = 0; i < presskey_max - 1; i++)
193 presskey[i] = presskey[i + 1];
194
195 presskey[i] = getc();
196 }
197 }
198
199 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
200 if (delaykey[i].len > 0 &&
201 presskey_len >= delaykey[i].len &&
202 memcmp(presskey + presskey_len -
203 delaykey[i].len, delaykey[i].str,
204 delaykey[i].len) == 0) {
205 debug_bootkeys("got %skey\n",
206 delaykey[i].retry ? "delay" :
207 "stop");
208
Simon Glass66ded172014-04-10 20:01:28 -0600209 /* don't retry auto boot */
210 if (!delaykey[i].retry)
211 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600212 abort = 1;
213 }
214 }
215 } while (!abort && get_ticks() <= etime);
216
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200217 return abort;
218}
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200219
220/***************************************************************************
221 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
222 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
223 */
Simon Glasse79e4b22019-07-20 20:51:19 -0600224static int abortboot_key_sequence(int bootdelay)
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200225{
226 int abort;
227 uint64_t etime = endtick(bootdelay);
228
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200229# ifdef CONFIG_AUTOBOOT_PROMPT
230 /*
231 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
232 * To print the bootdelay value upon bootup.
233 */
234 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
235# endif
236
Simon Glasse8c78052019-07-20 20:51:16 -0600237 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
238 abort = passwd_abort_sha256(etime);
239 else
240 abort = passwd_abort_key(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600241 if (!abort)
242 debug_bootkeys("key timeout\n");
243
Simon Glass66ded172014-04-10 20:01:28 -0600244 return abort;
245}
246
Simon Glasse79e4b22019-07-20 20:51:19 -0600247static int abortboot_single_key(int bootdelay)
Simon Glass66ded172014-04-10 20:01:28 -0600248{
249 int abort = 0;
250 unsigned long ts;
251
Masahiro Yamada46327392016-06-27 16:23:04 +0900252 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600253
Simon Glass66ded172014-04-10 20:01:28 -0600254 /*
255 * Check if key already pressed
Simon Glass66ded172014-04-10 20:01:28 -0600256 */
Masahiro Yamada46327392016-06-27 16:23:04 +0900257 if (tstc()) { /* we got a key press */
258 (void) getc(); /* consume input */
259 puts("\b\b\b 0");
260 abort = 1; /* don't auto boot */
Simon Glass66ded172014-04-10 20:01:28 -0600261 }
Simon Glass66ded172014-04-10 20:01:28 -0600262
263 while ((bootdelay > 0) && (!abort)) {
264 --bootdelay;
265 /* delay 1000 ms */
266 ts = get_timer(0);
267 do {
268 if (tstc()) { /* we got a key press */
Simon Glassd915ad22019-07-20 20:51:23 -0600269 int key;
270
Simon Glass66ded172014-04-10 20:01:28 -0600271 abort = 1; /* don't auto boot */
272 bootdelay = 0; /* no more delay */
Simon Glassd915ad22019-07-20 20:51:23 -0600273 key = getc(); /* consume input */
274 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
275 menukey = key;
Simon Glass66ded172014-04-10 20:01:28 -0600276 break;
277 }
278 udelay(10000);
279 } while (!abort && get_timer(ts) < 1000);
280
281 printf("\b\b\b%2d ", bootdelay);
282 }
283
284 putc('\n');
285
Simon Glass66ded172014-04-10 20:01:28 -0600286 return abort;
287}
Simon Glass66ded172014-04-10 20:01:28 -0600288
289static int abortboot(int bootdelay)
290{
Masahiro Yamada46327392016-06-27 16:23:04 +0900291 int abort = 0;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900292
Simon Glasse79e4b22019-07-20 20:51:19 -0600293 if (bootdelay >= 0) {
294 if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
295 abort = abortboot_key_sequence(bootdelay);
296 else
297 abort = abortboot_single_key(bootdelay);
298 }
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900299
Simon Glass42b4d142019-07-20 20:51:18 -0600300 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900301 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900302
303 return abort;
Simon Glass66ded172014-04-10 20:01:28 -0600304}
305
Simon Glass66ded172014-04-10 20:01:28 -0600306static void process_fdt_options(const void *blob)
307{
Simon Glass5fa3fd22019-07-20 20:51:27 -0600308#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass66ded172014-04-10 20:01:28 -0600309 ulong addr;
310
311 /* Add an env variable to point to a kernel payload, if available */
312 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
313 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600314 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600315
316 /* Add an env variable to point to a root disk, if available */
317 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
318 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600319 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass5fa3fd22019-07-20 20:51:27 -0600320#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glassaffb2152014-04-10 20:01:35 -0600321}
Simon Glass66ded172014-04-10 20:01:28 -0600322
Simon Glassaffb2152014-04-10 20:01:35 -0600323const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600324{
Simon Glass66ded172014-04-10 20:01:28 -0600325 char *s;
326 int bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600327
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200328 bootcount_inc();
Simon Glass66ded172014-04-10 20:01:28 -0600329
Simon Glass00caae62017-08-03 12:22:12 -0600330 s = env_get("bootdelay");
Simon Glass66ded172014-04-10 20:01:28 -0600331 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
332
Simon Glass5fa3fd22019-07-20 20:51:27 -0600333 if (IS_ENABLED(CONFIG_OF_CONTROL))
334 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
335 bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600336
337 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
338
Simon Glass5fa3fd22019-07-20 20:51:27 -0600339 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
340 bootdelay = menu_show(bootdelay);
Simon Glassb26440f2014-04-10 20:01:31 -0600341 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600342
343#ifdef CONFIG_POST
344 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass00caae62017-08-03 12:22:12 -0600345 s = env_get("failbootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600346 } else
347#endif /* CONFIG_POST */
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200348 if (bootcount_error())
Simon Glass00caae62017-08-03 12:22:12 -0600349 s = env_get("altbootcmd");
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200350 else
Simon Glass00caae62017-08-03 12:22:12 -0600351 s = env_get("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600352
Simon Glass5fa3fd22019-07-20 20:51:27 -0600353 if (IS_ENABLED(CONFIG_OF_CONTROL))
354 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600355 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600356
Simon Glassaffb2152014-04-10 20:01:35 -0600357 return s;
358}
Simon Glass66ded172014-04-10 20:01:28 -0600359
Simon Glassaffb2152014-04-10 20:01:35 -0600360void autoboot_command(const char *s)
361{
Simon Glass66ded172014-04-10 20:01:28 -0600362 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
363
Simon Glassaffb2152014-04-10 20:01:35 -0600364 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass5ec35ff2019-07-20 20:51:28 -0600365 bool lock;
366 int prev;
367
368 lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
369 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
370 if (lock)
371 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass66ded172014-04-10 20:01:28 -0600372
373 run_command_list(s, -1, 0);
374
Simon Glass5ec35ff2019-07-20 20:51:28 -0600375 if (lock)
376 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass66ded172014-04-10 20:01:28 -0600377 }
378
Simon Glassd915ad22019-07-20 20:51:23 -0600379 if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
380 menukey == AUTOBOOT_MENUKEY) {
Simon Glass00caae62017-08-03 12:22:12 -0600381 s = env_get("menucmd");
Simon Glass66ded172014-04-10 20:01:28 -0600382 if (s)
383 run_command_list(s, -1, 0);
384 }
Simon Glass66ded172014-04-10 20:01:28 -0600385}