changeset 746:f19c347d0a80

tiffs-mkfile utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 19 Oct 2020 17:39:19 +0000
parents 9e3b1ef1f440
children efd85963ec85
files .hgignore ffstools/newcomp/Makefile ffstools/newcomp/tiffs-mkfile.c
diffstat 3 files changed, 216 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Oct 19 06:47:13 2020 +0000
+++ b/.hgignore	Mon Oct 19 17:39:19 2020 +0000
@@ -12,6 +12,7 @@
 ^ffstools/caltools/fc-rftab2c$
 ^ffstools/newcomp/compile-fc-batt$
 ^ffstools/newcomp/compile-fc-chg$
+^ffstools/newcomp/tiffs-mkfile$
 ^ffstools/tiaud/compile$
 ^ffstools/tiaud/decomp$
 ^ffstools/tiaud/mkvol$
--- a/ffstools/newcomp/Makefile	Mon Oct 19 06:47:13 2020 +0000
+++ b/ffstools/newcomp/Makefile	Mon Oct 19 17:39:19 2020 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	compile-fc-batt compile-fc-chg
+PROGS=	compile-fc-batt compile-fc-chg tiffs-mkfile
 
 INSTALL_PREFIX=	/opt/freecalypso
 
@@ -14,6 +14,9 @@
 compile-fc-chg:		compile-fc-chg.c
 	${CC} ${CFLAGS} -o $@ $@.c
 
+tiffs-mkfile:	tiffs-mkfile.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
 install:	${PROGS}
 	mkdir -p ${INSTBIN}
 	install -c ${PROGS} ${INSTBIN}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ffstools/newcomp/tiffs-mkfile.c	Mon Oct 19 17:39:19 2020 +0000
@@ -0,0 +1,211 @@
+/*
+ * 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);
+}