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