blob: b7008df8e35ad381ea6e75d02e372c74b65a5817 [file] [log] [blame]
Peng Fan36986792019-09-16 03:09:31 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
Peng Fan36986792019-09-16 03:09:31 +00006#include <errno.h>
7#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -04008#include <imx_container.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>
Peng Fan36986792019-09-16 03:09:31 +000013#include <asm/arch/sys_proto.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Ye Li7a71c612021-08-07 16:00:39 +080017/* Caller need ensure the offset and size to align with page size */
18ulong spl_romapi_raw_seekable_read(u32 offset, u32 size, void *buf)
19{
Ye Li7a71c612021-08-07 16:00:39 +080020 int ret;
21
22 debug("%s 0x%x, size 0x%x\n", __func__, offset, size);
23
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020024 ret = rom_api_download_image(buf, offset, size);
Ye Li7a71c612021-08-07 16:00:39 +080025
26 if (ret == ROM_API_OKAY)
27 return size;
28
29 printf("%s Failure when load 0x%x, size 0x%x\n", __func__, offset, size);
30
31 return 0;
32}
33
34ulong __weak spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
35{
Simon Glass18422832024-08-22 07:55:00 -060036 u32 sector = 0;
37
Dario Binacchi7457e4b2025-05-20 10:54:17 +020038#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
Simon Glass18422832024-08-22 07:55:00 -060039 sector = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR;
Dario Binacchi7457e4b2025-05-20 10:54:17 +020040#elif IS_ENABLED(CONFIG_SPL_NAND_RAW_U_BOOT_USE_SECTOR)
41 sector = CONFIG_SPL_NAND_RAW_U_BOOT_SECTOR;
Simon Glass18422832024-08-22 07:55:00 -060042#endif
43
44 return image_offset + sector * 512 - 0x8000;
Ye Li7a71c612021-08-07 16:00:39 +080045}
46
Peng Fan36986792019-09-16 03:09:31 +000047static int is_boot_from_stream_device(u32 boot)
48{
49 u32 interface;
50
51 interface = boot >> 16;
52 if (interface >= BT_DEV_TYPE_USB)
53 return 1;
54
55 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
56 return 1;
57
58 return 0;
59}
60
61static ulong spl_romapi_read_seekable(struct spl_load_info *load,
Sean Anderson7d8d6132023-11-08 11:48:40 -050062 ulong offset, ulong byte,
Peng Fan36986792019-09-16 03:09:31 +000063 void *buf)
64{
Sean Anderson7d8d6132023-11-08 11:48:40 -050065 return spl_romapi_raw_seekable_read(offset, byte, buf);
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
Simon Glasse8066862024-08-22 07:55:02 -0600109 spl_load_init(&load, spl_romapi_read_seekable, NULL, pagesize);
Sean Anderson7d8d6132023-11-08 11:48:40 -0500110 return spl_load_simple_fit(spl_image, &load, offset, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400111 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
112 valid_container_hdr((void *)header)) {
Peng Fan36986792019-09-16 03:09:31 +0000113 struct spl_load_info load;
114
Simon Glasse8066862024-08-22 07:55:02 -0600115 spl_load_init(&load, spl_romapi_read_seekable, NULL, pagesize);
Sean Anderson7d8d6132023-11-08 11:48:40 -0500116 ret = spl_load_imx_container(spl_image, &load, offset);
Peng Fan36986792019-09-16 03:09:31 +0000117 } else {
118 /* TODO */
119 puts("Can't support legacy image\n");
120 return -1;
121 }
122
123 return 0;
Marek Vasut00639162023-07-02 03:03:51 +0200124
125err:
126 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
127 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000128}
129
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200130struct stream_state {
131 u8 *base;
132 u8 *end;
133 u32 pagesize;
134};
135
136static ulong spl_romapi_read_stream(struct spl_load_info *load, ulong sector,
137 ulong count, void *buf)
138{
139 struct stream_state *ss = load->priv;
140 u8 *end = (u8*)(sector + count);
141 u32 bytes;
142 int ret;
143
144 if (end > ss->end) {
145 bytes = end - ss->end;
146 bytes += ss->pagesize - 1;
147 bytes /= ss->pagesize;
148 bytes *= ss->pagesize;
149
150 debug("downloading another 0x%x bytes\n", bytes);
151 ret = rom_api_download_image(ss->end, 0, bytes);
152
153 if (ret != ROM_API_OKAY) {
154 printf("Failure download %d\n", bytes);
155 return 0;
156 }
157
Marcel Ziswiler4bd847f2023-10-26 09:32:20 +0200158 ss->end += bytes;
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200159 }
160
161 memcpy(buf, (void *)(sector), count);
162 return count;
163}
164
Peng Fan36986792019-09-16 03:09:31 +0000165static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
166 ulong count, void *buf)
167{
168 memcpy(buf, (void *)(sector), count);
169
170 if (load->priv) {
171 ulong *p = (ulong *)load->priv;
172 ulong total = sector + count;
173
174 if (total > *p)
175 *p = total;
176 }
177
178 return count;
179}
180
Ye Li7a71c612021-08-07 16:00:39 +0800181static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000182{
183 int i;
184
185 for (i = 0; i < size; i += 4)
186 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
187 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800188
189 return NULL;
190}
191
192static u8 *search_container_header(u8 *p, int size)
193{
194 int i = 0;
195 u8 *hdr;
196
197 for (i = 0; i < size; i += 4) {
198 hdr = p + i;
Sean Andersonc5126682023-10-14 16:47:43 -0400199 if (valid_container_hdr((void *)hdr) &&
200 (*(hdr + 1) != 0 || *(hdr + 2) != 0))
Ye Li7a71c612021-08-07 16:00:39 +0800201 return p + i;
202 }
203
204 return NULL;
205}
206
207static u8 *search_img_header(u8 *p, int size)
208{
209 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
210 return search_fit_header(p, size);
211 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
212 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000213
214 return NULL;
215}
216
Ye Li7a71c612021-08-07 16:00:39 +0800217static u32 img_header_size(void)
218{
219 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
220 return sizeof(struct fdt_header);
221 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
222 return sizeof(struct container_hdr);
223
224 return 0;
225}
226
227static int img_info_size(void *img_hdr)
228{
229#ifdef CONFIG_SPL_LOAD_FIT
230 return fit_get_size(img_hdr);
231#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
232 struct container_hdr *container = img_hdr;
233
234 return (container->length_lsb + (container->length_msb << 8));
235#else
236 return 0;
237#endif
238}
239
240static int img_total_size(void *img_hdr)
241{
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200242 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Ye Li7a71c612021-08-07 16:00:39 +0800243 int total = get_container_size((ulong)img_hdr, NULL);
244
245 if (total < 0) {
246 printf("invalid container image\n");
247 return 0;
248 }
249
250 return total;
251 }
252
253 return 0;
254}
255
Peng Fan36986792019-09-16 03:09:31 +0000256static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
257 struct spl_boot_device *bootdev)
258{
259 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000260 u32 pagesize, pg;
261 int ret;
262 int i = 0;
263 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800264 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000265 int imagesize;
266 int total;
267
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200268 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000269
270 if (ret != ROM_API_OKAY)
271 puts("failure at query_boot_info\n");
272
273 pg = pagesize;
274 if (pg < 1024)
275 pg = 1024;
276
277 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200278 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000279
280 if (ret != ROM_API_OKAY) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200281 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000282 return -1;
283 }
284
Ye Li7a71c612021-08-07 16:00:39 +0800285 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000286 p += pg;
287
Ye Li7a71c612021-08-07 16:00:39 +0800288 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000289 break;
290 }
291
Ye Li7a71c612021-08-07 16:00:39 +0800292 if (!phdr) {
293 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000294 return -1;
295 }
296
Ye Li7a71c612021-08-07 16:00:39 +0800297 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200298 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000299
300 if (ret != ROM_API_OKAY) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200301 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000302 return -1;
303 }
304
305 p += pg;
306 }
307
Ye Li7a71c612021-08-07 16:00:39 +0800308 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200309 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
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200320 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000321
322 p += imagesize;
323
324 if (ret != ROM_API_OKAY) {
325 printf("Failure download %d\n", imagesize);
326 return -1;
327 }
328 }
329
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200330 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
331 struct stream_state ss;
332
333 ss.base = phdr;
334 ss.end = p;
335 ss.pagesize = pagesize;
336
Simon Glasse8066862024-08-22 07:55:02 -0600337 spl_load_init(&load, spl_romapi_read_stream, &ss, 1);
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200338
339 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
340 }
341
Ye Li7a71c612021-08-07 16:00:39 +0800342 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000343 total += 3;
344 total &= ~0x3;
345
Ye Li7a71c612021-08-07 16:00:39 +0800346 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000347
348 imagesize += pagesize - 1;
349 imagesize /= pagesize;
350 imagesize *= pagesize;
351
Ye Li7a71c612021-08-07 16:00:39 +0800352 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000353
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200354 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000355 if (ret != ROM_API_OKAY)
356 printf("ROM download failure %d\n", imagesize);
357
358 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500359 spl_set_bl_len(&load, 1);
Peng Fan36986792019-09-16 03:09:31 +0000360 load.read = spl_ram_load_read;
361
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200362 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
Ye Li7a71c612021-08-07 16:00:39 +0800363 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
364
365 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000366}
367
368int board_return_to_bootrom(struct spl_image_info *spl_image,
369 struct spl_boot_device *bootdev)
370{
Peng Fan36986792019-09-16 03:09:31 +0000371 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800372 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000373
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200374 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200375 if (ret != ROM_API_OKAY)
376 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000377
Marek Vasut00639162023-07-02 03:03:51 +0200378 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
379 if (ret != ROM_API_OKAY)
380 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000381
Ye Li504514b2023-02-03 18:21:47 +0800382 printf("Boot Stage: ");
383
384 switch (bstage) {
385 case BT_STAGE_PRIMARY:
386 printf("Primary boot\n");
387 break;
388 case BT_STAGE_SECONDARY:
389 printf("Secondary boot\n");
390 break;
391 case BT_STAGE_RECOVERY:
392 printf("Recovery boot\n");
393 break;
394 case BT_STAGE_USB:
395 printf("USB boot\n");
396 break;
397 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800398 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800399 }
400
Peng Fan36986792019-09-16 03:09:31 +0000401 if (is_boot_from_stream_device(boot))
402 return spl_romapi_load_image_stream(spl_image, bootdev);
403
404 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200405err:
406 puts("ROMAPI: failure at query_boot_info\n");
407 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000408}