autoboot.c 11 KB

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