view simtool/hlread.c @ 81:8c23d2a9c013

doc/GrcardSIM2-programming: update for fcsim1-program
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 09 Apr 2021 08:35:54 +0000
parents aefc9fe653d3
children
line wrap: on
line source

/*
 * This module implements some high-level or user-friendly read commands.
 */

#include <sys/types.h>
#include <stdio.h>
#include "simresp.h"
#include "curfile.h"
#include "file_id.h"

retrieve_iccid(buf)
	char *buf;
{
	int rc;
	char *cp;

	rc = select_op(FILEID_MF);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_ICCID);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00 || curfile_total_size != 10) {
		fprintf(stderr, "error: expected transparent EF of 10 bytes\n");
		return(-1);
	}
	rc = readbin_op(0, 10);
	if (rc < 0)
		return(rc);
	decode_reversed_nibbles(sim_resp_data, 10, buf);
	for (cp = buf + 20; (cp > buf + 1) && (cp[-1] == 'F'); cp--)
		;
	*cp = '\0';
	return(0);
}

cmd_iccid(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int rc;
	char buf[21];

	rc = retrieve_iccid(buf);
	if (rc < 0)
		return(rc);
	fprintf(outf, "%s\n", buf);
	return(0);
}

cmd_imsi(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int rc;
	char buf[17], *endp;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_IMSI);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00 || curfile_total_size != 9) {
		fprintf(stderr, "error: expected transparent EF of 9 bytes\n");
		return(-1);
	}
	rc = readbin_op(0, 9);
	if (rc < 0)
		return(rc);
	if (sim_resp_data[0] < 1 || sim_resp_data[0] > 8) {
malformed:	fprintf(stderr, "error: malformed EF_IMSI record\n");
		return(-1);
	}
	decode_reversed_nibbles(sim_resp_data + 1, sim_resp_data[0], buf);
	endp = buf + (sim_resp_data[0] << 1);
	switch (buf[0]) {
	case '1':
		if (sim_resp_data[0] == 1)
			goto malformed;
		*--endp = '\0';
		break;
	case '9':
		*endp = '\0';
		break;
	default:
		goto malformed;
	}
	fprintf(outf, "%s\n", buf + 1);
	return(0);
}

cmd_imsi_raw(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int rc;
	char buf[17];

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_IMSI);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00 || curfile_total_size != 9) {
		fprintf(stderr, "error: expected transparent EF of 9 bytes\n");
		return(-1);
	}
	rc = readbin_op(0, 9);
	if (rc < 0)
		return(rc);
	decode_reversed_nibbles(sim_resp_data + 1, 8, buf);
	buf[16] = '\0';
	fprintf(outf, "%s parity=%c len=%u\n", buf + 1, buf[0],
		sim_resp_data[0]);
	return(0);
}

cmd_spn(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int rc;
	unsigned textlen;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_SPN);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00 || curfile_total_size != 17) {
		fprintf(stderr, "error: expected transparent EF of 17 bytes\n");
		return(-1);
	}
	rc = readbin_op(0, 17);
	if (rc < 0)
		return(rc);
	fprintf(outf, "Display condition: %02X\n", sim_resp_data[0]);
	fputs("SPN: ", outf);
	rc = validate_alpha_field(sim_resp_data + 1, 16, &textlen);
	if (rc >= 0)
		print_alpha_field(sim_resp_data + 1, textlen, outf);
	else
		fputs("malformed alpha field", outf);
	putc('\n', outf);
	return(0);
}