sim: re-structure how we support cards + applications

Before this change, a card application (USIM, ISIM, ...) didn't
exist as a separate concept from a card profile.  This meant,
we had a manual combination of UICC card profile with USIM application,
and another one of UICC card profile and ISIM application.  But what
if there's a combined USIM+ISIM?

In reality, applications exist as separate objects, on top of an
ETSI UICC.  Lets therefore register all known applications to the
osim library core, and add code to osmo-sim-test which dynamically
detects all applications present on a given card (by reading EF.DIR).

Change-Id: Ic4b4ac433a9976842b30a017fb0fc347d87201cd
diff --git a/src/sim/card_fs_usim.c b/src/sim/card_fs_usim.c
index 3b33c0f..97f712a 100644
--- a/src/sim/card_fs_usim.c
+++ b/src/sim/card_fs_usim.c
@@ -1,7 +1,7 @@
 /*! \file card_fs_usim.c
  * 3GPP USIM specific structures / routines. */
 /*
- * (C) 2012-2014 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2012-2020 by Harald Welte <laforge@gnumonks.org>
  *
  * All Rights Reserved
  *
@@ -47,24 +47,6 @@
 	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,
@@ -330,27 +312,21 @@
 /* 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_app_profile *osim_aprof_usim(void *ctx)
 {
-	struct osim_card_profile *cprof;
-	struct osim_file_desc *mf, *uadf;
-	int rc;
+	struct osim_card_app_profile *aprof;
+	struct osim_file_desc *uadf;
 
-	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));
+	aprof = talloc_zero(ctx, struct osim_card_app_profile);
+	aprof->name = "3GPP USIM";
+	aprof->sw = ts31_102_sw;
+	aprof->aid_len = sizeof(adf_usim_aid);
+	memcpy(aprof->aid, adf_usim_aid, aprof->aid_len);
 
 	/* 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));
+	uadf = alloc_adf_with_ef(aprof, adf_usim_aid, sizeof(adf_usim_aid),
+				 "ADF.USIM", usim_ef_in_adf_usim, ARRAY_SIZE(usim_ef_in_adf_usim));
+	aprof->adf = uadf;
 
 	/* DFs under ADF.USIM */
 	add_df_with_ef(uadf, 0x5F3A, "DF.PHONEBOOK", NULL, 0);
@@ -369,13 +345,14 @@
 	/* 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 */
+#if 0
+	/* DF.GSM 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;
 	}
+#endif
 
-	return cprof;
+	return aprof;
 }