blob: 9b0276cbdb5870d63b2a516f5f929f3a68d78b71 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Aneesh Vbcae7212011-07-21 09:10:21 -04002/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <aneesh@ti.com>
Aneesh Vbcae7212011-07-21 09:10:21 -04007 */
Philipp Tomsichf1c6e192017-06-30 19:02:53 +02008
Aneesh Vbcae7212011-07-21 09:10:21 -04009#include <common.h>
Simon Glasse945a722018-11-15 18:43:51 -070010#include <bloblist.h>
Simon Glass8bee2d22017-11-13 18:55:03 -070011#include <binman_sym.h>
Simon Glass52f24232020-05-10 11:40:00 -060012#include <bootstage.h>
Simon Glass11516512014-11-10 17:16:46 -070013#include <dm.h>
Simon Glassb0edea32018-11-15 18:44:09 -070014#include <handoff.h>
Simon Glassdb41d652019-12-28 10:45:07 -070015#include <hang.h>
Simon Glass691d7192020-05-10 11:40:02 -060016#include <init.h>
Simon Glassc30b7ad2019-11-14 12:57:41 -070017#include <irq_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060018#include <log.h>
Simon Glass891d9e82021-03-07 17:35:14 -070019#include <mapmem.h>
Simon Glassb03e0512019-11-14 12:57:24 -070020#include <serial.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070021#include <spl.h>
Tom Rini10f6e4d2022-05-27 12:48:32 -040022#include <system-constants.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Eddie James1d99e672022-02-07 17:09:01 -060024#include <asm-generic/gpio.h>
Aneesh Vbcae7212011-07-21 09:10:21 -040025#include <asm/u-boot.h>
Simon Schwarzbb085b82011-09-14 15:29:26 -040026#include <nand.h>
Aneesh V8cf686e2011-07-21 09:10:27 -040027#include <fat.h>
Simon Glass3db71102019-11-14 12:57:16 -070028#include <u-boot/crc.h>
Pali Rohár121a1652021-08-02 15:18:38 +020029#if CONFIG_IS_ENABLED(BANNER_PRINT)
30#include <timestamp.h>
31#endif
Simon Glassefb21722011-10-10 08:55:19 +000032#include <version.h>
Aneesh V8cf686e2011-07-21 09:10:27 -040033#include <image.h>
Aneesh V2d01dd92011-10-21 12:29:34 -040034#include <malloc.h>
Simon Glass31f9f0e2019-10-21 17:26:52 -060035#include <mapmem.h>
Simon Glass11516512014-11-10 17:16:46 -070036#include <dm/root.h>
Simon Glass4f6500a2022-05-08 04:39:27 -060037#include <dm/util.h>
Nikhil M Jain7a17e4c2023-04-10 14:19:13 +053038#include <dm/device-internal.h>
39#include <dm/uclass-internal.h>
Andreas Müller761ca312012-01-04 15:26:24 +000040#include <linux/compiler.h>
B, Ravi6e7585b2017-04-18 17:27:27 +053041#include <fdt_support.h>
Lukasz Majewskia8be2492018-05-02 16:10:54 +020042#include <bootcount.h>
Stefan Roese06985282019-04-11 15:58:44 +020043#include <wdt.h>
Aneesh Vbcae7212011-07-21 09:10:21 -040044
45DECLARE_GLOBAL_DATA_PTR;
Alper Nebi Yasak367ecbf2022-06-18 15:13:11 +030046DECLARE_BINMAN_MAGIC_SYM;
Aneesh Vbcae7212011-07-21 09:10:21 -040047
Tom Rini47f7bca2012-08-13 12:03:19 -070048u32 *boot_params_ptr = NULL;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040049
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +030050#if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
Simon Glass8bee2d22017-11-13 18:55:03 -070051/* See spl.h for information about this */
Simon Glassdbf6be92018-08-01 15:22:42 -060052binman_sym_declare(ulong, u_boot_any, image_pos);
Simon Glasse82c6242019-12-08 17:40:12 -070053binman_sym_declare(ulong, u_boot_any, size);
54
55#ifdef CONFIG_TPL
Simon Glass2b8d2cc2022-10-20 18:22:41 -060056binman_sym_declare(ulong, u_boot_spl_any, image_pos);
57binman_sym_declare(ulong, u_boot_spl_any, size);
Simon Glasse82c6242019-12-08 17:40:12 -070058#endif
Simon Glass8bee2d22017-11-13 18:55:03 -070059
Simon Glassf86ca5a2022-04-30 00:56:52 -060060#ifdef CONFIG_VPL
Simon Glass2b8d2cc2022-10-20 18:22:41 -060061binman_sym_declare(ulong, u_boot_vpl_any, image_pos);
62binman_sym_declare(ulong, u_boot_vpl_any, size);
Simon Glassf86ca5a2022-04-30 00:56:52 -060063#endif
64
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +030065#endif /* BINMAN_UBOOT_SYMBOLS */
66
Alexandru Gagniuc58b504e2021-04-08 11:56:11 -050067/* Define board data structure */
68static struct bd_info bdata __attribute__ ((section(".data")));
69
Marek Vasutb55881d2021-10-23 03:06:03 +020070#if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
Simon Schwarz379c19a2012-03-15 04:01:38 +000071/*
Heiko Schocher496c5482016-06-07 08:31:20 +020072 * Board-specific Platform code can reimplement show_boot_progress () if needed
73 */
74__weak void show_boot_progress(int val) {}
Tom Rinicb80ff22021-05-03 16:48:58 -040075#endif
Heiko Schocher496c5482016-06-07 08:31:20 +020076
Heiko Stuebnera343b4f2020-05-25 19:57:25 +020077#if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
78 defined(CONFIG_SPL_ATF)
Simon Glassb0edea32018-11-15 18:44:09 -070079/* weak, default platform-specific function to initialize dram banks */
80__weak int dram_init_banksize(void)
81{
82 return 0;
83}
84#endif
85
Heiko Schocher496c5482016-06-07 08:31:20 +020086/*
Simon Schwarz379c19a2012-03-15 04:01:38 +000087 * 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 */
Tom Rini71150072021-10-30 23:03:48 -040096#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz379c19a2012-03-15 04:01:38 +000097__weak int spl_start_uboot(void)
98{
Simon Glassd6330062018-11-15 18:43:56 -070099 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");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000102 return 1;
103}
Ladislav Michl431889d2016-07-12 20:28:14 +0200104
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}
Nathan Barrett-Morrison7a0d8802022-02-02 15:05:18 -0500113
114int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
115{
116 return 1;
117}
Simon Schwarz379c19a2012-03-15 04:01:38 +0000118#endif
119
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200120/* 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
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200125void spl_fixup_fdt(void *fdt_blob)
B, Ravi6e7585b2017-04-18 17:27:27 +0530126{
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200127#if defined(CONFIG_SPL_OF_LIBFDT)
B, Ravi6e7585b2017-04-18 17:27:27 +0530128 int err;
129
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200130 if (!fdt_blob)
131 return;
132
B, Ravi6e7585b2017-04-18 17:27:27 +0530133 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) {
Simon Glassd6330062018-11-15 18:43:56 -0700142 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
B, Ravi6e7585b2017-04-18 17:27:27 +0530143 return;
144 }
145
146 err = arch_fixup_fdt(fdt_blob);
147 if (err) {
Simon Glassd6330062018-11-15 18:43:56 -0700148 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
B, Ravi6e7585b2017-04-18 17:27:27 +0530149 return;
150 }
151#endif
152}
153
Simon Glasse82c6242019-12-08 17:40:12 -0700154ulong spl_get_image_pos(void)
155{
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +0300156 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
Alper Nebi Yasakd7f07172022-06-18 15:13:06 +0300157 return BINMAN_SYM_MISSING;
158
Simon Glassf86ca5a2022-04-30 00:56:52 -0600159#ifdef CONFIG_VPL
160 if (spl_next_phase() == PHASE_VPL)
Simon Glass2b8d2cc2022-10-20 18:22:41 -0600161 return binman_sym(ulong, u_boot_vpl_any, image_pos);
Simon Glassf86ca5a2022-04-30 00:56:52 -0600162#endif
163 return spl_next_phase() == PHASE_SPL ?
Simon Glass2b8d2cc2022-10-20 18:22:41 -0600164 binman_sym(ulong, u_boot_spl_any, image_pos) :
Simon Glasse82c6242019-12-08 17:40:12 -0700165 binman_sym(ulong, u_boot_any, image_pos);
166}
167
168ulong spl_get_image_size(void)
169{
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +0300170 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
Alper Nebi Yasakd7f07172022-06-18 15:13:06 +0300171 return BINMAN_SYM_MISSING;
172
Simon Glassf86ca5a2022-04-30 00:56:52 -0600173#ifdef CONFIG_VPL
174 if (spl_next_phase() == PHASE_VPL)
Simon Glass2b8d2cc2022-10-20 18:22:41 -0600175 return binman_sym(ulong, u_boot_vpl_any, size);
Simon Glassf86ca5a2022-04-30 00:56:52 -0600176#endif
177 return spl_next_phase() == PHASE_SPL ?
Simon Glass2b8d2cc2022-10-20 18:22:41 -0600178 binman_sym(ulong, u_boot_spl_any, size) :
Simon Glasse82c6242019-12-08 17:40:12 -0700179 binman_sym(ulong, u_boot_any, size);
180}
181
Simon Glass86c372a2021-01-24 10:06:03 -0700182ulong spl_get_image_text_base(void)
183{
Simon Glassf86ca5a2022-04-30 00:56:52 -0600184#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 :
Simon Glass98463902022-10-20 18:22:39 -0600189 CONFIG_TEXT_BASE;
Simon Glass86c372a2021-01-24 10:06:03 -0700190}
191
Stefan Roeseea8256f2012-08-23 08:34:21 +0200192/*
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
Alexandru Gagniuca25d6b62021-07-15 14:19:24 -0500202__weak void spl_board_prepare_for_optee(void *fdt)
203{
204}
205
Heiko Thieryc5922922022-01-17 16:25:41 +0100206__weak const char *spl_board_loader_name(u32 boot_device)
207{
208 return NULL;
209}
210
Ricardo Salveti949eb222021-10-20 15:12:06 +0300211#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
Michal Simek3a3b9142016-05-10 07:54:20 +0200219__weak void spl_board_prepare_for_boot(void)
220{
221 /* Nothing to do! */
222}
223
Simon Glassf3543e62022-09-06 20:26:52 -0600224__weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
Marek Vasut04ce5422018-08-14 11:27:02 +0200225{
Simon Glass98463902022-10-20 18:22:39 -0600226 return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
Marek Vasut04ce5422018-08-14 11:27:02 +0200227}
228
Tom Rini08574ed2022-10-28 20:27:07 -0400229#ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
Simon Glassd95ceb92016-09-24 18:19:52 -0600230void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100231{
Alper Nebi Yasakd7f07172022-06-18 15:13:06 +0300232 ulong u_boot_pos = spl_get_image_pos();
Simon Glass8bee2d22017-11-13 18:55:03 -0700233
Tom Rini08574ed2022-10-28 20:27:07 -0400234#if CONFIG_SYS_MONITOR_LEN != 0
Simon Glassd95ceb92016-09-24 18:19:52 -0600235 spl_image->size = CONFIG_SYS_MONITOR_LEN;
Tom Rini08574ed2022-10-28 20:27:07 -0400236#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
Miquel Raynal55fe0e22018-02-28 20:51:43 +0100240
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 */
Simon Glass8bee2d22017-11-13 18:55:03 -0700248 spl_image->entry_point = u_boot_pos;
249 spl_image->load_addr = u_boot_pos;
250 } else {
Jesse Taube6ab77bb2023-08-24 21:59:48 -0400251 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
Simon Glass98463902022-10-20 18:22:39 -0600252 spl_image->load_addr = CONFIG_TEXT_BASE;
Simon Glass8bee2d22017-11-13 18:55:03 -0700253 }
Simon Glassd95ceb92016-09-24 18:19:52 -0600254 spl_image->os = IH_OS_U_BOOT;
255 spl_image->name = "U-Boot";
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100256}
Tom Rini08574ed2022-10-28 20:27:07 -0400257#endif
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100258
John Keeping674afa62021-05-19 15:05:53 +0100259#if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
Marek Vasut8a9dc162018-05-13 00:23:17 +0200260/* Parse and load full fitImage in SPL */
261static int spl_load_fit_image(struct spl_image_info *spl_image,
Simon Glassf3543e62022-09-06 20:26:52 -0600262 const struct legacy_img_hdr *header)
Marek Vasut8a9dc162018-05-13 00:23:17 +0200263{
Simon Glassd9d7c202022-09-06 20:26:50 -0600264 struct bootm_headers images;
Marek Vasut8a9dc162018-05-13 00:23:17 +0200265 const char *fit_uname_config = NULL;
Alexandru Gagniuc60138aa2021-04-01 13:25:28 -0500266 uintptr_t fdt_hack;
Marek Vasut8a9dc162018-05-13 00:23:17 +0200267 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,
Alexandru Gagniuc51d3a8b2021-04-01 13:25:29 -0500279 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
Marek Vasut8a9dc162018-05-13 00:23:17 +0200297 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;
Alexandru Gagniuc2eaae172021-04-01 13:25:27 -0500303 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);
Marek Vasut8a9dc162018-05-13 00:23:17 +0200306
Simon Glassd6330062018-11-15 18:43:56 -0700307 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
Simon Goldschmidt30c07402018-11-02 21:49:52 +0100308 spl_image->name, spl_image->load_addr, spl_image->size);
Marek Vasut8a9dc162018-05-13 00:23:17 +0200309
310#ifdef CONFIG_SPL_FIT_SIGNATURE
311 images.verify = 1;
312#endif
Alexandru Gagniuc18fd6632021-04-01 13:25:26 -0500313 ret = fit_image_load(&images, (ulong)header, NULL, &fit_uname_config,
Marek Vasut8a9dc162018-05-13 00:23:17 +0200314 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
315 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Alexandru Gagniuc60138aa2021-04-01 13:25:28 -0500316 if (ret >= 0) {
Marek Vasuta9a82712019-05-07 21:17:02 +0200317 spl_image->fdt_addr = (void *)dt_data;
Marek Vasut8a9dc162018-05-13 00:23:17 +0200318
Alexandru Gagniuc60138aa2021-04-01 13:25:28 -0500319 if (spl_image->os == IH_OS_U_BOOT) {
Michal Simek1be82af2023-05-17 09:17:16 +0200320 /* HACK: U-Boot expects FDT at a specific address */
Alexandru Gagniuc60138aa2021-04-01 13:25:28 -0500321 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
Marek Vasut8a9dc162018-05-13 00:23:17 +0200328 conf_noffset = fit_conf_get_node((const void *)header,
329 fit_uname_config);
Bin Meng1310ad32023-05-01 11:35:26 +0800330 if (conf_noffset < 0)
Marek Vasut8a9dc162018-05-13 00:23:17 +0200331 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
Pali Rohár9baab602021-07-23 11:14:28 +0200355__weak int spl_parse_board_header(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100356 const struct spl_boot_device *bootdev,
Pali Rohár9baab602021-07-23 11:14:28 +0200357 const void *image_header, size_t size)
358{
359 return -EINVAL;
360}
361
Stefan Roesec1108172020-04-21 09:28:41 +0200362__weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
Simon Glassf3543e62022-09-06 20:26:52 -0600363 const struct legacy_img_hdr *header)
Stefan Roesec1108172020-04-21 09:28:41 +0200364{
365 /* LEGACY image not supported */
366 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
367 return -EINVAL;
368}
369
Simon Glass71316c12016-09-24 18:19:53 -0600370int spl_parse_image_header(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100371 const struct spl_boot_device *bootdev,
Simon Glassf3543e62022-09-06 20:26:52 -0600372 const struct legacy_img_hdr *header)
Aneesh V8cf686e2011-07-21 09:10:27 -0400373{
John Keeping674afa62021-05-19 15:05:53 +0100374#if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
Marek Vasut8a9dc162018-05-13 00:23:17 +0200375 int ret = spl_load_fit_image(spl_image, header);
376
377 if (!ret)
378 return ret;
379#endif
Stefan Roese77552b02012-08-27 12:50:57 +0200380 if (image_get_magic(header) == IH_MAGIC) {
Stefan Roesec1108172020-04-21 09:28:41 +0200381 int ret;
Andrew F. Davis722a6b12017-02-16 11:18:39 -0600382
Stefan Roesec1108172020-04-21 09:28:41 +0200383 ret = spl_parse_legacy_header(spl_image, header);
384 if (ret)
385 return ret;
Aneesh V8cf686e2011-07-21 09:10:27 -0400386 } else {
Albert ARIBAUD \(3ADEV\)8c80eb32015-03-31 11:40:50 +0200387#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");
Paul Kocialkowski85a37722016-08-24 20:04:42 +0200397#endif
398
Tom Rini71150072021-10-30 23:03:48 -0400399#if CONFIG_IS_ENABLED(OS_BOOT)
Nathan Barrett-Morrison7a0d8802022-02-02 15:05:18 -0500400#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)
Ladislav Michl431889d2016-07-12 20:28:14 +0200415 ulong start, end;
416
417 if (!bootz_setup((ulong)header, &start, &end)) {
Simon Glass71316c12016-09-24 18:19:53 -0600418 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;
Simon Glassd6330062018-11-15 18:43:56 -0700423 debug(SPL_TPL_PROMPT
424 "payload zImage, load addr: 0x%lx size: %d\n",
Simon Glass71316c12016-09-24 18:19:53 -0600425 spl_image->load_addr, spl_image->size);
Ladislav Michl431889d2016-07-12 20:28:14 +0200426 return 0;
427 }
428#endif
Nathan Barrett-Morrison7a0d8802022-02-02 15:05:18 -0500429#endif
Paul Kocialkowski85a37722016-08-24 20:04:42 +0200430
Pali Rohár2e0429b2022-01-14 14:31:38 +0100431 if (!spl_parse_board_header(spl_image, bootdev, (const void *)header, sizeof(*header)))
Pali Rohár9baab602021-07-23 11:14:28 +0200432 return 0;
433
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600434#ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
Aneesh V8cf686e2011-07-21 09:10:27 -0400435 /* Signature not found - assume u-boot.bin */
Tom Rini026b2fe2012-08-14 12:06:43 -0700436 debug("mkimage signature not found - ih_magic = %x\n",
Aneesh V8cf686e2011-07-21 09:10:27 -0400437 header->ih_magic);
Simon Glass71316c12016-09-24 18:19:53 -0600438 spl_set_header_raw_uboot(spl_image);
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600439#else
440 /* RAW image not supported, proceed to other boot methods. */
Anatolij Gustschin2d2531b2017-08-01 16:17:12 +0200441 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600442 return -EINVAL;
Albert ARIBAUD \(3ADEV\)8c80eb32015-03-31 11:40:50 +0200443#endif
Aneesh V8cf686e2011-07-21 09:10:27 -0400444 }
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600445
Marek Vasut7e0f2262016-04-29 00:44:54 +0200446 return 0;
Aneesh V8cf686e2011-07-21 09:10:27 -0400447}
448
Allen Martina759f1e2012-10-19 21:08:22 +0000449__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
Aneesh V8cf686e2011-07-21 09:10:27 -0400450{
SRICHARAN R4a0eb752013-04-24 00:41:24 +0000451 typedef void __noreturn (*image_entry_noargs_t)(void);
452
Aneesh V8cf686e2011-07-21 09:10:27 -0400453 image_entry_noargs_t image_entry =
Andre Przywara11e14792017-01-02 11:48:31 +0000454 (image_entry_noargs_t)spl_image->entry_point;
Aneesh V8cf686e2011-07-21 09:10:27 -0400455
Simon Goldschmidt30c07402018-11-02 21:49:52 +0100456 debug("image entry point: 0x%lx\n", spl_image->entry_point);
SRICHARAN R4a0eb752013-04-24 00:41:24 +0000457 image_entry();
Aneesh V8cf686e2011-07-21 09:10:27 -0400458}
459
Simon Glassb0edea32018-11-15 18:44:09 -0700460#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
Simon Glass7f3b79a2022-01-12 19:26:17 -0700470 ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
Simon Glassb0edea32018-11-15 18:44:09 -0700471 if (!ho)
472 return -ENOENT;
473
474 return 0;
475}
476
Simon Glass366291a2019-09-25 08:11:18 -0600477__weak int handoff_arch_save(struct spl_handoff *ho)
478{
479 return 0;
480}
481
Simon Glassb0edea32018-11-15 18:44:09 -0700482static int write_spl_handoff(void)
483{
484 struct spl_handoff *ho;
Simon Glass366291a2019-09-25 08:11:18 -0600485 int ret;
Simon Glassb0edea32018-11-15 18:44:09 -0700486
Simon Glass7f3b79a2022-01-12 19:26:17 -0700487 ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
Simon Glassb0edea32018-11-15 18:44:09 -0700488 if (!ho)
489 return -ENOENT;
490 handoff_save_dram(ho);
Simon Glass366291a2019-09-25 08:11:18 -0600491 ret = handoff_arch_save(ho);
492 if (ret)
493 return ret;
Simon Glassb0edea32018-11-15 18:44:09 -0700494 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
Simon Glass05e3a0d2021-03-15 18:11:15 +1300504/**
505 * get_bootstage_id() - Get the bootstage ID to emit
506 *
507 * @start: true if this is for starting SPL, false for ending it
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100508 * Return: bootstage ID to use
Simon Glass05e3a0d2021-03-15 18:11:15 +1300509 */
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;
Simon Glassf86ca5a2022-04-30 00:56:52 -0600516 else if (IS_ENABLED(CONFIG_VPL_BUILD) && phase == PHASE_VPL)
517 return start ? BOOTSTAGE_ID_START_VPL : BOOTSTAGE_ID_END_VPL;
Simon Glass05e3a0d2021-03-15 18:11:15 +1300518 else
519 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
520}
521
Eddie Cai340f4182017-03-15 08:43:28 -0600522static int spl_common_init(bool setup_malloc)
Aneesh Vbcae7212011-07-21 09:10:21 -0400523{
Simon Glassf3d46bd2015-02-27 22:06:42 -0700524 int ret;
525
Simon Glass3d6d5072023-09-26 08:14:27 -0600526#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
Eddie Cai340f4182017-03-15 08:43:28 -0600527 if (setup_malloc) {
Tom Rinidd5b58c2022-12-04 10:04:49 -0500528#ifdef CFG_MALLOC_F_ADDR
529 gd->malloc_base = CFG_MALLOC_F_ADDR;
Marek Vasut94b9e222016-05-25 02:14:56 +0200530#endif
Andy Yanf1896c42017-07-24 17:43:34 +0800531 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
Eddie Cai340f4182017-03-15 08:43:28 -0600532 gd->malloc_ptr = 0;
533 }
Tom Rini24dafad2012-08-13 14:13:07 -0700534#endif
Simon Glass31f9f0e2019-10-21 17:26:52 -0600535 ret = bootstage_init(u_boot_first_phase());
Simon Glass824bb1b2017-05-22 05:05:35 -0600536 if (ret) {
537 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
538 ret);
539 return ret;
540 }
Simon Glass31f9f0e2019-10-21 17:26:52 -0600541#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 */
Simon Glass05e3a0d2021-03-15 18:11:15 +1300552 bootstage_mark_name(get_bootstage_id(true),
553 spl_phase_name(spl_phase()));
Simon Glass4d8d3052018-11-15 18:43:49 -0700554#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
Simon Glass414cc152021-08-07 07:24:03 -0600561 if (CONFIG_IS_ENABLED(OF_REAL)) {
Simon Glassf3d46bd2015-02-27 22:06:42 -0700562 ret = fdtdec_setup();
563 if (ret) {
564 debug("fdtdec_setup() returned error %d\n", ret);
Simon Glass070d00b2015-06-23 15:39:10 -0600565 return ret;
Simon Glassf3d46bd2015-02-27 22:06:42 -0700566 }
567 }
Philipp Tomsichf1c6e192017-06-30 19:02:53 +0200568 if (CONFIG_IS_ENABLED(DM)) {
Simon Glassb67eefd2020-05-10 11:39:59 -0600569 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
Simon Glass5256bee2019-10-21 17:26:51 -0600570 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
Tom Rini7f73ca42017-01-14 12:20:23 -0500571 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
Simon Glass7d23b9c2016-07-04 11:58:14 -0600572 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
Simon Glassb67eefd2020-05-10 11:39:59 -0600573 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
Simon Glassf3d46bd2015-02-27 22:06:42 -0700574 if (ret) {
575 debug("dm_init_and_scan() returned error %d\n", ret);
Simon Glass070d00b2015-06-23 15:39:10 -0600576 return ret;
Simon Glassf3d46bd2015-02-27 22:06:42 -0700577 }
578 }
Eddie Cai340f4182017-03-15 08:43:28 -0600579
580 return 0;
581}
582
Alexandru Gagniuc58b504e2021-04-08 11:56:11 -0500583void spl_set_bd(void)
York Sund1fc0a32017-09-28 08:42:10 -0700584{
Simon Glassc21f4072018-11-15 18:43:58 -0700585 /*
586 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
587 * writeable.
588 */
Alexandru Gagniuc58b504e2021-04-08 11:56:11 -0500589 if (!gd->bd)
590 gd->bd = &bdata;
York Sund1fc0a32017-09-28 08:42:10 -0700591}
592
Eddie Cai340f4182017-03-15 08:43:28 -0600593int spl_early_init(void)
594{
595 int ret;
596
Simon Goldschmidt94cb9862018-08-13 11:24:05 +0200597 debug("%s\n", __func__);
598
Eddie Cai340f4182017-03-15 08:43:28 -0600599 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;
Tom Rinicf334ed2017-03-20 14:19:27 -0400610 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
611 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
Eddie Cai340f4182017-03-15 08:43:28 -0600612
Simon Goldschmidt94cb9862018-08-13 11:24:05 +0200613 debug("%s\n", __func__);
614
Eddie Cai340f4182017-03-15 08:43:28 -0600615 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
Tom Rinicf334ed2017-03-20 14:19:27 -0400616 ret = spl_common_init(setup_malloc);
Eddie Cai340f4182017-03-15 08:43:28 -0600617 if (ret)
618 return ret;
619 }
Simon Glass070d00b2015-06-23 15:39:10 -0600620 gd->flags |= GD_FLG_SPL_INIT;
Aneesh V2d01dd92011-10-21 12:29:34 -0400621
Simon Glass070d00b2015-06-23 15:39:10 -0600622 return 0;
623}
624
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200625#ifndef BOOT_DEVICE_NONE
626#define BOOT_DEVICE_NONE 0xdeadbeef
627#endif
628
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200629__weak void board_boot_order(u32 *spl_boot_list)
630{
631 spl_boot_list[0] = spl_boot_device();
632}
633
Pali Rohárc10939d2022-01-14 14:31:40 +0100634__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
Simon Glass29d357d2016-11-30 15:30:52 -0700640static int spl_load_image(struct spl_image_info *spl_image,
641 struct spl_image_loader *loader)
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200642{
Simon Goldschmidtdae5c2d2019-02-10 21:34:37 +0100643 int ret;
Simon Glassecdfd692016-09-24 18:19:57 -0600644 struct spl_boot_device bootdev;
645
Simon Glass29d357d2016-11-30 15:30:52 -0700646 bootdev.boot_device = loader->boot_device;
Simon Glassecdfd692016-09-24 18:19:57 -0600647 bootdev.boot_device_name = NULL;
648
Simon Goldschmidtdae5c2d2019-02-10 21:34:37 +0100649 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
Pali Rohárc10939d2022-01-14 14:31:40 +0100661 if (!ret)
662 ret = spl_check_board_image(spl_image, &bootdev);
663
Simon Goldschmidtdae5c2d2019-02-10 21:34:37 +0100664 return ret;
Simon Glass540bfe72016-11-30 15:30:51 -0700665}
666
667/**
Miquel Raynalfd4ee982019-05-07 14:18:43 +0200668 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
Simon Glass540bfe72016-11-30 15:30:51 -0700669 *
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
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100673 * Return: 0 if OK, -ENODEV if there were no boot devices
Simon Glass7d84fbb2021-07-05 16:32:57 -0600674 * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
675 * devices but none worked
Simon Glass540bfe72016-11-30 15:30:51 -0700676 */
677static int boot_from_devices(struct spl_image_info *spl_image,
678 u32 spl_boot_list[], int count)
679{
Simon Glass5a61bf12022-10-20 18:23:07 -0600680 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);
Simon Glass7d84fbb2021-07-05 16:32:57 -0600684 int ret = -ENODEV;
Simon Glass540bfe72016-11-30 15:30:51 -0700685 int i;
686
687 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
688 struct spl_image_loader *loader;
Simon Glass7d84fbb2021-07-05 16:32:57 -0600689 int bootdev = spl_boot_list[i];
Simon Glass540bfe72016-11-30 15:30:51 -0700690
Simon Glass7d84fbb2021-07-05 16:32:57 -0600691 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
692 ret = -ENXIO;
Simon Glass5a61bf12022-10-20 18:23:07 -0600693 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 }
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200714 }
Simon Glass540bfe72016-11-30 15:30:51 -0700715 }
716
Simon Glass7d84fbb2021-07-05 16:32:57 -0600717 return ret;
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200718}
719
Philippe Reynesa9a3aad2019-09-19 16:18:39 +0200720#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
Samuel Holland3988be52020-05-07 18:08:10 -0500733 preloader_console_init();
Philippe Reynesa9a3aad2019-09-19 16:18:39 +0200734}
735#endif
736
Simon Glass070d00b2015-06-23 15:39:10 -0600737void board_init_r(gd_t *dummy1, ulong dummy2)
738{
Simon Glassd32b2d12016-09-24 18:20:17 -0600739 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;
Simon Glassf817e082023-09-26 08:14:22 -0600747 int ret, os;
Simon Glass070d00b2015-06-23 15:39:10 -0600748
Simon Glassd6330062018-11-15 18:43:56 -0700749 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
York Sund1fc0a32017-09-28 08:42:10 -0700750
Alexandru Gagniuc58b504e2021-04-08 11:56:11 -0500751 spl_set_bd();
752
Simon Glass52779872023-09-26 08:14:18 -0600753 if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC)) {
754 mem_malloc_init(SPL_SYS_MALLOC_START, SPL_SYS_MALLOC_SIZE);
755 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
756 }
Simon Glass070d00b2015-06-23 15:39:10 -0600757 if (!(gd->flags & GD_FLG_SPL_INIT)) {
758 if (spl_init())
759 hang();
760 }
Ilya Yanok4063c772012-09-17 10:26:26 +0000761 timer_init();
Simon Glass3cd71982019-09-25 08:11:19 -0600762 if (CONFIG_IS_ENABLED(BLOBLIST)) {
763 ret = bloblist_init();
764 if (ret) {
765 debug("%s: Failed to set up bloblist: ret=%d\n",
766 __func__, ret);
767 puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
768 hang();
769 }
770 }
771 if (CONFIG_IS_ENABLED(HANDOFF)) {
772 int ret;
773
774 ret = setup_spl_handoff();
775 if (ret) {
776 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
777 hang();
778 }
779 }
Ilya Yanok4063c772012-09-17 10:26:26 +0000780
Simon Glass6371c472023-09-26 08:14:20 -0600781 if (CONFIG_IS_ENABLED(BOARD_INIT))
782 spl_board_init();
Tom Riniee08a822011-11-23 05:13:06 +0000783
Simon Glass6371c472023-09-26 08:14:20 -0600784 if (IS_ENABLED(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT))
785 initr_watchdog();
Stefan Roese06985282019-04-11 15:58:44 +0200786
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200787 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
788 IS_ENABLED(CONFIG_SPL_ATF))
Simon Glassb0edea32018-11-15 18:44:09 -0700789 dram_init_banksize();
790
Simon Glass15a23b62023-09-07 09:58:13 -0600791 if (CONFIG_IS_ENABLED(PCI) && !(gd->flags & GD_FLG_DM_DEAD)) {
Heinrich Schuchardt7d4c8cf2023-07-24 22:18:41 +0200792 ret = pci_init();
793 if (ret)
794 puts(SPL_TPL_PROMPT "Cannot initialize PCI\n");
795 /* Don't fail. We still can try other boot methods. */
796 }
797
Lukasz Majewskia8be2492018-05-02 16:10:54 +0200798 bootcount_inc();
799
Simon Glass4f6500a2022-05-08 04:39:27 -0600800 /* Dump driver model states to aid analysis */
801 if (CONFIG_IS_ENABLED(DM_STATS)) {
802 struct dm_stats mem;
803
804 dm_get_mem(&mem);
805 dm_dump_mem(&mem);
806 }
807
Simon Glassd32b2d12016-09-24 18:20:17 -0600808 memset(&spl_image, '\0', sizeof(spl_image));
Simon Glasse0be6ea2023-09-26 08:14:21 -0600809 if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
810 spl_image.arg = (void *)SPL_PAYLOAD_ARGS_ADDR;
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200811 spl_image.boot_device = BOOT_DEVICE_NONE;
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200812 board_boot_order(spl_boot_list);
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200813
Simon Glass7d84fbb2021-07-05 16:32:57 -0600814 ret = boot_from_devices(&spl_image, spl_boot_list,
815 ARRAY_SIZE(spl_boot_list));
816 if (ret) {
817 if (CONFIG_IS_ENABLED(SHOW_ERRORS) &&
818 CONFIG_IS_ENABLED(LIBCOMMON_SUPPORT))
819 printf(SPL_TPL_PROMPT "failed to boot from all boot devices (err=%d)\n",
820 ret);
821 else
822 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400823 hang();
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200824 }
Aneesh V8cf686e2011-07-21 09:10:27 -0400825
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200826 spl_perform_fixups(&spl_image);
Simon Glassb0edea32018-11-15 18:44:09 -0700827 if (CONFIG_IS_ENABLED(HANDOFF)) {
828 ret = write_spl_handoff();
829 if (ret)
830 printf(SPL_TPL_PROMPT
831 "SPL hand-off write failed (err=%d)\n", ret);
832 }
Simon Glasse945a722018-11-15 18:43:51 -0700833 if (CONFIG_IS_ENABLED(BLOBLIST)) {
834 ret = bloblist_finish();
835 if (ret)
836 printf("Warning: Failed to finish bloblist (ret=%d)\n",
837 ret);
838 }
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200839
Simon Glassf817e082023-09-26 08:14:22 -0600840 os = spl_image.os;
841 if (os == IH_OS_U_BOOT) {
Simon Glass1a9e75b2021-02-06 09:57:27 -0700842 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
Simon Glassf817e082023-09-26 08:14:22 -0600843 } else if (CONFIG_IS_ENABLED(ATF) && os == IH_OS_ARM_TRUSTED_FIRMWARE) {
Philipp Tomsich1d379092017-09-13 21:29:35 +0200844 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
Simon Glassf817e082023-09-26 08:14:22 -0600845 spl_fixup_fdt(spl_image_fdt_addr(&spl_image));
Philipp Tomsich1d379092017-09-13 21:29:35 +0200846 spl_invoke_atf(&spl_image);
Simon Glassf817e082023-09-26 08:14:22 -0600847 } else if (CONFIG_IS_ENABLED(OPTEE_IMAGE) && os == IH_OS_TEE) {
Kever Yang70fe2872018-08-23 17:17:59 +0800848 debug("Jumping to U-Boot via OP-TEE\n");
Simon Glassf817e082023-09-26 08:14:22 -0600849 spl_board_prepare_for_optee(spl_image_fdt_addr(&spl_image));
Ricardo Salveti949eb222021-10-20 15:12:06 +0300850 jump_to_image_optee(&spl_image);
Simon Glassf817e082023-09-26 08:14:22 -0600851 } else if (CONFIG_IS_ENABLED(OPENSBI) && os == IH_OS_OPENSBI) {
Lukas Auer5e30e452019-08-21 21:14:44 +0200852 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
853 spl_invoke_opensbi(&spl_image);
Simon Glassf817e082023-09-26 08:14:22 -0600854 } else if (CONFIG_IS_ENABLED(OS_BOOT) && os == IH_OS_LINUX) {
Simon Schwarz379c19a2012-03-15 04:01:38 +0000855 debug("Jumping to Linux\n");
Simon Glasse0be6ea2023-09-26 08:14:21 -0600856 if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
857 spl_fixup_fdt((void *)SPL_PAYLOAD_ARGS_ADDR);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000858 spl_board_prepare_for_linux();
Vikas Manocha5bf52502017-04-07 15:38:13 -0700859 jump_to_image_linux(&spl_image);
Simon Glassf817e082023-09-26 08:14:22 -0600860 } else {
Tom Rini42120982012-08-27 14:58:28 -0700861 debug("Unsupported OS image.. Jumping nevertheless..\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400862 }
Simon Glass3d6d5072023-09-26 08:14:27 -0600863#if CONFIG_IS_ENABLED(SYS_MALLOC_F) && !defined(CONFIG_SPL_SYS_MALLOC_SIZE)
Simon Goldschmidt30c07402018-11-02 21:49:52 +0100864 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
Simon Glassfb4f5e72014-11-10 17:16:45 -0700865 gd->malloc_ptr / 1024);
866#endif
Simon Glass05e3a0d2021-03-15 18:11:15 +1300867 bootstage_mark_name(get_bootstage_id(false), "end phase");
Simon Glass824bb1b2017-05-22 05:05:35 -0600868#ifdef CONFIG_BOOTSTAGE_STASH
Simon Glass824bb1b2017-05-22 05:05:35 -0600869 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
870 CONFIG_BOOTSTAGE_STASH_SIZE);
871 if (ret)
872 debug("Failed to stash bootstage: err=%d\n", ret);
873#endif
Kever Yanga8c51122017-09-13 18:24:24 +0800874
Nikhil M Jain954b0ad2023-07-18 14:27:33 +0530875 if (IS_ENABLED(CONFIG_SPL_VIDEO_REMOVE)) {
876 struct udevice *dev;
877 int rc;
Nikhil M Jain7a17e4c2023-04-10 14:19:13 +0530878
Nikhil M Jain954b0ad2023-07-18 14:27:33 +0530879 rc = uclass_find_device(UCLASS_VIDEO, 0, &dev);
880 if (!rc && dev) {
881 rc = device_remove(dev, DM_REMOVE_NORMAL);
882 if (rc)
883 printf("Cannot remove video device '%s' (err=%d)\n",
884 dev->name, rc);
885 }
Nikhil M Jain7a17e4c2023-04-10 14:19:13 +0530886 }
Nikhil M Jain7a17e4c2023-04-10 14:19:13 +0530887
Michal Simek3a3b9142016-05-10 07:54:20 +0200888 spl_board_prepare_for_boot();
Allen Martina759f1e2012-10-19 21:08:22 +0000889 jump_to_image_no_args(&spl_image);
Aneesh Vbcae7212011-07-21 09:10:21 -0400890}
891
Tom Rini6507f132012-08-22 15:31:05 -0700892/*
893 * This requires UART clocks to be enabled. In order for this to work the
894 * caller must ensure that the gd pointer is valid.
895 */
Aneesh Vbcae7212011-07-21 09:10:21 -0400896void preloader_console_init(void)
897{
Simon Glass2a736062021-08-08 12:20:12 -0600898#ifdef CONFIG_SPL_SERIAL
Aneesh Vbcae7212011-07-21 09:10:21 -0400899 gd->baudrate = CONFIG_BAUDRATE;
900
Aneesh Vbcae7212011-07-21 09:10:21 -0400901 serial_init(); /* serial communications setup */
902
Ilya Yanokb88befa2011-11-01 13:16:03 +0000903 gd->have_console = 1;
904
Simon Glassaedc08b2018-11-15 18:43:57 -0700905#if CONFIG_IS_ENABLED(BANNER_PRINT)
Simon Glassd6330062018-11-15 18:43:56 -0700906 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
907 U_BOOT_TIME " " U_BOOT_TZ ")\n");
Anatolij Gustschin0292bc02018-01-25 18:45:22 +0100908#endif
Tom Rini861a86f2012-08-13 11:37:56 -0700909#ifdef CONFIG_SPL_DISPLAY_PRINT
910 spl_display_print();
911#endif
Alex Kiernan117a0e02018-04-19 04:32:51 +0000912#endif
Samuel Holland3988be52020-05-07 18:08:10 -0500913}
Simon Glassdb910352015-03-03 08:03:00 -0700914
915/**
Simon Goldschmidtd8c03322019-07-16 22:30:36 +0200916 * This function is called before the stack is changed from initial stack to
917 * relocated stack. It tries to dump the stack size used
918 */
919__weak void spl_relocate_stack_check(void)
920{
921#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
922 ulong init_sp = gd->start_addr_sp;
923 ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
924 u8 *ptr = (u8 *)stack_bottom;
925 ulong i;
926
927 for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
928 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
929 break;
930 ptr++;
931 }
932 printf("SPL initial stack usage: %lu bytes\n",
933 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
934#endif
935}
936
937/**
Simon Glassdb910352015-03-03 08:03:00 -0700938 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
939 *
940 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
941 * for the main board_init_r() execution. This is typically because we need
942 * more stack space for things like the MMC sub-system.
943 *
944 * This function calculates the stack position, copies the global_data into
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100945 * place, sets the new gd (except for ARM, for which setting GD within a C
946 * function may not always work) and returns the new stack position. The
947 * caller is responsible for setting up the sp register and, in the case
948 * of ARM, setting up gd.
949 *
950 * All of this is done using the same layout and alignments as done in
951 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
Simon Glassdb910352015-03-03 08:03:00 -0700952 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100953 * Return: new stack location, or 0 to use the same stack
Simon Glassdb910352015-03-03 08:03:00 -0700954 */
955ulong spl_relocate_stack_gd(void)
956{
957#ifdef CONFIG_SPL_STACK_R
958 gd_t *new_gd;
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100959 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
Simon Glassdb910352015-03-03 08:03:00 -0700960
Simon Goldschmidtd8c03322019-07-16 22:30:36 +0200961 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
962 spl_relocate_stack_check();
963
Simon Glass3d6d5072023-09-26 08:14:27 -0600964#if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_IS_ENABLED(SYS_MALLOC_F)
Hans de Goededcfcb8d2015-09-13 15:04:17 +0200965 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
Simon Goldschmidt438dcab2019-02-26 22:27:52 +0100966 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
967 gd->malloc_ptr, gd->malloc_ptr / 1024);
Hans de Goededcfcb8d2015-09-13 15:04:17 +0200968 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
969 gd->malloc_base = ptr;
970 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
971 gd->malloc_ptr = 0;
972 }
973#endif
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100974 /* Get stack position: use 8-byte alignment for ABI compliance */
975 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
Nikhil M Jain149fb052023-07-18 14:27:27 +0530976 gd->start_addr_sp = ptr;
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100977 new_gd = (gd_t *)ptr;
978 memcpy(new_gd, (void *)gd, sizeof(gd_t));
Simon Glass2f11cd92016-11-13 14:21:58 -0700979#if CONFIG_IS_ENABLED(DM)
980 dm_fixup_for_gd_move(new_gd);
981#endif
Lukas Auerc7e1eff2019-08-21 21:14:46 +0200982#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100983 gd = new_gd;
984#endif
Simon Glassdb910352015-03-03 08:03:00 -0700985 return ptr;
986#else
987 return 0;
988#endif
989}
Simon Glassc6604442019-11-14 12:57:17 -0700990
Philippe Reynes4d145f22020-12-11 19:56:47 +0100991#if defined(CONFIG_BOOTCOUNT_LIMIT) && \
992 ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
993 (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
Simon Glassc6604442019-11-14 12:57:17 -0700994void bootcount_store(ulong a)
995{
996}
997
998ulong bootcount_load(void)
999{
1000 return 0;
1001}
1002#endif