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