1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // High frequency Legic commands
9 //-----------------------------------------------------------------------------
10 #include "cmdhflegic.h"
12 static int CmdHelp(const char *Cmd
);
14 #define MAX_LENGTH 1024
16 int usage_legic_calccrc8(void){
17 PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes.");
18 PrintAndLog("There must be an even number of hexsymbols as input.");
19 PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc> c <crc type>");
20 PrintAndLog("Options:");
21 PrintAndLog(" h : this help");
22 PrintAndLog(" b <hexbytes> : hex bytes");
23 PrintAndLog(" u <uidcrc> : MCC hexbyte");
24 PrintAndLog(" c <crc type> : 8|16 bit crc size");
26 PrintAndLog("Samples:");
27 PrintAndLog(" hf legic crc8 b deadbeef1122");
28 PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A c 16");
31 int usage_legic_load(void){
32 PrintAndLog("It loads datasamples from the file `filename` to device memory");
33 PrintAndLog("Usage: hf legic load [h] <file name>");
34 PrintAndLog("Options:");
35 PrintAndLog(" h : this help");
36 PrintAndLog(" <filename> : Name of file to load");
38 PrintAndLog("Samples:");
39 PrintAndLog(" hf legic load filename");
42 int usage_legic_read(void){
43 PrintAndLog("Read data from a legic tag.");
44 PrintAndLog("Usage: hf legic read [h] <offset> <length> <IV>");
45 PrintAndLog("Options:");
46 PrintAndLog(" h : this help");
47 PrintAndLog(" <offset> : offset in data array to start download from");
48 PrintAndLog(" <length> : number of bytes to download");
49 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
51 PrintAndLog("Samples:");
52 PrintAndLog(" hf legic read");
53 PrintAndLog(" hf legic read 10 4");
56 int usage_legic_sim(void){
57 PrintAndLog("Missing help text.");
60 int usage_legic_write(void){
61 PrintAndLog(" Write sample buffer to a legic tag. (use after load or read)");
62 PrintAndLog("Usage: hf legic write [h] <offset> <length> <IV>");
63 PrintAndLog("Options:");
64 PrintAndLog(" h : this help");
65 PrintAndLog(" <offset> : offset in data array to start writing from");
66 PrintAndLog(" <length> : number of bytes to write");
67 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
69 PrintAndLog("Samples:");
70 PrintAndLog(" hf legic write");
71 PrintAndLog(" hf legic write 10 4");
74 int usage_legic_rawwrite(void){
75 PrintAndLog("Write raw data direct to a specific address on legic tag.");
76 PrintAndLog("Usage: hf legic writeraw [h] <address> <value> <IV>");
77 PrintAndLog("Options:");
78 PrintAndLog(" h : this help");
79 PrintAndLog(" <address> : address to write to");
80 PrintAndLog(" <value> : value to write");
81 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
83 PrintAndLog("Samples:");
84 PrintAndLog(" hf legic writeraw");
85 PrintAndLog(" hf legic writeraw 10 4");
88 int usage_legic_fill(void){
89 PrintAndLog("Missing help text.");
92 int usage_legic_info(void){
93 PrintAndLog("Read info from a legic tag.");
94 PrintAndLog("Usage: hf legic info [h]");
95 PrintAndLog("Options:");
96 PrintAndLog(" h : this help");
98 PrintAndLog("Samples:");
99 PrintAndLog(" hf legic info");
103 * Output BigBuf and deobfuscate LEGIC RF tag data.
104 * This is based on information given in the talk held
105 * by Henryk Ploetz and Karsten Nohl at 26c3
107 int CmdLegicDecode(const char *Cmd
) {
109 int i
= 0, k
= 0, segmentNum
= 0, segment_len
= 0, segment_flag
= 0;
110 int crc
= 0, wrp
= 0, wrc
= 0;
111 uint8_t stamp_len
= 0;
112 uint8_t data_buf
[1024]; // receiver buffer
113 char token_type
[5] = {0,0,0,0,0};
115 int bIsSegmented
= 0;
117 // copy data from proxmark into buffer
118 GetFromBigBuf(data_buf
,sizeof(data_buf
),0);
119 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 2000)){
120 PrintAndLog("Command execute timeout");
124 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
126 uint32_t calc_crc
= CRC8Legic(data_buf
, 4);
128 PrintAndLog("\nCDF: System Area");
129 PrintAndLog("------------------------------------------------------");
130 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
136 (calc_crc
== crc
) ? "OK":"Fail"
141 dcf
= ((int)data_buf
[6] << 8) | (int)data_buf
[5];
143 // New unwritten media?
146 PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)",
152 } else if(dcf
> 60000) { // Master token?
156 if(data_buf
[6] == 0xec) {
157 strncpy(token_type
, "XAM", sizeof(token_type
));
159 stamp_len
= 0x0c - (data_buf
[5] >> 4);
161 switch (data_buf
[5] & 0x7f) {
163 strncpy(token_type
, "IAM", sizeof(token_type
));
164 fl
= (0x2f - (data_buf
[5] & 0x7f)) + 1;
167 strncpy(token_type
, "SAM", sizeof(token_type
));
168 fl
= (0x6f - (data_buf
[5] & 0x7f)) + 1;
171 strncpy(token_type
, "GAM", sizeof(token_type
));
172 fl
= (0x7f - (data_buf
[5] & 0x7f)) + 1;
176 stamp_len
= 0xfc - data_buf
[6];
179 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u",
184 (data_buf
[5] & 0x80 )>> 7,
189 } else { // Is IM(-S) type of card...
191 if(data_buf
[7] == 0x9F && data_buf
[8] == 0xFF) {
193 strncpy(token_type
, "IM-S", sizeof(token_type
));
195 strncpy(token_type
, "IM", sizeof(token_type
));
198 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)",
203 (data_buf
[5]&0x80) >> 7
207 // Makes no sence to show this on blank media...
211 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x",
213 (data_buf
[7] & 0x70) >> 4,
214 (data_buf
[7] & 0x80) >> 7,
219 // Header area is only available on IM-S cards, on master tokens this data is the master token data itself
220 if(bIsSegmented
|| dcf
> 60000) {
222 PrintAndLog("Master token data");
223 PrintAndLog("%s", sprint_hex(data_buf
+8, 14));
225 PrintAndLog("Remaining Header Area");
226 PrintAndLog("%s", sprint_hex(data_buf
+9, 13));
231 uint8_t segCrcBytes
[8] = {0,0,0,0,0,0,0,0};
232 uint32_t segCalcCRC
= 0;
238 PrintAndLog("\nADF: User Area");
239 PrintAndLog("------------------------------------------------------");
243 // Data start point on segmented cards
247 for (segmentNum
=1; segmentNum
< 128; segmentNum
++ )
249 segment_len
= ((data_buf
[i
+1] ^ crc
) & 0x0f) * 256 + (data_buf
[i
] ^ crc
);
250 segment_flag
= ((data_buf
[i
+1] ^ crc
) & 0xf0) >> 4;
251 wrp
= (data_buf
[i
+2] ^ crc
);
252 wrc
= ((data_buf
[i
+3] ^ crc
) & 0x70) >> 4;
254 bool hasWRC
= (wrc
> 0);
255 bool hasWRP
= (wrp
> wrc
);
256 int wrp_len
= (wrp
- wrc
);
257 int remain_seg_payload_len
= (segment_len
- wrp
- 5);
259 // validate segment-crc
260 segCrcBytes
[0]=data_buf
[0]; //uid0
261 segCrcBytes
[1]=data_buf
[1]; //uid1
262 segCrcBytes
[2]=data_buf
[2]; //uid2
263 segCrcBytes
[3]=data_buf
[3]; //uid3
264 segCrcBytes
[4]=(data_buf
[i
] ^ crc
); //hdr0
265 segCrcBytes
[5]=(data_buf
[i
+1] ^ crc
); //hdr1
266 segCrcBytes
[6]=(data_buf
[i
+2] ^ crc
); //hdr2
267 segCrcBytes
[7]=(data_buf
[i
+3] ^ crc
); //hdr3
269 segCalcCRC
= CRC8Legic(segCrcBytes
, 8);
270 segCRC
= data_buf
[i
+4] ^ crc
;
272 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)",
280 (segment_flag
& 0x4) >> 2,
281 (segment_flag
& 0x8) >> 3,
284 ((data_buf
[i
+3]^crc
) & 0x80) >> 7,
286 ( segCRC
== segCalcCRC
) ? "OK" : "fail"
292 PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i
, k
, wrc
);
293 PrintAndLog("\nrow | data");
294 PrintAndLog("-----+------------------------------------------------");
296 for ( k
=i
; k
< (i
+ wrc
); ++k
)
299 print_hex_break( data_buf
+i
, wrc
, 16);
305 PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i
, k
, wrc
, wrp
, wrp_len
);
306 PrintAndLog("\nrow | data");
307 PrintAndLog("-----+------------------------------------------------");
309 for (k
=i
; k
< (i
+wrp_len
); ++k
)
312 print_hex_break( data_buf
+i
, wrp_len
, 16);
316 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
318 PrintAndLog("Card ID: %2X%02X%02X", data_buf
[i
-4]^crc
, data_buf
[i
-3]^crc
, data_buf
[i
-2]^crc
);
321 PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i
, k
, remain_seg_payload_len
);
322 PrintAndLog("\nrow | data");
323 PrintAndLog("-----+------------------------------------------------");
325 for ( k
=i
; k
< (i
+remain_seg_payload_len
); ++k
)
328 print_hex_break( data_buf
+i
, remain_seg_payload_len
, 16);
330 i
+= remain_seg_payload_len
;
332 PrintAndLog("-----+------------------------------------------------\n");
334 // end with last segment
335 if (segment_flag
& 0x8) return 0;
341 // Data start point on unsegmented cards
344 wrp
= data_buf
[7] & 0x0F;
345 wrc
= (data_buf
[7] & 0x70) >> 4;
347 bool hasWRC
= (wrc
> 0);
348 bool hasWRP
= (wrp
> wrc
);
349 int wrp_len
= (wrp
- wrc
);
350 int remain_seg_payload_len
= (1024 - 22 - wrp
); // Any chance to get physical card size here!?
352 PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u",
355 (data_buf
[7] & 0x80) >> 7
359 PrintAndLog("WRC protected area: (I %d | WRC %d)", i
, wrc
);
360 PrintAndLog("\nrow | data");
361 PrintAndLog("-----+------------------------------------------------");
362 print_hex_break( data_buf
+i
, wrc
, 16);
367 PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i
, wrc
, wrp
, wrp_len
);
368 PrintAndLog("\nrow | data");
369 PrintAndLog("-----+------------------------------------------------");
370 print_hex_break( data_buf
+ i
, wrp_len
, 16);
373 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
375 PrintAndLog("Card ID: %2X%02X%02X", data_buf
[i
-4], data_buf
[i
-3], data_buf
[i
-2]);
378 PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i
, remain_seg_payload_len
);
379 PrintAndLog("\nrow | data");
380 PrintAndLog("-----+------------------------------------------------");
381 print_hex_break( data_buf
+ i
, remain_seg_payload_len
, 16);
382 i
+= remain_seg_payload_len
;
384 PrintAndLog("-----+------------------------------------------------\n");
390 int CmdLegicRFRead(const char *Cmd
) {
395 char cmdp
= param_getchar(Cmd
, 0);
396 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_read();
398 uint32_t offset
= 0, len
= 0, IV
= 1;
399 sscanf(Cmd
, "%x %x %x", &offset
, &len
, &IV
);
401 // OUT-OF-BOUNDS check
402 if ( len
+ offset
> MAX_LENGTH
) {
403 len
= MAX_LENGTH
- offset
;
404 PrintAndLog("Out-of-bound, shorten len to %d",len
);
407 if ( (IV
& 0x7F) != IV
){
409 PrintAndLog("Truncating IV to 7bits");
412 if ( (IV
& 1) == 0 ){
414 PrintAndLog("LSB of IV must be SET");
417 PrintAndLog("Using IV: 0x%02x", IV
);
419 UsbCommand c
= {CMD_READER_LEGIC_RF
, {offset
, len
, IV
}};
420 clearCommandBuffer();
423 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
424 uint8_t isOK
= resp
.arg
[0] & 0xFF;
425 uint16_t len
= resp
.arg
[1] & 0x3FF;
427 PrintAndLog("use 'hf legic decode'");
429 uint8_t *data
= resp
.d
.asBytes
;
430 PrintAndLog("\nData |");
431 PrintAndLog("-----------------------------");
432 PrintAndLog(" %s|\n", sprint_hex(data
, len
));
435 PrintAndLog("command execution time out");
441 int CmdLegicLoad(const char *Cmd
) {
443 // iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :)
444 char cmdp
= param_getchar(Cmd
, 0);
445 if ( cmdp
== 'H' || cmdp
== 'h' || cmdp
== 0x00) return usage_legic_load();
447 char filename
[FILE_PATH_SIZE
] = {0x00};
448 int len
= strlen(Cmd
);
450 if (len
> FILE_PATH_SIZE
) {
451 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len
, FILE_PATH_SIZE
);
454 memcpy(filename
, Cmd
, len
);
456 FILE *f
= fopen(filename
, "r");
458 PrintAndLog("couldn't open '%s'", Cmd
);
464 uint8_t data
[USB_CMD_DATA_SIZE
] = {0x00};
467 while ( fgets(line
, sizeof(line
), f
) ) {
468 int res
= sscanf(line
, "%x %x %x %x %x %x %x %x",
469 (unsigned int *)&data
[index
],
470 (unsigned int *)&data
[index
+ 1],
471 (unsigned int *)&data
[index
+ 2],
472 (unsigned int *)&data
[index
+ 3],
473 (unsigned int *)&data
[index
+ 4],
474 (unsigned int *)&data
[index
+ 5],
475 (unsigned int *)&data
[index
+ 6],
476 (unsigned int *)&data
[index
+ 7]);
479 PrintAndLog("Error: could not read samples");
485 if ( index
== USB_CMD_DATA_SIZE
){
486 // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
487 UsbCommand c
= { CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
488 memcpy(c
.d
.asBytes
, data
, sizeof(data
));
489 clearCommandBuffer();
491 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 1500)){
492 PrintAndLog("Command execute timeout");
505 UsbCommand c
= { CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
506 memcpy(c
.d
.asBytes
, data
, 8);
507 clearCommandBuffer();
509 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 1500)){
510 PrintAndLog("Command execute timeout");
516 PrintAndLog("loaded %u samples", totalbytes
);
520 int CmdLegicSave(const char *Cmd
) {
521 int requested
= 1024;
524 char filename
[FILE_PATH_SIZE
] = {0x00};
525 uint8_t got
[1024] = {0x00};
527 memset(filename
, 0, FILE_PATH_SIZE
);
529 sscanf(Cmd
, " %s %i %i", filename
, &requested
, &offset
);
531 /* If no length given save entire legic read buffer */
532 /* round up to nearest 8 bytes so the saved data can be used with legicload */
536 if (requested
% 8 != 0) {
537 int remainder
= requested
% 8;
538 requested
= requested
+ 8 - remainder
;
541 if (offset
+ requested
> sizeof(got
)) {
542 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
546 GetFromBigBuf(got
, requested
, offset
);
547 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 2000)){
548 PrintAndLog("Command execute timeout");
552 FILE *f
= fopen(filename
, "w");
554 PrintAndLog("couldn't open '%s'", Cmd
+1);
558 for (int j
= 0; j
< requested
; j
+= 8) {
559 fprintf(f
, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
560 got
[j
+0], got
[j
+1], got
[j
+2], got
[j
+3],
561 got
[j
+4], got
[j
+5], got
[j
+6], got
[j
+7]
564 if (delivered
>= requested
) break;
568 PrintAndLog("saved %u samples", delivered
);
572 //TODO: write a help text (iceman)
573 int CmdLegicRfSim(const char *Cmd
) {
574 UsbCommand c
= {CMD_SIMULATE_TAG_LEGIC_RF
, {6,3,0}};
575 sscanf(Cmd
, " %"lli
" %"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1], &c
.arg
[2]);
576 clearCommandBuffer();
581 int CmdLegicRfWrite(const char *Cmd
) {
584 // offset - in tag memory
585 // length - num of bytes to be written
586 // IV - initialisation vector
588 char cmdp
= param_getchar(Cmd
, 0);
589 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_write();
591 uint32_t offset
= 0, len
= 0, IV
= 0;
593 UsbCommand c
= {CMD_WRITER_LEGIC_RF
, {0,0,0}};
594 int res
= sscanf(Cmd
, "%x %x %x", &offset
, &len
, &IV
);
596 PrintAndLog("Please specify the offset and length as two hex strings and, optionally, the IV also as an hex string");
600 // OUT-OF-BOUNDS check
601 if(len
+ offset
> MAX_LENGTH
) len
= MAX_LENGTH
- offset
;
603 if ( (IV
& 0x7F) != IV
){
605 PrintAndLog("Truncating IV to 7bits");
607 if ( (IV
& 1) == 0 ){
608 IV
|= 0x01; // IV must be odd
609 PrintAndLog("LSB of IV must be SET");
612 PrintAndLog("Current IV: 0x%02x", IV
);
618 clearCommandBuffer();
623 int CmdLegicRfRawWrite(const char *Cmd
) {
625 char cmdp
= param_getchar(Cmd
, 0);
626 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_rawwrite();
628 uint32_t address
= 0, data
= 0, IV
= 0;
631 UsbCommand c
= { CMD_RAW_WRITER_LEGIC_RF
, {0,0,0} };
632 int res
= sscanf(Cmd
, "%x %x %x", &address
, &data
, &IV
);
634 return usage_legic_rawwrite();
636 // OUT-OF-BOUNDS check
637 if(address
> MAX_LENGTH
)
638 return usage_legic_rawwrite();
640 if ( (IV
& 0x7F) != IV
){
642 PrintAndLog("Truncating IV to 7bits");
644 if ( (IV
& 1) == 0 ){
645 IV
|= 0x01; // IV must be odd
646 PrintAndLog("LSB of IV must be SET");
648 PrintAndLog("Current IV: 0x%02x", IV
);
654 if (c
.arg
[0] == 0x05 || c
.arg
[0] == 0x06) {
655 PrintAndLog("############# DANGER !! #############");
656 PrintAndLog("# changing the DCF is irreversible #");
657 PrintAndLog("#####################################");
658 PrintAndLog("do youe really want to continue? y(es) n(o)");
659 if (scanf(" %c", &answer
) > 0 && (answer
== 'y' || answer
== 'Y')) {
666 clearCommandBuffer();
671 //TODO: write a help text (iceman)
672 int CmdLegicRfFill(const char *Cmd
) {
673 UsbCommand cmd
= {CMD_WRITER_LEGIC_RF
, {0,0,0} };
674 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
" 0x%"llx
, &cmd
.arg
[0], &cmd
.arg
[1], &cmd
.arg
[2]);
676 PrintAndLog("Please specify the offset, length and value as two hex strings");
681 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {0, 0, 0}};
682 memset(c
.d
.asBytes
, cmd
.arg
[2], 48);
684 for(i
= 0; i
< 22; i
++) {
687 clearCommandBuffer();
689 WaitForResponse(CMD_ACK
, NULL
);
691 clearCommandBuffer();
696 void static calc4(uint8_t *cmd
, uint8_t len
){
698 //crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, TRUE, TRUE);
699 crc_init(&crc
, 4, 0x19 >> 1, 0x5, 0);
702 crc_update(&crc
, 1, 1); /* CMD_READ */
703 crc_update(&crc
, cmd
[0], 8);
704 crc_update(&crc
, cmd
[1], 8);
705 printf("crc4 %X\n", reflect(crc_finish(&crc
), 4) ) ;
708 crc_update(&crc
, 1, 1); /* CMD_READ */
709 crc_update(&crc
, cmd
[0], 8);
710 crc_update(&crc
, cmd
[1], 8);
711 printf("crc4 %X\n", crc_finish(&crc
) ) ;
713 printf("---- old ---\n");
714 crc_update2(&crc
, 1, 1); /* CMD_READ */
715 crc_update2(&crc
, cmd
[0], 8);
716 crc_update2(&crc
, cmd
[1], 8);
717 printf("crc4 %X \n", reflect(crc_finish(&crc
), 4) ) ;
721 crc_update2(&crc
, 1, 1); /* CMD_READ */
722 crc_update2(&crc
, cmd
[0], 8);
723 crc_update2(&crc
, cmd
[1], 8);
724 printf("crc4 %X\n", crc_finish(&crc
) ) ;
727 int CmdLegicCalcCrc8(const char *Cmd
){
729 uint8_t *data
= NULL
;
730 uint8_t cmdp
= 0, uidcrc
= 0, type
=0;
735 while(param_getchar(Cmd
, cmdp
) != 0x00) {
736 switch(param_getchar(Cmd
, cmdp
)) {
739 // peek at length of the input string so we can
740 // figure out how many elements to malloc in "data"
742 if (param_getptr(Cmd
, &bg
, &en
, cmdp
+1)) {
748 // check that user entered even number of characters
749 // for hex data string
755 // it's possible for user to accidentally enter "b" parameter
756 // more than once - we have to clean previous malloc
757 if (data
) free(data
);
758 data
= malloc(len
>> 1);
759 if ( data
== NULL
) {
760 PrintAndLog("Can't allocate memory. exiting");
765 if (param_gethex(Cmd
, cmdp
+1, data
, len
)) {
775 uidcrc
= param_get8ex(Cmd
, cmdp
+1, 0, 16);
780 type
= param_get8ex(Cmd
, cmdp
+1, 0, 10);
788 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
796 if (data
) free(data
);
797 return usage_legic_calccrc8();
802 PrintAndLog("Legic crc16: %X", CRC16Legic(data
, len
, uidcrc
));
808 PrintAndLog("Legic crc8: %X", CRC8Legic(data
, len
) );
812 if (data
) free(data
);
816 int HFLegicInfo(const char *Cmd
, bool verbose
) {
818 char cmdp
= param_getchar(Cmd
, 0);
819 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_info();
821 UsbCommand c
= {CMD_LEGIC_INFO
, {0,0,0}};
822 clearCommandBuffer();
825 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 500)) {
826 if ( verbose
) PrintAndLog("command execution time out");
830 uint8_t isOK
= resp
.arg
[0] & 0xFF;
832 if ( verbose
) PrintAndLog("legic card select failed");
836 legic_card_select_t card
;
837 memcpy(&card
, (legic_card_select_t
*)resp
.d
.asBytes
, sizeof(legic_card_select_t
));
839 PrintAndLog(" UID : %s", sprint_hex(card
.uid
, sizeof(card
.uid
)));
840 switch(card
.cardsize
) {
844 PrintAndLog(" TYPE : MIM%d card (%d bytes)", card
.cardsize
, card
.cardsize
); break;
846 PrintAndLog("Unknown card format: %d", card
.cardsize
);
852 int CmdLegicInfo(const char *Cmd
){
853 return HFLegicInfo(Cmd
, TRUE
);
856 static command_t CommandTable
[] = {
857 {"help", CmdHelp
, 1, "This help"},
858 {"decode", CmdLegicDecode
, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
859 {"read", CmdLegicRFRead
, 0, "[offset][length] <iv> -- read bytes from a LEGIC card"},
860 {"save", CmdLegicSave
, 0, "<filename> [<length>] -- Store samples"},
861 {"load", CmdLegicLoad
, 0, "<filename> -- Restore samples"},
862 {"sim", CmdLegicRfSim
, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
863 {"write", CmdLegicRfWrite
,0, "<offset> <length> <iv> -- Write sample buffer (user after load or read)"},
864 {"writeraw",CmdLegicRfRawWrite
, 0, "<address> <value> <iv> -- Write direct to address"},
865 {"fill", CmdLegicRfFill
, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
866 {"crc8", CmdLegicCalcCrc8
, 1, "Calculate Legic CRC8 over given hexbytes"},
867 {"info", CmdLegicInfo
, 1, "Information"},
868 {NULL
, NULL
, 0, NULL
}
871 int CmdHFLegic(const char *Cmd
) {
872 clearCommandBuffer();
873 CmdsParse(CommandTable
, Cmd
);
877 int CmdHelp(const char *Cmd
) {
878 CmdsHelp(CommandTable
);