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