blob: c47f5a6bdb448cc4fc7ba07faeba4f9b58080779 [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{
21 volatile gd_t *pgd = gd;
22 int ret;
23
24 debug("%s 0x%x, size 0x%x\n", __func__, offset, size);
25
26 ret = g_rom_api->download_image(buf, offset, size,
27 ((uintptr_t)buf) ^ offset ^ size);
28
29 set_gd(pgd);
30
31 if (ret == ROM_API_OKAY)
32 return size;
33
34 printf("%s Failure when load 0x%x, size 0x%x\n", __func__, offset, size);
35
36 return 0;
37}
38
39ulong __weak spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
40{
Marek Vasut85107562022-03-09 17:09:45 +010041 return image_offset +
42 (CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000);
Ye Li7a71c612021-08-07 16:00:39 +080043}
44
Peng Fan36986792019-09-16 03:09:31 +000045static int is_boot_from_stream_device(u32 boot)
46{
47 u32 interface;
48
49 interface = boot >> 16;
50 if (interface >= BT_DEV_TYPE_USB)
51 return 1;
52
53 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
54 return 1;
55
56 return 0;
57}
58
59static ulong spl_romapi_read_seekable(struct spl_load_info *load,
60 ulong sector, ulong count,
61 void *buf)
62{
63 u32 pagesize = *(u32 *)load->priv;
Peng Fan36986792019-09-16 03:09:31 +000064 ulong byte = count * pagesize;
Peng Fan36986792019-09-16 03:09:31 +000065 u32 offset;
66
67 offset = sector * pagesize;
68
Ye Li7a71c612021-08-07 16:00:39 +080069 return spl_romapi_raw_seekable_read(offset, byte, buf) / pagesize;
Peng Fan36986792019-09-16 03:09:31 +000070}
71
72static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
73 struct spl_boot_device *bootdev,
74 u32 rom_bt_dev)
75{
76 volatile gd_t *pgd = gd;
77 int ret;
78 u32 offset;
79 u32 pagesize, size;
80 struct image_header *header;
81 u32 image_offset;
82
83 ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset,
84 ((uintptr_t)&offset) ^ QUERY_IVT_OFF);
85 ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
86 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
87 ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset,
88 ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF);
89
Marek Behúna8cf1552021-05-20 13:24:10 +020090 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +000091
92 if (ret != ROM_API_OKAY) {
93 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
94 return -1;
95 }
96
97 header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
98
99 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
100 image_offset, pagesize, offset);
101
Ye Li7a71c612021-08-07 16:00:39 +0800102 offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
Peng Fan36986792019-09-16 03:09:31 +0000103
104 size = ALIGN(sizeof(struct image_header), pagesize);
105 ret = g_rom_api->download_image((u8 *)header, offset, size,
106 ((uintptr_t)header) ^ offset ^ size);
Marek Behúna8cf1552021-05-20 13:24:10 +0200107 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000108
109 if (ret != ROM_API_OKAY) {
110 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
111 offset, size);
112 return -1;
113 }
114
Ye Li7a71c612021-08-07 16:00:39 +0800115 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
116 struct spl_load_info load;
117
118 memset(&load, 0, sizeof(load));
119 load.bl_len = pagesize;
120 load.read = spl_romapi_read_seekable;
121 load.priv = &pagesize;
122 return spl_load_simple_fit(spl_image, &load, offset / pagesize, header);
123 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Peng Fan36986792019-09-16 03:09:31 +0000124 struct spl_load_info load;
125
126 memset(&load, 0, sizeof(load));
127 load.bl_len = pagesize;
128 load.read = spl_romapi_read_seekable;
129 load.priv = &pagesize;
Ye Li7a71c612021-08-07 16:00:39 +0800130
131 ret = spl_load_imx_container(spl_image, &load, offset / pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000132 } else {
133 /* TODO */
134 puts("Can't support legacy image\n");
135 return -1;
136 }
137
138 return 0;
139}
140
141static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
142 ulong count, void *buf)
143{
144 memcpy(buf, (void *)(sector), count);
145
146 if (load->priv) {
147 ulong *p = (ulong *)load->priv;
148 ulong total = sector + count;
149
150 if (total > *p)
151 *p = total;
152 }
153
154 return count;
155}
156
157static ulong get_fit_image_size(void *fit)
158{
159 struct spl_image_info spl_image;
160 struct spl_load_info spl_load_info;
161 ulong last = (ulong)fit;
162
163 memset(&spl_load_info, 0, sizeof(spl_load_info));
164 spl_load_info.bl_len = 1;
165 spl_load_info.read = spl_ram_load_read;
166 spl_load_info.priv = &last;
167
168 spl_load_simple_fit(&spl_image, &spl_load_info,
169 (uintptr_t)fit, fit);
170
171 return last - (ulong)fit;
172}
173
Ye Li7a71c612021-08-07 16:00:39 +0800174static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000175{
176 int i;
177
178 for (i = 0; i < size; i += 4)
179 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
180 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800181
182 return NULL;
183}
184
185static u8 *search_container_header(u8 *p, int size)
186{
187 int i = 0;
188 u8 *hdr;
189
190 for (i = 0; i < size; i += 4) {
191 hdr = p + i;
192 if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
193 return p + i;
194 }
195
196 return NULL;
197}
198
199static u8 *search_img_header(u8 *p, int size)
200{
201 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
202 return search_fit_header(p, size);
203 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
204 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000205
206 return NULL;
207}
208
Ye Li7a71c612021-08-07 16:00:39 +0800209static u32 img_header_size(void)
210{
211 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
212 return sizeof(struct fdt_header);
213 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
214 return sizeof(struct container_hdr);
215
216 return 0;
217}
218
219static int img_info_size(void *img_hdr)
220{
221#ifdef CONFIG_SPL_LOAD_FIT
222 return fit_get_size(img_hdr);
223#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
224 struct container_hdr *container = img_hdr;
225
226 return (container->length_lsb + (container->length_msb << 8));
227#else
228 return 0;
229#endif
230}
231
232static int img_total_size(void *img_hdr)
233{
234 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
235 return get_fit_image_size(img_hdr);
236 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
237 int total = get_container_size((ulong)img_hdr, NULL);
238
239 if (total < 0) {
240 printf("invalid container image\n");
241 return 0;
242 }
243
244 return total;
245 }
246
247 return 0;
248}
249
Peng Fan36986792019-09-16 03:09:31 +0000250static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
251 struct spl_boot_device *bootdev)
252{
253 struct spl_load_info load;
254 volatile gd_t *pgd = gd;
255 u32 pagesize, pg;
256 int ret;
257 int i = 0;
258 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800259 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000260 int imagesize;
261 int total;
262
263 ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
264 ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
Marek Behúna8cf1552021-05-20 13:24:10 +0200265 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000266
267 if (ret != ROM_API_OKAY)
268 puts("failure at query_boot_info\n");
269
270 pg = pagesize;
271 if (pg < 1024)
272 pg = 1024;
273
274 for (i = 0; i < 640; i++) {
275 ret = g_rom_api->download_image(p, 0, pg,
276 ((uintptr_t)p) ^ pg);
Marek Behúna8cf1552021-05-20 13:24:10 +0200277 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000278
279 if (ret != ROM_API_OKAY) {
280 puts("Steam(USB) download failure\n");
281 return -1;
282 }
283
Ye Li7a71c612021-08-07 16:00:39 +0800284 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000285 p += pg;
286
Ye Li7a71c612021-08-07 16:00:39 +0800287 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000288 break;
289 }
290
Ye Li7a71c612021-08-07 16:00:39 +0800291 if (!phdr) {
292 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000293 return -1;
294 }
295
Ye Li7a71c612021-08-07 16:00:39 +0800296 if (p - phdr < img_header_size()) {
Peng Fan36986792019-09-16 03:09:31 +0000297 ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg);
Marek Behúna8cf1552021-05-20 13:24:10 +0200298 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000299
300 if (ret != ROM_API_OKAY) {
301 puts("Steam(USB) download failure\n");
302 return -1;
303 }
304
305 p += pg;
306 }
307
Ye Li7a71c612021-08-07 16:00:39 +0800308 imagesize = img_info_size(phdr);
309 printf("Find img info 0x&%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000310
Ye Li7a71c612021-08-07 16:00:39 +0800311 if (p - phdr < imagesize) {
312 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000313 /*need pagesize hear after ROM fix USB problme*/
314 imagesize += pg - 1;
315 imagesize /= pg;
316 imagesize *= pg;
317
318 printf("Need continue download %d\n", imagesize);
319
320 ret = g_rom_api->download_image(p, 0, imagesize,
321 ((uintptr_t)p) ^ imagesize);
Marek Behúna8cf1552021-05-20 13:24:10 +0200322 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000323
324 p += imagesize;
325
326 if (ret != ROM_API_OKAY) {
327 printf("Failure download %d\n", imagesize);
328 return -1;
329 }
330 }
331
Ye Li7a71c612021-08-07 16:00:39 +0800332 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000333 total += 3;
334 total &= ~0x3;
335
Ye Li7a71c612021-08-07 16:00:39 +0800336 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000337
338 imagesize += pagesize - 1;
339 imagesize /= pagesize;
340 imagesize *= pagesize;
341
Ye Li7a71c612021-08-07 16:00:39 +0800342 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000343
344 ret = g_rom_api->download_image(p, 0, imagesize,
345 ((uintptr_t)p) ^ imagesize);
Ye Li7a71c612021-08-07 16:00:39 +0800346 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000347 if (ret != ROM_API_OKAY)
348 printf("ROM download failure %d\n", imagesize);
349
350 memset(&load, 0, sizeof(load));
351 load.bl_len = 1;
352 load.read = spl_ram_load_read;
353
Ye Li7a71c612021-08-07 16:00:39 +0800354 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
355 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
356 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
357 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
358
359 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000360}
361
362int board_return_to_bootrom(struct spl_image_info *spl_image,
363 struct spl_boot_device *bootdev)
364{
365 volatile gd_t *pgd = gd;
366 int ret;
367 u32 boot;
368
369 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
370 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
Marek Behúna8cf1552021-05-20 13:24:10 +0200371 set_gd(pgd);
Peng Fan36986792019-09-16 03:09:31 +0000372
373 if (ret != ROM_API_OKAY) {
374 puts("ROMAPI: failure at query_boot_info\n");
375 return -1;
376 }
377
378 if (is_boot_from_stream_device(boot))
379 return spl_romapi_load_image_stream(spl_image, bootdev);
380
381 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
382}