1 //-----------------------------------------------------------------------------
3 // Edits by Iceman, July 2018
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // The main i2c code, for communications with smart card module
10 //-----------------------------------------------------------------------------
13 #include "proxmark3_arm.h"
21 #define GPIO_RST AT91C_PIO_PA1
22 #define GPIO_SCL AT91C_PIO_PA5
23 #define GPIO_SDA AT91C_PIO_PA7
25 #define SCL_H HIGH(GPIO_SCL)
26 #define SCL_L LOW(GPIO_SCL)
27 #define SDA_H HIGH(GPIO_SDA)
28 #define SDA_L LOW(GPIO_SDA)
30 #define SCL_read ((AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL) == GPIO_SCL)
31 #define SDA_read ((AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA) == GPIO_SDA)
33 #define I2C_ERROR "I2C_WaitAck Error"
35 // Direct use the loop to delay. 6 instructions loop, Masterclock 48MHz,
36 // delay=1 is about 200kbps
38 // I2CSpinDelayClk(4) = 12.31us
39 // I2CSpinDelayClk(1) = 3.07us
40 static volatile uint32_t c
;
41 static void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay
) {
42 for (c
= delay
* 2; c
; c
--) {};
45 #define I2C_DELAY_1CLK I2CSpinDelayClk(1)
46 #define I2C_DELAY_2CLK I2CSpinDelayClk(2)
47 #define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
49 #define ISO7618_MAX_FRAME 255
51 // try i2c bus recovery at 100kHz = 5us high, 5us low
52 void I2C_recovery(void) {
54 DbpString("Performing i2c bus recovery");
60 //9nth cycle acts as NACK
61 for (int i
= 0; i
< 10; i
++) {
68 //a STOP signal (SDA from low to high while CLK is high)
77 bool isok
= (SCL_read
&& SDA_read
);
79 DbpString("I2C bus recovery error: SDA still LOW");
81 DbpString("I2C bus recovery error: SCL still LOW");
83 DbpString("I2C bus recovery complete");
87 // Configure reset pin, close up pull up, push-pull output, default high
88 AT91C_BASE_PIOA
->PIO_PPUDR
= GPIO_RST
;
89 AT91C_BASE_PIOA
->PIO_MDDR
= GPIO_RST
;
91 // Configure I2C pin, open up, open leakage
92 AT91C_BASE_PIOA
->PIO_PPUER
|= (GPIO_SCL
| GPIO_SDA
);
93 AT91C_BASE_PIOA
->PIO_MDER
|= (GPIO_SCL
| GPIO_SDA
);
95 // default three lines all pull up
96 AT91C_BASE_PIOA
->PIO_SODR
|= (GPIO_SCL
| GPIO_SDA
| GPIO_RST
);
98 AT91C_BASE_PIOA
->PIO_OER
|= (GPIO_SCL
| GPIO_SDA
| GPIO_RST
);
99 AT91C_BASE_PIOA
->PIO_PER
|= (GPIO_SCL
| GPIO_SDA
| GPIO_RST
);
101 bool isok
= (SCL_read
&& SDA_read
);
106 // set the reset state
107 void I2C_SetResetStatus(uint8_t LineRST
, uint8_t LineSCK
, uint8_t LineSDA
) {
124 // Reset the SIM_Adapter, then enter the main program
125 // Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
126 void I2C_Reset_EnterMainProgram(void) {
129 I2C_SetResetStatus(0, 0, 0);
131 I2C_SetResetStatus(1, 0, 0);
133 I2C_SetResetStatus(1, 1, 1);
137 // Reset the SIM_Adapter, then enter the bootloader program
138 // Reserve for firmware update.
139 void I2C_Reset_EnterBootloader(void) {
142 I2C_SetResetStatus(0, 1, 1);
144 I2C_SetResetStatus(1, 1, 1);
148 // Wait for the clock to go High.
149 static bool WaitSCL_H_delay(uint32_t delay
) {
159 // 5000 * 3.07us = 15350us. 15.35ms
160 // 15000 * 3.07us = 46050us. 46.05ms
161 static bool WaitSCL_H(void) {
162 return WaitSCL_H_delay(15000);
165 static bool WaitSCL_L_delay(uint32_t delay
) {
174 // 5000 * 3.07us = 15350us. 15.35ms
175 static bool WaitSCL_L(void) {
176 return WaitSCL_L_delay(15000);
179 // Wait max 1800ms or until SCL goes LOW.
180 // It timeout reading response from card
181 // Which ever comes first
182 static bool WaitSCL_L_timeout(void) {
183 volatile uint32_t delay
= 1700;
194 static bool I2C_Start(void) {
201 if (!WaitSCL_H()) return false;
205 if (!SCL_read
) return false;
206 if (!SDA_read
) return false;
213 static bool I2C_WaitForSim(void) {
215 // wait for data from card
216 if (!WaitSCL_L_timeout())
219 // 8051 speaks with smart card.
220 // 1000*50*3.07 = 153.5ms
221 // 1byte transfer == 1ms with max frame being 256bytes
222 return WaitSCL_H_delay(1000 * 300);
226 static void I2C_Stop(void) {
233 if (!WaitSCL_H()) return;
242 static void I2C_Ack(void) {
249 if (!WaitSCL_H()) return;
255 static void I2C_NoAck(void) {
262 if (!WaitSCL_H()) return;
267 static bool I2C_WaitAck(void) {
286 static void I2C_SendByte(uint8_t data
) {
312 static int16_t I2C_ReadByte(void) {
313 uint8_t bits
= 8, b
= 0;
319 if (!WaitSCL_L()) return -2;
324 if (!WaitSCL_H()) return -1;
334 // Sends one byte ( command to be written, SlaveDevice address)
335 bool I2C_WriteCmd(uint8_t device_cmd
, uint8_t device_address
) {
341 I2C_SendByte(device_address
& 0xFE);
345 I2C_SendByte(device_cmd
);
354 if (DBGLEVEL
> 3) DbpString(I2C_ERROR
);
360 // Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ).
361 bool I2C_WriteByte(uint8_t data
, uint8_t device_cmd
, uint8_t device_address
) {
367 I2C_SendByte(device_address
& 0xFE);
371 I2C_SendByte(device_cmd
);
384 if (DBGLEVEL
> 3) DbpString(I2C_ERROR
);
390 //Sends array of data (Array, length, command to be written , SlaveDevice address ).
391 // len = uint8 (max buffer to write 256bytes)
392 bool I2C_BufferWrite(uint8_t *data
, uint8_t len
, uint8_t device_cmd
, uint8_t device_address
) {
398 I2C_SendByte(device_address
& 0xFE);
402 I2C_SendByte(device_cmd
);
422 if (DBGLEVEL
> 3) DbpString(I2C_ERROR
);
428 // read one array of data (Data array, Readout length, command to be written , SlaveDevice address ).
429 // len = uint8 (max buffer to read 256bytes)
430 int16_t I2C_BufferRead(uint8_t *data
, uint8_t len
, uint8_t device_cmd
, uint8_t device_address
) {
432 if (!data
|| len
== 0)
435 // extra wait 500us (514us measured)
436 // 200us (xx measured)
440 uint16_t readcount
= 0;
446 // 0xB0 / 0xC0 == i2c write
447 I2C_SendByte(device_address
& 0xFE);
451 I2C_SendByte(device_cmd
);
455 // 0xB1 / 0xC1 == i2c read
457 I2C_SendByte(device_address
| 1);
466 if (DBGLEVEL
> 3) DbpString(I2C_ERROR
);
472 int16_t tmp
= I2C_ReadByte();
476 *data
= (uint8_t)tmp
& 0xFF;
480 // The first byte in response is the message length
481 if (!readcount
&& (len
> *data
)) {
488 // acknowledgements. After last byte send NACK.
497 // return bytecount - first byte (which is length byte)
501 int16_t I2C_ReadFW(uint8_t *data
, uint8_t len
, uint8_t msb
, uint8_t lsb
, uint8_t device_address
) {
502 //START, 0xB0, 0x00, 0x00, START, 0xB1, xx, yy, zz, ......, STOP
504 uint8_t readcount
= 0;
511 // 0xB0 / 0xC0 i2c write
512 I2C_SendByte(device_address
& 0xFE);
524 // 0xB1 / 0xC1 i2c read
526 I2C_SendByte(device_address
| 1);
535 if (DBGLEVEL
> 3) DbpString(I2C_ERROR
);
542 int16_t tmp
= I2C_ReadByte();
546 *data
= (uint8_t)tmp
& 0xFF;
552 // acknowledgements. After last byte send NACK.
563 bool I2C_WriteFW(uint8_t *data
, uint8_t len
, uint8_t msb
, uint8_t lsb
, uint8_t device_address
) {
564 //START, 0xB0, 0x00, 0x00, xx, yy, zz, ......, STOP
572 I2C_SendByte(device_address
& 0xFE);
599 if (DBGLEVEL
> 3) DbpString(I2C_ERROR
);
605 void I2C_print_status(void) {
606 DbpString(_CYAN_("Smart card module (ISO 7816)"));
608 if (I2C_get_version(&maj
, &min
) == PM3_SUCCESS
)
609 Dbprintf(" version................. " _YELLOW_("v%x.%02d"), maj
, min
);
611 DbpString(" version................. " _RED_("FAILED"));
614 int I2C_get_version(uint8_t *maj
, uint8_t *min
) {
615 uint8_t resp
[] = {0, 0, 0, 0};
616 I2C_Reset_EnterMainProgram();
617 uint8_t len
= I2C_BufferRead(resp
, sizeof(resp
), I2C_DEVICE_CMD_GETVERSION
, I2C_DEVICE_ADDRESS_MAIN
);
623 return PM3_EDEVNOTSUPP
;
626 // Will read response from smart card module, retries 3 times to get the data.
627 bool sc_rx_bytes(uint8_t *dest
, uint8_t *destlen
) {
635 len
= I2C_BufferRead(dest
, *destlen
, I2C_DEVICE_CMD_READ
, I2C_DEVICE_ADDRESS_MAIN
);
641 } else if (len
== 1) {
643 } else if (len
<= 0) {
652 *destlen
= (uint8_t)len
& 0xFF;
656 bool GetATR(smart_card_atr_t
*card_ptr
, bool verbose
) {
658 if (card_ptr
== NULL
)
661 card_ptr
->atr_len
= 0;
662 memset(card_ptr
->atr
, 0, sizeof(card_ptr
->atr
));
665 // start [C0 01] stop start C1 len aa bb cc stop]
666 I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR
, I2C_DEVICE_ADDRESS_MAIN
);
668 //wait for sim card to answer.
669 // 1byte = 1ms , max frame 256bytes. Should wait 256ms atleast just in case.
670 if (I2C_WaitForSim() == false)
673 // read bytes from module
674 uint8_t len
= sizeof(card_ptr
->atr
);
675 if (sc_rx_bytes(card_ptr
->atr
, &len
) == false)
679 if ((card_ptr
->atr
[1] & 0x10) == 0x10) pos_td
++;
680 if ((card_ptr
->atr
[1] & 0x20) == 0x20) pos_td
++;
681 if ((card_ptr
->atr
[1] & 0x40) == 0x40) pos_td
++;
683 // T0 indicate presence T=0 vs T=1. T=1 has checksum TCK
684 if ((card_ptr
->atr
[1] & 0x80) == 0x80) {
688 // 1 == T1 , presence of checksum TCK
689 if ((card_ptr
->atr
[pos_td
] & 0x01) == 0x01) {
692 // xor property. will be zero when xored with chksum.
693 for (uint8_t i
= 1; i
< len
; ++i
)
694 chksum
^= card_ptr
->atr
[i
];
697 if (DBGLEVEL
> 2) DbpString("Wrong ATR checksum");
702 card_ptr
->atr_len
= len
;
704 LogTrace(card_ptr
->atr
, card_ptr
->atr_len
, 0, 0, NULL
, false);
710 void SmartCardAtr(void) {
713 I2C_Reset_EnterMainProgram();
714 smart_card_atr_t card
;
715 if (GetATR(&card
, true)) {
716 reply_ng(CMD_SMART_ATR
, PM3_SUCCESS
, (uint8_t *)&card
, sizeof(smart_card_atr_t
));
718 reply_ng(CMD_SMART_ATR
, PM3_ETIMEOUT
, NULL
, 0);
725 void SmartCardRaw(smart_card_raw_t
*p
) {
729 uint8_t *resp
= BigBuf_malloc(ISO7618_MAX_FRAME
);
730 // check if alloacted...
731 smartcard_command_t flags
= p
->flags
;
733 if ((flags
& SC_CLEARLOG
) == SC_CLEARLOG
)
736 if ((flags
& SC_LOG
) == SC_LOG
)
741 if ((flags
& SC_CONNECT
) == SC_CONNECT
) {
743 I2C_Reset_EnterMainProgram();
745 if ((flags
& SC_SELECT
) == SC_SELECT
) {
746 smart_card_atr_t card
;
747 bool gotATR
= GetATR(&card
, true);
748 //reply_old(CMD_ACK, gotATR, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
749 if (gotATR
== false) {
750 reply_ng(CMD_SMART_RAW
, PM3_ESOFT
, NULL
, 0);
756 if ((flags
& SC_RAW
) || (flags
& SC_RAW_T0
)) {
758 LogTrace(p
->data
, p
->len
, 0, 0, NULL
, true);
760 bool res
= I2C_BufferWrite(
763 ((flags
& SC_RAW_T0
) ? I2C_DEVICE_CMD_SEND_T0
: I2C_DEVICE_CMD_SEND
),
764 I2C_DEVICE_ADDRESS_MAIN
766 if (res
== false && DBGLEVEL
> 3) {
767 DbpString(I2C_ERROR
);
768 reply_ng(CMD_SMART_RAW
, PM3_ESOFT
, NULL
, 0);
772 // read bytes from module
773 len
= ISO7618_MAX_FRAME
;
774 res
= sc_rx_bytes(resp
, &len
);
776 LogTrace(resp
, len
, 0, 0, NULL
, false);
782 reply_ng(CMD_SMART_RAW
, PM3_SUCCESS
, resp
, len
);
790 void SmartCardUpgrade(uint64_t arg0
) {
794 #define I2C_BLOCK_SIZE 128
795 // write. Sector0, with 11,22,33,44
796 // erase is 128bytes, and takes 50ms to execute
798 I2C_Reset_EnterBootloader();
801 uint16_t length
= arg0
, pos
= 0;
802 uint8_t *fwdata
= BigBuf_get_addr();
803 uint8_t *verfiydata
= BigBuf_malloc(I2C_BLOCK_SIZE
);
807 uint8_t msb
= (pos
>> 8) & 0xFF;
808 uint8_t lsb
= pos
& 0xFF;
810 Dbprintf("FW %02X%02X", msb
, lsb
);
812 size_t size
= MIN(I2C_BLOCK_SIZE
, length
);
815 int16_t res
= I2C_WriteFW(fwdata
+ pos
, size
, msb
, lsb
, I2C_DEVICE_ADDRESS_BOOT
);
817 DbpString("Writing failed");
822 // writing takes time.
826 res
= I2C_ReadFW(verfiydata
, size
, msb
, lsb
, I2C_DEVICE_ADDRESS_BOOT
);
828 DbpString("Reading back failed");
834 if (0 != memcmp(fwdata
+ pos
, verfiydata
, size
)) {
835 DbpString("not equal data");
844 reply_ng(CMD_SMART_UPGRADE
, (isOK
) ? PM3_SUCCESS
: PM3_ESOFT
, NULL
, 0);
849 void SmartCardSetBaud(uint64_t arg0
) {
852 void SmartCardSetClock(uint64_t arg0
) {
855 I2C_Reset_EnterMainProgram();
857 // start [C0 05 xx] stop
858 I2C_WriteByte(arg0
, I2C_DEVICE_CMD_SIM_CLC
, I2C_DEVICE_ADDRESS_MAIN
);
859 reply_ng(CMD_SMART_SETCLOCK
, PM3_SUCCESS
, NULL
, 0);