patterns: Add PKM pattern + add BCC checksum verification to ntag pattern (#274)

* patterns/ntag: add BCC checksum verification

* patterns: add PKM file pattern

* Add entry to README

* Added missing import

---------

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
applecuckoo
2024-07-23 18:30:05 +12:00
committed by GitHub
parent 196695e37b
commit 0316f2b667
4 changed files with 63 additions and 0 deletions

View File

@@ -94,6 +94,7 @@ Everything will immediately show up in ImHex's Content Store and gets bundled wi
| PP | | [`patterns/selinuxpp.hexpat`](patterns/selinuxpp.pat) | SE Linux package |
| PFS0 | | [`patterns/pfs0.hexpat`](patterns/pfs0.hexpat) | Nintendo Switch PFS0 archive (NSP files) |
| PIF | `image/pif` | [`patterns/pif.hexpat`](patterns/pif.hexpat) | PIF Image Format |
| PKM | | [`patterns/pkm.hexpat`](patterns/pkm.hexpat) | PKM texture format |
| PNG | `image/png` | [`patterns/png.hexpat`](patterns/png.hexpat) | PNG image files |
| PRODINFO | | [`patterns/prodinfo.hexpat`](patterns/prodinfo.hexpat) | Nintendo Switch PRODINFO |
| Protobuf | | [`patterns/protobuf.hexpat`](patterns/protobuf.hexpat) | Google Protobuf encoding |

View File

@@ -2,6 +2,7 @@
#pragma description NTAG213/NTAG215/NTAG216, NFC Forum Type 2 Tag compliant IC
import std.core;
import std.io;
using BitfieldOrder = std::core::BitfieldOrder;
@@ -147,6 +148,38 @@ bitfield ACCESS {
AUTHLIM : 3;
};
fn validate_ntag_checksum(ManufacturerData mdata) {
/*
0x88 is the CT/cascade digit.
This is XOR'd with the first 3 bytes of the serial to calculate BCC0.
*/
u8 bcc0 = 0x88 ^
mdata.serial1[0] ^
mdata.serial1[1] ^
mdata.serial1[2];
if (bcc0 == mdata.checkByte0) {
std::print("NTAG BCC0 checksum OK. Value: {}", bcc0);
} else {
std::warning(std::format("NTAG BCC0 checksum failed! Value: {}", bcc0));
}
// BCC1 is the XOR of the last 4 serial bytes.
u8 bcc1 = mdata.serial2[0] ^
mdata.serial2[1] ^
mdata.serial2[2] ^
mdata.serial2[3];
if (bcc1 == mdata.checkByte1) {
std::print("NTAG BCC1 checksum OK. Value: {}", bcc1);
} else {
std::warning(std::format("NTAG BCC1 checksum failed! Value: {}", bcc1));
}
};
struct Config {
MIRROR MIRROR;
u8 MIRROR_PAGE;
@@ -166,3 +199,5 @@ struct NTAG {
};
NTAG ntag @ 0x00;
validate_ntag_checksum(ntag.manufacturerData);

27
patterns/pkm.hexpat Normal file
View File

@@ -0,0 +1,27 @@
#pragma description PKM (PacKMan) files containing ETC (Ericsson Texture Compression)
#pragma author applecuckoo
#pragma endian big
enum PKMFormat : u16 {
ETC1_RGB_NO_MIPMAPS,
ETC2PACKAGE_RGB_NO_MIPMAPS,
ETC2PACKAGE_RGBA_NO_MIPMAPS_OLD,
ETC2PACKAGE_RGBA_NO_MIPMAPS,
ETC2PACKAGE_RGBA1_NO_MIPMAPS,
ETC2PACKAGE_R_NO_MIPMAPS,
ETC2PACKAGE_RG_NO_MIPMAPS,
ETC2PACKAGE_R_SIGNED_NO_MIPMAPS,
ETC2PACKAGE_RG_SIGNED_NO_MIPMAPS
};
struct PKMHeader {
char id[4];
char version[2];
PKMFormat pkm_format;
u16 width;
u16 height;
u16 original_width;
u16 original_height;
};
PKMHeader pkm_header @ 0x00 [[comment("PKM file header"), name("Header")]];

Binary file not shown.