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