autoboot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <autoboot.h>
  8. #include <bootretry.h>
  9. #include <cli.h>
  10. #include <command.h>
  11. #include <console.h>
  12. #include <env.h>
  13. #include <fdtdec.h>
  14. #include <hash.h>
  15. #include <log.h>
  16. #include <malloc.h>
  17. #include <memalign.h>
  18. #include <menu.h>
  19. #include <post.h>
  20. #include <time.h>
  21. #include <asm/global_data.h>
  22. #include <linux/delay.h>
  23. #include <u-boot/sha256.h>
  24. #include <bootcount.h>
  25. #include <crypt.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #define DELAY_STOP_STR_MAX_LENGTH 64
  28. #ifndef DEBUG_BOOTKEYS
  29. #define DEBUG_BOOTKEYS 0
  30. #endif
  31. #define debug_bootkeys(fmt, args...) \
  32. debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
  33. /* Stored value of bootdelay, used by autoboot_command() */
  34. static int stored_bootdelay;
  35. static int menukey;
  36. #if !defined(CONFIG_AUTOBOOT_STOP_STR_CRYPT)
  37. #define CONFIG_AUTOBOOT_STOP_STR_CRYPT ""
  38. #endif
  39. #if !defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
  40. #define CONFIG_AUTOBOOT_STOP_STR_SHA256 ""
  41. #endif
  42. #ifdef CONFIG_AUTOBOOT_USE_MENUKEY
  43. #define AUTOBOOT_MENUKEY CONFIG_AUTOBOOT_MENUKEY
  44. #else
  45. #define AUTOBOOT_MENUKEY 0
  46. #endif
  47. /**
  48. * passwd_abort_crypt() - check for a crypt-style hashed key sequence to abort booting
  49. *
  50. * This checks for the user entering a password within a given time.
  51. *
  52. * The entered password is hashed via one of the crypt-style hash methods
  53. * and compared to the pre-defined value from either
  54. * the environment variable "bootstopkeycrypt"
  55. * or
  56. * the config value CONFIG_AUTOBOOT_STOP_STR_CRYPT
  57. *
  58. * In case the config value CONFIG_AUTOBOOT_NEVER_TIMEOUT has been enabled
  59. * this function never times out if the user presses the <Enter> key
  60. * before starting to enter the password.
  61. *
  62. * @etime: Timeout value ticks (stop when get_ticks() reachs this)
  63. * @return 0 if autoboot should continue, 1 if it should stop
  64. */
  65. static int passwd_abort_crypt(uint64_t etime)
  66. {
  67. const char *crypt_env_str = env_get("bootstopkeycrypt");
  68. char presskey[DELAY_STOP_STR_MAX_LENGTH];
  69. u_int presskey_len = 0;
  70. int abort = 0;
  71. int never_timeout = 0;
  72. int err;
  73. if (IS_ENABLED(CONFIG_AUTOBOOT_STOP_STR_ENABLE) && !crypt_env_str)
  74. crypt_env_str = CONFIG_AUTOBOOT_STOP_STR_CRYPT;
  75. if (!crypt_env_str)
  76. return 0;
  77. /* We expect the stop-string to be newline-terminated */
  78. do {
  79. if (tstc()) {
  80. /* Check for input string overflow */
  81. if (presskey_len >= sizeof(presskey))
  82. return 0;
  83. presskey[presskey_len] = getchar();
  84. if ((presskey[presskey_len] == '\r') ||
  85. (presskey[presskey_len] == '\n')) {
  86. if (IS_ENABLED(CONFIG_AUTOBOOT_NEVER_TIMEOUT) &&
  87. !presskey_len) {
  88. never_timeout = 1;
  89. continue;
  90. }
  91. presskey[presskey_len] = '\0';
  92. err = crypt_compare(crypt_env_str, presskey,
  93. &abort);
  94. if (err)
  95. debug_bootkeys(
  96. "crypt_compare() failed with: %s\n",
  97. errno_str(err));
  98. /* you had one chance */
  99. break;
  100. } else {
  101. presskey_len++;
  102. }
  103. }
  104. } while (never_timeout || get_ticks() <= etime);
  105. return abort;
  106. }
  107. /*
  108. * Use a "constant-length" time compare function for this
  109. * hash compare:
  110. *
  111. * https://crackstation.net/hashing-security.htm
  112. */
  113. static int slow_equals(u8 *a, u8 *b, int len)
  114. {
  115. int diff = 0;
  116. int i;
  117. for (i = 0; i < len; i++)
  118. diff |= a[i] ^ b[i];
  119. return diff == 0;
  120. }
  121. /**
  122. * passwd_abort_sha256() - check for a hashed key sequence to abort booting
  123. *
  124. * This checks for the user entering a SHA256 hash within a given time.
  125. *
  126. * @etime: Timeout value ticks (stop when get_ticks() reachs this)
  127. * @return 0 if autoboot should continue, 1 if it should stop
  128. */
  129. static int passwd_abort_sha256(uint64_t etime)
  130. {
  131. const char *sha_env_str = env_get("bootstopkeysha256");
  132. u8 sha_env[SHA256_SUM_LEN];
  133. u8 *sha;
  134. char *presskey;
  135. char *c;
  136. const char *algo_name = "sha256";
  137. u_int presskey_len = 0;
  138. int abort = 0;
  139. int size = sizeof(sha);
  140. int ret;
  141. if (sha_env_str == NULL)
  142. sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
  143. presskey = malloc_cache_aligned(DELAY_STOP_STR_MAX_LENGTH);
  144. c = strstr(sha_env_str, ":");
  145. if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
  146. /* preload presskey with salt */
  147. memcpy(presskey, sha_env_str, c - sha_env_str);
  148. presskey_len = c - sha_env_str;
  149. sha_env_str = c + 1;
  150. }
  151. /*
  152. * Generate the binary value from the environment hash value
  153. * so that we can compare this value with the computed hash
  154. * from the user input
  155. */
  156. ret = hash_parse_string(algo_name, sha_env_str, sha_env);
  157. if (ret) {
  158. printf("Hash %s not supported!\n", algo_name);
  159. return 0;
  160. }
  161. sha = malloc_cache_aligned(SHA256_SUM_LEN);
  162. size = SHA256_SUM_LEN;
  163. /*
  164. * We don't know how long the stop-string is, so we need to
  165. * generate the sha256 hash upon each input character and
  166. * compare the value with the one saved in the environment
  167. */
  168. do {
  169. if (tstc()) {
  170. /* Check for input string overflow */
  171. if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
  172. free(presskey);
  173. free(sha);
  174. return 0;
  175. }
  176. presskey[presskey_len++] = getchar();
  177. /* Calculate sha256 upon each new char */
  178. hash_block(algo_name, (const void *)presskey,
  179. presskey_len, sha, &size);
  180. /* And check if sha matches saved value in env */
  181. if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
  182. abort = 1;
  183. }
  184. } while (!abort && get_ticks() <= etime);
  185. free(presskey);
  186. free(sha);
  187. return abort;
  188. }
  189. /**
  190. * passwd_abort_key() - check for a key sequence to aborted booting
  191. *
  192. * This checks for the user entering a string within a given time.
  193. *
  194. * @etime: Timeout value ticks (stop when get_ticks() reachs this)
  195. * @return 0 if autoboot should continue, 1 if it should stop
  196. */
  197. static int passwd_abort_key(uint64_t etime)
  198. {
  199. int abort = 0;
  200. struct {
  201. char *str;
  202. u_int len;
  203. int retry;
  204. }
  205. delaykey[] = {
  206. { .str = env_get("bootdelaykey"), .retry = 1 },
  207. { .str = env_get("bootstopkey"), .retry = 0 },
  208. };
  209. char presskey[DELAY_STOP_STR_MAX_LENGTH];
  210. int presskey_len = 0;
  211. int presskey_max = 0;
  212. int i;
  213. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  214. if (delaykey[0].str == NULL)
  215. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  216. # endif
  217. # ifdef CONFIG_AUTOBOOT_STOP_STR
  218. if (delaykey[1].str == NULL)
  219. delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
  220. # endif
  221. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  222. delaykey[i].len = delaykey[i].str == NULL ?
  223. 0 : strlen(delaykey[i].str);
  224. delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
  225. DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
  226. presskey_max = presskey_max > delaykey[i].len ?
  227. presskey_max : delaykey[i].len;
  228. debug_bootkeys("%s key:<%s>\n",
  229. delaykey[i].retry ? "delay" : "stop",
  230. delaykey[i].str ? delaykey[i].str : "NULL");
  231. }
  232. /* In order to keep up with incoming data, check timeout only
  233. * when catch up.
  234. */
  235. do {
  236. if (tstc()) {
  237. if (presskey_len < presskey_max) {
  238. presskey[presskey_len++] = getchar();
  239. } else {
  240. for (i = 0; i < presskey_max - 1; i++)
  241. presskey[i] = presskey[i + 1];
  242. presskey[i] = getchar();
  243. }
  244. }
  245. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  246. if (delaykey[i].len > 0 &&
  247. presskey_len >= delaykey[i].len &&
  248. memcmp(presskey + presskey_len -
  249. delaykey[i].len, delaykey[i].str,
  250. delaykey[i].len) == 0) {
  251. debug_bootkeys("got %skey\n",
  252. delaykey[i].retry ? "delay" :
  253. "stop");
  254. /* don't retry auto boot */
  255. if (!delaykey[i].retry)
  256. bootretry_dont_retry();
  257. abort = 1;
  258. }
  259. }
  260. } while (!abort && get_ticks() <= etime);
  261. return abort;
  262. }
  263. /**
  264. * flush_stdin() - drops all pending characters from stdin
  265. */
  266. static void flush_stdin(void)
  267. {
  268. while (tstc())
  269. (void)getchar();
  270. }
  271. /**
  272. * fallback_to_sha256() - check whether we should fall back to sha256
  273. * password checking
  274. *
  275. * This checks for the environment variable `bootstopusesha256` in case
  276. * sha256-fallback has been enabled via the config setting
  277. * `AUTOBOOT_SHA256_FALLBACK`.
  278. *
  279. * @return `false` if we must not fall-back, `true` if plain sha256 should be tried
  280. */
  281. static bool fallback_to_sha256(void)
  282. {
  283. if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
  284. return env_get_yesno("bootstopusesha256") == 1;
  285. else if (IS_ENABLED(CONFIG_CRYPT_PW))
  286. return false;
  287. else
  288. return true;
  289. }
  290. /***************************************************************************
  291. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  292. * returns: 0 - no key string, allow autoboot 1 - got key string, abort
  293. */
  294. static int abortboot_key_sequence(int bootdelay)
  295. {
  296. int abort;
  297. uint64_t etime = endtick(bootdelay);
  298. if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
  299. flush_stdin();
  300. # ifdef CONFIG_AUTOBOOT_PROMPT
  301. /*
  302. * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
  303. * To print the bootdelay value upon bootup.
  304. */
  305. printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
  306. # endif
  307. if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
  308. if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
  309. abort = passwd_abort_crypt(etime);
  310. else
  311. abort = passwd_abort_sha256(etime);
  312. } else {
  313. abort = passwd_abort_key(etime);
  314. }
  315. if (!abort)
  316. debug_bootkeys("key timeout\n");
  317. return abort;
  318. }
  319. static int abortboot_single_key(int bootdelay)
  320. {
  321. int abort = 0;
  322. unsigned long ts;
  323. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  324. /*
  325. * Check if key already pressed
  326. */
  327. if (tstc()) { /* we got a key press */
  328. getchar(); /* consume input */
  329. puts("\b\b\b 0");
  330. abort = 1; /* don't auto boot */
  331. }
  332. while ((bootdelay > 0) && (!abort)) {
  333. --bootdelay;
  334. /* delay 1000 ms */
  335. ts = get_timer(0);
  336. do {
  337. if (tstc()) { /* we got a key press */
  338. int key;
  339. abort = 1; /* don't auto boot */
  340. bootdelay = 0; /* no more delay */
  341. key = getchar();/* consume input */
  342. if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
  343. menukey = key;
  344. break;
  345. }
  346. udelay(10000);
  347. } while (!abort && get_timer(ts) < 1000);
  348. printf("\b\b\b%2d ", bootdelay);
  349. }
  350. putc('\n');
  351. return abort;
  352. }
  353. static int abortboot(int bootdelay)
  354. {
  355. int abort = 0;
  356. if (bootdelay >= 0) {
  357. if (autoboot_keyed())
  358. abort = abortboot_key_sequence(bootdelay);
  359. else
  360. abort = abortboot_single_key(bootdelay);
  361. }
  362. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
  363. gd->flags &= ~GD_FLG_SILENT;
  364. return abort;
  365. }
  366. static void process_fdt_options(const void *blob)
  367. {
  368. #ifdef CONFIG_SYS_TEXT_BASE
  369. ulong addr;
  370. /* Add an env variable to point to a kernel payload, if available */
  371. addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
  372. if (addr)
  373. env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  374. /* Add an env variable to point to a root disk, if available */
  375. addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
  376. if (addr)
  377. env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  378. #endif /* CONFIG_SYS_TEXT_BASE */
  379. }
  380. const char *bootdelay_process(void)
  381. {
  382. char *s;
  383. int bootdelay;
  384. bootcount_inc();
  385. s = env_get("bootdelay");
  386. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  387. if (IS_ENABLED(CONFIG_OF_CONTROL))
  388. bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
  389. bootdelay);
  390. debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  391. if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
  392. bootdelay = menu_show(bootdelay);
  393. bootretry_init_cmd_timeout();
  394. #ifdef CONFIG_POST
  395. if (gd->flags & GD_FLG_POSTFAIL) {
  396. s = env_get("failbootcmd");
  397. } else
  398. #endif /* CONFIG_POST */
  399. if (bootcount_error())
  400. s = env_get("altbootcmd");
  401. else
  402. s = env_get("bootcmd");
  403. if (IS_ENABLED(CONFIG_OF_CONTROL))
  404. process_fdt_options(gd->fdt_blob);
  405. stored_bootdelay = bootdelay;
  406. return s;
  407. }
  408. void autoboot_command(const char *s)
  409. {
  410. debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  411. if (s && (stored_bootdelay == -2 ||
  412. (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
  413. bool lock;
  414. int prev;
  415. lock = autoboot_keyed() &&
  416. !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
  417. if (lock)
  418. prev = disable_ctrlc(1); /* disable Ctrl-C checking */
  419. run_command_list(s, -1, 0);
  420. if (lock)
  421. disable_ctrlc(prev); /* restore Ctrl-C checking */
  422. }
  423. if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
  424. menukey == AUTOBOOT_MENUKEY) {
  425. s = env_get("menucmd");
  426. if (s)
  427. run_command_list(s, -1, 0);
  428. }
  429. }