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
67 while(param_getchar(Cmd
, cmdp
) != 0x00)
69 switch(param_getchar(Cmd
, cmdp
))
72 return usage_lf_cmdread();
82 param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
);
86 c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
90 c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
94 c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
98 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
105 if(cmdp
== 0) errors
= 1;
108 if(errors
) return usage_lf_cmdread();
110 // in case they specified 'H'
111 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
113 clearCommandBuffer();
118 int CmdFlexdemod(const char *Cmd
)
121 for (i
= 0; i
< GraphTraceLen
; ++i
) {
122 if (GraphBuffer
[i
] < 0) {
129 #define LONG_WAIT 100
131 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
132 int first
= GraphBuffer
[start
];
133 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
134 if (GraphBuffer
[i
] != first
) {
138 if (i
== (start
+ LONG_WAIT
)) {
142 if (start
== GraphTraceLen
- LONG_WAIT
) {
143 PrintAndLog("nothing to wait for");
147 GraphBuffer
[start
] = 2;
148 GraphBuffer
[start
+1] = -2;
149 uint8_t bits
[64] = {0x00};
153 for (bit
= 0; bit
< 64; bit
++) {
155 for (int j
= 0; j
< 16; j
++) {
156 sum
+= GraphBuffer
[i
++];
159 bits
[bit
] = (sum
> 0) ? 1 : 0;
161 PrintAndLog("bit %d sum %d", bit
, sum
);
164 for (bit
= 0; bit
< 64; bit
++) {
167 for (j
= 0; j
< 16; j
++) {
168 sum
+= GraphBuffer
[i
++];
170 if (sum
> 0 && bits
[bit
] != 1) {
171 PrintAndLog("oops1 at %d", bit
);
173 if (sum
< 0 && bits
[bit
] != 0) {
174 PrintAndLog("oops2 at %d", bit
);
178 // HACK writing back to graphbuffer.
179 GraphTraceLen
= 32*64;
182 for (bit
= 0; bit
< 64; bit
++) {
184 phase
= (bits
[bit
] == 0) ? 0 : 1;
187 for (j
= 0; j
< 32; j
++) {
188 GraphBuffer
[i
++] = phase
;
193 RepaintGraphWindow();
197 int CmdIndalaDemod(const char *Cmd
)
199 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
205 // worst case with GraphTraceLen=64000 is < 4096
206 // under normal conditions it's < 2048
208 uint8_t rawbits
[4096];
210 int worst
= 0, worstPos
= 0;
211 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
213 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
214 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
216 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
217 // appears redundant - marshmellow
219 for (j
= 0; j
< count
- 8; j
+= 16) {
220 rawbits
[rawbit
++] = 0;
222 if ((abs(count
- j
)) > worst
) {
223 worst
= abs(count
- j
);
229 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
232 for (j
= 0; j
< count
- 8; j
+= 16) {
233 rawbits
[rawbit
++] = 1;
235 if ((abs(count
- j
)) > worst
) {
236 worst
= abs(count
- j
);
246 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32);
247 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
252 // Finding the start of a UID
253 int uidlen
, long_wait
;
254 if (strcmp(Cmd
, "224") == 0) {
264 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
265 first
= rawbits
[start
];
266 for (i
= start
; i
< start
+ long_wait
; i
++) {
267 if (rawbits
[i
] != first
) {
271 if (i
== (start
+ long_wait
)) {
276 if (start
== rawbit
- uidlen
+ 1) {
277 PrintAndLog("nothing to wait for");
281 // Inverting signal if needed
283 for (i
= start
; i
< rawbit
; i
++) {
284 rawbits
[i
] = !rawbits
[i
];
289 uint8_t bits
[224] = {0x00};
290 char showbits
[225] = {0x00};
295 if (uidlen
> rawbit
) {
296 PrintAndLog("Warning: not enough raw bits to get a full UID");
297 for (bit
= 0; bit
< rawbit
; bit
++) {
298 bits
[bit
] = rawbits
[i
++];
299 // As we cannot know the parity, let's use "." and "/"
300 showbits
[bit
] = '.' + bits
[bit
];
302 showbits
[bit
+1]='\0';
303 PrintAndLog("Partial UID=%s", showbits
);
306 for (bit
= 0; bit
< uidlen
; bit
++) {
307 bits
[bit
] = rawbits
[i
++];
308 showbits
[bit
] = '0' + bits
[bit
];
314 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
319 for( idx
=0; idx
<64; idx
++) {
320 if (showbits
[idx
] == '0') {
321 uid1
=(uid1
<<1)|(uid2
>>31);
324 uid1
=(uid1
<<1)|(uid2
>>31);
328 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
331 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
333 for( idx
=0; idx
<224; idx
++) {
334 uid1
=(uid1
<<1)|(uid2
>>31);
335 uid2
=(uid2
<<1)|(uid3
>>31);
336 uid3
=(uid3
<<1)|(uid4
>>31);
337 uid4
=(uid4
<<1)|(uid5
>>31);
338 uid5
=(uid5
<<1)|(uid6
>>31);
339 uid6
=(uid6
<<1)|(uid7
>>31);
341 if (showbits
[idx
] == '0')
342 uid7
= (uid7
<<1) | 0;
344 uid7
= (uid7
<<1) | 1;
346 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
349 // Checking UID against next occurrences
351 for (; i
+ uidlen
<= rawbit
;) {
353 for (bit
= 0; bit
< uidlen
; bit
++) {
354 if (bits
[bit
] != rawbits
[i
++]) {
365 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
367 // Remodulating for tag cloning
368 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
369 // since this changes graphbuffer data.
370 GraphTraceLen
= 32*uidlen
;
373 for (bit
= 0; bit
< uidlen
; bit
++) {
374 if (bits
[bit
] == 0) {
380 for (j
= 0; j
< 32; j
++) {
381 GraphBuffer
[i
++] = phase
;
386 RepaintGraphWindow();
390 int CmdIndalaClone(const char *Cmd
)
393 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
395 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
398 if (strchr(Cmd
,'l') != 0) {
399 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
400 uid1
= (uid1
<< 4) | (uid2
>> 28);
401 uid2
= (uid2
<< 4) | (uid3
>> 28);
402 uid3
= (uid3
<< 4) | (uid4
>> 28);
403 uid4
= (uid4
<< 4) | (uid5
>> 28);
404 uid5
= (uid5
<< 4) | (uid6
>> 28);
405 uid6
= (uid6
<< 4) | (uid7
>> 28);
406 uid7
= (uid7
<< 4) | (n
& 0xf);
408 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
409 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
410 c
.d
.asDwords
[0] = uid1
;
411 c
.d
.asDwords
[1] = uid2
;
412 c
.d
.asDwords
[2] = uid3
;
413 c
.d
.asDwords
[3] = uid4
;
414 c
.d
.asDwords
[4] = uid5
;
415 c
.d
.asDwords
[5] = uid6
;
416 c
.d
.asDwords
[6] = uid7
;
418 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
419 uid1
= (uid1
<< 4) | (uid2
>> 28);
420 uid2
= (uid2
<< 4) | (n
& 0xf);
422 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
423 c
.cmd
= CMD_INDALA_CLONE_TAG
;
428 clearCommandBuffer();
435 PrintAndLog("Usage: lf read");
436 PrintAndLog("Options: ");
437 PrintAndLog(" h This help");
438 PrintAndLog(" s silent run no printout");
439 PrintAndLog("This function takes no arguments. ");
440 PrintAndLog("Use 'lf config' to set parameters.");
445 PrintAndLog("Usage: lf snoop");
446 PrintAndLog("Options: ");
447 PrintAndLog(" h This help");
448 PrintAndLog("This function takes no arguments. ");
449 PrintAndLog("Use 'lf config' to set parameters.");
453 int usage_lf_config()
455 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
456 PrintAndLog("Options: ");
457 PrintAndLog(" h This help");
458 PrintAndLog(" L Low frequency (125 KHz)");
459 PrintAndLog(" H High frequency (134 KHz)");
460 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
461 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
462 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
463 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
464 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
465 PrintAndLog("Examples:");
466 PrintAndLog(" lf config b 8 L");
467 PrintAndLog(" Samples at 125KHz, 8bps.");
468 PrintAndLog(" lf config H b 4 d 3");
469 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
470 PrintAndLog(" a resolution of 4 bits per sample.");
471 PrintAndLog(" lf read");
472 PrintAndLog(" Performs a read (active field)");
473 PrintAndLog(" lf snoop");
474 PrintAndLog(" Performs a snoop (no active field)");
478 int CmdLFSetConfig(const char *Cmd
)
481 uint8_t divisor
= 0;//Frequency divisor
482 uint8_t bps
= 0; // Bits per sample
483 uint8_t decimation
= 0; //How many to keep
484 bool averaging
= 1; // Defaults to true
486 int trigger_threshold
=-1;//Means no change
487 uint8_t unsigned_trigg
= 0;
490 while(param_getchar(Cmd
, cmdp
) != 0x00)
492 switch(param_getchar(Cmd
, cmdp
))
495 return usage_lf_config();
505 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
509 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
511 if(!errors
) trigger_threshold
= unsigned_trigg
;
514 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
518 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
522 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
526 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
534 errors
= 1;// No args
540 return usage_lf_config();
542 //Bps is limited to 8, so fits in lower half of arg1
543 if(bps
>> 4) bps
= 8;
545 sample_config config
= {
546 decimation
,bps
,averaging
,divisor
,trigger_threshold
548 //Averaging is a flag on high-bit of arg[1]
549 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
550 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
551 clearCommandBuffer();
556 int CmdLFRead(const char *Cmd
)
561 if (param_getchar(Cmd
, cmdp
) == 'h')
563 return usage_lf_read();
565 if (param_getchar(Cmd
, cmdp
) == 's') arg1
= true; //suppress print
566 //And ship it to device
567 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}};
568 clearCommandBuffer();
570 //WaitForResponse(CMD_ACK,NULL);
571 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
572 PrintAndLog("command execution time out");
579 int CmdLFSnoop(const char *Cmd
)
582 if(param_getchar(Cmd
, cmdp
) == 'h')
584 return usage_lf_snoop();
587 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
588 clearCommandBuffer();
590 WaitForResponse(CMD_ACK
,NULL
);
594 static void ChkBitstream(const char *str
)
598 /* convert to bitstream if necessary */
599 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
600 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
606 //Attempt to simulate any wave in buffer (one bit per output sample)
607 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
608 int CmdLFSim(const char *Cmd
)
613 sscanf(Cmd
, "%i", &gap
);
615 // convert to bitstream if necessary
619 //can send only 512 bits at a time (1 byte sent per bit...)
620 printf("Sending [%d bytes]", GraphTraceLen
);
621 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
622 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
624 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
625 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
628 WaitForResponse(CMD_ACK
,NULL
);
633 PrintAndLog("Starting to simulate");
634 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
635 clearCommandBuffer();
640 int usage_lf_simfsk(void)
643 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
644 PrintAndLog("Options: ");
645 PrintAndLog(" h This help");
646 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
647 PrintAndLog(" i invert data");
648 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
649 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
650 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
651 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
652 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
656 int usage_lf_simask(void)
659 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
660 PrintAndLog("Options: ");
661 PrintAndLog(" h This help");
662 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
663 PrintAndLog(" i invert data");
664 PrintAndLog(" b sim ask/biphase");
665 PrintAndLog(" m sim ask/manchester - Default");
666 PrintAndLog(" r sim ask/raw");
667 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
668 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
672 int usage_lf_simpsk(void)
675 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
676 PrintAndLog("Options: ");
677 PrintAndLog(" h This help");
678 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
679 PrintAndLog(" i invert data");
680 PrintAndLog(" 1 set PSK1 (default)");
681 PrintAndLog(" 2 set PSK2");
682 PrintAndLog(" 3 set PSK3");
683 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
684 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
688 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
689 // - allow pull data from DemodBuffer
690 int CmdLFfskSim(const char *Cmd
)
692 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
693 // otherwise will need FChigh, FClow, Clock, and bitstream
694 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
697 char hexData
[32] = {0x00}; // store entered hex data
698 uint8_t data
[255] = {0x00};
701 while(param_getchar(Cmd
, cmdp
) != 0x00)
703 switch(param_getchar(Cmd
, cmdp
))
706 return usage_lf_simfsk();
712 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
716 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
720 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
728 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
732 dataLen
= hextobinarray((char *)data
, hexData
);
734 if (dataLen
==0) errors
=TRUE
;
735 if (errors
) PrintAndLog ("Error getting hex data");
739 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
745 if(cmdp
== 0 && DemodBufferLen
== 0)
747 errors
= TRUE
;// No args
753 return usage_lf_simfsk();
756 if (dataLen
== 0){ //using DemodBuffer
757 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
758 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0);
760 if (!fcHigh
) fcHigh
=10;
766 setDemodBuf(data
, dataLen
, 0);
769 //default if not found
770 if (clk
== 0) clk
= 50;
771 if (fcHigh
== 0) fcHigh
= 10;
772 if (fcLow
== 0) fcLow
= 8;
775 arg1
= fcHigh
<< 8 | fcLow
;
776 arg2
= invert
<< 8 | clk
;
777 size_t size
= DemodBufferLen
;
778 if (size
> USB_CMD_DATA_SIZE
) {
779 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
780 size
= USB_CMD_DATA_SIZE
;
782 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
784 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
785 clearCommandBuffer();
790 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
791 // - allow pull data from DemodBuffer
792 int CmdLFaskSim(const char *Cmd
)
794 //autodetect clock from Graphbuffer if using demod buffer
795 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
796 uint8_t encoding
= 1, separator
= 0;
797 uint8_t clk
=0, invert
=0;
799 char hexData
[32] = {0x00};
800 uint8_t data
[255]= {0x00}; // store entered hex data
803 while(param_getchar(Cmd
, cmdp
) != 0x00)
805 switch(param_getchar(Cmd
, cmdp
))
808 return usage_lf_simask();
814 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
818 encoding
=2; //biphase
834 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
838 dataLen
= hextobinarray((char *)data
, hexData
);
840 if (dataLen
==0) errors
=TRUE
;
841 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
845 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
851 if(cmdp
== 0 && DemodBufferLen
== 0)
853 errors
= TRUE
;// No args
859 return usage_lf_simask();
861 if (dataLen
== 0){ //using DemodBuffer
862 if (clk
== 0) clk
= GetAskClock("0", false, false);
864 setDemodBuf(data
, dataLen
, 0);
866 if (clk
== 0) clk
= 64;
867 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
869 size_t size
=DemodBufferLen
;
870 arg1
= clk
<< 8 | encoding
;
871 arg2
= invert
<< 8 | separator
;
872 if (size
> USB_CMD_DATA_SIZE
) {
873 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
874 size
= USB_CMD_DATA_SIZE
;
876 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
877 PrintAndLog("preparing to sim ask data: %d bits", size
);
878 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
879 clearCommandBuffer();
884 // by marshmellow - sim psk data given carrier, clock, invert
885 // - allow pull data from DemodBuffer or parameters
886 int CmdLFpskSim(const char *Cmd
)
888 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
889 //will need carrier, Clock, and bitstream
890 uint8_t carrier
=0, clk
=0;
893 char hexData
[32] = {0x00}; // store entered hex data
894 uint8_t data
[255] = {0x00};
898 while(param_getchar(Cmd
, cmdp
) != 0x00)
900 switch(param_getchar(Cmd
, cmdp
))
903 return usage_lf_simpsk();
909 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
913 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
929 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
933 dataLen
= hextobinarray((char *)data
, hexData
);
935 if (dataLen
==0) errors
=TRUE
;
936 if (errors
) PrintAndLog ("Error getting hex data");
940 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
946 if (cmdp
== 0 && DemodBufferLen
== 0)
948 errors
= TRUE
;// No args
954 return usage_lf_simpsk();
956 if (dataLen
== 0){ //using DemodBuffer
957 PrintAndLog("Getting Clocks");
958 if (clk
==0) clk
= GetPskClock("", FALSE
, FALSE
);
959 PrintAndLog("clk: %d",clk
);
960 if (!carrier
) carrier
= GetPskCarrier("", FALSE
, FALSE
);
961 PrintAndLog("carrier: %d", carrier
);
963 setDemodBuf(data
, dataLen
, 0);
966 if (clk
<= 0) clk
= 32;
967 if (carrier
== 0) carrier
= 2;
970 //need to convert psk2 to psk1 data before sim
971 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
973 PrintAndLog("Sorry, PSK3 not yet available");
977 arg1
= clk
<< 8 | carrier
;
979 size_t size
=DemodBufferLen
;
980 if (size
> USB_CMD_DATA_SIZE
) {
981 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
982 size
=USB_CMD_DATA_SIZE
;
984 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
985 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
986 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
987 clearCommandBuffer();
993 int CmdLFSimBidir(const char *Cmd
)
995 // Set ADC to twice the carrier for a slight supersampling
996 // HACK: not implemented in ARMSRC.
997 PrintAndLog("Not implemented yet.");
998 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
1003 int CmdVchDemod(const char *Cmd
)
1005 // Is this the entire sync pattern, or does this also include some
1006 // data bits that happen to be the same everywhere? That would be
1008 static const int SyncPattern
[] = {
1009 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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,
1021 // So first, we correlate for the sync pattern, and mark that.
1022 int bestCorrel
= 0, bestPos
= 0;
1024 // It does us no good to find the sync pattern, with fewer than
1025 // 2048 samples after it...
1026 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
1029 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
1030 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
1032 if (sum
> bestCorrel
) {
1037 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
1042 int worst
= INT_MAX
;
1045 for (i
= 0; i
< 2048; i
+= 8) {
1048 for (j
= 0; j
< 8; j
++) {
1049 sum
+= GraphBuffer
[bestPos
+i
+j
];
1056 if(abs(sum
) < worst
) {
1061 PrintAndLog("bits:");
1062 PrintAndLog("%s", bits
);
1063 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
1065 if (strcmp(Cmd
, "clone")==0) {
1068 for(s
= bits
; *s
; s
++) {
1070 for(j
= 0; j
< 16; j
++) {
1071 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
1074 RepaintGraphWindow();
1080 int CmdLFfind(const char *Cmd
)
1083 char cmdp
= param_getchar(Cmd
, 0);
1084 char testRaw
= param_getchar(Cmd
, 1);
1085 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') {
1086 PrintAndLog("Usage: lf search <0|1> [u]");
1087 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
1088 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
1090 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
1091 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
1092 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
1093 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
1098 if (!offline
&& (cmdp
!= '1')){
1100 getSamples("30000",false);
1101 } else if (GraphTraceLen
< 1000) {
1102 PrintAndLog("Data in Graphbuffer was too small.");
1105 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
1107 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1108 PrintAndLog("False Positives ARE possible\n");
1109 PrintAndLog("\nChecking for known tags:\n");
1111 ans
=CmdFSKdemodIO("");
1113 PrintAndLog("\nValid IO Prox ID Found!");
1117 ans
=CmdFSKdemodPyramid("");
1119 PrintAndLog("\nValid Pyramid ID Found!");
1123 ans
=CmdFSKdemodParadox("");
1125 PrintAndLog("\nValid Paradox ID Found!");
1129 ans
=CmdFSKdemodAWID("");
1131 PrintAndLog("\nValid AWID ID Found!");
1135 ans
=CmdFSKdemodHID("");
1137 PrintAndLog("\nValid HID Prox ID Found!");
1141 ans
=CmdAskEM410xDemod("");
1143 PrintAndLog("\nValid EM410x ID Found!");
1147 ans
=CmdG_Prox_II_Demod("");
1149 PrintAndLog("\nValid G Prox II ID Found!");
1153 ans
=CmdFDXBdemodBI("");
1155 PrintAndLog("\nValid FDX-B ID Found!");
1159 ans
=EM4x50Read("", false);
1161 PrintAndLog("\nValid EM4x50 ID Found!");
1165 ans
=CmdVikingDemod("");
1167 PrintAndLog("\nValid Viking ID Found!");
1171 ans
=CmdIndalaDecode("");
1173 PrintAndLog("\nValid Indala ID Found!");
1177 ans
=CmdPSKNexWatch("");
1179 PrintAndLog("\nValid NexWatch ID Found!");
1183 PrintAndLog("\nNo Known Tags Found!\n");
1184 if (testRaw
=='u' || testRaw
=='U'){
1185 //test unknown tag formats (raw mode)
1186 PrintAndLog("\nChecking for Unknown tags:\n");
1187 ans
=AutoCorrelate(4000, FALSE
, FALSE
);
1188 if (ans
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1189 ans
=GetFskClock("",FALSE
,FALSE
);
1190 if (ans
!= 0){ //fsk
1191 ans
=FSKrawDemod("",TRUE
);
1193 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1198 ans
=ASKDemod_ext("0 0 0",TRUE
,FALSE
,1,&st
);
1200 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1201 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1204 ans
=CmdPSK1rawDemod("");
1206 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1207 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1208 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1211 PrintAndLog("\nNo Data Found!\n");
1216 static command_t CommandTable
[] =
1218 {"help", CmdHelp
, 1, "This help"},
1219 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1220 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
1221 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1222 {"hitag", CmdLFHitag
, 1, "{ Hitag tags and transponders... }"},
1223 {"io", CmdLFIO
, 1, "{ ioProx tags... }"},
1224 {"presco", CmdLFPresco
, 1, "{ Presco RFIDs... }"},
1225 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 RFIDs... }"},
1226 {"pyramid", CmdLFPyramid
, 1, "{ Farpointe/Pyramid RFIDs... }"},
1227 {"t55xx", CmdLFT55XX
, 1, "{ T55xx RFIDs... }"},
1228 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
1229 {"viking", CmdLFViking
, 1, "{ Viking tags... }"},
1230 {"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)"},
1231 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1232 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1233 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1234 {"indalaclone", CmdIndalaClone
, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1235 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1236 {"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"},
1237 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1238 {"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"},
1239 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1240 {"simpsk", CmdLFpskSim
, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1241 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1242 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1243 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1244 {NULL
, NULL
, 0, NULL
}
1247 int CmdLF(const char *Cmd
)
1249 CmdsParse(CommandTable
, Cmd
);
1253 int CmdHelp(const char *Cmd
)
1255 CmdsHelp(CommandTable
);