Merge branch 'laforge/sim'

This contains some work from 2012 about creating a general library about
the data structure of data contained on SIM cards.  This 'libosmosim' is
supposed to be used by (among others)
* tools to review/modify/personalize SIM card filesystem data
* virtual SIM card implementation
diff --git a/.gitignore b/.gitignore
index 2bbb97b..0930491 100644
--- a/.gitignore
+++ b/.gitignore
@@ -82,6 +82,7 @@
 
 utils/osmo-arfcn
 utils/osmo-auc-gen
+utils/osmo-sim-test
 
 doc/codec
 doc/core
diff --git a/Makefile.am b/Makefile.am
index faf7a83..dc9dc37 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,7 @@
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS = include src src/vty src/codec src/gsm src/gb src/ctrl tests utils
+AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
+SUBDIRS = include src src/vty src/codec src/gsm src/gb src/ctrl src/sim tests utils
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libosmocore.pc libosmocodec.pc libosmovty.pc libosmogsm.pc \
@@ -49,7 +50,7 @@
 uninstall-hook:
 	cd $(DESTDIR)$(htmldir) && rm -rf {core,gsm,vty,codec}
 
-DX_CLEAN = doc/{core,gsm,vty,codec}/{html,latex}/* doc/html.tar
+DX_CLEAN = doc/{core,gsm,vty,codec}/{html,latex}/* doc/html.tar doc/{core,gsm,vty,codec}/doxygen_sqlite3.db
 endif
 
 MOSTLYCLEANFILES = $(DX_CLEAN)
diff --git a/configure.ac b/configure.ac
index deaa8bf..80e2bcc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -82,6 +82,16 @@
 dnl Generate the output
 AC_CONFIG_HEADER(config.h)
 
+AC_ARG_ENABLE([pcsc], [AS_HELP_STRING([--disable-pcsc], [Build without PC/SC support])],
+    [
+        osmo_ac_have_pcsc=$enableval
+    ],
+    [
+        PKG_CHECK_MODULES(PCSC, libpcsclite)
+        osmo_ac_have_pcsc="yes"
+    ])
+AM_CONDITIONAL(ENABLE_PCSC, test "x$osmo_ac_have_pcsc" = "xyes")
+
 AC_ARG_ENABLE(talloc,
 	[AS_HELP_STRING(
 		[--disable-talloc],
@@ -187,10 +197,12 @@
 	libosmogsm.pc
 	libosmogb.pc
 	libosmoctrl.pc
+	libosmosim.pc
 	include/Makefile
 	src/Makefile
 	src/vty/Makefile
 	src/codec/Makefile
+	src/sim/Makefile
 	src/gsm/Makefile
 	src/gb/Makefile
 	src/ctrl/Makefile
diff --git a/include/Makefile.am b/include/Makefile.am
index c59f9b2..149e29f 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -83,7 +83,8 @@
                        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/core/msgb.h b/include/osmocom/core/msgb.h
index 19e4a3d..644a639 100644
--- a/include/osmocom/core/msgb.h
+++ b/include/osmocom/core/msgb.h
@@ -224,7 +224,7 @@
  */
 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));
diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h
new file mode 100644
index 0000000..02cdcad
--- /dev/null
+++ b/include/osmocom/sim/sim.h
@@ -0,0 +1,371 @@
+#ifndef _OSMOCOM_SIM_H
+#define _OSMOCOM_SIM_H
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/linuxlist.h>
+
+#define APDU_HDR_LEN	5
+
+/*!
+ * \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) */
+};
+
+/*! \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 by T=0 */
+} __attribute__ ((packed));
+
+#define msgb_apdu_dr(__x)
+
+/*! \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 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 */
+} __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
+#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))
+
+/* 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_desc *desc,
+				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,
+};
+
+/*! \brief A single decoded element inside a file */
+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;
+		/*! 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;
+	/*! list of 'struct decoded_element' */
+	struct llist_head decoded_elements;
+};
+
+
+enum osim_file_type {
+	TYPE_NONE,
+	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,		/*!< 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
+
+#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 */
+	struct osim_file_desc *parent;	/*!< parent DF */
+
+	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 uint8_t *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;	/*!< Operations (parse/encode */
+
+	struct {
+		size_t min;		/*!< Minimum size of the file
+					  (transparent) or record in
+					  cyclic / linear file */
+		size_t rec;		/*!< Recommended size */
+	} 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,					\
+		.sfid		= sfi,					\
+		.type		= TYPE_EF,				\
+		.ef_type	= ptype,				\
+		.short_name	= pns,					\
+		.long_name	= pnl,					\
+		.flags		= pflags,				\
+		.ops 		= { .encode = penc, .parse = pdec },	\
+		.size		= { .min = smin, .rec = srec},		\
+	}
+
+
+/*! 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)
+
+
+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,
+};
+
+enum osim_card_sw_class {
+	SW_CLS_NONE,
+	SW_CLS_OK,
+	SW_CLS_POSTP,
+	SW_CLS_WARN,
+	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;
+};
+
+#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		\
+}
+
+/*! \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;
+};
+
+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);
+
+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);
+
+/* CARD READERS */
+
+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;
+	const struct osim_reader_ops *ops;
+	uint32_t proto_supported;
+	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 card protocol */
+	enum osim_proto proto;
+
+	/*! \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(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 */
diff --git a/libosmosim.pc.in b/libosmosim.pc.in
new file mode 100644
index 0000000..cec877b
--- /dev/null
+++ b/libosmosim.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: Osmocom SIM card related utilities Library
+Description: C Utility Library
+Version: @VERSION@
+Libs: -L${libdir} -losmosim
+Cflags: -I${includedir}/
+
diff --git a/src/sim/Makefile.am b/src/sim/Makefile.am
new file mode 100644
index 0000000..2b30ae9
--- /dev/null
+++ b/src/sim/Makefile.am
@@ -0,0 +1,21 @@
+# This is _NOT_ the library release version, it's an API version.
+# Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification
+LIBVERSION=0:0:0
+
+INCLUDES = $(all_includes) -I$(top_srcdir)/include
+AM_CFLAGS = -fPIC -Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(PCSC_CFLAGS)
+AM_LDFLAGS = $(COVERAGE_LDFLAGS)
+
+if ENABLE_PCSC
+# FIXME: only build the PC/SC dependent part conditional, but always build other parts
+
+noinst_HEADERS = sim_int.h gsm_int.h
+
+lib_LTLIBRARIES = libosmosim.la
+
+libosmosim_la_SOURCES = core.c reader.c reader_pcsc.c \
+			card_fs_sim.c card_fs_usim.c card_fs_uicc.c card_fs_isim.c card_fs_tetra.c
+libosmosim_la_LDFLAGS = -version-info $(LIBVERSION)
+libosmosim_la_LIBADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(PCSC_LIBS)
+
+endif
diff --git a/src/sim/card_fs_isim.c b/src/sim/card_fs_isim.c
new file mode 100644
index 0000000..339e862
--- /dev/null
+++ b/src/sim/card_fs_isim.c
@@ -0,0 +1,105 @@
+/* 3GPP ISIM specific structures / routines */
+/*
+ * (C) 2014 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/sim/sim.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/gsm/gsm48.h>
+
+#include "sim_int.h"
+#include "gsm_int.h"
+
+/* TS 31.103 Version 11.2.0 Release 11 / Chapoter 7.1.3 */
+const struct osim_card_sw ts31_103_sw[] = {
+	{
+		0x9862, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - Authentication error, incorrect MAC",
+	},
+	OSIM_CARD_SW_LAST
+};
+
+static const struct osim_card_sw *isim_card_sws[] = {
+	ts31_103_sw,
+	ts102221_uicc_sw,
+	NULL
+};
+
+/* TS 31.103 Version 11.2.0 Release 11 / Chapoter 4.2 */
+static const struct osim_file_desc isim_ef_in_adf_isim[] = {
+	EF_TRANSP_N(0x6F02, 0x02, "EF.IMPI", 0, 1, 256,
+		"IMS private user identity"),
+	EF_TRANSP_N(0x6F03, 0x05, "EF.DOMAIN", 0, 1, 256,
+		"Home Network Domain Name"),
+	EF_LIN_FIX_N(0x6F04, 0x04, "EF.IMPU", 0, 1, 256,
+		"IMS public user identity"),
+	EF_TRANSP_N(0x6FAD, 0x03, "EF.AD", 0, 4, 16,
+		"Administrative Data"),
+	EF_LIN_FIX_N(0x6F06, 0x06, "EF.ARR", 0, 1, 256,
+		"Access Rule TLV data objects"),
+	EF_TRANSP_N(0x6F07, 0x07, "EF.IST", F_OPTIONAL, 1, 16,
+		"ISIM Service Table"),
+	EF_LIN_FIX_N(0x6F09, SFI_NONE, "EF.P-CSCF", F_OPTIONAL, 1, 256,
+		"P-CSCF Address"),
+	EF_TRANSP_N(0x6FD5, SFI_NONE, "EF.GBABP", F_OPTIONAL, 1, 35,
+		"GBA Bootstrapping parameters"),
+	EF_LIN_FIX_N(0x6FD7, SFI_NONE, "EF.GBANL", F_OPTIONAL, 1, 256,
+		"NAF Key Identifier TLV Objects"),
+	EF_LIN_FIX_N(0x6FDD, SFI_NONE, "EF.NAFKCA", F_OPTIONAL, 1, 256,
+		"NAF Key Centre Address"),
+	EF_LIN_FIX_N(0x6F3C, SFI_NONE, "EF.SMS", F_OPTIONAL, 176, 176,
+		"Short messages"),
+	EF_TRANSP_N(0x6F43, SFI_NONE, "EF.SMSS", F_OPTIONAL, 2, 4,
+		"SMS status"),
+	EF_LIN_FIX_N(0x6F47, SFI_NONE, "EF.SMSR", F_OPTIONAL, 30, 30,
+		"Short message status reports"),
+	EF_LIN_FIX_N(0x6F42, SFI_NONE, "EF.SMSP", F_OPTIONAL, 29, 64,
+		"Short message service parameters"),
+	EF_LIN_FIX_N(0x6FE7, SFI_NONE, "EF.UICCIARI", F_OPTIONAL, 1, 256,
+		"UICC IARI"),
+};
+
+/* Annex E - TS 101 220 */
+static const uint8_t adf_isim_aid[] = { 0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x04 };
+
+struct osim_card_profile *osim_cprof_isim(void *ctx)
+{
+	struct osim_card_profile *cprof;
+	struct osim_file_desc *mf;
+
+	cprof = talloc_zero(ctx, struct osim_card_profile);
+	cprof->name = "3GPP ISIM";
+	cprof->sws = isim_card_sws;
+
+	mf = alloc_df(cprof, 0x3f00, "MF");
+
+	cprof->mf = mf;
+
+	/* ADF.USIM with its EF siblings */
+	add_adf_with_ef(mf, adf_isim_aid, sizeof(adf_isim_aid),
+			"ADF.ISIM", isim_ef_in_adf_isim,
+			ARRAY_SIZE(isim_ef_in_adf_isim));
+
+	return cprof;
+}
diff --git a/src/sim/card_fs_sim.c b/src/sim/card_fs_sim.c
new file mode 100644
index 0000000..432c945
--- /dev/null
+++ b/src/sim/card_fs_sim.c
@@ -0,0 +1,477 @@
+/* classic SIM card specific structures/routines */
+/*
+ * (C) 2012-2014 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/sim/sim.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/gsm/gsm48.h>
+
+#include "sim_int.h"
+
+/* 3GPP TS 51.011 / Chapter 9.4 */
+static const struct osim_card_sw ts11_11_sw[] = {
+	{
+		0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command",
+	}, {
+		0x9100, 0xff00, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command - proactive command from SIM pending",
+	}, {
+		0x9e00, 0xff00, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command - response data for SIM data download",
+	}, {
+		0x9f00, 0xff00, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command - response data available",
+	}, {
+		0x9300, 0xffff, SW_TYPE_STR, SW_CLS_POSTP,
+		.u.str = "SIM Application Toolkit is busy, command cannot be executed at present",
+	}, {
+		0x9200, 0xfff0, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Memory management - Command successful but after using an internal updat retry X times",
+	}, {
+		0x9240, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Memory management - Memory problem",
+	}, {
+		0x9400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - no EF selected",
+	}, {
+		0x9402, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - out of range (invalid address)",
+	}, {
+		0x9404, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - file ID not found / pattern not found",
+	}, {
+		0x9408, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - file is inconsistent with the command",
+	}, {
+		0x9802, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - no CHV initialized",
+	}, {
+		0x9804, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - access condition not fulfilled",
+	}, {
+		0x9808, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - in contradiction with CHV status",
+	}, {
+		0x9810, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - in contradiction with invalidation status",
+	}, {
+		0x9840, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - unsuccessful CHV verification, no attempt left",
+	}, {
+		0x9850, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - increase cannot be performed, max value reached",
+	}, {
+		0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - incorrect parameter P3",
+	}, {
+		0x6b00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - incorrect parameter P1 or P2",
+	}, {
+		0x6d00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - unknown instruction code",
+	}, {
+		0x6e00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - wrong instruction class",
+	}, {
+		0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - technical problem with no diagnostic given",
+	},
+	OSIM_CARD_SW_LAST
+};
+
+static const struct osim_card_sw *sim_card_sws[] = {
+	ts11_11_sw,
+	NULL
+};
+
+static int iccid_decode(struct osim_decoded_data *dd,
+			const struct osim_file_desc *desc,
+	     		int len, uint8_t *data)
+{
+	struct osim_decoded_element *elem;
+
+	elem = element_alloc(dd, "ICCID", ELEM_T_BCD, ELEM_REPR_DEC);
+	elem->length = len;
+	elem->u.buf = talloc_memdup(elem, data, len);
+
+	return 0;
+}
+
+static int elp_decode(struct osim_decoded_data *dd,
+		      const struct osim_file_desc *desc,
+		      int len, uint8_t *data)
+{
+	int i, num_lp = len / 2;
+
+	for (i = 0; i < num_lp; i++) {
+		uint8_t *cur = data + i*2;
+		struct osim_decoded_element *elem;
+		elem = element_alloc(dd, "Language Code", ELEM_T_STRING, ELEM_REPR_NONE);
+		elem->u.buf = (uint8_t *) talloc_strndup(elem, (const char *) cur, 2);
+	}
+
+	return 0;
+}
+
+/* 10.3.1 */
+int gsm_lp_decode(struct osim_decoded_data *dd,
+		 const struct osim_file_desc *desc,
+		 int len, uint8_t *data)
+{
+	int i;
+
+	for (i = 0; i < len; i++) {
+		struct osim_decoded_element *elem;
+		elem = element_alloc(dd, "Language Code", ELEM_T_UINT8, ELEM_REPR_DEC);
+		elem->u.u8 = data[i];
+	}
+
+	return 0;
+}
+
+/* 10.3.2 */
+int gsm_imsi_decode(struct osim_decoded_data *dd,
+		   const struct osim_file_desc *desc,
+		   int len, uint8_t *data)
+{
+	struct osim_decoded_element *elem;
+
+	if (len < 2)
+		return -EINVAL;
+
+	elem = element_alloc(dd, "IMSI", ELEM_T_BCD, ELEM_REPR_DEC);
+	elem->length = data[0];
+	elem->u.buf = talloc_memdup(elem, data+1, len-1);
+
+	return 0;
+}
+
+/* 10.3.3 */
+static int gsm_kc_decode(struct osim_decoded_data *dd,
+			 const struct osim_file_desc *desc,
+			 int len, uint8_t *data)
+{
+	struct osim_decoded_element *kc, *cksn;
+
+	if (len < 9)
+		return -EINVAL;
+
+	kc = element_alloc(dd, "Kc", ELEM_T_BYTES, ELEM_REPR_HEX);
+	kc->u.buf = talloc_memdup(kc, data, 8);
+	cksn = element_alloc(dd, "CKSN", ELEM_T_UINT8, ELEM_REPR_DEC);
+	cksn->u.u8 = data[8];
+
+	return 0;
+}
+
+/* 10.3.4 */
+static int gsm_plmnsel_decode(struct osim_decoded_data *dd,
+			      const struct osim_file_desc *desc,
+			      int len, uint8_t *data)
+{
+	int i, n_plmn = len / 3;
+
+	if (n_plmn < 1)
+		return -EINVAL;
+
+	for (i = 0; i < n_plmn; i++) {
+		uint8_t *cur = data + 3*i;
+		struct osim_decoded_element *elem, *mcc, *mnc;
+		uint8_t ra_buf[6];
+		struct gprs_ra_id ra_id;
+
+		memset(ra_buf, 0, sizeof(ra_buf));
+		memcpy(ra_buf, cur, 3);
+		gsm48_parse_ra(&ra_id, ra_buf);
+
+		elem = element_alloc(dd, "PLMN", ELEM_T_GROUP, ELEM_REPR_NONE);
+
+		mcc = element_alloc_sub(elem, "MCC", ELEM_T_UINT16, ELEM_REPR_DEC);
+		mcc->u.u16 = ra_id.mcc;
+
+		mnc = element_alloc_sub(elem, "MNC", ELEM_T_UINT16, ELEM_REPR_DEC);
+		mnc->u.u16 = ra_id.mnc;
+	}
+
+	return 0;
+}
+
+/* 10.3.5 */
+int gsm_hpplmn_decode(struct osim_decoded_data *dd,
+		     const struct osim_file_desc *desc,
+		     int len, uint8_t *data)
+{
+	struct osim_decoded_element *elem;
+
+	elem = element_alloc(dd, "Time interval", ELEM_T_UINT8, ELEM_REPR_DEC);
+	elem->u.u8 = *data;
+
+	return 0;
+}
+
+/* Chapter 10.1. Contents of the EFs at the MF level */
+static const struct osim_file_desc sim_ef_in_mf[] = {
+	EF_TRANSP(0x2FE2, SFI_NONE, "EF.ICCID", 0, 10, 10,
+		  "ICC Identification", &iccid_decode, NULL),
+	EF_TRANSP(0x2F05, SFI_NONE, "EF.PL", F_OPTIONAL, 2, 20,
+		  "Preferred language", &elp_decode, NULL),
+};
+
+/* Chapter 10.3.x Contents of files at the GSM application level */
+static const struct osim_file_desc sim_ef_in_gsm[] = {
+	EF_TRANSP(0x6F05, SFI_NONE, "EF.LP", 0, 1, 16,
+		  "Language preference", &gsm_lp_decode, NULL),
+	EF_TRANSP(0x6F07, SFI_NONE, "EF.IMSI", 0, 9, 9,
+		  "IMSI", &gsm_imsi_decode, NULL),
+	EF_TRANSP(0x6F20, SFI_NONE, "EF.Kc", 0, 9, 9,
+		  "Ciphering key Kc", &gsm_kc_decode, NULL),
+	EF_TRANSP(0x6F30, SFI_NONE, "EF.PLMNsel", F_OPTIONAL, 24, 72,
+		  "PLMN selector", &gsm_plmnsel_decode, NULL),
+	EF_TRANSP(0x6F31, SFI_NONE, "EF.HPPLMN", 0, 1, 1,
+		  "Higher Priority PLMN search period", &gsm_hpplmn_decode, NULL),
+	EF_TRANSP_N(0x6F37, SFI_NONE, "EF.ACMmax", F_OPTIONAL, 3, 3,
+		  "ACM maximum value"),
+	EF_TRANSP_N(0x6F38, SFI_NONE, "EF.SST", 0, 2, 16,
+		  "SIM service table"),
+	EF_CYCLIC_N(0x6F39, SFI_NONE, "EF.ACM", F_OPTIONAL, 3, 3,
+		  "Accumulated call meter"),
+	EF_TRANSP_N(0x6F3E, SFI_NONE, "EF.GID1", F_OPTIONAL, 1, 8,
+		  "Group Identifier Level 1"),
+	EF_TRANSP_N(0x6F3F, SFI_NONE, "EF.GID2", F_OPTIONAL, 1, 8,
+		  "Group Identifier Level 2"),
+	EF_TRANSP_N(0x6F46, SFI_NONE, "EF.SPN", F_OPTIONAL, 17, 17,
+		  "Service Provider Name"),
+	EF_TRANSP_N(0x6F41, SFI_NONE, "EF.PUCT", F_OPTIONAL, 5, 5,
+		  "Price per unit and currency table"),
+	EF_TRANSP_N(0x6F45, SFI_NONE, "EF.CBMI", F_OPTIONAL, 2, 32,
+		  "Cell broadcast massage identifier selection"),
+	EF_TRANSP_N(0x6F74, SFI_NONE, "EF.BCCH", 0, 16, 16,
+		  "Broadcast control channels"),
+	EF_TRANSP_N(0x6F78, SFI_NONE, "EF.ACC", 0, 2, 2,
+		  "Access control class"),
+	EF_TRANSP_N(0x6F7B, SFI_NONE, "EF.FPLMN", 0, 12, 12,
+		  "Forbidden PLMNs"),
+	EF_TRANSP_N(0x6F7E, SFI_NONE, "EF.LOCI", 0, 11, 11,
+		  "Location information"),
+	EF_TRANSP_N(0x6FAD, SFI_NONE, "EF.AD", 0, 3, 8,
+		  "Administrative data"),
+	EF_TRANSP_N(0x6FAE, SFI_NONE, "EF.Phase", 0, 1, 1,
+		  "Phase identification"),
+	EF_TRANSP_N(0x6FB1, SFI_NONE, "EF.VGCS", F_OPTIONAL, 4, 80,
+		  "Voice Group Call Service"),
+	EF_TRANSP_N(0x6FB2, SFI_NONE, "EF.VGCSS", F_OPTIONAL, 7, 7,
+		  "Voice Group Call Service Status"),
+	EF_TRANSP_N(0x6FB3, SFI_NONE, "EF.VBS", F_OPTIONAL, 4, 80,
+		  "Voice Broadcast Service"),
+	EF_TRANSP_N(0x6FB4, SFI_NONE, "EF.VBSS", F_OPTIONAL, 7, 7,
+		  "Voice Broadcast Service Status"),
+	EF_TRANSP_N(0x6FB5, SFI_NONE, "EF.eMLPP", F_OPTIONAL, 2, 2,
+		  "enhanced Mult Level Pre-emption and Priority"),
+	EF_TRANSP_N(0x6FB6, SFI_NONE, "EF.AAeM", F_OPTIONAL, 1, 1,
+		  "Automatic Answer for eMLPP Service"),
+	EF_TRANSP_N(0x6F48, SFI_NONE, "EF.CBMID", F_OPTIONAL, 2, 32,
+		  "Cell Broadcast Message Identifier for Data Download"),
+	EF_TRANSP_N(0x6FB7, SFI_NONE, "EF.ECC", F_OPTIONAL, 3, 15,
+		  "Emergency Call Code"),
+	EF_TRANSP_N(0x6F50, SFI_NONE, "EF.CBMIR", F_OPTIONAL, 4, 64,
+		  "Cell broadcast message identifier range selection"),
+	EF_TRANSP_N(0x6F2C, SFI_NONE, "EF.DCK", F_OPTIONAL, 16, 16,
+		  "De-personalization Control Keys"),
+	EF_TRANSP_N(0x6F32, SFI_NONE, "EF.CNL", F_OPTIONAL, 6, 60,
+		  "Co-operative Network List"),
+	EF_LIN_FIX_N(0x6F51, SFI_NONE, "EF.NIA", F_OPTIONAL, 1, 17,
+		   "Network's Indication of Alerting"),
+	EF_TRANSP_N(0x6F52, SFI_NONE, "EF.KcGPRS", F_OPTIONAL, 9, 9,
+		  "GPRS Ciphering key KcGPRS"),
+	EF_TRANSP_N(0x6F53, SFI_NONE, "EF.LOCIGPRS", F_OPTIONAL, 14, 14,
+		  "GPRS location information"),
+	EF_TRANSP_N(0x6F54, SFI_NONE, "EF.SUME", F_OPTIONAL, 1, 64,
+		  "SetUpMenu Elements"),
+	EF_TRANSP_N(0x6F60, SFI_NONE, "EF.PLMNwAcT", F_OPTIONAL, 40, 80,
+		  "User controlled PLMN Selector with Access Technology"),
+	EF_TRANSP_N(0x6F61, SFI_NONE, "EF.OPLMNwAcT", F_OPTIONAL, 40, 80,
+		  "Operator controlled PLMN Selector with Access Technology"),
+	EF_TRANSP_N(0x6F62, SFI_NONE, "EF.HPLMNwAcT", F_OPTIONAL, 5, 20,
+		  "HPLMN Selector with Access Technology"),
+	EF_TRANSP_N(0x6F63, SFI_NONE, "EF.CPBCCH", F_OPTIONAL, 2, 20,
+		  "CPBCCH Information"),
+	EF_TRANSP_N(0x6F64, SFI_NONE, "EF.InvScan", F_OPTIONAL, 1, 1,
+		  "Investigation Scan"),
+	EF_LIN_FIX_N(0x6FC5, SFI_NONE, "EF.PNN", F_OPTIONAL, 3, 20,
+		  "PLMN Network Name"),
+	EF_LIN_FIX_N(0x6FC6, SFI_NONE, "EF.OPL", F_OPTIONAL, 8, 8,
+		  "PLMN Operator PLMN List"),
+	EF_LIN_FIX_N(0x6FC7, SFI_NONE, "EF.MBDN", F_OPTIONAL, 14, 30,
+		  "Mailbox Dialling Number"),
+	EF_LIN_FIX_N(0x6FC9, SFI_NONE, "EF.MBI", F_OPTIONAL, 4, 4,
+		  "Maibox Identifier"),
+	EF_LIN_FIX_N(0x6FCA, SFI_NONE, "EF.MWIS", F_OPTIONAL, 5, 5,
+		  "Message Waiting Indication Status"),
+	EF_LIN_FIX_N(0x6FCB, SFI_NONE, "EF.CFIS", F_OPTIONAL, 16, 16,
+		  "Call Forwarding Indication Status"),
+	EF_LIN_FIX_N(0x6FC8, SFI_NONE, "EF.EXT6", F_OPTIONAL, 13, 13,
+		  "Extension6 (MBDN)"),
+	EF_LIN_FIX_N(0x6FCC, SFI_NONE, "EF.EXT7", F_OPTIONAL, 13, 13,
+		  "Extension7 (CFIS)"),
+	EF_TRANSP_N(0x6FCD, SFI_NONE, "EF.SPDI", F_OPTIONAL, 1, 32,
+		  "Extension7 (CFIS)"),
+	EF_LIN_FIX_N(0x6FCE, SFI_NONE, "EF.MMSN", F_OPTIONAL, 4, 32,
+		  "MMS Notification"),
+	EF_LIN_FIX_N(0x6FCF, SFI_NONE, "EF.EXT8", F_OPTIONAL, 2, 18,
+		  "Extension8 (MMSN)"),
+	EF_TRANSP_N(0x6FD0, SFI_NONE, "EF.MMSICP", F_OPTIONAL, 1, 64,
+		  "MMS Issuer Connectivity Parameters"),
+	EF_LIN_FIX_N(0x6FD1, SFI_NONE, "EF.MMSUP", F_OPTIONAL, 1, 64,
+		  "MMS User Preferences"),
+	EF_TRANSP_N(0x6FD2, SFI_NONE, "EF.MMSUCP", F_OPTIONAL, 1, 64,
+		  "MMS User Connectivity Parameters"),
+};
+
+/* 10.4.1 Contents of the files at the SoLSA level */
+static const struct osim_file_desc sim_ef_in_solsa[] = {
+	EF_TRANSP_N(0x4F30, SFI_NONE, "EF.SAI", F_OPTIONAL, 1, 32,
+		"SoLSA Access Indicator"),
+	EF_LIN_FIX_N(0x4F31, SFI_NONE, "EF.SLL", F_OPTIONAL, 1, 32,
+		"SoLSA LSA List"),
+	/* LSA Descriptor files */
+};
+
+/* 10.4.2 Contents of files at the MExE level */
+static const struct osim_file_desc sim_ef_in_mexe[] = {
+	EF_TRANSP_N(0x4F40, SFI_NONE, "EF.MExE-ST", F_OPTIONAL, 1, 8,
+		"MExE Service table"),
+	EF_LIN_FIX_N(0x4F41, SFI_NONE, "EF.ORPK", F_OPTIONAL, 11, 32,
+		"Operator Root Public Key"),
+	EF_LIN_FIX_N(0x4F42, SFI_NONE, "EF.ARPK", F_OPTIONAL, 11, 32,
+		"Administrator Root Public Key"),
+	EF_LIN_FIX_N(0x4F43, SFI_NONE, "EF.TRPK", F_OPTIONAL, 11, 32,
+		"Third Party Root Public Key"),
+};
+
+/* 10.5 Contents of files at the telecom level */
+static const struct osim_file_desc sim_ef_in_telecom[] = {
+	EF_LIN_FIX_N(0x6F3A, SFI_NONE, "EF.ADN", F_OPTIONAL, 14, 30,
+		"Abbreviated dialling numbers"),
+	EF_LIN_FIX_N(0x6F3B, SFI_NONE, "EF.FDN", F_OPTIONAL, 14, 30,
+		"Fixed dialling numbers"),
+	EF_LIN_FIX_N(0x6F3C, SFI_NONE, "EF.SMS", F_OPTIONAL, 176, 176,
+		"Short messages"),
+	EF_LIN_FIX_N(0x6F3D, SFI_NONE, "EF.CCP", F_OPTIONAL, 14, 14,
+		"Capability configuration parameters"),
+	EF_LIN_FIX_N(0x6F4F, SFI_NONE, "EF.ECCP", F_OPTIONAL, 15, 15,
+		"Extended Capability configuration parameters"),
+	EF_LIN_FIX_N(0x6F40, SFI_NONE, "EF.MSISDN", F_OPTIONAL, 14, 30,
+		"MSISDN"),
+	EF_LIN_FIX_N(0x6F42, SFI_NONE, "EF.SMSP", F_OPTIONAL, 28, 44,
+		"Short message service parameters"),
+	EF_TRANSP_N(0x6F43, SFI_NONE, "EF.SMSS", F_OPTIONAL, 2, 3,
+		"SMS Status"),
+	EF_CYCLIC_N(0x6F44, SFI_NONE, "EF.LND", F_OPTIONAL, 14, 30,
+		"Last number dialled"),
+	EF_LIN_FIX_N(0x6F49, SFI_NONE, "EF.SDN", F_OPTIONAL, 14, 30,
+		"Service Dialling Numbers"),
+	EF_LIN_FIX_N(0x6F4A, SFI_NONE, "EF.EXT1", F_OPTIONAL, 13, 13,
+		"Extension 1 (ADN/SSC, MSISDN, LND)"),
+	EF_LIN_FIX_N(0x6F4B, SFI_NONE, "EF.EXT2", F_OPTIONAL, 13, 13,
+		"Extension 2 (FDN/SSC)"),
+	EF_LIN_FIX_N(0x6F4C, SFI_NONE, "EF.EXT3", F_OPTIONAL, 13, 13,
+		"Extension 3 (SDN)"),
+	EF_LIN_FIX_N(0x6F4D, SFI_NONE, "EF.BDN", F_OPTIONAL, 15, 31,
+		"Barred dialling numbers"),
+	EF_LIN_FIX_N(0x6F4E, SFI_NONE, "EF.EXT4", F_OPTIONAL, 13, 13,
+		"Extension 4 (BDN/SSC)"),
+	EF_LIN_FIX_N(0x6F47, SFI_NONE, "EF.SMSR", F_OPTIONAL, 30, 30,
+		"Short message status reports"),
+	EF_LIN_FIX_N(0x6F58, SFI_NONE, "EF.CMI", F_OPTIONAL, 1, 17,
+		"Comparison Method Information"),
+};
+
+/* 10.6.1 Contents of files at the telecom graphics level */
+const struct osim_file_desc sim_ef_in_graphics[] = {
+	EF_LIN_FIX_N(0x4F20, SFI_NONE, "EF.IMG", F_OPTIONAL, 11, 38,
+		"Image"),
+};
+
+int osim_int_cprof_add_gsm(struct osim_file_desc *mf)
+{
+	struct osim_file_desc *gsm;
+
+	gsm = add_df_with_ef(mf, 0x7F20, "DF.GSM", sim_ef_in_gsm,
+			ARRAY_SIZE(sim_ef_in_gsm));
+	/* Chapter 10.2: DFs at the GSM Application Level */
+	add_df_with_ef(gsm, 0x5F30, "DF.IRIDIUM", NULL, 0);
+	add_df_with_ef(gsm, 0x5F31, "DF.GLOBALSTAR", NULL, 0);
+	add_df_with_ef(gsm, 0x5F32, "DF.ICO", NULL, 0);
+	add_df_with_ef(gsm, 0x5F33, "DF.ACeS", NULL, 0);
+	add_df_with_ef(gsm, 0x5F3C, "DF.MExE", sim_ef_in_mexe,
+			ARRAY_SIZE(sim_ef_in_mexe));
+	add_df_with_ef(gsm, 0x5F40, "DF.EIA/TIA-533", NULL, 0);
+	add_df_with_ef(gsm, 0x5F60, "DF.CTS", NULL, 0);
+	add_df_with_ef(gsm, 0x5F70, "DF.SoLSA", sim_ef_in_solsa,
+			ARRAY_SIZE(sim_ef_in_solsa));
+
+	return 0;
+}
+
+int osim_int_cprof_add_telecom(struct osim_file_desc *mf)
+{
+	struct osim_file_desc *tc;
+
+	tc = add_df_with_ef(mf, 0x7F10, "DF.TELECOM", sim_ef_in_telecom,
+			ARRAY_SIZE(sim_ef_in_telecom));
+	add_df_with_ef(tc, 0x5F50, "DF.GRAPHICS", sim_ef_in_graphics,
+			ARRAY_SIZE(sim_ef_in_graphics));
+	add_df_with_ef(mf, 0x7F22, "DF.IS-41", NULL, 0);
+	add_df_with_ef(mf, 0x7F23, "DF.FP-CTS", NULL, 0);	/* TS 11.19 */
+
+	return 0;
+}
+
+struct osim_card_profile *osim_cprof_sim(void *ctx)
+{
+	struct osim_card_profile *cprof;
+	struct osim_file_desc *mf;
+	int rc;
+
+	cprof = talloc_zero(ctx, struct osim_card_profile);
+	cprof->name = "GSM SIM";
+	cprof->sws = sim_card_sws;
+
+	mf = alloc_df(cprof, 0x3f00, "MF");
+
+	cprof->mf = mf;
+
+	/* According to Figure 8 */
+	add_filedesc(mf, sim_ef_in_mf, ARRAY_SIZE(sim_ef_in_mf));
+
+	rc = osim_int_cprof_add_gsm(mf);
+	rc |= osim_int_cprof_add_telecom(mf);
+	if (rc != 0) {
+		talloc_free(cprof);
+		return NULL;
+	}
+
+	return cprof;
+}
diff --git a/src/sim/card_fs_tetra.c b/src/sim/card_fs_tetra.c
new file mode 100644
index 0000000..625adbd
--- /dev/null
+++ b/src/sim/card_fs_tetra.c
@@ -0,0 +1,267 @@
+/* TETRA SIM card specific structures/routines */
+/*
+ * (C) 2014 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/sim/sim.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/gsm/gsm48.h>
+
+#include "sim_int.h"
+#include "gsm_int.h"
+
+/* EN 300 812 V2.1.1 (2001-12) 9.4 */
+static const struct osim_card_sw tsim_sw[] = {
+	{
+		0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command",
+	}, {
+		0x9f00, 0xff00, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command - response data available",
+	}, {
+		0x9300, 0xffff, SW_TYPE_STR, SW_CLS_POSTP,
+		.u.str = "SIM Application Toolkit is busy, command cannot be executed at present",
+	}, {
+		0x9200, 0xfff0, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Memory management - Command successful but after using an internal updat retry X times",
+	}, {
+		0x9240, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Memory management - Memory problem",
+	}, {
+		0x9400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - no EF selected",
+	}, {
+		0x9402, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - out of range (invalid address)",
+	}, {
+		0x9404, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - file ID not found / pattern not found",
+	}, {
+		0x9408, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Referencing management - file is inconsistent with the command",
+	}, {
+		0x9802, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - no CHV initialized",
+	}, {
+		0x9804, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - access condition not fulfilled",
+	}, {
+		0x9808, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - in contradiction with CHV status",
+	}, {
+		0x9810, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - in contradiction with invalidation status",
+	}, {
+		0x9840, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - unsuccessful CHV verification, no attempt left",
+	}, {
+		0x9860, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - manipulation flag set",
+	}, {
+		0x9870, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - SwMI authentication unsuccessful",
+	}, {
+		0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - incorrect parameter P3",
+	}, {
+		0x6b00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - incorrect parameter P1 or P2",
+	}, {
+		0x6d00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - unknown instruction code",
+	}, {
+		0x6e00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - wrong instruction class",
+	}, {
+		0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application independent - technical problem with no diagnostic given",
+	},
+	OSIM_CARD_SW_LAST
+};
+
+static const struct osim_card_sw *tsim_card_sws[] = {
+	tsim_sw,
+	NULL
+};
+
+/* Chapter 10.2.x */
+static const struct osim_file_desc sim_ef_in_mf[] = {
+	EF_TRANSP_N(0x2FE2, SFI_NONE, "EF.ICCID", 0, 10, 10,
+		  "ICC Identification"),
+	EF_TRANSP_N(0x2F00, SFI_NONE, "EF.DIR", F_OPTIONAL, 8, 54,
+		  "Application directory"),
+	EF_TRANSP_N(0x2F05, SFI_NONE, "EF.LP", 0, 2, 32,
+		  "Language preference"),
+};
+
+////////////////////////////
+
+/* Chapter 10.3.x */
+static const struct osim_file_desc sim_ef_in_tetra[] = {
+	EF_TRANSP_N(0x6F01, SFI_NONE, "EF.SST", 0, 4, 8,
+		"SIM service table"),
+	EF_TRANSP_N(0x6F02, SFI_NONE, "EF.ITSI", 0, 6, 6,
+		"ITSI"),
+	EF_TRANSP_N(0x6F03, SFI_NONE, "EF.ITSIDIS", 0, 1, 1,
+		"ITSI Disable"),
+	EF_TRANSP_N(0x6F05, SFI_NONE, "EF.SCT", 0, 4, 4,
+		"Subscriber Class Table"),
+	EF_TRANSP_N(0x6F06, SFI_NONE, "EF.PHASE", 0, 1, 1,
+		"Phase identification"),
+	EF_KEY_N(0x6F07, SFI_NONE, "EF.CCK", F_OPTIONAL, 12, 12,
+		"Common Cipher Key"),
+	EF_TRANSP_N(0x6F08, SFI_NONE, "EF.CCKLOC", F_OPTIONAL, 31, 31,
+		"CCK location areas"),
+	EF_KEY_N(0x6F09, SFI_NONE, "EF.SCK", F_OPTIONAL, 12, 12,
+		"Static Cipher Keys"),
+	/* X+4 for each record, suggested 1 to 10 */
+	EF_LIN_FIX_N(0x6F0A, SFI_NONE, "EF.GSSIS", 0, 5, 21,
+		"Static Cipher Keys"),
+	/* 2 for each record, one for each recod in GSSIS */
+	EF_LIN_FIX_N(0x6F0B, SFI_NONE, "EF.GRDS", 0, 2, 2,
+		"Group related data for static GSSIS"),
+	EF_LIN_FIX_N(0x6F0C, SFI_NONE, "EF.GSSID", 0, 5, 21,
+		"Dynamic GSSIs"),
+	/* 2 for each record, one for each recod in GSSID */
+	EF_LIN_FIX_N(0x6F0D, SFI_NONE, "EF.GRDD", 0, 2, 2,
+		"Dynamic GSSIs"),
+	EF_LIN_FIX_N(0x6F0E, SFI_NONE, "EF.GCK", F_OPTIONAL, 12, 12,
+		"Group Cipher Keys"),
+	EF_KEY_N(0x6F0F, SFI_NONE, "EF.MGCK", F_OPTIONAL, 12, 12,
+		"Modified Group Cipher Keys"),
+	EF_TRANSP_N(0x6F10, SFI_NONE, "EF.GINFO", 0, 9, 9,
+		"User's group information"),
+	EF_TRANSP_N(0x6F11, SFI_NONE, "EF.SEC", 0, 1, 1,
+		"Security settings"),
+	EF_CYCLIC_N(0x6F12, SFI_NONE, "EF.FORBID", 0, 3, 3,
+		"Security settings"),
+	EF_CYCLIC_N(0x6F13, SFI_NONE, "EF.PREF", F_OPTIONAL, 3, 3,
+		"Preferred networks"),
+	EF_TRANSP_N(0x6F14, SFI_NONE, "EF.SPN", F_OPTIONAL, 17, 17,
+		"Service Provider Name"),
+	EF_TRANSP_N(0x6F15, SFI_NONE, "EF.LOCI", F_OPTIONAL, 7, 7,
+		"Location Information"),
+	EF_TRANSP_N(0x6F16, SFI_NONE, "EF.DNWRK", 0, 3, 3,
+		"Broadcast network information"),
+	EF_LIN_FIX_N(0x6F17, SFI_NONE, "EF.NWT", 0, 5, 5,
+		"Network table"),
+	EF_LIN_FIX_N(0x6F18, SFI_NONE, "EF.GWT", F_OPTIONAL, 13, 13,
+		"Gateway table"),
+	EF_LIN_FIX_N(0x6F19, SFI_NONE, "EF.CMT", F_OPTIONAL, 5, 20,
+		"Call Modifier Table"),
+	EF_LIN_FIX_N(0x6F1A, SFI_NONE, "EF.ADNGWT", F_OPTIONAL, 13, 28,
+		"Abbreviated Dialling Number with Gateways"),
+	EF_LIN_FIX_N(0x6F1C, SFI_NONE, "EF.ADNTETRA", F_OPTIONAL, 9, 23,
+		"Abbreviated dialling numbers for TETRA network"),
+	EF_LIN_FIX_N(0x6F1D, SFI_NONE, "EF.EXTA", F_OPTIONAL, 20, 20,
+		"Extension A"),
+	EF_LIN_FIX_N(0x6F1E, SFI_NONE, "EF.FDNGWT", F_OPTIONAL, 13, 28,
+		"Fixed dialling numbers with Gateways"),
+	EF_LIN_FIX_N(0x6F1F, SFI_NONE, "EF.GWTEXT2", F_OPTIONAL, 13, 13,
+		"Gateway Extension2"),
+	EF_LIN_FIX_N(0x6F20, SFI_NONE, "EF.FDNTETRA", F_OPTIONAL, 9, 25,
+		"Fixed dialling numbers for TETRA network"),
+	EF_LIN_FIX_N(0x6F21, SFI_NONE, "EF.EXTB", F_OPTIONAL, 20, 20,
+		"Extension B"),
+	EF_LIN_FIX_N(0x6F22, SFI_NONE, "EF.LNDGWT", F_OPTIONAL, 13, 28,
+		"Last number dialled with Gateways"),
+	EF_CYCLIC_N(0x6F23, SFI_NONE, "EF.LNDTETRA", F_OPTIONAL, 9, 23,
+		"Last numbers dialled for TETRA network"),
+	EF_LIN_FIX_N(0x6F24, SFI_NONE, "EF.SDNGWT", F_OPTIONAL, 13, 28,
+		"Service Dialling Numbers with gateway"),
+	EF_LIN_FIX_N(0x6F25, SFI_NONE, "EF.GWTEXT3", F_OPTIONAL, 13, 13,
+		"Gateway Extension3"),
+	EF_LIN_FIX_N(0x6F26, SFI_NONE, "EF.SDNTETRA", F_OPTIONAL, 8, 22,
+		"Service Dialling Nubers for TETRA network"),
+	EF_LIN_FIX_N(0x6F27, SFI_NONE, "EF.STXT", F_OPTIONAL, 5, 18,
+		"Status message texts"),
+	EF_LIN_FIX_N(0x6F28, SFI_NONE, "EF.MSGTXT", F_OPTIONAL, 5, 18,
+		"SDS-1 message texts"),
+	EF_LIN_FIX_N(0x6F29, SFI_NONE, "EF.SDS123", F_OPTIONAL, 46, 46,
+		"Status and SDS type 1, 2 and 3 message storage"),
+	EF_LIN_FIX_N(0x6F2A, SFI_NONE, "EF.SDS4", F_OPTIONAL, 255, 255,
+		"Status and SDS type 4 message storage"),
+	EF_LIN_FIX_N(0x6F2B, SFI_NONE, "EF.MSGEXT", F_OPTIONAL, 16, 16,
+		"Message Extension"),
+	EF_LIN_FIX_N(0x6F2C, SFI_NONE, "EF.EADDR", 0, 17, 17,
+		"Emergency adresses"),
+	EF_TRANSP_N(0x6F2D, SFI_NONE, "EF.EINFO", 0, 2, 2,
+		"Emergency call information"),
+	EF_LIN_FIX_N(0x6F2E, SFI_NONE, "EF.DMOCh", F_OPTIONAL, 4, 4,
+		"DMO channel information"),
+	EF_TRANSP_N(0x6F2F, SFI_NONE, "EF.MSCh", F_OPTIONAL, 1, 16,
+		"MS allocation of DMO channels"),
+	EF_TRANSP_N(0x6F30, SFI_NONE, "EF.KH", F_OPTIONAL, 6, 6,
+		"List of Key Holders"),
+	EF_LIN_FIX_N(0x6F31, SFI_NONE, "EF.REPGATE", F_OPTIONAL, 2, 2,
+		"DMO repeater and gateway list"),
+	EF_TRANSP_N(0x6F32, SFI_NONE, "EF.AD", F_OPTIONAL, 2, 2,
+		"Administrative Data"),
+	EF_TRANSP_N(0x6F33, SFI_NONE, "EF.PREF_LA", F_OPTIONAL, 2, 2,
+		"Preferred location areas"),
+	EF_CYCLIC_N(0x6F34, SFI_NONE, "EF.LNDComp", F_OPTIONAL, 3, 3,
+		"Composite LND file"),
+	EF_TRANSP_N(0x6F35, SFI_NONE, "EF.DFLTSTSTGGT", F_OPTIONAL, 16, 16,
+		"Status Default Target"),
+	EF_TRANSP_N(0x6F36, SFI_NONE, "EF.SDSMEM_STATUS", F_OPTIONAL, 7, 7,
+		"SDS Memory Status"),
+	EF_TRANSP_N(0x6F37, SFI_NONE, "EF.WELCOME", F_OPTIONAL, 32, 32,
+		"Welcome Message"),
+	EF_LIN_FIX_N(0x6F38, SFI_NONE, "EF.SDSR", F_OPTIONAL, 2, 2,
+		"SDS delivery report"),
+	EF_LIN_FIX_N(0x6F39, SFI_NONE, "EF.SDSP", F_OPTIONAL, 20, 35,
+		"SDS parameters"),
+	EF_TRANSP_N(0x6F46, SFI_NONE, "EF.DIALSC", 0, 5, 5,
+		"Dialling schemes for TETRA network"),
+	EF_TRANSP_N(0x6F3E, SFI_NONE, "EF.APN", F_OPTIONAL, 65, 65,
+		"APN table"),
+	EF_LIN_FIX_N(0x6FC0, SFI_NONE, "EF.PNI", F_OPTIONAL, 14, 14,
+		"Private Number Information"),
+};
+
+struct osim_card_profile *osim_cprof_tsim(void *ctx)
+{
+	struct osim_card_profile *cprof;
+	struct osim_file_desc *mf;
+	int rc;
+
+	cprof = talloc_zero(ctx, struct osim_card_profile);
+	cprof->name = "TETRA SIM";
+	cprof->sws = tsim_card_sws;
+
+	mf = alloc_df(cprof, 0x3f00, "MF");
+
+	cprof->mf = mf;
+
+	add_filedesc(mf, sim_ef_in_mf, ARRAY_SIZE(sim_ef_in_mf));
+	add_df_with_ef(mf, 0x7F20, "DF.TETRA", sim_ef_in_tetra,
+			ARRAY_SIZE(sim_ef_in_tetra));
+
+	rc = osim_int_cprof_add_telecom(mf);
+	if (rc != 0) {
+		talloc_free(cprof);
+		return NULL;
+	}
+
+	return cprof;
+}
diff --git a/src/sim/card_fs_uicc.c b/src/sim/card_fs_uicc.c
new file mode 100644
index 0000000..70737b6
--- /dev/null
+++ b/src/sim/card_fs_uicc.c
@@ -0,0 +1,208 @@
+/* ETSI UICC specific structures / routines */
+/*
+ * (C) 2012 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+
+#include <osmocom/sim/sim.h>
+#include <osmocom/gsm/tlv.h>
+
+/* TS 102 221 V10.0.0 / 10.2.1 */
+const struct osim_card_sw ts102221_uicc_sw[] = {
+	{
+		0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command",
+	}, {
+		0x9100, 0xff00, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command, extra info proactive",
+	}, {
+		0x9200, 0xff00, SW_TYPE_STR, SW_CLS_OK,
+		.u.str = "Normal ending of the command, extra info regarding transfer session",
+	}, {
+		0x9300, 0xff00, SW_TYPE_STR, SW_CLS_POSTP,
+		.u.str = "SIM Application Toolkit is busy, command cannot be executed at present",
+	}, {
+		0x6200, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "No information given, state of non volatile memory unchanged",
+	}, {
+		0x6281, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Part of returned data may be corrupted",
+	}, {
+		0x6282, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "End of file/record reached before reading Le bytes",
+	}, {
+		0x6283, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Selected file invalidated",
+	}, {
+		0x6285, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Selected file in termination state",
+	}, {
+		0x62f1, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "More data available",
+	}, {
+		0x62f2, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "More data available and proactive command pending",
+	}, {
+		0x62f3, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Response data available",
+	}, {
+		0x63f1, 0xffff, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "More data expected",
+	}, {
+		0x63c0, 0xfff0, SW_TYPE_STR, SW_CLS_WARN,
+		.u.str = "Verification falied, X retries remaining",
+	}, {
+		0x6400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Execution - No information given, state of non-volatile memory unchanged",
+	}, {
+		0x6500, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Execution - No information given, state of non-volatile memory changed",
+	}, {
+		0x6581, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Execution - Memory problem",
+	}, {
+		0x6700, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Wrong length",
+	}, {
+		0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Command dependent error",
+	}, {
+		0x6b00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Wrong parameter(s) P1-P2",
+	}, {
+		0x6d00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Instruction code not supported or valid",
+	}, {
+		0x6e00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Class not supported",
+	}, {
+		0x6f00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Technical problem, no precise diagnostics",
+	}, {
+		0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Checking - Command dependent error",
+	}, {
+		0x6800, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Function in CLA not supported - No information given",
+	}, {
+		0x6881, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Function in CLA not supported - Logical channel not supported",
+	}, {
+		0x6882, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Function in CLA not supportied - Secure messaging not supported",
+	}, {
+		0x6900, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - No information given",
+	}, {
+		0x6981, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - Command incompatible with file structure",
+	}, {
+		0x6982, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - Security status not satisfied",
+	}, {
+		0x6983, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - Authentication/PIN method blocked",
+	}, {
+		0x6984, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - Referenced data invalidated",
+	}, {
+		0x6985, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - Conditions of use not satisfied",
+	}, {
+		0x6986, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - Noe EF selected",
+	}, {
+		0x6989, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Command not allowed - secure channel - security not satisfied",
+	}, {
+		0x6a80, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Incorrect parameters in the data field",
+	}, {
+		0x6a81, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Function not supported",
+	}, {
+		0x6a82, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - File not found",
+	}, {
+		0x6a83, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Record not found",
+	}, {
+		0x6a84, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Not enough memory space",
+	}, {
+		0x6a86, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Incorrect parameters P1 to P2",
+	}, {
+		0x6a87, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Lc inconsistent with P1 ot P2",
+	}, {
+		0x6a88, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Wrong parameters - Referenced data not found",
+	}, {
+		0x9850, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application error - INCREASE cannot be performed, max value reached",
+	}, {
+		0x9862, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application error - Authentication error, application specific",
+	}, {
+		0x9863, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Application error - Security session or association expired",
+	},
+	OSIM_CARD_SW_LAST
+};
+
+const struct value_string ts102221_fcp_vals[14] = {
+	{ UICC_FCP_T_FCP,		"File control parameters" },
+	{ UICC_FCP_T_FILE_SIZE,		"File size" },
+	{ UICC_FCP_T_TOT_F_SIZE,	"Total size of files" },
+	{ UICC_FCP_T_FILE_DESC,		"File descriptor" },
+	{ UICC_FCP_T_FILE_ID,		"File identifier" },
+	{ UICC_FCP_T_DF_NAME,		"DF name" },
+	{ UICC_FCP_T_SFID,		"Short file identifier" },
+	{ UICC_FCP_T_LIFEC_STS,		"Lifecycle status integer" },
+	{ UICC_FCP_T_SEC_ATTR_REFEXP,	"Security attributes (Referenced/Expanded)" },
+	{ UICC_FCP_T_SEC_ATTR_COMP,	"Security attributes (Compact)" },
+	{ UICC_FCP_T_PROPRIETARY,	"Proprietary" },
+	{ UICC_FCP_T_SEC_ATTR_EXP,	"Security attributes (Expanded)" },
+	{ UICC_FCP_T_PIN_STS_DO,	"PIN Status DO" },
+	{ 0, NULL }
+};
+
+/* FIXME: Ber-TLV ?? */
+const struct tlv_definition ts102221_fcp_tlv_def = {
+	.def = {
+		[UICC_FCP_T_FCP]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_FILE_SIZE]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_TOT_F_SIZE]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_FILE_DESC]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_FILE_ID]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_DF_NAME]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_SFID]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_LIFEC_STS]		= { TLV_TYPE_TLV },
+		[UICC_FCP_T_SEC_ATTR_REFEXP]	= { TLV_TYPE_TLV },
+		[UICC_FCP_T_SEC_ATTR_COMP]	= { TLV_TYPE_TLV },
+		[UICC_FCP_T_PROPRIETARY] 	= { TLV_TYPE_TLV },
+		[UICC_FCP_T_SEC_ATTR_EXP]	= { TLV_TYPE_TLV },
+		[UICC_FCP_T_PIN_STS_DO]		= { TLV_TYPE_TLV },
+	},
+};
+
+/* Annex E - TS 101 220 */
+static const uint8_t adf_uicc_aid[] = { 0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x01 };
diff --git a/src/sim/card_fs_usim.c b/src/sim/card_fs_usim.c
new file mode 100644
index 0000000..22c193f
--- /dev/null
+++ b/src/sim/card_fs_usim.c
@@ -0,0 +1,380 @@
+/* 3GPP USIM specific structures / routines */
+/*
+ * (C) 2012-2014 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/sim/sim.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/gsm/gsm48.h>
+
+#include "sim_int.h"
+#include "gsm_int.h"
+
+/* TS 31.102 Version 7.7.0 / Chapter 7.3 */
+const struct osim_card_sw ts31_102_sw[] = {
+	{
+		0x9862, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - Authentication error, incorrect MAC",
+	}, {
+		0x9864, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - Authentication error, security context not supported",
+	}, {
+		0x9865, 0xffff, SW_TYPE_STR, SW_CLS_ERROR,
+		.u.str = "Security management - Key freshness error",
+	},
+	OSIM_CARD_SW_LAST
+};
+
+static const struct osim_card_sw *usim_card_sws[] = {
+	ts31_102_sw,
+	ts102221_uicc_sw,
+	NULL
+};
+
+/* TS 102 221 Chapter 13.1 */
+static const struct osim_file_desc uicc_ef_in_mf[] = {
+	EF_LIN_FIX_N(0x2f00, SFI_NONE, "EF.DIR", 0, 1, 32,
+			"Application directory"),
+	EF_TRANSP_N(0x2FE2, SFI_NONE, "EF.ICCID", 0, 10, 10,
+			"ICC Identification"),
+	EF_TRANSP_N(0x2F05, SFI_NONE, "EF.PL", 0, 2, 20,
+			"Preferred Languages"),
+	EF_LIN_FIX_N(0x2F06, SFI_NONE, "EF.ARR", F_OPTIONAL, 1, 256,
+			"Access Rule Reference"),
+};
+
+/* 31.102 Chapter 4.4.3 */
+static const struct osim_file_desc usim_ef_in_df_gsm_access[] = {
+	EF_TRANSP_N(0x4f20, 0x01, "EF.Kc", 0, 9, 9,
+		"Ciphering Key Kc"),
+	EF_TRANSP_N(0x4f52, 0x02, "EF.KcGPRS", F_OPTIONAL, 9, 9,
+		"GPRS Ciphering key KcGPRS"),
+	EF_TRANSP_N(0x4f63, SFI_NONE, "EF.CPBCCH", F_OPTIONAL, 2, 20,
+		"CPBCCH Information"),
+	EF_TRANSP_N(0x4f64, SFI_NONE, "EF.invSCAN", F_OPTIONAL, 1, 1,
+		"Investigation Scan"),
+};
+
+/* 31.102 Chapter 4.2 */
+static const struct osim_file_desc usim_ef_in_adf_usim[] = {
+	EF_TRANSP(0x6F05, 0x02, "EF.LI", 0, 2, 16,
+		"Language Indication", &gsm_lp_decode, NULL),
+	EF_TRANSP(0x6F07, 0x07, "EF.IMSI", 0, 9, 9,
+		"IMSI", &gsm_imsi_decode, NULL),
+	EF_TRANSP_N(0x6F08, 0x08, "EF.Keys", 0, 33, 33,
+		"Ciphering and Integrity Keys"),
+	EF_TRANSP_N(0x6F09, 0x09, "EF.KeysPS", 0, 33, 33,
+		"Ciphering and Integrity Keys for Packet Switched domain"),
+	EF_TRANSP_N(0x6F60, 0x0A, "EF.PLMNwAcT", F_OPTIONAL, 40, 80,
+		"User controlled PLMN Selector with Access Technology"),
+	EF_TRANSP(0x6F31, 0x12, "EF.HPPLMN", 0, 1, 1,
+		"Higher Priority PLMN search period", &gsm_hpplmn_decode, NULL),
+	EF_TRANSP_N(0x6F37, SFI_NONE, "EF.ACMmax", F_OPTIONAL, 3, 3,
+		"ACM maximum value"),
+	EF_TRANSP_N(0x6F38, 0x04, "EF.UST", 0, 1, 16,
+		"USIM Service Table"),
+	EF_CYCLIC_N(0x6F39, SFI_NONE, "EF.ACM", F_OPTIONAL, 3, 3,
+		"Accumulated call meter"),
+	EF_TRANSP_N(0x6F3E, SFI_NONE, "EF.GID1", F_OPTIONAL, 1, 4,
+		"Group Identifier Level 1"),
+	EF_TRANSP_N(0x6F3F, SFI_NONE, "EF.GID2", F_OPTIONAL, 1, 4,
+		"Group Identifier Level 2"),
+	EF_TRANSP_N(0x6F46, SFI_NONE, "EF.SPN", F_OPTIONAL, 17, 17,
+		"Service Provider Name"),
+	EF_TRANSP_N(0x6F41, SFI_NONE, "EF.PUCT", F_OPTIONAL, 5, 5,
+		"Price per unit and currency table"),
+	EF_TRANSP_N(0x6F45, SFI_NONE, "EF.CBMI", F_OPTIONAL, 2, 32,
+		"Cell broadcast massage identifier selection"),
+	EF_TRANSP_N(0x6F78, 0x06, "EF.ACC", 0, 2, 2,
+		"Access control class"),
+	EF_TRANSP_N(0x6F7B, 0x0D, "EF.FPLMN", 0, 12, 36,
+		"Forbidden PLMNs"),
+	EF_TRANSP_N(0x6F7E, 0x0B, "EF.LOCI", 0, 11, 11,
+		"Location information"),
+	EF_TRANSP_N(0x6FAD, 0x03, "EF.AD", 0, 5, 16,
+		"Administrative data"),
+	EF_TRANSP_N(0x6F48, 0x0E, "EF.CBMID", F_OPTIONAL, 2, 32,
+		"Cell Broadcast Message Identifier for Data Download"),
+	EF_TRANSP_N(0x6FB7, 0x01, "EF.ECC", F_OPTIONAL, 5, 32,
+		"Emergency Call Code"),
+	EF_TRANSP_N(0x6F50, SFI_NONE, "EF.CBMIR", F_OPTIONAL, 4, 32,
+		"Cell broadcast message identifier range selection"),
+	EF_TRANSP_N(0x6F73, 0x0C, "EF.PSLOCI", 0, 14, 14,
+		"Pacet Switched location information"),
+	EF_LIN_FIX_N(0x6F3B, SFI_NONE, "EF.FDN", F_OPTIONAL, 14, 32,
+		"Fixed dialling numbers"),
+	EF_LIN_FIX_N(0x6F3C, SFI_NONE, "EF.SMS", F_OPTIONAL, 176, 176,
+		"Short messages"),
+	EF_LIN_FIX_N(0x6F40, SFI_NONE, "EF.MSISDN", F_OPTIONAL, 14, 32,
+		"MSISDN"),
+	EF_LIN_FIX_N(0x6F42, SFI_NONE, "EF.SMSP", F_OPTIONAL, 28, 64,
+		"Short message service parameters"),
+	EF_TRANSP_N(0x6F43, SFI_NONE, "EF.SMSS", F_OPTIONAL, 2, 8,
+		"SMS Status"),
+	EF_LIN_FIX_N(0x6F49, SFI_NONE, "EF.SDN", F_OPTIONAL, 14, 32,
+		"Service Dialling Numbers"),
+	EF_LIN_FIX_N(0x6F4B, SFI_NONE, "EF.EXT2", F_OPTIONAL, 13, 13,
+		"Extension 2"),
+	EF_LIN_FIX_N(0x6F4C, SFI_NONE, "EF.EXT3", F_OPTIONAL, 13, 13,
+		"Extension 3"),
+	EF_LIN_FIX_N(0x6F47, SFI_NONE, "EF.SMSR", F_OPTIONAL, 30, 30,
+		"Short message status reports"),
+	EF_CYCLIC_N(0x6F80, 0x14, "EF.ICI", F_OPTIONAL, 28, 64,
+		"Incoming Calling Information"),
+	EF_CYCLIC_N(0x6F81, 0x15, "EF.OCI", F_OPTIONAL, 27, 64,
+		"Outgoing Calling Information"),
+	EF_CYCLIC_N(0x6F82, SFI_NONE, "EF.ICT", F_OPTIONAL, 3, 3,
+		"Incoming Call Timer"),
+	EF_CYCLIC_N(0x6F83, SFI_NONE, "EF.OCT", F_OPTIONAL, 3, 3,
+		"Outgoing Call Timer"),
+	EF_LIN_FIX_N(0x6F4E, SFI_NONE, "EF.EXT5", F_OPTIONAL, 13, 13,
+		"Extension 5"),
+	EF_LIN_FIX_N(0x6F4F, 0x16, "EF.CCP2", F_OPTIONAL, 15, 32,
+		"Capability Configuration Parameters 2"),
+	EF_TRANSP_N(0x6FB5, SFI_NONE, "EF.eMLPP", F_OPTIONAL, 2, 2,
+		"enhanced Multi Level Precedence and Pre-emption"),
+	EF_TRANSP_N(0x6FB6, SFI_NONE, "EF.AAeM", F_OPTIONAL, 1, 1,
+		"Automatic Answer for eMLPP Service"),
+	EF_TRANSP_N(0x6FC3, SFI_NONE, "EF.Hiddenkey", F_OPTIONAL, 4, 4,
+		"Key for hidden phone book entries"),
+	EF_LIN_FIX_N(0x6F4D, SFI_NONE, "EF.BDN", F_OPTIONAL, 15, 32,
+		"Barred Dialling Numbers"),
+	EF_LIN_FIX_N(0x6F4E, SFI_NONE, "EF.EXT4", F_OPTIONAL, 13, 13,
+		"Extension 4"),
+	EF_LIN_FIX_N(0x6F58, SFI_NONE, "EF.CMI", F_OPTIONAL, 2, 16,
+		"Comparison Method Information"),
+	EF_TRANSP_N(0x6F56, 0x05, "EF.EST", F_OPTIONAL, 1, 8,
+		"Enhanced Services Table"),
+	EF_TRANSP_N(0x6F57, SFI_NONE, "EF.ACL", F_OPTIONAL, 2, 256,
+		"Access Point Name Control List"),
+	EF_TRANSP_N(0x6F2C, SFI_NONE, "EF.DCK", F_OPTIONAL, 16, 16,
+		"Depersonalisation Control Keys"),
+	EF_TRANSP_N(0x6F32, SFI_NONE, "EF.CNL", F_OPTIONAL, 6, 46,
+		"Co-operative Network List"),
+	EF_TRANSP_N(0x6F5B, 0x0F, "EF.START-HFN", 0, 6, 6,
+		"Initialisation values for Hyperframe number"),
+	EF_TRANSP_N(0x6F5C, 0x10, "EF.THRESHOLD", 0, 3, 3,
+		"Maximum value of START"),
+	EF_TRANSP_N(0x6F61, 0x11, "EF.OPLMNwAcT", F_OPTIONAL, 40, 80,
+		"Operator controlled PLMN Selector with Access Technology"),
+	EF_TRANSP_N(0x6F62, 0x13, "EF.HPLMNwAcT", F_OPTIONAL, 5, 30,
+		"HPLMN Selector with Access Technology"),
+	EF_LIN_FIX_N(0x6F06, 0x17, "EF.ARR", 0, 1, 256,
+		"Access Rule Reference"),
+	EF_TRANSP_N(0x6FC4, SFI_NONE, "EF.NETPAR", 0, 46, 256,
+		"Network Parameters"),
+	EF_LIN_FIX_N(0x6FC5, 0x19, "EF.PNN", F_OPTIONAL, 3, 128,
+		"PLMN Network Name"),
+	EF_LIN_FIX_N(0x6FC6, 0x1A, "EF.OPL", F_OPTIONAL, 8, 32,
+		"Operator PLMN List"),
+	EF_LIN_FIX_N(0x6FC7, SFI_NONE, "EF.MBDN", F_OPTIONAL, 14, 32,
+		"Mailbox Dialling Numbers"),
+	EF_LIN_FIX_N(0x6FC8, SFI_NONE, "EF.EXT6", F_OPTIONAL, 13, 13,
+		"Extension 6"),
+	EF_LIN_FIX_N(0x6FC9, SFI_NONE, "EF.MBI", F_OPTIONAL, 4, 5,
+		"Mailbox Identifier"),
+	EF_LIN_FIX_N(0x6FCA, SFI_NONE, "EF.MWIS", F_OPTIONAL, 5, 6,
+		"Message Waiting Indication Status"),
+	EF_LIN_FIX_N(0x6FCB, SFI_NONE, "EF.CFIS", F_OPTIONAL, 16, 16,
+		"Call Forwarding Indication Status"),
+	EF_LIN_FIX_N(0x6FCC, SFI_NONE, "EF.EXT7", F_OPTIONAL, 13, 13,
+		"Extension 7"),
+	EF_TRANSP_N(0x6FCD, 0x1B, "EF.SPDI", F_OPTIONAL, 1, 64,
+		"Service Provider Display Information"),
+	EF_LIN_FIX_N(0x6FCE, SFI_NONE, "EF.MMSN", F_OPTIONAL, 4, 32,
+		"MMS Notification"),
+	EF_LIN_FIX_N(0x6FCF, SFI_NONE, "EF.EXT8", F_OPTIONAL, 3, 16,
+		"Extension 8"),
+	EF_TRANSP_N(0x6FD0, SFI_NONE, "EF.MMSICP", F_OPTIONAL, 3, 256,
+		"MMS Issuer Connectivity Parameters"),
+	EF_LIN_FIX_N(0x6FD1, SFI_NONE, "EF.MMSUP", F_OPTIONAL, 1, 64,
+		"MMS User Preferences"),
+	EF_TRANSP_N(0x6FD2, SFI_NONE, "EF.MMSUCP", F_OPTIONAL, 1, 64,
+		"MMS User Connectivity Parameters"),
+	EF_LIN_FIX_N(0x6FD3, SFI_NONE, "EF.NIA", F_OPTIONAL, 2, 64,
+		"Network's Indication of Alerting"),
+	EF_TRANSP_N(0x6FB1, SFI_NONE, "EF.VGCS", F_OPTIONAL, 4, 80,
+		"Voice Group Call Service"),
+	EF_TRANSP_N(0x6FB2, SFI_NONE, "EF.VGCSS", F_OPTIONAL, 7, 7,
+		"Voice Group Call Service Status"),
+	EF_TRANSP_N(0x6FB3, SFI_NONE, "EF.VBS", F_OPTIONAL, 4, 80,
+		"Voice Broadcast Service"),
+	EF_TRANSP_N(0x6FB4, SFI_NONE, "EF.VBSS", F_OPTIONAL, 7, 7,
+		"Voice Broadcast Service Status"),
+	EF_TRANSP_N(0x6FD4, SFI_NONE, "EF.VGCSCA", F_OPTIONAL, 2, 40,
+		"Voice Group Call Service Ciphering Algorithm"),
+	EF_TRANSP_N(0x6FD5, SFI_NONE, "EF.VBSCA", F_OPTIONAL, 2, 40,
+		"Voice Broadcast Service Ciphering Algorithm"),
+	EF_TRANSP_N(0x6FD6, SFI_NONE, "EF.GBABP", F_OPTIONAL, 4, 128,
+		"GBA Bootstrapping parameters"),
+	EF_LIN_FIX_N(0x6FD7, SFI_NONE, "EF.MSK", F_OPTIONAL, 20, 84,
+		"MBMS Serviec Key List"),
+	EF_LIN_FIX_N(0x6FD8, SFI_NONE, "EF.MUK", F_OPTIONAL, 1, 128,
+		"MBMS User Key"),
+	EF_LIN_FIX_N(0x6FDA, SFI_NONE, "EF.GBANL", F_OPTIONAL, 1, 128,
+		"GBA NAF List"),
+	EF_TRANSP_N(0x6FD9, 0x1D, "EF.EHPLMN", F_OPTIONAL, 3, 30,
+		"Equivalent HPLMN"),
+	EF_TRANSP_N(0x6FDB, SFI_NONE, "EF.EHPLMNPI", F_OPTIONAL, 1, 1,
+		"Equivalent HPLMN Presentation Indication"),
+	EF_TRANSP_N(0x6FDC, SFI_NONE, "EF.LRPLMNSI", F_OPTIONAL, 1, 1,
+		"Last RPLMN Selection Indication"),
+	EF_LIN_FIX_N(0x6FDD, SFI_NONE, "EF.NAFKCA", F_OPTIONAL, 1, 128,
+		"NAF Key Centre Address"),
+	EF_TRANSP_N(0x6FDE, SFI_NONE, "EF.SPNI", F_OPTIONAL, 1, 128,
+		"Service Provider Name Icon"),
+	EF_LIN_FIX_N(0x6FDF, SFI_NONE, "EF.PNNI", F_OPTIONAL, 1, 128,
+		"PLMN Network Name Icon"),
+	EF_LIN_FIX_N(0x6FE2, SFI_NONE, "EF.NCP-IP", F_OPTIONAL, 2, 256,
+		"Network Connectivity Parameters for USIM IP Connections"),
+	EF_TRANSP_N(0x6FE3, 0x1E, "EF.EPSLOCI", F_OPTIONAL, 18, 18,
+		"EPS location information"),
+	EF_LIN_FIX_N(0x6FE4, 0x18, "EF.EPSNSC", F_OPTIONAL, 54, 128,
+		"EPS NAS Security Context"),
+	EF_TRANSP_N(0x6FE6, SFI_NONE, "EF.UFC", F_OPTIONAL, 1, 8,
+		"USAT Facility Control"),
+	EF_TRANSP_N(0x6FE8, SFI_NONE, "EF.NASCONFIG", F_OPTIONAL, 1, 128,
+		"Non Access Stratum Configuration"),
+	EF_LIN_FIX_N(0x6FE7, SFI_NONE, "EF.UICCIARI", F_OPTIONAL, 1, 32,
+		"UICC IARI"),
+	EF_TRANSP_N(0x6FEC, SFI_NONE, "EF.PWS", F_OPTIONAL, 1, 32,
+		"Public Warning System"),
+};
+
+
+
+/* 31.102 Chapter 4.4.1 */
+static const struct osim_file_desc usim_ef_in_solsa[] = {
+	EF_TRANSP_N(0x4F30, SFI_NONE, "EF.SAI", F_OPTIONAL, 2, 32,
+		"SoLSA Access Indicator"),
+	EF_LIN_FIX_N(0x4F31, SFI_NONE, "EF.SLL", F_OPTIONAL, 11, 32,
+		"SoLSA LSA List"),
+	/* LSA descriptor files 4Fxx, hard to represent here */
+};
+
+/* 31.102 Chapter 4.4.4 */
+static const struct osim_file_desc usim_ef_in_df_mexe[] = {
+	EF_TRANSP_N(0x4F40, SFI_NONE, "EF.MexE-ST", F_OPTIONAL, 1, 4,
+		"MexE Service table"),
+	EF_LIN_FIX_N(0x4F41, SFI_NONE, "EF.ORPK", F_OPTIONAL, 10, 16,
+		"Operator Root Public Key"),
+	EF_LIN_FIX_N(0x4F42, SFI_NONE, "EF.ARPK", F_OPTIONAL, 10, 16,
+		"Administrator Root Public Key"),
+	EF_LIN_FIX_N(0x4F43, SFI_NONE, "EF.TPRPK", F_OPTIONAL, 10, 16,
+		"Third Party Root Public Key"),
+	/* TKCDF files 4Fxx, hard to represent here */
+};
+
+/* 31.102 Chapter 4.4.5 */
+static const struct osim_file_desc usim_ef_in_df_wlan[] = {
+	EF_TRANSP_N(0x4F41, 0x01, "EF.Pseudo", F_OPTIONAL, 2, 16,
+		"Pseudonym"),
+	EF_TRANSP_N(0x4F42, 0x02, "EF.UPLMNWLAN", F_OPTIONAL, 30, 60,
+		"User controlled PLMN selector for I-WLAN Access"),
+	EF_TRANSP_N(0x4F43, 0x03, "EF.OPLMNWLAN", F_OPTIONAL, 30, 60,
+		"Operator controlled PLMN selector for I-WLAN Access"),
+	EF_LIN_FIX_N(0x4F44, 0x04, "EF.UWSIDL", F_OPTIONAL, 1, 16,
+		"User controlled WLAN Specific Identifier List"),
+	EF_LIN_FIX_N(0x4F45, 0x05, "EF.OWSIDL", F_OPTIONAL, 1, 16,
+		"Operator controlled WLAN Specific Identifier List"),
+	EF_TRANSP_N(0x4F46, 0x06, "EF.WRI", F_OPTIONAL, 1, 64,
+		"WLAN Reauthentication Identity"),
+	EF_LIN_FIX_N(0x4F47, 0x07, "EF.HWSIDL", F_OPTIONAL, 1, 16,
+		"Home I-WLAN Specific Identifier List"),
+	EF_TRANSP_N(0x4F48, 0x08, "EF.WEPLMNPI", F_OPTIONAL, 1, 1,
+		"I-WLAN Equivalent HPLMN Presentation Indication"),
+	EF_TRANSP_N(0x4F49, 0x09, "EF.WHPI", F_OPTIONAL, 1, 1,
+		"I-WLAN HPLMN Priority Indiation"),
+	EF_TRANSP_N(0x4F4A, 0x0a, "EF.WLRPLMN", F_OPTIONAL, 3, 3,
+		"I-WLAN Last Registered PLMN"),
+};
+
+/* 31.102 Chapter 4.4.6 */
+static const struct osim_file_desc usim_ef_in_df_hnb[] = {
+	EF_LIN_FIX_N(0x4F81, 0x01, "EF.ACSGL", F_OPTIONAL, 1, 128,
+		"Allowed CSG Lists"),
+	EF_LIN_FIX_N(0x4F82, 0x02, "EF.CSGT", F_OPTIONAL, 1, 64,
+		"CSG Type"),
+	EF_LIN_FIX_N(0x4F83, 0x03, "EF.HNBN", F_OPTIONAL, 1, 64,
+		"Home NodeB Name"),
+	EF_LIN_FIX_N(0x4F84, 0x04, "EF.OCSGL", F_OPTIONAL, 1, 64,
+		"Operator CSG List"),
+	EF_LIN_FIX_N(0x4F85, 0x05, "EF.OCSGT", F_OPTIONAL, 1, 64,
+		"Operator CSG Type"),
+	EF_LIN_FIX_N(0x4F86, 0x06, "EF.OHNBN", F_OPTIONAL, 1, 64,
+		"Oprator Home NodeB Name"),
+};
+
+/* Annex E - TS 101 220 */
+static const uint8_t adf_usim_aid[] = { 0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02 };
+
+struct osim_card_profile *osim_cprof_usim(void *ctx)
+{
+	struct osim_card_profile *cprof;
+	struct osim_file_desc *mf, *uadf;
+	int rc;
+
+	cprof = talloc_zero(ctx, struct osim_card_profile);
+	cprof->name = "3GPP USIM";
+	cprof->sws = usim_card_sws;
+
+	mf = alloc_df(cprof, 0x3f00, "MF");
+
+	cprof->mf = mf;
+
+	/* Core UICC Files */
+	add_filedesc(mf, uicc_ef_in_mf, ARRAY_SIZE(uicc_ef_in_mf));
+
+	/* ADF.USIM with its EF siblings */
+	uadf = add_adf_with_ef(mf, adf_usim_aid, sizeof(adf_usim_aid),
+				"ADF.USIM", usim_ef_in_adf_usim,
+				ARRAY_SIZE(usim_ef_in_adf_usim));
+
+	/* DFs under ADF.USIM */
+	add_df_with_ef(uadf, 0x5F3A, "DF.PHONEBOOK", NULL, 0);
+	add_df_with_ef(uadf, 0x5F3B, "DF.GSM-ACCESS", usim_ef_in_df_gsm_access,
+			ARRAY_SIZE(usim_ef_in_df_gsm_access));
+	add_df_with_ef(uadf, 0x5F3C, "DF.MexE", usim_ef_in_df_mexe,
+			ARRAY_SIZE(usim_ef_in_df_mexe));
+	add_df_with_ef(uadf, 0x5F40, "DF.WLAN", usim_ef_in_df_wlan,
+			ARRAY_SIZE(usim_ef_in_df_wlan));
+	/* Home-NodeB (femtocell) */
+	add_df_with_ef(uadf, 0x5F50, "DF.HNB", usim_ef_in_df_hnb,
+			ARRAY_SIZE(usim_ef_in_df_hnb));
+	/* Support of Localised Service Areas */
+	add_df_with_ef(uadf, 0x5F70, "DF.SoLSA", usim_ef_in_solsa,
+			ARRAY_SIZE(usim_ef_in_solsa));
+	/* OMA BCAST Smart Card Profile */
+	add_df_with_ef(uadf, 0x5F80, "DF.BCAST", NULL, 0);
+
+	/* DF.GSM and DF.TELECOM hierarchy as sub-directory of MF */
+	rc = osim_int_cprof_add_gsm(mf);
+	rc |= osim_int_cprof_add_telecom(mf);
+	if (rc != 0) {
+		talloc_free(cprof);
+		return NULL;
+	}
+
+	return cprof;
+}
diff --git a/src/sim/core.c b/src/sim/core.c
new file mode 100644
index 0000000..890df66
--- /dev/null
+++ b/src/sim/core.c
@@ -0,0 +1,312 @@
+/* Core routines for SIM/UICC/USIM access */
+/*
+ * (C) 2012 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/sim/sim.h>
+
+struct osim_decoded_data *osim_file_decode(struct osim_file *file,
+					   int len, uint8_t *data)
+{
+	struct osim_decoded_data *dd;
+
+	if (!file->desc->ops.parse)
+		return NULL;
+
+	dd = talloc_zero(file, struct osim_decoded_data);
+	if (!dd)
+		return NULL;
+	dd->file = file;
+
+	if (file->desc->ops.parse(dd, file->desc, len, data) < 0) {
+		talloc_free(dd);
+		return NULL;
+	} else
+		return dd;
+}
+
+struct msgb *osim_file_encode(const struct osim_file_desc *desc,
+				const struct osim_decoded_data *data)
+{
+	if (!desc->ops.encode)
+		return NULL;
+
+	return desc->ops.encode(desc, data);
+}
+
+static struct osim_decoded_element *
+__element_alloc(void *ctx, const char *name, enum osim_element_type type,
+		enum osim_element_repr repr)
+{
+	struct osim_decoded_element *elem;
+
+	elem = talloc_zero(ctx, struct osim_decoded_element);
+	if (!elem)
+		return NULL;
+	elem->name = name;
+	elem->type = type;
+	elem->representation = repr;
+
+	if (elem->type == ELEM_T_GROUP)
+		INIT_LLIST_HEAD(&elem->u.siblings);
+
+	return elem;
+}
+
+
+struct osim_decoded_element *
+element_alloc(struct osim_decoded_data *dd, const char *name,
+	      enum osim_element_type type, enum osim_element_repr repr)
+{
+	struct osim_decoded_element *elem;
+
+	elem = __element_alloc(dd, name, type, repr);
+	if (!elem)
+		return NULL;
+
+	llist_add_tail(&elem->list, &dd->decoded_elements);
+
+	return elem;
+}
+
+struct osim_decoded_element *
+element_alloc_sub(struct osim_decoded_element *ee, const char *name,
+	      enum osim_element_type type, enum osim_element_repr repr)
+{
+	struct osim_decoded_element *elem;
+
+	elem = __element_alloc(ee, name, type, repr);
+	if (!elem)
+		return NULL;
+
+	llist_add(&elem->list, &ee->u.siblings);
+
+	return elem;
+}
+
+
+void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num)
+{
+	int i;
+
+	for (i = 0; i < num; i++) {
+		struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in));
+		llist_add_tail(&ofd->list, &root->child_list);
+	}
+}
+
+struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name)
+{
+	struct osim_file_desc *mf;
+
+	mf = talloc_zero(ctx, struct osim_file_desc);
+	if (!mf)
+		return NULL;
+	mf->type = TYPE_DF;
+	mf->fid = fid;
+	mf->short_name = name;
+	INIT_LLIST_HEAD(&mf->child_list);
+
+	return mf;
+}
+
+struct osim_file_desc *
+add_df_with_ef(struct osim_file_desc *parent,
+		uint16_t fid, const char *name,
+		const struct osim_file_desc *in, int num)
+{
+	struct osim_file_desc *df;
+
+	df = alloc_df(parent, fid, name);
+	if (!df)
+		return NULL;
+	df->parent = parent;
+	llist_add_tail(&df->list, &parent->child_list);
+	add_filedesc(df, in, num);
+
+	return df;
+}
+
+struct osim_file_desc *
+add_adf_with_ef(struct osim_file_desc *parent,
+		const uint8_t *adf_name, uint8_t adf_name_len,
+		const char *name, const struct osim_file_desc *in,
+		int num)
+{
+	struct osim_file_desc *df;
+
+	df = alloc_df(parent, 0xffff, name);
+	if (!df)
+		return NULL;
+	df->type = TYPE_ADF;
+	df->df_name = adf_name;
+	df->df_name_len = adf_name_len;
+	df->parent = parent;
+	llist_add_tail(&df->list, &parent->child_list);
+	add_filedesc(df, in, num);
+
+	return df;
+}
+
+struct osim_file_desc *
+osim_file_find_name(struct osim_file_desc *parent, const char *name)
+{
+	struct osim_file_desc *ofd;
+	llist_for_each_entry(ofd, &parent->child_list, list) {
+		if (!strcmp(ofd->short_name, name)) {
+			return ofd;
+		}
+	}
+	return NULL;
+}
+
+/*! \brief Generate an APDU message and initialize APDU command header
+ *  \param[in] cla CLASS byte
+ *  \param[in] ins INSTRUCTION byte
+ *  \param[in] p1 Parameter 1 byte
+ *  \param[in] p2 Parameter 2 byte
+ *  \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc
+ *  \param[in] le maximum number of bytes expected in the response data field,  which will encoded in 0, 1, 2 or 3 bytes into Le
+ *  \returns an APDU message generated using provided APDU parameters
+ * 
+ * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) §5.1.
+ * The APDU command header, command and response fields lengths are initialized using the parameters.
+ * The APDU case is determined by the command and response fields lengths.
+ */
+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_apdu_cmd_hdr *ch;
+	struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU");
+	if (!msg)
+		return NULL;
+
+	ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch));
+	msg->l2h = (uint8_t *) ch;
+
+	ch->cla = cla;
+	ch->ins = ins;
+	ch->p1 = p1;
+	ch->p2 = p2;
+
+	msgb_apdu_lc(msg) = lc;
+	msgb_apdu_le(msg) = le;
+
+	if (lc == 0 && le == 0)
+		msgb_apdu_case(msg) = APDU_CASE_1;
+	else if (lc == 0 && le >= 1) {
+		if (le <= 256)
+			msgb_apdu_case(msg) = APDU_CASE_2S;
+		else
+			msgb_apdu_case(msg) = APDU_CASE_2E;
+	} else if (le == 0 && lc >= 1) {
+		if (lc <= 255)
+			msgb_apdu_case(msg) = APDU_CASE_3S;
+		else
+			msgb_apdu_case(msg) = APDU_CASE_3E;
+	} else if (lc >= 1 && le >= 1) {
+		if (lc <= 255 && le <= 256)
+			msgb_apdu_case(msg) = APDU_CASE_4S;
+		else
+			msgb_apdu_case(msg) = APDU_CASE_4E;
+	}
+
+	return msg;
+}
+
+/* FIXME: do we want to mark this as __thread? */
+static char sw_print_buf[256];
+
+char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
+{
+	const struct osim_card_sw *csw;
+
+	if (!ch || !ch->prof)
+		goto ret_def;
+
+	csw = osim_find_sw(ch->prof, sw_in);
+	if (!csw)
+		goto ret_def;
+
+	switch (csw->type) {
+	case SW_TYPE_STR:
+		snprintf(sw_print_buf, sizeof(sw_print_buf),
+			 "%04x (%s)", sw_in, csw->u.str);
+		break;
+	default:
+		goto ret_def;
+	}
+
+	sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
+
+	return sw_print_buf;
+
+ret_def:
+	snprintf(sw_print_buf, sizeof(sw_print_buf),
+		 "%04x (Unknown)", sw_in);
+	sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
+
+	return sw_print_buf;
+}
+
+
+const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
+					uint16_t sw_in)
+{
+	const struct osim_card_sw **sw_lists = cp->sws;
+	const struct osim_card_sw *sw_list, *sw;
+
+	for (sw_list = *sw_lists++; sw_list != NULL; sw = sw_list = *sw_lists++) {
+		for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) {
+			if ((sw_in & sw->mask) == sw->code)
+				return sw;
+		}
+	}
+	return NULL;
+}
+
+enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
+				      uint16_t sw_in)
+{
+	const struct osim_card_sw *csw = osim_find_sw(cp, sw_in);
+
+	if (!csw)
+		return SW_CLS_NONE;
+
+	return csw->class;
+}
+
+int default_decode(struct osim_decoded_data *dd,
+		   const struct osim_file_desc *desc,
+		   int len, uint8_t *data)
+{
+	struct osim_decoded_element *elem;
+
+	elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
+	elem->u.buf = talloc_memdup(elem, data, len);
+
+	return 0;
+}
diff --git a/src/sim/gsm_int.h b/src/sim/gsm_int.h
new file mode 100644
index 0000000..54a2fbf
--- /dev/null
+++ b/src/sim/gsm_int.h
@@ -0,0 +1,17 @@
+#include <sys/types.h>
+#include <osmocom/sim/sim.h>
+
+int osim_int_cprof_add_gsm(struct osim_file_desc *mf);
+int osim_int_cprof_add_telecom(struct osim_file_desc *mf);
+
+int gsm_hpplmn_decode(struct osim_decoded_data *dd,
+		     const struct osim_file_desc *desc,
+		     int len, uint8_t *data);
+
+int gsm_lp_decode(struct osim_decoded_data *dd,
+		 const struct osim_file_desc *desc,
+		 int len, uint8_t *data);
+
+int gsm_imsi_decode(struct osim_decoded_data *dd,
+		   const struct osim_file_desc *desc,
+		   int len, uint8_t *data);
diff --git a/src/sim/reader.c b/src/sim/reader.c
new file mode 100644
index 0000000..160f175
--- /dev/null
+++ b/src/sim/reader.c
@@ -0,0 +1,271 @@
+/* Card reader abstraction for libosmosim */
+/*
+ * (C) 2012 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <netinet/in.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/sim/sim.h>
+
+
+#include "sim_int.h"
+
+/* remove the SW from end of the message */
+static int get_sw(struct msgb *resp)
+{
+	int ret;
+
+	if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2)
+		return -EIO;
+
+	ret = msgb_get_u16(resp);
+
+	return ret;
+}
+
+/* According to ISO7816-4 Annex A */
+static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg)
+{
+	struct osim_reader_hdl *rh = st->reader;
+	struct msgb *tmsg = msgb_alloc(1024, "TPDU");
+	struct osim_apdu_cmd_hdr *tpduh;
+	uint8_t *cur;
+	uint16_t sw;
+	int rc, num_resp = 0;
+
+	/* create TPDU header from APDU header */
+	tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh));
+	memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh));
+
+	switch (msgb_apdu_case(amsg)) {
+	case APDU_CASE_1:
+		tpduh->p3 = 0x00;
+		break;
+	case APDU_CASE_2S:
+		tpduh->p3 = msgb_apdu_le(amsg);
+		break;
+	case APDU_CASE_2E:
+		if (msgb_apdu_le(amsg) <= 256) {
+			/* case 2E.1 */
+			tpduh->p3 = msgb_apdu_le(amsg) & 0xff;
+		} else {
+			/* case 2E.2 */
+			tpduh->p3 = 0;
+			msgb_put_u16(tmsg, msgb_apdu_le(amsg));
+		}
+		break;
+	case APDU_CASE_3S:
+	case APDU_CASE_4S:
+		tpduh->p3 = msgb_apdu_lc(amsg);
+		cur = msgb_put(tmsg, tpduh->p3);
+		memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3);
+		break;
+	case APDU_CASE_3E:
+	case APDU_CASE_4E:
+		if (msgb_apdu_lc(amsg) < 256) {
+			/* Case 3E.1 */
+			tpduh->p3 = msgb_apdu_lc(amsg);
+		} else {
+			/* Case 3E.2 */
+			/* FXIME: Split using ENVELOPE! */
+			return -1;
+		}
+		break;
+	}
+
+transceive_again:
+
+	/* store pointer to start of response */
+	tmsg->l3h = tmsg->tail;
+
+	/* transceive */
+	rc = rh->ops->transceive(st->reader, tmsg);
+	if (rc < 0) {
+		msgb_free(tmsg);
+		return rc;
+	}
+	msgb_apdu_sw(tmsg) = get_sw(tmsg);
+
+	/* increase number of responsese received */
+	num_resp++;
+
+	/* save SW */
+	sw = msgb_apdu_sw(tmsg);
+	printf("sw = 0x%04x\n", sw);
+	msgb_apdu_sw(amsg) = sw;
+
+	switch (msgb_apdu_case(amsg)) {
+	case APDU_CASE_1:
+	case APDU_CASE_3S:
+		/* just copy SW */
+		break;
+	case APDU_CASE_2S:
+case_2s:
+		switch (sw >> 8) {
+		case 0x67: /* Case 2S.2: Le definitely not accepted */
+			break;
+		case 0x6c: /* Case 2S.3: Le not accepted, La indicated */
+			tpduh->p3 = sw & 0xff;
+			/* re-issue the command with La as */
+			goto transceive_again;
+			break;
+		case 0x90:
+			/* Case 2S.1, fall-through */
+		case 0x91: case 0x92: case 0x93: case 0x94: case 0x95:
+		case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a:
+		case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
+			/* Case 2S.4 */
+			/* copy response data over */
+			cur = msgb_put(amsg, msgb_l3len(tmsg));
+			memcpy(cur, tmsg->l3h, msgb_l3len(tmsg));
+		}
+		break;
+	case APDU_CASE_4S:
+		/* FIXME: this is 4S.2 only for 2nd... response: */
+		if (num_resp >= 2)
+			goto case_2s;
+
+		switch (sw >> 8) {
+		case 0x60: case 0x62: case 0x63: case 0x64: case 0x65:
+		case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a:
+		case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
+			/* Case 4S.1: Command not accepted: just copy SW */
+			break;
+		case 0x90:
+			/* case 4S.2: Command accepted */
+			tpduh->ins = 0xC0;
+			tpduh->p1 = tpduh->p2 = 0;
+			tpduh->p3 = msgb_apdu_le(amsg);
+			/* strip off current result */
+			msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
+			goto transceive_again;
+			break;
+		case 0x61: /* Case 4S.3: command accepted with info added */
+		case 0x9F: /* FIXME: This is specific to SIM cards */
+			tpduh->ins = 0xC0;
+			tpduh->p1 = tpduh->p2 = 0;
+			tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff);
+			/* strip off current result */
+			msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh));
+			goto transceive_again;
+			break;
+		}
+		/* Case 4S.2: Command accepted: just copy SW */
+		/* Case 4S.4: Just copy SW */
+		break;
+	case APDU_CASE_2E:
+		if (msgb_apdu_le(amsg) <= 256) {
+			/* Case 2E.1: Le <= 256 */
+			goto case_2s;
+		}
+		switch (sw >> 8) {
+		case 0x67:
+			/* Case 2E.2a: wrong length, abort */
+			break;
+		case 0x6c:
+			/* Case 2E.2b: wrong length, La given */
+			tpduh->p3 = sw & 0xff;
+			/* re-issue the command with La as given */
+			goto transceive_again;
+			break;
+		case 0x90:
+			/* Case 2E.2c: */
+			break;
+		case 0x61:
+			/* Case 2E.2d: more data available */
+			/* FIXME: issue yet another GET RESPONSE */
+			break;
+		}
+		break;
+	case APDU_CASE_3E:
+		/* FIXME: handling for ENVELOPE splitting */
+		break;
+	case APDU_CASE_4E:
+		break;
+	}
+
+	msgb_free(tmsg);
+
+	/* compute total length of response data */
+	msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg);
+
+	return sw;
+}
+
+/* FIXME: T=1 According to ISO7816-4 Annex B */
+
+int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg)
+{
+	switch (st->card->proto) {
+	case OSIM_PROTO_T0:
+		return transceive_apdu_t0(st->card, amsg);
+	default:
+		return -ENOTSUP;
+	}
+}
+
+struct osim_reader_hdl *osim_reader_open(enum osim_reader_driver driver, int idx,
+					 const char *name, void *ctx)
+{
+	const struct osim_reader_ops *ops;
+	struct osim_reader_hdl *rh;
+
+	switch (driver) {
+	case OSIM_READER_DRV_PCSC:
+		ops = &pcsc_reader_ops;
+		break;
+	default:
+		return NULL;
+	}
+
+	rh = ops->reader_open(idx, name, ctx);
+	if (!rh)
+		return NULL;
+	rh->ops = ops;
+
+	/* FIXME: for now we only do T=0 on all readers */
+	rh->proto_supported = (1 << OSIM_PROTO_T0);
+
+	return rh;
+}
+
+struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh, enum osim_proto proto)
+{
+	struct osim_card_hdl *ch;
+
+	if (!(rh->proto_supported & (1 << proto)))
+		return NULL;
+
+	ch = rh->ops->card_open(rh, proto);
+	if (!ch)
+		return NULL;
+
+	ch->proto = proto;
+
+	return ch;
+}
diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c
new file mode 100644
index 0000000..5e67091
--- /dev/null
+++ b/src/sim/reader_pcsc.c
@@ -0,0 +1,161 @@
+/* PC/SC Card reader backend for libosmosim */
+/*
+ * (C) 2012 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+
+#include <string.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/sim/sim.h>
+
+#include <PCSC/wintypes.h>
+#include <PCSC/winscard.h>
+
+#include "sim_int.h"
+
+#define PCSC_ERROR(rv, text) \
+if (rv != SCARD_S_SUCCESS) { \
+	fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
+	goto end; \
+} else { \
+        printf(text ": OK\n\n"); \
+}
+
+
+
+struct pcsc_reader_state {
+	SCARDCONTEXT hContext;
+	SCARDHANDLE hCard;
+	DWORD dwActiveProtocol;
+	const SCARD_IO_REQUEST *pioSendPci;
+	SCARD_IO_REQUEST pioRecvPci;
+	char *name;
+};
+
+static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *ctx)
+{
+	struct osim_reader_hdl *rh;
+	struct pcsc_reader_state *st;
+	LONG rc;
+	LPSTR mszReaders = NULL;
+	DWORD dwReaders;
+	unsigned int num_readers;
+	char *ptr;
+
+	/* FIXME: implement matching on id or num */
+
+	rh = talloc_zero(ctx, struct osim_reader_hdl);
+	st = rh->priv = talloc_zero(rh, struct pcsc_reader_state);
+
+	rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL,
+				   &st->hContext);
+	PCSC_ERROR(rc, "SCardEstablishContext");
+	if (rc != SCARD_S_SUCCESS)
+		goto end;
+
+	dwReaders = SCARD_AUTOALLOCATE;
+	rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
+	PCSC_ERROR(rc, "SCardListReaders");
+
+	num_readers = 0;
+	ptr = mszReaders;
+	while (*ptr != '\0') {
+		ptr += strlen(ptr)+1;
+		num_readers++;
+	}
+
+	if (num_readers == 0)
+		goto end;
+
+	st->name = talloc_strdup(rh, mszReaders);
+	st->dwActiveProtocol = -1;
+
+	return rh;
+end:
+	talloc_free(rh);
+	return NULL;
+}
+
+static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh,
+					    enum osim_proto proto)
+{
+	struct pcsc_reader_state *st = rh->priv;
+	struct osim_card_hdl *card;
+	struct osim_chan_hdl *chan;
+	LONG rc;
+
+	if (proto != OSIM_PROTO_T0)
+		return NULL;
+
+	rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED,
+			  SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol);
+	PCSC_ERROR(rc, "SCardConnect");
+
+	st->pioSendPci = SCARD_PCI_T0;
+
+	card = talloc_zero(rh, struct osim_card_hdl);
+	INIT_LLIST_HEAD(&card->channels);
+	card->reader = rh;
+	rh->card = card;
+
+	/* create a default channel */
+	chan = talloc_zero(card, struct osim_chan_hdl);
+	chan->card = card;
+	llist_add(&chan->list, &card->channels);
+
+	return card;
+
+end:
+	return NULL;
+}
+
+
+static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg)
+{
+	struct pcsc_reader_state *st = rh->priv;
+	DWORD rlen = msgb_tailroom(msg);
+	LONG rc;
+
+	printf("TX: %s\n", osmo_hexdump(msg->data, msg->len));
+
+	rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg),
+			   &st->pioRecvPci, msg->tail, &rlen);
+	PCSC_ERROR(rc, "SCardEndTransaction");
+
+	printf("RX: %s\n", osmo_hexdump(msg->tail, rlen));
+	msgb_put(msg, rlen);
+	msgb_apdu_le(msg) = rlen;
+
+	return 0;
+end:
+	return -EIO;
+}
+
+const struct osim_reader_ops pcsc_reader_ops = {
+	.name = "PC/SC",
+	.reader_open = pcsc_reader_open,
+	.card_open = pcsc_card_open,
+	.transceive = pcsc_transceive,
+};
+
diff --git a/src/sim/sim_int.h b/src/sim/sim_int.h
new file mode 100644
index 0000000..c10c5f0
--- /dev/null
+++ b/src/sim/sim_int.h
@@ -0,0 +1,41 @@
+#ifndef _SIM_INT_H
+
+#include <osmocom/sim/sim.h>
+
+struct osim_decoded_element *
+element_alloc(struct osim_decoded_data *dd, const char *name,
+	      enum osim_element_type type, enum osim_element_repr repr);
+
+struct osim_decoded_element *
+element_alloc_sub(struct osim_decoded_element *ee, const char *name,
+	      enum osim_element_type type, enum osim_element_repr repr);
+
+extern const struct osim_card_sw ts102221_uicc_sw[0];
+
+int default_decode(struct osim_decoded_data *dd,
+		   const struct osim_file_desc *desc,
+		   int len, uint8_t *data);
+
+void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num);
+struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name);
+struct osim_file_desc *
+add_df_with_ef(struct osim_file_desc *parent,
+		uint16_t fid, const char *name,
+		const struct osim_file_desc *in, int num);
+
+struct osim_file_desc *
+add_adf_with_ef(struct osim_file_desc *parent,
+		const uint8_t *adf_name, uint8_t adf_name_len,
+		const char *name, const struct osim_file_desc *in,
+		int num);
+
+struct osim_reader_ops {
+	const char *name;
+	struct osim_reader_hdl *(*reader_open)(int idx, const char *name, void *ctx);
+	struct osim_card_hdl *(*card_open)(struct osim_reader_hdl *rh, enum osim_proto proto);
+	int (*transceive)(struct osim_reader_hdl *rh, struct msgb *msg);
+};
+
+const struct osim_reader_ops pcsc_reader_ops;
+
+#endif
diff --git a/utils/Makefile.am b/utils/Makefile.am
index 51c884a..7d784bd 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -3,10 +3,15 @@
 AM_CFLAGS = -Wall
 
 bin_PROGRAMS = osmo-arfcn osmo-auc-gen
