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