[PATCH v3 05/19] android: boot: kcomp: support andr_image_data

Safae Ouajih souajih at baylibre.com
Mon Feb 6 00:50:07 CET 2023


andr_image_data structure is used as a global representation of
boot image header structure. Introduce this new structure to
support all boot header versions : v0,v1.v2.v3.v4 and to support
v3 and v4 while maitaining support for v0,v1,v2.
The need of using andr_image_data comes from the change of header
structure in both version 3 and 4.

Rework android_image_get_kcomp()  to support this new struct.

Signed-off-by: Safae Ouajih <souajih at baylibre.com>
Reviewed-by: Simon Glass <sjg at chromium.org>
Reviewed-by: Mattijs Korpershoek <mkorpershoek at baylibre.com>
---
 boot/image-android.c    | 75 ++++++++++++++++++++++++++++++++++++++++-
 include/android_image.h | 27 +++++++++++++++
 include/image.h         | 12 +++++++
 3 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/boot/image-android.c b/boot/image-android.c
index ac7cb479c1..ea05c1814f 100644
--- a/boot/image-android.c
+++ b/boot/image-android.c
@@ -18,6 +18,74 @@
 
 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
 
+static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
+						  struct andr_image_data *data)
+{
+	ulong end;
+
+	data->image_name = hdr->name;
+	data->kcmdline = hdr->cmdline;
+	data->kernel_addr = hdr->kernel_addr;
+	data->ramdisk_addr = hdr->ramdisk_addr;
+	data->header_version = hdr->header_version;
+	data->dtb_load_addr = hdr->dtb_addr;
+
+	end = (ulong)hdr;
+
+	/*
+	 * The header takes a full page, the remaining components are aligned
+	 * on page boundary
+	 */
+
+	end += hdr->page_size;
+
+	data->kernel_ptr = end;
+	data->kernel_size = hdr->kernel_size;
+	end += ALIGN(hdr->kernel_size, hdr->page_size);
+
+	data->ramdisk_ptr = end;
+	data->ramdisk_size = hdr->ramdisk_size;
+	end += ALIGN(hdr->ramdisk_size, hdr->page_size);
+
+	data->second_ptr = end;
+	data->second_size = hdr->second_size;
+	end += ALIGN(hdr->second_size, hdr->page_size);
+
+	if (hdr->header_version >= 1) {
+		data->recovery_dtbo_ptr = end;
+		data->recovery_dtbo_size = hdr->recovery_dtbo_size;
+		end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
+	}
+
+	if (hdr->header_version >= 2) {
+		data->dtb_ptr = end;
+		data->dtb_size = hdr->dtb_size;
+		end += ALIGN(hdr->dtb_size, hdr->page_size);
+	}
+
+	data->boot_img_total_size = end - (ulong)hdr;
+}
+
+bool android_image_get_data(const void *boot_hdr, struct andr_image_data *data)
+{
+	if (!boot_hdr || !data) {
+		printf("boot_hdr or data params can't be NULL\n");
+		return false;
+	}
+
+	if (!is_android_boot_image_header(boot_hdr)) {
+		printf("Incorrect boot image header\n");
+		return false;
+	}
+
+	if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2)
+		printf("Only boot image header version 2 and below are supported\n");
+	else
+		android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
+
+	return true;
+}
+
 static ulong android_image_get_kernel_addr(const struct andr_boot_img_hdr_v0 *hdr)
 {
 	/*
@@ -157,8 +225,13 @@ ulong android_image_get_kload(const struct andr_boot_img_hdr_v0 *hdr)
 
 ulong android_image_get_kcomp(const struct andr_boot_img_hdr_v0 *hdr)
 {
-	const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
+	struct andr_image_data img_data;
+	const void *p;
+
+	if (!android_image_get_data(hdr, &img_data))
+		return -EINVAL;
 
+	p = (const void *)img_data.kernel_ptr;
 	if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
 		return image_get_comp((struct legacy_img_hdr *)p);
 	else if (get_unaligned_le32(p) == LZ4F_MAGIC)
diff --git a/include/android_image.h b/include/android_image.h
index f054363001..fb3a9d9858 100644
--- a/include/android_image.h
+++ b/include/android_image.h
@@ -317,4 +317,31 @@ struct andr_boot_img_hdr_v0 {
  *    contained outside boot and vendor boot partitions), otherwise
  *    jump to kernel_addr
  */
+
+/* Private struct */
+struct andr_image_data {
+	ulong kernel_ptr;  /* kernel address */
+	u32 kernel_size;  /* size in bytes */
+	u32 ramdisk_size;  /* size in bytes */
+	u32 boot_ramdisk_size;  /* size in bytes */
+	ulong second_ptr;  /* secondary bootloader address */
+	u32 second_size;  /* secondary bootloader size */
+	ulong dtb_ptr;  /* address of dtb image */
+	u32 dtb_size;  /* size of dtb image */
+	ulong recovery_dtbo_ptr;  /* size in bytes for recovery DTBO/ACPIO image */
+	u32 recovery_dtbo_size;  /* offset to recovery dtbo/acpio in boot image */
+
+	const char *kcmdline;  /* boot kernel cmdline */
+	const char *kcmdline_extra;  /* vendor-boot extra kernel cmdline */
+	const char *image_name;  /* asciiz product name */
+
+	u32 kernel_addr;  /* physical load addr */
+	ulong ramdisk_addr;  /* physical load addr */
+	ulong ramdisk_ptr;  /* ramdisk address */
+	ulong dtb_load_addr;  /* physical load address for DTB image */
+	ulong tags_addr;  /* physical addr for kernel tags */
+	u32 header_version;  /* version of the boot image header */
+	u32 boot_img_total_size;  /* boot image size */
+};
+
 #endif
diff --git a/include/image.h b/include/image.h
index 6e67cf4817..4bf6c98ced 100644
--- a/include/image.h
+++ b/include/image.h
@@ -1733,7 +1733,19 @@ struct cipher_algo {
 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo);
 
 struct cipher_algo *image_get_cipher_algo(const char *full_name);
+struct andr_image_data;
 
+/**
+ * android_image_get_data() - Parse Android boot image
+ *
+ * This is used to parse boot image header into andr_image_data
+ * generic structure.
+ *
+ * @boot_hdr: Pointer to boot image header
+ * @data: Pointer to generic boot format structure
+ * Return: true if succeeded, false otherwise
+ */
+bool android_image_get_data(const void *boot_hdr, struct andr_image_data *data);
 struct andr_boot_img_hdr_v0;
 
 /**
-- 
2.34.1



More information about the U-Boot mailing list