view uicc/restorebin.c @ 93:6041c601304d

fcsim1-mkprov: revert OTA key addition It appears that GrcardSIM2 cards (which is what we got for FCSIM1) do not support OTA after all, contrary to what we were previously led to believe by some tech support emails from Grcard - apparently those support emails and OTA descriptions referred to some other card model(s).
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 21 Apr 2021 05:38:39 +0000
parents abef3d5668b9
children
line wrap: on
line source

/*
 * This module implements the restore-file command; this command
 * reads binary files previously saved with the savebin command
 * and writes the backed-up bits back to the SIM.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "efstruct.h"

static
restore_bin_transparent(efs, data)
	struct ef_struct *efs;
	u_char *data;
{
	unsigned off, cc;
	int rc;

	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 + off, cc);
		if (rc < 0)
			return(rc);
	}
	return(0);
}

static
restore_bin_records(efs, data)
	struct ef_struct *efs;
	u_char *data;
{
	unsigned recno;
	u_char *dp;
	int rc;

	dp = data;
	for (recno = 1; recno <= efs->record_count; recno++) {
		rc = update_rec_op(recno, 0x04, dp, efs->record_len);
		if (rc < 0)
			return(rc);
		dp += efs->record_len;
	}
	return(0);
}

static
restore_bin_cyclic(efs, data)
	struct ef_struct *efs;
	u_char *data;
{
	unsigned count;
	u_char *dp;
	int rc;

	dp = data + efs->total_size;
	for (count = 0; count < efs->record_count; count++) {
		dp -= efs->record_len;
		rc = update_rec_op(0, 0x03, dp, efs->record_len);
		if (rc < 0)
			return(rc);
	}
	return(0);
}

cmd_restore_file(argc, argv)
	char **argv;
{
	int file_id, rc, fd;
	struct ef_struct efs;
	struct stat st;
	u_char *databuf;

	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 (!efs.total_size) {
		printf("SIM indicates file of zero length, nothing to do\n");
		return(0);
	}
	fd = open(argv[2], O_RDONLY);
	if (fd < 0) {
		perror(argv[2]);
		return(-1);
	}
	fstat(fd, &st);
	if ((st.st_mode & S_IFMT) != S_IFREG) {
		fprintf(stderr, "error: %s is not a regular file\n", argv[2]);
		close(fd);
		return(-1);
	}
	if (st.st_size != efs.total_size) {
		fprintf(stderr,
	"error: length of %s does not match SIM EF length of %u bytes\n",
			argv[2], efs.total_size);
		close(fd);
		return(-1);
	}
	databuf = malloc(efs.total_size);
	if (!databuf) {
		perror("malloc for file data");
		close(fd);
		return(-1);
	}
	read(fd, databuf, efs.total_size);
	close(fd);
	switch (efs.structure) {
	case 0x01:
		/* transparent */
		rc = restore_bin_transparent(&efs, databuf);
		break;
	case 0x02:
		/* record-based */
		rc = restore_bin_records(&efs, databuf);
		break;
	case 0x06:
		/* cyclic */
		rc = restore_bin_cyclic(&efs, databuf);
		break;
	}
	free(databuf);
	return(rc);
}