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