autoboot.c 8.4 KB

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