autoboot.c 8.9 KB

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