comparison 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
comparison
equal deleted inserted replaced
8:304ac8119c8a 9:bfcc8180cf3c
1 /*
2 * This module is the output stage of our bootmatch compiler.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "comp_defs.h"
9
10 extern u_char boot_image[BOOT_BLOCK_SIZE];
11 extern struct range range_list[MAX_RANGES];
12 extern unsigned range_count;
13 extern char *output_array_name;
14
15 static FILE *outf;
16
17 static void
18 emit_header()
19 {
20 fputs("#include <sys/types.h>\n", outf);
21 fputs("#include \"bootmatch.h\"\n", outf);
22 putc('\n', outf);
23 }
24
25 static void
26 emit_refbytes_chunk(rp)
27 struct range *rp;
28 {
29 u_char *dp, *endp;
30 unsigned linelen;
31
32 fprintf(outf, "static u_char refbytes_%04X[%u] = {\n", rp->offset,
33 rp->nbytes);
34 dp = boot_image + rp->offset;
35 endp = dp + rp->nbytes;
36 linelen = 0;
37 while (dp < endp) {
38 if (linelen >= 16) {
39 putc('\n', outf);
40 linelen = 0;
41 }
42 fprintf(outf, "0x%02X", *dp++);
43 if (dp < endp)
44 putc(',', outf);
45 }
46 fputs("\n};\n\n", outf);
47 }
48
49 static void
50 emit_refbytes_all()
51 {
52 unsigned n;
53
54 for (n = 0; n < range_count; n++)
55 emit_refbytes_chunk(range_list + n);
56 }
57
58 static void
59 emit_bootmatch_array()
60 {
61 unsigned n;
62 struct range *rp;
63
64 fprintf(outf, "struct bootmatch %s[] = {\n", output_array_name);
65 for (n = 0; n < range_count; n++) {
66 rp = range_list + n;
67 fprintf(outf, "\t{0x%04X, 0x%04X, refbytes_%04X},\n",
68 rp->offset, rp->nbytes, rp->offset);
69 }
70 fputs("\t{0, 0, 0}\n};\n", outf);
71 }
72
73 void
74 emit_output_file(filename)
75 char *filename;
76 {
77 outf = fopen(filename, "w");
78 if (!outf) {
79 perror(filename);
80 exit(1);
81 }
82 emit_header();
83 emit_refbytes_all();
84 emit_bootmatch_array();
85 fclose(outf);
86 }