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 SESSION_IV 0x55
15 #define MAX_LENGTH 1024
17 int usage_legic_calccrc8(void){
18 PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes.");
19 PrintAndLog("There must be an even number of hexsymbols as input.");
20 PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc> c <crc type>");
21 PrintAndLog("Options:");
22 PrintAndLog(" h : this help");
23 PrintAndLog(" b <hexbytes> : hex bytes");
24 PrintAndLog(" u <uidcrc> : MCC hexbyte");
25 PrintAndLog(" c <crc type> : 8|16 bit crc size");
27 PrintAndLog("Samples:");
28 PrintAndLog(" hf legic crc8 b deadbeef1122");
29 PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A c 16");
32 int usage_legic_load(void){
33 PrintAndLog("It loads datasamples from the file `filename` to device memory");
34 PrintAndLog("Usage: hf legic load [h] <file name>");
35 PrintAndLog("Options:");
36 PrintAndLog(" h : this help");
37 PrintAndLog(" <filename> : Name of file to load");
39 PrintAndLog("Samples:");
40 PrintAndLog(" hf legic load filename");
43 int usage_legic_read(void){
44 PrintAndLog("Read data from a legic tag.");
45 PrintAndLog("Usage: hf legic read [h] <offset> <length> <IV>");
46 PrintAndLog("Options:");
47 PrintAndLog(" h : this help");
48 PrintAndLog(" <offset> : offset in data array to start download from");
49 PrintAndLog(" <length> : number of bytes to download");
50 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
52 PrintAndLog("Samples:");
53 PrintAndLog(" hf legic read");
54 PrintAndLog(" hf legic read 10 4");
57 int usage_legic_sim(void){
58 PrintAndLog("Missing help text.");
61 int usage_legic_write(void){
62 PrintAndLog(" Write sample buffer to a legic tag. (use after load or read)");
63 PrintAndLog("Usage: hf legic write [h] <offset> <length> <IV>");
64 PrintAndLog("Options:");
65 PrintAndLog(" h : this help");
66 PrintAndLog(" <offset> : offset in data array to start writing from");
67 PrintAndLog(" <length> : number of bytes to write");
68 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
70 PrintAndLog("Samples:");
71 PrintAndLog(" hf legic write");
72 PrintAndLog(" hf legic write 10 4");
75 int usage_legic_rawwrite(void){
76 PrintAndLog("Write raw data direct to a specific address on legic tag.");
77 PrintAndLog("Usage: hf legic writeraw [h] <address> <value> <IV>");
78 PrintAndLog("Options:");
79 PrintAndLog(" h : this help");
80 PrintAndLog(" <address> : address to write to");
81 PrintAndLog(" <value> : value to write");
82 PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)");
84 PrintAndLog("Samples:");
85 PrintAndLog(" hf legic writeraw");
86 PrintAndLog(" hf legic writeraw 10 4");
89 int usage_legic_fill(void){
90 PrintAndLog("Missing help text.");
93 int usage_legic_info(void){
94 PrintAndLog("Read info from a legic tag.");
95 PrintAndLog("Usage: hf legic info [h]");
96 PrintAndLog("Options:");
97 PrintAndLog(" h : this help");
99 PrintAndLog("Samples:");
100 PrintAndLog(" hf legic info");
104 * Output BigBuf and deobfuscate LEGIC RF tag data.
105 * This is based on information given in the talk held
106 * by Henryk Ploetz and Karsten Nohl at 26c3
108 int CmdLegicDecode(const char *Cmd
) {
110 int i
= 0, k
= 0, segmentNum
= 0, segment_len
= 0, segment_flag
= 0;
111 int crc
= 0, wrp
= 0, wrc
= 0;
112 uint8_t stamp_len
= 0;
113 uint8_t data_buf
[1024]; // receiver buffer
114 char token_type
[5] = {0,0,0,0,0};
116 int bIsSegmented
= 0;
118 // copy data from proxmark into buffer
119 GetFromBigBuf(data_buf
,sizeof(data_buf
),0);
120 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 2000)){
121 PrintAndLog("Command execute timeout");
125 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
127 uint32_t calc_crc
= CRC8Legic(data_buf
, 4);
129 PrintAndLog("\nCDF: System Area");
130 PrintAndLog("------------------------------------------------------");
131 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
137 (calc_crc
== crc
) ? "OK":"Fail"
142 dcf
= ((int)data_buf
[6] << 8) | (int)data_buf
[5];
144 // New unwritten media?
147 PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)",
153 } else if(dcf
> 60000) { // Master token?
157 if(data_buf
[6] == 0xec) {
158 strncpy(token_type
, "XAM", sizeof(token_type
));
160 stamp_len
= 0x0c - (data_buf
[5] >> 4);
162 switch (data_buf
[5] & 0x7f) {
164 strncpy(token_type
, "IAM", sizeof(token_type
));
165 fl
= (0x2f - (data_buf
[5] & 0x7f)) + 1;
168 strncpy(token_type
, "SAM", sizeof(token_type
));
169 fl
= (0x6f - (data_buf
[5] & 0x7f)) + 1;
172 strncpy(token_type
, "GAM", sizeof(token_type
));
173 fl
= (0x7f - (data_buf
[5] & 0x7f)) + 1;
177 stamp_len
= 0xfc - data_buf
[6];
180 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u",
185 (data_buf
[5] & 0x80 )>> 7,
190 } else { // Is IM(-S) type of card...
192 if(data_buf
[7] == 0x9F && data_buf
[8] == 0xFF) {
194 strncpy(token_type
, "IM-S", sizeof(token_type
));
196 strncpy(token_type
, "IM", sizeof(token_type
));
199 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)",
204 (data_buf
[5]&0x80) >> 7
208 // Makes no sence to show this on blank media...
212 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x",
214 (data_buf
[7] & 0x70) >> 4,
215 (data_buf
[7] & 0x80) >> 7,
220 // Header area is only available on IM-S cards, on master tokens this data is the master token data itself
221 if(bIsSegmented
|| dcf
> 60000) {
223 PrintAndLog("Master token data");
224 PrintAndLog("%s", sprint_hex(data_buf
+8, 14));
226 PrintAndLog("Remaining Header Area");
227 PrintAndLog("%s", sprint_hex(data_buf
+9, 13));
232 uint8_t segCrcBytes
[8] = {0,0,0,0,0,0,0,0};
233 uint32_t segCalcCRC
= 0;
239 PrintAndLog("\nADF: User Area");
240 PrintAndLog("------------------------------------------------------");
244 // Data start point on segmented cards
248 for (segmentNum
=1; segmentNum
< 128; segmentNum
++ )
250 segment_len
= ((data_buf
[i
+1] ^ crc
) & 0x0f) * 256 + (data_buf
[i
] ^ crc
);
251 segment_flag
= ((data_buf
[i
+1] ^ crc
) & 0xf0) >> 4;
252 wrp
= (data_buf
[i
+2] ^ crc
);
253 wrc
= ((data_buf
[i
+3] ^ crc
) & 0x70) >> 4;
255 bool hasWRC
= (wrc
> 0);
256 bool hasWRP
= (wrp
> wrc
);
257 int wrp_len
= (wrp
- wrc
);
258 int remain_seg_payload_len
= (segment_len
- wrp
- 5);
260 // validate segment-crc
261 segCrcBytes
[0]=data_buf
[0]; //uid0
262 segCrcBytes
[1]=data_buf
[1]; //uid1
263 segCrcBytes
[2]=data_buf
[2]; //uid2
264 segCrcBytes
[3]=data_buf
[3]; //uid3
265 segCrcBytes
[4]=(data_buf
[i
] ^ crc
); //hdr0
266 segCrcBytes
[5]=(data_buf
[i
+1] ^ crc
); //hdr1
267 segCrcBytes
[6]=(data_buf
[i
+2] ^ crc
); //hdr2
268 segCrcBytes
[7]=(data_buf
[i
+3] ^ crc
); //hdr3
270 segCalcCRC
= CRC8Legic(segCrcBytes
, 8);
271 segCRC
= data_buf
[i
+4] ^ crc
;
273 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)",
281 (segment_flag
& 0x4) >> 2,
282 (segment_flag
& 0x8) >> 3,
285 ((data_buf
[i
+3]^crc
) & 0x80) >> 7,
287 ( segCRC
== segCalcCRC
) ? "OK" : "fail"
293 PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i
, k
, wrc
);
294 PrintAndLog("\nrow | data");
295 PrintAndLog("-----+------------------------------------------------");
297 for ( k
=i
; k
< (i
+ wrc
); ++k
)
300 print_hex_break( data_buf
+i
, wrc
, 16);
306 PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i
, k
, wrc
, wrp
, wrp_len
);
307 PrintAndLog("\nrow | data");
308 PrintAndLog("-----+------------------------------------------------");
310 for (k
=i
; k
< (i
+wrp_len
); ++k
)
313 print_hex_break( data_buf
+i
, wrp_len
, 16);
317 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
319 PrintAndLog("Card ID: %2X%02X%02X", data_buf
[i
-4]^crc
, data_buf
[i
-3]^crc
, data_buf
[i
-2]^crc
);
322 PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i
, k
, remain_seg_payload_len
);
323 PrintAndLog("\nrow | data");
324 PrintAndLog("-----+------------------------------------------------");
326 for ( k
=i
; k
< (i
+remain_seg_payload_len
); ++k
)
329 print_hex_break( data_buf
+i
, remain_seg_payload_len
, 16);
331 i
+= remain_seg_payload_len
;
333 PrintAndLog("-----+------------------------------------------------\n");
335 // end with last segment
336 if (segment_flag
& 0x8) return 0;
342 // Data start point on unsegmented cards
345 wrp
= data_buf
[7] & 0x0F;
346 wrc
= (data_buf
[7] & 0x70) >> 4;
348 bool hasWRC
= (wrc
> 0);
349 bool hasWRP
= (wrp
> wrc
);
350 int wrp_len
= (wrp
- wrc
);
351 int remain_seg_payload_len
= (1024 - 22 - wrp
); // Any chance to get physical card size here!?
353 PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u",
356 (data_buf
[7] & 0x80) >> 7
360 PrintAndLog("WRC protected area: (I %d | WRC %d)", i
, wrc
);
361 PrintAndLog("\nrow | data");
362 PrintAndLog("-----+------------------------------------------------");
363 print_hex_break( data_buf
+i
, wrc
, 16);
368 PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i
, wrc
, wrp
, wrp_len
);
369 PrintAndLog("\nrow | data");
370 PrintAndLog("-----+------------------------------------------------");
371 print_hex_break( data_buf
+ i
, wrp_len
, 16);
374 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
376 PrintAndLog("Card ID: %2X%02X%02X", data_buf
[i
-4], data_buf
[i
-3], data_buf
[i
-2]);
379 PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i
, remain_seg_payload_len
);
380 PrintAndLog("\nrow | data");
381 PrintAndLog("-----+------------------------------------------------");
382 print_hex_break( data_buf
+ i
, remain_seg_payload_len
, 16);
383 i
+= remain_seg_payload_len
;
385 PrintAndLog("-----+------------------------------------------------\n");
391 int CmdLegicRFRead(const char *Cmd
) {
396 char cmdp
= param_getchar(Cmd
, 0);
397 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_read();
399 uint32_t offset
= 0, len
= 0, IV
= 1;
400 sscanf(Cmd
, "%x %x %x", &offset
, &len
, &IV
);
402 // OUT-OF-BOUNDS check
403 if(len
+ offset
> MAX_LENGTH
) len
= MAX_LENGTH
- offset
;
405 if ( (IV
& 0x7F) != IV
){
407 PrintAndLog("Truncating IV to 7bits");
409 if ( (IV
& 1) == 0 ){
410 IV
|= 0x01; // IV must be odd
411 PrintAndLog("LSB of IV must be SET");
413 PrintAndLog("Using IV: 0x%02x", IV
);
415 UsbCommand c
= {CMD_READER_LEGIC_RF
, {offset
, len
, IV
}};
416 clearCommandBuffer();
419 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 2000)) {
420 uint8_t isOK
= resp
.arg
[0] & 0xFF;
421 uint16_t len
= resp
.arg
[1] & 0x3FF;
423 PrintAndLog("use 'hf legic decode'");
425 uint8_t *data
= resp
.d
.asBytes
;
426 PrintAndLog("\nData |");
427 PrintAndLog("-----------------------------");
428 PrintAndLog(" %s|\n", sprint_hex(data
, len
));
431 PrintAndLog("command execution time out");
437 int CmdLegicLoad(const char *Cmd
) {
439 // iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :)
440 char cmdp
= param_getchar(Cmd
, 0);
441 if ( cmdp
== 'H' || cmdp
== 'h' || cmdp
== 0x00) return usage_legic_load();
443 char filename
[FILE_PATH_SIZE
] = {0x00};
444 int len
= strlen(Cmd
);
446 if (len
> FILE_PATH_SIZE
) {
447 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len
, FILE_PATH_SIZE
);
450 memcpy(filename
, Cmd
, len
);
452 FILE *f
= fopen(filename
, "r");
454 PrintAndLog("couldn't open '%s'", Cmd
);
460 uint8_t data
[USB_CMD_DATA_SIZE
] = {0x00};
463 while ( fgets(line
, sizeof(line
), f
) ) {
464 int res
= sscanf(line
, "%x %x %x %x %x %x %x %x",
465 (unsigned int *)&data
[index
],
466 (unsigned int *)&data
[index
+ 1],
467 (unsigned int *)&data
[index
+ 2],
468 (unsigned int *)&data
[index
+ 3],
469 (unsigned int *)&data
[index
+ 4],
470 (unsigned int *)&data
[index
+ 5],
471 (unsigned int *)&data
[index
+ 6],
472 (unsigned int *)&data
[index
+ 7]);
475 PrintAndLog("Error: could not read samples");
481 if ( index
== USB_CMD_DATA_SIZE
){
482 // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
483 UsbCommand c
= { CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
484 memcpy(c
.d
.asBytes
, data
, sizeof(data
));
485 clearCommandBuffer();
487 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 1500)){
488 PrintAndLog("Command execute timeout");
501 UsbCommand c
= { CMD_DOWNLOADED_SIM_SAMPLES_125K
, {offset
, 0, 0}};
502 memcpy(c
.d
.asBytes
, data
, 8);
503 clearCommandBuffer();
505 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 1500)){
506 PrintAndLog("Command execute timeout");
512 PrintAndLog("loaded %u samples", totalbytes
);
516 int CmdLegicSave(const char *Cmd
) {
517 int requested
= 1024;
520 char filename
[FILE_PATH_SIZE
] = {0x00};
521 uint8_t got
[1024] = {0x00};
523 memset(filename
, 0, FILE_PATH_SIZE
);
525 sscanf(Cmd
, " %s %i %i", filename
, &requested
, &offset
);
527 /* If no length given save entire legic read buffer */
528 /* round up to nearest 8 bytes so the saved data can be used with legicload */
532 if (requested
% 8 != 0) {
533 int remainder
= requested
% 8;
534 requested
= requested
+ 8 - remainder
;
537 if (offset
+ requested
> sizeof(got
)) {
538 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
542 GetFromBigBuf(got
, requested
, offset
);
543 if ( !WaitForResponseTimeout(CMD_ACK
, NULL
, 2000)){
544 PrintAndLog("Command execute timeout");
548 FILE *f
= fopen(filename
, "w");
550 PrintAndLog("couldn't open '%s'", Cmd
+1);
554 for (int j
= 0; j
< requested
; j
+= 8) {
555 fprintf(f
, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
556 got
[j
+0], got
[j
+1], got
[j
+2], got
[j
+3],
557 got
[j
+4], got
[j
+5], got
[j
+6], got
[j
+7]
560 if (delivered
>= requested
) break;
564 PrintAndLog("saved %u samples", delivered
);
568 //TODO: write a help text (iceman)
569 int CmdLegicRfSim(const char *Cmd
) {
570 UsbCommand c
= {CMD_SIMULATE_TAG_LEGIC_RF
, {6,3,0}};
571 sscanf(Cmd
, " %"lli
" %"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1], &c
.arg
[2]);
572 clearCommandBuffer();
577 int CmdLegicRfWrite(const char *Cmd
) {
580 // offset - in tag memory
581 // length - num of bytes to be written
582 // IV - initialisation vector
584 char cmdp
= param_getchar(Cmd
, 0);
585 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_write();
587 uint32_t offset
= 0, len
= 0, IV
= 0;
589 UsbCommand c
= {CMD_WRITER_LEGIC_RF
, {0,0,0}};
590 int res
= sscanf(Cmd
, "%x %x %x", &offset
, &len
, &IV
);
592 PrintAndLog("Please specify the offset and length as two hex strings and, optionally, the IV also as an hex string");
596 // OUT-OF-BOUNDS check
597 if(len
+ offset
> MAX_LENGTH
) len
= MAX_LENGTH
- offset
;
599 if ( (IV
& 0x7F) != IV
){
601 PrintAndLog("Truncating IV to 7bits");
603 if ( (IV
& 1) == 0 ){
604 IV
|= 0x01; // IV must be odd
605 PrintAndLog("LSB of IV must be SET");
608 PrintAndLog("Current IV: 0x%02x", IV
);
614 clearCommandBuffer();
619 int CmdLegicRfRawWrite(const char *Cmd
) {
621 char cmdp
= param_getchar(Cmd
, 0);
622 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_rawwrite();
624 uint32_t address
= 0, data
= 0, IV
= 0;
627 UsbCommand c
= { CMD_RAW_WRITER_LEGIC_RF
, {0,0,0} };
628 int res
= sscanf(Cmd
, "%x %x %x", &address
, &data
, &IV
);
630 return usage_legic_rawwrite();
632 // OUT-OF-BOUNDS check
633 if(address
> MAX_LENGTH
)
634 return usage_legic_rawwrite();
636 if ( (IV
& 0x7F) != IV
){
638 PrintAndLog("Truncating IV to 7bits");
640 if ( (IV
& 1) == 0 ){
641 IV
|= 0x01; // IV must be odd
642 PrintAndLog("LSB of IV must be SET");
644 PrintAndLog("Current IV: 0x%02x", IV
);
650 if (c
.arg
[0] == 0x05 || c
.arg
[0] == 0x06) {
651 PrintAndLog("############# DANGER !! #############");
652 PrintAndLog("# changing the DCF is irreversible #");
653 PrintAndLog("#####################################");
654 PrintAndLog("do youe really want to continue? y(es) n(o)");
655 if (scanf(" %c", &answer
) > 0 && (answer
== 'y' || answer
== 'Y')) {
662 clearCommandBuffer();
667 //TODO: write a help text (iceman)
668 int CmdLegicRfFill(const char *Cmd
) {
669 UsbCommand cmd
= {CMD_WRITER_LEGIC_RF
, {0,0,0} };
670 int res
= sscanf(Cmd
, " 0x%"llx
" 0x%"llx
" 0x%"llx
, &cmd
.arg
[0], &cmd
.arg
[1], &cmd
.arg
[2]);
672 PrintAndLog("Please specify the offset, length and value as two hex strings");
677 UsbCommand c
= {CMD_DOWNLOADED_SIM_SAMPLES_125K
, {0, 0, 0}};
678 memset(c
.d
.asBytes
, cmd
.arg
[2], 48);
680 for(i
= 0; i
< 22; i
++) {
683 clearCommandBuffer();
685 WaitForResponse(CMD_ACK
, NULL
);
687 clearCommandBuffer();
692 void static calc4(uint8_t *cmd
, uint8_t len
){
694 //crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, TRUE, TRUE);
695 crc_init(&crc
, 4, 0x19 >> 1, 0x5, 0);
698 crc_update(&crc
, 1, 1); /* CMD_READ */
699 crc_update(&crc
, cmd
[0], 8);
700 crc_update(&crc
, cmd
[1], 8);
701 printf("crc4 %X\n", reflect(crc_finish(&crc
), 4) ) ;
704 crc_update(&crc
, 1, 1); /* CMD_READ */
705 crc_update(&crc
, cmd
[0], 8);
706 crc_update(&crc
, cmd
[1], 8);
707 printf("crc4 %X\n", crc_finish(&crc
) ) ;
709 printf("---- old ---\n");
710 crc_update2(&crc
, 1, 1); /* CMD_READ */
711 crc_update2(&crc
, cmd
[0], 8);
712 crc_update2(&crc
, cmd
[1], 8);
713 printf("crc4 %X \n", reflect(crc_finish(&crc
), 4) ) ;
717 crc_update2(&crc
, 1, 1); /* CMD_READ */
718 crc_update2(&crc
, cmd
[0], 8);
719 crc_update2(&crc
, cmd
[1], 8);
720 printf("crc4 %X\n", crc_finish(&crc
) ) ;
723 int CmdLegicCalcCrc8(const char *Cmd
){
725 uint8_t *data
= NULL
;
726 uint8_t cmdp
= 0, uidcrc
= 0, type
=0;
731 while(param_getchar(Cmd
, cmdp
) != 0x00) {
732 switch(param_getchar(Cmd
, cmdp
)) {
735 // peek at length of the input string so we can
736 // figure out how many elements to malloc in "data"
738 if (param_getptr(Cmd
, &bg
, &en
, cmdp
+1)) {
744 // check that user entered even number of characters
745 // for hex data string
751 // it's possible for user to accidentally enter "b" parameter
752 // more than once - we have to clean previous malloc
753 if (data
) free(data
);
754 data
= malloc(len
>> 1);
755 if ( data
== NULL
) {
756 PrintAndLog("Can't allocate memory. exiting");
761 if (param_gethex(Cmd
, cmdp
+1, data
, len
)) {
771 uidcrc
= param_get8ex(Cmd
, cmdp
+1, 0, 16);
776 type
= param_get8ex(Cmd
, cmdp
+1, 0, 10);
784 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
792 if (data
) free(data
);
793 return usage_legic_calccrc8();
798 PrintAndLog("Legic crc16: %X", CRC16Legic(data
, len
, uidcrc
));
804 PrintAndLog("Legic crc8: %X", CRC8Legic(data
, len
) );
808 if (data
) free(data
);
812 int HFLegicInfo(const char *Cmd
, bool verbose
) {
814 char cmdp
= param_getchar(Cmd
, 0);
815 if ( cmdp
== 'H' || cmdp
== 'h' ) return usage_legic_info();
817 UsbCommand c
= {CMD_LEGIC_INFO
, {0,0,0}};
818 clearCommandBuffer();
821 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)) {
822 uint8_t isOK
= resp
.arg
[0] & 0xFF;
823 uint16_t tagtype
= resp
.arg
[1] & 0xFFF;
825 PrintAndLog(" UID : %s", sprint_hex(resp
.d
.asBytes
, 4));
827 case 22: PrintAndLog("MIM22 card (22bytes)"); break;
828 case 256: PrintAndLog("MIM256 card (256bytes)"); break;
829 case 1024: PrintAndLog("MIM1024 card (1024bytes)"); break;
831 PrintAndLog("Unknown card format: %x", tagtype
);
836 if ( verbose
) PrintAndLog("legic card select failed");
840 if ( verbose
) PrintAndLog("command execution time out");
845 int CmdLegicInfo(const char *Cmd
){
846 return HFLegicInfo(Cmd
, TRUE
);
849 static command_t CommandTable
[] = {
850 {"help", CmdHelp
, 1, "This help"},
851 {"decode", CmdLegicDecode
, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
852 {"read", CmdLegicRFRead
, 0, "[offset][length] <iv> -- read bytes from a LEGIC card"},
853 {"save", CmdLegicSave
, 0, "<filename> [<length>] -- Store samples"},
854 {"load", CmdLegicLoad
, 0, "<filename> -- Restore samples"},
855 {"sim", CmdLegicRfSim
, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
856 {"write", CmdLegicRfWrite
,0, "<offset> <length> <iv> -- Write sample buffer (user after load or read)"},
857 {"writeraw",CmdLegicRfRawWrite
, 0, "<address> <value> <iv> -- Write direct to address"},
858 {"fill", CmdLegicRfFill
, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
859 {"crc8", CmdLegicCalcCrc8
, 1, "Calculate Legic CRC8 over given hexbytes"},
860 {"info", CmdLegicInfo
, 1, "Information"},
861 {NULL
, NULL
, 0, NULL
}
864 int CmdHFLegic(const char *Cmd
) {
865 clearCommandBuffer();
866 CmdsParse(CommandTable
, Cmd
);
870 int CmdHelp(const char *Cmd
) {
871 CmdsHelp(CommandTable
);