add function to set mifare key from internal eeprom
[librfid] / src / rfid_proto_mifare_classic.c
index fa9b4e2..a6e2e4f 100644 (file)
@@ -1,7 +1,7 @@
 
 /* Mifare Classic implementation, PCD side.
  *
- * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2008 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
@@ -115,7 +115,9 @@ mfcl_getopt(struct rfid_protocol_handle *ph, int optname, void *optval,
 {
        int ret = -EINVAL;
        u_int8_t atqa[2];
+       u_int8_t sak;
        unsigned int atqa_size = sizeof(atqa);
+       unsigned int sak_size = sizeof(sak);
        unsigned int *size = optval;
 
        switch (optname) {
@@ -126,9 +128,15 @@ mfcl_getopt(struct rfid_protocol_handle *ph, int optname, void *optval,
                ret = 0;
                rfid_layer2_getopt(ph->l2h, RFID_OPT_14443A_ATQA,
                                   atqa, &atqa_size);
-               if (atqa[0] == 0x04 && atqa[1] == 0x00)
-                       *size = 1024;
-               else if (atqa[0] == 0x02 && atqa[1] == 0x00)
+               rfid_layer2_getopt(ph->l2h, RFID_OPT_14443A_SAK,
+                                  &sak, &sak_size);
+               if (atqa[0] == 0x04 && atqa[1] == 0x00) {
+                       if (sak == 0x09) {
+                               /* mifare mini */
+                               *size = 320;
+                       } else
+                               *size = 1024;
+               } else if (atqa[0] == 0x02 && atqa[1] == 0x00)
                        *size = 4096;
                else
                        ret = -EIO;
@@ -179,6 +187,14 @@ int mfcl_set_key(struct rfid_protocol_handle *ph, unsigned char *key)
        return ph->l2h->rh->reader->mifare_classic.setkey(ph->l2h->rh, key);
 }
 
+int mfcl_set_key_ee(struct rfid_protocol_handle *ph, unsigned int addr)
+{
+       if (!ph->l2h->rh->reader->mifare_classic.setkey_ee)
+               return -ENODEV;
+
+       return ph->l2h->rh->reader->mifare_classic.setkey_ee(ph->l2h->rh, addr);
+}
+
 int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
 {
        u_int32_t serno = *((u_int32_t *)ph->l2h->uid);
@@ -189,3 +205,33 @@ int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
        return ph->l2h->rh->reader->mifare_classic.auth(ph->l2h->rh, cmd,
                                                       serno, block);
 }
+
+int mfcl_block2sector(u_int8_t block)
+{
+       if (block < MIFARE_CL_SMALL_SECTORS * MIFARE_CL_BLOCKS_P_SECTOR_1k)
+               return block/MIFARE_CL_BLOCKS_P_SECTOR_1k;
+       else
+               return (block - MIFARE_CL_SMALL_SECTORS * MIFARE_CL_BLOCKS_P_SECTOR_1k)
+                                       / MIFARE_CL_BLOCKS_P_SECTOR_4k;
+}
+
+int mfcl_sector2block(u_int8_t sector)
+{
+       if (sector < MIFARE_CL_SMALL_SECTORS)
+               return sector * MIFARE_CL_BLOCKS_P_SECTOR_1k;
+       else if (sector < MIFARE_CL_SMALL_SECTORS + MIFARE_CL_LARGE_SECTORS)
+               return MIFARE_CL_SMALL_SECTORS * MIFARE_CL_BLOCKS_P_SECTOR_1k + 
+                       (sector - MIFARE_CL_SMALL_SECTORS) * MIFARE_CL_BLOCKS_P_SECTOR_4k; 
+       else
+               return -EINVAL;
+}
+
+int mfcl_sector_blocks(u_int8_t sector)
+{
+       if (sector < MIFARE_CL_SMALL_SECTORS)
+               return MIFARE_CL_BLOCKS_P_SECTOR_1k;
+       else if (sector < MIFARE_CL_SMALL_SECTORS + MIFARE_CL_LARGE_SECTORS)
+               return MIFARE_CL_BLOCKS_P_SECTOR_4k;
+       else
+               return -EINVAL;
+}