blob: 93d48e56aca4e3736b3ae7e077b4eae197ca61c8 [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>
Sean Anderson952ed672023-10-14 16:47:44 -04009#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Peng Fan36986792019-09-16 03:09:31 +000012#include <linux/libfdt.h>
13#include <spl.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);
Sean Anderson952ed672023-10-14 16:47:44 -0400114 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
115 valid_container_hdr((void *)header)) {
Peng Fan36986792019-09-16 03:09:31 +0000116 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;
Ye Li7a71c612021-08-07 16:00:39 +0800122
123 ret = spl_load_imx_container(spl_image, &load, offset / pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000124 } else {
125 /* TODO */
126 puts("Can't support legacy image\n");
127 return -1;
128 }
129
130 return 0;
Marek Vasut00639162023-07-02 03:03:51 +0200131
132err:
133 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
134 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000135}
136
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200137struct stream_state {
138 u8 *base;
139 u8 *end;
140 u32 pagesize;
141};
142
143static ulong spl_romapi_read_stream(struct spl_load_info *load, ulong sector,
144 ulong count, void *buf)
145{
146 struct stream_state *ss = load->priv;
147 u8 *end = (u8*)(sector + count);
148 u32 bytes;
149 int ret;
150
151 if (end > ss->end) {
152 bytes = end - ss->end;
153 bytes += ss->pagesize - 1;
154 bytes /= ss->pagesize;
155 bytes *= ss->pagesize;
156
157 debug("downloading another 0x%x bytes\n", bytes);
158 ret = rom_api_download_image(ss->end, 0, bytes);
159
160 if (ret != ROM_API_OKAY) {
161 printf("Failure download %d\n", bytes);
162 return 0;
163 }
164
165 ss->end = end;
166 }
167
168 memcpy(buf, (void *)(sector), count);
169 return count;
170}
171
Peng Fan36986792019-09-16 03:09:31 +0000172static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
173 ulong count, void *buf)
174{
175 memcpy(buf, (void *)(sector), count);
176
177 if (load->priv) {
178 ulong *p = (ulong *)load->priv;
179 ulong total = sector + count;
180
181 if (total > *p)
182 *p = total;
183 }
184
185 return count;
186}
187
Ye Li7a71c612021-08-07 16:00:39 +0800188static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000189{
190 int i;
191
192 for (i = 0; i < size; i += 4)
193 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
194 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800195
196 return NULL;
197}
198
199static u8 *search_container_header(u8 *p, int size)
200{
201 int i = 0;
202 u8 *hdr;
203
204 for (i = 0; i < size; i += 4) {
205 hdr = p + i;
Sean Andersonc5126682023-10-14 16:47:43 -0400206 if (valid_container_hdr((void *)hdr) &&
207 (*(hdr + 1) != 0 || *(hdr + 2) != 0))
Ye Li7a71c612021-08-07 16:00:39 +0800208 return p + i;
209 }
210
211 return NULL;
212}
213
214static u8 *search_img_header(u8 *p, int size)
215{
216 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
217 return search_fit_header(p, size);
218 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
219 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000220
221 return NULL;
222}
223
Ye Li7a71c612021-08-07 16:00:39 +0800224static u32 img_header_size(void)
225{
226 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
227 return sizeof(struct fdt_header);
228 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
229 return sizeof(struct container_hdr);
230
231 return 0;
232}
233
234static int img_info_size(void *img_hdr)
235{
236#ifdef CONFIG_SPL_LOAD_FIT
237 return fit_get_size(img_hdr);
238#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
239 struct container_hdr *container = img_hdr;
240
241 return (container->length_lsb + (container->length_msb << 8));
242#else
243 return 0;
244#endif
245}
246
247static int img_total_size(void *img_hdr)
248{
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200249 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Ye Li7a71c612021-08-07 16:00:39 +0800250 int total = get_container_size((ulong)img_hdr, NULL);
251
252 if (total < 0) {
253 printf("invalid container image\n");
254 return 0;
255 }
256
257 return total;
258 }
259
260 return 0;
261}
262
Peng Fan36986792019-09-16 03:09:31 +0000263static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
264 struct spl_boot_device *bootdev)
265{
266 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000267 u32 pagesize, pg;
268 int ret;
269 int i = 0;
270 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800271 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000272 int imagesize;
273 int total;
274
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200275 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000276
277 if (ret != ROM_API_OKAY)
278 puts("failure at query_boot_info\n");
279
280 pg = pagesize;
281 if (pg < 1024)
282 pg = 1024;
283
284 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200285 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000286
287 if (ret != ROM_API_OKAY) {
288 puts("Steam(USB) download failure\n");
289 return -1;
290 }
291
Ye Li7a71c612021-08-07 16:00:39 +0800292 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000293 p += pg;
294
Ye Li7a71c612021-08-07 16:00:39 +0800295 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000296 break;
297 }
298
Ye Li7a71c612021-08-07 16:00:39 +0800299 if (!phdr) {
300 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000301 return -1;
302 }
303
Ye Li7a71c612021-08-07 16:00:39 +0800304 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200305 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000306
307 if (ret != ROM_API_OKAY) {
308 puts("Steam(USB) download failure\n");
309 return -1;
310 }
311
312 p += pg;
313 }
314
Ye Li7a71c612021-08-07 16:00:39 +0800315 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200316 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000317
Ye Li7a71c612021-08-07 16:00:39 +0800318 if (p - phdr < imagesize) {
319 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000320 /*need pagesize hear after ROM fix USB problme*/
321 imagesize += pg - 1;
322 imagesize /= pg;
323 imagesize *= pg;
324
325 printf("Need continue download %d\n", imagesize);
326
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200327 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000328
329 p += imagesize;
330
331 if (ret != ROM_API_OKAY) {
332 printf("Failure download %d\n", imagesize);
333 return -1;
334 }
335 }
336
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200337 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
338 struct stream_state ss;
339
340 ss.base = phdr;
341 ss.end = p;
342 ss.pagesize = pagesize;
343
344 memset(&load, 0, sizeof(load));
345 load.bl_len = 1;
346 load.read = spl_romapi_read_stream;
347 load.priv = &ss;
348
349 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
350 }
351
Ye Li7a71c612021-08-07 16:00:39 +0800352 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000353 total += 3;
354 total &= ~0x3;
355
Ye Li7a71c612021-08-07 16:00:39 +0800356 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000357
358 imagesize += pagesize - 1;
359 imagesize /= pagesize;
360 imagesize *= pagesize;
361
Ye Li7a71c612021-08-07 16:00:39 +0800362 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000363
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200364 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000365 if (ret != ROM_API_OKAY)
366 printf("ROM download failure %d\n", imagesize);
367
368 memset(&load, 0, sizeof(load));
369 load.bl_len = 1;
370 load.read = spl_ram_load_read;
371
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200372 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
Ye Li7a71c612021-08-07 16:00:39 +0800373 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
374
375 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000376}
377
378int board_return_to_bootrom(struct spl_image_info *spl_image,
379 struct spl_boot_device *bootdev)
380{
Peng Fan36986792019-09-16 03:09:31 +0000381 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800382 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000383
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200384 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200385 if (ret != ROM_API_OKAY)
386 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000387
Marek Vasut00639162023-07-02 03:03:51 +0200388 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
389 if (ret != ROM_API_OKAY)
390 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000391
Ye Li504514b2023-02-03 18:21:47 +0800392 printf("Boot Stage: ");
393
394 switch (bstage) {
395 case BT_STAGE_PRIMARY:
396 printf("Primary boot\n");
397 break;
398 case BT_STAGE_SECONDARY:
399 printf("Secondary boot\n");
400 break;
401 case BT_STAGE_RECOVERY:
402 printf("Recovery boot\n");
403 break;
404 case BT_STAGE_USB:
405 printf("USB boot\n");
406 break;
407 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800408 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800409 }
410
Peng Fan36986792019-09-16 03:09:31 +0000411 if (is_boot_from_stream_device(boot))
412 return spl_romapi_load_image_stream(spl_image, bootdev);
413
414 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200415err:
416 puts("ROMAPI: failure at query_boot_info\n");
417 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000418}