1 | // AiR-BOOT (c) Copyright 1998-2008 M. Kiewitz
|
---|
2 | //
|
---|
3 | // This file is part of AiR-BOOT
|
---|
4 | //
|
---|
5 | // AiR-BOOT is free software: you can redistribute it and/or modify it under
|
---|
6 | // the terms of the GNU General Public License as published by the Free
|
---|
7 | // Software Foundation, either version 3 of the License, or (at your option)
|
---|
8 | // any later version.
|
---|
9 | //
|
---|
10 | // AiR-BOOT is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
11 | // WARRANTY: without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
12 | // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
13 | // details.
|
---|
14 | //
|
---|
15 | // You should have received a copy of the GNU General Public License along with
|
---|
16 | // AiR-BOOT. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | //
|
---|
18 |
|
---|
19 | /*
|
---|
20 | // FIXCODE.C -- Fix the AiR-BOOT image; include the code-size and MBR prot-img.
|
---|
21 | // This reads AIR-BOOT.COM, merges MBR-PROT.BIN and writes AIRBOOT.BIN.
|
---|
22 | // It is a quick-and-dirty translation of the original DOS-only ASM file.
|
---|
23 | // Of course it's not as small but it's much easier to maintain across
|
---|
24 | // multiple platforms.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | #include "FIXCODE.H"
|
---|
29 |
|
---|
30 |
|
---|
31 | #ifdef PLATFORM_DOS
|
---|
32 | char welcome[] = "FIXCODE: Hello from DOS !";
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #ifdef PLATFORM_OS2
|
---|
36 | char welcome[] = "FIXCODE: Hello from OS/2 !";
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #ifdef PLATFORM_WINNT
|
---|
40 | char welcome[] = "FIXCODE: Hello from Windows NT !";
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #ifdef PLATFORM_LINUX
|
---|
44 | char welcome[] = "FIXCODE: Hello from Linux !";
|
---|
45 | #endif
|
---|
46 |
|
---|
47 |
|
---|
48 | /* File names */
|
---|
49 | #define IN_FILE "AIR-BOOT.COM" // Target from assembly.
|
---|
50 | #ifdef PLATFORM_LINUX
|
---|
51 | #define MERGE_FILE "MBR-PROT/MBR-PROT.BIN" // MBR protection TSR.
|
---|
52 | #else
|
---|
53 | #define MERGE_FILE "MBR-PROT\\MBR-PROT.BIN" // MBR protection TSR.
|
---|
54 | #endif
|
---|
55 | #define OUT_FILE "AIRBOOT.BIN" // Generated loader image.
|
---|
56 |
|
---|
57 |
|
---|
58 | /* Copyright message */
|
---|
59 | char Copyright[] = "AiR-BOOT Bootcode Image Fix\n"
|
---|
60 | " - (c) Copyright 2009-2012 by M. Kiewitz\n";
|
---|
61 | /* Progress messages */
|
---|
62 | char LoadCode[] = " - Loading bootcode from file...";
|
---|
63 | char LoadMBR[] = " - Loading MBR-protection from file...";
|
---|
64 | char MergeMBR[] = " - Merging MBR-protection into bootcode...";
|
---|
65 | char CountCode[] = " - Count code in bootcode-image...";
|
---|
66 | char WriteCode[] = " - Saving bootcode to file...";
|
---|
67 | char Okay[] = "ok\n";
|
---|
68 | char Failed[] = "failed\n";
|
---|
69 |
|
---|
70 | /* Error messages */
|
---|
71 | char FailedOpenCode[] = IN_FILE" not found\n";
|
---|
72 | char FailedReadCode[] = "Read "IN_FILE" failed\n";
|
---|
73 | char FailedInvalidCode[] = "Invalid "IN_FILE"\n";
|
---|
74 | char FailedOpenMBR[] = MERGE_FILE" not found\n";
|
---|
75 | char FailedReadMBR[] = "Read "MERGE_FILE" failed\n";
|
---|
76 | char FailedInvalidMBR[] = "Invalid "MERGE_FILE"\n";
|
---|
77 | char FailedWriteCode[] = "Write "OUT_FILE" failed\n";
|
---|
78 |
|
---|
79 | /* The signature we search for in the AIR-BOOT.COM image */
|
---|
80 | char MBRProtectionSignature[] = "AiR-BOOT MBR-Protection Image";
|
---|
81 |
|
---|
82 | /* File buffers */
|
---|
83 | char BootCode[IMAGE_SIZE]; // Buffer for boot-image
|
---|
84 | char MBRProtection[1024]; // Buffer for protection-image
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | /*
|
---|
90 | // Main Entrypoint.
|
---|
91 | */
|
---|
92 | int main(int argc, char* argv[]) {
|
---|
93 | FILE* ifile = NULL;
|
---|
94 | FILE* mfile = NULL;
|
---|
95 | FILE* ofile = NULL;
|
---|
96 | size_t ibytes = 0;
|
---|
97 | size_t mbytes = 0;
|
---|
98 | size_t obytes = 0;
|
---|
99 | unsigned i = 0;
|
---|
100 | unsigned found = 0;
|
---|
101 |
|
---|
102 |
|
---|
103 | #if DEBUG_LEVEL > 0
|
---|
104 | printf("\n%s\n", welcome);
|
---|
105 | printf("Debug level is: %d\n\n", DEBUG_LEVEL);
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | /*
|
---|
109 | // Show copyright message.
|
---|
110 | */
|
---|
111 | printf("%s",Copyright);
|
---|
112 |
|
---|
113 | /*
|
---|
114 | // Load AIR-BOOT.COM
|
---|
115 | */
|
---|
116 | printf("%s",LoadCode);
|
---|
117 | ifile = fopen(IN_FILE, "rb");
|
---|
118 | if (!ifile) {
|
---|
119 | printf("%s",FailedOpenCode);
|
---|
120 | exit(1);
|
---|
121 | }
|
---|
122 | ibytes = fread(BootCode, 1, IMAGE_SIZE, ifile);
|
---|
123 | if (ferror(ifile)) {
|
---|
124 | printf("%s",FailedReadCode);
|
---|
125 | exit(1);
|
---|
126 | }
|
---|
127 | //printf("ibytes: %d\n", ibytes);
|
---|
128 | fread(BootCode, 1, 1, ifile);
|
---|
129 | if (ibytes != IMAGE_SIZE || !feof(ifile)) {
|
---|
130 | printf("%s", FailedInvalidCode);
|
---|
131 | exit(1);
|
---|
132 | }
|
---|
133 | printf("%s", Okay);
|
---|
134 |
|
---|
135 |
|
---|
136 | /*
|
---|
137 | // Load MBR-PROT.BIN
|
---|
138 | */
|
---|
139 | printf("%s",LoadMBR);
|
---|
140 | mfile = fopen(MERGE_FILE, "rb");
|
---|
141 | if (!mfile) {
|
---|
142 | printf("%s",FailedOpenMBR);
|
---|
143 | exit(1);
|
---|
144 | }
|
---|
145 | mbytes = fread(MBRProtection, 1, MBRPROT_SIZE, mfile);
|
---|
146 | if (ferror(mfile)) {
|
---|
147 | printf("%s",FailedReadMBR);
|
---|
148 | exit(1);
|
---|
149 | }
|
---|
150 | fread(MBRProtection, 1, 1, mfile);
|
---|
151 | if (mbytes != MBRPROT_SIZE || !feof(mfile)) {
|
---|
152 | printf("%s", FailedInvalidMBR);
|
---|
153 | exit(1);
|
---|
154 | }
|
---|
155 | printf("%s", Okay);
|
---|
156 |
|
---|
157 |
|
---|
158 | /*
|
---|
159 | // Find Protection Image Signature.
|
---|
160 | // Note that this signature must reside on a sector boundary.
|
---|
161 | */
|
---|
162 | for (i=0; i<55; i++) {
|
---|
163 | if (!memcmp(MBRProtectionSignature, &BootCode[i*SECSIZE], strlen(MBRProtectionSignature))) {
|
---|
164 | found = 1;
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | /*
|
---|
170 | // Merge Protection Image.
|
---|
171 | */
|
---|
172 | printf("%s",MergeMBR);
|
---|
173 | if (!found) {
|
---|
174 | printf("%s",Failed);
|
---|
175 | exit(2);
|
---|
176 | }
|
---|
177 | memcpy(&BootCode[i*SECSIZE], MBRProtection, MBRPROT_SIZE);
|
---|
178 | printf("%s", Okay);
|
---|
179 |
|
---|
180 |
|
---|
181 | /*
|
---|
182 | // Count Code Sectors.
|
---|
183 | // Obsolete now since the Protection Image has moved just below the
|
---|
184 | // Configuration and the code is always max. size.
|
---|
185 | // Overlap checking is done while assembling AIR-BOOT.ASM.
|
---|
186 | */
|
---|
187 | printf("%s", CountCode);
|
---|
188 | BootCode[16] = 53;
|
---|
189 | printf("%s", Okay);
|
---|
190 |
|
---|
191 | /*
|
---|
192 | // Write AIRBOOT.BIN
|
---|
193 | */
|
---|
194 | printf("%s", WriteCode);
|
---|
195 | ofile = fopen(OUT_FILE, "wb");
|
---|
196 | if (!ofile) {
|
---|
197 | printf("%s", FailedWriteCode);
|
---|
198 | exit(3);
|
---|
199 | }
|
---|
200 | obytes = fwrite(BootCode, 1, IMAGE_SIZE, ofile);
|
---|
201 | if (obytes != IMAGE_SIZE || ferror(ofile)) {
|
---|
202 | printf("%s", FailedWriteCode);
|
---|
203 | exit(3);
|
---|
204 | }
|
---|
205 | printf("%s", Okay);
|
---|
206 |
|
---|
207 |
|
---|
208 | /*
|
---|
209 | // Close files.
|
---|
210 | */
|
---|
211 | if (ifile)
|
---|
212 | fclose(ifile);
|
---|
213 | if (mfile)
|
---|
214 | fclose(mfile);
|
---|
215 | if (ofile)
|
---|
216 | fclose(ofile);
|
---|
217 |
|
---|
218 |
|
---|
219 | return 0L;
|
---|
220 | }
|
---|
221 |
|
---|