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-> 134 KHz, 95-> 125 KHz");
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(" s <smplstoskip> Sets a number of samples to skip before capture. Default: 0");
236 PrintAndLog("Examples:");
237 PrintAndLog(" lf config b 8 L");
238 PrintAndLog(" Samples at 125KHz, 8bps.");
239 PrintAndLog(" lf config H b 4 d 3");
240 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
241 PrintAndLog(" a resolution of 4 bits per sample.");
242 PrintAndLog(" lf read");
243 PrintAndLog(" Performs a read (active field)");
244 PrintAndLog(" lf snoop");
245 PrintAndLog(" Performs a snoop (no active field)");
249 int CmdLFSetConfig(const char *Cmd
)
252 uint8_t divisor
= 0;//Frequency divisor
253 uint8_t bps
= 0; // Bits per sample
254 uint8_t decimation
= 0; //How many to keep
255 bool averaging
= 1; // Defaults to true
257 int trigger_threshold
=-1;//Means no change
258 uint8_t unsigned_trigg
= 0;
259 int samples_to_skip
= -1;
262 while(param_getchar(Cmd
, cmdp
) != 0x00)
264 switch(param_getchar(Cmd
, cmdp
))
267 return usage_lf_config();
277 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
281 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
284 trigger_threshold
= unsigned_trigg
;
285 if (trigger_threshold
> 0) g_lf_threshold_set
= true;
289 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
293 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
297 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
301 samples_to_skip
= param_get32ex(Cmd
,cmdp
+1,0,10);
305 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
313 errors
= 1;// No args
319 return usage_lf_config();
321 //Bps is limited to 8, so fits in lower half of arg1
322 if(bps
>> 4) bps
= 8;
324 sample_config config
= {
325 decimation
,bps
,averaging
,divisor
,trigger_threshold
,samples_to_skip
327 //Averaging is a flag on high-bit of arg[1]
328 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
329 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
330 clearCommandBuffer();
335 bool lf_read(bool silent
, uint32_t samples
) {
336 if (IsOffline()) return false;
337 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {silent
,samples
,0}};
338 clearCommandBuffer();
339 //And ship it to device
343 if (g_lf_threshold_set
) {
344 WaitForResponse(CMD_ACK
,&resp
);
346 if ( !WaitForResponseTimeout(CMD_ACK
,&resp
,2500) ) {
347 PrintAndLog("command execution time out");
351 // resp.arg[0] is bits read not bytes read.
352 getSamples(resp
.arg
[0]/8, silent
);
357 int CmdLFRead(const char *Cmd
)
361 if (param_getchar(Cmd
, cmdp
) == 'h')
363 return usage_lf_read();
365 if (param_getchar(Cmd
, cmdp
) == 's') {
366 silent
= true; //suppress print
369 uint32_t samples
= param_get32ex(Cmd
, cmdp
, 0, 10);
370 return lf_read(silent
, samples
);
373 int CmdLFSnoop(const char *Cmd
)
376 if(param_getchar(Cmd
, cmdp
) == 'h')
378 return usage_lf_snoop();
381 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
382 clearCommandBuffer();
384 WaitForResponse(CMD_ACK
,NULL
);
390 static void ChkBitstream(const char *str
)
394 /* convert to bitstream if necessary */
395 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
396 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
402 //Attempt to simulate any wave in buffer (one bit per output sample)
403 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
404 int CmdLFSim(const char *Cmd
)
409 sscanf(Cmd
, "%i", &gap
);
411 // convert to bitstream if necessary
414 //can send only 512 bits at a time (1 byte sent per bit...)
415 printf("Sending [%d bytes]", GraphTraceLen
);
416 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
417 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
419 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
420 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
423 WaitForResponse(CMD_ACK
,NULL
);
428 PrintAndLog("Starting to simulate");
429 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
430 clearCommandBuffer();
435 int usage_lf_simfsk(void)
438 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
439 PrintAndLog("Options: ");
440 PrintAndLog(" h This help");
441 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
442 PrintAndLog(" i invert data");
443 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
444 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
445 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
446 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
447 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
451 int usage_lf_simask(void)
454 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
455 PrintAndLog("Options: ");
456 PrintAndLog(" h This help");
457 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
458 PrintAndLog(" i invert data");
459 PrintAndLog(" b sim ask/biphase");
460 PrintAndLog(" m sim ask/manchester - Default");
461 PrintAndLog(" r sim ask/raw");
462 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
463 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
467 int usage_lf_simpsk(void)
470 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
471 PrintAndLog("Options: ");
472 PrintAndLog(" h This help");
473 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
474 PrintAndLog(" i invert data");
475 PrintAndLog(" 1 set PSK1 (default)");
476 PrintAndLog(" 2 set PSK2");
477 PrintAndLog(" 3 set PSK3");
478 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
479 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
483 // by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert
484 // - allow pull data from DemodBuffer
485 int CmdLFfskSim(const char *Cmd
)
487 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
488 // otherwise will need FChigh, FClow, Clock, and bitstream
489 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
492 char hexData
[64] = {0x00}; // store entered hex data
493 uint8_t data
[255] = {0x00};
496 while(param_getchar(Cmd
, cmdp
) != 0x00)
498 switch(param_getchar(Cmd
, cmdp
))
501 return usage_lf_simfsk();
507 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
511 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
515 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
523 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
, sizeof(hexData
));
527 dataLen
= hextobinarray((char *)data
, hexData
);
529 if (dataLen
==0) errors
=true;
530 if (errors
) PrintAndLog ("Error getting hex data");
534 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
540 if(cmdp
== 0 && DemodBufferLen
== 0)
542 errors
= true;// No args
548 return usage_lf_simfsk();
550 int firstClockEdge
= 0;
551 if (dataLen
== 0){ //using DemodBuffer
552 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
553 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0, &firstClockEdge
);
555 if (!fcHigh
) fcHigh
=10;
561 setDemodBuf(data
, dataLen
, 0);
564 //default if not found
565 if (clk
== 0) clk
= 50;
566 if (fcHigh
== 0) fcHigh
= 10;
567 if (fcLow
== 0) fcLow
= 8;
570 arg1
= fcHigh
<< 8 | fcLow
;
571 arg2
= invert
<< 8 | clk
;
572 size_t size
= DemodBufferLen
;
573 if (size
> USB_CMD_DATA_SIZE
) {
574 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
575 size
= USB_CMD_DATA_SIZE
;
577 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
579 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
580 clearCommandBuffer();
585 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
586 // - allow pull data from DemodBuffer
587 int CmdLFaskSim(const char *Cmd
)
589 //autodetect clock from Graphbuffer if using demod buffer
590 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
591 uint8_t encoding
= 1, separator
= 0;
592 uint8_t clk
=0, invert
=0;
594 char hexData
[64] = {0x00};
595 uint8_t data
[255]= {0x00}; // store entered hex data
598 while(param_getchar(Cmd
, cmdp
) != 0x00)
600 switch(param_getchar(Cmd
, cmdp
))
603 return usage_lf_simask();
609 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
613 encoding
=2; //biphase
629 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
, sizeof(hexData
));
633 dataLen
= hextobinarray((char *)data
, hexData
);
635 if (dataLen
==0) errors
=true;
636 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
640 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
646 if(cmdp
== 0 && DemodBufferLen
== 0)
648 errors
= true;// No args
654 return usage_lf_simask();
656 if (dataLen
== 0){ //using DemodBuffer
657 if (clk
== 0) clk
= GetAskClock("0", false, false);
659 setDemodBuf(data
, dataLen
, 0);
661 if (clk
== 0) clk
= 64;
662 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
664 size_t size
=DemodBufferLen
;
665 arg1
= clk
<< 8 | encoding
;
666 arg2
= invert
<< 8 | separator
;
667 if (size
> USB_CMD_DATA_SIZE
) {
668 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
669 size
= USB_CMD_DATA_SIZE
;
671 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
672 PrintAndLog("preparing to sim ask data: %d bits", size
);
673 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
674 clearCommandBuffer();
679 // by marshmellow - sim psk data given carrier, clock, invert
680 // - allow pull data from DemodBuffer or parameters
681 int CmdLFpskSim(const char *Cmd
)
683 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
684 //will need carrier, Clock, and bitstream
685 uint8_t carrier
=0, clk
=0;
688 char hexData
[64] = {0x00}; // store entered hex data
689 uint8_t data
[255] = {0x00};
693 while(param_getchar(Cmd
, cmdp
) != 0x00)
695 switch(param_getchar(Cmd
, cmdp
))
698 return usage_lf_simpsk();
704 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
708 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
724 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
, sizeof(hexData
));
728 dataLen
= hextobinarray((char *)data
, hexData
);
730 if (dataLen
==0) errors
=true;
731 if (errors
) PrintAndLog ("Error getting hex data");
735 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
741 if (cmdp
== 0 && DemodBufferLen
== 0)
743 errors
= true;// No args
749 return usage_lf_simpsk();
751 if (dataLen
== 0){ //using DemodBuffer
752 PrintAndLog("Getting Clocks");
753 if (clk
==0) clk
= GetPskClock("", false, false);
754 PrintAndLog("clk: %d",clk
);
755 if (!carrier
) carrier
= GetPskCarrier("", false, false);
756 PrintAndLog("carrier: %d", carrier
);
758 setDemodBuf(data
, dataLen
, 0);
761 if (clk
<= 0) clk
= 32;
762 if (carrier
== 0) carrier
= 2;
765 //need to convert psk2 to psk1 data before sim
766 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
768 PrintAndLog("Sorry, PSK3 not yet available");
772 arg1
= clk
<< 8 | carrier
;
774 size_t size
=DemodBufferLen
;
775 if (size
> USB_CMD_DATA_SIZE
) {
776 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
777 size
=USB_CMD_DATA_SIZE
;
779 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
780 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
781 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
782 clearCommandBuffer();
788 int CmdLFSimBidir(const char *Cmd
)
790 // Set ADC to twice the carrier for a slight supersampling
791 // HACK: not implemented in ARMSRC.
792 PrintAndLog("Not implemented yet.");
793 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
798 int CmdVchDemod(const char *Cmd
)
800 // Is this the entire sync pattern, or does this also include some
801 // data bits that happen to be the same everywhere? That would be
803 static const int SyncPattern
[] = {
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,
808 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
809 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
810 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
811 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
812 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
813 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
816 // So first, we correlate for the sync pattern, and mark that.
817 int bestCorrel
= 0, bestPos
= 0;
819 // It does us no good to find the sync pattern, with fewer than
820 // 2048 samples after it...
821 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
824 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
825 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
827 if (sum
> bestCorrel
) {
832 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
840 for (i
= 0; i
< 2048; i
+= 8) {
843 for (j
= 0; j
< 8; j
++) {
844 sum
+= GraphBuffer
[bestPos
+i
+j
];
851 if(abs(sum
) < worst
) {
856 PrintAndLog("bits:");
857 PrintAndLog("%s", bits
);
858 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
860 if (strcmp(Cmd
, "clone")==0) {
863 for(s
= bits
; *s
; s
++) {
865 for(j
= 0; j
< 16; j
++) {
866 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
869 RepaintGraphWindow();
876 int CheckChipType(char cmdp
) {
877 uint32_t wordData
= 0;
879 if (IsOffline() || cmdp
== '1') return 0;
881 save_restoreGB(GRAPH_SAVE
);
882 save_restoreDB(GRAPH_SAVE
);
883 //check for em4x05/em4x69 chips first
884 if (EM4x05Block0Test(&wordData
)) {
885 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n");
886 save_restoreGB(GRAPH_RESTORE
);
887 save_restoreDB(GRAPH_RESTORE
);
891 //check for t55xx chip...
892 if (tryDetectP1(true)) {
893 PrintAndLog("\nValid T55xx Chip Found\nTry lf t55xx ... commands\n");
894 save_restoreGB(GRAPH_RESTORE
);
895 save_restoreDB(GRAPH_RESTORE
);
898 save_restoreGB(GRAPH_RESTORE
);
899 save_restoreDB(GRAPH_RESTORE
);
904 int CmdLFfind(const char *Cmd
)
906 uint32_t wordData
= 0;
908 size_t minLength
= 1000;
909 char cmdp
= param_getchar(Cmd
, 0);
910 char testRaw
= param_getchar(Cmd
, 1);
911 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') {
912 PrintAndLog("Usage: lf search <0|1> [u]");
913 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
914 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
916 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
917 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
918 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
919 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
924 if (!IsOffline() && (cmdp
!= '1')) {
925 lf_read(true, 30000);
926 } else if (GraphTraceLen
< minLength
) {
927 PrintAndLog("Data in Graphbuffer was too small.");
930 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
932 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
933 PrintAndLog("False Positives ARE possible\n");
934 PrintAndLog("\nChecking for known tags:\n");
936 size_t testLen
= minLength
;
937 // only run if graphbuffer is just noise as it should be for hitag/cotag
938 if (graphJustNoise(GraphBuffer
, testLen
)) {
939 // only run these tests if we are in online mode
940 if (!IsOffline() && (cmdp
!= '1')) {
941 // test for em4x05 in reader talk first mode.
942 if (EM4x05Block0Test(&wordData
)) {
943 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n");
946 if (getHitagUid(NULL
, true)) {
947 PrintAndLog("\nValid Hitag2 tag Found!");
950 ans
= CmdCOTAGRead("");
952 PrintAndLog("\nValid COTAG ID Found!");
956 PrintAndLog("\nNo Data Found! - maybe not an LF tag?\n");
960 // TODO test for modulation then only test formats that use that modulation
962 ans
=CmdFSKdemodIO("");
964 PrintAndLog("\nValid IO Prox ID Found!");
965 return CheckChipType(cmdp
);
968 ans
=CmdFSKdemodPyramid("");
970 PrintAndLog("\nValid Pyramid ID Found!");
971 return CheckChipType(cmdp
);
974 ans
=CmdFSKdemodParadox("");
976 PrintAndLog("\nValid Paradox ID Found!");
977 return CheckChipType(cmdp
);
980 ans
=CmdFSKdemodAWID("");
982 PrintAndLog("\nValid AWID ID Found!");
983 return CheckChipType(cmdp
);
986 ans
=CmdFSKdemodHID("");
988 PrintAndLog("\nValid HID Prox ID Found!");
989 return CheckChipType(cmdp
);
992 ans
=CmdAskEM410xDemod("");
994 PrintAndLog("\nValid EM410x ID Found!");
995 return CheckChipType(cmdp
);
998 ans
=CmdVisa2kDemod("");
1000 PrintAndLog("\nValid Visa2000 ID Found!");
1001 return CheckChipType(cmdp
);
1004 ans
=CmdG_Prox_II_Demod("");
1006 PrintAndLog("\nValid G Prox II ID Found!");
1007 return CheckChipType(cmdp
);
1010 ans
=CmdFdxDemod(""); //biphase
1012 PrintAndLog("\nValid FDX-B ID Found!");
1013 return CheckChipType(cmdp
);
1016 ans
=EM4x50Read("", false); //ask
1018 PrintAndLog("\nValid EM4x50 ID Found!");
1022 ans
=CmdJablotronDemod("");
1024 PrintAndLog("\nValid Jablotron ID Found!");
1025 return CheckChipType(cmdp
);
1028 ans
=CmdNoralsyDemod("");
1030 PrintAndLog("\nValid Noralsy ID Found!");
1031 return CheckChipType(cmdp
);
1034 ans
=CmdSecurakeyDemod("");
1036 PrintAndLog("\nValid Securakey ID Found!");
1037 return CheckChipType(cmdp
);
1040 ans
=CmdVikingDemod("");
1042 PrintAndLog("\nValid Viking ID Found!");
1043 return CheckChipType(cmdp
);
1046 ans
=CmdIndalaDecode(""); //psk
1048 PrintAndLog("\nValid Indala ID Found!");
1049 return CheckChipType(cmdp
);
1052 ans
=CmdPSKNexWatch("");
1054 PrintAndLog("\nValid NexWatch ID Found!");
1055 return CheckChipType(cmdp
);
1058 ans
=CmdPacDemod("");
1060 PrintAndLog("\nValid PAC/Stanley ID Found!");
1061 return CheckChipType(cmdp
);
1064 PrintAndLog("\nNo Known Tags Found!\n");
1065 if (testRaw
=='u' || testRaw
=='U') {
1066 //ans=CheckChipType(cmdp);
1067 //test unknown tag formats (raw mode)0
1068 PrintAndLog("\nChecking for Unknown tags:\n");
1069 ans
=AutoCorrelate(GraphBuffer
, GraphBuffer
, GraphTraceLen
, 4000, false, false);
1070 if (ans
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1071 ans
=GetFskClock("",false,false);
1072 if (ans
!= 0) { //fsk
1073 ans
=FSKrawDemod("",true);
1075 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1076 return CheckChipType(cmdp
);
1080 ans
=ASKDemod_ext("0 0 0",true,false,1,&st
);
1082 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1083 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1084 return CheckChipType(cmdp
);
1086 ans
=CmdPSK1rawDemod("");
1088 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1089 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1090 PrintAndLog("\nCould also be NRZ - try 'data rawdemod nr'");
1091 return CheckChipType(cmdp
);
1093 ans
= CheckChipType(cmdp
);
1094 PrintAndLog("\nNo Data Found!\n");
1099 static command_t CommandTable
[] =
1101 {"help", CmdHelp
, 1, "This help"},
1102 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1103 {"cotag", CmdLFCOTAG
, 1, "{ COTAG CHIPs... }"},
1104 {"em", CmdLFEM4X
, 1, "{ EM4X CHIPs & RFIDs... }"},
1105 {"fdx", CmdLFFdx
, 1, "{ FDX-B RFIDs... }"},
1106 {"gproxii", CmdLF_G_Prox_II
, 1, "{ G Prox II RFIDs... }"},
1107 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1108 {"hitag", CmdLFHitag
, 1, "{ Hitag CHIPs... }"},
1109 {"io", CmdLFIO
, 1, "{ ioProx RFIDs... }"},
1110 {"indala", CmdLFINDALA
, 1, "{ Indala RFIDs... }"},
1111 {"jablotron", CmdLFJablotron
, 1, "{ Jablotron RFIDs... }"},
1112 {"nexwatch", CmdLFNexWatch
, 1, "{ NexWatch RFIDs... }"},
1113 {"noralsy", CmdLFNoralsy
, 1, "{ Noralsy RFIDs... }"},
1114 {"pac", CmdLFPac
, 1, "{ PAC/Stanley RFIDs... }"},
1115 {"paradox", CmdLFParadox
, 1, "{ Paradox RFIDs... }"},
1116 {"presco", CmdLFPresco
, 1, "{ Presco RFIDs... }"},
1117 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 CHIPs... }"},
1118 {"pyramid", CmdLFPyramid
, 1, "{ Farpointe/Pyramid RFIDs... }"},
1119 {"securakey", CmdLFSecurakey
, 1, "{ Securakey RFIDs... }"},
1120 {"t55xx", CmdLFT55XX
, 1, "{ T55xx CHIPs... }"},
1121 {"ti", CmdLFTI
, 1, "{ TI CHIPs... }"},
1122 {"viking", CmdLFViking
, 1, "{ Viking RFIDs... }"},
1123 {"visa2000", CmdLFVisa2k
, 1, "{ Visa2000 RFIDs... }"},
1124 {"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)"},
1125 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1126 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1127 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1128 {"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"},
1129 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1130 {"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"},
1131 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1132 {"simpsk", CmdLFpskSim
, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1133 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1134 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1135 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1136 {NULL
, NULL
, 0, NULL
}
1139 int CmdLF(const char *Cmd
)
1141 CmdsParse(CommandTable
, Cmd
);
1145 int CmdHelp(const char *Cmd
)
1147 CmdsHelp(CommandTable
);