summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorTom Rini <[email protected]>2023-04-05 10:40:05 -0400
committerTom Rini <[email protected]>2023-04-05 10:40:05 -0400
commit25eeda170c5e533ca0e3837c8b2d7404cdd749d1 (patch)
tree3ed815ab6105a6655fe25c91dace40c47336f8c6 /include
parent698c2bd364ce4122a0d0db82b5a8d842186b2fa4 (diff)
parentf4449397551a82f0c1d9714d648f1efb90d56962 (diff)
Merge branch '2023-04-04-android-image-v3-v4-support'
To quote the author: * This is based on Roman Stratiienko's work to support boot image header version 3 and 4. * This supports the new boot image headers v3, v4 and bootconfig feature. https://source.android.com/docs/core/architecture/bootloader/boot-image-header https://source.android.com/docs/core/architecture/bootloader/implementing-bootconfig - Tested on Amlogic Khadas vim3l, a reference board for Android Open Source Project https://www.khadas.com/vim3l And on AM625 Texas Instruments board with 5.10 linux kernel Main changes : - New partition : vendor boot, with a specific vendor ramdisk - DTB is stored in the vendor boot partition - The generic ramdisk is placed after the vendor ramdisk - Bootconfig feature support Here is a link to see the related android boot flow changes on KHADAS vim3l as an example: https://gitlab.baylibre.com/baylibre/amlogic/atv/u-boot/-/commits/souajih/BootImagev4/
Diffstat (limited to 'include')
-rw-r--r--include/android_image.h228
-rw-r--r--include/image.h173
2 files changed, 386 insertions, 15 deletions
diff --git a/include/android_image.h b/include/android_image.h
index 54d25af0684..d503c980b23 100644
--- a/include/android_image.h
+++ b/include/android_image.h
@@ -3,7 +3,7 @@
* This is from the Android Project,
* Repository: https://android.googlesource.com/platform/system/tools/mkbootimg
* File: include/bootimg/bootimg.h
- * Commit: e55998a0f2b61b685d5eb4a486ca3a0c680b1a2f
+ * Commit: cce5b1923e3cd2fcb765b512610bdc5c42bc501d
*
* Copyright (C) 2007 The Android Open Source Project
*/
@@ -14,15 +14,70 @@
#include <linux/compiler.h>
#include <linux/types.h>
+#define ANDR_GKI_PAGE_SIZE 4096
#define ANDR_BOOT_MAGIC "ANDROID!"
#define ANDR_BOOT_MAGIC_SIZE 8
#define ANDR_BOOT_NAME_SIZE 16
#define ANDR_BOOT_ARGS_SIZE 512
#define ANDR_BOOT_EXTRA_ARGS_SIZE 1024
+#define VENDOR_BOOT_MAGIC "VNDRBOOT"
+#define ANDR_VENDOR_BOOT_MAGIC_SIZE 8
+#define ANDR_VENDOR_BOOT_ARGS_SIZE 2048
+#define ANDR_VENDOR_BOOT_NAME_SIZE 16
-/* The bootloader expects the structure of andr_img_hdr with header
+#define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
+#define BOOTCONFIG_MAGIC_SIZE 12
+#define BOOTCONFIG_SIZE_SIZE 4
+#define BOOTCONFIG_CHECKSUM_SIZE 4
+#define BOOTCONFIG_TRAILER_SIZE BOOTCONFIG_MAGIC_SIZE + \
+ BOOTCONFIG_SIZE_SIZE + \
+ BOOTCONFIG_CHECKSUM_SIZE
+
+struct andr_boot_img_hdr_v3 {
+ u8 magic[ANDR_BOOT_MAGIC_SIZE];
+
+ u32 kernel_size; /* size in bytes */
+ u32 ramdisk_size; /* size in bytes */
+
+ u32 os_version;
+
+ u32 header_size; /* size of boot image header in bytes */
+ u32 reserved[4];
+ u32 header_version; /* offset remains constant for version check */
+
+ u8 cmdline[ANDR_BOOT_ARGS_SIZE + ANDR_BOOT_EXTRA_ARGS_SIZE];
+ /* for boot image header v4 only */
+ u32 signature_size; /* size in bytes */
+};
+
+struct andr_vnd_boot_img_hdr {
+ u8 magic[ANDR_VENDOR_BOOT_MAGIC_SIZE];
+ u32 header_version;
+ u32 page_size; /* flash page size we assume */
+
+ u32 kernel_addr; /* physical load addr */
+ u32 ramdisk_addr; /* physical load addr */
+
+ u32 vendor_ramdisk_size; /* size in bytes */
+
+ u8 cmdline[ANDR_VENDOR_BOOT_ARGS_SIZE];
+
+ u32 tags_addr; /* physical addr for kernel tags */
+
+ u8 name[ANDR_VENDOR_BOOT_NAME_SIZE]; /* asciiz product name */
+ u32 header_size; /* size of vendor boot image header in bytes */
+ u32 dtb_size; /* size of dtb image */
+ u64 dtb_addr; /* physical load address */
+ /* for boot image header v4 only */
+ u32 vendor_ramdisk_table_size; /* size in bytes for the vendor ramdisk table */
+ u32 vendor_ramdisk_table_entry_num; /* number of entries in the vendor ramdisk table */
+ u32 vendor_ramdisk_table_entry_size; /* size in bytes for a vendor ramdisk table entry */
+ u32 bootconfig_size; /* size in bytes for the bootconfig section */
+};
+
+/* The bootloader expects the structure of andr_boot_img_hdr_v0 with header
* version 0 to be as follows: */
-struct andr_img_hdr {
+struct andr_boot_img_hdr_v0 {
/* Must be ANDR_BOOT_MAGIC. */
char magic[ANDR_BOOT_MAGIC_SIZE];
@@ -136,4 +191,171 @@ struct andr_img_hdr {
* else: jump to kernel_addr
*/
+/* When the boot image header has a version of 3, the structure of the boot
+ * image is as follows:
+ *
+ * +---------------------+
+ * | boot header | 4096 bytes
+ * +---------------------+
+ * | kernel | m pages
+ * +---------------------+
+ * | ramdisk | n pages
+ * +---------------------+
+ *
+ * m = (kernel_size + 4096 - 1) / 4096
+ * n = (ramdisk_size + 4096 - 1) / 4096
+ *
+ * Note that in version 3 of the boot image header, page size is fixed at 4096 bytes.
+ *
+ * The structure of the vendor boot image (introduced with version 3 and
+ * required to be present when a v3 boot image is used) is as follows:
+ *
+ * +---------------------+
+ * | vendor boot header | o pages
+ * +---------------------+
+ * | vendor ramdisk | p pages
+ * +---------------------+
+ * | dtb | q pages
+ * +---------------------+
+ * o = (2112 + page_size - 1) / page_size
+ * p = (vendor_ramdisk_size + page_size - 1) / page_size
+ * q = (dtb_size + page_size - 1) / page_size
+ *
+ * 0. all entities in the boot image are 4096-byte aligned in flash, all
+ * entities in the vendor boot image are page_size (determined by the vendor
+ * and specified in the vendor boot image header) aligned in flash
+ * 1. kernel, ramdisk, vendor ramdisk, and DTB are required (size != 0)
+ * 2. load the kernel and DTB at the specified physical address (kernel_addr,
+ * dtb_addr)
+ * 3. load the vendor ramdisk at ramdisk_addr
+ * 4. load the generic ramdisk immediately following the vendor ramdisk in
+ * memory
+ * 5. set up registers for kernel entry as required by your architecture
+ * 6. if the platform has a second stage bootloader jump to it (must be
+ * contained outside boot and vendor boot partitions), otherwise
+ * jump to kernel_addr
+ */
+
+/* When the boot image header has a version of 4, the structure of the boot
+ * image is as follows:
+ *
+ * +---------------------+
+ * | boot header | 4096 bytes
+ * +---------------------+
+ * | kernel | m pages
+ * +---------------------+
+ * | ramdisk | n pages
+ * +---------------------+
+ * | boot signature | g pages
+ * +---------------------+
+ *
+ * m = (kernel_size + 4096 - 1) / 4096
+ * n = (ramdisk_size + 4096 - 1) / 4096
+ * g = (signature_size + 4096 - 1) / 4096
+ *
+ * Note that in version 4 of the boot image header, page size is fixed at 4096
+ * bytes.
+ *
+ * The structure of the vendor boot image version 4, which is required to be
+ * present when a version 4 boot image is used, is as follows:
+ *
+ * +------------------------+
+ * | vendor boot header | o pages
+ * +------------------------+
+ * | vendor ramdisk section | p pages
+ * +------------------------+
+ * | dtb | q pages
+ * +------------------------+
+ * | vendor ramdisk table | r pages
+ * +------------------------+
+ * | bootconfig | s pages
+ * +------------------------+
+ *
+ * o = (2128 + page_size - 1) / page_size
+ * p = (vendor_ramdisk_size + page_size - 1) / page_size
+ * q = (dtb_size + page_size - 1) / page_size
+ * r = (vendor_ramdisk_table_size + page_size - 1) / page_size
+ * s = (vendor_bootconfig_size + page_size - 1) / page_size
+ *
+ * Note that in version 4 of the vendor boot image, multiple vendor ramdisks can
+ * be included in the vendor boot image. The bootloader can select a subset of
+ * ramdisks to load at runtime. To help the bootloader select the ramdisks, each
+ * ramdisk is tagged with a type tag and a set of hardware identifiers
+ * describing the board, soc or platform that this ramdisk is intended for.
+ *
+ * The vendor ramdisk section is consist of multiple ramdisk images concatenated
+ * one after another, and vendor_ramdisk_size is the size of the section, which
+ * is the total size of all the ramdisks included in the vendor boot image.
+ *
+ * The vendor ramdisk table holds the size, offset, type, name and hardware
+ * identifiers of each ramdisk. The type field denotes the type of its content.
+ * The vendor ramdisk names are unique. The hardware identifiers are specified
+ * in the board_id field in each table entry. The board_id field is consist of a
+ * vector of unsigned integer words, and the encoding scheme is defined by the
+ * hardware vendor.
+ *
+ * For the different type of ramdisks, there are:
+ * - VENDOR_RAMDISK_TYPE_NONE indicates the value is unspecified.
+ * - VENDOR_RAMDISK_TYPE_PLATFORM ramdisks contain platform specific bits, so
+ * the bootloader should always load these into memory.
+ * - VENDOR_RAMDISK_TYPE_RECOVERY ramdisks contain recovery resources, so
+ * the bootloader should load these when booting into recovery.
+ * - VENDOR_RAMDISK_TYPE_DLKM ramdisks contain dynamic loadable kernel
+ * modules.
+ *
+ * Version 4 of the vendor boot image also adds a bootconfig section to the end
+ * of the image. This section contains Boot Configuration parameters known at
+ * build time. The bootloader is responsible for placing this section directly
+ * after the generic ramdisk, followed by the bootconfig trailer, before
+ * entering the kernel.
+ *
+ * 0. all entities in the boot image are 4096-byte aligned in flash, all
+ * entities in the vendor boot image are page_size (determined by the vendor
+ * and specified in the vendor boot image header) aligned in flash
+ * 1. kernel, ramdisk, and DTB are required (size != 0)
+ * 2. load the kernel and DTB at the specified physical address (kernel_addr,
+ * dtb_addr)
+ * 3. load the vendor ramdisks at ramdisk_addr
+ * 4. load the generic ramdisk immediately following the vendor ramdisk in
+ * memory
+ * 5. load the bootconfig immediately following the generic ramdisk. Add
+ * additional bootconfig parameters followed by the bootconfig trailer.
+ * 6. set up registers for kernel entry as required by your architecture
+ * 7. if the platform has a second stage bootloader jump to it (must be
+ * 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 */
+ ulong vendor_ramdisk_ptr; /* vendor ramdisk address */
+ u32 vendor_ramdisk_size; /* vendor ramdisk size*/
+ 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 */
+
+ ulong bootconfig_addr; /* bootconfig image address */
+ ulong bootconfig_size; /* bootconfig image size */
+
+ 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 */
+ u32 vendor_boot_img_total_size; /* vendor boot image size */
+};
+
#endif
diff --git a/include/image.h b/include/image.h
index 7717a4c13d3..456197d6fda 100644
--- a/include/image.h
+++ b/include/image.h
@@ -1733,25 +1733,174 @@ 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;
-struct andr_img_hdr;
-int android_image_check_header(const struct andr_img_hdr *hdr);
-int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
+/**
+ * android_image_get_data() - Parse Android boot images
+ *
+ * This is used to parse boot and vendor-boot header into
+ * andr_image_data generic structure.
+ *
+ * @boot_hdr: Pointer to boot image header
+ * @vendor_boot_hdr: Pointer to vendor 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, const void *vendor_boot_hdr,
+ struct andr_image_data *data);
+
+struct andr_boot_img_hdr_v0;
+
+/**
+ * android_image_get_kernel() - Processes kernel part of Android boot images
+ *
+ * This function returns the os image's start address and length. Also,
+ * it appends the kernel command line to the bootargs env variable.
+ *
+ * @hdr: Pointer to image header, which is at the start
+ * of the image.
+ * @vendor_boot_img : Pointer to vendor boot image header
+ * @verify: Checksum verification flag. Currently unimplemented.
+ * @os_data: Pointer to a ulong variable, will hold os data start
+ * address.
+ * @os_len: Pointer to a ulong variable, will hold os data length.
+ * Return: Zero, os start address and length on success,
+ * otherwise on failure.
+ */
+int android_image_get_kernel(const void *hdr,
+ const void *vendor_boot_img, int verify,
ulong *os_data, ulong *os_len);
-int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
+
+/**
+ * android_image_get_ramdisk() - Extracts the ramdisk load address and its size
+ *
+ * This extracts the load address of the ramdisk and its size
+ *
+ * @hdr: Pointer to image header
+ * @vendor_boot_img : Pointer to vendor boot image header
+ * @rd_data: Pointer to a ulong variable, will hold ramdisk address
+ * @rd_len: Pointer to a ulong variable, will hold ramdisk length
+ * Return: 0 if succeeded, -1 if ramdisk size is 0
+ */
+int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
ulong *rd_data, ulong *rd_len);
-int android_image_get_second(const struct andr_img_hdr *hdr,
- ulong *second_data, ulong *second_len);
+
+/**
+ * android_image_get_second() - Extracts the secondary bootloader address
+ * and its size
+ *
+ * This extracts the address of the secondary bootloader and its size
+ *
+ * @hdr: Pointer to image header
+ * @second_data: Pointer to a ulong variable, will hold secondary bootloader address
+ * @second_len : Pointer to a ulong variable, will hold secondary bootloader length
+ * Return: 0 if succeeded, -1 if secondary bootloader size is 0
+ */
+int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len);
bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size);
-bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
- u32 *size);
-ulong android_image_get_end(const struct andr_img_hdr *hdr);
-ulong android_image_get_kload(const struct andr_img_hdr *hdr);
-ulong android_image_get_kcomp(const struct andr_img_hdr *hdr);
-void android_print_contents(const struct andr_img_hdr *hdr);
+
+/**
+ * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
+ * @hdr_addr: Boot image header address
+ * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
+ * @index: Index of desired DTB in DTB area (starting from 0)
+ * @addr: If not NULL, will contain address to specified DTB
+ * @size: If not NULL, will contain size of specified DTB
+ *
+ * Get the address and size of DTB blob by its index in DTB area of Android
+ * Boot Image in RAM.
+ *
+ * Return: true on success or false on error.
+ */
+bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
+ u32 index, ulong *addr, u32 *size);
+
+/**
+ * android_image_get_end() - Get the end of Android boot image
+ *
+ * This returns the end address of Android boot image address
+ *
+ * @hdr: Pointer to image header
+ * @vendor_boot_img : Pointer to vendor boot image header
+ * Return: The end address of Android boot image
+ */
+ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
+ const void *vendor_boot_img);
+
+/**
+ * android_image_get_kload() - Get the kernel load address
+ *
+ * This returns the kernel load address. The load address is extracted
+ * from the boot image header or the "kernel_addr_r" environment variable
+ *
+ * @hdr: Pointer to image header
+ * @vendor_boot_img : Pointer to vendor boot image header
+ * Return: The kernel load address
+ */
+ulong android_image_get_kload(const void *hdr,
+ const void *vendor_boot_img);
+
+/**
+ * android_image_get_kcomp() - Get kernel compression type
+ *
+ * This gets the kernel compression type from the boot image header
+ *
+ * @hdr: Pointer to image header
+ * @vendor_boot_img : Pointer to vendor boot image header
+ * Return: Kernel compression type
+ */
+ulong android_image_get_kcomp(const void *hdr,
+ const void *vendor_boot_img);
+
+/**
+ * android_print_contents() - Prints out the contents of the Android format image
+ *
+ * This formats a multi line Android image contents description.
+ * The routine prints out Android image properties
+ *
+ * @hdr: Pointer to the Android format image header
+ * Return: no returned results
+ */
+void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr);
bool android_image_print_dtb_contents(ulong hdr_addr);
/**
+ * is_android_boot_image_header() - Check the magic of boot image
+ *
+ * This checks the header of Android boot image and verifies the
+ * magic is "ANDROID!"
+ *
+ * @hdr: Pointer to boot image
+ * Return: non-zero if the magic is correct, zero otherwise
+ */
+bool is_android_boot_image_header(const void *hdr);
+
+/**
+ * is_android_vendor_boot_image_header() - Check the magic of vendor boot image
+ *
+ * This checks the header of Android vendor boot image and verifies the magic
+ * is "VNDRBOOT"
+ *
+ * @vendor_boot_img: Pointer to boot image
+ * Return: non-zero if the magic is correct, zero otherwise
+ */
+bool is_android_vendor_boot_image_header(const void *vendor_boot_img);
+
+/**
+ * get_abootimg_addr() - Get Android boot image address
+ *
+ * Return: Android boot image address
+ */
+ulong get_abootimg_addr(void);
+
+/**
+ * get_avendor_bootimg_addr() - Get Android vendor boot image address
+ *
+ * Return: Android vendor boot image address
+ */
+ulong get_avendor_bootimg_addr(void);
+
+/**
* board_fit_config_name_match() - Check for a matching board name
*
* This is used when SPL loads a FIT containing multiple device tree files