]>
Commit | Line | Data |
---|---|---|
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 | // High frequency Legic commands | |
9 | //----------------------------------------------------------------------------- | |
10 | #include "cmdhflegic.h" | |
11 | ||
12 | static int CmdHelp(const char *Cmd); | |
13 | ||
14 | #define MAX_LENGTH 1024 | |
15 | ||
16 | int usage_legic_calccrc(void){ | |
17 | PrintAndLog("Calculates the legic crc8/crc16 on the given data."); | |
18 | PrintAndLog("There must be an even number of hexsymbols as input."); | |
19 | PrintAndLog("Usage: hf legic crc [h] d <data> u <uidcrc> c <8|16>"); | |
20 | PrintAndLog("Options:"); | |
21 | PrintAndLog(" h : this help"); | |
22 | PrintAndLog(" d <data> : (hex symbols) bytes to calculate crc over"); | |
23 | PrintAndLog(" u <uidcrc> : MCC hexbyte"); | |
24 | PrintAndLog(" c <8|16> : Crc type"); | |
25 | PrintAndLog(""); | |
26 | PrintAndLog("Samples:"); | |
27 | PrintAndLog(" hf legic crc b deadbeef1122"); | |
28 | PrintAndLog(" hf legic crc b deadbeef1122 u 9A c 16"); | |
29 | return 0; | |
30 | } | |
31 | int usage_legic_rdmem(void){ | |
32 | PrintAndLog("Read data from a legic tag."); | |
33 | PrintAndLog("Usage: hf legic rdmem [h] <offset> <length> <IV>"); | |
34 | PrintAndLog("Options:"); | |
35 | PrintAndLog(" h : this help"); | |
36 | PrintAndLog(" <offset> : (hex) offset in data array to start download from"); | |
37 | PrintAndLog(" <length> : (hex) number of bytes to read"); | |
38 | PrintAndLog(" <IV> : (hex) (optional) Initialization vector to use. Must be odd and 7bits max"); | |
39 | PrintAndLog(""); | |
40 | PrintAndLog("Samples:"); | |
41 | PrintAndLog(" hf legic rdmem 0 16 - reads from byte[0] 0x16 bytes(system header)"); | |
42 | PrintAndLog(" hf legic rdmem 0 4 55 - reads from byte[0] 0x4 bytes with IV 0x55"); | |
43 | PrintAndLog(" hf legic rdmem 0 100 55 - reads 0x100 bytes with IV 0x55"); | |
44 | return 0; | |
45 | } | |
46 | int usage_legic_sim(void){ | |
47 | PrintAndLog("Simulates a LEGIC Prime tag. MIM22, MIM256, MIM1024 types can be emulated"); | |
48 | PrintAndLog("Use eload/esave to upload a dump into emulator memory"); | |
49 | PrintAndLog("Usage: hf legic sim [h] <tagtype> <phase> <frame> <reqresp>"); | |
50 | PrintAndLog("Options:"); | |
51 | PrintAndLog(" h : this help"); | |
52 | PrintAndLog(" <tagtype> : 0 = MIM22"); | |
53 | PrintAndLog(" : 1 = MIM256 (default)"); | |
54 | PrintAndLog(" : 2 = MIM1024"); | |
55 | PrintAndLog(" <phase> : phase drift"); | |
56 | PrintAndLog(" <frame> : frame drift"); | |
57 | PrintAndLog(" <reqresp> : reqresp drift"); | |
58 | PrintAndLog(""); | |
59 | PrintAndLog("Samples:"); | |
60 | PrintAndLog(" hf legic sim"); | |
61 | PrintAndLog(" hf legic sim "); | |
62 | return 0; | |
63 | } | |
64 | int usage_legic_write(void){ | |
65 | PrintAndLog("Write data to a LEGIC Prime tag. It autodetects tagsize to make sure size"); | |
66 | PrintAndLog("Usage: hf legic write [h] o <offset> d <data (hex symbols)>"); | |
67 | PrintAndLog("Options:"); | |
68 | PrintAndLog(" h : this help"); | |
69 | PrintAndLog(" o <offset> : (hex) offset in data array to start writing"); | |
70 | //PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)"); | |
71 | PrintAndLog(" d <data> : (hex symbols) bytes to write "); | |
72 | PrintAndLog(""); | |
73 | PrintAndLog("Samples:"); | |
74 | PrintAndLog(" hf legic write o 10 d 11223344 - Write 0x11223344 starting from offset 0x10"); | |
75 | return 0; | |
76 | } | |
77 | int usage_legic_reader(void){ | |
78 | PrintAndLog("Read UID and type information from a legic tag."); | |
79 | PrintAndLog("Usage: hf legic reader [h]"); | |
80 | PrintAndLog("Options:"); | |
81 | PrintAndLog(" h : this help"); | |
82 | PrintAndLog(""); | |
83 | PrintAndLog("Samples:"); | |
84 | PrintAndLog(" hf legic reader"); | |
85 | return 0; | |
86 | } | |
87 | int usage_legic_info(void){ | |
88 | PrintAndLog("Reads information from a legic prime tag."); | |
89 | PrintAndLog("Shows systemarea, user areas etc"); | |
90 | PrintAndLog("Usage: hf legic info [h]"); | |
91 | PrintAndLog("Options:"); | |
92 | PrintAndLog(" h : this help"); | |
93 | PrintAndLog(""); | |
94 | PrintAndLog("Samples:"); | |
95 | PrintAndLog(" hf legic info"); | |
96 | return 0; | |
97 | } | |
98 | int usage_legic_dump(void){ | |
99 | PrintAndLog("Reads all pages from LEGIC Prime MIM22, MIM256, MIM1024"); | |
100 | PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`"); | |
101 | PrintAndLog("It autodetects card type.\n"); | |
102 | PrintAndLog("Usage: hf legic dump [h] o <filename w/o .bin>"); | |
103 | PrintAndLog("Options:"); | |
104 | PrintAndLog(" h : this help"); | |
105 | PrintAndLog(" o <filename> : filename w/o '.bin' to dump bytes"); | |
106 | PrintAndLog(""); | |
107 | PrintAndLog("Samples:"); | |
108 | PrintAndLog(" hf legic dump"); | |
109 | PrintAndLog(" hf legic dump o myfile"); | |
110 | return 0; | |
111 | } | |
112 | int usage_legic_restore(void){ | |
113 | PrintAndLog("Reads binary file and it autodetects card type and verifies that the file has the same size"); | |
114 | PrintAndLog("Then write the data back to card. All bytes except the first 7bytes [UID(4) MCC(1) DCF(2)]\n"); | |
115 | PrintAndLog("Usage: hf legic restore [h] i <filename w/o .bin>"); | |
116 | PrintAndLog("Options:"); | |
117 | PrintAndLog(" h : this help"); | |
118 | PrintAndLog(" i <filename> : filename w/o '.bin' to restore bytes on to card from"); | |
119 | PrintAndLog(""); | |
120 | PrintAndLog("Samples:"); | |
121 | PrintAndLog(" hf legic restore i myfile"); | |
122 | return 0; | |
123 | } | |
124 | int usage_legic_eload(void){ | |
125 | PrintAndLog("It loads binary dump from the file `filename.bin`"); | |
126 | PrintAndLog("Usage: hf legic eload [h] [card memory] <file name w/o `.bin`>"); | |
127 | PrintAndLog("Options:"); | |
128 | PrintAndLog(" h : this help"); | |
129 | PrintAndLog(" [card memory] : 0 = MIM22"); | |
130 | PrintAndLog(" : 1 = MIM256 (default)"); | |
131 | PrintAndLog(" : 2 = MIM1024"); | |
132 | PrintAndLog(" <filename> : filename w/o .bin to load"); | |
133 | PrintAndLog(""); | |
134 | PrintAndLog("Samples:"); | |
135 | PrintAndLog(" hf legic eload 2 myfile"); | |
136 | return 0; | |
137 | } | |
138 | int usage_legic_esave(void){ | |
139 | PrintAndLog("It saves binary dump into the file `filename.bin` or `cardID.bin`"); | |
140 | PrintAndLog(" Usage: hf legic esave [h] [card memory] [file name w/o `.bin`]"); | |
141 | PrintAndLog("Options:"); | |
142 | PrintAndLog(" h : this help"); | |
143 | PrintAndLog(" [card memory] : 0 = MIM22"); | |
144 | PrintAndLog(" : 1 = MIM256 (default)"); | |
145 | PrintAndLog(" : 2 = MIM1024"); | |
146 | PrintAndLog(" <filename> : filename w/o .bin to load"); | |
147 | PrintAndLog(""); | |
148 | PrintAndLog("Samples:"); | |
149 | PrintAndLog(" hf legic esave 2 myfile"); | |
150 | return 0; | |
151 | } | |
152 | ||
153 | /* | |
154 | * Output BigBuf and deobfuscate LEGIC RF tag data. | |
155 | * This is based on information given in the talk held | |
156 | * by Henryk Ploetz and Karsten Nohl at 26c3 | |
157 | */ | |
158 | int CmdLegicInfo(const char *Cmd) { | |
159 | ||
160 | char cmdp = param_getchar(Cmd, 0); | |
161 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_info(); | |
162 | ||
163 | int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0; | |
164 | int crc = 0, wrp = 0, wrc = 0; | |
165 | uint8_t stamp_len = 0; | |
166 | uint8_t data[1024]; // receiver buffer | |
167 | char token_type[5] = {0,0,0,0,0}; | |
168 | int dcf = 0; | |
169 | int bIsSegmented = 0; | |
170 | ||
171 | CmdLegicRdmem("0 22 55"); | |
172 | ||
173 | // copy data from device | |
174 | GetEMLFromBigBuf(data, sizeof(data), 0); | |
175 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ | |
176 | PrintAndLog("Command execute timeout"); | |
177 | return 1; | |
178 | } | |
179 | ||
180 | // Output CDF System area (9 bytes) plus remaining header area (12 bytes) | |
181 | crc = data[4]; | |
182 | uint32_t calc_crc = CRC8Legic(data, 4); | |
183 | ||
184 | PrintAndLog("\nCDF: System Area"); | |
185 | PrintAndLog("------------------------------------------------------"); | |
186 | PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s", | |
187 | data[0], | |
188 | data[1], | |
189 | data[2], | |
190 | data[3], | |
191 | data[4], | |
192 | (calc_crc == crc) ? "OK":"Fail" | |
193 | ); | |
194 | ||
195 | // MCD = Manufacturer ID (should be list meaning something?) | |
196 | ||
197 | token_type[0] = 0; | |
198 | dcf = ((int)data[6] << 8) | (int)data[5]; | |
199 | ||
200 | // New unwritten media? | |
201 | if(dcf == 0xFFFF) { | |
202 | ||
203 | PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)", | |
204 | dcf, | |
205 | data[5], | |
206 | data[6] | |
207 | ); | |
208 | ||
209 | } else if (dcf > 60000) { // Master token? | |
210 | ||
211 | int fl = 0; | |
212 | ||
213 | if(data[6] == 0xec) { | |
214 | strncpy(token_type, "XAM", sizeof(token_type)); | |
215 | fl = 1; | |
216 | stamp_len = 0x0c - (data[5] >> 4); | |
217 | } else { | |
218 | switch (data[5] & 0x7f) { | |
219 | case 0x00 ... 0x2f: | |
220 | strncpy(token_type, "IAM", sizeof(token_type)); | |
221 | fl = (0x2f - (data[5] & 0x7f)) + 1; | |
222 | break; | |
223 | case 0x30 ... 0x6f: | |
224 | strncpy(token_type, "SAM", sizeof(token_type)); | |
225 | fl = (0x6f - (data[5] & 0x7f)) + 1; | |
226 | break; | |
227 | case 0x70 ... 0x7f: | |
228 | strncpy(token_type, "GAM", sizeof(token_type)); | |
229 | fl = (0x7f - (data[5] & 0x7f)) + 1; | |
230 | break; | |
231 | } | |
232 | ||
233 | stamp_len = 0xfc - data[6]; | |
234 | } | |
235 | ||
236 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u", | |
237 | dcf, | |
238 | data[5], | |
239 | data[6], | |
240 | token_type, | |
241 | (data[5] & 0x80 )>> 7, | |
242 | stamp_len, | |
243 | fl | |
244 | ); | |
245 | ||
246 | } else { // Is IM(-S) type of card... | |
247 | ||
248 | if(data[7] == 0x9F && data[8] == 0xFF) { | |
249 | bIsSegmented = 1; | |
250 | strncpy(token_type, "IM-S", sizeof(token_type)); | |
251 | } else { | |
252 | strncpy(token_type, "IM", sizeof(token_type)); | |
253 | } | |
254 | ||
255 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)", | |
256 | dcf, | |
257 | data[5], | |
258 | data[6], | |
259 | token_type, | |
260 | (data[5]&0x80) >> 7 | |
261 | ); | |
262 | } | |
263 | ||
264 | // Makes no sence to show this on blank media... | |
265 | if(dcf != 0xFFFF) { | |
266 | ||
267 | if(bIsSegmented) { | |
268 | PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x", | |
269 | data[7] & 0x0f, | |
270 | (data[7] & 0x70) >> 4, | |
271 | (data[7] & 0x80) >> 7, | |
272 | data[8] | |
273 | ); | |
274 | } | |
275 | ||
276 | // Header area is only available on IM-S cards, on master tokens this data is the master token data itself | |
277 | if(bIsSegmented || dcf > 60000) { | |
278 | if(dcf > 60000) { | |
279 | PrintAndLog("Master token data"); | |
280 | PrintAndLog("%s", sprint_hex(data+8, 14)); | |
281 | } else { | |
282 | PrintAndLog("Remaining Header Area"); | |
283 | PrintAndLog("%s", sprint_hex(data+9, 13)); | |
284 | } | |
285 | } | |
286 | } | |
287 | ||
288 | uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0}; | |
289 | uint32_t segCalcCRC = 0; | |
290 | uint32_t segCRC = 0; | |
291 | ||
292 | // Data card? | |
293 | if(dcf <= 60000) { | |
294 | ||
295 | PrintAndLog("\nADF: User Area"); | |
296 | PrintAndLog("------------------------------------------------------"); | |
297 | ||
298 | if(bIsSegmented) { | |
299 | ||
300 | // Data start point on segmented cards | |
301 | i = 22; | |
302 | ||
303 | // decode segments | |
304 | for (segmentNum=1; segmentNum < 128; segmentNum++ ) | |
305 | { | |
306 | segment_len = ((data[i+1] ^ crc) & 0x0f) * 256 + (data[i] ^ crc); | |
307 | segment_flag = ((data[i+1] ^ crc) & 0xf0) >> 4; | |
308 | wrp = (data[i+2] ^ crc); | |
309 | wrc = ((data[i+3] ^ crc) & 0x70) >> 4; | |
310 | ||
311 | bool hasWRC = (wrc > 0); | |
312 | bool hasWRP = (wrp > wrc); | |
313 | int wrp_len = (wrp - wrc); | |
314 | int remain_seg_payload_len = (segment_len - wrp - 5); | |
315 | ||
316 | // validate segment-crc | |
317 | segCrcBytes[0]=data[0]; //uid0 | |
318 | segCrcBytes[1]=data[1]; //uid1 | |
319 | segCrcBytes[2]=data[2]; //uid2 | |
320 | segCrcBytes[3]=data[3]; //uid3 | |
321 | segCrcBytes[4]=(data[i] ^ crc); //hdr0 | |
322 | segCrcBytes[5]=(data[i+1] ^ crc); //hdr1 | |
323 | segCrcBytes[6]=(data[i+2] ^ crc); //hdr2 | |
324 | segCrcBytes[7]=(data[i+3] ^ crc); //hdr3 | |
325 | ||
326 | segCalcCRC = CRC8Legic(segCrcBytes, 8); | |
327 | segCRC = data[i+4] ^ crc; | |
328 | ||
329 | PrintAndLog("Segment %02u \nraw header | 0x%02X 0x%02X 0x%02X 0x%02X \nSegment len: %u, Flag: 0x%X (valid:%01u, last:%01u), WRP: %02u, WRC: %02u, RD: %01u, CRC: 0x%02X (%s)", | |
330 | segmentNum, | |
331 | data[i] ^ crc, | |
332 | data[i+1] ^ crc, | |
333 | data[i+2] ^ crc, | |
334 | data[i+3] ^ crc, | |
335 | segment_len, | |
336 | segment_flag, | |
337 | (segment_flag & 0x4) >> 2, | |
338 | (segment_flag & 0x8) >> 3, | |
339 | wrp, | |
340 | wrc, | |
341 | ((data[i+3]^crc) & 0x80) >> 7, | |
342 | segCRC, | |
343 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
344 | ); | |
345 | ||
346 | i += 5; | |
347 | ||
348 | if ( hasWRC ) { | |
349 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
350 | PrintAndLog("\nrow | data"); | |
351 | PrintAndLog("-----+------------------------------------------------"); | |
352 | ||
353 | for ( k=i; k < (i + wrc); ++k) | |
354 | data[k] ^= crc; | |
355 | ||
356 | print_hex_break( data+i, wrc, 16); | |
357 | ||
358 | i += wrc; | |
359 | } | |
360 | ||
361 | if ( hasWRP ) { | |
362 | PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len); | |
363 | PrintAndLog("\nrow | data"); | |
364 | PrintAndLog("-----+------------------------------------------------"); | |
365 | ||
366 | for (k=i; k < (i+wrp_len); ++k) | |
367 | data[k] ^= crc; | |
368 | ||
369 | print_hex_break( data+i, wrp_len, 16); | |
370 | ||
371 | i += wrp_len; | |
372 | ||
373 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
374 | if( wrp_len == 8 ) | |
375 | PrintAndLog("Card ID: %2X%02X%02X", data[i-4]^crc, data[i-3]^crc, data[i-2]^crc); | |
376 | } | |
377 | ||
378 | PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len); | |
379 | PrintAndLog("\nrow | data"); | |
380 | PrintAndLog("-----+------------------------------------------------"); | |
381 | ||
382 | for ( k=i; k < (i+remain_seg_payload_len); ++k) | |
383 | data[k] ^= crc; | |
384 | ||
385 | print_hex_break( data+i, remain_seg_payload_len, 16); | |
386 | ||
387 | i += remain_seg_payload_len; | |
388 | ||
389 | PrintAndLog("-----+------------------------------------------------\n"); | |
390 | ||
391 | // end with last segment | |
392 | if (segment_flag & 0x8) return 0; | |
393 | ||
394 | } // end for loop | |
395 | ||
396 | } else { | |
397 | ||
398 | // Data start point on unsegmented cards | |
399 | i = 8; | |
400 | ||
401 | wrp = data[7] & 0x0F; | |
402 | wrc = (data[7] & 0x70) >> 4; | |
403 | ||
404 | bool hasWRC = (wrc > 0); | |
405 | bool hasWRP = (wrp > wrc); | |
406 | int wrp_len = (wrp - wrc); | |
407 | int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!? | |
408 | ||
409 | PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u", | |
410 | wrp, | |
411 | wrc, | |
412 | (data[7] & 0x80) >> 7 | |
413 | ); | |
414 | ||
415 | if ( hasWRC ) { | |
416 | PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc); | |
417 | PrintAndLog("\nrow | data"); | |
418 | PrintAndLog("-----+------------------------------------------------"); | |
419 | print_hex_break( data+i, wrc, 16); | |
420 | i += wrc; | |
421 | } | |
422 | ||
423 | if ( hasWRP ) { | |
424 | PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len); | |
425 | PrintAndLog("\nrow | data"); | |
426 | PrintAndLog("-----+------------------------------------------------"); | |
427 | print_hex_break( data + i, wrp_len, 16); | |
428 | i += wrp_len; | |
429 | ||
430 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
431 | if( wrp_len == 8 ) | |
432 | PrintAndLog("Card ID: %2X%02X%02X", data[i-4], data[i-3], data[i-2]); | |
433 | } | |
434 | ||
435 | PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len); | |
436 | PrintAndLog("\nrow | data"); | |
437 | PrintAndLog("-----+------------------------------------------------"); | |
438 | print_hex_break( data + i, remain_seg_payload_len, 16); | |
439 | i += remain_seg_payload_len; | |
440 | ||
441 | PrintAndLog("-----+------------------------------------------------\n"); | |
442 | } | |
443 | } | |
444 | return 0; | |
445 | } | |
446 | ||
447 | // params: | |
448 | // offset in data memory | |
449 | // number of bytes to read | |
450 | int CmdLegicRdmem(const char *Cmd) { | |
451 | ||
452 | char cmdp = param_getchar(Cmd, 0); | |
453 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_rdmem(); | |
454 | ||
455 | uint32_t offset = 0, len = 0, IV = 1; | |
456 | sscanf(Cmd, "%x %x %x", &offset, &len, &IV); | |
457 | ||
458 | // tagtype | |
459 | legic_card_select_t card; | |
460 | if (legic_get_type(&card)) { | |
461 | PrintAndLog("Failed to identify tagtype"); | |
462 | return 1; | |
463 | } | |
464 | ||
465 | legic_print_type(card.cardsize, 0); | |
466 | ||
467 | // OUT-OF-BOUNDS check | |
468 | // UID 4 bytes can't be written to. | |
469 | if ( len + offset >= card.cardsize ) { | |
470 | len = card.cardsize - offset; | |
471 | PrintAndLog("Out-of-bounds, Cardsize = %d, Trunc offset+len = %d", card.cardsize, len + offset); | |
472 | } | |
473 | ||
474 | legic_chk_iv(&IV); | |
475 | ||
476 | UsbCommand c = {CMD_READER_LEGIC_RF, {offset, len, IV}}; | |
477 | clearCommandBuffer(); | |
478 | SendCommand(&c); | |
479 | UsbCommand resp; | |
480 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 3000) ) { | |
481 | PrintAndLog("command execution time out"); | |
482 | return 1; | |
483 | } | |
484 | ||
485 | uint8_t isOK = resp.arg[0] & 0xFF; | |
486 | uint16_t readlen = resp.arg[1]; | |
487 | if ( !isOK ) { | |
488 | PrintAndLog("failed reading tag"); | |
489 | return 2; | |
490 | } | |
491 | ||
492 | uint8_t *data = malloc(readlen); | |
493 | if ( !data ){ | |
494 | PrintAndLog("Cannot allocate memory"); | |
495 | return 2; | |
496 | } | |
497 | ||
498 | if ( readlen != len ) | |
499 | PrintAndLog("Fail, only managed to read 0x%02X bytes", readlen); | |
500 | ||
501 | // copy data from device | |
502 | GetEMLFromBigBuf(data, readlen, 0); | |
503 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)){ | |
504 | PrintAndLog("Command execute timeout"); | |
505 | free(data); | |
506 | return 1; | |
507 | } | |
508 | ||
509 | PrintAndLog("\n ## | 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"); | |
510 | PrintAndLog("-----+------------------------------------------------------------------------------------------------"); | |
511 | print_hex_break( data, readlen, 32); | |
512 | free(data); | |
513 | return 0; | |
514 | } | |
515 | ||
516 | // should say which tagtype | |
517 | // should load a tag to device mem. | |
518 | // int phase, int frame, int reqresp | |
519 | int CmdLegicRfSim(const char *Cmd) { | |
520 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; | |
521 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); | |
522 | clearCommandBuffer(); | |
523 | SendCommand(&c); | |
524 | return 0; | |
525 | } | |
526 | ||
527 | int CmdLegicRfWrite(const char *Cmd) { | |
528 | ||
529 | uint8_t *data = NULL; | |
530 | uint8_t cmdp = 0; | |
531 | bool errors = false; | |
532 | int len = 0, bg, en; | |
533 | uint32_t offset = 0, IV = 0x55; | |
534 | ||
535 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
536 | switch(param_getchar(Cmd, cmdp)) { | |
537 | case 'd': | |
538 | case 'D': | |
539 | // peek at length of the input string so we can | |
540 | // figure out how many elements to malloc in "data" | |
541 | bg=en=0; | |
542 | if (param_getptr(Cmd, &bg, &en, cmdp+1)) { | |
543 | errors = true; | |
544 | break; | |
545 | } | |
546 | len = (en - bg + 1); | |
547 | ||
548 | // check that user entered even number of characters | |
549 | // for hex data string | |
550 | if (len & 1) { | |
551 | errors = true; | |
552 | break; | |
553 | } | |
554 | ||
555 | // limit number of bytes to write. This is not a 'restore' command. | |
556 | if ( (len>>1) > 100 ){ | |
557 | PrintAndLog("Max bound on 100bytes to write a one time."); | |
558 | PrintAndLog("Use the 'hf legic restore' command if you want to write the whole tag at once"); | |
559 | errors = true; | |
560 | } | |
561 | ||
562 | // it's possible for user to accidentally enter "b" parameter | |
563 | // more than once - we have to clean previous malloc | |
564 | if (data) | |
565 | free(data); | |
566 | ||
567 | data = malloc(len >> 1); | |
568 | if ( data == NULL ) { | |
569 | PrintAndLog("Can't allocate memory. exiting"); | |
570 | errors = true; | |
571 | break; | |
572 | } | |
573 | ||
574 | if (param_gethex(Cmd, cmdp+1, data, len)) { | |
575 | errors = true; | |
576 | break; | |
577 | } | |
578 | ||
579 | len >>= 1; | |
580 | cmdp += 2; | |
581 | break; | |
582 | case 'o': | |
583 | case 'O': | |
584 | offset = param_get32ex(Cmd, cmdp+1, 4, 16); | |
585 | cmdp += 2; | |
586 | break; | |
587 | case 'h': | |
588 | case 'H': | |
589 | errors = true; | |
590 | break; | |
591 | default: | |
592 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
593 | errors = true; | |
594 | break; | |
595 | } | |
596 | if (errors) break; | |
597 | } | |
598 | //Validations | |
599 | if (errors){ | |
600 | if (data) | |
601 | free(data); | |
602 | return usage_legic_write(); | |
603 | } | |
604 | ||
605 | // tagtype | |
606 | legic_card_select_t card; | |
607 | if (legic_get_type(&card)) { | |
608 | PrintAndLog("Failed to identify tagtype"); | |
609 | return -1; | |
610 | } | |
611 | ||
612 | legic_print_type(card.cardsize, 0); | |
613 | ||
614 | // OUT-OF-BOUNDS checks | |
615 | // UID 4+1 bytes can't be written to. | |
616 | if ( offset < 5 ) { | |
617 | PrintAndLog("Out-of-bounds, bytes 0-1-2-3-4 can't be written to. Offset = %d", offset); | |
618 | return -2; | |
619 | } | |
620 | ||
621 | if ( len + offset >= card.cardsize ) { | |
622 | PrintAndLog("Out-of-bounds, Cardsize = %d, [offset+len = %d ]", card.cardsize, len + offset); | |
623 | return -2; | |
624 | } | |
625 | ||
626 | if (offset == 5 || offset == 6) { | |
627 | PrintAndLog("############# DANGER ################"); | |
628 | PrintAndLog("# changing the DCF is irreversible #"); | |
629 | PrintAndLog("#####################################"); | |
630 | PrintAndLog("do you really want to continue? y(es) n(o)"); | |
631 | char answer; | |
632 | sscanf("%c", &answer); | |
633 | bool exit = !(answer == 'n' || answer == 'N'); | |
634 | if (exit) | |
635 | return 0; | |
636 | printf("ICE DCF: %c answer, %d\n", answer, exit); | |
637 | return 0; | |
638 | } | |
639 | ||
640 | legic_chk_iv(&IV); | |
641 | ||
642 | PrintAndLog("Writing to tag"); | |
643 | ||
644 | UsbCommand c = {CMD_WRITER_LEGIC_RF, {offset, len, IV}}; | |
645 | memcpy(c.d.asBytes, data, len); | |
646 | UsbCommand resp; | |
647 | clearCommandBuffer(); | |
648 | SendCommand(&c); | |
649 | ||
650 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { | |
651 | PrintAndLog("command execution time out"); | |
652 | return 1; | |
653 | } | |
654 | uint8_t isOK = resp.arg[0] & 0xFF; | |
655 | if ( !isOK ) { | |
656 | PrintAndLog("failed writing tag"); | |
657 | return 1; | |
658 | } | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
663 | int CmdLegicCalcCrc(const char *Cmd){ | |
664 | ||
665 | uint8_t *data = NULL; | |
666 | uint8_t cmdp = 0, uidcrc = 0, type=0; | |
667 | bool errors = false; | |
668 | int len = 0; | |
669 | int bg, en; | |
670 | ||
671 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
672 | switch(param_getchar(Cmd, cmdp)) { | |
673 | case 'b': | |
674 | case 'B': | |
675 | // peek at length of the input string so we can | |
676 | // figure out how many elements to malloc in "data" | |
677 | bg=en=0; | |
678 | if (param_getptr(Cmd, &bg, &en, cmdp+1)) { | |
679 | errors = true; | |
680 | break; | |
681 | } | |
682 | len = (en - bg + 1); | |
683 | ||
684 | // check that user entered even number of characters | |
685 | // for hex data string | |
686 | if (len & 1) { | |
687 | errors = true; | |
688 | break; | |
689 | } | |
690 | ||
691 | // it's possible for user to accidentally enter "b" parameter | |
692 | // more than once - we have to clean previous malloc | |
693 | if (data) free(data); | |
694 | data = malloc(len >> 1); | |
695 | if ( data == NULL ) { | |
696 | PrintAndLog("Can't allocate memory. exiting"); | |
697 | errors = true; | |
698 | break; | |
699 | } | |
700 | ||
701 | if (param_gethex(Cmd, cmdp+1, data, len)) { | |
702 | errors = true; | |
703 | break; | |
704 | } | |
705 | ||
706 | len >>= 1; | |
707 | cmdp += 2; | |
708 | break; | |
709 | case 'u': | |
710 | case 'U': | |
711 | uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16); | |
712 | cmdp += 2; | |
713 | break; | |
714 | case 'c': | |
715 | case 'C': | |
716 | type = param_get8ex(Cmd, cmdp+1, 0, 10); | |
717 | cmdp += 2; | |
718 | break; | |
719 | case 'h': | |
720 | case 'H': | |
721 | errors = true; | |
722 | break; | |
723 | default: | |
724 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
725 | errors = true; | |
726 | break; | |
727 | } | |
728 | if (errors) break; | |
729 | } | |
730 | //Validations | |
731 | if (errors){ | |
732 | if (data) free(data); | |
733 | return usage_legic_calccrc(); | |
734 | } | |
735 | ||
736 | switch (type){ | |
737 | case 16: | |
738 | PrintAndLog("Legic crc16: %X", CRC16Legic(data, len, uidcrc)); | |
739 | break; | |
740 | default: | |
741 | PrintAndLog("Legic crc8: %X", CRC8Legic(data, len) ); | |
742 | break; | |
743 | } | |
744 | ||
745 | if (data) free(data); | |
746 | return 0; | |
747 | } | |
748 | ||
749 | int legic_print_type(uint32_t tagtype, uint8_t spaces){ | |
750 | char spc[11] = " "; | |
751 | spc[10]=0x00; | |
752 | char *spacer = spc + (10-spaces); | |
753 | ||
754 | if ( tagtype == 22 ) | |
755 | PrintAndLog("%sTYPE : MIM%d card (outdated)", spacer, tagtype); | |
756 | else if ( tagtype == 256 ) | |
757 | PrintAndLog("%sTYPE : MIM%d card (234 bytes)", spacer, tagtype); | |
758 | else if ( tagtype == 1024 ) | |
759 | PrintAndLog("%sTYPE : MIM%d card (1002 bytes)", spacer, tagtype); | |
760 | else | |
761 | PrintAndLog("%sTYPE : Unknown %06x", spacer, tagtype); | |
762 | return 0; | |
763 | } | |
764 | int legic_get_type(legic_card_select_t *card){ | |
765 | ||
766 | if ( card == NULL ) return 1; | |
767 | ||
768 | UsbCommand c = {CMD_LEGIC_INFO, {0,0,0}}; | |
769 | clearCommandBuffer(); | |
770 | SendCommand(&c); | |
771 | UsbCommand resp; | |
772 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 500)) | |
773 | return 2; | |
774 | ||
775 | uint8_t isOK = resp.arg[0] & 0xFF; | |
776 | if ( !isOK ) | |
777 | return 3; | |
778 | ||
779 | memcpy(card, (legic_card_select_t *)resp.d.asBytes, sizeof(legic_card_select_t)); | |
780 | return 0; | |
781 | } | |
782 | void legic_chk_iv(uint32_t *iv){ | |
783 | if ( (*iv & 0x7F) != *iv ){ | |
784 | *iv &= 0x7F; | |
785 | PrintAndLog("Truncating IV to 7bits, %u", *iv); | |
786 | } | |
787 | // IV must be odd | |
788 | if ( (*iv & 1) == 0 ){ | |
789 | *iv |= 0x01; | |
790 | PrintAndLog("LSB of IV must be SET %u", *iv); | |
791 | } | |
792 | } | |
793 | void legic_seteml(uint8_t *src, uint32_t offset, uint32_t numofbytes) { | |
794 | size_t len = 0; | |
795 | UsbCommand c = {CMD_LEGIC_ESET, {0, 0, 0}}; | |
796 | for(size_t i = 0; i < numofbytes; i += USB_CMD_DATA_SIZE) { | |
797 | ||
798 | len = MIN((numofbytes - i), USB_CMD_DATA_SIZE); | |
799 | c.arg[0] = i; // offset | |
800 | c.arg[1] = len; // number of bytes | |
801 | memcpy(c.d.asBytes, src+i, len); | |
802 | clearCommandBuffer(); | |
803 | SendCommand(&c); | |
804 | } | |
805 | } | |
806 | ||
807 | int HFLegicReader(const char *Cmd, bool verbose) { | |
808 | ||
809 | char cmdp = param_getchar(Cmd, 0); | |
810 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_reader(); | |
811 | ||
812 | legic_card_select_t card; | |
813 | switch(legic_get_type(&card)){ | |
814 | case 1: | |
815 | if ( verbose ) PrintAndLog("command execution time out"); | |
816 | return 1; | |
817 | case 2: | |
818 | case 3: | |
819 | if ( verbose ) PrintAndLog("legic card select failed"); | |
820 | return 2; | |
821 | default: break; | |
822 | } | |
823 | PrintAndLog(" UID : %s", sprint_hex(card.uid, sizeof(card.uid))); | |
824 | legic_print_type(card.cardsize, 0); | |
825 | return 0; | |
826 | } | |
827 | int CmdLegicReader(const char *Cmd){ | |
828 | return HFLegicReader(Cmd, TRUE); | |
829 | } | |
830 | ||
831 | int CmdLegicDump(const char *Cmd){ | |
832 | ||
833 | FILE *f; | |
834 | char filename[FILE_PATH_SIZE] = {0x00}; | |
835 | char *fnameptr = filename; | |
836 | size_t fileNlen = 0; | |
837 | bool errors = false; | |
838 | uint16_t dumplen; | |
839 | uint8_t cmdp = 0; | |
840 | ||
841 | memset(filename, 0, sizeof(filename)); | |
842 | ||
843 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
844 | switch(param_getchar(Cmd, cmdp)) { | |
845 | case 'h': | |
846 | case 'H': | |
847 | return usage_legic_dump(); | |
848 | case 'o': | |
849 | case 'O': | |
850 | fileNlen = param_getstr(Cmd, cmdp+1, filename); | |
851 | if (!fileNlen) | |
852 | errors = true; | |
853 | if (fileNlen > FILE_PATH_SIZE-5) | |
854 | fileNlen = FILE_PATH_SIZE-5; | |
855 | cmdp += 2; | |
856 | break; | |
857 | default: | |
858 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
859 | errors = true; | |
860 | break; | |
861 | } | |
862 | if(errors) break; | |
863 | } | |
864 | ||
865 | //Validations | |
866 | if(errors) return usage_legic_dump(); | |
867 | ||
868 | // tagtype | |
869 | legic_card_select_t card; | |
870 | if (legic_get_type(&card)) { | |
871 | PrintAndLog("Failed to identify tagtype"); | |
872 | return -1; | |
873 | } | |
874 | dumplen = card.cardsize; | |
875 | ||
876 | legic_print_type(dumplen, 0); | |
877 | PrintAndLog("Reading tag memory..."); | |
878 | ||
879 | UsbCommand c = {CMD_READER_LEGIC_RF, {0x00, dumplen, 0x55}}; | |
880 | clearCommandBuffer(); | |
881 | SendCommand(&c); | |
882 | UsbCommand resp; | |
883 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) { | |
884 | PrintAndLog("Command execute time-out"); | |
885 | return 1; | |
886 | } | |
887 | ||
888 | uint8_t isOK = resp.arg[0] & 0xFF; | |
889 | if ( !isOK ) { | |
890 | PrintAndLog("Failed dumping tag data"); | |
891 | return 2; | |
892 | } | |
893 | ||
894 | uint16_t readlen = resp.arg[1]; | |
895 | uint8_t *data = malloc(readlen); | |
896 | if (!data) { | |
897 | PrintAndLog("Fail, cannot allocate memory"); | |
898 | return 3; | |
899 | } | |
900 | memset(data, 0, readlen); | |
901 | ||
902 | if ( readlen != dumplen ) | |
903 | PrintAndLog("Fail, only managed to read 0x%02X bytes of 0x%02X", readlen, dumplen); | |
904 | ||
905 | // copy data from device | |
906 | GetEMLFromBigBuf(data, readlen, 0); | |
907 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) { | |
908 | PrintAndLog("Fail, transfer from device time-out"); | |
909 | free(data); | |
910 | return 4; | |
911 | } | |
912 | ||
913 | // user supplied filename? | |
914 | if (fileNlen < 1) | |
915 | sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]); | |
916 | else | |
917 | sprintf(fnameptr + fileNlen,".bin"); | |
918 | ||
919 | if ((f = fopen(filename,"wb")) == NULL) { | |
920 | PrintAndLog("Could not create file name %s", filename); | |
921 | if (data) | |
922 | free(data); | |
923 | return 5; | |
924 | } | |
925 | fwrite(data, 1, readlen, f); | |
926 | fclose(f); | |
927 | free(data); | |
928 | PrintAndLog("Wrote %d bytes to %s", readlen, filename); | |
929 | return 0; | |
930 | } | |
931 | ||
932 | int CmdLegicRestore(const char *Cmd){ | |
933 | ||
934 | FILE *f; | |
935 | char filename[FILE_PATH_SIZE] = {0x00}; | |
936 | char *fnameptr = filename; | |
937 | size_t fileNlen = 0; | |
938 | bool errors = true; | |
939 | uint16_t numofbytes; | |
940 | uint8_t cmdp = 0; | |
941 | ||
942 | memset(filename, 0, sizeof(filename)); | |
943 | ||
944 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
945 | switch(param_getchar(Cmd, cmdp)) { | |
946 | case 'h': | |
947 | case 'H': | |
948 | errors = true; | |
949 | break; | |
950 | case 'i': | |
951 | case 'I': | |
952 | fileNlen = param_getstr(Cmd, cmdp+1, filename); | |
953 | if (!fileNlen) | |
954 | errors = true; | |
955 | else | |
956 | errors = false; | |
957 | ||
958 | if (fileNlen > FILE_PATH_SIZE-5) | |
959 | fileNlen = FILE_PATH_SIZE-5; | |
960 | cmdp += 2; | |
961 | break; | |
962 | default: | |
963 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
964 | errors = true; | |
965 | break; | |
966 | } | |
967 | if (errors) break; | |
968 | } | |
969 | ||
970 | //Validations | |
971 | if(errors) return usage_legic_restore(); | |
972 | ||
973 | // tagtype | |
974 | legic_card_select_t card; | |
975 | if (legic_get_type(&card)) { | |
976 | PrintAndLog("Failed to identify tagtype"); | |
977 | return 1; | |
978 | } | |
979 | numofbytes = card.cardsize; | |
980 | ||
981 | // set up buffer | |
982 | uint8_t *data = malloc(numofbytes); | |
983 | if (!data) { | |
984 | PrintAndLog("Fail, cannot allocate memory"); | |
985 | return 2; | |
986 | } | |
987 | memset(data, 0, numofbytes); | |
988 | ||
989 | legic_print_type(numofbytes, 0); | |
990 | ||
991 | // set up file | |
992 | fnameptr += fileNlen; | |
993 | sprintf(fnameptr, ".bin"); | |
994 | ||
995 | if ((f = fopen(filename,"rb")) == NULL) { | |
996 | PrintAndLog("File %s not found or locked", filename); | |
997 | return 3; | |
998 | } | |
999 | ||
1000 | // verify size of dumpfile is the same as card. | |
1001 | fseek(f, 0, SEEK_END); // seek to end of file | |
1002 | size_t filesize = ftell(f); // get current file pointer | |
1003 | fseek(f, 0, SEEK_SET); // seek back to beginning of file | |
1004 | ||
1005 | if ( filesize != numofbytes) { | |
1006 | PrintAndLog("Fail, filesize and cardsize is not equal. [%u != %u]", filesize, numofbytes); | |
1007 | free(data); | |
1008 | fclose(f); | |
1009 | return 4; | |
1010 | } | |
1011 | ||
1012 | // load file | |
1013 | size_t bytes_read = fread(data, 1, numofbytes, f); | |
1014 | if ( bytes_read == 0){ | |
1015 | PrintAndLog("File reading error"); | |
1016 | free(data); | |
1017 | fclose(f); | |
1018 | return 2; | |
1019 | } | |
1020 | fclose(f); | |
1021 | ||
1022 | PrintAndLog("Restoring %s to card", filename); | |
1023 | ||
1024 | // transfer to device | |
1025 | size_t len = 0; | |
1026 | UsbCommand c = {CMD_WRITER_LEGIC_RF, {0, 0, 0x55}}; | |
1027 | UsbCommand resp; | |
1028 | for(size_t i = 7; i < numofbytes; i += USB_CMD_DATA_SIZE) { | |
1029 | ||
1030 | len = MIN((numofbytes - i), USB_CMD_DATA_SIZE); | |
1031 | c.arg[0] = i; // offset | |
1032 | c.arg[1] = len; // number of bytes | |
1033 | memcpy(c.d.asBytes, data+i, len); | |
1034 | PrintAndLog("offset %d | chunk %d | numofbytes %d", i, len, numofbytes); | |
1035 | clearCommandBuffer(); | |
1036 | SendCommand(&c); | |
1037 | ||
1038 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { | |
1039 | PrintAndLog("command execution time out"); | |
1040 | free(data); | |
1041 | return 1; | |
1042 | } | |
1043 | uint8_t isOK = resp.arg[0] & 0xFF; | |
1044 | if ( !isOK ) { | |
1045 | PrintAndLog("failed writing tag [msg = %u]", resp.arg[1] & 0xFF); | |
1046 | free(data); | |
1047 | return 1; | |
1048 | } | |
1049 | PrintAndLog("Wrote chunk %d - %d", i, len); | |
1050 | } | |
1051 | ||
1052 | free(data); | |
1053 | PrintAndLog("\nWrote %d bytes from file: %s to card", numofbytes, filename); | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | int CmdLegicELoad(const char *Cmd) { | |
1058 | FILE * f; | |
1059 | char filename[FILE_PATH_SIZE]; | |
1060 | char *fnameptr = filename; | |
1061 | int len, numofbytes; | |
1062 | int nameParamNo = 1; | |
1063 | ||
1064 | char cmdp = param_getchar(Cmd, 0); | |
1065 | if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00) | |
1066 | return usage_legic_eload(); | |
1067 | ||
1068 | switch (cmdp) { | |
1069 | case '0' : numofbytes = 22; break; | |
1070 | case '1' : | |
1071 | case '\0': numofbytes = 256; break; | |
1072 | case '2' : numofbytes = 1024; break; | |
1073 | default : numofbytes = 256; nameParamNo = 0;break; | |
1074 | } | |
1075 | ||
1076 | // set up buffer | |
1077 | uint8_t *data = malloc(numofbytes); | |
1078 | if (!data) { | |
1079 | PrintAndLog("Fail, cannot allocate memory"); | |
1080 | return 3; | |
1081 | } | |
1082 | memset(data, 0, numofbytes); | |
1083 | ||
1084 | // set up file | |
1085 | len = param_getstr(Cmd, nameParamNo, filename); | |
1086 | if (len > FILE_PATH_SIZE - 5) | |
1087 | len = FILE_PATH_SIZE - 5; | |
1088 | fnameptr += len; | |
1089 | sprintf(fnameptr, ".bin"); | |
1090 | ||
1091 | // open file | |
1092 | if ((f = fopen(filename,"rb")) == NULL) { | |
1093 | PrintAndLog("File %s not found or locked", filename); | |
1094 | free(data); | |
1095 | return 1; | |
1096 | } | |
1097 | ||
1098 | // load file | |
1099 | size_t bytes_read = fread(data, 1, numofbytes, f); | |
1100 | if ( bytes_read == 0){ | |
1101 | PrintAndLog("File reading error"); | |
1102 | free(data); | |
1103 | fclose(f); | |
1104 | return 2; | |
1105 | } | |
1106 | fclose(f); | |
1107 | ||
1108 | // transfer to device | |
1109 | legic_seteml(data, 0, numofbytes); | |
1110 | ||
1111 | free(data); | |
1112 | PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename); | |
1113 | return 0; | |
1114 | } | |
1115 | ||
1116 | int CmdLegicESave(const char *Cmd) { | |
1117 | FILE *f; | |
1118 | char filename[FILE_PATH_SIZE]; | |
1119 | char *fnameptr = filename; | |
1120 | int fileNlen, numofbytes, nameParamNo = 1; | |
1121 | ||
1122 | memset(filename, 0, sizeof(filename)); | |
1123 | ||
1124 | char cmdp = param_getchar(Cmd, 0); | |
1125 | ||
1126 | if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00) | |
1127 | return usage_legic_esave(); | |
1128 | ||
1129 | switch (cmdp) { | |
1130 | case '0' : numofbytes = 22; break; | |
1131 | case '1' : | |
1132 | case '\0': numofbytes = 256; break; | |
1133 | case '2' : numofbytes = 1024; break; | |
1134 | default : numofbytes = 256; nameParamNo = 0; break; | |
1135 | } | |
1136 | ||
1137 | fileNlen = param_getstr(Cmd, nameParamNo, filename); | |
1138 | ||
1139 | if (fileNlen > FILE_PATH_SIZE - 5) | |
1140 | fileNlen = FILE_PATH_SIZE - 5; | |
1141 | ||
1142 | // set up buffer | |
1143 | uint8_t *data = malloc(numofbytes); | |
1144 | if (!data) { | |
1145 | PrintAndLog("Fail, cannot allocate memory"); | |
1146 | return 3; | |
1147 | } | |
1148 | memset(data, 0, numofbytes); | |
1149 | ||
1150 | // download emulator memory | |
1151 | PrintAndLog("Reading emulator memory..."); | |
1152 | GetEMLFromBigBuf(data, numofbytes, 0); | |
1153 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) { | |
1154 | PrintAndLog("Fail, transfer from device time-out"); | |
1155 | free(data); | |
1156 | return 4; | |
1157 | } | |
1158 | ||
1159 | // user supplied filename? | |
1160 | if (fileNlen < 1) | |
1161 | sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]); | |
1162 | else | |
1163 | sprintf(fnameptr + fileNlen,".bin"); | |
1164 | ||
1165 | // open file | |
1166 | if ((f = fopen(filename,"wb")) == NULL) { | |
1167 | PrintAndLog("Could not create file name %s", filename); | |
1168 | free(data); | |
1169 | return 1; | |
1170 | } | |
1171 | fwrite(data, 1, numofbytes, f); | |
1172 | fclose(f); | |
1173 | free(data); | |
1174 | PrintAndLog("\nSaved %d bytes from emulator memory to file: %s", numofbytes, filename); | |
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | int CmdLegicList(const char *Cmd) { | |
1179 | CmdHFList("legic"); | |
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | static command_t CommandTable[] = { | |
1184 | {"help", CmdHelp, 1, "This help"}, | |
1185 | {"reader", CmdLegicReader, 1, "LEGIC Prime Reader UID and tag info"}, | |
1186 | {"info", CmdLegicInfo, 0, "Display deobfuscated and decoded LEGIC Prime tag data"}, | |
1187 | {"dump", CmdLegicDump, 0, "Dump LEGIC Prime tag to binary file"}, | |
1188 | {"restore", CmdLegicRestore, 0, "Restore a dump onto a LEGIC Prime tag"}, | |
1189 | {"rdmem", CmdLegicRdmem, 0, "Read bytes from a LEGIC Prime tag"}, | |
1190 | {"sim", CmdLegicRfSim, 0, "Start tag simulator"}, | |
1191 | {"write", CmdLegicRfWrite, 0, "Write data to a LEGIC Prime tag"}, | |
1192 | {"crc", CmdLegicCalcCrc, 1, "Calculate Legic CRC over given bytes"}, | |
1193 | {"eload", CmdLegicELoad, 1, "Load binary dump to emulator memory"}, | |
1194 | {"esave", CmdLegicESave, 1, "Save emulator memory to binary file"}, | |
1195 | {"list", CmdLegicList, 1, "[Deprecated] List LEGIC history"}, | |
1196 | {NULL, NULL, 0, NULL} | |
1197 | }; | |
1198 | ||
1199 | int CmdHFLegic(const char *Cmd) { | |
1200 | clearCommandBuffer(); | |
1201 | CmdsParse(CommandTable, Cmd); | |
1202 | return 0; | |
1203 | } | |
1204 | ||
1205 | int CmdHelp(const char *Cmd) { | |
1206 | CmdsHelp(CommandTable); | |
1207 | return 0; | |
1208 | } |