blob: cd294e81b2a115102492b23f02995e298540b12d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Aneesh Vb8e60b92011-07-21 09:10:21 -04002/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <aneesh@ti.com>
Aneesh Vb8e60b92011-07-21 09:10:21 -04007 */
Philipp Tomsich60e15782017-06-30 19:02:53 +02008
Aneesh Vb8e60b92011-07-21 09:10:21 -04009#include <common.h>
Simon Glass4e0a52d2018-11-15 18:43:51 -070010#include <bloblist.h>
Simon Glass867a3982017-11-13 18:55:03 -070011#include <binman_sym.h>
Simon Glass1ea97892020-05-10 11:40:00 -060012#include <bootstage.h>
Simon Glass1801a3e2014-11-10 17:16:46 -070013#include <dm.h>
Simon Glasse14f1a22018-11-15 18:44:09 -070014#include <handoff.h>
Simon Glassf11478f2019-12-28 10:45:07 -070015#include <hang.h>
Simon Glass97589732020-05-10 11:40:02 -060016#include <init.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070017#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Simon Glass2192e982021-03-07 17:35:14 -070019#include <mapmem.h>
Simon Glass36736182019-11-14 12:57:24 -070020#include <serial.h>
Tom Rini28591df2012-08-13 12:03:19 -070021#include <spl.h>
Tom Rini166e3222022-05-27 12:48:32 -040022#include <system-constants.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Eddie James652b8692022-02-07 17:09:01 -060024#include <asm-generic/gpio.h>
Aneesh Vb8e60b92011-07-21 09:10:21 -040025#include <asm/u-boot.h>
Simon Schwarz992dcf72011-09-14 15:29:26 -040026#include <nand.h>
Aneesh V13a74c12011-07-21 09:10:27 -040027#include <fat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070028#include <u-boot/crc.h>
Pali Rohár6e1f0852021-08-02 15:18:38 +020029#if CONFIG_IS_ENABLED(BANNER_PRINT)
30#include <timestamp.h>
31#endif
Simon Glass50402412011-10-10 08:55:19 +000032#include <version.h>
Aneesh V13a74c12011-07-21 09:10:27 -040033#include <image.h>
Aneesh V0560be22011-10-21 12:29:34 -040034#include <malloc.h>
Simon Glass0e864562019-10-21 17:26:52 -060035#include <mapmem.h>
Simon Glass1801a3e2014-11-10 17:16:46 -070036#include <dm/root.h>
Simon Glass099dc1f2022-05-08 04:39:27 -060037#include <dm/util.h>
Nikhil M Jaineda07d62023-04-10 14:19:13 +053038#include <dm/device-internal.h>
39#include <dm/uclass-internal.h>
Andreas Müller0cda7a42012-01-04 15:26:24 +000040#include <linux/compiler.h>
B, Ravi4cf02b12017-04-18 17:27:27 +053041#include <fdt_support.h>
Lukasz Majewskia4858602018-05-02 16:10:54 +020042#include <bootcount.h>
Stefan Roese502acb02019-04-11 15:58:44 +020043#include <wdt.h>
Aneesh Vb8e60b92011-07-21 09:10:21 -040044
45DECLARE_GLOBAL_DATA_PTR;
Alper Nebi Yasak9634dc92022-06-18 15:13:11 +030046DECLARE_BINMAN_MAGIC_SYM;
Aneesh Vb8e60b92011-07-21 09:10:21 -040047
Tom Rini28591df2012-08-13 12:03:19 -070048u32 *boot_params_ptr = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -040049
Alper Nebi Yasakd6d2c9b2022-06-18 15:13:09 +030050#if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
Simon Glass867a3982017-11-13 18:55:03 -070051/* See spl.h for information about this */
Simon Glass9dcc8612018-08-01 15:22:42 -060052binman_sym_declare(ulong, u_boot_any, image_pos);
Simon Glass596f31c2019-12-08 17:40:12 -070053binman_sym_declare(ulong, u_boot_any, size);
54
55#ifdef CONFIG_TPL
Simon Glass5a332c12022-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 Glass596f31c2019-12-08 17:40:12 -070058#endif
Simon Glass867a3982017-11-13 18:55:03 -070059
Simon Glass7eec8842022-04-30 00:56:52 -060060#ifdef CONFIG_VPL
Simon Glass5a332c12022-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 Glass7eec8842022-04-30 00:56:52 -060063#endif
64
Alper Nebi Yasakd6d2c9b2022-06-18 15:13:09 +030065#endif /* BINMAN_UBOOT_SYMBOLS */
66
Alexandru Gagniuc7861f8b2021-04-08 11:56:11 -050067/* Define board data structure */
68static struct bd_info bdata __attribute__ ((section(".data")));
69
Marek Vasut98154342021-10-23 03:06:03 +020070#if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
Simon Schwarz305bc4c2012-03-15 04:01:38 +000071/*
Heiko Schocher747ceee2016-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 Rinia9765d02021-05-03 16:48:58 -040075#endif
Heiko Schocher747ceee2016-06-07 08:31:20 +020076
Heiko Stuebner593b82e2020-05-25 19:57:25 +020077#if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
78 defined(CONFIG_SPL_ATF)
Simon Glasse14f1a22018-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 Schocher747ceee2016-06-07 08:31:20 +020086/*
Simon Schwarz305bc4c2012-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 Rinic2e78882021-10-30 23:03:48 -040096#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz305bc4c2012-03-15 04:01:38 +000097__weak int spl_start_uboot(void)
98{
Simon Glassdbd3f112018-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 Schwarz305bc4c2012-03-15 04:01:38 +0000102 return 1;
103}
Ladislav Michl9c743722016-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-Morrisonc20400a2022-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 Schwarz305bc4c2012-03-15 04:01:38 +0000118#endif
119
Philipp Tomsich58beb6e2018-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 Stuebner593b82e2020-05-25 19:57:25 +0200125void spl_fixup_fdt(void *fdt_blob)
B, Ravi4cf02b12017-04-18 17:27:27 +0530126{
Heiko Stuebner593b82e2020-05-25 19:57:25 +0200127#if defined(CONFIG_SPL_OF_LIBFDT)
B, Ravi4cf02b12017-04-18 17:27:27 +0530128 int err;
129
Heiko Stuebner593b82e2020-05-25 19:57:25 +0200130 if (!fdt_blob)
131 return;
132
B, Ravi4cf02b12017-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 Glassdbd3f112018-11-15 18:43:56 -0700142 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
B, Ravi4cf02b12017-04-18 17:27:27 +0530143 return;
144 }
145
146 err = arch_fixup_fdt(fdt_blob);
147 if (err) {
Simon Glassdbd3f112018-11-15 18:43:56 -0700148 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
B, Ravi4cf02b12017-04-18 17:27:27 +0530149 return;
150 }
151#endif
152}
153
Simon Glass596f31c2019-12-08 17:40:12 -0700154ulong spl_get_image_pos(void)
155{
Alper Nebi Yasakd6d2c9b2022-06-18 15:13:09 +0300156 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
Alper Nebi Yasak173321c2022-06-18 15:13:06 +0300157 return BINMAN_SYM_MISSING;
158
Simon Glass7eec8842022-04-30 00:56:52 -0600159#ifdef CONFIG_VPL
160 if (spl_next_phase() == PHASE_VPL)
Simon Glass5a332c12022-10-20 18:22:41 -0600161 return binman_sym(ulong, u_boot_vpl_any, image_pos);
Simon Glass7eec8842022-04-30 00:56:52 -0600162#endif
163 return spl_next_phase() == PHASE_SPL ?
Simon Glass5a332c12022-10-20 18:22:41 -0600164 binman_sym(ulong, u_boot_spl_any, image_pos) :
Simon Glass596f31c2019-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 Yasakd6d2c9b2022-06-18 15:13:09 +0300170 if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
Alper Nebi Yasak173321c2022-06-18 15:13:06 +0300171 return BINMAN_SYM_MISSING;
172
Simon Glass7eec8842022-04-30 00:56:52 -0600173#ifdef CONFIG_VPL
174 if (spl_next_phase() == PHASE_VPL)
Simon Glass5a332c12022-10-20 18:22:41 -0600175 return binman_sym(ulong, u_boot_vpl_any, size);
Simon Glass7eec8842022-04-30 00:56:52 -0600176#endif
177 return spl_next_phase() == PHASE_SPL ?
Simon Glass5a332c12022-10-20 18:22:41 -0600178 binman_sym(ulong, u_boot_spl_any, size) :
Simon Glass596f31c2019-12-08 17:40:12 -0700179 binman_sym(ulong, u_boot_any, size);
180}
181
Simon Glass53857ee2021-01-24 10:06:03 -0700182ulong spl_get_image_text_base(void)
183{
Simon Glass7eec8842022-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 Glass72cc5382022-10-20 18:22:39 -0600189 CONFIG_TEXT_BASE;
Simon Glass53857ee2021-01-24 10:06:03 -0700190}
191
Stefan Roeseec90d342012-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 Gagniucf2b410a2021-07-15 14:19:24 -0500202__weak void spl_board_prepare_for_optee(void *fdt)
203{
204}
205
Heiko Thiery3db30332022-01-17 16:25:41 +0100206__weak const char *spl_board_loader_name(u32 boot_device)
207{
208 return NULL;
209}
210
Ricardo Salvetiec0352a2021-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 Simek7a8465b2016-05-10 07:54:20 +0200219__weak void spl_board_prepare_for_boot(void)
220{
221 /* Nothing to do! */
222}
223
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600224__weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
Marek Vasut1dda46f2018-08-14 11:27:02 +0200225{
Simon Glass72cc5382022-10-20 18:22:39 -0600226 return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
Marek Vasut1dda46f2018-08-14 11:27:02 +0200227}
228
Tom Rini7f184322022-10-28 20:27:07 -0400229#ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
Simon Glass11a1a272016-09-24 18:19:52 -0600230void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
Heiko Schochercf000272014-10-31 08:31:00 +0100231{
Alper Nebi Yasak173321c2022-06-18 15:13:06 +0300232 ulong u_boot_pos = spl_get_image_pos();
Simon Glass867a3982017-11-13 18:55:03 -0700233
Tom Rini7f184322022-10-28 20:27:07 -0400234#if CONFIG_SYS_MONITOR_LEN != 0
Simon Glass11a1a272016-09-24 18:19:52 -0600235 spl_image->size = CONFIG_SYS_MONITOR_LEN;
Tom Rini7f184322022-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 Raynal31824ff2018-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 Glass867a3982017-11-13 18:55:03 -0700248 spl_image->entry_point = u_boot_pos;
249 spl_image->load_addr = u_boot_pos;
250 } else {
Jesse Taube93ee5c82023-08-24 21:59:48 -0400251 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
Simon Glass72cc5382022-10-20 18:22:39 -0600252 spl_image->load_addr = CONFIG_TEXT_BASE;
Simon Glass867a3982017-11-13 18:55:03 -0700253 }
Simon Glass11a1a272016-09-24 18:19:52 -0600254 spl_image->os = IH_OS_U_BOOT;
255 spl_image->name = "U-Boot";
Heiko Schochercf000272014-10-31 08:31:00 +0100256}
Tom Rini7f184322022-10-28 20:27:07 -0400257#endif
Heiko Schochercf000272014-10-31 08:31:00 +0100258
John Keeping6e6f2042021-05-19 15:05:53 +0100259#if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
Marek Vasut06623372018-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 Glassbb7d3bb2022-09-06 20:26:52 -0600262 const struct legacy_img_hdr *header)
Marek Vasut06623372018-05-13 00:23:17 +0200263{
Simon Glassdf00afa2022-09-06 20:26:50 -0600264 struct bootm_headers images;
Marek Vasut06623372018-05-13 00:23:17 +0200265 const char *fit_uname_config = NULL;
Alexandru Gagniucd8d728e2021-04-01 13:25:28 -0500266 uintptr_t fdt_hack;
Marek Vasut06623372018-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 Gagniuc5d6a29b2021-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 Vasut06623372018-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 Gagniuc089f7cf2021-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 Vasut06623372018-05-13 00:23:17 +0200306
Simon Glassdbd3f112018-11-15 18:43:56 -0700307 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100308 spl_image->name, spl_image->load_addr, spl_image->size);
Marek Vasut06623372018-05-13 00:23:17 +0200309
310#ifdef CONFIG_SPL_FIT_SIGNATURE
311 images.verify = 1;
312#endif
Alexandru Gagniucfdf312e2021-04-01 13:25:26 -0500313 ret = fit_image_load(&images, (ulong)header, NULL, &fit_uname_config,
Marek Vasut06623372018-05-13 00:23:17 +0200314 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
315 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Alexandru Gagniucd8d728e2021-04-01 13:25:28 -0500316 if (ret >= 0) {
Marek Vasut187b9e32019-05-07 21:17:02 +0200317 spl_image->fdt_addr = (void *)dt_data;
Marek Vasut06623372018-05-13 00:23:17 +0200318
Alexandru Gagniucd8d728e2021-04-01 13:25:28 -0500319 if (spl_image->os == IH_OS_U_BOOT) {
Michal Simek50fa1182023-05-17 09:17:16 +0200320 /* HACK: U-Boot expects FDT at a specific address */
Alexandru Gagniucd8d728e2021-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 Vasut06623372018-05-13 00:23:17 +0200328 conf_noffset = fit_conf_get_node((const void *)header,
329 fit_uname_config);
Bin Mengfabe7072023-05-01 11:35:26 +0800330 if (conf_noffset < 0)
Marek Vasut06623372018-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árbde23cb2021-07-23 11:14:28 +0200355__weak int spl_parse_board_header(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100356 const struct spl_boot_device *bootdev,
Pali Rohárbde23cb2021-07-23 11:14:28 +0200357 const void *image_header, size_t size)
358{
359 return -EINVAL;
360}
361
Stefan Roese21a1df12020-04-21 09:28:41 +0200362__weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600363 const struct legacy_img_hdr *header)
Stefan Roese21a1df12020-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 Glass6b2e4db2016-09-24 18:19:53 -0600370int spl_parse_image_header(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100371 const struct spl_boot_device *bootdev,
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600372 const struct legacy_img_hdr *header)
Aneesh V13a74c12011-07-21 09:10:27 -0400373{
John Keeping6e6f2042021-05-19 15:05:53 +0100374#if CONFIG_IS_ENABLED(LOAD_FIT_FULL)
Marek Vasut06623372018-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 Roese292db772012-08-27 12:50:57 +0200380 if (image_get_magic(header) == IH_MAGIC) {
Stefan Roese21a1df12020-04-21 09:28:41 +0200381 int ret;
Andrew F. Davis74630342017-02-16 11:18:39 -0600382
Stefan Roese21a1df12020-04-21 09:28:41 +0200383 ret = spl_parse_legacy_header(spl_image, header);
384 if (ret)
385 return ret;
Aneesh V13a74c12011-07-21 09:10:27 -0400386 } else {
Albert ARIBAUD \(3ADEV\)287b0942015-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 Kocialkowskie48b7752016-08-24 20:04:42 +0200397#endif
398
Tom Rinic2e78882021-10-30 23:03:48 -0400399#if CONFIG_IS_ENABLED(OS_BOOT)
Nathan Barrett-Morrisonc20400a2022-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 Michl9c743722016-07-12 20:28:14 +0200415 ulong start, end;
416
417 if (!bootz_setup((ulong)header, &start, &end)) {
Simon Glass6b2e4db2016-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 Glassdbd3f112018-11-15 18:43:56 -0700423 debug(SPL_TPL_PROMPT
424 "payload zImage, load addr: 0x%lx size: %d\n",
Simon Glass6b2e4db2016-09-24 18:19:53 -0600425 spl_image->load_addr, spl_image->size);
Ladislav Michl9c743722016-07-12 20:28:14 +0200426 return 0;
427 }
428#endif
Nathan Barrett-Morrisonc20400a2022-02-02 15:05:18 -0500429#endif
Paul Kocialkowskie48b7752016-08-24 20:04:42 +0200430
Pali Rohárdda8f882022-01-14 14:31:38 +0100431 if (!spl_parse_board_header(spl_image, bootdev, (const void *)header, sizeof(*header)))
Pali Rohárbde23cb2021-07-23 11:14:28 +0200432 return 0;
433
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600434#ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
Aneesh V13a74c12011-07-21 09:10:27 -0400435 /* Signature not found - assume u-boot.bin */
Tom Rini7b516862012-08-14 12:06:43 -0700436 debug("mkimage signature not found - ih_magic = %x\n",
Aneesh V13a74c12011-07-21 09:10:27 -0400437 header->ih_magic);
Simon Glass6b2e4db2016-09-24 18:19:53 -0600438 spl_set_header_raw_uboot(spl_image);
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600439#else
440 /* RAW image not supported, proceed to other boot methods. */
Anatolij Gustschindcb711f2017-08-01 16:17:12 +0200441 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600442 return -EINVAL;
Albert ARIBAUD \(3ADEV\)287b0942015-03-31 11:40:50 +0200443#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400444 }
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600445
Marek Vasut02266e22016-04-29 00:44:54 +0200446 return 0;
Aneesh V13a74c12011-07-21 09:10:27 -0400447}
448
Allen Martinc67e4852012-10-19 21:08:22 +0000449__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
Aneesh V13a74c12011-07-21 09:10:27 -0400450{
SRICHARAN R3f30b0a2013-04-24 00:41:24 +0000451 typedef void __noreturn (*image_entry_noargs_t)(void);
452
Aneesh V13a74c12011-07-21 09:10:27 -0400453 image_entry_noargs_t image_entry =
Andre Przywara6c526072017-01-02 11:48:31 +0000454 (image_entry_noargs_t)spl_image->entry_point;
Aneesh V13a74c12011-07-21 09:10:27 -0400455
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100456 debug("image entry point: 0x%lx\n", spl_image->entry_point);
SRICHARAN R3f30b0a2013-04-24 00:41:24 +0000457 image_entry();
Aneesh V13a74c12011-07-21 09:10:27 -0400458}
459
Simon Glasse14f1a22018-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 Glass90c1a582022-01-12 19:26:17 -0700470 ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
Simon Glasse14f1a22018-11-15 18:44:09 -0700471 if (!ho)
472 return -ENOENT;
473
474 return 0;
475}
476
Simon Glassc5d27202019-09-25 08:11:18 -0600477__weak int handoff_arch_save(struct spl_handoff *ho)
478{
479 return 0;
480}
481
Simon Glasse14f1a22018-11-15 18:44:09 -0700482static int write_spl_handoff(void)
483{
484 struct spl_handoff *ho;
Simon Glassc5d27202019-09-25 08:11:18 -0600485 int ret;
Simon Glasse14f1a22018-11-15 18:44:09 -0700486
Simon Glass90c1a582022-01-12 19:26:17 -0700487 ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
Simon Glasse14f1a22018-11-15 18:44:09 -0700488 if (!ho)
489 return -ENOENT;
490 handoff_save_dram(ho);
Simon Glassc5d27202019-09-25 08:11:18 -0600491 ret = handoff_arch_save(ho);
492 if (ret)
493 return ret;
Simon Glasse14f1a22018-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 Glass97447ea2021-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 Schuchardt47b4c022022-01-19 18:05:50 +0100508 * Return: bootstage ID to use
Simon Glass97447ea2021-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 Glass7eec8842022-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 Glass97447ea2021-03-15 18:11:15 +1300518 else
519 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
520}
521
Eddie Cai32258992017-03-15 08:43:28 -0600522static int spl_common_init(bool setup_malloc)
Aneesh Vb8e60b92011-07-21 09:10:21 -0400523{
Simon Glass9a49ec12015-02-27 22:06:42 -0700524 int ret;
525
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800526#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Eddie Cai32258992017-03-15 08:43:28 -0600527 if (setup_malloc) {
Tom Rinifb52b942022-12-04 10:04:49 -0500528#ifdef CFG_MALLOC_F_ADDR
529 gd->malloc_base = CFG_MALLOC_F_ADDR;
Marek Vasutd61ba112016-05-25 02:14:56 +0200530#endif
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800531 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
Eddie Cai32258992017-03-15 08:43:28 -0600532 gd->malloc_ptr = 0;
533 }
Tom Rini730bae72012-08-13 14:13:07 -0700534#endif
Simon Glass0e864562019-10-21 17:26:52 -0600535 ret = bootstage_init(u_boot_first_phase());
Simon Glass01154cb2017-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 Glass0e864562019-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 Glass97447ea2021-03-15 18:11:15 +1300552 bootstage_mark_name(get_bootstage_id(true),
553 spl_phase_name(spl_phase()));
Simon Glassb56cdc02018-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 Glass3580f6d2021-08-07 07:24:03 -0600561 if (CONFIG_IS_ENABLED(OF_REAL)) {
Simon Glass9a49ec12015-02-27 22:06:42 -0700562 ret = fdtdec_setup();
563 if (ret) {
564 debug("fdtdec_setup() returned error %d\n", ret);
Simon Glassfa2a4db2015-06-23 15:39:10 -0600565 return ret;
Simon Glass9a49ec12015-02-27 22:06:42 -0700566 }
567 }
Philipp Tomsich60e15782017-06-30 19:02:53 +0200568 if (CONFIG_IS_ENABLED(DM)) {
Simon Glassea6a6092020-05-10 11:39:59 -0600569 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
Simon Glassaa2abc42019-10-21 17:26:51 -0600570 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
Tom Rini9732f792017-01-14 12:20:23 -0500571 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
Simon Glasse9559fb2016-07-04 11:58:14 -0600572 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
Simon Glassea6a6092020-05-10 11:39:59 -0600573 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
Simon Glass9a49ec12015-02-27 22:06:42 -0700574 if (ret) {
575 debug("dm_init_and_scan() returned error %d\n", ret);
Simon Glassfa2a4db2015-06-23 15:39:10 -0600576 return ret;
Simon Glass9a49ec12015-02-27 22:06:42 -0700577 }
578 }
Eddie Cai32258992017-03-15 08:43:28 -0600579
580 return 0;
581}
582
Alexandru Gagniuc7861f8b2021-04-08 11:56:11 -0500583void spl_set_bd(void)
York Suna34ca5f2017-09-28 08:42:10 -0700584{
Simon Glassef8d48a2018-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 Gagniuc7861f8b2021-04-08 11:56:11 -0500589 if (!gd->bd)
590 gd->bd = &bdata;
York Suna34ca5f2017-09-28 08:42:10 -0700591}
592
Eddie Cai32258992017-03-15 08:43:28 -0600593int spl_early_init(void)
594{
595 int ret;
596
Simon Goldschmidt7dbffe42018-08-13 11:24:05 +0200597 debug("%s\n", __func__);
598
Eddie Cai32258992017-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 Rini89ea21e2017-03-20 14:19:27 -0400610 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
611 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
Eddie Cai32258992017-03-15 08:43:28 -0600612
Simon Goldschmidt7dbffe42018-08-13 11:24:05 +0200613 debug("%s\n", __func__);
614
Eddie Cai32258992017-03-15 08:43:28 -0600615 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
Tom Rini89ea21e2017-03-20 14:19:27 -0400616 ret = spl_common_init(setup_malloc);
Eddie Cai32258992017-03-15 08:43:28 -0600617 if (ret)
618 return ret;
619 }
Simon Glassfa2a4db2015-06-23 15:39:10 -0600620 gd->flags |= GD_FLG_SPL_INIT;
621
622 return 0;
623}
624
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200625#ifndef BOOT_DEVICE_NONE
626#define BOOT_DEVICE_NONE 0xdeadbeef
627#endif
628
Nikita Kiryanov3ed94782015-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árf47349a2022-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 Glassf1e55752016-11-30 15:30:52 -0700640static int spl_load_image(struct spl_image_info *spl_image,
641 struct spl_image_loader *loader)
Simon Glassfa2a4db2015-06-23 15:39:10 -0600642{
Simon Goldschmidt17140472019-02-10 21:34:37 +0100643 int ret;
Simon Glassc0b6c9b2016-09-24 18:19:57 -0600644 struct spl_boot_device bootdev;
645
Simon Glassf1e55752016-11-30 15:30:52 -0700646 bootdev.boot_device = loader->boot_device;
Simon Glassc0b6c9b2016-09-24 18:19:57 -0600647 bootdev.boot_device_name = NULL;
648
Simon Goldschmidt17140472019-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árf47349a2022-01-14 14:31:40 +0100661 if (!ret)
662 ret = spl_check_board_image(spl_image, &bootdev);
663
Simon Goldschmidt17140472019-02-10 21:34:37 +0100664 return ret;
Simon Glassc9ef1852016-11-30 15:30:51 -0700665}
666
667/**
Miquel Raynal9f8d9942019-05-07 14:18:43 +0200668 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
Simon Glassc9ef1852016-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 Schuchardt47b4c022022-01-19 18:05:50 +0100673 * Return: 0 if OK, -ENODEV if there were no boot devices
Simon Glass58254e02021-07-05 16:32:57 -0600674 * if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
675 * devices but none worked
Simon Glassc9ef1852016-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 Glass6b8705d2022-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 Glass58254e02021-07-05 16:32:57 -0600684 int ret = -ENODEV;
Simon Glassc9ef1852016-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 Glass58254e02021-07-05 16:32:57 -0600689 int bootdev = spl_boot_list[i];
Simon Glassc9ef1852016-11-30 15:30:51 -0700690
Simon Glass58254e02021-07-05 16:32:57 -0600691 if (CONFIG_IS_ENABLED(SHOW_ERRORS))
692 ret = -ENXIO;
Simon Glass6b8705d2022-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 Tomsich58beb6e2018-05-24 17:15:50 +0200714 }
Simon Glassc9ef1852016-11-30 15:30:51 -0700715 }
716
Simon Glass58254e02021-07-05 16:32:57 -0600717 return ret;
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200718}
719
Philippe Reynesc4510f42019-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 Hollandb03e6662020-05-07 18:08:10 -0500733 preloader_console_init();
Philippe Reynesc4510f42019-09-19 16:18:39 +0200734}
735#endif
736
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200737void board_init_r(gd_t *dummy1, ulong dummy2)
738{
Simon Glassce8f5092016-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 Glass4e0a52d2018-11-15 18:43:51 -0700747 int ret;
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200748
Simon Glassdbd3f112018-11-15 18:43:56 -0700749 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
York Suna34ca5f2017-09-28 08:42:10 -0700750
Alexandru Gagniuc7861f8b2021-04-08 11:56:11 -0500751 spl_set_bd();
752
Tom Rini166e3222022-05-27 12:48:32 -0400753#if defined(CONFIG_SYS_SPL_MALLOC)
754 mem_malloc_init(SYS_SPL_MALLOC_START, CONFIG_SYS_SPL_MALLOC_SIZE);
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200755 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
756#endif
757 if (!(gd->flags & GD_FLG_SPL_INIT)) {
758 if (spl_init())
759 hang();
760 }
Anatolij Gustschin28674c92017-08-28 17:46:33 +0200761#if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200762 /*
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
Simon Glassf1207752019-09-25 08:11:19 -0600768 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 }
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200786
Kever Yang2b5994b2018-01-20 18:00:26 +0800787#if CONFIG_IS_ENABLED(BOARD_INIT)
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200788 spl_board_init();
789#endif
790
Simon Glass1ba1d4e2021-07-10 21:14:28 -0600791#if defined(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT)
Stefan Roese502acb02019-04-11 15:58:44 +0200792 initr_watchdog();
793#endif
794
Heiko Stuebner593b82e2020-05-25 19:57:25 +0200795 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
796 IS_ENABLED(CONFIG_SPL_ATF))
Simon Glasse14f1a22018-11-15 18:44:09 -0700797 dram_init_banksize();
798
Simon Glass31ef8342023-09-07 09:58:13 -0600799 if (CONFIG_IS_ENABLED(PCI) && !(gd->flags & GD_FLG_DM_DEAD)) {
Heinrich Schuchardt1d8f1372023-07-24 22:18:41 +0200800 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
Lukasz Majewskia4858602018-05-02 16:10:54 +0200806 bootcount_inc();
807
Simon Glass099dc1f2022-05-08 04:39:27 -0600808 /* 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
Simon Glassce8f5092016-09-24 18:20:17 -0600816 memset(&spl_image, '\0', sizeof(spl_image));
Vikas Manocha62b021c2017-04-07 15:38:13 -0700817#ifdef CONFIG_SYS_SPL_ARGS_ADDR
818 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
819#endif
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200820 spl_image.boot_device = BOOT_DEVICE_NONE;
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200821 board_boot_order(spl_boot_list);
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200822
Simon Glass58254e02021-07-05 16:32:57 -0600823 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");
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200832 hang();
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200833 }
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200834
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200835 spl_perform_fixups(&spl_image);
Simon Glasse14f1a22018-11-15 18:44:09 -0700836 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 }
Simon Glass4e0a52d2018-11-15 18:43:51 -0700842 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 }
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200848
Simon Schwarze21d2d82011-09-14 15:33:34 -0400849 switch (spl_image.os) {
Aneesh V13a74c12011-07-21 09:10:27 -0400850 case IH_OS_U_BOOT:
Simon Glasse12d98a2021-02-06 09:57:27 -0700851 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
Aneesh V13a74c12011-07-21 09:10:27 -0400852 break;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200853#if CONFIG_IS_ENABLED(ATF)
854 case IH_OS_ARM_TRUSTED_FIRMWARE:
855 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
Heiko Stuebner593b82e2020-05-25 19:57:25 +0200856 spl_fixup_fdt(spl_image.fdt_addr);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200857 spl_invoke_atf(&spl_image);
858 break;
859#endif
Patrick Delaunaya7ca4802021-09-02 11:56:16 +0200860#if CONFIG_IS_ENABLED(OPTEE_IMAGE)
Kever Yanga28d42c2018-08-23 17:17:59 +0800861 case IH_OS_TEE:
862 debug("Jumping to U-Boot via OP-TEE\n");
Alexandru Gagniucf2b410a2021-07-15 14:19:24 -0500863 spl_board_prepare_for_optee(spl_image.fdt_addr);
Ricardo Salvetiec0352a2021-10-20 15:12:06 +0300864 jump_to_image_optee(&spl_image);
Kever Yanga28d42c2018-08-23 17:17:59 +0800865 break;
866#endif
Lukas Auer515b9342019-08-21 21:14:44 +0200867#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
Tom Rinic2e78882021-10-30 23:03:48 -0400873#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000874 case IH_OS_LINUX:
875 debug("Jumping to Linux\n");
Heiko Stuebner593b82e2020-05-25 19:57:25 +0200876#if defined(CONFIG_SYS_SPL_ARGS_ADDR)
877 spl_fixup_fdt((void *)CONFIG_SYS_SPL_ARGS_ADDR);
878#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000879 spl_board_prepare_for_linux();
Vikas Manocha62b021c2017-04-07 15:38:13 -0700880 jump_to_image_linux(&spl_image);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000881#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400882 default:
Tom Rini79178462012-08-27 14:58:28 -0700883 debug("Unsupported OS image.. Jumping nevertheless..\n");
Aneesh V13a74c12011-07-21 09:10:27 -0400884 }
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800885#if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100886 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
Simon Glassb2d69942014-11-10 17:16:45 -0700887 gd->malloc_ptr / 1024);
888#endif
Simon Glass97447ea2021-03-15 18:11:15 +1300889 bootstage_mark_name(get_bootstage_id(false), "end phase");
Simon Glass01154cb2017-05-22 05:05:35 -0600890#ifdef CONFIG_BOOTSTAGE_STASH
Simon Glass01154cb2017-05-22 05:05:35 -0600891 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
Kever Yangaca62db2017-09-13 18:24:24 +0800896
Nikhil M Jain1a606db2023-07-18 14:27:33 +0530897 if (IS_ENABLED(CONFIG_SPL_VIDEO_REMOVE)) {
898 struct udevice *dev;
899 int rc;
Nikhil M Jaineda07d62023-04-10 14:19:13 +0530900
Nikhil M Jain1a606db2023-07-18 14:27:33 +0530901 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 }
Nikhil M Jaineda07d62023-04-10 14:19:13 +0530908 }
Nikhil M Jaineda07d62023-04-10 14:19:13 +0530909
Michal Simek7a8465b2016-05-10 07:54:20 +0200910 spl_board_prepare_for_boot();
Allen Martinc67e4852012-10-19 21:08:22 +0000911 jump_to_image_no_args(&spl_image);
Aneesh Vb8e60b92011-07-21 09:10:21 -0400912}
913
Tom Rini31dfba42012-08-22 15:31:05 -0700914/*
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 */
Aneesh Vb8e60b92011-07-21 09:10:21 -0400918void preloader_console_init(void)
919{
Simon Glassf4d60392021-08-08 12:20:12 -0600920#ifdef CONFIG_SPL_SERIAL
Aneesh Vb8e60b92011-07-21 09:10:21 -0400921 gd->baudrate = CONFIG_BAUDRATE;
922
Aneesh Vb8e60b92011-07-21 09:10:21 -0400923 serial_init(); /* serial communications setup */
924
Ilya Yanok74193c62011-11-01 13:16:03 +0000925 gd->have_console = 1;
926
Simon Glass6324cce2018-11-15 18:43:57 -0700927#if CONFIG_IS_ENABLED(BANNER_PRINT)
Simon Glassdbd3f112018-11-15 18:43:56 -0700928 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
929 U_BOOT_TIME " " U_BOOT_TZ ")\n");
Anatolij Gustschin6fe74d22018-01-25 18:45:22 +0100930#endif
Tom Rinife3b0c72012-08-13 11:37:56 -0700931#ifdef CONFIG_SPL_DISPLAY_PRINT
932 spl_display_print();
933#endif
Alex Kiernanb09f8922018-04-19 04:32:51 +0000934#endif
Samuel Hollandb03e6662020-05-07 18:08:10 -0500935}
Simon Glassd8711af2015-03-03 08:03:00 -0700936
937/**
Simon Goldschmidt0b820852019-07-16 22:30:36 +0200938 * 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
959/**
Simon Glassd8711af2015-03-03 08:03:00 -0700960 * 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
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100967 * 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().
Simon Glassd8711af2015-03-03 08:03:00 -0700974 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100975 * Return: new stack location, or 0 to use the same stack
Simon Glassd8711af2015-03-03 08:03:00 -0700976 */
977ulong spl_relocate_stack_gd(void)
978{
979#ifdef CONFIG_SPL_STACK_R
980 gd_t *new_gd;
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100981 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
Simon Glassd8711af2015-03-03 08:03:00 -0700982
Simon Goldschmidt0b820852019-07-16 22:30:36 +0200983 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
984 spl_relocate_stack_check();
985
Philipp Tomsich10139352017-07-28 11:06:03 +0200986#if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
Hans de Goede8f280382015-09-13 15:04:17 +0200987 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
Simon Goldschmidtb3672af2019-02-26 22:27:52 +0100988 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
989 gd->malloc_ptr, gd->malloc_ptr / 1024);
Hans de Goede8f280382015-09-13 15:04:17 +0200990 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
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100996 /* Get stack position: use 8-byte alignment for ABI compliance */
997 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
Nikhil M Jain799ceff2023-07-18 14:27:27 +0530998 gd->start_addr_sp = ptr;
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100999 new_gd = (gd_t *)ptr;
1000 memcpy(new_gd, (void *)gd, sizeof(gd_t));
Simon Glass1b0deee2016-11-13 14:21:58 -07001001#if CONFIG_IS_ENABLED(DM)
1002 dm_fixup_for_gd_move(new_gd);
1003#endif
Lukas Auer2a2a9252019-08-21 21:14:46 +02001004#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
Albert ARIBAUD85260bb2015-11-25 17:56:33 +01001005 gd = new_gd;
1006#endif
Simon Glassd8711af2015-03-03 08:03:00 -07001007 return ptr;
1008#else
1009 return 0;
1010#endif
1011}
Simon Glassc11c19f2019-11-14 12:57:17 -07001012
Philippe Reynes8d82a202020-12-11 19:56:47 +01001013#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)))
Simon Glassc11c19f2019-11-14 12:57:17 -07001016void bootcount_store(ulong a)
1017{
1018}
1019
1020ulong bootcount_load(void)
1021{
1022 return 0;
1023}
1024#endif