xref: /rk3399_rockchip-uboot/common/image-android.c (revision c4e6abcd5e33b08affcf6b9eb33af1294e1443a5)
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 /*
481  * @ram_base: !NULL means require memcpy for an exist full android image.
482  */
483 static int android_image_separate(struct andr_img_hdr *hdr,
484 				  const disk_partition_t *part,
485 				  void *load_address,
486 				  void *ram_base)
487 {
488 	ulong bstart;
489 
490 	if (android_image_check_header(hdr)) {
491 		printf("Bad android image header\n");
492 		return -EINVAL;
493 	}
494 
495 	/* set for image_load(IMG_KERNEL, ...) */
496 	env_set_hex("android_addr_r", (ulong)load_address);
497 	bstart = part ? part->start : 0;
498 
499 	/*
500 	 * 1. Load images to their individual target ram position
501 	 *    in order to disable fdt/ramdisk relocation.
502 	 */
503 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH)
504 	struct udevice *dev;
505 	sha_context ctx;
506 	uchar hash[20];
507 
508 	ctx.length = 0;
509 	ctx.algo = CRYPTO_SHA1;
510 	dev = crypto_get_device(ctx.algo);
511 	if (!dev) {
512 		printf("Can't find crypto device for SHA1 capability\n");
513 		return -ENODEV;
514 	}
515 
516   #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
517 	/* v1: requires total length before sha init */
518 	ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) +
519 		      hdr->ramdisk_size + sizeof(hdr->ramdisk_size) +
520 		      hdr->second_size + sizeof(hdr->second_size);
521 	if (hdr->header_version > 0)
522 		ctx.length += hdr->recovery_dtbo_size +
523 					sizeof(hdr->recovery_dtbo_size);
524 	if (hdr->header_version > 1)
525 		ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size);
526   #endif
527 	crypto_sha_init(dev, &ctx);
528 
529 	/* load, never change order ! */
530 	if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
531 		return -1;
532 	if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, dev))
533 		return -1;
534 	if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, dev))
535 		return -1;
536 	if (image_load(IMG_SECOND,  hdr, bstart, ram_base, dev))
537 		return -1;
538 	if (hdr->header_version > 0) {
539 		if (image_load(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, dev))
540 			return -1;
541 	}
542 	if (hdr->header_version > 1) {
543 		if (image_load(IMG_DTB, hdr, bstart, ram_base, dev))
544 			return -1;
545 	}
546 
547 	crypto_sha_final(dev, &ctx, hash);
548 	if (memcmp(hash, hdr->id, 20)) {
549 		print_hash("Hash from header", (u8 *)hdr->id, 20);
550 		print_hash("Hash real", (u8 *)hash, 20);
551 		return -EBADFD;
552 	} else {
553 		printf("Image hash OK\n");
554 	}
555 
556 #else /* !(CONFIG_DM_CRYPTO && CONFIG_ANDROID_BOOT_IMAGE_HASH) */
557 	if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
558 		return -1;
559 	if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
560 		return -1;
561 	if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
562 		return -1;
563 	if (image_load(IMG_SECOND,  hdr, bstart, ram_base, NULL))
564 		return -1;
565 	if (hdr->header_version > 0) {
566 		if (image_load(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, NULL))
567 			return -1;
568 	}
569 	if (hdr->header_version > 1) {
570 		if (image_load(IMG_DTB, hdr, bstart, ram_base, NULL))
571 			return -1;
572 	}
573 #endif
574 
575 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
576 	env_set("bootm-no-reloc", "y");
577 
578 	return 0;
579 }
580 
581 static int android_image_separate_v3(struct andr_img_hdr *hdr,
582 				     const disk_partition_t *part,
583 				     void *load_address, void *ram_base)
584 {
585 	ulong bstart;
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 	if (image_load(IMG_RK_DTB,  hdr, bstart, ram_base, NULL))
601 		return -1;
602 	if (image_load(IMG_KERNEL,  hdr, bstart, ram_base, NULL))
603 		return -1;
604 	if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL))
605 		return -1;
606 	if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL))
607 		return -1;
608 
609 	/*
610 	 * Copy the populated hdr to load address after image_load(IMG_KERNEL)
611 	 *
612 	 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v3 while
613 	 * vendor_boot_img_hdr_v3 is not included, so fix it here.
614 	 */
615 	memcpy((char *)load_address, hdr, hdr->page_size);
616 
617 	/* 2. Disable fdt/ramdisk relocation, it saves boot time */
618 	env_set("bootm-no-reloc", "y");
619 
620 	return 0;
621 }
622 
623 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp)
624 {
625 	ulong kernel_addr_c;
626 	ulong load_addr = 0;
627 
628 	kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
629 
630 #ifdef CONFIG_ARM64
631 	/*
632 	 * On 64-bit kernel, assuming use IMAGE by default.
633 	 *
634 	 * kernel_addr_c is for LZ4-IMAGE but maybe not defined.
635 	 * kernel_addr_r is for IMAGE.
636 	 */
637 	if (comp != IH_COMP_NONE) {
638 		ulong comp_addr;
639 
640 		if (kernel_addr_c) {
641 			comp_addr = kernel_addr_c;
642 		} else {
643 			printf("Warn: No \"kernel_addr_c\"\n");
644 			comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */
645 			env_set_hex("kernel_addr_c", comp_addr);
646 		}
647 
648 		load_addr = comp_addr - hdr->page_size;
649 	}
650 #else
651 	/*
652 	 * On 32-bit kernel:
653 	 *
654 	 * The input load_addr is from env value: "kernel_addr_r", it has
655 	 * different role depends on whether kernel_addr_c is defined:
656 	 *
657 	 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined.
658 	 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined.
659 	 */
660 	if (comp == IH_COMP_NONE) {
661 		if (kernel_addr_c) {
662 			/* input load_addr is for Image, nothing to do */
663 		} else {
664 			/* input load_addr is for lz4/zImage, set default addr for Image */
665 			load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000;
666 			env_set_hex("kernel_addr_r", load_addr);
667 
668 			load_addr -= hdr->page_size;
669 		}
670 	} else {
671 		if (kernel_addr_c) {
672 			/* input load_addr is for Image, so use another for lz4/zImage */
673 			load_addr = kernel_addr_c - hdr->page_size;
674 		} else {
675 			/* input load_addr is for lz4/zImage, nothing to do */
676 		}
677 	}
678 #endif
679 
680 	return load_addr;
681 }
682 
683 /*
684  * 'boot_android' cmd use "kernel_addr_r" as default load address !
685  * We update it according to compress type and "kernel_addr_c/r".
686  */
687 int android_image_parse_comp(struct andr_img_hdr *hdr, ulong *load_addr)
688 {
689 	ulong new_load_addr;
690 	int comp;
691 
692 	comp = android_image_parse_kernel_comp(hdr);
693 	env_set_ulong("os_comp", comp);
694 
695 	new_load_addr = android_image_get_comp_addr(hdr, comp);
696 	if (new_load_addr != 0)
697 		*load_addr = new_load_addr;
698 
699 	return comp;
700 }
701 
702 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp)
703 {
704 	ulong kernel_addr_r;
705 
706 	/* zImage handles decompress itself */
707 	if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
708 		kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000);
709 		android_image_set_kload(hdr, kernel_addr_r);
710 		android_image_set_comp(hdr, comp);
711 	} else {
712 		android_image_set_comp(hdr, IH_COMP_NONE);
713 	}
714 }
715 
716 static int android_image_load_separate(struct andr_img_hdr *hdr,
717 				       const disk_partition_t *part,
718 				       void *load_addr)
719 {
720 	if (hdr->header_version < 3)
721 		return android_image_separate(hdr, part, load_addr, NULL);
722 	else
723 		return android_image_separate_v3(hdr, part, load_addr, NULL);
724 }
725 
726 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr)
727 {
728 	ulong comp_addr = *load_addr;
729 	int comp;
730 
731 	comp = android_image_parse_comp(hdr, &comp_addr);
732 	if (comp_addr == (ulong)hdr)
733 		return 0;
734 
735 	if (hdr->header_version < 3) {
736 		if (android_image_separate(hdr, NULL, (void *)comp_addr, hdr))
737 			return -1;
738 	} else {
739 		if (android_image_separate_v3(hdr, NULL, (void *)comp_addr, hdr))
740 			return -1;
741 	}
742 
743 	*load_addr = comp_addr;
744 	android_image_set_decomp((void *)comp_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 	int comp, ret;
755 	int blk_off;
756 
757 	if (max_size < part_info->blksz)
758 		return -1;
759 
760 	hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info);
761 	if (!hdr) {
762 		printf("No valid android hdr\n");
763 		return -1;
764 	}
765 
766 	/*
767 	 * create the layout:
768 	 *
769 	 * |<- page_size ->|1-blk |
770 	 * |-----|---------|------|-----|
771 	 * | hdr |   ...   |   kernel   |
772 	 * |-----|----- ---|------------|
773 	 *
774 	 * Alloc page_size and 1 more blk for reading kernel image to
775 	 * get it's compression type, then fill the android hdr what
776 	 * we have populated before.
777 	 *
778 	 * Why? see: android_image_get_kernel_addr().
779 	 */
780 	blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz);
781 	hdr = (struct andr_img_hdr *)
782 			realloc(hdr, (blk_off + 1) * dev_desc->blksz);
783 	if (!hdr)
784 		return -1;
785 
786 	if (blk_dread(dev_desc, part_info->start + blk_off, 1,
787 		      (char *)hdr + hdr->page_size) != 1) {
788 		free(hdr);
789 		return -1;
790 	}
791 
792 	/* Make kernel start address at load_address */
793 	load_address -= hdr->page_size;
794 
795 	/* Let's load kernel now ! */
796 	comp = android_image_parse_comp(hdr, &load_address);
797 	ret = android_image_load_separate(hdr, part_info, (void *)load_address);
798 	if (ret) {
799 		printf("Failed to load android image\n");
800 		goto fail;
801 	}
802 	android_image_set_decomp((void *)load_address, comp);
803 
804 	debug("Loading Android Image to 0x%08lx\n", load_address);
805 
806 	free(hdr);
807 	return load_address;
808 
809 fail:
810 	free(hdr);
811 	return -1;
812 }
813 
814 static struct andr_img_hdr *
815 extract_boot_image_v012_header(struct blk_desc *dev_desc,
816 			       const disk_partition_t *boot_img)
817 {
818 	struct andr_img_hdr *hdr;
819 	long blk_cnt, blks_read;
820 
821 	blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz);
822 	hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz);
823 
824 	if (!blk_cnt || !hdr)
825 		return NULL;
826 
827 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr);
828 	if (blks_read != blk_cnt) {
829 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
830 		      blk_cnt, blks_read);
831 		return NULL;
832 	}
833 
834 	if (android_image_check_header((void *)hdr)) {
835 		printf("boot header magic is invalid.\n");
836 		return NULL;
837 	}
838 
839 	if (hdr->page_size < sizeof(*hdr)) {
840 		printf("android hdr is over size\n");
841 		return NULL;
842 	}
843 
844 	return hdr;
845 }
846 
847 static struct boot_img_hdr_v3 *
848 extract_boot_image_v3_header(struct blk_desc *dev_desc,
849 			     const disk_partition_t *boot_img)
850 {
851 	struct boot_img_hdr_v3 *boot_hdr;
852 	long blk_cnt, blks_read;
853 
854 	blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v3), dev_desc->blksz);
855 	boot_hdr = (struct boot_img_hdr_v3 *)malloc(blk_cnt * dev_desc->blksz);
856 
857 	if (!blk_cnt || !boot_hdr)
858 		return NULL;
859 
860 	blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr);
861 	if (blks_read != blk_cnt) {
862 		debug("boot img header blk cnt is %ld and blks read is %ld\n",
863 		      blk_cnt, blks_read);
864 		return NULL;
865 	}
866 
867 	if (android_image_check_header((void *)boot_hdr)) {
868 		printf("boot header magic is invalid.\n");
869 		return NULL;
870 	}
871 
872 	if (boot_hdr->header_version != 3) {
873 		printf("boot header is not v3.\n");
874 		return NULL;
875 	}
876 
877 	return boot_hdr;
878 }
879 
880 static struct vendor_boot_img_hdr_v3 *
881 extract_vendor_boot_image_v3_header(struct blk_desc *dev_desc,
882 				    const disk_partition_t *part_vendor_boot)
883 {
884 	struct vendor_boot_img_hdr_v3 *vboot_hdr;
885 	long blk_cnt, blks_read;
886 
887 	blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v3),
888 				part_vendor_boot->blksz);
889 	vboot_hdr = (struct vendor_boot_img_hdr_v3 *)
890 				malloc(blk_cnt * part_vendor_boot->blksz);
891 
892 	if (!blk_cnt || !vboot_hdr)
893 		return NULL;
894 
895 	blks_read = blk_dread(dev_desc, part_vendor_boot->start,
896 			      blk_cnt, vboot_hdr);
897 	if (blks_read != blk_cnt) {
898 		debug("vboot img header blk cnt is %ld and blks read is %ld\n",
899 		      blk_cnt, blks_read);
900 		return NULL;
901 	}
902 
903 	if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic,
904 		    VENDOR_BOOT_MAGIC_SIZE)) {
905 		printf("vendor boot header is invalid.\n");
906 		return NULL;
907 	}
908 
909 	if (vboot_hdr->header_version != 3) {
910 		printf("vendor boot header is not v3.\n");
911 		return NULL;
912 	}
913 
914 	return vboot_hdr;
915 }
916 
917 static int populate_boot_info(const struct boot_img_hdr_v3 *boot_hdr,
918 			      const struct vendor_boot_img_hdr_v3 *vendor_hdr,
919 			      struct andr_img_hdr *hdr)
920 {
921 	memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE);
922 	memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE);
923 
924 	hdr->kernel_size = boot_hdr->kernel_size;
925 	/* don't use vendor_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */
926 	hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR;
927 	/* generic ramdisk: immediately following the vendor ramdisk */
928 	hdr->boot_ramdisk_size = boot_hdr->ramdisk_size;
929 	hdr->ramdisk_size = boot_hdr->ramdisk_size +
930 				vendor_hdr->vendor_ramdisk_size;
931 	/* actually, useless */
932 	hdr->ramdisk_addr = vendor_hdr->ramdisk_addr +
933 				vendor_hdr->vendor_ramdisk_size;
934 	/* removed in v3 */
935 	hdr->second_size = 0;
936 	hdr->second_addr = 0;
937 
938 	hdr->tags_addr = vendor_hdr->tags_addr;
939 
940 	/* fixed in v3 */
941 	hdr->page_size = 4096;
942 	hdr->header_version = boot_hdr->header_version;
943 	hdr->os_version = boot_hdr->os_version;
944 
945 	memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE);
946 	strncpy(hdr->name, (const char *)vendor_hdr->name, ANDR_BOOT_NAME_SIZE);
947 
948 	/* removed in v3 */
949 	memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE);
950 	memset(hdr->id, 0, 32);
951 	memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE);
952 	hdr->recovery_dtbo_size = 0;
953 	hdr->recovery_dtbo_offset = 0;
954 
955 	hdr->header_size = boot_hdr->header_size;
956 	hdr->dtb_size = vendor_hdr->dtb_size;
957 	hdr->dtb_addr = vendor_hdr->dtb_addr;
958 
959 	/* boot_img_hdr_v3 fields */
960 	hdr->vendor_ramdisk_size = vendor_hdr->vendor_ramdisk_size;
961 	hdr->vendor_page_size = vendor_hdr->page_size;
962 	hdr->vendor_header_version = vendor_hdr->header_version;
963 	hdr->vendor_header_size = vendor_hdr->header_size;
964 
965 	hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE);
966 	if (!hdr->total_cmdline)
967 		return -ENOMEM;
968 	strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline,
969 		sizeof(boot_hdr->cmdline));
970 	strncat(hdr->total_cmdline, " ", 1);
971 	strncat(hdr->total_cmdline, (const char *)vendor_hdr->cmdline,
972 		sizeof(vendor_hdr->cmdline));
973 
974 	if (hdr->page_size < sizeof(*hdr)) {
975 		printf("android hdr is over size\n");
976 		return -EINVAL;
977 	}
978 
979 	return 0;
980 }
981 
982 /*
983  * The possible cases of boot.img + recovery.img:
984  *
985  * [N]: 0, 1, 2
986  * [M]: 0, 1, 2, 3
987  *
988  * |--------------------|---------------------|
989  * |   boot.img         |    recovery.img     |
990  * |--------------------|---------------------|
991  * | boot_img_hdr_v[N]  |  boot_img_hdr_v[N]  | <= if A/B is not required
992  * |--------------------|---------------------|
993  * | boot_img_hdr_v3    |  boot_img_hdr_v2    | <= if A/B is not required
994  * |------------------------------------------|
995  * | boot_img_hdr_v[M], no recovery.img       | <= if A/B is required
996  * |------------------------------------------|
997  */
998 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc,
999 					   disk_partition_t *part_boot)
1000 {
1001 	disk_partition_t part_vendor_boot;
1002 	struct vendor_boot_img_hdr_v3 *vboot_hdr;
1003 	struct boot_img_hdr_v3 *boot_hdr;
1004 	struct andr_img_hdr *andr_hdr;
1005 	int header_version;
1006 
1007 	if (!dev_desc || !part_boot)
1008 		return NULL;
1009 
1010 	andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz);
1011 	if (!andr_hdr)
1012 		return NULL;
1013 
1014 	if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) {
1015 		free(andr_hdr);
1016 		return NULL;
1017 	}
1018 
1019 	if (android_image_check_header(andr_hdr)) {
1020 		free(andr_hdr);
1021 		return NULL;
1022 	}
1023 
1024 	header_version = andr_hdr->header_version;
1025 	free(andr_hdr);
1026 
1027 	if (header_version < 3) {
1028 		return extract_boot_image_v012_header(dev_desc, part_boot);
1029 	} else {
1030 		if (part_get_info_by_name(dev_desc,
1031 					  ANDROID_PARTITION_VENDOR_BOOT,
1032 					  &part_vendor_boot) < 0) {
1033 			printf("No vendor boot partition\n");
1034 			return NULL;
1035 		}
1036 		boot_hdr = extract_boot_image_v3_header(dev_desc, part_boot);
1037 		vboot_hdr = extract_vendor_boot_image_v3_header(dev_desc,
1038 							&part_vendor_boot);
1039 		if (!boot_hdr || !vboot_hdr)
1040 			goto image_load_exit;
1041 
1042 		andr_hdr = (struct andr_img_hdr *)
1043 				malloc(sizeof(struct andr_img_hdr));
1044 		if (!andr_hdr) {
1045 			printf("No memory for andr hdr\n");
1046 			goto image_load_exit;
1047 		}
1048 
1049 		if (populate_boot_info(boot_hdr, vboot_hdr, andr_hdr)) {
1050 			printf("populate boot info failed\n");
1051 			goto image_load_exit;
1052 		}
1053 
1054 		free(boot_hdr);
1055 		free(vboot_hdr);
1056 
1057 		return andr_hdr;
1058 
1059 image_load_exit:
1060 		free(boot_hdr);
1061 		free(vboot_hdr);
1062 
1063 		return NULL;
1064 	}
1065 
1066 	return NULL;
1067 }
1068 
1069 #if !defined(CONFIG_SPL_BUILD)
1070 /**
1071  * android_print_contents - prints out the contents of the Android format image
1072  * @hdr: pointer to the Android format image header
1073  *
1074  * android_print_contents() formats a multi line Android image contents
1075  * description.
1076  * The routine prints out Android image properties
1077  *
1078  * returns:
1079  *     no returned results
1080  */
1081 void android_print_contents(const struct andr_img_hdr *hdr)
1082 {
1083 	const char * const p = IMAGE_INDENT_STRING;
1084 	/* os_version = ver << 11 | lvl */
1085 	u32 os_ver = hdr->os_version >> 11;
1086 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
1087 	u32 header_version = hdr->header_version;
1088 
1089 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
1090 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
1091 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
1092 	printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
1093 	printf("%ssecond size:      %x\n", p, hdr->second_size);
1094 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
1095 	printf("%stags address:     %x\n", p, hdr->tags_addr);
1096 	printf("%spage size:        %x\n", p, hdr->page_size);
1097 	printf("%sheader_version:   %x\n", p, header_version);
1098 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
1099 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
1100 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
1101 	       p, hdr->os_version,
1102 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
1103 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
1104 	printf("%sname:             %s\n", p, hdr->name);
1105 	printf("%scmdline:          %s\n", p, hdr->cmdline);
1106 
1107 	if (header_version == 1 || header_version == 2) {
1108 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
1109 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
1110 		printf("%sheader size:           %x\n", p, hdr->header_size);
1111 	}
1112 
1113 	if (header_version == 2 || header_version == 3) {
1114 		printf("%sdtb size:              %x\n", p, hdr->dtb_size);
1115 		printf("%sdtb addr:              %llx\n", p, hdr->dtb_addr);
1116 	}
1117 
1118 	if (header_version == 3) {
1119 		printf("%scmdline:               %s\n", p, hdr->total_cmdline);
1120 		printf("%svendor ramdisk size:   %x\n", p, hdr->vendor_ramdisk_size);
1121 		printf("%svendor page size:      %x\n", p, hdr->vendor_page_size);
1122 		printf("%svendor header version: %d\n", p, hdr->vendor_header_version);
1123 		printf("%svendor header size:    %x\n", p, hdr->vendor_header_size);
1124 	}
1125 }
1126 #endif
1127