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 static int CmdHelp(const char *Cmd
);
37 int usage_lf_cmdread()
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(" H Freqency High (134 KHz), default is 'Low (125KHz)'");
43 PrintAndLog(" d <delay> delay OFF period, (dec)");
44 PrintAndLog(" z <zero> time period ZERO, (dec)");
45 PrintAndLog(" o <one> time period ONE, (dec)");
46 PrintAndLog(" c <cmd> Command bytes");
47 PrintAndLog(" ************* All periods in microseconds (ms)");
48 PrintAndLog("Examples:");
49 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
50 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
54 /* send a LF command before reading */
55 int CmdLFCommandRead(const char *Cmd
)
57 static char dummy
[3] = {0x20,0x00,0x00};
58 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
64 while(param_getchar(Cmd
, cmdp
) != 0x00) {
65 switch(param_getchar(Cmd
, cmdp
))
68 return usage_lf_cmdread();
77 strLength
= param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
);
81 c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
85 c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
89 c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10);
93 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
100 if (cmdp
== 0) errors
= 1;
103 if (errors
) return usage_lf_cmdread();
105 // in case they specified 'H'
106 // added to the end..
107 strcpy((char *)&c
.d
.asBytes
+ strLength
, dummy
);
109 clearCommandBuffer();
114 int CmdFlexdemod(const char *Cmd
)
117 for (i
= 0; i
< GraphTraceLen
; ++i
) {
118 if (GraphBuffer
[i
] < 0) {
125 #define LONG_WAIT 100
127 for (start
= 0; start
< GraphTraceLen
- LONG_WAIT
; start
++) {
128 int first
= GraphBuffer
[start
];
129 for (i
= start
; i
< start
+ LONG_WAIT
; i
++) {
130 if (GraphBuffer
[i
] != first
) {
134 if (i
== (start
+ LONG_WAIT
)) {
138 if (start
== GraphTraceLen
- LONG_WAIT
) {
139 PrintAndLog("nothing to wait for");
143 GraphBuffer
[start
] = 2;
144 GraphBuffer
[start
+1] = -2;
145 uint8_t bits
[64] = {0x00};
149 for (bit
= 0; bit
< 64; bit
++) {
151 for (int j
= 0; j
< 16; j
++) {
152 sum
+= GraphBuffer
[i
++];
155 bits
[bit
] = (sum
> 0) ? 1 : 0;
157 PrintAndLog("bit %d sum %d", bit
, sum
);
160 for (bit
= 0; bit
< 64; bit
++) {
163 for (j
= 0; j
< 16; j
++) {
164 sum
+= GraphBuffer
[i
++];
166 if (sum
> 0 && bits
[bit
] != 1) {
167 PrintAndLog("oops1 at %d", bit
);
169 if (sum
< 0 && bits
[bit
] != 0) {
170 PrintAndLog("oops2 at %d", bit
);
174 // HACK writing back to graphbuffer.
175 GraphTraceLen
= 32*64;
178 for (bit
= 0; bit
< 64; bit
++) {
180 phase
= (bits
[bit
] == 0) ? 0 : 1;
183 for (j
= 0; j
< 32; j
++) {
184 GraphBuffer
[i
++] = phase
;
189 RepaintGraphWindow();
193 int CmdIndalaDemod(const char *Cmd
)
195 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
201 // worst case with GraphTraceLen=64000 is < 4096
202 // under normal conditions it's < 2048
204 uint8_t rawbits
[4096];
206 int worst
= 0, worstPos
= 0;
207 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
208 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
210 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
212 for (j
= 0; j
< count
- 8; j
+= 16) {
213 rawbits
[rawbit
++] = 0;
215 if ((abs(count
- j
)) > worst
) {
216 worst
= abs(count
- j
);
222 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
224 for (j
= 0; j
< count
- 8; j
+= 16) {
225 rawbits
[rawbit
++] = 1;
227 if ((abs(count
- j
)) > worst
) {
228 worst
= abs(count
- j
);
238 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32);
239 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
244 // Finding the start of a UID
245 int uidlen
, long_wait
;
246 if (strcmp(Cmd
, "224") == 0) {
256 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
257 first
= rawbits
[start
];
258 for (i
= start
; i
< start
+ long_wait
; i
++) {
259 if (rawbits
[i
] != first
) {
263 if (i
== (start
+ long_wait
)) {
268 if (start
== rawbit
- uidlen
+ 1) {
269 PrintAndLog("nothing to wait for");
273 // Inverting signal if needed
275 for (i
= start
; i
< rawbit
; i
++) {
276 rawbits
[i
] = !rawbits
[i
];
281 uint8_t bits
[224] = {0x00};
282 char showbits
[225] = {0x00};
287 if (uidlen
> rawbit
) {
288 PrintAndLog("Warning: not enough raw bits to get a full UID");
289 for (bit
= 0; bit
< rawbit
; bit
++) {
290 bits
[bit
] = rawbits
[i
++];
291 // As we cannot know the parity, let's use "." and "/"
292 showbits
[bit
] = '.' + bits
[bit
];
294 showbits
[bit
+1]='\0';
295 PrintAndLog("Partial UID=%s", showbits
);
298 for (bit
= 0; bit
< uidlen
; bit
++) {
299 bits
[bit
] = rawbits
[i
++];
300 showbits
[bit
] = '0' + bits
[bit
];
306 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
311 for( idx
=0; idx
<64; idx
++) {
312 if (showbits
[idx
] == '0') {
313 uid1
=(uid1
<<1)|(uid2
>>31);
316 uid1
=(uid1
<<1)|(uid2
>>31);
320 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
323 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
325 for( idx
=0; idx
<224; idx
++) {
326 uid1
=(uid1
<<1)|(uid2
>>31);
327 uid2
=(uid2
<<1)|(uid3
>>31);
328 uid3
=(uid3
<<1)|(uid4
>>31);
329 uid4
=(uid4
<<1)|(uid5
>>31);
330 uid5
=(uid5
<<1)|(uid6
>>31);
331 uid6
=(uid6
<<1)|(uid7
>>31);
333 if (showbits
[idx
] == '0')
334 uid7
= (uid7
<<1) | 0;
336 uid7
= (uid7
<<1) | 1;
338 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
341 // Checking UID against next occurrences
343 for (; i
+ uidlen
<= rawbit
;) {
345 for (bit
= 0; bit
< uidlen
; bit
++) {
346 if (bits
[bit
] != rawbits
[i
++]) {
357 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
359 // Remodulating for tag cloning
360 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
361 // since this changes graphbuffer data.
362 GraphTraceLen
= 32*uidlen
;
365 for (bit
= 0; bit
< uidlen
; bit
++) {
366 if (bits
[bit
] == 0) {
372 for (j
= 0; j
< 32; j
++) {
373 GraphBuffer
[i
++] = phase
;
378 RepaintGraphWindow();
382 int CmdIndalaClone(const char *Cmd
)
385 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
387 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
390 if (strchr(Cmd
,'l') != 0) {
391 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
392 uid1
= (uid1
<< 4) | (uid2
>> 28);
393 uid2
= (uid2
<< 4) | (uid3
>> 28);
394 uid3
= (uid3
<< 4) | (uid4
>> 28);
395 uid4
= (uid4
<< 4) | (uid5
>> 28);
396 uid5
= (uid5
<< 4) | (uid6
>> 28);
397 uid6
= (uid6
<< 4) | (uid7
>> 28);
398 uid7
= (uid7
<< 4) | (n
& 0xf);
400 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
401 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
402 c
.d
.asDwords
[0] = uid1
;
403 c
.d
.asDwords
[1] = uid2
;
404 c
.d
.asDwords
[2] = uid3
;
405 c
.d
.asDwords
[3] = uid4
;
406 c
.d
.asDwords
[4] = uid5
;
407 c
.d
.asDwords
[5] = uid6
;
408 c
.d
.asDwords
[6] = uid7
;
410 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
411 uid1
= (uid1
<< 4) | (uid2
>> 28);
412 uid2
= (uid2
<< 4) | (n
& 0xf);
414 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
415 c
.cmd
= CMD_INDALA_CLONE_TAG
;
426 PrintAndLog("Usage: lf read");
427 PrintAndLog("Options: ");
428 PrintAndLog(" h This help");
429 PrintAndLog(" s silent run no printout");
430 PrintAndLog("This function takes no arguments. ");
431 PrintAndLog("Use 'lf config' to set parameters.");
436 PrintAndLog("Usage: lf snoop");
437 PrintAndLog("Options: ");
438 PrintAndLog(" h This help");
439 PrintAndLog("This function takes no arguments. ");
440 PrintAndLog("Use 'lf config' to set parameters.");
444 int usage_lf_config()
446 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
447 PrintAndLog("Options: ");
448 PrintAndLog(" h This help");
449 PrintAndLog(" L Low frequency (125 KHz)");
450 PrintAndLog(" H High frequency (134 KHz)");
451 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
452 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
453 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
454 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
455 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
456 PrintAndLog("Examples:");
457 PrintAndLog(" lf config b 8 L");
458 PrintAndLog(" Samples at 125KHz, 8bps.");
459 PrintAndLog(" lf config H b 4 d 3");
460 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
461 PrintAndLog(" a resolution of 4 bits per sample.");
462 PrintAndLog(" lf read");
463 PrintAndLog(" Performs a read (active field)");
464 PrintAndLog(" lf snoop");
465 PrintAndLog(" Performs a snoop (no active field)");
469 int CmdLFSetConfig(const char *Cmd
)
472 uint8_t divisor
= 0;//Frequency divisor
473 uint8_t bps
= 0; // Bits per sample
474 uint8_t decimation
= 0; //How many to keep
475 bool averaging
= 1; // Defaults to true
477 int trigger_threshold
=-1;//Means no change
478 uint8_t unsigned_trigg
= 0;
481 while(param_getchar(Cmd
, cmdp
) != 0x00)
483 switch(param_getchar(Cmd
, cmdp
))
486 return usage_lf_config();
496 errors
|= param_getdec(Cmd
,cmdp
+1,&divisor
);
500 errors
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
);
502 if(!errors
) trigger_threshold
= unsigned_trigg
;
505 errors
|= param_getdec(Cmd
,cmdp
+1,&bps
);
509 errors
|= param_getdec(Cmd
,cmdp
+1,&decimation
);
513 averaging
= param_getchar(Cmd
,cmdp
+1) == '1';
517 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
525 errors
= 1;// No args
531 return usage_lf_config();
533 //Bps is limited to 8, so fits in lower half of arg1
534 if(bps
>> 8) bps
= 8;
536 sample_config config
= {
537 decimation
,bps
,averaging
,divisor
,trigger_threshold
539 //Averaging is a flag on high-bit of arg[1]
540 UsbCommand c
= {CMD_SET_LF_SAMPLING_CONFIG
};
541 memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
));
546 int CmdLFRead(const char *Cmd
)
551 if (param_getchar(Cmd
, cmdp
) == 'h')
553 return usage_lf_read();
555 if (param_getchar(Cmd
, cmdp
) == 's') arg1
= true; //suppress print
556 //And ship it to device
557 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}};
559 //WaitForResponse(CMD_ACK,NULL);
560 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) {
561 PrintAndLog("command execution time out");
568 int CmdLFSnoop(const char *Cmd
)
571 if(param_getchar(Cmd
, cmdp
) == 'h')
573 return usage_lf_snoop();
576 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
578 WaitForResponse(CMD_ACK
,NULL
);
582 static void ChkBitstream(const char *str
)
586 /* convert to bitstream if necessary */
587 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++){
588 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) {
594 //Attempt to simulate any wave in buffer (one bit per output sample)
595 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
596 int CmdLFSim(const char *Cmd
)
601 sscanf(Cmd
, "%i", &gap
);
603 // convert to bitstream if necessary
607 //can send only 512 bits at a time (1 byte sent per bit...)
608 printf("Sending [%d bytes]", GraphTraceLen
);
609 for (i
= 0; i
< GraphTraceLen
; i
+= USB_CMD_DATA_SIZE
) {
610 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
612 for (j
= 0; j
< USB_CMD_DATA_SIZE
; j
++) {
613 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
616 WaitForResponse(CMD_ACK
,NULL
);
621 PrintAndLog("Starting to simulate");
622 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
627 int usage_lf_simfsk(void)
630 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
631 PrintAndLog("Options: ");
632 PrintAndLog(" h This help");
633 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
634 PrintAndLog(" i invert data");
635 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
636 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
637 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
638 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
639 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
643 int usage_lf_simask(void)
646 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
647 PrintAndLog("Options: ");
648 PrintAndLog(" h This help");
649 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
650 PrintAndLog(" i invert data");
651 PrintAndLog(" b sim ask/biphase");
652 PrintAndLog(" m sim ask/manchester - Default");
653 PrintAndLog(" r sim ask/raw");
654 PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
655 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
659 int usage_lf_simpsk(void)
662 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
663 PrintAndLog("Options: ");
664 PrintAndLog(" h This help");
665 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
666 PrintAndLog(" i invert data");
667 PrintAndLog(" 1 set PSK1 (default)");
668 PrintAndLog(" 2 set PSK2");
669 PrintAndLog(" 3 set PSK3");
670 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
671 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
675 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
676 // - allow pull data from DemodBuffer
677 int CmdLFfskSim(const char *Cmd
)
679 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
680 // otherwise will need FChigh, FClow, Clock, and bitstream
681 uint8_t fcHigh
=0, fcLow
=0, clk
=0;
684 char hexData
[32] = {0x00}; // store entered hex data
685 uint8_t data
[255] = {0x00};
688 while(param_getchar(Cmd
, cmdp
) != 0x00)
690 switch(param_getchar(Cmd
, cmdp
))
693 return usage_lf_simfsk();
699 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
703 errors
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
);
707 errors
|= param_getdec(Cmd
,cmdp
+1,&fcLow
);
715 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
719 dataLen
= hextobinarray((char *)data
, hexData
);
721 if (dataLen
==0) errors
=TRUE
;
722 if (errors
) PrintAndLog ("Error getting hex data");
726 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
732 if(cmdp
== 0 && DemodBufferLen
== 0)
734 errors
= TRUE
;// No args
740 return usage_lf_simfsk();
743 if (dataLen
== 0){ //using DemodBuffer
744 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all
745 uint8_t ans
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0);
747 if (!fcHigh
) fcHigh
=10;
753 setDemodBuf(data
, dataLen
, 0);
756 //default if not found
757 if (clk
== 0) clk
= 50;
758 if (fcHigh
== 0) fcHigh
= 10;
759 if (fcLow
== 0) fcLow
= 8;
762 arg1
= fcHigh
<< 8 | fcLow
;
763 arg2
= invert
<< 8 | clk
;
764 size_t size
= DemodBufferLen
;
765 if (size
> USB_CMD_DATA_SIZE
) {
766 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
767 size
= USB_CMD_DATA_SIZE
;
769 UsbCommand c
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}};
771 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
776 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
777 // - allow pull data from DemodBuffer
778 int CmdLFaskSim(const char *Cmd
)
780 //autodetect clock from Graphbuffer if using demod buffer
781 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
782 uint8_t encoding
= 1, separator
= 0;
783 uint8_t clk
=0, invert
=0;
785 char hexData
[32] = {0x00};
786 uint8_t data
[255]= {0x00}; // store entered hex data
789 while(param_getchar(Cmd
, cmdp
) != 0x00)
791 switch(param_getchar(Cmd
, cmdp
))
794 return usage_lf_simask();
800 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
804 encoding
=2; //biphase
820 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
824 dataLen
= hextobinarray((char *)data
, hexData
);
826 if (dataLen
==0) errors
=TRUE
;
827 if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
);
831 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
837 if(cmdp
== 0 && DemodBufferLen
== 0)
839 errors
= TRUE
;// No args
845 return usage_lf_simask();
847 if (dataLen
== 0){ //using DemodBuffer
848 if (clk
== 0) clk
= GetAskClock("0", false, false);
850 setDemodBuf(data
, dataLen
, 0);
852 if (clk
== 0) clk
= 64;
853 if (encoding
== 0) clk
= clk
/2; //askraw needs to double the clock speed
855 size_t size
=DemodBufferLen
;
856 arg1
= clk
<< 8 | encoding
;
857 arg2
= invert
<< 8 | separator
;
858 if (size
> USB_CMD_DATA_SIZE
) {
859 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
860 size
= USB_CMD_DATA_SIZE
;
862 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
863 PrintAndLog("preparing to sim ask data: %d bits", size
);
864 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
869 // by marshmellow - sim psk data given carrier, clock, invert
870 // - allow pull data from DemodBuffer or parameters
871 int CmdLFpskSim(const char *Cmd
)
873 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
874 //will need carrier, Clock, and bitstream
875 uint8_t carrier
=0, clk
=0;
878 char hexData
[32] = {0x00}; // store entered hex data
879 uint8_t data
[255] = {0x00};
883 while(param_getchar(Cmd
, cmdp
) != 0x00)
885 switch(param_getchar(Cmd
, cmdp
))
888 return usage_lf_simpsk();
894 errors
|= param_getdec(Cmd
,cmdp
+1,&clk
);
898 errors
|= param_getdec(Cmd
,cmdp
+1,&carrier
);
914 dataLen
= param_getstr(Cmd
, cmdp
+1, hexData
);
918 dataLen
= hextobinarray((char *)data
, hexData
);
920 if (dataLen
==0) errors
=TRUE
;
921 if (errors
) PrintAndLog ("Error getting hex data");
925 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
931 if (cmdp
== 0 && DemodBufferLen
== 0)
933 errors
= TRUE
;// No args
939 return usage_lf_simpsk();
941 if (dataLen
== 0){ //using DemodBuffer
942 PrintAndLog("Getting Clocks");
943 if (clk
==0) clk
= GetPskClock("", FALSE
, FALSE
);
944 PrintAndLog("clk: %d",clk
);
945 if (!carrier
) carrier
= GetPskCarrier("", FALSE
, FALSE
);
946 PrintAndLog("carrier: %d", carrier
);
948 setDemodBuf(data
, dataLen
, 0);
951 if (clk
<= 0) clk
= 32;
952 if (carrier
== 0) carrier
= 2;
955 //need to convert psk2 to psk1 data before sim
956 psk2TOpsk1(DemodBuffer
, DemodBufferLen
);
958 PrintAndLog("Sorry, PSK3 not yet available");
962 arg1
= clk
<< 8 | carrier
;
964 size_t size
=DemodBufferLen
;
965 if (size
> USB_CMD_DATA_SIZE
) {
966 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
);
967 size
=USB_CMD_DATA_SIZE
;
969 UsbCommand c
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}};
970 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
);
971 memcpy(c
.d
.asBytes
, DemodBuffer
, size
);
977 int CmdLFSimBidir(const char *Cmd
)
979 // Set ADC to twice the carrier for a slight supersampling
980 // HACK: not implemented in ARMSRC.
981 PrintAndLog("Not implemented yet.");
982 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
987 int CmdVchDemod(const char *Cmd
)
989 // Is this the entire sync pattern, or does this also include some
990 // data bits that happen to be the same everywhere? That would be
992 static const int SyncPattern
[] = {
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,
996 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
997 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
998 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
999 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1000 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1001 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1002 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1005 // So first, we correlate for the sync pattern, and mark that.
1006 int bestCorrel
= 0, bestPos
= 0;
1008 // It does us no good to find the sync pattern, with fewer than
1009 // 2048 samples after it...
1010 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
1013 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
1014 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
1016 if (sum
> bestCorrel
) {
1021 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
1026 int worst
= INT_MAX
;
1029 for (i
= 0; i
< 2048; i
+= 8) {
1032 for (j
= 0; j
< 8; j
++) {
1033 sum
+= GraphBuffer
[bestPos
+i
+j
];
1040 if(abs(sum
) < worst
) {
1045 PrintAndLog("bits:");
1046 PrintAndLog("%s", bits
);
1047 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
1049 if (strcmp(Cmd
, "clone")==0) {
1052 for(s
= bits
; *s
; s
++) {
1054 for(j
= 0; j
< 16; j
++) {
1055 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
1058 RepaintGraphWindow();
1064 int CmdLFfind(const char *Cmd
)
1067 char cmdp
= param_getchar(Cmd
, 0);
1068 char testRaw
= param_getchar(Cmd
, 1);
1069 if (strlen(Cmd
) > 3 || cmdp
== 'h' || cmdp
== 'H') {
1070 PrintAndLog("Usage: lf search <0|1> [u]");
1071 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
1072 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
1074 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
1075 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
1076 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
1077 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
1082 if (!offline
&& (cmdp
!= '1')){
1084 getSamples("30000",false);
1085 } else if (GraphTraceLen
< 1000) {
1086 PrintAndLog("Data in Graphbuffer was too small.");
1089 if (cmdp
== 'u' || cmdp
== 'U') testRaw
= 'u';
1091 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1092 PrintAndLog("False Positives ARE possible\n");
1093 PrintAndLog("\nChecking for known tags:\n");
1095 ans
=CmdFSKdemodIO("");
1097 PrintAndLog("\nValid IO Prox ID Found!");
1101 ans
=CmdFSKdemodPyramid("");
1103 PrintAndLog("\nValid Pyramid ID Found!");
1107 ans
=CmdFSKdemodParadox("");
1109 PrintAndLog("\nValid Paradox ID Found!");
1113 ans
=CmdFSKdemodAWID("");
1115 PrintAndLog("\nValid AWID ID Found!");
1119 ans
=CmdFSKdemodHID("");
1121 PrintAndLog("\nValid HID Prox ID Found!");
1125 //add psk and indala
1126 ans
=CmdIndalaDecode("");
1128 PrintAndLog("\nValid Indala ID Found!");
1132 ans
=CmdAskEM410xDemod("");
1134 PrintAndLog("\nValid EM410x ID Found!");
1138 ans
=CmdG_Prox_II_Demod("");
1140 PrintAndLog("\nValid Guardall G-Prox II ID Found!");
1144 ans
=CmdFDXBdemodBI("");
1146 PrintAndLog("\nValid FDX-B ID Found!");
1150 ans
=EM4x50Read("", false);
1152 PrintAndLog("\nValid EM4x50 ID Found!");
1156 ans
=CmdPSKNexWatch("");
1158 PrintAndLog("\nValid NexWatch ID Found!");
1162 PrintAndLog("\nNo Known Tags Found!\n");
1163 if (testRaw
=='u' || testRaw
=='U'){
1164 //test unknown tag formats (raw mode)
1165 PrintAndLog("\nChecking for Unknown tags:\n");
1166 ans
=AutoCorrelate(4000, FALSE
, FALSE
);
1170 PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
);
1172 if ( ans
% 8 == 0) {
1173 int bytes
= (ans
/ 8);
1174 PrintAndLog("Possible %d bytes", bytes
);
1176 if ( bytes
% 2 == 0) {
1177 blocks
= (bytes
/ 2);
1178 PrintAndLog("Possible 2 blocks, width %d", blocks
);
1180 if ( bytes
% 4 == 0) {
1181 blocks
= (bytes
/ 4);
1182 PrintAndLog("Possible 4 blocks, width %d", blocks
);
1184 if ( bytes
% 8 == 0) {
1185 blocks
= (bytes
/ 8);
1186 PrintAndLog("Possible 8 blocks, width %d", blocks
);
1188 if ( bytes
% 16 == 0) {
1189 blocks
= (bytes
/ 16);
1190 PrintAndLog("Possible 16 blocks, width %d", blocks
);
1194 ans
=GetFskClock("",FALSE
,FALSE
);
1195 if (ans
!= 0){ //fsk
1196 ans
=FSKrawDemod("",TRUE
);
1198 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1202 ans
=ASKDemod("0 0 0",TRUE
,FALSE
,1);
1204 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1205 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1208 ans
=CmdPSK1rawDemod("");
1210 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1211 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1212 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1215 PrintAndLog("\nNo Data Found!\n");
1220 static command_t CommandTable
[] =
1222 {"help", CmdHelp
, 1, "This help"},
1223 {"awid", CmdLFAWID
, 1, "{ AWID RFIDs... }"},
1224 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
1225 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
1226 {"hitag", CmdLFHitag
, 1, "{ HITAG RFIDs... }"},
1227 {"io", CmdLFIO
, 1, "{ IOPROX RFIDs... }"},
1228 {"pcf7931", CmdLFPCF7931
, 1, "{ PCF7931 RFIDs... }"},
1229 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
1230 {"t55xx", CmdLFT55XX
, 1, "{ T55X7 RFIDs... }"},
1231 {"viking", CmdLFViking
, 1, "{ Viking RFIDs... }"},
1232 {"config", CmdLFSetConfig
, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1234 {"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)"},
1235 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
1236 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1237 {"indalaclone", CmdIndalaClone
, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1238 {"read", CmdLFRead
, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1239 {"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"},
1240 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1241 {"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"},
1242 {"simfsk", CmdLFfskSim
, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] \n\t\t-- Simulate LF FSK tag from demodbuffer or input"},
1243 {"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"},
1244 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1245 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold] -- Snoop LF (l:125khz, h:134khz)"},
1246 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
1247 {NULL
, NULL
, 0, NULL
}
1250 int CmdLF(const char *Cmd
)
1252 CmdsParse(CommandTable
, Cmd
);
1256 int CmdHelp(const char *Cmd
)
1258 CmdsHelp(CommandTable
);