]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | \r | |
3 | Abstract:\r | |
4 | Patch the BPB information in boot sector image file.\r | |
5 | Patch the MBR code in MBR image file.\r | |
6 | \r | |
7 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
8 | This program and the accompanying materials \r | |
9 | are licensed and made available under the terms and conditions of the BSD License \r | |
10 | which accompanies this distribution. The full text of the license may be found at \r | |
11 | http://opensource.org/licenses/bsd-license.php \r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
15 | \r | |
16 | **/\r | |
17 | \r | |
18 | #include <stdio.h>\r | |
19 | #include <string.h>\r | |
20 | #include "fat.h"\r | |
21 | #include "mbr.h"\r | |
22 | #include "EfiUtilityMsgs.h"\r | |
23 | #include "ParseInf.h"\r | |
24 | \r | |
25 | #define DEBUG_WARN 0x1\r | |
26 | #define DEBUG_ERROR 0x2\r | |
27 | \r | |
28 | //\r | |
29 | // Utility Name\r | |
30 | //\r | |
31 | #define UTILITY_NAME "BootSectImage"\r | |
32 | \r | |
33 | //\r | |
34 | // Utility version information\r | |
35 | //\r | |
36 | #define UTILITY_MAJOR_VERSION 0\r | |
37 | #define UTILITY_MINOR_VERSION 1\r | |
38 | \r | |
39 | void\r | |
40 | Version (\r | |
41 | void\r | |
42 | )\r | |
43 | /*++\r | |
44 | \r | |
45 | Routine Description:\r | |
46 | \r | |
47 | Displays the standard utility information to SDTOUT\r | |
48 | \r | |
49 | Arguments:\r | |
50 | \r | |
51 | None\r | |
52 | \r | |
53 | Returns:\r | |
54 | \r | |
55 | None\r | |
56 | \r | |
57 | --*/\r | |
58 | {\r | |
59 | printf ("%s v%d.%d %s - Utility to break a file into two pieces at the specified offset.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r | |
60 | printf ("Copyright (c) 1999-2010 Intel Corporation. All rights reserved.\n");\r | |
61 | }\r | |
62 | \r | |
63 | void\r | |
64 | Usage (\r | |
65 | void\r | |
66 | )\r | |
67 | /*++\r | |
68 | \r | |
69 | Routine Description:\r | |
70 | \r | |
71 | GC_TODO: Add function description\r | |
72 | \r | |
73 | Arguments:\r | |
74 | \r | |
75 | \r | |
76 | Returns:\r | |
77 | \r | |
78 | GC_TODO: add return values\r | |
79 | \r | |
80 | --*/\r | |
81 | {\r | |
82 | Version();\r | |
83 | printf ("\nUsage: \n\\r | |
84 | BootSectImage\n\\r | |
85 | [-f, --force force patch even if the FAT type of SrcImage and DstImage mismatch]\n\\r | |
86 | [-m, --mbr process MBR instead of boot sector]\n\\r | |
87 | [-p, --parse parse SrcImageFile]\n\\r | |
88 | [-o, --output DstImage]\n\\r | |
89 | [-g, --patch patch DstImage using data from SrcImageFile]\n\\r | |
90 | [-v, --verbose]\n\\r | |
91 | [--version]\n\\r | |
92 | [-q, --quiet disable all messages except fatal errors]\n\\r | |
93 | [-d, --debug[#]\n\\r | |
94 | [-h, --help]\n\\r | |
95 | [SrcImageFile]\n");\r | |
96 | }\r | |
97 | \r | |
98 | int WriteToFile (\r | |
99 | void *BootSector, \r | |
100 | char *FileName\r | |
101 | )\r | |
102 | /*++\r | |
103 | Routine Description:\r | |
104 | Write 512 bytes boot sector to file.\r | |
105 | \r | |
106 | Arguments:\r | |
107 | BootSector - point to a buffer containing 512 bytes boot sector to write\r | |
108 | FileName - file to write to\r | |
109 | \r | |
110 | Return:\r | |
111 | int - number of bytes wrote,\r | |
112 | 512 indicates write successful\r | |
113 | 0 indicates write failure\r | |
114 | --*/\r | |
115 | {\r | |
116 | FILE *FileHandle;\r | |
117 | int result;\r | |
118 | \r | |
119 | FileHandle = fopen (FileName, "r+b");\r | |
120 | if (FileHandle == NULL) {\r | |
121 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "Open file: %s", FileName);\r | |
122 | return 0;\r | |
123 | }\r | |
124 | fseek (FileHandle, 0, SEEK_SET);\r | |
125 | \r | |
126 | result = fwrite (BootSector, 1, 512, FileHandle);\r | |
127 | if (result != 512) {\r | |
128 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "Write file: %s", FileName);\r | |
129 | result = 0;\r | |
130 | }\r | |
131 | \r | |
132 | fclose (FileHandle);\r | |
133 | return result;\r | |
134 | }\r | |
135 | \r | |
136 | int ReadFromFile (\r | |
137 | void *BootSector, \r | |
138 | char *FileName\r | |
139 | )\r | |
140 | /*++\r | |
141 | Routine Description:\r | |
142 | Read first 512 bytes from file.\r | |
143 | \r | |
144 | Arguments:\r | |
145 | BootSector - point to a buffer receiving the first 512 bytes data from file\r | |
146 | FileName - file to read from\r | |
147 | \r | |
148 | Return:\r | |
149 | int - number of bytes read,\r | |
150 | 512 indicates read successful\r | |
151 | 0 indicates read failure\r | |
152 | --*/\r | |
153 | {\r | |
154 | FILE *FileHandle;\r | |
155 | int result;\r | |
156 | \r | |
157 | FileHandle = fopen (FileName, "rb");\r | |
158 | if (FileHandle == NULL) {\r | |
159 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E0001: Error opening file: %s", FileName);\r | |
160 | return 0;\r | |
161 | }\r | |
162 | \r | |
163 | result = fread (BootSector, 1, 512, FileHandle);\r | |
164 | if (result != 512) {\r | |
165 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E0004: Error reading file: %s", FileName);\r | |
166 | result = 0;\r | |
167 | }\r | |
168 | \r | |
169 | fclose (FileHandle);\r | |
170 | return result;\r | |
171 | }\r | |
172 | \r | |
173 | char *\r | |
174 | FatTypeToString (\r | |
175 | IN FAT_TYPE FatType\r | |
176 | )\r | |
177 | /*++\r | |
178 | Routine Description:\r | |
179 | Convert enum type of FatType to string\r | |
180 | --*/\r | |
181 | {\r | |
182 | switch (FatType) {\r | |
183 | case FatTypeFat12:\r | |
184 | return "FAT12";\r | |
185 | case FatTypeFat16:\r | |
186 | return "FAT16";\r | |
187 | case FatTypeFat32:\r | |
188 | return "FAT32";\r | |
189 | default:\r | |
190 | break;\r | |
191 | }\r | |
192 | return "FAT Unknown";\r | |
193 | }\r | |
194 | \r | |
195 | FAT_TYPE\r | |
196 | GetFatType (\r | |
197 | IN FAT_BPB_STRUCT *FatBpb\r | |
198 | )\r | |
199 | /*++\r | |
200 | Routine Description:\r | |
201 | Determine the FAT type according to BIOS Paramater Block (BPB) data\r | |
202 | \r | |
203 | Arguments:\r | |
204 | FatBpb - BIOS Parameter Block (BPB) data, 512 Bytes\r | |
205 | \r | |
206 | Return:\r | |
207 | FatTypeUnknown - Cannot determine the FAT type\r | |
208 | FatTypeFat12 - FAT12\r | |
209 | FatTypeFat16 - FAT16\r | |
210 | FatTypeFat32 - FAT32\r | |
211 | --*/\r | |
212 | {\r | |
213 | FAT_TYPE FatType;\r | |
214 | UINTN RootDirSectors;\r | |
215 | UINTN FATSz;\r | |
216 | UINTN TotSec;\r | |
217 | UINTN DataSec;\r | |
218 | UINTN CountOfClusters;\r | |
219 | CHAR8 FilSysType[9];\r | |
220 | \r | |
221 | FatType = FatTypeUnknown;\r | |
222 | \r | |
223 | //\r | |
224 | // Simple check\r | |
225 | //\r | |
226 | if (FatBpb->Fat12_16.Signature != FAT_BS_SIGNATURE) {\r | |
227 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - Signature Invalid - %04x, expected: %04x",\r | |
228 | FatBpb->Fat12_16.Signature, FAT_BS_SIGNATURE);\r | |
229 | return FatTypeUnknown;\r | |
230 | }\r | |
231 | \r | |
232 | //\r | |
233 | // Check according to FAT spec\r | |
234 | //\r | |
235 | if ((FatBpb->Fat12_16.BS_jmpBoot[0] != FAT_BS_JMP1) &&\r | |
236 | (FatBpb->Fat12_16.BS_jmpBoot[0] != FAT_BS_JMP2)) {\r | |
237 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BS_jmpBoot - %02x, expected: %02x or %02x",\r | |
238 | FatBpb->Fat12_16.BS_jmpBoot[0], FAT_BS_JMP1, FAT_BS_JMP2);\r | |
239 | return FatTypeUnknown;\r | |
240 | }\r | |
241 | \r | |
242 | if ((FatBpb->Fat12_16.BPB_BytsPerSec != 512) &&\r | |
243 | (FatBpb->Fat12_16.BPB_BytsPerSec != 1024) &&\r | |
244 | (FatBpb->Fat12_16.BPB_BytsPerSec != 2048) &&\r | |
245 | (FatBpb->Fat12_16.BPB_BytsPerSec != 4096)) {\r | |
246 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x, %04x, %04x, or %04x",\r | |
247 | FatBpb->Fat12_16.BPB_BytsPerSec, 512, 1024, 2048, 4096);\r | |
248 | return FatTypeUnknown;\r | |
249 | }\r | |
250 | if (FatBpb->Fat12_16.BPB_BytsPerSec != 512) {\r | |
251 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x",\r | |
252 | FatBpb->Fat12_16.BPB_BytsPerSec, 512);\r | |
253 | }\r | |
254 | if ((FatBpb->Fat12_16.BPB_SecPerClus != 1) &&\r | |
255 | (FatBpb->Fat12_16.BPB_SecPerClus != 2) &&\r | |
256 | (FatBpb->Fat12_16.BPB_SecPerClus != 4) &&\r | |
257 | (FatBpb->Fat12_16.BPB_SecPerClus != 8) &&\r | |
258 | (FatBpb->Fat12_16.BPB_SecPerClus != 16) &&\r | |
259 | (FatBpb->Fat12_16.BPB_SecPerClus != 32) &&\r | |
260 | (FatBpb->Fat12_16.BPB_SecPerClus != 64) &&\r | |
261 | (FatBpb->Fat12_16.BPB_SecPerClus != 128)) {\r | |
262 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_SecPerClus - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",\r | |
263 | FatBpb->Fat12_16.BPB_BytsPerSec, 1, 2, 4, 8, 16, 32, 64, 128);\r | |
264 | return FatTypeUnknown;\r | |
265 | }\r | |
266 | if (FatBpb->Fat12_16.BPB_BytsPerSec * FatBpb->Fat12_16.BPB_SecPerClus > 32 * 1024) {\r | |
267 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec * BPB_SecPerClus - %08x, expected: <= %08x",\r | |
268 | FatBpb->Fat12_16.BPB_BytsPerSec * FatBpb->Fat12_16.BPB_SecPerClus, 32 * 1024);\r | |
269 | return FatTypeUnknown;\r | |
270 | }\r | |
271 | if (FatBpb->Fat12_16.BPB_RsvdSecCnt == 0) {\r | |
272 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_RsvdSecCnt - %04x, expected: Non-Zero Value",\r | |
273 | FatBpb->Fat12_16.BPB_RsvdSecCnt);\r | |
274 | return FatTypeUnknown;\r | |
275 | }\r | |
276 | if (FatBpb->Fat12_16.BPB_NumFATs != 2) {\r | |
277 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT - BPB_NumFATs - %02x, expected: %02x",\r | |
278 | FatBpb->Fat12_16.BPB_NumFATs, 2);\r | |
279 | }\r | |
280 | if ((FatBpb->Fat12_16.BPB_Media != 0xF0) &&\r | |
281 | (FatBpb->Fat12_16.BPB_Media != 0xF8) &&\r | |
282 | (FatBpb->Fat12_16.BPB_Media != 0xF9) &&\r | |
283 | (FatBpb->Fat12_16.BPB_Media != 0xFA) &&\r | |
284 | (FatBpb->Fat12_16.BPB_Media != 0xFB) &&\r | |
285 | (FatBpb->Fat12_16.BPB_Media != 0xFC) &&\r | |
286 | (FatBpb->Fat12_16.BPB_Media != 0xFD) &&\r | |
287 | (FatBpb->Fat12_16.BPB_Media != 0xFE) &&\r | |
288 | (FatBpb->Fat12_16.BPB_Media != 0xFF)) {\r | |
289 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_Media - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",\r | |
290 | FatBpb->Fat12_16.BPB_Media, 0xF0, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF);\r | |
291 | return FatTypeUnknown;\r | |
292 | }\r | |
293 | \r | |
294 | //\r | |
295 | // Algo in FAT spec\r | |
296 | //\r | |
297 | RootDirSectors = ((FatBpb->Fat12_16.BPB_RootEntCnt * sizeof(FAT_DIRECTORY_ENTRY)) +\r | |
298 | (FatBpb->Fat12_16.BPB_BytsPerSec - 1)) /\r | |
299 | FatBpb->Fat12_16.BPB_BytsPerSec;\r | |
300 | \r | |
301 | if (FatBpb->Fat12_16.BPB_FATSz16 != 0) {\r | |
302 | FATSz = FatBpb->Fat12_16.BPB_FATSz16;\r | |
303 | } else {\r | |
304 | FATSz = FatBpb->Fat32.BPB_FATSz32;\r | |
305 | }\r | |
306 | if (FATSz == 0) {\r | |
307 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_FATSz16, BPB_FATSz32 - 0, expected: Non-Zero Value");\r | |
308 | return FatTypeUnknown;\r | |
309 | }\r | |
310 | \r | |
311 | if (FatBpb->Fat12_16.BPB_TotSec16 != 0) {\r | |
312 | TotSec = FatBpb->Fat12_16.BPB_TotSec16;\r | |
313 | } else {\r | |
314 | TotSec = FatBpb->Fat12_16.BPB_TotSec32;\r | |
315 | }\r | |
316 | if (TotSec == 0) {\r | |
317 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_TotSec16, BPB_TotSec32 - 0, expected: Non-Zero Value");\r | |
318 | return FatTypeUnknown;\r | |
319 | }\r | |
320 | \r | |
321 | DataSec = TotSec - (\r | |
322 | FatBpb->Fat12_16.BPB_RsvdSecCnt +\r | |
323 | FatBpb->Fat12_16.BPB_NumFATs * FATSz +\r | |
324 | RootDirSectors\r | |
325 | );\r | |
326 | \r | |
327 | CountOfClusters = DataSec / FatBpb->Fat12_16.BPB_SecPerClus;\r | |
328 | \r | |
329 | if (CountOfClusters < FAT_MAX_FAT12_CLUSTER) {\r | |
330 | FatType = FatTypeFat12;\r | |
331 | } else if (CountOfClusters < FAT_MAX_FAT16_CLUSTER) {\r | |
332 | FatType = FatTypeFat16;\r | |
333 | } else {\r | |
334 | FatType = FatTypeFat32;\r | |
335 | }\r | |
336 | //\r | |
337 | // Check according to FAT spec\r | |
338 | //\r | |
339 | if (((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) &&\r | |
340 | (FatBpb->Fat12_16.BPB_RsvdSecCnt != 1)) {\r | |
341 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT12_16 - BPB_RsvdSecCnt - %04x, expected: %04x",\r | |
342 | FatBpb->Fat12_16.BPB_RsvdSecCnt, 1);\r | |
343 | }\r | |
344 | if ((FatType == FatTypeFat32) &&\r | |
345 | (FatBpb->Fat12_16.BPB_RsvdSecCnt != 32)) {\r | |
346 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_RsvdSecCnt - %04x, expected: %04x",\r | |
347 | FatBpb->Fat12_16.BPB_RsvdSecCnt, 32);\r | |
348 | }\r | |
349 | if ((FatType == FatTypeFat16) &&\r | |
350 | (FatBpb->Fat12_16.BPB_RootEntCnt != 512)) {\r | |
351 | printf ("WARNING: FAT16: BPB_RootEntCnt - %04x, expected - %04x\n",\r | |
352 | FatBpb->Fat12_16.BPB_RootEntCnt, 512);\r | |
353 | }\r | |
354 | if ((FatType == FatTypeFat32) &&\r | |
355 | (FatBpb->Fat12_16.BPB_RootEntCnt != 0)) {\r | |
356 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_RootEntCnt - %04x, expected: %04x",\r | |
357 | FatBpb->Fat12_16.BPB_RootEntCnt, 0);\r | |
358 | return FatTypeUnknown;\r | |
359 | }\r | |
360 | if ((FatType == FatTypeFat32) &&\r | |
361 | (FatBpb->Fat12_16.BPB_TotSec16 != 0)) {\r | |
362 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_TotSec16 - %04x, expected: %04x",\r | |
363 | FatBpb->Fat12_16.BPB_TotSec16, 0);\r | |
364 | return FatTypeUnknown;\r | |
365 | }\r | |
366 | if ((FatType == FatTypeFat32) &&\r | |
367 | (FatBpb->Fat12_16.BPB_FATSz16 != 0)) {\r | |
368 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_FATSz16 - %04x, expected: %04x",\r | |
369 | FatBpb->Fat12_16.BPB_FATSz16, 0);\r | |
370 | return FatTypeUnknown;\r | |
371 | }\r | |
372 | if ((FatType == FatTypeFat32) &&\r | |
373 | (FatBpb->Fat12_16.BPB_TotSec32 == 0)) {\r | |
374 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_TotSec32 - %04x, expected: Non-Zero",\r | |
375 | (unsigned) FatBpb->Fat12_16.BPB_TotSec32);\r | |
376 | return FatTypeUnknown;\r | |
377 | }\r | |
378 | if ((FatType == FatTypeFat32) &&\r | |
379 | (FatBpb->Fat32.BPB_FATSz32 == 0)) {\r | |
380 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_FATSz32 - %08x, expected: Non-Zero",\r | |
381 | (unsigned) FatBpb->Fat32.BPB_FATSz32);\r | |
382 | return FatTypeUnknown;\r | |
383 | }\r | |
384 | if ((FatType == FatTypeFat32) &&\r | |
385 | (FatBpb->Fat32.BPB_FSVer != 0)) {\r | |
386 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_FSVer - %08x, expected: %04x",\r | |
387 | FatBpb->Fat32.BPB_FSVer, 0);\r | |
388 | }\r | |
389 | if ((FatType == FatTypeFat32) &&\r | |
390 | (FatBpb->Fat32.BPB_RootClus != 2)) {\r | |
391 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_RootClus - %08x, expected: %04x",\r | |
392 | (unsigned) FatBpb->Fat32.BPB_RootClus, 2);\r | |
393 | }\r | |
394 | if ((FatType == FatTypeFat32) &&\r | |
395 | (FatBpb->Fat32.BPB_FSInfo != 1)) {\r | |
396 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_FSInfo - %08x, expected: %04x",\r | |
397 | FatBpb->Fat32.BPB_FSInfo, 1);\r | |
398 | }\r | |
399 | if ((FatType == FatTypeFat32) &&\r | |
400 | (FatBpb->Fat32.BPB_BkBootSec != 6)) {\r | |
401 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_BkBootSec - %08x, expected: %04x",\r | |
402 | FatBpb->Fat32.BPB_BkBootSec, 6);\r | |
403 | }\r | |
404 | if ((FatType == FatTypeFat32) &&\r | |
405 | ((*(UINT32 *)FatBpb->Fat32.BPB_Reserved != 0) ||\r | |
406 | (*((UINT32 *)FatBpb->Fat32.BPB_Reserved + 1) != 0) ||\r | |
407 | (*((UINT32 *)FatBpb->Fat32.BPB_Reserved + 2) != 0))) {\r | |
408 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_Reserved - %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x, expected: 0",\r | |
409 | FatBpb->Fat32.BPB_Reserved[0],\r | |
410 | FatBpb->Fat32.BPB_Reserved[1],\r | |
411 | FatBpb->Fat32.BPB_Reserved[2],\r | |
412 | FatBpb->Fat32.BPB_Reserved[3],\r | |
413 | FatBpb->Fat32.BPB_Reserved[4],\r | |
414 | FatBpb->Fat32.BPB_Reserved[5],\r | |
415 | FatBpb->Fat32.BPB_Reserved[6],\r | |
416 | FatBpb->Fat32.BPB_Reserved[7],\r | |
417 | FatBpb->Fat32.BPB_Reserved[8],\r | |
418 | FatBpb->Fat32.BPB_Reserved[9],\r | |
419 | FatBpb->Fat32.BPB_Reserved[10],\r | |
420 | FatBpb->Fat32.BPB_Reserved[11]);\r | |
421 | return FatTypeUnknown;\r | |
422 | }\r | |
423 | if (((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) &&\r | |
424 | (FatBpb->Fat12_16.BS_Reserved1 != 0)) {\r | |
425 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT12_16 - BS_Reserved1 - %02x, expected: 0\n",\r | |
426 | FatBpb->Fat12_16.BS_Reserved1);\r | |
427 | return FatTypeUnknown;\r | |
428 | }\r | |
429 | if ((FatType == FatTypeFat32) &&\r | |
430 | (FatBpb->Fat32.BS_Reserved1 != 0)) {\r | |
431 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BS_Reserved1 - %02x, expected: 0\n",\r | |
432 | FatBpb->Fat32.BS_Reserved1);\r | |
433 | return FatTypeUnknown;\r | |
434 | }\r | |
435 | if (((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) &&\r | |
436 | (FatBpb->Fat12_16.BS_BootSig != FAT_BS_BOOTSIG)) {\r | |
437 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT12_16 - BS_BootSig - %02x, expected: %02x\n",\r | |
438 | FatBpb->Fat12_16.BS_BootSig, FAT_BS_BOOTSIG);\r | |
439 | return FatTypeUnknown;\r | |
440 | }\r | |
441 | if ((FatType == FatTypeFat32) &&\r | |
442 | (FatBpb->Fat32.BS_BootSig != FAT_BS_BOOTSIG)) {\r | |
443 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BS_BootSig - %02x, expected: %02x\n",\r | |
444 | FatBpb->Fat32.BS_BootSig, FAT_BS_BOOTSIG);\r | |
445 | return FatTypeUnknown;\r | |
446 | }\r | |
447 | \r | |
448 | if ((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) {\r | |
449 | memcpy (FilSysType, FatBpb->Fat12_16.BS_FilSysType, 8);\r | |
450 | FilSysType[8] = 0;\r | |
451 | if ((FatType == FatTypeFat12) && \r | |
452 | (strcmp (FilSysType, FAT12_FILSYSTYPE) != 0) &&\r | |
453 | (strcmp (FilSysType, FAT_FILSYSTYPE) != 0)) {\r | |
454 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT12 - BS_FilSysType - %s, expected: %s, or %s\n",\r | |
455 | FilSysType, FAT12_FILSYSTYPE, FAT_FILSYSTYPE);\r | |
456 | }\r | |
457 | if ((FatType == FatTypeFat16) && \r | |
458 | (strcmp (FilSysType, FAT16_FILSYSTYPE) != 0) &&\r | |
459 | (strcmp (FilSysType, FAT_FILSYSTYPE) != 0)) {\r | |
460 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT16 - BS_FilSysType - %s, expected: %s, or %s\n",\r | |
461 | FilSysType, FAT16_FILSYSTYPE, FAT_FILSYSTYPE);\r | |
462 | }\r | |
463 | }\r | |
464 | if (FatType == FatTypeFat32) {\r | |
465 | memcpy (FilSysType, FatBpb->Fat32.BS_FilSysType, 8);\r | |
466 | FilSysType[8] = 0;\r | |
467 | if (strcmp (FilSysType, FAT32_FILSYSTYPE) != 0) {\r | |
468 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BS_FilSysType - %s, expected: %s\n",\r | |
469 | FilSysType, FAT32_FILSYSTYPE);\r | |
470 | }\r | |
471 | }\r | |
472 | \r | |
473 | //\r | |
474 | // pass all check, get FAT type\r | |
475 | //\r | |
476 | return FatType;\r | |
477 | }\r | |
478 | \r | |
479 | \r | |
480 | void\r | |
481 | ParseBootSector (\r | |
482 | char *FileName\r | |
483 | )\r | |
484 | {\r | |
485 | FAT_BPB_STRUCT FatBpb;\r | |
486 | FAT_TYPE FatType;\r | |
487 | \r | |
488 | if (ReadFromFile ((void *)&FatBpb, FileName) == 0) {\r | |
489 | return ;\r | |
490 | }\r | |
491 | \r | |
492 | FatType = GetFatType (&FatBpb);\r | |
493 | if (FatType <= FatTypeUnknown || FatType >= FatTypeMax) {\r | |
494 | printf ("ERROR: E3002: Unknown FAT Type!\n");\r | |
495 | return;\r | |
496 | }\r | |
497 | \r | |
498 | printf ("\nBoot Sector %s:\n", FatTypeToString (FatType));\r | |
499 | printf ("\n");\r | |
500 | printf (" Offset Title Data\n");\r | |
501 | printf ("==================================================================\n");\r | |
502 | printf (" 0 JMP instruction %02x %02x %02x\n",\r | |
503 | FatBpb.Fat12_16.BS_jmpBoot[0],\r | |
504 | FatBpb.Fat12_16.BS_jmpBoot[1],\r | |
505 | FatBpb.Fat12_16.BS_jmpBoot[2]);\r | |
506 | printf (" 3 OEM %c%c%c%c%c%c%c%c\n",\r | |
507 | FatBpb.Fat12_16.BS_OEMName[0],\r | |
508 | FatBpb.Fat12_16.BS_OEMName[1],\r | |
509 | FatBpb.Fat12_16.BS_OEMName[2],\r | |
510 | FatBpb.Fat12_16.BS_OEMName[3],\r | |
511 | FatBpb.Fat12_16.BS_OEMName[4],\r | |
512 | FatBpb.Fat12_16.BS_OEMName[5],\r | |
513 | FatBpb.Fat12_16.BS_OEMName[6],\r | |
514 | FatBpb.Fat12_16.BS_OEMName[7]);\r | |
515 | printf ("\n");\r | |
516 | printf ("BIOS Parameter Block\n");\r | |
517 | printf (" B Bytes per sector %04x\n", FatBpb.Fat12_16.BPB_BytsPerSec);\r | |
518 | printf (" D Sectors per cluster %02x\n", FatBpb.Fat12_16.BPB_SecPerClus);\r | |
519 | printf (" E Reserved sectors %04x\n", FatBpb.Fat12_16.BPB_RsvdSecCnt);\r | |
520 | printf (" 10 Number of FATs %02x\n", FatBpb.Fat12_16.BPB_NumFATs);\r | |
521 | printf (" 11 Root entries %04x\n", FatBpb.Fat12_16.BPB_RootEntCnt);\r | |
522 | printf (" 13 Sectors (under 32MB) %04x\n", FatBpb.Fat12_16.BPB_TotSec16);\r | |
523 | printf (" 15 Media descriptor %02x\n", FatBpb.Fat12_16.BPB_Media);\r | |
524 | printf (" 16 Sectors per FAT (small vol.) %04x\n", FatBpb.Fat12_16.BPB_FATSz16);\r | |
525 | printf (" 18 Sectors per track %04x\n", FatBpb.Fat12_16.BPB_SecPerTrk);\r | |
526 | printf (" 1A Heads %04x\n", FatBpb.Fat12_16.BPB_NumHeads);\r | |
527 | printf (" 1C Hidden sectors %08x\n", (unsigned) FatBpb.Fat12_16.BPB_HiddSec);\r | |
528 | printf (" 20 Sectors (over 32MB) %08x\n", (unsigned) FatBpb.Fat12_16.BPB_TotSec32);\r | |
529 | printf ("\n");\r | |
530 | if (FatType != FatTypeFat32) {\r | |
531 | printf (" 24 BIOS drive %02x\n", FatBpb.Fat12_16.BS_DrvNum);\r | |
532 | printf (" 25 (Unused) %02x\n", FatBpb.Fat12_16.BS_Reserved1);\r | |
533 | printf (" 26 Ext. boot signature %02x\n", FatBpb.Fat12_16.BS_BootSig);\r | |
534 | printf (" 27 Volume serial number %08x\n", (unsigned) FatBpb.Fat12_16.BS_VolID);\r | |
535 | printf (" 2B Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",\r | |
536 | FatBpb.Fat12_16.BS_VolLab[0],\r | |
537 | FatBpb.Fat12_16.BS_VolLab[1],\r | |
538 | FatBpb.Fat12_16.BS_VolLab[2],\r | |
539 | FatBpb.Fat12_16.BS_VolLab[3],\r | |
540 | FatBpb.Fat12_16.BS_VolLab[4],\r | |
541 | FatBpb.Fat12_16.BS_VolLab[5],\r | |
542 | FatBpb.Fat12_16.BS_VolLab[6],\r | |
543 | FatBpb.Fat12_16.BS_VolLab[7],\r | |
544 | FatBpb.Fat12_16.BS_VolLab[8],\r | |
545 | FatBpb.Fat12_16.BS_VolLab[9],\r | |
546 | FatBpb.Fat12_16.BS_VolLab[10]);\r | |
547 | printf (" 36 File system %c%c%c%c%c%c%c%c\n",\r | |
548 | FatBpb.Fat12_16.BS_FilSysType[0],\r | |
549 | FatBpb.Fat12_16.BS_FilSysType[1],\r | |
550 | FatBpb.Fat12_16.BS_FilSysType[2],\r | |
551 | FatBpb.Fat12_16.BS_FilSysType[3],\r | |
552 | FatBpb.Fat12_16.BS_FilSysType[4],\r | |
553 | FatBpb.Fat12_16.BS_FilSysType[5],\r | |
554 | FatBpb.Fat12_16.BS_FilSysType[6],\r | |
555 | FatBpb.Fat12_16.BS_FilSysType[7]);\r | |
556 | printf ("\n");\r | |
557 | } else {\r | |
558 | printf ("FAT32 Section\n");\r | |
559 | printf (" 24 Sectors per FAT (large vol.) %08x\n", (unsigned) FatBpb.Fat32.BPB_FATSz32);\r | |
560 | printf (" 28 Flags %04x\n", FatBpb.Fat32.BPB_ExtFlags);\r | |
561 | printf (" 2A Version %04x\n", FatBpb.Fat32.BPB_FSVer);\r | |
562 | printf (" 2C Root dir 1st cluster %08x\n", (unsigned) FatBpb.Fat32.BPB_RootClus);\r | |
563 | printf (" 30 FSInfo sector %04x\n", FatBpb.Fat32.BPB_FSInfo);\r | |
564 | printf (" 32 Backup boot sector %04x\n", FatBpb.Fat32.BPB_BkBootSec);\r | |
565 | printf (" 34 (Reserved) %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",\r | |
566 | FatBpb.Fat32.BPB_Reserved[0],\r | |
567 | FatBpb.Fat32.BPB_Reserved[1],\r | |
568 | FatBpb.Fat32.BPB_Reserved[2],\r | |
569 | FatBpb.Fat32.BPB_Reserved[3],\r | |
570 | FatBpb.Fat32.BPB_Reserved[4],\r | |
571 | FatBpb.Fat32.BPB_Reserved[5],\r | |
572 | FatBpb.Fat32.BPB_Reserved[6],\r | |
573 | FatBpb.Fat32.BPB_Reserved[7],\r | |
574 | FatBpb.Fat32.BPB_Reserved[8],\r | |
575 | FatBpb.Fat32.BPB_Reserved[9],\r | |
576 | FatBpb.Fat32.BPB_Reserved[10],\r | |
577 | FatBpb.Fat32.BPB_Reserved[11]);\r | |
578 | printf ("\n");\r | |
579 | printf (" 40 BIOS drive %02x\n", FatBpb.Fat32.BS_DrvNum);\r | |
580 | printf (" 41 (Unused) %02x\n", FatBpb.Fat32.BS_Reserved1);\r | |
581 | printf (" 42 Ext. boot signature %02x\n", FatBpb.Fat32.BS_BootSig);\r | |
582 | printf (" 43 Volume serial number %08x\n", (unsigned) FatBpb.Fat32.BS_VolID);\r | |
583 | printf (" 47 Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",\r | |
584 | FatBpb.Fat32.BS_VolLab[0],\r | |
585 | FatBpb.Fat32.BS_VolLab[1],\r | |
586 | FatBpb.Fat32.BS_VolLab[2],\r | |
587 | FatBpb.Fat32.BS_VolLab[3],\r | |
588 | FatBpb.Fat32.BS_VolLab[4],\r | |
589 | FatBpb.Fat32.BS_VolLab[5],\r | |
590 | FatBpb.Fat32.BS_VolLab[6],\r | |
591 | FatBpb.Fat32.BS_VolLab[7],\r | |
592 | FatBpb.Fat32.BS_VolLab[8],\r | |
593 | FatBpb.Fat32.BS_VolLab[9],\r | |
594 | FatBpb.Fat32.BS_VolLab[10]);\r | |
595 | printf (" 52 File system %c%c%c%c%c%c%c%c\n",\r | |
596 | FatBpb.Fat32.BS_FilSysType[0],\r | |
597 | FatBpb.Fat32.BS_FilSysType[1],\r | |
598 | FatBpb.Fat32.BS_FilSysType[2],\r | |
599 | FatBpb.Fat32.BS_FilSysType[3],\r | |
600 | FatBpb.Fat32.BS_FilSysType[4],\r | |
601 | FatBpb.Fat32.BS_FilSysType[5],\r | |
602 | FatBpb.Fat32.BS_FilSysType[6],\r | |
603 | FatBpb.Fat32.BS_FilSysType[7]);\r | |
604 | printf ("\n");\r | |
605 | }\r | |
606 | printf (" 1FE Signature %04x\n", FatBpb.Fat12_16.Signature);\r | |
607 | printf ("\n");\r | |
608 | \r | |
609 | \r | |
610 | return ;\r | |
611 | }\r | |
612 | \r | |
613 | void\r | |
614 | PatchBootSector (\r | |
615 | char *DestFileName,\r | |
616 | char *SourceFileName,\r | |
617 | BOOLEAN ForcePatch\r | |
618 | )\r | |
619 | /*++\r | |
620 | Routine Description:\r | |
621 | Patch destination file according to the information from source file.\r | |
622 | Only patch BPB data but leave boot code un-touched.\r | |
623 | \r | |
624 | Arguments:\r | |
625 | DestFileName - Destination file to patch\r | |
626 | SourceFileName - Source file where patch from\r | |
627 | --*/\r | |
628 | {\r | |
629 | FAT_BPB_STRUCT DestFatBpb;\r | |
630 | FAT_BPB_STRUCT SourceFatBpb;\r | |
631 | FAT_TYPE DestFatType;\r | |
632 | FAT_TYPE SourceFatType;\r | |
633 | CHAR8 VolLab[11];\r | |
634 | CHAR8 FilSysType[8];\r | |
635 | \r | |
636 | if (ReadFromFile ((void *)&DestFatBpb, DestFileName) == 0) {\r | |
637 | return ;\r | |
638 | }\r | |
639 | if (ReadFromFile ((void *)&SourceFatBpb, SourceFileName) == 0) {\r | |
640 | return ;\r | |
641 | }\r | |
642 | \r | |
643 | DestFatType = GetFatType (&DestFatBpb);\r | |
644 | SourceFatType = GetFatType (&SourceFatBpb);\r | |
645 | \r | |
646 | if (DestFatType != SourceFatType) {\r | |
647 | //\r | |
648 | // FAT type mismatch\r | |
649 | //\r | |
650 | if (ForcePatch) {\r | |
651 | DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s", \r | |
652 | FatTypeToString(SourceFatType), FatTypeToString(DestFatType));\r | |
653 | } else {\r | |
654 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s", \r | |
655 | FatTypeToString(SourceFatType), FatTypeToString(DestFatType));\r | |
656 | return ;\r | |
657 | }\r | |
658 | }\r | |
659 | \r | |
660 | if (SourceFatType <= FatTypeUnknown || SourceFatType >= FatTypeMax) {\r | |
661 | DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3002: Unknown FAT Type!\n");\r | |
662 | return;\r | |
663 | }\r | |
664 | \r | |
665 | //\r | |
666 | // Copy BPB/boot data (excluding BS_jmpBoot, BS_OEMName, BootCode and Signature) from SourceFatBpb to DestFatBpb\r | |
667 | //\r | |
668 | printf ("Patching %s BPB: ", FatTypeToString (SourceFatType));\r | |
669 | if (SourceFatType != FatTypeFat32) {\r | |
670 | memcpy (\r | |
671 | &DestFatBpb.Fat12_16.BPB_BytsPerSec,\r | |
672 | &SourceFatBpb.Fat12_16.BPB_BytsPerSec,\r | |
673 | ((UINTN)&DestFatBpb.Fat12_16.Reserved - (UINTN)&DestFatBpb.Fat12_16.BPB_BytsPerSec)\r | |
674 | );\r | |
675 | } else {\r | |
676 | memcpy (\r | |
677 | &DestFatBpb.Fat32.BPB_BytsPerSec,\r | |
678 | &SourceFatBpb.Fat32.BPB_BytsPerSec,\r | |
679 | ((UINTN)&DestFatBpb.Fat32.Reserved - (UINTN)&DestFatBpb.Fat32.BPB_BytsPerSec)\r | |
680 | );\r | |
681 | }\r | |
682 | \r | |
683 | //\r | |
684 | // Set BS_VolLab and BS_FilSysType of DestFatBpb\r | |
685 | //\r | |
686 | // BS_VolLab BS_FilSysType\r | |
687 | // FAT12: EFI FAT12 FAT12\r | |
688 | // FAT16: EFI FAT16 FAT16\r | |
689 | // FAT32: EFI FAT32 FAT32\r | |
690 | //\r | |
691 | if (SourceFatType == FatTypeFat32) {\r | |
692 | memcpy (VolLab, "EFI FAT32 ", sizeof(VolLab));\r | |
693 | memcpy (FilSysType, FAT32_FILSYSTYPE, sizeof(FilSysType));\r | |
694 | } else if (SourceFatType == FatTypeFat16) {\r | |
695 | memcpy (VolLab, "EFI FAT16 ", sizeof(VolLab));\r | |
696 | memcpy (FilSysType, FAT16_FILSYSTYPE, sizeof(FilSysType));\r | |
697 | } else {\r | |
698 | memcpy (VolLab, "EFI FAT12 ", sizeof(VolLab));\r | |
699 | memcpy (FilSysType, FAT12_FILSYSTYPE, sizeof(FilSysType));\r | |
700 | }\r | |
701 | if (SourceFatType != FatTypeFat32) {\r | |
702 | memcpy (DestFatBpb.Fat12_16.BS_VolLab, VolLab, sizeof(VolLab));\r | |
703 | memcpy (DestFatBpb.Fat12_16.BS_FilSysType, FilSysType, sizeof(FilSysType));\r | |
704 | } else {\r | |
705 | memcpy (DestFatBpb.Fat32.BS_VolLab, VolLab, sizeof(VolLab));\r | |
706 | memcpy (DestFatBpb.Fat32.BS_FilSysType, FilSysType, sizeof(FilSysType));\r | |
707 | }\r | |
708 | \r | |
709 | //\r | |
710 | // Set Signature of DestFatBpb to 55AA\r | |
711 | //\r | |
712 | DestFatBpb.Fat12_16.Signature = FAT_BS_SIGNATURE;\r | |
713 | \r | |
714 | //\r | |
715 | // Write DestFatBpb\r | |
716 | //\r | |
717 | if (WriteToFile ((void *)&DestFatBpb, DestFileName)) {\r | |
718 | printf ("successful!\n");\r | |
719 | } else {\r | |
720 | printf ("failed!\n");\r | |
721 | }\r | |
722 | \r | |
723 | return ;\r | |
724 | }\r | |
725 | \r | |
726 | void\r | |
727 | ParseMbr (\r | |
728 | char *FileName\r | |
729 | )\r | |
730 | {\r | |
731 | MASTER_BOOT_RECORD Mbr;\r | |
732 | \r | |
733 | if (ReadFromFile ((void *)&Mbr, FileName) == 0) {\r | |
734 | return ;\r | |
735 | }\r | |
736 | \r | |
737 | printf ("\nMaster Boot Record:\n");\r | |
738 | printf ("\n");\r | |
739 | printf (" Offset Title Value\n");\r | |
740 | printf ("==================================================================\n");\r | |
741 | printf (" 0 Master bootstrap loader code (not list)\n");\r | |
742 | printf (" 1B8 Windows disk signature %08x\n", (unsigned) Mbr.UniqueMbrSignature);\r | |
743 | printf ("\n");\r | |
744 | printf ("Partition Table Entry #1\n");\r | |
745 | printf (" 1BE 80 = active partition %02x\n", Mbr.PartitionRecord[0].BootIndicator);\r | |
746 | printf (" 1BF Start head %02x\n", Mbr.PartitionRecord[0].StartHead);\r | |
747 | printf (" 1C0 Start sector %02x\n", Mbr.PartitionRecord[0].StartSector);\r | |
748 | printf (" 1C1 Start cylinder %02x\n", Mbr.PartitionRecord[0].StartTrack);\r | |
749 | printf (" 1C2 Partition type indicator %02x\n", Mbr.PartitionRecord[0].OSType);\r | |
750 | printf (" 1C3 End head %02x\n", Mbr.PartitionRecord[0].EndHead);\r | |
751 | printf (" 1C4 End sector %02x\n", Mbr.PartitionRecord[0].EndSector);\r | |
752 | printf (" 1C5 End cylinder %02x\n", Mbr.PartitionRecord[0].EndTrack);\r | |
753 | printf (" 1C6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[0].StartingLBA);\r | |
754 | printf (" 1CA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[0].SizeInLBA);\r | |
755 | printf ("\n");\r | |
756 | printf ("Partition Table Entry #2\n");\r | |
757 | printf (" 1CE 80 = active partition %02x\n", Mbr.PartitionRecord[1].BootIndicator);\r | |
758 | printf (" 1CF Start head %02x\n", Mbr.PartitionRecord[1].StartHead);\r | |
759 | printf (" 1D0 Start sector %02x\n", Mbr.PartitionRecord[1].StartSector);\r | |
760 | printf (" 1D1 Start cylinder %02x\n", Mbr.PartitionRecord[1].StartTrack);\r | |
761 | printf (" 1D2 Partition type indicator %02x\n", Mbr.PartitionRecord[1].OSType);\r | |
762 | printf (" 1D3 End head %02x\n", Mbr.PartitionRecord[1].EndHead);\r | |
763 | printf (" 1D4 End sector %02x\n", Mbr.PartitionRecord[1].EndSector);\r | |
764 | printf (" 1D5 End cylinder %02x\n", Mbr.PartitionRecord[1].EndTrack);\r | |
765 | printf (" 1D6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[1].StartingLBA);\r | |
766 | printf (" 1DA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[1].SizeInLBA);\r | |
767 | printf ("\n");\r | |
768 | printf ("Partition Table Entry #3\n");\r | |
769 | printf (" 1DE 80 = active partition %02x\n", Mbr.PartitionRecord[2].BootIndicator);\r | |
770 | printf (" 1DF Start head %02x\n", Mbr.PartitionRecord[2].StartHead);\r | |
771 | printf (" 1E0 Start sector %02x\n", Mbr.PartitionRecord[2].StartSector);\r | |
772 | printf (" 1E1 Start cylinder %02x\n", Mbr.PartitionRecord[2].StartTrack);\r | |
773 | printf (" 1E2 Partition type indicator %02x\n", Mbr.PartitionRecord[2].OSType);\r | |
774 | printf (" 1E3 End head %02x\n", Mbr.PartitionRecord[2].EndHead);\r | |
775 | printf (" 1E4 End sector %02x\n", Mbr.PartitionRecord[2].EndSector);\r | |
776 | printf (" 1E5 End cylinder %02x\n", Mbr.PartitionRecord[2].EndTrack);\r | |
777 | printf (" 1E6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[2].StartingLBA);\r | |
778 | printf (" 1EA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[2].SizeInLBA);\r | |
779 | printf ("\n");\r | |
780 | printf ("Partition Table Entry #4\n");\r | |
781 | printf (" 1EE 80 = active partition %02x\n", Mbr.PartitionRecord[3].BootIndicator);\r | |
782 | printf (" 1EF Start head %02x\n", Mbr.PartitionRecord[3].StartHead);\r | |
783 | printf (" 1F0 Start sector %02x\n", Mbr.PartitionRecord[3].StartSector);\r | |
784 | printf (" 1F1 Start cylinder %02x\n", Mbr.PartitionRecord[3].StartTrack);\r | |
785 | printf (" 1F2 Partition type indicator %02x\n", Mbr.PartitionRecord[3].OSType);\r | |
786 | printf (" 1F3 End head %02x\n", Mbr.PartitionRecord[3].EndHead);\r | |
787 | printf (" 1F4 End sector %02x\n", Mbr.PartitionRecord[3].EndSector);\r | |
788 | printf (" 1F5 End cylinder %02x\n", Mbr.PartitionRecord[3].EndTrack);\r | |
789 | printf (" 1F6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[3].StartingLBA);\r | |
790 | printf (" 1FA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[3].SizeInLBA);\r | |
791 | printf ("\n");\r | |
792 | printf (" 1FE Signature %04x\n", Mbr.Signature);\r | |
793 | printf ("\n");\r | |
794 | \r | |
795 | return ;\r | |
796 | }\r | |
797 | \r | |
798 | void\r | |
799 | PatchMbr (\r | |
800 | char *DestFileName,\r | |
801 | char *SourceFileName\r | |
802 | )\r | |
803 | {\r | |
804 | MASTER_BOOT_RECORD DestMbr;\r | |
805 | MASTER_BOOT_RECORD SourceMbr;\r | |
806 | \r | |
807 | if (ReadFromFile ((void *)&DestMbr, DestFileName) == 0) {\r | |
808 | return ;\r | |
809 | }\r | |
810 | if (ReadFromFile ((void *)&SourceMbr, SourceFileName) == 0) {\r | |
811 | return ;\r | |
812 | }\r | |
813 | \r | |
814 | if (SourceMbr.Signature != MBR_SIGNATURE) {\r | |
815 | printf ("ERROR: E3000: Invalid MBR!\n");\r | |
816 | return;\r | |
817 | }\r | |
818 | \r | |
819 | printf ("Patching MBR:\n");\r | |
820 | memcpy (\r | |
821 | &DestMbr.PartitionRecord[0],\r | |
822 | &SourceMbr.PartitionRecord[0],\r | |
823 | sizeof(DestMbr.PartitionRecord)\r | |
824 | );\r | |
825 | \r | |
826 | DestMbr.Signature = MBR_SIGNATURE;\r | |
827 | \r | |
828 | \r | |
829 | if (WriteToFile ((void *)&DestMbr, DestFileName)) {\r | |
830 | printf ("\tsuccessful!\n");\r | |
831 | }\r | |
832 | \r | |
833 | return ;\r | |
834 | }\r | |
835 | \r | |
836 | \r | |
837 | int\r | |
838 | main (\r | |
839 | int argc,\r | |
840 | char *argv[]\r | |
841 | )\r | |
842 | {\r | |
843 | char *SrcImage;\r | |
844 | char *DstImage;\r | |
845 | BOOLEAN ForcePatch; // -f\r | |
846 | BOOLEAN ProcessMbr; // -m\r | |
847 | BOOLEAN DoParse; // -p SrcImage or -g SrcImage DstImage\r | |
848 | BOOLEAN Verbose; // -v\r | |
849 | UINT64 LogLevel;\r | |
850 | EFI_STATUS EfiStatus;\r | |
851 | \r | |
852 | SrcImage = DstImage = NULL;\r | |
853 | ForcePatch = FALSE;\r | |
854 | ProcessMbr = FALSE;\r | |
855 | DoParse = TRUE;\r | |
856 | Verbose = FALSE;\r | |
857 | \r | |
858 | SetUtilityName ("bootsectimage");\r | |
859 | \r | |
860 | argc--; argv++;\r | |
861 | \r | |
862 | if (argc == 0) {\r | |
863 | Usage ();\r | |
864 | return -1;\r | |
865 | }\r | |
866 | \r | |
867 | while (argc != 0) {\r | |
868 | if (strcmp (*argv, "-f") == 0 || strcmp (*argv, "--force") == 0) {\r | |
869 | ForcePatch = TRUE;\r | |
870 | } else if (strcmp (*argv, "-p") == 0 || strcmp (*argv, "--parse") == 0) {\r | |
871 | DoParse = TRUE;\r | |
872 | argc--; argv++;\r | |
873 | if (argc < 1) {\r | |
874 | Usage ();\r | |
875 | return -1;\r | |
876 | }\r | |
877 | SrcImage = *argv;\r | |
878 | } else if (strcmp (*argv, "-g") == 0 || strcmp (*argv, "--patch") == 0) {\r | |
879 | DoParse = FALSE;\r | |
880 | argc--; argv++;\r | |
881 | if (argc < 2) {\r | |
882 | Usage ();\r | |
883 | return -1;\r | |
884 | }\r | |
885 | SrcImage = *argv;\r | |
886 | argc--; argv++;\r | |
887 | DstImage = *argv;\r | |
888 | } else if (strcmp (*argv, "-m") == 0 || strcmp (*argv, "--mbr") == 0) {\r | |
889 | ProcessMbr = TRUE;\r | |
890 | } else if (strcmp (*argv, "-v") == 0 || strcmp (*argv, "--verbose") == 0) {\r | |
891 | Verbose = TRUE;\r | |
892 | } else if ((stricmp (*argv, "-d") == 0) || (stricmp (*argv, "--debug") == 0)) {\r | |
893 | argc--; argv++;\r | |
894 | if (argc < 1) {\r | |
895 | Usage ();\r | |
896 | return -1;\r | |
897 | }\r | |
898 | EfiStatus = AsciiStringToUint64 (*argv, FALSE, &LogLevel);\r | |
899 | if (EFI_ERROR (EfiStatus)) {\r | |
900 | Error (NULL, 0, 1003, "Invalid option value", "%s = %s", "--debug", *argv);\r | |
901 | return 1;\r | |
902 | }\r | |
903 | if (LogLevel > 9) {\r | |
904 | Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) LogLevel);\r | |
905 | return 1;\r | |
906 | }\r | |
907 | SetPrintLevel (LogLevel);\r | |
908 | DebugMsg (NULL, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", *argv);\r | |
909 | } else {\r | |
910 | Usage ();\r | |
911 | return -1;\r | |
912 | }\r | |
913 | \r | |
914 | argc--; argv++;\r | |
915 | }\r | |
916 | \r | |
917 | if (ForcePatch && DoParse) {\r | |
918 | printf ("ERROR: E1002: Conflicting options: -f, -p. Cannot apply force(-f) to parse(-p)!\n");\r | |
919 | Usage ();\r | |
920 | return -1;\r | |
921 | }\r | |
922 | if (ForcePatch && !DoParse && ProcessMbr) {\r | |
923 | printf ("ERROR: E1002: Conflicting options: -f, -g -m. Cannot apply force(-f) to processing MBR (-g -m)!\n");\r | |
924 | Usage ();\r | |
925 | return -1;\r | |
926 | }\r | |
927 | \r | |
928 | if (Verbose) {\r | |
929 | SetPrintLevel (VERBOSE_LOG_LEVEL);\r | |
930 | } else {\r | |
931 | SetPrintLevel (KEY_LOG_LEVEL);\r | |
932 | }\r | |
933 | \r | |
934 | if (DoParse) {\r | |
935 | if (ProcessMbr) {\r | |
936 | ParseMbr (SrcImage);\r | |
937 | } else {\r | |
938 | ParseBootSector (SrcImage);\r | |
939 | }\r | |
940 | } else {\r | |
941 | if (ProcessMbr) {\r | |
942 | PatchMbr (DstImage, SrcImage);\r | |
943 | } else {\r | |
944 | PatchBootSector (DstImage, SrcImage, ForcePatch);\r | |
945 | }\r | |
946 | }\r | |
947 | \r | |
948 | return 0;\r | |
949 | }\r | |
950 | \r |