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" 
  17 #include "lfdemod.h"     // for psk2TOpsk1 
  18 #include "util.h"        // for parsing cli command utils 
  19 #include "ui.h"          // for show graph controls 
  20 #include "graph.h"       // for graph data 
  21 #include "cmdparser.h"   // for getting cli commands included in cmdmain.h 
  22 #include "cmdmain.h"     // for sending cmds to device 
  23 #include "data.h"        // for GetFromBigBuf 
  24 #include "cmddata.h"     // for `lf search` 
  25 #include "cmdlfawid.h"   // for awid menu 
  26 #include "cmdlfem4x.h"   // for em4x menu 
  27 #include "cmdlfhid.h"    // for hid menu 
  28 #include "cmdlfhitag.h"  // for hitag menu 
  29 #include "cmdlfio.h"     // for ioprox menu 
  30 #include "cmdlft55xx.h"  // for t55xx menu 
  31 #include "cmdlfti.h"     // for ti menu 
  32 #include "cmdlfpresco.h" // for presco menu 
  33 #include "cmdlfpcf7931.h"// for pcf7931 menu 
  34 #include "cmdlfpyramid.h"// for pyramid menu 
  35 #include "cmdlfviking.h" // for viking menu 
  36 #include "cmdlfcotag.h"  // for COTAG menu 
  37 #include "cmdlfvisa2000.h"  // for VISA2000 menu 
  38 #include "cmdlfindala.h" // for indala menu 
  39 #include "cmdlfgproxii.h"// for gproxii menu 
  40 #include "cmdlffdx.h"    // for fdx-b menu 
  42 bool g_lf_threshold_set 
= false; 
  43 static int CmdHelp(const char *Cmd
); 
  47 int usage_lf_cmdread(void) 
  49         PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H] "); 
  50         PrintAndLog("Options:        "); 
  51         PrintAndLog("       h             This help"); 
  52         PrintAndLog("       L             Low frequency (125 KHz)"); 
  53         PrintAndLog("       H             High frequency (134 KHz)"); 
  54         PrintAndLog("       d <delay>     delay OFF period"); 
  55         PrintAndLog("       z <zero>      time period ZERO"); 
  56         PrintAndLog("       o <one>       time period ONE"); 
  57         PrintAndLog("       c <cmd>       Command bytes"); 
  58         PrintAndLog("       ************* All periods in microseconds"); 
  59         PrintAndLog("Examples:"); 
  60         PrintAndLog("      lf cmdread d 80 z 100 o 200 c 11000"); 
  61         PrintAndLog("      lf cmdread d 80 z 100 o 100 c 11000 H"); 
  65 /* send a command before reading */ 
  66 int CmdLFCommandRead(const char *Cmd
) 
  68         static char dummy
