]>
Commit | Line | Data |
---|---|---|
ac2df346 | 1 | #include "proxmark3.h" |
2 | #include "apps.h" | |
3 | #include "lfsampling.h" | |
36804420 | 4 | #include "pcf7931.h" |
ac2df346 | 5 | #include "string.h" |
36804420 | 6 | |
7 | #define T0_PCF 8 //period for the pcf7931 in us | |
8 | #define ALLOC 16 | |
9 | ||
36804420 | 10 | int DemodPCF7931(uint8_t **outBlocks) { |
11 | ||
12 | uint8_t bits[256] = {0x00}; | |
13 | uint8_t blocks[8][16]; | |
14 | uint8_t *dest = BigBuf_get_addr(); | |
15 | ||
16 | int GraphTraceLen = BigBuf_max_traceLen(); | |
17 | if ( GraphTraceLen > 18000 ) | |
18 | GraphTraceLen = 18000; | |
2efd6394 | 19 | |
36804420 | 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; | |
c0f15a05 | 28 | //clear read buffer |
29 | BigBuf_Clear_keep_EM(); | |
36804420 | 30 | |
31 | LFSetupFPGAForADC(95, true); | |
32 | DoAcquisition_default(0, true); | |
33 | ||
34 | lmin = 64; | |
35 | lmax = 192; | |
36 | ||
37 | i = 2; | |
38 | ||
39 | /* Find first local max/min */ | |
40 | if(dest[1] > dest[0]) { | |
41 | while(i < GraphTraceLen) { | |
42 | if( !(dest[i] > dest[i-1]) && dest[i] > lmax) | |
43 | break; | |
44 | i++; | |
45 | } | |
46 | dir = 0; | |
47 | } | |
48 | else { | |
49 | while(i < GraphTraceLen) { | |
50 | if( !(dest[i] < dest[i-1]) && dest[i] < lmin) | |
51 | break; | |
52 | i++; | |
53 | } | |
54 | dir = 1; | |
55 | } | |
56 | ||
57 | lastval = i++; | |
58 | half_switch = 0; | |
59 | pmc = 0; | |
60 | block_done = 0; | |
61 | ||
62 | for (bitidx = 0; i < GraphTraceLen; i++) | |
63 | { | |
64 | if ( (dest[i-1] > dest[i] && dir == 1 && dest[i] > lmax) || (dest[i-1] < dest[i] && dir == 0 && dest[i] < lmin)) | |
65 | { | |
66 | lc = i - lastval; | |
67 | lastval = i; | |
68 | ||
69 | // Switch depending on lc length: | |
70 | // Tolerance is 1/8 of clock rate (arbitrary) | |
f2c2b174 | 71 | if (ABS(lc-clock/4) < tolerance) { |
36804420 | 72 | // 16T0 |
73 | if((i - pmc) == lc) { /* 16T0 was previous one */ | |
74 | /* It's a PMC ! */ | |
75 | i += (128+127+16+32+33+16)-1; | |
76 | lastval = i; | |
77 | pmc = 0; | |
78 | block_done = 1; | |
79 | } | |
80 | else { | |
81 | pmc = i; | |
82 | } | |
f2c2b174 | 83 | } else if (ABS(lc-clock/2) < tolerance) { |
36804420 | 84 | // 32TO |
85 | if((i - pmc) == lc) { /* 16T0 was previous one */ | |
86 | /* It's a PMC ! */ | |
87 | i += (128+127+16+32+33)-1; | |
88 | lastval = i; | |
89 | pmc = 0; | |
90 | block_done = 1; | |
91 | } | |
92 | else if(half_switch == 1) { | |
93 | bits[bitidx++] = 0; | |
94 | half_switch = 0; | |
95 | } | |
96 | else | |
97 | half_switch++; | |
f2c2b174 | 98 | } else if (ABS(lc-clock) < tolerance) { |
36804420 | 99 | // 64TO |
100 | bits[bitidx++] = 1; | |
101 | } else { | |
102 | // Error | |
103 | warnings++; | |
104 | if (warnings > 10) | |
105 | { | |
106 | Dbprintf("Error: too many detection errors, aborting."); | |
107 | return 0; | |
108 | } | |
109 | } | |
110 | ||
111 | if(block_done == 1) { | |
112 | if(bitidx == 128) { | |
113 | for(j=0; j<16; j++) { | |
114 | blocks[num_blocks][j] = 128*bits[j*8+7]+ | |
115 | 64*bits[j*8+6]+ | |
116 | 32*bits[j*8+5]+ | |
117 | 16*bits[j*8+4]+ | |
118 | 8*bits[j*8+3]+ | |
119 | 4*bits[j*8+2]+ | |
120 | 2*bits[j*8+1]+ | |
121 | bits[j*8]; | |
122 | ||
123 | } | |
124 | num_blocks++; | |
125 | } | |
126 | bitidx = 0; | |
127 | block_done = 0; | |
128 | half_switch = 0; | |
129 | } | |
130 | if(i < GraphTraceLen) | |
131 | dir =(dest[i-1] > dest[i]) ? 0 : 1; | |
132 | } | |
133 | if(bitidx==255) | |
134 | bitidx=0; | |
135 | warnings = 0; | |
136 | if(num_blocks == 4) break; | |
137 | } | |
138 | memcpy(outBlocks, blocks, 16*num_blocks); | |
139 | return num_blocks; | |
140 | } | |
141 | ||
142 | int IsBlock0PCF7931(uint8_t *Block) { | |
143 | // Assume RFU means 0 :) | |
144 | 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 | |
145 | return 1; | |
146 | if((memcmp(Block+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block[7] == 0) // PAC disabled, can it *really* happen ? | |
147 | return 1; | |
148 | return 0; | |
149 | } | |
150 | ||
151 | int IsBlock1PCF7931(uint8_t *Block) { | |
152 | // Assume RFU means 0 :) | |
2efd6394 | 153 | if( Block[10] == 0 && |
154 | Block[11] == 0 && | |
155 | Block[12] == 0 && | |
156 | Block[13] == 0) | |
157 | if ( (Block[14] & 0x7f) <= 9 && Block[15] <= 9) | |
36804420 | 158 | return 1; |
36804420 | 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; | |
f2c2b174 | 205 | max_blocks = MAX((Blocks[1][14] & 0x7f), Blocks[1][15]) + 1; |
36804420 | 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 | ||
e16054a4 | 271 | cmd_send(CMD_ACK,0,0,0,0,0); |
36804420 | 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 | { | |
2efd6394 | 282 | uint32_t tab[1024] = {0}; // data times frame |
36804420 | 283 | uint32_t u = 0; |
284 | uint8_t parity = 0; | |
285 | bool comp = 0; | |
286 | ||
287 | //BUILD OF THE DATA FRAME | |
288 | ||
289 | //alimentation of the tag (time for initializing) | |
290 | AddPatternPCF7931(init_delay, 0, 8192/2*T0_PCF, tab); | |
291 | ||
292 | //PMC | |
293 | Dbprintf("Initialization delay : %d us", init_delay); | |
294 | AddPatternPCF7931(8192/2*T0_PCF + 319*T0_PCF+70, 3*T0_PCF, 29*T0_PCF, tab); | |
295 | ||
296 | Dbprintf("Offsets : %d us on the low pulses width, %d us on the low pulses positions", l, p); | |
297 | ||
298 | //password indication bit | |
299 | AddBitPCF7931(1, tab, l, p); | |
300 | ||
36804420 | 301 | //password (on 56 bits) |
302 | Dbprintf("Password (LSB first on each byte) : %02x %02x %02x %02x %02x %02x %02x", pass1,pass2,pass3,pass4,pass5,pass6,pass7); | |
303 | AddBytePCF7931(pass1, tab, l, p); | |
304 | AddBytePCF7931(pass2, tab, l, p); | |
305 | AddBytePCF7931(pass3, tab, l, p); | |
306 | AddBytePCF7931(pass4, tab, l, p); | |
307 | AddBytePCF7931(pass5, tab, l, p); | |
308 | AddBytePCF7931(pass6, tab, l, p); | |
309 | AddBytePCF7931(pass7, tab, l, p); | |
310 | ||
311 | ||
312 | //programming mode (0 or 1) | |
313 | AddBitPCF7931(0, tab, l, p); | |
314 | ||
315 | //block adress on 6 bits | |
316 | Dbprintf("Block address : %02x", address); | |
317 | for (u=0; u<6; u++) | |
318 | { | |
319 | if (address&(1<<u)) { // bit 1 | |
320 | parity++; | |
321 | AddBitPCF7931(1, tab, l, p); | |
322 | } else{ // bit 0 | |
323 | AddBitPCF7931(0, tab, l, p); | |
324 | } | |
325 | } | |
326 | ||
327 | //byte address on 4 bits | |
328 | Dbprintf("Byte address : %02x", byte); | |
329 | for (u=0; u<4; u++) | |
330 | { | |
331 | if (byte&(1<<u)) { // bit 1 | |
332 | parity++; | |
333 | AddBitPCF7931(1, tab, l, p); | |
334 | } else{ // bit 0 | |
335 | AddBitPCF7931(0, tab, l, p); | |
336 | } | |
337 | } | |
338 | ||
339 | //data on 8 bits | |
340 | Dbprintf("Data : %02x", data); | |
341 | for (u=0; u<8; u++) | |
342 | { | |
343 | if (data&(1<<u)) { // bit 1 | |
344 | parity++; | |
345 | AddBitPCF7931(1, tab, l, p); | |
346 | } else{ //bit 0 | |
347 | AddBitPCF7931(0, tab, l, p); | |
348 | } | |
349 | } | |
350 | ||
351 | ||
352 | //parity bit | |
353 | if((parity%2)==0){ | |
354 | AddBitPCF7931(0, tab, l, p); //even parity | |
355 | }else{ | |
356 | AddBitPCF7931(1, tab, l, p);//odd parity | |
357 | } | |
358 | ||
359 | //time access memory | |
360 | AddPatternPCF7931(5120+2680, 0, 0, tab); | |
361 | ||
362 | //conversion of the scale time | |
363 | for(u=0;u<500;u++){ | |
364 | tab[u]=(tab[u] * 3)/2; | |
365 | } | |
366 | ||
2efd6394 | 367 | //compensation of the counter reload |
36804420 | 368 | while (!comp){ |
369 | comp = 1; | |
370 | for(u=0;tab[u]!=0;u++){ | |
371 | if(tab[u] > 0xFFFF){ | |
372 | tab[u] -= 0xFFFF; | |
373 | comp = 0; | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
378 | SendCmdPCF7931(tab); | |
379 | } | |
380 | ||
381 | ||
382 | ||
383 | /* Send a trame to a PCF7931 tags | |
384 | * @param tab : array of the data frame | |
385 | */ | |
386 | ||
387 | void SendCmdPCF7931(uint32_t * tab){ | |
614da335 | 388 | uint16_t u=0, tempo=0; |
36804420 | 389 | |
614da335 | 390 | Dbprintf("Sending data frame..."); |
36804420 | 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 | |
f4d7d1fe | 405 | AT91C_BASE_PMC->PMC_PCER |= (0x1 << AT91C_ID_TC0); |
36804420 | 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; | |
614da335 | 414 | for( u = 0; tab[u] != 0; u += 3){ |
36804420 | 415 | |
416 | // modulate antenna | |
417 | HIGH(GPIO_SSC_DOUT); | |
614da335 | 418 | while(tempo != tab[u]) tempo = AT91C_BASE_TC0->TC_CV; |
36804420 | 419 | |
420 | // stop modulating antenna | |
421 | LOW(GPIO_SSC_DOUT); | |
614da335 | 422 | while(tempo != tab[u+1]) tempo = AT91C_BASE_TC0->TC_CV; |
36804420 | 423 | |
424 | // modulate antenna | |
425 | HIGH(GPIO_SSC_DOUT); | |
614da335 | 426 | while(tempo != tab[u+2]) tempo = AT91C_BASE_TC0->TC_CV; |
36804420 | 427 | } |
428 | ||
429 | LED_A_OFF(); | |
430 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); | |
431 | SpinDelay(200); | |
432 | ||
36804420 | 433 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable |
36804420 | 434 | LED(0xFFFF, 1000); |
435 | } | |
436 | ||
437 | ||
438 | /* Add a byte for building the data frame of PCF7931 tags | |
439 | * @param b : byte to add | |
440 | * @param tab : array of the data frame | |
441 | * @param l : offset on low pulse width | |
442 | * @param p : offset on low pulse positioning | |
443 | */ | |
444 | ||
445 | bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p){ | |
446 | ||
447 | uint32_t u; | |
614da335 | 448 | for ( u=0; u<8; u++) |
36804420 | 449 | { |
450 |