]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
d2ccfae8ef05768320979b3ea0e4730d1f51f73a
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] <address> <data> <pwd>");
767 PrintAndLog("Options:");
768 PrintAndLog(" h - this help");
769 PrintAndLog(" address - memory address to write to. (0-15)");
770 PrintAndLog(" data - data to write (hex)");
771 PrintAndLog(" pwd - password (hex) (optional)");
772 PrintAndLog("samples:");
773 PrintAndLog(" lf em 4x05writeword 1");
774 PrintAndLog(" lf em 4x05writeword 1 deadc0de 11223344");
778 int CmdEM4x05WriteWord(const char *Cmd
) {
779 uint8_t ctmp
= param_getchar(Cmd
, 0);
780 if ( strlen(Cmd
) == 0 || ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_write();
784 uint8_t addr
= 16; // default to invalid address
785 uint32_t data
= 0xFFFFFFFF; // default to blank data
786 uint32_t pwd
= 0xFFFFFFFF; // default to blank password
788 addr
= param_get8ex(Cmd
, 0, 16, 10);
789 data
= param_get32ex(Cmd
, 1, 0, 16);
790 pwd
= param_get32ex(Cmd
, 2, 1, 16);
794 PrintAndLog("Address must be between 0 and 15");
798 PrintAndLog("Writing address %d data %08X", addr
, data
);
801 PrintAndLog("Writing address %d data %08X using password %08X", addr
, data
, pwd
);
804 uint16_t flag
= (addr
<< 8 ) | usePwd
;
806 UsbCommand c
= {CMD_EM4X_WRITE_WORD
, {flag
, data
, pwd
}};
807 clearCommandBuffer();
810 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)){
811 PrintAndLog("Error occurred, device did not respond during write operation.");
814 if ( !downloadSamplesEM() ) {
817 //check response for 00001010 for write confirmation!
820 int result
= demodEM4x05resp(&dummy
,false);
822 PrintAndLog("Write Verified");
824 PrintAndLog("Write could not be verified");
829 void printEM4x05config(uint32_t wordData
) {
830 uint16_t datarate
= (((wordData
& 0x3F)+1)*2);
831 uint8_t encoder
= ((wordData
>> 6) & 0xF);
833 memset(enc
,0,sizeof(enc
));
835 uint8_t PSKcf
= (wordData
>> 10) & 0x3;
837 memset(cf
,0,sizeof(cf
));
838 uint8_t delay
= (wordData
>> 12) & 0x3;
840 memset(cdelay
,0,sizeof(cdelay
));
841 uint8_t LWR
= (wordData
>> 14) & 0xF; //last word read
844 case 0: snprintf(enc
,sizeof(enc
),"NRZ"); break;
845 case 1: snprintf(enc
,sizeof(enc
),"Manchester"); break;
846 case 2: snprintf(enc
,sizeof(enc
),"Biphase"); break;
847 case 3: snprintf(enc
,sizeof(enc
),"Miller"); break;
848 case 4: snprintf(enc
,sizeof(enc
),"PSK1"); break;
849 case 5: snprintf(enc
,sizeof(enc
),"PSK2"); break;
850 case 6: snprintf(enc
,sizeof(enc
),"PSK3"); break;
851 case 7: snprintf(enc
,sizeof(enc
),"Unknown"); break;
852 case 8: snprintf(enc
,sizeof(enc
),"FSK1"); break;
853 case 9: snprintf(enc
,sizeof(enc
),"FSK2"); break;
854 default: snprintf(enc
,sizeof(enc
),"Unknown"); break;
858 case 0: snprintf(cf
,sizeof(cf
),"RF/2"); break;
859 case 1: snprintf(cf
,sizeof(cf
),"RF/8"); break;
860 case 2: snprintf(cf
,sizeof(cf
),"RF/4"); break;
861 case 3: snprintf(cf
,sizeof(cf
),"unknown"); break;
865 case 0: snprintf(cdelay
, sizeof(cdelay
),"no delay"); break;
866 case 1: snprintf(cdelay
, sizeof(cdelay
),"BP/8 or 1/8th bit period delay"); break;
867 case 2: snprintf(cdelay
, sizeof(cdelay
),"BP/4 or 1/4th bit period delay"); break;
868 case 3: snprintf(cdelay
, sizeof(cdelay
),"no delay"); break;
870 PrintAndLog("ConfigWord: %08X (Word 4)\n", wordData
);
871 PrintAndLog("Config Breakdown:", wordData
);
872 PrintAndLog(" Data Rate: %02u | RF/%u", wordData
& 0x3F, datarate
);
873 PrintAndLog(" Encoder: %u | %s", encoder
, enc
);
874 PrintAndLog(" PSK CF: %u | %s", PSKcf
, cf
);
875 PrintAndLog(" Delay: %u | %s", delay
, cdelay
);
876 PrintAndLog(" LastWordR: %02u | Address of last word for default read", LWR
);
877 PrintAndLog(" ReadLogin: %u | Read Login is %s", (wordData
& 0x40000)>>18, (wordData
& 0x40000) ? "Required" : "Not Required");
878 PrintAndLog(" ReadHKL: %u | Read Housekeeping Words Login is %s", (wordData
& 0x80000)>>19, (wordData
& 0x80000) ? "Required" : "Not Required");
879 PrintAndLog("WriteLogin: %u | Write Login is %s", (wordData
& 0x100000)>>20, (wordData
& 0x100000) ? "Required" : "Not Required");
880 PrintAndLog(" WriteHKL: %u | Write Housekeeping Words Login is %s", (wordData
& 0x200000)>>21, (wordData
& 0x200000) ? "Required" : "Not Required");
881 PrintAndLog(" R.A.W.: %u | Read After Write is %s", (wordData
& 0x400000)>>22, (wordData
& 0x400000) ? "On" : "Off");
882 PrintAndLog(" Disable: %u | Disable Command is %s", (wordData
& 0x800000)>>23, (wordData
& 0x800000) ? "Accepted" : "Not Accepted");
883 PrintAndLog(" R.T.F.: %u | Reader Talk First is %s", (wordData
& 0x1000000)>>24, (wordData
& 0x1000000) ? "Enabled" : "Disabled");
884 PrintAndLog(" Pigeon: %u | Pigeon Mode is %s\n", (wordData
& 0x4000000)>>26, (wordData
& 0x4000000) ? "Enabled" : "Disabled");
887 void printEM4x05info(uint8_t chipType
, uint8_t cap
, uint16_t custCode
, uint32_t serial
) {
889 case 9: PrintAndLog("\n Chip Type: %u | EM4305", chipType
); break;
890 case 4: PrintAndLog(" Chip Type: %u | Unknown", chipType
); break;
891 case 2: PrintAndLog(" Chip Type: %u | EM4469", chipType
); break;
892 //add more here when known
893 default: PrintAndLog(" Chip Type: %u Unknown", chipType
); break;
897 case 3: PrintAndLog(" Cap Type: %u | 330pF",cap
); break;
898 case 2: PrintAndLog(" Cap Type: %u | %spF",cap
, (chipType
==2)? "75":"210"); break;
899 case 1: PrintAndLog(" Cap Type: %u | 250pF",cap
); break;
900 case 0: PrintAndLog(" Cap Type: %u | no resonant capacitor",cap
); break;
901 default: PrintAndLog(" Cap Type: %u | unknown",cap
); break;
904 PrintAndLog(" Cust Code: %03u | %s", custCode
, (custCode
== 0x200) ? "Default": "Unknown");
906 PrintAndLog("\n Serial #: %08X\n", serial
);
910 void printEM4x05ProtectionBits(uint32_t wordData
) {
911 for (uint8_t i
= 0; i
< 15; i
++) {
912 PrintAndLog(" Word: %02u | %s", i
, (((1 << i
) & wordData
) || i
< 2) ? "Is Write Locked" : "Is Not Write Locked");
914 PrintAndLog(" Word: %02u | %s", i
+1, (((1 << i
) & wordData
) || i
< 2) ? "Is Write Locked" : "Is Not Write Locked");
919 //quick test for EM4x05/EM4x69 tag
920 bool EM4x05Block0Test(uint32_t *wordData
) {
921 if (EM4x05ReadWord_ext(0,0,false,wordData
) == 1) {
927 int CmdEM4x05info(const char *Cmd
) {
930 uint32_t wordData
= 0;
932 uint8_t ctmp
= param_getchar(Cmd
, 0);
933 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_lf_em_dump();
935 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
936 pwd
= param_get32ex(Cmd
, 0, 1, 16);
942 // read word 0 (chip info)
943 // block 0 can be read even without a password.
944 if ( !EM4x05Block0Test(&wordData
) )
947 uint8_t chipType
= (wordData
>> 1) & 0xF;
948 uint8_t cap
= (wordData
>> 5) & 3;
949 uint16_t custCode
= (wordData
>> 9) & 0x3FF;
951 // read word 1 (serial #) doesn't need pwd
953 if (EM4x05ReadWord_ext(1, 0, false, &wordData
) != 1) {
954 //failed, but continue anyway...
956 printEM4x05info(chipType
, cap
, custCode
, wordData
);
958 // read word 4 (config block)
959 // needs password if one is set
961 if ( EM4x05ReadWord_ext(4, pwd
, usePwd
, &wordData
) != 1 ) {
965 printEM4x05config(wordData
);
967 // read word 14 and 15 to see which is being used for the protection bits
969 if ( EM4x05ReadWord_ext(14, pwd
, usePwd
, &wordData
) != 1 ) {
973 // if status bit says this is not the used protection word
974 if (!(wordData
& 0x8000)) {
975 if ( EM4x05ReadWord_ext(15, pwd
, usePwd
, &wordData
) != 1 ) {
980 if (!(wordData
& 0x8000)) {
981 //something went wrong
984 printEM4x05ProtectionBits(wordData
);
990 static command_t CommandTable
[] =
992 {"help", CmdHelp
, 1, "This help"},
993 {"410xdemod", CmdEMdemodASK
, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
994 {"410xread", CmdEM410xRead
, 1, "[clock rate] -- Extract ID from EM410x tag in GraphBuffer"},
995 {"410xsim", CmdEM410xSim
, 0, "<UID> [clock rate] -- Simulate EM410x tag"},
996 {"410xwatch", CmdEM410xWatch
, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
997 {"410xspoof", CmdEM410xWatchnSpoof
, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
998 {"410xwrite", CmdEM410xWrite
, 0, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
999 {"4x05dump", CmdEM4x05dump
, 0, "(pwd) -- Read EM4x05/EM4x69 all word data"},
1000 {"4x05info", CmdEM4x05info
, 0, "(pwd) -- Get info from EM4x05/EM4x69 tag"},
1001 {"4x05readword", CmdEM4x05ReadWord
, 0, "<Word> (pwd) -- Read EM4x05/EM4x69 word data"},
1002 {"4x05writeword", CmdEM4x05WriteWord
, 0, "<Word> <data> (pwd) -- Write EM4x05/EM4x69 word data"},
1003 {"4x50read", CmdEM4x50Read
, 1, "demod data from EM4x50 tag from the graph buffer"},
1004 {NULL
, NULL
, 0, NULL
}
1007 int CmdLFEM4X(const char *Cmd
)
1009 CmdsParse(CommandTable
, Cmd
);
1013 int CmdHelp(const char *Cmd
)
1015 CmdsHelp(CommandTable
);