blob: b9dfb3d41def8b01e05b1cbbabb685c9b0cee399 [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);
Marek Vasut00639162023-07-02 03:03:51 +020079 if (ret != ROM_API_OKAY)
80 goto err;
Peng Fan36986792019-09-16 03:09:31 +000081
Marek Vasut00639162023-07-02 03:03:51 +020082 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
83 if (ret != ROM_API_OKAY)
84 goto err;
85
86 ret = rom_api_query_boot_infor(QUERY_IMG_OFF, &image_offset);
87 if (ret != ROM_API_OKAY)
88 goto err;
Peng Fan36986792019-09-16 03:09:31 +000089
Simon Glassbb7d3bb2022-09-06 20:26:52 -060090 header = (struct legacy_img_hdr *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
Peng Fan36986792019-09-16 03:09:31 +000091
92 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
93 image_offset, pagesize, offset);
94
Ye Li7a71c612021-08-07 16:00:39 +080095 offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
Peng Fan36986792019-09-16 03:09:31 +000096
Simon Glassbb7d3bb2022-09-06 20:26:52 -060097 size = ALIGN(sizeof(struct legacy_img_hdr), pagesize);
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020098 ret = rom_api_download_image((u8 *)header, offset, size);
Peng Fan36986792019-09-16 03:09:31 +000099
100 if (ret != ROM_API_OKAY) {
101 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
102 offset, size);
103 return -1;
104 }
105
Ye Li7a71c612021-08-07 16:00:39 +0800106 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
107 struct spl_load_info load;
108
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, offset / pagesize, header);
114 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Peng Fan36986792019-09-16 03:09:31 +0000115 struct spl_load_info load;
116
117 memset(&load, 0, sizeof(load));
118 load.bl_len = pagesize;
119 load.read = spl_romapi_read_seekable;
120 load.priv = &pagesize;
Ye Li7a71c612021-08-07 16:00:39 +0800121
122 ret = spl_load_imx_container(spl_image, &load, offset / pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000123 } else {
124 /* TODO */
125 puts("Can't support legacy image\n");
126 return -1;
127 }
128
129 return 0;
Marek Vasut00639162023-07-02 03:03:51 +0200130
131err:
132 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
133 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000134}
135
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200136struct stream_state {
137 u8 *base;
138 u8 *end;
139 u32 pagesize;
140};
141
142static ulong spl_romapi_read_stream(struct spl_load_info *load, ulong sector,
143 ulong count, void *buf)
144{
145 struct stream_state *ss = load->priv;
146 u8 *end = (u8*)(sector + count);
147 u32 bytes;
148 int ret;
149
150 if (end > ss->end) {
151 bytes = end - ss->end;
152 bytes += ss->pagesize - 1;
153 bytes /= ss->pagesize;
154 bytes *= ss->pagesize;
155
156 debug("downloading another 0x%x bytes\n", bytes);
157 ret = rom_api_download_image(ss->end, 0, bytes);
158
159 if (ret != ROM_API_OKAY) {
160 printf("Failure download %d\n", bytes);
161 return 0;
162 }
163
164 ss->end = end;
165 }
166
167 memcpy(buf, (void *)(sector), count);
168 return count;
169}
170
Peng Fan36986792019-09-16 03:09:31 +0000171static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
172 ulong count, void *buf)
173{
174 memcpy(buf, (void *)(sector), count);
175
176 if (load->priv) {
177 ulong *p = (ulong *)load->priv;
178 ulong total = sector + count;
179
180 if (total > *p)
181 *p = total;
182 }
183
184 return count;
185}
186
187static ulong get_fit_image_size(void *fit)
188{
189 struct spl_image_info spl_image;
190 struct spl_load_info spl_load_info;
191 ulong last = (ulong)fit;
192
193 memset(&spl_load_info, 0, sizeof(spl_load_info));
194 spl_load_info.bl_len = 1;
195 spl_load_info.read = spl_ram_load_read;
196 spl_load_info.priv = &last;
197
198 spl_load_simple_fit(&spl_image, &spl_load_info,
199 (uintptr_t)fit, fit);
200
201 return last - (ulong)fit;
202}
203
Ye Li7a71c612021-08-07 16:00:39 +0800204static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000205{
206 int i;
207
208 for (i = 0; i < size; i += 4)
209 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
210 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800211
212 return NULL;
213}
214
215static u8 *search_container_header(u8 *p, int size)
216{
217 int i = 0;
218 u8 *hdr;
219
220 for (i = 0; i < size; i += 4) {
221 hdr = p + i;
222 if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
223 return p + i;
224 }
225
226 return NULL;
227}
228
229static u8 *search_img_header(u8 *p, int size)
230{
231 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
232 return search_fit_header(p, size);
233 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
234 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000235
236 return NULL;
237}
238
Ye Li7a71c612021-08-07 16:00:39 +0800239static u32 img_header_size(void)
240{
241 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
242 return sizeof(struct fdt_header);
243 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
244 return sizeof(struct container_hdr);
245
246 return 0;
247}
248
249static int img_info_size(void *img_hdr)
250{
251#ifdef CONFIG_SPL_LOAD_FIT
252 return fit_get_size(img_hdr);
253#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
254 struct container_hdr *container = img_hdr;
255
256 return (container->length_lsb + (container->length_msb << 8));
257#else
258 return 0;
259#endif
260}
261
262static int img_total_size(void *img_hdr)
263{
264 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
265 return get_fit_image_size(img_hdr);
266 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
267 int total = get_container_size((ulong)img_hdr, NULL);
268
269 if (total < 0) {
270 printf("invalid container image\n");
271 return 0;
272 }
273
274 return total;
275 }
276
277 return 0;
278}
279
Peng Fan36986792019-09-16 03:09:31 +0000280static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
281 struct spl_boot_device *bootdev)
282{
283 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000284 u32 pagesize, pg;
285 int ret;
286 int i = 0;
287 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800288 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000289 int imagesize;
290 int total;
291
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200292 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000293
294 if (ret != ROM_API_OKAY)
295 puts("failure at query_boot_info\n");
296
297 pg = pagesize;
298 if (pg < 1024)
299 pg = 1024;
300
301 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200302 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000303
304 if (ret != ROM_API_OKAY) {
305 puts("Steam(USB) download failure\n");
306 return -1;
307 }
308
Ye Li7a71c612021-08-07 16:00:39 +0800309 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000310 p += pg;
311
Ye Li7a71c612021-08-07 16:00:39 +0800312 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000313 break;
314 }
315
Ye Li7a71c612021-08-07 16:00:39 +0800316 if (!phdr) {
317 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000318 return -1;
319 }
320
Ye Li7a71c612021-08-07 16:00:39 +0800321 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200322 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000323
324 if (ret != ROM_API_OKAY) {
325 puts("Steam(USB) download failure\n");
326 return -1;
327 }
328
329 p += pg;
330 }
331
Ye Li7a71c612021-08-07 16:00:39 +0800332 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200333 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000334
Ye Li7a71c612021-08-07 16:00:39 +0800335 if (p - phdr < imagesize) {
336 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000337 /*need pagesize hear after ROM fix USB problme*/
338 imagesize += pg - 1;
339 imagesize /= pg;
340 imagesize *= pg;
341
342 printf("Need continue download %d\n", imagesize);
343
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200344 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000345
346 p += imagesize;
347
348 if (ret != ROM_API_OKAY) {
349 printf("Failure download %d\n", imagesize);
350 return -1;
351 }
352 }
353
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200354 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
355 struct stream_state ss;
356
357 ss.base = phdr;
358 ss.end = p;
359 ss.pagesize = pagesize;
360
361 memset(&load, 0, sizeof(load));
362 load.bl_len = 1;
363 load.read = spl_romapi_read_stream;
364 load.priv = &ss;
365
366 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
367 }
368
Ye Li7a71c612021-08-07 16:00:39 +0800369 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000370 total += 3;
371 total &= ~0x3;
372
Ye Li7a71c612021-08-07 16:00:39 +0800373 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000374
375 imagesize += pagesize - 1;
376 imagesize /= pagesize;
377 imagesize *= pagesize;
378
Ye Li7a71c612021-08-07 16:00:39 +0800379 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000380
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200381 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000382 if (ret != ROM_API_OKAY)
383 printf("ROM download failure %d\n", imagesize);
384
385 memset(&load, 0, sizeof(load));
386 load.bl_len = 1;
387 load.read = spl_ram_load_read;
388
Ye Li7a71c612021-08-07 16:00:39 +0800389 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
390 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
391 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
392 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
393
394 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000395}
396
397int board_return_to_bootrom(struct spl_image_info *spl_image,
398 struct spl_boot_device *bootdev)
399{
Peng Fan36986792019-09-16 03:09:31 +0000400 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800401 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000402
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200403 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200404 if (ret != ROM_API_OKAY)
405 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000406
Marek Vasut00639162023-07-02 03:03:51 +0200407 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
408 if (ret != ROM_API_OKAY)
409 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000410
Ye Li504514b2023-02-03 18:21:47 +0800411 printf("Boot Stage: ");
412
413 switch (bstage) {
414 case BT_STAGE_PRIMARY:
415 printf("Primary boot\n");
416 break;
417 case BT_STAGE_SECONDARY:
418 printf("Secondary boot\n");
419 break;
420 case BT_STAGE_RECOVERY:
421 printf("Recovery boot\n");
422 break;
423 case BT_STAGE_USB:
424 printf("USB boot\n");
425 break;
426 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800427 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800428 }
429
Peng Fan36986792019-09-16 03:09:31 +0000430 if (is_boot_from_stream_device(boot))
431 return spl_romapi_load_image_stream(spl_image, bootdev);
432
433 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200434err:
435 puts("ROMAPI: failure at query_boot_info\n");
436 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000437}