autoboot.c 8.6 KB

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