+noinst_PROGRAMS = osmo-sim-test
 
 osmo_arfcn_SOURCES = osmo-arfcn.c
 osmo_arfcn_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la
 
 osmo_auc_gen_SOURCES = osmo-auc-gen.c
 osmo_auc_gen_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la
+
+osmo_sim_test_SOURCES = osmo-sim-test.c
+osmo_sim_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/sim/libosmosim.la -lpcsclite
+osmo_sim_test_CFLAGS = -I/usr/include/PCSC
 endif
diff --git a/utils/osmo-sim-test.c b/utils/osmo-sim-test.c
new file mode 100644
index 0000000..244c2df
--- /dev/null
+++ b/utils/osmo-sim-test.c
@@ -0,0 +1,419 @@
+/* libosmosim test application - currently simply dumps a USIM */
+/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/sim/sim.h>
+#include <osmocom/gsm/tlv.h>
+
+
+/* FIXME: this needs to be moved to card_fs_uicc.c */
+
+/* 11.1.1 */
+static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
+			const uint8_t *data, uint8_t data_len)
+{
+	struct msgb *msg, *resp;
+	char *dst;
+
+	msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256);
+	dst = msgb_put(msg, data_len);
+	memcpy(dst, data, data_len);
+
+	osim_transceive_apdu(st, msg);
+
+	return msg;
+}
+
+/* 11.1.1 */
+static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
+{
+	int sw;
+
+	return _select_file(st, 0x04, 0x04, adf,adf_len);
+}
+
+/* 11.1.1 */
+static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
+{
+	uint16_t cfid = htons(fid);
+
+	return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2);
+}
+
+/* 11.1.9 */
+static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, uint8_t *pin)
+{
+	struct msgb *msg;
+	char *pindst;
+	int sw;
+
+	if (strlen(pin) > 8)
+		return -EINVAL;
+
+	msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0);
+	pindst = msgb_put(msg, 8);
+	memset(pindst, 0xFF, 8);
+	strncpy(pindst, pin, strlen(pin));
+
+	return osim_transceive_apdu(st, msg);
+}
+
+/* 11.1.5 */
+static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
+{
+	struct msgb *msg;
+
+	msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size);
+
+	osim_transceive_apdu(st, msg);
+
+	return msg;
+}
+
+/* 11.1.6 */
+static struct msgb *update_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr,
+				     const uint8_t *data, uint16_t rec_size)
+{
+	struct msgb *msg;
+	uint8_t *cur;
+
+	msg = osim_new_apdumsg(0x00, 0xDC, rec_nr, 0x04, rec_size, 0);
+	cur = msgb_put(msg, rec_size);
+	memcpy(cur, data, rec_size);
+
+	osim_transceive_apdu(st, msg);
+
+	return msg;
+}
+
+/* 11.1.3 */
+static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
+{
+	struct msgb *msg;
+
+	if (offset > 0x7fff || len > 256)
+		return NULL;
+
+	msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
+
+	osim_transceive_apdu(st, msg);
+
+	return msg;
+}
+
+/* 11.1.4 */
+static struct msgb *update_binary(struct osim_chan_hdl *st, uint16_t offset,
+				  const uint8_t *data, uint16_t len)
+{
+	struct msgb *msg;
+	uint8_t *cur;
+
+	if (offset > 0x7fff || len > 256)
+		return NULL;
+
+	msg = osim_new_apdumsg(0x00, 0xD6, offset >> 8, offset & 0xff, len & 0xff, 0);
+	cur = msgb_put(msg, len);
+	memcpy(cur, data, len);
+
+	osim_transceive_apdu(st, msg);
+
+	return msg;
+}
+
+
+
+static int dump_fcp_template(struct tlv_parsed *tp)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
+		if (TLVP_PRESENT(tp, i))
+			printf("Tag 0x%02x (%s): %s\n", i,
+				get_value_string(ts102221_fcp_vals, i),
+				osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
+	}
+
+	return 0;
+}
+
+static int dump_fcp_template_msg(struct msgb *msg)
+{
+	struct tlv_parsed tp;
+	int rc;
+
+	rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
+	if (rc < 0)
+		return rc;
+
+	return dump_fcp_template(&tp);
+}
+
+struct osim_fcp_fd_decoded {
+	enum osim_file_type type;
+	enum osim_ef_type ef_type;
+	uint16_t rec_len;
+	uint8_t num_rec;
+};
+
+static const enum osim_file_type iso2ftype[8] = {
+	[0] = TYPE_EF,
+	[1] = TYPE_EF_INT,
+	[7] = TYPE_DF,
+};
+
+static const enum osim_ef_type iso2eftype[8] = {
+	[1] = EF_TYPE_TRANSP,
+	[2] = EF_TYPE_RECORD_FIXED,
+	[6] = EF_TYPE_RECORD_CYCLIC,
+};
+
+static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
+{
+	memset(ofd, 0, sizeof(*ofd));
+
+	if (fcp_len != 2 && fcp_len != 5)
+		return -EINVAL;
+
+	ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
+	if (ofd->type != TYPE_DF)
+		ofd->ef_type = iso2eftype[fcp[0] & 7];
+
+	if (fcp[1] != 0x21)
+		return -EINVAL;
+
+	if (fcp_len >= 5) {
+		ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
+		ofd->num_rec = fcp[4];
+	}
+
+	return 0;
+}
+
+extern struct osim_card_profile *osim_cprof_usim(void *ctx);
+
+static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
+{
+	struct tlv_parsed tp;
+	struct osim_fcp_fd_decoded ofd;
+	struct msgb *msg, *msg2;
+	uint8_t *cur;
+	int rc, i;
+
+	msg = select_file(st, 0x2f00);
+	rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
+	if (rc < 0)
+		return NULL;
+
+	dump_fcp_template(&tp);
+
+	if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
+	    TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
+		msgb_free(msg);
+		return NULL;
+	}
+
+	rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
+				TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
+	if (rc < 0) {
+		msgb_free(msg);
+		return NULL;
+	}
+
+	if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
+		msgb_free(msg);
+		return NULL;
+	}
+
+	msgb_free(msg);
+
+	printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
+
+	for (i = 0; i < ofd.num_rec; i++) {
+		msg = read_record_nr(st, i+1, ofd.rec_len);
+		if (!msg)
+			return NULL;
+
+		cur = msgb_apdu_de(msg);
+		if (msgb_apdu_le(msg) < 5) {
+			msgb_free(msg);
+			return NULL;
+		}
+
+		if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
+		    cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
+			msgb_free(msg);
+			return NULL;
+		}
+
+		/* FIXME: actually check if it is an AID that we support, or
+		 * iterate until we find one that we support */
+
+		msg2 = select_adf(st, cur+4, cur[3]);
+
+		/* attach the USIM profile, FIXME: do this based on AID match */
+		st->card->prof = osim_cprof_usim(st->card);
+		st->cwd = osim_file_find_name(st->card->prof->mf, "ADF.USIM");
+
+		msgb_free(msg);
+
+		return msg2;
+	}
+
+	return NULL;
+}
+
+static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
+{
+	struct tlv_parsed tp;
+	struct osim_fcp_fd_decoded ffdd;
+	struct msgb *msg, *rmsg;
+	int rc, i, offset;
+
+	msg = select_file(chan, fid);
+	if (!msg) {
+		printf("Unable to select file\n");
+		return -EIO;
+	}
+	printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
+	if (msgb_apdu_sw(msg) != 0x9000) {
+		printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
+		goto out;
+	}
+
+	rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
+	if (rc < 0) {
+		printf("Unable to parse FCP\n");
+		goto out;
+	}
+
+	if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
+	    TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
+		printf("No file descriptor present ?!?\n");
+		goto out;
+	}
+
+	rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
+				TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
+	if (rc < 0) {
+		printf("Unable to decode File Descriptor\n");
+		goto out;
+	}
+
+	if (ffdd.type != TYPE_EF) {
+		printf("File Type != EF\n");
+		goto out;
+	}
+
+	printf("EF type: %u\n", ffdd.ef_type);
+
+	switch (ffdd.ef_type) {
+	case EF_TYPE_RECORD_FIXED:
+		for (i = 0; i < ffdd.num_rec; i++) {
+			rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
+			if (!msg)
+				return -EIO;
+			printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
+			printf("Rec %03u: %s\n", i+1,
+				osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
+		}
+		break;
+	case EF_TYPE_TRANSP:
+		if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
+			goto out;
+		i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
+		printf("File size: %d bytes\n", i);
+
+		for (offset = 0; offset < i-1; ) {
+			uint16_t remain_len = i - offset;
+			uint16_t read_len = OSMO_MIN(remain_len, 256);
+			rmsg = read_binary(chan, offset, read_len);
+			if (!msg)
+				return -EIO;
+			offset += read_len;
+			printf("Content: %s\n",
+				osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
+		}
+		break;
+	default:
+		goto out;
+	}
+
+out:
+	msgb_free(msg);
+	return -EINVAL;
+}
+
+int main(int argc, char **argv)
+{
+	struct osim_reader_hdl *reader;
+	struct osim_card_hdl *card;
+	struct osim_chan_hdl *chan;
+	struct msgb *msg;
+	int rc;
+
+	reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
+	if (!reader)
+		exit(1);
+	card = osim_card_open(reader, OSIM_PROTO_T0);
+	if (!card)
+		exit(2);
+	chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
+	if (!chan)
+		exit(3);
+
+	msg = try_select_adf_usim(chan);
+	if (!msg || msgb_apdu_sw(msg) != 0x9000)
+		exit(4);
+	dump_fcp_template_msg(msg);
+	msgb_free(msg);
+
+	msg = select_file(chan, 0x6fc5);
+	dump_fcp_template_msg(msg);
+	printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
+	msgb_free(msg);
+
+	verify_pin(chan, 1, "1653");
+
+	msg = select_file(chan, 0x6f06);
+	dump_fcp_template_msg(msg);
+	msgb_free(msg);
+
+	{
+		struct osim_file_desc *ofd;
+		llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
+			struct msgb *m;
+			printf("\n\n================ %s (%s) ==================\n",
+				ofd->short_name, ofd->long_name);
+
+			m = select_file(chan, ofd->fid);
+			dump_fcp_template_msg(m);
+			msgb_free(m);
+			dump_file(chan, ofd->fid);
+		}
+	}
+
+	exit(0);
+}