From 885911c469a8ef6d46cbb72c25947801704eb89d Mon Sep 17 00:00:00 2001 From: DidierA <1620015+DidierA@users.noreply.github.com> Date: Wed, 9 Nov 2022 15:49:07 +0100 Subject: [PATCH 1/4] Rewrite of magic Gen4 GTU commands : refactor and speed --- armsrc/appmain.c | 8 +- armsrc/mifarecmd.c | 131 +++++++++++++++++++-------------- armsrc/mifarecmd.h | 5 +- client/src/cmdhfmf.c | 8 +- client/src/mifare/mifarehost.c | 8 +- client/src/mifare/mifarehost.h | 4 +- 6 files changed, 94 insertions(+), 70 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index d48483b53..f59cc13a3 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1651,20 +1651,22 @@ static void PacketReceived(PacketCommandNG *packet) { struct p { uint8_t blockno; uint8_t pwd[4]; + uint8_t workFlags; } PACKED; struct p *payload = (struct p *) packet->data.asBytes; - MifareG4ReadBlk(payload->blockno, payload->pwd); + MifareG4ReadBlk(payload->blockno, payload->pwd, payload->workFlags); break; } - + // Gen 4 GTU magic cards case CMD_HF_MIFARE_G4_WRBL: { struct p { uint8_t blockno; uint8_t pwd[4]; uint8_t data[16]; // data to be written + uint8_t workFlags; } PACKED; struct p *payload = (struct p *) packet->data.asBytes; - MifareG4WriteBlk(payload->blockno, payload->pwd, payload->data); + MifareG4WriteBlk(payload->blockno, payload->pwd, payload->data, payload->workFlags); break; } case CMD_HF_MIFARE_PERSONALIZE_UID: { diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 476e7c96a..e1e8dc2a9 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -2684,90 +2684,107 @@ OUT: BigBuf_free(); } -void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd) { - iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); - clear_trace(); - set_tracing(true); +// read or write block to GEN4 GTU tag +void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags) { + bool read = rw & 0x1 & 0xFF ; + bool write = rw & 0x2 & 0xFF ; - int retval = PM3_SUCCESS; - uint8_t *par = BigBuf_malloc(MAX_PARITY_SIZE); - uint8_t *buf = BigBuf_malloc(PM3_CMD_DATA_SIZE); - uint8_t *uid = BigBuf_malloc(10); - if (iso14443a_select_card(uid, NULL, NULL, true, 0, true) == false) { - retval = PM3_ESOFT; - goto OUT; - } - - LED_B_ON(); - uint32_t save_iso14a_timeout = iso14a_get_timeout(); - iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout - - uint8_t cmd[] = { 0xCF, 0x00, 0x00, 0x00, 0x00, 0xCE, blockno, 0x00, 0x00}; - - memcpy(cmd + 1, pwd, 4); - - AddCrc14A(cmd, sizeof(cmd) - 2); - - ReaderTransmit(cmd, sizeof(cmd), NULL); - int res = ReaderReceive(buf, par); - if (res != 18) { - retval = PM3_ESOFT; - } - iso14a_set_timeout(save_iso14a_timeout); - LED_B_OFF(); - -OUT: - reply_ng(CMD_HF_MIFARE_G4_RDBL, retval, buf, 18); - // turns off - OnSuccessMagic(); - BigBuf_free(); -} - -void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data) { - iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); - clear_trace(); - set_tracing(true); + bool setup = workFlags & MAGIC_INIT & 0xFF ; + bool done = workFlags & MAGIC_OFF & 0xFF ; int res = 0; int retval = PM3_SUCCESS; - uint8_t *par = BigBuf_malloc(MAX_PARITY_SIZE); + uint8_t *buf = BigBuf_malloc(PM3_CMD_DATA_SIZE); - uint8_t *uid = BigBuf_malloc(10); - if (iso14443a_select_card(uid, NULL, NULL, true, 0, true) == false) { - retval = PM3_ESOFT; + if (buf == NULL) { + retval = PM3_EMALLOC; goto OUT; } - LED_B_ON(); - uint32_t save_iso14a_timeout = iso14a_get_timeout(); - iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout + // check args + if (write && (data == NULL)) { + retval = PM3_EINVARG; + goto OUT; + } - uint8_t cmd[] = { 0xCF, 0x00, 0x00, 0x00, 0x00, 0xCD, blockno, + if (!(read || write)) { + retval = PM3_EINVARG; + goto OUT; + } + + uint8_t *par = BigBuf_malloc(MAX_PARITY_SIZE); + if (par == NULL) { + retval = PM3_EMALLOC; + goto OUT; + } + + if (setup) { + uint8_t *uid = BigBuf_malloc(10); + if (uid == NULL) { + retval = PM3_EMALLOC; + goto OUT; + } + iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); + clear_trace(); + set_tracing(true); + + if (iso14443a_select_card(uid, NULL, NULL, true, 0, true) == false) { + retval = PM3_ESOFT; + goto OUT; + } + } + + LED_B_ON(); + + static uint32_t save_iso14a_timeout; + if (setup) { + save_iso14a_timeout = iso14a_get_timeout(); + iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout + } + + uint8_t cmd[] = { 0xCF, 0x00, 0x00, 0x00, 0x00, (write ? 0xCD : 0xCE), blockno, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; memcpy(cmd + 1, pwd, 4); - memcpy(cmd + 7, data, 16); + if (write) memcpy(cmd + 7, data, 16); - AddCrc14A(cmd, sizeof(cmd) - 2); + size_t crc_pos = read ? 7 : (sizeof(cmd) - 2) ; + AddCrc14A(cmd, crc_pos); - ReaderTransmit(cmd, sizeof(cmd), NULL); + ReaderTransmit(cmd, crc_pos + 2, NULL); res = ReaderReceive(buf, par); - if ((res != 4) || (memcmp(buf, "\x90\x00\xfd\x07", 4) != 0)) { + + if (write) { + if ((res != 4) || (memcmp(buf, "\x90\x00\xfd\x07", 4) != 0)) { + retval = PM3_ESOFT; + } + } else if (res != 18) { retval = PM3_ESOFT; } - iso14a_set_timeout(save_iso14a_timeout); + + if (done || retval != 0) iso14a_set_timeout(save_iso14a_timeout); LED_B_OFF(); OUT: - reply_ng(CMD_HF_MIFARE_G4_WRBL, retval, buf, res); + reply_ng(write ? CMD_HF_MIFARE_G4_WRBL : CMD_HF_MIFARE_G4_RDBL, retval, buf, res); // turns off - OnSuccessMagic(); + if (done || retval != 0) FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + LEDsoff(); + if (done || retval != 0) set_tracing(false); BigBuf_free(); } +void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd, uint8_t workFlags) { + MifareG4ReadWriteBlk(0x1, blockno, pwd, NULL, workFlags) ; +} + +void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags) { + MifareG4ReadWriteBlk(0x2, blockno, pwd, data, workFlags) ; +} + void MifareSetMod(uint8_t *datain) { uint8_t mod = datain[0]; diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index de3bd416c..a68fed8db 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -57,8 +57,9 @@ void MifareGen3Blk(uint8_t block_len, uint8_t *block); // Gen 3 magic card overw void MifareGen3Freez(void); // Gen 3 magic card lock further UID changes // MFC GEN4 GTU -void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd); -void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data); +void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags); +void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd, uint8_t workFlags); +void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags); void MifareSetMod(uint8_t *datain); void MifarePersonalizeUID(uint8_t keyType, uint8_t perso_option, uint64_t key); diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 222d0f961..b0c80f54a 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -6788,7 +6788,7 @@ static int CmdHF14AGen4GetBlk(const char *cmd) { PrintAndLogEx(NORMAL, "Block: %x", blockno) ; - int res = mfG4GetBlock(pwd, blockno, data); + int res = mfG4GetBlock(pwd, blockno, data, MAGIC_INIT | MAGIC_OFF); if (res) { PrintAndLogEx(ERR, "Can't read block. error=%d", res); return PM3_ESOFT; @@ -6980,7 +6980,7 @@ static int CmdHF14AGen4Load(const char *cmd) { fflush(stdout); // write block - if (mfG4SetBlock(pwd, blockno, data + (blockno * MFBLOCK_SIZE)) != PM3_SUCCESS) { + if (mfG4SetBlock(pwd, blockno, data + (blockno * MFBLOCK_SIZE), MAGIC_INIT | MAGIC_OFF) != PM3_SUCCESS) { PrintAndLogEx(WARNING, "Can't set magic card block: %d", blockno); PrintAndLogEx(HINT, "Verify your card size, and try again or try another tag position"); if (data != NULL) free(data); @@ -7047,7 +7047,7 @@ static int CmdHF14AGen4SetBlk(const char *cmd) { PrintAndLogEx(INFO, "Writing block number:%2d data:%s", b, sprint_hex_inrow(data, sizeof(data))); uint8_t blockno = (uint8_t)b; - int res = mfG4SetBlock(pwd, blockno, data); + int res = mfG4SetBlock(pwd, blockno, data, MAGIC_INIT | MAGIC_OFF); if (res) { PrintAndLogEx(ERR, "Can't write block. error=%d", res); return PM3_ESOFT; @@ -7164,7 +7164,7 @@ static int CmdHF14AGen4View(const char *Cmd) { PrintAndLogEx(NORMAL, "." NOLF); fflush(stdout); - if (mfG4GetBlock(pwd, i, dump + (i * MFBLOCK_SIZE)) != PM3_SUCCESS) { + if (mfG4GetBlock(pwd, i, dump + (i * MFBLOCK_SIZE), MAGIC_INIT | MAGIC_OFF) != PM3_SUCCESS) { PrintAndLogEx(WARNING, "Can't get magic card block: %u", i); PrintAndLogEx(HINT, "Verify your card size, and try again or try another tag position"); free(dump); diff --git a/client/src/mifare/mifarehost.c b/client/src/mifare/mifarehost.c index 2964289b2..1d57ec533 100644 --- a/client/src/mifare/mifarehost.c +++ b/client/src/mifare/mifarehost.c @@ -1158,13 +1158,15 @@ int mfGen3Freeze(void) { } } -int mfG4GetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data) { +int mfG4GetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data, uint8_t workFlags) { struct p { uint8_t blockno; uint8_t pwd[4]; + uint8_t workFlags; } PACKED payload; payload.blockno = blockno; memcpy(payload.pwd, pwd, sizeof(payload.pwd)); + payload.workFlags = workFlags; clearCommandBuffer(); SendCommandNG(CMD_HF_MIFARE_G4_RDBL, (uint8_t *)&payload, sizeof(payload)); @@ -1181,15 +1183,17 @@ int mfG4GetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data) { return PM3_SUCCESS; } -int mfG4SetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data) { +int mfG4SetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data, uint8_t workFlags) { struct p { uint8_t blockno; uint8_t pwd[4]; uint8_t data[16]; + uint8_t workFlags; } PACKED payload; payload.blockno = blockno; memcpy(payload.pwd, pwd, sizeof(payload.pwd)); memcpy(payload.data, data, sizeof(payload.data)); + payload.workFlags = workFlags; clearCommandBuffer(); SendCommandNG(CMD_HF_MIFARE_G4_WRBL, (uint8_t *)&payload, sizeof(payload)); diff --git a/client/src/mifare/mifarehost.h b/client/src/mifare/mifarehost.h index cc0fa1f83..8f50ccbd7 100644 --- a/client/src/mifare/mifarehost.h +++ b/client/src/mifare/mifarehost.h @@ -95,8 +95,8 @@ int mfGen3UID(uint8_t *uid, uint8_t uidlen, uint8_t *oldUid); int mfGen3Block(uint8_t *block, int blockLen, uint8_t *newBlock); int mfGen3Freeze(void); -int mfG4GetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data); -int mfG4SetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data); +int mfG4GetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data, uint8_t workFlags); +int mfG4SetBlock(uint8_t *pwd, uint8_t blockno, uint8_t *data, uint8_t workFlags); int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len); From da1ce305337e265a6e97d62a352dd2f94ef5b7ba Mon Sep 17 00:00:00 2001 From: DidierA <1620015+DidierA@users.noreply.github.com> Date: Sun, 13 Nov 2022 01:37:52 +0100 Subject: [PATCH 2/4] hf mf gview, gload : 2x speed on Gen4 GTU cards, a select was made before each read or write command. This commit adds a flag parameter to the read and write commands, and gload and gview commands use this flag to tell when to select and end. The trace buffer also contains the complete operation instead of the last blokc read/write. Speed gain: `time ./proxmark3 -p /dev/ttyACM0 -c 'hf mf gview --4k'` before: about 20s, now: about 7s `time ./proxmark3 -p /dev/ttyACM0 -c 'hf mf gload --4k -f ../../../dumps/4k.bin'` before: about 23s, now: about 10s --- client/src/cmdhfmf.c | 45 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index eabef0423..5609d0363 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -2402,7 +2402,7 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) { } } - bool load_success = true; + bool load_success = true; // Load the dictionary if (has_filename) { res = loadFileDICTIONARY_safe(filename, (void **) &keyBlock, 6, &key_cnt); @@ -7022,8 +7022,13 @@ static int CmdHF14AGen4Load(const char *cmd) { fflush(stdout); // write block - if (mfG4SetBlock(pwd, blockno, data + (blockno * MFBLOCK_SIZE), MAGIC_INIT | MAGIC_OFF) != PM3_SUCCESS) { - PrintAndLogEx(WARNING, "Can't set magic card block: %d", blockno); + uint8_t flags = 0 ; + if (blockno == start) flags |= MAGIC_INIT ; + if (blockno == end) flags |= MAGIC_OFF ; + + int res=mfG4SetBlock(pwd, blockno, data + (blockno * MFBLOCK_SIZE), flags); + if ( res != PM3_SUCCESS) { + PrintAndLogEx(WARNING, "Can't set magic card block: %d. error=%d", blockno, res); PrintAndLogEx(HINT, "Verify your card size, and try again or try another tag position"); free(data); return PM3_ESOFT; @@ -7163,31 +7168,6 @@ static int CmdHF14AGen4View(const char *Cmd) { } PrintAndLogEx(SUCCESS, "View magic gen4 GTU MIFARE Classic " _GREEN_("%s"), s); - // Select card to get UID/UIDLEN information - clearCommandBuffer(); - SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_CONNECT, 0, 0, NULL, 0); - PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) == false) { - PrintAndLogEx(WARNING, "iso14443a card select timeout"); - return PM3_ETIMEOUT; - } - - /* - 0: couldn't read - 1: OK, with ATS - 2: OK, no ATS - 3: proprietary Anticollision - */ - uint64_t select_status = resp.oldarg[0]; - - if (select_status == 0) { - PrintAndLogEx(WARNING, "iso14443a card select failed"); - return PM3_SUCCESS; - } - - iso14a_card_select_t card; - memcpy(&card, (iso14a_card_select_t *)resp.data.asBytes, sizeof(iso14a_card_select_t)); - // reserve memory uint16_t bytes = block_cnt * MFBLOCK_SIZE; uint8_t *dump = calloc(bytes, sizeof(uint8_t)); @@ -7206,8 +7186,13 @@ static int CmdHF14AGen4View(const char *Cmd) { PrintAndLogEx(NORMAL, "." NOLF); fflush(stdout); - if (mfG4GetBlock(pwd, i, dump + (i * MFBLOCK_SIZE), MAGIC_INIT | MAGIC_OFF) != PM3_SUCCESS) { - PrintAndLogEx(WARNING, "Can't get magic card block: %u", i); + uint8_t flags = 0 ; + if (i == 0) flags |= MAGIC_INIT ; + if (i+1 == block_cnt) flags |= MAGIC_OFF ; + + int res = mfG4GetBlock(pwd, i, dump + (i * MFBLOCK_SIZE), flags); + if ( res != PM3_SUCCESS) { + PrintAndLogEx(WARNING, "Can't get magic card block: %u. error=%d", i, res); PrintAndLogEx(HINT, "Verify your card size, and try again or try another tag position"); free(dump); return PM3_ESOFT; From ee3e4968d34ab6c9e2651f3201f42bdbb3964bb1 Mon Sep 17 00:00:00 2001 From: DidierA <1620015+DidierA@users.noreply.github.com> Date: Sun, 13 Nov 2022 02:08:23 +0100 Subject: [PATCH 3/4] remove MifareG4ReadWrite() --- armsrc/mifarecmd.c | 105 +++++++++++++++++++++++++++++++++------------ armsrc/mifarecmd.h | 1 - 2 files changed, 77 insertions(+), 29 deletions(-) diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index e1e8dc2a9..cf5e34d8d 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -2684,10 +2684,7 @@ OUT: BigBuf_free(); } -// read or write block to GEN4 GTU tag -void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags) { - bool read = rw & 0x1 & 0xFF ; - bool write = rw & 0x2 & 0xFF ; +void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd, uint8_t workFlags) { bool setup = workFlags & MAGIC_INIT & 0xFF ; bool done = workFlags & MAGIC_OFF & 0xFF ; @@ -2701,13 +2698,78 @@ void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *da goto OUT; } - // check args - if (write && (data == NULL)) { - retval = PM3_EINVARG; + uint8_t *par = BigBuf_malloc(MAX_PARITY_SIZE); + if (par == NULL) { + retval = PM3_EMALLOC; goto OUT; } - if (!(read || write)) { + if (setup) { + uint8_t *uid = BigBuf_malloc(10); + if (uid == NULL) { + retval = PM3_EMALLOC; + goto OUT; + } + iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); + clear_trace(); + set_tracing(true); + + if (iso14443a_select_card(uid, NULL, NULL, true, 0, true) == false) { + retval = PM3_ESOFT; + goto OUT; + } + } + + LED_B_ON(); + + static uint32_t save_iso14a_timeout; + if (setup) { + save_iso14a_timeout = iso14a_get_timeout(); + iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout + } + + uint8_t cmd[] = { 0xCF, 0x00, 0x00, 0x00, 0x00, 0xCE, blockno, + 0x00, 0x00 + }; + + memcpy(cmd + 1, pwd, 4); + + AddCrc14A(cmd, sizeof(cmd) - 2); + + ReaderTransmit(cmd, sizeof(cmd), NULL); + res = ReaderReceive(buf, par); + + if (res != 18) { + retval = PM3_ESOFT; + } + + if (done || retval != 0) iso14a_set_timeout(save_iso14a_timeout); + LED_B_OFF(); + +OUT: + reply_ng(CMD_HF_MIFARE_G4_RDBL, retval, buf, res); + // turns off + if (done || retval != 0) FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + LEDsoff(); + if (done || retval != 0) set_tracing(false); + BigBuf_free(); +} + +void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags) { + bool setup = workFlags & MAGIC_INIT & 0xFF ; + bool done = workFlags & MAGIC_OFF & 0xFF ; + + int res = 0; + int retval = PM3_SUCCESS; + + uint8_t *buf = BigBuf_malloc(PM3_CMD_DATA_SIZE); + if (buf == NULL) { + retval = PM3_EMALLOC; + goto OUT; + } + + // check args + if (data == NULL) { retval = PM3_EINVARG; goto OUT; } @@ -2742,26 +2804,21 @@ void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *da iso14a_set_timeout(13560000 / 1000 / (8 * 16) * 1000); // 2 seconds timeout } - uint8_t cmd[] = { 0xCF, 0x00, 0x00, 0x00, 0x00, (write ? 0xCD : 0xCE), blockno, + uint8_t cmd[] = { 0xCF, 0x00, 0x00, 0x00, 0x00, 0xCD, blockno, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; memcpy(cmd + 1, pwd, 4); - if (write) memcpy(cmd + 7, data, 16); + memcpy(cmd + 7, data, 16); - size_t crc_pos = read ? 7 : (sizeof(cmd) - 2) ; - AddCrc14A(cmd, crc_pos); + AddCrc14A(cmd, sizeof(cmd) - 2); - ReaderTransmit(cmd, crc_pos + 2, NULL); + ReaderTransmit(cmd, sizeof(cmd), NULL); res = ReaderReceive(buf, par); - if (write) { - if ((res != 4) || (memcmp(buf, "\x90\x00\xfd\x07", 4) != 0)) { - retval = PM3_ESOFT; - } - } else if (res != 18) { + if ((res != 4) || (memcmp(buf, "\x90\x00\xfd\x07", 4) != 0)) { retval = PM3_ESOFT; } @@ -2769,20 +2826,12 @@ void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *da LED_B_OFF(); OUT: - reply_ng(write ? CMD_HF_MIFARE_G4_WRBL : CMD_HF_MIFARE_G4_RDBL, retval, buf, res); + reply_ng(CMD_HF_MIFARE_G4_WRBL, retval, buf, res); // turns off if (done || retval != 0) FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LEDsoff(); if (done || retval != 0) set_tracing(false); - BigBuf_free(); -} - -void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd, uint8_t workFlags) { - MifareG4ReadWriteBlk(0x1, blockno, pwd, NULL, workFlags) ; -} - -void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags) { - MifareG4ReadWriteBlk(0x2, blockno, pwd, data, workFlags) ; + BigBuf_free(); } void MifareSetMod(uint8_t *datain) { diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index a68fed8db..30179aa3b 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -57,7 +57,6 @@ void MifareGen3Blk(uint8_t block_len, uint8_t *block); // Gen 3 magic card overw void MifareGen3Freez(void); // Gen 3 magic card lock further UID changes // MFC GEN4 GTU -void MifareG4ReadWriteBlk(uint8_t rw, uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags); void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd, uint8_t workFlags); void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags); From fa95119171cc0e383390fcae4b84140fadb01a58 Mon Sep 17 00:00:00 2001 From: DidierA <1620015+DidierA@users.noreply.github.com> Date: Mon, 14 Nov 2022 09:44:02 +0100 Subject: [PATCH 4/4] style: extract flags --- armsrc/mifarecmd.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index cf5e34d8d..4fd74c9ef 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -2685,9 +2685,8 @@ OUT: } void MifareG4ReadBlk(uint8_t blockno, uint8_t *pwd, uint8_t workFlags) { - - bool setup = workFlags & MAGIC_INIT & 0xFF ; - bool done = workFlags & MAGIC_OFF & 0xFF ; + bool setup = ((workFlags & MAGIC_INIT) == MAGIC_INIT) ; + bool done = ((workFlags & MAGIC_OFF) == MAGIC_OFF) ; int res = 0; int retval = PM3_SUCCESS; @@ -2756,8 +2755,8 @@ OUT: } void MifareG4WriteBlk(uint8_t blockno, uint8_t *pwd, uint8_t *data, uint8_t workFlags) { - bool setup = workFlags & MAGIC_INIT & 0xFF ; - bool done = workFlags & MAGIC_OFF & 0xFF ; + bool setup = ((workFlags & MAGIC_INIT) == MAGIC_INIT) ; + bool done = ((workFlags & MAGIC_OFF) == MAGIC_OFF) ; int res = 0; int retval = PM3_SUCCESS;