xref: /rk3399_rockchip-uboot/common/image-android.c (revision 39e1dbd6c1d3233ff700226400ae55037368663d)
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_Q_VER				10
32 #define ANDROID_PARTITION_VENDOR_BOOT		"vendor_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;
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() >= ANDROID_Q_VER) ? 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,
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 	__maybe_unused u32 typesz;
347 	ulong pgsz = hdr->page_size;
348 	ulong blksz = desc->blksz;
349 	ulong blkcnt, blkoff;
350 	ulong memmove_dst = 0;
351 	ulong bsoffs = 0;
352 	ulong extra = 0;
353 	ulong length;
354 	void *buffer;
355 	int ret = 0;
356 
357 	switch (img) {
358 	case IMG_KERNEL:
359 		bsoffs = 0; /* include a page_size(image header) */
360 		length = hdr->kernel_size + pgsz;
361 		buffer = (void *)env_get_ulong("android_addr_r", 16, 0);
362 		blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz);
363 		typesz = sizeof(hdr->kernel_size);
364 		if (!sysmem_alloc_base(MEM_KERNEL,
365 			(phys_addr_t)buffer, blkcnt * blksz))
366 			return -ENOMEM;
367 		break;
368 	case IMG_VENDOR_RAMDISK:
369 		if (part_get_info_by_name(desc,
370 					  ANDROID_PARTITION_VENDOR_BOOT,
371 					  &part_vendor_boot) < 0) {
372 			printf("No vendor boot partition\n");
373 			return -ENOENT;
374 		}
375 		/* Always load vendor boot from storage: avb full load boot/recovery */
376 		blkstart = part_vendor_boot.start;
377 		pgsz = hdr->vendor_page_size;
378 		ram_base = 0;
379 
380 		bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz);
381 		length = hdr->vendor_ramdisk_size;
382 		buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
383 		blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz);
384 		typesz = sizeof(hdr->vendor_ramdisk_size);
385 		/*
386 		 * Add extra memory for generic ramdisk space.
387 		 *
388 		 * In case of unaligned vendor ramdisk size, reserve
389 		 * 1 more blksz.
390 		 *
391 		 * Reserve 8KB for bootloader cmdline.
392 		 */
393 		if (hdr->header_version >= 3)
394 			extra += ALIGN(hdr->ramdisk_size, blksz) + blksz;
395 		if (hdr->header_version >= 4)
396 			extra += ALIGN(hdr->vendor_bootconfig_size, blksz) +
397 				 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE;
398 		if (length && !sysmem_alloc_base(MEM_RAMDISK,
399 			(phys_addr_t)buffer, blkcnt * blksz + extra))
400 			return -ENOMEM;
401 		break;
402 	case IMG_RAMDISK:
403 		bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz);
404 		length = hdr->ramdisk_size;
405 		buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
406 		blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz);
407 		typesz = sizeof(hdr->ramdisk_size);
408 
409 		/*
410 		 * ramdisk_addr_r v012:
411 		 *	|----------------|
412 		 *	|    ramdisk     |
413 		 *	|----------------|
414 		 *
415 		 * ramdisk_addr_r v3:
416 		 *	|----------------|---------|
417 		 *	| vendor-ramdisk | ramdisk |
418 		 *	|----------------|---------|
419 		 *
420 		 * ramdisk_addr_r v4:
421 		 *	|----------------|---------|------------|------------|
422 		 *	| vendor-ramdisk | ramdisk | bootconfig | bootparams |
423 		 *	|----------------|---------|------------|------------|
424 		 */
425 		if (hdr->header_version >= 3) {
426 			buffer += hdr->vendor_ramdisk_size;
427 			if (!IS_ALIGNED((ulong)buffer, blksz)) {
428 				memmove_dst = (ulong)buffer;
429 				buffer = (void *)ALIGN(memmove_dst, blksz);
430 			}
431 		}
432 		/* sysmem has been alloced by vendor ramdisk */
433 		if (hdr->header_version < 3) {
434 			if (length && !sysmem_alloc_base(MEM_RAMDISK,
435 				(phys_addr_t)buffer, blkcnt * blksz))
436 				return -ENOMEM;
437 		}
438 		break;
439 	case IMG_BOOTCONFIG:
440 		if (hdr->header_version < 4)
441 			return 0;
442 		if (part_get_info_by_name(desc,
443 					  ANDROID_PARTITION_VENDOR_BOOT,
444 					  &part_vendor_boot) < 0) {
445 			printf("No vendor boot partition\n");
446 			return -ENOENT;
447 		}
448 
449 		blkstart = part_vendor_boot.start;
450 		pgsz = hdr->vendor_page_size;
451 		ram_base = 0;
452 
453 		bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) +
454 			 ALIGN(hdr->vendor_ramdisk_size, pgsz) +
455 			 ALIGN(hdr->dtb_size, pgsz) +
456 			 ALIGN(hdr->vendor_ramdisk_table_size, pgsz);
457 		length = hdr->vendor_bootconfig_size;
458 		buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
459 		blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz);
460 		typesz = sizeof(hdr->vendor_bootconfig_size);
461 
462 		buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size;
463 		if (!IS_ALIGNED((ulong)buffer, blksz)) {
464 			memmove_dst = (ulong)buffer;
465 			buffer = (void *)ALIGN(memmove_dst, blksz);
466 		}
467 		break;
468 	case IMG_SECOND:
469 		bsoffs = pgsz +
470 			 ALIGN(hdr->kernel_size, pgsz) +
471 			 ALIGN(hdr->ramdisk_size, pgsz);
472 		length = hdr->second_size;
473 		blkcnt = DIV_ROUND_UP(hdr->second_size, blksz);
474 		buffer = malloc(blkcnt * blksz);
475 		typesz = sizeof(hdr->second_size);
476 		break;
477 	case IMG_RECOVERY_DTBO:
478 		bsoffs = pgsz +
479 			 ALIGN(hdr->kernel_size, pgsz) +
480 			 ALIGN(hdr->ramdisk_size, pgsz) +
481 			 ALIGN(hdr->second_size, pgsz);
482 		length = hdr->recovery_dtbo_size;
483 		blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz);
484 		buffer = malloc(blkcnt * blksz);
485 		typesz = sizeof(hdr->recovery_dtbo_size);
486 		break;
487 	case IMG_DTB:
488 		bsoffs = pgsz +
489 			 ALIGN(hdr->kernel_size, pgsz) +
490 			 ALIGN(hdr->ramdisk_size, pgsz) +
491 			 ALIGN(hdr->second_size, pgsz) +
492 			 ALIGN(hdr->recovery_dtbo_size, pgsz);
493 		length = hdr->dtb_size;
494 		blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz);
495 		buffer = malloc(blkcnt * blksz);
496 		typesz = sizeof(hdr->dtb_size);
497 		break;
498 	case IMG_RK_DTB:
499 #ifdef CONFIG_RKIMG_BOOTLOADER
500 		/* No going further, it handles DTBO, HW-ID, etc */
501 		buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0);
502 		if (gd->fdt_blob != (void *)buffer)
503 			ret = rockchip_read_dtb_file(buffer);
504 #endif
505 		return ret < 0 ? ret : 0;
506 	default:
507 		return -EINVAL;
508 	}
509 
510 	if (!buffer) {
511 		printf("No memory for image(%d)\n", img);
512 		return -ENOMEM;
513 	}
514 
515 	if (!blksz || !length)
516 		goto crypto_calc;
517 
518 	/* load */
519 	if (ram_base) {
520 		memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length);
521 	} else {
522 		blkoff = DIV_ROUND_UP(bsoffs, blksz);
523 		ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer);
524 		if (ret != blkcnt) {
525 			printf("Failed to read img(%d), ret=%d\n", img, ret);
526 			return -EIO;
527 		}
528 	}
529 
530 	if (memmove_dst)
531 		memmove((char *)memmove_dst, buffer, length);
532 
533 crypto_calc:
534 	if (img == IMG_KERNEL) {
535 		buffer += pgsz;
536 		length -= pgsz;
537 	}
538 
539 	/* sha1 */
540 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
541 #ifdef CONFIG_DM_CRYPTO
542 	crypto_sha_update(crypto, (u32 *)buffer, length);
543 	crypto_sha_update(crypto, (u32 *)&length, typesz);
544 #else
545 	sha1_update(&sha1_ctx, (void *)buffer, length);
546 	sha1_update(&sha1_ctx, (void *)&length, typesz);
547 #endif
548 #endif
549 
550 	return 0;
551 }
552 
553 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start,
554 			      void *ram_base, struct udevice *crypto)
555 {
556 	/* load, never change order ! */
557 	if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto))
558 		return -1;
559 	if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto))
560 		return -1;
561 	if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto))
562 		return -1;
563 	if (hdr->header_version > 0) {
564 		if (image_load(IMG_RECOVERY_DTBO, hdr, part_start,
565 			       ram_base, crypto))
566 			return -1;
567 	}
568 	if (hdr->header_version > 1) {
569 		if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto))
570 			return -1;
571 	}
572 
573 	return 0;
574 }
575 
576 /*
577  * @ram_base: !NULL means require memcpy for an exist full android image.
578  */
579 static int android_image_separate(struct andr_img_hdr *hdr,
580 				  const disk_partition_t *part,
581 				  void *load_address,
582 				  void *ram_base)
583 {
584 	ulong bstart;
585 	int ret;
586 
587 	if (android_image_check_header(hdr)) {
588 		printf("Bad android image header\n");
589 		return -EINVAL;
590 	}
591 
592 	/* set for image_load(IMG_KERNEL, ...) */
593 	env_set_hex("android_addr_r", (ulong)load_address);
594 	bstart = part ? part->start : 0;
595 
596 	/*
597 	 * 1. Load images to their individual target ram position
598 	 *    in order to disable fdt/ramdisk relocation.
599 	 */
600 
601 	/* load rk-kernel.dtb alone */
602 	if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL))
603 		return -1;
604 
605 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH
606 	if (hdr->header_version < 3) {
607 		struct udevice *dev = NULL;
608 		uchar hash[20];
609 #ifdef CONFIG_DM_CRYPTO
610 		sha_context ctx;
611 
612 		ctx.length = 0;
613 		ctx.algo = CRYPTO_SHA1;
614 		dev = crypto_get_device(ctx.algo);
615 		if (!dev) {
616 			printf("Can't find crypto device for SHA1\n");
617 			return -ENODEV;
618 		}
619 
620 		/* v1 & v2: requires total length before sha init */
621 		ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) +
622 			      hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
623 			      hdr->second_size + sizeof(hdr->second_size);
624 		if (hdr->header_version > 0)
625 			ctx.length += hdr->recovery_dtbo_size +
626 						sizeof(hdr->recovery_dtbo_size);
627 		if (hdr->header_version > 1)
628 			ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size);
629 		crypto_sha_init(dev, &ctx);
630 #else
631 		sha1_starts(&sha1_ctx);
632 #endif
633 		ret = images_load_verify(hdr, bstart, ram_base, dev);
634 		if (ret)
635 			return ret;
636 
637 #ifdef CONFIG_DM_CRYPTO
638 		crypto_sha_final(dev, &ctx, hash);
639 #else
640 		sha1_finish(&sha1_ctx, hash);
641 #endif
642 		if (memcmp(hash, hdr->id, 20)) {
643 			print_hash("Hash from header", (u8 *)hdr->id, 20);
644 			print_hash("Hash real", (u8 *)hash, 20);
645 			return -EBADFD;
646 		} else {
647 			printf("ANDROID: Hash OK\n");
648 		}
649 	} else
650 #endif
651 	{
652 		ret = images_load_verify(hdr, bstart, ram_base, NULL);
653 		if (ret)
654 			return ret;
655 	}
656 
657 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
658 	env_set("bootm-no-reloc", "y");
659 
660 	return 0;
661 }
662 
663 static int android_image_separate_v34(struct andr_img_hdr *hdr,
664 				      const disk_partition_t *part,
665 				      void *load_address, void *ram_base)
666 {
667 	ulong bstart;
668 
669 	if (android_image_check_header(hdr)) {
670 		printf("Bad android image header\n");
671 		return -EINVAL;
672 	}
673 
674 	/* set for image_load(IMG_KERNEL, ...) */
675 	env_set_hex("android_addr_r", (ulong)load_address);
676 	bstart = part ? part->start : 0;
677 
678 	/*
679 	 * 1. Load images to their individual target ram position
680 	 *    in order to disable fdt/ramdisk relocation.
681 	 */
682 	if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
683 		return -1;
684 	if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
685 		return -1;
686 	if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL))
687 		return -1;
688 	if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
689 		return -1;
690 	if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base, NULL))
691 		return -1;
692 	/*
693 	 * Copy the populated hdr to load address after image_load(IMG_KERNEL)
694 	 *
695 	 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while
696 	 * vendor_boot_img_hdr_v34 is not included, so fix it here.
697 	 */
698 	memcpy((char *)load_address, hdr, hdr->page_size);
699 
700 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
701 	env_set("bootm-no-reloc", "y");
702 
703 	return 0;
704 }
705 
706 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp)
707 {
708 	ulong kernel_addr_c;
709 	ulong load_addr = 0;
710 
711 	kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
712 
713 #ifdef CONFIG_ARM64
714 	/*
715 	 * On 64-bit kernel, assuming use IMAGE by default.
716 	 *
717 	 * kernel_addr_c is for LZ4-IMAGE but maybe not defined.
718 	 * kernel_addr_r is for IMAGE.
719 	 */
720 	if (comp != IH_COMP_NONE) {
721 		ulong comp_addr;
722 
723 		if (kernel_addr_c) {
724 			comp_addr = kernel_addr_c;
725 		} else {
726 			printf("Warn: No \"kernel_addr_c\"\n");
727 			comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */
728 			env_set_hex("kernel_addr_c", comp_addr);
729 		}
730 
731 		load_addr = comp_addr - hdr->page_size;
732 	}
733 #else
734 	/*
735 	 * On 32-bit kernel:
736 	 *
737 	 * The input load_addr is from env value: "kernel_addr_r", it has
738 	 * different role depends on whether kernel_addr_c is defined:
739 	 *
740 	 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined.
741 	 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined.
742 	 */
743 	if (comp == IH_COMP_NONE) {
744 		if (kernel_addr_c) {
745 			/* input load_addr is for Image, nothing to do */
746 		} else {
747 			/* input load_addr is for lz4/zImage, set default addr for Image */
748 			load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000;
749 			env_set_hex("kernel_addr_r", load_addr);
750 
751 			load_addr -= hdr->page_size;
752 		}
753 	} else {
754 		if (kernel_addr_c) {
755 			/* input load_addr is for Image, so use another for lz4/zImage */
756 			load_addr = kernel_addr_c - hdr->page_size;
757 		} else {
758 			/* input load_addr is for lz4/zImage, nothing to do */
759 		}
760 	}
761 #endif
762 
763 	return load_addr;
764 }
765 
766 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp)
767 {
768 	ulong kernel_addr_r;
769 
770 	env_set_ulong("os_comp", comp);
771 
772 	/* zImage handles decompress itself */
773 	if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
774 		kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000);
775 		android_image_set_kload(hdr, kernel_addr_r);
776 		android_image_set_comp(hdr, comp);
777 	} else {
778 		android_image_set_comp(hdr, IH_COMP_NONE);
779 	}
780 }
781 
782 static int android_image_load_separate(struct andr_img_hdr *hdr,
783 				       const disk_partition_t *part,
784 				       void *load_addr)
785 {
786 	if (hdr->header_version < 3)
787 		return android_image_separate(hdr, part, load_addr, NULL);
788 	else
789 		return android_image_separate_v34(hdr, part, load_addr, NULL);
790 }
791 
792 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr)
793 {
794 	ulong comp_addr;
795 	int comp;
796 
797 	comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
798 	comp_addr = android_image_get_comp_addr(hdr, comp);
799 
800 	/* non-compressed image: already in-place */
801 	if ((ulong)hdr == *load_addr)
802 		return 0;
803 
804 	/* compressed image */
805 	if (comp_addr) {
806 		*load_addr = comp_addr;
807 		if ((ulong)hdr == comp_addr)	/* already in-place */
808 			return 0;
809 	}
810 
811 	/*
812 	 * The most possible reason to arrive here is:
813 	 *
814 	 * VBoot=1 and AVB load full partition to a temp memory buffer, now we
815 	 * separate(memcpy) subimages from boot.img to where they should be.
816 	 */
817 	if (hdr->header_version < 3) {
818 		if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr))
819 			return -1;
820 	} else {
821 		if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr))
822 			return -1;
823 	}
824 
825 	android_image_set_decomp((void *)(*load_addr), comp);
826 
827 	return 0;
828 }
829 
830 long android_image_load(struct blk_desc *dev_desc,
831 			const disk_partition_t *part_info,
832 			unsigned long load_address,
833 			unsigned long max_size) {
834 	struct andr_img_hdr *hdr;
835 	ulong comp_addr;
836 	int comp, ret;
837 	int blk_off;
838 
839 	if (max_size < part_info->blksz)
840 		return -1;
841 
842 	hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info);
843 	if (!hdr) {
844 		printf("No valid android hdr\n");
845 		return -1;
846 	}
847 
848 	/*
849 	 * create the layout:
850 	 *
851 	 * |<- page_size ->|1-blk |
852 	 * |-----|---------|------|-----|
853 	 * | hdr |   ...   |   kernel   |
854 	 * |-----|----- ---|------------|
855 	 *
856 	 * Alloc page_size and 1 more blk for reading kernel image to
857 	 * get it's compression type, then fill the android hdr what
858 	 * we have populated before.
859 	 *
860 	 * Why? see: android_image_get_kernel_addr().
861 	 */
862 	blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz);
863 	hdr = (struct andr_img_hdr *)
864 			realloc(hdr, (blk_off + 1) * dev_desc->blksz);
865 	if (!hdr)
866 		return -1;
867 
868 	if (blk_dread(dev_desc, part_info->start + blk_off, 1,
869 		      (char *)hdr + hdr->page_size) != 1) {
870 		free(hdr);
871 		return -1;
872 	}
873 
874 	/* Changed to compressed address ? */
875 	comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
876 	comp_addr = android_image_get_comp_addr(hdr, comp);
877 	if (comp_addr)
878 		load_address = comp_addr;
879 	else
880 		load_address -= hdr->page_size;
881 
882 	ret = android_image_load_separate(hdr, part_info, (void *)load_address);
883 	if (ret) {
884 		printf("Failed to load android image\n");
885 		goto fail;
886 	}
887 	android_image_set_decomp((void *)load_address, comp);
888 
889 	debug("Loading Android Image to 0x%08lx\n", load_address);
890 
891 	free(hdr);
892 	return load_address;
893 
894 fail:
895 	free(hdr);
896 	return -1;
897 }
898 
899 static struct andr_img_hdr *
900 extract_boot_image_v012_header(struct blk_desc *dev_desc,
901 			       const disk_partition_t *boot_img)
902 {
903 	struct andr_img_hdr *hdr;
904 	long blk_cnt, blks_read;
905 
906 	blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz);
907 	hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz);
908 
909 	if (!blk_cnt || !hdr)
910 		return NULL;
911 
912 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr);
913 	if (blks_read != blk_cnt) {
914 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
915 		      blk_cnt, blks_read);
916 		return NULL;
917 	}
918 
919 	if (android_image_check_header((void *)hdr)) {
920 		printf("boot header magic is invalid.\n");
921 		return NULL;
922 	}
923 
924 	if (hdr->page_size < sizeof(*hdr)) {
925 		printf("android hdr is over size\n");
926 		return NULL;
927 	}
928 
929 	return hdr;
930 }
931 
932 static struct boot_img_hdr_v34 *
933 extract_boot_image_v34_header(struct blk_desc *dev_desc,
934 			      const disk_partition_t *boot_img)
935 {
936 	struct boot_img_hdr_v34 *boot_hdr;
937 	long blk_cnt, blks_read;
938 
939 	blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz);
940 	boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz);
941 
942 	if (!blk_cnt || !boot_hdr)
943 		return NULL;
944 
945 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_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 *)boot_hdr)) {
953 		printf("boot header magic is invalid.\n");
954 		return NULL;
955 	}
956 
957 	if (boot_hdr->header_version < 3) {
958 		printf("boot header %d, is not >= v3.\n",
959 		       boot_hdr->header_version);
960 		return NULL;
961 	}
962 
963 	return boot_hdr;
964 }
965 
966 static struct vendor_boot_img_hdr_v34 *
967 extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc,
968 				     const disk_partition_t *part_vendor_boot)
969 {
970 	struct vendor_boot_img_hdr_v34 *vboot_hdr;
971 	long blk_cnt, blks_read;
972 
973 	blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34),
974 				part_vendor_boot->blksz);
975 	vboot_hdr = (struct vendor_boot_img_hdr_v34 *)
976 				malloc(blk_cnt * part_vendor_boot->blksz);
977 
978 	if (!blk_cnt || !vboot_hdr)
979 		return NULL;
980 
981 	blks_read = blk_dread(dev_desc, part_vendor_boot->start,
982 			      blk_cnt, vboot_hdr);
983 	if (blks_read != blk_cnt) {
984 		debug("vboot img header blk cnt is %ld and blks read is %ld\n",
985 		      blk_cnt, blks_read);
986 		return NULL;
987 	}
988 
989 	if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic,
990 		    VENDOR_BOOT_MAGIC_SIZE)) {
991 		printf("vendor boot header is invalid.\n");
992 		return NULL;
993 	}
994 
995 	if (vboot_hdr->header_version < 3) {
996 		printf("vendor boot header %d, is not >= v3.\n",
997 		       vboot_hdr->header_version);
998 		return NULL;
999 	}
1000 
1001 	return vboot_hdr;
1002 }
1003 
1004 static int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr,
1005 			      const struct vendor_boot_img_hdr_v34 *vendor_hdr,
1006 			      struct andr_img_hdr *hdr)
1007 {
1008 	memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE);
1009 	memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE);
1010 
1011 	hdr->kernel_size = boot_hdr->kernel_size;
1012 	/* don't use vendor_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */
1013 	hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR;
1014 	/* generic ramdisk: immediately following the vendor ramdisk */
1015 	hdr->boot_ramdisk_size = boot_hdr->ramdisk_size;
1016 	hdr->ramdisk_size = boot_hdr->ramdisk_size;
1017 
1018 	/* actually, useless */
1019 	hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0);
1020 
1021 	/* removed in v3 */
1022 	hdr->second_size = 0;
1023 	hdr->second_addr = 0;
1024 
1025 	hdr->tags_addr = vendor_hdr->tags_addr;
1026 
1027 	/* fixed in v3 */
1028 	hdr->page_size = 4096;
1029 	hdr->header_version = boot_hdr->header_version;
1030 	hdr->os_version = boot_hdr->os_version;
1031 
1032 	memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE);
1033 	strncpy(hdr->name, (const char *)vendor_hdr->name, ANDR_BOOT_NAME_SIZE);
1034 
1035 	/* removed in v3 */
1036 	memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE);
1037 	memset(hdr->id, 0, 32);
1038 	memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE);
1039 	hdr->recovery_dtbo_size = 0;
1040 	hdr->recovery_dtbo_offset = 0;
1041 
1042 	hdr->header_size = boot_hdr->header_size;
1043 	hdr->dtb_size = vendor_hdr->dtb_size;
1044 	hdr->dtb_addr = vendor_hdr->dtb_addr;
1045 
1046 	/* boot_img_hdr_v34 fields */
1047 	hdr->vendor_ramdisk_size = vendor_hdr->vendor_ramdisk_size;
1048 	hdr->vendor_page_size = vendor_hdr->page_size;
1049 	hdr->vendor_header_version = vendor_hdr->header_version;
1050 	hdr->vendor_header_size = vendor_hdr->header_size;
1051 
1052 	hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE);
1053 	if (!hdr->total_cmdline)
1054 		return -ENOMEM;
1055 	strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline,
1056 		sizeof(boot_hdr->cmdline));
1057 	strncat(hdr->total_cmdline, " ", 1);
1058 	strncat(hdr->total_cmdline, (const char *)vendor_hdr->cmdline,
1059 		sizeof(vendor_hdr->cmdline));
1060 
1061 	/* new for header v4 */
1062 	if (vendor_hdr->header_version > 3) {
1063 		hdr->vendor_ramdisk_table_size =
1064 				vendor_hdr->vendor_ramdisk_table_size;
1065 		hdr->vendor_ramdisk_table_entry_num =
1066 				vendor_hdr->vendor_ramdisk_table_entry_num;
1067 		hdr->vendor_ramdisk_table_entry_size =
1068 				vendor_hdr->vendor_ramdisk_table_entry_size;
1069 		/*
1070 		 * If we place additional "androidboot.xxx" parameters after
1071 		 * bootconfig, this field value should be increased,
1072 		 * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE.
1073 		 */
1074 		hdr->vendor_bootconfig_size =
1075 				vendor_hdr->vendor_bootconfig_size;
1076 	} else {
1077 		hdr->vendor_ramdisk_table_size = 0;
1078 		hdr->vendor_ramdisk_table_entry_num = 0;
1079 		hdr->vendor_ramdisk_table_entry_size = 0;
1080 		hdr->vendor_bootconfig_size = 0;
1081 	}
1082 
1083 	if (hdr->page_size < sizeof(*hdr)) {
1084 		printf("android hdr is over size\n");
1085 		return -EINVAL;
1086 	}
1087 
1088 	return 0;
1089 }
1090 
1091 /*
1092  * The possible cases of boot.img + recovery.img:
1093  *
1094  * [N]: 0, 1, 2
1095  * [M]: 0, 1, 2, 3, 4
1096  *
1097  * |--------------------|---------------------|
1098  * |   boot.img         |    recovery.img     |
1099  * |--------------------|---------------------|
1100  * | boot_img_hdr_v[N]  |  boot_img_hdr_v[N]  | <= if A/B is not required
1101  * |--------------------|---------------------|
1102  * | boot_img_hdr_v34   |  boot_img_hdr_v2    | <= if A/B is not required
1103  * |------------------------------------------|
1104  * | boot_img_hdr_v[M], no recovery.img       | <= if A/B is required
1105  * |------------------------------------------|
1106  */
1107 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc,
1108 					   disk_partition_t *part_boot)
1109 {
1110 	disk_partition_t part_vendor_boot;
1111 	struct vendor_boot_img_hdr_v34 *vboot_hdr;
1112 	struct boot_img_hdr_v34 *boot_hdr;
1113 	struct andr_img_hdr *andr_hdr;
1114 	int header_version;
1115 
1116 	if (!dev_desc || !part_boot)
1117 		return NULL;
1118 
1119 	andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz);
1120 	if (!andr_hdr)
1121 		return NULL;
1122 
1123 	if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) {
1124 		free(andr_hdr);
1125 		return NULL;
1126 	}
1127 
1128 	if (android_image_check_header(andr_hdr)) {
1129 		free(andr_hdr);
1130 		return NULL;
1131 	}
1132 
1133 	header_version = andr_hdr->header_version;
1134 	free(andr_hdr);
1135 
1136 	if (header_version < 3) {
1137 		return extract_boot_image_v012_header(dev_desc, part_boot);
1138 	} else {
1139 		if (part_get_info_by_name(dev_desc,
1140 					  ANDROID_PARTITION_VENDOR_BOOT,
1141 					  &part_vendor_boot) < 0) {
1142 			printf("No vendor boot partition\n");
1143 			return NULL;
1144 		}
1145 		boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot);
1146 		vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc,
1147 							&part_vendor_boot);
1148 		if (!boot_hdr || !vboot_hdr)
1149 			goto image_load_exit;
1150 
1151 		andr_hdr = (struct andr_img_hdr *)
1152 				malloc(sizeof(struct andr_img_hdr));
1153 		if (!andr_hdr) {
1154 			printf("No memory for andr hdr\n");
1155 			goto image_load_exit;
1156 		}
1157 
1158 		if (populate_boot_info(boot_hdr, vboot_hdr, andr_hdr)) {
1159 			printf("populate boot info failed\n");
1160 			goto image_load_exit;
1161 		}
1162 
1163 		free(boot_hdr);
1164 		free(vboot_hdr);
1165 
1166 		return andr_hdr;
1167 
1168 image_load_exit:
1169 		free(boot_hdr);
1170 		free(vboot_hdr);
1171 
1172 		return NULL;
1173 	}
1174 
1175 	return NULL;
1176 }
1177 
1178 #if !defined(CONFIG_SPL_BUILD)
1179 /**
1180  * android_print_contents - prints out the contents of the Android format image
1181  * @hdr: pointer to the Android format image header
1182  *
1183  * android_print_contents() formats a multi line Android image contents
1184  * description.
1185  * The routine prints out Android image properties
1186  *
1187  * returns:
1188  *     no returned results
1189  */
1190 void android_print_contents(const struct andr_img_hdr *hdr)
1191 {
1192 	const char * const p = IMAGE_INDENT_STRING;
1193 	/* os_version = ver << 11 | lvl */
1194 	u32 os_ver = hdr->os_version >> 11;
1195 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
1196 	u32 header_version = hdr->header_version;
1197 
1198 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
1199 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
1200 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
1201 	printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
1202 	printf("%ssecond size:      %x\n", p, hdr->second_size);
1203 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
1204 	printf("%stags address:     %x\n", p, hdr->tags_addr);
1205 	printf("%spage size:        %x\n", p, hdr->page_size);
1206 	printf("%sheader_version:   %x\n", p, header_version);
1207 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
1208 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
1209 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
1210 	       p, hdr->os_version,
1211 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
1212 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
1213 	printf("%sname:             %s\n", p, hdr->name);
1214 	printf("%scmdline:          %s\n", p, hdr->cmdline);
1215 
1216 	if (header_version == 1 || header_version == 2) {
1217 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
1218 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
1219 		printf("%sheader size:           %x\n", p, hdr->header_size);
1220 	}
1221 
1222 	if (header_version == 2 || header_version == 3) {
1223 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
1224 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
1225 	}
1226 
1227 	if (header_version >= 3) {
1228 		printf("%scmdline:               %s\n", p, hdr->total_cmdline);
1229 		printf("%svendor ramdisk size:   %x\n", p, hdr->vendor_ramdisk_size);
1230 		printf("%svendor page size:      %x\n", p, hdr->vendor_page_size);
1231 		printf("%svendor header version: %d\n", p, hdr->vendor_header_version);
1232 		printf("%svendor header size:    %x\n", p, hdr->vendor_header_size);
1233 	}
1234 
1235 	if (header_version >= 4) {
1236 		printf("%svendor ramdisk table size:        %x\n",
1237 		       p, hdr->vendor_ramdisk_table_size);
1238 		printf("%svendor ramdisk table entry num:   %x\n",
1239 		       p, hdr->vendor_ramdisk_table_entry_num);
1240 		printf("%svendor ramdisk table entry size:  %x\n",
1241 		       p, hdr->vendor_ramdisk_table_entry_size);
1242 		printf("%svendor bootconfig size:           %d\n",
1243 		       p, hdr->vendor_bootconfig_size);
1244 	}
1245 }
1246 #endif
1247