autoboot.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #define uboot_version "02.01"
  19. #elif defined(MT29F8G08_BCH16)
  20. #define uboot_version "03.01"
  21. #else
  22. #define uboot_version "01.01"
  23. #endif
  24. #define MAX_DELAY_STOP_STR 32
  25. #ifndef DEBUG_BOOTKEYS
  26. #define DEBUG_BOOTKEYS 0
  27. #endif
  28. #define debug_bootkeys(fmt, args...) \
  29. debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
  30. /* Stored value of bootdelay, used by autoboot_command() */
  31. static int stored_bootdelay;
  32. #if defined(CONFIG_AUTOBOOT_KEYED)
  33. #if defined(CONFIG_AUTOBOOT_STOP_STR_SHA256)
  34. /*
  35. * Use a "constant-length" time compare function for this
  36. * hash compare:
  37. *
  38. * https://crackstation.net/hashing-security.htm
  39. */
  40. static int slow_equals(u8 *a, u8 *b, int len)
  41. {
  42. int diff = 0;
  43. int i;
  44. for (i = 0; i < len; i++)
  45. diff |= a[i] ^ b[i];
  46. return diff == 0;
  47. }
  48. static int passwd_abort(uint64_t etime)
  49. {
  50. const char *sha_env_str = getenv("bootstopkeysha256");
  51. u8 sha_env[SHA256_SUM_LEN];
  52. u8 sha[SHA256_SUM_LEN];
  53. char presskey[MAX_DELAY_STOP_STR];
  54. const char *algo_name = "sha256";
  55. u_int presskey_len = 0;
  56. int abort = 0;
  57. int size;
  58. int ret;
  59. if (sha_env_str == NULL)
  60. sha_env_str = CONFIG_AUTOBOOT_STOP_STR_SHA256;
  61. /*
  62. * Generate the binary value from the environment hash value
  63. * so that we can compare this value with the computed hash
  64. * from the user input
  65. */
  66. ret = hash_parse_string(algo_name, sha_env_str, sha_env);
  67. if (ret) {
  68. printf("Hash %s not supported!\n", algo_name);
  69. return 0;
  70. }
  71. /*
  72. * We don't know how long the stop-string is, so we need to
  73. * generate the sha256 hash upon each input character and
  74. * compare the value with the one saved in the environment
  75. */
  76. do {
  77. if (tstc()) {
  78. /* Check for input string overflow */
  79. if (presskey_len >= MAX_DELAY_STOP_STR)
  80. return 0;
  81. presskey[presskey_len++] = getc();
  82. /* Calculate sha256 upon each new char */
  83. hash_block(algo_name, (const void *)presskey,
  84. presskey_len, sha, &size);
  85. /* And check if sha matches saved value in env */
  86. if (slow_equals(sha, sha_env, SHA256_SUM_LEN))
  87. abort = 1;
  88. }
  89. } while (!abort && get_ticks() <= etime);
  90. return abort;
  91. }
  92. #else
  93. static int passwd_abort(uint64_t etime)
  94. {
  95. int abort = 0;
  96. struct {
  97. char *str;
  98. u_int len;
  99. int retry;
  100. }
  101. delaykey[] = {
  102. { .str = getenv("bootdelaykey"), .retry = 1 },
  103. { .str = getenv("bootstopkey"), .retry = 0 },
  104. };
  105. char presskey[MAX_DELAY_STOP_STR];
  106. u_int presskey_len = 0;
  107. u_int presskey_max = 0;
  108. u_int i;
  109. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  110. if (delaykey[0].str == NULL)
  111. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  112. # endif
  113. # ifdef CONFIG_AUTOBOOT_STOP_STR
  114. if (delaykey[1].str == NULL)
  115. delaykey[1].str = CONFIG_AUTOBOOT_STOP_STR;
  116. # endif
  117. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  118. delaykey[i].len = delaykey[i].str == NULL ?
  119. 0 : strlen(delaykey[i].str);
  120. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  121. MAX_DELAY_STOP_STR : delaykey[i].len;
  122. presskey_max = presskey_max > delaykey[i].len ?
  123. presskey_max : delaykey[i].len;
  124. debug_bootkeys("%s key:<%s>\n",
  125. delaykey[i].retry ? "delay" : "stop",
  126. delaykey[i].str ? delaykey[i].str : "NULL");
  127. }
  128. /* In order to keep up with incoming data, check timeout only
  129. * when catch up.
  130. */
  131. do {
  132. if (tstc()) {
  133. if (presskey_len < presskey_max) {
  134. presskey[presskey_len++] = getc();
  135. } else {
  136. for (i = 0; i < presskey_max - 1; i++)
  137. presskey[i] = presskey[i + 1];
  138. presskey[i] = getc();
  139. }
  140. }
  141. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
  142. if (delaykey[i].len > 0 &&
  143. presskey_len >= delaykey[i].len &&
  144. memcmp(presskey + presskey_len -
  145. delaykey[i].len, delaykey[i].str,
  146. delaykey[i].len) == 0) {
  147. debug_bootkeys("got %skey\n",
  148. delaykey[i].retry ? "delay" :
  149. "stop");
  150. /* don't retry auto boot */
  151. if (!delaykey[i].retry)
  152. bootretry_dont_retry();
  153. abort = 1;
  154. }
  155. }
  156. } while (!abort && get_ticks() <= etime);
  157. return abort;
  158. }
  159. #endif
  160. /***************************************************************************
  161. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  162. * returns: 0 - no key string, allow autoboot 1 - got key string, abort
  163. */
  164. static int __abortboot(int bootdelay)
  165. {
  166. int abort;
  167. uint64_t etime = endtick(bootdelay);
  168. # ifdef CONFIG_AUTOBOOT_PROMPT
  169. /*
  170. * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
  171. * To print the bootdelay value upon bootup.
  172. */
  173. printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
  174. # endif
  175. abort = passwd_abort(etime);
  176. if (!abort)
  177. debug_bootkeys("key timeout\n");
  178. return abort;
  179. }
  180. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  181. #ifdef CONFIG_MENUKEY
  182. static int menukey;
  183. #endif
  184. static int __abortboot(int bootdelay)
  185. {
  186. int abort = 0;
  187. unsigned long ts;
  188. #ifdef CONFIG_MENUPROMPT
  189. printf(CONFIG_MENUPROMPT);
  190. #else
  191. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  192. #endif
  193. /*
  194. * Check if key already pressed
  195. */
  196. if (tstc()) { /* we got a key press */
  197. (void) getc(); /* consume input */
  198. puts("\b\b\b 0");
  199. abort = 1; /* don't auto boot */
  200. }
  201. while ((bootdelay > 0) && (!abort)) {
  202. --bootdelay;
  203. /* delay 1000 ms */
  204. ts = get_timer(0);
  205. do {
  206. if (tstc()) { /* we got a key press */
  207. abort = 1; /* don't auto boot */
  208. bootdelay = 0; /* no more delay */
  209. # ifdef CONFIG_MENUKEY
  210. menukey = getc();
  211. # else
  212. (void) getc(); /* consume input */
  213. # endif
  214. break;
  215. }
  216. udelay(10000);
  217. } while (!abort && get_timer(ts) < 1000);
  218. printf("\b\b\b%2d ", bootdelay);
  219. }
  220. putc('\n');
  221. return abort;
  222. }
  223. # endif /* CONFIG_AUTOBOOT_KEYED */
  224. static int abortboot(int bootdelay)
  225. {
  226. int abort = 0;
  227. if (bootdelay >= 0)
  228. abort = __abortboot(bootdelay);
  229. #ifdef CONFIG_SILENT_CONSOLE
  230. if (abort)
  231. gd->flags &= ~GD_FLG_SILENT;
  232. #endif
  233. return abort;
  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. if(strstr(s, "run nandboot")!=NULL)
  300. {
  301. unsigned char *BufTmp;
  302. unsigned int i,Chk=0;
  303. BufTmp=0x80000000;
  304. memset(BufTmp,0,0x600000);
  305. run_command_list("nand read 0x80000000 0x09000000 0x00600000", -1, 0);
  306. sprintf(BufTmp,&uboot_version,strlen(uboot_version));
  307. for(i=32;i<(0x00300000-4);i++)
  308. {
  309. Chk+=*(BufTmp+i);
  310. }
  311. memcpy(BufTmp+(0x00300000-4),&Chk,4);
  312. Chk=0;
  313. for(i=0;i<(0x00600000-4);i++)
  314. {
  315. Chk+=*(BufTmp+i);
  316. }
  317. memcpy(BufTmp+(0x00600000-4),&Chk,4);
  318. run_command_list("nand erase 0x09000000 0x00600000", -1, 0);
  319. run_command_list("nand write 0x80000000 0x09000000 0x00600000", -1, 0);
  320. }
  321. run_command_list(s, -1, 0);
  322. #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
  323. disable_ctrlc(prev); /* restore Control C checking */
  324. #endif
  325. }
  326. #ifdef CONFIG_MENUKEY
  327. if (menukey == CONFIG_MENUKEY) {
  328. s = getenv("menucmd");
  329. if (s)
  330. run_command_list(s, -1, 0);
  331. }
  332. #endif /* CONFIG_MENUKEY */
  333. }