[46] | 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.
|
---|
[66] | 21 | // This reads 'airboot.com', merges 'mbr-prot.bin' and writes 'airboot.bin'.
|
---|
[46] | 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
|
---|
[47] | 24 | // multiple platforms. A small change with regard to the old ASM version is
|
---|
[66] | 25 | // that it directly writes 'airboot.bin' instead of writing 'airboot.com'.
|
---|
[47] | 26 | // This way the pre and post situations are kept valid.
|
---|
[46] | 27 | */
|
---|
| 28 |
|
---|
| 29 |
|
---|
[60] | 30 | #include "fixcode.h"
|
---|
[37] | 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 |
|
---|
[46] | 50 | /* File names */
|
---|
[66] | 51 | #define IN_FILE "airboot.com" // Target from assembly.
|
---|
[46] | 52 | #ifdef PLATFORM_LINUX
|
---|
[60] | 53 | #define MERGE_FILE "mbr-prot/mbr-prot.bin" // MBR Protection Image.
|
---|
[46] | 54 | #else
|
---|
[60] | 55 | #define MERGE_FILE "mbr-prot\\mbr-prot.bin" // MBR Protection Image.
|
---|
[46] | 56 | #endif
|
---|
[60] | 57 | #define OUT_FILE "airboot.bin" // Generated loader image.
|
---|
[37] | 58 |
|
---|
| 59 |
|
---|
[46] | 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";
|
---|
[37] | 71 |
|
---|
[46] | 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 */
|
---|
[51] | 85 | char BootCode[IMAGE_SIZE]; // Buffer for boot-image
|
---|
| 86 | char MBRProtection[MBRPROT_SIZE]; // Buffer for protection-image
|
---|
[46] | 87 |
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | // Main Entrypoint.
|
---|
| 93 | */
|
---|
[37] | 94 | int main(int argc, char* argv[]) {
|
---|
[46] | 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;
|
---|
[37] | 103 |
|
---|
[46] | 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);
|
---|
[37] | 119 | ifile = fopen(IN_FILE, "rb");
|
---|
[46] | 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);
|
---|
[37] | 142 | mfile = fopen(MERGE_FILE, "rb");
|
---|
[46] | 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);
|
---|
[37] | 158 |
|
---|
| 159 |
|
---|
[46] | 160 | /*
|
---|
| 161 | // Find Protection Image Signature.
|
---|
[47] | 162 | // Note that this signature must reside on a sector boundary in the
|
---|
| 163 | // AIR-BOOT.COM image.
|
---|
[46] | 164 | */
|
---|
[51] | 165 | //~ for (i=0; i<55; i++) {
|
---|
| 166 | for (i=0; i<110; i++) {
|
---|
| 167 | if (!memcmp(MBRProtectionSignature, &BootCode[i*PAGESIZE], strlen(MBRProtectionSignature))) {
|
---|
[46] | 168 | found = 1;
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[37] | 172 |
|
---|
[46] | 173 | /*
|
---|
[47] | 174 | // Abort if not found.
|
---|
[46] | 175 | */
|
---|
| 176 | printf("%s",MergeMBR);
|
---|
| 177 | if (!found) {
|
---|
| 178 | printf("%s",Failed);
|
---|
| 179 | exit(2);
|
---|
| 180 | }
|
---|
[47] | 181 |
|
---|
| 182 | /*
|
---|
| 183 | // Merge Protection Image.
|
---|
| 184 | */
|
---|
[51] | 185 | //~ memcpy(&BootCode[i*SECSIZE], MBRProtection, MBRPROT_SIZE);
|
---|
| 186 | memcpy(&BootCode[i*PAGESIZE], MBRProtection, MBRPROT_SIZE);
|
---|
[46] | 187 | printf("%s", Okay);
|
---|
[37] | 188 |
|
---|
| 189 |
|
---|
[46] | 190 | /*
|
---|
| 191 | // Count Code Sectors.
|
---|
[54] | 192 | // Obsolete now because since v1.0.8+ the Protection Image has moved just
|
---|
[47] | 193 | // below the Configuration and the code is always max. size.
|
---|
[46] | 194 | // Overlap checking is done while assembling AIR-BOOT.ASM.
|
---|
[47] | 195 | // So we just write the max. code sectors here.
|
---|
[46] | 196 | */
|
---|
| 197 | printf("%s", CountCode);
|
---|
| 198 | BootCode[16] = 53;
|
---|
| 199 | printf("%s", Okay);
|
---|
[37] | 200 |
|
---|
[46] | 201 | /*
|
---|
[60] | 202 | // Write 'airboot.bin'
|
---|
[46] | 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);
|
---|
[37] | 216 |
|
---|
[46] | 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 |
|
---|
[37] | 229 | return 0L;
|
---|
| 230 | }
|
---|
| 231 |
|
---|