blob: eb31c8875d9a0cfcc683fad80ec267fb20d7b742 [file] [log] [blame]
Simon Glass7b6a95a2014-04-10 20:01:28 -06001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Jeroen Hofsteef682d8c2014-07-13 22:57:58 +02009#include <autoboot.h>
Simon Glass399ed9a2014-04-10 20:01:30 -060010#include <bootretry.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060011#include <cli.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060013#include <fdtdec.h>
14#include <menu.h>
15#include <post.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020016#include <u-boot/sha256.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060017
18DECLARE_GLOBAL_DATA_PTR;
19
20#define MAX_DELAY_STOP_STR 32
21
22#ifndef DEBUG_BOOTKEYS
23#define DEBUG_BOOTKEYS 0
24#endif
25#define debug_bootkeys(fmt, args...) \
26 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
27
Simon Glass5b47e302014-04-10 20:01:35 -060028/* Stored value of bootdelay, used by autoboot_command() */
29static int stored_bootdelay;
30
Stefan Roese0ed2e462015-05-18 14:08:24 +020031#if defined(CONFIG_AUTOBOOT_KEYED)
32#if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
33
34/*
35 * Use a "constant-length" time compare function for this
36 * hash compare:
37 *
38 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -060039 */
Stefan Roese0ed2e462015-05-18 14:08:24 +020040static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -060041{
Stefan Roese0ed2e462015-05-18 14:08:24 +020042 int diff = 0;
43 int i;
44
45 for (i = 0; i < len; i++)
46 diff |= a[i] ^ b[i];
47
48 return diff == 0;
49}
50
51static int passwd_abort(uint64_t etime)
52{
53 const char *sha_env_str = getenv("bootstopkeysha256");
54 u8 sha_env[SHA256_SUM_LEN];
55 u8 sha[SHA256_SUM_LEN];
56 char presskey[MAX_DELAY_STOP_STR];
57 const char *algo_name = "sha256";
58 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -060059 int abort = 0;
Stefan Roese0ed2e462015-05-18 14:08:24 +020060 int size;
61 int ret;
62
63 if (sha_env_str == NULL)
64 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
65
66 /*
67 * Generate the binary value from the environment hash value
68 * so that we can compare this value with the computed hash
69 * from the user input
70 */
71 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
72 if (ret) {
73 printf("Hash %s not supported!\n", algo_name);
74 return 0;
75 }
76
77 /*
78 * We don't know how long the stop-string is, so we need to
79 * generate the sha256 hash upon each input character and
80 * compare the value with the one saved in the environment
81 */
82 do {
83 if (tstc()) {
84 /* Check for input string overflow */
85 if (presskey_len >= MAX_DELAY_STOP_STR)
86 return 0;
87
88 presskey[presskey_len++] = getc();
89
90 /* Calculate sha256 upon each new char */
91 hash_block(algo_name, (const void *)presskey,
92 presskey_len, sha, &size);
93
94 /* And check if sha matches saved value in env */
95 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
96 abort = 1;
97 }
98 } while (!abort && get_ticks() <= etime);
99
100 return abort;
101}
102#else
103static int passwd_abort(uint64_t etime)
104{
105 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600106 struct {
107 char *str;
108 u_int len;
109 int retry;
110 }
111 delaykey[] = {
Jeroen Hofstee5afc76f2014-06-16 00:17:33 +0200112 { .str = getenv("bootdelaykey"), .retry = 1 },
Jeroen Hofstee5afc76f2014-06-16 00:17:33 +0200113 { .str = getenv("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600114 };
115
116 char presskey[MAX_DELAY_STOP_STR];
117 u_int presskey_len = 0;
118 u_int presskey_max = 0;
119 u_int i;
120
Simon Glass7b6a95a2014-04-10 20:01:28 -0600121# ifdef CONFIG_AUTOBOOT_DELAY_STR
122 if (delaykey[0].str == NULL)
123 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
124# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600125# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200126 if (delaykey[1].str == NULL)
127 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600128# endif
129
130 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
131 delaykey[i].len = delaykey[i].str == NULL ?
132 0 : strlen(delaykey[i].str);
133 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
134 MAX_DELAY_STOP_STR : delaykey[i].len;
135
136 presskey_max = presskey_max > delaykey[i].len ?
137 presskey_max : delaykey[i].len;
138
139 debug_bootkeys("%s key:<%s>\n",
140 delaykey[i].retry ? "delay" : "stop",
141 delaykey[i].str ? delaykey[i].str : "NULL");
142 }
143
144 /* In order to keep up with incoming data, check timeout only
145 * when catch up.
146 */
147 do {
148 if (tstc()) {
149 if (presskey_len < presskey_max) {
150 presskey[presskey_len++] = getc();
151 } else {
152 for (i = 0; i < presskey_max - 1; i++)
153 presskey[i] = presskey[i + 1];
154
155 presskey[i] = getc();
156 }
157 }
158
159 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
160 if (delaykey[i].len > 0 &&
161 presskey_len >= delaykey[i].len &&
162 memcmp(presskey + presskey_len -
163 delaykey[i].len, delaykey[i].str,
164 delaykey[i].len) == 0) {
165 debug_bootkeys("got %skey\n",
166 delaykey[i].retry ? "delay" :
167 "stop");
168
Simon Glass7b6a95a2014-04-10 20:01:28 -0600169 /* don't retry auto boot */
170 if (!delaykey[i].retry)
171 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600172 abort = 1;
173 }
174 }
175 } while (!abort && get_ticks() <= etime);
176
Stefan Roese0ed2e462015-05-18 14:08:24 +0200177 return abort;
178}
179#endif
180
181/***************************************************************************
182 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
183 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
184 */
Masahiro Yamada6d1248a2016-06-27 16:23:02 +0900185static int __abortboot(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200186{
187 int abort;
188 uint64_t etime = endtick(bootdelay);
189
Masahiro Yamada5fadc7f2016-06-27 16:23:01 +0900190 if (bootdelay < 0)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200191 return 0;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200192
193# ifdef CONFIG_AUTOBOOT_PROMPT
194 /*
195 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
196 * To print the bootdelay value upon bootup.
197 */
198 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
199# endif
200
201 abort = passwd_abort(etime);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600202 if (!abort)
203 debug_bootkeys("key timeout\n");
204
205#ifdef CONFIG_SILENT_CONSOLE
206 if (abort)
207 gd->flags &= ~GD_FLG_SILENT;
208#endif
209
210 return abort;
211}
212
213# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
214
215#ifdef CONFIG_MENUKEY
216static int menukey;
217#endif
218
Masahiro Yamada6d1248a2016-06-27 16:23:02 +0900219static int __abortboot(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600220{
221 int abort = 0;
222 unsigned long ts;
223
224#ifdef CONFIG_MENUPROMPT
225 printf(CONFIG_MENUPROMPT);
226#else
227 if (bootdelay >= 0)
228 printf("Hit any key to stop autoboot: %2d ", bootdelay);
229#endif
230
Simon Glass7b6a95a2014-04-10 20:01:28 -0600231 /*
232 * Check if key already pressed
233 * Don't check if bootdelay < 0
234 */
235 if (bootdelay >= 0) {
236 if (tstc()) { /* we got a key press */
237 (void) getc(); /* consume input */
238 puts("\b\b\b 0");
239 abort = 1; /* don't auto boot */
240 }
241 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600242
243 while ((bootdelay > 0) && (!abort)) {
244 --bootdelay;
245 /* delay 1000 ms */
246 ts = get_timer(0);
247 do {
248 if (tstc()) { /* we got a key press */
249 abort = 1; /* don't auto boot */
250 bootdelay = 0; /* no more delay */
251# ifdef CONFIG_MENUKEY
252 menukey = getc();
253# else
254 (void) getc(); /* consume input */
255# endif
256 break;
257 }
258 udelay(10000);
259 } while (!abort && get_timer(ts) < 1000);
260
261 printf("\b\b\b%2d ", bootdelay);
262 }
263
264 putc('\n');
265
266#ifdef CONFIG_SILENT_CONSOLE
267 if (abort)
268 gd->flags &= ~GD_FLG_SILENT;
269#endif
270
271 return abort;
272}
273# endif /* CONFIG_AUTOBOOT_KEYED */
274
275static int abortboot(int bootdelay)
276{
Masahiro Yamada6d1248a2016-06-27 16:23:02 +0900277 return __abortboot(bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600278}
279
Simon Glass7b6a95a2014-04-10 20:01:28 -0600280static void process_fdt_options(const void *blob)
281{
Stefan Roesec9fa9a52016-01-28 17:34:40 +0100282#if defined(CONFIG_OF_CONTROL) && defined(CONFIG_SYS_TEXT_BASE)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600283 ulong addr;
284
285 /* Add an env variable to point to a kernel payload, if available */
286 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
287 if (addr)
288 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
289
290 /* Add an env variable to point to a root disk, if available */
291 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
292 if (addr)
293 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Stefan Roesec9fa9a52016-01-28 17:34:40 +0100294#endif /* CONFIG_OF_CONTROL && CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600295}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600296
Simon Glass5b47e302014-04-10 20:01:35 -0600297const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600298{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600299 char *s;
300 int bootdelay;
301#ifdef CONFIG_BOOTCOUNT_LIMIT
302 unsigned long bootcount = 0;
303 unsigned long bootlimit = 0;
304#endif /* CONFIG_BOOTCOUNT_LIMIT */
305
306#ifdef CONFIG_BOOTCOUNT_LIMIT
307 bootcount = bootcount_load();
308 bootcount++;
309 bootcount_store(bootcount);
310 setenv_ulong("bootcount", bootcount);
311 bootlimit = getenv_ulong("bootlimit", 10, 0);
312#endif /* CONFIG_BOOTCOUNT_LIMIT */
313
314 s = getenv("bootdelay");
315 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
316
317#ifdef CONFIG_OF_CONTROL
318 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
319 bootdelay);
320#endif
321
322 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
323
324#if defined(CONFIG_MENU_SHOW)
325 bootdelay = menu_show(bootdelay);
326#endif
Simon Glass09007c42014-04-10 20:01:31 -0600327 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600328
329#ifdef CONFIG_POST
330 if (gd->flags & GD_FLG_POSTFAIL) {
331 s = getenv("failbootcmd");
332 } else
333#endif /* CONFIG_POST */
334#ifdef CONFIG_BOOTCOUNT_LIMIT
335 if (bootlimit && (bootcount > bootlimit)) {
336 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
337 (unsigned)bootlimit);
338 s = getenv("altbootcmd");
339 } else
340#endif /* CONFIG_BOOTCOUNT_LIMIT */
341 s = getenv("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600342
343 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600344 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600345
Simon Glass5b47e302014-04-10 20:01:35 -0600346 return s;
347}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600348
Simon Glass5b47e302014-04-10 20:01:35 -0600349void autoboot_command(const char *s)
350{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600351 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
352
Simon Glass5b47e302014-04-10 20:01:35 -0600353 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass7b6a95a2014-04-10 20:01:28 -0600354#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
355 int prev = disable_ctrlc(1); /* disable Control C checking */
356#endif
357
358 run_command_list(s, -1, 0);
359
360#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
361 disable_ctrlc(prev); /* restore Control C checking */
362#endif
363 }
364
365#ifdef CONFIG_MENUKEY
366 if (menukey == CONFIG_MENUKEY) {
367 s = getenv("menucmd");
368 if (s)
369 run_command_list(s, -1, 0);
370 }
371#endif /* CONFIG_MENUKEY */
372}