fix mifare frame crc
[librfid] / rfid_layer2_iso14443a.c
1 /* ISO 14443-3 A anticollision implementation
2  *
3  * (C) 2005 by Harald Welte <laforge@gnumonks.org>
4  *
5  */
6
7 /*
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 
10  *  as published by the Free Software Foundation
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #include <rfid/rfid.h>
27 #include <rfid/rfid_layer2.h>
28 #include <rfid/rfid_reader.h>
29 #include <rfid/rfid_layer2_iso14443a.h>
30
31 #define TIMEOUT 1236
32
33 /* Transcieve a 7-bit short frame */
34 static int
35 iso14443a_transcieve_sf(struct rfid_layer2_handle *handle,
36                          unsigned char cmd,
37                          struct iso14443a_atqa *atqa)
38 {
39         struct rfid_reader *rdr = handle->rh->reader;
40
41         return rdr->iso14443a.transcieve_sf(handle->rh, cmd, atqa);
42 }
43
44 /* Transmit an anticollission bit frame */
45 static int
46 iso14443a_transcieve_acf(struct rfid_layer2_handle *handle,
47                          struct iso14443a_anticol_cmd *acf,
48                          unsigned int *bit_of_col)
49 {
50         struct rfid_reader *rdr = handle->rh->reader;
51
52         return rdr->iso14443a.transcieve_acf(handle->rh, acf, bit_of_col);
53 }
54
55 /* Transmit a regular frame */
56 static int 
57 iso14443a_transcieve(struct rfid_layer2_handle *handle,
58                      enum rfid_frametype frametype, 
59                         const unsigned char *tx_buf, unsigned int tx_len,
60                         unsigned char *rx_buf, unsigned int *rx_len,
61                         u_int64_t timeout, unsigned int flags)
62 {
63         return handle->rh->reader->transcieve(handle->rh, frametype, tx_buf,
64                                         tx_len, rx_buf, rx_len, timeout, flags);
65 }
66
67 static int 
68 iso14443a_code_nvb_bits(unsigned char *nvb, unsigned int bits)
69 {
70         unsigned int byte_count = bits / 8;
71         unsigned int bit_count = bits % 8;
72
73         if (byte_count < 2 || byte_count > 7)
74                 return -1;
75
76         *nvb = ((byte_count & 0xf) << 4) | bit_count;
77
78         return 0;
79 }
80
81 /* first bit is '1', second bit '2' */
82 static void
83 set_bit_in_field(unsigned char *bitfield, unsigned int bit)
84 {
85         unsigned int byte_count = bit / 8;
86         unsigned int bit_count = bit % 8;
87
88         DEBUGP("bitfield=%p, byte_count=%u, bit_count=%u\n",
89                         bitfield, byte_count, bit_count);
90         DEBUGP("%p = 0x%02x\n", (bitfield+byte_count), *(bitfield+byte_count));
91         *(bitfield+byte_count) |= 1 << (bit_count-1);
92         DEBUGP("%p = 0x%02x\n", (bitfield+byte_count), *(bitfield+byte_count));
93 }
94
95 static int
96 iso14443a_anticol(struct rfid_layer2_handle *handle)
97 {
98         int ret;
99         unsigned int uid_size;
100         struct iso14443a_handle *h = &handle->priv.iso14443a;
101         struct iso14443a_atqa atqa;
102         struct iso14443a_anticol_cmd acf;
103         unsigned int bit_of_col;
104         unsigned char sak[3];
105         unsigned int rx_len = sizeof(sak);
106         char *aqptr = (char *) &atqa;
107
108         memset(handle->uid, 0, sizeof(handle->uid));
109         memset(sak, 0, sizeof(sak));
110         memset(&atqa, 0, sizeof(atqa));
111         memset(&acf, 0, sizeof(acf));
112
113         ret = iso14443a_transcieve_sf(handle, ISO14443A_SF_CMD_REQA, &atqa);
114         if (ret < 0) {
115                 h->state = ISO14443A_STATE_REQA_SENT;
116                 DEBUGP("error during transcieve_sf: %d\n", ret);
117                 return ret;
118         }
119         h->state = ISO14443A_STATE_ATQA_RCVD;
120
121         DEBUGP("ATQA: 0x%02x 0x%02x\n", *aqptr, *(aqptr+1));
122
123         if (!atqa.bf_anticol) {
124                 h->state = ISO14443A_STATE_NO_BITFRAME_ANTICOL;
125                 DEBUGP("no bitframe anticollission bits set, aborting\n");
126                 return -1;
127         }
128
129         if (atqa.uid_size == 2 || atqa.uid_size == 3)
130                 uid_size = 3;
131         else if (atqa.uid_size == 1)
132                 uid_size = 2;
133         else
134                 uid_size = 1;
135         
136         acf.sel_code = ISO14443A_AC_SEL_CODE_CL1;
137
138         h->state = ISO14443A_STATE_ANTICOL_RUNNING;
139         h->level = ISO14443A_LEVEL_CL1;
140
141 cascade:
142         iso14443a_code_nvb_bits(&acf.nvb, 16);
143
144         ret = iso14443a_transcieve_acf(handle, &acf, &bit_of_col);
145         if (ret < 0)
146                 return ret;
147         DEBUGP("bit_of_col = %u\n", bit_of_col);
148         
149         while (bit_of_col != ISO14443A_BITOFCOL_NONE) {
150                 set_bit_in_field(&acf.uid_bits[0], bit_of_col-16);
151                 iso14443a_code_nvb_bits(&acf.nvb, bit_of_col);
152                 ret = iso14443a_transcieve_acf(handle, &acf, &bit_of_col);
153                 DEBUGP("bit_of_col = %u\n", bit_of_col);
154                 if (ret < 0)
155                         return ret;
156         }
157
158         iso14443a_code_nvb_bits(&acf.nvb, 7*8);
159         ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR,
160                                    (unsigned char *)&acf, 7, 
161                                    (unsigned char *) &sak, &rx_len,
162                                    TIMEOUT, 0);
163         if (ret < 0)
164                 return ret;
165
166         if (sak[0] & 0x04) {
167                 /* Cascade bit set, UID not complete */
168                 switch (acf.sel_code) {
169                 case ISO14443A_AC_SEL_CODE_CL1:
170                         /* cascading from CL1 to CL2 */
171                         if (acf.uid_bits[0] != 0x88) {
172                                 DEBUGP("Cascade bit set, but UID0 != 0x88\n");
173                                 return -1;
174                         }
175                         memcpy(&handle->uid[0], &acf.uid_bits[1], 3);
176                         acf.sel_code = ISO14443A_AC_SEL_CODE_CL2;
177                         h->level = ISO14443A_LEVEL_CL2;
178                         break;
179                 case ISO14443A_AC_SEL_CODE_CL2:
180                         /* cascading from CL2 to CL3 */
181                         memcpy(&handle->uid[3], &acf.uid_bits[1], 3);
182                         acf.sel_code = ISO14443A_AC_SEL_CODE_CL3;
183                         h->level = ISO14443A_LEVEL_CL3;
184                         break;
185                 default:
186                         DEBUGP("cannot cascade any further than CL3\n");
187                         h->state = ISO14443A_STATE_ERROR;
188                         return -1;
189                         break;
190                 }
191                 goto cascade;
192
193         } else {
194                 switch (acf.sel_code) {
195                 case ISO14443A_AC_SEL_CODE_CL1:
196                         /* single size UID (4 bytes) */
197                         memcpy(&handle->uid[0], &acf.uid_bits[0], 4);
198                         break;
199                 case ISO14443A_AC_SEL_CODE_CL2:
200                         /* double size UID (7 bytes) */
201                         memcpy(&handle->uid[3], &acf.uid_bits[0], 4);
202                         break;
203                 case ISO14443A_AC_SEL_CODE_CL3:
204                         /* triple size UID (10 bytes) */
205                         memcpy(&handle->uid[6], &acf.uid_bits[0], 4);
206                         break;
207                 }
208         }
209
210         h->level = ISO14443A_LEVEL_NONE;
211         h->state = ISO14443A_STATE_SELECTED;
212
213         {
214                 if (uid_size == 1)
215                         handle->uid_len = 4;
216                 else if (uid_size == 2)
217                         handle->uid_len = 7;
218                 else 
219                         handle->uid_len = 10;
220
221                 DEBUGP("UID %s\n", rfid_hexdump(handle->uid, handle->uid_len));
222         }
223
224         if (sak[0] & 0x20) {
225                 DEBUGP("we have a T=CL compliant PICC\n");
226                 h->tcl_capable = 1;
227         } else {
228                 DEBUGP("we have a T!=CL PICC\n");
229                 h->tcl_capable = 0;
230         }
231
232         return 0;
233 }
234
235 static int
236 iso14443a_hlta(struct rfid_layer2_handle *handle)
237 {
238         int ret;
239         unsigned char tx_buf[2] = { 0x50, 0x00 };
240         unsigned char rx_buf[10];
241         unsigned int rx_len = sizeof(rx_buf);
242
243         ret = iso14443a_transcieve(handle, RFID_14443A_FRAME_REGULAR,
244                                    tx_buf, sizeof(tx_buf),
245                                    rx_buf, &rx_len, 1000 /* 1ms */, 0);
246         if (ret < 0) {
247                 /* "error" case: we don't get somethng back from the card */
248                 return 0;
249         }
250         return -1;
251 }
252
253 static struct rfid_layer2_handle *
254 iso14443a_init(struct rfid_reader_handle *rh)
255 {
256         int ret;
257         struct rfid_layer2_handle *h = malloc(sizeof(*h));
258         if (!h)
259                 return NULL;
260
261         h->l2 = &rfid_layer2_iso14443a;
262         h->rh = rh;
263         h->priv.iso14443a.state = ISO14443A_STATE_NONE;
264         h->priv.iso14443a.level = ISO14443A_LEVEL_NONE;
265
266         ret = h->rh->reader->iso14443a.init(h->rh);
267         if (ret < 0) {
268                 free(h);
269                 return NULL;
270         }
271
272         return h;
273 }
274
275 static int
276 iso14443a_fini(struct rfid_layer2_handle *handle)
277 {
278         free(handle);
279         return 0;
280 }
281
282
283 struct rfid_layer2 rfid_layer2_iso14443a = {
284         .id     = RFID_LAYER2_ISO14443A,
285         .name   = "ISO 14443-3 A",
286         .fn     = {
287                 .init           = &iso14443a_init,
288                 .open           = &iso14443a_anticol,
289                 .transcieve     = &iso14443a_transcieve,
290                 .close          = &iso14443a_hlta,
291                 .fini           = &iso14443a_fini,
292         },
293 };
294