blob: 63f2587941dbb9d858e5764b2be93ae5f665df9a [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 }
Steffen Jaeckel792a13f2021-07-08 15:57:37 +0200118 } while (never_timeout || get_ticks() <= etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200119
120 return abort;
121}
122
Stefan Roese0ed2e462015-05-18 14:08:24 +0200123/*
124 * Use a "constant-length" time compare function for this
125 * hash compare:
126 *
127 * https://crackstation.net/hashing-security.htm
Simon Glass7b6a95a2014-04-10 20:01:28 -0600128 */
Stefan Roese0ed2e462015-05-18 14:08:24 +0200129static int slow_equals(u8 *a, u8 *b, int len)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600130{
Stefan Roese0ed2e462015-05-18 14:08:24 +0200131 int diff = 0;
132 int i;
133
134 for (i = 0; i < len; i++)
135 diff |= a[i] ^ b[i];
136
137 return diff == 0;
138}
139
Simon Glassa8cab882019-07-20 20:51:17 -0600140/**
141 * passwd_abort_sha256() - check for a hashed key sequence to abort booting
142 *
143 * This checks for the user entering a SHA256 hash within a given time.
144 *
145 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100146 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600147 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600148static int passwd_abort_sha256(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200149{
Simon Glass64b723f2017-08-03 12:22:12 -0600150 const char *sha_env_str = env_get("bootstopkeysha256");
Stefan Roese0ed2e462015-05-18 14:08:24 +0200151 u8 sha_env[SHA256_SUM_LEN];
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200152 u8 *sha;
153 char *presskey;
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800154 char *c;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200155 const char *algo_name = "sha256";
156 u_int presskey_len = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600157 int abort = 0;
Martin Etnestad055afba2018-01-12 09:04:38 +0100158 int size = sizeof(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200159 int ret;
160
161 if (sha_env_str == NULL)
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200162 sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200163
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200164 presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH);
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800165 c = strstr(sha_env_str, ":");
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200166 if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
Joel Peshkin8b7bf532020-11-21 17:18:59 -0800167 /* preload presskey with salt */
168 memcpy(presskey, sha_env_str, c - sha_env_str);
169 presskey_len = c - sha_env_str;
170 sha_env_str = c + 1;
171 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200172 /*
173 * Generate the binary value from the environment hash value
174 * so that we can compare this value with the computed hash
175 * from the user input
176 */
177 ret = hash_parse_string(algo_name, sha_env_str, sha_env);
178 if (ret) {
179 printf("Hash %s not supported!\n", algo_name);
180 return 0;
181 }
182
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200183 sha = malloc_cache_aligned(SHA256_SUM_LEN);
184 size = SHA256_SUM_LEN;
Stefan Roese0ed2e462015-05-18 14:08:24 +0200185 /*
186 * We don't know how long the stop-string is, so we need to
187 * generate the sha256 hash upon each input character and
188 * compare the value with the one saved in the environment
189 */
190 do {
191 if (tstc()) {
192 /* Check for input string overflow */
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200193 if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200194 free(presskey);
195 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200196 return 0;
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200197 }
Stefan Roese0ed2e462015-05-18 14:08:24 +0200198
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200199 presskey[presskey_len++] = getchar();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200200
201 /* Calculate sha256 upon each new char */
202 hash_block(algo_name, (const void *)presskey,
203 presskey_len, sha, &size);
204
205 /* And check if sha matches saved value in env */
206 if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
207 abort = 1;
208 }
209 } while (!abort && get_ticks() <= etime);
210
Heiko Schocher2ef3a952019-07-29 07:23:19 +0200211 free(presskey);
212 free(sha);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200213 return abort;
214}
Simon Glass6a2e09f2019-07-20 20:51:16 -0600215
Simon Glassa8cab882019-07-20 20:51:17 -0600216/**
217 * passwd_abort_key() - check for a key sequence to aborted booting
218 *
219 * This checks for the user entering a string within a given time.
220 *
221 * @etime: Timeout value ticks (stop when get_ticks() reachs this)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100222 * Return: 0 if autoboot should continue, 1 if it should stop
Simon Glassa8cab882019-07-20 20:51:17 -0600223 */
Simon Glass6a2e09f2019-07-20 20:51:16 -0600224static int passwd_abort_key(uint64_t etime)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200225{
226 int abort = 0;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600227 struct {
228 char *str;
229 u_int len;
230 int retry;
231 }
232 delaykey[] = {
Simon Glass64b723f2017-08-03 12:22:12 -0600233 { .str = env_get("bootdelaykey"), .retry = 1 },
234 { .str = env_get("bootstopkey"), .retry = 0 },
Simon Glass7b6a95a2014-04-10 20:01:28 -0600235 };
236
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200237 char presskey[DELAY_STOP_STR_MAX_LENGTH];
Yuezhang.Mo@sony.comd8eeb0f2021-01-15 03:11:49 +0000238 int presskey_len = 0;
239 int presskey_max = 0;
240 int i;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600241
Simon Glass7b6a95a2014-04-10 20:01:28 -0600242# ifdef CONFIG_AUTOBOOT_DELAY_STR
243 if (delaykey[0].str == NULL)
244 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
245# endif
Simon Glass7b6a95a2014-04-10 20:01:28 -0600246# ifdef CONFIG_AUTOBOOT_STOP_STR
Stefan Roese0a48c7a2015-05-18 14:08:22 +0200247 if (delaykey[1].str == NULL)
248 delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600249# endif
250
251 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
252 delaykey[i].len = delaykey[i].str == NULL ?
253 0 : strlen(delaykey[i].str);
Steffen Jaeckel092c8c62021-07-08 15:57:36 +0200254 delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
255 DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600256
257 presskey_max = presskey_max > delaykey[i].len ?
258 presskey_max : delaykey[i].len;
259
260 debug_bootkeys("%s key:<%s>\n",
261 delaykey[i].retry ? "delay" : "stop",
262 delaykey[i].str ? delaykey[i].str : "NULL");
263 }
264
265 /* In order to keep up with incoming data, check timeout only
266 * when catch up.
267 */
268 do {
269 if (tstc()) {
270 if (presskey_len < presskey_max) {
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200271 presskey[presskey_len++] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600272 } else {
273 for (i = 0; i < presskey_max - 1; i++)
274 presskey[i] = presskey[i + 1];
275
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200276 presskey[i] = getchar();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600277 }
278 }
279
280 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
281 if (delaykey[i].len > 0 &&
282 presskey_len >= delaykey[i].len &&
283 memcmp(presskey + presskey_len -
284 delaykey[i].len, delaykey[i].str,
285 delaykey[i].len) == 0) {
286 debug_bootkeys("got %skey\n",
287 delaykey[i].retry ? "delay" :
288 "stop");
289
Simon Glass7b6a95a2014-04-10 20:01:28 -0600290 /* don't retry auto boot */
291 if (!delaykey[i].retry)
292 bootretry_dont_retry();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600293 abort = 1;
294 }
295 }
296 } while (!abort && get_ticks() <= etime);
297
Stefan Roese0ed2e462015-05-18 14:08:24 +0200298 return abort;
299}
Stefan Roese0ed2e462015-05-18 14:08:24 +0200300
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200301/**
302 * flush_stdin() - drops all pending characters from stdin
303 */
304static void flush_stdin(void)
305{
306 while (tstc())
307 (void)getchar();
308}
309
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200310/**
311 * fallback_to_sha256() - check whether we should fall back to sha256
312 * password checking
313 *
314 * This checks for the environment variable `bootstopusesha256` in case
315 * sha256-fallback has been enabled via the config setting
316 * `AUTOBOOT_SHA256_FALLBACK`.
317 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100318 * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200319 */
320static bool fallback_to_sha256(void)
321{
322 if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
323 return env_get_yesno("bootstopusesha256") == 1;
324 else if (IS_ENABLED(CONFIG_CRYPT_PW))
325 return false;
326 else
327 return true;
328}
329
Stefan Roese0ed2e462015-05-18 14:08:24 +0200330/***************************************************************************
331 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
332 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
333 */
Simon Glass6d99d132019-07-20 20:51:19 -0600334static int abortboot_key_sequence(int bootdelay)
Stefan Roese0ed2e462015-05-18 14:08:24 +0200335{
336 int abort;
337 uint64_t etime = endtick(bootdelay);
338
Steffen Jaeckeldfc97322021-07-08 15:57:38 +0200339 if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
340 flush_stdin();
Stefan Roese0ed2e462015-05-18 14:08:24 +0200341# ifdef CONFIG_AUTOBOOT_PROMPT
342 /*
343 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
344 * To print the bootdelay value upon bootup.
345 */
346 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
347# endif
348
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200349 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
Steffen Jaeckel28be70d2021-07-08 15:57:39 +0200350 if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200351 abort = passwd_abort_crypt(etime);
352 else
353 abort = passwd_abort_sha256(etime);
354 } else {
Simon Glass6a2e09f2019-07-20 20:51:16 -0600355 abort = passwd_abort_key(etime);
Steffen Jaeckel6aa6bfb2021-07-08 15:57:35 +0200356 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600357 if (!abort)
358 debug_bootkeys("key timeout\n");
359
Simon Glass7b6a95a2014-04-10 20:01:28 -0600360 return abort;
361}
362
Simon Glass6d99d132019-07-20 20:51:19 -0600363static int abortboot_single_key(int bootdelay)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600364{
365 int abort = 0;
366 unsigned long ts;
367
Masahiro Yamada9a333862016-06-27 16:23:04 +0900368 printf("Hit any key to stop autoboot: %2d ", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600369
Simon Glass7b6a95a2014-04-10 20:01:28 -0600370 /*
371 * Check if key already pressed
Simon Glass7b6a95a2014-04-10 20:01:28 -0600372 */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900373 if (tstc()) { /* we got a key press */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200374 getchar(); /* consume input */
Masahiro Yamada9a333862016-06-27 16:23:04 +0900375 puts("\b\b\b 0");
376 abort = 1; /* don't auto boot */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600377 }
Simon Glass7b6a95a2014-04-10 20:01:28 -0600378
379 while ((bootdelay > 0) && (!abort)) {
380 --bootdelay;
381 /* delay 1000 ms */
382 ts = get_timer(0);
383 do {
384 if (tstc()) { /* we got a key press */
Simon Glass9d983852019-07-20 20:51:23 -0600385 int key;
386
Simon Glass7b6a95a2014-04-10 20:01:28 -0600387 abort = 1; /* don't auto boot */
388 bootdelay = 0; /* no more delay */
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200389 key = getchar();/* consume input */
Da Xue3769e552021-06-21 22:39:19 -0400390 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
Simon Glass9d983852019-07-20 20:51:23 -0600391 menukey = key;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600392 break;
393 }
394 udelay(10000);
395 } while (!abort && get_timer(ts) < 1000);
396
397 printf("\b\b\b%2d ", bootdelay);
398 }
399
400 putc('\n');
401
Simon Glass7b6a95a2014-04-10 20:01:28 -0600402 return abort;
403}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600404
405static int abortboot(int bootdelay)
406{
Masahiro Yamada9a333862016-06-27 16:23:04 +0900407 int abort = 0;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900408
Simon Glass6d99d132019-07-20 20:51:19 -0600409 if (bootdelay >= 0) {
Simon Glassd8c60172021-07-24 15:14:39 -0600410 if (autoboot_keyed())
Simon Glass6d99d132019-07-20 20:51:19 -0600411 abort = abortboot_key_sequence(bootdelay);
412 else
413 abort = abortboot_single_key(bootdelay);
414 }
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900415
Simon Glass3a315052019-07-20 20:51:18 -0600416 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900417 gd->flags &= ~GD_FLG_SILENT;
Masahiro Yamadad88f8722016-06-27 16:23:03 +0900418
419 return abort;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600420}
421
Simon Glass7b6a95a2014-04-10 20:01:28 -0600422static void process_fdt_options(const void *blob)
423{
Simon Glassb97ace52019-07-20 20:51:27 -0600424#ifdef CONFIG_SYS_TEXT_BASE
Simon Glass7b6a95a2014-04-10 20:01:28 -0600425 ulong addr;
426
427 /* Add an env variable to point to a kernel payload, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600428 addr = ofnode_conf_read_int("kernel-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600429 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600430 env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glass7b6a95a2014-04-10 20:01:28 -0600431
432 /* Add an env variable to point to a root disk, if available */
Simon Glass0034d962021-08-07 07:24:01 -0600433 addr = ofnode_conf_read_int("rootdisk-offset", 0);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600434 if (addr)
Simon Glass4d949a22017-08-03 12:22:10 -0600435 env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
Simon Glassb97ace52019-07-20 20:51:27 -0600436#endif /* CONFIG_SYS_TEXT_BASE */
Simon Glass5b47e302014-04-10 20:01:35 -0600437}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600438
Simon Glass5b47e302014-04-10 20:01:35 -0600439const char *bootdelay_process(void)
Simon Glass7b6a95a2014-04-10 20:01:28 -0600440{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600441 char *s;
442 int bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600443
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200444 bootcount_inc();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600445
Simon Glass64b723f2017-08-03 12:22:12 -0600446 s = env_get("bootdelay");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600447 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
448
Simon Glassb8ffa142022-03-11 16:22:38 -0700449 /*
450 * Does it really make sense that the devicetree overrides the user
451 * setting? It is possibly helpful for security since the device tree
452 * may be signed whereas the environment is often loaded from storage.
453 */
Simon Glassb97ace52019-07-20 20:51:27 -0600454 if (IS_ENABLED(CONFIG_OF_CONTROL))
Simon Glass0034d962021-08-07 07:24:01 -0600455 bootdelay = ofnode_conf_read_int("bootdelay", bootdelay);
Simon Glass7b6a95a2014-04-10 20:01:28 -0600456
457 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
458
Simon Glassb97ace52019-07-20 20:51:27 -0600459 if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
460 bootdelay = menu_show(bootdelay);
Simon Glass09007c42014-04-10 20:01:31 -0600461 bootretry_init_cmd_timeout();
Simon Glass7b6a95a2014-04-10 20:01:28 -0600462
463#ifdef CONFIG_POST
464 if (gd->flags & GD_FLG_POSTFAIL) {
Simon Glass64b723f2017-08-03 12:22:12 -0600465 s = env_get("failbootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600466 } else
467#endif /* CONFIG_POST */
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200468 if (bootcount_error())
Simon Glass64b723f2017-08-03 12:22:12 -0600469 s = env_get("altbootcmd");
Lukasz Majewski4fc18912018-05-02 16:10:53 +0200470 else
Simon Glass64b723f2017-08-03 12:22:12 -0600471 s = env_get("bootcmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600472
Simon Glassb97ace52019-07-20 20:51:27 -0600473 if (IS_ENABLED(CONFIG_OF_CONTROL))
474 process_fdt_options(gd->fdt_blob);
Simon Glass5b47e302014-04-10 20:01:35 -0600475 stored_bootdelay = bootdelay;
Simon Glass7b6a95a2014-04-10 20:01:28 -0600476
Simon Glass5b47e302014-04-10 20:01:35 -0600477 return s;
478}
Simon Glass7b6a95a2014-04-10 20:01:28 -0600479
Simon Glass5b47e302014-04-10 20:01:35 -0600480void autoboot_command(const char *s)
481{
Simon Glass7b6a95a2014-04-10 20:01:28 -0600482 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
483
Heiko Schocher12803652020-10-07 08:06:54 +0200484 if (s && (stored_bootdelay == -2 ||
485 (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
Simon Glass313fcbd2019-07-20 20:51:28 -0600486 bool lock;
487 int prev;
488
Simon Glassd8c60172021-07-24 15:14:39 -0600489 lock = autoboot_keyed() &&
Simon Glass313fcbd2019-07-20 20:51:28 -0600490 !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
491 if (lock)
492 prev = disable_ctrlc(1); /* disable Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600493
494 run_command_list(s, -1, 0);
495
Simon Glass313fcbd2019-07-20 20:51:28 -0600496 if (lock)
497 disable_ctrlc(prev); /* restore Ctrl-C checking */
Simon Glass7b6a95a2014-04-10 20:01:28 -0600498 }
499
Da Xue3769e552021-06-21 22:39:19 -0400500 if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
Simon Glass9d983852019-07-20 20:51:23 -0600501 menukey == AUTOBOOT_MENUKEY) {
Simon Glass64b723f2017-08-03 12:22:12 -0600502 s = env_get("menucmd");
Simon Glass7b6a95a2014-04-10 20:01:28 -0600503 if (s)
504 run_command_list(s, -1, 0);
505 }
Simon Glassd8c60172021-07-24 15:14:39 -0600506}