]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
Fix data mtrim
[proxmark3-svn] / armsrc / mifarecmd.c
1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
5 // Midnitesnake - Dec 2013
6 // Andy Davies - Apr 2014
7 // Iceman - May 2014
8 //
9 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
10 // at your option, any later version. See the LICENSE.txt file for the text of
11 // the license.
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
15
16 #include "mifarecmd.h"
17 #include "apps.h"
18 #include "util.h"
19 #include "parity.h"
20 #include "crc.h"
21
22 // the block number for the ISO14443-4 PCB
23 uint8_t pcb_blocknum = 0;
24 // Deselect card by sending a s-block. the crc is precalced for speed
25 static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
26
27 //-----------------------------------------------------------------------------
28 // Select, Authenticate, Read a MIFARE tag.
29 // read block
30 //-----------------------------------------------------------------------------
31 void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
32 {
33 // params
34 uint8_t blockNo = arg0;
35 uint8_t keyType = arg1;
36 uint64_t ui64Key = 0;
37 ui64Key = bytes_to_num(datain, 6);
38
39 // variables
40 byte_t isOK = 0;
41 byte_t dataoutbuf[16];
42 uint8_t uid[10];
43 uint32_t cuid;
44 struct Crypto1State mpcs = {0, 0};
45 struct Crypto1State *pcs;
46 pcs = &mpcs;
47
48 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
49
50 clear_trace();
51
52 LED_A_ON();
53 LED_B_OFF();
54 LED_C_OFF();
55
56 while (true) {
57 if(!iso14443a_select_card(uid, NULL, &cuid)) {
58 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
59 break;
60 };
61
62 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
63 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
64 break;
65 };
66
67 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {
68 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
69 break;
70 };
71
72 if(mifare_classic_halt(pcs, cuid)) {
73 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
74 break;
75 };
76
77 isOK = 1;
78 break;
79 }
80
81 // ----------------------------- crypto1 destroy
82 crypto1_destroy(pcs);
83
84 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
85
86 LED_B_ON();
87 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);
88 LED_B_OFF();
89
90 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
91 LEDsoff();
92 }
93
94 void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){
95
96 bool turnOffField = (arg0 == 1);
97
98 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
99
100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
101
102 clear_trace();
103
104 if(!iso14443a_select_card(NULL, NULL, NULL)) {
105 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
106 OnError(0);
107 return;
108 };
109
110 if(!mifare_ultra_auth(keybytes)){
111 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");
112 OnError(1);
113 return;
114 }
115
116 if (turnOffField) {
117 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
118 LEDsoff();
119 }
120 cmd_send(CMD_ACK,1,0,0,0,0);
121 }
122
123 // Arg0 = BlockNo,
124 // Arg1 = UsePwd bool
125 // datain = PWD bytes,
126 void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
127 {
128 uint8_t blockNo = arg0;
129 byte_t dataout[16] = {0x00};
130 bool useKey = (arg1 == 1); //UL_C
131 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
132
133 LEDsoff();
134 LED_A_ON();
135 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
136
137 clear_trace();
138
139 int len = iso14443a_select_card(NULL, NULL, NULL);
140 if(!len) {
141 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);
142 OnError(1);
143 return;
144 }
145
146 // UL-C authentication
147 if ( useKey ) {
148 uint8_t key[16] = {0x00};
149 memcpy(key, datain, sizeof(key) );
150
151 if ( !mifare_ultra_auth(key) ) {
152 OnError(1);
153 return;
154 }
155 }
156
157 // UL-EV1 / NTAG authentication
158 if ( usePwd ) {
159 uint8_t pwd[4] = {0x00};
160 memcpy(pwd, datain, 4);
161 uint8_t pack[4] = {0,0,0,0};
162 if (!mifare_ul_ev1_auth(pwd, pack)) {
163 OnError(1);
164 return;
165 }
166 }
167
168 if( mifare_ultra_readblock(blockNo, dataout) ) {
169 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");
170 OnError(2);
171 return;
172 }
173
174 if( mifare_ultra_halt() ) {
175 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
176 OnError(3);
177 return;
178 }
179
180 cmd_send(CMD_ACK,1,0,0,dataout,16);
181 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
182 LEDsoff();
183 }
184
185 //-----------------------------------------------------------------------------
186 // Select, Authenticate, Read a MIFARE tag.
187 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
188 //-----------------------------------------------------------------------------
189 void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
190 {
191 // params
192 uint8_t sectorNo = arg0;
193 uint8_t keyType = arg1;
194 uint64_t ui64Key = 0;
195 ui64Key = bytes_to_num(datain, 6);
196
197 // variables
198 byte_t isOK = 0;
199 byte_t dataoutbuf[16 * 16];
200 uint8_t uid[10];
201 uint32_t cuid;
202 struct Crypto1State mpcs = {0, 0};
203 struct Crypto1State *pcs;
204 pcs = &mpcs;
205
206 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
207
208 clear_trace();
209
210 LED_A_ON();
211 LED_B_OFF();
212 LED_C_OFF();
213
214 isOK = 1;
215 if(!iso14443a_select_card(uid, NULL, &cuid)) {
216 isOK = 0;
217 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
218 }
219
220
221 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
222 isOK = 0;
223 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
224 }
225
226 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
227 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {
228 isOK = 0;
229 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);
230 break;
231 }
232 }
233
234 if(mifare_classic_halt(pcs, cuid)) {
235 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
236 }
237
238 // ----------------------------- crypto1 destroy
239 crypto1_destroy(pcs);
240
241 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");
242
243 LED_B_ON();
244 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));
245 LED_B_OFF();
246
247 // Thats it...
248 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
249 LEDsoff();
250 }
251
252 // arg0 = blockNo (start)
253 // arg1 = Pages (number of blocks)
254 // arg2 = useKey
255 // datain = KEY bytes
256 void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
257 {
258 LEDsoff();
259 LED_A_ON();
260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
261
262 // free eventually allocated BigBuf memory
263 BigBuf_free();
264 clear_trace();
265
266 // params
267 uint8_t blockNo = arg0;
268 uint16_t blocks = arg1;
269 bool useKey = (arg2 == 1); //UL_C
270 bool usePwd = (arg2 == 2); //UL_EV1/NTAG
271 uint32_t countblocks = 0;
272 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);
273 if (dataout == NULL){
274 Dbprintf("out of memory");
275 OnError(1);
276 return;
277 }
278
279 int len = iso14443a_select_card(NULL, NULL, NULL);
280 if (!len) {
281 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);
282 OnError(1);
283 return;
284 }
285
286 // UL-C authentication
287 if ( useKey ) {
288 uint8_t key[16] = {0x00};
289 memcpy(key, datain, sizeof(key) );
290
291 if ( !mifare_ultra_auth(key) ) {
292 OnError(1);
293 return;
294 }
295 }
296
297 // UL-EV1 / NTAG authentication
298 if (usePwd) {
299 uint8_t pwd[4] = {0x00};
300 memcpy(pwd, datain, sizeof(pwd));
301 uint8_t pack[4] = {0,0,0,0};
302
303 if (!mifare_ul_ev1_auth(pwd, pack)){
304 OnError(1);
305 return;
306 }
307 }
308
309 for (int i = 0; i < blocks; i++){
310 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {
311 Dbprintf("Data exceeds buffer!!");
312 break;
313 }
314
315 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);
316
317 if (len) {
318 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);
319 // if no blocks read - error out
320 if (i==0){
321 OnError(2);
322 return;
323 } else {
324 //stop at last successful read block and return what we got
325 break;
326 }
327 } else {
328 countblocks++;
329 }
330 }
331
332 len = mifare_ultra_halt();
333 if (len) {
334 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
335 OnError(3);
336 return;
337 }
338
339 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);
340
341 countblocks *= 4;
342
343 cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);
344 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
345 LEDsoff();
346 BigBuf_free();
347 }
348
349 //-----------------------------------------------------------------------------
350 // Select, Authenticate, Write a MIFARE tag.
351 // read block
352 //-----------------------------------------------------------------------------
353 void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
354 {
355 // params
356 uint8_t blockNo = arg0;
357 uint8_t keyType = arg1;
358 uint64_t ui64Key = 0;
359 byte_t blockdata[16];
360
361 ui64Key = bytes_to_num(datain, 6);
362 memcpy(blockdata, datain + 10, 16);
363
364 // variables
365 byte_t isOK = 0;
366 uint8_t uid[10];
367 uint32_t cuid;
368 struct Crypto1State mpcs = {0, 0};
369 struct Crypto1State *pcs;
370 pcs = &mpcs;
371
372 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
373
374 clear_trace();
375
376 LED_A_ON();
377 LED_B_OFF();
378 LED_C_OFF();
379
380 while (true) {
381 if(!iso14443a_select_card(uid, NULL, &cuid)) {
382 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
383 break;
384 };
385
386 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
387 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
388 break;
389 };
390
391 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {
392 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
393 break;
394 };
395
396 if(mifare_classic_halt(pcs, cuid)) {
397 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
398 break;
399 };
400
401 isOK = 1;
402 break;
403 }
404
405 // ----------------------------- crypto1 destroy
406 crypto1_destroy(pcs);
407
408 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
409
410 LED_B_ON();
411 cmd_send(CMD_ACK,isOK,0,0,0,0);
412 LED_B_OFF();
413
414
415 // Thats it...
416 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
417 LEDsoff();
418 }
419
420 /* // Command not needed but left for future testing
421 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
422 {
423 uint8_t blockNo = arg0;
424 byte_t blockdata[16] = {0x00};
425
426 memcpy(blockdata, datain, 16);
427
428 uint8_t uid[10] = {0x00};
429
430 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
431
432 clear_trace();
433 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
434
435 if(!iso14443a_select_card(uid, NULL, NULL)) {
436 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
437 OnError(0);
438 return;
439 };
440
441 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
442 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
443 OnError(0);
444 return; };
445
446 if(mifare_ultra_halt()) {
447 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
448 OnError(0);
449 return;
450 };
451
452 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
453
454 cmd_send(CMD_ACK,1,0,0,0,0);
455 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
456 LEDsoff();
457 }
458 */
459
460 // Arg0 : Block to write to.
461 // Arg1 : 0 = use no authentication.
462 // 1 = use 0x1A authentication.
463 // 2 = use 0x1B authentication.
464 // datain : 4 first bytes is data to be written.
465 // : 4/16 next bytes is authentication key.
466 void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
467 {
468 uint8_t blockNo = arg0;
469 bool useKey = (arg1 == 1); //UL_C
470 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
471 byte_t blockdata[4] = {0x00};
472
473 memcpy(blockdata, datain,4);
474
475 LEDsoff();
476 LED_A_ON();
477 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
478
479 clear_trace();
480
481 if(!iso14443a_select_card(NULL, NULL, NULL)) {
482 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
483 OnError(0);
484 return;
485 };
486
487 // UL-C authentication
488 if ( useKey ) {
489 uint8_t key[16] = {0x00};
490 memcpy(key, datain+4, sizeof(key) );
491
492 if ( !mifare_ultra_auth(key) ) {
493 OnError(1);
494 return;
495 }
496 }
497
498 // UL-EV1 / NTAG authentication
499 if (usePwd) {
500 uint8_t pwd[4] = {0x00};
501 memcpy(pwd, datain+4, 4);
502 uint8_t pack[4] = {0,0,0,0};
503 if (!mifare_ul_ev1_auth(pwd, pack)) {
504 OnError(1);
505 return;
506 }
507 }
508
509 if(mifare_ultra_writeblock(blockNo, blockdata)) {
510 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
511 OnError(0);
512 return;
513 };
514
515 if(mifare_ultra_halt()) {
516 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
517 OnError(0);
518 return;
519 };
520
521 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
522
523 cmd_send(CMD_ACK,1,0,0,0,0);
524 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
525 LEDsoff();
526 }
527
528 void MifareUSetPwd(uint8_t arg0, uint8_t *datain){
529
530 uint8_t pwd[16] = {0x00};
531 byte_t blockdata[4] = {0x00};
532
533 memcpy(pwd, datain, 16);
534
535 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
536 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
537
538 clear_trace();
539
540 if(!iso14443a_select_card(NULL, NULL, NULL)) {
541 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
542 OnError(0);
543 return;
544 };
545
546 blockdata[0] = pwd[7];
547 blockdata[1] = pwd[6];
548 blockdata[2] = pwd[5];
549 blockdata[3] = pwd[4];
550 if(mifare_ultra_writeblock( 44, blockdata)) {
551 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
552 OnError(44);
553 return;
554 };
555
556 blockdata[0] = pwd[3];
557 blockdata[1] = pwd[2];
558 blockdata[2] = pwd[1];
559 blockdata[3] = pwd[0];
560 if(mifare_ultra_writeblock( 45, blockdata)) {
561 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
562 OnError(45);
563 return;
564 };
565
566 blockdata[0] = pwd[15];
567 blockdata[1] = pwd[14];
568 blockdata[2] = pwd[13];
569 blockdata[3] = pwd[12];
570 if(mifare_ultra_writeblock( 46, blockdata)) {
571 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
572 OnError(46);
573 return;
574 };
575
576 blockdata[0] = pwd[11];
577 blockdata[1] = pwd[10];
578 blockdata[2] = pwd[9];
579 blockdata[3] = pwd[8];
580 if(mifare_ultra_writeblock( 47, blockdata)) {
581 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
582 OnError(47);
583 return;
584 };
585
586 if(mifare_ultra_halt()) {
587 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
588 OnError(0);
589 return;
590 };
591
592 cmd_send(CMD_ACK,1,0,0,0,0);
593 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
594 LEDsoff();
595 }
596
597 // Return 1 if the nonce is invalid else return 0
598 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {
599 return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
600 (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \
601 (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;
602 }
603
604
605 //-----------------------------------------------------------------------------
606 // MIFARE nested authentication.
607 //
608 //-----------------------------------------------------------------------------
609 void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)
610 {
611 // params
612 uint8_t blockNo = arg0 & 0xff;
613 uint8_t keyType = (arg0 >> 8) & 0xff;
614 uint8_t targetBlockNo = arg1 & 0xff;
615 uint8_t targetKeyType = (arg1 >> 8) & 0xff;
616 uint64_t ui64Key = 0;
617
618 ui64Key = bytes_to_num(datain, 6);
619
620 // variables
621 uint16_t rtr, i, j, len;
622 uint16_t davg;
623 static uint16_t dmin, dmax;
624 uint8_t uid[10];
625 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;
626 uint8_t par[1];
627 uint32_t target_nt[2], target_ks[2];
628
629 uint8_t par_array[4];
630 uint16_t ncount = 0;
631 struct Crypto1State mpcs = {0, 0};
632 struct Crypto1State *pcs;
633 pcs = &mpcs;
634 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
635
636 uint32_t auth1_time, auth2_time;
637 static uint16_t delta_time;
638
639 LED_A_ON();
640 LED_C_OFF();
641 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
642
643 // free eventually allocated BigBuf memory
644 BigBuf_free();
645
646 if (calibrate) clear_trace();
647 set_tracing(true);
648
649 // statistics on nonce distance
650 int16_t isOK = 0;
651 #define NESTED_MAX_TRIES 12
652 uint16_t unsuccessfull_tries = 0;
653 if (calibrate) { // for first call only. Otherwise reuse previous calibration
654 LED_B_ON();
655 WDT_HIT();
656
657 davg = dmax = 0;
658 dmin = 2000;
659 delta_time = 0;
660
661 for (rtr = 0; rtr < 17; rtr++) {
662
663 // Test if the action was cancelled
664 if(BUTTON_PRESS()) {
665 isOK = -2;
666 break;
667 }
668
669 // prepare next select. No need to power down the card.
670 if(mifare_classic_halt(pcs, cuid)) {
671 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
672 rtr--;
673 continue;
674 }
675
676 if(!iso14443a_select_card(uid, NULL, &cuid)) {
677 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
678 rtr--;
679 continue;
680 };
681
682 auth1_time = 0;
683 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
684 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
685 rtr--;
686 continue;
687 };
688
689 if (delta_time) {
690 auth2_time = auth1_time + delta_time;
691 } else {
692 auth2_time = 0;
693 }
694 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {
695 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");
696 rtr--;
697 continue;
698 };
699
700 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
701 for (i = 101; i < 1200; i++) {
702 nttmp = prng_successor(nttmp, 1);
703 if (nttmp == nt2) break;
704 }
705
706 if (i != 1200) {
707 if (rtr != 0) {
708 davg += i;
709 dmin = MIN(dmin, i);
710 dmax = MAX(dmax, i);
711 }
712 else {
713 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing
714 }
715 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);
716 } else {
717 unsuccessfull_tries++;
718 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)
719 isOK = -3;
720 }
721 }
722 }
723
724 davg = (davg + (rtr - 1)/2) / (rtr - 1);
725
726 if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);
727
728 dmin = davg - 2;
729 dmax = davg + 2;
730
731 LED_B_OFF();
732
733 }
734 // -------------------------------------------------------------------------------------------------
735
736 LED_C_ON();
737
738 // get crypted nonces for target sector
739 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces
740
741 target_nt[i] = 0;
742 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce
743
744 // prepare next select. No need to power down the card.
745 if(mifare_classic_halt(pcs, cuid)) {
746 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
747 continue;
748 }
749
750 if(!iso14443a_select_card(uid, NULL, &cuid)) {
751 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
752 continue;
753 };
754
755 auth1_time = 0;
756 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
757 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
758 continue;
759 };
760
761 // nested authentication
762 auth2_time = auth1_time + delta_time;
763 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);
764 if (len != 4) {
765 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);
766 continue;
767 };
768
769 nt2 = bytes_to_num(receivedAnswer, 4);
770 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);
771
772 // Parity validity check
773 for (j = 0; j < 4; j++) {
774 par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));
775 }
776
777 ncount = 0;
778 nttest = prng_successor(nt1, dmin - 1);
779 for (j = dmin; j < dmax + 1; j++) {
780 nttest = prng_successor(nttest, 1);
781 ks1 = nt2 ^ nttest;
782
783 if (valid_nonce(nttest, nt2, ks1, par_array)){
784 if (ncount > 0) { // we are only interested in disambiguous nonces, try again
785 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);
786 target_nt[i] = 0;
787 break;
788 }
789 target_nt[i] = nttest;
790 target_ks[i] = ks1;
791 ncount++;
792 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces
793 target_nt[i] = 0;
794 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);
795 break;
796 }
797 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);
798 }
799 }
800 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);
801 }
802 }
803
804 LED_C_OFF();
805
806 // ----------------------------- crypto1 destroy
807 crypto1_destroy(pcs);
808
809 byte_t buf[4 + 4 * 4];
810 memcpy(buf, &cuid, 4);
811 memcpy(buf+4, &target_nt[0], 4);
812 memcpy(buf+8, &target_ks[0], 4);
813 memcpy(buf+12, &target_nt[1], 4);
814 memcpy(buf+16, &target_ks[1], 4);
815
816 LED_B_ON();
817 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));
818 LED_B_OFF();
819
820 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");
821
822 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
823 LEDsoff();
824 }
825
826 //-----------------------------------------------------------------------------
827 // MIFARE check keys. key count up to 85.
828 //
829 //-----------------------------------------------------------------------------
830 void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
831 {
832 // params
833 uint8_t blockNo = arg0 & 0xff;
834 uint8_t keyType = (arg0 >> 8) & 0xff;
835 bool clearTrace = arg1;
836 uint8_t keyCount = arg2;
837 uint64_t ui64Key = 0;
838
839 // variables
840 int i;
841 byte_t isOK = 0;
842 uint8_t uid[10];
843 uint32_t cuid;
844 struct Crypto1State mpcs = {0, 0};
845 struct Crypto1State *pcs;
846 pcs = &mpcs;
847
848 // clear debug level
849 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
850 MF_DBGLEVEL = MF_DBG_NONE;
851
852 LED_A_ON();
853 LED_B_OFF();
854 LED_C_OFF();
855 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
856
857 if (clearTrace) clear_trace();
858 set_tracing(TRUE);
859
860 for (i = 0; i < keyCount; i++) {
861 if(mifare_classic_halt(pcs, cuid)) {
862 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");
863 }
864
865 if(!iso14443a_select_card(uid, NULL, &cuid)) {
866 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");
867 break;
868 };
869
870 ui64Key = bytes_to_num(datain + i * 6, 6);
871 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
872 continue;
873 };
874
875 isOK = 1;
876 break;
877 }
878
879 // ----------------------------- crypto1 destroy
880 crypto1_destroy(pcs);
881
882 LED_B_ON();
883 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);
884 LED_B_OFF();
885
886 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
887 LEDsoff();
888
889 // restore debug level
890 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
891 }
892
893 //-----------------------------------------------------------------------------
894 // MIFARE commands set debug level
895 //
896 //-----------------------------------------------------------------------------
897 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
898 MF_DBGLEVEL = arg0;
899 Dbprintf("Debug level: %d", MF_DBGLEVEL);
900 }
901
902 //-----------------------------------------------------------------------------
903 // Work with emulator memory
904 //
905 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
906 // involved in dealing with emulator memory. But if it is called later, it might
907 // destroy the Emulator Memory.
908 //-----------------------------------------------------------------------------
909
910 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
911 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
912 emlClearMem();
913 }
914
915 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
916 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
917 emlSetMem(datain, arg0, arg1); // data, block num, blocks count
918 }
919
920 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
921 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
922 byte_t buf[USB_CMD_DATA_SIZE];
923 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)
924
925 LED_B_ON();
926 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);
927 LED_B_OFF();
928 }
929
930 //-----------------------------------------------------------------------------
931 // Load a card into the emulator memory
932 //
933 //-----------------------------------------------------------------------------
934 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
935 uint8_t numSectors = arg0;
936 uint8_t keyType = arg1;
937 uint64_t ui64Key = 0;
938 uint32_t cuid;
939 struct Crypto1State mpcs = {0, 0};
940 struct Crypto1State *pcs;
941 pcs = &mpcs;
942
943 // variables
944 byte_t dataoutbuf[16];
945 byte_t dataoutbuf2[16];
946 uint8_t uid[10];
947
948 LED_A_ON();
949 LED_B_OFF();
950 LED_C_OFF();
951 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
952
953 clear_trace();
954 set_tracing(false);
955
956 bool isOK = true;
957
958 if(!iso14443a_select_card(uid, NULL, &cuid)) {
959 isOK = false;
960 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
961 }
962
963 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
964 ui64Key = emlGetKey(sectorNo, keyType);
965 if (sectorNo == 0){
966 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
967 isOK = false;
968 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);
969 break;
970 }
971 } else {
972 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {
973 isOK = false;
974 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);
975 break;
976 }
977 }
978
979 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
980 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {
981 isOK = false;
982 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);
983 break;
984 };
985 if (isOK) {
986 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {
987 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);
988 } else { // sector trailer, keep the keys, set only the AC
989 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
990 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
991 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
992 }
993 }
994 }
995
996 }
997
998 if(mifare_classic_halt(pcs, cuid)) {
999 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1000 };
1001
1002 // ----------------------------- crypto1 destroy
1003 crypto1_destroy(pcs);
1004
1005 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1006 LEDsoff();
1007
1008 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
1009
1010 }
1011
1012
1013 //-----------------------------------------------------------------------------
1014 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1015 //
1016 //-----------------------------------------------------------------------------
1017 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1018
1019 // params
1020 uint8_t needWipe = arg0;
1021 // bit 0 - need get UID
1022 // bit 1 - need wupC
1023 // bit 2 - need HALT after sequence
1024 // bit 3 - need init FPGA and field before sequence
1025 // bit 4 - need reset FPGA and LED
1026 uint8_t workFlags = arg1;
1027 uint8_t blockNo = arg2;
1028
1029 // card commands
1030 uint8_t wupC1[] = { 0x40 };
1031 uint8_t wupC2[] = { 0x43 };
1032 uint8_t wipeC[] = { 0x41 };
1033
1034 // variables
1035 byte_t isOK = 0;
1036 uint8_t uid[10] = {0x00};
1037 uint8_t d_block[18] = {0x00};
1038 uint32_t cuid;
1039
1040 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1041 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1042
1043 // reset FPGA and LED
1044 if (workFlags & 0x08) {
1045 LED_A_ON();
1046 LED_B_OFF();
1047 LED_C_OFF();
1048 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1049
1050 clear_trace();
1051 set_tracing(TRUE);
1052 }
1053
1054 while (true) {
1055
1056 // get UID from chip
1057 if (workFlags & 0x01) {
1058 if(!iso14443a_select_card(uid, NULL, &cuid)) {
1059 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1060 break;
1061 };
1062
1063 if(mifare_classic_halt(NULL, cuid)) {
1064 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1065 break;
1066 };
1067 };
1068
1069 // reset chip
1070 if (needWipe){
1071 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1072 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1073 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1074 break;
1075 };
1076
1077 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1078 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1079 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1080 break;
1081 };
1082
1083 if(mifare_classic_halt(NULL, cuid)) {
1084 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1085 break;
1086 };
1087 };
1088
1089 // write block
1090 if (workFlags & 0x02) {
1091 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1092 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1093 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1094 break;
1095 };
1096
1097 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1098 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1099 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1100 break;
1101 };
1102 }
1103
1104 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1105 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1106 break;
1107 };
1108
1109 memcpy(d_block, datain, 16);
1110 AppendCrc14443a(d_block, 16);
1111
1112 ReaderTransmit(d_block, sizeof(d_block), NULL);
1113 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1114 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1115 break;
1116 };
1117
1118 if (workFlags & 0x04) {
1119 if (mifare_classic_halt(NULL, cuid)) {
1120 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1121 break;
1122 };
1123 }
1124
1125 isOK = 1;
1126 break;
1127 }
1128
1129 LED_B_ON();
1130 cmd_send(CMD_ACK,isOK,0,0,uid,4);
1131 LED_B_OFF();
1132
1133 if ((workFlags & 0x10) || (!isOK)) {
1134 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1135 LEDsoff();
1136 }
1137 }
1138
1139
1140 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1141
1142 // params
1143 // bit 1 - need wupC
1144 // bit 2 - need HALT after sequence
1145 // bit 3 - need init FPGA and field before sequence
1146 // bit 4 - need reset FPGA and LED
1147 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1148 uint8_t workFlags = arg0;
1149 uint8_t blockNo = arg2;
1150
1151 // card commands
1152 uint8_t wupC1[] = { 0x40 };
1153 uint8_t wupC2[] = { 0x43 };
1154
1155 // variables
1156 byte_t isOK = 0;
1157 uint8_t data[18] = {0x00};
1158 uint32_t cuid = 0;
1159
1160 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1161 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1162
1163 if (workFlags & 0x08) {
1164 LED_A_ON();
1165 LED_B_OFF();
1166 LED_C_OFF();
1167 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1168
1169 clear_trace();
1170 set_tracing(TRUE);
1171 }
1172
1173 while (true) {
1174 if (workFlags & 0x02) {
1175 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1176 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1177 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1178 break;
1179 };
1180
1181 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1182 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1183 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1184 break;
1185 };
1186 }
1187
1188 // read block
1189 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {
1190 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1191 break;
1192 };
1193 memcpy(data, receivedAnswer, 18);
1194
1195 if (workFlags & 0x04) {
1196 if (mifare_classic_halt(NULL, cuid)) {
1197 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1198 break;
1199 };
1200 }
1201
1202 isOK = 1;
1203 break;
1204 }
1205
1206 LED_B_ON();
1207 if (workFlags & 0x20) {
1208 if (isOK)
1209 memcpy(datain, data, 18);
1210 }
1211 else
1212 cmd_send(CMD_ACK,isOK,0,0,data,18);
1213 LED_B_OFF();
1214
1215 if ((workFlags & 0x10) || (!isOK)) {
1216 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1217 LEDsoff();
1218 }
1219 }
1220
1221 void MifareCIdent(){
1222
1223 // card commands
1224 uint8_t wupC1[] = { 0x40 };
1225 uint8_t wupC2[] = { 0x43 };
1226
1227 // variables
1228 byte_t isOK = 1;
1229
1230 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1231 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1232
1233 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1234 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1235 isOK = 0;
1236 };
1237
1238 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1239 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1240 isOK = 0;
1241 };
1242
1243 if (mifare_classic_halt(NULL, 0)) {
1244 isOK = 0;
1245 };
1246
1247 cmd_send(CMD_ACK,isOK,0,0,0,0);
1248 }
1249
1250 //
1251 // DESFIRE
1252 //
1253
1254 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
1255
1256 byte_t dataout[11] = {0x00};
1257 uint8_t uid[10] = {0x00};
1258 uint32_t cuid;
1259
1260 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1261 clear_trace();
1262
1263 int len = iso14443a_select_card(uid, NULL, &cuid);
1264 if(!len) {
1265 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
1266 OnError(1);
1267 return;
1268 };
1269
1270 if(mifare_desfire_des_auth1(cuid, dataout)){
1271 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");
1272 OnError(4);
1273 return;
1274 }
1275
1276 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");
1277 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));
1278 }
1279
1280 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){
1281
1282 uint32_t cuid = arg0;
1283 uint8_t key[16] = {0x00};
1284 byte_t isOK = 0;
1285 byte_t dataout[12] = {0x00};
1286
1287 memcpy(key, datain, 16);
1288
1289 isOK = mifare_desfire_des_auth2(cuid, key, dataout);
1290
1291 if( isOK) {
1292 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");
1293 OnError(4);
1294 return;
1295 }
1296
1297 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");
1298
1299 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));
1300 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1301 LEDsoff();
1302 }
1303
1304 void OnSuccess(){
1305 pcb_blocknum = 0;
1306 ReaderTransmit(deselect_cmd, 3 , NULL);
1307 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1308 LEDsoff();
1309 }
1310
1311 void OnError(uint8_t reason){
1312 pcb_blocknum = 0;
1313 ReaderTransmit(deselect_cmd, 3 , NULL);
1314 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1315 cmd_send(CMD_ACK,0,reason,0,0,0);
1316 LEDsoff();
1317 }
Impressum, Datenschutz