view simtool/fplmn.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 ddd767f6e15b
children
line wrap: on
line source

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

#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_fplmn()
{
	int rc;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_FPLMN);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00) {
		fprintf(stderr, "error: EF_FPLMN is not transparent\n");
		return(-1);
	}
	if (curfile_total_size != 12) {
		fprintf(stderr,
			"error: EF_FPLMN size does not equal 12 bytes\n");
		return(-1);
	}
	return(0);
}

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

	rc = select_ef_fplmn();
	if (rc < 0)
		return(rc);
	rc = readbin_op(0, 12);
	if (rc < 0)
		return(rc);
	dp = sim_resp_data;
	for (idx = 0; idx < 4; idx++, dp += 3) {
		if (idx)
			putc(' ', outf);
		if (dp[0] == 0xFF && dp[1] == 0xFF && dp[2] == 0xFF)
			fputs("-blank-", outf);
		else {
			decode_plmn_3bytes(dp, ascbuf, 1);
			fputs(ascbuf, outf);
		}
	}
	putc('\n', outf);
	return(0);
}

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

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

cmd_fplmn_write_list(argc, argv)
	char **argv;
{
	int rc;
	u_char buf[12];

	rc = select_ef_fplmn();
	if (rc < 0)
		return(rc);
	rc = read_plmn_list_from_file(argv[1], buf, 12);
	if (rc < 0)
		return(rc);
	return update_bin_op(0, buf, 12);
}

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

	rc = select_ef_fplmn();
	if (rc < 0)
		return(rc);
	start = strtoul(argv[1], 0, 0);
	if (start >= 4) {
		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 = 3;
	else {
		end = strtoul(argv[1], 0, 0);
		if (end >= 4) {
			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_fplmn_erase_all(argc, argv)
	char **argv;
{
	int rc;
	u_char ffbuf[12];

	rc = select_ef_fplmn();
	if (rc < 0)
		return(rc);
	memset(ffbuf, 0xFF, 12);
	return update_bin_op(0, ffbuf, 12);
}