1 // SPDX-License-Identifier: GPL-2.0+
10 #include <linux/libfdt.h>
13 #include <asm/arch/sys_proto.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static int is_boot_from_stream_device(u32 boot)
21 interface = boot >> 16;
22 if (interface >= BT_DEV_TYPE_USB)
25 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
31 static ulong spl_romapi_read_seekable(struct spl_load_info *load,
32 ulong sector, ulong count,
35 u32 pagesize = *(u32 *)load->priv;
36 volatile gd_t *pgd = gd;
37 ulong byte = count * pagesize;
41 offset = sector * pagesize;
43 debug("ROM API load from 0x%x, size 0x%x\n", offset, (u32)byte);
45 ret = g_rom_api->download_image(buf, offset, byte,
46 ((uintptr_t)buf) ^ offset ^ byte);
49 if (ret == ROM_API_OKAY)
52 printf("ROM API Failure when load 0x%x\n", offset);
57 static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev,
61 volatile gd_t *pgd = gd;
65 struct image_header *header;
68 ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset,
69 ((uintptr_t)&offset) ^ QUERY_IVT_OFF);
70 ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
71 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
72 ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset,
73 ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF);
77 if (ret != ROM_API_OKAY) {
78 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
82 header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
84 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
85 image_offset, pagesize, offset);
87 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_FLEXSPINOR)
88 offset = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512;
90 offset = image_offset +
91 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000;
93 size = ALIGN(sizeof(struct image_header), pagesize);
94 ret = g_rom_api->download_image((u8 *)header, offset, size,
95 ((uintptr_t)header) ^ offset ^ size);
98 if (ret != ROM_API_OKAY) {
99 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
104 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
105 image_get_magic(header) == FDT_MAGIC) {
106 struct spl_load_info load;
108 memset(&load, 0, sizeof(load));
109 load.bl_len = pagesize;
110 load.read = spl_romapi_read_seekable;
111 load.priv = &pagesize;
112 return spl_load_simple_fit(spl_image, &load,
113 offset / pagesize, header);
116 puts("Can't support legacy image\n");
123 static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
124 ulong count, void *buf)
126 memcpy(buf, (void *)(sector), count);
129 ulong *p = (ulong *)load->priv;
130 ulong total = sector + count;
139 static ulong get_fit_image_size(void *fit)
141 struct spl_image_info spl_image;
142 struct spl_load_info spl_load_info;
143 ulong last = (ulong)fit;
145 memset(&spl_load_info, 0, sizeof(spl_load_info));
146 spl_load_info.bl_len = 1;
147 spl_load_info.read = spl_ram_load_read;
148 spl_load_info.priv = &last;
150 spl_load_simple_fit(&spl_image, &spl_load_info,
151 (uintptr_t)fit, fit);
153 return last - (ulong)fit;
156 u8 *search_fit_header(u8 *p, int size)
160 for (i = 0; i < size; i += 4)
161 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
167 static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
168 struct spl_boot_device *bootdev)
170 struct spl_load_info load;
171 volatile gd_t *pgd = gd;
175 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
180 ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
181 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
184 if (ret != ROM_API_OKAY)
185 puts("failure at query_boot_info\n");
191 for (i = 0; i < 640; i++) {
192 ret = g_rom_api->download_image(p, 0, pg,
193 ((uintptr_t)p) ^ pg);
196 if (ret != ROM_API_OKAY) {
197 puts("Steam(USB) download failure\n");
201 pfit = search_fit_header(p, pg);
209 puts("Can't found uboot FIT image in 640K range \n");
213 if (p - pfit < sizeof(struct fdt_header)) {
214 ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg);
217 if (ret != ROM_API_OKAY) {
218 puts("Steam(USB) download failure\n");
225 imagesize = fit_get_size(pfit);
226 printf("Find FIT header 0x&%p, size %d\n", pfit, imagesize);
228 if (p - pfit < imagesize) {
229 imagesize -= p - pfit;
230 /*need pagesize hear after ROM fix USB problme*/
235 printf("Need continue download %d\n", imagesize);
237 ret = g_rom_api->download_image(p, 0, imagesize,
238 ((uintptr_t)p) ^ imagesize);
243 if (ret != ROM_API_OKAY) {
244 printf("Failure download %d\n", imagesize);
249 total = get_fit_image_size(pfit);
253 imagesize = total - (p - pfit);
255 imagesize += pagesize - 1;
256 imagesize /= pagesize;
257 imagesize *= pagesize;
259 printf("Download %d, total fit %d\n", imagesize, total);
261 ret = g_rom_api->download_image(p, 0, imagesize,
262 ((uintptr_t)p) ^ imagesize);
263 if (ret != ROM_API_OKAY)
264 printf("ROM download failure %d\n", imagesize);
266 memset(&load, 0, sizeof(load));
268 load.read = spl_ram_load_read;
270 return spl_load_simple_fit(spl_image, &load, (ulong)pfit, pfit);
273 int board_return_to_bootrom(struct spl_image_info *spl_image,
274 struct spl_boot_device *bootdev)
276 volatile gd_t *pgd = gd;
280 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
281 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
284 if (ret != ROM_API_OKAY) {
285 puts("ROMAPI: failure at query_boot_info\n");
289 if (is_boot_from_stream_device(boot))
290 return spl_romapi_load_image_stream(spl_image, bootdev);
292 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);