]>
Commit | Line | Data |
---|---|---|
d10e08ae | 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 | ||
d10e08ae | 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; | |
19 | ||
20 | ||
21 | int i, j, lastval, bitidx, half_switch; | |
22 | int clock = 64; | |
23 | int tolerance = clock / 8; | |
24 | int pmc, block_done; | |
25 | int lc, warnings = 0; | |
26 | int num_blocks = 0; | |
27 | int lmin=128, lmax=128; | |
28 | uint8_t dir; | |
3cec7061 | 29 | //clear read buffer |
29b75739 | 30 | BigBuf_Clear_keep_EM(); |
d10e08ae | 31 | |
32 | LFSetupFPGAForADC(95, true); | |
33 | DoAcquisition_default(0, true); | |
34 | ||
35 | lmin = 64; | |
36 | lmax = 192; | |
37 | ||
38 | i = 2; | |
39 | ||
40 | /* Find first local max/min */ | |
41 | if(dest[1] > dest[0]) { | |
42 | while(i < GraphTraceLen) { | |
43 | if( !(dest[i] > dest[i-1]) && dest[i] > lmax) | |
44 | break; | |
45 | i++; | |
46 | } | |
47 | dir = 0; | |
48 | } | |
49 | else { | |
50 | while(i < GraphTraceLen) { | |
51 | if( !(dest[i] < dest[i-1]) && dest[i] < lmin) | |
52 | break; | |
53 | i++; | |
54 | } | |
55 | dir = 1; | |
56 | } | |
57 | ||
58 | lastval = i++; | |
59 | half_switch = 0; | |
60 | pmc = 0; | |
61 | block_done = 0; | |
62 | ||
63 | for (bitidx = 0; i < GraphTraceLen; i++) | |
64 | { | |
65 | if ( (dest[i-1] > dest[i] && dir == 1 && dest[i] > lmax) || (dest[i-1] < dest[i] && dir == 0 && dest[i] < lmin)) | |
66 | { | |
67 | lc = i - lastval; | |
68 | lastval = i; | |
69 | ||
70 | // Switch depending on lc length: | |
71 | // Tolerance is 1/8 of clock rate (arbitrary) | |
cf194819 | 72 | if (ABS(lc-clock/4) < tolerance) { |
d10e08ae | 73 | // 16T0 |
74 | if((i - pmc) == lc) { /* 16T0 was previous one */ | |
75 | /* It's a PMC ! */ | |
76 | i += (128+127+16+32+33+16)-1; | |
77 | lastval = i; | |
78 | pmc = 0; | |
79 | block_done = 1; | |
80 | } | |
81 | else { | |
82 | pmc = i; | |
83 | } | |
cf194819 | 84 | } else if (ABS(lc-clock/2) < tolerance) { |
d10e08ae | 85 | // 32TO |
86 | if((i - pmc) == lc) { /* 16T0 was previous one */ | |
87 | /* It's a PMC ! */ | |
88 | i += (128+127+16+32+33)-1; | |
89 | lastval = i; | |
90 | pmc = 0; | |
91 | block_done = 1; | |
92 | } | |
93 | else if(half_switch == 1) { | |
94 | bits[bitidx++] = 0; | |
95 | half_switch = 0; | |
96 | } | |
97 | else | |
98 | half_switch++; | |
cf194819 | 99 | } else if (ABS(lc-clock) < tolerance) { |
d10e08ae | 100 | // 64TO |
101 | bits[bitidx++] = 1; | |
102 | } else { | |
103 | // Error | |
104 | warnings++; | |
105 | if (warnings > 10) | |
106 | { | |
107 | Dbprintf("Error: too many detection errors, aborting."); | |
108 | return 0; | |
109 | } | |
110 | } | |
111 | ||
112 | if(block_done == 1) { | |
113 | if(bitidx == 128) { | |
114 | for(j=0; j<16; j++) { | |
115 | blocks[num_blocks][j] = 128*bits[j*8+7]+ | |
116 | 64*bits[j*8+6]+ | |
117 | 32*bits[j*8+5]+ | |
118 | 16*bits[j*8+4]+ | |
119 | 8*bits[j*8+3]+ | |
120 | 4*bits[j*8+2]+ | |
121 | 2*bits[j*8+1]+ | |
122 | bits[j*8]; | |
123 | ||
124 | } | |
125 | num_blocks++; | |
126 | } | |
127 | bitidx = 0; | |
128 | block_done = 0; | |
129 | half_switch = 0; | |
130 | } | |
131 | if(i < GraphTraceLen) | |
132 | dir =(dest[i-1] > dest[i]) ? 0 : 1; | |
133 | } | |
134 | if(bitidx==255) | |
135 | bitidx=0; | |
136 | warnings = 0; | |
137 | if(num_blocks == 4) break; | |
138 | } | |
139 | memcpy(outBlocks, blocks, 16*num_blocks); | |
140 | return num_blocks; | |
141 | } | |
142 | ||
143 | int IsBlock0PCF7931(uint8_t *Block) { | |
144 | // Assume RFU means 0 :) | |
145 | 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 | |
146 | return 1; | |
147 | if((memcmp(Block+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block[7] == 0) // PAC disabled, can it *really* happen ? | |
148 | return 1; | |
149 | return 0; | |
150 | } | |
151 | ||
152 | int IsBlock1PCF7931(uint8_t *Block) { | |
153 | // Assume RFU means 0 :) | |
154 | if(Block[10] == 0 && Block[11] == 0 && Block[12] == 0 && Block[13] == 0) | |
155 | if((Block[14] & 0x7f) <= 9 && Block[15] <= 9) | |
156 | return 1; | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | void ReadPCF7931() { | |
162 | uint8_t Blocks[8][17]; | |
163 | uint8_t tmpBlocks[4][16]; | |
164 | int i, j, ind, ind2, n; | |
165 | int num_blocks = 0; | |
166 | int max_blocks = 8; | |
167 | int ident = 0; | |
168 | int error = 0; | |
169 | int tries = 0; | |
170 | ||
171 | memset(Blocks, 0, 8*17*sizeof(uint8_t)); | |
172 | ||
173 | do { | |
174 | memset(tmpBlocks, 0, 4*16*sizeof(uint8_t)); | |
175 | n = DemodPCF7931((uint8_t**)tmpBlocks); | |
176 | if(!n) | |
177 | error++; | |
178 | if(error==10 && num_blocks == 0) { | |
179 | Dbprintf("Error, no tag or bad tag"); | |
180 | return; | |
181 | } | |
182 | else if (tries==20 || error==10) { | |
183 | Dbprintf("Error reading the tag"); | |
184 | Dbprintf("Here is the partial content"); | |
185 | goto end; | |
186 | } | |
187 | ||
188 | for(i=0; i<n; i++) | |
189 | Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", | |
190 | tmpBlocks[i][0], tmpBlocks[i][1], tmpBlocks[i][2], tmpBlocks[i][3], tmpBlocks[i][4], tmpBlocks[i][5], tmpBlocks[i][6], tmpBlocks[i][7], | |
191 | tmpBlocks[i][8], tmpBlocks[i][9], tmpBlocks[i][10], tmpBlocks[i][11], tmpBlocks[i][12], tmpBlocks[i][13], tmpBlocks[i][14], tmpBlocks[i][15]); | |
192 | if(!ident) { | |
193 | for(i=0; i<n; i++) { | |
194 | if(IsBlock0PCF7931(tmpBlocks[i])) { | |
195 | // Found block 0 ? | |
196 | if(i < n-1 && IsBlock1PCF7931(tmpBlocks[i+1])) { | |
197 | // Found block 1! | |
198 | // \o/ | |
199 | ident = 1; | |
200 | memcpy(Blocks[0], tmpBlocks[i], 16); | |
201 | Blocks[0][ALLOC] = 1; | |
202 | memcpy(Blocks[1], tmpBlocks[i+1], 16); | |
203 | Blocks[1][ALLOC] = 1; | |
cf194819 | 204 | max_blocks = MAX((Blocks[1][14] & 0x7f), Blocks[1][15]) + 1; |
d10e08ae | 205 | // Debug print |
206 | Dbprintf("(dbg) Max blocks: %d", max_blocks); | |
207 | num_blocks = 2; | |
208 | // Handle following blocks | |
209 | for(j=i+2, ind2=2; j!=i; j++, ind2++, num_blocks++) { | |
210 | if(j==n) j=0; | |
211 | if(j==i) break; | |
212 | memcpy(Blocks[ind2], tmpBlocks[j], 16); | |
213 | Blocks[ind2][ALLOC] = 1; | |
214 | } | |
215 | break; | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | else { | |
221 | for(i=0; i<n; i++) { // Look for identical block in known blocks | |
222 | 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 | |
223 | for(j=0; j<max_blocks; j++) { | |
224 | if(Blocks[j][ALLOC] == 1 && !memcmp(tmpBlocks[i], Blocks[j], 16)) { | |
225 | // Found an identical block | |
226 | for(ind=i-1,ind2=j-1; ind >= 0; ind--,ind2--) { | |
227 | if(ind2 < 0) | |
228 | ind2 = max_blocks; | |
229 | if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found | |
230 | // Dbprintf("Tmp %d -> Block %d", ind, ind2); | |
231 | memcpy(Blocks[ind2], tmpBlocks[ind], 16); | |
232 | Blocks[ind2][ALLOC] = 1; | |
233 | num_blocks++; | |
234 | if(num_blocks == max_blocks) goto end; | |
235 | } | |
236 | } | |
237 | for(ind=i+1,ind2=j+1; ind < n; ind++,ind2++) { | |
238 | if(ind2 > max_blocks) | |
239 | ind2 = 0; | |
240 | if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found | |
241 | // Dbprintf("Tmp %d -> Block %d", ind, ind2); | |
242 | memcpy(Blocks[ind2], tmpBlocks[ind], 16); | |
243 | Blocks[ind2][ALLOC] = 1; | |
244 | num_blocks++; | |
245 | if(num_blocks == max_blocks) goto end; | |
246 | } | |
247 | } | |
248 | } | |
249 | } | |
250 | } | |
251 | } | |
252 | } | |
253 | tries++; | |
254 | if (BUTTON_PRESS()) return; | |
255 | } while (num_blocks != max_blocks); | |
256 | end: | |
257 | Dbprintf("-----------------------------------------"); | |
258 | Dbprintf("Memory content:"); | |
259 | Dbprintf("-----------------------------------------"); | |
260 | for(i=0; i<max_blocks; i++) { | |
261 | if(Blocks[i][ALLOC]==1) | |
262 | Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", | |
263 | Blocks[i][0], Blocks[i][1], Blocks[i][2], Blocks[i][3], Blocks[i][4], Blocks[i][5], Blocks[i][6], Blocks[i][7], | |
264 | Blocks[i][8], Blocks[i][9], Blocks[i][10], Blocks[i][11], Blocks[i][12], Blocks[i][13], Blocks[i][14], Blocks[i][15]); | |
265 | else | |
266 | Dbprintf("<missing block %d>", i); | |
267 | } | |
268 | Dbprintf("-----------------------------------------"); | |
269 | ||
7cfc777b | 270 | cmd_send(CMD_ACK,0,0,0,0,0); |
d10e08ae | 271 | } |
272 | ||
273 | ||
274 | /* Write on a byte of a PCF7931 tag | |
275 | * @param address : address of the block to write | |
276 | @param byte : address of the byte to write | |
277 | @param data : data to write | |
278 | */ | |
279 | 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) | |
280 | { | |
281 | ||
282 | uint32_t tab[1024]={0}; // data times frame | |
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 | ||
301 | ||
302 | //password (on 56 bits) | |
303 | Dbprintf("Password (LSB first on each byte) : %02x %02x %02x %02x %02x %02x %02x", pass1,pass2,pass3,pass4,pass5,pass6,pass7); | |
304 | AddBytePCF7931(pass1, tab, l, p); | |
305 | AddBytePCF7931(pass2, tab, l, p); | |
306 | AddBytePCF7931(pass3, tab, l, p); | |
307 | AddBytePCF7931(pass4, tab, l, p); | |
308 | AddBytePCF7931(pass5, tab, l, p); | |
309 | AddBytePCF7931(pass6, tab, l, p); | |
310 | AddBytePCF7931(pass7, tab, l, p); | |
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 | ||
367 | ||
368 | //compennsation of the counter reload | |
369 | while (!comp){ | |
370 | comp = 1; | |
371 | for(u=0;tab[u]!=0;u++){ | |
372 | if(tab[u] > 0xFFFF){ | |
373 | tab[u] -= 0xFFFF; | |
374 | comp = 0; | |
375 | } | |
376 | } | |
377 | } | |
378 | ||
379 | SendCmdPCF7931(tab); | |
380 | } | |
381 | ||
382 | ||
383 | ||
384 | /* Send a trame to a PCF7931 tags | |
385 | * @param tab : array of the data frame | |
386 | */ | |
387 | ||
388 | void SendCmdPCF7931(uint32_t * tab){ | |
389 | uint16_t u=0; | |
390 | uint16_t tempo=0; | |
391 | ||
392 | Dbprintf("SENDING DATA FRAME..."); | |
393 | ||
394 | FpgaDownloadAndGo(FPGA_BITSTREAM_LF); | |
395 | ||
396 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz | |
397 | ||
398 | FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU ); | |
399 | ||
400 | LED_A_ON(); | |
401 | ||
402 | // steal this pin from the SSP and use it to control the modulation | |
403 | AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; | |
404 | AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT; | |
405 | ||
406 | //initialization of the timer | |
407 | AT91C_BASE_PMC->PMC_PCER |= (0x1 << 12) | (0x1 << 13) | (0x1 << 14); | |
408 | AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE; | |
409 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable | |
410 | AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK; //clock at 48/32 MHz | |
411 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN; | |
412 | AT91C_BASE_TCB->TCB_BCR = 1; | |
413 | ||
414 | ||
415 | tempo = AT91C_BASE_TC0->TC_CV; | |
416 | for(u=0;tab[u]!= 0;u+=3){ | |
417 | ||
418 | ||
419 | // modulate antenna | |
420 | HIGH(GPIO_SSC_DOUT); | |
421 | while(tempo != tab[u]){ | |
422 | tempo = AT91C_BASE_TC0->TC_CV; | |
423 | } | |
424 | ||
425 | // stop modulating antenna | |
426 | LOW(GPIO_SSC_DOUT); | |
427 | while(tempo != tab[u+1]){ | |
428 | tempo = AT91C_BASE_TC0->TC_CV; | |
429 | } | |
430 | ||
431 | ||
432 | // modulate antenna | |
433 | HIGH(GPIO_SSC_DOUT); | |
434 | while(tempo != tab[u+2]){ | |
435 | tempo = AT91C_BASE_TC0->TC_CV; | |
436 | } | |
437 | ||
438 | ||
439 | } | |
440 | ||
441 | LED_A_OFF(); | |
442 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); | |
443 | SpinDelay(200); | |
444 | ||
445 | ||
446 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable | |
447 | DbpString("FINISH !"); | |
448 | DbpString("(Could be usefull to send the same trame many times)"); | |
449 | LED(0xFFFF, 1000); | |
450 | } | |
451 | ||
452 | ||
453 | /* Add a byte for building the data frame of PCF7931 tags | |
454 | * @param b : byte to add | |
455 | * @param tab : array of the data frame | |
456 | * @param l : offset on low pulse width | |
457 | * @param p : offset on low pulse positioning | |
458 | */ | |
459 | ||
460 | bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p){ | |
461 | ||
462 | uint32_t u; | |
463 | for (u=0; u<8; u++) | |
464 | { | |
465 |