]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
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 "proxmark3.h"
19 #include "cmdparser.h"
23 #include "cmdlfem4x.h"
26 char *global_em410xId
;
28 static int CmdHelp(const char *Cmd
);
30 int CmdEMdemodASK(const char *Cmd
)
32 char cmdp
= param_getchar(Cmd
, 0);
33 int findone
= (cmdp
== '1') ? 1 : 0;
34 UsbCommand c
={CMD_EM410X_DEMOD
};
40 /* Read the ID of an EM410x tag.
42 * 1111 1111 1 <-- standard non-repeatable header
43 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
45 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
46 * 0 <-- stop bit, end of tag
48 int CmdEM410xRead(const char *Cmd
)
53 if(!AskEm410xDemod("", &hi
, &lo
, false)) return 0;
54 PrintAndLog("EM410x pattern found: ");
57 PrintAndLog ("EM410x XL pattern found");
61 sprintf(id
, "%010"PRIx64
,lo
);
67 int usage_lf_em410x_sim(void) {
68 PrintAndLog("Simulating EM410x tag");
70 PrintAndLog("Usage: lf em 410xsim [h] <uid> <clock>");
71 PrintAndLog("Options:");
72 PrintAndLog(" h - this help");
73 PrintAndLog(" uid - uid (10 HEX symbols)");
74 PrintAndLog(" clock - clock (32|64) (optional)");
75 PrintAndLog("samples:");
76 PrintAndLog(" lf em 410xsim 0F0368568B");
77 PrintAndLog(" lf em 410xsim 0F0368568B 32");
81 // emulate an EM410X tag
82 int CmdEM410xSim(const char *Cmd
)
84 int i
, n
, j
, binary
[4], parity
[4];
86 char cmdp
= param_getchar(Cmd
, 0);
87 uint8_t uid
[5] = {0x00};
89 if (cmdp
== 'h' || cmdp
== 'H') return usage_lf_em410x_sim();
90 /* clock is 64 in EM410x tags */
93 if (param_gethex(Cmd
, 0, uid
, 10)) {
94 PrintAndLog("UID must include 10 HEX symbols");
97 param_getdec(Cmd
,1, &clock
);
99 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X clock: %d", uid
[0],uid
[1],uid
[2],uid
[3],uid
[4],clock
);
100 PrintAndLog("Press pm3-button to about simulation");
103 /* clear our graph */
106 /* write 9 start bits */
107 for (i
= 0; i
< 9; i
++)
108 AppendGraph(0, clock
, 1);
110 /* for each hex char */
111 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
112 for (i
= 0; i
< 10; i
++)
114 /* read each hex char */
115 sscanf(&Cmd
[i
], "%1x", &n
);
116 for (j
= 3; j
>= 0; j
--, n
/= 2)
119 /* append each bit */
120 AppendGraph(0, clock
, binary
[0]);
121 AppendGraph(0, clock
, binary
[1]);
122 AppendGraph(0, clock
, binary
[2]);
123 AppendGraph(0, clock
, binary
[3]);
125 /* append parity bit */
126 AppendGraph(0, clock
, binary
[0] ^ binary
[1] ^ binary
[2] ^ binary
[3]);
128 /* keep track of column parity */
129 parity
[0] ^= binary
[0];
130 parity
[1] ^= binary
[1];
131 parity
[2] ^= binary
[2];
132 parity
[3] ^= binary
[3];
136 AppendGraph(0, clock
, parity
[0]);
137 AppendGraph(0, clock
, parity
[1]);
138 AppendGraph(0, clock
, parity
[2]);
139 AppendGraph(0, clock
, parity
[3]);
142 AppendGraph(1, clock
, 0);
144 CmdLFSim("0"); //240 start_gap.
148 /* Function is equivalent of lf read + data samples + em410xread
149 * looped until an EM410x tag is detected
151 * Why is CmdSamples("16000")?
152 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
153 * rate gets lower, then grow the number of samples
154 * Changed by martin, 4000 x 4 = 16000,
155 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
157 int CmdEM410xWatch(const char *Cmd
)
161 printf("\naborted via keyboard!\n");
166 getSamples("8201",true); //capture enough to get 2 complete preambles (4096*2+9)
167 } while (!CmdEM410xRead(""));
172 //currently only supports manchester modulations
173 int CmdEM410xWatchnSpoof(const char *Cmd
)
176 PrintAndLog("# Replaying captured ID: %s",global_em410xId
);
181 int CmdEM410xWrite(const char *Cmd
)
183 uint64_t id
= 0xFFFFFFFFFFFFFFFF; // invalid id value
184 int card
= 0xFF; // invalid card value
185 unsigned int clock
= 0; // invalid clock value
187 sscanf(Cmd
, "%" SCNx64
" %d %d", &id
, &card
, &clock
);
190 if (id
== 0xFFFFFFFFFFFFFFFF) {
191 PrintAndLog("Error! ID is required.\n");
194 if (id
>= 0x10000000000) {
195 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
201 PrintAndLog("Error! Card type required.\n");
205 PrintAndLog("Error! Bad card type selected.\n");
214 // Allowed clock rates: 16, 32, 40 and 64
215 if ((clock
!= 16) && (clock
!= 32) && (clock
!= 64) && (clock
!= 40)) {
216 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32, 40 and 64.\n", clock
);
221 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
" (clock rate: %d)", "T55x7", id
, clock
);
222 // NOTE: We really should pass the clock in as a separate argument, but to
223 // provide for backwards-compatibility for older firmware, and to avoid
224 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
225 // the clock rate in bits 8-15 of the card value
226 card
= (card
& 0xFF) | ((clock
<< 8) & 0xFF00);
227 } else if (card
== 0) {
228 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
, "T5555", id
, clock
);
229 card
= (card
& 0xFF) | ((clock
<< 8) & 0xFF00);
231 PrintAndLog("Error! Bad card type selected.\n");
235 UsbCommand c
= {CMD_EM410X_WRITE_TAG
, {card
, (uint32_t)(id
>> 32), (uint32_t)id
}};
241 //**************** Start of EM4x50 Code ************************
242 bool EM_EndParityTest(uint8_t *BitStream
, size_t size
, uint8_t rows
, uint8_t cols
, uint8_t pType
)
244 if (rows
*cols
>size
) return false;
246 //assume last col is a parity and do not test
247 for (uint8_t colNum
= 0; colNum
< cols
-1; colNum
++) {
248 for (uint8_t rowNum
= 0; rowNum
< rows
; rowNum
++) {
249 colP
^= BitStream
[(rowNum
*cols
)+colNum
];
251 if (colP
!= pType
) return false;
256 bool EM_ByteParityTest(uint8_t *BitStream
, size_t size
, uint8_t rows
, uint8_t cols
, uint8_t pType
)
258 if (rows
*cols
>size
) return false;
260 //assume last row is a parity row and do not test
261 for (uint8_t rowNum
= 0; rowNum
< rows
-1; rowNum
++) {
262 for (uint8_t colNum
= 0; colNum
< cols
; colNum
++) {
263 rowP
^= BitStream
[(rowNum
*cols
)+colNum
];
265 if (rowP
!= pType
) return false;
270 uint32_t OutputEM4x50_Block(uint8_t *BitStream
, size_t size
, bool verbose
, bool pTest
)
272 if (size
<45) return 0;
273 uint32_t code
= bytebits_to_byte(BitStream
,8);
274 code
= code
<<8 | bytebits_to_byte(BitStream
+9,8);
275 code
= code
<<8 | bytebits_to_byte(BitStream
+18,8);
276 code
= code
<<8 | bytebits_to_byte(BitStream
+27,8);
277 if (verbose
|| g_debugMode
){
278 for (uint8_t i
= 0; i
<5; i
++){
279 if (i
== 4) PrintAndLog(""); //parity byte spacer
280 PrintAndLog("%d%d%d%d%d%d%d%d %d -> 0x%02x",
290 bytebits_to_byte(BitStream
+i
*9,8)
294 PrintAndLog("Parity Passed");
296 PrintAndLog("Parity Failed");
300 /* Read the transmitted data of an EM4x50 tag from the graphbuffer
303 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
304 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
305 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
306 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
307 * CCCCCCCC <- column parity bits
309 * LW <- Listen Window
311 * This pattern repeats for every block of data being transmitted.
312 * Transmission starts with two Listen Windows (LW - a modulated
313 * pattern of 320 cycles each (32/32/128/64/64)).
315 * Note that this data may or may not be the UID. It is whatever data
316 * is stored in the blocks defined in the control word First and Last
317 * Word Read values. UID is stored in block 32.
319 //completed by Marshmellow
320 int EM4x50Read(const char *Cmd
, bool verbose
)
322 uint8_t fndClk
[] = {8,16,32,40,50,64,128};
326 int i
, j
, startblock
, skip
, block
, start
, end
, low
, high
, minClk
;
327 bool complete
= false;
328 int tmpbuff
[MAX_GRAPH_TRACE_LEN
/ 64];
334 memset(tmpbuff
, 0, MAX_GRAPH_TRACE_LEN
/ 64);
336 // get user entry if any
337 sscanf(Cmd
, "%i %i", &clk
, &invert
);
339 // save GraphBuffer - to restore it later
342 // first get high and low values
343 for (i
= 0; i
< GraphTraceLen
; i
++) {
344 if (GraphBuffer
[i
] > high
)
345 high
= GraphBuffer
[i
];
346 else if (GraphBuffer
[i
] < low
)
347 low
= GraphBuffer
[i
];
353 // get to first full low to prime loop and skip incomplete first pulse
354 while ((GraphBuffer
[i
] < high
) && (i
< GraphTraceLen
))
356 while ((GraphBuffer
[i
] > low
) && (i
< GraphTraceLen
))
360 // populate tmpbuff buffer with pulse lengths
361 while (i
< GraphTraceLen
) {
362 // measure from low to low
363 while ((GraphBuffer
[i
] > low
) && (i
< GraphTraceLen
))
366 while ((GraphBuffer
[i
] < high
) && (i
< GraphTraceLen
))
368 while ((GraphBuffer
[i
] > low
) && (i
< GraphTraceLen
))
370 if (j
>=(MAX_GRAPH_TRACE_LEN
/64)) {
373 tmpbuff
[j
++]= i
- start
;
374 if (i
-start
< minClk
&& i
< GraphTraceLen
) {
380 for (uint8_t clkCnt
= 0; clkCnt
<7; clkCnt
++) {
381 tol
= fndClk
[clkCnt
]/8;
382 if (minClk
>= fndClk
[clkCnt
]-tol
&& minClk
<= fndClk
[clkCnt
]+1) {
390 // look for data start - should be 2 pairs of LW (pulses of clk*3,clk*2)
392 for (i
= 0; i
< j
- 4 ; ++i
) {
394 if (tmpbuff
[i
] >= clk
*3-tol
&& tmpbuff
[i
] <= clk
*3+tol
) //3 clocks
395 if (tmpbuff
[i
+1] >= clk
*2-tol
&& tmpbuff
[i
+1] <= clk
*2+tol
) //2 clocks
396 if (tmpbuff
[i
+2] >= clk
*3-tol
&& tmpbuff
[i
+2] <= clk
*3+tol
) //3 clocks
397 if (tmpbuff
[i
+3] >= clk
-tol
) //1.5 to 2 clocks - depends on bit following
405 // skip over the remainder of LW
406 skip
+= tmpbuff
[i
+1] + tmpbuff
[i
+2] + clk
;
407 if (tmpbuff
[i
+3]>clk
)
408 phaseoff
= tmpbuff
[i
+3]-clk
;
411 // now do it again to find the end
413 for (i
+= 3; i
< j
- 4 ; ++i
) {
415 if (tmpbuff
[i
] >= clk
*3-tol
&& tmpbuff
[i
] <= clk
*3+tol
) //3 clocks
416 if (tmpbuff
[i
+1] >= clk
*2-tol
&& tmpbuff
[i
+1] <= clk
*2+tol
) //2 clocks
417 if (tmpbuff
[i
+2] >= clk
*3-tol
&& tmpbuff
[i
+2] <= clk
*3+tol
) //3 clocks
418 if (tmpbuff
[i
+3] >= clk
-tol
) //1.5 to 2 clocks - depends on bit following
426 if (verbose
|| g_debugMode
) {
428 PrintAndLog("\nNote: one block = 50 bits (32 data, 12 parity, 6 marker)");
430 PrintAndLog("No data found!, clock tried:%d",clk
);
431 PrintAndLog("Try again with more samples.");
432 PrintAndLog(" or after a 'data askedge' command to clean up the read");
435 } else if (start
< 0) return 0;
437 snprintf(tmp2
, sizeof(tmp2
),"%d %d 1000 %d", clk
, invert
, clk
*47);
438 // get rid of leading crap
439 snprintf(tmp
, sizeof(tmp
), "%i", skip
);
442 bool AllPTest
= true;
443 // now work through remaining buffer printing out data blocks
447 if (verbose
|| g_debugMode
) PrintAndLog("\nBlock %i:", block
);
450 // look for LW before start of next block
451 for ( ; i
< j
- 4 ; ++i
) {
453 if (tmpbuff
[i
] >= clk
*3-tol
&& tmpbuff
[i
] <= clk
*3+tol
)
454 if (tmpbuff
[i
+1] >= clk
-tol
)
457 if (i
>= j
-4) break; //next LW not found
459 if (tmpbuff
[i
+1]>clk
)
460 phaseoff
= tmpbuff
[i
+1]-clk
;
464 if (ASKDemod(tmp2
, false, false, 1) < 1) {
468 //set DemodBufferLen to just one block
469 DemodBufferLen
= skip
/clk
;
471 pTest
= EM_ByteParityTest(DemodBuffer
,DemodBufferLen
,5,9,0);
472 pTest
&= EM_EndParityTest(DemodBuffer
,DemodBufferLen
,5,9,0);
475 Code
[block
] = OutputEM4x50_Block(DemodBuffer
,DemodBufferLen
,verbose
, pTest
);
476 if (g_debugMode
) PrintAndLog("\nskipping %d samples, bits:%d", skip
, skip
/clk
);
477 //skip to start of next block
478 snprintf(tmp
,sizeof(tmp
),"%i",skip
);
481 if (i
>= end
) break; //in case chip doesn't output 6 blocks
484 if (verbose
|| g_debugMode
|| AllPTest
){
486 PrintAndLog("*** Warning!");
487 PrintAndLog("Partial data - no end found!");
488 PrintAndLog("Try again with more samples.");
490 PrintAndLog("Found data at sample: %i - using clock: %i", start
, clk
);
492 for (block
=0; block
< end
; block
++){
493 PrintAndLog("Block %d: %08x",block
,Code
[block
]);
496 PrintAndLog("Parities Passed");
498 PrintAndLog("Parities Failed");
499 PrintAndLog("Try cleaning the read samples with 'data askedge'");
503 //restore GraphBuffer
505 return (int)AllPTest
;
508 int CmdEM4x50Read(const char *Cmd
)
510 return EM4x50Read(Cmd
, true);
513 //**************** Start of EM4x05/EM4x69 Code ************************
514 int usage_lf_em_read(void) {
515 PrintAndLog("Read EM4x05/EM4x69. Tag must be on antenna. ");
517 PrintAndLog("Usage: lf em 4x05readword [h] <address> <pwd>");
518 PrintAndLog("Options:");
519 PrintAndLog(" h - this help");
520 PrintAndLog(" address - memory address to read. (0-15)");
521 PrintAndLog(" pwd - password (hex) (optional)");
522 PrintAndLog("samples:");
523 PrintAndLog(" lf em 4x05readword 1");
524 PrintAndLog(" lf em 4x05readword 1 11223344");
528 // for command responses from em4x05 or em4x69
529 // download samples from device and copy them to the Graphbuffer
530 bool downloadSamplesEM() {
531 // 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
533 GetFromBigBuf(got
, sizeof(got
), 0);
534 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 4000) ) {
535 PrintAndLog("command execution time out");
538 setGraphBuf(got
, sizeof(got
));
542 bool EM4x05testDemodReadData(uint32_t *word
, bool readCmd
) {
543 // em4x05/em4x69 command response preamble is 00001010
544 // skip first two 0 bits as they might have been missed in the demod
545 uint8_t preamble
[] = {0,0,1,0,1,0};
548 // set size to 20 to only test first 14 positions for the preamble or less if not a read command
549 size_t size
= (readCmd
) ? 20 : 11;
551 size
= (size
> DemodBufferLen
) ? DemodBufferLen
: size
;
553 if ( !preambleSearchEx(DemodBuffer
, preamble
, sizeof(preamble
), &size
, &startIdx
, true) ) {
554 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305 preamble not found :: %d", startIdx
);
557 // if this is a readword command, get the read bytes and test the parities
559 if (!EM_EndParityTest(DemodBuffer
+ startIdx
+ sizeof(preamble
), 45, 5, 9, 0)) {
560 if (g_debugMode
) PrintAndLog("DEBUG: Error - End Parity check failed");
563 // test for even parity bits and remove them. (leave out the end row of parities so 36 bits)
564 if ( removeParity(DemodBuffer
, startIdx
+ sizeof(preamble
),9,0,36) == 0 ) {
565 if (g_debugMode
) PrintAndLog("DEBUG: Error - Parity not detected");
569 setDemodBuf(DemodBuffer
, 32, 0);
570 *word
= bytebits_to_byteLSBF(DemodBuffer
, 32);
575 // FSK, PSK, ASK/MANCHESTER, ASK/BIPHASE, ASK/DIPHASE
576 // should cover 90% of known used configs
577 // the rest will need to be manually demoded for now...
578 int demodEM4x05resp(uint32_t *word
, bool readCmd
) {
581 // test for FSK wave (easiest to 99% ID)
582 if (GetFskClock("", false, false)) {
583 //valid fsk clocks found
584 ans
= FSKrawDemod("0 0", false);
586 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: FSK Demod failed, ans: %d", ans
);
588 if (EM4x05testDemodReadData(word
, readCmd
)) {
593 // PSK clocks should be easy to detect ( but difficult to demod a non-repeating pattern... )
594 ans
= GetPskClock("", false, false);
597 ans
= PSKDemod("0 0 6", false);
599 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans
);
601 if (EM4x05testDemodReadData(word
, readCmd
)) {
605 psk1TOpsk2(DemodBuffer
, DemodBufferLen
);
606 if (EM4x05testDemodReadData(word
, readCmd
)) {
611 ans
= PSKDemod("0 1 6", false);
613 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans
);
615 if (EM4x05testDemodReadData(word
, readCmd
)) {
619 psk1TOpsk2(DemodBuffer
, DemodBufferLen
);
620 if (EM4x05testDemodReadData(word
, readCmd
)) {
628 // manchester is more common than biphase... try first
629 bool stcheck
= false;
630 // try manchester - NOTE: ST only applies to T55x7 tags.
631 ans
= ASKDemod_ext("0,0,1", false, false, 1, &stcheck
);
633 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: ASK/Manchester Demod failed, ans: %d", ans
);
635 if (EM4x05testDemodReadData(word
, readCmd
)) {
641 ans
= ASKbiphaseDemod("0 0 1", false);
643 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans
);
645 if (EM4x05testDemodReadData(word
, readCmd
)) {
650 //try diphase (differential biphase or inverted)
651 ans
= ASKbiphaseDemod("0 1 1", false);
653 if (g_debugMode
) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans
);
655 if (EM4x05testDemodReadData(word
, readCmd
)) {
663 int EM4x05ReadWord_ext(uint8_t addr
, uint32_t pwd
, bool usePwd
, uint32_t *wordData
) {
664 UsbCommand c
= {CMD_EM4X_READ_WORD
, {addr
, pwd
, usePwd
}};
665 clearCommandBuffer();
668 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2500)){
669 PrintAndLog("Command timed out");
672 if ( !downloadSamplesEM() ) {
675 int testLen
= (GraphTraceLen
< 1000) ? GraphTraceLen
: 1000;
676 if (graphJustNoise(GraphBuffer
, testLen
)) {
677 PrintAndLog("no tag not found");
681 return demodEM4x05resp(wordData
, true);
684 int EM4x05ReadWord(uint8_t addr
, uint32_t pwd
, bool usePwd
) {
685 uint32_t wordData
= 0;
686 int success
= EM4x05ReadWord_ext(addr
, pwd
, usePwd
, &wordData
);
688 PrintAndLog("%s Address %02d | %08X", (addr
>13) ? "Lock":" Got",addr
,wordData
);
690 PrintAndLog("Read Address %02d | failed",addr
);
695 int CmdEM4x05ReadWord(const char *Cmd
) {
699 uint8_t ctmp
= param_getchar(Cmd
, 0);
700 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_read();
702 addr
= param_get8ex(Cmd
, 0, 50, 10);
703 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
704 pwd
= param_get32ex(Cmd
, 1, 1, 16);
707 PrintAndLog("Address must be between 0 and 15");
711 PrintAndLog("Reading address %02u", addr
);
714 PrintAndLog("Reading address %02u | password %08X", addr
, pwd
);
717 return EM4x05ReadWord(addr
, pwd
, usePwd
);
720 int usage_lf_em_dump(void) {
721 PrintAndLog("Dump EM4x05/EM4x69. Tag must be on antenna. ");
723 PrintAndLog("Usage: lf em 4x05dump [h] <pwd>");
724 PrintAndLog("Options:");
725 PrintAndLog(" h - this help");
726 PrintAndLog(" pwd - password (hex) (optional)");
727 PrintAndLog("samples:");
728 PrintAndLog(" lf em 4x05dump");
729 PrintAndLog(" lf em 4x05dump 11223344");
733 int CmdEM4x05dump(const char *Cmd
) {
737 uint8_t ctmp
= param_getchar(Cmd
, 0);
738 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_dump();
740 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
741 pwd
= param_get32ex(Cmd
, 0, 1, 16);
747 for (; addr
< 16; addr
++) {
750 PrintAndLog(" PWD Address %02u | %08X",addr
,pwd
);
752 PrintAndLog(" PWD Address 02 | cannot read");
755 success
&= EM4x05ReadWord(addr
, pwd
, usePwd
);
763 int usage_lf_em_write(void) {
764 PrintAndLog("Write EM4x05/EM4x69. Tag must be on antenna. ");
766 PrintAndLog("Usage: lf em 4x05writeword [h] [s] <address> <data> <pwd>");
767 PrintAndLog("Options:");
768 PrintAndLog(" h - this help");
769 PrintAndLog(" s - swap data bit order before write");
770 PrintAndLog(" address - memory address to write to. (0-15)");
771 PrintAndLog(" data - data to write (hex)");
772 PrintAndLog(" pwd - password (hex) (optional)");
773 PrintAndLog("samples:");
774 PrintAndLog(" lf em 4x05writeword 1");
775 PrintAndLog(" lf em 4x05writeword 1 deadc0de 11223344");
779 int CmdEM4x05WriteWord(const char *Cmd
) {
780 uint8_t ctmp
= param_getchar(Cmd
, 0);
781 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_write();
785 uint8_t addr
= 16; // default to invalid address
786 uint32_t data
= 0xFFFFFFFF; // default to blank data
787 uint32_t pwd
= 0xFFFFFFFF; // default to blank password
791 swap
= param_getchar(Cmd
, 0);
792 if (swap
== 's' || swap
=='S') p
++;
793 addr
= param_get8ex(Cmd
, p
++, 16, 10);
794 data
= param_get32ex(Cmd
, p
++, 0, 16);
795 pwd
= param_get32ex(Cmd
, p
++, 1, 16);
797 if (swap
== 's' || swap
=='S') data
= SwapBits(data
, 32);
800 PrintAndLog("Address must be between 0 and 15");
804 PrintAndLog("Writing address %d data %08X", addr
, data
);
807 PrintAndLog("Writing address %d data %08X using password %08X", addr
, data
, pwd
);
810 uint16_t flag
= (addr
<< 8 ) | usePwd
;
812 UsbCommand c
= {CMD_EM4X_WRITE_WORD
, {flag
, data
, pwd
}};
813 clearCommandBuffer();
816 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)){
817 PrintAndLog("Error occurred, device did not respond during write operation.");
820 if ( !downloadSamplesEM() ) {
823 //check response for 00001010 for write confirmation!
826 int result
= demodEM4x05resp(&dummy
,false);
828 PrintAndLog("Write Verified");
830 PrintAndLog("Write could not be verified");
835 void printEM4x05config(uint32_t wordData
) {
836 uint16_t datarate
= (((wordData
& 0x3F)+1)*2);
837 uint8_t encoder
= ((wordData
>> 6) & 0xF);
839 memset(enc
,0,sizeof(enc
));
841 uint8_t PSKcf
= (wordData
>> 10) & 0x3;
843 memset(cf
,0,sizeof(cf
));
844 uint8_t delay
= (wordData
>> 12) & 0x3;
846 memset(cdelay
,0,sizeof(cdelay
));
847 uint8_t LWR
= (wordData
>> 14) & 0xF; //last word read
850 case 0: snprintf(enc
,sizeof(enc
),"NRZ"); break;
851 case 1: snprintf(enc
,sizeof(enc
),"Manchester"); break;
852 case 2: snprintf(enc
,sizeof(enc
),"Biphase"); break;
853 case 3: snprintf(enc
,sizeof(enc
),"Miller"); break;
854 case 4: snprintf(enc
,sizeof(enc
),"PSK1"); break;
855 case 5: snprintf(enc
,sizeof(enc
),"PSK2"); break;
856 case 6: snprintf(enc
,sizeof(enc
),"PSK3"); break;
857 case 7: snprintf(enc
,sizeof(enc
),"Unknown"); break;
858 case 8: snprintf(enc
,sizeof(enc
),"FSK1"); break;
859 case 9: snprintf(enc
,sizeof(enc
),"FSK2"); break;
860 default: snprintf(enc
,sizeof(enc
),"Unknown"); break;
864 case 0: snprintf(cf
,sizeof(cf
),"RF/2"); break;
865 case 1: snprintf(cf
,sizeof(cf
),"RF/8"); break;
866 case 2: snprintf(cf
,sizeof(cf
),"RF/4"); break;
867 case 3: snprintf(cf
,sizeof(cf
),"unknown"); break;
871 case 0: snprintf(cdelay
, sizeof(cdelay
),"no delay"); break;
872 case 1: snprintf(cdelay
, sizeof(cdelay
),"BP/8 or 1/8th bit period delay"); break;
873 case 2: snprintf(cdelay
, sizeof(cdelay
),"BP/4 or 1/4th bit period delay"); break;
874 case 3: snprintf(cdelay
, sizeof(cdelay
),"no delay"); break;
876 PrintAndLog("ConfigWord: %08X (Word 4)\n", wordData
);
877 PrintAndLog("Config Breakdown:", wordData
);
878 PrintAndLog(" Data Rate: %02u | RF/%u", wordData
& 0x3F, datarate
);
879 PrintAndLog(" Encoder: %u | %s", encoder
, enc
);
880 PrintAndLog(" PSK CF: %u | %s", PSKcf
, cf
);
881 PrintAndLog(" Delay: %u | %s", delay
, cdelay
);
882 PrintAndLog(" LastWordR: %02u | Address of last word for default read", LWR
);
883 PrintAndLog(" ReadLogin: %u | Read Login is %s", (wordData
& 0x40000)>>18, (wordData
& 0x40000) ? "Required" : "Not Required");
884 PrintAndLog(" ReadHKL: %u | Read Housekeeping Words Login is %s", (wordData
& 0x80000)>>19, (wordData
& 0x80000) ? "Required" : "Not Required");
885 PrintAndLog("WriteLogin: %u | Write Login is %s", (wordData
& 0x100000)>>20, (wordData
& 0x100000) ? "Required" : "Not Required");
886 PrintAndLog(" WriteHKL: %u | Write Housekeeping Words Login is %s", (wordData
& 0x200000)>>21, (wordData
& 0x200000) ? "Required" : "Not Required");
887 PrintAndLog(" R.A.W.: %u | Read After Write is %s", (wordData
& 0x400000)>>22, (wordData
& 0x400000) ? "On" : "Off");
888 PrintAndLog(" Disable: %u | Disable Command is %s", (wordData
& 0x800000)>>23, (wordData
& 0x800000) ? "Accepted" : "Not Accepted");
889 PrintAndLog(" R.T.F.: %u | Reader Talk First is %s", (wordData
& 0x1000000)>>24, (wordData
& 0x1000000) ? "Enabled" : "Disabled");
890 PrintAndLog(" Pigeon: %u | Pigeon Mode is %s\n", (wordData
& 0x4000000)>>26, (wordData
& 0x4000000) ? "Enabled" : "Disabled");
893 void printEM4x05info(uint8_t chipType
, uint8_t cap
, uint16_t custCode
, uint32_t serial
) {
895 case 9: PrintAndLog("\n Chip Type: %u | EM4305", chipType
); break;
896 case 4: PrintAndLog(" Chip Type: %u | Unknown", chipType
); break;
897 case 2: PrintAndLog(" Chip Type: %u | EM4469", chipType
); break;
898 //add more here when known
899 default: PrintAndLog(" Chip Type: %u Unknown", chipType
); break;
903 case 3: PrintAndLog(" Cap Type: %u | 330pF",cap
); break;
904 case 2: PrintAndLog(" Cap Type: %u | %spF",cap
, (chipType
==2)? "75":"210"); break;
905 case 1: PrintAndLog(" Cap Type: %u | 250pF",cap
); break;
906 case 0: PrintAndLog(" Cap Type: %u | no resonant capacitor",cap
); break;
907 default: PrintAndLog(" Cap Type: %u | unknown",cap
); break;
910 PrintAndLog(" Cust Code: %03u | %s", custCode
, (custCode
== 0x200) ? "Default": "Unknown");
912 PrintAndLog("\n Serial #: %08X\n", serial
);
916 void printEM4x05ProtectionBits(uint32_t wordData
) {
917 for (uint8_t i
= 0; i
< 15; i
++) {
918 PrintAndLog(" Word: %02u | %s", i
, (((1 << i
) & wordData
) || i
< 2) ? "Is Write Locked" : "Is Not Write Locked");
920 PrintAndLog(" Word: %02u | %s", i
+1, (((1 << i
) & wordData
) || i
< 2) ? "Is Write Locked" : "Is Not Write Locked");
925 //quick test for EM4x05/EM4x69 tag
926 bool EM4x05Block0Test(uint32_t *wordData
) {
927 if (EM4x05ReadWord_ext(0,0,false,wordData
) == 1) {
933 int CmdEM4x05info(const char *Cmd
) {
936 uint32_t wordData
= 0;
938 uint8_t ctmp
= param_getchar(Cmd
, 0);
939 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_dump();
941 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
942 pwd
= param_get32ex(Cmd
, 0, 1, 16);
948 // read word 0 (chip info)
949 // block 0 can be read even without a password.
950 if ( !EM4x05Block0Test(&wordData
) )
953 uint8_t chipType
= (wordData
>> 1) & 0xF;
954 uint8_t cap
= (wordData
>> 5) & 3;
955 uint16_t custCode
= (wordData
>> 9) & 0x3FF;
957 // read word 1 (serial #) doesn't need pwd
959 if (EM4x05ReadWord_ext(1, 0, false, &wordData
) != 1) {
960 //failed, but continue anyway...
962 printEM4x05info(chipType
, cap
, custCode
, wordData
);
964 // read word 4 (config block)
965 // needs password if one is set
967 if ( EM4x05ReadWord_ext(4, pwd
, usePwd
, &wordData
) != 1 ) {
971 printEM4x05config(wordData
);
973 // read word 14 and 15 to see which is being used for the protection bits
975 if ( EM4x05ReadWord_ext(14, pwd
, usePwd
, &wordData
) != 1 ) {
979 // if status bit says this is not the used protection word
980 if (!(wordData
& 0x8000)) {
981 if ( EM4x05ReadWord_ext(15, pwd
, usePwd
, &wordData
) != 1 ) {
986 if (!(wordData
& 0x8000)) {
987 //something went wrong
990 printEM4x05ProtectionBits(wordData
);
996 static command_t CommandTable
[] =
998 {"help", CmdHelp
, 1, "This help"},
999 {"410xdemod", CmdEMdemodASK
, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
1000 {"410xread", CmdEM410xRead
, 1, "[clock rate] -- Extract ID from EM410x tag in GraphBuffer"},
1001 {"410xsim", CmdEM410xSim
, 0, "<UID> [clock rate] -- Simulate EM410x tag"},
1002 {"410xwatch", CmdEM410xWatch
, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
1003 {"410xspoof", CmdEM410xWatchnSpoof
, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
1004 {"410xwrite", CmdEM410xWrite
, 0, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
1005 {"4x05dump", CmdEM4x05dump
, 0, "(pwd) -- Read EM4x05/EM4x69 all word data"},
1006 {"4x05info", CmdEM4x05info
, 0, "(pwd) -- Get info from EM4x05/EM4x69 tag"},
1007 {"4x05readword", CmdEM4x05ReadWord
, 0, "<Word> (pwd) -- Read EM4x05/EM4x69 word data"},
1008 {"4x05writeword", CmdEM4x05WriteWord
, 0, "<Word> <data> (pwd) -- Write EM4x05/EM4x69 word data"},
1009 {"4x50read", CmdEM4x50Read
, 1, "demod data from EM4x50 tag from the graph buffer"},
1010 {NULL
, NULL
, 0, NULL
}
1013 int CmdLFEM4X(const char *Cmd
)
1015 CmdsParse(CommandTable
, Cmd
);
1019 int CmdHelp(const char *Cmd
)
1021 CmdsHelp(CommandTable
);