added a custom timeout parameter to the smart raw command

This commit is contained in:
iceman1001 2023-09-16 18:38:53 +02:00
commit b6e1344e1c
5 changed files with 44 additions and 16 deletions

View file

@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
## [unreleased][unreleased] ## [unreleased][unreleased]
- Changed `smart raw --timeout` - allows for a custom timeout (@iceman1001)
- Changed `lf t55 detectp1` - now also accepts 0xE039 Silicon Craft Tech as valid card (@iceman1001) - Changed `lf t55 detectp1` - now also accepts 0xE039 Silicon Craft Tech as valid card (@iceman1001)
- Fixed `utils.lua` library function "convertdectohex" wasn't working (@iceman1001) - Fixed `utils.lua` library function "convertdectohex" wasn't working (@iceman1001)
- Added `hf iclass creditepurse` command to allow crediting the epurse debit value (@nvx) - Added `hf iclass creditepurse` command to allow crediting the epurse debit value (@nvx)

View file

@ -39,6 +39,11 @@
#define I2C_ERROR "I2C_WaitAck Error" #define I2C_ERROR "I2C_WaitAck Error"
// 8051 speaks with smart card.
// 1000*50*3.07 = 153.5ms
// 1 byte transfer == 1ms with max frame being 256 bytes
#define SIM_WAIT_DELAY 109773 // about 337.7ms delay
// Direct use the loop to delay. 6 instructions loop, Masterclock 48MHz, // Direct use the loop to delay. 6 instructions loop, Masterclock 48MHz,
// delay=1 is about 200kbps // delay=1 is about 200kbps
// timer. // timer.
@ -168,8 +173,8 @@ static bool WaitSCL_H_delay(uint32_t delay) {
return false; return false;
} }
// 5000 * 3.07us = 15350us. 15.35ms // 5000 * 3.07us = 15350 us = 15.35 ms
// 15000 * 3.07us = 46050us. 46.05ms // 15000 * 3.07us = 46050 us = 46.05 ms
static bool WaitSCL_H(void) { static bool WaitSCL_H(void) {
return WaitSCL_H_delay(5000); return WaitSCL_H_delay(5000);
} }
@ -194,7 +199,7 @@ static bool WaitSCL_L(void) {
// It timeout reading response from card // It timeout reading response from card
// Which ever comes first // Which ever comes first
static bool WaitSCL_L_timeout(void) { static bool WaitSCL_L_timeout(void) {
volatile uint32_t delay = 200; volatile uint32_t delay = 1800;
while (delay--) { while (delay--) {
// exit on SCL LOW // exit on SCL LOW
if (SCL_read == false) if (SCL_read == false)
@ -227,17 +232,22 @@ static bool I2C_Start(void) {
return true; return true;
} }
static bool I2C_WaitForSim(void) { static bool I2C_WaitForSim(uint32_t wait) {
// wait for data from card // wait for data from card
if (!WaitSCL_L_timeout()) if (WaitSCL_L_timeout() == false) {
return false; return false;
}
// 8051 speaks with smart card. // 8051 speaks with smart card.
// 1000*50*3.07 = 153.5ms // 1000*50*3.07 = 153.5ms
// 1000*110*3.07 = 337.7ms // 1000*110*3.07 = 337.7ms (337700)
// 4 560 000 * 3.07 = 13999,2ms (13999200)
// 1byte transfer == 1ms with max frame being 256bytes // 1byte transfer == 1ms with max frame being 256bytes
return WaitSCL_H_delay(1000 * 110);
// fct WaitSCL_H_delay uses a I2C_DELAY_1CLK in the loop with "wait" as number of iterations.
// I2C_DELAY_1CLK == I2CSpinDelayClk(1) = 3.07us
return WaitSCL_H_delay(wait);
} }
// send i2c STOP // send i2c STOP
@ -670,13 +680,13 @@ int I2C_get_version(uint8_t *maj, uint8_t *min) {
} }
// Will read response from smart card module, retries 3 times to get the data. // Will read response from smart card module, retries 3 times to get the data.
bool sc_rx_bytes(uint8_t *dest, uint16_t *destlen) { bool sc_rx_bytes(uint8_t *dest, uint16_t *destlen, uint32_t wait) {
uint8_t i = 5; uint8_t i = 5;
int16_t len = 0; int16_t len = 0;
while (i--) { while (i--) {
I2C_WaitForSim(); I2C_WaitForSim(wait);
len = I2C_BufferRead(dest, *destlen, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); len = I2C_BufferRead(dest, *destlen, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
@ -703,6 +713,7 @@ bool GetATR(smart_card_atr_t *card_ptr, bool verbose) {
if (card_ptr == NULL) if (card_ptr == NULL)
return false; return false;
card_ptr->atr_len = 0; card_ptr->atr_len = 0;
memset(card_ptr->atr, 0, sizeof(card_ptr->atr)); memset(card_ptr->atr, 0, sizeof(card_ptr->atr));
@ -712,13 +723,13 @@ bool GetATR(smart_card_atr_t *card_ptr, bool verbose) {
I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN); I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN);
//wait for sim card to answer. //wait for sim card to answer.
// 1byte = 1ms , max frame 256bytes. Should wait 256ms atleast just in case. // 1byte = 1ms , max frame 256bytes. Should wait 256ms atleast just in case.
if (I2C_WaitForSim() == false) if (I2C_WaitForSim(SIM_WAIT_DELAY) == false)
return false; return false;
// read bytes from module // read bytes from module
uint16_t len = sizeof(card_ptr->atr); uint16_t len = sizeof(card_ptr->atr);
if (sc_rx_bytes(card_ptr->atr, &len) == false) if (sc_rx_bytes(card_ptr->atr, &len, SIM_WAIT_DELAY) == false)
return false; return false;
if (len > sizeof(card_ptr->atr)) { if (len > sizeof(card_ptr->atr)) {
@ -805,6 +816,11 @@ void SmartCardRaw(const smart_card_raw_t *p) {
if ((flags & SC_RAW) || (flags & SC_RAW_T0)) { if ((flags & SC_RAW) || (flags & SC_RAW_T0)) {
uint32_t wait = SIM_WAIT_DELAY;
if ((flags & SC_WAIT) == SC_WAIT) {
wait = (uint32_t)((p->wait_delay * 1000) / 3.07);
}
LogTrace(p->data, p->len, 0, 0, NULL, true); LogTrace(p->data, p->len, 0, 0, NULL, true);
bool res = I2C_BufferWrite( bool res = I2C_BufferWrite(
@ -821,7 +837,7 @@ void SmartCardRaw(const smart_card_raw_t *p) {
// read bytes from module // read bytes from module
len = ISO7816_MAX_FRAME; len = ISO7816_MAX_FRAME;
res = sc_rx_bytes(resp, &len); res = sc_rx_bytes(resp, &len, wait);
if (res) { if (res) {
LogTrace(resp, len, 0, 0, NULL, false); LogTrace(resp, len, 0, 0, NULL, false);
} else { } else {

View file

@ -48,7 +48,7 @@ int16_t I2C_BufferRead(uint8_t *data, uint16_t len, uint8_t device_cmd, uint8_t
int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address); int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address);
bool I2C_WriteFW(const uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address); bool I2C_WriteFW(const uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address);
bool sc_rx_bytes(uint8_t *dest, uint16_t *destlen); bool sc_rx_bytes(uint8_t *dest, uint16_t *destlen, uint32_t wait);
// //
bool GetATR(smart_card_atr_t *card_ptr, bool verbose); bool GetATR(smart_card_atr_t *card_ptr, bool verbose);

View file

@ -416,6 +416,7 @@ static int CmdSmartRaw(const char *Cmd) {
arg_lit0("s", NULL, "active smartcard with select (get ATR)"), arg_lit0("s", NULL, "active smartcard with select (get ATR)"),
arg_lit0("t", "tlv", "executes TLV decoder if it possible"), arg_lit0("t", "tlv", "executes TLV decoder if it possible"),
arg_lit0("0", NULL, "use protocol T=0"), arg_lit0("0", NULL, "use protocol T=0"),
arg_int0(NULL, "timeout", "<ms>", "Timeout in MS waiting for SIM to respond. (def 337ms)"),
arg_str1("d", "data", "<hex>", "bytes to send"), arg_str1("d", "data", "<hex>", "bytes to send"),
arg_param_end arg_param_end
}; };
@ -426,10 +427,11 @@ static int CmdSmartRaw(const char *Cmd) {
bool active_select = arg_get_lit(ctx, 3); bool active_select = arg_get_lit(ctx, 3);
bool decode_tlv = arg_get_lit(ctx, 4); bool decode_tlv = arg_get_lit(ctx, 4);
bool use_t0 = arg_get_lit(ctx, 5); bool use_t0 = arg_get_lit(ctx, 5);
int timeout = arg_get_int_def(ctx, 6, -1);
int dlen = 0; int dlen = 0;
uint8_t data[PM3_CMD_DATA_SIZE] = {0x00}; uint8_t data[PM3_CMD_DATA_SIZE] = {0x00};
int res = CLIParamHexToBuf(arg_get_str(ctx, 6), data, sizeof(data), &dlen); int res = CLIParamHexToBuf(arg_get_str(ctx, 7), data, sizeof(data), &dlen);
CLIParserFree(ctx); CLIParserFree(ctx);
if (res) { if (res) {
@ -452,6 +454,13 @@ static int CmdSmartRaw(const char *Cmd) {
if (active_select) if (active_select)
payload->flags |= SC_SELECT; payload->flags |= SC_SELECT;
} }
payload->wait_delay = 0;
if (timeout > -1) {
payload->flags |= SC_WAIT;
payload->wait_delay = timeout;
}
PrintAndLogEx(DEBUG, "SIM Card timeout... %u ms", payload->wait_delay);
if (dlen > 0) { if (dlen > 0) {
if (use_t0) if (use_t0)

View file

@ -338,10 +338,12 @@ typedef enum SMARTCARD_COMMAND {
SC_RAW_T0 = (1 << 4), SC_RAW_T0 = (1 << 4),
SC_CLEARLOG = (1 << 5), SC_CLEARLOG = (1 << 5),
SC_LOG = (1 << 6), SC_LOG = (1 << 6),
SC_WAIT = (1 << 7),
} smartcard_command_t; } smartcard_command_t;
typedef struct { typedef struct {
uint8_t flags; uint8_t flags;
uint32_t wait_delay;
uint16_t len; uint16_t len;
uint8_t data[]; uint8_t data[];
} PACKED smart_card_raw_t; } PACKED smart_card_raw_t;