]>
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 | int usage_legic_calccrc8(void){ | |
15 | PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes."); | |
16 | PrintAndLog("There must be an even number of hexsymbols as input."); | |
17 | PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc>"); | |
18 | PrintAndLog("Options :"); | |
19 | PrintAndLog(" b <hexbytes> : hex bytes"); | |
20 | PrintAndLog(" u <uidcrc> : MCC hexbyte"); | |
21 | PrintAndLog(""); | |
22 | PrintAndLog("Samples :"); | |
23 | PrintAndLog(" hf legic crc8 b deadbeef1122"); | |
24 | PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A"); | |
25 | return 0; | |
26 | } | |
27 | ||
28 | int usage_legic_load(void){ | |
29 | PrintAndLog("It loads datasamples from the file `filename` to device memory"); | |
30 | PrintAndLog("Usage: hf legic load <file name>"); | |
31 | PrintAndLog(" sample: hf legic load filename"); | |
32 | return 0; | |
33 | } | |
34 | ||
35 | int usage_legic_read(void){ | |
36 | PrintAndLog("Read data from a legic tag."); | |
37 | PrintAndLog("Usage: hf legic read <offset> <num of bytes>"); | |
38 | PrintAndLog("Options :"); | |
39 | PrintAndLog(" <offset> : offset in data array to start download from"); | |
40 | PrintAndLog(" <num of bytes> : number of bytes to download"); | |
41 | PrintAndLog(""); | |
42 | PrintAndLog(" sample: hf legic read"); | |
43 | return 0; | |
44 | } | |
45 | ||
46 | /* | |
47 | * Output BigBuf and deobfuscate LEGIC RF tag data. | |
48 | * This is based on information given in the talk held | |
49 | * by Henryk Ploetz and Karsten Nohl at 26c3 | |
50 | */ | |
51 | int CmdLegicDecode(const char *Cmd) { | |
52 | // Index for the bytearray. | |
53 | int i = 0; | |
54 | int k = 0, segmentNum; | |
55 | int segment_len = 0; | |
56 | int segment_flag = 0; | |
57 | uint8_t stamp_len = 0; | |
58 | int crc = 0; | |
59 | int wrp = 0; | |
60 | int wrc = 0; | |
61 | uint8_t data_buf[1052]; // receiver buffer, should be 1024.. | |
62 | char token_type[5]; | |
63 | int dcf; | |
64 | int bIsSegmented = 0; | |
65 | ||
66 | // download EML memory, where the "legic read" command puts the data. | |
67 | GetEMLFromBigBuf(data_buf, sizeof(data_buf), 0); | |
68 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ | |
69 | PrintAndLog("Command execute timeout"); | |
70 | return 1; | |
71 | } | |
72 | ||
73 | // Output CDF System area (9 bytes) plus remaining header area (12 bytes) | |
74 | crc = data_buf[4]; | |
75 | uint32_t calc_crc = CRC8Legic(data_buf, 4); | |
76 | ||
77 | PrintAndLog("\nCDF: System Area"); | |
78 | PrintAndLog("------------------------------------------------------"); | |
79 | PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s", | |
80 | data_buf[0], | |
81 | data_buf[1], | |
82 | data_buf[2], | |
83 | data_buf[3], | |
84 | data_buf[4], | |
85 | (calc_crc == crc) ? "OK":"Fail" | |
86 | ); | |
87 | ||
88 | ||
89 | token_type[0] = 0; | |
90 | dcf = ((int)data_buf[6] << 8) | (int)data_buf[5]; | |
91 | ||
92 | // New unwritten media? | |
93 | if(dcf == 0xFFFF) { | |
94 | ||
95 | PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)", | |
96 | dcf, | |
97 | data_buf[5], | |
98 | data_buf[6] | |
99 | ); | |
100 | ||
101 | } else if(dcf > 60000) { // Master token? | |
102 | ||
103 | int fl = 0; | |
104 | ||
105 | if(data_buf[6] == 0xec) { | |
106 | strncpy(token_type, "XAM", sizeof(token_type)); | |
107 | fl = 1; | |
108 | stamp_len = 0x0c - (data_buf[5] >> 4); | |
109 | } else { | |
110 | switch (data_buf[5] & 0x7f) { | |
111 | case 0x00 ... 0x2f: | |
112 | strncpy(token_type, "IAM",sizeof(token_type)); | |
113 | fl = (0x2f - (data_buf[5] & 0x7f)) + 1; | |
114 | break; | |
115 | case 0x30 ... 0x6f: | |
116 | strncpy(token_type, "SAM",sizeof(token_type)); | |
117 | fl = (0x6f - (data_buf[5] & 0x7f)) + 1; | |
118 | break; | |
119 | case 0x70 ... 0x7f: | |
120 | strncpy(token_type, "GAM",sizeof(token_type)); | |
121 | fl = (0x7f - (data_buf[5] & 0x7f)) + 1; | |
122 | break; | |
123 | } | |
124 | ||
125 | stamp_len = 0xfc - data_buf[6]; | |
126 | } | |
127 | ||
128 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u", | |
129 | dcf, | |
130 | data_buf[5], | |
131 | data_buf[6], | |
132 | token_type, | |
133 | (data_buf[5]&0x80)>>7, | |
134 | stamp_len, | |
135 | fl | |
136 | ); | |
137 | ||
138 | } else { // Is IM(-S) type of card... | |
139 | ||
140 | if(data_buf[7] == 0x9F && data_buf[8] == 0xFF) { | |
141 | bIsSegmented = 1; | |
142 | strncpy(token_type, "IM-S", sizeof(token_type)); | |
143 | } else { | |
144 | strncpy(token_type, "IM", sizeof(token_type)); | |
145 | } | |
146 | ||
147 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)", | |
148 | dcf, | |
149 | data_buf[5], | |
150 | data_buf[6], | |
151 | token_type, | |
152 | (data_buf[5]&0x80)>>7 | |
153 | ); | |
154 | } | |
155 | ||
156 | // Makes no sence to show this on blank media... | |
157 | if(dcf != 0xFFFF) { | |
158 | ||
159 | if(bIsSegmented) { | |
160 | PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x", | |
161 | data_buf[7]&0x0f, | |
162 | (data_buf[7]&0x70)>>4, | |
163 | (data_buf[7]&0x80)>>7, | |
164 | data_buf[8] | |
165 | ); | |
166 | } | |
167 | ||
168 | // Header area is only available on IM-S cards, on master tokens this data is the master token data itself | |
169 | if(bIsSegmented || dcf > 60000) { | |
170 | if(dcf > 60000) { | |
171 | PrintAndLog("Master token data"); | |
172 | PrintAndLog("%s", sprint_hex(data_buf+8, 14)); | |
173 | } else { | |
174 | PrintAndLog("Remaining Header Area"); | |
175 | PrintAndLog("%s", sprint_hex(data_buf+9, 13)); | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | ||
181 | uint8_t segCrcBytes[8] = {0x00}; | |
182 | uint32_t segCalcCRC = 0; | |
183 | uint32_t segCRC = 0; | |
184 | ||
185 | ||
186 | // Data card? | |
187 | if(dcf <= 60000) { | |
188 | ||
189 | PrintAndLog("\nADF: User Area"); | |
190 | PrintAndLog("------------------------------------------------------"); | |
191 | ||
192 | if(bIsSegmented) { | |
193 | ||
194 | // Data start point on segmented cards | |
195 | i = 22; | |
196 | ||
197 | // decode segments | |
198 | for (segmentNum=1; segmentNum < 128; segmentNum++ ) | |
199 | { | |
200 | segment_len = ((data_buf[i+1]^crc)&0x0f) * 256 + (data_buf[i]^crc); | |
201 | segment_flag = ((data_buf[i+1]^crc)&0xf0)>>4; | |
202 | wrp = (data_buf[i+2]^crc); | |
203 | wrc = ((data_buf[i+3]^crc)&0x70)>>4; | |
204 | ||
205 | bool hasWRC = (wrc > 0); | |
206 | bool hasWRP = (wrp > wrc); | |
207 | int wrp_len = (wrp - wrc); | |
208 | int remain_seg_payload_len = (segment_len - wrp - 5); | |
209 | ||
210 | // validate segment-crc | |
211 | segCrcBytes[0]=data_buf[0]; //uid0 | |
212 | segCrcBytes[1]=data_buf[1]; //uid1 | |
213 | segCrcBytes[2]=data_buf[2]; //uid2 | |
214 | segCrcBytes[3]=data_buf[3]; //uid3 | |
215 | segCrcBytes[4]=(data_buf[i]^crc); //hdr0 | |
216 | segCrcBytes[5]=(data_buf[i+1]^crc); //hdr1 | |
217 | segCrcBytes[6]=(data_buf[i+2]^crc); //hdr2 | |
218 | segCrcBytes[7]=(data_buf[i+3]^crc); //hdr3 | |
219 | ||
220 | segCalcCRC = CRC8Legic(segCrcBytes, 8); | |
221 | segCRC = data_buf[i+4]^crc; | |
222 | ||
223 | 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)", | |
224 | segmentNum, | |
225 | data_buf[i]^crc, | |
226 | data_buf[i+1]^crc, | |
227 | data_buf[i+2]^crc, | |
228 | data_buf[i+3]^crc, | |
229 | segment_len, | |
230 | segment_flag, | |
231 | (segment_flag & 0x4) >> 2, | |
232 | (segment_flag & 0x8) >> 3, | |
233 | wrp, | |
234 | wrc, | |
235 | ((data_buf[i+3]^crc) & 0x80) >> 7, | |
236 | segCRC, | |
237 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
238 | ); | |
239 | ||
240 | i += 5; | |
241 | ||
242 | if ( hasWRC ) { | |
243 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
244 | PrintAndLog("\nrow | data"); | |
245 | PrintAndLog("-----+------------------------------------------------"); | |
246 | ||
247 | for ( k=i; k < (i+wrc); ++k) | |
248 | data_buf[k] ^= crc; | |
249 | ||
250 | print_hex_break( data_buf+i, wrc, 16); | |
251 | ||
252 | i += wrc; | |
253 | } | |
254 | ||
255 | if ( hasWRP ) { | |
256 | PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len); | |
257 | PrintAndLog("\nrow | data"); | |
258 | PrintAndLog("-----+------------------------------------------------"); | |
259 | ||
260 | for (k=i; k < (i+wrp_len); ++k) | |
261 | data_buf[k] ^= crc; | |
262 | ||
263 | print_hex_break( data_buf+i, wrp_len, 16); | |
264 | ||
265 | i += wrp_len; | |
266 | ||
267 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
268 | if( wrp_len == 8 ) | |
269 | PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc); | |
270 | } | |
271 | ||
272 | PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len); | |
273 | PrintAndLog("\nrow | data"); | |
274 | PrintAndLog("-----+------------------------------------------------"); | |
275 | ||
276 | for ( k=i; k < (i+remain_seg_payload_len); ++k) | |
277 | data_buf[k] ^= crc; | |
278 | ||
279 | print_hex_break( data_buf+i, remain_seg_payload_len, 16); | |
280 | ||
281 | i += remain_seg_payload_len; | |
282 | ||
283 | PrintAndLog("-----+------------------------------------------------\n"); | |
284 | ||
285 | // end with last segment | |
286 | if (segment_flag & 0x8) return 0; | |
287 | ||
288 | } // end for loop | |
289 | ||
290 | } else { | |
291 | ||
292 | // Data start point on unsegmented cards | |
293 | i = 8; | |
294 | ||
295 | wrp = data_buf[7] & 0x0F; | |
296 | wrc = (data_buf[7] & 0x07) >> 4; | |
297 | ||
298 | bool hasWRC = (wrc > 0); | |
299 | bool hasWRP = (wrp > wrc); | |
300 | int wrp_len = (wrp - wrc); | |
301 | int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!? | |
302 | ||
303 | PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u", | |
304 | wrp, | |
305 | wrc, | |
306 | (data_buf[7] & 0x80) >> 7 | |
307 | ); | |
308 | ||
309 | if ( hasWRC ) { | |
310 | PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc); | |
311 | PrintAndLog("\nrow | data"); | |
312 | PrintAndLog("-----+------------------------------------------------"); | |
313 | print_hex_break( data_buf+i, wrc, 16); | |
314 | i += wrc; | |
315 | } | |
316 | ||
317 | if ( hasWRP ) { | |
318 | PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len); | |
319 | PrintAndLog("\nrow | data"); | |
320 | PrintAndLog("-----+------------------------------------------------"); | |
321 | print_hex_break( data_buf+i, wrp_len, 16); | |
322 | i += wrp_len; | |
323 | ||
324 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
325 | if( wrp_len == 8 ) | |
326 | PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4], data_buf[i-3], data_buf[i-2]); | |
327 | } | |
328 | ||
329 | PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len); | |
330 | PrintAndLog("\nrow | data"); | |
331 | PrintAndLog("-----+------------------------------------------------"); | |
332 | print_hex_break( data_buf+i, remain_seg_payload_len, 16); | |
333 | i += remain_seg_payload_len; | |
334 | ||
335 | PrintAndLog("-----+------------------------------------------------\n"); | |
336 | } | |
337 | } | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | int CmdLegicRFRead(const char *Cmd) { | |
343 | ||
344 | // params: | |
345 | // offset in data | |
346 | // number of bytes. | |
347 | char cmdp = param_getchar(Cmd, 0); | |
348 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_read(); | |
349 | ||
350 | int byte_count=0, offset=0; | |
351 | sscanf(Cmd, "%i %i", &offset, &byte_count); | |
352 | if(byte_count == 0) byte_count = -1; | |
353 | if(byte_count + offset > 1024) byte_count = 1024 - offset; | |
354 | ||
355 | UsbCommand c= {CMD_READER_LEGIC_RF, {offset, byte_count, 0}}; | |
356 | clearCommandBuffer(); | |
357 | SendCommand(&c); | |
358 | return 0; | |
359 | } | |
360 | ||
361 | int CmdLegicLoad(const char *Cmd) { | |
362 | ||
363 | char cmdp = param_getchar(Cmd, 0); | |
364 | if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load(); | |
365 | ||
366 | char filename[FILE_PATH_SIZE] = {0x00}; | |
367 | int len = strlen(Cmd); | |
368 | ||
369 | if (len > FILE_PATH_SIZE) { | |
370 | PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE); | |
371 | return 0; | |
372 | } | |
373 | memcpy(filename, Cmd, len); | |
374 | ||
375 | FILE *f = fopen(filename, "r"); | |
376 | if(!f) { | |
377 | PrintAndLog("couldn't open '%s'", Cmd); | |
378 | return -1; | |
379 | } | |
380 | ||
381 | char line[80]; | |
382 | int offset = 0; | |
383 | uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; | |
384 | int index = 0; | |
385 | int totalbytes = 0; | |
386 | while ( fgets(line, sizeof(line), f) ) { | |
387 | int res = sscanf(line, "%x %x %x %x %x %x %x %x", | |
388 | (unsigned int *)&data[index], | |
389 | (unsigned int *)&data[index + 1], | |
390 | (unsigned int *)&data[index + 2], | |
391 | (unsigned int *)&data[index + 3], | |
392 | (unsigned int *)&data[index + 4], | |
393 | (unsigned int *)&data[index + 5], | |
394 | (unsigned int *)&data[index + 6], | |
395 | (unsigned int *)&data[index + 7]); | |
396 | ||
397 | if(res != 8) { | |
398 | PrintAndLog("Error: could not read samples"); | |
399 | fclose(f); | |
400 | return -1; | |
401 | } | |
402 | index += res; | |
403 | ||
404 | if ( index == USB_CMD_DATA_SIZE ){ | |
405 | // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes); | |
406 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
407 | memcpy(c.d.asBytes, data, sizeof(data)); | |
408 | clearCommandBuffer(); | |
409 | SendCommand(&c); | |
410 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ | |
411 | PrintAndLog("Command execute timeout"); | |
412 | fclose(f); | |
413 | return 1; | |
414 | } | |
415 | offset += index; | |
416 | totalbytes += index; | |
417 | index = 0; | |
418 | } | |
419 | } | |
420 | fclose(f); | |
421 | ||
422 | // left over bytes? | |
423 | if ( index != 0 ) { | |
424 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
425 | memcpy(c.d.asBytes, data, 8); | |
426 | clearCommandBuffer(); | |
427 | SendCommand(&c); | |
428 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ | |
429 | PrintAndLog("Command execute timeout"); | |
430 | return 1; | |
431 | } | |
432 | totalbytes += index; | |
433 | } | |
434 | ||
435 | PrintAndLog("loaded %u samples", totalbytes); | |
436 | return 0; | |
437 | } | |
438 | ||
439 | int CmdLegicSave(const char *Cmd) { | |
440 | int requested = 1024; | |
441 | int offset = 0; | |
442 | int delivered = 0; | |
443 | char filename[FILE_PATH_SIZE]; | |
444 | uint8_t got[1024] = {0x00}; | |
445 | ||
446 | sscanf(Cmd, " %s %i %i", filename, &requested, &offset); | |
447 | ||
448 | /* If no length given save entire legic read buffer */ | |
449 | /* round up to nearest 8 bytes so the saved data can be used with legicload */ | |
450 | if (requested == 0) | |
451 | requested = 1024; | |
452 | ||
453 | if (requested % 8 != 0) { | |
454 | int remainder = requested % 8; | |
455 | requested = requested + 8 - remainder; | |
456 | } | |
457 | ||
458 | if (offset + requested > sizeof(got)) { | |
459 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024"); | |
460 | return 0; | |
461 | } | |
462 | ||
463 | GetFromBigBuf(got, requested, offset); | |
464 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ | |
465 | PrintAndLog("Command execute timeout"); | |
466 | return 1; | |
467 | } | |
468 | ||
469 | FILE *f = fopen(filename, "w"); | |
470 | if(!f) { | |
471 | PrintAndLog("couldn't open '%s'", Cmd+1); | |
472 | return -1; | |
473 | } | |
474 | ||
475 | for (int j = 0; j < requested; j += 8) { | |
476 | fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
477 | got[j+0], got[j+1], got[j+2], got[j+3], | |
478 | got[j+4], got[j+5], got[j+6], got[j+7] | |
479 | ); | |
480 | delivered += 8; | |
481 | if (delivered >= requested) break; | |
482 | } | |
483 | ||
484 | fclose(f); | |
485 | PrintAndLog("saved %u samples", delivered); | |
486 | return 0; | |
487 | } | |
488 | ||
489 | //TODO: write a help text (iceman) | |
490 | int CmdLegicRfSim(const char *Cmd) { | |
491 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; | |
492 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); | |
493 | clearCommandBuffer(); | |
494 | SendCommand(&c); | |
495 | return 0; | |
496 | } | |
497 | ||
498 | //TODO: write a help text (iceman) | |
499 | int CmdLegicRfWrite(const char *Cmd) { | |
500 | UsbCommand c = {CMD_WRITER_LEGIC_RF}; | |
501 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]); | |
502 | if(res != 2) { | |
503 | PrintAndLog("Please specify the offset and length as two hex strings"); | |
504 | return -1; | |
505 | } | |
506 | clearCommandBuffer(); | |
507 | SendCommand(&c); | |
508 | return 0; | |
509 | } | |
510 | ||
511 | //TODO: write a help text (iceman) | |
512 | int CmdLegicRfRawWrite(const char *Cmd) { | |
513 | char answer; | |
514 | UsbCommand c = { CMD_RAW_WRITER_LEGIC_RF, {0,0,0} }; | |
515 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]); | |
516 | if(res != 2) { | |
517 | PrintAndLog("Please specify the offset and value as two hex strings"); | |
518 | return -1; | |
519 | } | |
520 | ||
521 | if (c.arg[0] == 0x05 || c.arg[0] == 0x06) { | |
522 | PrintAndLog("############# DANGER !! #############"); | |
523 | PrintAndLog("# changing the DCF is irreversible #"); | |
524 | PrintAndLog("#####################################"); | |
525 | PrintAndLog("do youe really want to continue? y(es) n(o)"); | |
526 | scanf(" %c", &answer); | |
527 | if (answer == 'y' || answer == 'Y') { | |
528 | SendCommand(&c); | |
529 | return 0; | |
530 | } | |
531 | return -1; | |
532 | } | |
533 | ||
534 | clearCommandBuffer(); | |
535 | SendCommand(&c); | |
536 | return 0; | |
537 | } | |
538 | ||
539 | //TODO: write a help text (iceman) | |
540 | int CmdLegicRfFill(const char *Cmd) { | |
541 | UsbCommand cmd = {CMD_WRITER_LEGIC_RF, {0,0,0} }; | |
542 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]); | |
543 | if(res != 3) { | |
544 | PrintAndLog("Please specify the offset, length and value as two hex strings"); | |
545 | return -1; | |
546 | } | |
547 | ||
548 | int i; | |
549 | UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}}; | |
550 | memcpy(c.d.asBytes, cmd.arg[2], 48); | |
551 | ||
552 | for(i = 0; i < 22; i++) { | |
553 | c.arg[0] = i*48; | |
554 | ||
555 | clearCommandBuffer(); | |
556 | SendCommand(&c); | |
557 | WaitForResponse(CMD_ACK, NULL); | |
558 | } | |
559 | clearCommandBuffer(); | |
560 | SendCommand(&cmd); | |
561 | return 0; | |
562 | } | |
563 | ||
564 | int CmdLegicCalcCrc8(const char *Cmd){ | |
565 | ||
566 | uint8_t *data; | |
567 | uint8_t cmdp = 0, uidcrc = 0, type=0; | |
568 | bool errors = false; | |
569 | int len = 0; | |
570 | ||
571 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
572 | switch(param_getchar(Cmd, cmdp)) { | |
573 | case 'b': | |
574 | case 'B': | |
575 | data = malloc(len); | |
576 | if ( data == NULL ) { | |
577 | PrintAndLog("Can't allocate memory. exiting"); | |
578 | errors = true; | |
579 | break; | |
580 | } | |
581 | param_gethex_ex(Cmd, cmdp+1, data, &len); | |
582 | // if odd symbols, (hexbyte must be two symbols) | |
583 | if ( len & 1 ) errors = true; | |
584 | ||
585 | len >>= 1; | |
586 | cmdp += 2; | |
587 | break; | |
588 | case 'u': | |
589 | case 'U': | |
590 | uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16); | |
591 | cmdp += 2; | |
592 | break; | |
593 | case 'c': | |
594 | case 'C': | |
595 | type = param_get8ex(Cmd, cmdp+1, 0, 10); | |
596 | cmdp += 2; | |
597 | break; | |
598 | case 'h': | |
599 | case 'H': | |
600 | errors = true; | |
601 | break; | |
602 | default: | |
603 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
604 | errors = true; | |
605 | break; | |
606 | } | |
607 | if (errors) break; | |
608 | } | |
609 | //Validations | |
610 | if (errors){ | |
611 | if (data != NULL) free(data); | |
612 | return usage_legic_calccrc8(); | |
613 | } | |
614 | ||
615 | switch (type){ | |
616 | case 16: | |
617 | PrintAndLog("LEGIC CRC16: %X", CRC16Legic(data, len, uidcrc)); | |
618 | break; | |
619 | default: | |
620 | PrintAndLog("LEGIC CRC8: %X", CRC8Legic(data, len) ); | |
621 | break; | |
622 | } | |
623 | ||
624 | free(data); | |
625 | return 0; | |
626 | } | |
627 | ||
628 | static command_t CommandTable[] = { | |
629 | {"help", CmdHelp, 1, "This help"}, | |
630 | {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"}, | |
631 | {"read", CmdLegicRFRead, 0, "[offset][length] -- read bytes from a LEGIC card"}, | |
632 | {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"}, | |
633 | {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"}, | |
634 | {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"}, | |
635 | {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"}, | |
636 | {"writeRaw",CmdLegicRfRawWrite, 0, "<address> <value> -- Write direct to address"}, | |
637 | {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"}, | |
638 | {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"}, | |
639 | {NULL, NULL, 0, NULL} | |
640 | }; | |
641 | ||
642 | int CmdHFLegic(const char *Cmd) { | |
643 | clearCommandBuffer(); | |
644 | CmdsParse(CommandTable, Cmd); | |
645 | return 0; | |
646 | } | |
647 | ||
648 | int CmdHelp(const char *Cmd) { | |
649 | CmdsHelp(CommandTable); | |
650 | return 0; | |
651 | } |