autoboot.c 7.8 KB

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