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