Refactor hitag config_page_t for better memory alignment

This commit is contained in:
douniwan5788 2025-03-22 06:05:17 +08:00
commit 5122039547
2 changed files with 51 additions and 47 deletions

View file

@ -713,35 +713,35 @@ static int CmdEM410xClone(const char *Cmd) {
switch (step) { switch (step) {
case 0: { case 0: {
hitags_config_t config = {0}; hitags_config_page_t config_page = {0};
config.MEMT = 0x02; // compatiable for 82xx, no impact on Hitag S config_page.s.MEMT = 0x02; // compatiable for 82xx, no impact on Hitag S
config.TTFM = 0x01; // 0 = "Block 0, Block 1, Block 2, Block 3", 1 = "Block 0, Block 1" config_page.s.TTFM = 0x01; // 0 = "Block 0, Block 1, Block 2, Block 3", 1 = "Block 0, Block 1"
config.TTFC = 0x00; // Manchester config_page.s.TTFC = 0x00; // Manchester
config.auth = 0x00; // Plain config_page.s.auth = 0x00; // Plain
//compatiable for 82xx, no impact on Hitag S //compatiable for 82xx, no impact on Hitag S
config.RES1 = 0x01; config_page.s.RES1 = 0x01;
config.RES4 = 0x01; config_page.s.RES4 = 0x01;
config.RES5 = 0x01; config_page.s.RES5 = 0x01;
switch (clk) { switch (clk) {
case 64: { case 64: {
// 2 kBit/s // 2 kBit/s
config.TTFDR = 0x02; config_page.s.TTFDR = 0x02;
break; break;
} }
case 32: { case 32: {
// 4 kBit/s // 4 kBit/s
config.TTFDR = 0x00; config_page.s.TTFDR = 0x00;
break; break;
} }
case 16: { case 16: {
// 8 kBit/s // 8 kBit/s
config.TTFDR = 0x01; config_page.s.TTFDR = 0x01;
break; break;
} }
} }
//TODO: keep other fields? //TODO: keep other fields?
memcpy(packet.data, &config, sizeof(config)); memcpy(packet.data, &config_page, sizeof(config_page));
// PrintAndLogEx(INFO, "packet.data: %s", sprint_hex(packet.data, sizeof(packet.data))); // PrintAndLogEx(INFO, "packet.data: %s", sprint_hex(packet.data, sizeof(packet.data)));
packet.page = 1; packet.page = 1;
break; break;
@ -792,27 +792,27 @@ static int CmdEM410xClone(const char *Cmd) {
// 64 -> 0x00 2 kBit/s // 64 -> 0x00 2 kBit/s
// 32 -> 0x01 4 kBit/s // 32 -> 0x01 4 kBit/s
// 16 -> 0x10 8 kBit/s // 16 -> 0x10 8 kBit/s
hitagu82xx_config_t config = {0}; hitagu_config_page_t config_page = {0};
config.datarate_override = 0x00; // no datarate override config_page.s82xx.datarate_override = 0x00; // no datarate override
config.encoding = 0x00; // Manchester config_page.s82xx.encoding = 0x00; // Manchester
config.ttf_mode = 0x01; // 01 = "Block 0, Block 1" config_page.s82xx.ttf_mode = 0x01; // 01 = "Block 0, Block 1"
config.ttf = 0x01; // enable TTF config_page.s82xx.ttf = 0x01; // enable TTF
switch (clk) { switch (clk) {
case 64: { case 64: {
break; break;
} }
case 32: { case 32: {
config.datarate = 0x01; config_page.s82xx.datarate = 0x01;
break; break;
} }
case 16: { case 16: {
config.datarate = 0x02; config_page.s82xx.datarate = 0x02;
break; break;
} }
} }
packet.data[0] = reflect8(*(uint8_t *)&config); reverse_arraybytes_copy(config_page.asBytes, packet.data, sizeof(config_page));
packet.page = HITAGU_CONFIG_PADR; // Config block packet.page = HITAGU_CONFIG_PADR; // Config block
break; break;
} }

View file

@ -123,30 +123,30 @@ typedef enum TAG_STATE {
typedef struct { typedef struct {
// con0 // con0
uint8_t MEMT : 2; uint8_t MEMT : 2;
bool RES0 : 1; // for 82xx. Enable somekind extended TTF mode in conjunction with TTFM uint8_t RES0 : 1; // for 82xx. Enable somekind extended TTF mode in conjunction with TTFM
bool RES1 : 1; uint8_t RES1 : 1;
bool RES2 : 1; uint8_t RES2 : 1;
bool RES3 : 1; // for 82xx. Enable TTF FSK mode 0=RF/10 1=RF/8 uint8_t RES3 : 1; // for 82xx. Enable TTF FSK mode 0=RF/10 1=RF/8
bool RES4 : 1; uint8_t RES4 : 1;
bool RES5 : 1; uint8_t RES5 : 1;
// con1 // con1
bool LKP : 1; // 0 = page2/3 read write 1 =page2/3 read only in Plain mode and no access in authenticate mode uint8_t LKP : 1; // 0 = page2/3 read write 1 =page2/3 read only in Plain mode and no access in authenticate mode
bool LCON : 1; // 0 = con1/2 read write 1 =con1 read only and con2 OTP uint8_t LCON : 1; // 0 = con1/2 read write 1 =con1 read only and con2 OTP
uint8_t TTFM : 2; // the number of pages that are sent to the RWD uint8_t TTFM : 2; // the number of pages that are sent to the RWD
uint8_t TTFDR : 2; // data rate in TTF Mode uint8_t TTFDR : 2; // data rate in TTF Mode
bool TTFC : 1; // Transponder Talks first coding. 0 = Manchester 1 = Biphase uint8_t TTFC : 1; // Transponder Talks first coding. 0 = Manchester 1 = Biphase
bool auth : 1; // 0 = Plain 1 = Auth uint8_t auth : 1; // 0 = Plain 1 = Auth
// con2 // con2
// 0 = read write 1 = read only // 0 = read write 1 = read only
bool LCK0 : 1; // page48-63 uint8_t LCK0 : 1; // page48-63
bool LCK1 : 1; // page32-47 uint8_t LCK1 : 1; // page32-47
bool LCK2 : 1; // page24-31 uint8_t LCK2 : 1; // page24-31
bool LCK3 : 1; // page16-23 uint8_t LCK3 : 1; // page16-23
bool LCK4 : 1; // page12-15 uint8_t LCK4 : 1; // page12-15
bool LCK5 : 1; // page8-11 uint8_t LCK5 : 1; // page8-11
bool LCK6 : 1; // page6/7 uint8_t LCK6 : 1; // page6/7
bool LCK7 : 1; // page4/5 uint8_t LCK7 : 1; // page4/5
// reserved/pwdh0 // reserved/pwdh0
uint8_t pwdh0; uint8_t pwdh0;
} PACKED hitags_config_t; } PACKED hitags_config_t;
@ -257,22 +257,26 @@ typedef struct {
uint8_t data[256]; uint8_t data[256];
} PACKED lf_hitag_crack_response_t; } PACKED lf_hitag_crack_response_t;
typedef union {
uint8_t asBytes[HITAGS_PAGE_SIZE];
hitags_config_t s;
} hitags_config_page_t;
typedef struct { typedef struct {
union { hitags_config_page_t config_page;
uint8_t asBytes[HITAGS_PAGE_SIZE];
hitags_config_t s;
} config_page;
int8_t pages_reason[HITAGS_MAX_PAGES]; int8_t pages_reason[HITAGS_MAX_PAGES];
uint8_t pages[HITAGS_MAX_PAGES][HITAGS_PAGE_SIZE]; uint8_t pages[HITAGS_MAX_PAGES][HITAGS_PAGE_SIZE];
} PACKED lf_hts_read_response_t; } PACKED lf_hts_read_response_t;
typedef union {
uint8_t asBytes[HITAGU_BLOCK_SIZE];
hitagu_config_t s;
hitagu82xx_config_t s82xx;
} hitagu_config_page_t;
// Hitag µ read response structure // Hitag µ read response structure
typedef struct { typedef struct {
union { hitagu_config_page_t config_page;
uint8_t asBytes[HITAGU_BLOCK_SIZE];
hitagu_config_t s;
hitagu82xx_config_t s82xx;
} config_page;
uint8_t uid[HITAGU_UID_SIZE]; uint8_t uid[HITAGU_UID_SIZE];
uint8_t icr; // IC Revision value for memory size detection uint8_t icr; // IC Revision value for memory size detection
int8_t pages_reason[HITAGU_MAX_PAGE_ADVANCED_PLUS]; int8_t pages_reason[HITAGU_MAX_PAGE_ADVANCED_PLUS];