common: Move serial functions out of common.h
[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 <serial.h>
15 #include <spl.h>
16 #include <asm/u-boot.h>
17 #include <nand.h>
18 #include <fat.h>
19 #include <u-boot/crc.h>
20 #include <version.h>
21 #include <image.h>
22 #include <malloc.h>
23 #include <mapmem.h>
24 #include <dm/root.h>
25 #include <linux/compiler.h>
26 #include <fdt_support.h>
27 #include <bootcount.h>
28 #include <wdt.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #ifndef CONFIG_SYS_UBOOT_START
33 #define CONFIG_SYS_UBOOT_START  CONFIG_SYS_TEXT_BASE
34 #endif
35 #ifndef CONFIG_SYS_MONITOR_LEN
36 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
37 #define CONFIG_SYS_MONITOR_LEN  (200 * 1024)
38 #endif
39
40 u32 *boot_params_ptr = NULL;
41
42 /* See spl.h for information about this */
43 binman_sym_declare(ulong, u_boot_any, image_pos);
44
45 /* Define board data structure */
46 static bd_t bdata __attribute__ ((section(".data")));
47
48 /*
49  * Board-specific Platform code can reimplement show_boot_progress () if needed
50  */
51 __weak void show_boot_progress(int val) {}
52
53 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF)
54 /* weak, default platform-specific function to initialize dram banks */
55 __weak int dram_init_banksize(void)
56 {
57         return 0;
58 }
59 #endif
60
61 /*
62  * Default function to determine if u-boot or the OS should
63  * be started. This implementation always returns 1.
64  *
65  * Please implement your own board specific funcion to do this.
66  *
67  * RETURN
68  * 0 to not start u-boot
69  * positive if u-boot should start
70  */
71 #ifdef CONFIG_SPL_OS_BOOT
72 __weak int spl_start_uboot(void)
73 {
74         puts(SPL_TPL_PROMPT
75              "Please implement spl_start_uboot() for your board\n");
76         puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
77         return 1;
78 }
79
80 /*
81  * Weak default function for arch specific zImage check. Return zero
82  * and fill start and end address if image is recognized.
83  */
84 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
85 {
86          return 1;
87 }
88 #endif
89
90 /* Weak default function for arch/board-specific fixups to the spl_image_info */
91 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
92 {
93 }
94
95 void spl_fixup_fdt(void)
96 {
97 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
98         void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
99         int err;
100
101         err = fdt_check_header(fdt_blob);
102         if (err < 0) {
103                 printf("fdt_root: %s\n", fdt_strerror(err));
104                 return;
105         }
106
107         /* fixup the memory dt node */
108         err = fdt_shrink_to_minimum(fdt_blob, 0);
109         if (err == 0) {
110                 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
111                 return;
112         }
113
114         err = arch_fixup_fdt(fdt_blob);
115         if (err) {
116                 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
117                 return;
118         }
119 #endif
120 }
121
122 /*
123  * Weak default function for board specific cleanup/preparation before
124  * Linux boot. Some boards/platforms might not need it, so just provide
125  * an empty stub here.
126  */
127 __weak void spl_board_prepare_for_linux(void)
128 {
129         /* Nothing to do! */
130 }
131
132 __weak void spl_board_prepare_for_boot(void)
133 {
134         /* Nothing to do! */
135 }
136
137 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
138 {
139         return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
140 }
141
142 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
143 {
144         ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
145
146         spl_image->size = CONFIG_SYS_MONITOR_LEN;
147
148         /*
149          * Binman error cases: address of the end of the previous region or the
150          * start of the image's entry area (usually 0) if there is no previous
151          * region.
152          */
153         if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
154                 /* Binman does not support separated entry addresses */
155                 spl_image->entry_point = u_boot_pos;
156                 spl_image->load_addr = u_boot_pos;
157         } else {
158                 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
159                 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
160         }
161         spl_image->os = IH_OS_U_BOOT;
162         spl_image->name = "U-Boot";
163 }
164
165 #ifdef CONFIG_SPL_LOAD_FIT_FULL
166 /* Parse and load full fitImage in SPL */
167 static int spl_load_fit_image(struct spl_image_info *spl_image,
168                               const struct image_header *header)
169 {
170         bootm_headers_t images;
171         const char *fit_uname_config = NULL;
172         const char *fit_uname_fdt = FIT_FDT_PROP;
173         const char *uname;
174         ulong fw_data = 0, dt_data = 0, img_data = 0;
175         ulong fw_len = 0, dt_len = 0, img_len = 0;
176         int idx, conf_noffset;
177         int ret;
178
179 #ifdef CONFIG_SPL_FIT_SIGNATURE
180         images.verify = 1;
181 #endif
182         ret = fit_image_load(&images, (ulong)header,
183                              NULL, &fit_uname_config,
184                              IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
185                              FIT_LOAD_REQUIRED, &fw_data, &fw_len);
186         if (ret < 0)
187                 return ret;
188
189         spl_image->size = fw_len;
190         spl_image->entry_point = fw_data;
191         spl_image->load_addr = fw_data;
192         spl_image->os = IH_OS_U_BOOT;
193         spl_image->name = "U-Boot";
194
195         debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
196               spl_image->name, spl_image->load_addr, spl_image->size);
197
198 #ifdef CONFIG_SPL_FIT_SIGNATURE
199         images.verify = 1;
200 #endif
201         ret = fit_image_load(&images, (ulong)header,
202                        &fit_uname_fdt, &fit_uname_config,
203                        IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
204                        FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
205         if (ret >= 0)
206                 spl_image->fdt_addr = (void *)dt_data;
207
208         conf_noffset = fit_conf_get_node((const void *)header,
209                                          fit_uname_config);
210         if (conf_noffset <= 0)
211                 return 0;
212
213         for (idx = 0;
214              uname = fdt_stringlist_get((const void *)header, conf_noffset,
215                                         FIT_LOADABLE_PROP, idx,
216                                 NULL), uname;
217              idx++)
218         {
219 #ifdef CONFIG_SPL_FIT_SIGNATURE
220                 images.verify = 1;
221 #endif
222                 ret = fit_image_load(&images, (ulong)header,
223                                      &uname, &fit_uname_config,
224                                      IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
225                                      FIT_LOAD_OPTIONAL_NON_ZERO,
226                                      &img_data, &img_len);
227                 if (ret < 0)
228                         return ret;
229         }
230
231         return 0;
232 }
233 #endif
234
235 int spl_parse_image_header(struct spl_image_info *spl_image,
236                            const struct image_header *header)
237 {
238 #ifdef CONFIG_SPL_LOAD_FIT_FULL
239         int ret = spl_load_fit_image(spl_image, header);
240
241         if (!ret)
242                 return ret;
243 #endif
244         if (image_get_magic(header) == IH_MAGIC) {
245 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
246                 u32 header_size = sizeof(struct image_header);
247
248 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
249                 /* check uImage header CRC */
250                 if (!image_check_hcrc(header)) {
251                         puts("SPL: Image header CRC check failed!\n");
252                         return -EINVAL;
253                 }
254 #endif
255
256                 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
257                         /*
258                          * On some system (e.g. powerpc), the load-address and
259                          * entry-point is located at address 0. We can't load
260                          * to 0-0x40. So skip header in this case.
261                          */
262                         spl_image->load_addr = image_get_load(header);
263                         spl_image->entry_point = image_get_ep(header);
264                         spl_image->size = image_get_data_size(header);
265                 } else {
266                         spl_image->entry_point = image_get_load(header);
267                         /* Load including the header */
268                         spl_image->load_addr = spl_image->entry_point -
269                                 header_size;
270                         spl_image->size = image_get_data_size(header) +
271                                 header_size;
272                 }
273 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
274                 /* store uImage data length and CRC to check later */
275                 spl_image->dcrc_data = image_get_load(header);
276                 spl_image->dcrc_length = image_get_data_size(header);
277                 spl_image->dcrc = image_get_dcrc(header);
278 #endif
279
280                 spl_image->os = image_get_os(header);
281                 spl_image->name = image_get_name(header);
282                 debug(SPL_TPL_PROMPT
283                       "payload image: %32s load addr: 0x%lx size: %d\n",
284                       spl_image->name, spl_image->load_addr, spl_image->size);
285 #else
286                 /* LEGACY image not supported */
287                 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
288                 return -EINVAL;
289 #endif
290         } else {
291 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
292                 /*
293                  * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
294                  * code which loads images in SPL cannot guarantee that
295                  * absolutely all read errors will be reported.
296                  * An example is the LPC32XX MLC NAND driver, which
297                  * will consider that a completely unreadable NAND block
298                  * is bad, and thus should be skipped silently.
299                  */
300                 panic("** no mkimage signature but raw image not supported");
301 #endif
302
303 #ifdef CONFIG_SPL_OS_BOOT
304                 ulong start, end;
305
306                 if (!bootz_setup((ulong)header, &start, &end)) {
307                         spl_image->name = "Linux";
308                         spl_image->os = IH_OS_LINUX;
309                         spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
310                         spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
311                         spl_image->size = end - start;
312                         debug(SPL_TPL_PROMPT
313                               "payload zImage, load addr: 0x%lx size: %d\n",
314                               spl_image->load_addr, spl_image->size);
315                         return 0;
316                 }
317 #endif
318
319 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
320                 /* Signature not found - assume u-boot.bin */
321                 debug("mkimage signature not found - ih_magic = %x\n",
322                         header->ih_magic);
323                 spl_set_header_raw_uboot(spl_image);
324 #else
325                 /* RAW image not supported, proceed to other boot methods. */
326                 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
327                 return -EINVAL;
328 #endif
329         }
330
331         return 0;
332 }
333
334 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
335 {
336         typedef void __noreturn (*image_entry_noargs_t)(void);
337
338         image_entry_noargs_t image_entry =
339                 (image_entry_noargs_t)spl_image->entry_point;
340
341         debug("image entry point: 0x%lx\n", spl_image->entry_point);
342         image_entry();
343 }
344
345 #if CONFIG_IS_ENABLED(HANDOFF)
346 /**
347  * Set up the SPL hand-off information
348  *
349  * This is initially empty (zero) but can be written by
350  */
351 static int setup_spl_handoff(void)
352 {
353         struct spl_handoff *ho;
354
355         ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
356         if (!ho)
357                 return -ENOENT;
358
359         return 0;
360 }
361
362 __weak int handoff_arch_save(struct spl_handoff *ho)
363 {
364         return 0;
365 }
366
367 static int write_spl_handoff(void)
368 {
369         struct spl_handoff *ho;
370         int ret;
371
372         ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
373         if (!ho)
374                 return -ENOENT;
375         handoff_save_dram(ho);
376         ret = handoff_arch_save(ho);
377         if (ret)
378                 return ret;
379         debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
380
381         return 0;
382 }
383 #else
384 static inline int setup_spl_handoff(void) { return 0; }
385 static inline int write_spl_handoff(void) { return 0; }
386
387 #endif /* HANDOFF */
388
389 static int spl_common_init(bool setup_malloc)
390 {
391         int ret;
392
393 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
394         if (setup_malloc) {
395 #ifdef CONFIG_MALLOC_F_ADDR
396                 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
397 #endif
398                 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
399                 gd->malloc_ptr = 0;
400         }
401 #endif
402         ret = bootstage_init(u_boot_first_phase());
403         if (ret) {
404                 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
405                       ret);
406                 return ret;
407         }
408 #ifdef CONFIG_BOOTSTAGE_STASH
409         if (!u_boot_first_phase()) {
410                 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
411                                                CONFIG_BOOTSTAGE_STASH_SIZE);
412
413                 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
414                 if (ret)
415                         debug("%s: Failed to unstash bootstage: ret=%d\n",
416                               __func__, ret);
417         }
418 #endif /* CONFIG_BOOTSTAGE_STASH */
419         bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_START_TPL :
420                             BOOTSTAGE_ID_START_SPL, SPL_TPL_NAME);
421 #if CONFIG_IS_ENABLED(LOG)
422         ret = log_init();
423         if (ret) {
424                 debug("%s: Failed to set up logging\n", __func__);
425                 return ret;
426         }
427 #endif
428         if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
429                 ret = fdtdec_setup();
430                 if (ret) {
431                         debug("fdtdec_setup() returned error %d\n", ret);
432                         return ret;
433                 }
434         }
435         if (CONFIG_IS_ENABLED(DM)) {
436                 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL,
437                                 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
438                 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
439                 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
440                 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
441                 if (ret) {
442                         debug("dm_init_and_scan() returned error %d\n", ret);
443                         return ret;
444                 }
445         }
446
447         return 0;
448 }
449
450 void spl_set_bd(void)
451 {
452         /*
453          * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
454          * writeable.
455          */
456         if (!gd->bd)
457                 gd->bd = &bdata;
458 }
459
460 int spl_early_init(void)
461 {
462         int ret;
463
464         debug("%s\n", __func__);
465
466         ret = spl_common_init(true);
467         if (ret)
468                 return ret;
469         gd->flags |= GD_FLG_SPL_EARLY_INIT;
470
471         return 0;
472 }
473
474 int spl_init(void)
475 {
476         int ret;
477         bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
478                         IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
479
480         debug("%s\n", __func__);
481
482         if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
483                 ret = spl_common_init(setup_malloc);
484                 if (ret)
485                         return ret;
486         }
487         gd->flags |= GD_FLG_SPL_INIT;
488
489         return 0;
490 }
491
492 #ifndef BOOT_DEVICE_NONE
493 #define BOOT_DEVICE_NONE 0xdeadbeef
494 #endif
495
496 __weak void board_boot_order(u32 *spl_boot_list)
497 {
498         spl_boot_list[0] = spl_boot_device();
499 }
500
501 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
502 {
503         struct spl_image_loader *drv =
504                 ll_entry_start(struct spl_image_loader, spl_image_loader);
505         const int n_ents =
506                 ll_entry_count(struct spl_image_loader, spl_image_loader);
507         struct spl_image_loader *entry;
508
509         for (entry = drv; entry != drv + n_ents; entry++) {
510                 if (boot_device == entry->boot_device)
511                         return entry;
512         }
513
514         /* Not found */
515         return NULL;
516 }
517
518 static int spl_load_image(struct spl_image_info *spl_image,
519                           struct spl_image_loader *loader)
520 {
521         int ret;
522         struct spl_boot_device bootdev;
523
524         bootdev.boot_device = loader->boot_device;
525         bootdev.boot_device_name = NULL;
526
527         ret = loader->load_image(spl_image, &bootdev);
528 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
529         if (!ret && spl_image->dcrc_length) {
530                 /* check data crc */
531                 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
532                                       spl_image->dcrc_length, CHUNKSZ_CRC32);
533                 if (dcrc != spl_image->dcrc) {
534                         puts("SPL: Image data CRC check failed!\n");
535                         ret = -EINVAL;
536                 }
537         }
538 #endif
539         return ret;
540 }
541
542 /**
543  * boot_from_devices() - Try loading a booting U-Boot from a list of devices
544  *
545  * @spl_image: Place to put the image details if successful
546  * @spl_boot_list: List of boot devices to try
547  * @count: Number of elements in spl_boot_list
548  * @return 0 if OK, -ve on error
549  */
550 static int boot_from_devices(struct spl_image_info *spl_image,
551                              u32 spl_boot_list[], int count)
552 {
553         int i;
554
555         for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
556                 struct spl_image_loader *loader;
557
558                 loader = spl_ll_find_loader(spl_boot_list[i]);
559 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
560                 if (loader)
561                         printf("Trying to boot from %s\n", loader->name);
562                 else
563                         puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
564 #endif
565                 if (loader && !spl_load_image(spl_image, loader)) {
566                         spl_image->boot_device = spl_boot_list[i];
567                         return 0;
568                 }
569         }
570
571         return -ENODEV;
572 }
573
574 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
575 void board_init_f(ulong dummy)
576 {
577         if (CONFIG_IS_ENABLED(OF_CONTROL)) {
578                 int ret;
579
580                 ret = spl_early_init();
581                 if (ret) {
582                         debug("spl_early_init() failed: %d\n", ret);
583                         hang();
584                 }
585         }
586
587         if (CONFIG_IS_ENABLED(SERIAL_SUPPORT))
588                 preloader_console_init();
589 }
590 #endif
591
592 void board_init_r(gd_t *dummy1, ulong dummy2)
593 {
594         u32 spl_boot_list[] = {
595                 BOOT_DEVICE_NONE,
596                 BOOT_DEVICE_NONE,
597                 BOOT_DEVICE_NONE,
598                 BOOT_DEVICE_NONE,
599                 BOOT_DEVICE_NONE,
600         };
601         struct spl_image_info spl_image;
602         int ret;
603
604         debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
605
606         spl_set_bd();
607
608 #if defined(CONFIG_SYS_SPL_MALLOC_START)
609         mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
610                         CONFIG_SYS_SPL_MALLOC_SIZE);
611         gd->flags |= GD_FLG_FULL_MALLOC_INIT;
612 #endif
613         if (!(gd->flags & GD_FLG_SPL_INIT)) {
614                 if (spl_init())
615                         hang();
616         }
617 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
618         /*
619          * timer_init() does not exist on PPC systems. The timer is initialized
620          * and enabled (decrementer) in interrupt_init() here.
621          */
622         timer_init();
623 #endif
624         if (CONFIG_IS_ENABLED(BLOBLIST)) {
625                 ret = bloblist_init();
626                 if (ret) {
627                         debug("%s: Failed to set up bloblist: ret=%d\n",
628                               __func__, ret);
629                         puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
630                         hang();
631                 }
632         }
633         if (CONFIG_IS_ENABLED(HANDOFF)) {
634                 int ret;
635
636                 ret = setup_spl_handoff();
637                 if (ret) {
638                         puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
639                         hang();
640                 }
641         }
642
643 #if CONFIG_IS_ENABLED(BOARD_INIT)
644         spl_board_init();
645 #endif
646
647 #if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
648         initr_watchdog();
649 #endif
650
651         if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
652                 dram_init_banksize();
653
654         bootcount_inc();
655
656         memset(&spl_image, '\0', sizeof(spl_image));
657 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
658         spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
659 #endif
660         spl_image.boot_device = BOOT_DEVICE_NONE;
661         board_boot_order(spl_boot_list);
662
663         if (boot_from_devices(&spl_image, spl_boot_list,
664                               ARRAY_SIZE(spl_boot_list))) {
665                 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
666                 hang();
667         }
668
669         spl_perform_fixups(&spl_image);
670         if (CONFIG_IS_ENABLED(HANDOFF)) {
671                 ret = write_spl_handoff();
672                 if (ret)
673                         printf(SPL_TPL_PROMPT
674                                "SPL hand-off write failed (err=%d)\n", ret);
675         }
676         if (CONFIG_IS_ENABLED(BLOBLIST)) {
677                 ret = bloblist_finish();
678                 if (ret)
679                         printf("Warning: Failed to finish bloblist (ret=%d)\n",
680                                ret);
681         }
682
683 #ifdef CONFIG_CPU_V7M
684         spl_image.entry_point |= 0x1;
685 #endif
686         switch (spl_image.os) {
687         case IH_OS_U_BOOT:
688                 debug("Jumping to U-Boot\n");
689                 break;
690 #if CONFIG_IS_ENABLED(ATF)
691         case IH_OS_ARM_TRUSTED_FIRMWARE:
692                 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
693                 spl_invoke_atf(&spl_image);
694                 break;
695 #endif
696 #if CONFIG_IS_ENABLED(OPTEE)
697         case IH_OS_TEE:
698                 debug("Jumping to U-Boot via OP-TEE\n");
699                 spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
700                                 (void *)spl_image.entry_point);
701                 break;
702 #endif
703 #if CONFIG_IS_ENABLED(OPENSBI)
704         case IH_OS_OPENSBI:
705                 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
706                 spl_invoke_opensbi(&spl_image);
707                 break;
708 #endif
709 #ifdef CONFIG_SPL_OS_BOOT
710         case IH_OS_LINUX:
711                 debug("Jumping to Linux\n");
712                 spl_fixup_fdt();
713                 spl_board_prepare_for_linux();
714                 jump_to_image_linux(&spl_image);
715 #endif
716         default:
717                 debug("Unsupported OS image.. Jumping nevertheless..\n");
718         }
719 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
720         debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
721               gd->malloc_ptr / 1024);
722 #endif
723         bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_END_TPL :
724                             BOOTSTAGE_ID_END_SPL, "end " SPL_TPL_NAME);
725 #ifdef CONFIG_BOOTSTAGE_STASH
726         ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
727                               CONFIG_BOOTSTAGE_STASH_SIZE);
728         if (ret)
729                 debug("Failed to stash bootstage: err=%d\n", ret);
730 #endif
731
732         debug("loaded - jumping to U-Boot...\n");
733         spl_board_prepare_for_boot();
734         jump_to_image_no_args(&spl_image);
735 }
736
737 #ifdef CONFIG_SPL_SERIAL_SUPPORT
738 /*
739  * This requires UART clocks to be enabled.  In order for this to work the
740  * caller must ensure that the gd pointer is valid.
741  */
742 void preloader_console_init(void)
743 {
744         gd->baudrate = CONFIG_BAUDRATE;
745
746         serial_init();          /* serial communications setup */
747
748         gd->have_console = 1;
749
750 #if CONFIG_IS_ENABLED(BANNER_PRINT)
751         puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
752              U_BOOT_TIME " " U_BOOT_TZ ")\n");
753 #endif
754 #ifdef CONFIG_SPL_DISPLAY_PRINT
755         spl_display_print();
756 #endif
757 }
758 #endif
759
760 /**
761  * This function is called before the stack is changed from initial stack to
762  * relocated stack. It tries to dump the stack size used
763  */
764 __weak void spl_relocate_stack_check(void)
765 {
766 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
767         ulong init_sp = gd->start_addr_sp;
768         ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
769         u8 *ptr = (u8 *)stack_bottom;
770         ulong i;
771
772         for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
773                 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
774                         break;
775                 ptr++;
776         }
777         printf("SPL initial stack usage: %lu bytes\n",
778                CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
779 #endif
780 }
781
782 /**
783  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
784  *
785  * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
786  * for the main board_init_r() execution. This is typically because we need
787  * more stack space for things like the MMC sub-system.
788  *
789  * This function calculates the stack position, copies the global_data into
790  * place, sets the new gd (except for ARM, for which setting GD within a C
791  * function may not always work) and returns the new stack position. The
792  * caller is responsible for setting up the sp register and, in the case
793  * of ARM, setting up gd.
794  *
795  * All of this is done using the same layout and alignments as done in
796  * board_init_f_init_reserve() / board_init_f_alloc_reserve().
797  *
798  * @return new stack location, or 0 to use the same stack
799  */
800 ulong spl_relocate_stack_gd(void)
801 {
802 #ifdef CONFIG_SPL_STACK_R
803         gd_t *new_gd;
804         ulong ptr = CONFIG_SPL_STACK_R_ADDR;
805
806         if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
807                 spl_relocate_stack_check();
808
809 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
810         if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
811                 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
812                       gd->malloc_ptr, gd->malloc_ptr / 1024);
813                 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
814                 gd->malloc_base = ptr;
815                 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
816                 gd->malloc_ptr = 0;
817         }
818 #endif
819         /* Get stack position: use 8-byte alignment for ABI compliance */
820         ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
821         new_gd = (gd_t *)ptr;
822         memcpy(new_gd, (void *)gd, sizeof(gd_t));
823 #if CONFIG_IS_ENABLED(DM)
824         dm_fixup_for_gd_move(new_gd);
825 #endif
826 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
827         gd = new_gd;
828 #endif
829         return ptr;
830 #else
831         return 0;
832 #endif
833 }
834
835 #if defined(CONFIG_BOOTCOUNT_LIMIT) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
836 void bootcount_store(ulong a)
837 {
838 }
839
840 ulong bootcount_load(void)
841 {
842         return 0;
843 }
844 #endif