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