]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
10a70d7cee606523355af1ebf783a1e3084be924
[proxmark3-svn] / client / cmdlf.c
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
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "proxmark3.h"
16 #include "cmdlf.h"
17 #include "lfdemod.h" // for psk2TOpsk1
18 #include "util.h" // for parsing cli command utils
19 #include "ui.h" // for show graph controls
20 #include "graph.h" // for graph data
21 #include "cmdparser.h" // for getting cli commands included in cmdmain.h
22 #include "cmdmain.h" // for sending cmds to device
23 #include "data.h" // for GetFromBigBuf
24 #include "cmddata.h" // for `lf search`
25 #include "cmdlfawid.h" // for awid menu
26 #include "cmdlfem4x.h" // for em4x menu
27 #include "cmdlfhid.h" // for hid menu
28 #include "cmdlfhitag.h" // for hitag menu
29 #include "cmdlfio.h" // for ioprox menu
30 #include "cmdlft55xx.h" // for t55xx menu
31 #include "cmdlfti.h" // for ti menu
32 #include "cmdlfpresco.h" // for presco menu
33 #include "cmdlfpcf7931.h"// for pcf7931 menu
34 #include "cmdlfpyramid.h"// for pyramid menu
35 #include "cmdlfviking.h" // for viking menu
36 #include "cmdlfcotag.h" // for COTAG menu
37 #include "cmdlfvisa2000.h" // for VISA2000 menu
38 #include "cmdlfindala.h" // for indala menu
39 #include "cmdlfgproxii.h"// for gproxii menu
40
41 bool g_lf_threshold_set = false;
42 static int CmdHelp(const char *Cmd);
43
44
45
46 int usage_lf_cmdread(void)
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
64 /* send a command before reading */
65 int CmdLFCommandRead(const char *Cmd)
66 {
67 static char dummy[3] = {0x20,0x00,0x00};
68 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
69 bool errors = false;
70 //uint8_t divisor = 95; //125khz
71 uint8_t cmdp = 0;
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':
87 param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes);
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'
116 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
117
118 clearCommandBuffer();
119 SendCommand(&c);
120 return 0;
121 }
122
123 int CmdFlexdemod(const char *Cmd)
124 {
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 }
133
134 #define LONG_WAIT 100
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;
154 uint8_t bits[64] = {0x00};
155
156 int bit, sum;
157 i = start;
158 for (bit = 0; bit < 64; bit++) {
159 sum = 0;
160 for (int j = 0; j < 16; j++) {
161 sum += GraphBuffer[i++];
162 }
163
164 bits[bit] = (sum > 0) ? 1 : 0;
165
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 }
182
183 // HACK writing back to graphbuffer.
184 GraphTraceLen = 32*64;
185 i = 0;
186 int phase = 0;
187 for (bit = 0; bit < 64; bit++) {
188
189 phase = (bits[bit] == 0) ? 0 : 1;
190
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;
200 }
201
202 int usage_lf_read(void)
203 {
204 PrintAndLog("Usage: lf read");
205 PrintAndLog("Options: ");
206 PrintAndLog(" h This help");
207 PrintAndLog(" s silent run no printout");
208 PrintAndLog("This function takes no arguments. ");
209 PrintAndLog("Use 'lf config' to set parameters.");
210 return 0;
211 }
212 int usage_lf_snoop(void)
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
222 int usage_lf_config(void)
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");
233 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
234 PrintAndLog("Examples:");
235 PrintAndLog(" lf config b 8 L");
236 PrintAndLog(" Samples at 125KHz, 8bps.");
237 PrintAndLog(" lf config H b 4 d 3");
238 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
239 PrintAndLog(" a resolution of 4 bits per sample.");
240 PrintAndLog(" lf read");
241 PrintAndLog(" Performs a read (active field)");
242 PrintAndLog(" lf snoop");
243 PrintAndLog(" Performs a snoop (no active field)");
244 return 0;
245 }
246
247 int CmdLFSetConfig(const char *Cmd)
248 {
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
254 bool errors = false;
255 int trigger_threshold =-1;//Means no change
256 uint8_t unsigned_trigg = 0;
257
258 uint8_t cmdp =0;
259 while(param_getchar(Cmd, cmdp) != 0x00)
260 {
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;
280 if(!errors) {
281 trigger_threshold = unsigned_trigg;
282 if (trigger_threshold > 0) g_lf_threshold_set = true;
283 }
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;
303 }
304 if(cmdp == 0)
305 {
306 errors = 1;// No args
307 }
308
309 //Validations
310 if(errors)
311 {
312 return usage_lf_config();
313 }
314 //Bps is limited to 8, so fits in lower half of arg1
315 if(bps >> 4) bps = 8;
316
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));
323 clearCommandBuffer();
324 SendCommand(&c);
325 return 0;
326 }
327
328 int CmdLFRead(const char *Cmd)
329 {
330 if (offline) return 0;
331 uint8_t cmdp = 0;
332 bool arg1 = false;
333 if (param_getchar(Cmd, cmdp) == 'h')
334 {
335 return usage_lf_read();
336 }
337 if (param_getchar(Cmd, cmdp) == 's') arg1 = true; //suppress print
338 //And ship it to device
339 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {arg1,0,0}};
340 clearCommandBuffer();
341 SendCommand(&c);
342 if (g_lf_threshold_set) {
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 }
350 return 0;
351 }
352
353 int 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};
362 clearCommandBuffer();
363 SendCommand(&c);
364 WaitForResponse(CMD_ACK,NULL);
365 return 0;
366 }
367
368 static void ChkBitstream(const char *str)
369 {
370 int i;
371
372 /* convert to bitstream if necessary */
373 for (i = 0; i < (int)(GraphTraceLen / 2); i++){
374 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) {
375 CmdGetBitStream("");
376 break;
377 }
378 }
379 }
380 //Attempt to simulate any wave in buffer (one bit per output sample)
381 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
382 int CmdLFSim(const char *Cmd)
383 {
384 int i,j;
385 static int gap;
386
387 sscanf(Cmd, "%i", &gap);
388
389 // convert to bitstream if necessary
390
391 ChkBitstream(Cmd);
392
393 //can send only 512 bits at a time (1 byte sent per bit...)
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}};
397
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 }
405
406 printf("\n");
407 PrintAndLog("Starting to simulate");
408 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
409 clearCommandBuffer();
410 SendCommand(&c);
411 return 0;
412 }
413
414 int usage_lf_simfsk(void)
415 {
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;
428 }
429
430 int usage_lf_simask(void)
431 {
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");
441 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
442 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
443 return 0;
444 }
445
446 int usage_lf_simpsk(void)
447 {
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;
460 }
461
462 // by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert
463 // - allow pull data from DemodBuffer
464 int CmdLFfskSim(const char *Cmd)
465 {
466 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
467 // otherwise will need FChigh, FClow, Clock, and bitstream
468 uint8_t fcHigh=0, fcLow=0, clk=0;
469 uint8_t invert=0;
470 bool errors = false;
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) {
504 errors=true;
505 } else {
506 dataLen = hextobinarray((char *)data, hexData);
507 }
508 if (dataLen==0) errors=true;
509 if (errors) PrintAndLog ("Error getting hex data");
510 cmdp+=2;
511 break;
512 default:
513 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
514 errors = true;
515 break;
516 }
517 if(errors) break;
518 }
519 if(cmdp == 0 && DemodBufferLen == 0)
520 {
521 errors = true;// No args
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 }
542
543 //default if not found
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);
559 clearCommandBuffer();
560 SendCommand(&c);
561 return 0;
562 }
563
564 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
565 // - allow pull data from DemodBuffer
566 int CmdLFaskSim(const char *Cmd)
567 {
568 //autodetect clock from Graphbuffer if using demod buffer
569 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
570 uint8_t encoding = 1, separator = 0;
571 uint8_t clk=0, invert=0;
572 bool errors = false;
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) {
610 errors=true;
611 } else {
612 dataLen = hextobinarray((char *)data, hexData);
613 }
614 if (dataLen==0) errors=true;
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));
620 errors = true;
621 break;
622 }
623 if(errors) break;
624 }
625 if(cmdp == 0 && DemodBufferLen == 0)
626 {
627 errors = true;// No args
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);
653 clearCommandBuffer();
654 SendCommand(&c);
655 return 0;
656 }
657
658 // by marshmellow - sim psk data given carrier, clock, invert
659 // - allow pull data from DemodBuffer or parameters
660 int CmdLFpskSim(const char *Cmd)
661 {
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;
666 bool errors = false;
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) {
705 errors=true;
706 } else {
707 dataLen = hextobinarray((char *)data, hexData);
708 }
709 if (dataLen==0) errors=true;
710 if (errors) PrintAndLog ("Error getting hex data");
711 cmdp+=2;
712 break;
713 default:
714 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
715 errors = true;
716 break;
717 }
718 if (errors) break;
719 }
720 if (cmdp == 0 && DemodBufferLen == 0)
721 {
722 errors = true;// No args
723 }
724
725 //Validations
726 if (errors)
727 {
728 return usage_lf_simpsk();
729 }
730 if (dataLen == 0){ //using DemodBuffer
731 PrintAndLog("Getting Clocks");
732 if (clk==0) clk = GetPskClock("", false, false);
733 PrintAndLog("clk: %d",clk);
734 if (!carrier) carrier = GetPskCarrier("", false, false);
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);
761 clearCommandBuffer();
762 SendCommand(&c);
763
764 return 0;
765 }
766
767 int CmdLFSimBidir(const char *Cmd)
768 {
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;
775 }
776
777 int CmdVchDemod(const char *Cmd)
778 {
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;
851 }
852
853
854 //by marshmellow
855 int 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
872 //by marshmellow
873 int CmdLFfind(const char *Cmd)
874 {
875 uint32_t wordData = 0;
876 int ans=0;
877 size_t minLength = 1000;
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')){
894 CmdLFRead("s");
895 getSamples("30000",false);
896 } else if (GraphTraceLen < minLength) {
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
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
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 }
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
929 ans=CmdFSKdemodIO("");
930 if (ans>0) {
931 PrintAndLog("\nValid IO Prox ID Found!");
932 return CheckChipType(cmdp);
933 }
934
935 ans=CmdFSKdemodPyramid("");
936 if (ans>0) {
937 PrintAndLog("\nValid Pyramid ID Found!");
938 return CheckChipType(cmdp);
939 }
940
941 ans=CmdFSKdemodParadox("");
942 if (ans>0) {
943 PrintAndLog("\nValid Paradox ID Found!");
944 return CheckChipType(cmdp);
945 }
946
947 ans=CmdFSKdemodAWID("");
948 if (ans>0) {
949 PrintAndLog("\nValid AWID ID Found!");
950 return CheckChipType(cmdp);
951 }
952
953 ans=CmdFSKdemodHID("");
954 if (ans>0) {
955 PrintAndLog("\nValid HID Prox ID Found!");
956 return CheckChipType(cmdp);
957 }
958
959 ans=CmdAskEM410xDemod("");
960 if (ans>0) {
961 PrintAndLog("\nValid EM410x ID Found!");
962 return CheckChipType(cmdp);
963 }
964
965 ans=CmdVisa2kDemod("");
966 if (ans>0) {
967 PrintAndLog("\nValid Visa2000 ID Found!");
968 return CheckChipType(cmdp);
969 }
970
971 ans=CmdG_Prox_II_Demod("");
972 if (ans>0) {
973 PrintAndLog("\nValid G Prox II ID Found!");
974 return CheckChipType(cmdp);
975 }
976
977 ans=CmdFDXBdemodBI("");
978 if (ans>0) {
979 PrintAndLog("\nValid FDX-B ID Found!");
980 return CheckChipType(cmdp);
981 }
982
983 ans=EM4x50Read("", false);
984 if (ans>0) {
985 PrintAndLog("\nValid EM4x50 ID Found!");
986 return 1;
987 }
988
989 ans=CmdVikingDemod("");
990 if (ans>0) {
991 PrintAndLog("\nValid Viking ID Found!");
992 return CheckChipType(cmdp);
993 }
994
995 ans=CmdIndalaDecode("");
996 if (ans>0) {
997 PrintAndLog("\nValid Indala ID Found!");
998 return CheckChipType(cmdp);
999 }
1000
1001 ans=CmdPSKNexWatch("");
1002 if (ans>0) {
1003 PrintAndLog("\nValid NexWatch ID Found!");
1004 return CheckChipType(cmdp);
1005 }
1006
1007 PrintAndLog("\nNo Known Tags Found!\n");
1008 if (testRaw=='u' || testRaw=='U'){
1009 ans=CheckChipType(cmdp);
1010 //test unknown tag formats (raw mode)0
1011 PrintAndLog("\nChecking for Unknown tags:\n");
1012 ans=AutoCorrelate(4000, false, false);
1013 if (ans > 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
1014 ans=GetFskClock("",false,false);
1015 if (ans != 0){ //fsk
1016 ans=FSKrawDemod("",true);
1017 if (ans>0) {
1018 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1019 return 1;
1020 }
1021 }
1022 bool st = true;
1023 ans=ASKDemod_ext("0 0 0",true,false,1,&st);
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'");
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");
1034 return 1;
1035 }
1036 PrintAndLog("\nNo Data Found!\n");
1037 }
1038 return 0;
1039 }
1040
1041 static command_t CommandTable[] =
1042 {
1043 {"help", CmdHelp, 1, "This help"},
1044 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
1045 {"cotag", CmdLFCOTAG, 1, "{ COTAG RFIDs... }"},
1046 {"em", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
1047 {"gproxii", CmdLF_G_Prox_II, 1, "{ G Prox II tags... }"},
1048 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
1049 {"hitag", CmdLFHitag, 1, "{ Hitag tags and transponders... }"},
1050 {"io", CmdLFIO, 1, "{ ioProx tags... }"},
1051 {"indala", CmdLFINDALA, 1, "{ Indala tags... }"},
1052 {"presco", CmdLFPresco, 1, "{ Presco RFIDs... }"},
1053 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 RFIDs... }"},
1054 {"pyramid", CmdLFPyramid, 1, "{ Farpointe/Pyramid RFIDs... }"},
1055 {"t55xx", CmdLFT55XX, 1, "{ T55xx RFIDs... }"},
1056 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
1057 {"viking", CmdLFViking, 1, "{ Viking tags... }"},
1058 {"visa2000", CmdLFVisa2k, 1, "{ Visa2000 RFIDs...}"},
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)"},
1060 {"config", CmdLFSetConfig, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1061 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
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)"},
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"},
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)"},
1069 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1070 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
1071 {NULL, NULL, 0, NULL}
1072 };
1073
1074 int CmdLF(const char *Cmd)
1075 {
1076 CmdsParse(CommandTable, Cmd);
1077 return 0;
1078 }
1079
1080 int CmdHelp(const char *Cmd)
1081 {
1082 CmdsHelp(CommandTable);
1083 return 0;
1084 }
Impressum, Datenschutz