Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 66ded17 | 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 | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Jeroen Hofstee | 39e1230 | 2014-07-13 22:57:58 +0200 | [diff] [blame] | 8 | #include <autoboot.h> |
Simon Glass | 0098e17 | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 9 | #include <bootretry.h> |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 10 | #include <cli.h> |
Simon Glass | 288b29e | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 12 | #include <console.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 14 | #include <fdtdec.h> |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 15 | #include <hash.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 18 | #include <memalign.h> |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 19 | #include <menu.h> |
| 20 | #include <post.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 21 | #include <time.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 24 | #include <u-boot/sha256.h> |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 25 | #include <bootcount.h> |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 26 | #include <crypt.h> |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 30 | #define DELAY_STOP_STR_MAX_LENGTH 64 |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 31 | |
| 32 | #ifndef DEBUG_BOOTKEYS |
| 33 | #define DEBUG_BOOTKEYS 0 |
| 34 | #endif |
| 35 | #define debug_bootkeys(fmt, args...) \ |
| 36 | debug_cond(DEBUG_BOOTKEYS, fmt, ##args) |
| 37 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 38 | /* Stored value of bootdelay, used by autoboot_command() */ |
| 39 | static int stored_bootdelay; |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 40 | static int menukey; |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 41 | |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 42 | #if !defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT) |
| 43 | #define CONFIG_AUTOBOOT_STOP_STR_CRYPT "" |
| 44 | #endif |
| 45 | #if !defined(CONFIG_AUTOBOOT_STOP_STR_SHA256) |
| 46 | #define CONFIG_AUTOBOOT_STOP_STR_SHA256 "" |
Simon Glass | 0c4bd31 | 2019-07-20 20:51:15 -0600 | [diff] [blame] | 47 | #endif |
| 48 | |
Da Xue | 5a87df8 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 49 | #ifdef CONFIG_AUTOBOOT_USE_MENUKEY |
Da Xue | 760d2f9 | 2021-07-02 12:32:43 -0400 | [diff] [blame] | 50 | #define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 51 | #else |
| 52 | #define AUTOBOOT_MENUKEY 0 |
| 53 | #endif |
| 54 | |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 55 | /** |
| 56 | * passwd_abort_crypt() - check for a crypt-style hashed key sequence to abort booting |
| 57 | * |
| 58 | * This checks for the user entering a password within a given time. |
| 59 | * |
| 60 | * The entered password is hashed via one of the crypt-style hash methods |
| 61 | * and compared to the pre-defined value from either |
| 62 | * the environment variable "bootstopkeycrypt" |
| 63 | * or |
| 64 | * the config value CONFIG_AUTOBOOT_STOP_STR_CRYPT |
| 65 | * |
Steffen Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 66 | * In case the config value CONFIG_AUTOBOOT_NEVER_TIMEOUT has been enabled |
| 67 | * this function never times out if the user presses the <Enter> key |
| 68 | * before starting to enter the password. |
| 69 | * |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 70 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
| 71 | * @return 0 if autoboot should continue, 1 if it should stop |
| 72 | */ |
| 73 | static int passwd_abort_crypt(uint64_t etime) |
| 74 | { |
| 75 | const char *crypt_env_str = env_get("bootstopkeycrypt"); |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 76 | char presskey[DELAY_STOP_STR_MAX_LENGTH]; |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 77 | u_int presskey_len = 0; |
| 78 | int abort = 0; |
Steffen Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 79 | int never_timeout = 0; |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 80 | int err; |
| 81 | |
| 82 | if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str) |
| 83 | crypt_env_str = CONFIG_AUTOBOOT_STOP_STR_CRYPT; |
| 84 | |
| 85 | if (!crypt_env_str) |
| 86 | return 0; |
| 87 | |
| 88 | /* We expect the stop-string to be newline-terminated */ |
| 89 | do { |
| 90 | if (tstc()) { |
| 91 | /* Check for input string overflow */ |
| 92 | if (presskey_len >= sizeof(presskey)) |
| 93 | return 0; |
| 94 | |
| 95 | presskey[presskey_len] = getchar(); |
| 96 | |
| 97 | if ((presskey[presskey_len] == '\r') || |
| 98 | (presskey[presskey_len] == '\n')) { |
Steffen Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 99 | if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) && |
| 100 | !presskey_len) { |
| 101 | never_timeout = 1; |
| 102 | continue; |
| 103 | } |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 104 | presskey[presskey_len] = '\0'; |
| 105 | err = crypt_compare(crypt_env_str, presskey, |
| 106 | &abort); |
| 107 | if (err) |
| 108 | debug_bootkeys( |
| 109 | "crypt_compare() failed with: %s\n", |
| 110 | errno_str(err)); |
| 111 | /* you had one chance */ |
| 112 | break; |
| 113 | } else { |
| 114 | presskey_len++; |
| 115 | } |
| 116 | } |
Steffen Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 117 | } while (never_timeout || get_ticks() <= etime); |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 118 | |
| 119 | return abort; |
| 120 | } |
| 121 | |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Use a "constant-length" time compare function for this |
| 124 | * hash compare: |
| 125 | * |
| 126 | * https://crackstation.net/hashing-security.htm |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 127 | */ |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 128 | static int slow_equals(u8 *a, u8 *b, int len) |
| 129 | { |
| 130 | int diff = 0; |
| 131 | int i; |
| 132 | |
| 133 | for (i = 0; i < len; i++) |
| 134 | diff |= a[i] ^ b[i]; |
| 135 | |
| 136 | return diff == 0; |
| 137 | } |
| 138 | |
Simon Glass | 88fa4be | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 139 | /** |
| 140 | * passwd_abort_sha256() - check for a hashed key sequence to abort booting |
| 141 | * |
| 142 | * This checks for the user entering a SHA256 hash within a given time. |
| 143 | * |
| 144 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
| 145 | * @return 0 if autoboot should continue, 1 if it should stop |
| 146 | */ |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 147 | static int passwd_abort_sha256(uint64_t etime) |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 148 | { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 149 | const char *sha_env_str = env_get("bootstopkeysha256"); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 150 | u8 sha_env[SHA256_SUM_LEN]; |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 151 | u8 *sha; |
| 152 | char *presskey; |
Joel Peshkin | 652b504 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 153 | char *c; |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 154 | const char *algo_name = "sha256"; |
| 155 | u_int presskey_len = 0; |
| 156 | int abort = 0; |
Martin Etnestad | 2d06fd8 | 2018-01-12 09:04:38 +0100 | [diff] [blame] | 157 | int size = sizeof(sha); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 158 | int ret; |
| 159 | |
| 160 | if (sha_env_str == NULL) |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 161 | sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256; |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 162 | |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 163 | presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH); |
Joel Peshkin | 652b504 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 164 | c = strstr(sha_env_str, ":"); |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 165 | if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) { |
Joel Peshkin | 652b504 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 166 | /* preload presskey with salt */ |
| 167 | memcpy(presskey, sha_env_str, c - sha_env_str); |
| 168 | presskey_len = c - sha_env_str; |
| 169 | sha_env_str = c + 1; |
| 170 | } |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 171 | /* |
| 172 | * Generate the binary value from the environment hash value |
| 173 | * so that we can compare this value with the computed hash |
| 174 | * from the user input |
| 175 | */ |
| 176 | ret = hash_parse_string(algo_name, sha_env_str, sha_env); |
| 177 | if (ret) { |
| 178 | printf("Hash %s not supported!\n", algo_name); |
| 179 | return 0; |
| 180 | } |
| 181 | |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 182 | sha = malloc_cache_aligned(SHA256_SUM_LEN); |
| 183 | size = SHA256_SUM_LEN; |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 184 | /* |
| 185 | * We don't know how long the stop-string is, so we need to |
| 186 | * generate the sha256 hash upon each input character and |
| 187 | * compare the value with the one saved in the environment |
| 188 | */ |
| 189 | do { |
| 190 | if (tstc()) { |
| 191 | /* Check for input string overflow */ |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 192 | if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) { |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 193 | free(presskey); |
| 194 | free(sha); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 195 | return 0; |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 196 | } |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 197 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 198 | presskey[presskey_len++] = getchar(); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 199 | |
| 200 | /* Calculate sha256 upon each new char */ |
| 201 | hash_block(algo_name, (const void *)presskey, |
| 202 | presskey_len, sha, &size); |
| 203 | |
| 204 | /* And check if sha matches saved value in env */ |
| 205 | if (slow_equals(sha, sha_env, SHA256_SUM_LEN)) |
| 206 | abort = 1; |
| 207 | } |
| 208 | } while (!abort && get_ticks() <= etime); |
| 209 | |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 210 | free(presskey); |
| 211 | free(sha); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 212 | return abort; |
| 213 | } |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 214 | |
Simon Glass | 88fa4be | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 215 | /** |
| 216 | * passwd_abort_key() - check for a key sequence to aborted booting |
| 217 | * |
| 218 | * This checks for the user entering a string within a given time. |
| 219 | * |
| 220 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
| 221 | * @return 0 if autoboot should continue, 1 if it should stop |
| 222 | */ |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 223 | static int passwd_abort_key(uint64_t etime) |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 224 | { |
| 225 | int abort = 0; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 226 | struct { |
| 227 | char *str; |
| 228 | u_int len; |
| 229 | int retry; |
| 230 | } |
| 231 | delaykey[] = { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 232 | { .str = env_get("bootdelaykey"), .retry = 1 }, |
| 233 | { .str = env_get("bootstopkey"), .retry = 0 }, |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 234 | }; |
| 235 | |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 236 | char presskey[DELAY_STOP_STR_MAX_LENGTH]; |
Yuezhang.Mo@sony.com | e088f0c | 2021-01-15 03:11:49 +0000 | [diff] [blame] | 237 | int presskey_len = 0; |
| 238 | int presskey_max = 0; |
| 239 | int i; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 240 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 241 | # ifdef CONFIG_AUTOBOOT_DELAY_STR |
| 242 | if (delaykey[0].str == NULL) |
| 243 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; |
| 244 | # endif |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 245 | # ifdef CONFIG_AUTOBOOT_STOP_STR |
Stefan Roese | 2d908fa | 2015-05-18 14:08:22 +0200 | [diff] [blame] | 246 | if (delaykey[1].str == NULL) |
| 247 | delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 248 | # endif |
| 249 | |
| 250 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) { |
| 251 | delaykey[i].len = delaykey[i].str == NULL ? |
| 252 | 0 : strlen(delaykey[i].str); |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 253 | delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ? |
| 254 | DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 255 | |
| 256 | presskey_max = presskey_max > delaykey[i].len ? |
| 257 | presskey_max : delaykey[i].len; |
| 258 | |
| 259 | debug_bootkeys("%s key:<%s>\n", |
| 260 | delaykey[i].retry ? "delay" : "stop", |
| 261 | delaykey[i].str ? delaykey[i].str : "NULL"); |
| 262 | } |
| 263 | |
| 264 | /* In order to keep up with incoming data, check timeout only |
| 265 | * when catch up. |
| 266 | */ |
| 267 | do { |
| 268 | if (tstc()) { |
| 269 | if (presskey_len < presskey_max) { |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 270 | presskey[presskey_len++] = getchar(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 271 | } else { |
| 272 | for (i = 0; i < presskey_max - 1; i++) |
| 273 | presskey[i] = presskey[i + 1]; |
| 274 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 275 | presskey[i] = getchar(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) { |
| 280 | if (delaykey[i].len > 0 && |
| 281 | presskey_len >= delaykey[i].len && |
| 282 | memcmp(presskey + presskey_len - |
| 283 | delaykey[i].len, delaykey[i].str, |
| 284 | delaykey[i].len) == 0) { |
| 285 | debug_bootkeys("got %skey\n", |
| 286 | delaykey[i].retry ? "delay" : |
| 287 | "stop"); |
| 288 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 289 | /* don't retry auto boot */ |
| 290 | if (!delaykey[i].retry) |
| 291 | bootretry_dont_retry(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 292 | abort = 1; |
| 293 | } |
| 294 | } |
| 295 | } while (!abort && get_ticks() <= etime); |
| 296 | |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 297 | return abort; |
| 298 | } |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 299 | |
Steffen Jaeckel | d199c3a | 2021-07-08 15:57:38 +0200 | [diff] [blame] | 300 | /** |
| 301 | * flush_stdin() - drops all pending characters from stdin |
| 302 | */ |
| 303 | static void flush_stdin(void) |
| 304 | { |
| 305 | while (tstc()) |
| 306 | (void)getchar(); |
| 307 | } |
| 308 | |
Steffen Jaeckel | 3319874 | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 309 | /** |
| 310 | * fallback_to_sha256() - check whether we should fall back to sha256 |
| 311 | * password checking |
| 312 | * |
| 313 | * This checks for the environment variable `bootstopusesha256` in case |
| 314 | * sha256-fallback has been enabled via the config setting |
| 315 | * `AUTOBOOT_SHA256_FALLBACK`. |
| 316 | * |
| 317 | * @return `false` if we must not fall-back, `true` if plain sha256 should be tried |
| 318 | */ |
| 319 | static bool fallback_to_sha256(void) |
| 320 | { |
| 321 | if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK)) |
| 322 | return env_get_yesno("bootstopusesha256") == 1; |
| 323 | else if (IS_ENABLED(CONFIG_CRYPT_PW)) |
| 324 | return false; |
| 325 | else |
| 326 | return true; |
| 327 | } |
| 328 | |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 329 | /*************************************************************************** |
| 330 | * Watch for 'delay' seconds for autoboot stop or autoboot delay string. |
| 331 | * returns: 0 - no key string, allow autoboot 1 - got key string, abort |
| 332 | */ |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 333 | static int abortboot_key_sequence(int bootdelay) |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 334 | { |
| 335 | int abort; |
| 336 | uint64_t etime = endtick(bootdelay); |
| 337 | |
Steffen Jaeckel | d199c3a | 2021-07-08 15:57:38 +0200 | [diff] [blame] | 338 | if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN)) |
| 339 | flush_stdin(); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 340 | # ifdef CONFIG_AUTOBOOT_PROMPT |
| 341 | /* |
| 342 | * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards. |
| 343 | * To print the bootdelay value upon bootup. |
| 344 | */ |
| 345 | printf(CONFIG_AUTOBOOT_PROMPT, bootdelay); |
| 346 | # endif |
| 347 | |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 348 | if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) { |
Steffen Jaeckel | 3319874 | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 349 | if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256()) |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 350 | abort = passwd_abort_crypt(etime); |
| 351 | else |
| 352 | abort = passwd_abort_sha256(etime); |
| 353 | } else { |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 354 | abort = passwd_abort_key(etime); |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 355 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 356 | if (!abort) |
| 357 | debug_bootkeys("key timeout\n"); |
| 358 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 359 | return abort; |
| 360 | } |
| 361 | |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 362 | static int abortboot_single_key(int bootdelay) |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 363 | { |
| 364 | int abort = 0; |
| 365 | unsigned long ts; |
| 366 | |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 367 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 368 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 369 | /* |
| 370 | * Check if key already pressed |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 371 | */ |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 372 | if (tstc()) { /* we got a key press */ |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 373 | getchar(); /* consume input */ |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 374 | puts("\b\b\b 0"); |
| 375 | abort = 1; /* don't auto boot */ |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 376 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 377 | |
| 378 | while ((bootdelay > 0) && (!abort)) { |
| 379 | --bootdelay; |
| 380 | /* delay 1000 ms */ |
| 381 | ts = get_timer(0); |
| 382 | do { |
| 383 | if (tstc()) { /* we got a key press */ |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 384 | int key; |
| 385 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 386 | abort = 1; /* don't auto boot */ |
| 387 | bootdelay = 0; /* no more delay */ |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 388 | key = getchar();/* consume input */ |
Da Xue | 5a87df8 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 389 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY)) |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 390 | menukey = key; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | udelay(10000); |
| 394 | } while (!abort && get_timer(ts) < 1000); |
| 395 | |
| 396 | printf("\b\b\b%2d ", bootdelay); |
| 397 | } |
| 398 | |
| 399 | putc('\n'); |
| 400 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 401 | return abort; |
| 402 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 403 | |
| 404 | static int abortboot(int bootdelay) |
| 405 | { |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 406 | int abort = 0; |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 407 | |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 408 | if (bootdelay >= 0) { |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 409 | if (autoboot_keyed()) |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 410 | abort = abortboot_key_sequence(bootdelay); |
| 411 | else |
| 412 | abort = abortboot_single_key(bootdelay); |
| 413 | } |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 414 | |
Simon Glass | 42b4d14 | 2019-07-20 20:51:18 -0600 | [diff] [blame] | 415 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort) |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 416 | gd->flags &= ~GD_FLG_SILENT; |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 417 | |
| 418 | return abort; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 419 | } |
| 420 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 421 | static void process_fdt_options(const void *blob) |
| 422 | { |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 423 | #ifdef CONFIG_SYS_TEXT_BASE |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 424 | ulong addr; |
| 425 | |
| 426 | /* Add an env variable to point to a kernel payload, if available */ |
| 427 | addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0); |
| 428 | if (addr) |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 429 | env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 430 | |
| 431 | /* Add an env variable to point to a root disk, if available */ |
| 432 | addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0); |
| 433 | if (addr) |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 434 | env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 435 | #endif /* CONFIG_SYS_TEXT_BASE */ |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 436 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 437 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 438 | const char *bootdelay_process(void) |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 439 | { |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 440 | char *s; |
| 441 | int bootdelay; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 442 | |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 443 | bootcount_inc(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 444 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 445 | s = env_get("bootdelay"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 446 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
| 447 | |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 448 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
| 449 | bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay", |
| 450 | bootdelay); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 451 | |
| 452 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); |
| 453 | |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 454 | if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW)) |
| 455 | bootdelay = menu_show(bootdelay); |
Simon Glass | b26440f | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 456 | bootretry_init_cmd_timeout(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 457 | |
| 458 | #ifdef CONFIG_POST |
| 459 | if (gd->flags & GD_FLG_POSTFAIL) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 460 | s = env_get("failbootcmd"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 461 | } else |
| 462 | #endif /* CONFIG_POST */ |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 463 | if (bootcount_error()) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 464 | s = env_get("altbootcmd"); |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 465 | else |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 466 | s = env_get("bootcmd"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 467 | |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 468 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
| 469 | process_fdt_options(gd->fdt_blob); |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 470 | stored_bootdelay = bootdelay; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 471 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 472 | return s; |
| 473 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 474 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 475 | void autoboot_command(const char *s) |
| 476 | { |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 477 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); |
| 478 | |
Heiko Schocher | 6ebe6b3 | 2020-10-07 08:06:54 +0200 | [diff] [blame] | 479 | if (s && (stored_bootdelay == -2 || |
| 480 | (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) { |
Simon Glass | 5ec35ff | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 481 | bool lock; |
| 482 | int prev; |
| 483 | |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 484 | lock = autoboot_keyed() && |
Simon Glass | 5ec35ff | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 485 | !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC); |
| 486 | if (lock) |
| 487 | prev = disable_ctrlc(1); /* disable Ctrl-C checking */ |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 488 | |
| 489 | run_command_list(s, -1, 0); |
| 490 | |
Simon Glass | 5ec35ff | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 491 | if (lock) |
| 492 | disable_ctrlc(prev); /* restore Ctrl-C checking */ |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 493 | } |
| 494 | |
Da Xue | 5a87df8 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 495 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) && |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 496 | menukey == AUTOBOOT_MENUKEY) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 497 | s = env_get("menucmd"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 498 | if (s) |
| 499 | run_command_list(s, -1, 0); |
| 500 | } |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 501 | } |