From: Harald Welte Date: Sun, 24 Feb 2008 12:35:52 +0000 (+0000) Subject: add function to set mifare key from internal eeprom X-Git-Url: http://git.rot13.org/?p=librfid;a=commitdiff_plain;h=967687ffce29d0034a16be9a0babdc91bf3187cd add function to set mifare key from internal eeprom git-svn-id: https://svn.gnumonks.org/trunk/librfid@2086 e0336214-984f-0b4b-a45f-81c69e1f0ede --- diff --git a/include/librfid/rfid_asic_rc632.h b/include/librfid/rfid_asic_rc632.h index c78a720..d8adac7 100644 --- a/include/librfid/rfid_asic_rc632.h +++ b/include/librfid/rfid_asic_rc632.h @@ -65,6 +65,8 @@ struct rfid_asic_rc632 { struct { int (*setkey)(struct rfid_asic_handle *h, const unsigned char *key); + int (*setkey_ee)(struct rfid_asic_handle *h, + const unsigned int addr); int (*auth)(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno, u_int8_t block); } mifare_classic; diff --git a/include/librfid/rfid_reader.h b/include/librfid/rfid_reader.h index 1c53677..27bcef3 100644 --- a/include/librfid/rfid_reader.h +++ b/include/librfid/rfid_reader.h @@ -70,6 +70,7 @@ struct rfid_reader { } iso15693; struct rfid_mifare_classic_reader { int (*setkey)(struct rfid_reader_handle *h, const unsigned char *key); + int (*setkey_ee)(struct rfid_reader_handle *h, const unsigned int addr); int (*auth)(struct rfid_reader_handle *h, u_int8_t cmd, u_int32_t serno, u_int8_t block); } mifare_classic; diff --git a/src/rfid_asic_rc632.c b/src/rfid_asic_rc632.c index 135c7d9..effe813 100644 --- a/src/rfid_asic_rc632.c +++ b/src/rfid_asic_rc632.c @@ -1924,6 +1924,52 @@ rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key) return 0; } +static int +rc632_mifare_set_key_ee(struct rfid_asic_handle *h, unsigned int addr) +{ + int ret; + u_int8_t cmd_addr[2]; + u_int8_t reg; + + if (addr > 0xffff - RFID_MIFARE_KEY_CODED_LEN) + return -EINVAL; + + cmd_addr[0] = addr & 0xff; /* LSB */ + cmd_addr[1] = (addr >> 8) & 0xff; /* MSB */ + + /* Terminate probably running command */ + ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE); + if (ret < 0) + return ret; + + /* Write the key address to the FIFO */ + ret = rc632_fifo_write(h, 2, cmd_addr, 0x03); + if (ret < 0) + return ret; + + ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY_E2); + if (ret < 0) + return ret; + + ret = rc632_timer_set(h, RC632_TMO_AUTH1); + if (ret < 0) + return ret; + + //ret = rc632_wait_idle(h, RC632_TMO_AUTH1); + ret = rc632_wait_idle_timer(h); + if (ret < 0) + return ret; + + ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®); + if (ret < 0) + return ret; + + if (reg & RC632_ERR_FLAG_KEY_ERR) + return -EINVAL; + + return 0; +} + static int rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno, u_int8_t block) @@ -2092,6 +2138,7 @@ const struct rfid_asic rc632 = { }, .mifare_classic = { .setkey = &rc632_mifare_set_key, + .setkey_ee = &rc632_mifare_set_key_ee, .auth = &rc632_mifare_auth, }, }, diff --git a/src/rfid_proto_mifare_classic.c b/src/rfid_proto_mifare_classic.c index 66e4287..a6e2e4f 100644 --- a/src/rfid_proto_mifare_classic.c +++ b/src/rfid_proto_mifare_classic.c @@ -187,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); diff --git a/src/rfid_reader_cm5121.c b/src/rfid_reader_cm5121.c index 2d1ce24..12bc358 100644 --- a/src/rfid_reader_cm5121.c +++ b/src/rfid_reader_cm5121.c @@ -275,6 +275,7 @@ const struct rfid_reader rfid_reader_cm5121 = { }, .mifare_classic = { .setkey = &_rdr_rc632_mifare_setkey, + .setkey_ee = &_rdr_rc632_mifare_setkey_ee, .auth = &_rdr_rc632_mifare_auth, }, }; diff --git a/src/rfid_reader_openpcd.c b/src/rfid_reader_openpcd.c index 2a3aca5..d81a7dd 100644 --- a/src/rfid_reader_openpcd.c +++ b/src/rfid_reader_openpcd.c @@ -485,6 +485,7 @@ const struct rfid_reader rfid_reader_openpcd = { }, .mifare_classic = { .setkey = &_rdr_rc632_mifare_setkey, + .setkey_ee = &_rdr_rc632_mifare_setkey_ee, .auth = &_rdr_rc632_mifare_auth, }, }; diff --git a/src/rfid_reader_rc632_common.c b/src/rfid_reader_rc632_common.c index 4a41f15..101c1f8 100644 --- a/src/rfid_reader_rc632_common.c +++ b/src/rfid_reader_rc632_common.c @@ -116,6 +116,12 @@ _rdr_rc632_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key) return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key); } +int +_rdr_rc632_mifare_setkey_ee(struct rfid_reader_handle *rh, unsigned int addr) +{ + return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey_ee(rh->ah, addr); +} + int _rdr_rc632_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, u_int32_t serno, u_int8_t block) diff --git a/src/rfid_reader_rc632_common.h b/src/rfid_reader_rc632_common.h index 38d195d..999dfa3 100644 --- a/src/rfid_reader_rc632_common.h +++ b/src/rfid_reader_rc632_common.h @@ -20,6 +20,7 @@ int _rdr_rc632_14443a_set_speed(struct rfid_reader_handle *rh, unsigned int tx, unsigned int speed); int _rdr_rc632_l2_init(struct rfid_reader_handle *rh, enum rfid_layer2_id l2); int _rdr_rc632_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key); +int _rdr_rc632_mifare_setkey_ee(struct rfid_reader_handle *rh, const unsigned int addr); int _rdr_rc632_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, u_int32_t serno, u_int8_t block); int _rdr_rc632_getopt(struct rfid_reader_handle *rh, int optname, diff --git a/src/rfid_reader_spidev.c b/src/rfid_reader_spidev.c index 13d1bcb..0eee241 100644 --- a/src/rfid_reader_spidev.c +++ b/src/rfid_reader_spidev.c @@ -294,6 +294,7 @@ struct rfid_reader rfid_reader_spidev = { }, .mifare_classic = { .setkey = &_rdr_rc632_mifare_setkey, + .setkey_ee = &_rdr_rc632_mifare_setkey_ee, .auth = &_rdr_rc632_mifare_auth, }, };