blob: 4af41699678cd8a520f25689ad070888f844c75d [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
136static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
137 ulong count, void *buf)
138{
139 memcpy(buf, (void *)(sector), count);
140
141 if (load->priv) {
142 ulong *p = (ulong *)load->priv;
143 ulong total = sector + count;
144
145 if (total > *p)
146 *p = total;
147 }
148
149 return count;
150}
151
152static ulong get_fit_image_size(void *fit)
153{
154 struct spl_image_info spl_image;
155 struct spl_load_info spl_load_info;
156 ulong last = (ulong)fit;
157
158 memset(&spl_load_info, 0, sizeof(spl_load_info));
159 spl_load_info.bl_len = 1;
160 spl_load_info.read = spl_ram_load_read;
161 spl_load_info.priv = &last;
162
163 spl_load_simple_fit(&spl_image, &spl_load_info,
164 (uintptr_t)fit, fit);
165
166 return last - (ulong)fit;
167}
168
Ye Li7a71c612021-08-07 16:00:39 +0800169static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000170{
171 int i;
172
173 for (i = 0; i < size; i += 4)
174 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
175 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800176
177 return NULL;
178}
179
180static u8 *search_container_header(u8 *p, int size)
181{
182 int i = 0;
183 u8 *hdr;
184
185 for (i = 0; i < size; i += 4) {
186 hdr = p + i;
187 if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
188 return p + i;
189 }
190
191 return NULL;
192}
193
194static u8 *search_img_header(u8 *p, int size)
195{
196 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
197 return search_fit_header(p, size);
198 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
199 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000200
201 return NULL;
202}
203
Ye Li7a71c612021-08-07 16:00:39 +0800204static u32 img_header_size(void)
205{
206 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
207 return sizeof(struct fdt_header);
208 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
209 return sizeof(struct container_hdr);
210
211 return 0;
212}
213
214static int img_info_size(void *img_hdr)
215{
216#ifdef CONFIG_SPL_LOAD_FIT
217 return fit_get_size(img_hdr);
218#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
219 struct container_hdr *container = img_hdr;
220
221 return (container->length_lsb + (container->length_msb << 8));
222#else
223 return 0;
224#endif
225}
226
227static int img_total_size(void *img_hdr)
228{
229 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
230 return get_fit_image_size(img_hdr);
231 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
232 int total = get_container_size((ulong)img_hdr, NULL);
233
234 if (total < 0) {
235 printf("invalid container image\n");
236 return 0;
237 }
238
239 return total;
240 }
241
242 return 0;
243}
244
Peng Fan36986792019-09-16 03:09:31 +0000245static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
246 struct spl_boot_device *bootdev)
247{
248 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000249 u32 pagesize, pg;
250 int ret;
251 int i = 0;
252 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800253 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000254 int imagesize;
255 int total;
256
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200257 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000258
259 if (ret != ROM_API_OKAY)
260 puts("failure at query_boot_info\n");
261
262 pg = pagesize;
263 if (pg < 1024)
264 pg = 1024;
265
266 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200267 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000268
269 if (ret != ROM_API_OKAY) {
270 puts("Steam(USB) download failure\n");
271 return -1;
272 }
273
Ye Li7a71c612021-08-07 16:00:39 +0800274 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000275 p += pg;
276
Ye Li7a71c612021-08-07 16:00:39 +0800277 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000278 break;
279 }
280
Ye Li7a71c612021-08-07 16:00:39 +0800281 if (!phdr) {
282 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000283 return -1;
284 }
285
Ye Li7a71c612021-08-07 16:00:39 +0800286 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200287 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000288
289 if (ret != ROM_API_OKAY) {
290 puts("Steam(USB) download failure\n");
291 return -1;
292 }
293
294 p += pg;
295 }
296
Ye Li7a71c612021-08-07 16:00:39 +0800297 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200298 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000299
Ye Li7a71c612021-08-07 16:00:39 +0800300 if (p - phdr < imagesize) {
301 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000302 /*need pagesize hear after ROM fix USB problme*/
303 imagesize += pg - 1;
304 imagesize /= pg;
305 imagesize *= pg;
306
307 printf("Need continue download %d\n", imagesize);
308
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200309 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000310
311 p += imagesize;
312
313 if (ret != ROM_API_OKAY) {
314 printf("Failure download %d\n", imagesize);
315 return -1;
316 }
317 }
318
Ye Li7a71c612021-08-07 16:00:39 +0800319 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000320 total += 3;
321 total &= ~0x3;
322
Ye Li7a71c612021-08-07 16:00:39 +0800323 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000324
325 imagesize += pagesize - 1;
326 imagesize /= pagesize;
327 imagesize *= pagesize;
328
Ye Li7a71c612021-08-07 16:00:39 +0800329 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000330
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200331 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000332 if (ret != ROM_API_OKAY)
333 printf("ROM download failure %d\n", imagesize);
334
335 memset(&load, 0, sizeof(load));
336 load.bl_len = 1;
337 load.read = spl_ram_load_read;
338
Ye Li7a71c612021-08-07 16:00:39 +0800339 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
340 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
341 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
342 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
343
344 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000345}
346
347int board_return_to_bootrom(struct spl_image_info *spl_image,
348 struct spl_boot_device *bootdev)
349{
Peng Fan36986792019-09-16 03:09:31 +0000350 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800351 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000352
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200353 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200354 if (ret != ROM_API_OKAY)
355 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000356
Marek Vasut00639162023-07-02 03:03:51 +0200357 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
358 if (ret != ROM_API_OKAY)
359 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000360
Ye Li504514b2023-02-03 18:21:47 +0800361 printf("Boot Stage: ");
362
363 switch (bstage) {
364 case BT_STAGE_PRIMARY:
365 printf("Primary boot\n");
366 break;
367 case BT_STAGE_SECONDARY:
368 printf("Secondary boot\n");
369 break;
370 case BT_STAGE_RECOVERY:
371 printf("Recovery boot\n");
372 break;
373 case BT_STAGE_USB:
374 printf("USB boot\n");
375 break;
376 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800377 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800378 }
379
Peng Fan36986792019-09-16 03:09:31 +0000380 if (is_boot_from_stream_device(boot))
381 return spl_romapi_load_image_stream(spl_image, bootdev);
382
383 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200384err:
385 puts("ROMAPI: failure at query_boot_info\n");
386 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000387}