changeset 104:a60645b75a57

fc-simtool: phonebook dump implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 25 Jan 2021 05:55:40 +0000
parents 90eff13a72fd
children b1bf0ec6fff5
files simtool/Makefile simtool/dispatch.c simtool/pbcommon.c simtool/pbdump.c
diffstat 4 files changed, 215 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Mon Jan 25 04:54:47 2021 +0000
+++ b/simtool/Makefile	Mon Jan 25 05:55:40 2021 +0000
@@ -2,8 +2,9 @@
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-simtool
 OBJS=	alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o dispatch.o \
-	globals.o hexdump.o hexread.o hlread.o main.o names.o readcmd.o \
-	readops.o saverestore.o select.o telsum.o writecmd.o writeops.o
+	globals.o hexdump.o hexread.o hlread.o main.o names.o pbcommon.o \
+	pbdump.o readcmd.o readops.o saverestore.o select.o telsum.o writecmd.o\
+	writeops.o
 
 all:	${PROG}
 
--- a/simtool/dispatch.c	Mon Jan 25 04:54:47 2021 +0000
+++ b/simtool/dispatch.c	Mon Jan 25 05:55:40 2021 +0000
@@ -14,6 +14,7 @@
 
 extern int cmd_iccid();
 extern int cmd_imsi();
+extern int cmd_pb_dump();
 extern int cmd_readbin();
 extern int cmd_readef();
 extern int cmd_readrec();
@@ -43,6 +44,7 @@
 	{"exit", 0, 0, cmd_exit},
 	{"iccid", 0, 0, cmd_iccid},
 	{"imsi", 0, 0, cmd_imsi},
+	{"pb-dump", 1, 2, cmd_pb_dump},
 	{"quit", 0, 0, cmd_exit},
 	{"readbin", 2, 2, cmd_readbin},
 	{"readef", 1, 1, cmd_readef},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/pbcommon.c	Mon Jan 25 05:55:40 2021 +0000
