blob: aa5d23a6fbee8c7ce4a0ec192126f18a49b2b572 [file] [log] [blame]
Peng Fan36986792019-09-16 03:09:31 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
6#include <common.h>
7#include <errno.h>
8#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Peng Fan36986792019-09-16 03:09:31 +000011#include <linux/libfdt.h>
12#include <spl.h>
Ye Li7a71c612021-08-07 16:00:39 +080013#include <asm/mach-imx/image.h>
Peng Fan36986792019-09-16 03:09:31 +000014#include <asm/arch/sys_proto.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Ye Li7a71c612021-08-07 16:00:39 +080018/* Caller need ensure the offset and size to align with page size */
19ulong spl_romapi_raw_seekable_read(u32 offset, u32 size, void *buf)
20{
Ye Li7a71c612021-08-07 16:00:39 +080021 int ret;
22
23 debug("%s 0x%x, size 0x%x\n", __func__, offset, size);
24
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020025 ret = rom_api_download_image(buf, offset, size);
Ye Li7a71c612021-08-07 16:00:39 +080026
27 if (ret == ROM_API_OKAY)
28 return size;
29
30 printf("%s Failure when load 0x%x, size 0x%x\n", __func__, offset, size);
31
32 return 0;
33}
34
35ulong __weak spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
36{
Marek Vasut85107562022-03-09 17:09:45 +010037 return image_offset +
38 (CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000);
Ye Li7a71c612021-08-07 16:00:39 +080039}
40
Peng Fan36986792019-09-16 03:09:31 +000041static int is_boot_from_stream_device(u32 boot)
42{
43 u32 interface;
44
45 interface = boot >> 16;
46 if (interface >= BT_DEV_TYPE_USB)
47 return 1;
48
49 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
50 return 1;
51
52 return 0;
53}
54
55static ulong spl_romapi_read_seekable(struct spl_load_info *load,
56 ulong sector, ulong count,
57 void *buf)
58{
59 u32 pagesize = *(u32 *)load->priv;
Peng Fan36986792019-09-16 03:09:31 +000060 ulong byte = count * pagesize;
Peng Fan36986792019-09-16 03:09:31 +000061 u32 offset;
62
63 offset = sector * pagesize;
64
Ye Li7a71c612021-08-07 16:00:39 +080065 return spl_romapi_raw_seekable_read(offset, byte, buf) / pagesize;
Peng Fan36986792019-09-16 03:09:31 +000066}
67
68static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
69 struct spl_boot_device *bootdev,
70 u32 rom_bt_dev)
71{
Peng Fan36986792019-09-16 03:09:31 +000072 int ret;
73 u32 offset;
74 u32 pagesize, size;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060075 struct legacy_img_hdr *header;
Peng Fan36986792019-09-16 03:09:31 +000076 u32 image_offset;
77
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020078 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);
Peng Fan36986792019-09-16 03:09:31 +000081
82 if (ret != ROM_API_OKAY) {
83 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
84 return -1;
85 }
86
Simon Glassbb7d3bb2022-09-06 20:26:52 -060087 header = (struct legacy_img_hdr *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
Peng Fan36986792019-09-16 03:09:31 +000088
89 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
90 image_offset, pagesize, offset);
91
Ye Li7a71c612021-08-07 16:00:39 +080092 offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
Peng Fan36986792019-09-16 03:09:31 +000093
Simon Glassbb7d3bb2022-09-06 20:26:52 -060094 size = ALIGN(sizeof(struct legacy_img_hdr), pagesize);
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020095 ret = rom_api_download_image((u8 *)header, offset, size);
Peng Fan36986792019-09-16 03:09:31 +000096
97 if (ret != ROM_API_OKAY) {
98 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
99 offset, size);
100 return -1;
101 }
102
Ye Li7a71c612021-08-07 16:00:39 +0800103 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
104 struct spl_load_info load;
105
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)) {
Peng Fan36986792019-09-16 03:09:31 +0000112 struct spl_load_info load;
113
114 memset(&load, 0, sizeof(load));
115 load.bl_len = pagesize;
116 load.read = spl_romapi_read_seekable;
117 load.priv = &pagesize;
Ye Li7a71c612021-08-07 16:00:39 +0800118
119 ret = spl_load_imx_container(spl_image, &load, offset / pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000120 } else {
121 /* TODO */
122 puts("Can't support legacy image\n");
123 return -1;
124 }
125
126 return 0;
127}
128
129static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
130 ulong count, void *buf)
131{
132 memcpy(buf, (void *)(sector), count);
133
134 if (load->priv) {
135 ulong *p = (ulong *)load->priv;
136 ulong total = sector + count;
137
138 if (total > *p)
139 *p = total;
140 }
141
142 return count;
143}
144
145static ulong get_fit_image_size(void *fit)
146{
147 struct spl_image_info spl_image;
148 struct spl_load_info spl_load_info;
149 ulong last = (ulong)fit;
150
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;
155
156 spl_load_simple_fit(&spl_image, &spl_load_info,
157 (uintptr_t)fit, fit);
158
159 return last - (ulong)fit;
160}
161
Ye Li7a71c612021-08-07 16:00:39 +0800162static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000163{
164 int i;
165
166 for (i = 0; i < size; i += 4)
167 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
168 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800169
170 return NULL;
171}
172
173static u8 *search_container_header(u8 *p, int size)
174{
175 int i = 0;
176 u8 *hdr;
177
178 for (i = 0; i < size; i += 4) {
179 hdr = p + i;
180 if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
181 return p + i;
182 }
183
184 return NULL;
185}
186
187static u8 *search_img_header(u8 *p, int size)
188{
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);
Peng Fan36986792019-09-16 03:09:31 +0000193
194 return NULL;
195}
196
Ye Li7a71c612021-08-07 16:00:39 +0800197static u32 img_header_size(void)
198{
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);
203
204 return 0;
205}
206
207static int img_info_size(void *img_hdr)
208{
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;
213
214 return (container->length_lsb + (container->length_msb << 8));
215#else
216 return 0;
217#endif
218}
219
220static int img_total_size(void *img_hdr)
221{
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);
226
227 if (total < 0) {
228 printf("invalid container image\n");
229 return 0;
230 }
231
232 return total;
233 }
234
235 return 0;
236}
237
Peng Fan36986792019-09-16 03:09:31 +0000238static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
239 struct spl_boot_device *bootdev)
240{
241 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000242 u32 pagesize, pg;
243 int ret;
244 int i = 0;
245 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800246 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000247 int imagesize;
248 int total;
249
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200250 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000251
252 if (ret != ROM_API_OKAY)
253 puts("failure at query_boot_info\n");
254
255 pg = pagesize;
256 if (pg < 1024)
257 pg = 1024;
258
259 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200260 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000261
262 if (ret != ROM_API_OKAY) {
263 puts("Steam(USB) download failure\n");
264 return -1;
265 }
266
Ye Li7a71c612021-08-07 16:00:39 +0800267 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000268 p += pg;
269
Ye Li7a71c612021-08-07 16:00:39 +0800270 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000271 break;
272 }
273
Ye Li7a71c612021-08-07 16:00:39 +0800274 if (!phdr) {
275 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000276 return -1;
277 }
278
Ye Li7a71c612021-08-07 16:00:39 +0800279 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200280 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000281
282 if (ret != ROM_API_OKAY) {
283 puts("Steam(USB) download failure\n");
284 return -1;
285 }
286
287 p += pg;
288 }
289
Ye Li7a71c612021-08-07 16:00:39 +0800290 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200291 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000292
Ye Li7a71c612021-08-07 16:00:39 +0800293 if (p - phdr < imagesize) {
294 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000295 /*need pagesize hear after ROM fix USB problme*/
296 imagesize += pg - 1;
297 imagesize /= pg;
298 imagesize *= pg;
299
300 printf("Need continue download %d\n", imagesize);
301
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200302 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000303
304 p += imagesize;
305
306 if (ret != ROM_API_OKAY) {
307 printf("Failure download %d\n", imagesize);
308 return -1;
309 }
310 }
311
Ye Li7a71c612021-08-07 16:00:39 +0800312 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000313 total += 3;
314 total &= ~0x3;
315
Ye Li7a71c612021-08-07 16:00:39 +0800316 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000317
318 imagesize += pagesize - 1;
319 imagesize /= pagesize;
320 imagesize *= pagesize;
321
Ye Li7a71c612021-08-07 16:00:39 +0800322 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000323
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200324 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000325 if (ret != ROM_API_OKAY)
326 printf("ROM download failure %d\n", imagesize);
327
328 memset(&load, 0, sizeof(load));
329 load.bl_len = 1;
330 load.read = spl_ram_load_read;
331
Ye Li7a71c612021-08-07 16:00:39 +0800332 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);
336
337 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000338}
339
340int board_return_to_bootrom(struct spl_image_info *spl_image,
341 struct spl_boot_device *bootdev)
342{
Peng Fan36986792019-09-16 03:09:31 +0000343 int ret;
344 u32 boot;
345
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200346 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Peng Fan36986792019-09-16 03:09:31 +0000347
348 if (ret != ROM_API_OKAY) {
349 puts("ROMAPI: failure at query_boot_info\n");
350 return -1;
351 }
352
353 if (is_boot_from_stream_device(boot))
354 return spl_romapi_load_image_stream(spl_image, bootdev);
355
356 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
357}