autoboot.c 8.7 KB

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