blob: 36604f5a634efdd468e655d8a2b7f6b0cdbd3a28 [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 Palanisamye1b91062017-12-20 17:57:24 +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
Simon Glass66ded172014-04-10 20:01:28 -0600275 return abort;
276}
277# endif /* CONFIG_AUTOBOOT_KEYED */
278
279static int abortboot(int bootdelay)
280{
281#ifdef CONFIG_AUTOBOOT_KEYED
282 return abortboot_keyed(bootdelay);
283#else
284 return abortboot_normal(bootdelay);
285#endif
286}
287
Simon Glass66ded172014-04-10 20:01:28 -0600288static void process_fdt_options(const void *blob)
289{
Simon Glassaffb2152014-04-10 20:01:35 -0600290#if defined(CONFIG_OF_CONTROL)
Simon Glass66ded172014-04-10 20:01:28 -0600291 ulong addr;
292
293 /* Add an env variable to point to a kernel payload, if available */
294 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
295 if (addr)
296 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
297
298 /* Add an env variable to point to a root disk, if available */
299 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
300 if (addr)
301 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600302#endif /* CONFIG_OF_CONTROL */
Simon Glassaffb2152014-04-10 20:01:35 -0600303}
Simon Glass66ded172014-04-10 20:01:28 -0600304
Simon Glassaffb2152014-04-10 20:01:35 -0600305const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600306{
Simon Glass66ded172014-04-10 20:01:28 -0600307 char *s;
308 int bootdelay;
309#ifdef CONFIG_BOOTCOUNT_LIMIT
310 unsigned long bootcount = 0;
311 unsigned long bootlimit = 0;
312#endif /* CONFIG_BOOTCOUNT_LIMIT */
313
314#ifdef CONFIG_BOOTCOUNT_LIMIT
315 bootcount = bootcount_load();
316 bootcount++;
317 bootcount_store(bootcount);
318 setenv_ulong("bootcount", bootcount);
319 bootlimit = getenv_ulong("bootlimit", 10, 0);
320#endif /* CONFIG_BOOTCOUNT_LIMIT */
321
322 s = getenv("bootdelay");
323 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
324
325#ifdef CONFIG_OF_CONTROL
326 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
327 bootdelay);
328#endif
329
330 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
331
332#if defined(CONFIG_MENU_SHOW)
333 bootdelay = menu_show(bootdelay);
334#endif
Simon Glassb26440f2014-04-10 20:01:31 -0600335 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600336
337#ifdef CONFIG_POST
338 if (gd->flags & GD_FLG_POSTFAIL) {
339 s = getenv("failbootcmd");
340 } else
341#endif /* CONFIG_POST */
342#ifdef CONFIG_BOOTCOUNT_LIMIT
343 if (bootlimit && (bootcount > bootlimit)) {
344 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
345 (unsigned)bootlimit);
346 s = getenv("altbootcmd");
347 } else
348#endif /* CONFIG_BOOTCOUNT_LIMIT */
349 s = getenv("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600350
351 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600352 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600353
Simon Glassaffb2152014-04-10 20:01:35 -0600354 return s;
355}
Simon Glass66ded172014-04-10 20:01:28 -0600356
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530357__weak int apps_iscrashed(void)
358{
359 return 0;
360}
361
Simon Glassaffb2152014-04-10 20:01:35 -0600362void autoboot_command(const char *s)
363{
Simon Glass66ded172014-04-10 20:01:28 -0600364 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
365
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530366#ifdef CONFIG_QCA_APPSBL_DLOAD
367 /*
368 * If kernel has crashed in previous boot,
369 * jump to crash dump collection.
370 */
371 if (apps_iscrashed()) {
372 printf("Crashdump magic found, initializing dump activity..\n");
Gokul Sriram Palanisamyebfe5532018-01-29 13:18:16 +0530373 s = getenv("dump_to_flash");
Venkat Raju Sana2e7684f2019-03-11 19:29:46 -0700374 if (!s)
375 s = getenv("dump_minimal");
376 if (s) {
377 do_dumpqca_minimal_data(s); /* write core dump data to flash */
378 run_command("reset", 0);
379 }
Gokul Sriram Palanisamyebfe5532018-01-29 13:18:16 +0530380 else
Venkat Raju Sana2e7684f2019-03-11 19:29:46 -0700381 dump_func(FULL_DUMP);
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530382 return;
383 }
384#endif
385
Simon Glassaffb2152014-04-10 20:01:35 -0600386 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass66ded172014-04-10 20:01:28 -0600387#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
388 int prev = disable_ctrlc(1); /* disable Control C checking */
389#endif
390
391 run_command_list(s, -1, 0);
392
393#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
394 disable_ctrlc(prev); /* restore Control C checking */
395#endif
396 }
397
398#ifdef CONFIG_MENUKEY
399 if (menukey == CONFIG_MENUKEY) {
400 s = getenv("menucmd");
401 if (s)
402 run_command_list(s, -1, 0);
403 }
404#endif /* CONFIG_MENUKEY */
Gokul Sriram Palanisamya23d3222017-12-21 11:50:24 +0530405
406#ifdef CONFIG_IPQ_ETH_INIT_DEFER
407 puts("\nNet: ");
408 eth_initialize();
409#endif
Simon Glass66ded172014-04-10 20:01:28 -0600410}