annotate bootmatch/comp_output.c @ 14:09c18549921b

add install of bootutil
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 10 Jun 2023 05:09:38 +0000
parents 39b921d851fb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module is the output stage of our bootmatch compiler.
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include "comp_defs.h"
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 extern u_char boot_image[BOOT_BLOCK_SIZE];
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 extern struct range range_list[MAX_RANGES];
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern unsigned range_count;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern char *output_array_name;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 static FILE *outf;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static void
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 emit_header()
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 fputs("#include <sys/types.h>\n", outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 fputs("#include \"bootmatch.h\"\n", outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 putc('\n', outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 static void
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 emit_refbytes_chunk(rp)
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 struct range *rp;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 u_char *dp, *endp;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 unsigned linelen;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 fprintf(outf, "static u_char refbytes_%04X[%u] = {\n", rp->offset,
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 rp->nbytes);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 dp = boot_image + rp->offset;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 endp = dp + rp->nbytes;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 linelen = 0;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 while (dp < endp) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (linelen >= 16) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 putc('\n', outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 linelen = 0;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 fprintf(outf, "0x%02X", *dp++);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (dp < endp)
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 putc(',', outf);
10
39b921d851fb bootmatch: generating C outputs
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
45 linelen++;
9
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 fputs("\n};\n\n", outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 static void
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 emit_refbytes_all()
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 unsigned n;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 for (n = 0; n < range_count; n++)
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 emit_refbytes_chunk(range_list + n);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 static void
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 emit_bootmatch_array()
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 unsigned n;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 struct range *rp;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 fprintf(outf, "struct bootmatch %s[] = {\n", output_array_name);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 for (n = 0; n < range_count; n++) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 rp = range_list + n;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 fprintf(outf, "\t{0x%04X, 0x%04X, refbytes_%04X},\n",
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 rp->offset, rp->nbytes, rp->offset);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 fputs("\t{0, 0, 0}\n};\n", outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 void
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 emit_output_file(filename)
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 char *filename;
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 outf = fopen(filename, "w");
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (!outf) {
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 perror(filename);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 exit(1);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 emit_header();
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 emit_refbytes_all();
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 emit_bootmatch_array();
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 fclose(outf);
bfcc8180cf3c bootmatch compiler written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }