spl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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_legacy_header(struct spl_image_info *spl_image,
  265. const struct image_header *header)
  266. {
  267. /* LEGACY image not supported */
  268. debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
  269. return -EINVAL;
  270. }
  271. int spl_parse_image_header(struct spl_image_info *spl_image,
  272. const struct image_header *header)
  273. {
  274. #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
  275. int ret = spl_load_fit_image(spl_image, header);
  276. if (!ret)
  277. return ret;
  278. #endif
  279. if (image_get_magic(header) == IH_MAGIC) {
  280. int ret;
  281. ret = spl_parse_legacy_header(spl_image, header);
  282. if (ret)
  283. return ret;
  284. } else {
  285. #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
  286. /*
  287. * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
  288. * code which loads images in SPL cannot guarantee that
  289. * absolutely all read errors will be reported.
  290. * An example is the LPC32XX MLC NAND driver, which
  291. * will consider that a completely unreadable NAND block
  292. * is bad, and thus should be skipped silently.
  293. */
  294. panic("** no mkimage signature but raw image not supported");
  295. #endif
  296. #ifdef CONFIG_SPL_OS_BOOT
  297. ulong start, end;
  298. if (!bootz_setup((ulong)header, &start, &end)) {
  299. spl_image->name = "Linux";
  300. spl_image->os = IH_OS_LINUX;
  301. spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
  302. spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
  303. spl_image->size = end - start;
  304. debug(SPL_TPL_PROMPT
  305. "payload zImage, load addr: 0x%lx size: %d\n",
  306. spl_image->load_addr, spl_image->size);
  307. return 0;
  308. }
  309. #endif
  310. #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
  311. /* Signature not found - assume u-boot.bin */
  312. debug("mkimage signature not found - ih_magic = %x\n",
  313. header->ih_magic);
  314. spl_set_header_raw_uboot(spl_image);
  315. #else
  316. /* RAW image not supported, proceed to other boot methods. */
  317. debug("Raw boot image support not enabled, proceeding to other boot methods\n");
  318. return -EINVAL;
  319. #endif
  320. }
  321. return 0;
  322. }
  323. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  324. {
  325. typedef void __noreturn (*image_entry_noargs_t)(void);
  326. image_entry_noargs_t image_entry =
  327. (image_entry_noargs_t)spl_image->entry_point;
  328. debug("image entry point: 0x%lx\n", spl_image->entry_point);
  329. image_entry();
  330. }
  331. #if CONFIG_IS_ENABLED(HANDOFF)
  332. /**
  333. * Set up the SPL hand-off information
  334. *
  335. * This is initially empty (zero) but can be written by
  336. */
  337. static int setup_spl_handoff(void)
  338. {
  339. struct spl_handoff *ho;
  340. ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
  341. if (!ho)
  342. return -ENOENT;
  343. return 0;
  344. }
  345. __weak int handoff_arch_save(struct spl_handoff *ho)
  346. {
  347. return 0;
  348. }
  349. static int write_spl_handoff(void)
  350. {
  351. struct spl_handoff *ho;
  352. int ret;
  353. ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
  354. if (!ho)
  355. return -ENOENT;
  356. handoff_save_dram(ho);
  357. ret = handoff_arch_save(ho);
  358. if (ret)
  359. return ret;
  360. debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
  361. return 0;
  362. }
  363. #else
  364. static inline int setup_spl_handoff(void) { return 0; }
  365. static inline int write_spl_handoff(void) { return 0; }
  366. #endif /* HANDOFF */
  367. /**
  368. * get_bootstage_id() - Get the bootstage ID to emit
  369. *
  370. * @start: true if this is for starting SPL, false for ending it
  371. * @return bootstage ID to use
  372. */
  373. static enum bootstage_id get_bootstage_id(bool start)
  374. {
  375. enum u_boot_phase phase = spl_phase();
  376. if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
  377. return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
  378. else
  379. return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
  380. }
  381. static int spl_common_init(bool setup_malloc)
  382. {
  383. int ret;
  384. #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  385. if (setup_malloc) {
  386. #ifdef CONFIG_MALLOC_F_ADDR
  387. gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  388. #endif
  389. gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
  390. gd->malloc_ptr = 0;
  391. }
  392. #endif
  393. ret = bootstage_init(u_boot_first_phase());
  394. if (ret) {
  395. debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
  396. ret);
  397. return ret;
  398. }
  399. #ifdef CONFIG_BOOTSTAGE_STASH
  400. if (!u_boot_first_phase()) {
  401. const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
  402. CONFIG_BOOTSTAGE_STASH_SIZE);
  403. ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
  404. if (ret)
  405. debug("%s: Failed to unstash bootstage: ret=%d\n",
  406. __func__, ret);
  407. }
  408. #endif /* CONFIG_BOOTSTAGE_STASH */
  409. bootstage_mark_name(get_bootstage_id(true),
  410. spl_phase_name(spl_phase()));
  411. #if CONFIG_IS_ENABLED(LOG)
  412. ret = log_init();
  413. if (ret) {
  414. debug("%s: Failed to set up logging\n", __func__);
  415. return ret;
  416. }
  417. #endif
  418. if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  419. ret = fdtdec_setup();
  420. if (ret) {
  421. debug("fdtdec_setup() returned error %d\n", ret);
  422. return ret;
  423. }
  424. }
  425. if (CONFIG_IS_ENABLED(DM)) {
  426. bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
  427. spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
  428. /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
  429. ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
  430. bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
  431. if (ret) {
  432. debug("dm_init_and_scan() returned error %d\n", ret);
  433. return ret;
  434. }
  435. }
  436. return 0;
  437. }
  438. void spl_set_bd(void)
  439. {
  440. /*
  441. * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
  442. * writeable.
  443. */
  444. if (!gd->bd)
  445. gd->bd = &bdata;
  446. }
  447. int spl_early_init(void)
  448. {
  449. int ret;
  450. debug("%s\n", __func__);
  451. ret = spl_common_init(true);
  452. if (ret)
  453. return ret;
  454. gd->flags |= GD_FLG_SPL_EARLY_INIT;
  455. return 0;
  456. }
  457. int spl_init(void)
  458. {
  459. int ret;
  460. bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
  461. IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
  462. debug("%s\n", __func__);
  463. if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
  464. ret = spl_common_init(setup_malloc);
  465. if (ret)
  466. return ret;
  467. }
  468. gd->flags |= GD_FLG_SPL_INIT;
  469. return 0;
  470. }
  471. #ifndef BOOT_DEVICE_NONE
  472. #define BOOT_DEVICE_NONE 0xdeadbeef
  473. #endif
  474. __weak void board_boot_order(u32 *spl_boot_list)
  475. {
  476. spl_boot_list[0] = spl_boot_device();
  477. }
  478. static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
  479. {
  480. struct spl_image_loader *drv =
  481. ll_entry_start(struct spl_image_loader, spl_image_loader);
  482. const int n_ents =
  483. ll_entry_count(struct spl_image_loader, spl_image_loader);
  484. struct spl_image_loader *entry;
  485. for (entry = drv; entry != drv + n_ents; entry++) {
  486. if (boot_device == entry->boot_device)
  487. return entry;
  488. }
  489. /* Not found */
  490. return NULL;
  491. }
  492. static int spl_load_image(struct spl_image_info *spl_image,
  493. struct spl_image_loader *loader)
  494. {
  495. int ret;
  496. struct spl_boot_device bootdev;
  497. bootdev.boot_device = loader->boot_device;
  498. bootdev.boot_device_name = NULL;
  499. ret = loader->load_image(spl_image, &bootdev);
  500. #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
  501. if (!ret && spl_image->dcrc_length) {
  502. /* check data crc */
  503. ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
  504. spl_image->dcrc_length, CHUNKSZ_CRC32);
  505. if (dcrc != spl_image->dcrc) {
  506. puts("SPL: Image data CRC check failed!\n");
  507. ret = -EINVAL;
  508. }
  509. }
  510. #endif
  511. return ret;
  512. }
  513. /**
  514. * boot_from_devices() - Try loading a booting U-Boot from a list of devices
  515. *
  516. * @spl_image: Place to put the image details if successful
  517. * @spl_boot_list: List of boot devices to try
  518. * @count: Number of elements in spl_boot_list
  519. * @return 0 if OK, -ENODEV if there were no boot devices
  520. * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
  521. * devices but none worked
  522. */
  523. static int boot_from_devices(struct spl_image_info *spl_image,
  524. u32 spl_boot_list[], int count)
  525. {
  526. int ret = -ENODEV;
  527. int i;
  528. for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
  529. struct spl_image_loader *loader;
  530. int bootdev = spl_boot_list[i];
  531. if (CONFIG_IS_ENABLED(SHOW_ERRORS))
  532. ret = -ENXIO;
  533. loader = spl_ll_find_loader(bootdev);
  534. if (CONFIG_IS_ENABLED(SERIAL_SUPPORT) &&
  535. CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT) &&
  536. !IS_ENABLED(CONFIG_SILENT_CONSOLE)) {
  537. if (loader)
  538. printf("Trying to boot from %s\n",
  539. spl_loader_name(loader));
  540. else if (CONFIG_IS_ENABLED(SHOW_ERRORS))
  541. printf(SPL_TPL_PROMPT
  542. "Unsupported Boot Device %d\n", bootdev);
  543. else
  544. puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
  545. }
  546. if (loader && !spl_load_image(spl_image, loader)) {
  547. spl_image->boot_device = bootdev;
  548. return 0;
  549. }
  550. }
  551. return ret;
  552. }
  553. #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
  554. void board_init_f(ulong dummy)
  555. {
  556. if (CONFIG_IS_ENABLED(OF_CONTROL)) {
  557. int ret;
  558. ret = spl_early_init();
  559. if (ret) {
  560. debug("spl_early_init() failed: %d\n", ret);
  561. hang();
  562. }
  563. }
  564. preloader_console_init();
  565. }
  566. #endif
  567. void board_init_r(gd_t *dummy1, ulong dummy2)
  568. {
  569. u32 spl_boot_list[] = {
  570. BOOT_DEVICE_NONE,
  571. BOOT_DEVICE_NONE,
  572. BOOT_DEVICE_NONE,
  573. BOOT_DEVICE_NONE,
  574. BOOT_DEVICE_NONE,
  575. };
  576. struct spl_image_info spl_image;
  577. int ret;
  578. debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
  579. spl_set_bd();
  580. #if defined(CONFIG_SYS_SPL_MALLOC_START)
  581. mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
  582. CONFIG_SYS_SPL_MALLOC_SIZE);
  583. gd->flags |= GD_FLG_FULL_MALLOC_INIT;
  584. #endif
  585. if (!(gd->flags & GD_FLG_SPL_INIT)) {
  586. if (spl_init())
  587. hang();
  588. }
  589. #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
  590. /*
  591. * timer_init() does not exist on PPC systems. The timer is initialized
  592. * and enabled (decrementer) in interrupt_init() here.
  593. */
  594. timer_init();
  595. #endif
  596. if (CONFIG_IS_ENABLED(BLOBLIST)) {
  597. ret = bloblist_init();
  598. if (ret) {
  599. debug("%s: Failed to set up bloblist: ret=%d\n",
  600. __func__, ret);
  601. puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
  602. hang();
  603. }
  604. }
  605. if (CONFIG_IS_ENABLED(HANDOFF)) {
  606. int ret;
  607. ret = setup_spl_handoff();
  608. if (ret) {
  609. puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
  610. hang();
  611. }
  612. }
  613. #if CONFIG_IS_ENABLED(BOARD_INIT)
  614. spl_board_init();
  615. #endif
  616. #if defined(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT)
  617. initr_watchdog();
  618. #endif
  619. if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
  620. IS_ENABLED(CONFIG_SPL_ATF))
  621. dram_init_banksize();
  622. bootcount_inc();
  623. memset(&spl_image, '\0', sizeof(spl_image));
  624. #ifdef CONFIG_SYS_SPL_ARGS_ADDR
  625. spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
  626. #endif
  627. spl_image.boot_device = BOOT_DEVICE_NONE;
  628. board_boot_order(spl_boot_list);
  629. ret = boot_from_devices(&spl_image, spl_boot_list,
  630. ARRAY_SIZE(spl_boot_list));
  631. if (ret) {
  632. if (CONFIG_IS_ENABLED(SHOW_ERRORS) &&
  633. CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT))
  634. printf(SPL_TPL_PROMPT "failed to boot from all boot devices (err=%d)\n",
  635. ret);
  636. else
  637. puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
  638. hang();
  639. }
  640. spl_perform_fixups(&spl_image);
  641. if (CONFIG_IS_ENABLED(HANDOFF)) {
  642. ret = write_spl_handoff();
  643. if (ret)
  644. printf(SPL_TPL_PROMPT
  645. "SPL hand-off write failed (err=%d)\n", ret);
  646. }
  647. if (CONFIG_IS_ENABLED(BLOBLIST)) {
  648. ret = bloblist_finish();
  649. if (ret)
  650. printf("Warning: Failed to finish bloblist (ret=%d)\n",
  651. ret);
  652. }
  653. #ifdef CONFIG_CPU_V7M
  654. spl_image.entry_point |= 0x1;
  655. #endif
  656. switch (spl_image.os) {
  657. case IH_OS_U_BOOT:
  658. debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
  659. break;
  660. #if CONFIG_IS_ENABLED(ATF)
  661. case IH_OS_ARM_TRUSTED_FIRMWARE:
  662. debug("Jumping to U-Boot via ARM Trusted Firmware\n");
  663. spl_fixup_fdt(spl_image.fdt_addr);
  664. spl_invoke_atf(&spl_image);
  665. break;
  666. #endif
  667. #if CONFIG_IS_ENABLED(OPTEE)
  668. case IH_OS_TEE:
  669. debug("Jumping to U-Boot via OP-TEE\n");
  670. spl_board_prepare_for_optee(spl_image.fdt_addr);
  671. spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
  672. (void *)spl_image.entry_point);
  673. break;
  674. #endif
  675. #if CONFIG_IS_ENABLED(OPENSBI)
  676. case IH_OS_OPENSBI:
  677. debug("Jumping to U-Boot via RISC-V OpenSBI\n");
  678. spl_invoke_opensbi(&spl_image);
  679. break;
  680. #endif
  681. #ifdef CONFIG_SPL_OS_BOOT
  682. case IH_OS_LINUX:
  683. debug("Jumping to Linux\n");
  684. #if defined(CONFIG_SYS_SPL_ARGS_ADDR)
  685. spl_fixup_fdt((void *)CONFIG_SYS_SPL_ARGS_ADDR);
  686. #endif
  687. spl_board_prepare_for_linux();
  688. jump_to_image_linux(&spl_image);
  689. #endif
  690. default:
  691. debug("Unsupported OS image.. Jumping nevertheless..\n");
  692. }
  693. #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
  694. debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
  695. gd->malloc_ptr / 1024);
  696. #endif
  697. bootstage_mark_name(get_bootstage_id(false), "end phase");
  698. #ifdef CONFIG_BOOTSTAGE_STASH
  699. ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  700. CONFIG_BOOTSTAGE_STASH_SIZE);
  701. if (ret)
  702. debug("Failed to stash bootstage: err=%d\n", ret);
  703. #endif
  704. spl_board_prepare_for_boot();
  705. jump_to_image_no_args(&spl_image);
  706. }
  707. /*
  708. * This requires UART clocks to be enabled. In order for this to work the
  709. * caller must ensure that the gd pointer is valid.
  710. */
  711. void preloader_console_init(void)
  712. {
  713. #ifdef CONFIG_SPL_SERIAL_SUPPORT
  714. gd->baudrate = CONFIG_BAUDRATE;
  715. serial_init(); /* serial communications setup */
  716. gd->have_console = 1;
  717. #if CONFIG_IS_ENABLED(BANNER_PRINT)
  718. puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
  719. U_BOOT_TIME " " U_BOOT_TZ ")\n");
  720. #endif
  721. #ifdef CONFIG_SPL_DISPLAY_PRINT
  722. spl_display_print();
  723. #endif
  724. #endif
  725. }
  726. /**
  727. * This function is called before the stack is changed from initial stack to
  728. * relocated stack. It tries to dump the stack size used
  729. */
  730. __weak void spl_relocate_stack_check(void)
  731. {
  732. #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
  733. ulong init_sp = gd->start_addr_sp;
  734. ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
  735. u8 *ptr = (u8 *)stack_bottom;
  736. ulong i;
  737. for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
  738. if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
  739. break;
  740. ptr++;
  741. }
  742. printf("SPL initial stack usage: %lu bytes\n",
  743. CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
  744. #endif
  745. }
  746. /**
  747. * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  748. *
  749. * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
  750. * for the main board_init_r() execution. This is typically because we need
  751. * more stack space for things like the MMC sub-system.
  752. *
  753. * This function calculates the stack position, copies the global_data into
  754. * place, sets the new gd (except for ARM, for which setting GD within a C
  755. * function may not always work) and returns the new stack position. The
  756. * caller is responsible for setting up the sp register and, in the case
  757. * of ARM, setting up gd.
  758. *
  759. * All of this is done using the same layout and alignments as done in
  760. * board_init_f_init_reserve() / board_init_f_alloc_reserve().
  761. *
  762. * @return new stack location, or 0 to use the same stack
  763. */
  764. ulong spl_relocate_stack_gd(void)
  765. {
  766. #ifdef CONFIG_SPL_STACK_R
  767. gd_t *new_gd;
  768. ulong ptr = CONFIG_SPL_STACK_R_ADDR;
  769. if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
  770. spl_relocate_stack_check();
  771. #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  772. if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
  773. debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
  774. gd->malloc_ptr, gd->malloc_ptr / 1024);
  775. ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  776. gd->malloc_base = ptr;
  777. gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
  778. gd->malloc_ptr = 0;
  779. }
  780. #endif
  781. /* Get stack position: use 8-byte alignment for ABI compliance */
  782. ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
  783. new_gd = (gd_t *)ptr;
  784. memcpy(new_gd, (void *)gd, sizeof(gd_t));
  785. #if CONFIG_IS_ENABLED(DM)
  786. dm_fixup_for_gd_move(new_gd);
  787. #endif
  788. #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
  789. gd = new_gd;
  790. #endif
  791. return ptr;
  792. #else
  793. return 0;
  794. #endif
  795. }
  796. #if defined(CONFIG_BOOTCOUNT_LIMIT) && \
  797. ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
  798. (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
  799. void bootcount_store(ulong a)
  800. {
  801. }
  802. ulong bootcount_load(void)
  803. {
  804. return 0;
  805. }
  806. #endif