spl.c 21 KB

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