3 #include "proxmark3_arm.h"
6 #include "fpgaloader.h"
10 #include "lfsampling.h"
13 #define T0_PCF 8 //period for the pcf7931 in us
16 size_t DemodPCF7931(uint8_t **outBlocks
) {
17 uint8_t bits
[256] = {0x00};
18 uint8_t blocks
[8][16];
19 uint8_t *dest
= BigBuf_get_addr();
21 int GraphTraceLen
= BigBuf_max_traceLen();
22 if (GraphTraceLen
> 18000)
23 GraphTraceLen
= 18000;
25 int i
= 2, j
, lastval
, bitidx
, half_switch
;
27 int tolerance
= clock
/ 8;
30 size_t num_blocks
= 0;
31 int lmin
= 64, lmax
= 192;
34 BigBuf_Clear_keep_EM();
35 LFSetupFPGAForADC(LF_DIVISOR_125
, true);
36 DoAcquisition_default(0, true);
38 /* Find first local max/min */
39 if (dest
[1] > dest
[0]) {
40 while (i
< GraphTraceLen
) {
41 if (!(dest
[i
] > dest
[i
- 1]) && dest
[i
] > lmax
)
47 while (i
< GraphTraceLen
) {
48 if (!(dest
[i
] < dest
[i
- 1]) && dest
[i
] < lmin
)
60 for (bitidx
= 0; i
< GraphTraceLen
; i
++) {
61 if ((dest
[i
- 1] > dest
[i
] && dir
== 1 && dest
[i
] > lmax
) || (dest
[i
- 1] < dest
[i
] && dir
== 0 && dest
[i
] < lmin
)) {
65 // Switch depending on lc length:
66 // Tolerance is 1/8 of clock rate (arbitrary)
67 if (ABS(lc
- clock
/ 4) < tolerance
) {
69 if ((i
- pmc
) == lc
) { /* 16T0 was previous one */
71 i
+= (128 + 127 + 16 + 32 + 33 + 16) - 1;
78 } else if (ABS(lc
- clock
/ 2) < tolerance
) {
80 if ((i
- pmc
) == lc
) { /* 16T0 was previous one */
82 i
+= (128 + 127 + 16 + 32 + 33) - 1;
86 } else if (half_switch
== 1) {
91 } else if (ABS(lc
- clock
) < tolerance
) {
96 if (++warnings
> 10) {
98 if (DBGLEVEL
>= DBG_EXTENDED
)
99 Dbprintf("Error: too many detection errors, aborting.");
105 if (block_done
== 1) {
107 for (j
= 0; j
< 16; ++j
) {
108 blocks
[num_blocks
][j
] =
109 128 * bits
[j
* 8 + 7] +
110 64 * bits
[j
* 8 + 6] +
111 32 * bits
[j
* 8 + 5] +
112 16 * bits
[j
* 8 + 4] +
113 8 * bits
[j
* 8 + 3] +
114 4 * bits
[j
* 8 + 2] +
115 2 * bits
[j
* 8 + 1] +
125 if (i
< GraphTraceLen
)
126 dir
= (dest
[i
- 1] > dest
[i
]) ? 0 : 1;
131 if (num_blocks
== 4) break;
133 memcpy(outBlocks
, blocks
, 16 * num_blocks
);
137 bool IsBlock0PCF7931(uint8_t *block
) {
138 // assuming all RFU bits are set to 0
139 // if PAC is enabled password is set to 0
140 if (block
[7] == 0x01) {
141 if (!memcmp(block
, "\x00\x00\x00\x00\x00\x00\x00", 7)
142 && !memcmp(block
+ 9, "\x00\x00\x00\x00\x00\x00\x00", 7)) {
145 } else if (block
[7] == 0x00) {
146 if (!memcmp(block
+ 9, "\x00\x00\x00\x00\x00\x00\x00", 7)) {
153 bool IsBlock1PCF7931(uint8_t *block
) {
154 // assuming all RFU bits are set to 0
156 uint8_t rb1
= block
[14] & 0x80;
157 uint8_t rfb
= block
[14] & 0x7f;
158 uint8_t rlb
= block
[15];
164 // block 1 is sent only if (RLB >= 1 && RFB <= 1) or RB1 enabled
168 && ((rfb
<= 1 && rlb
>= 1) || rb1
)) {
176 void ReadPCF7931(void) {
177 int found_blocks
= 0; // successfully read blocks
178 int max_blocks
= 8; // readable blocks
179 uint8_t memory_blocks
[8][17]; // PCF content
180 uint8_t single_blocks
[8][17]; // PFC blocks with unknown position
181 int single_blocks_cnt
= 0;
183 size_t n
= 0; // transmitted blocks
184 uint8_t tmp_blocks
[4][16]; // temporary read buffer
186 uint8_t found_0_1
= 0; // flag: blocks 0 and 1 were found
187 int errors
= 0; // error counter
188 int tries
= 0; // tries counter
190 memset(memory_blocks
, 0, 8 * 17 * sizeof(uint8_t));
191 memset(single_blocks
, 0, 8 * 17 * sizeof(uint8_t));
198 memset(tmp_blocks
, 0, 4 * 16 * sizeof(uint8_t));
199 n
= DemodPCF7931((uint8_t **)tmp_blocks
);
203 // exit if no block is received
204 if (errors
>= 10 && found_blocks
== 0 && single_blocks_cnt
== 0) {
206 if (DBGLEVEL
>= DBG_INFO
)
207 Dbprintf("[!!] Error, no tag or bad tag");
211 // exit if too many errors during reading
212 if (tries
> 50 && (2 * errors
> tries
)) {
214 if (DBGLEVEL
>= DBG_INFO
)
215 Dbprintf("[!!] Error reading the tag, only partial content");
220 // our logic breaks if we don't get at least two blocks
222 // skip if all 0s block or no blocks
223 if (n
== 0 || !memcmp(tmp_blocks
[0], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16))
226 // add block to single blocks list
227 if (single_blocks_cnt
< max_blocks
) {
228 for (i
= 0; i
< single_blocks_cnt
; ++i
) {
229 if (!memcmp(single_blocks
[i
], tmp_blocks
[0], 16)) {
235 memcpy(single_blocks
[single_blocks_cnt
], tmp_blocks
[0], 16);
236 print_result("got single block", single_blocks
[single_blocks_cnt
], 16);
245 if (DBGLEVEL
>= DBG_EXTENDED
)
246 Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n
, found_blocks
, (max_blocks
== 0 ? found_blocks
: max_blocks
), tries
, errors
);
248 for (i
= 0; i
< n
; ++i
) {
249 print_result("got consecutive blocks", tmp_blocks
[i
], 16);
255 if (IsBlock0PCF7931(tmp_blocks
[i
]) && IsBlock1PCF7931(tmp_blocks
[i
+ 1])) {
257 memcpy(memory_blocks
[0], tmp_blocks
[i
], 16);
258 memcpy(memory_blocks
[1], tmp_blocks
[i
+ 1], 16);
259 memory_blocks
[0][ALLOC
] = memory_blocks
[1][ALLOC
] = 1;
260 // block 1 tells how many blocks are going to be sent
261 max_blocks
= MAX((memory_blocks
[1][14] & 0x7f), memory_blocks
[1][15]) + 1;
264 Dbprintf("Found blocks 0 and 1. PCF is transmitting %d blocks.", max_blocks
);
266 // handle the following blocks
267 for (j
= i
+ 2; j
< n
; ++j
) {
268 memcpy(memory_blocks
[found_blocks
], tmp_blocks
[j
], 16);
269 memory_blocks
[found_blocks
][ALLOC
] = 1;
277 // Trying to re-order blocks
278 // Look for identical block in memory blocks
280 // skip all zeroes blocks
281 if (memcmp(tmp_blocks
[i
], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
282 for (j
= 1; j
< max_blocks
- 1; ++j
) {
283 if (!memcmp(tmp_blocks
[i
], memory_blocks
[j
], 16) && !memory_blocks
[j
+ 1][ALLOC
]) {
284 memcpy(memory_blocks
[j
+ 1], tmp_blocks
[i
+ 1], 16);
285 memory_blocks
[j
+ 1][ALLOC
] = 1;
286 if (++found_blocks
>= max_blocks
) goto end
;
290 if (memcmp(tmp_blocks
[i
+ 1], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
291 for (j
= 0; j
< max_blocks
; ++j
) {
292 if (!memcmp(tmp_blocks
[i
+ 1], memory_blocks
[j
], 16) && !memory_blocks
[(j
== 0 ? max_blocks
: j
) - 1][ALLOC
]) {
294 memcpy(memory_blocks
[max_blocks
- 1], tmp_blocks
[i
], 16);
295 memory_blocks
[max_blocks
- 1][ALLOC
] = 1;
297 memcpy(memory_blocks
[j
- 1], tmp_blocks
[i
], 16);
298 memory_blocks
[j
- 1][ALLOC
] = 1;
300 if (++found_blocks
>= max_blocks
) goto end
;
308 if (BUTTON_PRESS()) {
309 if (DBGLEVEL
>= DBG_EXTENDED
)
310 Dbprintf("Button pressed, stopping.");
314 } while (found_blocks
< max_blocks
);
317 Dbprintf("-----------------------------------------");
318 Dbprintf("Memory content:");
319 Dbprintf("-----------------------------------------");
320 for (i
= 0; i
< max_blocks
; ++i
) {
321 if (memory_blocks
[i
][ALLOC
])
322 print_result("Block", memory_blocks
[i
], 16);
324 Dbprintf("<missing block %d>", i
);
326 Dbprintf("-----------------------------------------");
328 if (found_blocks
< max_blocks
) {
329 Dbprintf("-----------------------------------------");
330 Dbprintf("Blocks with unknown position:");
331 Dbprintf("-----------------------------------------");
332 for (i
= 0; i
< single_blocks_cnt
; ++i
)
333 print_result("Block", single_blocks
[i
], 16);
335 Dbprintf("-----------------------------------------");
337 reply_mix(CMD_ACK
, 0, 0, 0, 0, 0);
340 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
) {
341 uint32_t tab
[1024] = {0}; // data times frame
346 //BUILD OF THE DATA FRAME
347 //alimentation of the tag (time for initializing)
348 AddPatternPCF7931(init_delay
, 0, 8192 / 2 * T0_PCF
, tab
);
349 AddPatternPCF7931(8192 / 2 * T0_PCF
+ 319 * T0_PCF
+ 70, 3 * T0_PCF
, 29 * T0_PCF
, tab
);
350 //password indication bit
351 AddBitPCF7931(1, tab
, l
, p
);
352 //password (on 56 bits)
353 AddBytePCF7931(pass
[0], tab
, l
, p
);
354 AddBytePCF7931(pass
[1], tab
, l
, p
);
355 AddBytePCF7931(pass
[2], tab
, l
, p
);
356 AddBytePCF7931(pass
[3], tab
, l
, p
);
357 AddBytePCF7931(pass
[4], tab
, l
, p
);
358 AddBytePCF7931(pass
[5], tab
, l
, p
);
359 AddBytePCF7931(pass
[6], tab
, l
, p
);
360 //programming mode (0 or 1)
361 AddBitPCF7931(0, tab
, l
, p
);
363 //block adress on 6 bits
364 for (u
= 0; u
< 6; ++u
) {
365 if (address
& (1 << u
)) { // bit 1
367 AddBitPCF7931(1, tab
, l
, p
);
369 AddBitPCF7931(0, tab
, l
, p
);
373 //byte address on 4 bits
374 for (u
= 0; u
< 4; ++u
) {
375 if (byte
& (1 << u
)) { // bit 1
377 AddBitPCF7931(1, tab
, l
, p
);
379 AddBitPCF7931(0, tab
, l
, p
);
383 for (u
= 0; u
< 8; u
++) {
384 if (data
& (1 << u
)) { // bit 1
386 AddBitPCF7931(1, tab
, l
, p
);
388 AddBitPCF7931(0, tab
, l
, p
);
392 if ((parity
% 2) == 0)
393 AddBitPCF7931(0, tab
, l
, p
); //even parity
395 AddBitPCF7931(1, tab
, l
, p
);//odd parity
398 AddPatternPCF7931(5120 + 2680, 0, 0, tab
);
400 //conversion of the scale time
401 for (u
= 0; u
< 500; ++u
)
402 tab
[u
] = (tab
[u
] * 3) / 2;
404 //compensation of the counter reload
407 for (u
= 0; tab
[u
] != 0; ++u
)
408 if (tab
[u
] > 0xFFFF) {
417 /* Write on a byte of a PCF7931 tag
418 * @param address : address of the block to write
419 @param byte : address of the byte to write
420 @param data : data to write
422 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
) {
424 if (DBGLEVEL
>= DBG_INFO
) {
425 Dbprintf("Initialization delay : %d us", init_delay
);
426 Dbprintf("Offsets : %d us on the low pulses width, %d us on the low pulses positions", l
, p
);
429 Dbprintf("Password (LSB first on each byte): %02x %02x %02x %02x %02x %02x %02x", pass1
, pass2
, pass3
, pass4
, pass5
, pass6
, pass7
);
430 Dbprintf("Block address : %02x", address
);
431 Dbprintf("Byte address : %02x", byte
);
432 Dbprintf("Data : %02x", data
);
434 uint8_t password
[7] = {pass1
, pass2
, pass3
, pass4
, pass5
, pass6
, pass7
};
436 RealWritePCF7931(password
, init_delay
, l
, p
, address
, byte
, data
);
440 /* Send a trame to a PCF7931 tags
441 * @param tab : array of the data frame
444 void SendCmdPCF7931(uint32_t *tab
) {
445 uint16_t u
= 0, tempo
= 0;
447 if (DBGLEVEL
>= DBG_INFO
) {
448 Dbprintf("Sending data frame...");
451 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
452 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
); //125kHz
453 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
457 // steal this pin from the SSP and use it to control the modulation
458 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
459 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
461 //initialization of the timer
462 AT91C_BASE_PMC
->PMC_PCER
|= (0x1 << AT91C_ID_TC0
);
463 AT91C_BASE_TCB
->TCB_BMR
= AT91C_TCB_TC0XC0S_NONE
| AT91C_TCB_TC1XC1S_TIOA0
| AT91C_TCB_TC2XC2S_NONE
;
464 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
465 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
; //clock at 48/32 MHz
466 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
;
467 AT91C_BASE_TCB
->TCB_BCR
= 1;
469 tempo
= AT91C_BASE_TC0
->TC_CV
;
470 for (u
= 0; tab
[u
] != 0; u
+= 3) {
473 while (tempo
!= tab
[u
])
474 tempo
= AT91C_BASE_TC0
->TC_CV
;
476 // stop modulating antenna
478 while (tempo
!= tab
[u
+ 1])
479 tempo
= AT91C_BASE_TC0
->TC_CV
;
483 while (tempo
!= tab
[u
+ 2])
484 tempo
= AT91C_BASE_TC0
->TC_CV
;
488 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
491 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
495 /* Add a byte for building the data frame of PCF7931 tags
496 * @param b : byte to add
497 * @param tab : array of the data frame
498 * @param l : offset on low pulse width
499 * @param p : offset on low pulse positioning
501 bool AddBytePCF7931(uint8_t byte
, uint32_t *tab
, int32_t l
, int32_t p
) {
503 for (u
= 0; u
< 8; ++u
) {
504 if (byte
& (1 << u
)) { //bit is 1
505 if (AddBitPCF7931(1, tab
, l
, p
) == 1) return true;
507 if (AddBitPCF7931(0, tab
, l
, p
) == 1) return true;
514 /* Add a bits for building the data frame of PCF7931 tags
515 * @param b : bit to add
516 * @param tab : array of the data frame
517 * @param l : offset on low pulse width
518 * @param p : offset on low pulse positioning
520 bool AddBitPCF7931(bool b
, uint32_t *tab
, int32_t l
, int32_t p
) {
523 //we put the cursor at the last value of the array
524 for (u
= 0; tab
[u
] != 0; u
+= 3) { };
526 if (b
== 1) { //add a bit 1
528 tab
[u
] = 34 * T0_PCF
+ p
;
530 tab
[u
] = 34 * T0_PCF
+ tab
[u
- 1] + p
;
532 tab
[u
+ 1] = 6 * T0_PCF
+ tab
[u
] + l
;
533 tab
[u
+ 2] = 88 * T0_PCF
+ tab
[u
+ 1] - l
- p
;
535 } else { //add a bit 0
538 tab
[u
] = 98 * T0_PCF
+ p
;
540 tab
[u
] = 98 * T0_PCF
+ tab
[u
- 1] + p
;
542 tab
[u
+ 1] = 6 * T0_PCF
+ tab
[u
] + l
;
543 tab
[u
+ 2] = 24 * T0_PCF
+ tab
[u
+ 1] - l
- p
;
549 /* Add a custom pattern in the data frame
550 * @param a : delay of the first high pulse
551 * @param b : delay of the low pulse
552 * @param c : delay of the last high pulse
553 * @param tab : array of the data frame
555 bool AddPatternPCF7931(uint32_t a
, uint32_t b
, uint32_t c
, uint32_t *tab
) {
557 for (u
= 0; tab
[u
] != 0; u
+= 3) {} //we put the cursor at the last value of the array
559 tab
[u
] = (u
== 0) ? a
: a
+ tab
[u
- 1];
560 tab
[u
+ 1] = b
+ tab
[u
];
561 tab
[u
+ 2] = c
+ tab
[u
+ 1];