1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
11 #include <binman_sym.h>
12 #include <bootstage.h>
23 #include <system-constants.h>
24 #include <asm/global_data.h>
25 #include <asm-generic/gpio.h>
28 #include <u-boot/crc.h>
29 #if CONFIG_IS_ENABLED(BANNER_PRINT)
30 #include <timestamp.h>
38 #include <dm/device-internal.h>
39 #include <dm/uclass-internal.h>
40 #include <linux/compiler.h>
41 #include <fdt_support.h>
42 #include <bootcount.h>
46 DECLARE_GLOBAL_DATA_PTR;
47 DECLARE_BINMAN_MAGIC_SYM;
49 u32 *boot_params_ptr = NULL;
51 #if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
52 /* See spl.h for information about this */
53 binman_sym_declare(ulong, u_boot_any, image_pos);
54 binman_sym_declare(ulong, u_boot_any, size);
57 binman_sym_declare(ulong, u_boot_spl_any, image_pos);
58 binman_sym_declare(ulong, u_boot_spl_any, size);
62 binman_sym_declare(ulong, u_boot_vpl_any, image_pos);
63 binman_sym_declare(ulong, u_boot_vpl_any, size);
66 #endif /* BINMAN_UBOOT_SYMBOLS */
68 /* Define board data structure */
69 static struct bd_info bdata __attribute__ ((section(".data")));
71 #if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
73 * Board-specific Platform code can reimplement show_boot_progress () if needed
75 __weak void show_boot_progress(int val) {}
78 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
79 defined(CONFIG_SPL_ATF)
80 /* weak, default platform-specific function to initialize dram banks */
81 __weak int dram_init_banksize(void)
88 * Default function to determine if u-boot or the OS should
89 * be started. This implementation always returns 1.
91 * Please implement your own board specific funcion to do this.
94 * 0 to not start u-boot
95 * positive if u-boot should start
97 #if CONFIG_IS_ENABLED(OS_BOOT)
98 __weak int spl_start_uboot(void)
101 "Please implement spl_start_uboot() for your board\n");
102 puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
107 * Weak default function for arch specific zImage check. Return zero
108 * and fill start and end address if image is recognized.
110 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
115 int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
121 /* Weak default function for arch/board-specific fixups to the spl_image_info */
122 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
126 void spl_fixup_fdt(void *fdt_blob)
128 #if defined(CONFIG_SPL_OF_LIBFDT)
134 err = fdt_check_header(fdt_blob);
136 printf("fdt_root: %s\n", fdt_strerror(err));
140 /* fixup the memory dt node */
141 err = fdt_shrink_to_minimum(fdt_blob, 0);
143 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
147 err = arch_fixup_fdt(fdt_blob);
149 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
155 int spl_reserve_video_from_ram_top(void)
157 if (CONFIG_IS_ENABLED(VIDEO)) {
162 ret = video_reserve(&addr);
165 debug("Reserving %luk for video at: %08lx\n",
166 ((unsigned long)gd->relocaddr - addr) >> 10, addr);
167 gd->relocaddr = addr;
173 ulong spl_get_image_pos(void)
175 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
176 return BINMAN_SYM_MISSING;
179 if (spl_next_phase() == PHASE_VPL)
180 return binman_sym(ulong, u_boot_vpl_any, image_pos);
182 return spl_next_phase() == PHASE_SPL ?
183 binman_sym(ulong, u_boot_spl_any, image_pos) :
184 binman_sym(ulong, u_boot_any, image_pos);
187 ulong spl_get_image_size(void)
189 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
190 return BINMAN_SYM_MISSING;
193 if (spl_next_phase() == PHASE_VPL)
194 return binman_sym(ulong, u_boot_vpl_any, size);
196 return spl_next_phase() == PHASE_SPL ?
197 binman_sym(ulong, u_boot_spl_any, size) :
198 binman_sym(ulong, u_boot_any, size);
201 ulong spl_get_image_text_base(void)
204 if (spl_next_phase() == PHASE_VPL)
205 return CONFIG_VPL_TEXT_BASE;
207 return spl_next_phase() == PHASE_SPL ? CONFIG_SPL_TEXT_BASE :
212 * Weak default function for board specific cleanup/preparation before
213 * Linux boot. Some boards/platforms might not need it, so just provide
214 * an empty stub here.
216 __weak void spl_board_prepare_for_linux(void)
221 __weak void spl_board_prepare_for_optee(void *fdt)
225 __weak const char *spl_board_loader_name(u32 boot_device)
230 #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
231 __weak void __noreturn jump_to_image_optee(struct spl_image_info *spl_image)
233 spl_optee_entry(NULL, NULL, spl_image->fdt_addr,
234 (void *)spl_image->entry_point);
238 __weak void spl_board_prepare_for_boot(void)
243 __weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
245 return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
248 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
250 ulong u_boot_pos = spl_get_image_pos();
252 #if CONFIG_SYS_MONITOR_LEN != 0
253 spl_image->size = CONFIG_SYS_MONITOR_LEN;
255 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
256 spl_image->size = 200 * 1024;
260 * Binman error cases: address of the end of the previous region or the
261 * start of the image's entry area (usually 0) if there is no previous
264 if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
265 /* Binman does not support separated entry addresses */
266 spl_image->entry_point = u_boot_pos;
267 spl_image->load_addr = u_boot_pos;
269 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
270 spl_image->load_addr = CONFIG_TEXT_BASE;
272 spl_image->os = IH_OS_U_BOOT;
273 spl_image->name = "U-Boot";
276 __weak int spl_parse_board_header(struct spl_image_info *spl_image,
277 const struct spl_boot_device *bootdev,
278 const void *image_header, size_t size)
283 __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
284 const struct legacy_img_hdr *header)
286 /* LEGACY image not supported */
287 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
291 int spl_parse_image_header(struct spl_image_info *spl_image,
292 const struct spl_boot_device *bootdev,
293 const struct legacy_img_hdr *header)
297 if (CONFIG_IS_ENABLED(LOAD_FIT_FULL)) {
298 ret = spl_load_fit_image(spl_image, header);
303 if (image_get_magic(header) == IH_MAGIC) {
306 ret = spl_parse_legacy_header(spl_image, header);
312 if (IS_ENABLED(CONFIG_SPL_PANIC_ON_RAW_IMAGE)) {
314 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
315 * code which loads images in SPL cannot guarantee that
316 * absolutely all read errors will be reported.
317 * An example is the LPC32XX MLC NAND driver, which
318 * will consider that a completely unreadable NAND block
319 * is bad, and thus should be skipped silently.
321 panic("** no mkimage signature but raw image not supported");
324 if (CONFIG_IS_ENABLED(OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI)) {
327 if (!booti_setup((ulong)header, &start, &size, 0)) {
328 spl_image->name = "Linux";
329 spl_image->os = IH_OS_LINUX;
330 spl_image->load_addr = start;
331 spl_image->entry_point = start;
332 spl_image->size = size;
334 "payload Image, load addr: 0x%lx size: %d\n",
335 spl_image->load_addr, spl_image->size);
338 } else if (CONFIG_IS_ENABLED(OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTZ)) {
341 if (!bootz_setup((ulong)header, &start, &end)) {
342 spl_image->name = "Linux";
343 spl_image->os = IH_OS_LINUX;
344 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
345 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
346 spl_image->size = end - start;
348 "payload zImage, load addr: 0x%lx size: %d\n",
349 spl_image->load_addr, spl_image->size);
354 if (!spl_parse_board_header(spl_image, bootdev, (const void *)header,
358 if (IS_ENABLED(CONFIG_SPL_RAW_IMAGE_SUPPORT)) {
359 /* Signature not found - assume u-boot.bin */
360 debug("mkimage signature not found - ih_magic = %x\n",
362 spl_set_header_raw_uboot(spl_image);
364 /* RAW image not supported, proceed to other boot methods. */
365 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
372 #if SPL_LOAD_USERS > 1
373 int spl_load(struct spl_image_info *spl_image,
374 const struct spl_boot_device *bootdev, struct spl_load_info *info,
375 size_t size, size_t offset)
377 return _spl_load(spl_image, bootdev, info, size, offset);
381 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
383 typedef void __noreturn (*image_entry_noargs_t)(void);
385 image_entry_noargs_t image_entry =
386 (image_entry_noargs_t)spl_image->entry_point;
388 debug("image entry point: 0x%lx\n", spl_image->entry_point);
392 #if CONFIG_IS_ENABLED(HANDOFF)
394 * Set up the SPL hand-off information
396 * This is initially empty (zero) but can be written by
398 static int setup_spl_handoff(void)
400 struct spl_handoff *ho;
402 ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
409 __weak int handoff_arch_save(struct spl_handoff *ho)
414 static int write_spl_handoff(void)
416 struct spl_handoff *ho;
419 ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
422 handoff_save_dram(ho);
423 ret = handoff_arch_save(ho);
426 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
431 static inline int setup_spl_handoff(void) { return 0; }
432 static inline int write_spl_handoff(void) { return 0; }
437 * get_bootstage_id() - Get the bootstage ID to emit
439 * @start: true if this is for starting SPL, false for ending it
440 * Return: bootstage ID to use
442 static enum bootstage_id get_bootstage_id(bool start)
444 enum u_boot_phase phase = spl_phase();
446 if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
447 return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
448 else if (IS_ENABLED(CONFIG_VPL_BUILD) && phase == PHASE_VPL)
449 return start ? BOOTSTAGE_ID_START_VPL : BOOTSTAGE_ID_END_VPL;
451 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
454 static int spl_common_init(bool setup_malloc)
458 #if CONFIG_IS_ENABLED(SYS_MALLOC_F)
460 #ifdef CFG_MALLOC_F_ADDR
461 gd->malloc_base = CFG_MALLOC_F_ADDR;
463 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
467 ret = bootstage_init(u_boot_first_phase());
469 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
473 if (!u_boot_first_phase()) {
474 ret = bootstage_unstash_default();
476 log_debug("Failed to unstash bootstage: ret=%d\n", ret);
478 bootstage_mark_name(get_bootstage_id(true),
479 spl_phase_name(spl_phase()));
480 #if CONFIG_IS_ENABLED(LOG)
483 debug("%s: Failed to set up logging\n", __func__);
487 if (CONFIG_IS_ENABLED(OF_REAL)) {
488 ret = fdtdec_setup();
490 debug("fdtdec_setup() returned error %d\n", ret);
494 if (CONFIG_IS_ENABLED(DM)) {
495 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
496 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
497 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
498 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
499 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
501 debug("dm_init_and_scan() returned error %d\n", ret);
509 void spl_set_bd(void)
512 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
519 int spl_early_init(void)
523 debug("%s\n", __func__);
525 ret = spl_common_init(true);
528 gd->flags |= GD_FLG_SPL_EARLY_INIT;
536 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
537 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
539 debug("%s\n", __func__);
541 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
542 ret = spl_common_init(setup_malloc);
546 gd->flags |= GD_FLG_SPL_INIT;
551 #ifndef BOOT_DEVICE_NONE
552 #define BOOT_DEVICE_NONE 0xdeadbeef
555 __weak void board_boot_order(u32 *spl_boot_list)
557 spl_boot_list[0] = spl_boot_device();
560 __weak int spl_check_board_image(struct spl_image_info *spl_image,
561 const struct spl_boot_device *bootdev)
566 static int spl_load_image(struct spl_image_info *spl_image,
567 struct spl_image_loader *loader)
570 struct spl_boot_device bootdev;
572 bootdev.boot_device = loader->boot_device;
573 bootdev.boot_device_name = NULL;
575 ret = loader->load_image(spl_image, &bootdev);
576 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
577 if (!ret && spl_image->dcrc_length) {
579 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
580 spl_image->dcrc_length, CHUNKSZ_CRC32);
581 if (dcrc != spl_image->dcrc) {
582 puts("SPL: Image data CRC check failed!\n");
588 ret = spl_check_board_image(spl_image, &bootdev);
594 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
596 * @spl_image: Place to put the image details if successful
597 * @spl_boot_list: List of boot devices to try
598 * @count: Number of elements in spl_boot_list
599 * Return: 0 if OK, -ENODEV if there were no boot devices
600 * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
601 * devices but none worked
603 static int boot_from_devices(struct spl_image_info *spl_image,
604 u32 spl_boot_list[], int count)
606 struct spl_image_loader *drv =
607 ll_entry_start(struct spl_image_loader, spl_image_loader);
609 ll_entry_count(struct spl_image_loader, spl_image_loader);
613 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
614 struct spl_image_loader *loader;
615 int bootdev = spl_boot_list[i];
617 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
619 for (loader = drv; loader != drv + n_ents; loader++) {
620 if (bootdev != loader->boot_device)
622 if (!CONFIG_IS_ENABLED(SILENT_CONSOLE)) {
624 printf("Trying to boot from %s\n",
625 spl_loader_name(loader));
626 else if (CONFIG_IS_ENABLED(SHOW_ERRORS)) {
627 printf(SPL_TPL_PROMPT
628 "Unsupported Boot Device %d\n",
632 "Unsupported Boot Device!\n");
636 !spl_load_image(spl_image, loader)) {
637 spl_image->boot_device = bootdev;
646 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
647 void board_init_f(ulong dummy)
649 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
652 ret = spl_early_init();
654 debug("spl_early_init() failed: %d\n", ret);
659 preloader_console_init();
663 void board_init_r(gd_t *dummy1, ulong dummy2)
665 u32 spl_boot_list[] = {
672 typedef void __noreturn (*jump_to_image_t)(struct spl_image_info *);
673 jump_to_image_t jump_to_image = &jump_to_image_no_args;
674 struct spl_image_info spl_image;
677 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
681 if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC)) {
682 mem_malloc_init((ulong)map_sysmem(SPL_SYS_MALLOC_START,
683 SPL_SYS_MALLOC_SIZE),
684 SPL_SYS_MALLOC_SIZE);
685 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
687 if (!(gd->flags & GD_FLG_SPL_INIT)) {
692 if (CONFIG_IS_ENABLED(BLOBLIST)) {
693 ret = bloblist_init();
695 debug("%s: Failed to set up bloblist: ret=%d\n",
697 puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
701 if (CONFIG_IS_ENABLED(HANDOFF)) {
704 ret = setup_spl_handoff();
706 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
711 if (CONFIG_IS_ENABLED(SOC_INIT))
714 if (CONFIG_IS_ENABLED(BOARD_INIT))
717 if (IS_ENABLED(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT))
720 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
721 IS_ENABLED(CONFIG_SPL_ATF) || IS_ENABLED(CONFIG_SPL_NET))
722 dram_init_banksize();
724 if (CONFIG_IS_ENABLED(PCI) && !(gd->flags & GD_FLG_DM_DEAD)) {
727 puts(SPL_TPL_PROMPT "Cannot initialize PCI\n");
728 /* Don't fail. We still can try other boot methods. */
733 /* Dump driver model states to aid analysis */
734 if (CONFIG_IS_ENABLED(DM_STATS)) {
741 memset(&spl_image, '\0', sizeof(spl_image));
742 if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
743 spl_image.arg = (void *)SPL_PAYLOAD_ARGS_ADDR;
744 spl_image.boot_device = BOOT_DEVICE_NONE;
745 board_boot_order(spl_boot_list);
747 ret = boot_from_devices(&spl_image, spl_boot_list,
748 ARRAY_SIZE(spl_boot_list));
750 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
751 printf(SPL_TPL_PROMPT "failed to boot from all boot devices (err=%d)\n",
754 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
758 spl_perform_fixups(&spl_image);
761 if (os == IH_OS_U_BOOT) {
762 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
763 } else if (CONFIG_IS_ENABLED(ATF) && os == IH_OS_ARM_TRUSTED_FIRMWARE) {
764 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
765 spl_fixup_fdt(spl_image_fdt_addr(&spl_image));
766 jump_to_image = &spl_invoke_atf;
767 } else if (CONFIG_IS_ENABLED(OPTEE_IMAGE) && os == IH_OS_TEE) {
768 debug("Jumping to U-Boot via OP-TEE\n");
769 spl_board_prepare_for_optee(spl_image_fdt_addr(&spl_image));
770 jump_to_image = &jump_to_image_optee;
771 } else if (CONFIG_IS_ENABLED(OPENSBI) && os == IH_OS_OPENSBI) {
772 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
773 jump_to_image = &spl_invoke_opensbi;
774 } else if (CONFIG_IS_ENABLED(OS_BOOT) && os == IH_OS_LINUX) {
775 debug("Jumping to Linux\n");
776 if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
777 spl_fixup_fdt((void *)SPL_PAYLOAD_ARGS_ADDR);
778 spl_board_prepare_for_linux();
779 jump_to_image = &jump_to_image_linux;
781 debug("Unsupported OS image.. Jumping nevertheless..\n");
783 if (CONFIG_IS_ENABLED(SYS_MALLOC_F) &&
784 !IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIZE))
785 debug("SPL malloc() used 0x%x bytes (%d KB)\n",
786 gd_malloc_ptr(), gd_malloc_ptr() / 1024);
788 bootstage_mark_name(get_bootstage_id(false), "end phase");
789 ret = bootstage_stash_default();
791 debug("Failed to stash bootstage: err=%d\n", ret);
793 if (IS_ENABLED(CONFIG_SPL_VIDEO_REMOVE)) {
797 rc = uclass_find_device(UCLASS_VIDEO, 0, &dev);
799 rc = device_remove(dev, DM_REMOVE_NORMAL);
801 printf("Cannot remove video device '%s' (err=%d)\n",
805 if (CONFIG_IS_ENABLED(HANDOFF)) {
806 ret = write_spl_handoff();
808 printf(SPL_TPL_PROMPT
809 "SPL hand-off write failed (err=%d)\n", ret);
811 if (CONFIG_IS_ENABLED(UPL_OUT) && (gd->flags & GD_FLG_UPL)) {
812 ret = spl_write_upl_handoff(&spl_image);
814 printf(SPL_TPL_PROMPT
815 "UPL hand-off write failed (err=%d)\n", ret);
819 if (CONFIG_IS_ENABLED(BLOBLIST)) {
820 ret = bloblist_finish();
822 printf("Warning: Failed to finish bloblist (ret=%d)\n",
826 spl_board_prepare_for_boot();
827 jump_to_image(&spl_image);
831 * This requires UART clocks to be enabled. In order for this to work the
832 * caller must ensure that the gd pointer is valid.
834 void preloader_console_init(void)
836 #ifdef CONFIG_SPL_SERIAL
837 gd->baudrate = CONFIG_BAUDRATE;
839 serial_init(); /* serial communications setup */
841 gd->flags |= GD_FLG_HAVE_CONSOLE;
843 #if CONFIG_IS_ENABLED(BANNER_PRINT)
844 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
845 U_BOOT_TIME " " U_BOOT_TZ ")\n");
847 #ifdef CONFIG_SPL_DISPLAY_PRINT
854 * This function is called before the stack is changed from initial stack to
855 * relocated stack. It tries to dump the stack size used
857 __weak void spl_relocate_stack_check(void)
859 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
860 ulong init_sp = gd->start_addr_sp;
861 ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
862 u8 *ptr = (u8 *)stack_bottom;
865 for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
866 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
870 printf("SPL initial stack usage: %lu bytes\n",
871 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
876 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
878 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
879 * for the main board_init_r() execution. This is typically because we need
880 * more stack space for things like the MMC sub-system.
882 * This function calculates the stack position, copies the global_data into
883 * place, sets the new gd (except for ARM, for which setting GD within a C
884 * function may not always work) and returns the new stack position. The
885 * caller is responsible for setting up the sp register and, in the case
886 * of ARM, setting up gd.
888 * All of this is done using the same layout and alignments as done in
889 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
891 * Return: new stack location, or 0 to use the same stack
893 ulong spl_relocate_stack_gd(void)
895 #ifdef CONFIG_SPL_STACK_R
897 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
899 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
900 spl_relocate_stack_check();
902 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_IS_ENABLED(SYS_MALLOC_F)
903 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
904 debug("SPL malloc() before relocation used 0x%x bytes (%d KB)\n",
905 gd->malloc_ptr, gd->malloc_ptr / 1024);
906 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
907 gd->malloc_base = ptr;
908 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
912 /* Get stack position: use 8-byte alignment for ABI compliance */
913 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
914 gd->start_addr_sp = ptr;
915 new_gd = (gd_t *)ptr;
916 memcpy(new_gd, (void *)gd, sizeof(gd_t));
917 #if CONFIG_IS_ENABLED(DM)
918 dm_fixup_for_gd_move(new_gd);
920 #if CONFIG_IS_ENABLED(LOG)
921 log_fixup_for_gd_move(new_gd);
923 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
932 #if defined(CONFIG_BOOTCOUNT_LIMIT) && \
933 ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
934 (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
935 void bootcount_store(ulong a)
939 ulong bootcount_load(void)