From f5d054b93c6ed30b0662fd6313142d94b77c967d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 8 Oct 2019 23:49:21 -0400 Subject: [PATCH] Add support for PXA DTBs --- native/jni/magiskboot/dtb.cpp | 55 ++++++++++++++++++++++++++++---- native/jni/magiskboot/format.cpp | 4 --- native/jni/magiskboot/format.h | 3 -- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/native/jni/magiskboot/dtb.cpp b/native/jni/magiskboot/dtb.cpp index 5a8a98990..6ca6442af 100644 --- a/native/jni/magiskboot/dtb.cpp +++ b/native/jni/magiskboot/dtb.cpp @@ -10,12 +10,14 @@ extern "C" { #include #include "magiskboot.h" -#include "format.h" using namespace std; -constexpr int MAX_DEPTH = 32; -static bitset depth_set; +#define DTB_MAGIC "\xd0\x0d\xfe\xed" +#define QCDT_MAGIC "QCDT" +#define DTBH_MAGIC "DTBH" +#define PXADT_MAGIC "PXA-DT" +#define PXA_19xx_MAGIC "PXA-19xx" struct qcdt_hdr { char magic[4]; /* "QCDT" */ @@ -54,6 +56,24 @@ struct bhtable_v2 { uint32_t space; /* 0x00000020 */ } __attribute__((packed)); +struct pxadt_hdr { + char magic[6]; /* "PXA-DT" */ + uint32_t version; /* PXA-DT version */ + uint32_t num_dtbs; /* Number of DTBs */ +} __attribute__((packed)); + +struct pxa_19xx_hdr { + char magic[8]; /* "PXA-19xx" */ + uint32_t version; /* PXA-DT version */ + uint32_t num_dtbs; /* Number of DTBs */ +} __attribute__((packed)); + +struct pxa_table_v1 { + uint32_t cpu_info[2]; /* Some CPU info */ + uint32_t offset; /* DTB offset in PXA-DT */ + uint32_t len; /* DTB size */ +} __attribute__((packed)); + struct dtb_blob { void *fdt; uint32_t offset; @@ -83,6 +103,9 @@ private: template inline fdt_map_iter make_iter(Iter j) { return fdt_map_iter(j); } +constexpr int MAX_DEPTH = 32; +static bitset depth_set; + static void pretty_node(int depth) { if (depth == 0) return; @@ -282,7 +305,7 @@ static int dtb_patch(const Header *hdr, const char *in, const char *out) { } // Patch tables - auto tables_rw = reinterpret_cast(addr + sizeof(qcdt_hdr)); + auto tables_rw = reinterpret_cast
(addr + sizeof(Header)); for (int i = 0; i < hdr->num_dtbs; ++i) { auto &blob = dtb_map[tables_rw[i].offset]; tables_rw[i].offset = blob.offset; @@ -295,6 +318,8 @@ static int dtb_patch(const Header *hdr, const char *in, const char *out) { return 0; } +#define MATCH(s) (memcmp(dtb, s, sizeof(s) - 1) == 0) + static int dtb_patch(const char *in, const char *out) { if (!out) out = in; @@ -304,7 +329,7 @@ static int dtb_patch(const char *in, const char *out) { mmap_ro(in, dtb, dtb_sz); run_finally f([&]{ munmap(dtb, dtb_sz); }); - if (memcmp(dtb, QCDT_MAGIC, 4) == 0) { + if (MATCH(QCDT_MAGIC)) { auto hdr = reinterpret_cast(dtb); switch (hdr->version) { case 1: @@ -319,7 +344,7 @@ static int dtb_patch(const char *in, const char *out) { default: return 1; } - } else if (memcmp(dtb, DTBH_MAGIC, 4) == 0) { + } else if (MATCH(DTBH_MAGIC)) { auto hdr = reinterpret_cast(dtb); switch (hdr->version) { case 2: @@ -328,6 +353,24 @@ static int dtb_patch(const char *in, const char *out) { default: return 1; } + } else if (MATCH(PXADT_MAGIC)) { + auto hdr = reinterpret_cast(dtb); + switch (hdr->version) { + case 1: + fprintf(stderr, "PXA-DT v1\n"); + return dtb_patch(hdr, in, out); + default: + return 1; + } + } else if (MATCH(PXA_19xx_MAGIC)) { + auto hdr = reinterpret_cast(dtb); + switch (hdr->version) { + case 1: + fprintf(stderr, "PXA-19xx v1\n"); + return dtb_patch(hdr, in, out); + default: + return 1; + } } else { vector fdt_list; for (int i = 0; i < dtb_sz; ++i) { diff --git a/native/jni/magiskboot/format.cpp b/native/jni/magiskboot/format.cpp index 7ed50a2fd..6d2d86b3a 100644 --- a/native/jni/magiskboot/format.cpp +++ b/native/jni/magiskboot/format.cpp @@ -44,8 +44,6 @@ format_t check_fmt(const void *buf, size_t len) { return LZ4_LEGACY; } else if (MATCH(MTK_MAGIC)) { return MTK; - } else if (MATCH(DTB_MAGIC)) { - return DTB; } else if (MATCH(DHTB_MAGIC)) { return DHTB; } else if (MATCH(TEGRABLOB_MAGIC)) { @@ -77,8 +75,6 @@ const char *Fmt2Name::operator[](format_t fmt) { return "lz4_legacy"; case MTK: return "mtk"; - case DTB: - return "dtb"; default: return "raw"; } diff --git a/native/jni/magiskboot/format.h b/native/jni/magiskboot/format.h index 782cde54b..6fa8c2a83 100644 --- a/native/jni/magiskboot/format.h +++ b/native/jni/magiskboot/format.h @@ -38,11 +38,8 @@ typedef enum { #define LZ41_MAGIC "\x03\x21\x4c\x18" #define LZ42_MAGIC "\x04\x22\x4d\x18" #define MTK_MAGIC "\x88\x16\x88\x58" -#define DTB_MAGIC "\xd0\x0d\xfe\xed" #define LG_BUMP_MAGIC "\x41\xa9\xe4\x67\x74\x4d\x1d\x1b\xa4\x29\xf2\xec\xea\x65\x52\x79" #define DHTB_MAGIC "\x44\x48\x54\x42\x01\x00\x00\x00" -#define QCDT_MAGIC "QCDT" -#define DTBH_MAGIC "DTBH" #define SEANDROID_MAGIC "SEANDROIDENFORCE" #define TEGRABLOB_MAGIC "-SIGNED-BY-SIGNBLOB-" #define NOOKHD_RL_MAGIC "Red Loader"