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