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. A small change with regard to the old ASM version is
|
---|
25 | // that it directly writes AIRBOOT.BIN instead of writing AIR-BOOT.COM.
|
---|
26 | // This way the pre and post situations are kept valid.
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 | #include "FIXCODE.H"
|
---|
31 |
|
---|
32 |
|
---|
33 | #ifdef PLATFORM_DOS
|
---|
34 | char welcome[] = "FIXCODE: Hello from DOS !";
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #ifdef PLATFORM_OS2
|
---|
38 | char welcome[] = "FIXCODE: Hello from OS/2 !";
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #ifdef PLATFORM_WINNT
|
---|
42 | char welcome[] = "FIXCODE: Hello from Windows NT !";
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #ifdef PLATFORM_LINUX
|
---|
46 | char welcome[] = "FIXCODE: Hello from Linux !";
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 | /* File names */
|
---|
51 | #define IN_FILE "AIR-BOOT.COM" // Target from assembly.
|
---|
52 | #ifdef PLATFORM_LINUX
|
---|
53 | #define MERGE_FILE "MBR-PROT/MBR-PROT.BIN" // MBR Protection Image.
|
---|
54 | #else
|
---|
55 | #define MERGE_FILE "MBR-PROT\\MBR-PROT.BIN" // MBR Protection Image.
|
---|
56 | #endif
|
---|
57 | #define OUT_FILE "AIRBOOT.BIN" // Generated loader image.
|
---|
58 |
|
---|
59 |
|
---|
60 | /* Copyright message */
|
---|
61 | char Copyright[] = "AiR-BOOT Bootcode Image Fix\n"
|
---|
62 | " - (c) Copyright 2009-2012 by M. Kiewitz\n";
|
---|
63 | /* Progress messages */
|
---|
64 | char LoadCode[] = " - Loading bootcode from file...";
|
---|
65 | char LoadMBR[] = " - Loading MBR-protection from file...";
|
---|
66 | char MergeMBR[] = " - Merging MBR-protection into bootcode...";
|
---|
67 | char CountCode[] = " - Count code in bootcode-image...";
|
---|
68 | char WriteCode[] = " - Saving bootcode to file...";
|
---|
69 | char Okay[] = "ok\n";
|
---|
70 | char Failed[] = "failed\n";
|
---|
71 |
|
---|
72 | /* Error messages */
|
---|
73 | char FailedOpenCode[] = IN_FILE" not found\n";
|
---|
74 | char FailedReadCode[] = "Read "IN_FILE" failed\n";
|
---|
75 | char FailedInvalidCode[] = "Invalid "IN_FILE"\n";
|
---|
76 | char FailedOpenMBR[] = MERGE_FILE" not found\n";
|
---|
77 | char FailedReadMBR[] = "Read "MERGE_FILE" failed\n";
|
---|
78 | char FailedInvalidMBR[] = "Invalid "MERGE_FILE"\n";
|
---|
79 | char FailedWriteCode[] = "Write "OUT_FILE" failed\n";
|
---|
80 |
|
---|
81 | /* The signature we search for in the AIR-BOOT.COM image */
|
---|
82 | char MBRProtectionSignature[] = "AiR-BOOT MBR-Protection Image";
|
---|
83 |
|
---|
84 | /* File buffers */
|
---|
85 | char BootCode[IMAGE_SIZE]; // Buffer for boot-image
|
---|
86 | char MBRProtection[MBRPROT_SIZE]; // Buffer for protection-image
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | /*
|
---|
92 | // Main Entrypoint.
|
---|
93 | */
|
---|
94 | int main(int argc, char* argv[]) {
|
---|
95 | FILE* ifile = NULL;
|
---|
96 | FILE* mfile = NULL;
|
---|
97 | FILE* ofile = NULL;
|
---|
98 | size_t ibytes = 0;
|
---|
99 | size_t mbytes = 0;
|
---|
100 | size_t obytes = 0;
|
---|
101 | unsigned i = 0;
|
---|
102 | unsigned found = 0;
|
---|
103 |
|
---|
104 |
|
---|
105 | #if DEBUG_LEVEL > 0
|
---|
106 | printf("\n%s\n", welcome);
|
---|
107 | printf("Debug level is: %d\n\n", DEBUG_LEVEL);
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | /*
|
---|
111 | // Show copyright message.
|
---|
112 | */
|
---|
113 | printf("%s",Copyright);
|
---|
114 |
|
---|
115 | /*
|
---|
116 | // Load AIR-BOOT.COM
|
---|
117 | */
|
---|
118 | printf("%s",LoadCode);
|
---|
119 | ifile = fopen(IN_FILE, "rb");
|
---|
120 | if (!ifile) {
|
---|
121 | printf("%s",FailedOpenCode);
|
---|
122 | exit(1);
|
---|
123 | }
|
---|
124 | ibytes = fread(BootCode, 1, IMAGE_SIZE, ifile);
|
---|
125 | if (ferror(ifile)) {
|
---|
126 | printf("%s",FailedReadCode);
|
---|
127 | exit(1);
|
---|
128 | }
|
---|
129 | //printf("ibytes: %d\n", ibytes);
|
---|
130 | fread(BootCode, 1, 1, ifile);
|
---|
131 | if (ibytes != IMAGE_SIZE || !feof(ifile)) {
|
---|
132 | printf("%s", FailedInvalidCode);
|
---|
133 | exit(1);
|
---|
134 | }
|
---|
135 | printf("%s", Okay);
|
---|
136 |
|
---|
137 |
|
---|
138 | /*
|
---|
139 | // Load MBR-PROT.BIN
|
---|
140 | */
|
---|
141 | printf("%s",LoadMBR);
|
---|
142 | mfile = fopen(MERGE_FILE, "rb");
|
---|
143 | if (!mfile) {
|
---|
144 | printf("%s",FailedOpenMBR);
|
---|
145 | exit(1);
|
---|
146 | }
|
---|
147 | mbytes = fread(MBRProtection, 1, MBRPROT_SIZE, mfile);
|
---|
148 | if (ferror(mfile)) {
|
---|
149 | printf("%s",FailedReadMBR);
|
---|
150 | exit(1);
|
---|
151 | }
|
---|
152 | fread(MBRProtection, 1, 1, mfile);
|
---|
153 | if (mbytes != MBRPROT_SIZE || !feof(mfile)) {
|
---|
154 | printf("%s", FailedInvalidMBR);
|
---|
155 | exit(1);
|
---|
156 | }
|
---|
157 | printf("%s", Okay);
|
---|
158 |
|
---|
159 |
|
---|
160 | /*
|
---|
161 | // Find Protection Image Signature.
|
---|
162 | // Note that this signature must reside on a sector boundary in the
|
---|
163 | // AIR-BOOT.COM image.
|
---|
164 | */
|
---|
165 | //~ for (i=0; i<55; i++) {
|
---|
166 | for (i=0; i<110; i++) {
|
---|
167 | if (!memcmp(MBRProtectionSignature, &BootCode[i*PAGESIZE], strlen(MBRProtectionSignature))) {
|
---|
168 | found = 1;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /*
|
---|
174 | // Abort if not found.
|
---|
175 | */
|
---|
176 | printf("%s",MergeMBR);
|
---|
177 | if (!found) {
|
---|
178 | printf("%s",Failed);
|
---|
179 | exit(2);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /*
|
---|
183 | // Merge Protection Image.
|
---|
184 | */
|
---|
185 | //~ memcpy(&BootCode[i*SECSIZE], MBRProtection, MBRPROT_SIZE);
|
---|
186 | memcpy(&BootCode[i*PAGESIZE], MBRProtection, MBRPROT_SIZE);
|
---|
187 | printf("%s", Okay);
|
---|
188 |
|
---|
189 |
|
---|
190 | /*
|
---|
191 | // Count Code Sectors.
|
---|
192 | // Obsolete now because since v1.0.8+ the Protection Image has moved just
|
---|
193 | // below the Configuration and the code is always max. size.
|
---|
194 | // Overlap checking is done while assembling AIR-BOOT.ASM.
|
---|
195 | // So we just write the max. code sectors here.
|
---|
196 | */
|
---|
197 | printf("%s", CountCode);
|
---|
198 | BootCode[16] = 53;
|
---|
199 | printf("%s", Okay);
|
---|
200 |
|
---|
201 | /*
|
---|
202 | // Write AIRBOOT.BIN
|
---|
203 | */
|
---|
204 | printf("%s", WriteCode);
|
---|
205 | ofile = fopen(OUT_FILE, "wb");
|
---|
206 | if (!ofile) {
|
---|
207 | printf("%s", FailedWriteCode);
|
---|
208 | exit(3);
|
---|
209 | }
|
---|
210 | obytes = fwrite(BootCode, 1, IMAGE_SIZE, ofile);
|
---|
211 | if (obytes != IMAGE_SIZE || ferror(ofile)) {
|
---|
212 | printf("%s", FailedWriteCode);
|
---|
213 | exit(3);
|
---|
214 | }
|
---|
215 | printf("%s", Okay);
|
---|
216 |
|
---|
217 |
|
---|
218 | /*
|
---|
219 | // Close files.
|
---|
220 | */
|
---|
221 | if (ifile)
|
---|
222 | fclose(ifile);
|
---|
223 | if (mfile)
|
---|
224 | fclose(mfile);
|
---|
225 | if (ofile)
|
---|
226 | fclose(ofile);
|
---|
227 |
|
---|
228 |
|
---|
229 | return 0L;
|
---|
230 | }
|
---|
231 |
|
---|