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" 
  26 #include "cmdlfem4x.h" 
  27 #include "cmdlfhitag.h" 
  28 #include "cmdlft55xx.h" 
  29 #include "cmdlfpcf7931.h" 
  32 static int CmdHelp(const char *Cmd
); 
  34 /* send a command before reading */ 
  35 int CmdLFCommandRead(const char *Cmd
) 
  41   UsbCommand c 
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
}; 
  42   sscanf(Cmd
, "%"lli
" %"lli
" %"lli
" %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2],(char*)(&c
.d
.asBytes
),(char*)(&dummy
+1)); 
  43   // in case they specified 'h' 
  44   strcpy((char *)&c
.d
.asBytes 
+ strlen((char *)c
.d
.asBytes
), dummy
); 
  49 int CmdFlexdemod(const char *Cmd
) 
  52   for (i 
= 0; i 
< GraphTraceLen
; ++i
) { 
  53     if (GraphBuffer
[i
] < 0) { 
  62   for (start 
= 0; start 
< GraphTraceLen 
- LONG_WAIT
; start
++) { 
  63     int first 
= GraphBuffer
[start
]; 
  64     for (i 
= start
; i 
< start 
+ LONG_WAIT
; i
++) { 
  65       if (GraphBuffer
[i
] != first
) { 
  69     if (i 
== (start 
+ LONG_WAIT
)) { 
  73   if (start 
== GraphTraceLen 
- LONG_WAIT
) { 
  74     PrintAndLog("nothing to wait for"); 
  78   GraphBuffer
[start
] = 2; 
  79   GraphBuffer
[start
+1] = -2; 
  80         uint8_t bits
[64] = {0x00}; 
  84   for (bit 
= 0; bit 
< 64; bit
++) { 
  86                 for (int j 
= 0; j 
< 16; j
++) { 
  87       sum 
+= GraphBuffer
[i
++]; 
  90                 bits
[bit
] = (sum 
> 0) ? 1 : 0; 
  92     PrintAndLog("bit %d sum %d", bit
, sum
); 
  95   for (bit 
= 0; bit 
< 64; bit
++) { 
  98     for (j 
= 0; j 
< 16; j
++) { 
  99       sum 
+= GraphBuffer
[i
++]; 
 101     if (sum 
> 0 && bits
[bit
] != 1) { 
 102       PrintAndLog("oops1 at %d", bit
); 
 104     if (sum 
< 0 && bits
[bit
] != 0) { 
 105       PrintAndLog("oops2 at %d", bit
); 
 109         // HACK writing back to graphbuffer. 
 110   GraphTraceLen 
= 32*64; 
 113   for (bit 
= 0; bit 
< 64; bit
++) { 
 115                 phase 
= (bits
[bit
] == 0) ? 0 : 1; 
 118     for (j 
= 0; j 
< 32; j
++) { 
 119       GraphBuffer
[i
++] = phase
; 
 124   RepaintGraphWindow(); 
 128 int CmdIndalaDemod(const char *Cmd
) 
 130   // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID 
 136   // worst case with GraphTraceLen=64000 is < 4096 
 137   // under normal conditions it's < 2048 
 139   uint8_t rawbits
[4096]; 
 141   int worst 
= 0, worstPos 
= 0; 
 142  // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32); 
 143   for (i 
= 0; i 
< GraphTraceLen
-1; i 
+= 2) { 
 145     if ((GraphBuffer
[i
] > GraphBuffer
[i 
+ 1]) && (state 
!= 1)) { 
 147         for (j 
= 0; j 
<  count 
- 8; j 
+= 16) { 
 148           rawbits
[rawbit
++] = 0; 
 150         if ((abs(count 
- j
)) > worst
) { 
 151           worst 
= abs(count 
- j
); 
 157     } else if ((GraphBuffer
[i
] < GraphBuffer
[i 
+ 1]) && (state 
!= 0)) { 
 159         for (j 
= 0; j 
<  count 
- 8; j 
+= 16) { 
 160           rawbits
[rawbit
++] = 1; 
 162         if ((abs(count 
- j
)) > worst
) { 
 163           worst 
= abs(count 
- j
); 
 173     PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32); 
 174     PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
); 
 179   // Finding the start of a UID 
 180   int uidlen
, long_wait
; 
 181   if (strcmp(Cmd
, "224") == 0) { 
 191   for (start 
= 0; start 
<= rawbit 
- uidlen
; start
++) { 
 192     first 
= rawbits
[start
]; 
 193     for (i 
= start
; i 
< start 
+ long_wait
; i
++) { 
 194       if (rawbits
[i
] != first
) { 
 198     if (i 
== (start 
+ long_wait
)) { 
 203   if (start 
== rawbit 
- uidlen 
+ 1) { 
 204     PrintAndLog("nothing to wait for"); 
 208   // Inverting signal if needed 
 210     for (i 
= start
; i 
< rawbit
; i
++) { 
 211       rawbits
[i
] = !rawbits
[i
]; 
 216         uint8_t bits
[224] = {0x00}; 
 217         char showbits
[225] = {0x00}; 
 222   if (uidlen 
> rawbit
) { 
 223     PrintAndLog("Warning: not enough raw bits to get a full UID"); 
 224     for (bit 
= 0; bit 
< rawbit
; bit
++) { 
 225       bits
[bit
] = rawbits
[i
++]; 
 226       // As we cannot know the parity, let's use "." and "/" 
 227       showbits
[bit
] = '.' + bits
[bit
]; 
 229     showbits
[bit
+1]='\0'; 
 230     PrintAndLog("Partial UID=%s", showbits
); 
 233     for (bit 
= 0; bit 
< uidlen
; bit
++) { 
 234       bits
[bit
] = rawbits
[i
++]; 
 235       showbits
[bit
] = '0' + bits
[bit
]; 
 241   uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
; 
 246     for( idx
=0; idx
<64; idx
++) { 
 247         if (showbits
[idx
] == '0') { 
 248         uid1
=(uid1
<<1)|(uid2
>>31); 
 251         uid1
=(uid1
<<1)|(uid2
>>31); 
 255     PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
); 
 258                 uid3 
= uid4 
= uid5 
= uid6 
= uid7 
= 0; 
 260     for( idx
=0; idx
<224; idx
++) { 
 261         uid1
=(uid1
<<1)|(uid2
>>31); 
 262         uid2
=(uid2
<<1)|(uid3
>>31); 
 263         uid3
=(uid3
<<1)|(uid4
>>31); 
 264         uid4
=(uid4
<<1)|(uid5
>>31); 
 265         uid5
=(uid5
<<1)|(uid6
>>31); 
 266         uid6
=(uid6
<<1)|(uid7
>>31); 
 268                         if (showbits
[idx
] == '0')  
 269                                 uid7 
= (uid7
<<1) | 0; 
 271                                 uid7 
= (uid7
<<1) | 1; 
 273     PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
); 
 276   // Checking UID against next occurrences 
 278         for (; i 
+ uidlen 
<= rawbit
;) { 
 280     for (bit 
= 0; bit 
< uidlen
; bit
++) { 
 281       if (bits
[bit
] != rawbits
[i
++]) { 
 292   PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit 
- start
) / uidlen
); 
 294   // Remodulating for tag cloning 
 295         // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)  
 296         // since this changes graphbuffer data. 
 297   GraphTraceLen 
= 32*uidlen
; 
 300   for (bit 
= 0; bit 
< uidlen
; bit
++) { 
 301     if (bits
[bit
] == 0) { 
 307     for (j 
= 0; j 
< 32; j
++) { 
 308       GraphBuffer
[i
++] = phase
; 
 313   RepaintGraphWindow(); 
 317 int CmdIndalaClone(const char *Cmd
) 
 320         unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
; 
 322         uid1 
=  uid2 
= uid3 
= uid4 
= uid5 
= uid6 
= uid7 
= 0; 
 325   if (strchr(Cmd
,'l') != 0) { 
 326     while (sscanf(&Cmd
[i
++], "%1x", &n 
) == 1) { 
 327       uid1 
= (uid1 
<< 4) | (uid2 
>> 28); 
 328       uid2 
= (uid2 
<< 4) | (uid3 
>> 28); 
 329       uid3 
= (uid3 
<< 4) | (uid4 
>> 28); 
 330       uid4 
= (uid4 
<< 4) | (uid5 
>> 28); 
 331       uid5 
= (uid5 
<< 4) | (uid6 
>> 28); 
 332       uid6 
= (uid6 
<< 4) | (uid7 
>> 28); 
 333         uid7 
= (uid7 
<< 4) | (n 
& 0xf); 
 335     PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
); 
 336     c
.cmd 
= CMD_INDALA_CLONE_TAG_L
; 
 337     c
.d
.asDwords
[0] = uid1
; 
 338     c
.d
.asDwords
[1] = uid2
; 
 339     c
.d
.asDwords
[2] = uid3
; 
 340     c
.d
.asDwords
[3] = uid4
; 
 341     c
.d
.asDwords
[4] = uid5
; 
 342     c
.d
.asDwords
[5] = uid6
; 
 343     c
.d
.asDwords
[6] = uid7
; 
 345     while (sscanf(&Cmd
[i
++], "%1x", &n 
) == 1) { 
 346       uid1 
= (uid1 
<< 4) | (uid2 
>> 28); 
 347       uid2 
= (uid2 
<< 4) | (n 
& 0xf); 
 349     PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
); 
 350     c
.cmd 
= CMD_INDALA_CLONE_TAG
; 
 361         PrintAndLog("Usage: lf read"); 
 362         PrintAndLog("Options:        "); 
 363         PrintAndLog("       h            This help"); 
 364         PrintAndLog("This function takes no arguments. "); 
 365         PrintAndLog("Use 'lf config' to set parameters."); 
 370         PrintAndLog("Usage: lf snoop"); 
 371         PrintAndLog("Options:        "); 
 372         PrintAndLog("       h            This help"); 
 373         PrintAndLog("This function takes no arguments. "); 
 374         PrintAndLog("Use 'lf config' to set parameters."); 
 378 int usage_lf_config() 
 380         PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]"); 
 381         PrintAndLog("Options:        "); 
 382         PrintAndLog("       h             This help"); 
 383         PrintAndLog("       L             Low frequency (125 KHz)"); 
 384         PrintAndLog("       H             High frequency (134 KHz)"); 
 385         PrintAndLog("       q <divisor>   Manually set divisor. 88-> 134KHz, 95-> 125 Hz"); 
 386         PrintAndLog("       b <bps>       Sets resolution of bits per sample. Default (max): 8"); 
 387         PrintAndLog("       d <decim>     Sets decimation. A value of N saves only 1 in N samples. Default: 1"); 
 388         PrintAndLog("       a [0|1]       Averaging - if set, will average the stored sample value when decimating. Default: 1"); 
 389         PrintAndLog("       t <threshold> Sets trigger threshold. 0 means no threshold"); 
 390         PrintAndLog("Examples:"); 
 391         PrintAndLog("      lf config b 8 L"); 
 392         PrintAndLog("                    Samples at 125KHz, 8bps."); 
 393         PrintAndLog("      lf config H b 4 d 3"); 
 394         PrintAndLog("                    Samples at 134KHz, averages three samples into one, stored with "); 
 395         PrintAndLog("                    a resolution of 4 bits per sample."); 
 396         PrintAndLog("      lf read"); 
 397         PrintAndLog("                    Performs a read (active field)"); 
 398         PrintAndLog("      lf snoop"); 
 399         PrintAndLog("                    Performs a snoop (no active field)"); 
 403 int CmdLFSetConfig(const char *Cmd
) 
 406         uint8_t divisor 
=  0;//Frequency divisor 
 407         uint8_t bps 
= 0; // Bits per sample 
 408         uint8_t decimation 
= 0; //How many to keep 
 409         bool averaging 
= 1; // Defaults to true 
 411         int trigger_threshold 
=-1;//Means no change 
 412         uint8_t unsigned_trigg 
= 0; 
 415         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 417                 switch(param_getchar(Cmd
, cmdp
)) 
 420                         return usage_lf_config(); 
 430                         errors 
|= param_getdec(Cmd
,cmdp
+1,&divisor
); 
 434                         errors 
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
); 
 436                         if(!errors
) trigger_threshold 
= unsigned_trigg
; 
 439                         errors 
|= param_getdec(Cmd
,cmdp
+1,&bps
); 
 443                         errors 
|= param_getdec(Cmd
,cmdp
+1,&decimation
); 
 447                         averaging 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 451                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 459                 errors 
= 1;// No args 
 465                 return usage_lf_config(); 
 467         //Bps is limited to 8, so fits in lower half of arg1 
 468         if(bps 
>> 8) bps 
= 8; 
 470         sample_config config 
= { 
 471                 decimation
,bps
,averaging
,divisor
,trigger_threshold
 
 473         //Averaging is a flag on high-bit of arg[1] 
 474         UsbCommand c 
= {CMD_SET_LF_SAMPLING_CONFIG
}; 
 475         memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
)); 
 480 int CmdLFRead(const char *Cmd
) 
 484         if(param_getchar(Cmd
, cmdp
) == 'h') 
 486                 return usage_lf_read(); 
 488         //And ship it to device 
 489         UsbCommand c 
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
}; 
 491         WaitForResponse(CMD_ACK
,NULL
); 
 495 int CmdLFSnoop(const char *Cmd
) 
 498         if(param_getchar(Cmd
, cmdp
) == 'h') 
 500                 return usage_lf_snoop(); 
 503         UsbCommand c 
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
}; 
 505         WaitForResponse(CMD_ACK
,NULL
); 
 509 static void ChkBitstream(const char *str
) 
 513   /* convert to bitstream if necessary */ 
 514         for (i 
= 0; i 
< (int)(GraphTraceLen 
/ 2); i
++){ 
 515                 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) { 
 521 //appears to attempt to simulate manchester 
 522 int CmdLFSim(const char *Cmd
) 
 527   sscanf(Cmd
, "%i", &gap
); 
 529   /* convert to bitstream if necessary */ 
 532   //can send 512 bits at a time (1 byte sent per bit...) 
 533         printf("Sending [%d bytes]", GraphTraceLen
); 
 534         for (i 
= 0; i 
< GraphTraceLen
; i 
+= USB_CMD_DATA_SIZE
) { 
 535     UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}}; 
 537                 for (j 
= 0; j 
< USB_CMD_DATA_SIZE
; j
++) { 
 538       c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
]; 
 541     WaitForResponse(CMD_ACK
,NULL
); 
 546         PrintAndLog("Starting to simulate"); 
 547   UsbCommand c 
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}}; 
 552 int usage_lf_simfsk(void) 
 555   PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]"); 
 556   PrintAndLog("Options:        "); 
 557   PrintAndLog("       h              This help"); 
 558   PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 559   PrintAndLog("       i              invert data"); 
 560   PrintAndLog("       H <fcHigh>     Manually set the larger Field Clock"); 
 561   PrintAndLog("       L <fcLow>      Manually set the smaller Field Clock"); 
 562   //PrintAndLog("       s              TBD- -to enable a gap between playback repetitions - default: no gap"); 
 563   PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 567 int usage_lf_simask(void) 
 570   PrintAndLog("Usage: lf simask [c <clock>] [i] [m|r] [s] [d <raw hex to sim>]"); 
 571   PrintAndLog("Options:        "); 
 572   PrintAndLog("       h              This help"); 
 573   PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 574   PrintAndLog("       i              invert data"); 
 575   PrintAndLog("       m              sim ask/manchester"); 
 576   PrintAndLog("       r              sim ask/raw"); 
 577   PrintAndLog("       s              TBD- -to enable a gap between playback repetitions - default: no gap"); 
 578   PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 582 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert  
 583 // - allow pull data from DemodBuffer 
 584 int CmdLFfskSim(const char *Cmd
) 
 586   //todo - allow data from demodbuffer or parameters 
 587   //might be able to autodetect FC and clock from Graphbuffer if using demod buffer 
 588   //will need FChigh, FClow, Clock, and bitstream 
 589   uint8_t fcHigh
=0, fcLow
=0, clk
=0; 
 592   char hexData
[32] = {0x00}; // store entered hex data 
 593   uint8_t data
[255] = {0x00};  
 596   while(param_getchar(Cmd
, cmdp
) != 0x00) 
 598     switch(param_getchar(Cmd
, cmdp
)) 
 601       return usage_lf_simfsk(); 
 607       errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 611       errors 
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
); 
 615       errors 
|= param_getdec(Cmd
,cmdp
+1,&fcLow
); 
 623       dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 627         dataLen 
= hextobinarray((char *)data
, hexData
); 
 628       }    if (dataLen
==0) errors
=TRUE
;  
 629       if (errors
) PrintAndLog ("Error getting hex data"); 
 633       PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 639   if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 641     errors 
= TRUE
;// No args 
 647     return usage_lf_simfsk(); 
 649   if (dataLen 
== 0){ //using DemodBuffer 
 650     if (clk
==0 || fcHigh
==0 || fcLow
==0){ 
 651       uint8_t ans 
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0); 
 659     setDemodBuf(data
, dataLen
, 0); 
 661   if (clk 
== 0) clk 
= 50; 
 662   if (fcHigh 
== 0) fcHigh 
= 10; 
 663   if (fcLow 
== 0) fcLow 
= 8; 
 666   arg1 
= fcHigh 
<< 8 | fcLow
; 
 667   arg2 
= invert 
<< 8 | clk
; 
 668   UsbCommand c 
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, DemodBufferLen
}}; 
 669   if (DemodBufferLen 
> USB_CMD_DATA_SIZE
) { 
 670     PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", DemodBufferLen
, USB_CMD_DATA_SIZE
); 
 672   memcpy(c
.d
.asBytes
, DemodBuffer
, DemodBufferLen
); 
 677 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator  
 678 // - allow pull data from DemodBuffer 
 679 int CmdLFaskSim(const char *Cmd
) 
 681   //todo - allow data from demodbuffer or parameters 
 682   //autodetect clock from Graphbuffer if using demod buffer 
 683   //will need clock, invert, manchester/raw as m or r, separator as s, and bitstream 
 684   uint8_t manchester 
= 1, separator 
= 0; 
 685   //char cmdp = Cmd[0], par3='m', par4=0; 
 686   uint8_t clk
=0, invert
=0; 
 688   char hexData
[32] = {0x00};  
 689   uint8_t data
[255]= {0x00}; // store entered hex data 
 692   while(param_getchar(Cmd
, cmdp
) != 0x00) 
 694     switch(param_getchar(Cmd
, cmdp
)) 
 697       return usage_lf_simask(); 
 703       errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 719       dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 723         dataLen 
= hextobinarray((char *)data
, hexData
); 
 725       if (dataLen
==0) errors
=TRUE
;  
 726       if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
); 
 730       PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 736   if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 738     errors 
= TRUE
;// No args 
 744     return usage_lf_simask(); 
 746   if (dataLen 
== 0){ //using DemodBuffer 
 747     if (clk 
== 0) clk 
= GetAskClock("0", false, false); 
 749     setDemodBuf(data
, dataLen
, 0); 
 751   if (clk 
== 0) clk 
= 64; 
 754   arg1 
= clk 
<< 8 | manchester
; 
 755   arg2 
= invert 
<< 8 | separator
; 
 756   UsbCommand c 
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, DemodBufferLen
}}; 
 757   if (DemodBufferLen 
> USB_CMD_DATA_SIZE
) { 
 758     PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", DemodBufferLen
, USB_CMD_DATA_SIZE
); 
 760   PrintAndLog("preparing to sim ask data: %d bits", DemodBufferLen
); 
 761   memcpy(c
.d
.asBytes
, DemodBuffer
, DemodBufferLen
); 
 767 int CmdLFSimBidir(const char *Cmd
) 
 769   // Set ADC to twice the carrier for a slight supersampling 
 770   // HACK: not implemented in ARMSRC. 
 771   PrintAndLog("Not implemented yet."); 
 772   UsbCommand c 
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}}; 
 777 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */ 
 778 int CmdLFSimManchester(const char *Cmd
) 
 780   static int clock
, gap
; 
 781   static char data
[1024], gapstring
[8]; 
 783   sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
); 
 787   for (int i 
= 0; i 
< strlen(data
) ; ++i
) 
 788     AppendGraph(0, clock
, data
[i
]- '0'); 
 790   CmdManchesterMod(""); 
 792   RepaintGraphWindow(); 
 794   sprintf(&gapstring
[0], "%i", gap
); 
 800 int CmdVchDemod(const char *Cmd
) 
 802   // Is this the entire sync pattern, or does this also include some 
 803   // data bits that happen to be the same everywhere? That would be 
 805   static const int SyncPattern
[] = { 
 806     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 807     1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 808     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 809     1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 810     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 811     1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 812     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 813     1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 814     1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 815     1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 818   // So first, we correlate for the sync pattern, and mark that. 
 819   int bestCorrel 
= 0, bestPos 
= 0; 
 821   // It does us no good to find the sync pattern, with fewer than 
 822   // 2048 samples after it... 
 823   for (i 
= 0; i 
< (GraphTraceLen
-2048); i
++) { 
 826     for (j 
= 0; j 
< arraylen(SyncPattern
); j
++) { 
 827       sum 
+= GraphBuffer
[i
+j
]*SyncPattern
[j
]; 
 829     if (sum 
> bestCorrel
) { 
 834   PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
); 
 842   for (i 
= 0; i 
< 2048; i 
+= 8) { 
 845     for (j 
= 0; j 
< 8; j
++) { 
 846       sum 
+= GraphBuffer
[bestPos
+i
+j
]; 
 853     if(abs(sum
) < worst
) { 
 858   PrintAndLog("bits:"); 
 859   PrintAndLog("%s", bits
); 
 860   PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
); 
 862   if (strcmp(Cmd
, "clone")==0) { 
 865     for(s 
= bits
; *s
; s
++) { 
 867       for(j 
= 0; j 
< 16; j
++) { 
 868         GraphBuffer
[GraphTraceLen
++] = (*s 
== '1') ? 1 : 0; 
 871     RepaintGraphWindow(); 
 877 int CmdLFfind(const char *Cmd
) 
 880   char cmdp 
= param_getchar(Cmd
, 0); 
 881   char testRaw 
= param_getchar(Cmd
, 1); 
 882   if (strlen(Cmd
) > 2 || cmdp 
== 'h' || cmdp 
== 'H') { 
 883     PrintAndLog("Usage:  lf search <0|1> [u]"); 
 884     PrintAndLog("     <use data from Graphbuffer> , if not set, try reading data from tag."); 
 885     PrintAndLog("     [Search for Unknown tags] , if not set, reads only known tags."); 
 887     PrintAndLog("    sample: lf search     = try reading data from tag & search for known tags"); 
 888     PrintAndLog("          : lf search 1   = use data from GraphBuffer & search for known tags"); 
 889     PrintAndLog("          : lf search u   = try reading data from tag & search for known and unknown tags"); 
 890     PrintAndLog("          : lf search 1 u = use data from GraphBuffer & search for known and unknown tags"); 
 895   if (!offline 
&& (cmdp 
!= '1')){ 
 897     ans
=CmdSamples("20000"); 
 898   } else if (GraphTraceLen 
< 1000) { 
 899     PrintAndLog("Data in Graphbuffer was too small."); 
 902   if (cmdp 
== 'u' || cmdp 
== 'U') testRaw 
= 'u'; 
 903   PrintAndLog("NOTE: some demods output possible binary\n  if it finds something that looks like a tag"); 
 904   PrintAndLog("False Positives ARE possible\n");   
 905   PrintAndLog("\nChecking for known tags:\n"); 
 906   ans
=CmdFSKdemodIO(""); 
 908     PrintAndLog("\nValid IO Prox ID Found!"); 
 911   ans
=CmdFSKdemodPyramid(""); 
 913     PrintAndLog("\nValid Pyramid ID Found!"); 
 916   ans
=CmdFSKdemodParadox(""); 
 918     PrintAndLog("\nValid Paradox ID Found!"); 
 921   ans
=CmdFSKdemodAWID(""); 
 923     PrintAndLog("\nValid AWID ID Found!"); 
 926   ans
=CmdFSKdemodHID(""); 
 928     PrintAndLog("\nValid HID Prox ID Found!"); 
 932   ans
=CmdIndalaDecode(""); 
 934     PrintAndLog("\nValid Indala ID Found!"); 
 937   ans
=CmdAskEM410xDemod(""); 
 939     PrintAndLog("\nValid EM410x ID Found!"); 
 942   PrintAndLog("\nNo Known Tags Found!\n"); 
 943   if (testRaw
=='u' || testRaw
=='U'){ 
 944     //test unknown tag formats (raw mode) 
 945     PrintAndLog("\nChecking for Unknown tags:\n"); 
 946     ans
=CmdDetectClockRate("f"); 
 948       ans
=CmdFSKrawdemod(""); 
 950         PrintAndLog("\nUnknown FSK Modulated Tag Found!"); 
 954     ans
=Cmdaskmandemod(""); 
 956       PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!"); 
 959     ans
=CmdPSK1rawDemod(""); 
 961       PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data psk2rawdemod'"); 
 962       PrintAndLog("\nCould also be PSK3 - [currently not supported]"); 
 963       PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod"); 
 966     PrintAndLog("\nNo Data Found!\n"); 
 971 static command_t CommandTable
[] =  
 973   {"help",        CmdHelp
,            1, "This help"}, 
 974   {"cmdread",     CmdLFCommandRead
,   0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"}, 
 975   {"em4x",        CmdLFEM4X
,          1, "{ EM4X RFIDs... }"}, 
 976   {"config",      CmdLFSetConfig
,     0, "Set config for LF sampling, bit/sample, decimation, frequency"}, 
 977   {"flexdemod",   CmdFlexdemod
,       1, "Demodulate samples for FlexPass"}, 
 978   {"hid",         CmdLFHID
,           1, "{ HID RFIDs... }"}, 
 979   {"io",          CmdLFIO
,                1, "{ ioProx tags... }"}, 
 980   {"indalademod", CmdIndalaDemod
,     1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"}, 
 981   {"indalaclone", CmdIndalaClone
,     0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"}, 
 982   {"read",        CmdLFRead
,          0, "Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"}, 
 983   {"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"}, 
 984   {"sim",         CmdLFSim
,           0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"}, 
 985   {"simask",      CmdLFaskSim
,        0, "[clock] [invert <1|0>] [manchester/raw <'m'|'r'>] [trs separator 's'] -- Simulate LF ASK tag from demodbuffer"}, 
 986   {"simfsk",      CmdLFfskSim
,        0, "[invert <1|0>] -- Simulate LF FSK tag from demodbuffer"}, 
 987   {"simbidir",    CmdLFSimBidir
,      0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"}, 
 988   {"simman",      CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"}, 
 989   {"snoop",       CmdLFSnoop
,         0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"}, 
 990   {"ti",          CmdLFTI
,            1, "{ TI RFIDs... }"}, 
 991   {"hitag",       CmdLFHitag
,         1, "{ Hitag tags and transponders... }"}, 
 992   {"vchdemod",    CmdVchDemod
,        1, "['clone'] -- Demodulate samples for VeriChip"}, 
 993   {"t55xx",       CmdLFT55XX
,         1, "{ T55xx RFIDs... }"}, 
 994   {"pcf7931",     CmdLFPCF7931
,       1, "{PCF7931 RFIDs...}"}, 
 995   {NULL
, NULL
, 0, NULL
} 
 998 int CmdLF(const char *Cmd
) 
1000   CmdsParse(CommandTable
, Cmd
); 
1004 int CmdHelp(const char *Cmd
) 
1006   CmdsHelp(CommandTable
);