1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
11 #include <binman_sym.h>
15 #include <asm/u-boot.h>
23 #include <linux/compiler.h>
24 #include <fdt_support.h>
25 #include <bootcount.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 #ifndef CONFIG_SYS_UBOOT_START
31 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
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)
38 u32 *boot_params_ptr = NULL;
40 /* See spl.h for information about this */
41 binman_sym_declare(ulong, u_boot_any, image_pos);
43 /* Define board data structure */
44 static bd_t bdata __attribute__ ((section(".data")));
47 * Board-specific Platform code can reimplement show_boot_progress () if needed
49 __weak void show_boot_progress(int val) {}
51 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF)
52 /* weak, default platform-specific function to initialize dram banks */
53 __weak int dram_init_banksize(void)
60 * Default function to determine if u-boot or the OS should
61 * be started. This implementation always returns 1.
63 * Please implement your own board specific funcion to do this.
66 * 0 to not start u-boot
67 * positive if u-boot should start
69 #ifdef CONFIG_SPL_OS_BOOT
70 __weak int spl_start_uboot(void)
73 "Please implement spl_start_uboot() for your board\n");
74 puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
79 * Weak default function for arch specific zImage check. Return zero
80 * and fill start and end address if image is recognized.
82 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
88 /* Weak default function for arch/board-specific fixups to the spl_image_info */
89 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
93 void spl_fixup_fdt(void)
95 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
96 void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
99 err = fdt_check_header(fdt_blob);
101 printf("fdt_root: %s\n", fdt_strerror(err));
105 /* fixup the memory dt node */
106 err = fdt_shrink_to_minimum(fdt_blob, 0);
108 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
112 err = arch_fixup_fdt(fdt_blob);
114 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
121 * Weak default function for board specific cleanup/preparation before
122 * Linux boot. Some boards/platforms might not need it, so just provide
123 * an empty stub here.
125 __weak void spl_board_prepare_for_linux(void)
130 __weak void spl_board_prepare_for_boot(void)
135 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
137 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
140 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
142 ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
144 spl_image->size = CONFIG_SYS_MONITOR_LEN;
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
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;
156 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
157 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
159 spl_image->os = IH_OS_U_BOOT;
160 spl_image->name = "U-Boot";
163 #ifdef CONFIG_SPL_LOAD_FIT_FULL
164 /* Parse and load full fitImage in SPL */
165 static int spl_load_fit_image(struct spl_image_info *spl_image,
166 const struct image_header *header)
168 bootm_headers_t images;
169 const char *fit_uname_config = NULL;
170 const char *fit_uname_fdt = FIT_FDT_PROP;
172 ulong fw_data = 0, dt_data = 0, img_data = 0;
173 ulong fw_len = 0, dt_len = 0, img_len = 0;
174 int idx, conf_noffset;
177 #ifdef CONFIG_SPL_FIT_SIGNATURE
180 ret = fit_image_load(&images, (ulong)header,
181 NULL, &fit_uname_config,
182 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
183 FIT_LOAD_REQUIRED, &fw_data, &fw_len);
187 spl_image->size = fw_len;
188 spl_image->entry_point = fw_data;
189 spl_image->load_addr = fw_data;
190 spl_image->os = IH_OS_U_BOOT;
191 spl_image->name = "U-Boot";
193 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
194 spl_image->name, spl_image->load_addr, spl_image->size);
196 #ifdef CONFIG_SPL_FIT_SIGNATURE
199 ret = fit_image_load(&images, (ulong)header,
200 &fit_uname_fdt, &fit_uname_config,
201 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
202 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
204 spl_image->fdt_addr = (void *)dt_data;
206 conf_noffset = fit_conf_get_node((const void *)header,
208 if (conf_noffset <= 0)
212 uname = fdt_stringlist_get((const void *)header, conf_noffset,
213 FIT_LOADABLE_PROP, idx,
217 #ifdef CONFIG_SPL_FIT_SIGNATURE
220 ret = fit_image_load(&images, (ulong)header,
221 &uname, &fit_uname_config,
222 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
223 FIT_LOAD_OPTIONAL_NON_ZERO,
224 &img_data, &img_len);
233 int spl_parse_image_header(struct spl_image_info *spl_image,
234 const struct image_header *header)
236 #ifdef CONFIG_SPL_LOAD_FIT_FULL
237 int ret = spl_load_fit_image(spl_image, header);
242 if (image_get_magic(header) == IH_MAGIC) {
243 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
244 u32 header_size = sizeof(struct image_header);
246 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
247 /* check uImage header CRC */
248 if (!image_check_hcrc(header)) {
249 puts("SPL: Image header CRC check failed!\n");
254 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
256 * On some system (e.g. powerpc), the load-address and
257 * entry-point is located at address 0. We can't load
258 * to 0-0x40. So skip header in this case.
260 spl_image->load_addr = image_get_load(header);
261 spl_image->entry_point = image_get_ep(header);
262 spl_image->size = image_get_data_size(header);
264 spl_image->entry_point = image_get_load(header);
265 /* Load including the header */
266 spl_image->load_addr = spl_image->entry_point -
268 spl_image->size = image_get_data_size(header) +
271 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
272 /* store uImage data length and CRC to check later */
273 spl_image->dcrc_data = image_get_load(header);
274 spl_image->dcrc_length = image_get_data_size(header);
275 spl_image->dcrc = image_get_dcrc(header);
278 spl_image->os = image_get_os(header);
279 spl_image->name = image_get_name(header);
281 "payload image: %32s load addr: 0x%lx size: %d\n",
282 spl_image->name, spl_image->load_addr, spl_image->size);
284 /* LEGACY image not supported */
285 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
289 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
291 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
292 * code which loads images in SPL cannot guarantee that
293 * absolutely all read errors will be reported.
294 * An example is the LPC32XX MLC NAND driver, which
295 * will consider that a completely unreadable NAND block
296 * is bad, and thus should be skipped silently.
298 panic("** no mkimage signature but raw image not supported");
301 #ifdef CONFIG_SPL_OS_BOOT
304 if (!bootz_setup((ulong)header, &start, &end)) {
305 spl_image->name = "Linux";
306 spl_image->os = IH_OS_LINUX;
307 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
308 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
309 spl_image->size = end - start;
311 "payload zImage, load addr: 0x%lx size: %d\n",
312 spl_image->load_addr, spl_image->size);
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",
321 spl_set_header_raw_uboot(spl_image);
323 /* RAW image not supported, proceed to other boot methods. */
324 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
332 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
334 typedef void __noreturn (*image_entry_noargs_t)(void);
336 image_entry_noargs_t image_entry =
337 (image_entry_noargs_t)spl_image->entry_point;
339 debug("image entry point: 0x%lx\n", spl_image->entry_point);
343 #if CONFIG_IS_ENABLED(HANDOFF)
345 * Set up the SPL hand-off information
347 * This is initially empty (zero) but can be written by
349 static int setup_spl_handoff(void)
351 struct spl_handoff *ho;
353 ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
360 __weak int handoff_arch_save(struct spl_handoff *ho)
365 static int write_spl_handoff(void)
367 struct spl_handoff *ho;
370 ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
373 handoff_save_dram(ho);
374 ret = handoff_arch_save(ho);
377 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
382 static inline int setup_spl_handoff(void) { return 0; }
383 static inline int write_spl_handoff(void) { return 0; }
387 static int spl_common_init(bool setup_malloc)
391 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
393 #ifdef CONFIG_MALLOC_F_ADDR
394 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
396 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
400 ret = bootstage_init(u_boot_first_phase());
402 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
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);
411 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
413 debug("%s: Failed to unstash bootstage: ret=%d\n",
416 #endif /* CONFIG_BOOTSTAGE_STASH */
417 bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_START_TPL :
418 BOOTSTAGE_ID_START_SPL, SPL_TPL_NAME);
419 #if CONFIG_IS_ENABLED(LOG)
422 debug("%s: Failed to set up logging\n", __func__);
426 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
427 ret = fdtdec_setup();
429 debug("fdtdec_setup() returned error %d\n", ret);
433 if (CONFIG_IS_ENABLED(DM)) {
434 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL,
435 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
436 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
437 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
438 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
440 debug("dm_init_and_scan() returned error %d\n", ret);
448 void spl_set_bd(void)
451 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
458 int spl_early_init(void)
462 debug("%s\n", __func__);
464 ret = spl_common_init(true);
467 gd->flags |= GD_FLG_SPL_EARLY_INIT;
475 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
476 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
478 debug("%s\n", __func__);
480 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
481 ret = spl_common_init(setup_malloc);
485 gd->flags |= GD_FLG_SPL_INIT;
490 #ifndef BOOT_DEVICE_NONE
491 #define BOOT_DEVICE_NONE 0xdeadbeef
494 __weak void board_boot_order(u32 *spl_boot_list)
496 spl_boot_list[0] = spl_boot_device();
499 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
501 struct spl_image_loader *drv =
502 ll_entry_start(struct spl_image_loader, spl_image_loader);
504 ll_entry_count(struct spl_image_loader, spl_image_loader);
505 struct spl_image_loader *entry;
507 for (entry = drv; entry != drv + n_ents; entry++) {
508 if (boot_device == entry->boot_device)
516 static int spl_load_image(struct spl_image_info *spl_image,
517 struct spl_image_loader *loader)
520 struct spl_boot_device bootdev;
522 bootdev.boot_device = loader->boot_device;
523 bootdev.boot_device_name = NULL;
525 ret = loader->load_image(spl_image, &bootdev);
526 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
527 if (!ret && spl_image->dcrc_length) {
529 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
530 spl_image->dcrc_length, CHUNKSZ_CRC32);
531 if (dcrc != spl_image->dcrc) {
532 puts("SPL: Image data CRC check failed!\n");
541 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
543 * @spl_image: Place to put the image details if successful
544 * @spl_boot_list: List of boot devices to try
545 * @count: Number of elements in spl_boot_list
546 * @return 0 if OK, -ve on error
548 static int boot_from_devices(struct spl_image_info *spl_image,
549 u32 spl_boot_list[], int count)
553 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
554 struct spl_image_loader *loader;
556 loader = spl_ll_find_loader(spl_boot_list[i]);
557 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
559 printf("Trying to boot from %s\n", loader->name);
561 puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
563 if (loader && !spl_load_image(spl_image, loader)) {
564 spl_image->boot_device = spl_boot_list[i];
572 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
573 void board_init_f(ulong dummy)
575 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
578 ret = spl_early_init();
580 debug("spl_early_init() failed: %d\n", ret);
585 if (CONFIG_IS_ENABLED(SERIAL_SUPPORT))
586 preloader_console_init();
590 void board_init_r(gd_t *dummy1, ulong dummy2)
592 u32 spl_boot_list[] = {
599 struct spl_image_info spl_image;
602 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
606 #if defined(CONFIG_SYS_SPL_MALLOC_START)
607 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
608 CONFIG_SYS_SPL_MALLOC_SIZE);
609 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
611 if (!(gd->flags & GD_FLG_SPL_INIT)) {
615 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
617 * timer_init() does not exist on PPC systems. The timer is initialized
618 * and enabled (decrementer) in interrupt_init() here.
622 if (CONFIG_IS_ENABLED(BLOBLIST)) {
623 ret = bloblist_init();
625 debug("%s: Failed to set up bloblist: ret=%d\n",
627 puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
631 if (CONFIG_IS_ENABLED(HANDOFF)) {
634 ret = setup_spl_handoff();
636 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
641 #if CONFIG_IS_ENABLED(BOARD_INIT)
645 #if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
649 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
650 dram_init_banksize();
654 memset(&spl_image, '\0', sizeof(spl_image));
655 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
656 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
658 spl_image.boot_device = BOOT_DEVICE_NONE;
659 board_boot_order(spl_boot_list);
661 if (boot_from_devices(&spl_image, spl_boot_list,
662 ARRAY_SIZE(spl_boot_list))) {
663 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
667 spl_perform_fixups(&spl_image);
668 if (CONFIG_IS_ENABLED(HANDOFF)) {
669 ret = write_spl_handoff();
671 printf(SPL_TPL_PROMPT
672 "SPL hand-off write failed (err=%d)\n", ret);
674 if (CONFIG_IS_ENABLED(BLOBLIST)) {
675 ret = bloblist_finish();
677 printf("Warning: Failed to finish bloblist (ret=%d)\n",
681 #ifdef CONFIG_CPU_V7M
682 spl_image.entry_point |= 0x1;
684 switch (spl_image.os) {
686 debug("Jumping to U-Boot\n");
688 #if CONFIG_IS_ENABLED(ATF)
689 case IH_OS_ARM_TRUSTED_FIRMWARE:
690 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
691 spl_invoke_atf(&spl_image);
694 #if CONFIG_IS_ENABLED(OPTEE)
696 debug("Jumping to U-Boot via OP-TEE\n");
697 spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
698 (void *)spl_image.entry_point);
701 #if CONFIG_IS_ENABLED(OPENSBI)
703 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
704 spl_invoke_opensbi(&spl_image);
707 #ifdef CONFIG_SPL_OS_BOOT
709 debug("Jumping to Linux\n");
711 spl_board_prepare_for_linux();
712 jump_to_image_linux(&spl_image);
715 debug("Unsupported OS image.. Jumping nevertheless..\n");
717 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
718 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
719 gd->malloc_ptr / 1024);
721 bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_END_TPL :
722 BOOTSTAGE_ID_END_SPL, "end " SPL_TPL_NAME);
723 #ifdef CONFIG_BOOTSTAGE_STASH
724 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
725 CONFIG_BOOTSTAGE_STASH_SIZE);
727 debug("Failed to stash bootstage: err=%d\n", ret);
730 debug("loaded - jumping to U-Boot...\n");
731 spl_board_prepare_for_boot();
732 jump_to_image_no_args(&spl_image);
735 #ifdef CONFIG_SPL_SERIAL_SUPPORT
737 * This requires UART clocks to be enabled. In order for this to work the
738 * caller must ensure that the gd pointer is valid.
740 void preloader_console_init(void)
742 gd->baudrate = CONFIG_BAUDRATE;
744 serial_init(); /* serial communications setup */
746 gd->have_console = 1;
748 #if CONFIG_IS_ENABLED(BANNER_PRINT)
749 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
750 U_BOOT_TIME " " U_BOOT_TZ ")\n");
752 #ifdef CONFIG_SPL_DISPLAY_PRINT
759 * This function is called before the stack is changed from initial stack to
760 * relocated stack. It tries to dump the stack size used
762 __weak void spl_relocate_stack_check(void)
764 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
765 ulong init_sp = gd->start_addr_sp;
766 ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
767 u8 *ptr = (u8 *)stack_bottom;
770 for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
771 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
775 printf("SPL initial stack usage: %lu bytes\n",
776 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
781 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
783 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
784 * for the main board_init_r() execution. This is typically because we need
785 * more stack space for things like the MMC sub-system.
787 * This function calculates the stack position, copies the global_data into
788 * place, sets the new gd (except for ARM, for which setting GD within a C
789 * function may not always work) and returns the new stack position. The
790 * caller is responsible for setting up the sp register and, in the case
791 * of ARM, setting up gd.
793 * All of this is done using the same layout and alignments as done in
794 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
796 * @return new stack location, or 0 to use the same stack
798 ulong spl_relocate_stack_gd(void)
800 #ifdef CONFIG_SPL_STACK_R
802 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
804 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
805 spl_relocate_stack_check();
807 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
808 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
809 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
810 gd->malloc_ptr, gd->malloc_ptr / 1024);
811 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
812 gd->malloc_base = ptr;
813 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
817 /* Get stack position: use 8-byte alignment for ABI compliance */
818 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
819 new_gd = (gd_t *)ptr;
820 memcpy(new_gd, (void *)gd, sizeof(gd_t));
821 #if CONFIG_IS_ENABLED(DM)
822 dm_fixup_for_gd_move(new_gd);
824 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)