spl.c 23 KB

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