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