Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbootimg: Fix support of Stock Sony ELF boot images #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
13 changes: 10 additions & 3 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_UNSTRIPPED_PATH := $(TARGET_OUT_EXECUTABLES_UNSTRIPPED)

LOCAL_STATIC_LIBRARIES := libc
LOCAL_CFLAGS := -DDEBUG_KMSG
LOCAL_STATIC_LIBRARIES := libc libcutils

include $(BUILD_EXECUTABLE)

# bbootimge_host
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= $(bbootimg_src_file)
Expand All @@ -36,14 +38,19 @@ LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
LOCAL_MODULE_STEM := bbootimg

LOCAL_SRC_FILES:= src/bbootimg.c src/libbootimg.c
LOCAL_SHARED_LIBRARIES := libc

include $(BUILD_EXECUTABLE)
LOCAL_CFLAGS := -DDEBUG_KMSG
LOCAL_SHARED_LIBRARIES := libc libcutils

include $(BUILD_EXECUTABLE)

# libbootimg
include $(CLEAR_VARS)

LOCAL_SRC_FILES := src/libbootimg.c
LOCAL_MODULE := libbootimg
LOCAL_MODULE_TAGS := eng

LOCAL_CFLAGS := -DDEBUG_KMSG

include $(BUILD_STATIC_LIBRARY)
179 changes: 126 additions & 53 deletions include/boot_img_hdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <stdint.h>

/*
** +-----------------+
** +-----------------+
** | boot header | 1 page
** +-----------------+
** | kernel | n pages
Expand Down Expand Up @@ -51,11 +51,25 @@

#define BOOT_MAGIC "ANDROID!"
#define BOOT_MAGIC_SIZE 8
#define BOOT_MAGIC_ELF "ELF"
#define BOOT_MAGIC_ELF_SIZE 3
#define BOOT_NAME_SIZE 16
#define BOOT_ARGS_SIZE 512

#define BOOT_MAGIC_ELF "ELF"
#define BOOT_MAGIC_ELF_SIZE 3
#define VER_ELF_1 (1 << 0)
#define VER_ELF_2 (1 << 1)
#define VER_ELF_4 (1 << 3)

#define OUT_ELF (1 << 0) /* Same output format: ELF container */
#define OUT_AND (1 << 1) /* Different output format: standard Android container */

#define ELF_PROG_KER 0
#define ELF_PROG_RAM 1
#define ELF_PROG_RPM 2
#define ELF_PROG_CMD 3

#define ELF_SECT_CMD 0

struct boot_img_hdr
{
uint8_t magic[BOOT_MAGIC_SIZE];
Expand All @@ -81,58 +95,117 @@ struct boot_img_hdr
uint32_t id[8]; /* timestamp / checksum / sha1 / etc */
};

