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