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