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