struct boot_img_hdr_elf
struct boot_img_elf_hdr_32
{
uint8_t magic[8]; /* .ELF (0x00 to 0x07) */
uint8_t unused[8]; /* unused chars */
uint16_t type; /* boot type */
uint16_t machine; /* boot machine */
uint32_t version; /* boot version */
uint32_t entry_addr; /* boot entry */
uint32_t phoff; /* boot phoff */
uint32_t shoff; /* boot shoff */
uint32_t flags; /* boot flags */
uint16_t ehsize; /* boot ehsize */
uint16_t phentsize; /* boot phentsize */
uint16_t phnum; /* boot phnum */
uint16_t shentsize; /* boot shentsize */
uint16_t shnum; /* boot shnum */
uint16_t shstrndx; /* boot shstrndx */
uint32_t kernel_type; /* kernel type (0x34 to 0x37) */
uint32_t kernel_offset; /* kernel offset (0x38 to 0x3B) */
uint32_t kernel_vaddr; /* kernel address (0x3C to 0x3F) */
uint32_t kernel_paddr; /* kernel address duplicate */
uint32_t kernel_size; /* kernel size (0x44 to 0x47) */
uint32_t kernel_msize; /* kernel size duplicate */
uint32_t kernel_flags; /* kernel flags (0x4C to 0x4F) */
uint32_t kernel_align; /* kernel alignment */
uint32_t ramdisk_type; /* kernel type (0x54) */
uint32_t ramdisk_offset; /* ramdisk offset (0x58 to 0x5B) */
uint32_t ramdisk_vaddr; /* ramdisk address (0x5C to 0x5F) */
uint32_t ramdisk_paddr; /* ramdisk address duplicate */
uint32_t ramdisk_size; /* ramdisk size (0x64 to 0x67) */
uint32_t ramdisk_msize; /* ramdisk size duplicate */
uint32_t ramdisk_flags; /* ramdisk flags (0x6C to 0x6F) */
uint32_t ramdisk_align; /* cmdline alignment */
uint32_t rpm_type; /* rpm type (0x74 to 0x77) */
uint32_t rpm_offset; /* rpm offset (0x78 to 0x7B) */
uint32_t rpm_vaddr; /* rpm address (0x7C to 0x7F) */
uint32_t rpm_paddr; /* rpm address duplicate */
uint32_t rpm_size; /* rpm size (0x84 to 0x87) */
uint32_t rpm_msize; /* rpm size duplicate */
uint32_t rpm_flags; /* rpm flags (0x8C to 0x8F) */
uint32_t rpm_align; /* rpm alignment */
uint32_t cmd_type; /* cmdline type (0x94 to 0x97) */
uint32_t cmd_offset; /* cmdline offset (0x98 to 0x9B) */
uint32_t cmd_vaddr; /* cmdline address (0x9C to 0x9F) */
uint32_t cmd_paddr; /* cmdline address duplicate */
uint32_t cmd_size; /* cmdline size (0xA4 to 0xA7) */
uint32_t cmd_msize; /* cmdline size duplicate */
uint32_t cmd_flags; /* cmdline flags (0xAC to 0xAF) */
uint32_t cmd_align; /* cmdline alignment */
uint8_t name[BOOT_NAME_SIZE]; /* added - asciiz product name */
/* Global structure of the Sony ELF header - Respective usual values: | 8960 | 8974 | */
uint8_t magic[8]; /* .ELF (0x00 to 0x07) | .ELF... | .ELF... | */
uint8_t unused[8]; /* unused chars (0x08 to 0x0F) | 0x00 | 0x00 | */
uint16_t type; /* boot type (0x10 to 0x11) | 0x02 | 0x02 | */
uint16_t machine; /* boot machine (0x12 to 0x13) | 0x28 | 0x28 | */
uint32_t version; /* boot version (0x14 to 0x17) | 0x01 | 0x01 | */
uint32_t entry_addr; /* boot entry (0x18 to 0x1B) | 0x80208000 | 0x00008000 | */
uint32_t phoff; /* boot phoff (0x1C to 0x1F) | 0x34 | 0x34 | */
uint32_t shoff; /* boot shoff (0x20 to 0x23) | 0x00000000 | 0x00B3.... | */
uint32_t flags; /* boot flags (0x24 to 0x27) | 0x00 | 0x00 | */
uint16_t ehsize; /* boot ehsize (0x28 to 0x29) | 0x34 | 0x34 | */
uint16_t phentsize; /* boot phentsize (0x2A to 0x2B) | 0x20 | 0x20 | */
uint16_t phnum; /* boot phnum (0x2C to 0x2D) | 0x05/0x04 | 0x03 | */
uint16_t shentsize; /* boot shentsize (0x2E to 0x2F) | 0x00 | 0x28 | */
uint16_t shnum; /* boot shnum (0x30 to 0x31) | 0x00 | 0x01 | */
uint16_t shstrndx; /* boot shstrndx (0x32 to 0x33) | 0x00 | 0x00 | */
};

