bootstage: Allow SPL to obtain bootstage info from TPL
[oweals/u-boot.git] / common / spl / spl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010
4  * Texas Instruments, <www.ti.com>
5  *
6  * Aneesh V <aneesh@ti.com>
7  */
8
9 #include <common.h>
10 #include <bloblist.h>
11 #include <binman_sym.h>
12 #include <dm.h>
13 #include <handoff.h>
14 #include <spl.h>
15 #include <asm/u-boot.h>
16 #include <nand.h>
17 #include <fat.h>
18 #include <version.h>
19 #include <image.h>
20 #include <malloc.h>
21 #include <mapmem.h>
22 #include <dm/root.h>
23 #include <linux/compiler.h>
24 #include <fdt_support.h>
25 #include <bootcount.h>
26 #include <wdt.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 #ifndef CONFIG_SYS_UBOOT_START
31 #define CONFIG_SYS_UBOOT_START  CONFIG_SYS_TEXT_BASE
32 #endif
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)
36 #endif
37
38 u32 *boot_params_ptr = NULL;
39
40 /* See spl.h for information about this */
41 binman_sym_declare(ulong, u_boot_any, image_pos);
42
43 /* Define board data structure */
44 static bd_t bdata __attribute__ ((section(".data")));
45
46 /*
47  * Board-specific Platform code can reimplement show_boot_progress () if needed
48  */
49 __weak void show_boot_progress(int val) {}
50
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)
54 {
55         return 0;
56 }
57 #endif
58
59 /*
60  * Default function to determine if u-boot or the OS should
61  * be started. This implementation always returns 1.
62  *
63  * Please implement your own board specific funcion to do this.
64  *
65  * RETURN
66  * 0 to not start u-boot
67  * positive if u-boot should start
68  */
69 #ifdef CONFIG_SPL_OS_BOOT
70 __weak int spl_start_uboot(void)
71 {
72         puts(SPL_TPL_PROMPT
73              "Please implement spl_start_uboot() for your board\n");
74         puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
75         return 1;
76 }
77
78 /*
79  * Weak default function for arch specific zImage check. Return zero
80  * and fill start and end address if image is recognized.
81  */
82 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
83 {
84          return 1;
85 }
86 #endif
87
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)
90 {
91 }
92
93 void spl_fixup_fdt(void)
94 {
95 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
96         void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
97         int err;
98
99         err = fdt_check_header(fdt_blob);
100         if (err < 0) {
101                 printf("fdt_root: %s\n", fdt_strerror(err));
102                 return;
103         }
104
105         /* fixup the memory dt node */
106         err = fdt_shrink_to_minimum(fdt_blob, 0);
107         if (err == 0) {
108                 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
109                 return;
110         }
111
112         err = arch_fixup_fdt(fdt_blob);
113         if (err) {
114                 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
115                 return;
116         }
117 #endif
118 }
119
120 /*
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.
124  */
125 __weak void spl_board_prepare_for_linux(void)
126 {
127         /* Nothing to do! */
128 }
129
130 __weak void spl_board_prepare_for_boot(void)
131 {
132         /* Nothing to do! */
133 }
134
135 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
136 {
137         return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
138 }
139
140 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
141 {
142         ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
143
144         spl_image->size = CONFIG_SYS_MONITOR_LEN;
145
146         /*
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
149          * region.
150          */
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;
155         } else {
156                 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
157                 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
158         }
159         spl_image->os = IH_OS_U_BOOT;
160         spl_image->name = "U-Boot";
161 }
162
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)
167 {
168         bootm_headers_t images;
169         const char *fit_uname_config = NULL;
170         const char *fit_uname_fdt = FIT_FDT_PROP;
171         const char *uname;
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;
175         int ret;
176
177 #ifdef CONFIG_SPL_FIT_SIGNATURE
178         images.verify = 1;
179 #endif
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);
184         if (ret < 0)
185                 return ret;
186
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";
192
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);
195
196 #ifdef CONFIG_SPL_FIT_SIGNATURE
197         images.verify = 1;
198 #endif
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);
203         if (ret >= 0)
204                 spl_image->fdt_addr = (void *)dt_data;
205
206         conf_noffset = fit_conf_get_node((const void *)header,
207                                          fit_uname_config);
208         if (conf_noffset <= 0)
209                 return 0;
210
211         for (idx = 0;
212              uname = fdt_stringlist_get((const void *)header, conf_noffset,
213                                         FIT_LOADABLE_PROP, idx,
214                                 NULL), uname;
215              idx++)
216         {
217 #ifdef CONFIG_SPL_FIT_SIGNATURE
218                 images.verify = 1;
219 #endif
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);
225                 if (ret < 0)
226                         return ret;
227         }
228
229         return 0;
230 }
231 #endif
232
233 int spl_parse_image_header(struct spl_image_info *spl_image,
234                            const struct image_header *header)
235 {
236 #ifdef CONFIG_SPL_LOAD_FIT_FULL
237         int ret = spl_load_fit_image(spl_image, header);
238
239         if (!ret)
240                 return ret;
241 #endif
242         if (image_get_magic(header) == IH_MAGIC) {
243 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
244                 u32 header_size = sizeof(struct image_header);
245
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");
250                         return -EINVAL;
251                 }
252 #endif
253
254                 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
255                         /*
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.
259                          */
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);
263                 } else {
264                         spl_image->entry_point = image_get_load(header);
265                         /* Load including the header */
266                         spl_image->load_addr = spl_image->entry_point -
267                                 header_size;
268                         spl_image->size = image_get_data_size(header) +
269                                 header_size;
270                 }
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);
276 #endif
277
278                 spl_image->os = image_get_os(header);
279                 spl_image->name = image_get_name(header);
280                 debug(SPL_TPL_PROMPT
281                       "payload image: %32s load addr: 0x%lx size: %d\n",
282                       spl_image->name, spl_image->load_addr, spl_image->size);
283 #else
284                 /* LEGACY image not supported */
285                 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
286                 return -EINVAL;
287 #endif
288         } else {
289 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
290                 /*
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.
297                  */
298                 panic("** no mkimage signature but raw image not supported");
299 #endif
300
301 #ifdef CONFIG_SPL_OS_BOOT
302                 ulong start, end;
303
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;
310                         debug(SPL_TPL_PROMPT
311                               "payload zImage, load addr: 0x%lx size: %d\n",
312                               spl_image->load_addr, spl_image->size);
313                         return 0;
314                 }
315 #endif
316
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",
320                         header->ih_magic);
321                 spl_set_header_raw_uboot(spl_image);
322 #else
323                 /* RAW image not supported, proceed to other boot methods. */
324                 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
325                 return -EINVAL;
326 #endif
327         }
328
329         return 0;
330 }
331
332 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
333 {
334         typedef void __noreturn (*image_entry_noargs_t)(void);
335
336         image_entry_noargs_t image_entry =
337                 (image_entry_noargs_t)spl_image->entry_point;
338
339         debug("image entry point: 0x%lx\n", spl_image->entry_point);
340         image_entry();
341 }
342
343 #if CONFIG_IS_ENABLED(HANDOFF)
344 /**
345  * Set up the SPL hand-off information
346  *
347  * This is initially empty (zero) but can be written by
348  */
349 static int setup_spl_handoff(void)
350 {
351         struct spl_handoff *ho;
352
353         ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
354         if (!ho)
355                 return -ENOENT;
356
357         return 0;
358 }
359
360 __weak int handoff_arch_save(struct spl_handoff *ho)
361 {
362         return 0;
363 }
364
365 static int write_spl_handoff(void)
366 {
367         struct spl_handoff *ho;
368         int ret;
369
370         ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
371         if (!ho)
372                 return -ENOENT;
373         handoff_save_dram(ho);
374         ret = handoff_arch_save(ho);
375         if (ret)
376                 return ret;
377         debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
378
379         return 0;
380 }
381 #else
382 static inline int setup_spl_handoff(void) { return 0; }
383 static inline int write_spl_handoff(void) { return 0; }
384
385 #endif /* HANDOFF */
386
387 static int spl_common_init(bool setup_malloc)
388 {
389         int ret;
390
391 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
392         if (setup_malloc) {
393 #ifdef CONFIG_MALLOC_F_ADDR
394                 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
395 #endif
396                 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
397                 gd->malloc_ptr = 0;
398         }
399 #endif
400         ret = bootstage_init(u_boot_first_phase());
401         if (ret) {
402                 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
403                       ret);
404                 return ret;
405         }
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);
410
411                 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
412                 if (ret)
413                         debug("%s: Failed to unstash bootstage: ret=%d\n",
414                               __func__, ret);
415         }
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)
420         ret = log_init();
421         if (ret) {
422                 debug("%s: Failed to set up logging\n", __func__);
423                 return ret;
424         }
425 #endif
426         if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
427                 ret = fdtdec_setup();
428                 if (ret) {
429                         debug("fdtdec_setup() returned error %d\n", ret);
430                         return ret;
431                 }
432         }
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);
439                 if (ret) {
440                         debug("dm_init_and_scan() returned error %d\n", ret);
441                         return ret;
442                 }
443         }
444
445         return 0;
446 }
447
448 void spl_set_bd(void)
449 {
450         /*
451          * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
452          * writeable.
453          */
454         if (!gd->bd)
455                 gd->bd = &bdata;
456 }
457
458 int spl_early_init(void)
459 {
460         int ret;
461
462         debug("%s\n", __func__);
463
464         ret = spl_common_init(true);
465         if (ret)
466                 return ret;
467         gd->flags |= GD_FLG_SPL_EARLY_INIT;
468
469         return 0;
470 }
471
472 int spl_init(void)
473 {
474         int ret;
475         bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
476                         IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
477
478         debug("%s\n", __func__);
479
480         if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
481                 ret = spl_common_init(setup_malloc);
482                 if (ret)
483                         return ret;
484         }
485         gd->flags |= GD_FLG_SPL_INIT;
486
487         return 0;
488 }
489
490 #ifndef BOOT_DEVICE_NONE
491 #define BOOT_DEVICE_NONE 0xdeadbeef
492 #endif
493
494 __weak void board_boot_order(u32 *spl_boot_list)
495 {
496         spl_boot_list[0] = spl_boot_device();
497 }
498
499 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
500 {
501         struct spl_image_loader *drv =
502                 ll_entry_start(struct spl_image_loader, spl_image_loader);
503         const int n_ents =
504                 ll_entry_count(struct spl_image_loader, spl_image_loader);
505         struct spl_image_loader *entry;
506
507         for (entry = drv; entry != drv + n_ents; entry++) {
508                 if (boot_device == entry->boot_device)
509                         return entry;
510         }
511
512         /* Not found */
513         return NULL;
514 }
515
516 static int spl_load_image(struct spl_image_info *spl_image,
517                           struct spl_image_loader *loader)
518 {
519         int ret;
520         struct spl_boot_device bootdev;
521
522         bootdev.boot_device = loader->boot_device;
523         bootdev.boot_device_name = NULL;
524
525         ret = loader->load_image(spl_image, &bootdev);
526 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
527         if (!ret && spl_image->dcrc_length) {
528                 /* check data crc */
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");
533                         ret = -EINVAL;
534                 }
535         }
536 #endif
537         return ret;
538 }
539
540 /**
541  * boot_from_devices() - Try loading a booting U-Boot from a list of devices
542  *
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
547  */
548 static int boot_from_devices(struct spl_image_info *spl_image,
549                              u32 spl_boot_list[], int count)
550 {
551         int i;
552
553         for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
554                 struct spl_image_loader *loader;
555
556                 loader = spl_ll_find_loader(spl_boot_list[i]);
557 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
558                 if (loader)
559                         printf("Trying to boot from %s\n", loader->name);
560                 else
561                         puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
562 #endif
563                 if (loader && !spl_load_image(spl_image, loader)) {
564                         spl_image->boot_device = spl_boot_list[i];
565                         return 0;
566                 }
567         }
568
569         return -ENODEV;
570 }
571
572 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
573 void board_init_f(ulong dummy)
574 {
575         if (CONFIG_IS_ENABLED(OF_CONTROL)) {
576                 int ret;
577
578                 ret = spl_early_init();
579                 if (ret) {
580                         debug("spl_early_init() failed: %d\n", ret);
581                         hang();
582                 }
583         }
584
585         if (CONFIG_IS_ENABLED(SERIAL_SUPPORT))
586                 preloader_console_init();
587 }
588 #endif
589
590 void board_init_r(gd_t *dummy1, ulong dummy2)
591 {
592         u32 spl_boot_list[] = {
593                 BOOT_DEVICE_NONE,
594                 BOOT_DEVICE_NONE,
595                 BOOT_DEVICE_NONE,
596                 BOOT_DEVICE_NONE,
597                 BOOT_DEVICE_NONE,
598         };
599         struct spl_image_info spl_image;
600         int ret;
601
602         debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
603
604         spl_set_bd();
605
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;
610 #endif
611         if (!(gd->flags & GD_FLG_SPL_INIT)) {
612                 if (spl_init())
613                         hang();
614         }
615 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
616         /*
617          * timer_init() does not exist on PPC systems. The timer is initialized
618          * and enabled (decrementer) in interrupt_init() here.
619          */
620         timer_init();
621 #endif
622         if (CONFIG_IS_ENABLED(BLOBLIST)) {
623                 ret = bloblist_init();
624                 if (ret) {
625                         debug("%s: Failed to set up bloblist: ret=%d\n",
626                               __func__, ret);
627                         puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
628                         hang();
629                 }
630         }
631         if (CONFIG_IS_ENABLED(HANDOFF)) {
632                 int ret;
633
634                 ret = setup_spl_handoff();
635                 if (ret) {
636                         puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
637                         hang();
638                 }
639         }
640
641 #if CONFIG_IS_ENABLED(BOARD_INIT)
642         spl_board_init();
643 #endif
644
645 #if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
646         initr_watchdog();
647 #endif
648
649         if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
650                 dram_init_banksize();
651
652         bootcount_inc();
653
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;
657 #endif
658         spl_image.boot_device = BOOT_DEVICE_NONE;
659         board_boot_order(spl_boot_list);
660
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");
664                 hang();
665         }
666
667         spl_perform_fixups(&spl_image);
668         if (CONFIG_IS_ENABLED(HANDOFF)) {
669                 ret = write_spl_handoff();
670                 if (ret)
671                         printf(SPL_TPL_PROMPT
672                                "SPL hand-off write failed (err=%d)\n", ret);
673         }
674         if (CONFIG_IS_ENABLED(BLOBLIST)) {
675                 ret = bloblist_finish();
676                 if (ret)
677                         printf("Warning: Failed to finish bloblist (ret=%d)\n",
678                                ret);
679         }
680
681 #ifdef CONFIG_CPU_V7M
682         spl_image.entry_point |= 0x1;
683 #endif
684         switch (spl_image.os) {
685         case IH_OS_U_BOOT:
686                 debug("Jumping to U-Boot\n");
687                 break;
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);
692                 break;
693 #endif
694 #if CONFIG_IS_ENABLED(OPTEE)
695         case IH_OS_TEE:
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);
699                 break;
700 #endif
701 #if CONFIG_IS_ENABLED(OPENSBI)
702         case IH_OS_OPENSBI:
703                 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
704                 spl_invoke_opensbi(&spl_image);
705                 break;
706 #endif
707 #ifdef CONFIG_SPL_OS_BOOT
708         case IH_OS_LINUX:
709                 debug("Jumping to Linux\n");
710                 spl_fixup_fdt();
711                 spl_board_prepare_for_linux();
712                 jump_to_image_linux(&spl_image);
713 #endif
714         default:
715                 debug("Unsupported OS image.. Jumping nevertheless..\n");
716         }
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);
720 #endif
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);
726         if (ret)
727                 debug("Failed to stash bootstage: err=%d\n", ret);
728 #endif
729
730         debug("loaded - jumping to U-Boot...\n");
731         spl_board_prepare_for_boot();
732         jump_to_image_no_args(&spl_image);
733 }
734
735 #ifdef CONFIG_SPL_SERIAL_SUPPORT
736 /*
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.
739  */
740 void preloader_console_init(void)
741 {
742         gd->baudrate = CONFIG_BAUDRATE;
743
744         serial_init();          /* serial communications setup */
745
746         gd->have_console = 1;
747
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");
751 #endif
752 #ifdef CONFIG_SPL_DISPLAY_PRINT
753         spl_display_print();
754 #endif
755 }
756 #endif
757
758 /**
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
761  */
762 __weak void spl_relocate_stack_check(void)
763 {
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;
768         ulong i;
769
770         for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
771                 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
772                         break;
773                 ptr++;
774         }
775         printf("SPL initial stack usage: %lu bytes\n",
776                CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
777 #endif
778 }
779
780 /**
781  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
782  *
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.
786  *
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.
792  *
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().
795  *
796  * @return new stack location, or 0 to use the same stack
797  */
798 ulong spl_relocate_stack_gd(void)
799 {
800 #ifdef CONFIG_SPL_STACK_R
801         gd_t *new_gd;
802         ulong ptr = CONFIG_SPL_STACK_R_ADDR;
803
804         if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
805                 spl_relocate_stack_check();
806
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;
814                 gd->malloc_ptr = 0;
815         }
816 #endif
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);
823 #endif
824 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
825         gd = new_gd;
826 #endif
827         return ptr;
828 #else
829         return 0;
830 #endif
831 }