From 537ce67895e9401af02f006244ed6f53da5f33b4 Mon Sep 17 00:00:00 2001 From: ZHANG Yuntian Date: Mon, 24 Mar 2025 14:15:25 +0800 Subject: [PATCH] patterns/partition_table: Added basic MBR and GPT patterns (#358) Both are conbined into a single pattern due to how GPT always has a pseudo MBR to maintain backward compatibility. --- README.md | 1 + patterns/partition_table.hexpat | 75 ++++++++++++++++++ .../test_data/partition_table.hexpat.bin | Bin 0 -> 17408 bytes 3 files changed, 76 insertions(+) create mode 100644 patterns/partition_table.hexpat create mode 100644 tests/patterns/test_data/partition_table.hexpat.bin diff --git a/README.md b/README.md index 5f69996..80dd1f3 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ Everything will immediately show up in ImHex's Content Store and gets bundled wi | LCE Savefile | | [`patterns/lcesave.hexpat`](patterns/lcesave.hexpat) | Minecraft Legacy Console Edition save file | | LZNT1 | | [`patterns/lznt1.hexpat`](patterns/lznt1.hexpat) | LZNT1 compressed data format | | Mach-O | `application/x-mach-binary` | [`patterns/macho.hexpat`](patterns/macho.hexpat) | Mach-O executable | +| MBR & GPT | [`patterns/partition_table.hexpat`](patterns/partition_table.hexpat) | Partition tables with primary focus on GPT | | MIDI | `audio/midi` | [`patterns/midi.hexpat`](patterns/midi.hexpat) | MIDI header, event fields provided | | MiniDump | `application/x-dmp` | [`patterns/minidump.hexpat`](patterns/minidump.hexpat) | Windows MiniDump files | | mp4 | `video/mp4` | [`patterns/mp4.hexpat`](patterns/mp4.hexpat) | MPEG-4 Part 14 digital multimedia container format | diff --git a/patterns/partition_table.hexpat b/patterns/partition_table.hexpat new file mode 100644 index 0000000..1596678 --- /dev/null +++ b/patterns/partition_table.hexpat @@ -0,0 +1,75 @@ +#pragma author RadxaYuntian +#pragma description Master Boot Record & GUID Partition Table +#pragma endian little + +import type.base; +import type.guid; + +#ifndef SECTOR_SIZE +#define SECTOR_SIZE 512 +#endif + +bitfield CHSAddress { + u8 head; + sector : 6; + cylinder: 10; +}; + +struct MBREntry { + u8 status; + CHSAddress first_chs; + u8 type; + CHSAddress last_chs; + u32 first_lba; + u32 sector_count; +}; + +struct MasterBootRecord { + padding[446]; + MBREntry entries[4]; + type::Hex boot_signature; + padding[SECTOR_SIZE - 512]; +}; + +bitfield GPTAttributes { + bool platform_required : 1; + padding : 59; + bool read_only : 1; + bool shadow_copy : 1; + bool hidden : 1; + bool no_drive_letter : 1; +}; + +struct GPTEntry { + type::GUID partition_type; + type::GUID partition_guid; + u64 first_lba; + u64 last_lba; + GPTAttributes attribute; + char16 partition_name[36]; + padding[parent.entry_size - 128]; +}; + +struct GUIDPartitionTable { + char signature[8]; // "EFI PART" + u16 version_minor; + u16 version_major; + u32 header_size; + type::Hex header_crc32; // from 0x0 to 0x5b + u32 reserved; // must be 0 + u64 current_lba; + u64 backup_lba; + u64 first_usable_lba; + u64 last_usable_lba; + type::GUID disk_guid; + u64 entry_lba; + u32 entry_count; + u32 entry_size; + type::Hex entry_crc32; + padding[SECTOR_SIZE - 92]; // end of first LBA + padding[SECTOR_SIZE * (entry_lba - current_lba - 1)]; // non-GPT data + GPTEntry entries[entry_count]; +}; + +MasterBootRecord mbr @ 0 * SECTOR_SIZE; +GUIDPartitionTable gpt @ 1 * SECTOR_SIZE; diff --git a/tests/patterns/test_data/partition_table.hexpat.bin b/tests/patterns/test_data/partition_table.hexpat.bin new file mode 100644 index 0000000000000000000000000000000000000000..c75fa79764376a45f0dd5c2d7481afaa330905cd GIT binary patch literal 17408 zcmeIwF-t;G6u|MLLplT+MPr4N_yJnv1xYEsAVdlxT9OtAL5v84AW#~GK0pwTEl+}$ zgrG&SrE-WqKtWU5COO>hQybp+pupgN;BvU!d(XY+#}y&|vw&AVlu{nug4K>5y6t51 zay6b@NG+!~L_D&ikJlIH`gRq&q8&d~L&K?5Mz=g3#J0^@%REn?-Nu_4pLeu=qO(0R zSJUe{5o0b8-jCeP_u5l8YjV+AYDIgw{QL7ncct7klBaLJE%6TWy