blob: 9a86f5c133f73fa8943d4ad1b96a6b81bf682a11 [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{
Marek Vasut85107562022-03-09 17:09:45 +010036 return image_offset +
37 (CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000);
Ye Li7a71c612021-08-07 16:00:39 +080038}
39
Peng Fan36986792019-09-16 03:09:31 +000040static int is_boot_from_stream_device(u32 boot)
41{
42 u32 interface;
43
44 interface = boot >> 16;
45 if (interface >= BT_DEV_TYPE_USB)
46 return 1;
47
48 if (interface == BT_DEV_TYPE_MMC && (boot & 1))
49 return 1;
50
51 return 0;
52}
53
54static ulong spl_romapi_read_seekable(struct spl_load_info *load,
Sean Anderson7d8d6132023-11-08 11:48:40 -050055 ulong offset, ulong byte,
Peng Fan36986792019-09-16 03:09:31 +000056 void *buf)
57{
Sean Anderson7d8d6132023-11-08 11:48:40 -050058 return spl_romapi_raw_seekable_read(offset, byte, buf);
Peng Fan36986792019-09-16 03:09:31 +000059}
60
61static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
62 struct spl_boot_device *bootdev,
63 u32 rom_bt_dev)
64{
Peng Fan36986792019-09-16 03:09:31 +000065 int ret;
66 u32 offset;
67 u32 pagesize, size;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060068 struct legacy_img_hdr *header;
Peng Fan36986792019-09-16 03:09:31 +000069 u32 image_offset;
70
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020071 ret = rom_api_query_boot_infor(QUERY_IVT_OFF, &offset);
Marek Vasut00639162023-07-02 03:03:51 +020072 if (ret != ROM_API_OKAY)
73 goto err;
Peng Fan36986792019-09-16 03:09:31 +000074
Marek Vasut00639162023-07-02 03:03:51 +020075 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
76 if (ret != ROM_API_OKAY)
77 goto err;
78
79 ret = rom_api_query_boot_infor(QUERY_IMG_OFF, &image_offset);
80 if (ret != ROM_API_OKAY)
81 goto err;
Peng Fan36986792019-09-16 03:09:31 +000082
Simon Glassbb7d3bb2022-09-06 20:26:52 -060083 header = (struct legacy_img_hdr *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
Peng Fan36986792019-09-16 03:09:31 +000084
85 printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
86 image_offset, pagesize, offset);
87
Ye Li7a71c612021-08-07 16:00:39 +080088 offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
Peng Fan36986792019-09-16 03:09:31 +000089
Simon Glassbb7d3bb2022-09-06 20:26:52 -060090 size = ALIGN(sizeof(struct legacy_img_hdr), pagesize);
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +020091 ret = rom_api_download_image((u8 *)header, offset, size);
Peng Fan36986792019-09-16 03:09:31 +000092
93 if (ret != ROM_API_OKAY) {
94 printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
95 offset, size);
96 return -1;
97 }
98
Ye Li7a71c612021-08-07 16:00:39 +080099 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
100 struct spl_load_info load;
101
102 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500103 spl_set_bl_len(&load, pagesize);
Ye Li7a71c612021-08-07 16:00:39 +0800104 load.read = spl_romapi_read_seekable;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500105 return spl_load_simple_fit(spl_image, &load, offset, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400106 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
107 valid_container_hdr((void *)header)) {
Peng Fan36986792019-09-16 03:09:31 +0000108 struct spl_load_info load;
109
110 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500111 spl_set_bl_len(&load, pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000112 load.read = spl_romapi_read_seekable;
Ye Li7a71c612021-08-07 16:00:39 +0800113
Sean Anderson7d8d6132023-11-08 11:48:40 -0500114 ret = spl_load_imx_container(spl_image, &load, offset);
Peng Fan36986792019-09-16 03:09:31 +0000115 } else {
116 /* TODO */
117 puts("Can't support legacy image\n");
118 return -1;
119 }
120
121 return 0;
Marek Vasut00639162023-07-02 03:03:51 +0200122
123err:
124 puts("ROMAPI: Failure query boot infor pagesize/offset\n");
125 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000126}
127
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200128struct stream_state {
129 u8 *base;
130 u8 *end;
131 u32 pagesize;
132};
133
134static ulong spl_romapi_read_stream(struct spl_load_info *load, ulong sector,
135 ulong count, void *buf)
136{
137 struct stream_state *ss = load->priv;
138 u8 *end = (u8*)(sector + count);
139 u32 bytes;
140 int ret;
141
142 if (end > ss->end) {
143 bytes = end - ss->end;
144 bytes += ss->pagesize - 1;
145 bytes /= ss->pagesize;
146 bytes *= ss->pagesize;
147
148 debug("downloading another 0x%x bytes\n", bytes);
149 ret = rom_api_download_image(ss->end, 0, bytes);
150
151 if (ret != ROM_API_OKAY) {
152 printf("Failure download %d\n", bytes);
153 return 0;
154 }
155
Marcel Ziswiler4bd847f2023-10-26 09:32:20 +0200156 ss->end += bytes;
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200157 }
158
159 memcpy(buf, (void *)(sector), count);
160 return count;
161}
162
Peng Fan36986792019-09-16 03:09:31 +0000163static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
164 ulong count, void *buf)
165{
166 memcpy(buf, (void *)(sector), count);
167
168 if (load->priv) {
169 ulong *p = (ulong *)load->priv;
170 ulong total = sector + count;
171
172 if (total > *p)
173 *p = total;
174 }
175
176 return count;
177}
178
Ye Li7a71c612021-08-07 16:00:39 +0800179static u8 *search_fit_header(u8 *p, int size)
Peng Fan36986792019-09-16 03:09:31 +0000180{
181 int i;
182
183 for (i = 0; i < size; i += 4)
184 if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
185 return p + i;
Ye Li7a71c612021-08-07 16:00:39 +0800186
187 return NULL;
188}
189
190static u8 *search_container_header(u8 *p, int size)
191{
192 int i = 0;
193 u8 *hdr;
194
195 for (i = 0; i < size; i += 4) {
196 hdr = p + i;
Sean Andersonc5126682023-10-14 16:47:43 -0400197 if (valid_container_hdr((void *)hdr) &&
198 (*(hdr + 1) != 0 || *(hdr + 2) != 0))
Ye Li7a71c612021-08-07 16:00:39 +0800199 return p + i;
200 }
201
202 return NULL;
203}
204
205static u8 *search_img_header(u8 *p, int size)
206{
207 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
208 return search_fit_header(p, size);
209 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
210 return search_container_header(p, size);
Peng Fan36986792019-09-16 03:09:31 +0000211
212 return NULL;
213}
214
Ye Li7a71c612021-08-07 16:00:39 +0800215static u32 img_header_size(void)
216{
217 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
218 return sizeof(struct fdt_header);
219 else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
220 return sizeof(struct container_hdr);
221
222 return 0;
223}
224
225static int img_info_size(void *img_hdr)
226{
227#ifdef CONFIG_SPL_LOAD_FIT
228 return fit_get_size(img_hdr);
229#elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
230 struct container_hdr *container = img_hdr;
231
232 return (container->length_lsb + (container->length_msb << 8));
233#else
234 return 0;
235#endif
236}
237
238static int img_total_size(void *img_hdr)
239{
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200240 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
Ye Li7a71c612021-08-07 16:00:39 +0800241 int total = get_container_size((ulong)img_hdr, NULL);
242
243 if (total < 0) {
244 printf("invalid container image\n");
245 return 0;
246 }
247
248 return total;
249 }
250
251 return 0;
252}
253
Peng Fan36986792019-09-16 03:09:31 +0000254static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
255 struct spl_boot_device *bootdev)
256{
257 struct spl_load_info load;
Peng Fan36986792019-09-16 03:09:31 +0000258 u32 pagesize, pg;
259 int ret;
260 int i = 0;
261 u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
Ye Li7a71c612021-08-07 16:00:39 +0800262 u8 *phdr = NULL;
Peng Fan36986792019-09-16 03:09:31 +0000263 int imagesize;
264 int total;
265
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200266 ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
Peng Fan36986792019-09-16 03:09:31 +0000267
268 if (ret != ROM_API_OKAY)
269 puts("failure at query_boot_info\n");
270
271 pg = pagesize;
272 if (pg < 1024)
273 pg = 1024;
274
275 for (i = 0; i < 640; i++) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200276 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000277
278 if (ret != ROM_API_OKAY) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200279 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000280 return -1;
281 }
282
Ye Li7a71c612021-08-07 16:00:39 +0800283 phdr = search_img_header(p, pg);
Peng Fan36986792019-09-16 03:09:31 +0000284 p += pg;
285
Ye Li7a71c612021-08-07 16:00:39 +0800286 if (phdr)
Peng Fan36986792019-09-16 03:09:31 +0000287 break;
288 }
289
Ye Li7a71c612021-08-07 16:00:39 +0800290 if (!phdr) {
291 puts("Can't found uboot image in 640K range\n");
Peng Fan36986792019-09-16 03:09:31 +0000292 return -1;
293 }
294
Ye Li7a71c612021-08-07 16:00:39 +0800295 if (p - phdr < img_header_size()) {
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200296 ret = rom_api_download_image(p, 0, pg);
Peng Fan36986792019-09-16 03:09:31 +0000297
298 if (ret != ROM_API_OKAY) {
Marcel Ziswiler0f38fef2023-10-26 09:32:19 +0200299 puts("Stream(USB) download failure\n");
Peng Fan36986792019-09-16 03:09:31 +0000300 return -1;
301 }
302
303 p += pg;
304 }
305
Ye Li7a71c612021-08-07 16:00:39 +0800306 imagesize = img_info_size(phdr);
Marcel Ziswilerff110e32022-07-20 09:27:55 +0200307 printf("Find img info 0x%p, size %d\n", phdr, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000308
Ye Li7a71c612021-08-07 16:00:39 +0800309 if (p - phdr < imagesize) {
310 imagesize -= p - phdr;
Peng Fan36986792019-09-16 03:09:31 +0000311 /*need pagesize hear after ROM fix USB problme*/
312 imagesize += pg - 1;
313 imagesize /= pg;
314 imagesize *= pg;
315
316 printf("Need continue download %d\n", imagesize);
317
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200318 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000319
320 p += imagesize;
321
322 if (ret != ROM_API_OKAY) {
323 printf("Failure download %d\n", imagesize);
324 return -1;
325 }
326 }
327
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200328 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
329 struct stream_state ss;
330
331 ss.base = phdr;
332 ss.end = p;
333 ss.pagesize = pagesize;
334
335 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500336 spl_set_bl_len(&load, 1);
Rasmus Villemoes51600f12023-09-19 15:49:31 +0200337 load.read = spl_romapi_read_stream;
338 load.priv = &ss;
339
340 return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
341 }
342
Ye Li7a71c612021-08-07 16:00:39 +0800343 total = img_total_size(phdr);
Peng Fan36986792019-09-16 03:09:31 +0000344 total += 3;
345 total &= ~0x3;
346
Ye Li7a71c612021-08-07 16:00:39 +0800347 imagesize = total - (p - phdr);
Peng Fan36986792019-09-16 03:09:31 +0000348
349 imagesize += pagesize - 1;
350 imagesize /= pagesize;
351 imagesize *= pagesize;
352
Ye Li7a71c612021-08-07 16:00:39 +0800353 printf("Download %d, Total size %d\n", imagesize, total);
Peng Fan36986792019-09-16 03:09:31 +0000354
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200355 ret = rom_api_download_image(p, 0, imagesize);
Peng Fan36986792019-09-16 03:09:31 +0000356 if (ret != ROM_API_OKAY)
357 printf("ROM download failure %d\n", imagesize);
358
359 memset(&load, 0, sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -0500360 spl_set_bl_len(&load, 1);
Peng Fan36986792019-09-16 03:09:31 +0000361 load.read = spl_ram_load_read;
362
Rasmus Villemoes6eb4f782023-09-19 15:49:32 +0200363 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
Ye Li7a71c612021-08-07 16:00:39 +0800364 return spl_load_imx_container(spl_image, &load, (ulong)phdr);
365
366 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000367}
368
369int board_return_to_bootrom(struct spl_image_info *spl_image,
370 struct spl_boot_device *bootdev)
371{
Peng Fan36986792019-09-16 03:09:31 +0000372 int ret;
Ye Li504514b2023-02-03 18:21:47 +0800373 u32 boot, bstage;
Peng Fan36986792019-09-16 03:09:31 +0000374
Rasmus Villemoes9bfb1ba2022-06-20 10:53:20 +0200375 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Marek Vasut00639162023-07-02 03:03:51 +0200376 if (ret != ROM_API_OKAY)
377 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000378
Marek Vasut00639162023-07-02 03:03:51 +0200379 ret = rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
380 if (ret != ROM_API_OKAY)
381 goto err;
Peng Fan36986792019-09-16 03:09:31 +0000382
Ye Li504514b2023-02-03 18:21:47 +0800383 printf("Boot Stage: ");
384
385 switch (bstage) {
386 case BT_STAGE_PRIMARY:
387 printf("Primary boot\n");
388 break;
389 case BT_STAGE_SECONDARY:
390 printf("Secondary boot\n");
391 break;
392 case BT_STAGE_RECOVERY:
393 printf("Recovery boot\n");
394 break;
395 case BT_STAGE_USB:
396 printf("USB boot\n");
397 break;
398 default:
Peng Fanb7052db2023-04-28 12:08:08 +0800399 printf("Unknown (0x%x)\n", bstage);
Ye Li504514b2023-02-03 18:21:47 +0800400 }
401
Peng Fan36986792019-09-16 03:09:31 +0000402 if (is_boot_from_stream_device(boot))
403 return spl_romapi_load_image_stream(spl_image, bootdev);
404
405 return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
Marek Vasut00639162023-07-02 03:03:51 +0200406err:
407 puts("ROMAPI: failure at query_boot_info\n");
408 return -1;
Peng Fan36986792019-09-16 03:09:31 +0000409}