2 /* Mifare Classic implementation, PCD side.
4 * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <librfid/rfid.h>
29 #include <librfid/rfid_protocol.h>
30 #include <librfid/rfid_layer2.h>
31 #include <librfid/rfid_protocol_mifare_classic.h>
33 #include <librfid/rfid_reader.h>
35 #include "rfid_iso14443_common.h"
38 #define MIFARE_UL_CMD_WRITE 0xA2
39 #define MIFARE_UL_CMD_READ 0x30
42 #define MIFARE_CL_READ_FWT 100
43 #define MIFARE_CL_WRITE_FWT 100
46 mfcl_read(struct rfid_protocol_handle *ph, unsigned int page,
47 unsigned char *rx_data, unsigned int *rx_len)
49 unsigned char rx_buf[16];
50 unsigned int real_rx_len = sizeof(rx_buf);
54 if (page > MIFARE_CL_PAGE_MAX)
57 tx[0] = MIFARE_CL_CMD_READ;
60 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
61 sizeof(tx), rx_buf, &real_rx_len,
62 MIFARE_CL_READ_FWT, 0);
67 if (real_rx_len == 1 && *rx_buf == 0x04)
70 if (real_rx_len < *rx_len)
71 *rx_len = real_rx_len;
73 memcpy(rx_data, rx_buf, *rx_len);
79 mfcl_write(struct rfid_protocol_handle *ph, unsigned int page,
80 unsigned char *tx_data, unsigned int tx_len)
88 if (page > MIFARE_CL_PAGE_MAX)
91 if (tx_len != 16 && tx_len != 4)
95 tx[0] = MIFARE_CL_CMD_WRITE16;
98 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
100 MIFARE_CL_WRITE_FWT, 0);
104 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx_data,
106 MIFARE_CL_WRITE_FWT, 0);
110 if (rx[0] != MIFARE_UL_RESP_ACK)
113 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
114 sizeof(tx), rx, &rx_len,
115 MIFARE_CL_WRITE_FWT, 0);
119 if (rx[0] != MIFARE_UL_RESP_ACK)
122 } else if (tx_len == 4) {
124 tx[0] = MIFARE_CL_CMD_WRITE4;
127 memcpy(tx+2, tx_data, 4);
129 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
131 MIFARE_CL_WRITE_FWT, 0);
135 if (rx[0] != MIFARE_UL_RESP_ACK)
144 mfcl_getopt(struct rfid_protocol_handle *ph, int optname, void *optval,
145 unsigned int *optlen)
149 unsigned int atqa_size = sizeof(atqa);
150 unsigned int *size = optval;
153 case RFID_OPT_PROTO_SIZE:
154 if (*optlen < sizeof(*size))
156 *optlen = sizeof(*size);
158 rfid_layer2_getopt(ph->l2h, RFID_OPT_14443A_ATQA,
159 (void *) &atqa, &atqa_size);
162 else if (atqa == 0x0002)
172 static struct rfid_protocol_handle *
173 mfcl_init(struct rfid_layer2_handle *l2h)
175 struct rfid_protocol_handle *ph;
177 if (l2h->l2->id != RFID_LAYER2_ISO14443A)
180 if (l2h->uid_len != 4)
183 ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
187 static int mfcl_fini(struct rfid_protocol_handle *ph)
189 free_protocol_handle(ph);
193 const struct rfid_protocol rfid_protocol_mfcl = {
194 .id = RFID_PROTOCOL_MIFARE_CLASSIC,
195 .name = "Mifare Classic",
199 .write = &mfcl_write,
201 .getopt = &mfcl_getopt,
205 int mfcl_set_key(struct rfid_protocol_handle *ph, unsigned char *key)
207 if (!ph->l2h->rh->reader->mifare_classic.setkey)
210 return ph->l2h->rh->reader->mifare_classic.setkey(ph->l2h->rh, key);
213 int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
215 u_int32_t serno = *((u_int32_t *)ph->l2h->uid);
217 if (!ph->l2h->rh->reader->mifare_classic.auth)
220 return ph->l2h->rh->reader->mifare_classic.auth(ph->l2h->rh, cmd,