xref: /rk3399_rockchip-uboot/common/image-android.c (revision 2602b1ba1429717f5d33bbe0fe45c601c9a6c923)
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_PARTITION_VENDOR_BOOT		"vendor_boot"
32 #define ANDROID_PARTITION_INIT_BOOT		"init_boot"
33 
34 #define BLK_CNT(_num_bytes, _block_size)	\
35 		((_num_bytes + _block_size - 1) / _block_size)
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 & 0x7f;
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 0x00 offset.
51 	 *
52 	 * From Android-Q, the 0x00 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() >= 10) ? 0x00 : 0x20;
57 #else
58 	return 0x00;
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 	const char *cmdline = hdr->header_version < 3 ?
135 			      hdr->cmdline : hdr->total_cmdline;
136 	/*
137 	 * Not all Android tools use the id field for signing the image with
138 	 * sha1 (or anything) so we don't check it. It is not obvious that the
139 	 * string is null terminated so we take care of this.
140 	 */
141 	strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
142 	andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
143 	if (strlen(andr_tmp_str))
144 		printf("Android's image name: %s\n", andr_tmp_str);
145 
146 	printf("Kernel: 0x%08x - 0x%08x (%u KiB)\n",
147 	       kernel_addr, kernel_addr + hdr->kernel_size,
148 	       DIV_ROUND_UP(hdr->kernel_size, 1024));
149 
150 	int len = 0;
151 	if (cmdline) {
152 		debug("Kernel command line: %s\n", cmdline);
153 		len += strlen(cmdline);
154 	}
155 
156 	char *bootargs = env_get("bootargs");
157 	if (bootargs)
158 		len += strlen(bootargs);
159 
160 	char *newbootargs = malloc(len + 2);
161 	if (!newbootargs) {
162 		puts("Error: malloc in android_image_get_kernel failed!\n");
163 		return -ENOMEM;
164 	}
165 	*newbootargs = '\0';
166 
167 	if (bootargs) {
168 		strcpy(newbootargs, bootargs);
169 		strcat(newbootargs, " ");
170 	}
171 	if (cmdline)
172 		strcat(newbootargs, cmdline);
173 
174 	env_set("bootargs", newbootargs);
175 
176 	if (os_data) {
177 		*os_data = (ulong)hdr;
178 		*os_data += hdr->page_size;
179 	}
180 	if (os_len)
181 		*os_len = hdr->kernel_size;
182 	return 0;
183 }
184 
185 int android_image_check_header(const struct andr_img_hdr *hdr)
186 {
187 	return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
188 }
189 
190 ulong android_image_get_end(const struct andr_img_hdr *hdr)
191 {
192 	ulong end;
193 	/*
194 	 * The header takes a full page, the remaining components are aligned
195 	 * on page boundary
196 	 */
197 	end = (ulong)hdr;
198 	if (hdr->header_version < 3) {
199 		end += hdr->page_size;
200 		end += ALIGN(hdr->kernel_size, hdr->page_size);
201 		end += ALIGN(hdr->ramdisk_size, hdr->page_size);
202 		end += ALIGN(hdr->second_size, hdr->page_size);
203 		if (hdr->header_version == 1) {
204 			end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
205 		} else if (hdr->header_version == 2) {
206 			end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
207 			end += ALIGN(hdr->dtb_size, hdr->page_size);
208 		}
209 	} else {
210 		/* boot_img_hdr_v34 */
211 		end += hdr->page_size;
212 		end += ALIGN(hdr->kernel_size, hdr->page_size);
213 		end += ALIGN(hdr->ramdisk_size, hdr->page_size);
214 	}
215 
216 	return end;
217 }
218 
219 u32 android_image_get_ksize(const struct andr_img_hdr *hdr)
220 {
221 	return hdr->kernel_size;
222 }
223 
224 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address)
225 {
226 	hdr->kernel_addr = load_address;
227 }
228 
229 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
230 {
231 	return android_image_get_kernel_addr(hdr);
232 }
233 
234 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
235 			      ulong *rd_data, ulong *rd_len)
236 {
237 	ulong ramdisk_addr_r;
238 	ulong start, end;
239 
240 	if (!hdr->ramdisk_size) {
241 		*rd_data = *rd_len = 0;
242 		return -1;
243 	}
244 
245 	/* Have been loaded by android_image_load_separate() on ramdisk_addr_r */
246 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
247 	if (!ramdisk_addr_r) {
248 		printf("No Found Ramdisk Load Address.\n");
249 		return -1;
250 	}
251 
252 	*rd_data = ramdisk_addr_r;
253 	*rd_len = hdr->ramdisk_size;
254 	if (hdr->header_version >= 3)
255 		*rd_len += hdr->vendor_ramdisk_size;
256 	if (hdr->header_version >= 4) {
257 		 *rd_len += hdr->vendor_bootconfig_size +
258 		  ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
259 	}
260 
261 	/* just for print msg */
262 	start = ramdisk_addr_r;
263 	if (hdr->header_version >= 3) {
264 		end = start + (ulong)hdr->vendor_ramdisk_size;
265 		printf("v-ramdisk:  0x%08lx - 0x%08lx (%u KiB)\n",
266 		       start, end, DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024));
267 		start = end;
268 	}
269 	{
270 		end = start + (ulong)hdr->ramdisk_size;
271 		printf("ramdisk:    0x%08lx - 0x%08lx (%u KiB)\n",
272 		       start, end, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
273 		start = end;
274 	}
275 	if (hdr->header_version >= 4) {
276 		end = start + (ulong)hdr->vendor_bootconfig_size;
277 		printf("bootconfig: 0x%08lx - 0x%08lx (%u KiB)\n",
278 		       start, end, DIV_ROUND_UP(hdr->vendor_bootconfig_size, 1024));
279 		start = end;
280 		end = start + ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
281 		printf("bootparams: 0x%08lx - 0x%08lx\n", start, end);
282 	}
283 
284 	return 0;
285 }
286 
287 int android_image_get_fdt(const struct andr_img_hdr *hdr,
288 			      ulong *rd_data)
289 {
290 	ulong fdt_addr_r;
291 
292 	if (!hdr->second_size) {
293 		*rd_data = 0;
294 		return -1;
295 	}
296 
297 	/* Have been loaded by android_image_load_separate() on fdt_addr_r */
298 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
299 	if (!fdt_addr_r) {
300 		printf("No Found FDT Load Address.\n");
301 		return -1;
302 	}
303 
304 	*rd_data = fdt_addr_r;
305 
306 	debug("FDT load addr 0x%08x size %u KiB\n",
307 	      hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
308 
309 	return 0;
310 }
311 
312 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
313 static void print_hash(const char *label, u8 *hash, int len)
314 {
315 	int i;
316 
317 	printf("%s:\n    0x", label ? : "Hash");
318 	for (i = 0; i < len; i++)
319 		printf("%02x", hash[i]);
320 	printf("\n");
321 }
322 #endif
323 
324 typedef enum {
325 	IMG_KERNEL,
326 	IMG_RAMDISK,	/* within boot.img or init_boot.img(Android-13 or later) */
327 	IMG_SECOND,
328 	IMG_RECOVERY_DTBO,
329 	IMG_RK_DTB,	/* within resource.img in second position */
330 	IMG_DTB,
331 	IMG_VENDOR_RAMDISK,
332 	IMG_BOOTCONFIG,
333 	IMG_MAX,
334 } img_t;
335 
336 #if defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) && !defined(CONFIG_DM_CRYPTO)
337 static sha1_context sha1_ctx;
338 #endif
339 
340 static int image_load(img_t img, struct andr_img_hdr *hdr,
341 		      ulong blkstart, void *ram_base,
342 		      struct udevice *crypto)
343 {
344 	struct blk_desc *desc = rockchip_get_bootdev();
345 	disk_partition_t part_vendor_boot;
346 	disk_partition_t part_init_boot;
347 	__maybe_unused u32 typesz;
348 	u32 andr_version = (hdr->os_version >> 25) & 0x7f;
349 	ulong pgsz = hdr->page_size;
350 	ulong blksz = desc->blksz;
351 	ulong blkcnt, blkoff;
352 	ulong memmove_dst = 0;
353 	ulong bsoffs = 0;
354 	ulong extra = 0;
355 	ulong length;
356 	void *buffer;
357 	int ret = 0;
358 
359 	switch (img) {
360 	case IMG_KERNEL:
361 		bsoffs = 0; /* include a page_size(image header) */
362 		length = hdr->kernel_size + pgsz;
363 		buffer = (void *)env_get_ulong("android_addr_r", 16, 0);
364 		blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz);
365 		typesz = sizeof(hdr->kernel_size);
366 		if (!sysmem_alloc_base(MEM_KERNEL,
367 			(phys_addr_t)buffer, blkcnt * blksz))
368 			return -ENOMEM;
369 		break;
370 	case IMG_VENDOR_RAMDISK:
371 		if (hdr->vendor_boot_buf) {
372 			ram_base = hdr->vendor_boot_buf;
373 		} else {
374 			if (part_get_info_by_name(desc,
375 						  ANDROID_PARTITION_VENDOR_BOOT,
376 						  &part_vendor_boot) < 0) {
377 				printf("No vendor boot partition\n");
378 				return -ENOENT;
379 			}
380 			ram_base = 0;
381 		}
382 
383 		blkstart = part_vendor_boot.start;
384 		pgsz = hdr->vendor_page_size;
385 		bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz);
386 		length = hdr->vendor_ramdisk_size;
387 		buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
388 		blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz);
389 		typesz = sizeof(hdr->vendor_ramdisk_size);
390 		/*
391 		 * Add extra memory for generic ramdisk space.
392 		 *
393 		 * In case of unaligned vendor ramdisk size, reserve
394 		 * 1 more blksz.
395 		 *
396 		 * Reserve 8KB for bootloader cmdline.
397 		 */
398 		if (hdr->header_version >= 3)
399 			extra += ALIGN(hdr->ramdisk_size, blksz) + blksz;
400 		if (hdr->header_version >= 4)
401 			extra += ALIGN(hdr->vendor_bootconfig_size, blksz) +
402 				 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
403 		if (length && !sysmem_alloc_base(MEM_RAMDISK,
404 			(phys_addr_t)buffer, blkcnt * blksz + extra))
405 			return -ENOMEM;
406 		break;
407 	case IMG_RAMDISK:
408 		/* get ramdisk from init_boot.img ? */
409 		if (hdr->header_version >= 4 && andr_version >= 13) {
410 			if (hdr->init_boot_buf) {
411 				ram_base = hdr->init_boot_buf;
412 			} else {
413 				if (part_get_info_by_name(desc,
414 				    ANDROID_PARTITION_INIT_BOOT, &part_init_boot) < 0) {
415 					printf("No init boot partition\n");
416 					return -ENOENT;
417 				}
418 				blkstart = part_init_boot.start;
419 				ram_base = 0;
420 			}
421 			bsoffs = pgsz;
422 		} else {
423 			/* get ramdisk from generic boot.img */
424 			bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz);
425 		}
426 
427 		length = hdr->ramdisk_size;
428 		buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
429 		blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz);
430 		typesz = sizeof(hdr->ramdisk_size);
431 
432 		/*
433 		 * ramdisk_addr_r v012:
434 		 *	|----------------|
435 		 *	|    ramdisk     |
436 		 *	|----------------|
437 		 *
438 		 * ramdisk_addr_r v3 (Android-11 and later):
439 		 *	|----------------|---------|
440 		 *	| vendor-ramdisk | ramdisk |
441 		 *	|----------------|---------|
442 		 *
443 		 * ramdisk_addr_r v4 (Android-12 and later):
444 		 *	|----------------|---------|------------|------------|
445 		 *	| vendor-ramdisk | ramdisk | bootconfig | bootparams |
446 		 *	|----------------|---------|------------|------------|
447 		 *
448 		 * ramdisk_addr_r v4 + init_boot(Android-13 and later):
449 		 *	|----------------|----------------|------------|------------|
450 		 *	| vendor-ramdisk | (init_)ramdisk | bootconfig | bootparams |
451 		 *	|----------------|----------------|------------|------------|
452 		 */
453 		if (hdr->header_version >= 3) {
454 			buffer += hdr->vendor_ramdisk_size;
455 			if (!IS_ALIGNED((ulong)buffer, blksz)) {
456 				memmove_dst = (ulong)buffer;
457 				buffer = (void *)ALIGN(memmove_dst, blksz);
458 			}
459 		}
460 		/* sysmem has been alloced by vendor ramdisk */
461 		if (hdr->header_version < 3) {
462 			if (length && !sysmem_alloc_base(MEM_RAMDISK,
463 				(phys_addr_t)buffer, blkcnt * blksz))
464 				return -ENOMEM;
465 		}
466 		break;
467 	case IMG_BOOTCONFIG:
468 		if (hdr->header_version < 4)
469 			return 0;
470 
471 		if (hdr->vendor_boot_buf) {
472 			ram_base = hdr->vendor_boot_buf;
473 		} else {
474 			if (part_get_info_by_name(desc,
475 						  ANDROID_PARTITION_VENDOR_BOOT,
476 						  &part_vendor_boot) < 0) {
477 				printf("No vendor boot partition\n");
478 				return -ENOENT;
479 			}
480 			ram_base = 0;
481 		}
482 		blkstart = part_vendor_boot.start;
483 		pgsz = hdr->vendor_page_size;
484 		bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) +
485 			 ALIGN(hdr->vendor_ramdisk_size, pgsz) +
486 			 ALIGN(hdr->dtb_size, pgsz) +
487 			 ALIGN(hdr->vendor_ramdisk_table_size, pgsz);
488 		length = hdr->vendor_bootconfig_size;
489 		buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
490 		blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz);
491 		typesz = sizeof(hdr->vendor_bootconfig_size);
492 
493 		buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size;
494 		if (!IS_ALIGNED((ulong)buffer, blksz)) {
495 			memmove_dst = (ulong)buffer;
496 			buffer = (void *)ALIGN(memmove_dst, blksz);
497 		}
498 		break;
499 	case IMG_SECOND:
500 		bsoffs = pgsz +
501 			 ALIGN(hdr->kernel_size, pgsz) +
502 			 ALIGN(hdr->ramdisk_size, pgsz);
503 		length = hdr->second_size;
504 		blkcnt = DIV_ROUND_UP(hdr->second_size, blksz);
505 		buffer = malloc(blkcnt * blksz);
506 		typesz = sizeof(hdr->second_size);
507 		break;
508 	case IMG_RECOVERY_DTBO:
509 		bsoffs = pgsz +
510 			 ALIGN(hdr->kernel_size, pgsz) +
511 			 ALIGN(hdr->ramdisk_size, pgsz) +
512 			 ALIGN(hdr->second_size, pgsz);
513 		length = hdr->recovery_dtbo_size;
514 		blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz);
515 		buffer = malloc(blkcnt * blksz);
516 		typesz = sizeof(hdr->recovery_dtbo_size);
517 		break;
518 	case IMG_DTB:
519 		bsoffs = pgsz +
520 			 ALIGN(hdr->kernel_size, pgsz) +
521 			 ALIGN(hdr->ramdisk_size, pgsz) +
522 			 ALIGN(hdr->second_size, pgsz) +
523 			 ALIGN(hdr->recovery_dtbo_size, pgsz);
524 		length = hdr->dtb_size;
525 		blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz);
526 		buffer = malloc(blkcnt * blksz);
527 		typesz = sizeof(hdr->dtb_size);
528 		break;
529 	case IMG_RK_DTB:
530 #ifdef CONFIG_RKIMG_BOOTLOADER
531 		/* No going further, it handles DTBO, HW-ID, etc */
532 		buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0);
533 		if (gd->fdt_blob != (void *)buffer)
534 			ret = rockchip_read_dtb_file(buffer);
535 #endif
536 		return ret < 0 ? ret : 0;
537 	default:
538 		return -EINVAL;
539 	}
540 
541 	if (!buffer) {
542 		printf("No memory for image(%d)\n", img);
543 		return -ENOMEM;
544 	}
545 
546 	if (!blksz || !length)
547 		goto crypto_calc;
548 
549 	/* load */
550 	if (ram_base) {
551 		memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length);
552 	} else {
553 		blkoff = DIV_ROUND_UP(bsoffs, blksz);
554 		ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer);
555 		if (ret != blkcnt) {
556 			printf("Failed to read img(%d), ret=%d\n", img, ret);
557 			return -EIO;
558 		}
559 	}
560 
561 	if (memmove_dst)
562 		memmove((char *)memmove_dst, buffer, length);
563 
564 crypto_calc:
565 	if (img == IMG_KERNEL) {
566 		buffer += pgsz;
567 		length -= pgsz;
568 	}
569 
570 	/* sha1 */
571 	if (hdr->header_version < 3) {
572 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
573 #ifdef CONFIG_DM_CRYPTO
574 		crypto_sha_update(crypto, (u32 *)buffer, length);
575 		crypto_sha_update(crypto, (u32 *)&length, typesz);
576 #else
577 		sha1_update(&sha1_ctx, (void *)buffer, length);
578 		sha1_update(&sha1_ctx, (void *)&length, typesz);
579 #endif
580 #endif
581 	}
582 
583 	return 0;
584 }
585 
586 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start,
587 			      void *ram_base, struct udevice *crypto)
588 {
589 	/* load, never change order ! */
590 	if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto))
591 		return -1;
592 	if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto))
593 		return -1;
594 	if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto))
595 		return -1;
596 	if (hdr->header_version > 0) {
597 		if (image_load(IMG_RECOVERY_DTBO, hdr, part_start,
598 			       ram_base, crypto))
599 			return -1;
600 	}
601 	if (hdr->header_version > 1) {
602 		if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto))
603 			return -1;
604 	}
605 
606 	return 0;
607 }
608 
609 /*
610  * @ram_base: !NULL means require memcpy for an exist full android image.
611  */
612 static int android_image_separate(struct andr_img_hdr *hdr,
613 				  const disk_partition_t *part,
614 				  void *load_address,
615 				  void *ram_base)
616 {
617 	ulong bstart;
618 	int ret;
619 
620 	if (android_image_check_header(hdr)) {
621 		printf("Bad android image header\n");
622 		return -EINVAL;
623 	}
624 
625 	/* set for image_load(IMG_KERNEL, ...) */
626 	env_set_hex("android_addr_r", (ulong)load_address);
627 	bstart = part ? part->start : 0;
628 
629 	/*
630 	 * 1. Load images to their individual target ram position
631 	 *    in order to disable fdt/ramdisk relocation.
632 	 */
633 
634 	/* load rk-kernel.dtb alone */
635 	if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL))
636 		return -1;
637 
638 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
639 	if (hdr->header_version < 3) {
640 		struct udevice *dev = NULL;
641 		uchar hash[20];
642 #ifdef CONFIG_DM_CRYPTO
643 		sha_context ctx;
644 
645 		ctx.length = 0;
646 		ctx.algo = CRYPTO_SHA1;
647 		dev = crypto_get_device(ctx.algo);
648 		if (!dev) {
649 			printf("Can't find crypto device for SHA1\n");
650 			return -ENODEV;
651 		}
652 
653 		/* v1 & v2: requires total length before sha init */
654 		ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) +
655 			      hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
656 			      hdr->second_size + sizeof(hdr->second_size);
657 		if (hdr->header_version > 0)
658 			ctx.length += hdr->recovery_dtbo_size +
659 						sizeof(hdr->recovery_dtbo_size);
660 		if (hdr->header_version > 1)
661 			ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size);
662 		crypto_sha_init(dev, &ctx);
663 #else
664 		sha1_starts(&sha1_ctx);
665 #endif
666 		ret = images_load_verify(hdr, bstart, ram_base, dev);
667 		if (ret)
668 			return ret;
669 
670 #ifdef CONFIG_DM_CRYPTO
671 		crypto_sha_final(dev, &ctx, hash);
672 #else
673 		sha1_finish(&sha1_ctx, hash);
674 #endif
675 		if (memcmp(hash, hdr->id, 20)) {
676 			print_hash("Hash from header", (u8 *)hdr->id, 20);
677 			print_hash("Hash real", (u8 *)hash, 20);
678 			return -EBADFD;
679 		} else {
680 			printf("ANDROID: Hash OK\n");
681 		}
682 	} else
683 #endif
684 	{
685 		ret = images_load_verify(hdr, bstart, ram_base, NULL);
686 		if (ret)
687 			return ret;
688 	}
689 
690 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
691 	env_set("bootm-no-reloc", "y");
692 
693 	return 0;
694 }
695 
696 static int android_image_separate_v34(struct andr_img_hdr *hdr,
697 				      const disk_partition_t *part,
698 				      void *load_address, void *ram_base)
699 {
700 	ulong bstart;
701 
702 	if (android_image_check_header(hdr)) {
703 		printf("Bad android image header\n");
704 		return -EINVAL;
705 	}
706 
707 	/* set for image_load(IMG_KERNEL, ...) */
708 	env_set_hex("android_addr_r", (ulong)load_address);
709 	bstart = part ? part->start : 0;
710 
711 	/*
712 	 * 1. Load images to their individual target ram position
713 	 *    in order to disable fdt/ramdisk relocation.
714 	 */
715 	if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
716 		return -1;
717 	if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
718 		return -1;
719 	if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL))
720 		return -1;
721 	if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
722 		return -1;
723 	if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base, NULL))
724 		return -1;
725 	/*
726 	 * Copy the populated hdr to load address after image_load(IMG_KERNEL)
727 	 *
728 	 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while
729 	 * vendor_boot_img_hdr_v34 is not included, so fix it here.
730 	 */
731 	memcpy((char *)load_address, hdr, hdr->page_size);
732 
733 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
734 	env_set("bootm-no-reloc", "y");
735 
736 	return 0;
737 }
738 
739 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp)
740 {
741 	ulong kernel_addr_c;
742 	ulong load_addr = 0;
743 
744 	kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
745 
746 #ifdef CONFIG_ARM64
747 	/*
748 	 * On 64-bit kernel, assuming use IMAGE by default.
749 	 *
750 	 * kernel_addr_c is for LZ4-IMAGE but maybe not defined.
751 	 * kernel_addr_r is for IMAGE.
752 	 */
753 	if (comp != IH_COMP_NONE) {
754 		ulong comp_addr;
755 
756 		if (kernel_addr_c) {
757 			comp_addr = kernel_addr_c;
758 		} else {
759 			printf("Warn: No \"kernel_addr_c\"\n");
760 			comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */
761 			env_set_hex("kernel_addr_c", comp_addr);
762 		}
763 
764 		load_addr = comp_addr - hdr->page_size;
765 	}
766 #else
767 	/*
768 	 * On 32-bit kernel:
769 	 *
770 	 * The input load_addr is from env value: "kernel_addr_r", it has
771 	 * different role depends on whether kernel_addr_c is defined:
772 	 *
773 	 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined.
774 	 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined.
775 	 */
776 	if (comp == IH_COMP_NONE) {
777 		if (kernel_addr_c) {
778 			/* input load_addr is for Image, nothing to do */
779 		} else {
780 			/* input load_addr is for lz4/zImage, set default addr for Image */
781 			load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000;
782 			env_set_hex("kernel_addr_r", load_addr);
783 
784 			load_addr -= hdr->page_size;
785 		}
786 	} else {
787 		if (kernel_addr_c) {
788 			/* input load_addr is for Image, so use another for lz4/zImage */
789 			load_addr = kernel_addr_c - hdr->page_size;
790 		} else {
791 			/* input load_addr is for lz4/zImage, nothing to do */
792 		}
793 	}
794 #endif
795 
796 	return load_addr;
797 }
798 
799 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp)
800 {
801 	ulong kernel_addr_r;
802 
803 	env_set_ulong("os_comp", comp);
804 
805 	/* zImage handles decompress itself */
806 	if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
807 		kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000);
808 		android_image_set_kload(hdr, kernel_addr_r);
809 		android_image_set_comp(hdr, comp);
810 	} else {
811 		android_image_set_comp(hdr, IH_COMP_NONE);
812 	}
813 }
814 
815 static int android_image_load_separate(struct andr_img_hdr *hdr,
816 				       const disk_partition_t *part,
817 				       void *load_addr)
818 {
819 	if (hdr->header_version < 3)
820 		return android_image_separate(hdr, part, load_addr, NULL);
821 	else
822 		return android_image_separate_v34(hdr, part, load_addr, NULL);
823 }
824 
825 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr)
826 {
827 	ulong comp_addr;
828 	int comp;
829 
830 	comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
831 	comp_addr = android_image_get_comp_addr(hdr, comp);
832 
833 	/* non-compressed image: already in-place */
834 	if ((ulong)hdr == *load_addr)
835 		return 0;
836 
837 	/* compressed image */
838 	if (comp_addr) {
839 		*load_addr = comp_addr;
840 		if ((ulong)hdr == comp_addr)	/* already in-place */
841 			return 0;
842 	}
843 
844 	/*
845 	 * The most possible reason to arrive here is:
846 	 *
847 	 * VBoot=1 and AVB load full partition to a temp memory buffer, now we
848 	 * separate(memcpy) subimages from boot.img to where they should be.
849 	 */
850 	if (hdr->header_version < 3) {
851 		if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr))
852 			return -1;
853 	} else {
854 		if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr))
855 			return -1;
856 	}
857 
858 	android_image_set_decomp((void *)(*load_addr), comp);
859 
860 	return 0;
861 }
862 
863 long android_image_load(struct blk_desc *dev_desc,
864 			const disk_partition_t *part_info,
865 			unsigned long load_address,
866 			unsigned long max_size) {
867 	struct andr_img_hdr *hdr;
868 	ulong comp_addr;
869 	int comp, ret;
870 	int blk_off;
871 
872 	if (max_size < part_info->blksz)
873 		return -1;
874 
875 	hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info);
876 	if (!hdr) {
877 		printf("No valid android hdr\n");
878 		return -1;
879 	}
880 
881 	/*
882 	 * create the layout:
883 	 *
884 	 * |<- page_size ->|1-blk |
885 	 * |-----|---------|------|-----|
886 	 * | hdr |   ...   |   kernel   |
887 	 * |-----|----- ---|------------|
888 	 *
889 	 * Alloc page_size and 1 more blk for reading kernel image to
890 	 * get it's compression type, then fill the android hdr what
891 	 * we have populated before.
892 	 *
893 	 * Why? see: android_image_get_kernel_addr().
894 	 */
895 	blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz);
896 	hdr = (struct andr_img_hdr *)
897 			realloc(hdr, (blk_off + 1) * dev_desc->blksz);
898 	if (!hdr)
899 		return -1;
900 
901 	if (blk_dread(dev_desc, part_info->start + blk_off, 1,
902 		      (char *)hdr + hdr->page_size) != 1) {
903 		free(hdr);
904 		return -1;
905 	}
906 
907 	/* Changed to compressed address ? */
908 	comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
909 	comp_addr = android_image_get_comp_addr(hdr, comp);
910 	if (comp_addr)
911 		load_address = comp_addr;
912 	else
913 		load_address -= hdr->page_size;
914 
915 	ret = android_image_load_separate(hdr, part_info, (void *)load_address);
916 	if (ret) {
917 		printf("Failed to load android image\n");
918 		goto fail;
919 	}
920 	android_image_set_decomp((void *)load_address, comp);
921 
922 	debug("Loading Android Image to 0x%08lx\n", load_address);
923 
924 	free(hdr);
925 	return load_address;
926 
927 fail:
928 	free(hdr);
929 	return -1;
930 }
931 
932 static struct andr_img_hdr *
933 extract_boot_image_v012_header(struct blk_desc *dev_desc,
934 			       const disk_partition_t *boot_img)
935 {
936 	struct andr_img_hdr *hdr;
937 	long blk_cnt, blks_read;
938 
939 	blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz);
940 	hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz);
941 
942 	if (!blk_cnt || !hdr)
943 		return NULL;
944 
945 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr);
946 	if (blks_read != blk_cnt) {
947 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
948 		      blk_cnt, blks_read);
949 		return NULL;
950 	}
951 
952 	if (android_image_check_header((void *)hdr)) {
953 		printf("boot header magic is invalid.\n");
954 		return NULL;
955 	}
956 
957 	if (hdr->page_size < sizeof(*hdr)) {
958 		printf("android hdr is over size\n");
959 		return NULL;
960 	}
961 
962 	return hdr;
963 }
964 
965 static struct boot_img_hdr_v34 *
966 extract_boot_image_v34_header(struct blk_desc *dev_desc,
967 			      const disk_partition_t *boot_img)
968 {
969 	struct boot_img_hdr_v34 *boot_hdr;
970 	disk_partition_t part;
971 	long blk_cnt, blks_read;
972 
973 	blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz);
974 	boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz);
975 
976 	if (!blk_cnt || !boot_hdr)
977 		return NULL;
978 
979 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr);
980 	if (blks_read != blk_cnt) {
981 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
982 		      blk_cnt, blks_read);
983 		return NULL;
984 	}
985 
986 	if (android_image_check_header((void *)boot_hdr)) {
987 		printf("boot header magic is invalid.\n");
988 		return NULL;
989 	}
990 
991 	if (boot_hdr->header_version < 3) {
992 		printf("boot header %d, is not >= v3.\n",
993 		       boot_hdr->header_version);
994 		return NULL;
995 	}
996 
997 	/* Start from android-13 GKI, it doesn't assign 'os_version' */
998 	if (boot_hdr->header_version >= 4 && boot_hdr->os_version == 0) {
999 		if (part_get_info_by_name(dev_desc,
1000 				ANDROID_PARTITION_INIT_BOOT, &part) > 0)
1001 			boot_hdr->os_version = 13 << 25;
1002 	}
1003 
1004 	return boot_hdr;
1005 }
1006 
1007 static struct vendor_boot_img_hdr_v34 *
1008 extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc,
1009 				     const disk_partition_t *part_vendor_boot)
1010 {
1011 	struct vendor_boot_img_hdr_v34 *vboot_hdr;
1012 	long blk_cnt, blks_read;
1013 
1014 	blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34),
1015 				part_vendor_boot->blksz);
1016 	vboot_hdr = (struct vendor_boot_img_hdr_v34 *)
1017 				malloc(blk_cnt * part_vendor_boot->blksz);
1018 
1019 	if (!blk_cnt || !vboot_hdr)
1020 		return NULL;
1021 
1022 	blks_read = blk_dread(dev_desc, part_vendor_boot->start,
1023 			      blk_cnt, vboot_hdr);
1024 	if (blks_read != blk_cnt) {
1025 		debug("vboot img header blk cnt is %ld and blks read is %ld\n",
1026 		      blk_cnt, blks_read);
1027 		return NULL;
1028 	}
1029 
1030 	if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic,
1031 		    VENDOR_BOOT_MAGIC_SIZE)) {
1032 		printf("vendor boot header is invalid.\n");
1033 		return NULL;
1034 	}
1035 
1036 	if (vboot_hdr->header_version < 3) {
1037 		printf("vendor boot header %d, is not >= v3.\n",
1038 		       vboot_hdr->header_version);
1039 		return NULL;
1040 	}
1041 
1042 	return vboot_hdr;
1043 }
1044 
1045 int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr,
1046 		       const struct vendor_boot_img_hdr_v34 *vendor_boot_hdr,
1047 		       const struct boot_img_hdr_v34 *init_boot_hdr,
1048 		       struct andr_img_hdr *hdr, bool save_hdr)
1049 {
1050 	memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE);
1051 	memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE);
1052 
1053 	hdr->kernel_size = boot_hdr->kernel_size;
1054 	/* don't use vendor_boot_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */
1055 	hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR;
1056 
1057 	/*
1058 	 * generic ramdisk: immediately following the vendor ramdisk.
1059 	 * It can be from init_boot.img or boot.img.
1060 	 */
1061 	if (init_boot_hdr)
1062 		hdr->ramdisk_size = init_boot_hdr->ramdisk_size;
1063 	else
1064 		hdr->ramdisk_size = boot_hdr->ramdisk_size;
1065 
1066 	/* actually, useless */
1067 	hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0);
1068 
1069 	/* removed in v3 */
1070 	hdr->second_size = 0;
1071 	hdr->second_addr = 0;
1072 
1073 	hdr->tags_addr = vendor_boot_hdr->tags_addr;
1074 
1075 	/* fixed in v3 */
1076 	hdr->page_size = 4096;
1077 	hdr->header_version = boot_hdr->header_version;
1078 	hdr->os_version = boot_hdr->os_version;
1079 
1080 	memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE);
1081 	strncpy(hdr->name, (const char *)vendor_boot_hdr->name, ANDR_BOOT_NAME_SIZE);
1082 
1083 	/* removed in v3 */
1084 	memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE);
1085 	memset(hdr->id, 0, 32);
1086 	memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE);
1087 	hdr->recovery_dtbo_size = 0;
1088 	hdr->recovery_dtbo_offset = 0;
1089 
1090 	hdr->header_size = boot_hdr->header_size;
1091 	hdr->dtb_size = vendor_boot_hdr->dtb_size;
1092 	hdr->dtb_addr = vendor_boot_hdr->dtb_addr;
1093 
1094 	/* boot_img_hdr_v34 fields */
1095 	hdr->vendor_ramdisk_size = vendor_boot_hdr->vendor_ramdisk_size;
1096 	hdr->vendor_page_size = vendor_boot_hdr->page_size;
1097 	hdr->vendor_header_version = vendor_boot_hdr->header_version;
1098 	hdr->vendor_header_size = vendor_boot_hdr->header_size;
1099 
1100 	hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE);
1101 	if (!hdr->total_cmdline)
1102 		return -ENOMEM;
1103 	strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline,
1104 		sizeof(boot_hdr->cmdline));
1105 	strncat(hdr->total_cmdline, " ", 1);
1106 	strncat(hdr->total_cmdline, (const char *)vendor_boot_hdr->cmdline,
1107 		sizeof(vendor_boot_hdr->cmdline));
1108 
1109 	/* new for header v4 */
1110 	if (vendor_boot_hdr->header_version >= 4) {
1111 		hdr->vendor_ramdisk_table_size =
1112 				vendor_boot_hdr->vendor_ramdisk_table_size;
1113 		hdr->vendor_ramdisk_table_entry_num =
1114 				vendor_boot_hdr->vendor_ramdisk_table_entry_num;
1115 		hdr->vendor_ramdisk_table_entry_size =
1116 				vendor_boot_hdr->vendor_ramdisk_table_entry_size;
1117 		/*
1118 		 * If we place additional "androidboot.xxx" parameters after
1119 		 * bootconfig, this field value should be increased,
1120 		 * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE.
1121 		 */
1122 		hdr->vendor_bootconfig_size =
1123 				vendor_boot_hdr->vendor_bootconfig_size;
1124 	} else {
1125 		hdr->vendor_ramdisk_table_size = 0;
1126 		hdr->vendor_ramdisk_table_entry_num = 0;
1127 		hdr->vendor_ramdisk_table_entry_size = 0;
1128 		hdr->vendor_bootconfig_size = 0;
1129 	}
1130 
1131 	hdr->init_boot_buf = save_hdr ? (void *)init_boot_hdr : 0;
1132 	hdr->vendor_boot_buf = save_hdr ? (void *)vendor_boot_hdr : 0;
1133 
1134 	if (hdr->page_size < sizeof(*hdr)) {
1135 		printf("android hdr is over size\n");
1136 		return -EINVAL;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 /*
1143  * The possible cases of boot.img + recovery.img:
1144  *
1145  * [N]: 0, 1, 2
1146  * [M]: 0, 1, 2, 3, 4
1147  *
1148  * |--------------------|---------------------|
1149  * |   boot.img         |    recovery.img     |
1150  * |--------------------|---------------------|
1151  * | boot_img_hdr_v[N]  |  boot_img_hdr_v[N]  | <= if A/B is not required
1152  * |--------------------|---------------------|
1153  * | boot_img_hdr_v34   |  boot_img_hdr_v2    | <= if A/B is not required
1154  * |------------------------------------------|
1155  * | boot_img_hdr_v[M], no recovery.img       | <= if A/B is required
1156  * |------------------------------------------|
1157  */
1158 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc,
1159 					   disk_partition_t *part_boot)
1160 {
1161 	disk_partition_t part_vendor_boot;
1162 	disk_partition_t part_init_boot;
1163 	struct vendor_boot_img_hdr_v34 *vboot_hdr = NULL;
1164 	struct boot_img_hdr_v34 *iboot_hdr = NULL;
1165 	struct boot_img_hdr_v34 *boot_hdr = NULL;
1166 	struct andr_img_hdr *andr_hdr = NULL;
1167 	int header_version;
1168 	int andr_version;
1169 
1170 	if (!dev_desc || !part_boot)
1171 		return NULL;
1172 
1173 	andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz);
1174 	if (!andr_hdr)
1175 		return NULL;
1176 
1177 	if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) {
1178 		free(andr_hdr);
1179 		return NULL;
1180 	}
1181 
1182 	if (android_image_check_header(andr_hdr)) {
1183 		free(andr_hdr);
1184 		return NULL;
1185 	}
1186 
1187 	header_version = andr_hdr->header_version;
1188 	free(andr_hdr);
1189 	andr_hdr = NULL;
1190 
1191 	if (header_version < 3) {
1192 		return extract_boot_image_v012_header(dev_desc, part_boot);
1193 	} else {
1194 		if (part_get_info_by_name(dev_desc,
1195 					  ANDROID_PARTITION_VENDOR_BOOT,
1196 					  &part_vendor_boot) < 0) {
1197 			printf("No vendor boot partition\n");
1198 			return NULL;
1199 		}
1200 		boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot);
1201 		vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc,
1202 							&part_vendor_boot);
1203 		if (!boot_hdr || !vboot_hdr)
1204 			goto image_load_exit;
1205 
1206 		andr_version = (boot_hdr->os_version >> 25) & 0x7f;
1207 		if (header_version >= 4 && andr_version >= 13) {
1208 			if (part_get_info_by_name(dev_desc,
1209 						  ANDROID_PARTITION_INIT_BOOT,
1210 						  &part_init_boot) < 0) {
1211 				printf("No init boot partition\n");
1212 				return NULL;
1213 			}
1214 			iboot_hdr = extract_boot_image_v34_header(dev_desc, &part_init_boot);
1215 			if (!iboot_hdr)
1216 				goto image_load_exit;
1217 			if (!iboot_hdr->ramdisk_size) {
1218 				printf("No ramdisk in init boot partition\n");
1219 				goto image_load_exit;
1220 			}
1221 		}
1222 
1223 		andr_hdr = (struct andr_img_hdr *)
1224 				malloc(sizeof(struct andr_img_hdr));
1225 		if (!andr_hdr) {
1226 			printf("No memory for andr hdr\n");
1227 			goto image_load_exit;
1228 		}
1229 
1230 		if (populate_boot_info(boot_hdr, vboot_hdr,
1231 				       iboot_hdr, andr_hdr, false)) {
1232 			printf("populate boot info failed\n");
1233 			goto image_load_exit;
1234 		}
1235 
1236 image_load_exit:
1237 		if (boot_hdr)
1238 			free(boot_hdr);
1239 		if (iboot_hdr)
1240 			free(iboot_hdr);
1241 		if (vboot_hdr)
1242 			free(vboot_hdr);
1243 
1244 		return andr_hdr;
1245 	}
1246 
1247 	return NULL;
1248 }
1249 
1250 #if !defined(CONFIG_SPL_BUILD)
1251 /**
1252  * android_print_contents - prints out the contents of the Android format image
1253  * @hdr: pointer to the Android format image header
1254  *
1255  * android_print_contents() formats a multi line Android image contents
1256  * description.
1257  * The routine prints out Android image properties
1258  *
1259  * returns:
1260  *     no returned results
1261  */
1262 void android_print_contents(const struct andr_img_hdr *hdr)
1263 {
1264 	const char * const p = IMAGE_INDENT_STRING;
1265 	/* os_version = ver << 11 | lvl */
1266 	u32 os_ver = hdr->os_version >> 11;
1267 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
1268 	u32 header_version = hdr->header_version;
1269 
1270 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
1271 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
1272 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
1273 	printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
1274 	printf("%ssecond size:      %x\n", p, hdr->second_size);
1275 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
1276 	printf("%stags address:     %x\n", p, hdr->tags_addr);
1277 	printf("%spage size:        %x\n", p, hdr->page_size);
1278 	printf("%sheader_version:   %x\n", p, header_version);
1279 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
1280 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
1281 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
1282 	       p, hdr->os_version,
1283 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
1284 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
1285 	printf("%sname:             %s\n", p, hdr->name);
1286 	printf("%scmdline:          %s\n", p, hdr->cmdline);
1287 
1288 	if (header_version == 1 || header_version == 2) {
1289 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
1290 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
1291 		printf("%sheader size:           %x\n", p, hdr->header_size);
1292 	}
1293 
1294 	if (header_version == 2 || header_version == 3) {
1295 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
1296 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
1297 	}
1298 
1299 	if (header_version >= 3) {
1300 		printf("%scmdline:               %s\n", p, hdr->total_cmdline);
1301 		printf("%svendor ramdisk size:   %x\n", p, hdr->vendor_ramdisk_size);
1302 		printf("%svendor page size:      %x\n", p, hdr->vendor_page_size);
1303 		printf("%svendor header version: %d\n", p, hdr->vendor_header_version);
1304 		printf("%svendor header size:    %x\n", p, hdr->vendor_header_size);
1305 	}
1306 
1307 	if (header_version >= 4) {
1308 		printf("%svendor ramdisk table size:        %x\n",
1309 		       p, hdr->vendor_ramdisk_table_size);
1310 		printf("%svendor ramdisk table entry num:   %x\n",
1311 		       p, hdr->vendor_ramdisk_table_entry_num);
1312 		printf("%svendor ramdisk table entry size:  %x\n",
1313 		       p, hdr->vendor_ramdisk_table_entry_size);
1314 		printf("%svendor bootconfig size:           %d\n",
1315 		       p, hdr->vendor_bootconfig_size);
1316 	}
1317 }
1318 #endif
1319