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 <config.h>
10 #include <bloblist.h>
11 #include <binman_sym.h>
12 #include <bootstage.h>
13 #include <dm.h>
14 #include <handoff.h>
15 #include <hang.h>
16 #include <init.h>
17 #include <irq_func.h>
18 #include <log.h>
19 #include <mapmem.h>
20 #include <serial.h>
21 #include <spl.h>
22 #include <spl_load.h>
23 #include <system-constants.h>
24 #include <asm/global_data.h>
25 #include <asm-generic/gpio.h>
26 #include <nand.h>
27 #include <fat.h>
28 #include <u-boot/crc.h>
29 #if CONFIG_IS_ENABLED(BANNER_PRINT)
30 #include <timestamp.h>
31 #endif
32 #include <version.h>
33 #include <image.h>
34 #include <malloc.h>
35 #include <mapmem.h>
36 #include <dm/root.h>
37 #include <dm/util.h>
38 #include <dm/device-internal.h>
39 #include <dm/uclass-internal.h>
40 #include <linux/compiler.h>
41 #include <fdt_support.h>
42 #include <bootcount.h>
43 #include <wdt.h>
44 #include <video.h>
45 
46 DECLARE_GLOBAL_DATA_PTR;
47 DECLARE_BINMAN_MAGIC_SYM;
48 
49 u32 *boot_params_ptr = NULL;
50 
51 #if CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS)
52 /* See spl.h for information about this */
53 #if defined(CONFIG_SPL_BUILD)
54 binman_sym_declare(ulong, u_boot_any, image_pos);
55 binman_sym_declare(ulong, u_boot_any, size);
56 #endif
57 
58 #ifdef CONFIG_TPL
59 binman_sym_declare(ulong, u_boot_spl_any, image_pos);
60 binman_sym_declare(ulong, u_boot_spl_any, size);
61 #endif
62 
63 #ifdef CONFIG_VPL
64 binman_sym_declare(ulong, u_boot_vpl_any, image_pos);
65 binman_sym_declare(ulong, u_boot_vpl_any, size);
66 #endif
67 
68 #endif /* BINMAN_UBOOT_SYMBOLS */
69 
70 /* Define board data structure */
71 static struct bd_info bdata __attribute__ ((section(".data")));
72 
73 #if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
74 /*
75  * Board-specific Platform code can reimplement show_boot_progress () if needed
76  */
show_boot_progress(int val)77 __weak void show_boot_progress(int val) {}
78 #endif
79 
80 #if defined(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) || \
81 	defined(CONFIG_SPL_ATF)
82 /* weak, default platform-specific function to initialize dram banks */
dram_init_banksize(void)83 __weak int dram_init_banksize(void)
84 {
85 	return 0;
86 }
87 #endif
88 
89 /*
90  * Default function to determine if u-boot or the OS should
91  * be started. This implementation always returns 1.
92  *
93  * Please implement your own board specific funcion to do this.
94  *
95  * RETURN
96  * 0 to not start u-boot
97  * positive if u-boot should start
98  */
99 #if CONFIG_IS_ENABLED(OS_BOOT)
spl_start_uboot(void)100 __weak int spl_start_uboot(void)
101 {
102 	puts(PHASE_PROMPT
103 	     "Please implement spl_start_uboot() for your board\n");
104 	puts(PHASE_PROMPT "Direct Linux boot not active!\n");
105 	return 1;
106 }
107 
108 /*
109  * Weak default function for arch specific zImage check. Return zero
110  * and fill start and end address if image is recognized.
111  */
bootz_setup(ulong image,ulong * start,ulong * end)112 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
113 {
114 	 return 1;
115 }
116 
booti_setup(ulong image,ulong * relocated_addr,ulong * size,bool force_reloc)117 int __weak booti_setup(ulong image, ulong *relocated_addr, ulong *size, bool force_reloc)
118 {
119 	 return 1;
120 }
121 #endif
122 
123 /* Weak default function for arch/board-specific fixups to the spl_image_info */
spl_perform_fixups(struct spl_image_info * spl_image)124 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
125 {
126 }
127 
spl_fixup_fdt(void * fdt_blob)128 void spl_fixup_fdt(void *fdt_blob)
129 {
130 #if defined(CONFIG_SPL_OF_LIBFDT)
131 	int err;
132 
133 	if (!fdt_blob)
134 		return;
135 
136 	err = fdt_check_header(fdt_blob);
137 	if (err < 0) {
138 		printf("fdt_root: %s\n", fdt_strerror(err));
139 		return;
140 	}
141 
142 	/* fixup the memory dt node */
143 	err = fdt_shrink_to_minimum(fdt_blob, 0);
144 	if (err == 0) {
145 		printf(PHASE_PROMPT "fdt_shrink_to_minimum err - %d\n", err);
146 		return;
147 	}
148 
149 	err = arch_fixup_fdt(fdt_blob);
150 	if (err) {
151 		printf(PHASE_PROMPT "arch_fixup_fdt err - %d\n", err);
152 		return;
153 	}
154 #endif
155 }
156 
spl_reserve_video_from_ram_top(void)157 int spl_reserve_video_from_ram_top(void)
158 {
159 	if (CONFIG_IS_ENABLED(VIDEO)) {
160 		ulong addr;
161 		int ret;
162 
163 		addr = gd->ram_top;
164 		ret = video_reserve(&addr);
165 		if (ret)
166 			return ret;
167 		debug("Reserving %luk for video at: %08lx\n",
168 		      ((unsigned long)gd->relocaddr - addr) >> 10, addr);
169 		gd->relocaddr = addr;
170 	}
171 
172 	return 0;
173 }
174 
spl_get_image_pos(void)175 ulong spl_get_image_pos(void)
176 {
177 	if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
178 		return BINMAN_SYM_MISSING;
179 
180 #ifdef CONFIG_VPL
181 	if (xpl_next_phase() == PHASE_VPL)
182 		return binman_sym(ulong, u_boot_vpl_any, image_pos);
183 #endif
184 #if defined(CONFIG_TPL) && !defined(CONFIG_VPL)
185 	if (xpl_next_phase() == PHASE_SPL)
186 		return binman_sym(ulong, u_boot_spl_any, image_pos);
187 #endif
188 #if defined(CONFIG_SPL_BUILD)
189 	return binman_sym(ulong, u_boot_any, image_pos);
190 #endif
191 
192 	return BINMAN_SYM_MISSING;
193 }
194 
spl_get_image_size(void)195 ulong spl_get_image_size(void)
196 {
197 	if (!CONFIG_IS_ENABLED(BINMAN_UBOOT_SYMBOLS))
198 		return BINMAN_SYM_MISSING;
199 
200 #ifdef CONFIG_VPL
201 	if (xpl_next_phase() == PHASE_VPL)
202 		return binman_sym(ulong, u_boot_vpl_any, size);
203 #endif
204 	return xpl_next_phase() == PHASE_SPL ?
205 		binman_sym(ulong, u_boot_spl_any, size) :
206 		binman_sym(ulong, u_boot_any, size);
207 }
208 
spl_get_image_text_base(void)209 ulong spl_get_image_text_base(void)
210 {
211 #ifdef CONFIG_VPL
212 	if (xpl_next_phase() == PHASE_VPL)
213 		return CONFIG_VPL_TEXT_BASE;
214 #endif
215 	return xpl_next_phase() == PHASE_SPL ? CONFIG_SPL_TEXT_BASE :
216 		CONFIG_TEXT_BASE;
217 }
218 
219 /*
220  * Weak default function for board specific cleanup/preparation before
221  * Linux boot. Some boards/platforms might not need it, so just provide
222  * an empty stub here.
223  */
spl_board_prepare_for_linux(void)224 __weak void spl_board_prepare_for_linux(void)
225 {
226 	/* Nothing to do! */
227 }
228 
spl_board_prepare_for_optee(void * fdt)229 __weak void spl_board_prepare_for_optee(void *fdt)
230 {
231 }
232 
spl_board_loader_name(u32 boot_device)233 __weak const char *spl_board_loader_name(u32 boot_device)
234 {
235 	return NULL;
236 }
237 
238 #if CONFIG_IS_ENABLED(OPTEE_IMAGE)
jump_to_image_optee(struct spl_image_info * spl_image)239 __weak void __noreturn jump_to_image_optee(struct spl_image_info *spl_image)
240 {
241 	spl_optee_entry(NULL, NULL, spl_image->fdt_addr,
242 			(void *)spl_image->entry_point);
243 }
244 #endif
245 
spl_board_prepare_for_boot(void)246 __weak void spl_board_prepare_for_boot(void)
247 {
248 	/* Nothing to do! */
249 }
250 
spl_get_load_buffer(ssize_t offset,size_t size)251 __weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
252 {
253 	return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
254 }
255 
spl_set_header_raw_uboot(struct spl_image_info * spl_image)256 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
257 {
258 	ulong u_boot_pos = spl_get_image_pos();
259 
260 #if CONFIG_SYS_MONITOR_LEN != 0
261 	spl_image->size = CONFIG_SYS_MONITOR_LEN;
262 #else
263 	/* Unknown U-Boot size, let's assume it will not be more than 200 KB */
264 	spl_image->size = 200 * 1024;
265 #endif
266 
267 	/*
268 	 * Binman error cases: address of the end of the previous region or the
269 	 * start of the image's entry area (usually 0) if there is no previous
270 	 * region.
271 	 */
272 	if (u_boot_pos && u_boot_pos != BINMAN_SYM_MISSING) {
273 		/* Binman does not support separated entry addresses */
274 		spl_image->entry_point = spl_get_image_text_base();
275 		spl_image->load_addr = spl_get_image_text_base();
276 		spl_image->size = spl_get_image_size();
277 		log_debug("Next load addr %lx\n", spl_image->load_addr);
278 	} else {
279 		spl_image->entry_point = CONFIG_SYS_UBOOT_START;
280 		spl_image->load_addr = CONFIG_TEXT_BASE;
281 		log_debug("Default load addr %lx (u_boot_pos=%lx)\n",
282 			  spl_image->load_addr, u_boot_pos);
283 	}
284 	spl_image->os = IH_OS_U_BOOT;
285 	spl_image->name = xpl_name(xpl_next_phase());
286 	log_debug("Next phase: %s at %lx size %lx\n", spl_image->name,
287 		  spl_image->load_addr, (ulong)spl_image->size);
288 }
289 
spl_parse_board_header(struct spl_image_info * spl_image,const struct spl_boot_device * bootdev,const void * image_header,size_t size)290 __weak int spl_parse_board_header(struct spl_image_info *spl_image,
291 				  const struct spl_boot_device *bootdev,
292 				  const void *image_header, size_t size)
293 {
294 	return -EINVAL;
295 }
296 
spl_parse_legacy_header(struct spl_image_info * spl_image,const struct legacy_img_hdr * header)297 __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
298 				   const struct legacy_img_hdr *header)
299 {
300 	/* LEGACY image not supported */
301 	debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
302 	return -EINVAL;
303 }
304 
spl_parse_image_header(struct spl_image_info * spl_image,const struct spl_boot_device * bootdev,const struct legacy_img_hdr * header)305 int spl_parse_image_header(struct spl_image_info *spl_image,
306 			   const struct spl_boot_device *bootdev,
307 			   const struct legacy_img_hdr *header)
308 {
309 	int ret;
310 
311 	if (CONFIG_IS_ENABLED(LOAD_FIT_FULL)) {
312 		ret = spl_load_fit_image(spl_image, header);
313 
314 		if (!ret)
315 			return ret;
316 	}
317 	if (image_get_magic(header) == IH_MAGIC) {
318 		int ret;
319 
320 		ret = spl_parse_legacy_header(spl_image, header);
321 		if (ret)
322 			return ret;
323 		return 0;
324 	}
325 
326 	if (IS_ENABLED(CONFIG_SPL_PANIC_ON_RAW_IMAGE)) {
327 		/*
328 		 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
329 		 * code which loads images in SPL cannot guarantee that
330 		 * absolutely all read errors will be reported.
331 		 * An example is the LPC32XX MLC NAND driver, which
332 		 * will consider that a completely unreadable NAND block
333 		 * is bad, and thus should be skipped silently.
334 		 */
335 		panic("** no mkimage signature but raw image not supported");
336 	}
337 
338 	if (CONFIG_IS_ENABLED(OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI)) {
339 		ulong start, size;
340 
341 		if (!booti_setup((ulong)header, &start, &size, 0)) {
342 			spl_image->name = "Linux";
343 			spl_image->os = IH_OS_LINUX;
344 			spl_image->load_addr = start;
345 			spl_image->entry_point = start;
346 			spl_image->size = size;
347 			debug(PHASE_PROMPT
348 			      "payload Image, load addr: 0x%lx size: %d\n",
349 			      spl_image->load_addr, spl_image->size);
350 			return 0;
351 		}
352 	} else if (CONFIG_IS_ENABLED(OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTZ)) {
353 		ulong start, end;
354 
355 		if (!bootz_setup((ulong)header, &start, &end)) {
356 			spl_image->name = "Linux";
357 			spl_image->os = IH_OS_LINUX;
358 			spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
359 			spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
360 			spl_image->size = end - start;
361 			debug(PHASE_PROMPT
362 			      "payload zImage, load addr: 0x%lx size: %d\n",
363 			      spl_image->load_addr, spl_image->size);
364 			return 0;
365 		}
366 	}
367 
368 	if (!spl_parse_board_header(spl_image, bootdev, (const void *)header,
369 				    sizeof(*header)))
370 		return 0;
371 
372 	if (IS_ENABLED(CONFIG_SPL_RAW_IMAGE_SUPPORT)) {
373 		/* Signature not found - assume u-boot.bin */
374 		debug("mkimage signature not found - ih_magic = %x\n",
375 		      header->ih_magic);
376 		spl_set_header_raw_uboot(spl_image);
377 	} else {
378 		/* RAW image not supported, proceed to other boot methods. */
379 		debug("Raw boot image support not enabled, proceeding to other boot methods\n");
380 		return -EINVAL;
381 	}
382 
383 	return 0;
384 }
385 
386 #if SPL_LOAD_USERS > 1
spl_load(struct spl_image_info * spl_image,const struct spl_boot_device * bootdev,struct spl_load_info * info,size_t size,size_t offset)387 int spl_load(struct spl_image_info *spl_image,
388 	     const struct spl_boot_device *bootdev, struct spl_load_info *info,
389 	     size_t size, size_t offset)
390 {
391 	return _spl_load(spl_image, bootdev, info, size, offset);
392 }
393 #endif
394 
jump_to_image(struct spl_image_info * spl_image)395 __weak void __noreturn jump_to_image(struct spl_image_info *spl_image)
396 {
397 	typedef void __noreturn (*image_entry_noargs_t)(void);
398 
399 	image_entry_noargs_t image_entry =
400 		(image_entry_noargs_t)spl_image->entry_point;
401 
402 	debug("image entry point: 0x%lx\n", spl_image->entry_point);
403 	image_entry();
404 }
405 
406 #if CONFIG_IS_ENABLED(HANDOFF)
407 /**
408  * Set up the SPL hand-off information
409  *
410  * This is initially empty (zero) but can be written by
411  */
setup_spl_handoff(void)412 static int setup_spl_handoff(void)
413 {
414 	struct spl_handoff *ho;
415 
416 	ho = bloblist_ensure(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
417 	if (!ho)
418 		return -ENOENT;
419 
420 	return 0;
421 }
422 
handoff_arch_save(struct spl_handoff * ho)423 __weak int handoff_arch_save(struct spl_handoff *ho)
424 {
425 	return 0;
426 }
427 
write_spl_handoff(void)428 static int write_spl_handoff(void)
429 {
430 	struct spl_handoff *ho;
431 	int ret;
432 
433 	ho = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF, sizeof(struct spl_handoff));
434 	if (!ho)
435 		return -ENOENT;
436 	handoff_save_dram(ho);
437 	ret = handoff_arch_save(ho);
438 	if (ret)
439 		return ret;
440 	debug(PHASE_PROMPT "Wrote SPL handoff\n");
441 
442 	return 0;
443 }
444 #else
setup_spl_handoff(void)445 static inline int setup_spl_handoff(void) { return 0; }
write_spl_handoff(void)446 static inline int write_spl_handoff(void) { return 0; }
447 
448 #endif /* HANDOFF */
449 
450 /**
451  * get_bootstage_id() - Get the bootstage ID to emit
452  *
453  * @start: true if this is for starting SPL, false for ending it
454  * Return: bootstage ID to use
455  */
get_bootstage_id(bool start)456 static enum bootstage_id get_bootstage_id(bool start)
457 {
458 	enum xpl_phase_t phase = xpl_phase();
459 
460 	if (IS_ENABLED(CONFIG_TPL_BUILD) && phase == PHASE_TPL)
461 		return start ? BOOTSTAGE_ID_START_TPL : BOOTSTAGE_ID_END_TPL;
462 	else if (IS_ENABLED(CONFIG_VPL_BUILD) && phase == PHASE_VPL)
463 		return start ? BOOTSTAGE_ID_START_VPL : BOOTSTAGE_ID_END_VPL;
464 	else
465 		return start ? BOOTSTAGE_ID_START_SPL : BOOTSTAGE_ID_END_SPL;
466 }
467 
spl_common_init(bool setup_malloc)468 static int spl_common_init(bool setup_malloc)
469 {
470 	int ret;
471 
472 #if CONFIG_IS_ENABLED(SYS_MALLOC_F)
473 	if (setup_malloc) {
474 #ifdef CFG_MALLOC_F_ADDR
475 		gd->malloc_base = CFG_MALLOC_F_ADDR;
476 #endif
477 		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
478 		gd->malloc_ptr = 0;
479 	}
480 #endif
481 	ret = bootstage_init(xpl_is_first_phase());
482 	if (ret) {
483 		debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
484 		      ret);
485 		return ret;
486 	}
487 	if (!xpl_is_first_phase()) {
488 		ret = bootstage_unstash_default();
489 		if (ret)
490 			log_debug("Failed to unstash bootstage: ret=%d\n", ret);
491 	}
492 	bootstage_mark_name(get_bootstage_id(true), xpl_name(xpl_phase()));
493 #if CONFIG_IS_ENABLED(LOG)
494 	ret = log_init();
495 	if (ret) {
496 		debug("%s: Failed to set up logging\n", __func__);
497 		return ret;
498 	}
499 #endif
500 	if (CONFIG_IS_ENABLED(OF_REAL)) {
501 		ret = fdtdec_setup();
502 		if (ret) {
503 			debug("fdtdec_setup() returned error %d\n", ret);
504 			return ret;
505 		}
506 	}
507 	if (CONFIG_IS_ENABLED(DM)) {
508 		bootstage_start(BOOTSTAGE_ID_ACCUM_DM_SPL,
509 				xpl_phase() == PHASE_TPL ? "dm tpl" : "dm_spl");
510 		/* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
511 		ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
512 		bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_SPL);
513 		if (ret) {
514 			debug("dm_init_and_scan() returned error %d\n", ret);
515 			return ret;
516 		}
517 
518 		ret = dm_autoprobe();
519 		if (ret)
520 			return ret;
521 	}
522 
523 	return 0;
524 }
525 
spl_set_bd(void)526 void spl_set_bd(void)
527 {
528 	/*
529 	 * NOTE: On some platforms (e.g. x86) bdata may be in flash and not
530 	 * writeable.
531 	 */
532 	if (!gd->bd)
533 		gd->bd = &bdata;
534 }
535 
spl_early_init(void)536 int spl_early_init(void)
537 {
538 	int ret;
539 
540 	debug("%s\n", __func__);
541 
542 	ret = spl_common_init(true);
543 	if (ret)
544 		return ret;
545 	gd->flags |= GD_FLG_SPL_EARLY_INIT;
546 
547 	return 0;
548 }
549 
spl_init(void)550 int spl_init(void)
551 {
552 	int ret;
553 	bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
554 			IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
555 
556 	debug("%s\n", __func__);
557 
558 	if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
559 		ret = spl_common_init(setup_malloc);
560 		if (ret)
561 			return ret;
562 	}
563 	gd->flags |= GD_FLG_SPL_INIT;
564 
565 	return 0;
566 }
567 
568 #ifndef BOOT_DEVICE_NONE
569 #define BOOT_DEVICE_NONE 0xdeadbeef
570 #endif
571 
board_boot_order(u32 * spl_boot_list)572 __weak void board_boot_order(u32 *spl_boot_list)
573 {
574 	spl_boot_list[0] = spl_boot_device();
575 }
576 
spl_check_board_image(struct spl_image_info * spl_image,const struct spl_boot_device * bootdev)577 __weak int spl_check_board_image(struct spl_image_info *spl_image,
578 				 const struct spl_boot_device *bootdev)
579 {
580 	return 0;
581 }
582 
spl_load_image(struct spl_image_info * spl_image,struct spl_image_loader * loader)583 static int spl_load_image(struct spl_image_info *spl_image,
584 			  struct spl_image_loader *loader)
585 {
586 	int ret;
587 	struct spl_boot_device bootdev;
588 
589 	bootdev.boot_device = loader->boot_device;
590 	bootdev.boot_device_name = NULL;
591 
592 	ret = loader->load_image(spl_image, &bootdev);
593 #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
594 	if (!ret && spl_image->dcrc_length) {
595 		/* check data crc */
596 		ulong dcrc = crc32_wd(0, (unsigned char *)spl_image->dcrc_data,
597 				      spl_image->dcrc_length, CHUNKSZ_CRC32);
598 		if (dcrc != spl_image->dcrc) {
599 			puts("SPL: Image data CRC check failed!\n");
600 			ret = -EINVAL;
601 		}
602 	}
603 #endif
604 	if (!ret)
605 		ret = spl_check_board_image(spl_image, &bootdev);
606 
607 	return ret;
608 }
609 
610 /**
611  * boot_from_devices() - Try loading a booting U-Boot from a list of devices
612  *
613  * @spl_image: Place to put the image details if successful
614  * @spl_boot_list: List of boot devices to try
615  * @count: Number of elements in spl_boot_list
616  * Return: 0 if OK, -ENODEV if there were no boot devices
617  *	if CONFIG_SHOW_ERRORS is enabled, returns -ENXIO if there were
618  *	devices but none worked
619  */
boot_from_devices(struct spl_image_info * spl_image,u32 spl_boot_list[],int count)620 static int boot_from_devices(struct spl_image_info *spl_image,
621 			     u32 spl_boot_list[], int count)
622 {
623 	struct spl_image_loader *drv =
624 		ll_entry_start(struct spl_image_loader, spl_image_loader);
625 	const int n_ents =
626 		ll_entry_count(struct spl_image_loader, spl_image_loader);
627 	int ret = -ENODEV;
628 	int i;
629 
630 	for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
631 		struct spl_image_loader *loader;
632 		int bootdev = spl_boot_list[i];
633 
634 		if (CONFIG_IS_ENABLED(SHOW_ERRORS))
635 			ret = -ENXIO;
636 		for (loader = drv; loader != drv + n_ents; loader++) {
637 			if (loader && bootdev != loader->boot_device)
638 				continue;
639 			if (!CONFIG_IS_ENABLED(SILENT_CONSOLE)) {
640 				if (loader)
641 					printf("Trying to boot from %s\n",
642 					       spl_loader_name(loader));
643 				else if (CONFIG_IS_ENABLED(SHOW_ERRORS)) {
644 					printf(PHASE_PROMPT
645 					       "Unsupported Boot Device %d\n",
646 					       bootdev);
647 				} else {
648 					puts(PHASE_PROMPT
649 					     "Unsupported Boot Device!\n");
650 				}
651 			}
652 			if (loader) {
653 				ret = spl_load_image(spl_image, loader);
654 				if (!ret) {
655 					spl_image->boot_device = bootdev;
656 					return 0;
657 				}
658 				printf("Error: %d\n", ret);
659 			}
660 		}
661 	}
662 
663 	return ret;
664 }
665 
666 #if defined(CONFIG_SPL_FRAMEWORK_BOARD_INIT_F)
board_init_f(ulong dummy)667 void board_init_f(ulong dummy)
668 {
669 	if (CONFIG_IS_ENABLED(OF_CONTROL)) {
670 		int ret;
671 
672 		ret = spl_early_init();
673 		if (ret) {
674 			debug("spl_early_init() failed: %d\n", ret);
675 			hang();
676 		}
677 	}
678 
679 	preloader_console_init();
680 }
681 #endif
682 
board_init_r(gd_t * dummy1,ulong dummy2)683 void board_init_r(gd_t *dummy1, ulong dummy2)
684 {
685 	u32 spl_boot_list[] = {
686 		BOOT_DEVICE_NONE,
687 		BOOT_DEVICE_NONE,
688 		BOOT_DEVICE_NONE,
689 		BOOT_DEVICE_NONE,
690 		BOOT_DEVICE_NONE,
691 	};
692 	spl_jump_to_image_t jumper = &jump_to_image;
693 	struct spl_image_info spl_image;
694 	int ret, os;
695 
696 	debug(">>" PHASE_PROMPT "board_init_r()\n");
697 
698 	spl_set_bd();
699 
700 	if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC)) {
701 		mem_malloc_init(SPL_SYS_MALLOC_START, SPL_SYS_MALLOC_SIZE);
702 		gd->flags |= GD_FLG_FULL_MALLOC_INIT;
703 	}
704 	if (!(gd->flags & GD_FLG_SPL_INIT)) {
705 		if (spl_init())
706 			hang();
707 	}
708 	timer_init();
709 	if (CONFIG_IS_ENABLED(BLOBLIST)) {
710 		ret = bloblist_init();
711 		if (ret) {
712 			debug("%s: Failed to set up bloblist: ret=%d\n",
713 			      __func__, ret);
714 			puts(PHASE_PROMPT "Cannot set up bloblist\n");
715 			hang();
716 		}
717 	}
718 	if (CONFIG_IS_ENABLED(HANDOFF)) {
719 		int ret;
720 
721 		ret = setup_spl_handoff();
722 		if (ret) {
723 			puts(PHASE_PROMPT "Cannot set up SPL handoff\n");
724 			hang();
725 		}
726 	}
727 
728 	if (CONFIG_IS_ENABLED(SOC_INIT))
729 		spl_soc_init();
730 
731 	if (IS_ENABLED(CONFIG_SPL_WATCHDOG) && CONFIG_IS_ENABLED(WDT))
732 		initr_watchdog();
733 
734 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF) ||
735 	    IS_ENABLED(CONFIG_SPL_ATF) || IS_ENABLED(CONFIG_SPL_NET))
736 		dram_init_banksize();
737 
738 	if (IS_ENABLED(CONFIG_SPL_LMB))
739 		lmb_init();
740 
741 	if (CONFIG_IS_ENABLED(PCI) && !(gd->flags & GD_FLG_DM_DEAD)) {
742 		ret = pci_init();
743 		if (ret)
744 			puts(PHASE_PROMPT "Cannot initialize PCI\n");
745 		/* Don't fail. We still can try other boot methods. */
746 	}
747 
748 	if (CONFIG_IS_ENABLED(BOARD_INIT))
749 		spl_board_init();
750 
751 	bootcount_inc();
752 
753 	/* Dump driver model states to aid analysis */
754 	if (CONFIG_IS_ENABLED(DM_STATS)) {
755 		struct dm_stats mem;
756 
757 		dm_get_mem(&mem);
758 		dm_dump_mem(&mem);
759 	}
760 
761 	memset(&spl_image, '\0', sizeof(spl_image));
762 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
763 		spl_image.arg = (void *)SPL_PAYLOAD_ARGS_ADDR;
764 	spl_image.boot_device = BOOT_DEVICE_NONE;
765 	board_boot_order(spl_boot_list);
766 
767 	ret = boot_from_devices(&spl_image, spl_boot_list,
768 				ARRAY_SIZE(spl_boot_list));
769 	if (ret) {
770 		if (CONFIG_IS_ENABLED(SHOW_ERRORS))
771 			printf(PHASE_PROMPT "failed to boot from all boot devices (err=%d)\n",
772 			       ret);
773 		else
774 			puts(PHASE_PROMPT "failed to boot from all boot devices\n");
775 		hang();
776 	}
777 
778 	spl_perform_fixups(&spl_image);
779 
780 	os = spl_image.os;
781 	if (os == IH_OS_U_BOOT) {
782 		debug("Jumping to %s...\n", xpl_name(xpl_next_phase()));
783 	} else if (CONFIG_IS_ENABLED(ATF) && os == IH_OS_ARM_TRUSTED_FIRMWARE) {
784 		debug("Jumping to U-Boot via ARM Trusted Firmware\n");
785 		spl_fixup_fdt(spl_image_fdt_addr(&spl_image));
786 		jumper = &spl_invoke_atf;
787 	} else if (CONFIG_IS_ENABLED(OPTEE_IMAGE) && os == IH_OS_TEE) {
788 		debug("Jumping to U-Boot via OP-TEE\n");
789 		spl_board_prepare_for_optee(spl_image_fdt_addr(&spl_image));
790 		jumper = &jump_to_image_optee;
791 	} else if (CONFIG_IS_ENABLED(OPENSBI) && os == IH_OS_OPENSBI) {
792 		debug("Jumping to U-Boot via RISC-V OpenSBI\n");
793 		jumper = &spl_invoke_opensbi;
794 	} else if (CONFIG_IS_ENABLED(OS_BOOT) && os == IH_OS_LINUX) {
795 		debug("Jumping to Linux\n");
796 		if (IS_ENABLED(CONFIG_SPL_OS_BOOT))
797 			spl_fixup_fdt((void *)SPL_PAYLOAD_ARGS_ADDR);
798 		spl_board_prepare_for_linux();
799 		jumper = &jump_to_image_linux;
800 	} else {
801 		debug("Unsupported OS image.. Jumping nevertheless..\n");
802 	}
803 	if (CONFIG_IS_ENABLED(SYS_MALLOC_F) &&
804 	    !IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIZE))
805 		debug("SPL malloc() used 0x%x bytes (%d KB)\n",
806 		      gd_malloc_ptr(), gd_malloc_ptr() / 1024);
807 
808 	bootstage_mark_name(get_bootstage_id(false), "end phase");
809 	ret = bootstage_stash_default();
810 	if (ret)
811 		debug("Failed to stash bootstage: err=%d\n", ret);
812 
813 	if (IS_ENABLED(CONFIG_SPL_VIDEO_REMOVE)) {
814 		struct udevice *dev;
815 		int rc;
816 
817 		rc = uclass_find_device(UCLASS_VIDEO, 0, &dev);
818 		if (!rc && dev) {
819 			rc = device_remove(dev, DM_REMOVE_NORMAL);
820 			if (rc)
821 				printf("Cannot remove video device '%s' (err=%d)\n",
822 				       dev->name, rc);
823 		}
824 	}
825 	if (CONFIG_IS_ENABLED(HANDOFF)) {
826 		ret = write_spl_handoff();
827 		if (ret)
828 			printf(PHASE_PROMPT
829 			       "SPL hand-off write failed (err=%d)\n", ret);
830 	}
831 	if (CONFIG_IS_ENABLED(UPL_OUT) && (gd->flags & GD_FLG_UPL)) {
832 		ret = spl_write_upl_handoff(&spl_image);
833 		if (ret) {
834 			printf(PHASE_PROMPT
835 			       "UPL hand-off write failed (err=%d)\n", ret);
836 			hang();
837 		}
838 	}
839 	if (CONFIG_IS_ENABLED(BLOBLIST)) {
840 		ret = bloblist_finish();
841 		if (ret)
842 			printf("Warning: Failed to finish bloblist (ret=%d)\n",
843 			       ret);
844 	}
845 
846 	spl_board_prepare_for_boot();
847 
848 	if (CONFIG_IS_ENABLED(RELOC_LOADER)) {
849 		int ret;
850 
851 		ret = spl_reloc_jump(&spl_image, jumper);
852 		if (ret) {
853 			if (xpl_phase() == PHASE_VPL)
854 				printf("jump failed %d\n", ret);
855 			hang();
856 		}
857 	}
858 
859 	jumper(&spl_image);
860 }
861 
862 /*
863  * This requires UART clocks to be enabled.  In order for this to work the
864  * caller must ensure that the gd pointer is valid.
865  */
preloader_console_init(void)866 void preloader_console_init(void)
867 {
868 #if CONFIG_IS_ENABLED(SERIAL)
869 	gd->baudrate = CONFIG_BAUDRATE;
870 
871 	serial_init();		/* serial communications setup */
872 
873 	gd->flags |= GD_FLG_HAVE_CONSOLE;
874 
875 #if CONFIG_IS_ENABLED(BANNER_PRINT)
876 	puts("\nU-Boot " PHASE_NAME " " PLAIN_VERSION " (" U_BOOT_DATE " - "
877 	     U_BOOT_TIME " " U_BOOT_TZ ")\n");
878 #endif
879 #ifdef CONFIG_SPL_DISPLAY_PRINT
880 	spl_display_print();
881 #endif
882 #endif
883 }
884 
885 /**
886  * This function is called before the stack is changed from initial stack to
887  * relocated stack. It tries to dump the stack size used
888  */
spl_relocate_stack_check(void)889 __weak void spl_relocate_stack_check(void)
890 {
891 #if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
892 	ulong init_sp = gd->start_addr_sp;
893 	ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
894 	u8 *ptr = (u8 *)stack_bottom;
895 	ulong i;
896 
897 	for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
898 		if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
899 			break;
900 		ptr++;
901 	}
902 	printf("SPL initial stack usage: %lu bytes\n",
903 	       CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
904 #endif
905 }
906 
907 /**
908  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
909  *
910  * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
911  * for the main board_init_r() execution. This is typically because we need
912  * more stack space for things like the MMC sub-system.
913  *
914  * This function calculates the stack position, copies the global_data into
915  * place, sets the new gd (except for ARM, for which setting GD within a C
916  * function may not always work) and returns the new stack position. The
917  * caller is responsible for setting up the sp register and, in the case
918  * of ARM, setting up gd.
919  *
920  * All of this is done using the same layout and alignments as done in
921  * board_init_f_init_reserve() / board_init_f_alloc_reserve().
922  *
923  * Return: new stack location, or 0 to use the same stack
924  */
spl_relocate_stack_gd(void)925 ulong spl_relocate_stack_gd(void)
926 {
927 #if CONFIG_IS_ENABLED(STACK_R)
928 	gd_t *new_gd;
929 	ulong ptr = CONFIG_SPL_STACK_R_ADDR;
930 
931 	if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
932 		spl_relocate_stack_check();
933 
934 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_IS_ENABLED(SYS_MALLOC_F)
935 	if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
936 		debug("SPL malloc() before relocation used 0x%x bytes (%d KB)\n",
937 		      gd->malloc_ptr, gd->malloc_ptr / 1024);
938 		ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
939 		gd->malloc_base = ptr;
940 		gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
941 		gd->malloc_ptr = 0;
942 	}
943 #endif
944 	/* Get stack position: use 8-byte alignment for ABI compliance */
945 	ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
946 	gd->start_addr_sp = ptr;
947 	new_gd = (gd_t *)ptr;
948 	memcpy(new_gd, (void *)gd, sizeof(gd_t));
949 #if CONFIG_IS_ENABLED(DM)
950 	dm_fixup_for_gd_move(new_gd);
951 #endif
952 #if CONFIG_IS_ENABLED(LOG)
953 	log_fixup_for_gd_move(new_gd);
954 #endif
955 #if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
956 	gd = new_gd;
957 #endif
958 	return ptr;
959 #else
960 	return 0;
961 #endif
962 }
963 
964 #if defined(CONFIG_BOOTCOUNT_LIMIT) && \
965 	((!defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)) || \
966 	 (defined(CONFIG_TPL_BUILD) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)))
bootcount_store(ulong a)967 void bootcount_store(ulong a)
968 {
969 }
970 
bootcount_load(void)971 ulong bootcount_load(void)
972 {
973 	return 0;
974 }
975 #endif
976