autoboot.c 9.2 KB

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