]>
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 | } | |
152 | ||
669c1b80 | 153 | /* |
154 | * Output BigBuf and deobfuscate LEGIC RF tag data. | |
cbdcc89a | 155 | * This is based on information given in the talk held |
669c1b80 | 156 | * by Henryk Ploetz and Karsten Nohl at 26c3 |
669c1b80 | 157 | */ |
633d0686 | 158 | int CmdLegicInfo(const char *Cmd) { |
159 | ||
160 | char cmdp = param_getchar(Cmd, 0); | |
161 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_info(); | |
e8fecd72 | 162 | |
163 | int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0; | |
164 | int crc = 0, wrp = 0, wrc = 0; | |
60bb5ef7 | 165 | uint8_t stamp_len = 0; |
77a689db | 166 | uint8_t data[1024]; // receiver buffer |
e8fecd72 | 167 | char token_type[5] = {0,0,0,0,0}; |
168 | int dcf = 0; | |
3e134b4c | 169 | int bIsSegmented = 0; |
52cf34c1 | 170 | |
9015ae0f | 171 | CmdLegicRdmem("0 22 55"); |
633d0686 | 172 | |
77a689db | 173 | // copy data from device |
174 | GetEMLFromBigBuf(data, sizeof(data), 0); | |
f7f844d0 | 175 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ |
176 | PrintAndLog("Command execute timeout"); | |
177 | return 1; | |
178 | } | |
179 | ||
52cf34c1 | 180 | // Output CDF System area (9 bytes) plus remaining header area (12 bytes) |
77a689db | 181 | crc = data[4]; |
182 | uint32_t calc_crc = CRC8Legic(data, 4); | |
3b920280 | 183 | |
52cf34c1 | 184 | PrintAndLog("\nCDF: System Area"); |
aacb96d7 | 185 | PrintAndLog("------------------------------------------------------"); |
3b920280 | 186 | PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s", |
77a689db | 187 | data[0], |
188 | data[1], | |
189 | data[2], | |
190 | data[3], | |
191 | data[4], | |
192 | (calc_crc == crc) ? "OK":"Fail" | |
52cf34c1 | 193 | ); |
669c1b80 | 194 | |
0e8cabed | 195 | // MCD = Manufacturer ID (should be list meaning something?) |
3e134b4c | 196 | |
197 | token_type[0] = 0; | |
77a689db | 198 | dcf = ((int)data[6] << 8) | (int)data[5]; |
3e134b4c | 199 | |
200 | // New unwritten media? | |
201 | if(dcf == 0xFFFF) { | |
202 | ||
203 | PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)", | |
204 | dcf, | |
77a689db | 205 | data[5], |
206 | data[6] | |
3e134b4c | 207 | ); |
208 | ||
5b9fb6f4 | 209 | } else if (dcf > 60000) { // Master token? |
3e134b4c | 210 | |
211 | int fl = 0; | |
212 | ||
77a689db | 213 | if(data[6] == 0xec) { |
3e134b4c | 214 | strncpy(token_type, "XAM", sizeof(token_type)); |
215 | fl = 1; | |
77a689db | 216 | stamp_len = 0x0c - (data[5] >> 4); |
3e134b4c | 217 | } else { |
77a689db | 218 | switch (data[5] & 0x7f) { |
e8fecd72 | 219 | case 0x00 ... 0x2f: |
220 | strncpy(token_type, "IAM", sizeof(token_type)); | |
77a689db | 221 | fl = (0x2f - (data[5] & 0x7f)) + 1; |
e8fecd72 | 222 | break; |
223 | case 0x30 ... 0x6f: | |
224 | strncpy(token_type, "SAM", sizeof(token_type)); | |
77a689db | 225 | fl = (0x6f - (data[5] & 0x7f)) + 1; |
e8fecd72 | 226 | break; |
227 | case 0x70 ... 0x7f: | |
228 | strncpy(token_type, "GAM", sizeof(token_type)); | |
77a689db | 229 | fl = (0x7f - (data[5] & 0x7f)) + 1; |
e8fecd72 | 230 | break; |
231 | } | |
52cf34c1 | 232 | |
77a689db | 233 | stamp_len = 0xfc - data[6]; |
3e134b4c | 234 | } |
52cf34c1 | 235 | |
3e134b4c | 236 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u", |
237 | dcf, | |
77a689db | 238 | data[5], |
239 | data[6], | |
e8fecd72 | 240 | token_type, |
77a689db | 241 | (data[5] & 0x80 )>> 7, |
3e134b4c | 242 | stamp_len, |
243 | fl | |
e8fecd72 | 244 | ); |
52cf34c1 | 245 | |
e8fecd72 | 246 | } else { // Is IM(-S) type of card... |
3e134b4c | 247 | |
77a689db | 248 | if(data[7] == 0x9F && data[8] == 0xFF) { |
3e134b4c | 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, | |
77a689db | 257 | data[5], |
258 | data[6], | |
3e134b4c | 259 | token_type, |
77a689db | 260 | (data[5]&0x80) >> 7 |
3e134b4c | 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", | |
77a689db | 269 | data[7] & 0x0f, |
270 | (data[7] & 0x70) >> 4, | |
271 | (data[7] & 0x80) >> 7, | |
272 | data[8] | |
e8fecd72 | 273 | ); |
3e134b4c | 274 | } |
52cf34c1 | 275 | |
3e134b4c | 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"); | |
77a689db | 280 | PrintAndLog("%s", sprint_hex(data+8, 14)); |
3e134b4c | 281 | } else { |
e8fecd72 | 282 | PrintAndLog("Remaining Header Area"); |
77a689db | 283 | PrintAndLog("%s", sprint_hex(data+9, 13)); |
3e134b4c | 284 | } |
285 | } | |
286 | } | |
d7fd9084 | 287 | |
e8fecd72 | 288 | uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0}; |
e579e768 | 289 | uint32_t segCalcCRC = 0; |
290 | uint32_t segCRC = 0; | |
d7fd9084 | 291 | |
3e134b4c | 292 | // Data card? |
293 | if(dcf <= 60000) { | |
cbdcc89a | 294 | |
e8fecd72 | 295 | PrintAndLog("\nADF: User Area"); |
296 | PrintAndLog("------------------------------------------------------"); | |
3e134b4c | 297 | |
298 | if(bIsSegmented) { | |
299 | ||
300 | // Data start point on segmented cards | |
e8fecd72 | 301 | i = 22; |
3e134b4c | 302 | |
303 | // decode segments | |
304 | for (segmentNum=1; segmentNum < 128; segmentNum++ ) | |
305 | { | |
77a689db | 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; | |
e8fecd72 | 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); | |
e579e768 | 315 | |
e8fecd72 | 316 | // validate segment-crc |
77a689db | 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 | |
e8fecd72 | 325 | |
326 | segCalcCRC = CRC8Legic(segCrcBytes, 8); | |
77a689db | 327 | segCRC = data[i+4] ^ crc; |
e8fecd72 | 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, | |
77a689db | 331 | data[i] ^ crc, |
332 | data[i+1] ^ crc, | |
333 | data[i+2] ^ crc, | |
334 | data[i+3] ^ crc, | |
e8fecd72 | 335 | segment_len, |
336 | segment_flag, | |
337 | (segment_flag & 0x4) >> 2, | |
338 | (segment_flag & 0x8) >> 3, | |
339 | wrp, | |
340 | wrc, | |
77a689db | 341 | ((data[i+3]^crc) & 0x80) >> 7, |
e8fecd72 | 342 | segCRC, |
343 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
344 | ); | |
345 | ||
346 | i += 5; | |
669c1b80 | 347 | |
e8fecd72 | 348 | if ( hasWRC ) { |
349 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
350 | PrintAndLog("\nrow | data"); | |
351 | PrintAndLog("-----+------------------------------------------------"); | |
3e134b4c | 352 | |
26778ea7 | 353 | for ( k=i; k < (i + wrc); ++k) |
77a689db | 354 | data[k] ^= crc; |
3e134b4c | 355 | |
77a689db | 356 | print_hex_break( data+i, wrc, 16); |
3b920280 | 357 | |
e8fecd72 | 358 | i += wrc; |
359 | } | |
669c1b80 | 360 | |
e8fecd72 | 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("-----+------------------------------------------------"); | |
d7fd9084 | 365 | |
3e134b4c | 366 | for (k=i; k < (i+wrp_len); ++k) |
77a689db | 367 | data[k] ^= crc; |
3b920280 | 368 | |
77a689db | 369 | print_hex_break( data+i, wrp_len, 16); |
3b920280 | 370 | |
e8fecd72 | 371 | i += wrp_len; |
d7fd9084 | 372 | |
3e134b4c | 373 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) |
e8fecd72 | 374 | if( wrp_len == 8 ) |
77a689db | 375 | PrintAndLog("Card ID: %2X%02X%02X", data[i-4]^crc, data[i-3]^crc, data[i-2]^crc); |
e8fecd72 | 376 | } |
669c1b80 | 377 | |
e8fecd72 | 378 | PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len); |
379 | PrintAndLog("\nrow | data"); | |
380 | PrintAndLog("-----+------------------------------------------------"); | |
3e134b4c | 381 | |
382 | for ( k=i; k < (i+remain_seg_payload_len); ++k) | |
77a689db | 383 | data[k] ^= crc; |
3b920280 | 384 | |
77a689db | 385 | print_hex_break( data+i, remain_seg_payload_len, 16); |
669c1b80 | 386 | |
e8fecd72 | 387 | i += remain_seg_payload_len; |
d7fd9084 | 388 | |
e8fecd72 | 389 | PrintAndLog("-----+------------------------------------------------\n"); |
a182a680 | 390 | |
e8fecd72 | 391 | // end with last segment |
392 | if (segment_flag & 0x8) return 0; | |
52cf34c1 | 393 | |
e8fecd72 | 394 | } // end for loop |
3e134b4c | 395 | |
396 | } else { | |
397 | ||
398 | // Data start point on unsegmented cards | |
399 | i = 8; | |
400 | ||
77a689db | 401 | wrp = data[7] & 0x0F; |
402 | wrc = (data[7] & 0x70) >> 4; | |
3e134b4c | 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, | |
77a689db | 412 | (data[7] & 0x80) >> 7 |
3e134b4c | 413 | ); |
414 | ||
415 | if ( hasWRC ) { | |
416 | PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc); | |
417 | PrintAndLog("\nrow | data"); | |
418 | PrintAndLog("-----+------------------------------------------------"); | |
77a689db | 419 | print_hex_break( data+i, wrc, 16); |
3e134b4c | 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("-----+------------------------------------------------"); | |
77a689db | 427 | print_hex_break( data + i, wrp_len, 16); |
3e134b4c | 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 ) | |
77a689db | 432 | PrintAndLog("Card ID: %2X%02X%02X", data[i-4], data[i-3], data[i-2]); |
3e134b4c | 433 | } |
434 | ||
435 | PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len); | |
436 | PrintAndLog("\nrow | data"); | |
437 | PrintAndLog("-----+------------------------------------------------"); | |
77a689db | 438 | print_hex_break( data + i, remain_seg_payload_len, 16); |
3e134b4c | 439 | i += remain_seg_payload_len; |
440 | ||
441 | PrintAndLog("-----+------------------------------------------------\n"); | |
442 | } | |
443 | } | |
52cf34c1 | 444 | return 0; |
669c1b80 | 445 | } |
41dab153 | 446 | |
77e1bab9 | 447 | // params: |
448 | // offset in data memory | |
449 | // number of bytes to read | |
00271f77 | 450 | int CmdLegicRdmem(const char *Cmd) { |
ffa306de | 451 | |
cbdcc89a | 452 | char cmdp = param_getchar(Cmd, 0); |
00271f77 | 453 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_rdmem(); |
cbdcc89a | 454 | |
b98827ff | 455 | uint32_t offset = 0, len = 0, IV = 1; |
ffa306de | 456 | sscanf(Cmd, "%x %x %x", &offset, &len, &IV); |
52cf34c1 | 457 | |
5b9fb6f4 | 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 | ||
ffa306de | 467 | // OUT-OF-BOUNDS check |
5b9fb6f4 | 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); | |
a3994421 | 472 | } |
ffa306de | 473 | |
0e8cabed | 474 | legic_chk_iv(&IV); |
ffa306de | 475 | |
ad5bc8cc | 476 | UsbCommand c = {CMD_READER_LEGIC_RF, {offset, len, IV}}; |
52cf34c1 | 477 | clearCommandBuffer(); |
478 | SendCommand(&c); | |
ad5bc8cc | 479 | UsbCommand resp; |
77e1bab9 | 480 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 3000) ) { |
481 | PrintAndLog("command execution time out"); | |
482 | return 1; | |
483 | } | |
c649c433 | 484 | |
77e1bab9 | 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 | } | |
86087eba | 497 | |
77e1bab9 | 498 | if ( readlen != len ) |
499 | PrintAndLog("Fail, only managed to read 0x%02X bytes", readlen); | |
86087eba | 500 | |
77e1bab9 | 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); | |
ad5bc8cc | 506 | return 1; |
507 | } | |
77e1bab9 | 508 | |
509 | PrintAndLog("\n ## | Data"); | |
510 | PrintAndLog("-----+-----"); | |
511 | print_hex_break( data, readlen, 32); | |
512 | free(data); | |
52cf34c1 | 513 | return 0; |
41dab153 | 514 | } |
3612a8a8 | 515 | |
0e8cabed | 516 | // should say which tagtype |
517 | // should load a tag to device mem. | |
5b9fb6f4 | 518 | // int phase, int frame, int reqresp |
52cf34c1 | 519 | int CmdLegicRfSim(const char *Cmd) { |
3b920280 | 520 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; |
52cf34c1 | 521 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); |
522 | clearCommandBuffer(); | |
523 | SendCommand(&c); | |
524 | return 0; | |
3612a8a8 | 525 | } |
526 | ||
52cf34c1 | 527 | int CmdLegicRfWrite(const char *Cmd) { |
ffa306de | 528 | |
f0fa6638 | 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; | |
0e8cabed | 534 | |
f0fa6638 | 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 | // it's possible for user to accidentally enter "b" parameter | |
556 | // more than once - we have to clean previous malloc | |
557 | if (data) | |
558 | free(data); | |
559 | data = malloc(len >> 1); | |
560 | if ( data == NULL ) { | |
561 | PrintAndLog("Can't allocate memory. exiting"); | |
562 | errors = true; | |
563 | break; | |
564 | } | |
565 | ||
566 | if (param_gethex(Cmd, cmdp+1, data, len)) { | |
567 | errors = true; | |
568 | break; | |
569 | } | |
570 | ||
571 | len >>= 1; | |
572 | cmdp += 2; | |
573 | break; | |
574 | case 'o': | |
575 | case 'O': | |
5b9fb6f4 | 576 | offset = param_get32ex(Cmd, cmdp+1, 4, 16); |
f0fa6638 | 577 | cmdp += 2; |
578 | break; | |
579 | case 'h': | |
580 | case 'H': | |
581 | errors = true; | |
582 | break; | |
583 | default: | |
584 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
585 | errors = true; | |
586 | break; | |
587 | } | |
588 | if (errors) break; | |
589 | } | |
590 | //Validations | |
591 | if (errors){ | |
592 | if (data) | |
593 | free(data); | |
594 | return usage_legic_write(); | |
595 | } | |
596 | ||
0e8cabed | 597 | // tagtype |
598 | legic_card_select_t card; | |
599 | if (legic_get_type(&card)) { | |
600 | PrintAndLog("Failed to identify tagtype"); | |
601 | return -1; | |
602 | } | |
ffa306de | 603 | |
0e8cabed | 604 | legic_print_type(card.cardsize, 0); |
605 | ||
5b9fb6f4 | 606 | // OUT-OF-BOUNDS checks |
f0fa6638 | 607 | // UID 4 bytes can't be written to. |
5b9fb6f4 | 608 | if ( offset < 4 ) { |
609 | PrintAndLog("Out-of-bounds, UID 4bytes can't be written to. Offset = %d", offset); | |
610 | return -2; | |
611 | } | |
612 | ||
f0fa6638 | 613 | if ( len + offset + 4 >= card.cardsize ) { |
614 | PrintAndLog("Out-of-bounds, Cardsize = %d, [offset+len = %d ]", card.cardsize, len + offset + 4); | |
0e8cabed | 615 | return -2; |
b98827ff | 616 | } |
f0fa6638 | 617 | |
0e8cabed | 618 | legic_chk_iv(&IV); |
619 | ||
f0fa6638 | 620 | PrintAndLog("Writing to tag"); |
621 | UsbCommand c = {CMD_WRITER_LEGIC_RF, {offset, len, IV}}; | |
622 | memcpy(c.d.asBytes, data, len); | |
623 | ||
52cf34c1 | 624 | clearCommandBuffer(); |
3612a8a8 | 625 | SendCommand(&c); |
7bc3c99e | 626 | UsbCommand resp; |
b8168868 | 627 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 4000)) { |
7bc3c99e | 628 | PrintAndLog("command execution time out"); |
629 | return 1; | |
630 | } | |
f0fa6638 | 631 | uint8_t isOK = resp.arg[0] & 0xFF; |
632 | if ( !isOK ) { | |
633 | PrintAndLog("failed writing tag"); | |
634 | return 1; | |
635 | } | |
7bc3c99e | 636 | |
3612a8a8 | 637 | return 0; |
638 | } | |
639 | ||
f6e01a34 | 640 | /* |
77e1bab9 | 641 | PrintAndLog("############# DANGER !! #############"); |
642 | PrintAndLog("# changing the DCF is irreversible #"); | |
643 | PrintAndLog("#####################################"); | |
644 | PrintAndLog("do youe really want to continue? y(es) n(o)"); | |
645 | // if (scanf(" %c", &answer) > 0 && (answer == 'y' || answer == 'Y')) { | |
646 | // return 0; | |
647 | // } | |
f6e01a34 | 648 | */ |
3e134b4c | 649 | |
f6e01a34 | 650 | int CmdLegicCalcCrc(const char *Cmd){ |
3b920280 | 651 | |
cc4c8fd6 | 652 | uint8_t *data = NULL; |
3e134b4c | 653 | uint8_t cmdp = 0, uidcrc = 0, type=0; |
654 | bool errors = false; | |
655 | int len = 0; | |
e31a0f73 | 656 | int bg, en; |
3b920280 | 657 | |
3e134b4c | 658 | while(param_getchar(Cmd, cmdp) != 0x00) { |
659 | switch(param_getchar(Cmd, cmdp)) { | |
660 | case 'b': | |
661 | case 'B': | |
e31a0f73 AG |
662 | // peek at length of the input string so we can |
663 | // figure out how many elements to malloc in "data" | |
664 | bg=en=0; | |
987c5984 AG |
665 | if (param_getptr(Cmd, &bg, &en, cmdp+1)) { |
666 | errors = true; | |
667 | break; | |
668 | } | |
e31a0f73 AG |
669 | len = (en - bg + 1); |
670 | ||
671 | // check that user entered even number of characters | |
672 | // for hex data string | |
673 | if (len & 1) { | |
674 | errors = true; | |
675 | break; | |
676 | } | |
677 | ||
08927081 AG |
678 | // it's possible for user to accidentally enter "b" parameter |
679 | // more than once - we have to clean previous malloc | |
680 | if (data) free(data); | |
e31a0f73 | 681 | data = malloc(len >> 1); |
3e134b4c | 682 | if ( data == NULL ) { |
683 | PrintAndLog("Can't allocate memory. exiting"); | |
684 | errors = true; | |
685 | break; | |
e31a0f73 AG |
686 | } |
687 | ||
987c5984 AG |
688 | if (param_gethex(Cmd, cmdp+1, data, len)) { |
689 | errors = true; | |
690 | break; | |
691 | } | |
3e134b4c | 692 | |
693 | len >>= 1; | |
694 | cmdp += 2; | |
695 | break; | |
696 | case 'u': | |
697 | case 'U': | |
698 | uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16); | |
699 | cmdp += 2; | |
700 | break; | |
701 | case 'c': | |
702 | case 'C': | |
703 | type = param_get8ex(Cmd, cmdp+1, 0, 10); | |
704 | cmdp += 2; | |
705 | break; | |
706 | case 'h': | |
707 | case 'H': | |
708 | errors = true; | |
709 | break; | |
710 | default: | |
711 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
712 | errors = true; | |
713 | break; | |
714 | } | |
715 | if (errors) break; | |
716 | } | |
717 | //Validations | |
718 | if (errors){ | |
aeb128e2 | 719 | if (data) free(data); |
f6e01a34 | 720 | return usage_legic_calccrc(); |
3e134b4c | 721 | } |
722 | ||
723 | switch (type){ | |
724 | case 16: | |
87342aad | 725 | PrintAndLog("Legic crc16: %X", CRC16Legic(data, len, uidcrc)); |
726 | break; | |
3e134b4c | 727 | default: |
87342aad | 728 | PrintAndLog("Legic crc8: %X", CRC8Legic(data, len) ); |
3e134b4c | 729 | break; |
eb5206bd | 730 | } |
3b920280 | 731 | |
aeb128e2 | 732 | if (data) free(data); |
3b920280 | 733 | return 0; |
734 | } | |
3e750be3 | 735 | |
9015ae0f | 736 | int legic_print_type(uint32_t tagtype, uint8_t spaces){ |
737 | char spc[11] = " "; | |
738 | spc[10]=0x00; | |
739 | char *spacer = spc + (10-spaces); | |
740 | ||
741 | if ( tagtype == 22 ) | |
742 | PrintAndLog("%sTYPE : MIM%d card (outdated)", spacer, tagtype); | |
743 | else if ( tagtype == 256 ) | |
744 | PrintAndLog("%sTYPE : MIM%d card (234 bytes)", spacer, tagtype); | |
745 | else if ( tagtype == 1024 ) | |
746 | PrintAndLog("%sTYPE : MIM%d card (1002 bytes)", spacer, tagtype); | |
747 | else | |
748 | PrintAndLog("%sTYPE : Unknown %06x", spacer, tagtype); | |
749 | return 0; | |
750 | } | |
751 | int legic_get_type(legic_card_select_t *card){ | |
752 | ||
753 | if ( card == NULL ) return 1; | |
754 | ||
3e750be3 | 755 | UsbCommand c = {CMD_LEGIC_INFO, {0,0,0}}; |
756 | clearCommandBuffer(); | |
757 | SendCommand(&c); | |
758 | UsbCommand resp; | |
9015ae0f | 759 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 500)) |
760 | return 2; | |
a3994421 | 761 | |
762 | uint8_t isOK = resp.arg[0] & 0xFF; | |
9015ae0f | 763 | if ( !isOK ) |
764 | return 3; | |
765 | ||
766 | memcpy(card, (legic_card_select_t *)resp.d.asBytes, sizeof(legic_card_select_t)); | |
767 | return 0; | |
768 | } | |
0e8cabed | 769 | void legic_chk_iv(uint32_t *iv){ |
770 | if ( (*iv & 0x7F) != *iv ){ | |
771 | *iv &= 0x7F; | |
772 | PrintAndLog("Truncating IV to 7bits, %u", *iv); | |
773 | } | |
774 | // IV must be odd | |
775 | if ( (*iv & 1) == 0 ){ | |
776 | *iv |= 0x01; | |
777 | PrintAndLog("LSB of IV must be SET %u", *iv); | |
778 | } | |
779 | } | |
0e8cabed | 780 | void legic_seteml(uint8_t *src, uint32_t offset, uint32_t numofbytes) { |
0e8cabed | 781 | size_t len = 0; |
782 | UsbCommand c = {CMD_LEGIC_ESET, {0, 0, 0}}; | |
0e8cabed | 783 | for(size_t i = 0; i < numofbytes; i += USB_CMD_DATA_SIZE) { |
784 | ||
785 | len = MIN((numofbytes - i), USB_CMD_DATA_SIZE); | |
786 | c.arg[0] = i; // offset | |
787 | c.arg[1] = len; // number of bytes | |
77e1bab9 | 788 | memcpy(c.d.asBytes, src+i, len); |
0e8cabed | 789 | clearCommandBuffer(); |
790 | SendCommand(&c); | |
0e8cabed | 791 | } |
792 | } | |
9015ae0f | 793 | |
794 | int HFLegicReader(const char *Cmd, bool verbose) { | |
795 | ||
796 | char cmdp = param_getchar(Cmd, 0); | |
797 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_reader(); | |
a3994421 | 798 | |
799 | legic_card_select_t card; | |
9015ae0f | 800 | switch(legic_get_type(&card)){ |
801 | case 1: | |
802 | if ( verbose ) PrintAndLog("command execution time out"); | |
a3994421 | 803 | return 1; |
9015ae0f | 804 | case 2: |
805 | case 3: | |
806 | if ( verbose ) PrintAndLog("legic card select failed"); | |
807 | return 2; | |
808 | default: break; | |
809 | } | |
810 | PrintAndLog(" UID : %s", sprint_hex(card.uid, sizeof(card.uid))); | |
811 | legic_print_type(card.cardsize, 0); | |
3e750be3 | 812 | return 0; |
813 | } | |
633d0686 | 814 | int CmdLegicReader(const char *Cmd){ |
815 | return HFLegicReader(Cmd, TRUE); | |
3e750be3 | 816 | } |
633d0686 | 817 | |
818 | int CmdLegicDump(const char *Cmd){ | |
00271f77 | 819 | |
0e8cabed | 820 | FILE *f; |
00271f77 | 821 | char filename[FILE_PATH_SIZE] = {0x00}; |
822 | char *fnameptr = filename; | |
823 | size_t fileNlen = 0; | |
824 | bool errors = false; | |
9015ae0f | 825 | uint16_t dumplen; |
826 | uint8_t cmdp = 0; | |
00271f77 | 827 | |
0e8cabed | 828 | memset(filename, 0, sizeof(filename)); |
829 | ||
830 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
831 | switch(param_getchar(Cmd, cmdp)) { | |
832 | case 'h': | |
833 | case 'H': | |
834 | return usage_legic_dump(); | |
835 | case 'o': | |
836 | case 'O': | |
837 | fileNlen = param_getstr(Cmd, cmdp+1, filename); | |
838 | if (!fileNlen) | |
839 | errors = true; | |
840 | if (fileNlen > FILE_PATH_SIZE-5) | |
841 | fileNlen = FILE_PATH_SIZE-5; | |
842 | cmdp += 2; | |
843 | break; | |
844 | default: | |
845 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
846 | errors = true; | |
847 | break; | |
00271f77 | 848 | } |
849 | if(errors) break; | |
850 | } | |
851 | ||
852 | //Validations | |
853 | if(errors) return usage_legic_dump(); | |
854 | ||
855 | // tagtype | |
9015ae0f | 856 | legic_card_select_t card; |
857 | if (legic_get_type(&card)) { | |
858 | PrintAndLog("Failed to identify tagtype"); | |
859 | return -1; | |
860 | } | |
861 | dumplen = card.cardsize; | |
00271f77 | 862 | |
9015ae0f | 863 | legic_print_type(dumplen, 0); |
864 | PrintAndLog("Reading tag memory..."); | |
865 | ||
00271f77 | 866 | UsbCommand c = {CMD_READER_LEGIC_RF, {0x00, dumplen, 0x55}}; |
867 | clearCommandBuffer(); | |
868 | SendCommand(&c); | |
869 | UsbCommand resp; | |
870 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) { | |
871 | PrintAndLog("Command execute time-out"); | |
872 | return 1; | |
873 | } | |
874 | ||
875 | uint8_t isOK = resp.arg[0] & 0xFF; | |
876 | if ( !isOK ) { | |
877 | PrintAndLog("Failed dumping tag data"); | |
878 | return 2; | |
879 | } | |
880 | ||
881 | uint16_t readlen = resp.arg[1]; | |
882 | uint8_t *data = malloc(readlen); | |
0e8cabed | 883 | if (!data) { |
00271f77 | 884 | PrintAndLog("Fail, cannot allocate memory"); |
885 | return 3; | |
886 | } | |
77e1bab9 | 887 | memset(data, 0, readlen); |
00271f77 | 888 | |
889 | if ( readlen != dumplen ) | |
890 | PrintAndLog("Fail, only managed to read 0x%02X bytes of 0x%02X", readlen, dumplen); | |
891 | ||
892 | // copy data from device | |
893 | GetEMLFromBigBuf(data, readlen, 0); | |
894 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) { | |
895 | PrintAndLog("Fail, transfer from device time-out"); | |
0e8cabed | 896 | free(data); |
00271f77 | 897 | return 4; |
898 | } | |
899 | ||
900 | // user supplied filename? | |
901 | if (fileNlen < 1) | |
902 | sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]); | |
903 | else | |
904 | sprintf(fnameptr + fileNlen,".bin"); | |
905 | ||
0e8cabed | 906 | if ((f = fopen(filename,"wb")) == NULL) { |
00271f77 | 907 | PrintAndLog("Could not create file name %s", filename); |
0e8cabed | 908 | if (data) |
909 | free(data); | |
00271f77 | 910 | return 5; |
911 | } | |
0e8cabed | 912 | fwrite(data, 1, readlen, f); |
913 | fclose(f); | |
914 | free(data); | |
00271f77 | 915 | PrintAndLog("Wrote %d bytes to %s", readlen, filename); |
633d0686 | 916 | return 0; |
917 | } | |
0e8cabed | 918 | |
5b9fb6f4 | 919 | int CmdLegicRestore(const char *Cmd){ |
920 | ||
921 | FILE *f; | |
922 | char filename[FILE_PATH_SIZE] = {0x00}; | |
923 | char *fnameptr = filename; | |
924 | size_t fileNlen = 0; | |
925 | bool errors = false; | |
926 | uint16_t numofbytes; | |
927 | uint8_t cmdp = 0; | |
928 | ||
929 | memset(filename, 0, sizeof(filename)); | |
930 | ||
931 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
932 | switch(param_getchar(Cmd, cmdp)) { | |
933 | case 'h': | |
934 | case 'H': | |
935 | errors = true; | |
936 | break; | |
937 | case 'i': | |
938 | case 'I': | |
939 | fileNlen = param_getstr(Cmd, cmdp+1, filename); | |
940 | if (!fileNlen) | |
941 | errors = true; | |
942 | if (fileNlen > FILE_PATH_SIZE-5) | |
943 | fileNlen = FILE_PATH_SIZE-5; | |
944 | cmdp += 2; | |
945 | break; | |
946 | default: | |
947 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
948 | errors = true; | |
949 | break; | |
950 | } | |
951 | if(errors) break; | |
952 | } | |
953 | ||
954 | //Validations | |
955 | if(errors) return usage_legic_restore(); | |
956 | ||
957 | // tagtype | |
958 | legic_card_select_t card; | |
959 | if (legic_get_type(&card)) { | |
960 | PrintAndLog("Failed to identify tagtype"); | |
961 | return 1; | |
962 | } | |
963 | numofbytes = card.cardsize; | |
964 | ||
965 | // set up buffer | |
966 | uint8_t *data = malloc(numofbytes); | |
967 | if (!data) { | |
968 | PrintAndLog("Fail, cannot allocate memory"); | |
969 | return 3; | |
970 | } | |
971 | memset(data, 0, numofbytes); | |
972 | ||
973 | legic_print_type(numofbytes, 0); | |
974 | ||
975 | // set up file | |
976 | fnameptr += fileNlen; | |
977 | sprintf(fnameptr, ".bin"); | |
978 | ||
979 | PrintAndLog("Reading binary file..."); | |
980 | ||
981 | if ((f = fopen(filename,"rb")) == NULL) { | |
982 | PrintAndLog("File %s not found or locked", filename); | |
983 | return 2; | |
984 | } | |
985 | ||
986 | // verify size of dumpfile is the same as card. | |
987 | ||
988 | // load file | |
989 | size_t bytes_read = fread(data, 1, numofbytes, f); | |
990 | if ( bytes_read == 0){ | |
991 | PrintAndLog("File reading error"); | |
992 | free(data); | |
993 | fclose(f); | |
994 | return 2; | |
995 | } | |
996 | fclose(f); | |
997 | ||
998 | PrintAndLog("Restoring %s to card", filename); | |
999 | ||
1000 | //loop writing :) | |
1001 | ||
1002 | //endloop | |
1003 | ||
1004 | free(data); | |
1005 | PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename); | |
1006 | return 0; | |
1007 | } | |
1008 | ||
0e8cabed | 1009 | int CmdLegicELoad(const char *Cmd) { |
1010 | FILE * f; | |
1011 | char filename[FILE_PATH_SIZE]; | |
1012 | char *fnameptr = filename; | |
1013 | int len, numofbytes; | |
1014 | int nameParamNo = 1; | |
1015 | ||
1016 | char cmdp = param_getchar(Cmd, 0); | |
1017 | if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00) | |
1018 | return usage_legic_eload(); | |
1019 | ||
1020 | switch (cmdp) { | |
1021 | case '0' : numofbytes = 22; break; | |
1022 | case '1' : | |
1023 | case '\0': numofbytes = 256; break; | |
1024 | case '2' : numofbytes = 1024; break; | |
1025 | default : numofbytes = 256; nameParamNo = 0;break; | |
1026 | } | |
1027 | ||
1028 | // set up buffer | |
1029 | uint8_t *data = malloc(numofbytes); | |
1030 | if (!data) { | |
1031 | PrintAndLog("Fail, cannot allocate memory"); | |
1032 | return 3; | |
1033 | } | |
1034 | memset(data, 0, numofbytes); | |
1035 | ||
1036 | // set up file | |
1037 | len = param_getstr(Cmd, nameParamNo, filename); | |
1038 | if (len > FILE_PATH_SIZE - 5) | |
1039 | len = FILE_PATH_SIZE - 5; | |
1040 | fnameptr += len; | |
5b9fb6f4 | 1041 | sprintf(fnameptr, ".bin"); |
0e8cabed | 1042 | |
1043 | // open file | |
1044 | if ((f = fopen(filename,"rb")) == NULL) { | |
1045 | PrintAndLog("File %s not found or locked", filename); | |
1046 | free(data); | |
1047 | return 1; | |
1048 | } | |
1049 | ||
1050 | // load file | |
1051 | size_t bytes_read = fread(data, 1, numofbytes, f); | |
1052 | if ( bytes_read == 0){ | |
1053 | PrintAndLog("File reading error"); | |
1054 | free(data); | |
1055 | fclose(f); | |
1056 | return 2; | |
1057 | } | |
1058 | fclose(f); | |
1059 | ||
1060 | // transfer to device | |
1061 | legic_seteml(data, 0, numofbytes); | |
1062 | ||
1063 | free(data); | |
1064 | PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename); | |
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | int CmdLegicESave(const char *Cmd) { | |
1069 | FILE *f; | |
1070 | char filename[FILE_PATH_SIZE]; | |
1071 | char *fnameptr = filename; | |
1072 | int fileNlen, numofbytes, nameParamNo = 1; | |
1073 | ||
1074 | memset(filename, 0, sizeof(filename)); | |
1075 | ||
1076 | char cmdp = param_getchar(Cmd, 0); | |
1077 | ||
1078 | if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00) | |
1079 | return usage_legic_esave(); | |
1080 | ||
1081 | switch (cmdp) { | |
1082 | case '0' : numofbytes = 22; break; | |
1083 | case '1' : | |
1084 | case '\0': numofbytes = 256; break; | |
1085 | case '2' : numofbytes = 1024; break; | |
1086 | default : numofbytes = 256; nameParamNo = 0; break; | |
1087 | } | |
1088 | ||
1089 | fileNlen = param_getstr(Cmd, nameParamNo, filename); | |
1090 | ||
1091 | if (fileNlen > FILE_PATH_SIZE - 5) | |
1092 | fileNlen = FILE_PATH_SIZE - 5; | |
1093 | ||
1094 | // set up buffer | |
1095 | uint8_t *data = malloc(numofbytes); | |
1096 | if (!data) { | |
1097 | PrintAndLog("Fail, cannot allocate memory"); | |
1098 | return 3; | |
1099 | } | |
1100 | memset(data, 0, numofbytes); | |
1101 | ||
1102 | // download emulator memory | |
77e1bab9 | 1103 | PrintAndLog("Reading emulator memory..."); |
0e8cabed | 1104 | GetEMLFromBigBuf(data, numofbytes, 0); |
1105 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) { | |
1106 | PrintAndLog("Fail, transfer from device time-out"); | |
1107 | free(data); | |
1108 | return 4; | |
1109 | } | |
1110 | ||
1111 | // user supplied filename? | |
1112 | if (fileNlen < 1) | |
1113 | sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]); | |
1114 | else | |
1115 | sprintf(fnameptr + fileNlen,".bin"); | |
1116 | ||
1117 | // open file | |
1118 | if ((f = fopen(filename,"wb")) == NULL) { | |
1119 | PrintAndLog("Could not create file name %s", filename); | |
1120 | free(data); | |
1121 | return 1; | |
1122 | } | |
1123 | fwrite(data, 1, numofbytes, f); | |
1124 | fclose(f); | |
1125 | free(data); | |
1126 | PrintAndLog("\nSaved %d bytes from emulator memory to file: %s", numofbytes, filename); | |
1127 | return 0; | |
1128 | } | |
5b9fb6f4 | 1129 | |
1130 | int CmdLegicList(const char *Cmd) { | |
1131 | CmdHFList("legic"); | |
1132 | return 0; | |
1133 | } | |
1134 | ||
52cf34c1 | 1135 | static command_t CommandTable[] = { |
633d0686 | 1136 | {"help", CmdHelp, 1, "This help"}, |
5b9fb6f4 | 1137 | {"reader", CmdLegicReader, 1, "LEGIC Prime Reader UID and tag info"}, |
633d0686 | 1138 | {"info", CmdLegicInfo, 0, "Display deobfuscated and decoded LEGIC Prime tag data"}, |
5b9fb6f4 | 1139 | {"dump", CmdLegicDump, 0, "Dump LEGIC Prime tag to binary file"}, |
1140 | {"restore", CmdLegicRestore, 0, "Restore a dump onto a LEGIC Prime tag"}, | |
1141 | {"rdmem", CmdLegicRdmem, 0, "Read bytes from a LEGIC Prime tag"}, | |
1142 | {"sim", CmdLegicRfSim, 0, "Start tag simulator"}, | |
1143 | {"write", CmdLegicRfWrite, 0, "Write data to a LEGIC Prime tag"}, | |
1144 | {"crc", CmdLegicCalcCrc, 1, "Calculate Legic CRC over given bytes"}, | |
0e8cabed | 1145 | {"eload", CmdLegicELoad, 1, "Load binary dump to emulator memory"}, |
1146 | {"esave", CmdLegicESave, 1, "Save emulator memory to binary file"}, | |
5b9fb6f4 | 1147 | {"list", CmdLegicList, 1, "[Deprecated] List LEGIC history"}, |
52cf34c1 | 1148 | {NULL, NULL, 0, NULL} |
1149 | }; | |
1150 | ||
1151 | int CmdHFLegic(const char *Cmd) { | |
1152 | clearCommandBuffer(); | |
1153 | CmdsParse(CommandTable, Cmd); | |
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | int CmdHelp(const char *Cmd) { | |
1158 | CmdsHelp(CommandTable); | |
1159 | return 0; | |
1160 | } |