]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
osx: fix annoying focus behaviour (#689)
[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 "cmdlf.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include "comms.h"
20 #include "lfdemod.h" // for psk2TOpsk1
21 #include "util.h" // for parsing cli command utils
22 #include "ui.h" // for show graph controls
23 #include "graph.h" // for graph data
24 #include "cmdparser.h" // for getting cli commands included in cmdmain.h
25 #include "cmdmain.h" // for sending cmds to device
26 #include "cmddata.h" // for `lf search`
27 #include "cmdlfawid.h" // for awid menu
28 #include "cmdlfem4x.h" // for em4x menu
29 #include "cmdlfhid.h" // for hid menu
30 #include "cmdlfhitag.h" // for hitag menu
31 #include "cmdlfio.h" // for ioprox menu
32 #include "cmdlft55xx.h" // for t55xx menu
33 #include "cmdlfti.h" // for ti menu
34 #include "cmdlfpresco.h" // for presco menu
35 #include "cmdlfpcf7931.h"// for pcf7931 menu
36 #include "cmdlfpyramid.h"// for pyramid menu
37 #include "cmdlfviking.h" // for viking menu
38 #include "cmdlfcotag.h" // for COTAG menu
39 #include "cmdlfvisa2000.h" // for VISA2000 menu
40 #include "cmdlfindala.h" // for indala menu
41 #include "cmdlfgproxii.h"// for gproxii menu
42 #include "cmdlffdx.h" // for fdx-b menu
43 #include "cmdlfparadox.h"// for paradox menu
44 #include "cmdlfnexwatch.h"//for nexwatch menu
45 #include "cmdlfjablotron.h" //for jablotron menu
46 #include "cmdlfnoralsy.h"// for noralsy menu
47 #include "cmdlfsecurakey.h"//for securakey menu
48 #include "cmdlfpac.h" // for pac menu
49
50 bool g_lf_threshold_set = false;
51 static int CmdHelp(const char *Cmd);
52
53
54
55 int usage_lf_cmdread(void)
56 {
57 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> ");
58 PrintAndLog("Options: ");
59 PrintAndLog(" h This help");
60 PrintAndLog(" d <delay> delay OFF period between bits (0 for bitbang mode)");
61 PrintAndLog(" z <zero> time period ZERO (antenna off in bitbang mode)");
62 PrintAndLog(" o <one> time period ONE (antenna on in bitbang mode)");
63 PrintAndLog(" c <cmd> Command bytes");
64 PrintAndLog(" ************* All periods in microseconds");
65 PrintAndLog(" ************* Use lf config to configure options.");
66 PrintAndLog("Examples:");
67 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
68 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000");
69 return 0;
70 }
71
72 /* send a command before reading */
73 int CmdLFCommandRead(const char *Cmd)
74 {
75 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
76 bool errors = false;
77 //uint8_t divisor = 95; //125khz
78 uint8_t cmdp = 0;
79 while(param_getchar(Cmd, cmdp) != 0x00)
80 {
81 switch(param_getchar(Cmd, cmdp))
82 {
83 case 'h':
84 return usage_lf_cmdread();
85 case 'c':
86 param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes, sizeof(c.d.asBytes));
87 cmdp+=2;
88 break;
89 case 'd':
90 c.arg[0] = param_get32ex(Cmd, cmdp+1, 0, 10);
91 cmdp+=2;
92 break;
93 case 'z':
94 c.arg[1] = param_get32ex(Cmd, cmdp+1, 0, 10);
95 cmdp+=2;
96 break;
97 case 'o':
98 c.arg[2] = param_get32ex(Cmd, cmdp+1, 0, 10);
99 cmdp+=2;
100 break;
101 default:
102 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
103 errors = 1;
104 break;
105 }
106 if(errors) break;
107 }
108 // No args
109 if(cmdp == 0) errors = 1;
110
111 //Validations
112 if(errors) return usage_lf_cmdread();
113
114 clearCommandBuffer();
115 SendCommand(&c);
116
117 WaitForResponse(CMD_ACK,NULL);
118 getSamples(0, true);
119
120
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(" [# samples] # samples to collect (optional)");
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 bool lf_read(bool silent, uint32_t samples) {
330 if (IsOffline()) return false;
331 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {silent,samples,0}};
332 clearCommandBuffer();
333 //And ship it to device
334 SendCommand(&c);
335
336 UsbCommand resp;
337 if (g_lf_threshold_set) {
338 WaitForResponse(CMD_ACK,&resp);
339 } else {
340 if ( !WaitForResponseTimeout(CMD_ACK,&resp,2500) ) {
341 PrintAndLog("command execution time out");
342 return false;
343 }
344 }
345 // resp.arg[0] is bits read not bytes read.
346 getSamples(resp.arg[0]/8, silent);
347
348 return true;
349 }
350
351 int CmdLFRead(const char *Cmd)
352 {
353 uint8_t cmdp = 0;
354 bool silent = false;
355 if (param_getchar(Cmd, cmdp) == 'h')
356 {
357 return usage_lf_read();
358 }
359 if (param_getchar(Cmd, cmdp) == 's') {
360 silent = true; //suppress print
361 cmdp++;
362 }
363 uint32_t samples = param_get32ex(Cmd, cmdp, 0, 10);
364 return lf_read(silent, samples);
365 }
366
367 int CmdLFSnoop(const char *Cmd)
368 {
369 uint8_t cmdp =0;
370 if(param_getchar(Cmd, cmdp) == 'h')
371 {
372 return usage_lf_snoop();
373 }
374
375 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
376 clearCommandBuffer();
377 SendCommand(&c);
378 WaitForResponse(CMD_ACK,NULL);
379 getSamples(0, true);
380
381 return 0;
382 }
383
384 static void ChkBitstream(const char *str)
385 {
386 int i;
387
388 /* convert to bitstream if necessary */
389 for (i = 0; i < (int)(GraphTraceLen / 2); i++){
390 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) {
391 CmdGetBitStream("");
392 break;
393 }
394 }
395 }
396 //Attempt to simulate any wave in buffer (one bit per output sample)
397 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
398 int CmdLFSim(const char *Cmd)
399 {
400 int i,j;
401 static int gap;
402
403 sscanf(Cmd, "%i", &gap);
404
405 // convert to bitstream if necessary
406 ChkBitstream(Cmd);
407
408 //can send only 512 bits at a time (1 byte sent per bit...)
409 printf("Sending [%d bytes]", GraphTraceLen);
410 for (i = 0; i < GraphTraceLen; i += USB_CMD_DATA_SIZE) {
411 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
412
413 for (j = 0; j < USB_CMD_DATA_SIZE; j++) {
414 c.d.asBytes[j] = GraphBuffer[i+j];
415 }
416 SendCommand(&c);
417 WaitForResponse(CMD_ACK,NULL);
418 printf(".");
419 }
420
421 printf("\n");
422 PrintAndLog("Starting to simulate");
423 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
424 clearCommandBuffer();
425 SendCommand(&c);
426 return 0;
427 }
428
429 int usage_lf_simfsk(void)
430 {
431 //print help
432 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
433 PrintAndLog("Options: ");
434 PrintAndLog(" h This help");
435 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
436 PrintAndLog(" i invert data");
437 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
438 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
439 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
440 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
441 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
442 return 0;
443 }
444
445 int usage_lf_simask(void)
446 {
447 //print help
448 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
449 PrintAndLog("Options: ");
450 PrintAndLog(" h This help");
451 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
452 PrintAndLog(" i invert data");
453 PrintAndLog(" b sim ask/biphase");
454 PrintAndLog(" m sim ask/manchester - Default");
455 PrintAndLog(" r sim ask/raw");
456 PrintAndLog(" s add t55xx Sequence Terminator gap - default: no gaps (only manchester)");
457 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
458 return 0;
459 }
460
461 int usage_lf_simpsk(void)
462 {
463 //print help
464 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
465 PrintAndLog("Options: ");
466 PrintAndLog(" h This help");
467 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
468 PrintAndLog(" i invert data");
469 PrintAndLog(" 1 set PSK1 (default)");
470 PrintAndLog(" 2 set PSK2");
471 PrintAndLog(" 3 set PSK3");
472 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
473 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
474 return 0;
475 }
476
477 // by marshmellow - sim fsk data given clock, fcHigh, fcLow, invert
478 // - allow pull data from DemodBuffer
479 int CmdLFfskSim(const char *Cmd)
480 {
481 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
482 // otherwise will need FChigh, FClow, Clock, and bitstream
483 uint8_t fcHigh=0, fcLow=0, clk=0;
484 uint8_t invert=0;
485 bool errors = false;
486 char hexData[64] = {0x00}; // store entered hex data
487 uint8_t data[255] = {0x00};
488 int dataLen = 0;
489 uint8_t cmdp = 0;
490 while(param_getchar(Cmd, cmdp) != 0x00)
491 {
492 switch(param_getchar(Cmd, cmdp))
493 {
494 case 'h':
495 return usage_lf_simfsk();
496 case 'i':
497 invert = 1;
498 cmdp++;
499 break;
500 case 'c':
501 errors |= param_getdec(Cmd,cmdp+1,&clk);
502 cmdp+=2;
503 break;
504 case 'H':
505 errors |= param_getdec(Cmd,cmdp+1,&fcHigh);
506 cmdp+=2;
507 break;
508 case 'L':
509 errors |= param_getdec(Cmd,cmdp+1,&fcLow);
510 cmdp+=2;
511 break;
512 //case 's':
513 // separator=1;
514 // cmdp++;
515 // break;
516 case 'd':
517 dataLen = param_getstr(Cmd, cmdp+1, hexData, sizeof(hexData));
518 if (dataLen==0) {
519 errors=true;
520 } else {
521 dataLen = hextobinarray((char *)data, hexData);
522 }
523 if (dataLen==0) errors=true;
524 if (errors) PrintAndLog ("Error getting hex data");
525 cmdp+=2;
526 break;
527 default:
528 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
529 errors = true;
530 break;
531 }
532 if(errors) break;
533 }
534 if(cmdp == 0 && DemodBufferLen == 0)
535 {
536 errors = true;// No args
537 }
538
539 //Validations
540 if(errors)
541 {
542 return usage_lf_simfsk();
543 }
544 int firstClockEdge = 0;
545 if (dataLen == 0){ //using DemodBuffer
546 if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all
547 uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0, &firstClockEdge);
548 if (ans==0){
549 if (!fcHigh) fcHigh=10;
550 if (!fcLow) fcLow=8;
551 if (!clk) clk=50;
552 }
553 }
554 } else {
555 setDemodBuf(data, dataLen, 0);
556 }
557
558 //default if not found
559 if (clk == 0) clk = 50;
560 if (fcHigh == 0) fcHigh = 10;
561 if (fcLow == 0) fcLow = 8;
562
563 uint16_t arg1, arg2;
564 arg1 = fcHigh << 8 | fcLow;
565 arg2 = invert << 8 | clk;
566 size_t size = DemodBufferLen;
567 if (size > USB_CMD_DATA_SIZE) {
568 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
569 size = USB_CMD_DATA_SIZE;
570 }
571 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
572
573 memcpy(c.d.asBytes, DemodBuffer, size);
574 clearCommandBuffer();
575 SendCommand(&c);
576 return 0;
577 }
578
579 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
580 // - allow pull data from DemodBuffer
581 int CmdLFaskSim(const char *Cmd)
582 {
583 //autodetect clock from Graphbuffer if using demod buffer
584 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
585 uint8_t encoding = 1, separator = 0;
586 uint8_t clk=0, invert=0;
587 bool errors = false;
588 char hexData[64] = {0x00};
589 uint8_t data[255]= {0x00}; // store entered hex data
590 int dataLen = 0;
591 uint8_t cmdp = 0;
592 while(param_getchar(Cmd, cmdp) != 0x00)
593 {
594 switch(param_getchar(Cmd, cmdp))
595 {
596 case 'h':
597 return usage_lf_simask();
598 case 'i':
599 invert = 1;
600 cmdp++;
601 break;
602 case 'c':
603 errors |= param_getdec(Cmd,cmdp+1,&clk);
604 cmdp+=2;
605 break;
606 case 'b':
607 encoding=2; //biphase
608 cmdp++;
609 break;
610 case 'm':
611 encoding=1;
612 cmdp++;
613 break;
614 case 'r':
615 encoding=0;
616 cmdp++;
617 break;
618 case 's':
619 separator=1;
620 cmdp++;
621 break;
622 case 'd':
623 dataLen = param_getstr(Cmd, cmdp+1, hexData, sizeof(hexData));
624 if (dataLen==0) {
625 errors=true;
626 } else {
627 dataLen = hextobinarray((char *)data, hexData);
628 }
629 if (dataLen==0) errors=true;
630 if (errors) PrintAndLog ("Error getting hex data, datalen: %d",dataLen);
631 cmdp+=2;
632 break;
633 default:
634 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
635 errors = true;
636 break;
637 }
638 if(errors) break;
639 }
640 if(cmdp == 0 && DemodBufferLen == 0)
641 {
642 errors = true;// No args
643 }
644
645 //Validations
646 if(errors)
647 {
648 return usage_lf_simask();
649 }
650 if (dataLen == 0){ //using DemodBuffer
651 if (clk == 0) clk = GetAskClock("0", false, false);
652 } else {
653 setDemodBuf(data, dataLen, 0);
654 }
655 if (clk == 0) clk = 64;
656 if (encoding == 0) clk = clk/2; //askraw needs to double the clock speed
657 uint16_t arg1, arg2;
658 size_t size=DemodBufferLen;
659 arg1 = clk << 8 | encoding;
660 arg2 = invert << 8 | separator;
661 if (size > USB_CMD_DATA_SIZE) {
662 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
663 size = USB_CMD_DATA_SIZE;
664 }
665 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
666 PrintAndLog("preparing to sim ask data: %d bits", size);
667 memcpy(c.d.asBytes, DemodBuffer, size);
668 clearCommandBuffer();
669 SendCommand(&c);
670 return 0;
671 }
672
673 // by marshmellow - sim psk data given carrier, clock, invert
674 // - allow pull data from DemodBuffer or parameters
675 int CmdLFpskSim(const char *Cmd)
676 {
677 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
678 //will need carrier, Clock, and bitstream
679 uint8_t carrier=0, clk=0;
680 uint8_t invert=0;
681 bool errors = false;
682 char hexData[64] = {0x00}; // store entered hex data
683 uint8_t data[255] = {0x00};
684 int dataLen = 0;
685 uint8_t cmdp = 0;
686 uint8_t pskType = 1;
687 while(param_getchar(Cmd, cmdp) != 0x00)
688 {
689 switch(param_getchar(Cmd, cmdp))
690 {
691 case 'h':
692 return usage_lf_simpsk();
693 case 'i':
694 invert = 1;
695 cmdp++;
696 break;
697 case 'c':
698 errors |= param_getdec(Cmd,cmdp+1,&clk);
699 cmdp+=2;
700 break;
701 case 'r':
702 errors |= param_getdec(Cmd,cmdp+1,&carrier);
703 cmdp+=2;
704 break;
705 case '1':
706 pskType=1;
707 cmdp++;
708 break;
709 case '2':
710 pskType=2;
711 cmdp++;
712 break;
713 case '3':
714 pskType=3;
715 cmdp++;
716 break;
717 case 'd':
718 dataLen = param_getstr(Cmd, cmdp+1, hexData, sizeof(hexData));
719 if (dataLen==0) {
720 errors=true;
721 } else {
722 dataLen = hextobinarray((char *)data, hexData);
723 }
724 if (dataLen==0) errors=true;
725 if (errors) PrintAndLog ("Error getting hex data");
726 cmdp+=2;
727 break;
728 default:
729 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
730 errors = true;
731 break;
732 }
733 if (errors) break;
734 }
735 if (cmdp == 0 && DemodBufferLen == 0)
736 {
737 errors = true;// No args
738 }
739
740 //Validations
741 if (errors)
742 {
743 return usage_lf_simpsk();
744 }
745 if (dataLen == 0){ //using DemodBuffer
746 PrintAndLog("Getting Clocks");
747 if (clk==0) clk = GetPskClock("", false, false);
748 PrintAndLog("clk: %d",clk);
749 if (!carrier) carrier = GetPskCarrier("", false, false);
750 PrintAndLog("carrier: %d", carrier);
751 } else {
752 setDemodBuf(data, dataLen, 0);
753 }
754
755 if (clk <= 0) clk = 32;
756 if (carrier == 0) carrier = 2;
757 if (pskType != 1){
758 if (pskType == 2){
759 //need to convert psk2 to psk1 data before sim
760 psk2TOpsk1(DemodBuffer, DemodBufferLen);
761 } else {
762 PrintAndLog("Sorry, PSK3 not yet available");
763 }
764 }
765 uint16_t arg1, arg2;
766 arg1 = clk << 8 | carrier;
767 arg2 = invert;
768 size_t size=DemodBufferLen;
769 if (size > USB_CMD_DATA_SIZE) {
770 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
771 size=USB_CMD_DATA_SIZE;
772 }
773 UsbCommand c = {CMD_PSK_SIM_TAG, {arg1, arg2, size}};
774 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size);
775 memcpy(c.d.asBytes, DemodBuffer, size);
776 clearCommandBuffer();
777 SendCommand(&c);
778
779 return 0;
780 }
781
782 int CmdLFSimBidir(const char *Cmd)
783 {
784 // Set ADC to twice the carrier for a slight supersampling
785 // HACK: not implemented in ARMSRC.
786 PrintAndLog("Not implemented yet.");
787 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
788 SendCommand(&c);
789 return 0;
790 }
791
792 int CmdVchDemod(const char *Cmd)
793 {
794 // Is this the entire sync pattern, or does this also include some
795 // data bits that happen to be the same everywhere? That would be
796 // lovely to know.
797 static const int SyncPattern[] = {
798 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
799 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
800 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
801 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
802 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
803 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
804 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
805 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
806 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
807 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
808 };
809
810 // So first, we correlate for the sync pattern, and mark that.
811 int bestCorrel = 0, bestPos = 0;
812 int i;
813 // It does us no good to find the sync pattern, with fewer than
814 // 2048 samples after it...
815 for (i = 0; i < (GraphTraceLen-2048); i++) {
816 int sum = 0;
817 int j;
818 for (j = 0; j < arraylen(SyncPattern); j++) {
819 sum += GraphBuffer[i+j]*SyncPattern[j];
820 }
821 if (sum > bestCorrel) {
822 bestCorrel = sum;
823 bestPos = i;
824 }
825 }
826 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
827
828 char bits[257];
829 bits[256] = '\0';
830
831 int worst = INT_MAX;
832 int worstPos = 0;
833
834 for (i = 0; i < 2048; i += 8) {
835 int sum = 0;
836 int j;
837 for (j = 0; j < 8; j++) {
838 sum += GraphBuffer[bestPos+i+j];
839 }
840 if (sum < 0) {
841 bits[i/8] = '.';
842 } else {
843 bits[i/8] = '1';
844 }
845 if(abs(sum) < worst) {
846 worst = abs(sum);
847 worstPos = i;
848 }
849 }
850 PrintAndLog("bits:");
851 PrintAndLog("%s", bits);
852 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
853
854 if (strcmp(Cmd, "clone")==0) {
855 GraphTraceLen = 0;
856 char *s;
857 for(s = bits; *s; s++) {
858 int j;
859 for(j = 0; j < 16; j++) {
860 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
861 }
862 }
863 RepaintGraphWindow();
864 }
865 return 0;
866 }
867
868
869 //by marshmellow
870 int CheckChipType(char cmdp) {
871 uint32_t wordData = 0;
872
873 if (IsOffline() || cmdp == '1') return 0;
874
875 save_restoreGB(GRAPH_SAVE);
876 save_restoreDB(GRAPH_SAVE);
877 //check for em4x05/em4x69 chips first
878 if (EM4x05Block0Test(&wordData)) {
879 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nTry lf em 4x05... commands\n");
880 save_restoreGB(GRAPH_RESTORE);
881 save_restoreDB(GRAPH_RESTORE);
882 return 1;
883 }
884
885 //check for t55xx chip...
886 if (tryDetectP1(true)) {
887 PrintAndLog("\nValid T55xx Chip Found\nTry lf t55xx ... commands\n");
888 save_restoreGB(GRAPH_RESTORE);
889 save_restoreDB(GRAPH_RESTORE);
890 return 1;
891 }
892 save_restoreGB(GRAPH_RESTORE);
893 save_restoreDB(GRAPH_RESTORE);
894 return 0;
895 }
896
897 //by marshmellow
898 int CmdLFfind(const char *Cmd)
899 {
900 uint32_t wordData = 0;
901 int ans=0;
902 size_t minLength = 1000;
903 char cmdp = param_getchar(Cmd, 0);
904 char testRaw = param_getchar(Cmd, 1);
905 if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') {
906 PrintAndLog("Usage: lf search <0|1> [u]");
907 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
908 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
909 PrintAndLog("");
910 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
911 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
912 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
913 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
914
915 return 0;
916 }
917
918 if (!IsOffline() && (cmdp != '1')) {
919 lf_read(true, 30000);
920 } else if (GraphTraceLen < minLength) {
921 PrintAndLog("Data in Graphbuffer was too small.");
922 return 0;
923 }
924 if (cmdp == 'u' || cmdp == 'U') testRaw = 'u';
925
926 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
927 PrintAndLog("False Positives ARE possible\n");
928 PrintAndLog("\nChecking for known tags:\n");
929
930 size_t testLen = minLength;
931 // only run if graphbuffer is just noise as it should be for hitag/cotag
932 if (graphJustNoise(GraphBuffer, testLen)) {
933 // only run these tests if we are in online mode
934 if (!IsOffline() && (cmdp != '1')) {
935 // test for em4x05 in reader talk first mode.
936 if (EM4x05Block0Test(&wordData)) {
937 PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n");
938 return 1;
939 }
940 ans=CmdLFHitagReader("26"); // 26 = RHT2F_UID_ONLY
941 if (ans==0) {
942 return 1;
943 }
944 ans=CmdCOTAGRead("");
945 if (ans>0) {
946 PrintAndLog("\nValid COTAG ID Found!");
947 return 1;
948 }
949 }
950 PrintAndLog("\nNo Data Found! - maybe not an LF tag?\n");
951 return 0;
952 }
953
954 // TODO test for modulation then only test formats that use that modulation
955
956 ans=CmdFSKdemodIO("");
957 if (ans>0) {
958 PrintAndLog("\nValid IO Prox ID Found!");
959 return CheckChipType(cmdp);
960 }
961
962 ans=CmdFSKdemodPyramid("");
963 if (ans>0) {
964 PrintAndLog("\nValid Pyramid ID Found!");
965 return CheckChipType(cmdp);
966 }
967
968 ans=CmdFSKdemodParadox("");
969 if (ans>0) {
970 PrintAndLog("\nValid Paradox ID Found!");
971 return CheckChipType(cmdp);
972 }
973
974 ans=CmdFSKdemodAWID("");
975 if (ans>0) {
976 PrintAndLog("\nValid AWID ID Found!");
977 return CheckChipType(cmdp);
978 }
979
980 ans=CmdFSKdemodHID("");
981 if (ans>0) {
982 PrintAndLog("\nValid HID Prox ID Found!");
983 return CheckChipType(cmdp);
984 }
985
986 ans=CmdAskEM410xDemod("");
987 if (ans>0) {
988 PrintAndLog("\nValid EM410x ID Found!");
989 return CheckChipType(cmdp);
990 }
991
992 ans=CmdVisa2kDemod("");
993 if (ans>0) {
994 PrintAndLog("\nValid Visa2000 ID Found!");
995 return CheckChipType(cmdp);
996 }
997
998 ans=CmdG_Prox_II_Demod("");
999 if (ans>0) {
1000 PrintAndLog("\nValid G Prox II ID Found!");
1001 return CheckChipType(cmdp);
1002 }
1003
1004 ans=CmdFdxDemod(""); //biphase
1005 if (ans>0) {
1006 PrintAndLog("\nValid FDX-B ID Found!");
1007 return CheckChipType(cmdp);
1008 }
1009
1010 ans=EM4x50Read("", false); //ask
1011 if (ans>0) {
1012 PrintAndLog("\nValid EM4x50 ID Found!");
1013 return 1;
1014 }
1015
1016 ans=CmdJablotronDemod("");
1017 if (ans>0) {
1018 PrintAndLog("\nValid Jablotron ID Found!");
1019 return CheckChipType(cmdp);
1020 }
1021
1022 ans=CmdNoralsyDemod("");
1023 if (ans>0) {
1024 PrintAndLog("\nValid Noralsy ID Found!");
1025 return CheckChipType(cmdp);
1026 }
1027
1028 ans=CmdSecurakeyDemod("");
1029 if (ans>0) {
1030 PrintAndLog("\nValid Securakey ID Found!");
1031 return CheckChipType(cmdp);
1032 }
1033
1034 ans=CmdVikingDemod("");
1035 if (ans>0) {
1036 PrintAndLog("\nValid Viking ID Found!");
1037 return CheckChipType(cmdp);
1038 }
1039
1040 ans=CmdIndalaDecode(""); //psk
1041 if (ans>0) {
1042 PrintAndLog("\nValid Indala ID Found!");
1043 return CheckChipType(cmdp);
1044 }
1045
1046 ans=CmdPSKNexWatch("");
1047 if (ans>0) {
1048 PrintAndLog("\nValid NexWatch ID Found!");
1049 return CheckChipType(cmdp);
1050 }
1051
1052 ans=CmdPacDemod("");
1053 if (ans>0) {
1054 PrintAndLog("\nValid PAC/Stanley ID Found!");
1055 return CheckChipType(cmdp);
1056 }
1057
1058 PrintAndLog("\nNo Known Tags Found!\n");
1059 if (testRaw=='u' || testRaw=='U') {
1060 //ans=CheckChipType(cmdp);
1061 //test unknown tag formats (raw mode)0
1062 PrintAndLog("\nChecking for Unknown tags:\n");
1063 ans=AutoCorrelate(GraphBuffer, GraphBuffer, GraphTraceLen, 4000, false, false);
1064 if (ans > 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
1065 ans=GetFskClock("",false,false);
1066 if (ans != 0) { //fsk
1067 ans=FSKrawDemod("",true);
1068 if (ans>0) {
1069 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1070 return CheckChipType(cmdp);
1071 }
1072 }
1073 bool st = true;
1074 ans=ASKDemod_ext("0 0 0",true,false,1,&st);
1075 if (ans>0) {
1076 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1077 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1078 return CheckChipType(cmdp);
1079 }
1080 ans=CmdPSK1rawDemod("");
1081 if (ans>0) {
1082 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1083 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1084 PrintAndLog("\nCould also be NRZ - try 'data rawdemod nr'");
1085 return CheckChipType(cmdp);
1086 }
1087 ans = CheckChipType(cmdp);
1088 PrintAndLog("\nNo Data Found!\n");
1089 }
1090 return 0;
1091 }
1092
1093 static command_t CommandTable[] =
1094 {
1095 {"help", CmdHelp, 1, "This help"},
1096 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
1097 {"cotag", CmdLFCOTAG, 1, "{ COTAG CHIPs... }"},
1098 {"em", CmdLFEM4X, 1, "{ EM4X CHIPs & RFIDs... }"},
1099 {"fdx", CmdLFFdx, 1, "{ FDX-B RFIDs... }"},
1100 {"gproxii", CmdLF_G_Prox_II, 1, "{ G Prox II RFIDs... }"},
1101 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
1102 {"hitag", CmdLFHitag, 1, "{ Hitag CHIPs... }"},
1103 {"io", CmdLFIO, 1, "{ ioProx RFIDs... }"},
1104 {"indala", CmdLFINDALA, 1, "{ Indala RFIDs... }"},
1105 {"jablotron", CmdLFJablotron, 1, "{ Jablotron RFIDs... }"},
1106 {"nexwatch", CmdLFNexWatch, 1, "{ NexWatch RFIDs... }"},
1107 {"noralsy", CmdLFNoralsy, 1, "{ Noralsy RFIDs... }"},
1108 {"pac", CmdLFPac, 1, "{ PAC/Stanley RFIDs... }"},
1109 {"paradox", CmdLFParadox, 1, "{ Paradox RFIDs... }"},
1110 {"presco", CmdLFPresco, 1, "{ Presco RFIDs... }"},
1111 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 CHIPs... }"},
1112 {"pyramid", CmdLFPyramid, 1, "{ Farpointe/Pyramid RFIDs... }"},
1113 {"securakey", CmdLFSecurakey, 1, "{ Securakey RFIDs... }"},
1114 {"t55xx", CmdLFT55XX, 1, "{ T55xx CHIPs... }"},
1115 {"ti", CmdLFTI, 1, "{ TI CHIPs... }"},
1116 {"viking", CmdLFViking, 1, "{ Viking RFIDs... }"},
1117 {"visa2000", CmdLFVisa2k, 1, "{ Visa2000 RFIDs... }"},
1118 {"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)"},
1119 {"config", CmdLFSetConfig, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1120 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
1121 {"read", CmdLFRead, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1122 {"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"},
1123 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1124 {"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"},
1125 {"simfsk", CmdLFfskSim, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1126 {"simpsk", CmdLFpskSim, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1127 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1128 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1129 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
1130 {NULL, NULL, 0, NULL}
1131 };
1132
1133 int CmdLF(const char *Cmd)
1134 {
1135 CmdsParse(CommandTable, Cmd);
1136 return 0;
1137 }
1138
1139 int CmdHelp(const char *Cmd)
1140 {
1141 CmdsHelp(CommandTable);
1142 return 0;
1143 }
Impressum, Datenschutz