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