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