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, Authenticate, Read a 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 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
46 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
47 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
51 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
52 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
56 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) {
57 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
61 if(mifare_classic_halt(pcs
, cuid
)) {
62 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
70 // ----------------------------- crypto1 destroy
73 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
76 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
79 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
84 void MifareUReadBlock(uint8_t arg0
,uint8_t *datain
)
87 uint8_t blockNo
= arg0
;
91 byte_t dataoutbuf
[16];
97 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
104 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
105 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
109 if(mifare_ultra_readblock(cuid
, blockNo
, dataoutbuf
)) {
110 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block error");
114 if(mifare_ultra_halt(cuid
)) {
115 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
123 if (MF_DBGLEVEL
>= 2) DbpString("READ BLOCK FINISHED");
126 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16);
128 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
133 //-----------------------------------------------------------------------------
134 // Select, Authenticate, Read a MIFARE tag.
135 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
136 //-----------------------------------------------------------------------------
137 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
140 uint8_t sectorNo
= arg0
;
141 uint8_t keyType
= arg1
;
142 uint64_t ui64Key
= 0;
143 ui64Key
= bytes_to_num(datain
, 6);
147 byte_t dataoutbuf
[16 * 16];
150 struct Crypto1State mpcs
= {0, 0};
151 struct Crypto1State
*pcs
;
155 iso14a_clear_trace();
157 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
164 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
166 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
170 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
172 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
175 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
176 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
+ 16 * blockNo
)) {
178 if (MF_DBGLEVEL
>= 1) Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
);
183 if(mifare_classic_halt(pcs
, cuid
)) {
184 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
187 // ----------------------------- crypto1 destroy
188 crypto1_destroy(pcs
);
190 if (MF_DBGLEVEL
>= 2) DbpString("READ SECTOR FINISHED");
193 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
));
197 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
202 void MifareUReadCard(uint8_t arg0
, uint8_t *datain
)
205 uint8_t sectorNo
= arg0
;
209 byte_t dataoutbuf
[16 * 4];
214 iso14a_clear_trace();
216 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
223 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
224 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
227 for(int sec
=0;sec
<16;sec
++){
228 if(mifare_ultra_readblock(cuid
, sectorNo
* 4 + sec
, dataoutbuf
+ 4 * sec
)) {
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Read block %d error",sec
);
233 if(mifare_ultra_halt(cuid
)) {
234 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
242 if (MF_DBGLEVEL
>= 2) DbpString("READ CARD FINISHED");
245 cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,64);
249 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
255 //-----------------------------------------------------------------------------
256 // Select, Authenticate, Write a MIFARE tag.
258 //-----------------------------------------------------------------------------
259 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
262 uint8_t blockNo
= arg0
;
263 uint8_t keyType
= arg1
;
264 uint64_t ui64Key
= 0;
265 byte_t blockdata
[16];
267 ui64Key
= bytes_to_num(datain
, 6);
268 memcpy(blockdata
, datain
+ 10, 16);
274 struct Crypto1State mpcs
= {0, 0};
275 struct Crypto1State
*pcs
;
279 iso14a_clear_trace();
281 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
288 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
289 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
293 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
294 if (MF_DBGLEVEL
>= 1) Dbprintf("Auth error");
298 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) {
299 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
303 if(mifare_classic_halt(pcs
, cuid
)) {
304 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
312 // ----------------------------- crypto1 destroy
313 crypto1_destroy(pcs
);
315 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
318 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
323 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
327 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
)
330 uint8_t blockNo
= arg0
;
331 byte_t blockdata
[16];
333 memset(blockdata
,'\0',16);
334 memcpy(blockdata
, datain
,16);
342 iso14a_clear_trace();
344 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
351 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
352 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
356 if(mifare_ultra_writeblock(cuid
, blockNo
, blockdata
)) {
357 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
361 if(mifare_ultra_halt(cuid
)) {
362 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
370 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
373 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
378 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
380 // iso14a_set_tracing(TRUE);
383 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
)
386 uint8_t blockNo
= arg0
;
389 memcpy(blockdata
, datain
,4);
397 iso14a_clear_trace();
399 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
406 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
407 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
411 if(mifare_ultra_special_writeblock(cuid
, blockNo
, blockdata
)) {
412 if (MF_DBGLEVEL
>= 1) Dbprintf("Write block error");
416 if(mifare_ultra_halt(cuid
)) {
417 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
425 if (MF_DBGLEVEL
>= 2) DbpString("WRITE BLOCK FINISHED");
428 cmd_send(CMD_ACK
,isOK
,0,0,0,0);
432 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
436 // Return 1 if the nonce is invalid else return 0
437 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, byte_t
* parity
) {
438 return ((oddparity((Nt
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
439 (oddparity((Nt
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
440 (oddparity((Nt
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0;
444 //-----------------------------------------------------------------------------
445 // MIFARE nested authentication.
447 //-----------------------------------------------------------------------------
448 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
)
451 uint8_t blockNo
= arg0
& 0xff;
452 uint8_t keyType
= (arg0
>> 8) & 0xff;
453 uint8_t targetBlockNo
= arg1
& 0xff;
454 uint8_t targetKeyType
= (arg1
>> 8) & 0xff;
455 uint64_t ui64Key
= 0;
457 ui64Key
= bytes_to_num(datain
, 6);
460 uint16_t rtr
, i
, j
, len
;
462 static uint16_t dmin
, dmax
;
464 uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, par
, ks1
;
465 uint32_t target_nt
[2], target_ks
[2];
467 uint8_t par_array
[4];
469 struct Crypto1State mpcs
= {0, 0};
470 struct Crypto1State
*pcs
;
472 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
474 uint32_t auth1_time
, auth2_time
;
475 static uint16_t delta_time
;
478 iso14a_clear_trace();
479 iso14a_set_tracing(false);
481 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
487 // statistics on nonce distance
488 if (calibrate
) { // for first call only. Otherwise reuse previous calibration
495 for (rtr
= 0; rtr
< 17; rtr
++) {
497 // prepare next select. No need to power down the card.
498 if(mifare_classic_halt(pcs
, cuid
)) {
499 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
504 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
505 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
511 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
512 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
518 auth2_time
= auth1_time
+ delta_time
;
522 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) {
523 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error");
528 nttmp
= prng_successor(nt1
, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
529 for (i
= 101; i
< 1200; i
++) {
530 nttmp
= prng_successor(nttmp
, 1);
531 if (nttmp
== nt2
) break;
541 delta_time
= auth2_time
- auth1_time
+ 32; // allow some slack for proper timing
543 if (MF_DBGLEVEL
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
);
547 if (rtr
<= 1) return;
549 davg
= (davg
+ (rtr
- 1)/2) / (rtr
- 1);
551 if (MF_DBGLEVEL
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
);
559 // -------------------------------------------------------------------------------------------------
563 // get crypted nonces for target sector
564 for(i
=0; i
< 2; i
++) { // look for exactly two different nonces
567 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce
569 // prepare next select. No need to power down the card.
570 if(mifare_classic_halt(pcs
, cuid
)) {
571 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Halt error");
575 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
576 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Can't select card");
581 if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) {
582 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth1 error");
586 // nested authentication
587 auth2_time
= auth1_time
+ delta_time
;
588 len
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType
& 0x01), targetBlockNo
, receivedAnswer
, &par
, &auth2_time
);
590 if (MF_DBGLEVEL
>= 1) Dbprintf("Nested: Auth2 error len=%d", len
);
594 nt2
= bytes_to_num(receivedAnswer
, 4);
595 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
);
597 // Parity validity check
598 for (j
= 0; j
< 4; j
++) {
599 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
& 0x08) >> 3));
604 nttest
= prng_successor(nt1
, dmin
- 1);
605 for (j
= dmin
; j
< dmax
+ 1; j
++) {
606 nttest
= prng_successor(nttest
, 1);
609 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){
610 if (ncount
> 0) { // we are only interested in disambiguous nonces, try again
611 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
);
615 target_nt
[i
] = nttest
;
618 if (i
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces
620 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
);
623 if (MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
);
626 if (target_nt
[i
] == 0 && j
== dmax
+1 && MF_DBGLEVEL
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1);
632 // ----------------------------- crypto1 destroy
633 crypto1_destroy(pcs
);
636 memset(uid
, 0x44, 4);
637 LogTrace(uid
, 4, 0, 0, TRUE
);
639 byte_t buf
[4 + 4 * 4];
640 memcpy(buf
, &cuid
, 4);
641 memcpy(buf
+4, &target_nt
[0], 4);
642 memcpy(buf
+8, &target_ks
[0], 4);
643 memcpy(buf
+12, &target_nt
[1], 4);
644 memcpy(buf
+16, &target_ks
[1], 4);
647 cmd_send(CMD_ACK
, 0, 2, targetBlockNo
+ (targetKeyType
* 0x100), buf
, sizeof(buf
));
650 if (MF_DBGLEVEL
>= 3) DbpString("NESTED FINISHED");
652 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
654 iso14a_set_tracing(TRUE
);
657 //-----------------------------------------------------------------------------
658 // MIFARE check keys. key count up to 85.
660 //-----------------------------------------------------------------------------
661 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
)
664 uint8_t blockNo
= arg0
;
665 uint8_t keyType
= arg1
;
666 uint8_t keyCount
= arg2
;
667 uint64_t ui64Key
= 0;
674 struct Crypto1State mpcs
= {0, 0};
675 struct Crypto1State
*pcs
;
679 int OLD_MF_DBGLEVEL
= MF_DBGLEVEL
;
680 MF_DBGLEVEL
= MF_DBG_NONE
;
683 iso14a_clear_trace();
684 iso14a_set_tracing(TRUE
);
686 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
692 for (i
= 0; i
< keyCount
; i
++) {
693 if(mifare_classic_halt(pcs
, cuid
)) {
694 if (MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Halt error");
697 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
698 if (OLD_MF_DBGLEVEL
>= 1) Dbprintf("ChkKeys: Can't select card");
702 ui64Key
= bytes_to_num(datain
+ i
* 6, 6);
703 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
711 // ----------------------------- crypto1 destroy
712 crypto1_destroy(pcs
);
715 cmd_send(CMD_ACK
,isOK
,0,0,datain
+ i
* 6,6);
719 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
722 // restore debug level
723 MF_DBGLEVEL
= OLD_MF_DBGLEVEL
;
726 //-----------------------------------------------------------------------------
727 // MIFARE commands set debug level
729 //-----------------------------------------------------------------------------
730 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
732 Dbprintf("Debug level: %d", MF_DBGLEVEL
);
735 //-----------------------------------------------------------------------------
736 // Work with emulator memory
738 //-----------------------------------------------------------------------------
739 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
743 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
744 emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count
747 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
749 emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4)
752 cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,48);
756 //-----------------------------------------------------------------------------
757 // Load a card into the emulator memory
759 //-----------------------------------------------------------------------------
760 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
761 uint8_t numSectors
= arg0
;
762 uint8_t keyType
= arg1
;
763 uint64_t ui64Key
= 0;
765 struct Crypto1State mpcs
= {0, 0};
766 struct Crypto1State
*pcs
;
770 byte_t dataoutbuf
[16];
771 byte_t dataoutbuf2
[16];
775 iso14a_clear_trace();
776 iso14a_set_tracing(false);
778 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
786 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
788 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
791 for (uint8_t sectorNo
= 0; isOK
&& sectorNo
< numSectors
; sectorNo
++) {
792 ui64Key
= emlGetKey(sectorNo
, keyType
);
794 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) {
796 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo
);
800 if(isOK
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) {
802 if (MF_DBGLEVEL
>= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo
);
807 for (uint8_t blockNo
= 0; isOK
&& blockNo
< NumBlocksPerSector(sectorNo
); blockNo
++) {
808 if(isOK
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) {
810 if (MF_DBGLEVEL
>= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
);
814 if (blockNo
< NumBlocksPerSector(sectorNo
) - 1) {
815 emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
816 } else { // sector trailer, keep the keys, set only the AC
817 emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
818 memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4);
819 emlSetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1);
826 if(mifare_classic_halt(pcs
, cuid
)) {
827 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
830 // ----------------------------- crypto1 destroy
831 crypto1_destroy(pcs
);
833 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
836 if (MF_DBGLEVEL
>= 2) DbpString("EMUL FILL SECTORS FINISHED");
841 //-----------------------------------------------------------------------------
842 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
844 //-----------------------------------------------------------------------------
845 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
848 uint8_t needWipe
= arg0
;
849 // bit 0 - need get UID
851 // bit 2 - need HALT after sequence
852 // bit 3 - need init FPGA and field before sequence
853 // bit 4 - need reset FPGA and LED
854 uint8_t workFlags
= arg1
;
855 uint8_t blockNo
= arg2
;
858 uint8_t wupC1
[] = { 0x40 };
859 uint8_t wupC2
[] = { 0x43 };
860 uint8_t wipeC
[] = { 0x41 };
868 memset(uid
, 0x00, 10);
869 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
871 if (workFlags
& 0x08) {
873 iso14a_clear_trace();
874 iso14a_set_tracing(TRUE
);
876 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
883 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
885 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
890 if (workFlags
& 0x01) {
891 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) {
892 if (MF_DBGLEVEL
>= 1) Dbprintf("Can't select card");
896 if(mifare_classic_halt(NULL
, cuid
)) {
897 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
904 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
905 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
906 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
910 ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
);
911 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
912 if (MF_DBGLEVEL
>= 1) Dbprintf("wipeC error");
916 if(mifare_classic_halt(NULL
, cuid
)) {
917 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
923 if (workFlags
& 0x02) {
924 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
925 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
926 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
930 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
931 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
932 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
937 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) {
938 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send command error");
942 memcpy(d_block
, datain
, 16);
943 AppendCrc14443a(d_block
, 16);
945 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
946 if ((ReaderReceive(receivedAnswer
) != 1) || (receivedAnswer
[0] != 0x0a)) {
947 if (MF_DBGLEVEL
>= 1) Dbprintf("write block send data error");
951 if (workFlags
& 0x04) {
952 if (mifare_classic_halt(NULL
, cuid
)) {
953 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
963 cmd_send(CMD_ACK
,isOK
,0,0,uid
,4);
966 if ((workFlags
& 0x10) || (!isOK
)) {
968 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
974 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){
978 // bit 2 - need HALT after sequence
979 // bit 3 - need init FPGA and field before sequence
980 // bit 4 - need reset FPGA and LED
981 uint8_t workFlags
= arg0
;
982 uint8_t blockNo
= arg2
;
985 uint8_t wupC1
[] = { 0x40 };
986 uint8_t wupC2
[] = { 0x43 };
993 memset(data
, 0x00, 18);
994 uint8_t* receivedAnswer
= mifare_get_bigbufptr();
996 if (workFlags
& 0x08) {
998 iso14a_clear_trace();
999 iso14a_set_tracing(TRUE
);
1001 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);
1008 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1010 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1014 if (workFlags
& 0x02) {
1015 ReaderTransmitBitsPar(wupC1
,7,0, NULL
);
1016 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1017 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC1 error");
1021 ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
);
1022 if(!ReaderReceive(receivedAnswer
) || (receivedAnswer
[0] != 0x0a)) {
1023 if (MF_DBGLEVEL
>= 1) Dbprintf("wupC2 error");
1029 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, NULL
) != 18)) {
1030 if (MF_DBGLEVEL
>= 1) Dbprintf("read block send command error");
1033 memcpy(data
, receivedAnswer
, 18);
1035 if (workFlags
& 0x04) {
1036 if (mifare_classic_halt(NULL
, cuid
)) {
1037 if (MF_DBGLEVEL
>= 1) Dbprintf("Halt error");
1047 cmd_send(CMD_ACK
,isOK
,0,0,data
,18);
1050 if ((workFlags
& 0x10) || (!isOK
)) {
1052 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);