autoboot.c 8.5 KB

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