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