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 
  40 bool g_lf_threshold_set 
= false; 
  41 static int CmdHelp(const char *Cmd
); 
  45 int usage_lf_cmdread(void) 
  47         PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H] "); 
  48         PrintAndLog("Options:        "); 
  49         PrintAndLog("       h             This help"); 
  50         PrintAndLog("       L             Low frequency (125 KHz)"); 
  51         PrintAndLog("       H             High frequency (134 KHz)"); 
  52         PrintAndLog("       d <delay>     delay OFF period"); 
  53         PrintAndLog("       z <zero>      time period ZERO"); 
  54         PrintAndLog("       o <one>       time period ONE"); 
  55         PrintAndLog("       c <cmd>       Command bytes"); 
  56         PrintAndLog("       ************* All periods in microseconds"); 
  57         PrintAndLog("Examples:"); 
  58         PrintAndLog("      lf cmdread d 80 z 100 o 200 c 11000"); 
  59         PrintAndLog("      lf cmdread d 80 z 100 o 100 c 11000 H"); 
  63 /* send a command before reading */ 
  64 int CmdLFCommandRead(const char *Cmd
) 
  66         static char dummy
[3] = {0x20,0x00,0x00}; 
  67         UsbCommand c 
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
}; 
  69         //uint8_t divisor = 95; //125khz 
  71         while(param_getchar(Cmd
, cmdp
) != 0x00) 
  73                 switch(param_getchar(Cmd
, cmdp
)) 
  76                         return usage_lf_cmdread(); 
  86                         param_getstr(Cmd
, cmdp
+1, (char *)&c
.d
.asBytes
); 
  90                         c
.arg
[0] = param_get32ex(Cmd
, cmdp
+1, 0, 10); 
  94                         c
.arg
[1] = param_get32ex(Cmd
, cmdp
+1, 0, 10); 
  98                         c
