view uicc/createfile.c @ 47:b0cf75d0bb2d

doc/Serial-SIM-readers article written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Mar 2021 04:32:18 +0000
parents b70d35f5476f
children
line wrap: on
line source

/*
 * This module implements commands that exercise ETSI TS 102 222
 * CREATE FILE and DELETE FILE operations.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "simresp.h"

cmd_create_file(argc, argv)
	char **argv;
{
	u_char apdu[260], inbuf[252], *dp;
	unsigned len1, len2;
	int rc;

	rc = read_hex_data_file(argv[1], inbuf, 252);
	if (rc < 0)
		return(rc);
	len1 = rc;
	dp = apdu + 5;
	*dp++ = 0x62;
	if (len1 < 0x80) {
		*dp++ = len1;
		len2 = len1 + 2;
	} else {
		*dp++ = 0x81;
		*dp++ = len1;
		len2 = len1 + 3;
	}
	bcopy(inbuf, dp, len1);
	/* command header */
	apdu[0] = 0x00;
	apdu[1] = 0xE0;
	apdu[2] = 0;
	apdu[3] = 0;
	apdu[4] = len2;
	rc = apdu_exchange(apdu, len2 + 5);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_delete_file(argc, argv)
	char **argv;
{
	u_char apdu[7];
	unsigned file_id;
	int rc;

	if (!isxdigit(argv[1][0]) || !isxdigit(argv[1][1]) ||
	    !isxdigit(argv[1][2]) || !isxdigit(argv[1][3]) || argv[1][4]) {
		fprintf(stderr, "error: 4-digit hex argument required\n");
		return(-1);
	}
	file_id = strtoul(argv[1], 0, 16);
	/* form command APDU */
	apdu[0] = 0x00;
	apdu[1] = 0xE4;
	apdu[2] = 0;
	apdu[3] = 0;
	apdu[4] = 2;
	apdu[5] = file_id >> 8;
	apdu[6] = file_id;
	rc = apdu_exchange(apdu, 7);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}