]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
CHG: removing some time-debug statements.
[proxmark3-svn] / client / cmdlf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Low frequency commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "proxmark3.h"
16 #include "data.h"
17 #include "graph.h"
18 #include "ui.h"
19 #include "cmdparser.h"
20 #include "cmdmain.h"
21 #include "cmddata.h"
22 #include "util.h"
23 #include "cmdlf.h"
24 #include "cmdlfhid.h"
25 #include "cmdlfawid.h"
26 #include "cmdlfti.h"
27 #include "cmdlfem4x.h"
28 #include "cmdlfhitag.h"
29 #include "cmdlft55xx.h"
30 #include "cmdlfpcf7931.h"
31 #include "cmdlfio.h"
32 #include "lfdemod.h"
33 #include "cmdlfviking.h"
34 #include "cmdlfpresco.h"
35 static int CmdHelp(const char *Cmd);
36
37 int usage_lf_cmdread(void) {
38 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H]");
39 PrintAndLog("Options: ");
40 PrintAndLog(" h This help");
41 PrintAndLog(" L Low frequency (125 KHz)");
42 PrintAndLog(" H High frequency (134 KHz)");
43 PrintAndLog(" d <delay> delay OFF period, (dec)");
44 PrintAndLog(" z <zero> time period ZERO, (dec)");
45 PrintAndLog(" o <one> time period ONE, (dec)");
46 PrintAndLog(" c <cmd> Command bytes");
47 PrintAndLog(" ************* All periods in microseconds (ms)");
48 PrintAndLog("Examples:");
49 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
50 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
51 return 0;
52 }
53 int usage_lf_read(void){
54 PrintAndLog("Usage: lf read [h] [s]");
55 PrintAndLog("Options: ");
56 PrintAndLog(" h This help");
57 PrintAndLog(" s silent run no printout");
58 PrintAndLog("This function takes no arguments. ");
59 PrintAndLog("Use 'lf config' to set parameters.");
60 return 0;
61 }
62 int usage_lf_snoop(void) {
63 PrintAndLog("Usage: lf snoop");
64 PrintAndLog("Options: ");
65 PrintAndLog(" h This help");
66 PrintAndLog("This function takes no arguments. ");
67 PrintAndLog("Use 'lf config' to set parameters.");
68 return 0;
69 }
70 int usage_lf_config(void) {
71 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
72 PrintAndLog("Options: ");
73 PrintAndLog(" h This help");
74 PrintAndLog(" L Low frequency (125 KHz)");
75 PrintAndLog(" H High frequency (134 KHz)");
76 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
77 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
78 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
79 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
80 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
81 PrintAndLog("Examples:");
82 PrintAndLog(" lf config b 8 L");
83 PrintAndLog(" Samples at 125KHz, 8bps.");
84 PrintAndLog(" lf config H b 4 d 3");
85 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
86 PrintAndLog(" a resolution of 4 bits per sample.");
87 PrintAndLog(" lf read");
88 PrintAndLog(" Performs a read (active field)");
89 PrintAndLog(" lf snoop");
90 PrintAndLog(" Performs a snoop (no active field)");
91 return 0;
92 }
93 int usage_lf_simfsk(void) {
94 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
95 PrintAndLog("Options: ");
96 PrintAndLog(" h This help");
97 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
98 PrintAndLog(" i invert data");
99 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
100 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
101 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
102 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
103 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
104 return 0;
105 }
106 int usage_lf_simask(void) {
107 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
108 PrintAndLog("Options: ");
109 PrintAndLog(" h This help");
110 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
111 PrintAndLog(" i invert data");
112 PrintAndLog(" b sim ask/biphase");
113 PrintAndLog(" m sim ask/manchester - Default");
114 PrintAndLog(" r sim ask/raw");
115 PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
116 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
117 return 0;
118 }
119 int usage_lf_simpsk(void) {
120 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
121 PrintAndLog("Options: ");
122 PrintAndLog(" h This help");
123 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
124 PrintAndLog(" i invert data");
125 PrintAndLog(" 1 set PSK1 (default)");
126 PrintAndLog(" 2 set PSK2");
127 PrintAndLog(" 3 set PSK3");
128 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
129 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
130 return 0;
131 }
132 int usage_lf_find(void){
133 PrintAndLog("Usage: lf search <0|1> [u]");
134 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
135 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
136 PrintAndLog("");
137 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
138 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
139 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
140 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
141 return 0;
142 }
143
144
145 /* send a LF command before reading */
146 int CmdLFCommandRead(const char *Cmd)
147 {
148 static char dummy[3] = {0x20,0x00,0x00};
149 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
150 bool errors = FALSE;
151
152 uint8_t cmdp = 0;
153 int strLength = 0;
154
155 while(param_getchar(Cmd, cmdp) != 0x00) {
156 switch(param_getchar(Cmd, cmdp))
157 {
158 case 'h':
159 return usage_lf_cmdread();
160 case 'H':
161 dummy[1]='h';
162 cmdp++;
163 break;
164 case 'L':
165 cmdp++;
166 break;
167 case 'c':
168 strLength = param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes);
169 cmdp+=2;
170 break;
171 case 'd':
172 c.arg[0] = param_get32ex(Cmd, cmdp+1, 0, 10);
173 cmdp+=2;
174 break;
175 case 'z':
176 c.arg[1] = param_get32ex(Cmd, cmdp+1, 0, 10);
177 cmdp+=2;
178 break;
179 case 'o':
180 c.arg[2] = param_get32ex(Cmd, cmdp+1, 0, 10);
181 cmdp+=2;
182 break;
183 default:
184 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
185 errors = 1;
186 break;
187 }
188 if(errors) break;
189 }
190 // No args
191 if (cmdp == 0) errors = 1;
192
193 //Validations
194 if (errors) return usage_lf_cmdread();
195
196 // in case they specified 'H'
197 strcpy((char *)&c.d.asBytes + strLength, dummy);
198
199 clearCommandBuffer();
200 SendCommand(&c);
201 return 0;
202 }
203
204 int CmdFlexdemod(const char *Cmd)
205 {
206 int i;
207 for (i = 0; i < GraphTraceLen; ++i) {
208 if (GraphBuffer[i] < 0) {
209 GraphBuffer[i] = -1;
210 } else {
211 GraphBuffer[i] = 1;
212 }
213 }
214
215 #define LONG_WAIT 100
216 int start;
217 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
218 int first = GraphBuffer[start];
219 for (i = start; i < start + LONG_WAIT; i++) {
220 if (GraphBuffer[i] != first) {
221 break;
222 }
223 }
224 if (i == (start + LONG_WAIT)) {
225 break;
226 }
227 }
228 if (start == GraphTraceLen - LONG_WAIT) {
229 PrintAndLog("nothing to wait for");
230 return 0;
231 }
232
233 GraphBuffer[start] = 2;
234 GraphBuffer[start+1] = -2;
235 uint8_t bits[64] = {0x00};
236
237 int bit, sum;
238 i = start;
239 for (bit = 0; bit < 64; bit++) {
240 sum = 0;
241 for (int j = 0; j < 16; j++) {
242 sum += GraphBuffer[i++];
243 }
244
245 bits[bit] = (sum > 0) ? 1 : 0;
246
247 PrintAndLog("bit %d sum %d", bit, sum);
248 }
249
250 for (bit = 0; bit < 64; bit++) {
251 int j;
252 int sum = 0;
253 for (j = 0; j < 16; j++) {
254 sum += GraphBuffer[i++];
255 }
256 if (sum > 0 && bits[bit] != 1) {
257 PrintAndLog("oops1 at %d", bit);
258 }
259 if (sum < 0 && bits[bit] != 0) {
260 PrintAndLog("oops2 at %d", bit);
261 }
262 }
263
264 // HACK writing back to graphbuffer.
265 GraphTraceLen = 32*64;
266 i = 0;
267 int phase = 0;
268 for (bit = 0; bit < 64; bit++) {
269
270 phase = (bits[bit] == 0) ? 0 : 1;
271
272 int j;
273 for (j = 0; j < 32; j++) {
274 GraphBuffer[i++] = phase;
275 phase = !phase;
276 }
277 }
278
279 RepaintGraphWindow();
280 return 0;
281 }
282
283 int CmdIndalaDemod(const char *Cmd)
284 {
285 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
286
287 int state = -1;
288 int count = 0;
289 int i, j;
290
291 // worst case with GraphTraceLen=64000 is < 4096
292 // under normal conditions it's < 2048
293
294 uint8_t rawbits[4096];
295 int rawbit = 0;
296 int worst = 0, worstPos = 0;
297 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
298
299 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
300 for (i = 0; i < GraphTraceLen-1; i += 2) {
301 count += 1;
302 if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
303 // appears redundant - marshmellow
304 if (state == 0) {
305 for (j = 0; j < count - 8; j += 16) {
306 rawbits[rawbit++] = 0;
307 }
308 if ((abs(count - j)) > worst) {
309 worst = abs(count - j);
310 worstPos = i;
311 }
312 }
313 state = 1;
314 count = 0;
315 } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
316 //appears redundant
317 if (state == 1) {
318 for (j = 0; j < count - 8; j += 16) {
319 rawbits[rawbit++] = 1;
320 }
321 if ((abs(count - j)) > worst) {
322 worst = abs(count - j);
323 worstPos = i;
324 }
325 }
326 state = 0;
327 count = 0;
328 }
329 }
330
331 if ( rawbit>0 ){
332 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32);
333 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
334 } else {
335 return 0;
336 }
337
338 // Finding the start of a UID
339 int uidlen, long_wait;
340 if (strcmp(Cmd, "224") == 0) {
341 uidlen = 224;
342 long_wait = 30;
343 } else {
344 uidlen = 64;
345 long_wait = 29;
346 }
347
348 int start;
349 int first = 0;
350 for (start = 0; start <= rawbit - uidlen; start++) {
351 first = rawbits[start];
352 for (i = start; i < start + long_wait; i++) {
353 if (rawbits[i] != first) {
354 break;
355 }
356 }
357 if (i == (start + long_wait)) {
358 break;
359 }
360 }
361
362 if (start == rawbit - uidlen + 1) {
363 PrintAndLog("nothing to wait for");
364 return 0;
365 }
366
367 // Inverting signal if needed
368 if (first == 1) {
369 for (i = start; i < rawbit; i++) {
370 rawbits[i] = !rawbits[i];
371 }
372 }
373
374 // Dumping UID
375 uint8_t bits[224] = {0x00};
376 char showbits[225] = {0x00};
377 int bit;
378 i = start;
379 int times = 0;
380
381 if (uidlen > rawbit) {
382 PrintAndLog("Warning: not enough raw bits to get a full UID");
383 for (bit = 0; bit < rawbit; bit++) {
384 bits[bit] = rawbits[i++];
385 // As we cannot know the parity, let's use "." and "/"
386 showbits[bit] = '.' + bits[bit];
387 }
388 showbits[bit+1]='\0';
389 PrintAndLog("Partial UID=%s", showbits);
390 return 0;
391 } else {
392 for (bit = 0; bit < uidlen; bit++) {
393 bits[bit] = rawbits[i++];
394 showbits[bit] = '0' + bits[bit];
395 }
396 times = 1;
397 }
398
399 //convert UID to HEX
400 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
401 int idx;
402 uid1 = uid2 = 0;
403
404 if (uidlen==64){
405 for( idx=0; idx<64; idx++) {
406 if (showbits[idx] == '0') {
407 uid1 = (uid1<<1) | (uid2>>31);
408 uid2 = (uid2<<1) | 0;
409 } else {
410 uid1 = (uid1<<1) | (uid2>>31);
411 uid2 = (uid2<<1) | 1;
412 }
413 }
414 PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
415 } else {
416 uid3 = uid4 = uid5 = uid6 = uid7 = 0;
417
418 for( idx=0; idx<224; idx++) {
419 uid1 = (uid1<<1) | (uid2>>31);
420 uid2 = (uid2<<1) | (uid3>>31);
421 uid3 = (uid3<<1) | (uid4>>31);
422 uid4 = (uid4<<1) | (uid5>>31);
423 uid5 = (uid5<<1) | (uid6>>31);
424 uid6 = (uid6<<1) | (uid7>>31);
425
426 if (showbits[idx] == '0')
427 uid7 = (uid7<<1) | 0;
428 else
429 uid7 = (uid7<<1) | 1;
430 }
431 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
432 }
433
434 // Checking UID against next occurrences
435 int failed = 0;
436 for (; i + uidlen <= rawbit;) {
437 failed = 0;
438 for (bit = 0; bit < uidlen; bit++) {
439 if (bits[bit] != rawbits[i++]) {
440 failed = 1;
441 break;
442 }
443 }
444 if (failed == 1) {
445 break;
446 }
447 times += 1;
448 }
449
450 PrintAndLog("Occurrences: %d (expected %d)", times, (rawbit - start) / uidlen);
451
452 // Remodulating for tag cloning
453 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
454 // since this changes graphbuffer data.
455 GraphTraceLen = 32 * uidlen;
456 i = 0;
457 int phase = 0;
458 for (bit = 0; bit < uidlen; bit++) {
459 phase = (bits[bit] == 0) ? 0 : 1;
460 int j;
461 for (j = 0; j < 32; j++) {
462 GraphBuffer[i++] = phase;
463 phase = !phase;
464 }
465 }
466
467 RepaintGraphWindow();
468 return 1;
469 }
470
471 int CmdIndalaClone(const char *Cmd)
472 {
473 UsbCommand c;
474 unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
475
476 uid1 = uid2 = uid3 = uid4 = uid5 = uid6 = uid7 = 0;
477 int n = 0, i = 0;
478
479 if (strchr(Cmd,'l') != 0) {
480 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
481 uid1 = (uid1 << 4) | (uid2 >> 28);
482 uid2 = (uid2 << 4) | (uid3 >> 28);
483 uid3 = (uid3 << 4) | (uid4 >> 28);
484 uid4 = (uid4 << 4) | (uid5 >> 28);
485 uid5 = (uid5 << 4) | (uid6 >> 28);
486 uid6 = (uid6 << 4) | (uid7 >> 28);
487 uid7 = (uid7 << 4) | (n & 0xf);
488 }
489 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7);
490 c.cmd = CMD_INDALA_CLONE_TAG_L;
491 c.d.asDwords[0] = uid1;
492 c.d.asDwords[1] = uid2;
493 c.d.asDwords[2] = uid3;
494 c.d.asDwords[3] = uid4;
495 c.d.asDwords[4] = uid5;
496 c.d.asDwords[5] = uid6;
497 c.d.asDwords[6] = uid7;
498 } else {
499 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
500 uid1 = (uid1 << 4) | (uid2 >> 28);
501 uid2 = (uid2 << 4) | (n & 0xf);
502 }
503 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2);
504 c.cmd = CMD_INDALA_CLONE_TAG;
505 c.arg[0] = uid1;
506 c.arg[1] = uid2;
507 }
508
509 clearCommandBuffer();
510 SendCommand(&c);
511 return 0;
512 }
513
514 int CmdLFSetConfig(const char *Cmd)
515 {
516 uint8_t divisor = 0;//Frequency divisor
517 uint8_t bps = 0; // Bits per sample
518 uint8_t decimation = 0; //How many to keep
519 bool averaging = 1; // Defaults to true
520 bool errors = FALSE;
521 int trigger_threshold = -1;//Means no change
522 uint8_t unsigned_trigg = 0;
523
524 uint8_t cmdp = 0;
525 while(param_getchar(Cmd, cmdp) != 0x00)
526 {
527 switch(param_getchar(Cmd, cmdp))
528 {
529 case 'h':
530 return usage_lf_config();
531 case 'H':
532 divisor = 88;
533 cmdp++;
534 break;
535 case 'L':
536 divisor = 95;
537 cmdp++;
538 break;
539 case 'q':
540 errors |= param_getdec(Cmd,cmdp+1,&divisor);
541 cmdp+=2;
542 break;
543 case 't':
544 errors |= param_getdec(Cmd,cmdp+1,&unsigned_trigg);
545 cmdp+=2;
546 if(!errors) trigger_threshold = unsigned_trigg;
547 break;
548 case 'b':
549 errors |= param_getdec(Cmd,cmdp+1,&bps);
550 cmdp+=2;
551 break;
552 case 'd':
553 errors |= param_getdec(Cmd,cmdp+1,&decimation);
554 cmdp+=2;
555 break;
556 case 'a':
557 averaging = param_getchar(Cmd,cmdp+1) == '1';
558 cmdp+=2;
559 break;
560 default:
561 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
562 errors = 1;
563 break;
564 }
565 if(errors) break;
566 }
567
568 // No args
569 if (cmdp == 0) errors = 1;
570
571 //Validations
572 if (errors) return usage_lf_config();
573
574 //Bps is limited to 8, so fits in lower half of arg1
575 if (bps >> 4) bps = 8;
576
577 sample_config config = { decimation, bps, averaging, divisor, trigger_threshold };
578
579 //Averaging is a flag on high-bit of arg[1]
580 UsbCommand c = {CMD_SET_LF_SAMPLING_CONFIG};
581 memcpy(c.d.asBytes,&config,sizeof(sample_config));
582 clearCommandBuffer();
583 SendCommand(&c);
584 return 0;
585 }
586
587 int CmdLFRead(const char *Cmd)
588 {
589 bool arg1 = false;
590 uint8_t cmdp = param_getchar(Cmd, 0);
591
592 if ( cmdp == 'h' || cmdp == 'H') return usage_lf_read();
593
594 //suppress print
595 if ( cmdp == 's' || cmdp == 'S') arg1 = true;
596
597 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {arg1,0,0}};
598 clearCommandBuffer();
599 SendCommand(&c);
600 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
601 PrintAndLog("command execution time out");
602 return 1;
603 }
604 return 0;
605 }
606
607 int CmdLFSnoop(const char *Cmd)
608 {
609 uint8_t cmdp = param_getchar(Cmd, 0);
610 if(cmdp == 'h' || cmdp == 'H') return usage_lf_snoop();
611
612 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
613 clearCommandBuffer();
614 SendCommand(&c);
615 WaitForResponse(CMD_ACK,NULL);
616 return 0;
617 }
618
619 static void ChkBitstream(const char *str)
620 {
621 int i;
622
623 /* convert to bitstream if necessary */
624 for (i = 0; i < (int)(GraphTraceLen / 2); i++){
625 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) {
626 CmdGetBitStream("");
627 break;
628 }
629 }
630 }
631 //Attempt to simulate any wave in buffer (one bit per output sample)
632 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
633 int CmdLFSim(const char *Cmd)
634 {
635 int i,j;
636 static int gap;
637
638 sscanf(Cmd, "%i", &gap);
639
640 // convert to bitstream if necessary
641 ChkBitstream(Cmd);
642
643 //can send only 512 bits at a time (1 byte sent per bit...)
644 printf("Sending [%d bytes]", GraphTraceLen);
645 for (i = 0; i < GraphTraceLen; i += USB_CMD_DATA_SIZE) {
646 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
647
648 for (j = 0; j < USB_CMD_DATA_SIZE; j++) {
649 c.d.asBytes[j] = GraphBuffer[i+j];
650 }
651 clearCommandBuffer();
652 SendCommand(&c);
653 WaitForResponse(CMD_ACK,NULL);
654 printf(".");
655 }
656
657 PrintAndLog("\nStarting to simulate");
658 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
659 clearCommandBuffer();
660 SendCommand(&c);
661 return 0;
662 }
663
664 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
665 // - allow pull data from DemodBuffer
666 int CmdLFfskSim(const char *Cmd)
667 {
668 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
669 // otherwise will need FChigh, FClow, Clock, and bitstream
670 uint8_t fcHigh=0, fcLow=0, clk=0;
671 uint8_t invert=0;
672 bool errors = FALSE;
673 char hexData[32] = {0x00}; // store entered hex data
674 uint8_t data[255] = {0x00};
675 int dataLen = 0;
676 uint8_t cmdp = 0;
677 while(param_getchar(Cmd, cmdp) != 0x00)
678 {
679 switch(param_getchar(Cmd, cmdp))
680 {
681 case 'h':
682 return usage_lf_simfsk();
683 case 'i':
684 invert = 1;
685 cmdp++;
686 break;
687 case 'c':
688 errors |= param_getdec(Cmd,cmdp+1,&clk);
689 cmdp+=2;
690 break;
691 case 'H':
692 errors |= param_getdec(Cmd,cmdp+1,&fcHigh);
693 cmdp+=2;
694 break;
695 case 'L':
696 errors |= param_getdec(Cmd,cmdp+1,&fcLow);
697 cmdp+=2;
698 break;
699 //case 's':
700 // separator=1;
701 // cmdp++;
702 // break;
703 case 'd':
704 dataLen = param_getstr(Cmd, cmdp+1, hexData);
705 if (dataLen==0) {
706 errors=TRUE;
707 } else {
708 dataLen = hextobinarray((char *)data, hexData);
709 }
710 if (dataLen==0) errors=TRUE;
711 if (errors) PrintAndLog ("Error getting hex data");
712 cmdp+=2;
713 break;
714 default:
715 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
716 errors = TRUE;
717 break;
718 }
719 if(errors) break;
720 }
721 if(cmdp == 0 && DemodBufferLen == 0)
722 {
723 errors = TRUE;// No args
724 }
725
726 //Validations
727 if(errors)
728 {
729 return usage_lf_simfsk();
730 }
731
732 if (dataLen == 0){ //using DemodBuffer
733 if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all
734 uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0);
735 if (ans==0){
736 if (!fcHigh) fcHigh=10;
737 if (!fcLow) fcLow=8;
738 if (!clk) clk=50;
739 }
740 }
741 } else {
742 setDemodBuf(data, dataLen, 0);
743 }
744
745 //default if not found
746 if (clk == 0) clk = 50;
747 if (fcHigh == 0) fcHigh = 10;
748 if (fcLow == 0) fcLow = 8;
749
750 uint16_t arg1, arg2;
751 arg1 = fcHigh << 8 | fcLow;
752 arg2 = invert << 8 | clk;
753 size_t size = DemodBufferLen;
754 if (size > USB_CMD_DATA_SIZE) {
755 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
756 size = USB_CMD_DATA_SIZE;
757 }
758 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
759
760 memcpy(c.d.asBytes, DemodBuffer, size);
761 clearCommandBuffer();
762 SendCommand(&c);
763 return 0;
764 }
765
766 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
767 // - allow pull data from DemodBuffer
768 int CmdLFaskSim(const char *Cmd)
769 {
770 //autodetect clock from Graphbuffer if using demod buffer
771 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
772 uint8_t encoding = 1, separator = 0;
773 uint8_t clk=0, invert=0;
774 bool errors = FALSE;
775 char hexData[32] = {0x00};
776 uint8_t data[255]= {0x00}; // store entered hex data
777 int dataLen = 0;
778 uint8_t cmdp = 0;
779 while(param_getchar(Cmd, cmdp) != 0x00)
780 {
781 switch(param_getchar(Cmd, cmdp))
782 {
783 case 'h':
784 return usage_lf_simask();
785 case 'i':
786 invert = 1;
787 cmdp++;
788 break;
789 case 'c':
790 errors |= param_getdec(Cmd,cmdp+1,&clk);
791 cmdp+=2;
792 break;
793 case 'b':
794 encoding=2; //biphase
795 cmdp++;
796 break;
797 case 'm':
798 encoding=1;
799 cmdp++;
800 break;
801 case 'r':
802 encoding=0;
803 cmdp++;
804 break;
805 case 's':
806 separator=1;
807 cmdp++;
808 break;
809 case 'd':
810 dataLen = param_getstr(Cmd, cmdp+1, hexData);
811 if (dataLen==0) {
812 errors=TRUE;
813 } else {
814 dataLen = hextobinarray((char *)data, hexData);
815 }
816 if (dataLen==0) errors=TRUE;
817 if (errors) PrintAndLog ("Error getting hex data, datalen: %d",dataLen);
818 cmdp+=2;
819 break;
820 default:
821 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
822 errors = TRUE;
823 break;
824 }
825 if(errors) break;
826 }
827 if(cmdp == 0 && DemodBufferLen == 0)
828 {
829 errors = TRUE;// No args
830 }
831
832 //Validations
833 if(errors)
834 {
835 return usage_lf_simask();
836 }
837 if (dataLen == 0){ //using DemodBuffer
838 if (clk == 0) clk = GetAskClock("0", false, false);
839 } else {
840 setDemodBuf(data, dataLen, 0);
841 }
842 if (clk == 0) clk = 64;
843 if (encoding == 0) clk = clk/2; //askraw needs to double the clock speed
844 uint16_t arg1, arg2;
845 size_t size=DemodBufferLen;
846 arg1 = clk << 8 | encoding;
847 arg2 = invert << 8 | separator;
848 if (size > USB_CMD_DATA_SIZE) {
849 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
850 size = USB_CMD_DATA_SIZE;
851 }
852 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
853 PrintAndLog("preparing to sim ask data: %d bits", size);
854 memcpy(c.d.asBytes, DemodBuffer, size);
855 clearCommandBuffer();
856 SendCommand(&c);
857 return 0;
858 }
859
860 // by marshmellow - sim psk data given carrier, clock, invert
861 // - allow pull data from DemodBuffer or parameters
862 int CmdLFpskSim(const char *Cmd)
863 {
864 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
865 //will need carrier, Clock, and bitstream
866 uint8_t carrier=0, clk=0;
867 uint8_t invert=0;
868 bool errors = FALSE;
869 char hexData[32] = {0x00}; // store entered hex data
870 uint8_t data[255] = {0x00};
871 int dataLen = 0;
872 uint8_t cmdp = 0;
873 uint8_t pskType = 1;
874 while(param_getchar(Cmd, cmdp) != 0x00)
875 {
876 switch(param_getchar(Cmd, cmdp))
877 {
878 case 'h':
879 return usage_lf_simpsk();
880 case 'i':
881 invert = 1;
882 cmdp++;
883 break;
884 case 'c':
885 errors |= param_getdec(Cmd,cmdp+1,&clk);
886 cmdp+=2;
887 break;
888 case 'r':
889 errors |= param_getdec(Cmd,cmdp+1,&carrier);
890 cmdp+=2;
891 break;
892 case '1':
893 pskType=1;
894 cmdp++;
895 break;
896 case '2':
897 pskType=2;
898 cmdp++;
899 break;
900 case '3':
901 pskType=3;
902 cmdp++;
903 break;
904 case 'd':
905 dataLen = param_getstr(Cmd, cmdp+1, hexData);
906 if (dataLen==0) {
907 errors=TRUE;
908 } else {
909 dataLen = hextobinarray((char *)data, hexData);
910 }
911 if (dataLen==0) errors=TRUE;
912 if (errors) PrintAndLog ("Error getting hex data");
913 cmdp+=2;
914 break;
915 default:
916 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
917 errors = TRUE;
918 break;
919 }
920 if (errors) break;
921 }
922 if (cmdp == 0 && DemodBufferLen == 0)
923 {
924 errors = TRUE;// No args
925 }
926
927 //Validations
928 if (errors)
929 {
930 return usage_lf_simpsk();
931 }
932 if (dataLen == 0){ //using DemodBuffer
933 PrintAndLog("Getting Clocks");
934 if (clk==0) clk = GetPskClock("", FALSE, FALSE);
935 PrintAndLog("clk: %d",clk);
936 if (!carrier) carrier = GetPskCarrier("", FALSE, FALSE);
937 PrintAndLog("carrier: %d", carrier);
938 } else {
939 setDemodBuf(data, dataLen, 0);
940 }
941
942 if (clk <= 0) clk = 32;
943 if (carrier == 0) carrier = 2;
944 if (pskType != 1){
945 if (pskType == 2){
946 //need to convert psk2 to psk1 data before sim
947 psk2TOpsk1(DemodBuffer, DemodBufferLen);
948 } else {
949 PrintAndLog("Sorry, PSK3 not yet available");
950 }
951 }
952 uint16_t arg1, arg2;
953 arg1 = clk << 8 | carrier;
954 arg2 = invert;
955 size_t size=DemodBufferLen;
956 if (size > USB_CMD_DATA_SIZE) {
957 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
958 size=USB_CMD_DATA_SIZE;
959 }
960 UsbCommand c = {CMD_PSK_SIM_TAG, {arg1, arg2, size}};
961 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size);
962 memcpy(c.d.asBytes, DemodBuffer, size);
963 clearCommandBuffer();
964 SendCommand(&c);
965
966 return 0;
967 }
968
969 int CmdLFSimBidir(const char *Cmd)
970 {
971 // Set ADC to twice the carrier for a slight supersampling
972 // HACK: not implemented in ARMSRC.
973 PrintAndLog("Not implemented yet.");
974 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
975 SendCommand(&c);
976 return 0;
977 }
978
979 int CmdVchDemod(const char *Cmd)
980 {
981 // Is this the entire sync pattern, or does this also include some
982 // data bits that happen to be the same everywhere? That would be
983 // lovely to know.
984 static const int SyncPattern[] = {
985 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
986 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
987 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
988 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
989 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
990 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
991 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
992 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
993 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
994 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
995 };
996
997 // So first, we correlate for the sync pattern, and mark that.
998 int bestCorrel = 0, bestPos = 0;
999 int i;
1000 // It does us no good to find the sync pattern, with fewer than
1001 // 2048 samples after it...
1002 for (i = 0; i < (GraphTraceLen-2048); i++) {
1003 int sum = 0;
1004 int j;
1005 for (j = 0; j < arraylen(SyncPattern); j++) {
1006 sum += GraphBuffer[i+j]*SyncPattern[j];
1007 }
1008 if (sum > bestCorrel) {
1009 bestCorrel = sum;
1010 bestPos = i;
1011 }
1012 }
1013 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
1014
1015 char bits[257];
1016 bits[256] = '\0';
1017
1018 int worst = INT_MAX;
1019 int worstPos = 0;
1020
1021 for (i = 0; i < 2048; i += 8) {
1022 int sum = 0;
1023 int j;
1024 for (j = 0; j < 8; j++) {
1025 sum += GraphBuffer[bestPos+i+j];
1026 }
1027 if (sum < 0) {
1028 bits[i/8] = '.';
1029 } else {
1030 bits[i/8] = '1';
1031 }
1032 if(abs(sum) < worst) {
1033 worst = abs(sum);
1034 worstPos = i;
1035 }
1036 }
1037 PrintAndLog("bits:");
1038 PrintAndLog("%s", bits);
1039 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
1040
1041 if (strcmp(Cmd, "clone")==0) {
1042 GraphTraceLen = 0;
1043 char *s;
1044 for(s = bits; *s; s++) {
1045 int j;
1046 for(j = 0; j < 16; j++) {
1047 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
1048 }
1049 }
1050 RepaintGraphWindow();
1051 }
1052 return 0;
1053 }
1054
1055 //by marshmellow
1056 int CmdLFfind(const char *Cmd) {
1057 int ans = 0;
1058 char cmdp = param_getchar(Cmd, 0);
1059 char testRaw = param_getchar(Cmd, 1);
1060 if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') return usage_lf_find();
1061
1062 if (!offline && (cmdp != '1')){
1063 CmdLFRead("s");
1064 getSamples("30000",false);
1065 } else if (GraphTraceLen < 1000) {
1066 PrintAndLog("Data in Graphbuffer was too small.");
1067 return 0;
1068 }
1069 if (cmdp == 'u' || cmdp == 'U') testRaw = 'u';
1070
1071 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1072 PrintAndLog("False Positives ARE possible\n");
1073 PrintAndLog("\nChecking for known tags:\n");
1074
1075 ans=CmdFSKdemodIO("");
1076 if (ans>0) {
1077 PrintAndLog("\nValid IO Prox ID Found!");
1078 return 1;
1079 }
1080
1081 ans=CmdFSKdemodPyramid("");
1082 if (ans>0) {
1083 PrintAndLog("\nValid Pyramid ID Found!");
1084 return 1;
1085 }
1086
1087 ans=CmdFSKdemodParadox("");
1088 if (ans>0) {
1089 PrintAndLog("\nValid Paradox ID Found!");
1090 return 1;
1091 }
1092
1093 ans=CmdFSKdemodAWID("");
1094 if (ans>0) {
1095 PrintAndLog("\nValid AWID ID Found!");
1096 return 1;
1097 }
1098
1099 ans=CmdFSKdemodHID("");
1100 if (ans>0) {
1101 PrintAndLog("\nValid HID Prox ID Found!");
1102 return 1;
1103 }
1104
1105 ans=CmdAskEM410xDemod("");
1106 if (ans>0) {
1107 PrintAndLog("\nValid EM410x ID Found!");
1108 return 1;
1109 }
1110
1111 ans=CmdG_Prox_II_Demod("");
1112 if (ans>0) {
1113 PrintAndLog("\nValid Guardall G-Prox II ID Found!");
1114 return 1;
1115 }
1116
1117 ans=CmdFDXBdemodBI("");
1118 if (ans>0) {
1119 PrintAndLog("\nValid FDX-B ID Found!");
1120 return 1;
1121 }
1122
1123 ans=EM4x50Read("", false);
1124 if (ans>0) {
1125 PrintAndLog("\nValid EM4x50 ID Found!");
1126 return 1;
1127 }
1128
1129 ans=CmdVikingDemod("");
1130 if (ans>0) {
1131 PrintAndLog("\nValid Viking ID Found!");
1132 return 1;
1133 }
1134
1135 ans=CmdIndalaDecode("");
1136 if (ans>0) {
1137 PrintAndLog("\nValid Indala ID Found!");
1138 return 1;
1139 }
1140
1141 ans=CmdPSKNexWatch("");
1142 if (ans>0) {
1143 PrintAndLog("\nValid NexWatch ID Found!");
1144 return 1;
1145 }
1146 // TIdemod?
1147
1148
1149 PrintAndLog("\nNo Known Tags Found!\n");
1150 if (testRaw=='u' || testRaw=='U'){
1151 //test unknown tag formats (raw mode)
1152 PrintAndLog("\nChecking for Unknown tags:\n");
1153 ans=AutoCorrelate(4000, FALSE, FALSE);
1154
1155 if (ans > 0) {
1156
1157 PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
1158
1159 if ( ans % 8 == 0) {
1160 int bytes = (ans / 8);
1161 PrintAndLog("Possible %d bytes", bytes);
1162 int blocks = 0;
1163 if ( bytes % 2 == 0) {
1164 blocks = (bytes / 2);
1165 PrintAndLog("Possible 2 blocks, width %d", blocks);
1166 }
1167 if ( bytes % 4 == 0) {
1168 blocks = (bytes / 4);
1169 PrintAndLog("Possible 4 blocks, width %d", blocks);
1170 }
1171 if ( bytes % 8 == 0) {
1172 blocks = (bytes / 8);
1173 PrintAndLog("Possible 8 blocks, width %d", blocks);
1174 }
1175 if ( bytes % 16 == 0) {
1176 blocks = (bytes / 16);
1177 PrintAndLog("Possible 16 blocks, width %d", blocks);
1178 }
1179 }
1180 }
1181
1182 ans=GetFskClock("",FALSE,FALSE);
1183 if (ans != 0){ //fsk
1184 ans=FSKrawDemod("",TRUE);
1185 if (ans>0) {
1186 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1187 return 1;
1188 }
1189 }
1190 bool st = TRUE;
1191 ans=ASKDemod_ext("0 0 0",TRUE,FALSE,1,&st);
1192 if (ans>0) {
1193 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1194 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1195 return 1;
1196 }
1197
1198 ans=CmdPSK1rawDemod("");
1199 if (ans>0) {
1200 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1201 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1202 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1203 return 1;
1204 }
1205 PrintAndLog("\nNo Data Found!\n");
1206 }
1207 return 0;
1208 }
1209
1210 static command_t CommandTable[] =
1211 {
1212 {"help", CmdHelp, 1, "This help"},
1213 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
1214 {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
1215 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
1216 {"hitag", CmdLFHitag, 1, "{ HITAG RFIDs... }"},
1217 {"io", CmdLFIO, 1, "{ IOPROX RFIDs... }"},
1218 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 RFIDs... }"},
1219 {"presco", CmdLFPresco, 1, "{ Presco RFIDs... }"},
1220 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
1221 {"t55xx", CmdLFT55XX, 1, "{ T55xx RFIDs... }"},
1222 {"viking", CmdLFViking, 1, "{ Viking RFIDs... }"},
1223 {"config", CmdLFSetConfig, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1224 {"cmdread", CmdLFCommandRead, 0, "<off period> <'0' period> <'1' period> <command> ['h' 134] \n\t\t-- Modulate LF reader field to send command before read (all periods in microseconds)"},
1225 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
1226 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1227 {"indalaclone", CmdIndalaClone, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1228 {"read", CmdLFRead, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1229 {"search", CmdLFfind, 1, "[offline] ['u'] Read and Search for valid known tag (in offline mode it you can load first then search) \n\t\t-- 'u' to search for unknown tags"},
1230 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1231 {"simask", CmdLFaskSim, 0, "[clock] [invert <1|0>] [biphase/manchester/raw <'b'|'m'|'r'>] [msg separator 's'] [d <hexdata>] \n\t\t-- Simulate LF ASK tag from demodbuffer or input"},
1232 {"simfsk", CmdLFfskSim, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] \n\t\t-- Simulate LF FSK tag from demodbuffer or input"},
1233 {"simpsk", CmdLFpskSim, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] \n\t\t-- Simulate LF PSK tag from demodbuffer or input"},
1234 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1235 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1236 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
1237 {NULL, NULL, 0, NULL}
1238 };
1239
1240 int CmdLF(const char *Cmd) {
1241 clearCommandBuffer();
1242 CmdsParse(CommandTable, Cmd);
1243 return 0;
1244 }
1245
1246 int CmdHelp(const char *Cmd) {
1247 CmdsHelp(CommandTable);
1248 return 0;
1249 }
Impressum, Datenschutz