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