1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
17 #include "proxmark3_arm.h"
20 #include "protocols.h"
22 #include "fpgaloader.h"
24 #include "commonutil.h"
30 // minimum time between the start bits of consecutive transfers from reader to tag: 6800 carrier (13.56MHz) cycles
31 #ifndef FELICA_REQUEST_GUARD_TIME
32 //# define FELICA_REQUEST_GUARD_TIME (6800 / 16 + 1) // 426
33 # define FELICA_REQUEST_GUARD_TIME ((512 + 0 * 256) * 64 / 16 + 1)
35 // FRAME DELAY TIME 2672 carrier cycles
36 #ifndef FELICA_FRAME_DELAY_TIME
37 # define FELICA_FRAME_DELAY_TIME (2672/16 + 1) // 168
39 #ifndef DELAY_AIR2ARM_AS_READER
40 #define DELAY_AIR2ARM_AS_READER (3 + 16 + 8 + 8*16 + 4*16 - 8*16) // 91
42 #ifndef DELAY_ARM2AIR_AS_READER
43 #define DELAY_ARM2AIR_AS_READER (4*16 + 8*16 + 8 + 8 + 1) // 209
45 #define AddCrc(data, len) compute_crc(CRC_FELICA, (data), (len), (data)+(len)+1, (data)+(len))
47 static uint32_t felica_timeout
;
48 static uint32_t felica_nexttransfertime
;
49 static uint32_t felica_lasttime_prox2air_start
;
51 static void iso18092_setup(uint8_t fpga_minor_mode
);
52 static uint8_t felica_select_card(felica_card_select_t
*card
);
53 static void TransmitFor18092_AsReader(const uint8_t *frame
, uint16_t len
, const uint32_t *NYI_timing_NYI
, uint8_t power
, uint8_t highspeed
);
54 static bool WaitForFelicaReply(uint16_t maxbytes
);
56 static void iso18092_set_timeout(uint32_t timeout
) {
57 felica_timeout
= timeout
+ (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / (16 * 8) + 2;
60 static uint32_t iso18092_get_timeout(void) {
61 return felica_timeout
- (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / (16 * 8) - 2;
64 #ifndef FELICA_MAX_FRAME_SIZE
65 #define FELICA_MAX_FRAME_SIZE 260
73 //structure to hold outgoing NFC frame
74 static uint8_t frameSpace
[FELICA_MAX_FRAME_SIZE
+ 4];
76 //structure to hold incoming NFC frame, used for ISO/IEC 18092-compatible frames
87 uint16_t shiftReg
; //for synchronization and offset calculation
94 //should be enough. maxlen is 255, 254 for data, 2 for sync, 2 for crc
95 // 0,1 -> SYNC, 2 - len, 3-(len+1)->data, then crc
98 //b2 4d is SYNC, 45645 in 16-bit notation, 10110010 01001101 binary. Frame will not start filling until this is shifted in
99 //bit order in byte -reverse, I guess? [((bt>>0)&1),((bt>>1)&1),((bt>>2)&1),((bt>>3)&1),((bt>>4)&1),((bt>>5)&1),((bt>>6)&1),((bt>>7)&1)] -at least in the mode that I read those in
101 # define SYNC_16BIT 0xB24D
104 static void FelicaFrameReset(void) {
105 FelicaFrame
.state
= STATE_UNSYNCD
;
106 FelicaFrame
.posCnt
= 0;
107 FelicaFrame
.crc_ok
= false;
108 FelicaFrame
.byte_offset
= 0;
110 static void FelicaFrameinit(uint8_t *data
) {
111 FelicaFrame
.framebytes
= data
;
115 //shift byte into frame, reversing it at the same time
116 static void shiftInByte(uint8_t bt
) {
118 for (j
= 0; j
< FelicaFrame
.byte_offset
; j
++) {
119 FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] = (FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] << 1) + (bt
& 1);
122 FelicaFrame
.posCnt
++;
123 FelicaFrame
.rem_len
--;
124 for (j
= FelicaFrame
.byte_offset
; j
< 8; j
++) {
125 FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] = (FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] << 1) + (bt
& 1);
130 static void Process18092Byte(uint8_t bt
) {
132 switch (FelicaFrame
.state
) {
134 case STATE_UNSYNCD
: {
135 // almost any nonzero byte can be start of SYNC. SYNC should be preceded by zeros, but that is not always the case
137 FelicaFrame
.shiftReg
= reflect8(bt
);
138 FelicaFrame
.state
= STATE_TRYING_SYNC
;
143 case STATE_TRYING_SYNC
: {
147 FelicaFrame
.shiftReg
= bt
;
148 FelicaFrame
.state
= STATE_UNSYNCD
;
151 for (uint8_t i
= 0; i
< 8; i
++) {
153 if (FelicaFrame
.shiftReg
== SYNC_16BIT
) {
155 FelicaFrame
.state
= STATE_GET_LENGTH
;
156 FelicaFrame
.framebytes
[0] = 0xb2;
157 FelicaFrame
.framebytes
[1] = 0x4d;
158 FelicaFrame
.byte_offset
= i
;
160 // shift in remaining byte, slowly...
161 for (uint8_t j
= i
; j
< 8; j
++) {
162 FelicaFrame
.framebytes
[2] = (FelicaFrame
.framebytes
[2] << 1) + (bt
& 1);
166 FelicaFrame
.posCnt
= 2;
171 FelicaFrame
.shiftReg
= (FelicaFrame
.shiftReg
<< 1) + (bt
& 1);
175 //that byte was last byte of sync
176 if (FelicaFrame
.shiftReg
== SYNC_16BIT
) {
177 //Force SYNC on next byte
178 FelicaFrame
.state
= STATE_GET_LENGTH
;
179 FelicaFrame
.framebytes
[0] = 0xb2;
180 FelicaFrame
.framebytes
[1] = 0x4d;
181 FelicaFrame
.byte_offset
= 0;
182 FelicaFrame
.posCnt
= 1;
187 case STATE_GET_LENGTH
: {
189 FelicaFrame
.rem_len
= FelicaFrame
.framebytes
[2] - 1;
190 FelicaFrame
.len
= FelicaFrame
.framebytes
[2] + 4; //with crc and sync
191 FelicaFrame
.state
= STATE_GET_DATA
;
194 case STATE_GET_DATA
: {
196 if (FelicaFrame
.rem_len
<= 0) {
197 FelicaFrame
.state
= STATE_GET_CRC
;
198 FelicaFrame
.rem_len
= 2;
202 case STATE_GET_CRC
: {
204 if (FelicaFrame
.rem_len
<= 0) {
205 FelicaFrame
.rem_len
= 0;
206 // skip sync 2bytes. IF ok, residue should be 0x0000
207 FelicaFrame
.crc_ok
= check_crc(CRC_FELICA
, FelicaFrame
.framebytes
+ 2, FelicaFrame
.len
- 2);
208 FelicaFrame
.state
= STATE_FULL
;
212 case STATE_FULL
: //ignore byte. Don't forget to clear frame to receive next one...
218 /* Perform FeliCa polling card
219 * Currently does NOT do any collision handling.
220 * It expects 0-1 cards in the device's range.
221 * return 0 if selection was successful
223 static uint8_t felica_select_card(felica_card_select_t
*card
) {
226 // 0xB2 0x4B = sync code
229 // 0xff = system code service
230 // 0xff = system code service
231 // 0x00 = request code
232 // b7 = automatic switching of data rate
234 // b1 = fc/32 (414kbps)
235 // b0 = fc/64 (212kbps)
238 static uint8_t poll
[10] = {0xb2, 0x4d, 0x06, FELICA_POLL_REQ
, 0xFF, 0xFF, 0x00, 0x00, 0x09, 0x21};
241 // We try 10 times, or if answer was received.
244 // end-of-reception response packet data, wait approx. 501μs
245 // end-of-transmission command packet data, wait approx. 197μs
247 TransmitFor18092_AsReader(poll
, sizeof(poll
), NULL
, 1, 0);
249 // polling card, break if success
250 if (WaitForFelicaReply(1024) && FelicaFrame
.framebytes
[3] == FELICA_POLL_ACK
) {
264 if (FelicaFrame
.framebytes
[3] != FELICA_POLL_ACK
) {
268 // 3. wrong crc. residue is 0, hence if crc is a value it failed.
269 if (check_crc(CRC_FELICA
, FelicaFrame
.framebytes
+ 2, FelicaFrame
.len
- 2) == false) {
271 if (g_dbglevel
>= DBG_DEBUG
) {
272 Dbprintf("Error: CRC check failed!");
273 Dbhexdump(FelicaFrame
.len
- 2, FelicaFrame
.framebytes
+ 2, 0);
281 memcpy(card
->IDm
, FelicaFrame
.framebytes
+ 4, 8);
282 memcpy(card
->PMm
, FelicaFrame
.framebytes
+ 4 + 8, 8);
283 // memcpy(card->servicecode, FelicaFrame.framebytes + 4 + 8 + 8, 2);
284 memcpy(card
->code
, card
->IDm
, 2);
285 memcpy(card
->uid
, card
->IDm
+ 2, 6);
286 memcpy(card
->iccode
, card
->PMm
, 2);
287 memcpy(card
->mrt
, card
->PMm
+ 2, 6);
288 if (g_dbglevel
>= DBG_DEBUG
) {
289 Dbprintf("Received Frame: ");
290 Dbhexdump(FelicaFrame
.len
, FelicaFrame
.framebytes
, 0);
297 // poll-0: 0xb2,0x4d,0x06,0x00,0xff,0xff,0x00,0x00,0x09,0x21,
298 // resp: 0xb2,0x4d,0x12,0x01,0x01,0x2e,0x3d,0x17,0x26,0x47,0x80,0x95,0x00,0xf1,0x00,0x00,0x00,0x01,0x43,0x00,0xb3,0x7f,
299 // poll-1 (reply with available system codes - NFC Tag3 specs, IIRC): 0xb2,0x4d,0x06,0x00,0xff,0xff,0x01,0x00,0x3a,0x10
300 // resp: 0xb2,0x4d,0x14,0x01, 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX, 0x00,0xf1,0x00,0x00,0x00,0x01,0x43,0x00, 0x88,0xb4,0x0c,0xe2,
301 // page-req: 0xb2,0x4d,0x10,0x06, 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX, 0x01, 0x0b,0x00, 0x01, 0x80,0x00, 0x2e,0xb3,
302 // page-req: 0x06, IDm(8), ServiceNum(1),Slist(2*num) BLocknum (1) BLockids(2-3*num)
303 // page-resp: 0xb2,0x4d,0x1d,0x07, 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX, 0x00, 0x00, 0x01, 0x10,0x04,0x01,0x00,0x0d,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x23, 0xcb,0x6e,
305 // builds a readblock frame for felica lite(s). Using SERVICE: SERVICE_FELICA_LITE_READONLY
306 // Felica standard has a different file system, AFAIK,
307 // 8-byte IDm, number of blocks, blocks numbers
308 // number of blocks limited to 4 for FelicaLite(S)
309 static void BuildFliteRdblk(const uint8_t *idm
, uint8_t blocknum
, const uint16_t *blocks
) {
311 if (blocknum
> 4 || blocknum
== 0) {
312 Dbprintf("Invalid number of blocks, %d != 4", blocknum
);
315 uint8_t c
= 0, i
= 0;
318 frameSpace
[c
++] = 0xb2;
319 frameSpace
[c
++] = 0x4d;
321 c
++; // set length later
323 frameSpace
[c
++] = FELICA_RDBLK_REQ
; // command number
325 // card IDm, from poll
326 frameSpace
[c
++] = idm
[0];
327 frameSpace
[c
++] = idm
[1];
328 frameSpace
[c
++] = idm
[2];
329 frameSpace
[c
++] = idm
[3];
330 frameSpace
[c
++] = idm
[4];
331 frameSpace
[c
++] = idm
[5];
332 frameSpace
[c
++] = idm
[6];
333 frameSpace
[c
++] = idm
[7];
335 // number of services
336 frameSpace
[c
++] = 0x01;
339 frameSpace
[c
++] = (SERVICE_FELICA_LITE_READONLY
>> 8);
340 frameSpace
[c
++] = SERVICE_FELICA_LITE_READONLY
& 0xFF;
343 frameSpace
[c
++] = blocknum
;
345 for (i
= 0; i
< blocknum
; i
++) {
348 if (blocks
[i
] >= 256) {
349 frameSpace
[c
++] = 0x00;
350 frameSpace
[c
++] = (blocks
[i
] >> 8); // block number, little endian....
351 frameSpace
[c
++] = (blocks
[i
] & 0xff);
353 frameSpace
[c
++] = 0x80;
354 frameSpace
[c
++] = blocks
[i
];
359 frameSpace
[2] = c
- 2;
361 AddCrc(frameSpace
+ 2, c
- 2);
364 static void TransmitFor18092_AsReader(const uint8_t *frame
, uint16_t len
, const uint32_t *NYI_timing_NYI
, uint8_t power
, uint8_t highspeed
) {
366 if (NYI_timing_NYI
!= NULL
) {
367 DbpString("Error: TransmitFor18092_AsReader does not check or set parameter NYI_timing_NYI");
371 uint16_t flags
= FPGA_MAJOR_MODE_HF_ISO18092
;
374 flags
|= FPGA_HF_ISO18092_FLAG_READER
;
378 flags
|= FPGA_HF_ISO18092_FLAG_424K
;
381 FpgaWriteConfWord(flags
);
383 uint32_t curr_transfer_time
= ((MAX(felica_nexttransfertime
, GetCountSspClk()) & 0xfffffff8) + 8);
385 while (GetCountSspClk() < curr_transfer_time
) {};
387 felica_lasttime_prox2air_start
= curr_transfer_time
;
390 // sending 0x00 0x00 0x00 0x00 0x00 0x00
393 // keep tx buffer in a defined state anyway.
394 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
395 AT91C_BASE_SSC
->SSC_THR
= 0x00;
399 // sending data with sync bytes
403 // Put byte into tx holding register as soon as it is ready
404 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
405 AT91C_BASE_SSC
->SSC_THR
= frame
[c
++];
410 while (!(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))) {};
411 AT91C_BASE_SSC
->SSC_THR
= 0x00; //minimum delay
413 while (!(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))) {};
414 AT91C_BASE_SSC
->SSC_THR
= 0x00; //spin
421 (felica_lasttime_prox2air_start
<< 4) + DELAY_ARM2AIR_AS_READER
,
422 ((felica_lasttime_prox2air_start
+ felica_lasttime_prox2air_start
) << 4) + DELAY_ARM2AIR_AS_READER
,
427 felica_nexttransfertime
= MAX(felica_nexttransfertime
, felica_lasttime_prox2air_start
+ FELICA_REQUEST_GUARD_TIME
);
430 // Wait for tag reply
431 // stop when button is pressed
432 // or return TRUE when command is captured
433 bool WaitForFelicaReply(uint16_t maxbytes
) {
435 // if (g_dbglevel >= DBG_DEBUG) { Dbprintf("WaitForFelicaReply Start"); }
439 // power, no modulation
440 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092
| FPGA_HF_ISO18092_FLAG_READER
| FPGA_HF_ISO18092_FLAG_NOMOD
);
444 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
447 uint32_t timeout
= iso18092_get_timeout();
453 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
455 b
= (uint8_t)(AT91C_BASE_SSC
->SSC_RHR
);
459 if (FelicaFrame
.state
== STATE_FULL
) {
461 felica_nexttransfertime
= MAX(
462 felica_nexttransfertime
,
463 (GetCountSspClk() & 0xfffffff8) - (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / 16 + FELICA_FRAME_DELAY_TIME
);
466 FelicaFrame
.framebytes
,
468 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
- timeout
,
469 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
,
475 } else if (c
++ > timeout
&& (FelicaFrame
.state
== STATE_UNSYNCD
|| FelicaFrame
.state
== STATE_TRYING_SYNC
)) {
477 // if (g_dbglevel >= DBG_DEBUG) Dbprintf("Error: Timeout! STATE_UNSYNCD");
485 // Set up FeliCa communication (similar to iso14443a_setup)
486 // field is setup for "Sending as Reader"
487 static void iso18092_setup(uint8_t fpga_minor_mode
) {
491 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
493 FpgaDownloadAndGo(FPGA_BITSTREAM_HF_FELICA
);
495 // allocate command receive buffer
497 BigBuf_Clear_ext(false);
499 // Initialize Demod and Uart structs
500 // DemodInit(BigBuf_malloc(MAX_FRAME_SIZE));
501 FelicaFrameinit(BigBuf_calloc(FELICA_MAX_FRAME_SIZE
));
503 felica_nexttransfertime
= 2 * DELAY_ARM2AIR_AS_READER
; // 418
504 // iso18092_set_timeout(2120); // 106 * 20ms maximum start-up time of card
505 iso18092_set_timeout(1060); // 106 * 10ms maximum start-up time of card
507 init_table(CRC_FELICA
);
509 // connect Demodulated Signal to ADC:
510 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
512 // Set up the synchronous serial port
513 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO18092
);
515 // LSB transfer. Remember to set it back to MSB with
516 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
518 // Signal field is on with the appropriate LED
519 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092
| fpga_minor_mode
);
521 //20.4 ms generate field, start sending polling command afterwars.
530 static void felica_reset_frame_mode(void) {
532 //Resetting Frame mode (First set in fpgaloader.c)
533 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
537 //-----------------------------------------------------------------------------
538 // RAW FeliCa commands. Send out commands and store answers.
539 //-----------------------------------------------------------------------------
541 // arg1 len of commandbytes
542 // d.asBytes command bytes to send
543 void felica_sendraw(const PacketCommandNG
*c
) {
545 felica_command_t param
= c
->oldarg
[0];
546 size_t len
= c
->oldarg
[1] & 0xffff;
549 if ((param
& FELICA_CONNECT
) == FELICA_CONNECT
) {
554 iso18092_setup(FPGA_HF_ISO18092_FLAG_READER
| FPGA_HF_ISO18092_FLAG_NOMOD
);
556 if ((param
& FELICA_CONNECT
) == FELICA_CONNECT
) {
558 // notify client selecting status.
559 // if failed selecting, turn off antenna and quite.
560 if ((param
& FELICA_NO_SELECT
) != FELICA_NO_SELECT
) {
562 felica_card_select_t card
;
563 arg0
= felica_select_card(&card
);
564 reply_mix(CMD_ACK
, arg0
, sizeof(card
.uid
), 0, &card
, sizeof(felica_card_select_t
));
566 felica_reset_frame_mode();
573 if ((param
& FELICA_RAW
) == FELICA_RAW
) {
575 // 2 sync, 1 len, 2crc == 5
576 uint8_t *buf
= BigBuf_calloc(len
+ 5);
583 memcpy(buf
+ 2, c
->data
.asBytes
, len
);
585 if ((param
& FELICA_APPEND_CRC
) == FELICA_APPEND_CRC
) {
586 // Don't append crc on empty bytearray...
588 AddCrc(buf
+ 2, len
);
592 if (g_dbglevel
>= DBG_DEBUG
) {
593 Dbprintf("Transmit Frame (no CRC shown):");
594 Dbhexdump(len
, buf
, 0);
595 Dbprintf("Buffer Length: %i", buf
[2] + 4);
598 TransmitFor18092_AsReader(buf
, buf
[2] + 4, NULL
, 1, 0);
599 arg0
= WaitForFelicaReply(1024);
601 if (g_dbglevel
>= DBG_DEBUG
) {
602 Dbprintf("Received Frame Code: %d", arg0
);
603 Dbhexdump(FelicaFrame
.len
, FelicaFrame
.framebytes
, 0);
606 uint32_t result
= reply_mix(CMD_ACK
, FelicaFrame
.len
, arg0
, 0, FelicaFrame
.framebytes
, FelicaFrame
.len
);
608 Dbprintf("Reply to Client Error Code: %i", result
);
612 if ((param
& FELICA_NO_DISCONNECT
) == FELICA_NO_DISCONNECT
) {
616 felica_reset_frame_mode();
620 void felica_sniff(uint32_t samplesToSkip
, uint32_t triggersToSkip
) {
624 iso18092_setup(FPGA_HF_ISO18092_FLAG_NOMOD
);
628 int retval
= PM3_SUCCESS
;
629 int remFrames
= (samplesToSkip
) ? samplesToSkip
: 0;
631 uint32_t timeout
= iso18092_get_timeout();
635 uint16_t checker
= 0;
640 // since simulation is a tight time critical loop,
641 // we only check for user request to end at iteration 3000, 9000.
643 if (data_available()) {
644 retval
= PM3_EOPABORTED
;
650 if (checker
>= 3000) {
652 if (BUTTON_PRESS()) {
653 retval
= PM3_EOPABORTED
;
661 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
663 uint8_t dist
= (uint8_t)(AT91C_BASE_SSC
->SSC_RHR
);
664 Process18092Byte(dist
);
666 if ((dist
>= 178) && (++trigger_cnt
> triggersToSkip
)) {
667 Dbprintf("triggers To skip kicked %d", dist
);
670 if (FelicaFrame
.state
== STATE_FULL
) {
671 if ((FelicaFrame
.framebytes
[3] % 2) == 0) {
672 isReaderFrame
= true; // All Reader Frames are even and all Tag frames are odd
674 isReaderFrame
= false;
677 if (remFrames
<= 0) {
678 Dbprintf("Stop Sniffing - samples To skip reached!");
681 LogTrace(FelicaFrame
.framebytes
,
683 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
- timeout
,
684 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
,
694 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
696 Dbprintf("Felica sniffing done, tracelen: %i", BigBuf_get_traceLen());
697 reply_ng(CMD_HF_FELICA_SNIFF
, retval
, NULL
, 0);
701 #define R_POLL0_LEN 0x16
702 #define R_POLL1_LEN 0x18
703 #define R_READBLK_LEN 0x21
704 //simulate NFC Tag3 card - for now only poll response works
705 // second half (4 bytes) of NDEF2 goes into nfcid2_0, first into nfcid2_1
706 void felica_sim_lite(const uint8_t *uid
) {
708 // prepare our 3 responses...
709 uint8_t resp_poll0
[R_POLL0_LEN
] = { 0xb2, 0x4d, 0x12, FELICA_POLL_ACK
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x01, 0x43, 0x00, 0xb3, 0x7f};
710 uint8_t resp_poll1
[R_POLL1_LEN
] = { 0xb2, 0x4d, 0x14, FELICA_POLL_ACK
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x01, 0x43, 0x00, 0x88, 0xb4, 0xb3, 0x7f};
711 uint8_t resp_readblk
[R_READBLK_LEN
] = { 0xb2, 0x4d, 0x1d, FELICA_RDBLK_ACK
, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x04, 0x01, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x23, 0xcb, 0x6e};
713 // NFC tag 3/ ISo technically. Many overlapping standards
714 DbpString("Felica Lite-S simulation start");
715 Dbprintf("NDEF2 UID: %02x %02x %02x %02x %02x %02x %02x %02x",
716 uid
[0], uid
[1], uid
[2], uid
[3], uid
[4], uid
[5], uid
[6], uid
[7]
720 for (uint8_t i
= 0; i
< 8; i
++) {
721 resp_poll0
[i
+ 4] = uid
[i
];
722 resp_poll1
[i
+ 4] = uid
[i
];
723 resp_readblk
[i
+ 4] = uid
[i
];
726 // calculate and set CRC
727 AddCrc(&resp_poll0
[2], resp_poll0
[2]);
728 AddCrc(&resp_poll1
[2], resp_poll1
[2]);
729 AddCrc(&resp_readblk
[2], resp_readblk
[2]);
731 iso18092_setup(FPGA_HF_ISO18092_FLAG_NOMOD
);
733 int retval
= PM3_SUCCESS
;
735 const uint8_t *curresp
= NULL
;
736 bool listenmode
= true;
737 // uint32_t frtm = GetCountSspClk();
740 uint16_t checker
= 0;
745 // since simulation is a tight time critical loop,
746 // we only check for user request to end at iteration 3000, 9000.
748 if (data_available()) {
749 retval
= PM3_EOPABORTED
;
755 if (checker
>= 3000) {
757 if (BUTTON_PRESS()) {
758 retval
= PM3_EOPABORTED
;
769 // waiting for request...
770 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
772 uint8_t dist
= (uint8_t)(AT91C_BASE_SSC
->SSC_RHR
);
773 // frtm = GetCountSspClk();
774 Process18092Byte(dist
);
776 if (FelicaFrame
.state
== STATE_FULL
) {
778 if (FelicaFrame
.crc_ok
) {
780 if (FelicaFrame
.framebytes
[2] == 6 && FelicaFrame
.framebytes
[3] == 0) {
781 static uint8_t timeslot
= 0;
783 // polling... there are two types of polling we answer to
784 if (FelicaFrame
.framebytes
[6] == 0) {
785 curresp
= resp_poll0
;
786 curlen
= R_POLL0_LEN
;
789 if (FelicaFrame
.framebytes
[6] == 1) {
790 curresp
= resp_poll1
;
791 curlen
= R_POLL1_LEN
;
794 if (timeslot
> FelicaFrame
.framebytes
[7]) {
795 // framebytes[7] contains the maximum time slot in which we are allowed to respond (#0..#15)
798 // first time slot (#0) starts after 512 * 64 / fc, slot length equals 256 * 64 / fc
799 felica_nexttransfertime
= GetCountSspClk() - (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / 16 + (512 + timeslot
* 256) * 64 / 16 + 1;
800 timeslot
++; // we should use a random time slot, but responding in incremental slots should do just fine for now
803 if (FelicaFrame
.framebytes
[2] > 5 && FelicaFrame
.framebytes
[3] == 0x06) {
804 // we should rebuild it depending on page size, but...
806 curresp
= resp_readblk
;
807 curlen
= R_READBLK_LEN
;
813 // frame invalid, clear it out to allow for the next one
821 if (listenmode
== false) {
822 // trying to answer... here to start answering immediately.
823 // this one is a bit finicky. Seems that being a bit late is better than earlier
824 // TransmitFor18092_AsReader(curresp, curlen, frtm+512, 0, 0);
825 TransmitFor18092_AsReader(curresp
, curlen
, NULL
, 0, 0);
828 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092
| FPGA_HF_ISO18092_FLAG_NOMOD
);
840 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
842 Dbprintf("FeliCa Lite-S emulator stopped. Trace length: %d ", BigBuf_get_traceLen());
843 reply_ng(CMD_HF_FELICALITE_SIMULATE
, retval
, NULL
, 0);
846 #define RES_SVC_LEN 11 + 3
848 void felica_dump_lite_s(void) {
850 uint8_t poll
[10] = { 0xb2, 0x4d, 0x06, FELICA_POLL_REQ
, 0xff, 0xff, 0x00, 0x00, 0x09, 0x21};
851 uint16_t liteblks
[28] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x90, 0x91, 0x92, 0xa0};
854 iso18092_setup(FPGA_HF_ISO18092_FLAG_READER
| FPGA_HF_ISO18092_FLAG_NOMOD
);
858 uint16_t cnt
= 0, cntfails
= 0;
859 uint8_t *dest
= BigBuf_get_addr();
861 while ((BUTTON_PRESS() == false) && (data_available() == false)) {
864 //TransmitFor18092_AsReader(poll, 10, GetCountSspClk()+512, 1, 0);
865 TransmitFor18092_AsReader(poll
, 10, NULL
, 1, 0);
867 if (WaitForFelicaReply(512) && FelicaFrame
.framebytes
[3] == FELICA_POLL_ACK
) {
868 // copy 8bytes to ndef.
869 memcpy(ndef
, FelicaFrame
.framebytes
+ 4, 8);
870 // for (c=0; c < 8; c++)
871 // ndef[c] = FelicaFrame.framebytes[c+4];
873 for (blknum
= 0; blknum
< ARRAYLEN(liteblks
);) {
875 BuildFliteRdblk(ndef
, 1, &liteblks
[blknum
]);
877 //TransmitFor18092_AsReader(frameSpace, frameSpace[2]+4, GetCountSspClk()+512, 1, 0);
879 TransmitFor18092_AsReader(frameSpace
, frameSpace
[2] + 4, NULL
, 1, 0);
881 if (WaitForFelicaReply(1024) && FelicaFrame
.framebytes
[3] == FELICA_RDBLK_ACK
) {
883 dest
[cnt
++] = liteblks
[blknum
];
885 const uint8_t *fb
= FelicaFrame
.framebytes
;
886 dest
[cnt
++] = fb
[12];
887 dest
[cnt
++] = fb
[13];
889 //memcpy(dest+cnt, FelicaFrame.framebytes + 15, 16);
891 for (uint8_t j
= 0; j
< 16; j
++) {
892 dest
[cnt
++] = fb
[15 + j
];
899 // Dbprintf("LEN %u | Dump bytes count %u ", FelicaFrame.len, cnt);
900 // Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes + 15, 0);
916 // Resetting Frame mode (First set in fpgaloader.c)
917 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
919 // setting tracelen - important! it was set by buffer overflow before
920 // iceman: is this still needed?!?
922 reply_mix(CMD_ACK
, isOK
, cnt
, 0, 0, 0);