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