xref: /openbmc/u-boot/common/spl/spl.c (revision 23e8bd7e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010
4  * Texas Instruments, <www.ti.com>
5  *
6  * Aneesh V <aneesh@ti.com>
7  */
8 
9 #include <common.h>
10 #include <bloblist.h>
11 #include <binman_sym.h>
12 #include <dm.h>
13 #include <handoff.h>
14 #include <spl.h>
15 #include <asm/u-boot.h>
16 #include <nand.h>
17 #include <fat.h>
18 #include <version.h>
19 #include <image.h>
20 #include <malloc.h>
21 #include <dm/root.h>
22 #include <linux/compiler.h>
23 #include <fdt_support.h>
24 #include <bootcount.h>
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 #ifndef CONFIG_SYS_UBOOT_START
29 #define CONFIG_SYS_UBOOT_START	CONFIG_SYS_TEXT_BASE
30 #endif
31 #ifndef CONFIG_SYS_MONITOR_LEN
32 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
33 #define CONFIG_SYS_MONITOR_LEN	(200 * 1024)
34 #endif
35 
36 u32 *boot_params_ptr = NULL;
37 
38 /* See spl.h for information about this */
39 binman_sym_declare(ulong, u_boot_any, image_pos);
40 
41 /* Define board data structure */
42 static bd_t bdata __attribute__ ((section(".data")));
43 
44 /*
45  * Board-specific Platform code can reimplement show_boot_progress () if needed
46  */
47 __weak void show_boot_progress(int val) {}
48 
49 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF)
50 /* weak, default platform-specific function to initialize dram banks */
51 __weak int dram_init_banksize(void)
52 {
53 	return 0;
54 }
55 #endif
56 
57 /*
58  * Default function to determine if u-boot or the OS should
59  * be started. This implementation always returns 1.
60  *
61  * Please implement your own board specific funcion to do this.
62  *
63  * RETURN
64  * 0 to not start u-boot
65  * positive if u-boot should start
66  */
67 #ifdef CONFIG_SPL_OS_BOOT
68 __weak int spl_start_uboot(void)
69 {
70 	puts(SPL_TPL_PROMPT
71 	     "Please implement spl_start_uboot() for your board\n");
72 	puts(SPL_TPL_PROMPT "Direct Linux boot not active!\n");
73 	return 1;
74 }
75 
76 /*
77  * Weak default function for arch specific zImage check. Return zero
78  * and fill start and end address if image is recognized.
79  */
80 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
81 {
82 	 return 1;
83 }
84 #endif
85 
86 /* Weak default function for arch/board-specific fixups to the spl_image_info */
87 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
88 {
89 }
90 
91 void spl_fixup_fdt(void)
92 {
93 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
94 	void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
95 	int err;
96 
97 	err = fdt_check_header(fdt_blob);
98 	if (err < 0) {
99 		printf("fdt_root: %s\n", fdt_strerror(err));
100 		return;
101 	}
102 
103 	/* fixup the memory dt node */
104 	err = fdt_shrink_to_minimum(fdt_blob, 0);
105 	if (err == 0) {
106 		printf(SPL_TPL_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
107 		return;
108 	}
109 
110 	err = arch_fixup_fdt(fdt_blob);
111 	if (err) {
112 		printf(SPL_TPL_PROMPT "arch_fixup_fdt err - %d\n", err);
113 		return;
114 	}
115 #endif
116 }
117 
118 /*
119  * Weak default function for board specific cleanup/preparation before
120  * Linux boot. Some boards/platforms might not need it, so just provide
121  * an empty stub here.
122  */
123 __weak void spl_board_prepare_for_linux(void)
124 {
125 	/* Nothing to do! */
126 }
127 
128 __weak void spl_board_prepare_for_boot(void)
129 {
130 	/* Nothing to do! */
131 }
132 
133 __weak struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
134 {
135 	return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
136 }
137 
138 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
139 {
140 	ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
141 
142 	spl_image->size = CONFIG_SYS_MONITOR_LEN;
143 
144 	/*
145 	 * Binman error cases: address of the end of the previous region or the
146 	 * start of the image's entry area (usually 0) if there is no previous
147 	 * region.
148 	 */
149 	if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
150 		/* Binman does not support separated entry addresses */
151 		spl_image->entry_point = u_boot_pos;
152 		spl_image->load_addr = u_boot_pos;
153 	} else {
154 		spl_image->entry_point = CONFIG_SYS_UBOOT_START;
155 		spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
156 	}
157 	spl_image->os = IH_OS_U_BOOT;
158 	spl_image->name = "U-Boot";
159 }
160 
161 #ifdef CONFIG_SPL_LOAD_FIT_FULL
162 /* Parse and load full fitImage in SPL */
163 static int spl_load_fit_image(struct spl_image_info *spl_image,
164 			      const struct image_header *header)
165 {
166 	bootm_headers_t images;
167 	const char *fit_uname_config = NULL;
168 	const char *fit_uname_fdt = FIT_FDT_PROP;
169 	const char *uname;
170 	ulong fw_data = 0, dt_data = 0, img_data = 0;
171 	ulong fw_len = 0, dt_len = 0, img_len = 0;
172 	int idx, conf_noffset;
173 	int ret;
174 
175 #ifdef CONFIG_SPL_FIT_SIGNATURE
176 	images.verify = 1;
177 #endif
178 	ret = fit_image_load(&images, (ulong)header,
179 			     NULL, &fit_uname_config,
180 			     IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
181 			     FIT_LOAD_REQUIRED, &fw_data, &fw_len);
182 	if (ret < 0)
183 		return ret;
184 
185 	spl_image->size = fw_len;
186 	spl_image->entry_point = fw_data;
187 	spl_image->load_addr = fw_data;
188 	spl_image->os = IH_OS_U_BOOT;
189 	spl_image->name = "U-Boot";
190 
191 	debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
192 	      spl_image->name, spl_image->load_addr, spl_image->size);
193 
194 #ifdef CONFIG_SPL_FIT_SIGNATURE
195 	images.verify = 1;
196 #endif
197 	fit_image_load(&images, (ulong)header,
198 		       &fit_uname_fdt, &fit_uname_config,
199 		       IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
200 		       FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
201 
202 	conf_noffset = fit_conf_get_node((const void *)header,
203 					 fit_uname_config);
204 	if (conf_noffset <= 0)
205 		return 0;
206 
207 	for (idx = 0;
208 	     uname = fdt_stringlist_get((const void *)header, conf_noffset,
209 					FIT_LOADABLE_PROP, idx,
210 				NULL), uname;
211 	     idx++)
212 	{
213 #ifdef CONFIG_SPL_FIT_SIGNATURE
214 		images.verify = 1;
215 #endif
216 		ret = fit_image_load(&images, (ulong)header,
217 				     &uname, &fit_uname_config,
218 				     IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
219 				     FIT_LOAD_OPTIONAL_NON_ZERO,
220 				     &img_data, &img_len);
221 		if (ret < 0)
222 			return ret;
223 	}
224 
225 	return 0;
226 }
227 #endif
228 
229 int spl_parse_image_header(struct spl_image_info *spl_image,
230 			   const struct image_header *header)
231 {
232 #ifdef CONFIG_SPL_LOAD_FIT_FULL
233 	int ret = spl_load_fit_image(spl_image, header);
234 
235 	if (!ret)
236 		return ret;
237 #endif
238 	if (image_get_magic(header) == IH_MAGIC) {
239 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
240 		u32 header_size = sizeof(struct image_header);
241 
242 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
243 		/* check uImage header CRC */
244 		if (!image_check_hcrc(header)) {
245 			puts("SPL: Image header CRC check failed!\n");
246 			return -EINVAL;
247 		}
248 #endif
249 
250 		if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
251 			/*
252 			 * On some system (e.g. powerpc), the load-address and
253 			 * entry-point is located at address 0. We can't load
254 			 * to 0-0x40. So skip header in this case.
255 			 */
256 			spl_image->load_addr = image_get_load(header);
257 			spl_image->entry_point = image_get_ep(header);
258 			spl_image->size = image_get_data_size(header);
259 		} else {
260 			spl_image->entry_point = image_get_load(header);
261 			/* Load including the header */
262 			spl_image->load_addr = spl_image->entry_point -
263 				header_size;
264 			spl_image->size = image_get_data_size(header) +
265 				header_size;
266 		}
267 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
268 		/* store uImage data length and CRC to check later */
269 		spl_image->dcrc_data = image_get_load(header);
270 		spl_image->dcrc_length = image_get_data_size(header);
271 		spl_image->dcrc = image_get_dcrc(header);
272 #endif
273 
274 		spl_image->os = image_get_os(header);
275 		spl_image->name = image_get_name(header);
276 		debug(SPL_TPL_PROMPT
277 		      "payload image: %32s load addr: 0x%lx size: %d\n",
278 		      spl_image->name, spl_image->load_addr, spl_image->size);
279 #else
280 		/* LEGACY image not supported */
281 		debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
282 		return -EINVAL;
283 #endif
284 	} else {
285 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
286 		/*
287 		 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
288 		 * code which loads images in SPL cannot guarantee that
289 		 * absolutely all read errors will be reported.
290 		 * An example is the LPC32XX MLC NAND driver, which
291 		 * will consider that a completely unreadable NAND block
292 		 * is bad, and thus should be skipped silently.
293 		 */
294 		panic("** no mkimage signature but raw image not supported");
295 #endif
296 
297 #ifdef CONFIG_SPL_OS_BOOT
298 		ulong start, end;
299 
300 		if (!bootz_setup((ulong)header, &start, &end)) {
301 			spl_image->name = "Linux";
302 			spl_image->os = IH_OS_LINUX;
303 			spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
304 			spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
305 			spl_image->size = end - start;
306 			debug(SPL_TPL_PROMPT
307 			      "payload zImage, load addr: 0x%lx size: %d\n",
308 			      spl_image->load_addr, spl_image->size);
309 			return 0;
310 		}
311 #endif
312 
313 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
314 		/* Signature not found - assume u-boot.bin */
315 		debug("mkimage signature not found - ih_magic = %x\n",
316 			header->ih_magic);
317 		spl_set_header_raw_uboot(spl_image);
318 #else
319 		/* RAW image not supported, proceed to other boot methods. */
320 		debug("Raw boot image support not enabled, proceeding to other boot methods\n");
321 		return -EINVAL;
322 #endif
323 	}
324 
325 	return 0;
326 }
327 
328 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
329 {
330 	typedef void __noreturn (*image_entry_noargs_t)(void);
331 
332 	image_entry_noargs_t image_entry =
333 		(image_entry_noargs_t)spl_image->entry_point;
334 
335 	debug("image entry point: 0x%lx\n", spl_image->entry_point);
336 	image_entry();
337 }
338 
339 #if CONFIG_IS_ENABLED(HANDOFF)
340 /**
341  * Set up the SPL hand-off information
342  *
343  * This is initially empty (zero) but can be written by
344  */
345 static int setup_spl_handoff(void)
346 {
347 	struct spl_handoff *ho;
348 
349 	ho = bloblist_ensure(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
350 	if (!ho)
351 		return -ENOENT;
352 
353 	return 0;
354 }
355 
356 static int write_spl_handoff(void)
357 {
358 	struct spl_handoff *ho;
359 
360 	ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
361 	if (!ho)
362 		return -ENOENT;
363 	handoff_save_dram(ho);
364 #ifdef CONFIG_SANDBOX
365 	ho->arch.magic = TEST_HANDOFF_MAGIC;
366 #endif
367 	debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
368 
369 	return 0;
370 }
371 #else
372 static inline int setup_spl_handoff(void) { return 0; }
373 static inline int write_spl_handoff(void) { return 0; }
374 
375 #endif /* HANDOFF */
376 
377 static int spl_common_init(bool setup_malloc)
378 {
379 	int ret;
380 
381 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
382 	if (setup_malloc) {
383 #ifdef CONFIG_MALLOC_F_ADDR
384 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
385 #endif
386 		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
387 		gd->malloc_ptr = 0;
388 	}
389 #endif
390 	ret = bootstage_init(true);
391 	if (ret) {
392 		debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
393 		      ret);
394 		return ret;
395 	}
396 	bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
397 #if CONFIG_IS_ENABLED(LOG)
398 	ret = log_init();
399 	if (ret) {
400 		debug("%s: Failed to set up logging\n", __func__);
401 		return ret;
402 	}
403 #endif
404 	if (CONFIG_IS_ENABLED(BLOBLIST)) {
405 		ret = bloblist_init();
406 		if (ret) {
407 			debug("%s: Failed to set up bloblist: ret=%d\n",
408 			      __func__, ret);
409 			return ret;
410 		}
411 	}
412 	if (CONFIG_IS_ENABLED(HANDOFF)) {
413 		int ret;
414 
415 		ret = setup_spl_handoff();
416 		if (ret) {
417 			puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
418 			hang();
419 		}
420 	}
421 	if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
422 		ret = fdtdec_setup();
423 		if (ret) {
424 			debug("fdtdec_setup() returned error %d\n", ret);
425 			return ret;
426 		}
427 	}
428 	if (CONFIG_IS_ENABLED(DM)) {
429 		bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
430 		/* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
431 		ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
432 		bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
433 		if (ret) {
434 			debug("dm_init_and_scan() returned error %d\n", ret);
435 			return ret;
436 		}
437 	}
438 
439 	return 0;
440 }
441 
442 void spl_set_bd(void)
443 {
444 	/*
445 	 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
446 	 * writeable.
447 	 */
448 	if (!gd->bd)
449 		gd->bd = &bdata;
450 }
451 
452 int spl_early_init(void)
453 {
454 	int ret;
455 
456 	debug("%s\n", __func__);
457 
458 	ret = spl_common_init(true);
459 	if (ret)
460 		return ret;
461 	gd->flags |= GD_FLG_SPL_EARLY_INIT;
462 
463 	return 0;
464 }
465 
466 int spl_init(void)
467 {
468 	int ret;
469 	bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
470 			IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
471 
472 	debug("%s\n", __func__);
473 
474 	if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
475 		ret = spl_common_init(setup_malloc);
476 		if (ret)
477 			return ret;
478 	}
479 	gd->flags |= GD_FLG_SPL_INIT;
480 
481 	return 0;
482 }
483 
484 #ifndef BOOT_DEVICE_NONE
485 #define BOOT_DEVICE_NONE 0xdeadbeef
486 #endif
487 
488 __weak void board_boot_order(u32 *spl_boot_list)
489 {
490 	spl_boot_list[0] = spl_boot_device();
491 }
492 
493 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
494 {
495 	struct spl_image_loader *drv =
496 		ll_entry_start(struct spl_image_loader, spl_image_loader);
497 	const int n_ents =
498 		ll_entry_count(struct spl_image_loader, spl_image_loader);
499 	struct spl_image_loader *entry;
500 
501 	for (entry = drv; entry != drv + n_ents; entry++) {
502 		if (boot_device == entry->boot_device)
503 			return entry;
504 	}
505 
506 	/* Not found */
507 	return NULL;
508 }
509 
510 static int spl_load_image(struct spl_image_info *spl_image,
511 			  struct spl_image_loader *loader)
512 {
513 	int ret;
514 	struct spl_boot_device bootdev;
515 
516 	bootdev.boot_device = loader->boot_device;
517 	bootdev.boot_device_name = NULL;
518 
519 	ret = loader->load_image(spl_image, &bootdev);
520 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
521 	if (!ret && spl_image->dcrc_length) {
522 		/* check data crc */
523 		ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
524 				      spl_image->dcrc_length, CHUNKSZ_CRC32);
525 		if (dcrc != spl_image->dcrc) {
526 			puts("SPL: Image data CRC check failed!\n");
527 			ret = -EINVAL;
528 		}
529 	}
530 #endif
531 	return ret;
532 }
533 
534 /**
535  * boot_from_devices() - Try loading an booting U-Boot from a list of devices
536  *
537  * @spl_image: Place to put the image details if successful
538  * @spl_boot_list: List of boot devices to try
539  * @count: Number of elements in spl_boot_list
540  * @return 0 if OK, -ve on error
541  */
542 static int boot_from_devices(struct spl_image_info *spl_image,
543 			     u32 spl_boot_list[], int count)
544 {
545 	int i;
546 
547 	for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
548 		struct spl_image_loader *loader;
549 
550 		loader = spl_ll_find_loader(spl_boot_list[i]);
551 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
552 		if (loader)
553 			printf("Trying to boot from %s\n", loader->name);
554 		else
555 			puts(SPL_TPL_PROMPT "Unsupported Boot Device!\n");
556 #endif
557 		if (loader && !spl_load_image(spl_image, loader)) {
558 			spl_image->boot_device = spl_boot_list[i];
559 			return 0;
560 		}
561 	}
562 
563 	return -ENODEV;
564 }
565 
566 void board_init_r(gd_t *dummy1, ulong dummy2)
567 {
568 	u32 spl_boot_list[] = {
569 		BOOT_DEVICE_NONE,
570 		BOOT_DEVICE_NONE,
571 		BOOT_DEVICE_NONE,
572 		BOOT_DEVICE_NONE,
573 		BOOT_DEVICE_NONE,
574 	};
575 	struct spl_image_info spl_image;
576 	int ret;
577 
578 	debug(">>" SPL_TPL_PROMPT "board_init_r()\n");
579 
580 	spl_set_bd();
581 
582 #if defined(CONFIG_SYS_SPL_MALLOC_START)
583 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
584 			CONFIG_SYS_SPL_MALLOC_SIZE);
585 	gd->flags |= GD_FLG_FULL_MALLOC_INIT;
586 #endif
587 	if (!(gd->flags & GD_FLG_SPL_INIT)) {
588 		if (spl_init())
589 			hang();
590 	}
591 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
592 	/*
593 	 * timer_init() does not exist on PPC systems. The timer is initialized
594 	 * and enabled (decrementer) in interrupt_init() here.
595 	 */
596 	timer_init();
597 #endif
598 
599 #if CONFIG_IS_ENABLED(BOARD_INIT)
600 	spl_board_init();
601 #endif
602 
603 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
604 		dram_init_banksize();
605 
606 	bootcount_inc();
607 
608 	memset(&spl_image, '\0', sizeof(spl_image));
609 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
610 	spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
611 #endif
612 	spl_image.boot_device = BOOT_DEVICE_NONE;
613 	board_boot_order(spl_boot_list);
614 
615 	if (boot_from_devices(&spl_image, spl_boot_list,
616 			      ARRAY_SIZE(spl_boot_list))) {
617 		puts(SPL_TPL_PROMPT "failed to boot from all boot devices\n");
618 		hang();
619 	}
620 
621 	spl_perform_fixups(&spl_image);
622 	if (CONFIG_IS_ENABLED(HANDOFF)) {
623 		ret = write_spl_handoff();
624 		if (ret)
625 			printf(SPL_TPL_PROMPT
626 			       "SPL hand-off write failed (err=%d)\n", ret);
627 	}
628 	if (CONFIG_IS_ENABLED(BLOBLIST)) {
629 		ret = bloblist_finish();
630 		if (ret)
631 			printf("Warning: Failed to finish bloblist (ret=%d)\n",
632 			       ret);
633 	}
634 
635 #ifdef CONFIG_CPU_V7M
636 	spl_image.entry_point |= 0x1;
637 #endif
638 	switch (spl_image.os) {
639 	case IH_OS_U_BOOT:
640 		debug("Jumping to U-Boot\n");
641 		break;
642 #if CONFIG_IS_ENABLED(ATF)
643 	case IH_OS_ARM_TRUSTED_FIRMWARE:
644 		debug("Jumping to U-Boot via ARM Trusted Firmware\n");
645 		spl_invoke_atf(&spl_image);
646 		break;
647 #endif
648 #if CONFIG_IS_ENABLED(OPTEE)
649 	case IH_OS_TEE:
650 		debug("Jumping to U-Boot via OP-TEE\n");
651 		spl_optee_entry(NULL, NULL, spl_image.fdt_addr,
652 				(void *)spl_image.entry_point);
653 		break;
654 #endif
655 #ifdef CONFIG_SPL_OS_BOOT
656 	case IH_OS_LINUX:
657 		debug("Jumping to Linux\n");
658 		spl_fixup_fdt();
659 		spl_board_prepare_for_linux();
660 		jump_to_image_linux(&spl_image);
661 #endif
662 	default:
663 		debug("Unsupported OS image.. Jumping nevertheless..\n");
664 	}
665 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
666 	debug("SPL malloc() used 0x%lx bytes (%ld KB)\n", gd->malloc_ptr,
667 	      gd->malloc_ptr / 1024);
668 #endif
669 #ifdef CONFIG_BOOTSTAGE_STASH
670 	bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
671 	ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
672 			      CONFIG_BOOTSTAGE_STASH_SIZE);
673 	if (ret)
674 		debug("Failed to stash bootstage: err=%d\n", ret);
675 #endif
676 
677 	debug("loaded - jumping to U-Boot...\n");
678 	spl_board_prepare_for_boot();
679 	jump_to_image_no_args(&spl_image);
680 }
681 
682 #ifdef CONFIG_SPL_SERIAL_SUPPORT
683 /*
684  * This requires UART clocks to be enabled.  In order for this to work the
685  * caller must ensure that the gd pointer is valid.
686  */
687 void preloader_console_init(void)
688 {
689 	gd->baudrate = CONFIG_BAUDRATE;
690 
691 	serial_init();		/* serial communications setup */
692 
693 	gd->have_console = 1;
694 
695 #if CONFIG_IS_ENABLED(BANNER_PRINT)
696 	puts("\nU-Boot " SPL_TPL_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
697 	     U_BOOT_TIME " " U_BOOT_TZ ")\n");
698 #endif
699 #ifdef CONFIG_SPL_DISPLAY_PRINT
700 	spl_display_print();
701 #endif
702 }
703 #endif
704 
705 /**
706  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
707  *
708  * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
709  * for the main board_init_r() execution. This is typically because we need
710  * more stack space for things like the MMC sub-system.
711  *
712  * This function calculates the stack position, copies the global_data into
713  * place, sets the new gd (except for ARM, for which setting GD within a C
714  * function may not always work) and returns the new stack position. The
715  * caller is responsible for setting up the sp register and, in the case
716  * of ARM, setting up gd.
717  *
718  * All of this is done using the same layout and alignments as done in
719  * board_init_f_init_reserve() / board_init_f_alloc_reserve().
720  *
721  * @return new stack location, or 0 to use the same stack
722  */
723 ulong spl_relocate_stack_gd(void)
724 {
725 #ifdef CONFIG_SPL_STACK_R
726 	gd_t *new_gd;
727 	ulong ptr = CONFIG_SPL_STACK_R_ADDR;
728 
729 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
730 	if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
731 		ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
732 		gd->malloc_base = ptr;
733 		gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
734 		gd->malloc_ptr = 0;
735 	}
736 #endif
737 	/* Get stack position: use 8-byte alignment for ABI compliance */
738 	ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
739 	new_gd = (gd_t *)ptr;
740 	memcpy(new_gd, (void *)gd, sizeof(gd_t));
741 #if CONFIG_IS_ENABLED(DM)
742 	dm_fixup_for_gd_move(new_gd);
743 #endif
744 #if !defined(CONFIG_ARM)
745 	gd = new_gd;
746 #endif
747 	return ptr;
748 #else
749 	return 0;
750 #endif
751 }
752