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 static int CmdHelp(const char *Cmd
);
37 int usage_lf_cmdread(void) {
38 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H]");
39 PrintAndLog("Options: ");
40 PrintAndLog(" h This help");
41 PrintAndLog(" H Freqency High (134 KHz), default is 'Low (125KHz)'");
42 PrintAndLog(" d <delay> delay OFF period, (dec)");
43 PrintAndLog(" z <zero> time period ZERO, (dec)");
44 PrintAndLog(" o <one> time period ONE, (dec)");
45 PrintAndLog(" c <cmd> Command bytes");
46 PrintAndLog(" ************* All periods in microseconds (ms)");
47 PrintAndLog("Examples:");
48 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
49 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
52 int usage_lf_read(void){
53 PrintAndLog("Usage: lf read [h] [s]");
54 PrintAndLog("Options: ");
55 PrintAndLog(" h This help");
56 PrintAndLog(" s silent run no printout");
57 PrintAndLog("This function takes no arguments. ");
58 PrintAndLog("Use 'lf config' to set parameters.");
61 int usage_lf_snoop(void) {
62 PrintAndLog("Usage: lf snoop");
63 PrintAndLog("Options: ");
64 PrintAndLog(" h This help");
65 PrintAndLog("This function takes no arguments. ");
66 PrintAndLog("Use 'lf config' to set parameters.");
69 int usage_lf_config(void) {
70 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
71 PrintAndLog("Options: ");
72 PrintAndLog(" h This help");
73 PrintAndLog(" L Low frequency (125 KHz)");
74 PrintAndLog(" H High frequency (134 KHz)");
75 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
76 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
77 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
78 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
79 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
80 PrintAndLog("Examples:");
81 PrintAndLog(" lf config b 8 L");
82 PrintAndLog(" Samples at 125KHz, 8bps.");
83 PrintAndLog(" lf config H b 4 d 3");
84 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
85 PrintAndLog(" a resolution of 4 bits per sample.");
86 PrintAndLog(" lf read");
87 PrintAndLog(" Performs a read (active field)");
88 PrintAndLog(" lf snoop");
89 PrintAndLog(" Performs a snoop (no active field)");
92 int usage_lf_simfsk(void) {
93 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
94 PrintAndLog("Options: ");
95 PrintAndLog(" h This help");
96 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
97 PrintAndLog(" i invert data");
98 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
99 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
100 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
101 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
102 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
105 int usage_lf_simask(void) {
106 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
107 PrintAndLog("Options: ");
108 PrintAndLog(" h This help");
109 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
110 PrintAndLog(" i invert data");
111 PrintAndLog(" b sim ask/biphase");
112 PrintAndLog(" m sim ask/manchester - Default");
113 PrintAndLog(" r sim ask/raw");
114 PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
115 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
118 int usage_lf_simpsk(void) {
119 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
120 PrintAndLog("Options: ");
121 PrintAndLog(" h This help");
122 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
123 PrintAndLog(" i invert data");
124 PrintAndLog(" 1 set PSK1 (default)");
125 PrintAndLog(" 2 set PSK2");
126 PrintAndLog(" 3 set PSK3");
127 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
128 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
131 int usage_lf_find(void){
132 PrintAndLog("Usage: lf search <0|1> [u]");
133 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
134 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
136 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
137 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
138 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
139 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
144 /* send a LF command before reading */
145 int CmdLFCommandRead(const char *Cmd
)
147 static char dummy
[3] = {0x20,0x00,0x00};
148 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
154 while(param_getchar(Cmd
, cmdp
) != 0x00) {
155 switch(param_getchar(Cmd
, cmdp
))
158 return usage_lf_cmdread();
167 strLength
= param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
);
171 c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
175 c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
179 c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
183 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
190 if (cmdp
== 0) errors
= 1;
193 if (errors
) return usage_lf_cmdread();
195 // in case they specified 'H'
196 strcpy((char *)&c
.d
.asBytes
+ strLength
, dummy
);
198 clearCommandBuffer();
203 int CmdFlexdemod(const char *Cmd
)
206 for (i
= 0; i
< GraphTraceLen
; ++i
) {
207 if (GraphBuffer
[i
] < 0) {
214 #define LONG_WAIT 100
216 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
217 int first
= GraphBuffer
[start
];
218 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
219 if (GraphBuffer
[i
] != first
) {
223 if (i
== (start
+ LONG_WAIT
)) {
227 if (start
== GraphTraceLen
- LONG_WAIT
) {
228 PrintAndLog("nothing to wait for");
232 GraphBuffer
[start
] = 2;
233 GraphBuffer
[start
+1] = -2;
234 uint8_t bits
[64] = {0x00};
238 for (bit
= 0; bit
< 64; bit
++) {
240 for (int j
= 0; j
< 16; j
++) {
241 sum
+= GraphBuffer
[i
++];
244 bits
[bit
] = (sum
> 0) ? 1 : 0;
246 PrintAndLog("bit %d sum %d", bit
, sum
);
249 for (bit
= 0; bit
< 64; bit
++) {
252 for (j
= 0; j
< 16; j
++) {
253 sum
+= GraphBuffer
[i
++];
255 if (sum
> 0 && bits
[bit
] != 1) {
256 PrintAndLog("oops1 at %d", bit
);
258 if (sum
< 0 && bits
[bit
] != 0) {
259 PrintAndLog("oops2 at %d", bit
);
263 // HACK writing back to graphbuffer.
264 GraphTraceLen
= 32*64;
267 for (bit
= 0; bit
< 64; bit
++) {
269 phase
= (bits
[bit
] == 0) ? 0 : 1;
272 for (j
= 0; j
< 32; j
++) {
273 GraphBuffer
[i
++] = phase
;
278 RepaintGraphWindow();
282 int CmdIndalaDemod(const char *Cmd
)
284 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
290 // worst case with GraphTraceLen=64000 is < 4096
291 // under normal conditions it's < 2048
293 uint8_t rawbits
[4096];
295 int worst
= 0, worstPos
= 0;
296 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
298 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
299 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
301 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
302 // appears redundant - marshmellow
304 for (j
= 0; j
< count
- 8; j
+= 16) {
305 rawbits
[rawbit
++] = 0;
307 if ((abs(count
- j
)) > worst
) {
308 worst
= abs(count
- j
);
314 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
317 for (j
= 0; j
< count
- 8; j
+= 16) {
318 rawbits
[rawbit
++] = 1;
320 if ((abs(count
- j
)) > worst
) {
321 worst
= abs(count
- j
);
331 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32);
332 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
337 // Finding the start of a UID
338 int uidlen
, long_wait
;
339 if (strcmp(Cmd
, "224") == 0) {
349 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
350 first
= rawbits
[start
];
351 for (i
= start
; i
< start
+ long_wait
; i
++) {
352 if (rawbits
[i
] != first
) {
356 if (i
== (start
+ long_wait
)) {
361 if (start
== rawbit
- uidlen
+ 1) {
362 PrintAndLog("nothing to wait for");
366 // Inverting signal if needed
368 for (i
= start
; i
< rawbit
; i
++) {
369 rawbits
[i
] = !rawbits
[i
];
374 uint8_t bits
[224] = {0x00};
375 char showbits
[225] = {0x00};
380 if (uidlen
> rawbit
) {
381 PrintAndLog("Warning: not enough raw bits to get a full UID");
382 for (bit
= 0; bit
< rawbit
; bit
++) {
383 bits
[bit
] = rawbits
[i
++];
384 // As we cannot know the parity, let's use "." and "/"
385 showbits
[bit
] = '.' + bits
[bit
];
387 showbits
[bit
+1]='\0';
388 PrintAndLog("Partial UID=%s", showbits
);
391 for (bit
= 0; bit
< uidlen
; bit
++) {
392 bits
[bit
] = rawbits
[i
++];
393 showbits
[bit
] = '0' + bits
[bit
];
399 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
404 for( idx
=0; idx
<64; idx
++) {
405 if (showbits
[idx
] == '0') {
406 uid1
= (uid1
<<1) | (uid2
>>31);
407 uid2
= (uid2
<<1) | 0;
409 uid1
= (uid1
<<1) | (uid2
>>31);
410 uid2
= (uid2
<<1) | 1;
413 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
415 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
417 for( idx
=0; idx
<224; idx
++) {
418 uid1
= (uid1
<<1) | (uid2
>>31);
419 uid2
= (uid2
<<1) | (uid3
>>31);
420 uid3
= (uid3
<<1) | (uid4
>>31);
421 uid4
= (uid4
<<1) | (uid5
>>31);
422 uid5
= (uid5
<<1) | (uid6
>>31);
423 uid6
= (uid6
<<1) | (uid7
>>31);
425 if (showbits
[idx
] == '0')
426 uid7
= (uid7
<<1) | 0;
428 uid7
= (uid7
<<1) | 1;
430 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
433 // Checking UID against next occurrences
435 for (; i
+ uidlen
<= rawbit
;) {
437 for (bit
= 0; bit
< uidlen
; bit
++) {
438 if (bits
[bit
] != rawbits
[i
++]) {
449 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
451 // Remodulating for tag cloning
452 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
453 // since this changes graphbuffer data.
454 GraphTraceLen
= 32 * uidlen
;
457 for (bit
= 0; bit
< uidlen
; bit
++) {
458 phase
= (bits
[bit
] == 0) ? 0 : 1;
460 for (j
= 0; j
< 32; j
++) {
461 GraphBuffer
[i
++] = phase
;
466 RepaintGraphWindow();
470 int CmdIndalaClone(const char *Cmd
)
473 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
475 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
478 if (strchr(Cmd
,'l') != 0) {
479 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
480 uid1
= (uid1
<< 4) | (uid2
>> 28);
481 uid2
= (uid2
<< 4) | (uid3
>> 28);
482 uid3
= (uid3
<< 4) | (uid4
>> 28);
483 uid4
= (uid4
<< 4) | (uid5
>> 28);
484 uid5
= (uid5
<< 4) | (uid6
>> 28);
485 uid6
= (uid6
<< 4) | (uid7
>> 28);
486 uid7
= (uid7
<< 4) | (n
& 0xf);
488 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
489 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
490 c
.d
.asDwords
[0] = uid1
;
491 c
.d
.asDwords
[1] = uid2
;
492 c
.d
.asDwords
[2] = uid3
;
493 c
.d
.asDwords
[3] = uid4
;
494 c
.d
.asDwords
[4] = uid5
;
495 c
.d
.asDwords
[5] = uid6
;
496 c
.d
.asDwords
[6] = uid7
;
498 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
499 uid1
= (uid1
<< 4) | (uid2
>> 28);
500 uid2
= (uid2
<< 4) | (n
& 0xf);
502 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
503 c
.cmd
= CMD_INDALA_CLONE_TAG
;
508 clearCommandBuffer();
513 int CmdLFSetConfig(const char *Cmd
)
515 uint8_t divisor
= 0;//Frequency divisor
516 uint8_t bps
= 0; // Bits per sample
517 uint8_t decimation
= 0; //How many to keep
518 bool averaging
= 1; // Defaults to true
520 int trigger_threshold
= -1;//Means no change
521 uint8_t unsigned_trigg
= 0;
524 while(param_getchar(Cmd
, cmdp
) != 0x00)
526 switch(param_getchar(Cmd
, cmdp
))
529 return usage_lf_config();
539 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
543 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
545 if(!errors
) trigger_threshold
= unsigned_trigg
;
548 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
552 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
556 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
560 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
568 if (cmdp
== 0) errors
= 1;
571 if (errors
) return usage_lf_config();
573 //Bps is limited to 8, so fits in lower half of arg1
574 if (bps
>> 4) bps
= 8;
576 sample_config config
= { decimation
, bps
, averaging
, divisor
, trigger_threshold
};
578 //Averaging is a flag on high-bit of arg[1]
579 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
580 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
581 clearCommandBuffer();
586 int CmdLFRead(const char *Cmd
)
589 uint8_t cmdp
= param_getchar(Cmd
, 0);
591 if ( cmdp
== 'h' || cmdp
== 'H') return usage_lf_read();
594 if ( cmdp
== 's' || cmdp
== 'S') arg1
= true;
596 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}};
597 clearCommandBuffer();
599 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
600 PrintAndLog("command execution time out");
606 int CmdLFSnoop(const char *Cmd
)
608 uint8_t cmdp
= param_getchar(Cmd
, 0);
609 if(cmdp
== 'h' || cmdp
== 'H') return usage_lf_snoop();
611 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
612 clearCommandBuffer();
614 WaitForResponse(CMD_ACK
,NULL
);
618 static void ChkBitstream(const char *str
)
622 /* convert to bitstream if necessary */
623 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
624 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
630 //Attempt to simulate any wave in buffer (one bit per output sample)
631 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
632 int CmdLFSim(const char *Cmd
)
637 sscanf(Cmd
, "%i", &gap
);
639 // convert to bitstream if necessary
642 //can send only 512 bits at a time (1 byte sent per bit...)
643 printf("Sending [%d bytes]", GraphTraceLen
);
644 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
645 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
647 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
648 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
650 clearCommandBuffer();
652 WaitForResponse(CMD_ACK
,NULL
);
656 PrintAndLog("\nStarting to simulate");
657 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
658 clearCommandBuffer();
663 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
664 // - allow pull data from DemodBuffer
665 int CmdLFfskSim(const char *Cmd
)
667 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
668 // otherwise will need FChigh, FClow, Clock, and bitstream
669 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
672 char hexData
[32] = {0x00}; // store entered hex data
673 uint8_t data
[255] = {0x00};
676 while(param_getchar(Cmd
, cmdp
) != 0x00)
678 switch(param_getchar(Cmd
, cmdp
))
681 return usage_lf_simfsk();
687 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
691 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
695 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
703 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
707 dataLen
= hextobinarray((char *)data
, hexData
);
709 if (dataLen
==0) errors
=TRUE
;
710 if (errors
) PrintAndLog ("Error getting hex data");
714 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
720 if(cmdp
== 0 && DemodBufferLen
== 0)
722 errors
= TRUE
;// No args
728 return usage_lf_simfsk();
731 if (dataLen
== 0){ //using DemodBuffer
732 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
733 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0);
735 if (!fcHigh
) fcHigh
=10;
741 setDemodBuf(data
, dataLen
, 0);
744 //default if not found
745 if (clk
== 0) clk
= 50;
746 if (fcHigh
== 0) fcHigh
= 10;
747 if (fcLow
== 0) fcLow
= 8;
750 arg1
= fcHigh
<< 8 | fcLow
;
751 arg2
= invert
<< 8 | clk
;
752 size_t size
= DemodBufferLen
;
753 if (size
> USB_CMD_DATA_SIZE
) {
754 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
755 size
= USB_CMD_DATA_SIZE
;
757 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
759 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
760 clearCommandBuffer();
765 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
766 // - allow pull data from DemodBuffer
767 int CmdLFaskSim(const char *Cmd
)
769 //autodetect clock from Graphbuffer if using demod buffer
770 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
771 uint8_t encoding
= 1, separator
= 0;
772 uint8_t clk
=0, invert
=0;
774 char hexData
[32] = {0x00};
775 uint8_t data
[255]= {0x00}; // store entered hex data
778 while(param_getchar(Cmd
, cmdp
) != 0x00)
780 switch(param_getchar(Cmd
, cmdp
))
783 return usage_lf_simask();
789 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
793 encoding
=2; //biphase
809 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
813 dataLen
= hextobinarray((char *)data
, hexData
);
815 if (dataLen
==0) errors
=TRUE
;
816 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
820 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
826 if(cmdp
== 0 && DemodBufferLen
== 0)
828 errors
= TRUE
;// No args
834 return usage_lf_simask();
836 if (dataLen
== 0){ //using DemodBuffer
837 if (clk
== 0) clk
= GetAskClock("0", false, false);
839 setDemodBuf(data
, dataLen
, 0);
841 if (clk
== 0) clk
= 64;
842 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
844 size_t size
=DemodBufferLen
;
845 arg1
= clk
<< 8 | encoding
;
846 arg2
= invert
<< 8 | separator
;
847 if (size
> USB_CMD_DATA_SIZE
) {
848 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
849 size
= USB_CMD_DATA_SIZE
;
851 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
852 PrintAndLog("preparing to sim ask data: %d bits", size
);
853 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
854 clearCommandBuffer();
859 // by marshmellow - sim psk data given carrier, clock, invert
860 // - allow pull data from DemodBuffer or parameters
861 int CmdLFpskSim(const char *Cmd
)
863 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
864 //will need carrier, Clock, and bitstream
865 uint8_t carrier
=0, clk
=0;
868 char hexData
[32] = {0x00}; // store entered hex data
869 uint8_t data
[255] = {0x00};
873 while(param_getchar(Cmd
, cmdp
) != 0x00)
875 switch(param_getchar(Cmd
, cmdp
))
878 return usage_lf_simpsk();
884 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
888 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
904 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
908 dataLen
= hextobinarray((char *)data
, hexData
);
910 if (dataLen
==0) errors
=TRUE
;
911 if (errors
) PrintAndLog ("Error getting hex data");
915 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
921 if (cmdp
== 0 && DemodBufferLen
== 0)
923 errors
= TRUE
;// No args
929 return usage_lf_simpsk();
931 if (dataLen
== 0){ //using DemodBuffer
932 PrintAndLog("Getting Clocks");
933 if (clk
==0) clk
= GetPskClock("", FALSE
, FALSE
);
934 PrintAndLog("clk: %d",clk
);
935 if (!carrier
) carrier
= GetPskCarrier("", FALSE
, FALSE
);
936 PrintAndLog("carrier: %d", carrier
);
938 setDemodBuf(data
, dataLen
, 0);
941 if (clk
<= 0) clk
= 32;
942 if (carrier
== 0) carrier
= 2;
945 //need to convert psk2 to psk1 data before sim
946 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
948 PrintAndLog("Sorry, PSK3 not yet available");
952 arg1
= clk
<< 8 | carrier
;
954 size_t size
=DemodBufferLen
;
955 if (size
> USB_CMD_DATA_SIZE
) {
956 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
957 size
=USB_CMD_DATA_SIZE
;
959 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
960 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
961 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
962 clearCommandBuffer();
968 int CmdLFSimBidir(const char *Cmd
)
970 // Set ADC to twice the carrier for a slight supersampling
971 // HACK: not implemented in ARMSRC.
972 PrintAndLog("Not implemented yet.");
973 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
978 int CmdVchDemod(const char *Cmd
)
980 // Is this the entire sync pattern, or does this also include some
981 // data bits that happen to be the same everywhere? That would be
983 static const int SyncPattern
[] = {
984 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
985 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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,
996 // So first, we correlate for the sync pattern, and mark that.
997 int bestCorrel
= 0, bestPos
= 0;
999 // It does us no good to find the sync pattern, with fewer than
1000 // 2048 samples after it...
1001 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
1004 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
1005 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
1007 if (sum
> bestCorrel
) {
1012 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
1017 int worst
= INT_MAX
;
1020 for (i
= 0; i
< 2048; i
+= 8) {
1023 for (j
= 0; j
< 8; j
++) {
1024 sum
+= GraphBuffer
[bestPos
+i
+j
];
1031 if(abs(sum
) < worst
) {
1036 PrintAndLog("bits:");
1037 PrintAndLog("%s", bits
);
1038 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
1040 if (strcmp(Cmd
, "clone")==0) {
1043 for(s
= bits
; *s
; s
++) {
1045 for(j
= 0; j
< 16; j
++) {
1046 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
1049 RepaintGraphWindow();
1055 int CmdLFfind(const char *Cmd
) {
1057 char cmdp
= param_getchar(Cmd
, 0);
1058 char testRaw
= param_getchar(Cmd
, 1);
1059 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_find();
1061 if (!offline
&& (cmdp
!= '1')){
1063 getSamples("30000",false);
1064 } else if (GraphTraceLen
< 1000) {
1065 PrintAndLog("Data in Graphbuffer was too small.");
1068 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
1070 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1071 PrintAndLog("False Positives ARE possible\n");
1072 PrintAndLog("\nChecking for known tags:\n");
1074 ans
=CmdFSKdemodIO("");
1076 PrintAndLog("\nValid IO Prox ID Found!");
1080 ans
=CmdFSKdemodPyramid("");
1082 PrintAndLog("\nValid Pyramid ID Found!");
1086 ans
=CmdFSKdemodParadox("");
1088 PrintAndLog("\nValid Paradox ID Found!");
1092 ans
=CmdFSKdemodAWID("");
1094 PrintAndLog("\nValid AWID ID Found!");
1098 ans
=CmdFSKdemodHID("");
1100 PrintAndLog("\nValid HID Prox ID Found!");
1104 ans
=CmdAskEM410xDemod("");
1106 PrintAndLog("\nValid EM410x ID Found!");
1110 ans
=CmdG_Prox_II_Demod("");
1112 PrintAndLog("\nValid Guardall G-Prox II ID Found!");
1116 ans
=CmdFDXBdemodBI("");
1118 PrintAndLog("\nValid FDX-B ID Found!");
1122 ans
=EM4x50Read("", false);
1124 PrintAndLog("\nValid EM4x50 ID Found!");
1128 ans
=CmdVikingDemod("");
1130 PrintAndLog("\nValid Viking ID Found!");
1134 ans
=CmdIndalaDecode("");
1136 PrintAndLog("\nValid Indala ID Found!");
1140 ans
=CmdPSKNexWatch("");
1142 PrintAndLog("\nValid NexWatch ID Found!");
1148 PrintAndLog("\nNo Known Tags Found!\n");
1149 if (testRaw
=='u' || testRaw
=='U'){
1150 //test unknown tag formats (raw mode)
1151 PrintAndLog("\nChecking for Unknown tags:\n");
1152 ans
=AutoCorrelate(4000, FALSE
, FALSE
);
1156 PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1158 if ( ans
% 8 == 0) {
1159 int bytes
= (ans
/ 8);
1160 PrintAndLog("Possible %d bytes", bytes
);
1162 if ( bytes
% 2 == 0) {
1163 blocks
= (bytes
/ 2);
1164 PrintAndLog("Possible 2 blocks, width %d", blocks
);
1166 if ( bytes
% 4 == 0) {
1167 blocks
= (bytes
/ 4);
1168 PrintAndLog("Possible 4 blocks, width %d", blocks
);
1170 if ( bytes
% 8 == 0) {
1171 blocks
= (bytes
/ 8);
1172 PrintAndLog("Possible 8 blocks, width %d", blocks
);
1174 if ( bytes
% 16 == 0) {
1175 blocks
= (bytes
/ 16);
1176 PrintAndLog("Possible 16 blocks, width %d", blocks
);
1181 ans
=GetFskClock("",FALSE
,FALSE
);
1182 if (ans
!= 0){ //fsk
1183 ans
=FSKrawDemod("",TRUE
);
1185 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1190 ans
=ASKDemod("0 0 0",TRUE
,FALSE
,1);
1192 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1193 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1197 ans
=CmdPSK1rawDemod("");
1199 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1200 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1201 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1204 PrintAndLog("\nNo Data Found!\n");
1209 static command_t CommandTable
[] =
1211 {"help", CmdHelp
, 1, "This help"},
1212 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1213 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
1214 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1215 {"hitag", CmdLFHitag
, 1, "{ HITAG RFIDs... }"},
1216 {"io", CmdLFIO
, 1, "{ IOPROX RFIDs... }"},
1217 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 RFIDs... }"},
1218 {"presco", CmdLFPresco
, 1, "{ Presco RFIDs... }"},
1219 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
1220 {"t55xx", CmdLFT55XX
, 1, "{ T55X7 RFIDs... }"},
1221 {"viking", CmdLFViking
, 1, "{ Viking RFIDs... }"},
1222 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1223 {"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)"},
1224 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1225 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1226 {"indalaclone", CmdIndalaClone
, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1227 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1228 {"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"},
1229 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1230 {"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"},
1231 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] \n\t\t-- Simulate LF FSK tag from demodbuffer or input"},
1232 {"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"},
1233 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1234 {"snoop", CmdLFSnoop
, 0, "Snoop LF"},
1235 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1236 {NULL
, NULL
, 0, NULL
}
1239 int CmdLF(const char *Cmd
) {
1240 clearCommandBuffer();
1241 CmdsParse(CommandTable
, Cmd
);
1245 int CmdHelp(const char *Cmd
) {
1246 CmdsHelp(CommandTable
);