xref: /rk3399_rockchip-uboot/common/image-android.c (revision 8dd9db5d1cd5826638c3cdb5f681300ff2f29f3b)
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 <sysmem.h>
16 #ifdef CONFIG_RKIMG_BOOTLOADER
17 #include <asm/arch/resource_img.h>
18 #endif
19 #ifdef CONFIG_RK_AVB_LIBAVB_USER
20 #include <android_avb/avb_slot_verify.h>
21 #include <android_avb/avb_ops_user.h>
22 #include <android_avb/rk_avb_ops_user.h>
23 #endif
24 #include <optee_include/OpteeClientInterface.h>
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR	0x10008000
29 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb"
30 
31 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
32 static u32 android_kernel_comp_type = IH_COMP_NONE;
33 
34 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
35 {
36 	/*
37 	 * All the Android tools that generate a boot.img use this
38 	 * address as the default.
39 	 *
40 	 * Even though it doesn't really make a lot of sense, and it
41 	 * might be valid on some platforms, we treat that address as
42 	 * the default value for this field, and try to execute the
43 	 * kernel in place in such a case.
44 	 *
45 	 * Otherwise, we will return the actual value set by the user.
46 	 */
47 	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
48 		return (ulong)hdr + hdr->page_size;
49 
50 #ifdef CONFIG_ARCH_ROCKCHIP
51 	/*
52 	 * If kernel is compressed, kernel_addr is set as decompressed address
53 	 * after compressed being loaded to ram, so let's use it.
54 	 */
55 	if (android_kernel_comp_type != IH_COMP_NONE &&
56 	    android_kernel_comp_type != IH_COMP_ZIMAGE)
57 		return hdr->kernel_addr;
58 
59 	/*
60 	 * Compatble with rockchip legacy packing with kernel/ramdisk/second
61 	 * address base from 0x60000000(SDK versiont < 8.1), these are invalid
62 	 * address, so we calc it by real size.
63 	 */
64 	return (ulong)hdr + hdr->page_size;
65 #else
66 	return hdr->kernel_addr;
67 #endif
68 
69 }
70 
71 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp)
72 {
73 	android_kernel_comp_type = comp;
74 }
75 
76 u32 android_image_get_comp(const struct andr_img_hdr *hdr)
77 {
78 	return android_kernel_comp_type;
79 }
80 
81 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr)
82 {
83 	ulong kaddr = android_image_get_kernel_addr(hdr);
84 	return bootm_parse_comp((const unsigned char *)kaddr);
85 }
86 
87 /**
88  * android_image_get_kernel() - processes kernel part of Android boot images
89  * @hdr:	Pointer to image header, which is at the start
90  *			of the image.
91  * @verify:	Checksum verification flag. Currently unimplemented.
92  * @os_data:	Pointer to a ulong variable, will hold os data start
93  *			address.
94  * @os_len:	Pointer to a ulong variable, will hold os data length.
95  *
96  * This function returns the os image's start address and length. Also,
97  * it appends the kernel command line to the bootargs env variable.
98  *
99  * Return: Zero, os start address and length on success,
100  *		otherwise on failure.
101  */
102 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
103 			     ulong *os_data, ulong *os_len)
104 {
105 	u32 kernel_addr = android_image_get_kernel_addr(hdr);
106 
107 	/*
108 	 * Not all Android tools use the id field for signing the image with
109 	 * sha1 (or anything) so we don't check it. It is not obvious that the
110 	 * string is null terminated so we take care of this.
111 	 */
112 	strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
113 	andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
114 	if (strlen(andr_tmp_str))
115 		printf("Android's image name: %s\n", andr_tmp_str);
116 
117 	printf("Kernel load addr 0x%08x size %u KiB\n",
118 	       kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
119 
120 	int len = 0;
121 	if (*hdr->cmdline) {
122 		debug("Kernel command line: %s\n", hdr->cmdline);
123 		len += strlen(hdr->cmdline);
124 	}
125 
126 	char *bootargs = env_get("bootargs");
127 	if (bootargs)
128 		len += strlen(bootargs);
129 
130 	char *newbootargs = malloc(len + 2);
131 	if (!newbootargs) {
132 		puts("Error: malloc in android_image_get_kernel failed!\n");
133 		return -ENOMEM;
134 	}
135 	*newbootargs = '\0';
136 
137 	if (bootargs) {
138 		strcpy(newbootargs, bootargs);
139 		strcat(newbootargs, " ");
140 	}
141 	if (*hdr->cmdline)
142 		strcat(newbootargs, hdr->cmdline);
143 
144 	env_set("bootargs", newbootargs);
145 
146 	if (os_data) {
147 		*os_data = (ulong)hdr;
148 		*os_data += hdr->page_size;
149 	}
150 	if (os_len)
151 		*os_len = hdr->kernel_size;
152 	return 0;
153 }
154 
155 int android_image_check_header(const struct andr_img_hdr *hdr)
156 {
157 	return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
158 }
159 
160 ulong android_image_get_end(const struct andr_img_hdr *hdr)
161 {
162 	ulong end;
163 	/*
164 	 * The header takes a full page, the remaining components are aligned
165 	 * on page boundary
166 	 */
167 	end = (ulong)hdr;
168 	end += hdr->page_size;
169 	end += ALIGN(hdr->kernel_size, hdr->page_size);
170 	end += ALIGN(hdr->ramdisk_size, hdr->page_size);
171 	end += ALIGN(hdr->second_size, hdr->page_size);
172 
173 	if (hdr->header_version >= 1)
174 		end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
175 
176 	return end;
177 }
178 
179 u32 android_image_get_ksize(const struct andr_img_hdr *hdr)
180 {
181 	return hdr->kernel_size;
182 }
183 
184 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address)
185 {
186 	hdr->kernel_addr = load_address;
187 }
188 
189 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
190 {
191 	return android_image_get_kernel_addr(hdr);
192 }
193 
194 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
195 			      ulong *rd_data, ulong *rd_len)
196 {
197 	if (!hdr->ramdisk_size) {
198 		*rd_data = *rd_len = 0;
199 		return -1;
200 	}
201 
202 	/* We have load ramdisk at "ramdisk_addr_r" */
203 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
204 	ulong ramdisk_addr_r;
205 
206 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
207 	if (!ramdisk_addr_r) {
208 		printf("No Found Ramdisk Load Address.\n");
209 		return -1;
210 	}
211 
212 	*rd_data = ramdisk_addr_r;
213 #else
214 	*rd_data = (unsigned long)hdr;
215 	*rd_data += hdr->page_size;
216 	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
217 #endif
218 
219 	*rd_len = hdr->ramdisk_size;
220 
221 	printf("RAM disk load addr 0x%08lx size %u KiB\n",
222 	       *rd_data, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
223 
224 	return 0;
225 }
226 
227 int android_image_get_fdt(const struct andr_img_hdr *hdr,
228 			      ulong *rd_data)
229 {
230 	if (!hdr->second_size) {
231 		*rd_data = 0;
232 		return -1;
233 	}
234 
235 	/* We have load fdt at "fdt_addr_r" */
236 #if defined(CONFIG_USING_KERNEL_DTB) || \
237     defined(CONFIG_ANDROID_BOOT_IMAGE_SEPARATE)
238 	ulong fdt_addr_r;
239 
240 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
241 	if (!fdt_addr_r) {
242 		printf("No Found FDT Load Address.\n");
243 		return -1;
244 	}
245 
246 	*rd_data = fdt_addr_r;
247 #else
248 	*rd_data = (unsigned long)hdr;
249 	*rd_data += hdr->page_size;
250 	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
251 	*rd_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
252 #endif
253 
254 	debug("FDT load addr 0x%08x size %u KiB\n",
255 	      hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
256 
257 	return 0;
258 }
259 
260 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
261 int android_image_load_separate(struct andr_img_hdr *hdr,
262 				const disk_partition_t *part,
263 				void *load_address, void *ram_src)
264 {
265 	struct blk_desc *dev_desc = rockchip_get_bootdev();
266 	ulong fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
267 	char *fdt_high = env_get("fdt_high");
268 	char *ramdisk_high = env_get("initrd_high");
269 	ulong blk_start, blk_cnt, size;
270 	int ret, blk_read = 0;
271 	ulong start;
272 
273 	if (hdr->kernel_size) {
274 		size = hdr->kernel_size + hdr->page_size;
275 		blk_cnt = DIV_ROUND_UP(size, dev_desc->blksz);
276 		if (!sysmem_alloc_base(MEMBLK_ID_KERNEL,
277 				       (phys_addr_t)load_address,
278 				       blk_cnt * dev_desc->blksz))
279 			return -ENXIO;
280 
281 		if (ram_src) {
282 			start = (ulong)ram_src;
283 			memcpy((char *)load_address, (char *)start, size);
284 		} else {
285 			blk_start = part->start;
286 			ret = blk_dread(dev_desc, blk_start,
287 					blk_cnt, load_address);
288 			if (ret != blk_cnt) {
289 				debug("%s: read kernel failed, ret=%d\n",
290 				      __func__, ret);
291 				return -1;
292 			}
293 			blk_read += ret;
294 		}
295 	}
296 
297 	if (hdr->ramdisk_size) {
298 		ulong ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
299 
300 		size = hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size);
301 		blk_cnt = DIV_ROUND_UP(hdr->ramdisk_size, dev_desc->blksz);
302 		if (!sysmem_alloc_base(MEMBLK_ID_RAMDISK,
303 				       ramdisk_addr_r,
304 				       blk_cnt * dev_desc->blksz))
305 			return -ENXIO;
306 		if (ram_src) {
307 			start = (unsigned long)ram_src;
308 			start += hdr->page_size;
309 			start += ALIGN(hdr->kernel_size, hdr->page_size);
310 			memcpy((char *)ramdisk_addr_r,
311 			       (char *)start, hdr->ramdisk_size);
312 		} else {
313 			blk_start = part->start +
314 				DIV_ROUND_UP(size, dev_desc->blksz);
315 			ret = blk_dread(dev_desc, blk_start,
316 					blk_cnt, (void *)ramdisk_addr_r);
317 			if (ret != blk_cnt) {
318 				debug("%s: read ramdisk failed, ret=%d\n",
319 				      __func__, ret);
320 				return -1;
321 			}
322 			blk_read += ret;
323 		}
324 	}
325 
326 	if ((gd->fdt_blob != (void *)fdt_addr_r) && hdr->second_size) {
327 #ifdef CONFIG_RKIMG_BOOTLOADER
328 		/* Rockchip AOSP, resource.img is in second position */
329 		ulong fdt_size;
330 
331 		fdt_size = rockchip_read_dtb_file((void *)fdt_addr_r);
332 		if (fdt_size < 0) {
333 			printf("%s: read fdt failed\n", __func__);
334 			return ret;
335 		}
336 
337 		blk_read += DIV_ROUND_UP(fdt_size, dev_desc->blksz);
338 #else
339 		/* Standard AOSP, dtb is in second position */
340 		ulong blk_start, blk_cnt;
341 
342 		size = hdr->page_size +
343 		       ALIGN(hdr->kernel_size, hdr->page_size) +
344 		       ALIGN(hdr->ramdisk_size, hdr->page_size);
345 		blk_cnt = DIV_ROUND_UP(hdr->second_size, dev_desc->blksz);
346 		if (!sysmem_alloc_base(MEMBLK_ID_FDT_AOSP,
347 				       fdt_addr_r,
348 				       blk_cnt * dev_desc->blksz +
349 				       CONFIG_SYS_FDT_PAD))
350 			return -ENXIO;
351 
352 		if (ram_src) {
353 			start = (unsigned long)ram_src;
354 			start += hdr->page_size;
355 			start += ALIGN(hdr->kernel_size, hdr->page_size);
356 			start += ALIGN(hdr->ramdisk_size, hdr->page_size);
357 			memcpy((char *)fdt_addr_r,
358 			       (char *)start, hdr->second_size);
359 		} else {
360 			blk_start = part->start +
361 					DIV_ROUND_UP(size, dev_desc->blksz);
362 			ret = blk_dread(dev_desc, blk_start, blk_cnt,
363 					(void *)fdt_addr_r);
364 			if (ret != blk_cnt) {
365 				debug("%s: read dtb failed, ret=%d\n",
366 				      __func__, ret);
367 				return -1;
368 			}
369 
370 			blk_read += blk_cnt;
371 		}
372 #endif /* CONFIG_RKIMG_BOOTLOADER */
373 	}
374 
375 	if (blk_read > 0 || ram_src) {
376 		if (!fdt_high) {
377 			env_set_hex("fdt_high", -1UL);
378 			printf("Fdt ");
379 		}
380 		if (!ramdisk_high) {
381 			env_set_hex("initrd_high", -1UL);
382 			printf("Ramdisk ");
383 		}
384 		if (!fdt_high || !ramdisk_high)
385 			printf("skip relocation\n");
386 	}
387 
388 	return blk_read;
389 }
390 #endif /* CONFIG_ANDROID_BOOT_IMAGE_SEPARATE */
391 
392 long android_image_load(struct blk_desc *dev_desc,
393 			const disk_partition_t *part_info,
394 			unsigned long load_address,
395 			unsigned long max_size) {
396 	void *buf;
397 	long blk_cnt = 0;
398 	long blk_read = 0;
399 	u32 comp;
400 	u32 kload_addr;
401 	u32 blkcnt;
402 	struct andr_img_hdr *hdr;
403 
404 	if (max_size < part_info->blksz)
405 		return -1;
406 
407 	/*
408 	 * Read the Android boot.img header and a few parts of
409 	 * the head of kernel image(2 blocks maybe enough).
410 	 */
411 	blkcnt = DIV_ROUND_UP(sizeof(*hdr), 512) + 2;
412 	hdr = memalign(ARCH_DMA_MINALIGN, blkcnt * 512);
413 	if (!hdr) {
414 		printf("%s: no memory\n", __func__);
415 		return -1;
416 	}
417 
418 	if (blk_dread(dev_desc, part_info->start, blkcnt, hdr) != blkcnt)
419 		blk_read = -1;
420 
421 	if (!blk_read && android_image_check_header(hdr) != 0) {
422 		printf("** Invalid Android Image header **\n");
423 		blk_read = -1;
424 	}
425 
426 	/* page_size for image header */
427 	load_address -= hdr->page_size;
428 
429 	/* We don't know the size of the Android image before reading the header
430 	 * so we don't limit the size of the mapped memory.
431 	 */
432 	buf = map_sysmem(load_address, 0 /* size */);
433 	if (!blk_read) {
434 		blk_cnt = (android_image_get_end(hdr) - (ulong)hdr +
435 			   part_info->blksz - 1) / part_info->blksz;
436 		comp = android_image_parse_kernel_comp(hdr);
437 		/*
438 		 * We should load compressed kernel Image to high memory at
439 		 * address "kernel_addr_c".
440 		 */
441 		if (comp != IH_COMP_NONE) {
442 			ulong kernel_addr_c;
443 
444 			env_set_ulong("os_comp", comp);
445 			kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0);
446 			if (kernel_addr_c) {
447 				load_address = kernel_addr_c - hdr->page_size;
448 				unmap_sysmem(buf);
449 				buf = map_sysmem(load_address, 0 /* size */);
450 			}
451 #ifdef CONFIG_ARM64
452 			else {
453 				printf("Warn: \"kernel_addr_c\" is not defined "
454 				       "for compressed kernel Image!\n");
455 				load_address += android_image_get_ksize(hdr) * 3;
456 				load_address = ALIGN(load_address, ARCH_DMA_MINALIGN);
457 				env_set_ulong("kernel_addr_c", load_address);
458 
459 				load_address -= hdr->page_size;
460 				unmap_sysmem(buf);
461 				buf = map_sysmem(load_address, 0 /* size */);
462 			}
463 #endif
464 		}
465 
466 		if (blk_cnt * part_info->blksz > max_size) {
467 			debug("Android Image too big (%lu bytes, max %lu)\n",
468 			      android_image_get_end(hdr) - (ulong)hdr,
469 			      max_size);
470 			blk_read = -1;
471 		} else {
472 			debug("Loading Android Image (%lu blocks) to 0x%lx... ",
473 			      blk_cnt, load_address);
474 
475 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
476 			blk_read =
477 			android_image_load_separate(hdr, part_info, buf, NULL);
478 #else
479 			if (!sysmem_alloc_base(MEMBLK_ID_ANDROID,
480 					       (phys_addr_t)buf,
481 						blk_cnt * part_info->blksz))
482 				return -ENXIO;
483 
484 			blk_read = blk_dread(dev_desc, part_info->start,
485 					     blk_cnt, buf);
486 #endif
487 		}
488 
489 		/*
490 		 * zImage is not need to decompress
491 		 * kernel will handle decompress itself
492 		 */
493 		if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
494 			kload_addr = env_get_ulong("kernel_addr_r", 16, 0x02080000);
495 			android_image_set_kload(buf, kload_addr);
496 			android_image_set_comp(buf, comp);
497 		} else {
498 			android_image_set_comp(buf, IH_COMP_NONE);
499 		}
500 
501 	}
502 
503 	free(hdr);
504 	unmap_sysmem(buf);
505 
506 #ifndef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE
507 	debug("%lu blocks read: %s\n",
508 	      blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR");
509 	if (blk_read != blk_cnt)
510 		return -1;
511 #else
512 	debug("%lu blocks read\n", blk_read);
513 	if (blk_read < 0)
514 		return blk_read;
515 #endif
516 
517 	return load_address;
518 }
519 
520 #if !defined(CONFIG_SPL_BUILD)
521 /**
522  * android_print_contents - prints out the contents of the Android format image
523  * @hdr: pointer to the Android format image header
524  *
525  * android_print_contents() formats a multi line Android image contents
526  * description.
527  * The routine prints out Android image properties
528  *
529  * returns:
530  *     no returned results
531  */
532 void android_print_contents(const struct andr_img_hdr *hdr)
533 {
534 	const char * const p = IMAGE_INDENT_STRING;
535 	/* os_version = ver << 11 | lvl */
536 	u32 os_ver = hdr->os_version >> 11;
537 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
538 	u32 header_version = hdr->header_version;
539 
540 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
541 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
542 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
543 	printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr);
544 	printf("%ssecond size:      %x\n", p, hdr->second_size);
545 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
546 	printf("%stags address:     %x\n", p, hdr->tags_addr);
547 	printf("%spage size:        %x\n", p, hdr->page_size);
548 	printf("%sheader_version:   %x\n", p, header_version);
549 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
550 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
551 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
552 	       p, hdr->os_version,
553 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
554 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
555 	printf("%sname:             %s\n", p, hdr->name);
556 	printf("%scmdline:          %s\n", p, hdr->cmdline);
557 
558 	if (header_version >= 1) {
559 		printf("%srecovery dtbo size:    %x\n", p, hdr->recovery_dtbo_size);
560 		printf("%srecovery dtbo offset:  %llx\n", p, hdr->recovery_dtbo_offset);
561 		printf("%sheader size:           %x\n", p, hdr->header_size);
562 	}
563 }
564 #endif
565