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