proxmark3/armsrc/pcf7931.c

531 lines
17 KiB
C
Raw Normal View History

#include "pcf7931.h"
#include "proxmark3_arm.h"
#include "cmd.h"
#include "BigBuf.h"
#include "fpgaloader.h"
#include "ticks.h"
#include "dbprint.h"
#include "util.h"
#include "lfsampling.h"
#include "string.h"
#define T0_PCF 8 //period for the pcf7931 in us
#define ALLOC 16
size_t DemodPCF7931(uint8_t **outBlocks) {
uint8_t bits[256] = {0x00};
2019-03-10 03:34:41 +08:00
uint8_t blocks[8][16];
uint8_t *dest = BigBuf_get_addr();
2019-03-10 03:34:41 +08:00
int GraphTraceLen = BigBuf_max_traceLen();
2019-03-10 07:00:59 +08:00
if (GraphTraceLen > 18000)
2019-03-10 03:34:41 +08:00
GraphTraceLen = 18000;
2019-03-19 05:25:21 +08:00
int i = 2, j, lastval, bitidx, half_switch;
2019-03-10 03:34:41 +08:00
int clock = 64;
int tolerance = clock / 8;
int pmc, block_done;
int lc, warnings = 0;
size_t num_blocks = 0;
2019-03-19 05:25:21 +08:00
int lmin = 64, lmax = 192;
2019-03-10 03:34:41 +08:00
uint8_t dir;
2019-03-19 05:25:21 +08:00
BigBuf_Clear_keep_EM();
2019-03-10 03:34:41 +08:00
LFSetupFPGAForADC(95, true);
DoAcquisition_default(0, true);
2019-03-10 03:34:41 +08:00
/* Find first local max/min */
2019-03-10 07:00:59 +08:00
if (dest[1] > dest[0]) {
while (i < GraphTraceLen) {
if (!(dest[i] > dest[i - 1]) && dest[i] > lmax)
2019-03-10 03:34:41 +08:00
break;
i++;
}
dir = 0;
} else {
2019-03-10 07:00:59 +08:00
while (i < GraphTraceLen) {
if (!(dest[i] < dest[i - 1]) && dest[i] < lmin)
2019-03-10 03:34:41 +08:00
break;
i++;
}
dir = 1;
}
lastval = i++;
half_switch = 0;
pmc = 0;
block_done = 0;
for (bitidx = 0; i < GraphTraceLen; i++) {
2019-03-10 07:00:59 +08:00
if ((dest[i - 1] > dest[i] && dir == 1 && dest[i] > lmax) || (dest[i - 1] < dest[i] && dir == 0 && dest[i] < lmin)) {
2019-03-10 03:34:41 +08:00
lc = i - lastval;
lastval = i;
// Switch depending on lc length:
// Tolerance is 1/8 of clock rate (arbitrary)
2019-03-10 07:00:59 +08:00
if (ABS(lc - clock / 4) < tolerance) {
2019-03-10 03:34:41 +08:00
// 16T0
2019-03-10 07:00:59 +08:00
if ((i - pmc) == lc) { /* 16T0 was previous one */
2019-03-10 03:34:41 +08:00
/* It's a PMC ! */
2019-03-10 07:00:59 +08:00
i += (128 + 127 + 16 + 32 + 33 + 16) - 1;
2019-03-10 03:34:41 +08:00
lastval = i;
pmc = 0;
block_done = 1;
} else {
pmc = i;
}
2019-03-10 07:00:59 +08:00
} else if (ABS(lc - clock / 2) < tolerance) {
2019-03-10 03:34:41 +08:00
// 32TO
2019-03-10 07:00:59 +08:00
if ((i - pmc) == lc) { /* 16T0 was previous one */
2019-03-10 03:34:41 +08:00
/* It's a PMC ! */
2019-03-10 07:00:59 +08:00
i += (128 + 127 + 16 + 32 + 33) - 1;
2019-03-10 03:34:41 +08:00
lastval = i;
pmc = 0;
block_done = 1;
2019-03-10 07:00:59 +08:00
} else if (half_switch == 1) {
bits[bitidx++] = 0;
2019-03-10 03:34:41 +08:00
half_switch = 0;
2019-03-10 07:00:59 +08:00
} else
2019-03-10 03:34:41 +08:00
half_switch++;
2019-03-10 07:00:59 +08:00
} else if (ABS(lc - clock) < tolerance) {
2019-03-10 03:34:41 +08:00
// 64TO
bits[bitidx++] = 1;
2019-03-10 03:34:41 +08:00
} else {
// Error
if (++warnings > 10) {
Dbprintf("Error: too many detection errors, aborting.");
return 0;
}
}
2019-03-10 07:00:59 +08:00
if (block_done == 1) {
if (bitidx == 128) {
for (j = 0; j < 16; ++j) {
2019-03-10 03:34:41 +08:00
blocks[num_blocks][j] =
2019-03-10 07:00:59 +08:00
128 * bits[j * 8 + 7] +
64 * bits[j * 8 + 6] +
32 * bits[j * 8 + 5] +
16 * bits[j * 8 + 4] +
8 * bits[j * 8 + 3] +
4 * bits[j * 8 + 2] +
2 * bits[j * 8 + 1] +
bits[j * 8]
;
2019-03-10 03:34:41 +08:00
}
num_blocks++;
}
bitidx = 0;
block_done = 0;
half_switch = 0;
}
2019-03-10 07:00:59 +08:00
if (i < GraphTraceLen)
dir = (dest[i - 1] > dest[i]) ? 0 : 1;
2019-03-10 03:34:41 +08:00
}
2019-03-10 07:00:59 +08:00
if (bitidx == 255)
bitidx = 0;
2019-03-10 03:34:41 +08:00
warnings = 0;
2019-03-10 07:00:59 +08:00
if (num_blocks == 4) break;
2019-03-10 03:34:41 +08:00
}
2019-03-10 07:00:59 +08:00
memcpy(outBlocks, blocks, 16 * num_blocks);
2019-03-10 03:34:41 +08:00
return num_blocks;
}
bool IsBlock0PCF7931(uint8_t *block) {
2019-03-10 03:34:41 +08:00
// assuming all RFU bits are set to 0
// if PAC is enabled password is set to 0
2019-03-10 07:00:59 +08:00
if (block[7] == 0x01) {
if (!memcmp(block, "\x00\x00\x00\x00\x00\x00\x00", 7) && !memcmp(block + 9, "\x00\x00\x00\x00\x00\x00\x00", 7))
2019-03-10 03:34:41 +08:00
return true;
2019-03-10 07:00:59 +08:00
} else if (block[7] == 0x00) {
if (!memcmp(block + 9, "\x00\x00\x00\x00\x00\x00\x00", 7))
2019-03-10 03:34:41 +08:00
return true;
}
return false;
}
bool IsBlock1PCF7931(uint8_t *block) {
2019-03-10 03:34:41 +08:00
// assuming all RFU bits are set to 0
2019-03-18 20:42:17 +08:00
if (block[10] == 0
2019-03-28 21:19:41 +08:00
&& block[11] == 0
&& block[12] == 0
&& block[13] == 0) {
if ((block[14] & 0x7f) <= 9
&& block[15] <= 9) {
return true;
2019-03-18 20:42:17 +08:00
}
2019-03-28 21:19:41 +08:00
}
2019-03-10 03:34:41 +08:00
return false;
}
void ReadPCF7931() {
2019-03-10 03:34:41 +08:00
int found_blocks = 0; // successfully read blocks
int max_blocks = 8; // readable blocks
uint8_t memory_blocks[8][17]; // PCF content
uint8_t single_blocks[8][17]; // PFC blocks with unknown position
int single_blocks_cnt = 0;
size_t n = 0; // transmitted blocks
uint8_t tmp_blocks[4][16]; // temporary read buffer
uint8_t found_0_1 = 0; // flag: blocks 0 and 1 were found
int errors = 0; // error counter
int tries = 0; // tries counter
2019-03-10 07:00:59 +08:00
memset(memory_blocks, 0, 8 * 17 * sizeof(uint8_t));
memset(single_blocks, 0, 8 * 17 * sizeof(uint8_t));
2019-03-10 03:34:41 +08:00
int i = 0, j = 0;
do {
i = 0;
2019-03-10 07:00:59 +08:00
memset(tmp_blocks, 0, 4 * 16 * sizeof(uint8_t));
n = DemodPCF7931((uint8_t **)tmp_blocks);
if (!n)
2019-03-10 03:34:41 +08:00
++errors;
// exit if no block is received
if (errors >= 10 && found_blocks == 0 && single_blocks_cnt == 0) {
Dbprintf("Error, no tag or bad tag");
return;
}
// exit if too many errors during reading
2019-03-10 07:00:59 +08:00
if (tries > 50 && (2 * errors > tries)) {
2019-03-10 03:34:41 +08:00
Dbprintf("Error reading the tag");
Dbprintf("Here is the partial content");
goto end;
}
// our logic breaks if we don't get at least two blocks
if (n < 2) {
if (n == 0 || !memcmp(tmp_blocks[0], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16))
continue;
if (single_blocks_cnt < max_blocks) {
for (i = 0; i < single_blocks_cnt; ++i) {
if (!memcmp(single_blocks[i], tmp_blocks[0], 16)) {
j = 1;
break;
}
}
if (j != 1) {
memcpy(single_blocks[single_blocks_cnt], tmp_blocks[0], 16);
single_blocks_cnt++;
}
j = 0;
}
++tries;
continue;
}
Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (max_blocks == 0 ? found_blocks : max_blocks), tries, errors);
i = 0;
2019-03-10 07:00:59 +08:00
if (!found_0_1) {
2019-03-10 03:34:41 +08:00
while (i < n - 1) {
2019-03-10 07:00:59 +08:00
if (IsBlock0PCF7931(tmp_blocks[i]) && IsBlock1PCF7931(tmp_blocks[i + 1])) {
2019-03-10 03:34:41 +08:00
found_0_1 = 1;
memcpy(memory_blocks[0], tmp_blocks[i], 16);
2019-03-10 07:00:59 +08:00
memcpy(memory_blocks[1], tmp_blocks[i + 1], 16);
2019-03-10 03:34:41 +08:00
memory_blocks[0][ALLOC] = memory_blocks[1][ALLOC] = 1;
// block 1 tells how many blocks are going to be sent
max_blocks = MAX((memory_blocks[1][14] & 0x7f), memory_blocks[1][15]) + 1;
found_blocks = 2;
Dbprintf("Found blocks 0 and 1. PCF is transmitting %d blocks.", max_blocks);
// handle the following blocks
for (j = i + 2; j < n; ++j) {
memcpy(memory_blocks[found_blocks], tmp_blocks[j], 16);
memory_blocks[found_blocks][ALLOC] = 1;
++found_blocks;
}
break;
}
++i;
}
} else {
// Trying to re-order blocks
// Look for identical block in memory blocks
2019-03-10 07:00:59 +08:00
while (i < n - 1) {
2019-03-10 03:34:41 +08:00
// skip all zeroes blocks
if (memcmp(tmp_blocks[i], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
for (j = 1; j < max_blocks - 1; ++j) {
2019-03-10 07:00:59 +08:00
if (!memcmp(tmp_blocks[i], memory_blocks[j], 16) && !memory_blocks[j + 1][ALLOC]) {
memcpy(memory_blocks[j + 1], tmp_blocks[i + 1], 16);
memory_blocks[j + 1][ALLOC] = 1;
2019-03-10 03:34:41 +08:00
if (++found_blocks >= max_blocks) goto end;
}
}
}
2019-03-10 07:00:59 +08:00
if (memcmp(tmp_blocks[i + 1], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
2019-03-10 03:34:41 +08:00
for (j = 0; j < max_blocks; ++j) {
2019-03-10 07:00:59 +08:00
if (!memcmp(tmp_blocks[i + 1], memory_blocks[j], 16) && !memory_blocks[(j == 0 ? max_blocks : j) - 1][ALLOC]) {
2019-03-10 03:34:41 +08:00
if (j == 0) {
memcpy(memory_blocks[max_blocks - 1], tmp_blocks[i], 16);
memory_blocks[max_blocks - 1][ALLOC] = 1;
} else {
2019-03-10 07:00:59 +08:00
memcpy(memory_blocks[j - 1], tmp_blocks[i], 16);
memory_blocks[j - 1][ALLOC] = 1;
2019-03-10 03:34:41 +08:00
}
if (++found_blocks >= max_blocks) goto end;
}
}
}
++i;
}
}
++tries;
if (BUTTON_PRESS()) {
Dbprintf("Button pressed, stopping.");
goto end;
}
2019-03-10 07:00:59 +08:00
} while (found_blocks != max_blocks);
2019-03-10 07:00:59 +08:00
end:
2019-03-10 03:34:41 +08:00
Dbprintf("-----------------------------------------");
Dbprintf("Memory content:");
Dbprintf("-----------------------------------------");
for (i = 0; i < max_blocks; ++i) {
if (memory_blocks[i][ALLOC])
print_result("Block", memory_blocks[i], 16);
else
Dbprintf("<missing block %d>", i);
}
Dbprintf("-----------------------------------------");
if (found_blocks < max_blocks) {
Dbprintf("-----------------------------------------");
Dbprintf("Blocks with unknown position:");
Dbprintf("-----------------------------------------");
for (i = 0; i < single_blocks_cnt; ++i)
print_result("Block", single_blocks[i], 16);
Dbprintf("-----------------------------------------");
}
2019-04-18 18:43:35 +08:00
reply_old(CMD_ACK, 0, 0, 0, 0, 0);
}
static void RealWritePCF7931(uint8_t *pass, uint16_t init_delay, int32_t l, int32_t p, uint8_t address, uint8_t byte, uint8_t data) {
2019-03-10 03:34:41 +08:00
uint32_t tab[1024] = {0}; // data times frame
uint32_t u = 0;
uint8_t parity = 0;
bool comp = 0;
//BUILD OF THE DATA FRAME
//alimentation of the tag (time for initializing)
2019-03-10 07:00:59 +08:00
AddPatternPCF7931(init_delay, 0, 8192 / 2 * T0_PCF, tab);
AddPatternPCF7931(8192 / 2 * T0_PCF + 319 * T0_PCF + 70, 3 * T0_PCF, 29 * T0_PCF, tab);
2019-03-10 03:34:41 +08:00
//password indication bit
AddBitPCF7931(1, tab, l, p);
//password (on 56 bits)
AddBytePCF7931(pass[0], tab, l, p);
AddBytePCF7931(pass[1], tab, l, p);
AddBytePCF7931(pass[2], tab, l, p);
AddBytePCF7931(pass[3], tab, l, p);
AddBytePCF7931(pass[4], tab, l, p);
AddBytePCF7931(pass[5], tab, l, p);
AddBytePCF7931(pass[6], tab, l, p);
//programming mode (0 or 1)
AddBitPCF7931(0, tab, l, p);
//block adress on 6 bits
for (u = 0; u < 6; ++u) {
2019-03-10 07:00:59 +08:00
if (address & (1 << u)) { // bit 1
++parity;
AddBitPCF7931(1, tab, l, p);
} else { // bit 0
AddBitPCF7931(0, tab, l, p);
2019-03-10 03:34:41 +08:00
}
}
//byte address on 4 bits
2019-03-10 07:00:59 +08:00
for (u = 0; u < 4; ++u) {
if (byte & (1 << u)) { // bit 1
parity++;
AddBitPCF7931(1, tab, l, p);
} else // bit 0
AddBitPCF7931(0, tab, l, p);
2019-03-10 03:34:41 +08:00
}
//data on 8 bits
2019-03-10 07:00:59 +08:00
for (u = 0; u < 8; u++) {
if (data & (1 << u)) { // bit 1
parity++;
AddBitPCF7931(1, tab, l, p);
} else //bit 0
AddBitPCF7931(0, tab, l, p);
}
2019-03-10 03:34:41 +08:00
//parity bit
if ((parity % 2) == 0)
AddBitPCF7931(0, tab, l, p); //even parity
else
AddBitPCF7931(1, tab, l, p);//odd parity
//time access memory
2019-03-10 07:00:59 +08:00
AddPatternPCF7931(5120 + 2680, 0, 0, tab);
2019-03-10 03:34:41 +08:00
//conversion of the scale time
for (u = 0; u < 500; ++u)
2019-03-10 07:00:59 +08:00
tab[u] = (tab[u] * 3) / 2;
2019-03-10 03:34:41 +08:00
//compensation of the counter reload
2019-03-10 07:00:59 +08:00
while (!comp) {
2019-03-10 03:34:41 +08:00
comp = 1;
for (u = 0; tab[u] != 0; ++u)
2019-03-10 07:00:59 +08:00
if (tab[u] > 0xFFFF) {
tab[u] -= 0xFFFF;
comp = 0;
2019-03-10 03:34:41 +08:00
}
2019-03-10 07:00:59 +08:00
}
2019-03-10 03:34:41 +08:00
SendCmdPCF7931(tab);
}
/* Write on a byte of a PCF7931 tag
* @param address : address of the block to write
@param byte : address of the byte to write
@param data : data to write
*/
void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, uint8_t pass5, uint8_t pass6, uint8_t pass7, uint16_t init_delay, int32_t l, int32_t p, uint8_t address, uint8_t byte, uint8_t data) {
2019-03-10 03:34:41 +08:00
Dbprintf("Initialization delay : %d us", init_delay);
Dbprintf("Offsets : %d us on the low pulses width, %d us on the low pulses positions", l, p);
Dbprintf("Password (LSB first on each byte): %02x %02x %02x %02x %02x %02x %02x", pass1, pass2, pass3, pass4, pass5, pass6, pass7);
Dbprintf("Block address : %02x", address);
Dbprintf("Byte address : %02x", byte);
Dbprintf("Data : %02x", data);
2019-03-10 03:34:41 +08:00
uint8_t password[7] = {pass1, pass2, pass3, pass4, pass5, pass6, pass7};
2019-03-10 07:00:59 +08:00
RealWritePCF7931(password, init_delay, l, p, address, byte, data);
}
/* Send a trame to a PCF7931 tags
* @param tab : array of the data frame
*/
void SendCmdPCF7931(uint32_t *tab) {
2019-03-10 07:00:59 +08:00
uint16_t u = 0, tempo = 0;
2019-03-10 03:34:41 +08:00
Dbprintf("Sending data frame...");
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
2019-08-06 19:51:10 +08:00
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125kHz
2019-03-10 07:00:59 +08:00
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU);
2019-03-10 03:34:41 +08:00
LED_A_ON();
// steal this pin from the SSP and use it to control the modulation
AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
//initialization of the timer
AT91C_BASE_PMC->PMC_PCER |= (0x1 << AT91C_ID_TC0);
AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK; //clock at 48/32 MHz
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN;
AT91C_BASE_TCB->TCB_BCR = 1;
tempo = AT91C_BASE_TC0->TC_CV;
2019-03-10 07:00:59 +08:00
for (u = 0; tab[u] != 0; u += 3) {
2019-03-10 03:34:41 +08:00
// modulate antenna
HIGH(GPIO_SSC_DOUT);
while (tempo != tab[u])
tempo = AT91C_BASE_TC0->TC_CV;
// stop modulating antenna
LOW(GPIO_SSC_DOUT);
2019-03-10 07:00:59 +08:00
while (tempo != tab[u + 1])
2019-03-10 03:34:41 +08:00
tempo = AT91C_BASE_TC0->TC_CV;
// modulate antenna
HIGH(GPIO_SSC_DOUT);
2019-03-10 07:00:59 +08:00
while (tempo != tab[u + 2])
2019-03-10 03:34:41 +08:00
tempo = AT91C_BASE_TC0->TC_CV;
}
LED_A_OFF();
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
SpinDelay(200);
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
LED(0xFFFF, 1000);
}
/* Add a byte for building the data frame of PCF7931 tags
* @param b : byte to add
* @param tab : array of the data frame
* @param l : offset on low pulse width
* @param p : offset on low pulse positioning
*/
bool AddBytePCF7931(uint8_t byte, uint32_t *tab, int32_t l, int32_t p) {
2019-03-10 03:34:41 +08:00
uint32_t u;
for (u = 0; u < 8; ++u) {
if (byte & (1 << u)) { //bit is 1
2019-03-10 07:00:59 +08:00
if (AddBitPCF7931(1, tab, l, p) == 1) return 1;
2019-03-10 03:34:41 +08:00
} else { //bit is 0
2019-03-10 07:00:59 +08:00
if (AddBitPCF7931(0, tab, l, p) == 1) return 1;
2019-03-10 03:34:41 +08:00
}
}
return 0;
}
/* Add a bits for building the data frame of PCF7931 tags
* @param b : bit to add
* @param tab : array of the data frame
* @param l : offset on low pulse width
* @param p : offset on low pulse positioning
*/
bool AddBitPCF7931(bool b, uint32_t *tab, int32_t l, int32_t p) {
2019-03-10 03:34:41 +08:00
uint8_t u = 0;
//we put the cursor at the last value of the array
2019-03-10 07:00:59 +08:00
for (u = 0; tab[u] != 0; u += 3) { }
2019-03-10 03:34:41 +08:00
2019-03-10 07:00:59 +08:00
if (b == 1) { //add a bit 1
if (u == 0)
2019-03-10 03:34:41 +08:00
tab[u] = 34 * T0_PCF + p;
else
2019-03-10 07:00:59 +08:00
tab[u] = 34 * T0_PCF + tab[u - 1] + p;
2019-03-10 03:34:41 +08:00
2019-03-10 07:00:59 +08:00
tab[u + 1] = 6 * T0_PCF + tab[u] + l;
tab[u + 2] = 88 * T0_PCF + tab[u + 1] - l - p;
2019-03-10 03:34:41 +08:00
return 0;
} else { //add a bit 0
2019-03-10 07:00:59 +08:00
if (u == 0)
2019-03-10 03:34:41 +08:00
tab[u] = 98 * T0_PCF + p;
else
2019-03-10 07:00:59 +08:00
tab[u] = 98 * T0_PCF + tab[u - 1] + p;
2019-03-10 03:34:41 +08:00
2019-03-10 07:00:59 +08:00
tab[u + 1] = 6 * T0_PCF + tab[u] + l;
tab[u + 2] = 24 * T0_PCF + tab[u + 1] - l - p;
2019-03-10 03:34:41 +08:00
return 0;
}
return 1;
}
/* Add a custom pattern in the data frame
* @param a : delay of the first high pulse
* @param b : delay of the low pulse
* @param c : delay of the last high pulse
* @param tab : array of the data frame
*/
bool AddPatternPCF7931(uint32_t a, uint32_t b, uint32_t c, uint32_t *tab) {
2019-03-10 03:34:41 +08:00
uint32_t u = 0;
2019-03-10 07:00:59 +08:00
for (u = 0; tab[u] != 0; u += 3) {} //we put the cursor at the last value of the array
2019-03-10 07:00:59 +08:00
tab[u] = (u == 0) ? a : a + tab[u - 1];
tab[u + 1] = b + tab[u];
tab[u + 2] = c + tab[u + 1];
2019-03-10 03:34:41 +08:00
return 0;
2019-03-12 07:12:26 +08:00
}