2018-07-04 18:19:04 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Willok, June 2018
|
|
|
|
// Edits by Iceman, July 2018
|
|
|
|
//
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// The main i2c code, for communications with smart card module
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#include "i2c.h"
|
|
|
|
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "proxmark3_arm.h"
|
|
|
|
#include "cmd.h"
|
|
|
|
#include "BigBuf.h"
|
|
|
|
#include "ticks.h"
|
|
|
|
#include "dbprint.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
#define GPIO_RST AT91C_PIO_PA1
|
|
|
|
#define GPIO_SCL AT91C_PIO_PA5
|
|
|
|
#define GPIO_SDA AT91C_PIO_PA7
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
#define SCL_H HIGH(GPIO_SCL)
|
|
|
|
#define SCL_L LOW(GPIO_SCL)
|
|
|
|
#define SDA_H HIGH(GPIO_SDA)
|
|
|
|
#define SDA_L LOW(GPIO_SDA)
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2020-06-12 01:20:59 +08:00
|
|
|
#define SCL_read ((AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL) == GPIO_SCL)
|
|
|
|
#define SDA_read ((AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA) == GPIO_SDA)
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-09 15:59:13 +08:00
|
|
|
#define I2C_ERROR "I2C_WaitAck Error"
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-08-06 19:51:10 +08:00
|
|
|
// Direct use the loop to delay. 6 instructions loop, Masterclock 48MHz,
|
2019-04-23 01:31:22 +08:00
|
|
|
// delay=1 is about 200kbps
|
2018-07-06 23:10:49 +08:00
|
|
|
// timer.
|
|
|
|
// I2CSpinDelayClk(4) = 12.31us
|
|
|
|
// I2CSpinDelayClk(1) = 3.07us
|
2021-04-08 15:34:11 +08:00
|
|
|
static volatile uint32_t c;
|
2020-05-10 22:59:38 +08:00
|
|
|
static void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
|
2019-03-10 02:19:50 +08:00
|
|
|
for (c = delay * 2; c; c--) {};
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
2018-09-25 08:54:51 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
#define I2C_DELAY_1CLK I2CSpinDelayClk(1)
|
|
|
|
#define I2C_DELAY_2CLK I2CSpinDelayClk(2)
|
|
|
|
#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
|
2018-07-04 21:29:27 +08:00
|
|
|
|
2018-07-09 17:22:51 +08:00
|
|
|
#define ISO7618_MAX_FRAME 255
|
|
|
|
|
2019-08-06 19:51:10 +08:00
|
|
|
// try i2c bus recovery at 100kHz = 5us high, 5us low
|
2019-03-10 18:20:22 +08:00
|
|
|
void I2C_recovery(void) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
DbpString("Performing i2c bus recovery");
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// reset I2C
|
2019-03-10 07:00:59 +08:00
|
|
|
SDA_H;
|
|
|
|
SCL_H;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
//9nth cycle acts as NACK
|
2019-03-10 02:19:50 +08:00
|
|
|
for (int i = 0; i < 10; i++) {
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_H;
|
|
|
|
WaitUS(5);
|
|
|
|
SCL_L;
|
|
|
|
WaitUS(5);
|
2019-03-10 02:19:50 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
|
|
|
//a STOP signal (SDA from low to high while CLK is high)
|
2019-03-10 07:00:59 +08:00
|
|
|
SDA_L;
|
|
|
|
WaitUS(5);
|
2020-06-12 01:20:59 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_H;
|
|
|
|
WaitUS(2);
|
|
|
|
SDA_H;
|
|
|
|
WaitUS(2);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bool isok = (SCL_read && SDA_read);
|
2018-11-14 22:07:04 +08:00
|
|
|
if (!SDA_read)
|
2019-03-10 02:19:50 +08:00
|
|
|
DbpString("I2C bus recovery error: SDA still LOW");
|
|
|
|
if (!SCL_read)
|
|
|
|
DbpString("I2C bus recovery error: SCL still LOW");
|
|
|
|
if (isok)
|
|
|
|
DbpString("I2C bus recovery complete");
|
2018-11-14 22:07:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void I2C_init(void) {
|
2019-03-10 02:19:50 +08:00
|
|
|
// Configure reset pin, close up pull up, push-pull output, default high
|
|
|
|
AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST;
|
|
|
|
AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// Configure I2C pin, open up, open leakage
|
|
|
|
AT91C_BASE_PIOA->PIO_PPUER |= (GPIO_SCL | GPIO_SDA);
|
|
|
|
AT91C_BASE_PIOA->PIO_MDER |= (GPIO_SCL | GPIO_SDA);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// default three lines all pull up
|
|
|
|
AT91C_BASE_PIOA->PIO_SODR |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
AT91C_BASE_PIOA->PIO_OER |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
|
|
|
|
AT91C_BASE_PIOA->PIO_PER |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
bool isok = (SCL_read && SDA_read);
|
2021-04-08 15:34:11 +08:00
|
|
|
if (isok == false)
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_recovery();
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the reset state
|
2019-03-10 18:20:22 +08:00
|
|
|
void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA) {
|
2019-03-10 02:19:50 +08:00
|
|
|
if (LineRST)
|
|
|
|
HIGH(GPIO_RST);
|
|
|
|
else
|
|
|
|
LOW(GPIO_RST);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (LineSCK)
|
|
|
|
HIGH(GPIO_SCL);
|
|
|
|
else
|
|
|
|
LOW(GPIO_SCL);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (LineSDA)
|
|
|
|
HIGH(GPIO_SDA);
|
|
|
|
else
|
|
|
|
LOW(GPIO_SDA);
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the SIM_Adapter, then enter the main program
|
|
|
|
// Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
|
2019-03-10 18:20:22 +08:00
|
|
|
void I2C_Reset_EnterMainProgram(void) {
|
2019-03-10 02:19:50 +08:00
|
|
|
StartTicks();
|
|
|
|
I2C_init();
|
|
|
|
I2C_SetResetStatus(0, 0, 0);
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitMS(30);
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SetResetStatus(1, 0, 0);
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitMS(30);
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SetResetStatus(1, 1, 1);
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitMS(10);
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the SIM_Adapter, then enter the bootloader program
|
2018-09-25 08:54:51 +08:00
|
|
|
// Reserve for firmware update.
|
2019-03-10 18:20:22 +08:00
|
|
|
void I2C_Reset_EnterBootloader(void) {
|
2019-03-10 02:19:50 +08:00
|
|
|
StartTicks();
|
|
|
|
I2C_init();
|
|
|
|
I2C_SetResetStatus(0, 1, 1);
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitMS(100);
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SetResetStatus(1, 1, 1);
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitMS(10);
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:59:13 +08:00
|
|
|
// Wait for the clock to go High.
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool WaitSCL_H_delay(uint32_t delay) {
|
2019-03-10 02:19:50 +08:00
|
|
|
while (delay--) {
|
|
|
|
if (SCL_read) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
}
|
|
|
|
return false;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
2018-07-07 00:30:01 +08:00
|
|
|
|
2018-07-06 23:10:49 +08:00
|
|
|
// 5000 * 3.07us = 15350us. 15.35ms
|
2018-08-13 03:54:31 +08:00
|
|
|
// 15000 * 3.07us = 46050us. 46.05ms
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool WaitSCL_H(void) {
|
2021-04-08 15:34:11 +08:00
|
|
|
return WaitSCL_H_delay(15000);
|
2018-08-13 03:54:31 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool WaitSCL_L_delay(uint32_t delay) {
|
2019-03-10 02:19:50 +08:00
|
|
|
while (delay--) {
|
|
|
|
if (!SCL_read) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
}
|
|
|
|
return false;
|
2018-08-13 03:54:31 +08:00
|
|
|
}
|
|
|
|
// 5000 * 3.07us = 15350us. 15.35ms
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool WaitSCL_L(void) {
|
2021-04-08 15:34:11 +08:00
|
|
|
return WaitSCL_L_delay(15000);
|
2018-07-06 22:01:23 +08:00
|
|
|
}
|
2018-07-07 00:30:01 +08:00
|
|
|
|
2019-01-10 23:06:59 +08:00
|
|
|
// Wait max 1800ms or until SCL goes LOW.
|
|
|
|
// It timeout reading response from card
|
2018-07-06 21:20:21 +08:00
|
|
|
// Which ever comes first
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool WaitSCL_L_timeout(void) {
|
2021-04-08 15:34:11 +08:00
|
|
|
volatile uint32_t delay = 1700;
|
2019-03-10 07:00:59 +08:00
|
|
|
while (delay--) {
|
2019-03-10 02:19:50 +08:00
|
|
|
// exit on SCL LOW
|
|
|
|
if (!SCL_read)
|
|
|
|
return true;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitMS(1);
|
2019-03-10 02:19:50 +08:00
|
|
|
}
|
|
|
|
return (delay == 0);
|
2018-07-06 21:20:21 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool I2C_Start(void) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2021-04-08 15:34:11 +08:00
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
I2C_DELAY_2CLK;
|
2019-03-10 07:00:59 +08:00
|
|
|
SDA_H;
|
|
|
|
I2C_DELAY_1CLK;
|
2019-03-10 02:19:50 +08:00
|
|
|
SCL_H;
|
|
|
|
if (!WaitSCL_H()) return false;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_2CLK;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (!SCL_read) return false;
|
|
|
|
if (!SDA_read) return false;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
SDA_L;
|
|
|
|
I2C_DELAY_2CLK;
|
2019-03-10 02:19:50 +08:00
|
|
|
return true;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool I2C_WaitForSim(void) {
|
2018-11-14 22:07:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// wait for data from card
|
|
|
|
if (!WaitSCL_L_timeout())
|
|
|
|
return false;
|
2018-07-07 18:42:33 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 8051 speaks with smart card.
|
|
|
|
// 1000*50*3.07 = 153.5ms
|
|
|
|
// 1byte transfer == 1ms with max frame being 256bytes
|
2021-04-08 15:34:11 +08:00
|
|
|
return WaitSCL_H_delay(1000 * 300);
|
2018-07-07 18:42:33 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:19:04 +08:00
|
|
|
// send i2c STOP
|
2020-05-10 22:59:38 +08:00
|
|
|
static void I2C_Stop(void) {
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_L;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
SDA_L;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
SCL_H;
|
|
|
|
I2C_DELAY_2CLK;
|
2019-03-10 02:19:50 +08:00
|
|
|
if (!WaitSCL_H()) return;
|
|
|
|
SDA_H;
|
2021-04-08 15:34:11 +08:00
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
I2C_DELAY_2CLK;
|
2018-07-08 15:18:08 +08:00
|
|
|
}
|
2018-07-09 17:22:51 +08:00
|
|
|
|
2018-07-04 18:19:04 +08:00
|
|
|
// Send i2c ACK
|
2020-05-10 22:59:38 +08:00
|
|
|
static void I2C_Ack(void) {
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_L;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
SDA_L;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
SCL_H;
|
|
|
|
I2C_DELAY_2CLK;
|
2019-03-10 02:19:50 +08:00
|
|
|
if (!WaitSCL_H()) return;
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_L;
|
|
|
|
I2C_DELAY_2CLK;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send i2c NACK
|
2020-05-10 22:59:38 +08:00
|
|
|
static void I2C_NoAck(void) {
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_L;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
SDA_H;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
SCL_H;
|
|
|
|
I2C_DELAY_2CLK;
|
2019-03-10 02:19:50 +08:00
|
|
|
if (!WaitSCL_H()) return;
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_L;
|
|
|
|
I2C_DELAY_2CLK;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
static bool I2C_WaitAck(void) {
|
2019-03-10 07:00:59 +08:00
|
|
|
SCL_L;
|
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
SDA_H;
|
|
|
|
I2C_DELAY_1CLK;
|
2019-03-10 02:19:50 +08:00
|
|
|
SCL_H;
|
|
|
|
if (!WaitSCL_H())
|
|
|
|
return false;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
if (SDA_read) {
|
|
|
|
SCL_L;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SCL_L;
|
|
|
|
return true;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
static void I2C_SendByte(uint8_t data) {
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t bits = 8;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
while (bits--) {
|
|
|
|
SCL_L;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_1CLK;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (data & 0x80)
|
|
|
|
SDA_H;
|
|
|
|
else
|
|
|
|
SDA_L;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
data <<= 1;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_1CLK;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
SCL_H;
|
|
|
|
if (!WaitSCL_H())
|
|
|
|
return;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
}
|
|
|
|
SCL_L;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
static int16_t I2C_ReadByte(void) {
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t bits = 8, b = 0;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
SDA_H;
|
|
|
|
while (bits--) {
|
|
|
|
b <<= 1;
|
|
|
|
SCL_L;
|
|
|
|
if (!WaitSCL_L()) return -2;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_1CLK;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
SCL_H;
|
|
|
|
if (!WaitSCL_H()) return -1;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
if (SDA_read)
|
|
|
|
b |= 0x01;
|
|
|
|
}
|
|
|
|
SCL_L;
|
|
|
|
return b;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2018-07-07 00:30:01 +08:00
|
|
|
// Sends one byte ( command to be written, SlaveDevice address)
|
2019-03-10 18:20:22 +08:00
|
|
|
bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
|
2019-03-10 02:19:50 +08:00
|
|
|
bool bBreak = true;
|
|
|
|
do {
|
|
|
|
if (!I2C_Start())
|
|
|
|
return false;
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bBreak = false;
|
|
|
|
} while (false);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Stop();
|
|
|
|
if (bBreak) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 3) DbpString(I2C_ERROR);
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-05 22:32:10 +08:00
|
|
|
}
|
|
|
|
|
2018-07-07 00:30:01 +08:00
|
|
|
// Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ).
|
2019-03-10 18:20:22 +08:00
|
|
|
bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) {
|
2019-03-10 02:19:50 +08:00
|
|
|
bool bBreak = true;
|
|
|
|
do {
|
|
|
|
if (!I2C_Start())
|
|
|
|
return false;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(data);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bBreak = false;
|
|
|
|
} while (false);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Stop();
|
|
|
|
if (bBreak) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 3) DbpString(I2C_ERROR);
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2018-09-25 08:54:51 +08:00
|
|
|
//Sends array of data (Array, length, command to be written , SlaveDevice address ).
|
2018-07-04 18:19:04 +08:00
|
|
|
// len = uint8 (max buffer to write 256bytes)
|
2019-03-10 18:20:22 +08:00
|
|
|
bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
|
2019-03-10 02:19:50 +08:00
|
|
|
bool bBreak = true;
|
|
|
|
do {
|
|
|
|
if (!I2C_Start())
|
|
|
|
return false;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
while (len) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(*data);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
len--;
|
|
|
|
data++;
|
|
|
|
}
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (len == 0)
|
|
|
|
bBreak = false;
|
|
|
|
} while (false);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Stop();
|
|
|
|
if (bBreak) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 3) DbpString(I2C_ERROR);
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2018-09-25 08:54:51 +08:00
|
|
|
// read one array of data (Data array, Readout length, command to be written , SlaveDevice address ).
|
2018-07-04 18:19:04 +08:00
|
|
|
// len = uint8 (max buffer to read 256bytes)
|
2019-03-10 18:20:22 +08:00
|
|
|
int16_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
|
2018-07-07 18:42:33 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!data || len == 0)
|
2019-03-10 02:19:50 +08:00
|
|
|
return 0;
|
2018-07-08 19:52:30 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// extra wait 500us (514us measured)
|
|
|
|
// 200us (xx measured)
|
2020-10-20 23:34:42 +08:00
|
|
|
WaitUS(600);
|
2020-06-12 01:20:59 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bool bBreak = true;
|
|
|
|
uint16_t readcount = 0;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
do {
|
|
|
|
if (!I2C_Start())
|
|
|
|
return 0;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 0xB0 / 0xC0 == i2c write
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 0xB1 / 0xC1 == i2c read
|
|
|
|
I2C_Start();
|
|
|
|
I2C_SendByte(device_address | 1);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bBreak = false;
|
|
|
|
} while (false);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (bBreak) {
|
|
|
|
I2C_Stop();
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 3) DbpString(I2C_ERROR);
|
2019-03-10 02:19:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-04 18:19:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
while (len) {
|
2018-07-08 16:50:05 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
int16_t tmp = I2C_ReadByte();
|
2019-03-10 07:00:59 +08:00
|
|
|
if (tmp < 0)
|
2019-03-10 02:19:50 +08:00
|
|
|
return tmp;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
*data = (uint8_t)tmp & 0xFF;
|
2018-07-08 16:50:05 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
len--;
|
2020-06-12 01:20:59 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// The first byte in response is the message length
|
|
|
|
if (!readcount && (len > *data)) {
|
|
|
|
len = *data;
|
|
|
|
} else {
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
readcount++;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// acknowledgements. After last byte send NACK.
|
|
|
|
if (len == 0)
|
|
|
|
I2C_NoAck();
|
|
|
|
else
|
|
|
|
I2C_Ack();
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Stop();
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// return bytecount - first byte (which is length byte)
|
|
|
|
return --readcount;
|
2018-07-04 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
|
2019-03-10 02:19:50 +08:00
|
|
|
//START, 0xB0, 0x00, 0x00, START, 0xB1, xx, yy, zz, ......, STOP
|
|
|
|
bool bBreak = true;
|
|
|
|
uint8_t readcount = 0;
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// sending
|
|
|
|
do {
|
|
|
|
if (!I2C_Start())
|
|
|
|
return 0;
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 0xB0 / 0xC0 i2c write
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(msb);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(lsb);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 0xB1 / 0xC1 i2c read
|
|
|
|
I2C_Start();
|
|
|
|
I2C_SendByte(device_address | 1);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bBreak = false;
|
|
|
|
} while (false);
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (bBreak) {
|
|
|
|
I2C_Stop();
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 3) DbpString(I2C_ERROR);
|
2019-03-10 02:19:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// reading
|
|
|
|
while (len) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
int16_t tmp = I2C_ReadByte();
|
2019-03-10 07:00:59 +08:00
|
|
|
if (tmp < 0)
|
2019-03-10 02:19:50 +08:00
|
|
|
return tmp;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
*data = (uint8_t)tmp & 0xFF;
|
2018-07-05 16:48:24 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
data++;
|
|
|
|
readcount++;
|
|
|
|
len--;
|
2018-07-08 16:50:05 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// acknowledgements. After last byte send NACK.
|
|
|
|
if (len == 0)
|
|
|
|
I2C_NoAck();
|
|
|
|
else
|
|
|
|
I2C_Ack();
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Stop();
|
|
|
|
return readcount;
|
2018-07-05 16:48:24 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
|
2019-03-10 02:19:50 +08:00
|
|
|
//START, 0xB0, 0x00, 0x00, xx, yy, zz, ......, STOP
|
|
|
|
bool bBreak = true;
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
do {
|
|
|
|
if (!I2C_Start())
|
|
|
|
return false;
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 0xB0 == i2c write
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(msb);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_SendByte(lsb);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
while (len) {
|
|
|
|
I2C_SendByte(*data);
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
break;
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
len--;
|
|
|
|
data++;
|
|
|
|
}
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if (len == 0)
|
|
|
|
bBreak = false;
|
|
|
|
} while (false);
|
2018-07-05 20:38:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Stop();
|
|
|
|
if (bBreak) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 3) DbpString(I2C_ERROR);
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-05 20:38:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void I2C_print_status(void) {
|
2020-06-12 01:20:59 +08:00
|
|
|
DbpString(_CYAN_("Smart card module (ISO 7816)"));
|
2019-05-03 15:04:28 +08:00
|
|
|
uint8_t maj, min;
|
|
|
|
if (I2C_get_version(&maj, &min) == PM3_SUCCESS)
|
2021-05-11 22:11:33 +08:00
|
|
|
Dbprintf(" version................. " _YELLOW_("v%x.%02d"), maj, min);
|
2019-05-03 15:04:28 +08:00
|
|
|
else
|
2021-05-11 22:11:33 +08:00
|
|
|
DbpString(" version................. " _RED_("FAILED"));
|
2019-05-03 15:04:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int I2C_get_version(uint8_t *maj, uint8_t *min) {
|
2019-03-10 07:00:59 +08:00
|
|
|
uint8_t resp[] = {0, 0, 0, 0};
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Reset_EnterMainProgram();
|
|
|
|
uint8_t len = I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN);
|
2019-05-03 15:04:28 +08:00
|
|
|
if (len > 0) {
|
|
|
|
*maj = resp[0];
|
|
|
|
*min = resp[1];
|
|
|
|
return PM3_SUCCESS;
|
|
|
|
}
|
2020-06-12 23:50:11 +08:00
|
|
|
return PM3_EDEVNOTSUPP;
|
2018-07-05 22:32:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 03:54:31 +08:00
|
|
|
// Will read response from smart card module, retries 3 times to get the data.
|
2020-08-03 23:38:18 +08:00
|
|
|
bool sc_rx_bytes(uint8_t *dest, uint8_t *destlen) {
|
2018-08-13 03:54:31 +08:00
|
|
|
|
2021-04-04 17:21:17 +08:00
|
|
|
uint8_t i = 5;
|
2019-03-10 02:19:50 +08:00
|
|
|
int16_t len = 0;
|
|
|
|
while (i--) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_WaitForSim();
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
len = I2C_BufferRead(dest, *destlen, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-10-25 07:12:20 +08:00
|
|
|
LED_C_ON();
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (len > 1) {
|
2019-03-10 02:19:50 +08:00
|
|
|
break;
|
2019-03-10 07:00:59 +08:00
|
|
|
} else if (len == 1) {
|
2019-03-10 02:19:50 +08:00
|
|
|
continue;
|
2019-03-10 07:00:59 +08:00
|
|
|
} else if (len <= 0) {
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 03:54:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// after three
|
2019-03-10 07:00:59 +08:00
|
|
|
if (len <= 1)
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
*destlen = (uint8_t)len & 0xFF;
|
|
|
|
return true;
|
2018-08-13 03:54:31 +08:00
|
|
|
}
|
|
|
|
|
2020-08-03 23:38:18 +08:00
|
|
|
bool GetATR(smart_card_atr_t *card_ptr, bool verbose) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2021-04-08 15:34:11 +08:00
|
|
|
if (card_ptr == NULL)
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
card_ptr->atr_len = 0;
|
|
|
|
memset(card_ptr->atr, 0, sizeof(card_ptr->atr));
|
2018-08-18 21:22:59 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// Send ATR
|
|
|
|
// start [C0 01] stop start C1 len aa bb cc stop]
|
|
|
|
I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN);
|
2018-07-07 18:42:33 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
//wait for sim card to answer.
|
2020-08-06 22:20:29 +08:00
|
|
|
// 1byte = 1ms , max frame 256bytes. Should wait 256ms atleast just in case.
|
|
|
|
if (I2C_WaitForSim() == false)
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
2020-11-02 08:46:47 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// read bytes from module
|
|
|
|
uint8_t len = sizeof(card_ptr->atr);
|
2020-08-06 22:20:29 +08:00
|
|
|
if (sc_rx_bytes(card_ptr->atr, &len) == false)
|
2019-03-10 02:19:50 +08:00
|
|
|
return false;
|
2018-07-08 19:52:30 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t pos_td = 1;
|
2019-03-10 07:00:59 +08:00
|
|
|
if ((card_ptr->atr[1] & 0x10) == 0x10) pos_td++;
|
|
|
|
if ((card_ptr->atr[1] & 0x20) == 0x20) pos_td++;
|
|
|
|
if ((card_ptr->atr[1] & 0x40) == 0x40) pos_td++;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// T0 indicate presence T=0 vs T=1. T=1 has checksum TCK
|
2019-03-10 07:00:59 +08:00
|
|
|
if ((card_ptr->atr[1] & 0x80) == 0x80) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
pos_td++;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// 1 == T1 , presence of checksum TCK
|
2019-03-10 07:00:59 +08:00
|
|
|
if ((card_ptr->atr[pos_td] & 0x01) == 0x01) {
|
2018-08-13 03:54:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t chksum = 0;
|
|
|
|
// xor property. will be zero when xored with chksum.
|
|
|
|
for (uint8_t i = 1; i < len; ++i)
|
|
|
|
chksum ^= card_ptr->atr[i];
|
2018-08-13 03:54:31 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (chksum) {
|
2021-08-22 05:02:27 +08:00
|
|
|
if (g_dbglevel > 2) DbpString("Wrong ATR checksum");
|
2019-03-10 02:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 03:54:31 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
card_ptr->atr_len = len;
|
2020-08-03 23:38:18 +08:00
|
|
|
if (verbose) {
|
|
|
|
LogTrace(card_ptr->atr, card_ptr->atr_len, 0, 0, NULL, false);
|
|
|
|
}
|
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
return true;
|
2018-07-06 17:16:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SmartCardAtr(void) {
|
2019-03-10 02:19:50 +08:00
|
|
|
LED_D_ON();
|
|
|
|
set_tracing(true);
|
|
|
|
I2C_Reset_EnterMainProgram();
|
2020-10-25 07:12:20 +08:00
|
|
|
smart_card_atr_t card;
|
2021-04-08 15:34:11 +08:00
|
|
|
if (GetATR(&card, true)) {
|
|
|
|
reply_ng(CMD_SMART_ATR, PM3_SUCCESS, (uint8_t *)&card, sizeof(smart_card_atr_t));
|
|
|
|
} else {
|
|
|
|
reply_ng(CMD_SMART_ATR, PM3_ETIMEOUT, NULL, 0);
|
2021-04-04 23:01:43 +08:00
|
|
|
}
|
2019-03-10 02:19:50 +08:00
|
|
|
set_tracing(false);
|
|
|
|
LEDsoff();
|
2021-04-08 15:34:11 +08:00
|
|
|
// StopTicks();
|
2018-07-05 22:32:10 +08:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:34:11 +08:00
|
|
|
void SmartCardRaw(smart_card_raw_t *p) {
|
2019-03-10 02:19:50 +08:00
|
|
|
LED_D_ON();
|
2018-07-09 17:22:51 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t len = 0;
|
|
|
|
uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME);
|
2021-04-08 15:34:11 +08:00
|
|
|
// check if alloacted...
|
|
|
|
smartcard_command_t flags = p->flags;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-07-29 17:00:06 +08:00
|
|
|
if ((flags & SC_CLEARLOG) == SC_CLEARLOG)
|
2019-03-10 02:19:50 +08:00
|
|
|
clear_trace();
|
2018-07-09 17:22:51 +08:00
|
|
|
|
2020-07-29 17:00:06 +08:00
|
|
|
if ((flags & SC_LOG) == SC_LOG)
|
|
|
|
set_tracing(true);
|
2021-04-08 16:44:31 +08:00
|
|
|
else
|
2020-07-29 17:00:06 +08:00
|
|
|
set_tracing(false);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2021-04-08 15:34:11 +08:00
|
|
|
if ((flags & SC_CONNECT) == SC_CONNECT) {
|
2018-11-14 22:07:04 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Reset_EnterMainProgram();
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2021-04-08 15:34:11 +08:00
|
|
|
if ((flags & SC_SELECT) == SC_SELECT) {
|
2019-03-10 02:19:50 +08:00
|
|
|
smart_card_atr_t card;
|
2020-08-03 23:38:18 +08:00
|
|
|
bool gotATR = GetATR(&card, true);
|
2019-04-18 18:43:35 +08:00
|
|
|
//reply_old(CMD_ACK, gotATR, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
|
2021-04-08 15:34:11 +08:00
|
|
|
if (gotATR == false) {
|
|
|
|
reply_ng(CMD_SMART_RAW, PM3_ESOFT, NULL, 0);
|
2019-03-10 02:19:50 +08:00
|
|
|
goto OUT;
|
2021-04-08 15:34:11 +08:00
|
|
|
}
|
2019-03-10 02:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 17:51:19 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
if ((flags & SC_RAW) || (flags & SC_RAW_T0)) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2021-04-08 15:34:11 +08:00
|
|
|
LogTrace(p->data, p->len, 0, 0, NULL, true);
|
|
|
|
|
|
|
|
bool res = I2C_BufferWrite(
|
2021-04-08 16:44:31 +08:00
|
|
|
p->data,
|
|
|
|
p->len,
|
|
|
|
((flags & SC_RAW_T0) ? I2C_DEVICE_CMD_SEND_T0 : I2C_DEVICE_CMD_SEND),
|
|
|
|
I2C_DEVICE_ADDRESS_MAIN
|
|
|
|
);
|
2021-08-22 05:02:27 +08:00
|
|
|
if (res == false && g_dbglevel > 3) {
|
2021-04-08 15:34:11 +08:00
|
|
|
DbpString(I2C_ERROR);
|
|
|
|
reply_ng(CMD_SMART_RAW, PM3_ESOFT, NULL, 0);
|
|
|
|
goto OUT;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// read bytes from module
|
|
|
|
len = ISO7618_MAX_FRAME;
|
|
|
|
res = sc_rx_bytes(resp, &len);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (res) {
|
2019-03-10 02:19:50 +08:00
|
|
|
LogTrace(resp, len, 0, 0, NULL, false);
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 15:34:11 +08:00
|
|
|
|
|
|
|
reply_ng(CMD_SMART_RAW, PM3_SUCCESS, resp, len);
|
|
|
|
|
2019-03-09 15:59:13 +08:00
|
|
|
OUT:
|
2019-03-10 02:19:50 +08:00
|
|
|
BigBuf_free();
|
|
|
|
set_tracing(false);
|
|
|
|
LEDsoff();
|
2018-07-05 22:32:10 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SmartCardUpgrade(uint64_t arg0) {
|
2018-07-07 02:14:55 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
LED_C_ON();
|
2018-07-07 02:14:55 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
#define I2C_BLOCK_SIZE 128
|
2019-03-10 02:19:50 +08:00
|
|
|
// write. Sector0, with 11,22,33,44
|
|
|
|
// erase is 128bytes, and takes 50ms to execute
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
I2C_Reset_EnterBootloader();
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
bool isOK = true;
|
2019-04-10 03:35:06 +08:00
|
|
|
uint16_t length = arg0, pos = 0;
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t *fwdata = BigBuf_get_addr();
|
|
|
|
uint8_t *verfiydata = BigBuf_malloc(I2C_BLOCK_SIZE);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
while (length) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
uint8_t msb = (pos >> 8) & 0xFF;
|
|
|
|
uint8_t lsb = pos & 0xFF;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
Dbprintf("FW %02X%02X", msb, lsb);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
size_t size = MIN(I2C_BLOCK_SIZE, length);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// write
|
2019-04-10 03:35:06 +08:00
|
|
|
int16_t res = I2C_WriteFW(fwdata + pos, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!res) {
|
2019-03-10 02:19:50 +08:00
|
|
|
DbpString("Writing failed");
|
|
|
|
isOK = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// writing takes time.
|
2020-10-26 07:38:13 +08:00
|
|
|
WaitMS(50);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// read
|
|
|
|
res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (res <= 0) {
|
2019-03-10 02:19:50 +08:00
|
|
|
DbpString("Reading back failed");
|
|
|
|
isOK = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
// cmp
|
2019-03-10 07:00:59 +08:00
|
|
|
if (0 != memcmp(fwdata + pos, verfiydata, size)) {
|
2019-03-10 02:19:50 +08:00
|
|
|
DbpString("not equal data");
|
|
|
|
isOK = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 02:19:50 +08:00
|
|
|
length -= size;
|
|
|
|
pos += size;
|
|
|
|
}
|
2020-11-02 08:46:47 +08:00
|
|
|
|
2020-10-26 07:38:13 +08:00
|
|
|
reply_ng(CMD_SMART_UPGRADE, (isOK) ? PM3_SUCCESS : PM3_ESOFT, NULL, 0);
|
2019-03-10 02:19:50 +08:00
|
|
|
LED_C_OFF();
|
|
|
|
BigBuf_free();
|
2018-07-06 06:24:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SmartCardSetBaud(uint64_t arg0) {
|
2018-07-06 06:24:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void SmartCardSetClock(uint64_t arg0) {
|
2019-03-10 02:19:50 +08:00
|
|
|
LED_D_ON();
|
|
|
|
set_tracing(true);
|
|
|
|
I2C_Reset_EnterMainProgram();
|
|
|
|
// Send SIM CLC
|
|
|
|
// start [C0 05 xx] stop
|
|
|
|
I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN);
|
2020-10-20 23:34:42 +08:00
|
|
|
reply_ng(CMD_SMART_SETCLOCK, PM3_SUCCESS, NULL, 0);
|
2019-03-10 02:19:50 +08:00
|
|
|
set_tracing(false);
|
|
|
|
LEDsoff();
|
2019-03-06 02:44:02 +08:00
|
|
|
}
|