xref: /rk3399_rockchip-uboot/common/image-android.c (revision 3f7fb06b2474d0aaf082577806e3bd64586415d9)
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 	ulong ramdisk_addr_r;
226 
227 	if (!hdr->ramdisk_size) {
228 		*rd_data = *rd_len = 0;
229 		return -1;
230 	}
231 
232 	/* Have been loaded by android_image_load_separate() on ramdisk_addr_r */
233 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
234 	if (!ramdisk_addr_r) {
235 		printf("No Found Ramdisk Load Address.\n");
236 		return -1;
237 	}
238 
239 	*rd_data = ramdisk_addr_r;
240 	*rd_len = hdr->ramdisk_size;
241 
242 	printf("RAM disk load addr 0x%08lx size %u KiB\n",
243 	       *rd_data, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
244 
245 	return 0;
246 }
247 
248 int android_image_get_fdt(const struct andr_img_hdr *hdr,
249 			      ulong *rd_data)
250 {
251 	ulong fdt_addr_r;
252 
253 	if (!hdr->second_size) {
254 		*rd_data = 0;
255 		return -1;
256 	}
257 
258 	/* Have been loaded by android_image_load_separate() on fdt_addr_r */
259 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
260 	if (!fdt_addr_r) {
261 		printf("No Found FDT Load Address.\n");
262 		return -1;
263 	}
264 
265 	*rd_data = fdt_addr_r;
266 
267 	debug("FDT load addr 0x%08x size %u KiB\n",
268 	      hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
269 
270 	return 0;
271 }
272 
273 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
274 static void print_hash(const char *label, u8 *hash, int len)
275 {
276 	int i;
277 
278 	printf("%s:\n    0x", label ? : "Hash");
279 	for (i = 0; i < len; i++)
280 		printf("%02x", hash[i]);
281 	printf("\n");
282 }
283 
284 /*
285  * This is only for Non-AVB image, because AVB image is verified by AVB bootflow.
286  * The kernel/ramdisk/second address should be the real address in hdr before
287  * calling this function.
288  *
289  * mkbootimg tool always use SHA1 for images.
290  */
291 static int android_image_hash_verify(struct andr_img_hdr *hdr)
292 {
293 	u8 hash[20];
294 
295 #ifdef DEBUG
296 	android_print_contents(hdr);
297 #endif
298 
299 	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) {
300 		printf("No real image address in android hdr\n");
301 		return -EINVAL;
302 	}
303 
304 #ifdef CONFIG_DM_CRYPTO
305 	struct udevice *dev;
306 	sha_context ctx;
307 
308 	dev = crypto_get_device(CRYPTO_SHA1);
309 	if (!dev) {
310 		printf("Can't find crypto device for SHA1 capability\n");
311 		return -ENODEV;
312 	}
313 
314 	ctx.algo = CRYPTO_SHA1;
315 	ctx.length = hdr->kernel_size + sizeof(hdr->kernel_size) +
316 		     hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
317 		     hdr->second_size + sizeof(hdr->second_size);
318 #ifdef CONFIG_HASH_ROCKCHIP_LEGACY
319 	ctx.length += sizeof(hdr->tags_addr) + sizeof(hdr->page_size) +
320 		      sizeof(hdr->unused) + sizeof(hdr->name) +
321 		      sizeof(hdr->cmdline);
322 #endif
323 
324 	crypto_sha_init(dev, &ctx);
325 
326 	crypto_sha_update(dev, (u32 *)(ulong)hdr->kernel_addr,
327 			  hdr->kernel_size);
328 	crypto_sha_update(dev, (u32 *)&hdr->kernel_size,
329 			  sizeof(hdr->kernel_size));
330 	crypto_sha_update(dev, (u32 *)(ulong)hdr->ramdisk_addr,
331 			  hdr->ramdisk_size);
332 	crypto_sha_update(dev, (u32 *)&hdr->ramdisk_size,
333 			  sizeof(hdr->ramdisk_size));
334 	crypto_sha_update(dev, (u32 *)(ulong)hdr->second_addr,
335 			  hdr->second_size);
336 	crypto_sha_update(dev, (u32 *)&hdr->second_size,
337 			  sizeof(hdr->second_size));
338 #ifdef CONFIG_HASH_ROCKCHIP_LEGACY
339 	crypto_sha_update(dev, (u32 *)&hdr->tags_addr, sizeof(hdr->tags_addr));
340 	crypto_sha_update(dev, (u32 *)&hdr->page_size, sizeof(hdr->page_size));
341 	crypto_sha_update(dev, (u32 *)&hdr->header_version,
342 			  sizeof(hdr->header_version));
343 	crypto_sha_update(dev, (u32 *)&hdr->os_version, sizeof(hdr->os_version));
344 	crypto_sha_update(dev, (u32 *)&hdr->name, sizeof(hdr->name));
345 	crypto_sha_update(dev, (u32 *)&hdr->cmdline, sizeof(hdr->cmdline));
346 #endif
347 
348 	crypto_sha_final(dev, &ctx, hash);
349 
350 #elif CONFIG_SHA1
351 	sha1_context ctx;
352 
353 	sha1_starts(&ctx);
354 	sha1_update(&ctx, (u8 *)(ulong)hdr->kernel_addr, hdr->kernel_size);
355 	sha1_update(&ctx, (u8 *)&hdr->kernel_size, sizeof(hdr->kernel_size));
356 	sha1_update(&ctx, (u8 *)(ulong)hdr->ramdisk_addr, hdr->ramdisk_size);
357 	sha1_update(&ctx, (u8 *)&hdr->ramdisk_size, sizeof(hdr->ramdisk_size));
358 	sha1_update(&ctx, (u8 *)(ulong)hdr->second_addr, hdr->second_size);
359 	sha1_update(&ctx, (u8 *)&hdr->second_size, sizeof(hdr->second_size));
360 #ifdef CONFIG_HASH_ROCKCHIP_LEGACY
361 	sha1_update(&ctx, (u8 *)&hdr->tags_addr, sizeof(hdr->tags_addr));
362 	sha1_update(&ctx, (u8 *)&hdr->page_size, sizeof(hdr->page_size));
363 	sha1_update(&ctx, (u8 *)&hdr->header_version,
364 		    sizeof(hdr->header_version));
365 	sha1_update(&ctx, (u8 *)&hdr->os_version, sizeof(hdr->os_version));
366 	sha1_update(&ctx, (u8 *)&hdr->name, sizeof(hdr->name));
367 	sha1_update(&ctx, (u8 *)&hdr->cmdline, sizeof(hdr->cmdline));
368 #endif
369 
370 	sha1_finish(&ctx, hash);
371 #endif	/* CONFIG_SHA1 */
372 
373 	if (memcmp(hash, hdr->id, 20)) {
374 		print_hash("SHA1 from image header", (u8 *)hdr->id, 20);
375 		print_hash("SHA1 real", (u8 *)hash, 20);
376 		return -EBADFD;
377 	}
378 
379 	return 0;
380 }
381 #endif
382 
383 int android_image_load_separate(struct andr_img_hdr *hdr,
384 				const disk_partition_t *part,
385 				void *load_address, void *ram_base)
386 {
387 	struct blk_desc *dev_desc = rockchip_get_bootdev();
388 	ulong ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
389 	ulong kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0);
390 	char *fdt_high = env_get("fdt_high");
391 	char *ramdisk_high = env_get("initrd_high");
392 	ulong blk_start, blk_cnt, size;
393 	ulong start, second_addr_r = 0;
394 	int ret, blk_read = 0;
395 
396 	if (android_image_check_header(hdr)) {
397 		printf("Bad android image header\n");
398 		return -EINVAL;
399 	}
400 
401 	if (hdr->kernel_size) {
402 		size = hdr->kernel_size + hdr->page_size;
403 		blk_cnt = DIV_ROUND_UP(size, dev_desc->blksz);
404 		if (!sysmem_alloc_base(MEMBLK_ID_KERNEL,
405 				       (phys_addr_t)load_address,
406 				       blk_cnt * dev_desc->blksz))
407 			return -ENXIO;
408 
409 		if (ram_base) {
410 			start = (ulong)ram_base;
411 			memcpy((char *)load_address, (char *)start, size);
412 		} else {
413 			blk_start = part->start;
414 			ret = blk_dread(dev_desc, blk_start,
415 					blk_cnt, load_address);
416 			if (ret != blk_cnt) {
417 				printf("%s: read kernel failed, ret=%d\n",
418 				      __func__, ret);
419 				return -1;
420 			}
421 			blk_read += ret;
422 		}
423 	}
424 
425 	if (hdr->ramdisk_size) {
426 		size = hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size);
427 		blk_cnt = DIV_ROUND_UP(hdr->ramdisk_size, dev_desc->blksz);
428 		if (!sysmem_alloc_base(MEMBLK_ID_RAMDISK,
429 				       ramdisk_addr_r,
430 				       blk_cnt * dev_desc->blksz))
431 			return -ENXIO;
432 		if (ram_base) {
433 			start = (unsigned long)ram_base;
434 			start += hdr->page_size;
435 			start += ALIGN(hdr->kernel_size, hdr->page_size);
436 			memcpy((char *)ramdisk_addr_r,
437 			       (char *)start, hdr->ramdisk_size);
438 		} else {
439 			blk_start = part->start +
440 				DIV_ROUND_UP(size, dev_desc->blksz);
441 			ret = blk_dread(dev_desc, blk_start,
442 					blk_cnt, (void *)ramdisk_addr_r);
443 			if (ret != blk_cnt) {
444 				printf("%s: read ramdisk failed, ret=%d\n",
445 				      __func__, ret);
446 				return -1;
447 			}
448 			blk_read += ret;
449 		}
450 	}
451 
452 	/*
453 	 * Load dtb file by rockchip_read_dtb_file() which support pack
454 	 * dtb in second position or resource file.
455 	 */
456 #ifdef CONFIG_RKIMG_BOOTLOADER
457 	ulong fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
458 
459 	if (hdr->second_size && (gd->fdt_blob != (void *)fdt_addr_r)) {
460 		ulong fdt_size;
461 
462 		fdt_size = rockchip_read_dtb_file((void *)fdt_addr_r);
463 		if (fdt_size < 0) {
464 			printf("%s: read fdt failed\n", __func__);
465 			return ret;
466 		}
467 
468 		blk_read += DIV_ROUND_UP(fdt_size, dev_desc->blksz);
469 	}
470 #endif
471 
472 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
473 	if (hdr->second_size) {
474 		ulong blk_start, blk_cnt;
475 
476 		/* Just for image data hash calculation */
477 		second_addr_r = (ulong)malloc(hdr->second_size);
478 		if (!second_addr_r)
479 			return -ENOMEM;
480 
481 		size = hdr->page_size +
482 		       ALIGN(hdr->kernel_size, hdr->page_size) +
483 		       ALIGN(hdr->ramdisk_size, hdr->page_size);
484 		blk_cnt = DIV_ROUND_UP(hdr->second_size, dev_desc->blksz);
485 
486 		if (ram_base) {
487 			start = (unsigned long)ram_base;
488 			start += hdr->page_size;
489 			start += ALIGN(hdr->kernel_size, hdr->page_size);
490 			start += ALIGN(hdr->ramdisk_size, hdr->page_size);
491 			memcpy((char *)second_addr_r,
492 			       (char *)start, hdr->second_size);
493 		} else {
494 			blk_start = part->start +
495 					DIV_ROUND_UP(size, dev_desc->blksz);
496 			ret = blk_dread(dev_desc, blk_start, blk_cnt,
497 					(void *)second_addr_r);
498 			if (ret != blk_cnt) {
499 				printf("%s: read second pos failed, ret=%d\n",
500 				       __func__, ret);
501 				return -1;
502 			}
503 
504 			blk_read += blk_cnt;
505 		}
506 	}
507 #endif
508 
509 	/* Update hdr with real image address */
510 	hdr->kernel_addr = kernel_addr_r;
511 	hdr->second_addr = second_addr_r;
512 	hdr->ramdisk_addr = ramdisk_addr_r;
513 
514 	/*
515 	 * Since images are loaded separate, fdt/ramdisk relocation
516 	 * can be disabled, it saves boot time.
517 	 */
518 	if (blk_read > 0 || ram_base) {
519 		if (!fdt_high) {
520 			env_set_hex("fdt_high", -1UL);
521 			printf("Fdt ");
522 		}
523 		if (!ramdisk_high) {
524 			env_set_hex("initrd_high", -1UL);
525 			printf("Ramdisk ");
526 		}
527 		if (!fdt_high || !ramdisk_high)
528 			printf("skip relocation\n");
529 	}
530 
531 	return blk_read;
532 }
533 
534 int android_image_memcpy_separate(struct andr_img_hdr *hdr, void *load_address)
535 {
536 	return android_image_load_separate(hdr, NULL, load_address, hdr);
537 }
538 
539 long android_image_load(struct blk_desc *dev_desc,
540 			const disk_partition_t *part_info,
541 			unsigned long load_address,
542 			unsigned long max_size) {
543 	struct andr_img_hdr *hdr;
544 	u32 blksz = dev_desc->blksz;
545 	u32 pszcnt, hdrcnt, kercnt;
546 	void *buf;
547 	long blk_cnt = 0;
548 	long blk_read = 0;
549 	u32 comp;
550 	u32 kload_addr;
551 
552 	if (max_size < part_info->blksz)
553 		return -1;
554 
555 	/*
556 	 * read Android image header and leave enough space for page_size align
557 	 * and kernel image header(1 block maybe enough).
558 	 *
559 	 * ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE is defined by rockchip legacy
560 	 * mkboot tool(SDK version < 8.1) and larger than Google defined.
561 	 *
562 	 * To compatible this, we malloc enough buffer but only read android
563 	 * header and kernel image(1 block) from storage(ignore page size).
564 	 */
565 	kercnt = 1;
566 	hdrcnt = DIV_ROUND_UP(sizeof(*hdr), blksz);
567 	pszcnt = DIV_ROUND_UP(ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE, blksz);
568 
569 	hdr = memalign(ARCH_DMA_MINALIGN, (hdrcnt + pszcnt + kercnt) * blksz);
570 	if (!hdr) {
571 		printf("%s: no memory\n", __func__);
572 		return -1;
573 	}
574 
575 	if (blk_dread(dev_desc, part_info->start, hdrcnt, hdr) != hdrcnt)
576 		blk_read = -1;
577 
578 	if (!blk_read && android_image_check_header(hdr) != 0) {
579 		printf("** Invalid Android Image header **\n");
580 		blk_read = -1;
581 	}
582 
583 	/*
584 	 * Update and skip pszcnt(hdr is included) according to hdr->page_size,
585 	 * reading kernel image for compress validation.
586 	 */
587 	pszcnt = DIV_ROUND_UP(hdr->page_size, blksz);
588 
589 	if (blk_dread(dev_desc, part_info->start + pszcnt, kercnt,
590 		      (void *)((ulong)hdr + hdr->page_size)) != kercnt)
591 		blk_read = -1;
592 
593 	/* page_size for image header */
594 	load_address -= hdr->page_size;
595 
596 	/* We don't know the size of the Android image before reading the header
597 	 * so we don't limit the size of the mapped memory.
598 	 */
599 	buf = map_sysmem(load_address, 0 /* size */);
600 	if (!blk_read) {
601 		blk_cnt = (android_image_get_end(hdr) - (ulong)hdr +
602 			   part_info->blksz - 1) / part_info->blksz;
603 		comp = android_image_parse_kernel_comp(hdr);
604 		/*
605 		 * We should load compressed kernel Image to high memory at
606 		 * address "kernel_addr_c".
607 		 */
608 		if (comp != IH_COMP_NONE) {
609 			ulong kernel_addr_c;
610 
611 			env_set_ulong("os_comp", comp);
612 			kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
613 			if (kernel_addr_c) {
614 				load_address = kernel_addr_c - hdr->page_size;
615 				unmap_sysmem(buf);
616 				buf = map_sysmem(load_address, 0 /* size */);
617 			}
618 #ifdef CONFIG_ARM64
619 			else {
620 				printf("Warn: \"kernel_addr_c\" is not defined "
621 				       "for compressed kernel Image!\n");
622 				load_address += android_image_get_ksize(hdr) * 3;
623 				load_address = ALIGN(load_address, ARCH_DMA_MINALIGN);
624 				env_set_ulong("kernel_addr_c", load_address);
625 
626 				load_address -= hdr->page_size;
627 				unmap_sysmem(buf);
628 				buf = map_sysmem(load_address, 0 /* size */);
629 			}
630 #endif
631 		}
632 
633 		if (blk_cnt * part_info->blksz > max_size) {
634 			debug("Android Image too big (%lu bytes, max %lu)\n",
635 			      android_image_get_end(hdr) - (ulong)hdr,
636 			      max_size);
637 			blk_read = -1;
638 		} else {
639 			debug("Loading Android Image (%lu blocks) to 0x%lx... ",
640 			      blk_cnt, load_address);
641 			blk_read =
642 			android_image_load_separate(hdr, part_info, buf, NULL);
643 		}
644 
645 		/* Verify image hash */
646 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
647 		if (android_image_hash_verify(hdr)) {
648 			printf("Image hash miss match!\n");
649 			return -EBADFD;
650 		}
651 
652 		printf("Image hash verify ok\n");
653 #endif
654 		/*
655 		 * zImage is not need to decompress
656 		 * kernel will handle decompress itself
657 		 */
658 		if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
659 			kload_addr = env_get_ulong("kernel_addr_r", 16, 0x02080000);
660 			android_image_set_kload(buf, kload_addr);
661 			android_image_set_comp(buf, comp);
662 		} else {
663 			android_image_set_comp(buf, IH_COMP_NONE);
664 		}
665 
666 	}
667 
668 	free(hdr);
669 	unmap_sysmem(buf);
670 
671 	debug("%lu blocks read\n", blk_read);
672 	if (blk_read < 0)
673 		return -1;
674 
675 	return load_address;
676 }
677 
678 #if !defined(CONFIG_SPL_BUILD)
679 /**
680  * android_print_contents - prints out the contents of the Android format image
681  * @hdr: pointer to the Android format image header
682  *
683  * android_print_contents() formats a multi line Android image contents
684  * description.
685  * The routine prints out Android image properties
686  *
687  * returns:
688  *     no returned results
689  */
690 void android_print_contents(const struct andr_img_hdr *hdr)
691 {
692 	const char * const p = IMAGE_INDENT_STRING;
693 	/* os_version = ver << 11 | lvl */
694 	u32 os_ver = hdr->os_version >> 11;
695 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
696 	u32 header_version = hdr->header_version;
697 
698 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
699 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
700 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
701 	printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr);
702 	printf("%ssecond size:      %x\n", p, hdr->second_size);
703 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
704 	printf("%stags address:     %x\n", p, hdr->tags_addr);
705 	printf("%spage size:        %x\n", p, hdr->page_size);
706 	printf("%sheader_version:   %x\n", p, header_version);
707 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
708 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
709 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
710 	       p, hdr->os_version,
711 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
712 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
713 	printf("%sname:             %s\n", p, hdr->name);
714 	printf("%scmdline:          %s\n", p, hdr->cmdline);
715 
716 	if (header_version >= 1) {
717 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
718 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
719 		printf("%sheader size:           %x\n", p, hdr->header_size);
720 	}
721 
722 	if (header_version >= 2) {
723 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
724 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
725 	}
726 }
727 #endif
728