1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Low frequency commands
9 //-----------------------------------------------------------------------------
20 #include "lfdemod.h" // for psk2TOpsk1
21 #include "util.h" // for parsing cli command utils
22 #include "ui.h" // for show graph controls
23 #include "graph.h" // for graph data
24 #include "cmdparser.h" // for getting cli commands included in cmdmain.h
25 #include "cmdmain.h" // for sending cmds to device
26 #include "cmddata.h" // for `lf search`
27 #include "cmdlfawid.h" // for awid menu
28 #include "cmdlfem4x.h" // for em4x menu
29 #include "cmdlfhid.h" // for hid menu
30 #include "cmdlfhitag.h" // for hitag menu
31 #include "cmdlfio.h" // for ioprox menu
32 #include "cmdlft55xx.h" // for t55xx menu
33 #include "cmdlfti.h" // for ti menu
34 #include "cmdlfpresco.h" // for presco menu
35 #include "cmdlfpcf7931.h"// for pcf7931 menu
36 #include "cmdlfpyramid.h"// for pyramid menu
37 #include "cmdlfviking.h" // for viking menu
38 #include "cmdlfcotag.h" // for COTAG menu
39 #include "cmdlfvisa2000.h" // for VISA2000 menu
40 #include "cmdlfindala.h" // for indala menu
41 #include "cmdlfgproxii.h"// for gproxii menu
42 #include "cmdlffdx.h" // for fdx-b menu
43 #include "cmdlfparadox.h"// for paradox menu
44 #include "cmdlfnexwatch.h"//for nexwatch menu
45 #include "cmdlfjablotron.h" //for jablotron menu
46 #include "cmdlfnoralsy.h"// for noralsy menu
47 #include "cmdlfsecurakey.h"//for securakey menu
48 #include "cmdlfpac.h" // for pac menu
50 bool g_lf_threshold_set
= false;
51 static int CmdHelp(const char *Cmd
);
55 int usage_lf_cmdread(void)
57 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> ");
58 PrintAndLog("Options: ");
59 PrintAndLog(" h This help");
60 PrintAndLog(" d <delay> delay OFF period between bits (0 for bitbang mode)");
61 PrintAndLog(" z <zero> time period ZERO (antenna off in bitbang mode)");
62 PrintAndLog(" o <one> time period ONE (antenna on in bitbang mode)");
63 PrintAndLog(" c <cmd> Command bytes");
64 PrintAndLog(" ************* All periods in microseconds");
65 PrintAndLog(" ************* Use lf config to configure options.");
66 PrintAndLog("Examples:");
67 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
68 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000");
72 /* send a command before reading */
73 int CmdLFCommandRead(const char *Cmd
)
75 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
77 //uint8_t divisor = 95; //125khz
79 while(param_getchar(Cmd
, cmdp
) != 0x00)
81 switch(param_getchar(Cmd
, cmdp
))
84 return usage_lf_cmdread();
86 param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
, sizeof(c
.d
.asBytes
));
90 c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
94 c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
98 c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
102 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
109 if(cmdp
== 0) errors
= 1;
112 if(errors
) return usage_lf_cmdread();
114 clearCommandBuffer();
117 WaitForResponse(CMD_ACK
,NULL
);
124 int CmdFlexdemod(const char *Cmd
)
127 for (i
= 0; i
< GraphTraceLen
; ++i
) {
128 if (GraphBuffer
[i
] < 0) {
135 #define LONG_WAIT 100
137 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
138 int first
= GraphBuffer
[start
];
139 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
140 if (GraphBuffer
[i
] != first
) {
144 if (i
== (start
+ LONG_WAIT
)) {
148 if (start
== GraphTraceLen
- LONG_WAIT
) {
149 PrintAndLog("nothing to wait for");
153 GraphBuffer
[start
] = 2;
154 GraphBuffer
[start
+1] = -2;
155 uint8_t bits
[64] = {0x00};
159 for (bit
= 0; bit
< 64; bit
++) {
161 for (int j
= 0; j
< 16; j
++) {
162 sum
+= GraphBuffer
[i
++];
165 bits
[bit
] = (sum
> 0) ? 1 : 0;
167 PrintAndLog("bit %d sum %d", bit
, sum
);
170 for (bit
= 0; bit
< 64; bit
++) {
173 for (j
= 0; j
< 16; j
++) {
174 sum
+= GraphBuffer
[i
++];
176 if (sum
> 0 && bits
[bit
] != 1) {
177 PrintAndLog("oops1 at %d", bit
);
179 if (sum
< 0 && bits
[bit
] != 0) {
180 PrintAndLog("oops2 at %d", bit
);
184 // HACK writing back to graphbuffer.
185 GraphTraceLen
= 32*64;
188 for (bit
= 0; bit
< 64; bit
++) {
190 phase
= (bits
[bit
] == 0) ? 0 : 1;
193 for (j
= 0; j
< 32; j
++) {
194 GraphBuffer
[i
++] = phase
;
199 RepaintGraphWindow();
203 int usage_lf_read(void)
205 PrintAndLog("Usage: lf read");
206 PrintAndLog("Options: ");
207 PrintAndLog(" h This help");
208 PrintAndLog(" s silent run no printout");
209 PrintAndLog(" [# samples] # samples to collect (optional)");
210 PrintAndLog("Use 'lf config' to set parameters.");
213 int usage_lf_snoop(void)
215 PrintAndLog("Usage: lf snoop");
216 PrintAndLog("Options: ");
217 PrintAndLog(" h This help");
218 PrintAndLog("This function takes no arguments. ");
219 PrintAndLog("Use 'lf config' to set parameters.");
223 int usage_lf_config(void)
225 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
226 PrintAndLog("Options: ");
227 PrintAndLog(" h This help");
228 PrintAndLog(" L Low frequency (125 KHz)");
229 PrintAndLog(" H High frequency (134 KHz)");
230 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
231 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
232 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
233 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
234 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
235 PrintAndLog("Examples:");
236 PrintAndLog(" lf config b 8 L");
237 PrintAndLog(" Samples at 125KHz, 8bps.");
238 PrintAndLog(" lf config H b 4 d 3");
239 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
240 PrintAndLog(" a resolution of 4 bits per sample.");
241 PrintAndLog(" lf read");
242 PrintAndLog(" Performs a read (active field)");
243 PrintAndLog(" lf snoop");
244 PrintAndLog(" Performs a snoop (no active field)");
248 int CmdLFSetConfig(const char *Cmd
)
251 uint8_t divisor
= 0;//Frequency divisor
252 uint8_t bps
= 0; // Bits per sample
253 uint8_t decimation
= 0; //How many to keep
254 bool averaging
= 1; // Defaults to true
256 int trigger_threshold
=-1;//Means no change
257 uint8_t unsigned_trigg
= 0;
260 while(param_getchar(Cmd
, cmdp
) != 0x00)
262 switch(param_getchar(Cmd
, cmdp
))
265 return usage_lf_config();
275 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
279 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
282 trigger_threshold
= unsigned_trigg
;
283 if (trigger_threshold
> 0) g_lf_threshold_set
= true;
287 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
291 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
295 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
299 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
307 errors
= 1;// No args
313 return usage_lf_config();
315 //Bps is limited to 8, so fits in lower half of arg1
316 if(bps
>> 4) bps
= 8;
318 sample_config config
= {
319 decimation
,bps
,averaging
,divisor
,trigger_threshold
321 //Averaging is a flag on high-bit of arg[1]
322 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
323 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
324 clearCommandBuffer();
329 bool lf_read(bool silent
, uint32_t samples
) {
330 if (IsOffline()) return false;
331 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {silent
,samples
,0}};
332 clearCommandBuffer();
333 //And ship it to device
337 if (g_lf_threshold_set
) {
338 WaitForResponse(CMD_ACK
,&resp
);
340 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,2500) ) {
341 PrintAndLog("command execution time out");
345 // resp.arg[0] is bits read not bytes read.
346 getSamples(resp
.arg
[0]/8, silent
);
351 int CmdLFRead(const char *Cmd
)
355 if (param_getchar(Cmd
, cmdp
) == 'h')
357 return usage_lf_read();
359 if (param_getchar(Cmd
, cmdp
) == 's') {
360 silent
= true; //suppress print
363 uint32_t samples
= param_get32ex(Cmd
, cmdp
, 0, 10);
364 return lf_read(silent
, samples
);
367 int CmdLFSnoop(const char *Cmd
)
370 if(param_getchar(Cmd
, cmdp
) == 'h')
372 return usage_lf_snoop();
375 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
376 clearCommandBuffer();
378 WaitForResponse(CMD_ACK
,NULL
);
384 static void ChkBitstream(const char *str
)
388 /* convert to bitstream if necessary */
389 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
390 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
396 //Attempt to simulate any wave in buffer (one bit per output sample)
397 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
398 int CmdLFSim(const char *Cmd
)
403 sscanf(Cmd
, "%i", &gap
);
405 // convert to bitstream if necessary
408 //can send only 512 bits at a time (1 byte sent per bit...)
409 printf("Sending [%d bytes]", GraphTraceLen
);
410 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
411 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
413 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
414 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
417 WaitForResponse(CMD_ACK
,NULL
);
422 PrintAndLog("Starting to simulate");
423 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
424 clearCommandBuffer();
429 int usage_lf_simfsk(void)
432 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
433 PrintAndLog("Options: ");
434 PrintAndLog(" h This help");
435 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
436 PrintAndLog(" i invert data");
437 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
438 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
439 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
440 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
441 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
445 int usage_lf_simask(void)
448 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
449 PrintAndLog("Options: ");
450 PrintAndLog(" h This help");
451 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
452 PrintAndLog(" i invert data");
453 PrintAndLog(" b sim ask/biphase");
454 PrintAndLog(" m sim ask/manchester - Default");
455 PrintAndLog(" r sim ask/raw");
456 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
457 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
461 int usage_lf_simpsk(void)
464 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
465 PrintAndLog("Options: ");
466 PrintAndLog(" h This help");
467 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
468 PrintAndLog(" i invert data");
469 PrintAndLog(" 1 set PSK1 (default)");
470 PrintAndLog(" 2 set PSK2");
471 PrintAndLog(" 3 set PSK3");
472 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
473 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
477 // by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert
478 // - allow pull data from DemodBuffer
479 int CmdLFfskSim(const char *Cmd
)
481 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
482 // otherwise will need FChigh, FClow, Clock, and bitstream
483 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
486 char hexData
[64] = {0x00}; // store entered hex data
487 uint8_t data
[255] = {0x00};
490 while(param_getchar(Cmd
, cmdp
) != 0x00)
492 switch(param_getchar(Cmd
, cmdp
))
495 return usage_lf_simfsk();
501 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
505 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
509 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
517 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
, sizeof(hexData
));
521 dataLen
= hextobinarray((char *)data
, hexData
);
523 if (dataLen
==0) errors
=true;
524 if (errors
) PrintAndLog ("Error getting hex data");
528 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
534 if(cmdp
== 0 && DemodBufferLen
== 0)
536 errors
= true;// No args
542 return usage_lf_simfsk();
544 int firstClockEdge
= 0;
545 if (dataLen
== 0){ //using DemodBuffer
546 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
547 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0, &firstClockEdge
);
549 if (!fcHigh
) fcHigh
=10;
555 setDemodBuf(data
, dataLen
, 0);
558 //default if not found
559 if (clk
== 0) clk
= 50;
560 if (fcHigh
== 0) fcHigh
= 10;
561 if (fcLow
== 0) fcLow
= 8;
564 arg1
= fcHigh
<< 8 | fcLow
;
565 arg2
= invert
<< 8 | clk
;
566 size_t size
= DemodBufferLen
;
567 if (size
> USB_CMD_DATA_SIZE
) {
568 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
569 size
= USB_CMD_DATA_SIZE
;
571 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
573 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
574 clearCommandBuffer();
579 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
580 // - allow pull data from DemodBuffer
581 int CmdLFaskSim(const char *Cmd
)
583 //autodetect clock from Graphbuffer if using demod buffer
584 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
585 uint8_t encoding
= 1, separator
= 0;
586 uint8_t clk
=0, invert
=0;
588 char hexData
[64] = {0x00};
589 uint8_t data
[255]= {0x00}; // store entered hex data
592 while(param_getchar(Cmd
, cmdp
) != 0x00)
594 switch(param_getchar(Cmd
, cmdp
))
597 return usage_lf_simask();
603 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
607 encoding
=2; //biphase
623 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
, sizeof(hexData
));
627 dataLen
= hextobinarray((char *)data
, hexData
);
629 if (dataLen
==0) errors
=true;
630 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
634 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
640 if(cmdp
== 0 && DemodBufferLen
== 0)
642 errors
= true;// No args
648 return usage_lf_simask();
650 if (dataLen
== 0){ //using DemodBuffer
651 if (clk
== 0) clk
= GetAskClock("0", false, false);
653 setDemodBuf(data
, dataLen
, 0);
655 if (clk
== 0) clk
= 64;
656 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
658 size_t size
=DemodBufferLen
;
659 arg1
= clk
<< 8 | encoding
;
660 arg2
= invert
<< 8 | separator
;
661 if (size
> USB_CMD_DATA_SIZE
) {
662 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
663 size
= USB_CMD_DATA_SIZE
;
665 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
666 PrintAndLog("preparing to sim ask data: %d bits", size
);
667 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
668 clearCommandBuffer();
673 // by marshmellow - sim psk data given carrier, clock, invert
674 // - allow pull data from DemodBuffer or parameters
675 int CmdLFpskSim(const char *Cmd
)
677 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
678 //will need carrier, Clock, and bitstream
679 uint8_t carrier
=0, clk
=0;
682 char hexData
[64] = {0x00}; // store entered hex data
683 uint8_t data
[255] = {0x00};
687 while(param_getchar(Cmd
, cmdp
) != 0x00)
689 switch(param_getchar(Cmd
, cmdp
))
692 return usage_lf_simpsk();
698 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
702 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
718 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
, sizeof(hexData
));
722 dataLen
= hextobinarray((char *)data
, hexData
);
724 if (dataLen
==0) errors
=true;
725 if (errors
) PrintAndLog ("Error getting hex data");
729 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
735 if (cmdp
== 0 && DemodBufferLen
== 0)
737 errors
= true;// No args
743 return usage_lf_simpsk();
745 if (dataLen
== 0){ //using DemodBuffer
746 PrintAndLog("Getting Clocks");
747 if (clk
==0) clk
= GetPskClock("", false, false);
748 PrintAndLog("clk: %d",clk
);
749 if (!carrier
) carrier
= GetPskCarrier("", false, false);
750 PrintAndLog("carrier: %d", carrier
);
752 setDemodBuf(data
, dataLen
, 0);
755 if (clk
<= 0) clk
= 32;
756 if (carrier
== 0) carrier
= 2;
759 //need to convert psk2 to psk1 data before sim
760 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
762 PrintAndLog("Sorry, PSK3 not yet available");
766 arg1
= clk
<< 8 | carrier
;
768 size_t size
=DemodBufferLen
;
769 if (size
> USB_CMD_DATA_SIZE
) {
770 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
771 size
=USB_CMD_DATA_SIZE
;
773 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
774 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
775 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
776 clearCommandBuffer();
782 int CmdLFSimBidir(const char *Cmd
)
784 // Set ADC to twice the carrier for a slight supersampling
785 // HACK: not implemented in ARMSRC.
786 PrintAndLog("Not implemented yet.");
787 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
792 int CmdVchDemod(const char *Cmd
)
794 // Is this the entire sync pattern, or does this also include some
795 // data bits that happen to be the same everywhere? That would be
797 static const int SyncPattern
[] = {
798 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
799 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
800 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
801 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
802 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
803 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
804 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
805 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
806 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
807 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
810 // So first, we correlate for the sync pattern, and mark that.
811 int bestCorrel
= 0, bestPos
= 0;
813 // It does us no good to find the sync pattern, with fewer than
814 // 2048 samples after it...
815 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
818 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
819 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
821 if (sum
> bestCorrel
) {
826 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
834 for (i
= 0; i
< 2048; i
+= 8) {
837 for (j
= 0; j
< 8; j
++) {
838 sum
+= GraphBuffer
[bestPos
+i
+j
];
845 if(abs(sum
) < worst
) {
850 PrintAndLog("bits:");
851 PrintAndLog("%s", bits
);
852 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
854 if (strcmp(Cmd
, "clone")==0) {
857 for(s
= bits
; *s
; s
++) {
859 for(j
= 0; j
< 16; j
++) {
860 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
863 RepaintGraphWindow();
870 int CheckChipType(char cmdp
) {
871 uint32_t wordData
= 0;
873 if (IsOffline() || cmdp
== '1') return 0;
875 save_restoreGB(GRAPH_SAVE
);
876 save_restoreDB(GRAPH_SAVE
);
877 //check for em4x05/em4x69 chips first
878 if (EM4x05Block0Test(&wordData
)) {
879 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n");
880 save_restoreGB(GRAPH_RESTORE
);
881 save_restoreDB(GRAPH_RESTORE
);
885 //check for t55xx chip...
886 if (tryDetectP1(true)) {
887 PrintAndLog("\nValid T55xx Chip Found\nTry lf t55xx ... commands\n");
888 save_restoreGB(GRAPH_RESTORE
);
889 save_restoreDB(GRAPH_RESTORE
);
892 save_restoreGB(GRAPH_RESTORE
);
893 save_restoreDB(GRAPH_RESTORE
);
898 int CmdLFfind(const char *Cmd
)
900 uint32_t wordData
= 0;
902 size_t minLength
= 1000;
903 char cmdp
= param_getchar(Cmd
, 0);
904 char testRaw
= param_getchar(Cmd
, 1);
905 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') {
906 PrintAndLog("Usage: lf search <0|1> [u]");
907 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
908 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
910 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
911 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
912 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
913 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
918 if (!IsOffline() && (cmdp
!= '1')) {
919 lf_read(true, 30000);
920 } else if (GraphTraceLen
< minLength
) {
921 PrintAndLog("Data in Graphbuffer was too small.");
924 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
926 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
927 PrintAndLog("False Positives ARE possible\n");
928 PrintAndLog("\nChecking for known tags:\n");
930 size_t testLen
= minLength
;
931 // only run if graphbuffer is just noise as it should be for hitag/cotag
932 if (graphJustNoise(GraphBuffer
, testLen
)) {
933 // only run these tests if we are in online mode
934 if (!IsOffline() && (cmdp
!= '1')) {
935 // test for em4x05 in reader talk first mode.
936 if (EM4x05Block0Test(&wordData
)) {
937 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n");
940 ans
=CmdLFHitagReader("26"); // 26 = RHT2F_UID_ONLY
944 ans
=CmdCOTAGRead("");
946 PrintAndLog("\nValid COTAG ID Found!");
950 PrintAndLog("\nNo Data Found! - maybe not an LF tag?\n");
954 // TODO test for modulation then only test formats that use that modulation
956 ans
=CmdFSKdemodIO("");
958 PrintAndLog("\nValid IO Prox ID Found!");
959 return CheckChipType(cmdp
);
962 ans
=CmdFSKdemodPyramid("");
964 PrintAndLog("\nValid Pyramid ID Found!");
965 return CheckChipType(cmdp
);
968 ans
=CmdFSKdemodParadox("");
970 PrintAndLog("\nValid Paradox ID Found!");
971 return CheckChipType(cmdp
);
974 ans
=CmdFSKdemodAWID("");
976 PrintAndLog("\nValid AWID ID Found!");
977 return CheckChipType(cmdp
);
980 ans
=CmdFSKdemodHID("");
982 PrintAndLog("\nValid HID Prox ID Found!");
983 return CheckChipType(cmdp
);
986 ans
=CmdAskEM410xDemod("");
988 PrintAndLog("\nValid EM410x ID Found!");
989 return CheckChipType(cmdp
);
992 ans
=CmdVisa2kDemod("");
994 PrintAndLog("\nValid Visa2000 ID Found!");
995 return CheckChipType(cmdp
);
998 ans
=CmdG_Prox_II_Demod("");
1000 PrintAndLog("\nValid G Prox II ID Found!");
1001 return CheckChipType(cmdp
);
1004 ans
=CmdFdxDemod(""); //biphase
1006 PrintAndLog("\nValid FDX-B ID Found!");
1007 return CheckChipType(cmdp
);
1010 ans
=EM4x50Read("", false); //ask
1012 PrintAndLog("\nValid EM4x50 ID Found!");
1016 ans
=CmdJablotronDemod("");
1018 PrintAndLog("\nValid Jablotron ID Found!");
1019 return CheckChipType(cmdp
);
1022 ans
=CmdNoralsyDemod("");
1024 PrintAndLog("\nValid Noralsy ID Found!");
1025 return CheckChipType(cmdp
);
1028 ans
=CmdSecurakeyDemod("");
1030 PrintAndLog("\nValid Securakey ID Found!");
1031 return CheckChipType(cmdp
);
1034 ans
=CmdVikingDemod("");
1036 PrintAndLog("\nValid Viking ID Found!");
1037 return CheckChipType(cmdp
);
1040 ans
=CmdIndalaDecode(""); //psk
1042 PrintAndLog("\nValid Indala ID Found!");
1043 return CheckChipType(cmdp
);
1046 ans
=CmdPSKNexWatch("");
1048 PrintAndLog("\nValid NexWatch ID Found!");
1049 return CheckChipType(cmdp
);
1052 ans
=CmdPacDemod("");
1054 PrintAndLog("\nValid PAC/Stanley ID Found!");
1055 return CheckChipType(cmdp
);
1058 PrintAndLog("\nNo Known Tags Found!\n");
1059 if (testRaw
=='u' || testRaw
=='U') {
1060 //ans=CheckChipType(cmdp);
1061 //test unknown tag formats (raw mode)0
1062 PrintAndLog("\nChecking for Unknown tags:\n");
1063 ans
=AutoCorrelate(GraphBuffer
, GraphBuffer
, GraphTraceLen
, 4000, false, false);
1064 if (ans
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1065 ans
=GetFskClock("",false,false);
1066 if (ans
!= 0) { //fsk
1067 ans
=FSKrawDemod("",true);
1069 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1070 return CheckChipType(cmdp
);
1074 ans
=ASKDemod_ext("0 0 0",true,false,1,&st
);
1076 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1077 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1078 return CheckChipType(cmdp
);
1080 ans
=CmdPSK1rawDemod("");
1082 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1083 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1084 PrintAndLog("\nCould also be NRZ - try 'data rawdemod nr'");
1085 return CheckChipType(cmdp
);
1087 ans
= CheckChipType(cmdp
);
1088 PrintAndLog("\nNo Data Found!\n");
1093 static command_t CommandTable
[] =
1095 {"help", CmdHelp
, 1, "This help"},
1096 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1097 {"cotag", CmdLFCOTAG
, 1, "{ COTAG CHIPs... }"},
1098 {"em", CmdLFEM4X
, 1, "{ EM4X CHIPs & RFIDs... }"},
1099 {"fdx", CmdLFFdx
, 1, "{ FDX-B RFIDs... }"},
1100 {"gproxii", CmdLF_G_Prox_II
, 1, "{ G Prox II RFIDs... }"},
1101 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1102 {"hitag", CmdLFHitag
, 1, "{ Hitag CHIPs... }"},
1103 {"io", CmdLFIO
, 1, "{ ioProx RFIDs... }"},
1104 {"indala", CmdLFINDALA
, 1, "{ Indala RFIDs... }"},
1105 {"jablotron", CmdLFJablotron
, 1, "{ Jablotron RFIDs... }"},
1106 {"nexwatch", CmdLFNexWatch
, 1, "{ NexWatch RFIDs... }"},
1107 {"noralsy", CmdLFNoralsy
, 1, "{ Noralsy RFIDs... }"},
1108 {"pac", CmdLFPac
, 1, "{ PAC/Stanley RFIDs... }"},
1109 {"paradox", CmdLFParadox
, 1, "{ Paradox RFIDs... }"},
1110 {"presco", CmdLFPresco
, 1, "{ Presco RFIDs... }"},
1111 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 CHIPs... }"},
1112 {"pyramid", CmdLFPyramid
, 1, "{ Farpointe/Pyramid RFIDs... }"},
1113 {"securakey", CmdLFSecurakey
, 1, "{ Securakey RFIDs... }"},
1114 {"t55xx", CmdLFT55XX
, 1, "{ T55xx CHIPs... }"},
1115 {"ti", CmdLFTI
, 1, "{ TI CHIPs... }"},
1116 {"viking", CmdLFViking
, 1, "{ Viking RFIDs... }"},
1117 {"visa2000", CmdLFVisa2k
, 1, "{ Visa2000 RFIDs... }"},
1118 {"cmdread", CmdLFCommandRead
, 0, "<d period> <z period> <o period> <c command> ['H'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'H' for 134)"},
1119 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1120 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1121 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1122 {"search", CmdLFfind
, 1, "[offline] ['u'] Read and Search for valid known tag (in offline mode it you can load first then search) - 'u' to search for unknown tags"},
1123 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1124 {"simask", CmdLFaskSim
, 0, "[clock] [invert <1|0>] [biphase/manchester/raw <'b'|'m'|'r'>] [msg separator 's'] [d <hexdata>] -- Simulate LF ASK tag from demodbuffer or input"},
1125 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1126 {"simpsk", CmdLFpskSim
, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1127 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1128 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1129 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1130 {NULL
, NULL
, 0, NULL
}
1133 int CmdLF(const char *Cmd
)
1135 CmdsParse(CommandTable
, Cmd
);
1139 int CmdHelp(const char *Cmd
)
1141 CmdsHelp(CommandTable
);