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