From 5eda5a15bf6c6bc280a0ee36224de39ac63045ca Mon Sep 17 00:00:00 2001 From: Nik Date: Tue, 27 Dec 2022 10:35:07 +0100 Subject: [PATCH] patterns: Added XCI and WAD pattern --- README.md | 2 + patterns/wad.hexpat | 24 +++++++++++ patterns/xci.hexpat | 97 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 patterns/wad.hexpat create mode 100644 patterns/xci.hexpat diff --git a/README.md b/README.md index 9a3cfb9..0691093 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ Hex patterns, include patterns and magic files for the use with the ImHex Hex Ed | PCX | `application/x-pcx` | [`patterns/pcx.hexpat`](patterns/pcx.hexpat) | PCX Image format | | GZIP | `application/gzip` | [`patterns/gzip.hexpat`](patterns/gzip.hexpat) | GZip compressed data format | | PFS0 | | [`patterns/pfs0.hexpat`](patterns/pfs0.hexpat) | Nintendo Switch PFS0 archive (NSP files) | +| XCI | | [`patterns/xci.hexpat`](patterns/xci.hexpat) | Nintendo Switch XCI cardridge ROM | +| WAD | | [`patterns/wad.hexpat`](patterns/wad.hexpat) | DOOM WAD Archive | ### Scripts diff --git a/patterns/wad.hexpat b/patterns/wad.hexpat new file mode 100644 index 0000000..6304f34 --- /dev/null +++ b/patterns/wad.hexpat @@ -0,0 +1,24 @@ +#include +#include + +enum WADType : char { + Internal = 'I', + Patch = 'P' +}; + +struct FileLump { + u32 filePos; + type::Size size; + char name[8]; + + u8 data[size] @ filePos [[sealed]]; +}; + +struct WAD { + WADType type; + type::Magic<"WAD"> identification; + u32 numLumps; + FileLump *infoTable[numLumps] : u32; +}; + +WAD wad @ 0x00; \ No newline at end of file diff --git a/patterns/xci.hexpat b/patterns/xci.hexpat new file mode 100644 index 0000000..836d77e --- /dev/null +++ b/patterns/xci.hexpat @@ -0,0 +1,97 @@ +#include + +#include +#include + +#define PAGE_SIZE 0x200 + +enum RomSize : u8 { + _1GB = 0xFA, + _2GB = 0xF8, + _4GB = 0xF0, + _8GB = 0xE0, + _16GB = 0xE1, + _32GB = 0xE2 +}; + +bitfield Index { + titleKeyDecIndex : 4; + kekIndex : 4; +}; + +bitfield Flags { + autoBoot : 1; + historyErase : 1; + repairTool : 1; + differentRegionCupToTerraDevice : 1; + differentRegionCupToGlobalDevice : 1; + padding : 2; + hasNewCardHeader : 1; +}; + +struct SelSec { + u16 t1, t2; +}; + +struct CardHeader { + u8 headerSignature[0x100]; + type::Magic<"HEAD"> magic; + u32 romAreaStartPageAddress; + u32 backupAreaStartPageAddress; + Index index [[inline]]; + RomSize romSize; + u8 cardHeaderVersion; + Flags flags; + u64 packageId; + u32 validDataEndAddress; + padding[4]; + u8 iv[0x10]; + u64 partitionFsHeaderAddress; + type::Size partitionFsHeaderSize; + u8 partitionFsHeaderHash[0x20]; + u8 initialDataHash[0x20]; + SelSec selSec; + u32 selT1Key; + u32 selKey; + u32 limArea; + + u8 cardHeaderEncryptedData[0x70]; +}; + +struct FileEntry { + u64 dataOffset; + type::Size dataSize; + u32 fileNameOffset; + type::Size hashedRegionSize; + padding[8]; + u8 hash[0x20]; +}; + +struct String { + char string[]; +}; + +struct File { + String fileName @ addressof(parent.stringTable) + parent.fileEntryTable[std::core::array_index()].fileNameOffset; + u8 data[parent.fileEntryTable[std::core::array_index()].dataSize] @ addressof(parent.stringTable) + sizeof(parent.stringTable) + parent.fileEntryTable[std::core::array_index()].dataOffset [[sealed]]; +}; + +struct PartitionFs { + type::Magic<"HFS0"> magic; + u32 fileCount; + type::Size stringTableSize; + padding[4]; + FileEntry fileEntryTable[fileCount]; + String stringTable[while($ < (addressof(fileEntryTable) + sizeof(fileEntryTable) + stringTableSize))]; + + File files[fileCount]; +}; + +struct XCI { + CardHeader header; + + PartitionFs x @ header.romAreaStartPageAddress * PAGE_SIZE; + +}; + +XCI xci @ 0x00; \ No newline at end of file