blob: 48b06063abd747e66742d2fecfe423cc66a1a771 [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
Gitanjali Krishna9af31fe2019-06-10 14:50:33 -070021extern int do_dumpqca_minimal_data(const char *offset);
22
Simon Glass66ded172014-04-10 20:01:28 -060023#define MAX_DELAY_STOP_STR 32
24
25#ifndef DEBUG_BOOTKEYS
26#define DEBUG_BOOTKEYS 0
27#endif
28#define debug_bootkeys(fmt, args...) \
29 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
30
Simon Glassaffb2152014-04-10 20:01:35 -060031/* Stored value of bootdelay, used by autoboot_command() */
32static int stored_bootdelay;
33
Stefan Roese8f0b1e22015-05-18 14:08:24 +020034#if defined(CONFIG_AUTOBOOT_KEYED)
35#if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
36
37/*
38 * Use a "constant-length" time compare function for this
39 * hash compare:
40 *
41 * https://crackstation.net/hashing-security.htm
Simon Glass66ded172014-04-10 20:01:28 -060042 */
Stefan Roese8f0b1e22015-05-18 14:08:24 +020043static int slow_equals(u8 *a, u8 *b, int len)
44{
45 int diff = 0;
46 int i;
47
48 for (i = 0; i < len; i++)
49 diff |= a[i] ^ b[i];
50
51 return diff == 0;
52}
53
54static int passwd_abort(uint64_t etime)
55{
56 const char *sha_env_str = getenv("bootstopkeysha256");
57 u8 sha_env[SHA256_SUM_LEN];
58 u8 sha[SHA256_SUM_LEN];
59 char presskey[MAX_DELAY_STOP_STR];
60 const char *algo_name = "sha256";
61 u_int presskey_len = 0;
62 int abort = 0;
63 int size;
64 int ret;
65
66 if (sha_env_str == NULL)
67 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
68
69 /*
70 * Generate the binary value from the environment hash value
71 * so that we can compare this value with the computed hash
72 * from the user input
73 */
74 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
75 if (ret) {
76 printf("Hash %s not supported!\n", algo_name);
77 return 0;
78 }
79
80 /*
81 * We don't know how long the stop-string is, so we need to
82 * generate the sha256 hash upon each input character and
83 * compare the value with the one saved in the environment
84 */
85 do {
86 if (tstc()) {
87 /* Check for input string overflow */
88 if (presskey_len >= MAX_DELAY_STOP_STR)
89 return 0;
90
91 presskey[presskey_len++] = getc();
92
93 /* Calculate sha256 upon each new char */
94 hash_block(algo_name, (const void *)presskey,
95 presskey_len, sha, &size);
96
97 /* And check if sha matches saved value in env */
98 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
99 abort = 1;
100 }
101 } while (!abort && get_ticks() <= etime);
102
103 return abort;
104}
105#else
106static int passwd_abort(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600107{
108 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600109 struct {
110 char *str;
111 u_int len;
112 int retry;
113 }
114 delaykey[] = {
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +0200115 { .str = getenv("bootdelaykey"), .retry = 1 },
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +0200116 { .str = getenv("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600117 };
118
119 char presskey[MAX_DELAY_STOP_STR];
120 u_int presskey_len = 0;
121 u_int presskey_max = 0;
122 u_int i;
123
Simon Glass66ded172014-04-10 20:01:28 -0600124# ifdef CONFIG_AUTOBOOT_DELAY_STR
125 if (delaykey[0].str == NULL)
126 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
127# endif
Simon Glass66ded172014-04-10 20:01:28 -0600128# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200129 if (delaykey[1].str == NULL)
130 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600131# endif
132
133 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
134 delaykey[i].len = delaykey[i].str == NULL ?
135 0 : strlen(delaykey[i].str);
136 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
137 MAX_DELAY_STOP_STR : delaykey[i].len;
138
139 presskey_max = presskey_max > delaykey[i].len ?
140 presskey_max : delaykey[i].len;
141
142 debug_bootkeys("%s key:<%s>\n",
143 delaykey[i].retry ? "delay" : "stop",
144 delaykey[i].str ? delaykey[i].str : "NULL");
145 }
146
147 /* In order to keep up with incoming data, check timeout only
148 * when catch up.
149 */
150 do {
151 if (tstc()) {
152 if (presskey_len < presskey_max) {
153 presskey[presskey_len++] = getc();
154 } else {
155 for (i = 0; i < presskey_max - 1; i++)
156 presskey[i] = presskey[i + 1];
157
158 presskey[i] = getc();
159 }
160 }
161
162 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
163 if (delaykey[i].len > 0 &&
164 presskey_len >= delaykey[i].len &&
165 memcmp(presskey + presskey_len -
166 delaykey[i].len, delaykey[i].str,
167 delaykey[i].len) == 0) {
168 debug_bootkeys("got %skey\n",
169 delaykey[i].retry ? "delay" :
170 "stop");
171
Simon Glass66ded172014-04-10 20:01:28 -0600172 /* don't retry auto boot */
173 if (!delaykey[i].retry)
174 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600175 abort = 1;
176 }
177 }
178 } while (!abort && get_ticks() <= etime);
179
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200180 return abort;
181}
182#endif
183
184/***************************************************************************
185 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
186 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
187 */
188static int abortboot_keyed(int bootdelay)
189{
190 int abort;
191 uint64_t etime = endtick(bootdelay);
192
193#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
194 if (bootdelay == 0)
195 return 0;
196#endif
197
198# ifdef CONFIG_AUTOBOOT_PROMPT
199 /*
200 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
201 * To print the bootdelay value upon bootup.
202 */
203 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
204# endif
205
206 abort = passwd_abort(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600207 if (!abort)
208 debug_bootkeys("key timeout\n");
209
210#ifdef CONFIG_SILENT_CONSOLE
211 if (abort)
212 gd->flags &= ~GD_FLG_SILENT;
213#endif
214
215 return abort;
216}
217
218# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
219
220#ifdef CONFIG_MENUKEY
221static int menukey;
222#endif
223
224static int abortboot_normal(int bootdelay)
225{
226 int abort = 0;
227 unsigned long ts;
228
229#ifdef CONFIG_MENUPROMPT
230 printf(CONFIG_MENUPROMPT);
231#else
232 if (bootdelay >= 0)
233 printf("Hit any key to stop autoboot: %2d ", bootdelay);
234#endif
235
236#if defined CONFIG_ZERO_BOOTDELAY_CHECK
237 /*
238 * Check if key already pressed
239 * Don't check if bootdelay < 0
240 */
241 if (bootdelay >= 0) {
242 if (tstc()) { /* we got a key press */
243 (void) getc(); /* consume input */
244 puts("\b\b\b 0");
245 abort = 1; /* don't auto boot */
246 }
247 }
248#endif
249
250 while ((bootdelay > 0) && (!abort)) {
251 --bootdelay;
252 /* delay 1000 ms */
253 ts = get_timer(0);
254 do {
255 if (tstc()) { /* we got a key press */
256 abort = 1; /* don't auto boot */
257 bootdelay = 0; /* no more delay */
258# ifdef CONFIG_MENUKEY
259 menukey = getc();
260# else
261 (void) getc(); /* consume input */
262# endif
263 break;
264 }
265 udelay(10000);
266 } while (!abort && get_timer(ts) < 1000);
267
268 printf("\b\b\b%2d ", bootdelay);
269 }
270
271 putc('\n');
272
273#ifdef CONFIG_SILENT_CONSOLE
274 if (abort)
275 gd->flags &= ~GD_FLG_SILENT;
276#endif
Simon Glass66ded172014-04-10 20:01:28 -0600277 return abort;
278}
279# endif /* CONFIG_AUTOBOOT_KEYED */
280
281static int abortboot(int bootdelay)
282{
283#ifdef CONFIG_AUTOBOOT_KEYED
284 return abortboot_keyed(bootdelay);
285#else
286 return abortboot_normal(bootdelay);
287#endif
288}
289
Simon Glass66ded172014-04-10 20:01:28 -0600290static void process_fdt_options(const void *blob)
291{
Simon Glassaffb2152014-04-10 20:01:35 -0600292#if defined(CONFIG_OF_CONTROL)
Simon Glass66ded172014-04-10 20:01:28 -0600293 ulong addr;
294
295 /* Add an env variable to point to a kernel payload, if available */
296 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
297 if (addr)
298 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
299
300 /* Add an env variable to point to a root disk, if available */
301 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
302 if (addr)
303 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600304#endif /* CONFIG_OF_CONTROL */
Simon Glassaffb2152014-04-10 20:01:35 -0600305}
Simon Glass66ded172014-04-10 20:01:28 -0600306
Simon Glassaffb2152014-04-10 20:01:35 -0600307const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600308{
Simon Glass66ded172014-04-10 20:01:28 -0600309 char *s;
310 int bootdelay;
311#ifdef CONFIG_BOOTCOUNT_LIMIT
312 unsigned long bootcount = 0;
313 unsigned long bootlimit = 0;
314#endif /* CONFIG_BOOTCOUNT_LIMIT */
315
316#ifdef CONFIG_BOOTCOUNT_LIMIT
317 bootcount = bootcount_load();
318 bootcount++;
319 bootcount_store(bootcount);
320 setenv_ulong("bootcount", bootcount);
321 bootlimit = getenv_ulong("bootlimit", 10, 0);
322#endif /* CONFIG_BOOTCOUNT_LIMIT */
323
324 s = getenv("bootdelay");
325 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
326
327#ifdef CONFIG_OF_CONTROL
328 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
329 bootdelay);
330#endif
331
332 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
333
334#if defined(CONFIG_MENU_SHOW)
335 bootdelay = menu_show(bootdelay);
336#endif
Simon Glassb26440f2014-04-10 20:01:31 -0600337 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600338
339#ifdef CONFIG_POST
340 if (gd->flags & GD_FLG_POSTFAIL) {
341 s = getenv("failbootcmd");
342 } else
343#endif /* CONFIG_POST */
344#ifdef CONFIG_BOOTCOUNT_LIMIT
345 if (bootlimit && (bootcount > bootlimit)) {
346 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
347 (unsigned)bootlimit);
348 s = getenv("altbootcmd");
349 } else
350#endif /* CONFIG_BOOTCOUNT_LIMIT */
351 s = getenv("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600352
353 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600354 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600355
Simon Glassaffb2152014-04-10 20:01:35 -0600356 return s;
357}
Simon Glass66ded172014-04-10 20:01:28 -0600358
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530359__weak int apps_iscrashed(void)
360{
361 return 0;
362}
363
speriaka98e44092019-08-27 11:27:13 +0530364__weak int apps_iscrashed_crashdump_disabled(void)
365{
366 return 0;
367}
368
Simon Glassaffb2152014-04-10 20:01:35 -0600369void autoboot_command(const char *s)
370{
Simon Glass66ded172014-04-10 20:01:28 -0600371 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
372
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530373#ifdef CONFIG_QCA_APPSBL_DLOAD
374 /*
375 * If kernel has crashed in previous boot,
376 * jump to crash dump collection.
377 */
378 if (apps_iscrashed()) {
379 printf("Crashdump magic found, initializing dump activity..\n");
Gokul Sriram Palanisamyebfe5532018-01-29 13:18:16 +0530380 s = getenv("dump_to_flash");
Venkat Raju Sanad0efe152019-07-22 20:31:38 -0700381 if (!s) {
Venkat Raju Sana2e7684f2019-03-11 19:29:46 -0700382 s = getenv("dump_minimal");
Venkat Raju Sanad0efe152019-07-22 20:31:38 -0700383 if (s) {
384 if (strncmp(s, "1", sizeof("1"))) {
385 printf("\nError: Invalid variable dump_minimal \n");
386 reset_board();
387 }
388 }
389 }
Sandhya0b984e92019-06-26 10:24:59 +0530390 if (s) {
Venkat Raju Sanad0efe152019-07-22 20:31:38 -0700391 do_dumpqca_minimal_data(s);
Antony Arun T6cc115d2019-07-19 16:13:50 +0530392 reset_board();
Sandhya0b984e92019-06-26 10:24:59 +0530393 }
Gokul Sriram Palanisamyebfe5532018-01-29 13:18:16 +0530394 else
Venkat Raju Sana2e7684f2019-03-11 19:29:46 -0700395 dump_func(FULL_DUMP);
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530396 return;
397 }
398#endif
399
speriaka98e44092019-08-27 11:27:13 +0530400 if (apps_iscrashed_crashdump_disabled()) {
401 printf("Crashdump disabled, resetting the board..\n");
402 reset_board();
403 }
404
Simon Glassaffb2152014-04-10 20:01:35 -0600405 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass66ded172014-04-10 20:01:28 -0600406#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
407 int prev = disable_ctrlc(1); /* disable Control C checking */
408#endif
409
410 run_command_list(s, -1, 0);
411
412#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
413 disable_ctrlc(prev); /* restore Control C checking */
414#endif
415 }
416
417#ifdef CONFIG_MENUKEY
418 if (menukey == CONFIG_MENUKEY) {
419 s = getenv("menucmd");
420 if (s)
421 run_command_list(s, -1, 0);
422 }
423#endif /* CONFIG_MENUKEY */
Gokul Sriram Palanisamya23d3222017-12-21 11:50:24 +0530424
425#ifdef CONFIG_IPQ_ETH_INIT_DEFER
426 puts("\nNet: ");
427 eth_initialize();
428#endif
Simon Glass66ded172014-04-10 20:01:28 -0600429}