xref: /rk3399_rockchip-uboot/common/image-android.c (revision 548715c7d5ed761875cc95bcb03b9b4519687db6)
1 /*
2  * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <image.h>
9 #include <android_image.h>
10 #include <android_bootloader.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <errno.h>
14 #include <boot_rkimg.h>
15 #include <crypto.h>
16 #include <sysmem.h>
17 #include <u-boot/sha1.h>
18 #ifdef CONFIG_RKIMG_BOOTLOADER
19 #include <asm/arch/resource_img.h>
20 #endif
21 #ifdef CONFIG_RK_AVB_LIBAVB_USER
22 #include <android_avb/avb_slot_verify.h>
23 #include <android_avb/avb_ops_user.h>
24 #include <android_avb/rk_avb_ops_user.h>
25 #endif
26 #include <optee_include/OpteeClientInterface.h>
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
30 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR	0x10008000
31 #define ANDROID_ARG_FDT_FILENAME		"rk-kernel.dtb"
32 #define ANDROID_Q_VER				10
33 #define ANDROID_PARTITION_VENDOR_BOOT		"vendor_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 u32 android_image_major_version(void)
42 {
43 	/* MSB 7-bits */
44 	return gd->bd->bi_andr_version >> 25;
45 }
46 
47 u32 android_bcb_msg_sector_offset(void)
48 {
49 	/*
50 	 * Rockchip platforms defines BCB message at the 16KB offset of
51 	 * misc partition while the Google defines it at 0x00 offset.
52 	 *
53 	 * From Android-Q, the 0x00 offset is mandary on Google VTS, so that
54 	 * this is a compatibility according to android image 'os_version'.
55 	 */
56 #ifdef CONFIG_RKIMG_BOOTLOADER
57 	return (android_image_major_version() >= ANDROID_Q_VER) ? 0x00 : 0x20;
58 #else
59 	return 0x00;
60 #endif
61 }
62 
63 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
64 {
65 	/*
66 	 * All the Android tools that generate a boot.img use this
67 	 * address as the default.
68 	 *
69 	 * Even though it doesn't really make a lot of sense, and it
70 	 * might be valid on some platforms, we treat that address as
71 	 * the default value for this field, and try to execute the
72 	 * kernel in place in such a case.
73 	 *
74 	 * Otherwise, we will return the actual value set by the user.
75 	 */
76 	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
77 		return (ulong)hdr + hdr->page_size;
78 
79 #ifdef CONFIG_ARCH_ROCKCHIP
80 	/*
81 	 * If kernel is compressed, kernel_addr is set as decompressed address
82 	 * after compressed being loaded to ram, so let's use it.
83 	 */
84 	if (android_kernel_comp_type != IH_COMP_NONE &&
85 	    android_kernel_comp_type != IH_COMP_ZIMAGE)
86 		return hdr->kernel_addr;
87 
88 	/*
89 	 * Compatble with rockchip legacy packing with kernel/ramdisk/second
90 	 * address base from 0x60000000(SDK versiont < 8.1), these are invalid
91 	 * address, so we calc it by real size.
92 	 */
93 	return (ulong)hdr + hdr->page_size;
94 #else
95 	return hdr->kernel_addr;
96 #endif
97 
98 }
99 
100 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp)
101 {
102 	android_kernel_comp_type = comp;
103 }
104 
105 u32 android_image_get_comp(const struct andr_img_hdr *hdr)
106 {
107 	return android_kernel_comp_type;
108 }
109 
110 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr)
111 {
112 	ulong kaddr = android_image_get_kernel_addr(hdr);
113 	return bootm_parse_comp((const unsigned char *)kaddr);
114 }
115 
116 /**
117  * android_image_get_kernel() - processes kernel part of Android boot images
118  * @hdr:	Pointer to image header, which is at the start
119  *			of the image.
120  * @verify:	Checksum verification flag. Currently unimplemented.
121  * @os_data:	Pointer to a ulong variable, will hold os data start
122  *			address.
123  * @os_len:	Pointer to a ulong variable, will hold os data length.
124  *
125  * This function returns the os image's start address and length. Also,
126  * it appends the kernel command line to the bootargs env variable.
127  *
128  * Return: Zero, os start address and length on success,
129  *		otherwise on failure.
130  */
131 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
132 			     ulong *os_data, ulong *os_len)
133 {
134 	u32 kernel_addr = android_image_get_kernel_addr(hdr);
135 	const char *cmdline = hdr->header_version < 3 ?
136 			      hdr->cmdline : hdr->total_cmdline;
137 	/*
138 	 * Not all Android tools use the id field for signing the image with
139 	 * sha1 (or anything) so we don't check it. It is not obvious that the
140 	 * string is null terminated so we take care of this.
141 	 */
142 	strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
143 	andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
144 	if (strlen(andr_tmp_str))
145 		printf("Android's image name: %s\n", andr_tmp_str);
146 
147 	printf("Kernel load addr 0x%08x size %u KiB\n",
148 	       kernel_addr, 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_v3 */
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 
239 	if (!hdr->ramdisk_size) {
240 		*rd_data = *rd_len = 0;
241 		return -1;
242 	}
243 
244 	/* Have been loaded by android_image_load_separate() on ramdisk_addr_r */
245 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
246 	if (!ramdisk_addr_r) {
247 		printf("No Found Ramdisk Load Address.\n");
248 		return -1;
249 	}
250 
251 	*rd_data = ramdisk_addr_r;
252 	*rd_len = hdr->ramdisk_size;
253 
254 	printf("RAM disk load addr 0x%08lx ", *rd_data);
255 
256 	if (hdr->header_version < 3)
257 		printf("size %u KiB\n", DIV_ROUND_UP(hdr->ramdisk_size, 1024));
258 	else
259 		printf("size: boot %u KiB, vendor-boot %u KiB\n",
260 		       DIV_ROUND_UP(hdr->boot_ramdisk_size, 1024),
261 		       DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024));
262 	return 0;
263 }
264 
265 int android_image_get_fdt(const struct andr_img_hdr *hdr,
266 			      ulong *rd_data)
267 {
268 	ulong fdt_addr_r;
269 
270 	if (!hdr->second_size) {
271 		*rd_data = 0;
272 		return -1;
273 	}
274 
275 	/* Have been loaded by android_image_load_separate() on fdt_addr_r */
276 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
277 	if (!fdt_addr_r) {
278 		printf("No Found FDT Load Address.\n");
279 		return -1;
280 	}
281 
282 	*rd_data = fdt_addr_r;
283 
284 	debug("FDT load addr 0x%08x size %u KiB\n",
285 	      hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
286 
287 	return 0;
288 }
289 
290 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH)
291 static void print_hash(const char *label, u8 *hash, int len)
292 {
293 	int i;
294 
295 	printf("%s:\n    0x", label ? : "Hash");
296 	for (i = 0; i < len; i++)
297 		printf("%02x", hash[i]);
298 	printf("\n");
299 }
300 #endif
301 
302 typedef enum {
303 	IMG_KERNEL,
304 	IMG_RAMDISK,
305 	IMG_SECOND,
306 	IMG_RECOVERY_DTBO,
307 	IMG_RK_DTB,	/* within resource.img in second position */
308 	IMG_DTB,
309 	IMG_VENDOR_RAMDISK,
310 	IMG_MAX,
311 } img_t;
312 
313 static int image_load(img_t img, struct andr_img_hdr *hdr,
314 		      ulong blkstart, void *ram_base,
315 		      struct udevice *crypto)
316 {
317 	struct blk_desc *desc = rockchip_get_bootdev();
318 	disk_partition_t part_vendor_boot;
319 	__maybe_unused u32 sizesz;
320 	ulong pgsz = hdr->page_size;
321 	ulong blksz = desc->blksz;
322 	ulong blkcnt, blkoff;
323 	ulong orgdst = 0;
324 	ulong offset = 0;
325 	ulong extra = 0;
326 	ulong datasz;
327 	void *ramdst;
328 	int ret = 0;
329 
330 	switch (img) {
331 	case IMG_KERNEL:
332 		offset = 0; /* include a page_size(image header) */
333 		blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz);
334 		ramdst = (void *)env_get_ulong("android_addr_r", 16, 0);
335 		datasz = hdr->kernel_size + pgsz;
336 		sizesz = sizeof(hdr->kernel_size);
337 		if (!sysmem_alloc_base(MEM_KERNEL,
338 				(phys_addr_t)ramdst, blkcnt * blksz))
339 			return -ENOMEM;
340 		break;
341 	case IMG_VENDOR_RAMDISK:
342 		if (part_get_info_by_name(desc,
343 					  ANDROID_PARTITION_VENDOR_BOOT,
344 					  &part_vendor_boot) < 0) {
345 			printf("No vendor boot partition\n");
346 			return -ENOENT;
347 		}
348 		/* Always load vendor boot from storage: avb full load boot/recovery */
349 		blkstart = part_vendor_boot.start;
350 		ram_base = 0;
351 
352 		pgsz = hdr->vendor_page_size;
353 		offset = ALIGN(VENDOR_BOOT_HDR_SIZE, pgsz);
354 		blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz);
355 		ramdst = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
356 		datasz = hdr->vendor_ramdisk_size;
357 		sizesz = sizeof(hdr->vendor_ramdisk_size);
358 		/*
359 		 * Add extra memory for generic ramdisk space.
360 		 *
361 		 * In case of unaligned vendor ramdisk size, reserve
362 		 * 1 more blksz.
363 		 */
364 		if (hdr->header_version == 3)
365 			extra = ALIGN(hdr->ramdisk_size, blksz) + blksz;
366 		if (datasz && !sysmem_alloc_base(MEM_RAMDISK,
367 			(phys_addr_t)ramdst, blkcnt * blksz + extra))
368 			return -ENOMEM;
369 		break;
370 	case IMG_RAMDISK:
371 		offset = pgsz + ALIGN(hdr->kernel_size, pgsz);
372 		blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz);
373 		ramdst = (void *)env_get_ulong("ramdisk_addr_r", 16, 0);
374 		/*
375 		 * ramdisk_addr_r:
376 		 *	|----------------|---------|
377 		 *	| vendor-ramdisk | ramdisk |
378 		 *	|----------------|---------|
379 		 */
380 		if (hdr->header_version >= 3) {
381 			ramdst += hdr->vendor_ramdisk_size;
382 			if (!IS_ALIGNED((ulong)ramdst, blksz)) {
383 				orgdst = (ulong)ramdst;
384 				ramdst = (void *)ALIGN(orgdst, blksz);
385 			}
386 		}
387 		datasz = hdr->ramdisk_size;
388 		sizesz = sizeof(hdr->ramdisk_size);
389 		/*
390 		 * skip v3: sysmem has been alloced by vendor ramdisk.
391 		 */
392 		if (hdr->header_version < 3) {
393 			if (datasz && !sysmem_alloc_base(MEM_RAMDISK,
394 				(phys_addr_t)ramdst, blkcnt * blksz))
395 				return -ENOMEM;
396 		}
397 		break;
398 	case IMG_SECOND:
399 		offset = pgsz +
400 			 ALIGN(hdr->kernel_size, pgsz) +
401 			 ALIGN(hdr->ramdisk_size, pgsz);
402 		blkcnt = DIV_ROUND_UP(hdr->second_size, blksz);
403 		datasz = hdr->second_size;
404 		sizesz = sizeof(hdr->second_size);
405 		ramdst = malloc(blkcnt * blksz);
406 		break;
407 	case IMG_RECOVERY_DTBO:
408 		offset = pgsz +
409 			 ALIGN(hdr->kernel_size, pgsz) +
410 			 ALIGN(hdr->ramdisk_size, pgsz) +
411 			 ALIGN(hdr->second_size, pgsz);
412 		blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz);
413 		datasz = hdr->recovery_dtbo_size;
414 		sizesz = sizeof(hdr->recovery_dtbo_size);
415 		ramdst = malloc(blkcnt * blksz);
416 		break;
417 	case IMG_DTB:
418 		offset = pgsz +
419 			 ALIGN(hdr->kernel_size, pgsz) +
420 			 ALIGN(hdr->ramdisk_size, pgsz) +
421 			 ALIGN(hdr->second_size, pgsz) +
422 			 ALIGN(hdr->recovery_dtbo_size, pgsz);
423 		blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz);
424 		datasz = hdr->dtb_size;
425 		sizesz = sizeof(hdr->dtb_size);
426 		ramdst = malloc(blkcnt * blksz);
427 		break;
428 	case IMG_RK_DTB:
429 #ifdef CONFIG_RKIMG_BOOTLOADER
430 		/* No going further, it handles DTBO, HW-ID, etc */
431 		ramdst = (void *)env_get_ulong("fdt_addr_r", 16, 0);
432 		if (gd->fdt_blob != (void *)ramdst)
433 			ret = rockchip_read_dtb_file(ramdst);
434 #endif
435 		return ret < 0 ? ret : 0;
436 	default:
437 		return -EINVAL;
438 	}
439 
440 	if (!ramdst) {
441 		printf("No memory for image(%d)\n", img);
442 		return -ENOMEM;
443 	}
444 
445 	if (!blksz || !datasz)
446 		goto crypto_calc;
447 
448 	/* load */
449 	if (ram_base) {
450 		memcpy(ramdst, (char *)((ulong)ram_base + offset), datasz);
451 	} else {
452 		blkoff = DIV_ROUND_UP(offset, blksz);
453 		ret = blk_dread(desc, blkstart + blkoff, blkcnt, ramdst);
454 		if (ret != blkcnt) {
455 			printf("Failed to read img(%d), ret=%d\n", img, ret);
456 			return -EIO;
457 		}
458 	}
459 
460 	if (orgdst)
461 		memmove((char *)orgdst, ramdst, datasz);
462 
463 crypto_calc:
464 	/* sha1 */
465 #ifdef CONFIG_DM_CRYPTO
466 	if (crypto) {
467 		if (img == IMG_KERNEL) {
468 			ramdst += pgsz;
469 			datasz -= pgsz;
470 		}
471 
472 		crypto_sha_update(crypto, (u32 *)ramdst, datasz);
473 		crypto_sha_update(crypto, (u32 *)&datasz, sizesz);
474 	}
475 #endif
476 
477 	return 0;
478 }
479 
480 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start,
481 			      void *ram_base, struct udevice *crypto)
482 {
483 	/* load, never change order ! */
484 	if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto))
485 		return -1;
486 	if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto))
487 		return -1;
488 	if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto))
489 		return -1;
490 	if (hdr->header_version > 0) {
491 		if (image_load(IMG_RECOVERY_DTBO, hdr, part_start,
492 			       ram_base, crypto))
493 			return -1;
494 	}
495 	if (hdr->header_version > 1) {
496 		if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto))
497 			return -1;
498 	}
499 
500 	return 0;
501 }
502 
503 /*
504  * @ram_base: !NULL means require memcpy for an exist full android image.
505  */
506 static int android_image_separate(struct andr_img_hdr *hdr,
507 				  const disk_partition_t *part,
508 				  void *load_address,
509 				  void *ram_base)
510 {
511 	ulong bstart;
512 	int ret;
513 
514 	if (android_image_check_header(hdr)) {
515 		printf("Bad android image header\n");
516 		return -EINVAL;
517 	}
518 
519 	/* set for image_load(IMG_KERNEL, ...) */
520 	env_set_hex("android_addr_r", (ulong)load_address);
521 	bstart = part ? part->start : 0;
522 
523 	/*
524 	 * 1. Load images to their individual target ram position
525 	 *    in order to disable fdt/ramdisk relocation.
526 	 */
527 
528 	/* load rk-kernel.dtb alone */
529 	if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL))
530 		return -1;
531 
532 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH)
533 	if (hdr->header_version < 3) {
534 		struct udevice *dev;
535 		sha_context ctx;
536 		uchar hash[20];
537 
538 		ctx.length = 0;
539 		ctx.algo = CRYPTO_SHA1;
540 		dev = crypto_get_device(ctx.algo);
541 		if (!dev) {
542 			printf("Can't find crypto device for SHA1\n");
543 			return -ENODEV;
544 		}
545 
546 		/* v1 & v2: requires total length before sha init */
547 		ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) +
548 			      hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
549 			      hdr->second_size + sizeof(hdr->second_size);
550 		if (hdr->header_version > 0)
551 			ctx.length += hdr->recovery_dtbo_size +
552 						sizeof(hdr->recovery_dtbo_size);
553 		if (hdr->header_version > 1)
554 			ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size);
555 
556 		crypto_sha_init(dev, &ctx);
557 		ret = images_load_verify(hdr, bstart, ram_base, dev);
558 		if (ret)
559 			return ret;
560 		crypto_sha_final(dev, &ctx, hash);
561 
562 		if (memcmp(hash, hdr->id, 20)) {
563 			print_hash("Hash from header", (u8 *)hdr->id, 20);
564 			print_hash("Hash real", (u8 *)hash, 20);
565 			return -EBADFD;
566 		} else {
567 			printf("ANDROID: Hash OK\n");
568 		}
569 	} else
570 #endif
571 	{
572 		ret = images_load_verify(hdr, bstart, ram_base, NULL);
573 		if (ret)
574 			return ret;
575 	}
576 
577 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
578 	env_set("bootm-no-reloc", "y");
579 
580 	return 0;
581 }
582 
583 static int android_image_separate_v3(struct andr_img_hdr *hdr,
584 				     const disk_partition_t *part,
585 				     void *load_address, void *ram_base)
586 {
587 	ulong bstart;
588 
589 	if (android_image_check_header(hdr)) {
590 		printf("Bad android image header\n");
591 		return -EINVAL;
592 	}
593 
594 	/* set for image_load(IMG_KERNEL, ...) */
595 	env_set_hex("android_addr_r", (ulong)load_address);
596 	bstart = part ? part->start : 0;
597 
598 	/*
599 	 * 1. Load images to their individual target ram position
600 	 *    in order to disable fdt/ramdisk relocation.
601 	 */
602 	if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
603 		return -1;
604 	if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
605 		return -1;
606 	if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL))
607 		return -1;
608 	if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
609 		return -1;
610 
611 	/*
612 	 * Copy the populated hdr to load address after image_load(IMG_KERNEL)
613 	 *
614 	 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v3 while
615 	 * vendor_boot_img_hdr_v3 is not included, so fix it here.
616 	 */
617 	memcpy((char *)load_address, hdr, hdr->page_size);
618 
619 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
620 	env_set("bootm-no-reloc", "y");
621 
622 	return 0;
623 }
624 
625 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp)
626 {
627 	ulong kernel_addr_c;
628 	ulong load_addr = 0;
629 
630 	kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
631 
632 #ifdef CONFIG_ARM64
633 	/*
634 	 * On 64-bit kernel, assuming use IMAGE by default.
635 	 *
636 	 * kernel_addr_c is for LZ4-IMAGE but maybe not defined.
637 	 * kernel_addr_r is for IMAGE.
638 	 */
639 	if (comp != IH_COMP_NONE) {
640 		ulong comp_addr;
641 
642 		if (kernel_addr_c) {
643 			comp_addr = kernel_addr_c;
644 		} else {
645 			printf("Warn: No \"kernel_addr_c\"\n");
646 			comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */
647 			env_set_hex("kernel_addr_c", comp_addr);
648 		}
649 
650 		load_addr = comp_addr - hdr->page_size;
651 	}
652 #else
653 	/*
654 	 * On 32-bit kernel:
655 	 *
656 	 * The input load_addr is from env value: "kernel_addr_r", it has
657 	 * different role depends on whether kernel_addr_c is defined:
658 	 *
659 	 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined.
660 	 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined.
661 	 */
662 	if (comp == IH_COMP_NONE) {
663 		if (kernel_addr_c) {
664 			/* input load_addr is for Image, nothing to do */
665 		} else {
666 			/* input load_addr is for lz4/zImage, set default addr for Image */
667 			load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000;
668 			env_set_hex("kernel_addr_r", load_addr);
669 
670 			load_addr -= hdr->page_size;
671 		}
672 	} else {
673 		if (kernel_addr_c) {
674 			/* input load_addr is for Image, so use another for lz4/zImage */
675 			load_addr = kernel_addr_c - hdr->page_size;
676 		} else {
677 			/* input load_addr is for lz4/zImage, nothing to do */
678 		}
679 	}
680 #endif
681 
682 	return load_addr;
683 }
684 
685 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp)
686 {
687 	ulong kernel_addr_r;
688 
689 	env_set_ulong("os_comp", comp);
690 
691 	/* zImage handles decompress itself */
692 	if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
693 		kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000);
694 		android_image_set_kload(hdr, kernel_addr_r);
695 		android_image_set_comp(hdr, comp);
696 	} else {
697 		android_image_set_comp(hdr, IH_COMP_NONE);
698 	}
699 }
700 
701 static int android_image_load_separate(struct andr_img_hdr *hdr,
702 				       const disk_partition_t *part,
703 				       void *load_addr)
704 {
705 	if (hdr->header_version < 3)
706 		return android_image_separate(hdr, part, load_addr, NULL);
707 	else
708 		return android_image_separate_v3(hdr, part, load_addr, NULL);
709 }
710 
711 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr)
712 {
713 	ulong comp_addr;
714 	int comp;
715 
716 	comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
717 	comp_addr = android_image_get_comp_addr(hdr, comp);
718 
719 	/* non-compressed image: already in-place */
720 	if ((ulong)hdr == *load_addr)
721 		return 0;
722 
723 	/* compressed image */
724 	if (comp_addr) {
725 		*load_addr = comp_addr;
726 		if ((ulong)hdr == comp_addr)	/* already in-place */
727 			return 0;
728 	}
729 
730 	/*
731 	 * The most possible reason to arrive here is:
732 	 *
733 	 * VBoot=1 and AVB load full partition to a temp memory buffer, now we
734 	 * separate(memcpy) subimages from boot.img to where they should be.
735 	 */
736 	if (hdr->header_version < 3) {
737 		if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr))
738 			return -1;
739 	} else {
740 		if (android_image_separate_v3(hdr, NULL, (void *)(*load_addr), hdr))
741 			return -1;
742 	}
743 
744 	android_image_set_decomp((void *)(*load_addr), comp);
745 
746 	return 0;
747 }
748 
749 long android_image_load(struct blk_desc *dev_desc,
750 			const disk_partition_t *part_info,
751 			unsigned long load_address,
752 			unsigned long max_size) {
753 	struct andr_img_hdr *hdr;
754 	ulong comp_addr;
755 	int comp, ret;
756 	int blk_off;
757 
758 	if (max_size < part_info->blksz)
759 		return -1;
760 
761 	hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info);
762 	if (!hdr) {
763 		printf("No valid android hdr\n");
764 		return -1;
765 	}
766 
767 	/*
768 	 * create the layout:
769 	 *
770 	 * |<- page_size ->|1-blk |
771 	 * |-----|---------|------|-----|
772 	 * | hdr |   ...   |   kernel   |
773 	 * |-----|----- ---|------------|
774 	 *
775 	 * Alloc page_size and 1 more blk for reading kernel image to
776 	 * get it's compression type, then fill the android hdr what
777 	 * we have populated before.
778 	 *
779 	 * Why? see: android_image_get_kernel_addr().
780 	 */
781 	blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz);
782 	hdr = (struct andr_img_hdr *)
783 			realloc(hdr, (blk_off + 1) * dev_desc->blksz);
784 	if (!hdr)
785 		return -1;
786 
787 	if (blk_dread(dev_desc, part_info->start + blk_off, 1,
788 		      (char *)hdr + hdr->page_size) != 1) {
789 		free(hdr);
790 		return -1;
791 	}
792 
793 	/* Changed to compressed address ? */
794 	comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size);
795 	comp_addr = android_image_get_comp_addr(hdr, comp);
796 	if (comp_addr)
797 		load_address = comp_addr;
798 	else
799 		load_address -= hdr->page_size;
800 
801 	ret = android_image_load_separate(hdr, part_info, (void *)load_address);
802 	if (ret) {
803 		printf("Failed to load android image\n");
804 		goto fail;
805 	}
806 	android_image_set_decomp((void *)load_address, comp);
807 
808 	debug("Loading Android Image to 0x%08lx\n", load_address);
809 
810 	free(hdr);
811 	return load_address;
812 
813 fail:
814 	free(hdr);
815 	return -1;
816 }
817 
818 static struct andr_img_hdr *
819 extract_boot_image_v012_header(struct blk_desc *dev_desc,
820 			       const disk_partition_t *boot_img)
821 {
822 	struct andr_img_hdr *hdr;
823 	long blk_cnt, blks_read;
824 
825 	blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz);
826 	hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz);
827 
828 	if (!blk_cnt || !hdr)
829 		return NULL;
830 
831 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr);
832 	if (blks_read != blk_cnt) {
833 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
834 		      blk_cnt, blks_read);
835 		return NULL;
836 	}
837 
838 	if (android_image_check_header((void *)hdr)) {
839 		printf("boot header magic is invalid.\n");
840 		return NULL;
841 	}
842 
843 	if (hdr->page_size < sizeof(*hdr)) {
844 		printf("android hdr is over size\n");
845 		return NULL;
846 	}
847 
848 	return hdr;
849 }
850 
851 static struct boot_img_hdr_v3 *
852 extract_boot_image_v3_header(struct blk_desc *dev_desc,
853 			     const disk_partition_t *boot_img)
854 {
855 	struct boot_img_hdr_v3 *boot_hdr;
856 	long blk_cnt, blks_read;
857 
858 	blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v3), dev_desc->blksz);
859 	boot_hdr = (struct boot_img_hdr_v3 *)malloc(blk_cnt * dev_desc->blksz);
860 
861 	if (!blk_cnt || !boot_hdr)
862 		return NULL;
863 
864 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr);
865 	if (blks_read != blk_cnt) {
866 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
867 		      blk_cnt, blks_read);
868 		return NULL;
869 	}
870 
871 	if (android_image_check_header((void *)boot_hdr)) {
872 		printf("boot header magic is invalid.\n");
873 		return NULL;
874 	}
875 
876 	if (boot_hdr->header_version != 3) {
877 		printf("boot header is not v3.\n");
878 		return NULL;
879 	}
880 
881 	return boot_hdr;
882 }
883 
884 static struct vendor_boot_img_hdr_v3 *
885 extract_vendor_boot_image_v3_header(struct blk_desc *dev_desc,
886 				    const disk_partition_t *part_vendor_boot)
887 {
888 	struct vendor_boot_img_hdr_v3 *vboot_hdr;
889 	long blk_cnt, blks_read;
890 
891 	blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v3),
892 				part_vendor_boot->blksz);
893 	vboot_hdr = (struct vendor_boot_img_hdr_v3 *)
894 				malloc(blk_cnt * part_vendor_boot->blksz);
895 
896 	if (!blk_cnt || !vboot_hdr)
897 		return NULL;
898 
899 	blks_read = blk_dread(dev_desc, part_vendor_boot->start,
900 			      blk_cnt, vboot_hdr);
901 	if (blks_read != blk_cnt) {
902 		debug("vboot img header blk cnt is %ld and blks read is %ld\n",
903 		      blk_cnt, blks_read);
904 		return NULL;
905 	}
906 
907 	if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic,
908 		    VENDOR_BOOT_MAGIC_SIZE)) {
909 		printf("vendor boot header is invalid.\n");
910 		return NULL;
911 	}
912 
913 	if (vboot_hdr->header_version != 3) {
914 		printf("vendor boot header is not v3.\n");
915 		return NULL;
916 	}
917 
918 	return vboot_hdr;
919 }
920 
921 static int populate_boot_info(const struct boot_img_hdr_v3 *boot_hdr,
922 			      const struct vendor_boot_img_hdr_v3 *vendor_hdr,
923 			      struct andr_img_hdr *hdr)
924 {
925 	memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE);
926 	memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE);
927 
928 	hdr->kernel_size = boot_hdr->kernel_size;
929 	/* don't use vendor_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */
930 	hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR;
931 	/* generic ramdisk: immediately following the vendor ramdisk */
932 	hdr->boot_ramdisk_size = boot_hdr->ramdisk_size;
933 	hdr->ramdisk_size = boot_hdr->ramdisk_size +
934 				vendor_hdr->vendor_ramdisk_size;
935 	/* actually, useless */
936 	hdr->ramdisk_addr = vendor_hdr->ramdisk_addr +
937 				vendor_hdr->vendor_ramdisk_size;
938 	/* removed in v3 */
939 	hdr->second_size = 0;
940 	hdr->second_addr = 0;
941 
942 	hdr->tags_addr = vendor_hdr->tags_addr;
943 
944 	/* fixed in v3 */
945 	hdr->page_size = 4096;
946 	hdr->header_version = boot_hdr->header_version;
947 	hdr->os_version = boot_hdr->os_version;
948 
949 	memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE);
950 	strncpy(hdr->name, (const char *)vendor_hdr->name, ANDR_BOOT_NAME_SIZE);
951 
952 	/* removed in v3 */
953 	memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE);
954 	memset(hdr->id, 0, 32);
955 	memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE);
956 	hdr->recovery_dtbo_size = 0;
957 	hdr->recovery_dtbo_offset = 0;
958 
959 	hdr->header_size = boot_hdr->header_size;
960 	hdr->dtb_size = vendor_hdr->dtb_size;
961 	hdr->dtb_addr = vendor_hdr->dtb_addr;
962 
963 	/* boot_img_hdr_v3 fields */
964 	hdr->vendor_ramdisk_size = vendor_hdr->vendor_ramdisk_size;
965 	hdr->vendor_page_size = vendor_hdr->page_size;
966 	hdr->vendor_header_version = vendor_hdr->header_version;
967 	hdr->vendor_header_size = vendor_hdr->header_size;
968 
969 	hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE);
970 	if (!hdr->total_cmdline)
971 		return -ENOMEM;
972 	strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline,
973 		sizeof(boot_hdr->cmdline));
974 	strncat(hdr->total_cmdline, " ", 1);
975 	strncat(hdr->total_cmdline, (const char *)vendor_hdr->cmdline,
976 		sizeof(vendor_hdr->cmdline));
977 
978 	if (hdr->page_size < sizeof(*hdr)) {
979 		printf("android hdr is over size\n");
980 		return -EINVAL;
981 	}
982 
983 	return 0;
984 }
985 
986 /*
987  * The possible cases of boot.img + recovery.img:
988  *
989  * [N]: 0, 1, 2
990  * [M]: 0, 1, 2, 3
991  *
992  * |--------------------|---------------------|
993  * |   boot.img         |    recovery.img     |
994  * |--------------------|---------------------|
995  * | boot_img_hdr_v[N]  |  boot_img_hdr_v[N]  | <= if A/B is not required
996  * |--------------------|---------------------|
997  * | boot_img_hdr_v3    |  boot_img_hdr_v2    | <= if A/B is not required
998  * |------------------------------------------|
999  * | boot_img_hdr_v[M], no recovery.img       | <= if A/B is required
1000  * |------------------------------------------|
1001  */
1002 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc,
1003 					   disk_partition_t *part_boot)
1004 {
1005 	disk_partition_t part_vendor_boot;
1006 	struct vendor_boot_img_hdr_v3 *vboot_hdr;
1007 	struct boot_img_hdr_v3 *boot_hdr;
1008 	struct andr_img_hdr *andr_hdr;
1009 	int header_version;
1010 
1011 	if (!dev_desc || !part_boot)
1012 		return NULL;
1013 
1014 	andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz);
1015 	if (!andr_hdr)
1016 		return NULL;
1017 
1018 	if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) {
1019 		free(andr_hdr);
1020 		return NULL;
1021 	}
1022 
1023 	if (android_image_check_header(andr_hdr)) {
1024 		free(andr_hdr);
1025 		return NULL;
1026 	}
1027 
1028 	header_version = andr_hdr->header_version;
1029 	free(andr_hdr);
1030 
1031 	if (header_version < 3) {
1032 		return extract_boot_image_v012_header(dev_desc, part_boot);
1033 	} else {
1034 		if (part_get_info_by_name(dev_desc,
1035 					  ANDROID_PARTITION_VENDOR_BOOT,
1036 					  &part_vendor_boot) < 0) {
1037 			printf("No vendor boot partition\n");
1038 			return NULL;
1039 		}
1040 		boot_hdr = extract_boot_image_v3_header(dev_desc, part_boot);
1041 		vboot_hdr = extract_vendor_boot_image_v3_header(dev_desc,
1042 							&part_vendor_boot);
1043 		if (!boot_hdr || !vboot_hdr)
1044 			goto image_load_exit;
1045 
1046 		andr_hdr = (struct andr_img_hdr *)
1047 				malloc(sizeof(struct andr_img_hdr));
1048 		if (!andr_hdr) {
1049 			printf("No memory for andr hdr\n");
1050 			goto image_load_exit;
1051 		}
1052 
1053 		if (populate_boot_info(boot_hdr, vboot_hdr, andr_hdr)) {
1054 			printf("populate boot info failed\n");
1055 			goto image_load_exit;
1056 		}
1057 
1058 		free(boot_hdr);
1059 		free(vboot_hdr);
1060 
1061 		return andr_hdr;
1062 
1063 image_load_exit:
1064 		free(boot_hdr);
1065 		free(vboot_hdr);
1066 
1067 		return NULL;
1068 	}
1069 
1070 	return NULL;
1071 }
1072 
1073 #if !defined(CONFIG_SPL_BUILD)
1074 /**
1075  * android_print_contents - prints out the contents of the Android format image
1076  * @hdr: pointer to the Android format image header
1077  *
1078  * android_print_contents() formats a multi line Android image contents
1079  * description.
1080  * The routine prints out Android image properties
1081  *
1082  * returns:
1083  *     no returned results
1084  */
1085 void android_print_contents(const struct andr_img_hdr *hdr)
1086 {
1087 	const char * const p = IMAGE_INDENT_STRING;
1088 	/* os_version = ver << 11 | lvl */
1089 	u32 os_ver = hdr->os_version >> 11;
1090 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
1091 	u32 header_version = hdr->header_version;
1092 
1093 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
1094 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
1095 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
1096 	printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
1097 	printf("%ssecond size:      %x\n", p, hdr->second_size);
1098 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
1099 	printf("%stags address:     %x\n", p, hdr->tags_addr);
1100 	printf("%spage size:        %x\n", p, hdr->page_size);
1101 	printf("%sheader_version:   %x\n", p, header_version);
1102 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
1103 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
1104 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
1105 	       p, hdr->os_version,
1106 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
1107 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
1108 	printf("%sname:             %s\n", p, hdr->name);
1109 	printf("%scmdline:          %s\n", p, hdr->cmdline);
1110 
1111 	if (header_version == 1 || header_version == 2) {
1112 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
1113 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
1114 		printf("%sheader size:           %x\n", p, hdr->header_size);
1115 	}
1116 
1117 	if (header_version == 2 || header_version == 3) {
1118 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
1119 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
1120 	}
1121 
1122 	if (header_version == 3) {
1123 		printf("%scmdline:               %s\n", p, hdr->total_cmdline);
1124 		printf("%svendor ramdisk size:   %x\n", p, hdr->vendor_ramdisk_size);
1125 		printf("%svendor page size:      %x\n", p, hdr->vendor_page_size);
1126 		printf("%svendor header version: %d\n", p, hdr->vendor_header_version);
1127 		printf("%svendor header size:    %x\n", p, hdr->vendor_header_size);
1128 	}
1129 }
1130 #endif
1131