From e404cccecd20a2418c2429cd182c18218e54bd28 Mon Sep 17 00:00:00 2001 From: laforge Date: Sat, 14 Oct 2006 12:34:38 +0000 Subject: [PATCH] Prepare RFID compilation in firmware mode - switch from linked lists to static arrays - remove all non-handle dynamic allocations (at the expense of stack) - declare all proto/reader/asic/layer2 structures as const - wrap all handle allocations in macros that expand to references to static structures in case of firmware mode - update copyright notices - add skeleton code for openpcd-inside-firmware driver - update TODO with remaining TODO for firmware mode git-svn-id: https://svn.gnumonks.org/trunk/librfid@1903 e0336214-984f-0b4b-a45f-81c69e1f0ede --- TODO | 10 ++- include/librfid/Makefile.am | 1 + include/librfid/rfid.h | 2 + include/librfid/rfid_layer2_iso14443a.h | 2 +- include/librfid/rfid_layer2_iso14443b.h | 6 +- include/librfid/rfid_layer2_iso15693.h | 2 +- .../librfid/rfid_protocol_mifare_classic.h | 4 +- include/librfid/rfid_protocol_mifare_ul.h | 2 +- include/librfid/rfid_protocol_tcl.h | 5 +- include/librfid/rfid_reader_cm5121.h | 5 +- include/librfid/rfid_reader_openpcd.h | 2 +- include/librfid/rfid_system.h | 53 +++++++++++ src/rfid.c | 12 ++- src/rfid_asic_rc632.c | 14 +-- src/rfid_layer2.c | 26 +++--- src/rfid_layer2_iso14443a.c | 8 +- src/rfid_layer2_iso14443b.c | 34 +++----- src/rfid_layer2_iso15693.c | 10 +-- src/rfid_proto_mifare_classic.c | 8 +- src/rfid_proto_mifare_ul.c | 8 +- src/rfid_proto_tcl.c | 10 +-- src/rfid_protocol.c | 28 +++--- src/rfid_reader.c | 34 ++++---- src/rfid_reader_cm5121.c | 18 ++-- src/rfid_reader_openpcd.c | 87 ++++++++++++++----- 25 files changed, 249 insertions(+), 142 deletions(-) create mode 100644 include/librfid/rfid_system.h diff --git a/TODO b/TODO index 136a254..736cb6a 100644 --- a/TODO +++ b/TODO @@ -16,7 +16,6 @@ iso15693: - implement all the rest mifare_clasic: -- figure out why authentication doensn't work, even though it is exactly like in other drivers tcl: - implement pps for asymmetric (rx/tx) speeds @@ -28,3 +27,12 @@ openct: other: - implementation of code for various passive tags - documentation + +firmware compilation: +- add configure '--firmware-mode' + - define LIBRFID_FIRMWARE + - don't link with libusb + - don't include CCID code + - specify include path for reader firmware system include files + - get rid of remaining malloc()s in 14443b and tcl + - fix 'mru' allocation size problem in tcl diff --git a/include/librfid/Makefile.am b/include/librfid/Makefile.am index 4b1337d..1edb60b 100644 --- a/include/librfid/Makefile.am +++ b/include/librfid/Makefile.am @@ -6,6 +6,7 @@ pkginclude_HEADERS = rfid.h rfid_scan.h rfid_asic.h rfid_asic_rc632.h \ rfid_protocol_mifare_ul.h \ rfid_protocol_mifare_classic.h \ rfid_reader.h \ + rfid_system.h \ rfid_reader_cm5121.h \ rfid_reader_openpcd.h diff --git a/include/librfid/rfid.h b/include/librfid/rfid.h index c3f8fc1..f0ecc16 100644 --- a/include/librfid/rfid.h +++ b/include/librfid/rfid.h @@ -5,6 +5,8 @@ #ifdef __LIBRFID__ +#include + enum rfid_frametype { RFID_14443A_FRAME_REGULAR, RFID_14443B_FRAME_REGULAR, diff --git a/include/librfid/rfid_layer2_iso14443a.h b/include/librfid/rfid_layer2_iso14443a.h index 87b6be0..283e6b6 100644 --- a/include/librfid/rfid_layer2_iso14443a.h +++ b/include/librfid/rfid_layer2_iso14443a.h @@ -84,7 +84,7 @@ enum iso14443a_state { #include -struct rfid_layer2 rfid_layer2_iso14443a; +extern const struct rfid_layer2 rfid_layer2_iso14443a; #endif /* __LIBRFID__ */ diff --git a/include/librfid/rfid_layer2_iso14443b.h b/include/librfid/rfid_layer2_iso14443b.h index 07c185f..7d2cb29 100644 --- a/include/librfid/rfid_layer2_iso14443b.h +++ b/include/librfid/rfid_layer2_iso14443b.h @@ -26,7 +26,7 @@ struct iso14443b_atqb { adc:2, fwi:4; } protocol_info; -}; +} __attribute__ ((packed)); struct iso14443b_attrib_hdr { unsigned char one_d; @@ -51,7 +51,7 @@ struct iso14443b_attrib_hdr { unsigned char cid:4, rfu:4; } param4; -}; +} __attribute__ ((packed)); struct iso14443b_handle { unsigned int tcl_capable; /* do we support T=CL */ @@ -89,7 +89,7 @@ enum { }; #include -struct rfid_layer2 rfid_layer2_iso14443b; +extern const struct rfid_layer2 rfid_layer2_iso14443b; #endif /* __LIBRFID__ */ diff --git a/include/librfid/rfid_layer2_iso15693.h b/include/librfid/rfid_layer2_iso15693.h index d91b4ec..37d204d 100644 --- a/include/librfid/rfid_layer2_iso15693.h +++ b/include/librfid/rfid_layer2_iso15693.h @@ -50,6 +50,6 @@ enum iso15693_state { }; #include -extern struct rfid_layer2 rfid_layer2_iso15693; +extern const struct rfid_layer2 rfid_layer2_iso15693; #endif /* _ISO15693_H */ diff --git a/include/librfid/rfid_protocol_mifare_classic.h b/include/librfid/rfid_protocol_mifare_classic.h index e6b2400..06235b8 100644 --- a/include/librfid/rfid_protocol_mifare_classic.h +++ b/include/librfid/rfid_protocol_mifare_classic.h @@ -1,5 +1,7 @@ #ifndef _MIFARE_CLASSIC_H +#include + #define MIFARE_CL_KEYA_DEFAULT "\xa0\xa1\xa2\xa3\xa4\xa5" #define MIFARE_CL_KEYB_DEFAULT "\xb0\xb1\xb2\xb3\xb4\xb5" @@ -13,7 +15,7 @@ #ifdef __LIBRFID__ -extern struct rfid_protocol rfid_protocol_mfcl; +extern const struct rfid_protocol rfid_protocol_mfcl; #define MIFARE_CL_CMD_WRITE16 0xA0 diff --git a/include/librfid/rfid_protocol_mifare_ul.h b/include/librfid/rfid_protocol_mifare_ul.h index cc93b6b..593894e 100644 --- a/include/librfid/rfid_protocol_mifare_ul.h +++ b/include/librfid/rfid_protocol_mifare_ul.h @@ -17,7 +17,7 @@ int rfid_mful_lock_otp(struct rfid_protocol_handle *ph); #define MIFARE_UL_PAGE_LOCK 2 #define MIFARE_UL_PAGE_OTP 3 -extern struct rfid_protocol rfid_protocol_mful; +extern const struct rfid_protocol rfid_protocol_mful; #endif /* __LIBRFID__ */ diff --git a/include/librfid/rfid_protocol_tcl.h b/include/librfid/rfid_protocol_tcl.h index 180c008..2ee1c27 100644 --- a/include/librfid/rfid_protocol_tcl.h +++ b/include/librfid/rfid_protocol_tcl.h @@ -36,7 +36,8 @@ struct tcl_handle { unsigned int toggle; /* send toggle with next frame */ unsigned int ats_len; - unsigned char ats[0]; + unsigned char ats[256]; /* ATS cannot be bigger than FSD-2 bytes, + according to ISO 14443-4 5.2.2 */ }; enum tcl_handle_flags { @@ -63,7 +64,7 @@ enum tcl_pcd_state { TCL_STATE_DESELECTED, /* card deselected or HLTA'd */ }; -struct rfid_protocol rfid_protocol_tcl; +extern const struct rfid_protocol rfid_protocol_tcl; #endif /* __LIBRFID__ */ diff --git a/include/librfid/rfid_reader_cm5121.h b/include/librfid/rfid_reader_cm5121.h index 8d9a312..5a1b7c3 100644 --- a/include/librfid/rfid_reader_cm5121.h +++ b/include/librfid/rfid_reader_cm5121.h @@ -1,6 +1,8 @@ #ifndef _RFID_READER_CM5121_H #define _RFID_READER_CM5121_H +#include + #define CM5121_CW_CONDUCTANCE 0x3f #define CM5121_MOD_CONDUCTANCE 0x3f #define CM5121_14443A_BITPHASE 0xa9 @@ -9,13 +11,12 @@ #define CM5121_14443B_BITPHASE 0xad #define CM5121_14443B_THRESHOLD 0xff - extern int PC_to_RDR_Escape(void *handle, const unsigned char *tx_buf, unsigned int tx_len, unsigned char *rx_buf, unsigned int *rx_len); -extern struct rfid_reader rfid_reader_cm5121; +extern const struct rfid_reader rfid_reader_cm5121; // extern struct rfid_asic_transport cm5121_ccid; #endif diff --git a/include/librfid/rfid_reader_openpcd.h b/include/librfid/rfid_reader_openpcd.h index e345132..49ee31c 100644 --- a/include/librfid/rfid_reader_openpcd.h +++ b/include/librfid/rfid_reader_openpcd.h @@ -92,6 +92,6 @@ enum openpcd_cmd_class { #define OPENPCD_IN_EP 0x82 #define OPENPCD_IRQ_EP 0x83 -extern struct rfid_reader rfid_reader_openpcd; +extern const struct rfid_reader rfid_reader_openpcd; #endif diff --git a/include/librfid/rfid_system.h b/include/librfid/rfid_system.h new file mode 100644 index 0000000..56441bb --- /dev/null +++ b/include/librfid/rfid_system.h @@ -0,0 +1,53 @@ +/* system / environment specific defines */ + +//#define LIBRFID_FIRMWARE + +#ifdef __LIBRFID__ + +#ifndef LIBRFID_FIRMWARE +/* If we're not doing a firmware compile, then we just use the regular + * malloc()/free() functions as expected */ + +#define malloc_asic_handle(x) malloc(x) +#define free_asic_handle(x) free(x) + +#define malloc_layer2_handle(x) malloc(x) +#define free_layer2_handle(x) free(x) + +#define malloc_protocol_handle(x) malloc(x) +#define free_protocol_handle(x) free(x) + +#define malloc_rat_handle(x) malloc(x) +#define free_rat_handle(x) free(x) + +#define malloc_reader_handle(x) malloc(x) +#define free_reader_handle(x) free(x) + +#else +/* If we're actually doing a firmware compile, then we use pre-allocated + * handles in order to avoid dynamic memory allocation. */ + +#define EMPTY_STATEMENT do {} while(0) +extern struct rfid_asic_handle rfid_ah; +#define malloc_asic_handle(x) &rfid_ah +#define free_asic_handle(x) EMPTY_STATEMENT + +extern struct rfid_layer2_handle rfid_l2h; +#define malloc_layer2_handle(x) &rfid_l2h +#define free_layer2_handle(x) EMPTY_STATEMENT + +extern struct rfid_protocol_handle rfid_ph; +#define malloc_protocol_handle(x) &rfid_ph +#define free_protocol_handle(x) EMPTY_STATEMENT + +extern struct rfid_asic_transport_handle rfid_ath; +#define malloc_rat_handle(x) &rfid_ath +#define free_rat_handle(x) EMPTY_STATEMENT + +extern struct rfid_reader_handle rfid_rh; +#define malloc_reader_handle(x) &rfid_rh +#define free_reader_handle(x) EMPTY_STATEMENT + +#endif /* LIBRFID_FIRMWARE */ + +#endif /* __LIBRFID__ */ diff --git a/src/rfid.c b/src/rfid.c index ba166a2..ec0ce1f 100644 --- a/src/rfid.c +++ b/src/rfid.c @@ -18,13 +18,19 @@ #include #include -#include -#include #include #include #include #include +#ifdef LIBRFID_FIRMWARE +struct rfid_asic_handle rfid_ah; +struct rfid_layer2_handle rfid_l2h; +struct rfid_protocol_handle rfid_ph; +struct rfid_asic_transport_handle rfid_ath; +struct rfid_reader_handle rfid_rh; +#endif + const char * rfid_hexdump(const void *data, unsigned int len) { @@ -92,6 +98,7 @@ int rfid_getopt(struct rfid_handle *rh, unsigned int level, int rfid_init() { +#if 0 rfid_reader_register(&rfid_reader_cm5121); rfid_reader_register(&rfid_reader_openpcd); rfid_layer2_register(&rfid_layer2_iso14443a); @@ -99,6 +106,7 @@ int rfid_init() rfid_protocol_register(&rfid_protocol_tcl); rfid_protocol_register(&rfid_protocol_mful); rfid_protocol_register(&rfid_protocol_mfcl); +#endif return 0; } diff --git a/src/rfid_asic_rc632.c b/src/rfid_asic_rc632.c index 05ed2cd..f8a8483 100644 --- a/src/rfid_asic_rc632.c +++ b/src/rfid_asic_rc632.c @@ -1,6 +1,6 @@ /* Generic Philips CL RC632 Routines * - * (C) Harald Welte + * (C) 2005-2006 Harald Welte * */ @@ -41,7 +41,7 @@ #define RC632_TMO_AUTH1 14000 #define ENTER() DEBUGP("entering\n") -struct rfid_asic rc632; +const struct rfid_asic rc632; /* Register and FIFO Access functions */ static int @@ -596,7 +596,7 @@ rc632_open(struct rfid_asic_transport_handle *th) { struct rfid_asic_handle *h; - h = malloc(sizeof(*h)); + h = malloc_asic_handle(sizeof(*h)); if (!h) return NULL; memset(h, 0, sizeof(*h)); @@ -609,7 +609,7 @@ rc632_open(struct rfid_asic_transport_handle *th) h->mtu = h->mru = 64; if (rc632_init(h) < 0) { - free(h); + free_asic_handle(h); return NULL; } @@ -620,14 +620,14 @@ void rc632_close(struct rfid_asic_handle *h) { rc632_fini(h); - free(h); + free_asic_handle(h); } /* * Philips CL RC632 primitives for ISO 14443-A compliant PICC's * - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte * */ @@ -1581,7 +1581,7 @@ rc632_mifare_transceive(struct rfid_asic_handle *handle, return 0; } -struct rfid_asic rc632 = { +const struct rfid_asic rc632 = { .name = "Philips CL RC632", .fc = ISO14443_FREQ_CARRIER, .priv.rc632 = { diff --git a/src/rfid_layer2.c b/src/rfid_layer2.c index a6ee43f..ff6d2b0 100644 --- a/src/rfid_layer2.c +++ b/src/rfid_layer2.c @@ -24,19 +24,24 @@ #include #include -static struct rfid_layer2 *rfid_layer2_list; +static const struct rfid_layer2 *rfid_layer2s[] = { + [RFID_LAYER2_ISO14443A] = &rfid_layer2_iso14443a, + [RFID_LAYER2_ISO14443B] = &rfid_layer2_iso14443b, + [RFID_LAYER2_ISO15693] = &rfid_layer2_iso15693, +}; struct rfid_layer2_handle * rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id) { struct rfid_layer2 *p; - for (p = rfid_layer2_list; p; p = p->next) - if (p->id == id) - return p->fn.init(rh); + if (id >= ARRAY_SIZE(rfid_layer2s)) { + DEBUGP("unable to find matching layer2 protocol\n"); + return NULL; + } - DEBUGP("unable to find matching layer2 protocol\n"); - return NULL; + p = rfid_layer2s[id]; + return p->fn.init(rh); } int @@ -79,15 +84,6 @@ rfid_layer2_close(struct rfid_layer2_handle *ph) return ph->l2->fn.close(ph); } -int -rfid_layer2_register(struct rfid_layer2 *p) -{ - p->next = rfid_layer2_list; - rfid_layer2_list = p; - - return 0; -} - int rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname, void *optval, unsigned int *optlen) diff --git a/src/rfid_layer2_iso14443a.c b/src/rfid_layer2_iso14443a.c index 7ebc240..34bd93f 100644 --- a/src/rfid_layer2_iso14443a.c +++ b/src/rfid_layer2_iso14443a.c @@ -287,7 +287,7 @@ static struct rfid_layer2_handle * iso14443a_init(struct rfid_reader_handle *rh) { int ret; - struct rfid_layer2_handle *h = malloc(sizeof(*h)); + struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h)); if (!h) return NULL; @@ -298,7 +298,7 @@ iso14443a_init(struct rfid_reader_handle *rh) ret = h->rh->reader->iso14443a.init(h->rh); if (ret < 0) { - free(h); + free_layer2_handle(h); return NULL; } @@ -308,12 +308,12 @@ iso14443a_init(struct rfid_reader_handle *rh) static int iso14443a_fini(struct rfid_layer2_handle *handle) { - free(handle); + free_layer2_handle(handle); return 0; } -struct rfid_layer2 rfid_layer2_iso14443a = { +const struct rfid_layer2 rfid_layer2_iso14443a = { .id = RFID_LAYER2_ISO14443A, .name = "ISO 14443-3 A", .fn = { diff --git a/src/rfid_layer2_iso14443b.c b/src/rfid_layer2_iso14443b.c index 8227a32..d88fee5 100644 --- a/src/rfid_layer2_iso14443b.c +++ b/src/rfid_layer2_iso14443b.c @@ -162,29 +162,24 @@ static int transceive_attrib(struct rfid_layer2_handle *h, const unsigned char *inf, unsigned int inf_len, unsigned char *rx_data, unsigned int *rx_len) { - struct iso14443b_attrib_hdr *attrib; - unsigned int attrib_size = sizeof(*attrib) + inf_len; - unsigned char *rx_buf; + struct { + struct iso14443b_attrib_hdr attrib; + char buf[256-3]; + } _attrib_buf; + + struct iso14443b_attrib_hdr *attrib = &_attrib_buf.attrib; + unsigned char rx_buf[256]; unsigned char fsdi; int ret = 0; DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd); - attrib = malloc(attrib_size); - if (!attrib) { - perror("attrib_alloc"); - return -1; - } - - DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd); - rx_buf = malloc(*rx_len+1); - if (!rx_buf) { - perror("rx_buf malloc"); - ret = -1; + if (rx_len >= rx_len-1) { + perror("rx_len too large\n"); goto out_attrib; } /* initialize attrib frame */ - memset(attrib, 0, attrib_size); + memset(&_attrib_buf, 0, sizeof(_attrib_buf)); if (inf_len) memcpy((unsigned char *)attrib+sizeof(*attrib), inf, inf_len); @@ -237,7 +232,6 @@ transceive_attrib(struct rfid_layer2_handle *h, const unsigned char *inf, out_rx: free(rx_buf); out_attrib: - free(attrib); return ret; } @@ -295,7 +289,7 @@ static struct rfid_layer2_handle * iso14443b_init(struct rfid_reader_handle *rh) { int ret; - struct rfid_layer2_handle *h = malloc(sizeof(*h)); + struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h)); if (!h) return NULL; @@ -317,7 +311,7 @@ iso14443b_init(struct rfid_reader_handle *rh) ret = h->rh->reader->iso14443b.init(h->rh); if (ret < 0) { DEBUGP("error during reader 14443b init\n"); - free(h); + free_layer2_handle(h); return NULL; } @@ -327,7 +321,7 @@ iso14443b_init(struct rfid_reader_handle *rh) static int iso14443b_fini(struct rfid_layer2_handle *handle) { - free(handle); + free_layer2_handle(handle); return 0; } @@ -394,7 +388,7 @@ iso14443b_setopt(struct rfid_layer2_handle *handle, } -struct rfid_layer2 rfid_layer2_iso14443b = { +const struct rfid_layer2 rfid_layer2_iso14443b = { .id = RFID_LAYER2_ISO14443B, .name = "ISO 14443-3 B", .fn = { diff --git a/src/rfid_layer2_iso15693.c b/src/rfid_layer2_iso15693.c index 8c5d808..8ab0b1e 100644 --- a/src/rfid_layer2_iso15693.c +++ b/src/rfid_layer2_iso15693.c @@ -1,6 +1,6 @@ /* ISO 15693 anticollision implementation * - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte * */ @@ -263,7 +263,7 @@ static struct rfid_layer2_handle * iso15693_init(struct rfid_reader_handle *rh) { int ret; - struct rfid_layer2_handle *h = malloc(sizeof(*h)); + struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h)); if (!h) return NULL; @@ -273,7 +273,7 @@ iso15693_init(struct rfid_reader_handle *rh) ret = h->rh->reader->iso15693.init(h->rh); if (ret < 0) { - free(h); + free_layer2_handle(h); return NULL; } @@ -283,12 +283,12 @@ iso15693_init(struct rfid_reader_handle *rh) static int iso15693_fini(struct rfid_layer2_handle *handle) { - free(handle); + free_layer2_handle(handle); return 0; } -struct rfid_layer2 rfid_layer2_iso15693 = { +const struct rfid_layer2 rfid_layer2_iso15693 = { .id = RFID_LAYER2_ISO15693, .name = "ISO 15693", .fn = { diff --git a/src/rfid_proto_mifare_classic.c b/src/rfid_proto_mifare_classic.c index 5d2b3ff..d45eefc 100644 --- a/src/rfid_proto_mifare_classic.c +++ b/src/rfid_proto_mifare_classic.c @@ -1,7 +1,7 @@ /* Mifare Classic implementation, PCD side. * - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte * */ @@ -110,17 +110,17 @@ static struct rfid_protocol_handle * mfcl_init(struct rfid_layer2_handle *l2h) { struct rfid_protocol_handle *ph; - ph = malloc(sizeof(struct rfid_protocol_handle)); + ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle)); return ph; } static int mfcl_fini(struct rfid_protocol_handle *ph) { - free(ph); + free_protocol_handle(ph); return 0; } -struct rfid_protocol rfid_protocol_mfcl = { +const struct rfid_protocol rfid_protocol_mfcl = { .id = RFID_PROTOCOL_MIFARE_CLASSIC, .name = "Mifare Classic", .fn = { diff --git a/src/rfid_proto_mifare_ul.c b/src/rfid_proto_mifare_ul.c index 5de050d..0ea55d2 100644 --- a/src/rfid_proto_mifare_ul.c +++ b/src/rfid_proto_mifare_ul.c @@ -1,7 +1,7 @@ /* Mifare Ultralight implementation, PCD side. * - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte * */ @@ -112,17 +112,17 @@ static struct rfid_protocol_handle * mful_init(struct rfid_layer2_handle *l2h) { struct rfid_protocol_handle *ph; - ph = malloc(sizeof(struct rfid_protocol_handle)); + ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle)); return ph; } static int mful_fini(struct rfid_protocol_handle *ph) { - free(ph); + free_protocol_handle(ph); return 0; } -struct rfid_protocol rfid_protocol_mful = { +const struct rfid_protocol rfid_protocol_mful = { .id = RFID_PROTOCOL_MIFARE_UL, .name = "Mifare Ultralight", .fn = { diff --git a/src/rfid_proto_tcl.c b/src/rfid_proto_tcl.c index 2ed7a7c..d00e966 100644 --- a/src/rfid_proto_tcl.c +++ b/src/rfid_proto_tcl.c @@ -1,6 +1,6 @@ /* ISO 14443-4 (T=CL) implementation, PCD side. * - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte * */ @@ -737,13 +737,13 @@ tcl_init(struct rfid_layer2_handle *l2h) struct rfid_protocol_handle *th; unsigned int mru = l2h->rh->ah->mru; - th = malloc(sizeof(struct rfid_protocol_handle) + mru); + th = malloc_protocol_handle(sizeof(struct rfid_protocol_handle)); if (!th) return NULL; /* FIXME: mru should be attribute of layer2 (in case it adds/removes * some overhead */ - memset(th, 0, sizeof(struct rfid_protocol_handle) + mru); + memset(th, 0, sizeof(struct rfid_protocol_handle)); /* maximum received ats length equals mru of asic/reader */ th->priv.tcl.state = TCL_STATE_INITIAL; @@ -758,11 +758,11 @@ tcl_init(struct rfid_layer2_handle *l2h) static int tcl_fini(struct rfid_protocol_handle *ph) { - free(ph); + free_protocol_handle(ph); return 0; } -struct rfid_protocol rfid_protocol_tcl = { +const struct rfid_protocol rfid_protocol_tcl = { .id = RFID_PROTOCOL_TCL, .name = "ISO 14443-4 / T=CL", .fn = { diff --git a/src/rfid_protocol.c b/src/rfid_protocol.c index b24243a..1a4262a 100644 --- a/src/rfid_protocol.c +++ b/src/rfid_protocol.c @@ -1,5 +1,5 @@ /* librfid - layer 3 protocol handler - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte */ /* @@ -24,7 +24,11 @@ #include #include -static struct rfid_protocol *rfid_protocol_list; +static const struct rfid_protocol *rfid_protocols[] = { + [RFID_PROTOCOL_MIFARE_CLASSIC] = &rfid_protocol_mfcl, + [RFID_PROTOCOL_MIFARE_UL] = &rfid_protocol_mful, + [RFID_PROTOCOL_TCL] = &rfid_protocol_tcl, +}; struct rfid_protocol_handle * rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id) @@ -32,13 +36,12 @@ rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id) struct rfid_protocol *p; struct rfid_protocol_handle *ph = NULL; - for (p = rfid_protocol_list; p; p = p->next) { - if (p->id == id) { - ph = p->fn.init(l2h); - break; - } - } + if (id >= ARRAY_SIZE(rfid_protocols)) + return NULL; + + p = rfid_protocols[id]; + ph = p->fn.init(l2h); if (!ph) return NULL; @@ -103,15 +106,6 @@ rfid_protocol_close(struct rfid_protocol_handle *ph) return 0; } -int -rfid_protocol_register(struct rfid_protocol *p) -{ - p->next = rfid_protocol_list; - rfid_protocol_list = p; - - return 0; -} - char *rfid_protocol_name(struct rfid_protocol_handle *ph) { return ph->proto->name; diff --git a/src/rfid_reader.c b/src/rfid_reader.c index f41ae8f..a5dbeb2 100644 --- a/src/rfid_reader.c +++ b/src/rfid_reader.c @@ -1,5 +1,5 @@ /* librfid - core reader handling - * (C) 2005 by Harald Welte + * (C) 2005-2006 by Harald Welte */ /* @@ -22,20 +22,29 @@ #include #include +#include +#include -static struct rfid_reader *rfid_reader_list; +static const struct rfid_reader *rfid_readers[] = { +#ifndef LIBRFID_FIRMWARE + [RFID_READER_CM5121] = &rfid_reader_cm5121, +#endif + [RFID_READER_OPENPCD] = &rfid_reader_openpcd, +}; struct rfid_reader_handle * rfid_reader_open(void *data, unsigned int id) { - struct rfid_reader *p; + const struct rfid_reader *p; - for (p = rfid_reader_list; p; p = p->next) - if (p->id == id) - return p->open(data); + if (id >= ARRAY_SIZE(rfid_readers)) { + DEBUGP("unable to find matching reader\n"); + return NULL; + } - DEBUGP("unable to find matching reader\n"); - return NULL; + p = rfid_readers[id]; + + return p->open(data); } int @@ -54,12 +63,3 @@ rfid_reader_close(struct rfid_reader_handle *rh) { rh->reader->close(rh); } - -int -rfid_reader_register(struct rfid_reader *r) -{ - r->next = rfid_reader_list; - rfid_reader_list = r; - - return 0; -} diff --git a/src/rfid_reader_cm5121.c b/src/rfid_reader_cm5121.c index a9907ad..dd15fd6 100644 --- a/src/rfid_reader_cm5121.c +++ b/src/rfid_reader_cm5121.c @@ -27,6 +27,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#ifndef LIBRFID_FIRMWARE + #include #include #include @@ -314,12 +316,12 @@ cm5121_open(void *data) struct rfid_reader_handle *rh; struct rfid_asic_transport_handle *rath; - rh = malloc(sizeof(*rh)); + rh = malloc_reader_handle(sizeof(*rh)); if (!rh) return NULL; memset(rh, 0, sizeof(*rh)); - rath = malloc(sizeof(*rath)); + rath = malloc_rat_handle(sizeof(*rath)); if (!rath) goto out_rh; memset(rath, 0, sizeof(*rath)); @@ -341,9 +343,9 @@ cm5121_open(void *data) return rh; out_rath: - free(rath); + free_rat_handle(rath); out_rh: - free(rh); + free_reader_handle(rh); return NULL; } @@ -353,11 +355,11 @@ cm5121_close(struct rfid_reader_handle *rh) { struct rfid_asic_transport_handle *rath = rh->ah->rath; rc632_close(rh->ah); - free(rath); - free(rh); + free_rat_handle(rath); + free_reader_handle(rh); } -struct rfid_reader rfid_reader_cm5121 = { +const struct rfid_reader rfid_reader_cm5121 = { .name = "Omnikey CardMan 5121 RFID", .open = &cm5121_open, .close = &cm5121_close, @@ -385,4 +387,4 @@ struct rfid_reader rfid_reader_cm5121 = { }, }; - +#endif /* LIBRFID_FIRMWARE */ diff --git a/src/rfid_reader_openpcd.c b/src/rfid_reader_openpcd.c index ca2cdff..15486b6 100644 --- a/src/rfid_reader_openpcd.c +++ b/src/rfid_reader_openpcd.c @@ -45,7 +45,6 @@ /* FIXME */ #include "rc632.h" - #define SENDBUF_LEN (256+4+10) /* 256bytes max FSD/FSC, plus 4 bytes header, plus 10 bytes reserve */ #define RECVBUF_LEN SENDBUF_LEN @@ -56,6 +55,8 @@ static struct openpcd_hdr *snd_hdr; static struct openpcd_hdr *rcv_hdr; +#ifndef LIBRFID_FIRMWARE + static struct usb_device *dev; static struct usb_dev_handle *hdl; @@ -129,6 +130,8 @@ static struct usb_device *find_opcd_device(void) return NULL; } +/* RC632 access primitives for librfid inside reader firmware */ + static int openpcd_reg_write(struct rfid_asic_transport_handle *rath, unsigned char reg, unsigned char value) { @@ -205,6 +208,60 @@ static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath, return ret; } +const struct rfid_asic_transport openpcd_rat = { + .name = "OpenPCD Dumb USB Protocol", + .priv.rc632 = { + .fn = { + .reg_write = &openpcd_reg_write, + .reg_read = &openpcd_reg_read, + .fifo_write = &openpcd_fifo_write, + .fifo_read = &openpcd_fifo_read, + }, + }, +}; + +#else +/* RC632 access primitives for librfid inside reader firmware */ + +static int openpcd_reg_write(struct rfid_asic_transport_handle *rath, + unsigned char reg, unsigned char value) +{ +} + +static int openpcd_reg_read(struct rfid_asic_transport_handle *rath, + unsigned char reg, + unsigned char *value) +{ +} + + +static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath, + unsigned char num_bytes, + unsigned char *buf) +{ +} + +static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath, + unsigned char len, + const unsigned char *bytes, + unsigned char flags) +{ +} + +const struct rfid_asic_transport openpcd_rat = { + .name = "OpenPCD Firmware RC632 Access", + .priv.rc632 = { + .fn = { + .reg_write = &openpcd_reg_write, + .reg_read = &openpcd_reg_read, + .fifo_write = &openpcd_fifo_write, + .fifo_read = &openpcd_fifo_read, + }, + }, +}; + +#endif /* LIBRFID_FIRMWARE */ + static int openpcd_transceive(struct rfid_reader_handle *rh, enum rfid_frametype frametype, const unsigned char *tx_data, unsigned int tx_len, @@ -299,18 +356,6 @@ openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, cmd, serno, block); } -struct rfid_asic_transport openpcd_ccid = { - .name = "OpenPCD Dumb USB Protocol", - .priv.rc632 = { - .fn = { - .reg_write = &openpcd_reg_write, - .reg_read = &openpcd_reg_read, - .fifo_write = &openpcd_fifo_write, - .fifo_read = &openpcd_fifo_read, - }, - }, -}; - static struct rfid_reader_handle * openpcd_open(void *data) { @@ -344,17 +389,17 @@ openpcd_open(void *data) return NULL; } - rh = malloc(sizeof(*rh)); + rh = malloc_reader_handle(sizeof(*rh)); if (!rh) return NULL; memset(rh, 0, sizeof(*rh)); - rath = malloc(sizeof(*rath)); + rath = malloc_rat_handle(sizeof(*rath)); if (!rath) goto out_rh; memset(rath, 0, sizeof(*rath)); - rath->rat = &openpcd_ccid; + rath->rat = &openpcd_rat; rh->reader = &rfid_reader_openpcd; rh->ah = rc632_open(rath); @@ -365,9 +410,9 @@ openpcd_open(void *data) return rh; out_rath: - free(rath); + free_rat_handle(rath); out_rh: - free(rh); + free_reader_handle(rh); return NULL; } @@ -378,13 +423,13 @@ openpcd_close(struct rfid_reader_handle *rh) struct rfid_asic_transport_handle *rath = rh->ah->rath; rc632_close(rh->ah); - free(rath); - free(rh); + free_rat_handle(rath); + free_reader_handle(rh); usb_close(hdl); } -struct rfid_reader rfid_reader_openpcd = { +const struct rfid_reader rfid_reader_openpcd = { .name = "OpenPCD RFID Reader", .id = RFID_READER_OPENPCD, .open = &openpcd_open, -- 2.20.1