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