strongarm ioctl rename fallout
[qemu/aliguori.git] / hw / ccid-card-passthru.c
blob60b4e2b77df6b5c167e5444adef0b676424cef95
1 /*
2 * CCID Passthru Card Device emulation
4 * Copyright (c) 2011 Red Hat.
5 * Written by Alon Levy.
7 * This work is licensed under the terms of the GNU GPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu-char.h"
12 #include "qemu_socket.h"
13 #include "monitor.h"
14 #include "hw/ccid.h"
15 #include "libcacard/vscard_common.h"
17 #define DPRINTF(card, lvl, fmt, ...) \
18 do { \
19 if (lvl <= card->debug) { \
20 printf("ccid-card-passthru: " fmt , ## __VA_ARGS__); \
21 } \
22 } while (0)
24 #define D_WARN 1
25 #define D_INFO 2
26 #define D_MORE_INFO 3
27 #define D_VERBOSE 4
29 /* TODO: do we still need this? */
30 uint8_t DEFAULT_ATR[] = {
32 * From some example somewhere
33 * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
36 /* From an Athena smart card */
37 0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
38 0x13, 0x08
42 #define PASSTHRU_DEV_NAME "ccid-card-passthru"
43 #define VSCARD_IN_SIZE 65536
45 /* maximum size of ATR - from 7816-3 */
46 #define MAX_ATR_SIZE 40
48 typedef struct PassthruState PassthruState;
50 struct PassthruState {
51 CCIDCardState base;
52 CharDriverState *cs;
53 uint8_t vscard_in_data[VSCARD_IN_SIZE];
54 uint32_t vscard_in_pos;
55 uint32_t vscard_in_hdr;
56 uint8_t atr[MAX_ATR_SIZE];
57 uint8_t atr_length;
58 uint8_t debug;
61 static void ccid_card_vscard_update_handlers(PassthruState *card);
64 * VSCard protocol over chardev
65 * This code should not depend on the card type.
68 static void ccid_card_vscard_send_msg(PassthruState *s,
69 VSCMsgType type, uint32_t reader_id,
70 const uint8_t *payload, uint32_t length)
72 VSCMsgHeader scr_msg_header;
74 scr_msg_header.type = htonl(type);
75 scr_msg_header.reader_id = htonl(reader_id);
76 scr_msg_header.length = htonl(length);
77 qemu_chr_fe_write(s->cs, (uint8_t *)&scr_msg_header, sizeof(VSCMsgHeader));
78 qemu_chr_fe_write(s->cs, payload, length);
81 static void ccid_card_vscard_send_apdu(PassthruState *s,
82 const uint8_t *apdu, uint32_t length)
84 ccid_card_vscard_send_msg(
85 s, VSC_APDU, VSCARD_MINIMAL_READER_ID, apdu, length);
88 static void ccid_card_vscard_send_error(PassthruState *s,
89 uint32_t reader_id, VSCErrorCode code)
91 VSCMsgError msg = {.code = htonl(code)};
93 ccid_card_vscard_send_msg(
94 s, VSC_Error, reader_id, (uint8_t *)&msg, sizeof(msg));
97 static void ccid_card_vscard_send_init(PassthruState *s)
99 VSCMsgInit msg = {
100 .version = htonl(VSCARD_VERSION),
101 .magic = VSCARD_MAGIC,
102 .capabilities = {0}
105 ccid_card_vscard_send_msg(s, VSC_Init, VSCARD_UNDEFINED_READER_ID,
106 (uint8_t *)&msg, sizeof(msg));
109 static int ccid_card_vscard_can_read(PassthruState *card)
111 return VSCARD_IN_SIZE >= card->vscard_in_pos ?
112 VSCARD_IN_SIZE - card->vscard_in_pos : 0;
115 static void ccid_card_vscard_handle_init(
116 PassthruState *card, VSCMsgHeader *hdr, VSCMsgInit *init)
118 uint32_t *capabilities;
119 int num_capabilities;
120 int i;
122 capabilities = init->capabilities;
123 num_capabilities =
124 1 + ((hdr->length - sizeof(VSCMsgInit)) / sizeof(uint32_t));
125 init->version = ntohl(init->version);
126 for (i = 0 ; i < num_capabilities; ++i) {
127 capabilities[i] = ntohl(capabilities[i]);
129 if (init->magic != VSCARD_MAGIC) {
130 error_report("wrong magic");
131 /* we can't disconnect the chardev */
133 if (init->version != VSCARD_VERSION) {
134 DPRINTF(card, D_WARN,
135 "got version %d, have %d", init->version, VSCARD_VERSION);
137 /* future handling of capabilities, none exist atm */
138 ccid_card_vscard_send_init(card);
141 static void ccid_card_vscard_handle_message(PassthruState *card,
142 VSCMsgHeader *scr_msg_header)
144 uint8_t *data = (uint8_t *)&scr_msg_header[1];
146 switch (scr_msg_header->type) {
147 case VSC_ATR:
148 DPRINTF(card, D_INFO, "VSC_ATR %d\n", scr_msg_header->length);
149 if (scr_msg_header->length > MAX_ATR_SIZE) {
150 error_report("ATR size exceeds spec, ignoring");
151 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
152 VSC_GENERAL_ERROR);
154 memcpy(card->atr, data, scr_msg_header->length);
155 card->atr_length = scr_msg_header->length;
156 ccid_card_card_inserted(&card->base);
157 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
158 VSC_SUCCESS);
159 break;
160 case VSC_APDU:
161 ccid_card_send_apdu_to_guest(
162 &card->base, data, scr_msg_header->length);
163 break;
164 case VSC_CardRemove:
165 DPRINTF(card, D_INFO, "VSC_CardRemove\n");
166 ccid_card_card_removed(&card->base);
167 ccid_card_vscard_send_error(card,
168 scr_msg_header->reader_id, VSC_SUCCESS);
169 break;
170 case VSC_Init:
171 ccid_card_vscard_handle_init(
172 card, scr_msg_header, (VSCMsgInit *)data);
173 break;
174 case VSC_Error:
175 ccid_card_card_error(&card->base, *(uint32_t *)data);
176 break;
177 case VSC_ReaderAdd:
178 if (ccid_card_ccid_attach(&card->base) < 0) {
179 ccid_card_vscard_send_error(card, VSCARD_UNDEFINED_READER_ID,
180 VSC_CANNOT_ADD_MORE_READERS);
181 } else {
182 ccid_card_vscard_send_error(card, VSCARD_MINIMAL_READER_ID,
183 VSC_SUCCESS);
185 break;
186 case VSC_ReaderRemove:
187 ccid_card_ccid_detach(&card->base);
188 ccid_card_vscard_send_error(card,
189 scr_msg_header->reader_id, VSC_SUCCESS);
190 break;
191 default:
192 printf("usb-ccid: chardev: unexpected message of type %X\n",
193 scr_msg_header->type);
194 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
195 VSC_GENERAL_ERROR);
199 static void ccid_card_vscard_drop_connection(PassthruState *card)
201 qemu_chr_fe_delete(card->cs);
202 card->vscard_in_pos = card->vscard_in_hdr = 0;
203 ccid_card_vscard_update_handlers(card);
206 static void ccid_card_vscard_read(PassthruState *card, const uint8_t *buf, int size)
208 VSCMsgHeader *hdr;
210 if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
211 error_report(
212 "no room for data: pos %d + size %d > %d. dropping connection.",
213 card->vscard_in_pos, size, VSCARD_IN_SIZE);
214 ccid_card_vscard_drop_connection(card);
215 return;
217 assert(card->vscard_in_pos < VSCARD_IN_SIZE);
218 assert(card->vscard_in_hdr < VSCARD_IN_SIZE);
219 memcpy(card->vscard_in_data + card->vscard_in_pos, buf, size);
220 card->vscard_in_pos += size;
221 hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
223 while ((card->vscard_in_pos - card->vscard_in_hdr >= sizeof(VSCMsgHeader))
224 &&(card->vscard_in_pos - card->vscard_in_hdr >=
225 sizeof(VSCMsgHeader) + ntohl(hdr->length))) {
226 hdr->reader_id = ntohl(hdr->reader_id);
227 hdr->length = ntohl(hdr->length);
228 hdr->type = ntohl(hdr->type);
229 ccid_card_vscard_handle_message(card, hdr);
230 card->vscard_in_hdr += hdr->length + sizeof(VSCMsgHeader);
231 hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
233 if (card->vscard_in_hdr == card->vscard_in_pos) {
234 card->vscard_in_pos = card->vscard_in_hdr = 0;
236 ccid_card_vscard_update_handlers(card);
239 static int ccid_card_vscard_event(void *opaque, int event, void *data)
241 PassthruState *card = opaque;
243 switch (event) {
244 case CHR_EVENT_BREAK:
245 card->vscard_in_pos = card->vscard_in_hdr = 0;
246 ccid_card_vscard_update_handlers(card);
247 break;
248 case CHR_EVENT_FOCUS:
249 break;
250 case CHR_EVENT_OPENED:
251 DPRINTF(card, D_INFO, "%s: CHR_EVENT_OPENED\n", __func__);
252 break;
254 return 0;
257 static void ccid_card_vscard_read_handler(void *opaque)
259 PassthruState *card = opaque;
260 uint8_t buf[32];
261 int size;
263 size = ccid_card_vscard_can_read(card);
264 size = MIN(size, sizeof(buf));
265 size = qemu_chr_fe_read(card->cs, buf, size);
267 ccid_card_vscard_read(card, buf, size);
270 static void ccid_card_vscard_update_handlers(PassthruState *card)
272 if (ccid_card_vscard_can_read(card) > 0) {
273 qemu_chr_fe_set_handlers(card->cs,
274 ccid_card_vscard_read_handler,
275 NULL,
276 ccid_card_vscard_event, card);
277 } else {
278 qemu_chr_fe_set_handlers(card->cs,
279 NULL, NULL,
280 ccid_card_vscard_event, card);
284 /* End VSCard handling */
286 static void passthru_apdu_from_guest(
287 CCIDCardState *base, const uint8_t *apdu, uint32_t len)
289 PassthruState *card = DO_UPCAST(PassthruState, base, base);
291 if (!card->cs) {
292 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len);
293 return;
295 ccid_card_vscard_send_apdu(card, apdu, len);
298 static const uint8_t *passthru_get_atr(CCIDCardState *base, uint32_t *len)
300 PassthruState *card = DO_UPCAST(PassthruState, base, base);
302 *len = card->atr_length;
303 return card->atr;
306 static int passthru_initfn(CCIDCardState *base)
308 PassthruState *card = DO_UPCAST(PassthruState, base, base);
310 card->vscard_in_pos = 0;
311 card->vscard_in_hdr = 0;
312 if (card->cs) {
313 DPRINTF(card, D_INFO, "initing chardev\n");
314 qemu_chr_fe_open(card->cs);
315 ccid_card_vscard_update_handlers(card);
316 } else {
317 error_report("missing chardev");
318 return -1;
320 assert(sizeof(DEFAULT_ATR) <= MAX_ATR_SIZE);
321 memcpy(card->atr, DEFAULT_ATR, sizeof(DEFAULT_ATR));
322 card->atr_length = sizeof(DEFAULT_ATR);
323 return 0;
326 static int passthru_exitfn(CCIDCardState *base)
328 return 0;
331 static VMStateDescription passthru_vmstate = {
332 .name = PASSTHRU_DEV_NAME,
333 .version_id = 1,
334 .minimum_version_id = 1,
335 .fields = (VMStateField[]) {
336 VMSTATE_BUFFER(vscard_in_data, PassthruState),
337 VMSTATE_UINT32(vscard_in_pos, PassthruState),
338 VMSTATE_UINT32(vscard_in_hdr, PassthruState),
339 VMSTATE_BUFFER(atr, PassthruState),
340 VMSTATE_UINT8(atr_length, PassthruState),
341 VMSTATE_END_OF_LIST()
345 static CCIDCardInfo passthru_card_info = {
346 .qdev.name = PASSTHRU_DEV_NAME,
347 .qdev.desc = "passthrough smartcard",
348 .qdev.size = sizeof(PassthruState),
349 .qdev.vmsd = &passthru_vmstate,
350 .initfn = passthru_initfn,
351 .exitfn = passthru_exitfn,
352 .get_atr = passthru_get_atr,
353 .apdu_from_guest = passthru_apdu_from_guest,
354 .qdev.props = (Property[]) {
355 DEFINE_PROP_CHR("chardev", PassthruState, cs),
356 DEFINE_PROP_UINT8("debug", PassthruState, debug, 0),
357 DEFINE_PROP_END_OF_LIST(),
361 static void ccid_card_passthru_register_devices(void)
363 ccid_card_qdev_register(&passthru_card_info);
366 device_init(ccid_card_passthru_register_devices)