1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 Stefan Roese <sr@denx.de>
4  */
5 
6 #include <config.h>
7 #include <image.h>
8 #include <imx_container.h>
9 #include <log.h>
10 #include <spl.h>
11 #include <spl_load.h>
12 
spl_nor_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)13 static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
14 			       ulong count, void *buf)
15 {
16 	debug("%s: sector %lx, count %lx, buf %p\n",
17 	      __func__, sector, count, buf);
18 	memcpy(buf, map_sysmem(sector, count), count);
19 
20 	return count;
21 }
22 
spl_nor_get_uboot_base(void)23 unsigned long __weak spl_nor_get_uboot_base(void)
24 {
25 	return CFG_SYS_UBOOT_BASE;
26 }
27 
spl_nor_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)28 static int spl_nor_load_image(struct spl_image_info *spl_image,
29 			      struct spl_boot_device *bootdev)
30 {
31 	struct spl_load_info load;
32 
33 	/*
34 	 * Loading of the payload to SDRAM is done with skipping of
35 	 * the mkimage header in this SPL NOR driver
36 	 */
37 	spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
38 
39 #if CONFIG_IS_ENABLED(OS_BOOT)
40 	if (!spl_start_uboot()) {
41 		/*
42 		 * Load Linux from its location in NOR flash to its defined
43 		 * location in SDRAM
44 		 */
45 		const struct legacy_img_hdr *header =
46 			(const struct legacy_img_hdr *)CONFIG_SYS_OS_BASE;
47 #ifdef CONFIG_SPL_LOAD_FIT
48 		if (image_get_magic(header) == FDT_MAGIC) {
49 			int ret;
50 
51 			debug("Found FIT\n");
52 			spl_load_init(&load, spl_nor_load_read, NULL, 1);
53 
54 			ret = spl_load_simple_fit(spl_image, &load,
55 						  CONFIG_SYS_OS_BASE,
56 						  (void *)header);
57 
58 #if defined CONFIG_SPL_PAYLOAD_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
59 			memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
60 			       (void *)CONFIG_CMD_SPL_NOR_OFS,
61 			       CONFIG_CMD_SPL_WRITE_SIZE);
62 #endif
63 			return ret;
64 		}
65 #endif
66 		if (image_get_os(header) == IH_OS_LINUX) {
67 			/* happy - was a Linux */
68 			int ret;
69 
70 			ret = spl_parse_image_header(spl_image, bootdev, header);
71 			if (ret)
72 				return ret;
73 
74 			memcpy((void *)spl_image->load_addr,
75 			       (void *)(CONFIG_SYS_OS_BASE +
76 					sizeof(struct legacy_img_hdr)),
77 			       spl_image->size);
78 #ifdef CONFIG_SPL_PAYLOAD_ARGS_ADDR
79 			spl_image->arg = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR;
80 #endif
81 
82 			return 0;
83 		} else {
84 			puts("The Expected Linux image was not found.\n"
85 			     "Please check your NOR configuration.\n"
86 			     "Trying to start u-boot now...\n");
87 		}
88 	}
89 #endif
90 
91 	/*
92 	 * Load real U-Boot from its location in NOR flash to its
93 	 * defined location in SDRAM
94 	 */
95 	spl_load_init(&load, spl_nor_load_read, NULL, 1);
96 	return spl_load(spl_image, bootdev, &load, 0, spl_nor_get_uboot_base());
97 }
98 SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);
99