mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-20 13:23:51 -07:00
Adapt ARM code to comply with SIM module firmware 4.12
This commit is contained in:
parent
89702a4e6e
commit
83e6b223c4
3 changed files with 49 additions and 15 deletions
51
armsrc/i2c.c
51
armsrc/i2c.c
|
@ -53,7 +53,8 @@ static void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
|
||||||
#define I2C_DELAY_2CLK I2CSpinDelayClk(2)
|
#define I2C_DELAY_2CLK I2CSpinDelayClk(2)
|
||||||
#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
|
#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
|
||||||
|
|
||||||
#define ISO7618_MAX_FRAME 260
|
// The SIM module v4 supports up to 384 bytes for the length.
|
||||||
|
#define ISO7816_MAX_FRAME 260
|
||||||
|
|
||||||
// try i2c bus recovery at 100kHz = 5us high, 5us low
|
// try i2c bus recovery at 100kHz = 5us high, 5us low
|
||||||
void I2C_recovery(void) {
|
void I2C_recovery(void) {
|
||||||
|
@ -445,6 +446,7 @@ int16_t I2C_BufferRead(uint8_t *data, uint16_t len, uint8_t device_cmd, uint8_t
|
||||||
|
|
||||||
bool bBreak = true;
|
bool bBreak = true;
|
||||||
uint16_t readcount = 0;
|
uint16_t readcount = 0;
|
||||||
|
uint16_t recv_len = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (!I2C_Start())
|
if (!I2C_Start())
|
||||||
|
@ -484,11 +486,34 @@ int16_t I2C_BufferRead(uint8_t *data, uint16_t len, uint8_t device_cmd, uint8_t
|
||||||
|
|
||||||
len--;
|
len--;
|
||||||
|
|
||||||
// The first byte in response is the message length
|
// Starting firmware v4 the length is encoded on the first two bytes.
|
||||||
if (!readcount && (len > *data)) {
|
// This only applies if command is I2C_DEVICE_CMD_READ.
|
||||||
len = *data;
|
if (device_cmd == I2C_DEVICE_CMD_READ) {
|
||||||
|
switch (readcount) {
|
||||||
|
case 0:
|
||||||
|
// Length (MSB)
|
||||||
|
recv_len = (*data) << 8;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// Length (LSB)
|
||||||
|
recv_len += *data;
|
||||||
|
// Adjust len if needed
|
||||||
|
if (len > recv_len) {
|
||||||
|
len = recv_len;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Data byte received
|
||||||
|
data++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
data++;
|
// Length is encoded on 1 byte
|
||||||
|
if ((readcount == 0) && (len > *data)) {
|
||||||
|
len = *data;
|
||||||
|
} else {
|
||||||
|
data++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
readcount++;
|
readcount++;
|
||||||
|
|
||||||
|
@ -501,8 +526,8 @@ int16_t I2C_BufferRead(uint8_t *data, uint16_t len, uint8_t device_cmd, uint8_t
|
||||||
|
|
||||||
I2C_Stop();
|
I2C_Stop();
|
||||||
|
|
||||||
// return bytecount - first byte (which is length byte)
|
// return bytecount - bytes encoding length
|
||||||
return --readcount;
|
return readcount - (device_cmd == I2C_DEVICE_CMD_READ ? 2 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
@ -612,10 +637,14 @@ bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t d
|
||||||
void I2C_print_status(void) {
|
void I2C_print_status(void) {
|
||||||
DbpString(_CYAN_("Smart card module (ISO 7816)"));
|
DbpString(_CYAN_("Smart card module (ISO 7816)"));
|
||||||
uint8_t maj, min;
|
uint8_t maj, min;
|
||||||
if (I2C_get_version(&maj, &min) == PM3_SUCCESS)
|
if (I2C_get_version(&maj, &min) == PM3_SUCCESS) {
|
||||||
Dbprintf(" version................. " _YELLOW_("v%x.%02d"), maj, min);
|
Dbprintf(" version................. " _YELLOW_("v%x.%02d"), maj, min);
|
||||||
else
|
if (maj < 4) {
|
||||||
|
DbpString(" " _RED_("Outdated firmware.") " Please upgrade to v4.x or above.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
DbpString(" version................. " _RED_("FAILED"));
|
DbpString(" version................. " _RED_("FAILED"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int I2C_get_version(uint8_t *maj, uint8_t *min) {
|
int I2C_get_version(uint8_t *maj, uint8_t *min) {
|
||||||
|
@ -736,7 +765,7 @@ void SmartCardRaw(smart_card_raw_t *p) {
|
||||||
LED_D_ON();
|
LED_D_ON();
|
||||||
|
|
||||||
uint16_t len = 0;
|
uint16_t len = 0;
|
||||||
uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME);
|
uint8_t *resp = BigBuf_malloc(ISO7816_MAX_FRAME);
|
||||||
// check if alloacted...
|
// check if alloacted...
|
||||||
smartcard_command_t flags = p->flags;
|
smartcard_command_t flags = p->flags;
|
||||||
|
|
||||||
|
@ -780,7 +809,7 @@ void SmartCardRaw(smart_card_raw_t *p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// read bytes from module
|
// read bytes from module
|
||||||
len = ISO7618_MAX_FRAME;
|
len = ISO7816_MAX_FRAME;
|
||||||
res = sc_rx_bytes(resp, &len);
|
res = sc_rx_bytes(resp, &len);
|
||||||
if (res) {
|
if (res) {
|
||||||
LogTrace(resp, len, 0, 0, NULL, false);
|
LogTrace(resp, len, 0, 0, NULL, false);
|
||||||
|
|
|
@ -329,10 +329,15 @@ static int smart_responseEx(uint8_t *out, int maxoutlen, bool verbose) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needGetData == true) {
|
if (needGetData == true) {
|
||||||
// Don't discard data we already received except the SW code
|
// Don't discard data we already received except the SW code.
|
||||||
|
// If we only received 1 byte, this is the echo of INS, we discard it.
|
||||||
totallen -= 2;
|
totallen -= 2;
|
||||||
|
if (totallen == 1) {
|
||||||
|
totallen = 0;
|
||||||
|
}
|
||||||
int ofs = totallen;
|
int ofs = totallen;
|
||||||
maxoutlen -= totallen;
|
maxoutlen -= totallen;
|
||||||
|
PrintAndLogEx(INFO, "Keeping data (%d bytes): %s", ofs, sprint_hex(out, ofs));
|
||||||
|
|
||||||
int len = out[datalen - 1];
|
int len = out[datalen - 1];
|
||||||
if (len == 0 || len > MAX_APDU_SIZE) {
|
if (len == 0 || len > MAX_APDU_SIZE) {
|
||||||
|
|
|
@ -22,10 +22,10 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "pm3_cmd.h" // structs
|
#include "pm3_cmd.h" // structs
|
||||||
|
|
||||||
// On ARM side, ISO7816_MAX_FRAME is set to 255
|
// On ARM side, ISO7816_MAX_FRAME is set to 260
|
||||||
// This means we can't receive more than 250 bytes of data to leave enough room for
|
// This means we can receive a full short APDU (256 bytes) of data and have enough room for
|
||||||
// SW status code and surrounding metadata without creating a buffer overflow.
|
// SW status code and surrounding metadata without creating a buffer overflow.
|
||||||
#define MAX_APDU_SIZE 250
|
#define MAX_APDU_SIZE 256
|
||||||
|
|
||||||
int CmdSmartcard(const char *Cmd);
|
int CmdSmartcard(const char *Cmd);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue