[FL-1396] Mifare Classic read (#1034)
* rfal: add new data exchange function * core: add FURI_BIT to common defines * furi_hal_nfc: add data exchange with custom patiry bits * lib: extend nfc common API * assets: add mf classic dictionary * lib: introduce mifare classic library * nfc: add dictionary reader helper * nfc worker: add worker events, add mifare classic read * nfc: rework scenes with worker events * nfc: add read mifare classic GUI * nfc device: add mifare classic save * nfc: add dictionary open fail scene * nfc: mention resources * stream: fix stream read line * subghz: rework file read with fixed stream_read_line * furi_hal_nfc: decrease communication timeout * nfc: rework keys load from dictionary with file_stream * nfc: add read mifare classic suggestion * nfc: fix mifare classic read view * nfc: fix index size * nfc: add switch to no dictionary found scene * nfc: add mifare classic load * nfc: improve read mifare classic design * mifare_classic: add proxmark3 mention * nfc: format sources * nfc: fix typos, add documentation
This commit is contained in:
@@ -103,6 +103,61 @@ bool furi_hal_nfc_detect(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool furi_hal_nfc_activate_nfca(uint32_t timeout, uint32_t* cuid) {
|
||||
rfalNfcDevice* dev_list;
|
||||
uint8_t dev_cnt = 0;
|
||||
rfalLowPowerModeStop();
|
||||
rfalNfcState state = rfalNfcGetState();
|
||||
if(state == RFAL_NFC_STATE_NOTINIT) {
|
||||
rfalNfcInitialize();
|
||||
}
|
||||
rfalNfcDiscoverParam params = {
|
||||
.compMode = RFAL_COMPLIANCE_MODE_NFC,
|
||||
.techs2Find = RFAL_NFC_POLL_TECH_A,
|
||||
.totalDuration = 1000,
|
||||
.devLimit = 3,
|
||||
.wakeupEnabled = false,
|
||||
.wakeupConfigDefault = true,
|
||||
.nfcfBR = RFAL_BR_212,
|
||||
.ap2pBR = RFAL_BR_424,
|
||||
.maxBR = RFAL_BR_KEEP,
|
||||
.GBLen = RFAL_NFCDEP_GB_MAX_LEN,
|
||||
.notifyCb = NULL,
|
||||
};
|
||||
uint32_t start = DWT->CYCCNT;
|
||||
rfalNfcDiscover(¶ms);
|
||||
while(state != RFAL_NFC_STATE_ACTIVATED) {
|
||||
rfalNfcWorker();
|
||||
state = rfalNfcGetState();
|
||||
FURI_LOG_T(TAG, "Current state %d", state);
|
||||
if(state == RFAL_NFC_STATE_POLL_ACTIVATION) {
|
||||
start = DWT->CYCCNT;
|
||||
continue;
|
||||
}
|
||||
if(state == RFAL_NFC_STATE_POLL_SELECT) {
|
||||
rfalNfcSelect(0);
|
||||
}
|
||||
if(DWT->CYCCNT - start > timeout * clocks_in_ms) {
|
||||
rfalNfcDeactivate(true);
|
||||
FURI_LOG_T(TAG, "Timeout");
|
||||
return false;
|
||||
}
|
||||
osThreadYield();
|
||||
}
|
||||
rfalNfcGetDevicesFound(&dev_list, &dev_cnt);
|
||||
// Take first device and set cuid
|
||||
if(cuid) {
|
||||
uint8_t* cuid_start = dev_list[0].nfcid;
|
||||
if(dev_list[0].nfcidLen == 7) {
|
||||
cuid_start = &dev_list[0].nfcid[3];
|
||||
}
|
||||
*cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
|
||||
(cuid_start[3]);
|
||||
FURI_LOG_T(TAG, "Activated tag with cuid: %lX", *cuid);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool furi_hal_nfc_listen(
|
||||
uint8_t* uid,
|
||||
uint8_t uid_len,
|
||||
@@ -297,12 +352,10 @@ ReturnCode furi_hal_nfc_data_exchange(
|
||||
rfalNfcWorker();
|
||||
state = rfalNfcGetState();
|
||||
ret = rfalNfcDataExchangeGetStatus();
|
||||
if(ret > ERR_SLEEP_REQ) {
|
||||
return ret;
|
||||
}
|
||||
if(ret == ERR_BUSY) {
|
||||
if(DWT->CYCCNT - start > 1000 * clocks_in_ms) {
|
||||
return ERR_TIMEOUT;
|
||||
ret = ERR_TIMEOUT;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
@@ -314,36 +367,100 @@ ReturnCode furi_hal_nfc_data_exchange(
|
||||
rfalNfcDeactivate(false);
|
||||
rfalLowPowerModeStart();
|
||||
}
|
||||
return ERR_NONE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ReturnCode furi_hal_nfc_raw_bitstream_exchange(
|
||||
uint8_t* tx_buff,
|
||||
uint16_t tx_bit_len,
|
||||
uint8_t** rx_buff,
|
||||
uint16_t** rx_bit_len,
|
||||
bool deactivate) {
|
||||
furi_assert(rx_buff);
|
||||
furi_assert(rx_bit_len);
|
||||
static uint16_t furi_hal_nfc_data_and_parity_to_bitstream(
|
||||
uint8_t* data,
|
||||
uint16_t len,
|
||||
uint8_t* parity,
|
||||
uint8_t* out) {
|
||||
furi_assert(data);
|
||||
furi_assert(out);
|
||||
|
||||
uint8_t next_par_bit = 0;
|
||||
uint16_t curr_bit_pos = 0;
|
||||
for(uint16_t i = 0; i < len; i++) {
|
||||
next_par_bit = FURI_BIT(parity[i / 8], 7 - (i % 8));
|
||||
if(curr_bit_pos % 8 == 0) {
|
||||
out[curr_bit_pos / 8] = data[i];
|
||||
curr_bit_pos += 8;
|
||||
out[curr_bit_pos / 8] = next_par_bit;
|
||||
curr_bit_pos++;
|
||||
} else {
|
||||
out[curr_bit_pos / 8] |= data[i] << curr_bit_pos % 8;
|
||||
out[curr_bit_pos / 8 + 1] = data[i] >> (8 - curr_bit_pos % 8);
|
||||
out[curr_bit_pos / 8 + 1] |= next_par_bit << curr_bit_pos % 8;
|
||||
curr_bit_pos += 9;
|
||||
}
|
||||
}
|
||||
return curr_bit_pos;
|
||||
}
|
||||
|
||||
uint16_t furi_hal_nfc_bitstream_to_data_and_parity(
|
||||
uint8_t* in_buff,
|
||||
uint16_t in_buff_bits,
|
||||
uint8_t* out_data,
|
||||
uint8_t* out_parity) {
|
||||
if(in_buff_bits % 9 != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t curr_byte = 0;
|
||||
uint16_t bit_processed = 0;
|
||||
memset(out_parity, 0, in_buff_bits / 9);
|
||||
while(bit_processed < in_buff_bits) {
|
||||
out_data[curr_byte] = in_buff[bit_processed / 8] >> bit_processed % 8;
|
||||
out_data[curr_byte] |= in_buff[bit_processed / 8 + 1] << (8 - bit_processed % 8);
|
||||
out_parity[curr_byte / 8] |= FURI_BIT(in_buff[bit_processed / 8 + 1], bit_processed % 8)
|
||||
<< (7 - curr_byte % 8);
|
||||
bit_processed += 9;
|
||||
curr_byte++;
|
||||
}
|
||||
return curr_byte;
|
||||
}
|
||||
|
||||
bool furi_hal_nfc_tx_rx(FuriHalNfcTxRxContext* tx_rx_ctx) {
|
||||
furi_assert(tx_rx_ctx);
|
||||
|
||||
ReturnCode ret;
|
||||
rfalNfcState state = RFAL_NFC_STATE_ACTIVATED;
|
||||
ret =
|
||||
rfalNfcDataExchangeStart(tx_buff, tx_bit_len, rx_buff, rx_bit_len, 0, RFAL_TXRX_FLAGS_RAW);
|
||||
uint8_t temp_tx_buff[FURI_HAL_NFC_DATA_BUFF_SIZE] = {};
|
||||
uint16_t temp_tx_bits = 0;
|
||||
uint8_t* temp_rx_buff = NULL;
|
||||
uint16_t* temp_rx_bits = NULL;
|
||||
|
||||
// Prepare data for FIFO if necessary
|
||||
if(tx_rx_ctx->tx_rx_type == FURI_HAL_NFC_TXRX_RAW) {
|
||||
temp_tx_bits = furi_hal_nfc_data_and_parity_to_bitstream(
|
||||
tx_rx_ctx->tx_data, tx_rx_ctx->tx_bits / 8, tx_rx_ctx->tx_parity, temp_tx_buff);
|
||||
ret = rfalNfcDataExchangeCustomStart(
|
||||
temp_tx_buff,
|
||||
temp_tx_bits,
|
||||
&temp_rx_buff,
|
||||
&temp_rx_bits,
|
||||
RFAL_FWT_NONE,
|
||||
tx_rx_ctx->tx_rx_type);
|
||||
} else {
|
||||
ret = rfalNfcDataExchangeCustomStart(
|
||||
tx_rx_ctx->tx_data,
|
||||
tx_rx_ctx->tx_bits,
|
||||
&temp_rx_buff,
|
||||
&temp_rx_bits,
|
||||
RFAL_FWT_NONE,
|
||||
tx_rx_ctx->tx_rx_type);
|
||||
}
|
||||
if(ret != ERR_NONE) {
|
||||
return ret;
|
||||
return false;
|
||||
}
|
||||
uint32_t start = DWT->CYCCNT;
|
||||
while(state != RFAL_NFC_STATE_DATAEXCHANGE_DONE) {
|
||||
rfalNfcWorker();
|
||||
state = rfalNfcGetState();
|
||||
ret = rfalNfcDataExchangeGetStatus();
|
||||
if(ret > ERR_SLEEP_REQ) {
|
||||
return ret;
|
||||
}
|
||||
if(ret == ERR_BUSY) {
|
||||
if(DWT->CYCCNT - start > 1000 * clocks_in_ms) {
|
||||
return ERR_TIMEOUT;
|
||||
if(DWT->CYCCNT - start > 4 * clocks_in_ms) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
@@ -351,11 +468,16 @@ ReturnCode furi_hal_nfc_raw_bitstream_exchange(
|
||||
}
|
||||
taskYIELD();
|
||||
}
|
||||
if(deactivate) {
|
||||
rfalNfcDeactivate(false);
|
||||
rfalLowPowerModeStart();
|
||||
|
||||
if(tx_rx_ctx->tx_rx_type == FURI_HAL_NFC_TXRX_RAW) {
|
||||
tx_rx_ctx->rx_bits =
|
||||
8 * furi_hal_nfc_bitstream_to_data_and_parity(
|
||||
temp_rx_buff, *temp_rx_bits, tx_rx_ctx->rx_data, tx_rx_ctx->rx_parity);
|
||||
} else {
|
||||
memcpy(tx_rx_ctx->rx_data, temp_rx_buff, *temp_rx_bits / 8);
|
||||
}
|
||||
return ERR_NONE;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void furi_hal_nfc_deactivate() {
|
||||
|
52
firmware/targets/furi_hal_include/furi_hal_nfc.h
Normal file → Executable file
52
firmware/targets/furi_hal_include/furi_hal_nfc.h
Normal file → Executable file
@@ -15,6 +15,8 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define FURI_HAL_NFC_UID_MAX_LEN 10
|
||||
#define FURI_HAL_NFC_DATA_BUFF_SIZE (64)
|
||||
#define FURI_HAL_NFC_PARITY_BUFF_SIZE (FURI_HAL_NFC_DATA_BUFF_SIZE / 8)
|
||||
|
||||
#define FURI_HAL_NFC_TXRX_DEFAULT \
|
||||
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
|
||||
@@ -22,10 +24,22 @@ extern "C" {
|
||||
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
|
||||
|
||||
#define FURI_HAL_NFC_TXRX_RAW \
|
||||
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
|
||||
#define FURI_HAL_NFC_TX_DEFAULT_RX_NO_CRC \
|
||||
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
|
||||
|
||||
#define FURI_HAL_NFC_TXRX_WITH_PAR \
|
||||
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
|
||||
|
||||
#define FURI_HAL_NFC_TXRX_RAW \
|
||||
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCIP1_OFF | (uint32_t)RFAL_TXRX_FLAGS_AGC_ON | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE | \
|
||||
(uint32_t)RFAL_TXRX_FLAGS_NFCV_FLAG_AUTO)
|
||||
|
||||
typedef bool (*FuriHalNfcEmulateCallback)(
|
||||
@@ -36,6 +50,16 @@ typedef bool (*FuriHalNfcEmulateCallback)(
|
||||
uint32_t* flags,
|
||||
void* context);
|
||||
|
||||
typedef struct {
|
||||
uint8_t tx_data[FURI_HAL_NFC_DATA_BUFF_SIZE];
|
||||
uint8_t tx_parity[FURI_HAL_NFC_PARITY_BUFF_SIZE];
|
||||
uint16_t tx_bits;
|
||||
uint8_t rx_data[FURI_HAL_NFC_DATA_BUFF_SIZE];
|
||||
uint8_t rx_parity[FURI_HAL_NFC_PARITY_BUFF_SIZE];
|
||||
uint16_t rx_bits;
|
||||
uint32_t tx_rx_type;
|
||||
} FuriHalNfcTxRxContext;
|
||||
|
||||
/** Init nfc
|
||||
*/
|
||||
void furi_hal_nfc_init();
|
||||
@@ -77,6 +101,15 @@ bool furi_hal_nfc_detect(
|
||||
uint32_t timeout,
|
||||
bool deactivate);
|
||||
|
||||
/** Activate NFC-A tag
|
||||
*
|
||||
* @param timeout timeout in ms
|
||||
* @param cuid pointer to 32bit uid
|
||||
*
|
||||
* @return true on succeess
|
||||
*/
|
||||
bool furi_hal_nfc_activate_nfca(uint32_t timeout, uint32_t* cuid);
|
||||
|
||||
/** NFC listen
|
||||
*
|
||||
* @param uid pointer to uid buffer
|
||||
@@ -131,12 +164,13 @@ ReturnCode furi_hal_nfc_data_exchange(
|
||||
uint16_t** rx_len,
|
||||
bool deactivate);
|
||||
|
||||
ReturnCode furi_hal_nfc_raw_bitstream_exchange(
|
||||
uint8_t* tx_buff,
|
||||
uint16_t tx_bit_len,
|
||||
uint8_t** rx_buff,
|
||||
uint16_t** rx_bit_len,
|
||||
bool deactivate);
|
||||
/** NFC data exchange
|
||||
*
|
||||
* @param tx_rx_ctx FuriHalNfcTxRxContext instance
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool furi_hal_nfc_tx_rx(FuriHalNfcTxRxContext* tx_rx_ctx);
|
||||
|
||||
/** NFC deactivate and start sleep
|
||||
*/
|
||||
|
Reference in New Issue
Block a user