blob: 90449a39fc2dc856bd41de343c69dd2327acd944 [file] [log] [blame]
Simon Glass66ded172014-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 Hofstee39e12302014-07-13 22:57:58 +02009#include <autoboot.h>
Simon Glass0098e172014-04-10 20:01:30 -060010#include <bootretry.h>
Simon Glass66ded172014-04-10 20:01:28 -060011#include <cli.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Simon Glass66ded172014-04-10 20:01:28 -060013#include <fdtdec.h>
14#include <menu.h>
15#include <post.h>
Stefan Roese8f0b1e22015-05-18 14:08:24 +020016#include <u-boot/sha256.h>
Gokul Sriram Palanisamy7e12e802017-11-17 16:41:57 +053017#include <asm/arch-qca-common/qca_common.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
Stefan Roese8f0b1e22015-05-18 14:08:24 +020032#if defined(CONFIG_AUTOBOOT_KEYED)
33#if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
34
35/*
36 * Use a "constant-length" time compare function for this
37 * hash compare:
38 *
39 * https://crackstation.net/hashing-security.htm
Simon Glass66ded172014-04-10 20:01:28 -060040 */
Stefan Roese8f0b1e22015-05-18 14:08:24 +020041static int slow_equals(u8 *a, u8 *b, int len)
42{
43 int diff = 0;
44 int i;
45
46 for (i = 0; i < len; i++)
47 diff |= a[i] ^ b[i];
48
49 return diff == 0;
50}
51
52static int passwd_abort(uint64_t etime)
53{
54 const char *sha_env_str = getenv("bootstopkeysha256");
55 u8 sha_env[SHA256_SUM_LEN];
56 u8 sha[SHA256_SUM_LEN];
57 char presskey[MAX_DELAY_STOP_STR];
58 const char *algo_name = "sha256";
59 u_int presskey_len = 0;
60 int abort = 0;
61 int size;
62 int ret;
63
64 if (sha_env_str == NULL)
65 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
66
67 /*
68 * Generate the binary value from the environment hash value
69 * so that we can compare this value with the computed hash
70 * from the user input
71 */
72 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
73 if (ret) {
74 printf("Hash %s not supported!\n", algo_name);
75 return 0;
76 }
77
78 /*
79 * We don't know how long the stop-string is, so we need to
80 * generate the sha256 hash upon each input character and
81 * compare the value with the one saved in the environment
82 */
83 do {
84 if (tstc()) {
85 /* Check for input string overflow */
86 if (presskey_len >= MAX_DELAY_STOP_STR)
87 return 0;
88
89 presskey[presskey_len++] = getc();
90
91 /* Calculate sha256 upon each new char */
92 hash_block(algo_name, (const void *)presskey,
93 presskey_len, sha, &size);
94
95 /* And check if sha matches saved value in env */
96 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
97 abort = 1;
98 }
99 } while (!abort && get_ticks() <= etime);
100
101 return abort;
102}
103#else
104static int passwd_abort(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600105{
106 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600107 struct {
108 char *str;
109 u_int len;
110 int retry;
111 }
112 delaykey[] = {
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +0200113 { .str = getenv("bootdelaykey"), .retry = 1 },
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +0200114 { .str = getenv("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600115 };
116
117 char presskey[MAX_DELAY_STOP_STR];
118 u_int presskey_len = 0;
119 u_int presskey_max = 0;
120 u_int i;
121
Simon Glass66ded172014-04-10 20:01:28 -0600122# ifdef CONFIG_AUTOBOOT_DELAY_STR
123 if (delaykey[0].str == NULL)
124 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
125# endif
Simon Glass66ded172014-04-10 20:01:28 -0600126# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200127 if (delaykey[1].str == NULL)
128 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600129# endif
130
131 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
132 delaykey[i].len = delaykey[i].str == NULL ?
133 0 : strlen(delaykey[i].str);
134 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
135 MAX_DELAY_STOP_STR : delaykey[i].len;
136
137 presskey_max = presskey_max > delaykey[i].len ?
138 presskey_max : delaykey[i].len;
139
140 debug_bootkeys("%s key:<%s>\n",
141 delaykey[i].retry ? "delay" : "stop",
142 delaykey[i].str ? delaykey[i].str : "NULL");
143 }
144
145 /* In order to keep up with incoming data, check timeout only
146 * when catch up.
147 */
148 do {
149 if (tstc()) {
150 if (presskey_len < presskey_max) {
151 presskey[presskey_len++] = getc();
152 } else {
153 for (i = 0; i < presskey_max - 1; i++)
154 presskey[i] = presskey[i + 1];
155
156 presskey[i] = getc();
157 }
158 }
159
160 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
161 if (delaykey[i].len > 0 &&
162 presskey_len >= delaykey[i].len &&
163 memcmp(presskey + presskey_len -
164 delaykey[i].len, delaykey[i].str,
165 delaykey[i].len) == 0) {
166 debug_bootkeys("got %skey\n",
167 delaykey[i].retry ? "delay" :
168 "stop");
169
Simon Glass66ded172014-04-10 20:01:28 -0600170 /* don't retry auto boot */
171 if (!delaykey[i].retry)
172 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600173 abort = 1;
174 }
175 }
176 } while (!abort && get_ticks() <= etime);
177
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200178 return abort;
179}
180#endif
181
182/***************************************************************************
183 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
184 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
185 */
186static int abortboot_keyed(int bootdelay)
187{
188 int abort;
189 uint64_t etime = endtick(bootdelay);
190
191#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
192 if (bootdelay == 0)
193 return 0;
194#endif
195
196# ifdef CONFIG_AUTOBOOT_PROMPT
197 /*
198 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
199 * To print the bootdelay value upon bootup.
200 */
201 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
202# endif
203
204 abort = passwd_abort(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600205 if (!abort)
206 debug_bootkeys("key timeout\n");
207
208#ifdef CONFIG_SILENT_CONSOLE
209 if (abort)
210 gd->flags &= ~GD_FLG_SILENT;
211#endif
212
213 return abort;
214}
215
216# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
217
218#ifdef CONFIG_MENUKEY
219static int menukey;
220#endif
221
222static int abortboot_normal(int bootdelay)
223{
224 int abort = 0;
225 unsigned long ts;
226
227#ifdef CONFIG_MENUPROMPT
228 printf(CONFIG_MENUPROMPT);
229#else
230 if (bootdelay >= 0)
231 printf("Hit any key to stop autoboot: %2d ", bootdelay);
232#endif
233
234#if defined CONFIG_ZERO_BOOTDELAY_CHECK
235 /*
236 * Check if key already pressed
237 * Don't check if bootdelay < 0
238 */
239 if (bootdelay >= 0) {
240 if (tstc()) { /* we got a key press */
241 (void) getc(); /* consume input */
242 puts("\b\b\b 0");
243 abort = 1; /* don't auto boot */
244 }
245 }
246#endif
247
248 while ((bootdelay > 0) && (!abort)) {
249 --bootdelay;
250 /* delay 1000 ms */
251 ts = get_timer(0);
252 do {
253 if (tstc()) { /* we got a key press */
254 abort = 1; /* don't auto boot */
255 bootdelay = 0; /* no more delay */
256# ifdef CONFIG_MENUKEY
257 menukey = getc();
258# else
259 (void) getc(); /* consume input */
260# endif
261 break;
262 }
263 udelay(10000);
264 } while (!abort && get_timer(ts) < 1000);
265
266 printf("\b\b\b%2d ", bootdelay);
267 }
268
269 putc('\n');
270
271#ifdef CONFIG_SILENT_CONSOLE
272 if (abort)
273 gd->flags &= ~GD_FLG_SILENT;
274#endif
Jaiganesh Narayanan67e7d1b2017-06-12 15:50:37 +0530275#ifdef CONFIG_IPQ_ETH_INIT_DEFER
276 if (abort) {
277 puts("\nNet: ");
278 eth_initialize();
279 }
280#endif
Simon Glass66ded172014-04-10 20:01:28 -0600281 return abort;
282}
283# endif /* CONFIG_AUTOBOOT_KEYED */
284
285static int abortboot(int bootdelay)
286{
Gokul Sriram Palanisamy7e12e802017-11-17 16:41:57 +0530287#ifdef CONFIG_QCA_APPSBL_DLOAD
288 /* If kernel has crashed in previous boot, skipping abortboot */
289 if (qca_iscrashed()) {
290 printf("Crashdump magic found, initializing dump activity..\n");
291 return 0;
292 }
293#endif
294
Simon Glass66ded172014-04-10 20:01:28 -0600295#ifdef CONFIG_AUTOBOOT_KEYED
296 return abortboot_keyed(bootdelay);
297#else
298 return abortboot_normal(bootdelay);
299#endif
300}
301
Simon Glass66ded172014-04-10 20:01:28 -0600302static void process_fdt_options(const void *blob)
303{
Simon Glassaffb2152014-04-10 20:01:35 -0600304#if defined(CONFIG_OF_CONTROL)
Simon Glass66ded172014-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)
310 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
311
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)
315 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600316#endif /* CONFIG_OF_CONTROL */
Simon Glassaffb2152014-04-10 20:01:35 -0600317}
Simon Glass66ded172014-04-10 20:01:28 -0600318
Simon Glassaffb2152014-04-10 20:01:35 -0600319const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600320{
Simon Glass66ded172014-04-10 20:01:28 -0600321 char *s;
322 int bootdelay;
323#ifdef CONFIG_BOOTCOUNT_LIMIT
324 unsigned long bootcount = 0;
325 unsigned long bootlimit = 0;
326#endif /* CONFIG_BOOTCOUNT_LIMIT */
327
328#ifdef CONFIG_BOOTCOUNT_LIMIT
329 bootcount = bootcount_load();
330 bootcount++;
331 bootcount_store(bootcount);
332 setenv_ulong("bootcount", bootcount);
333 bootlimit = getenv_ulong("bootlimit", 10, 0);
334#endif /* CONFIG_BOOTCOUNT_LIMIT */
335
336 s = getenv("bootdelay");
337 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
338
339#ifdef CONFIG_OF_CONTROL
340 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
341 bootdelay);
342#endif
343
344 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
345
346#if defined(CONFIG_MENU_SHOW)
347 bootdelay = menu_show(bootdelay);
348#endif
Simon Glassb26440f2014-04-10 20:01:31 -0600349 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600350
351#ifdef CONFIG_POST
352 if (gd->flags & GD_FLG_POSTFAIL) {
353 s = getenv("failbootcmd");
354 } else
355#endif /* CONFIG_POST */
356#ifdef CONFIG_BOOTCOUNT_LIMIT
357 if (bootlimit && (bootcount > bootlimit)) {
358 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
359 (unsigned)bootlimit);
360 s = getenv("altbootcmd");
361 } else
362#endif /* CONFIG_BOOTCOUNT_LIMIT */
363 s = getenv("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600364
365 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600366 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600367
Simon Glassaffb2152014-04-10 20:01:35 -0600368 return s;
369}
Simon Glass66ded172014-04-10 20:01:28 -0600370
Simon Glassaffb2152014-04-10 20:01:35 -0600371void autoboot_command(const char *s)
372{
Simon Glass66ded172014-04-10 20:01:28 -0600373 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
374
Simon Glassaffb2152014-04-10 20:01:35 -0600375 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass66ded172014-04-10 20:01:28 -0600376#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
377 int prev = disable_ctrlc(1); /* disable Control C checking */
378#endif
379
380 run_command_list(s, -1, 0);
381
382#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
383 disable_ctrlc(prev); /* restore Control C checking */
384#endif
385 }
386
387#ifdef CONFIG_MENUKEY
388 if (menukey == CONFIG_MENUKEY) {
389 s = getenv("menucmd");
390 if (s)
391 run_command_list(s, -1, 0);
392 }
393#endif /* CONFIG_MENUKEY */
394}