view rvinterf/etmsync/pirmagnetite.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents 8136fb5eb292
children
line wrap: on
line source

/*
 * This pirelli-magnetite-init special command should ONLY be run
 * against FreeCalypso Magnetite firmware running on the Pirelli target.
 * It initializes the FFS for FC Magnetite (essentially TCS211)
 * appropriately for the Pirelli target, copying the IMEI and the
 * RF calibration values from Pirelli's factory data block.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "etm.h"
#include "ffs.h"
#include "tmffs2.h"
#include "localtypes.h"
#include "exitcodes.h"

extern u_char pirelli_imeisv[8];

static
write_pcm_imei()
{
	static char destfile[] = "/pcm/IMEI";
	u_char swapped[8];
	int i, d1, d2;

	printf("Writing %s\n", destfile);
	for (i = 0; i < 8; i++) {
		d1 = pirelli_imeisv[i] >> 4;
		d2 = pirelli_imeisv[i] & 0xF;
		swapped[i] = (d2 << 4) | d1;
	}
	return do_short_fwrite(destfile, swapped, 8);
}

static
read_mem_region(memaddr, databuf, total_bytes)
	u32 memaddr;
	u_char *databuf;
{
	int chunk, remain, rc;

	for (remain = total_bytes; remain; remain -= chunk) {
		chunk = remain;
		if (chunk > MAX_MEMREAD_BYTES)
			chunk = MAX_MEMREAD_BYTES;
		rc = do_memory_read(memaddr, databuf, chunk);
		if (rc)
			return(rc);
		memaddr += chunk;
		databuf += chunk;
	}
	return(0);
}

static
copy_calib_record(memaddr, pathname, size)
	u32 memaddr;
	char *pathname;
	int size;
{
	u_char *buf;
	int rc;

	buf = malloc(size);
	if (!buf) {
		perror("malloc");
		exit(ERROR_UNIX);
	}
	rc = read_mem_region(memaddr, buf, size);
	if (rc) {
		free(buf);
		return(rc);
	}
	rc = write_buf_to_file(pathname, buf, size);
	free(buf);
	return(rc);
}

static struct calmap {
	u32	offset;
	int	size;
	char	*ti_equiv;
} pirelli_cal_map[] = {
	{0x0528, 2,   "/gsm/rf/afcdac"},
	{0x06E5, 36,  "/sys/adccal"},
	{0x072B, 512, "/gsm/rf/tx/ramps.900"},
	{0x092C, 128, "/gsm/rf/tx/levels.900"},
	{0x09AD, 128, "/gsm/rf/tx/calchan.900"},
	{0x0A2E, 512, "/gsm/rf/tx/ramps.1800"},
	{0x0C2F, 128, "/gsm/rf/tx/levels.1800"},
	{0x0CB0, 128, "/gsm/rf/tx/calchan.1800"},
	{0x0D31, 512, "/gsm/rf/tx/ramps.1900"},
	{0x0F32, 128, "/gsm/rf/tx/levels.1900"},
	{0x0FB3, 128, "/gsm/rf/tx/calchan.1900"},
	{0x10AF, 40,  "/gsm/rf/rx/calchan.900"},
	{0x10D8, 8,   "/gsm/rf/rx/agcparams.900"},
	{0x10E1, 40,  "/gsm/rf/rx/calchan.1800"},
	{0x110A, 8,   "/gsm/rf/rx/agcparams.1800"},
	{0x1113, 40,  "/gsm/rf/rx/calchan.1900"},
	{0x113C, 8,   "/gsm/rf/rx/agcparams.1900"},
	{0, 0, 0}
};

static
copy_calib_data()
{
	struct calmap *tp;
	int rc;

	printf("Copying calibration records to FFS\n");
	for (tp = pirelli_cal_map; tp->size; tp++) {
		rc = copy_calib_record(0x027F0000 + tp->offset, tp->ti_equiv,
					tp->size);
		if (rc)
			return(rc);
	}
	return(0);
}

pirelli_magnetite_init()
{
	int rc;

	rc = get_pirelli_imei();
	if (rc)
		return(rc);
	printf("Creating TCS211 file system directories\n");
	rc = create_std_dirs();
	if (rc)
		return(rc);
	rc = write_pcm_imei();
	if (rc)
		return(rc);
	rc = copy_calib_data();
	if (rc)
		return(rc);
	return set_rfcap("tri900");
}