Pass the requested system code to firmware selection so default and system-specific reader modes share one polling path. Keep the RF field active across retries for the system-specific mode. Co-Authored-By: Codex (GPT-6) <noreply@openai.com>
1147 lines
40 KiB
C
1147 lines
40 KiB
C
//-----------------------------------------------------------------------------
|
|
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// See LICENSE.txt for the text of the license.
|
|
//-----------------------------------------------------------------------------
|
|
#include "felica.h"
|
|
#include "proxmark3_arm.h"
|
|
#include "BigBuf.h"
|
|
#include "util.h"
|
|
#include "protocols.h"
|
|
#include "crc16.h"
|
|
#include "fpga_loader.h"
|
|
#include "string.h"
|
|
#include "commonutil.h"
|
|
#include "dbprint.h"
|
|
#include "ticks_apis.h"
|
|
#include "fpga_apis.h"
|
|
#include "rssi_apis.h"
|
|
#include "iso18.h"
|
|
|
|
#define AddCrc(data, len) compute_crc(CRC_FELICA, (data), (len), (data)+(len)+1, (data)+(len))
|
|
static uint8_t felica_select_card(felica_card_select_t *card, uint16_t system_code, uint8_t request_code);
|
|
|
|
//structure to hold outgoing NFC frame
|
|
static uint8_t frameSpace[FELICA_MAX_RF_FRAME_SIZE];
|
|
|
|
#define FELICA_PREAMBLE_BYTES 6U
|
|
|
|
// how much raw demodulator output to keep for `hw dbg -4` diagnostics
|
|
#define FELICA_RAW_CAPTURE_BYTES 128U
|
|
|
|
// Keep a conservative reader-to-reader guard of one FeliCa polling slot-0 wait
|
|
// (512 bit periods). The spec minimum of 6800 carrier periods is shorter.
|
|
#ifndef FELICA_REQUEST_GUARD_TIME
|
|
# define FELICA_REQUEST_GUARD_TIME ((512 + 0 * 256) + 1)
|
|
#endif
|
|
// FRAME DELAY TIME 2672 carrier cycles
|
|
#ifndef FELICA_FRAME_DELAY_TIME
|
|
# define FELICA_FRAME_DELAY_TIME (FELICA_212K_CARRIER_TO_TIMER_TICKS(2672) + 1)
|
|
#endif
|
|
|
|
//b2 4d is SYNC, 45645 in 16-bit notation, 10110010 01001101 binary. Frame will not start filling until this is shifted in
|
|
//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
|
|
#ifndef SYNC_16BIT
|
|
# define SYNC_16BIT 0xB24D
|
|
#endif
|
|
|
|
static uint32_t felica_timeout;
|
|
uint32_t felica_nexttransfertime;
|
|
static uint32_t felica_lasttime_prox2air_start;
|
|
static bool felica_field_active;
|
|
// when set, the FPGA streams envelope peak-to-peak instead of demodulated bits
|
|
static bool felica_probe_mode = false;
|
|
|
|
felica_frame_t FelicaFrame;
|
|
|
|
static void iso18092_set_timeout(uint32_t timeout) {
|
|
felica_timeout = timeout + (DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) / (FELICA_212K_CARRIER_PERIODS_PER_BIT * FELICA_BITS_PER_BYTE) + 2;
|
|
}
|
|
|
|
static uint32_t iso18092_get_timeout(void) {
|
|
return felica_timeout - (DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) / (FELICA_212K_CARRIER_PERIODS_PER_BIT * FELICA_BITS_PER_BYTE) - 2;
|
|
}
|
|
|
|
bool felica_field_is_active(void) {
|
|
return felica_field_active;
|
|
}
|
|
|
|
void FelicaFrameReset(felica_frame_t *f) {
|
|
f->state = STATE_UNSYNCD;
|
|
f->posCnt = 0;
|
|
f->shiftReg = 0;
|
|
f->shiftRegInv = 0;
|
|
f->crc_ok = false;
|
|
f->rem_len = 0;
|
|
f->len = 0;
|
|
f->byte_offset = 0;
|
|
f->polarity = FELICA_POLARITY_UNKNOWN;
|
|
f->startTime = 0;
|
|
f->endTime = 0;
|
|
}
|
|
|
|
static void FelicaFrameinit(felica_frame_t *f, uint8_t *data) {
|
|
f->framebytes = data;
|
|
FelicaFrameReset(f);
|
|
}
|
|
|
|
uint32_t felica_timer_to_carrier_periods(uint32_t timer_ticks, bool highspeed) {
|
|
return timer_ticks * (highspeed ? FELICA_424K_CARRIER_PERIODS_PER_BIT : FELICA_212K_CARRIER_PERIODS_PER_BIT);
|
|
}
|
|
|
|
uint32_t felica_get_rx_byte_start_time(void) {
|
|
return (GetCountSspClk() & 0xfffffff8) - FELICA_BITS_PER_BYTE;
|
|
}
|
|
|
|
//shift byte into frame, reversing it at the same time
|
|
static void shiftInByte(felica_frame_t *f, uint8_t bt, uint32_t byte_start_time) {
|
|
uint8_t j;
|
|
for (j = 0; j < f->byte_offset; j++) {
|
|
f->framebytes[f->posCnt] = (f->framebytes[f->posCnt] << 1) + (bt & 1);
|
|
bt >>= 1;
|
|
}
|
|
if (f->byte_offset > 0) {
|
|
f->endTime = byte_start_time + f->byte_offset;
|
|
}
|
|
f->posCnt++;
|
|
f->rem_len--;
|
|
for (j = f->byte_offset; j < 8; j++) {
|
|
f->framebytes[f->posCnt] = (f->framebytes[f->posCnt] << 1) + (bt & 1);
|
|
bt >>= 1;
|
|
}
|
|
if (f->byte_offset == 0) {
|
|
f->endTime = byte_start_time + FELICA_BITS_PER_BYTE;
|
|
}
|
|
}
|
|
|
|
void Process18092Byte(felica_frame_t *f, uint8_t bt, uint32_t byte_start_time) {
|
|
|
|
switch (f->state) {
|
|
|
|
case STATE_UNSYNCD: {
|
|
// almost any nonzero byte can be start of SYNC. SYNC should be preceded by zeros, but that is not always the case
|
|
if (bt > 0) {
|
|
uint8_t btr = reflect8(bt);
|
|
f->shiftReg = btr;
|
|
f->shiftRegInv = (uint8_t)~btr;
|
|
f->polarity = FELICA_POLARITY_UNKNOWN;
|
|
f->state = STATE_TRYING_SYNC;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case STATE_TRYING_SYNC: {
|
|
uint8_t bt_norm = bt;
|
|
uint8_t bt_inv = (uint8_t)~bt;
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
bool sync_normal = (f->shiftReg == SYNC_16BIT);
|
|
bool sync_inverted = (f->shiftRegInv == SYNC_16BIT);
|
|
|
|
if (sync_normal || sync_inverted) {
|
|
bool use_inverted = sync_inverted;
|
|
uint8_t shift_bt = use_inverted ? bt_inv : bt_norm;
|
|
|
|
// SYNC done!
|
|
f->state = STATE_GET_LENGTH;
|
|
f->framebytes[0] = 0xb2;
|
|
f->framebytes[1] = 0x4d;
|
|
f->framebytes[2] = 0x00;
|
|
f->byte_offset = i;
|
|
f->polarity = use_inverted ? FELICA_POLARITY_INVERTED : FELICA_POLARITY_NORMAL;
|
|
f->startTime = byte_start_time + i - (2U * FELICA_BITS_PER_BYTE);
|
|
f->endTime = f->startTime;
|
|
|
|
// shift in remaining byte, slowly...
|
|
for (uint8_t j = i; j < 8; j++) {
|
|
f->framebytes[2] = (f->framebytes[2] << 1) + (shift_bt & 1);
|
|
shift_bt >>= 1;
|
|
}
|
|
|
|
f->posCnt = 2;
|
|
return;
|
|
}
|
|
|
|
f->shiftReg = (f->shiftReg << 1) + (bt_norm & 1);
|
|
f->shiftRegInv = (f->shiftRegInv << 1) + (bt_inv & 1);
|
|
bt_norm >>= 1;
|
|
bt_inv >>= 1;
|
|
}
|
|
|
|
// that byte was last byte of sync
|
|
if (f->shiftReg == SYNC_16BIT || f->shiftRegInv == SYNC_16BIT) {
|
|
bool use_inverted = (f->shiftRegInv == SYNC_16BIT);
|
|
// Force SYNC on next byte
|
|
f->state = STATE_GET_LENGTH;
|
|
f->framebytes[0] = 0xb2;
|
|
f->framebytes[1] = 0x4d;
|
|
f->framebytes[2] = 0x00;
|
|
f->byte_offset = 0;
|
|
f->posCnt = 1;
|
|
f->polarity = use_inverted ? FELICA_POLARITY_INVERTED : FELICA_POLARITY_NORMAL;
|
|
f->startTime = byte_start_time + FELICA_BITS_PER_BYTE - (2U * FELICA_BITS_PER_BYTE);
|
|
f->endTime = f->startTime;
|
|
}
|
|
break;
|
|
}
|
|
case STATE_GET_LENGTH: {
|
|
if (f->polarity == FELICA_POLARITY_INVERTED) {
|
|
bt = (uint8_t)~bt;
|
|
}
|
|
shiftInByte(f, bt, byte_start_time);
|
|
if (f->framebytes[2] == 0 || (f->framebytes[2] + 4 > FELICA_MAX_RF_FRAME_SIZE)) {
|
|
// invalid frame length, drop frame and start over.
|
|
FelicaFrameReset(f);
|
|
break;
|
|
}
|
|
f->rem_len = f->framebytes[2] - 1;
|
|
f->len = f->framebytes[2] + 4; //with crc and sync
|
|
f->state = STATE_GET_DATA;
|
|
break;
|
|
}
|
|
case STATE_GET_DATA: {
|
|
if (f->polarity == FELICA_POLARITY_INVERTED) {
|
|
bt = (uint8_t)~bt;
|
|
}
|
|
shiftInByte(f, bt, byte_start_time);
|
|
if (f->rem_len <= 0) {
|
|
f->state = STATE_GET_CRC;
|
|
f->rem_len = 2;
|
|
}
|
|
break;
|
|
}
|
|
case STATE_GET_CRC: {
|
|
if (f->polarity == FELICA_POLARITY_INVERTED) {
|
|
bt = (uint8_t)~bt;
|
|
}
|
|
shiftInByte(f, bt, byte_start_time);
|
|
if (f->rem_len <= 0) {
|
|
f->rem_len = 0;
|
|
// skip sync 2bytes. IF ok, residue should be 0x0000
|
|
f->crc_ok = check_crc(CRC_FELICA, f->framebytes + 2, f->len - 2);
|
|
f->state = STATE_FULL;
|
|
}
|
|
break;
|
|
}
|
|
case STATE_FULL: //ignore byte. Don't forget to clear frame to receive next one...
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Perform FeliCa polling card
|
|
* Currently does NOT do any collision handling.
|
|
* It expects 0-1 cards in the device's range.
|
|
* return 0 if selection was successful
|
|
*/
|
|
static uint8_t felica_select_card(felica_card_select_t *card, uint16_t system_code, uint8_t request_code) {
|
|
|
|
// POLL command
|
|
// 0xB2 0x4B = sync code
|
|
// 0x06 = len
|
|
// 0x00 = rfu
|
|
// system code service (high byte)
|
|
// system code service (low byte)
|
|
// request code
|
|
// b7 = automatic switching of data rate
|
|
// b6-b2 = reserved
|
|
// b1 = fc/32 (414kbps)
|
|
// b0 = fc/64 (212kbps)
|
|
// 0x00 = timeslot
|
|
// 0x09 0x21 = crc
|
|
uint8_t poll[10] = {0xb2, 0x4d, 0x06, FELICA_POLLING_REQ, 0xFF, 0xFF, 0x00, 0x00, 0x09, 0x21};
|
|
poll[4] = (uint8_t)(system_code >> 8);
|
|
poll[5] = (uint8_t)system_code;
|
|
poll[6] = request_code;
|
|
|
|
// Number of time slots offered to the card(s), the FeliCa polling TSN field.
|
|
// Slot 0 only is what a single card wants, but when several cards share the
|
|
// field they all answer in slot 0 and keep colliding. Widen the window on
|
|
// some attempts so a colliding card gets a slot of its own.
|
|
static const uint8_t timeslots[] = {0x00, 0x00, 0x00, 0x01, 0x03, 0x07};
|
|
|
|
bool got_reply = false;
|
|
|
|
// We try 24 times, or stop early once an answer was received.
|
|
int len = 24;
|
|
do {
|
|
poll[7] = timeslots[(unsigned int)len % ARRAYLEN(timeslots)];
|
|
AddCrc(poll + 2, 6);
|
|
|
|
// end-of-reception response packet data, wait approx. 501µs
|
|
// end-of-transmission command packet data, wait approx. 197µs
|
|
// polling card
|
|
TransmitFor18092_AsReader(poll, sizeof(poll), NULL, 1, 0);
|
|
|
|
// polling card, break if success
|
|
if (WaitForFelicaReply(1024)) {
|
|
if (FelicaFrame.framebytes[3] == FELICA_POLLING_RES) {
|
|
got_reply = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
WDT_HIT();
|
|
|
|
} while (--len);
|
|
|
|
// 1. timed-out. Never inspect FelicaFrame past this point without a reply,
|
|
// it still holds whatever a previous exchange left in it.
|
|
if (got_reply == false) {
|
|
return 1;
|
|
}
|
|
|
|
// 2. wrong answer
|
|
if (FelicaFrame.framebytes[3] != FELICA_POLLING_RES) {
|
|
return 2;
|
|
}
|
|
|
|
// 3. wrong crc. residue is 0, hence if crc is a value it failed.
|
|
// len covers sync(2) + payload + crc(2); anything shorter would underflow
|
|
// the size_t length handed to check_crc().
|
|
if (FelicaFrame.len < 5 ||
|
|
check_crc(CRC_FELICA, FelicaFrame.framebytes + 2, FelicaFrame.len - 2) == false) {
|
|
|
|
if (g_dbglevel >= DBG_DEBUG) {
|
|
Dbprintf("Error: CRC check failed!");
|
|
Dbhexdump(FelicaFrame.len - 2, FelicaFrame.framebytes + 2, 0);
|
|
}
|
|
return 3;
|
|
}
|
|
|
|
// 4. frame too short to contain IDm + PMm (need 4 header + 8 + 8 = 20 bytes)
|
|
if (FelicaFrame.len < 20) {
|
|
if (g_dbglevel >= DBG_DEBUG) {
|
|
Dbprintf("Error: Frame length smaller than 20, got %u", FelicaFrame.len);
|
|
}
|
|
return 4;
|
|
}
|
|
|
|
// copy UID
|
|
// idm 8
|
|
if (card) {
|
|
memcpy(card->IDm, FelicaFrame.framebytes + 4, 8);
|
|
memcpy(card->PMm, FelicaFrame.framebytes + 4 + 8, 8);
|
|
// memcpy(card->servicecode, FelicaFrame.framebytes + 4 + 8 + 8, 2);
|
|
memcpy(card->code, card->IDm, 2);
|
|
memcpy(card->uid, card->IDm + 2, 6);
|
|
memcpy(card->iccode, card->PMm, 2);
|
|
memcpy(card->mrt, card->PMm + 2, 6);
|
|
if (g_dbglevel >= DBG_DEBUG) {
|
|
Dbprintf("Received Frame: ");
|
|
Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0);
|
|
}
|
|
}
|
|
// 0. OK
|
|
return 0;
|
|
}
|
|
|
|
// poll-0: 0xb2,0x4d,0x06,0x00,0xff,0xff,0x00,0x00,0x09,0x21,
|
|
// resp: 0xb2,0x4d,0x12,0x01,0x01,0x2e,0x3d,0x17,0x26,0x47,0x80,0x95,0x00,0xf1,0x00,0x00,0x00,0x01,0x43,0x00,0xb3,0x7f,
|
|
// poll-1 (reply with available system codes - NFC Tag3 specs, IIRC): 0xb2,0x4d,0x06,0x00,0xff,0xff,0x01,0x00,0x3a,0x10
|
|
// 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,
|
|
// page-req: 0xb2,0x4d,0x10,0x06, 0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX,0xXX, 0x01, 0x0b,0x00, 0x01, 0x80,0x00, 0x2e,0xb3,
|
|
// page-req: 0x06, IDm(8), ServiceNum(1),Slist(2*num) BLocknum (1) BLockids(2-3*num)
|
|
// 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,
|
|
|
|
// builds a readblock frame for felica lite(s). Using SERVICE: FELICA_SERVICE_LITE_READONLY
|
|
// Felica standard has a different file system, AFAIK,
|
|
// 8-byte IDm, number of blocks, blocks numbers
|
|
// number of blocks limited to 4 for FelicaLite(S)
|
|
static void BuildFliteRdblk(const uint8_t *idm, uint8_t blocknum, const uint16_t *blocks) {
|
|
|
|
if (blocknum > 4 || blocknum == 0) {
|
|
Dbprintf("Invalid number of blocks, %d, expected 1..4", blocknum);
|
|
// make sure a caller that ignores this cannot transmit a stale frame
|
|
frameSpace[2] = 0;
|
|
return;
|
|
}
|
|
|
|
uint8_t c = 0, i = 0;
|
|
|
|
// Sync bytes
|
|
frameSpace[c++] = 0xb2;
|
|
frameSpace[c++] = 0x4d;
|
|
|
|
c++; // set length later
|
|
|
|
frameSpace[c++] = FELICA_READ_WITHOUT_ENCRYPTION_REQ; // command number
|
|
|
|
// card IDm, from poll
|
|
frameSpace[c++] = idm[0];
|
|
frameSpace[c++] = idm[1];
|
|
frameSpace[c++] = idm[2];
|
|
frameSpace[c++] = idm[3];
|
|
frameSpace[c++] = idm[4];
|
|
frameSpace[c++] = idm[5];
|
|
frameSpace[c++] = idm[6];
|
|
frameSpace[c++] = idm[7];
|
|
|
|
// number of services
|
|
frameSpace[c++] = 0x01;
|
|
|
|
// service code
|
|
frameSpace[c++] = (FELICA_SERVICE_LITE_READONLY >> 8);
|
|
frameSpace[c++] = FELICA_SERVICE_LITE_READONLY & 0xFF;
|
|
|
|
// number of blocks
|
|
frameSpace[c++] = blocknum;
|
|
|
|
for (i = 0; i < blocknum; i++) {
|
|
|
|
// 3-byte block
|
|
if (blocks[i] >= 256) {
|
|
frameSpace[c++] = 0x00;
|
|
frameSpace[c++] = (blocks[i] >> 8); // block number, little endian....
|
|
frameSpace[c++] = (blocks[i] & 0xff);
|
|
} else {
|
|
frameSpace[c++] = 0x80;
|
|
frameSpace[c++] = blocks[i];
|
|
}
|
|
}
|
|
|
|
// set length
|
|
frameSpace[2] = c - 2;
|
|
// Add CRC
|
|
AddCrc(frameSpace + 2, c - 2);
|
|
}
|
|
|
|
void TransmitFor18092_AsReaderEx(const uint8_t *frame, uint16_t len, const uint32_t *NYI_timing_NYI, uint8_t power, uint8_t highspeed, bool reader2tag) {
|
|
|
|
if (NYI_timing_NYI != NULL) {
|
|
DbpString("Error: TransmitFor18092_AsReader does not check or set parameter NYI_timing_NYI");
|
|
return;
|
|
}
|
|
|
|
uint16_t flags = FPGA_MAJOR_MODE_HF_ISO18092;
|
|
|
|
if (power) {
|
|
flags |= FPGA_HF_ISO18092_FLAG_READER;
|
|
}
|
|
|
|
if (highspeed) {
|
|
flags |= FPGA_HF_ISO18092_FLAG_424K;
|
|
}
|
|
|
|
FpgaWriteConfWord(flags);
|
|
|
|
uint32_t curr_transfer_time = ((MAX(felica_nexttransfertime, GetCountSspClk()) & 0xfffffff8) + 8);
|
|
|
|
while (GetCountSspClk() < curr_transfer_time) {};
|
|
|
|
felica_lasttime_prox2air_start = curr_transfer_time;
|
|
|
|
// preamble
|
|
// sending 0x00 0x00 0x00 0x00 0x00 0x00
|
|
uint16_t c = 0;
|
|
while (c < 6) {
|
|
// keep tx buffer in a defined state anyway.
|
|
if (FPGA_SSC_TX_Ready()) {
|
|
FPGA_SSC_TX_Value(0x00);
|
|
c++;
|
|
}
|
|
}
|
|
// sending data with sync bytes
|
|
c = 0;
|
|
|
|
while (c < len) {
|
|
// Put byte into tx holding register as soon as it is ready
|
|
if (FPGA_SSC_TX_Ready()) {
|
|
FPGA_SSC_TX_Value(frame[c++]);
|
|
}
|
|
}
|
|
|
|
/**/
|
|
while (!FPGA_SSC_TX_Ready()) {};
|
|
FPGA_SSC_TX_Value(0x00); //minimum delay
|
|
|
|
while (!FPGA_SSC_TX_Ready()) {};
|
|
FPGA_SSC_TX_Value(0x00); //spin
|
|
/**/
|
|
|
|
const uint32_t frame_start = felica_lasttime_prox2air_start + (FELICA_PREAMBLE_BYTES * FELICA_BITS_PER_BYTE);
|
|
const uint32_t frame_end = frame_start + (len * FELICA_BITS_PER_BYTE);
|
|
|
|
// log
|
|
LogTrace(
|
|
frame,
|
|
len,
|
|
felica_timer_to_carrier_periods(frame_start, highspeed) + DELAY_ARM2AIR_AS_READER,
|
|
felica_timer_to_carrier_periods(frame_end, highspeed) + DELAY_ARM2AIR_AS_READER,
|
|
NULL,
|
|
reader2tag
|
|
);
|
|
|
|
felica_nexttransfertime = MAX(felica_nexttransfertime, felica_lasttime_prox2air_start + FELICA_REQUEST_GUARD_TIME);
|
|
}
|
|
|
|
void TransmitFor18092_AsReader(const uint8_t *frame, uint16_t len, const uint32_t *NYI_timing_NYI, uint8_t power, uint8_t highspeed) {
|
|
TransmitFor18092_AsReaderEx(frame, len, NYI_timing_NYI, power, highspeed, true);
|
|
}
|
|
|
|
// Wait for tag reply
|
|
// stop when button is pressed
|
|
// or return TRUE when command is captured
|
|
bool WaitForFelicaReply(uint16_t maxbytes) {
|
|
|
|
// if (g_dbglevel >= DBG_DEBUG) { Dbprintf("WaitForFelicaReply Start"); }
|
|
|
|
uint32_t c = 0;
|
|
uint32_t rx_bytes = 0;
|
|
uint16_t crc_fail_normal = 0;
|
|
uint16_t crc_fail_inverted = 0;
|
|
|
|
// raw demodulator output, only kept when the user asks for extended debug.
|
|
uint8_t rawcap[FELICA_RAW_CAPTURE_BYTES];
|
|
uint16_t rawcnt = 0;
|
|
|
|
// power, no modulation
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092 | FPGA_HF_ISO18092_FLAG_READER | FPGA_HF_ISO18092_FLAG_NOMOD
|
|
| (felica_probe_mode ? FPGA_HF_ISO18092_FLAG_PROBE : 0));
|
|
FelicaFrameReset(&FelicaFrame);
|
|
|
|
// clear RXRDY:
|
|
uint8_t b = (uint8_t)FPGA_SSC_RX_Value();
|
|
(void)b;
|
|
|
|
uint32_t timeout = iso18092_get_timeout();
|
|
|
|
for (;;) {
|
|
|
|
WDT_HIT();
|
|
|
|
if (FPGA_SSC_RX_Ready()) {
|
|
|
|
b = (uint8_t)(FPGA_SSC_RX_Value());
|
|
rx_bytes++;
|
|
|
|
if (g_dbglevel >= DBG_EXTENDED && rawcnt < sizeof(rawcap)) {
|
|
rawcap[rawcnt++] = b;
|
|
}
|
|
|
|
Process18092Byte(&FelicaFrame, b, felica_get_rx_byte_start_time());
|
|
felica_frame_t *received = NULL;
|
|
|
|
if (FelicaFrame.state == STATE_FULL) {
|
|
if (FelicaFrame.crc_ok) {
|
|
received = &FelicaFrame;
|
|
} else {
|
|
if (FelicaFrame.polarity == FELICA_POLARITY_INVERTED) {
|
|
crc_fail_inverted++;
|
|
} else {
|
|
crc_fail_normal++;
|
|
}
|
|
FelicaFrameReset(&FelicaFrame);
|
|
}
|
|
}
|
|
|
|
if (received != NULL) {
|
|
if (maxbytes && received->len > maxbytes) {
|
|
if (g_dbglevel >= DBG_DEBUG) {
|
|
Dbprintf("FeliCa RX frame dropped (len %u > max %u)", received->len, maxbytes);
|
|
}
|
|
FelicaFrameReset(&FelicaFrame);
|
|
continue;
|
|
}
|
|
|
|
if (g_dbglevel >= DBG_DEBUG && received->polarity == FELICA_POLARITY_INVERTED) {
|
|
DbpString("FeliCa RX decoded using inverted polarity fallback");
|
|
}
|
|
|
|
felica_nexttransfertime = MAX(
|
|
felica_nexttransfertime,
|
|
received->endTime - FELICA_212K_CARRIER_TO_TIMER_TICKS(DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) + FELICA_FRAME_DELAY_TIME);
|
|
|
|
LogTrace(
|
|
received->framebytes,
|
|
received->len,
|
|
felica_timer_to_carrier_periods(received->startTime, false) - DELAY_AIR2ARM_AS_READER,
|
|
felica_timer_to_carrier_periods(received->endTime, false) - DELAY_AIR2ARM_AS_READER,
|
|
NULL,
|
|
false
|
|
);
|
|
return true;
|
|
|
|
} else if (
|
|
(c++ > timeout
|
|
&& (FelicaFrame.state == STATE_UNSYNCD || FelicaFrame.state == STATE_TRYING_SYNC))
|
|
// A frame that never completes (bogus length byte, demodulator stuck
|
|
// mid-frame) leaves the states above, so it would never hit the check
|
|
// above. Bound the total wait as well.
|
|
|| (rx_bytes > timeout + FELICA_MAX_RF_FRAME_SIZE)
|
|
) {
|
|
|
|
// if (g_dbglevel >= DBG_DEBUG) Dbprintf("Error: Timeout! STATE_UNSYNCD");
|
|
if (g_dbglevel >= DBG_DEBUG && (crc_fail_normal || crc_fail_inverted)) {
|
|
Dbprintf("FeliCa RX timeout, CRC fails normal=%u inverted=%u", crc_fail_normal, crc_fail_inverted);
|
|
}
|
|
|
|
if (g_dbglevel >= DBG_EXTENDED) {
|
|
Dbprintf("FeliCa RX timeout, state %u, %u bytes from the front end. Below is %s:",
|
|
FelicaFrame.state, rx_bytes,
|
|
felica_probe_mode ? "envelope peak-to-peak per 8 bit periods"
|
|
: "raw demodulator output");
|
|
Dbhexdump(rawcnt, rawcap, 0);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set up FeliCa communication (similar to iso14443a_setup)
|
|
// field is setup for "Sending as Reader"
|
|
bool iso18092_setup_ex(uint8_t fpga_minor_mode, uint32_t preserve_low_bytes) {
|
|
|
|
LEDsoff();
|
|
#if defined XC3 || defined PM5
|
|
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
|
|
#else
|
|
FpgaDownloadAndGo(FPGA_BITSTREAM_HF_FELICA);
|
|
#endif
|
|
// allocate command receive buffer
|
|
BigBuf_free();
|
|
if (preserve_low_bytes) {
|
|
set_tracelen(preserve_low_bytes);
|
|
}
|
|
|
|
uint8_t *rx = BigBuf_calloc(FELICA_MAX_RF_FRAME_SIZE);
|
|
if (rx == NULL) {
|
|
return false;
|
|
}
|
|
FelicaFrameinit(&FelicaFrame, rx);
|
|
|
|
felica_nexttransfertime = 2 * FELICA_212K_CARRIER_TO_TIMER_TICKS(DELAY_ARM2AIR_AS_READER);
|
|
// iso18092_set_timeout(2120); // 106 * 20ms maximum start-up time of card
|
|
iso18092_set_timeout(1060); // 106 * 10ms maximum start-up time of card
|
|
|
|
init_table(CRC_FELICA);
|
|
|
|
// connect Demodulated Signal to ADC:
|
|
SetAdcMuxFor(ADC_MUXSEL_HIPKD);
|
|
|
|
// Set up the synchronous serial port
|
|
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO18092);
|
|
|
|
// RX LSB transfer. TX MSB transfer, Remember to set it(RX) back to MSB with
|
|
FpgaUpdateFrameMode(8, false, true);
|
|
|
|
// Signal field is on with the appropriate LED
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092 | fpga_minor_mode);
|
|
|
|
//20.4 ms generate field, start sending polling command afterwars.
|
|
SpinDelay(100);
|
|
|
|
if (g_dbglevel >= DBG_EXTENDED) {
|
|
Dbprintf("FeliCa field: HF antenna %u mV", AdcRssiAvgToMilliVolt(ADC_RSSI_CH_HF));
|
|
}
|
|
|
|
// Start the timer
|
|
StartCountSspClk();
|
|
|
|
LED_D_ON();
|
|
felica_field_active = true;
|
|
return true;
|
|
}
|
|
|
|
void iso18092_setup(uint8_t fpga_minor_mode) {
|
|
(void)iso18092_setup_ex(fpga_minor_mode, 0);
|
|
}
|
|
|
|
void felica_reset_frame_mode(void) {
|
|
switch_off();
|
|
felica_field_active = false;
|
|
// Resetting Frame mode (First set in FpgaSetupSsc() function)
|
|
FpgaUpdateFrameMode(8, true, true);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// RAW FeliCa commands. Send out commands and store answers.
|
|
//-----------------------------------------------------------------------------
|
|
void felica_sendraw(const PacketCommandNG *c) {
|
|
|
|
felica_command_t param = 0;
|
|
size_t len = 0;
|
|
const uint8_t *payload = NULL;
|
|
|
|
if (!c->ng) {
|
|
reply_ng(CMD_HF_FELICA_COMMAND, PM3_EINVARG, NULL, 0);
|
|
return;
|
|
}
|
|
if (c->length < sizeof(felica_raw_cmd_t)) {
|
|
reply_ng(CMD_HF_FELICA_COMMAND, PM3_EINVARG, NULL, 0);
|
|
return;
|
|
}
|
|
const felica_raw_cmd_t *request = (const felica_raw_cmd_t *)c->data.asBytes;
|
|
if ((size_t)request->rawlen > PM3_CMD_DATA_SIZE || FELICA_RAW_LEN(request->rawlen) > c->length) {
|
|
reply_ng(CMD_HF_FELICA_COMMAND, PM3_EINVARG, NULL, 0);
|
|
return;
|
|
}
|
|
|
|
param = request->flags;
|
|
len = request->rawlen;
|
|
payload = request->raw;
|
|
|
|
bool do_connect = ((param & FELICA_CONNECT) == FELICA_CONNECT);
|
|
bool no_disconnect = ((param & FELICA_NO_DISCONNECT) == FELICA_NO_DISCONNECT);
|
|
bool system_select = ((param & FELICA_SYSTEM_SELECT) == FELICA_SYSTEM_SELECT);
|
|
bool replied = false;
|
|
|
|
if (system_select && (len != 2 || !do_connect || (param & (FELICA_RAW | FELICA_NO_SELECT)))) {
|
|
reply_ng(CMD_HF_FELICA_COMMAND, PM3_EINVARG, NULL, 0);
|
|
return;
|
|
}
|
|
|
|
// Signal probe. The FPGA hands us envelope peak-to-peak per 8 bit periods
|
|
// instead of demodulated bits, so nothing will decode while this is on.
|
|
// It is how you tell "tag out of range" apart from "demodulator not
|
|
// locking", which are otherwise identical from up here.
|
|
felica_probe_mode = ((param & FELICA_PROBE) == FELICA_PROBE);
|
|
|
|
if ((param & FELICA_CLEARTRACE) == FELICA_CLEARTRACE) {
|
|
clear_trace();
|
|
}
|
|
|
|
set_tracing(true);
|
|
|
|
// Preserve compatibility with existing commands that do not send CONNECT:
|
|
// set up reader path when starting from field-off state.
|
|
if (!felica_field_is_active() || (do_connect && !system_select)) {
|
|
iso18092_setup(FPGA_HF_ISO18092_FLAG_READER | FPGA_HF_ISO18092_FLAG_NOMOD);
|
|
}
|
|
|
|
if (do_connect && ((param & FELICA_NO_SELECT) != FELICA_NO_SELECT)) {
|
|
|
|
// notify client selecting status.
|
|
// on failed selection, retain the field only for system-specific polling.
|
|
felica_card_select_t card = {0};
|
|
uint16_t system_code = system_select ? ((uint16_t)payload[0] << 8) | payload[1] : 0xFFFF;
|
|
uint8_t select_result = felica_select_card(&card, system_code, system_select ? 0x01 : 0x00);
|
|
|
|
int select_status = PM3_SUCCESS;
|
|
switch (select_result) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
select_status = PM3_ETIMEOUT;
|
|
break;
|
|
case 2:
|
|
select_status = PM3_EWRONGANSWER;
|
|
break;
|
|
case 3:
|
|
select_status = PM3_ECRC;
|
|
break;
|
|
case 4:
|
|
// polling response too short to hold IDm + PMm, card is all zeroes
|
|
select_status = PM3_ELENGTH;
|
|
break;
|
|
default:
|
|
select_status = PM3_ESOFT;
|
|
break;
|
|
}
|
|
|
|
reply_ng(CMD_HF_FELICA_COMMAND, select_status, (uint8_t *)&card, sizeof(felica_card_select_t));
|
|
replied = true;
|
|
if (select_status != PM3_SUCCESS) {
|
|
if (!system_select || !no_disconnect) {
|
|
felica_reset_frame_mode();
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
if ((param & FELICA_RAW) == FELICA_RAW) {
|
|
if (len > FELICA_MAX_DATA_SIZE) {
|
|
Dbprintf("FeliCa raw payload too long: %u (max %u)", len, FELICA_MAX_DATA_SIZE);
|
|
reply_ng(CMD_HF_FELICA_COMMAND, PM3_ELENGTH, NULL, 0);
|
|
replied = true;
|
|
if (!no_disconnect) {
|
|
felica_reset_frame_mode();
|
|
}
|
|
return;
|
|
}
|
|
|
|
uint8_t buf[FELICA_MAX_RF_FRAME_SIZE];
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
// add sync bits
|
|
buf[0] = 0xb2;
|
|
buf[1] = 0x4d;
|
|
|
|
// len (number of bytes) + 1 for len byte itself
|
|
buf[2] = len + 1;
|
|
|
|
// copy command
|
|
memcpy(buf + 3, payload, len);
|
|
|
|
if ((param & FELICA_APPEND_CRC) == FELICA_APPEND_CRC) {
|
|
// Don't append crc on empty bytearray...
|
|
if (len) {
|
|
// n bytes + len 1 byte
|
|
AddCrc(buf + 2, len + 1);
|
|
}
|
|
}
|
|
|
|
if (g_dbglevel >= DBG_DEBUG) {
|
|
Dbprintf("Transmit Frame (no CRC shown):");
|
|
// 0,1,2, n
|
|
Dbhexdump(len + 1 + 2, buf, 0);
|
|
// total buffer length: len + 1 len byte + 2 sync bytes + 2 crc bytes
|
|
Dbprintf("Buffer Length: %i", buf[2] + 4);
|
|
};
|
|
|
|
TransmitFor18092_AsReader(buf, buf[2] + 4, NULL, 1, 0);
|
|
bool got_frame = WaitForFelicaReply(1024);
|
|
|
|
if (g_dbglevel >= DBG_DEBUG) {
|
|
Dbprintf("Received Frame Code: %d", got_frame);
|
|
Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes, 0);
|
|
};
|
|
|
|
int status = got_frame ? PM3_SUCCESS : PM3_ERFTRANS;
|
|
uint16_t frame_len = got_frame ? FelicaFrame.len : 0;
|
|
reply_ng(CMD_HF_FELICA_COMMAND, status, got_frame ? FelicaFrame.framebytes : NULL, frame_len);
|
|
replied = true;
|
|
}
|
|
|
|
if (replied == false) {
|
|
// Nothing above answered the client. Say so instead of letting it block
|
|
// until its own timeout expires.
|
|
reply_ng(CMD_HF_FELICA_COMMAND, PM3_EINVARG, NULL, 0);
|
|
}
|
|
|
|
if (no_disconnect) {
|
|
return;
|
|
}
|
|
|
|
felica_reset_frame_mode();
|
|
felica_probe_mode = false;
|
|
return;
|
|
}
|
|
|
|
void felica_sniff(uint32_t samplesToSkip, uint32_t triggersToSkip) {
|
|
|
|
clear_trace();
|
|
set_tracing(true);
|
|
iso18092_setup(FPGA_HF_ISO18092_FLAG_NOMOD);
|
|
|
|
LED_D_ON();
|
|
|
|
int retval = PM3_SUCCESS;
|
|
// 0 == no limit, keep sniffing until the user aborts
|
|
int remFrames = (samplesToSkip) ? (int)samplesToSkip : -1;
|
|
int trigger_cnt = 0;
|
|
bool isReaderFrame;
|
|
|
|
uint8_t flip = 0;
|
|
uint16_t checker = 0;
|
|
for (;;) {
|
|
|
|
WDT_HIT();
|
|
|
|
// since simulation is a tight time critical loop,
|
|
// we only check for user request to end at iteration 3000, 9000.
|
|
if (flip == 3) {
|
|
if (data_available()) {
|
|
retval = PM3_EOPABORTED;
|
|
break;
|
|
}
|
|
flip = 0;
|
|
}
|
|
|
|
if (checker >= 3000) {
|
|
|
|
if (BUTTON_PRESS()) {
|
|
retval = PM3_EOPABORTED;
|
|
break;
|
|
}
|
|
flip++;
|
|
checker = 0;
|
|
}
|
|
++checker;
|
|
|
|
if (FPGA_SSC_RX_Ready()) {
|
|
|
|
uint8_t dist = (uint8_t)FPGA_SSC_RX_Value();
|
|
Process18092Byte(&FelicaFrame, dist, felica_get_rx_byte_start_time());
|
|
|
|
if ((dist >= 178) && (++trigger_cnt > triggersToSkip)) {
|
|
Dbprintf("triggers To skip kicked %d", dist);
|
|
break;
|
|
}
|
|
if (FelicaFrame.state == STATE_FULL) {
|
|
|
|
// A frame that failed CRC is demodulator noise, not traffic.
|
|
// Logging it only pollutes the trace and eats the frame budget.
|
|
if (FelicaFrame.crc_ok == false) {
|
|
FelicaFrameReset(&FelicaFrame);
|
|
continue;
|
|
}
|
|
|
|
if ((FelicaFrame.framebytes[3] % 2) == 0) {
|
|
isReaderFrame = true; // All Reader Frames are even and all Tag frames are odd
|
|
} else {
|
|
isReaderFrame = false;
|
|
}
|
|
|
|
// log first, then decide whether we are done. The old order threw
|
|
// away the very frame that reached the limit.
|
|
LogTrace(FelicaFrame.framebytes,
|
|
FelicaFrame.len,
|
|
felica_timer_to_carrier_periods(FelicaFrame.startTime, false) - DELAY_AIR2ARM_AS_READER,
|
|
felica_timer_to_carrier_periods(FelicaFrame.endTime, false) - DELAY_AIR2ARM_AS_READER,
|
|
NULL,
|
|
isReaderFrame
|
|
);
|
|
FelicaFrameReset(&FelicaFrame);
|
|
|
|
if (remFrames > 0 && --remFrames == 0) {
|
|
Dbprintf("Stop Sniffing - samples To skip reached!");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
felica_reset_frame_mode();
|
|
|
|
Dbprintf("Felica sniffing done, tracelen: %i", BigBuf_get_traceLen());
|
|
reply_ng(CMD_HF_FELICA_SNIFF, retval, NULL, 0);
|
|
LED_D_OFF();
|
|
}
|
|
|
|
#define R_POLL0_LEN 0x16
|
|
#define R_POLL1_LEN 0x18
|
|
#define R_READBLK_LEN 0x21
|
|
//simulate NFC Tag3 card - for now only poll response works
|
|
// second half (4 bytes) of NDEF2 goes into nfcid2_0, first into nfcid2_1
|
|
void felica_sim_lite(const uint8_t *uid) {
|
|
|
|
// prepare our 3 responses...
|
|
uint8_t resp_poll0[R_POLL0_LEN] = { 0xb2, 0x4d, 0x12, FELICA_POLLING_RES, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x01, 0x43, 0x00, 0xb3, 0x7f};
|
|
uint8_t resp_poll1[R_POLL1_LEN] = { 0xb2, 0x4d, 0x14, FELICA_POLLING_RES, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00, 0x01, 0x43, 0x00, 0x88, 0xb4, 0xb3, 0x7f};
|
|
uint8_t resp_readblk[R_READBLK_LEN] = { 0xb2, 0x4d, 0x1d, FELICA_READ_WITHOUT_ENCRYPTION_RES, 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};
|
|
|
|
// NFC tag 3/ ISo technically. Many overlapping standards
|
|
DbpString("Felica Lite-S simulation start");
|
|
Dbprintf("NDEF2 UID: %02x %02x %02x %02x %02x %02x %02x %02x",
|
|
uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]
|
|
);
|
|
|
|
// fill in blanks
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
resp_poll0[i + 4] = uid[i];
|
|
resp_poll1[i + 4] = uid[i];
|
|
resp_readblk[i + 4] = uid[i];
|
|
}
|
|
|
|
// calculate and set CRC
|
|
AddCrc(&resp_poll0[2], resp_poll0[2]);
|
|
AddCrc(&resp_poll1[2], resp_poll1[2]);
|
|
AddCrc(&resp_readblk[2], resp_readblk[2]);
|
|
|
|
clear_trace();
|
|
iso18092_setup(FPGA_HF_ISO18092_FLAG_NOMOD);
|
|
|
|
int retval = PM3_SUCCESS;
|
|
int curlen = 0;
|
|
const uint8_t *curresp = NULL;
|
|
bool listenmode = true;
|
|
// uint32_t frtm = GetCountSspClk();
|
|
|
|
uint8_t flip = 0;
|
|
uint16_t checker = 0;
|
|
for (;;) {
|
|
|
|
WDT_HIT();
|
|
|
|
// since simulation is a tight time critical loop,
|
|
// we only check for user request to end at iteration 3000, 9000.
|
|
if (flip == 3) {
|
|
if (data_available()) {
|
|
retval = PM3_EOPABORTED;
|
|
break;
|
|
}
|
|
flip = 0;
|
|
}
|
|
|
|
if (checker >= 3000) {
|
|
|
|
if (BUTTON_PRESS()) {
|
|
retval = PM3_EOPABORTED;
|
|
break;
|
|
}
|
|
flip++;
|
|
checker = 0;
|
|
}
|
|
++checker;
|
|
|
|
WDT_HIT();
|
|
|
|
if (listenmode) {
|
|
// waiting for request...
|
|
if (FPGA_SSC_RX_Ready()) {
|
|
|
|
uint8_t dist = (uint8_t)(FPGA_SSC_RX_Value());
|
|
// frtm = GetCountSspClk();
|
|
Process18092Byte(&FelicaFrame, dist, felica_get_rx_byte_start_time());
|
|
|
|
if (FelicaFrame.state == STATE_FULL) {
|
|
|
|
if (FelicaFrame.crc_ok) {
|
|
|
|
if (FelicaFrame.framebytes[2] == 6 && FelicaFrame.framebytes[3] == 0) {
|
|
static uint8_t timeslot = 0;
|
|
|
|
// polling... there are two types of polling we answer to
|
|
if (FelicaFrame.framebytes[6] == 0) {
|
|
curresp = resp_poll0;
|
|
curlen = R_POLL0_LEN;
|
|
listenmode = false;
|
|
}
|
|
if (FelicaFrame.framebytes[6] == 1) {
|
|
curresp = resp_poll1;
|
|
curlen = R_POLL1_LEN;
|
|
listenmode = false;
|
|
}
|
|
if (timeslot > FelicaFrame.framebytes[7]) {
|
|
// framebytes[7] contains the maximum time slot in which we are allowed to respond (#0..#15)
|
|
timeslot = 0;
|
|
}
|
|
// first time slot (#0) starts after 512 * 64 / fc, slot length equals 256 * 64 / fc
|
|
felica_nexttransfertime = GetCountSspClk() - FELICA_212K_CARRIER_TO_TIMER_TICKS(DELAY_AIR2ARM_AS_READER + DELAY_ARM2AIR_AS_READER) + (512 + timeslot * 256) + 1;
|
|
timeslot++; // we should use a random time slot, but responding in incremental slots should do just fine for now
|
|
}
|
|
|
|
if (FelicaFrame.framebytes[2] > 5 && FelicaFrame.framebytes[3] == 0x06) {
|
|
// we should rebuild it depending on page size, but...
|
|
// Let's see first
|
|
curresp = resp_readblk;
|
|
curlen = R_READBLK_LEN;
|
|
listenmode = false;
|
|
}
|
|
// clear frame
|
|
FelicaFrameReset(&FelicaFrame);
|
|
} else {
|
|
// frame invalid, clear it out to allow for the next one
|
|
FelicaFrameReset(&FelicaFrame);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (listenmode == false) {
|
|
// trying to answer... here to start answering immediately.
|
|
// this one is a bit finicky. Seems that being a bit late is better than earlier
|
|
// TransmitFor18092_AsReader(curresp, curlen, frtm+512, 0, 0);
|
|
TransmitFor18092_AsReader(curresp, curlen, NULL, 0, 0);
|
|
|
|
// switch back
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO18092 | FPGA_HF_ISO18092_FLAG_NOMOD);
|
|
|
|
FelicaFrameReset(&FelicaFrame);
|
|
listenmode = true;
|
|
curlen = 0;
|
|
curresp = NULL;
|
|
}
|
|
}
|
|
|
|
felica_reset_frame_mode();
|
|
|
|
Dbprintf("FeliCa Lite-S emulator stopped. Trace length: %d ", BigBuf_get_traceLen());
|
|
reply_ng(CMD_HF_FELICALITE_SIMULATE, retval, NULL, 0);
|
|
}
|
|
|
|
#define RES_SVC_LEN 11 + 3
|
|
|
|
void felica_dump_lite_s(void) {
|
|
uint8_t ndef[8];
|
|
uint8_t poll[10] = { 0xb2, 0x4d, 0x06, FELICA_POLLING_REQ, 0xff, 0xff, 0x00, 0x00, 0x09, 0x21};
|
|
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};
|
|
|
|
// setup device.
|
|
clear_trace();
|
|
iso18092_setup(FPGA_HF_ISO18092_FLAG_READER | FPGA_HF_ISO18092_FLAG_NOMOD);
|
|
|
|
uint8_t blknum;
|
|
bool isOK = false;
|
|
uint16_t cnt = 0, cntfails = 0;
|
|
uint8_t *dest = BigBuf_get_addr();
|
|
|
|
while ((BUTTON_PRESS() == false) && (data_available() == false)) {
|
|
WDT_HIT();
|
|
// polling?
|
|
//TransmitFor18092_AsReader(poll, 10, GetCountSspClk()+512, 1, 0);
|
|
TransmitFor18092_AsReader(poll, 10, NULL, 1, 0);
|
|
|
|
if (WaitForFelicaReply(512) && FelicaFrame.framebytes[3] == FELICA_POLLING_RES) {
|
|
// copy 8bytes to ndef.
|
|
memcpy(ndef, FelicaFrame.framebytes + 4, 8);
|
|
// for (c=0; c < 8; c++)
|
|
// ndef[c] = FelicaFrame.framebytes[c+4];
|
|
|
|
for (blknum = 0; blknum < ARRAYLEN(liteblks);) {
|
|
// block to read.
|
|
BuildFliteRdblk(ndef, 1, &liteblks[blknum]);
|
|
|
|
//TransmitFor18092_AsReader(frameSpace, frameSpace[2]+4, GetCountSspClk()+512, 1, 0);
|
|
|
|
TransmitFor18092_AsReader(frameSpace, frameSpace[2] + 4, NULL, 1, 0);
|
|
// read block
|
|
if (WaitForFelicaReply(1024) && FelicaFrame.framebytes[3] == FELICA_READ_WITHOUT_ENCRYPTION_RES) {
|
|
|
|
dest[cnt++] = liteblks[blknum];
|
|
|
|
const uint8_t *fb = FelicaFrame.framebytes;
|
|
dest[cnt++] = fb[12];
|
|
dest[cnt++] = fb[13];
|
|
|
|
//memcpy(dest+cnt, FelicaFrame.framebytes + 15, 16);
|
|
//cnt += 16;
|
|
for (uint8_t j = 0; j < 16; j++) {
|
|
dest[cnt++] = fb[15 + j];
|
|
}
|
|
|
|
blknum++;
|
|
cntfails = 0;
|
|
|
|
// // print raw log.
|
|
// Dbprintf("LEN %u | Dump bytes count %u ", FelicaFrame.len, cnt);
|
|
// Dbhexdump(FelicaFrame.len, FelicaFrame.framebytes + 15, 0);
|
|
} else {
|
|
cntfails++;
|
|
if (cntfails > 12) {
|
|
blknum++;
|
|
cntfails = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
isOK = true;
|
|
break;
|
|
}
|
|
}
|
|
felica_reset_frame_mode();
|
|
|
|
// setting tracelen - important! it was set by buffer overflow before
|
|
// iceman: is this still needed?!?
|
|
set_tracelen(cnt);
|
|
|
|
felica_lite_dump_resp_t payload = {
|
|
.completed = isOK,
|
|
.tracelen = cnt,
|
|
};
|
|
reply_ng(CMD_HF_FELICALITE_DUMP, isOK ? PM3_SUCCESS : PM3_EOPABORTED, (uint8_t *)&payload, sizeof(payload));
|
|
}
|