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> |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 22 | #include <u-boot/sha256.h> |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 23 | #include <bootcount.h> |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
| 27 | #define MAX_DELAY_STOP_STR 32 |
| 28 | |
| 29 | #ifndef DEBUG_BOOTKEYS |
| 30 | #define DEBUG_BOOTKEYS 0 |
| 31 | #endif |
| 32 | #define debug_bootkeys(fmt, args...) \ |
| 33 | debug_cond(DEBUG_BOOTKEYS, fmt, ##args) |
| 34 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 35 | /* Stored value of bootdelay, used by autoboot_command() */ |
| 36 | static int stored_bootdelay; |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 37 | static int menukey; |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 38 | |
Simon Glass | 760d3fb | 2019-07-20 20:51:15 -0600 | [diff] [blame] | 39 | #ifdef CONFIG_AUTOBOOT_ENCRYPTION |
| 40 | #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256 |
| 41 | #else |
| 42 | #define AUTOBOOT_STOP_STR_SHA256 "" |
| 43 | #endif |
| 44 | |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 45 | #ifdef CONFIG_USE_AUTOBOOT_MENUKEY |
| 46 | #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY |
| 47 | #else |
| 48 | #define AUTOBOOT_MENUKEY 0 |
| 49 | #endif |
| 50 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 51 | /* |
| 52 | * Use a "constant-length" time compare function for this |
| 53 | * hash compare: |
| 54 | * |
| 55 | * https://crackstation.net/hashing-security.htm |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 56 | */ |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 57 | static int slow_equals(u8 *a, u8 *b, int len) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 58 | { |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 59 | int diff = 0; |
| 60 | int i; |
| 61 | |
| 62 | for (i = 0; i < len; i++) |
| 63 | diff |= a[i] ^ b[i]; |
| 64 | |
| 65 | return diff == 0; |
| 66 | } |
| 67 | |
Simon Glass | a8cab88 | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 68 | /** |
| 69 | * passwd_abort_sha256() - check for a hashed key sequence to abort booting |
| 70 | * |
| 71 | * This checks for the user entering a SHA256 hash within a given time. |
| 72 | * |
| 73 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
| 74 | * @return 0 if autoboot should continue, 1 if it should stop |
| 75 | */ |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 76 | static int passwd_abort_sha256(uint64_t etime) |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 77 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 78 | const char *sha_env_str = env_get("bootstopkeysha256"); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 79 | u8 sha_env[SHA256_SUM_LEN]; |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 80 | u8 *sha; |
| 81 | char *presskey; |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 82 | const char *algo_name = "sha256"; |
| 83 | u_int presskey_len = 0; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 84 | int abort = 0; |
Martin Etnestad | 055afba | 2018-01-12 09:04:38 +0100 | [diff] [blame] | 85 | int size = sizeof(sha); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 86 | int ret; |
| 87 | |
| 88 | if (sha_env_str == NULL) |
Simon Glass | 760d3fb | 2019-07-20 20:51:15 -0600 | [diff] [blame] | 89 | sha_env_str = AUTOBOOT_STOP_STR_SHA256; |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Generate the binary value from the environment hash value |
| 93 | * so that we can compare this value with the computed hash |
| 94 | * from the user input |
| 95 | */ |
| 96 | ret = hash_parse_string(algo_name, sha_env_str, sha_env); |
| 97 | if (ret) { |
| 98 | printf("Hash %s not supported!\n", algo_name); |
| 99 | return 0; |
| 100 | } |
| 101 | |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 102 | presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR); |
| 103 | sha = malloc_cache_aligned(SHA256_SUM_LEN); |
| 104 | size = SHA256_SUM_LEN; |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 105 | /* |
| 106 | * We don't know how long the stop-string is, so we need to |
| 107 | * generate the sha256 hash upon each input character and |
| 108 | * compare the value with the one saved in the environment |
| 109 | */ |
| 110 | do { |
| 111 | if (tstc()) { |
| 112 | /* Check for input string overflow */ |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 113 | if (presskey_len >= MAX_DELAY_STOP_STR) { |
| 114 | free(presskey); |
| 115 | free(sha); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 116 | return 0; |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 117 | } |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 118 | |
| 119 | presskey[presskey_len++] = getc(); |
| 120 | |
| 121 | /* Calculate sha256 upon each new char */ |
| 122 | hash_block(algo_name, (const void *)presskey, |
| 123 | presskey_len, sha, &size); |
| 124 | |
| 125 | /* And check if sha matches saved value in env */ |
| 126 | if (slow_equals(sha, sha_env, SHA256_SUM_LEN)) |
| 127 | abort = 1; |
| 128 | } |
| 129 | } while (!abort && get_ticks() <= etime); |
| 130 | |
Heiko Schocher | 2ef3a95 | 2019-07-29 07:23:19 +0200 | [diff] [blame] | 131 | free(presskey); |
| 132 | free(sha); |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 133 | return abort; |
| 134 | } |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 135 | |
Simon Glass | a8cab88 | 2019-07-20 20:51:17 -0600 | [diff] [blame] | 136 | /** |
| 137 | * passwd_abort_key() - check for a key sequence to aborted booting |
| 138 | * |
| 139 | * This checks for the user entering a string within a given time. |
| 140 | * |
| 141 | * @etime: Timeout value ticks (stop when get_ticks() reachs this) |
| 142 | * @return 0 if autoboot should continue, 1 if it should stop |
| 143 | */ |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 144 | static int passwd_abort_key(uint64_t etime) |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 145 | { |
| 146 | int abort = 0; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 147 | struct { |
| 148 | char *str; |
| 149 | u_int len; |
| 150 | int retry; |
| 151 | } |
| 152 | delaykey[] = { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 153 | { .str = env_get("bootdelaykey"), .retry = 1 }, |
| 154 | { .str = env_get("bootstopkey"), .retry = 0 }, |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | char presskey[MAX_DELAY_STOP_STR]; |
| 158 | u_int presskey_len = 0; |
| 159 | u_int presskey_max = 0; |
| 160 | u_int i; |
| 161 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 162 | # ifdef CONFIG_AUTOBOOT_DELAY_STR |
| 163 | if (delaykey[0].str == NULL) |
| 164 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; |
| 165 | # endif |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 166 | # ifdef CONFIG_AUTOBOOT_STOP_STR |
Stefan Roese | 0a48c7a | 2015-05-18 14:08:22 +0200 | [diff] [blame] | 167 | if (delaykey[1].str == NULL) |
| 168 | delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 169 | # endif |
| 170 | |
| 171 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) { |
| 172 | delaykey[i].len = delaykey[i].str == NULL ? |
| 173 | 0 : strlen(delaykey[i].str); |
| 174 | delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ? |
| 175 | MAX_DELAY_STOP_STR : delaykey[i].len; |
| 176 | |
| 177 | presskey_max = presskey_max > delaykey[i].len ? |
| 178 | presskey_max : delaykey[i].len; |
| 179 | |
| 180 | debug_bootkeys("%s key:<%s>\n", |
| 181 | delaykey[i].retry ? "delay" : "stop", |
| 182 | delaykey[i].str ? delaykey[i].str : "NULL"); |
| 183 | } |
| 184 | |
| 185 | /* In order to keep up with incoming data, check timeout only |
| 186 | * when catch up. |
| 187 | */ |
| 188 | do { |
| 189 | if (tstc()) { |
| 190 | if (presskey_len < presskey_max) { |
| 191 | presskey[presskey_len++] = getc(); |
| 192 | } else { |
| 193 | for (i = 0; i < presskey_max - 1; i++) |
| 194 | presskey[i] = presskey[i + 1]; |
| 195 | |
| 196 | presskey[i] = getc(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) { |
| 201 | if (delaykey[i].len > 0 && |
| 202 | presskey_len >= delaykey[i].len && |
| 203 | memcmp(presskey + presskey_len - |
| 204 | delaykey[i].len, delaykey[i].str, |
| 205 | delaykey[i].len) == 0) { |
| 206 | debug_bootkeys("got %skey\n", |
| 207 | delaykey[i].retry ? "delay" : |
| 208 | "stop"); |
| 209 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 210 | /* don't retry auto boot */ |
| 211 | if (!delaykey[i].retry) |
| 212 | bootretry_dont_retry(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 213 | abort = 1; |
| 214 | } |
| 215 | } |
| 216 | } while (!abort && get_ticks() <= etime); |
| 217 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 218 | return abort; |
| 219 | } |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 220 | |
| 221 | /*************************************************************************** |
| 222 | * Watch for 'delay' seconds for autoboot stop or autoboot delay string. |
| 223 | * returns: 0 - no key string, allow autoboot 1 - got key string, abort |
| 224 | */ |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 225 | static int abortboot_key_sequence(int bootdelay) |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 226 | { |
| 227 | int abort; |
| 228 | uint64_t etime = endtick(bootdelay); |
| 229 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 230 | # ifdef CONFIG_AUTOBOOT_PROMPT |
| 231 | /* |
| 232 | * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards. |
| 233 | * To print the bootdelay value upon bootup. |
| 234 | */ |
| 235 | printf(CONFIG_AUTOBOOT_PROMPT, bootdelay); |
| 236 | # endif |
| 237 | |
Simon Glass | 6a2e09f | 2019-07-20 20:51:16 -0600 | [diff] [blame] | 238 | if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) |
| 239 | abort = passwd_abort_sha256(etime); |
| 240 | else |
| 241 | abort = passwd_abort_key(etime); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 242 | if (!abort) |
| 243 | debug_bootkeys("key timeout\n"); |
| 244 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 245 | return abort; |
| 246 | } |
| 247 | |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 248 | static int abortboot_single_key(int bootdelay) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 249 | { |
| 250 | int abort = 0; |
| 251 | unsigned long ts; |
| 252 | |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 253 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 254 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 255 | /* |
| 256 | * Check if key already pressed |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 257 | */ |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 258 | if (tstc()) { /* we got a key press */ |
| 259 | (void) getc(); /* consume input */ |
| 260 | puts("\b\b\b 0"); |
| 261 | abort = 1; /* don't auto boot */ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 262 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 263 | |
| 264 | while ((bootdelay > 0) && (!abort)) { |
| 265 | --bootdelay; |
| 266 | /* delay 1000 ms */ |
| 267 | ts = get_timer(0); |
| 268 | do { |
| 269 | if (tstc()) { /* we got a key press */ |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 270 | int key; |
| 271 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 272 | abort = 1; /* don't auto boot */ |
| 273 | bootdelay = 0; /* no more delay */ |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 274 | key = getc(); /* consume input */ |
| 275 | if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY)) |
| 276 | menukey = key; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | udelay(10000); |
| 280 | } while (!abort && get_timer(ts) < 1000); |
| 281 | |
| 282 | printf("\b\b\b%2d ", bootdelay); |
| 283 | } |
| 284 | |
| 285 | putc('\n'); |
| 286 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 287 | return abort; |
| 288 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 289 | |
| 290 | static int abortboot(int bootdelay) |
| 291 | { |
Masahiro Yamada | 9a33386 | 2016-06-27 16:23:04 +0900 | [diff] [blame] | 292 | int abort = 0; |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 293 | |
Simon Glass | 6d99d13 | 2019-07-20 20:51:19 -0600 | [diff] [blame] | 294 | if (bootdelay >= 0) { |
| 295 | if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED)) |
| 296 | abort = abortboot_key_sequence(bootdelay); |
| 297 | else |
| 298 | abort = abortboot_single_key(bootdelay); |
| 299 | } |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 300 | |
Simon Glass | 3a31505 | 2019-07-20 20:51:18 -0600 | [diff] [blame] | 301 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort) |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 302 | gd->flags &= ~GD_FLG_SILENT; |
Masahiro Yamada | d88f872 | 2016-06-27 16:23:03 +0900 | [diff] [blame] | 303 | |
| 304 | return abort; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 305 | } |
| 306 | |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 307 | static void process_fdt_options(const void *blob) |
| 308 | { |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 309 | #ifdef CONFIG_SYS_TEXT_BASE |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 310 | ulong addr; |
| 311 | |
| 312 | /* Add an env variable to point to a kernel payload, if available */ |
| 313 | addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0); |
| 314 | if (addr) |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 315 | env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 316 | |
| 317 | /* Add an env variable to point to a root disk, if available */ |
| 318 | addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0); |
| 319 | if (addr) |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 320 | env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr)); |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 321 | #endif /* CONFIG_SYS_TEXT_BASE */ |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 322 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 323 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 324 | const char *bootdelay_process(void) |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 325 | { |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 326 | char *s; |
| 327 | int bootdelay; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 328 | |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 329 | bootcount_inc(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 330 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 331 | s = env_get("bootdelay"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 332 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
| 333 | |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 334 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
| 335 | bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay", |
| 336 | bootdelay); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 337 | |
| 338 | debug("### main_loop entered: bootdelay=%d\n\n", bootdelay); |
| 339 | |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 340 | if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW)) |
| 341 | bootdelay = menu_show(bootdelay); |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 342 | bootretry_init_cmd_timeout(); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 343 | |
| 344 | #ifdef CONFIG_POST |
| 345 | if (gd->flags & GD_FLG_POSTFAIL) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 346 | s = env_get("failbootcmd"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 347 | } else |
| 348 | #endif /* CONFIG_POST */ |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 349 | if (bootcount_error()) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 350 | s = env_get("altbootcmd"); |
Lukasz Majewski | 4fc1891 | 2018-05-02 16:10:53 +0200 | [diff] [blame] | 351 | else |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 352 | s = env_get("bootcmd"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 353 | |
Simon Glass | b97ace5 | 2019-07-20 20:51:27 -0600 | [diff] [blame] | 354 | if (IS_ENABLED(CONFIG_OF_CONTROL)) |
| 355 | process_fdt_options(gd->fdt_blob); |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 356 | stored_bootdelay = bootdelay; |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 357 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 358 | return s; |
| 359 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 360 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 361 | void autoboot_command(const char *s) |
| 362 | { |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 363 | debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); |
| 364 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 365 | if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) { |
Simon Glass | 313fcbd | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 366 | bool lock; |
| 367 | int prev; |
| 368 | |
| 369 | lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) && |
| 370 | !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC); |
| 371 | if (lock) |
| 372 | prev = disable_ctrlc(1); /* disable Ctrl-C checking */ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 373 | |
| 374 | run_command_list(s, -1, 0); |
| 375 | |
Simon Glass | 313fcbd | 2019-07-20 20:51:28 -0600 | [diff] [blame] | 376 | if (lock) |
| 377 | disable_ctrlc(prev); /* restore Ctrl-C checking */ |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 378 | } |
| 379 | |
Simon Glass | 9d98385 | 2019-07-20 20:51:23 -0600 | [diff] [blame] | 380 | if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) && |
| 381 | menukey == AUTOBOOT_MENUKEY) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 382 | s = env_get("menucmd"); |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 383 | if (s) |
| 384 | run_command_list(s, -1, 0); |
| 385 | } |
Simon Glass | 7b6a95a | 2014-04-10 20:01:28 -0600 | [diff] [blame] | 386 | } |