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