changeset 193:bc127ba44488

fc-simtool erase-file command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 Mar 2021 22:55:21 +0000
parents edaccdbac95b
children a5fee308b699
files simtool/Makefile simtool/dispatch.c simtool/erasefile.c
diffstat 3 files changed, 119 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Sat Mar 06 21:41:12 2021 +0000
+++ b/simtool/Makefile	Sat Mar 06 22:55:21 2021 +0000
@@ -1,13 +1,14 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC -I../libcommon
 PROG=	fc-simtool
-OBJS=	a38.o bfsearch.o chv.o chvext.o curfile.o dispatch.o dumpdir.o fplmn.o \
-	getresp.o grcard1.o grcard2.o hlread.o inval_rehab.o lndwrite.o main.o \
-	miscadm.o opldump.o pbcommon.o pbdump.o pberase.o pbrestore.o \
-	pbupd_imm.o pbupd_immhex.o plmnsel.o pnndump.o readcmd.o readef.o \
-	readops.o restorebin.o savebin.o script.o select.o smserase.o \
-	smsp_common.o smsp_dump.o smsp_erase.o smsp_restore.o smsp_set.o \
-	sstlist.o stktest.o sysmo.o telsum.o usersum.o writecmd.o writeops.o
+OBJS=	a38.o bfsearch.o chv.o chvext.o curfile.o dispatch.o dumpdir.o \
+	erasefile.o fplmn.o getresp.o grcard1.o grcard2.o hlread.o \
+	inval_rehab.o lndwrite.o main.o miscadm.o opldump.o pbcommon.o pbdump.o\
+	pberase.o pbrestore.o pbupd_imm.o pbupd_immhex.o plmnsel.o pnndump.o \
+	readcmd.o readef.o readops.o restorebin.o savebin.o script.o select.o \
+	smserase.o smsp_common.o smsp_dump.o smsp_erase.o smsp_restore.o \
+	smsp_set.o sstlist.o stktest.o sysmo.o telsum.o usersum.o writecmd.o \
+	writeops.o
 LIBS=	../libcommon/libcommon.a ../libutil/libutil.a
 INSTBIN=/opt/freecalypso/bin
 
--- a/simtool/dispatch.c	Sat Mar 06 21:41:12 2021 +0000
+++ b/simtool/dispatch.c	Sat Mar 06 22:55:21 2021 +0000
@@ -20,6 +20,7 @@
 extern int cmd_enable_chv1_rpt();
 extern int cmd_envelope();
 extern int cmd_envelope_imm();
+extern int cmd_erase_file();
 extern int cmd_exec();
 extern int cmd_fix_sysmo_msisdn();
 extern int cmd_fplmn_dump();
@@ -136,6 +137,7 @@
 	{"enable-pin1-rpt", 1, 1, 0, cmd_enable_chv1_rpt},
 	{"envelope", 1, 1, 0, cmd_envelope},
 	{"envelope-imm", 1, 1, 0, cmd_envelope_imm},
+	{"erase-file", 1, 2, 0, cmd_erase_file},
 	{"exec", 1, 1, 0, cmd_exec},
 	{"exit", 0, 0, 0, good_exit},
 	{"fix-sysmo-msisdn", 0, 0, 0, cmd_fix_sysmo_msisdn},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/erasefile.c	Sat Mar 06 22:55:21 2021 +0000
@@ -0,0 +1,109 @@
+/*
+ * 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 "curfile.h"
+
+static
+erase_transparent(fill_byte)
+{
+	u_char data[255];
+	unsigned off, cc;
+	int rc;
+
+	memset(data, fill_byte, 255);
+	for (off = 0; off < curfile_total_size; off += cc) {
+		cc = curfile_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(fill_byte)
+{
+	u_char data[255];
+	unsigned recno;
+	int rc;
+
+	memset(data, fill_byte, curfile_record_len);
+	for (recno = 1; recno <= curfile_record_count; recno++) {
+		rc = update_rec_op(recno, 0x04, data, curfile_record_len);
+		if (rc < 0)
+			return(rc);
+	}
+	return(0);
+}
+
+static
+erase_cyclic(fill_byte)
+{
+	u_char data[255];
+	unsigned count;
+	int rc;
+
+	memset(data, fill_byte, curfile_record_len);
+	for (count = 0; count < curfile_record_count; count++) {
+		rc = update_rec_op(0, 0x03, data, curfile_record_len);
+		if (rc < 0)
+			return(rc);
+	}
+	return(0);
+}
+
+cmd_erase_file(argc, argv)
+	char **argv;
+{
+	int file_id, rc;
+	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 = parse_ef_select_response();
+	if (rc < 0)
+		return(rc);
+	if (argc > 2) {
+		fill_byte = strtoul(argv[2], 0, 0);
+		if (fill_byte > 0xFF) {
+			fprintf(stderr, "error: invalid fill byte argument\n");
+			return(-1);
+		}
+	} else
+		fill_byte = 0xFF;
+	switch (curfile_structure) {
+	case 0x00:
+		/* transparent */
+		rc = erase_transparent(fill_byte);
+		break;
+	case 0x01:
+		/* record-based */
+		rc = erase_records(fill_byte);
+		break;
+	case 0x03:
+		/* cyclic */
+		rc = erase_cyclic(fill_byte);
+		break;
+	}
+	return(rc);
+}