1 // SPDX-License-Identifier: GPL-2.0+
10 #include <asm/global_data.h>
11 #include <linux/libfdt.h>
14 #include <asm/arch/sys_proto.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static int is_boot_from_stream_device(u32 boot)
22 interface = boot >> 16;
23 if (interface >= BT_DEV_TYPE_USB)
26 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
32 static ulong spl_romapi_read_seekable(struct spl_load_info *load,
33 ulong sector, ulong count,
36 u32 pagesize = *(u32 *)load->priv;
37 volatile gd_t *pgd = gd;
38 ulong byte = count * pagesize;
42 offset = sector * pagesize;
44 debug("ROM API load from 0x%x, size 0x%x\n", offset, (u32)byte);
46 ret = g_rom_api->download_image(buf, offset, byte,
47 ((uintptr_t)buf) ^ offset ^ byte);
50 if (ret == ROM_API_OKAY)
53 printf("ROM API Failure when load 0x%x\n", offset);
58 static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
59 struct spl_boot_device *bootdev,
62 volatile gd_t *pgd = gd;
66 struct image_header *header;
69 ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset,
70 ((uintptr_t)&offset) ^ QUERY_IVT_OFF);
71 ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
72 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
73 ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset,
74 ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF);
78 if (ret != ROM_API_OKAY) {
79 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
83 header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
85 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
86 image_offset, pagesize, offset);
88 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_FLEXSPINOR)
89 offset = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512;
91 offset = image_offset +
92 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000;
94 size = ALIGN(sizeof(struct image_header), pagesize);
95 ret = g_rom_api->download_image((u8 *)header, offset, size,
96 ((uintptr_t)header) ^ offset ^ size);
99 if (ret != ROM_API_OKAY) {
100 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
105 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
106 image_get_magic(header) == FDT_MAGIC) {
107 struct spl_load_info load;
109 memset(&load, 0, sizeof(load));
110 load.bl_len = pagesize;
111 load.read = spl_romapi_read_seekable;
112 load.priv = &pagesize;
113 return spl_load_simple_fit(spl_image, &load,
114 offset / pagesize, header);
117 puts("Can't support legacy image\n");
124 static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
125 ulong count, void *buf)
127 memcpy(buf, (void *)(sector), count);
130 ulong *p = (ulong *)load->priv;
131 ulong total = sector + count;
140 static ulong get_fit_image_size(void *fit)
142 struct spl_image_info spl_image;
143 struct spl_load_info spl_load_info;
144 ulong last = (ulong)fit;
146 memset(&spl_load_info, 0, sizeof(spl_load_info));
147 spl_load_info.bl_len = 1;
148 spl_load_info.read = spl_ram_load_read;
149 spl_load_info.priv = &last;
151 spl_load_simple_fit(&spl_image, &spl_load_info,
152 (uintptr_t)fit, fit);
154 return last - (ulong)fit;
157 u8 *search_fit_header(u8 *p, int size)
161 for (i = 0; i < size; i += 4)
162 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
168 static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
169 struct spl_boot_device *bootdev)
171 struct spl_load_info load;
172 volatile gd_t *pgd = gd;
176 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
181 ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
182 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
185 if (ret != ROM_API_OKAY)
186 puts("failure at query_boot_info\n");
192 for (i = 0; i < 640; i++) {
193 ret = g_rom_api->download_image(p, 0, pg,
194 ((uintptr_t)p) ^ pg);
197 if (ret != ROM_API_OKAY) {
198 puts("Steam(USB) download failure\n");
202 pfit = search_fit_header(p, pg);
210 puts("Can't found uboot FIT image in 640K range \n");
214 if (p - pfit < sizeof(struct fdt_header)) {
215 ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg);
218 if (ret != ROM_API_OKAY) {
219 puts("Steam(USB) download failure\n");
226 imagesize = fit_get_size(pfit);
227 printf("Find FIT header 0x&%p, size %d\n", pfit, imagesize);
229 if (p - pfit < imagesize) {
230 imagesize -= p - pfit;
231 /*need pagesize hear after ROM fix USB problme*/
236 printf("Need continue download %d\n", imagesize);
238 ret = g_rom_api->download_image(p, 0, imagesize,
239 ((uintptr_t)p) ^ imagesize);
244 if (ret != ROM_API_OKAY) {
245 printf("Failure download %d\n", imagesize);
250 total = get_fit_image_size(pfit);
254 imagesize = total - (p - pfit);
256 imagesize += pagesize - 1;
257 imagesize /= pagesize;
258 imagesize *= pagesize;
260 printf("Download %d, total fit %d\n", imagesize, total);
262 ret = g_rom_api->download_image(p, 0, imagesize,
263 ((uintptr_t)p) ^ imagesize);
264 if (ret != ROM_API_OKAY)
265 printf("ROM download failure %d\n", imagesize);
267 memset(&load, 0, sizeof(load));
269 load.read = spl_ram_load_read;
271 return spl_load_simple_fit(spl_image, &load, (ulong)pfit, pfit);
274 int board_return_to_bootrom(struct spl_image_info *spl_image,
275 struct spl_boot_device *bootdev)
277 volatile gd_t *pgd = gd;
281 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
282 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
285 if (ret != ROM_API_OKAY) {
286 puts("ROMAPI: failure at query_boot_info\n");
290 if (is_boot_from_stream_device(boot))
291 return spl_romapi_load_image_stream(spl_image, bootdev);
293 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);