spl: Use CONFIG_SPL... instead of CONFIG_..._SPL_...
[pandora-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 <bootstage.h>
13 #include <dm.h>
14 #include <handoff.h>
15 #include <hang.h>
16 #include <init.h>
17 #include <irq_func.h>
18 #include <log.h>
19 #include <mapmem.h>
20 #include <serial.h>
21 #include <spl.h>
22 #include <system-constants.h>
23 #include <asm/global_data.h>
24 #include <asm-generic/gpio.h>
25 #include <asm/u-boot.h>
26 #include <nand.h>
27 #include <fat.h>
28 #include <u-boot/crc.h>
29 #if CONFIG_IS_ENABLED(BANNER_PRINT)
30 #include <timestamp.h>
31 #endif
32 #include <version.h>
33 #include <image.h>
34 #include <malloc.h>
35 #include <mapmem.h>
36 #include <dm/root.h>
37 #include <dm/util.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>
43 #include <wdt.h>
44
45 DECLARE_GLOBAL_DATA_PTR;
46 DECLARE_BINMAN_MAGIC_SYM;
47
48 u32 *boot_params_ptr = NULL;
49
50 #if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
51 /* See spl.h for information about this */
52 binman_sym_declare(ulong, u_boot_any, image_pos);
53 binman_sym_declare(ulong, u_boot_any, size);
54
55 #ifdef CONFIG_TPL
56 binman_sym_declare(ulong, u_boot_spl_any, image_pos);
57 binman_sym_declare(ulong, u_boot_spl_any, size);
58 #endif
59
60 #ifdef CONFIG_VPL
61 binman_sym_declare(ulong, u_boot_vpl_any, image_pos);
62 binman_sym_declare(ulong, u_boot_vpl_any, size);
63 #endif
64
65 #endif /* BINMAN_UBOOT_SYMBOLS */
66
67 /* Define board data structure */
68 static struct bd_info bdata __attribute__ ((section(".data")));
69
70 #if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
71 /*
72  * Board-specific Platform code can reimplement show_boot_progress () if needed
73  */
74 __weak void show_boot_progress(int val) {}
75 #endif
76
77 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
78         defined(CONFIG_SPL_ATF)
79 /* weak, default platform-specific function to initialize dram banks */
80 __weak int dram_init_banksize(void)
81 {
82         return 0;
83 }
84 #endif
85
86 /*
87  * Default function to determine if u-boot or the OS should
88  * be started. This implementation always returns 1.
89  *
90  * Please implement your own board specific funcion to do this.
91  *
92  * RETURN
93  * 0 to not start u-boot
94  * positive if u-boot should start
95  */
96 #if CONFIG_IS_ENABLED(OS_BOOT)
97 __weak int spl_start_uboot(void)
98 {
99         puts(SPL_TPL_PROMPT
100              "Please implement spl_start_uboot() for your board\n");
101         puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
102         return 1;
103 }
104
105 /*
106  * Weak default function for arch specific zImage check. Return zero
107  * and fill start and end address if image is recognized.
108  */
109 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
110 {
111          return 1;
112 }
113
114 int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
115 {
116          return 1;
117 }
118 #endif
119
120 /* Weak default function for arch/board-specific fixups to the spl_image_info */
121 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
122 {
123 }
124
125 void spl_fixup_fdt(void *fdt_blob)
126 {
127 #if defined(CONFIG_SPL_OF_LIBFDT)
128         int err;
129
130         if (!fdt_blob)
131                 return;
132
133         err = fdt_check_header(fdt_blob);
134         if (err < 0) {
135                 printf("fdt_root: %s\n", fdt_strerror(err));
136                 return;
137         }
138
139         /* fixup the memory dt node */
140         err = fdt_shrink_to_minimum(fdt_blob, 0);
141         if (err == 0) {
142                 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
143                 return;
144         }
145
146         err = arch_fixup_fdt(fdt_blob);
147         if (err) {
148                 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
149                 return;
150         }
151 #endif
152 }
153
154 ulong spl_get_image_pos(void)
155 {
156         if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
157                 return BINMAN_SYM_MISSING;
158
159 #ifdef CONFIG_VPL
160         if (spl_next_phase() == PHASE_VPL)
161                 return binman_sym(ulong, u_boot_vpl_any, image_pos);
162 #endif
163         return spl_next_phase() == PHASE_SPL ?
164                 binman_sym(ulong, u_boot_spl_any, image_pos) :
165                 binman_sym(ulong, u_boot_any, image_pos);
166 }
167
168 ulong spl_get_image_size(void)
169 {
170         if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
171                 return BINMAN_SYM_MISSING;
172
173 #ifdef CONFIG_VPL
174         if (spl_next_phase() == PHASE_VPL)
175                 return binman_sym(ulong, u_boot_vpl_any, size);
176 #endif
177         return spl_next_phase() == PHASE_SPL ?
178                 binman_sym(ulong, u_boot_spl_any, size) :
179                 binman_sym(ulong, u_boot_any, size);
180 }
181
182 ulong spl_get_image_text_base(void)
183 {
184 #ifdef CONFIG_VPL
185         if (spl_next_phase() == PHASE_VPL)
186                 return CONFIG_VPL_TEXT_BASE;
187 #endif
188         return spl_next_phase() == PHASE_SPL ? CONFIG_SPL_TEXT_BASE :
189                 CONFIG_TEXT_BASE;
190 }
191
192 /*
193  * Weak default function for board specific cleanup/preparation before
194  * Linux boot. Some boards/platforms might not need it, so just provide
195  * an empty stub here.
196  */
197 __weak void spl_board_prepare_for_linux(void)
198 {
199         /* Nothing to do! */
200 }
201
202 __weak void spl_board_prepare_for_optee(void *fdt)
203 {
204 }
205
206 __weak const char *spl_board_loader_name(u32 boot_device)
207 {
208         return NULL;
209 }
210
211 #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
212 __weak void __noreturn jump_to_image_optee(struct spl_image_info *spl_image)
213 {
214         spl_optee_entry(NULL, NULL, spl_image->fdt_addr,
215                         (void *)spl_image->entry_point);
216 }
217 #endif
218
219 __weak void spl_board_prepare_for_boot(void)
220 {
221         /* Nothing to do! */
222 }
223
224 __weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
225 {
226         return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
227 }
228
229 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
230 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
231 {
232         ulong u_boot_pos = spl_get_image_pos();
233
234 #if CONFIG_SYS_MONITOR_LEN != 0
235         spl_image->size = CONFIG_SYS_MONITOR_LEN;
236 #else
237         /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
238         spl_image->size = 200 * 1024;
239 #endif
240
241         /*
242          * Binman error cases: address of the end of the previous region or the
243          * start of the image's entry area (usually 0) if there is no previous
244          * region.
245          */
246         if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
247                 /* Binman does not support separated entry addresses */
248                 spl_image->entry_point = u_boot_pos;
249                 spl_image->load_addr = u_boot_pos;
250         } else {
251                 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
252                 spl_image->load_addr = CONFIG_TEXT_BASE;
253         }
254         spl_image->os = IH_OS_U_BOOT;
255         spl_image->name = "U-Boot";
256 }
257 #endif
258
259 #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
260 /* Parse and load full fitImage in SPL */
261 static int spl_load_fit_image(struct spl_image_info *spl_image,
262                               const struct legacy_img_hdr *header)
263 {
264         struct bootm_headers images;
265         const char *fit_uname_config = NULL;
266         uintptr_t fdt_hack;
267         const char *uname;
268         ulong fw_data = 0, dt_data = 0, img_data = 0;
269         ulong fw_len = 0, dt_len = 0, img_len = 0;
270         int idx, conf_noffset;
271         int ret;
272
273 #ifdef CONFIG_SPL_FIT_SIGNATURE
274         images.verify = 1;
275 #endif
276         ret = fit_image_load(&images, (ulong)header,
277                              NULL, &fit_uname_config,
278                              IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
279                              FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
280         if (ret >= 0) {
281                 printf("DEPRECATED: 'standalone = ' property.");
282                 printf("Please use either 'firmware =' or 'kernel ='\n");
283         } else {
284                 ret = fit_image_load(&images, (ulong)header, NULL,
285                                      &fit_uname_config, IH_ARCH_DEFAULT,
286                                      IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
287                                      &fw_data, &fw_len);
288         }
289
290         if (ret < 0) {
291                 ret = fit_image_load(&images, (ulong)header, NULL,
292                                      &fit_uname_config, IH_ARCH_DEFAULT,
293                                      IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
294                                      &fw_data, &fw_len);
295         }
296
297         if (ret < 0)
298                 return ret;
299
300         spl_image->size = fw_len;
301         spl_image->entry_point = fw_data;
302         spl_image->load_addr = fw_data;
303         if (fit_image_get_os(header, ret, &spl_image->os))
304                 spl_image->os = IH_OS_INVALID;
305         spl_image->name = genimg_get_os_name(spl_image->os);
306
307         debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
308               spl_image->name, spl_image->load_addr, spl_image->size);
309
310 #ifdef CONFIG_SPL_FIT_SIGNATURE
311         images.verify = 1;
312 #endif
313         ret = fit_image_load(&images, (ulong)header, NULL, &fit_uname_config,
314                        IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
315                        FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
316         if (ret >= 0) {
317                 spl_image->fdt_addr = (void *)dt_data;
318
319                 if (spl_image->os == IH_OS_U_BOOT) {
320                         /* HACK: U-Boot expects FDT at a specific address */
321                         fdt_hack = spl_image->load_addr + spl_image->size;
322                         fdt_hack = (fdt_hack + 3) & ~3;
323                         debug("Relocating FDT to %p\n", spl_image->fdt_addr);
324                         memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
325                 }
326         }
327
328         conf_noffset = fit_conf_get_node((const void *)header,
329                                          fit_uname_config);
330         if (conf_noffset < 0)
331                 return 0;
332
333         for (idx = 0;
334              uname = fdt_stringlist_get((const void *)header, conf_noffset,
335                                         FIT_LOADABLE_PROP, idx,
336                                 NULL), uname;
337              idx++)
338         {
339 #ifdef CONFIG_SPL_FIT_SIGNATURE
340                 images.verify = 1;
341 #endif
342                 ret = fit_image_load(&images, (ulong)header,
343                                      &uname, &fit_uname_config,
344                                      IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
345                                      FIT_LOAD_OPTIONAL_NON_ZERO,
346                                      &img_data, &img_len);
347                 if (ret < 0)
348                         return ret;
349         }
350
351         return 0;
352 }
353 #endif
354
355 __weak int spl_parse_board_header(struct spl_image_info *spl_image,
356                                   const struct spl_boot_device *bootdev,
357                                   const void *image_header, size_t size)
358 {
359         return -EINVAL;
360 }
361
362 __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
363                                    const struct legacy_img_hdr *header)
364 {
365         /* LEGACY image not supported */
366         debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
367         return -EINVAL;
368 }
369
370 int spl_parse_image_header(struct spl_image_info *spl_image,
371                            const struct spl_boot_device *bootdev,
372                            const struct legacy_img_hdr *header)
373 {
374 #if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
375         int ret = spl_load_fit_image(spl_image, header);
376
377         if (!ret)
378                 return ret;
379 #endif
380         if (image_get_magic(header) == IH_MAGIC) {
381                 int ret;
382
383                 ret = spl_parse_legacy_header(spl_image, header);
384                 if (ret)
385                         return ret;
386         } else {
387 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
388                 /*
389                  * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
390                  * code which loads images in SPL cannot guarantee that
391                  * absolutely all read errors will be reported.
392                  * An example is the LPC32XX MLC NAND driver, which
393                  * will consider that a completely unreadable NAND block
394                  * is bad, and thus should be skipped silently.
395                  */
396                 panic("** no mkimage signature but raw image not supported");
397 #endif
398
399 #if CONFIG_IS_ENABLED(OS_BOOT)
400 #if defined(CMD_BOOTI)
401                 ulong start, size;
402
403                 if (!booti_setup((ulong)header, &start, &size, 0)) {
404                         spl_image->name = "Linux";
405                         spl_image->os = IH_OS_LINUX;
406                         spl_image->load_addr = start;
407                         spl_image->entry_point = start;
408                         spl_image->size = size;
409                         debug(SPL_TPL_PROMPT
410                               "payload Image, load addr: 0x%lx size: %d\n",
411                               spl_image->load_addr, spl_image->size);
412                         return 0;
413                 }
414 #elif defined(CMD_BOOTZ)
415                 ulong start, end;
416
417                 if (!bootz_setup((ulong)header, &start, &end)) {
418                         spl_image->name = "Linux";
419                         spl_image->os = IH_OS_LINUX;
420                         spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
421                         spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
422                         spl_image->size = end - start;
423                         debug(SPL_TPL_PROMPT
424                               "payload zImage, load addr: 0x%lx size: %d\n",
425                               spl_image->load_addr, spl_image->size);
426                         return 0;
427                 }
428 #endif
429 #endif
430
431                 if (!spl_parse_board_header(spl_image, bootdev, (const void *)header, sizeof(*header)))
432                         return 0;
433
434 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
435                 /* Signature not found - assume u-boot.bin */
436                 debug("mkimage signature not found - ih_magic = %x\n",
437                         header->ih_magic);
438                 spl_set_header_raw_uboot(spl_image);
439 #else
440                 /* RAW image not supported, proceed to other boot methods. */
441                 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
442                 return -EINVAL;
443 #endif
444         }
445
446         return 0;
447 }
448
449 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
450 {
451         typedef void __noreturn (*image_entry_noargs_t)(void);
452
453         image_entry_noargs_t image_entry =
454                 (image_entry_noargs_t)spl_image->entry_point;
455
456         debug("image entry point: 0x%lx\n", spl_image->entry_point);
457         image_entry();
458 }
459
460 #if CONFIG_IS_ENABLED(HANDOFF)
461 /**
462  * Set up the SPL hand-off information
463  *
464  * This is initially empty (zero) but can be written by
465  */
466 static int setup_spl_handoff(void)
467 {
468         struct spl_handoff *ho;
469
470         ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
471         if (!ho)
472                 return -ENOENT;
473
474         return 0;
475 }
476
477 __weak int handoff_arch_save(struct spl_handoff *ho)
478 {
479         return 0;
480 }
481
482 static int write_spl_handoff(void)
483 {
484         struct spl_handoff *ho;
485         int ret;
486
487         ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
488         if (!ho)
489                 return -ENOENT;
490         handoff_save_dram(ho);
491         ret = handoff_arch_save(ho);
492         if (ret)
493                 return ret;
494         debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
495
496         return 0;
497 }
498 #else
499 static inline int setup_spl_handoff(void) { return 0; }
500 static inline int write_spl_handoff(void) { return 0; }
501
502 #endif /* HANDOFF */
503
504 /**
505  * get_bootstage_id() - Get the bootstage ID to emit
506  *
507  * @start: true if this is for starting SPL, false for ending it
508  * Return: bootstage ID to use
509  */
510 static enum bootstage_id get_bootstage_id(bool start)
511 {
512         enum u_boot_phase phase = spl_phase();
513
514         if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
515                 return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
516         else if (IS_ENABLED(CONFIG_VPL_BUILD) && phase == PHASE_VPL)
517                 return start ? BOOTSTAGE_ID_START_VPL : BOOTSTAGE_ID_END_VPL;
518         else
519                 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
520 }
521
522 static int spl_common_init(bool setup_malloc)
523 {
524         int ret;
525
526 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
527         if (setup_malloc) {
528 #ifdef CFG_MALLOC_F_ADDR
529                 gd->malloc_base = CFG_MALLOC_F_ADDR;
530 #endif
531                 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
532                 gd->malloc_ptr = 0;
533         }
534 #endif
535         ret = bootstage_init(u_boot_first_phase());
536         if (ret) {
537                 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
538                       ret);
539                 return ret;
540         }
541 #ifdef CONFIG_BOOTSTAGE_STASH
542         if (!u_boot_first_phase()) {
543                 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
544                                                CONFIG_BOOTSTAGE_STASH_SIZE);
545
546                 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
547                 if (ret)
548                         debug("%s: Failed to unstash bootstage: ret=%d\n",
549                               __func__, ret);
550         }
551 #endif /* CONFIG_BOOTSTAGE_STASH */
552         bootstage_mark_name(get_bootstage_id(true),
553                             spl_phase_name(spl_phase()));
554 #if CONFIG_IS_ENABLED(LOG)
555         ret = log_init();
556         if (ret) {
557                 debug("%s: Failed to set up logging\n", __func__);
558                 return ret;
559         }
560 #endif
561         if (CONFIG_IS_ENABLED(OF_REAL)) {
562                 ret = fdtdec_setup();
563                 if (ret) {
564                         debug("fdtdec_setup() returned error %d\n", ret);
565                         return ret;
566                 }
567         }
568         if (CONFIG_IS_ENABLED(DM)) {
569                 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
570                                 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
571                 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
572                 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
573                 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
574                 if (ret) {
575                         debug("dm_init_and_scan() returned error %d\n", ret);
576                         return ret;
577                 }
578         }
579
580         return 0;
581 }
582
583 void spl_set_bd(void)
584 {
585         /*
586          * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
587          * writeable.
588          */
589         if (!gd->bd)
590                 gd->bd = &bdata;
591 }
592
593 int spl_early_init(void)
594 {
595         int ret;
596
597         debug("%s\n", __func__);
598
599         ret = spl_common_init(true);
600         if (ret)
601                 return ret;
602         gd->flags |= GD_FLG_SPL_EARLY_INIT;
603
604         return 0;
605 }
606
607 int spl_init(void)
608 {
609         int ret;
610         bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
611                         IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
612
613         debug("%s\n", __func__);
614
615         if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
616                 ret = spl_common_init(setup_malloc);
617                 if (ret)
618                         return ret;
619         }
620         gd->flags |= GD_FLG_SPL_INIT;
621
622         return 0;
623 }
624
625 #ifndef BOOT_DEVICE_NONE
626 #define BOOT_DEVICE_NONE 0xdeadbeef
627 #endif
628
629 __weak void board_boot_order(u32 *spl_boot_list)
630 {
631         spl_boot_list[0] = spl_boot_device();
632 }
633
634 __weak int spl_check_board_image(struct spl_image_info *spl_image,
635                                  const struct spl_boot_device *bootdev)
636 {
637         return 0;
638 }
639
640 static int spl_load_image(struct spl_image_info *spl_image,
641                           struct spl_image_loader *loader)
642 {
643         int ret;
644         struct spl_boot_device bootdev;
645
646         bootdev.boot_device = loader->boot_device;
647         bootdev.boot_device_name = NULL;
648
649         ret = loader->load_image(spl_image, &bootdev);
650 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
651         if (!ret && spl_image->dcrc_length) {
652                 /* check data crc */
653                 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
654                                       spl_image->dcrc_length, CHUNKSZ_CRC32);
655                 if (dcrc != spl_image->dcrc) {
656                         puts("SPL: Image data CRC check failed!\n");
657                         ret = -EINVAL;
658                 }
659         }
660 #endif
661         if (!ret)
662                 ret = spl_check_board_image(spl_image, &bootdev);
663
664         return ret;
665 }
666
667 /**
668  * boot_from_devices() - Try loading a booting U-Boot from a list of devices
669  *
670  * @spl_image: Place to put the image details if successful
671  * @spl_boot_list: List of boot devices to try
672  * @count: Number of elements in spl_boot_list
673  * Return: 0 if OK, -ENODEV if there were no boot devices
674  *      if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
675  *      devices but none worked
676  */
677 static int boot_from_devices(struct spl_image_info *spl_image,
678                              u32 spl_boot_list[], int count)
679 {
680         struct spl_image_loader *drv =
681                 ll_entry_start(struct spl_image_loader, spl_image_loader);
682         const int n_ents =
683                 ll_entry_count(struct spl_image_loader, spl_image_loader);
684         int ret = -ENODEV;
685         int i;
686
687         for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
688                 struct spl_image_loader *loader;
689                 int bootdev = spl_boot_list[i];
690
691                 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
692                         ret = -ENXIO;
693                 for (loader = drv; loader != drv + n_ents; loader++) {
694                         if (bootdev != loader->boot_device)
695                                 continue;
696                         if (!CONFIG_IS_ENABLED(SILENT_CONSOLE)) {
697                                 if (loader)
698                                         printf("Trying to boot from %s\n",
699                                                spl_loader_name(loader));
700                                 else if (CONFIG_IS_ENABLED(SHOW_ERRORS)) {
701                                         printf(SPL_TPL_PROMPT
702                                                "Unsupported Boot Device %d\n",
703                                                bootdev);
704                                 } else {
705                                         puts(SPL_TPL_PROMPT
706                                              "Unsupported Boot Device!\n");
707                                 }
708                         }
709                         if (loader &&
710                                 !spl_load_image(spl_image, loader)) {
711                                 spl_image->boot_device = bootdev;
712                                 return 0;
713                         }
714                 }
715         }
716
717         return ret;
718 }
719
720 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
721 void board_init_f(ulong dummy)
722 {
723         if (CONFIG_IS_ENABLED(OF_CONTROL)) {
724                 int ret;
725
726                 ret = spl_early_init();
727                 if (ret) {
728                         debug("spl_early_init() failed: %d\n", ret);
729                         hang();
730                 }
731         }
732
733         preloader_console_init();
734 }
735 #endif
736
737 void board_init_r(gd_t *dummy1, ulong dummy2)
738 {
739         u32 spl_boot_list[] = {
740                 BOOT_DEVICE_NONE,
741                 BOOT_DEVICE_NONE,
742                 BOOT_DEVICE_NONE,
743                 BOOT_DEVICE_NONE,
744                 BOOT_DEVICE_NONE,
745         };
746         struct spl_image_info spl_image;
747         int ret;
748
749         debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
750
751         spl_set_bd();
752
753 #if defined(CONFIG_SPL_SYS_MALLOC)
754         mem_malloc_init(SPL_SYS_MALLOC_START, CONFIG_SPL_SYS_MALLOC_SIZE);
755         gd->flags |= GD_FLG_FULL_MALLOC_INIT;
756 #endif
757         if (!(gd->flags & GD_FLG_SPL_INIT)) {
758                 if (spl_init())
759                         hang();
760         }
761 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
762         /*
763          * timer_init() does not exist on PPC systems. The timer is initialized
764          * and enabled (decrementer) in interrupt_init() here.
765          */
766         timer_init();
767 #endif
768         if (CONFIG_IS_ENABLED(BLOBLIST)) {
769                 ret = bloblist_init();
770                 if (ret) {
771                         debug("%s: Failed to set up bloblist: ret=%d\n",
772                               __func__, ret);
773                         puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
774                         hang();
775                 }
776         }
777         if (CONFIG_IS_ENABLED(HANDOFF)) {
778                 int ret;
779
780                 ret = setup_spl_handoff();
781                 if (ret) {
782                         puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
783                         hang();
784                 }
785         }
786
787 #if CONFIG_IS_ENABLED(BOARD_INIT)
788         spl_board_init();
789 #endif
790
791 #if defined(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT)
792         initr_watchdog();
793 #endif
794
795         if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
796             IS_ENABLED(CONFIG_SPL_ATF))
797                 dram_init_banksize();
798
799         if (CONFIG_IS_ENABLED(PCI) && !(gd->flags & GD_FLG_DM_DEAD)) {
800                 ret = pci_init();
801                 if (ret)
802                         puts(SPL_TPL_PROMPT "Cannot initialize PCI\n");
803                 /* Don't fail. We still can try other boot methods. */
804         }
805
806         bootcount_inc();
807
808         /* Dump driver model states to aid analysis */
809         if (CONFIG_IS_ENABLED(DM_STATS)) {
810                 struct dm_stats mem;
811
812                 dm_get_mem(&mem);
813                 dm_dump_mem(&mem);
814         }
815
816         memset(&spl_image, '\0', sizeof(spl_image));
817 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
818         spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
819 #endif
820         spl_image.boot_device = BOOT_DEVICE_NONE;
821         board_boot_order(spl_boot_list);
822
823         ret = boot_from_devices(&spl_image, spl_boot_list,
824                                 ARRAY_SIZE(spl_boot_list));
825         if (ret) {
826                 if (CONFIG_IS_ENABLED(SHOW_ERRORS) &&
827                     CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT))
828                         printf(SPL_TPL_PROMPT "failed to boot from all boot devices (err=%d)\n",
829                                ret);
830                 else
831                         puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
832                 hang();
833         }
834
835         spl_perform_fixups(&spl_image);
836         if (CONFIG_IS_ENABLED(HANDOFF)) {
837                 ret = write_spl_handoff();
838                 if (ret)
839                         printf(SPL_TPL_PROMPT
840                                "SPL hand-off write failed (err=%d)\n", ret);
841         }
842         if (CONFIG_IS_ENABLED(BLOBLIST)) {
843                 ret = bloblist_finish();
844                 if (ret)
845                         printf("Warning: Failed to finish bloblist (ret=%d)\n",
846                                ret);
847         }
848
849         switch (spl_image.os) {
850         case IH_OS_U_BOOT:
851                 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
852                 break;
853 #if CONFIG_IS_ENABLED(ATF)
854         case IH_OS_ARM_TRUSTED_FIRMWARE:
855                 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
856                 spl_fixup_fdt(spl_image.fdt_addr);
857                 spl_invoke_atf(&spl_image);
858                 break;
859 #endif
860 #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
861         case IH_OS_TEE:
862                 debug("Jumping to U-Boot via OP-TEE\n");
863                 spl_board_prepare_for_optee(spl_image.fdt_addr);
864                 jump_to_image_optee(&spl_image);
865                 break;
866 #endif
867 #if CONFIG_IS_ENABLED(OPENSBI)
868         case IH_OS_OPENSBI:
869                 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
870                 spl_invoke_opensbi(&spl_image);
871                 break;
872 #endif
873 #if CONFIG_IS_ENABLED(OS_BOOT)
874         case IH_OS_LINUX:
875                 debug("Jumping to Linux\n");
876 #if defined(CONFIG_SYS_SPL_ARGS_ADDR)
877                 spl_fixup_fdt((void *)CONFIG_SYS_SPL_ARGS_ADDR);
878 #endif
879                 spl_board_prepare_for_linux();
880                 jump_to_image_linux(&spl_image);
881 #endif
882         default:
883                 debug("Unsupported OS image.. Jumping nevertheless..\n");
884         }
885 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SPL_SYS_MALLOC_SIZE)
886         debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
887               gd->malloc_ptr / 1024);
888 #endif
889         bootstage_mark_name(get_bootstage_id(false), "end phase");
890 #ifdef CONFIG_BOOTSTAGE_STASH
891         ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
892                               CONFIG_BOOTSTAGE_STASH_SIZE);
893         if (ret)
894                 debug("Failed to stash bootstage: err=%d\n", ret);
895 #endif
896
897         if (IS_ENABLED(CONFIG_SPL_VIDEO_REMOVE)) {
898                 struct udevice *dev;
899                 int rc;
900
901                 rc = uclass_find_device(UCLASS_VIDEO, 0, &dev);
902                 if (!rc && dev) {
903                         rc = device_remove(dev, DM_REMOVE_NORMAL);
904                         if (rc)
905                                 printf("Cannot remove video device '%s' (err=%d)\n",
906                                        dev->name, rc);
907                 }
908         }
909
910         spl_board_prepare_for_boot();
911         jump_to_image_no_args(&spl_image);
912 }
913
914 /*
915  * This requires UART clocks to be enabled.  In order for this to work the
916  * caller must ensure that the gd pointer is valid.
917  */
918 void preloader_console_init(void)
919 {
920 #ifdef CONFIG_SPL_SERIAL
921         gd->baudrate = CONFIG_BAUDRATE;
922
923         serial_init();          /* serial communications setup */
924
925         gd->have_console = 1;
926
927 #if CONFIG_IS_ENABLED(BANNER_PRINT)
928         puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
929              U_BOOT_TIME " " U_BOOT_TZ ")\n");
930 #endif
931 #ifdef CONFIG_SPL_DISPLAY_PRINT
932         spl_display_print();
933 #endif
934 #endif
935 }
936
937 /**
938  * This function is called before the stack is changed from initial stack to
939  * relocated stack. It tries to dump the stack size used
940  */
941 __weak void spl_relocate_stack_check(void)
942 {
943 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
944         ulong init_sp = gd->start_addr_sp;
945         ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
946         u8 *ptr = (u8 *)stack_bottom;
947         ulong i;
948
949         for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
950                 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
951                         break;
952                 ptr++;
953         }
954         printf("SPL initial stack usage: %lu bytes\n",
955                CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
956 #endif
957 }
958
959 /**
960  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
961  *
962  * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
963  * for the main board_init_r() execution. This is typically because we need
964  * more stack space for things like the MMC sub-system.
965  *
966  * This function calculates the stack position, copies the global_data into
967  * place, sets the new gd (except for ARM, for which setting GD within a C
968  * function may not always work) and returns the new stack position. The
969  * caller is responsible for setting up the sp register and, in the case
970  * of ARM, setting up gd.
971  *
972  * All of this is done using the same layout and alignments as done in
973  * board_init_f_init_reserve() / board_init_f_alloc_reserve().
974  *
975  * Return: new stack location, or 0 to use the same stack
976  */
977 ulong spl_relocate_stack_gd(void)
978 {
979 #ifdef CONFIG_SPL_STACK_R
980         gd_t *new_gd;
981         ulong ptr = CONFIG_SPL_STACK_R_ADDR;
982
983         if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
984                 spl_relocate_stack_check();
985
986 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
987         if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
988                 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
989                       gd->malloc_ptr, gd->malloc_ptr / 1024);
990                 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
991                 gd->malloc_base = ptr;
992                 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
993                 gd->malloc_ptr = 0;
994         }
995 #endif
996         /* Get stack position: use 8-byte alignment for ABI compliance */
997         ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
998         gd->start_addr_sp = ptr;
999         new_gd = (gd_t *)ptr;
1000         memcpy(new_gd, (void *)gd, sizeof(gd_t));
1001 #if CONFIG_IS_ENABLED(DM)
1002         dm_fixup_for_gd_move(new_gd);
1003 #endif
1004 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
1005         gd = new_gd;
1006 #endif
1007         return ptr;
1008 #else
1009         return 0;
1010 #endif
1011 }
1012
1013 #if defined(CONFIG_BOOTCOUNT_LIMIT) && \
1014         ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
1015          (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
1016 void bootcount_store(ulong a)
1017 {
1018 }
1019
1020 ulong bootcount_load(void)
1021 {
1022         return 0;
1023 }
1024 #endif