1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-03-15 15:46:06 +00:00

refactor(core): Refactor requested changes from review

This commit is contained in:
kopecdav 2025-02-07 13:38:18 +01:00
parent 840d7d7ce1
commit bd5de3ab87
8 changed files with 239 additions and 161 deletions

View File

@ -20,7 +20,7 @@
#pragma once #pragma once
#include "trezor_types.h" #include <trezor_types.h>
#define NFC_MAX_UID_LEN 10 #define NFC_MAX_UID_LEN 10
@ -45,7 +45,7 @@ typedef enum {
typedef enum { typedef enum {
NFC_NO_EVENT, NFC_NO_EVENT,
NFC_STATE_ACTIVATED, NFC_EVENT_ACTIVATED,
} nfc_event_t; } nfc_event_t;
typedef enum { typedef enum {
@ -84,7 +84,7 @@ nfc_status_t nfc_deactivate_stm(void);
// Calls NFC RFAL worker to service the NFC state machine and expolore // Calls NFC RFAL worker to service the NFC state machine and expolore
// registered technologies. This function has to be actively called in loop // registered technologies. This function has to be actively called in loop
// (main NFC poll function), returns nfc event. // (main NFC poll function), returns nfc event.
nfc_event_t nfc_get_event(void); nfc_status_t nfc_get_event(nfc_event_t *event);
// Deactivate the currently activated NFC device and put RFAL state machine back to // Deactivate the currently activated NFC device and put RFAL state machine back to
// discovary state. // discovary state.

View File

@ -176,7 +176,7 @@ static uint8_t InformationBlock[] = {
* @retval False : Different command. * @retval False : Different command.
***************************************************************************** *****************************************************************************
*/ */
static bool cmdCompare(uint8_t *cmd, uint8_t *find, uint16_t len) { static bool cmd_compare(uint8_t *cmd, uint8_t *find, uint16_t len) {
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
if (!memcmp(&cmd[i], find, len)) { if (!memcmp(&cmd[i], find, len)) {
return true; return true;
@ -212,17 +212,17 @@ static uint16_t card_emulation_t4t_select(uint8_t *cmdData, uint8_t *rspData) {
uint8_t fidNDEF[] = {FID_NDEF >> 8, FID_NDEF & 0xFF}; uint8_t fidNDEF[] = {FID_NDEF >> 8, FID_NDEF & 0xFF};
uint8_t selectFileId[] = {0xA4, 0x00, 0x0C, 0x02, 0x00, 0x01}; uint8_t selectFileId[] = {0xA4, 0x00, 0x0C, 0x02, 0x00, 0x01};
if (cmdCompare(cmdData, aid, sizeof(aid))) { /* Select Appli */ if (cmd_compare(cmdData, aid, sizeof(aid))) { /* Select Appli */
nState = STATE_APP_SELECTED; nState = STATE_APP_SELECTED;
success = true; success = true;
} else if ((nState >= STATE_APP_SELECTED) && } else if ((nState >= STATE_APP_SELECTED) &&
cmdCompare(cmdData, fidCC, sizeof(fidCC))) { /* Select CC */ cmd_compare(cmdData, fidCC, sizeof(fidCC))) { /* Select CC */
nState = STATE_CC_SELECTED; nState = STATE_CC_SELECTED;
nSelectedIdx = 0; nSelectedIdx = 0;
success = true; success = true;
} else if ((nState >= STATE_APP_SELECTED) && } else if ((nState >= STATE_APP_SELECTED) &&
(cmdCompare(cmdData, fidNDEF, sizeof(fidNDEF)) || (cmd_compare(cmdData, fidNDEF, sizeof(fidNDEF)) ||
cmdCompare(cmdData, selectFileId, cmd_compare(cmdData, selectFileId,
sizeof(selectFileId)))) { /* Select NDEF */ sizeof(selectFileId)))) { /* Select NDEF */
nState = STATE_FID_SELECTED; nState = STATE_FID_SELECTED;
nSelectedIdx = 1; nSelectedIdx = 1;

View File

@ -24,34 +24,57 @@
ndef_status_t ndef_parse_message(const uint8_t *buffer, uint16_t buffer_len, ndef_status_t ndef_parse_message(const uint8_t *buffer, uint16_t buffer_len,
ndef_message_t *message) { ndef_message_t *message) {
memset(message, 0, sizeof(ndef_message_t)); memset(message, 0, sizeof(ndef_message_t));
uint16_t remaining_len = buffer_len; int16_t remaining_len = buffer_len;
if (remaining_len <= 0){
return NDEF_ERROR;
}
// Indicate TLV header // Indicate TLV header
if (*buffer == 0x3) { if (*buffer == 0x3) {
buffer++; buffer++;
remaining_len--;
} else { } else {
return NDEF_ERROR; // Not a valid TLV structure return NDEF_ERROR; // Not a valid TLV structure
} }
// TLV length if(remaining_len <= 0){
if (*buffer == 0xFF) { return NDEF_ERROR;
// TLV 3 byte length format
buffer++;
message->message_total_len = (int16_t)(buffer[0] << 8 | buffer[1]);
buffer = buffer + 2;
} else {
message->message_total_len = *buffer;
buffer++;
} }
// TLV length
if (*buffer == 0xFF) {
// TLV 3 byte length format
buffer++;
remaining_len--;
if (remaining_len < 2) {
return NDEF_ERROR;
}
message->message_total_len = (int16_t)(buffer[0] << 8 | buffer[1]);
buffer = buffer + 2;
remaining_len = remaining_len - 2;
} else {
message->message_total_len = *buffer;
buffer++;
remaining_len--;
}
if(message->message_total_len > remaining_len){
return NDEF_ERROR; // Not enough room to cover while message
}
remaining_len = message->message_total_len; remaining_len = message->message_total_len;
while (1) { while (1) {
ndef_parse_record(buffer, remaining_len, ndef_parse_record(buffer, remaining_len,
&(message->records[message->records_cnt])); &message->records[message->records_cnt]);
buffer += message->records[message->records_cnt].record_total_len; buffer += message->records[message->records_cnt].record_total_len;
remaining_len -= message->records[message->records_cnt].record_total_len; remaining_len -= message->records[message->records_cnt].record_total_len;
message->records_cnt++; message->records_cnt++;
@ -84,7 +107,7 @@ ndef_status_t ndef_parse_record(const uint8_t *buffer, uint16_t len,
} }
// Look at first byte, parse header // Look at first byte, parse header
memcpy(&(rec->header), buffer, 1); memcpy(&rec->header, buffer, 1);
bp++; bp++;
if (rec->header.tnf == 0x00 || rec->header.tnf > 0x06) { if (rec->header.tnf == 0x00 || rec->header.tnf > 0x06) {
@ -96,7 +119,7 @@ ndef_status_t ndef_parse_record(const uint8_t *buffer, uint16_t len,
if (rec->header.sr) { if (rec->header.sr) {
rec->payload_length = buffer[bp++]; rec->payload_length = buffer[bp++];
} else { } else {
memcpy(&(rec->payload_length), buffer + bp, 4); memcpy(&rec->payload_length, buffer + bp, 4);
bp += 4; bp += 4;
} }
@ -108,7 +131,7 @@ ndef_status_t ndef_parse_record(const uint8_t *buffer, uint16_t len,
} }
if (rec->type_length > 0) { if (rec->type_length > 0) {
memcpy(&(rec->type), buffer + bp, rec->type_length); memcpy(&rec->type, buffer + bp, rec->type_length);
bp += rec->type_length; bp += rec->type_length;
} else { } else {
// Type length ommited // Type length ommited
@ -116,7 +139,7 @@ ndef_status_t ndef_parse_record(const uint8_t *buffer, uint16_t len,
} }
if (rec->id_length > 0) { if (rec->id_length > 0) {
memcpy(&(rec->id), buffer + bp, rec->id_length); memcpy(&rec->id, buffer + bp, rec->id_length);
bp += rec->id_length; bp += rec->id_length;
} else { } else {
// ID length ommited // ID length ommited

View File

@ -1,3 +1,22 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once #pragma once
#include <trezor_types.h> #include <trezor_types.h>

View File

@ -17,15 +17,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <trezor_bsp.h> #include <trezor_bsp.h>
#include <trezor_rtl.h> #include <trezor_rtl.h>
#include <sys/irq.h> #include <sys/irq.h>
#include <sys/systick.h> #include <sys/systick.h>
#include <rtl/strutils.h> #include <rtl/strutils.h>
#include <io/nfc.h>
#include "io/nfc.h"
#include "rfal_isoDep.h" #include "rfal_isoDep.h"
#include "rfal_nfc.h" #include "rfal_nfc.h"
#include "rfal_nfca.h" #include "rfal_nfca.h"
@ -37,16 +36,20 @@
#include "nfc_internal.h" #include "nfc_internal.h"
#include "rfal_platform.h" #include "rfal_platform.h"
#define LM_SEL_RES \ // NFC-A SEL_RES configured for Type 4A Tag Platform
0x20U /*!<NFC-A SEL_RES configured for Type 4A Tag Platform */ #define LM_SEL_RES 0x20U
#define LM_NFCID2_BYTE1 \
0x02U /*!<NFC-F SENSF_RES configured for Type 3 Tag Platform */ // NFC-F SENSF_RES configured for Type 3 Tag Platform
#define LM_SC_BYTE1 \ #define LM_NFCID2_BYTE1 0x02U
0x12U /*!<NFC-F System Code byte 1 */
#define LM_SC_BYTE2 \ // NFC-F System Code byte 1
0xFCU /*!<NFC-F System Code byte 2 */ #define LM_SC_BYTE1 0x12U
#define LM_PAD0 \
0x00U /*!<NFC-F PAD0 */ // NFC-F System Code byte 2
#define LM_SC_BYTE2 0xFCU
// NFC-F PAD0
#define LM_PAD0 0x00U
typedef struct { typedef struct {
bool initialized; bool initialized;
@ -79,39 +82,38 @@ typedef struct {
}; };
} nfc_device_header_t2t_t; } nfc_device_header_t2t_t;
/* P2P communication data */ // P2P communication data
const uint8_t NFCID3[] = {0x01, 0xFE, 0x03, 0x04, 0x05, static const uint8_t NFCID3[] = {0x01, 0xFE, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x0A}; 0x06, 0x07, 0x08, 0x09, 0x0A};
const uint8_t GB[] = {0x46, 0x66, 0x6d, 0x01, 0x01, 0x11, 0x02, static const uint8_t GB[] = {0x46, 0x66, 0x6d, 0x01, 0x01, 0x11, 0x02,
0x02, 0x07, 0x80, 0x03, 0x02, 0x00, 0x03, 0x02, 0x07, 0x80, 0x03, 0x02, 0x00, 0x03,
0x04, 0x01, 0x32, 0x07, 0x01, 0x03}; 0x04, 0x01, 0x32, 0x07, 0x01, 0x03};
/* NFC-A CE config */ // NFC-A CE config
/* 4-byte UIDs with first byte 0x08 would need random number for the subsequent // 4-byte UIDs with first byte 0x08 would need random number for the subsequent
* 3 bytes. 4-byte UIDs with first byte 0x*F are Fixed number, not unique, use // 3 bytes. 4-byte UIDs with first byte 0x*F are Fixed number, not unique, use
* for this demo 7-byte UIDs need a manufacturer ID and need to assure // for this demo 7-byte UIDs need a manufacturer ID and need to assure
* uniqueness of the rest.*/ // uniqueness of the rest.
const uint8_t ceNFCA_NFCID[] = { static const uint8_t ceNFCA_NFCID[] = {
0x1, 0x2, 0x3, 0x4}; /* =_STM, 5F 53 54 4D NFCID1 / UID (4 bytes) */ 0x1, 0x2, 0x3, 0x4}; // =_STM, 5F 53 54 4D NFCID1 / UID (4 bytes)
const uint8_t ceNFCA_SENS_RES[] = {0x02, static const uint8_t ceNFCA_SENS_RES[] = {0x02,
0x00}; /* SENS_RES / ATQA for 4-byte UID */ 0x00}; // SENS_RES / ATQA for 4-byte UID
const uint8_t ceNFCA_SEL_RES = LM_SEL_RES; /* SEL_RES / SAK */ static const uint8_t ceNFCA_SEL_RES = LM_SEL_RES; // SEL_RES / SAK
const uint8_t ceNFCF_nfcid2[] = { static const uint8_t ceNFCF_nfcid2[] = {
LM_NFCID2_BYTE1, 0xFE, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; LM_NFCID2_BYTE1, 0xFE, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
/* NFC-F CE config */ // NFC-F CE config
const uint8_t ceNFCF_SC[] = {LM_SC_BYTE1, LM_SC_BYTE2}; static const uint8_t ceNFCF_SC[] = {LM_SC_BYTE1, LM_SC_BYTE2};
uint8_t ceNFCF_SENSF_RES[] = { static uint8_t ceNFCF_SENSF_RES[] = {
0x01, /* SENSF_RES */ 0x01, // SENSF_RES
0x02, 0xFE, 0x11, 0x22, 0x33, 0x02, 0xFE, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, /* NFCID2 */ 0x44, 0x55, 0x66, // NFCID2
LM_PAD0, LM_PAD0, 0x00, 0x00, 0x00, LM_PAD0, LM_PAD0, 0x00, 0x00, 0x00,
0x7F, 0x7F, 0x00, /* PAD0, PAD1, MRTIcheck, MRTIupdate, PAD2 0x7F, 0x7F, 0x00, // PAD0, PAD1, MRTIcheck, MRTIupdate, PAD2
*/ 0x00, 0x00}; // RD
0x00, 0x00}; /* RD */
static ReturnCode nfc_transcieve_blocking(uint8_t *txBuf, uint16_t txBufSize, static nfc_status_t nfc_transcieve_blocking(uint8_t *txBuf, uint16_t txBufSize,
uint8_t **rxBuf, uint16_t **rcvLen, uint8_t **rxBuf, uint16_t **rcvLen,
uint32_t fwt); uint32_t fwt);
@ -166,7 +168,7 @@ nfc_status_t nfc_init() {
GPIO_InitStructure_int.Pin = NFC_INT_PIN; GPIO_InitStructure_int.Pin = NFC_INT_PIN;
HAL_GPIO_Init(NFC_INT_PORT, &GPIO_InitStructure_int); HAL_GPIO_Init(NFC_INT_PORT, &GPIO_InitStructure_int);
memset(&(drv->hspi), 0, sizeof(drv->hspi)); memset(&drv->hspi, 0, sizeof(drv->hspi));
drv->hspi.Instance = NFC_SPI_INSTANCE; drv->hspi.Instance = NFC_SPI_INSTANCE;
drv->hspi.Init.Mode = SPI_MODE_MASTER; drv->hspi.Init.Mode = SPI_MODE_MASTER;
@ -192,7 +194,7 @@ nfc_status_t nfc_init() {
HAL_StatusTypeDef status; HAL_StatusTypeDef status;
status = HAL_SPI_Init(&(drv->hspi)); status = HAL_SPI_Init(&drv->hspi);
if (status != HAL_OK) { if (status != HAL_OK) {
return false; return false;
@ -235,7 +237,7 @@ nfc_status_t nfc_deinit(void) {
return NFC_ERROR; return NFC_ERROR;
} }
HAL_SPI_DeInit(&(drv->hspi)); HAL_SPI_DeInit(&drv->hspi);
HAL_GPIO_DeInit(NFC_SPI_MISO_PORT, NFC_SPI_MISO_PIN); HAL_GPIO_DeInit(NFC_SPI_MISO_PORT, NFC_SPI_MISO_PIN);
HAL_GPIO_DeInit(NFC_SPI_MOSI_PORT, NFC_SPI_MOSI_PIN); HAL_GPIO_DeInit(NFC_SPI_MOSI_PORT, NFC_SPI_MOSI_PIN);
@ -249,7 +251,6 @@ nfc_status_t nfc_deinit(void) {
} }
nfc_status_t nfc_register_tech(const nfc_tech_t tech) { nfc_status_t nfc_register_tech(const nfc_tech_t tech) {
st25r3916b_driver_t *drv = &g_st25r3916b_driver; st25r3916b_driver_t *drv = &g_st25r3916b_driver;
@ -269,7 +270,6 @@ nfc_status_t nfc_register_tech(const nfc_tech_t tech) {
} }
// Set general discovery parameters. // Set general discovery parameters.
if (tech & NFC_POLLER_TECH_A) { if (tech & NFC_POLLER_TECH_A) {
drv->disc_params.techs2Find |= RFAL_NFC_POLL_TECH_A; drv->disc_params.techs2Find |= RFAL_NFC_POLL_TECH_A;
} }
@ -287,26 +287,42 @@ nfc_status_t nfc_register_tech(const nfc_tech_t tech) {
} }
if (tech & NFC_CARD_EMU_TECH_A) { if (tech & NFC_CARD_EMU_TECH_A) {
card_emulation_init(ceNFCF_nfcid2); card_emulation_init(ceNFCF_nfcid2);
// Set SENS_RES / ATQA
memcpy(drv->disc_params.lmConfigPA.SENS_RES, ceNFCA_SENS_RES, memcpy(drv->disc_params.lmConfigPA.SENS_RES, ceNFCA_SENS_RES,
RFAL_LM_SENS_RES_LEN); /* Set SENS_RES / ATQA */ RFAL_LM_SENS_RES_LEN);
// Set NFCID / UID
memcpy(drv->disc_params.lmConfigPA.nfcid, ceNFCA_NFCID, memcpy(drv->disc_params.lmConfigPA.nfcid, ceNFCA_NFCID,
RFAL_LM_NFCID_LEN_04); /* Set NFCID / UID */ RFAL_LM_NFCID_LEN_04);
// Set NFCID length to 4 bytes
drv->disc_params.lmConfigPA.nfcidLen = drv->disc_params.lmConfigPA.nfcidLen =
RFAL_LM_NFCID_LEN_04; /* Set NFCID length to 4 bytes */ RFAL_LM_NFCID_LEN_04;
// Set SEL_RES / SAK
drv->disc_params.lmConfigPA.SEL_RES = drv->disc_params.lmConfigPA.SEL_RES =
ceNFCA_SEL_RES; /* Set SEL_RES / SAK */ ceNFCA_SEL_RES;
drv->disc_params.techs2Find |= RFAL_NFC_LISTEN_TECH_A; drv->disc_params.techs2Find |= RFAL_NFC_LISTEN_TECH_A;
} }
if (tech & NFC_CARD_EMU_TECH_F) { if (tech & NFC_CARD_EMU_TECH_F) {
/* Set configuration for NFC-F CE */
// Set configuration for NFC-F CE
memcpy(drv->disc_params.lmConfigPF.SC, ceNFCF_SC, memcpy(drv->disc_params.lmConfigPF.SC, ceNFCF_SC,
RFAL_LM_SENSF_SC_LEN); /* Set System Code */ RFAL_LM_SENSF_SC_LEN); // Set System Code
// Load NFCID2 on SENSF_RES
memcpy(&ceNFCF_SENSF_RES[RFAL_NFCF_CMD_LEN], ceNFCF_nfcid2, memcpy(&ceNFCF_SENSF_RES[RFAL_NFCF_CMD_LEN], ceNFCF_nfcid2,
RFAL_NFCID2_LEN); /* Load NFCID2 on SENSF_RES */ RFAL_NFCID2_LEN);
// Set SENSF_RES / Poll Response
memcpy(drv->disc_params.lmConfigPF.SENSF_RES, ceNFCF_SENSF_RES, memcpy(drv->disc_params.lmConfigPF.SENSF_RES, ceNFCF_SENSF_RES,
RFAL_LM_SENSF_RES_LEN); /* Set SENSF_RES / Poll Response */ RFAL_LM_SENSF_RES_LEN);
drv->disc_params.techs2Find |= RFAL_NFC_LISTEN_TECH_F; drv->disc_params.techs2Find |= RFAL_NFC_LISTEN_TECH_F;
} }
@ -321,7 +337,7 @@ nfc_status_t nfc_activate_stm(void) {
} }
ReturnCode err; ReturnCode err;
err = rfalNfcDiscover(&(drv->disc_params)); err = rfalNfcDiscover(&drv->disc_params);
if (err != RFAL_ERR_NONE) { if (err != RFAL_ERR_NONE) {
return NFC_ERROR; return NFC_ERROR;
} }
@ -349,20 +365,25 @@ nfc_status_t nfc_deactivate_stm(void) {
return NFC_OK; return NFC_OK;
} }
nfc_event_t nfc_get_event(void) { nfc_status_t nfc_get_event(nfc_event_t *event) {
st25r3916b_driver_t *drv = &g_st25r3916b_driver; st25r3916b_driver_t *drv = &g_st25r3916b_driver;
*event = NFC_NO_EVENT;
if(!drv->initialized) { if(!drv->initialized) {
return NFC_NOT_INITIALIZED; return NFC_NOT_INITIALIZED;
} }
static rfalNfcDevice *nfcDevice; static rfalNfcDevice *nfcDevice;
rfalNfcWorker(); /* Run RFAL worker periodically */ // Run RFAL worker periodically
rfalNfcWorker();
if(rfalNfcIsDevActivated(rfalNfcGetState())) { if(rfalNfcIsDevActivated(rfalNfcGetState())) {
*event = NFC_EVENT_ACTIVATED;
rfalNfcGetActiveDevice(&nfcDevice); rfalNfcGetActiveDevice(&nfcDevice);
// Perform immediate mandatory actions for certain technology (Placeholder) // Perform immediate mandatory actions for certain technology (Placeholder)
@ -413,19 +434,18 @@ nfc_event_t nfc_get_event(void) {
rfalNfcDeactivate(RFAL_NFC_DEACTIVATE_DISCOVERY); // Automatically deactivate rfalNfcDeactivate(RFAL_NFC_DEACTIVATE_DISCOVERY); // Automatically deactivate
} }
return NFC_NO_EVENT; *event = NFC_NO_EVENT;
break; break;
default: default:
break; break;
}
return NFC_STATE_ACTIVATED; }
} }
return NFC_NO_EVENT; return NFC_OK;
} }
nfc_status_t nfc_dev_deactivate(void) { nfc_status_t nfc_dev_deactivate(void) {
@ -485,6 +505,7 @@ nfc_status_t nfc_dev_write_ndef_uri(void) {
} }
nfc_status_t nfc_dev_read_info(nfc_dev_info_t *dev_info) { nfc_status_t nfc_dev_read_info(nfc_dev_info_t *dev_info) {
if (rfalNfcIsDevActivated(rfalNfcGetState())) { if (rfalNfcIsDevActivated(rfalNfcGetState())) {
rfalNfcDevice *nfcDevice; rfalNfcDevice *nfcDevice;
rfalNfcGetActiveDevice(&nfcDevice); rfalNfcGetActiveDevice(&nfcDevice);
@ -542,21 +563,17 @@ HAL_StatusTypeDef nfc_spi_transmit_receive(const uint8_t *txData,
HAL_StatusTypeDef status; HAL_StatusTypeDef status;
if ((txData != NULL) && (rxData == NULL)) { if ((txData != NULL) && (rxData == NULL)) {
status = HAL_SPI_Transmit(&(drv->hspi), (uint8_t *)txData, length, 1000); status = HAL_SPI_Transmit(&drv->hspi, (uint8_t *)txData, length, 1000);
} else if ((txData == NULL) && (rxData != NULL)) { } else if ((txData == NULL) && (rxData != NULL)) {
status = HAL_SPI_Receive(&(drv->hspi), rxData, length, 1000); status = HAL_SPI_Receive(&drv->hspi, rxData, length, 1000);
} else { } else {
status = HAL_SPI_TransmitReceive(&(drv->hspi), (uint8_t *)txData, rxData, status = HAL_SPI_TransmitReceive(&drv->hspi, (uint8_t *)txData, rxData,
length, 1000); length, 1000);
} }
return status; return status;
} }
uint32_t nfc_create_timer(uint16_t time) { return ticks_timeout(time); }
bool nfc_timer_is_expired(uint32_t timer) { return ticks_expired(timer); }
void nfc_ext_irq_set_callback(void (*cb)(void)) { void nfc_ext_irq_set_callback(void (*cb)(void)) {
st25r3916b_driver_t *drv = &g_st25r3916b_driver; st25r3916b_driver_t *drv = &g_st25r3916b_driver;
drv->nfc_irq_callback = cb; drv->nfc_irq_callback = cb;
@ -612,11 +629,10 @@ static void nfc_card_emulator_loop(rfalNfcDevice *nfcDev) {
} while ((err == RFAL_ERR_NONE) || (err == RFAL_ERR_SLEEP_REQ)); } while ((err == RFAL_ERR_NONE) || (err == RFAL_ERR_SLEEP_REQ));
} }
static ReturnCode nfc_transcieve_blocking(uint8_t *txBuf, uint16_t txBufSize, static nfc_status_t nfc_transcieve_blocking(uint8_t *txBuf, uint16_t txBufSize,
uint8_t **rxBuf, uint16_t **rcvLen, uint8_t **rxBuf, uint16_t **rcvLen,
uint32_t fwt) { uint32_t fwt) {
ReturnCode err; ReturnCode err;
err = rfalNfcDataExchangeStart(txBuf, txBufSize, rxBuf, rcvLen, fwt); err = rfalNfcDataExchangeStart(txBuf, txBufSize, rxBuf, rcvLen, fwt);
if (err == RFAL_ERR_NONE) { if (err == RFAL_ERR_NONE) {
do { do {
@ -624,5 +640,11 @@ static ReturnCode nfc_transcieve_blocking(uint8_t *txBuf, uint16_t txBufSize,
err = rfalNfcDataExchangeGetStatus(); err = rfalNfcDataExchangeGetStatus();
} while (err == RFAL_ERR_BUSY); } while (err == RFAL_ERR_BUSY);
} }
return err;
if (err != RFAL_ERR_NONE) {
return NFC_ERROR;
}else {
return NFC_OK;
}
} }

View File

@ -1,14 +1,28 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once #pragma once
#include <trezor_bsp.h> #include <trezor_types.h>
HAL_StatusTypeDef nfc_spi_transmit_receive(const uint8_t *txData, HAL_StatusTypeDef nfc_spi_transmit_receive(const uint8_t *txData,
uint8_t *rxData, uint16_t length); uint8_t *rxData, uint16_t length);
uint32_t nfc_create_timer(uint16_t time);
bool nfc_timer_is_expired(uint32_t timer);
void nfc_ext_irq_set_callback(void (*cb)(void)); void nfc_ext_irq_set_callback(void (*cb)(void));

View File

@ -1,23 +1,20 @@
/****************************************************************************** /*
* @attention * This file is part of the Trezor project, https://trezor.io/
* *
* COPYRIGHT 2018 STMicroelectronics, all rights reserved * Copyright (c) SatoshiLabs
* *
* Unless required by applicable law or agreed to in writing, software * This program is free software: you can redistribute it and/or modify
* distributed under the License is distributed on an "AS IS" BASIS, * it under the terms of the GNU General Public License as published by
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, * the Free Software Foundation, either version 3 of the License, or
* AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY, * (at your option) any later version.
* FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
* See the License for the specific language governing permissions and
* limitations under the License.
* *
******************************************************************************/ * This program is distributed in the hope that it will be useful,
/*! \file * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* \author * GNU General Public License for more details.
*
* \brief Platform header file. Defining platform independent functionality.
* *
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
@ -26,28 +23,18 @@
extern "C" { extern "C" {
#endif #endif
/*ReturnCode
******************************************************************************
* INCLUDES
******************************************************************************
*/
#include <trezor_bsp.h> #include <trezor_bsp.h>
#include <trezor_types.h> #include <trezor_types.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <sys/systimer.h> #include <sys/systick.h>
#include "io/nfc.h" #include "io/nfc.h"
#include "nfc_internal.h" #include "nfc_internal.h"
/*
******************************************************************************
* GLOBAL DEFINES
******************************************************************************
*/
// Device type definition // Device type definition
#define ST25R3916B #define ST25R3916B
@ -63,11 +50,6 @@ extern "C" {
// GPIO port used for ST25R External Interrupt // GPIO port used for ST25R External Interrupt
#define ST25R_INT_PORT NFC_INT_PORT #define ST25R_INT_PORT NFC_INT_PORT
/*
******************************************************************************
* GLOBAL MACROS
******************************************************************************
*/
#define platformProtectST25RComm() \ #define platformProtectST25RComm() \
NVIC_DisableIRQ(NFC_EXTI_INTERRUPT_NUM) NVIC_DisableIRQ(NFC_EXTI_INTERRUPT_NUM)
@ -101,10 +83,10 @@ extern "C" {
#define platformGpioIsLow(port, pin) (!platformGpioIsHigh(port, pin)) #define platformGpioIsLow(port, pin) (!platformGpioIsHigh(port, pin))
// Create a timer with the given time (ms) // Create a timer with the given time (ms)
#define platformTimerCreate(t) nfc_create_timer(t) #define platformTimerCreate(t) ticks_timeout(t)
// Checks if the given timer is expired // Checks if the given timer is expired
#define platformTimerIsExpired(timer) nfc_timer_is_expired(timer) #define platformTimerIsExpired(timer) ticks_expired(timer)
// Performs a delay for the given time (ms) // Performs a delay for the given time (ms)
#define platformDelay(t) HAL_Delay(t) #define platformDelay(t) HAL_Delay(t)
@ -135,19 +117,9 @@ extern "C" {
// Log method // Log method
#define platformLog(...) // logUsart(__VA_ARGS__) #define platformLog(...) // logUsart(__VA_ARGS__)
/*
******************************************************************************
* GLOBAL VARIABLES
******************************************************************************
*/
extern uint8_t globalCommProtectCnt; /* Global Protection Counter provided per extern uint8_t globalCommProtectCnt; /* Global Protection Counter provided per
platform - instantiated in main.c */ platform - instantiated in main.c */
/*
******************************************************************************
* RFAL FEATURES CONFIGURATION
******************************************************************************
*/
#define RFAL_FEATURE_LISTEN_MODE \ #define RFAL_FEATURE_LISTEN_MODE \
true /*!< Enable/Disable RFAL support for Listen Mode */ true /*!< Enable/Disable RFAL support for Listen Mode */

View File

@ -43,8 +43,10 @@ static void prodtest_nfc_read_card(cli_t* cli) {
} }
nfc_status_t ret = nfc_init(); nfc_status_t ret = nfc_init();
if (ret != NFC_OK) { if (ret != NFC_OK) {
cli_error(cli, CLI_ERROR_FATAL, "NFC init failed"); cli_error(cli, CLI_ERROR_FATAL, "NFC init failed");
goto cleanup;
} else { } else {
cli_trace(cli, "NFC activated in reader mode for %d ms.", timeout); cli_trace(cli, "NFC activated in reader mode for %d ms.", timeout);
} }
@ -53,28 +55,32 @@ static void prodtest_nfc_read_card(cli_t* cli) {
NFC_POLLER_TECH_V); NFC_POLLER_TECH_V);
nfc_activate_stm(); nfc_activate_stm();
nfc_event_t nfc_event = NFC_NO_EVENT; nfc_event_t nfc_event;
uint32_t expire_time = ticks_timeout(timeout); uint32_t expire_time = ticks_timeout(timeout);
while (true) { while (true) {
if (ticks_expired(expire_time)) { if (ticks_expired(expire_time)) {
nfc_deinit();
cli_error(cli, CLI_ERROR_TIMEOUT, "NFC timeout"); cli_error(cli, CLI_ERROR_TIMEOUT, "NFC timeout");
return; goto cleanup;
} }
nfc_event = nfc_get_event(); nfc_status_t nfc_status = nfc_get_event(&nfc_event);
if(nfc_event == NFC_STATE_ACTIVATED) { if(nfc_status != NFC_OK) {
cli_error(cli, CLI_ERROR, "NFC error");
goto cleanup;
}
if(nfc_event == NFC_EVENT_ACTIVATED) {
nfc_dev_read_info(&dev_info); nfc_dev_read_info(&dev_info);
nfc_dev_deactivate(); nfc_dev_deactivate();
break; break;
} }
if (cli_aborted(cli)) { if (cli_aborted(cli)) {
return; goto cleanup;
} }
} }
@ -102,16 +108,20 @@ static void prodtest_nfc_read_card(cli_t* cli) {
break; break;
case NFC_DEV_TYPE_UNKNOWN: case NFC_DEV_TYPE_UNKNOWN:
cli_error(cli, CLI_ERROR, "NFC Type UNKNOWN"); cli_error(cli, CLI_ERROR, "NFC Type UNKNOWN");
break; goto cleanup;
return;
default: default:
cli_error(cli, CLI_ERROR, "NFC Type UNKNOWN"); cli_error(cli, CLI_ERROR, "NFC Type UNKNOWN");
break; goto cleanup;
} }
nfc_deinit();
cli_ok(cli, ""); cli_ok(cli, "");
cleanup:
nfc_deinit();
} }
static void prodtest_nfc_emulate_card(cli_t* cli) { static void prodtest_nfc_emulate_card(cli_t* cli) {
@ -132,6 +142,7 @@ static void prodtest_nfc_emulate_card(cli_t* cli) {
if (ret != NFC_OK) { if (ret != NFC_OK) {
cli_error(cli, CLI_ERROR_FATAL, "NFC init failed"); cli_error(cli, CLI_ERROR_FATAL, "NFC init failed");
goto cleanup;
} else { } else {
cli_trace(cli, "Emulation started for %d ms", timeout); cli_trace(cli, "Emulation started for %d ms", timeout);
} }
@ -140,23 +151,33 @@ static void prodtest_nfc_emulate_card(cli_t* cli) {
nfc_activate_stm(); nfc_activate_stm();
uint32_t expire_time = ticks_timeout(timeout); uint32_t expire_time = ticks_timeout(timeout);
nfc_event_t nfc_event;
while (!ticks_expired(expire_time)) { while (!ticks_expired(expire_time)) {
nfc_get_event();
nfc_status_t nfc_status = nfc_get_event(&nfc_event);
if(nfc_status != NFC_OK) {
cli_error(cli, CLI_ERROR, "NFC error");
goto cleanup;
}
if (cli_aborted(cli)) { if (cli_aborted(cli)) {
return; goto cleanup;
} }
} }
cli_trace(cli, "Emulation over"); cli_trace(cli, "Emulation over");
cli_ok(cli, "");
cleanup:
nfc_deinit(); nfc_deinit();
cli_ok(cli, "");
} }
static void prodtest_nfc_write_card(cli_t* cli) { static void prodtest_nfc_write_card(cli_t* cli) {
uint32_t timeout = 0; uint32_t timeout = 0;
memset(&dev_info, 0, sizeof(dev_info)); memset(&dev_info, 0, sizeof(dev_info));
@ -176,6 +197,7 @@ static void prodtest_nfc_write_card(cli_t* cli) {
if (ret != NFC_OK) { if (ret != NFC_OK) {
cli_error(cli, CLI_ERROR_FATAL, "NFC init failed"); cli_error(cli, CLI_ERROR_FATAL, "NFC init failed");
goto cleanup;
} else { } else {
cli_trace(cli, "NFC reader on, put the card on the reader (timeout %d ms)", cli_trace(cli, "NFC reader on, put the card on the reader (timeout %d ms)",
timeout); timeout);
@ -184,25 +206,30 @@ static void prodtest_nfc_write_card(cli_t* cli) {
nfc_register_tech(NFC_POLLER_TECH_A); nfc_register_tech(NFC_POLLER_TECH_A);
nfc_activate_stm(); nfc_activate_stm();
nfc_event_t nfc_event = NFC_NO_EVENT; nfc_event_t nfc_event;
uint32_t expire_time = ticks_timeout(timeout); uint32_t expire_time = ticks_timeout(timeout);
while (true) { while (true) {
if (ticks_expired(expire_time)) { if (ticks_expired(expire_time)) {
nfc_deinit();
cli_error(cli, CLI_ERROR_TIMEOUT, "NFC timeout"); cli_error(cli, CLI_ERROR_TIMEOUT, "NFC timeout");
return; goto cleanup;
} }
nfc_event = nfc_get_event(); nfc_status_t nfc_status = nfc_get_event(&nfc_event);
if (nfc_event == NFC_STATE_ACTIVATED) {
if(nfc_status != NFC_OK) {
cli_error(cli, CLI_ERROR, "NFC error");
goto cleanup;
}
if (nfc_event == NFC_EVENT_ACTIVATED) {
nfc_dev_read_info(&dev_info); nfc_dev_read_info(&dev_info);
if (dev_info.type != NFC_DEV_TYPE_A) { if (dev_info.type != NFC_DEV_TYPE_A) {
cli_error(cli, CLI_ERROR, "Only NFC type A cards supported"); cli_error(cli, CLI_ERROR, "Only NFC type A cards supported");
return; goto cleanup;
} }
cli_trace(cli, "Writting URI to NFC tag %s", dev_info.uid); cli_trace(cli, "Writting URI to NFC tag %s", dev_info.uid);
@ -214,14 +241,15 @@ static void prodtest_nfc_write_card(cli_t* cli) {
} }
if (cli_aborted(cli)) { if (cli_aborted(cli)) {
return; goto cleanup;
} }
} }
nfc_deinit();
cli_ok(cli, ""); cli_ok(cli, "");
cleanup:
nfc_deinit();
} }
// clang-format off // clang-format off