blob: c4a4185eed1fb5c5cdad17494c7504e68b2a7171 [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
Ye Li7a71c612021-08-07 16:00:39 +0800187static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000188{
189 int i;
190
191 for (i = 0; i < size; i += 4)
192 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
193 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800194
195 return NULL;
196}
197
198static u8 *search_container_header(u8 *p, int size)
199{
200 int i = 0;
201 u8 *hdr;
202
203 for (i = 0; i < size; i += 4) {
204 hdr = p + i;
205 if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
206 return p + i;
207 }
208
209 return NULL;
210}
211
212static u8 *search_img_header(u8 *p, int size)
213{
214 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
215 return search_fit_header(p, size);
216 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
217 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000218
219 return NULL;
220}
221
Ye Li7a71c612021-08-07 16:00:39 +0800222static u32 img_header_size(void)
223{
224 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
225 return sizeof(struct fdt_header);
226 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
227 return sizeof(struct container_hdr);
228
229 return 0;
230}
231
232static int img_info_size(void *img_hdr)
233{
234#ifdef CONFIG_SPL_LOAD_FIT
235 return fit_get_size(img_hdr);
236#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
237 struct container_hdr *container = img_hdr;
238
239 return (container->length_lsb + (container->length_msb << 8));
240#else
241 return 0;
242#endif
243}
244
245static int img_total_size(void *img_hdr)
246{
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200247 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Ye Li7a71c612021-08-07 16:00:39 +0800248 int total = get_container_size((ulong)img_hdr, NULL);
249
250 if (total < 0) {
251 printf("invalid container image\n");
252 return 0;
253 }
254
255 return total;
256 }
257
258 return 0;
259}
260
Peng Fan36986792019-09-16 03:09:31 +0000261static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
262 struct spl_boot_device *bootdev)
263{
264 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000265 u32 pagesize, pg;
266 int ret;
267 int i = 0;
268 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800269 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000270 int imagesize;
271 int total;
272
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200273 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000274
275 if (ret != ROM_API_OKAY)
276 puts("failure at query_boot_info\n");
277
278 pg = pagesize;
279 if (pg < 1024)
280 pg = 1024;
281
282 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200283 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000284
285 if (ret != ROM_API_OKAY) {
286 puts("Steam(USB) download failure\n");
287 return -1;
288 }
289
Ye Li7a71c612021-08-07 16:00:39 +0800290 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000291 p += pg;
292
Ye Li7a71c612021-08-07 16:00:39 +0800293 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000294 break;
295 }
296
Ye Li7a71c612021-08-07 16:00:39 +0800297 if (!phdr) {
298 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000299 return -1;
300 }
301
Ye Li7a71c612021-08-07 16:00:39 +0800302 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200303 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000304
305 if (ret != ROM_API_OKAY) {
306 puts("Steam(USB) download failure\n");
307 return -1;
308 }
309
310 p += pg;
311 }
312
Ye Li7a71c612021-08-07 16:00:39 +0800313 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200314 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000315
Ye Li7a71c612021-08-07 16:00:39 +0800316 if (p - phdr < imagesize) {
317 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000318 /*need pagesize hear after ROM fix USB problme*/
319 imagesize += pg - 1;
320 imagesize /= pg;
321 imagesize *= pg;
322
323 printf("Need continue download %d\n", imagesize);
324
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200325 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000326
327 p += imagesize;
328
329 if (ret != ROM_API_OKAY) {
330 printf("Failure download %d\n", imagesize);
331 return -1;
332 }
333 }
334
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200335 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
336 struct stream_state ss;
337
338 ss.base = phdr;
339 ss.end = p;
340 ss.pagesize = pagesize;
341
342 memset(&load, 0, sizeof(load));
343 load.bl_len = 1;
344 load.read = spl_romapi_read_stream;
345 load.priv = &ss;
346
347 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
348 }
349
Ye Li7a71c612021-08-07 16:00:39 +0800350 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000351 total += 3;
352 total &= ~0x3;
353
Ye Li7a71c612021-08-07 16:00:39 +0800354 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000355
356 imagesize += pagesize - 1;
357 imagesize /= pagesize;
358 imagesize *= pagesize;
359
Ye Li7a71c612021-08-07 16:00:39 +0800360 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000361
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200362 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000363 if (ret != ROM_API_OKAY)
364 printf("ROM download failure %d\n", imagesize);
365
366 memset(&load, 0, sizeof(load));
367 load.bl_len = 1;
368 load.read = spl_ram_load_read;
369
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200370 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
Ye Li7a71c612021-08-07 16:00:39 +0800371 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
372
373 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000374}
375
376int board_return_to_bootrom(struct spl_image_info *spl_image,
377 struct spl_boot_device *bootdev)
378{
Peng Fan36986792019-09-16 03:09:31 +0000379 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800380 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000381
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200382 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200383 if (ret != ROM_API_OKAY)
384 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000385
Marek Vasut00639162023-07-02 03:03:51 +0200386 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
387 if (ret != ROM_API_OKAY)
388 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000389
Ye Li504514b2023-02-03 18:21:47 +0800390 printf("Boot Stage: ");
391
392 switch (bstage) {
393 case BT_STAGE_PRIMARY:
394 printf("Primary boot\n");
395 break;
396 case BT_STAGE_SECONDARY:
397 printf("Secondary boot\n");
398 break;
399 case BT_STAGE_RECOVERY:
400 printf("Recovery boot\n");
401 break;
402 case BT_STAGE_USB:
403 printf("USB boot\n");
404 break;
405 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800406 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800407 }
408
Peng Fan36986792019-09-16 03:09:31 +0000409 if (is_boot_from_stream_device(boot))
410 return spl_romapi_load_image_stream(spl_image, bootdev);
411
412 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200413err:
414 puts("ROMAPI: failure at query_boot_info\n");
415 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000416}