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"
19 #include "cmdparser.h"
25 #include "cmdlfawid.h"
27 #include "cmdlfem4x.h"
28 #include "cmdlfhitag.h"
29 #include "cmdlft55xx.h"
30 #include "cmdlfpcf7931.h"
33 #include "cmdlfviking.h"
34 #include "cmdlfpresco.h"
35 #include "cmdlfpyramid.h"
36 static int CmdHelp(const char *Cmd
);
38 int usage_lf_cmdread(void) {
39 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H]");
40 PrintAndLog("Options: ");
41 PrintAndLog(" h This help");
42 PrintAndLog(" L Low frequency (125 KHz)");
43 PrintAndLog(" H High frequency (134 KHz)");
44 PrintAndLog(" d <delay> delay OFF period, (dec)");
45 PrintAndLog(" z <zero> time period ZERO, (dec)");
46 PrintAndLog(" o <one> time period ONE, (dec)");
47 PrintAndLog(" c <cmd> Command bytes");
48 PrintAndLog(" ************* All periods in microseconds (ms)");
49 PrintAndLog("Examples:");
50 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
51 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
54 int usage_lf_read(void){
55 PrintAndLog("Usage: lf read [h] [s]");
56 PrintAndLog("Options: ");
57 PrintAndLog(" h This help");
58 PrintAndLog(" s silent run no printout");
59 PrintAndLog("This function takes no arguments. ");
60 PrintAndLog("Use 'lf config' to set parameters.");
63 int usage_lf_snoop(void) {
64 PrintAndLog("Usage: lf snoop");
65 PrintAndLog("Options: ");
66 PrintAndLog(" h This help");
67 PrintAndLog("This function takes no arguments. ");
68 PrintAndLog("Use 'lf config' to set parameters.");
71 int usage_lf_config(void) {
72 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
73 PrintAndLog("Options: ");
74 PrintAndLog(" h This help");
75 PrintAndLog(" L Low frequency (125 KHz)");
76 PrintAndLog(" H High frequency (134 KHz)");
77 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
78 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
79 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
80 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
81 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
82 PrintAndLog("Examples:");
83 PrintAndLog(" lf config b 8 L");
84 PrintAndLog(" Samples at 125KHz, 8bps.");
85 PrintAndLog(" lf config H b 4 d 3");
86 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
87 PrintAndLog(" a resolution of 4 bits per sample.");
88 PrintAndLog(" lf read");
89 PrintAndLog(" Performs a read (active field)");
90 PrintAndLog(" lf snoop");
91 PrintAndLog(" Performs a snoop (no active field)");
94 int usage_lf_simfsk(void) {
95 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
96 PrintAndLog("Options: ");
97 PrintAndLog(" h This help");
98 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
99 PrintAndLog(" i invert data");
100 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
101 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
102 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
103 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
104 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
107 int usage_lf_simask(void) {
108 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
109 PrintAndLog("Options: ");
110 PrintAndLog(" h This help");
111 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
112 PrintAndLog(" i invert data");
113 PrintAndLog(" b sim ask/biphase");
114 PrintAndLog(" m sim ask/manchester - Default");
115 PrintAndLog(" r sim ask/raw");
116 PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
117 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
120 int usage_lf_simpsk(void) {
121 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
122 PrintAndLog("Options: ");
123 PrintAndLog(" h This help");
124 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
125 PrintAndLog(" i invert data");
126 PrintAndLog(" 1 set PSK1 (default)");
127 PrintAndLog(" 2 set PSK2");
128 PrintAndLog(" 3 set PSK3");
129 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
130 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
133 int usage_lf_find(void){
134 PrintAndLog("Usage: lf search <0|1> [u]");
135 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
136 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
138 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
139 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
140 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
141 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
146 /* send a LF command before reading */
147 int CmdLFCommandRead(const char *Cmd
)
149 static char dummy
[3] = {0x20,0x00,0x00};
150 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
156 while(param_getchar(Cmd
, cmdp
) != 0x00) {
157 switch(param_getchar(Cmd
, cmdp
))
160 return usage_lf_cmdread();
169 strLength
= param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
);
173 c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
177 c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
181 c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
185 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
192 if (cmdp
== 0) errors
= 1;
195 if (errors
) return usage_lf_cmdread();
197 // in case they specified 'H'
198 strcpy((char *)&c
.d
.asBytes
+ strLength
, dummy
);
200 clearCommandBuffer();
205 int CmdFlexdemod(const char *Cmd
)
208 for (i
= 0; i
< GraphTraceLen
; ++i
) {
209 if (GraphBuffer
[i
] < 0) {
216 #define LONG_WAIT 100
218 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
219 int first
= GraphBuffer
[start
];
220 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
221 if (GraphBuffer
[i
] != first
) {
225 if (i
== (start
+ LONG_WAIT
)) {
229 if (start
== GraphTraceLen
- LONG_WAIT
) {
230 PrintAndLog("nothing to wait for");
234 GraphBuffer
[start
] = 2;
235 GraphBuffer
[start
+1] = -2;
236 uint8_t bits
[64] = {0x00};
240 for (bit
= 0; bit
< 64; bit
++) {
242 for (int j
= 0; j
< 16; j
++) {
243 sum
+= GraphBuffer
[i
++];
246 bits
[bit
] = (sum
> 0) ? 1 : 0;
248 PrintAndLog("bit %d sum %d", bit
, sum
);
251 for (bit
= 0; bit
< 64; bit
++) {
254 for (j
= 0; j
< 16; j
++) {
255 sum
+= GraphBuffer
[i
++];
257 if (sum
> 0 && bits
[bit
] != 1) {
258 PrintAndLog("oops1 at %d", bit
);
260 if (sum
< 0 && bits
[bit
] != 0) {
261 PrintAndLog("oops2 at %d", bit
);
265 // HACK writing back to graphbuffer.
266 GraphTraceLen
= 32*64;
269 for (bit
= 0; bit
< 64; bit
++) {
271 phase
= (bits
[bit
] == 0) ? 0 : 1;
274 for (j
= 0; j
< 32; j
++) {
275 GraphBuffer
[i
++] = phase
;
280 RepaintGraphWindow();
284 int CmdIndalaDemod(const char *Cmd
)
286 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
292 // worst case with GraphTraceLen=64000 is < 4096
293 // under normal conditions it's < 2048
295 uint8_t rawbits
[4096];
297 int worst
= 0, worstPos
= 0;
298 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
300 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
301 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
303 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
304 // appears redundant - marshmellow
306 for (j
= 0; j
< count
- 8; j
+= 16) {
307 rawbits
[rawbit
++] = 0;
309 if ((abs(count
- j
)) > worst
) {
310 worst
= abs(count
- j
);
316 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
319 for (j
= 0; j
< count
- 8; j
+= 16) {
320 rawbits
[rawbit
++] = 1;
322 if ((abs(count
- j
)) > worst
) {
323 worst
= abs(count
- j
);
333 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32);
334 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
339 // Finding the start of a UID
340 int uidlen
, long_wait
;
341 if (strcmp(Cmd
, "224") == 0) {
351 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
352 first
= rawbits
[start
];
353 for (i
= start
; i
< start
+ long_wait
; i
++) {
354 if (rawbits
[i
] != first
) {
358 if (i
== (start
+ long_wait
)) {
363 if (start
== rawbit
- uidlen
+ 1) {
364 PrintAndLog("nothing to wait for");
368 // Inverting signal if needed
370 for (i
= start
; i
< rawbit
; i
++) {
371 rawbits
[i
] = !rawbits
[i
];
376 uint8_t bits
[224] = {0x00};
377 char showbits
[225] = {0x00};
382 if (uidlen
> rawbit
) {
383 PrintAndLog("Warning: not enough raw bits to get a full UID");
384 for (bit
= 0; bit
< rawbit
; bit
++) {
385 bits
[bit
] = rawbits
[i
++];
386 // As we cannot know the parity, let's use "." and "/"
387 showbits
[bit
] = '.' + bits
[bit
];
389 showbits
[bit
+1]='\0';
390 PrintAndLog("Partial UID=%s", showbits
);
393 for (bit
= 0; bit
< uidlen
; bit
++) {
394 bits
[bit
] = rawbits
[i
++];
395 showbits
[bit
] = '0' + bits
[bit
];
401 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
406 for( idx
=0; idx
<64; idx
++) {
407 if (showbits
[idx
] == '0') {
408 uid1
= (uid1
<<1) | (uid2
>>31);
409 uid2
= (uid2
<<1) | 0;
411 uid1
= (uid1
<<1) | (uid2
>>31);
412 uid2
= (uid2
<<1) | 1;
415 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
417 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
419 for( idx
=0; idx
<224; idx
++) {
420 uid1
= (uid1
<<1) | (uid2
>>31);
421 uid2
= (uid2
<<1) | (uid3
>>31);
422 uid3
= (uid3
<<1) | (uid4
>>31);
423 uid4
= (uid4
<<1) | (uid5
>>31);
424 uid5
= (uid5
<<1) | (uid6
>>31);
425 uid6
= (uid6
<<1) | (uid7
>>31);
427 if (showbits
[idx
] == '0')
428 uid7
= (uid7
<<1) | 0;
430 uid7
= (uid7
<<1) | 1;
432 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
435 // Checking UID against next occurrences
437 for (; i
+ uidlen
<= rawbit
;) {
439 for (bit
= 0; bit
< uidlen
; bit
++) {
440 if (bits
[bit
] != rawbits
[i
++]) {
451 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
453 // Remodulating for tag cloning
454 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
455 // since this changes graphbuffer data.
456 GraphTraceLen
= 32 * uidlen
;
459 for (bit
= 0; bit
< uidlen
; bit
++) {
460 phase
= (bits
[bit
] == 0) ? 0 : 1;
462 for (j
= 0; j
< 32; j
++) {
463 GraphBuffer
[i
++] = phase
;
468 RepaintGraphWindow();
472 int CmdIndalaClone(const char *Cmd
)
475 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
477 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
480 if (strchr(Cmd
,'l') != 0) {
481 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
482 uid1
= (uid1
<< 4) | (uid2
>> 28);
483 uid2
= (uid2
<< 4) | (uid3
>> 28);
484 uid3
= (uid3
<< 4) | (uid4
>> 28);
485 uid4
= (uid4
<< 4) | (uid5
>> 28);
486 uid5
= (uid5
<< 4) | (uid6
>> 28);
487 uid6
= (uid6
<< 4) | (uid7
>> 28);
488 uid7
= (uid7
<< 4) | (n
& 0xf);
490 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
491 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
492 c
.d
.asDwords
[0] = uid1
;
493 c
.d
.asDwords
[1] = uid2
;
494 c
.d
.asDwords
[2] = uid3
;
495 c
.d
.asDwords
[3] = uid4
;
496 c
.d
.asDwords
[4] = uid5
;
497 c
.d
.asDwords
[5] = uid6
;
498 c
.d
.asDwords
[6] = uid7
;
500 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
501 uid1
= (uid1
<< 4) | (uid2
>> 28);
502 uid2
= (uid2
<< 4) | (n
& 0xf);
504 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
505 c
.cmd
= CMD_INDALA_CLONE_TAG
;
510 clearCommandBuffer();
515 int CmdLFSetConfig(const char *Cmd
)
517 uint8_t divisor
= 0;//Frequency divisor
518 uint8_t bps
= 0; // Bits per sample
519 uint8_t decimation
= 0; //How many to keep
520 bool averaging
= 1; // Defaults to true
522 int trigger_threshold
= -1;//Means no change
523 uint8_t unsigned_trigg
= 0;
526 while(param_getchar(Cmd
, cmdp
) != 0x00)
528 switch(param_getchar(Cmd
, cmdp
))
531 return usage_lf_config();
541 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
545 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
547 if(!errors
) trigger_threshold
= unsigned_trigg
;
550 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
554 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
558 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
562 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
570 if (cmdp
== 0) errors
= 1;
573 if (errors
) return usage_lf_config();
575 //Bps is limited to 8, so fits in lower half of arg1
576 if (bps
>> 4) bps
= 8;
578 sample_config config
= { decimation
, bps
, averaging
, divisor
, trigger_threshold
};
580 //Averaging is a flag on high-bit of arg[1]
581 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
582 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
583 clearCommandBuffer();
588 int CmdLFRead(const char *Cmd
)
591 uint8_t cmdp
= param_getchar(Cmd
, 0);
593 if ( cmdp
== 'h' || cmdp
== 'H') return usage_lf_read();
596 if ( cmdp
== 's' || cmdp
== 'S') arg1
= true;
598 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}};
599 clearCommandBuffer();
601 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
602 PrintAndLog("command execution time out");
608 int CmdLFSnoop(const char *Cmd
)
610 uint8_t cmdp
= param_getchar(Cmd
, 0);
611 if(cmdp
== 'h' || cmdp
== 'H') return usage_lf_snoop();
613 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
614 clearCommandBuffer();
616 WaitForResponse(CMD_ACK
,NULL
);
620 static void ChkBitstream(const char *str
)
624 /* convert to bitstream if necessary */
625 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
626 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
632 //Attempt to simulate any wave in buffer (one bit per output sample)
633 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
634 int CmdLFSim(const char *Cmd
)
639 sscanf(Cmd
, "%i", &gap
);
641 // convert to bitstream if necessary
644 //can send only 512 bits at a time (1 byte sent per bit...)
645 printf("Sending [%d bytes]", GraphTraceLen
);
646 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
647 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
649 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
650 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
652 clearCommandBuffer();
654 WaitForResponse(CMD_ACK
,NULL
);
658 PrintAndLog("\nStarting to simulate");
659 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
660 clearCommandBuffer();
665 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
666 // - allow pull data from DemodBuffer
667 int CmdLFfskSim(const char *Cmd
)
669 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
670 // otherwise will need FChigh, FClow, Clock, and bitstream
671 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
674 char hexData
[32] = {0x00}; // store entered hex data
675 uint8_t data
[255] = {0x00};
678 while(param_getchar(Cmd
, cmdp
) != 0x00)
680 switch(param_getchar(Cmd
, cmdp
))
683 return usage_lf_simfsk();
689 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
693 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
697 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
705 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
709 dataLen
= hextobinarray((char *)data
, hexData
);
711 if (dataLen
==0) errors
=TRUE
;
712 if (errors
) PrintAndLog ("Error getting hex data");
716 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
722 if(cmdp
== 0 && DemodBufferLen
== 0)
724 errors
= TRUE
;// No args
730 return usage_lf_simfsk();
733 if (dataLen
== 0){ //using DemodBuffer
734 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
735 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0);
737 if (!fcHigh
) fcHigh
=10;
743 setDemodBuf(data
, dataLen
, 0);
746 //default if not found
747 if (clk
== 0) clk
= 50;
748 if (fcHigh
== 0) fcHigh
= 10;
749 if (fcLow
== 0) fcLow
= 8;
752 arg1
= fcHigh
<< 8 | fcLow
;
753 arg2
= invert
<< 8 | clk
;
754 size_t size
= DemodBufferLen
;
755 if (size
> USB_CMD_DATA_SIZE
) {
756 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
757 size
= USB_CMD_DATA_SIZE
;
759 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
761 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
762 clearCommandBuffer();
767 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
768 // - allow pull data from DemodBuffer
769 int CmdLFaskSim(const char *Cmd
)
771 //autodetect clock from Graphbuffer if using demod buffer
772 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
773 uint8_t encoding
= 1, separator
= 0;
774 uint8_t clk
=0, invert
=0;
776 char hexData
[32] = {0x00};
777 uint8_t data
[255]= {0x00}; // store entered hex data
780 while(param_getchar(Cmd
, cmdp
) != 0x00)
782 switch(param_getchar(Cmd
, cmdp
))
785 return usage_lf_simask();
791 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
795 encoding
=2; //biphase
811 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
815 dataLen
= hextobinarray((char *)data
, hexData
);
817 if (dataLen
==0) errors
=TRUE
;
818 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
822 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
828 if(cmdp
== 0 && DemodBufferLen
== 0)
830 errors
= TRUE
;// No args
836 return usage_lf_simask();
838 if (dataLen
== 0){ //using DemodBuffer
839 if (clk
== 0) clk
= GetAskClock("0", false, false);
841 setDemodBuf(data
, dataLen
, 0);
843 if (clk
== 0) clk
= 64;
844 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
846 size_t size
=DemodBufferLen
;
847 arg1
= clk
<< 8 | encoding
;
848 arg2
= invert
<< 8 | separator
;
849 if (size
> USB_CMD_DATA_SIZE
) {
850 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
851 size
= USB_CMD_DATA_SIZE
;
853 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
854 PrintAndLog("preparing to sim ask data: %d bits", size
);
855 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
856 clearCommandBuffer();
861 // by marshmellow - sim psk data given carrier, clock, invert
862 // - allow pull data from DemodBuffer or parameters
863 int CmdLFpskSim(const char *Cmd
)
865 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
866 //will need carrier, Clock, and bitstream
867 uint8_t carrier
=0, clk
=0;
870 char hexData
[32] = {0x00}; // store entered hex data
871 uint8_t data
[255] = {0x00};
875 while(param_getchar(Cmd
, cmdp
) != 0x00)
877 switch(param_getchar(Cmd
, cmdp
))
880 return usage_lf_simpsk();
886 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
890 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
906 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
910 dataLen
= hextobinarray((char *)data
, hexData
);
912 if (dataLen
==0) errors
=TRUE
;
913 if (errors
) PrintAndLog ("Error getting hex data");
917 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
923 if (cmdp
== 0 && DemodBufferLen
== 0)
925 errors
= TRUE
;// No args
931 return usage_lf_simpsk();
933 if (dataLen
== 0){ //using DemodBuffer
934 PrintAndLog("Getting Clocks");
935 if (clk
==0) clk
= GetPskClock("", FALSE
, FALSE
);
936 PrintAndLog("clk: %d",clk
);
937 if (!carrier
) carrier
= GetPskCarrier("", FALSE
, FALSE
);
938 PrintAndLog("carrier: %d", carrier
);
940 setDemodBuf(data
, dataLen
, 0);
943 if (clk
<= 0) clk
= 32;
944 if (carrier
== 0) carrier
= 2;
947 //need to convert psk2 to psk1 data before sim
948 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
950 PrintAndLog("Sorry, PSK3 not yet available");
954 arg1
= clk
<< 8 | carrier
;
956 size_t size
=DemodBufferLen
;
957 if (size
> USB_CMD_DATA_SIZE
) {
958 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
959 size
=USB_CMD_DATA_SIZE
;
961 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
962 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
963 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
964 clearCommandBuffer();
970 int CmdLFSimBidir(const char *Cmd
)
972 // Set ADC to twice the carrier for a slight supersampling
973 // HACK: not implemented in ARMSRC.
974 PrintAndLog("Not implemented yet.");
975 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
980 int CmdVchDemod(const char *Cmd
)
982 // Is this the entire sync pattern, or does this also include some
983 // data bits that happen to be the same everywhere? That would be
985 static const int SyncPattern
[] = {
986 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
987 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
988 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
989 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
990 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
991 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
992 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
993 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
994 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
995 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
998 // So first, we correlate for the sync pattern, and mark that.
999 int bestCorrel
= 0, bestPos
= 0;
1001 // It does us no good to find the sync pattern, with fewer than
1002 // 2048 samples after it...
1003 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
1006 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
1007 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
1009 if (sum
> bestCorrel
) {
1014 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
1019 int worst
= INT_MAX
;
1022 for (i
= 0; i
< 2048; i
+= 8) {
1025 for (j
= 0; j
< 8; j
++) {
1026 sum
+= GraphBuffer
[bestPos
+i
+j
];
1033 if(abs(sum
) < worst
) {
1038 PrintAndLog("bits:");
1039 PrintAndLog("%s", bits
);
1040 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
1042 if (strcmp(Cmd
, "clone")==0) {
1045 for(s
= bits
; *s
; s
++) {
1047 for(j
= 0; j
< 16; j
++) {
1048 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
1051 RepaintGraphWindow();
1057 int CmdLFfind(const char *Cmd
) {
1059 char cmdp
= param_getchar(Cmd
, 0);
1060 char testRaw
= param_getchar(Cmd
, 1);
1061 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_find();
1063 if (!offline
&& (cmdp
!= '1')){
1065 getSamples("30000",false);
1066 } else if (GraphTraceLen
< 1000) {
1067 PrintAndLog("Data in Graphbuffer was too small.");
1070 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
1072 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1073 PrintAndLog("False Positives ARE possible\n");
1074 PrintAndLog("\nChecking for known tags:\n");
1076 ans
=CmdFSKdemodIO("");
1078 PrintAndLog("\nValid IO Prox ID Found!");
1082 ans
=CmdFSKdemodPyramid("");
1084 PrintAndLog("\nValid Pyramid ID Found!");
1088 ans
=CmdFSKdemodParadox("");
1090 PrintAndLog("\nValid Paradox ID Found!");
1094 ans
=CmdFSKdemodAWID("");
1096 PrintAndLog("\nValid AWID ID Found!");
1100 ans
=CmdFSKdemodHID("");
1102 PrintAndLog("\nValid HID Prox ID Found!");
1106 ans
=CmdAskEM410xDemod("");
1108 PrintAndLog("\nValid EM410x ID Found!");
1112 ans
=CmdG_Prox_II_Demod("");
1114 PrintAndLog("\nValid Guardall G-Prox II ID Found!");
1118 ans
=CmdFDXBdemodBI("");
1120 PrintAndLog("\nValid FDX-B ID Found!");
1124 ans
=EM4x50Read("", false);
1126 PrintAndLog("\nValid EM4x50 ID Found!");
1130 ans
=CmdVikingDemod("");
1132 PrintAndLog("\nValid Viking ID Found!");
1136 ans
=CmdIndalaDecode("");
1138 PrintAndLog("\nValid Indala ID Found!");
1142 ans
=CmdPSKNexWatch("");
1144 PrintAndLog("\nValid NexWatch ID Found!");
1150 PrintAndLog("\nNo Known Tags Found!\n");
1151 if (testRaw
=='u' || testRaw
=='U'){
1152 //test unknown tag formats (raw mode)
1153 PrintAndLog("\nChecking for Unknown tags:\n");
1154 ans
=AutoCorrelate(4000, FALSE
, FALSE
);
1158 PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1160 if ( ans
% 8 == 0) {
1161 int bytes
= (ans
/ 8);
1162 PrintAndLog("Possible %d bytes", bytes
);
1164 if ( bytes
% 2 == 0) {
1165 blocks
= (bytes
/ 2);
1166 PrintAndLog("Possible 2 blocks, width %d", blocks
);
1168 if ( bytes
% 4 == 0) {
1169 blocks
= (bytes
/ 4);
1170 PrintAndLog("Possible 4 blocks, width %d", blocks
);
1172 if ( bytes
% 8 == 0) {
1173 blocks
= (bytes
/ 8);
1174 PrintAndLog("Possible 8 blocks, width %d", blocks
);
1176 if ( bytes
% 16 == 0) {
1177 blocks
= (bytes
/ 16);
1178 PrintAndLog("Possible 16 blocks, width %d", blocks
);
1183 ans
=GetFskClock("",FALSE
,FALSE
);
1184 if (ans
!= 0){ //fsk
1185 ans
=FSKrawDemod("",TRUE
);
1187 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1192 ans
=ASKDemod_ext("0 0 0",TRUE
,FALSE
,1,&st
);
1194 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1195 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1199 ans
=CmdPSK1rawDemod("");
1201 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1202 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1203 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1206 PrintAndLog("\nNo Data Found!\n");
1211 static command_t CommandTable
[] =
1213 {"help", CmdHelp
, 1, "This help"},
1214 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1215 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
1216 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1217 {"hitag", CmdLFHitag
, 1, "{ HITAG RFIDs... }"},
1218 {"io", CmdLFIO
, 1, "{ IOPROX RFIDs... }"},
1219 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 RFIDs... }"},
1220 {"presco", CmdLFPresco
, 1, "{ Presco RFIDs... }"},
1221 {"pyramid", CmdLFPyramid
, 1, "{ Farpointe/Pyramid RFIDs... }"},
1222 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
1223 {"t55xx", CmdLFT55XX
, 1, "{ T55xx RFIDs... }"},
1224 {"viking", CmdLFViking
, 1, "{ Viking RFIDs... }"},
1225 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1226 {"cmdread", CmdLFCommandRead
, 0, "<off period> <'0' period> <'1' period> <command> ['h' 134] \n\t\t-- Modulate LF reader field to send command before read (all periods in microseconds)"},
1227 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1228 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1229 {"indalaclone", CmdIndalaClone
, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1230 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1231 {"search", CmdLFfind
, 1, "[offline] ['u'] Read and Search for valid known tag (in offline mode it you can load first then search) \n\t\t-- 'u' to search for unknown tags"},
1232 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1233 {"simask", CmdLFaskSim
, 0, "[clock] [invert <1|0>] [biphase/manchester/raw <'b'|'m'|'r'>] [msg separator 's'] [d <hexdata>] \n\t\t-- Simulate LF ASK tag from demodbuffer or input"},
1234 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] \n\t\t-- Simulate LF FSK tag from demodbuffer or input"},
1235 {"simpsk", CmdLFpskSim
, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] \n\t\t-- Simulate LF PSK tag from demodbuffer or input"},
1236 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1237 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1238 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1239 {NULL
, NULL
, 0, NULL
}
1242 int CmdLF(const char *Cmd
) {
1243 clearCommandBuffer();
1244 CmdsParse(CommandTable
, Cmd
);
1248 int CmdHelp(const char *Cmd
) {
1249 CmdsHelp(CommandTable
);