1 // SPDX-License-Identifier: GPL-2.0+
10 #include <asm/global_data.h>
11 #include <linux/libfdt.h>
13 #include <asm/mach-imx/image.h>
14 #include <asm/arch/sys_proto.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 /* Caller need ensure the offset and size to align with page size */
19 ulong spl_romapi_raw_seekable_read(u32 offset, u32 size, void *buf)
23 debug("%s 0x%x, size 0x%x\n", __func__, offset, size);
25 ret = rom_api_download_image(buf, offset, size);
27 if (ret == ROM_API_OKAY)
30 printf("%s Failure when load 0x%x, size 0x%x\n", __func__, offset, size);
35 ulong __weak spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
38 (CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000);
41 static int is_boot_from_stream_device(u32 boot)
45 interface = boot >> 16;
46 if (interface >= BT_DEV_TYPE_USB)
49 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
55 static ulong spl_romapi_read_seekable(struct spl_load_info *load,
56 ulong sector, ulong count,
59 u32 pagesize = *(u32 *)load->priv;
60 ulong byte = count * pagesize;
63 offset = sector * pagesize;
65 return spl_romapi_raw_seekable_read(offset, byte, buf) / pagesize;
68 static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
69 struct spl_boot_device *bootdev,
75 struct legacy_img_hdr *header;
78 ret = rom_api_query_boot_infor(QUERY_IVT_OFF, &offset);
79 ret |= rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
80 ret |= rom_api_query_boot_infor(QUERY_IMG_OFF, &image_offset);
82 if (ret != ROM_API_OKAY) {
83 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
87 header = (struct legacy_img_hdr *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
89 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
90 image_offset, pagesize, offset);
92 offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
94 size = ALIGN(sizeof(struct legacy_img_hdr), pagesize);
95 ret = rom_api_download_image((u8 *)header, offset, size);
97 if (ret != ROM_API_OKAY) {
98 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
103 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
104 struct spl_load_info load;
106 memset(&load, 0, sizeof(load));
107 load.bl_len = pagesize;
108 load.read = spl_romapi_read_seekable;
109 load.priv = &pagesize;
110 return spl_load_simple_fit(spl_image, &load, offset / pagesize, header);
111 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
112 struct spl_load_info load;
114 memset(&load, 0, sizeof(load));
115 load.bl_len = pagesize;
116 load.read = spl_romapi_read_seekable;
117 load.priv = &pagesize;
119 ret = spl_load_imx_container(spl_image, &load, offset / pagesize);
122 puts("Can't support legacy image\n");
129 static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
130 ulong count, void *buf)
132 memcpy(buf, (void *)(sector), count);
135 ulong *p = (ulong *)load->priv;
136 ulong total = sector + count;
145 static ulong get_fit_image_size(void *fit)
147 struct spl_image_info spl_image;
148 struct spl_load_info spl_load_info;
149 ulong last = (ulong)fit;
151 memset(&spl_load_info, 0, sizeof(spl_load_info));
152 spl_load_info.bl_len = 1;
153 spl_load_info.read = spl_ram_load_read;
154 spl_load_info.priv = &last;
156 spl_load_simple_fit(&spl_image, &spl_load_info,
157 (uintptr_t)fit, fit);
159 return last - (ulong)fit;
162 static u8 *search_fit_header(u8 *p, int size)
166 for (i = 0; i < size; i += 4)
167 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
173 static u8 *search_container_header(u8 *p, int size)
178 for (i = 0; i < size; i += 4) {
180 if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
187 static u8 *search_img_header(u8 *p, int size)
189 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
190 return search_fit_header(p, size);
191 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
192 return search_container_header(p, size);
197 static u32 img_header_size(void)
199 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
200 return sizeof(struct fdt_header);
201 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
202 return sizeof(struct container_hdr);
207 static int img_info_size(void *img_hdr)
209 #ifdef CONFIG_SPL_LOAD_FIT
210 return fit_get_size(img_hdr);
211 #elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
212 struct container_hdr *container = img_hdr;
214 return (container->length_lsb + (container->length_msb << 8));
220 static int img_total_size(void *img_hdr)
222 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
223 return get_fit_image_size(img_hdr);
224 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
225 int total = get_container_size((ulong)img_hdr, NULL);
228 printf("invalid container image\n");
238 static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
239 struct spl_boot_device *bootdev)
241 struct spl_load_info load;
245 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
250 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
252 if (ret != ROM_API_OKAY)
253 puts("failure at query_boot_info\n");
259 for (i = 0; i < 640; i++) {
260 ret = rom_api_download_image(p, 0, pg);
262 if (ret != ROM_API_OKAY) {
263 puts("Steam(USB) download failure\n");
267 phdr = search_img_header(p, pg);
275 puts("Can't found uboot image in 640K range\n");
279 if (p - phdr < img_header_size()) {
280 ret = rom_api_download_image(p, 0, pg);
282 if (ret != ROM_API_OKAY) {
283 puts("Steam(USB) download failure\n");
290 imagesize = img_info_size(phdr);
291 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
293 if (p - phdr < imagesize) {
294 imagesize -= p - phdr;
295 /*need pagesize hear after ROM fix USB problme*/
300 printf("Need continue download %d\n", imagesize);
302 ret = rom_api_download_image(p, 0, imagesize);
306 if (ret != ROM_API_OKAY) {
307 printf("Failure download %d\n", imagesize);
312 total = img_total_size(phdr);
316 imagesize = total - (p - phdr);
318 imagesize += pagesize - 1;
319 imagesize /= pagesize;
320 imagesize *= pagesize;
322 printf("Download %d, Total size %d\n", imagesize, total);
324 ret = rom_api_download_image(p, 0, imagesize);
325 if (ret != ROM_API_OKAY)
326 printf("ROM download failure %d\n", imagesize);
328 memset(&load, 0, sizeof(load));
330 load.read = spl_ram_load_read;
332 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
333 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
334 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
335 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
340 int board_return_to_bootrom(struct spl_image_info *spl_image,
341 struct spl_boot_device *bootdev)
346 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
348 if (ret != ROM_API_OKAY) {
349 puts("ROMAPI: failure at query_boot_info\n");
353 if (is_boot_from_stream_device(boot))
354 return spl_romapi_load_image_stream(spl_image, bootdev);
356 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);