view simtool/plmnsel.c @ 169:c37a3cc0fafe

bfsearch-booty: bfsearch captures from GR2 and SJA2 cards
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 28 Feb 2021 21:22:59 +0000
parents 32acef9d20ff
children 3ddbc3fea5f0
line wrap: on
line source

/*
 * This module implements commands for working with EF_PLMNsel.
 */

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

static
select_ef_plmnsel()
{
	int rc;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_PLMNsel);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00) {
		fprintf(stderr, "error: EF_PLMNsel is not transparent\n");
		return(-1);
	}
	if (curfile_total_size < 24) {
		fprintf(stderr,
		"error: EF_PLMNsel is shorter than spec minimum of 24 bytes\n");
		return(-1);
	}
	if (curfile_total_size > 255) {
		fprintf(stderr,
		"error: EF_PLMNsel is longer than our 255 byte limit\n");
		return(-1);
	}
	if (curfile_total_size % 3) {
		fprintf(stderr,
		"error: EF_PLMNsel length is not a multiple of 3 bytes\n");
		return(-1);
	}
	return(0);
}

cmd_plmnsel_dump(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int rc, gap_flag;
	u_char *dp, *endp;
	char ascbuf[8];
	unsigned idx, linelen;

	rc = select_ef_plmnsel();
	if (rc < 0)
		return(rc);
	rc = readbin_op(0, curfile_total_size);
	if (rc < 0)
		return(rc);
	dp = sim_resp_data;
	endp = sim_resp_data + sim_resp_data_len;
	gap_flag = 0;
	linelen = 0;
	for (idx = 0; dp < endp; idx++, dp += 3) {
		if (dp[0] == 0xFF && dp[1] == 0xFF && dp[2] == 0xFF) {
			gap_flag = 1;
			continue;
		}
		if (gap_flag) {
			if (linelen) {
				putc('\n', outf);
				linelen = 0;
			}
			fprintf(outf, "GAP, continuing at index %u:\n", idx);
			gap_flag = 0;
		}
		if (linelen >= 10) {
			putc('\n', outf);
			linelen = 0;
		}
		decode_plmn_3bytes(dp, ascbuf, 1);
		if (linelen)
			putc(' ', outf);
		fputs(ascbuf, outf);
		linelen++;
	}
	if (linelen)
		putc('\n', outf);
	return(0);
}

cmd_plmnsel_write(argc, argv)
	char **argv;
{
	int rc;
	unsigned idx;
	u_char rec[3];

	rc = select_ef_plmnsel();
	if (rc < 0)
		return(rc);
	idx = strtoul(argv[1], 0, 0);
	if (idx >= curfile_total_size / 3) {
		fprintf(stderr, "error: specified index is out of range\n");
		return(-1);
	}
	rc = encode_plmn_3bytes(argv[2], rec);
	if (rc < 0)
		return(rc);
	return update_bin_op(idx * 3, rec, 3);
}

cmd_plmnsel_erase(argc, argv)
	char **argv;
{
	int rc;
	unsigned idx, start, end, nrec;
	u_char rec[3];

	rc = select_ef_plmnsel();
	if (rc < 0)
		return(rc);
	nrec = curfile_total_size / 3;
	start = strtoul(argv[1], 0, 0);
	if (start >= nrec) {
		fprintf(stderr,
			"error: specified starting index is out of range\n");
		return(-1);
	}
	if (!argv[2])
		end = start;
	else if (!strcmp(argv[2], "end"))
		end = nrec - 1;
	else {
		end = strtoul(argv[1], 0, 0);
		if (end >= nrec) {
			fprintf(stderr,
			"error: specified ending index is out of range\n");
			return(-1);
		}
		if (start > end) {
			fprintf(stderr,
				"error: reverse index range specified\n");
			return(-1);
		}
	}
	memset(rec, 0xFF, 3);
	for (idx = start; idx <= end; idx++) {
		rc = update_bin_op(idx * 3, rec, 3);
		if (rc < 0)
			return(rc);
	}
	return(0);
}

cmd_plmnsel_erase_all(argc, argv)
	char **argv;
{
	int rc;
	u_char ffbuf[255];

	rc = select_ef_plmnsel();
	if (rc < 0)
		return(rc);
	memset(ffbuf, 0xFF, curfile_total_size);
	return update_bin_op(0, ffbuf, curfile_total_size);
}