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