1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
6 * Aneesh V <aneesh@ti.com>
11 #include <binman_sym.h>
12 #include <bootstage.h>
22 #include <asm/global_data.h>
23 #include <asm-generic/gpio.h>
24 #include <asm/u-boot.h>
27 #include <u-boot/crc.h>
28 #if CONFIG_IS_ENABLED(BANNER_PRINT)
29 #include <timestamp.h>
36 #include <linux/compiler.h>
37 #include <fdt_support.h>
38 #include <bootcount.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 #ifndef CONFIG_SYS_UBOOT_START
44 #define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
46 #ifndef CONFIG_SYS_MONITOR_LEN
47 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
48 #define CONFIG_SYS_MONITOR_LEN (200 * 1024)
51 u32 *boot_params_ptr = NULL;
53 #if CONFIG_IS_ENABLED(BINMAN_SYMBOLS)
54 /* See spl.h for information about this */
55 binman_sym_declare(ulong, u_boot_any, image_pos);
56 binman_sym_declare(ulong, u_boot_any, size);
60 binman_sym_declare(ulong, u_boot_spl, image_pos);
61 binman_sym_declare(ulong, u_boot_spl, size);
64 /* Define board data structure */
65 static struct bd_info bdata __attribute__ ((section(".data")));
67 #if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
69 * Board-specific Platform code can reimplement show_boot_progress () if needed
71 __weak void show_boot_progress(int val) {}
74 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
75 defined(CONFIG_SPL_ATF)
76 /* weak, default platform-specific function to initialize dram banks */
77 __weak int dram_init_banksize(void)
84 * Default function to determine if u-boot or the OS should
85 * be started. This implementation always returns 1.
87 * Please implement your own board specific funcion to do this.
90 * 0 to not start u-boot
91 * positive if u-boot should start
93 #if CONFIG_IS_ENABLED(OS_BOOT)
94 __weak int spl_start_uboot(void)
97 "Please implement spl_start_uboot() for your board\n");
98 puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
103 * Weak default function for arch specific zImage check. Return zero
104 * and fill start and end address if image is recognized.
106 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
112 /* Weak default function for arch/board-specific fixups to the spl_image_info */
113 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
117 void spl_fixup_fdt(void *fdt_blob)
119 #if defined(CONFIG_SPL_OF_LIBFDT)
125 err = fdt_check_header(fdt_blob);
127 printf("fdt_root: %s\n", fdt_strerror(err));
131 /* fixup the memory dt node */
132 err = fdt_shrink_to_minimum(fdt_blob, 0);
134 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
138 err = arch_fixup_fdt(fdt_blob);
140 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
146 #if CONFIG_IS_ENABLED(BINMAN_SYMBOLS)
147 ulong spl_get_image_pos(void)
149 return spl_phase() == PHASE_TPL ?
150 binman_sym(ulong, u_boot_spl, image_pos) :
151 binman_sym(ulong, u_boot_any, image_pos);
154 ulong spl_get_image_size(void)
156 return spl_phase() == PHASE_TPL ?
157 binman_sym(ulong, u_boot_spl, size) :
158 binman_sym(ulong, u_boot_any, size);
160 #endif /* BINMAN_SYMBOLS */
162 ulong spl_get_image_text_base(void)
164 return spl_phase() == PHASE_TPL ? CONFIG_SPL_TEXT_BASE :
165 CONFIG_SYS_TEXT_BASE;
169 * Weak default function for board specific cleanup/preparation before
170 * Linux boot. Some boards/platforms might not need it, so just provide
171 * an empty stub here.
173 __weak void spl_board_prepare_for_linux(void)
178 __weak void spl_board_prepare_for_optee(void *fdt)
182 __weak const char *spl_board_loader_name(u32 boot_device)
187 #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
188 __weak void __noreturn jump_to_image_optee(struct spl_image_info *spl_image)
190 spl_optee_entry(NULL, NULL, spl_image->fdt_addr,
191 (void *)spl_image->entry_point);
195 __weak void spl_board_prepare_for_boot(void)
200 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
202 return map_sysmem(CONFIG_SYS_TEXT_BASE + offset, 0);
205 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
207 ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
209 spl_image->size = CONFIG_SYS_MONITOR_LEN;
212 * Binman error cases: address of the end of the previous region or the
213 * start of the image's entry area (usually 0) if there is no previous
216 if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
217 /* Binman does not support separated entry addresses */
218 spl_image->entry_point = u_boot_pos;
219 spl_image->load_addr = u_boot_pos;
221 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
222 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
224 spl_image->os = IH_OS_U_BOOT;
225 spl_image->name = "U-Boot";
228 #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
229 /* Parse and load full fitImage in SPL */
230 static int spl_load_fit_image(struct spl_image_info *spl_image,
231 const struct image_header *header)
233 bootm_headers_t images;
234 const char *fit_uname_config = NULL;
237 ulong fw_data = 0, dt_data = 0, img_data = 0;
238 ulong fw_len = 0, dt_len = 0, img_len = 0;
239 int idx, conf_noffset;
242 #ifdef CONFIG_SPL_FIT_SIGNATURE
245 ret = fit_image_load(&images, (ulong)header,
246 NULL, &fit_uname_config,
247 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
248 FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
250 printf("DEPRECATED: 'standalone = ' property.");
251 printf("Please use either 'firmware =' or 'kernel ='\n");
253 ret = fit_image_load(&images, (ulong)header, NULL,
254 &fit_uname_config, IH_ARCH_DEFAULT,
255 IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
260 ret = fit_image_load(&images, (ulong)header, NULL,
261 &fit_uname_config, IH_ARCH_DEFAULT,
262 IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
269 spl_image->size = fw_len;
270 spl_image->entry_point = fw_data;
271 spl_image->load_addr = fw_data;
272 if (fit_image_get_os(header, ret, &spl_image->os))
273 spl_image->os = IH_OS_INVALID;
274 spl_image->name = genimg_get_os_name(spl_image->os);
276 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
277 spl_image->name, spl_image->load_addr, spl_image->size);
279 #ifdef CONFIG_SPL_FIT_SIGNATURE
282 ret = fit_image_load(&images, (ulong)header, NULL, &fit_uname_config,
283 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
284 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
286 spl_image->fdt_addr = (void *)dt_data;
288 if (spl_image->os == IH_OS_U_BOOT) {
289 /* HACK: U-boot expects FDT at a specific address */
290 fdt_hack = spl_image->load_addr + spl_image->size;
291 fdt_hack = (fdt_hack + 3) & ~3;
292 debug("Relocating FDT to %p\n", spl_image->fdt_addr);
293 memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
297 conf_noffset = fit_conf_get_node((const void *)header,
299 if (conf_noffset <= 0)
303 uname = fdt_stringlist_get((const void *)header, conf_noffset,
304 FIT_LOADABLE_PROP, idx,
308 #ifdef CONFIG_SPL_FIT_SIGNATURE
311 ret = fit_image_load(&images, (ulong)header,
312 &uname, &fit_uname_config,
313 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
314 FIT_LOAD_OPTIONAL_NON_ZERO,
315 &img_data, &img_len);
324 __weak int spl_parse_board_header(struct spl_image_info *spl_image,
325 const struct spl_boot_device *bootdev,
326 const void *image_header, size_t size)
331 __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
332 const struct image_header *header)
334 /* LEGACY image not supported */
335 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
339 int spl_parse_image_header(struct spl_image_info *spl_image,
340 const struct spl_boot_device *bootdev,
341 const struct image_header *header)
343 #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
344 int ret = spl_load_fit_image(spl_image, header);
349 if (image_get_magic(header) == IH_MAGIC) {
352 ret = spl_parse_legacy_header(spl_image, header);
356 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
358 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
359 * code which loads images in SPL cannot guarantee that
360 * absolutely all read errors will be reported.
361 * An example is the LPC32XX MLC NAND driver, which
362 * will consider that a completely unreadable NAND block
363 * is bad, and thus should be skipped silently.
365 panic("** no mkimage signature but raw image not supported");
368 #if CONFIG_IS_ENABLED(OS_BOOT)
371 if (!bootz_setup((ulong)header, &start, &end)) {
372 spl_image->name = "Linux";
373 spl_image->os = IH_OS_LINUX;
374 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
375 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
376 spl_image->size = end - start;
378 "payload zImage, load addr: 0x%lx size: %d\n",
379 spl_image->load_addr, spl_image->size);
384 if (!spl_parse_board_header(spl_image, bootdev, (const void *)header, sizeof(*header)))
387 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
388 /* Signature not found - assume u-boot.bin */
389 debug("mkimage signature not found - ih_magic = %x\n",
391 spl_set_header_raw_uboot(spl_image);
393 /* RAW image not supported, proceed to other boot methods. */
394 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
402 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
404 typedef void __noreturn (*image_entry_noargs_t)(void);
406 image_entry_noargs_t image_entry =
407 (image_entry_noargs_t)spl_image->entry_point;
409 debug("image entry point: 0x%lx\n", spl_image->entry_point);
413 #if CONFIG_IS_ENABLED(HANDOFF)
415 * Set up the SPL hand-off information
417 * This is initially empty (zero) but can be written by
419 static int setup_spl_handoff(void)
421 struct spl_handoff *ho;
423 ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
430 __weak int handoff_arch_save(struct spl_handoff *ho)
435 static int write_spl_handoff(void)
437 struct spl_handoff *ho;
440 ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
443 handoff_save_dram(ho);
444 ret = handoff_arch_save(ho);
447 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
452 static inline int setup_spl_handoff(void) { return 0; }
453 static inline int write_spl_handoff(void) { return 0; }
458 * get_bootstage_id() - Get the bootstage ID to emit
460 * @start: true if this is for starting SPL, false for ending it
461 * Return: bootstage ID to use
463 static enum bootstage_id get_bootstage_id(bool start)
465 enum u_boot_phase phase = spl_phase();
467 if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
468 return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
470 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
473 static int spl_common_init(bool setup_malloc)
477 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
479 #ifdef CONFIG_MALLOC_F_ADDR
480 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
482 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
486 ret = bootstage_init(u_boot_first_phase());
488 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
492 #ifdef CONFIG_BOOTSTAGE_STASH
493 if (!u_boot_first_phase()) {
494 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
495 CONFIG_BOOTSTAGE_STASH_SIZE);
497 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
499 debug("%s: Failed to unstash bootstage: ret=%d\n",
502 #endif /* CONFIG_BOOTSTAGE_STASH */
503 bootstage_mark_name(get_bootstage_id(true),
504 spl_phase_name(spl_phase()));
505 #if CONFIG_IS_ENABLED(LOG)
508 debug("%s: Failed to set up logging\n", __func__);
512 if (CONFIG_IS_ENABLED(OF_REAL)) {
513 ret = fdtdec_setup();
515 debug("fdtdec_setup() returned error %d\n", ret);
519 if (CONFIG_IS_ENABLED(DM)) {
520 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
521 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
522 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
523 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
524 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
526 debug("dm_init_and_scan() returned error %d\n", ret);
534 void spl_set_bd(void)
537 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
544 int spl_early_init(void)
548 debug("%s\n", __func__);
550 ret = spl_common_init(true);
553 gd->flags |= GD_FLG_SPL_EARLY_INIT;
561 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
562 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
564 debug("%s\n", __func__);
566 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
567 ret = spl_common_init(setup_malloc);
571 gd->flags |= GD_FLG_SPL_INIT;
576 #ifndef BOOT_DEVICE_NONE
577 #define BOOT_DEVICE_NONE 0xdeadbeef
580 __weak void board_boot_order(u32 *spl_boot_list)
582 spl_boot_list[0] = spl_boot_device();
585 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
587 struct spl_image_loader *drv =
588 ll_entry_start(struct spl_image_loader, spl_image_loader);
590 ll_entry_count(struct spl_image_loader, spl_image_loader);
591 struct spl_image_loader *entry;
593 for (entry = drv; entry != drv + n_ents; entry++) {
594 if (boot_device == entry->boot_device)
602 __weak int spl_check_board_image(struct spl_image_info *spl_image,
603 const struct spl_boot_device *bootdev)
608 static int spl_load_image(struct spl_image_info *spl_image,
609 struct spl_image_loader *loader)
612 struct spl_boot_device bootdev;
614 bootdev.boot_device = loader->boot_device;
615 bootdev.boot_device_name = NULL;
617 ret = loader->load_image(spl_image, &bootdev);
618 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
619 if (!ret && spl_image->dcrc_length) {
621 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
622 spl_image->dcrc_length, CHUNKSZ_CRC32);
623 if (dcrc != spl_image->dcrc) {
624 puts("SPL: Image data CRC check failed!\n");
630 ret = spl_check_board_image(spl_image, &bootdev);
636 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
638 * @spl_image: Place to put the image details if successful
639 * @spl_boot_list: List of boot devices to try
640 * @count: Number of elements in spl_boot_list
641 * Return: 0 if OK, -ENODEV if there were no boot devices
642 * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
643 * devices but none worked
645 static int boot_from_devices(struct spl_image_info *spl_image,
646 u32 spl_boot_list[], int count)
651 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
652 struct spl_image_loader *loader;
653 int bootdev = spl_boot_list[i];
655 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
657 loader = spl_ll_find_loader(bootdev);
658 if (CONFIG_IS_ENABLED(SERIAL) &&
659 CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT) &&
660 !IS_ENABLED(CONFIG_SILENT_CONSOLE)) {
662 printf("Trying to boot from %s\n",
663 spl_loader_name(loader));
664 else if (CONFIG_IS_ENABLED(SHOW_ERRORS))
665 printf(SPL_TPL_PROMPT
666 "Unsupported Boot Device %d\n", bootdev);
668 puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
670 if (loader && !spl_load_image(spl_image, loader)) {
671 spl_image->boot_device = bootdev;
679 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
680 void board_init_f(ulong dummy)
682 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
685 ret = spl_early_init();
687 debug("spl_early_init() failed: %d\n", ret);
692 preloader_console_init();
696 void board_init_r(gd_t *dummy1, ulong dummy2)
698 u32 spl_boot_list[] = {
705 struct spl_image_info spl_image;
708 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
712 #if defined(CONFIG_SYS_SPL_MALLOC_START)
713 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
714 CONFIG_SYS_SPL_MALLOC_SIZE);
715 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
717 if (!(gd->flags & GD_FLG_SPL_INIT)) {
721 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
723 * timer_init() does not exist on PPC systems. The timer is initialized
724 * and enabled (decrementer) in interrupt_init() here.
728 if (CONFIG_IS_ENABLED(BLOBLIST)) {
729 ret = bloblist_init();
731 debug("%s: Failed to set up bloblist: ret=%d\n",
733 puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
737 if (CONFIG_IS_ENABLED(HANDOFF)) {
740 ret = setup_spl_handoff();
742 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
747 if (CONFIG_IS_ENABLED(GPIO_HOG))
748 gpio_hog_probe_all();
750 #if CONFIG_IS_ENABLED(BOARD_INIT)
754 #if defined(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT)
758 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
759 IS_ENABLED(CONFIG_SPL_ATF))
760 dram_init_banksize();
764 memset(&spl_image, '\0', sizeof(spl_image));
765 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
766 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
768 spl_image.boot_device = BOOT_DEVICE_NONE;
769 board_boot_order(spl_boot_list);
771 ret = boot_from_devices(&spl_image, spl_boot_list,
772 ARRAY_SIZE(spl_boot_list));
774 if (CONFIG_IS_ENABLED(SHOW_ERRORS) &&
775 CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT))
776 printf(SPL_TPL_PROMPT "failed to boot from all boot devices (err=%d)\n",
779 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
783 spl_perform_fixups(&spl_image);
784 if (CONFIG_IS_ENABLED(HANDOFF)) {
785 ret = write_spl_handoff();
787 printf(SPL_TPL_PROMPT
788 "SPL hand-off write failed (err=%d)\n", ret);
790 if (CONFIG_IS_ENABLED(BLOBLIST)) {
791 ret = bloblist_finish();
793 printf("Warning: Failed to finish bloblist (ret=%d)\n",
797 switch (spl_image.os) {
799 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
801 #if CONFIG_IS_ENABLED(ATF)
802 case IH_OS_ARM_TRUSTED_FIRMWARE:
803 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
804 spl_fixup_fdt(spl_image.fdt_addr);
805 spl_invoke_atf(&spl_image);
808 #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
810 debug("Jumping to U-Boot via OP-TEE\n");
811 spl_board_prepare_for_optee(spl_image.fdt_addr);
812 jump_to_image_optee(&spl_image);
815 #if CONFIG_IS_ENABLED(OPENSBI)
817 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
818 spl_invoke_opensbi(&spl_image);
821 #if CONFIG_IS_ENABLED(OS_BOOT)
823 debug("Jumping to Linux\n");
824 #if defined(CONFIG_SYS_SPL_ARGS_ADDR)
825 spl_fixup_fdt((void *)CONFIG_SYS_SPL_ARGS_ADDR);
827 spl_board_prepare_for_linux();
828 jump_to_image_linux(&spl_image);
831 debug("Unsupported OS image.. Jumping nevertheless..\n");
833 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
834 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
835 gd->malloc_ptr / 1024);
837 bootstage_mark_name(get_bootstage_id(false), "end phase");
838 #ifdef CONFIG_BOOTSTAGE_STASH
839 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
840 CONFIG_BOOTSTAGE_STASH_SIZE);
842 debug("Failed to stash bootstage: err=%d\n", ret);
845 spl_board_prepare_for_boot();
846 jump_to_image_no_args(&spl_image);
850 * This requires UART clocks to be enabled. In order for this to work the
851 * caller must ensure that the gd pointer is valid.
853 void preloader_console_init(void)
855 #ifdef CONFIG_SPL_SERIAL
856 gd->baudrate = CONFIG_BAUDRATE;
858 serial_init(); /* serial communications setup */
860 gd->have_console = 1;
862 #if CONFIG_IS_ENABLED(BANNER_PRINT)
863 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
864 U_BOOT_TIME " " U_BOOT_TZ ")\n");
866 #ifdef CONFIG_SPL_DISPLAY_PRINT
873 * This function is called before the stack is changed from initial stack to
874 * relocated stack. It tries to dump the stack size used
876 __weak void spl_relocate_stack_check(void)
878 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
879 ulong init_sp = gd->start_addr_sp;
880 ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
881 u8 *ptr = (u8 *)stack_bottom;
884 for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
885 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
889 printf("SPL initial stack usage: %lu bytes\n",
890 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
895 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
897 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
898 * for the main board_init_r() execution. This is typically because we need
899 * more stack space for things like the MMC sub-system.
901 * This function calculates the stack position, copies the global_data into
902 * place, sets the new gd (except for ARM, for which setting GD within a C
903 * function may not always work) and returns the new stack position. The
904 * caller is responsible for setting up the sp register and, in the case
905 * of ARM, setting up gd.
907 * All of this is done using the same layout and alignments as done in
908 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
910 * Return: new stack location, or 0 to use the same stack
912 ulong spl_relocate_stack_gd(void)
914 #ifdef CONFIG_SPL_STACK_R
916 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
918 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
919 spl_relocate_stack_check();
921 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
922 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
923 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
924 gd->malloc_ptr, gd->malloc_ptr / 1024);
925 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
926 gd->malloc_base = ptr;
927 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
931 /* Get stack position: use 8-byte alignment for ABI compliance */
932 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
933 new_gd = (gd_t *)ptr;
934 memcpy(new_gd, (void *)gd, sizeof(gd_t));
935 #if CONFIG_IS_ENABLED(DM)
936 dm_fixup_for_gd_move(new_gd);
938 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
947 #if defined(CONFIG_BOOTCOUNT_LIMIT) && \
948 ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
949 (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
950 void bootcount_store(ulong a)
954 ulong bootcount_load(void)