changeset 89:db131929ee96

fc-uicc-tool: extended readef ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:17:58 +0000
parents 49b7e02787c1
children 3afa61d98961
files uicc/Makefile uicc/readcmd.c uicc/readef.c uicc/select.c
diffstat 4 files changed, 132 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/uicc/Makefile	Sun Apr 11 04:01:21 2021 +0000
+++ b/uicc/Makefile	Sun Apr 11 04:17:58 2021 +0000
@@ -3,7 +3,7 @@
 CPPFLAGS=-I../libcommon
 PROG=	fc-uicc-tool
 OBJS=	bfsearch.o cmdtab.o createfile.o dumpdir.o getresp.o hlread.o main.o \
-	pins.o readcmd.o readops.o select.o sws.o writecmd.o writeops.o
+	pins.o readcmd.o readef.o readops.o select.o sws.o writecmd.o writeops.o
 LIBS=	../libcommon/libcommon.a ../libutil/libutil.a
 
 INSTALL_PREFIX=	/opt/freecalypso
--- a/uicc/readcmd.c	Sun Apr 11 04:01:21 2021 +0000
+++ b/uicc/readcmd.c	Sun Apr 11 04:17:58 2021 +0000
@@ -1,5 +1,8 @@
+/*
+ * This module implements elementary low-level readbin and readrec commands.
+ */
+
 #include <sys/types.h>
-#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "simresp.h"
@@ -64,39 +67,3 @@
 	display_sim_resp_in_hex(outf);
 	return(0);
 }
-
-cmd_readef(argc, argv, outf)
-	char **argv;
-	FILE *outf;
-{
-	int file_id, rc;
-	unsigned file_len, readlen;
-
-	if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
-	    isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
-		file_id = strtoul(argv[1], 0, 16);
-	else
-		file_id = find_symbolic_file_name(argv[1]);
-	if (file_id < 0) {
-		fprintf(stderr,
-"error: file ID argument is not a hex value or a recognized symbolic name\n");
-		return(-1);
-	}
-	rc = select_op(file_id);
-	if (rc < 0)
-		return(rc);
-	rc = select_resp_get_transparent(&file_len);
-	if (rc < 0)
-		return(rc);
-	fprintf(outf, "Transparent EF of %u byte(s)\n", file_len);
-	if (!file_len)
-		return(0);
-	readlen = file_len;
-	if (readlen > 256)
-		readlen = 256;
-	rc = readbin_op(0, readlen);
-	if (rc < 0)
-		return(rc);
-	display_sim_resp_in_hex(outf);
-	return(0);
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/readef.c	Sun Apr 11 04:17:58 2021 +0000
@@ -0,0 +1,122 @@
+/*
+ * This module implements the readef command for dumping the complete
+ * content of SIM files.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "simresp.h"
+#include "efstruct.h"
+
+static void
+hexdump_with_offset(outf, extoff)
+	FILE *outf;
+	unsigned extoff;
+{
+	unsigned off, cc, n, c;
+
+	for (off = 0; off < sim_resp_data_len; off += cc) {
+		fprintf(outf, "%04X:", extoff + off);
+		cc = 16;
+		if (sim_resp_data_len - off < cc)
+			cc = sim_resp_data_len - off;
+		for (n = 0; n < 16; n++) {
+			if (n == 0 || n == 8)
+				putc(' ', outf);
+			putc(' ', outf);
+			if (n < cc)
+				fprintf(outf, "%02X", sim_resp_data[off + n]);
+			else {
+				putc(' ', outf);
+				putc(' ', outf);
+			}
+		}
+		putc(' ', outf);
+		putc(' ', outf);
+		for (n = 0; n < cc; n++) {
+			c = sim_resp_data[off + n];
+			if (c < 0x20 || c > 0x7E)
+				c = '.';
+			putc(c, outf);
+		}
+		putc('\n', outf);
+	}
+}
+
+static
+readef_transparent(efs, outf)
+	struct ef_struct *efs;
+	FILE *outf;
+{
+	unsigned off, cc;
+	int rc;
+
+	for (off = 0; off < efs->total_size; off += cc) {
+		cc = efs->total_size - off;
+		if (cc > 256)
+			cc = 256;
+		rc = readbin_op(off, cc);
+		if (rc < 0)
+			return(rc);
+		hexdump_with_offset(outf, off);
+	}
+	return(0);
+}
+
+static
+readef_records(efs, outf)
+	struct ef_struct *efs;
+	FILE *outf;
+{
+	unsigned recno;
+	int rc;
+
+	for (recno = 1; recno <= efs->record_count; recno++) {
+		fprintf(outf, "Record #%u:\n", recno);
+		rc = readrec_op(recno, 0x04, efs->record_len);
+		if (rc < 0)
+			return(rc);
+		display_sim_resp_in_hex(outf);
+	}
+	return(0);
+}
+
+cmd_readef(argc, argv, outf)
+	char **argv;
+	FILE *outf;
+{
+	int file_id, rc;
+	struct ef_struct efs;
+
+	if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
+	    isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
+		file_id = strtoul(argv[1], 0, 16);
+	else
+		file_id = find_symbolic_file_name(argv[1]);
+	if (file_id < 0) {
+		fprintf(stderr,
+"error: file ID argument is not a hex value or a recognized symbolic name\n");
+		return(-1);
+	}
+	rc = select_op(file_id);
+	if (rc < 0)
+		return(rc);
+	rc = select_resp_get_ef_struct(&efs);
+	if (rc < 0)
+		return(rc);
+	switch (efs.structure) {
+	case 0x01:
+		fprintf(outf, "Transparent EF of %u byte(s)\n", efs.total_size);
+		return readef_transparent(&efs, outf);
+	case 0x02:
+		fprintf(outf, "%u records of %u bytes (linear fixed)\n",
+			efs.record_count, efs.record_len);
+		return readef_records(&efs, outf);
+	case 0x06:
+		fprintf(outf, "%u records of %u bytes (cyclic)\n",
+			efs.record_count, efs.record_len);
+		return readef_records(&efs, outf);
+	}
+}
--- a/uicc/select.c	Sun Apr 11 04:01:21 2021 +0000
+++ b/uicc/select.c	Sun Apr 11 04:17:58 2021 +0000
@@ -427,6 +427,11 @@
 			return(-1);
 		}
 		efs->total_size = (tlv[2] << 8) | tlv[3];
+		if (efs->total_size > 0x8000) {
+			fprintf(stderr,
+		"error: transparent file size exceeds UICC protocol limit\n");
+			return(-1);
+		}
 		return(0);
 	case 0x02:
 	case 0x06: