autoboot.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <autoboot.h>
  9. #include <bootretry.h>
  10. #include <cli.h>
  11. #include <console.h>
  12. #include <fdtdec.h>
  13. #include <menu.h>
  14. #include <post.h>
  15. #include <u-boot/sha256.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 = getenv("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;
  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 = getenv("bootdelaykey"), .retry = 1 },
  96. { .str = getenv("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_keyed(int bootdelay)
  158. {
  159. int abort;
  160. uint64_t etime = endtick(bootdelay);
  161. #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
  162. if (bootdelay == 0)
  163. return 0;
  164. #endif
  165. # ifdef CONFIG_AUTOBOOT_PROMPT
  166. /*
  167. * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
  168. * To print the bootdelay value upon bootup.
  169. */
  170. printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
  171. # endif
  172. abort = passwd_abort(etime);
  173. if (!abort)
  174. debug_bootkeys("key timeout\n");
  175. #ifdef CONFIG_SILENT_CONSOLE
  176. if (abort)
  177. gd->flags &= ~GD_FLG_SILENT;
  178. #endif
  179. return abort;
  180. }
  181. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  182. #ifdef CONFIG_MENUKEY
  183. static int menukey;
  184. #endif
  185. static int abortboot_normal(int bootdelay)
  186. {
  187. int abort = 0;
  188. unsigned long ts;
  189. #ifdef CONFIG_MENUPROMPT
  190. printf(CONFIG_MENUPROMPT);
  191. #else
  192. if (bootdelay >= 0)
  193. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  194. #endif
  195. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  196. /*
  197. * Check if key already pressed
  198. * Don't check if bootdelay < 0
  199. */
  200. if (bootdelay >= 0) {
  201. if (tstc()) { /* we got a key press */
  202. (void) getc(); /* consume input */
  203. puts("\b\b\b 0");
  204. abort = 1; /* don't auto boot */
  205. }
  206. }
  207. #endif
  208. while ((bootdelay > 0) && (!abort)) {
  209. --bootdelay;
  210. /* delay 1000 ms */
  211. ts = get_timer(0);
  212. do {
  213. if (tstc()) { /* we got a key press */
  214. abort = 1; /* don't auto boot */
  215. bootdelay = 0; /* no more delay */
  216. # ifdef CONFIG_MENUKEY
  217. menukey = getc();
  218. # else
  219. (void) getc(); /* consume input */
  220. # endif
  221. break;
  222. }
  223. udelay(10000);
  224. } while (!abort && get_timer(ts) < 1000);
  225. printf("\b\b\b%2d ", bootdelay);
  226. }
  227. putc('\n');
  228. #ifdef CONFIG_SILENT_CONSOLE
  229. if (abort)
  230. gd->flags &= ~GD_FLG_SILENT;
  231. #endif
  232. return abort;
  233. }
  234. # endif /* CONFIG_AUTOBOOT_KEYED */
  235. static int abortboot(int bootdelay)
  236. {
  237. #ifdef CONFIG_AUTOBOOT_KEYED
  238. return abortboot_keyed(bootdelay);
  239. #else
  240. return abortboot_normal(bootdelay);
  241. #endif
  242. }
  243. static void process_fdt_options(const void *blob)
  244. {
  245. #if defined(CONFIG_OF_CONTROL)
  246. ulong addr;
  247. /* Add an env variable to point to a kernel payload, if available */
  248. addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
  249. if (addr)
  250. setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  251. /* Add an env variable to point to a root disk, if available */
  252. addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
  253. if (addr)
  254. setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  255. #endif /* CONFIG_OF_CONTROL */
  256. }
  257. const char *bootdelay_process(void)
  258. {
  259. char *s;
  260. int bootdelay;
  261. #ifdef CONFIG_BOOTCOUNT_LIMIT
  262. unsigned long bootcount = 0;
  263. unsigned long bootlimit = 0;
  264. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  265. #ifdef CONFIG_BOOTCOUNT_LIMIT
  266. bootcount = bootcount_load();
  267. bootcount++;
  268. bootcount_store(bootcount);
  269. setenv_ulong("bootcount", bootcount);
  270. bootlimit = getenv_ulong("bootlimit", 10, 0);
  271. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  272. s = getenv("bootdelay");
  273. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  274. #ifdef CONFIG_OF_CONTROL
  275. bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
  276. bootdelay);
  277. #endif
  278. debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  279. #if defined(CONFIG_MENU_SHOW)
  280. bootdelay = menu_show(bootdelay);
  281. #endif
  282. bootretry_init_cmd_timeout();
  283. #ifdef CONFIG_POST
  284. if (gd->flags & GD_FLG_POSTFAIL) {
  285. s = getenv("failbootcmd");
  286. } else
  287. #endif /* CONFIG_POST */
  288. #ifdef CONFIG_BOOTCOUNT_LIMIT
  289. if (bootlimit && (bootcount > bootlimit)) {
  290. printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  291. (unsigned)bootlimit);
  292. s = getenv("altbootcmd");
  293. } else
  294. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  295. s = getenv("bootcmd");
  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. #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
  305. int prev = disable_ctrlc(1); /* disable Control C checking */
  306. #endif
  307. run_command_list(s, -1, 0);
  308. #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
  309. disable_ctrlc(prev); /* restore Control C checking */
  310. #endif
  311. }
  312. #ifdef CONFIG_MENUKEY
  313. if (menukey == CONFIG_MENUKEY) {
  314. s = getenv("menucmd");
  315. if (s)
  316. run_command_list(s, -1, 0);
  317. }
  318. #endif /* CONFIG_MENUKEY */
  319. }