@@ -0,0 +1,71 @@
+/*
+ * This module implements the common functions for all phonebook commands.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+#include "file_id.h"
+
+static struct map {
+	char	*user_name;
+	char	*canon_name;
+	int	file_id;
+} phonebook_map[] = {
+	{"adn",		"EF_ADN",	EF_ADN},
+	{"ADN",		"EF_ADN",	EF_ADN},
+	{"EF_ADN",	"EF_ADN",	EF_ADN},
+	{"fdn",		"EF_FDN",	EF_FDN},
+	{"FDN",		"EF_FDN",	EF_FDN},
+	{"EF_FDN",	"EF_FDN",	EF_FDN},
+	{"sdn",		"EF_SDN",	EF_SDN},
+	{"SDN",		"EF_SDN",	EF_SDN},
+	{"EF_SDN",	"EF_SDN",	EF_SDN},
+	{"msisdn",	"EF_MSISDN",	EF_MSISDN},
+	{"MSISDN",	"EF_MSISDN",	EF_MSISDN},
+	{"EF_MSISDN",	"EF_MSISDN",	EF_MSISDN},
+	/* table search terminator */
+	{0,		0,		-1}
+};
+
+phonebook_op_common(reqname)
+	char *reqname;
+{
+	struct map *tp;
+	int rc;
+
+	for (tp = phonebook_map; *tp->user_name; tp++)
+		if (!strcmp(tp->user_name, reqname))
+			break;
+	if (!tp->canon_name) {
+		fprintf(stderr, "error: phone book name \"%s\" not known",
+			reqname);
+		return(-1);
+	}
+	rc = select_op(DF_TELECOM);
+	if (rc < 0)
+		return(rc);
+	rc = select_op(tp->file_id);
+	if (rc < 0)
+		return(rc);
+	rc = parse_ef_select_response();
+	if (rc < 0)
+		return(rc);
+	if (curfile_structure != 0x01) {
+		fprintf(stderr, "error: %s is not linear fixed\n",
+			tp->canon_name);
+		return(-1);
+	}
+	if (curfile_record_len < 14) {
+		fprintf(stderr,
+	"error: %s has record length of %u bytes, less than minimum 14\n",
+			tp->canon_name, curfile_record_len);
+		return(-1);
+	}
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/pbdump.c	Mon Jan 25 05:55:40 2021 +0000
@@ -0,0 +1,139 @@
+/*
+ * This module implements the pb-dump command.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+
+static char gsm_address_digits[16] =
+	{'0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'};
+
+static
+check_all_blank()
+{
+	u_char *dp, *endp;
+
+	dp = sim_resp_data;
+	endp = sim_resp_data + sim_resp_data_len;
+	while (dp < endp)
+		if (*dp++ != 0xFF)
+			return(0);
+	return(1);
+}
+
+static
+decode_number(data, nbytes, out)
+	u_char *data;
+	unsigned nbytes;
+	char *out;
+{
+	u_char *dp, *endp;
+	int c;
+
+	dp = data;
+	endp = data + nbytes;
+	while (dp < endp) {
+		c = *dp & 0xF;
+		if (c == 0xF)
+			return(-1);
+		*out++ = gsm_address_digits[c];
+		c = *dp >> 4;
+		if (c == 0xF) {
+			if (dp + 1 == endp)
+				break;
+			else
+				return(-1);
+		}
+		*out++ = gsm_address_digits[c];
+		dp++;
+	}
+	*out = '\0';
+	return(0);
+}
+
+static
+check_blank_area(dp, endp)
+	u_char *dp, *endp;
+{
+	while (dp < endp)
+		if (*dp++ != 0xFF)
+			return(-1);
+	return(0);
+}
+
+static void
+dump_record(recno, outf)
+	unsigned recno;
+	FILE *outf;
+{
+	int rc;
+	unsigned textlen;
+	u_char *fixp;
+	char digits[21];
+
+	fprintf(outf, "#%u: ", recno);
+	if (curfile_record_len > 14) {
+		rc = validate_alpha_field(sim_resp_data,
+					  curfile_record_len - 14,
+					  &textlen);
+		if (rc < 0) {
+malformed:		fprintf(outf, "malformed record\n");
+			return;
+		}
+	} else
+		textlen = 0;
+	fixp = sim_resp_data + sim_resp_data_len - 14;
+	if (fixp[0] < 2 || fixp[0] > 11)
+		goto malformed;
+	rc = decode_number(fixp + 2, fixp[0] - 1, digits);
+	if (rc < 0)
+		goto malformed;
+	rc = check_blank_area(fixp + 1 + fixp[0], fixp + 12);
+	if (rc < 0)
+		goto malformed;
+	/* all checks passed */
+	fprintf(outf, "%s,0x%02X ", digits, fixp[1]);
+	if (fixp[12] != 0xFF)
+		fprintf(outf, "CCP=%u ", fixp[12]);
+	if (fixp[13] != 0xFF)
+		fprintf(outf, "EXT=%u ", fixp[12]);
+	print_alpha_field(sim_resp_data, textlen, outf);
+}
+
+cmd_pb_dump(argc, argv)
+	char **argv;
+{
+	int rc;
+	FILE *outf;
+	unsigned recno;
+
+	rc = phonebook_op_common(argv[1]);
+	if (argv[2]) {
+		outf = fopen(argv[2], "w");
+		if (!outf) {
+			perror(argv[2]);
+			return(-1);
+		}
+	} else
+		outf = stdout;
+	for (recno = 1; recno <= curfile_record_count; recno++) {
+		rc = readrec_op(recno, 0x04, curfile_record_len);
+		if (rc < 0) {
+			if (argv[2])
+				fclose(outf);
+			return(rc);
+		}
+		if (check_all_blank())
+			continue;
+		dump_record(recno, outf);
+	}
+	if (argv[2])
+		fclose(outf);
+	return(0);
+}