]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
e5bea0683a4723dab9b51d333d3e7b860cf06520
[proxmark3-svn] / client / cmdlfem4x.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Low frequency EM4x commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <inttypes.h>
14 #include "cmdlfem4x.h"
15 #include "proxmark3.h"
16 #include "ui.h"
17 #include "util.h"
18 #include "data.h"
19 #include "graph.h"
20 #include "cmdparser.h"
21 #include "cmddata.h"
22 #include "cmdlf.h"
23 #include "cmdmain.h"
24 #include "lfdemod.h"
25 #include "protocols.h"
26
27 uint64_t g_em410xId=0;
28
29 static int CmdHelp(const char *Cmd);
30
31 int CmdEMdemodASK(const char *Cmd)
32 {
33 char cmdp = param_getchar(Cmd, 0);
34 int findone = (cmdp == '1') ? 1 : 0;
35 UsbCommand c={CMD_EM410X_DEMOD};
36 c.arg[0]=findone;
37 SendCommand(&c);
38 return 0;
39 }
40
41 //by marshmellow
42 //print 64 bit EM410x ID in multiple formats
43 void printEM410x(uint32_t hi, uint64_t id)
44 {
45 if (id || hi){
46 uint64_t iii=1;
47 uint64_t id2lo=0;
48 uint32_t ii=0;
49 uint32_t i=0;
50 for (ii=5; ii>0;ii--){
51 for (i=0;i<8;i++){
52 id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8)));
53 }
54 }
55 if (hi){
56 //output 88 bit em id
57 PrintAndLog("\nEM TAG ID : %06X%016" PRIX64, hi, id);
58 } else{
59 //output 40 bit em 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)
83 );
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);
87
88 uint32_t p1id = (id & 0xFFFFFF);
89 uint8_t arr[32] = {0x00};
90 int i =0;
91 int j = 23;
92 for (; i < 24; ++i, --j ){
93 arr[i] = (p1id >> i) & 1;
94 }
95
96 uint32_t p1 = 0;
97
98 p1 |= arr[23] << 21;
99 p1 |= arr[22] << 23;
100 p1 |= arr[21] << 20;
101 p1 |= arr[20] << 22;
102
103 p1 |= arr[19] << 18;
104 p1 |= arr[18] << 16;
105 p1 |= arr[17] << 19;
106 p1 |= arr[16] << 17;
107
108 p1 |= arr[15] << 13;
109 p1 |= arr[14] << 15;
110 p1 |= arr[13] << 12;
111 p1 |= arr[12] << 14;
112
113 p1 |= arr[11] << 6;
114 p1 |= arr[10] << 2;
115 p1 |= arr[9] << 7;
116 p1 |= arr[8] << 1;
117
118 p1 |= arr[7] << 0;
119 p1 |= arr[6] << 8;
120 p1 |= arr[5] << 11;
121 p1 |= arr[4] << 3;
122
123 p1 |= arr[3] << 10;
124 p1 |= arr[2] << 4;
125 p1 |= arr[1] << 5;
126 p1 |= arr[0] << 9;
127 PrintAndLog("Pattern 1 : %d [0x%X]", p1, p1);
128
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);
133 }
134 }
135 return;
136 }
137
138 /* Read the ID of an EM410x tag.
139 * Format:
140 * 1111 1111 1 <-- standard non-repeatable header
141 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
142 * ....
143 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
144 * 0 <-- stop bit, end of tag
145 */
146 int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo )
147 {
148 size_t idx = 0;
149 size_t BitLen = DemodBufferLen;
150 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
151 memcpy(BitStream, DemodBuffer, BitLen);
152 if (Em410xDecode(BitStream, &BitLen, &idx, hi, lo)){
153 //set GraphBuffer for clone or sim command
154 setDemodBuf(BitStream, BitLen, idx);
155 if (g_debugMode){
156 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
157 printDemodBuff();
158 }
159 if (verbose){
160 PrintAndLog("EM410x pattern found: ");
161 printEM410x(*hi, *lo);
162 g_em410xId = *lo;
163 }
164 return 1;
165 }
166 return 0;
167 }
168
169 //askdemod then call Em410xdecode
170 int AskEm410xDemod(const char *Cmd, uint32_t *hi, uint64_t *lo, bool verbose)
171 {
172 bool st = true;
173 if (!ASKDemod_ext(Cmd, false, false, 1, &st)) return 0;
174 return AskEm410xDecode(verbose, hi, lo);
175 }
176
177 //by marshmellow
178 //takes 3 arguments - clock, invert and maxErr as integers
179 //attempts to demodulate ask while decoding manchester
180 //prints binary found and saves in graphbuffer for further commands
181 int CmdAskEM410xDemod(const char *Cmd)
182 {
183 char cmdp = param_getchar(Cmd, 0);
184 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
185 PrintAndLog("Usage: lf em 410xdemod [clock] <0|1> [maxError]");
186 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
187 PrintAndLog(" <invert>, 1 for invert output");
188 PrintAndLog(" [set maximum allowed errors], default = 100.");
189 PrintAndLog("");
190 PrintAndLog(" sample: lf em 410xdemod = demod an EM410x Tag ID from GraphBuffer");
191 PrintAndLog(" : lf em 410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32");
192 PrintAndLog(" : lf em 410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data");
193 PrintAndLog(" : lf em 410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data");
194 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");
195 return 0;
196 }
197 uint64_t lo = 0;
198 uint32_t hi = 0;
199 return AskEm410xDemod(Cmd, &hi, &lo, true);
200 }
201
202 int usage_lf_em410x_sim(void) {
203 PrintAndLog("Simulating EM410x tag");
204 PrintAndLog("");
205 PrintAndLog("Usage: lf em 410xsim [h] <uid> <clock>");
206 PrintAndLog("Options:");
207 PrintAndLog(" h - this help");
208 PrintAndLog(" uid - uid (10 HEX symbols)");
209 PrintAndLog(" clock - clock (32|64) (optional)");
210 PrintAndLog("samples:");
211 PrintAndLog(" lf em 410xsim 0F0368568B");
212 PrintAndLog(" lf em 410xsim 0F0368568B 32");
213 return 0;
214 }
215
216 // emulate an EM410X tag
217 int CmdEM410xSim(const char *Cmd)
218 {
219 int i, n, j, binary[4], parity[4];
220
221 char cmdp = param_getchar(Cmd, 0);
222 uint8_t uid[5] = {0x00};
223
224 if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_sim();
225 /* clock is 64 in EM410x tags */
226 uint8_t clock = 64;
227
228 if (param_gethex(Cmd, 0, uid, 10)) {
229 PrintAndLog("UID must include 10 HEX symbols");
230 return 0;
231 }
232 param_getdec(Cmd,1, &clock);
233
234 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X clock: %d", uid[0],uid[1],uid[2],uid[3],uid[4],clock);
235 PrintAndLog("Press pm3-button to about simulation");
236
237
238 /* clear our graph */
239 ClearGraph(0);
240
241 /* write 9 start bits */
242 for (i = 0; i < 9; i++)
243 AppendGraph(0, clock, 1);
244
245 /* for each hex char */
246 parity[0] = parity[1] = parity[2] = parity[3] = 0;
247 for (i = 0; i < 10; i++)
248 {
249 /* read each hex char */
250 sscanf(&Cmd[i], "%1x", &n);
251 for (j = 3; j >= 0; j--, n/= 2)
252 binary[j] = n % 2;
253
254 /* append each bit */
255 AppendGraph(0, clock, binary[0]);
256 AppendGraph(0, clock, binary[1]);
257 AppendGraph(0, clock, binary[2]);
258 AppendGraph(0, clock, binary[3]);
259
260 /* append parity bit */
261 AppendGraph(0, clock, binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
262
263 /* keep track of column parity */
264 parity[0] ^= binary[0];
265 parity[1] ^= binary[1];
266 parity[2] ^= binary[2];
267 parity[3] ^= binary[3];
268 }
269
270 /* parity columns */
271 AppendGraph(0, clock, parity[0]);
272 AppendGraph(0, clock, parity[1]);
273 AppendGraph(0, clock, parity[2]);
274 AppendGraph(0, clock, parity[3]);
275
276 /* stop bit */
277 AppendGraph(1, clock, 0);
278
279 CmdLFSim("0"); //240 start_gap.
280 return 0;
281 }
282
283 /* Function is equivalent of lf read + data samples + em410xread
284 * looped until an EM410x tag is detected
285 *
286 * Why is CmdSamples("16000")?
287 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
288 * rate gets lower, then grow the number of samples
289 * Changed by martin, 4000 x 4 = 16000,
290 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
291 *
292 * 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
293 */
294 int CmdEM410xWatch(const char *Cmd)
295 {
296 do {
297 if (ukbhit()) {
298 printf("\naborted via keyboard!\n");
299 break;
300 }
301
302 CmdLFRead("s");
303 getSamples("8201",true);
304 } while (!CmdAskEM410xDemod(""));
305
306 return 0;
307 }
308
309 //currently only supports manchester modulations
310 int CmdEM410xWatchnSpoof(const char *Cmd)
311 {
312 CmdEM410xWatch(Cmd);
313 PrintAndLog("# Replaying captured ID: %010"PRIx64, g_em410xId);
314 CmdLFaskSim("");
315 return 0;
316 }
317
318 int CmdEM410xWrite(const char *Cmd)
319 {
320 uint64_t id = 0xFFFFFFFFFFFFFFFF; // invalid id value
321 int card = 0xFF; // invalid card value
322 unsigned int clock = 0; // invalid clock value
323
324 sscanf(Cmd, "%" SCNx64 " %d %d", &id, &card, &clock);
325
326 // Check ID
327 if (id == 0xFFFFFFFFFFFFFFFF) {
328 PrintAndLog("Error! ID is required.\n");
329 return 0;
330 }
331 if (id >= 0x10000000000) {
332 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
333 return 0;
334 }
335
336 // Check Card
337 if (card == 0xFF) {
338 PrintAndLog("Error! Card type required.\n");
339 return 0;
340 }
341 if (card < 0) {
342 PrintAndLog("Error! Bad card type selected.\n");
343 return 0;
344 }
345
346 // Check Clock
347 // Default: 64
348 if (clock == 0)
349 clock = 64;
350
351 // Allowed clock rates: 16, 32, 40 and 64
352 if ((clock != 16) && (clock != 32) && (clock != 64) && (clock != 40)) {
353 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32, 40 and 64.\n", clock);
354 return 0;
355 }
356
357 if (card == 1) {
358 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64 " (clock rate: %d)", "T55x7", id, clock);
359 // NOTE: We really should pass the clock in as a separate argument, but to
360 // provide for backwards-compatibility for older firmware, and to avoid
361 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
362 // the clock rate in bits 8-15 of the card value
363 card = (card & 0xFF) | ((clock << 8) & 0xFF00);
364 } else if (card == 0) {
365 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64, "T5555", id, clock);
366 card = (card & 0xFF) | ((clock << 8) & 0xFF00);
367 } else {
368 PrintAndLog("Error! Bad card type selected.\n");
369 return 0;
370 }
371
372 UsbCommand c = {CMD_EM410X_WRITE_TAG, {card, (uint32_t)(id >> 32), (uint32_t)id}};
373 SendCommand(&c);
374
375 return 0;
376 }
377
378 //**************** Start of EM4x50 Code ************************
379 bool EM_EndParityTest(uint8_t *BitStream, size_t size, uint8_t rows, uint8_t cols, uint8_t pType)
380 {
381 if (rows*cols>size) return false;
382 uint8_t colP=0;
383 //assume last col is a parity and do not test
384 for (uint8_t colNum = 0; colNum < cols-1; colNum++) {
385 for (uint8_t rowNum = 0; rowNum < rows; rowNum++) {
386 colP ^= BitStream[(rowNum*cols)+colNum];
387 }
388 if (colP != pType) return false;
389 }
390 return true;
391 }
392
393 bool EM_ByteParityTest(uint8_t *BitStream, size_t size, uint8_t rows, uint8_t cols, uint8_t pType)
394 {
395 if (rows*cols>size) return false;
396 uint8_t rowP=0;
397 //assume last row is a parity row and do not test
398 for (uint8_t rowNum = 0; rowNum < rows-1; rowNum++) {
399 for (uint8_t colNum = 0; colNum < cols; colNum++) {
400 rowP ^= BitStream[(rowNum*cols)+colNum];
401 }
402 if (rowP != pType) return false;
403 }
404 return true;
405 }
406
407 uint32_t OutputEM4x50_Block(uint8_t *BitStream, size_t size, bool verbose, bool pTest)
408 {
409 if (size<45) return 0;
410 uint32_t code = bytebits_to_byte(BitStream,8);
411 code = code<<8 | bytebits_to_byte(BitStream+9,8);
412 code = code<<8 | bytebits_to_byte(BitStream+18,8);
413 code = code<<8 | bytebits_to_byte(BitStream+27,8);
414 if (verbose || g_debugMode){
415 for (uint8_t i = 0; i<5; i++){
416 if (i == 4) PrintAndLog(""); //parity byte spacer
417 PrintAndLog("%d%d%d%d%d%d%d%d %d -> 0x%02x",
418 BitStream[i*9],
419 BitStream[i*9+1],
420 BitStream[i*9+2],
421 BitStream[i*9+3],
422 BitStream[i*9+4],
423 BitStream[i*9+5],
424 BitStream[i*9+6],
425 BitStream[i*9+7],
426 BitStream[i*9+8],
427 bytebits_to_byte(BitStream+i*9,8)
428 );
429 }
430 if (pTest)
431 PrintAndLog("Parity Passed");
432 else
433 PrintAndLog("Parity Failed");
434 }
435 return code;
436 }
437 /* Read the transmitted data of an EM4x50 tag from the graphbuffer
438 * Format:
439 *
440 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
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 * CCCCCCCC <- column parity bits
445 * 0 <- stop bit
446 * LW <- Listen Window
447 *
448 * This pattern repeats for every block of data being transmitted.
449 * Transmission starts with two Listen Windows (LW - a modulated
450 * pattern of 320 cycles each (32/32/128/64/64)).
451 *
452 * Note that this data may or may not be the UID. It is whatever data
453 * is stored in the blocks defined in the control word First and Last
454 * Word Read values. UID is stored in block 32.
455 */
456 //completed by Marshmellow
457 int EM4x50Read(const char *Cmd, bool verbose)
458 {
459 uint8_t fndClk[] = {8,16,32,40,50,64,128};
460 int clk = 0;
461 int invert = 0;
462 int tol = 0;
463 int i, j, startblock, skip, block, start, end, low, high, minClk;
464 bool complete = false;
465 int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
466 uint32_t Code[6];
467 char tmp[6];
468 char tmp2[20];
469 int phaseoff;
470 high = low = 0;
471 memset(tmpbuff, 0, MAX_GRAPH_TRACE_LEN / 64);
472
473 // get user entry if any
474 sscanf(Cmd, "%i %i", &clk, &invert);
475
476 // save GraphBuffer - to restore it later
477 save_restoreGB(1);
478
479 // first get high and low values
480 for (i = 0; i < GraphTraceLen; i++) {
481 if (GraphBuffer[i] > high)
482 high = GraphBuffer[i];
483 else if (GraphBuffer[i] < low)
484 low = GraphBuffer[i];
485 }
486
487 i = 0;
488 j = 0;
489 minClk = 255;
490 // get to first full low to prime loop and skip incomplete first pulse
491 while ((GraphBuffer[i] < high) && (i < GraphTraceLen))
492 ++i;
493 while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
494 ++i;
495 skip = i;
496
497 // populate tmpbuff buffer with pulse lengths
498 while (i < GraphTraceLen) {
499 // measure from low to low
500 while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
501 ++i;
502 start= i;
503 while ((GraphBuffer[i] < high) && (i < GraphTraceLen))
504 ++i;
505 while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
506 ++i;
507 if (j>=(MAX_GRAPH_TRACE_LEN/64)) {
508 break;
509 }
510 tmpbuff[j++]= i - start;
511 if (i-start < minClk && i < GraphTraceLen) {
512 minClk = i - start;
513 }
514 }
515 // set clock
516 if (!clk) {
517 for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) {
518 tol = fndClk[clkCnt]/8;
519 if (minClk >= fndClk[clkCnt]-tol && minClk <= fndClk[clkCnt]+1) {
520 clk=fndClk[clkCnt];
521 break;
522 }
523 }
524 if (!clk) return 0;
525 } else tol = clk/8;
526
527 // look for data start - should be 2 pairs of LW (pulses of clk*3,clk*2)
528 start = -1;
529 for (i= 0; i < j - 4 ; ++i) {
530 skip += tmpbuff[i];
531 if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) //3 clocks
532 if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol) //2 clocks
533 if (tmpbuff[i+2] >= clk*3-tol && tmpbuff[i+2] <= clk*3+tol) //3 clocks
534 if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following
535 {
536 start= i + 4;
537 break;
538 }
539 }
540 startblock = i + 4;
541
542 // skip over the remainder of LW
543 skip += tmpbuff[i+1] + tmpbuff[i+2] + clk;
544 if (tmpbuff[i+3]>clk)
545 phaseoff = tmpbuff[i+3]-clk;
546 else
547 phaseoff = 0;
548 // now do it again to find the end
549 end = skip;
550 for (i += 3; i < j - 4 ; ++i) {
551 end += tmpbuff[i];
552 if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) //3 clocks
553 if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol) //2 clocks
554 if (tmpbuff[i+2] >= clk*3-tol && tmpbuff[i+2] <= clk*3+tol) //3 clocks
555 if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following
556 {
557 complete= true;
558 break;
559 }
560 }
561 end = i;
562 // report back
563 if (verbose || g_debugMode) {
564 if (start >= 0) {
565 PrintAndLog("\nNote: one block = 50 bits (32 data, 12 parity, 6 marker)");
566 } else {
567 PrintAndLog("No data found!, clock tried:%d",clk);
568 PrintAndLog("Try again with more samples.");
569 PrintAndLog(" or after a 'data askedge' command to clean up the read");
570 return 0;
571 }
572 } else if (start < 0) return 0;
573 start = skip;
574 snprintf(tmp2, sizeof(tmp2),"%d %d 1000 %d", clk, invert, clk*47);
575 // get rid of leading crap
576 snprintf(tmp, sizeof(tmp), "%i", skip);
577 CmdLtrim(tmp);
578 bool pTest;
579 bool AllPTest = true;
580 // now work through remaining buffer printing out data blocks
581 block = 0;
582 i = startblock;
583 while (block < 6) {
584 if (verbose || g_debugMode) PrintAndLog("\nBlock %i:", block);
585 skip = phaseoff;
586
587 // look for LW before start of next block
588 for ( ; i < j - 4 ; ++i) {
589 skip += tmpbuff[i];
590 if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol)
591 if (tmpbuff[i+1] >= clk-tol)
592 break;
593 }
594 if (i >= j-4) break; //next LW not found
595 skip += clk;
596 if (tmpbuff[i+1]>clk)
597 phaseoff = tmpbuff[i+1]-clk;
598 else
599 phaseoff = 0;
600 i += 2;
601 if (ASKDemod(tmp2, false, false, 1) < 1) {
602 save_restoreGB(0);
603 return 0;
604 }
605 //set DemodBufferLen to just one block
606 DemodBufferLen = skip/clk;
607 //test parities
608 pTest = EM_ByteParityTest(DemodBuffer,DemodBufferLen,5,9,0);
609 pTest &= EM_EndParityTest(DemodBuffer,DemodBufferLen,5,9,0);
610 AllPTest &= pTest;
611 //get output
612 Code[block] = OutputEM4x50_Block(DemodBuffer,DemodBufferLen,verbose, pTest);
613 if (g_debugMode) PrintAndLog("\nskipping %d samples, bits:%d", skip, skip/clk);
614 //skip to start of next block
615 snprintf(tmp,sizeof(tmp),"%i",skip);
616 CmdLtrim(tmp);
617 block++;
618 if (i >= end) break; //in case chip doesn't output 6 blocks
619 }
620 //print full code:
621 if (verbose || g_debugMode || AllPTest){
622 if (!complete) {
623 PrintAndLog("*** Warning!");
624 PrintAndLog("Partial data - no end found!");
625 PrintAndLog("Try again with more samples.");
626 }
627 PrintAndLog("Found data at sample: %i - using clock: %i", start, clk);
628 end = block;
629 for (block=0; block < end; block++){
630 PrintAndLog("Block %d: %08x",block,Code[block]);
631 }
632 if (AllPTest) {
633 PrintAndLog("Parities Passed");
634 } else {
635 PrintAndLog("Parities Failed");
636 PrintAndLog("Try cleaning the read samples with 'data askedge'");
637 }
638 }
639
640 //restore GraphBuffer
641 save_restoreGB(0);
642 return (int)AllPTest;
643 }
644
645 int CmdEM4x50Read(const char *Cmd)
646 {
647 return EM4x50Read(Cmd, true);
648 }
649
650 //**************** Start of EM4x05/EM4x69 Code ************************
651 int usage_lf_em_read(void) {
652 PrintAndLog("Read EM4x05/EM4x69. Tag must be on antenna. ");
653 PrintAndLog("");
654 PrintAndLog("Usage: lf em 4x05readword [h] <address> <pwd>");
655 PrintAndLog("Options:");
656 PrintAndLog(" h - this help");
657 PrintAndLog(" address - memory address to read. (0-15)");
658 PrintAndLog(" pwd - password (hex) (optional)");
659 PrintAndLog("samples:");
660 PrintAndLog(" lf em 4x05readword 1");
661 PrintAndLog(" lf em 4x05readword 1 11223344");
662 return 0;
663 }
664
665 // for command responses from em4x05 or em4x69
666 // download samples from device and copy them to the Graphbuffer
667 bool downloadSamplesEM() {
668 // 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
669 uint8_t got[6000];
670 GetFromBigBuf(got, sizeof(got), 0);
671 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 4000) ) {
672 PrintAndLog("command execution time out");
673 return false;
674 }
675 setGraphBuf(got, sizeof(got));
676 return true;
677 }
678
679 bool EM4x05testDemodReadData(uint32_t *word, bool readCmd) {
680 // em4x05/em4x69 command response preamble is 00001010
681 // skip first two 0 bits as they might have been missed in the demod
682 uint8_t preamble[] = {0,0,1,0,1,0};
683 size_t startIdx = 0;
684
685 // set size to 20 to only test first 14 positions for the preamble or less if not a read command
686 size_t size = (readCmd) ? 20 : 11;
687 // sanity check
688 size = (size > DemodBufferLen) ? DemodBufferLen : size;
689 // test preamble
690 if ( !preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &size, &startIdx, true) ) {
691 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305 preamble not found :: %d", startIdx);
692 return false;
693 }
694 // if this is a readword command, get the read bytes and test the parities
695 if (readCmd) {
696 if (!EM_EndParityTest(DemodBuffer + startIdx + sizeof(preamble), 45, 5, 9, 0)) {
697 if (g_debugMode) PrintAndLog("DEBUG: Error - End Parity check failed");
698 return false;
699 }
700 // test for even parity bits and remove them. (leave out the end row of parities so 36 bits)
701 if ( removeParity(DemodBuffer, startIdx + sizeof(preamble),9,0,36) == 0 ) {
702 if (g_debugMode) PrintAndLog("DEBUG: Error - Parity not detected");
703 return false;
704 }
705
706 setDemodBuf(DemodBuffer, 32, 0);
707 *word = bytebits_to_byteLSBF(DemodBuffer, 32);
708 }
709 return true;
710 }
711
712 // FSK, PSK, ASK/MANCHESTER, ASK/BIPHASE, ASK/DIPHASE
713 // should cover 90% of known used configs
714 // the rest will need to be manually demoded for now...
715 int demodEM4x05resp(uint32_t *word, bool readCmd) {
716 int ans = 0;
717
718 // test for FSK wave (easiest to 99% ID)
719 if (GetFskClock("", false, false)) {
720 //valid fsk clocks found
721 ans = FSKrawDemod("0 0", false);
722 if (!ans) {
723 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: FSK Demod failed, ans: %d", ans);
724 } else {
725 if (EM4x05testDemodReadData(word, readCmd)) {
726 return 1;
727 }
728 }
729 }
730 // PSK clocks should be easy to detect ( but difficult to demod a non-repeating pattern... )
731 ans = GetPskClock("", false, false);
732 if (ans>0) {
733 //try psk1
734 ans = PSKDemod("0 0 6", false);
735 if (!ans) {
736 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans);
737 } else {
738 if (EM4x05testDemodReadData(word, readCmd)) {
739 return 1;
740 } else {
741 //try psk2
742 psk1TOpsk2(DemodBuffer, DemodBufferLen);
743 if (EM4x05testDemodReadData(word, readCmd)) {
744 return 1;
745 }
746 }
747 //try psk1 inverted
748 ans = PSKDemod("0 1 6", false);
749 if (!ans) {
750 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans);
751 } else {
752 if (EM4x05testDemodReadData(word, readCmd)) {
753 return 1;
754 } else {
755 //try psk2
756 psk1TOpsk2(DemodBuffer, DemodBufferLen);
757 if (EM4x05testDemodReadData(word, readCmd)) {
758 return 1;
759 }
760 }
761 }
762 }
763 }
764
765 // manchester is more common than biphase... try first
766 bool stcheck = false;
767 // try manchester - NOTE: ST only applies to T55x7 tags.
768 ans = ASKDemod_ext("0,0,1", false, false, 1, &stcheck);
769 if (!ans) {
770 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: ASK/Manchester Demod failed, ans: %d", ans);
771 } else {
772 if (EM4x05testDemodReadData(word, readCmd)) {
773 return 1;
774 }
775 }
776
777 //try biphase
778 ans = ASKbiphaseDemod("0 0 1", false);
779 if (!ans) {
780 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans);
781 } else {
782 if (EM4x05testDemodReadData(word, readCmd)) {
783 return 1;
784 }
785 }
786
787 //try diphase (differential biphase or inverted)
788 ans = ASKbiphaseDemod("0 1 1", false);
789 if (!ans) {
790 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans);
791 } else {
792 if (EM4x05testDemodReadData(word, readCmd)) {
793 return 1;
794 }
795 }
796
797 return -1;
798 }
799
800 int EM4x05ReadWord_ext(uint8_t addr, uint32_t pwd, bool usePwd, uint32_t *wordData) {
801 UsbCommand c = {CMD_EM4X_READ_WORD, {addr, pwd, usePwd}};
802 clearCommandBuffer();
803 SendCommand(&c);
804 UsbCommand resp;
805 if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)){
806 PrintAndLog("Command timed out");
807 return -1;
808 }
809 if ( !downloadSamplesEM() ) {
810 return -1;
811 }
812 int testLen = (GraphTraceLen < 1000) ? GraphTraceLen : 1000;
813 if (graphJustNoise(GraphBuffer, testLen)) {
814 PrintAndLog("no tag not found");
815 return -1;
816 }
817 //attempt demod:
818 return demodEM4x05resp(wordData, true);
819 }
820
821 int EM4x05ReadWord(uint8_t addr, uint32_t pwd, bool usePwd) {
822 uint32_t wordData = 0;
823 int success = EM4x05ReadWord_ext(addr, pwd, usePwd, &wordData);
824 if (success == 1)
825 PrintAndLog("%s Address %02d | %08X", (addr>13) ? "Lock":" Got",addr,wordData);
826 else
827 PrintAndLog("Read Address %02d | failed",addr);
828
829 return success;
830 }
831
832 int CmdEM4x05ReadWord(const char *Cmd) {
833 uint8_t addr;
834 uint32_t pwd;
835 bool usePwd = false;
836 uint8_t ctmp = param_getchar(Cmd, 0);
837 if ( strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_em_read();
838
839 addr = param_get8ex(Cmd, 0, 50, 10);
840 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
841 pwd = param_get32ex(Cmd, 1, 1, 16);
842
843 if ( (addr > 15) ) {
844 PrintAndLog("Address must be between 0 and 15");
845 return 1;
846 }
847 if ( pwd == 1 ) {
848 PrintAndLog("Reading address %02u", addr);
849 } else {
850 usePwd = true;
851 PrintAndLog("Reading address %02u | password %08X", addr, pwd);
852 }
853
854 return EM4x05ReadWord(addr, pwd, usePwd);
855 }
856
857 int usage_lf_em_dump(void) {
858 PrintAndLog("Dump EM4x05/EM4x69. Tag must be on antenna. ");
859 PrintAndLog("");
860 PrintAndLog("Usage: lf em 4x05dump [h] <pwd>");
861 PrintAndLog("Options:");
862 PrintAndLog(" h - this help");
863 PrintAndLog(" pwd - password (hex) (optional)");
864 PrintAndLog("samples:");
865 PrintAndLog(" lf em 4x05dump");
866 PrintAndLog(" lf em 4x05dump 11223344");
867 return 0;
868 }
869
870 int CmdEM4x05dump(const char *Cmd) {
871 uint8_t addr = 0;
872 uint32_t pwd;
873 bool usePwd = false;
874 uint8_t ctmp = param_getchar(Cmd, 0);
875 if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em_dump();
876
877 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
878 pwd = param_get32ex(Cmd, 0, 1, 16);
879
880 if ( pwd != 1 ) {
881 usePwd = true;
882 }
883 int success = 1;
884 for (; addr < 16; addr++) {
885 if (addr == 2) {
886 if (usePwd) {
887 PrintAndLog(" PWD Address %02u | %08X",addr,pwd);
888 } else {
889 PrintAndLog(" PWD Address 02 | cannot read");
890 }
891 } else {
892 success &= EM4x05ReadWord(addr, pwd, usePwd);
893 }
894 }
895
896 return success;
897 }
898
899
900 int usage_lf_em_write(void) {
901 PrintAndLog("Write EM4x05/EM4x69. Tag must be on antenna. ");
902 PrintAndLog("");
903 PrintAndLog("Usage: lf em 4x05writeword [h] a <address> d <data> p <pwd> [s] [i]");
904 PrintAndLog("Options:");
905 PrintAndLog(" h - this help");
906 PrintAndLog(" a <address> - memory address to write to. (0-15)");
907 PrintAndLog(" d <data> - data to write (hex)");
908 PrintAndLog(" p <pwd> - password (hex) (optional)");
909 PrintAndLog(" s - swap the data bit order before write");
910 PrintAndLog(" i - invert the data bits before write");
911 PrintAndLog("samples:");
912 PrintAndLog(" lf em 4x05writeword a 5 d 11223344");
913 PrintAndLog(" lf em 4x05writeword a 5 p deadc0de d 11223344 s i");
914 return 0;
915 }
916
917 int EM4x05WriteWord(uint8_t addr, uint32_t data, uint32_t pwd, bool usePwd, bool swap, bool invert) {
918 if (swap) data = SwapBits(data, 32);
919
920 if (invert) data ^= 0xFFFFFFFF;
921
922 if ( (addr > 15) ) {
923 PrintAndLog("Address must be between 0 and 15");
924 return -1;
925 }
926 if ( !usePwd ) {
927 PrintAndLog("Writing address %d data %08X", addr, data);
928 } else {
929 PrintAndLog("Writing address %d data %08X using password %08X", addr, data, pwd);
930 }
931
932 uint16_t flag = (addr << 8 ) | usePwd;
933
934 UsbCommand c = {CMD_EM4X_WRITE_WORD, {flag, data, pwd}};
935 clearCommandBuffer();
936 SendCommand(&c);
937 UsbCommand resp;
938 if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)){
939 PrintAndLog("Error occurred, device did not respond during write operation.");
940 return -1;
941 }
942 if ( !downloadSamplesEM() ) {
943 return -1;
944 }
945 //check response for 00001010 for write confirmation!
946 //attempt demod:
947 uint32_t dummy = 0;
948 int result = demodEM4x05resp(&dummy,false);
949 if (result == 1) {
950 PrintAndLog("Write Verified");
951 } else {
952 PrintAndLog("Write could not be verified");
953 }
954 return result;
955 }
956
957 int CmdEM4x05WriteWord(const char *Cmd) {
958 bool errors = false;
959 bool usePwd = false;
960 uint32_t data = 0xFFFFFFFF;
961 uint32_t pwd = 0xFFFFFFFF;
962 bool swap = false;
963 bool invert = false;
964 uint8_t addr = 16; // default to invalid address
965
966 char cmdp = 0;
967 while(param_getchar(Cmd, cmdp) != 0x00)
968 {
969 switch(param_getchar(Cmd, cmdp))
970 {
971 case 'h':
972 case 'H':
973 return usage_lf_em_write();
974 case 'a':
975 case 'A':
976 addr = param_get8ex(Cmd, cmdp+1, 16, 10);
977 cmdp += 2;
978 break;
979 case 'd':
980 case 'D':
981 data = param_get32ex(Cmd, cmdp+1, 0, 16);
982 cmdp += 2;
983 break;
984 case 'i':
985 case 'I':
986 invert = true;
987 cmdp++;
988 break;
989 case 'p':
990 case 'P':
991 pwd = param_get32ex(Cmd, cmdp+1, 1, 16);
992 if (pwd == 1) {
993 PrintAndLog("invalid pwd");
994 errors = true;
995 }
996 usePwd = true;
997 cmdp += 2;
998 break;
999 case 's':
1000 case 'S':
1001 swap = true;
1002 cmdp++;
1003 break;
1004 default:
1005 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1006 errors = true;
1007 break;
1008 }
1009 if(errors) break;
1010 }
1011 //Validations
1012 if(errors) return usage_lf_em_write();
1013
1014 if ( strlen(Cmd) == 0 ) return usage_lf_em_write();
1015
1016 return EM4x05WriteWord(addr, data, pwd, usePwd, swap, invert);
1017 }
1018
1019 void printEM4x05config(uint32_t wordData) {
1020 uint16_t datarate = EM4x05_GET_BITRATE(wordData);
1021 uint8_t encoder = ((wordData >> 6) & 0xF);
1022 char enc[14];
1023 memset(enc,0,sizeof(enc));
1024
1025 uint8_t PSKcf = (wordData >> 10) & 0x3;
1026 char cf[10];
1027 memset(cf,0,sizeof(cf));
1028 uint8_t delay = (wordData >> 12) & 0x3;
1029 char cdelay[33];
1030 memset(cdelay,0,sizeof(cdelay));
1031 uint8_t numblks = EM4x05_GET_NUM_BLOCKS(wordData);
1032 uint8_t LWR = numblks+5-1; //last word read
1033 switch (encoder) {
1034 case 0: snprintf(enc,sizeof(enc),"NRZ"); break;
1035 case 1: snprintf(enc,sizeof(enc),"Manchester"); break;
1036 case 2: snprintf(enc,sizeof(enc),"Biphase"); break;
1037 case 3: snprintf(enc,sizeof(enc),"Miller"); break;
1038 case 4: snprintf(enc,sizeof(enc),"PSK1"); break;
1039 case 5: snprintf(enc,sizeof(enc),"PSK2"); break;
1040 case 6: snprintf(enc,sizeof(enc),"PSK3"); break;
1041 case 7: snprintf(enc,sizeof(enc),"Unknown"); break;
1042 case 8: snprintf(enc,sizeof(enc),"FSK1"); break;
1043 case 9: snprintf(enc,sizeof(enc),"FSK2"); break;
1044 default: snprintf(enc,sizeof(enc),"Unknown"); break;
1045 }
1046
1047 switch (PSKcf) {
1048 case 0: snprintf(cf,sizeof(cf),"RF/2"); break;
1049 case 1: snprintf(cf,sizeof(cf),"RF/8"); break;
1050 case 2: snprintf(cf,sizeof(cf),"RF/4"); break;
1051 case 3: snprintf(cf,sizeof(cf),"unknown"); break;
1052 }
1053
1054 switch (delay) {
1055 case 0: snprintf(cdelay, sizeof(cdelay),"no delay"); break;
1056 case 1: snprintf(cdelay, sizeof(cdelay),"BP/8 or 1/8th bit period delay"); break;
1057 case 2: snprintf(cdelay, sizeof(cdelay),"BP/4 or 1/4th bit period delay"); break;
1058 case 3: snprintf(cdelay, sizeof(cdelay),"no delay"); break;
1059 }
1060 uint8_t readLogin = (wordData & EM4x05_READ_LOGIN_REQ)>>18;
1061 uint8_t readHKL = (wordData & EM4x05_READ_HK_LOGIN_REQ)>>19;
1062 uint8_t writeLogin = (wordData & EM4x05_WRITE_LOGIN_REQ)>>20;
1063 uint8_t writeHKL = (wordData & EM4x05_WRITE_HK_LOGIN_REQ)>>21;
1064 uint8_t raw = (wordData & EM4x05_READ_AFTER_WRITE)>>22;
1065 uint8_t disable = (wordData & EM4x05_DISABLE_ALLOWED)>>23;
1066 uint8_t rtf = (wordData & EM4x05_READER_TALK_FIRST)>>24;
1067 uint8_t pigeon = (wordData & (1<<26))>>26;
1068 PrintAndLog("ConfigWord: %08X (Word 4)\n", wordData);
1069 PrintAndLog("Config Breakdown:");
1070 PrintAndLog(" Data Rate: %02u | RF/%u", wordData & 0x3F, datarate);
1071 PrintAndLog(" Encoder: %u | %s", encoder, enc);
1072 PrintAndLog(" PSK CF: %u | %s", PSKcf, cf);
1073 PrintAndLog(" Delay: %u | %s", delay, cdelay);
1074 PrintAndLog(" LastWordR: %02u | Address of last word for default read - meaning %u blocks are output", LWR, numblks);
1075 PrintAndLog(" ReadLogin: %u | Read Login is %s", readLogin, readLogin ? "Required" : "Not Required");
1076 PrintAndLog(" ReadHKL: %u | Read Housekeeping Words Login is %s", readHKL, readHKL ? "Required" : "Not Required");
1077 PrintAndLog("WriteLogin: %u | Write Login is %s", writeLogin, writeLogin ? "Required" : "Not Required");
1078 PrintAndLog(" WriteHKL: %u | Write Housekeeping Words Login is %s", writeHKL, writeHKL ? "Required" : "Not Required");
1079 PrintAndLog(" R.A.W.: %u | Read After Write is %s", raw, raw ? "On" : "Off");
1080 PrintAndLog(" Disable: %u | Disable Command is %s", disable, disable ? "Accepted" : "Not Accepted");
1081 PrintAndLog(" R.T.F.: %u | Reader Talk First is %s", rtf, rtf ? "Enabled" : "Disabled");
1082 PrintAndLog(" Pigeon: %u | Pigeon Mode is %s\n", pigeon, pigeon ? "Enabled" : "Disabled");
1083 }
1084
1085 void printEM4x05info(uint8_t chipType, uint8_t cap, uint16_t custCode, uint32_t serial) {
1086 switch (chipType) {
1087 case 9: PrintAndLog("\n Chip Type: %u | EM4305", chipType); break;
1088 case 4: PrintAndLog(" Chip Type: %u | Unknown", chipType); break;
1089 case 2: PrintAndLog(" Chip Type: %u | EM4469", chipType); break;
1090 //add more here when known
1091 default: PrintAndLog(" Chip Type: %u Unknown", chipType); break;
1092 }
1093
1094 switch (cap) {
1095 case 3: PrintAndLog(" Cap Type: %u | 330pF",cap); break;
1096 case 2: PrintAndLog(" Cap Type: %u | %spF",cap, (chipType==2)? "75":"210"); break;
1097 case 1: PrintAndLog(" Cap Type: %u | 250pF",cap); break;
1098 case 0: PrintAndLog(" Cap Type: %u | no resonant capacitor",cap); break;
1099 default: PrintAndLog(" Cap Type: %u | unknown",cap); break;
1100 }
1101
1102 PrintAndLog(" Cust Code: %03u | %s", custCode, (custCode == 0x200) ? "Default": "Unknown");
1103 if (serial != 0) {
1104 PrintAndLog("\n Serial #: %08X\n", serial);
1105 }
1106 }
1107
1108 void printEM4x05ProtectionBits(uint32_t wordData) {
1109 for (uint8_t i = 0; i < 15; i++) {
1110 PrintAndLog(" Word: %02u | %s", i, (((1 << i) & wordData ) || i < 2) ? "Is Write Locked" : "Is Not Write Locked");
1111 if (i==14) {
1112 PrintAndLog(" Word: %02u | %s", i+1, (((1 << i) & wordData ) || i < 2) ? "Is Write Locked" : "Is Not Write Locked");
1113 }
1114 }
1115 }
1116
1117 //quick test for EM4x05/EM4x69 tag
1118 bool EM4x05Block0Test(uint32_t *wordData) {
1119 if (EM4x05ReadWord_ext(0,0,false,wordData) == 1) {
1120 return true;
1121 }
1122 return false;
1123 }
1124
1125 int CmdEM4x05info(const char *Cmd) {
1126 //uint8_t addr = 0;
1127 uint32_t pwd;
1128 uint32_t wordData = 0;
1129 bool usePwd = false;
1130 uint8_t ctmp = param_getchar(Cmd, 0);
1131 if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em_dump();
1132
1133 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
1134 pwd = param_get32ex(Cmd, 0, 1, 16);
1135
1136 if ( pwd != 1 ) {
1137 usePwd = true;
1138 }
1139
1140 // read word 0 (chip info)
1141 // block 0 can be read even without a password.
1142 if ( !EM4x05Block0Test(&wordData) )
1143 return -1;
1144
1145 uint8_t chipType = (wordData >> 1) & 0xF;
1146 uint8_t cap = (wordData >> 5) & 3;
1147 uint16_t custCode = (wordData >> 9) & 0x3FF;
1148
1149 // read word 1 (serial #) doesn't need pwd
1150 wordData = 0;
1151 if (EM4x05ReadWord_ext(1, 0, false, &wordData) != 1) {
1152 //failed, but continue anyway...
1153 }
1154 printEM4x05info(chipType, cap, custCode, wordData);
1155
1156 // read word 4 (config block)
1157 // needs password if one is set
1158 wordData = 0;
1159 if ( EM4x05ReadWord_ext(4, pwd, usePwd, &wordData) != 1 ) {
1160 //failed
1161 PrintAndLog("Config block read failed - might be password protected.");
1162 return 0;
1163 }
1164 printEM4x05config(wordData);
1165
1166 // read word 14 and 15 to see which is being used for the protection bits
1167 wordData = 0;
1168 if ( EM4x05ReadWord_ext(14, pwd, usePwd, &wordData) != 1 ) {
1169 //failed
1170 return 0;
1171 }
1172 // if status bit says this is not the used protection word
1173 if (!(wordData & 0x8000)) {
1174 if ( EM4x05ReadWord_ext(15, pwd, usePwd, &wordData) != 1 ) {
1175 //failed
1176 return 0;
1177 }
1178 }
1179 if (!(wordData & 0x8000)) {
1180 //something went wrong
1181 return 0;
1182 }
1183 printEM4x05ProtectionBits(wordData);
1184
1185 return 1;
1186 }
1187
1188
1189 static command_t CommandTable[] =
1190 {
1191 {"help", CmdHelp, 1, "This help"},
1192 {"410xread", CmdEMdemodASK, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
1193 {"410xdemod", CmdAskEM410xDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
1194 {"410xsim", CmdEM410xSim, 0, "<UID> [clock rate] -- Simulate EM410x tag"},
1195 {"410xwatch", CmdEM410xWatch, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
1196 {"410xspoof", CmdEM410xWatchnSpoof, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
1197 {"410xwrite", CmdEM410xWrite, 0, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
1198 {"4x05dump", CmdEM4x05dump, 0, "(pwd) -- Read EM4x05/EM4x69 all word data"},
1199 {"4x05info", CmdEM4x05info, 0, "(pwd) -- Get info from EM4x05/EM4x69 tag"},
1200 {"4x05readword", CmdEM4x05ReadWord, 0, "<Word> (pwd) -- Read EM4x05/EM4x69 word data"},
1201 {"4x05writeword", CmdEM4x05WriteWord, 0, "<Word> <data> (pwd) -- Write EM4x05/EM4x69 word data"},
1202 {"4x50read", CmdEM4x50Read, 1, "demod data from EM4x50 tag from the graph buffer"},
1203 {NULL, NULL, 0, NULL}
1204 };
1205
1206 int CmdLFEM4X(const char *Cmd)
1207 {
1208 CmdsParse(CommandTable, Cmd);
1209 return 0;
1210 }
1211
1212 int CmdHelp(const char *Cmd)
1213 {
1214 CmdsHelp(CommandTable);
1215 return 0;
1216 }
Impressum, Datenschutz