app: aboot: Change to add support functions for appended recovery dtbo header.
Change to add functions to support appended recovery dtbo header,
in boot image.
Change-Id: Ibd439a6c90fe3a2fe547dafc453bd76e4de83acc
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 02644c2..930621e 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -248,6 +248,9 @@
static uint32_t dt_size = 0;
static char *vbcmdline;
static bootinfo info = {0};
+static void *recovery_dtbo_buf = NULL;
+static uint32_t recovery_dtbo_size = 0;
+
/* Assuming unauthorized kernel image by default */
static int auth_kernel_img = 0;
static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
@@ -345,7 +348,7 @@
extern int fastboot_trigger(void);
#endif
-static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
+static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
{
/* overwrite the destination of specified for the project */
#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
@@ -1425,10 +1428,18 @@
}
}
+/* Function to return recovery appended dtbo buffer info */
+void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
+{
+ *dtbo_size = recovery_dtbo_size;
+ *dtbo_buf = recovery_dtbo_buf;
+ return;
+}
+
int boot_linux_from_mmc(void)
{
- struct boot_img_hdr *hdr = (void*) buf;
- struct boot_img_hdr *uhdr;
+ boot_img_hdr *hdr = (void*) buf;
+ boot_img_hdr *uhdr;
unsigned offset = 0;
int rcode;
unsigned long long ptn = 0;
@@ -1448,6 +1459,7 @@
unsigned char *kernel_start_addr = NULL;
unsigned int kernel_size = 0;
unsigned int patched_kernel_hdr_size = 0;
+ uint64_t image_size = 0;
int rc;
#if VERIFIED_BOOT_2
int status;
@@ -1482,7 +1494,7 @@
boot_into_ffbm = true;
} else
boot_into_ffbm = false;
- uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
+ uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
dprintf(INFO, "Unified boot method!\n");
hdr = uhdr;
@@ -1499,7 +1511,8 @@
index = partition_get_index(ptn_name);
ptn = partition_get_offset(index);
- if(ptn == 0) {
+ image_size = partition_get_size(index);
+ if(ptn == 0 || image_size == 0) {
dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
return -1;
}
@@ -1541,6 +1554,8 @@
#ifndef OSVERSION_IN_BOOTIMAGE
dt_size = hdr->dt_size;
#endif
+ dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
+
dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
@@ -1555,6 +1570,63 @@
imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
#endif
+#ifdef OSVERSION_IN_BOOTIMAGE
+ /* If header version is ONE and booting into recovery,
+ dtbo is appended with recovery image.
+ Doing following:
+ * Validating the recovery offset and size.
+ * Extracting recovery dtbo to be used as dtbo.
+ */
+ if (boot_into_recovery &&
+ hdr->header_version == BOOT_HEADER_VERSION_ONE)
+ {
+ struct boot_img_hdr_v1 *hdr1 =
+ (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
+
+ if ((hdr1->header_size !=
+ sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
+ {
+ dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
+ return -1;
+ }
+
+ if (UINT_MAX < (hdr1->recovery_dtbo_offset + hdr1->recovery_dtbo_size)) {
+ dprintf(CRITICAL,
+ "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
+ return -1;
+ }
+
+ if (hdr1->recovery_dtbo_size > MAX_SUPPORTED_DTBO_IMG_BUF) {
+ dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", hdr1->recovery_dtbo_size,
+ MAX_SUPPORTED_DTBO_IMG_BUF);
+ return -1;
+ }
+
+ if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_size))
+ {
+ dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
+ return -1;
+ }
+
+ if (hdr1->recovery_dtbo_offset + recovery_dtbo_size > image_size)
+ {
+ dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
+ " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
+ hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
+ return -1;
+ }
+
+ recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
+ recovery_dtbo_size = hdr1->recovery_dtbo_size;
+ imagesize_actual += recovery_dtbo_size;
+
+ dprintf(SPEW, "Header version: %d\n", hdr->header_version);
+ dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
+ dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
+
+ }
+#endif
+
#if VERIFIED_BOOT
boot_verifier_init();
#endif
@@ -1663,6 +1735,7 @@
++info.num_loaded_images;
}
+ info.header_version = hdr->header_version;
info.multi_slot_boot = partition_multislot_is_supported();
info.bootreason_alarm = boot_reason_alarm;
info.bootinto_recovery = boot_into_recovery;
@@ -1943,7 +2016,7 @@
int boot_linux_from_flash(void)
{
- struct boot_img_hdr *hdr = (void*) buf;
+ boot_img_hdr *hdr = (void*) buf;
struct ptentry *ptn;
struct ptable *ptable;
unsigned offset = 0;
@@ -1966,7 +2039,7 @@
#endif
if (target_is_emmc_boot()) {
- hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
+ hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
return -1;
@@ -2751,7 +2824,7 @@
unsigned char *best_match_dt_addr = NULL;
int rc;
- struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
+ boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
#ifndef OSVERSION_IN_BOOTIMAGE
dt_size = hdr->dt_size;
@@ -2839,7 +2912,7 @@
unsigned second_actual;
uint32_t image_actual;
uint32_t dt_actual = 0;
- struct boot_img_hdr *hdr = NULL;
+ boot_img_hdr *hdr = NULL;
struct kernel64_hdr *kptr = NULL;
char *ptr = ((char*) data);
int ret = 0;
@@ -2877,7 +2950,7 @@
goto boot_failed;
}
- hdr = (struct boot_img_hdr *)data;
+ hdr = (boot_img_hdr *)data;
/* ensure commandline is terminated */
hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
diff --git a/app/aboot/bootimg.h b/app/aboot/bootimg.h
index 7dfbef8..a07db93 100644
--- a/app/aboot/bootimg.h
+++ b/app/aboot/bootimg.h
@@ -31,16 +31,19 @@
#ifndef _BOOT_IMAGE_H_
#define _BOOT_IMAGE_H_
-typedef struct boot_img_hdr boot_img_hdr;
-
#define BOOT_MAGIC "ANDROID!"
#define BOOT_MAGIC_SIZE 8
#define BOOT_NAME_SIZE 16
#define BOOT_ARGS_SIZE 512
#define BOOT_IMG_MAX_PAGE_SIZE 4096
+#define BOOT_EXTRA_ARGS_SIZE 1024
-struct boot_img_hdr
-{
+#define BOOT_HEADER_VERSION_ZERO 0
+/* Struct def for boot image header
+ * Bootloader expects the structure of boot_img_hdr with header version
+ * BOOT_HEADER_VERSION_ZERO to be as follows:
+ */
+struct boot_img_hdr_v0 {
unsigned char magic[BOOT_MAGIC_SIZE];
unsigned kernel_size; /* size in bytes */
@@ -55,7 +58,7 @@
unsigned tags_addr; /* physical addr for kernel tags */
unsigned page_size; /* flash page size we assume */
#if OSVERSION_IN_BOOTIMAGE
- uint32_t unused; /* future expansion: should be 0 */
+ uint32_t header_version; /* version for the boot image header */
uint32_t os_version; /* version << 11 | patch_level */
#else
unsigned dt_size; /* device_tree in bytes */
@@ -66,7 +69,24 @@
unsigned char cmdline[BOOT_ARGS_SIZE];
unsigned id[8]; /* timestamp / checksum / sha1 / etc */
-};
+
+ /* Supplemental command line data; kept here to maintain
+ * binary compatibility with older versions of mkbootimg
+ */
+ uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
+} __attribute__((packed));
+
+/*
+ * It is expected that callers would explicitly specify which version of the
+ * boot image header they need to use.
+ */
+typedef struct boot_img_hdr_v0 boot_img_hdr;
+
+/**
+ * Offset of recovery DTBO length in a boot image header of version V1 or
+ * above.
+ */
+#define BOOT_IMAGE_HEADER_V1_RECOVERY_DTBO_SIZE_OFFSET sizeof (boot_img_hdr)
/*
** +-----------------+
@@ -78,13 +98,9 @@
** +-----------------+
** | second stage | o pages
** +-----------------+
-** | device tree | p pages
-** +-----------------+
-**
** n = (kernel_size + page_size - 1) / page_size
** m = (ramdisk_size + page_size - 1) / page_size
** o = (second_size + page_size - 1) / page_size
-** p = (dt_size + page_size - 1) / page_size
** 0. all entities are page_size aligned in flash
** 1. kernel and ramdisk are required (size != 0)
** 2. second is optional (second_size == 0 -> no second)
@@ -97,6 +113,47 @@
** else: jump to kernel_addr
*/
+#define BOOT_HEADER_VERSION_ONE 1
+
+struct boot_img_hdr_v1 {
+ uint32_t recovery_dtbo_size; /* size in bytes for recovery DTBO image */
+ uint64_t recovery_dtbo_offset; /* physical load addr */
+ uint32_t header_size;
+} __attribute__((packed));
+
+/* When the boot image header has a version of BOOT_HEADER_VERSION_ONE,
+ * the structure of the boot image is as follows:
+ *
+ * +-----------------+
+ * | boot header | 1 page
+ * +-----------------+
+ * | kernel | n pages
+ * +-----------------+
+ * | ramdisk | m pages
+ * +-----------------+
+ * | second stage | o pages
+ * +-----------------+
+ * | recovery dtbo | p pages
+ * +-----------------+
+ * n = (kernel_size + page_size - 1) / page_size
+ * m = (ramdisk_size + page_size - 1) / page_size
+ * o = (second_size + page_size - 1) / page_size
+ * p = (recovery_dtbo_size + page_size - 1) / page_size
+ *
+ * 0. all entities are page_size aligned in flash
+ * 1. kernel and ramdisk are required (size != 0)
+ * 2. recovery_dtbo is required for recovery.img
+ * in non-A/B devices(recovery_dtbo_size != 0)
+ * 3. second is optional (second_size == 0 -> no second)
+ * 4. load each element (kernel, ramdisk, second, recovery_dtbo) at
+ * the specified physical address (kernel_addr, etc)
+ * 5. prepare tags at tag_addr. kernel_args[] is
+ * appended to the kernel commandline in the tags.
+ * 6. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
+ * 7. if second_size != 0: jump to second_addr
+ * else: jump to kernel_addr
+ */
+
boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
void *ramdisk, unsigned ramdisk_size,
void *second, unsigned second_size,