]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdlf.c
Update README.md
[proxmark3-svn] / client / cmdlf.c
... / ...
CommitLineData
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 <stdbool.h>
16#include <stdint.h>
17#include "proxmark3.h"
18#include "cmdlf.h"
19#include "lfdemod.h" // for psk2TOpsk1
20#include "util.h" // for parsing cli command utils
21#include "ui.h" // for show graph controls
22#include "graph.h" // for graph data
23#include "cmdparser.h" // for getting cli commands included in cmdmain.h
24#include "cmdmain.h" // for sending cmds to device
25#include "data.h" // for GetFromBigBuf
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
50bool g_lf_threshold_set = false;
51static int CmdHelp(const char *Cmd);
52
53
54
55int 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 */
73int 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
124int 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
203int 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}
213int 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
223int 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
248int 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
329bool lf_read(bool silent, uint32_t samples) {
330 if (offline) 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
351int 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
367int 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
384static 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.
398int 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
429int 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
445int 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
461int 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
479int 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
581int 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
675int 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
782int 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
792int 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
870int CheckChipType(char cmdp) {
871 uint32_t wordData = 0;
872
873 if (offline || 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
898int 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 (!offline && (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 (!offline && (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 return 0;
951 }
952
953 // TODO test for modulation then only test formats that use that modulation
954
955 ans=CmdFSKdemodIO("");
956 if (ans>0) {
957 PrintAndLog("\nValid IO Prox ID Found!");
958 return CheckChipType(cmdp);
959 }
960
961 ans=CmdFSKdemodPyramid("");
962 if (ans>0) {
963 PrintAndLog("\nValid Pyramid ID Found!");
964 return CheckChipType(cmdp);
965 }
966
967 ans=CmdFSKdemodParadox("");
968 if (ans>0) {
969 PrintAndLog("\nValid Paradox ID Found!");
970 return CheckChipType(cmdp);
971 }
972
973 ans=CmdFSKdemodAWID("");
974 if (ans>0) {
975 PrintAndLog("\nValid AWID ID Found!");
976 return CheckChipType(cmdp);
977 }
978
979 ans=CmdFSKdemodHID("");
980 if (ans>0) {
981 PrintAndLog("\nValid HID Prox ID Found!");
982 return CheckChipType(cmdp);
983 }
984
985 ans=CmdAskEM410xDemod("");
986 if (ans>0) {
987 PrintAndLog("\nValid EM410x ID Found!");
988 return CheckChipType(cmdp);
989 }
990
991 ans=CmdVisa2kDemod("");
992 if (ans>0) {
993 PrintAndLog("\nValid Visa2000 ID Found!");
994 return CheckChipType(cmdp);
995 }
996
997 ans=CmdG_Prox_II_Demod("");
998 if (ans>0) {
999 PrintAndLog("\nValid G Prox II ID Found!");
1000 return CheckChipType(cmdp);
1001 }
1002
1003 ans=CmdFdxDemod(""); //biphase
1004 if (ans>0) {
1005 PrintAndLog("\nValid FDX-B ID Found!");
1006 return CheckChipType(cmdp);
1007 }
1008
1009 ans=EM4x50Read("", false); //ask
1010 if (ans>0) {
1011 PrintAndLog("\nValid EM4x50 ID Found!");
1012 return 1;
1013 }
1014
1015 ans=CmdJablotronDemod("");
1016 if (ans>0) {
1017 PrintAndLog("\nValid Jablotron ID Found!");
1018 return CheckChipType(cmdp);
1019 }
1020
1021 ans=CmdNoralsyDemod("");
1022 if (ans>0) {
1023 PrintAndLog("\nValid Noralsy ID Found!");
1024 return CheckChipType(cmdp);
1025 }
1026
1027 ans=CmdSecurakeyDemod("");
1028 if (ans>0) {
1029 PrintAndLog("\nValid Securakey ID Found!");
1030 return CheckChipType(cmdp);
1031 }
1032
1033 ans=CmdVikingDemod("");
1034 if (ans>0) {
1035 PrintAndLog("\nValid Viking ID Found!");
1036 return CheckChipType(cmdp);
1037 }
1038
1039 ans=CmdIndalaDecode(""); //psk
1040 if (ans>0) {
1041 PrintAndLog("\nValid Indala ID Found!");
1042 return CheckChipType(cmdp);
1043 }
1044
1045 ans=CmdPSKNexWatch("");
1046 if (ans>0) {
1047 PrintAndLog("\nValid NexWatch ID Found!");
1048 return CheckChipType(cmdp);
1049 }
1050
1051 ans=CmdPacDemod("");
1052 if (ans>0) {
1053 PrintAndLog("\nValid PAC/Stanley ID Found!");
1054 return CheckChipType(cmdp);
1055 }
1056
1057 PrintAndLog("\nNo Known Tags Found!\n");
1058 if (testRaw=='u' || testRaw=='U') {
1059 //ans=CheckChipType(cmdp);
1060 //test unknown tag formats (raw mode)0
1061 PrintAndLog("\nChecking for Unknown tags:\n");
1062 ans=AutoCorrelate(GraphBuffer, GraphBuffer, GraphTraceLen, 4000, false, false);
1063 if (ans > 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
1064 ans=GetFskClock("",false,false);
1065 if (ans != 0) { //fsk
1066 ans=FSKrawDemod("",true);
1067 if (ans>0) {
1068 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1069 return CheckChipType(cmdp);
1070 }
1071 }
1072 bool st = true;
1073 ans=ASKDemod_ext("0 0 0",true,false,1,&st);
1074 if (ans>0) {
1075 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1076 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1077 return CheckChipType(cmdp);
1078 }
1079 ans=CmdPSK1rawDemod("");
1080 if (ans>0) {
1081 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1082 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1083 PrintAndLog("\nCould also be NRZ - try 'data rawdemod nr'");
1084 return CheckChipType(cmdp);
1085 }
1086 ans = CheckChipType(cmdp);
1087 PrintAndLog("\nNo Data Found!\n");
1088 }
1089 return 0;
1090}
1091
1092static command_t CommandTable[] =
1093{
1094 {"help", CmdHelp, 1, "This help"},
1095 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
1096 {"cotag", CmdLFCOTAG, 1, "{ COTAG CHIPs... }"},
1097 {"em", CmdLFEM4X, 1, "{ EM4X CHIPs & RFIDs... }"},
1098 {"fdx", CmdLFFdx, 1, "{ FDX-B RFIDs... }"},
1099 {"gproxii", CmdLF_G_Prox_II, 1, "{ G Prox II RFIDs... }"},
1100 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
1101 {"hitag", CmdLFHitag, 1, "{ Hitag CHIPs... }"},
1102 {"io", CmdLFIO, 1, "{ ioProx RFIDs... }"},
1103 {"indala", CmdLFINDALA, 1, "{ Indala RFIDs... }"},
1104 {"jablotron", CmdLFJablotron, 1, "{ Jablotron RFIDs... }"},
1105 {"nexwatch", CmdLFNexWatch, 1, "{ NexWatch RFIDs... }"},
1106 {"noralsy", CmdLFNoralsy, 1, "{ Noralsy RFIDs... }"},
1107 {"pac", CmdLFPac, 1, "{ PAC/Stanley RFIDs... }"},
1108 {"paradox", CmdLFParadox, 1, "{ Paradox RFIDs... }"},
1109 {"presco", CmdLFPresco, 1, "{ Presco RFIDs... }"},
1110 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 CHIPs... }"},
1111 {"pyramid", CmdLFPyramid, 1, "{ Farpointe/Pyramid RFIDs... }"},
1112 {"securakey", CmdLFSecurakey, 1, "{ Securakey RFIDs... }"},
1113 {"t55xx", CmdLFT55XX, 1, "{ T55xx CHIPs... }"},
1114 {"ti", CmdLFTI, 1, "{ TI CHIPs... }"},
1115 {"viking", CmdLFViking, 1, "{ Viking RFIDs... }"},
1116 {"visa2000", CmdLFVisa2k, 1, "{ Visa2000 RFIDs... }"},
1117 {"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)"},
1118 {"config", CmdLFSetConfig, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1119 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
1120 {"read", CmdLFRead, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1121 {"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"},
1122 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1123 {"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"},
1124 {"simfsk", CmdLFfskSim, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] -- Simulate LF FSK tag from demodbuffer or input"},
1125 {"simpsk", CmdLFpskSim, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] -- Simulate LF PSK tag from demodbuffer or input"},
1126 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1127 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
1128 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
1129 {NULL, NULL, 0, NULL}
1130};
1131
1132int CmdLF(const char *Cmd)
1133{
1134 CmdsParse(CommandTable, Cmd);
1135 return 0;
1136}
1137
1138int CmdHelp(const char *Cmd)
1139{
1140 CmdsHelp(CommandTable);
1141 return 0;
1142}
Impressum, Datenschutz