autoboot.c 8.8 KB

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