view ffstools/newcomp/tiffs-mkfile.c @ 1009:4a153059abbb

doc/DUART28-boot-control: update for fc-linux-patch and fc-usbser-tools
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 12 Dec 2023 06:57:11 +0000
parents f19c347d0a80
children
line wrap: on
line source

/*
 * This program generates certain types of binary files that go into TIFFS.
 * It is intended for use in shell scripts that prepare input trees for
 * tiffs-mkfs.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#define	MAX_FILE_SIZE	256

u_char databuf[MAX_FILE_SIZE];
unsigned datalen;

void
write_binary_file(filename)
	char *filename;
{
	int fd, cc;

	fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
	if (fd < 0) {
		perror(filename);
		exit(1);
	}
	cc = write(fd, databuf, datalen);
	if (cc != datalen) {
		perror("error writing to file");
		exit(1);
	}
	close(fd);
}

void
do_filetype_ascii(str)
	char *str;
{
	unsigned len;

	len = strlen(str);
	if (len > MAX_FILE_SIZE) {
		fprintf(stderr,
			"error: ASCII string exceeds file size limit\n");
		exit(1);
	}
	bcopy(str, databuf, len);
	datalen = len;
}

hexdigit(c)
{
	if (isdigit(c))
		return(c - '0');
	else if (isupper(c))
		return(c - 'A' + 10);
	else
		return(c - 'a' + 10);
}

void
do_filetype_hex(hexstr)
	char *hexstr;
{
	char *cp;
	unsigned len;

	for (cp = hexstr, len = 0; ; cp += 2) {
		while (isspace(*cp))
			cp++;
		if (!*cp)
			break;
		if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
			fprintf(stderr, "error: invalid hex string argument\n");
			exit(1);
		}
		if (len >= MAX_FILE_SIZE) {
			fprintf(stderr,
				"error: hex string exceeds file size limit\n");
			exit(1);
		}
		databuf[len++] = hexdigit(cp[0]) << 4 | hexdigit(cp[1]);
	}
	datalen = len;
}

void
parse_imeisv_arg(input, buf)
	char *input, *buf;
{
	char *cp;
	int i;

	cp = input;
	if (!isdigit(*cp)) {
inv:		fprintf(stderr,
			"error: IMEISV argument must have 16 decimal digits\n");
		exit(1);
	}
	for (i = 0; i < 16; i++) {
		if (ispunct(*cp))
			cp++;
		if (!isdigit(*cp))
			goto inv;
		buf[i] = *cp++ - '0';
	}
	if (*cp)
		goto inv;
}

void
do_filetype_imeisv(strarg)
	char *strarg;
{
	char digits[16];
	int i;

	parse_imeisv_arg(strarg, digits);
	for (i = 0; i < 8; i++)
		databuf[i] = digits[i*2] << 4 | digits[i*2+1];
	datalen = 8;
}

void
do_filetype_pcm_imei(strarg)
	char *strarg;
{
	char digits[16];
	int i;

	parse_imeisv_arg(strarg, digits);
	for (i = 0; i < 8; i++)
		databuf[i] = digits[i*2+1] << 4 | digits[i*2];
	datalen = 8;
}

static struct band_table {
	char	*keyword;
	u_char	bytes[4];
} band_table[] = {
	{"dual-eu",	{0x00, 0x0B, 0x41, 0x00}},
	{"dual-us",	{0x00, 0x14, 0x00, 0x14}},
	{"tri900",	{0x00, 0x0F, 0x41, 0x10}},
	{"tri850",	{0x00, 0x16, 0x01, 0x14}},
	{"quad",	{0x00, 0x1F, 0x41, 0x14}},
	{0,		{0x00, 0x00, 0x00, 0x00}}
};

static u_char rfcap_tail[12] = {0x00, 0x00, 0x00, 0x00,
				0x50, 0x00, 0x00, 0xA5,
				0x05, 0x00, 0xC0, 0x00};

void
do_filetype_rfcap(band_config_kw)
	char *band_config_kw;
{
	struct band_table *tp;

	for (tp = band_table; tp->keyword; tp++)
		if (!strcmp(tp->keyword, band_config_kw))
			break;
	if (!tp->keyword) {
		fprintf(stderr, "error: band configuration \"%s\" not known\n",
			band_config_kw);
		exit(1);
	}
	bcopy(tp->bytes, databuf, 4);
	bcopy(rfcap_tail, databuf + 4, 12);
	datalen = 16;
}

static struct file_type {
	char	*keyword;
	void	(*func)();
} file_type_table[] = {
	{"ascii",	do_filetype_ascii},
	{"hex",		do_filetype_hex},
	{"imeisv",	do_filetype_imeisv},
	{"pcm-imei",	do_filetype_pcm_imei},
	{"rfcap",	do_filetype_rfcap},
	/* table search terminator */
	{0,		0}
};

main(argc, argv)
	char **argv;
{
	struct file_type *tp;

	if (argc != 4) {
		fprintf(stderr, "usage: %s dest-file file-type content-str\n",
			argv[0]);
		exit(1);
	}
	for (tp = file_type_table; tp->keyword; tp++)
		if (!strcmp(tp->keyword, argv[2]))
			break;
	if (!tp->func) {
		fprintf(stderr, "error: file type \"%s\" not supported\n",
			argv[2]);
		exit(1);
	}
	tp->func(argv[3]);
	write_binary_file(argv[1]);
	exit(0);
}