Skip to content

Commit

Permalink
Updated Gen4 Fast-Configuration command (#55)
Browse files Browse the repository at this point in the history
Co-authored-by: あく <alleteam@gmail.com>
  • Loading branch information
jordam and skotopes authored Sep 27, 2023
1 parent e7b96d2 commit 7b8f268
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/magic/gen4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down
4 changes: 3 additions & 1 deletion lib/magic/gen4.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <lib/nfc/protocols/mifare_classic.h>

#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
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions nfc_magic_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down

0 comments on commit 7b8f268

Please sign in to comment.