blob: 5d331991c1903f96bda428aa9b492f6b875e5fc6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7b6a95a2014-04-10 20:01:28 -06002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass7b6a95a2014-04-10 20:01:28 -06005 */
6
7#include <common.h>
Jeroen Hofsteef682d8c2014-07-13 22:57:58 +02008#include <autoboot.h>
Simon Glass399ed9a2014-04-10 20:01:30 -06009#include <bootretry.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060010#include <cli.h>
Simon Glassadaaa482019-11-14 12:57:43 -070011#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060013#include <env.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060014#include <fdtdec.h>
Simon Glass6a2e09f2019-07-20 20:51:16 -060015#include <hash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Heiko Schocher2ef3a952019-07-29 07:23:19 +020018#include <memalign.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060019#include <menu.h>
20#include <post.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070021#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020024#include <u-boot/sha256.h>
Lukasz Majewski4fc18912018-05-02 16:10:53 +020025#include <bootcount.h>
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020026#include <crypt.h>
Simon Glass0034d962021-08-07 07:24:01 -060027#include <dm/ofnode.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060028
29DECLARE_GLOBAL_DATA_PTR;
30
Steffen Jaeckel092c8c62021-07-08 15:57:36 +020031#define DELAY_STOP_STR_MAX_LENGTH 64
Simon Glass7b6a95a2014-04-10 20:01:28 -060032
33#ifndef DEBUG_BOOTKEYS
34#define DEBUG_BOOTKEYS 0
35#endif
36#define debug_bootkeys(fmt, args...) \
37 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
38
Simon Glass5b47e302014-04-10 20:01:35 -060039/* Stored value of bootdelay, used by autoboot_command() */
40static int stored_bootdelay;
Simon Glass9d983852019-07-20 20:51:23 -060041static int menukey;
Simon Glass5b47e302014-04-10 20:01:35 -060042
Tom Rini0c23c5b2023-01-10 11:19:34 -050043#if defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT)
44#define AUTOBOOT_STOP_STR_CRYPT CONFIG_AUTOBOOT_STOP_STR_CRYPT
45#else
46#define AUTOBOOT_STOP_STR_CRYPT ""
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020047#endif
Tom Rini0c23c5b2023-01-10 11:19:34 -050048#if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
49#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
50#else
51#define AUTOBOOT_STOP_STR_SHA256 ""
Simon Glass760d3fb2019-07-20 20:51:15 -060052#endif
53
Da Xue3769e552021-06-21 22:39:19 -040054#ifdef CONFIG_AUTOBOOT_USE_MENUKEY
Da Xue4387f2e2021-07-02 12:32:43 -040055#define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY
Simon Glass9d983852019-07-20 20:51:23 -060056#else
57#define AUTOBOOT_MENUKEY 0
58#endif
59
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020060/**
61 * passwd_abort_crypt() - check for a crypt-style hashed key sequence to abort booting
62 *
63 * This checks for the user entering a password within a given time.
64 *
65 * The entered password is hashed via one of the crypt-style hash methods
66 * and compared to the pre-defined value from either
67 * the environment variable "bootstopkeycrypt"
68 * or
69 * the config value CONFIG_AUTOBOOT_STOP_STR_CRYPT
70 *
Steffen Jaeckel792a13f2021-07-08 15:57:37 +020071 * In case the config value CONFIG_AUTOBOOT_NEVER_TIMEOUT has been enabled
72 * this function never times out if the user presses the <Enter> key
73 * before starting to enter the password.
74 *
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020075 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010076 * Return: 0 if autoboot should continue, 1 if it should stop
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020077 */
78static int passwd_abort_crypt(uint64_t etime)
79{
80 const char *crypt_env_str = env_get("bootstopkeycrypt");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +020081 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020082 u_int presskey_len = 0;
83 int abort = 0;
Steffen Jaeckel792a13f2021-07-08 15:57:37 +020084 int never_timeout = 0;
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020085 int err;
86
87 if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str)
Tom Rini0c23c5b2023-01-10 11:19:34 -050088 crypt_env_str = AUTOBOOT_STOP_STR_CRYPT;
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020089
90 if (!crypt_env_str)
91 return 0;
92
93 /* We expect the stop-string to be newline-terminated */
94 do {
95 if (tstc()) {
96 /* Check for input string overflow */
97 if (presskey_len >= sizeof(presskey))
98 return 0;
99
100 presskey[presskey_len] = getchar();
101
102 if ((presskey[presskey_len] == '\r') ||
103 (presskey[presskey_len] == '\n')) {
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200104 if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) &&
105 !presskey_len) {
106 never_timeout = 1;
107 continue;
108 }
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200109 presskey[presskey_len] = '\0';
110 err = crypt_compare(crypt_env_str, presskey,
111 &abort);
112 if (err)
113 debug_bootkeys(
114 "crypt_compare() failed with: %s\n",
115 errno_str(err));
116 /* you had one chance */
117 break;
118 } else {
119 presskey_len++;
120 }
121 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200122 udelay(10000);
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200123 } while (never_timeout || get_ticks() <= etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200124
125 return abort;
126}
127
Stefan Roese0ed2e462015-05-18 14:08:24 +0200128/*
129 * Use a "constant-length" time compare function for this
130 * hash compare:
131 *
132 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -0600133 */
Stefan Roese0ed2e462015-05-18 14:08:24 +0200134static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600135{
Stefan Roese0ed2e462015-05-18 14:08:24 +0200136 int diff = 0;
137 int i;
138
139 for (i = 0; i < len; i++)
140 diff |= a[i] ^ b[i];
141
142 return diff == 0;
143}
144
Simon Glassa8cab882019-07-20 20:51:17 -0600145/**
146 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
147 *
148 * This checks for the user entering a SHA256 hash within a given time.
149 *
150 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100151 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600152 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600153static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200154{
Simon Glass64b723f2017-08-03 12:22:12 -0600155 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +0200156 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200157 u8 *sha;
158 char *presskey;
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800159 char *c;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200160 const char *algo_name = "sha256";
161 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600162 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +0100163 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200164 int ret;
165
166 if (sha_env_str == NULL)
Tom Rini0c23c5b2023-01-10 11:19:34 -0500167 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200168
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200169 presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH);
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800170 c = strstr(sha_env_str, ":");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200171 if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800172 /* preload presskey with salt */
173 memcpy(presskey, sha_env_str, c - sha_env_str);
174 presskey_len = c - sha_env_str;
175 sha_env_str = c + 1;
176 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200177 /*
178 * Generate the binary value from the environment hash value
179 * so that we can compare this value with the computed hash
180 * from the user input
181 */
182 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
183 if (ret) {
184 printf("Hash %s not supported!\n", algo_name);
185 return 0;
186 }
187
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200188 sha = malloc_cache_aligned(SHA256_SUM_LEN);
189 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200190 /*
191 * We don't know how long the stop-string is, so we need to
192 * generate the sha256 hash upon each input character and
193 * compare the value with the one saved in the environment
194 */
195 do {
196 if (tstc()) {
197 /* Check for input string overflow */
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200198 if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200199 free(presskey);
200 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200201 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200202 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200203
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200204 presskey[presskey_len++] = getchar();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200205
206 /* Calculate sha256 upon each new char */
207 hash_block(algo_name, (const void *)presskey,
208 presskey_len, sha, &size);
209
210 /* And check if sha matches saved value in env */
211 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
212 abort = 1;
213 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200214 udelay(10000);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200215 } while (!abort && get_ticks() <= etime);
216
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200217 free(presskey);
218 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200219 return abort;
220}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600221
Simon Glassa8cab882019-07-20 20:51:17 -0600222/**
223 * passwd_abort_key() - check for a key sequence to aborted booting
224 *
225 * This checks for the user entering a string within a given time.
226 *
227 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100228 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600229 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600230static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200231{
232 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600233 struct {
234 char *str;
235 u_int len;
236 int retry;
237 }
238 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600239 { .str = env_get("bootdelaykey"), .retry = 1 },
240 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600241 };
242
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200243 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Yuezhang.Mo@sony.comd8eeb0f2021-01-15 03:11:49 +0000244 int presskey_len = 0;
245 int presskey_max = 0;
246 int i;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600247
Simon Glass7b6a95a2014-04-10 20:01:28 -0600248# ifdef CONFIG_AUTOBOOT_DELAY_STR
249 if (delaykey[0].str == NULL)
250 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
251# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600252# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200253 if (delaykey[1].str == NULL)
254 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600255# endif
256
257 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
258 delaykey[i].len = delaykey[i].str == NULL ?
259 0 : strlen(delaykey[i].str);
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200260 delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
261 DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600262
263 presskey_max = presskey_max > delaykey[i].len ?
264 presskey_max : delaykey[i].len;
265
266 debug_bootkeys("%s key:<%s>\n",
267 delaykey[i].retry ? "delay" : "stop",
268 delaykey[i].str ? delaykey[i].str : "NULL");
269 }
270
271 /* In order to keep up with incoming data, check timeout only
272 * when catch up.
273 */
274 do {
275 if (tstc()) {
276 if (presskey_len < presskey_max) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200277 presskey[presskey_len++] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600278 } else {
279 for (i = 0; i < presskey_max - 1; i++)
280 presskey[i] = presskey[i + 1];
281
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200282 presskey[i] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600283 }
284 }
285
286 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
287 if (delaykey[i].len > 0 &&
288 presskey_len >= delaykey[i].len &&
289 memcmp(presskey + presskey_len -
290 delaykey[i].len, delaykey[i].str,
291 delaykey[i].len) == 0) {
292 debug_bootkeys("got %skey\n",
293 delaykey[i].retry ? "delay" :
294 "stop");
295
Simon Glass7b6a95a2014-04-10 20:01:28 -0600296 /* don't retry auto boot */
297 if (!delaykey[i].retry)
298 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600299 abort = 1;
300 }
301 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200302 udelay(10000);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600303 } while (!abort && get_ticks() <= etime);
304
Stefan Roese0ed2e462015-05-18 14:08:24 +0200305 return abort;
306}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200307
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200308/**
309 * flush_stdin() - drops all pending characters from stdin
310 */
311static void flush_stdin(void)
312{
313 while (tstc())
314 (void)getchar();
315}
316
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200317/**
318 * fallback_to_sha256() - check whether we should fall back to sha256
319 * password checking
320 *
321 * This checks for the environment variable `bootstopusesha256` in case
322 * sha256-fallback has been enabled via the config setting
323 * `AUTOBOOT_SHA256_FALLBACK`.
324 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100325 * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200326 */
327static bool fallback_to_sha256(void)
328{
329 if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
330 return env_get_yesno("bootstopusesha256") == 1;
331 else if (IS_ENABLED(CONFIG_CRYPT_PW))
332 return false;
333 else
334 return true;
335}
336
Stefan Roese0ed2e462015-05-18 14:08:24 +0200337/***************************************************************************
338 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
339 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
340 */
Simon Glass6d99d132019-07-20 20:51:19 -0600341static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200342{
343 int abort;
344 uint64_t etime = endtick(bootdelay);
345
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200346 if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
347 flush_stdin();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200348# ifdef CONFIG_AUTOBOOT_PROMPT
349 /*
350 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
351 * To print the bootdelay value upon bootup.
352 */
353 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
354# endif
355
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200356 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200357 if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200358 abort = passwd_abort_crypt(etime);
359 else
360 abort = passwd_abort_sha256(etime);
361 } else {
Simon Glass6a2e09f2019-07-20 20:51:16 -0600362 abort = passwd_abort_key(etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200363 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600364 if (!abort)
365 debug_bootkeys("key timeout\n");
366
Simon Glass7b6a95a2014-04-10 20:01:28 -0600367 return abort;
368}
369
Simon Glass6d99d132019-07-20 20:51:19 -0600370static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600371{
372 int abort = 0;
373 unsigned long ts;
374
Masahiro Yamada9a333862016-06-27 16:23:04 +0900375 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600376
Simon Glass7b6a95a2014-04-10 20:01:28 -0600377 /*
378 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600379 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900380 if (tstc()) { /* we got a key press */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200381 getchar(); /* consume input */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900382 puts("\b\b\b 0");
383 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600384 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600385
386 while ((bootdelay > 0) && (!abort)) {
387 --bootdelay;
388 /* delay 1000 ms */
389 ts = get_timer(0);
390 do {
391 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600392 int key;
393
Simon Glass7b6a95a2014-04-10 20:01:28 -0600394 abort = 1; /* don't auto boot */
395 bootdelay = 0; /* no more delay */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200396 key = getchar();/* consume input */
Da Xue3769e552021-06-21 22:39:19 -0400397 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
Simon Glass9d983852019-07-20 20:51:23 -0600398 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600399 break;
400 }
401 udelay(10000);
402 } while (!abort && get_timer(ts) < 1000);
403
404 printf("\b\b\b%2d ", bootdelay);
405 }
406
407 putc('\n');
408
Simon Glass7b6a95a2014-04-10 20:01:28 -0600409 return abort;
410}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600411
412static int abortboot(int bootdelay)
413{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900414 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900415
Simon Glass6d99d132019-07-20 20:51:19 -0600416 if (bootdelay >= 0) {
Simon Glassd8c60172021-07-24 15:14:39 -0600417 if (autoboot_keyed())
Simon Glass6d99d132019-07-20 20:51:19 -0600418 abort = abortboot_key_sequence(bootdelay);
419 else
420 abort = abortboot_single_key(bootdelay);
421 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900422
Simon Glass3a315052019-07-20 20:51:18 -0600423 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900424 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900425
426 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600427}
428
Shenlin Liangbb0f62a2022-12-02 12:53:48 +0800429static void process_fdt_options(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600430{
Simon Glass72cc5382022-10-20 18:22:39 -0600431#ifdef CONFIG_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600432 ulong addr;
433
434 /* Add an env variable to point to a kernel payload, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600435 addr = ofnode_conf_read_int("kernel-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600436 if (addr)
Simon Glass72cc5382022-10-20 18:22:39 -0600437 env_set_addr("kernaddr", (void *)(CONFIG_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600438
439 /* Add an env variable to point to a root disk, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600440 addr = ofnode_conf_read_int("rootdisk-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600441 if (addr)
Simon Glass72cc5382022-10-20 18:22:39 -0600442 env_set_addr("rootaddr", (void *)(CONFIG_TEXT_BASE + addr));
443#endif /* CONFIG_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600444}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600445
Simon Glass5b47e302014-04-10 20:01:35 -0600446const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600447{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600448 char *s;
449 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600450
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200451 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600452
Simon Glass64b723f2017-08-03 12:22:12 -0600453 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600454 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
455
Simon Glassb8ffa142022-03-11 16:22:38 -0700456 /*
457 * Does it really make sense that the devicetree overrides the user
458 * setting? It is possibly helpful for security since the device tree
459 * may be signed whereas the environment is often loaded from storage.
460 */
Simon Glassb97ace52019-07-20 20:51:27 -0600461 if (IS_ENABLED(CONFIG_OF_CONTROL))
Simon Glass0034d962021-08-07 07:24:01 -0600462 bootdelay = ofnode_conf_read_int("bootdelay", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600463
464 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
465
Simon Glassb97ace52019-07-20 20:51:27 -0600466 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
467 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600468 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600469
470#ifdef CONFIG_POST
471 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600472 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600473 } else
474#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200475 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600476 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200477 else
Simon Glass64b723f2017-08-03 12:22:12 -0600478 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600479
Simon Glassb97ace52019-07-20 20:51:27 -0600480 if (IS_ENABLED(CONFIG_OF_CONTROL))
Shenlin Liangbb0f62a2022-12-02 12:53:48 +0800481 process_fdt_options();
Simon Glass5b47e302014-04-10 20:01:35 -0600482 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600483
Simon Glass5b47e302014-04-10 20:01:35 -0600484 return s;
485}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600486
Simon Glass5b47e302014-04-10 20:01:35 -0600487void autoboot_command(const char *s)
488{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600489 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
490
Heiko Schocher12803652020-10-07 08:06:54 +0200491 if (s && (stored_bootdelay == -2 ||
492 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600493 bool lock;
494 int prev;
495
Simon Glassd8c60172021-07-24 15:14:39 -0600496 lock = autoboot_keyed() &&
Simon Glass313fcbd2019-07-20 20:51:28 -0600497 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
498 if (lock)
499 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600500
501 run_command_list(s, -1, 0);
502
Simon Glass313fcbd2019-07-20 20:51:28 -0600503 if (lock)
504 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600505 }
506
Da Xue3769e552021-06-21 22:39:19 -0400507 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
Simon Glass9d983852019-07-20 20:51:23 -0600508 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600509 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600510 if (s)
511 run_command_list(s, -1, 0);
512 }
Simon Glassd8c60172021-07-24 15:14:39 -0600513}