]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmddata.c
Merge pull request #53 from iceman1001/feature
[proxmark3-svn] / client / cmddata.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// Data and Graph commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <limits.h>
902cb3c0 15#include "proxmark3.h"
7fe9b0b7 16#include "data.h"
17#include "ui.h"
18#include "graph.h"
19#include "cmdparser.h"
d51b2eda 20#include "util.h"
7fe9b0b7 21#include "cmdmain.h"
22#include "cmddata.h"
7db5f1ca 23#include "lfdemod.h"
4118b74d 24uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
ec75f5c1 25uint8_t g_debugMode;
4118b74d 26int DemodBufferLen;
7fe9b0b7 27static int CmdHelp(const char *Cmd);
28
4118b74d 29//set the demod buffer with given array of binary (one bit per byte)
30//by marshmellow
ec75f5c1 31void setDemodBuf(uint8_t *buff, size_t size, size_t startIdx)
4118b74d 32{
ec75f5c1 33 size_t i = 0;
34 for (; i < size; i++){
35 DemodBuffer[i]=buff[startIdx++];
ba1a299c 36 }
37 DemodBufferLen=size;
38 return;
4118b74d 39}
40
ec75f5c1 41int CmdSetDebugMode(const char *Cmd)
42{
43 int demod=0;
44 sscanf(Cmd, "%i", &demod);
45 g_debugMode=(uint8_t)demod;
46 return 1;
47}
48
4118b74d 49//by marshmellow
50void printDemodBuff()
51{
ba1a299c 52 uint32_t i = 0;
53 int bitLen = DemodBufferLen;
54 if (bitLen<16) {
55 PrintAndLog("no bits found in demod buffer");
56 return;
57 }
58 if (bitLen>512) bitLen=512; //max output to 512 bits if we have more - should be plenty
33c7e2f3 59
0c5998c0
I
60 // ensure equally divided by 16
61 bitLen &= 0xfff0;
33c7e2f3 62
ba1a299c 63 for (i = 0; i <= (bitLen-16); i+=16) {
64 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
65 DemodBuffer[i],
66 DemodBuffer[i+1],
67 DemodBuffer[i+2],
68 DemodBuffer[i+3],
69 DemodBuffer[i+4],
70 DemodBuffer[i+5],
71 DemodBuffer[i+6],
72 DemodBuffer[i+7],
73 DemodBuffer[i+8],
74 DemodBuffer[i+9],
75 DemodBuffer[i+10],
76 DemodBuffer[i+11],
77 DemodBuffer[i+12],
78 DemodBuffer[i+13],
79 DemodBuffer[i+14],
80 DemodBuffer[i+15]);
81 }
82 return;
4118b74d 83}
84
85
7fe9b0b7 86int CmdAmp(const char *Cmd)
87{
88 int i, rising, falling;
89 int max = INT_MIN, min = INT_MAX;
90
91 for (i = 10; i < GraphTraceLen; ++i) {
92 if (GraphBuffer[i] > max)
93 max = GraphBuffer[i];
94 if (GraphBuffer[i] < min)
95 min = GraphBuffer[i];
96 }
97
98 if (max != min) {
99 rising = falling= 0;
100 for (i = 0; i < GraphTraceLen; ++i) {
101 if (GraphBuffer[i + 1] < GraphBuffer[i]) {
102 if (rising) {
103 GraphBuffer[i] = max;
104 rising = 0;
105 }
106 falling = 1;
107 }
108 if (GraphBuffer[i + 1] > GraphBuffer[i]) {
109 if (falling) {
110 GraphBuffer[i] = min;
111 falling = 0;
112 }
113 rising= 1;
114 }
115 }
116 }
117 RepaintGraphWindow();
118 return 0;
119}
120
121/*
122 * Generic command to demodulate ASK.
123 *
124 * Argument is convention: positive or negative (High mod means zero
125 * or high mod means one)
126 *
127 * Updates the Graph trace with 0/1 values
128 *
129 * Arguments:
130 * c : 0 or 1
131 */
2fc2150e 132 //this method is dependant on all highs and lows to be the same(or clipped) this creates issues[marshmellow] it also ignores the clock
7fe9b0b7 133int Cmdaskdemod(const char *Cmd)
134{
135 int i;
136 int c, high = 0, low = 0;
ba1a299c 137
7fe9b0b7 138 // TODO: complain if we do not give 2 arguments here !
139 // (AL - this doesn't make sense! we're only using one argument!!!)
140 sscanf(Cmd, "%i", &c);
ba1a299c 141
7fe9b0b7 142 /* Detect high and lows and clock */
ba1a299c 143 // (AL - clock???)
7fe9b0b7 144 for (i = 0; i < GraphTraceLen; ++i)
145 {
146 if (GraphBuffer[i] > high)
147 high = GraphBuffer[i];
148 else if (GraphBuffer[i] < low)
149 low = GraphBuffer[i];
150 }
eb191de6 151 high=abs(high*.75);
152 low=abs(low*.75);
7fe9b0b7 153 if (c != 0 && c != 1) {
154 PrintAndLog("Invalid argument: %s", Cmd);
155 return 0;
156 }
b3b70669 157 //prime loop
7fe9b0b7 158 if (GraphBuffer[0] > 0) {
159 GraphBuffer[0] = 1-c;
160 } else {
161 GraphBuffer[0] = c;
162 }
163 for (i = 1; i < GraphTraceLen; ++i) {
164 /* Transitions are detected at each peak
165 * Transitions are either:
166 * - we're low: transition if we hit a high
167 * - we're high: transition if we hit a low
168 * (we need to do it this way because some tags keep high or
169 * low for long periods, others just reach the peak and go
170 * down)
171 */
ae2f73c1 172 //[marhsmellow] change == to >= for high and <= for low for fuzz
173 if ((GraphBuffer[i] == high) && (GraphBuffer[i - 1] == c)) {
7fe9b0b7 174 GraphBuffer[i] = 1 - c;
ae2f73c1 175 } else if ((GraphBuffer[i] == low) && (GraphBuffer[i - 1] == (1 - c))){
7fe9b0b7 176 GraphBuffer[i] = c;
177 } else {
178 /* No transition */
179 GraphBuffer[i] = GraphBuffer[i - 1];
180 }
181 }
182 RepaintGraphWindow();
183 return 0;
184}
185
d5a72d2f 186//by marshmellow
187void printBitStream(uint8_t BitStream[], uint32_t bitLen)
188{
2fc2150e 189 uint32_t i = 0;
190 if (bitLen<16) {
191 PrintAndLog("Too few bits found: %d",bitLen);
192 return;
193 }
194 if (bitLen>512) bitLen=512;
0c5998c0
I
195
196 // ensure equally divided by 16
197 bitLen &= 0xfff0;
198
199
2df8c079 200 for (i = 0; i <= (bitLen-16); i+=16) {
2fc2150e 201 PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
202 BitStream[i],
203 BitStream[i+1],
204 BitStream[i+2],
205 BitStream[i+3],
206 BitStream[i+4],
207 BitStream[i+5],
208 BitStream[i+6],
209 BitStream[i+7],
210 BitStream[i+8],
211 BitStream[i+9],
212 BitStream[i+10],
213 BitStream[i+11],
214 BitStream[i+12],
215 BitStream[i+13],
216 BitStream[i+14],
217 BitStream[i+15]);
218 }
ba1a299c 219 return;
2fc2150e 220}
d5a72d2f 221//by marshmellow
ba1a299c 222//print EM410x ID in multiple formats
eb191de6 223void printEM410x(uint64_t id)
2fc2150e 224{
eb191de6 225 if (id !=0){
0e74c023 226 uint64_t iii=1;
ec75f5c1 227 uint64_t id2lo=0;
eb191de6 228 uint32_t ii=0;
229 uint32_t i=0;
0e74c023 230 for (ii=5; ii>0;ii--){
2fc2150e 231 for (i=0;i<8;i++){
ba1a299c 232 id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8)));
2fc2150e 233 }
234 }
0e74c023 235 //output em id
eb191de6 236 PrintAndLog("EM TAG ID : %010llx", id);
ec75f5c1 237 PrintAndLog("Unique TAG ID: %010llx", id2lo);
eb191de6 238 PrintAndLog("DEZ 8 : %08lld",id & 0xFFFFFF);
239 PrintAndLog("DEZ 10 : %010lld",id & 0xFFFFFF);
240 PrintAndLog("DEZ 5.5 : %05lld.%05lld",(id>>16LL) & 0xFFFF,(id & 0xFFFF));
241 PrintAndLog("DEZ 3.5A : %03lld.%05lld",(id>>32ll),(id & 0xFFFF));
242 PrintAndLog("DEZ 14/IK2 : %014lld",id);
0e74c023 243 PrintAndLog("DEZ 15/IK3 : %015lld",id2lo);
eb191de6 244 PrintAndLog("Other : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF));
ba1a299c 245 }
eb191de6 246 return;
247}
248
d5a72d2f 249//by marshmellow
ba1a299c 250//take binary from demod buffer and see if we can find an EM410x ID
eb191de6 251int CmdEm410xDecode(const char *Cmd)
252{
253 uint64_t id=0;
ec75f5c1 254 size_t size = DemodBufferLen, idx=0;
255 id = Em410xDecode(DemodBuffer, &size, &idx);
256 if (id>0){
257 setDemodBuf(DemodBuffer, size, idx);
258 if (g_debugMode){
259 PrintAndLog("DEBUG: Printing demod buffer:");
260 printDemodBuff();
261 }
262 printEM410x(id);
263 return 1;
264 }
2fc2150e 265 return 0;
266}
267
f822a063 268
e888ed8e 269//by marshmellow
eb191de6 270//takes 2 arguments - clock and invert both as integers
ba1a299c 271//attempts to demodulate ask while decoding manchester
e888ed8e 272//prints binary found and saves in graphbuffer for further commands
9e6dd4eb 273int Cmdaskmandemod(const char *Cmd)
e888ed8e 274{
ec75f5c1 275 int invert=0;
ba1a299c 276 int clk=0;
eb191de6 277 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 278 sscanf(Cmd, "%i %i", &clk, &invert);
e888ed8e 279 if (invert != 0 && invert != 1) {
280 PrintAndLog("Invalid argument: %s", Cmd);
281 return 0;
282 }
ba1a299c 283
284 size_t BitLen = getFromGraphBuf(BitStream);
ec75f5c1 285 if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
eb191de6 286 int errCnt=0;
287 errCnt = askmandemod(BitStream, &BitLen,&clk,&invert);
ba1a299c 288 if (errCnt<0||BitLen<16){ //if fatal error (or -1)
ec75f5c1 289 if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
e888ed8e 290 return 0;
ba1a299c 291 }
f822a063 292 PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
ba1a299c 293
d5a72d2f 294 //output
eb191de6 295 if (errCnt>0){
296 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
e888ed8e 297 }
eb191de6 298 PrintAndLog("ASK/Manchester decoded bitstream:");
299 // Now output the bitstream to the scrollback by line of 16 bits
ec75f5c1 300 setDemodBuf(BitStream,BitLen,0);
ba1a299c 301 printDemodBuff();
eb191de6 302 uint64_t lo =0;
ec75f5c1 303 size_t idx=0;
304 lo = Em410xDecode(BitStream, &BitLen, &idx);
d5a72d2f 305 if (lo>0){
306 //set GraphBuffer for clone or sim command
ec75f5c1 307 setDemodBuf(BitStream, BitLen, idx);
308 if (g_debugMode){
309 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
310 printDemodBuff();
311 }
d5a72d2f 312 PrintAndLog("EM410x pattern found: ");
313 printEM410x(lo);
ac914e56 314 return 1;
d5a72d2f 315 }
eb191de6 316 return 0;
317}
318
319//by marshmellow
d5a72d2f 320//manchester decode
eb191de6 321//stricktly take 10 and 01 and convert to 0 and 1
322int Cmdmandecoderaw(const char *Cmd)
323{
324 int i =0;
325 int errCnt=0;
ba1a299c 326 size_t size=0;
eb191de6 327 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
328 int high=0,low=0;
ba1a299c 329 for (;i<DemodBufferLen;++i){
330 if (DemodBuffer[i]>high) high=DemodBuffer[i];
331 else if(DemodBuffer[i]<low) low=DemodBuffer[i];
332 BitStream[i]=DemodBuffer[i];
eb191de6 333 }
334 if (high>1 || low <0 ){
335 PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
336 return 0;
337 }
ba1a299c 338 size=i;
339 errCnt=manrawdecode(BitStream, &size);
2df8c079 340 if (errCnt>=20){
341 PrintAndLog("Too many errors: %d",errCnt);
342 return 0;
343 }
eb191de6 344 PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
ba1a299c 345 printBitStream(BitStream, size);
eb191de6 346 if (errCnt==0){
ba1a299c 347 uint64_t id = 0;
ec75f5c1 348 size_t idx=0;
349 id = Em410xDecode(BitStream, &size, &idx);
350 if (id>0){
351 //need to adjust to set bitstream back to manchester encoded data
352 //setDemodBuf(BitStream, size, idx);
353
354 printEM410x(id);
355 }
eb191de6 356 }
d5a72d2f 357 return 1;
358}
359
360//by marshmellow
361//biphase decode
362//take 01 or 10 = 0 and 11 or 00 = 1
1e090a61 363//takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
364// and "invert" default = 0 if 1 it will invert output
ba1a299c 365// since it is not like manchester and doesn't have an incorrect bit pattern we
d5a72d2f 366// cannot determine if our decode is correct or if it should be shifted by one bit
367// the argument offset allows us to manually shift if the output is incorrect
368// (better would be to demod and decode at the same time so we can distinguish large
369// width waves vs small width waves to help the decode positioning) or askbiphdemod
370int CmdBiphaseDecodeRaw(const char *Cmd)
371{
372 int i = 0;
373 int errCnt=0;
ba1a299c 374 size_t size=0;
d5a72d2f 375 int offset=0;
1e090a61 376 int invert=0;
d5a72d2f 377 int high=0, low=0;
1e090a61 378 sscanf(Cmd, "%i %i", &offset, &invert);
d5a72d2f 379 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
380 //get graphbuffer & high and low
ba1a299c 381 for (;i<DemodBufferLen;++i){
382 if(DemodBuffer[i]>high)high=DemodBuffer[i];
383 else if(DemodBuffer[i]<low)low=DemodBuffer[i];
384 BitStream[i]=DemodBuffer[i];
d5a72d2f 385 }
386 if (high>1 || low <0){
387 PrintAndLog("Error: please raw demod the wave first then decode");
388 return 0;
389 }
ba1a299c 390 size=i;
1e090a61 391 errCnt=BiphaseRawDecode(BitStream, &size, offset, invert);
d5a72d2f 392 if (errCnt>=20){
393 PrintAndLog("Too many errors attempting to decode: %d",errCnt);
394 return 0;
395 }
396 PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt);
ba1a299c 397 printBitStream(BitStream, size);
d5a72d2f 398 PrintAndLog("\nif bitstream does not look right try offset=1");
399 return 1;
eb191de6 400}
401
402//by marshmellow
403//takes 2 arguments - clock and invert both as integers
404//attempts to demodulate ask only
405//prints binary found and saves in graphbuffer for further commands
406int Cmdaskrawdemod(const char *Cmd)
407{
ba1a299c 408 int invert=0;
409 int clk=0;
eb191de6 410 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 411 sscanf(Cmd, "%i %i", &clk, &invert);
eb191de6 412 if (invert != 0 && invert != 1) {
413 PrintAndLog("Invalid argument: %s", Cmd);
414 return 0;
415 }
ba1a299c 416 size_t BitLen = getFromGraphBuf(BitStream);
eb191de6 417 int errCnt=0;
ba1a299c 418 errCnt = askrawdemod(BitStream, &BitLen,&clk,&invert);
419 if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
420 PrintAndLog("no data found");
ec75f5c1 421 if (g_debugMode==1) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert);
eb191de6 422 return 0;
ba1a299c 423 }
f822a063 424 PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
ec75f5c1 425
426 //move BitStream back to DemodBuffer
427 setDemodBuf(BitStream,BitLen,0);
ba1a299c 428
ec75f5c1 429 //output
eb191de6 430 if (errCnt>0){
431 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
432 }
433 PrintAndLog("ASK demoded bitstream:");
434 // Now output the bitstream to the scrollback by line of 16 bits
435 printBitStream(BitStream,BitLen);
952a8bb5 436
f822a063 437 return 1;
e888ed8e 438}
439
7fe9b0b7 440int CmdAutoCorr(const char *Cmd)
441{
442 static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];
443
444 int window = atoi(Cmd);
445
446 if (window == 0) {
447 PrintAndLog("needs a window");
448 return 0;
449 }
450 if (window >= GraphTraceLen) {
451 PrintAndLog("window must be smaller than trace (%d samples)",
452 GraphTraceLen);
453 return 0;
454 }
455
456 PrintAndLog("performing %d correlations", GraphTraceLen - window);
457
458 for (int i = 0; i < GraphTraceLen - window; ++i) {
459 int sum = 0;
460 for (int j = 0; j < window; ++j) {
461 sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256;
462 }
463 CorrelBuffer[i] = sum;
464 }
465 GraphTraceLen = GraphTraceLen - window;
466 memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int));
467
468 RepaintGraphWindow();
469 return 0;
470}
471
472int CmdBitsamples(const char *Cmd)
473{
474 int cnt = 0;
90d74dc2 475 uint8_t got[12288];
952a8bb5 476
90d74dc2 477 GetFromBigBuf(got,sizeof(got),0);
478 WaitForResponse(CMD_ACK,NULL);
7fe9b0b7 479
90d74dc2 480 for (int j = 0; j < sizeof(got); j++) {
7fe9b0b7 481 for (int k = 0; k < 8; k++) {
90d74dc2 482 if(got[j] & (1 << (7 - k))) {
7fe9b0b7 483 GraphBuffer[cnt++] = 1;
484 } else {
485 GraphBuffer[cnt++] = 0;
486 }
487 }
7fe9b0b7 488 }
489 GraphTraceLen = cnt;
490 RepaintGraphWindow();
491 return 0;
492}
493
494/*
495 * Convert to a bitstream
496 */
497int CmdBitstream(const char *Cmd)
498{
499 int i, j;
500 int bit;
501 int gtl;
502 int clock;
503 int low = 0;
504 int high = 0;
505 int hithigh, hitlow, first;
506
507 /* Detect high and lows and clock */
508 for (i = 0; i < GraphTraceLen; ++i)
509 {
510 if (GraphBuffer[i] > high)
511 high = GraphBuffer[i];
512 else if (GraphBuffer[i] < low)
513 low = GraphBuffer[i];
514 }
515
516 /* Get our clock */
517 clock = GetClock(Cmd, high, 1);
518 gtl = ClearGraph(0);
519
520 bit = 0;
521 for (i = 0; i < (int)(gtl / clock); ++i)
522 {
523 hithigh = 0;
524 hitlow = 0;
525 first = 1;
526 /* Find out if we hit both high and low peaks */
527 for (j = 0; j < clock; ++j)
528 {
529 if (GraphBuffer[(i * clock) + j] == high)
530 hithigh = 1;
531 else if (GraphBuffer[(i * clock) + j] == low)
532 hitlow = 1;
533 /* it doesn't count if it's the first part of our read
534 because it's really just trailing from the last sequence */
535 if (first && (hithigh || hitlow))
536 hithigh = hitlow = 0;
537 else
538 first = 0;
539
540 if (hithigh && hitlow)
541 break;
542 }
543
544 /* If we didn't hit both high and low peaks, we had a bit transition */
545 if (!hithigh || !hitlow)
546 bit ^= 1;
547
548 AppendGraph(0, clock, bit);
7fe9b0b7 549 }
ba1a299c 550
7fe9b0b7 551 RepaintGraphWindow();
552 return 0;
553}
554
555int CmdBuffClear(const char *Cmd)
556{
557 UsbCommand c = {CMD_BUFF_CLEAR};
558 SendCommand(&c);
559 ClearGraph(true);
560 return 0;
561}
562
563int CmdDec(const char *Cmd)
564{
565 for (int i = 0; i < (GraphTraceLen / 2); ++i)
566 GraphBuffer[i] = GraphBuffer[i * 2];
567 GraphTraceLen /= 2;
568 PrintAndLog("decimated by 2");
569 RepaintGraphWindow();
570 return 0;
571}
572
ec75f5c1 573//by marshmellow
574//shift graph zero up or down based on input + or -
575int CmdGraphShiftZero(const char *Cmd)
576{
577
578 int shift=0;
579 //set options from parameters entered with the command
580 sscanf(Cmd, "%i", &shift);
581 int shiftedVal=0;
582 for(int i = 0; i<GraphTraceLen; i++){
583 shiftedVal=GraphBuffer[i]+shift;
584 if (shiftedVal>127)
585 shiftedVal=127;
586 else if (shiftedVal<-127)
587 shiftedVal=-127;
588 GraphBuffer[i]= shiftedVal;
589 }
590 CmdNorm("");
591 return 0;
592}
593
7fe9b0b7 594/* Print our clock rate */
ba1a299c 595// uses data from graphbuffer
7fe9b0b7 596int CmdDetectClockRate(const char *Cmd)
597{
d5a72d2f 598 GetClock("",0,0);
ba1a299c 599 //int clock = DetectASKClock(0);
600 //PrintAndLog("Auto-detected clock rate: %d", clock);
7fe9b0b7 601 return 0;
602}
66707a3b 603
2fc2150e 604//by marshmellow
eb191de6 605//fsk raw demod and print binary
f822a063 606//takes 4 arguments - Clock, invert, rchigh, rclow
607//defaults: clock = 50, invert=0, rchigh=10, rclow=8 (RF/10 RF/8 (fsk2a))
e888ed8e 608int CmdFSKrawdemod(const char *Cmd)
b3b70669 609{
eb191de6 610 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
b3b70669 611 //set defaults
03e6bb4a 612 int rfLen = 0;
f822a063 613 int invert=0;
03e6bb4a 614 int fchigh=0;
615 int fclow=0;
b3b70669 616 //set options from parameters entered with the command
ba1a299c 617 sscanf(Cmd, "%i %i %i %i", &rfLen, &invert, &fchigh, &fclow);
618
b3b70669 619 if (strlen(Cmd)>0 && strlen(Cmd)<=2) {
b3b70669 620 if (rfLen==1){
621 invert=1; //if invert option only is used
03e6bb4a 622 rfLen = 0;
623 }
ba1a299c 624 }
03e6bb4a 625
eb191de6 626 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 627 size_t BitLen = getFromGraphBuf(BitStream);
03e6bb4a 628 //get field clock lengths
629 uint16_t fcs=0;
630 if (fchigh==0 || fclow == 0){
631 fcs=countFC(BitStream, BitLen);
632 if (fcs==0){
633 fchigh=10;
634 fclow=8;
635 }else{
636 fchigh = (fcs >> 8) & 0xFF;
637 fclow = fcs & 0xFF;
638 }
639 }
640 //get bit clock length
641 if (rfLen==0){
642 rfLen = detectFSKClk(BitStream, BitLen, fchigh, fclow);
643 if (rfLen == 0) rfLen = 50;
644 }
645 PrintAndLog("Args invert: %d - Clock:%d - fchigh:%d - fclow: %d",invert,rfLen,fchigh, fclow);
ba1a299c 646 int size = fskdemod(BitStream,BitLen,(uint8_t)rfLen,(uint8_t)invert,(uint8_t)fchigh,(uint8_t)fclow);
d5a72d2f 647 if (size>0){
648 PrintAndLog("FSK decoded bitstream:");
ec75f5c1 649 setDemodBuf(BitStream,size,0);
ba1a299c 650
d5a72d2f 651 // Now output the bitstream to the scrollback by line of 16 bits
652 if(size > (8*32)+2) size = (8*32)+2; //only output a max of 8 blocks of 32 bits most tags will have full bit stream inside that sample size
653 printBitStream(BitStream,size);
654 } else{
655 PrintAndLog("no FSK data found");
eb191de6 656 }
b3b70669 657 return 0;
658}
659
eb191de6 660//by marshmellow (based on existing demod + holiman's refactor)
661//HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
662//print full HID Prox ID and some bit format details if found
b3b70669 663int CmdFSKdemodHID(const char *Cmd)
664{
665 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
b3b70669 666 uint32_t hi2=0, hi=0, lo=0;
ba1a299c 667
eb191de6 668 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 669 size_t BitLen = getFromGraphBuf(BitStream);
b3b70669 670 //get binary from fsk wave
a1d17964 671 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo);
ec75f5c1 672 if (idx<0){
a1d17964 673 if (g_debugMode){
674 if (idx==-1){
675 PrintAndLog("DEBUG: Just Noise Detected");
676 } else if (idx == -2) {
677 PrintAndLog("DEBUG: Error demoding fsk");
678 } else if (idx == -3) {
679 PrintAndLog("DEBUG: Preamble not found");
680 } else if (idx == -4) {
681 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
682 } else {
683 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
684 }
685 }
ec75f5c1 686 return 0;
687 }
688 if (hi2==0 && hi==0 && lo==0) {
689 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
eb191de6 690 return 0;
691 }
692 if (hi2 != 0){ //extra large HID tags
ba1a299c 693 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
eb191de6 694 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF);
695 }
696 else { //standard HID tags <38 bits
d5a72d2f 697 uint8_t fmtLen = 0;
eb191de6 698 uint32_t fc = 0;
699 uint32_t cardnum = 0;
700 if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
701 uint32_t lo2=0;
84871873 702 lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit
eb191de6 703 uint8_t idx3 = 1;
704 while(lo2>1){ //find last bit set to 1 (format len bit)
705 lo2=lo2>>1;
706 idx3++;
b3b70669 707 }
ba1a299c 708 fmtLen =idx3+19;
eb191de6 709 fc =0;
710 cardnum=0;
d5a72d2f 711 if(fmtLen==26){
eb191de6 712 cardnum = (lo>>1)&0xFFFF;
713 fc = (lo>>17)&0xFF;
714 }
d5a72d2f 715 if(fmtLen==34){
eb191de6 716 cardnum = (lo>>1)&0xFFFF;
717 fc= ((hi&1)<<15)|(lo>>17);
718 }
d5a72d2f 719 if(fmtLen==35){
eb191de6 720 cardnum = (lo>>1)&0xFFFFF;
721 fc = ((hi&1)<<11)|(lo>>21);
b3b70669 722 }
b3b70669 723 }
eb191de6 724 else { //if bit 38 is not set then 37 bit format is used
84871873 725 fmtLen = 37;
726 fc = 0;
727 cardnum = 0;
728 if(fmtLen == 37){
eb191de6 729 cardnum = (lo>>1)&0x7FFFF;
730 fc = ((hi&0xF)<<12)|(lo>>20);
731 }
ba1a299c 732 }
733 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
eb191de6 734 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
d5a72d2f 735 (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum);
b3b70669 736 }
ec75f5c1 737 setDemodBuf(BitStream,BitLen,idx);
738 if (g_debugMode){
739 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
740 printDemodBuff();
741 }
742 return 1;
743}
744
04d2721b 745//by marshmellow
746//Paradox Prox demod - FSK RF/50 with preamble of 00001111 (then manchester encoded)
ec75f5c1 747//print full Paradox Prox ID and some bit format details if found
748int CmdFSKdemodParadox(const char *Cmd)
749{
750 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
751 uint32_t hi2=0, hi=0, lo=0;
752
753 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
754 size_t BitLen = getFromGraphBuf(BitStream);
755 //get binary from fsk wave
a1d17964 756 int idx = ParadoxdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo);
ec75f5c1 757 if (idx<0){
a1d17964 758 if (g_debugMode){
759 if (idx==-1){
760 PrintAndLog("DEBUG: Just Noise Detected");
761 } else if (idx == -2) {
762 PrintAndLog("DEBUG: Error demoding fsk");
763 } else if (idx == -3) {
764 PrintAndLog("DEBUG: Preamble not found");
765 } else if (idx == -4) {
766 PrintAndLog("DEBUG: Error in Manchester data");
767 } else {
768 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
769 }
770 }
ec75f5c1 771 return 0;
772 }
773 if (hi2==0 && hi==0 && lo==0){
774 if (g_debugMode) PrintAndLog("DEBUG: Error - no value found");
775 return 0;
776 }
777 uint32_t fc = ((hi & 0x3)<<6) | (lo>>26);
778 uint32_t cardnum = (lo>>10)&0xFFFF;
779
780 PrintAndLog("Paradox TAG ID: %x%08x - FC: %d - Card: %d - Checksum: %02x",
781 hi>>10, (hi & 0x3)<<26 | (lo>>10), fc, cardnum, (lo>>2) & 0xFF );
782 setDemodBuf(BitStream,BitLen,idx);
783 if (g_debugMode){
784 PrintAndLog("DEBUG: idx: %d, len: %d, Printing Demod Buffer:", idx, BitLen);
785 printDemodBuff();
786 }
787 return 1;
b3b70669 788}
789
ec75f5c1 790
2fc2150e 791//by marshmellow
eb191de6 792//IO-Prox demod - FSK RF/64 with preamble of 000000001
793//print ioprox ID and some format details
b3b70669 794int CmdFSKdemodIO(const char *Cmd)
795{
796 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
797 //set defaults
ba1a299c 798 int idx=0;
ec75f5c1 799 //something in graphbuffer?
800 if (GraphTraceLen < 65) {
801 if (g_debugMode)PrintAndLog("DEBUG: not enough samples in GraphBuffer");
802 return 0;
803 }
eb191de6 804 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
ba1a299c 805 size_t BitLen = getFromGraphBuf(BitStream);
ec75f5c1 806
eb191de6 807 //get binary from fsk wave
ba1a299c 808 idx = IOdemodFSK(BitStream,BitLen);
eb191de6 809 if (idx<0){
a1d17964 810 if (g_debugMode){
811 if (idx==-1){
812 PrintAndLog("DEBUG: Just Noise Detected");
813 } else if (idx == -2) {
814 PrintAndLog("DEBUG: not enough samples");
815 } else if (idx == -3) {
816 PrintAndLog("DEBUG: error during fskdemod");
817 } else if (idx == -4) {
818 PrintAndLog("DEBUG: Preamble not found");
819 } else if (idx == -5) {
820 PrintAndLog("DEBUG: Separator bits not found");
821 } else {
822 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
823 }
824 }
eb191de6 825 return 0;
826 }
827 if (idx==0){
ec75f5c1 828 if (g_debugMode==1){
829 PrintAndLog("DEBUG: IO Prox Data not found - FSK Bits: %d",BitLen);
830 if (BitLen > 92) printBitStream(BitStream,92);
831 }
d5a72d2f 832 return 0;
b3b70669 833 }
b3b70669 834 //Index map
835 //0 10 20 30 40 50 60
836 //| | | | | | |
837 //01234567 8 90123456 7 89012345 6 78901234 5 67890123 4 56789012 3 45678901 23
838 //-----------------------------------------------------------------------------
839 //00000000 0 11110000 1 facility 1 version* 1 code*one 1 code*two 1 ???????? 11
840 //
841 //XSF(version)facility:codeone+codetwo (raw)
842 //Handle the data
ec75f5c1 843 if (idx+64>BitLen) {
844 if (g_debugMode==1) PrintAndLog("not enough bits found - bitlen: %d",BitLen);
845 return 0;
846 }
eb191de6 847 PrintAndLog("%d%d%d%d%d%d%d%d %d",BitStream[idx], BitStream[idx+1], BitStream[idx+2], BitStream[idx+3], BitStream[idx+4], BitStream[idx+5], BitStream[idx+6], BitStream[idx+7], BitStream[idx+8]);
ba1a299c 848 PrintAndLog("%d%d%d%d%d%d%d%d %d",BitStream[idx+9], BitStream[idx+10], BitStream[idx+11],BitStream[idx+12],BitStream[idx+13],BitStream[idx+14],BitStream[idx+15],BitStream[idx+16],BitStream[idx+17]);
f822a063 849 PrintAndLog("%d%d%d%d%d%d%d%d %d facility",BitStream[idx+18], BitStream[idx+19], BitStream[idx+20],BitStream[idx+21],BitStream[idx+22],BitStream[idx+23],BitStream[idx+24],BitStream[idx+25],BitStream[idx+26]);
850 PrintAndLog("%d%d%d%d%d%d%d%d %d version",BitStream[idx+27], BitStream[idx+28], BitStream[idx+29],BitStream[idx+30],BitStream[idx+31],BitStream[idx+32],BitStream[idx+33],BitStream[idx+34],BitStream[idx+35]);
851 PrintAndLog("%d%d%d%d%d%d%d%d %d code1",BitStream[idx+36], BitStream[idx+37], BitStream[idx+38],BitStream[idx+39],BitStream[idx+40],BitStream[idx+41],BitStream[idx+42],BitStream[idx+43],BitStream[idx+44]);
852 PrintAndLog("%d%d%d%d%d%d%d%d %d code2",BitStream[idx+45], BitStream[idx+46], BitStream[idx+47],BitStream[idx+48],BitStream[idx+49],BitStream[idx+50],BitStream[idx+51],BitStream[idx+52],BitStream[idx+53]);
853 PrintAndLog("%d%d%d%d%d%d%d%d %d%d checksum",BitStream[idx+54],BitStream[idx+55],BitStream[idx+56],BitStream[idx+57],BitStream[idx+58],BitStream[idx+59],BitStream[idx+60],BitStream[idx+61],BitStream[idx+62],BitStream[idx+63]);
ba1a299c 854
eb191de6 855 uint32_t code = bytebits_to_byte(BitStream+idx,32);
ba1a299c 856 uint32_t code2 = bytebits_to_byte(BitStream+idx+32,32);
f822a063 857 uint8_t version = bytebits_to_byte(BitStream+idx+27,8); //14,4
858 uint8_t facilitycode = bytebits_to_byte(BitStream+idx+18,8) ;
eb191de6 859 uint16_t number = (bytebits_to_byte(BitStream+idx+36,8)<<8)|(bytebits_to_byte(BitStream+idx+45,8)); //36,9
ba1a299c 860 PrintAndLog("IO Prox XSF(%02d)%02x:%05d (%08x%08x)",version,facilitycode,number,code,code2);
ec75f5c1 861 setDemodBuf(BitStream,64,idx);
862 if (g_debugMode){
863 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing demod buffer:",idx,64);
864 printDemodBuff();
865 }
866 return 1;
b3b70669 867}
1e090a61 868
869
870//by marshmellow
871//AWID Prox demod - FSK RF/50 with preamble of 00000001 (always a 96 bit data stream)
872//print full AWID Prox ID and some bit format details if found
873int CmdFSKdemodAWID(const char *Cmd)
874{
875
ec75f5c1 876 //int verbose=1;
877 //sscanf(Cmd, "%i", &verbose);
1e090a61 878
879 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
880 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
881 size_t size = getFromGraphBuf(BitStream);
882
883 //get binary from fsk wave
a1d17964 884 int idx = AWIDdemodFSK(BitStream, &size);
1e090a61 885 if (idx<=0){
ec75f5c1 886 if (g_debugMode==1){
1e090a61 887 if (idx == -1)
ec75f5c1 888 PrintAndLog("DEBUG: Error - not enough samples");
1e090a61 889 else if (idx == -2)
a1d17964 890 PrintAndLog("DEBUG: Error - only noise found");
1e090a61 891 else if (idx == -3)
ec75f5c1 892 PrintAndLog("DEBUG: Error - problem during FSK demod");
1e090a61 893 // else if (idx == -3)
894 // PrintAndLog("Error: thought we had a tag but the parity failed");
895 else if (idx == -4)
ec75f5c1 896 PrintAndLog("DEBUG: Error - AWID preamble not found");
897 else if (idx == -5)
a1d17964 898 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
ec75f5c1 899 else
900 PrintAndLog("DEBUG: Error %d",idx);
1e090a61 901 }
902 return 0;
903 }
904
905 // Index map
906 // 0 10 20 30 40 50 60
907 // | | | | | | |
908 // 01234567 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 - to 96
909 // -----------------------------------------------------------------------------
910 // 00000001 000 1 110 1 101 1 011 1 101 1 010 0 000 1 000 1 010 0 001 0 110 1 100 0 000 1 000 1
911 // premable bbb o bbb o bbw o fff o fff o ffc o ccc o ccc o ccc o ccc o ccc o wxx o xxx o xxx o - to 96
912 // |---26 bit---| |-----117----||-------------142-------------|
913 // b = format bit len, o = odd parity of last 3 bits
914 // f = facility code, c = card number
915 // w = wiegand parity
916 // (26 bit format shown)
917
918 //get raw ID before removing parities
919 uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32);
920 uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32);
921 uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32);
ec75f5c1 922 setDemodBuf(BitStream,96,idx);
923
1e090a61 924 size = removeParity(BitStream, idx+8, 4, 1, 88);
925 if (size != 66){
ec75f5c1 926 if (g_debugMode==1) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format");
1e090a61 927 return 0;
928 }
929 // ok valid card found!
930
931 // Index map
932 // 0 10 20 30 40 50 60
933 // | | | | | | |
934 // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456
935 // -----------------------------------------------------------------------------
936 // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000
937 // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
938 // |26 bit| |-117--| |-----142------|
939 // b = format bit len, o = odd parity of last 3 bits
940 // f = facility code, c = card number
941 // w = wiegand parity
942 // (26 bit format shown)
943
944 uint32_t fc = 0;
945 uint32_t cardnum = 0;
946 uint32_t code1 = 0;
947 uint32_t code2 = 0;
948 uint8_t fmtLen = bytebits_to_byte(BitStream,8);
949 if (fmtLen==26){
950 fc = bytebits_to_byte(BitStream+9, 8);
951 cardnum = bytebits_to_byte(BitStream+17, 16);
952 code1 = bytebits_to_byte(BitStream+8,fmtLen);
953 PrintAndLog("AWID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %x%08x%08x", fmtLen, fc, cardnum, code1, rawHi2, rawHi, rawLo);
954 } else {
955 cardnum = bytebits_to_byte(BitStream+8+(fmtLen-17), 16);
956 if (fmtLen>32){
957 code1 = bytebits_to_byte(BitStream+8,fmtLen-32);
958 code2 = bytebits_to_byte(BitStream+8+(fmtLen-32),32);
959 PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x%08x, Raw: %x%08x%08x", fmtLen, cardnum, code1, code2, rawHi2, rawHi, rawLo);
960 } else{
961 code1 = bytebits_to_byte(BitStream+8,fmtLen);
962 PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x, Raw: %x%08x%08x", fmtLen, cardnum, code1, rawHi2, rawHi, rawLo);
963 }
964 }
ec75f5c1 965 if (g_debugMode){
966 PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx, 96);
967 printDemodBuff();
968 }
1e090a61 969 //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands
1e090a61 970 return 1;
971}
972
973//by marshmellow
974//Pyramid Prox demod - FSK RF/50 with preamble of 0000000000000001 (always a 128 bit data stream)
975//print full Farpointe Data/Pyramid Prox ID and some bit format details if found
976int CmdFSKdemodPyramid(const char *Cmd)
977{
1e090a61 978 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
979 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
980 size_t size = getFromGraphBuf(BitStream);
981
982 //get binary from fsk wave
a1d17964 983 int idx = PyramiddemodFSK(BitStream, &size);
1e090a61 984 if (idx < 0){
ec75f5c1 985 if (g_debugMode==1){
1e090a61 986 if (idx == -5)
ec75f5c1 987 PrintAndLog("DEBUG: Error - not enough samples");
1e090a61 988 else if (idx == -1)
a1d17964 989 PrintAndLog("DEBUG: Error - only noise found");
1e090a61 990 else if (idx == -2)
ec75f5c1 991 PrintAndLog("DEBUG: Error - problem during FSK demod");
992 else if (idx == -3)
a1d17964 993 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
1e090a61 994 else if (idx == -4)
ec75f5c1 995 PrintAndLog("DEBUG: Error - Pyramid preamble not found");
996 else
997 PrintAndLog("DEBUG: Error - idx: %d",idx);
1e090a61 998 }
1e090a61 999 return 0;
1000 }
1e090a61 1001 // Index map
1002 // 0 10 20 30 40 50 60
1003 // | | | | | | |
1004 // 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3
1005 // -----------------------------------------------------------------------------
1006 // 0000000 0 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1 0000000 1
1007 // premable xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o xxxxxxx o
1008
1009 // 64 70 80 90 100 110 120
1010 // | | | | | | |
1011 // 4567890 1 2345678 9 0123456 7 8901234 5 6789012 3 4567890 1 2345678 9 0123456 7
1012 // -----------------------------------------------------------------------------
1013 // 0000000 1 0000000 1 0000000 1 0110111 0 0011000 1 0000001 0 0001100 1 1001010 0
1014 // xxxxxxx o xxxxxxx o xxxxxxx o xswffff o ffffccc o ccccccc o ccccccw o ppppppp o
1015 // |---115---||---------71---------|
1016 // s = format start bit, o = odd parity of last 7 bits
1017 // f = facility code, c = card number
1018 // w = wiegand parity, x = extra space for other formats
1019 // p = unknown checksum
1020 // (26 bit format shown)
1021
1022 //get raw ID before removing parities
1023 uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32);
1024 uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32);
1025 uint32_t rawHi2 = bytebits_to_byte(BitStream+idx+32,32);
1026 uint32_t rawHi3 = bytebits_to_byte(BitStream+idx,32);
ec75f5c1 1027 setDemodBuf(BitStream,128,idx);
1028
1e090a61 1029 size = removeParity(BitStream, idx+8, 8, 1, 120);
1030 if (size != 105){
ec75f5c1 1031 if (g_debugMode==1) PrintAndLog("DEBUG: Error at parity check-tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size, idx, rawHi3);
1e090a61 1032 return 0;
1033 }
1034
1035 // ok valid card found!
1036
1037 // Index map
1038 // 0 10 20 30 40 50 60 70
1039 // | | | | | | | |
1040 // 01234567890123456789012345678901234567890123456789012345678901234567890
1041 // -----------------------------------------------------------------------
1042 // 00000000000000000000000000000000000000000000000000000000000000000000000
1043 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1044
1045 // 71 80 90 100
1046 // | | | |
1047 // 1 2 34567890 1234567890123456 7 8901234
1048 // ---------------------------------------
1049 // 1 1 01110011 0000000001000110 0 1001010
1050 // s w ffffffff cccccccccccccccc w ppppppp
1051 // |--115-| |------71------|
1052 // s = format start bit, o = odd parity of last 7 bits
1053 // f = facility code, c = card number
1054 // w = wiegand parity, x = extra space for other formats
1055 // p = unknown checksum
1056 // (26 bit format shown)
1057
1058 //find start bit to get fmtLen
1e090a61 1059 int j;
1060 for (j=0; j<size; j++){
ec75f5c1 1061 if(BitStream[j]) break;
1e090a61 1062 }
1063 uint8_t fmtLen = size-j-8;
1064 uint32_t fc = 0;
1065 uint32_t cardnum = 0;
1066 uint32_t code1 = 0;
1067 //uint32_t code2 = 0;
1068 if (fmtLen==26){
1069 fc = bytebits_to_byte(BitStream+73, 8);
1070 cardnum = bytebits_to_byte(BitStream+81, 16);
1071 code1 = bytebits_to_byte(BitStream+72,fmtLen);
ec75f5c1 1072 PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %x%08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1073 } else if (fmtLen==45){
1074 fmtLen=42; //end = 10 bits not 7 like 26 bit fmt
1075 fc = bytebits_to_byte(BitStream+53, 10);
1076 cardnum = bytebits_to_byte(BitStream+63, 32);
ec75f5c1 1077 PrintAndLog("Pyramid ID Found - BitLength: %d, FC: %d, Card: %d - Raw: %x%08x%08x%08x", fmtLen, fc, cardnum, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1078 } else {
1079 cardnum = bytebits_to_byte(BitStream+81, 16);
1080 if (fmtLen>32){
1081 //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen-32);
1082 //code2 = bytebits_to_byte(BitStream+(size-32),32);
ec75f5c1 1083 PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1084 } else{
1085 //code1 = bytebits_to_byte(BitStream+(size-fmtLen),fmtLen);
ec75f5c1 1086 PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo);
1e090a61 1087 }
1088 }
ec75f5c1 1089 if (g_debugMode){
1090 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
1091 printDemodBuff();
1092 }
1e090a61 1093 return 1;
1094}
1095
e888ed8e 1096int CmdFSKdemod(const char *Cmd) //old CmdFSKdemod needs updating
7fe9b0b7 1097{
1098 static const int LowTone[] = {
1099 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1100 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1101 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1102 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
1103 1, 1, 1, 1, 1, -1, -1, -1, -1, -1
1104 };
1105 static const int HighTone[] = {
1106 1, 1, 1, 1, 1, -1, -1, -1, -1,
1107 1, 1, 1, 1, -1, -1, -1, -1,
1108 1, 1, 1, 1, -1, -1, -1, -1,
1109 1, 1, 1, 1, -1, -1, -1, -1,
1110 1, 1, 1, 1, -1, -1, -1, -1,
1111 1, 1, 1, 1, -1, -1, -1, -1, -1,
1112 };
1113
1114 int lowLen = sizeof (LowTone) / sizeof (int);
1115 int highLen = sizeof (HighTone) / sizeof (int);
b915fda3 1116 int convLen = (highLen > lowLen) ? highLen : lowLen;
7fe9b0b7 1117 uint32_t hi = 0, lo = 0;
1118
1119 int i, j;
1120 int minMark = 0, maxMark = 0;
952a8bb5 1121
7fe9b0b7 1122 for (i = 0; i < GraphTraceLen - convLen; ++i) {
1123 int lowSum = 0, highSum = 0;
1124
1125 for (j = 0; j < lowLen; ++j) {
1126 lowSum += LowTone[j]*GraphBuffer[i+j];
1127 }
1128 for (j = 0; j < highLen; ++j) {
1129 highSum += HighTone[j] * GraphBuffer[i + j];
1130 }
1131 lowSum = abs(100 * lowSum / lowLen);
1132 highSum = abs(100 * highSum / highLen);
1133 GraphBuffer[i] = (highSum << 16) | lowSum;
1134 }
1135
1136 for(i = 0; i < GraphTraceLen - convLen - 16; ++i) {
1137 int lowTot = 0, highTot = 0;
1138 // 10 and 8 are f_s divided by f_l and f_h, rounded
1139 for (j = 0; j < 10; ++j) {
1140 lowTot += (GraphBuffer[i+j] & 0xffff);
1141 }
1142 for (j = 0; j < 8; j++) {
1143 highTot += (GraphBuffer[i + j] >> 16);
1144 }
1145 GraphBuffer[i] = lowTot - highTot;
1146 if (GraphBuffer[i] > maxMark) maxMark = GraphBuffer[i];
1147 if (GraphBuffer[i] < minMark) minMark = GraphBuffer[i];
1148 }
1149
1150 GraphTraceLen -= (convLen + 16);
1151 RepaintGraphWindow();
1152
b3b70669 1153 // Find bit-sync (3 lo followed by 3 high) (HID ONLY)
7fe9b0b7 1154 int max = 0, maxPos = 0;
1155 for (i = 0; i < 6000; ++i) {
1156 int dec = 0;
1157 for (j = 0; j < 3 * lowLen; ++j) {
1158 dec -= GraphBuffer[i + j];
1159 }
1160 for (; j < 3 * (lowLen + highLen ); ++j) {
1161 dec += GraphBuffer[i + j];
1162 }
1163 if (dec > max) {
1164 max = dec;
1165 maxPos = i;
1166 }
1167 }
1168
1169 // place start of bit sync marker in graph
1170 GraphBuffer[maxPos] = maxMark;
1171 GraphBuffer[maxPos + 1] = minMark;
1172
1173 maxPos += j;
1174
1175 // place end of bit sync marker in graph
1176 GraphBuffer[maxPos] = maxMark;
1177 GraphBuffer[maxPos+1] = minMark;
1178
1179 PrintAndLog("actual data bits start at sample %d", maxPos);
1180 PrintAndLog("length %d/%d", highLen, lowLen);
ba1a299c 1181
a1557c4c 1182 uint8_t bits[46] = {0x00};
ba1a299c 1183
7fe9b0b7 1184 // find bit pairs and manchester decode them
1185 for (i = 0; i < arraylen(bits) - 1; ++i) {
1186 int dec = 0;
1187 for (j = 0; j < lowLen; ++j) {
1188 dec -= GraphBuffer[maxPos + j];
1189 }
1190 for (; j < lowLen + highLen; ++j) {
1191 dec += GraphBuffer[maxPos + j];
1192 }
1193 maxPos += j;
1194 // place inter bit marker in graph
1195 GraphBuffer[maxPos] = maxMark;
1196 GraphBuffer[maxPos + 1] = minMark;
1197
1198 // hi and lo form a 64 bit pair
1199 hi = (hi << 1) | (lo >> 31);
1200 lo = (lo << 1);
1201 // store decoded bit as binary (in hi/lo) and text (in bits[])
1202 if(dec < 0) {
1203 bits[i] = '1';
1204 lo |= 1;
1205 } else {
1206 bits[i] = '0';
1207 }
1208 }
1209 PrintAndLog("bits: '%s'", bits);
1210 PrintAndLog("hex: %08x %08x", hi, lo);
1211 return 0;
1212}
e888ed8e 1213
ec75f5c1 1214//by marshmellow
1215//attempt to detect the field clock and bit clock for FSK
1e090a61 1216int CmdFSKfcDetect(const char *Cmd)
1217{
1218 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1219 size_t size = getFromGraphBuf(BitStream);
1220
03e6bb4a 1221 uint16_t ans = countFC(BitStream, size);
1222 if (ans==0) {
1223 if (g_debugMode) PrintAndLog("DEBUG: No data found");
1224 return 0;
1225 }
1226 uint8_t fc1, fc2;
1e090a61 1227 fc1 = (ans >> 8) & 0xFF;
1228 fc2 = ans & 0xFF;
03e6bb4a 1229
1230 uint8_t rf1 = detectFSKClk(BitStream, size, fc1, fc2);
1231 if (rf1==0) {
1232 if (g_debugMode) PrintAndLog("DEBUG: Clock detect error");
1233 return 0;
1234 }
1e090a61 1235 PrintAndLog("Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
1236 return 1;
1237}
1238
8c65b650 1239//by marshmellow
1240//attempt to detect the bit clock for PSK or NRZ modulations
4118b74d 1241int CmdDetectNRZpskClockRate(const char *Cmd)
1242{
ba1a299c 1243 GetNRZpskClock("",0,0);
1244 return 0;
4118b74d 1245}
1246
8c65b650 1247//by marshmellow
1248//attempt to psk1 or nrz demod graph buffer
1249//NOTE CURRENTLY RELIES ON PEAKS :(
04d2721b 1250int PSKnrzDemod(const char *Cmd, uint8_t verbose)
ec75f5c1 1251{
ba1a299c 1252 int invert=0;
1253 int clk=0;
1254 sscanf(Cmd, "%i %i", &clk, &invert);
1255 if (invert != 0 && invert != 1) {
1256 PrintAndLog("Invalid argument: %s", Cmd);
1257 return -1;
1258 }
1259 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
1260 size_t BitLen = getFromGraphBuf(BitStream);
1261 int errCnt=0;
1262 errCnt = pskNRZrawDemod(BitStream, &BitLen,&clk,&invert);
1263 if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
ec75f5c1 1264 if (g_debugMode==1) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
ba1a299c 1265 return -1;
1266 }
04d2721b 1267 if (verbose) PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
ba1a299c 1268
1269 //prime demod buffer for output
ec75f5c1 1270 setDemodBuf(BitStream,BitLen,0);
ba1a299c 1271 return errCnt;
4118b74d 1272}
1273// Indala 26 bit decode
1274// by marshmellow
1275// optional arguments - same as CmdpskNRZrawDemod (clock & invert)
1276int CmdIndalaDecode(const char *Cmd)
1277{
84871873 1278 int ans;
1279 if (strlen(Cmd)>0){
04d2721b 1280 ans = PSKnrzDemod(Cmd, 0);
84871873 1281 } else{ //default to RF/32
04d2721b 1282 ans = PSKnrzDemod("32", 0);
84871873 1283 }
4118b74d 1284
ba1a299c 1285 if (ans < 0){
ec75f5c1 1286 if (g_debugMode==1)
84871873 1287 PrintAndLog("Error1: %d",ans);
ba1a299c 1288 return 0;
1289 }
1290 uint8_t invert=0;
1291 ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert);
1292 if (ans < 1) {
ec75f5c1 1293 if (g_debugMode==1)
84871873 1294 PrintAndLog("Error2: %d",ans);
ba1a299c 1295 return -1;
1296 }
a1d17964 1297 char showbits[251]={0x00};
84871873 1298 if (invert)
ec75f5c1 1299 if (g_debugMode==1)
84871873 1300 PrintAndLog("Had to invert bits");
ec75f5c1 1301
ba1a299c 1302 //convert UID to HEX
1303 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
1304 int idx;
1305 uid1=0;
1306 uid2=0;
1307 PrintAndLog("BitLen: %d",DemodBufferLen);
1308 if (DemodBufferLen==64){
1309 for( idx=0; idx<64; idx++) {
1310 uid1=(uid1<<1)|(uid2>>31);
1311 if (DemodBuffer[idx] == 0) {
1312 uid2=(uid2<<1)|0;
1313 showbits[idx]='0';
1314 } else {
1315 uid2=(uid2<<1)|1;
1316 showbits[idx]='1';
1317 }
1318 }
1319 showbits[idx]='\0';
1320 PrintAndLog("Indala UID=%s (%x%08x)", showbits, uid1, uid2);
1321 }
1322 else {
1323 uid3=0;
1324 uid4=0;
1325 uid5=0;
1326 uid6=0;
1327 uid7=0;
1328 for( idx=0; idx<DemodBufferLen; idx++) {
1329 uid1=(uid1<<1)|(uid2>>31);
1330 uid2=(uid2<<1)|(uid3>>31);
1331 uid3=(uid3<<1)|(uid4>>31);
1332 uid4=(uid4<<1)|(uid5>>31);
1333 uid5=(uid5<<1)|(uid6>>31);
1334 uid6=(uid6<<1)|(uid7>>31);
1335 if (DemodBuffer[idx] == 0) {
1336 uid7=(uid7<<1)|0;
1337 showbits[idx]='0';
1338 }
1339 else {
1340 uid7=(uid7<<1)|1;
1341 showbits[idx]='1';
1342 }
1343 }
1344 showbits[idx]='\0';
1345 PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
1346 }
a1d17964 1347 if (g_debugMode){
1348 PrintAndLog("DEBUG: printing demodbuffer:");
1349 printDemodBuff();
1350 }
ba1a299c 1351 return 1;
4118b74d 1352}
1353
8c65b650 1354//by marshmellow
1355//attempt to clean psk wave noise after a peak
1356//NOTE RELIES ON PEAKS :(
4118b74d 1357int CmdPskClean(const char *Cmd)
1358{
ba1a299c 1359 uint8_t bitStream[MAX_GRAPH_TRACE_LEN]={0};
1360 size_t bitLen = getFromGraphBuf(bitStream);
1361 pskCleanWave(bitStream, bitLen);
1362 setGraphBuf(bitStream, bitLen);
1363 return 0;
4118b74d 1364}
1365
04d2721b 1366// by marshmellow
1367// takes 2 arguments - clock and invert both as integers
1368// attempts to demodulate psk only
1369// prints binary found and saves in demodbuffer for further commands
4118b74d 1370int CmdpskNRZrawDemod(const char *Cmd)
1371{
84871873 1372 int errCnt;
ec75f5c1 1373
04d2721b 1374 errCnt = PSKnrzDemod(Cmd, 1);
ba1a299c 1375 //output
ec75f5c1 1376 if (errCnt<0){
1377 if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
1378 return 0;
1379 }
ba1a299c 1380 if (errCnt>0){
ec75f5c1 1381 if (g_debugMode){
84871873 1382 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
ec75f5c1 1383 PrintAndLog("PSK or NRZ demoded bitstream:");
1384 // Now output the bitstream to the scrollback by line of 16 bits
1385 printDemodBuff();
1386 }
1387 }else{
1388 PrintAndLog("PSK or NRZ demoded bitstream:");
1389 // Now output the bitstream to the scrollback by line of 16 bits
1390 printDemodBuff();
1391 return 1;
1392 }
1393 return 0;
4118b74d 1394}
1395
04d2721b 1396// by marshmellow
1397// takes same args as cmdpsknrzrawdemod
1398int CmdPSK2rawDemod(const char *Cmd)
1399{
1400 int errCnt=0;
1401 errCnt=PSKnrzDemod(Cmd, 1);
1402 if (errCnt<0){
1403 if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
1404 return 0;
1405 }
1406 psk1TOpsk2(DemodBuffer, DemodBufferLen);
1407 if (errCnt>0){
1408 if (g_debugMode){
1409 PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
1410 PrintAndLog("PSK2 demoded bitstream:");
1411 // Now output the bitstream to the scrollback by line of 16 bits
1412 printDemodBuff();
1413 }
1414 }else{
1415 PrintAndLog("PSK2 demoded bitstream:");
1416 // Now output the bitstream to the scrollback by line of 16 bits
1417 printDemodBuff();
1418 }
1419 return 1;
1420}
1421
7fe9b0b7 1422int CmdGrid(const char *Cmd)
1423{
1424 sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
7ddb9900 1425 PlotGridXdefault= PlotGridX;
1426 PlotGridYdefault= PlotGridY;
7fe9b0b7 1427 RepaintGraphWindow();
1428 return 0;
1429}
1430
1431int CmdHexsamples(const char *Cmd)
1432{
4961e292 1433 int i, j;
7fe9b0b7 1434 int requested = 0;
1435 int offset = 0;
4961e292 1436 char string_buf[25];
1437 char* string_ptr = string_buf;
f71f4deb 1438 uint8_t got[BIGBUF_SIZE];
952a8bb5 1439
4961e292 1440 sscanf(Cmd, "%i %i", &requested, &offset);
90d74dc2 1441
4961e292 1442 /* if no args send something */
1443 if (requested == 0) {
90d74dc2 1444 requested = 8;
1445 }
90d74dc2 1446 if (offset + requested > sizeof(got)) {
f71f4deb 1447 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE);
4961e292 1448 return 0;
952a8bb5 1449 }
90d74dc2 1450
4961e292 1451 GetFromBigBuf(got,requested,offset);
90d74dc2 1452 WaitForResponse(CMD_ACK,NULL);
1453
4961e292 1454 i = 0;
1455 for (j = 0; j < requested; j++) {
1456 i++;
1457 string_ptr += sprintf(string_ptr, "%02x ", got[j]);
1458 if (i == 8) {
1459 *(string_ptr - 1) = '\0'; // remove the trailing space
1460 PrintAndLog("%s", string_buf);
1461 string_buf[0] = '\0';
1462 string_ptr = string_buf;
1463 i = 0;
1464 }
1465 if (j == requested - 1 && string_buf[0] != '\0') { // print any remaining bytes
1466 *(string_ptr - 1) = '\0';
1467 PrintAndLog("%s", string_buf);
1468 string_buf[0] = '\0';
7fe9b0b7 1469 }
ba1a299c 1470 }
7fe9b0b7 1471 return 0;
1472}
1473
7fe9b0b7 1474int CmdHide(const char *Cmd)
1475{
1476 HideGraphWindow();
1477 return 0;
1478}
1479
1480int CmdHpf(const char *Cmd)
1481{
1482 int i;
1483 int accum = 0;
1484
1485 for (i = 10; i < GraphTraceLen; ++i)
1486 accum += GraphBuffer[i];
1487 accum /= (GraphTraceLen - 10);
1488 for (i = 0; i < GraphTraceLen; ++i)
1489 GraphBuffer[i] -= accum;
1490
1491 RepaintGraphWindow();
1492 return 0;
1493}
1494
8d183c53 1495int CmdSamples(const char *Cmd)
7fe9b0b7 1496{
f71f4deb 1497 uint8_t got[BIGBUF_SIZE] = {0x00};
ba1a299c 1498
d6d20c54 1499 int n = strtol(Cmd, NULL, 0);
1500 if (n == 0)
1501 n = 20000;
1502
1503 if (n > sizeof(got))
1504 n = sizeof(got);
ba1a299c 1505
d6d20c54 1506 PrintAndLog("Reading %d samples from device memory\n", n);
f71f4deb 1507 GetFromBigBuf(got,n,0);
1508 WaitForResponse(CMD_ACK,NULL);
ba1a299c 1509 for (int j = 0; j < n; j++) {
d6d20c54 1510 GraphBuffer[j] = ((int)got[j]) - 128;
f71f4deb 1511 }
1512 GraphTraceLen = n;
1513 RepaintGraphWindow();
1514 return 0;
7fe9b0b7 1515}
1516
d6a120a2
MHS
1517int CmdTuneSamples(const char *Cmd)
1518{
3aa4014b 1519 int timeout = 0;
1520 printf("\nMeasuring antenna characteristics, please wait...");
ba1a299c 1521
3aa4014b 1522 UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING};
1523 SendCommand(&c);
1524
1525 UsbCommand resp;
1526 while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING,&resp,1000)) {
1527 timeout++;
1528 printf(".");
1529 if (timeout > 7) {
1530 PrintAndLog("\nNo response from Proxmark. Aborting...");
1531 return 1;
1532 }
1533 }
1534
1535 int peakv, peakf;
1536 int vLf125, vLf134, vHf;
1537 vLf125 = resp.arg[0] & 0xffff;
1538 vLf134 = resp.arg[0] >> 16;
1539 vHf = resp.arg[1] & 0xffff;;
1540 peakf = resp.arg[2] & 0xffff;
1541 peakv = resp.arg[2] >> 16;
1542 PrintAndLog("");
1543 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
1544 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
1545 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
1546 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
1547 if (peakv<2000)
1548 PrintAndLog("# Your LF antenna is unusable.");
1549 else if (peakv<10000)
1550 PrintAndLog("# Your LF antenna is marginal.");
1551 if (vHf<2000)
1552 PrintAndLog("# Your HF antenna is unusable.");
1553 else if (vHf<5000)
1554 PrintAndLog("# Your HF antenna is marginal.");
1555
1556 for (int i = 0; i < 256; i++) {
1557 GraphBuffer[i] = resp.d.asBytes[i] - 128;
ba1a299c 1558 }
1559
d5a72d2f 1560 PrintAndLog("Done! Divisor 89 is 134khz, 95 is 125khz.\n");
1561 PrintAndLog("\n");
3aa4014b 1562 GraphTraceLen = 256;
1563 ShowGraphWindow();
1564
1565 return 0;
d6a120a2
MHS
1566}
1567
3aa4014b 1568
7fe9b0b7 1569int CmdLoad(const char *Cmd)
1570{
ec75f5c1 1571 char filename[FILE_PATH_SIZE] = {0x00};
1572 int len = 0;
b915fda3 1573
ec75f5c1 1574 len = strlen(Cmd);
1575 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
1576 memcpy(filename, Cmd, len);
b915fda3 1577
ec75f5c1 1578 FILE *f = fopen(filename, "r");
7fe9b0b7 1579 if (!f) {
b915fda3 1580 PrintAndLog("couldn't open '%s'", filename);
7fe9b0b7 1581 return 0;
1582 }
1583
1584 GraphTraceLen = 0;
1585 char line[80];
1586 while (fgets(line, sizeof (line), f)) {
1587 GraphBuffer[GraphTraceLen] = atoi(line);
1588 GraphTraceLen++;
1589 }
1590 fclose(f);
1591 PrintAndLog("loaded %d samples", GraphTraceLen);
1592 RepaintGraphWindow();
1593 return 0;
1594}
1595
1596int CmdLtrim(const char *Cmd)
1597{
1598 int ds = atoi(Cmd);
1599
1600 for (int i = ds; i < GraphTraceLen; ++i)
1601 GraphBuffer[i-ds] = GraphBuffer[i];
1602 GraphTraceLen -= ds;
1603
1604 RepaintGraphWindow();
1605 return 0;
1606}
ec75f5c1 1607
1608// trim graph to input argument length
9ec1416a 1609int CmdRtrim(const char *Cmd)
1610{
1611 int ds = atoi(Cmd);
1612
1613 GraphTraceLen = ds;
1614
1615 RepaintGraphWindow();
1616 return 0;
1617}
7fe9b0b7 1618
1619/*
1620 * Manchester demodulate a bitstream. The bitstream needs to be already in
1621 * the GraphBuffer as 0 and 1 values
1622 *
1623 * Give the clock rate as argument in order to help the sync - the algorithm
1624 * resyncs at each pulse anyway.
1625 *
1626 * Not optimized by any means, this is the 1st time I'm writing this type of
1627 * routine, feel free to improve...
1628 *
1629 * 1st argument: clock rate (as number of samples per clock rate)
1630 * Typical values can be 64, 32, 128...
1631 */
1632int CmdManchesterDemod(const char *Cmd)
1633{
1634 int i, j, invert= 0;
1635 int bit;
1636 int clock;
fddf220a 1637 int lastval = 0;
7fe9b0b7 1638 int low = 0;
1639 int high = 0;
1640 int hithigh, hitlow, first;
1641 int lc = 0;
1642 int bitidx = 0;
1643 int bit2idx = 0;
1644 int warnings = 0;
1645
1646 /* check if we're inverting output */
c6f1fb9d 1647 if (*Cmd == 'i')
7fe9b0b7 1648 {
1649 PrintAndLog("Inverting output");
1650 invert = 1;
fffad860 1651 ++Cmd;
7fe9b0b7 1652 do
1653 ++Cmd;
1654 while(*Cmd == ' '); // in case a 2nd argument was given
1655 }
1656
1657 /* Holds the decoded bitstream: each clock period contains 2 bits */
1658 /* later simplified to 1 bit after manchester decoding. */
1659 /* Add 10 bits to allow for noisy / uncertain traces without aborting */
1660 /* int BitStream[GraphTraceLen*2/clock+10]; */
1661
1662 /* But it does not work if compiling on WIndows: therefore we just allocate a */
1663 /* large array */
90e278d3 1664 uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
7fe9b0b7 1665
1666 /* Detect high and lows */
1667 for (i = 0; i < GraphTraceLen; i++)
1668 {
1669 if (GraphBuffer[i] > high)
1670 high = GraphBuffer[i];
1671 else if (GraphBuffer[i] < low)
1672 low = GraphBuffer[i];
1673 }
1674
1675 /* Get our clock */
1676 clock = GetClock(Cmd, high, 1);
1677
1678 int tolerance = clock/4;
1679
1680 /* Detect first transition */
1681 /* Lo-Hi (arbitrary) */
1682 /* skip to the first high */
1683 for (i= 0; i < GraphTraceLen; i++)
1684 if (GraphBuffer[i] == high)
1685 break;
1686 /* now look for the first low */
1687 for (; i < GraphTraceLen; i++)
1688 {
1689 if (GraphBuffer[i] == low)
1690 {
1691 lastval = i;
1692 break;
1693 }
1694 }
1695
1696 /* If we're not working with 1/0s, demod based off clock */
1697 if (high != 1)
1698 {
1699 bit = 0; /* We assume the 1st bit is zero, it may not be
1700 * the case: this routine (I think) has an init problem.
1701 * Ed.
1702 */
1703 for (; i < (int)(GraphTraceLen / clock); i++)
1704 {
1705 hithigh = 0;
1706 hitlow = 0;
1707 first = 1;
1708
1709 /* Find out if we hit both high and low peaks */
1710 for (j = 0; j < clock; j++)
1711 {
1712 if (GraphBuffer[(i * clock) + j] == high)
1713 hithigh = 1;
1714 else if (GraphBuffer[(i * clock) + j] == low)
1715 hitlow = 1;
1716
1717 /* it doesn't count if it's the first part of our read
1718 because it's really just trailing from the last sequence */
1719 if (first && (hithigh || hitlow))
1720 hithigh = hitlow = 0;
1721 else
1722 first = 0;
1723
1724 if (hithigh && hitlow)
1725 break;
1726 }
1727
1728 /* If we didn't hit both high and low peaks, we had a bit transition */
1729 if (!hithigh || !hitlow)
1730 bit ^= 1;
1731
1732 BitStream[bit2idx++] = bit ^ invert;
1733 }
1734 }
1735
1736 /* standard 1/0 bitstream */
1737 else
1738 {
1739
1740 /* Then detect duration between 2 successive transitions */
1741 for (bitidx = 1; i < GraphTraceLen; i++)
1742 {
1743 if (GraphBuffer[i-1] != GraphBuffer[i])
1744 {
b3b70669 1745 lc = i-lastval;
1746 lastval = i;
1747
1748 // Error check: if bitidx becomes too large, we do not
1749 // have a Manchester encoded bitstream or the clock is really
1750 // wrong!
1751 if (bitidx > (GraphTraceLen*2/clock+8) ) {
1752 PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
1753 return 0;
1754 }
1755 // Then switch depending on lc length:
1756 // Tolerance is 1/4 of clock rate (arbitrary)
1757 if (abs(lc-clock/2) < tolerance) {
1758 // Short pulse : either "1" or "0"
1759 BitStream[bitidx++]=GraphBuffer[i-1];
1760 } else if (abs(lc-clock) < tolerance) {
1761 // Long pulse: either "11" or "00"
1762 BitStream[bitidx++]=GraphBuffer[i-1];
1763 BitStream[bitidx++]=GraphBuffer[i-1];
1764 } else {
7fe9b0b7 1765 // Error
1766 warnings++;
b3b70669 1767 PrintAndLog("Warning: Manchester decode error for pulse width detection.");
1768 PrintAndLog("(too many of those messages mean either the stream is not Manchester encoded, or clock is wrong)");
7fe9b0b7 1769
1770 if (warnings > 10)
1771 {
1772 PrintAndLog("Error: too many detection errors, aborting.");
1773 return 0;
1774 }
1775 }
1776 }
1777 }
1778
1779 // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
1780 // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
1781 // to stop output at the final bitidx2 value, not bitidx
1782 for (i = 0; i < bitidx; i += 2) {
1783 if ((BitStream[i] == 0) && (BitStream[i+1] == 1)) {
1784 BitStream[bit2idx++] = 1 ^ invert;
b3b70669 1785 } else if ((BitStream[i] == 1) && (BitStream[i+1] == 0)) {
1786 BitStream[bit2idx++] = 0 ^ invert;
1787 } else {
1788 // We cannot end up in this state, this means we are unsynchronized,
1789 // move up 1 bit:
1790 i++;
7fe9b0b7 1791 warnings++;
b3b70669 1792 PrintAndLog("Unsynchronized, resync...");
1793 PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
7fe9b0b7 1794
1795 if (warnings > 10)
1796 {
1797 PrintAndLog("Error: too many decode errors, aborting.");
1798 return 0;
1799 }
1800 }
1801 }
1802 }
1803
1804 PrintAndLog("Manchester decoded bitstream");
1805 // Now output the bitstream to the scrollback by line of 16 bits
1806 for (i = 0; i < (bit2idx-16); i+=16) {
1807 PrintAndLog("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i %i",
1808 BitStream[i],
1809 BitStream[i+1],
1810 BitStream[i+2],
1811 BitStream[i+3],
1812 BitStream[i+4],
1813 BitStream[i+5],
1814 BitStream[i+6],
1815 BitStream[i+7],
1816 BitStream[i+8],
1817 BitStream[i+9],
1818 BitStream[i+10],
1819 BitStream[i+11],
1820 BitStream[i+12],
1821 BitStream[i+13],
1822 BitStream[i+14],
1823 BitStream[i+15]);
1824 }
1825 return 0;
1826}
1827
1828/* Modulate our data into manchester */
1829int CmdManchesterMod(const char *Cmd)
1830{
1831 int i, j;
1832 int clock;
1833 int bit, lastbit, wave;
1834
1835 /* Get our clock */
1836 clock = GetClock(Cmd, 0, 1);
1837
1838 wave = 0;
1839 lastbit = 1;
1840 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
1841 {
1842 bit = GraphBuffer[i * clock] ^ 1;
1843
1844 for (j = 0; j < (int)(clock/2); j++)
1845 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave;
1846 for (j = (int)(clock/2); j < clock; j++)
1847 GraphBuffer[(i * clock) + j] = bit ^ lastbit ^ wave ^ 1;
1848
1849 /* Keep track of how we start our wave and if we changed or not this time */
1850 wave ^= bit ^ lastbit;
1851 lastbit = bit;
1852 }
1853
1854 RepaintGraphWindow();
1855 return 0;
1856}
1857
1858int CmdNorm(const char *Cmd)
1859{
1860 int i;
1861 int max = INT_MIN, min = INT_MAX;
1862
1863 for (i = 10; i < GraphTraceLen; ++i) {
1864 if (GraphBuffer[i] > max)
1865 max = GraphBuffer[i];
1866 if (GraphBuffer[i] < min)
1867 min = GraphBuffer[i];
1868 }
1869
1870 if (max != min) {
1871 for (i = 0; i < GraphTraceLen; ++i) {
ba1a299c 1872 GraphBuffer[i] = (GraphBuffer[i] - ((max + min) / 2)) * 256 /
7fe9b0b7 1873 (max - min);
ba1a299c 1874 //marshmelow: adjusted *1000 to *256 to make +/- 128 so demod commands still work
7fe9b0b7 1875 }
1876 }
1877 RepaintGraphWindow();
1878 return 0;
1879}
1880
1881int CmdPlot(const char *Cmd)
1882{
1883 ShowGraphWindow();
1884 return 0;
1885}
1886
1887int CmdSave(const char *Cmd)
1888{
ec75f5c1 1889 char filename[FILE_PATH_SIZE] = {0x00};
1890 int len = 0;
b915fda3 1891
ec75f5c1 1892 len = strlen(Cmd);
1893 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
1894 memcpy(filename, Cmd, len);
b915fda3 1895
1896
1897 FILE *f = fopen(filename, "w");
7fe9b0b7 1898 if(!f) {
b915fda3 1899 PrintAndLog("couldn't open '%s'", filename);
7fe9b0b7 1900 return 0;
1901 }
1902 int i;
1903 for (i = 0; i < GraphTraceLen; i++) {
1904 fprintf(f, "%d\n", GraphBuffer[i]);
1905 }
1906 fclose(f);
1907 PrintAndLog("saved to '%s'", Cmd);
1908 return 0;
1909}
1910
1911int CmdScale(const char *Cmd)
1912{
1913 CursorScaleFactor = atoi(Cmd);
1914 if (CursorScaleFactor == 0) {
1915 PrintAndLog("bad, can't have zero scale");
1916 CursorScaleFactor = 1;
1917 }
1918 RepaintGraphWindow();
1919 return 0;
1920}
1921
1922int CmdThreshold(const char *Cmd)
1923{
1924 int threshold = atoi(Cmd);
1925
1926 for (int i = 0; i < GraphTraceLen; ++i) {
1927 if (GraphBuffer[i] >= threshold)
1928 GraphBuffer[i] = 1;
1929 else
7bb9d33e 1930 GraphBuffer[i] = -1;
7fe9b0b7 1931 }
1932 RepaintGraphWindow();
1933 return 0;
1934}
1935
d51b2eda
MHS
1936int CmdDirectionalThreshold(const char *Cmd)
1937{
1938 int8_t upThres = param_get8(Cmd, 0);
1939 int8_t downThres = param_get8(Cmd, 1);
ba1a299c 1940
d51b2eda 1941 printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres);
952a8bb5 1942
d51b2eda
MHS
1943 int lastValue = GraphBuffer[0];
1944 GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
952a8bb5 1945
d51b2eda
MHS
1946 for (int i = 1; i < GraphTraceLen; ++i) {
1947 // Apply first threshold to samples heading up
1948 if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue)
1949 {
1950 lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it.
1951 GraphBuffer[i] = 1;
1952 }
1953 // Apply second threshold to samples heading down
1954 else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue)
1955 {
1956 lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it.
1957 GraphBuffer[i] = -1;
1958 }
1959 else
1960 {
1961 lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it.
1962 GraphBuffer[i] = GraphBuffer[i-1];
1963
1964 }
1965 }
1966 GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample.
1967 RepaintGraphWindow();
1968 return 0;
1969}
1970
7fe9b0b7 1971int CmdZerocrossings(const char *Cmd)
1972{
1973 // Zero-crossings aren't meaningful unless the signal is zero-mean.
1974 CmdHpf("");
1975
1976 int sign = 1;
1977 int zc = 0;
1978 int lastZc = 0;
1979
1980 for (int i = 0; i < GraphTraceLen; ++i) {
1981 if (GraphBuffer[i] * sign >= 0) {
1982 // No change in sign, reproduce the previous sample count.
1983 zc++;
1984 GraphBuffer[i] = lastZc;
1985 } else {
1986 // Change in sign, reset the sample count.
1987 sign = -sign;
1988 GraphBuffer[i] = lastZc;
1989 if (sign > 0) {
1990 lastZc = zc;
1991 zc = 0;
1992 }
1993 }
1994 }
1995
1996 RepaintGraphWindow();
1997 return 0;
1998}
1999
ba1a299c 2000static command_t CommandTable[] =
7fe9b0b7 2001{
2002 {"help", CmdHelp, 1, "This help"},
2003 {"amp", CmdAmp, 1, "Amplify peaks"},
57c69556 2004 {"askdemod", Cmdaskdemod, 1, "<0 or 1> -- Attempt to demodulate simple ASK tags"},
1e090a61 2005 {"askmandemod", Cmdaskmandemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK/Manchester tags and output binary (args optional)"},
2006 {"askrawdemod", Cmdaskrawdemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate ASK tags and output bin (args optional)"},
7fe9b0b7 2007 {"autocorr", CmdAutoCorr, 1, "<window length> -- Autocorrelation over window"},
1e090a61 2008 {"biphaserawdecode",CmdBiphaseDecodeRaw,1,"[offset] [invert<0|1>] Biphase decode bin stream in demod buffer (offset = 0|1 bits to shift the decode start)"},
7fe9b0b7 2009 {"bitsamples", CmdBitsamples, 0, "Get raw samples as bitstring"},
2010 {"bitstream", CmdBitstream, 1, "[clock rate] -- Convert waveform into a bitstream"},
2011 {"buffclear", CmdBuffClear, 1, "Clear sample buffer and graph window"},
2012 {"dec", CmdDec, 1, "Decimate samples"},
ba1a299c 2013 {"detectclock", CmdDetectClockRate, 1, "Detect ASK clock rate"},
e888ed8e 2014 {"fskdemod", CmdFSKdemod, 1, "Demodulate graph window as a HID FSK"},
1e090a61 2015 {"fskawiddemod", CmdFSKdemodAWID, 1, "Demodulate graph window as an AWID FSK tag using raw"},
2016 {"fskfcdetect", CmdFSKfcDetect, 1, "Try to detect the Field Clock of an FSK wave"},
2017 {"fskhiddemod", CmdFSKdemodHID, 1, "Demodulate graph window as a HID FSK tag using raw"},
2018 {"fskiodemod", CmdFSKdemodIO, 1, "Demodulate graph window as an IO Prox tag FSK using raw"},
2019 {"fskpyramiddemod",CmdFSKdemodPyramid,1, "Demodulate graph window as a Pyramid FSK tag using raw"},
ec75f5c1 2020 {"fskparadoxdemod",CmdFSKdemodParadox,1, "Demodulate graph window as a Paradox FSK tag using raw"},
1e090a61 2021 {"fskrawdemod", CmdFSKrawdemod, 1, "[clock rate] [invert] [rchigh] [rclow] Demodulate graph window from FSK to bin (clock = 50)(invert = 1|0)(rchigh = 10)(rclow=8)"},
7fe9b0b7 2022 {"grid", CmdGrid, 1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},
ba1a299c 2023 {"hexsamples", CmdHexsamples, 0, "<bytes> [<offset>] -- Dump big buffer as hex bytes"},
7fe9b0b7 2024 {"hide", CmdHide, 1, "Hide graph window"},
2025 {"hpf", CmdHpf, 1, "Remove DC offset from trace"},
7fe9b0b7 2026 {"load", CmdLoad, 1, "<filename> -- Load trace (to graph window"},
2027 {"ltrim", CmdLtrim, 1, "<samples> -- Trim samples from left of trace"},
9ec1416a 2028 {"rtrim", CmdRtrim, 1, "<location to end trace> -- Trim samples from right of trace"},
7fe9b0b7 2029 {"mandemod", CmdManchesterDemod, 1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},
eb191de6 2030 {"manrawdecode", Cmdmandecoderaw, 1, "Manchester decode binary stream already in graph buffer"},
7fe9b0b7 2031 {"manmod", CmdManchesterMod, 1, "[clock rate] -- Manchester modulate a binary stream"},
ba1a299c 2032 {"norm", CmdNorm, 1, "Normalize max/min to +/-128"},
7ddb9900 2033 {"plot", CmdPlot, 1, "Show graph window (hit 'h' in window for keystroke help)"},
ba1a299c 2034 {"pskclean", CmdPskClean, 1, "Attempt to clean psk wave"},
2035 {"pskdetectclock",CmdDetectNRZpskClockRate, 1, "Detect ASK, PSK, or NRZ clock rate"},
04d2721b 2036 {"pskindalademod",CmdIndalaDecode, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk1 indala tags and output ID binary & hex (args optional)"},
2037 {"psk1nrzrawdemod",CmdpskNRZrawDemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk1 or nrz tags and output binary (args optional)"},
2038 {"psk2rawdemod", CmdPSK2rawDemod, 1, "[clock] [invert<0|1>] -- Attempt to demodulate psk2 tags and output binary (args optional)"},
90d74dc2 2039 {"samples", CmdSamples, 0, "[512 - 40000] -- Get raw samples for graph window"},
7fe9b0b7 2040 {"save", CmdSave, 1, "<filename> -- Save trace (from graph window)"},
2041 {"scale", CmdScale, 1, "<int> -- Set cursor display scale"},
ec75f5c1 2042 {"setdebugmode", CmdSetDebugMode, 1, "<0|1> -- Turn on or off Debugging Mode for demods"},
2043 {"shiftgraphzero",CmdGraphShiftZero, 1, "<shift> -- Shift 0 for Graphed wave + or - shift value"},
dbf444a1 2044 {"threshold", CmdThreshold, 1, "<threshold> -- Maximize/minimize every value in the graph window depending on threshold"},
ba1a299c 2045 {"dirthreshold", CmdDirectionalThreshold, 1, "<thres up> <thres down> -- Max rising higher up-thres/ Min falling lower down-thres, keep rest as prev."},
2046 {"tune", CmdTuneSamples, 0, "Get hw tune samples for graph window"},
7fe9b0b7 2047 {"zerocrossings", CmdZerocrossings, 1, "Count time between zero-crossings"},
2048 {NULL, NULL, 0, NULL}
2049};
2050
2051int CmdData(const char *Cmd)
2052{
2053 CmdsParse(CommandTable, Cmd);
2054 return 0;
2055}
2056
2057int CmdHelp(const char *Cmd)
2058{
2059 CmdsHelp(CommandTable);
2060 return 0;
2061}
Impressum, Datenschutz