1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 #include <image-android-dt.h>
10 #include <android_image.h>
13 #include <asm/unaligned.h>
16 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
18 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
23 * All the Android tools that generate a boot.img use this
24 * address as the default.
26 * Even though it doesn't really make a lot of sense, and it
27 * might be valid on some platforms, we treat that adress as
28 * the default value for this field, and try to execute the
29 * kernel in place in such a case.
31 * Otherwise, we will return the actual value set by the user.
33 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
34 return (ulong)hdr + hdr->page_size;
36 return hdr->kernel_addr;
40 * android_image_get_kernel() - processes kernel part of Android boot images
41 * @hdr: Pointer to image header, which is at the start
43 * @verify: Checksum verification flag. Currently unimplemented.
44 * @os_data: Pointer to a ulong variable, will hold os data start
46 * @os_len: Pointer to a ulong variable, will hold os data length.
48 * This function returns the os image's start address and length. Also,
49 * it appends the kernel command line to the bootargs env variable.
51 * Return: Zero, os start address and length on success,
52 * otherwise on failure.
54 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
55 ulong *os_data, ulong *os_len)
57 u32 kernel_addr = android_image_get_kernel_addr(hdr);
58 const struct image_header *ihdr = (const struct image_header *)
59 ((uintptr_t)hdr + hdr->page_size);
62 * Not all Android tools use the id field for signing the image with
63 * sha1 (or anything) so we don't check it. It is not obvious that the
64 * string is null terminated so we take care of this.
66 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
67 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
68 if (strlen(andr_tmp_str))
69 printf("Android's image name: %s\n", andr_tmp_str);
71 printf("Kernel load addr 0x%08x size %u KiB\n",
72 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
76 printf("Kernel command line: %s\n", hdr->cmdline);
77 len += strlen(hdr->cmdline);
80 char *bootargs = env_get("bootargs");
82 len += strlen(bootargs);
84 char *newbootargs = malloc(len + 2);
86 puts("Error: malloc in android_image_get_kernel failed!\n");
92 strcpy(newbootargs, bootargs);
93 strcat(newbootargs, " ");
96 strcat(newbootargs, hdr->cmdline);
98 env_set("bootargs", newbootargs);
101 if (image_get_magic(ihdr) == IH_MAGIC) {
102 *os_data = image_get_data(ihdr);
104 *os_data = (ulong)hdr;
105 *os_data += hdr->page_size;
109 if (image_get_magic(ihdr) == IH_MAGIC)
110 *os_len = image_get_data_size(ihdr);
112 *os_len = hdr->kernel_size;
117 int android_image_check_header(const struct andr_img_hdr *hdr)
119 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
122 ulong android_image_get_end(const struct andr_img_hdr *hdr)
127 * The header takes a full page, the remaining components are aligned
131 end += hdr->page_size;
132 end += ALIGN(hdr->kernel_size, hdr->page_size);
133 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
134 end += ALIGN(hdr->second_size, hdr->page_size);
136 if (hdr->header_version >= 1)
137 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
139 if (hdr->header_version >= 2)
140 end += ALIGN(hdr->dtb_size, hdr->page_size);
145 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
147 return android_image_get_kernel_addr(hdr);
150 ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
152 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
154 if (image_get_magic((image_header_t *)p) == IH_MAGIC)
155 return image_get_comp((image_header_t *)p);
156 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
162 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
163 ulong *rd_data, ulong *rd_len)
165 if (!hdr->ramdisk_size) {
166 *rd_data = *rd_len = 0;
170 printf("RAM disk load addr 0x%08x size %u KiB\n",
171 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
173 *rd_data = (unsigned long)hdr;
174 *rd_data += hdr->page_size;
175 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
177 *rd_len = hdr->ramdisk_size;
181 int android_image_get_second(const struct andr_img_hdr *hdr,
182 ulong *second_data, ulong *second_len)
184 if (!hdr->second_size) {
185 *second_data = *second_len = 0;
189 *second_data = (unsigned long)hdr;
190 *second_data += hdr->page_size;
191 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
192 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
194 printf("second address is 0x%lx\n",*second_data);
196 *second_len = hdr->second_size;
201 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
202 * @hdr_addr: Boot image header address
203 * @addr: If not NULL, will contain address of recovery DTBO image
204 * @size: If not NULL, will contain size of recovery DTBO image
206 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
207 * Boot Image in RAM. The format of this image is Android DTBO (see
208 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
209 * the address is obtained from this function, one can use 'adtimg' U-Boot
210 * command or android_dt_*() functions to extract desired DTBO blob.
212 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
213 * only can be found in recovery image. On A/B devices we can always rely on
214 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
215 * AOSP documentation for details.
217 * Return: true on success or false on error.
219 bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
221 const struct andr_img_hdr *hdr;
225 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
226 if (android_image_check_header(hdr)) {
227 printf("Error: Boot Image header is incorrect\n");
232 if (hdr->header_version < 1) {
233 printf("Error: header_version must be >= 1 to get dtbo\n");
238 if (hdr->recovery_dtbo_size == 0) {
239 printf("Error: recovery_dtbo_size is 0\n");
244 /* Calculate the address of DTB area in boot image */
245 dtbo_img_addr = hdr_addr;
246 dtbo_img_addr += hdr->page_size;
247 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
248 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
249 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
252 *addr = dtbo_img_addr;
254 *size = hdr->recovery_dtbo_size;
262 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
263 * @hdr_addr: Boot image header address
264 * @addr: Will contain the address of DTB area in boot image
266 * Return: true on success or false on fail.
268 static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
270 const struct andr_img_hdr *hdr;
274 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
275 if (android_image_check_header(hdr)) {
276 printf("Error: Boot Image header is incorrect\n");
281 if (hdr->header_version < 2) {
282 printf("Error: header_version must be >= 2 to get dtb\n");
287 if (hdr->dtb_size == 0) {
288 printf("Error: dtb_size is 0\n");
293 /* Calculate the address of DTB area in boot image */
294 dtb_img_addr = hdr_addr;
295 dtb_img_addr += hdr->page_size;
296 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
297 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
298 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
299 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
301 *addr = dtb_img_addr;
309 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
310 * @hdr_addr: Boot image header address
311 * @index: Index of desired DTB in DTB area (starting from 0)
312 * @addr: If not NULL, will contain address to specified DTB
313 * @size: If not NULL, will contain size of specified DTB
315 * Get the address and size of DTB blob by its index in DTB area of Android
318 * Return: true on success or false on error.
320 bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
323 const struct andr_img_hdr *hdr;
325 ulong dtb_img_addr; /* address of DTB part in boot image */
326 u32 dtb_img_size; /* size of DTB payload in boot image */
327 ulong dtb_addr; /* address of DTB blob with specified index */
328 u32 i; /* index iterator */
330 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
334 /* Check if DTB area of boot image is in DTBO format */
335 if (android_dt_check_header(dtb_img_addr)) {
336 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
340 /* Find out the address of DTB with specified index in concat blobs */
341 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
342 dtb_img_size = hdr->dtb_size;
345 dtb_addr = dtb_img_addr;
346 while (dtb_addr < dtb_img_addr + dtb_img_size) {
347 const struct fdt_header *fdt;
350 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
351 if (fdt_check_header(fdt) != 0) {
353 printf("Error: Invalid FDT header for index %u\n", i);
357 dtb_size = fdt_totalsize(fdt);
368 dtb_addr += dtb_size;
372 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
376 #if !defined(CONFIG_SPL_BUILD)
378 * android_print_contents - prints out the contents of the Android format image
379 * @hdr: pointer to the Android format image header
381 * android_print_contents() formats a multi line Android image contents
383 * The routine prints out Android image properties
386 * no returned results
388 void android_print_contents(const struct andr_img_hdr *hdr)
390 const char * const p = IMAGE_INDENT_STRING;
391 /* os_version = ver << 11 | lvl */
392 u32 os_ver = hdr->os_version >> 11;
393 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
395 printf("%skernel size: %x\n", p, hdr->kernel_size);
396 printf("%skernel address: %x\n", p, hdr->kernel_addr);
397 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
398 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
399 printf("%ssecond size: %x\n", p, hdr->second_size);
400 printf("%ssecond address: %x\n", p, hdr->second_addr);
401 printf("%stags address: %x\n", p, hdr->tags_addr);
402 printf("%spage size: %x\n", p, hdr->page_size);
403 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
404 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
405 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
407 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
408 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
409 printf("%sname: %s\n", p, hdr->name);
410 printf("%scmdline: %s\n", p, hdr->cmdline);
411 printf("%sheader_version: %d\n", p, hdr->header_version);
413 if (hdr->header_version >= 1) {
414 printf("%srecovery dtbo size: %x\n", p,
415 hdr->recovery_dtbo_size);
416 printf("%srecovery dtbo offset: %llx\n", p,
417 hdr->recovery_dtbo_offset);
418 printf("%sheader size: %x\n", p,
422 if (hdr->header_version >= 2) {
423 printf("%sdtb size: %x\n", p, hdr->dtb_size);
424 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
429 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
431 * @index: Number of DTB blob in DTB area.
433 * Return: true on success or false on error.
435 static bool android_image_print_dtb_info(const struct fdt_header *fdt,
441 const char *compatible;
443 root_node_off = fdt_path_offset(fdt, "/");
444 if (root_node_off < 0) {
445 printf("Error: Root node not found\n");
449 fdt_size = fdt_totalsize(fdt);
450 compatible = fdt_getprop(fdt, root_node_off, "compatible",
452 model = fdt_getprop(fdt, root_node_off, "model", NULL);
454 printf(" - DTB #%u:\n", index);
455 printf(" (DTB)size = %d\n", fdt_size);
456 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
457 printf(" (DTB)compatible = %s\n",
458 compatible ? compatible : "(unknown)");
464 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
465 * @hdr_addr: Boot image header address
467 * DTB payload in Android Boot Image v2+ can be in one of following formats:
468 * 1. Concatenated DTB blobs
469 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
471 * This function does next:
472 * 1. Prints out the format used in DTB area
473 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
476 * Return: true on success or false on error.
478 bool android_image_print_dtb_contents(ulong hdr_addr)
480 const struct andr_img_hdr *hdr;
482 ulong dtb_img_addr; /* address of DTB part in boot image */
483 u32 dtb_img_size; /* size of DTB payload in boot image */
484 ulong dtb_addr; /* address of DTB blob with specified index */
485 u32 i; /* index iterator */
487 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
491 /* Check if DTB area of boot image is in DTBO format */
492 if (android_dt_check_header(dtb_img_addr)) {
493 printf("## DTB area contents (DTBO format):\n");
494 android_dt_print_contents(dtb_img_addr);
498 printf("## DTB area contents (concat format):\n");
500 /* Iterate over concatenated DTB blobs */
501 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
502 dtb_img_size = hdr->dtb_size;
505 dtb_addr = dtb_img_addr;
506 while (dtb_addr < dtb_img_addr + dtb_img_size) {
507 const struct fdt_header *fdt;
510 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
511 if (fdt_check_header(fdt) != 0) {
513 printf("Error: Invalid FDT header for index %u\n", i);
517 res = android_image_print_dtb_info(fdt, i);
523 dtb_size = fdt_totalsize(fdt);
525 dtb_addr += dtb_size;