1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
6 * Aneesh V <aneesh@ti.com>
11 #include <binman_sym.h>
15 #include <asm/u-boot.h>
22 #include <linux/compiler.h>
23 #include <fdt_support.h>
24 #include <bootcount.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifndef CONFIG_SYS_UBOOT_START
29 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
31 #ifndef CONFIG_SYS_MONITOR_LEN
32 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
33 #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
36 u32 *boot_params_ptr = NULL;
38 /* See spl.h for information about this */
39 binman_sym_declare(ulong, u_boot_any, image_pos);
41 /* Define board data structure */
42 static bd_t bdata __attribute__ ((section(".data")));
45 * Board-specific Platform code can reimplement show_boot_progress () if needed
47 __weak void show_boot_progress(int val) {}
49 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF)
50 /* weak, default platform-specific function to initialize dram banks */
51 __weak int dram_init_banksize(void)
58 * Default function to determine if u-boot or the OS should
59 * be started. This implementation always returns 1.
61 * Please implement your own board specific funcion to do this.
64 * 0 to not start u-boot
65 * positive if u-boot should start
67 #ifdef CONFIG_SPL_OS_BOOT
68 __weak int spl_start_uboot(void)
71 "Please implement spl_start_uboot() for your board\n");
72 puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
77 * Weak default function for arch specific zImage check. Return zero
78 * and fill start and end address if image is recognized.
80 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
86 /* Weak default function for arch/board-specific fixups to the spl_image_info */
87 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
91 void spl_fixup_fdt(void)
93 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
94 void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
97 err = fdt_check_header(fdt_blob);
99 printf("fdt_root: %s\n", fdt_strerror(err));
103 /* fixup the memory dt node */
104 err = fdt_shrink_to_minimum(fdt_blob, 0);
106 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
110 err = arch_fixup_fdt(fdt_blob);
112 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
119 * Weak default function for board specific cleanup/preparation before
120 * Linux boot. Some boards/platforms might not need it, so just provide
121 * an empty stub here.
123 __weak void spl_board_prepare_for_linux(void)
128 __weak void spl_board_prepare_for_boot(void)
133 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
135 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
138 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
140 ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
142 spl_image->size = CONFIG_SYS_MONITOR_LEN;
145 * Binman error cases: address of the end of the previous region or the
146 * start of the image's entry area (usually 0) if there is no previous
149 if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
150 /* Binman does not support separated entry addresses */
151 spl_image->entry_point = u_boot_pos;
152 spl_image->load_addr = u_boot_pos;
154 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
155 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
157 spl_image->os = IH_OS_U_BOOT;
158 spl_image->name = "U-Boot";
161 #ifdef CONFIG_SPL_LOAD_FIT_FULL
162 /* Parse and load full fitImage in SPL */
163 static int spl_load_fit_image(struct spl_image_info *spl_image,
164 const struct image_header *header)
166 bootm_headers_t images;
167 const char *fit_uname_config = NULL;
168 const char *fit_uname_fdt = FIT_FDT_PROP;
170 ulong fw_data = 0, dt_data = 0, img_data = 0;
171 ulong fw_len = 0, dt_len = 0, img_len = 0;
172 int idx, conf_noffset;
175 #ifdef CONFIG_SPL_FIT_SIGNATURE
178 ret = fit_image_load(&images, (ulong)header,
179 NULL, &fit_uname_config,
180 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
181 FIT_LOAD_REQUIRED, &fw_data, &fw_len);
185 spl_image->size = fw_len;
186 spl_image->entry_point = fw_data;
187 spl_image->load_addr = fw_data;
188 spl_image->os = IH_OS_U_BOOT;
189 spl_image->name = "U-Boot";
191 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
192 spl_image->name, spl_image->load_addr, spl_image->size);
194 #ifdef CONFIG_SPL_FIT_SIGNATURE
197 fit_image_load(&images, (ulong)header,
198 &fit_uname_fdt, &fit_uname_config,
199 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
200 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
202 conf_noffset = fit_conf_get_node((const void *)header,
204 if (conf_noffset <= 0)
208 uname = fdt_stringlist_get((const void *)header, conf_noffset,
209 FIT_LOADABLE_PROP, idx,
213 #ifdef CONFIG_SPL_FIT_SIGNATURE
216 ret = fit_image_load(&images, (ulong)header,
217 &uname, &fit_uname_config,
218 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
219 FIT_LOAD_OPTIONAL_NON_ZERO,
220 &img_data, &img_len);
229 int spl_parse_image_header(struct spl_image_info *spl_image,
230 const struct image_header *header)
232 #ifdef CONFIG_SPL_LOAD_FIT_FULL
233 int ret = spl_load_fit_image(spl_image, header);
238 if (image_get_magic(header) == IH_MAGIC) {
239 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
240 u32 header_size = sizeof(struct image_header);
242 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
243 /* check uImage header CRC */
244 if (!image_check_hcrc(header)) {
245 puts("SPL: Image header CRC check failed!\n");
250 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
252 * On some system (e.g. powerpc), the load-address and
253 * entry-point is located at address 0. We can't load
254 * to 0-0x40. So skip header in this case.
256 spl_image->load_addr = image_get_load(header);
257 spl_image->entry_point = image_get_ep(header);
258 spl_image->size = image_get_data_size(header);
260 spl_image->entry_point = image_get_load(header);
261 /* Load including the header */
262 spl_image->load_addr = spl_image->entry_point -
264 spl_image->size = image_get_data_size(header) +
267 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
268 /* store uImage data length and CRC to check later */
269 spl_image->dcrc_data = image_get_load(header);
270 spl_image->dcrc_length = image_get_data_size(header);
271 spl_image->dcrc = image_get_dcrc(header);
274 spl_image->os = image_get_os(header);
275 spl_image->name = image_get_name(header);
277 "payload image: %32s load addr: 0x%lx size: %d\n",
278 spl_image->name, spl_image->load_addr, spl_image->size);
280 /* LEGACY image not supported */
281 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
285 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
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.
294 panic("** no mkimage signature but raw image not supported");
297 #ifdef CONFIG_SPL_OS_BOOT
300 if (!bootz_setup((ulong)header, &start, &end)) {
301 spl_image->name = "Linux";
302 spl_image->os = IH_OS_LINUX;
303 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
304 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
305 spl_image->size = end - start;
307 "payload zImage, load addr: 0x%lx size: %d\n",
308 spl_image->load_addr, spl_image->size);
313 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
314 /* Signature not found - assume u-boot.bin */
315 debug("mkimage signature not found - ih_magic = %x\n",
317 spl_set_header_raw_uboot(spl_image);
319 /* RAW image not supported, proceed to other boot methods. */
320 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
328 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
330 typedef void __noreturn (*image_entry_noargs_t)(void);
332 image_entry_noargs_t image_entry =
333 (image_entry_noargs_t)spl_image->entry_point;
335 debug("image entry point: 0x%lx\n", spl_image->entry_point);
339 #if CONFIG_IS_ENABLED(HANDOFF)
341 * Set up the SPL hand-off information
343 * This is initially empty (zero) but can be written by
345 static int setup_spl_handoff(void)
347 struct spl_handoff *ho;
349 ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
356 static int write_spl_handoff(void)
358 struct spl_handoff *ho;
360 ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
363 handoff_save_dram(ho);
364 #ifdef CONFIG_SANDBOX
365 ho->arch.magic = TEST_HANDOFF_MAGIC;
367 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
372 static inline int setup_spl_handoff(void) { return 0; }
373 static inline int write_spl_handoff(void) { return 0; }
377 static int spl_common_init(bool setup_malloc)
381 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
383 #ifdef CONFIG_MALLOC_F_ADDR
384 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
386 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
390 ret = bootstage_init(true);
392 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
396 bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
397 #if CONFIG_IS_ENABLED(LOG)
400 debug("%s: Failed to set up logging\n", __func__);
404 if (CONFIG_IS_ENABLED(BLOBLIST)) {
405 ret = bloblist_init();
407 debug("%s: Failed to set up bloblist: ret=%d\n",
412 if (CONFIG_IS_ENABLED(HANDOFF)) {
415 ret = setup_spl_handoff();
417 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
421 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
422 ret = fdtdec_setup();
424 debug("fdtdec_setup() returned error %d\n", ret);
428 if (CONFIG_IS_ENABLED(DM)) {
429 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
430 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
431 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
432 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
434 debug("dm_init_and_scan() returned error %d\n", ret);
442 void spl_set_bd(void)
445 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
452 int spl_early_init(void)
456 debug("%s\n", __func__);
458 ret = spl_common_init(true);
461 gd->flags |= GD_FLG_SPL_EARLY_INIT;
469 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
470 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
472 debug("%s\n", __func__);
474 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
475 ret = spl_common_init(setup_malloc);
479 gd->flags |= GD_FLG_SPL_INIT;
484 #ifndef BOOT_DEVICE_NONE
485 #define BOOT_DEVICE_NONE 0xdeadbeef
488 __weak void board_boot_order(u32 *spl_boot_list)
490 spl_boot_list[0] = spl_boot_device();
493 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
495 struct spl_image_loader *drv =
496 ll_entry_start(struct spl_image_loader, spl_image_loader);
498 ll_entry_count(struct spl_image_loader, spl_image_loader);
499 struct spl_image_loader *entry;
501 for (entry = drv; entry != drv + n_ents; entry++) {
502 if (boot_device == entry->boot_device)
510 static int spl_load_image(struct spl_image_info *spl_image,
511 struct spl_image_loader *loader)
514 struct spl_boot_device bootdev;
516 bootdev.boot_device = loader->boot_device;
517 bootdev.boot_device_name = NULL;
519 ret = loader->load_image(spl_image, &bootdev);
520 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
521 if (!ret && spl_image->dcrc_length) {
523 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
524 spl_image->dcrc_length, CHUNKSZ_CRC32);
525 if (dcrc != spl_image->dcrc) {
526 puts("SPL: Image data CRC check failed!\n");
535 * boot_from_devices() - Try loading an booting U-Boot from a list of devices
537 * @spl_image: Place to put the image details if successful
538 * @spl_boot_list: List of boot devices to try
539 * @count: Number of elements in spl_boot_list
540 * @return 0 if OK, -ve on error
542 static int boot_from_devices(struct spl_image_info *spl_image,
543 u32 spl_boot_list[], int count)
547 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
548 struct spl_image_loader *loader;
550 loader = spl_ll_find_loader(spl_boot_list[i]);
551 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
553 printf("Trying to boot from %s\n", loader->name);
555 puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
557 if (loader && !spl_load_image(spl_image, loader)) {
558 spl_image->boot_device = spl_boot_list[i];
566 void board_init_r(gd_t *dummy1, ulong dummy2)
568 u32 spl_boot_list[] = {
575 struct spl_image_info spl_image;
578 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
582 #if defined(CONFIG_SYS_SPL_MALLOC_START)
583 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
584 CONFIG_SYS_SPL_MALLOC_SIZE);
585 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
587 if (!(gd->flags & GD_FLG_SPL_INIT)) {
591 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
593 * timer_init() does not exist on PPC systems. The timer is initialized
594 * and enabled (decrementer) in interrupt_init() here.
599 #if CONFIG_IS_ENABLED(BOARD_INIT)
603 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
604 dram_init_banksize();
608 memset(&spl_image, '\0', sizeof(spl_image));
609 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
610 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
612 spl_image.boot_device = BOOT_DEVICE_NONE;
613 board_boot_order(spl_boot_list);
615 if (boot_from_devices(&spl_image, spl_boot_list,
616 ARRAY_SIZE(spl_boot_list))) {
617 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
621 spl_perform_fixups(&spl_image);
622 if (CONFIG_IS_ENABLED(HANDOFF)) {
623 ret = write_spl_handoff();
625 printf(SPL_TPL_PROMPT
626 "SPL hand-off write failed (err=%d)\n", ret);
628 if (CONFIG_IS_ENABLED(BLOBLIST)) {
629 ret = bloblist_finish();
631 printf("Warning: Failed to finish bloblist (ret=%d)\n",
635 #ifdef CONFIG_CPU_V7M
636 spl_image.entry_point |= 0x1;
638 switch (spl_image.os) {
640 debug("Jumping to U-Boot\n");
642 #if CONFIG_IS_ENABLED(ATF)
643 case IH_OS_ARM_TRUSTED_FIRMWARE:
644 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
645 spl_invoke_atf(&spl_image);
648 #if CONFIG_IS_ENABLED(OPTEE)
650 debug("Jumping to U-Boot via OP-TEE\n");
651 spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
652 (void *)spl_image.entry_point);
655 #ifdef CONFIG_SPL_OS_BOOT
657 debug("Jumping to Linux\n");
659 spl_board_prepare_for_linux();
660 jump_to_image_linux(&spl_image);
663 debug("Unsupported OS image.. Jumping nevertheless..\n");
665 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
666 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
667 gd->malloc_ptr / 1024);
669 #ifdef CONFIG_BOOTSTAGE_STASH
670 bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
671 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
672 CONFIG_BOOTSTAGE_STASH_SIZE);
674 debug("Failed to stash bootstage: err=%d\n", ret);
677 debug("loaded - jumping to U-Boot...\n");
678 spl_board_prepare_for_boot();
679 jump_to_image_no_args(&spl_image);
682 #ifdef CONFIG_SPL_SERIAL_SUPPORT
684 * This requires UART clocks to be enabled. In order for this to work the
685 * caller must ensure that the gd pointer is valid.
687 void preloader_console_init(void)
689 gd->baudrate = CONFIG_BAUDRATE;
691 serial_init(); /* serial communications setup */
693 gd->have_console = 1;
695 #if CONFIG_IS_ENABLED(BANNER_PRINT)
696 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
697 U_BOOT_TIME " " U_BOOT_TZ ")\n");
699 #ifdef CONFIG_SPL_DISPLAY_PRINT
706 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
708 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
709 * for the main board_init_r() execution. This is typically because we need
710 * more stack space for things like the MMC sub-system.
712 * This function calculates the stack position, copies the global_data into
713 * place, sets the new gd (except for ARM, for which setting GD within a C
714 * function may not always work) and returns the new stack position. The
715 * caller is responsible for setting up the sp register and, in the case
716 * of ARM, setting up gd.
718 * All of this is done using the same layout and alignments as done in
719 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
721 * @return new stack location, or 0 to use the same stack
723 ulong spl_relocate_stack_gd(void)
725 #ifdef CONFIG_SPL_STACK_R
727 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
729 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
730 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
731 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
732 gd->malloc_base = ptr;
733 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
737 /* Get stack position: use 8-byte alignment for ABI compliance */
738 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
739 new_gd = (gd_t *)ptr;
740 memcpy(new_gd, (void *)gd, sizeof(gd_t));
741 #if CONFIG_IS_ENABLED(DM)
742 dm_fixup_for_gd_move(new_gd);
744 #if !defined(CONFIG_ARM)