2 #include "proxmark3_arm.h"
7 #include "fpgaloader.h"
9 #include "commonutil.h"
15 // minimum time between the start bits of consecutive transfers from reader to tag: 6800 carrier (13.56MHz) cycles
16 #ifndef FELICA_REQUEST_GUARD_TIME
17 # define FELICA_REQUEST_GUARD_TIME (6800/16 + 1) // 426
19 // FRAME DELAY TIME 2672 carrier cycles
20 #ifndef FELICA_FRAME_DELAY_TIME
21 # define FELICA_FRAME_DELAY_TIME (2672/16 + 1) // 168
23 #ifndef DELAY_AIR2ARM_AS_READER
24 #define DELAY_AIR2ARM_AS_READER (3 + 16 + 8 + 8*16 + 4*16 - 8*16) // 91
26 #ifndef DELAY_ARM2AIR_AS_READER
27 #define DELAY_ARM2AIR_AS_READER (4*16 + 8*16 + 8 + 8 + 1) // 209
29 #define AddCrc(data, len) compute_crc(CRC_FELICA, (data), (len), (data)+(len)+1, (data)+(len))
31 static uint32_t felica_timeout
;
32 static uint32_t felica_nexttransfertime
;
33 static uint32_t felica_lasttime_prox2air_start
;
35 static void iso18092_setup(uint8_t fpga_minor_mode
);
36 static uint8_t felica_select_card(felica_card_select_t
*card
);
37 static void TransmitFor18092_AsReader(uint8_t *frame
, int len
, uint32_t *timing
, uint8_t power
, uint8_t highspeed
);
38 static bool WaitForFelicaReply(uint16_t maxbytes
);
40 static void iso18092_set_timeout(uint32_t timeout
) {
41 felica_timeout
= timeout
+ (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / (16 * 8) + 2;
44 static uint32_t iso18092_get_timeout(void) {
45 return felica_timeout
- (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / (16 * 8) - 2;
48 #ifndef FELICA_MAX_FRAME_SIZE
49 #define FELICA_MAX_FRAME_SIZE 260
52 //structure to hold outgoing NFC frame
53 static uint8_t frameSpace
[FELICA_MAX_FRAME_SIZE
+ 4];
55 //structure to hold incoming NFC frame, used for ISO/IEC 18092-compatible frames
66 uint16_t shiftReg
; //for synchronization and offset calculation
73 //should be enough. maxlen is 255, 254 for data, 2 for sync, 2 for crc
74 // 0,1 -> SYNC, 2 - len, 3-(len+1)->data, then crc
77 //b2 4d is SYNC, 45645 in 16-bit notation, 10110010 01001101 binary. Frame will not start filling until this is shifted in
78 //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
80 # define SYNC_16BIT 0xB24D
83 static void FelicaFrameReset(void) {
84 FelicaFrame
.state
= STATE_UNSYNCD
;
85 FelicaFrame
.posCnt
= 0;
86 FelicaFrame
.crc_ok
= false;
87 FelicaFrame
.byte_offset
= 0;
89 static void FelicaFrameinit(uint8_t *data
) {
90 FelicaFrame
.framebytes
= data
;
94 //shift byte into frame, reversing it at the same time
95 static void shiftInByte(uint8_t bt
) {
97 for (j
= 0; j
< FelicaFrame
.byte_offset
; j
++) {
98 FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] = (FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] << 1) + (bt
& 1);
101 FelicaFrame
.posCnt
++;
102 FelicaFrame
.rem_len
--;
103 for (j
= FelicaFrame
.byte_offset
; j
< 8; j
++) {
104 FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] = (FelicaFrame
.framebytes
[FelicaFrame
.posCnt
] << 1) + (bt
& 1);
109 static void Process18092Byte(uint8_t bt
) {
110 switch (FelicaFrame
.state
) {
111 case STATE_UNSYNCD
: {
112 //almost any nonzero byte can be start of SYNC. SYNC should be preceded by zeros, but that is not always the case
114 FelicaFrame
.shiftReg
= reflect8(bt
);
115 FelicaFrame
.state
= STATE_TRYING_SYNC
;
119 case STATE_TRYING_SYNC
: {
122 FelicaFrame
.shiftReg
= bt
;
123 FelicaFrame
.state
= STATE_UNSYNCD
;
125 for (uint8_t i
= 0; i
< 8; i
++) {
127 if (FelicaFrame
.shiftReg
== SYNC_16BIT
) {
129 FelicaFrame
.state
= STATE_GET_LENGTH
;
130 FelicaFrame
.framebytes
[0] = 0xb2;
131 FelicaFrame
.framebytes
[1] = 0x4d;
132 FelicaFrame
.byte_offset
= i
;
133 //shift in remaining byte, slowly...
134 for (uint8_t j
= i
; j
< 8; j
++) {
135 FelicaFrame
.framebytes
[2] = (FelicaFrame
.framebytes
[2] << 1) + (bt
& 1);
139 FelicaFrame
.posCnt
= 2;
143 FelicaFrame
.shiftReg
= (FelicaFrame
.shiftReg
<< 1) + (bt
& 1);
147 //that byte was last byte of sync
148 if (FelicaFrame
.shiftReg
== SYNC_16BIT
) {
149 //Force SYNC on next byte
150 FelicaFrame
.state
= STATE_GET_LENGTH
;
151 FelicaFrame
.framebytes
[0] = 0xb2;
152 FelicaFrame
.framebytes
[1] = 0x4d;
153 FelicaFrame
.byte_offset
= 0;
154 FelicaFrame
.posCnt
= 1;
159 case STATE_GET_LENGTH
: {
161 FelicaFrame
.rem_len
= FelicaFrame
.framebytes
[2] - 1;
162 FelicaFrame
.len
= FelicaFrame
.framebytes
[2] + 4; //with crc and sync
163 FelicaFrame
.state
= STATE_GET_DATA
;
166 case STATE_GET_DATA
: {
168 if (FelicaFrame
.rem_len
<= 0) {
169 FelicaFrame
.state
= STATE_GET_CRC
;
170 FelicaFrame
.rem_len
= 2;
174 case STATE_GET_CRC
: {
176 if (FelicaFrame
.rem_len
<= 0) {
177 FelicaFrame
.rem_len
= 0;
178 // skip sync 2bytes. IF ok, residue should be 0x0000
179 FelicaFrame
.crc_ok
= check_crc(CRC_FELICA
, FelicaFrame
.framebytes
+ 2, FelicaFrame
.len
- 2);
180 FelicaFrame
.state
= STATE_FULL
;
184 case STATE_FULL
: //ignore byte. Don't forget to clear frame to receive next one...
190 /* Perform FeliCa polling card
191 * Currently does NOT do any collision handling.
192 * It expects 0-1 cards in the device's range.
193 * return 0 if selection was successful
195 static uint8_t felica_select_card(felica_card_select_t
*card
) {
198 // 0xB2 0x4B = sync code
201 // 0xff = system code service
202 // 0xff = system code service
203 // 0x00 = request code
204 // b7 = automatic switching of data rate
206 // b1 = fc/32 (414kbps)
207 // b0 = fc/64 (212kbps)
210 static uint8_t poll
[10] = {0xb2, 0x4d, 0x06, FELICA_POLL_REQ
, 0xFF, 0xFF, 0x00, 0x00, 0x09, 0x21};
213 // We try 10 times, or if answer was received.
215 // end-of-reception response packet data, wait approx. 501μs
216 // end-of-transmission command packet data, wait approx. 197μs
218 TransmitFor18092_AsReader(poll
, sizeof(poll
), NULL
, 1, 0);
220 // polling card, break if success
221 if (WaitForFelicaReply(1024) && FelicaFrame
.framebytes
[3] == FELICA_POLL_ACK
)
230 if (DBGLEVEL
>= DBG_DEBUG
)
231 Dbprintf("Error: Time out card selection!");
236 if (FelicaFrame
.framebytes
[3] != FELICA_POLL_ACK
) {
237 if (DBGLEVEL
>= DBG_DEBUG
)
238 Dbprintf("Error: Wrong answer selecting card!");
242 // VALIDATE CRC residue is 0, hence if crc is a value it failed.
243 if (!check_crc(CRC_FELICA
, FelicaFrame
.framebytes
+ 2, FelicaFrame
.len
- 2)) {
244 if (DBGLEVEL
>= DBG_DEBUG
) {
245 Dbprintf("Error: CRC check failed!");
246 Dbprintf("CRC check was done on Frame: ");
247 Dbhexdump(FelicaFrame
.len
- 2, FelicaFrame
.framebytes
+ 2, 0);
252 if (DBGLEVEL
>= DBG_DEBUG
)
253 Dbprintf("Card selection successful!");
257 memcpy(card
->IDm
, FelicaFrame
.framebytes
+ 4, 8);
258 memcpy(card
->PMm
, FelicaFrame
.framebytes
+ 4 + 8, 8);
259 //memcpy(card->servicecode, FelicaFrame.framebytes + 4 + 8 + 8, 2);
260 memcpy(card
->code
, card
->IDm
, 2);
261 memcpy(card
->uid
, card
->IDm
+ 2, 6);
262 memcpy(card
->iccode
, card
->PMm
, 2);
263 memcpy(card
->mrt
, card
->PMm
+ 2, 6);
264 if (DBGLEVEL
>= DBG_DEBUG
) {
265 Dbprintf("Received Frame: ");
266 Dbhexdump(FelicaFrame
.len
, FelicaFrame
.framebytes
, 0);
269 // more status bytes?
273 // poll-0: 0xb2,0x4d,0x06,0x00,0xff,0xff,0x00,0x00,0x09,0x21,
274 // resp: 0xb2,0x4d,0x12,0x01,0x01,0x2e,0x3d,0x17,0x26,0x47,0x80,0x95,0x00,0xf1,0x00,0x00,0x00,0x01,0x43,0x00,0xb3,0x7f,
275 // poll-1 (reply with available system codes - NFC Tag3 specs, IIRC): 0xb2,0x4d,0x06,0x00,0xff,0xff,0x01,0x00,0x3a,0x10
276 // 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,
277 // page-req: 0xb2,0x4d,0x10,0x06, 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX, 0x01, 0x0b,0x00, 0x01, 0x80,0x00, 0x2e,0xb3,
278 // page-req: 0x06, IDm(8), ServiceNum(1),Slist(2*num) BLocknum (1) BLockids(2-3*num)
279 // 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,
281 // builds a readblock frame for felica lite(s). Using SERVICE: SERVICE_FELICA_LITE_READONLY
282 // Felica standard has a different file system, AFAIK,
283 // 8-byte IDm, number of blocks, blocks numbers
284 // number of blocks limited to 4 for FelicaLite(S)
285 static void BuildFliteRdblk(uint8_t *idm
, int blocknum
, uint16_t *blocks
) {
286 if (blocknum
> 4 || blocknum
<= 0)
287 Dbprintf("Invalid number of blocks, %d != 4", blocknum
);
289 uint8_t c
= 0, i
= 0;
292 frameSpace
[c
++] = 0xb2;
293 frameSpace
[c
++] = 0x4d;
295 c
++; //set length later
297 frameSpace
[c
++] = FELICA_RDBLK_REQ
; //command number
299 //card IDm, from poll
300 frameSpace
[c
++] = idm
[0];
301 frameSpace
[c
++] = idm
[1];
302 frameSpace
[c
++] = idm
[2];
303 frameSpace
[c
++] = idm
[3];
304 frameSpace
[c
++] = idm
[4];
305 frameSpace
[c
++] = idm
[5];
306 frameSpace
[c
++] = idm
[6];
307 frameSpace
[c
++] = idm
[7];
310 frameSpace
[c
++] = 0x01;
313 frameSpace
[c
++] = (SERVICE_FELICA_LITE_READONLY
>> 8);
314 frameSpace
[c
++] = SERVICE_FELICA_LITE_READONLY
& 0xFF;
317 frameSpace
[c
++] = blocknum
;
319 for (i
= 0; i
< blocknum
; i
++) {
322 if (blocks
[i
] >= 256) {
323 frameSpace
[c
++] = 0x00;
324 frameSpace
[c
++] = (blocks
[i
] >> 8); //block number, little endian....
325 frameSpace
[c
++] = (blocks
[i
] & 0xff);
327 frameSpace
[c
++] = 0x80;
328 frameSpace
[c
++] = blocks
[i
];
333 frameSpace
[2] = c
- 2;
335 AddCrc(frameSpace
+ 2, c
- 2);
338 static void TransmitFor18092_AsReader(uint8_t *frame
, int len
, uint32_t *timing
, uint8_t power
, uint8_t highspeed
) {
339 uint16_t flags
= FPGA_MAJOR_MODE_HF_ISO18092
;
341 flags
|= FPGA_HF_ISO18092_FLAG_READER
;
343 flags
|= FPGA_HF_ISO18092_FLAG_424K
;
345 FpgaWriteConfWord(flags
);
347 uint32_t curr_transfer_time
= ((MAX(felica_nexttransfertime
, GetCountSspClk()) & 0xfffffff8) + 8);
349 while (GetCountSspClk() < curr_transfer_time
) {};
351 felica_lasttime_prox2air_start
= curr_transfer_time
;
354 // sending 0x00 0x00 0x00 0x00 0x00 0x00
357 // keep tx buffer in a defined state anyway.
358 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
359 AT91C_BASE_SSC
->SSC_THR
= 0x00;
363 // sending data with sync bytes
365 if (DBGLEVEL
>= DBG_DEBUG
) {
366 Dbprintf("Sending frame:");
367 Dbhexdump(len
, frame
, 0);
371 // Put byte into tx holding register as soon as it is ready
372 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
373 AT91C_BASE_SSC
->SSC_THR
= frame
[c
++];
378 while (!(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))) {};
379 AT91C_BASE_SSC
->SSC_THR
= 0x00; //minimum delay
381 while (!(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))) {};
382 AT91C_BASE_SSC
->SSC_THR
= 0x00; //spin
389 (felica_lasttime_prox2air_start
<< 4) + DELAY_ARM2AIR_AS_READER
,
390 ((felica_lasttime_prox2air_start
+ felica_lasttime_prox2air_start
) << 4) + DELAY_ARM2AIR_AS_READER
,
395 felica_nexttransfertime
= MAX(felica_nexttransfertime
, felica_lasttime_prox2air_start
+ FELICA_REQUEST_GUARD_TIME
);
398 // Wait for tag reply
399 // stop when button is pressed
400 // or return TRUE when command is captured
401 bool WaitForFelicaReply(uint16_t maxbytes
) {
402 if (DBGLEVEL
>= DBG_DEBUG
)
403 Dbprintf("WaitForFelicaReply Start");
405 // power, no modulation
406 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092
| FPGA_HF_ISO18092_FLAG_READER
| FPGA_HF_ISO18092_FLAG_NOMOD
);
410 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
413 uint32_t timeout
= iso18092_get_timeout();
417 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
418 b
= (uint8_t)(AT91C_BASE_SSC
->SSC_RHR
);
420 if (FelicaFrame
.state
== STATE_FULL
) {
421 felica_nexttransfertime
= MAX(felica_nexttransfertime
,
422 (GetCountSspClk() & 0xfffffff8) - (DELAY_AIR2ARM_AS_READER
+ DELAY_ARM2AIR_AS_READER
) / 16 + FELICA_FRAME_DELAY_TIME
);
425 FelicaFrame
.framebytes
,
427 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
- timeout
,
428 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
,
432 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("All bytes received! STATE_FULL");
434 } else if (c
++ > timeout
&& (FelicaFrame
.state
== STATE_UNSYNCD
|| FelicaFrame
.state
== STATE_TRYING_SYNC
)) {
435 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("Error: Timeout! STATE_UNSYNCD");
442 // Set up FeliCa communication (similar to iso14443a_setup)
443 // field is setup for "Sending as Reader"
444 static void iso18092_setup(uint8_t fpga_minor_mode
) {
445 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("Start iso18092_setup");
448 FpgaDownloadAndGo(FPGA_BITSTREAM_HF_FELICA
);
450 // allocate command receive buffer
452 BigBuf_Clear_ext(false);
454 // Initialize Demod and Uart structs
455 // DemodInit(BigBuf_malloc(MAX_FRAME_SIZE));
456 FelicaFrameinit(BigBuf_malloc(FELICA_MAX_FRAME_SIZE
));
458 felica_nexttransfertime
= 2 * DELAY_ARM2AIR_AS_READER
;
459 iso18092_set_timeout(2120); // 106 * 20ms maximum start-up time of card
461 init_table(CRC_FELICA
);
463 // connect Demodulated Signal to ADC:
464 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
466 // Set up the synchronous serial port
467 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO18092
);
469 // LSB transfer. Remember to set it back to MSB with
470 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
472 // Signal field is on with the appropriate LED
473 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092
| fpga_minor_mode
);
475 //20.4 ms generate field, start sending polling command afterwars.
484 static void felica_reset_frame_mode(void) {
486 //Resetting Frame mode (First set in fpgaloader.c)
487 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
491 //-----------------------------------------------------------------------------
492 // RAW FeliCa commands. Send out commands and store answers.
493 //-----------------------------------------------------------------------------
495 // arg1 len of commandbytes
496 // d.asBytes command bytes to send
497 void felica_sendraw(PacketCommandNG
*c
) {
498 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("FeliCa_sendraw Enter");
500 felica_command_t param
= c
->oldarg
[0];
501 size_t len
= c
->oldarg
[1] & 0xffff;
502 uint8_t *cmd
= c
->data
.asBytes
;
505 felica_card_select_t card
;
507 if ((param
& FELICA_CONNECT
))
508 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("Clear trace");
512 iso18092_setup(FPGA_HF_ISO18092_FLAG_READER
| FPGA_HF_ISO18092_FLAG_NOMOD
);
514 if ((param
& FELICA_CONNECT
)) {
515 // notify client selecting status.
516 // if failed selecting, turn off antenna and quite.
517 if (!(param
& FELICA_NO_SELECT
)) {
518 arg0
= felica_select_card(&card
);
519 reply_mix(CMD_ACK
, arg0
, sizeof(card
.uid
), 0, &card
, sizeof(felica_card_select_t
));
521 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("Error: Failed selecting card! ");
522 felica_reset_frame_mode();
527 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("No card selection");
530 if ((param
& FELICA_RAW
)) {
532 // 2 sync, 1 len, 2crc == 5
533 uint8_t *buf
= BigBuf_malloc(len
+ 5);
540 memcpy(buf
+ 2, cmd
, len
);
542 if ((param
& FELICA_APPEND_CRC
)) {
543 // Don't append crc on empty bytearray...
548 if (DBGLEVEL
>= DBG_DEBUG
) {
549 Dbprintf("Transmit Frame (no CRC shown):");
550 Dbhexdump(len
, buf
, 0);
551 Dbprintf("Buffer Length: %i", buf
[2] + 4);
553 TransmitFor18092_AsReader(buf
, buf
[2] + 4, NULL
, 1, 0);
554 arg0
= WaitForFelicaReply(1024);
555 if (DBGLEVEL
>= DBG_DEBUG
) {
556 Dbprintf("Received Frame Code: %d", arg0
);
557 Dbhexdump(FelicaFrame
.len
, FelicaFrame
.framebytes
, 0);
560 uint32_t result
= reply_mix(CMD_ACK
, FelicaFrame
.len
, arg0
, 0, FelicaFrame
.framebytes
, FelicaFrame
.len
);
562 Dbprintf("Reply to Client Error Code: %i", result
);
565 if ((param
& FELICA_NO_DISCONNECT
)) {
566 Dbprintf("Disconnect");
568 if (DBGLEVEL
>= DBG_DEBUG
)
569 Dbprintf("FeliCa_sendraw Exit");
570 felica_reset_frame_mode();
574 void felica_sniff(uint32_t samplesToSkip
, uint32_t triggersToSkip
) {
578 iso18092_setup(FPGA_HF_ISO18092_FLAG_NOMOD
);
582 int retval
= PM3_SUCCESS
;
583 int remFrames
= (samplesToSkip
) ? samplesToSkip
: 0;
585 uint32_t timeout
= iso18092_get_timeout();
586 bool isReaderFrame
= true;
589 uint16_t checker
= 0;
594 // since simulation is a tight time critical loop,
595 // we only check for user request to end at iteration 3000, 9000.
597 if (data_available()) {
598 retval
= PM3_EOPABORTED
;
604 if (checker
>= 3000) {
606 if (BUTTON_PRESS()) {
607 retval
= PM3_EOPABORTED
;
615 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
617 uint8_t dist
= (uint8_t)(AT91C_BASE_SSC
->SSC_RHR
);
618 Process18092Byte(dist
);
620 if ((dist
>= 178) && (++trigger_cnt
> triggersToSkip
)) {
621 Dbprintf("triggers To skip kicked %d", dist
);
624 if (FelicaFrame
.state
== STATE_FULL
) {
625 if ((FelicaFrame
.framebytes
[3] % 2) == 0) {
626 isReaderFrame
= true; // All Reader Frames are even and all Tag frames are odd
628 isReaderFrame
= false;
631 if (remFrames
<= 0) {
632 Dbprintf("Stop Sniffing - samples To skip reached!");
635 LogTrace(FelicaFrame
.framebytes
,
637 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
- timeout
,
638 ((GetCountSspClk() & 0xfffffff8) << 4) - DELAY_AIR2ARM_AS_READER
,
648 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
650 Dbprintf("Felica sniffing done, tracelen: %i", BigBuf_get_traceLen());
651 reply_ng(CMD_HF_FELICA_SNIFF
, retval
, NULL
, 0);
655 #define R_POLL0_LEN 0x16
656 #define R_POLL1_LEN 0x18
657 #define R_READBLK_LEN 0x21
658 //simulate NFC Tag3 card - for now only poll response works
659 // second half (4 bytes) of NDEF2 goes into nfcid2_0, first into nfcid2_1
660 void felica_sim_lite(uint8_t *uid
) {
662 // prepare our 3 responses...
663 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};
664 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};
665 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};
667 // NFC tag 3/ ISo technically. Many overlapping standards
668 DbpString("Felica Lite-S simulation start");
669 Dbprintf("NDEF2 UID: %02x %02x %02x %02x %02x %02x %02x %02x",
670 uid
[0], uid
[1], uid
[2], uid
[3], uid
[4], uid
[5], uid
[6], uid
[7]
674 for (uint8_t i
= 0; i
< 8; i
++) {
675 resp_poll0
[i
+ 4] = uid
[i
];
676 resp_poll1
[i
+ 4] = uid
[i
];
677 resp_readblk
[i
+ 4] = uid
[i
];
680 // calculate and set CRC
681 AddCrc(resp_poll0
, resp_poll0
[2]);
682 AddCrc(resp_poll1
, resp_poll1
[2]);
683 AddCrc(resp_readblk
, resp_readblk
[2]);
685 iso18092_setup(FPGA_HF_ISO18092_FLAG_NOMOD
);
687 int retval
= PM3_SUCCESS
;
689 uint8_t *curresp
= NULL
;
690 bool listenmode
= true;
691 // uint32_t frtm = GetCountSspClk();
694 uint16_t checker
= 0;
699 // since simulation is a tight time critical loop,
700 // we only check for user request to end at iteration 3000, 9000.
702 if (data_available()) {
703 retval
= PM3_EOPABORTED
;
709 if (checker
>= 3000) {
711 if (BUTTON_PRESS()) {
712 retval
= PM3_EOPABORTED
;
723 // waiting for request...
724 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
726 uint8_t dist
= (uint8_t)(AT91C_BASE_SSC
->SSC_RHR
);
727 // frtm = GetCountSspClk();
728 Process18092Byte(dist
);
730 if (FelicaFrame
.state
== STATE_FULL
) {
732 if (FelicaFrame
.crc_ok
) {
734 if (FelicaFrame
.framebytes
[2] == 6 && FelicaFrame
.framebytes
[3] == 0) {
736 // polling... there are two types of polling we answer to
737 if (FelicaFrame
.framebytes
[6] == 0) {
738 curresp
= resp_poll0
;
739 curlen
= R_POLL0_LEN
;
742 if (FelicaFrame
.framebytes
[6] == 1) {
743 curresp
= resp_poll1
;
744 curlen
= R_POLL1_LEN
;
749 if (FelicaFrame
.framebytes
[2] > 5 && FelicaFrame
.framebytes
[3] == 0x06) {
750 // we should rebuild it depending on page size, but...
752 curresp
= resp_readblk
;
753 curlen
= R_READBLK_LEN
;
759 // frame invalid, clear it out to allow for the next one
767 if (listenmode
== false) {
768 // trying to answer... here to start answering immediately.
769 // this one is a bit finicky. Seems that being a bit late is better than earlier
770 // TransmitFor18092_AsReader(curresp, curlen, frtm+512, 0, 0);
771 TransmitFor18092_AsReader(curresp
, curlen
, NULL
, 0, 0);
774 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092
| FPGA_HF_ISO18092_FLAG_NOMOD
);
786 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
788 Dbprintf("FeliCa Lite-S emulator stopped. Trace length: %d ", BigBuf_get_traceLen());
789 reply_ng(CMD_HF_FELICALITE_SIMULATE
, retval
, NULL
, 0);
792 #define RES_SVC_LEN 11 + 3
794 void felica_dump_lite_s(void) {
796 uint8_t poll
[10] = { 0xb2, 0x4d, 0x06, FELICA_POLL_REQ
, 0xff, 0xff, 0x00, 0x00, 0x09, 0x21};
797 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};
800 iso18092_setup(FPGA_HF_ISO18092_FLAG_READER
| FPGA_HF_ISO18092_FLAG_NOMOD
);
804 uint16_t cnt
= 0, cntfails
= 0;
805 uint8_t *dest
= BigBuf_get_addr();
807 while (!BUTTON_PRESS() && !data_available()) {
810 //TransmitFor18092_AsReader(poll, 10, GetCountSspClk()+512, 1, 0);
811 TransmitFor18092_AsReader(poll
, 10, NULL
, 1, 0);
813 if (WaitForFelicaReply(512) && FelicaFrame
.framebytes
[3] == FELICA_POLL_ACK
) {
814 // copy 8bytes to ndef.
815 memcpy(ndef
, FelicaFrame
.framebytes
+ 4, 8);
816 // for (c=0; c < 8; c++)
817 // ndef[c] = FelicaFrame.framebytes[c+4];
819 for (blknum
= 0; blknum
< ARRAYLEN(liteblks
);) {
821 BuildFliteRdblk(ndef
, 1, &liteblks
[blknum
]);
823 //TransmitFor18092_AsReader(frameSpace, frameSpace[2]+4, GetCountSspClk()+512, 1, 0);
827 TransmitFor18092_AsReader(frameSpace
, frameSpace
[2] + 4, NULL
, 1, 0);
829 if (WaitForFelicaReply(1024) && FelicaFrame
.framebytes
[3] == FELICA_RDBLK_ACK
) {
831 dest
[cnt
++] = liteblks
[blknum
];
833 uint8_t *fb
= FelicaFrame
.framebytes
;
834 dest
[cnt
++] = fb
[12];
835 dest
[cnt
++] = fb
[13];
837 //memcpy(dest+cnt, FelicaFrame.framebytes + 15, 16);
839 for (uint8_t j
= 0; j
< 16; j
++)
840 dest
[cnt
++] = fb
[15 + j
];
846 // Dbprintf("LEN %u | Dump bytes count %u ", FelicaFrame.len, cnt);
847 Dbhexdump(FelicaFrame
.len
, FelicaFrame
.framebytes
+ 15, 0);
863 //Resetting Frame mode (First set in fpgaloader.c)
864 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF
| SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
866 //setting tracelen - important! it was set by buffer overflow before
868 reply_mix(CMD_ACK
, isOK
, cnt
, 0, 0, 0);