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