changeset 145:14dee03e9675

fc-uicc-tool: low-level write commands ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 Feb 2021 02:17:51 +0000
parents 429a8f80426e
children ce2a880ab704
files uicc/Makefile uicc/dispatch.c uicc/hexread.c uicc/writecmd.c uicc/writeops.c
diffstat 5 files changed, 194 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/uicc/Makefile	Sat Feb 06 02:03:11 2021 +0000
+++ b/uicc/Makefile	Sat Feb 06 02:17:51 2021 +0000
@@ -2,8 +2,9 @@
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-uicc-tool
 OBJS=	alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o dispatch.o \
-	dumpdir.o exit.o globals.o hexdump.o hexstr.o hlread.o main.o names.o \
-	pbcommon.o pbdump.o readcmd.o readops.o script.o select.o telsum.o
+	dumpdir.o exit.o globals.o hexdump.o hexread.o hexstr.o hlread.o main.o\
+	names.o pbcommon.o pbdump.o readcmd.o readops.o script.o select.o \
+	telsum.o writecmd.o writeops.o
 INSTBIN=/opt/freecalypso/bin
 
 all:	${PROG}
--- a/uicc/dispatch.c	Sat Feb 06 02:03:11 2021 +0000
+++ b/uicc/dispatch.c	Sat Feb 06 02:17:51 2021 +0000
@@ -20,6 +20,9 @@
 extern int cmd_select_isim();
 extern int cmd_select_usim();
 extern int cmd_telecom_sum();
+extern int cmd_update_bin();
+extern int cmd_update_bin_imm();
+extern int cmd_update_rec();
 
 extern int display_sim_resp_in_hex();
 extern int good_exit();
@@ -45,6 +48,9 @@
 	{"select-usim", 0, 0, cmd_select_usim},
 	{"sim-resp", 0, 0, display_sim_resp_in_hex},
 	{"telecom-sum", 0, 0, cmd_telecom_sum},
+	{"update-bin", 2, 2, cmd_update_bin},
+	{"update-bin-imm", 2, 2, cmd_update_bin_imm},
+	{"update-rec", 2, 2, cmd_update_rec},
 	{0, 0, 0, 0}
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/hexread.c	Sat Feb 06 02:17:51 2021 +0000
@@ -0,0 +1,56 @@
+/*
+ * This module contains the function for reading hex files,
+ * to be used in the implementation of manual write commands.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+read_hex_data_file(filename, databuf)
+	char *filename;
+	u_char *databuf;
+{
+	FILE *inf;
+	unsigned count;
+	int c, c2;
+
+	inf = fopen(filename, "r");
+	if (!inf) {
+		perror(filename);
+		return(-1);
+	}
+	for (count = 0; ; count++) {
+		do
+			c = getc(inf);
+		while (isspace(c));
+		if (c < 0)
+			break;
+		if (!isxdigit(c)) {
+inv_input:		fprintf(stderr, "%s: invalid hex file input\n",
+				filename);
+			fclose(inf);
+			return(-1);
+		}
+		c2 = getc(inf);
+		if (!isxdigit(c2))
+			goto inv_input;
+		if (count >= 255) {
+			fprintf(stderr, "%s: hex input data is too long\n",
+				filename);
+			fclose(inf);
+			return(-1);
+		}
+		databuf[count] = (decode_hex_digit(c) << 4) |
+				 decode_hex_digit(c2);
+	}
+	fclose(inf);
+	if (!count) {
+		fprintf(stderr, "%s: no hex data input found\n", filename);
+		return(-1);
+	}
+	return(count);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/writecmd.c	Sat Feb 06 02:17:51 2021 +0000
@@ -0,0 +1,71 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+
+cmd_update_bin(argc, argv)
+	char **argv;
+{
+	unsigned offset, len;
+	u_char data[255];
+	int rc;
+
+	offset = strtoul(argv[1], 0, 0);
+	if (offset > 0x7FFF) {
+		fprintf(stderr, "error: offset argument is out of range\n");
+		return(-1);
+	}
+	rc = read_hex_data_file(argv[2], data);
+	if (rc < 0)
+		return(rc);
+	len = rc;
+	return update_bin_op(offset, data, len);
+}
+
+cmd_update_bin_imm(argc, argv)
+	char **argv;
+{
+	unsigned offset, len;
+	u_char data[255];
+	int rc;
+
+	offset = strtoul(argv[1], 0, 0);
+	if (offset > 0x7FFF) {
+		fprintf(stderr, "error: offset argument is out of range\n");
+		return(-1);
+	}
+	rc = decode_hex_data_from_string(argv[2], data, 255);
+	if (rc < 0)
+		return(rc);
+	len = rc;
+	return update_bin_op(offset, data, len);
+}
+
+cmd_update_rec(argc, argv)
+	char **argv;
+{
+	unsigned recno;
+	u_char data[255];
+	int rc;
+
+	if (!last_sel_file_record_len) {
+		fprintf(stderr, "error: no record-based file selected\n");
+		return(-1);
+	}
+	recno = strtoul(argv[1], 0, 0);
+	if (recno < 1 || recno > 255) {
+		fprintf(stderr,
+			"error: record number argument is out of range\n");
+		return(-1);
+	}
+	rc = read_hex_data_file(argv[2], data);
+	if (rc < 0)
+		return(rc);
+	if (rc != last_sel_file_record_len) {
+		fprintf(stderr, "error: hex data length != EF record length\n");
+		return(-1);
+	}
+	return update_rec_op(recno, 0x04, data, last_sel_file_record_len);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/writeops.c	Sat Feb 06 02:17:51 2021 +0000
@@ -0,0 +1,58 @@
+#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"
+
+update_bin_op(offset, data, len)
+	unsigned offset, len;
+	u_char *data;
+{
+	u_char cmd[260];
+	int rc;
+
+	/* UPDATE BINARY command APDU */
+	cmd[0] = 0x00;
+	cmd[1] = 0xD6;
+	cmd[2] = offset >> 8;
+	cmd[3] = offset;
+	cmd[4] = len;
+	bcopy(data, cmd + 5, len);
+	rc = apdu_exchange(cmd, len + 5);
+	if (rc < 0)
+		return(rc);
+	if (sim_resp_sw != 0x9000) {
+		fprintf(stderr, "bad SW response to UPDATE BINARY: %04X\n",
+			sim_resp_sw);
+		return(-1);
+	}
+	return(0);
+}
+
+update_rec_op(recno, mode, data, len)
+	unsigned recno, mode, len;
+	u_char *data;
+{
+	u_char cmd[260];
+	int rc;
+
+	/* UPDATE RECORD command APDU */
+	cmd[0] = 0x00;
+	cmd[1] = 0xDC;
+	cmd[2] = recno;
+	cmd[3] = mode;
+	cmd[4] = len;
+	bcopy(data, cmd + 5, len);
+	rc = apdu_exchange(cmd, len + 5);
+	if (rc < 0)
+		return(rc);
+	if (sim_resp_sw != 0x9000) {
+		fprintf(stderr, "bad SW response to UPDATE RECORD: %04X\n",
+			sim_resp_sw);
+		return(-1);
+	}
+	return(0);
+}