autoboot.c 12 KB

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