]> Git Repo - u-boot.git/blame - 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
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
bcae7211
A
2/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <[email protected]>
bcae7211 7 */
f1c6e192 8
03de305e 9#include <config.h>
e945a726 10#include <bloblist.h>
8bee2d25 11#include <binman_sym.h>
52f24238 12#include <bootstage.h>
11516518 13#include <dm.h>
b0edea3c 14#include <handoff.h>
db41d65a 15#include <hang.h>
691d719d 16#include <init.h>
c30b7adb 17#include <irq_func.h>
f7ae49fc 18#include <log.h>
891d9e84 19#include <mapmem.h>
b03e0510 20#include <serial.h>
47f7bcae 21#include <spl.h>
77507416 22#include <spl_load.h>
10f6e4dc 23#include <system-constants.h>
401d1c4f 24#include <asm/global_data.h>
1d99e673 25#include <asm-generic/gpio.h>
bb085b87 26#include <nand.h>
8cf686e1 27#include <fat.h>
3db71108 28#include <u-boot/crc.h>
121a165c
T
29#if CONFIG_IS_ENABLED(BANNER_PRINT)
30#include <timestamp.h>
31#endif
efb2172e 32#include <version.h>
8cf686e1 33#include <image.h>
2d01dd95 34#include <malloc.h>
31f9f0ea 35#include <mapmem.h>
11516518 36#include <dm/root.h>
4f6500aa 37#include <dm/util.h>
7a17e4c6
NJ
38#include <dm/device-internal.h>
39#include <dm/uclass-internal.h>
761ca31e 40#include <linux/compiler.h>
6e7585bb 41#include <fdt_support.h>
a8be2494 42#include <bootcount.h>
06985289 43#include <wdt.h>
1d3c2667 44#include <video.h>
bcae7211
A
45
46DECLARE_GLOBAL_DATA_PTR;
367ecbf2 47DECLARE_BINMAN_MAGIC_SYM;
bcae7211 48
47f7bcae 49u32 *boot_params_ptr = NULL;
9ea5c6ef 50
d8830cf8 51#if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
8bee2d25 52/* See spl.h for information about this */
dbf6be9f 53binman_sym_declare(ulong, u_boot_any, image_pos);
e82c624d
SG
54binman_sym_declare(ulong, u_boot_any, size);
55
56#ifdef CONFIG_TPL
2b8d2ccd
SG
57binman_sym_declare(ulong, u_boot_spl_any, image_pos);
58binman_sym_declare(ulong, u_boot_spl_any, size);
e82c624d 59#endif
8bee2d25 60
f86ca5ad 61#ifdef CONFIG_VPL
2b8d2ccd
SG
62binman_sym_declare(ulong, u_boot_vpl_any, image_pos);
63binman_sym_declare(ulong, u_boot_vpl_any, size);
f86ca5ad
SG
64#endif
65
d8830cf8
ANY
66#endif /* BINMAN_UBOOT_SYMBOLS */
67
58b504e5
AG
68/* Define board data structure */
69static struct bd_info bdata __attribute__ ((section(".data")));
70
b55881dd 71#if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
496c5483
HS
72/*
73 * Board-specific Platform code can reimplement show_boot_progress () if needed
74 */
75__weak void show_boot_progress(int val) {}
cb80ff20 76#endif
496c5483 77
a343b4fe
HS
78#if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
79 defined(CONFIG_SPL_ATF)
b0edea3c
SG
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
379c19ab
SS
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 */
7115007c 97#if CONFIG_IS_ENABLED(OS_BOOT)
379c19ab
SS
98__weak int spl_start_uboot(void)
99{
d6330064
SG
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");
379c19ab
SS
103 return 1;
104}
431889d6
LM
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 */
110int __weak bootz_setup(ulong image, ulong *start, ulong *end)
111{
112 return 1;
113}
7a0d8807
NBM
114
115int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
116{
117 return 1;
118}
379c19ab
SS
119#endif
120
de5dd4c4
PT
121/* Weak default function for arch/board-specific fixups to the spl_image_info */
122void __weak spl_perform_fixups(struct spl_image_info *spl_image)
123{
124}
125
a343b4fe 126void spl_fixup_fdt(void *fdt_blob)
6e7585bb 127{
a343b4fe 128#if defined(CONFIG_SPL_OF_LIBFDT)
6e7585bb
R
129 int err;
130
a343b4fe
HS
131 if (!fdt_blob)
132 return;
133
6e7585bb
R
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) {
d6330064 143 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
6e7585bb
R
144 return;
145 }
146
147 err = arch_fixup_fdt(fdt_blob);
148 if (err) {
d6330064 149 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
6e7585bb
R
150 return;
151 }
152#endif
153}
154
1d3c2667
DT
155int 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
e82c624d
SG
173ulong spl_get_image_pos(void)
174{
d8830cf8 175 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
d7f07172
ANY
176 return BINMAN_SYM_MISSING;
177
f86ca5ad
SG
178#ifdef CONFIG_VPL
179 if (spl_next_phase() == PHASE_VPL)
2b8d2ccd 180 return binman_sym(ulong, u_boot_vpl_any, image_pos);
f86ca5ad
SG
181#endif
182 return spl_next_phase() == PHASE_SPL ?
2b8d2ccd 183 binman_sym(ulong, u_boot_spl_any, image_pos) :
e82c624d
SG
184 binman_sym(ulong, u_boot_any, image_pos);
185}
186
187ulong spl_get_image_size(void)
188{
d8830cf8 189 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
d7f07172
ANY
190 return BINMAN_SYM_MISSING;
191
f86ca5ad
SG
192#ifdef CONFIG_VPL
193 if (spl_next_phase() == PHASE_VPL)
2b8d2ccd 194 return binman_sym(ulong, u_boot_vpl_any, size);
f86ca5ad
SG
195#endif
196 return spl_next_phase() == PHASE_SPL ?
2b8d2ccd 197 binman_sym(ulong, u_boot_spl_any, size) :
e82c624d
SG
198 binman_sym(ulong, u_boot_any, size);
199}
200
86c372af
SG
201ulong spl_get_image_text_base(void)
202{
f86ca5ad
SG
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 :
98463903 208 CONFIG_TEXT_BASE;
86c372af
SG
209}
210
ea8256f0
SR
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
a25d6b65
AG
221__weak void spl_board_prepare_for_optee(void *fdt)
222{
223}
224
c5922923
HT
225__weak const char *spl_board_loader_name(u32 boot_device)
226{
227 return NULL;
228}
229
949eb228
RS
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
3a3b9147
MS
238__weak void spl_board_prepare_for_boot(void)
239{
240 /* Nothing to do! */
241}
242
f3543e69 243__weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
04ce5427 244{
98463903 245 return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
04ce5427
MV
246}
247
d95ceb97 248void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
0c3117b1 249{
d7f07172 250 ulong u_boot_pos = spl_get_image_pos();
8bee2d25 251
08574ed3 252#if CONFIG_SYS_MONITOR_LEN != 0
d95ceb97 253 spl_image->size = CONFIG_SYS_MONITOR_LEN;
08574ed3
TR
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
55fe0e2b
MR
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 */
8bee2d25
SG
266 spl_image->entry_point = u_boot_pos;
267 spl_image->load_addr = u_boot_pos;
268 } else {
6ab77bb1 269 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
98463903 270 spl_image->load_addr = CONFIG_TEXT_BASE;
8bee2d25 271 }
d95ceb97
SG
272 spl_image->os = IH_OS_U_BOOT;
273 spl_image->name = "U-Boot";
0c3117b1
HS
274}
275
9baab60b 276__weak int spl_parse_board_header(struct spl_image_info *spl_image,
2e0429bc 277 const struct spl_boot_device *bootdev,
9baab60b
T
278 const void *image_header, size_t size)
279{
280 return -EINVAL;
281}
282
c1108172 283__weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
f3543e69 284 const struct legacy_img_hdr *header)
c1108172
SR
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
71316c1d 291int spl_parse_image_header(struct spl_image_info *spl_image,
2e0429bc 292 const struct spl_boot_device *bootdev,
f3543e69 293 const struct legacy_img_hdr *header)
8cf686e1 294{
035ab46e 295 int ret;
8a9dc16e 296
035ab46e
SG
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 }
77552b06 303 if (image_get_magic(header) == IH_MAGIC) {
c1108172 304 int ret;
dae5c2dc 305
c1108172
SR
306 ret = spl_parse_legacy_header(spl_image, header);
307 if (ret)
308 return ret;
af4ff286
SG
309 return 0;
310 }
311
312 if (IS_ENABLED(CONFIG_SPL_PANIC_ON_RAW_IMAGE)) {
8c80eb3b
AA
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");
af4ff286 322 }
85a37729 323
af4ff286 324 if (CONFIG_IS_ENABLED(OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI)) {
7a0d8807
NBM
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 }
af4ff286 338 } else if (CONFIG_IS_ENABLED(OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTZ)) {
431889d6
LM
339 ulong start, end;
340
341 if (!bootz_setup((ulong)header, &start, &end)) {
71316c1d
SG
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;
d6330064
SG
347 debug(SPL_TPL_PROMPT
348 "payload zImage, load addr: 0x%lx size: %d\n",
71316c1d 349 spl_image->load_addr, spl_image->size);
431889d6
LM
350 return 0;
351 }
af4ff286 352 }
85a37729 353
af4ff286
SG
354 if (!spl_parse_board_header(spl_image, bootdev, (const void *)header,
355 sizeof(*header)))
356 return 0;
9baab60b 357
bf85af66 358 if (IS_ENABLED(CONFIG_SPL_RAW_IMAGE_SUPPORT)) {
8cf686e1 359 /* Signature not found - assume u-boot.bin */
026b2fe3 360 debug("mkimage signature not found - ih_magic = %x\n",
bf85af66 361 header->ih_magic);
71316c1d 362 spl_set_header_raw_uboot(spl_image);
bf85af66 363 } else {
24eb39b5 364 /* RAW image not supported, proceed to other boot methods. */
2d2531be 365 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
24eb39b5 366 return -EINVAL;
bf85af66 367 }
24eb39b5 368
7e0f2267 369 return 0;
8cf686e1
A
370}
371
77507416
SA
372#if SPL_LOAD_USERS > 1
373int 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
a759f1e0 381__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
8cf686e1 382{
4a0eb757
S
383 typedef void __noreturn (*image_entry_noargs_t)(void);
384
8cf686e1 385 image_entry_noargs_t image_entry =
11e1479b 386 (image_entry_noargs_t)spl_image->entry_point;
8cf686e1 387
30c0740e 388 debug("image entry point: 0x%lx\n", spl_image->entry_point);
4a0eb757 389 image_entry();
8cf686e1
A
390}
391
b0edea3c
SG
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 */
398static int setup_spl_handoff(void)
399{
400 struct spl_handoff *ho;
401
7f3b79af 402 ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
b0edea3c
SG
403 if (!ho)
404 return -ENOENT;
405
406 return 0;
407}
408
366291af
SG
409__weak int handoff_arch_save(struct spl_handoff *ho)
410{
411 return 0;
412}
413
b0edea3c
SG
414static int write_spl_handoff(void)
415{
416 struct spl_handoff *ho;
366291af 417 int ret;
b0edea3c 418
7f3b79af 419 ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
b0edea3c
SG
420 if (!ho)
421 return -ENOENT;
422 handoff_save_dram(ho);
366291af
SG
423 ret = handoff_arch_save(ho);
424 if (ret)
425 return ret;
b0edea3c
SG
426 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
427
428 return 0;
429}
430#else
431static inline int setup_spl_handoff(void) { return 0; }
432static inline int write_spl_handoff(void) { return 0; }
433
434#endif /* HANDOFF */
435
05e3a0d6
SG
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
185f812c 440 * Return: bootstage ID to use
05e3a0d6
SG
441 */
442static 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;
f86ca5ad
SG
448 else if (IS_ENABLED(CONFIG_VPL_BUILD) && phase == PHASE_VPL)
449 return start ? BOOTSTAGE_ID_START_VPL : BOOTSTAGE_ID_END_VPL;
05e3a0d6
SG
450 else
451 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
452}
453
340f418a 454static int spl_common_init(bool setup_malloc)
bcae7211 455{
f3d46bd6
SG
456 int ret;
457
3d6d5075 458#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
340f418a 459 if (setup_malloc) {
dd5b58c4
TR
460#ifdef CFG_MALLOC_F_ADDR
461 gd->malloc_base = CFG_MALLOC_F_ADDR;
94b9e22e 462#endif
f1896c45 463 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
340f418a
EC
464 gd->malloc_ptr = 0;
465 }
24dafad5 466#endif
31f9f0ea 467 ret = bootstage_init(u_boot_first_phase());
824bb1b4
SG
468 if (ret) {
469 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
470 ret);
471 return ret;
472 }
31f9f0ea 473 if (!u_boot_first_phase()) {
17ba5010 474 ret = bootstage_unstash_default();
31f9f0ea 475 if (ret)
17ba5010 476 log_debug("Failed to unstash bootstage: ret=%d\n", ret);
31f9f0ea 477 }
05e3a0d6
SG
478 bootstage_mark_name(get_bootstage_id(true),
479 spl_phase_name(spl_phase()));
4d8d3056
SG
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
414cc151 487 if (CONFIG_IS_ENABLED(OF_REAL)) {
f3d46bd6
SG
488 ret = fdtdec_setup();
489 if (ret) {
490 debug("fdtdec_setup() returned error %d\n", ret);
070d00b8 491 return ret;
f3d46bd6
SG
492 }
493 }
f1c6e192 494 if (CONFIG_IS_ENABLED(DM)) {
b67eefdb 495 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
5256beec 496 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
7f73ca48 497 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
7d23b9cf 498 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
b67eefdb 499 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
f3d46bd6
SG
500 if (ret) {
501 debug("dm_init_and_scan() returned error %d\n", ret);
070d00b8 502 return ret;
f3d46bd6
SG
503 }
504 }
340f418a
EC
505
506 return 0;
507}
508
58b504e5 509void spl_set_bd(void)
d1fc0a31 510{
c21f407b
SG
511 /*
512 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
513 * writeable.
514 */
58b504e5
AG
515 if (!gd->bd)
516 gd->bd = &bdata;
d1fc0a31
YS
517}
518
340f418a
EC
519int spl_early_init(void)
520{
521 int ret;
522
94cb986e
SG
523 debug("%s\n", __func__);
524
340f418a
EC
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
533int spl_init(void)
534{
535 int ret;
cf334edf
TR
536 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
537 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
340f418a 538
94cb986e
SG
539 debug("%s\n", __func__);
540
340f418a 541 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
cf334edf 542 ret = spl_common_init(setup_malloc);
340f418a
EC
543 if (ret)
544 return ret;
545 }
070d00b8 546 gd->flags |= GD_FLG_SPL_INIT;
2d01dd95 547
070d00b8
SG
548 return 0;
549}
550
f101e4bd
NK
551#ifndef BOOT_DEVICE_NONE
552#define BOOT_DEVICE_NONE 0xdeadbeef
553#endif
554
f101e4bd
NK
555__weak void board_boot_order(u32 *spl_boot_list)
556{
557 spl_boot_list[0] = spl_boot_device();
558}
559
c10939d8
T
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
29d357d7
SG
566static int spl_load_image(struct spl_image_info *spl_image,
567 struct spl_image_loader *loader)
070d00b8 568{
dae5c2dc 569 int ret;
ecdfd69a
SG
570 struct spl_boot_device bootdev;
571
29d357d7 572 bootdev.boot_device = loader->boot_device;
ecdfd69a
SG
573 bootdev.boot_device_name = NULL;
574
dae5c2dc
SG
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
c10939d8
T
587 if (!ret)
588 ret = spl_check_board_image(spl_image, &bootdev);
589
dae5c2dc 590 return ret;
540bfe7d
SG
591}
592
593/**
fd4ee98d 594 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
540bfe7d
SG
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
185f812c 599 * Return: 0 if OK, -ENODEV if there were no boot devices
7d84fbb5
SG
600 * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
601 * devices but none worked
540bfe7d
SG
602 */
603static int boot_from_devices(struct spl_image_info *spl_image,
604 u32 spl_boot_list[], int count)
605{
5a61bf17
SG
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);
7d84fbb5 610 int ret = -ENODEV;
540bfe7d
SG
611 int i;
612
613 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
614 struct spl_image_loader *loader;
7d84fbb5
SG
615 int bootdev = spl_boot_list[i];
616
617 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
618 ret = -ENXIO;
5a61bf17
SG
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 }
de5dd4c4 640 }
540bfe7d
SG
641 }
642
7d84fbb5 643 return ret;
5211b87e
NK
644}
645
a9a3aada
PR
646#if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
647void 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
3988be5f 659 preloader_console_init();
a9a3aada
PR
660}
661#endif
662
5211b87e
NK
663void board_init_r(gd_t *dummy1, ulong dummy2)
664{
d32b2d1c
SG
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 };
6826c432
JK
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;
d32b2d1c 674 struct spl_image_info spl_image;
f817e08f 675 int ret, os;
5211b87e 676
d6330064 677 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
d1fc0a31 678
58b504e5
AG
679 spl_set_bd();
680
52779874 681 if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC)) {
b02c4e94
SA
682 mem_malloc_init((ulong)map_sysmem(SPL_SYS_MALLOC_START,
683 SPL_SYS_MALLOC_SIZE),
684 SPL_SYS_MALLOC_SIZE);
52779874
SG
685 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
686 }
5211b87e
NK
687 if (!(gd->flags & GD_FLG_SPL_INIT)) {
688 if (spl_init())
689 hang();
690 }
5211b87e 691 timer_init();
3cd71981
SG
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 }
5211b87e 710
52ef6a9c
LF
711 if (CONFIG_IS_ENABLED(SOC_INIT))
712 spl_soc_init();
713
6371c479
SG
714 if (CONFIG_IS_ENABLED(BOARD_INIT))
715 spl_board_init();
5211b87e 716
6371c479
SG
717 if (IS_ENABLED(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT))
718 initr_watchdog();
06985289 719
a343b4fe 720 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
62b096d9 721 IS_ENABLED(CONFIG_SPL_ATF) || IS_ENABLED(CONFIG_SPL_NET))
b0edea3c
SG
722 dram_init_banksize();
723
15a23b6f 724 if (CONFIG_IS_ENABLED(PCI) && !(gd->flags & GD_FLG_DM_DEAD)) {
7d4c8cfe
HS
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
a8be2494
LM
731 bootcount_inc();
732
4f6500aa
SG
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
d32b2d1c 741 memset(&spl_image, '\0', sizeof(spl_image));
e0be6eaf
SG
742 if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
743 spl_image.arg = (void *)SPL_PAYLOAD_ARGS_ADDR;
de5dd4c4 744 spl_image.boot_device = BOOT_DEVICE_NONE;
f101e4bd 745 board_boot_order(spl_boot_list);
f101e4bd 746
7d84fbb5
SG
747 ret = boot_from_devices(&spl_image, spl_boot_list,
748 ARRAY_SIZE(spl_boot_list));
749 if (ret) {
06b1bca6 750 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
7d84fbb5
SG
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");
5211b87e 755 hang();
f101e4bd 756 }
5211b87e 757
de5dd4c4
PT
758 spl_perform_fixups(&spl_image);
759
f817e08f
SG
760 os = spl_image.os;
761 if (os == IH_OS_U_BOOT) {
1a9e75bd 762 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
f817e08f 763 } else if (CONFIG_IS_ENABLED(ATF) && os == IH_OS_ARM_TRUSTED_FIRMWARE) {
1d379090 764 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
f817e08f 765 spl_fixup_fdt(spl_image_fdt_addr(&spl_image));
6826c432 766 jump_to_image = &spl_invoke_atf;
f817e08f 767 } else if (CONFIG_IS_ENABLED(OPTEE_IMAGE) && os == IH_OS_TEE) {
70fe2876 768 debug("Jumping to U-Boot via OP-TEE\n");
f817e08f 769 spl_board_prepare_for_optee(spl_image_fdt_addr(&spl_image));
6826c432 770 jump_to_image = &jump_to_image_optee;
f817e08f 771 } else if (CONFIG_IS_ENABLED(OPENSBI) && os == IH_OS_OPENSBI) {
5e30e45c 772 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
6826c432 773 jump_to_image = &spl_invoke_opensbi;
f817e08f 774 } else if (CONFIG_IS_ENABLED(OS_BOOT) && os == IH_OS_LINUX) {
379c19ab 775 debug("Jumping to Linux\n");
e0be6eaf
SG
776 if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
777 spl_fixup_fdt((void *)SPL_PAYLOAD_ARGS_ADDR);
379c19ab 778 spl_board_prepare_for_linux();
6826c432 779 jump_to_image = &jump_to_image_linux;
f817e08f 780 } else {
42120981 781 debug("Unsupported OS image.. Jumping nevertheless..\n");
8cf686e1 782 }
2003a83c
SG
783 if (CONFIG_IS_ENABLED(SYS_MALLOC_F) &&
784 !IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIZE))
92aa3ec3 785 debug("SPL malloc() used 0x%x bytes (%d KB)\n",
2003a83c
SG
786 gd_malloc_ptr(), gd_malloc_ptr() / 1024);
787
05e3a0d6 788 bootstage_mark_name(get_bootstage_id(false), "end phase");
17ba5010 789 ret = bootstage_stash_default();
824bb1b4
SG
790 if (ret)
791 debug("Failed to stash bootstage: err=%d\n", ret);
a8c5112a 792
954b0ad4
NJ
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 }
7a17e4c6 804 }
a17e1e76
SG
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 }
ec2186ac
SG
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 }
a17e1e76
SG
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 }
7a17e4c6 825
3a3b9147 826 spl_board_prepare_for_boot();
6826c432 827 jump_to_image(&spl_image);
bcae7211
A
828}
829
6507f133
TR
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 */
bcae7211
A
834void preloader_console_init(void)
835{
2a736066 836#ifdef CONFIG_SPL_SERIAL
bcae7211
A
837 gd->baudrate = CONFIG_BAUDRATE;
838
bcae7211
A
839 serial_init(); /* serial communications setup */
840
f44fded2 841 gd->flags |= GD_FLG_HAVE_CONSOLE;
b88befa5 842
aedc08b2 843#if CONFIG_IS_ENABLED(BANNER_PRINT)
d6330064
SG
844 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
845 U_BOOT_TIME " " U_BOOT_TZ ")\n");
0292bc0d 846#endif
861a86f4
TR
847#ifdef CONFIG_SPL_DISPLAY_PRINT
848 spl_display_print();
849#endif
117a0e02 850#endif
3988be5f 851}
db910353 852
d8c03320
SG
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
db910353
SG
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
adc421e4
AA
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().
db910353 890 *
185f812c 891 * Return: new stack location, or 0 to use the same stack
db910353
SG
892 */
893ulong spl_relocate_stack_gd(void)
894{
895#ifdef CONFIG_SPL_STACK_R
896 gd_t *new_gd;
adc421e4 897 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
db910353 898
d8c03320
SG
899 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
900 spl_relocate_stack_check();
901
3d6d5075 902#if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_IS_ENABLED(SYS_MALLOC_F)
dcfcb8d4 903 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
92aa3ec3 904 debug("SPL malloc() before relocation used 0x%x bytes (%d KB)\n",
438dcabb 905 gd->malloc_ptr, gd->malloc_ptr / 1024);
dcfcb8d4
HG
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
adc421e4
AA
912 /* Get stack position: use 8-byte alignment for ABI compliance */
913 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
149fb05b 914 gd->start_addr_sp = ptr;
adc421e4
AA
915 new_gd = (gd_t *)ptr;
916 memcpy(new_gd, (void *)gd, sizeof(gd_t));
2f11cd91
SG
917#if CONFIG_IS_ENABLED(DM)
918 dm_fixup_for_gd_move(new_gd);
919#endif
39162d93
TW
920#if CONFIG_IS_ENABLED(LOG)
921 log_fixup_for_gd_move(new_gd);
922#endif
c7e1effb 923#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
adc421e4
AA
924 gd = new_gd;
925#endif
db910353
SG
926 return ptr;
927#else
928 return 0;
929#endif
930}
c6604441 931
4d145f26
PR
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)))
c6604441
SG
935void bootcount_store(ulong a)
936{
937}
938
939ulong bootcount_load(void)
940{
941 return 0;
942}
943#endif
This page took 0.553648 seconds and 4 git commands to generate.