mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 08:18:35 -07:00
Temporarily backport app updates from apps repo
This commit is contained in:
279
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller.c
vendored
Normal file
279
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller.c
vendored
Normal file
@@ -0,0 +1,279 @@
|
||||
#include "gen1a_poller_i.h"
|
||||
#include <nfc/protocols/iso14443_3a/iso14443_3a.h>
|
||||
#include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
|
||||
#include <nfc/helpers/nfc_data_generator.h>
|
||||
|
||||
#include <furi/furi.h>
|
||||
|
||||
#define GEN1A_POLLER_THREAD_FLAG_DETECTED (1U << 0)
|
||||
|
||||
typedef NfcCommand (*Gen1aPollerStateHandler)(Gen1aPoller* instance);
|
||||
|
||||
typedef struct {
|
||||
Nfc* nfc;
|
||||
BitBuffer* tx_buffer;
|
||||
BitBuffer* rx_buffer;
|
||||
FuriThreadId thread_id;
|
||||
bool detected;
|
||||
} Gen1aPollerDetectContext;
|
||||
|
||||
Gen1aPoller* gen1a_poller_alloc(Nfc* nfc) {
|
||||
furi_assert(nfc);
|
||||
|
||||
Gen1aPoller* instance = malloc(sizeof(Gen1aPoller));
|
||||
instance->nfc = nfc;
|
||||
|
||||
nfc_config(instance->nfc, NfcModePoller, NfcTechIso14443a);
|
||||
nfc_set_guard_time_us(instance->nfc, ISO14443_3A_GUARD_TIME_US);
|
||||
nfc_set_fdt_poll_fc(instance->nfc, ISO14443_3A_FDT_POLL_FC);
|
||||
nfc_set_fdt_poll_poll_us(instance->nfc, ISO14443_3A_POLL_POLL_MIN_US);
|
||||
|
||||
instance->tx_buffer = bit_buffer_alloc(GEN1A_POLLER_MAX_BUFFER_SIZE);
|
||||
instance->rx_buffer = bit_buffer_alloc(GEN1A_POLLER_MAX_BUFFER_SIZE);
|
||||
|
||||
instance->mfc_device = nfc_device_alloc();
|
||||
|
||||
instance->gen1a_event.data = &instance->gen1a_event_data;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void gen1a_poller_free(Gen1aPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
bit_buffer_free(instance->tx_buffer);
|
||||
bit_buffer_free(instance->rx_buffer);
|
||||
|
||||
nfc_device_free(instance->mfc_device);
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_detect_callback(NfcEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
|
||||
NfcCommand command = NfcCommandStop;
|
||||
Gen1aPollerDetectContext* gen1a_poller_detect_ctx = context;
|
||||
|
||||
if(event.type == NfcEventTypePollerReady) {
|
||||
do {
|
||||
bit_buffer_set_size(gen1a_poller_detect_ctx->tx_buffer, 7);
|
||||
bit_buffer_set_byte(gen1a_poller_detect_ctx->tx_buffer, 0, 0x40);
|
||||
|
||||
NfcError error = nfc_poller_trx(
|
||||
gen1a_poller_detect_ctx->nfc,
|
||||
gen1a_poller_detect_ctx->tx_buffer,
|
||||
gen1a_poller_detect_ctx->rx_buffer,
|
||||
GEN1A_POLLER_MAX_FWT);
|
||||
|
||||
if(error != NfcErrorNone) break;
|
||||
if(bit_buffer_get_size(gen1a_poller_detect_ctx->rx_buffer) != 4) break;
|
||||
if(bit_buffer_get_byte(gen1a_poller_detect_ctx->rx_buffer, 0) != 0x0A) break;
|
||||
|
||||
gen1a_poller_detect_ctx->detected = true;
|
||||
} while(false);
|
||||
}
|
||||
furi_thread_flags_set(gen1a_poller_detect_ctx->thread_id, GEN1A_POLLER_THREAD_FLAG_DETECTED);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
bool gen1a_poller_detect(Nfc* nfc) {
|
||||
furi_assert(nfc);
|
||||
|
||||
nfc_config(nfc, NfcModePoller, NfcTechIso14443a);
|
||||
nfc_set_guard_time_us(nfc, ISO14443_3A_GUARD_TIME_US);
|
||||
nfc_set_fdt_poll_fc(nfc, ISO14443_3A_FDT_POLL_FC);
|
||||
nfc_set_fdt_poll_poll_us(nfc, ISO14443_3A_POLL_POLL_MIN_US);
|
||||
|
||||
Gen1aPollerDetectContext gen1a_poller_detect_ctx = {};
|
||||
gen1a_poller_detect_ctx.nfc = nfc;
|
||||
gen1a_poller_detect_ctx.tx_buffer = bit_buffer_alloc(GEN1A_POLLER_MAX_BUFFER_SIZE);
|
||||
gen1a_poller_detect_ctx.rx_buffer = bit_buffer_alloc(GEN1A_POLLER_MAX_BUFFER_SIZE);
|
||||
gen1a_poller_detect_ctx.thread_id = furi_thread_get_current_id();
|
||||
gen1a_poller_detect_ctx.detected = false;
|
||||
|
||||
nfc_start(nfc, gen1a_poller_detect_callback, &gen1a_poller_detect_ctx);
|
||||
uint32_t flags = furi_thread_flags_wait(
|
||||
GEN1A_POLLER_THREAD_FLAG_DETECTED, FuriFlagWaitAny, FuriWaitForever);
|
||||
if(flags & GEN1A_POLLER_THREAD_FLAG_DETECTED) {
|
||||
furi_thread_flags_clear(GEN1A_POLLER_THREAD_FLAG_DETECTED);
|
||||
}
|
||||
nfc_stop(nfc);
|
||||
|
||||
bit_buffer_free(gen1a_poller_detect_ctx.tx_buffer);
|
||||
bit_buffer_free(gen1a_poller_detect_ctx.rx_buffer);
|
||||
|
||||
return gen1a_poller_detect_ctx.detected;
|
||||
}
|
||||
|
||||
static void gen1a_poller_reset(Gen1aPoller* instance) {
|
||||
instance->current_block = 0;
|
||||
nfc_data_generator_fill_data(NfcDataGeneratorTypeMfClassic1k_4b, instance->mfc_device);
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_idle_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
gen1a_poller_reset(instance);
|
||||
Gen1aPollerError error = gen1a_poller_wupa(instance);
|
||||
if(error == Gen1aPollerErrorNone) {
|
||||
instance->state = Gen1aPollerStateRequestMode;
|
||||
instance->gen1a_event.type = Gen1aPollerEventTypeDetected;
|
||||
command = instance->callback(instance->gen1a_event, instance->context);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_request_mode_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen1a_event.type = Gen1aPollerEventTypeRequestMode;
|
||||
command = instance->callback(instance->gen1a_event, instance->context);
|
||||
if(instance->gen1a_event_data.request_mode.mode == Gen1aPollerModeWipe) {
|
||||
instance->state = Gen1aPollerStateWipe;
|
||||
} else {
|
||||
instance->state = Gen1aPollerStateWriteDataRequest;
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_wipe_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
Gen1aPollerError error = Gen1aPollerErrorNone;
|
||||
|
||||
const MfClassicData* mfc_data =
|
||||
nfc_device_get_data(instance->mfc_device, NfcProtocolMfClassic);
|
||||
uint16_t total_block_num = mf_classic_get_total_block_num(mfc_data->type);
|
||||
|
||||
if(instance->current_block == total_block_num) {
|
||||
instance->state = Gen1aPollerStateSuccess;
|
||||
} else {
|
||||
do {
|
||||
if(instance->current_block == 0) {
|
||||
error = gen1a_poller_data_access(instance);
|
||||
if(error != Gen1aPollerErrorNone) {
|
||||
instance->state = Gen1aPollerStateFail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
error = gen1a_poller_write_block(
|
||||
instance, instance->current_block, &mfc_data->block[instance->current_block]);
|
||||
if(error != Gen1aPollerErrorNone) {
|
||||
instance->state = Gen1aPollerStateFail;
|
||||
break;
|
||||
}
|
||||
instance->current_block++;
|
||||
} while(false);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_write_data_request_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen1a_event.type = Gen1aPollerEventTypeRequestDataToWrite;
|
||||
command = instance->callback(instance->gen1a_event, instance->context);
|
||||
instance->state = Gen1aPollerStateWrite;
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_write_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
Gen1aPollerError error = Gen1aPollerErrorNone;
|
||||
|
||||
const MfClassicData* mfc_data = instance->gen1a_event_data.data_to_write.mfc_data;
|
||||
uint16_t total_block_num = mf_classic_get_total_block_num(mfc_data->type);
|
||||
|
||||
if(instance->current_block == total_block_num) {
|
||||
instance->state = Gen1aPollerStateSuccess;
|
||||
} else {
|
||||
do {
|
||||
if(instance->current_block == 0) {
|
||||
error = gen1a_poller_data_access(instance);
|
||||
if(error != Gen1aPollerErrorNone) {
|
||||
instance->state = Gen1aPollerStateFail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
error = gen1a_poller_write_block(
|
||||
instance, instance->current_block, &mfc_data->block[instance->current_block]);
|
||||
if(error != Gen1aPollerErrorNone) {
|
||||
instance->state = Gen1aPollerStateFail;
|
||||
break;
|
||||
}
|
||||
instance->current_block++;
|
||||
} while(false);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_success_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen1a_event.type = Gen1aPollerEventTypeSuccess;
|
||||
command = instance->callback(instance->gen1a_event, instance->context);
|
||||
instance->state = Gen1aPollerStateIdle;
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen1a_poller_fail_handler(Gen1aPoller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen1a_event.type = Gen1aPollerEventTypeFail;
|
||||
command = instance->callback(instance->gen1a_event, instance->context);
|
||||
instance->state = Gen1aPollerStateIdle;
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
static const Gen1aPollerStateHandler gen1a_poller_state_handlers[Gen1aPollerStateNum] = {
|
||||
[Gen1aPollerStateIdle] = gen1a_poller_idle_handler,
|
||||
[Gen1aPollerStateRequestMode] = gen1a_poller_request_mode_handler,
|
||||
[Gen1aPollerStateWipe] = gen1a_poller_wipe_handler,
|
||||
[Gen1aPollerStateWriteDataRequest] = gen1a_poller_write_data_request_handler,
|
||||
[Gen1aPollerStateWrite] = gen1a_poller_write_handler,
|
||||
[Gen1aPollerStateSuccess] = gen1a_poller_success_handler,
|
||||
[Gen1aPollerStateFail] = gen1a_poller_fail_handler,
|
||||
|
||||
};
|
||||
|
||||
NfcCommand gen1a_poller_run(NfcEvent event, void* context) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
Gen1aPoller* instance = context;
|
||||
|
||||
if(event.type == NfcEventTypePollerReady) {
|
||||
command = gen1a_poller_state_handlers[instance->state](instance);
|
||||
}
|
||||
|
||||
if(instance->session_state == Gen1aPollerSessionStateStopRequest) {
|
||||
command = NfcCommandStop;
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
void gen1a_poller_start(Gen1aPoller* instance, Gen1aPollerCallback callback, void* context) {
|
||||
furi_assert(instance);
|
||||
furi_assert(callback);
|
||||
|
||||
instance->callback = callback;
|
||||
instance->context = context;
|
||||
|
||||
instance->session_state = Gen1aPollerSessionStateStarted;
|
||||
nfc_start(instance->nfc, gen1a_poller_run, instance);
|
||||
}
|
||||
|
||||
void gen1a_poller_stop(Gen1aPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
instance->session_state = Gen1aPollerSessionStateStopRequest;
|
||||
nfc_stop(instance->nfc);
|
||||
instance->session_state = Gen1aPollerSessionStateIdle;
|
||||
}
|
||||
59
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller.h
vendored
Normal file
59
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller.h
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/nfc.h>
|
||||
#include <nfc/protocols/nfc_generic_event.h>
|
||||
#include <nfc/protocols/mf_classic/mf_classic.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
Gen1aPollerEventTypeDetected,
|
||||
Gen1aPollerEventTypeRequestMode,
|
||||
Gen1aPollerEventTypeRequestDataToWrite,
|
||||
|
||||
Gen1aPollerEventTypeSuccess,
|
||||
Gen1aPollerEventTypeFail,
|
||||
} Gen1aPollerEventType;
|
||||
|
||||
typedef enum {
|
||||
Gen1aPollerModeWipe,
|
||||
Gen1aPollerModeWrite,
|
||||
} Gen1aPollerMode;
|
||||
|
||||
typedef struct {
|
||||
Gen1aPollerMode mode;
|
||||
} Gen1aPollerEventDataRequestMode;
|
||||
|
||||
typedef struct {
|
||||
const MfClassicData* mfc_data;
|
||||
} Gen1aPollerEventDataRequestDataToWrite;
|
||||
|
||||
typedef union {
|
||||
Gen1aPollerEventDataRequestMode request_mode;
|
||||
Gen1aPollerEventDataRequestDataToWrite data_to_write;
|
||||
} Gen1aPollerEventData;
|
||||
|
||||
typedef struct {
|
||||
Gen1aPollerEventType type;
|
||||
Gen1aPollerEventData* data;
|
||||
} Gen1aPollerEvent;
|
||||
|
||||
typedef NfcCommand (*Gen1aPollerCallback)(Gen1aPollerEvent event, void* context);
|
||||
|
||||
typedef struct Gen1aPoller Gen1aPoller;
|
||||
|
||||
bool gen1a_poller_detect(Nfc* nfc);
|
||||
|
||||
Gen1aPoller* gen1a_poller_alloc(Nfc* nfc);
|
||||
|
||||
void gen1a_poller_free(Gen1aPoller* instance);
|
||||
|
||||
void gen1a_poller_start(Gen1aPoller* instance, Gen1aPollerCallback callback, void* context);
|
||||
|
||||
void gen1a_poller_stop(Gen1aPoller* instance);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
132
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller_i.c
vendored
Normal file
132
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller_i.c
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "gen1a_poller_i.h"
|
||||
#include <nfc/helpers/iso14443_crc.h>
|
||||
|
||||
#include <furi/furi.h>
|
||||
|
||||
static Gen1aPollerError gen1a_poller_process_nfc_error(NfcError error) {
|
||||
Gen1aPollerError ret = Gen1aPollerErrorNone;
|
||||
|
||||
if(error == NfcErrorNone) {
|
||||
ret = Gen1aPollerErrorNone;
|
||||
} else if(error == NfcErrorTimeout) {
|
||||
ret = Gen1aPollerErrorTimeout;
|
||||
} else {
|
||||
ret = Gen1aPollerErrorNotPresent;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Gen1aPollerError gen1a_poller_wupa(Gen1aPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
Gen1aPollerError ret = Gen1aPollerErrorNone;
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
|
||||
do {
|
||||
bit_buffer_set_size(instance->tx_buffer, 7);
|
||||
bit_buffer_set_byte(instance->tx_buffer, 0, 0x40);
|
||||
|
||||
NfcError error = nfc_poller_trx(
|
||||
instance->nfc, instance->tx_buffer, instance->rx_buffer, GEN1A_POLLER_MAX_FWT);
|
||||
|
||||
if(error != NfcErrorNone) {
|
||||
ret = gen1a_poller_process_nfc_error(error);
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_size(instance->rx_buffer) != 4) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_byte(instance->rx_buffer, 0) != 0x0A) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Gen1aPollerError gen1a_poller_data_access(Gen1aPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
Gen1aPollerError ret = Gen1aPollerErrorNone;
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
|
||||
do {
|
||||
bit_buffer_set_size(instance->tx_buffer, 8);
|
||||
bit_buffer_set_byte(instance->tx_buffer, 0, 0x43);
|
||||
|
||||
NfcError error = nfc_poller_trx(
|
||||
instance->nfc, instance->tx_buffer, instance->rx_buffer, GEN1A_POLLER_MAX_FWT);
|
||||
|
||||
if(error != NfcErrorNone) {
|
||||
ret = gen1a_poller_process_nfc_error(error);
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_size(instance->rx_buffer) != 4) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_byte(instance->rx_buffer, 0) != 0x0A) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Gen1aPollerError gen1a_poller_write_block(
|
||||
Gen1aPoller* instance,
|
||||
uint8_t block_num,
|
||||
const MfClassicBlock* block) {
|
||||
furi_assert(instance);
|
||||
furi_assert(block);
|
||||
|
||||
Gen1aPollerError ret = Gen1aPollerErrorNone;
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
|
||||
do {
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0xA0);
|
||||
bit_buffer_append_byte(instance->tx_buffer, block_num);
|
||||
iso14443_crc_append(Iso14443CrcTypeA, instance->tx_buffer);
|
||||
|
||||
NfcError error = nfc_poller_trx(
|
||||
instance->nfc, instance->tx_buffer, instance->rx_buffer, GEN1A_POLLER_MAX_FWT);
|
||||
|
||||
if(error != NfcErrorNone) {
|
||||
ret = gen1a_poller_process_nfc_error(error);
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_size(instance->rx_buffer) != 4) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_byte(instance->rx_buffer, 0) != 0x0A) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
|
||||
bit_buffer_copy_bytes(instance->tx_buffer, block->data, sizeof(MfClassicBlock));
|
||||
iso14443_crc_append(Iso14443CrcTypeA, instance->tx_buffer);
|
||||
|
||||
error = nfc_poller_trx(
|
||||
instance->nfc, instance->tx_buffer, instance->rx_buffer, GEN1A_POLLER_MAX_FWT);
|
||||
|
||||
if(error != NfcErrorNone) {
|
||||
ret = gen1a_poller_process_nfc_error(error);
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_size(instance->rx_buffer) != 4) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
if(bit_buffer_get_byte(instance->rx_buffer, 0) != 0x0A) {
|
||||
ret = Gen1aPollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
67
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller_i.h
vendored
Normal file
67
applications/external/nfc_magic/lib/magic/protocols/gen1a/gen1a_poller_i.h
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include "gen1a_poller.h"
|
||||
#include <nfc/protocols/nfc_generic_event.h>
|
||||
#include <nfc/nfc_device.h>
|
||||
#include <nfc/protocols/mf_classic/mf_classic.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GEN1A_POLLER_MAX_BUFFER_SIZE (64U)
|
||||
#define GEN1A_POLLER_MAX_FWT (60000U)
|
||||
|
||||
typedef enum {
|
||||
Gen1aPollerErrorNone,
|
||||
Gen1aPollerErrorTimeout,
|
||||
Gen1aPollerErrorNotPresent,
|
||||
Gen1aPollerErrorProtocol,
|
||||
} Gen1aPollerError;
|
||||
|
||||
typedef enum {
|
||||
Gen1aPollerStateIdle,
|
||||
Gen1aPollerStateRequestMode,
|
||||
Gen1aPollerStateWipe,
|
||||
Gen1aPollerStateWriteDataRequest,
|
||||
Gen1aPollerStateWrite,
|
||||
Gen1aPollerStateSuccess,
|
||||
Gen1aPollerStateFail,
|
||||
|
||||
Gen1aPollerStateNum,
|
||||
} Gen1aPollerState;
|
||||
|
||||
typedef enum {
|
||||
Gen1aPollerSessionStateIdle,
|
||||
Gen1aPollerSessionStateStarted,
|
||||
Gen1aPollerSessionStateStopRequest,
|
||||
} Gen1aPollerSessionState;
|
||||
|
||||
struct Gen1aPoller {
|
||||
Nfc* nfc;
|
||||
Gen1aPollerState state;
|
||||
Gen1aPollerSessionState session_state;
|
||||
|
||||
uint16_t current_block;
|
||||
NfcDevice* mfc_device;
|
||||
|
||||
BitBuffer* tx_buffer;
|
||||
BitBuffer* rx_buffer;
|
||||
|
||||
Gen1aPollerEvent gen1a_event;
|
||||
Gen1aPollerEventData gen1a_event_data;
|
||||
|
||||
Gen1aPollerCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
Gen1aPollerError gen1a_poller_wupa(Gen1aPoller* instance);
|
||||
|
||||
Gen1aPollerError gen1a_poller_data_access(Gen1aPoller* instance);
|
||||
|
||||
Gen1aPollerError
|
||||
gen1a_poller_write_block(Gen1aPoller* instance, uint8_t block_num, const MfClassicBlock* block);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
533
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller.c
vendored
Normal file
533
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller.c
vendored
Normal file
@@ -0,0 +1,533 @@
|
||||
#include "gen4_poller_i.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 <nfc/nfc_poller.h>
|
||||
|
||||
#include <furi/furi.h>
|
||||
|
||||
#define GEN4_POLLER_THREAD_FLAG_DETECTED (1U << 0)
|
||||
|
||||
typedef NfcCommand (*Gen4PollerStateHandler)(Gen4Poller* instance);
|
||||
|
||||
typedef struct {
|
||||
NfcPoller* poller;
|
||||
uint32_t password;
|
||||
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 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};
|
||||
|
||||
static const uint8_t gen4_poller_default_empty_block[GEN4_POLLER_BLOCK_SIZE] = {0};
|
||||
|
||||
static const uint8_t gen4_poller_default_sector_trailer_block[GEN4_POLLER_BLOCK_SIZE] =
|
||||
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
static bool gen4_poller_is_sector_trailer(uint8_t block_num) {
|
||||
uint8_t sec_tr_block_num = 0;
|
||||
|
||||
if(block_num < 128) {
|
||||
sec_tr_block_num = block_num | 0x03;
|
||||
} else {
|
||||
sec_tr_block_num = block_num | 0x0f;
|
||||
}
|
||||
|
||||
return block_num == sec_tr_block_num;
|
||||
}
|
||||
|
||||
Gen4Poller* gen4_poller_alloc(Nfc* nfc) {
|
||||
furi_assert(nfc);
|
||||
|
||||
Gen4Poller* instance = malloc(sizeof(Gen4Poller));
|
||||
instance->poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
|
||||
|
||||
instance->gen4_event.data = &instance->gen4_event_data;
|
||||
|
||||
instance->tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
|
||||
instance->rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void gen4_poller_free(Gen4Poller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
nfc_poller_free(instance->poller);
|
||||
|
||||
bit_buffer_free(instance->tx_buffer);
|
||||
bit_buffer_free(instance->rx_buffer);
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void gen4_poller_set_password(Gen4Poller* instance, uint32_t password) {
|
||||
furi_assert(instance);
|
||||
|
||||
instance->password = password;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
furi_assert(event.protocol == NfcProtocolIso14443_3a);
|
||||
furi_assert(event.instance);
|
||||
furi_assert(event.event_data);
|
||||
|
||||
NfcCommand command = NfcCommandStop;
|
||||
Gen4PollerDetectContext* gen4_poller_detect_ctx = context;
|
||||
Iso14443_3aPoller* iso3_poller = event.instance;
|
||||
Iso14443_3aPollerEvent* iso3_event = event.event_data;
|
||||
gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
|
||||
|
||||
if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
|
||||
do {
|
||||
bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
|
||||
uint8_t pwd_arr[4] = {};
|
||||
nfc_util_num2bytes(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_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_CFG);
|
||||
|
||||
Iso14443_3aError 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;
|
||||
}
|
||||
size_t rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
|
||||
if((rx_bytes != 30) && (rx_bytes != 32)) {
|
||||
gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
|
||||
gen4_poller_detect_ctx->error = Gen4PollerErrorNone;
|
||||
} while(false);
|
||||
} else if(iso3_event->type == Iso14443_3aPollerEventTypeError) {
|
||||
gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
|
||||
}
|
||||
furi_thread_flags_set(gen4_poller_detect_ctx->thread_id, GEN4_POLLER_THREAD_FLAG_DETECTED);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
Gen4PollerError gen4_poller_detect(Nfc* nfc, uint32_t password) {
|
||||
furi_assert(nfc);
|
||||
|
||||
Gen4PollerDetectContext gen4_poller_detect_ctx = {};
|
||||
gen4_poller_detect_ctx.poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
|
||||
gen4_poller_detect_ctx.password = password;
|
||||
gen4_poller_detect_ctx.tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
|
||||
gen4_poller_detect_ctx.rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
|
||||
gen4_poller_detect_ctx.thread_id = furi_thread_get_current_id();
|
||||
gen4_poller_detect_ctx.error = Gen4PollerErrorNone;
|
||||
|
||||
nfc_poller_start(
|
||||
gen4_poller_detect_ctx.poller, gen4_poller_detect_callback, &gen4_poller_detect_ctx);
|
||||
uint32_t flags =
|
||||
furi_thread_flags_wait(GEN4_POLLER_THREAD_FLAG_DETECTED, FuriFlagWaitAny, FuriWaitForever);
|
||||
if(flags & GEN4_POLLER_THREAD_FLAG_DETECTED) {
|
||||
furi_thread_flags_clear(GEN4_POLLER_THREAD_FLAG_DETECTED);
|
||||
}
|
||||
nfc_poller_stop(gen4_poller_detect_ctx.poller);
|
||||
|
||||
nfc_poller_free(gen4_poller_detect_ctx.poller);
|
||||
bit_buffer_free(gen4_poller_detect_ctx.tx_buffer);
|
||||
bit_buffer_free(gen4_poller_detect_ctx.rx_buffer);
|
||||
|
||||
return gen4_poller_detect_ctx.error;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_request_mode_handler(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen4_event.type = Gen4PollerEventTypeRequestMode;
|
||||
command = instance->callback(instance->gen4_event, instance->context);
|
||||
if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWipe) {
|
||||
instance->state = Gen4PollerStateWipe;
|
||||
} else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWrite) {
|
||||
instance->state = Gen4PollerStateRequestWriteData;
|
||||
} else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetPassword) {
|
||||
instance->state = Gen4PollerStateChangePassword;
|
||||
} else {
|
||||
instance->state = Gen4PollerStateFail;
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_wipe_handler(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
do {
|
||||
Gen4PollerError error = Gen4PollerErrorNone;
|
||||
if(instance->current_block == 0) {
|
||||
error = gen4_poller_set_config(
|
||||
instance,
|
||||
instance->password,
|
||||
gen4_poller_default_config,
|
||||
sizeof(gen4_poller_default_config),
|
||||
false);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to set default config: %d", error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
error = gen4_poller_write_block(
|
||||
instance, instance->password, instance->current_block, gen4_poller_default_block_0);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to write 0 block: %d", error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
} else if(instance->current_block < GEN4_POLLER_BLOCKS_TOTAL) {
|
||||
const uint8_t* block = gen4_poller_is_sector_trailer(instance->current_block) ?
|
||||
gen4_poller_default_sector_trailer_block :
|
||||
gen4_poller_default_empty_block;
|
||||
error = gen4_poller_write_block(
|
||||
instance, instance->password, instance->current_block, block);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
instance->state = Gen4PollerStateSuccess;
|
||||
break;
|
||||
}
|
||||
instance->current_block++;
|
||||
} while(false);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_request_write_data_handler(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen4_event.type = Gen4PollerEventTypeRequestDataToWrite;
|
||||
command = instance->callback(instance->gen4_event, instance->context);
|
||||
instance->protocol = instance->gen4_event_data.request_data.protocol;
|
||||
instance->data = instance->gen4_event_data.request_data.data;
|
||||
|
||||
if((instance->protocol == NfcProtocolMfClassic) ||
|
||||
(instance->protocol == NfcProtocolMfUltralight)) {
|
||||
instance->state = Gen4PollerStateWrite;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Unsupported protocol");
|
||||
instance->state = Gen4PollerStateFail;
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
static NfcCommand gen4_poller_write_mf_classic(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
do {
|
||||
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->total_blocks = mf_classic_get_total_block_num(mfc_data->type);
|
||||
|
||||
if(iso3_data->uid_len == 4) {
|
||||
instance->config[1] = Gen4PollerUIDLengthSingle;
|
||||
} else if(iso3_data->uid_len == 7) {
|
||||
instance->config[1] = Gen4PollerUIDLengthDouble;
|
||||
} 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;
|
||||
instance->config[29] = 0x01;
|
||||
|
||||
Gen4PollerError error = gen4_poller_set_config(
|
||||
instance, instance->password, instance->config, sizeof(instance->config), false);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to write config: %d", error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(instance->current_block < instance->total_blocks) {
|
||||
FURI_LOG_D(TAG, "Writing block %d", instance->current_block);
|
||||
Gen4PollerError error = gen4_poller_write_block(
|
||||
instance,
|
||||
instance->password,
|
||||
instance->current_block,
|
||||
mfc_data->block[instance->current_block].data);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
instance->state = Gen4PollerStateSuccess;
|
||||
break;
|
||||
}
|
||||
instance->current_block++;
|
||||
} while(false);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
do {
|
||||
const MfUltralightData* mfu_data = instance->data;
|
||||
const Iso14443_3aData* iso3_data = mfu_data->iso14443_3a_data;
|
||||
if(instance->current_block == 0) {
|
||||
instance->total_blocks = 64;
|
||||
instance->config[0] = 0x01;
|
||||
switch(mfu_data->type) {
|
||||
case MfUltralightTypeNTAG203:
|
||||
case MfUltralightTypeNTAG213:
|
||||
case MfUltralightTypeNTAG215:
|
||||
case MfUltralightTypeNTAG216:
|
||||
case MfUltralightTypeNTAGI2C1K:
|
||||
case MfUltralightTypeNTAGI2C2K:
|
||||
case MfUltralightTypeNTAGI2CPlus1K:
|
||||
case MfUltralightTypeNTAGI2CPlus2K:
|
||||
instance->config[27] = Gen4PollerUltralightModeNTAG;
|
||||
instance->total_blocks = 64 * 2;
|
||||
break;
|
||||
|
||||
case MfUltralightTypeUL11:
|
||||
case MfUltralightTypeUL21:
|
||||
// UL-C?
|
||||
// UL?
|
||||
default:
|
||||
instance->config[27] = Gen4PollerUltralightModeUL_EV1;
|
||||
break;
|
||||
}
|
||||
|
||||
if(iso3_data->uid_len == 4) {
|
||||
instance->config[1] = Gen4PollerUIDLengthSingle;
|
||||
} else if(iso3_data->uid_len == 7) {
|
||||
instance->config[1] = Gen4PollerUIDLengthDouble;
|
||||
} 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;
|
||||
instance->config[29] = 0x01;
|
||||
|
||||
Gen4PollerError error = gen4_poller_set_config(
|
||||
instance, instance->password, instance->config, sizeof(instance->config), false);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to write config: %d", error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(instance->current_block < mfu_data->pages_read) {
|
||||
FURI_LOG_D(
|
||||
TAG, "Writing page %zu / %zu", instance->current_block, mfu_data->pages_read);
|
||||
Gen4PollerError error = gen4_poller_write_block(
|
||||
instance,
|
||||
instance->password,
|
||||
instance->current_block,
|
||||
mfu_data->page[instance->current_block].data);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_D(TAG, "Failed to write %d page: %d", instance->current_block, error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
instance->current_block++;
|
||||
} 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;
|
||||
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;
|
||||
}
|
||||
|
||||
instance->state = Gen4PollerStateSuccess;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
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] = {};
|
||||
nfc_util_num2bytes(instance->password, sizeof(password_arr), password_arr);
|
||||
memcpy(&instance->config[2], password_arr, sizeof(password_arr));
|
||||
memset(&instance->config[7], 0, 17);
|
||||
if(instance->protocol == NfcProtocolMfClassic) {
|
||||
command = gen4_poller_write_mf_classic(instance);
|
||||
} else if(instance->protocol == NfcProtocolMfUltralight) {
|
||||
command = gen4_poller_write_mf_ultralight(instance);
|
||||
} else {
|
||||
furi_crash("Unsupported protocol to write");
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
do {
|
||||
instance->gen4_event.type = Gen4PollerEventTypeRequestNewPassword;
|
||||
command = instance->callback(instance->gen4_event, instance->context);
|
||||
if(command != NfcCommandContinue) break;
|
||||
|
||||
uint32_t new_password = instance->gen4_event_data.request_password.password;
|
||||
Gen4PollerError error =
|
||||
gen4_poller_change_password(instance, instance->password, new_password);
|
||||
if(error != Gen4PollerErrorNone) {
|
||||
FURI_LOG_E(TAG, "Failed to change password: %d", error);
|
||||
instance->state = Gen4PollerStateFail;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->password = new_password;
|
||||
instance->state = Gen4PollerStateSuccess;
|
||||
} while(false);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_success_handler(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen4_event.type = Gen4PollerEventTypeSuccess;
|
||||
command = instance->callback(instance->gen4_event, instance->context);
|
||||
if(command != NfcCommandStop) {
|
||||
furi_delay_ms(100);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
NfcCommand gen4_poller_fail_handler(Gen4Poller* instance) {
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
instance->gen4_event.type = Gen4PollerEventTypeFail;
|
||||
command = instance->callback(instance->gen4_event, instance->context);
|
||||
if(command != NfcCommandStop) {
|
||||
furi_delay_ms(100);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNum] = {
|
||||
[Gen4PollerStateIdle] = gen4_poller_idle_handler,
|
||||
[Gen4PollerStateRequestMode] = gen4_poller_request_mode_handler,
|
||||
[Gen4PollerStateRequestWriteData] = gen4_poller_request_write_data_handler,
|
||||
[Gen4PollerStateWrite] = gen4_poller_write_handler,
|
||||
[Gen4PollerStateWipe] = gen4_poller_wipe_handler,
|
||||
[Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
|
||||
[Gen4PollerStateSuccess] = gen4_poller_success_handler,
|
||||
[Gen4PollerStateFail] = gen4_poller_fail_handler,
|
||||
|
||||
};
|
||||
|
||||
static NfcCommand gen4_poller_callback(NfcGenericEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
furi_assert(event.protocol == NfcProtocolIso14443_3a);
|
||||
furi_assert(event.event_data);
|
||||
furi_assert(event.instance);
|
||||
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
Gen4Poller* instance = context;
|
||||
instance->iso3_poller = event.instance;
|
||||
Iso14443_3aPollerEvent* iso3_event = event.event_data;
|
||||
|
||||
if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
|
||||
command = gen4_poller_state_handlers[instance->state](instance);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context) {
|
||||
furi_assert(instance);
|
||||
furi_assert(callback);
|
||||
|
||||
instance->callback = callback;
|
||||
instance->context = context;
|
||||
|
||||
nfc_poller_start(instance->poller, gen4_poller_callback, instance);
|
||||
}
|
||||
|
||||
void gen4_poller_stop(Gen4Poller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
nfc_poller_stop(instance->poller);
|
||||
}
|
||||
84
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller.h
vendored
Normal file
84
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller.h
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/nfc.h>
|
||||
#include <nfc/protocols/nfc_protocol.h>
|
||||
#include <nfc/protocols/mf_classic/mf_classic.h>
|
||||
#include <nfc/protocols/mf_ultralight/mf_ultralight.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GEN4_CMD_PREFIX (0xCF)
|
||||
#define GEN4_CMD_GET_CFG (0xC6)
|
||||
#define GEN4_CMD_WRITE (0xCD)
|
||||
#define GEN4_CMD_READ (0xCE)
|
||||
#define GEN4_CMD_SET_CFG (0xF0)
|
||||
#define GEN4_CMD_FUSE_CFG (0xF1)
|
||||
#define GEN4_CMD_SET_PWD (0xFE)
|
||||
|
||||
typedef enum {
|
||||
Gen4PollerErrorNone,
|
||||
Gen4PollerErrorTimeout,
|
||||
Gen4PollerErrorProtocol,
|
||||
} Gen4PollerError;
|
||||
|
||||
typedef enum {
|
||||
Gen4PollerEventTypeCardDetected,
|
||||
Gen4PollerEventTypeRequestMode,
|
||||
Gen4PollerEventTypeRequestDataToWrite,
|
||||
Gen4PollerEventTypeRequestNewPassword,
|
||||
|
||||
Gen4PollerEventTypeSuccess,
|
||||
Gen4PollerEventTypeFail,
|
||||
} Gen4PollerEventType;
|
||||
|
||||
typedef enum {
|
||||
Gen4PollerModeWipe,
|
||||
Gen4PollerModeWrite,
|
||||
Gen4PollerModeSetPassword,
|
||||
} Gen4PollerMode;
|
||||
|
||||
typedef struct {
|
||||
Gen4PollerMode mode;
|
||||
} Gen4PollerEventDataRequestMode;
|
||||
|
||||
typedef struct {
|
||||
NfcProtocol protocol;
|
||||
const NfcDeviceData* data;
|
||||
} Gen4PollerEventDataRequestDataToWrite;
|
||||
|
||||
typedef struct {
|
||||
uint32_t password;
|
||||
} Gen4PollerEventDataRequestNewPassword;
|
||||
|
||||
typedef union {
|
||||
Gen4PollerEventDataRequestMode request_mode;
|
||||
Gen4PollerEventDataRequestDataToWrite request_data;
|
||||
Gen4PollerEventDataRequestNewPassword request_password;
|
||||
} Gen4PollerEventData;
|
||||
|
||||
typedef struct {
|
||||
Gen4PollerEventType type;
|
||||
Gen4PollerEventData* data;
|
||||
} Gen4PollerEvent;
|
||||
|
||||
typedef NfcCommand (*Gen4PollerCallback)(Gen4PollerEvent event, void* context);
|
||||
|
||||
typedef struct Gen4Poller Gen4Poller;
|
||||
|
||||
Gen4PollerError gen4_poller_detect(Nfc* nfc, uint32_t password);
|
||||
|
||||
Gen4Poller* gen4_poller_alloc(Nfc* nfc);
|
||||
|
||||
void gen4_poller_free(Gen4Poller* instance);
|
||||
|
||||
void gen4_poller_set_password(Gen4Poller* instance, uint32_t password);
|
||||
|
||||
void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context);
|
||||
|
||||
void gen4_poller_stop(Gen4Poller* instance);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
129
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller_i.c
vendored
Normal file
129
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller_i.c
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "gen4_poller_i.h"
|
||||
|
||||
#include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
|
||||
#include <nfc/helpers/nfc_util.h>
|
||||
|
||||
#define GEN4_CMD_PREFIX (0xCF)
|
||||
|
||||
#define GEN4_CMD_GET_CFG (0xC6)
|
||||
#define GEN4_CMD_WRITE (0xCD)
|
||||
#define GEN4_CMD_READ (0xCE)
|
||||
#define GEN4_CMD_SET_CFG (0xF0)
|
||||
#define GEN4_CMD_FUSE_CFG (0xF1)
|
||||
#define GEN4_CMD_SET_PWD (0xFE)
|
||||
|
||||
static Gen4PollerError gen4_poller_process_error(Iso14443_3aError error) {
|
||||
Gen4PollerError ret = Gen4PollerErrorNone;
|
||||
|
||||
if(error == Iso14443_3aErrorNone) {
|
||||
ret = Gen4PollerErrorNone;
|
||||
} else {
|
||||
ret = Gen4PollerErrorTimeout;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Gen4PollerError gen4_poller_set_config(
|
||||
Gen4Poller* instance,
|
||||
uint32_t password,
|
||||
const uint8_t* config,
|
||||
size_t config_size,
|
||||
bool fuse) {
|
||||
Gen4PollerError ret = Gen4PollerErrorNone;
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
|
||||
do {
|
||||
uint8_t password_arr[4] = {};
|
||||
nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
|
||||
bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
|
||||
uint8_t fuse_config = fuse ? GEN4_CMD_FUSE_CFG : GEN4_CMD_SET_CFG;
|
||||
bit_buffer_append_byte(instance->tx_buffer, fuse_config);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, config, config_size);
|
||||
|
||||
Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
|
||||
instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
|
||||
|
||||
if(error != Iso14443_3aErrorNone) {
|
||||
ret = gen4_poller_process_error(error);
|
||||
break;
|
||||
}
|
||||
|
||||
size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
|
||||
if(rx_bytes != 2) {
|
||||
ret = Gen4PollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Gen4PollerError gen4_poller_write_block(
|
||||
Gen4Poller* instance,
|
||||
uint32_t password,
|
||||
uint8_t block_num,
|
||||
const uint8_t* data) {
|
||||
Gen4PollerError ret = Gen4PollerErrorNone;
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
|
||||
do {
|
||||
uint8_t password_arr[4] = {};
|
||||
nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
|
||||
bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
|
||||
bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_WRITE);
|
||||
bit_buffer_append_byte(instance->tx_buffer, block_num);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, data, GEN4_POLLER_BLOCK_SIZE);
|
||||
|
||||
Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
|
||||
instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
|
||||
|
||||
if(error != Iso14443_3aErrorNone) {
|
||||
ret = gen4_poller_process_error(error);
|
||||
break;
|
||||
}
|
||||
|
||||
size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
|
||||
if(rx_bytes != 2) {
|
||||
ret = Gen4PollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Gen4PollerError
|
||||
gen4_poller_change_password(Gen4Poller* instance, uint32_t pwd_current, uint32_t pwd_new) {
|
||||
Gen4PollerError ret = Gen4PollerErrorNone;
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
|
||||
do {
|
||||
uint8_t password_arr[4] = {};
|
||||
nfc_util_num2bytes(pwd_current, COUNT_OF(password_arr), password_arr);
|
||||
bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
|
||||
|
||||
bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_SET_PWD);
|
||||
nfc_util_num2bytes(pwd_new, COUNT_OF(password_arr), password_arr);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
|
||||
|
||||
Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
|
||||
instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
|
||||
|
||||
if(error != Iso14443_3aErrorNone) {
|
||||
ret = gen4_poller_process_error(error);
|
||||
break;
|
||||
}
|
||||
|
||||
size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
|
||||
if(rx_bytes != 2) {
|
||||
ret = Gen4PollerErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
101
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller_i.h
vendored
Normal file
101
applications/external/nfc_magic/lib/magic/protocols/gen4/gen4_poller_i.h
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
#pragma once
|
||||
|
||||
#include "gen4_poller.h"
|
||||
#include <nfc/nfc_poller.h>
|
||||
#include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
|
||||
|
||||
#define TAG "Gen4Poller"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GEN4_POLLER_MAX_BUFFER_SIZE (64U)
|
||||
#define GEN4_POLLER_MAX_FWT (200000U)
|
||||
|
||||
#define GEN4_POLLER_BLOCK_SIZE (16)
|
||||
#define GEN4_POLLER_BLOCKS_TOTAL (256)
|
||||
|
||||
#define GEN4_POLLER_CONFIG_SIZE_MAX (30)
|
||||
|
||||
typedef enum {
|
||||
Gen4PollerUIDLengthSingle = 0x00,
|
||||
Gen4PollerUIDLengthDouble = 0x01,
|
||||
Gen4PollerUIDLengthTriple = 0x02
|
||||
} Gen4PollerUIDLength;
|
||||
|
||||
typedef enum {
|
||||
Gen4PollerUltralightModeUL_EV1 = 0x00,
|
||||
Gen4PollerUltralightModeNTAG = 0x01,
|
||||
Gen4PollerUltralightModeUL_C = 0x02,
|
||||
Gen4PollerUltralightModeUL = 0x03
|
||||
} Gen4PollerUltralightMode;
|
||||
|
||||
typedef enum {
|
||||
// for writing original (shadow) data
|
||||
Gen4PollerShadowModePreWrite = 0x00,
|
||||
// written data can be read once before restored to original
|
||||
Gen4PollerShadowModeRestore = 0x01,
|
||||
// written data is discarded
|
||||
Gen4PollerShadowModeIgnore = 0x02,
|
||||
// apparently for UL?
|
||||
Gen4PollerShadowModeHighSpeedIgnore = 0x03
|
||||
} Gen4PollerShadowMode;
|
||||
|
||||
typedef enum {
|
||||
Gen4PollerStateIdle,
|
||||
Gen4PollerStateRequestMode,
|
||||
Gen4PollerStateRequestWriteData,
|
||||
Gen4PollerStateWrite,
|
||||
Gen4PollerStateWipe,
|
||||
Gen4PollerStateChangePassword,
|
||||
Gen4PollerStateSuccess,
|
||||
Gen4PollerStateFail,
|
||||
|
||||
Gen4PollerStateNum,
|
||||
} Gen4PollerState;
|
||||
|
||||
struct Gen4Poller {
|
||||
NfcPoller* poller;
|
||||
Iso14443_3aPoller* iso3_poller;
|
||||
Gen4PollerState state;
|
||||
uint32_t password;
|
||||
|
||||
BitBuffer* tx_buffer;
|
||||
BitBuffer* rx_buffer;
|
||||
|
||||
uint16_t current_block;
|
||||
uint16_t total_blocks;
|
||||
|
||||
NfcProtocol protocol;
|
||||
const NfcDeviceData* data;
|
||||
uint32_t new_password;
|
||||
|
||||
uint8_t config[GEN4_POLLER_CONFIG_SIZE_MAX];
|
||||
|
||||
Gen4PollerEvent gen4_event;
|
||||
Gen4PollerEventData gen4_event_data;
|
||||
|
||||
Gen4PollerCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
Gen4PollerError gen4_poller_set_config(
|
||||
Gen4Poller* instance,
|
||||
uint32_t password,
|
||||
const uint8_t* config,
|
||||
size_t config_size,
|
||||
bool fuse);
|
||||
|
||||
Gen4PollerError gen4_poller_write_block(
|
||||
Gen4Poller* instance,
|
||||
uint32_t password,
|
||||
uint8_t block_num,
|
||||
const uint8_t* data);
|
||||
|
||||
Gen4PollerError
|
||||
gen4_poller_change_password(Gen4Poller* instance, uint32_t pwd_current, uint32_t pwd_new);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
14
applications/external/nfc_magic/lib/magic/protocols/nfc_magic_protocols.c
vendored
Normal file
14
applications/external/nfc_magic/lib/magic/protocols/nfc_magic_protocols.c
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "nfc_magic_protocols.h"
|
||||
|
||||
#include <furi/furi.h>
|
||||
|
||||
static const char* nfc_magic_protocol_names[NfcMagicProtocolNum] = {
|
||||
[NfcMagicProtocolGen1] = "Classic Gen 1A/B",
|
||||
[NfcMagicProtocolGen4] = "Gen 4 GTU",
|
||||
};
|
||||
|
||||
const char* nfc_magic_protocols_get_name(NfcMagicProtocol protocol) {
|
||||
furi_assert(protocol < NfcMagicProtocolNum);
|
||||
|
||||
return nfc_magic_protocol_names[protocol];
|
||||
}
|
||||
19
applications/external/nfc_magic/lib/magic/protocols/nfc_magic_protocols.h
vendored
Normal file
19
applications/external/nfc_magic/lib/magic/protocols/nfc_magic_protocols.h
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
NfcMagicProtocolGen1,
|
||||
NfcMagicProtocolGen4,
|
||||
|
||||
NfcMagicProtocolNum,
|
||||
NfcMagicProtocolInvalid,
|
||||
} NfcMagicProtocol;
|
||||
|
||||
const char* nfc_magic_protocols_get_name(NfcMagicProtocol protocol);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user