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