1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Low frequency EM4x commands
9 //-----------------------------------------------------------------------------
14 #include "cmdlfem4x.h"
15 #include "proxmark3.h"
20 #include "cmdparser.h"
25 #include "protocols.h"
27 uint64_t g_em410xId
=0;
29 static int CmdHelp(const char *Cmd
);
31 int CmdEMdemodASK(const char *Cmd
)
33 char cmdp
= param_getchar(Cmd
, 0);
34 int findone
= (cmdp
== '1') ? 1 : 0;
35 UsbCommand c
={CMD_EM410X_DEMOD
};
42 //print 64 bit EM410x ID in multiple formats
43 void printEM410x(uint32_t hi
, uint64_t id
)
50 for (ii
=5; ii
>0;ii
--){
52 id2lo
=(id2lo
<<1LL) | ((id
& (iii
<< (i
+((ii
-1)*8)))) >> (i
+((ii
-1)*8)));
57 PrintAndLog("\nEM TAG ID : %06X%016" PRIX64
, hi
, id
);
60 PrintAndLog("\nEM TAG ID : %010" PRIX64
, id
);
61 PrintAndLog("\nPossible de-scramble patterns");
62 PrintAndLog("Unique TAG ID : %010" PRIX64
, id2lo
);
63 PrintAndLog("HoneyWell IdentKey {");
64 PrintAndLog("DEZ 8 : %08" PRIu64
,id
& 0xFFFFFF);
65 PrintAndLog("DEZ 10 : %010" PRIu64
,id
& 0xFFFFFFFF);
66 PrintAndLog("DEZ 5.5 : %05lld.%05" PRIu64
,(id
>>16LL) & 0xFFFF,(id
& 0xFFFF));
67 PrintAndLog("DEZ 3.5A : %03lld.%05" PRIu64
,(id
>>32ll),(id
& 0xFFFF));
68 PrintAndLog("DEZ 3.5B : %03lld.%05" PRIu64
,(id
& 0xFF000000) >> 24,(id
& 0xFFFF));
69 PrintAndLog("DEZ 3.5C : %03lld.%05" PRIu64
,(id
& 0xFF0000) >> 16,(id
& 0xFFFF));
70 PrintAndLog("DEZ 14/IK2 : %014" PRIu64
,id
);
71 PrintAndLog("DEZ 15/IK3 : %015" PRIu64
,id2lo
);
72 PrintAndLog("DEZ 20/ZK : %02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
"%02" PRIu64
,
73 (id2lo
& 0xf000000000) >> 36,
74 (id2lo
& 0x0f00000000) >> 32,
75 (id2lo
& 0x00f0000000) >> 28,
76 (id2lo
& 0x000f000000) >> 24,
77 (id2lo
& 0x0000f00000) >> 20,
78 (id2lo
& 0x00000f0000) >> 16,
79 (id2lo
& 0x000000f000) >> 12,
80 (id2lo
& 0x0000000f00) >> 8,
81 (id2lo
& 0x00000000f0) >> 4,
82 (id2lo
& 0x000000000f)
84 uint64_t paxton
= (((id
>>32) << 24) | (id
& 0xffffff)) + 0x143e00;
85 PrintAndLog("}\nOther : %05" PRIu64
"_%03" PRIu64
"_%08" PRIu64
"",(id
&0xFFFF),((id
>>16LL) & 0xFF),(id
& 0xFFFFFF));
86 PrintAndLog("Pattern Paxton : %" PRIu64
" [0x%" PRIX64
"]", paxton
, paxton
);
88 uint32_t p1id
= (id
& 0xFFFFFF);
89 uint8_t arr
[32] = {0x00};
92 for (; i
< 24; ++i
, --j
){
93 arr
[i
] = (p1id
>> i
) & 1;
127 PrintAndLog("Pattern 1 : %d [0x%X]", p1
, p1
);
129 uint16_t sebury1
= id
& 0xFFFF;
130 uint8_t sebury2
= (id
>> 16) & 0x7F;
131 uint32_t sebury3
= id
& 0x7FFFFF;
132 PrintAndLog("Pattern Sebury : %d %d %d [0x%X 0x%X 0x%X]", sebury1
, sebury2
, sebury3
, sebury1
, sebury2
, sebury3
);
138 /* Read the ID of an EM410x tag.
140 * 1111 1111 1 <-- standard non-repeatable header
141 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
143 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
144 * 0 <-- stop bit, end of tag
146 int AskEm410xDecode(bool verbose
, uint32_t *hi
, uint64_t *lo
)
149 uint8_t BitStream
[512]={0};
150 size_t BitLen
= sizeof(BitStream
);
151 if ( !getDemodBuf(BitStream
, &BitLen
) ) return 0;
153 if (Em410xDecode(BitStream
, &BitLen
, &idx
, hi
, lo
)) {
154 //set GraphBuffer for clone or sim command
155 setDemodBuf(DemodBuffer
, (BitLen
==40) ? 64 : 128, idx
+1);
156 setClockGrid(g_DemodClock
, g_DemodStartIdx
+ ((idx
+1)*g_DemodClock
));
159 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, BitLen
);
163 PrintAndLog("EM410x pattern found: ");
164 printEM410x(*hi
, *lo
);
172 //askdemod then call Em410xdecode
173 int AskEm410xDemod(const char *Cmd
, uint32_t *hi
, uint64_t *lo
, bool verbose
)
176 if (!ASKDemod_ext(Cmd
, false, false, 1, &st
)) return 0;
177 return AskEm410xDecode(verbose
, hi
, lo
);
181 //takes 3 arguments - clock, invert and maxErr as integers
182 //attempts to demodulate ask while decoding manchester
183 //prints binary found and saves in graphbuffer for further commands
184 int CmdAskEM410xDemod(const char *Cmd
)
186 char cmdp
= param_getchar(Cmd
, 0);
187 if (strlen(Cmd
) > 10 || cmdp
== 'h' || cmdp
== 'H') {
188 PrintAndLog("Usage: lf em 410xdemod [clock] <0|1> [maxError]");
189 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
190 PrintAndLog(" <invert>, 1 for invert output");
191 PrintAndLog(" [set maximum allowed errors], default = 100.");
193 PrintAndLog(" sample: lf em 410xdemod = demod an EM410x Tag ID from GraphBuffer");
194 PrintAndLog(" : lf em 410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32");
195 PrintAndLog(" : lf em 410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data");
196 PrintAndLog(" : lf em 410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data");
197 PrintAndLog(" : lf em 410xdemod 64 1 0 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/64 and inverting data and allowing 0 demod errors");
202 return AskEm410xDemod(Cmd
, &hi
, &lo
, true);
205 int usage_lf_em410x_sim(void) {
206 PrintAndLog("Simulating EM410x tag");
208 PrintAndLog("Usage: lf em 410xsim [h] <uid> <clock>");
209 PrintAndLog("Options:");
210 PrintAndLog(" h - this help");
211 PrintAndLog(" uid - uid (10 HEX symbols)");
212 PrintAndLog(" clock - clock (32|64) (optional)");
213 PrintAndLog("samples:");
214 PrintAndLog(" lf em 410xsim 0F0368568B");
215 PrintAndLog(" lf em 410xsim 0F0368568B 32");
219 // emulate an EM410X tag
220 int CmdEM410xSim(const char *Cmd
)
222 int i
, n
, j
, binary
[4], parity
[4];
224 char cmdp
= param_getchar(Cmd
, 0);
225 uint8_t uid
[5] = {0x00};
227 if (cmdp
== 'h' || cmdp
== 'H') return usage_lf_em410x_sim();
228 /* clock is 64 in EM410x tags */
231 if (param_gethex(Cmd
, 0, uid
, 10)) {
232 PrintAndLog("UID must include 10 HEX symbols");
235 param_getdec(Cmd
,1, &clock
);
237 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X clock: %d", uid
[0],uid
[1],uid
[2],uid
[3],uid
[4],clock
);
238 PrintAndLog("Press pm3-button to about simulation");
241 /* clear our graph */
244 /* write 9 start bits */
245 for (i
= 0; i
< 9; i
++)
246 AppendGraph(0, clock
, 1);
248 /* for each hex char */
249 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
250 for (i
= 0; i
< 10; i
++)
252 /* read each hex char */
253 sscanf(&Cmd
[i
], "%1x", &n
);
254 for (j
= 3; j
>= 0; j
--, n
/= 2)
257 /* append each bit */
258 AppendGraph(0, clock
, binary
[0]);
259 AppendGraph(0, clock
, binary
[1]);
260 AppendGraph(0, clock
, binary
[2]);
261 AppendGraph(0, clock
, binary
[3]);
263 /* append parity bit */
264 AppendGraph(0, clock
, binary
[0] ^ binary
[1] ^ binary
[2] ^ binary
[3]);
266 /* keep track of column parity */
267 parity
[0] ^= binary
[0];
268 parity
[1] ^= binary
[1];
269 parity
[2] ^= binary
[2];
270 parity
[3] ^= binary
[3];
274 AppendGraph(0, clock
, parity
[0]);
275 AppendGraph(0, clock
, parity
[1]);
276 AppendGraph(0, clock
, parity
[2]);
277 AppendGraph(0, clock
, parity
[3]);
280 AppendGraph(1, clock
, 0);
282 CmdLFSim("0"); //240 start_gap.
286 /* Function is equivalent of lf read + data samples + em410xread
287 * looped until an EM410x tag is detected
289 * Why is CmdSamples("16000")?
290 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
291 * rate gets lower, then grow the number of samples
292 * Changed by martin, 4000 x 4 = 16000,
293 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
295 * EDIT -- capture enough to get 2 complete preambles at the slowest data rate known to be used (rf/64) (64*64*2+9 = 8201) marshmellow
297 int CmdEM410xWatch(const char *Cmd
)
301 printf("\naborted via keyboard!\n");
305 } while (!CmdAskEM410xDemod(""));
310 //currently only supports manchester modulations
311 int CmdEM410xWatchnSpoof(const char *Cmd
)
314 PrintAndLog("# Replaying captured ID: %010"PRIx64
, g_em410xId
);
319 int CmdEM410xWrite(const char *Cmd
)
321 uint64_t id
= 0xFFFFFFFFFFFFFFFF; // invalid id value
322 int card
= 0xFF; // invalid card value
323 unsigned int clock
= 0; // invalid clock value
325 sscanf(Cmd
, "%" SCNx64
" %d %d", &id
, &card
, &clock
);
328 if (id
== 0xFFFFFFFFFFFFFFFF) {
329 PrintAndLog("Error! ID is required.\n");
332 if (id
>= 0x10000000000) {
333 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
339 PrintAndLog("Error! Card type required.\n");
343 PrintAndLog("Error! Bad card type selected.\n");
352 // Allowed clock rates: 16, 32, 40 and 64
353 if ((clock
!= 16) && (clock
!= 32) && (clock
!= 64) && (clock
!= 40)) {
354 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32, 40 and 64.\n", clock
);
359 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
" (clock rate: %d)", "T55x7", id
, clock
);
360 // NOTE: We really should pass the clock in as a separate argument, but to
361 // provide for backwards-compatibility for older firmware, and to avoid
362 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
363 // the clock rate in bits 8-15 of the card value
364 card
= (card
& 0xFF) | ((clock
<< 8) & 0xFF00);
365 } else if (card
== 0) {
366 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
, "T5555", id
, clock
);
367 card
= (card
& 0xFF) | ((clock
<< 8) & 0xFF00);
369 PrintAndLog("Error! Bad card type selected.\n");
373 UsbCommand c
= {CMD_EM410X_WRITE_TAG
, {card
, (uint32_t)(id
>> 32), (uint32_t)id
}};
379 //**************** Start of EM4x50 Code ************************
380 bool EM_EndParityTest(uint8_t *BitStream
, size_t size
, uint8_t rows
, uint8_t cols
, uint8_t pType
)
382 if (rows
*cols
>size
) return false;
384 //assume last col is a parity and do not test
385 for (uint8_t colNum
= 0; colNum
< cols
-1; colNum
++) {
386 for (uint8_t rowNum
= 0; rowNum
< rows
; rowNum
++) {
387 colP
^= BitStream
[(rowNum
*cols
)+colNum
];
389 if (colP
!= pType
) return false;
394 bool EM_ByteParityTest(uint8_t *BitStream
, size_t size
, uint8_t rows
, uint8_t cols
, uint8_t pType
)
396 if (rows
*cols
>size
) return false;
398 //assume last row is a parity row and do not test
399 for (uint8_t rowNum
= 0; rowNum
< rows
-1; rowNum
++) {
400 for (uint8_t colNum
= 0; colNum
< cols
; colNum
++) {
401 rowP
^= BitStream
[(rowNum
*cols
)+colNum
];
403 if (rowP
!= pType
) return false;
408 uint32_t OutputEM4x50_Block(uint8_t *BitStream
, size_t size
, bool verbose
, bool pTest
)
410 if (size
<45) return 0;
411 uint32_t code
= bytebits_to_byte(BitStream
,8);
412 code
= code
<<8 | bytebits_to_byte(BitStream
+9,8);
413 code
= code
<<8 | bytebits_to_byte(BitStream
+18,8);
414 code
= code
<<8 | bytebits_to_byte(BitStream
+27,8);
415 if (verbose
|| g_debugMode
){
416 for (uint8_t i
= 0; i
<5; i
++){
417 if (i
== 4) PrintAndLog(""); //parity byte spacer
418 PrintAndLog("%d%d%d%d%d%d%d%d %d -> 0x%02x",
428 bytebits_to_byte(BitStream
+i
*9,8)
432 PrintAndLog("Parity Passed");
434 PrintAndLog("Parity Failed");
438 /* Read the transmitted data of an EM4x50 tag from the graphbuffer
441 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
442 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
443 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
444 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
445 * CCCCCCCC <- column parity bits
447 * LW <- Listen Window
449 * This pattern repeats for every block of data being transmitted.
450 * Transmission starts with two Listen Windows (LW - a modulated
451 * pattern of 320 cycles each (32/32/128/64/64)).
453 * Note that this data may or may not be the UID. It is whatever data
454 * is stored in the blocks defined in the control word First and Last
455 * Word Read values. UID is stored in block 32.
457 //completed by Marshmellow
458 int EM4x50Read(const char *Cmd
, bool verbose
)
460 uint8_t fndClk
[] = {8,16,32,40,50,64,128};
464 int i
, j
, startblock
, skip
, block
, start
, end
, low
, high
, minClk
;
465 bool complete
= false;
466 int tmpbuff
[MAX_GRAPH_TRACE_LEN
/ 64];
472 memset(tmpbuff
, 0, MAX_GRAPH_TRACE_LEN
/ 64);
474 // get user entry if any
475 sscanf(Cmd
, "%i %i", &clk
, &invert
);
477 // save GraphBuffer - to restore it later
480 // first get high and low values
481 for (i
= 0; i
< GraphTraceLen
; i
++) {
482 if (GraphBuffer
[i
] > high
)
483 high
= GraphBuffer
[i
];
484 else if (GraphBuffer
[i
] < low
)
485 low
= GraphBuffer
[i
];
491 // get to first full low to prime loop and skip incomplete first pulse
492 while ((GraphBuffer
[i
] < high
) && (i
< GraphTraceLen
))
494 while ((GraphBuffer
[i
] > low
) && (i
< GraphTraceLen
))
498 // populate tmpbuff buffer with pulse lengths
499 while (i
< GraphTraceLen
) {
500 // measure from low to low
501 while ((GraphBuffer
[i
] > low
) && (i
< GraphTraceLen
))
504 while ((GraphBuffer
[i
] < high
) && (i
< GraphTraceLen
))
506 while ((GraphBuffer
[i
] > low
) && (i
< GraphTraceLen
))
508 if (j
>=(MAX_GRAPH_TRACE_LEN
/64)) {
511 tmpbuff
[j
++]= i
- start
;
512 if (i
-start
< minClk
&& i
< GraphTraceLen
) {
518 for (uint8_t clkCnt
= 0; clkCnt
<7; clkCnt
++) {
519 tol
= fndClk
[clkCnt
]/8;
520 if (minClk
>= fndClk
[clkCnt
]-tol
&& minClk
<= fndClk
[clkCnt
]+1) {
528 // look for data start - should be 2 pairs of LW (pulses of clk*3,clk*2)
530 for (i
= 0; i
< j
- 4 ; ++i
) {
532 if (tmpbuff
[i
] >= clk
*3-tol
&& tmpbuff
[i
] <= clk
*3+tol
) //3 clocks
533 if (tmpbuff
[i
+1] >= clk
*2-tol
&& tmpbuff
[i
+1] <= clk
*2+tol
) //2 clocks
534 if (tmpbuff
[i
+2] >= clk
*3-tol
&& tmpbuff
[i
+2] <= clk
*3+tol
) //3 clocks
535 if (tmpbuff
[i
+3] >= clk
-tol
) //1.5 to 2 clocks - depends on bit following
543 // skip over the remainder of LW
544 skip
+= tmpbuff
[i
+1] + tmpbuff
[i
+2] + clk
;
545 if (tmpbuff
[i
+3]>clk
)
546 phaseoff
= tmpbuff
[i
+3]-clk
;
549 // now do it again to find the end
551 for (i
+= 3; i
< j
- 4 ; ++i
) {
553 if (tmpbuff
[i
] >= clk
*3-tol
&& tmpbuff
[i
] <= clk
*3+tol
) //3 clocks
554 if (tmpbuff
[i
+1] >= clk
*2-tol
&& tmpbuff
[i
+1] <= clk
*2+tol
) //2 clocks
555 if (tmpbuff
[i
+2] >= clk
*3-tol
&& tmpbuff
[i
+2] <= clk
*3+tol
) //3 clocks
556 if (tmpbuff
[i
+3] >= clk
-tol
) //1.5 to 2 clocks - depends on bit following
564 if (verbose
|| g_debugMode
) {
566 PrintAndLog("\nNote: one block = 50 bits (32 data, 12 parity, 6 marker)");
568 PrintAndLog("No data found!, clock tried:%d",clk
);
569 PrintAndLog("Try again with more samples.");
570 PrintAndLog(" or after a 'data askedge' command to clean up the read");
573 } else if (start
< 0) return 0;
575 snprintf(tmp2
, sizeof(tmp2
),"%d %d 1000 %d", clk
, invert
, clk
*47);
576 // get rid of leading crap
577 snprintf(tmp
, sizeof(tmp
), "%i", skip
);
580 bool AllPTest
= true;
581 // now work through remaining buffer printing out data blocks
585 if (verbose
|| g_debugMode
) PrintAndLog("\nBlock %i:", block
);
588 // look for LW before start of next block
589 for ( ; i
< j
- 4 ; ++i
) {
591 if (tmpbuff
[i
] >= clk
*3-tol
&& tmpbuff
[i
] <= clk
*3+tol
)
592 if (tmpbuff
[i
+1] >= clk
-tol
)
595 if (i
>= j
-4) break; //next LW not found
597 if (tmpbuff
[i
+1]>clk
)
598 phaseoff
= tmpbuff
[i
+1]-clk
;
602 if (ASKDemod(tmp2
, false, false, 1) < 1) {
606 //set DemodBufferLen to just one block
607 DemodBufferLen
= skip
/clk
;
609 pTest
= EM_ByteParityTest(DemodBuffer
,DemodBufferLen
,5,9,0);
610 pTest
&= EM_EndParityTest(DemodBuffer
,DemodBufferLen
,5,9,0);
613 Code
[block
] = OutputEM4x50_Block(DemodBuffer
,DemodBufferLen
,verbose
, pTest
);
614 if (g_debugMode
) PrintAndLog("\nskipping %d samples, bits:%d", skip
, skip
/clk
);
615 //skip to start of next block
616 snprintf(tmp
,sizeof(tmp
),"%i",skip
);
619 if (i
>= end
) break; //in case chip doesn't output 6 blocks
622 if (verbose
|| g_debugMode
|| AllPTest
){
624 PrintAndLog("*** Warning!");
625 PrintAndLog("Partial data - no end found!");
626 PrintAndLog("Try again with more samples.");
628 PrintAndLog("Found data at sample: %i - using clock: %i", start
, clk
);
630 for (block
=0; block
< end
; block
++){
631 PrintAndLog("Block %d: %08x",block
,Code
[block
]);
634 PrintAndLog("Parities Passed");
636 PrintAndLog("Parities Failed");
637 PrintAndLog("Try cleaning the read samples with 'data askedge'");
641 //restore GraphBuffer
643 return (int)AllPTest
;
646 int CmdEM4x50Read(const char *Cmd
)
648 return EM4x50Read(Cmd
, true);
651 //**************** Start of EM4x05/EM4x69 Code ************************
652 int usage_lf_em_read(void) {
653 PrintAndLog("Read EM4x05/EM4x69. Tag must be on antenna. ");
655 PrintAndLog("Usage: lf em 4x05readword [h] <address> <pwd>");
656 PrintAndLog("Options:");
657 PrintAndLog(" h - this help");
658 PrintAndLog(" address - memory address to read. (0-15)");
659 PrintAndLog(" pwd - password (hex) (optional)");
660 PrintAndLog("samples:");
661 PrintAndLog(" lf em 4x05readword 1");
662 PrintAndLog(" lf em 4x05readword 1 11223344");
666 // for command responses from em4x05 or em4x69
667 // download samples from device and copy them to the Graphbuffer
668 bool downloadSamplesEM() {
669 // 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
671 GetFromBigBuf(got
, sizeof(got
), 0);
672 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 4000) ) {
673 PrintAndLog("command execution time out");
676 setGraphBuf(got
, sizeof(got
));
680 bool EM4x05testDemodReadData(uint32_t *word
, bool readCmd
) {
681 // em4x05/em4x69 command response preamble is 00001010
682 // skip first two 0 bits as they might have been missed in the demod
683 uint8_t preamble
[] = {0,0,1,0,1,0};
686 // set size to 20 to only test first 14 positions for the preamble or less if not a read command
687 size_t size
= (readCmd
) ? 20 : 11;
689 size
= (size
> DemodBufferLen
) ? DemodBufferLen
: size
;
691 if ( !preambleSearchEx(DemodBuffer
, preamble
, sizeof(preamble
), &size
, &startIdx
, true) ) {
692 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305 preamble not found :: %d", startIdx
);
695 // if this is a readword command, get the read bytes and test the parities
697 if (!EM_EndParityTest(DemodBuffer
+ startIdx
+ sizeof(preamble
), 45, 5, 9, 0)) {
698 if (g_debugMode
) PrintAndLog("DEBUG: Error - End Parity check failed");
701 // test for even parity bits and remove them. (leave out the end row of parities so 36 bits)
702 if ( removeParity(DemodBuffer
, startIdx
+ sizeof(preamble
),9,0,36) == 0 ) {
703 if (g_debugMode
) PrintAndLog("DEBUG: Error - Parity not detected");
707 setDemodBuf(DemodBuffer
, 32, 0);
710 *word
= bytebits_to_byteLSBF(DemodBuffer
, 32);
715 // FSK, PSK, ASK/MANCHESTER, ASK/BIPHASE, ASK/DIPHASE
716 // should cover 90% of known used configs
717 // the rest will need to be manually demoded for now...
718 int demodEM4x05resp(uint32_t *word
, bool readCmd
) {
721 // test for FSK wave (easiest to 99% ID)
722 if (GetFskClock("", false, false)) {
723 //valid fsk clocks found
724 ans
= FSKrawDemod("0 0", false);
726 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: FSK Demod failed, ans: %d", ans
);
728 if (EM4x05testDemodReadData(word
, readCmd
)) {
733 // PSK clocks should be easy to detect ( but difficult to demod a non-repeating pattern... )
734 ans
= GetPskClock("", false, false);
737 ans
= PSKDemod("0 0 6", false);
739 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans
);
741 if (EM4x05testDemodReadData(word
, readCmd
)) {
745 psk1TOpsk2(DemodBuffer
, DemodBufferLen
);
746 if (EM4x05testDemodReadData(word
, readCmd
)) {
751 ans
= PSKDemod("0 1 6", false);
753 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans
);
755 if (EM4x05testDemodReadData(word
, readCmd
)) {
759 psk1TOpsk2(DemodBuffer
, DemodBufferLen
);
760 if (EM4x05testDemodReadData(word
, readCmd
)) {
768 // manchester is more common than biphase... try first
769 bool stcheck
= false;
770 // try manchester - NOTE: ST only applies to T55x7 tags.
771 ans
= ASKDemod_ext("0,0,1", false, false, 1, &stcheck
);
773 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: ASK/Manchester Demod failed, ans: %d", ans
);
775 if (EM4x05testDemodReadData(word
, readCmd
)) {
781 ans
= ASKbiphaseDemod("0 0 1", false);
783 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans
);
785 if (EM4x05testDemodReadData(word
, readCmd
)) {
790 //try diphase (differential biphase or inverted)
791 ans
= ASKbiphaseDemod("0 1 1", false);
793 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans
);
795 if (EM4x05testDemodReadData(word
, readCmd
)) {
803 int EM4x05ReadWord_ext(uint8_t addr
, uint32_t pwd
, bool usePwd
, uint32_t *wordData
) {
804 UsbCommand c
= {CMD_EM4X_READ_WORD
, {addr
, pwd
, usePwd
}};
805 clearCommandBuffer();
808 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2500)){
809 PrintAndLog("Command timed out");
812 if ( !downloadSamplesEM() ) {
815 int testLen
= (GraphTraceLen
< 1000) ? GraphTraceLen
: 1000;
816 if (graphJustNoise(GraphBuffer
, testLen
)) {
817 PrintAndLog("no tag not found");
821 return demodEM4x05resp(wordData
, true);
824 int EM4x05ReadWord(uint8_t addr
, uint32_t pwd
, bool usePwd
) {
825 uint32_t wordData
= 0;
826 int success
= EM4x05ReadWord_ext(addr
, pwd
, usePwd
, &wordData
);
828 PrintAndLog("%s Address %02d | %08X", (addr
>13) ? "Lock":" Got",addr
,wordData
);
830 PrintAndLog("Read Address %02d | failed",addr
);
835 int CmdEM4x05ReadWord(const char *Cmd
) {
839 uint8_t ctmp
= param_getchar(Cmd
, 0);
840 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_read();
842 addr
= param_get8ex(Cmd
, 0, 50, 10);
843 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
844 pwd
= param_get32ex(Cmd
, 1, 1, 16);
847 PrintAndLog("Address must be between 0 and 15");
851 PrintAndLog("Reading address %02u", addr
);
854 PrintAndLog("Reading address %02u | password %08X", addr
, pwd
);
857 return EM4x05ReadWord(addr
, pwd
, usePwd
);
860 int usage_lf_em_dump(void) {
861 PrintAndLog("Dump EM4x05/EM4x69. Tag must be on antenna. ");
863 PrintAndLog("Usage: lf em 4x05dump [h] <pwd>");
864 PrintAndLog("Options:");
865 PrintAndLog(" h - this help");
866 PrintAndLog(" pwd - password (hex) (optional)");
867 PrintAndLog("samples:");
868 PrintAndLog(" lf em 4x05dump");
869 PrintAndLog(" lf em 4x05dump 11223344");
873 int CmdEM4x05dump(const char *Cmd
) {
877 uint8_t ctmp
= param_getchar(Cmd
, 0);
878 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_dump();
880 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
881 pwd
= param_get32ex(Cmd
, 0, 1, 16);
887 for (; addr
< 16; addr
++) {
890 PrintAndLog(" PWD Address %02u | %08X",addr
,pwd
);
892 PrintAndLog(" PWD Address 02 | cannot read");
895 success
&= EM4x05ReadWord(addr
, pwd
, usePwd
);
903 int usage_lf_em_write(void) {
904 PrintAndLog("Write EM4x05/EM4x69. Tag must be on antenna. ");
906 PrintAndLog("Usage: lf em 4x05writeword [h] a <address> d <data> p <pwd> [s] [i]");
907 PrintAndLog("Options:");
908 PrintAndLog(" h - this help");
909 PrintAndLog(" a <address> - memory address to write to. (0-15)");
910 PrintAndLog(" d <data> - data to write (hex)");
911 PrintAndLog(" p <pwd> - password (hex) (optional)");
912 PrintAndLog(" s - swap the data bit order before write");
913 PrintAndLog(" i - invert the data bits before write");
914 PrintAndLog("samples:");
915 PrintAndLog(" lf em 4x05writeword a 5 d 11223344");
916 PrintAndLog(" lf em 4x05writeword a 5 p deadc0de d 11223344 s i");
920 // note: em4x05 doesn't have a way to invert data output so we must invert the data prior to writing
921 // it if invertion is needed. (example FSK2a vs FSK)
922 // also em4x05 requires swapping word data when compared to the data used for t55xx chips.
923 int EM4x05WriteWord(uint8_t addr
, uint32_t data
, uint32_t pwd
, bool usePwd
, bool swap
, bool invert
) {
924 if (swap
) data
= SwapBits(data
, 32);
926 if (invert
) data
^= 0xFFFFFFFF;
929 PrintAndLog("Address must be between 0 and 15");
933 PrintAndLog("Writing address %d data %08X", addr
, data
);
935 PrintAndLog("Writing address %d data %08X using password %08X", addr
, data
, pwd
);
938 uint16_t flag
= (addr
<< 8 ) | usePwd
;
940 UsbCommand c
= {CMD_EM4X_WRITE_WORD
, {flag
, data
, pwd
}};
941 clearCommandBuffer();
944 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)){
945 PrintAndLog("Error occurred, device did not respond during write operation.");
948 if ( !downloadSamplesEM() ) {
951 //check response for 00001010 for write confirmation!
954 int result
= demodEM4x05resp(&dummy
,false);
956 PrintAndLog("Write Verified");
958 PrintAndLog("Write could not be verified");
963 int CmdEM4x05WriteWord(const char *Cmd
) {
966 uint32_t data
= 0xFFFFFFFF;
967 uint32_t pwd
= 0xFFFFFFFF;
970 uint8_t addr
= 16; // default to invalid address
971 bool gotData
= false;
973 while(param_getchar(Cmd
, cmdp
) != 0x00)
975 switch(param_getchar(Cmd
, cmdp
))
979 return usage_lf_em_write();
982 addr
= param_get8ex(Cmd
, cmdp
+1, 16, 10);
987 data
= param_get32ex(Cmd
, cmdp
+1, 0, 16);
998 pwd
= param_get32ex(Cmd
, cmdp
+1, 1, 16);
1000 PrintAndLog("invalid pwd");
1012 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1019 if(errors
) return usage_lf_em_write();
1021 if ( strlen(Cmd
) == 0 ) return usage_lf_em_write();
1024 PrintAndLog("You must enter the data you want to write");
1025 return usage_lf_em_write();
1027 return EM4x05WriteWord(addr
, data
, pwd
, usePwd
, swap
, invert
);
1030 void printEM4x05config(uint32_t wordData
) {
1031 uint16_t datarate
= EM4x05_GET_BITRATE(wordData
);
1032 uint8_t encoder
= ((wordData
>> 6) & 0xF);
1034 memset(enc
,0,sizeof(enc
));
1036 uint8_t PSKcf
= (wordData
>> 10) & 0x3;
1038 memset(cf
,0,sizeof(cf
));
1039 uint8_t delay
= (wordData
>> 12) & 0x3;
1041 memset(cdelay
,0,sizeof(cdelay
));
1042 uint8_t numblks
= EM4x05_GET_NUM_BLOCKS(wordData
);
1043 uint8_t LWR
= numblks
+5-1; //last word read
1045 case 0: snprintf(enc
,sizeof(enc
),"NRZ"); break;
1046 case 1: snprintf(enc
,sizeof(enc
),"Manchester"); break;
1047 case 2: snprintf(enc
,sizeof(enc
),"Biphase"); break;
1048 case 3: snprintf(enc
,sizeof(enc
),"Miller"); break;
1049 case 4: snprintf(enc
,sizeof(enc
),"PSK1"); break;
1050 case 5: snprintf(enc
,sizeof(enc
),"PSK2"); break;
1051 case 6: snprintf(enc
,sizeof(enc
),"PSK3"); break;
1052 case 7: snprintf(enc
,sizeof(enc
),"Unknown"); break;
1053 case 8: snprintf(enc
,sizeof(enc
),"FSK1"); break;
1054 case 9: snprintf(enc
,sizeof(enc
),"FSK2"); break;
1055 default: snprintf(enc
,sizeof(enc
),"Unknown"); break;
1059 case 0: snprintf(cf
,sizeof(cf
),"RF/2"); break;
1060 case 1: snprintf(cf
,sizeof(cf
),"RF/8"); break;
1061 case 2: snprintf(cf
,sizeof(cf
),"RF/4"); break;
1062 case 3: snprintf(cf
,sizeof(cf
),"unknown"); break;
1066 case 0: snprintf(cdelay
, sizeof(cdelay
),"no delay"); break;
1067 case 1: snprintf(cdelay
, sizeof(cdelay
),"BP/8 or 1/8th bit period delay"); break;
1068 case 2: snprintf(cdelay
, sizeof(cdelay
),"BP/4 or 1/4th bit period delay"); break;
1069 case 3: snprintf(cdelay
, sizeof(cdelay
),"no delay"); break;
1071 uint8_t readLogin
= (wordData
& EM4x05_READ_LOGIN_REQ
)>>18;
1072 uint8_t readHKL
= (wordData
& EM4x05_READ_HK_LOGIN_REQ
)>>19;
1073 uint8_t writeLogin
= (wordData
& EM4x05_WRITE_LOGIN_REQ
)>>20;
1074 uint8_t writeHKL
= (wordData
& EM4x05_WRITE_HK_LOGIN_REQ
)>>21;
1075 uint8_t raw
= (wordData
& EM4x05_READ_AFTER_WRITE
)>>22;
1076 uint8_t disable
= (wordData
& EM4x05_DISABLE_ALLOWED
)>>23;
1077 uint8_t rtf
= (wordData
& EM4x05_READER_TALK_FIRST
)>>24;
1078 uint8_t pigeon
= (wordData
& (1<<26))>>26;
1079 PrintAndLog("ConfigWord: %08X (Word 4)\n", wordData
);
1080 PrintAndLog("Config Breakdown:");
1081 PrintAndLog(" Data Rate: %02u | RF/%u", wordData
& 0x3F, datarate
);
1082 PrintAndLog(" Encoder: %u | %s", encoder
, enc
);
1083 PrintAndLog(" PSK CF: %u | %s", PSKcf
, cf
);
1084 PrintAndLog(" Delay: %u | %s", delay
, cdelay
);
1085 PrintAndLog(" LastWordR: %02u | Address of last word for default read - meaning %u blocks are output", LWR
, numblks
);
1086 PrintAndLog(" ReadLogin: %u | Read Login is %s", readLogin
, readLogin
? "Required" : "Not Required");
1087 PrintAndLog(" ReadHKL: %u | Read Housekeeping Words Login is %s", readHKL
, readHKL
? "Required" : "Not Required");
1088 PrintAndLog("WriteLogin: %u | Write Login is %s", writeLogin
, writeLogin
? "Required" : "Not Required");
1089 PrintAndLog(" WriteHKL: %u | Write Housekeeping Words Login is %s", writeHKL
, writeHKL
? "Required" : "Not Required");
1090 PrintAndLog(" R.A.W.: %u | Read After Write is %s", raw
, raw
? "On" : "Off");
1091 PrintAndLog(" Disable: %u | Disable Command is %s", disable
, disable
? "Accepted" : "Not Accepted");
1092 PrintAndLog(" R.T.F.: %u | Reader Talk First is %s", rtf
, rtf
? "Enabled" : "Disabled");
1093 PrintAndLog(" Pigeon: %u | Pigeon Mode is %s\n", pigeon
, pigeon
? "Enabled" : "Disabled");
1096 void printEM4x05info(uint8_t chipType
, uint8_t cap
, uint16_t custCode
, uint32_t serial
) {
1098 case 9: PrintAndLog("\n Chip Type: %u | EM4305", chipType
); break;
1099 case 4: PrintAndLog(" Chip Type: %u | Unknown", chipType
); break;
1100 case 2: PrintAndLog(" Chip Type: %u | EM4469", chipType
); break;
1101 //add more here when known
1102 default: PrintAndLog(" Chip Type: %u Unknown", chipType
); break;
1106 case 3: PrintAndLog(" Cap Type: %u | 330pF",cap
); break;
1107 case 2: PrintAndLog(" Cap Type: %u | %spF",cap
, (chipType
==2)? "75":"210"); break;
1108 case 1: PrintAndLog(" Cap Type: %u | 250pF",cap
); break;
1109 case 0: PrintAndLog(" Cap Type: %u | no resonant capacitor",cap
); break;
1110 default: PrintAndLog(" Cap Type: %u | unknown",cap
); break;
1113 PrintAndLog(" Cust Code: %03u | %s", custCode
, (custCode
== 0x200) ? "Default": "Unknown");
1115 PrintAndLog("\n Serial #: %08X\n", serial
);
1119 void printEM4x05ProtectionBits(uint32_t wordData
) {
1120 for (uint8_t i
= 0; i
< 15; i
++) {
1121 PrintAndLog(" Word: %02u | %s", i
, (((1 << i
) & wordData
) || i
< 2) ? "Is Write Locked" : "Is Not Write Locked");
1123 PrintAndLog(" Word: %02u | %s", i
+1, (((1 << i
) & wordData
) || i
< 2) ? "Is Write Locked" : "Is Not Write Locked");
1128 //quick test for EM4x05/EM4x69 tag
1129 bool EM4x05Block0Test(uint32_t *wordData
) {
1130 if (EM4x05ReadWord_ext(0,0,false,wordData
) == 1) {
1136 int CmdEM4x05info(const char *Cmd
) {
1139 uint32_t wordData
= 0;
1140 bool usePwd
= false;
1141 uint8_t ctmp
= param_getchar(Cmd
, 0);
1142 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_dump();
1144 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
1145 pwd
= param_get32ex(Cmd
, 0, 1, 16);
1151 // read word 0 (chip info)
1152 // block 0 can be read even without a password.
1153 if ( !EM4x05Block0Test(&wordData
) )
1156 uint8_t chipType
= (wordData
>> 1) & 0xF;
1157 uint8_t cap
= (wordData
>> 5) & 3;
1158 uint16_t custCode
= (wordData
>> 9) & 0x3FF;
1160 // read word 1 (serial #) doesn't need pwd
1162 if (EM4x05ReadWord_ext(1, 0, false, &wordData
) != 1) {
1163 //failed, but continue anyway...
1165 printEM4x05info(chipType
, cap
, custCode
, wordData
);
1167 // read word 4 (config block)
1168 // needs password if one is set
1170 if ( EM4x05ReadWord_ext(4, pwd
, usePwd
, &wordData
) != 1 ) {
1172 PrintAndLog("Config block read failed - might be password protected.");
1175 printEM4x05config(wordData
);
1177 // read word 14 and 15 to see which is being used for the protection bits
1179 if ( EM4x05ReadWord_ext(14, pwd
, usePwd
, &wordData
) != 1 ) {
1183 // if status bit says this is not the used protection word
1184 if (!(wordData
& 0x8000)) {
1185 if ( EM4x05ReadWord_ext(15, pwd
, usePwd
, &wordData
) != 1 ) {
1190 if (!(wordData
& 0x8000)) {
1191 //something went wrong
1194 printEM4x05ProtectionBits(wordData
);
1200 static command_t CommandTable
[] =
1202 {"help", CmdHelp
, 1, "This help"},
1203 {"410xread", CmdEMdemodASK
, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
1204 {"410xdemod", CmdAskEM410xDemod
, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
1205 {"410xsim", CmdEM410xSim
, 0, "<UID> [clock rate] -- Simulate EM410x tag"},
1206 {"410xwatch", CmdEM410xWatch
, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
1207 {"410xspoof", CmdEM410xWatchnSpoof
, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
1208 {"410xwrite", CmdEM410xWrite
, 0, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
1209 {"4x05dump", CmdEM4x05dump
, 0, "(pwd) -- Read EM4x05/EM4x69 all word data"},
1210 {"4x05info", CmdEM4x05info
, 0, "(pwd) -- Get info from EM4x05/EM4x69 tag"},
1211 {"4x05readword", CmdEM4x05ReadWord
, 0, "<Word> (pwd) -- Read EM4x05/EM4x69 word data"},
1212 {"4x05writeword", CmdEM4x05WriteWord
, 0, "<Word> <data> (pwd) -- Write EM4x05/EM4x69 word data"},
1213 {"4x50read", CmdEM4x50Read
, 1, "demod data from EM4x50 tag from the graph buffer"},
1214 {NULL
, NULL
, 0, NULL
}
1217 int CmdLFEM4X(const char *Cmd
)
1219 CmdsParse(CommandTable
, Cmd
);
1223 int CmdHelp(const char *Cmd
)
1225 CmdsHelp(CommandTable
);