spl.c 21 KB

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