From 49d92e3c7e632b3d068b91f9df9b1dfe0e2687bf Mon Sep 17 00:00:00 2001 From: marshmellow42 Date: Tue, 7 Aug 2018 12:28:08 -0400 Subject: [PATCH] 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) --- armsrc/appmain.c | 13 ++-- armsrc/i2c.c | 167 ++++++++++++++++++++++-------------------- armsrc/i2c.h | 5 +- client/cmdsmartcard.c | 28 +++---- include/usb_cmd.h | 1 + 5 files changed, 109 insertions(+), 105 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 6385cafc..4034788a 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -361,12 +361,15 @@ void SendStatus(void) { BigBuf_print_status(); Fpga_print_status(); +#ifdef WITH_SMARTCARD + I2C_print_status(); +#endif printConfig(); //LF Sampling config printUSBSpeed(); Dbprintf("Various"); - Dbprintf(" MF_DBGLEVEL......%d", MF_DBGLEVEL); - Dbprintf(" ToSendMax........%d",ToSendMax); - Dbprintf(" ToSendBit........%d",ToSendBit); + Dbprintf(" MF_DBGLEVEL........%d", MF_DBGLEVEL); + Dbprintf(" ToSendMax..........%d", ToSendMax); + Dbprintf(" ToSendBit..........%d", ToSendBit); cmd_send(CMD_ACK,1,0,0,0,0); } @@ -1262,10 +1265,6 @@ void UsbPacketReceived(uint8_t *packet, int len) SmartCardAtr(); break; } - case CMD_SMART_SETBAUD:{ - SmartCardSetBaud(c->arg[0]); - break; - } case CMD_SMART_SETCLOCK:{ SmartCardSetClock(c->arg[0]); break; diff --git a/armsrc/i2c.c b/armsrc/i2c.c index 8fb9f397..721b4b2e 100644 --- a/armsrc/i2c.c +++ b/armsrc/i2c.c @@ -13,17 +13,17 @@ #include "string.h" //for memset memcmp // 定义连接引脚 -#define GPIO_RST AT91C_PIO_PA1 -#define GPIO_SCL AT91C_PIO_PA5 -#define GPIO_SDA AT91C_PIO_PA7 +#define GPIO_RST AT91C_PIO_PA1 +#define GPIO_SCL AT91C_PIO_PA5 +#define GPIO_SDA AT91C_PIO_PA7 -#define SCL_H HIGH(GPIO_SCL) -#define SCL_L LOW(GPIO_SCL) -#define SDA_H HIGH(GPIO_SDA) -#define SDA_L LOW(GPIO_SDA) +#define SCL_H HIGH(GPIO_SCL) +#define SCL_L LOW(GPIO_SCL) +#define SDA_H HIGH(GPIO_SDA) +#define SDA_L LOW(GPIO_SDA) -#define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL) -#define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA) +#define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL) +#define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA) #define I2C_ERROR "I2C_WaitAck Error" @@ -36,11 +36,11 @@ volatile unsigned long c; void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) { for (c = delay * 2; c; c--) {}; } - -// 通讯延迟函数 communication delay function -#define I2C_DELAY_1CLK I2CSpinDelayClk(1) -#define I2C_DELAY_2CLK I2CSpinDelayClk(2) -#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x)) + +// 通讯延迟函数 communication delay function +#define I2C_DELAY_1CLK I2CSpinDelayClk(1) +#define I2C_DELAY_2CLK I2CSpinDelayClk(2) +#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x)) #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 AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST; AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST; - + // 配置 I2C 引脚,开启上拉,开漏输出 // 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); // 默认三根线全部拉高 @@ -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 // 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) { - I2C_SetResetStatus(0, 0, 0); // 拉低复位线 + I2C_SetResetStatus(0, 0, 0); // 拉低复位线 SpinDelay(30); - I2C_SetResetStatus(1, 0, 0); // 解除复位 + I2C_SetResetStatus(1, 0, 0); // 解除复位 SpinDelay(30); - I2C_SetResetStatus(1, 1, 1); // 拉高数据线 + I2C_SetResetStatus(1, 1, 1); // 拉高数据线 SpinDelay(10); } @@ -102,16 +102,16 @@ void I2C_Reset_EnterMainProgram(void) { // Reset the SIM_Adapter, then enter the bootloader program // Reserve:For firmware update. void I2C_Reset_EnterBootloader(void) { - I2C_SetResetStatus(0, 1, 1); // 拉低复位线 + I2C_SetResetStatus(0, 1, 1); // 拉低复位线 SpinDelay(100); - I2C_SetResetStatus(1, 1, 1); // 解除复位 + I2C_SetResetStatus(1, 1, 1); // 解除复位 SpinDelay(10); } // 等待时钟变高 // Wait for the clock to go High. bool WaitSCL_H_delay(uint32_t delay) { - while (delay--) { + while (delay--) { if (SCL_read) { return true; } @@ -127,27 +127,27 @@ bool WaitSCL_H(void) { // Wait max 300ms or until SCL goes LOW. // Which ever comes first -bool WaitSCL_L_300ms(void){ +bool WaitSCL_L_300ms(void) { volatile uint16_t delay = 300; - while ( delay-- ) { + while ( delay-- ) { // exit on SCL LOW if (!SCL_read) return true; - + SpinDelay(1); } return (delay == 0); } bool I2C_Start(void) { - + I2C_DELAY_XCLK(4); SDA_H; I2C_DELAY_1CLK; - SCL_H; + SCL_H; if (!WaitSCL_H()) return false; I2C_DELAY_2CLK; - + if (!SCL_read) return false; if (!SDA_read) return false; @@ -211,17 +211,17 @@ bool I2C_WaitAck(void) { return true; } -void I2C_SendByte(uint8_t data) { +void I2C_SendByte(uint8_t data) { uint8_t i = 8; while (i--) { SCL_L; I2C_DELAY_1CLK; - + if (data & 0x80) SDA_H; else SDA_L; - + data <<= 1; I2C_DELAY_1CLK; @@ -238,7 +238,7 @@ uint8_t I2C_ReadByte(void) { uint8_t i = 8, b = 0; SDA_H; - while (i--) { + while (i--) { b <<= 1; SCL_L; I2C_DELAY_2CLK; SCL_H; @@ -256,7 +256,7 @@ uint8_t I2C_ReadByte(void) { // Sends one byte ( command to be written, SlaveDevice address) bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) { bool bBreak = true; - do { + do { if (!I2C_Start()) return false; //[C0] @@ -272,7 +272,7 @@ bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) { } while (false); I2C_Stop(); - if (bBreak) { + if (bBreak) { if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); 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 ). bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) { bool bBreak = true; - do { + do { if (!I2C_Start()) return false; @@ -303,7 +303,7 @@ bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) { } while (false); I2C_Stop(); - if (bBreak) { + if (bBreak) { if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); return false; } @@ -334,7 +334,7 @@ bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t dev break; len--; - data++; + data++; } 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); I2C_Stop(); - if (bBreak) { + if (bBreak) { if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); return false; } - return true; + return true; } // 读出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) // 200us (xx measured) - SpinDelayUs(200); + SpinDelayUs(200); bool bBreak = true; uint8_t readcount = 0; - + do { if (!I2C_Start()) 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); if (!I2C_WaitAck()) break; - + // 0xB1 / 0xC1 == i2c read I2C_Start(); 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; } while (false); - if (bBreak) { + if (bBreak) { I2C_Stop(); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); 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)) { len = *data; } else { - data++; + data++; } readcount++; - + // acknowledgements. After last byte send NACK. if (len == 0) I2C_NoAck(); else I2C_Ack(); } - + I2C_Stop(); // return bytecount - first byte (which is length byte) 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; } while (false); - if (bBreak) { + if (bBreak) { I2C_Stop(); if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); 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 I2C_Ack(); } - + I2C_Stop(); 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); if (!I2C_WaitAck()) break; - + // msb I2C_SendByte(msb); 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; len--; - data++; + data++; } 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); I2C_Stop(); - if (bBreak) { + if (bBreak) { if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR); return false; } - return true; + return true; } void I2C_print_status(void) { @@ -529,19 +529,19 @@ void I2C_print_status(void) { I2C_Reset_EnterMainProgram(); uint8_t len = I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN); if ( len > 0 ) - Dbprintf(" version.................v%x.%02x", resp[0], resp[1]); + Dbprintf(" version.................v%x.%02x", resp[0], resp[1]); else - DbpString(" version.................FAILED"); + DbpString(" version.................FAILED"); } bool GetATR(smart_card_atr_t *card_ptr) { - + // clear if ( card_ptr ) { card_ptr->atr_len = 0; memset(card_ptr->atr, 0, sizeof(card_ptr->atr)); } - + // Send ATR // start [C0 01] stop start C1 len aa bb cc stop] 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. if (!I2C_WaitForSim()) return false; - + // read answer uint8_t len = I2C_BufferRead(card_ptr->atr, sizeof(card_ptr->atr), I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); - + if ( len == 0 ) return false; @@ -584,6 +584,7 @@ void SmartCardAtr(void) { LED_D_ON(); clear_trace(); set_tracing(true); + I2C_init(); I2C_Reset_EnterMainProgram(); bool isOK = GetATR( &card ); 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 *resp = BigBuf_malloc(ISO7618_MAX_FRAME); smartcard_command_t flags = arg0; - + if ((flags & SC_CONNECT)) clear_trace(); set_tracing(true); if ((flags & SC_CONNECT)) { - + + I2C_init(); I2C_Reset_EnterMainProgram(); - + if ( !(flags & SC_NO_SELECT) ) { smart_card_atr_t card; bool gotATR = GetATR( &card ); @@ -618,9 +620,9 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) { } if ((flags & SC_RAW)) { - + LogTrace(data, arg1, 0, 0, NULL, true); - + // Send raw bytes // asBytes = A0 A4 00 00 02 // 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); LogTrace(resp, len, 0, 0, NULL, false); } -OUT: +OUT: cmd_send(CMD_ACK, len, 0, 0, resp, len); set_tracing(false); LEDsoff(); @@ -645,8 +647,9 @@ void SmartCardUpgrade(uint64_t arg0) { #define I2C_BLOCK_SIZE 128 // write. Sector0, with 11,22,33,44 // erase is 128bytes, and takes 50ms to execute - - I2C_Reset_EnterBootloader(); + + I2C_init(); + I2C_Reset_EnterBootloader(); bool isOK = true; uint8_t res = 0; @@ -654,16 +657,16 @@ void SmartCardUpgrade(uint64_t arg0) { uint16_t pos = 0; uint8_t *fwdata = BigBuf_get_addr(); uint8_t *verfiydata = BigBuf_malloc(I2C_BLOCK_SIZE); - + while (length) { - + uint8_t msb = (pos >> 8) & 0xFF; uint8_t lsb = pos & 0xFF; - + Dbprintf("FW %02X%02X", msb, lsb); size_t size = MIN(I2C_BLOCK_SIZE, length); - + // write res = I2C_WriteFW(fwdata+pos, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT); if ( !res ) { @@ -671,7 +674,7 @@ void SmartCardUpgrade(uint64_t arg0) { isOK = false; break; } - + // writing takes time. SpinDelay(50); @@ -679,36 +682,38 @@ void SmartCardUpgrade(uint64_t arg0) { res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT); if ( res == 0) { DbpString("Reading back failed"); - isOK = false; + isOK = false; break; } - + // cmp if ( 0 != memcmp(fwdata+pos, verfiydata, size)) { DbpString("not equal data"); - isOK = false; + isOK = false; break; } - + length -= size; pos += size; - } + } cmd_send(CMD_ACK, isOK, pos, 0, 0, 0); LED_C_OFF(); } -void SmartCardSetBaud(uint64_t arg0) { -} +// unfinished (or not needed?) +//void SmartCardSetBaud(uint64_t arg0) { +//} void SmartCardSetClock(uint64_t arg0) { LED_D_ON(); - set_tracing(true); - I2C_Reset_EnterMainProgram(); + set_tracing(true); + I2C_init(); + I2C_Reset_EnterMainProgram(); // Send SIM CLC // start [C0 05 xx] stop I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN); - + cmd_send(CMD_ACK, 1, 0, 0, 0, 0); set_tracing(false); LEDsoff(); diff --git a/armsrc/i2c.h b/armsrc/i2c.h index d3ac1361..4c5c5228 100644 --- a/armsrc/i2c.h +++ b/armsrc/i2c.h @@ -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); 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); -// generice functions +// generic functions void SmartCardAtr(void); void SmartCardRaw(uint64_t arg0, uint64_t arg1, uint8_t *data); void SmartCardUpgrade(uint64_t arg0); -void SmartCardSetBaud(uint64_t arg0); +//void SmartCardSetBaud(uint64_t arg0); void SmartCardSetClock(uint64_t arg0); void I2C_print_status(void); #endif diff --git a/client/cmdsmartcard.c b/client/cmdsmartcard.c index 1b7ef138..b2a5705d 100644 --- a/client/cmdsmartcard.c +++ b/client/cmdsmartcard.c @@ -43,7 +43,7 @@ int usage_sm_info(void) { PrintAndLog(" s : silent (no messages)"); PrintAndLog(""); PrintAndLog("Examples:"); - PrintAndLog(" sc info"); + PrintAndLog(" sc info"); return 0; } int usage_sm_upgrade(void) { @@ -64,7 +64,7 @@ int usage_sm_setclock(void) { PrintAndLog(" c <> : clockspeed (0 = 16mhz, 1=8mhz, 2=4mhz) "); PrintAndLog(""); PrintAndLog("Examples:"); - PrintAndLog(" sc setclock c 2"); + PrintAndLog(" sc setclock c 2"); return 0; } @@ -72,11 +72,11 @@ int CmdSmartRaw(const char *Cmd) { int hexlen = 0; bool active = false; - bool active_select = false; + bool active_select = false; uint8_t cmdp = 0; bool errors = false, reply = true, decodeTLV = false, breakloop = false; uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; - + while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { switch (tolower(param_getchar(Cmd, cmdp))) { case 'h': return usage_sm_raw(); @@ -123,11 +123,11 @@ int CmdSmartRaw(const char *Cmd) { } //Validations - if (errors || cmdp == 0 ) return usage_sm_raw(); + if (errors || cmdp == 0 ) return usage_sm_raw(); // arg0 = RFU flags // arg1 = length - UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}}; + UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}}; if (active || active_select) { c.arg[0] |= SC_CONNECT; @@ -249,20 +249,20 @@ int CmdSmartUpgrade(const char *Cmd) { uint32_t bytes_remaining = bytes_read; 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}}; // Fill usb bytes with 0xFF memset(c.d.asBytes, 0xFF, USB_CMD_DATA_SIZE); memcpy(c.d.asBytes, dump + bytes_sent, bytes_in_packet); clearCommandBuffer(); - SendCommand(&c); + SendCommand(&c); if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000) ) { PrintAndLog("timeout while waiting for reply."); free(dump); return 1; } - + bytes_remaining -= bytes_in_packet; bytes_sent += bytes_in_packet; printf("."); fflush(stdout); @@ -562,7 +562,7 @@ int ScTraceList(const char *Cmd) { bool saveToFile = false; char type[5] = {0}; char filename[FILE_PATH_SIZE] = {0}; - + // parse command line param_getstr(Cmd, 0, type, sizeof(type)); param_getstr(Cmd, 1, filename, sizeof(filename)); @@ -579,7 +579,7 @@ int ScTraceList(const char *Cmd) { loadFromFile = true; } } - + if ((loadFromFile || saveToFile) && strlen(filename) == 0) { errors = true; } @@ -587,7 +587,7 @@ int ScTraceList(const char *Cmd) { if (loadFromFile && saveToFile) { errors = true; } - + if (errors) { PrintAndLog("List or save protocol data."); PrintAndLog("Usage: sc list [l ]"); @@ -604,9 +604,9 @@ int ScTraceList(const char *Cmd) { uint8_t *trace; uint32_t tracepos = 0; uint32_t traceLen = 0; - + 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; size_t bytes_read; trace = malloc(TRACE_CHUNK_SIZE); diff --git a/include/usb_cmd.h b/include/usb_cmd.h index e10d7aef..bdff7261 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -68,6 +68,7 @@ typedef struct{ #define CMD_SMART_UPGRADE 0x0141 #define CMD_SMART_UPLOAD 0x0142 #define CMD_SMART_ATR 0x0143 +// CMD_SMART_SETBAUD is unused for now #define CMD_SMART_SETBAUD 0x0144 #define CMD_SMART_SETCLOCK 0x0145