spl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010
  4. * Texas Instruments, <www.ti.com>
  5. *
  6. * Aneesh V <aneesh@ti.com>
  7. */
  8. #include <common.h>
  9. #include <bloblist.h>
  10. #include <binman_sym.h>
  11. #include <bootstage.h>
  12. #include <dm.h>
  13. #include <handoff.h>
  14. #include <hang.h>
  15. #include <irq_func.h>
  16. #include <serial.h>
  17. #include <spl.h>
  18. #include <asm/u-boot.h>
  19. #include <nand.h>
  20. #include <fat.h>
  21. #include <u-boot/crc.h>
  22. #include <version.h>
  23. #include <image.h>
  24. #include <malloc.h>
  25. #include <mapmem.h>
  26. #include <dm/root.h>
  27. #include <linux/compiler.h>
  28. #include <fdt_support.h>
  29. #include <bootcount.h>
  30. #include <wdt.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #ifndef CONFIG_SYS_UBOOT_START
  33. #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
  34. #endif
  35. #ifndef CONFIG_SYS_MONITOR_LEN
  36. /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
  37. #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
  38. #endif
  39. u32 *boot_params_ptr = NULL;
  40. /* See spl.h for information about this */
  41. binman_sym_declare(ulong, u_boot_any, image_pos);
  42. binman_sym_declare(ulong, u_boot_any, size);
  43. #ifdef CONFIG_TPL
  44. binman_sym_declare(ulong, spl, image_pos);
  45. binman_sym_declare(ulong, spl, size);
  46. #endif
  47. /* Define board data structure */
  48. static bd_t bdata __attribute__ ((section(".data")));
  49. /*
  50. * Board-specific Platform code can reimplement show_boot_progress () if needed
  51. */
  52. __weak void show_boot_progress(int val) {}
  53. #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF)
  54. /* weak, default platform-specific function to initialize dram banks */
  55. __weak int dram_init_banksize(void)
  56. {
  57. return 0;
  58. }
  59. #endif
  60. /*
  61. * Default function to determine if u-boot or the OS should
  62. * be started. This implementation always returns 1.
  63. *
  64. * Please implement your own board specific funcion to do this.
  65. *
  66. * RETURN
  67. * 0 to not start u-boot
  68. * positive if u-boot should start
  69. */
  70. #ifdef CONFIG_SPL_OS_BOOT
  71. __weak int spl_start_uboot(void)
  72. {
  73. puts(SPL_TPL_PROMPT
  74. "Please implement spl_start_uboot() for your board\n");
  75. puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
  76. return 1;
  77. }
  78. /*
  79. * Weak default function for arch specific zImage check. Return zero
  80. * and fill start and end address if image is recognized.
  81. */
  82. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  83. {
  84. return 1;
  85. }
  86. #endif
  87. /* Weak default function for arch/board-specific fixups to the spl_image_info */
  88. void __weak spl_perform_fixups(struct spl_image_info *spl_image)
  89. {
  90. }
  91. void spl_fixup_fdt(void)
  92. {
  93. #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
  94. void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  95. int err;
  96. err = fdt_check_header(fdt_blob);
  97. if (err < 0) {
  98. printf("fdt_root: %s\n", fdt_strerror(err));
  99. return;
  100. }
  101. /* fixup the memory dt node */
  102. err = fdt_shrink_to_minimum(fdt_blob, 0);
  103. if (err == 0) {
  104. printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
  105. return;
  106. }
  107. err = arch_fixup_fdt(fdt_blob);
  108. if (err) {
  109. printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
  110. return;
  111. }
  112. #endif
  113. }
  114. ulong spl_get_image_pos(void)
  115. {
  116. return spl_phase() == PHASE_TPL ?
  117. binman_sym(ulong, spl, image_pos) :
  118. binman_sym(ulong, u_boot_any, image_pos);
  119. }
  120. ulong spl_get_image_size(void)
  121. {
  122. return spl_phase() == PHASE_TPL ?
  123. binman_sym(ulong, spl, size) :
  124. binman_sym(ulong, u_boot_any, size);
  125. }
  126. /*
  127. * Weak default function for board specific cleanup/preparation before
  128. * Linux boot. Some boards/platforms might not need it, so just provide
  129. * an empty stub here.
  130. */
  131. __weak void spl_board_prepare_for_linux(void)
  132. {
  133. /* Nothing to do! */
  134. }
  135. __weak void spl_board_prepare_for_boot(void)
  136. {
  137. /* Nothing to do! */
  138. }
  139. __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
  140. {
  141. return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
  142. }
  143. void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
  144. {
  145. ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
  146. spl_image->size = CONFIG_SYS_MONITOR_LEN;
  147. /*
  148. * Binman error cases: address of the end of the previous region or the
  149. * start of the image's entry area (usually 0) if there is no previous
  150. * region.
  151. */
  152. if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
  153. /* Binman does not support separated entry addresses */
  154. spl_image->entry_point = u_boot_pos;
  155. spl_image->load_addr = u_boot_pos;
  156. } else {
  157. spl_image->entry_point = CONFIG_SYS_UBOOT_START;
  158. spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
  159. }
  160. spl_image->os = IH_OS_U_BOOT;
  161. spl_image->name = "U-Boot";
  162. }
  163. #ifdef CONFIG_SPL_LOAD_FIT_FULL
  164. /* Parse and load full fitImage in SPL */
  165. static int spl_load_fit_image(struct spl_image_info *spl_image,
  166. const struct image_header *header)
  167. {
  168. bootm_headers_t images;
  169. const char *fit_uname_config = NULL;
  170. const char *fit_uname_fdt = FIT_FDT_PROP;
  171. const char *uname;
  172. ulong fw_data = 0, dt_data = 0, img_data = 0;
  173. ulong fw_len = 0, dt_len = 0, img_len = 0;
  174. int idx, conf_noffset;
  175. int ret;
  176. #ifdef CONFIG_SPL_FIT_SIGNATURE
  177. images.verify = 1;
  178. #endif
  179. ret = fit_image_load(&images, (ulong)header,
  180. NULL, &fit_uname_config,
  181. IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
  182. FIT_LOAD_REQUIRED, &fw_data, &fw_len);
  183. if (ret < 0)
  184. return ret;
  185. spl_image->size = fw_len;
  186. spl_image->entry_point = fw_data;
  187. spl_image->load_addr = fw_data;
  188. spl_image->os = IH_OS_U_BOOT;
  189. spl_image->name = "U-Boot";
  190. debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
  191. spl_image->name, spl_image->load_addr, spl_image->size);
  192. #ifdef CONFIG_SPL_FIT_SIGNATURE
  193. images.verify = 1;
  194. #endif
  195. ret = fit_image_load(&images, (ulong)header,
  196. &fit_uname_fdt, &fit_uname_config,
  197. IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
  198. FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
  199. if (ret >= 0)
  200. spl_image->fdt_addr = (void *)dt_data;
  201. conf_noffset = fit_conf_get_node((const void *)header,
  202. fit_uname_config);
  203. if (conf_noffset <= 0)
  204. return 0;
  205. for (idx = 0;
  206. uname = fdt_stringlist_get((const void *)header, conf_noffset,
  207. FIT_LOADABLE_PROP, idx,
  208. NULL), uname;
  209. idx++)
  210. {
  211. #ifdef CONFIG_SPL_FIT_SIGNATURE
  212. images.verify = 1;
  213. #endif
  214. ret = fit_image_load(&images, (ulong)header,
  215. &uname, &fit_uname_config,
  216. IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
  217. FIT_LOAD_OPTIONAL_NON_ZERO,
  218. &img_data, &img_len);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. #endif
  225. __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
  226. const struct image_header *header)
  227. {
  228. /* LEGACY image not supported */
  229. debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
  230. return -EINVAL;
  231. }
  232. int spl_parse_image_header(struct spl_image_info *spl_image,
  233. const struct image_header *header)
  234. {
  235. #ifdef CONFIG_SPL_LOAD_FIT_FULL
  236. int ret = spl_load_fit_image(spl_image, header);
  237. if (!ret)
  238. return ret;
  239. #endif
  240. if (image_get_magic(header) == IH_MAGIC) {
  241. int ret;
  242. ret = spl_parse_legacy_header(spl_image, header);
  243. if (ret)
  244. return ret;
  245. } else {
  246. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  247. /*
  248. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  249. * code which loads images in SPL cannot guarantee that
  250. * absolutely all read errors will be reported.
  251. * An example is the LPC32XX MLC NAND driver, which
  252. * will consider that a completely unreadable NAND block
  253. * is bad, and thus should be skipped silently.
  254. */
  255. panic("** no mkimage signature but raw image not supported");
  256. #endif
  257. #ifdef CONFIG_SPL_OS_BOOT
  258. ulong start, end;
  259. if (!bootz_setup((ulong)header, &start, &end)) {
  260. spl_image->name = "Linux";
  261. spl_image->os = IH_OS_LINUX;
  262. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  263. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  264. spl_image->size = end - start;
  265. debug(SPL_TPL_PROMPT
  266. "payload zImage, load addr: 0x%lx size: %d\n",
  267. spl_image->load_addr, spl_image->size);
  268. return 0;
  269. }
  270. #endif
  271. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  272. /* Signature not found - assume u-boot.bin */
  273. debug("mkimage signature not found - ih_magic = %x\n",
  274. header->ih_magic);
  275. spl_set_header_raw_uboot(spl_image);
  276. #else
  277. /* RAW image not supported, proceed to other boot methods. */
  278. debug("Raw boot image support not enabled, proceeding to other boot methods\n");
  279. return -EINVAL;
  280. #endif
  281. }
  282. return 0;
  283. }
  284. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  285. {
  286. typedef void __noreturn (*image_entry_noargs_t)(void);
  287. image_entry_noargs_t image_entry =
  288. (image_entry_noargs_t)spl_image->entry_point;
  289. debug("image entry point: 0x%lx\n", spl_image->entry_point);
  290. image_entry();
  291. }
  292. #if CONFIG_IS_ENABLED(HANDOFF)
  293. /**
  294. * Set up the SPL hand-off information
  295. *
  296. * This is initially empty (zero) but can be written by
  297. */
  298. static int setup_spl_handoff(void)
  299. {
  300. struct spl_handoff *ho;
  301. ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
  302. if (!ho)
  303. return -ENOENT;
  304. return 0;
  305. }
  306. __weak int handoff_arch_save(struct spl_handoff *ho)
  307. {
  308. return 0;
  309. }
  310. static int write_spl_handoff(void)
  311. {
  312. struct spl_handoff *ho;
  313. int ret;
  314. ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
  315. if (!ho)
  316. return -ENOENT;
  317. handoff_save_dram(ho);
  318. ret = handoff_arch_save(ho);
  319. if (ret)
  320. return ret;
  321. debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
  322. return 0;
  323. }
  324. #else
  325. static inline int setup_spl_handoff(void) { return 0; }
  326. static inline int write_spl_handoff(void) { return 0; }
  327. #endif /* HANDOFF */
  328. static int spl_common_init(bool setup_malloc)
  329. {
  330. int ret;
  331. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  332. if (setup_malloc) {
  333. #ifdef CONFIG_MALLOC_F_ADDR
  334. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  335. #endif
  336. gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
  337. gd->malloc_ptr = 0;
  338. }
  339. #endif
  340. ret = bootstage_init(u_boot_first_phase());
  341. if (ret) {
  342. debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
  343. ret);
  344. return ret;
  345. }
  346. #ifdef CONFIG_BOOTSTAGE_STASH
  347. if (!u_boot_first_phase()) {
  348. const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
  349. CONFIG_BOOTSTAGE_STASH_SIZE);
  350. ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
  351. if (ret)
  352. debug("%s: Failed to unstash bootstage: ret=%d\n",
  353. __func__, ret);
  354. }
  355. #endif /* CONFIG_BOOTSTAGE_STASH */
  356. bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_START_TPL :
  357. BOOTSTAGE_ID_START_SPL, SPL_TPL_NAME);
  358. #if CONFIG_IS_ENABLED(LOG)
  359. ret = log_init();
  360. if (ret) {
  361. debug("%s: Failed to set up logging\n", __func__);
  362. return ret;
  363. }
  364. #endif
  365. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  366. ret = fdtdec_setup();
  367. if (ret) {
  368. debug("fdtdec_setup() returned error %d\n", ret);
  369. return ret;
  370. }
  371. }
  372. if (CONFIG_IS_ENABLED(DM)) {
  373. bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
  374. spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
  375. /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
  376. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  377. bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
  378. if (ret) {
  379. debug("dm_init_and_scan() returned error %d\n", ret);
  380. return ret;
  381. }
  382. }
  383. return 0;
  384. }
  385. void spl_set_bd(void)
  386. {
  387. /*
  388. * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
  389. * writeable.
  390. */
  391. if (!gd->bd)
  392. gd->bd = &bdata;
  393. }
  394. int spl_early_init(void)
  395. {
  396. int ret;
  397. debug("%s\n", __func__);
  398. ret = spl_common_init(true);
  399. if (ret)
  400. return ret;
  401. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  402. return 0;
  403. }
  404. int spl_init(void)
  405. {
  406. int ret;
  407. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  408. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  409. debug("%s\n", __func__);
  410. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  411. ret = spl_common_init(setup_malloc);
  412. if (ret)
  413. return ret;
  414. }
  415. gd->flags |= GD_FLG_SPL_INIT;
  416. return 0;
  417. }
  418. #ifndef BOOT_DEVICE_NONE
  419. #define BOOT_DEVICE_NONE 0xdeadbeef
  420. #endif
  421. __weak void board_boot_order(u32 *spl_boot_list)
  422. {
  423. spl_boot_list[0] = spl_boot_device();
  424. }
  425. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  426. {
  427. struct spl_image_loader *drv =
  428. ll_entry_start(struct spl_image_loader, spl_image_loader);
  429. const int n_ents =
  430. ll_entry_count(struct spl_image_loader, spl_image_loader);
  431. struct spl_image_loader *entry;
  432. for (entry = drv; entry != drv + n_ents; entry++) {
  433. if (boot_device == entry->boot_device)
  434. return entry;
  435. }
  436. /* Not found */
  437. return NULL;
  438. }
  439. static int spl_load_image(struct spl_image_info *spl_image,
  440. struct spl_image_loader *loader)
  441. {
  442. int ret;
  443. struct spl_boot_device bootdev;
  444. bootdev.boot_device = loader->boot_device;
  445. bootdev.boot_device_name = NULL;
  446. ret = loader->load_image(spl_image, &bootdev);
  447. #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
  448. if (!ret && spl_image->dcrc_length) {
  449. /* check data crc */
  450. ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
  451. spl_image->dcrc_length, CHUNKSZ_CRC32);
  452. if (dcrc != spl_image->dcrc) {
  453. puts("SPL: Image data CRC check failed!\n");
  454. ret = -EINVAL;
  455. }
  456. }
  457. #endif
  458. return ret;
  459. }
  460. /**
  461. * boot_from_devices() - Try loading a booting U-Boot from a list of devices
  462. *
  463. * @spl_image: Place to put the image details if successful
  464. * @spl_boot_list: List of boot devices to try
  465. * @count: Number of elements in spl_boot_list
  466. * @return 0 if OK, -ve on error
  467. */
  468. static int boot_from_devices(struct spl_image_info *spl_image,
  469. u32 spl_boot_list[], int count)
  470. {
  471. int i;
  472. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  473. struct spl_image_loader *loader;
  474. loader = spl_ll_find_loader(spl_boot_list[i]);
  475. #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  476. if (loader)
  477. printf("Trying to boot from %s\n", loader->name);
  478. else
  479. puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
  480. #endif
  481. if (loader && !spl_load_image(spl_image, loader)) {
  482. spl_image->boot_device = spl_boot_list[i];
  483. return 0;
  484. }
  485. }
  486. return -ENODEV;
  487. }
  488. #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
  489. void board_init_f(ulong dummy)
  490. {
  491. if (CONFIG_IS_ENABLED(OF_CONTROL)) {
  492. int ret;
  493. ret = spl_early_init();
  494. if (ret) {
  495. debug("spl_early_init() failed: %d\n", ret);
  496. hang();
  497. }
  498. }
  499. preloader_console_init();
  500. }
  501. #endif
  502. void board_init_r(gd_t *dummy1, ulong dummy2)
  503. {
  504. u32 spl_boot_list[] = {
  505. BOOT_DEVICE_NONE,
  506. BOOT_DEVICE_NONE,
  507. BOOT_DEVICE_NONE,
  508. BOOT_DEVICE_NONE,
  509. BOOT_DEVICE_NONE,
  510. };
  511. struct spl_image_info spl_image;
  512. int ret;
  513. debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
  514. spl_set_bd();
  515. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  516. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  517. CONFIG_SYS_SPL_MALLOC_SIZE);
  518. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  519. #endif
  520. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  521. if (spl_init())
  522. hang();
  523. }
  524. #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
  525. /*
  526. * timer_init() does not exist on PPC systems. The timer is initialized
  527. * and enabled (decrementer) in interrupt_init() here.
  528. */
  529. timer_init();
  530. #endif
  531. if (CONFIG_IS_ENABLED(BLOBLIST)) {
  532. ret = bloblist_init();
  533. if (ret) {
  534. debug("%s: Failed to set up bloblist: ret=%d\n",
  535. __func__, ret);
  536. puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
  537. hang();
  538. }
  539. }
  540. if (CONFIG_IS_ENABLED(HANDOFF)) {
  541. int ret;
  542. ret = setup_spl_handoff();
  543. if (ret) {
  544. puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
  545. hang();
  546. }
  547. }
  548. #if CONFIG_IS_ENABLED(BOARD_INIT)
  549. spl_board_init();
  550. #endif
  551. #if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
  552. initr_watchdog();
  553. #endif
  554. if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
  555. dram_init_banksize();
  556. bootcount_inc();
  557. memset(&spl_image, '\0', sizeof(spl_image));
  558. #ifdef CONFIG_SYS_SPL_ARGS_ADDR
  559. spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  560. #endif
  561. spl_image.boot_device = BOOT_DEVICE_NONE;
  562. board_boot_order(spl_boot_list);
  563. if (boot_from_devices(&spl_image, spl_boot_list,
  564. ARRAY_SIZE(spl_boot_list))) {
  565. puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
  566. hang();
  567. }
  568. spl_perform_fixups(&spl_image);
  569. if (CONFIG_IS_ENABLED(HANDOFF)) {
  570. ret = write_spl_handoff();
  571. if (ret)
  572. printf(SPL_TPL_PROMPT
  573. "SPL hand-off write failed (err=%d)\n", ret);
  574. }
  575. if (CONFIG_IS_ENABLED(BLOBLIST)) {
  576. ret = bloblist_finish();
  577. if (ret)
  578. printf("Warning: Failed to finish bloblist (ret=%d)\n",
  579. ret);
  580. }
  581. #ifdef CONFIG_CPU_V7M
  582. spl_image.entry_point |= 0x1;
  583. #endif
  584. switch (spl_image.os) {
  585. case IH_OS_U_BOOT:
  586. debug("Jumping to U-Boot\n");
  587. break;
  588. #if CONFIG_IS_ENABLED(ATF)
  589. case IH_OS_ARM_TRUSTED_FIRMWARE:
  590. debug("Jumping to U-Boot via ARM Trusted Firmware\n");
  591. spl_invoke_atf(&spl_image);
  592. break;
  593. #endif
  594. #if CONFIG_IS_ENABLED(OPTEE)
  595. case IH_OS_TEE:
  596. debug("Jumping to U-Boot via OP-TEE\n");
  597. spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
  598. (void *)spl_image.entry_point);
  599. break;
  600. #endif
  601. #if CONFIG_IS_ENABLED(OPENSBI)
  602. case IH_OS_OPENSBI:
  603. debug("Jumping to U-Boot via RISC-V OpenSBI\n");
  604. spl_invoke_opensbi(&spl_image);
  605. break;
  606. #endif
  607. #ifdef CONFIG_SPL_OS_BOOT
  608. case IH_OS_LINUX:
  609. debug("Jumping to Linux\n");
  610. spl_fixup_fdt();
  611. spl_board_prepare_for_linux();
  612. jump_to_image_linux(&spl_image);
  613. #endif
  614. default:
  615. debug("Unsupported OS image.. Jumping nevertheless..\n");
  616. }
  617. #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  618. debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
  619. gd->malloc_ptr / 1024);
  620. #endif
  621. bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_END_TPL :
  622. BOOTSTAGE_ID_END_SPL, "end " SPL_TPL_NAME);
  623. #ifdef CONFIG_BOOTSTAGE_STASH
  624. ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  625. CONFIG_BOOTSTAGE_STASH_SIZE);
  626. if (ret)
  627. debug("Failed to stash bootstage: err=%d\n", ret);
  628. #endif
  629. debug("loaded - jumping to U-Boot...\n");
  630. spl_board_prepare_for_boot();
  631. jump_to_image_no_args(&spl_image);
  632. }
  633. /*
  634. * This requires UART clocks to be enabled. In order for this to work the
  635. * caller must ensure that the gd pointer is valid.
  636. */
  637. void preloader_console_init(void)
  638. {
  639. #ifdef CONFIG_SPL_SERIAL_SUPPORT
  640. gd->baudrate = CONFIG_BAUDRATE;
  641. serial_init(); /* serial communications setup */
  642. gd->have_console = 1;
  643. #if CONFIG_IS_ENABLED(BANNER_PRINT)
  644. puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
  645. U_BOOT_TIME " " U_BOOT_TZ ")\n");
  646. #endif
  647. #ifdef CONFIG_SPL_DISPLAY_PRINT
  648. spl_display_print();
  649. #endif
  650. #endif
  651. }
  652. /**
  653. * This function is called before the stack is changed from initial stack to
  654. * relocated stack. It tries to dump the stack size used
  655. */
  656. __weak void spl_relocate_stack_check(void)
  657. {
  658. #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
  659. ulong init_sp = gd->start_addr_sp;
  660. ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
  661. u8 *ptr = (u8 *)stack_bottom;
  662. ulong i;
  663. for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
  664. if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
  665. break;
  666. ptr++;
  667. }
  668. printf("SPL initial stack usage: %lu bytes\n",
  669. CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
  670. #endif
  671. }
  672. /**
  673. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  674. *
  675. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  676. * for the main board_init_r() execution. This is typically because we need
  677. * more stack space for things like the MMC sub-system.
  678. *
  679. * This function calculates the stack position, copies the global_data into
  680. * place, sets the new gd (except for ARM, for which setting GD within a C
  681. * function may not always work) and returns the new stack position. The
  682. * caller is responsible for setting up the sp register and, in the case
  683. * of ARM, setting up gd.
  684. *
  685. * All of this is done using the same layout and alignments as done in
  686. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  687. *
  688. * @return new stack location, or 0 to use the same stack
  689. */
  690. ulong spl_relocate_stack_gd(void)
  691. {
  692. #ifdef CONFIG_SPL_STACK_R
  693. gd_t *new_gd;
  694. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  695. if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
  696. spl_relocate_stack_check();
  697. #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  698. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  699. debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
  700. gd->malloc_ptr, gd->malloc_ptr / 1024);
  701. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  702. gd->malloc_base = ptr;
  703. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  704. gd->malloc_ptr = 0;
  705. }
  706. #endif
  707. /* Get stack position: use 8-byte alignment for ABI compliance */
  708. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  709. new_gd = (gd_t *)ptr;
  710. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  711. #if CONFIG_IS_ENABLED(DM)
  712. dm_fixup_for_gd_move(new_gd);
  713. #endif
  714. #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
  715. gd = new_gd;
  716. #endif
  717. return ptr;
  718. #else
  719. return 0;
  720. #endif
  721. }
  722. #if defined(CONFIG_BOOTCOUNT_LIMIT) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
  723. void bootcount_store(ulong a)
  724. {
  725. }
  726. ulong bootcount_load(void)
  727. {
  728. return 0;
  729. }
  730. #endif