1 // SPDX-License-Identifier: GPL-2.0+
9 #include <linux/libfdt.h>
12 #include <asm/arch/sys_proto.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 static int is_boot_from_stream_device(u32 boot)
20 interface = boot >> 16;
21 if (interface >= BT_DEV_TYPE_USB)
24 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
30 static ulong spl_romapi_read_seekable(struct spl_load_info *load,
31 ulong sector, ulong count,
34 u32 pagesize = *(u32 *)load->priv;
35 volatile gd_t *pgd = gd;
36 ulong byte = count * pagesize;
40 offset = sector * pagesize;
42 debug("ROM API load from 0x%x, size 0x%x\n", offset, (u32)byte);
44 ret = g_rom_api->download_image(buf, offset, byte,
45 ((uintptr_t)buf) ^ offset ^ byte);
48 if (ret == ROM_API_OKAY)
51 printf("ROM API Failure when load 0x%x\n", offset);
56 static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
57 struct spl_boot_device *bootdev,
60 volatile gd_t *pgd = gd;
64 struct image_header *header;
67 ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset,
68 ((uintptr_t)&offset) ^ QUERY_IVT_OFF);
69 ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
70 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
71 ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset,
72 ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF);
76 if (ret != ROM_API_OKAY) {
77 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
81 header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
83 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
84 image_offset, pagesize, offset);
86 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_FLEXSPINOR)
87 offset = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512;
89 offset = image_offset +
90 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000;
92 size = ALIGN(sizeof(struct image_header), pagesize);
93 ret = g_rom_api->download_image((u8 *)header, offset, size,
94 ((uintptr_t)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) &&
104 image_get_magic(header) == FDT_MAGIC) {
105 struct spl_load_info load;
107 memset(&load, 0, sizeof(load));
108 load.bl_len = pagesize;
109 load.read = spl_romapi_read_seekable;
110 load.priv = &pagesize;
111 return spl_load_simple_fit(spl_image, &load,
112 offset / pagesize, header);
115 puts("Can't support legacy image\n");
122 static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
123 ulong count, void *buf)
125 memcpy(buf, (void *)(sector), count);
128 ulong *p = (ulong *)load->priv;
129 ulong total = sector + count;
138 static ulong get_fit_image_size(void *fit)
140 struct spl_image_info spl_image;
141 struct spl_load_info spl_load_info;
142 ulong last = (ulong)fit;
144 memset(&spl_load_info, 0, sizeof(spl_load_info));
145 spl_load_info.bl_len = 1;
146 spl_load_info.read = spl_ram_load_read;
147 spl_load_info.priv = &last;
149 spl_load_simple_fit(&spl_image, &spl_load_info,
150 (uintptr_t)fit, fit);
152 return last - (ulong)fit;
155 u8 *search_fit_header(u8 *p, int size)
159 for (i = 0; i < size; i += 4)
160 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
166 static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
167 struct spl_boot_device *bootdev)
169 struct spl_load_info load;
170 volatile gd_t *pgd = gd;
174 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
179 ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
180 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
183 if (ret != ROM_API_OKAY)
184 puts("failure at query_boot_info\n");
190 for (i = 0; i < 640; i++) {
191 ret = g_rom_api->download_image(p, 0, pg,
192 ((uintptr_t)p) ^ pg);
195 if (ret != ROM_API_OKAY) {
196 puts("Steam(USB) download failure\n");
200 pfit = search_fit_header(p, pg);
208 puts("Can't found uboot FIT image in 640K range \n");
212 if (p - pfit < sizeof(struct fdt_header)) {
213 ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg);
216 if (ret != ROM_API_OKAY) {
217 puts("Steam(USB) download failure\n");
224 imagesize = fit_get_size(pfit);
225 printf("Find FIT header 0x&%p, size %d\n", pfit, imagesize);
227 if (p - pfit < imagesize) {
228 imagesize -= p - pfit;
229 /*need pagesize hear after ROM fix USB problme*/
234 printf("Need continue download %d\n", imagesize);
236 ret = g_rom_api->download_image(p, 0, imagesize,
237 ((uintptr_t)p) ^ imagesize);
242 if (ret != ROM_API_OKAY) {
243 printf("Failure download %d\n", imagesize);
248 total = get_fit_image_size(pfit);
252 imagesize = total - (p - pfit);
254 imagesize += pagesize - 1;
255 imagesize /= pagesize;
256 imagesize *= pagesize;
258 printf("Download %d, total fit %d\n", imagesize, total);
260 ret = g_rom_api->download_image(p, 0, imagesize,
261 ((uintptr_t)p) ^ imagesize);
262 if (ret != ROM_API_OKAY)
263 printf("ROM download failure %d\n", imagesize);
265 memset(&load, 0, sizeof(load));
267 load.read = spl_ram_load_read;
269 return spl_load_simple_fit(spl_image, &load, (ulong)pfit, pfit);
272 int board_return_to_bootrom(struct spl_image_info *spl_image,
273 struct spl_boot_device *bootdev)
275 volatile gd_t *pgd = gd;
279 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
280 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
283 if (ret != ROM_API_OKAY) {
284 puts("ROMAPI: failure at query_boot_info\n");
288 if (is_boot_from_stream_device(boot))
289 return spl_romapi_load_image_stream(spl_image, bootdev);
291 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);