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