view bootmatch/comp_output.c @ 22:873d5f33e8f3

doc: beginning with FC-aftermarket-intro
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Jun 2023 06:18:53 +0000
parents 39b921d851fb
children
line wrap: on
line source

/*
 * 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);
		linelen++;
	}
	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);
}