1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
9 //-----------------------------------------------------------------------------
10 // Routines to support ISO 14443 type A.
11 //-----------------------------------------------------------------------------
13 #include "mifarecmd.h"
16 //-----------------------------------------------------------------------------
17 // Select, Authenticaate, Read an MIFARE tag.
19 //-----------------------------------------------------------------------------
20 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
23 uint8_t blockNo
= arg0
;
24 uint8_t keyType
= arg1
;
26 ui64Key
= bytes_to_num(datain
, 6);
30 byte_t dataoutbuf
[16];
33 struct Crypto1State mpcs
= {0, 0};
34 struct Crypto1State
*pcs
;
39 // iso14a_set_tracing(false);
48 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
49 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
53 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
54 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
58 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
59 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
63 if(mifare_classic_halt(pcs
, cuid
)) {
64 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
72 // ----------------------------- crypto1 destroy
75 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
79 LogTrace(uid
, 4, 0, 0, TRUE
);
81 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
82 // memcpy(ack.d.asBytes, dataoutbuf, 16);
85 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
86 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
91 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
93 // iso14a_set_tracing(TRUE);
97 //-----------------------------------------------------------------------------
98 // Select, Authenticaate, Read an MIFARE tag.
99 // read sector (data = 4 x 16 bytes = 64 bytes)
100 //-----------------------------------------------------------------------------
101 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
104 uint8_t sectorNo
= arg0
;
105 uint8_t keyType
= arg1
;
106 uint64_t ui64Key
= 0;
107 ui64Key
= bytes_to_num(datain
, 6);
111 byte_t dataoutbuf
[16 * 4];
114 struct Crypto1State mpcs
= {0, 0};
115 struct Crypto1State
*pcs
;
119 iso14a_clear_trace();
120 // iso14a_set_tracing(false);
129 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
130 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
134 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_FIRST
)) {
135 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
139 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 0, dataoutbuf
+ 16 * 0)) {
140 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 0 error");
143 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 1, dataoutbuf
+ 16 * 1)) {
144 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 1 error");
147 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 2, dataoutbuf
+ 16 * 2)) {
148 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 2 error");
151 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 3, dataoutbuf
+ 16 * 3)) {
152 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 3 error");
156 if(mifare_classic_halt(pcs
, cuid
)) {
157 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
165 // ----------------------------- crypto1 destroy
166 crypto1_destroy(pcs
);
168 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
171 memset(uid
, 0x44, 4);
172 LogTrace(uid
, 4, 0, 0, TRUE
);
174 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
175 // memcpy(ack.d.asBytes, dataoutbuf, 16 * 2);
178 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,32);
179 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
182 // memcpy(ack.d.asBytes, dataoutbuf + 16 * 2, 16 * 2);
183 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
184 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
+32, 32);
188 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
190 // iso14a_set_tracing(TRUE);
194 //-----------------------------------------------------------------------------
195 // Select, Authenticaate, Read an MIFARE tag.
197 //-----------------------------------------------------------------------------
198 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
201 uint8_t blockNo
= arg0
;
202 uint8_t keyType
= arg1
;
203 uint64_t ui64Key
= 0;
204 byte_t blockdata
[16];
206 ui64Key
= bytes_to_num(datain
, 6);
207 memcpy(blockdata
, datain
+ 10, 16);
213 struct Crypto1State mpcs
= {0, 0};
214 struct Crypto1State
*pcs
;
218 iso14a_clear_trace();
219 // iso14a_set_tracing(false);
228 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
233 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
234 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
238 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
239 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
243 if(mifare_classic_halt(pcs
, cuid
)) {
244 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
252 // ----------------------------- crypto1 destroy
253 crypto1_destroy(pcs
);
255 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
258 memset(uid
, 0x44, 4);
259 LogTrace(uid
, 4, 0, 0, TRUE
);
261 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
264 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
265 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
270 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
272 // iso14a_set_tracing(TRUE);
276 // Return 1 if the nonce is invalid else return 0
277 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, byte_t
* parity
) {
278 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
279 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
280 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
285 //-----------------------------------------------------------------------------
286 // MIFARE nested authentication.
288 //-----------------------------------------------------------------------------
289 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
292 uint8_t blockNo
= arg0
& 0xff;
293 uint8_t keyType
= (arg0
>> 8) & 0xff;
294 uint8_t targetBlockNo
= arg1
& 0xff;
295 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
296 uint64_t ui64Key
= 0;
298 ui64Key
= bytes_to_num(datain
, 6);
301 uint16_t rtr
, i
, j
, len
;
303 static uint16_t dmin
, dmax
;
305 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, par
, ks1
;
306 uint32_t target_nt
[2], target_ks
[2];
308 uint8_t par_array
[4];
310 struct Crypto1State mpcs
= {0, 0};
311 struct Crypto1State
*pcs
;
313 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
315 uint32_t auth1_time
, auth2_time
;
316 static uint16_t delta_time
;
321 iso14a_clear_trace();
322 iso14a_set_tracing(false);
330 while((GetCountMifare() & 0xffff0000) != 0x00010000); // wait for counter to reset and "warm up"
332 // statistics on nonce distance
333 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
340 for (rtr
= 0; rtr
< 17; rtr
++) {
342 // prepare next select. No need to power down the card.
343 if(mifare_classic_halt(pcs
, cuid
)) {
344 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
349 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
350 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
356 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
357 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
363 auth2_time
= auth1_time
+ delta_time
;
367 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
368 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
373 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
374 for (i
= 101; i
< 1200; i
++) {
375 nttmp
= prng_successor(nttmp
, 1);
376 if (nttmp
== nt2
) break;
386 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
388 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
392 if (rtr
<= 1) return;
394 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
396 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
404 // -------------------------------------------------------------------------------------------------
408 // get crypted nonces for target sector
409 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
412 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
414 // prepare next select. No need to power down the card.
415 if(mifare_classic_halt(pcs
, cuid
)) {
416 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
420 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
421 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
426 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
427 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
431 // nested authentication
432 auth2_time
= auth1_time
+ delta_time
;
433 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, &par
, &auth2_time
);
435 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
439 nt2
= bytes_to_num(receivedAnswer
, 4);
440 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
);
442 // Parity validity check
443 for (j
= 0; j
< 4; j
++) {
444 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
& 0x08) >> 3));
449 nttest
= prng_successor(nt1
, dmin
- 1);
450 for (j
= dmin
; j
< dmax
+ 1; j
++) {
451 nttest
= prng_successor(nttest
, 1);
454 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
455 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
456 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
460 target_nt
[i
] = nttest
;
463 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
465 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
468 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
471 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
477 // ----------------------------- crypto1 destroy
478 crypto1_destroy(pcs
);
481 memset(uid
, 0x44, 4);
482 LogTrace(uid
, 4, 0, 0, TRUE
);
484 byte_t buf
[4 + 4 * 4];
485 memcpy(buf
, &cuid
, 4);
486 memcpy(buf
+4, &target_nt
[0], 4);
487 memcpy(buf
+8, &target_ks
[0], 4);
488 memcpy(buf
+12, &target_nt
[1], 4);
489 memcpy(buf
+16, &target_ks
[1], 4);
492 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
495 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
497 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
499 iso14a_set_tracing(TRUE
);
502 //-----------------------------------------------------------------------------
503 // MIFARE check keys. key count up to 85.
505 //-----------------------------------------------------------------------------
506 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
509 uint8_t blockNo
= arg0
;
510 uint8_t keyType
= arg1
;
511 uint8_t keyCount
= arg2
;
512 uint64_t ui64Key
= 0;
519 struct Crypto1State mpcs
= {0, 0};
520 struct Crypto1State
*pcs
;
524 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
525 MF_DBGLEVEL
= MF_DBG_NONE
;
528 iso14a_clear_trace();
529 iso14a_set_tracing(TRUE
);
538 for (i
= 0; i
< keyCount
; i
++) {
539 // FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
541 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
542 // prepare next select by sending a HALT. There is no need to power down the card.
543 if(mifare_classic_halt(pcs
, cuid
)) {
544 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
549 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
550 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
554 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
555 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
563 // ----------------------------- crypto1 destroy
564 crypto1_destroy(pcs
);
567 memset(uid
, 0x44, 4);
568 LogTrace(uid
, 4, 0, 0, TRUE
);
571 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
575 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
578 // restore debug level
579 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
582 //-----------------------------------------------------------------------------
583 // MIFARE commands set debug level
585 //-----------------------------------------------------------------------------
586 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
588 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
591 //-----------------------------------------------------------------------------
592 // Work with emulator memory
594 //-----------------------------------------------------------------------------
595 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
599 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
600 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
603 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
604 // UsbCommand ack = {CMD_ACK, {arg0, arg1, 0}};
607 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count
610 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
611 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
615 //-----------------------------------------------------------------------------
616 // Load a card into the emulator memory
618 //-----------------------------------------------------------------------------
619 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
621 uint8_t sectorNo
= 0;
622 uint8_t keyType
= arg1
;
623 uint64_t ui64Key
= 0;
625 struct Crypto1State mpcs
= {0, 0};
626 struct Crypto1State
*pcs
;
630 byte_t dataoutbuf
[16];
631 byte_t dataoutbuf2
[16];
635 iso14a_clear_trace();
636 iso14a_set_tracing(false);
645 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
646 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
650 for (i
= 0; i
< 16; i
++) {
652 ui64Key
= emlGetKey(sectorNo
, keyType
);
655 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_FIRST
)) {
656 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%d]. Auth error", i
);
660 if(mifare_classic_auth(pcs
, cuid
, sectorNo
* 4, keyType
, ui64Key
, AUTH_NESTED
)) {
661 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%d]. Auth nested error", i
);
666 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 0, dataoutbuf
)) {
667 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 0 error");
670 emlSetMem(dataoutbuf
, sectorNo
* 4 + 0, 1);
672 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 1, dataoutbuf
)) {
673 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 1 error");
676 emlSetMem(dataoutbuf
, sectorNo
* 4 + 1, 1);
678 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 2, dataoutbuf
)) {
679 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 2 error");
682 emlSetMem(dataoutbuf
, sectorNo
* 4 + 2, 1);
684 // get block 3 bytes 6-9
685 if(mifare_classic_readblock(pcs
, cuid
, sectorNo
* 4 + 3, dataoutbuf
)) {
686 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block 3 error");
689 emlGetMem(dataoutbuf2
, sectorNo
* 4 + 3, 1);
690 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
691 emlSetMem(dataoutbuf2
, sectorNo
* 4 + 3, 1);
694 if(mifare_classic_halt(pcs
, cuid
)) {
695 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
702 // ----------------------------- crypto1 destroy
703 crypto1_destroy(pcs
);
705 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
708 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
711 memset(uid
, 0x44, 4);
712 LogTrace(uid
, 4, 0, 0, TRUE
);
715 //-----------------------------------------------------------------------------
716 // MIFARE 1k emulator
718 //-----------------------------------------------------------------------------
721 //-----------------------------------------------------------------------------
722 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
724 //-----------------------------------------------------------------------------
725 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
728 uint8_t needWipe
= arg0
;
729 // bit 0 - need get UID
731 // bit 2 - need HALT after sequence
732 // bit 3 - need init FPGA and field before sequence
733 // bit 4 - need reset FPGA and LED
734 uint8_t workFlags
= arg1
;
735 uint8_t blockNo
= arg2
;
738 uint8_t wupC1
[] = { 0x40 };
739 uint8_t wupC2
[] = { 0x43 };
740 uint8_t wipeC
[] = { 0x41 };
748 memset(uid
, 0x00, 10);
749 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
751 if (workFlags
& 0x08) {
753 iso14a_clear_trace();
754 iso14a_set_tracing(TRUE
);
763 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
765 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
770 if (workFlags
& 0x01) {
771 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
772 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
776 if(mifare_classic_halt(NULL
, cuid
)) {
777 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
784 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
785 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
786 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
790 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
791 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
792 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
796 if(mifare_classic_halt(NULL
, cuid
)) {
797 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
803 if (workFlags
& 0x02) {
804 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
805 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
806 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
810 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
811 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
812 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
817 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
818 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
822 memcpy(d_block
, datain
, 16);
823 AppendCrc14443a(d_block
, 16);
825 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
826 if ((ReaderReceive(receivedAnswer
) != 1) || (receivedAnswer
[0] != 0x0a)) {
827 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
831 if (workFlags
& 0x04) {
832 if (mifare_classic_halt(NULL
, cuid
)) {
833 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
842 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
843 // if (isOK) memcpy(ack.d.asBytes, uid, 4);
847 * Removed by Martin, the uid is overwritten with 0x44,
848 * which can 't be intended.
850 * memset(uid, 0x44, 4);
851 * LogTrace(uid, 4, 0, 0, TRUE);
856 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
857 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
860 if ((workFlags
& 0x10) || (!isOK
)) {
862 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
867 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
871 // bit 2 - need HALT after sequence
872 // bit 3 - need init FPGA and field before sequence
873 // bit 4 - need reset FPGA and LED
874 uint8_t workFlags
= arg0
;
875 uint8_t blockNo
= arg2
;
878 uint8_t wupC1
[] = { 0x40 };
879 uint8_t wupC2
[] = { 0x43 };
886 memset(data
, 0x00, 18);
887 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
889 if (workFlags
& 0x08) {
891 iso14a_clear_trace();
892 iso14a_set_tracing(TRUE
);
901 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
903 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
907 if (workFlags
& 0x02) {
908 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
909 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
910 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
914 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
915 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
916 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
922 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, NULL
) != 18)) {
923 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
926 memcpy(data
, receivedAnswer
, 18);
928 if (workFlags
& 0x04) {
929 if (mifare_classic_halt(NULL
, cuid
)) {
930 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
939 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
940 // if (isOK) memcpy(ack.d.asBytes, data, 18);
944 * Removed by Martin, this piece of overwrites the 'data' variable
945 * which is sent two lines down, and is obviously not correct.
947 * memset(data, 0x44, 4);
948 * LogTrace(data, 4, 0, 0, TRUE);
951 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
952 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
955 if ((workFlags
& 0x10) || (!isOK
)) {
957 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);