blob: 33119e95b89b4bd2fbbfc72a1974f15175b178eb [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>
Simon Glass401d1c42020-10-30 21:38:53 -060022#include <asm/global_data.h>
Aneesh Vbcae7212011-07-21 09:10:21 -040023#include <asm/u-boot.h>
Simon Schwarzbb085b82011-09-14 15:29:26 -040024#include <nand.h>
Aneesh V8cf686e2011-07-21 09:10:27 -040025#include <fat.h>
Simon Glass3db71102019-11-14 12:57:16 -070026#include <u-boot/crc.h>
Simon Glassefb21722011-10-10 08:55:19 +000027#include <version.h>
Aneesh V8cf686e2011-07-21 09:10:27 -040028#include <image.h>
Aneesh V2d01dd92011-10-21 12:29:34 -040029#include <malloc.h>
Simon Glass31f9f0e2019-10-21 17:26:52 -060030#include <mapmem.h>
Simon Glass11516512014-11-10 17:16:46 -070031#include <dm/root.h>
Andreas Müller761ca312012-01-04 15:26:24 +000032#include <linux/compiler.h>
B, Ravi6e7585b2017-04-18 17:27:27 +053033#include <fdt_support.h>
Lukasz Majewskia8be2492018-05-02 16:10:54 +020034#include <bootcount.h>
Stefan Roese06985282019-04-11 15:58:44 +020035#include <wdt.h>
Aneesh Vbcae7212011-07-21 09:10:21 -040036
37DECLARE_GLOBAL_DATA_PTR;
38
Stefan Roese3c6f8a02012-08-28 10:50:59 +020039#ifndef CONFIG_SYS_UBOOT_START
40#define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
41#endif
Stefano Babicae83d882012-08-23 12:46:16 +020042#ifndef CONFIG_SYS_MONITOR_LEN
Andreas Bießmanne76caa62014-10-25 02:54:55 +020043/* Unknown U-Boot size, let's assume it will not be more than 200 KB */
Stefano Babicae83d882012-08-23 12:46:16 +020044#define CONFIG_SYS_MONITOR_LEN (200 * 1024)
45#endif
46
Tom Rini47f7bca2012-08-13 12:03:19 -070047u32 *boot_params_ptr = NULL;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040048
Simon Glass8bee2d22017-11-13 18:55:03 -070049/* See spl.h for information about this */
Simon Glassdbf6be92018-08-01 15:22:42 -060050binman_sym_declare(ulong, u_boot_any, image_pos);
Simon Glasse82c6242019-12-08 17:40:12 -070051binman_sym_declare(ulong, u_boot_any, size);
52
53#ifdef CONFIG_TPL
54binman_sym_declare(ulong, spl, image_pos);
55binman_sym_declare(ulong, spl, size);
56#endif
Simon Glass8bee2d22017-11-13 18:55:03 -070057
Simon Schwarz379c19a2012-03-15 04:01:38 +000058/*
Heiko Schocher496c5482016-06-07 08:31:20 +020059 * Board-specific Platform code can reimplement show_boot_progress () if needed
60 */
61__weak void show_boot_progress(int val) {}
62
Heiko Stuebnera343b4f2020-05-25 19:57:25 +020063#if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
64 defined(CONFIG_SPL_ATF)
Simon Glassb0edea32018-11-15 18:44:09 -070065/* weak, default platform-specific function to initialize dram banks */
66__weak int dram_init_banksize(void)
67{
68 return 0;
69}
70#endif
71
Heiko Schocher496c5482016-06-07 08:31:20 +020072/*
Simon Schwarz379c19a2012-03-15 04:01:38 +000073 * Default function to determine if u-boot or the OS should
74 * be started. This implementation always returns 1.
75 *
76 * Please implement your own board specific funcion to do this.
77 *
78 * RETURN
79 * 0 to not start u-boot
80 * positive if u-boot should start
81 */
82#ifdef CONFIG_SPL_OS_BOOT
83__weak int spl_start_uboot(void)
84{
Simon Glassd6330062018-11-15 18:43:56 -070085 puts(SPL_TPL_PROMPT
86 "Please implement spl_start_uboot() for your board\n");
87 puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +000088 return 1;
89}
Ladislav Michl431889d2016-07-12 20:28:14 +020090
91/*
92 * Weak default function for arch specific zImage check. Return zero
93 * and fill start and end address if image is recognized.
94 */
95int __weak bootz_setup(ulong image, ulong *start, ulong *end)
96{
97 return 1;
98}
Simon Schwarz379c19a2012-03-15 04:01:38 +000099#endif
100
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200101/* Weak default function for arch/board-specific fixups to the spl_image_info */
102void __weak spl_perform_fixups(struct spl_image_info *spl_image)
103{
104}
105
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200106void spl_fixup_fdt(void *fdt_blob)
B, Ravi6e7585b2017-04-18 17:27:27 +0530107{
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200108#if defined(CONFIG_SPL_OF_LIBFDT)
B, Ravi6e7585b2017-04-18 17:27:27 +0530109 int err;
110
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200111 if (!fdt_blob)
112 return;
113
B, Ravi6e7585b2017-04-18 17:27:27 +0530114 err = fdt_check_header(fdt_blob);
115 if (err < 0) {
116 printf("fdt_root: %s\n", fdt_strerror(err));
117 return;
118 }
119
120 /* fixup the memory dt node */
121 err = fdt_shrink_to_minimum(fdt_blob, 0);
122 if (err == 0) {
Simon Glassd6330062018-11-15 18:43:56 -0700123 printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
B, Ravi6e7585b2017-04-18 17:27:27 +0530124 return;
125 }
126
127 err = arch_fixup_fdt(fdt_blob);
128 if (err) {
Simon Glassd6330062018-11-15 18:43:56 -0700129 printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
B, Ravi6e7585b2017-04-18 17:27:27 +0530130 return;
131 }
132#endif
133}
134
Simon Glasse82c6242019-12-08 17:40:12 -0700135ulong spl_get_image_pos(void)
136{
137 return spl_phase() == PHASE_TPL ?
138 binman_sym(ulong, spl, image_pos) :
139 binman_sym(ulong, u_boot_any, image_pos);
140}
141
142ulong spl_get_image_size(void)
143{
144 return spl_phase() == PHASE_TPL ?
145 binman_sym(ulong, spl, size) :
146 binman_sym(ulong, u_boot_any, size);
147}
148
Simon Glass86c372a2021-01-24 10:06:03 -0700149ulong spl_get_image_text_base(void)
150{
151 return spl_phase() == PHASE_TPL ? CONFIG_SPL_TEXT_BASE :
152 CONFIG_SYS_TEXT_BASE;
153}
154
Stefan Roeseea8256f2012-08-23 08:34:21 +0200155/*
156 * Weak default function for board specific cleanup/preparation before
157 * Linux boot. Some boards/platforms might not need it, so just provide
158 * an empty stub here.
159 */
160__weak void spl_board_prepare_for_linux(void)
161{
162 /* Nothing to do! */
163}
164
Michal Simek3a3b9142016-05-10 07:54:20 +0200165__weak void spl_board_prepare_for_boot(void)
166{
167 /* Nothing to do! */
168}
169
Marek Vasut04ce5422018-08-14 11:27:02 +0200170__weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
171{
Simon Glass891d9e82021-03-07 17:35:14 -0700172 return map_sysmem(CONFIG_SYS_TEXT_BASE + offset, 0);
Marek Vasut04ce5422018-08-14 11:27:02 +0200173}
174
Simon Glassd95ceb92016-09-24 18:19:52 -0600175void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100176{
Simon Glassdbf6be92018-08-01 15:22:42 -0600177 ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
Simon Glass8bee2d22017-11-13 18:55:03 -0700178
Simon Glassd95ceb92016-09-24 18:19:52 -0600179 spl_image->size = CONFIG_SYS_MONITOR_LEN;
Miquel Raynal55fe0e22018-02-28 20:51:43 +0100180
181 /*
182 * Binman error cases: address of the end of the previous region or the
183 * start of the image's entry area (usually 0) if there is no previous
184 * region.
185 */
186 if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
187 /* Binman does not support separated entry addresses */
Simon Glass8bee2d22017-11-13 18:55:03 -0700188 spl_image->entry_point = u_boot_pos;
189 spl_image->load_addr = u_boot_pos;
190 } else {
191 spl_image->entry_point = CONFIG_SYS_UBOOT_START;
192 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
193 }
Simon Glassd95ceb92016-09-24 18:19:52 -0600194 spl_image->os = IH_OS_U_BOOT;
195 spl_image->name = "U-Boot";
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100196}
197
Marek Vasut8a9dc162018-05-13 00:23:17 +0200198#ifdef CONFIG_SPL_LOAD_FIT_FULL
199/* Parse and load full fitImage in SPL */
200static int spl_load_fit_image(struct spl_image_info *spl_image,
201 const struct image_header *header)
202{
203 bootm_headers_t images;
204 const char *fit_uname_config = NULL;
Marek Vasut8a9dc162018-05-13 00:23:17 +0200205 const char *uname;
206 ulong fw_data = 0, dt_data = 0, img_data = 0;
207 ulong fw_len = 0, dt_len = 0, img_len = 0;
208 int idx, conf_noffset;
209 int ret;
210
211#ifdef CONFIG_SPL_FIT_SIGNATURE
212 images.verify = 1;
213#endif
214 ret = fit_image_load(&images, (ulong)header,
215 NULL, &fit_uname_config,
216 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
217 FIT_LOAD_REQUIRED, &fw_data, &fw_len);
218 if (ret < 0)
219 return ret;
220
221 spl_image->size = fw_len;
222 spl_image->entry_point = fw_data;
223 spl_image->load_addr = fw_data;
224 spl_image->os = IH_OS_U_BOOT;
225 spl_image->name = "U-Boot";
226
Simon Glassd6330062018-11-15 18:43:56 -0700227 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
Simon Goldschmidt30c07402018-11-02 21:49:52 +0100228 spl_image->name, spl_image->load_addr, spl_image->size);
Marek Vasut8a9dc162018-05-13 00:23:17 +0200229
230#ifdef CONFIG_SPL_FIT_SIGNATURE
231 images.verify = 1;
232#endif
Alexandru Gagniuc18fd6632021-04-01 13:25:26 -0500233 ret = fit_image_load(&images, (ulong)header, NULL, &fit_uname_config,
Marek Vasut8a9dc162018-05-13 00:23:17 +0200234 IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
235 FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Marek Vasuta9a82712019-05-07 21:17:02 +0200236 if (ret >= 0)
237 spl_image->fdt_addr = (void *)dt_data;
Marek Vasut8a9dc162018-05-13 00:23:17 +0200238
239 conf_noffset = fit_conf_get_node((const void *)header,
240 fit_uname_config);
241 if (conf_noffset <= 0)
242 return 0;
243
244 for (idx = 0;
245 uname = fdt_stringlist_get((const void *)header, conf_noffset,
246 FIT_LOADABLE_PROP, idx,
247 NULL), uname;
248 idx++)
249 {
250#ifdef CONFIG_SPL_FIT_SIGNATURE
251 images.verify = 1;
252#endif
253 ret = fit_image_load(&images, (ulong)header,
254 &uname, &fit_uname_config,
255 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
256 FIT_LOAD_OPTIONAL_NON_ZERO,
257 &img_data, &img_len);
258 if (ret < 0)
259 return ret;
260 }
261
262 return 0;
263}
264#endif
265
Stefan Roesec1108172020-04-21 09:28:41 +0200266__weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
267 const struct image_header *header)
268{
269 /* LEGACY image not supported */
270 debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
271 return -EINVAL;
272}
273
Simon Glass71316c12016-09-24 18:19:53 -0600274int spl_parse_image_header(struct spl_image_info *spl_image,
275 const struct image_header *header)
Aneesh V8cf686e2011-07-21 09:10:27 -0400276{
Marek Vasut8a9dc162018-05-13 00:23:17 +0200277#ifdef CONFIG_SPL_LOAD_FIT_FULL
278 int ret = spl_load_fit_image(spl_image, header);
279
280 if (!ret)
281 return ret;
282#endif
Stefan Roese77552b02012-08-27 12:50:57 +0200283 if (image_get_magic(header) == IH_MAGIC) {
Stefan Roesec1108172020-04-21 09:28:41 +0200284 int ret;
Andrew F. Davis722a6b12017-02-16 11:18:39 -0600285
Stefan Roesec1108172020-04-21 09:28:41 +0200286 ret = spl_parse_legacy_header(spl_image, header);
287 if (ret)
288 return ret;
Aneesh V8cf686e2011-07-21 09:10:27 -0400289 } else {
Albert ARIBAUD \(3ADEV\)8c80eb32015-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 Kocialkowski85a37722016-08-24 20:04:42 +0200300#endif
301
Ladislav Michl431889d2016-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 Glass71316c12016-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 Glassd6330062018-11-15 18:43:56 -0700311 debug(SPL_TPL_PROMPT
312 "payload zImage, load addr: 0x%lx size: %d\n",
Simon Glass71316c12016-09-24 18:19:53 -0600313 spl_image->load_addr, spl_image->size);
Ladislav Michl431889d2016-07-12 20:28:14 +0200314 return 0;
315 }
316#endif
Paul Kocialkowski85a37722016-08-24 20:04:42 +0200317
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600318#ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
Aneesh V8cf686e2011-07-21 09:10:27 -0400319 /* Signature not found - assume u-boot.bin */
Tom Rini026b2fe2012-08-14 12:06:43 -0700320 debug("mkimage signature not found - ih_magic = %x\n",
Aneesh V8cf686e2011-07-21 09:10:27 -0400321 header->ih_magic);
Simon Glass71316c12016-09-24 18:19:53 -0600322 spl_set_header_raw_uboot(spl_image);
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600323#else
324 /* RAW image not supported, proceed to other boot methods. */
Anatolij Gustschin2d2531b2017-08-01 16:17:12 +0200325 debug("Raw boot image support not enabled, proceeding to other boot methods\n");
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600326 return -EINVAL;
Albert ARIBAUD \(3ADEV\)8c80eb32015-03-31 11:40:50 +0200327#endif
Aneesh V8cf686e2011-07-21 09:10:27 -0400328 }
Andrew F. Davis24eb39b2017-02-16 11:18:38 -0600329
Marek Vasut7e0f2262016-04-29 00:44:54 +0200330 return 0;
Aneesh V8cf686e2011-07-21 09:10:27 -0400331}
332
Allen Martina759f1e2012-10-19 21:08:22 +0000333__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
Aneesh V8cf686e2011-07-21 09:10:27 -0400334{
SRICHARAN R4a0eb752013-04-24 00:41:24 +0000335 typedef void __noreturn (*image_entry_noargs_t)(void);
336
Aneesh V8cf686e2011-07-21 09:10:27 -0400337 image_entry_noargs_t image_entry =
Andre Przywara11e14792017-01-02 11:48:31 +0000338 (image_entry_noargs_t)spl_image->entry_point;
Aneesh V8cf686e2011-07-21 09:10:27 -0400339
Simon Goldschmidt30c07402018-11-02 21:49:52 +0100340 debug("image entry point: 0x%lx\n", spl_image->entry_point);
SRICHARAN R4a0eb752013-04-24 00:41:24 +0000341 image_entry();
Aneesh V8cf686e2011-07-21 09:10:27 -0400342}
343
Simon Glassb0edea32018-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 Glass366291a2019-09-25 08:11:18 -0600361__weak int handoff_arch_save(struct spl_handoff *ho)
362{
363 return 0;
364}
365
Simon Glassb0edea32018-11-15 18:44:09 -0700366static int write_spl_handoff(void)
367{
368 struct spl_handoff *ho;
Simon Glass366291a2019-09-25 08:11:18 -0600369 int ret;
Simon Glassb0edea32018-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 Glass366291a2019-09-25 08:11:18 -0600375 ret = handoff_arch_save(ho);
376 if (ret)
377 return ret;
Simon Glassb0edea32018-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
Simon Glass05e3a0d2021-03-15 18:11:15 +1300388/**
389 * get_bootstage_id() - Get the bootstage ID to emit
390 *
391 * @start: true if this is for starting SPL, false for ending it
392 * @return bootstage ID to use
393 */
394static enum bootstage_id get_bootstage_id(bool start)
395{
396 enum u_boot_phase phase = spl_phase();
397
398 if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
399 return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
400 else
401 return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
402}
403
Eddie Cai340f4182017-03-15 08:43:28 -0600404static int spl_common_init(bool setup_malloc)
Aneesh Vbcae7212011-07-21 09:10:21 -0400405{
Simon Glassf3d46bd2015-02-27 22:06:42 -0700406 int ret;
407
Andy Yanf1896c42017-07-24 17:43:34 +0800408#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Eddie Cai340f4182017-03-15 08:43:28 -0600409 if (setup_malloc) {
Marek Vasut94b9e222016-05-25 02:14:56 +0200410#ifdef CONFIG_MALLOC_F_ADDR
Eddie Cai340f4182017-03-15 08:43:28 -0600411 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
Marek Vasut94b9e222016-05-25 02:14:56 +0200412#endif
Andy Yanf1896c42017-07-24 17:43:34 +0800413 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
Eddie Cai340f4182017-03-15 08:43:28 -0600414 gd->malloc_ptr = 0;
415 }
Tom Rini24dafad2012-08-13 14:13:07 -0700416#endif
Simon Glass31f9f0e2019-10-21 17:26:52 -0600417 ret = bootstage_init(u_boot_first_phase());
Simon Glass824bb1b2017-05-22 05:05:35 -0600418 if (ret) {
419 debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
420 ret);
421 return ret;
422 }
Simon Glass31f9f0e2019-10-21 17:26:52 -0600423#ifdef CONFIG_BOOTSTAGE_STASH
424 if (!u_boot_first_phase()) {
425 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
426 CONFIG_BOOTSTAGE_STASH_SIZE);
427
428 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
429 if (ret)
430 debug("%s: Failed to unstash bootstage: ret=%d\n",
431 __func__, ret);
432 }
433#endif /* CONFIG_BOOTSTAGE_STASH */
Simon Glass05e3a0d2021-03-15 18:11:15 +1300434 bootstage_mark_name(get_bootstage_id(true),
435 spl_phase_name(spl_phase()));
Simon Glass4d8d3052018-11-15 18:43:49 -0700436#if CONFIG_IS_ENABLED(LOG)
437 ret = log_init();
438 if (ret) {
439 debug("%s: Failed to set up logging\n", __func__);
440 return ret;
441 }
442#endif
Simon Glassd223e0a2016-07-04 11:57:56 -0600443 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glassf3d46bd2015-02-27 22:06:42 -0700444 ret = fdtdec_setup();
445 if (ret) {
446 debug("fdtdec_setup() returned error %d\n", ret);
Simon Glass070d00b2015-06-23 15:39:10 -0600447 return ret;
Simon Glassf3d46bd2015-02-27 22:06:42 -0700448 }
449 }
Philipp Tomsichf1c6e192017-06-30 19:02:53 +0200450 if (CONFIG_IS_ENABLED(DM)) {
Simon Glassb67eefd2020-05-10 11:39:59 -0600451 bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
Simon Glass5256bee2019-10-21 17:26:51 -0600452 spl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
Tom Rini7f73ca42017-01-14 12:20:23 -0500453 /* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
Simon Glass7d23b9c2016-07-04 11:58:14 -0600454 ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
Simon Glassb67eefd2020-05-10 11:39:59 -0600455 bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
Simon Glassf3d46bd2015-02-27 22:06:42 -0700456 if (ret) {
457 debug("dm_init_and_scan() returned error %d\n", ret);
Simon Glass070d00b2015-06-23 15:39:10 -0600458 return ret;
Simon Glassf3d46bd2015-02-27 22:06:42 -0700459 }
460 }
Eddie Cai340f4182017-03-15 08:43:28 -0600461
462 return 0;
463}
464
Simon Glass38d6b7e2020-12-22 19:30:21 -0700465int spl_alloc_bd(void)
York Sund1fc0a32017-09-28 08:42:10 -0700466{
Simon Glassc21f4072018-11-15 18:43:58 -0700467 /*
468 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
469 * writeable.
470 */
Simon Glass38d6b7e2020-12-22 19:30:21 -0700471 if (!gd->bd) {
472 gd->bd = malloc(sizeof(*gd->bd));
473 if (!gd->bd)
474 return -ENOMEM;
475 }
476
477 return 0;
York Sund1fc0a32017-09-28 08:42:10 -0700478}
479
Eddie Cai340f4182017-03-15 08:43:28 -0600480int spl_early_init(void)
481{
482 int ret;
483
Simon Goldschmidt94cb9862018-08-13 11:24:05 +0200484 debug("%s\n", __func__);
485
Eddie Cai340f4182017-03-15 08:43:28 -0600486 ret = spl_common_init(true);
487 if (ret)
488 return ret;
489 gd->flags |= GD_FLG_SPL_EARLY_INIT;
490
491 return 0;
492}
493
494int spl_init(void)
495{
496 int ret;
Tom Rinicf334ed2017-03-20 14:19:27 -0400497 bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
498 IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
Eddie Cai340f4182017-03-15 08:43:28 -0600499
Simon Goldschmidt94cb9862018-08-13 11:24:05 +0200500 debug("%s\n", __func__);
501
Eddie Cai340f4182017-03-15 08:43:28 -0600502 if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
Tom Rinicf334ed2017-03-20 14:19:27 -0400503 ret = spl_common_init(setup_malloc);
Eddie Cai340f4182017-03-15 08:43:28 -0600504 if (ret)
505 return ret;
506 }
Simon Glass070d00b2015-06-23 15:39:10 -0600507 gd->flags |= GD_FLG_SPL_INIT;
Aneesh V2d01dd92011-10-21 12:29:34 -0400508
Simon Glass070d00b2015-06-23 15:39:10 -0600509 return 0;
510}
511
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200512#ifndef BOOT_DEVICE_NONE
513#define BOOT_DEVICE_NONE 0xdeadbeef
514#endif
515
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200516__weak void board_boot_order(u32 *spl_boot_list)
517{
518 spl_boot_list[0] = spl_boot_device();
519}
520
Simon Glassa0a80292016-09-24 18:19:58 -0600521static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
522{
523 struct spl_image_loader *drv =
524 ll_entry_start(struct spl_image_loader, spl_image_loader);
525 const int n_ents =
526 ll_entry_count(struct spl_image_loader, spl_image_loader);
527 struct spl_image_loader *entry;
528
529 for (entry = drv; entry != drv + n_ents; entry++) {
530 if (boot_device == entry->boot_device)
531 return entry;
532 }
533
534 /* Not found */
535 return NULL;
536}
537
Simon Glass29d357d2016-11-30 15:30:52 -0700538static int spl_load_image(struct spl_image_info *spl_image,
539 struct spl_image_loader *loader)
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200540{
Simon Goldschmidtdae5c2d2019-02-10 21:34:37 +0100541 int ret;
Simon Glassecdfd692016-09-24 18:19:57 -0600542 struct spl_boot_device bootdev;
543
Simon Glass29d357d2016-11-30 15:30:52 -0700544 bootdev.boot_device = loader->boot_device;
Simon Glassecdfd692016-09-24 18:19:57 -0600545 bootdev.boot_device_name = NULL;
546
Simon Goldschmidtdae5c2d2019-02-10 21:34:37 +0100547 ret = loader->load_image(spl_image, &bootdev);
548#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
549 if (!ret && spl_image->dcrc_length) {
550 /* check data crc */
551 ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
552 spl_image->dcrc_length, CHUNKSZ_CRC32);
553 if (dcrc != spl_image->dcrc) {
554 puts("SPL: Image data CRC check failed!\n");
555 ret = -EINVAL;
556 }
557 }
558#endif
559 return ret;
Simon Glass540bfe72016-11-30 15:30:51 -0700560}
561
562/**
Miquel Raynalfd4ee982019-05-07 14:18:43 +0200563 * boot_from_devices() - Try loading a booting U-Boot from a list of devices
Simon Glass540bfe72016-11-30 15:30:51 -0700564 *
565 * @spl_image: Place to put the image details if successful
566 * @spl_boot_list: List of boot devices to try
567 * @count: Number of elements in spl_boot_list
568 * @return 0 if OK, -ve on error
569 */
570static int boot_from_devices(struct spl_image_info *spl_image,
571 u32 spl_boot_list[], int count)
572{
573 int i;
574
575 for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
576 struct spl_image_loader *loader;
577
Simon Glass540bfe72016-11-30 15:30:51 -0700578 loader = spl_ll_find_loader(spl_boot_list[i]);
Otavio Salvadorf9b9b772020-09-03 14:25:15 -0300579#if defined(CONFIG_SPL_SERIAL_SUPPORT) \
580 && defined(CONFIG_SPL_LIBCOMMON_SUPPORT) \
581 && !defined(CONFIG_SILENT_CONSOLE)
Simon Glass2acf35d2016-11-30 15:30:53 -0700582 if (loader)
Andrew F. Daviscf947da2017-01-12 10:19:55 -0600583 printf("Trying to boot from %s\n", loader->name);
Simon Glass2acf35d2016-11-30 15:30:53 -0700584 else
Simon Glassd6330062018-11-15 18:43:56 -0700585 puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200586#endif
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200587 if (loader && !spl_load_image(spl_image, loader)) {
588 spl_image->boot_device = spl_boot_list[i];
Simon Glass540bfe72016-11-30 15:30:51 -0700589 return 0;
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200590 }
Simon Glass540bfe72016-11-30 15:30:51 -0700591 }
592
Simon Glass97d9df02016-09-24 18:20:12 -0600593 return -ENODEV;
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200594}
595
Philippe Reynesa9a3aad2019-09-19 16:18:39 +0200596#if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
597void board_init_f(ulong dummy)
598{
599 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
600 int ret;
601
602 ret = spl_early_init();
603 if (ret) {
604 debug("spl_early_init() failed: %d\n", ret);
605 hang();
606 }
607 }
608
Samuel Holland3988be52020-05-07 18:08:10 -0500609 preloader_console_init();
Philippe Reynesa9a3aad2019-09-19 16:18:39 +0200610}
611#endif
612
Simon Glass070d00b2015-06-23 15:39:10 -0600613void board_init_r(gd_t *dummy1, ulong dummy2)
614{
Simon Glassd32b2d12016-09-24 18:20:17 -0600615 u32 spl_boot_list[] = {
616 BOOT_DEVICE_NONE,
617 BOOT_DEVICE_NONE,
618 BOOT_DEVICE_NONE,
619 BOOT_DEVICE_NONE,
620 BOOT_DEVICE_NONE,
621 };
622 struct spl_image_info spl_image;
Simon Glasse945a722018-11-15 18:43:51 -0700623 int ret;
Simon Glass070d00b2015-06-23 15:39:10 -0600624
Simon Glassd6330062018-11-15 18:43:56 -0700625 debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
York Sund1fc0a32017-09-28 08:42:10 -0700626
Simon Glass070d00b2015-06-23 15:39:10 -0600627#if defined(CONFIG_SYS_SPL_MALLOC_START)
628 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
629 CONFIG_SYS_SPL_MALLOC_SIZE);
630 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
631#endif
632 if (!(gd->flags & GD_FLG_SPL_INIT)) {
633 if (spl_init())
634 hang();
635 }
Simon Glass38d6b7e2020-12-22 19:30:21 -0700636 if (IS_ENABLED(CONFIG_SPL_ALLOC_BD) && spl_alloc_bd()) {
637 puts("Cannot alloc bd\n");
638 hang();
639 }
Anatolij Gustschinf9d42d82017-08-28 17:46:33 +0200640#if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
Stefan Roeseea8256f2012-08-23 08:34:21 +0200641 /*
642 * timer_init() does not exist on PPC systems. The timer is initialized
643 * and enabled (decrementer) in interrupt_init() here.
644 */
Ilya Yanok4063c772012-09-17 10:26:26 +0000645 timer_init();
Stefan Roeseea8256f2012-08-23 08:34:21 +0200646#endif
Simon Glass3cd71982019-09-25 08:11:19 -0600647 if (CONFIG_IS_ENABLED(BLOBLIST)) {
648 ret = bloblist_init();
649 if (ret) {
650 debug("%s: Failed to set up bloblist: ret=%d\n",
651 __func__, ret);
652 puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
653 hang();
654 }
655 }
656 if (CONFIG_IS_ENABLED(HANDOFF)) {
657 int ret;
658
659 ret = setup_spl_handoff();
660 if (ret) {
661 puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
662 hang();
663 }
664 }
Ilya Yanok4063c772012-09-17 10:26:26 +0000665
Kever Yangaf2f4422018-01-20 18:00:26 +0800666#if CONFIG_IS_ENABLED(BOARD_INIT)
Tom Riniee08a822011-11-23 05:13:06 +0000667 spl_board_init();
668#endif
669
Marek Vasut6874cb72019-06-09 03:46:21 +0200670#if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
Stefan Roese06985282019-04-11 15:58:44 +0200671 initr_watchdog();
672#endif
673
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200674 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
675 IS_ENABLED(CONFIG_SPL_ATF))
Simon Glassb0edea32018-11-15 18:44:09 -0700676 dram_init_banksize();
677
Lukasz Majewskia8be2492018-05-02 16:10:54 +0200678 bootcount_inc();
679
Simon Glassd32b2d12016-09-24 18:20:17 -0600680 memset(&spl_image, '\0', sizeof(spl_image));
Vikas Manocha5bf52502017-04-07 15:38:13 -0700681#ifdef CONFIG_SYS_SPL_ARGS_ADDR
682 spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
683#endif
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200684 spl_image.boot_device = BOOT_DEVICE_NONE;
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200685 board_boot_order(spl_boot_list);
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200686
Simon Glass540bfe72016-11-30 15:30:51 -0700687 if (boot_from_devices(&spl_image, spl_boot_list,
688 ARRAY_SIZE(spl_boot_list))) {
Simon Glassd6330062018-11-15 18:43:56 -0700689 puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400690 hang();
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200691 }
Aneesh V8cf686e2011-07-21 09:10:27 -0400692
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200693 spl_perform_fixups(&spl_image);
Simon Glassb0edea32018-11-15 18:44:09 -0700694 if (CONFIG_IS_ENABLED(HANDOFF)) {
695 ret = write_spl_handoff();
696 if (ret)
697 printf(SPL_TPL_PROMPT
698 "SPL hand-off write failed (err=%d)\n", ret);
699 }
Simon Glasse945a722018-11-15 18:43:51 -0700700 if (CONFIG_IS_ENABLED(BLOBLIST)) {
701 ret = bloblist_finish();
702 if (ret)
703 printf("Warning: Failed to finish bloblist (ret=%d)\n",
704 ret);
705 }
Philipp Tomsichde5dd4c2018-05-24 17:15:50 +0200706
Vikas Manocha6bcdd662017-05-28 12:55:08 -0700707#ifdef CONFIG_CPU_V7M
708 spl_image.entry_point |= 0x1;
709#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400710 switch (spl_image.os) {
Aneesh V8cf686e2011-07-21 09:10:27 -0400711 case IH_OS_U_BOOT:
Simon Glass1a9e75b2021-02-06 09:57:27 -0700712 debug("Jumping to %s...\n", spl_phase_name(spl_next_phase()));
Aneesh V8cf686e2011-07-21 09:10:27 -0400713 break;
Philipp Tomsich1d379092017-09-13 21:29:35 +0200714#if CONFIG_IS_ENABLED(ATF)
715 case IH_OS_ARM_TRUSTED_FIRMWARE:
716 debug("Jumping to U-Boot via ARM Trusted Firmware\n");
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200717 spl_fixup_fdt(spl_image.fdt_addr);
Philipp Tomsich1d379092017-09-13 21:29:35 +0200718 spl_invoke_atf(&spl_image);
719 break;
720#endif
Kever Yang70fe2872018-08-23 17:17:59 +0800721#if CONFIG_IS_ENABLED(OPTEE)
722 case IH_OS_TEE:
723 debug("Jumping to U-Boot via OP-TEE\n");
724 spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
725 (void *)spl_image.entry_point);
726 break;
727#endif
Lukas Auer5e30e452019-08-21 21:14:44 +0200728#if CONFIG_IS_ENABLED(OPENSBI)
729 case IH_OS_OPENSBI:
730 debug("Jumping to U-Boot via RISC-V OpenSBI\n");
731 spl_invoke_opensbi(&spl_image);
732 break;
733#endif
Simon Schwarz379c19a2012-03-15 04:01:38 +0000734#ifdef CONFIG_SPL_OS_BOOT
735 case IH_OS_LINUX:
736 debug("Jumping to Linux\n");
Heiko Stuebnera343b4f2020-05-25 19:57:25 +0200737#if defined(CONFIG_SYS_SPL_ARGS_ADDR)
738 spl_fixup_fdt((void *)CONFIG_SYS_SPL_ARGS_ADDR);
739#endif
Simon Schwarz379c19a2012-03-15 04:01:38 +0000740 spl_board_prepare_for_linux();
Vikas Manocha5bf52502017-04-07 15:38:13 -0700741 jump_to_image_linux(&spl_image);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000742#endif
Aneesh V8cf686e2011-07-21 09:10:27 -0400743 default:
Tom Rini42120982012-08-27 14:58:28 -0700744 debug("Unsupported OS image.. Jumping nevertheless..\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400745 }
Andy Yanf1896c42017-07-24 17:43:34 +0800746#if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
Simon Goldschmidt30c07402018-11-02 21:49:52 +0100747 debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
Simon Glassfb4f5e72014-11-10 17:16:45 -0700748 gd->malloc_ptr / 1024);
749#endif
Simon Glass05e3a0d2021-03-15 18:11:15 +1300750 bootstage_mark_name(get_bootstage_id(false), "end phase");
Simon Glass824bb1b2017-05-22 05:05:35 -0600751#ifdef CONFIG_BOOTSTAGE_STASH
Simon Glass824bb1b2017-05-22 05:05:35 -0600752 ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
753 CONFIG_BOOTSTAGE_STASH_SIZE);
754 if (ret)
755 debug("Failed to stash bootstage: err=%d\n", ret);
756#endif
Kever Yanga8c51122017-09-13 18:24:24 +0800757
Michal Simek3a3b9142016-05-10 07:54:20 +0200758 spl_board_prepare_for_boot();
Allen Martina759f1e2012-10-19 21:08:22 +0000759 jump_to_image_no_args(&spl_image);
Aneesh Vbcae7212011-07-21 09:10:21 -0400760}
761
Tom Rini6507f132012-08-22 15:31:05 -0700762/*
763 * This requires UART clocks to be enabled. In order for this to work the
764 * caller must ensure that the gd pointer is valid.
765 */
Aneesh Vbcae7212011-07-21 09:10:21 -0400766void preloader_console_init(void)
767{
Samuel Holland3988be52020-05-07 18:08:10 -0500768#ifdef CONFIG_SPL_SERIAL_SUPPORT
Aneesh Vbcae7212011-07-21 09:10:21 -0400769 gd->baudrate = CONFIG_BAUDRATE;
770
Aneesh Vbcae7212011-07-21 09:10:21 -0400771 serial_init(); /* serial communications setup */
772
Ilya Yanokb88befa2011-11-01 13:16:03 +0000773 gd->have_console = 1;
774
Simon Glassaedc08b2018-11-15 18:43:57 -0700775#if CONFIG_IS_ENABLED(BANNER_PRINT)
Simon Glassd6330062018-11-15 18:43:56 -0700776 puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
777 U_BOOT_TIME " " U_BOOT_TZ ")\n");
Anatolij Gustschin0292bc02018-01-25 18:45:22 +0100778#endif
Tom Rini861a86f2012-08-13 11:37:56 -0700779#ifdef CONFIG_SPL_DISPLAY_PRINT
780 spl_display_print();
781#endif
Alex Kiernan117a0e02018-04-19 04:32:51 +0000782#endif
Samuel Holland3988be52020-05-07 18:08:10 -0500783}
Simon Glassdb910352015-03-03 08:03:00 -0700784
785/**
Simon Goldschmidtd8c03322019-07-16 22:30:36 +0200786 * This function is called before the stack is changed from initial stack to
787 * relocated stack. It tries to dump the stack size used
788 */
789__weak void spl_relocate_stack_check(void)
790{
791#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
792 ulong init_sp = gd->start_addr_sp;
793 ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
794 u8 *ptr = (u8 *)stack_bottom;
795 ulong i;
796
797 for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
798 if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
799 break;
800 ptr++;
801 }
802 printf("SPL initial stack usage: %lu bytes\n",
803 CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
804#endif
805}
806
807/**
Simon Glassdb910352015-03-03 08:03:00 -0700808 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
809 *
810 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
811 * for the main board_init_r() execution. This is typically because we need
812 * more stack space for things like the MMC sub-system.
813 *
814 * This function calculates the stack position, copies the global_data into
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100815 * place, sets the new gd (except for ARM, for which setting GD within a C
816 * function may not always work) and returns the new stack position. The
817 * caller is responsible for setting up the sp register and, in the case
818 * of ARM, setting up gd.
819 *
820 * All of this is done using the same layout and alignments as done in
821 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
Simon Glassdb910352015-03-03 08:03:00 -0700822 *
823 * @return new stack location, or 0 to use the same stack
824 */
825ulong spl_relocate_stack_gd(void)
826{
827#ifdef CONFIG_SPL_STACK_R
828 gd_t *new_gd;
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100829 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
Simon Glassdb910352015-03-03 08:03:00 -0700830
Simon Goldschmidtd8c03322019-07-16 22:30:36 +0200831 if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
832 spl_relocate_stack_check();
833
Philipp Tomsichae2cee22017-07-28 11:06:03 +0200834#if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
Hans de Goededcfcb8d2015-09-13 15:04:17 +0200835 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
Simon Goldschmidt438dcab2019-02-26 22:27:52 +0100836 debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
837 gd->malloc_ptr, gd->malloc_ptr / 1024);
Hans de Goededcfcb8d2015-09-13 15:04:17 +0200838 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
839 gd->malloc_base = ptr;
840 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
841 gd->malloc_ptr = 0;
842 }
843#endif
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100844 /* Get stack position: use 8-byte alignment for ABI compliance */
845 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
846 new_gd = (gd_t *)ptr;
847 memcpy(new_gd, (void *)gd, sizeof(gd_t));
Simon Glass2f11cd92016-11-13 14:21:58 -0700848#if CONFIG_IS_ENABLED(DM)
849 dm_fixup_for_gd_move(new_gd);
850#endif
Lukas Auerc7e1eff2019-08-21 21:14:46 +0200851#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100852 gd = new_gd;
853#endif
Simon Glassdb910352015-03-03 08:03:00 -0700854 return ptr;
855#else
856 return 0;
857#endif
858}
Simon Glassc6604442019-11-14 12:57:17 -0700859
Philippe Reynes4d145f22020-12-11 19:56:47 +0100860#if defined(CONFIG_BOOTCOUNT_LIMIT) && \
861 ((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
862 (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
Simon Glassc6604442019-11-14 12:57:17 -0700863void bootcount_store(ulong a)
864{
865}
866
867ulong bootcount_load(void)
868{
869 return 0;
870}
871#endif