blob: cdafe76309b222cd2b2b91fe2f80916bd997b040 [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
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020043#if !defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT)
44#define CONFIG_AUTOBOOT_STOP_STR_CRYPT ""
45#endif
46#if !defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
47#define CONFIG_AUTOBOOT_STOP_STR_SHA256 ""
Simon Glass760d3fb2019-07-20 20:51:15 -060048#endif
49
Da Xue3769e552021-06-21 22:39:19 -040050#ifdef CONFIG_AUTOBOOT_USE_MENUKEY
Da Xue4387f2e2021-07-02 12:32:43 -040051#define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY
Simon Glass9d983852019-07-20 20:51:23 -060052#else
53#define AUTOBOOT_MENUKEY 0
54#endif
55
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020056/**
57 * passwd_abort_crypt() - check for a crypt-style hashed key sequence to abort booting
58 *
59 * This checks for the user entering a password within a given time.
60 *
61 * The entered password is hashed via one of the crypt-style hash methods
62 * and compared to the pre-defined value from either
63 * the environment variable "bootstopkeycrypt"
64 * or
65 * the config value CONFIG_AUTOBOOT_STOP_STR_CRYPT
66 *
Steffen Jaeckel792a13f2021-07-08 15:57:37 +020067 * In case the config value CONFIG_AUTOBOOT_NEVER_TIMEOUT has been enabled
68 * this function never times out if the user presses the <Enter> key
69 * before starting to enter the password.
70 *
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020071 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010072 * Return: 0 if autoboot should continue, 1 if it should stop
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020073 */
74static int passwd_abort_crypt(uint64_t etime)
75{
76 const char *crypt_env_str = env_get("bootstopkeycrypt");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +020077 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020078 u_int presskey_len = 0;
79 int abort = 0;
Steffen Jaeckel792a13f2021-07-08 15:57:37 +020080 int never_timeout = 0;
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +020081 int err;
82
83 if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str)
84 crypt_env_str = CONFIG_AUTOBOOT_STOP_STR_CRYPT;
85
86 if (!crypt_env_str)
87 return 0;
88
89 /* We expect the stop-string to be newline-terminated */
90 do {
91 if (tstc()) {
92 /* Check for input string overflow */
93 if (presskey_len >= sizeof(presskey))
94 return 0;
95
96 presskey[presskey_len] = getchar();
97
98 if ((presskey[presskey_len] == '\r') ||
99 (presskey[presskey_len] == '\n')) {
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200100 if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) &&
101 !presskey_len) {
102 never_timeout = 1;
103 continue;
104 }
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200105 presskey[presskey_len] = '\0';
106 err = crypt_compare(crypt_env_str, presskey,
107 &abort);
108 if (err)
109 debug_bootkeys(
110 "crypt_compare() failed with: %s\n",
111 errno_str(err));
112 /* you had one chance */
113 break;
114 } else {
115 presskey_len++;
116 }
117 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200118 udelay(10000);
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200119 } while (never_timeout || get_ticks() <= etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200120
121 return abort;
122}
123
Stefan Roese0ed2e462015-05-18 14:08:24 +0200124/*
125 * Use a "constant-length" time compare function for this
126 * hash compare:
127 *
128 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -0600129 */
Stefan Roese0ed2e462015-05-18 14:08:24 +0200130static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600131{
Stefan Roese0ed2e462015-05-18 14:08:24 +0200132 int diff = 0;
133 int i;
134
135 for (i = 0; i < len; i++)
136 diff |= a[i] ^ b[i];
137
138 return diff == 0;
139}
140
Simon Glassa8cab882019-07-20 20:51:17 -0600141/**
142 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
143 *
144 * This checks for the user entering a SHA256 hash within a given time.
145 *
146 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100147 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600148 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600149static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200150{
Simon Glass64b723f2017-08-03 12:22:12 -0600151 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +0200152 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200153 u8 *sha;
154 char *presskey;
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800155 char *c;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200156 const char *algo_name = "sha256";
157 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600158 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +0100159 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200160 int ret;
161
162 if (sha_env_str == NULL)
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200163 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200164
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200165 presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH);
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800166 c = strstr(sha_env_str, ":");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200167 if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800168 /* preload presskey with salt */
169 memcpy(presskey, sha_env_str, c - sha_env_str);
170 presskey_len = c - sha_env_str;
171 sha_env_str = c + 1;
172 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200173 /*
174 * Generate the binary value from the environment hash value
175 * so that we can compare this value with the computed hash
176 * from the user input
177 */
178 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
179 if (ret) {
180 printf("Hash %s not supported!\n", algo_name);
181 return 0;
182 }
183
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200184 sha = malloc_cache_aligned(SHA256_SUM_LEN);
185 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200186 /*
187 * We don't know how long the stop-string is, so we need to
188 * generate the sha256 hash upon each input character and
189 * compare the value with the one saved in the environment
190 */
191 do {
192 if (tstc()) {
193 /* Check for input string overflow */
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200194 if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200195 free(presskey);
196 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200197 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200198 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200199
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200200 presskey[presskey_len++] = getchar();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200201
202 /* Calculate sha256 upon each new char */
203 hash_block(algo_name, (const void *)presskey,
204 presskey_len, sha, &size);
205
206 /* And check if sha matches saved value in env */
207 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
208 abort = 1;
209 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200210 udelay(10000);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200211 } while (!abort && get_ticks() <= etime);
212
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200213 free(presskey);
214 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200215 return abort;
216}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600217
Simon Glassa8cab882019-07-20 20:51:17 -0600218/**
219 * passwd_abort_key() - check for a key sequence to aborted booting
220 *
221 * This checks for the user entering a string within a given time.
222 *
223 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100224 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600225 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600226static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200227{
228 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600229 struct {
230 char *str;
231 u_int len;
232 int retry;
233 }
234 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600235 { .str = env_get("bootdelaykey"), .retry = 1 },
236 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600237 };
238
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200239 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Yuezhang.Mo@sony.comd8eeb0f2021-01-15 03:11:49 +0000240 int presskey_len = 0;
241 int presskey_max = 0;
242 int i;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600243
Simon Glass7b6a95a2014-04-10 20:01:28 -0600244# ifdef CONFIG_AUTOBOOT_DELAY_STR
245 if (delaykey[0].str == NULL)
246 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
247# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600248# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200249 if (delaykey[1].str == NULL)
250 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600251# endif
252
253 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
254 delaykey[i].len = delaykey[i].str == NULL ?
255 0 : strlen(delaykey[i].str);
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200256 delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
257 DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600258
259 presskey_max = presskey_max > delaykey[i].len ?
260 presskey_max : delaykey[i].len;
261
262 debug_bootkeys("%s key:<%s>\n",
263 delaykey[i].retry ? "delay" : "stop",
264 delaykey[i].str ? delaykey[i].str : "NULL");
265 }
266
267 /* In order to keep up with incoming data, check timeout only
268 * when catch up.
269 */
270 do {
271 if (tstc()) {
272 if (presskey_len < presskey_max) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200273 presskey[presskey_len++] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600274 } else {
275 for (i = 0; i < presskey_max - 1; i++)
276 presskey[i] = presskey[i + 1];
277
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200278 presskey[i] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600279 }
280 }
281
282 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
283 if (delaykey[i].len > 0 &&
284 presskey_len >= delaykey[i].len &&
285 memcmp(presskey + presskey_len -
286 delaykey[i].len, delaykey[i].str,
287 delaykey[i].len) == 0) {
288 debug_bootkeys("got %skey\n",
289 delaykey[i].retry ? "delay" :
290 "stop");
291
Simon Glass7b6a95a2014-04-10 20:01:28 -0600292 /* don't retry auto boot */
293 if (!delaykey[i].retry)
294 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600295 abort = 1;
296 }
297 }
Rasmus Villemoesdb794c22022-09-27 11:54:02 +0200298 udelay(10000);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600299 } while (!abort && get_ticks() <= etime);
300
Stefan Roese0ed2e462015-05-18 14:08:24 +0200301 return abort;
302}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200303
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200304/**
305 * flush_stdin() - drops all pending characters from stdin
306 */
307static void flush_stdin(void)
308{
309 while (tstc())
310 (void)getchar();
311}
312
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200313/**
314 * fallback_to_sha256() - check whether we should fall back to sha256
315 * password checking
316 *
317 * This checks for the environment variable `bootstopusesha256` in case
318 * sha256-fallback has been enabled via the config setting
319 * `AUTOBOOT_SHA256_FALLBACK`.
320 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100321 * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200322 */
323static bool fallback_to_sha256(void)
324{
325 if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
326 return env_get_yesno("bootstopusesha256") == 1;
327 else if (IS_ENABLED(CONFIG_CRYPT_PW))
328 return false;
329 else
330 return true;
331}
332
Stefan Roese0ed2e462015-05-18 14:08:24 +0200333/***************************************************************************
334 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
335 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
336 */
Simon Glass6d99d132019-07-20 20:51:19 -0600337static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200338{
339 int abort;
340 uint64_t etime = endtick(bootdelay);
341
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200342 if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
343 flush_stdin();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200344# ifdef CONFIG_AUTOBOOT_PROMPT
345 /*
346 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
347 * To print the bootdelay value upon bootup.
348 */
349 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
350# endif
351
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200352 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200353 if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200354 abort = passwd_abort_crypt(etime);
355 else
356 abort = passwd_abort_sha256(etime);
357 } else {
Simon Glass6a2e09f2019-07-20 20:51:16 -0600358 abort = passwd_abort_key(etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200359 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600360 if (!abort)
361 debug_bootkeys("key timeout\n");
362
Simon Glass7b6a95a2014-04-10 20:01:28 -0600363 return abort;
364}
365
Simon Glass6d99d132019-07-20 20:51:19 -0600366static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600367{
368 int abort = 0;
369 unsigned long ts;
370
Masahiro Yamada9a333862016-06-27 16:23:04 +0900371 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600372
Simon Glass7b6a95a2014-04-10 20:01:28 -0600373 /*
374 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600375 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900376 if (tstc()) { /* we got a key press */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200377 getchar(); /* consume input */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900378 puts("\b\b\b 0");
379 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600380 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600381
382 while ((bootdelay > 0) && (!abort)) {
383 --bootdelay;
384 /* delay 1000 ms */
385 ts = get_timer(0);
386 do {
387 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600388 int key;
389
Simon Glass7b6a95a2014-04-10 20:01:28 -0600390 abort = 1; /* don't auto boot */
391 bootdelay = 0; /* no more delay */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200392 key = getchar();/* consume input */
Da Xue3769e552021-06-21 22:39:19 -0400393 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
Simon Glass9d983852019-07-20 20:51:23 -0600394 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600395 break;
396 }
397 udelay(10000);
398 } while (!abort && get_timer(ts) < 1000);
399
400 printf("\b\b\b%2d ", bootdelay);
401 }
402
403 putc('\n');
404
Simon Glass7b6a95a2014-04-10 20:01:28 -0600405 return abort;
406}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600407
408static int abortboot(int bootdelay)
409{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900410 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900411
Simon Glass6d99d132019-07-20 20:51:19 -0600412 if (bootdelay >= 0) {
Simon Glassd8c60172021-07-24 15:14:39 -0600413 if (autoboot_keyed())
Simon Glass6d99d132019-07-20 20:51:19 -0600414 abort = abortboot_key_sequence(bootdelay);
415 else
416 abort = abortboot_single_key(bootdelay);
417 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900418
Simon Glass3a315052019-07-20 20:51:18 -0600419 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900420 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900421
422 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600423}
424
Simon Glass7b6a95a2014-04-10 20:01:28 -0600425static void process_fdt_options(const void *blob)
426{
Simon Glassb97ace52019-07-20 20:51:27 -0600427#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600428 ulong addr;
429
430 /* Add an env variable to point to a kernel payload, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600431 addr = ofnode_conf_read_int("kernel-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600432 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600433 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600434
435 /* Add an env variable to point to a root disk, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600436 addr = ofnode_conf_read_int("rootdisk-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600437 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600438 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glassb97ace52019-07-20 20:51:27 -0600439#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600440}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600441
Simon Glass5b47e302014-04-10 20:01:35 -0600442const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600443{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600444 char *s;
445 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600446
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200447 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600448
Simon Glass64b723f2017-08-03 12:22:12 -0600449 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600450 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
451
Simon Glassb8ffa142022-03-11 16:22:38 -0700452 /*
453 * Does it really make sense that the devicetree overrides the user
454 * setting? It is possibly helpful for security since the device tree
455 * may be signed whereas the environment is often loaded from storage.
456 */
Simon Glassb97ace52019-07-20 20:51:27 -0600457 if (IS_ENABLED(CONFIG_OF_CONTROL))
Simon Glass0034d962021-08-07 07:24:01 -0600458 bootdelay = ofnode_conf_read_int("bootdelay", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600459
460 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
461
Simon Glassb97ace52019-07-20 20:51:27 -0600462 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
463 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600464 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600465
466#ifdef CONFIG_POST
467 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600468 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600469 } else
470#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200471 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600472 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200473 else
Simon Glass64b723f2017-08-03 12:22:12 -0600474 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600475
Simon Glassb97ace52019-07-20 20:51:27 -0600476 if (IS_ENABLED(CONFIG_OF_CONTROL))
477 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600478 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600479
Simon Glass5b47e302014-04-10 20:01:35 -0600480 return s;
481}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600482
Simon Glass5b47e302014-04-10 20:01:35 -0600483void autoboot_command(const char *s)
484{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600485 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
486
Heiko Schocher12803652020-10-07 08:06:54 +0200487 if (s && (stored_bootdelay == -2 ||
488 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600489 bool lock;
490 int prev;
491
Simon Glassd8c60172021-07-24 15:14:39 -0600492 lock = autoboot_keyed() &&
Simon Glass313fcbd2019-07-20 20:51:28 -0600493 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
494 if (lock)
495 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600496
497 run_command_list(s, -1, 0);
498
Simon Glass313fcbd2019-07-20 20:51:28 -0600499 if (lock)
500 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600501 }
502
Da Xue3769e552021-06-21 22:39:19 -0400503 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
Simon Glass9d983852019-07-20 20:51:23 -0600504 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600505 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600506 if (s)
507 run_command_list(s, -1, 0);
508 }
Simon Glassd8c60172021-07-24 15:14:39 -0600509}