Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Jeroen Hofstee | f682d8c | 2014-07-13 22:57:58 +0200 | [diff] [blame] | 8 | #include <autoboot.h> |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 9 | #include <bootretry.h> |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 10 | #include <cli.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 12 | #include <console.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 14 | #include <fdtdec.h> |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 15 | #include <hash.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 18 | #include <memalign.h> |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 19 | #include <menu.h> |
| 20 | #include <post.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 21 | #include <time.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 24 | #include <u-boot/sha256.h> |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 25 | #include <bootcount.h> |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 26 | #include <crypt.h> |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 27 | #include <dm/ofnode.h> |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 31 | #define DELAY_STOP_STR_MAX_LENGTH 64 |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 32 | |
| 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 Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 39 | /* Stored value of bootdelay, used by autoboot_command() */ |
| 40 | static int stored_bootdelay; |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 41 | static int menukey; |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 42 | |
Tom Rini | 0c23c5b | 2023-01-10 11:19:34 -0500 | [diff] [blame] | 43 | #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 Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 47 | #endif |
Tom Rini | 0c23c5b | 2023-01-10 11:19:34 -0500 | [diff] [blame] | 48 | #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 Glass | 760d3fb | 2019-07-20 20:51:15 -0600 | [diff] [blame] | 52 | #endif |
| 53 | |
Da Xue | 3769e55 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 54 | #ifdef CONFIG_AUTOBOOT_USE_MENUKEY |
Da Xue | 4387f2e | 2021-07-02 12:32:43 -0400 | [diff] [blame] | 55 | #define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 56 | #else |
| 57 | #define AUTOBOOT_MENUKEY 0 |
| 58 | #endif |
| 59 | |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 60 | /** |
| 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 Jaeckel | 792a13f | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 71 | * 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 Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 75 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 76 | * Return: 0 if autoboot should continue, 1 if it should stop |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 77 | */ |
| 78 | static int passwd_abort_crypt(uint64_t etime) |
| 79 | { |
| 80 | const char *crypt_env_str = env_get("bootstopkeycrypt"); |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 81 | char presskey[DELAY_STOP_STR_MAX_LENGTH]; |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 82 | u_int presskey_len = 0; |
| 83 | int abort = 0; |
Steffen Jaeckel | 792a13f | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 84 | int never_timeout = 0; |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 85 | int err; |
| 86 | |
| 87 | if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str) |
Tom Rini | 0c23c5b | 2023-01-10 11:19:34 -0500 | [diff] [blame] | 88 | crypt_env_str = AUTOBOOT_STOP_STR_CRYPT; |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 89 | |
| 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 Jaeckel | 792a13f | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 104 | if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) && |
| 105 | !presskey_len) { |
| 106 | never_timeout = 1; |
| 107 | continue; |
| 108 | } |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 109 | 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 Villemoes | db794c2 | 2022-09-27 11:54:02 +0200 | [diff] [blame] | 122 | udelay(10000); |
Steffen Jaeckel | 792a13f | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 123 | } while (never_timeout || get_ticks() <= etime); |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 124 | |
| 125 | return abort; |
| 126 | } |
| 127 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 128 | /* |
| 129 | * Use a "constant-length" time compare function for this |
| 130 | * hash compare: |
| 131 | * |
| 132 | * https://crackstation.net/hashing-security.htm |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 133 | */ |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 134 | static int slow_equals(u8 *a, u8 *b, int len) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 135 | { |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 136 | 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 Glass | a8cab88 | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 145 | /** |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 151 | * Return: 0 if autoboot should continue, 1 if it should stop |
Simon Glass | a8cab88 | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 152 | */ |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 153 | static int passwd_abort_sha256(uint64_t etime) |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 154 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 155 | const char *sha_env_str = env_get("bootstopkeysha256"); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 156 | u8 sha_env[SHA256_SUM_LEN]; |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 157 | u8 *sha; |
| 158 | char *presskey; |
Joel Peshkin | 8b7bf53 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 159 | char *c; |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 160 | const char *algo_name = "sha256"; |
| 161 | u_int presskey_len = 0; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 162 | int abort = 0; |
Martin Etnestad | 055afba | 2018-01-12 09:04:38 +0100 | [diff] [blame] | 163 | int size = sizeof(sha); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 164 | int ret; |
| 165 | |
| 166 | if (sha_env_str == NULL) |
Tom Rini | 0c23c5b | 2023-01-10 11:19:34 -0500 | [diff] [blame] | 167 | sha_env_str = AUTOBOOT_STOP_STR_SHA256; |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 168 | |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 169 | presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH); |
Maks Mishin | c5bd289 | 2024-03-01 01:32:11 +0300 | [diff] [blame] | 170 | if (!presskey) |
| 171 | return -ENOMEM; |
| 172 | |
Joel Peshkin | 8b7bf53 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 173 | c = strstr(sha_env_str, ":"); |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 174 | if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) { |
Joel Peshkin | 8b7bf53 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 175 | /* preload presskey with salt */ |
| 176 | memcpy(presskey, sha_env_str, c - sha_env_str); |
| 177 | presskey_len = c - sha_env_str; |
| 178 | sha_env_str = c + 1; |
| 179 | } |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 180 | /* |
| 181 | * Generate the binary value from the environment hash value |
| 182 | * so that we can compare this value with the computed hash |
| 183 | * from the user input |
| 184 | */ |
| 185 | ret = hash_parse_string(algo_name, sha_env_str, sha_env); |
| 186 | if (ret) { |
| 187 | printf("Hash %s not supported!\n", algo_name); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 191 | sha = malloc_cache_aligned(SHA256_SUM_LEN); |
| 192 | size = SHA256_SUM_LEN; |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 193 | /* |
| 194 | * We don't know how long the stop-string is, so we need to |
| 195 | * generate the sha256 hash upon each input character and |
| 196 | * compare the value with the one saved in the environment |
| 197 | */ |
| 198 | do { |
| 199 | if (tstc()) { |
| 200 | /* Check for input string overflow */ |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 201 | if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) { |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 202 | free(presskey); |
| 203 | free(sha); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 204 | return 0; |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 205 | } |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 206 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 207 | presskey[presskey_len++] = getchar(); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 208 | |
| 209 | /* Calculate sha256 upon each new char */ |
| 210 | hash_block(algo_name, (const void *)presskey, |
| 211 | presskey_len, sha, &size); |
| 212 | |
| 213 | /* And check if sha matches saved value in env */ |
| 214 | if (slow_equals(sha, sha_env, SHA256_SUM_LEN)) |
| 215 | abort = 1; |
| 216 | } |
Rasmus Villemoes | db794c2 | 2022-09-27 11:54:02 +0200 | [diff] [blame] | 217 | udelay(10000); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 218 | } while (!abort && get_ticks() <= etime); |
| 219 | |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 220 | free(presskey); |
| 221 | free(sha); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 222 | return abort; |
| 223 | } |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 224 | |
Simon Glass | a8cab88 | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 225 | /** |
| 226 | * passwd_abort_key() - check for a key sequence to aborted booting |
| 227 | * |
| 228 | * This checks for the user entering a string within a given time. |
| 229 | * |
| 230 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 231 | * Return: 0 if autoboot should continue, 1 if it should stop |
Simon Glass | a8cab88 | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 232 | */ |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 233 | static int passwd_abort_key(uint64_t etime) |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 234 | { |
| 235 | int abort = 0; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 236 | struct { |
| 237 | char *str; |
| 238 | u_int len; |
| 239 | int retry; |
| 240 | } |
| 241 | delaykey[] = { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 242 | { .str = env_get("bootdelaykey"), .retry = 1 }, |
| 243 | { .str = env_get("bootstopkey"), .retry = 0 }, |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 244 | }; |
| 245 | |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 246 | char presskey[DELAY_STOP_STR_MAX_LENGTH]; |
Yuezhang.Mo@sony.com | d8eeb0f | 2021-01-15 03:11:49 +0000 | [diff] [blame] | 247 | int presskey_len = 0; |
| 248 | int presskey_max = 0; |
| 249 | int i; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 250 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 251 | # ifdef CONFIG_AUTOBOOT_DELAY_STR |
| 252 | if (delaykey[0].str == NULL) |
| 253 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; |
| 254 | # endif |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 255 | # ifdef CONFIG_AUTOBOOT_STOP_STR |
Stefan Roese | 0a48c7a | 2015-05-18 14:08:22 +0200 | [diff] [blame] | 256 | if (delaykey[1].str == NULL) |
| 257 | delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 258 | # endif |
| 259 | |
| 260 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) { |
| 261 | delaykey[i].len = delaykey[i].str == NULL ? |
| 262 | 0 : strlen(delaykey[i].str); |
Steffen Jaeckel | 092c8c6 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 263 | delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ? |
| 264 | DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 265 | |
| 266 | presskey_max = presskey_max > delaykey[i].len ? |
| 267 | presskey_max : delaykey[i].len; |
| 268 | |
| 269 | debug_bootkeys("%s key:<%s>\n", |
| 270 | delaykey[i].retry ? "delay" : "stop", |
| 271 | delaykey[i].str ? delaykey[i].str : "NULL"); |
| 272 | } |
| 273 | |
| 274 | /* In order to keep up with incoming data, check timeout only |
| 275 | * when catch up. |
| 276 | */ |
| 277 | do { |
| 278 | if (tstc()) { |
| 279 | if (presskey_len < presskey_max) { |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 280 | presskey[presskey_len++] = getchar(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 281 | } else { |
| 282 | for (i = 0; i < presskey_max - 1; i++) |
| 283 | presskey[i] = presskey[i + 1]; |
| 284 | |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 285 | presskey[i] = getchar(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) { |
| 290 | if (delaykey[i].len > 0 && |
| 291 | presskey_len >= delaykey[i].len && |
| 292 | memcmp(presskey + presskey_len - |
| 293 | delaykey[i].len, delaykey[i].str, |
| 294 | delaykey[i].len) == 0) { |
| 295 | debug_bootkeys("got %skey\n", |
| 296 | delaykey[i].retry ? "delay" : |
| 297 | "stop"); |
| 298 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 299 | /* don't retry auto boot */ |
| 300 | if (!delaykey[i].retry) |
| 301 | bootretry_dont_retry(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 302 | abort = 1; |
| 303 | } |
| 304 | } |
Rasmus Villemoes | db794c2 | 2022-09-27 11:54:02 +0200 | [diff] [blame] | 305 | udelay(10000); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 306 | } while (!abort && get_ticks() <= etime); |
| 307 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 308 | return abort; |
| 309 | } |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 310 | |
Steffen Jaeckel | dfc9732 | 2021-07-08 15:57:38 +0200 | [diff] [blame] | 311 | /** |
| 312 | * flush_stdin() - drops all pending characters from stdin |
| 313 | */ |
| 314 | static void flush_stdin(void) |
| 315 | { |
| 316 | while (tstc()) |
| 317 | (void)getchar(); |
| 318 | } |
| 319 | |
Steffen Jaeckel | 28be70d | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 320 | /** |
| 321 | * fallback_to_sha256() - check whether we should fall back to sha256 |
| 322 | * password checking |
| 323 | * |
| 324 | * This checks for the environment variable `bootstopusesha256` in case |
| 325 | * sha256-fallback has been enabled via the config setting |
| 326 | * `AUTOBOOT_SHA256_FALLBACK`. |
| 327 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 328 | * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried |
Steffen Jaeckel | 28be70d | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 329 | */ |
| 330 | static bool fallback_to_sha256(void) |
| 331 | { |
| 332 | if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK)) |
| 333 | return env_get_yesno("bootstopusesha256") == 1; |
| 334 | else if (IS_ENABLED(CONFIG_CRYPT_PW)) |
| 335 | return false; |
| 336 | else |
| 337 | return true; |
| 338 | } |
| 339 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 340 | /*************************************************************************** |
| 341 | * Watch for 'delay' seconds for autoboot stop or autoboot delay string. |
| 342 | * returns: 0 - no key string, allow autoboot 1 - got key string, abort |
| 343 | */ |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 344 | static int abortboot_key_sequence(int bootdelay) |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 345 | { |
| 346 | int abort; |
| 347 | uint64_t etime = endtick(bootdelay); |
| 348 | |
Steffen Jaeckel | dfc9732 | 2021-07-08 15:57:38 +0200 | [diff] [blame] | 349 | if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN)) |
| 350 | flush_stdin(); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 351 | # ifdef CONFIG_AUTOBOOT_PROMPT |
| 352 | /* |
| 353 | * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards. |
| 354 | * To print the bootdelay value upon bootup. |
| 355 | */ |
| 356 | printf(CONFIG_AUTOBOOT_PROMPT, bootdelay); |
| 357 | # endif |
| 358 | |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 359 | if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) { |
Steffen Jaeckel | 28be70d | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 360 | if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256()) |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 361 | abort = passwd_abort_crypt(etime); |
| 362 | else |
| 363 | abort = passwd_abort_sha256(etime); |
| 364 | } else { |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 365 | abort = passwd_abort_key(etime); |
Steffen Jaeckel | 6aa6bfb | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 366 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 367 | if (!abort) |
| 368 | debug_bootkeys("key timeout\n"); |
| 369 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 370 | return abort; |
| 371 | } |
| 372 | |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 373 | static int abortboot_single_key(int bootdelay) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 374 | { |
| 375 | int abort = 0; |
| 376 | unsigned long ts; |
| 377 | |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 378 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 379 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 380 | /* |
| 381 | * Check if key already pressed |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 382 | */ |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 383 | if (tstc()) { /* we got a key press */ |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 384 | getchar(); /* consume input */ |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 385 | puts("\b\b\b 0"); |
| 386 | abort = 1; /* don't auto boot */ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 387 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 388 | |
| 389 | while ((bootdelay > 0) && (!abort)) { |
| 390 | --bootdelay; |
| 391 | /* delay 1000 ms */ |
| 392 | ts = get_timer(0); |
| 393 | do { |
| 394 | if (tstc()) { /* we got a key press */ |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 395 | int key; |
| 396 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 397 | abort = 1; /* don't auto boot */ |
| 398 | bootdelay = 0; /* no more delay */ |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 399 | key = getchar();/* consume input */ |
Da Xue | 3769e55 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 400 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY)) |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 401 | menukey = key; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | udelay(10000); |
| 405 | } while (!abort && get_timer(ts) < 1000); |
| 406 | |
| 407 | printf("\b\b\b%2d ", bootdelay); |
| 408 | } |
| 409 | |
| 410 | putc('\n'); |
| 411 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 412 | return abort; |
| 413 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 414 | |
| 415 | static int abortboot(int bootdelay) |
| 416 | { |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 417 | int abort = 0; |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 418 | |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 419 | if (bootdelay >= 0) { |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 420 | if (autoboot_keyed()) |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 421 | abort = abortboot_key_sequence(bootdelay); |
| 422 | else |
| 423 | abort = abortboot_single_key(bootdelay); |
| 424 | } |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 425 | |
Simon Glass | 3a31505 | 2019-07-20 20:51:18 -0600 | [diff] [blame] | 426 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort) |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 427 | gd->flags &= ~GD_FLG_SILENT; |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 428 | |
| 429 | return abort; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 430 | } |
| 431 | |
Shenlin Liang | bb0f62a | 2022-12-02 12:53:48 +0800 | [diff] [blame] | 432 | static void process_fdt_options(void) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 433 | { |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 434 | #ifdef CONFIG_TEXT_BASE |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 435 | ulong addr; |
| 436 | |
| 437 | /* Add an env variable to point to a kernel payload, if available */ |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 438 | addr = ofnode_conf_read_int("kernel-offset", 0); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 439 | if (addr) |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 440 | env_set_addr("kernaddr", (void *)(CONFIG_TEXT_BASE + addr)); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 441 | |
| 442 | /* Add an env variable to point to a root disk, if available */ |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 443 | addr = ofnode_conf_read_int("rootdisk-offset", 0); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 444 | if (addr) |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 445 | env_set_addr("rootaddr", (void *)(CONFIG_TEXT_BASE + addr)); |
| 446 | #endif /* CONFIG_TEXT_BASE */ |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 447 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 448 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 449 | const char *bootdelay_process(void) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 450 | { |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 451 | char *s; |
| 452 | int bootdelay; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 453 | |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 454 | bootcount_inc(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 455 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 456 | s = env_get("bootdelay"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 457 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
| 458 | |
Simon Glass | b8ffa14 | 2022-03-11 16:22:38 -0700 | [diff] [blame] | 459 | /* |
| 460 | * Does it really make sense that the devicetree overrides the user |
| 461 | * setting? It is possibly helpful for security since the device tree |
| 462 | * may be signed whereas the environment is often loaded from storage. |
| 463 | */ |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 464 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 465 | bootdelay = ofnode_conf_read_int("bootdelay", bootdelay); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 466 | |
| 467 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); |
| 468 | |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 469 | if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW)) |
| 470 | bootdelay = menu_show(bootdelay); |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 471 | bootretry_init_cmd_timeout(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 472 | |
| 473 | #ifdef CONFIG_POST |
| 474 | if (gd->flags & GD_FLG_POSTFAIL) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 475 | s = env_get("failbootcmd"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 476 | } else |
| 477 | #endif /* CONFIG_POST */ |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 478 | if (bootcount_error()) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 479 | s = env_get("altbootcmd"); |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 480 | else |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 481 | s = env_get("bootcmd"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 482 | |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 483 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
Shenlin Liang | bb0f62a | 2022-12-02 12:53:48 +0800 | [diff] [blame] | 484 | process_fdt_options(); |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 485 | stored_bootdelay = bootdelay; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 486 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 487 | return s; |
| 488 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 489 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 490 | void autoboot_command(const char *s) |
| 491 | { |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 492 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); |
| 493 | |
Heiko Schocher | 1280365 | 2020-10-07 08:06:54 +0200 | [diff] [blame] | 494 | if (s && (stored_bootdelay == -2 || |
| 495 | (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) { |
Simon Glass | 313fcbd | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 496 | bool lock; |
| 497 | int prev; |
| 498 | |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 499 | lock = autoboot_keyed() && |
Simon Glass | 313fcbd | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 500 | !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC); |
| 501 | if (lock) |
| 502 | prev = disable_ctrlc(1); /* disable Ctrl-C checking */ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 503 | |
| 504 | run_command_list(s, -1, 0); |
| 505 | |
Simon Glass | 313fcbd | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 506 | if (lock) |
| 507 | disable_ctrlc(prev); /* restore Ctrl-C checking */ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 508 | } |
| 509 | |
Da Xue | 3769e55 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 510 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) && |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 511 | menukey == AUTOBOOT_MENUKEY) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 512 | s = env_get("menucmd"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 513 | if (s) |
| 514 | run_command_list(s, -1, 0); |
| 515 | } |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 516 | } |