autoboot.c 8.8 KB

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