blob: bdaea439d7f37504722ec5ea29dcf246e87383a2 [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
111 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500112 spl_set_bl_len(&load, pagesize);
Ye Li7a71c612021-08-07 16:00:39 +0800113 load.read = spl_romapi_read_seekable;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500114 return spl_load_simple_fit(spl_image, &load, offset, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400115 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
116 valid_container_hdr((void *)header)) {
Peng Fan36986792019-09-16 03:09:31 +0000117 struct spl_load_info load;
118
119 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500120 spl_set_bl_len(&load, pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000121 load.read = spl_romapi_read_seekable;
Ye Li7a71c612021-08-07 16:00:39 +0800122
Sean Anderson7d8d6132023-11-08 11:48:40 -0500123 ret = spl_load_imx_container(spl_image, &load, offset);
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
Marcel Ziswiler4bd847f2023-10-26 09:32:20 +0200165 ss->end += bytes;
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200166 }
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) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200288 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000289 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) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200308 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000309 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));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500345 spl_set_bl_len(&load, 1);
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200346 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));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500369 spl_set_bl_len(&load, 1);
Peng Fan36986792019-09-16 03:09:31 +0000370 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}