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