1 // 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>
9 #include <log.h>
10 #include <asm/global_data.h>
11 #include <linux/libfdt.h>
12 #include <spl.h>
13 #include <asm/mach-imx/image.h>
14 #include <asm/arch/sys_proto.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 /* Caller need ensure the offset and size to align with page size */
spl_romapi_raw_seekable_read(u32 offset,u32 size,void * buf)19 ulong spl_romapi_raw_seekable_read(u32 offset, u32 size, void *buf)
20 {
21 	int ret;
22 
23 	debug("%s 0x%x, size 0x%x\n", __func__, offset, size);
24 
25 	ret = rom_api_download_image(buf, offset, size);
26 
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 
spl_romapi_get_uboot_base(u32 image_offset,u32 rom_bt_dev)35 ulong __weak spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
36 {
37 	return image_offset +
38 		(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000);
39 }
40 
is_boot_from_stream_device(u32 boot)41 static 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 
spl_romapi_read_seekable(struct spl_load_info * load,ulong sector,ulong count,void * buf)55 static 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;
60 	ulong byte = count * pagesize;
61 	u32 offset;
62 
63 	offset = sector * pagesize;
64 
65 	return spl_romapi_raw_seekable_read(offset, byte, buf) / pagesize;
66 }
67 
spl_romapi_load_image_seekable(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,u32 rom_bt_dev)68 static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
69 					  struct spl_boot_device *bootdev,
70 					  u32 rom_bt_dev)
71 {
72 	int ret;
73 	u32 offset;
74 	u32 pagesize, size;
75 	struct legacy_img_hdr *header;
76 	u32 image_offset;
77 
78 	ret = rom_api_query_boot_infor(QUERY_IVT_OFF, &offset);
79 	ret |= rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
80 	ret |= rom_api_query_boot_infor(QUERY_IMG_OFF, &image_offset);
81 
82 	if (ret != ROM_API_OKAY) {
83 		puts("ROMAPI: Failure query boot infor pagesize/offset\n");
84 		return -1;
85 	}
86 
87 	header = (struct legacy_img_hdr *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
88 
89 	printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
90 	       image_offset, pagesize, offset);
91 
92 	offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev);
93 
94 	size = ALIGN(sizeof(struct legacy_img_hdr), pagesize);
95 	ret = rom_api_download_image((u8 *)header, offset, size);
96 
97 	if (ret != ROM_API_OKAY) {
98 		printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
99 		       offset, size);
100 		return -1;
101 	}
102 
103 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
104 		struct spl_load_info load;
105 
106 		memset(&load, 0, sizeof(load));
107 		load.bl_len = pagesize;
108 		load.read = spl_romapi_read_seekable;
109 		load.priv = &pagesize;
110 		return spl_load_simple_fit(spl_image, &load, offset / pagesize, header);
111 	} else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
112 		struct spl_load_info load;
113 
114 		memset(&load, 0, sizeof(load));
115 		load.bl_len = pagesize;
116 		load.read = spl_romapi_read_seekable;
117 		load.priv = &pagesize;
118 
119 		ret = spl_load_imx_container(spl_image, &load, offset / pagesize);
120 	} else {
121 		/* TODO */
122 		puts("Can't support legacy image\n");
123 		return -1;
124 	}
125 
126 	return 0;
127 }
128 
spl_ram_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)129 static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
130 			       ulong count, void *buf)
131 {
132 	memcpy(buf, (void *)(sector), count);
133 
134 	if (load->priv) {
135 		ulong *p = (ulong *)load->priv;
136 		ulong total = sector + count;
137 
138 		if (total > *p)
139 			*p = total;
140 	}
141 
142 	return count;
143 }
144 
get_fit_image_size(void * fit)145 static ulong get_fit_image_size(void *fit)
146 {
147 	struct spl_image_info spl_image;
148 	struct spl_load_info spl_load_info;
149 	ulong last = (ulong)fit;
150 
151 	memset(&spl_load_info, 0, sizeof(spl_load_info));
152 	spl_load_info.bl_len = 1;
153 	spl_load_info.read = spl_ram_load_read;
154 	spl_load_info.priv = &last;
155 
156 	spl_load_simple_fit(&spl_image, &spl_load_info,
157 			    (uintptr_t)fit, fit);
158 
159 	return last - (ulong)fit;
160 }
161 
search_fit_header(u8 * p,int size)162 static u8 *search_fit_header(u8 *p, int size)
163 {
164 	int i;
165 
166 	for (i = 0; i < size; i += 4)
167 		if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
168 			return p + i;
169 
170 	return NULL;
171 }
172 
search_container_header(u8 * p,int size)173 static u8 *search_container_header(u8 *p, int size)
174 {
175 	int i = 0;
176 	u8 *hdr;
177 
178 	for (i = 0; i < size; i += 4) {
179 		hdr = p + i;
180 		if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0))
181 			return p + i;
182 	}
183 
184 	return NULL;
185 }
186 
search_img_header(u8 * p,int size)187 static u8 *search_img_header(u8 *p, int size)
188 {
189 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
190 		return search_fit_header(p, size);
191 	else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
192 		return search_container_header(p, size);
193 
194 	return NULL;
195 }
196 
img_header_size(void)197 static u32 img_header_size(void)
198 {
199 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
200 		return sizeof(struct fdt_header);
201 	else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
202 		return sizeof(struct container_hdr);
203 
204 	return 0;
205 }
206 
img_info_size(void * img_hdr)207 static int img_info_size(void *img_hdr)
208 {
209 #ifdef CONFIG_SPL_LOAD_FIT
210 	return fit_get_size(img_hdr);
211 #elif defined CONFIG_SPL_LOAD_IMX_CONTAINER
212 	struct container_hdr *container = img_hdr;
213 
214 	return (container->length_lsb + (container->length_msb << 8));
215 #else
216 	return 0;
217 #endif
218 }
219 
img_total_size(void * img_hdr)220 static int img_total_size(void *img_hdr)
221 {
222 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
223 		return get_fit_image_size(img_hdr);
224 	} else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
225 		int total = get_container_size((ulong)img_hdr, NULL);
226 
227 		if (total < 0) {
228 			printf("invalid container image\n");
229 			return 0;
230 		}
231 
232 		return total;
233 	}
234 
235 	return 0;
236 }
237 
spl_romapi_load_image_stream(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)238 static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
239 					struct spl_boot_device *bootdev)
240 {
241 	struct spl_load_info load;
242 	u32 pagesize, pg;
243 	int ret;
244 	int i = 0;
245 	u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
246 	u8 *phdr = NULL;
247 	int imagesize;
248 	int total;
249 
250 	ret = rom_api_query_boot_infor(QUERY_PAGE_SZ, &pagesize);
251 
252 	if (ret != ROM_API_OKAY)
253 		puts("failure at query_boot_info\n");
254 
255 	pg = pagesize;
256 	if (pg < 1024)
257 		pg = 1024;
258 
259 	for (i = 0; i < 640; i++) {
260 		ret = rom_api_download_image(p, 0, pg);
261 
262 		if (ret != ROM_API_OKAY) {
263 			puts("Steam(USB) download failure\n");
264 			return -1;
265 		}
266 
267 		phdr = search_img_header(p, pg);
268 		p += pg;
269 
270 		if (phdr)
271 			break;
272 	}
273 
274 	if (!phdr) {
275 		puts("Can't found uboot image in 640K range\n");
276 		return -1;
277 	}
278 
279 	if (p - phdr < img_header_size()) {
280 		ret = rom_api_download_image(p, 0, pg);
281 
282 		if (ret != ROM_API_OKAY) {
283 			puts("Steam(USB) download failure\n");
284 			return -1;
285 		}
286 
287 		p += pg;
288 	}
289 
290 	imagesize = img_info_size(phdr);
291 	printf("Find img info 0x%p, size %d\n", phdr, imagesize);
292 
293 	if (p - phdr < imagesize) {
294 		imagesize -= p - phdr;
295 		/*need pagesize hear after ROM fix USB problme*/
296 		imagesize += pg - 1;
297 		imagesize /= pg;
298 		imagesize *= pg;
299 
300 		printf("Need continue download %d\n", imagesize);
301 
302 		ret = rom_api_download_image(p, 0, imagesize);
303 
304 		p += imagesize;
305 
306 		if (ret != ROM_API_OKAY) {
307 			printf("Failure download %d\n", imagesize);
308 			return -1;
309 		}
310 	}
311 
312 	total = img_total_size(phdr);
313 	total += 3;
314 	total &= ~0x3;
315 
316 	imagesize = total - (p - phdr);
317 
318 	imagesize += pagesize - 1;
319 	imagesize /= pagesize;
320 	imagesize *= pagesize;
321 
322 	printf("Download %d, Total size %d\n", imagesize, total);
323 
324 	ret = rom_api_download_image(p, 0, imagesize);
325 	if (ret != ROM_API_OKAY)
326 		printf("ROM download failure %d\n", imagesize);
327 
328 	memset(&load, 0, sizeof(load));
329 	load.bl_len = 1;
330 	load.read = spl_ram_load_read;
331 
332 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT))
333 		return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
334 	else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER))
335 		return spl_load_imx_container(spl_image, &load, (ulong)phdr);
336 
337 	return -1;
338 }
339 
board_return_to_bootrom(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)340 int board_return_to_bootrom(struct spl_image_info *spl_image,
341 			    struct spl_boot_device *bootdev)
342 {
343 	int ret;
344 	u32 boot, bstage;
345 
346 	ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
347 	ret |= rom_api_query_boot_infor(QUERY_BT_STAGE, &bstage);
348 
349 	if (ret != ROM_API_OKAY) {
350 		puts("ROMAPI: failure at query_boot_info\n");
351 		return -1;
352 	}
353 
354 	printf("Boot Stage: ");
355 
356 	switch (bstage) {
357 	case BT_STAGE_PRIMARY:
358 		printf("Primary boot\n");
359 		break;
360 	case BT_STAGE_SECONDARY:
361 		printf("Secondary boot\n");
362 		break;
363 	case BT_STAGE_RECOVERY:
364 		printf("Recovery boot\n");
365 		break;
366 	case BT_STAGE_USB:
367 		printf("USB boot\n");
368 		break;
369 	default:
370 		printf("Unknown (0x%x)\n", bstage);
371 	}
372 
373 	if (is_boot_from_stream_device(boot))
374 		return spl_romapi_load_image_stream(spl_image, bootdev);
375 
376 	return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
377 }
378