Skip to content

Commit

Permalink
libbootimg: Add support for 64bits Sony ELF format
Browse files Browse the repository at this point in the history
 * Compatibility for the ELF partition used on the
    msm8996 devices like the Xperia X Performance

 * Structures aligned with 64bits addresses,
    offsets and sizes to match Stock bootimage

 * Usage of "__attribute__((packed))" to avoid
    structures padding breakage

 * Add the VER_ELF_4 identifier
 * Properly convert 32bits -> 64bits and reverse
 * Use the 64bits structures as main data
 * Add the libbootimg_load_elf_header function
    to separate the ELF header handling

Change-Id: I40559ecc32833aed7b3ca7f3ebf8d53b9bc9b471
Signed-off-by: Adrian DC <[email protected]>
  • Loading branch information
AdrianDC committed Dec 18, 2016
1 parent 0f35e22 commit 0aaf4e8
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 54 deletions.
63 changes: 56 additions & 7 deletions include/boot_img_hdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#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 */
Expand Down Expand Up @@ -94,7 +95,7 @@ struct boot_img_hdr
uint32_t id[8]; /* timestamp / checksum / sha1 / etc */
};

struct boot_img_elf_hdr
struct boot_img_elf_hdr_32
{
/* Global structure of the Sony ELF header - Respective usual values: | 8960 | 8974 | */
uint8_t magic[8]; /* .ELF (0x00 to 0x07) | .ELF... | .ELF... | */
Expand All @@ -114,12 +115,36 @@ struct boot_img_elf_hdr
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. */
struct boot_img_elf_prog_hdr* prog; /* The program header entries. */
struct boot_img_elf_sect_hdr* sect; /* The section header entries. */
struct boot_img_elf_misc_hdr* misc; /* Miscellaneous information found in some ELF versions. */
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;
Expand All @@ -129,7 +154,7 @@ struct boot_img_elf_info
uint32_t cmdline_signature_cnt;
};

struct boot_img_elf_prog_hdr
struct boot_img_elf_prog_hdr_32
{
uint32_t type; /* type (position + 0x0 to 0x3) */
uint32_t offset; /* offset (position + 0x4 to 0x7) */
Expand All @@ -141,7 +166,19 @@ struct boot_img_elf_prog_hdr
uint32_t align; /* alignment (position + 0x1C to 0x1F)*/
};

struct boot_img_elf_sect_hdr
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;
Expand All @@ -152,6 +189,18 @@ struct boot_img_elf_sect_hdr
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 */
Expand Down
9 changes: 9 additions & 0 deletions include/libbootimg.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ int libbootimg_load_header(struct boot_img_hdr *hdr, const char *path);
*/
void libbootimg_get_elf_version(struct boot_img_elf_info *hdr_info);

/**
* Reads the image header from the given ELF file and adds the content
* to the given structure, adapting 32 bits to 64 bits if needed.
* @param hdr_info structure holding the meta-information of the given ELF file
* @param f pointer to the file descriptor of the ELF file
* @return zero on success or the error code returned by the file operations.
*/
int libbootimg_load_elf_header(struct boot_img_elf_info *hdr_info, FILE *f);

/**
* Reads the program headers from the given ELF file and adds the content
* of each header to the given structure.
Expand Down
Loading

0 comments on commit 0aaf4e8

Please sign in to comment.