]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/pcf7931.c
ADD: @marshmellows42 's fixes for "lf cmdread" and CHANGELOG.md
[proxmark3-svn] / armsrc / pcf7931.c
1 #include "proxmark3.h"
2 #include "apps.h"
3 #include "lfsampling.h"
4 #include "pcf7931.h"
5 #include "string.h"
6
7 #define T0_PCF 8 //period for the pcf7931 in us
8 #define ALLOC 16
9
10 #define abs(x) ( ((x)<0) ? -(x) : (x) )
11 #define max(x,y) ( x<y ? y:x)
12
13 int DemodPCF7931(uint8_t **outBlocks) {
14
15 uint8_t bits[256] = {0x00};
16 uint8_t blocks[8][16];
17 uint8_t *dest = BigBuf_get_addr();
18
19 int GraphTraceLen = BigBuf_max_traceLen();
20 if ( GraphTraceLen > 18000 )
21 GraphTraceLen = 18000;
22
23
24 int i, j, lastval, bitidx, half_switch;
25 int clock = 64;
26 int tolerance = clock / 8;
27 int pmc, block_done;
28 int lc, warnings = 0;
29 int num_blocks = 0;
30 int lmin=128, lmax=128;
31 uint8_t dir;
32
33 LFSetupFPGAForADC(95, true);
34 DoAcquisition_default(0, true);
35
36 lmin = 64;
37 lmax = 192;
38
39 i = 2;
40
41 /* Find first local max/min */
42 if(dest[1] > dest[0]) {
43 while(i < GraphTraceLen) {
44 if( !(dest[i] > dest[i-1]) && dest[i] > lmax)
45 break;
46 i++;
47 }
48 dir = 0;
49 }
50 else {
51 while(i < GraphTraceLen) {
52 if( !(dest[i] < dest[i-1]) && dest[i] < lmin)
53 break;
54 i++;
55 }
56 dir = 1;
57 }
58
59 lastval = i++;
60 half_switch = 0;
61 pmc = 0;
62 block_done = 0;
63
64 for (bitidx = 0; i < GraphTraceLen; i++)
65 {
66 if ( (dest[i-1] > dest[i] && dir == 1 && dest[i] > lmax) || (dest[i-1] < dest[i] && dir == 0 && dest[i] < lmin))
67 {
68 lc = i - lastval;
69 lastval = i;
70
71 // Switch depending on lc length:
72 // Tolerance is 1/8 of clock rate (arbitrary)
73 if (abs(lc-clock/4) < tolerance) {
74 // 16T0
75 if((i - pmc) == lc) { /* 16T0 was previous one */
76 /* It's a PMC ! */
77 i += (128+127+16+32+33+16)-1;
78 lastval = i;
79 pmc = 0;
80 block_done = 1;
81 }
82 else {
83 pmc = i;
84 }
85 } else if (abs(lc-clock/2) < tolerance) {
86 // 32TO
87 if((i - pmc) == lc) { /* 16T0 was previous one */
88 /* It's a PMC ! */
89 i += (128+127+16+32+33)-1;
90 lastval = i;
91 pmc = 0;
92 block_done = 1;
93 }
94 else if(half_switch == 1) {
95 bits[bitidx++] = 0;
96 half_switch = 0;
97 }
98 else
99 half_switch++;
100 } else if (abs(lc-clock) < tolerance) {
101 // 64TO
102 bits[bitidx++] = 1;
103 } else {
104 // Error
105 warnings++;
106 if (warnings > 10)
107 {
108 Dbprintf("Error: too many detection errors, aborting.");
109 return 0;
110 }
111 }
112
113 if(block_done == 1) {
114 if(bitidx == 128) {
115 for(j=0; j<16; j++) {
116 blocks[num_blocks][j] = 128*bits[j*8+7]+
117 64*bits[j*8+6]+
118 32*bits[j*8+5]+
119 16*bits[j*8+4]+
120 8*bits[j*8+3]+
121 4*bits[j*8+2]+
122 2*bits[j*8+1]+
123 bits[j*8];
124
125 }
126 num_blocks++;
127 }
128 bitidx = 0;
129 block_done = 0;
130 half_switch = 0;
131 }
132 if(i < GraphTraceLen)
133 dir =(dest[i-1] > dest[i]) ? 0 : 1;
134 }
135 if(bitidx==255)
136 bitidx=0;
137 warnings = 0;
138 if(num_blocks == 4) break;
139 }
140 memcpy(outBlocks, blocks, 16*num_blocks);
141 return num_blocks;
142 }
143
144 int IsBlock0PCF7931(uint8_t *Block) {
145 // Assume RFU means 0 :)
146 if((memcmp(Block, "\x00\x00\x00\x00\x00\x00\x00\x01", 8) == 0) && memcmp(Block+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) // PAC enabled
147 return 1;
148 if((memcmp(Block+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block[7] == 0) // PAC disabled, can it *really* happen ?
149 return 1;
150 return 0;
151 }
152
153 int IsBlock1PCF7931(uint8_t *Block) {
154 // Assume RFU means 0 :)
155 if(Block[10] == 0 && Block[11] == 0 && Block[12] == 0 && Block[13] == 0)
156 if((Block[14] & 0x7f) <= 9 && Block[15] <= 9)
157 return 1;
158
159 return 0;
160 }
161
162 void ReadPCF7931() {
163 uint8_t Blocks[8][17];
164 uint8_t tmpBlocks[4][16];
165 int i, j, ind, ind2, n;
166 int num_blocks = 0;
167 int max_blocks = 8;
168 int ident = 0;
169 int error = 0;
170 int tries = 0;
171
172 memset(Blocks, 0, 8*17*sizeof(uint8_t));
173
174 do {
175 memset(tmpBlocks, 0, 4*16*sizeof(uint8_t));
176 n = DemodPCF7931((uint8_t**)tmpBlocks);
177 if(!n)
178 error++;
179 if(error==10 && num_blocks == 0) {
180 Dbprintf("Error, no tag or bad tag");
181 return;
182 }
183 else if (tries==20 || error==10) {
184 Dbprintf("Error reading the tag");
185 Dbprintf("Here is the partial content");
186 goto end;
187 }
188
189 for(i=0; i<n; i++)
190 Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
191 tmpBlocks[i][0], tmpBlocks[i][1], tmpBlocks[i][2], tmpBlocks[i][3], tmpBlocks[i][4], tmpBlocks[i][5], tmpBlocks[i][6], tmpBlocks[i][7],
192 tmpBlocks[i][8], tmpBlocks[i][9], tmpBlocks[i][10], tmpBlocks[i][11], tmpBlocks[i][12], tmpBlocks[i][13], tmpBlocks[i][14], tmpBlocks[i][15]);
193 if(!ident) {
194 for(i=0; i<n; i++) {
195 if(IsBlock0PCF7931(tmpBlocks[i])) {
196 // Found block 0 ?
197 if(i < n-1 && IsBlock1PCF7931(tmpBlocks[i+1])) {
198 // Found block 1!
199 // \o/
200 ident = 1;
201 memcpy(Blocks[0], tmpBlocks[i], 16);
202 Blocks[0][ALLOC] = 1;
203 memcpy(Blocks[1], tmpBlocks[i+1], 16);
204 Blocks[1][ALLOC] = 1;
205 max_blocks = max((Blocks[1][14] & 0x7f), Blocks[1][15]) + 1;
206 // Debug print
207 Dbprintf("(dbg) Max blocks: %d", max_blocks);
208 num_blocks = 2;
209 // Handle following blocks
210 for(j=i+2, ind2=2; j!=i; j++, ind2++, num_blocks++) {
211 if(j==n) j=0;
212 if(j==i) break;
213 memcpy(Blocks[ind2], tmpBlocks[j], 16);
214 Blocks[ind2][ALLOC] = 1;
215 }
216 break;
217 }
218 }
219 }
220 }
221 else {
222 for(i=0; i<n; i++) { // Look for identical block in known blocks
223 if(memcmp(tmpBlocks[i], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) { // Block is not full of 00
224 for(j=0; j<max_blocks; j++) {
225 if(Blocks[j][ALLOC] == 1 && !memcmp(tmpBlocks[i], Blocks[j], 16)) {
226 // Found an identical block
227 for(ind=i-1,ind2=j-1; ind >= 0; ind--,ind2--) {
228 if(ind2 < 0)
229 ind2 = max_blocks;
230 if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found
231 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
232 memcpy(Blocks[ind2], tmpBlocks[ind], 16);
233 Blocks[ind2][ALLOC] = 1;
234 num_blocks++;
235 if(num_blocks == max_blocks) goto end;
236 }
237 }
238 for(ind=i+1,ind2=j+1; ind < n; ind++,ind2++) {
239 if(ind2 > max_blocks)
240 ind2 = 0;
241 if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found
242 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
243 memcpy(Blocks[ind2], tmpBlocks[ind], 16);
244 Blocks[ind2][ALLOC] = 1;
245 num_blocks++;
246 if(num_blocks == max_blocks) goto end;
247 }
248 }
249 }
250 }
251 }
252 }
253 }
254 tries++;
255 if (BUTTON_PRESS()) return;
256 } while (num_blocks != max_blocks);
257 end:
258 Dbprintf("-----------------------------------------");
259 Dbprintf("Memory content:");
260 Dbprintf("-----------------------------------------");
261 for(i=0; i<max_blocks; i++) {
262 if(Blocks[i][ALLOC]==1)
263 Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
264 Blocks[i][0], Blocks[i][1], Blocks[i][2], Blocks[i][3], Blocks[i][4], Blocks[i][5], Blocks[i][6], Blocks[i][7],
265 Blocks[i][8], Blocks[i][9], Blocks[i][10], Blocks[i][11], Blocks[i][12], Blocks[i][13], Blocks[i][14], Blocks[i][15]);
266 else
267 Dbprintf("<missing block %d>", i);
268 }
269 Dbprintf("-----------------------------------------");
270
271 cmd_send(CMD_ACK,0,0,0,0,0);
272 }
273
274
275 /* Write on a byte of a PCF7931 tag
276 * @param address : address of the block to write
277 @param byte : address of the byte to write
278 @param data : data to write
279 */
280 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)
281 {
282
283 uint32_t tab[1024]={0}; // data times frame
284 uint32_t u = 0;
285 uint8_t parity = 0;
286 bool comp = 0;
287
288 //BUILD OF THE DATA FRAME
289
290 //alimentation of the tag (time for initializing)
291 AddPatternPCF7931(init_delay, 0, 8192/2*T0_PCF, tab);
292
293 //PMC
294 Dbprintf("Initialization delay : %d us", init_delay);
295 AddPatternPCF7931(8192/2*T0_PCF + 319*T0_PCF+70, 3*T0_PCF, 29*T0_PCF, tab);
296
297 Dbprintf("Offsets : %d us on the low pulses width, %d us on the low pulses positions", l, p);
298
299 //password indication bit
300 AddBitPCF7931(1, tab, l, p);
301
302
303 //password (on 56 bits)
304 Dbprintf("Password (LSB first on each byte) : %02x %02x %02x %02x %02x %02x %02x", pass1,pass2,pass3,pass4,pass5,pass6,pass7);
305 AddBytePCF7931(pass1, tab, l, p);
306 AddBytePCF7931(pass2, tab, l, p);
307 AddBytePCF7931(pass3, tab, l, p);
308 AddBytePCF7931(pass4, tab, l, p);
309 AddBytePCF7931(pass5, tab, l, p);
310 AddBytePCF7931(pass6, tab, l, p);
311 AddBytePCF7931(pass7, tab, l, p);
312
313
314 //programming mode (0 or 1)
315 AddBitPCF7931(0, tab, l, p);
316
317 //block adress on 6 bits
318 Dbprintf("Block address : %02x", address);
319 for (u=0; u<6; u++)
320 {
321 if (address&(1<<u)) { // bit 1
322 parity++;
323 AddBitPCF7931(1, tab, l, p);
324 } else{ // bit 0
325 AddBitPCF7931(0, tab, l, p);
326 }
327 }
328
329 //byte address on 4 bits
330 Dbprintf("Byte address : %02x", byte);
331 for (u=0; u<4; u++)
332 {
333 if (byte&(1<<u)) { // bit 1
334 parity++;
335 AddBitPCF7931(1, tab, l, p);
336 } else{ // bit 0
337 AddBitPCF7931(0, tab, l, p);
338 }
339 }
340
341 //data on 8 bits
342 Dbprintf("Data : %02x", data);
343 for (u=0; u<8; u++)
344 {
345 if (data&(1<<u)) { // bit 1
346 parity++;
347 AddBitPCF7931(1, tab, l, p);
348 } else{ //bit 0
349 AddBitPCF7931(0, tab, l, p);
350 }
351 }
352
353
354 //parity bit
355 if((parity%2)==0){
356 AddBitPCF7931(0, tab, l, p); //even parity
357 }else{
358 AddBitPCF7931(1, tab, l, p);//odd parity
359 }
360
361 //time access memory
362 AddPatternPCF7931(5120+2680, 0, 0, tab);
363
364 //conversion of the scale time
365 for(u=0;u<500;u++){
366 tab[u]=(tab[u] * 3)/2;
367 }
368
369
370 //compennsation of the counter reload
371 while (!comp){
372 comp = 1;
373 for(u=0;tab[u]!=0;u++){
374 if(tab[u] > 0xFFFF){
375 tab[u] -= 0xFFFF;
376 comp = 0;
377 }
378 }
379 }
380
381 SendCmdPCF7931(tab);
382 }
383
384
385
386 /* Send a trame to a PCF7931 tags
387 * @param tab : array of the data frame
388 */
389
390 void SendCmdPCF7931(uint32_t * tab){
391 uint16_t u=0;
392 uint16_t tempo=0;
393
394 Dbprintf("SENDING DATA FRAME...");
395
396 FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
397
398 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
399
400 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU );
401
402 LED_A_ON();
403
404 // steal this pin from the SSP and use it to control the modulation
405 AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
406 AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
407
408 //initialization of the timer
409 AT91C_BASE_PMC->PMC_PCER |= (0x1 << 12) | (0x1 << 13) | (0x1 << 14);
410 AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
411 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
412 AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK; //clock at 48/32 MHz
413 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN;
414 AT91C_BASE_TCB->TCB_BCR = 1;
415
416
417 tempo = AT91C_BASE_TC0->TC_CV;
418 for(u=0;tab[u]!= 0;u+=3){
419
420
421 // modulate antenna
422 HIGH(GPIO_SSC_DOUT);
423 while(tempo != tab[u]){
424 tempo = AT91C_BASE_TC0->TC_CV;
425 }
426
427 // stop modulating antenna
428 LOW(GPIO_SSC_DOUT);
429 while(tempo != tab[u+1]){
430 tempo = AT91C_BASE_TC0->TC_CV;
431 }
432
433
434 // modulate antenna
435 HIGH(GPIO_SSC_DOUT);
436 while(tempo != tab[u+2]){
437 tempo = AT91C_BASE_TC0->TC_CV;
438 }
439 }
440
441 LED_A_OFF();
442 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
443 SpinDelay(200);
444
445 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
446 LED(0xFFFF, 1000);
447 }
448
449
450 /* Add a byte for building the data frame of PCF7931 tags
451 * @param b : byte to add
452 * @param tab : array of the data frame
453 * @param l : offset on low pulse width
454 * @param p : offset on low pulse positioning
455 */
456
457 bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p){
458
459 uint32_t u;
460 for (u=0; u<8; u++)
461 {
462 if (byte&(1<<u)) { //bit à 1
463 if(AddBitPCF7931(1, tab, l, p)==1)return 1;
464 } else { //bit à 0
465 if(AddBitPCF7931(0, tab, l, p)==1)return 1;
466 }
467 }
468
469 return 0;
470 }
471
472 /* Add a bits for building the data frame of PCF7931 tags
473 * @param b : bit to add
474 * @param tab : array of the data frame
475 * @param l : offset on low pulse width
476 * @param p : offset on low pulse positioning
477 */
478 bool AddBitPCF7931(bool b, uint32_t * tab, int32_t l, int32_t p){
479 uint8_t u = 0;
480
481 for(u=0;tab[u]!=0;u+=3){} //we put the cursor at the last value of the array
482
483
484 if(b==1){ //add a bit 1
485 if(u==0) tab[u] = 34*T0_PCF+p;
486 else tab[u] = 34*T0_PCF+tab[u-1]+p;
487
488 tab[u+1] = 6*T0_PCF+tab[u]+l;
489 tab[u+2] = 88*T0_PCF+tab[u+1]-l-p;
490 return 0;
491 }else{ //add a bit 0
492
493 if(u==0) tab[u] = 98*T0_PCF+p;
494 else tab[u] = 98*T0_PCF+tab[u-1]+p;
495
496 tab[u+1] = 6*T0_PCF+tab[u]+l;
497 tab[u+2] = 24*T0_PCF+tab[u+1]-l-p;
498 return 0;
499 }
500
501
502 return 1;
503 }
504
505 /* Add a custom pattern in the data frame
506 * @param a : delay of the first high pulse
507 * @param b : delay of the low pulse
508 * @param c : delay of the last high pulse
509 * @param tab : array of the data frame
510 */
511 bool AddPatternPCF7931(uint32_t a, uint32_t b, uint32_t c, uint32_t * tab){
512 uint32_t u = 0;
513 for(u=0;tab[u]!=0;u+=3){} //we put the cursor at the last value of the array
514
515 if(u==0) tab[u] = a;
516 else tab[u] = a + tab[u-1];
517
518 tab[u+1] = b+tab[u];
519 tab[u+2] = c+tab[u+1];
520
521 return 0;
522 }
Impressum, Datenschutz