spl.c 21 KB

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