spl.c 20 KB

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