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