diff simtool/pnndump.c @ 50:dc8a2e6fa03e

fc-simtool pnn-dump implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 13 Feb 2021 04:31:02 +0000
parents
children 2c07684a3980
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/pnndump.c	Sat Feb 13 04:31:02 2021 +0000
@@ -0,0 +1,104 @@
+/*
+ * This module implements the pnn-dump command, providing a
+ * user-accessible way to identify MVNO SIMs.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include "simresp.h"
+#include "curfile.h"
+#include "file_id.h"
+
+static void
+dump_record(recno)
+	unsigned recno;
+{
+	u_char *dp, *endp;
+	char *name_kw;
+	unsigned ielen, code_byte, nsept;
+	u_char gsm7_buf[288];
+
+	printf("#%u:", recno);
+	dp = sim_resp_data;
+	endp = sim_resp_data + sim_resp_data_len;
+	while (dp < endp) {
+		if (*dp == 0xFF)
+			break;
+		switch (*dp++) {
+		case 0x43:
+			name_kw = "Ln";
+			break;
+		case 0x45:
+			name_kw = "Sn";
+			break;
+		default:
+			printf(" unknown-IEI\n");
+			return;
+		}
+		if (dp >= endp) {
+			printf(" truncated-IE\n");
+			return;
+		}
+		ielen = *dp++;
+		if (ielen < 1 || ielen > (endp - dp)) {
+			printf(" bad-length\n");
+			return;
+		}
+		code_byte = *dp++;
+		ielen--;
+		printf(" %s=0x%02X", name_kw, code_byte);
+		if (!ielen)
+			continue;
+		putchar(',');
+		if ((code_byte & 0x70) == 0) {
+			nsept = ielen * 8 / 7;
+			gsm7_unpack(dp, gsm7_buf, nsept);
+			dp += ielen;
+			print_gsm7_string_to_file(gsm7_buf, nsept, stdout);
+		} else {
+			for (; ielen; ielen--)
+				printf("%02X", *dp++);
+		}
+	}
+	for (; dp < endp; dp++) {
+		if (*dp != 0xFF) {
+			printf(" bad-padding\n");
+			return;
+		}
+	}
+	putchar('\n');
+}
+
+cmd_pnn_dump()
+{
+	int rc;
+	unsigned recno;
+
+	rc = select_op(DF_GSM);
+	if (rc < 0)
+		return(rc);
+	rc = select_op(EF_PNN);
+	if (rc < 0)
+		return(rc);
+	rc = parse_ef_select_response();
+	if (rc < 0)
+		return(rc);
+	if (curfile_structure != 0x01) {
+		fprintf(stderr, "error: EF_PNN is not linear fixed\n");
+		return(-1);
+	}
+	if (curfile_record_len < 3) {
+		fprintf(stderr,
+"error: EF_PNN record length is less than the spec minimum of 3 bytes\n");
+		return(-1);
+	}
+	for (recno = 1; recno <= curfile_record_count; recno++) {
+		rc = readrec_op(recno, 0x04, curfile_record_len);
+		if (rc < 0)
+			return(rc);
+		if (check_simresp_all_blank())
+			continue;
+		dump_record(recno);
+	}
+	return(0);
+}