]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/i2c.c
0560d306b508d1c6fb648da9ed75761c7e666371
[proxmark3-svn] / armsrc / i2c.c
1 //-----------------------------------------------------------------------------
2 // Willok, June 2018
3 // Edits by Iceman, July 2018
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // The main i2c code, for communications with smart card module
10 //-----------------------------------------------------------------------------
11
12 #include "i2c.h"
13
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include "string.h" //for memset memcmp
17 #include "proxmark3.h"
18 #include "mifareutil.h" // for MF_DBGLEVEL
19 #include "BigBuf.h"
20 #include "apps.h"
21
22 #ifdef WITH_SMARTCARD
23 #include "smartcard.h"
24 #endif
25
26
27 #define GPIO_RST AT91C_PIO_PA1
28 #define GPIO_SCL AT91C_PIO_PA5
29 #define GPIO_SDA AT91C_PIO_PA7
30
31 #define SCL_H HIGH(GPIO_SCL)
32 #define SCL_L LOW(GPIO_SCL)
33 #define SDA_H HIGH(GPIO_SDA)
34 #define SDA_L LOW(GPIO_SDA)
35
36 #define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL)
37 #define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA)
38
39 #define I2C_ERROR "I2C_WaitAck Error"
40
41 static volatile unsigned long c;
42
43 // Ö±½ÓʹÓÃÑ­»·À´ÑÓʱ£¬Ò»¸öÑ­»· 6 ÌõÖ¸Á48M£¬ Delay=1 ´ó¸ÅΪ 200kbps
44 // timer.
45 // I2CSpinDelayClk(4) = 12.31us
46 // I2CSpinDelayClk(1) = 3.07us
47 static void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
48 for (c = delay * 2; c; c--) {};
49 }
50
51 // communication delay functions
52 #define I2C_DELAY_1CLK I2CSpinDelayClk(1)
53 #define I2C_DELAY_2CLK I2CSpinDelayClk(2)
54 #define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
55
56 #define ISO7618_MAX_FRAME 255
57
58 // try i2c bus recovery at 100kHz = 5uS high, 5uS low
59 static void I2C_recovery(void) {
60
61 DbpString("Performing i2c bus recovery");
62
63 // reset I2C
64 SDA_H; SCL_H;
65
66 //9nth cycle acts as NACK
67 for (int i = 0; i < 10; i++) {
68 SCL_H; WaitUS(5);
69 SCL_L; WaitUS(5);
70 }
71
72 //a STOP signal (SDA from low to high while CLK is high)
73 SDA_L; WaitUS(5);
74 SCL_H; WaitUS(2);
75 SDA_H; WaitUS(2);
76
77 bool isok = (SCL_read && SDA_read);
78 if (!SDA_read)
79 DbpString("I2C bus recovery error: SDA still LOW");
80 if (!SCL_read)
81 DbpString("I2C bus recovery error: SCL still LOW");
82 if (isok)
83 DbpString("I2C bus recovery complete");
84 }
85
86 static void I2C_init(void) {
87 // Configure reset pin
88 AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST; // disable pull up resistor
89 AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST; // push-pull output (multidriver disabled)
90
91 // Configure SCL and SDA pins
92 AT91C_BASE_PIOA->PIO_PPUER |= (GPIO_SCL | GPIO_SDA); // enable pull up resistor
93 AT91C_BASE_PIOA->PIO_MDER |= (GPIO_SCL | GPIO_SDA); // open drain output (multidriver enabled) - requires external pull up resistor
94
95 // set all three outputs to high
96 AT91C_BASE_PIOA->PIO_SODR |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
97
98 // configure all three pins as output, controlled by PIOA
99 AT91C_BASE_PIOA->PIO_OER |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
100 AT91C_BASE_PIOA->PIO_PER |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
101
102 bool isok = (SCL_read && SDA_read);
103 if ( !isok )
104 I2C_recovery();
105
106 }
107
108 // set the reset state
109 static void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA) {
110 if (LineRST)
111 HIGH(GPIO_RST);
112 else
113 LOW(GPIO_RST);
114
115 if (LineSCK)
116 HIGH(GPIO_SCL);
117 else
118 LOW(GPIO_SCL);
119
120 if (LineSDA)
121 HIGH(GPIO_SDA);
122 else
123 LOW(GPIO_SDA);
124 }
125
126 // Reset the SIM_Adapter, then enter the main program
127 // Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
128 static void I2C_Reset_EnterMainProgram(void) {
129 StartTicks();
130 I2C_init();
131 I2C_SetResetStatus(0, 0, 0);
132 WaitMS(30);
133 I2C_SetResetStatus(1, 0, 0);
134 WaitMS(30);
135 I2C_SetResetStatus(1, 1, 1);
136 WaitMS(10);
137 }
138
139 // Wait for the clock to go High.
140 static bool WaitSCL_H_delay(uint32_t delay) {
141 while (delay--) {
142 if (SCL_read) {
143 return true;
144 }
145 I2C_DELAY_1CLK;
146 }
147 return false;
148 }
149
150 // 15000 * 3.07us = 46050us. 46.05ms
151 static bool WaitSCL_H(void) {
152 return WaitSCL_H_delay(15000);
153 }
154
155 bool WaitSCL_L_delay(uint32_t delay) {
156 while (delay--) {
157 if (!SCL_read) {
158 return true;
159 }
160 I2C_DELAY_1CLK;
161 }
162 return false;
163 }
164
165 bool WaitSCL_L(void) {
166 return WaitSCL_L_delay(15000);
167 }
168
169 static bool I2C_Start(void) {
170
171 I2C_DELAY_XCLK(4);
172 SDA_H; I2C_DELAY_1CLK;
173 SCL_H;
174 if (!WaitSCL_H()) return false;
175
176 I2C_DELAY_2CLK;
177
178 if (!SCL_read) return false;
179 if (!SDA_read) return false;
180
181 SDA_L; I2C_DELAY_2CLK;
182 return true;
183 }
184
185 // send i2c STOP
186 static void I2C_Stop(void) {
187 SCL_L; I2C_DELAY_2CLK;
188 SDA_L; I2C_DELAY_2CLK;
189 SCL_H; I2C_DELAY_2CLK;
190 if (!WaitSCL_H()) return;
191 SDA_H;
192 I2C_DELAY_XCLK(8);
193 }
194
195 static bool I2C_WaitAck(void) {
196 SCL_L; I2C_DELAY_1CLK;
197 SDA_H; I2C_DELAY_1CLK;
198 SCL_H;
199 if (!WaitSCL_H())
200 return false;
201
202 I2C_DELAY_2CLK;
203 I2C_DELAY_2CLK;
204 if (SDA_read) {
205 SCL_L;
206 return false;
207 }
208 SCL_L;
209 return true;
210 }
211
212 static void I2C_SendByte(uint8_t data) {
213 uint8_t bits = 8;
214
215 while (bits--) {
216 SCL_L;
217 I2C_DELAY_1CLK;
218
219 if (data & 0x80)
220 SDA_H;
221 else
222 SDA_L;
223
224 data <<= 1;
225
226 I2C_DELAY_1CLK;
227
228 SCL_H;
229 if (!WaitSCL_H())
230 return;
231
232 I2C_DELAY_2CLK;
233 }
234 SCL_L;
235 }
236
237 bool I2C_is_available(void) {
238 I2C_Reset_EnterMainProgram();
239 if (!I2C_Start()) // some other device is active on the bus
240 return true;
241 I2C_SendByte(I2C_DEVICE_ADDRESS_MAIN & 0xFE);
242 if (!I2C_WaitAck()) { // no response from smartcard reader
243 I2C_Stop();
244 return false;
245 }
246 I2C_Stop();
247 return true;
248 }
249
250 #ifdef WITH_SMARTCARD
251 // Reset the SIM_Adapter, then enter the bootloader program
252 // Reserve£ºFor firmware update.
253 static void I2C_Reset_EnterBootloader(void) {
254 I2C_SetResetStatus(0, 1, 1);
255 WaitMS(100);
256 I2C_SetResetStatus(1, 1, 1);
257 WaitMS(10);
258 }
259
260 // Wait max 300ms or until SCL goes LOW.
261 // Which ever comes first
262 static bool WaitSCL_L_300ms(void) {
263 volatile uint16_t delay = 310;
264 while ( delay-- ) {
265 // exit on SCL LOW
266 if (!SCL_read)
267 return true;
268
269 WaitMS(1);
270 }
271 return (delay == 0);
272 }
273
274 static bool I2C_WaitForSim() {
275 // variable delay here.
276 if (!WaitSCL_L_300ms())
277 return false;
278
279 // 8051 speaks with smart card.
280 // 1000*50*3.07 = 153.5ms
281 // 1byte transfer == 1ms with max frame being 256bytes
282 if (!WaitSCL_H_delay(10 * 1000 * 50))
283 return false;
284
285 return true;
286 }
287
288 // Send i2c ACK
289 static void I2C_Ack(void) {
290 SCL_L; I2C_DELAY_2CLK;
291 SDA_L; I2C_DELAY_2CLK;
292 SCL_H; I2C_DELAY_2CLK;
293 if (!WaitSCL_H()) return;
294 SCL_L; I2C_DELAY_2CLK;
295 }
296
297 // Send i2c NACK
298 static void I2C_NoAck(void) {
299 SCL_L; I2C_DELAY_2CLK;
300 SDA_H; I2C_DELAY_2CLK;
301 SCL_H; I2C_DELAY_2CLK;
302 if (!WaitSCL_H()) return;
303 SCL_L; I2C_DELAY_2CLK;
304 }
305
306 static int16_t I2C_ReadByte(void) {
307 uint8_t bits = 8, b = 0;
308
309 SDA_H;
310 while (bits--) {
311 b <<= 1;
312 SCL_L;
313 if (!WaitSCL_L()) return -2;
314
315 I2C_DELAY_1CLK;
316
317 SCL_H;
318 if (!WaitSCL_H()) return -1;
319
320 I2C_DELAY_1CLK;
321 if (SDA_read)
322 b |= 0x01;
323 }
324 SCL_L;
325 return b;
326 }
327
328 // Sends one byte ( command to be written, SlaveDevice address)
329 static bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
330 bool bBreak = true;
331 do {
332 if (!I2C_Start())
333 return false;
334
335 I2C_SendByte(device_address & 0xFE);
336 if (!I2C_WaitAck())
337 break;
338
339 I2C_SendByte(device_cmd);
340 if (!I2C_WaitAck())
341 break;
342
343 bBreak = false;
344 } while (false);
345
346 I2C_Stop();
347 if (bBreak) {
348 if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
349 return false;
350 }
351 return true;
352 }
353
354 // Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ).
355 static bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) {
356 bool bBreak = true;
357 do {
358 if (!I2C_Start())
359 return false;
360
361 I2C_SendByte(device_address & 0xFE);
362 if (!I2C_WaitAck())
363 break;
364
365 I2C_SendByte(device_cmd);
366 if (!I2C_WaitAck())
367 break;
368
369 I2C_SendByte(data);
370 if (!I2C_WaitAck())
371 break;
372
373 bBreak = false;
374 } while (false);
375
376 I2C_Stop();
377 if (bBreak) {
378 if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
379 return false;
380 }
381 return true;
382 }
383
384 //Sends a string of data (Array, length, command to be written , SlaveDevice address ).
385 // len = uint8 (max buffer to write 256bytes)
386 static bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
387 bool bBreak = true;
388 do {
389 if (!I2C_Start())
390 return false;
391
392 I2C_SendByte(device_address & 0xFE);
393 if (!I2C_WaitAck())
394 break;
395
396 I2C_SendByte(device_cmd);
397 if (!I2C_WaitAck())
398 break;
399
400 while (len) {
401
402 I2C_SendByte(*data);
403 if (!I2C_WaitAck())
404 break;
405
406 len--;
407 data++;
408 }
409
410 if (len == 0)
411 bBreak = false;
412 } while (false);
413
414 I2C_Stop();
415 if (bBreak) {
416 if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
417 return false;
418 }
419 return true;
420 }
421
422 // read 1 strings of data (Data array, Readout length, command to be written , SlaveDevice address ).
423 // len = uint8 (max buffer to read 256bytes)
424 static int16_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
425
426 if ( !data || len == 0 )
427 return 0;
428
429 // extra wait 500us (514us measured)
430 // 200us (xx measured)
431 WaitUS(600);
432 bool bBreak = true;
433 uint16_t readcount = 0;
434
435 do {
436 if (!I2C_Start())
437 return 0;
438
439 // 0xB0 / 0xC0 == i2c write
440 I2C_SendByte(device_address & 0xFE);
441 if (!I2C_WaitAck())
442 break;
443
444 I2C_SendByte(device_cmd);
445 if (!I2C_WaitAck())
446 break;
447
448 // 0xB1 / 0xC1 == i2c read
449 I2C_Start();
450 I2C_SendByte(device_address | 1);
451 if (!I2C_WaitAck())
452 break;
453
454 bBreak = false;
455 } while (false);
456
457 if (bBreak) {
458 I2C_Stop();
459 if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
460 return 0;
461 }
462
463 while (len) {
464
465 int16_t tmp = I2C_ReadByte();
466 if ( tmp < 0 )
467 return tmp;
468
469 *data = (uint8_t)tmp & 0xFF;
470
471 len--;
472
473 // ¶ÁÈ¡µÄµÚÒ»¸ö×Ö½ÚΪºóÐø³¤¶È
474 // The first byte in response is the message length
475 if (!readcount && (len > *data)) {
476 len = *data;
477 } else {
478 data++;
479 }
480 readcount++;
481
482 // acknowledgements. After last byte send NACK.
483 if (len == 0)
484 I2C_NoAck();
485 else
486 I2C_Ack();
487 }
488
489 I2C_Stop();
490
491 // return bytecount - first byte (which is length byte)
492 return --readcount;
493 }
494
495 static int16_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
496 //START, 0xB0, 0x00, 0x00, START, 0xB1, xx, yy, zz, ......, STOP
497 bool bBreak = true;
498 uint8_t readcount = 0;
499
500 // sending
501 do {
502 if (!I2C_Start())
503 return 0;
504
505 // 0xB0 / 0xC0 i2c write
506 I2C_SendByte(device_address & 0xFE);
507 if (!I2C_WaitAck())
508 break;
509
510 I2C_SendByte(msb);
511 if (!I2C_WaitAck())
512 break;
513
514 I2C_SendByte(lsb);
515 if (!I2C_WaitAck())
516 break;
517
518 // 0xB1 / 0xC1 i2c read
519 I2C_Start();
520 I2C_SendByte(device_address | 1);
521 if (!I2C_WaitAck())
522 break;
523
524 bBreak = false;
525 } while (false);
526
527 if (bBreak) {
528 I2C_Stop();
529 if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
530 return 0;
531 }
532
533 // reading
534 while (len) {
535
536 int16_t tmp = I2C_ReadByte();
537 if ( tmp < 0 )
538 return tmp;
539
540 *data = (uint8_t)tmp & 0xFF;
541
542 data++;
543 readcount++;
544 len--;
545
546 // acknowledgements. After last byte send NACK.
547 if (len == 0)
548 I2C_NoAck();
549 else
550 I2C_Ack();
551 }
552
553 I2C_Stop();
554 return readcount;
555 }
556
557 static bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
558 //START, 0xB0, 0x00, 0x00, xx, yy, zz, ......, STOP
559 bool bBreak = true;
560
561 do {
562 if (!I2C_Start())
563 return false;
564
565 // 0xB0 == i2c write
566 I2C_SendByte(device_address & 0xFE);
567 if (!I2C_WaitAck())
568 break;
569
570 I2C_SendByte(msb);
571 if (!I2C_WaitAck())
572 break;
573
574 I2C_SendByte(lsb);
575 if (!I2C_WaitAck())
576 break;
577
578 while (len) {
579 I2C_SendByte(*data);
580 if (!I2C_WaitAck())
581 break;
582
583 len--;
584 data++;
585 }
586
587 if (len == 0)
588 bBreak = false;
589 } while (false);
590
591 I2C_Stop();
592 if (bBreak) {
593 if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
594 return false;
595 }
596 return true;
597 }
598
599 void I2C_print_status(void) {
600 DbpString("Smart card module (ISO 7816)");
601 uint8_t resp[] = {0,0,0,0};
602 I2C_Reset_EnterMainProgram();
603 uint8_t len = I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN);
604 if ( len > 0 )
605 Dbprintf(" version.................v%x.%02x", resp[0], resp[1]);
606 else
607 DbpString(" version.................FAILED");
608 }
609
610 // Will read response from smart card module, retries 3 times to get the data.
611 static bool sc_rx_bytes(uint8_t* dest, uint8_t *destlen) {
612 uint8_t i = 3;
613 int16_t len = 0;
614 while (i--) {
615
616 I2C_WaitForSim();
617
618 len = I2C_BufferRead(dest, *destlen, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
619
620 if ( len > 1 ){
621 break;
622 } else if ( len == 1 ) {
623 continue;
624 } else if ( len <= 0 ) {
625 return false;
626 }
627 }
628 // after three
629 if ( len <= 1 )
630 return false;
631
632 *destlen = (uint8_t)len & 0xFF;
633 return true;
634 }
635
636 static bool GetATR(smart_card_atr_t *card_ptr) {
637
638 if ( !card_ptr ) {
639 return false;
640 }
641
642 card_ptr->atr_len = 0;
643 memset(card_ptr->atr, 0, sizeof(card_ptr->atr));
644
645 // Send ATR
646 // start [C0 01] stop start C1 len aa bb cc stop]
647 I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN);
648
649 // wait for sim card to answer.
650 // 1byte = 1ms, max frame 256bytes. Should wait 256ms at least just in case.
651 if (!I2C_WaitForSim())
652 return false;
653
654 // read bytes from module
655 uint8_t len = sizeof(card_ptr->atr);
656 if ( !sc_rx_bytes(card_ptr->atr, &len) )
657 return false;
658
659 uint8_t pos_td = 1;
660 if ( (card_ptr->atr[1] & 0x10) == 0x10) pos_td++;
661 if ( (card_ptr->atr[1] & 0x20) == 0x20) pos_td++;
662 if ( (card_ptr->atr[1] & 0x40) == 0x40) pos_td++;
663
664 // T0 indicate presence T=0 vs T=1. T=1 has checksum TCK
665 if ( (card_ptr->atr[1] & 0x80) == 0x80) {
666
667 pos_td++;
668
669 // 1 == T1 , presence of checksum TCK
670 if ( (card_ptr->atr[pos_td] & 0x01) == 0x01) {
671 uint8_t chksum = 0;
672 // xor property. will be zero when xored with chksum.
673 for (uint8_t i = 1; i < len; ++i)
674 chksum ^= card_ptr->atr[i];
675 if ( chksum ) {
676 if ( MF_DBGLEVEL > 2) DbpString("Wrong ATR checksum");
677 }
678 }
679 }
680
681 card_ptr->atr_len = len;
682 LogTrace(card_ptr->atr, card_ptr->atr_len, 0, 0, NULL, false);
683
684 return true;
685 }
686
687 void SmartCardAtr(void) {
688 smart_card_atr_t card;
689 LED_D_ON();
690 clear_trace();
691 set_tracing(true);
692 I2C_Reset_EnterMainProgram();
693 bool isOK = GetATR( &card );
694 cmd_send(CMD_ACK, isOK, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
695 set_tracing(false);
696 LEDsoff();
697 }
698
699 void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) {
700
701 LED_D_ON();
702
703 uint8_t len = 0;
704 uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME);
705 smartcard_command_t flags = arg0;
706
707 if ((flags & SC_CONNECT))
708 clear_trace();
709
710 set_tracing(true);
711
712 if ((flags & SC_CONNECT)) {
713
714 I2C_Reset_EnterMainProgram();
715
716 if ((flags & SC_SELECT)) {
717 smart_card_atr_t card;
718 bool gotATR = GetATR( &card );
719 //cmd_send(CMD_ACK, gotATR, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
720 if ( !gotATR )
721 goto OUT;
722 }
723 }
724
725 if ((flags & SC_RAW) || (flags & SC_RAW_T0)) {
726
727 LogTrace(data, arg1, 0, 0, NULL, true);
728
729 // Send raw bytes
730 // asBytes = A0 A4 00 00 02
731 // arg1 = len 5
732 bool res = I2C_BufferWrite(data, arg1, ((flags & SC_RAW_T0) ? I2C_DEVICE_CMD_SEND_T0 : I2C_DEVICE_CMD_SEND), I2C_DEVICE_ADDRESS_MAIN);
733 if ( !res && MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
734
735 // read bytes from module
736 len = ISO7618_MAX_FRAME;
737 res = sc_rx_bytes(resp, &len);
738 if ( res ) {
739 LogTrace(resp, len, 0, 0, NULL, false);
740 } else {
741 len = 0;
742 }
743 }
744 OUT:
745 cmd_send(CMD_ACK, len, 0, 0, resp, len);
746 BigBuf_free();
747 set_tracing(false);
748 LEDsoff();
749 }
750
751 void SmartCardUpgrade(uint64_t arg0) {
752
753 LED_C_ON();
754
755 #define I2C_BLOCK_SIZE 128
756 // write. Sector0, with 11,22,33,44
757 // erase is 128bytes, and takes 50ms to execute
758
759 I2C_Reset_EnterBootloader();
760
761 bool isOK = true;
762 int16_t res = 0;
763 uint16_t length = arg0;
764 uint16_t pos = 0;
765 uint8_t *fwdata = BigBuf_get_addr();
766 uint8_t *verfiydata = BigBuf_malloc(I2C_BLOCK_SIZE);
767
768 while (length) {
769
770 uint8_t msb = (pos >> 8) & 0xFF;
771 uint8_t lsb = pos & 0xFF;
772
773 Dbprintf("FW %02X%02X", msb, lsb);
774
775 size_t size = MIN(I2C_BLOCK_SIZE, length);
776
777 // write
778 res = I2C_WriteFW(fwdata+pos, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
779 if ( !res ) {
780 DbpString("Writing failed");
781 isOK = false;
782 break;
783 }
784
785 // writing takes time.
786 WaitMS(50);
787
788 // read
789 res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
790 if ( res <= 0) {
791 DbpString("Reading back failed");
792 isOK = false;
793 break;
794 }
795
796 // cmp
797 if ( 0 != memcmp(fwdata+pos, verfiydata, size)) {
798 DbpString("not equal data");
799 isOK = false;
800 break;
801 }
802
803 length -= size;
804 pos += size;
805 }
806 cmd_send(CMD_ACK, isOK, pos, 0, 0, 0);
807 LED_C_OFF();
808 BigBuf_free();
809 }
810
811 // unfinished (or not needed?)
812 //void SmartCardSetBaud(uint64_t arg0) {
813 //}
814
815 void SmartCardSetClock(uint64_t arg0) {
816 LED_D_ON();
817 set_tracing(true);
818 I2C_Reset_EnterMainProgram();
819
820 // Send SIM CLC
821 // start [C0 05 xx] stop
822 I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN);
823
824 cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
825 set_tracing(false);
826 LEDsoff();
827 }
828
829 #endif
Impressum, Datenschutz