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