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 //-----------------------------------------------------------------------------
15 #include "proxmark3.h"
17 #include "lfdemod.h" // for psk2TOpsk1
18 #include "util.h" // for parsing cli command utils
19 #include "ui.h" // for show graph controls
20 #include "graph.h" // for graph data
21 #include "cmdparser.h" // for getting cli commands included in cmdmain.h
22 #include "cmdmain.h" // for sending cmds to device
23 #include "data.h" // for GetFromBigBuf
24 #include "cmddata.h" // for `lf search`
25 #include "cmdlfawid.h" // for awid menu
26 #include "cmdlfem4x.h" // for em4x menu
27 #include "cmdlfhid.h" // for hid menu
28 #include "cmdlfhitag.h" // for hitag menu
29 #include "cmdlfio.h" // for ioprox menu
30 #include "cmdlft55xx.h" // for t55xx menu
31 #include "cmdlfti.h" // for ti menu
32 #include "cmdlfpresco.h" // for presco menu
33 #include "cmdlfpcf7931.h"// for pcf7931 menu
34 #include "cmdlfpyramid.h"// for pyramid menu
35 #include "cmdlfviking.h" // for viking menu
37 static int CmdHelp(const char *Cmd
);
41 int usage_lf_cmdread()
43 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H] ");
44 PrintAndLog("Options: ");
45 PrintAndLog(" h This help");
46 PrintAndLog(" L Low frequency (125 KHz)");
47 PrintAndLog(" H High frequency (134 KHz)");
48 PrintAndLog(" d <delay> delay OFF period");
49 PrintAndLog(" z <zero> time period ZERO");
50 PrintAndLog(" o <one> time period ONE");
51 PrintAndLog(" c <cmd> Command bytes");
52 PrintAndLog(" ************* All periods in microseconds");
53 PrintAndLog("Examples:");
54 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
55 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
59 /* send a command before reading */
60 int CmdLFCommandRead(const char *Cmd
)
62 static char dummy
[3] = {0x20,0x00,0x00};
63 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
65 //uint8_t divisor = 95; //125khz
68 while(param_getchar(Cmd
, cmdp
) != 0x00)
70 switch(param_getchar(Cmd
, cmdp
))
73 return usage_lf_cmdread();
83 strLength
= param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
);
87 c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
91 c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
95 c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
99 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
106 if(cmdp
== 0) errors
= 1;
109 if(errors
) return usage_lf_cmdread();
111 // in case they specified 'H'
112 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
114 clearCommandBuffer();
119 int CmdFlexdemod(const char *Cmd
)
122 for (i
= 0; i
< GraphTraceLen
; ++i
) {
123 if (GraphBuffer
[i
] < 0) {
130 #define LONG_WAIT 100
132 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
133 int first
= GraphBuffer
[start
];
134 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
135 if (GraphBuffer
[i
] != first
) {
139 if (i
== (start
+ LONG_WAIT
)) {
143 if (start
== GraphTraceLen
- LONG_WAIT
) {
144 PrintAndLog("nothing to wait for");
148 GraphBuffer
[start
] = 2;
149 GraphBuffer
[start
+1] = -2;
150 uint8_t bits
[64] = {0x00};
154 for (bit
= 0; bit
< 64; bit
++) {
156 for (int j
= 0; j
< 16; j
++) {
157 sum
+= GraphBuffer
[i
++];
160 bits
[bit
] = (sum
> 0) ? 1 : 0;
162 PrintAndLog("bit %d sum %d", bit
, sum
);
165 for (bit
= 0; bit
< 64; bit
++) {
168 for (j
= 0; j
< 16; j
++) {
169 sum
+= GraphBuffer
[i
++];
171 if (sum
> 0 && bits
[bit
] != 1) {
172 PrintAndLog("oops1 at %d", bit
);
174 if (sum
< 0 && bits
[bit
] != 0) {
175 PrintAndLog("oops2 at %d", bit
);
179 // HACK writing back to graphbuffer.
180 GraphTraceLen
= 32*64;
183 for (bit
= 0; bit
< 64; bit
++) {
185 phase
= (bits
[bit
] == 0) ? 0 : 1;
188 for (j
= 0; j
< 32; j
++) {
189 GraphBuffer
[i
++] = phase
;
194 RepaintGraphWindow();
198 int CmdIndalaDemod(const char *Cmd
)
200 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
206 // worst case with GraphTraceLen=64000 is < 4096
207 // under normal conditions it's < 2048
209 uint8_t rawbits
[4096];
211 int worst
= 0, worstPos
= 0;
212 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
214 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
215 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
217 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
218 // appears redundant - marshmellow
220 for (j
= 0; j
< count
- 8; j
+= 16) {
221 rawbits
[rawbit
++] = 0;
223 if ((abs(count
- j
)) > worst
) {
224 worst
= abs(count
- j
);
230 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
233 for (j
= 0; j
< count
- 8; j
+= 16) {
234 rawbits
[rawbit
++] = 1;
236 if ((abs(count
- j
)) > worst
) {
237 worst
= abs(count
- j
);
247 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32);
248 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
253 // Finding the start of a UID
254 int uidlen
, long_wait
;
255 if (strcmp(Cmd
, "224") == 0) {
265 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
266 first
= rawbits
[start
];
267 for (i
= start
; i
< start
+ long_wait
; i
++) {
268 if (rawbits
[i
] != first
) {
272 if (i
== (start
+ long_wait
)) {
277 if (start
== rawbit
- uidlen
+ 1) {
278 PrintAndLog("nothing to wait for");
282 // Inverting signal if needed
284 for (i
= start
; i
< rawbit
; i
++) {
285 rawbits
[i
] = !rawbits
[i
];
290 uint8_t bits
[224] = {0x00};
291 char showbits
[225] = {0x00};
296 if (uidlen
> rawbit
) {
297 PrintAndLog("Warning: not enough raw bits to get a full UID");
298 for (bit
= 0; bit
< rawbit
; bit
++) {
299 bits
[bit
] = rawbits
[i
++];
300 // As we cannot know the parity, let's use "." and "/"
301 showbits
[bit
] = '.' + bits
[bit
];
303 showbits
[bit
+1]='\0';
304 PrintAndLog("Partial UID=%s", showbits
);
307 for (bit
= 0; bit
< uidlen
; bit
++) {
308 bits
[bit
] = rawbits
[i
++];
309 showbits
[bit
] = '0' + bits
[bit
];
315 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
320 for( idx
=0; idx
<64; idx
++) {
321 if (showbits
[idx
] == '0') {
322 uid1
=(uid1
<<1)|(uid2
>>31);
325 uid1
=(uid1
<<1)|(uid2
>>31);
329 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
332 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
334 for( idx
=0; idx
<224; idx
++) {
335 uid1
=(uid1
<<1)|(uid2
>>31);
336 uid2
=(uid2
<<1)|(uid3
>>31);
337 uid3
=(uid3
<<1)|(uid4
>>31);
338 uid4
=(uid4
<<1)|(uid5
>>31);
339 uid5
=(uid5
<<1)|(uid6
>>31);
340 uid6
=(uid6
<<1)|(uid7
>>31);
342 if (showbits
[idx
] == '0')
343 uid7
= (uid7
<<1) | 0;
345 uid7
= (uid7
<<1) | 1;
347 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
350 // Checking UID against next occurrences
352 for (; i
+ uidlen
<= rawbit
;) {
354 for (bit
= 0; bit
< uidlen
; bit
++) {
355 if (bits
[bit
] != rawbits
[i
++]) {
366 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
368 // Remodulating for tag cloning
369 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
370 // since this changes graphbuffer data.
371 GraphTraceLen
= 32*uidlen
;
374 for (bit
= 0; bit
< uidlen
; bit
++) {
375 if (bits
[bit
] == 0) {
381 for (j
= 0; j
< 32; j
++) {
382 GraphBuffer
[i
++] = phase
;
387 RepaintGraphWindow();
391 int CmdIndalaClone(const char *Cmd
)
394 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
396 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
399 if (strchr(Cmd
,'l') != 0) {
400 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
401 uid1
= (uid1
<< 4) | (uid2
>> 28);
402 uid2
= (uid2
<< 4) | (uid3
>> 28);
403 uid3
= (uid3
<< 4) | (uid4
>> 28);
404 uid4
= (uid4
<< 4) | (uid5
>> 28);
405 uid5
= (uid5
<< 4) | (uid6
>> 28);
406 uid6
= (uid6
<< 4) | (uid7
>> 28);
407 uid7
= (uid7
<< 4) | (n
& 0xf);
409 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
410 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
411 c
.d
.asDwords
[0] = uid1
;
412 c
.d
.asDwords
[1] = uid2
;
413 c
.d
.asDwords
[2] = uid3
;
414 c
.d
.asDwords
[3] = uid4
;
415 c
.d
.asDwords
[4] = uid5
;
416 c
.d
.asDwords
[5] = uid6
;
417 c
.d
.asDwords
[6] = uid7
;
419 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
420 uid1
= (uid1
<< 4) | (uid2
>> 28);
421 uid2
= (uid2
<< 4) | (n
& 0xf);
423 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
424 c
.cmd
= CMD_INDALA_CLONE_TAG
;
429 clearCommandBuffer();
436 PrintAndLog("Usage: lf read");
437 PrintAndLog("Options: ");
438 PrintAndLog(" h This help");
439 PrintAndLog(" s silent run no printout");
440 PrintAndLog("This function takes no arguments. ");
441 PrintAndLog("Use 'lf config' to set parameters.");
446 PrintAndLog("Usage: lf snoop");
447 PrintAndLog("Options: ");
448 PrintAndLog(" h This help");
449 PrintAndLog("This function takes no arguments. ");
450 PrintAndLog("Use 'lf config' to set parameters.");
454 int usage_lf_config()
456 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
457 PrintAndLog("Options: ");
458 PrintAndLog(" h This help");
459 PrintAndLog(" L Low frequency (125 KHz)");
460 PrintAndLog(" H High frequency (134 KHz)");
461 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
462 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
463 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
464 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
465 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
466 PrintAndLog("Examples:");
467 PrintAndLog(" lf config b 8 L");
468 PrintAndLog(" Samples at 125KHz, 8bps.");
469 PrintAndLog(" lf config H b 4 d 3");
470 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
471 PrintAndLog(" a resolution of 4 bits per sample.");
472 PrintAndLog(" lf read");
473 PrintAndLog(" Performs a read (active field)");
474 PrintAndLog(" lf snoop");
475 PrintAndLog(" Performs a snoop (no active field)");
479 int CmdLFSetConfig(const char *Cmd
)
482 uint8_t divisor
= 0;//Frequency divisor
483 uint8_t bps
= 0; // Bits per sample
484 uint8_t decimation
= 0; //How many to keep
485 bool averaging
= 1; // Defaults to true
487 int trigger_threshold
=-1;//Means no change
488 uint8_t unsigned_trigg
= 0;
491 while(param_getchar(Cmd
, cmdp
) != 0x00)
493 switch(param_getchar(Cmd
, cmdp
))
496 return usage_lf_config();
506 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
510 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
512 if(!errors
) trigger_threshold
= unsigned_trigg
;
515 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
519 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
523 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
527 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
535 errors
= 1;// No args
541 return usage_lf_config();
543 //Bps is limited to 8, so fits in lower half of arg1
544 if(bps
>> 4) bps
= 8;
546 sample_config config
= {
547 decimation
,bps
,averaging
,divisor
,trigger_threshold
549 //Averaging is a flag on high-bit of arg[1]
550 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
551 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
552 clearCommandBuffer();
557 int CmdLFRead(const char *Cmd
)
562 if (param_getchar(Cmd
, cmdp
) == 'h')
564 return usage_lf_read();
566 if (param_getchar(Cmd
, cmdp
) == 's') arg1
= true; //suppress print
567 //And ship it to device
568 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}};
569 clearCommandBuffer();
571 //WaitForResponse(CMD_ACK,NULL);
572 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
573 PrintAndLog("command execution time out");
580 int CmdLFSnoop(const char *Cmd
)
583 if(param_getchar(Cmd
, cmdp
) == 'h')
585 return usage_lf_snoop();
588 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
589 clearCommandBuffer();
591 WaitForResponse(CMD_ACK
,NULL
);
595 static void ChkBitstream(const char *str
)
599 /* convert to bitstream if necessary */
600 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
601 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
607 //Attempt to simulate any wave in buffer (one bit per output sample)
608 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
609 int CmdLFSim(const char *Cmd
)
614 sscanf(Cmd
, "%i", &gap
);
616 // convert to bitstream if necessary
620 //can send only 512 bits at a time (1 byte sent per bit...)
621 printf("Sending [%d bytes]", GraphTraceLen
);
622 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
623 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
625 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
626 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
629 WaitForResponse(CMD_ACK
,NULL
);
634 PrintAndLog("Starting to simulate");
635 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
636 clearCommandBuffer();
641 int usage_lf_simfsk(void)
644 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
645 PrintAndLog("Options: ");
646 PrintAndLog(" h This help");
647 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
648 PrintAndLog(" i invert data");
649 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
650 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
651 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
652 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
653 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
657 int usage_lf_simask(void)
660 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
661 PrintAndLog("Options: ");
662 PrintAndLog(" h This help");
663 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
664 PrintAndLog(" i invert data");
665 PrintAndLog(" b sim ask/biphase");
666 PrintAndLog(" m sim ask/manchester - Default");
667 PrintAndLog(" r sim ask/raw");
668 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
669 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
673 int usage_lf_simpsk(void)
676 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
677 PrintAndLog("Options: ");
678 PrintAndLog(" h This help");
679 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
680 PrintAndLog(" i invert data");
681 PrintAndLog(" 1 set PSK1 (default)");
682 PrintAndLog(" 2 set PSK2");
683 PrintAndLog(" 3 set PSK3");
684 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
685 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
689 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
690 // - allow pull data from DemodBuffer
691 int CmdLFfskSim(const char *Cmd
)
693 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
694 // otherwise will need FChigh, FClow, Clock, and bitstream
695 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
698 char hexData
[32] = {0x00}; // store entered hex data
699 uint8_t data
[255] = {0x00};
702 while(param_getchar(Cmd
, cmdp
) != 0x00)
704 switch(param_getchar(Cmd
, cmdp
))
707 return usage_lf_simfsk();
713 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
717 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
721 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
729 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
733 dataLen
= hextobinarray((char *)data
, hexData
);
735 if (dataLen
==0) errors
=TRUE
;
736 if (errors
) PrintAndLog ("Error getting hex data");
740 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
746 if(cmdp
== 0 && DemodBufferLen
== 0)
748 errors
= TRUE
;// No args
754 return usage_lf_simfsk();
757 if (dataLen
== 0){ //using DemodBuffer
758 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
759 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0);
761 if (!fcHigh
) fcHigh
=10;
767 setDemodBuf(data
, dataLen
, 0);
770 //default if not found
771 if (clk
== 0) clk
= 50;
772 if (fcHigh
== 0) fcHigh
= 10;
773 if (fcLow
== 0) fcLow
= 8;
776 arg1
= fcHigh
<< 8 | fcLow
;
777 arg2
= invert
<< 8 | clk
;
778 size_t size
= DemodBufferLen
;
779 if (size
> USB_CMD_DATA_SIZE
) {
780 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
781 size
= USB_CMD_DATA_SIZE
;
783 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
785 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
786 clearCommandBuffer();
791 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
792 // - allow pull data from DemodBuffer
793 int CmdLFaskSim(const char *Cmd
)
795 //autodetect clock from Graphbuffer if using demod buffer
796 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
797 uint8_t encoding
= 1, separator
= 0;
798 uint8_t clk
=0, invert
=0;
800 char hexData
[32] = {0x00};
801 uint8_t data
[255]= {0x00}; // store entered hex data
804 while(param_getchar(Cmd
, cmdp
) != 0x00)
806 switch(param_getchar(Cmd
, cmdp
))
809 return usage_lf_simask();
815 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
819 encoding
=2; //biphase
835 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
839 dataLen
= hextobinarray((char *)data
, hexData
);
841 if (dataLen
==0) errors
=TRUE
;
842 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
846 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
852 if(cmdp
== 0 && DemodBufferLen
== 0)
854 errors
= TRUE
;// No args
860 return usage_lf_simask();
862 if (dataLen
== 0){ //using DemodBuffer
863 if (clk
== 0) clk
= GetAskClock("0", false, false);
865 setDemodBuf(data
, dataLen
, 0);
867 if (clk
== 0) clk
= 64;
868 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
870 size_t size
=DemodBufferLen
;
871 arg1
= clk
<< 8 | encoding
;
872 arg2
= invert
<< 8 | separator
;
873 if (size
> USB_CMD_DATA_SIZE
) {
874 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
875 size
= USB_CMD_DATA_SIZE
;
877 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
878 PrintAndLog("preparing to sim ask data: %d bits", size
);
879 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
880 clearCommandBuffer();
885 // by marshmellow - sim psk data given carrier, clock, invert
886 // - allow pull data from DemodBuffer or parameters
887 int CmdLFpskSim(const char *Cmd
)
889 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
890 //will need carrier, Clock, and bitstream
891 uint8_t carrier
=0, clk
=0;
894 char hexData
[32] = {0x00}; // store entered hex data
895 uint8_t data
[255] = {0x00};
899 while(param_getchar(Cmd
, cmdp
) != 0x00)
901 switch(param_getchar(Cmd
, cmdp
))
904 return usage_lf_simpsk();
910 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
914 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
930 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
934 dataLen
= hextobinarray((char *)data
, hexData
);
936 if (dataLen
==0) errors
=TRUE
;
937 if (errors
) PrintAndLog ("Error getting hex data");
941 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
947 if (cmdp
== 0 && DemodBufferLen
== 0)
949 errors
= TRUE
;// No args
955 return usage_lf_simpsk();
957 if (dataLen
== 0){ //using DemodBuffer
958 PrintAndLog("Getting Clocks");
959 if (clk
==0) clk
= GetPskClock("", FALSE
, FALSE
);
960 PrintAndLog("clk: %d",clk
);
961 if (!carrier
) carrier
= GetPskCarrier("", FALSE
, FALSE
);
962 PrintAndLog("carrier: %d", carrier
);
964 setDemodBuf(data
, dataLen
, 0);
967 if (clk
<= 0) clk
= 32;
968 if (carrier
== 0) carrier
= 2;
971 //need to convert psk2 to psk1 data before sim
972 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
974 PrintAndLog("Sorry, PSK3 not yet available");
978 arg1
= clk
<< 8 | carrier
;
980 size_t size
=DemodBufferLen
;
981 if (size
> USB_CMD_DATA_SIZE
) {
982 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
983 size
=USB_CMD_DATA_SIZE
;
985 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
986 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
987 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
988 clearCommandBuffer();
994 int CmdLFSimBidir(const char *Cmd
)
996 // Set ADC to twice the carrier for a slight supersampling
997 // HACK: not implemented in ARMSRC.
998 PrintAndLog("Not implemented yet.");
999 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
1004 int CmdVchDemod(const char *Cmd
)
1006 // Is this the entire sync pattern, or does this also include some
1007 // data bits that happen to be the same everywhere? That would be
1009 static const int SyncPattern
[] = {
1010 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1011 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1012 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1013 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1014 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1015 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1016 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1017 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1018 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1019 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1022 // So first, we correlate for the sync pattern, and mark that.
1023 int bestCorrel
= 0, bestPos
= 0;
1025 // It does us no good to find the sync pattern, with fewer than
1026 // 2048 samples after it...
1027 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
1030 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
1031 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
1033 if (sum
> bestCorrel
) {
1038 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
1043 int worst
= INT_MAX
;
1046 for (i
= 0; i
< 2048; i
+= 8) {
1049 for (j
= 0; j
< 8; j
++) {
1050 sum
+= GraphBuffer
[bestPos
+i
+j
];
1057 if(abs(sum
) < worst
) {
1062 PrintAndLog("bits:");
1063 PrintAndLog("%s", bits
);
1064 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
1066 if (strcmp(Cmd
, "clone")==0) {
1069 for(s
= bits
; *s
; s
++) {
1071 for(j
= 0; j
< 16; j
++) {
1072 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
1075 RepaintGraphWindow();
1081 int CmdLFfind(const char *Cmd
)
1084 char cmdp
= param_getchar(Cmd
, 0);
1085 char testRaw
= param_getchar(Cmd
, 1);
1086 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') {
1087 PrintAndLog("Usage: lf search <0|1> [u]");
1088 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
1089 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
1091 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
1092 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
1093 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
1094 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
1099 if (!offline
&& (cmdp
!= '1')){
1101 getSamples("30000",false);
1102 } else if (GraphTraceLen
< 1000) {
1103 PrintAndLog("Data in Graphbuffer was too small.");
1106 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
1108 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1109 PrintAndLog("False Positives ARE possible\n");
1110 PrintAndLog("\nChecking for known tags:\n");
1112 ans
=CmdFSKdemodIO("");
1114 PrintAndLog("\nValid IO Prox ID Found!");
1118 ans
=CmdFSKdemodPyramid("");
1120 PrintAndLog("\nValid Pyramid ID Found!");
1124 ans
=CmdFSKdemodParadox("");
1126 PrintAndLog("\nValid Paradox ID Found!");
1130 ans
=CmdFSKdemodAWID("");
1132 PrintAndLog("\nValid AWID ID Found!");
1136 ans
=CmdFSKdemodHID("");
1138 PrintAndLog("\nValid HID Prox ID Found!");
1142 ans
=CmdAskEM410xDemod("");
1144 PrintAndLog("\nValid EM410x ID Found!");
1148 ans
=CmdG_Prox_II_Demod("");
1150 PrintAndLog("\nValid G Prox II ID Found!");
1154 ans
=CmdFDXBdemodBI("");
1156 PrintAndLog("\nValid FDX-B ID Found!");
1160 ans
=EM4x50Read("", false);
1162 PrintAndLog("\nValid EM4x50 ID Found!");
1166 ans
=CmdVikingDemod("");
1168 PrintAndLog("\nValid Viking ID Found!");
1172 ans
=CmdIndalaDecode("");
1174 PrintAndLog("\nValid Indala ID Found!");
1178 ans
=CmdPSKNexWatch("");
1180 PrintAndLog("\nValid NexWatch ID Found!");
1184 PrintAndLog("\nNo Known Tags Found!\n");
1185 if (testRaw
=='u' || testRaw
=='U'){
1186 //test unknown tag formats (raw mode)
1187 PrintAndLog("\nChecking for Unknown tags:\n");
1188 ans
=AutoCorrelate(4000, FALSE
, FALSE
);
1189 if (ans
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1190 ans
=GetFskClock("",FALSE
,FALSE
);
1191 if (ans
!= 0){ //fsk
1192 ans
=FSKrawDemod("",TRUE
);
1194 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1199 ans
=ASKDemod_ext("0 0 0",TRUE
,FALSE
,1,&st
);
1201 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1202 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1205 ans
=CmdPSK1rawDemod("");
1207 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1208 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1209 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1212 PrintAndLog("\nNo Data Found!\n");
1217 static command_t CommandTable
[] =
1219 {"help", CmdHelp
, 1, "This help"},
1220 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1221 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
1222 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1223 {"hitag", CmdLFHitag
, 1, "{ Hitag tags and transponders... }"},
1224 {"io", CmdLFIO
, 1, "{ ioProx tags... }"},
1225 {"presco", CmdLFPresco
, 1, "{ Presco RFIDs... }"},
1226 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 RFIDs... }"},
1227 {"pyramid", CmdLFPyramid
, 1, "{ Farpointe/Pyramid RFIDs... }"},
1228 {"t55xx", CmdLFT55XX
, 1, "{ T55xx RFIDs... }"},
1229 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
1230 {"viking", CmdLFViking
, 1, "{ Viking tags... }"},
1231 {"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)"},
1232 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1233 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1234 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1235 {"indalaclone", CmdIndalaClone
, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1236 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1237 {"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"},
1238 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1239 {"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"},
1240 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1241 {"simpsk", CmdLFpskSim
, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1242 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1243 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1244 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1245 {NULL
, NULL
, 0, NULL
}
1248 int CmdLF(const char *Cmd
)
1250 CmdsParse(CommandTable
, Cmd
);
1254 int CmdHelp(const char *Cmd
)
1256 CmdsHelp(CommandTable
);