xref: /rk3399_rockchip-uboot/common/image-android.c (revision 827e2ae92e2103f82dab5b54228ad24e40db6263)
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 >= 2) {
202 		end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
203 		end += ALIGN(hdr->dtb_size, hdr->page_size);
204 	} else if (hdr->header_version >= 1) {
205 		end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
206 	}
207 
208 	return end;
209 }
210 
211 u32 android_image_get_ksize(const struct andr_img_hdr *hdr)
212 {
213 	return hdr->kernel_size;
214 }
215 
216 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address)
217 {
218 	hdr->kernel_addr = load_address;
219 }
220 
221 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
222 {
223 	return android_image_get_kernel_addr(hdr);
224 }
225 
226 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
227 			      ulong *rd_data, ulong *rd_len)
228 {
229 	ulong ramdisk_addr_r;
230 
231 	if (!hdr->ramdisk_size) {
232 		*rd_data = *rd_len = 0;
233 		return -1;
234 	}
235 
236 	/* Have been loaded by android_image_load_separate() on ramdisk_addr_r */
237 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
238 	if (!ramdisk_addr_r) {
239 		printf("No Found Ramdisk Load Address.\n");
240 		return -1;
241 	}
242 
243 	*rd_data = ramdisk_addr_r;
244 	*rd_len = hdr->ramdisk_size;
245 
246 	printf("RAM disk load addr 0x%08lx size %u KiB\n",
247 	       *rd_data, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
248 
249 	return 0;
250 }
251 
252 int android_image_get_fdt(const struct andr_img_hdr *hdr,
253 			      ulong *rd_data)
254 {
255 	ulong fdt_addr_r;
256 
257 	if (!hdr->second_size) {
258 		*rd_data = 0;
259 		return -1;
260 	}
261 
262 	/* Have been loaded by android_image_load_separate() on fdt_addr_r */
263 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
264 	if (!fdt_addr_r) {
265 		printf("No Found FDT Load Address.\n");
266 		return -1;
267 	}
268 
269 	*rd_data = fdt_addr_r;
270 
271 	debug("FDT load addr 0x%08x size %u KiB\n",
272 	      hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
273 
274 	return 0;
275 }
276 
277 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH)
278 static void print_hash(const char *label, u8 *hash, int len)
279 {
280 	int i;
281 
282 	printf("%s:\n    0x", label ? : "Hash");
283 	for (i = 0; i < len; i++)
284 		printf("%02x", hash[i]);
285 	printf("\n");
286 }
287 #endif
288 
289 typedef enum {
290 	IMG_KERNEL,
291 	IMG_RAMDISK,
292 	IMG_SECOND,
293 	IMG_RECOVERY_DTBO,
294 	IMG_RK_DTB,	/* within resource.img in second position */
295 	IMG_DTB,
296 	IMG_MAX,
297 } img_t;
298 
299 static int image_read(img_t img, struct andr_img_hdr *hdr,
300 		      ulong blkstart, void *ram_base,
301 		      struct udevice *crypto)
302 {
303 	struct blk_desc *desc = rockchip_get_bootdev();
304 	__maybe_unused u32 sizesz;
305 	ulong pgsz = hdr->page_size;
306 	ulong blksz = desc->blksz;
307 	ulong blkcnt, blkoff;
308 	ulong offset = 0;
309 	ulong datasz;
310 	void *ramdst;
311 	int ret = 0;
312 
313 	switch (img) {
314 	case IMG_KERNEL:
315 		offset = 0; /* include a page_size(image header) */
316 		blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz);
317 		ramdst = (void *)env_get_ulong("android_addr_r", 16, 0);
318 		datasz = hdr->kernel_size + pgsz;
319 		sizesz = sizeof(hdr->kernel_size);
320 		if (!sysmem_alloc_base(MEM_KERNEL,
321 				(phys_addr_t)ramdst, blkcnt * blksz))
322 			return -ENOMEM;
323 		break;
324 	case IMG_RAMDISK:
325 		offset = pgsz + ALIGN(hdr->kernel_size, pgsz);
326 		blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz);
327 		ramdst = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
328 		datasz = hdr->ramdisk_size;
329 		sizesz = sizeof(hdr->ramdisk_size);
330 		if (datasz && !sysmem_alloc_base(MEM_RAMDISK,
331 				(phys_addr_t)ramdst, blkcnt * blksz))
332 			return -ENOMEM;
333 		break;
334 	case IMG_SECOND:
335 		offset = pgsz +
336 			 ALIGN(hdr->kernel_size, pgsz) +
337 			 ALIGN(hdr->ramdisk_size, pgsz);
338 		blkcnt = DIV_ROUND_UP(hdr->second_size, blksz);
339 		datasz = hdr->second_size;
340 		sizesz = sizeof(hdr->second_size);
341 		ramdst = malloc(blkcnt * blksz);
342 		break;
343 	case IMG_RECOVERY_DTBO:
344 		offset = pgsz +
345 			 ALIGN(hdr->kernel_size, pgsz) +
346 			 ALIGN(hdr->ramdisk_size, pgsz) +
347 			 ALIGN(hdr->second_size, pgsz);
348 		blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz);
349 		datasz = hdr->recovery_dtbo_size;
350 		sizesz = sizeof(hdr->recovery_dtbo_size);
351 		ramdst = malloc(blkcnt * blksz);
352 		break;
353 	case IMG_DTB:
354 		offset = pgsz +
355 			 ALIGN(hdr->kernel_size, pgsz) +
356 			 ALIGN(hdr->ramdisk_size, pgsz) +
357 			 ALIGN(hdr->second_size, pgsz) +
358 			 ALIGN(hdr->recovery_dtbo_size, pgsz);
359 		blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz);
360 		datasz = hdr->dtb_size;
361 		sizesz = sizeof(hdr->dtb_size);
362 		ramdst = malloc(blkcnt * blksz);
363 		break;
364 	case IMG_RK_DTB:
365 #ifdef CONFIG_RKIMG_BOOTLOADER
366 		/* No going further, it handles DTBO, HW-ID, etc */
367 		ramdst = (void *)env_get_ulong("fdt_addr_r", 16, 0);
368 		if (gd->fdt_blob != (void *)ramdst)
369 			ret = rockchip_read_dtb_file(ramdst);
370 #endif
371 		return ret < 0 ? ret : 0;
372 	default:
373 		return -EINVAL;
374 	}
375 
376 	if (!ramdst) {
377 		printf("No memory for image(%d)\n", img);
378 		return -ENOMEM;
379 	}
380 
381 	if (!blksz || !datasz)
382 		goto crypto_calc;
383 
384 	/* load */
385 	if (ram_base) {
386 		memcpy(ramdst, (char *)((ulong)ram_base + offset), datasz);
387 	} else {
388 		blkoff = DIV_ROUND_UP(offset, blksz);
389 		ret = blk_dread(desc, blkstart + blkoff, blkcnt, ramdst);
390 		if (ret != blkcnt) {
391 			printf("Failed to read img(%d), ret=%d\n", img, ret);
392 			return -EIO;
393 		}
394 	}
395 
396 crypto_calc:
397 	/* sha1 */
398 #ifdef CONFIG_DM_CRYPTO
399 	if (crypto) {
400 		if (img == IMG_KERNEL) {
401 			ramdst += pgsz;
402 			datasz -= pgsz;
403 		}
404 
405 		crypto_sha_update(crypto, (u32 *)ramdst, datasz);
406 		crypto_sha_update(crypto, (u32 *)&datasz, sizesz);
407 	}
408 #endif
409 
410 	return 0;
411 }
412 
413 static int android_image_separate(struct andr_img_hdr *hdr,
414 				  const disk_partition_t *part,
415 				  void *load_address,
416 				  void *ram_base)
417 {
418 	ulong bstart;
419 
420 	if (android_image_check_header(hdr)) {
421 		printf("Bad android image header\n");
422 		return -EINVAL;
423 	}
424 
425 	/* set for image_read(IMG_KERNEL, ...) */
426 	env_set_hex("android_addr_r", (ulong)load_address);
427 	bstart = part ? part->start : 0;
428 
429 	/*
430 	 * 1. Load images to their individual target ram position
431 	 *    in order to disable fdt/ramdisk relocation.
432 	 */
433 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH)
434 	struct udevice *dev;
435 	sha_context ctx;
436 	uchar hash[20];
437 
438 	ctx.length = 0;
439 	ctx.algo = CRYPTO_SHA1;
440 	dev = crypto_get_device(ctx.algo);
441 	if (!dev) {
442 		printf("Can't find crypto device for SHA1 capability\n");
443 		return -ENODEV;
444 	}
445 
446   #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
447 	/* v1: requires total length before sha init */
448 	ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) +
449 		      hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
450 		      hdr->second_size + sizeof(hdr->second_size);
451 	if (hdr->header_version > 0)
452 		ctx.length += hdr->recovery_dtbo_size +
453 					sizeof(hdr->recovery_dtbo_size);
454 	if (hdr->header_version > 1)
455 		ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size);
456   #endif
457 	crypto_sha_init(dev, &ctx);
458 
459 	/* load, never change order ! */
460 	if (image_read(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
461 		return -1;
462 	if (image_read(IMG_KERNEL,  hdr, bstart, ram_base, dev))
463 		return -1;
464 	if (image_read(IMG_RAMDISK, hdr, bstart, ram_base, dev))
465 		return -1;
466 	if (image_read(IMG_SECOND,  hdr, bstart, ram_base, dev))
467 		return -1;
468 	if (hdr->header_version > 0) {
469 		if (image_read(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, dev))
470 			return -1;
471 	}
472 	if (hdr->header_version > 1) {
473 		if (image_read(IMG_DTB, hdr, bstart, ram_base, dev))
474 			return -1;
475 	}
476 
477 	crypto_sha_final(dev, &ctx, hash);
478 	if (memcmp(hash, hdr->id, 20)) {
479 		print_hash("Hash from header", (u8 *)hdr->id, 20);
480 		print_hash("Hash real", (u8 *)hash, 20);
481 		return -EBADFD;
482 	} else {
483 		printf("Image hash OK\n");
484 	}
485 
486 #else /* !(CONFIG_DM_CRYPTO && CONFIG_ANDROID_BOOT_IMAGE_HASH) */
487 	if (image_read(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
488 		return -1;
489 	if (image_read(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
490 		return -1;
491 	if (image_read(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
492 		return -1;
493 	if (image_read(IMG_SECOND,  hdr, bstart, ram_base, NULL))
494 		return -1;
495 	if (hdr->header_version > 0) {
496 		if (image_read(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, NULL))
497 			return -1;
498 	}
499 	if (hdr->header_version > 1) {
500 		if (image_read(IMG_DTB, hdr, bstart, ram_base, NULL))
501 			return -1;
502 	}
503 #endif
504 
505 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
506 	env_set("bootm-no-reloc", "y");
507 
508 	return 0;
509 }
510 
511 /*
512  * 'boot_android' cmd use "kernel_addr_r" as default load address !
513  * We update it according to compress type and "kernel_addr_c/r".
514  */
515 int android_image_parse_comp(struct andr_img_hdr *hdr, ulong *load_addr)
516 {
517 	ulong kernel_addr_c;
518 	int comp;
519 
520 	kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
521 	comp = android_image_parse_kernel_comp(hdr);
522 
523 #ifdef CONFIG_ARM64
524 	/*
525 	 * On 64-bit kernel, assuming use IMAGE by default.
526 	 *
527 	 * kernel_addr_c is for LZ4-IMAGE but maybe not defined.
528 	 * kernel_addr_r is for IMAGE.
529 	 */
530 	if (comp != IH_COMP_NONE) {
531 		ulong comp_addr;
532 
533 		if (kernel_addr_c) {
534 			comp_addr = kernel_addr_c;
535 		} else {
536 			printf("Warn: No \"kernel_addr_c\"\n");
537 			comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */
538 			env_set_hex("kernel_addr_c", comp_addr);
539 		}
540 
541 		*load_addr = comp_addr - hdr->page_size;
542 	}
543 #else
544 	/*
545 	 * On 32-bit kernel:
546 	 *
547 	 * The input load_addr is from env value: "kernel_addr_r", it has
548 	 * different role depends on whether kernel_addr_c is defined:
549 	 *
550 	 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined.
551 	 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined.
552 	 */
553 	if (comp == IH_COMP_NONE) {
554 		if (kernel_addr_c) {
555 			/* input load_addr is for Image, nothing to do */
556 		} else {
557 			/* input load_addr is for lz4/zImage, set default addr for Image */
558 			*load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000;
559 			env_set_hex("kernel_addr_r", *load_addr);
560 
561 			*load_addr -= hdr->page_size;
562 		}
563 	} else {
564 		if (kernel_addr_c) {
565 			/* input load_addr is for Image, so use another for lz4/zImage */
566 			*load_addr = kernel_addr_c - hdr->page_size;
567 		} else {
568 			/* input load_addr is for lz4/zImage, nothing to do */
569 		}
570 	}
571 #endif
572 
573 	env_set_ulong("os_comp", comp);
574 	return comp;
575 }
576 
577 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp)
578 {
579 	ulong kernel_addr_r;
580 
581 	/* zImage handles decompress itself */
582 	if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
583 		kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000);
584 		android_image_set_kload(hdr, kernel_addr_r);
585 		android_image_set_comp(hdr, comp);
586 	} else {
587 		android_image_set_comp(hdr, IH_COMP_NONE);
588 	}
589 }
590 
591 static int android_image_load_separate(struct andr_img_hdr *hdr,
592 				       const disk_partition_t *part,
593 				       void *load_addr)
594 {
595 	return android_image_separate(hdr, part, load_addr, NULL);
596 }
597 
598 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr)
599 {
600 	ulong comp_addr = *load_addr;
601 	int comp;
602 
603 	comp = android_image_parse_comp(hdr, &comp_addr);
604 	if (comp_addr == (ulong)hdr)
605 		return 0;
606 
607 	if (android_image_separate(hdr, NULL, (void *)comp_addr, hdr))
608 		return -1;
609 
610 	*load_addr = comp_addr;
611 	android_image_set_decomp((void *)comp_addr, comp);
612 
613 	return 0;
614 }
615 
616 long android_image_load(struct blk_desc *dev_desc,
617 			const disk_partition_t *part_info,
618 			unsigned long load_address,
619 			unsigned long max_size) {
620 	struct andr_img_hdr *hdr;
621 	u32 blksz = dev_desc->blksz;
622 	u32 pszcnt, hdrcnt, kercnt;
623 	int comp, ret;
624 
625 	if (max_size < part_info->blksz)
626 		return -1;
627 
628 	/*
629 	 * read Android image header and leave enough space for page_size align
630 	 * and kernel image header(1 block maybe enough).
631 	 *
632 	 * ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE is defined by rockchip legacy
633 	 * mkboot tool(SDK version < 8.1) and larger than Google defined.
634 	 *
635 	 * To compatible this, we malloc enough buffer but only read android
636 	 * header and kernel image(1 block) from storage(ignore page size).
637 	 */
638 	kercnt = 1;
639 	hdrcnt = DIV_ROUND_UP(sizeof(*hdr), blksz);
640 	pszcnt = DIV_ROUND_UP(ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE, blksz);
641 
642 	hdr = memalign(ARCH_DMA_MINALIGN, (hdrcnt + pszcnt + kercnt) * blksz);
643 	if (!hdr) {
644 		printf("No memory\n");
645 		return -1;
646 	}
647 
648 	if (blk_dread(dev_desc, part_info->start, hdrcnt, hdr) != hdrcnt) {
649 		printf("Failed to read image header\n");
650 		goto fail;
651 	}
652 
653 	if (android_image_check_header(hdr) != 0) {
654 		printf("** Invalid Android Image header **\n");
655 		goto fail;
656 	}
657 
658 	/*
659 	 * Update and skip pszcnt(hdr is included) according to hdr->page_size,
660 	 * reading kernel image for compress validation.
661 	 */
662 	pszcnt = DIV_ROUND_UP(hdr->page_size, blksz);
663 	if (blk_dread(dev_desc, part_info->start + pszcnt, kercnt,
664 		      (void *)((ulong)hdr + hdr->page_size)) != kercnt) {
665 		printf("Failed to read kernel header\n");
666 		goto fail;
667 	}
668 
669 	load_address -= hdr->page_size;
670 
671 	/* Let's load kernel now ! */
672 	comp = android_image_parse_comp(hdr, &load_address);
673 	ret = android_image_load_separate(hdr, part_info, (void *)load_address);
674 	if (ret) {
675 		printf("Failed to load android image\n");
676 		goto fail;
677 	}
678 	android_image_set_decomp((void *)load_address, comp);
679 
680 	debug("Loading Android Image to 0x%08lx\n", load_address);
681 
682 	free(hdr);
683 	return load_address;
684 
685 fail:
686 	free(hdr);
687 	return -1;
688 }
689 
690 #if !defined(CONFIG_SPL_BUILD)
691 /**
692  * android_print_contents - prints out the contents of the Android format image
693  * @hdr: pointer to the Android format image header
694  *
695  * android_print_contents() formats a multi line Android image contents
696  * description.
697  * The routine prints out Android image properties
698  *
699  * returns:
700  *     no returned results
701  */
702 void android_print_contents(const struct andr_img_hdr *hdr)
703 {
704 	const char * const p = IMAGE_INDENT_STRING;
705 	/* os_version = ver << 11 | lvl */
706 	u32 os_ver = hdr->os_version >> 11;
707 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
708 	u32 header_version = hdr->header_version;
709 
710 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
711 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
712 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
713 	printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr);
714 	printf("%ssecond size:      %x\n", p, hdr->second_size);
715 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
716 	printf("%stags address:     %x\n", p, hdr->tags_addr);
717 	printf("%spage size:        %x\n", p, hdr->page_size);
718 	printf("%sheader_version:   %x\n", p, header_version);
719 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
720 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
721 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
722 	       p, hdr->os_version,
723 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
724 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
725 	printf("%sname:             %s\n", p, hdr->name);
726 	printf("%scmdline:          %s\n", p, hdr->cmdline);
727 
728 	if (header_version >= 1) {
729 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
730 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
731 		printf("%sheader size:           %x\n", p, hdr->header_size);
732 	}
733 
734 	if (header_version >= 2) {
735 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
736 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
737 	}
738 }
739 #endif
740