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" 
  33 static int CmdHelp(const char *Cmd
); 
  35 /* send a command before reading */ 
  36 int CmdLFCommandRead(const char *Cmd
) 
  42         UsbCommand c 
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
}; 
  43         sscanf(Cmd
, "%"lli
" %"lli
" %"lli
" %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2],(char*)(&c
.d
.asBytes
),(char*)(&dummy
+1)); 
  44         // in case they specified 'h' 
  45         strcpy((char *)&c
.d
.asBytes 
+ strlen((char *)c
.d
.asBytes
), dummy
); 
  50 int CmdFlexdemod(const char *Cmd
) 
  53         for (i 
= 0; i 
< GraphTraceLen
; ++i
) { 
  54                 if (GraphBuffer
[i
] < 0) { 
  63         for (start 
= 0; start 
< GraphTraceLen 
- LONG_WAIT
; start
++) { 
  64                 int first 
= GraphBuffer
[start
]; 
  65                 for (i 
= start
; i 
< start 
+ LONG_WAIT
; i
++) { 
  66                         if (GraphBuffer
[i
] != first
) { 
  70                 if (i 
== (start 
+ LONG_WAIT
)) { 
  74         if (start 
== GraphTraceLen 
- LONG_WAIT
) { 
  75                 PrintAndLog("nothing to wait for"); 
  79         GraphBuffer
[start
] = 2; 
  80         GraphBuffer
[start
+1] = -2; 
  81         uint8_t bits
[64] = {0x00}; 
  85         for (bit 
= 0; bit 
< 64; bit
++) { 
  87                 for (int j 
= 0; j 
< 16; j
++) { 
  88                         sum 
+= GraphBuffer
[i
++]; 
  91                 bits
[bit
] = (sum 
> 0) ? 1 : 0; 
  93                 PrintAndLog("bit %d sum %d", bit
, sum
); 
  96         for (bit 
= 0; bit 
< 64; bit
++) { 
  99                 for (j 
= 0; j 
< 16; j
++) { 
 100                         sum 
+= GraphBuffer
[i
++]; 
 102                 if (sum 
> 0 && bits
[bit
] != 1) { 
 103                         PrintAndLog("oops1 at %d", bit
); 
 105                 if (sum 
< 0 && bits
[bit
] != 0) { 
 106                         PrintAndLog("oops2 at %d", bit
); 
 110         // HACK writing back to graphbuffer. 
 111         GraphTraceLen 
= 32*64; 
 114         for (bit 
= 0; bit 
< 64; bit
++) { 
 116                 phase 
= (bits
[bit
] == 0) ? 0 : 1; 
 119                 for (j 
= 0; j 
< 32; j
++) { 
 120                         GraphBuffer
[i
++] = phase
; 
 125         RepaintGraphWindow(); 
 129 int CmdIndalaDemod(const char *Cmd
) 
 131         // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID 
 137         // worst case with GraphTraceLen=64000 is < 4096 
 138         // under normal conditions it's < 2048 
 140         uint8_t rawbits
[4096]; 
 142         int worst 
= 0, worstPos 
= 0; 
 143  // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32); 
 144         for (i 
= 0; i 
< GraphTraceLen
-1; i 
+= 2) { 
 146                 if ((GraphBuffer
[i
] > GraphBuffer
[i 
+ 1]) && (state 
!= 1)) { 
 148                                 for (j 
= 0; j 
<  count 
- 8; j 
+= 16) { 
 149                                         rawbits
[rawbit
++] = 0; 
 151                                 if ((abs(count 
- j
)) > worst
) { 
 152                                         worst 
= abs(count 
- j
); 
 158                 } else if ((GraphBuffer
[i
] < GraphBuffer
[i 
+ 1]) && (state 
!= 0)) { 
 160                                 for (j 
= 0; j 
<  count 
- 8; j 
+= 16) { 
 161                                         rawbits
[rawbit
++] = 1; 
 163                                 if ((abs(count 
- j
)) > worst
) { 
 164                                         worst 
= abs(count 
- j
); 
 174                 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit
, GraphTraceLen
/32); 
 175                 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
); 
 180         // Finding the start of a UID 
 181         int uidlen
, long_wait
; 
 182         if (strcmp(Cmd
, "224") == 0) { 
 192         for (start 
= 0; start 
<= rawbit 
- uidlen
; start
++) { 
 193                 first 
= rawbits
[start
]; 
 194                 for (i 
= start
; i 
< start 
+ long_wait
; i
++) { 
 195                         if (rawbits
[i
] != first
) { 
 199                 if (i 
== (start 
+ long_wait
)) { 
 204         if (start 
== rawbit 
- uidlen 
+ 1) { 
 205                 PrintAndLog("nothing to wait for"); 
 209         // Inverting signal if needed 
 211                 for (i 
= start
; i 
< rawbit
; i
++) { 
 212                         rawbits
[i
] = !rawbits
[i
]; 
 217         uint8_t bits
[224] = {0x00}; 
 218         char showbits
[225] = {0x00}; 
 223         if (uidlen 
> rawbit
) { 
 224                 PrintAndLog("Warning: not enough raw bits to get a full UID"); 
 225                 for (bit 
= 0; bit 
< rawbit
; bit
++) { 
 226                         bits
[bit
] = rawbits
[i
++]; 
 227                         // As we cannot know the parity, let's use "." and "/" 
 228                         showbits
[bit
] = '.' + bits
[bit
]; 
 230                 showbits
[bit
+1]='\0'; 
 231                 PrintAndLog("Partial UID=%s", showbits
); 
 234                 for (bit 
= 0; bit 
< uidlen
; bit
++) { 
 235                         bits
[bit
] = rawbits
[i
++]; 
 236                         showbits
[bit
] = '0' + bits
[bit
]; 
 242         uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
; 
 247                 for( idx
=0; idx
<64; idx
++) { 
 248                                 if (showbits
[idx
] == '0') { 
 249                                 uid1
=(uid1
<<1)|(uid2
>>31); 
 252                                 uid1
=(uid1
<<1)|(uid2
>>31); 
 256                 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
); 
 259                 uid3 
= uid4 
= uid5 
= uid6 
= uid7 
= 0; 
 261                 for( idx
=0; idx
<224; idx
++) { 
 262                                 uid1
=(uid1
<<1)|(uid2
>>31); 
 263                                 uid2
=(uid2
<<1)|(uid3
>>31); 
 264                                 uid3
=(uid3
<<1)|(uid4
>>31); 
 265                                 uid4
=(uid4
<<1)|(uid5
>>31); 
 266                                 uid5
=(uid5
<<1)|(uid6
>>31); 
 267                                 uid6
=(uid6
<<1)|(uid7
>>31); 
 269                         if (showbits
[idx
] == '0')  
 270                                 uid7 
= (uid7
<<1) | 0; 
 272                                 uid7 
= (uid7
<<1) | 1; 
 274                 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
); 
 277         // Checking UID against next occurrences 
 279         for (; i 
+ uidlen 
<= rawbit
;) { 
 281                 for (bit 
= 0; bit 
< uidlen
; bit
++) { 
 282                         if (bits
[bit
] != rawbits
[i
++]) { 
 293         PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit 
- start
) / uidlen
); 
 295         // Remodulating for tag cloning 
 296         // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)  
 297         // since this changes graphbuffer data. 
 298         GraphTraceLen 
= 32*uidlen
; 
 301         for (bit 
= 0; bit 
< uidlen
; bit
++) { 
 302                 if (bits
[bit
] == 0) { 
 308                 for (j 
= 0; j 
< 32; j
++) { 
 309                         GraphBuffer
[i
++] = phase
; 
 314         RepaintGraphWindow(); 
 318 int CmdIndalaClone(const char *Cmd
) 
 321         unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
; 
 323         uid1 
=  uid2 
= uid3 
= uid4 
= uid5 
= uid6 
= uid7 
= 0; 
 326         if (strchr(Cmd
,'l') != 0) { 
 327                 while (sscanf(&Cmd
[i
++], "%1x", &n 
) == 1) { 
 328                         uid1 
= (uid1 
<< 4) | (uid2 
>> 28); 
 329                         uid2 
= (uid2 
<< 4) | (uid3 
>> 28); 
 330                         uid3 
= (uid3 
<< 4) | (uid4 
>> 28); 
 331                         uid4 
= (uid4 
<< 4) | (uid5 
>> 28); 
 332                         uid5 
= (uid5 
<< 4) | (uid6 
>> 28); 
 333                         uid6 
= (uid6 
<< 4) | (uid7 
>> 28); 
 334                         uid7 
= (uid7 
<< 4) | (n 
& 0xf); 
 336                 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
); 
 337                 c
.cmd 
= CMD_INDALA_CLONE_TAG_L
; 
 338                 c
.d
.asDwords
[0] = uid1
; 
 339                 c
.d
.asDwords
[1] = uid2
; 
 340                 c
.d
.asDwords
[2] = uid3
; 
 341                 c
.d
.asDwords
[3] = uid4
; 
 342                 c
.d
.asDwords
[4] = uid5
; 
 343                 c
.d
.asDwords
[5] = uid6
; 
 344                 c
.d
.asDwords
[6] = uid7
; 
 346                 while (sscanf(&Cmd
[i
++], "%1x", &n 
) == 1) { 
 347                         uid1 
= (uid1 
<< 4) | (uid2 
>> 28); 
 348                         uid2 
= (uid2 
<< 4) | (n 
& 0xf); 
 350                 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
); 
 351                 c
.cmd 
= CMD_INDALA_CLONE_TAG
; 
 362         PrintAndLog("Usage: lf read"); 
 363         PrintAndLog("Options:        "); 
 364         PrintAndLog("       h            This help"); 
 365         PrintAndLog("       s            silent run no printout"); 
 366         PrintAndLog("This function takes no arguments. "); 
 367         PrintAndLog("Use 'lf config' to set parameters."); 
 372         PrintAndLog("Usage: lf snoop"); 
 373         PrintAndLog("Options:        "); 
 374         PrintAndLog("       h            This help"); 
 375         PrintAndLog("This function takes no arguments. "); 
 376         PrintAndLog("Use 'lf config' to set parameters."); 
 380 int usage_lf_config() 
 382         PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]"); 
 383         PrintAndLog("Options:        "); 
 384         PrintAndLog("       h             This help"); 
 385         PrintAndLog("       L             Low frequency (125 KHz)"); 
 386         PrintAndLog("       H             High frequency (134 KHz)"); 
 387         PrintAndLog("       q <divisor>   Manually set divisor. 88-> 134KHz, 95-> 125 Hz"); 
 388         PrintAndLog("       b <bps>       Sets resolution of bits per sample. Default (max): 8"); 
 389         PrintAndLog("       d <decim>     Sets decimation. A value of N saves only 1 in N samples. Default: 1"); 
 390         PrintAndLog("       a [0|1]       Averaging - if set, will average the stored sample value when decimating. Default: 1"); 
 391         PrintAndLog("       t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)"); 
 392         PrintAndLog("Examples:"); 
 393         PrintAndLog("      lf config b 8 L"); 
 394         PrintAndLog("                    Samples at 125KHz, 8bps."); 
 395         PrintAndLog("      lf config H b 4 d 3"); 
 396         PrintAndLog("                    Samples at 134KHz, averages three samples into one, stored with "); 
 397         PrintAndLog("                    a resolution of 4 bits per sample."); 
 398         PrintAndLog("      lf read"); 
 399         PrintAndLog("                    Performs a read (active field)"); 
 400         PrintAndLog("      lf snoop"); 
 401         PrintAndLog("                    Performs a snoop (no active field)"); 
 405 int CmdLFSetConfig(const char *Cmd
) 
 408         uint8_t divisor 
=  0;//Frequency divisor 
 409         uint8_t bps 
= 0; // Bits per sample 
 410         uint8_t decimation 
= 0; //How many to keep 
 411         bool averaging 
= 1; // Defaults to true 
 413         int trigger_threshold 
=-1;//Means no change 
 414         uint8_t unsigned_trigg 
= 0; 
 417         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 419                 switch(param_getchar(Cmd
, cmdp
)) 
 422                         return usage_lf_config(); 
 432                         errors 
|= param_getdec(Cmd
,cmdp
+1,&divisor
); 
 436                         errors 
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
); 
 438                         if(!errors
) trigger_threshold 
= unsigned_trigg
; 
 441                         errors 
|= param_getdec(Cmd
,cmdp
+1,&bps
); 
 445                         errors 
|= param_getdec(Cmd
,cmdp
+1,&decimation
); 
 449                         averaging 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 453                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 461                 errors 
= 1;// No args 
 467                 return usage_lf_config(); 
 469         //Bps is limited to 8, so fits in lower half of arg1 
 470         if(bps 
>> 8) bps 
= 8; 
 472         sample_config config 
= { 
 473                 decimation
,bps
,averaging
,divisor
,trigger_threshold
 
 475         //Averaging is a flag on high-bit of arg[1] 
 476         UsbCommand c 
= {CMD_SET_LF_SAMPLING_CONFIG
}; 
 477         memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
)); 
 482 int CmdLFRead(const char *Cmd
) 
 487         if (param_getchar(Cmd
, cmdp
) == 'h') 
 489                 return usage_lf_read(); 
 491         if (param_getchar(Cmd
, cmdp
) == 's') arg1 
= true; //suppress print 
 492         //And ship it to device 
 493         UsbCommand c 
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}}; 
 495         WaitForResponse(CMD_ACK
,NULL
); 
 499 int CmdLFSnoop(const char *Cmd
) 
 502         if(param_getchar(Cmd
, cmdp
) == 'h') 
 504                 return usage_lf_snoop(); 
 507         UsbCommand c 
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
}; 
 509         WaitForResponse(CMD_ACK
,NULL
); 
 513 static void ChkBitstream(const char *str
) 
 517         /* convert to bitstream if necessary */ 
 518         for (i 
= 0; i 
< (int)(GraphTraceLen 
/ 2); i
++){ 
 519                 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) { 
 525 //Attempt to simulate any wave in buffer (one bit per output sample) 
 526 // converts GraphBuffer to bitstream (based on zero crossings) if needed. 
 527 int CmdLFSim(const char *Cmd
) 
 532         sscanf(Cmd
, "%i", &gap
); 
 534         // convert to bitstream if necessary  
 538         //can send only 512 bits at a time (1 byte sent per bit...) 
 539         printf("Sending [%d bytes]", GraphTraceLen
); 
 540         for (i 
= 0; i 
< GraphTraceLen
; i 
+= USB_CMD_DATA_SIZE
) { 
 541                 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}}; 
 543                 for (j 
= 0; j 
< USB_CMD_DATA_SIZE
; j
++) { 
 544                         c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
]; 
 547                 WaitForResponse(CMD_ACK
,NULL
); 
 552         PrintAndLog("Starting to simulate"); 
 553         UsbCommand c 
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}}; 
 558 int usage_lf_simfsk(void) 
 561         PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]"); 
 562         PrintAndLog("Options:        "); 
 563         PrintAndLog("       h              This help"); 
 564         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 565         PrintAndLog("       i              invert data"); 
 566         PrintAndLog("       H <fcHigh>     Manually set the larger Field Clock"); 
 567         PrintAndLog("       L <fcLow>      Manually set the smaller Field Clock"); 
 568         //PrintAndLog("       s              TBD- -to enable a gap between playback repetitions - default: no gap"); 
 569         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 570         PrintAndLog("\n  NOTE: if you set one clock manually set them all manually"); 
 574 int usage_lf_simask(void) 
 577         PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]"); 
 578         PrintAndLog("Options:        "); 
 579         PrintAndLog("       h              This help"); 
 580         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 581         PrintAndLog("       i              invert data"); 
 582         PrintAndLog("       b              sim ask/biphase"); 
 583         PrintAndLog("       m              sim ask/manchester - Default"); 
 584         PrintAndLog("       r              sim ask/raw"); 
 585         PrintAndLog("       s              TBD- -to enable a gap between playback repetitions - default: no gap"); 
 586         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 590 int usage_lf_simpsk(void) 
 593         PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]"); 
 594         PrintAndLog("Options:        "); 
 595         PrintAndLog("       h              This help"); 
 596         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 597         PrintAndLog("       i              invert data"); 
 598         PrintAndLog("       1              set PSK1 (default)"); 
 599         PrintAndLog("       2              set PSK2"); 
 600         PrintAndLog("       3              set PSK3"); 
 601         PrintAndLog("       r <carrier>    2|4|8 are valid carriers: default = 2"); 
 602         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 606 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert  
 607 // - allow pull data from DemodBuffer 
 608 int CmdLFfskSim(const char *Cmd
) 
 610         //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer 
 611         // otherwise will need FChigh, FClow, Clock, and bitstream 
 612         uint8_t fcHigh
=0, fcLow
=0, clk
=0; 
 615         char hexData
[32] = {0x00}; // store entered hex data 
 616         uint8_t data
[255] = {0x00};  
 619         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 621                 switch(param_getchar(Cmd
, cmdp
)) 
 624                         return usage_lf_simfsk(); 
 630                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 634                         errors 
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
); 
 638                         errors 
|= param_getdec(Cmd
,cmdp
+1,&fcLow
); 
 646                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 650                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 652                         if (dataLen
==0) errors
=TRUE
;  
 653                         if (errors
) PrintAndLog ("Error getting hex data"); 
 657                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 663         if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 665                 errors 
= TRUE
;// No args 
 671                 return usage_lf_simfsk(); 
 674         if (dataLen 
== 0){ //using DemodBuffer  
 675                 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all 
 676                         uint8_t ans 
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0); 
 678                                 if (!fcHigh
) fcHigh
=10; 
 684                 setDemodBuf(data
, dataLen
, 0); 
 687         //default if not found 
 688         if (clk 
== 0) clk 
= 50; 
 689         if (fcHigh 
== 0) fcHigh 
= 10; 
 690         if (fcLow 
== 0) fcLow 
= 8; 
 693         arg1 
= fcHigh 
<< 8 | fcLow
; 
 694         arg2 
= invert 
<< 8 | clk
; 
 695         size_t size 
= DemodBufferLen
; 
 696         if (size 
> USB_CMD_DATA_SIZE
) { 
 697                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 698                 size 
= USB_CMD_DATA_SIZE
; 
 700         UsbCommand c 
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 702         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 707 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator  
 708 // - allow pull data from DemodBuffer 
 709 int CmdLFaskSim(const char *Cmd
) 
 711         //autodetect clock from Graphbuffer if using demod buffer 
 712         // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream 
 713         uint8_t encoding 
= 1, separator 
= 0; 
 714         uint8_t clk
=0, invert
=0; 
 716         char hexData
[32] = {0x00};  
 717         uint8_t data
[255]= {0x00}; // store entered hex data 
 720         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 722                 switch(param_getchar(Cmd
, cmdp
)) 
 725                         return usage_lf_simask(); 
 731                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 735                         encoding
=2; //biphase 
 751                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 755                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 757                         if (dataLen
==0) errors
=TRUE
;  
 758                         if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
); 
 762                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 768         if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 770                 errors 
= TRUE
;// No args 
 776                 return usage_lf_simask(); 
 778         if (dataLen 
== 0){ //using DemodBuffer 
 779                 if (clk 
== 0) clk 
= GetAskClock("0", false, false); 
 781                 setDemodBuf(data
, dataLen
, 0); 
 783         if (clk 
== 0) clk 
= 64; 
 784         if (encoding 
== 0) clk 
= clk
/2; //askraw needs to double the clock speed 
 786         size_t size
=DemodBufferLen
; 
 787         arg1 
= clk 
<< 8 | encoding
; 
 788         arg2 
= invert 
<< 8 | separator
; 
 789         if (size 
> USB_CMD_DATA_SIZE
) { 
 790                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 791                 size 
= USB_CMD_DATA_SIZE
; 
 793         UsbCommand c 
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 794         PrintAndLog("preparing to sim ask data: %d bits", size
); 
 795         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 800 // by marshmellow - sim psk data given carrier, clock, invert  
 801 // - allow pull data from DemodBuffer or parameters 
 802 int CmdLFpskSim(const char *Cmd
) 
 804         //might be able to autodetect FC and clock from Graphbuffer if using demod buffer 
 805         //will need carrier, Clock, and bitstream 
 806         uint8_t carrier
=0, clk
=0; 
 809         char hexData
[32] = {0x00}; // store entered hex data 
 810         uint8_t data
[255] = {0x00};  
 814         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 816                 switch(param_getchar(Cmd
, cmdp
)) 
 819                         return usage_lf_simpsk(); 
 825                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 829                         errors 
|= param_getdec(Cmd
,cmdp
+1,&carrier
); 
 845                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 849                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 851                         if (dataLen
==0) errors
=TRUE
;  
 852                         if (errors
) PrintAndLog ("Error getting hex data"); 
 856                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 862         if (cmdp 
== 0 && DemodBufferLen 
== 0) 
 864                 errors 
= TRUE
;// No args 
 870                 return usage_lf_simpsk(); 
 872         if (dataLen 
== 0){ //using DemodBuffer 
 873                 PrintAndLog("Getting Clocks"); 
 874                 if (clk
==0) clk 
= GetPskClock("", FALSE
, FALSE
); 
 875                 PrintAndLog("clk: %d",clk
); 
 876                 if (!carrier
) carrier 
= GetPskCarrier("", FALSE
, FALSE
);  
 877                 PrintAndLog("carrier: %d", carrier
); 
 879                 setDemodBuf(data
, dataLen
, 0); 
 882         if (clk 
<= 0) clk 
= 32; 
 883         if (carrier 
== 0) carrier 
= 2; 
 886                         //need to convert psk2 to psk1 data before sim 
 887                         psk2TOpsk1(DemodBuffer
, DemodBufferLen
); 
 889                         PrintAndLog("Sorry, PSK3 not yet available"); 
 893         arg1 
= clk 
<< 8 | carrier
; 
 895         size_t size
=DemodBufferLen
; 
 896         if (size 
> USB_CMD_DATA_SIZE
) { 
 897                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 898                 size
=USB_CMD_DATA_SIZE
; 
 900         UsbCommand c 
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 901         PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
); 
 902         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 908 int CmdLFSimBidir(const char *Cmd
) 
 910         // Set ADC to twice the carrier for a slight supersampling 
 911         // HACK: not implemented in ARMSRC. 
 912         PrintAndLog("Not implemented yet."); 
 913         UsbCommand c 
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}}; 
 918 int CmdVchDemod(const char *Cmd
) 
 920         // Is this the entire sync pattern, or does this also include some 
 921         // data bits that happen to be the same everywhere? That would be 
 923         static const int SyncPattern
[] = { 
 924                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 925                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 926                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 927                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 928                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 929                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 930                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 931                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 932                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 933                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 936         // So first, we correlate for the sync pattern, and mark that. 
 937         int bestCorrel 
= 0, bestPos 
= 0; 
 939         // It does us no good to find the sync pattern, with fewer than 
 940         // 2048 samples after it... 
 941         for (i 
= 0; i 
< (GraphTraceLen
-2048); i
++) { 
 944                 for (j 
= 0; j 
< arraylen(SyncPattern
); j
++) { 
 945                         sum 
+= GraphBuffer
[i
+j
]*SyncPattern
[j
]; 
 947                 if (sum 
> bestCorrel
) { 
 952         PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
); 
 960         for (i 
= 0; i 
< 2048; i 
+= 8) { 
 963                 for (j 
= 0; j 
< 8; j
++) { 
 964                         sum 
+= GraphBuffer
[bestPos
+i
+j
]; 
 971                 if(abs(sum
) < worst
) { 
 976         PrintAndLog("bits:"); 
 977         PrintAndLog("%s", bits
); 
 978         PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
); 
 980         if (strcmp(Cmd
, "clone")==0) { 
 983                 for(s 
= bits
; *s
; s
++) { 
 985                         for(j 
= 0; j 
< 16; j
++) { 
 986                                 GraphBuffer
[GraphTraceLen
++] = (*s 
== '1') ? 1 : 0; 
 989                 RepaintGraphWindow(); 
 995 int CmdLFfind(const char *Cmd
) 
 998         char cmdp 
= param_getchar(Cmd
, 0); 
 999         char testRaw 
= param_getchar(Cmd
, 1); 
1000         if (strlen(Cmd
) > 3 || cmdp 
== 'h' || cmdp 
== 'H') { 
1001                 PrintAndLog("Usage:  lf search <0|1> [u]"); 
1002                 PrintAndLog("     <use data from Graphbuffer> , if not set, try reading data from tag."); 
1003                 PrintAndLog("     [Search for Unknown tags] , if not set, reads only known tags."); 
1005                 PrintAndLog("    sample: lf search     = try reading data from tag & search for known tags"); 
1006                 PrintAndLog("          : lf search 1   = use data from GraphBuffer & search for known tags"); 
1007                 PrintAndLog("          : lf search u   = try reading data from tag & search for known and unknown tags"); 
1008                 PrintAndLog("          : lf search 1 u = use data from GraphBuffer & search for known and unknown tags"); 
1013         if (!offline 
&& (cmdp 
!= '1')){ 
1015                 getSamples("30000",false); 
1016         } else if (GraphTraceLen 
< 1000) { 
1017                 PrintAndLog("Data in Graphbuffer was too small."); 
1020         if (cmdp 
== 'u' || cmdp 
== 'U') testRaw 
= 'u'; 
1022         PrintAndLog("NOTE: some demods output possible binary\n  if it finds something that looks like a tag"); 
1023         PrintAndLog("False Positives ARE possible\n");   
1024         PrintAndLog("\nChecking for known tags:\n"); 
1026         ans
=CmdFSKdemodIO(""); 
1028                 PrintAndLog("\nValid IO Prox ID Found!"); 
1032         ans
=CmdFSKdemodPyramid(""); 
1034                 PrintAndLog("\nValid Pyramid ID Found!"); 
1038         ans
=CmdFSKdemodParadox(""); 
1040                 PrintAndLog("\nValid Paradox ID Found!"); 
1044         ans
=CmdFSKdemodAWID(""); 
1046                 PrintAndLog("\nValid AWID ID Found!"); 
1050         ans
=CmdFSKdemodHID(""); 
1052                 PrintAndLog("\nValid HID Prox ID Found!"); 
1056         //add psk and indala 
1057         ans
=CmdIndalaDecode(""); 
1059                 PrintAndLog("\nValid Indala ID Found!"); 
1063         ans
=CmdAskEM410xDemod(""); 
1065                 PrintAndLog("\nValid EM410x ID Found!"); 
1069         ans
=CmdG_Prox_II_Demod(""); 
1071                 PrintAndLog("\nValid G Prox II ID Found!"); 
1075         ans
=CmdFDXBdemodBI(""); 
1077                 PrintAndLog("\nValid FDX-B ID Found!"); 
1081         ans
=EM4x50Read("", false); 
1083                 PrintAndLog("\nValid EM4x50 ID Found!"); 
1087         ans
=CmdPSKNexWatch(""); 
1089                 PrintAndLog("\nValid NexWatch ID Found!"); 
1093         PrintAndLog("\nNo Known Tags Found!\n"); 
1094         if (testRaw
=='u' || testRaw
=='U'){ 
1095                 //test unknown tag formats (raw mode) 
1096                 PrintAndLog("\nChecking for Unknown tags:\n"); 
1097                 ans
=AutoCorrelate(4000, FALSE
, FALSE
); 
1098                 if (ans 
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
); 
1099                 ans
=GetFskClock("",FALSE
,FALSE
);  
1100                 if (ans 
!= 0){ //fsk 
1101                         ans
=FSKrawDemod("",TRUE
); 
1103                                 PrintAndLog("\nUnknown FSK Modulated Tag Found!"); 
1107                 ans
=ASKDemod("0 0 0",TRUE
,FALSE
,1); 
1109                         PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!"); 
1110                         PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'"); 
1113                 ans
=CmdPSK1rawDemod(""); 
1115                         PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'"); 
1116                         PrintAndLog("\nCould also be PSK3 - [currently not supported]"); 
1117                         PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod"); 
1120                 PrintAndLog("\nNo Data Found!\n"); 
1125 static command_t CommandTable
[] =  
1127         {"help",        CmdHelp
,            1, "This help"}, 
1128         {"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)"}, 
1129         {"em4x",        CmdLFEM4X
,          1, "{ EM4X RFIDs... }"}, 
1130         {"config",      CmdLFSetConfig
,     0, "Set config for LF sampling, bit/sample, decimation, frequency"}, 
1131         {"flexdemod",   CmdFlexdemod
,       1, "Demodulate samples for FlexPass"}, 
1132         {"hid",         CmdLFHID
,           1, "{ HID RFIDs... }"}, 
1133         {"io",            CmdLFIO
,                1, "{ ioProx tags... }"}, 
1134         {"indalademod", CmdIndalaDemod
,     1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"}, 
1135         {"indalaclone", CmdIndalaClone
,     0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"}, 
1136         {"read",        CmdLFRead
,          0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"}, 
1137         {"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"}, 
1138         {"sim",         CmdLFSim
,           0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"}, 
1139         {"simask",      CmdLFaskSim
,        0, "[clock] [invert <1|0>] [manchester/raw <'m'|'r'>] [msg separator 's'] [d <hexdata>] -- Simulate LF ASK tag from demodbuffer or input"}, 
1140         {"simfsk",      CmdLFfskSim
,        0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"}, 
1141         {"simpsk",      CmdLFpskSim
,        0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"}, 
1142         {"simbidir",    CmdLFSimBidir
,      0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"}, 
1143         {"snoop",       CmdLFSnoop
,         0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"}, 
1144         {"ti",          CmdLFTI
,            1, "{ TI RFIDs... }"}, 
1145         {"hitag",       CmdLFHitag
,         1, "{ Hitag tags and transponders... }"}, 
1146         {"vchdemod",    CmdVchDemod
,        1, "['clone'] -- Demodulate samples for VeriChip"}, 
1147         {"t55xx",       CmdLFT55XX
,         1, "{ T55xx RFIDs... }"}, 
1148         {"pcf7931",     CmdLFPCF7931
,       1, "{PCF7931 RFIDs...}"}, 
1149         {NULL
, NULL
, 0, NULL
} 
1152 int CmdLF(const char *Cmd
) 
1154         CmdsParse(CommandTable
, Cmd
); 
1158 int CmdHelp(const char *Cmd
) 
1160         CmdsHelp(CommandTable
);