view miscprog/calextract.c @ 325:7df08926a4e7

frbl2test: better message for Hello response
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 06 Mar 2020 01:59:20 +0000
parents c38075b8a625
children
line wrap: on
line source

/*
 * GSM firmwares constructed per TI's canon contain several hard-coded
 * T_RF_BAND structures, one per band, emitted literally in the .const
 * section as they are constant initialized data, i.e.,
 * const T_RF_BAND rf_XXX = { blah }.  This utility extracts one of these
 * structures from a binary file (hex offset must be given) and parses it
 * into a C form suitable for placement in l1_rf12.c in the FreeCalypso
 * gsm-fw source.
 */

#include <stdio.h>
#include <stdlib.h>

char *binfilename;
FILE *binf;
u_long start_offset;

getbyte()
{
	int c;

	c = getc(binf);
	if (c < 0) {
		fprintf(stderr, "error or EOF reading from %s\n", binfilename);
		exit(1);
	}
	return(c);
}

get_u16()
{
	int lo, hi;

	lo = getbyte();
	hi = getbyte();
	return((hi << 8) | lo);
}

get_s16()
{
	int i;

	i = get_u16();
	if (i >= 32768)
		i -= 65536;
	return(i);
}

do_rx_cal_params()
{
	int i;

	puts("    { /* T_RX_CAL_PARAMS */");
	for (i = 0; i < 4; i++)
		printf("%10d,\n", get_u16());
	puts("    },");
}

do_rx_agc_bands()
{
	int i, u, s;

	puts("    { /* T_RF_AGC_BANDs */");
	for (i = 0; i < 10; i++) {
		u = get_u16();
		s = get_s16();
		printf("      {%5d,%6d},\n", u, s);
	}
	puts("    },");
}

do_rx_temp_comp()
{
	int i, s1, s2;

	puts("    { /* Rx temperature compensation */");
	for (i = 0; i < 11; i++) {
		s1 = get_s16();
		s2 = get_s16();
		printf("      {%6d,%6d},\n", s1, s2);
	}
	puts("    },");
}

do_rx()
{
	puts("  { /* Rx structure */");
	do_rx_cal_params();
	do_rx_agc_bands();
	do_rx_temp_comp();
	puts("  },");
}

do_tx_levels()
{
	int i, u, b1, b2;

	puts("    { /* levels */");
	for (i = 0; i < 32; i++) {
		u = get_u16();
		b1 = getbyte();
		b2 = getbyte();
		printf("      {%5d,%3d,%3d}, /* %d */\n", u, b1, b2, i);
	}
	puts("    },");
}

do_tx_calchan()
{
	int i, j, u, s;

	puts("    { /* channel calibration tables */");
	for (i = 0; i < 4; i++) {
		printf("      { /* calibration table %d */\n", i);
		for (j = 0; j < 8; j++) {
			u = get_u16();
			s = get_s16();
			printf("\t{%5d,%6d},\n", u, s);
		}
		puts("      },");
	}
	puts("    },");
}

do_ramp_16bytes()
{
	int i, b;

	putchar('\t');
	putchar('{');
	for (i = 0; i < 16; i++) {
		b = getbyte();
		printf("%3d%c", b, i == 15 ? '}' : ',');
	}
	putchar(',');
	putchar('\n');
}

do_tx_ramps()
{
	int i;

	puts("    { /* ramps */");
	for (i = 0; i < 16; i++) {
		printf("      { /* profile %d */\n", i);
		puts("\t/* ramp-up */");
		do_ramp_16bytes();
		puts("\t/* ramp-down */");
		do_ramp_16bytes();
		puts("      },");
	}
	puts("    },");
}

do_tx_temp_comp()
{
	int i, j, s[4];

	puts("    { /* Tx temperature compensation */");
	for (i = 0; i < 5; i++) {
		for (j = 0; j < 4; j++)
			s[j] = get_s16();
		printf("      {%6d,%6d,%6d,%6d},\n", s[0], s[1], s[2], s[3]);
	}
	puts("    },");
}

do_tx()
{
	puts("  { /* Tx structure */");
	do_tx_levels();
	do_tx_calchan();
	do_tx_ramps();
	do_tx_temp_comp();
	puts("  },");
}

main(argc, argv)
	char **argv;
{
	if (argc != 3) {
		fprintf(stderr, "usage: %s binfile hex-offset\n", argv[0]);
		exit(1);
	}
	binfilename = argv[1];
	start_offset = strtoul(argv[2], 0, 16);
	binf = fopen(binfilename, "r");
	if (!binf) {
		perror(binfilename);
		exit(1);
	}
	fseek(binf, start_offset, SEEK_SET);
	puts("const T_RF_BAND rf_XXX = {");
	do_rx();
	do_tx();
	printf("  %d\n};\n", getbyte());
	exit(0);
}