//-----------------------------------------------------------------------------
 // (c) 2009 Henryk Plötz <henryk@ploetzli.ch>
+//     2016 Iceman
 //
 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
 // at your option, any later version. See the LICENSE.txt file for the text of
 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
 
 #ifndef SHORT_COIL
-# define SHORT_COIL    LOW(GPIO_SSC_DOUT);
+# define SHORT_COIL     LOW(GPIO_SSC_DOUT);
 #endif
 #ifndef OPEN_COIL
 # define OPEN_COIL     HIGH(GPIO_SSC_DOUT);
                WaitTicks( (RWD_TIME_PAUSE) ); \
                OPEN_COIL; \
                WaitTicks((x)); \
-       } while (0) 
+       } while (0); 
 #endif
 
 // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces. 
                                
        for (; mask < BITMASK(bits); mask <<= 1) {      
                if (send & mask)
-                       COIL_PULSE(RWD_TIME_1);
+                       COIL_PULSE(RWD_TIME_1)
                else
-                       COIL_PULSE(RWD_TIME_0);
+                       COIL_PULSE(RWD_TIME_0)
        }
 
        // Final pause to mark the end of the frame
 
        int dcf = 0;
        int bIsSegmented = 0;
 
-       CmdLegicRdmem("0 21 55");
+       CmdLegicRdmem("0 22 55");
        
        // copy data from device
        GetEMLFromBigBuf(data, sizeof(data), 0);
        if (data) free(data);
        return 0;
 } 
- 
-int HFLegicReader(const char *Cmd, bool verbose) {
 
-       char cmdp = param_getchar(Cmd, 0);
-       if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_info();
-       
+int legic_print_type(uint32_t tagtype, uint8_t spaces){
+       char spc[11] = "          ";
+       spc[10]=0x00;
+       char *spacer = spc + (10-spaces);
+
+       if ( tagtype == 22 )    
+               PrintAndLog("%sTYPE : MIM%d card (outdated)", spacer, tagtype);
+       else if ( tagtype == 256 )
+               PrintAndLog("%sTYPE : MIM%d card (234 bytes)", spacer, tagtype);
+       else if ( tagtype == 1024 )
+               PrintAndLog("%sTYPE : MIM%d card (1002 bytes)", spacer, tagtype);
+       else
+               PrintAndLog("%sTYPE : Unknown %06x", spacer, tagtype);
+       return 0;
+}
+int legic_get_type(legic_card_select_t *card){
+
+       if ( card == NULL ) return 1;
+
        UsbCommand c = {CMD_LEGIC_INFO, {0,0,0}};
        clearCommandBuffer();
     SendCommand(&c);
        UsbCommand resp;
-       if (!WaitForResponseTimeout(CMD_ACK, &resp, 500)) {
-               if ( verbose ) PrintAndLog("command execution time out");
-               return 1;
-       }
+       if (!WaitForResponseTimeout(CMD_ACK, &resp, 500))
+               return 2;
        
        uint8_t isOK = resp.arg[0] & 0xFF;
-       if ( !isOK ) {
-               if ( verbose ) PrintAndLog("legic card select failed");
-               return 1;
-       }
+       if ( !isOK ) 
+               return 3;
+       
+       memcpy(card, (legic_card_select_t *)resp.d.asBytes, sizeof(legic_card_select_t));
+       return 0;
+}
+
+int HFLegicReader(const char *Cmd, bool verbose) {
+
+       char cmdp = param_getchar(Cmd, 0);
+       if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_reader();
        
        legic_card_select_t card;
-       memcpy(&card, (legic_card_select_t *)resp.d.asBytes, sizeof(legic_card_select_t));
-
-       PrintAndLog("  UID : %s", sprint_hex(card.uid, sizeof(card.uid)));
-       switch(card.cardsize) {
-               case 22:
-               case 256: 
-               case 1024:
-                       PrintAndLog(" TYPE : MIM%d card (%d bytes)", card.cardsize, card.cardsize); break;                              
-               default: {
-                       PrintAndLog("Unknown card format: %d", card.cardsize); 
+       switch(legic_get_type(&card)){
+               case 1: 
+                       if ( verbose ) PrintAndLog("command execution time out"); 
                        return 1;
-               }
+               case 2: 
+               case 3: 
+                       if ( verbose ) PrintAndLog("legic card select failed");
+                       return 2;
+               default: break;
        }
+       PrintAndLog(" UID : %s", sprint_hex(card.uid, sizeof(card.uid)));
+       legic_print_type(card.cardsize, 0);
        return 0;
 }
 int CmdLegicReader(const char *Cmd){
        char *fnameptr = filename;
        size_t fileNlen = 0;
        bool errors = false;
-       uint16_t dumplen = 0x100;
-       
-       char cmdp = param_getchar(Cmd, 0);
+       uint16_t dumplen;       
+       uint8_t cmdp = 0;
        
        while(param_getchar(Cmd, cmdp) != 0x00)
        {
        if(errors) return usage_legic_dump();
        
        // tagtype
-       //uint32_t tagtype = GetHF14AMfU_Type();
-       //if (tagtype == -1) return -1;
+       legic_card_select_t card;
+       if (legic_get_type(&card)) {
+               PrintAndLog("Failed to identify tagtype");
+               return -1;
+       }
+       dumplen = card.cardsize;
        
+       legic_print_type(dumplen, 0);   
+       PrintAndLog("Reading tag memory...");
+
        UsbCommand c = {CMD_READER_LEGIC_RF, {0x00, dumplen, 0x55}};
        clearCommandBuffer();
        SendCommand(&c);
 
 #include "legic.h" // legic_card_select_t struct
 
 int CmdHFLegic(const char *Cmd);
-int CmdLegicInfo(const char *Cmd);
 
+int CmdLegicInfo(const char *Cmd);
 int CmdLegicRdmem(const char *Cmd);
 int CmdLegicLoad(const char *Cmd);
 int CmdLegicSave(const char *Cmd);
 int CmdLegicRfWrite(const char *Cmd);
 int CmdLegicRfRawWrite(const char *Cmd);
 int CmdLegicRfFill(const char *Cmd);
-
 int CmdLegicCalcCrc8(const char *Cmd);
-
-int CmdLegicReader(const char *Cmd);
-int HFLegicReader(const char *Cmd, bool verbose);
-
 int CmdLegicDump(const char *Cmd);
+int CmdLegicReader(const char *Cmd);
 
+int HFLegicReader(const char *Cmd, bool verbose);
+int legic_print_type(uint32_t tagtype, uint8_t spaces);
+int legic_get_type(legic_card_select_t *card);
 
 int usage_legic_calccrc8(void);
 int usage_legic_load(void);