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