From cac3cd6fcd941ae0906e5a95a1cb3b5ebec8a72a Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 10 Sep 2012 20:58:20 +0200 Subject: msgb: make msgb_get() finally work as expected --- include/osmocom/core/msgb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index 19e4a3d0..644a6391 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -224,7 +224,7 @@ static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) */ static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) { - unsigned char *tmp = msgb->data - len; + unsigned char *tmp = msgb->tail - len; if (msgb_length(msgb) < len) MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n", len, msgb_length(msgb)); -- cgit v1.2.3 From d54c2ee8c51b41b7f7a5a469efd6bb391a0c2b75 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 17 Jan 2012 18:25:50 +0100 Subject: initial checkin of 'libosmosim' --- include/Makefile.am | 3 +- include/osmocom/sim/sim.h | 268 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 include/osmocom/sim/sim.h (limited to 'include') diff --git a/include/Makefile.am b/include/Makefile.am index c59f9b21..149e29fa 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -83,7 +83,8 @@ nobase_include_HEADERS = \ osmocom/gsm/rsl.h \ osmocom/gsm/rxlev_stat.h \ osmocom/gsm/sysinfo.h \ - osmocom/gsm/tlv.h + osmocom/gsm/tlv.h \ + osmocom/sim/sim.h if ENABLE_PLUGIN nobase_include_HEADERS += osmocom/core/plugin.h diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h new file mode 100644 index 00000000..5ad1643e --- /dev/null +++ b/include/osmocom/sim/sim.h @@ -0,0 +1,268 @@ +#ifndef _OSMOCOM_SIM_H +#define _OSMOCOM_SIM_H + +#include +#include + +#define APDU_HDR_LEN 5 + +/* ISO 7816 / 5.3.1 / Figure 3 + Figure 4 */ +enum osim_apdu_case { + APDU_CASE_1, + APDU_CASE_2, + APDU_CASE_2_EXT, + APDU_CASE_3, + APDU_CASE_3_EXT, + APDU_CASE_4, + APDU_CASE_4_EXT +}; + +struct osim_apdu_cmd_hdr { + uint8_t cla; + uint8_t ins; + uint8_t p1; + uint8_t p2; + uint8_t p3; +} __attribute__ ((packed)); + +#define msgb_apdu_dr(__x) + +struct osim_msgb_cb { + enum osim_apdu_case apduc; + uint16_t lc; + uint16_t le; + uint16_t sw; +}; +#define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) +/*! \brief status word from msgb->cb */ +#define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc +#define msgb_apdu_lc(__x) OSIM_MSGB_CB(__x)->lc +#define msgb_apdu_le(__x) OSIM_MSGB_CB(__x)->le +#define msgb_apdu_sw(__x) OSIM_MSGB_CB(__x)->sw +/*! \brief pointer to the command header of the APDU */ +#define msgb_apdu_h(__x) ((struct osim_apdu_cmd_hdr *)(__x)->l2h) + +#define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr)) +#define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x)) + +struct osim_file; +struct osim_file_desc; +struct osim_decoded_data; + +struct osim_file_ops { + int (*parse)(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data); + struct msgb * (*encode)(const struct osim_file *file, + const struct osim_decoded_data *decoded); +}; + +enum osim_element_type { + ELEM_T_NONE, + ELEM_T_BOOL, /*!< a boolean flag */ + ELEM_T_UINT8, /*!< unsigned integer */ + ELEM_T_UINT16, /*!< unsigned integer */ + ELEM_T_UINT32, /*!< unsigned integer */ + ELEM_T_STRING, /*!< generic string */ + ELEM_T_BCD, /*!< BCD encoded digits */ + ELEM_T_BYTES, /*!< BCD encoded digits */ + ELEM_T_GROUP, /*!< group container, has siblings */ +}; + +enum osim_element_repr { + ELEM_REPR_NONE, + ELEM_REPR_DEC, + ELEM_REPR_HEX, +}; + +struct osim_decoded_element { + struct llist_head list; + + enum osim_element_type type; + enum osim_element_repr representation; + const char *name; + + unsigned int length; + union { + uint8_t u8; + uint16_t u16; + uint32_t u32; + uint8_t *buf; + struct llist_head siblings; + } u; +}; + +struct osim_decoded_data { + /*! file to which we belong */ + const struct osim_file *file; + /*! list of 'struct decoded_element' */ + struct llist_head decoded_elements; +}; + + +enum osim_file_type { + TYPE_NONE, + TYPE_DF, + TYPE_ADF, + TYPE_EF, + TYPE_EF_INT, +}; + +enum osim_ef_type { + EF_TYPE_TRANSP, + EF_TYPE_RECORD_FIXED, + EF_TYPE_RECORD_CYCLIC, +}; + +#define F_OPTIONAL 0x0001 + +struct osim_file_desc { + struct llist_head list; /*!< local element in list */ + struct llist_head child_list; /*!< list of children EF in DF */ + struct osim_file_desc *parent; /*!< parent DF */ + + enum osim_file_type type; + enum osim_ef_type ef_type; + + uint16_t fid; /*!< File Identifier */ + uint8_t sfid; /*!< Short File IDentifier */ + const char *df_name; + uint8_t df_name_len; + + const char *short_name; /*!< Short Name (like EF.ICCID) */ + const char *long_name; /*!< Long / description */ + unsigned int flags; + + struct osim_file_ops ops; +}; + +struct osim_file { + const struct osim_file_desc *desc; + + struct msgb *encoded_data; + struct osim_decoded_data *decoded_data; +}; + +#define EF(pfid, pns, pflags, pnl, ptype, pdec, penc) \ + { \ + .fid = pfid, \ + .type = TYPE_EF, \ + .ef_type = ptype, \ + .short_name = pns, \ + .long_name = pnl, \ + .flags = pflags, \ + .ops = { .encode = penc, .parse = pdec }, \ + } + + +#define EF_TRANSP(fid, ns, flags, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_TRANSP, dec, enc) +#define EF_TRANSP_N(fid, ns, flags, nl) \ + EF_TRANSP(fid, ns, flags, nl, &default_decode, NULL) + +#define EF_CYCLIC(fid, ns, flags, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, dec, enc) +#define EF_CYCLIC_N(fid, ns, flags, nl) \ + EF_CYCLIC(fid, ns, flags, nl, &default_decode, NULL) + +#define EF_LIN_FIX(fid, ns, flags, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_RECORD_FIXED, dec, enc) +#define EF_LIN_FIX_N(fid, ns, flags, nl) \ + EF_LIN_FIX(fid, ns, flags, nl, &default_decode, NULL) + +struct osim_file_desc * +osim_file_find_name(struct osim_file_desc *parent, const char *name); + +enum osim_card_sw_type { + SW_TYPE_NONE, + SW_TYPE_STR, +}; + +enum osim_card_sw_class { + SW_CLS_NONE, + SW_CLS_OK, + SW_CLS_POSTP, + SW_CLS_WARN, + SW_CLS_ERROR, +}; + +struct osim_card_sw { + uint16_t code; + uint16_t mask; + enum osim_card_sw_type type; + enum osim_card_sw_class class; + union { + const char *str; + } u; +}; + +#define OSIM_CARD_SW_LAST { \ + .code = 0, .mask = 0, .type = SW_TYPE_NONE, \ + .class = SW_CLS_NONE, .u.str = NULL \ +} + +struct osim_card_profile { + const char *name; + struct osim_file_desc *mf; + struct osim_card_sw **sws; +}; + +extern const struct tlv_definition ts102221_fcp_tlv_def; +const struct value_string ts102221_fcp_vals[14]; + +/* 11.1.1.3 */ +enum ts102221_fcp_tag { + UICC_FCP_T_FCP = 0x62, + UICC_FCP_T_FILE_SIZE = 0x80, + UICC_FCP_T_TOT_F_SIZE = 0x81, + UICC_FCP_T_FILE_DESC = 0x82, + UICC_FCP_T_FILE_ID = 0x83, + UICC_FCP_T_DF_NAME = 0x84, + UICC_FCP_T_SFID = 0x88, + UICC_FCP_T_LIFEC_STS = 0x8A, + UICC_FCP_T_SEC_ATTR_REFEXP= 0x8B, + UICC_FCP_T_SEC_ATTR_COMP= 0x8C, + UICC_FCP_T_PROPRIETARY = 0xA5, + UICC_FCP_T_SEC_ATTR_EXP = 0xAB, + UICC_FCP_T_PIN_STS_DO = 0xC6, +}; + +struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, + uint8_t p2, uint16_t lc, uint16_t le); + +struct osim_reader_ops; + +struct osim_reader_hdl { + /*! \brief member in global list of readers */ + struct llist_head list; + struct osim_reader_ops *ops; + void *priv; + /*! \brief current card, if any */ + struct osim_card_hdl *card; +}; + +struct osim_card_hdl { + /*! \brief member in global list of cards */ + struct llist_head list; + /*! \brief reader through which card is accessed */ + struct osim_reader_hdl *reader; + /*! \brief card profile */ + struct osim_card_profile *prof; + + /*! \brief list of channels for this card */ + struct llist_head channels; +}; + +struct osim_chan_hdl { + /*! \brief linked to card->channels */ + struct llist_head list; + /*! \brief card to which this channel belongs */ + struct osim_card_hdl *card; + const struct osim_file_desc *cwd; +}; + +/* reader.c */ +int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg); +struct osim_reader_hdl *osim_reader_open(int idx, const char *name); +struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh); +#endif /* _OSMOCOM_SIM_H */ -- cgit v1.2.3 From 6956ee1ba11aa2b7321e0d4fd41877184d9a3650 Mon Sep 17 00:00:00 2001 From: Kevin Redon Date: Tue, 11 Sep 2012 11:42:29 +0200 Subject: sim: add comment/explaination for structure osim_apdu_case --- include/osmocom/sim/sim.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 5ad1643e..35ff7b4c 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -6,15 +6,15 @@ #define APDU_HDR_LEN 5 -/* ISO 7816 / 5.3.1 / Figure 3 + Figure 4 */ +/* command-response pairs cases, as specified in ISO/IEC 7816-3:2006(E) §12.1 */ enum osim_apdu_case { - APDU_CASE_1, - APDU_CASE_2, - APDU_CASE_2_EXT, - APDU_CASE_3, - APDU_CASE_3_EXT, - APDU_CASE_4, - APDU_CASE_4_EXT + APDU_CASE_1, /* command header, no command data field, no response data field */ + APDU_CASE_2, /* command header, no command data field, response data field (short) */ + APDU_CASE_2_EXT, /* command header, no command data field, response data field (extended) */ + APDU_CASE_3, /* command header, command data field (short), no response data field */ + APDU_CASE_3_EXT, /* command header, command data field (extended), no response data field */ + APDU_CASE_4, /* command header, command data field (short), response data field (short) */ + APDU_CASE_4_EXT /* command header, command data field (extended), response data field (extended) */ }; struct osim_apdu_cmd_hdr { -- cgit v1.2.3 From e07967f689c56473a8492ec422ee38964d00c9c3 Mon Sep 17 00:00:00 2001 From: Kevin Redon Date: Tue, 11 Sep 2012 11:44:18 +0200 Subject: sim: renamed case to the one defined in ISO7816-3 APDU_CASE_2 becomes APDU_CASE_2S APDU_CASE_2_EXT becmoes APDU_CASE_2E APDU_CASE_3 becomes APDU_CASE_3S APDU_CASE_3_EXT becmoes APDU_CASE_3E APDU_CASE_4 becomes APDU_CASE_4S APDU_CASE_4_EXT becmoes APDU_CASE_4E --- include/osmocom/sim/sim.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 35ff7b4c..32731939 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -9,12 +9,12 @@ /* command-response pairs cases, as specified in ISO/IEC 7816-3:2006(E) §12.1 */ enum osim_apdu_case { APDU_CASE_1, /* command header, no command data field, no response data field */ - APDU_CASE_2, /* command header, no command data field, response data field (short) */ - APDU_CASE_2_EXT, /* command header, no command data field, response data field (extended) */ - APDU_CASE_3, /* command header, command data field (short), no response data field */ - APDU_CASE_3_EXT, /* command header, command data field (extended), no response data field */ - APDU_CASE_4, /* command header, command data field (short), response data field (short) */ - APDU_CASE_4_EXT /* command header, command data field (extended), response data field (extended) */ + APDU_CASE_2S, /* command header, no command data field, response data field (short) */ + APDU_CASE_2E, /* command header, no command data field, response data field (extended) */ + APDU_CASE_3S, /* command header, command data field (short), no response data field */ + APDU_CASE_3E, /* command header, command data field (extended), no response data field */ + APDU_CASE_4S, /* command header, command data field (short), response data field (short) */ + APDU_CASE_4E /* command header, command data field (extended), response data field (extended) */ }; struct osim_apdu_cmd_hdr { -- cgit v1.2.3 From 1707306da59a53c8ce72952fc5e41b999e36b73c Mon Sep 17 00:00:00 2001 From: Kevin Redon Date: Tue, 11 Sep 2012 11:54:52 +0200 Subject: sim: add comment/explaination for structure osim_apdu_cmd_hdr --- include/osmocom/sim/sim.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 32731939..12f0074d 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -17,12 +17,13 @@ enum osim_apdu_case { APDU_CASE_4E /* command header, command data field (extended), response data field (extended) */ }; +/* command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 */ struct osim_apdu_cmd_hdr { - uint8_t cla; - uint8_t ins; - uint8_t p1; - uint8_t p2; - uint8_t p3; + uint8_t cla; /* CLASS byte */ + uint8_t ins; /* INSTRUCTION byte */ + uint8_t p1; /* Parameter 1 byte */ + uint8_t p2; /* Parameter 2 byte */ + uint8_t p3; /* Parameter 3 byte, used for TPDU */ } __attribute__ ((packed)); #define msgb_apdu_dr(__x) -- cgit v1.2.3 From dede831fcd5ccc67ad4d0e05aaa9688c119a5b93 Mon Sep 17 00:00:00 2001 From: Kevin Redon Date: Tue, 11 Sep 2012 11:59:21 +0200 Subject: sim: add comment/explaination for structure osim_msgb_cb --- include/osmocom/sim/sim.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 12f0074d..e415843e 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -28,11 +28,12 @@ struct osim_apdu_cmd_hdr { #define msgb_apdu_dr(__x) +/* command body, as specified in ISO/IEC 7816-3:2006(E) §12.1 */ struct osim_msgb_cb { - enum osim_apdu_case apduc; - uint16_t lc; - uint16_t le; - uint16_t sw; + enum osim_apdu_case apduc; /* command-response pairs case, defining the encoding of Lc and Le */ + uint16_t lc; /* number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc */ + uint16_t le; /* maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le */ + uint16_t sw; /* status word, composed of SW1 and SW2 bytes */ }; #define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) /*! \brief status word from msgb->cb */ -- cgit v1.2.3 From 43eabeeeaa993b6628e0490fbf6b78f24cee4788 Mon Sep 17 00:00:00 2001 From: Kevin Redon Date: Sun, 16 Sep 2012 18:40:02 +0200 Subject: sim: replaced function/structure comments with doxygen comments affected files: sim.h, sim/core.c affected funtions/strucutres: osim_new_apdumsg, osim_apdu_case,osim_apdu_cmd_hdr, osim_msgb_cb --- include/osmocom/sim/sim.h | 58 +++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index e415843e..6ead7ffb 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -6,34 +6,54 @@ #define APDU_HDR_LEN 5 -/* command-response pairs cases, as specified in ISO/IEC 7816-3:2006(E) §12.1 */ +/*! + * \file sim.h + * \brief Routines for helping with SIM (ISO/IEC 7816-4 more generally) communication. + */ + +/*! \brief command-response pairs cases + * + * Enumeration used to identify the APDU structure based on command-response pair case , as specified in ISO/IEC 7816-3:2006(E) §12.1. + */ enum osim_apdu_case { - APDU_CASE_1, /* command header, no command data field, no response data field */ - APDU_CASE_2S, /* command header, no command data field, response data field (short) */ - APDU_CASE_2E, /* command header, no command data field, response data field (extended) */ - APDU_CASE_3S, /* command header, command data field (short), no response data field */ - APDU_CASE_3E, /* command header, command data field (extended), no response data field */ - APDU_CASE_4S, /* command header, command data field (short), response data field (short) */ - APDU_CASE_4E /* command header, command data field (extended), response data field (extended) */ + APDU_CASE_1, /*!< command header, no command data field, no response data field */ + APDU_CASE_2S, /*!< command header, no command data field, response data field (short) */ + APDU_CASE_2E, /*!< command header, no command data field, response data field (extended) */ + APDU_CASE_3S, /*!< command header, command data field (short), no response data field */ + APDU_CASE_3E, /*!< command header, command data field (extended), no response data field */ + APDU_CASE_4S, /*!< command header, command data field (short), response data field (short) */ + APDU_CASE_4E /*!< command header, command data field (extended), response data field (extended) */ }; -/* command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 */ +/*! \brief APDU/TPDU command header + * + * This structure encode an APDU/TPDU command header, as specified in ISO/IEC 7816-3:2006(E) §12.2 and §12.3. + * The APDU (application layer) can be encoded as different TPDUs (transport layer), depending on the transport protocol used. + * The TPDU encoding by T=1 of the APDU command header is identical to the APDU. + * The TPDU encoding by T=0 of the APDU command header adds a Parameter 3 field, generally used instead of Lc/Le. + * + * @todo have different structures for APDU, TPDU by T=0, and TPDU by T=1. + */ struct osim_apdu_cmd_hdr { - uint8_t cla; /* CLASS byte */ - uint8_t ins; /* INSTRUCTION byte */ - uint8_t p1; /* Parameter 1 byte */ - uint8_t p2; /* Parameter 2 byte */ - uint8_t p3; /* Parameter 3 byte, used for TPDU */ + uint8_t cla; /*!< CLASS byte */ + uint8_t ins; /*!< INSTRUCTION byte */ + uint8_t p1; /*!< Parameter 1 byte */ + uint8_t p2; /*!< Parameter 2 byte */ + uint8_t p3; /*!< Parameter 3 byte, used for TPDU by T=0 */ } __attribute__ ((packed)); #define msgb_apdu_dr(__x) -/* command body, as specified in ISO/IEC 7816-3:2006(E) §12.1 */ +/*! \brief APDU command body + * + * This structure encode a command body, as specified in ISO/IEC 7816-3:2006(E) §12.1. + * The data and response contents should be provided along with this structure. + */ struct osim_msgb_cb { - enum osim_apdu_case apduc; /* command-response pairs case, defining the encoding of Lc and Le */ - uint16_t lc; /* number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc */ - uint16_t le; /* maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le */ - uint16_t sw; /* status word, composed of SW1 and SW2 bytes */ + enum osim_apdu_case apduc; /*!< command-response pair case, defining the encoding of Lc and Le */ + uint16_t lc; /*!< number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc, depending on the case */ + uint16_t le; /*!< maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le, depending on the case */ + uint16_t sw; /*!< status word, composed of SW1 and SW2 bytes */ }; #define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) /*! \brief status word from msgb->cb */ -- cgit v1.2.3 From 7674960ffadd5d7cb1a58ecee77252fe19e1e86f Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 19 Sep 2012 20:55:54 +0200 Subject: sim: add decoding of status words --- include/osmocom/sim/sim.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 6ead7ffb..1b136938 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -218,7 +218,7 @@ struct osim_card_sw { } u; }; -#define OSIM_CARD_SW_LAST { \ +#define OSIM_CARD_SW_LAST (const struct osim_card_sw) { \ .code = 0, .mask = 0, .type = SW_TYPE_NONE, \ .class = SW_CLS_NONE, .u.str = NULL \ } @@ -226,9 +226,15 @@ struct osim_card_sw { struct osim_card_profile { const char *name; struct osim_file_desc *mf; - struct osim_card_sw **sws; + const struct osim_card_sw **sws; }; +const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp, + uint16_t sw); + +struct osim_card_hdl; +char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in); + extern const struct tlv_definition ts102221_fcp_tlv_def; const struct value_string ts102221_fcp_vals[14]; -- cgit v1.2.3 From d83d29610a7c63a387fee77dbc2b18b19dc83b14 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 4 Mar 2013 17:52:33 +0000 Subject: sim: further updates/fixes --- include/osmocom/sim/sim.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 1b136938..92b3b5a6 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -231,6 +231,8 @@ struct osim_card_profile { const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp, uint16_t sw); +enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp, + uint16_t sw_in); struct osim_card_hdl; char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in); @@ -291,6 +293,6 @@ struct osim_chan_hdl { /* reader.c */ int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg); -struct osim_reader_hdl *osim_reader_open(int idx, const char *name); +struct osim_reader_hdl *osim_reader_open(int idx, const char *name, void *ctx); struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh); #endif /* _OSMOCOM_SIM_H */ -- cgit v1.2.3 From 1e0dfdad69fcb48d8bc134be4ddabc72d5dfc9e3 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 3 May 2014 14:22:12 +0200 Subject: sim: Add minimal/recommended size of file / record --- include/osmocom/sim/sim.h | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 92b3b5a6..7549df64 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -156,6 +156,13 @@ struct osim_file_desc { unsigned int flags; struct osim_file_ops ops; + + struct { + size_t min; /*!< Minimum size of the file + (transparent) or record in + cyclic / linear file */ + size_t rec; /*!< Recommended size */ + } size; }; struct osim_file { @@ -165,7 +172,7 @@ struct osim_file { struct osim_decoded_data *decoded_data; }; -#define EF(pfid, pns, pflags, pnl, ptype, pdec, penc) \ +#define EF(pfid, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \ { \ .fid = pfid, \ .type = TYPE_EF, \ @@ -174,23 +181,30 @@ struct osim_file { .long_name = pnl, \ .flags = pflags, \ .ops = { .encode = penc, .parse = pdec }, \ + .size = { .min = smin, .rec = srec}, \ } -#define EF_TRANSP(fid, ns, flags, nl, dec, enc) \ - EF(fid, ns, flags, nl, EF_TYPE_TRANSP, dec, enc) -#define EF_TRANSP_N(fid, ns, flags, nl) \ - EF_TRANSP(fid, ns, flags, nl, &default_decode, NULL) - -#define EF_CYCLIC(fid, ns, flags, nl, dec, enc) \ - EF(fid, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, dec, enc) -#define EF_CYCLIC_N(fid, ns, flags, nl) \ - EF_CYCLIC(fid, ns, flags, nl, &default_decode, NULL) - -#define EF_LIN_FIX(fid, ns, flags, nl, dec, enc) \ - EF(fid, ns, flags, nl, EF_TYPE_RECORD_FIXED, dec, enc) -#define EF_LIN_FIX_N(fid, ns, flags, nl) \ - EF_LIN_FIX(fid, ns, flags, nl, &default_decode, NULL) +#define EF_TRANSP(fid, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_TRANSP, \ + smin, srec, dec, enc) +#define EF_TRANSP_N(fid, ns, flags, smin, srec, nl) \ + EF_TRANSP(fid, ns, flags, smin, srec, \ + nl, &default_decode, NULL) + +#define EF_CYCLIC(fid, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \ + smin, srec, dec, enc) +#define EF_CYCLIC_N(fid, ns, flags, smin, srec, nl) \ + EF_CYCLIC(fid, ns, flags, smin, srec, nl, \ + &default_decode, NULL) + +#define EF_LIN_FIX(fid, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_RECORD_FIXED, \ + smin, srec, dec, enc) +#define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \ + EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \ + &default_decode, NULL) struct osim_file_desc * osim_file_find_name(struct osim_file_desc *parent, const char *name); -- cgit v1.2.3 From 2656e65bed12680d288c55dbe53e1d6c92bdfdc5 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 3 May 2014 14:23:44 +0200 Subject: sim: Add sfi to EF_*() macros --- include/osmocom/sim/sim.h | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 7549df64..e1e9997e 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -138,6 +138,8 @@ enum osim_ef_type { #define F_OPTIONAL 0x0001 +#define SFI_NONE 0xFF + struct osim_file_desc { struct llist_head list; /*!< local element in list */ struct llist_head child_list; /*!< list of children EF in DF */ @@ -172,9 +174,10 @@ struct osim_file { struct osim_decoded_data *decoded_data; }; -#define EF(pfid, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \ +#define EF(pfid, sfi, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \ { \ .fid = pfid, \ + .sfid = sfi, \ .type = TYPE_EF, \ .ef_type = ptype, \ .short_name = pns, \ @@ -185,22 +188,22 @@ struct osim_file { } -#define EF_TRANSP(fid, ns, flags, smin, srec, nl, dec, enc) \ - EF(fid, ns, flags, nl, EF_TYPE_TRANSP, \ +#define EF_TRANSP(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, sfi, ns, flags, nl, EF_TYPE_TRANSP, \ smin, srec, dec, enc) -#define EF_TRANSP_N(fid, ns, flags, smin, srec, nl) \ - EF_TRANSP(fid, ns, flags, smin, srec, \ +#define EF_TRANSP_N(fid, sfi, ns, flags, smin, srec, nl) \ + EF_TRANSP(fid, sfi, ns, flags, smin, srec, \ nl, &default_decode, NULL) -#define EF_CYCLIC(fid, ns, flags, smin, srec, nl, dec, enc) \ - EF(fid, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \ +#define EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \ smin, srec, dec, enc) -#define EF_CYCLIC_N(fid, ns, flags, smin, srec, nl) \ - EF_CYCLIC(fid, ns, flags, smin, srec, nl, \ +#define EF_CYCLIC_N(fid, sfi, ns, flags, smin, srec, nl) \ + EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, \ &default_decode, NULL) -#define EF_LIN_FIX(fid, ns, flags, smin, srec, nl, dec, enc) \ - EF(fid, ns, flags, nl, EF_TYPE_RECORD_FIXED, \ +#define EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_FIXED, \ smin, srec, dec, enc) #define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \ EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \ -- cgit v1.2.3 From e8dd2bd4b52f4ee6c3ecb4a5b4b4386d281b35a7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 4 May 2014 13:42:38 +0200 Subject: sim: mark osim_msgb_cb as __may_alias__ to avoid gcc warnings --- include/osmocom/sim/sim.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index e1e9997e..4e63905e 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -54,7 +54,7 @@ struct osim_msgb_cb { uint16_t lc; /*!< number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc, depending on the case */ uint16_t le; /*!< maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le, depending on the case */ uint16_t sw; /*!< status word, composed of SW1 and SW2 bytes */ -}; +} __attribute__((__may_alias__)); #define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) /*! \brief status word from msgb->cb */ #define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc -- cgit v1.2.3 From aad7e0664b310ccc463538a18ac5238cb7c0ac3c Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 4 May 2014 16:31:24 +0200 Subject: sim: Make TETRA SIM (TSIM) support compile --- include/osmocom/sim/sim.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 4e63905e..0c6352c5 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -134,6 +134,7 @@ enum osim_ef_type { EF_TYPE_TRANSP, EF_TYPE_RECORD_FIXED, EF_TYPE_RECORD_CYCLIC, + EF_TYPE_KEY, /* TETRA */ }; #define F_OPTIONAL 0x0001 @@ -209,6 +210,14 @@ struct osim_file { EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \ &default_decode, NULL) +#define EF_KEY(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ + EF(fid, sfi, ns, flags, nl, EF_TYPE_KEY, \ + smin, srec, dec, enc) +#define EF_KEY_N(fid, sfi, ns, flags, smin, srec, nl) \ + EF_KEY(fid, sfi, ns, flags, smin, srec, nl, \ + &default_decode, NULL) + + struct osim_file_desc * osim_file_find_name(struct osim_file_desc *parent, const char *name); -- cgit v1.2.3 From 586d710e0586a51ba19b71bf20f2bb12aa8c0aeb Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 4 May 2014 17:01:16 +0200 Subject: sim: More comments in sim.h --- include/osmocom/sim/sim.h | 56 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 0c6352c5..4c47afc4 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -67,14 +67,19 @@ struct osim_msgb_cb { #define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr)) #define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x)) +/* FILES */ + struct osim_file; struct osim_file_desc; struct osim_decoded_data; +/*! \brief Operations for a given File */ struct osim_file_ops { + /*! Parse binary file data into osim_decoded_data */ int (*parse)(struct osim_decoded_data *dd, const struct osim_file_desc *desc, int len, uint8_t *data); + /*! Encode osim_decoded_data into binary file */ struct msgb * (*encode)(const struct osim_file *file, const struct osim_decoded_data *decoded); }; @@ -97,6 +102,7 @@ enum osim_element_repr { ELEM_REPR_HEX, }; +/*! \brief A single decoded element inside a file */ struct osim_decoded_element { struct llist_head list; @@ -110,10 +116,12 @@ struct osim_decoded_element { uint16_t u16; uint32_t u32; uint8_t *buf; + /*! A list of sibling decoded_items */ struct llist_head siblings; } u; }; +/*! Decoded data for a single file, consisting of all decoded elements */ struct osim_decoded_data { /*! file to which we belong */ const struct osim_file *file; @@ -124,17 +132,17 @@ struct osim_decoded_data { enum osim_file_type { TYPE_NONE, - TYPE_DF, - TYPE_ADF, - TYPE_EF, - TYPE_EF_INT, + TYPE_DF, /*!< Dedicated File */ + TYPE_ADF, /*!< Application Dedicated File */ + TYPE_EF, /*!< Entry File */ + TYPE_EF_INT, /*!< Internal Entry File */ }; enum osim_ef_type { - EF_TYPE_TRANSP, - EF_TYPE_RECORD_FIXED, - EF_TYPE_RECORD_CYCLIC, - EF_TYPE_KEY, /* TETRA */ + EF_TYPE_TRANSP, /*!< Transparent EF */ + EF_TYPE_RECORD_FIXED, /*!< Fixed-Size Record EF */ + EF_TYPE_RECORD_CYCLIC, /*!< Cyclic Record EF */ + EF_TYPE_KEY, /*!< Key file as used in TETRA */ }; #define F_OPTIONAL 0x0001 @@ -146,19 +154,19 @@ struct osim_file_desc { struct llist_head child_list; /*!< list of children EF in DF */ struct osim_file_desc *parent; /*!< parent DF */ - enum osim_file_type type; - enum osim_ef_type ef_type; + enum osim_file_type type; /*!< Type of the file (EF, DF, ...) */ + enum osim_ef_type ef_type; /*!< Type of the EF, if type == TYPE_EF */ uint16_t fid; /*!< File Identifier */ uint8_t sfid; /*!< Short File IDentifier */ - const char *df_name; + const char *df_name; uint8_t df_name_len; const char *short_name; /*!< Short Name (like EF.ICCID) */ const char *long_name; /*!< Long / description */ unsigned int flags; - struct osim_file_ops ops; + struct osim_file_ops ops; /*!< Operations (parse/encode */ struct { size_t min; /*!< Minimum size of the file @@ -168,13 +176,18 @@ struct osim_file_desc { } size; }; +/*! \brief A single instance of a file: Descriptor and contents */ struct osim_file { + /*! Descriptor for the file */ const struct osim_file_desc *desc; + /*! Encoded file contents */ struct msgb *encoded_data; + /*! Parsed/Decoded file contents */ struct osim_decoded_data *decoded_data; }; +/*! Convenience macros for defining EF */ #define EF(pfid, sfi, pns, pflags, pnl, ptype, smin, srec, pdec, penc) \ { \ .fid = pfid, \ @@ -189,30 +202,38 @@ struct osim_file { } +/*! Convenience macros for defining EF */ #define EF_TRANSP(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ EF(fid, sfi, ns, flags, nl, EF_TYPE_TRANSP, \ smin, srec, dec, enc) +/*! Convenience macros for defining EF */ #define EF_TRANSP_N(fid, sfi, ns, flags, smin, srec, nl) \ EF_TRANSP(fid, sfi, ns, flags, smin, srec, \ nl, &default_decode, NULL) +/*! Convenience macros for defining EF */ #define EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, \ smin, srec, dec, enc) +/*! Convenience macros for defining EF */ #define EF_CYCLIC_N(fid, sfi, ns, flags, smin, srec, nl) \ EF_CYCLIC(fid, sfi, ns, flags, smin, srec, nl, \ &default_decode, NULL) +/*! Convenience macros for defining EF */ #define EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ EF(fid, sfi, ns, flags, nl, EF_TYPE_RECORD_FIXED, \ smin, srec, dec, enc) +/*! Convenience macros for defining EF */ #define EF_LIN_FIX_N(fid, sfi, ns, flags, smin, srec, nl) \ EF_LIN_FIX(fid, sfi, ns, flags, smin, srec, nl, \ &default_decode, NULL) +/*! Convenience macros for defining EF */ #define EF_KEY(fid, sfi, ns, flags, smin, srec, nl, dec, enc) \ EF(fid, sfi, ns, flags, nl, EF_TYPE_KEY, \ smin, srec, dec, enc) +/*! Convenience macros for defining EF */ #define EF_KEY_N(fid, sfi, ns, flags, smin, srec, nl) \ EF_KEY(fid, sfi, ns, flags, smin, srec, nl, \ &default_decode, NULL) @@ -221,6 +242,8 @@ struct osim_file { struct osim_file_desc * osim_file_find_name(struct osim_file_desc *parent, const char *name); +/* STATUS WORDS */ + enum osim_card_sw_type { SW_TYPE_NONE, SW_TYPE_STR, @@ -234,12 +257,16 @@ enum osim_card_sw_class { SW_CLS_ERROR, }; +/*! A card status word (SW) */ struct osim_card_sw { + /*! status word code (2 bytes) */ uint16_t code; + /*! status word mask (2 bytes), to match range/prefix of SW */ uint16_t mask; enum osim_card_sw_type type; enum osim_card_sw_class class; union { + /*! Human-readable meaning of SW */ const char *str; } u; }; @@ -249,9 +276,12 @@ struct osim_card_sw { .class = SW_CLS_NONE, .u.str = NULL \ } +/*! \brief A card profile (e.g. SIM card */ struct osim_card_profile { const char *name; + /*! Descriptor for the MF (root directory */ struct osim_file_desc *mf; + /*! Array of pointers to status words */ const struct osim_card_sw **sws; }; @@ -286,6 +316,8 @@ enum ts102221_fcp_tag { struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, uint16_t lc, uint16_t le); +/* CARD READERS */ + struct osim_reader_ops; struct osim_reader_hdl { -- cgit v1.2.3 From 55790aa09a8f92d437ea06b3ef2c74465612fa8b Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Oct 2014 18:46:50 +0100 Subject: sim: Prepare infrastructure for protocols != T=0 and other drivers --- include/osmocom/sim/sim.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 4c47afc4..d68d7c2c 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -320,10 +320,22 @@ struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, struct osim_reader_ops; +enum osim_proto { + OSIM_PROTO_T0 = 0, + OSIM_PROTO_T1 = 1, +}; + +enum osim_reader_driver { + OSIM_READER_DRV_PCSC = 0, + OSIM_READER_DRV_OPENCT = 1, + OSIM_READER_DRV_SERIAL = 2, +}; + struct osim_reader_hdl { /*! \brief member in global list of readers */ struct llist_head list; struct osim_reader_ops *ops; + uint32_t proto_supported; void *priv; /*! \brief current card, if any */ struct osim_card_hdl *card; @@ -336,6 +348,8 @@ struct osim_card_hdl { struct osim_reader_hdl *reader; /*! \brief card profile */ struct osim_card_profile *prof; + /*! \brief card protocol */ + enum osim_proto proto; /*! \brief list of channels for this card */ struct llist_head channels; @@ -351,6 +365,7 @@ struct osim_chan_hdl { /* reader.c */ int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg); -struct osim_reader_hdl *osim_reader_open(int idx, const char *name, void *ctx); -struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh); +struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver drv, int idx, + const char *name, void *ctx); +struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto); #endif /* _OSMOCOM_SIM_H */ -- cgit v1.2.3 From 67354b1b4ee412a8e878a53607fcd65f2b344948 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Oct 2014 18:47:30 +0100 Subject: sim: add missing const to reader_ops member of reader_handle --- include/osmocom/sim/sim.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index d68d7c2c..80d1fcd1 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -334,7 +334,7 @@ enum osim_reader_driver { struct osim_reader_hdl { /*! \brief member in global list of readers */ struct llist_head list; - struct osim_reader_ops *ops; + const struct osim_reader_ops *ops; uint32_t proto_supported; void *priv; /*! \brief current card, if any */ -- cgit v1.2.3 From 6729a9776aeffde08b11342dfc53e33873a33594 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 26 Oct 2014 19:04:56 +0100 Subject: make sure to register DF.TELECOM and DF.GSM for SIM, USIM + TSIM before we only did it partially, and by exporting data from sim, rather than the new osim_int_cprof_add_{gsm,telecom}() functions. --- include/osmocom/sim/sim.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h index 80d1fcd1..02cdcad5 100644 --- a/include/osmocom/sim/sim.h +++ b/include/osmocom/sim/sim.h @@ -80,7 +80,7 @@ struct osim_file_ops { const struct osim_file_desc *desc, int len, uint8_t *data); /*! Encode osim_decoded_data into binary file */ - struct msgb * (*encode)(const struct osim_file *file, + struct msgb * (*encode)(const struct osim_file_desc *desc, const struct osim_decoded_data *decoded); }; @@ -159,7 +159,7 @@ struct osim_file_desc { uint16_t fid; /*!< File Identifier */ uint8_t sfid; /*!< Short File IDentifier */ - const char *df_name; + const uint8_t *df_name; uint8_t df_name_len; const char *short_name; /*!< Short Name (like EF.ICCID) */ -- cgit v1.2.3