spl.c 20 KB

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