2 /* Mifare Ultralight 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_ul.h>
33 #include "rfid_iso14443_common.h"
37 #define MIFARE_UL_READ_FWT 100
38 #define MIFARE_UL_WRITE_FWT 100
41 mful_read(struct rfid_protocol_handle *ph, unsigned int page,
42 unsigned char *rx_data, unsigned int *rx_len)
44 unsigned char rx_buf[16];
45 unsigned int real_rx_len = sizeof(rx_buf);
49 if (page > MIFARE_UL_PAGE_MAX)
52 tx[0] = MIFARE_UL_CMD_READ;
55 ret = rfid_layer2_transceive(ph->l2h, RFID_14443A_FRAME_REGULAR,
56 tx, sizeof(tx), rx_buf,
57 &real_rx_len, MIFARE_UL_READ_FWT, 0);
62 if (real_rx_len < *rx_len)
63 *rx_len = real_rx_len;
65 memcpy(rx_data, rx_buf, *rx_len);
71 mful_write(struct rfid_protocol_handle *ph, unsigned int page,
72 unsigned char *tx_data, unsigned int tx_len)
77 unsigned int rx_len = sizeof(rx);
80 if (tx_len != 4 || page > MIFARE_UL_PAGE_MAX)
83 tx[0] = MIFARE_UL_CMD_WRITE;
86 for (i = 0; i < 4; i++)
89 ret = rfid_layer2_transceive(ph->l2h, RFID_14443A_FRAME_REGULAR,
90 tx, sizeof(tx), rx, &rx_len,
91 MIFARE_UL_WRITE_FWT, 0);
96 if (rx[0] != MIFARE_UL_RESP_ACK)
103 mful_transceive(struct rfid_protocol_handle *ph,
104 const unsigned char *tx_data, unsigned int tx_len,
105 unsigned char *rx_data, unsigned int *rx_len,
106 unsigned int timeout, unsigned int flags)
112 mful_getopt(struct rfid_protocol_handle *ph, int optname, void *optval,
113 unsigned int *optlen)
117 unsigned int *size = optval;
120 case RFID_OPT_PROTO_SIZE:
130 static struct rfid_protocol_handle *
131 mful_init(struct rfid_layer2_handle *l2h)
133 struct rfid_protocol_handle *ph;
135 unsigned int atqa_len = sizeof(atqa);
137 if (l2h->l2->id != RFID_LAYER2_ISO14443A)
140 /* According to "Type Identification Procedure Rev. 1.3" */
141 rfid_layer2_getopt(l2h, RFID_OPT_14443A_ATQA,
146 /* according to "Functional Specification Rev. 3.0 */
147 if (l2h->uid_len != 7)
150 ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
154 static int mful_fini(struct rfid_protocol_handle *ph)
156 free_protocol_handle(ph);
160 const struct rfid_protocol rfid_protocol_mful = {
161 .id = RFID_PROTOCOL_MIFARE_UL,
162 .name = "Mifare Ultralight",
166 .write = &mful_write,
168 .getopt = &mful_getopt,
172 /* Functions below are not (yet? covered in the generic librfid api */
175 /* lock a certain page */
176 int rfid_mful_lock_page(struct rfid_protocol_handle *ph, unsigned int page)
178 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
180 if (ph->proto != &rfid_protocol_mful)
183 if (page < 3 || page > 15)
187 buf[2] = (1 << page);
189 buf[3] = (1 << (page - 8));
191 return mful_write(ph, MIFARE_UL_PAGE_LOCK, buf, sizeof(buf));
194 /* convenience wrapper to lock the otp page */
195 int rfid_mful_lock_otp(struct rfid_protocol_handle *ph)
197 return rfid_mful_lock_page(ph, MIFARE_UL_PAGE_OTP);