|
|
@@ -1,30 +1,34 @@
|
|
|
-#include "core/log.h"
|
|
|
+#include "bit_buffer.h"
|
|
|
+#include "core/check.h"
|
|
|
#include "gen4_poller_i.h"
|
|
|
+#include "magic/protocols/gen4/gen4.h"
|
|
|
+#include "magic/protocols/gen4/gen4_poller.h"
|
|
|
#include <nfc/protocols/iso14443_3a/iso14443_3a.h>
|
|
|
#include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
|
|
|
-#include <nfc/helpers/nfc_util.h>
|
|
|
-#include <bit_lib/bit_lib.h>
|
|
|
#include <nfc/nfc_poller.h>
|
|
|
-
|
|
|
-#include <furi/furi.h>
|
|
|
+#include <bit_lib.h>
|
|
|
+#include <string.h>
|
|
|
|
|
|
#define GEN4_POLLER_THREAD_FLAG_DETECTED (1U << 0)
|
|
|
+#define GEN4_POLLER_DEFAULT_CONFIG_SIZE (28)
|
|
|
|
|
|
typedef NfcCommand (*Gen4PollerStateHandler)(Gen4Poller* instance);
|
|
|
|
|
|
typedef struct {
|
|
|
NfcPoller* poller;
|
|
|
- uint32_t password;
|
|
|
+ Gen4Password password;
|
|
|
+ Gen4 gen4_data;
|
|
|
BitBuffer* tx_buffer;
|
|
|
BitBuffer* rx_buffer;
|
|
|
FuriThreadId thread_id;
|
|
|
Gen4PollerError error;
|
|
|
} Gen4PollerDetectContext;
|
|
|
|
|
|
-static const uint8_t gen4_poller_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};
|
|
|
+static const Gen4Config gen4_poller_default_config = {
|
|
|
+ .data_raw = {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}};
|
|
|
+
|
|
|
static const uint8_t gen4_poller_default_block_0[GEN4_POLLER_BLOCK_SIZE] =
|
|
|
{0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
|
|
|
@@ -56,6 +60,8 @@ Gen4Poller* gen4_poller_alloc(Nfc* nfc) {
|
|
|
instance->tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
|
|
|
instance->rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
|
|
|
|
|
|
+ instance->gen4_data = gen4_alloc();
|
|
|
+
|
|
|
return instance;
|
|
|
}
|
|
|
|
|
|
@@ -67,10 +73,12 @@ void gen4_poller_free(Gen4Poller* instance) {
|
|
|
bit_buffer_free(instance->tx_buffer);
|
|
|
bit_buffer_free(instance->rx_buffer);
|
|
|
|
|
|
+ gen4_free(instance->gen4_data);
|
|
|
+
|
|
|
free(instance);
|
|
|
}
|
|
|
|
|
|
-void gen4_poller_set_password(Gen4Poller* instance, uint32_t password) {
|
|
|
+void gen4_poller_set_password(Gen4Poller* instance, Gen4Password password) {
|
|
|
furi_assert(instance);
|
|
|
|
|
|
instance->password = password;
|
|
|
@@ -90,10 +98,12 @@ NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
|
|
|
|
|
|
if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
|
|
|
do {
|
|
|
+ // check config
|
|
|
bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
|
|
|
- uint8_t pwd_arr[4] = {};
|
|
|
- bit_lib_num_to_bytes_be(gen4_poller_detect_ctx->password, COUNT_OF(pwd_arr), pwd_arr);
|
|
|
- bit_buffer_append_bytes(gen4_poller_detect_ctx->tx_buffer, pwd_arr, COUNT_OF(pwd_arr));
|
|
|
+ bit_buffer_append_bytes(
|
|
|
+ gen4_poller_detect_ctx->tx_buffer,
|
|
|
+ gen4_poller_detect_ctx->password.bytes,
|
|
|
+ GEN4_PASSWORD_LEN);
|
|
|
bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_CFG);
|
|
|
|
|
|
Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
|
|
|
@@ -107,11 +117,48 @@ NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
|
|
|
break;
|
|
|
}
|
|
|
size_t rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
|
|
|
- if((rx_bytes != 30) && (rx_bytes != 32)) {
|
|
|
+ if(rx_bytes != GEN4_CONFIG_SIZE) {
|
|
|
gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+ memcpy(
|
|
|
+ gen4_poller_detect_ctx->gen4_data.config.data_raw,
|
|
|
+ bit_buffer_get_data(gen4_poller_detect_ctx->rx_buffer),
|
|
|
+ GEN4_CONFIG_SIZE);
|
|
|
+
|
|
|
+ // check revision
|
|
|
+ bit_buffer_reset(gen4_poller_detect_ctx->tx_buffer);
|
|
|
+ bit_buffer_reset(gen4_poller_detect_ctx->rx_buffer);
|
|
|
+
|
|
|
+ bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
|
|
|
+ bit_buffer_append_bytes(
|
|
|
+ gen4_poller_detect_ctx->tx_buffer,
|
|
|
+ gen4_poller_detect_ctx->password.bytes,
|
|
|
+ GEN4_PASSWORD_LEN);
|
|
|
+ bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_REVISION);
|
|
|
+
|
|
|
+ error = iso14443_3a_poller_send_standard_frame(
|
|
|
+ iso3_poller,
|
|
|
+ gen4_poller_detect_ctx->tx_buffer,
|
|
|
+ gen4_poller_detect_ctx->rx_buffer,
|
|
|
+ GEN4_POLLER_MAX_FWT);
|
|
|
+
|
|
|
+ if(error != Iso14443_3aErrorNone) {
|
|
|
+ gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
|
|
|
+ if(rx_bytes != GEN4_REVISION_SIZE) {
|
|
|
+ gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ memcpy(
|
|
|
+ gen4_poller_detect_ctx->gen4_data.revision.data,
|
|
|
+ bit_buffer_get_data(gen4_poller_detect_ctx->rx_buffer),
|
|
|
+ GEN4_REVISION_SIZE);
|
|
|
+
|
|
|
gen4_poller_detect_ctx->error = Gen4PollerErrorNone;
|
|
|
} while(false);
|
|
|
} else if(iso3_event->type == Iso14443_3aPollerEventTypeError) {
|
|
|
@@ -122,7 +169,7 @@ NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
|
|
|
return command;
|
|
|
}
|
|
|
|
|
|
-Gen4PollerError gen4_poller_detect(Nfc* nfc, uint32_t password) {
|
|
|
+Gen4PollerError gen4_poller_detect(Nfc* nfc, Gen4Password password, Gen4* gen4_data) {
|
|
|
furi_assert(nfc);
|
|
|
|
|
|
Gen4PollerDetectContext gen4_poller_detect_ctx = {};
|
|
|
@@ -146,6 +193,9 @@ Gen4PollerError gen4_poller_detect(Nfc* nfc, uint32_t password) {
|
|
|
bit_buffer_free(gen4_poller_detect_ctx.tx_buffer);
|
|
|
bit_buffer_free(gen4_poller_detect_ctx.rx_buffer);
|
|
|
|
|
|
+ if(gen4_poller_detect_ctx.error == Gen4PollerErrorNone)
|
|
|
+ gen4_copy(gen4_data, &gen4_poller_detect_ctx.gen4_data);
|
|
|
+
|
|
|
return gen4_poller_detect_ctx.error;
|
|
|
}
|
|
|
|
|
|
@@ -153,7 +203,7 @@ NfcCommand gen4_poller_idle_handler(Gen4Poller* instance) {
|
|
|
NfcCommand command = NfcCommandContinue;
|
|
|
|
|
|
instance->current_block = 0;
|
|
|
- memset(instance->config, 0, sizeof(instance->config));
|
|
|
+
|
|
|
instance->gen4_event.type = Gen4PollerEventTypeCardDetected;
|
|
|
command = instance->callback(instance->gen4_event, instance->context);
|
|
|
instance->state = Gen4PollerStateRequestMode;
|
|
|
@@ -172,12 +222,14 @@ NfcCommand gen4_poller_request_mode_handler(Gen4Poller* instance) {
|
|
|
instance->state = Gen4PollerStateRequestWriteData;
|
|
|
} else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetPassword) {
|
|
|
instance->state = Gen4PollerStateChangePassword;
|
|
|
- } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDefaultCFG) {
|
|
|
+ } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetInfo) {
|
|
|
+ instance->state = Gen4PollerStateGetInfo;
|
|
|
+ } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDefaultCfg) {
|
|
|
instance->state = Gen4PollerStateSetDefaultConfig;
|
|
|
- } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetCFG) {
|
|
|
- instance->state = Gen4PollerStateGetCurrentConfig;
|
|
|
- } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetRevision) {
|
|
|
- instance->state = Gen4PollerStateGetRevision;
|
|
|
+ } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetShadowMode) {
|
|
|
+ instance->state = Gen4PollerStateSetShadowMode;
|
|
|
+ } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDirectWriteBlock0Mode) {
|
|
|
+ instance->state = Gen4PollerStateSetDirectWriteBlock0;
|
|
|
} else {
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
}
|
|
|
@@ -194,15 +246,15 @@ NfcCommand gen4_poller_wipe_handler(Gen4Poller* instance) {
|
|
|
error = gen4_poller_set_config(
|
|
|
instance,
|
|
|
instance->password,
|
|
|
- gen4_poller_default_config,
|
|
|
- sizeof(gen4_poller_default_config),
|
|
|
+ &gen4_poller_default_config,
|
|
|
+ GEN4_POLLER_DEFAULT_CONFIG_SIZE,
|
|
|
false);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
FURI_LOG_D(TAG, "Failed to set default config: %d", error);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
break;
|
|
|
}
|
|
|
- instance->password = 0;
|
|
|
+ gen4_password_reset(&instance->password);
|
|
|
error = gen4_poller_write_block(
|
|
|
instance, instance->password, instance->current_block, gen4_poller_default_block_0);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
@@ -257,29 +309,29 @@ static NfcCommand gen4_poller_write_mf_classic(Gen4Poller* instance) {
|
|
|
const MfClassicData* mfc_data = instance->data;
|
|
|
const Iso14443_3aData* iso3_data = mfc_data->iso14443_3a_data;
|
|
|
if(instance->current_block == 0) {
|
|
|
- instance->config[0] = 0x00;
|
|
|
+ instance->config.data_parsed.protocol = Gen4ProtocolMfClassic;
|
|
|
instance->total_blocks = mf_classic_get_total_block_num(mfc_data->type);
|
|
|
|
|
|
if(iso3_data->uid_len == 4) {
|
|
|
- instance->config[1] = Gen4PollerUIDLengthSingle;
|
|
|
+ instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
|
|
|
} else if(iso3_data->uid_len == 7) {
|
|
|
- instance->config[1] = Gen4PollerUIDLengthDouble;
|
|
|
+ instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
|
|
|
} else {
|
|
|
FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- instance->config[6] = Gen4PollerShadowModeIgnore;
|
|
|
- instance->config[24] = iso3_data->atqa[0];
|
|
|
- instance->config[25] = iso3_data->atqa[1];
|
|
|
- instance->config[26] = iso3_data->sak;
|
|
|
- instance->config[27] = 0x00;
|
|
|
- instance->config[28] = instance->total_blocks - 1;
|
|
|
- instance->config[29] = 0x01;
|
|
|
+ instance->config.data_parsed.gtu_mode = Gen4ShadowModeDisabled;
|
|
|
+ instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
|
|
|
+ instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
|
|
|
+ instance->config.data_parsed.sak = iso3_data->sak;
|
|
|
+ instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
|
|
|
+ instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
|
|
|
+ instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
|
|
|
|
|
|
Gen4PollerError error = gen4_poller_set_config(
|
|
|
- instance, instance->password, instance->config, sizeof(instance->config), false);
|
|
|
+ instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
FURI_LOG_D(TAG, "Failed to write config: %d", error);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
@@ -316,7 +368,7 @@ static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
|
|
|
const Iso14443_3aData* iso3_data = mfu_data->iso14443_3a_data;
|
|
|
if(instance->current_block == 0) {
|
|
|
instance->total_blocks = 64;
|
|
|
- instance->config[0] = 0x01;
|
|
|
+ instance->config.data_parsed.protocol = Gen4ProtocolMfUltralight;
|
|
|
switch(mfu_data->type) {
|
|
|
case MfUltralightTypeNTAG203:
|
|
|
case MfUltralightTypeNTAG213:
|
|
|
@@ -326,39 +378,48 @@ static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
|
|
|
case MfUltralightTypeNTAGI2C2K:
|
|
|
case MfUltralightTypeNTAGI2CPlus1K:
|
|
|
case MfUltralightTypeNTAGI2CPlus2K:
|
|
|
- instance->config[27] = Gen4PollerUltralightModeNTAG;
|
|
|
+ FURI_LOG_D(TAG, "NTAG type");
|
|
|
+ instance->config.data_parsed.mfu_mode = Gen4UltralightModeNTAG;
|
|
|
instance->total_blocks = 64 * 2;
|
|
|
break;
|
|
|
|
|
|
+ case MfUltralightTypeUnknown:
|
|
|
+ FURI_LOG_D(TAG, "Ultralight type");
|
|
|
+ instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case MfUltralightTypeMfulC:
|
|
|
+ FURI_LOG_D(TAG, "MfulC type");
|
|
|
+ instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_C;
|
|
|
+ break;
|
|
|
+
|
|
|
case MfUltralightTypeUL11:
|
|
|
case MfUltralightTypeUL21:
|
|
|
- // UL-C?
|
|
|
- // UL?
|
|
|
default:
|
|
|
- instance->config[27] = Gen4PollerUltralightModeUL_EV1;
|
|
|
+ FURI_LOG_D(TAG, "EV1 type");
|
|
|
+ instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if(iso3_data->uid_len == 4) {
|
|
|
- instance->config[1] = Gen4PollerUIDLengthSingle;
|
|
|
+ instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
|
|
|
} else if(iso3_data->uid_len == 7) {
|
|
|
- instance->config[1] = Gen4PollerUIDLengthDouble;
|
|
|
+ instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
|
|
|
} else {
|
|
|
FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- instance->config[6] = Gen4PollerShadowModeHighSpeedIgnore;
|
|
|
- instance->config[24] = iso3_data->atqa[0];
|
|
|
- instance->config[25] = iso3_data->atqa[1];
|
|
|
- instance->config[26] = iso3_data->sak;
|
|
|
- instance->config[27] = 0x00;
|
|
|
- instance->config[28] = instance->total_blocks - 1;
|
|
|
- instance->config[29] = 0x01;
|
|
|
+ instance->config.data_parsed.gtu_mode = Gen4ShadowModeHighSpeedDisabled;
|
|
|
+ instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
|
|
|
+ instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
|
|
|
+ instance->config.data_parsed.sak = iso3_data->sak;
|
|
|
+ instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
|
|
|
+ instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
|
|
|
|
|
|
Gen4PollerError error = gen4_poller_set_config(
|
|
|
- instance, instance->password, instance->config, sizeof(instance->config), false);
|
|
|
+ instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
FURI_LOG_D(TAG, "Failed to write config: %d", error);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
@@ -383,81 +444,104 @@ static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
|
|
|
} else {
|
|
|
uint8_t block[GEN4_POLLER_BLOCK_SIZE] = {};
|
|
|
bool write_success = true;
|
|
|
- for(size_t i = 0; i < 8; i++) {
|
|
|
- memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
|
|
|
- Gen4PollerError error =
|
|
|
- gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- write_success = false;
|
|
|
+
|
|
|
+ if(mf_ultralight_support_feature(
|
|
|
+ mf_ultralight_get_feature_support_set(mfu_data->type),
|
|
|
+ MfUltralightFeatureSupportReadSignature)) {
|
|
|
+ FURI_LOG_D(TAG, "Writing Signature");
|
|
|
+ for(size_t i = 0; i < 8; i++) {
|
|
|
+ memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
|
|
|
+ Gen4PollerError error =
|
|
|
+ gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ write_success = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!write_success) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write Signature");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
break;
|
|
|
}
|
|
|
- }
|
|
|
- if(!write_success) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write Signature");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- block[0] = mfu_data->version.header;
|
|
|
- block[1] = mfu_data->version.vendor_id;
|
|
|
- block[2] = mfu_data->version.prod_type;
|
|
|
- block[3] = mfu_data->version.prod_subtype;
|
|
|
- Gen4PollerError error =
|
|
|
- gen4_poller_write_block(instance, instance->password, 0xFA, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write 1st part Version");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- block[0] = mfu_data->version.prod_ver_major;
|
|
|
- block[1] = mfu_data->version.prod_ver_minor;
|
|
|
- block[2] = mfu_data->version.storage_size;
|
|
|
- block[3] = mfu_data->version.protocol_type;
|
|
|
- error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write 2nd part Version");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
+ } else {
|
|
|
+ FURI_LOG_D(TAG, "Signature is not supported, skipping");
|
|
|
}
|
|
|
|
|
|
- // Password
|
|
|
- MfUltralightConfigPages* config_pages = NULL;
|
|
|
- mf_ultralight_get_config_page(mfu_data, &config_pages);
|
|
|
+ if(mf_ultralight_support_feature(
|
|
|
+ mf_ultralight_get_feature_support_set(mfu_data->type),
|
|
|
+ MfUltralightFeatureSupportReadVersion)) {
|
|
|
+ FURI_LOG_D(TAG, "Writing Version part 1");
|
|
|
+ block[0] = mfu_data->version.header;
|
|
|
+ block[1] = mfu_data->version.vendor_id;
|
|
|
+ block[2] = mfu_data->version.prod_type;
|
|
|
+ block[3] = mfu_data->version.prod_subtype;
|
|
|
+ Gen4PollerError error =
|
|
|
+ gen4_poller_write_block(instance, instance->password, 0xFA, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write 1st part Version");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- block[0] = config_pages->password.data[0];
|
|
|
- block[1] = config_pages->password.data[1];
|
|
|
- block[2] = config_pages->password.data[2];
|
|
|
- block[3] = config_pages->password.data[3];
|
|
|
- error = gen4_poller_write_block(instance, instance->password, 0xE5, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write Password to sector E5");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
- }
|
|
|
- error = gen4_poller_write_block(instance, instance->password, 0xF0, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write Password to sector F0");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
+ FURI_LOG_D(TAG, "Writing Version part 2");
|
|
|
+ block[0] = mfu_data->version.prod_ver_major;
|
|
|
+ block[1] = mfu_data->version.prod_ver_minor;
|
|
|
+ block[2] = mfu_data->version.storage_size;
|
|
|
+ block[3] = mfu_data->version.protocol_type;
|
|
|
+ error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write 2nd part Version");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FURI_LOG_D(TAG, "Version is not supported, skipping");
|
|
|
}
|
|
|
|
|
|
- // PACK
|
|
|
- block[0] = config_pages->pack.data[0];
|
|
|
- block[1] = config_pages->pack.data[1];
|
|
|
- block[2] = 0x00;
|
|
|
- block[3] = 0x00;
|
|
|
- error = gen4_poller_write_block(instance, instance->password, 0xE6, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write PACK to sector E6");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
- }
|
|
|
- error = gen4_poller_write_block(instance, instance->password, 0xF1, block);
|
|
|
- if(error != Gen4PollerErrorNone) {
|
|
|
- FURI_LOG_E(TAG, "Failed to write PACK to sector F1");
|
|
|
- instance->state = Gen4PollerStateFail;
|
|
|
- break;
|
|
|
+ if(mf_ultralight_support_feature(
|
|
|
+ mf_ultralight_get_feature_support_set(mfu_data->type),
|
|
|
+ MfUltralightFeatureSupportPasswordAuth)) {
|
|
|
+ FURI_LOG_D(TAG, "Writing Password");
|
|
|
+ MfUltralightConfigPages* config_pages = NULL;
|
|
|
+ if(mf_ultralight_get_config_page(mfu_data, &config_pages)) {
|
|
|
+ block[0] = config_pages->password.data[0];
|
|
|
+ block[1] = config_pages->password.data[1];
|
|
|
+ block[2] = config_pages->password.data[2];
|
|
|
+ block[3] = config_pages->password.data[3];
|
|
|
+ Gen4PollerError error =
|
|
|
+ gen4_poller_write_block(instance, instance->password, 0xE5, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write Password to sector E5");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ error = gen4_poller_write_block(instance, instance->password, 0xF0, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write Password to sector F0");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ FURI_LOG_D(TAG, "Writing PACK");
|
|
|
+ block[0] = config_pages->pack.data[0];
|
|
|
+ block[1] = config_pages->pack.data[1];
|
|
|
+ block[2] = 0x00;
|
|
|
+ block[3] = 0x00;
|
|
|
+ error = gen4_poller_write_block(instance, instance->password, 0xE6, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write PACK to sector E6");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ error = gen4_poller_write_block(instance, instance->password, 0xF1, block);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to write PACK to sector F1");
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FURI_LOG_D(TAG, "Password is not supported, skipping");
|
|
|
}
|
|
|
|
|
|
instance->state = Gen4PollerStateSuccess;
|
|
|
@@ -470,11 +554,14 @@ static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
|
|
|
NfcCommand gen4_poller_write_handler(Gen4Poller* instance) {
|
|
|
NfcCommand command = NfcCommandContinue;
|
|
|
|
|
|
- memcpy(instance->config, gen4_poller_default_config, sizeof(gen4_poller_default_config));
|
|
|
- uint8_t password_arr[4] = {};
|
|
|
- bit_lib_num_to_bytes_be(instance->password, sizeof(password_arr), password_arr);
|
|
|
- memcpy(&instance->config[2], password_arr, sizeof(password_arr));
|
|
|
- memset(&instance->config[7], 0, 17);
|
|
|
+ memcpy(
|
|
|
+ instance->config.data_raw,
|
|
|
+ gen4_poller_default_config.data_raw,
|
|
|
+ GEN4_POLLER_DEFAULT_CONFIG_SIZE);
|
|
|
+
|
|
|
+ memcpy(
|
|
|
+ instance->config.data_parsed.password.bytes, instance->password.bytes, GEN4_PASSWORD_LEN);
|
|
|
+ memset(&instance->config.data_raw[7], 0, 17);
|
|
|
if(instance->protocol == NfcProtocolMfClassic) {
|
|
|
command = gen4_poller_write_mf_classic(instance);
|
|
|
} else if(instance->protocol == NfcProtocolMfUltralight) {
|
|
|
@@ -494,7 +581,7 @@ NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
|
|
|
command = instance->callback(instance->gen4_event, instance->context);
|
|
|
if(command != NfcCommandContinue) break;
|
|
|
|
|
|
- uint32_t new_password = instance->gen4_event_data.request_password.password;
|
|
|
+ Gen4Password new_password = instance->gen4_event_data.request_password.password;
|
|
|
Gen4PollerError error =
|
|
|
gen4_poller_change_password(instance, instance->password, new_password);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
@@ -517,8 +604,8 @@ NfcCommand gen4_poller_set_default_cfg_handler(Gen4Poller* instance) {
|
|
|
Gen4PollerError error = gen4_poller_set_config(
|
|
|
instance,
|
|
|
instance->password,
|
|
|
- gen4_poller_default_config,
|
|
|
- sizeof(gen4_poller_default_config),
|
|
|
+ &gen4_poller_default_config,
|
|
|
+ GEN4_POLLER_DEFAULT_CONFIG_SIZE,
|
|
|
false);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
FURI_LOG_E(TAG, "Failed to set default config: %d", error);
|
|
|
@@ -536,16 +623,16 @@ NfcCommand gen4_poller_get_current_cfg_handler(Gen4Poller* instance) {
|
|
|
NfcCommand command = NfcCommandContinue;
|
|
|
|
|
|
do {
|
|
|
- uint8_t the_config[32] = {};
|
|
|
+ Gen4Config config;
|
|
|
|
|
|
- Gen4PollerError error = gen4_poller_get_config(instance, instance->password, the_config);
|
|
|
+ Gen4PollerError error = gen4_poller_get_config(instance, instance->password, &config);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
FURI_LOG_E(TAG, "Failed to get current config: %d", error);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
break;
|
|
|
}
|
|
|
// Copy config data to event data buffer
|
|
|
- memcpy(instance->gen4_event_data.display_config, the_config, sizeof(the_config));
|
|
|
+ memcpy(instance->gen4_data->config.data_raw, config.data_raw, sizeof(config));
|
|
|
|
|
|
instance->state = Gen4PollerStateSuccess;
|
|
|
} while(false);
|
|
|
@@ -557,16 +644,83 @@ NfcCommand gen4_poller_get_revision_handler(Gen4Poller* instance) {
|
|
|
NfcCommand command = NfcCommandContinue;
|
|
|
|
|
|
do {
|
|
|
- uint8_t the_revision[5] = {0};
|
|
|
- Gen4PollerError error =
|
|
|
- gen4_poller_get_revision(instance, instance->password, the_revision);
|
|
|
+ Gen4Revision revision;
|
|
|
+ Gen4PollerError error = gen4_poller_get_revision(instance, instance->password, &revision);
|
|
|
if(error != Gen4PollerErrorNone) {
|
|
|
FURI_LOG_E(TAG, "Failed to get revision: %d", error);
|
|
|
instance->state = Gen4PollerStateFail;
|
|
|
break;
|
|
|
}
|
|
|
// Copy revision data to event data buffer
|
|
|
- memcpy(instance->gen4_event_data.revision_data, the_revision, sizeof(the_revision));
|
|
|
+ memcpy(instance->gen4_data->revision.data, revision.data, sizeof(revision));
|
|
|
+
|
|
|
+ instance->state = Gen4PollerStateSuccess;
|
|
|
+ } while(false);
|
|
|
+
|
|
|
+ return command;
|
|
|
+}
|
|
|
+
|
|
|
+NfcCommand gen4_poller_get_info_handler(Gen4Poller* instance) {
|
|
|
+ NfcCommand command = NfcCommandContinue;
|
|
|
+
|
|
|
+ do {
|
|
|
+ Gen4 gen4_data;
|
|
|
+
|
|
|
+ Gen4PollerError error =
|
|
|
+ gen4_poller_get_revision(instance, instance->password, &gen4_data.revision);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to get revision: %d", error);
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ error = gen4_poller_get_config(instance, instance->password, &gen4_data.config);
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to get current config: %d", error);
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Copy config&&revision data to event data buffer
|
|
|
+ gen4_copy(instance->gen4_data, &gen4_data);
|
|
|
+
|
|
|
+ instance->state = Gen4PollerStateSuccess;
|
|
|
+ } while(false);
|
|
|
+
|
|
|
+ return command;
|
|
|
+}
|
|
|
+
|
|
|
+NfcCommand gen4_poller_set_shadow_mode_handler(Gen4Poller* instance) {
|
|
|
+ NfcCommand command = NfcCommandContinue;
|
|
|
+
|
|
|
+ do {
|
|
|
+ Gen4PollerError error =
|
|
|
+ gen4_poller_set_shadow_mode(instance, instance->password, instance->shadow_mode);
|
|
|
+
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to set shadow mode: %d", error);
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ instance->state = Gen4PollerStateSuccess;
|
|
|
+ } while(false);
|
|
|
+
|
|
|
+ return command;
|
|
|
+}
|
|
|
+
|
|
|
+NfcCommand gen4_poller_set_direct_write_block_0_mode_handler(Gen4Poller* instance) {
|
|
|
+ NfcCommand command = NfcCommandContinue;
|
|
|
+
|
|
|
+ do {
|
|
|
+ Gen4PollerError error = gen4_poller_set_direct_write_block_0_mode(
|
|
|
+ instance, instance->password, instance->direct_write_block_0_mode);
|
|
|
+
|
|
|
+ if(error != Gen4PollerErrorNone) {
|
|
|
+ FURI_LOG_E(TAG, "Failed to set direct write to block 0 mode: %d", error);
|
|
|
+ instance->state = Gen4PollerStateFail;
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
instance->state = Gen4PollerStateSuccess;
|
|
|
} while(false);
|
|
|
@@ -605,9 +759,10 @@ static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNu
|
|
|
[Gen4PollerStateWrite] = gen4_poller_write_handler,
|
|
|
[Gen4PollerStateWipe] = gen4_poller_wipe_handler,
|
|
|
[Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
|
|
|
+ [Gen4PollerStateGetInfo] = gen4_poller_get_info_handler,
|
|
|
[Gen4PollerStateSetDefaultConfig] = gen4_poller_set_default_cfg_handler,
|
|
|
- [Gen4PollerStateGetCurrentConfig] = gen4_poller_get_current_cfg_handler,
|
|
|
- [Gen4PollerStateGetRevision] = gen4_poller_get_revision_handler,
|
|
|
+ [Gen4PollerStateSetShadowMode] = gen4_poller_set_shadow_mode_handler,
|
|
|
+ [Gen4PollerStateSetDirectWriteBlock0] = gen4_poller_set_direct_write_block_0_mode_handler,
|
|
|
[Gen4PollerStateSuccess] = gen4_poller_success_handler,
|
|
|
[Gen4PollerStateFail] = gen4_poller_fail_handler,
|
|
|
|
|
|
@@ -646,3 +801,20 @@ void gen4_poller_stop(Gen4Poller* instance) {
|
|
|
|
|
|
nfc_poller_stop(instance->poller);
|
|
|
}
|
|
|
+
|
|
|
+const Gen4* gen4_poller_get_gen4_data(const Gen4Poller* instance) {
|
|
|
+ furi_assert(instance);
|
|
|
+ return instance->gen4_data;
|
|
|
+}
|
|
|
+
|
|
|
+void gen4_poller_struct_set_direct_write_block_0_mode(
|
|
|
+ Gen4Poller* instance,
|
|
|
+ Gen4DirectWriteBlock0Mode mode) {
|
|
|
+ furi_assert(instance);
|
|
|
+ instance->direct_write_block_0_mode = mode;
|
|
|
+}
|
|
|
+
|
|
|
+void gen4_poller_struct_set_shadow_mode(Gen4Poller* instance, Gen4ShadowMode mode) {
|
|
|
+ furi_assert(instance);
|
|
|
+ instance->shadow_mode = mode;
|
|
|
+}
|