autoboot.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. DECLARE_GLOBAL_DATA_PTR;
  26. #define MAX_DELAY_STOP_STR 64
  27. #ifndef DEBUG_BOOTKEYS
  28. #define DEBUG_BOOTKEYS 0
  29. #endif
  30. #define debug_bootkeys(fmt, args...) \
  31. debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
  32. /* Stored value of bootdelay, used by autoboot_command() */
  33. static int stored_bootdelay;
  34. static int menukey;
  35. #ifdef CONFIG_AUTOBOOT_ENCRYPTION
  36. #define AUTOBOOT_STOP_STR_SHA256 CONFIG_AUTOBOOT_STOP_STR_SHA256
  37. #else
  38. #define AUTOBOOT_STOP_STR_SHA256 ""
  39. #endif
  40. #ifdef CONFIG_USE_AUTOBOOT_MENUKEY
  41. #define AUTOBOOT_MENUKEY CONFIG_USE_AUTOBOOT_MENUKEY
  42. #else
  43. #define AUTOBOOT_MENUKEY 0
  44. #endif
  45. /*
  46. * Use a "constant-length" time compare function for this
  47. * hash compare:
  48. *
  49. * https://crackstation.net/hashing-security.htm
  50. */
  51. static int slow_equals(u8 *a, u8 *b, int len)
  52. {
  53. int diff = 0;
  54. int i;
  55. for (i = 0; i < len; i++)
  56. diff |= a[i] ^ b[i];
  57. return diff == 0;
  58. }
  59. /**
  60. * passwd_abort_sha256() - check for a hashed key sequence to abort booting
  61. *
  62. * This checks for the user entering a SHA256 hash within a given time.
  63. *
  64. * @etime: Timeout value ticks (stop when get_ticks() reachs this)
  65. * @return 0 if autoboot should continue, 1 if it should stop
  66. */
  67. static int passwd_abort_sha256(uint64_t etime)
  68. {
  69. const char *sha_env_str = env_get("bootstopkeysha256");
  70. u8 sha_env[SHA256_SUM_LEN];
  71. u8 *sha;
  72. char *presskey;
  73. char *c;
  74. const char *algo_name = "sha256";
  75. u_int presskey_len = 0;
  76. int abort = 0;
  77. int size = sizeof(sha);
  78. int ret;
  79. if (sha_env_str == NULL)
  80. sha_env_str = AUTOBOOT_STOP_STR_SHA256;
  81. presskey = malloc_cache_aligned(MAX_DELAY_STOP_STR);
  82. c = strstr(sha_env_str, ":");
  83. if (c && (c - sha_env_str < MAX_DELAY_STOP_STR)) {
  84. /* preload presskey with salt */
  85. memcpy(presskey, sha_env_str, c - sha_env_str);
  86. presskey_len = c - sha_env_str;
  87. sha_env_str = c + 1;
  88. }
  89. /*
  90. * Generate the binary value from the environment hash value
  91. * so that we can compare this value with the computed hash
  92. * from the user input
  93. */
  94. ret = hash_parse_string(algo_name, sha_env_str, sha_env);
  95. if (ret) {
  96. printf("Hash %s not supported!\n", algo_name);
  97. return 0;
  98. }
  99. sha = malloc_cache_aligned(SHA256_SUM_LEN);
  100. size = SHA256_SUM_LEN;
  101. /*
  102. * We don't know how long the stop-string is, so we need to
  103. * generate the sha256 hash upon each input character and
  104. * compare the value with the one saved in the environment
  105. */
  106. do {
  107. if (tstc()) {
  108. /* Check for input string overflow */
  109. if (presskey_len >= MAX_DELAY_STOP_STR) {
  110. free(presskey);
  111. free(sha);
  112. return 0;
  113. }
  114. presskey[presskey_len++] = getchar();
  115. /* Calculate sha256 upon each new char */
  116. hash_block(algo_name, (const void *)presskey,
  117. presskey_len, sha, &size);
  118. /* And check if sha matches saved value in env */
  119. if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
  120. abort = 1;
  121. }
  122. } while (!abort && get_ticks() <= etime);
  123. free(presskey);
  124. free(sha);
  125. return abort;
  126. }
  127. /**
  128. * passwd_abort_key() - check for a key sequence to aborted booting
  129. *
  130. * This checks for the user entering a string 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_key(uint64_t etime)
  136. {
  137. int abort = 0;
  138. struct {
  139. char *str;
  140. u_int len;
  141. int retry;
  142. }
  143. delaykey[] = {
  144. { .str = env_get("bootdelaykey"), .retry = 1 },
  145. { .str = env_get("bootstopkey"), .retry = 0 },
  146. };
  147. char presskey[MAX_DELAY_STOP_STR];
  148. int presskey_len = 0;
  149. int presskey_max = 0;
  150. int i;
  151. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  152. if (delaykey[0].str == NULL)
  153. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  154. # endif
  155. # ifdef CONFIG_AUTOBOOT_STOP_STR
  156. if (delaykey[1].str == NULL)
  157. delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
  158. # endif
  159. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  160. delaykey[i].len = delaykey[i].str == NULL ?
  161. 0 : strlen(delaykey[i].str);
  162. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  163. MAX_DELAY_STOP_STR : delaykey[i].len;
  164. presskey_max = presskey_max > delaykey[i].len ?
  165. presskey_max : delaykey[i].len;
  166. debug_bootkeys("%s key:<%s>\n",
  167. delaykey[i].retry ? "delay" : "stop",
  168. delaykey[i].str ? delaykey[i].str : "NULL");
  169. }
  170. /* In order to keep up with incoming data, check timeout only
  171. * when catch up.
  172. */
  173. do {
  174. if (tstc()) {
  175. if (presskey_len < presskey_max) {
  176. presskey[presskey_len++] = getchar();
  177. } else {
  178. for (i = 0; i < presskey_max - 1; i++)
  179. presskey[i] = presskey[i + 1];
  180. presskey[i] = getchar();
  181. }
  182. }
  183. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  184. if (delaykey[i].len > 0 &&
  185. presskey_len >= delaykey[i].len &&
  186. memcmp(presskey + presskey_len -
  187. delaykey[i].len, delaykey[i].str,
  188. delaykey[i].len) == 0) {
  189. debug_bootkeys("got %skey\n",
  190. delaykey[i].retry ? "delay" :
  191. "stop");
  192. /* don't retry auto boot */
  193. if (!delaykey[i].retry)
  194. bootretry_dont_retry();
  195. abort = 1;
  196. }
  197. }
  198. } while (!abort && get_ticks() <= etime);
  199. return abort;
  200. }
  201. /***************************************************************************
  202. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  203. * returns: 0 - no key string, allow autoboot 1 - got key string, abort
  204. */
  205. static int abortboot_key_sequence(int bootdelay)
  206. {
  207. int abort;
  208. uint64_t etime = endtick(bootdelay);
  209. # ifdef CONFIG_AUTOBOOT_PROMPT
  210. /*
  211. * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
  212. * To print the bootdelay value upon bootup.
  213. */
  214. printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
  215. # endif
  216. if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION))
  217. abort = passwd_abort_sha256(etime);
  218. else
  219. abort = passwd_abort_key(etime);
  220. if (!abort)
  221. debug_bootkeys("key timeout\n");
  222. return abort;
  223. }
  224. static int abortboot_single_key(int bootdelay)
  225. {
  226. int abort = 0;
  227. unsigned long ts;
  228. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  229. /*
  230. * Check if key already pressed
  231. */
  232. if (tstc()) { /* we got a key press */
  233. getchar(); /* consume input */
  234. puts("\b\b\b 0");
  235. abort = 1; /* don't auto boot */
  236. }
  237. while ((bootdelay > 0) && (!abort)) {
  238. --bootdelay;
  239. /* delay 1000 ms */
  240. ts = get_timer(0);
  241. do {
  242. if (tstc()) { /* we got a key press */
  243. int key;
  244. abort = 1; /* don't auto boot */
  245. bootdelay = 0; /* no more delay */
  246. key = getchar();/* consume input */
  247. if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY))
  248. menukey = key;
  249. break;
  250. }
  251. udelay(10000);
  252. } while (!abort && get_timer(ts) < 1000);
  253. printf("\b\b\b%2d ", bootdelay);
  254. }
  255. putc('\n');
  256. return abort;
  257. }
  258. static int abortboot(int bootdelay)
  259. {
  260. int abort = 0;
  261. if (bootdelay >= 0) {
  262. if (IS_ENABLED(CONFIG_AUTOBOOT_KEYED))
  263. abort = abortboot_key_sequence(bootdelay);
  264. else
  265. abort = abortboot_single_key(bootdelay);
  266. }
  267. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && abort)
  268. gd->flags &= ~GD_FLG_SILENT;
  269. return abort;
  270. }
  271. static void process_fdt_options(const void *blob)
  272. {
  273. #ifdef CONFIG_SYS_TEXT_BASE
  274. ulong addr;
  275. /* Add an env variable to point to a kernel payload, if available */
  276. addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
  277. if (addr)
  278. env_set_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  279. /* Add an env variable to point to a root disk, if available */
  280. addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
  281. if (addr)
  282. env_set_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  283. #endif /* CONFIG_SYS_TEXT_BASE */
  284. }
  285. const char *bootdelay_process(void)
  286. {
  287. char *s;
  288. int bootdelay;
  289. bootcount_inc();
  290. s = env_get("bootdelay");
  291. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  292. if (IS_ENABLED(CONFIG_OF_CONTROL))
  293. bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
  294. bootdelay);
  295. debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  296. if (IS_ENABLED(CONFIG_AUTOBOOT_MENU_SHOW))
  297. bootdelay = menu_show(bootdelay);
  298. bootretry_init_cmd_timeout();
  299. #ifdef CONFIG_POST
  300. if (gd->flags & GD_FLG_POSTFAIL) {
  301. s = env_get("failbootcmd");
  302. } else
  303. #endif /* CONFIG_POST */
  304. if (bootcount_error())
  305. s = env_get("altbootcmd");
  306. else
  307. s = env_get("bootcmd");
  308. if (IS_ENABLED(CONFIG_OF_CONTROL))
  309. process_fdt_options(gd->fdt_blob);
  310. stored_bootdelay = bootdelay;
  311. return s;
  312. }
  313. void autoboot_command(const char *s)
  314. {
  315. debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  316. if (s && (stored_bootdelay == -2 ||
  317. (stored_bootdelay != -1 && !abortboot(stored_bootdelay)))) {
  318. bool lock;
  319. int prev;
  320. lock = IS_ENABLED(CONFIG_AUTOBOOT_KEYED) &&
  321. !IS_ENABLED(CONFIG_AUTOBOOT_KEYED_CTRLC);
  322. if (lock)
  323. prev = disable_ctrlc(1); /* disable Ctrl-C checking */
  324. run_command_list(s, -1, 0);
  325. if (lock)
  326. disable_ctrlc(prev); /* restore Ctrl-C checking */
  327. }
  328. if (IS_ENABLED(CONFIG_USE_AUTOBOOT_MENUKEY) &&
  329. menukey == AUTOBOOT_MENUKEY) {
  330. s = env_get("menucmd");
  331. if (s)
  332. run_command_list(s, -1, 0);
  333. }
  334. }