From b9e8c17d8c4f28938c7cb0a665a1f21232ebdf66 Mon Sep 17 00:00:00 2001 From: laforge Date: Sun, 23 Oct 2005 20:03:56 +0000 Subject: [PATCH] make "frametype" a parameter of transcieve functions git-svn-id: https://svn.gnumonks.org/trunk/librfid@1559 e0336214-984f-0b4b-a45f-81c69e1f0ede --- include/rfid/rfid.h | 7 + include/rfid/rfid_asic.h | 2 + include/rfid/rfid_asic_rc632.h | 6 +- include/rfid/rfid_layer2.h | 4 + include/rfid/rfid_protocol_mifare_classic.h | 11 +- include/rfid/rfid_reader.h | 7 + openct-escape.c | 49 ++++-- rfid_asic_rc632.c | 160 ++++++++++++++------ rfid_layer2.c | 5 +- rfid_layer2_iso14443a.c | 11 +- rfid_layer2_iso14443b.c | 14 +- rfid_proto_mifare_classic.c | 36 +++-- rfid_proto_mifare_ul.c | 10 +- rfid_proto_tcl.c | 9 +- rfid_reader.c | 5 +- rfid_reader_cm5121.c | 15 +- 16 files changed, 253 insertions(+), 98 deletions(-) diff --git a/include/rfid/rfid.h b/include/rfid/rfid.h index bebb42a..f9d36c9 100644 --- a/include/rfid/rfid.h +++ b/include/rfid/rfid.h @@ -3,6 +3,13 @@ #include +enum rfid_frametype { + RFID_14443A_FRAME_REGULAR, + RFID_14443B_FRAME_REGULAR, + RFID_MIFARE_FRAME, +}; + + #define DEBUGP(x, args ...) fprintf(stderr, "%s(%d):%s: " x, __FILE__, __LINE__, __FUNCTION__, ## args) #define DEBUGPC(x, args ...) fprintf(stderr, x, ## args) diff --git a/include/rfid/rfid_asic.h b/include/rfid/rfid_asic.h index e336d01..6511615 100644 --- a/include/rfid/rfid_asic.h +++ b/include/rfid/rfid_asic.h @@ -1,6 +1,8 @@ #ifndef _RFID_ASIC_H #define _RFID_ASIC_H +enum rfid_frametype; + #include struct rfid_asic_transport { diff --git a/include/rfid/rfid_asic_rc632.h b/include/rfid/rfid_asic_rc632.h index 7a984ff..a84f792 100644 --- a/include/rfid/rfid_asic_rc632.h +++ b/include/rfid/rfid_asic_rc632.h @@ -3,6 +3,8 @@ struct rfid_asic_transport_handle; +#include + struct rfid_asic_rc632_transport { struct { int (*reg_write)(struct rfid_asic_transport_handle *rath, @@ -33,6 +35,7 @@ struct rfid_asic_rc632 { int (*turn_on_rf)(struct rfid_asic_handle *h); int (*turn_off_rf)(struct rfid_asic_handle *h); int (*transcieve)(struct rfid_asic_handle *h, + enum rfid_frametype, const u_int32_t *tx_buf, unsigned int tx_len, u_int32_t *rx_buf, @@ -55,7 +58,8 @@ struct rfid_asic_rc632 { int (*init)(struct rfid_asic_handle *h); } iso15693; struct { - int (*setkey)(struct rfid_asic_handle *h, unsigned char *key); + int (*setkey)(struct rfid_asic_handle *h, + const unsigned char *key); 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/rfid/rfid_layer2.h b/include/rfid/rfid_layer2.h index c87578b..f6d9117 100644 --- a/include/rfid/rfid_layer2.h +++ b/include/rfid/rfid_layer2.h @@ -4,6 +4,8 @@ struct rfid_layer2_handle; struct rfid_reader_handle; +#include + #include #include #include @@ -17,6 +19,7 @@ struct rfid_layer2 { struct rfid_layer2_handle *(*init)(struct rfid_reader_handle *h); int (*open)(struct rfid_layer2_handle *h); int (*transcieve)(struct rfid_layer2_handle *h, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int tx_len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, @@ -50,6 +53,7 @@ struct rfid_layer2_handle *rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id); int rfid_layer2_open(struct rfid_layer2_handle *l2h); int rfid_layer2_transcieve(struct rfid_layer2_handle *l2h, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int tx_len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags); diff --git a/include/rfid/rfid_protocol_mifare_classic.h b/include/rfid/rfid_protocol_mifare_classic.h index 01066f4..64ef11e 100644 --- a/include/rfid/rfid_protocol_mifare_classic.h +++ b/include/rfid/rfid_protocol_mifare_classic.h @@ -5,7 +5,16 @@ extern struct rfid_protocol rfid_protocol_mfcl; #define RFID_CMD_MIFARE_AUTH1A 0x60 #define RFID_CMD_MIFARE_AUTH1B 0x61 -#define MIFARE_CLASSIC_KEY_DEFAULT "\xa0\xa1\xa2\xa3\xa4\xa5" +#define MIFARE_CL_CMD_WRITE16 0xA0 +#define MIFARE_CL_CMD_READ 0x30 + +#define MIFARE_CL_RESP_ACK 0x0a +#define MIFARE_CL_RESP_NAK 0x00 + +#define MIFARE_CL_PAGE_MAX 0xff + +#define MIFARE_CLASSIC_KEYA_DEFAULT "\xa0\xa1\xa2\xa3\xa4\xa5" +#define MIFARE_CLASSIC_KEYB_DEFAULT "\xb0\xb1\xb2\xb3\xb4\xb5" #endif diff --git a/include/rfid/rfid_reader.h b/include/rfid/rfid_reader.h index 0acd532..501119b 100644 --- a/include/rfid/rfid_reader.h +++ b/include/rfid/rfid_reader.h @@ -10,6 +10,7 @@ struct rfid_reader { char *name; unsigned int id; int (*transcieve)(struct rfid_reader_handle *h, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int tx_len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags); @@ -61,4 +62,10 @@ struct rfid_reader_handle { } priv; struct rfid_reader *reader; }; + + +extern struct rfid_reader_handle * +rfid_reader_open(void *data, unsigned int id); + +extern void rfid_reader_close(struct rfid_reader_handle *rh); #endif diff --git a/openct-escape.c b/openct-escape.c index b488610..628aded 100644 --- a/openct-escape.c +++ b/openct-escape.c @@ -237,12 +237,13 @@ iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len) return iso7816_read_binary(buf, len); } +/* mifare ultralight helpers */ int mifare_ulight_write(struct rfid_protocol_handle *ph) { unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 }; - return rfid_protocol_write(ph, 20, buf, 4); + return rfid_protocol_write(ph, 10, buf, 4); } int @@ -259,7 +260,7 @@ mifare_ulight_blank(struct rfid_protocol_handle *ph) return 0; } -int +static int mifare_ulight_read(struct rfid_protocol_handle *ph) { unsigned char buf[20]; @@ -277,6 +278,26 @@ mifare_ulight_read(struct rfid_protocol_handle *ph) return 0; } +/* mifare classic helpers */ +static int +mifare_classic_read(struct rfid_protocol_handle *ph) +{ + unsigned char buf[20]; + unsigned int len = sizeof(buf); + int ret; + int i; + + for (i = 0; i <= MIFARE_CL_PAGE_MAX; i++) { + ret = rfid_protocol_read(ph, i, buf, &len); + if (ret < 0) + return ret; + + printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4)); + } + return 0; +} + + int main(int argc, char **argv) { int rc; @@ -286,9 +307,9 @@ int main(int argc, char **argv) if (init() < 0) exit(1); - protocol = RFID_PROTOCOL_MIFARE_UL; - protocol = RFID_PROTOCOL_MIFARE_CLASSIC; -// protocol = RFID_PROTOCOL_TCL; + //protocol = RFID_PROTOCOL_MIFARE_UL; + //protocol = RFID_PROTOCOL_MIFARE_CLASSIC; + protocol = RFID_PROTOCOL_TCL; if (l3(protocol) < 0) exit(1); @@ -298,8 +319,8 @@ int main(int argc, char **argv) /* we've established T=CL at this point */ select_mf(); - rc632_register_dump(rh->ah, buf); - select_mf(); + //rc632_register_dump(rh->ah, buf); + //select_mf(); iso7816_select_application(); iso7816_select_ef(0x011e); @@ -312,19 +333,25 @@ int main(int argc, char **argv) case RFID_PROTOCOL_MIFARE_UL: mifare_ulight_read(ph); #if 0 - //mifare_ulight_blank(ph); - mifare_ulight_write(ph); + mifare_ulight_blank(ph); + //mifare_ulight_write(ph); mifare_ulight_read(ph); #endif break; case RFID_PROTOCOL_MIFARE_CLASSIC: - mfcl_set_key(ph, MIFARE_CLASSIC_KEY_DEFAULT); - rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, 0); + rc = mfcl_set_key(ph, MIFARE_CLASSIC_KEYB_DEFAULT); + if (rc < 0) { + printf("key format error\n"); + exit(1); + } + //mfcl_set_key(ph, "xasdfr"); + rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1B, 10); if (rc < 0) { printf("mifare auth error\n"); exit(1); } else printf("mifare authe succeeded!\n"); + mifare_classic_read(ph); break; } diff --git a/rfid_asic_rc632.c b/rfid_asic_rc632.c index 36cca71..eeb6d89 100644 --- a/rfid_asic_rc632.c +++ b/rfid_asic_rc632.c @@ -43,7 +43,7 @@ struct rfid_asic rc632; /* Register and FIFO Access functions */ -int +static int rc632_reg_write(struct rfid_asic_handle *handle, u_int8_t reg, u_int8_t val) @@ -51,7 +51,7 @@ rc632_reg_write(struct rfid_asic_handle *handle, return handle->rath->rat->priv.rc632.fn.reg_write(handle->rath, reg, val); } -int +static int rc632_reg_read(struct rfid_asic_handle *handle, u_int8_t reg, u_int8_t *val) @@ -59,7 +59,7 @@ rc632_reg_read(struct rfid_asic_handle *handle, return handle->rath->rat->priv.rc632.fn.reg_read(handle->rath, reg, val); } -int +static int rc632_fifo_write(struct rfid_asic_handle *handle, u_int8_t len, const u_int8_t *buf, @@ -69,7 +69,7 @@ rc632_fifo_write(struct rfid_asic_handle *handle, len, buf, flags); } -int +static int rc632_fifo_read(struct rfid_asic_handle *handle, u_int8_t len, u_int8_t *buf) @@ -78,7 +78,7 @@ rc632_fifo_read(struct rfid_asic_handle *handle, } -int +static int rc632_set_bits(struct rfid_asic_handle *handle, u_int8_t reg, u_int8_t val) @@ -97,7 +97,7 @@ rc632_set_bits(struct rfid_asic_handle *handle, return rc632_reg_write(handle, reg, (tmp|val)&0xff); } -int +static int rc632_clear_bits(struct rfid_asic_handle *handle, u_int8_t reg, u_int8_t val) @@ -118,23 +118,21 @@ rc632_clear_bits(struct rfid_asic_handle *handle, return rc632_reg_write(handle, reg, (tmp & ~val)&0xff); } - - -int +static int rc632_turn_on_rf(struct rfid_asic_handle *handle) { ENTER(); return rc632_set_bits(handle, RC632_REG_TX_CONTROL, 0x03); } -int +static int rc632_turn_off_rf(struct rfid_asic_handle *handle) { ENTER(); return rc632_clear_bits(handle, RC632_REG_TX_CONTROL, 0x03); } -int +static int rc632_power_up(struct rfid_asic_handle *handle) { ENTER(); @@ -142,7 +140,7 @@ rc632_power_up(struct rfid_asic_handle *handle) RC632_CONTROL_POWERDOWN); } -int +static int rc632_power_down(struct rfid_asic_handle *handle) { return rc632_set_bits(handle, RC632_REG_CONTROL, @@ -151,7 +149,7 @@ rc632_power_down(struct rfid_asic_handle *handle) /* Stupid RC623 implementations don't evaluate interrupts but poll the * command register for "status idle" */ -int +static int rc632_wait_idle(struct rfid_asic_handle *handle, u_int64_t timeout) { u_int8_t cmd = 0xff; @@ -182,7 +180,7 @@ rc632_wait_idle(struct rfid_asic_handle *handle, u_int64_t timeout) return 0; } -int +static int rc632_transmit(struct rfid_asic_handle *handle, const u_int8_t *buf, u_int8_t len, @@ -208,7 +206,7 @@ tcl_toggle_pcb(struct rfid_asic_handle *handle) return 0; } -int +static int rc632_transcieve(struct rfid_asic_handle *handle, const u_int8_t *tx_buf, u_int8_t tx_len, @@ -252,7 +250,7 @@ rc632_transcieve(struct rfid_asic_handle *handle, return rc632_fifo_read(handle, *rx_len, rx_buf); } -int +static int rc632_read_eeprom(struct rfid_asic_handle *handle) { u_int8_t recvbuf[60]; @@ -281,7 +279,7 @@ rc632_read_eeprom(struct rfid_asic_handle *handle) return ret; } -int +static int rc632_calc_crc16_from(struct rfid_asic_handle *handle) { u_int8_t sndbuf[2] = { 0x01, 0x02 }; @@ -618,17 +616,29 @@ rc632_iso14443a_transcieve_sf(struct rfid_asic_handle *handle, /* transcieve regular frame */ static int -rc632_iso14443a_transcieve(struct rfid_asic_handle *handle, +rc632_iso14443ab_transcieve(struct rfid_asic_handle *handle, + unsigned int frametype, const u_int8_t *tx_buf, unsigned int tx_len, u_int8_t *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags) { int ret; u_int8_t rxl = *rx_len & 0xff; + u_int8_t channel_red; DEBUGP("entered\n"); memset(rx_buf, 0, *rx_len); + switch (frametype) { + case RFID_14443A_FRAME_REGULAR: + case RFID_14443B_FRAME_REGULAR: + channel_red = RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE + |RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD; + break; + case RFID_MIFARE_FRAME: + channel_red = RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD; + break; + } #if 0 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY, (RC632_CR_PARITY_ENABLE | @@ -636,8 +646,8 @@ rc632_iso14443a_transcieve(struct rfid_asic_handle *handle, RC632_CR_TX_CRC_ENABLE | RC632_CR_RX_CRC_ENABLE)); #endif - ret = rc632_set_bits(handle, RC632_REG_CHANNEL_REDUNDANCY, - RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE); + ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY, + channel_red); if (ret < 0) return ret; @@ -1117,6 +1127,7 @@ static int rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key) { u_int8_t coded_key[RFID_MIFARE_KEY_CODED_LEN]; + u_int8_t reg; int ret; ret = rc632_mifare_transform_key(key, coded_key); @@ -1135,6 +1146,13 @@ rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key) 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; } @@ -1152,10 +1170,20 @@ rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno, /* Initialize acmd */ acmd.block_address = block & 0xff; acmd.auth_cmd = cmd; + //acmd.serno = htonl(serno); acmd.serno = serno; + ret = rc632_clear_bits(h, RC632_REG_CONTROL, + RC632_CONTROL_CRYPTO1_ON); + + /* Clear Rx CRC */ + ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY, + RC632_CR_RX_CRC_ENABLE); + if (ret < 0) + return ret; + /* Send Authent1 Command */ - ret = rc632_fifo_write(h, sizeof(acmd), &acmd, 0x03); + ret = rc632_fifo_write(h, sizeof(acmd), (unsigned char *)&acmd, 0x03); if (ret < 0) return ret; @@ -1168,9 +1196,15 @@ rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno, if (ret < 0) return ret; - /* Clear Rx/Tx CRC */ + ret = rc632_reg_read(h, RC632_REG_SECONDARY_STATUS, ®); + if (ret < 0) + return ret; + if (reg & 0x07) + return -EIO; + + /* Clear Tx CRC */ ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY, - RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE); + RC632_CR_TX_CRC_ENABLE); if (ret < 0) return ret; @@ -1179,10 +1213,14 @@ rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno, if (ret < 0) return ret; +#if 0 /* Wait until transmitter is idle */ ret = rc632_wait_idle(h, RC632_TMO_AUTH1); if (ret < 0) return ret; +#else + sleep(1); +#endif /* Check whether authentication was successful */ ret = rc632_reg_read(h, RC632_REG_CONTROL, ®); @@ -1196,31 +1234,65 @@ rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno, } +/* transcieve regular frame */ +static int +rc632_mifare_transcieve(struct rfid_asic_handle *handle, + const u_int8_t *tx_buf, unsigned int tx_len, + u_int8_t *rx_buf, unsigned int *rx_len, + u_int64_t timeout, unsigned int flags) +{ + int ret; + u_int8_t rxl = *rx_len & 0xff; + + DEBUGP("entered\n"); + memset(rx_buf, 0, *rx_len); + +#if 0 + ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY, + (RC632_CR_PARITY_ENABLE | + RC632_CR_PARITY_ODD | + RC632_CR_TX_CRC_ENABLE | + RC632_CR_RX_CRC_ENABLE)); +#endif + ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY, + RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE); + if (ret < 0) + return ret; + + ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0); + *rx_len = rxl; + if (ret < 0) + return ret; + + + return 0; +} + struct rfid_asic rc632 = { .name = "Philips CL RC632", .fc = ISO14443_FREQ_CARRIER, .priv.rc632 = { - .fn.power_up = &rc632_power_up, - .fn.power_down = &rc632_power_down, - .fn.turn_on_rf = &rc632_turn_on_rf, - .fn.turn_off_rf = &rc632_turn_off_rf, - .fn.transcieve = &rc632_iso14443a_transcieve, - .fn.iso14443a = { - .init = &rc632_iso14443a_init, - .transcieve_sf = &rc632_iso14443a_transcieve_sf, - .transcieve_acf = &rc632_iso14443a_transcieve_acf, - }, - .fn.iso14443b = { - .init = &rc632_iso14443b_init, - }, - .fn.iso15693 = { - .init = &rc632_iso15693_init, - }, - .fn.mifare_classic = { - .setkey = &rc632_mifare_set_key, - .auth = &rc632_mifare_auth, + .fn = { + .power_up = &rc632_power_up, + .power_down = &rc632_power_down, + .turn_on_rf = &rc632_turn_on_rf, + .turn_off_rf = &rc632_turn_off_rf, + .transcieve = &rc632_iso14443ab_transcieve, + .iso14443a = { + .init = &rc632_iso14443a_init, + .transcieve_sf = &rc632_iso14443a_transcieve_sf, + .transcieve_acf = &rc632_iso14443a_transcieve_acf, + }, + .iso14443b = { + .init = &rc632_iso14443b_init, + }, + .iso15693 = { + .init = &rc632_iso15693_init, + }, + .mifare_classic = { + .setkey = &rc632_mifare_set_key, + .auth = &rc632_mifare_auth, + }, }, }, }; - - diff --git a/rfid_layer2.c b/rfid_layer2.c index ef59bad..c1ab6a6 100644 --- a/rfid_layer2.c +++ b/rfid_layer2.c @@ -46,12 +46,13 @@ rfid_layer2_open(struct rfid_layer2_handle *ph) int rfid_layer2_transcieve(struct rfid_layer2_handle *ph, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags) { - return ph->l2->fn.transcieve(ph, tx_buf, len, rx_buf, rx_len, - timeout, flags); + return ph->l2->fn.transcieve(ph, frametype, tx_buf, len, rx_buf, + rx_len, timeout, flags); } int rfid_layer2_fini(struct rfid_layer2_handle *ph) diff --git a/rfid_layer2_iso14443a.c b/rfid_layer2_iso14443a.c index 7d7c377..d93f917 100644 --- a/rfid_layer2_iso14443a.c +++ b/rfid_layer2_iso14443a.c @@ -55,12 +55,13 @@ iso14443a_transcieve_acf(struct rfid_layer2_handle *handle, /* Transmit a regular frame */ static int iso14443a_transcieve(struct rfid_layer2_handle *handle, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int tx_len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags) { - return handle->rh->reader->transcieve(handle->rh, tx_buf, tx_len, - rx_buf, rx_len, timeout, flags); + return handle->rh->reader->transcieve(handle->rh, frametype, tx_buf, + tx_len, rx_buf, rx_len, timeout, flags); } static int @@ -155,7 +156,8 @@ cascade: } iso14443a_code_nvb_bits(&acf.nvb, 7*8); - ret = iso14443a_transcieve(handle, (unsigned char *)&acf, 7, + ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR, + (unsigned char *)&acf, 7, (unsigned char *) &sak, &rx_len, TIMEOUT, 0); if (ret < 0) @@ -238,7 +240,8 @@ iso14443a_hlta(struct rfid_layer2_handle *handle) unsigned char rx_buf[10]; unsigned int rx_len = sizeof(rx_buf); - ret = iso14443a_transcieve(handle, tx_buf, sizeof(tx_buf), + ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR, + tx_buf, sizeof(tx_buf), rx_buf, &rx_len, 1000 /* 1ms */, 0); if (ret < 0) { /* "error" case: we don't get somethng back from the card */ diff --git a/rfid_layer2_iso14443b.c b/rfid_layer2_iso14443b.c index c5ff592..f874071 100644 --- a/rfid_layer2_iso14443b.c +++ b/rfid_layer2_iso14443b.c @@ -114,7 +114,8 @@ send_reqb(struct rfid_layer2_handle *h, unsigned char afi, if (is_wup) reqb[2] |= 0x08; - ret = h->rh->reader->transcieve(h->rh, reqb, sizeof(reqb), + ret = h->rh->reader->transcieve(h->rh, RFID_14443B_FRAME_REGULAR, + reqb, sizeof(reqb), (unsigned char *)&atqb, &atqb_len, ATQB_TIMEOUT, 0); h->priv.iso14443b.state = ISO14443B_STATE_REQB_SENT; @@ -199,7 +200,8 @@ transcieve_attrib(struct rfid_layer2_handle *h, const unsigned char *inf, attrib->param3.protocol_type = 0x1; *rx_len = *rx_len + 1; - ret = h->rh->reader->transcieve(h->rh, (unsigned char *) attrib, + ret = h->rh->reader->transcieve(h->rh, RFID_14443B_FRAME_REGULAR, + (unsigned char *) attrib, sizeof(*attrib)+inf_len, rx_buf, rx_len, h->priv.iso14443b.fwt, 0); @@ -242,7 +244,8 @@ iso14443b_hltb(struct rfid_layer2_handle *h) hltb[0] = 0x50; memcpy(hltb+1, h->uid, 4); - ret = h->rh->reader->transcieve(h->rh, hltb, 5, + ret = h->rh->reader->transcieve(h->rh, RFID_14443B_FRAME_REGULAR, + hltb, 5, hltb_resp, &hltb_len, h->priv.iso14443b.fwt, 0); h->priv.iso14443b.state = ISO14443B_STATE_HLTB_SENT; @@ -317,12 +320,14 @@ iso14443b_fini(struct rfid_layer2_handle *handle) static int iso14443b_transcieve(struct rfid_layer2_handle *handle, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int tx_len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags) { DEBUGP("transcieving %u bytes, expecting max %u\n", tx_len, *rx_len); - return handle->rh->reader->transcieve(handle->rh, tx_buf, tx_len, + return handle->rh->reader->transcieve(handle->rh, frametype, + tx_buf, tx_len, rx_buf, rx_len, timeout, flags); } @@ -337,4 +342,3 @@ struct rfid_layer2 rfid_layer2_iso14443b = { .fini = &iso14443b_fini, }, }; - diff --git a/rfid_proto_mifare_classic.c b/rfid_proto_mifare_classic.c index 474f554..10de288 100644 --- a/rfid_proto_mifare_classic.c +++ b/rfid_proto_mifare_classic.c @@ -28,9 +28,8 @@ #include #include #include -//#include +#include -//#include #include #include "rfid_iso14443_common.h" @@ -40,8 +39,8 @@ #define MIFARE_UL_CMD_READ 0x30 /* FIXME */ -#define MIFARE_UL_READ_FWT 100 -#define MIFARE_UL_WRITE_FWT 100 +#define MIFARE_CL_READ_FWT 100 +#define MIFARE_CL_WRITE_FWT 100 static int mfcl_read(struct rfid_protocol_handle *ph, unsigned int page, @@ -52,14 +51,15 @@ mfcl_read(struct rfid_protocol_handle *ph, unsigned int page, unsigned char tx[2]; int ret; - if (page > 7) + if (page > MIFARE_CL_PAGE_MAX) return -EINVAL; - tx[0] = MIFARE_UL_CMD_READ; + tx[0] = MIFARE_CL_CMD_READ; tx[1] = page & 0xff; - ret = ph->l2h->l2->fn.transcieve(ph->l2h, tx, sizeof(tx), rx_buf, - &real_rx_len, MIFARE_UL_READ_FWT, 0); + ret = ph->l2h->l2->fn.transcieve(ph->l2h, RFID_MIFARE_FRAME, tx, + sizeof(tx), rx_buf, &real_rx_len, + MIFARE_CL_READ_FWT, 0); if (ret < 0) return ret; @@ -77,24 +77,28 @@ mfcl_write(struct rfid_protocol_handle *ph, unsigned int page, unsigned char *tx_data, unsigned int tx_len) { unsigned int i; - unsigned char tx[6]; + unsigned char tx[18]; unsigned char rx[1]; unsigned int rx_len; int ret; - if (tx_len != 4 || page > 7) + if (tx_len != 16 || page > MIFARE_CL_PAGE_MAX) return -EINVAL; - tx[0] = MIFARE_UL_CMD_WRITE; + tx[0] = MIFARE_CL_CMD_WRITE16; tx[1] = page & 0xff; - for (i = 0; i < 4; i++) - tx[2+i] = tx_data[i]; + memcpy(tx+2, tx_data, 16); - ret = ph->l2h->l2->fn.transcieve(ph->l2h, tx, sizeof(tx), rx, - &rx_len, MIFARE_UL_WRITE_FWT, 0); + ret = ph->l2h->l2->fn.transcieve(ph->l2h, RFID_MIFARE_FRAME, tx, + sizeof(tx), rx, &rx_len, + MIFARE_CL_WRITE_FWT, 0); - /* FIXME:look at RX, check for ACK/NAK */ + if (ret < 0) + return ret; + + if (rx[0] != MIFARE_UL_RESP_ACK) + return -EIO; return ret; } diff --git a/rfid_proto_mifare_ul.c b/rfid_proto_mifare_ul.c index adac9cc..9e5363f 100644 --- a/rfid_proto_mifare_ul.c +++ b/rfid_proto_mifare_ul.c @@ -52,7 +52,8 @@ mful_read(struct rfid_protocol_handle *ph, unsigned int page, tx[0] = MIFARE_UL_CMD_READ; tx[1] = page & 0xff; - ret = ph->l2h->l2->fn.transcieve(ph->l2h, tx, sizeof(tx), rx_buf, + ret = ph->l2h->l2->fn.transcieve(ph->l2h, RFID_14443A_FRAME_REGULAR, + tx, sizeof(tx), rx_buf, &real_rx_len, MIFARE_UL_READ_FWT, 0); if (ret < 0) @@ -76,10 +77,8 @@ mful_write(struct rfid_protocol_handle *ph, unsigned int page, unsigned int rx_len = sizeof(rx); int ret; -#if 0 if (tx_len != 4 || page > MIFARE_UL_PAGE_MAX) return -EINVAL; -#endif tx[0] = MIFARE_UL_CMD_WRITE; tx[1] = page & 0xff; @@ -87,8 +86,9 @@ mful_write(struct rfid_protocol_handle *ph, unsigned int page, for (i = 0; i < 4; i++) tx[2+i] = tx_data[i]; - ret = ph->l2h->l2->fn.transcieve(ph->l2h, tx, sizeof(tx), rx, - &rx_len, MIFARE_UL_WRITE_FWT, 0); + ret = ph->l2h->l2->fn.transcieve(ph->l2h, RFID_14443A_FRAME_REGULAR, + tx, sizeof(tx), rx, &rx_len, + MIFARE_UL_WRITE_FWT, 0); if (ret < 0) return ret; diff --git a/rfid_proto_tcl.c b/rfid_proto_tcl.c index 248eea5..8590fd4 100644 --- a/rfid_proto_tcl.c +++ b/rfid_proto_tcl.c @@ -165,7 +165,8 @@ tcl_request_ats(struct rfid_protocol_handle *h) rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0); /* transcieve (with CRC) */ - ret = h->l2h->l2->fn.transcieve(h->l2h, rats, 2, h->priv.tcl.ats, + ret = h->l2h->l2->fn.transcieve(h->l2h, RFID_14443A_FRAME_REGULAR, + rats, 2, h->priv.tcl.ats, &h->priv.tcl.ats_len, activation_fwt(h), TCL_TRANSP_F_TX_CRC); if (ret < 0) { @@ -432,7 +433,8 @@ tcl_deselect(struct rfid_protocol_handle *h) if (ret < 0) return ret; - ret = h->l2h->l2->fn.transcieve(h->l2h, frame, prlg_len, rx, + ret = h->l2h->l2->fn.transcieve(h->l2h, RFID_14443A_FRAME_REGULAR, + frame, prlg_len, rx, &rx_len, deactivation_fwt(h), TCL_TRANSP_F_TX_CRC); if (ret < 0) { @@ -494,7 +496,8 @@ tcl_transcieve(struct rfid_protocol_handle *h, _timeout = th->fwt; do_tx: - ret = h->l2h->l2->fn.transcieve(h->l2h, _tx, _tx_len, + ret = h->l2h->l2->fn.transcieve(h->l2h, RFID_14443A_FRAME_REGULAR, + _tx, _tx_len, rx_buf, rx_len, _timeout, 0); DEBUGP("l2 transcieve finished\n"); if (ret < 0) diff --git a/rfid_reader.c b/rfid_reader.c index 2a34db8..9e46a29 100644 --- a/rfid_reader.c +++ b/rfid_reader.c @@ -40,12 +40,13 @@ rfid_reader_open(void *data, unsigned int id) int rfid_reader_transcieve(struct rfid_reader_handle *rh, + enum rfid_frametype frametype, const unsigned char *tx_buf, unsigned int len, unsigned char *rx_buf, unsigned int *rx_len, u_int64_t timeout, unsigned int flags) { - return rh->reader->transcieve(rh, tx_buf, len, rx_buf, rx_len, - timeout, flags); + return rh->reader->transcieve(rh, frametype, tx_buf, len, rx_buf, + rx_len, timeout, flags); } void diff --git a/rfid_reader_cm5121.c b/rfid_reader_cm5121.c index e1fc859..0a72836 100644 --- a/rfid_reader_cm5121.c +++ b/rfid_reader_cm5121.c @@ -180,14 +180,14 @@ static int TestFIFO(struct rc632_handle *handle) #endif static int cm5121_transcieve(struct rfid_reader_handle *rh, + enum rfid_frametype frametype, const unsigned char *tx_data, unsigned int tx_len, unsigned char *rx_data, unsigned int *rx_len, u_int64_t timeout, unsigned int flags) { - return rh->ah->asic->priv.rc632.fn.transcieve(rh->ah, tx_data, - tx_len, rx_data, - rx_len, timeout, - flags); + return rh->ah->asic->priv.rc632.fn.transcieve(rh->ah, frametype, + tx_data, tx_len, rx_data, + rx_len, timeout, flags); } static int cm5121_transcieve_sf(struct rfid_reader_handle *rh, @@ -225,6 +225,12 @@ cm5121_15693_init(struct rfid_reader_handle *rh) return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah); } +static int +cm5121_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); +} + static int cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, u_int32_t serno, u_int8_t block) @@ -321,6 +327,7 @@ struct rfid_reader rfid_reader_cm5121 = { .init = &cm5121_14443b_init, }, .mifare_classic = { + .setkey = &cm5121_mifare_setkey, .auth = &cm5121_mifare_auth, }, }; -- 2.20.1