blob: 3982f4cca184716f0000a28767c14a57567ed54d [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
38 /*
39 * Some boards use this value even though MMC is not enabled in SPL, for
40 * example imx8mn_bsh_smm_s2
41 */
42#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
43 sector = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR;
44#endif
45
46 return image_offset + sector * 512 - 0x8000;
Ye Li7a71c612021-08-07 16:00:39 +080047}
48
Peng Fan36986792019-09-16 03:09:31 +000049static int is_boot_from_stream_device(u32 boot)
50{
51 u32 interface;
52
53 interface = boot >> 16;
54 if (interface >= BT_DEV_TYPE_USB)
55 return 1;
56
57 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
58 return 1;
59
60 return 0;
61}
62
63static ulong spl_romapi_read_seekable(struct spl_load_info *load,
Sean Anderson7d8d6132023-11-08 11:48:40 -050064 ulong offset, ulong byte,
Peng Fan36986792019-09-16 03:09:31 +000065 void *buf)
66{
Sean Anderson7d8d6132023-11-08 11:48:40 -050067 return spl_romapi_raw_seekable_read(offset, byte, buf);
Peng Fan36986792019-09-16 03:09:31 +000068}
69
70static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
71 struct spl_boot_device *bootdev,
72 u32 rom_bt_dev)
73{
Peng Fan36986792019-09-16 03:09:31 +000074 int ret;
75 u32 offset;
76 u32 pagesize, size;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060077 struct legacy_img_hdr *header;
Peng Fan36986792019-09-16 03:09:31 +000078 u32 image_offset;
79
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020080 ret = rom_api_query_boot_infor(QUERY_IVT_OFF, &offset);
Marek Vasut00639162023-07-02 03:03:51 +020081 if (ret != ROM_API_OKAY)
82 goto err;
Peng Fan36986792019-09-16 03:09:31 +000083
Marek Vasut00639162023-07-02 03:03:51 +020084 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
85 if (ret != ROM_API_OKAY)
86 goto err;
87
88 ret = rom_api_query_boot_infor(QUERY_IMG_OFF, &image_offset);
89 if (ret != ROM_API_OKAY)
90 goto err;
Peng Fan36986792019-09-16 03:09:31 +000091
Simon Glassbb7d3bb2022-09-06 20:26:52 -060092 header = (struct legacy_img_hdr *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
Peng Fan36986792019-09-16 03:09:31 +000093
94 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
95 image_offset, pagesize, offset);
96
Ye Li7a71c612021-08-07 16:00:39 +080097 offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
Peng Fan36986792019-09-16 03:09:31 +000098
Simon Glassbb7d3bb2022-09-06 20:26:52 -060099 size = ALIGN(sizeof(struct legacy_img_hdr), pagesize);
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200100 ret = rom_api_download_image((u8 *)header, offset, size);
Peng Fan36986792019-09-16 03:09:31 +0000101
102 if (ret != ROM_API_OKAY) {
103 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
104 offset, size);
105 return -1;
106 }
107
Ye Li7a71c612021-08-07 16:00:39 +0800108 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
109 struct spl_load_info load;
110
Simon Glasse8066862024-08-22 07:55:02 -0600111 spl_load_init(&load, spl_romapi_read_seekable, NULL, pagesize);
Sean Anderson7d8d6132023-11-08 11:48:40 -0500112 return spl_load_simple_fit(spl_image, &load, offset, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400113 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
114 valid_container_hdr((void *)header)) {
Peng Fan36986792019-09-16 03:09:31 +0000115 struct spl_load_info load;
116
Simon Glasse8066862024-08-22 07:55:02 -0600117 spl_load_init(&load, spl_romapi_read_seekable, NULL, pagesize);
Sean Anderson7d8d6132023-11-08 11:48:40 -0500118 ret = spl_load_imx_container(spl_image, &load, offset);
Peng Fan36986792019-09-16 03:09:31 +0000119 } else {
120 /* TODO */
121 puts("Can't support legacy image\n");
122 return -1;
123 }
124
125 return 0;
Marek Vasut00639162023-07-02 03:03:51 +0200126
127err:
128 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
129 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000130}
131
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200132struct stream_state {
133 u8 *base;
134 u8 *end;
135 u32 pagesize;
136};
137
138static ulong spl_romapi_read_stream(struct spl_load_info *load, ulong sector,
139 ulong count, void *buf)
140{
141 struct stream_state *ss = load->priv;
142 u8 *end = (u8*)(sector + count);
143 u32 bytes;
144 int ret;
145
146 if (end > ss->end) {
147 bytes = end - ss->end;
148 bytes += ss->pagesize - 1;
149 bytes /= ss->pagesize;
150 bytes *= ss->pagesize;
151
152 debug("downloading another 0x%x bytes\n", bytes);
153 ret = rom_api_download_image(ss->end, 0, bytes);
154
155 if (ret != ROM_API_OKAY) {
156 printf("Failure download %d\n", bytes);
157 return 0;
158 }
159
Marcel Ziswiler4bd847f2023-10-26 09:32:20 +0200160 ss->end += bytes;
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200161 }
162
163 memcpy(buf, (void *)(sector), count);
164 return count;
165}
166
Peng Fan36986792019-09-16 03:09:31 +0000167static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
168 ulong count, void *buf)
169{
170 memcpy(buf, (void *)(sector), count);
171
172 if (load->priv) {
173 ulong *p = (ulong *)load->priv;
174 ulong total = sector + count;
175
176 if (total > *p)
177 *p = total;
178 }
179
180 return count;
181}
182
Ye Li7a71c612021-08-07 16:00:39 +0800183static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000184{
185 int i;
186
187 for (i = 0; i < size; i += 4)
188 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
189 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800190
191 return NULL;
192}
193
194static u8 *search_container_header(u8 *p, int size)
195{
196 int i = 0;
197 u8 *hdr;
198
199 for (i = 0; i < size; i += 4) {
200 hdr = p + i;
Sean Andersonc5126682023-10-14 16:47:43 -0400201 if (valid_container_hdr((void *)hdr) &&
202 (*(hdr + 1) != 0 || *(hdr + 2) != 0))
Ye Li7a71c612021-08-07 16:00:39 +0800203 return p + i;
204 }
205
206 return NULL;
207}
208
209static u8 *search_img_header(u8 *p, int size)
210{
211 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
212 return search_fit_header(p, size);
213 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
214 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000215
216 return NULL;
217}
218
Ye Li7a71c612021-08-07 16:00:39 +0800219static u32 img_header_size(void)
220{
221 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
222 return sizeof(struct fdt_header);
223 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
224 return sizeof(struct container_hdr);
225
226 return 0;
227}
228
229static int img_info_size(void *img_hdr)
230{
231#ifdef CONFIG_SPL_LOAD_FIT
232 return fit_get_size(img_hdr);
233#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
234 struct container_hdr *container = img_hdr;
235
236 return (container->length_lsb + (container->length_msb << 8));
237#else
238 return 0;
239#endif
240}
241
242static int img_total_size(void *img_hdr)
243{
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200244 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Ye Li7a71c612021-08-07 16:00:39 +0800245 int total = get_container_size((ulong)img_hdr, NULL);
246
247 if (total < 0) {
248 printf("invalid container image\n");
249 return 0;
250 }
251
252 return total;
253 }
254
255 return 0;
256}
257
Peng Fan36986792019-09-16 03:09:31 +0000258static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
259 struct spl_boot_device *bootdev)
260{
261 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000262 u32 pagesize, pg;
263 int ret;
264 int i = 0;
265 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800266 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000267 int imagesize;
268 int total;
269
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200270 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000271
272 if (ret != ROM_API_OKAY)
273 puts("failure at query_boot_info\n");
274
275 pg = pagesize;
276 if (pg < 1024)
277 pg = 1024;
278
279 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200280 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000281
282 if (ret != ROM_API_OKAY) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200283 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000284 return -1;
285 }
286
Ye Li7a71c612021-08-07 16:00:39 +0800287 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000288 p += pg;
289
Ye Li7a71c612021-08-07 16:00:39 +0800290 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000291 break;
292 }
293
Ye Li7a71c612021-08-07 16:00:39 +0800294 if (!phdr) {
295 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000296 return -1;
297 }
298
Ye Li7a71c612021-08-07 16:00:39 +0800299 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200300 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000301
302 if (ret != ROM_API_OKAY) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200303 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000304 return -1;
305 }
306
307 p += pg;
308 }
309
Ye Li7a71c612021-08-07 16:00:39 +0800310 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200311 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000312
Ye Li7a71c612021-08-07 16:00:39 +0800313 if (p - phdr < imagesize) {
314 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000315 /*need pagesize hear after ROM fix USB problme*/
316 imagesize += pg - 1;
317 imagesize /= pg;
318 imagesize *= pg;
319
320 printf("Need continue download %d\n", imagesize);
321
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200322 ret = rom_api_download_image(p, 0, imagesize);
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
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200332 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
333 struct stream_state ss;
334
335 ss.base = phdr;
336 ss.end = p;
337 ss.pagesize = pagesize;
338
Simon Glasse8066862024-08-22 07:55:02 -0600339 spl_load_init(&load, spl_romapi_read_stream, &ss, 1);
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200340
341 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
342 }
343
Ye Li7a71c612021-08-07 16:00:39 +0800344 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000345 total += 3;
346 total &= ~0x3;
347
Ye Li7a71c612021-08-07 16:00:39 +0800348 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000349
350 imagesize += pagesize - 1;
351 imagesize /= pagesize;
352 imagesize *= pagesize;
353
Ye Li7a71c612021-08-07 16:00:39 +0800354 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000355
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200356 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000357 if (ret != ROM_API_OKAY)
358 printf("ROM download failure %d\n", imagesize);
359
360 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500361 spl_set_bl_len(&load, 1);
Peng Fan36986792019-09-16 03:09:31 +0000362 load.read = spl_ram_load_read;
363
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200364 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
Ye Li7a71c612021-08-07 16:00:39 +0800365 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
366
367 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000368}
369
370int board_return_to_bootrom(struct spl_image_info *spl_image,
371 struct spl_boot_device *bootdev)
372{
Peng Fan36986792019-09-16 03:09:31 +0000373 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800374 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000375
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200376 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200377 if (ret != ROM_API_OKAY)
378 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000379
Marek Vasut00639162023-07-02 03:03:51 +0200380 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
381 if (ret != ROM_API_OKAY)
382 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000383
Ye Li504514b2023-02-03 18:21:47 +0800384 printf("Boot Stage: ");
385
386 switch (bstage) {
387 case BT_STAGE_PRIMARY:
388 printf("Primary boot\n");
389 break;
390 case BT_STAGE_SECONDARY:
391 printf("Secondary boot\n");
392 break;
393 case BT_STAGE_RECOVERY:
394 printf("Recovery boot\n");
395 break;
396 case BT_STAGE_USB:
397 printf("USB boot\n");
398 break;
399 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800400 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800401 }
402
Peng Fan36986792019-09-16 03:09:31 +0000403 if (is_boot_from_stream_device(boot))
404 return spl_romapi_load_image_stream(spl_image, bootdev);
405
406 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200407err:
408 puts("ROMAPI: failure at query_boot_info\n");
409 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000410}