]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlf.c
Split data askgproxiidemod to own file
[proxmark3-svn] / client / cmdlf.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// Low frequency commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
590f8ff9 12#include <stdlib.h>
7fe9b0b7 13#include <string.h>
393c3ef9 14#include <limits.h>
902cb3c0 15#include "proxmark3.h"
7fe9b0b7 16#include "cmdlf.h"
29ada8fc 17#include "lfdemod.h" // for psk2TOpsk1
6923d3f1 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
e04475c4 36#include "cmdlfcotag.h" // for COTAG menu
8b6abef5 37#include "cmdlfvisa2000.h" // for VISA2000 menu
0fb65a26 38#include "cmdlfindala.h" // for indala menu
946a84c3 39#include "cmdlfgproxii.h"// for gproxii menu
e04475c4 40
fac69c3d 41bool g_lf_threshold_set = false;
7fe9b0b7 42static int CmdHelp(const char *Cmd);
43
21a615cb 44
45
f86d6b55 46int usage_lf_cmdread(void)
21a615cb 47{
48 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H] ");
49 PrintAndLog("Options: ");
50 PrintAndLog(" h This help");
51 PrintAndLog(" L Low frequency (125 KHz)");
52 PrintAndLog(" H High frequency (134 KHz)");
53 PrintAndLog(" d <delay> delay OFF period");
54 PrintAndLog(" z <zero> time period ZERO");
55 PrintAndLog(" o <one> time period ONE");
56 PrintAndLog(" c <cmd> Command bytes");
57 PrintAndLog(" ************* All periods in microseconds");
58 PrintAndLog("Examples:");
59 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
60 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
61 return 0;
62}
63
7fe9b0b7 64/* send a command before reading */
65int CmdLFCommandRead(const char *Cmd)
66{
21a615cb 67 static char dummy[3] = {0x20,0x00,0x00};
e0165dcf 68 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
7cb8516c 69 bool errors = false;
21a615cb 70 //uint8_t divisor = 95; //125khz
71 uint8_t cmdp = 0;
21a615cb 72 while(param_getchar(Cmd, cmdp) != 0x00)
73 {
74 switch(param_getchar(Cmd, cmdp))
75 {
76 case 'h':
77 return usage_lf_cmdread();
78 case 'H':
79 //divisor = 88;
80 dummy[1]='h';
81 cmdp++;
82 break;
83 case 'L':
84 cmdp++;
85 break;
86 case 'c':
f9ce1c3a 87 param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes);
21a615cb 88 cmdp+=2;
89 break;
90 case 'd':
91 c.arg[0] = param_get32ex(Cmd, cmdp+1, 0, 10);
92 cmdp+=2;
93 break;
94 case 'z':
95 c.arg[1] = param_get32ex(Cmd, cmdp+1, 0, 10);
96 cmdp+=2;
97 break;
98 case 'o':
99 c.arg[2] = param_get32ex(Cmd, cmdp+1, 0, 10);
100 cmdp+=2;
101 break;
102 default:
103 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
104 errors = 1;
105 break;
106 }
107 if(errors) break;
108 }
109 // No args
110 if(cmdp == 0) errors = 1;
111
112 //Validations
113 if(errors) return usage_lf_cmdread();
114
115 // in case they specified 'H'
e0165dcf 116 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
21a615cb 117
118 clearCommandBuffer();
e0165dcf 119 SendCommand(&c);
120 return 0;
7fe9b0b7 121}
122
123int CmdFlexdemod(const char *Cmd)
124{
e0165dcf 125 int i;
126 for (i = 0; i < GraphTraceLen; ++i) {
127 if (GraphBuffer[i] < 0) {
128 GraphBuffer[i] = -1;
129 } else {
130 GraphBuffer[i] = 1;
131 }
132 }
7fe9b0b7 133
f6650679 134 #define LONG_WAIT 100
e0165dcf 135 int start;
136 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
137 int first = GraphBuffer[start];
138 for (i = start; i < start + LONG_WAIT; i++) {
139 if (GraphBuffer[i] != first) {
140 break;
141 }
142 }
143 if (i == (start + LONG_WAIT)) {
144 break;
145 }
146 }
147 if (start == GraphTraceLen - LONG_WAIT) {
148 PrintAndLog("nothing to wait for");
149 return 0;
150 }
151
152 GraphBuffer[start] = 2;
153 GraphBuffer[start+1] = -2;
3fe4ff4f 154 uint8_t bits[64] = {0x00};
7fe9b0b7 155
3fe4ff4f 156 int bit, sum;
e0165dcf 157 i = start;
158 for (bit = 0; bit < 64; bit++) {
3fe4ff4f 159 sum = 0;
160 for (int j = 0; j < 16; j++) {
e0165dcf 161 sum += GraphBuffer[i++];
162 }
3fe4ff4f 163
164 bits[bit] = (sum > 0) ? 1 : 0;
165
e0165dcf 166 PrintAndLog("bit %d sum %d", bit, sum);
167 }
168
169 for (bit = 0; bit < 64; bit++) {
170 int j;
171 int sum = 0;
172 for (j = 0; j < 16; j++) {
173 sum += GraphBuffer[i++];
174 }
175 if (sum > 0 && bits[bit] != 1) {
176 PrintAndLog("oops1 at %d", bit);
177 }
178 if (sum < 0 && bits[bit] != 0) {
179 PrintAndLog("oops2 at %d", bit);
180 }
181 }
7fe9b0b7 182
3fe4ff4f 183 // HACK writing back to graphbuffer.
e0165dcf 184 GraphTraceLen = 32*64;
185 i = 0;
186 int phase = 0;
187 for (bit = 0; bit < 64; bit++) {
3fe4ff4f 188
189 phase = (bits[bit] == 0) ? 0 : 1;
190
e0165dcf 191 int j;
192 for (j = 0; j < 32; j++) {
193 GraphBuffer[i++] = phase;
194 phase = !phase;
195 }
196 }
197
198 RepaintGraphWindow();
199 return 0;
0fb65a26 200}
2414f978 201
f86d6b55 202int usage_lf_read(void)
f6d9fb17 203{
31abe49f 204 PrintAndLog("Usage: lf read");
f6d9fb17
MHS
205 PrintAndLog("Options: ");
206 PrintAndLog(" h This help");
1fbf8956 207 PrintAndLog(" s silent run no printout");
31abe49f
MHS
208 PrintAndLog("This function takes no arguments. ");
209 PrintAndLog("Use 'lf config' to set parameters.");
210 return 0;
211}
f86d6b55 212int usage_lf_snoop(void)
31abe49f
MHS
213{
214 PrintAndLog("Usage: lf snoop");
215 PrintAndLog("Options: ");
216 PrintAndLog(" h This help");
217 PrintAndLog("This function takes no arguments. ");
218 PrintAndLog("Use 'lf config' to set parameters.");
219 return 0;
220}
221
f86d6b55 222int usage_lf_config(void)
31abe49f
MHS
223{
224 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
225 PrintAndLog("Options: ");
226 PrintAndLog(" h This help");
227 PrintAndLog(" L Low frequency (125 KHz)");
228 PrintAndLog(" H High frequency (134 KHz)");
229 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
230 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
231 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
232 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
b29d55f2 233 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
f6d9fb17 234 PrintAndLog("Examples:");
31abe49f 235 PrintAndLog(" lf config b 8 L");
f6d9fb17 236 PrintAndLog(" Samples at 125KHz, 8bps.");
31abe49f 237 PrintAndLog(" lf config H b 4 d 3");
f6d9fb17
MHS
238 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
239 PrintAndLog(" a resolution of 4 bits per sample.");
31abe49f
MHS
240 PrintAndLog(" lf read");
241 PrintAndLog(" Performs a read (active field)");
242 PrintAndLog(" lf snoop");
243 PrintAndLog(" Performs a snoop (no active field)");
f6d9fb17
MHS
244 return 0;
245}
31abe49f
MHS
246
247int CmdLFSetConfig(const char *Cmd)
7fe9b0b7 248{
31abe49f
MHS
249
250 uint8_t divisor = 0;//Frequency divisor
251 uint8_t bps = 0; // Bits per sample
252 uint8_t decimation = 0; //How many to keep
253 bool averaging = 1; // Defaults to true
7cb8516c 254 bool errors = false;
31abe49f
MHS
255 int trigger_threshold =-1;//Means no change
256 uint8_t unsigned_trigg = 0;
f6d9fb17
MHS
257
258 uint8_t cmdp =0;
31abe49f 259 while(param_getchar(Cmd, cmdp) != 0x00)
f6d9fb17 260 {
31abe49f
MHS
261 switch(param_getchar(Cmd, cmdp))
262 {
263 case 'h':
264 return usage_lf_config();
265 case 'H':
266 divisor = 88;
267 cmdp++;
268 break;
269 case 'L':
270 divisor = 95;
271 cmdp++;
272 break;
273 case 'q':
274 errors |= param_getdec(Cmd,cmdp+1,&divisor);
275 cmdp+=2;
276 break;
277 case 't':
278 errors |= param_getdec(Cmd,cmdp+1,&unsigned_trigg);
279 cmdp+=2;
2b11c7c7 280 if(!errors) {
281 trigger_threshold = unsigned_trigg;
fac69c3d 282 if (trigger_threshold > 0) g_lf_threshold_set = true;
2b11c7c7 283 }
31abe49f
MHS
284 break;
285 case 'b':
286 errors |= param_getdec(Cmd,cmdp+1,&bps);
287 cmdp+=2;
288 break;
289 case 'd':
290 errors |= param_getdec(Cmd,cmdp+1,&decimation);
291 cmdp+=2;
292 break;
293 case 'a':
294 averaging = param_getchar(Cmd,cmdp+1) == '1';
295 cmdp+=2;
296 break;
297 default:
298 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
299 errors = 1;
300 break;
301 }
302 if(errors) break;
f6d9fb17 303 }
31abe49f 304 if(cmdp == 0)
f6d9fb17 305 {
31abe49f 306 errors = 1;// No args
f6d9fb17 307 }
31abe49f 308
f6d9fb17
MHS
309 //Validations
310 if(errors)
311 {
31abe49f 312 return usage_lf_config();
f6d9fb17 313 }
f6d9fb17 314 //Bps is limited to 8, so fits in lower half of arg1
72c5877a 315 if(bps >> 4) bps = 8;
f6d9fb17 316
31abe49f
MHS
317 sample_config config = {
318 decimation,bps,averaging,divisor,trigger_threshold
319 };
320 //Averaging is a flag on high-bit of arg[1]
321 UsbCommand c = {CMD_SET_LF_SAMPLING_CONFIG};
322 memcpy(c.d.asBytes,&config,sizeof(sample_config));
709665b5 323 clearCommandBuffer();
31abe49f
MHS
324 SendCommand(&c);
325 return 0;
326}
f6d9fb17 327
7fe9b0b7 328int CmdLFRead(const char *Cmd)
329{
2b11c7c7 330 if (offline) return 0;
1fbf8956 331 uint8_t cmdp = 0;
332 bool arg1 = false;
333 if (param_getchar(Cmd, cmdp) == 'h')
31abe49f
MHS
334 {
335 return usage_lf_read();
336 }
1fbf8956 337 if (param_getchar(Cmd, cmdp) == 's') arg1 = true; //suppress print
f6d9fb17 338 //And ship it to device
1fbf8956 339 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {arg1,0,0}};
709665b5 340 clearCommandBuffer();
31abe49f 341 SendCommand(&c);
fac69c3d 342 if (g_lf_threshold_set) {
2b11c7c7 343 WaitForResponse(CMD_ACK,NULL);
344 } else {
345 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
346 PrintAndLog("command execution time out");
347 return 1;
348 }
349 }
31abe49f
MHS
350 return 0;
351}
f6d9fb17 352
31abe49f
MHS
353int CmdLFSnoop(const char *Cmd)
354{
355 uint8_t cmdp =0;
356 if(param_getchar(Cmd, cmdp) == 'h')
357 {
358 return usage_lf_snoop();
359 }
360
361 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
709665b5 362 clearCommandBuffer();
f6d9fb17
MHS
363 SendCommand(&c);
364 WaitForResponse(CMD_ACK,NULL);
365 return 0;
7fe9b0b7 366}
367
368static void ChkBitstream(const char *str)
369{
e0165dcf 370 int i;
78f5b1a7 371
e0165dcf 372 /* convert to bitstream if necessary */
b915fda3 373 for (i = 0; i < (int)(GraphTraceLen / 2); i++){
374 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) {
e0165dcf 375 CmdGetBitStream("");
376 break;
377 }
378 }
7fe9b0b7 379}
2767fc02 380//Attempt to simulate any wave in buffer (one bit per output sample)
381// converts GraphBuffer to bitstream (based on zero crossings) if needed.
7fe9b0b7 382int CmdLFSim(const char *Cmd)
383{
e0165dcf 384 int i,j;
385 static int gap;
7fe9b0b7 386
e0165dcf 387 sscanf(Cmd, "%i", &gap);
7fe9b0b7 388
2767fc02 389 // convert to bitstream if necessary
78f5b1a7 390
e0165dcf 391 ChkBitstream(Cmd);
7fe9b0b7 392
2767fc02 393 //can send only 512 bits at a time (1 byte sent per bit...)
e0165dcf 394 printf("Sending [%d bytes]", GraphTraceLen);
395 for (i = 0; i < GraphTraceLen; i += USB_CMD_DATA_SIZE) {
396 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
52ab55ab 397
e0165dcf 398 for (j = 0; j < USB_CMD_DATA_SIZE; j++) {
399 c.d.asBytes[j] = GraphBuffer[i+j];
400 }
401 SendCommand(&c);
402 WaitForResponse(CMD_ACK,NULL);
403 printf(".");
404 }
7fe9b0b7 405
e0165dcf 406 printf("\n");
407 PrintAndLog("Starting to simulate");
408 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
709665b5 409 clearCommandBuffer();
e0165dcf 410 SendCommand(&c);
411 return 0;
7fe9b0b7 412}
413
abd6112f 414int usage_lf_simfsk(void)
415{
e0165dcf 416 //print help
417 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
418 PrintAndLog("Options: ");
419 PrintAndLog(" h This help");
420 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
421 PrintAndLog(" i invert data");
422 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
423 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
424 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
425 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
426 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
427 return 0;
abd6112f 428}
429
430int usage_lf_simask(void)
431{
e0165dcf 432 //print help
433 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
434 PrintAndLog("Options: ");
435 PrintAndLog(" h This help");
436 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
437 PrintAndLog(" i invert data");
438 PrintAndLog(" b sim ask/biphase");
439 PrintAndLog(" m sim ask/manchester - Default");
440 PrintAndLog(" r sim ask/raw");
29ada8fc 441 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
e0165dcf 442 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
443 return 0;
abd6112f 444}
445
872e3d4d 446int usage_lf_simpsk(void)
447{
e0165dcf 448 //print help
449 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
450 PrintAndLog("Options: ");
451 PrintAndLog(" h This help");
452 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
453 PrintAndLog(" i invert data");
454 PrintAndLog(" 1 set PSK1 (default)");
455 PrintAndLog(" 2 set PSK2");
456 PrintAndLog(" 3 set PSK3");
457 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
458 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
459 return 0;
872e3d4d 460}
712ebfa6 461
f86d6b55 462// by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert
abd6112f 463// - allow pull data from DemodBuffer
464int CmdLFfskSim(const char *Cmd)
465{
2767fc02 466 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
467 // otherwise will need FChigh, FClow, Clock, and bitstream
e0165dcf 468 uint8_t fcHigh=0, fcLow=0, clk=0;
469 uint8_t invert=0;
7cb8516c 470 bool errors = false;
e0165dcf 471 char hexData[32] = {0x00}; // store entered hex data
472 uint8_t data[255] = {0x00};
473 int dataLen = 0;
474 uint8_t cmdp = 0;
475 while(param_getchar(Cmd, cmdp) != 0x00)
476 {
477 switch(param_getchar(Cmd, cmdp))
478 {
479 case 'h':
480 return usage_lf_simfsk();
481 case 'i':
482 invert = 1;
483 cmdp++;
484 break;
485 case 'c':
486 errors |= param_getdec(Cmd,cmdp+1,&clk);
487 cmdp+=2;
488 break;
489 case 'H':
490 errors |= param_getdec(Cmd,cmdp+1,&fcHigh);
491 cmdp+=2;
492 break;
493 case 'L':
494 errors |= param_getdec(Cmd,cmdp+1,&fcLow);
495 cmdp+=2;
496 break;
497 //case 's':
498 // separator=1;
499 // cmdp++;
500 // break;
501 case 'd':
502 dataLen = param_getstr(Cmd, cmdp+1, hexData);
503 if (dataLen==0) {
7cb8516c 504 errors=true;
e0165dcf 505 } else {
506 dataLen = hextobinarray((char *)data, hexData);
507 }
7cb8516c 508 if (dataLen==0) errors=true;
e0165dcf 509 if (errors) PrintAndLog ("Error getting hex data");
510 cmdp+=2;
511 break;
512 default:
513 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
7cb8516c 514 errors = true;
e0165dcf 515 break;
516 }
517 if(errors) break;
518 }
519 if(cmdp == 0 && DemodBufferLen == 0)
520 {
7cb8516c 521 errors = true;// No args
e0165dcf 522 }
523
524 //Validations
525 if(errors)
526 {
527 return usage_lf_simfsk();
528 }
529
530 if (dataLen == 0){ //using DemodBuffer
531 if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all
532 uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0);
533 if (ans==0){
534 if (!fcHigh) fcHigh=10;
535 if (!fcLow) fcLow=8;
536 if (!clk) clk=50;
537 }
538 }
539 } else {
540 setDemodBuf(data, dataLen, 0);
541 }
2767fc02 542
543 //default if not found
e0165dcf 544 if (clk == 0) clk = 50;
545 if (fcHigh == 0) fcHigh = 10;
546 if (fcLow == 0) fcLow = 8;
547
548 uint16_t arg1, arg2;
549 arg1 = fcHigh << 8 | fcLow;
550 arg2 = invert << 8 | clk;
551 size_t size = DemodBufferLen;
552 if (size > USB_CMD_DATA_SIZE) {
553 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
554 size = USB_CMD_DATA_SIZE;
555 }
556 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
557
558 memcpy(c.d.asBytes, DemodBuffer, size);
709665b5 559 clearCommandBuffer();
e0165dcf 560 SendCommand(&c);
561 return 0;
abd6112f 562}
563
564// by marshmellow - sim ask data given clock, invert, manchester or raw, separator
565// - allow pull data from DemodBuffer
566int CmdLFaskSim(const char *Cmd)
567{
e0165dcf 568 //autodetect clock from Graphbuffer if using demod buffer
2767fc02 569 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
e0165dcf 570 uint8_t encoding = 1, separator = 0;
e0165dcf 571 uint8_t clk=0, invert=0;
7cb8516c 572 bool errors = false;
e0165dcf 573 char hexData[32] = {0x00};
574 uint8_t data[255]= {0x00}; // store entered hex data
575 int dataLen = 0;
576 uint8_t cmdp = 0;
577 while(param_getchar(Cmd, cmdp) != 0x00)
578 {
579 switch(param_getchar(Cmd, cmdp))
580 {
581 case 'h':
582 return usage_lf_simask();
583 case 'i':
584 invert = 1;
585 cmdp++;
586 break;
587 case 'c':
588 errors |= param_getdec(Cmd,cmdp+1,&clk);
589 cmdp+=2;
590 break;
591 case 'b':
592 encoding=2; //biphase
593 cmdp++;
594 break;
595 case 'm':
596 encoding=1;
597 cmdp++;
598 break;
599 case 'r':
600 encoding=0;
601 cmdp++;
602 break;
603 case 's':
604 separator=1;
605 cmdp++;
606 break;
607 case 'd':
608 dataLen = param_getstr(Cmd, cmdp+1, hexData);
609 if (dataLen==0) {
7cb8516c 610 errors=true;
e0165dcf 611 } else {
612 dataLen = hextobinarray((char *)data, hexData);
613 }
7cb8516c 614 if (dataLen==0) errors=true;
e0165dcf 615 if (errors) PrintAndLog ("Error getting hex data, datalen: %d",dataLen);
616 cmdp+=2;
617 break;
618 default:
619 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
7cb8516c 620 errors = true;
e0165dcf 621 break;
622 }
623 if(errors) break;
624 }
625 if(cmdp == 0 && DemodBufferLen == 0)
626 {
7cb8516c 627 errors = true;// No args
e0165dcf 628 }
629
630 //Validations
631 if(errors)
632 {
633 return usage_lf_simask();
634 }
635 if (dataLen == 0){ //using DemodBuffer
636 if (clk == 0) clk = GetAskClock("0", false, false);
637 } else {
638 setDemodBuf(data, dataLen, 0);
639 }
640 if (clk == 0) clk = 64;
641 if (encoding == 0) clk = clk/2; //askraw needs to double the clock speed
642 uint16_t arg1, arg2;
643 size_t size=DemodBufferLen;
644 arg1 = clk << 8 | encoding;
645 arg2 = invert << 8 | separator;
646 if (size > USB_CMD_DATA_SIZE) {
647 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
648 size = USB_CMD_DATA_SIZE;
649 }
650 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
651 PrintAndLog("preparing to sim ask data: %d bits", size);
652 memcpy(c.d.asBytes, DemodBuffer, size);
709665b5 653 clearCommandBuffer();
e0165dcf 654 SendCommand(&c);
655 return 0;
abd6112f 656}
657
872e3d4d 658// by marshmellow - sim psk data given carrier, clock, invert
659// - allow pull data from DemodBuffer or parameters
660int CmdLFpskSim(const char *Cmd)
661{
e0165dcf 662 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
663 //will need carrier, Clock, and bitstream
664 uint8_t carrier=0, clk=0;
665 uint8_t invert=0;
7cb8516c 666 bool errors = false;
e0165dcf 667 char hexData[32] = {0x00}; // store entered hex data
668 uint8_t data[255] = {0x00};
669 int dataLen = 0;
670 uint8_t cmdp = 0;
671 uint8_t pskType = 1;
672 while(param_getchar(Cmd, cmdp) != 0x00)
673 {
674 switch(param_getchar(Cmd, cmdp))
675 {
676 case 'h':
677 return usage_lf_simpsk();
678 case 'i':
679 invert = 1;
680 cmdp++;
681 break;
682 case 'c':
683 errors |= param_getdec(Cmd,cmdp+1,&clk);
684 cmdp+=2;
685 break;
686 case 'r':
687 errors |= param_getdec(Cmd,cmdp+1,&carrier);
688 cmdp+=2;
689 break;
690 case '1':
691 pskType=1;
692 cmdp++;
693 break;
694 case '2':
695 pskType=2;
696 cmdp++;
697 break;
698 case '3':
699 pskType=3;
700 cmdp++;
701 break;
702 case 'd':
703 dataLen = param_getstr(Cmd, cmdp+1, hexData);
704 if (dataLen==0) {
7cb8516c 705 errors=true;
e0165dcf 706 } else {
707 dataLen = hextobinarray((char *)data, hexData);
708 }
7cb8516c 709 if (dataLen==0) errors=true;
e0165dcf 710 if (errors) PrintAndLog ("Error getting hex data");
711 cmdp+=2;
712 break;
713 default:
714 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
7cb8516c 715 errors = true;
e0165dcf 716 break;
717 }
718 if (errors) break;
719 }
720 if (cmdp == 0 && DemodBufferLen == 0)
721 {
7cb8516c 722 errors = true;// No args
e0165dcf 723 }
724
725 //Validations
726 if (errors)
727 {
728 return usage_lf_simpsk();
729 }
730 if (dataLen == 0){ //using DemodBuffer
731 PrintAndLog("Getting Clocks");
7cb8516c 732 if (clk==0) clk = GetPskClock("", false, false);
e0165dcf 733 PrintAndLog("clk: %d",clk);
7cb8516c 734 if (!carrier) carrier = GetPskCarrier("", false, false);
e0165dcf 735 PrintAndLog("carrier: %d", carrier);
736 } else {
737 setDemodBuf(data, dataLen, 0);
738 }
739
740 if (clk <= 0) clk = 32;
741 if (carrier == 0) carrier = 2;
742 if (pskType != 1){
743 if (pskType == 2){
744 //need to convert psk2 to psk1 data before sim
745 psk2TOpsk1(DemodBuffer, DemodBufferLen);
746 } else {
747 PrintAndLog("Sorry, PSK3 not yet available");
748 }
749 }
750 uint16_t arg1, arg2;
751 arg1 = clk << 8 | carrier;
752 arg2 = invert;
753 size_t size=DemodBufferLen;
754 if (size > USB_CMD_DATA_SIZE) {
755 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
756 size=USB_CMD_DATA_SIZE;
757 }
758 UsbCommand c = {CMD_PSK_SIM_TAG, {arg1, arg2, size}};
759 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size);
760 memcpy(c.d.asBytes, DemodBuffer, size);
709665b5 761 clearCommandBuffer();
e0165dcf 762 SendCommand(&c);
763
764 return 0;
872e3d4d 765}
abd6112f 766
7fe9b0b7 767int CmdLFSimBidir(const char *Cmd)
768{
e0165dcf 769 // Set ADC to twice the carrier for a slight supersampling
770 // HACK: not implemented in ARMSRC.
771 PrintAndLog("Not implemented yet.");
772 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
773 SendCommand(&c);
774 return 0;
7fe9b0b7 775}
776
7fe9b0b7 777int CmdVchDemod(const char *Cmd)
778{
e0165dcf 779 // Is this the entire sync pattern, or does this also include some
780 // data bits that happen to be the same everywhere? That would be
781 // lovely to know.
782 static const int SyncPattern[] = {
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,
792 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
793 };
794
795 // So first, we correlate for the sync pattern, and mark that.
796 int bestCorrel = 0, bestPos = 0;
797 int i;
798 // It does us no good to find the sync pattern, with fewer than
799 // 2048 samples after it...
800 for (i = 0; i < (GraphTraceLen-2048); i++) {
801 int sum = 0;
802 int j;
803 for (j = 0; j < arraylen(SyncPattern); j++) {
804 sum += GraphBuffer[i+j]*SyncPattern[j];
805 }
806 if (sum > bestCorrel) {
807 bestCorrel = sum;
808 bestPos = i;
809 }
810 }
811 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
812
813 char bits[257];
814 bits[256] = '\0';
815
816 int worst = INT_MAX;
817 int worstPos = 0;
818
819 for (i = 0; i < 2048; i += 8) {
820 int sum = 0;
821 int j;
822 for (j = 0; j < 8; j++) {
823 sum += GraphBuffer[bestPos+i+j];
824 }
825 if (sum < 0) {
826 bits[i/8] = '.';
827 } else {
828 bits[i/8] = '1';
829 }
830 if(abs(sum) < worst) {
831 worst = abs(sum);
832 worstPos = i;
833 }
834 }
835 PrintAndLog("bits:");
836 PrintAndLog("%s", bits);
837 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
838
839 if (strcmp(Cmd, "clone")==0) {
840 GraphTraceLen = 0;
841 char *s;
842 for(s = bits; *s; s++) {
843 int j;
844 for(j = 0; j < 16; j++) {
845 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
846 }
847 }
848 RepaintGraphWindow();
849 }
850 return 0;
7fe9b0b7 851}
852
d0b05864 853
854//by marshmellow
855int CheckChipType(char cmdp) {
856 uint32_t wordData = 0;
857
858 //check for em4x05/em4x69 chips first
859 save_restoreGB(1);
860 if ((!offline && (cmdp != '1')) && EM4x05Block0Test(&wordData)) {
861 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n");
862 save_restoreGB(0);
863 return 1;
864 }
865
866 //TODO check for t55xx chip...
867
868 save_restoreGB(0);
869 return 1;
870}
871
d5a72d2f 872//by marshmellow
873int CmdLFfind(const char *Cmd)
874{
d0b05864 875 uint32_t wordData = 0;
e0165dcf 876 int ans=0;
e04475c4 877 size_t minLength = 1000;
e0165dcf 878 char cmdp = param_getchar(Cmd, 0);
879 char testRaw = param_getchar(Cmd, 1);
880 if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') {
881 PrintAndLog("Usage: lf search <0|1> [u]");
882 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
883 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
884 PrintAndLog("");
885 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
886 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
887 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
888 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
889
890 return 0;
891 }
892
893 if (!offline && (cmdp != '1')){
2767fc02 894 CmdLFRead("s");
895 getSamples("30000",false);
e04475c4 896 } else if (GraphTraceLen < minLength) {
e0165dcf 897 PrintAndLog("Data in Graphbuffer was too small.");
898 return 0;
899 }
900 if (cmdp == 'u' || cmdp == 'U') testRaw = 'u';
901
902 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
903 PrintAndLog("False Positives ARE possible\n");
904 PrintAndLog("\nChecking for known tags:\n");
905
e04475c4 906 size_t testLen = minLength;
907 // only run if graphbuffer is just noise as it should be for hitag/cotag
908 if (graphJustNoise(GraphBuffer, testLen)) {
909 // only run these tests if we are in online mode
d0b05864 910 if (!offline && (cmdp != '1')) {
911 // test for em4x05 in reader talk first mode.
912 if (EM4x05Block0Test(&wordData)) {
913 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n");
914 return 1;
915 }
e04475c4 916 ans=CmdLFHitagReader("26");
917 if (ans==0) {
918 return 1;
919 }
920 ans=CmdCOTAGRead("");
921 if (ans>0){
922 PrintAndLog("\nValid COTAG ID Found!");
923 return 1;
924 }
925 }
926 return 0;
927 }
928
e0165dcf 929 ans=CmdFSKdemodIO("");
930 if (ans>0) {
931 PrintAndLog("\nValid IO Prox ID Found!");
d0b05864 932 return CheckChipType(cmdp);
e0165dcf 933 }
934
935 ans=CmdFSKdemodPyramid("");
936 if (ans>0) {
937 PrintAndLog("\nValid Pyramid ID Found!");
d0b05864 938 return CheckChipType(cmdp);
e0165dcf 939 }
940
941 ans=CmdFSKdemodParadox("");
942 if (ans>0) {
943 PrintAndLog("\nValid Paradox ID Found!");
d0b05864 944 return CheckChipType(cmdp);
e0165dcf 945 }
946
947 ans=CmdFSKdemodAWID("");
948 if (ans>0) {
949 PrintAndLog("\nValid AWID ID Found!");
d0b05864 950 return CheckChipType(cmdp);
e0165dcf 951 }
952
953 ans=CmdFSKdemodHID("");
954 if (ans>0) {
955 PrintAndLog("\nValid HID Prox ID Found!");
d0b05864 956 return CheckChipType(cmdp);
e0165dcf 957 }
958
e0165dcf 959 ans=CmdAskEM410xDemod("");
960 if (ans>0) {
961 PrintAndLog("\nValid EM410x ID Found!");
d0b05864 962 return CheckChipType(cmdp);
e0165dcf 963 }
964
8b6abef5 965 ans=CmdVisa2kDemod("");
966 if (ans>0) {
967 PrintAndLog("\nValid Visa2000 ID Found!");
968 return CheckChipType(cmdp);
969 }
970
e0165dcf 971 ans=CmdG_Prox_II_Demod("");
972 if (ans>0) {
973 PrintAndLog("\nValid G Prox II ID Found!");
d0b05864 974 return CheckChipType(cmdp);
e0165dcf 975 }
976
ecfcb34c 977 ans=CmdFDXBdemodBI("");
978 if (ans>0) {
979 PrintAndLog("\nValid FDX-B ID Found!");
d0b05864 980 return CheckChipType(cmdp);
ecfcb34c 981 }
982
23f0a7d8 983 ans=EM4x50Read("", false);
984 if (ans>0) {
985 PrintAndLog("\nValid EM4x50 ID Found!");
986 return 1;
987 }
411105e0 988
415274a7 989 ans=CmdVikingDemod("");
990 if (ans>0) {
991 PrintAndLog("\nValid Viking ID Found!");
d0b05864 992 return CheckChipType(cmdp);
415274a7 993 }
994
6fe5c94b 995 ans=CmdIndalaDecode("");
996 if (ans>0) {
997 PrintAndLog("\nValid Indala ID Found!");
d0b05864 998 return CheckChipType(cmdp);
6fe5c94b 999 }
1000
411105e0 1001 ans=CmdPSKNexWatch("");
1002 if (ans>0) {
1003 PrintAndLog("\nValid NexWatch ID Found!");
d0b05864 1004 return CheckChipType(cmdp);
411105e0 1005 }
1006
e0165dcf 1007 PrintAndLog("\nNo Known Tags Found!\n");
1008 if (testRaw=='u' || testRaw=='U'){
d0b05864 1009 ans=CheckChipType(cmdp);
1010 //test unknown tag formats (raw mode)0
e0165dcf 1011 PrintAndLog("\nChecking for Unknown tags:\n");
7cb8516c 1012 ans=AutoCorrelate(4000, false, false);
e0165dcf 1013 if (ans > 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
7cb8516c 1014 ans=GetFskClock("",false,false);
e0165dcf 1015 if (ans != 0){ //fsk
7cb8516c 1016 ans=FSKrawDemod("",true);
e0165dcf 1017 if (ans>0) {
1018 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
e0165dcf 1019 return 1;
1020 }
1021 }
7cb8516c 1022 bool st = true;
1023 ans=ASKDemod_ext("0 0 0",true,false,1,&st);
e0165dcf 1024 if (ans>0) {
1025 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1026 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
e0165dcf 1027 return 1;
1028 }
1029 ans=CmdPSK1rawDemod("");
1030 if (ans>0) {
1031 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1032 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1033 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
e0165dcf 1034 return 1;
1035 }
1036 PrintAndLog("\nNo Data Found!\n");
1037 }
1038 return 0;
d5a72d2f 1039}
1040
7fe9b0b7 1041static command_t CommandTable[] =
1042{
e0165dcf 1043 {"help", CmdHelp, 1, "This help"},
fe876493 1044 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
e04475c4 1045 {"cotag", CmdLFCOTAG, 1, "{ COTAG RFIDs... }"},
73a2a84f 1046 {"em", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
946a84c3 1047 {"gproxii", CmdLF_G_Prox_II, 1, "{ G Prox II tags... }"},
fe876493 1048 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
21a615cb 1049 {"hitag", CmdLFHitag, 1, "{ Hitag tags and transponders... }"},
fe876493 1050 {"io", CmdLFIO, 1, "{ ioProx tags... }"},
0fb65a26 1051 {"indala", CmdLFINDALA, 1, "{ Indala tags... }"},
8b6abef5 1052 {"presco", CmdLFPresco, 1, "{ Presco RFIDs... }"},
21a615cb 1053 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 RFIDs... }"},
6923d3f1 1054 {"pyramid", CmdLFPyramid, 1, "{ Farpointe/Pyramid RFIDs... }"},
fe876493 1055 {"t55xx", CmdLFT55XX, 1, "{ T55xx RFIDs... }"},
1056 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
1057 {"viking", CmdLFViking, 1, "{ Viking tags... }"},
8b6abef5 1058 {"visa2000", CmdLFVisa2k, 1, "{ Visa2000 RFIDs...}"},
21a615cb 1059 {"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)"},
e0165dcf 1060 {"config", CmdLFSetConfig, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1061 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
e0165dcf 1062 {"read", CmdLFRead, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1063 {"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"},
1064 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
aa53efc3 1065 {"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"},
e0165dcf 1066 {"simfsk", CmdLFfskSim, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1067 {"simpsk", CmdLFpskSim, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1068 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
e0165dcf 1069 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
e0165dcf 1070 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
e0165dcf 1071 {NULL, NULL, 0, NULL}
7fe9b0b7 1072};
1073
1074int CmdLF(const char *Cmd)
1075{
e0165dcf 1076 CmdsParse(CommandTable, Cmd);
1077 return 0;
7fe9b0b7 1078}
1079
1080int CmdHelp(const char *Cmd)
1081{
e0165dcf 1082 CmdsHelp(CommandTable);
1083 return 0;
7fe9b0b7 1084}
Impressum, Datenschutz