]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlf.c
Fix FMCN setting so it actually matches datasheet
[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>
7fe9b0b7 15#include "proxusb.h"
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"
22#include "cmdlf.h"
23#include "cmdlfhid.h"
24#include "cmdlfti.h"
25#include "cmdlfem4x.h"
26
27static int CmdHelp(const char *Cmd);
28
29/* send a command before reading */
30int CmdLFCommandRead(const char *Cmd)
31{
32 static char dummy[3];
33
34 dummy[0]= ' ';
35
36 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
37 sscanf(Cmd, "%i %i %i %s %s", &c.arg[0], &c.arg[1], &c.arg[2], (char *) &c.d.asBytes,(char *) &dummy+1);
38 // in case they specified 'h'
39 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
40 SendCommand(&c);
41 return 0;
42}
43
44int CmdFlexdemod(const char *Cmd)
45{
46 int i;
47 for (i = 0; i < GraphTraceLen; ++i) {
48 if (GraphBuffer[i] < 0) {
49 GraphBuffer[i] = -1;
50 } else {
51 GraphBuffer[i] = 1;
52 }
53 }
54
55#define LONG_WAIT 100
56 int start;
57 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
58 int first = GraphBuffer[start];
59 for (i = start; i < start + LONG_WAIT; i++) {
60 if (GraphBuffer[i] != first) {
61 break;
62 }
63 }
64 if (i == (start + LONG_WAIT)) {
65 break;
66 }
67 }
68 if (start == GraphTraceLen - LONG_WAIT) {
69 PrintAndLog("nothing to wait for");
70 return 0;
71 }
72
73 GraphBuffer[start] = 2;
74 GraphBuffer[start+1] = -2;
75
76 uint8_t bits[64];
77
78 int bit;
79 i = start;
80 for (bit = 0; bit < 64; bit++) {
81 int j;
82 int sum = 0;
83 for (j = 0; j < 16; j++) {
84 sum += GraphBuffer[i++];
85 }
86 if (sum > 0) {
87 bits[bit] = 1;
88 } else {
89 bits[bit] = 0;
90 }
91 PrintAndLog("bit %d sum %d", bit, sum);
92 }
93
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++];
99 }
100 if (sum > 0 && bits[bit] != 1) {
101 PrintAndLog("oops1 at %d", bit);
102 }
103 if (sum < 0 && bits[bit] != 0) {
104 PrintAndLog("oops2 at %d", bit);
105 }
106 }
107
108 GraphTraceLen = 32*64;
109 i = 0;
110 int phase = 0;
111 for (bit = 0; bit < 64; bit++) {
112 if (bits[bit] == 0) {
113 phase = 0;
114 } else {
115 phase = 1;
116 }
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}
127
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;
135 // worst case with GraphTraceLen=64000 is < 4096
136 // under normal conditions it's < 2048
137 uint8_t rawbits[4096];
138 int rawbit = 0;
139 int worst = 0, worstPos = 0;
140 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
141 for (i = 0; i < GraphTraceLen-1; i += 2) {
142 count += 1;
143 if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
144 if (state == 0) {
145 for (j = 0; j < count - 8; j += 16) {
146 rawbits[rawbit++] = 0;
147 }
148 if ((abs(count - j)) > worst) {
149 worst = abs(count - j);
150 worstPos = i;
151 }
152 }
153 state = 1;
154 count = 0;
155 } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
156 if (state == 1) {
157 for (j = 0; j < count - 8; j += 16) {
158 rawbits[rawbit++] = 1;
159 }
160 if ((abs(count - j)) > worst) {
161 worst = abs(count - j);
162 worstPos = i;
163 }
164 }
165 state = 0;
166 count = 0;
167 }
168 }
169 PrintAndLog("Recovered %d raw bits", rawbit);
170 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
171
172 // Finding the start of a UID
173 int uidlen, long_wait;
174 if (strcmp(Cmd, "224") == 0) {
175 uidlen = 224;
176 long_wait = 30;
177 } else {
178 uidlen = 64;
179 long_wait = 29;
180 }
181 int start;
182 int first = 0;
183 for (start = 0; start <= rawbit - uidlen; start++) {
184 first = rawbits[start];
185 for (i = start; i < start + long_wait; i++) {
186 if (rawbits[i] != first) {
187 break;
188 }
189 }
190 if (i == (start + long_wait)) {
191 break;
192 }
193 }
194 if (start == rawbit - uidlen + 1) {
195 PrintAndLog("nothing to wait for");
196 return 0;
197 }
198
199 // Inverting signal if needed
200 if (first == 1) {
201 for (i = start; i < rawbit; i++) {
202 rawbits[i] = !rawbits[i];
203 }
204 }
205
206 // Dumping UID
207 uint8_t bits[224];
208 char showbits[225];
209 showbits[uidlen]='\0';
210 int bit;
211 i = start;
212 int times = 0;
213 if (uidlen > rawbit) {
214 PrintAndLog("Warning: not enough raw bits to get a full UID");
215 for (bit = 0; bit < rawbit; bit++) {
216 bits[bit] = rawbits[i++];
217 // As we cannot know the parity, let's use "." and "/"
218 showbits[bit] = '.' + bits[bit];
219 }
220 showbits[bit+1]='\0';
221 PrintAndLog("Partial UID=%s", showbits);
222 return 0;
223 } else {
224 for (bit = 0; bit < uidlen; bit++) {
225 bits[bit] = rawbits[i++];
226 showbits[bit] = '0' + bits[bit];
227 }
228 times = 1;
229 }
230 PrintAndLog("UID=%s", showbits);
231
232 // Checking UID against next occurences
233 for (; i + uidlen <= rawbit;) {
234 int failed = 0;
235 for (bit = 0; bit < uidlen; bit++) {
236 if (bits[bit] != rawbits[i++]) {
237 failed = 1;
238 break;
239 }
240 }
241 if (failed == 1) {
242 break;
243 }
244 times += 1;
245 }
246 PrintAndLog("Occurences: %d (expected %d)", times, (rawbit - start) / uidlen);
247
248 // Remodulating for tag cloning
249 GraphTraceLen = 32*uidlen;
250 i = 0;
251 int phase = 0;
252 for (bit = 0; bit < uidlen; bit++) {
253 if (bits[bit] == 0) {
254 phase = 0;
255 } else {
256 phase = 1;
257 }
258 int j;
259 for (j = 0; j < 32; j++) {
260 GraphBuffer[i++] = phase;
261 phase = !phase;
262 }
263 }
264
265 RepaintGraphWindow();
266 return 0;
267}
268
269int CmdLFRead(const char *Cmd)
270{
271 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};
272 // 'h' means higher-low-frequency, 134 kHz
273 if(*Cmd == 'h') {
274 c.arg[0] = 1;
275 } else if (*Cmd == '\0') {
276 c.arg[0] = 0;
277 } else {
278 PrintAndLog("use 'read' or 'read h'");
279 return 0;
280 }
281 SendCommand(&c);
bdd1de1b 282 WaitForResponse(CMD_ACK);
7fe9b0b7 283 return 0;
284}
285
286static void ChkBitstream(const char *str)
287{
288 int i;
289
290 /* convert to bitstream if necessary */
291 for (i = 0; i < (int)(GraphTraceLen / 2); i++)
292 {
293 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0)
294 {
295 CmdBitstream(str);
296 break;
297 }
298 }
299}
300
301int CmdLFSim(const char *Cmd)
302{
303 int i;
304 static int gap;
305
306 sscanf(Cmd, "%i", &gap);
307
308 /* convert to bitstream if necessary */
309 ChkBitstream(Cmd);
310
311 PrintAndLog("Sending data, please wait...");
312 for (i = 0; i < GraphTraceLen; i += 48) {
313 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
314 int j;
315 for (j = 0; j < 48; j++) {
316 c.d.asBytes[j] = GraphBuffer[i+j];
317 }
318 SendCommand(&c);
319 WaitForResponse(CMD_ACK);
320 }
321
322 PrintAndLog("Starting simulator...");
323 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
324 SendCommand(&c);
325 return 0;
326}
327
328int CmdLFSimBidir(const char *Cmd)
329{
330 /* Set ADC to twice the carrier for a slight supersampling */
331 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
332 SendCommand(&c);
333 return 0;
334}
335
336/* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
337int CmdLFSimManchester(const char *Cmd)
338{
339 static int clock, gap;
340 static char data[1024], gapstring[8];
341
342 /* get settings/bits */
343 sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap);
344
345 /* clear our graph */
346 ClearGraph(0);
347
348 /* fill it with our bitstream */
349 for (int i = 0; i < strlen(data) ; ++i)
350 AppendGraph(0, clock, data[i]- '0');
351
352 /* modulate */
353 CmdManchesterMod("");
354
355 /* show what we've done */
356 RepaintGraphWindow();
357
358 /* simulate */
359 sprintf(&gapstring[0], "%i", gap);
360 CmdLFSim(gapstring);
361 return 0;
362}
363
364int CmdVchDemod(const char *Cmd)
365{
366 // Is this the entire sync pattern, or does this also include some
367 // data bits that happen to be the same everywhere? That would be
368 // lovely to know.
369 static const int SyncPattern[] = {
370 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
371 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
372 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
373 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
375 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
376 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
377 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
378 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
379 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
380 };
381
382 // So first, we correlate for the sync pattern, and mark that.
383 int bestCorrel = 0, bestPos = 0;
384 int i;
385 // It does us no good to find the sync pattern, with fewer than
386 // 2048 samples after it...
387 for (i = 0; i < (GraphTraceLen-2048); i++) {
388 int sum = 0;
389 int j;
390 for (j = 0; j < arraylen(SyncPattern); j++) {
391 sum += GraphBuffer[i+j]*SyncPattern[j];
392 }
393 if (sum > bestCorrel) {
394 bestCorrel = sum;
395 bestPos = i;
396 }
397 }
398 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
399
400 char bits[257];
401 bits[256] = '\0';
402
403 int worst = INT_MAX;
fddf220a 404 int worstPos = 0;
7fe9b0b7 405
406 for (i = 0; i < 2048; i += 8) {
407 int sum = 0;
408 int j;
409 for (j = 0; j < 8; j++) {
410 sum += GraphBuffer[bestPos+i+j];
411 }
412 if (sum < 0) {
413 bits[i/8] = '.';
414 } else {
415 bits[i/8] = '1';
416 }
417 if(abs(sum) < worst) {
418 worst = abs(sum);
419 worstPos = i;
420 }
421 }
422 PrintAndLog("bits:");
423 PrintAndLog("%s", bits);
424 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
425
426 if (strcmp(Cmd, "clone")==0) {
427 GraphTraceLen = 0;
428 char *s;
429 for(s = bits; *s; s++) {
430 int j;
431 for(j = 0; j < 16; j++) {
432 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
433 }
434 }
435 RepaintGraphWindow();
436 }
437 return 0;
438}
439
440static command_t CommandTable[] =
441{
442 {"help", CmdHelp, 1, "This help"},
443 {"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 444 {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
7fe9b0b7 445 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
37239a7c 446 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
7fe9b0b7 447 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
448 {"read", CmdLFRead, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
449 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
450 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
451 {"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
37239a7c 452 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
7fe9b0b7 453 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
454 {NULL, NULL, 0, NULL}
455};
456
457int CmdLF(const char *Cmd)
458{
459 CmdsParse(CommandTable, Cmd);
460 return 0;
461}
462
463int CmdHelp(const char *Cmd)
464{
465 CmdsHelp(CommandTable);
466 return 0;
467}
Impressum, Datenschutz