use autoconf/automake
[librfid] / src / rfid_proto_mifare_classic.c
1
2 /* Mifare Classic implementation, PCD side.
3  *
4  * (C) 2005 by Harald Welte <laforge@gnumonks.org>
5  *
6  */
7
8 /*
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
12  *
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.
17  *
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
21  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include <rfid/rfid.h>
29 #include <rfid/rfid_protocol.h>
30 #include <rfid/rfid_layer2.h>
31 #include <rfid/rfid_protocol_mifare_classic.h>
32
33 #include <rfid/rfid_reader.h>
34
35 #include "rfid_iso14443_common.h"
36
37
38 #define MIFARE_UL_CMD_WRITE     0xA2
39 #define MIFARE_UL_CMD_READ      0x30
40
41 /* FIXME */
42 #define MIFARE_CL_READ_FWT      100
43 #define MIFARE_CL_WRITE_FWT     100
44
45 static int
46 mfcl_read(struct rfid_protocol_handle *ph, unsigned int page,
47           unsigned char *rx_data, unsigned int *rx_len)
48 {
49         unsigned char rx_buf[16];
50         unsigned int real_rx_len = sizeof(rx_buf);
51         unsigned char tx[2];
52         int ret;
53
54         if (page > MIFARE_CL_PAGE_MAX)
55                 return -EINVAL;
56
57         tx[0] = MIFARE_CL_CMD_READ;
58         tx[1] = page & 0xff;
59
60         ret = ph->l2h->l2->fn.transcieve(ph->l2h, RFID_MIFARE_FRAME, tx,
61                                          sizeof(tx), rx_buf, &real_rx_len,
62                                          MIFARE_CL_READ_FWT, 0);
63
64         if (ret < 0)
65                 return ret;
66
67         if (real_rx_len < *rx_len)
68                 *rx_len = real_rx_len;
69
70         memcpy(rx_data, rx_buf, *rx_len);
71
72         return ret;
73 }
74
75 static int
76 mfcl_write(struct rfid_protocol_handle *ph, unsigned int page,
77            unsigned char *tx_data, unsigned int tx_len)
78 {
79         unsigned int i;
80         unsigned char tx[18];
81         unsigned char rx[1];
82         unsigned int rx_len;
83         int ret;
84
85         if (tx_len != 16 || page > MIFARE_CL_PAGE_MAX)
86                 return -EINVAL;
87
88         tx[0] = MIFARE_CL_CMD_WRITE16;
89         tx[1] = page & 0xff;
90
91         memcpy(tx+2, tx_data, 16);
92
93         ret = ph->l2h->l2->fn.transcieve(ph->l2h, RFID_MIFARE_FRAME, tx,
94                                          sizeof(tx), rx, &rx_len, 
95                                          MIFARE_CL_WRITE_FWT, 0);
96                                         
97         if (ret < 0)
98                 return ret;
99
100         if (rx[0] != MIFARE_UL_RESP_ACK)
101                 return -EIO;
102
103         return ret;
104 }
105
106 static struct rfid_protocol_handle *
107 mfcl_init(struct rfid_layer2_handle *l2h)
108 {
109         struct rfid_protocol_handle *ph;
110         ph = malloc(sizeof(struct rfid_protocol_handle));
111         return ph;
112 }
113
114 static int mfcl_fini(struct rfid_protocol_handle *ph)
115 {
116         free(ph);
117         return 0;
118 }
119
120 struct rfid_protocol rfid_protocol_mfcl = {
121         .id     = RFID_PROTOCOL_MIFARE_CLASSIC,
122         .name   = "Mifare Classic",
123         .fn     = {
124                 .init           = &mfcl_init,
125                 .read           = &mfcl_read,
126                 .write          = &mfcl_write,
127                 .fini           = &mfcl_fini,
128         },
129 };
130
131 int mfcl_set_key(struct rfid_protocol_handle *ph, unsigned char *key)
132 {
133         if (!ph->l2h->rh->reader->mifare_classic.setkey)
134                 return -ENODEV;
135
136         return ph->l2h->rh->reader->mifare_classic.setkey(ph->l2h->rh, key);
137 }
138
139 int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
140 {
141         u_int32_t serno = *((u_int32_t *)ph->l2h->uid);
142
143         if (!ph->l2h->rh->reader->mifare_classic.auth)
144                 return -ENODEV;
145
146         return ph->l2h->rh->reader->mifare_classic.auth(ph->l2h->rh, cmd,
147                                                        serno, block);
148 }