sc cleanup - add init to all sc commands...

because cmds won't work until the first init happens.  (multiple inits
don't appear to affect it negatively)
This commit is contained in:
marshmellow42 2018-08-07 12:28:08 -04:00
commit 49d92e3c7e
5 changed files with 109 additions and 105 deletions

View file

@ -361,12 +361,15 @@ void SendStatus(void)
{ {
BigBuf_print_status(); BigBuf_print_status();
Fpga_print_status(); Fpga_print_status();
#ifdef WITH_SMARTCARD
I2C_print_status();
#endif
printConfig(); //LF Sampling config printConfig(); //LF Sampling config
printUSBSpeed(); printUSBSpeed();
Dbprintf("Various"); Dbprintf("Various");
Dbprintf(" MF_DBGLEVEL......%d", MF_DBGLEVEL); Dbprintf(" MF_DBGLEVEL........%d", MF_DBGLEVEL);
Dbprintf(" ToSendMax........%d",ToSendMax); Dbprintf(" ToSendMax..........%d", ToSendMax);
Dbprintf(" ToSendBit........%d",ToSendBit); Dbprintf(" ToSendBit..........%d", ToSendBit);
cmd_send(CMD_ACK,1,0,0,0,0); cmd_send(CMD_ACK,1,0,0,0,0);
} }
@ -1262,10 +1265,6 @@ void UsbPacketReceived(uint8_t *packet, int len)
SmartCardAtr(); SmartCardAtr();
break; break;
} }
case CMD_SMART_SETBAUD:{
SmartCardSetBaud(c->arg[0]);
break;
}
case CMD_SMART_SETCLOCK:{ case CMD_SMART_SETCLOCK:{
SmartCardSetClock(c->arg[0]); SmartCardSetClock(c->arg[0]);
break; break;

View file

@ -13,17 +13,17 @@
#include "string.h" //for memset memcmp #include "string.h" //for memset memcmp
// 定义连接引脚 // 定义连接引脚
#define GPIO_RST AT91C_PIO_PA1 #define GPIO_RST AT91C_PIO_PA1
#define GPIO_SCL AT91C_PIO_PA5 #define GPIO_SCL AT91C_PIO_PA5
#define GPIO_SDA AT91C_PIO_PA7 #define GPIO_SDA AT91C_PIO_PA7
#define SCL_H HIGH(GPIO_SCL) #define SCL_H HIGH(GPIO_SCL)
#define SCL_L LOW(GPIO_SCL) #define SCL_L LOW(GPIO_SCL)
#define SDA_H HIGH(GPIO_SDA) #define SDA_H HIGH(GPIO_SDA)
#define SDA_L LOW(GPIO_SDA) #define SDA_L LOW(GPIO_SDA)
#define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL) #define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL)
#define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA) #define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA)
#define I2C_ERROR "I2C_WaitAck Error" #define I2C_ERROR "I2C_WaitAck Error"
@ -36,11 +36,11 @@ volatile unsigned long c;
void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) { void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
for (c = delay * 2; c; c--) {}; for (c = delay * 2; c; c--) {};
} }
// 通讯延迟函数 communication delay function // 通讯延迟函数 communication delay function
#define I2C_DELAY_1CLK I2CSpinDelayClk(1) #define I2C_DELAY_1CLK I2CSpinDelayClk(1)
#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 255 #define ISO7618_MAX_FRAME 255
@ -50,10 +50,10 @@ void I2C_init(void) {
// Configure reset pin, close up pull up, push-pull output, default high // Configure reset pin, close up pull up, push-pull output, default high
AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST; AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST;
AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST; AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST;
// 配置 I2C 引脚,开启上拉,开漏输出 // 配置 I2C 引脚,开启上拉,开漏输出
// Configure I2C pin, open up, open leakage // Configure I2C pin, open up, open leakage
AT91C_BASE_PIOA->PIO_PPUER |= (GPIO_SCL | GPIO_SDA); // 打开上拉 Open up the pull up AT91C_BASE_PIOA->PIO_PPUER |= (GPIO_SCL | GPIO_SDA); // 打开上拉 Open up the pull up
AT91C_BASE_PIOA->PIO_MDER |= (GPIO_SCL | GPIO_SDA); AT91C_BASE_PIOA->PIO_MDER |= (GPIO_SCL | GPIO_SDA);
// 默认三根线全部拉高 // 默认三根线全部拉高
@ -90,11 +90,11 @@ void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA) {
// Reset the SIM_Adapter, then enter the main program // 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. // Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
void I2C_Reset_EnterMainProgram(void) { void I2C_Reset_EnterMainProgram(void) {
I2C_SetResetStatus(0, 0, 0); // 拉低复位线 I2C_SetResetStatus(0, 0, 0); // 拉低复位线
SpinDelay(30); SpinDelay(30);
I2C_SetResetStatus(1, 0, 0); // 解除复位 I2C_SetResetStatus(1, 0, 0); // 解除复位
SpinDelay(30); SpinDelay(30);
I2C_SetResetStatus(1, 1, 1); // 拉高数据线 I2C_SetResetStatus(1, 1, 1); // 拉高数据线
SpinDelay(10); SpinDelay(10);
} }
@ -102,16 +102,16 @@ void I2C_Reset_EnterMainProgram(void) {
// Reset the SIM_Adapter, then enter the bootloader program // Reset the SIM_Adapter, then enter the bootloader program
// ReserveFor firmware update. // ReserveFor firmware update.
void I2C_Reset_EnterBootloader(void) { void I2C_Reset_EnterBootloader(void) {
I2C_SetResetStatus(0, 1, 1); // 拉低复位线 I2C_SetResetStatus(0, 1, 1); // 拉低复位线
SpinDelay(100); SpinDelay(100);
I2C_SetResetStatus(1, 1, 1); // 解除复位 I2C_SetResetStatus(1, 1, 1); // 解除复位
SpinDelay(10); SpinDelay(10);
} }
// 等待时钟变高 // 等待时钟变高
// Wait for the clock to go High. // Wait for the clock to go High.
bool WaitSCL_H_delay(uint32_t delay) { bool WaitSCL_H_delay(uint32_t delay) {
while (delay--) { while (delay--) {
if (SCL_read) { if (SCL_read) {
return true; return true;
} }
@ -127,27 +127,27 @@ bool WaitSCL_H(void) {
// Wait max 300ms or until SCL goes LOW. // Wait max 300ms or until SCL goes LOW.
// Which ever comes first // Which ever comes first
bool WaitSCL_L_300ms(void){ bool WaitSCL_L_300ms(void) {
volatile uint16_t delay = 300; volatile uint16_t delay = 300;
while ( delay-- ) { while ( delay-- ) {
// exit on SCL LOW // exit on SCL LOW
if (!SCL_read) if (!SCL_read)
return true; return true;
SpinDelay(1); SpinDelay(1);
} }
return (delay == 0); return (delay == 0);
} }
bool I2C_Start(void) { bool I2C_Start(void) {
I2C_DELAY_XCLK(4); I2C_DELAY_XCLK(4);
SDA_H; I2C_DELAY_1CLK; SDA_H; I2C_DELAY_1CLK;
SCL_H; SCL_H;
if (!WaitSCL_H()) return false; if (!WaitSCL_H()) return false;
I2C_DELAY_2CLK; I2C_DELAY_2CLK;
if (!SCL_read) return false; if (!SCL_read) return false;
if (!SDA_read) return false; if (!SDA_read) return false;
@ -211,17 +211,17 @@ bool I2C_WaitAck(void) {
return true; return true;
} }
void I2C_SendByte(uint8_t data) { void I2C_SendByte(uint8_t data) {
uint8_t i = 8; uint8_t i = 8;
while (i--) { while (i--) {
SCL_L; I2C_DELAY_1CLK; SCL_L; I2C_DELAY_1CLK;
if (data & 0x80) if (data & 0x80)
SDA_H; SDA_H;
else else
SDA_L; SDA_L;
data <<= 1; data <<= 1;
I2C_DELAY_1CLK; I2C_DELAY_1CLK;
@ -238,7 +238,7 @@ uint8_t I2C_ReadByte(void) {
uint8_t i = 8, b = 0; uint8_t i = 8, b = 0;
SDA_H; SDA_H;
while (i--) { while (i--) {
b <<= 1; b <<= 1;
SCL_L; I2C_DELAY_2CLK; SCL_L; I2C_DELAY_2CLK;
SCL_H; SCL_H;
@ -256,7 +256,7 @@ uint8_t I2C_ReadByte(void) {
// Sends one byte ( command to be written, SlaveDevice address) // Sends one byte ( command to be written, SlaveDevice address)
bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) { bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
bool bBreak = true; bool bBreak = true;
do { do {
if (!I2C_Start()) if (!I2C_Start())
return false; return false;
//[C0] //[C0]
@ -272,7 +272,7 @@ bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
} while (false); } while (false);
I2C_Stop(); I2C_Stop();
if (bBreak) { if (bBreak) {
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
return false; return false;
} }
@ -283,7 +283,7 @@ bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
// Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ). // Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ).
bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) { bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) {
bool bBreak = true; bool bBreak = true;
do { do {
if (!I2C_Start()) if (!I2C_Start())
return false; return false;
@ -303,7 +303,7 @@ bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) {
} while (false); } while (false);
I2C_Stop(); I2C_Stop();
if (bBreak) { if (bBreak) {
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
return false; return false;
} }
@ -334,7 +334,7 @@ bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t dev
break; break;
len--; len--;
data++; data++;
} }
if (len == 0) if (len == 0)
@ -342,11 +342,11 @@ bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t dev
} while (false); } while (false);
I2C_Stop(); I2C_Stop();
if (bBreak) { if (bBreak) {
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
return false; return false;
} }
return true; return true;
} }
// 读出1串数据存放读出数据待读出长度带读出地址器件类型 // 读出1串数据存放读出数据待读出长度带读出地址器件类型
@ -359,10 +359,10 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
// extra wait 500us (514us measured) // extra wait 500us (514us measured)
// 200us (xx measured) // 200us (xx measured)
SpinDelayUs(200); SpinDelayUs(200);
bool bBreak = true; bool bBreak = true;
uint8_t readcount = 0; uint8_t readcount = 0;
do { do {
if (!I2C_Start()) if (!I2C_Start())
return 0; return 0;
@ -375,7 +375,7 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
I2C_SendByte(device_cmd); I2C_SendByte(device_cmd);
if (!I2C_WaitAck()) if (!I2C_WaitAck())
break; break;
// 0xB1 / 0xC1 == i2c read // 0xB1 / 0xC1 == i2c read
I2C_Start(); I2C_Start();
I2C_SendByte(device_address | 1); I2C_SendByte(device_address | 1);
@ -385,7 +385,7 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
bBreak = false; bBreak = false;
} while (false); } while (false);
if (bBreak) { if (bBreak) {
I2C_Stop(); I2C_Stop();
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
return 0; return 0;
@ -403,17 +403,17 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
if (!readcount && (len > *data)) { if (!readcount && (len > *data)) {
len = *data; len = *data;
} else { } else {
data++; data++;
} }
readcount++; readcount++;
// acknowledgements. After last byte send NACK. // acknowledgements. After last byte send NACK.
if (len == 0) if (len == 0)
I2C_NoAck(); I2C_NoAck();
else else
I2C_Ack(); I2C_Ack();
} }
I2C_Stop(); I2C_Stop();
// return bytecount - first byte (which is length byte) // return bytecount - first byte (which is length byte)
return (readcount) ? --readcount : 0; return (readcount) ? --readcount : 0;
@ -453,7 +453,7 @@ uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t
bBreak = false; bBreak = false;
} while (false); } while (false);
if (bBreak) { if (bBreak) {
I2C_Stop(); I2C_Stop();
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
return 0; return 0;
@ -473,7 +473,7 @@ uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t
else else
I2C_Ack(); I2C_Ack();
} }
I2C_Stop(); I2C_Stop();
return readcount; return readcount;
} }
@ -490,7 +490,7 @@ bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t d
I2C_SendByte(device_address & 0xFE); I2C_SendByte(device_address & 0xFE);
if (!I2C_WaitAck()) if (!I2C_WaitAck())
break; break;
// msb // msb
I2C_SendByte(msb); I2C_SendByte(msb);
if (!I2C_WaitAck()) if (!I2C_WaitAck())
@ -507,7 +507,7 @@ bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t d
break; break;
len--; len--;
data++; data++;
} }
if (len == 0) if (len == 0)
@ -515,11 +515,11 @@ bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t d
} while (false); } while (false);
I2C_Stop(); I2C_Stop();
if (bBreak) { if (bBreak) {
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
return false; return false;
} }
return true; return true;
} }
void I2C_print_status(void) { void I2C_print_status(void) {
@ -529,19 +529,19 @@ void I2C_print_status(void) {
I2C_Reset_EnterMainProgram(); I2C_Reset_EnterMainProgram();
uint8_t len = I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN); uint8_t len = I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN);
if ( len > 0 ) if ( len > 0 )
Dbprintf(" version.................v%x.%02x", resp[0], resp[1]); Dbprintf(" version.................v%x.%02x", resp[0], resp[1]);
else else
DbpString(" version.................FAILED"); DbpString(" version.................FAILED");
} }
bool GetATR(smart_card_atr_t *card_ptr) { bool GetATR(smart_card_atr_t *card_ptr) {
// clear // clear
if ( card_ptr ) { if ( card_ptr ) {
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));
} }
// Send ATR // Send ATR
// start [C0 01] stop start C1 len aa bb cc stop] // start [C0 01] stop start C1 len aa bb cc stop]
I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN); I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN);
@ -551,10 +551,10 @@ bool GetATR(smart_card_atr_t *card_ptr) {
//wait for sim card to answer. //wait for sim card to answer.
if (!I2C_WaitForSim()) if (!I2C_WaitForSim())
return false; return false;
// read answer // read answer
uint8_t len = I2C_BufferRead(card_ptr->atr, sizeof(card_ptr->atr), I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); uint8_t len = I2C_BufferRead(card_ptr->atr, sizeof(card_ptr->atr), I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
if ( len == 0 ) if ( len == 0 )
return false; return false;
@ -584,6 +584,7 @@ void SmartCardAtr(void) {
LED_D_ON(); LED_D_ON();
clear_trace(); clear_trace();
set_tracing(true); set_tracing(true);
I2C_init();
I2C_Reset_EnterMainProgram(); I2C_Reset_EnterMainProgram();
bool isOK = GetATR( &card ); bool isOK = GetATR( &card );
cmd_send(CMD_ACK, isOK, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t)); cmd_send(CMD_ACK, isOK, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
@ -598,16 +599,17 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) {
uint8_t len = 0; uint8_t len = 0;
uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME); uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME);
smartcard_command_t flags = arg0; smartcard_command_t flags = arg0;
if ((flags & SC_CONNECT)) if ((flags & SC_CONNECT))
clear_trace(); clear_trace();
set_tracing(true); set_tracing(true);
if ((flags & SC_CONNECT)) { if ((flags & SC_CONNECT)) {
I2C_init();
I2C_Reset_EnterMainProgram(); I2C_Reset_EnterMainProgram();
if ( !(flags & SC_NO_SELECT) ) { if ( !(flags & SC_NO_SELECT) ) {
smart_card_atr_t card; smart_card_atr_t card;
bool gotATR = GetATR( &card ); bool gotATR = GetATR( &card );
@ -618,9 +620,9 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) {
} }
if ((flags & SC_RAW)) { if ((flags & SC_RAW)) {
LogTrace(data, arg1, 0, 0, NULL, true); LogTrace(data, arg1, 0, 0, NULL, true);
// Send raw bytes // Send raw bytes
// asBytes = A0 A4 00 00 02 // asBytes = A0 A4 00 00 02
// arg1 = len 5 // arg1 = len 5
@ -632,7 +634,7 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) {
len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
LogTrace(resp, len, 0, 0, NULL, false); LogTrace(resp, len, 0, 0, NULL, false);
} }
OUT: OUT:
cmd_send(CMD_ACK, len, 0, 0, resp, len); cmd_send(CMD_ACK, len, 0, 0, resp, len);
set_tracing(false); set_tracing(false);
LEDsoff(); LEDsoff();
@ -645,8 +647,9 @@ void SmartCardUpgrade(uint64_t arg0) {
#define I2C_BLOCK_SIZE 128 #define I2C_BLOCK_SIZE 128
// write. Sector0, with 11,22,33,44 // write. Sector0, with 11,22,33,44
// erase is 128bytes, and takes 50ms to execute // erase is 128bytes, and takes 50ms to execute
I2C_Reset_EnterBootloader(); I2C_init();
I2C_Reset_EnterBootloader();
bool isOK = true; bool isOK = true;
uint8_t res = 0; uint8_t res = 0;
@ -654,16 +657,16 @@ void SmartCardUpgrade(uint64_t arg0) {
uint16_t pos = 0; uint16_t pos = 0;
uint8_t *fwdata = BigBuf_get_addr(); uint8_t *fwdata = BigBuf_get_addr();
uint8_t *verfiydata = BigBuf_malloc(I2C_BLOCK_SIZE); uint8_t *verfiydata = BigBuf_malloc(I2C_BLOCK_SIZE);
while (length) { while (length) {
uint8_t msb = (pos >> 8) & 0xFF; uint8_t msb = (pos >> 8) & 0xFF;
uint8_t lsb = pos & 0xFF; uint8_t lsb = pos & 0xFF;
Dbprintf("FW %02X%02X", msb, lsb); Dbprintf("FW %02X%02X", msb, lsb);
size_t size = MIN(I2C_BLOCK_SIZE, length); size_t size = MIN(I2C_BLOCK_SIZE, length);
// write // write
res = I2C_WriteFW(fwdata+pos, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT); res = I2C_WriteFW(fwdata+pos, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
if ( !res ) { if ( !res ) {
@ -671,7 +674,7 @@ void SmartCardUpgrade(uint64_t arg0) {
isOK = false; isOK = false;
break; break;
} }
// writing takes time. // writing takes time.
SpinDelay(50); SpinDelay(50);
@ -679,36 +682,38 @@ void SmartCardUpgrade(uint64_t arg0) {
res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT); res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
if ( res == 0) { if ( res == 0) {
DbpString("Reading back failed"); DbpString("Reading back failed");
isOK = false; isOK = false;
break; break;
} }
// cmp // cmp
if ( 0 != memcmp(fwdata+pos, verfiydata, size)) { if ( 0 != memcmp(fwdata+pos, verfiydata, size)) {
DbpString("not equal data"); DbpString("not equal data");
isOK = false; isOK = false;
break; break;
} }
length -= size; length -= size;
pos += size; pos += size;
} }
cmd_send(CMD_ACK, isOK, pos, 0, 0, 0); cmd_send(CMD_ACK, isOK, pos, 0, 0, 0);
LED_C_OFF(); LED_C_OFF();
} }
void SmartCardSetBaud(uint64_t arg0) { // unfinished (or not needed?)
} //void SmartCardSetBaud(uint64_t arg0) {
//}
void SmartCardSetClock(uint64_t arg0) { void SmartCardSetClock(uint64_t arg0) {
LED_D_ON(); LED_D_ON();
set_tracing(true); set_tracing(true);
I2C_Reset_EnterMainProgram(); I2C_init();
I2C_Reset_EnterMainProgram();
// Send SIM CLC // Send SIM CLC
// start [C0 05 xx] stop // start [C0 05 xx] stop
I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN); I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN);
cmd_send(CMD_ACK, 1, 0, 0, 0, 0); cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
set_tracing(false); set_tracing(false);
LEDsoff(); LEDsoff();

View file

@ -46,14 +46,13 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address); uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address);
bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address); bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address);
//
bool GetATR(smart_card_atr_t *card_ptr); bool GetATR(smart_card_atr_t *card_ptr);
// generice functions // generic functions
void SmartCardAtr(void); void SmartCardAtr(void);
void SmartCardRaw(uint64_t arg0, uint64_t arg1, uint8_t *data); void SmartCardRaw(uint64_t arg0, uint64_t arg1, uint8_t *data);
void SmartCardUpgrade(uint64_t arg0); void SmartCardUpgrade(uint64_t arg0);
void SmartCardSetBaud(uint64_t arg0); //void SmartCardSetBaud(uint64_t arg0);
void SmartCardSetClock(uint64_t arg0); void SmartCardSetClock(uint64_t arg0);
void I2C_print_status(void); void I2C_print_status(void);
#endif #endif

View file

@ -43,7 +43,7 @@ int usage_sm_info(void) {
PrintAndLog(" s : silent (no messages)"); PrintAndLog(" s : silent (no messages)");
PrintAndLog(""); PrintAndLog("");
PrintAndLog("Examples:"); PrintAndLog("Examples:");
PrintAndLog(" sc info"); PrintAndLog(" sc info");
return 0; return 0;
} }
int usage_sm_upgrade(void) { int usage_sm_upgrade(void) {
@ -64,7 +64,7 @@ int usage_sm_setclock(void) {
PrintAndLog(" c <> : clockspeed (0 = 16mhz, 1=8mhz, 2=4mhz) "); PrintAndLog(" c <> : clockspeed (0 = 16mhz, 1=8mhz, 2=4mhz) ");
PrintAndLog(""); PrintAndLog("");
PrintAndLog("Examples:"); PrintAndLog("Examples:");
PrintAndLog(" sc setclock c 2"); PrintAndLog(" sc setclock c 2");
return 0; return 0;
} }
@ -72,11 +72,11 @@ int CmdSmartRaw(const char *Cmd) {
int hexlen = 0; int hexlen = 0;
bool active = false; bool active = false;
bool active_select = false; bool active_select = false;
uint8_t cmdp = 0; uint8_t cmdp = 0;
bool errors = false, reply = true, decodeTLV = false, breakloop = false; bool errors = false, reply = true, decodeTLV = false, breakloop = false;
uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) { switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h': return usage_sm_raw(); case 'h': return usage_sm_raw();
@ -123,11 +123,11 @@ int CmdSmartRaw(const char *Cmd) {
} }
//Validations //Validations
if (errors || cmdp == 0 ) return usage_sm_raw(); if (errors || cmdp == 0 ) return usage_sm_raw();
// arg0 = RFU flags // arg0 = RFU flags
// arg1 = length // arg1 = length
UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}}; UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}};
if (active || active_select) { if (active || active_select) {
c.arg[0] |= SC_CONNECT; c.arg[0] |= SC_CONNECT;
@ -249,20 +249,20 @@ int CmdSmartUpgrade(const char *Cmd) {
uint32_t bytes_remaining = bytes_read; uint32_t bytes_remaining = bytes_read;
while (bytes_remaining > 0){ while (bytes_remaining > 0){
uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining); uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining);
UsbCommand c = {CMD_SMART_UPLOAD, {index + bytes_sent, bytes_in_packet, 0}}; UsbCommand c = {CMD_SMART_UPLOAD, {index + bytes_sent, bytes_in_packet, 0}};
// Fill usb bytes with 0xFF // Fill usb bytes with 0xFF
memset(c.d.asBytes, 0xFF, USB_CMD_DATA_SIZE); memset(c.d.asBytes, 0xFF, USB_CMD_DATA_SIZE);
memcpy(c.d.asBytes, dump + bytes_sent, bytes_in_packet); memcpy(c.d.asBytes, dump + bytes_sent, bytes_in_packet);
clearCommandBuffer(); clearCommandBuffer();
SendCommand(&c); SendCommand(&c);
if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000) ) { if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000) ) {
PrintAndLog("timeout while waiting for reply."); PrintAndLog("timeout while waiting for reply.");
free(dump); free(dump);
return 1; return 1;
} }
bytes_remaining -= bytes_in_packet; bytes_remaining -= bytes_in_packet;
bytes_sent += bytes_in_packet; bytes_sent += bytes_in_packet;
printf("."); fflush(stdout); printf("."); fflush(stdout);
@ -562,7 +562,7 @@ int ScTraceList(const char *Cmd) {
bool saveToFile = false; bool saveToFile = false;
char type[5] = {0}; char type[5] = {0};
char filename[FILE_PATH_SIZE] = {0}; char filename[FILE_PATH_SIZE] = {0};
// parse command line // parse command line
param_getstr(Cmd, 0, type, sizeof(type)); param_getstr(Cmd, 0, type, sizeof(type));
param_getstr(Cmd, 1, filename, sizeof(filename)); param_getstr(Cmd, 1, filename, sizeof(filename));
@ -579,7 +579,7 @@ int ScTraceList(const char *Cmd) {
loadFromFile = true; loadFromFile = true;
} }
} }
if ((loadFromFile || saveToFile) && strlen(filename) == 0) { if ((loadFromFile || saveToFile) && strlen(filename) == 0) {
errors = true; errors = true;
} }
@ -587,7 +587,7 @@ int ScTraceList(const char *Cmd) {
if (loadFromFile && saveToFile) { if (loadFromFile && saveToFile) {
errors = true; errors = true;
} }
if (errors) { if (errors) {
PrintAndLog("List or save protocol data."); PrintAndLog("List or save protocol data.");
PrintAndLog("Usage: sc list [l <filename>]"); PrintAndLog("Usage: sc list [l <filename>]");
@ -604,9 +604,9 @@ int ScTraceList(const char *Cmd) {
uint8_t *trace; uint8_t *trace;
uint32_t tracepos = 0; uint32_t tracepos = 0;
uint32_t traceLen = 0; uint32_t traceLen = 0;
if (loadFromFile) { if (loadFromFile) {
#define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions
FILE *tracefile = NULL; FILE *tracefile = NULL;
size_t bytes_read; size_t bytes_read;
trace = malloc(TRACE_CHUNK_SIZE); trace = malloc(TRACE_CHUNK_SIZE);

View file

@ -68,6 +68,7 @@ typedef struct{
#define CMD_SMART_UPGRADE 0x0141 #define CMD_SMART_UPGRADE 0x0141
#define CMD_SMART_UPLOAD 0x0142 #define CMD_SMART_UPLOAD 0x0142
#define CMD_SMART_ATR 0x0143 #define CMD_SMART_ATR 0x0143
// CMD_SMART_SETBAUD is unused for now
#define CMD_SMART_SETBAUD 0x0144 #define CMD_SMART_SETBAUD 0x0144
#define CMD_SMART_SETCLOCK 0x0145 #define CMD_SMART_SETCLOCK 0x0145