CHG: generic code clean up. Removal of commented code.
[legacy-proxmark3.git] / client / cmdlf.c
blob18bcf747e7c16f109a4c0220153bbf6bc9810db2
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 //-----------------------------------------------------------------------------
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 "cmdlf.h"
23 #include "cmdlfhid.h"
24 #include "cmdlfti.h"
25 #include "cmdlfem4x.h"
26 #include "cmdlfhitag.h"
27 #include "cmdlft55xx.h"
28 #include "cmdlfpcf7931.h"
29 #include "cmdlfio.h"
31 static int CmdHelp(const char *Cmd);
33 /* send a command before reading */
34 int CmdLFCommandRead(const char *Cmd)
36 static char dummy[3];
38 dummy[0]= ' ';
40 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
41 sscanf(Cmd, "%"lli" %"lli" %"lli" %s %s", &c.arg[0], &c.arg[1], &c.arg[2],(char*)(&c.d.asBytes),(char*)(&dummy+1));
42 // in case they specified 'h'
43 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
44 SendCommand(&c);
45 return 0;
48 int CmdFlexdemod(const char *Cmd)
50 int i;
51 for (i = 0; i < GraphTraceLen; ++i) {
52 if (GraphBuffer[i] < 0) {
53 GraphBuffer[i] = -1;
54 } else {
55 GraphBuffer[i] = 1;
59 #define LONG_WAIT 100
60 int start;
61 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
62 int first = GraphBuffer[start];
63 for (i = start; i < start + LONG_WAIT; i++) {
64 if (GraphBuffer[i] != first) {
65 break;
68 if (i == (start + LONG_WAIT)) {
69 break;
72 if (start == GraphTraceLen - LONG_WAIT) {
73 PrintAndLog("nothing to wait for");
74 return 0;
77 GraphBuffer[start] = 2;
78 GraphBuffer[start+1] = -2;
79 uint8_t bits[64] = {0x00};
81 int bit, sum;
82 i = start;
83 for (bit = 0; bit < 64; bit++) {
84 sum = 0;
85 for (int j = 0; j < 16; j++) {
86 sum += GraphBuffer[i++];
89 bits[bit] = (sum > 0) ? 1 : 0;
91 PrintAndLog("bit %d sum %d", bit, sum);
94 for (bit = 0; bit < 64; bit++) {
95 int j;
96 int sum = 0;
97 for (j = 0; j < 16; j++) {
98 sum += GraphBuffer[i++];
100 if (sum > 0 && bits[bit] != 1) {
101 PrintAndLog("oops1 at %d", bit);
103 if (sum < 0 && bits[bit] != 0) {
104 PrintAndLog("oops2 at %d", bit);
108 // HACK writing back to graphbuffer.
109 GraphTraceLen = 32*64;
110 i = 0;
111 int phase = 0;
112 for (bit = 0; bit < 64; bit++) {
114 phase = (bits[bit] == 0) ? 0 : 1;
116 int j;
117 for (j = 0; j < 32; j++) {
118 GraphBuffer[i++] = phase;
119 phase = !phase;
123 RepaintGraphWindow();
124 return 0;
127 int CmdIndalaDemod(const char *Cmd)
129 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
131 int state = -1;
132 int count = 0;
133 int i, j;
135 // worst case with GraphTraceLen=64000 is < 4096
136 // under normal conditions it's < 2048
138 uint8_t rawbits[4096];
139 int rawbit = 0;
140 int worst = 0, worstPos = 0;
141 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
142 for (i = 0; i < GraphTraceLen-1; i += 2) {
143 count += 1;
144 if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
145 if (state == 0) {
146 for (j = 0; j < count - 8; j += 16) {
147 rawbits[rawbit++] = 0;
149 if ((abs(count - j)) > worst) {
150 worst = abs(count - j);
151 worstPos = i;
154 state = 1;
155 count = 0;
156 } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
157 if (state == 1) {
158 for (j = 0; j < count - 8; j += 16) {
159 rawbits[rawbit++] = 1;
161 if ((abs(count - j)) > worst) {
162 worst = abs(count - j);
163 worstPos = i;
166 state = 0;
167 count = 0;
171 if (rawbit>0){
172 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32);
173 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
174 } else {
175 return 0;
178 // Finding the start of a UID
179 int uidlen, long_wait;
180 if (strcmp(Cmd, "224") == 0) {
181 uidlen = 224;
182 long_wait = 30;
183 } else {
184 uidlen = 64;
185 long_wait = 29;
188 int start;
189 int first = 0;
190 for (start = 0; start <= rawbit - uidlen; start++) {
191 first = rawbits[start];
192 for (i = start; i < start + long_wait; i++) {
193 if (rawbits[i] != first) {
194 break;
197 if (i == (start + long_wait)) {
198 break;
202 if (start == rawbit - uidlen + 1) {
203 PrintAndLog("nothing to wait for");
204 return 0;
207 // Inverting signal if needed
208 if (first == 1) {
209 for (i = start; i < rawbit; i++) {
210 rawbits[i] = !rawbits[i];
214 // Dumping UID
215 uint8_t bits[224] = {0x00};
216 char showbits[225] = {0x00};
217 int bit;
218 i = start;
219 int times = 0;
221 if (uidlen > rawbit) {
222 PrintAndLog("Warning: not enough raw bits to get a full UID");
223 for (bit = 0; bit < rawbit; bit++) {
224 bits[bit] = rawbits[i++];
225 // As we cannot know the parity, let's use "." and "/"
226 showbits[bit] = '.' + bits[bit];
228 showbits[bit+1]='\0';
229 PrintAndLog("Partial UID=%s", showbits);
230 return 0;
231 } else {
232 for (bit = 0; bit < uidlen; bit++) {
233 bits[bit] = rawbits[i++];
234 showbits[bit] = '0' + bits[bit];
236 times = 1;
239 //convert UID to HEX
240 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
241 int idx;
242 uid1 = uid2 = 0;
244 if (uidlen==64){
245 for( idx=0; idx<64; idx++) {
246 if (showbits[idx] == '0') {
247 uid1=(uid1<<1)|(uid2>>31);
248 uid2=(uid2<<1)|0;
249 } else {
250 uid1=(uid1<<1)|(uid2>>31);
251 uid2=(uid2<<1)|1;
254 PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
256 else {
257 uid3 = uid4 = uid5 = uid6 = uid7 = 0;
259 for( idx=0; idx<224; idx++) {
260 uid1=(uid1<<1)|(uid2>>31);
261 uid2=(uid2<<1)|(uid3>>31);
262 uid3=(uid3<<1)|(uid4>>31);
263 uid4=(uid4<<1)|(uid5>>31);
264 uid5=(uid5<<1)|(uid6>>31);
265 uid6=(uid6<<1)|(uid7>>31);
267 if (showbits[idx] == '0')
268 uid7 = (uid7<<1) | 0;
269 else
270 uid7 = (uid7<<1) | 1;
272 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
275 // Checking UID against next occurrences
276 int failed = 0;
277 for (; i + uidlen <= rawbit;) {
278 failed = 0;
279 for (bit = 0; bit < uidlen; bit++) {
280 if (bits[bit] != rawbits[i++]) {
281 failed = 1;
282 break;
285 if (failed == 1) {
286 break;
288 times += 1;
291 PrintAndLog("Occurrences: %d (expected %d)", times, (rawbit - start) / uidlen);
293 // Remodulating for tag cloning
294 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
295 // since this changes graphbuffer data.
296 GraphTraceLen = 32*uidlen;
297 i = 0;
298 int phase = 0;
299 for (bit = 0; bit < uidlen; bit++) {
300 if (bits[bit] == 0) {
301 phase = 0;
302 } else {
303 phase = 1;
305 int j;
306 for (j = 0; j < 32; j++) {
307 GraphBuffer[i++] = phase;
308 phase = !phase;
312 RepaintGraphWindow();
313 return 1;
316 int CmdIndalaClone(const char *Cmd)
318 UsbCommand c;
319 unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
321 uid1 = uid2 = uid3 = uid4 = uid5 = uid6 = uid7 = 0;
322 int n = 0, i = 0;
324 if (strchr(Cmd,'l') != 0) {
325 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
326 uid1 = (uid1 << 4) | (uid2 >> 28);
327 uid2 = (uid2 << 4) | (uid3 >> 28);
328 uid3 = (uid3 << 4) | (uid4 >> 28);
329 uid4 = (uid4 << 4) | (uid5 >> 28);
330 uid5 = (uid5 << 4) | (uid6 >> 28);
331 uid6 = (uid6 << 4) | (uid7 >> 28);
332 uid7 = (uid7 << 4) | (n & 0xf);
334 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7);
335 c.cmd = CMD_INDALA_CLONE_TAG_L;
336 c.d.asDwords[0] = uid1;
337 c.d.asDwords[1] = uid2;
338 c.d.asDwords[2] = uid3;
339 c.d.asDwords[3] = uid4;
340 c.d.asDwords[4] = uid5;
341 c.d.asDwords[5] = uid6;
342 c.d.asDwords[6] = uid7;
343 } else {
344 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
345 uid1 = (uid1 << 4) | (uid2 >> 28);
346 uid2 = (uid2 << 4) | (n & 0xf);
348 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2);
349 c.cmd = CMD_INDALA_CLONE_TAG;
350 c.arg[0] = uid1;
351 c.arg[1] = uid2;
354 SendCommand(&c);
355 return 0;
358 int CmdLFRead(const char *Cmd)
360 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};
362 // 'h' means higher-low-frequency, 134 kHz
363 if(*Cmd == 'h') {
364 c.arg[0] = 1;
365 } else if (*Cmd == '\0') {
366 c.arg[0] = 0;
367 } else if (sscanf(Cmd, "%"lli, &c.arg[0]) != 1) {
368 PrintAndLog("Samples 1: 'lf read'");
369 PrintAndLog(" 2: 'lf read h'");
370 PrintAndLog(" 3: 'lf read <divisor>'");
371 return 0;
373 SendCommand(&c);
374 WaitForResponse(CMD_ACK,NULL);
375 return 0;
378 static void ChkBitstream(const char *str)
380 int i;
382 /* convert to bitstream if necessary */
383 for (i = 0; i < (int)(GraphTraceLen / 2); i++)
385 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0)
387 CmdBitstream(str);
388 break;
393 int CmdLFSim(const char *Cmd)
395 int i;
396 static int gap;
398 sscanf(Cmd, "%i", &gap);
400 /* convert to bitstream if necessary */
401 ChkBitstream(Cmd);
403 PrintAndLog("Sending data, please wait...");
404 for (i = 0; i < GraphTraceLen; i += 48) {
405 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
406 int j;
407 for (j = 0; j < 48; j++) {
408 c.d.asBytes[j] = GraphBuffer[i+j];
410 SendCommand(&c);
411 WaitForResponse(CMD_ACK,NULL);
414 PrintAndLog("Starting simulator...");
415 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
416 SendCommand(&c);
417 return 0;
420 int CmdLFSimBidir(const char *Cmd)
422 // Set ADC to twice the carrier for a slight supersampling
423 // HACK: not implemented in ARMSRC.
424 PrintAndLog("Not implemented yet.");
425 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
426 SendCommand(&c);
427 return 0;
430 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
431 int CmdLFSimManchester(const char *Cmd)
433 static int clock, gap;
434 static char data[1024], gapstring[8];
436 sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap);
438 ClearGraph(0);
440 for (int i = 0; i < strlen(data) ; ++i)
441 AppendGraph(0, clock, data[i]- '0');
443 CmdManchesterMod("");
445 RepaintGraphWindow();
447 sprintf(&gapstring[0], "%i", gap);
448 CmdLFSim(gapstring);
449 return 0;
452 int CmdLFSnoop(const char *Cmd)
454 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
456 // 'h' means higher-low-frequency, 134 kHz
457 c.arg[0] = 0;
458 c.arg[1] = -1;
460 if (*Cmd == 'l') {
461 sscanf(Cmd, "l %"lli, &c.arg[1]);
462 } else if(*Cmd == 'h') {
463 c.arg[0] = 1;
464 sscanf(Cmd, "h %"lli, &c.arg[1]);
465 } else if (sscanf(Cmd, "%"lli" %"lli, &c.arg[0], &c.arg[1]) < 1) {
466 PrintAndLog("usage 1: snoop");
467 PrintAndLog(" 2: snoop {l,h} [trigger threshold]");
468 PrintAndLog(" 3: snoop <divisor> [trigger threshold]");
469 return 0;
472 SendCommand(&c);
473 WaitForResponse(CMD_ACK,NULL);
474 return 0;
477 int CmdVchDemod(const char *Cmd)
479 // Is this the entire sync pattern, or does this also include some
480 // data bits that happen to be the same everywhere? That would be
481 // lovely to know.
482 static const int SyncPattern[] = {
483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
484 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
485 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
486 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
487 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
488 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
490 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
492 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
495 // So first, we correlate for the sync pattern, and mark that.
496 int bestCorrel = 0, bestPos = 0;
497 int i;
498 // It does us no good to find the sync pattern, with fewer than
499 // 2048 samples after it...
500 for (i = 0; i < (GraphTraceLen-2048); i++) {
501 int sum = 0;
502 int j;
503 for (j = 0; j < arraylen(SyncPattern); j++) {
504 sum += GraphBuffer[i+j]*SyncPattern[j];
506 if (sum > bestCorrel) {
507 bestCorrel = sum;
508 bestPos = i;
511 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
513 char bits[257];
514 bits[256] = '\0';
516 int worst = INT_MAX;
517 int worstPos = 0;
519 for (i = 0; i < 2048; i += 8) {
520 int sum = 0;
521 int j;
522 for (j = 0; j < 8; j++) {
523 sum += GraphBuffer[bestPos+i+j];
525 if (sum < 0) {
526 bits[i/8] = '.';
527 } else {
528 bits[i/8] = '1';
530 if(abs(sum) < worst) {
531 worst = abs(sum);
532 worstPos = i;
535 PrintAndLog("bits:");
536 PrintAndLog("%s", bits);
537 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
539 if (strcmp(Cmd, "clone")==0) {
540 GraphTraceLen = 0;
541 char *s;
542 for(s = bits; *s; s++) {
543 int j;
544 for(j = 0; j < 16; j++) {
545 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
548 RepaintGraphWindow();
550 return 0;
553 //by marshmellow
554 int CmdLFfind(const char *Cmd)
556 int ans=0;
557 if (!offline){
558 ans=CmdLFRead("");
559 ans=CmdSamples("20000");
561 if (GraphTraceLen<1000) return 0;
562 PrintAndLog("Checking for known tags:");
563 ans=Cmdaskmandemod("");
564 if (ans>0) return 1;
565 ans=CmdFSKdemodHID("");
566 if (ans>0) return 1;
567 ans=CmdFSKdemodIO("");
568 if (ans>0) return 1;
569 //add psk and indala
570 ans=CmdIndalaDemod("");
571 if (ans>0) return 1;
572 ans=CmdIndalaDemod("224");
573 if (ans>0) return 1;
574 PrintAndLog("No Known Tags Found!\n");
575 return 0;
578 static command_t CommandTable[] =
580 {"help", CmdHelp, 1, "This help"},
581 {"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)"},
582 {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
583 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
584 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
585 {"io", CmdLFIO, 1, "{ ioProx tags... }"},
586 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
587 {"indalaclone", CmdIndalaClone, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
588 {"read", CmdLFRead, 0, "['h' or <divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
589 {"search", CmdLFfind, 1, "Read and Search for valid known tag (in offline mode it you can load first then search)"},
590 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
591 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
592 {"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
593 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
594 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
595 {"hitag", CmdLFHitag, 1, "{ Hitag tags and transponders... }"},
596 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
597 {"t55xx", CmdLFT55XX, 1, "{ T55xx RFIDs... }"},
598 {"pcf7931", CmdLFPCF7931, 1, "{PCF7931 RFIDs...}"},
599 {NULL, NULL, 0, NULL}
602 int CmdLF(const char *Cmd)
604 CmdsParse(CommandTable, Cmd);
605 return 0;
608 int CmdHelp(const char *Cmd)
610 CmdsHelp(CommandTable);
611 return 0;