blob: f832808b71ed1a3a8b0dab4b66d4d6537a415352 [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 Glass24b852a2015-11-08 23:47:45 -070011#include <console.h>
Simon Glass66ded172014-04-10 20:01:28 -060012#include <fdtdec.h>
Simon Glasse8c78052019-07-20 20:51:16 -060013#include <hash.h>
Simon Glass66ded172014-04-10 20:01:28 -060014#include <menu.h>
15#include <post.h>
Stefan Roese8f0b1e22015-05-18 14:08:24 +020016#include <u-boot/sha256.h>
Lukasz Majewskibc8c4402018-05-02 16:10:53 +020017#include <bootcount.h>
Simon Glass66ded172014-04-10 20:01:28 -060018
19DECLARE_GLOBAL_DATA_PTR;
20
21#define MAX_DELAY_STOP_STR 32
22
23#ifndef DEBUG_BOOTKEYS
24#define DEBUG_BOOTKEYS 0
25#endif
26#define debug_bootkeys(fmt, args...) \
27 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
28
Simon Glassaffb2152014-04-10 20:01:35 -060029/* Stored value of bootdelay, used by autoboot_command() */
30static int stored_bootdelay;
31
Simon Glass0c4bd312019-07-20 20:51:15 -060032#ifdef CONFIG_AUTOBOOT_ENCRYPTION
33#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
34#else
35#define AUTOBOOT_STOP_STR_SHA256 ""
36#endif
37
Stefan Roese8f0b1e22015-05-18 14:08:24 +020038#if defined(CONFIG_AUTOBOOT_KEYED)
Stefan Roese8f0b1e22015-05-18 14:08:24 +020039
40/*
41 * Use a "constant-length" time compare function for this
42 * hash compare:
43 *
44 * https://crackstation.net/hashing-security.htm
Simon Glass66ded172014-04-10 20:01:28 -060045 */
Stefan Roese8f0b1e22015-05-18 14:08:24 +020046static int slow_equals(u8 *a, u8 *b, int len)
47{
48 int diff = 0;
49 int i;
50
51 for (i = 0; i < len; i++)
52 diff |= a[i] ^ b[i];
53
54 return diff == 0;
55}
56
Simon Glass88fa4be2019-07-20 20:51:17 -060057/**
58 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
59 *
60 * This checks for the user entering a SHA256 hash within a given time.
61 *
62 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
63 * @return 0 if autoboot should continue, 1 if it should stop
64 */
Simon Glasse8c78052019-07-20 20:51:16 -060065static int passwd_abort_sha256(uint64_t etime)
Stefan Roese8f0b1e22015-05-18 14:08:24 +020066{
Simon Glass00caae62017-08-03 12:22:12 -060067 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese8f0b1e22015-05-18 14:08:24 +020068 u8 sha_env[SHA256_SUM_LEN];
69 u8 sha[SHA256_SUM_LEN];
70 char presskey[MAX_DELAY_STOP_STR];
71 const char *algo_name = "sha256";
72 u_int presskey_len = 0;
73 int abort = 0;
Martin Etnestad2d06fd82018-01-12 09:04:38 +010074 int size = sizeof(sha);
Stefan Roese8f0b1e22015-05-18 14:08:24 +020075 int ret;
76
77 if (sha_env_str == NULL)
Simon Glass0c4bd312019-07-20 20:51:15 -060078 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese8f0b1e22015-05-18 14:08:24 +020079
80 /*
81 * Generate the binary value from the environment hash value
82 * so that we can compare this value with the computed hash
83 * from the user input
84 */
85 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
86 if (ret) {
87 printf("Hash %s not supported!\n", algo_name);
88 return 0;
89 }
90
91 /*
92 * We don't know how long the stop-string is, so we need to
93 * generate the sha256 hash upon each input character and
94 * compare the value with the one saved in the environment
95 */
96 do {
97 if (tstc()) {
98 /* Check for input string overflow */
99 if (presskey_len >= MAX_DELAY_STOP_STR)
100 return 0;
101
102 presskey[presskey_len++] = getc();
103
104 /* Calculate sha256 upon each new char */
105 hash_block(algo_name, (const void *)presskey,
106 presskey_len, sha, &size);
107
108 /* And check if sha matches saved value in env */
109 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
110 abort = 1;
111 }
112 } while (!abort && get_ticks() <= etime);
113
114 return abort;
115}
Simon Glasse8c78052019-07-20 20:51:16 -0600116
Simon Glass88fa4be2019-07-20 20:51:17 -0600117/**
118 * passwd_abort_key() - check for a key sequence to aborted booting
119 *
120 * This checks for the user entering a string within a given time.
121 *
122 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
123 * @return 0 if autoboot should continue, 1 if it should stop
124 */
Simon Glasse8c78052019-07-20 20:51:16 -0600125static int passwd_abort_key(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600126{
127 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600128 struct {
129 char *str;
130 u_int len;
131 int retry;
132 }
133 delaykey[] = {
Simon Glass00caae62017-08-03 12:22:12 -0600134 { .str = env_get("bootdelaykey"), .retry = 1 },
135 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600136 };
137
138 char presskey[MAX_DELAY_STOP_STR];
139 u_int presskey_len = 0;
140 u_int presskey_max = 0;
141 u_int i;
142
Simon Glass66ded172014-04-10 20:01:28 -0600143# ifdef CONFIG_AUTOBOOT_DELAY_STR
144 if (delaykey[0].str == NULL)
145 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
146# endif
Simon Glass66ded172014-04-10 20:01:28 -0600147# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200148 if (delaykey[1].str == NULL)
149 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600150# endif
151
152 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
153 delaykey[i].len = delaykey[i].str == NULL ?
154 0 : strlen(delaykey[i].str);
155 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
156 MAX_DELAY_STOP_STR : delaykey[i].len;
157
158 presskey_max = presskey_max > delaykey[i].len ?
159 presskey_max : delaykey[i].len;
160
161 debug_bootkeys("%s key:<%s>\n",
162 delaykey[i].retry ? "delay" : "stop",
163 delaykey[i].str ? delaykey[i].str : "NULL");
164 }
165
166 /* In order to keep up with incoming data, check timeout only
167 * when catch up.
168 */
169 do {
170 if (tstc()) {
171 if (presskey_len < presskey_max) {
172 presskey[presskey_len++] = getc();
173 } else {
174 for (i = 0; i < presskey_max - 1; i++)
175 presskey[i] = presskey[i + 1];
176
177 presskey[i] = getc();
178 }
179 }
180
181 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
182 if (delaykey[i].len > 0 &&
183 presskey_len >= delaykey[i].len &&
184 memcmp(presskey + presskey_len -
185 delaykey[i].len, delaykey[i].str,
186 delaykey[i].len) == 0) {
187 debug_bootkeys("got %skey\n",
188 delaykey[i].retry ? "delay" :
189 "stop");
190
Simon Glass66ded172014-04-10 20:01:28 -0600191 /* don't retry auto boot */
192 if (!delaykey[i].retry)
193 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600194 abort = 1;
195 }
196 }
197 } while (!abort && get_ticks() <= etime);
198
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200199 return abort;
200}
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200201
202/***************************************************************************
203 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
204 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
205 */
Masahiro Yamadad8da8292016-06-27 16:23:02 +0900206static int __abortboot(int bootdelay)
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200207{
208 int abort;
209 uint64_t etime = endtick(bootdelay);
210
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200211# ifdef CONFIG_AUTOBOOT_PROMPT
212 /*
213 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
214 * To print the bootdelay value upon bootup.
215 */
216 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
217# endif
218
Simon Glasse8c78052019-07-20 20:51:16 -0600219 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
220 abort = passwd_abort_sha256(etime);
221 else
222 abort = passwd_abort_key(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600223 if (!abort)
224 debug_bootkeys("key timeout\n");
225
Simon Glass66ded172014-04-10 20:01:28 -0600226 return abort;
227}
228
229# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
230
231#ifdef CONFIG_MENUKEY
232static int menukey;
233#endif
234
Masahiro Yamadad8da8292016-06-27 16:23:02 +0900235static int __abortboot(int bootdelay)
Simon Glass66ded172014-04-10 20:01:28 -0600236{
237 int abort = 0;
238 unsigned long ts;
239
240#ifdef CONFIG_MENUPROMPT
241 printf(CONFIG_MENUPROMPT);
242#else
Masahiro Yamada46327392016-06-27 16:23:04 +0900243 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass66ded172014-04-10 20:01:28 -0600244#endif
245
Simon Glass66ded172014-04-10 20:01:28 -0600246 /*
247 * Check if key already pressed
Simon Glass66ded172014-04-10 20:01:28 -0600248 */
Masahiro Yamada46327392016-06-27 16:23:04 +0900249 if (tstc()) { /* we got a key press */
250 (void) getc(); /* consume input */
251 puts("\b\b\b 0");
252 abort = 1; /* don't auto boot */
Simon Glass66ded172014-04-10 20:01:28 -0600253 }
Simon Glass66ded172014-04-10 20:01:28 -0600254
255 while ((bootdelay > 0) && (!abort)) {
256 --bootdelay;
257 /* delay 1000 ms */
258 ts = get_timer(0);
259 do {
260 if (tstc()) { /* we got a key press */
261 abort = 1; /* don't auto boot */
262 bootdelay = 0; /* no more delay */
263# ifdef CONFIG_MENUKEY
264 menukey = getc();
265# else
266 (void) getc(); /* consume input */
267# endif
268 break;
269 }
270 udelay(10000);
271 } while (!abort && get_timer(ts) < 1000);
272
273 printf("\b\b\b%2d ", bootdelay);
274 }
275
276 putc('\n');
277
Simon Glass66ded172014-04-10 20:01:28 -0600278 return abort;
279}
280# endif /* CONFIG_AUTOBOOT_KEYED */
281
282static int abortboot(int bootdelay)
283{
Masahiro Yamada46327392016-06-27 16:23:04 +0900284 int abort = 0;
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900285
Masahiro Yamada46327392016-06-27 16:23:04 +0900286 if (bootdelay >= 0)
287 abort = __abortboot(bootdelay);
Masahiro Yamada09b9d9e2016-06-27 16:23:03 +0900288
289#ifdef CONFIG_SILENT_CONSOLE
290 if (abort)
291 gd->flags &= ~GD_FLG_SILENT;
292#endif
293
294 return abort;
Simon Glass66ded172014-04-10 20:01:28 -0600295}
296
Simon Glass66ded172014-04-10 20:01:28 -0600297static void process_fdt_options(const void *blob)
298{
Stefan Roese9f736902016-01-28 17:34:40 +0100299#if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
Simon Glass66ded172014-04-10 20:01:28 -0600300 ulong addr;
301
302 /* Add an env variable to point to a kernel payload, if available */
303 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
304 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600305 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600306
307 /* Add an env variable to point to a root disk, if available */
308 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
309 if (addr)
Simon Glass018f5302017-08-03 12:22:10 -0600310 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Stefan Roese9f736902016-01-28 17:34:40 +0100311#endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
Simon Glassaffb2152014-04-10 20:01:35 -0600312}
Simon Glass66ded172014-04-10 20:01:28 -0600313
Simon Glassaffb2152014-04-10 20:01:35 -0600314const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600315{
Simon Glass66ded172014-04-10 20:01:28 -0600316 char *s;
317 int bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600318
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200319 bootcount_inc();
Simon Glass66ded172014-04-10 20:01:28 -0600320
Simon Glass00caae62017-08-03 12:22:12 -0600321 s = env_get("bootdelay");
Simon Glass66ded172014-04-10 20:01:28 -0600322 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
323
324#ifdef CONFIG_OF_CONTROL
325 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
326 bootdelay);
327#endif
328
329 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
330
331#if defined(CONFIG_MENU_SHOW)
332 bootdelay = menu_show(bootdelay);
333#endif
Simon Glassb26440f2014-04-10 20:01:31 -0600334 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600335
336#ifdef CONFIG_POST
337 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass00caae62017-08-03 12:22:12 -0600338 s = env_get("failbootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600339 } else
340#endif /* CONFIG_POST */
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200341 if (bootcount_error())
Simon Glass00caae62017-08-03 12:22:12 -0600342 s = env_get("altbootcmd");
Lukasz Majewskibc8c4402018-05-02 16:10:53 +0200343 else
Simon Glass00caae62017-08-03 12:22:12 -0600344 s = env_get("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600345
346 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600347 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600348
Simon Glassaffb2152014-04-10 20:01:35 -0600349 return s;
350}
Simon Glass66ded172014-04-10 20:01:28 -0600351
Simon Glassaffb2152014-04-10 20:01:35 -0600352void autoboot_command(const char *s)
353{
Simon Glass66ded172014-04-10 20:01:28 -0600354 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
355
Simon Glassaffb2152014-04-10 20:01:35 -0600356 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass66ded172014-04-10 20:01:28 -0600357#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
358 int prev = disable_ctrlc(1); /* disable Control C checking */
359#endif
360
361 run_command_list(s, -1, 0);
362
363#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
364 disable_ctrlc(prev); /* restore Control C checking */
365#endif
366 }
367
368#ifdef CONFIG_MENUKEY
369 if (menukey == CONFIG_MENUKEY) {
Simon Glass00caae62017-08-03 12:22:12 -0600370 s = env_get("menucmd");
Simon Glass66ded172014-04-10 20:01:28 -0600371 if (s)
372 run_command_list(s, -1, 0);
373 }
374#endif /* CONFIG_MENUKEY */
375}