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. c = strstr(sha_env_str, ":");
  151. if (c && (c - sha_env_str < DELAY_STOP_STR_MAX_LENGTH)) {
  152. /* preload presskey with salt */
  153. memcpy(presskey, sha_env_str, c - sha_env_str);
  154. presskey_len = c - sha_env_str;
  155. sha_env_str = c + 1;
  156. }
  157. /*
  158. * Generate the binary value from the environment hash value
  159. * so that we can compare this value with the computed hash
  160. * from the user input
  161. */
  162. ret = hash_parse_string(algo_name, sha_env_str, sha_env);
  163. if (ret) {
  164. printf("Hash %s not supported!\n", algo_name);
  165. return 0;
  166. }
  167. sha = malloc_cache_aligned(SHA256_SUM_LEN);
  168. size = SHA256_SUM_LEN;
  169. /*
  170. * We don't know how long the stop-string is, so we need to
  171. * generate the sha256 hash upon each input character and
  172. * compare the value with the one saved in the environment
  173. */
  174. do {
  175. if (tstc()) {
  176. /* Check for input string overflow */
  177. if (presskey_len >= DELAY_STOP_STR_MAX_LENGTH) {
  178. free(presskey);
  179. free(sha);
  180. return 0;
  181. }
  182. presskey[presskey_len++] = getchar();
  183. /* Calculate sha256 upon each new char */
  184. hash_block(algo_name, (const void *)presskey,
  185. presskey_len, sha, &size);
  186. /* And check if sha matches saved value in env */
  187. if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
  188. abort = 1;
  189. }
  190. udelay(10000);
  191. } while (!abort && get_ticks() <= etime);
  192. free(presskey);
  193. free(sha);
  194. return abort;
  195. }
  196. /**
  197. * passwd_abort_key() - check for a key sequence to aborted booting
  198. *
  199. * This checks for the user entering a string within a given time.
  200. *
  201. * @etime: Timeout value ticks (stop when get_ticks() reachs this)
  202. * Return: 0 if autoboot should continue, 1 if it should stop
  203. */
  204. static int passwd_abort_key(uint64_t etime)
  205. {
  206. int abort = 0;
  207. struct {
  208. char *str;
  209. u_int len;
  210. int retry;
  211. }
  212. delaykey[] = {
  213. { .str = env_get("bootdelaykey"), .retry = 1 },
  214. { .str = env_get("bootstopkey"), .retry = 0 },
  215. };
  216. char presskey[DELAY_STOP_STR_MAX_LENGTH];
  217. int presskey_len = 0;
  218. int presskey_max = 0;
  219. int i;
  220. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  221. if (delaykey[0].str == NULL)
  222. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  223. # endif
  224. # ifdef CONFIG_AUTOBOOT_STOP_STR
  225. if (delaykey[1].str == NULL)
  226. delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
  227. # endif
  228. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  229. delaykey[i].len = delaykey[i].str == NULL ?
  230. 0 : strlen(delaykey[i].str);
  231. delaykey[i].len = delaykey[i].len > DELAY_STOP_STR_MAX_LENGTH ?
  232. DELAY_STOP_STR_MAX_LENGTH : delaykey[i].len;
  233. presskey_max = presskey_max > delaykey[i].len ?
  234. presskey_max : delaykey[i].len;
  235. debug_bootkeys("%s key:<%s>\n",
  236. delaykey[i].retry ? "delay" : "stop",
  237. delaykey[i].str ? delaykey[i].str : "NULL");
  238. }
  239. /* In order to keep up with incoming data, check timeout only
  240. * when catch up.
  241. */
  242. do {
  243. if (tstc()) {
  244. if (presskey_len < presskey_max) {
  245. presskey[presskey_len++] = getchar();
  246. } else {
  247. for (i = 0; i < presskey_max - 1; i++)
  248. presskey[i] = presskey[i + 1];
  249. presskey[i] = getchar();
  250. }
  251. }
  252. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  253. if (delaykey[i].len > 0 &&
  254. presskey_len >= delaykey[i].len &&
  255. memcmp(presskey + presskey_len -
  256. delaykey[i].len, delaykey[i].str,
  257. delaykey[i].len) == 0) {
  258. debug_bootkeys("got %skey\n",
  259. delaykey[i].retry ? "delay" :
  260. "stop");
  261. /* don't retry auto boot */
  262. if (!delaykey[i].retry)
  263. bootretry_dont_retry();
  264. abort = 1;
  265. }
  266. }
  267. udelay(10000);
  268. } while (!abort && get_ticks() <= etime);
  269. return abort;
  270. }
  271. /**
  272. * flush_stdin() - drops all pending characters from stdin
  273. */
  274. static void flush_stdin(void)
  275. {
  276. while (tstc())
  277. (void)getchar();
  278. }
  279. /**
  280. * fallback_to_sha256() - check whether we should fall back to sha256
  281. * password checking
  282. *
  283. * This checks for the environment variable `bootstopusesha256` in case
  284. * sha256-fallback has been enabled via the config setting
  285. * `AUTOBOOT_SHA256_FALLBACK`.
  286. *
  287. * Return: `false` if we must not fall-back, `true` if plain sha256 should be tried
  288. */
  289. static bool fallback_to_sha256(void)
  290. {
  291. if (IS_ENABLED(CONFIG_AUTOBOOT_SHA256_FALLBACK))
  292. return env_get_yesno("bootstopusesha256") == 1;
  293. else if (IS_ENABLED(CONFIG_CRYPT_PW))
  294. return false;
  295. else
  296. return true;
  297. }
  298. /***************************************************************************
  299. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  300. * returns: 0 - no key string, allow autoboot 1 - got key string, abort
  301. */
  302. static int abortboot_key_sequence(int bootdelay)
  303. {
  304. int abort;
  305. uint64_t etime = endtick(bootdelay);
  306. if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
  307. flush_stdin();
  308. # ifdef CONFIG_AUTOBOOT_PROMPT
  309. /*
  310. * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
  311. * To print the bootdelay value upon bootup.
  312. */
  313. printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
  314. # endif
  315. if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
  316. if (IS_ENABLED(CONFIG_CRYPT_PW) && !fallback_to_sha256())
  317. abort = passwd_abort_crypt(etime);
  318. else
  319. abort = passwd_abort_sha256(etime);
  320. } else {
  321. abort = passwd_abort_key(etime);
  322. }
  323. if (!abort)
  324. debug_bootkeys("key timeout\n");
  325. return abort;
  326. }
  327. static int abortboot_single_key(int bootdelay)
  328. {
  329. int abort = 0;
  330. unsigned long ts;
  331. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  332. /*
  333. * Check if key already pressed
  334. */
  335. if (tstc()) { /* we got a key press */
  336. getchar(); /* consume input */
  337. puts("\b\b\b 0");
  338. abort = 1; /* don't auto boot */
  339. }
  340. if(abort == 1) {
  341. extern void ark_watchdog_stop(void);
  342. ark_watchdog_stop();
  343. }
  344. while ((bootdelay > 0) && (!abort)) {
  345. --bootdelay;
  346. /* delay 1000 ms */
  347. ts = get_timer(0);
  348. do {
  349. if (tstc()) { /* we got a key press */
  350. int key;
  351. abort = 1; /* don't auto boot */
  352. bootdelay = 0; /* no more delay */
  353. key = getchar();/* consume input */
  354. if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY))
  355. menukey = key;
  356. break;
  357. }
  358. udelay(10000);
  359. } while (!abort && get_timer(ts) < 1000);
  360. printf("\b\b\b%2d ", bootdelay);
  361. }
  362. putc('\n');
  363. return abort;
  364. }
  365. static int abortboot(int bootdelay)
  366. {
  367. int abort = 0;
  368. if (bootdelay >= 0) {
  369. if (autoboot_keyed())
  370. abort = abortboot_key_sequence(bootdelay);
  371. else
  372. abort = abortboot_single_key(bootdelay);
  373. }
  374. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
  375. gd->flags &= ~GD_FLG_SILENT;
  376. return abort;
  377. }
  378. static void process_fdt_options(void)
  379. {
  380. #ifdef CONFIG_TEXT_BASE
  381. ulong addr;
  382. /* Add an env variable to point to a kernel payload, if available */
  383. addr = ofnode_conf_read_int("kernel-offset", 0);
  384. if (addr)
  385. env_set_addr("kernaddr", (void *)(CONFIG_TEXT_BASE + addr));
  386. /* Add an env variable to point to a root disk, if available */
  387. addr = ofnode_conf_read_int("rootdisk-offset", 0);
  388. if (addr)
  389. env_set_addr("rootaddr", (void *)(CONFIG_TEXT_BASE + addr));
  390. #endif /* CONFIG_TEXT_BASE */
  391. }
  392. const char *bootdelay_process(void)
  393. {
  394. char *s;
  395. int bootdelay;
  396. bootcount_inc();
  397. s = env_get("bootdelay");
  398. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  399. /*
  400. * Does it really make sense that the devicetree overrides the user
  401. * setting? It is possibly helpful for security since the device tree
  402. * may be signed whereas the environment is often loaded from storage.
  403. */
  404. if (IS_ENABLED(CONFIG_OF_CONTROL))
  405. bootdelay = ofnode_conf_read_int("bootdelay", bootdelay);
  406. debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  407. if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
  408. bootdelay = menu_show(bootdelay);
  409. bootretry_init_cmd_timeout();
  410. #ifdef CONFIG_POST
  411. if (gd->flags & GD_FLG_POSTFAIL) {
  412. s = env_get("failbootcmd");
  413. } else
  414. #endif /* CONFIG_POST */
  415. if (bootcount_error())
  416. s = env_get("altbootcmd");
  417. else
  418. s = env_get("bootcmd");
  419. if (IS_ENABLED(CONFIG_OF_CONTROL))
  420. process_fdt_options();
  421. stored_bootdelay = bootdelay;
  422. return s;
  423. }
  424. void autoboot_command(const char *s)
  425. {
  426. debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  427. if (s && (stored_bootdelay == -2 ||
  428. (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
  429. bool lock;
  430. int prev;
  431. lock = autoboot_keyed() &&
  432. !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
  433. if (lock)
  434. prev = disable_ctrlc(1); /* disable Ctrl-C checking */
  435. run_command_list(s, -1, 0);
  436. if (lock)
  437. disable_ctrlc(prev); /* restore Ctrl-C checking */
  438. }
  439. if (IS_ENABLED(CONFIG_AUTOBOOT_USE_MENUKEY) &&
  440. menukey == AUTOBOOT_MENUKEY) {
  441. s = env_get("menucmd");
  442. if (s)
  443. run_command_list(s, -1, 0);
  444. }
  445. }