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 | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 27 | #include <dm/ofnode.h> |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 31 | #define DELAY_STOP_STR_MAX_LENGTH 64 |
Simon Glass | 66ded17 | 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 | affb215 | 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 | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 41 | static int menukey; |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 42 | |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 43 | #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 Glass | 0c4bd31 | 2019-07-20 20:51:15 -0600 | [diff] [blame] | 48 | #endif |
| 49 | |
Da Xue | 5a87df8 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 50 | #ifdef CONFIG_AUTOBOOT_USE_MENUKEY |
Da Xue | 760d2f9 | 2021-07-02 12:32:43 -0400 | [diff] [blame] | 51 | #define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 52 | #else |
| 53 | #define AUTOBOOT_MENUKEY 0 |
| 54 | #endif |
| 55 | |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 56 | /** |
| 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 Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 67 | * 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 Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 71 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 72 | * Return: 0 if autoboot should continue, 1 if it should stop |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 73 | */ |
| 74 | static int passwd_abort_crypt(uint64_t etime) |
| 75 | { |
| 76 | const char *crypt_env_str = env_get("bootstopkeycrypt"); |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 77 | char presskey[DELAY_STOP_STR_MAX_LENGTH]; |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 78 | u_int presskey_len = 0; |
| 79 | int abort = 0; |
Steffen Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 80 | int never_timeout = 0; |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 81 | 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 Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 100 | if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) && |
| 101 | !presskey_len) { |
| 102 | never_timeout = 1; |
| 103 | continue; |
| 104 | } |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 105 | 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 Jaeckel | 6c0ce6d | 2021-07-08 15:57:37 +0200 | [diff] [blame] | 118 | } while (never_timeout || get_ticks() <= etime); |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 119 | |
| 120 | return abort; |
| 121 | } |
| 122 | |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 123 | /* |
| 124 | * Use a "constant-length" time compare function for this |
| 125 | * hash compare: |
| 126 | * |
| 127 | * https://crackstation.net/hashing-security.htm |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 128 | */ |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 129 | static int slow_equals(u8 *a, u8 *b, int len) |
| 130 | { |
| 131 | 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 Glass | 88fa4be | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 140 | /** |
| 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 Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 146 | * Return: 0 if autoboot should continue, 1 if it should stop |
Simon Glass | 88fa4be | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 147 | */ |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 148 | static int passwd_abort_sha256(uint64_t etime) |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 149 | { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 150 | const char *sha_env_str = env_get("bootstopkeysha256"); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 151 | u8 sha_env[SHA256_SUM_LEN]; |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 152 | u8 *sha; |
| 153 | char *presskey; |
Joel Peshkin | 652b504 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 154 | char *c; |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 155 | const char *algo_name = "sha256"; |
| 156 | u_int presskey_len = 0; |
| 157 | int abort = 0; |
Martin Etnestad | 2d06fd8 | 2018-01-12 09:04:38 +0100 | [diff] [blame] | 158 | int size = sizeof(sha); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 159 | int ret; |
| 160 | |
| 161 | if (sha_env_str == NULL) |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 162 | sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256; |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 163 | |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 164 | presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH); |
Joel Peshkin | 652b504 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 165 | c = strstr(sha_env_str, ":"); |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 166 | if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) { |
Joel Peshkin | 652b504 | 2020-11-21 17:18:59 -0800 | [diff] [blame] | 167 | /* 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 Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 172 | /* |
| 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 Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 183 | sha = malloc_cache_aligned(SHA256_SUM_LEN); |
| 184 | size = SHA256_SUM_LEN; |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 185 | /* |
| 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 Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 193 | if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) { |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 194 | free(presskey); |
| 195 | free(sha); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 196 | return 0; |
Heiko Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 197 | } |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 198 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 199 | presskey[presskey_len++] = getchar(); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 200 | |
| 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 Schocher | ecaae80 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 211 | free(presskey); |
| 212 | free(sha); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 213 | return abort; |
| 214 | } |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 215 | |
Simon Glass | 88fa4be | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 216 | /** |
| 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 Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 222 | * Return: 0 if autoboot should continue, 1 if it should stop |
Simon Glass | 88fa4be | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 223 | */ |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 224 | static int passwd_abort_key(uint64_t etime) |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 225 | { |
| 226 | int abort = 0; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 227 | struct { |
| 228 | char *str; |
| 229 | u_int len; |
| 230 | int retry; |
| 231 | } |
| 232 | delaykey[] = { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 233 | { .str = env_get("bootdelaykey"), .retry = 1 }, |
| 234 | { .str = env_get("bootstopkey"), .retry = 0 }, |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 235 | }; |
| 236 | |
Steffen Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 237 | char presskey[DELAY_STOP_STR_MAX_LENGTH]; |
Yuezhang.Mo@sony.com | e088f0c | 2021-01-15 03:11:49 +0000 | [diff] [blame] | 238 | int presskey_len = 0; |
| 239 | int presskey_max = 0; |
| 240 | int i; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 241 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 242 | # ifdef CONFIG_AUTOBOOT_DELAY_STR |
| 243 | if (delaykey[0].str == NULL) |
| 244 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; |
| 245 | # endif |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 246 | # ifdef CONFIG_AUTOBOOT_STOP_STR |
Stefan Roese | 2d908fa | 2015-05-18 14:08:22 +0200 | [diff] [blame] | 247 | if (delaykey[1].str == NULL) |
| 248 | delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 249 | # 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 Jaeckel | 1b2d680 | 2021-07-08 15:57:36 +0200 | [diff] [blame] | 254 | delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ? |
| 255 | DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 256 | |
| 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 Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 271 | presskey[presskey_len++] = getchar(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 272 | } else { |
| 273 | for (i = 0; i < presskey_max - 1; i++) |
| 274 | presskey[i] = presskey[i + 1]; |
| 275 | |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 276 | presskey[i] = getchar(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 277 | } |
| 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 Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 290 | /* don't retry auto boot */ |
| 291 | if (!delaykey[i].retry) |
| 292 | bootretry_dont_retry(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 293 | abort = 1; |
| 294 | } |
| 295 | } |
| 296 | } while (!abort && get_ticks() <= etime); |
| 297 | |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 298 | return abort; |
| 299 | } |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 300 | |
Steffen Jaeckel | d199c3a | 2021-07-08 15:57:38 +0200 | [diff] [blame] | 301 | /** |
| 302 | * flush_stdin() - drops all pending characters from stdin |
| 303 | */ |
| 304 | static void flush_stdin(void) |
| 305 | { |
| 306 | while (tstc()) |
| 307 | (void)getchar(); |
| 308 | } |
| 309 | |
Steffen Jaeckel | 3319874 | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 310 | /** |
| 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 Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 318 | * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried |
Steffen Jaeckel | 3319874 | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 319 | */ |
| 320 | static 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 Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 330 | /*************************************************************************** |
| 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 Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 334 | static int abortboot_key_sequence(int bootdelay) |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 335 | { |
| 336 | int abort; |
| 337 | uint64_t etime = endtick(bootdelay); |
| 338 | |
Steffen Jaeckel | d199c3a | 2021-07-08 15:57:38 +0200 | [diff] [blame] | 339 | if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN)) |
| 340 | flush_stdin(); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 341 | # 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 Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 349 | if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) { |
Steffen Jaeckel | 3319874 | 2021-07-08 15:57:39 +0200 | [diff] [blame] | 350 | if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256()) |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 351 | abort = passwd_abort_crypt(etime); |
| 352 | else |
| 353 | abort = passwd_abort_sha256(etime); |
| 354 | } else { |
Simon Glass | e8c7805 | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 355 | abort = passwd_abort_key(etime); |
Steffen Jaeckel | 1a4a778 | 2021-07-08 15:57:35 +0200 | [diff] [blame] | 356 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 357 | if (!abort) |
| 358 | debug_bootkeys("key timeout\n"); |
| 359 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 360 | return abort; |
| 361 | } |
| 362 | |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 363 | static int abortboot_single_key(int bootdelay) |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 364 | { |
| 365 | int abort = 0; |
| 366 | unsigned long ts; |
| 367 | |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 368 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 369 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 370 | /* |
| 371 | * Check if key already pressed |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 372 | */ |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 373 | if (tstc()) { /* we got a key press */ |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 374 | getchar(); /* consume input */ |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 375 | puts("\b\b\b 0"); |
| 376 | abort = 1; /* don't auto boot */ |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 377 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 378 | |
| 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 Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 385 | int key; |
| 386 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 387 | abort = 1; /* don't auto boot */ |
| 388 | bootdelay = 0; /* no more delay */ |
Heinrich Schuchardt | c670aee | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 389 | key = getchar();/* consume input */ |
Da Xue | 5a87df8 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 390 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY)) |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 391 | menukey = key; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 392 | 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 Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 402 | return abort; |
| 403 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 404 | |
| 405 | static int abortboot(int bootdelay) |
| 406 | { |
Masahiro Yamada | 4632739 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 407 | int abort = 0; |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 408 | |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 409 | if (bootdelay >= 0) { |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 410 | if (autoboot_keyed()) |
Simon Glass | e79e4b2 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 411 | abort = abortboot_key_sequence(bootdelay); |
| 412 | else |
| 413 | abort = abortboot_single_key(bootdelay); |
| 414 | } |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 415 | |
Simon Glass | 42b4d14 | 2019-07-20 20:51:18 -0600 | [diff] [blame] | 416 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort) |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 417 | gd->flags &= ~GD_FLG_SILENT; |
Masahiro Yamada | 09b9d9e | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 418 | |
| 419 | return abort; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 420 | } |
| 421 | |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 422 | static void process_fdt_options(const void *blob) |
| 423 | { |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 424 | #ifdef CONFIG_SYS_TEXT_BASE |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 425 | ulong addr; |
| 426 | |
| 427 | /* Add an env variable to point to a kernel payload, if available */ |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 428 | addr = ofnode_conf_read_int("kernel-offset", 0); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 429 | if (addr) |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 430 | env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 431 | |
| 432 | /* Add an env variable to point to a root disk, if available */ |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 433 | addr = ofnode_conf_read_int("rootdisk-offset", 0); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 434 | if (addr) |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 435 | env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 436 | #endif /* CONFIG_SYS_TEXT_BASE */ |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 437 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 438 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 439 | const char *bootdelay_process(void) |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 440 | { |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 441 | char *s; |
| 442 | int bootdelay; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 443 | |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 444 | bootcount_inc(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 445 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 446 | s = env_get("bootdelay"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 447 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
| 448 | |
Simon Glass | 9d4e88c | 2022-03-11 16:22:38 -0700 | [diff] [blame] | 449 | /* |
| 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 Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 454 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 455 | bootdelay = ofnode_conf_read_int("bootdelay", bootdelay); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 456 | |
| 457 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); |
| 458 | |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 459 | if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW)) |
| 460 | bootdelay = menu_show(bootdelay); |
Simon Glass | b26440f | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 461 | bootretry_init_cmd_timeout(); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 462 | |
| 463 | #ifdef CONFIG_POST |
| 464 | if (gd->flags & GD_FLG_POSTFAIL) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 465 | s = env_get("failbootcmd"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 466 | } else |
| 467 | #endif /* CONFIG_POST */ |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 468 | if (bootcount_error()) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 469 | s = env_get("altbootcmd"); |
Lukasz Majewski | bc8c440 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 470 | else |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 471 | s = env_get("bootcmd"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 472 | |
Simon Glass | 5fa3fd2 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 473 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
| 474 | process_fdt_options(gd->fdt_blob); |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 475 | stored_bootdelay = bootdelay; |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 476 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 477 | return s; |
| 478 | } |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 479 | |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 480 | void autoboot_command(const char *s) |
| 481 | { |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 482 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); |
| 483 | |
Heiko Schocher | 6ebe6b3 | 2020-10-07 08:06:54 +0200 | [diff] [blame] | 484 | if (s && (stored_bootdelay == -2 || |
| 485 | (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) { |
Simon Glass | 5ec35ff | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 486 | bool lock; |
| 487 | int prev; |
| 488 | |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 489 | lock = autoboot_keyed() && |
Simon Glass | 5ec35ff | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 490 | !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC); |
| 491 | if (lock) |
| 492 | prev = disable_ctrlc(1); /* disable Ctrl-C checking */ |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 493 | |
| 494 | run_command_list(s, -1, 0); |
| 495 | |
Simon Glass | 5ec35ff | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 496 | if (lock) |
| 497 | disable_ctrlc(prev); /* restore Ctrl-C checking */ |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 498 | } |
| 499 | |
Da Xue | 5a87df8 | 2021-06-21 22:39:19 -0400 | [diff] [blame] | 500 | if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) && |
Simon Glass | d915ad2 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 501 | menukey == AUTOBOOT_MENUKEY) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 502 | s = env_get("menucmd"); |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 503 | if (s) |
| 504 | run_command_list(s, -1, 0); |
| 505 | } |
Simon Glass | cb89700 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 506 | } |