Add support for PXA DTBs

This commit is contained in:
topjohnwu 2019-10-08 23:49:21 -04:00
parent d25ae5e0a9
commit f5d054b93c
3 changed files with 49 additions and 13 deletions

View File

@ -10,12 +10,14 @@ extern "C" {
#include <map>
#include "magiskboot.h"
#include "format.h"
using namespace std;
constexpr int MAX_DEPTH = 32;
static bitset<MAX_DEPTH> 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<class Iter>
inline fdt_map_iter<Iter> make_iter(Iter j) { return fdt_map_iter<Iter>(j); }
constexpr int MAX_DEPTH = 32;
static bitset<MAX_DEPTH> 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<Table *>(addr + sizeof(qcdt_hdr));
auto tables_rw = reinterpret_cast<Table *>(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<qcdt_hdr*>(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<dtbh_hdr *>(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<pxadt_hdr *>(dtb);
switch (hdr->version) {
case 1:
fprintf(stderr, "PXA-DT v1\n");
return dtb_patch<pxa_table_v1>(hdr, in, out);
default:
return 1;
}
} else if (MATCH(PXA_19xx_MAGIC)) {
auto hdr = reinterpret_cast<pxa_19xx_hdr *>(dtb);
switch (hdr->version) {
case 1:
fprintf(stderr, "PXA-19xx v1\n");
return dtb_patch<pxa_table_v1>(hdr, in, out);
default:
return 1;
}
} else {
vector<uint8_t *> fdt_list;
for (int i = 0; i < dtb_sz; ++i) {

View File

@ -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";
}

View File

@ -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"