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