blob: cc5507f7573d82908e9b07af3f2a37ada8d7e116 [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 Glass1801a3e2014-11-10 17:16:46 -070012#include <dm.h>
Simon Glasse14f1a22018-11-15 18:44:09 -070013#include <handoff.h>
Tom Rini28591df2012-08-13 12:03:19 -070014#include <spl.h>
Aneesh Vb8e60b92011-07-21 09:10:21 -040015#include <asm/u-boot.h>
Simon Schwarz992dcf72011-09-14 15:29:26 -040016#include <nand.h>
Aneesh V13a74c12011-07-21 09:10:27 -040017#include <fat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Simon Glass50402412011-10-10 08:55:19 +000019#include <version.h>
Aneesh V13a74c12011-07-21 09:10:27 -040020#include <image.h>
Aneesh V0560be22011-10-21 12:29:34 -040021#include <malloc.h>
Simon Glass0e864562019-10-21 17:26:52 -060022#include <mapmem.h>
Simon Glass1801a3e2014-11-10 17:16:46 -070023#include <dm/root.h>
Andreas Müller0cda7a42012-01-04 15:26:24 +000024#include <linux/compiler.h>
B, Ravi4cf02b12017-04-18 17:27:27 +053025#include <fdt_support.h>
Lukasz Majewskia4858602018-05-02 16:10:54 +020026#include <bootcount.h>
Stefan Roese502acb02019-04-11 15:58:44 +020027#include <wdt.h>
Aneesh Vb8e60b92011-07-21 09:10:21 -040028
29DECLARE_GLOBAL_DATA_PTR;
30
Stefan Roese5f169922012-08-28 10:50:59 +020031#ifndef CONFIG_SYS_UBOOT_START
32#define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
33#endif
Stefano Babicfb5d62a2012-08-23 12:46:16 +020034#ifndef CONFIG_SYS_MONITOR_LEN
Andreas Bießmannad244122014-10-25 02:54:55 +020035/* Unknown U-Boot size, let's assume it will not be more than 200 KB */
Stefano Babicfb5d62a2012-08-23 12:46:16 +020036#define CONFIG_SYS_MONITOR_LEN (200 * 1024)
37#endif
38
Tom Rini28591df2012-08-13 12:03:19 -070039u32 *boot_params_ptr = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -040040
Simon Glass867a3982017-11-13 18:55:03 -070041/* See spl.h for information about this */
Simon Glass9dcc8612018-08-01 15:22:42 -060042binman_sym_declare(ulong, u_boot_any, image_pos);
Simon Glass867a3982017-11-13 18:55:03 -070043
Tom Rini31dfba42012-08-22 15:31:05 -070044/* Define board data structure */
Aneesh Vb8e60b92011-07-21 09:10:21 -040045static bd_t bdata __attribute__ ((section(".data")));
46
Simon Schwarz305bc4c2012-03-15 04:01:38 +000047/*
Heiko Schocher747ceee2016-06-07 08:31:20 +020048 * Board-specific Platform code can reimplement show_boot_progress () if needed
49 */
50__weak void show_boot_progress(int val) {}
51
Simon Glasse14f1a22018-11-15 18:44:09 -070052#if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF)
53/* weak, default platform-specific function to initialize dram banks */
54__weak int dram_init_banksize(void)
55{
56 return 0;
57}
58#endif
59
Heiko Schocher747ceee2016-06-07 08:31:20 +020060/*
Simon Schwarz305bc4c2012-03-15 04:01:38 +000061 * Default function to determine if u-boot or the OS should
62 * be started. This implementation always returns 1.
63 *
64 * Please implement your own board specific funcion to do this.
65 *
66 * RETURN
67 * 0 to not start u-boot
68 * positive if u-boot should start
69 */
70#ifdef CONFIG_SPL_OS_BOOT
71__weak int spl_start_uboot(void)
72{
Simon Glassdbd3f112018-11-15 18:43:56 -070073 puts(SPL_TPL_PROMPT
74 "Please implement spl_start_uboot() for your board\n");
75 puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +000076 return 1;
77}
Ladislav Michl9c743722016-07-12 20:28:14 +020078
79/*
80 * Weak default function for arch specific zImage check. Return zero
81 * and fill start and end address if image is recognized.
82 */
83int __weak bootz_setup(ulong image, ulong *start, ulong *end)
84{
85 return 1;
86}
Simon Schwarz305bc4c2012-03-15 04:01:38 +000087#endif
88
Philipp Tomsich58beb6e2018-05-24 17:15:50 +020089/* Weak default function for arch/board-specific fixups to the spl_image_info */
90void __weak spl_perform_fixups(struct spl_image_info *spl_image)
91{
92}
93
B, Ravi4cf02b12017-04-18 17:27:27 +053094void spl_fixup_fdt(void)
95{
96#if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
97 void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
98 int err;
99
100 err = fdt_check_header(fdt_blob);
101 if (err < 0) {
102 printf("fdt_root: %s\n", fdt_strerror(err));
103 return;
104 }
105
106 /* fixup the memory dt node */
107 err = fdt_shrink_to_minimum(fdt_blob, 0);
108 if (err == 0) {
Simon Glassdbd3f112018-11-15 18:43:56 -0700109 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
B, Ravi4cf02b12017-04-18 17:27:27 +0530110 return;
111 }
112
113 err = arch_fixup_fdt(fdt_blob);
114 if (err) {
Simon Glassdbd3f112018-11-15 18:43:56 -0700115 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
B, Ravi4cf02b12017-04-18 17:27:27 +0530116 return;
117 }
118#endif
119}
120
Stefan Roeseec90d342012-08-23 08:34:21 +0200121/*
122 * Weak default function for board specific cleanup/preparation before
123 * Linux boot. Some boards/platforms might not need it, so just provide
124 * an empty stub here.
125 */
126__weak void spl_board_prepare_for_linux(void)
127{
128 /* Nothing to do! */
129}
130
Michal Simek7a8465b2016-05-10 07:54:20 +0200131__weak void spl_board_prepare_for_boot(void)
132{
133 /* Nothing to do! */
134}
135
Marek Vasut1dda46f2018-08-14 11:27:02 +0200136__weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
137{
138 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
139}
140
Simon Glass11a1a272016-09-24 18:19:52 -0600141void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
Heiko Schochercf000272014-10-31 08:31:00 +0100142{
Simon Glass9dcc8612018-08-01 15:22:42 -0600143 ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
Simon Glass867a3982017-11-13 18:55:03 -0700144
Simon Glass11a1a272016-09-24 18:19:52 -0600145 spl_image->size = CONFIG_SYS_MONITOR_LEN;
Miquel Raynal31824ff2018-02-28 20:51:43 +0100146
147 /*
148 * Binman error cases: address of the end of the previous region or the
149 * start of the image's entry area (usually 0) if there is no previous
150 * region.
151 */
152 if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
153 /* Binman does not support separated entry addresses */
Simon Glass867a3982017-11-13 18:55:03 -0700154 spl_image->entry_point = u_boot_pos;
155 spl_image->load_addr = u_boot_pos;
156 } else {
157 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
158 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
159 }
Simon Glass11a1a272016-09-24 18:19:52 -0600160 spl_image->os = IH_OS_U_BOOT;
161 spl_image->name = "U-Boot";
Heiko Schochercf000272014-10-31 08:31:00 +0100162}
163
Marek Vasut06623372018-05-13 00:23:17 +0200164#ifdef CONFIG_SPL_LOAD_FIT_FULL
165/* Parse and load full fitImage in SPL */
166static int spl_load_fit_image(struct spl_image_info *spl_image,
167 const struct image_header *header)
168{
169 bootm_headers_t images;
170 const char *fit_uname_config = NULL;
171 const char *fit_uname_fdt = FIT_FDT_PROP;
172 const char *uname;
173 ulong fw_data = 0, dt_data = 0, img_data = 0;
174 ulong fw_len = 0, dt_len = 0, img_len = 0;
175 int idx, conf_noffset;
176 int ret;
177
178#ifdef CONFIG_SPL_FIT_SIGNATURE
179 images.verify = 1;
180#endif
181 ret = fit_image_load(&images, (ulong)header,
182 NULL, &fit_uname_config,
183 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
184 FIT_LOAD_REQUIRED, &fw_data, &fw_len);
185 if (ret < 0)
186 return ret;
187
188 spl_image->size = fw_len;
189 spl_image->entry_point = fw_data;
190 spl_image->load_addr = fw_data;
191 spl_image->os = IH_OS_U_BOOT;
192 spl_image->name = "U-Boot";
193
Simon Glassdbd3f112018-11-15 18:43:56 -0700194 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100195 spl_image->name, spl_image->load_addr, spl_image->size);
Marek Vasut06623372018-05-13 00:23:17 +0200196
197#ifdef CONFIG_SPL_FIT_SIGNATURE
198 images.verify = 1;
199#endif
Marek Vasut187b9e32019-05-07 21:17:02 +0200200 ret = fit_image_load(&images, (ulong)header,
Marek Vasut06623372018-05-13 00:23:17 +0200201 &fit_uname_fdt, &fit_uname_config,
202 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
203 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Marek Vasut187b9e32019-05-07 21:17:02 +0200204 if (ret >= 0)
205 spl_image->fdt_addr = (void *)dt_data;
Marek Vasut06623372018-05-13 00:23:17 +0200206
207 conf_noffset = fit_conf_get_node((const void *)header,
208 fit_uname_config);
209 if (conf_noffset <= 0)
210 return 0;
211
212 for (idx = 0;
213 uname = fdt_stringlist_get((const void *)header, conf_noffset,
214 FIT_LOADABLE_PROP, idx,
215 NULL), uname;
216 idx++)
217 {
218#ifdef CONFIG_SPL_FIT_SIGNATURE
219 images.verify = 1;
220#endif
221 ret = fit_image_load(&images, (ulong)header,
222 &uname, &fit_uname_config,
223 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
224 FIT_LOAD_OPTIONAL_NON_ZERO,
225 &img_data, &img_len);
226 if (ret < 0)
227 return ret;
228 }
229
230 return 0;
231}
232#endif
233
Simon Glass6b2e4db2016-09-24 18:19:53 -0600234int spl_parse_image_header(struct spl_image_info *spl_image,
235 const struct image_header *header)
Aneesh V13a74c12011-07-21 09:10:27 -0400236{
Marek Vasut06623372018-05-13 00:23:17 +0200237#ifdef CONFIG_SPL_LOAD_FIT_FULL
238 int ret = spl_load_fit_image(spl_image, header);
239
240 if (!ret)
241 return ret;
242#endif
Stefan Roese292db772012-08-27 12:50:57 +0200243 if (image_get_magic(header) == IH_MAGIC) {
Andrew F. Davis74630342017-02-16 11:18:39 -0600244#ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
245 u32 header_size = sizeof(struct image_header);
246
Simon Goldschmidt17140472019-02-10 21:34:37 +0100247#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
248 /* check uImage header CRC */
249 if (!image_check_hcrc(header)) {
250 puts("SPL: Image header CRC check failed!\n");
251 return -EINVAL;
252 }
253#endif
254
Simon Glass6b2e4db2016-09-24 18:19:53 -0600255 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
Stefan Roese00b57b32012-08-27 12:50:58 +0200256 /*
257 * On some system (e.g. powerpc), the load-address and
258 * entry-point is located at address 0. We can't load
259 * to 0-0x40. So skip header in this case.
260 */
Simon Glass6b2e4db2016-09-24 18:19:53 -0600261 spl_image->load_addr = image_get_load(header);
262 spl_image->entry_point = image_get_ep(header);
263 spl_image->size = image_get_data_size(header);
Stefan Roese00b57b32012-08-27 12:50:58 +0200264 } else {
Simon Glass6b2e4db2016-09-24 18:19:53 -0600265 spl_image->entry_point = image_get_load(header);
Stefan Roese00b57b32012-08-27 12:50:58 +0200266 /* Load including the header */
Simon Glass6b2e4db2016-09-24 18:19:53 -0600267 spl_image->load_addr = spl_image->entry_point -
Stefan Roese00b57b32012-08-27 12:50:58 +0200268 header_size;
Simon Glass6b2e4db2016-09-24 18:19:53 -0600269 spl_image->size = image_get_data_size(header) +
Stefan Roese00b57b32012-08-27 12:50:58 +0200270 header_size;
271 }
Simon Goldschmidt17140472019-02-10 21:34:37 +0100272#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
273 /* store uImage data length and CRC to check later */
274 spl_image->dcrc_data = image_get_load(header);
275 spl_image->dcrc_length = image_get_data_size(header);
276 spl_image->dcrc = image_get_dcrc(header);
277#endif
278
Simon Glass6b2e4db2016-09-24 18:19:53 -0600279 spl_image->os = image_get_os(header);
280 spl_image->name = image_get_name(header);
Simon Glassdbd3f112018-11-15 18:43:56 -0700281 debug(SPL_TPL_PROMPT
282 "payload image: %32s load addr: 0x%lx size: %d\n",
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100283 spl_image->name, spl_image->load_addr, spl_image->size);
Andrew F. Davis74630342017-02-16 11:18:39 -0600284#else
285 /* LEGACY image not supported */
Anatolij Gustschindcb711f2017-08-01 16:17:12 +0200286 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
Andrew F. Davis74630342017-02-16 11:18:39 -0600287 return -EINVAL;
288#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400289 } else {
Albert ARIBAUD \(3ADEV\)287b0942015-03-31 11:40:50 +0200290#ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
291 /*
292 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
293 * code which loads images in SPL cannot guarantee that
294 * absolutely all read errors will be reported.
295 * An example is the LPC32XX MLC NAND driver, which
296 * will consider that a completely unreadable NAND block
297 * is bad, and thus should be skipped silently.
298 */
299 panic("** no mkimage signature but raw image not supported");
Paul Kocialkowskie48b7752016-08-24 20:04:42 +0200300#endif
301
Ladislav Michl9c743722016-07-12 20:28:14 +0200302#ifdef CONFIG_SPL_OS_BOOT
303 ulong start, end;
304
305 if (!bootz_setup((ulong)header, &start, &end)) {
Simon Glass6b2e4db2016-09-24 18:19:53 -0600306 spl_image->name = "Linux";
307 spl_image->os = IH_OS_LINUX;
308 spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
309 spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
310 spl_image->size = end - start;
Simon Glassdbd3f112018-11-15 18:43:56 -0700311 debug(SPL_TPL_PROMPT
312 "payload zImage, load addr: 0x%lx size: %d\n",
Simon Glass6b2e4db2016-09-24 18:19:53 -0600313 spl_image->load_addr, spl_image->size);
Ladislav Michl9c743722016-07-12 20:28:14 +0200314 return 0;
315 }
316#endif
Paul Kocialkowskie48b7752016-08-24 20:04:42 +0200317
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600318#ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
Aneesh V13a74c12011-07-21 09:10:27 -0400319 /* Signature not found - assume u-boot.bin */
Tom Rini7b516862012-08-14 12:06:43 -0700320 debug("mkimage signature not found - ih_magic = %x\n",
Aneesh V13a74c12011-07-21 09:10:27 -0400321 header->ih_magic);
Simon Glass6b2e4db2016-09-24 18:19:53 -0600322 spl_set_header_raw_uboot(spl_image);
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600323#else
324 /* RAW image not supported, proceed to other boot methods. */
Anatolij Gustschindcb711f2017-08-01 16:17:12 +0200325 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600326 return -EINVAL;
Albert ARIBAUD \(3ADEV\)287b0942015-03-31 11:40:50 +0200327#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400328 }
Andrew F. Davisb4be3c32017-02-16 11:18:38 -0600329
Marek Vasut02266e22016-04-29 00:44:54 +0200330 return 0;
Aneesh V13a74c12011-07-21 09:10:27 -0400331}
332
Allen Martinc67e4852012-10-19 21:08:22 +0000333__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
Aneesh V13a74c12011-07-21 09:10:27 -0400334{
SRICHARAN R3f30b0a2013-04-24 00:41:24 +0000335 typedef void __noreturn (*image_entry_noargs_t)(void);
336
Aneesh V13a74c12011-07-21 09:10:27 -0400337 image_entry_noargs_t image_entry =
Andre Przywara6c526072017-01-02 11:48:31 +0000338 (image_entry_noargs_t)spl_image->entry_point;
Aneesh V13a74c12011-07-21 09:10:27 -0400339
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100340 debug("image entry point: 0x%lx\n", spl_image->entry_point);
SRICHARAN R3f30b0a2013-04-24 00:41:24 +0000341 image_entry();
Aneesh V13a74c12011-07-21 09:10:27 -0400342}
343
Simon Glasse14f1a22018-11-15 18:44:09 -0700344#if CONFIG_IS_ENABLED(HANDOFF)
345/**
346 * Set up the SPL hand-off information
347 *
348 * This is initially empty (zero) but can be written by
349 */
350static int setup_spl_handoff(void)
351{
352 struct spl_handoff *ho;
353
354 ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
355 if (!ho)
356 return -ENOENT;
357
358 return 0;
359}
360
Simon Glassc5d27202019-09-25 08:11:18 -0600361__weak int handoff_arch_save(struct spl_handoff *ho)
362{
363 return 0;
364}
365
Simon Glasse14f1a22018-11-15 18:44:09 -0700366static int write_spl_handoff(void)
367{
368 struct spl_handoff *ho;
Simon Glassc5d27202019-09-25 08:11:18 -0600369 int ret;
Simon Glasse14f1a22018-11-15 18:44:09 -0700370
371 ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
372 if (!ho)
373 return -ENOENT;
374 handoff_save_dram(ho);
Simon Glassc5d27202019-09-25 08:11:18 -0600375 ret = handoff_arch_save(ho);
376 if (ret)
377 return ret;
Simon Glasse14f1a22018-11-15 18:44:09 -0700378 debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
379
380 return 0;
381}
382#else
383static inline int setup_spl_handoff(void) { return 0; }
384static inline int write_spl_handoff(void) { return 0; }
385
386#endif /* HANDOFF */
387
Eddie Cai32258992017-03-15 08:43:28 -0600388static int spl_common_init(bool setup_malloc)
Aneesh Vb8e60b92011-07-21 09:10:21 -0400389{
Simon Glass9a49ec12015-02-27 22:06:42 -0700390 int ret;
391
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800392#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Eddie Cai32258992017-03-15 08:43:28 -0600393 if (setup_malloc) {
Marek Vasutd61ba112016-05-25 02:14:56 +0200394#ifdef CONFIG_MALLOC_F_ADDR
Eddie Cai32258992017-03-15 08:43:28 -0600395 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
Marek Vasutd61ba112016-05-25 02:14:56 +0200396#endif
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800397 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
Eddie Cai32258992017-03-15 08:43:28 -0600398 gd->malloc_ptr = 0;
399 }
Tom Rini730bae72012-08-13 14:13:07 -0700400#endif
Simon Glass0e864562019-10-21 17:26:52 -0600401 ret = bootstage_init(u_boot_first_phase());
Simon Glass01154cb2017-05-22 05:05:35 -0600402 if (ret) {
403 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
404 ret);
405 return ret;
406 }
Simon Glass0e864562019-10-21 17:26:52 -0600407#ifdef CONFIG_BOOTSTAGE_STASH
408 if (!u_boot_first_phase()) {
409 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
410 CONFIG_BOOTSTAGE_STASH_SIZE);
411
412 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
413 if (ret)
414 debug("%s: Failed to unstash bootstage: ret=%d\n",
415 __func__, ret);
416 }
417#endif /* CONFIG_BOOTSTAGE_STASH */
Simon Glassaa2abc42019-10-21 17:26:51 -0600418 bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_START_TPL :
419 BOOTSTAGE_ID_START_SPL, SPL_TPL_NAME);
Simon Glassb56cdc02018-11-15 18:43:49 -0700420#if CONFIG_IS_ENABLED(LOG)
421 ret = log_init();
422 if (ret) {
423 debug("%s: Failed to set up logging\n", __func__);
424 return ret;
425 }
426#endif
Simon Glassbb948382016-07-04 11:57:56 -0600427 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass9a49ec12015-02-27 22:06:42 -0700428 ret = fdtdec_setup();
429 if (ret) {
430 debug("fdtdec_setup() returned error %d\n", ret);
Simon Glassfa2a4db2015-06-23 15:39:10 -0600431 return ret;
Simon Glass9a49ec12015-02-27 22:06:42 -0700432 }
433 }
Philipp Tomsich60e15782017-06-30 19:02:53 +0200434 if (CONFIG_IS_ENABLED(DM)) {
Simon Glassaa2abc42019-10-21 17:26:51 -0600435 bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL,
436 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
Tom Rini9732f792017-01-14 12:20:23 -0500437 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
Simon Glasse9559fb2016-07-04 11:58:14 -0600438 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
Simon Glass01154cb2017-05-22 05:05:35 -0600439 bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
Simon Glass9a49ec12015-02-27 22:06:42 -0700440 if (ret) {
441 debug("dm_init_and_scan() returned error %d\n", ret);
Simon Glassfa2a4db2015-06-23 15:39:10 -0600442 return ret;
Simon Glass9a49ec12015-02-27 22:06:42 -0700443 }
444 }
Eddie Cai32258992017-03-15 08:43:28 -0600445
446 return 0;
447}
448
York Suna34ca5f2017-09-28 08:42:10 -0700449void spl_set_bd(void)
450{
Simon Glassef8d48a2018-11-15 18:43:58 -0700451 /*
452 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
453 * writeable.
454 */
York Suna34ca5f2017-09-28 08:42:10 -0700455 if (!gd->bd)
456 gd->bd = &bdata;
457}
458
Eddie Cai32258992017-03-15 08:43:28 -0600459int spl_early_init(void)
460{
461 int ret;
462
Simon Goldschmidt7dbffe42018-08-13 11:24:05 +0200463 debug("%s\n", __func__);
464
Eddie Cai32258992017-03-15 08:43:28 -0600465 ret = spl_common_init(true);
466 if (ret)
467 return ret;
468 gd->flags |= GD_FLG_SPL_EARLY_INIT;
469
470 return 0;
471}
472
473int spl_init(void)
474{
475 int ret;
Tom Rini89ea21e2017-03-20 14:19:27 -0400476 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
477 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
Eddie Cai32258992017-03-15 08:43:28 -0600478
Simon Goldschmidt7dbffe42018-08-13 11:24:05 +0200479 debug("%s\n", __func__);
480
Eddie Cai32258992017-03-15 08:43:28 -0600481 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
Tom Rini89ea21e2017-03-20 14:19:27 -0400482 ret = spl_common_init(setup_malloc);
Eddie Cai32258992017-03-15 08:43:28 -0600483 if (ret)
484 return ret;
485 }
Simon Glassfa2a4db2015-06-23 15:39:10 -0600486 gd->flags |= GD_FLG_SPL_INIT;
487
488 return 0;
489}
490
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200491#ifndef BOOT_DEVICE_NONE
492#define BOOT_DEVICE_NONE 0xdeadbeef
493#endif
494
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200495__weak void board_boot_order(u32 *spl_boot_list)
496{
497 spl_boot_list[0] = spl_boot_device();
498}
499
Simon Glassd1cf3732016-09-24 18:19:58 -0600500static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
501{
502 struct spl_image_loader *drv =
503 ll_entry_start(struct spl_image_loader, spl_image_loader);
504 const int n_ents =
505 ll_entry_count(struct spl_image_loader, spl_image_loader);
506 struct spl_image_loader *entry;
507
508 for (entry = drv; entry != drv + n_ents; entry++) {
509 if (boot_device == entry->boot_device)
510 return entry;
511 }
512
513 /* Not found */
514 return NULL;
515}
516
Simon Glassf1e55752016-11-30 15:30:52 -0700517static int spl_load_image(struct spl_image_info *spl_image,
518 struct spl_image_loader *loader)
Simon Glassfa2a4db2015-06-23 15:39:10 -0600519{
Simon Goldschmidt17140472019-02-10 21:34:37 +0100520 int ret;
Simon Glassc0b6c9b2016-09-24 18:19:57 -0600521 struct spl_boot_device bootdev;
522
Simon Glassf1e55752016-11-30 15:30:52 -0700523 bootdev.boot_device = loader->boot_device;
Simon Glassc0b6c9b2016-09-24 18:19:57 -0600524 bootdev.boot_device_name = NULL;
525
Simon Goldschmidt17140472019-02-10 21:34:37 +0100526 ret = loader->load_image(spl_image, &bootdev);
527#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
528 if (!ret && spl_image->dcrc_length) {
529 /* check data crc */
530 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
531 spl_image->dcrc_length, CHUNKSZ_CRC32);
532 if (dcrc != spl_image->dcrc) {
533 puts("SPL: Image data CRC check failed!\n");
534 ret = -EINVAL;
535 }
536 }
537#endif
538 return ret;
Simon Glassc9ef1852016-11-30 15:30:51 -0700539}
540
541/**
Miquel Raynal9f8d9942019-05-07 14:18:43 +0200542 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
Simon Glassc9ef1852016-11-30 15:30:51 -0700543 *
544 * @spl_image: Place to put the image details if successful
545 * @spl_boot_list: List of boot devices to try
546 * @count: Number of elements in spl_boot_list
547 * @return 0 if OK, -ve on error
548 */
549static int boot_from_devices(struct spl_image_info *spl_image,
550 u32 spl_boot_list[], int count)
551{
552 int i;
553
554 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
555 struct spl_image_loader *loader;
556
Simon Glassc9ef1852016-11-30 15:30:51 -0700557 loader = spl_ll_find_loader(spl_boot_list[i]);
Stefan Roesece21aa12014-11-11 19:03:55 +0100558#if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
Simon Glass2b530f92016-11-30 15:30:53 -0700559 if (loader)
Andrew F. Davis07009932017-01-12 10:19:55 -0600560 printf("Trying to boot from %s\n", loader->name);
Simon Glass2b530f92016-11-30 15:30:53 -0700561 else
Simon Glassdbd3f112018-11-15 18:43:56 -0700562 puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
Stefan Roesece21aa12014-11-11 19:03:55 +0100563#endif
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200564 if (loader && !spl_load_image(spl_image, loader)) {
565 spl_image->boot_device = spl_boot_list[i];
Simon Glassc9ef1852016-11-30 15:30:51 -0700566 return 0;
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200567 }
Simon Glassc9ef1852016-11-30 15:30:51 -0700568 }
569
Simon Glassa4996482016-09-24 18:20:12 -0600570 return -ENODEV;
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200571}
572
Philippe Reynesc4510f42019-09-19 16:18:39 +0200573#if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
574void board_init_f(ulong dummy)
575{
576 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
577 int ret;
578
579 ret = spl_early_init();
580 if (ret) {
581 debug("spl_early_init() failed: %d\n", ret);
582 hang();
583 }
584 }
585
586 if (CONFIG_IS_ENABLED(SERIAL_SUPPORT))
587 preloader_console_init();
588}
589#endif
590
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200591void board_init_r(gd_t *dummy1, ulong dummy2)
592{
Simon Glassce8f5092016-09-24 18:20:17 -0600593 u32 spl_boot_list[] = {
594 BOOT_DEVICE_NONE,
595 BOOT_DEVICE_NONE,
596 BOOT_DEVICE_NONE,
597 BOOT_DEVICE_NONE,
598 BOOT_DEVICE_NONE,
599 };
600 struct spl_image_info spl_image;
Simon Glass4e0a52d2018-11-15 18:43:51 -0700601 int ret;
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200602
Simon Glassdbd3f112018-11-15 18:43:56 -0700603 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
York Suna34ca5f2017-09-28 08:42:10 -0700604
605 spl_set_bd();
606
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200607#if defined(CONFIG_SYS_SPL_MALLOC_START)
608 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
609 CONFIG_SYS_SPL_MALLOC_SIZE);
610 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
611#endif
612 if (!(gd->flags & GD_FLG_SPL_INIT)) {
613 if (spl_init())
614 hang();
615 }
Anatolij Gustschin28674c92017-08-28 17:46:33 +0200616#if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200617 /*
618 * timer_init() does not exist on PPC systems. The timer is initialized
619 * and enabled (decrementer) in interrupt_init() here.
620 */
621 timer_init();
622#endif
Simon Glassf1207752019-09-25 08:11:19 -0600623 if (CONFIG_IS_ENABLED(BLOBLIST)) {
624 ret = bloblist_init();
625 if (ret) {
626 debug("%s: Failed to set up bloblist: ret=%d\n",
627 __func__, ret);
628 puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
629 hang();
630 }
631 }
632 if (CONFIG_IS_ENABLED(HANDOFF)) {
633 int ret;
634
635 ret = setup_spl_handoff();
636 if (ret) {
637 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
638 hang();
639 }
640 }
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200641
Kever Yang2b5994b2018-01-20 18:00:26 +0800642#if CONFIG_IS_ENABLED(BOARD_INIT)
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200643 spl_board_init();
644#endif
645
Marek Vasut55ec91b2019-06-09 03:46:21 +0200646#if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
Stefan Roese502acb02019-04-11 15:58:44 +0200647 initr_watchdog();
648#endif
649
Simon Glasse14f1a22018-11-15 18:44:09 -0700650 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
651 dram_init_banksize();
652
Lukasz Majewskia4858602018-05-02 16:10:54 +0200653 bootcount_inc();
654
Simon Glassce8f5092016-09-24 18:20:17 -0600655 memset(&spl_image, '\0', sizeof(spl_image));
Vikas Manocha62b021c2017-04-07 15:38:13 -0700656#ifdef CONFIG_SYS_SPL_ARGS_ADDR
657 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
658#endif
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200659 spl_image.boot_device = BOOT_DEVICE_NONE;
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200660 board_boot_order(spl_boot_list);
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200661
Simon Glassc9ef1852016-11-30 15:30:51 -0700662 if (boot_from_devices(&spl_image, spl_boot_list,
663 ARRAY_SIZE(spl_boot_list))) {
Simon Glassdbd3f112018-11-15 18:43:56 -0700664 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200665 hang();
Nikita Kiryanov3ed94782015-11-08 17:11:51 +0200666 }
Nikita Kiryanov0a978812015-11-08 17:11:50 +0200667
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200668 spl_perform_fixups(&spl_image);
Simon Glasse14f1a22018-11-15 18:44:09 -0700669 if (CONFIG_IS_ENABLED(HANDOFF)) {
670 ret = write_spl_handoff();
671 if (ret)
672 printf(SPL_TPL_PROMPT
673 "SPL hand-off write failed (err=%d)\n", ret);
674 }
Simon Glass4e0a52d2018-11-15 18:43:51 -0700675 if (CONFIG_IS_ENABLED(BLOBLIST)) {
676 ret = bloblist_finish();
677 if (ret)
678 printf("Warning: Failed to finish bloblist (ret=%d)\n",
679 ret);
680 }
Philipp Tomsich58beb6e2018-05-24 17:15:50 +0200681
Vikas Manocha11dd7062017-05-28 12:55:08 -0700682#ifdef CONFIG_CPU_V7M
683 spl_image.entry_point |= 0x1;
684#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400685 switch (spl_image.os) {
Aneesh V13a74c12011-07-21 09:10:27 -0400686 case IH_OS_U_BOOT:
687 debug("Jumping to U-Boot\n");
Aneesh V13a74c12011-07-21 09:10:27 -0400688 break;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200689#if CONFIG_IS_ENABLED(ATF)
690 case IH_OS_ARM_TRUSTED_FIRMWARE:
691 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
692 spl_invoke_atf(&spl_image);
693 break;
694#endif
Kever Yanga28d42c2018-08-23 17:17:59 +0800695#if CONFIG_IS_ENABLED(OPTEE)
696 case IH_OS_TEE:
697 debug("Jumping to U-Boot via OP-TEE\n");
698 spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
699 (void *)spl_image.entry_point);
700 break;
701#endif
Lukas Auer515b9342019-08-21 21:14:44 +0200702#if CONFIG_IS_ENABLED(OPENSBI)
703 case IH_OS_OPENSBI:
704 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
705 spl_invoke_opensbi(&spl_image);
706 break;
707#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000708#ifdef CONFIG_SPL_OS_BOOT
709 case IH_OS_LINUX:
710 debug("Jumping to Linux\n");
B, Ravi4cf02b12017-04-18 17:27:27 +0530711 spl_fixup_fdt();
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000712 spl_board_prepare_for_linux();
Vikas Manocha62b021c2017-04-07 15:38:13 -0700713 jump_to_image_linux(&spl_image);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000714#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400715 default:
Tom Rini79178462012-08-27 14:58:28 -0700716 debug("Unsupported OS image.. Jumping nevertheless..\n");
Aneesh V13a74c12011-07-21 09:10:27 -0400717 }
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800718#if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
Simon Goldschmidt1ddade62018-11-02 21:49:52 +0100719 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
Simon Glassb2d69942014-11-10 17:16:45 -0700720 gd->malloc_ptr / 1024);
721#endif
Simon Glassaa2abc42019-10-21 17:26:51 -0600722 bootstage_mark_name(spl_phase() == PHASE_TPL ? BOOTSTAGE_ID_END_TPL :
723 BOOTSTAGE_ID_END_SPL, "end " SPL_TPL_NAME);
Simon Glass01154cb2017-05-22 05:05:35 -0600724#ifdef CONFIG_BOOTSTAGE_STASH
Simon Glass01154cb2017-05-22 05:05:35 -0600725 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
726 CONFIG_BOOTSTAGE_STASH_SIZE);
727 if (ret)
728 debug("Failed to stash bootstage: err=%d\n", ret);
729#endif
Kever Yangaca62db2017-09-13 18:24:24 +0800730
Kever Yangaca62db2017-09-13 18:24:24 +0800731 debug("loaded - jumping to U-Boot...\n");
Michal Simek7a8465b2016-05-10 07:54:20 +0200732 spl_board_prepare_for_boot();
Allen Martinc67e4852012-10-19 21:08:22 +0000733 jump_to_image_no_args(&spl_image);
Aneesh Vb8e60b92011-07-21 09:10:21 -0400734}
735
Alex Kiernanb09f8922018-04-19 04:32:51 +0000736#ifdef CONFIG_SPL_SERIAL_SUPPORT
Tom Rini31dfba42012-08-22 15:31:05 -0700737/*
738 * This requires UART clocks to be enabled. In order for this to work the
739 * caller must ensure that the gd pointer is valid.
740 */
Aneesh Vb8e60b92011-07-21 09:10:21 -0400741void preloader_console_init(void)
742{
Aneesh Vb8e60b92011-07-21 09:10:21 -0400743 gd->baudrate = CONFIG_BAUDRATE;
744
Aneesh Vb8e60b92011-07-21 09:10:21 -0400745 serial_init(); /* serial communications setup */
746
Ilya Yanok74193c62011-11-01 13:16:03 +0000747 gd->have_console = 1;
748
Simon Glass6324cce2018-11-15 18:43:57 -0700749#if CONFIG_IS_ENABLED(BANNER_PRINT)
Simon Glassdbd3f112018-11-15 18:43:56 -0700750 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
751 U_BOOT_TIME " " U_BOOT_TZ ")\n");
Anatolij Gustschin6fe74d22018-01-25 18:45:22 +0100752#endif
Tom Rinife3b0c72012-08-13 11:37:56 -0700753#ifdef CONFIG_SPL_DISPLAY_PRINT
754 spl_display_print();
755#endif
Tom Rini09c797b2011-10-04 04:59:23 +0000756}
Alex Kiernanb09f8922018-04-19 04:32:51 +0000757#endif
Simon Glassd8711af2015-03-03 08:03:00 -0700758
759/**
Simon Goldschmidt0b820852019-07-16 22:30:36 +0200760 * This function is called before the stack is changed from initial stack to
761 * relocated stack. It tries to dump the stack size used
762 */
763__weak void spl_relocate_stack_check(void)
764{
765#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
766 ulong init_sp = gd->start_addr_sp;
767 ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
768 u8 *ptr = (u8 *)stack_bottom;
769 ulong i;
770
771 for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
772 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
773 break;
774 ptr++;
775 }
776 printf("SPL initial stack usage: %lu bytes\n",
777 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
778#endif
779}
780
781/**
Simon Glassd8711af2015-03-03 08:03:00 -0700782 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
783 *
784 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
785 * for the main board_init_r() execution. This is typically because we need
786 * more stack space for things like the MMC sub-system.
787 *
788 * This function calculates the stack position, copies the global_data into
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100789 * place, sets the new gd (except for ARM, for which setting GD within a C
790 * function may not always work) and returns the new stack position. The
791 * caller is responsible for setting up the sp register and, in the case
792 * of ARM, setting up gd.
793 *
794 * All of this is done using the same layout and alignments as done in
795 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
Simon Glassd8711af2015-03-03 08:03:00 -0700796 *
797 * @return new stack location, or 0 to use the same stack
798 */
799ulong spl_relocate_stack_gd(void)
800{
801#ifdef CONFIG_SPL_STACK_R
802 gd_t *new_gd;
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100803 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
Simon Glassd8711af2015-03-03 08:03:00 -0700804
Simon Goldschmidt0b820852019-07-16 22:30:36 +0200805 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
806 spl_relocate_stack_check();
807
Philipp Tomsich10139352017-07-28 11:06:03 +0200808#if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
Hans de Goede8f280382015-09-13 15:04:17 +0200809 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
Simon Goldschmidtb3672af2019-02-26 22:27:52 +0100810 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
811 gd->malloc_ptr, gd->malloc_ptr / 1024);
Hans de Goede8f280382015-09-13 15:04:17 +0200812 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
813 gd->malloc_base = ptr;
814 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
815 gd->malloc_ptr = 0;
816 }
817#endif
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100818 /* Get stack position: use 8-byte alignment for ABI compliance */
819 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
820 new_gd = (gd_t *)ptr;
821 memcpy(new_gd, (void *)gd, sizeof(gd_t));
Simon Glass1b0deee2016-11-13 14:21:58 -0700822#if CONFIG_IS_ENABLED(DM)
823 dm_fixup_for_gd_move(new_gd);
824#endif
Lukas Auer2a2a9252019-08-21 21:14:46 +0200825#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
Albert ARIBAUD85260bb2015-11-25 17:56:33 +0100826 gd = new_gd;
827#endif
Simon Glassd8711af2015-03-03 08:03:00 -0700828 return ptr;
829#else
830 return 0;
831#endif
832}