From 7b8f2688e9fcbb8d30b62a83c665f5d0776a8c00 Mon Sep 17 00:00:00 2001 From: jordam Date: Wed, 27 Sep 2023 02:26:57 -0700 Subject: [PATCH] Updated Gen4 Fast-Configuration command (#55) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- lib/magic/gen4.c | 2 +- lib/magic/gen4.h | 4 +++- nfc_magic_worker.c | 15 ++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/magic/gen4.c b/lib/magic/gen4.c index 31be649a04e..9afe4973a6e 100644 --- a/lib/magic/gen4.c +++ b/lib/magic/gen4.c @@ -17,7 +17,7 @@ #define MAGIC_BUFFER_SIZE (40) const uint8_t MAGIC_DEFAULT_CONFIG[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x78, 0x00, 0x91, 0x02, 0xDA, 0xBC, 0x19, 0x10, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x04, 0x00, 0x08, 0x00 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x78, 0x00, 0x91, 0x02, 0xDA, 0xBC, 0x19, 0x10, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x04, 0x00, 0x08, 0x00, 0x6b, 0x02 }; const uint8_t MAGIC_DEFAULT_BLOCK0[] = { diff --git a/lib/magic/gen4.h b/lib/magic/gen4.h index c515af820b0..937a2ba3018 100644 --- a/lib/magic/gen4.h +++ b/lib/magic/gen4.h @@ -3,7 +3,7 @@ #include #define MAGIC_GEN4_DEFAULT_PWD 0x00000000 -#define MAGIC_GEN4_CONFIG_LEN 32 +#define MAGIC_GEN4_CONFIG_LEN 30 #define NFCID1_SINGLE_SIZE 4 #define NFCID1_DOUBLE_SIZE 7 @@ -33,6 +33,8 @@ typedef enum { MagicGen4ShadowModeHighSpeedIgnore = 0x03 } MagicGen4ShadowMode; +extern const uint8_t MAGIC_DEFAULT_CONFIG[]; + bool magic_gen4_get_cfg(uint32_t pwd, uint8_t* config); bool magic_gen4_set_cfg(uint32_t pwd, const uint8_t* config, uint8_t config_length, bool fuse); diff --git a/nfc_magic_worker.c b/nfc_magic_worker.c index eb715fe0dda..17cca765987 100644 --- a/nfc_magic_worker.c +++ b/nfc_magic_worker.c @@ -128,13 +128,17 @@ void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker) { } } else if(magic_dev->type == MagicTypeGen4) { if(furi_hal_nfc_detect(&nfc_data, 200)) { - uint8_t gen4_config[28]; + uint8_t gen4_config[MAGIC_GEN4_CONFIG_LEN]; + memcpy(gen4_config, MAGIC_DEFAULT_CONFIG, MAGIC_GEN4_CONFIG_LEN); uint32_t password = magic_dev->password; - uint32_t cuid; + size_t block_count = 64; + MfClassicData* mfc_data; if(dev_protocol == NfcDeviceProtocolMifareClassic) { gen4_config[0] = 0x00; gen4_config[27] = 0x00; + mfc_data = &dev_data->mf_classic_data; + if(mfc_data->type == MfClassicType4k) block_count = 256; } else if(dev_protocol == NfcDeviceProtocolMifareUl) { MfUltralightData* mf_ul_data = &dev_data->mf_ul_data; gen4_config[0] = 0x01; @@ -155,6 +159,7 @@ void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker) { case MfUltralightTypeNTAGI2CPlus1K: case MfUltralightTypeNTAGI2CPlus2K: gen4_config[27] = MagicGen4UltralightModeNTAG; + block_count = 64 * 2; break; } } @@ -187,6 +192,9 @@ void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker) { gen4_config[25] = dev_data->nfc_data.atqa[1]; gen4_config[26] = dev_data->nfc_data.sak; + gen4_config[28] = block_count; + gen4_config[29] = 0x01; + furi_hal_nfc_sleep(); furi_hal_nfc_activate_nfca(200, &cuid); if(!magic_gen4_set_cfg(password, gen4_config, sizeof(gen4_config), false)) { @@ -196,9 +204,6 @@ void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker) { break; } if(dev_protocol == NfcDeviceProtocolMifareClassic) { - MfClassicData* mfc_data = &dev_data->mf_classic_data; - size_t block_count = 64; - if(mfc_data->type == MfClassicType4k) block_count = 256; for(size_t i = 0; i < block_count; i++) { FURI_LOG_D(TAG, "Writing block %d", i); if(!magic_gen4_write_blk(password, i, mfc_data->block[i].value)) {