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