autoboot.c 9.0 KB

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