struct __attribute__((packed)) boot_img_elf_hdr
{
/* Global structure of the Sony ELF header - Respective usual values: | 8996 | */
uint8_t magic[8]; /* .ELF (0x00 to 0x07) | .ELF... | */
uint8_t unused[8]; /* unused chars (0x08 to 0x0F) | 0x00 | */
uint16_t type; /* boot type (0x10 to 0x11) | 0x02 | */
uint16_t machine; /* boot machine (0x12 to 0x13) | 0x28 | */
uint32_t version; /* boot version (0x14 to 0x17) | 0x01 | */
uint64_t entry_addr; /* boot entry (0x18 to 0x1F) | 0x80080000 | */
uint64_t phoff; /* boot phoff (0x20 to 0x27) | 0x40 | */
uint64_t shoff; /* boot shoff (0x28 to 0x2F) | 0x0214.... | */
uint32_t flags; /* boot flags (0x30 to 0x33) | 0x00 | */
uint16_t ehsize; /* boot ehsize (0x34 to 0x35) | 0x40 | */
uint16_t phentsize; /* boot phentsize (0x36 to 0x37) | 0x38 | */
uint16_t phnum; /* boot phnum (0x38 to 0x39) | 0x03 | */
uint16_t shentsize; /* boot shentsize (0x3A to 0x3B) | 0x40 | */
uint16_t shnum; /* boot shnum (0x3C to 0x3D) | 0x01 | */
uint16_t shstrndx; /* boot shstrndx (0x3E to 0x3F) | 0x00 | */
};

struct boot_img_elf_info
{
struct boot_img_elf_hdr hdr; /* The ELF file header (64 bits). */
struct boot_img_elf_hdr_32 hdr_32; /* The ELF file header (32 bits). */
struct boot_img_elf_prog_hdr* prog; /* The program header entries (64 bits). */
struct boot_img_elf_prog_hdr_32* prog_32; /* The program header entries (32 bits). */
struct boot_img_elf_sect_hdr* sect; /* The section header entries (64 bits). */
struct boot_img_elf_sect_hdr_32* sect_32; /* The section header entries (32 bits). */
struct boot_img_elf_misc_hdr* misc; /* Miscellaneous information found in some ELF versions. */
uint8_t elf_architecture;
uint8_t elf_version;
uint8_t elf_out_format;
uint32_t cmdline_size;
uint8_t cmdline_metadata[8];
uint32_t cmdline_metadata_cnt;
uint8_t* cmdline_signature;
uint32_t cmdline_signature_cnt;
};

typedef struct boot_img_hdr boot_img_hdr;
struct boot_img_elf_prog_hdr_32
{
uint32_t type; /* type (position + 0x0 to 0x3) */
uint32_t offset; /* offset (position + 0x4 to 0x7) */
uint32_t vaddr; /* address (position + 0x8 to 0xB) */
uint32_t paddr; /* address duplicate (position + 0xC to 0xF) */
uint32_t size; /* size (position + 0x10 to 0x13) */
uint32_t msize; /* size duplicate (position + 0x14 to 0x17) */
uint32_t flags; /* flags (position + 0x18 to 0x1B) */
uint32_t align; /* alignment (position + 0x1C to 0x1F)*/
};

struct __attribute__((packed)) boot_img_elf_prog_hdr
{
uint64_t type; /* type (position + 0x0 to 0x3) */
uint64_t offset; /* offset (position + 0x4 to 0x7) */
uint64_t vaddr; /* address (position + 0x8 to 0xF) */
uint64_t paddr; /* address duplicate (position + 0x10 to 0x17) */
uint64_t size; /* size (position + 0x18 to 0x1F) */
uint64_t msize; /* size duplicate (position + 0x20 to 0x23) */
uint32_t flags; /* flags (position + 0x24 to 0x27) */
uint32_t align; /* alignment (position + 0x28 to 0x2B)*/
};

struct boot_img_elf_sect_hdr_32
{
uint32_t name;
uint32_t type;
uint32_t flags;
uint32_t addr;
uint32_t offset;
uint32_t size;
uint8_t misc[16];
};

struct __attribute__((packed)) boot_img_elf_sect_hdr
{
uint32_t name;
uint64_t type;
uint32_t flags;
uint64_t addr;
uint64_t offset;
uint64_t size;
uint8_t misc[16];
uint8_t padding[8];
};

struct boot_img_elf_misc_hdr
{
uint8_t* data; /* header additional data */
uint32_t data_size; /* header additional size */
uint8_t name[BOOT_NAME_SIZE]; /* added - asciiz product name */
};

#endif
Loading