blob: 517171c9dbcd336d74f32665999a1f5c96c52e8e [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
Kyle Swenson3c6e30f2021-03-29 09:52:45 -060034extern void check_serial_console(void);
35extern void check_for_enable_usb_passthrough(void);
36extern void print_menu_selections(void);
37extern void process_menu_selection(int);
38#if defined(CONFIG_DEBUG_ENABLE)
39extern int process_debug_package(void);
40#endif
41extern void check_factory_reset(void);
42extern void usb_passthrough(void);
43
Stefan Roese8f0b1e22015-05-18 14:08:24 +020044#if defined(CONFIG_AUTOBOOT_KEYED)
45#if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
46
47/*
48 * Use a "constant-length" time compare function for this
49 * hash compare:
50 *
51 * https://crackstation.net/hashing-security.htm
Simon Glass66ded172014-04-10 20:01:28 -060052 */
Stefan Roese8f0b1e22015-05-18 14:08:24 +020053static int slow_equals(u8 *a, u8 *b, int len)
54{
55 int diff = 0;
56 int i;
57
58 for (i = 0; i < len; i++)
59 diff |= a[i] ^ b[i];
60
61 return diff == 0;
62}
63
64static int passwd_abort(uint64_t etime)
65{
66 const char *sha_env_str = getenv("bootstopkeysha256");
67 u8 sha_env[SHA256_SUM_LEN];
68 u8 sha[SHA256_SUM_LEN];
69 char presskey[MAX_DELAY_STOP_STR];
70 const char *algo_name = "sha256";
71 u_int presskey_len = 0;
72 int abort = 0;
73 int size;
74 int ret;
75
76 if (sha_env_str == NULL)
77 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
78
79 /*
80 * Generate the binary value from the environment hash value
81 * so that we can compare this value with the computed hash
82 * from the user input
83 */
84 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
85 if (ret) {
86 printf("Hash %s not supported!\n", algo_name);
87 return 0;
88 }
89
90 /*
91 * We don't know how long the stop-string is, so we need to
92 * generate the sha256 hash upon each input character and
93 * compare the value with the one saved in the environment
94 */
95 do {
96 if (tstc()) {
97 /* Check for input string overflow */
98 if (presskey_len >= MAX_DELAY_STOP_STR)
99 return 0;
100
101 presskey[presskey_len++] = getc();
102
103 /* Calculate sha256 upon each new char */
104 hash_block(algo_name, (const void *)presskey,
105 presskey_len, sha, &size);
106
107 /* And check if sha matches saved value in env */
108 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
109 abort = 1;
110 }
111 } while (!abort && get_ticks() <= etime);
112
113 return abort;
114}
115#else
116static int passwd_abort(uint64_t etime)
Simon Glass66ded172014-04-10 20:01:28 -0600117{
118 int abort = 0;
Simon Glass66ded172014-04-10 20:01:28 -0600119 struct {
120 char *str;
121 u_int len;
122 int retry;
123 }
124 delaykey[] = {
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +0200125 { .str = getenv("bootdelaykey"), .retry = 1 },
Jeroen Hofstee9e546ee2014-06-16 00:17:33 +0200126 { .str = getenv("bootstopkey"), .retry = 0 },
Simon Glass66ded172014-04-10 20:01:28 -0600127 };
128
129 char presskey[MAX_DELAY_STOP_STR];
130 u_int presskey_len = 0;
131 u_int presskey_max = 0;
132 u_int i;
133
Simon Glass66ded172014-04-10 20:01:28 -0600134# ifdef CONFIG_AUTOBOOT_DELAY_STR
135 if (delaykey[0].str == NULL)
136 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
137# endif
Simon Glass66ded172014-04-10 20:01:28 -0600138# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese2d908fa2015-05-18 14:08:22 +0200139 if (delaykey[1].str == NULL)
140 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass66ded172014-04-10 20:01:28 -0600141# endif
142
143 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
144 delaykey[i].len = delaykey[i].str == NULL ?
145 0 : strlen(delaykey[i].str);
146 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
147 MAX_DELAY_STOP_STR : delaykey[i].len;
148
149 presskey_max = presskey_max > delaykey[i].len ?
150 presskey_max : delaykey[i].len;
151
152 debug_bootkeys("%s key:<%s>\n",
153 delaykey[i].retry ? "delay" : "stop",
154 delaykey[i].str ? delaykey[i].str : "NULL");
155 }
156
157 /* In order to keep up with incoming data, check timeout only
158 * when catch up.
159 */
160 do {
161 if (tstc()) {
162 if (presskey_len < presskey_max) {
163 presskey[presskey_len++] = getc();
164 } else {
165 for (i = 0; i < presskey_max - 1; i++)
166 presskey[i] = presskey[i + 1];
167
168 presskey[i] = getc();
169 }
170 }
171
172 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
173 if (delaykey[i].len > 0 &&
174 presskey_len >= delaykey[i].len &&
175 memcmp(presskey + presskey_len -
176 delaykey[i].len, delaykey[i].str,
177 delaykey[i].len) == 0) {
178 debug_bootkeys("got %skey\n",
179 delaykey[i].retry ? "delay" :
180 "stop");
181
Simon Glass66ded172014-04-10 20:01:28 -0600182 /* don't retry auto boot */
183 if (!delaykey[i].retry)
184 bootretry_dont_retry();
Simon Glass66ded172014-04-10 20:01:28 -0600185 abort = 1;
186 }
187 }
188 } while (!abort && get_ticks() <= etime);
189
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200190 return abort;
191}
192#endif
193
194/***************************************************************************
195 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
196 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
197 */
198static int abortboot_keyed(int bootdelay)
199{
200 int abort;
201 uint64_t etime = endtick(bootdelay);
202
203#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
204 if (bootdelay == 0)
205 return 0;
206#endif
207
208# ifdef CONFIG_AUTOBOOT_PROMPT
209 /*
210 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
211 * To print the bootdelay value upon bootup.
212 */
213 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
214# endif
215
216 abort = passwd_abort(etime);
Simon Glass66ded172014-04-10 20:01:28 -0600217 if (!abort)
218 debug_bootkeys("key timeout\n");
219
220#ifdef CONFIG_SILENT_CONSOLE
221 if (abort)
222 gd->flags &= ~GD_FLG_SILENT;
223#endif
224
225 return abort;
226}
227
228# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
229
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600230static int menukey = 0;
231
232/* Check if we've got CPConfig in the environment. If not, add it and save the environment */
233void fixup_environment(void)
234{
235 char * s = getenv("CPConfig");
236 if (s == NULL) {
237 printf("Found unsaved environment...\n");
238 setenv("CPConfig", "yes");
239 saveenv();
240 }
241}
242int validateUserSelection(unsigned char c){
243 char valid_min = '1';
244 char valid_max = '9' ;
245 if (c >= valid_min && c <= valid_max){
246 return 0;
247 }
248 return 1;
249}
250
Simon Glass66ded172014-04-10 20:01:28 -0600251
252static int abortboot_normal(int bootdelay)
253{
254 int abort = 0;
255 unsigned long ts;
256
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600257 fixup_environment();
258
259#if defined(CONFIG_DEBUG_ENABLE)
260 if (process_debug_package() == 0) {
261 printf("Successfully validated the debug enable package.\n");
262 } else {
263 printf("Debug package not found or otherwise invalid\n");
264 }
265#endif
266
Simon Glass66ded172014-04-10 20:01:28 -0600267#ifdef CONFIG_MENUPROMPT
268 printf(CONFIG_MENUPROMPT);
269#else
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600270 /* check_serial_console(); */
271 print_menu_selections();
Simon Glass66ded172014-04-10 20:01:28 -0600272 if (bootdelay >= 0)
273 printf("Hit any key to stop autoboot: %2d ", bootdelay);
274#endif
275
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600276
Simon Glass66ded172014-04-10 20:01:28 -0600277#if defined CONFIG_ZERO_BOOTDELAY_CHECK
278 /*
279 * Check if key already pressed
280 * Don't check if bootdelay < 0
281 */
282 if (bootdelay >= 0) {
283 if (tstc()) { /* we got a key press */
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600284 menukey = getc(); /* consume input */
285 if (!validateUserSelection(menukey)){
286 puts ("\b\b\b 0");
287 abort = 1; /* don't auto boot */
288 }
Simon Glass66ded172014-04-10 20:01:28 -0600289 }
290 }
291#endif
292
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600293 check_factory_reset();
Simon Glass66ded172014-04-10 20:01:28 -0600294 while ((bootdelay > 0) && (!abort)) {
295 --bootdelay;
296 /* delay 1000 ms */
297 ts = get_timer(0);
298 do {
299 if (tstc()) { /* we got a key press */
Simon Glass66ded172014-04-10 20:01:28 -0600300 menukey = getc();
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600301 printf("You pressed %c\n", (char) menukey);
302 if (!validateUserSelection((char)menukey)){
303 abort = 1; /* don't auto boot */
304 bootdelay = 0; /* no more delay */
305 break;
306 }
Simon Glass66ded172014-04-10 20:01:28 -0600307 }
308 udelay(10000);
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600309 check_factory_reset();
Simon Glass66ded172014-04-10 20:01:28 -0600310 } while (!abort && get_timer(ts) < 1000);
311
312 printf("\b\b\b%2d ", bootdelay);
313 }
314
315 putc('\n');
316
317#ifdef CONFIG_SILENT_CONSOLE
318 if (abort)
319 gd->flags &= ~GD_FLG_SILENT;
320#endif
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600321 if (abort)
322 process_menu_selection(menukey);
323 else
324 process_menu_selection('3');
325
Simon Glass66ded172014-04-10 20:01:28 -0600326 return abort;
327}
328# endif /* CONFIG_AUTOBOOT_KEYED */
329
330static int abortboot(int bootdelay)
331{
332#ifdef CONFIG_AUTOBOOT_KEYED
333 return abortboot_keyed(bootdelay);
334#else
335 return abortboot_normal(bootdelay);
336#endif
337}
338
Simon Glass66ded172014-04-10 20:01:28 -0600339static void process_fdt_options(const void *blob)
340{
Simon Glassaffb2152014-04-10 20:01:35 -0600341#if defined(CONFIG_OF_CONTROL)
Simon Glass66ded172014-04-10 20:01:28 -0600342 ulong addr;
343
344 /* Add an env variable to point to a kernel payload, if available */
345 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
346 if (addr)
347 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
348
349 /* Add an env variable to point to a root disk, if available */
350 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
351 if (addr)
352 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass66ded172014-04-10 20:01:28 -0600353#endif /* CONFIG_OF_CONTROL */
Simon Glassaffb2152014-04-10 20:01:35 -0600354}
Simon Glass66ded172014-04-10 20:01:28 -0600355
Simon Glassaffb2152014-04-10 20:01:35 -0600356const char *bootdelay_process(void)
Simon Glass66ded172014-04-10 20:01:28 -0600357{
Simon Glass66ded172014-04-10 20:01:28 -0600358 char *s;
359 int bootdelay;
360#ifdef CONFIG_BOOTCOUNT_LIMIT
361 unsigned long bootcount = 0;
362 unsigned long bootlimit = 0;
363#endif /* CONFIG_BOOTCOUNT_LIMIT */
364
365#ifdef CONFIG_BOOTCOUNT_LIMIT
366 bootcount = bootcount_load();
367 bootcount++;
368 bootcount_store(bootcount);
369 setenv_ulong("bootcount", bootcount);
370 bootlimit = getenv_ulong("bootlimit", 10, 0);
371#endif /* CONFIG_BOOTCOUNT_LIMIT */
372
373 s = getenv("bootdelay");
374 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
375
376#ifdef CONFIG_OF_CONTROL
377 bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
378 bootdelay);
379#endif
380
381 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
382
383#if defined(CONFIG_MENU_SHOW)
384 bootdelay = menu_show(bootdelay);
385#endif
Simon Glassb26440f2014-04-10 20:01:31 -0600386 bootretry_init_cmd_timeout();
Simon Glass66ded172014-04-10 20:01:28 -0600387
388#ifdef CONFIG_POST
389 if (gd->flags & GD_FLG_POSTFAIL) {
390 s = getenv("failbootcmd");
391 } else
392#endif /* CONFIG_POST */
393#ifdef CONFIG_BOOTCOUNT_LIMIT
394 if (bootlimit && (bootcount > bootlimit)) {
395 printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
396 (unsigned)bootlimit);
397 s = getenv("altbootcmd");
398 } else
399#endif /* CONFIG_BOOTCOUNT_LIMIT */
400 s = getenv("bootcmd");
Simon Glass66ded172014-04-10 20:01:28 -0600401
402 process_fdt_options(gd->fdt_blob);
Simon Glassaffb2152014-04-10 20:01:35 -0600403 stored_bootdelay = bootdelay;
Simon Glass66ded172014-04-10 20:01:28 -0600404
Simon Glassaffb2152014-04-10 20:01:35 -0600405 return s;
406}
Simon Glass66ded172014-04-10 20:01:28 -0600407
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530408__weak int apps_iscrashed(void)
409{
410 return 0;
411}
412
speriaka98e44092019-08-27 11:27:13 +0530413__weak int apps_iscrashed_crashdump_disabled(void)
414{
415 return 0;
416}
417
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600418__weak void check_reset_button(void)
419{
420 printf("This is where the reset button check would be running, if it were complete\n");
421 return;
422}
Simon Glassaffb2152014-04-10 20:01:35 -0600423void autoboot_command(const char *s)
424{
Simon Glass66ded172014-04-10 20:01:28 -0600425 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
426
Kyle Swenson3c6e30f2021-03-29 09:52:45 -0600427 char * skip = NULL;
428 skip = getenv("skip_usb_passthrough");
429 if (!(skip != NULL && strcmp(skip, "yes") == 0)) {
430 printf("Doing usbpassthrough, since skip_usb_passthrough isn't set to yes\n");
431 usb_passthrough();
432 }
433
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530434#ifdef CONFIG_QCA_APPSBL_DLOAD
435 /*
436 * If kernel has crashed in previous boot,
437 * jump to crash dump collection.
438 */
439 if (apps_iscrashed()) {
440 printf("Crashdump magic found, initializing dump activity..\n");
Gokul Sriram Palanisamyebfe5532018-01-29 13:18:16 +0530441 s = getenv("dump_to_flash");
Venkat Raju Sanad0efe152019-07-22 20:31:38 -0700442 if (!s) {
Venkat Raju Sana2e7684f2019-03-11 19:29:46 -0700443 s = getenv("dump_minimal");
Venkat Raju Sanad0efe152019-07-22 20:31:38 -0700444 if (s) {
445 if (strncmp(s, "1", sizeof("1"))) {
446 printf("\nError: Invalid variable dump_minimal \n");
447 reset_board();
448 }
449 }
450 }
Sandhya0b984e92019-06-26 10:24:59 +0530451 if (s) {
Venkat Raju Sanad0efe152019-07-22 20:31:38 -0700452 do_dumpqca_minimal_data(s);
Antony Arun T6cc115d2019-07-19 16:13:50 +0530453 reset_board();
Sandhya0b984e92019-06-26 10:24:59 +0530454 }
Gokul Sriram Palanisamyebfe5532018-01-29 13:18:16 +0530455 else
Venkat Raju Sana2e7684f2019-03-11 19:29:46 -0700456 dump_func(FULL_DUMP);
Gokul Sriram Palanisamye1b91062017-12-20 17:57:24 +0530457 return;
458 }
459#endif
460
speriaka98e44092019-08-27 11:27:13 +0530461 if (apps_iscrashed_crashdump_disabled()) {
462 printf("Crashdump disabled, resetting the board..\n");
463 reset_board();
464 }
465
Simon Glassaffb2152014-04-10 20:01:35 -0600466 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
Simon Glass66ded172014-04-10 20:01:28 -0600467#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
468 int prev = disable_ctrlc(1); /* disable Control C checking */
469#endif
470
471 run_command_list(s, -1, 0);
472
473#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
474 disable_ctrlc(prev); /* restore Control C checking */
475#endif
476 }
477
478#ifdef CONFIG_MENUKEY
479 if (menukey == CONFIG_MENUKEY) {
480 s = getenv("menucmd");
481 if (s)
482 run_command_list(s, -1, 0);
483 }
484#endif /* CONFIG_MENUKEY */
Gokul Sriram Palanisamya23d3222017-12-21 11:50:24 +0530485
486#ifdef CONFIG_IPQ_ETH_INIT_DEFER
487 puts("\nNet: ");
488 eth_initialize();
489#endif
Simon Glass66ded172014-04-10 20:01:28 -0600490}