diff bootmatch/comp_output.c @ 9:bfcc8180cf3c

bootmatch compiler written
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 10 Jun 2023 02:55:29 +0000
parents
children 39b921d851fb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bootmatch/comp_output.c	Sat Jun 10 02:55:29 2023 +0000
@@ -0,0 +1,86 @@
+/*
+ * This module is the output stage of our bootmatch compiler.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "comp_defs.h"
+
+extern u_char boot_image[BOOT_BLOCK_SIZE];
+extern struct range range_list[MAX_RANGES];
+extern unsigned range_count;
+extern char *output_array_name;
+
+static FILE *outf;
+
+static void
+emit_header()
+{
+	fputs("#include <sys/types.h>\n", outf);
+	fputs("#include \"bootmatch.h\"\n", outf);
+	putc('\n', outf);
+}
+
+static void
+emit_refbytes_chunk(rp)
+	struct range *rp;
+{
+	u_char *dp, *endp;
+	unsigned linelen;
+
+	fprintf(outf, "static u_char refbytes_%04X[%u] = {\n", rp->offset,
+		rp->nbytes);
+	dp = boot_image + rp->offset;
+	endp = dp + rp->nbytes;
+	linelen = 0;
+	while (dp < endp) {
+		if (linelen >= 16) {
+			putc('\n', outf);
+			linelen = 0;
+		}
+		fprintf(outf, "0x%02X", *dp++);
+		if (dp < endp)
+			putc(',', outf);
+	}
+	fputs("\n};\n\n", outf);
+}
+
+static void
+emit_refbytes_all()
+{
+	unsigned n;
+
+	for (n = 0; n < range_count; n++)
+		emit_refbytes_chunk(range_list + n);
+}
+
+static void
+emit_bootmatch_array()
+{
+	unsigned n;
+	struct range *rp;
+
+	fprintf(outf, "struct bootmatch %s[] = {\n", output_array_name);
+	for (n = 0; n < range_count; n++) {
+		rp = range_list + n;
+		fprintf(outf, "\t{0x%04X, 0x%04X, refbytes_%04X},\n",
+			rp->offset, rp->nbytes, rp->offset);
+	}
+	fputs("\t{0,      0,      0}\n};\n", outf);
+}
+
+void
+emit_output_file(filename)
+	char *filename;
+{
+	outf = fopen(filename, "w");
+	if (!outf) {
+		perror(filename);
+		exit(1);
+	}
+	emit_header();
+	emit_refbytes_all();
+	emit_bootmatch_array();
+	fclose(outf);
+}