changeset 92:5560261fc516

fc-uicc-tool: erase-file ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:55:02 +0000
parents abef3d5668b9
children 6041c601304d
files uicc/Makefile uicc/cmdtab.c uicc/erasefile.c
diffstat 3 files changed, 118 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/uicc/Makefile	Sun Apr 11 04:44:34 2021 +0000
+++ b/uicc/Makefile	Sun Apr 11 04:55:02 2021 +0000
@@ -2,9 +2,9 @@
 CFLAGS=	-O2
 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 readef.o readops.o restorebin.o savebin.o select.o \
-	sws.o writecmd.o writeops.o
+OBJS=	bfsearch.o cmdtab.o createfile.o dumpdir.o erasefile.o getresp.o \
+	hlread.o main.o pins.o readcmd.o readef.o readops.o restorebin.o \
+	savebin.o select.o sws.o writecmd.o writeops.o
 LIBS=	../libcommon/libcommon.a ../libutil/libutil.a
 
 INSTALL_PREFIX=	/opt/freecalypso
--- a/uicc/cmdtab.c	Sun Apr 11 04:44:34 2021 +0000
+++ b/uicc/cmdtab.c	Sun Apr 11 04:55:02 2021 +0000
@@ -16,6 +16,7 @@
 extern int cmd_dir();
 extern int cmd_disable_pin();
 extern int cmd_enable_pin();
+extern int cmd_erase_file();
 extern int cmd_exec();
 extern int cmd_exit();
 extern int cmd_get_response();
@@ -58,6 +59,7 @@
 	{"dir", 0, 0, 1, cmd_dir},
 	{"disable-pin", 2, 2, 0, cmd_disable_pin},
 	{"enable-pin", 2, 2, 0, cmd_enable_pin},
+	{"erase-file", 1, 2, 0, cmd_erase_file},
 	{"exec", 1, 1, 0, cmd_exec},
 	{"exit", 0, 1, 0, cmd_exit},
 	{"get-response", 1, 1, 1, cmd_get_response},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/erasefile.c	Sun Apr 11 04:55:02 2021 +0000
@@ -0,0 +1,113 @@
+/*
+ * This module implements the erase-file command.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "efstruct.h"
+
+static
+erase_transparent(efs, fill_byte)
+	struct ef_struct *efs;
+{
+	u_char data[255];
+	unsigned off, cc;
+	int rc;
+
+	memset(data, fill_byte, 255);
+	for (off = 0; off < efs->total_size; off += cc) {
+		cc = efs->total_size - off;
+		if (cc > 255)
+			cc = 255;
+		rc = update_bin_op(off, data, cc);
+		if (rc < 0)
+			return(rc);
+	}
+	return(0);
+}
+
+static
+erase_records(efs, fill_byte)
+	struct ef_struct *efs;
+{
+	u_char data[255];
+	unsigned recno;
+	int rc;
+
+	memset(data, fill_byte, efs->record_len);
+	for (recno = 1; recno <= efs->record_count; recno++) {
+		rc = update_rec_op(recno, 0x04, data, efs->record_len);
+		if (rc < 0)
+			return(rc);
+	}
+	return(0);
+}
+
+static
+erase_cyclic(efs, fill_byte)
+	struct ef_struct *efs;
+{
+	u_char data[255];
+	unsigned count;
+	int rc;
+
+	memset(data, fill_byte, efs->record_len);
+	for (count = 0; count < efs->record_count; count++) {
+		rc = update_rec_op(0, 0x03, data, efs->record_len);
+		if (rc < 0)
+			return(rc);
+	}
+	return(0);
+}
+
+cmd_erase_file(argc, argv)
+	char **argv;
+{
+	int file_id, rc;
+	struct ef_struct efs;
+	unsigned fill_byte;
+
+	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);
+	if (argc > 2) {
+		fill_byte = strtoul(argv[2], 0, 16);
+		if (fill_byte > 0xFF) {
+			fprintf(stderr, "error: invalid fill byte argument\n");
+			return(-1);
+		}
+	} else
+		fill_byte = 0xFF;
+	switch (efs.structure) {
+	case 0x01:
+		/* transparent */
+		rc = erase_transparent(&efs, fill_byte);
+		break;
+	case 0x02:
+		/* record-based */
+		rc = erase_records(&efs, fill_byte);
+		break;
+	case 0x06:
+		/* cyclic */
+		rc = erase_cyclic(&efs, fill_byte);
+		break;
+	}
+	return(rc);
+}