spl.c 21 KB

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