1 // SPDX-License-Identifier: GPL-2.0+
8 #include <image-android-dt.h>
9 #include <android_image.h>
12 #include <asm/unaligned.h>
14 #include <linux/libfdt.h>
16 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
17 #define ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR 0x11000000
19 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
21 static ulong checksum(const unsigned char *buffer, ulong size)
25 for (ulong i = 0; i < size; i++)
30 static bool is_trailer_present(ulong bootconfig_end_addr)
32 return !strncmp((char *)(bootconfig_end_addr - BOOTCONFIG_MAGIC_SIZE),
33 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
36 static ulong add_trailer(ulong bootconfig_start_addr, ulong bootconfig_size)
41 if (!bootconfig_start_addr)
46 end = bootconfig_start_addr + bootconfig_size;
47 if (is_trailer_present(end))
50 memcpy((void *)(end), &bootconfig_size, BOOTCONFIG_SIZE_SIZE);
51 sum = checksum((unsigned char *)bootconfig_start_addr, bootconfig_size);
52 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE), &sum,
53 BOOTCONFIG_CHECKSUM_SIZE);
54 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE + BOOTCONFIG_CHECKSUM_SIZE),
55 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
57 return BOOTCONFIG_TRAILER_SIZE;
60 __weak ulong get_avendor_bootimg_addr(void)
65 static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
66 struct andr_image_data *data)
70 data->kcmdline = hdr->cmdline;
71 data->header_version = hdr->header_version;
74 * The header takes a full page, the remaining components are aligned
78 end += ANDR_GKI_PAGE_SIZE;
79 data->kernel_ptr = end;
80 data->kernel_size = hdr->kernel_size;
81 end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
82 data->ramdisk_ptr = end;
83 data->ramdisk_size = hdr->ramdisk_size;
84 data->boot_ramdisk_size = hdr->ramdisk_size;
85 end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
87 if (hdr->header_version > 3)
88 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
90 data->boot_img_total_size = end - (ulong)hdr;
93 static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
94 *hdr, struct andr_image_data *data)
99 * The header takes a full page, the remaining components are aligned
102 data->kcmdline_extra = hdr->cmdline;
103 data->tags_addr = hdr->tags_addr;
104 data->image_name = hdr->name;
105 data->kernel_addr = hdr->kernel_addr;
106 data->ramdisk_addr = hdr->ramdisk_addr;
107 data->dtb_load_addr = hdr->dtb_addr;
108 data->bootconfig_size = hdr->bootconfig_size;
110 end += hdr->page_size;
111 if (hdr->vendor_ramdisk_size) {
112 data->vendor_ramdisk_ptr = end;
113 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
114 data->ramdisk_size += hdr->vendor_ramdisk_size;
115 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
119 data->dtb_size = hdr->dtb_size;
121 end += ALIGN(hdr->dtb_size, hdr->page_size);
122 end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
123 data->bootconfig_addr = end;
124 if (hdr->bootconfig_size) {
125 data->bootconfig_size += add_trailer(data->bootconfig_addr,
126 data->bootconfig_size);
127 data->ramdisk_size += data->bootconfig_size;
129 end += ALIGN(data->bootconfig_size, hdr->page_size);
130 data->vendor_boot_img_total_size = end - (ulong)hdr;
133 static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
134 struct andr_image_data *data)
138 data->image_name = hdr->name;
139 data->kcmdline = hdr->cmdline;
140 data->kernel_addr = hdr->kernel_addr;
141 data->ramdisk_addr = hdr->ramdisk_addr;
142 data->header_version = hdr->header_version;
143 data->dtb_load_addr = hdr->dtb_addr;
148 * The header takes a full page, the remaining components are aligned
152 end += hdr->page_size;
154 data->kernel_ptr = end;
155 data->kernel_size = hdr->kernel_size;
156 end += ALIGN(hdr->kernel_size, hdr->page_size);
158 data->ramdisk_ptr = end;
159 data->ramdisk_size = hdr->ramdisk_size;
160 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
162 data->second_ptr = end;
163 data->second_size = hdr->second_size;
164 end += ALIGN(hdr->second_size, hdr->page_size);
166 if (hdr->header_version >= 1) {
167 data->recovery_dtbo_ptr = end;
168 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
169 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
172 if (hdr->header_version >= 2) {
174 data->dtb_size = hdr->dtb_size;
175 end += ALIGN(hdr->dtb_size, hdr->page_size);
178 data->boot_img_total_size = end - (ulong)hdr;
181 bool android_image_get_bootimg_size(const void *hdr, u32 *boot_img_size)
183 struct andr_image_data data;
185 if (!hdr || !boot_img_size) {
186 printf("hdr or boot_img_size can't be NULL\n");
190 if (!is_android_boot_image_header(hdr)) {
191 printf("Incorrect boot image header\n");
195 if (((struct andr_boot_img_hdr_v0 *)hdr)->header_version <= 2)
196 android_boot_image_v0_v1_v2_parse_hdr(hdr, &data);
198 android_boot_image_v3_v4_parse_hdr(hdr, &data);
200 *boot_img_size = data.boot_img_total_size;
205 bool android_image_get_vendor_bootimg_size(const void *hdr, u32 *vendor_boot_img_size)
207 struct andr_image_data data;
209 if (!hdr || !vendor_boot_img_size) {
210 printf("hdr or vendor_boot_img_size can't be NULL\n");
214 if (!is_android_vendor_boot_image_header(hdr)) {
215 printf("Incorrect vendor boot image header\n");
219 android_vendor_boot_image_v3_v4_parse_hdr(hdr, &data);
221 *vendor_boot_img_size = data.vendor_boot_img_total_size;
226 bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
227 struct andr_image_data *data)
229 if (!boot_hdr || !data) {
230 printf("boot_hdr or data params can't be NULL\n");
234 if (!is_android_boot_image_header(boot_hdr)) {
235 printf("Incorrect boot image header\n");
239 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
240 if (!vendor_boot_hdr) {
241 printf("For boot header v3+ vendor boot image has to be provided\n");
244 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
245 printf("Incorrect vendor boot image header\n");
248 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
249 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
251 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
257 static ulong android_image_get_kernel_addr(struct andr_image_data *img_data,
261 * All the Android tools that generate a boot.img use this
262 * address as the default.
264 * Even though it doesn't really make a lot of sense, and it
265 * might be valid on some platforms, we treat that adress as
266 * the default value for this field, and try to execute the
267 * kernel in place in such a case.
269 * Otherwise, we will return the actual value set by the user.
271 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) {
272 if (comp == IH_COMP_NONE)
273 return img_data->kernel_ptr;
274 return env_get_ulong("kernel_addr_r", 16, 0);
278 * abootimg creates images where all load addresses are 0
279 * and we need to fix them.
281 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
282 return env_get_ulong("kernel_addr_r", 16, 0);
284 return img_data->kernel_addr;
288 * android_image_get_kernel() - processes kernel part of Android boot images
289 * @hdr: Pointer to boot image header, which is at the start
291 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
292 * start of the image.
293 * @verify: Checksum verification flag. Currently unimplemented.
294 * @os_data: Pointer to a ulong variable, will hold os data start
296 * @os_len: Pointer to a ulong variable, will hold os data length.
298 * This function returns the os image's start address and length. Also,
299 * it appends the kernel command line to the bootargs env variable.
301 * Return: Zero, os start address and length on success,
302 * otherwise on failure.
304 int android_image_get_kernel(const void *hdr,
305 const void *vendor_boot_img, int verify,
306 ulong *os_data, ulong *os_len)
308 struct andr_image_data img_data = {0};
310 const struct legacy_img_hdr *ihdr;
313 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
316 comp = android_image_get_kcomp(hdr, vendor_boot_img);
318 kernel_addr = android_image_get_kernel_addr(&img_data, comp);
319 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
322 * Not all Android tools use the id field for signing the image with
323 * sha1 (or anything) so we don't check it. It is not obvious that the
324 * string is null terminated so we take care of this.
326 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
327 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
328 if (strlen(andr_tmp_str))
329 printf("Android's image name: %s\n", andr_tmp_str);
331 printf("Kernel load addr 0x%08lx size %u KiB\n",
332 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
335 if (*img_data.kcmdline) {
336 printf("Kernel command line: %s\n", img_data.kcmdline);
337 len += strlen(img_data.kcmdline);
340 if (img_data.kcmdline_extra) {
341 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
342 len += strlen(img_data.kcmdline_extra);
345 char *bootargs = env_get("bootargs");
347 len += strlen(bootargs);
349 char *newbootargs = malloc(len + 2);
351 puts("Error: malloc in android_image_get_kernel failed!\n");
357 strcpy(newbootargs, bootargs);
358 strcat(newbootargs, " ");
361 if (*img_data.kcmdline)
362 strcat(newbootargs, img_data.kcmdline);
364 if (img_data.kcmdline_extra) {
365 strcat(newbootargs, " ");
366 strcat(newbootargs, img_data.kcmdline_extra);
369 env_set("bootargs", newbootargs);
372 if (image_get_magic(ihdr) == IH_MAGIC) {
373 *os_data = image_get_data(ihdr);
375 *os_data = img_data.kernel_ptr;
379 if (image_get_magic(ihdr) == IH_MAGIC)
380 *os_len = image_get_data_size(ihdr);
382 *os_len = img_data.kernel_size;
387 bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
389 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
392 bool is_android_boot_image_header(const void *hdr)
394 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
397 ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
398 const void *vendor_boot_img)
400 struct andr_image_data img_data;
402 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
405 if (img_data.header_version > 2)
408 return img_data.boot_img_total_size;
411 ulong android_image_get_kload(const void *hdr,
412 const void *vendor_boot_img)
414 struct andr_image_data img_data;
417 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
420 comp = android_image_get_kcomp(hdr, vendor_boot_img);
422 return android_image_get_kernel_addr(&img_data, comp);
425 ulong android_image_get_kcomp(const void *hdr,
426 const void *vendor_boot_img)
428 struct andr_image_data img_data;
431 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
434 p = (const void *)img_data.kernel_ptr;
435 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
436 return image_get_comp((struct legacy_img_hdr *)p);
437 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
440 return image_decomp_type(p, sizeof(u32));
443 int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
444 ulong *rd_data, ulong *rd_len)
446 struct andr_image_data img_data = {0};
449 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
452 if (!img_data.ramdisk_size)
455 * Android tools can generate a boot.img with default load address
456 * or 0, even though it doesn't really make a lot of sense, and it
457 * might be valid on some platforms, we treat that address as
458 * the default value for this field, and try to pass ramdisk
459 * in place if possible.
461 if (img_data.header_version > 2) {
462 /* Ramdisk can't be used in-place, copy it to ramdisk_addr_r */
463 if (img_data.ramdisk_addr == ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR) {
464 ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0);
466 printf("Invalid ramdisk_addr_r to copy ramdisk into\n");
470 ramdisk_ptr = img_data.ramdisk_addr;
472 *rd_data = ramdisk_ptr;
473 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
474 img_data.vendor_ramdisk_size);
475 ramdisk_ptr += img_data.vendor_ramdisk_size;
476 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
477 img_data.boot_ramdisk_size);
478 ramdisk_ptr += img_data.boot_ramdisk_size;
479 if (img_data.bootconfig_size) {
481 (ramdisk_ptr), (void *)img_data.bootconfig_addr,
482 img_data.bootconfig_size);
485 /* Ramdisk can be used in-place, use current ptr */
486 if (img_data.ramdisk_addr == 0 ||
487 img_data.ramdisk_addr == ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR) {
488 *rd_data = img_data.ramdisk_ptr;
490 ramdisk_ptr = img_data.ramdisk_addr;
491 *rd_data = ramdisk_ptr;
492 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
493 img_data.ramdisk_size);
497 printf("RAM disk load addr 0x%08lx size %u KiB\n",
498 *rd_data, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
500 *rd_len = img_data.ramdisk_size;
504 int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
506 struct andr_image_data img_data;
508 if (!android_image_get_data(hdr, NULL, &img_data))
511 if (img_data.header_version > 2) {
512 printf("Second stage bootloader is only supported for boot image version <= 2\n");
516 if (!img_data.second_size) {
517 *second_data = *second_len = 0;
521 *second_data = img_data.second_ptr;
523 printf("second address is 0x%lx\n",*second_data);
525 *second_len = img_data.second_size;
530 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
531 * @hdr_addr: Boot image header address
532 * @addr: If not NULL, will contain address of recovery DTBO image
533 * @size: If not NULL, will contain size of recovery DTBO image
535 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
536 * Boot Image in RAM. The format of this image is Android DTBO (see
537 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
538 * the address is obtained from this function, one can use 'adtimg' U-Boot
539 * command or android_dt_*() functions to extract desired DTBO blob.
541 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
542 * only can be found in recovery image. On A/B devices we can always rely on
543 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
544 * AOSP documentation for details.
546 * Return: true on success or false on error.
548 bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
550 const struct andr_boot_img_hdr_v0 *hdr;
554 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
555 if (!is_android_boot_image_header(hdr)) {
556 printf("Error: Boot Image header is incorrect\n");
561 if (hdr->header_version != 1 && hdr->header_version != 2) {
562 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
567 if (hdr->recovery_dtbo_size == 0) {
568 printf("Error: recovery_dtbo_size is 0\n");
573 /* Calculate the address of DTB area in boot image */
574 dtbo_img_addr = hdr_addr;
575 dtbo_img_addr += hdr->page_size;
576 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
577 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
578 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
581 *addr = dtbo_img_addr;
583 *size = hdr->recovery_dtbo_size;
591 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
592 * @hdr_addr: Boot image header address
593 * @vhdr_addr: Vendor Boot image header address
594 * @addr: Will contain the address of DTB area in boot image
596 * Return: true on success or false on fail.
598 static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
600 const struct andr_boot_img_hdr_v0 *hdr;
601 const struct andr_vnd_boot_img_hdr *v_hdr;
605 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
606 if (!is_android_boot_image_header(hdr)) {
607 printf("Error: Boot Image header is incorrect\n");
612 if (hdr->header_version < 2) {
613 printf("Error: header_version must be >= 2 to get dtb\n");
618 if (hdr->header_version == 2) {
619 if (!hdr->dtb_size) {
620 printf("Error: dtb_size is 0\n");
624 /* Calculate the address of DTB area in boot image */
625 dtb_img_addr = hdr_addr;
626 dtb_img_addr += hdr->page_size;
627 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
628 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
629 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
630 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
632 *addr = dtb_img_addr;
635 if (hdr->header_version > 2) {
636 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
637 if (!v_hdr->dtb_size) {
638 printf("Error: dtb_size is 0\n");
643 /* Calculate the address of DTB area in boot image */
644 dtb_img_addr = vhdr_addr;
645 dtb_img_addr += v_hdr->page_size;
646 if (v_hdr->vendor_ramdisk_size)
647 dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
648 *addr = dtb_img_addr;
658 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
659 * @hdr_addr: Boot image header address
660 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
661 * @index: Index of desired DTB in DTB area (starting from 0)
662 * @addr: If not NULL, will contain address to specified DTB
663 * @size: If not NULL, will contain size of specified DTB
665 * Get the address and size of DTB blob by its index in DTB area of Android
668 * Return: true on success or false on error.
670 bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
671 u32 index, ulong *addr, u32 *size)
673 struct andr_image_data img_data;
674 const struct andr_boot_img_hdr_v0 *hdr;
675 const struct andr_vnd_boot_img_hdr *vhdr;
677 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
678 if (vendor_boot_img != -1)
679 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
680 if (!android_image_get_data(hdr, vhdr, &img_data)) {
681 if (vendor_boot_img != -1)
686 if (vendor_boot_img != -1)
690 ulong dtb_img_addr; /* address of DTB part in boot image */
691 u32 dtb_img_size; /* size of DTB payload in boot image */
692 ulong dtb_addr; /* address of DTB blob with specified index */
693 u32 i; /* index iterator */
695 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
696 /* Check if DTB area of boot image is in DTBO format */
697 if (android_dt_check_header(dtb_img_addr)) {
698 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
702 /* Find out the address of DTB with specified index in concat blobs */
703 dtb_img_size = img_data.dtb_size;
705 dtb_addr = dtb_img_addr;
706 while (dtb_addr < dtb_img_addr + dtb_img_size) {
707 const struct fdt_header *fdt;
710 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
711 if (fdt_check_header(fdt) != 0) {
713 printf("Error: Invalid FDT header for index %u\n", i);
717 dtb_size = fdt_totalsize(fdt);
728 dtb_addr += dtb_size;
732 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
736 #if !defined(CONFIG_XPL_BUILD)
738 * android_print_contents - prints out the contents of the Android format image
739 * @hdr: pointer to the Android format image header
741 * android_print_contents() formats a multi line Android image contents
743 * The routine prints out Android image properties
746 * no returned results
748 void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
750 if (hdr->header_version >= 3) {
751 printf("Content print is not supported for boot image header version > 2");
754 const char * const p = IMAGE_INDENT_STRING;
755 /* os_version = ver << 11 | lvl */
756 u32 os_ver = hdr->os_version >> 11;
757 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
759 printf("%skernel size: %x\n", p, hdr->kernel_size);
760 printf("%skernel address: %x\n", p, hdr->kernel_addr);
761 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
762 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
763 printf("%ssecond size: %x\n", p, hdr->second_size);
764 printf("%ssecond address: %x\n", p, hdr->second_addr);
765 printf("%stags address: %x\n", p, hdr->tags_addr);
766 printf("%spage size: %x\n", p, hdr->page_size);
767 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
768 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
769 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
771 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
772 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
773 printf("%sname: %s\n", p, hdr->name);
774 printf("%scmdline: %s\n", p, hdr->cmdline);
775 printf("%sheader_version: %d\n", p, hdr->header_version);
777 if (hdr->header_version >= 1) {
778 printf("%srecovery dtbo size: %x\n", p,
779 hdr->recovery_dtbo_size);
780 printf("%srecovery dtbo offset: %llx\n", p,
781 hdr->recovery_dtbo_offset);
782 printf("%sheader size: %x\n", p,
786 if (hdr->header_version == 2) {
787 printf("%sdtb size: %x\n", p, hdr->dtb_size);
788 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
793 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
795 * @index: Number of DTB blob in DTB area.
797 * Return: true on success or false on error.
799 static bool android_image_print_dtb_info(const struct fdt_header *fdt,
805 const char *compatible;
807 root_node_off = fdt_path_offset(fdt, "/");
808 if (root_node_off < 0) {
809 printf("Error: Root node not found\n");
813 fdt_size = fdt_totalsize(fdt);
814 compatible = fdt_getprop(fdt, root_node_off, "compatible",
816 model = fdt_getprop(fdt, root_node_off, "model", NULL);
818 printf(" - DTB #%u:\n", index);
819 printf(" (DTB)size = %d\n", fdt_size);
820 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
821 printf(" (DTB)compatible = %s\n",
822 compatible ? compatible : "(unknown)");
828 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
829 * @hdr_addr: Boot image header address
831 * DTB payload in Android Boot Image v2+ can be in one of following formats:
832 * 1. Concatenated DTB blobs
833 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
835 * This function does next:
836 * 1. Prints out the format used in DTB area
837 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
840 * Return: true on success or false on error.
842 bool android_image_print_dtb_contents(ulong hdr_addr)
844 const struct andr_boot_img_hdr_v0 *hdr;
846 ulong dtb_img_addr; /* address of DTB part in boot image */
847 u32 dtb_img_size; /* size of DTB payload in boot image */
848 ulong dtb_addr; /* address of DTB blob with specified index */
849 u32 i; /* index iterator */
851 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
855 /* Check if DTB area of boot image is in DTBO format */
856 if (android_dt_check_header(dtb_img_addr)) {
857 printf("## DTB area contents (DTBO format):\n");
858 android_dt_print_contents(dtb_img_addr);
862 printf("## DTB area contents (concat format):\n");
864 /* Iterate over concatenated DTB blobs */
865 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
866 dtb_img_size = hdr->dtb_size;
869 dtb_addr = dtb_img_addr;
870 while (dtb_addr < dtb_img_addr + dtb_img_size) {
871 const struct fdt_header *fdt;
874 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
875 if (fdt_check_header(fdt) != 0) {
877 printf("Error: Invalid FDT header for index %u\n", i);
881 res = android_image_print_dtb_info(fdt, i);
887 dtb_size = fdt_totalsize(fdt);
889 dtb_addr += dtb_size;