.arg
[2] = param_get32ex(Cmd
, cmdp
+1, 0, 10); 
 102                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 109         if(cmdp 
== 0) errors 
= 1; 
 112         if(errors
) return usage_lf_cmdread(); 
 114         // in case they specified 'H' 
 115         strcpy((char *)&c
.d
.asBytes 
+ strlen((char *)c
.d
.asBytes
), dummy
); 
 117         clearCommandBuffer(); 
 122 int CmdFlexdemod(const char *Cmd
) 
 125         for (i 
= 0; i 
< GraphTraceLen
; ++i
) { 
 126                 if (GraphBuffer
[i
] < 0) { 
 133  #define LONG_WAIT 100 
 135         for (start 
= 0; start 
< GraphTraceLen 
- LONG_WAIT
; start
++) { 
 136                 int first 
= GraphBuffer
[start
]; 
 137                 for (i 
= start
; i 
< start 
+ LONG_WAIT
; i
++) { 
 138                         if (GraphBuffer
[i
] != first
) { 
 142                 if (i 
== (start 
+ LONG_WAIT
)) { 
 146         if (start 
== GraphTraceLen 
- LONG_WAIT
) { 
 147                 PrintAndLog("nothing to wait for"); 
 151         GraphBuffer
[start
] = 2; 
 152         GraphBuffer
[start
+1] = -2; 
 153         uint8_t bits
[64] = {0x00}; 
 157         for (bit 
= 0; bit 
< 64; bit
++) { 
 159                 for (int j 
= 0; j 
< 16; j
++) { 
 160                         sum 
+= GraphBuffer
[i
++]; 
 163                 bits
[bit
] = (sum 
> 0) ? 1 : 0; 
 165                 PrintAndLog("bit %d sum %d", bit
, sum
); 
 168         for (bit 
= 0; bit 
< 64; bit
++) { 
 171                 for (j 
= 0; j 
< 16; j
++) { 
 172                         sum 
+= GraphBuffer
[i
++]; 
 174                 if (sum 
> 0 && bits
[bit
] != 1) { 
 175                         PrintAndLog("oops1 at %d", bit
); 
 177                 if (sum 
< 0 && bits
[bit
] != 0) { 
 178                         PrintAndLog("oops2 at %d", bit
); 
 182         // HACK writing back to graphbuffer. 
 183         GraphTraceLen 
= 32*64; 
 186         for (bit 
= 0; bit 
< 64; bit
++) { 
 188                 phase 
= (bits
[bit
] == 0) ? 0 : 1; 
 191                 for (j 
= 0; j 
< 32; j
++) { 
 192                         GraphBuffer
[i
++] = phase
; 
 197         RepaintGraphWindow(); 
 201 int usage_lf_read(void) 
 203         PrintAndLog("Usage: lf read"); 
 204         PrintAndLog("Options:        "); 
 205         PrintAndLog("       h            This help"); 
 206         PrintAndLog("       s            silent run no printout"); 
 207         PrintAndLog("This function takes no arguments. "); 
 208         PrintAndLog("Use 'lf config' to set parameters."); 
 211 int usage_lf_snoop(void) 
 213         PrintAndLog("Usage: lf snoop"); 
 214         PrintAndLog("Options:        "); 
 215         PrintAndLog("       h            This help"); 
 216         PrintAndLog("This function takes no arguments. "); 
 217         PrintAndLog("Use 'lf config' to set parameters."); 
 221 int usage_lf_config(void) 
 223         PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]"); 
 224         PrintAndLog("Options:        "); 
 225         PrintAndLog("       h             This help"); 
 226         PrintAndLog("       L             Low frequency (125 KHz)"); 
 227         PrintAndLog("       H             High frequency (134 KHz)"); 
 228         PrintAndLog("       q <divisor>   Manually set divisor. 88-> 134KHz, 95-> 125 Hz"); 
 229         PrintAndLog("       b <bps>       Sets resolution of bits per sample. Default (max): 8"); 
 230         PrintAndLog("       d <decim>     Sets decimation. A value of N saves only 1 in N samples. Default: 1"); 
 231         PrintAndLog("       a [0|1]       Averaging - if set, will average the stored sample value when decimating. Default: 1"); 
 232         PrintAndLog("       t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)"); 
 233         PrintAndLog("Examples:"); 
 234         PrintAndLog("      lf config b 8 L"); 
 235         PrintAndLog("                    Samples at 125KHz, 8bps."); 
 236         PrintAndLog("      lf config H b 4 d 3"); 
 237         PrintAndLog("                    Samples at 134KHz, averages three samples into one, stored with "); 
 238         PrintAndLog("                    a resolution of 4 bits per sample."); 
 239         PrintAndLog("      lf read"); 
 240         PrintAndLog("                    Performs a read (active field)"); 
 241         PrintAndLog("      lf snoop"); 
 242         PrintAndLog("                    Performs a snoop (no active field)"); 
 246 int CmdLFSetConfig(const char *Cmd
) 
 249         uint8_t divisor 
=  0;//Frequency divisor 
 250         uint8_t bps 
= 0; // Bits per sample 
 251         uint8_t decimation 
= 0; //How many to keep 
 252         bool averaging 
= 1; // Defaults to true 
 254         int trigger_threshold 
=-1;//Means no change 
 255         uint8_t unsigned_trigg 
= 0; 
 258         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 260                 switch(param_getchar(Cmd
, cmdp
)) 
 263                         return usage_lf_config(); 
 273                         errors 
|= param_getdec(Cmd
,cmdp
+1,&divisor
); 
 277                         errors 
|= param_getdec(Cmd
,cmdp
+1,&unsigned_trigg
); 
 280                                 trigger_threshold 
= unsigned_trigg
; 
 281                                 if (trigger_threshold 
> 0) g_lf_threshold_set 
= true; 
 285                         errors 
|= param_getdec(Cmd
,cmdp
+1,&bps
); 
 289                         errors 
|= param_getdec(Cmd
,cmdp
+1,&decimation
); 
 293                         averaging 
= param_getchar(Cmd
,cmdp
+1) == '1'; 
 297                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 305                 errors 
= 1;// No args 
 311                 return usage_lf_config(); 
 313         //Bps is limited to 8, so fits in lower half of arg1 
 314         if(bps 
>> 4) bps 
= 8; 
 316         sample_config config 
= { 
 317                 decimation
,bps
,averaging
,divisor
,trigger_threshold
 
 319         //Averaging is a flag on high-bit of arg[1] 
 320         UsbCommand c 
= {CMD_SET_LF_SAMPLING_CONFIG
}; 
 321         memcpy(c
.d
.asBytes
,&config
,sizeof(sample_config
)); 
 322         clearCommandBuffer(); 
 327 int CmdLFRead(const char *Cmd
) 
 329         if (offline
) return 0; 
 332         if (param_getchar(Cmd
, cmdp
) == 'h') 
 334                 return usage_lf_read(); 
 336         if (param_getchar(Cmd
, cmdp
) == 's') arg1 
= true; //suppress print 
 337         //And ship it to device 
 338         UsbCommand c 
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
, {arg1
,0,0}}; 
 339         clearCommandBuffer(); 
 341         if (g_lf_threshold_set
) { 
 342                 WaitForResponse(CMD_ACK
,NULL
); 
 344                 if ( !WaitForResponseTimeout(CMD_ACK
,NULL
,2500) ) { 
 345                         PrintAndLog("command execution time out"); 
 352 int CmdLFSnoop(const char *Cmd
) 
 355         if(param_getchar(Cmd
, cmdp
) == 'h') 
 357                 return usage_lf_snoop(); 
 360         UsbCommand c 
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
}; 
 361         clearCommandBuffer(); 
 363         WaitForResponse(CMD_ACK
,NULL
); 
 367 static void ChkBitstream(const char *str
) 
 371         /* convert to bitstream if necessary */ 
 372         for (i 
= 0; i 
< (int)(GraphTraceLen 
/ 2); i
++){ 
 373                 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0) { 
 379 //Attempt to simulate any wave in buffer (one bit per output sample) 
 380 // converts GraphBuffer to bitstream (based on zero crossings) if needed. 
 381 int CmdLFSim(const char *Cmd
) 
 386         sscanf(Cmd
, "%i", &gap
); 
 388         // convert to bitstream if necessary  
 392         //can send only 512 bits at a time (1 byte sent per bit...) 
 393         printf("Sending [%d bytes]", GraphTraceLen
); 
 394         for (i 
= 0; i 
< GraphTraceLen
; i 
+= USB_CMD_DATA_SIZE
) { 
 395                 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}}; 
 397                 for (j 
= 0; j 
< USB_CMD_DATA_SIZE
; j
++) { 
 398                         c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
]; 
 401                 WaitForResponse(CMD_ACK
,NULL
); 
 406         PrintAndLog("Starting to simulate"); 
 407         UsbCommand c 
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}}; 
 408         clearCommandBuffer(); 
 413 int usage_lf_simfsk(void) 
 416         PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]"); 
 417         PrintAndLog("Options:        "); 
 418         PrintAndLog("       h              This help"); 
 419         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 420         PrintAndLog("       i              invert data"); 
 421         PrintAndLog("       H <fcHigh>     Manually set the larger Field Clock"); 
 422         PrintAndLog("       L <fcLow>      Manually set the smaller Field Clock"); 
 423         //PrintAndLog("       s              TBD- -to enable a gap between playback repetitions - default: no gap"); 
 424         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 425         PrintAndLog("\n  NOTE: if you set one clock manually set them all manually"); 
 429 int usage_lf_simask(void) 
 432         PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]"); 
 433         PrintAndLog("Options:        "); 
 434         PrintAndLog("       h              This help"); 
 435         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 436         PrintAndLog("       i              invert data"); 
 437         PrintAndLog("       b              sim ask/biphase"); 
 438         PrintAndLog("       m              sim ask/manchester - Default"); 
 439         PrintAndLog("       r              sim ask/raw"); 
 440         PrintAndLog("       s              add t55xx Sequence Terminator gap - default: no gaps (only manchester)"); 
 441         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 445 int usage_lf_simpsk(void) 
 448         PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]"); 
 449         PrintAndLog("Options:        "); 
 450         PrintAndLog("       h              This help"); 
 451         PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer"); 
 452         PrintAndLog("       i              invert data"); 
 453         PrintAndLog("       1              set PSK1 (default)"); 
 454         PrintAndLog("       2              set PSK2"); 
 455         PrintAndLog("       3              set PSK3"); 
 456         PrintAndLog("       r <carrier>    2|4|8 are valid carriers: default = 2"); 
 457         PrintAndLog("       d <hexdata>    Data to sim as hex - omit to sim from DemodBuffer"); 
 461 // by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert  
 462 // - allow pull data from DemodBuffer 
 463 int CmdLFfskSim(const char *Cmd
) 
 465         //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer 
 466         // otherwise will need FChigh, FClow, Clock, and bitstream 
 467         uint8_t fcHigh
=0, fcLow
=0, clk
=0; 
 470         char hexData
[32] = {0x00}; // store entered hex data 
 471         uint8_t data
[255] = {0x00};  
 474         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 476                 switch(param_getchar(Cmd
, cmdp
)) 
 479                         return usage_lf_simfsk(); 
 485                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 489                         errors 
|= param_getdec(Cmd
,cmdp
+1,&fcHigh
); 
 493                         errors 
|= param_getdec(Cmd
,cmdp
+1,&fcLow
); 
 501                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 505                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 507                         if (dataLen
==0) errors
=true;  
 508                         if (errors
) PrintAndLog ("Error getting hex data"); 
 512                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 518         if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 520                 errors 
= true;// No args 
 526                 return usage_lf_simfsk(); 
 529         if (dataLen 
== 0){ //using DemodBuffer  
 530                 if (clk
==0 || fcHigh
==0 || fcLow
==0){ //manual settings must set them all 
 531                         uint8_t ans 
= fskClocks(&fcHigh
, &fcLow
, &clk
, 0); 
 533                                 if (!fcHigh
) fcHigh
=10; 
 539                 setDemodBuf(data
, dataLen
, 0); 
 542         //default if not found 
 543         if (clk 
== 0) clk 
= 50; 
 544         if (fcHigh 
== 0) fcHigh 
= 10; 
 545         if (fcLow 
== 0) fcLow 
= 8; 
 548         arg1 
= fcHigh 
<< 8 | fcLow
; 
 549         arg2 
= invert 
<< 8 | clk
; 
 550         size_t size 
= DemodBufferLen
; 
 551         if (size 
> USB_CMD_DATA_SIZE
) { 
 552                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 553                 size 
= USB_CMD_DATA_SIZE
; 
 555         UsbCommand c 
= {CMD_FSK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 557         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 558         clearCommandBuffer(); 
 563 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator  
 564 // - allow pull data from DemodBuffer 
 565 int CmdLFaskSim(const char *Cmd
) 
 567         //autodetect clock from Graphbuffer if using demod buffer 
 568         // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream 
 569         uint8_t encoding 
= 1, separator 
= 0; 
 570         uint8_t clk
=0, invert
=0; 
 572         char hexData
[32] = {0x00};  
 573         uint8_t data
[255]= {0x00}; // store entered hex data 
 576         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 578                 switch(param_getchar(Cmd
, cmdp
)) 
 581                         return usage_lf_simask(); 
 587                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 591                         encoding
=2; //biphase 
 607                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 611                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 613                         if (dataLen
==0) errors
=true;  
 614                         if (errors
) PrintAndLog ("Error getting hex data, datalen: %d",dataLen
); 
 618                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 624         if(cmdp 
== 0 && DemodBufferLen 
== 0) 
 626                 errors 
= true;// No args 
 632                 return usage_lf_simask(); 
 634         if (dataLen 
== 0){ //using DemodBuffer 
 635                 if (clk 
== 0) clk 
= GetAskClock("0", false, false); 
 637                 setDemodBuf(data
, dataLen
, 0); 
 639         if (clk 
== 0) clk 
= 64; 
 640         if (encoding 
== 0) clk 
= clk
/2; //askraw needs to double the clock speed 
 642         size_t size
=DemodBufferLen
; 
 643         arg1 
= clk 
<< 8 | encoding
; 
 644         arg2 
= invert 
<< 8 | separator
; 
 645         if (size 
> USB_CMD_DATA_SIZE
) { 
 646                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 647                 size 
= USB_CMD_DATA_SIZE
; 
 649         UsbCommand c 
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 650         PrintAndLog("preparing to sim ask data: %d bits", size
); 
 651         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 652         clearCommandBuffer(); 
 657 // by marshmellow - sim psk data given carrier, clock, invert  
 658 // - allow pull data from DemodBuffer or parameters 
 659 int CmdLFpskSim(const char *Cmd
) 
 661         //might be able to autodetect FC and clock from Graphbuffer if using demod buffer 
 662         //will need carrier, Clock, and bitstream 
 663         uint8_t carrier
=0, clk
=0; 
 666         char hexData
[32] = {0x00}; // store entered hex data 
 667         uint8_t data
[255] = {0x00};  
 671         while(param_getchar(Cmd
, cmdp
) != 0x00) 
 673                 switch(param_getchar(Cmd
, cmdp
)) 
 676                         return usage_lf_simpsk(); 
 682                         errors 
|= param_getdec(Cmd
,cmdp
+1,&clk
); 
 686                         errors 
|= param_getdec(Cmd
,cmdp
+1,&carrier
); 
 702                         dataLen 
= param_getstr(Cmd
, cmdp
+1, hexData
); 
 706                                 dataLen 
= hextobinarray((char *)data
, hexData
); 
 708                         if (dataLen
==0) errors
=true;  
 709                         if (errors
) PrintAndLog ("Error getting hex data"); 
 713                         PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
)); 
 719         if (cmdp 
== 0 && DemodBufferLen 
== 0) 
 721                 errors 
= true;// No args 
 727                 return usage_lf_simpsk(); 
 729         if (dataLen 
== 0){ //using DemodBuffer 
 730                 PrintAndLog("Getting Clocks"); 
 731                 if (clk
==0) clk 
= GetPskClock("", false, false); 
 732                 PrintAndLog("clk: %d",clk
); 
 733                 if (!carrier
) carrier 
= GetPskCarrier("", false, false);  
 734                 PrintAndLog("carrier: %d", carrier
); 
 736                 setDemodBuf(data
, dataLen
, 0); 
 739         if (clk 
<= 0) clk 
= 32; 
 740         if (carrier 
== 0) carrier 
= 2; 
 743                         //need to convert psk2 to psk1 data before sim 
 744                         psk2TOpsk1(DemodBuffer
, DemodBufferLen
); 
 746                         PrintAndLog("Sorry, PSK3 not yet available"); 
 750         arg1 
= clk 
<< 8 | carrier
; 
 752         size_t size
=DemodBufferLen
; 
 753         if (size 
> USB_CMD_DATA_SIZE
) { 
 754                 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size
, USB_CMD_DATA_SIZE
); 
 755                 size
=USB_CMD_DATA_SIZE
; 
 757         UsbCommand c 
= {CMD_PSK_SIM_TAG
, {arg1
, arg2
, size
}}; 
 758         PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size
); 
 759         memcpy(c
.d
.asBytes
, DemodBuffer
, size
); 
 760         clearCommandBuffer(); 
 766 int CmdLFSimBidir(const char *Cmd
) 
 768         // Set ADC to twice the carrier for a slight supersampling 
 769         // HACK: not implemented in ARMSRC. 
 770         PrintAndLog("Not implemented yet."); 
 771         UsbCommand c 
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}}; 
 776 int CmdVchDemod(const char *Cmd
) 
 778         // Is this the entire sync pattern, or does this also include some 
 779         // data bits that happen to be the same everywhere? That would be 
 781         static const int SyncPattern
[] = { 
 782                 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 
 783                 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
 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, 
 794         // So first, we correlate for the sync pattern, and mark that. 
 795         int bestCorrel 
= 0, bestPos 
= 0; 
 797         // It does us no good to find the sync pattern, with fewer than 
 798         // 2048 samples after it... 
 799         for (i 
= 0; i 
< (GraphTraceLen
-2048); i
++) { 
 802                 for (j 
= 0; j 
< arraylen(SyncPattern
); j
++) { 
 803                         sum 
+= GraphBuffer
[i
+j
]*SyncPattern
[j
]; 
 805                 if (sum 
> bestCorrel
) { 
 810         PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
); 
 818         for (i 
= 0; i 
< 2048; i 
+= 8) { 
 821                 for (j 
= 0; j 
< 8; j
++) { 
 822                         sum 
+= GraphBuffer
[bestPos
+i
+j
]; 
 829                 if(abs(sum
) < worst
) { 
 834         PrintAndLog("bits:"); 
 835         PrintAndLog("%s", bits
); 
 836         PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
); 
 838         if (strcmp(Cmd
, "clone")==0) { 
 841                 for(s 
= bits
; *s
; s
++) { 
 843                         for(j 
= 0; j 
< 16; j
++) { 
 844                                 GraphBuffer
[GraphTraceLen
++] = (*s 
== '1') ? 1 : 0; 
 847                 RepaintGraphWindow(); 
 854 int CheckChipType(char cmdp
) { 
 855         uint32_t wordData 
= 0; 
 857         //check for em4x05/em4x69 chips first 
 859         if ((!offline 
&& (cmdp 
!= '1')) && EM4x05Block0Test(&wordData
)) { 
 860                 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n"); 
 865         //TODO check for t55xx chip... 
 872 int CmdLFfind(const char *Cmd
) 
 874         uint32_t wordData 
= 0; 
 876         size_t minLength 
= 1000; 
 877         char cmdp 
= param_getchar(Cmd
, 0); 
 878         char testRaw 
= param_getchar(Cmd
, 1); 
 879         if (strlen(Cmd
) > 3 || cmdp 
== 'h' || cmdp 
== 'H') { 
 880                 PrintAndLog("Usage:  lf search <0|1> [u]"); 
 881                 PrintAndLog("     <use data from Graphbuffer> , if not set, try reading data from tag."); 
 882                 PrintAndLog("     [Search for Unknown tags] , if not set, reads only known tags."); 
 884                 PrintAndLog("    sample: lf search     = try reading data from tag & search for known tags"); 
 885                 PrintAndLog("          : lf search 1   = use data from GraphBuffer & search for known tags"); 
 886                 PrintAndLog("          : lf search u   = try reading data from tag & search for known and unknown tags"); 
 887                 PrintAndLog("          : lf search 1 u = use data from GraphBuffer & search for known and unknown tags"); 
 892         if (!offline 
&& (cmdp 
!= '1')){ 
 894                 getSamples("30000",false); 
 895         } else if (GraphTraceLen 
< minLength
) { 
 896                 PrintAndLog("Data in Graphbuffer was too small."); 
 899         if (cmdp 
== 'u' || cmdp 
== 'U') testRaw 
= 'u'; 
 901         PrintAndLog("NOTE: some demods output possible binary\n  if it finds something that looks like a tag"); 
 902         PrintAndLog("False Positives ARE possible\n");   
 903         PrintAndLog("\nChecking for known tags:\n"); 
 905         size_t testLen 
= minLength
; 
 906         // only run if graphbuffer is just noise as it should be for hitag/cotag 
 907         if (graphJustNoise(GraphBuffer
, testLen
)) { 
 908                 // only run these tests if we are in online mode  
 909                 if (!offline 
&& (cmdp 
!= '1')) { 
 910                         // test for em4x05 in reader talk first mode. 
 911                         if (EM4x05Block0Test(&wordData
)) { 
 912                                 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n"); 
 915                         ans
=CmdLFHitagReader("26"); 
 919                         ans
=CmdCOTAGRead(""); 
 921                                 PrintAndLog("\nValid COTAG ID Found!"); 
 928         ans
=CmdFSKdemodIO(""); 
 930                 PrintAndLog("\nValid IO Prox ID Found!"); 
 931                 return CheckChipType(cmdp
); 
 934         ans
=CmdFSKdemodPyramid(""); 
 936                 PrintAndLog("\nValid Pyramid ID Found!"); 
 937                 return CheckChipType(cmdp
); 
 940         ans
=CmdFSKdemodParadox(""); 
 942                 PrintAndLog("\nValid Paradox ID Found!"); 
 943                 return CheckChipType(cmdp
); 
 946         ans
=CmdFSKdemodAWID(""); 
 948                 PrintAndLog("\nValid AWID ID Found!"); 
 949                 return CheckChipType(cmdp
); 
 952         ans
=CmdFSKdemodHID(""); 
 954                 PrintAndLog("\nValid HID Prox ID Found!"); 
 955                 return CheckChipType(cmdp
); 
 958         ans
=CmdAskEM410xDemod(""); 
 960                 PrintAndLog("\nValid EM410x ID Found!"); 
 961                 return CheckChipType(cmdp
); 
 964         ans
=CmdVisa2kDemod(""); 
 966                 PrintAndLog("\nValid Visa2000 ID Found!"); 
 967                 return CheckChipType(cmdp
);              
 970         ans
=CmdG_Prox_II_Demod(""); 
 972                 PrintAndLog("\nValid G Prox II ID Found!"); 
 973                 return CheckChipType(cmdp
); 
 976         ans
=CmdFDXBdemodBI(""); 
 978                 PrintAndLog("\nValid FDX-B ID Found!"); 
 979                 return CheckChipType(cmdp
); 
 982         ans
=EM4x50Read("", false); 
 984                 PrintAndLog("\nValid EM4x50 ID Found!"); 
 988         ans
=CmdVikingDemod(""); 
 990                 PrintAndLog("\nValid Viking ID Found!"); 
 991                 return CheckChipType(cmdp
); 
 994         ans
=CmdIndalaDecode(""); 
 996                 PrintAndLog("\nValid Indala ID Found!"); 
 997                 return CheckChipType(cmdp
); 
1000         ans
=CmdPSKNexWatch(""); 
1002                 PrintAndLog("\nValid NexWatch ID Found!"); 
1003                 return CheckChipType(cmdp
); 
1006         PrintAndLog("\nNo Known Tags Found!\n"); 
1007         if (testRaw
=='u' || testRaw
=='U'){ 
1008                 ans
=CheckChipType(cmdp
); 
1009                 //test unknown tag formats (raw mode)0 
1010                 PrintAndLog("\nChecking for Unknown tags:\n"); 
1011                 ans
=AutoCorrelate(4000, false, false); 
1012                 if (ans 
> 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans
); 
1013                 ans
=GetFskClock("",false,false);  
1014                 if (ans 
!= 0){ //fsk 
1015                         ans
=FSKrawDemod("",true); 
1017                                 PrintAndLog("\nUnknown FSK Modulated Tag Found!"); 
1022                 ans
=ASKDemod_ext("0 0 0",true,false,1,&st
); 
1024                         PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!"); 
1025                         PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'"); 
1028                 ans
=CmdPSK1rawDemod(""); 
1030                         PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'"); 
1031                         PrintAndLog("\nCould also be PSK3 - [currently not supported]"); 
1032                         PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod"); 
1035                 PrintAndLog("\nNo Data Found!\n"); 
1040 static command_t CommandTable
[] =  
1042         {"help",        CmdHelp
,            1, "This help"}, 
1043         {"awid",        CmdLFAWID
,          1, "{ AWID RFIDs...    }"}, 
1044         {"cotag",       CmdLFCOTAG
,         1, "{ COTAG RFIDs...   }"}, 
1045         {"em",          CmdLFEM4X
,          1, "{ EM4X RFIDs...    }"}, 
1046         {"hid",         CmdLFHID
,           1, "{ HID RFIDs...     }"}, 
1047         {"hitag",       CmdLFHitag
,         1, "{ Hitag tags and transponders... }"}, 
1048         {"io",          CmdLFIO
,            1, "{ ioProx tags...   }"}, 
1049         {"indala",      CmdLFINDALA
,        1, "{ Indala tags...   }"}, 
1050         {"presco",      CmdLFPresco
,        1, "{ Presco RFIDs...  }"}, 
1051         {"pcf7931",     CmdLFPCF7931
,       1, "{ PCF7931 RFIDs... }"}, 
1052         {"pyramid",     CmdLFPyramid
,       1, "{ Farpointe/Pyramid RFIDs... }"}, 
1053         {"t55xx",       CmdLFT55XX
,         1, "{ T55xx RFIDs...   }"}, 
1054         {"ti",          CmdLFTI
,            1, "{ TI RFIDs...      }"}, 
1055         {"viking",      CmdLFViking
,        1, "{ Viking tags...   }"}, 
1056         {"visa2000",    CmdLFVisa2k
,        1, "{ Visa2000 RFIDs...}"}, 
1057         {"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)"}, 
1058         {"config",      CmdLFSetConfig
,     0, "Set config for LF sampling, bit/sample, decimation, frequency"}, 
1059         {"flexdemod",   CmdFlexdemod
,       1, "Demodulate samples for FlexPass"}, 
1060         {"read",        CmdLFRead
,          0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"}, 
1061         {"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"}, 
1062         {"sim",         CmdLFSim
,           0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"}, 
1063         {"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"}, 
1064         {"simfsk",      CmdLFfskSim
,        0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"}, 
1065         {"simpsk",      CmdLFpskSim
,        0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"}, 
1066         {"simbidir",    CmdLFSimBidir
,      0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"}, 
1067         {"snoop",       CmdLFSnoop
,         0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"}, 
1068         {"vchdemod",    CmdVchDemod
,        1, "['clone'] -- Demodulate samples for VeriChip"}, 
1069         {NULL
, NULL
, 0, NULL
} 
1072 int CmdLF(const char *Cmd
) 
1074         CmdsParse(CommandTable
, Cmd
); 
1078 int CmdHelp(const char *Cmd
) 
1080         CmdsHelp(CommandTable
);