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 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
244 * On some system (e.g. powerpc), the load-address and
245 * entry-point is located at address 0. We can't load
246 * to 0-0x40. So skip header in this case.
248 spl_image->load_addr = image_get_load(header);
249 spl_image->entry_point = image_get_ep(header);
250 spl_image->size = image_get_data_size(header);
252 spl_image->entry_point = image_get_load(header);
253 /* Load including the header */
254 spl_image->load_addr = spl_image->entry_point -
256 spl_image->size = image_get_data_size(header) +
259 spl_image->os = image_get_os(header);
260 spl_image->name = image_get_name(header);
262 "payload image: %32s load addr: 0x%lx size: %d\n",
263 spl_image->name, spl_image->load_addr, spl_image->size);
265 /* LEGACY image not supported */
266 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
270 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
272 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
273 * code which loads images in SPL cannot guarantee that
274 * absolutely all read errors will be reported.
275 * An example is the LPC32XX MLC NAND driver, which
276 * will consider that a completely unreadable NAND block
277 * is bad, and thus should be skipped silently.
279 panic("** no mkimage signature but raw image not supported");
282 #ifdef CONFIG_SPL_OS_BOOT
285 if (!bootz_setup((ulong)header, &start, &end)) {
286 spl_image->name = "Linux";
287 spl_image->os = IH_OS_LINUX;
288 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
289 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
290 spl_image->size = end - start;
292 "payload zImage, load addr: 0x%lx size: %d\n",
293 spl_image->load_addr, spl_image->size);
298 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
299 /* Signature not found - assume u-boot.bin */
300 debug("mkimage signature not found - ih_magic = %x\n",
302 spl_set_header_raw_uboot(spl_image);
304 /* RAW image not supported, proceed to other boot methods. */
305 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
313 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
315 typedef void __noreturn (*image_entry_noargs_t)(void);
317 image_entry_noargs_t image_entry =
318 (image_entry_noargs_t)spl_image->entry_point;
320 debug("image entry point: 0x%lx\n", spl_image->entry_point);
324 #if CONFIG_IS_ENABLED(HANDOFF)
326 * Set up the SPL hand-off information
328 * This is initially empty (zero) but can be written by
330 static int setup_spl_handoff(void)
332 struct spl_handoff *ho;
334 ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
341 static int write_spl_handoff(void)
343 struct spl_handoff *ho;
345 ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
348 handoff_save_dram(ho);
349 #ifdef CONFIG_SANDBOX
350 ho->arch.magic = TEST_HANDOFF_MAGIC;
352 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
357 static inline int setup_spl_handoff(void) { return 0; }
358 static inline int write_spl_handoff(void) { return 0; }
362 static int spl_common_init(bool setup_malloc)
366 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
368 #ifdef CONFIG_MALLOC_F_ADDR
369 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
371 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
375 ret = bootstage_init(true);
377 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
381 bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
382 #if CONFIG_IS_ENABLED(LOG)
385 debug("%s: Failed to set up logging\n", __func__);
389 if (CONFIG_IS_ENABLED(BLOBLIST)) {
390 ret = bloblist_init();
392 debug("%s: Failed to set up bloblist: ret=%d\n",
397 if (CONFIG_IS_ENABLED(HANDOFF)) {
400 ret = setup_spl_handoff();
402 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
406 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
407 ret = fdtdec_setup();
409 debug("fdtdec_setup() returned error %d\n", ret);
413 if (CONFIG_IS_ENABLED(DM)) {
414 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
415 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
416 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
417 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
419 debug("dm_init_and_scan() returned error %d\n", ret);
427 void spl_set_bd(void)
430 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
437 int spl_early_init(void)
441 debug("%s\n", __func__);
443 ret = spl_common_init(true);
446 gd->flags |= GD_FLG_SPL_EARLY_INIT;
454 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
455 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
457 debug("%s\n", __func__);
459 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
460 ret = spl_common_init(setup_malloc);
464 gd->flags |= GD_FLG_SPL_INIT;
469 #ifndef BOOT_DEVICE_NONE
470 #define BOOT_DEVICE_NONE 0xdeadbeef
473 __weak void board_boot_order(u32 *spl_boot_list)
475 spl_boot_list[0] = spl_boot_device();
478 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
480 struct spl_image_loader *drv =
481 ll_entry_start(struct spl_image_loader, spl_image_loader);
483 ll_entry_count(struct spl_image_loader, spl_image_loader);
484 struct spl_image_loader *entry;
486 for (entry = drv; entry != drv + n_ents; entry++) {
487 if (boot_device == entry->boot_device)
495 static int spl_load_image(struct spl_image_info *spl_image,
496 struct spl_image_loader *loader)
498 struct spl_boot_device bootdev;
500 bootdev.boot_device = loader->boot_device;
501 bootdev.boot_device_name = NULL;
503 return loader->load_image(spl_image, &bootdev);
507 * boot_from_devices() - Try loading an booting U-Boot from a list of devices
509 * @spl_image: Place to put the image details if successful
510 * @spl_boot_list: List of boot devices to try
511 * @count: Number of elements in spl_boot_list
512 * @return 0 if OK, -ve on error
514 static int boot_from_devices(struct spl_image_info *spl_image,
515 u32 spl_boot_list[], int count)
519 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
520 struct spl_image_loader *loader;
522 loader = spl_ll_find_loader(spl_boot_list[i]);
523 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
525 printf("Trying to boot from %s\n", loader->name);
527 puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
529 if (loader && !spl_load_image(spl_image, loader)) {
530 spl_image->boot_device = spl_boot_list[i];
538 void board_init_r(gd_t *dummy1, ulong dummy2)
540 u32 spl_boot_list[] = {
547 struct spl_image_info spl_image;
550 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
554 #if defined(CONFIG_SYS_SPL_MALLOC_START)
555 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
556 CONFIG_SYS_SPL_MALLOC_SIZE);
557 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
559 if (!(gd->flags & GD_FLG_SPL_INIT)) {
563 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
565 * timer_init() does not exist on PPC systems. The timer is initialized
566 * and enabled (decrementer) in interrupt_init() here.
571 #if CONFIG_IS_ENABLED(BOARD_INIT)
575 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
576 dram_init_banksize();
580 memset(&spl_image, '\0', sizeof(spl_image));
581 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
582 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
584 spl_image.boot_device = BOOT_DEVICE_NONE;
585 board_boot_order(spl_boot_list);
587 if (boot_from_devices(&spl_image, spl_boot_list,
588 ARRAY_SIZE(spl_boot_list))) {
589 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
593 spl_perform_fixups(&spl_image);
594 if (CONFIG_IS_ENABLED(HANDOFF)) {
595 ret = write_spl_handoff();
597 printf(SPL_TPL_PROMPT
598 "SPL hand-off write failed (err=%d)\n", ret);
600 if (CONFIG_IS_ENABLED(BLOBLIST)) {
601 ret = bloblist_finish();
603 printf("Warning: Failed to finish bloblist (ret=%d)\n",
607 #ifdef CONFIG_CPU_V7M
608 spl_image.entry_point |= 0x1;
610 switch (spl_image.os) {
612 debug("Jumping to U-Boot\n");
614 #if CONFIG_IS_ENABLED(ATF)
615 case IH_OS_ARM_TRUSTED_FIRMWARE:
616 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
617 spl_invoke_atf(&spl_image);
620 #if CONFIG_IS_ENABLED(OPTEE)
622 debug("Jumping to U-Boot via OP-TEE\n");
623 spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
624 (void *)spl_image.entry_point);
627 #ifdef CONFIG_SPL_OS_BOOT
629 debug("Jumping to Linux\n");
631 spl_board_prepare_for_linux();
632 jump_to_image_linux(&spl_image);
635 debug("Unsupported OS image.. Jumping nevertheless..\n");
637 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
638 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
639 gd->malloc_ptr / 1024);
641 #ifdef CONFIG_BOOTSTAGE_STASH
642 bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
643 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
644 CONFIG_BOOTSTAGE_STASH_SIZE);
646 debug("Failed to stash bootstage: err=%d\n", ret);
649 debug("loaded - jumping to U-Boot...\n");
650 spl_board_prepare_for_boot();
651 jump_to_image_no_args(&spl_image);
654 #ifdef CONFIG_SPL_SERIAL_SUPPORT
656 * This requires UART clocks to be enabled. In order for this to work the
657 * caller must ensure that the gd pointer is valid.
659 void preloader_console_init(void)
661 gd->baudrate = CONFIG_BAUDRATE;
663 serial_init(); /* serial communications setup */
665 gd->have_console = 1;
667 #if CONFIG_IS_ENABLED(BANNER_PRINT)
668 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
669 U_BOOT_TIME " " U_BOOT_TZ ")\n");
671 #ifdef CONFIG_SPL_DISPLAY_PRINT
678 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
680 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
681 * for the main board_init_r() execution. This is typically because we need
682 * more stack space for things like the MMC sub-system.
684 * This function calculates the stack position, copies the global_data into
685 * place, sets the new gd (except for ARM, for which setting GD within a C
686 * function may not always work) and returns the new stack position. The
687 * caller is responsible for setting up the sp register and, in the case
688 * of ARM, setting up gd.
690 * All of this is done using the same layout and alignments as done in
691 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
693 * @return new stack location, or 0 to use the same stack
695 ulong spl_relocate_stack_gd(void)
697 #ifdef CONFIG_SPL_STACK_R
699 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
701 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
702 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
703 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
704 gd->malloc_base = ptr;
705 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
709 /* Get stack position: use 8-byte alignment for ABI compliance */
710 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
711 new_gd = (gd_t *)ptr;
712 memcpy(new_gd, (void *)gd, sizeof(gd_t));
713 #if CONFIG_IS_ENABLED(DM)
714 dm_fixup_for_gd_move(new_gd);
716 #if !defined(CONFIG_ARM)