mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
magiskboot: port find_dtb_offset to Rust
Port find_dtb_offset from C++ to Rust in dtb.rs, using the fdt crate for header parsing and root node validation. Key changes: - Upgrade fdt crate to 0.2.0-alpha2 and adapt dtb.rs to updated APIs. - Implement find_dtb_offset using Fdt::new_unaligned_fallible. - Define MIN_NON_EMPTY_DTB_SIZE (0x48) per Linux kernel ARM zImage specification to ignore empty or false positive DTB headers. - Expose find_dtb_offset to C++ via CXX bridge and clean up legacy structs and declarations. Assisted-by: Gemini 3.7 Flash
This commit is contained in:
@@ -20,6 +20,14 @@ using namespace std;
|
||||
#define RETURN_CHROMEOS 2
|
||||
#define RETURN_VENDOR 3
|
||||
|
||||
static FileFormat check_fmt(const void *buf, size_t len) {
|
||||
return check_fmt({static_cast<const uint8_t *>(buf), len});
|
||||
}
|
||||
|
||||
static int find_dtb_offset(const void *buf, size_t len) {
|
||||
return find_dtb_offset({static_cast<const uint8_t *>(buf), len});
|
||||
}
|
||||
|
||||
static void decompress(FileFormat type, int fd, const void *in, size_t size) {
|
||||
decompress_bytes(type, byte_view { in, size }, fd);
|
||||
}
|
||||
@@ -192,77 +200,6 @@ boot_img::~boot_img() {
|
||||
delete hdr;
|
||||
}
|
||||
|
||||
struct [[gnu::packed]] fdt_header {
|
||||
struct fdt32_t {
|
||||
uint32_t byte0: 8;
|
||||
uint32_t byte1: 8;
|
||||
uint32_t byte2: 8;
|
||||
uint32_t byte3: 8;
|
||||
|
||||
constexpr operator uint32_t() const {
|
||||
return bit_cast<uint32_t>(fdt32_t {
|
||||
.byte0 = byte3,
|
||||
.byte1 = byte2,
|
||||
.byte2 = byte1,
|
||||
.byte3 = byte0
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
struct node_header {
|
||||
fdt32_t tag;
|
||||
char name[0];
|
||||
};
|
||||
|
||||
fdt32_t magic; /* magic word FDT_MAGIC */
|
||||
fdt32_t totalsize; /* total size of DT block */
|
||||
fdt32_t off_dt_struct; /* offset to structure */
|
||||
fdt32_t off_dt_strings; /* offset to strings */
|
||||
fdt32_t off_mem_rsvmap; /* offset to memory reserve map */
|
||||
fdt32_t version; /* format version */
|
||||
fdt32_t last_comp_version; /* last compatible version */
|
||||
|
||||
/* version 2 fields below */
|
||||
fdt32_t boot_cpuid_phys; /* Which physical CPU id we're
|
||||
booting on */
|
||||
/* version 3 fields below */
|
||||
fdt32_t size_dt_strings; /* size of the strings block */
|
||||
|
||||
/* version 17 fields below */
|
||||
fdt32_t size_dt_struct; /* size of the structure block */
|
||||
};
|
||||
|
||||
static int find_dtb_offset(const uint8_t *buf, unsigned sz) {
|
||||
const uint8_t * const end = buf + sz;
|
||||
|
||||
for (auto curr = buf; curr < end; curr += sizeof(fdt_header)) {
|
||||
curr = static_cast<uint8_t*>(memmem(curr, end - curr, DTB_MAGIC, sizeof(fdt_header::fdt32_t)));
|
||||
if (curr == nullptr)
|
||||
return -1;
|
||||
|
||||
auto fdt_hdr = reinterpret_cast<const fdt_header *>(curr);
|
||||
|
||||
// Check that fdt_header.totalsize does not overflow kernel image size or is empty dtb
|
||||
// https://github.com/torvalds/linux/commit/7b937cc243e5b1df8780a0aa743ce800df6c68d1
|
||||
uint32_t totalsize = fdt_hdr->totalsize;
|
||||
if (totalsize > end - curr || totalsize <= 0x48)
|
||||
continue;
|
||||
|
||||
// Check that fdt_header.off_dt_struct does not overflow kernel image size
|
||||
uint32_t off_dt_struct = fdt_hdr->off_dt_struct;
|
||||
if (off_dt_struct > end - curr)
|
||||
continue;
|
||||
|
||||
// Check that fdt_node_header.tag of first node is FDT_BEGIN_NODE
|
||||
auto fdt_node_hdr = reinterpret_cast<const fdt_header::node_header *>(curr + off_dt_struct);
|
||||
if (fdt_node_hdr->tag != 0x1u)
|
||||
continue;
|
||||
|
||||
return curr - buf;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static FileFormat check_fmt_lg(const uint8_t *buf, unsigned sz) {
|
||||
FileFormat fmt = check_fmt(buf, sz);
|
||||
if (fmt == FileFormat::LZ4_LEGACY) {
|
||||
|
||||
Reference in New Issue
Block a user