blob: 898a57bc92bda200840ba480d28cd85eb13a48d8 [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
Tom Rini728cb882024-04-27 08:11:00 -06007#include <config.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>
Tom Rini728cb882024-04-27 08:11:00 -060014#include <errno.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060015#include <fdtdec.h>
Simon Glass6a2e09f2019-07-20 20:51:16 -060016#include <hash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <malloc.h>
Heiko Schocher2ef3a952019-07-29 07:23:19 +020019#include <memalign.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060020#include <menu.h>
21#include <post.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070022#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Stefan Roese0ed2e462015-05-18 14:08:24 +020025#include <u-boot/sha256.h>
Lukasz Majewski4fc18912018-05-02 16:10:53 +020026#include <bootcount.h>
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020027#include <crypt.h>
Simon Glass0034d962021-08-07 07:24:01 -060028#include <dm/ofnode.h>
Simon Glass7b6a95a2014-04-10 20:01:28 -060029
30DECLARE_GLOBAL_DATA_PTR;
31
Steffen Jaeckel092c8c62021-07-08 15:57:36 +020032#define DELAY_STOP_STR_MAX_LENGTH 64
Simon Glass7b6a95a2014-04-10 20:01:28 -060033
34#ifndef DEBUG_BOOTKEYS
35#define DEBUG_BOOTKEYS 0
36#endif
37#define debug_bootkeys(fmt, args...) \
38 debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
39
Simon Glass5b47e302014-04-10 20:01:35 -060040/* Stored value of bootdelay, used by autoboot_command() */
41static int stored_bootdelay;
Simon Glass9d983852019-07-20 20:51:23 -060042static int menukey;
Simon Glass5b47e302014-04-10 20:01:35 -060043
Tom Rini0c23c5b2023-01-10 11:19:34 -050044#if defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT)
45#define AUTOBOOT_STOP_STR_CRYPT CONFIG_AUTOBOOT_STOP_STR_CRYPT
46#else
47#define AUTOBOOT_STOP_STR_CRYPT ""
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020048#endif
Tom Rini0c23c5b2023-01-10 11:19:34 -050049#if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
50#define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
51#else
52#define AUTOBOOT_STOP_STR_SHA256 ""
Simon Glass760d3fb2019-07-20 20:51:15 -060053#endif
54
Da Xue3769e552021-06-21 22:39:19 -040055#ifdef CONFIG_AUTOBOOT_USE_MENUKEY
Da Xue4387f2e2021-07-02 12:32:43 -040056#define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY
Simon Glass9d983852019-07-20 20:51:23 -060057#else
58#define AUTOBOOT_MENUKEY 0
59#endif
60
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020061/**
62 * passwd_abort_crypt() - check for a crypt-style hashed key sequence to abort booting
63 *
64 * This checks for the user entering a password within a given time.
65 *
66 * The entered password is hashed via one of the crypt-style hash methods
67 * and compared to the pre-defined value from either
68 * the environment variable "bootstopkeycrypt"
69 * or
70 * the config value CONFIG_AUTOBOOT_STOP_STR_CRYPT
71 *
Steffen Jaeckel792a13f2021-07-08 15:57:37 +020072 * In case the config value CONFIG_AUTOBOOT_NEVER_TIMEOUT has been enabled
73 * this function never times out if the user presses the <Enter> key
74 * before starting to enter the password.
75 *
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020076 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010077 * Return: 0 if autoboot should continue, 1 if it should stop
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020078 */
79static int passwd_abort_crypt(uint64_t etime)
80{
81 const char *crypt_env_str = env_get("bootstopkeycrypt");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +020082 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020083 u_int presskey_len = 0;
84 int abort = 0;
Steffen Jaeckel792a13f2021-07-08 15:57:37 +020085 int never_timeout = 0;
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020086 int err;
87
88 if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str)
Tom Rini0c23c5b2023-01-10 11:19:34 -050089 crypt_env_str = AUTOBOOT_STOP_STR_CRYPT;
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020090
91 if (!crypt_env_str)
92 return 0;
93
94 /* We expect the stop-string to be newline-terminated */
95 do {
96 if (tstc()) {
97 /* Check for input string overflow */
98 if (presskey_len >= sizeof(presskey))
99 return 0;
100
101 presskey[presskey_len] = getchar();
102
103 if ((presskey[presskey_len] == '\r') ||
104 (presskey[presskey_len] == '\n')) {
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200105 if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) &&
106 !presskey_len) {
107 never_timeout = 1;
108 continue;
109 }
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200110 presskey[presskey_len] = '\0';
111 err = crypt_compare(crypt_env_str, presskey,
112 &abort);
113 if (err)
114 debug_bootkeys(
115 "crypt_compare() failed with: %s\n",
116 errno_str(err));
117 /* you had one chance */
118 break;
119 } else {
120 presskey_len++;
121 }
122 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200123 udelay(10000);
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200124 } while (never_timeout || get_ticks() <= etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200125
126 return abort;
127}
128
Stefan Roese0ed2e462015-05-18 14:08:24 +0200129/*
130 * Use a "constant-length" time compare function for this
131 * hash compare:
132 *
133 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -0600134 */
Stefan Roese0ed2e462015-05-18 14:08:24 +0200135static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600136{
Stefan Roese0ed2e462015-05-18 14:08:24 +0200137 int diff = 0;
138 int i;
139
140 for (i = 0; i < len; i++)
141 diff |= a[i] ^ b[i];
142
143 return diff == 0;
144}
145
Simon Glassa8cab882019-07-20 20:51:17 -0600146/**
147 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
148 *
149 * This checks for the user entering a SHA256 hash within a given time.
150 *
151 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100152 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600153 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600154static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200155{
Simon Glass64b723f2017-08-03 12:22:12 -0600156 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +0200157 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200158 u8 *sha;
159 char *presskey;
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800160 char *c;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200161 const char *algo_name = "sha256";
162 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600163 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +0100164 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200165 int ret;
166
167 if (sha_env_str == NULL)
Tom Rini0c23c5b2023-01-10 11:19:34 -0500168 sha_env_str = AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200169
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200170 presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH);
Maks Mishinc5bd2892024-03-01 01:32:11 +0300171 if (!presskey)
172 return -ENOMEM;
173
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800174 c = strstr(sha_env_str, ":");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200175 if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800176 /* preload presskey with salt */
177 memcpy(presskey, sha_env_str, c - sha_env_str);
178 presskey_len = c - sha_env_str;
179 sha_env_str = c + 1;
180 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200181 /*
182 * Generate the binary value from the environment hash value
183 * so that we can compare this value with the computed hash
184 * from the user input
185 */
186 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
187 if (ret) {
188 printf("Hash %s not supported!\n", algo_name);
189 return 0;
190 }
191
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200192 sha = malloc_cache_aligned(SHA256_SUM_LEN);
193 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200194 /*
195 * We don't know how long the stop-string is, so we need to
196 * generate the sha256 hash upon each input character and
197 * compare the value with the one saved in the environment
198 */
199 do {
200 if (tstc()) {
201 /* Check for input string overflow */
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200202 if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200203 free(presskey);
204 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200205 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200206 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200207
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200208 presskey[presskey_len++] = getchar();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200209
210 /* Calculate sha256 upon each new char */
211 hash_block(algo_name, (const void *)presskey,
212 presskey_len, sha, &size);
213
214 /* And check if sha matches saved value in env */
215 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
216 abort = 1;
217 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200218 udelay(10000);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200219 } while (!abort && get_ticks() <= etime);
220
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200221 free(presskey);
222 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200223 return abort;
224}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600225
Simon Glassa8cab882019-07-20 20:51:17 -0600226/**
227 * passwd_abort_key() - check for a key sequence to aborted booting
228 *
229 * This checks for the user entering a string within a given time.
230 *
231 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100232 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600233 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600234static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200235{
236 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600237 struct {
238 char *str;
239 u_int len;
240 int retry;
241 }
242 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600243 { .str = env_get("bootdelaykey"), .retry = 1 },
244 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600245 };
246
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200247 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Yuezhang.Mo@sony.comd8eeb0f2021-01-15 03:11:49 +0000248 int presskey_len = 0;
249 int presskey_max = 0;
250 int i;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600251
Simon Glass7b6a95a2014-04-10 20:01:28 -0600252# ifdef CONFIG_AUTOBOOT_DELAY_STR
253 if (delaykey[0].str == NULL)
254 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
255# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600256# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200257 if (delaykey[1].str == NULL)
258 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600259# endif
260
261 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
262 delaykey[i].len = delaykey[i].str == NULL ?
263 0 : strlen(delaykey[i].str);
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200264 delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
265 DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600266
267 presskey_max = presskey_max > delaykey[i].len ?
268 presskey_max : delaykey[i].len;
269
270 debug_bootkeys("%s key:<%s>\n",
271 delaykey[i].retry ? "delay" : "stop",
272 delaykey[i].str ? delaykey[i].str : "NULL");
273 }
274
275 /* In order to keep up with incoming data, check timeout only
276 * when catch up.
277 */
278 do {
279 if (tstc()) {
280 if (presskey_len < presskey_max) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200281 presskey[presskey_len++] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600282 } else {
283 for (i = 0; i < presskey_max - 1; i++)
284 presskey[i] = presskey[i + 1];
285
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200286 presskey[i] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600287 }
288 }
289
290 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
291 if (delaykey[i].len > 0 &&
292 presskey_len >= delaykey[i].len &&
293 memcmp(presskey + presskey_len -
294 delaykey[i].len, delaykey[i].str,
295 delaykey[i].len) == 0) {
296 debug_bootkeys("got %skey\n",
297 delaykey[i].retry ? "delay" :
298 "stop");
299
Simon Glass7b6a95a2014-04-10 20:01:28 -0600300 /* don't retry auto boot */
301 if (!delaykey[i].retry)
302 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600303 abort = 1;
304 }
305 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200306 udelay(10000);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600307 } while (!abort && get_ticks() <= etime);
308
Stefan Roese0ed2e462015-05-18 14:08:24 +0200309 return abort;
310}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200311
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200312/**
313 * flush_stdin() - drops all pending characters from stdin
314 */
315static void flush_stdin(void)
316{
317 while (tstc())
318 (void)getchar();
319}
320
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200321/**
322 * fallback_to_sha256() - check whether we should fall back to sha256
323 * password checking
324 *
325 * This checks for the environment variable `bootstopusesha256` in case
326 * sha256-fallback has been enabled via the config setting
327 * `AUTOBOOT_SHA256_FALLBACK`.
328 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100329 * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200330 */
331static bool fallback_to_sha256(void)
332{
333 if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
334 return env_get_yesno("bootstopusesha256") == 1;
335 else if (IS_ENABLED(CONFIG_CRYPT_PW))
336 return false;
337 else
338 return true;
339}
340
Stefan Roese0ed2e462015-05-18 14:08:24 +0200341/***************************************************************************
342 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
343 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
344 */
Simon Glass6d99d132019-07-20 20:51:19 -0600345static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200346{
347 int abort;
348 uint64_t etime = endtick(bootdelay);
349
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200350 if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
351 flush_stdin();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200352# ifdef CONFIG_AUTOBOOT_PROMPT
353 /*
354 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
355 * To print the bootdelay value upon bootup.
356 */
357 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
358# endif
359
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200360 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200361 if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200362 abort = passwd_abort_crypt(etime);
363 else
364 abort = passwd_abort_sha256(etime);
365 } else {
Simon Glass6a2e09f2019-07-20 20:51:16 -0600366 abort = passwd_abort_key(etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200367 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600368 if (!abort)
369 debug_bootkeys("key timeout\n");
370
Simon Glass7b6a95a2014-04-10 20:01:28 -0600371 return abort;
372}
373
Simon Glass6d99d132019-07-20 20:51:19 -0600374static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600375{
376 int abort = 0;
377 unsigned long ts;
378
Masahiro Yamada9a333862016-06-27 16:23:04 +0900379 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600380
Simon Glass7b6a95a2014-04-10 20:01:28 -0600381 /*
382 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600383 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900384 if (tstc()) { /* we got a key press */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200385 getchar(); /* consume input */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900386 puts("\b\b\b 0");
387 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600388 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600389
390 while ((bootdelay > 0) && (!abort)) {
391 --bootdelay;
392 /* delay 1000 ms */
393 ts = get_timer(0);
394 do {
395 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600396 int key;
397
Simon Glass7b6a95a2014-04-10 20:01:28 -0600398 abort = 1; /* don't auto boot */
399 bootdelay = 0; /* no more delay */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200400 key = getchar();/* consume input */
Da Xue3769e552021-06-21 22:39:19 -0400401 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
Simon Glass9d983852019-07-20 20:51:23 -0600402 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600403 break;
404 }
405 udelay(10000);
406 } while (!abort && get_timer(ts) < 1000);
407
408 printf("\b\b\b%2d ", bootdelay);
409 }
410
411 putc('\n');
412
Simon Glass7b6a95a2014-04-10 20:01:28 -0600413 return abort;
414}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600415
416static int abortboot(int bootdelay)
417{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900418 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900419
Simon Glass6d99d132019-07-20 20:51:19 -0600420 if (bootdelay >= 0) {
Simon Glassd8c60172021-07-24 15:14:39 -0600421 if (autoboot_keyed())
Simon Glass6d99d132019-07-20 20:51:19 -0600422 abort = abortboot_key_sequence(bootdelay);
423 else
424 abort = abortboot_single_key(bootdelay);
425 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900426
Simon Glass3a315052019-07-20 20:51:18 -0600427 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900428 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900429
430 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600431}
432
Shenlin Liangbb0f62a2022-12-02 12:53:48 +0800433static void process_fdt_options(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600434{
Simon Glass72cc5382022-10-20 18:22:39 -0600435#ifdef CONFIG_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600436 ulong addr;
437
438 /* Add an env variable to point to a kernel payload, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600439 addr = ofnode_conf_read_int("kernel-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600440 if (addr)
Simon Glass72cc5382022-10-20 18:22:39 -0600441 env_set_addr("kernaddr", (void *)(CONFIG_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600442
443 /* Add an env variable to point to a root disk, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600444 addr = ofnode_conf_read_int("rootdisk-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600445 if (addr)
Simon Glass72cc5382022-10-20 18:22:39 -0600446 env_set_addr("rootaddr", (void *)(CONFIG_TEXT_BASE + addr));
447#endif /* CONFIG_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600448}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600449
Simon Glass5b47e302014-04-10 20:01:35 -0600450const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600451{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600452 char *s;
453 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600454
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200455 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600456
Simon Glass64b723f2017-08-03 12:22:12 -0600457 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600458 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
459
Simon Glassb8ffa142022-03-11 16:22:38 -0700460 /*
461 * Does it really make sense that the devicetree overrides the user
462 * setting? It is possibly helpful for security since the device tree
463 * may be signed whereas the environment is often loaded from storage.
464 */
Simon Glassb97ace52019-07-20 20:51:27 -0600465 if (IS_ENABLED(CONFIG_OF_CONTROL))
Simon Glass0034d962021-08-07 07:24:01 -0600466 bootdelay = ofnode_conf_read_int("bootdelay", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600467
468 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
469
Simon Glassb97ace52019-07-20 20:51:27 -0600470 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
471 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600472 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600473
474#ifdef CONFIG_POST
475 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600476 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600477 } else
478#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200479 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600480 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200481 else
Simon Glass64b723f2017-08-03 12:22:12 -0600482 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600483
Simon Glassb97ace52019-07-20 20:51:27 -0600484 if (IS_ENABLED(CONFIG_OF_CONTROL))
Shenlin Liangbb0f62a2022-12-02 12:53:48 +0800485 process_fdt_options();
Simon Glass5b47e302014-04-10 20:01:35 -0600486 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600487
Simon Glass5b47e302014-04-10 20:01:35 -0600488 return s;
489}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600490
Simon Glass5b47e302014-04-10 20:01:35 -0600491void autoboot_command(const char *s)
492{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600493 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
494
Heiko Schocher12803652020-10-07 08:06:54 +0200495 if (s && (stored_bootdelay == -2 ||
496 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600497 bool lock;
498 int prev;
499
Simon Glassd8c60172021-07-24 15:14:39 -0600500 lock = autoboot_keyed() &&
Simon Glass313fcbd2019-07-20 20:51:28 -0600501 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
502 if (lock)
503 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600504
505 run_command_list(s, -1, 0);
506
Simon Glass313fcbd2019-07-20 20:51:28 -0600507 if (lock)
508 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600509 }
510
Da Xue3769e552021-06-21 22:39:19 -0400511 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
Simon Glass9d983852019-07-20 20:51:23 -0600512 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600513 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600514 if (s)
515 run_command_list(s, -1, 0);
516 }
Simon Glassd8c60172021-07-24 15:14:39 -0600517}