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