[3] = {0x20,0x00,0x00}; 
  69         UsbCommand c 
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
}; 
  71         //uint8_t divisor = 95; //125khz 
  73         while(param_getchar(Cmd
, cmdp
) != 0x00) 
  75                 switch(param_getchar(Cmd
, cmdp
)) 
  78                         return usage_lf_cmdread(); 
  88                         param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
); 
  92                         c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10); 
  96                         c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10); 
 100                         c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10); 
 104                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 111         if(cmdp 
== 0) errors 
= 1; 
 114         if(errors
) return usage_lf_cmdread(); 
 116         // in case they specified 'H' 
 117         strcpy((char *)&c
.d
.asBytes 
+ strlen((char *)c
.d
.asBytes
), dummy
); 
 119         clearCommandBuffer(); 
 124 int CmdFlexdemod(const char *Cmd
) 
 127         for (i 
= 0; i 
< GraphTraceLen
; ++i
) { 
 128                 if (GraphBuffer
[i
] < 0) { 
 135  #define LONG_WAIT 100 
 137         for (start 
= 0; start 
< GraphTraceLen 
- LONG_WAIT
; start
++) { 
 138                 int first 
= GraphBuffer
[start
]; 
 139                 for (i 
= start
; i 
< start 
+ LONG_WAIT
; i
++) { 
 140                         if (GraphBuffer
[i
] != first
) { 
 144                 if (i 
== (start 
+ LONG_WAIT
)) { 
 148         if (start 
== GraphTraceLen 
- LONG_WAIT
) { 
 149                 PrintAndLog("nothing to wait for"); 
 153         GraphBuffer
[start
] = 2; 
 154         GraphBuffer
[start
+1] = -2; 
 155         uint8_t bits
[64] = {0x00}; 
 159         for (bit 
= 0; bit 
< 64; bit
++) { 
 161                 for (int j 
= 0; j 
< 16; j
++) { 
 162                         sum 
+= GraphBuffer
[i
++]; 
 165                 bits
[bit
] = (sum 
> 0) ? 1 : 0; 
 167                 PrintAndLog("bit %d sum %d", bit
, sum
); 
 170         for (bit 
= 0; bit 
< 64; bit
++) { 
 173                 for (j 
= 0; j 
< 16; j
++) { 
 174                         sum 
+= GraphBuffer
[i
++]; 
 176                 if (sum 
> 0 && bits
[bit
] != 1) { 
 177                         PrintAndLog("oops1 at %d", bit
); 
 179                 if (sum 
< 0 && bits
[bit
] != 0) { 
 180                         PrintAndLog("oops2 at %d", bit
); 
 184         // HACK writing back to graphbuffer. 
 185         GraphTraceLen 
= 32*64; 
 188         for (bit 
= 0; bit 
< 64; bit
++) { 
 190                 phase 
= (bits
[bit
] == 0) ? 0 : 1; 
 193                 for (j 
= 0; j 
< 32; j
++) { 
 194                         GraphBuffer
[i
++] = phase
; 
 199         RepaintGraphWindow(); 
 203 int usage_lf_read(void) 
 205         PrintAndLog("Usage: lf read"); 
 206         PrintAndLog("Options:        "); 
 207         PrintAndLog("       h            This help"); 
 208         PrintAndLog("       s            silent run no printout"); 
 209         PrintAndLog("This function takes no arguments. "); 
 210         PrintAndLog("Use 'lf config' to set parameters."); 
 213 int usage_lf_snoop(void) 
 215         PrintAndLog("Usage: lf snoop"); 
 216         PrintAndLog("Options:        "); 
 217         PrintAndLog("       h            This help"); 
 218         PrintAndLog("This function takes no arguments. "); 
 219         PrintAndLog("Use 'lf config' to set parameters."); 
 223 int usage_lf_config(void) 
 225         PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]"); 
 226         PrintAndLog("Options:        "); 
 227         PrintAndLog("       h             This help"); 
 228         PrintAndLog("       L             Low frequency (125 KHz)"); 
 229         PrintAndLog("       H             High frequency (134 KHz)"); 
 230         PrintAndLog("       q <divisor>   Manually set divisor. 88-> 134KHz, 95-> 125 Hz"); 
 231         PrintAndLog("       b <bps>       Sets resolution of bits per sample. Default (max): 8"); 
 232         PrintAndLog("       d <decim>     Sets decimation. A value of N saves only 1 in N samples. Default: 1"); 
 233         PrintAndLog("       a [0|1]       Averaging - if set, will average the stored sample value when decimating. Default: 1"); 
 234         PrintAndLog("       t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)"); 
 235         PrintAndLog("Examples:"); 
 236         PrintAndLog("      lf config b 8 L"); 
 237         PrintAndLog("                    Samples at 125KHz, 8bps."); 
 238         PrintAndLog("      lf config H b 4 d 3"); 
 239         PrintAndLog("                    Samples at 134KHz, averages three samples into one, stored with "); 
 240         PrintAndLog("                    a resolution of 4 bits per sample."); 
 241         PrintAndLog("      lf read"); 
 242         PrintAndLog("                    Performs a read (active field)"); 
 243         PrintAndLog("      lf snoop"); 
 244         PrintAndLog("                    Performs a snoop (no active field)"); 
 248 int CmdLFSetConfig(const char *Cmd
) 
 251         uint8_t divisor 
=  0;//Frequency divisor 
 252         uint8_t bps 
= 0; // Bits per sample 
 253         uint8_t decimation 
= 0; //How many to keep 
 254         bool averaging 
= 1; // Defaults to true 
 256         int trigger_threshold 
=-1;//Means no change 
 257         uint8_t unsigned_trigg 
= 0; 
 260         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 262                 switch(param_getchar(Cmd
, cmdp
)) 
 265                         return usage_lf_config(); 
 275                         errors 
|= param_getdec(Cmd
,cmdp
+1,&divisor
); 
 279                         errors 
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
); 
 282                                 trigger_threshold 
= unsigned_trigg
; 
 283                                 if (trigger_threshold 
> 0) g_lf_threshold_set 
= true; 
 287                         errors 
|= param_getdec(Cmd
,cmdp
+1,&bps
); 
 291                         errors 
|= param_getdec(Cmd
,cmdp
+1,&decimation
); 
 295                         averaging 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 299                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 307                 errors 
= 1;// No args 
 313                 return usage_lf_config(); 
 315         //Bps is limited to 8, so fits in lower half of arg1 
 316         if(bps 
>> 4) bps 
= 8; 
 318         sample_config config 
= { 
 319                 decimation
,bps
,averaging
,divisor
,trigger_threshold
 
 321         //Averaging is a flag on high-bit of arg[1] 
 322         UsbCommand c 
= {CMD_SET_LF_SAMPLING_CONFIG
}; 
 323         memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
)); 
 324         clearCommandBuffer(); 
 329 int CmdLFRead(const char *Cmd
) 
 331         if (offline
) return 0; 
 334         if (param_getchar(Cmd
, cmdp
) == 'h') 
 336                 return usage_lf_read(); 
 338         if (param_getchar(Cmd
, cmdp
) == 's') arg1 
= true; //suppress print 
 339         //And ship it to device 
 340         UsbCommand c 
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}}; 
 341         clearCommandBuffer(); 
 343         if (g_lf_threshold_set
) { 
 344                 WaitForResponse(CMD_ACK
,NULL
); 
 346                 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
 347                         PrintAndLog("command execution time out"); 
 354 int CmdLFSnoop(const char *Cmd
) 
 357         if(param_getchar(Cmd
, cmdp
) == 'h') 
 359                 return usage_lf_snoop(); 
 362         UsbCommand c 
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
}; 
 363         clearCommandBuffer(); 
 365         WaitForResponse(CMD_ACK
,NULL
); 
 369 static void ChkBitstream(const char *str
) 
 373         /* convert to bitstream if necessary */ 
 374         for (i 
= 0; i 
< (int)(GraphTraceLen 
/ 2); i
++){ 
 375                 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) { 
 381 //Attempt to simulate any wave in buffer (one bit per output sample) 
 382 // converts GraphBuffer to bitstream (based on zero crossings) if needed. 
 383 int CmdLFSim(const char *Cmd
) 
 388         sscanf(Cmd
, "%i", &gap
); 
 390         // convert to bitstream if necessary  
 394         //can send only 512 bits at a time (1 byte sent per bit...) 
 395         printf("Sending [%d bytes]", GraphTraceLen
); 
 396         for (i 
= 0; i 
< GraphTraceLen
; i 
+= USB_CMD_DATA_SIZE
) { 
 397                 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}}; 
 399                 for (j 
= 0; j 
< USB_CMD_DATA_SIZE
; j
++) { 
 400                         c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
]; 
 403                 WaitForResponse(CMD_ACK
,NULL
); 
 408         PrintAndLog("Starting to simulate"); 
 409         UsbCommand c 
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}}; 
 410         clearCommandBuffer(); 
 415 int usage_lf_simfsk(void) 
 418         PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]"); 
 419         PrintAndLog("Options:        "); 
 420         PrintAndLog("       h              This help"); 
 421         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 422         PrintAndLog("       i              invert data"); 
 423         PrintAndLog("       H <fcHigh>     Manually set the larger Field Clock"); 
 424         PrintAndLog("       L <fcLow>      Manually set the smaller Field Clock"); 
 425         //PrintAndLog("       s              TBD- -to enable a gap between playback repetitions - default: no gap"); 
 426         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 427         PrintAndLog("\n  NOTE: if you set one clock manually set them all manually"); 
 431 int usage_lf_simask(void) 
 434         PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]"); 
 435         PrintAndLog("Options:        "); 
 436         PrintAndLog("       h              This help"); 
 437         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 438         PrintAndLog("       i              invert data"); 
 439         PrintAndLog("       b              sim ask/biphase"); 
 440         PrintAndLog("       m              sim ask/manchester - Default"); 
 441         PrintAndLog("       r              sim ask/raw"); 
 442         PrintAndLog("       s              add t55xx Sequence Terminator gap - default: no gaps (only manchester)"); 
 443         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 447 int usage_lf_simpsk(void) 
 450         PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]"); 
 451         PrintAndLog("Options:        "); 
 452         PrintAndLog("       h              This help"); 
 453         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 454         PrintAndLog("       i              invert data"); 
 455         PrintAndLog("       1              set PSK1 (default)"); 
 456         PrintAndLog("       2              set PSK2"); 
 457         PrintAndLog("       3              set PSK3"); 
 458         PrintAndLog("       r <carrier>    2|4|8 are valid carriers: default = 2"); 
 459         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 463 // by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert  
 464 // - allow pull data from DemodBuffer 
 465 int CmdLFfskSim(const char *Cmd
) 
 467         //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer 
 468         // otherwise will need FChigh, FClow, Clock, and bitstream 
 469         uint8_t fcHigh
=0, fcLow
=0, clk
=0; 
 472         char hexData
[32] = {0x00}; // store entered hex data 
 473         uint8_t data
[255] = {0x00};  
 476         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 478                 switch(param_getchar(Cmd
, cmdp
)) 
 481                         return usage_lf_simfsk(); 
 487                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 491                         errors 
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
); 
 495                         errors 
|= param_getdec(Cmd
,cmdp
+1,&fcLow
); 
 503                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 507                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 509                         if (dataLen
==0) errors
=true;  
 510                         if (errors
) PrintAndLog ("Error getting hex data"); 
 514                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 520         if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 522                 errors 
= true;// No args 
 528                 return usage_lf_simfsk(); 
 531         if (dataLen 
== 0){ //using DemodBuffer  
 532                 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all 
 533                         uint8_t ans 
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0); 
 535                                 if (!fcHigh
) fcHigh
=10; 
 541                 setDemodBuf(data
, dataLen
, 0); 
 544         //default if not found 
 545         if (clk 
== 0) clk 
= 50; 
 546         if (fcHigh 
== 0) fcHigh 
= 10; 
 547         if (fcLow 
== 0) fcLow 
= 8; 
 550         arg1 
= fcHigh 
<< 8 | fcLow
; 
 551         arg2 
= invert 
<< 8 | clk
; 
 552         size_t size 
= DemodBufferLen
; 
 553         if (size 
> USB_CMD_DATA_SIZE
) { 
 554                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 555                 size 
= USB_CMD_DATA_SIZE
; 
 557         UsbCommand c 
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 559         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 560         clearCommandBuffer(); 
 565 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator  
 566 // - allow pull data from DemodBuffer 
 567 int CmdLFaskSim(const char *Cmd
) 
 569         //autodetect clock from Graphbuffer if using demod buffer 
 570         // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream 
 571         uint8_t encoding 
= 1, separator 
= 0; 
 572         uint8_t clk
=0, invert
=0; 
 574         char hexData
[32] = {0x00};  
 575         uint8_t data
[255]= {0x00}; // store entered hex data 
 578         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 580                 switch(param_getchar(Cmd
, cmdp
)) 
 583                         return usage_lf_simask(); 
 589                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 593                         encoding
=2; //biphase 
 609                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 613                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 615                         if (dataLen
==0) errors
=true;  
 616                         if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
); 
 620                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 626         if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 628                 errors 
= true;// No args 
 634                 return usage_lf_simask(); 
 636         if (dataLen 
== 0){ //using DemodBuffer 
 637                 if (clk 
== 0) clk 
= GetAskClock("0", false, false); 
 639                 setDemodBuf(data
, dataLen
, 0); 
 641         if (clk 
== 0) clk 
= 64; 
 642         if (encoding 
== 0) clk 
= clk
/2; //askraw needs to double the clock speed 
 644         size_t size
=DemodBufferLen
; 
 645         arg1 
= clk 
<< 8 | encoding
; 
 646         arg2 
= invert 
<< 8 | separator
; 
 647         if (size 
> USB_CMD_DATA_SIZE
) { 
 648                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 649                 size 
= USB_CMD_DATA_SIZE
; 
 651         UsbCommand c 
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 652         PrintAndLog("preparing to sim ask data: %d bits", size
); 
 653         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 654         clearCommandBuffer(); 
 659 // by marshmellow - sim psk data given carrier, clock, invert  
 660 // - allow pull data from DemodBuffer or parameters 
 661 int CmdLFpskSim(const char *Cmd
) 
 663         //might be able to autodetect FC and clock from Graphbuffer if using demod buffer 
 664         //will need carrier, Clock, and bitstream 
 665         uint8_t carrier
=0, clk
=0; 
 668         char hexData
[32] = {0x00}; // store entered hex data 
 669         uint8_t data
[255] = {0x00};  
 673         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 675                 switch(param_getchar(Cmd
, cmdp
)) 
 678                         return usage_lf_simpsk(); 
 684                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 688                         errors 
|= param_getdec(Cmd
,cmdp
+1,&carrier
); 
 704                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 708                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 710                         if (dataLen
==0) errors
=true;  
 711                         if (errors
) PrintAndLog ("Error getting hex data"); 
 715                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 721         if (cmdp 
== 0 && DemodBufferLen 
== 0) 
 723                 errors 
= true;// No args 
 729                 return usage_lf_simpsk(); 
 731         if (dataLen 
== 0){ //using DemodBuffer 
 732                 PrintAndLog("Getting Clocks"); 
 733                 if (clk
==0) clk 
= GetPskClock("", false, false); 
 734                 PrintAndLog("clk: %d",clk
); 
 735                 if (!carrier
) carrier 
= GetPskCarrier("", false, false);  
 736                 PrintAndLog("carrier: %d", carrier
); 
 738                 setDemodBuf(data
, dataLen
, 0); 
 741         if (clk 
<= 0) clk 
= 32; 
 742         if (carrier 
== 0) carrier 
= 2; 
 745                         //need to convert psk2 to psk1 data before sim 
 746                         psk2TOpsk1(DemodBuffer
, DemodBufferLen
); 
 748                         PrintAndLog("Sorry, PSK3 not yet available"); 
 752         arg1 
= clk 
<< 8 | carrier
; 
 754         size_t size
=DemodBufferLen
; 
 755         if (size 
> USB_CMD_DATA_SIZE
) { 
 756                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 757                 size
=USB_CMD_DATA_SIZE
; 
 759         UsbCommand c 
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 760         PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
); 
 761         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 762         clearCommandBuffer(); 
 768 int CmdLFSimBidir(const char *Cmd
) 
 770         // Set ADC to twice the carrier for a slight supersampling 
 771         // HACK: not implemented in ARMSRC. 
 772         PrintAndLog("Not implemented yet."); 
 773         UsbCommand c 
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}}; 
 778 int CmdVchDemod(const char *Cmd
) 
 780         // Is this the entire sync pattern, or does this also include some 
 781         // data bits that happen to be the same everywhere? That would be 
 783         static const int SyncPattern
[] = { 
 784                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 785                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 786                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 787                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 788                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 789                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 790                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 791                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 792                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 793                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 796         // So first, we correlate for the sync pattern, and mark that. 
 797         int bestCorrel 
= 0, bestPos 
= 0; 
 799         // It does us no good to find the sync pattern, with fewer than 
 800         // 2048 samples after it... 
 801         for (i 
= 0; i 
< (GraphTraceLen
-2048); i
++) { 
 804                 for (j 
= 0; j 
< arraylen(SyncPattern
); j
++) { 
 805                         sum 
+= GraphBuffer
[i
+j
]*SyncPattern
[j
]; 
 807                 if (sum 
> bestCorrel
) { 
 812         PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
); 
 820         for (i 
= 0; i 
< 2048; i 
+= 8) { 
 823                 for (j 
= 0; j 
< 8; j
++) { 
 824                         sum 
+= GraphBuffer
[bestPos
+i
+j
]; 
 831                 if(abs(sum
) < worst
) { 
 836         PrintAndLog("bits:"); 
 837         PrintAndLog("%s", bits
); 
 838         PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
); 
 840         if (strcmp(Cmd
, "clone")==0) { 
 843                 for(s 
= bits
; *s
; s
++) { 
 845                         for(j 
= 0; j 
< 16; j
++) { 
 846                                 GraphBuffer
[GraphTraceLen
++] = (*s 
== '1') ? 1 : 0; 
 849                 RepaintGraphWindow(); 
 856 int CheckChipType(char cmdp
) { 
 857         uint32_t wordData 
= 0; 
 859         //check for em4x05/em4x69 chips first 
 861         if ((!offline 
&& (cmdp 
!= '1')) && EM4x05Block0Test(&wordData
)) { 
 862                 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n"); 
 867         //TODO check for t55xx chip... 
 874 int CmdLFfind(const char *Cmd
) 
 876         uint32_t wordData 
= 0; 
 878         size_t minLength 
= 1000; 
 879         char cmdp 
= param_getchar(Cmd
, 0); 
 880         char testRaw 
= param_getchar(Cmd
, 1); 
 881         if (strlen(Cmd
) > 3 || cmdp 
== 'h' || cmdp 
== 'H') { 
 882                 PrintAndLog("Usage:  lf search <0|1> [u]"); 
 883                 PrintAndLog("     <use data from Graphbuffer> , if not set, try reading data from tag."); 
 884                 PrintAndLog("     [Search for Unknown tags] , if not set, reads only known tags."); 
 886                 PrintAndLog("    sample: lf search     = try reading data from tag & search for known tags"); 
 887                 PrintAndLog("          : lf search 1   = use data from GraphBuffer & search for known tags"); 
 888                 PrintAndLog("          : lf search u   = try reading data from tag & search for known and unknown tags"); 
 889                 PrintAndLog("          : lf search 1 u = use data from GraphBuffer & search for known and unknown tags"); 
 894         if (!offline 
&& (cmdp 
!= '1')){ 
 896                 getSamples("30000",false); 
 897         } else if (GraphTraceLen 
< minLength
) { 
 898                 PrintAndLog("Data in Graphbuffer was too small."); 
 901         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"); 
 907         size_t testLen 
= minLength
; 
 908         // only run if graphbuffer is just noise as it should be for hitag/cotag 
 909         if (graphJustNoise(GraphBuffer
, testLen
)) { 
 910                 // only run these tests if we are in online mode  
 911                 if (!offline 
&& (cmdp 
!= '1')) { 
 912                         // test for em4x05 in reader talk first mode. 
 913                         if (EM4x05Block0Test(&wordData
)) { 
 914                                 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n"); 
 917                         ans
=CmdLFHitagReader("26"); 
 921                         ans
=CmdCOTAGRead(""); 
 923                                 PrintAndLog("\nValid COTAG ID Found!"); 
 930         ans
=CmdFSKdemodIO(""); 
 932                 PrintAndLog("\nValid IO Prox ID Found!"); 
 933                 return CheckChipType(cmdp
); 
 936         ans
=CmdFSKdemodPyramid(""); 
 938                 PrintAndLog("\nValid Pyramid ID Found!"); 
 939                 return CheckChipType(cmdp
); 
 942         ans
=CmdFSKdemodParadox(""); 
 944                 PrintAndLog("\nValid Paradox ID Found!"); 
 945                 return CheckChipType(cmdp
); 
 948         ans
=CmdFSKdemodAWID(""); 
 950                 PrintAndLog("\nValid AWID ID Found!"); 
 951                 return CheckChipType(cmdp
); 
 954         ans
=CmdFSKdemodHID(""); 
 956                 PrintAndLog("\nValid HID Prox ID Found!"); 
 957                 return CheckChipType(cmdp
); 
 960         ans
=CmdAskEM410xDemod(""); 
 962                 PrintAndLog("\nValid EM410x ID Found!"); 
 963                 return CheckChipType(cmdp
); 
 966         ans
=CmdVisa2kDemod(""); 
 968                 PrintAndLog("\nValid Visa2000 ID Found!"); 
 969                 return CheckChipType(cmdp
);              
 972         ans
=CmdG_Prox_II_Demod(""); 
 974                 PrintAndLog("\nValid G Prox II ID Found!"); 
 975                 return CheckChipType(cmdp
); 
 980                 PrintAndLog("\nValid FDX-B ID Found!"); 
 981                 return CheckChipType(cmdp
); 
 984         ans
=EM4x50Read("", false); 
 986                 PrintAndLog("\nValid EM4x50 ID Found!"); 
 990         ans
=CmdVikingDemod(""); 
 992                 PrintAndLog("\nValid Viking ID Found!"); 
 993                 return CheckChipType(cmdp
); 
 996         ans
=CmdIndalaDecode(""); 
 998                 PrintAndLog("\nValid Indala ID Found!"); 
 999                 return CheckChipType(cmdp
); 
1002         ans
=CmdPSKNexWatch(""); 
1004                 PrintAndLog("\nValid NexWatch ID Found!"); 
1005                 return CheckChipType(cmdp
); 
1008         PrintAndLog("\nNo Known Tags Found!\n"); 
1009         if (testRaw
=='u' || testRaw
=='U'){ 
1010                 ans
=CheckChipType(cmdp
); 
1011                 //test unknown tag formats (raw mode)0 
1012                 PrintAndLog("\nChecking for Unknown tags:\n"); 
1013                 ans
=AutoCorrelate(4000, false, false); 
1014                 if (ans 
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
); 
1015                 ans
=GetFskClock("",false,false);  
1016                 if (ans 
!= 0){ //fsk 
1017                         ans
=FSKrawDemod("",true); 
1019                                 PrintAndLog("\nUnknown FSK Modulated Tag Found!"); 
1024                 ans
=ASKDemod_ext("0 0 0",true,false,1,&st
); 
1026                         PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!"); 
1027                         PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'"); 
1030                 ans
=CmdPSK1rawDemod(""); 
1032                         PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'"); 
1033                         PrintAndLog("\nCould also be PSK3 - [currently not supported]"); 
1034                         PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod"); 
1037                 PrintAndLog("\nNo Data Found!\n"); 
1042 static command_t CommandTable
[] =  
1044         {"help",        CmdHelp
,            1, "This help"}, 
1045         {"awid",        CmdLFAWID
,          1, "{ AWID RFIDs...    }"}, 
1046         {"cotag",       CmdLFCOTAG
,         1, "{ COTAG RFIDs...   }"}, 
1047         {"em",          CmdLFEM4X
,          1, "{ EM4X RFIDs...    }"}, 
1048         {"fdx",         CmdLFFdx
,           1, "{ FDX-B tags...    }"}, 
1049         {"gproxii",     CmdLF_G_Prox_II
,    1, "{ G Prox II tags...    }"}, 
1050         {"hid",         CmdLFHID
,           1, "{ HID RFIDs...     }"}, 
1051         {"hitag",       CmdLFHitag
,         1, "{ Hitag tags and transponders... }"}, 
1052         {"io",          CmdLFIO
,            1, "{ ioProx tags...   }"}, 
1053         {"indala",      CmdLFINDALA
,        1, "{ Indala tags...   }"}, 
1054         {"presco",      CmdLFPresco
,        1, "{ Presco RFIDs...  }"}, 
1055         {"pcf7931",     CmdLFPCF7931
,       1, "{ PCF7931 RFIDs... }"}, 
1056         {"pyramid",     CmdLFPyramid
,       1, "{ Farpointe/Pyramid RFIDs... }"}, 
1057         {"t55xx",       CmdLFT55XX
,         1, "{ T55xx RFIDs...   }"}, 
1058         {"ti",          CmdLFTI
,            1, "{ TI RFIDs...      }"}, 
1059         {"viking",      CmdLFViking
,        1, "{ Viking tags...   }"}, 
1060         {"visa2000",    CmdLFVisa2k
,        1, "{ Visa2000 RFIDs...}"}, 
1061         {"cmdread",     CmdLFCommandRead
,   0, "<d period> <z period> <o period> <c command> ['H'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'H' for 134)"}, 
1062         {"config",      CmdLFSetConfig
,     0, "Set config for LF sampling, bit/sample, decimation, frequency"}, 
1063         {"flexdemod",   CmdFlexdemod
,       1, "Demodulate samples for FlexPass"}, 
1064         {"read",        CmdLFRead
,          0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"}, 
1065         {"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"}, 
1066         {"sim",         CmdLFSim
,           0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"}, 
1067         {"simask",      CmdLFaskSim
,        0, "[clock] [invert <1|0>] [biphase/manchester/raw <'b'|'m'|'r'>] [msg separator 's'] [d <hexdata>] -- Simulate LF ASK tag from demodbuffer or input"}, 
1068         {"simfsk",      CmdLFfskSim
,        0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"}, 
1069         {"simpsk",      CmdLFpskSim
,        0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"}, 
1070         {"simbidir",    CmdLFSimBidir
,      0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"}, 
1071         {"snoop",       CmdLFSnoop
,         0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"}, 
1072         {"vchdemod",    CmdVchDemod
,        1, "['clone'] -- Demodulate samples for VeriChip"}, 
1073         {NULL
, NULL
, 0, NULL
} 
1076 int CmdLF(const char *Cmd
) 
1078         CmdsParse(CommandTable
, Cmd
); 
1082 int CmdHelp(const char *Cmd
) 
1084         CmdsHelp(CommandTable
);