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