1 |
d60b7ba4
|
laforge
|
/* main_mifare - OpenPCD firmware using in-firmware librfid
|
2 |
|
|
*
|
3 |
|
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
4 |
|
|
*
|
5 |
|
|
* This program is free software; you can redistribute it and/or modify
|
6 |
|
|
* it under the terms of the GNU General Public License as published by
|
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
8 |
|
|
* (at your option) any later version.
|
9 |
|
|
*
|
10 |
|
|
* This program is distributed in the hope that it will be useful,
|
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
|
|
* GNU General Public License for more details.
|
14 |
|
|
*
|
15 |
|
|
* You should have received a copy of the GNU General Public License
|
16 |
|
|
* along with this program; if not, write to the Free Software
|
17 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
|
|
*
|
19 |
|
|
*/
|
20 |
|
|
|
21 |
|
|
#include <errno.h>
|
22 |
|
|
#include <string.h>
|
23 |
|
|
#include <lib_AT91SAM7.h>
|
24 |
|
|
#include <librfid/rfid.h>
|
25 |
|
|
#include <librfid/rfid_scan.h>
|
26 |
|
|
#include <librfid/rfid_reader.h>
|
27 |
|
|
#include <librfid/rfid_layer2.h>
|
28 |
|
|
#include <librfid/rfid_protocol.h>
|
29 |
|
|
//#include "rc632.h"
|
30 |
|
|
#include <os/dbgu.h>
|
31 |
|
|
#include <os/led.h>
|
32 |
|
|
#include <os/pcd_enumerate.h>
|
33 |
|
|
#include <os/trigger.h>
|
34 |
|
|
#include <os/req_ctx.h>
|
35 |
|
|
#include <pcd/rc632.h>
|
36 |
|
|
|
37 |
|
|
#include "../openpcd.h"
|
38 |
|
|
|
39 |
|
|
static struct rfid_reader_handle *rh;
|
40 |
|
|
static struct rfid_layer2_handle *l2h;
|
41 |
|
|
static struct rfid_protocol_handle *ph;
|
42 |
|
|
|
43 |
373c172a
|
Harald Welte
|
static uint8_t sector = 0;
|
44 |
d60b7ba4
|
laforge
|
|
45 |
|
|
void _init_func(void)
|
46 |
|
|
{
|
47 |
|
|
trigger_init();
|
48 |
|
|
rc632_init();
|
49 |
|
|
rc632_test(NULL);
|
50 |
|
|
DEBUGP("opening reader ");
|
51 |
|
|
#if 1
|
52 |
|
|
rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
|
53 |
|
|
DEBUGP("rh=%p ", rh);
|
54 |
|
|
#endif
|
55 |
|
|
led_switch(2, 1);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
int _main_dbgu(char key)
|
59 |
|
|
{
|
60 |
|
|
int ret = -EINVAL;
|
61 |
|
|
switch (key) {
|
62 |
|
|
case 'j':
|
63 |
|
|
ret = 0;
|
64 |
|
|
if (sector > 0);
|
65 |
|
|
sector--;
|
66 |
|
|
break;
|
67 |
|
|
case 'k':
|
68 |
|
|
ret = 0;
|
69 |
|
|
if (sector < 15)
|
70 |
|
|
sector++;
|
71 |
|
|
break;
|
72 |
|
|
}
|
73 |
|
|
return ret;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
struct openpcd_l2_connectinfo {
|
77 |
373c172a
|
Harald Welte
|
uint32_t proto_supported;
|
78 |
d60b7ba4
|
laforge
|
|
79 |
373c172a
|
Harald Welte
|
uint8_t speed_rx;
|
80 |
|
|
uint8_t speed_tx;
|
81 |
d60b7ba4
|
laforge
|
|
82 |
373c172a
|
Harald Welte
|
uint8_t uid_len;
|
83 |
|
|
uint8_t uid[10];
|
84 |
d60b7ba4
|
laforge
|
} __attribute__ ((packed));
|
85 |
|
|
|
86 |
|
|
struct openpcd_proto_connectinfo {
|
87 |
|
|
} __attribute__ ((packed));
|
88 |
|
|
|
89 |
|
|
struct openpcd_proto_tcl_connectinfo {
|
90 |
373c172a
|
Harald Welte
|
uint8_t fsc;
|
91 |
|
|
uint8_t fsd;
|
92 |
|
|
uint8_t ta;
|
93 |
|
|
uint8_t sfgt;
|
94 |
d60b7ba4
|
laforge
|
|
95 |
373c172a
|
Harald Welte
|
uint8_t flags;
|
96 |
|
|
uint8_t cid;
|
97 |
|
|
uint8_t nad;
|
98 |
d60b7ba4
|
laforge
|
|
99 |
373c172a
|
Harald Welte
|
uint8_t ats_tot_len;
|
100 |
|
|
uint8_t ats_snippet[0];
|
101 |
d60b7ba4
|
laforge
|
} __attribute__ ((packed));
|
102 |
|
|
|
103 |
|
|
/* mifare classic helper */
|
104 |
|
|
static int
|
105 |
|
|
mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
|
106 |
|
|
{
|
107 |
|
|
unsigned char buf[20];
|
108 |
|
|
unsigned int len = sizeof(buf);
|
109 |
|
|
int ret;
|
110 |
|
|
int block;
|
111 |
|
|
|
112 |
|
|
/* FIXME: make this work for sectors > 31 */
|
113 |
|
|
DEBUGPCR("Reading sector %u", sector);
|
114 |
|
|
|
115 |
|
|
for (block = sector*4; block < sector*4+4; block++) {
|
116 |
|
|
DEBUGP("Reading block %u: ", block);
|
117 |
|
|
ret = rfid_protocol_read(ph, block, buf, &len);
|
118 |
|
|
if(ret == -ETIMEDOUT)
|
119 |
|
|
DEBUGPCR("TIMEOUT");
|
120 |
|
|
if (ret < 0) {
|
121 |
|
|
DEBUGPCR("Error %d reading", ret);
|
122 |
|
|
return ret;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
DEBUGPCR("Page 0x%x: %s", block, hexdump(buf, len));
|
126 |
|
|
}
|
127 |
|
|
return 0;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
static int init_proto(void)
|
131 |
|
|
{
|
132 |
|
|
struct req_ctx *detect_rctx;
|
133 |
|
|
struct openpcd_hdr *opcdh;
|
134 |
|
|
struct openpcd_l2_connectinfo *l2c;
|
135 |
|
|
struct openpcd_proto_connectinfo *pc;
|
136 |
|
|
unsigned int size;
|
137 |
|
|
|
138 |
|
|
l2h = rfid_layer2_scan(rh);
|
139 |
|
|
if (!l2h)
|
140 |
|
|
return 0;
|
141 |
|
|
|
142 |
|
|
DEBUGP("l2='%s' ", rfid_layer2_name(l2h));
|
143 |
|
|
|
144 |
|
|
detect_rctx = req_ctx_find_get(0, RCTX_STATE_FREE,
|
145 |
|
|
RCTX_STATE_LIBRFID_BUSY);
|
146 |
|
|
if (detect_rctx) {
|
147 |
|
|
unsigned int uid_len;
|
148 |
|
|
opcdh = (struct openpcd_hdr *) detect_rctx->data;
|
149 |
|
|
l2c = (struct openpcd_l2_connectinfo *)
|
150 |
|
|
(char *) opcdh + sizeof(opcdh);
|
151 |
|
|
l2c->uid_len = sizeof(l2c->uid);
|
152 |
|
|
opcdh->cmd = OPENPCD_CMD_LRFID_DETECT_IRQ;
|
153 |
|
|
opcdh->flags = 0x00;
|
154 |
|
|
opcdh->reg = 0x03;
|
155 |
|
|
opcdh->val = l2h->l2->id;
|
156 |
|
|
|
157 |
|
|
detect_rctx->tot_len = sizeof(*opcdh) + sizeof(*l2c);
|
158 |
|
|
#if 0
|
159 |
|
|
/* copy UID / PUPI into data section */
|
160 |
|
|
rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, (void *)l2c->uid,
|
161 |
|
|
&uid_len);
|
162 |
|
|
l2c->uid_len = uid_len & 0xff;
|
163 |
|
|
|
164 |
|
|
size = sizeof(l2c->proto_supported);
|
165 |
|
|
rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_PROTO_SUPP,
|
166 |
|
|
&l2c->proto_supported, &size);
|
167 |
|
|
|
168 |
|
|
switch (l2h->l2->id) {
|
169 |
|
|
case RFID_LAYER2_ISO14443A:
|
170 |
|
|
break;
|
171 |
|
|
case RFID_LAYER2_ISO14443B:
|
172 |
|
|
break;
|
173 |
|
|
case RFID_LAYER2_ISO15693:
|
174 |
|
|
break;
|
175 |
|
|
}
|
176 |
|
|
#endif
|
177 |
|
|
req_ctx_set_state(detect_rctx, RCTX_STATE_UDP_EP3_PENDING);
|
178 |
|
|
} else
|
179 |
|
|
DEBUGPCRF("=>>>>>>>>>>>>>>no req_ctx for L2!");
|
180 |
|
|
ph = rfid_protocol_scan(l2h);
|
181 |
|
|
if (!ph)
|
182 |
|
|
return 3;
|
183 |
|
|
|
184 |
|
|
DEBUGP("p='%s' ", rfid_protocol_name(ph));
|
185 |
|
|
detect_rctx = req_ctx_find_get(0, RCTX_STATE_FREE,
|
186 |
|
|
RCTX_STATE_LIBRFID_BUSY);
|
187 |
|
|
if (detect_rctx) {
|
188 |
|
|
opcdh = (struct openpcd_hdr *) detect_rctx->data;
|
189 |
|
|
pc = (struct openpcd_proto_connectinfo *)
|
190 |
|
|
((char *) opcdh + sizeof(*opcdh));
|
191 |
|
|
detect_rctx->tot_len = sizeof(*opcdh) + sizeof(*pc);
|
192 |
|
|
opcdh->cmd = OPENPCD_CMD_LRFID_DETECT_IRQ;
|
193 |
|
|
opcdh->flags = 0x00;
|
194 |
|
|
opcdh->reg = 0x04;
|
195 |
|
|
opcdh->val = ph->proto->id;
|
196 |
|
|
/* copy L4 info into data section */
|
197 |
|
|
|
198 |
|
|
#if 0
|
199 |
|
|
switch (ph->proto->id) {
|
200 |
|
|
case RFID_PROTOCOL_TCL: {
|
201 |
|
|
struct openpcd_proto_tcl_connectinfo *ptc
|
202 |
|
|
= (struct openpcd_proto_tcl_connectinfo *)
|
203 |
|
|
((char *) ph + sizeof(*ph));
|
204 |
|
|
unsigned int space;
|
205 |
|
|
detect_rctx->tot_len += sizeof(*ptc);
|
206 |
|
|
space = detect_rctx->size - sizeof(*opcdh)-sizeof(*pc);
|
207 |
|
|
size = space;
|
208 |
|
|
rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS,
|
209 |
|
|
&ptc->ats_snippet, &size);
|
210 |
|
|
if (size == space) {
|
211 |
|
|
/* we've only copied part of the ATS */
|
212 |
|
|
size = sizeof(ptc->ats_tot_len);
|
213 |
|
|
rfid_protocol_getopt(ph,
|
214 |
|
|
RFID_OPT_P_TCL_ATS_LEN,
|
215 |
|
|
&ptc->ats_tot_len, &size);
|
216 |
|
|
} else {
|
217 |
|
|
ptc->ats_tot_len = size;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
} break;
|
221 |
|
|
}
|
222 |
|
|
#endif
|
223 |
|
|
req_ctx_set_state(detect_rctx, RCTX_STATE_UDP_EP3_PENDING);
|
224 |
|
|
} else
|
225 |
|
|
DEBUGPCRF("=>>>>>>>>>>>>>>no req_ctx for L2!");
|
226 |
|
|
led_switch(1, 1);
|
227 |
|
|
|
228 |
|
|
if (ph->proto->id == RFID_PROTOCOL_MIFARE_CLASSIC) {
|
229 |
|
|
int rc;
|
230 |
|
|
|
231 |
|
|
DEBUGPCR("Authenticating sector %u: ", sector);
|
232 |
|
|
rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
|
233 |
|
|
if (rc < 0) {
|
234 |
|
|
DEBUGPCR("key format error");
|
235 |
|
|
return 4;
|
236 |
|
|
}
|
237 |
|
|
rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
|
238 |
|
|
if (rc < 0) {
|
239 |
|
|
DEBUGPCR("mifare auth error");
|
240 |
|
|
return 4;
|
241 |
|
|
} else
|
242 |
|
|
DEBUGPCR("mifare auth succeeded!\n");
|
243 |
|
|
|
244 |
|
|
mifare_classic_read_sector(ph, sector);
|
245 |
|
|
|
246 |
|
|
return 5;
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
return 4;
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
static int opcd_lrfid_usb_in(struct req_ctx *rctx)
|
253 |
|
|
{
|
254 |
|
|
struct openpcd_hdr *poh = (struct openpcd_hdr *) rctx->data;
|
255 |
|
|
return 0;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
void _main_func(void)
|
260 |
|
|
{
|
261 |
|
|
int ret;
|
262 |
|
|
|
263 |
|
|
usb_out_process();
|
264 |
|
|
usb_in_process();
|
265 |
|
|
|
266 |
|
|
ret = init_proto();
|
267 |
|
|
|
268 |
|
|
if (ret >= 4)
|
269 |
|
|
rfid_protocol_close(ph);
|
270 |
|
|
if (ret >= 3)
|
271 |
|
|
rfid_layer2_close(l2h);
|
272 |
|
|
|
273 |
|
|
rc632_turn_off_rf(NULL);
|
274 |
|
|
{ volatile int i; for (i = 0; i < 0xfffff; i++) ; }
|
275 |
|
|
rc632_turn_on_rf(NULL);
|
276 |
|
|
|
277 |
|
|
led_switch(1, 0);
|
278 |
|
|
led_toggle(2);
|
279 |
|
|
}
|