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