xref: /rk3399_rockchip-uboot/common/image-android.c (revision cc3d6741441b8b18959be8e13eb74fd0ce4f4237)
1 /*
2  * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <image.h>
9 #include <android_image.h>
10 #include <android_bootloader.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <errno.h>
14 #include <boot_rkimg.h>
15 #include <crypto.h>
16 #include <sysmem.h>
17 #include <u-boot/sha1.h>
18 #ifdef CONFIG_RKIMG_BOOTLOADER
19 #include <asm/arch/resource_img.h>
20 #endif
21 #ifdef CONFIG_RK_AVB_LIBAVB_USER
22 #include <android_avb/avb_slot_verify.h>
23 #include <android_avb/avb_ops_user.h>
24 #include <android_avb/rk_avb_ops_user.h>
25 #endif
26 #include <optee_include/OpteeClientInterface.h>
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
30 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR	0x10008000
31 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb"
32 #define ANDROID_Q_VER				10
33 
34 /* Defined by rockchip legacy mkboot tool(SDK version < 8.1) */
35 #define ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE	0x4000
36 
37 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
38 static u32 android_kernel_comp_type = IH_COMP_NONE;
39 
40 u32 android_image_major_version(void)
41 {
42 	/* MSB 7-bits */
43 	return gd->bd->bi_andr_version >> 25;
44 }
45 
46 u32 android_bcb_msg_sector_offset(void)
47 {
48 	/*
49 	 * Rockchip platforms defines BCB message at the 16KB offset of
50 	 * misc partition while the Google defines it at 0x0 offset.
51 	 *
52 	 * From Android-Q, the 0x0 offset is mandary on Google VTS, so that
53 	 * this is a compatibility according to android image 'os_version'.
54 	 */
55 #ifdef CONFIG_RKIMG_BOOTLOADER
56 	return (android_image_major_version() >= ANDROID_Q_VER) ? 0x0 : 0x20;
57 #else
58 	return 0x0;
59 #endif
60 }
61 
62 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
63 {
64 	/*
65 	 * All the Android tools that generate a boot.img use this
66 	 * address as the default.
67 	 *
68 	 * Even though it doesn't really make a lot of sense, and it
69 	 * might be valid on some platforms, we treat that address as
70 	 * the default value for this field, and try to execute the
71 	 * kernel in place in such a case.
72 	 *
73 	 * Otherwise, we will return the actual value set by the user.
74 	 */
75 	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
76 		return (ulong)hdr + hdr->page_size;
77 
78 #ifdef CONFIG_ARCH_ROCKCHIP
79 	/*
80 	 * If kernel is compressed, kernel_addr is set as decompressed address
81 	 * after compressed being loaded to ram, so let's use it.
82 	 */
83 	if (android_kernel_comp_type != IH_COMP_NONE &&
84 	    android_kernel_comp_type != IH_COMP_ZIMAGE)
85 		return hdr->kernel_addr;
86 
87 	/*
88 	 * Compatble with rockchip legacy packing with kernel/ramdisk/second
89 	 * address base from 0x60000000(SDK versiont < 8.1), these are invalid
90 	 * address, so we calc it by real size.
91 	 */
92 	return (ulong)hdr + hdr->page_size;
93 #else
94 	return hdr->kernel_addr;
95 #endif
96 
97 }
98 
99 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp)
100 {
101 	android_kernel_comp_type = comp;
102 }
103 
104 u32 android_image_get_comp(const struct andr_img_hdr *hdr)
105 {
106 	return android_kernel_comp_type;
107 }
108 
109 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr)
110 {
111 	ulong kaddr = android_image_get_kernel_addr(hdr);
112 	return bootm_parse_comp((const unsigned char *)kaddr);
113 }
114 
115 /**
116  * android_image_get_kernel() - processes kernel part of Android boot images
117  * @hdr:	Pointer to image header, which is at the start
118  *			of the image.
119  * @verify:	Checksum verification flag. Currently unimplemented.
120  * @os_data:	Pointer to a ulong variable, will hold os data start
121  *			address.
122  * @os_len:	Pointer to a ulong variable, will hold os data length.
123  *
124  * This function returns the os image's start address and length. Also,
125  * it appends the kernel command line to the bootargs env variable.
126  *
127  * Return: Zero, os start address and length on success,
128  *		otherwise on failure.
129  */
130 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
131 			     ulong *os_data, ulong *os_len)
132 {
133 	u32 kernel_addr = android_image_get_kernel_addr(hdr);
134 
135 	/*
136 	 * Not all Android tools use the id field for signing the image with
137 	 * sha1 (or anything) so we don't check it. It is not obvious that the
138 	 * string is null terminated so we take care of this.
139 	 */
140 	strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
141 	andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
142 	if (strlen(andr_tmp_str))
143 		printf("Android's image name: %s\n", andr_tmp_str);
144 
145 	printf("Kernel load addr 0x%08x size %u KiB\n",
146 	       kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
147 
148 	int len = 0;
149 	if (*hdr->cmdline) {
150 		debug("Kernel command line: %s\n", hdr->cmdline);
151 		len += strlen(hdr->cmdline);
152 	}
153 
154 	char *bootargs = env_get("bootargs");
155 	if (bootargs)
156 		len += strlen(bootargs);
157 
158 	char *newbootargs = malloc(len + 2);
159 	if (!newbootargs) {
160 		puts("Error: malloc in android_image_get_kernel failed!\n");
161 		return -ENOMEM;
162 	}
163 	*newbootargs = '\0';
164 
165 	if (bootargs) {
166 		strcpy(newbootargs, bootargs);
167 		strcat(newbootargs, " ");
168 	}
169 	if (*hdr->cmdline)
170 		strcat(newbootargs, hdr->cmdline);
171 
172 	env_set("bootargs", newbootargs);
173 
174 	if (os_data) {
175 		*os_data = (ulong)hdr;
176 		*os_data += hdr->page_size;
177 	}
178 	if (os_len)
179 		*os_len = hdr->kernel_size;
180 	return 0;
181 }
182 
183 int android_image_check_header(const struct andr_img_hdr *hdr)
184 {
185 	return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
186 }
187 
188 ulong android_image_get_end(const struct andr_img_hdr *hdr)
189 {
190 	ulong end;
191 	/*
192 	 * The header takes a full page, the remaining components are aligned
193 	 * on page boundary
194 	 */
195 	end = (ulong)hdr;
196 	end += hdr->page_size;
197 	end += ALIGN(hdr->kernel_size, hdr->page_size);
198 	end += ALIGN(hdr->ramdisk_size, hdr->page_size);
199 	end += ALIGN(hdr->second_size, hdr->page_size);
200 
201 	if (hdr->header_version >= 1)
202 		end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
203 
204 	return end;
205 }
206 
207 u32 android_image_get_ksize(const struct andr_img_hdr *hdr)
208 {
209 	return hdr->kernel_size;
210 }
211 
212 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address)
213 {
214 	hdr->kernel_addr = load_address;
215 }
216 
217 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
218 {
219 	return android_image_get_kernel_addr(hdr);
220 }
221 
222 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
223 			      ulong *rd_data, ulong *rd_len)
224 {
225 	if (!hdr->ramdisk_size) {
226 		*rd_data = *rd_len = 0;
227 		return -1;
228 	}
229 
230 	/* We have load ramdisk at "ramdisk_addr_r" */
231 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
232 	ulong ramdisk_addr_r;
233 
234 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
235 	if (!ramdisk_addr_r) {
236 		printf("No Found Ramdisk Load Address.\n");
237 		return -1;
238 	}
239 
240 	*rd_data = ramdisk_addr_r;
241 #else
242 	*rd_data = (unsigned long)hdr;
243 	*rd_data += hdr->page_size;
244 	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
245 #endif
246 
247 	*rd_len = hdr->ramdisk_size;
248 
249 	printf("RAM disk load addr 0x%08lx size %u KiB\n",
250 	       *rd_data, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
251 
252 	return 0;
253 }
254 
255 int android_image_get_fdt(const struct andr_img_hdr *hdr,
256 			      ulong *rd_data)
257 {
258 	if (!hdr->second_size) {
259 		*rd_data = 0;
260 		return -1;
261 	}
262 
263 	/* We have load fdt at "fdt_addr_r" */
264 #if defined(CONFIG_USING_KERNEL_DTB) || \
265     defined(CONFIG_ANDROID_BOOT_IMAGE_SEPARATE)
266 	ulong fdt_addr_r;
267 
268 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
269 	if (!fdt_addr_r) {
270 		printf("No Found FDT Load Address.\n");
271 		return -1;
272 	}
273 
274 	*rd_data = fdt_addr_r;
275 #else
276 	*rd_data = (unsigned long)hdr;
277 	*rd_data += hdr->page_size;
278 	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
279 	*rd_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
280 #endif
281 
282 	debug("FDT load addr 0x%08x size %u KiB\n",
283 	      hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
284 
285 	return 0;
286 }
287 
288 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
289 static void print_hash(const char *label, u8 *hash, int len)
290 {
291 	int i;
292 
293 	printf("%s:\n    0x", label ? : "Hash");
294 	for (i = 0; i < len; i++)
295 		printf("%02x", hash[i]);
296 	printf("\n");
297 }
298 
299 /*
300  * This is only for Non-AVB image, because AVB image is verified by AVB bootflow.
301  * The kernel/ramdisk/second address should be the real address in hdr before
302  * calling this function.
303  *
304  * mkbootimg tool always use SHA1 for images.
305  */
306 static int android_image_hash_verify(struct andr_img_hdr *hdr)
307 {
308 	u8 hash[20];
309 
310 #ifdef DEBUG
311 	android_print_contents(hdr);
312 #endif
313 
314 	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) {
315 		printf("No real image address in android hdr\n");
316 		return -EINVAL;
317 	}
318 
319 #ifdef CONFIG_DM_CRYPTO
320 	struct udevice *dev;
321 	sha_context ctx;
322 
323 	dev = crypto_get_device(CRYPTO_SHA1);
324 	if (!dev) {
325 		printf("Can't find crypto device for SHA1 capability\n");
326 		return -ENODEV;
327 	}
328 
329 	ctx.algo = CRYPTO_SHA1;
330 	ctx.length = hdr->kernel_size + sizeof(hdr->kernel_size) +
331 		     hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
332 		     hdr->second_size + sizeof(hdr->second_size);
333 #ifdef CONFIG_HASH_ROCKCHIP_LEGACY
334 	ctx.length += sizeof(hdr->tags_addr) + sizeof(hdr->page_size) +
335 		      sizeof(hdr->unused) + sizeof(hdr->name) +
336 		      sizeof(hdr->cmdline);
337 #endif
338 
339 	crypto_sha_init(dev, &ctx);
340 
341 	crypto_sha_update(dev, (u32 *)(ulong)hdr->kernel_addr,
342 			  hdr->kernel_size);
343 	crypto_sha_update(dev, (u32 *)&hdr->kernel_size,
344 			  sizeof(hdr->kernel_size));
345 	crypto_sha_update(dev, (u32 *)(ulong)hdr->ramdisk_addr,
346 			  hdr->ramdisk_size);
347 	crypto_sha_update(dev, (u32 *)&hdr->ramdisk_size,
348 			  sizeof(hdr->ramdisk_size));
349 	crypto_sha_update(dev, (u32 *)(ulong)hdr->second_addr,
350 			  hdr->second_size);
351 	crypto_sha_update(dev, (u32 *)&hdr->second_size,
352 			  sizeof(hdr->second_size));
353 #ifdef CONFIG_HASH_ROCKCHIP_LEGACY
354 	crypto_sha_update(dev, (u32 *)&hdr->tags_addr, sizeof(hdr->tags_addr));
355 	crypto_sha_update(dev, (u32 *)&hdr->page_size, sizeof(hdr->page_size));
356 	crypto_sha_update(dev, (u32 *)&hdr->header_version,
357 			  sizeof(hdr->header_version));
358 	crypto_sha_update(dev, (u32 *)&hdr->os_version, sizeof(hdr->os_version));
359 	crypto_sha_update(dev, (u32 *)&hdr->name, sizeof(hdr->name));
360 	crypto_sha_update(dev, (u32 *)&hdr->cmdline, sizeof(hdr->cmdline));
361 #endif
362 
363 	crypto_sha_final(dev, &ctx, hash);
364 
365 #elif CONFIG_SHA1
366 	sha1_context ctx;
367 
368 	sha1_starts(&ctx);
369 	sha1_update(&ctx, (u8 *)(ulong)hdr->kernel_addr, hdr->kernel_size);
370 	sha1_update(&ctx, (u8 *)&hdr->kernel_size, sizeof(hdr->kernel_size));
371 	sha1_update(&ctx, (u8 *)(ulong)hdr->ramdisk_addr, hdr->ramdisk_size);
372 	sha1_update(&ctx, (u8 *)&hdr->ramdisk_size, sizeof(hdr->ramdisk_size));
373 	sha1_update(&ctx, (u8 *)(ulong)hdr->second_addr, hdr->second_size);
374 	sha1_update(&ctx, (u8 *)&hdr->second_size, sizeof(hdr->second_size));
375 #ifdef CONFIG_HASH_ROCKCHIP_LEGACY
376 	sha1_update(&ctx, (u8 *)&hdr->tags_addr, sizeof(hdr->tags_addr));
377 	sha1_update(&ctx, (u8 *)&hdr->page_size, sizeof(hdr->page_size));
378 	sha1_update(&ctx, (u8 *)&hdr->header_version,
379 		    sizeof(hdr->header_version));
380 	sha1_update(&ctx, (u8 *)&hdr->os_version, sizeof(hdr->os_version));
381 	sha1_update(&ctx, (u8 *)&hdr->name, sizeof(hdr->name));
382 	sha1_update(&ctx, (u8 *)&hdr->cmdline, sizeof(hdr->cmdline));
383 #endif
384 
385 	sha1_finish(&ctx, hash);
386 #endif	/* CONFIG_SHA1 */
387 
388 	if (memcmp(hash, hdr->id, 20)) {
389 		print_hash("SHA1 from image header", (u8 *)hdr->id, 20);
390 		print_hash("SHA1 real", (u8 *)hash, 20);
391 		return -EBADFD;
392 	}
393 
394 	return 0;
395 }
396 #endif
397 
398 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
399 int android_image_load_separate(struct andr_img_hdr *hdr,
400 				const disk_partition_t *part,
401 				void *load_address, void *ram_src)
402 {
403 	struct blk_desc *dev_desc = rockchip_get_bootdev();
404 	ulong ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
405 	ulong kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0);
406 	char *fdt_high = env_get("fdt_high");
407 	char *ramdisk_high = env_get("initrd_high");
408 	ulong blk_start, blk_cnt, size;
409 	ulong start, second_addr_r = 0;
410 	int ret, blk_read = 0;
411 
412 	if (android_image_check_header(hdr)) {
413 		printf("Bad android image header\n");
414 		return -EINVAL;
415 	}
416 
417 	if (hdr->kernel_size) {
418 		size = hdr->kernel_size + hdr->page_size;
419 		blk_cnt = DIV_ROUND_UP(size, dev_desc->blksz);
420 		if (!sysmem_alloc_base(MEMBLK_ID_KERNEL,
421 				       (phys_addr_t)load_address,
422 				       blk_cnt * dev_desc->blksz))
423 			return -ENXIO;
424 
425 		if (ram_src) {
426 			start = (ulong)ram_src;
427 			memcpy((char *)load_address, (char *)start, size);
428 		} else {
429 			blk_start = part->start;
430 			ret = blk_dread(dev_desc, blk_start,
431 					blk_cnt, load_address);
432 			if (ret != blk_cnt) {
433 				printf("%s: read kernel failed, ret=%d\n",
434 				      __func__, ret);
435 				return -1;
436 			}
437 			blk_read += ret;
438 		}
439 	}
440 
441 	if (hdr->ramdisk_size) {
442 		size = hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size);
443 		blk_cnt = DIV_ROUND_UP(hdr->ramdisk_size, dev_desc->blksz);
444 		if (!sysmem_alloc_base(MEMBLK_ID_RAMDISK,
445 				       ramdisk_addr_r,
446 				       blk_cnt * dev_desc->blksz))
447 			return -ENXIO;
448 		if (ram_src) {
449 			start = (unsigned long)ram_src;
450 			start += hdr->page_size;
451 			start += ALIGN(hdr->kernel_size, hdr->page_size);
452 			memcpy((char *)ramdisk_addr_r,
453 			       (char *)start, hdr->ramdisk_size);
454 		} else {
455 			blk_start = part->start +
456 				DIV_ROUND_UP(size, dev_desc->blksz);
457 			ret = blk_dread(dev_desc, blk_start,
458 					blk_cnt, (void *)ramdisk_addr_r);
459 			if (ret != blk_cnt) {
460 				printf("%s: read ramdisk failed, ret=%d\n",
461 				      __func__, ret);
462 				return -1;
463 			}
464 			blk_read += ret;
465 		}
466 	}
467 
468 	/*
469 	 * Load dtb file by rockchip_read_dtb_file() which support pack
470 	 * dtb in second position or resource file.
471 	 */
472 #ifdef CONFIG_RKIMG_BOOTLOADER
473 	ulong fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
474 
475 	if (hdr->second_size && (gd->fdt_blob != (void *)fdt_addr_r)) {
476 		ulong fdt_size;
477 
478 		fdt_size = rockchip_read_dtb_file((void *)fdt_addr_r);
479 		if (fdt_size < 0) {
480 			printf("%s: read fdt failed\n", __func__);
481 			return ret;
482 		}
483 
484 		blk_read += DIV_ROUND_UP(fdt_size, dev_desc->blksz);
485 	}
486 #endif
487 
488 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
489 	if (hdr->second_size) {
490 		ulong blk_start, blk_cnt;
491 
492 		/* Just for image data hash calculation */
493 		second_addr_r = (ulong)malloc(hdr->second_size);
494 		if (!second_addr_r)
495 			return -ENOMEM;
496 
497 		size = hdr->page_size +
498 		       ALIGN(hdr->kernel_size, hdr->page_size) +
499 		       ALIGN(hdr->ramdisk_size, hdr->page_size);
500 		blk_cnt = DIV_ROUND_UP(hdr->second_size, dev_desc->blksz);
501 
502 		if (ram_src) {
503 			start = (unsigned long)ram_src;
504 			start += hdr->page_size;
505 			start += ALIGN(hdr->kernel_size, hdr->page_size);
506 			start += ALIGN(hdr->ramdisk_size, hdr->page_size);
507 			memcpy((char *)second_addr_r,
508 			       (char *)start, hdr->second_size);
509 		} else {
510 			blk_start = part->start +
511 					DIV_ROUND_UP(size, dev_desc->blksz);
512 			ret = blk_dread(dev_desc, blk_start, blk_cnt,
513 					(void *)second_addr_r);
514 			if (ret != blk_cnt) {
515 				printf("%s: read second pos failed, ret=%d\n",
516 				       __func__, ret);
517 				return -1;
518 			}
519 
520 			blk_read += blk_cnt;
521 		}
522 	}
523 #endif
524 
525 	/* Update hdr with real image address */
526 	hdr->kernel_addr = kernel_addr_r;
527 	hdr->second_addr = second_addr_r;
528 	hdr->ramdisk_addr = ramdisk_addr_r;
529 
530 	/*
531 	 * Since images are loaded separate, fdt/ramdisk relocation
532 	 * can be disabled, it saves boot time.
533 	 */
534 	if (blk_read > 0 || ram_src) {
535 		if (!fdt_high) {
536 			env_set_hex("fdt_high", -1UL);
537 			printf("Fdt ");
538 		}
539 		if (!ramdisk_high) {
540 			env_set_hex("initrd_high", -1UL);
541 			printf("Ramdisk ");
542 		}
543 		if (!fdt_high || !ramdisk_high)
544 			printf("skip relocation\n");
545 	}
546 
547 	return blk_read;
548 }
549 
550 int android_image_memcpy_separate(struct andr_img_hdr *hdr, void *load_address)
551 {
552 	return android_image_load_separate(hdr, NULL, load_address, hdr);
553 }
554 #endif /* CONFIG_ANDROID_BOOT_IMAGE_SEPARATE */
555 
556 long android_image_load(struct blk_desc *dev_desc,
557 			const disk_partition_t *part_info,
558 			unsigned long load_address,
559 			unsigned long max_size) {
560 	struct andr_img_hdr *hdr;
561 	u32 blksz = dev_desc->blksz;
562 	u32 pszcnt, hdrcnt, kercnt;
563 	void *buf;
564 	long blk_cnt = 0;
565 	long blk_read = 0;
566 	u32 comp;
567 	u32 kload_addr;
568 
569 	if (max_size < part_info->blksz)
570 		return -1;
571 
572 	/*
573 	 * read Android image header and leave enough space for page_size align
574 	 * and kernel image header(1 block maybe enough).
575 	 *
576 	 * ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE is defined by rockchip legacy
577 	 * mkboot tool(SDK version < 8.1) and larger than Google defined.
578 	 *
579 	 * To compatible this, we malloc enough buffer but only read android
580 	 * header and kernel image(1 block) from storage(ignore page size).
581 	 */
582 	kercnt = 1;
583 	hdrcnt = DIV_ROUND_UP(sizeof(*hdr), blksz);
584 	pszcnt = DIV_ROUND_UP(ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE, blksz);
585 
586 	hdr = memalign(ARCH_DMA_MINALIGN, (hdrcnt + pszcnt + kercnt) * blksz);
587 	if (!hdr) {
588 		printf("%s: no memory\n", __func__);
589 		return -1;
590 	}
591 
592 	if (blk_dread(dev_desc, part_info->start, hdrcnt, hdr) != hdrcnt)
593 		blk_read = -1;
594 
595 	if (!blk_read && android_image_check_header(hdr) != 0) {
596 		printf("** Invalid Android Image header **\n");
597 		blk_read = -1;
598 	}
599 
600 	/*
601 	 * Update and skip pszcnt(hdr is included) according to hdr->page_size,
602 	 * reading kernel image for compress validation.
603 	 */
604 	pszcnt = DIV_ROUND_UP(hdr->page_size, blksz);
605 
606 	if (blk_dread(dev_desc, part_info->start + pszcnt, kercnt,
607 		      (void *)((ulong)hdr + hdr->page_size)) != kercnt)
608 		blk_read = -1;
609 
610 	/* page_size for image header */
611 	load_address -= hdr->page_size;
612 
613 	/* We don't know the size of the Android image before reading the header
614 	 * so we don't limit the size of the mapped memory.
615 	 */
616 	buf = map_sysmem(load_address, 0 /* size */);
617 	if (!blk_read) {
618 		blk_cnt = (android_image_get_end(hdr) - (ulong)hdr +
619 			   part_info->blksz - 1) / part_info->blksz;
620 		comp = android_image_parse_kernel_comp(hdr);
621 		/*
622 		 * We should load compressed kernel Image to high memory at
623 		 * address "kernel_addr_c".
624 		 */
625 		if (comp != IH_COMP_NONE) {
626 			ulong kernel_addr_c;
627 
628 			env_set_ulong("os_comp", comp);
629 			kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
630 			if (kernel_addr_c) {
631 				load_address = kernel_addr_c - hdr->page_size;
632 				unmap_sysmem(buf);
633 				buf = map_sysmem(load_address, 0 /* size */);
634 			}
635 #ifdef CONFIG_ARM64
636 			else {
637 				printf("Warn: \"kernel_addr_c\" is not defined "
638 				       "for compressed kernel Image!\n");
639 				load_address += android_image_get_ksize(hdr) * 3;
640 				load_address = ALIGN(load_address, ARCH_DMA_MINALIGN);
641 				env_set_ulong("kernel_addr_c", load_address);
642 
643 				load_address -= hdr->page_size;
644 				unmap_sysmem(buf);
645 				buf = map_sysmem(load_address, 0 /* size */);
646 			}
647 #endif
648 		}
649 
650 		if (blk_cnt * part_info->blksz > max_size) {
651 			debug("Android Image too big (%lu bytes, max %lu)\n",
652 			      android_image_get_end(hdr) - (ulong)hdr,
653 			      max_size);
654 			blk_read = -1;
655 		} else {
656 			debug("Loading Android Image (%lu blocks) to 0x%lx... ",
657 			      blk_cnt, load_address);
658 
659 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
660 			blk_read =
661 			android_image_load_separate(hdr, part_info, buf, NULL);
662 #else
663 			if (!sysmem_alloc_base(MEMBLK_ID_ANDROID,
664 					       (phys_addr_t)buf,
665 						blk_cnt * part_info->blksz))
666 				return -ENXIO;
667 
668 			blk_read = blk_dread(dev_desc, part_info->start,
669 					     blk_cnt, buf);
670 #endif
671 		}
672 
673 		/* Verify image hash */
674 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
675 		if (android_image_hash_verify(hdr)) {
676 			printf("Image hash miss match!\n");
677 			return -EBADFD;
678 		}
679 
680 		printf("Image hash verify ok\n");
681 #endif
682 		/*
683 		 * zImage is not need to decompress
684 		 * kernel will handle decompress itself
685 		 */
686 		if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
687 			kload_addr = env_get_ulong("kernel_addr_r", 16, 0x02080000);
688 			android_image_set_kload(buf, kload_addr);
689 			android_image_set_comp(buf, comp);
690 		} else {
691 			android_image_set_comp(buf, IH_COMP_NONE);
692 		}
693 
694 	}
695 
696 	free(hdr);
697 	unmap_sysmem(buf);
698 
699 #ifndef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
700 	debug("%lu blocks read: %s\n",
701 	      blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR");
702 	if (blk_read != blk_cnt)
703 		return -1;
704 #else
705 	debug("%lu blocks read\n", blk_read);
706 	if (blk_read < 0)
707 		return blk_read;
708 #endif
709 
710 	return load_address;
711 }
712 
713 #if !defined(CONFIG_SPL_BUILD)
714 /**
715  * android_print_contents - prints out the contents of the Android format image
716  * @hdr: pointer to the Android format image header
717  *
718  * android_print_contents() formats a multi line Android image contents
719  * description.
720  * The routine prints out Android image properties
721  *
722  * returns:
723  *     no returned results
724  */
725 void android_print_contents(const struct andr_img_hdr *hdr)
726 {
727 	const char * const p = IMAGE_INDENT_STRING;
728 	/* os_version = ver << 11 | lvl */
729 	u32 os_ver = hdr->os_version >> 11;
730 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
731 	u32 header_version = hdr->header_version;
732 
733 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
734 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
735 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
736 	printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr);
737 	printf("%ssecond size:      %x\n", p, hdr->second_size);
738 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
739 	printf("%stags address:     %x\n", p, hdr->tags_addr);
740 	printf("%spage size:        %x\n", p, hdr->page_size);
741 	printf("%sheader_version:   %x\n", p, header_version);
742 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
743 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
744 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
745 	       p, hdr->os_version,
746 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
747 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
748 	printf("%sname:             %s\n", p, hdr->name);
749 	printf("%scmdline:          %s\n", p, hdr->cmdline);
750 
751 	if (header_version >= 1) {
752 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
753 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
754 		printf("%sheader size:           %x\n", p, hdr->header_size);
755 	}
756 
757 	if (header_version >= 2) {
758 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
759 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
760 	}
761 }
762 #endif
763