From 71ee41fe2c4c43dd745d6c9b6c2d7013f9450d63 Mon Sep 17 00:00:00 2001 From: Nik Date: Tue, 20 Dec 2022 17:14:23 +0100 Subject: [PATCH] patterns/fdt: Added flat device tree blob pattern --- README.md | 1 + patterns/fdt.pat | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 patterns/fdt.pat diff --git a/README.md b/README.md index 522b309..ce8e643 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Hex patterns, include patterns and magic files for the use with the ImHex Hex Ed | ID3 | `audio/mpeg` | [`patterns/id3tag.hexpat`](patterns/id3tag.hexpat) | ID3 tags in MP3 files | | TAR | `application/x-tar` | [`patterns/tar.hexpat`](patterns/tar.hexpat) | Tar file format | | CPIO | `application/x-cpio` | [`patterns/cpio.hexpat`](patterns/cpio.hexpat) | Old Binary CPIO Format | +| FDT | | [`patterns/cpio.hexpat`](patterns/fdt.hexpat) | Flat Linux Device Tree blob | ### Scripts diff --git a/patterns/fdt.pat b/patterns/fdt.pat new file mode 100644 index 0000000..27fe0ea --- /dev/null +++ b/patterns/fdt.pat @@ -0,0 +1,70 @@ +#pragma endian big + +#include +#include + +#include +#include + +struct FDTHeader { + type::Magic<"\xD0\x0D\xFE\xED"> magic; + u32 totalsize; + u32 off_dt_struct; + u32 off_dt_strings; + u32 off_mem_rsvmap; + u32 version; + u32 last_comp_version; + u32 boot_cpuid_phys; + u32 size_dt_strings; + u32 size_dt_struct; +}; + +struct AlignTo { + padding[Alignment- ((($ - 1) % Alignment) + 1)]; +}; + +struct FDTReserveEntry { + u64 address; + type::Size size; + + if (address == 0x00 && size == 0x00) + break; +}; + +enum FDTToken : u32 { + FDT_BEGIN_NODE = 0x00000001, + FDT_END_NODE = 0x00000002, + FDT_PROP = 0x00000003, + FDT_NOP = 0x00000004, + FDT_END = 0x00000009 +}; + +struct FDTStructureBlock { + FDTToken token; + if (token == FDTToken::FDT_BEGIN_NODE) { + char nodeName[]; + AlignTo<4>; + } else if (token == FDTToken::FDT_END) { + break; + } else if (token == FDTToken::FDT_PROP) { + u32 len; + u32 nameoff; + char value[len]; + AlignTo<4>; + char name[] @ parent.header.off_dt_strings + nameoff; + } else if (token == FDTToken::FDT_NOP || token == FDTToken::FDT_END_NODE) { + // Nothing to do + } else { + std::error(std::format("Invalid token at address 0x{:02X}", addressof(token))); + } +}; + +struct FDT { + FDTHeader header; + std::assert(header.version == 17, "Unsupported format version"); + + FDTStructureBlock structureBlocks[while(true)] @ header.off_dt_struct; + FDTReserveEntry reserveEntries[while(true)] @ header.off_mem_rsvmap; +}; + +FDT fdt @ 0x00; \ No newline at end of file