view simtool/pbdump.c @ 74:8562d8508cf2

grcard2-set-{adm,super}-hex commands implemented It appears that GrcardSIM2 cards allow arbitrary 64-bit keys for ADM and SUPER ADM, not necessarily consisting of ASCII digits like the specs require for standard PIN and PUK, and pySim-prog.py in fact sets the ADM key to 4444444444444444 in hex by default, which is not an ASCII digit string. If the cards allow such keys, we need to support them too.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 16 Feb 2021 04:10:36 +0000
parents 1861d9fb7751
children 0b29c0d19db4
line wrap: on
line source

/*
 * This module implements pb-dump and pb-dump-rec commands.
 */

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

static
check_blank_area(dp, endp)
	u_char *dp, *endp;
{
	while (dp < endp)
		if (*dp++ != 0xFF)
			return(-1);
	return(0);
}

static void
dump_record(recno, outf)
	unsigned recno;
	FILE *outf;
{
	int rc;
	unsigned textlen;
	u_char *fixp;
	char digits[21];

	fprintf(outf, "#%u: ", recno);
	if (sim_resp_data_len > 14) {
		rc = validate_alpha_field(sim_resp_data,
					  sim_resp_data_len - 14,
					  &textlen);
		if (rc < 0) {
malformed:		fprintf(outf, "malformed record\n");
			return;
		}
	} else
		textlen = 0;
	fixp = sim_resp_data + sim_resp_data_len - 14;
	if (fixp[0] < 2 || fixp[0] > 11)
		goto malformed;
	rc = decode_phone_number(fixp + 2, fixp[0] - 1, digits);
	if (rc < 0)
		goto malformed;
	rc = check_blank_area(fixp + 1 + fixp[0], fixp + 12);
	if (rc < 0)
		goto malformed;
	/* all checks passed */
	fprintf(outf, "%s,0x%02X ", digits, fixp[1]);
	if (fixp[12] != 0xFF)
		fprintf(outf, "CCP=%u ", fixp[12]);
	if (fixp[13] != 0xFF)
		fprintf(outf, "EXT=%u ", fixp[13]);
	print_alpha_field(sim_resp_data, textlen, outf);
	putc('\n', outf);
}

cmd_pb_dump(argc, argv)
	char **argv;
{
	int rc;
	FILE *outf;
	unsigned recno;

	rc = phonebook_op_common(argv[1]);
	if (rc < 0)
		return(rc);
	if (argv[2]) {
		outf = fopen(argv[2], "w");
		if (!outf) {
			perror(argv[2]);
			return(-1);
		}
	} else
		outf = stdout;
	for (recno = 1; recno <= curfile_record_count; recno++) {
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0) {
			if (argv[2])
				fclose(outf);
			return(rc);
		}
		if (check_simresp_all_blank())
			continue;
		dump_record(recno, outf);
	}
	if (argv[2])
		fclose(outf);
	return(0);
}

cmd_pb_dump_rec(argc, argv)
	char **argv;
{
	int rc;
	unsigned recno, startrec, endrec;

	rc = phonebook_op_common(argv[1]);
	if (rc < 0)
		return(rc);
	startrec = strtoul(argv[2], 0, 0);
	if (startrec < 1 || startrec > curfile_record_count) {
		fprintf(stderr,
			"error: specified starting record number is invalid\n");
		return(-1);
	}
	if (argv[3]) {
		endrec = strtoul(argv[3], 0, 0);
		if (endrec < 1 || endrec > curfile_record_count) {
			fprintf(stderr,
			"error: specified final record number is invalid\n");
			return(-1);
		}
		if (startrec > endrec) {
			fprintf(stderr,
				"error: reverse record range specified\n");
			return(-1);
		}
	} else
		endrec = startrec;
	for (recno = startrec; recno <= endrec; recno++) {
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0)
			return(rc);
		if (check_simresp_all_blank())
			continue;
		dump_record(recno, stdout);
	}
	return(0);
}

cmd_dump_lnd()
{
	int rc;
	unsigned recno;

	rc = select_op(DF_TELECOM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_LND);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x03) {
		fprintf(stderr, "error: EF_LND is not cyclic\n");
		return(-1);
	}
	if (curfile_record_len < 14) {
		fprintf(stderr,
	"error: EF_LND has record length of %u bytes, less than minimum 14\n",
			curfile_record_len);
		return(-1);
	}
	for (recno = 1; recno <= curfile_record_count; recno++) {
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0)
			return(rc);
		if (check_simresp_all_blank())
			continue;
		dump_record(recno, stdout);
	}
	return(0);
}