4 Patch the BPB information in boot sector image file.
5 Patch the MBR code in MBR image file.
7 Copyright (c) 2006 - 2016, 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
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.
22 #include "EfiUtilityMsgs.h"
25 #define DEBUG_WARN 0x1
26 #define DEBUG_ERROR 0x2
31 #define UTILITY_NAME "BootSectImage"
34 // Utility version information
36 #define UTILITY_MAJOR_VERSION 1
37 #define UTILITY_MINOR_VERSION 0
47 Displays the standard utility information to SDTOUT
59 printf ("%s Version %d.%d Build %s\n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
70 GC_TODO: Add function description
77 GC_TODO: add return values
82 printf ("Copyright (c) 1999-2016 Intel Corporation. All rights reserved.\n");
83 printf ("\n The BootSectImage tool prints information or patch destination file by source\n");
84 printf (" file for BIOS Parameter Block (BPB) or Master Boot Record (MBR).\n");
87 [-f, --force force patch even if the FAT type of SrcImage and DstImage mismatch]\n\
88 [-m, --mbr process MBR instead of boot sector]\n\
89 [-p, --parse parse SrcImageFile]\n\
90 [-o, --output DstImage]\n\
91 [-g, --patch patch DstImage using data from SrcImageFile]\n\
94 [-q, --quiet disable all messages except fatal errors]\n\
106 Write 512 bytes boot sector to file.
109 BootSector - point to a buffer containing 512 bytes boot sector to write
110 FileName - file to write to
113 int - number of bytes wrote,
114 512 indicates write successful
115 0 indicates write failure
121 FileHandle
= fopen (LongFilePath (FileName
), "r+b");
122 if (FileHandle
== NULL
) {
123 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "Open file: %s", FileName
);
126 fseek (FileHandle
, 0, SEEK_SET
);
128 result
= fwrite (BootSector
, 1, 512, FileHandle
);
130 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "Write file: %s", FileName
);
144 Read first 512 bytes from file.
147 BootSector - point to a buffer receiving the first 512 bytes data from file
148 FileName - file to read from
151 int - number of bytes read,
152 512 indicates read successful
153 0 indicates read failure
159 FileHandle
= fopen (LongFilePath (FileName
), "rb");
160 if (FileHandle
== NULL
) {
161 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E0001: Error opening file: %s", FileName
);
165 result
= fread (BootSector
, 1, 512, FileHandle
);
167 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E0004: Error reading file: %s", FileName
);
181 Convert enum type of FatType to string
194 return "FAT Unknown";
199 IN FAT_BPB_STRUCT
*FatBpb
203 Determine the FAT type according to BIOS Paramater Block (BPB) data
206 FatBpb - BIOS Parameter Block (BPB) data, 512 Bytes
209 FatTypeUnknown - Cannot determine the FAT type
216 UINTN RootDirSectors
;
220 UINTN CountOfClusters
;
223 FatType
= FatTypeUnknown
;
228 if (FatBpb
->Fat12_16
.Signature
!= FAT_BS_SIGNATURE
) {
229 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - Signature Invalid - %04x, expected: %04x",
230 FatBpb
->Fat12_16
.Signature
, FAT_BS_SIGNATURE
);
231 return FatTypeUnknown
;
235 // Check according to FAT spec
237 if ((FatBpb
->Fat12_16
.BS_jmpBoot
[0] != FAT_BS_JMP1
) &&
238 (FatBpb
->Fat12_16
.BS_jmpBoot
[0] != FAT_BS_JMP2
)) {
239 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BS_jmpBoot - %02x, expected: %02x or %02x",
240 FatBpb
->Fat12_16
.BS_jmpBoot
[0], FAT_BS_JMP1
, FAT_BS_JMP2
);
241 return FatTypeUnknown
;
244 if ((FatBpb
->Fat12_16
.BPB_BytsPerSec
!= 512) &&
245 (FatBpb
->Fat12_16
.BPB_BytsPerSec
!= 1024) &&
246 (FatBpb
->Fat12_16
.BPB_BytsPerSec
!= 2048) &&
247 (FatBpb
->Fat12_16
.BPB_BytsPerSec
!= 4096)) {
248 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x, %04x, %04x, or %04x",
249 FatBpb
->Fat12_16
.BPB_BytsPerSec
, 512, 1024, 2048, 4096);
250 return FatTypeUnknown
;
252 if (FatBpb
->Fat12_16
.BPB_BytsPerSec
!= 512) {
253 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x",
254 FatBpb
->Fat12_16
.BPB_BytsPerSec
, 512);
256 if ((FatBpb
->Fat12_16
.BPB_SecPerClus
!= 1) &&
257 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 2) &&
258 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 4) &&
259 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 8) &&
260 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 16) &&
261 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 32) &&
262 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 64) &&
263 (FatBpb
->Fat12_16
.BPB_SecPerClus
!= 128)) {
264 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_SecPerClus - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",
265 FatBpb
->Fat12_16
.BPB_BytsPerSec
, 1, 2, 4, 8, 16, 32, 64, 128);
266 return FatTypeUnknown
;
268 if (FatBpb
->Fat12_16
.BPB_BytsPerSec
* FatBpb
->Fat12_16
.BPB_SecPerClus
> 32 * 1024) {
269 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_BytsPerSec * BPB_SecPerClus - %08x, expected: <= %08x",
270 FatBpb
->Fat12_16
.BPB_BytsPerSec
* FatBpb
->Fat12_16
.BPB_SecPerClus
, 32 * 1024);
271 return FatTypeUnknown
;
273 if (FatBpb
->Fat12_16
.BPB_RsvdSecCnt
== 0) {
274 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_RsvdSecCnt - %04x, expected: Non-Zero Value",
275 FatBpb
->Fat12_16
.BPB_RsvdSecCnt
);
276 return FatTypeUnknown
;
278 if (FatBpb
->Fat12_16
.BPB_NumFATs
!= 2) {
279 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT - BPB_NumFATs - %02x, expected: %02x",
280 FatBpb
->Fat12_16
.BPB_NumFATs
, 2);
282 if ((FatBpb
->Fat12_16
.BPB_Media
!= 0xF0) &&
283 (FatBpb
->Fat12_16
.BPB_Media
!= 0xF8) &&
284 (FatBpb
->Fat12_16
.BPB_Media
!= 0xF9) &&
285 (FatBpb
->Fat12_16
.BPB_Media
!= 0xFA) &&
286 (FatBpb
->Fat12_16
.BPB_Media
!= 0xFB) &&
287 (FatBpb
->Fat12_16
.BPB_Media
!= 0xFC) &&
288 (FatBpb
->Fat12_16
.BPB_Media
!= 0xFD) &&
289 (FatBpb
->Fat12_16
.BPB_Media
!= 0xFE) &&
290 (FatBpb
->Fat12_16
.BPB_Media
!= 0xFF)) {
291 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_Media - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",
292 FatBpb
->Fat12_16
.BPB_Media
, 0xF0, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF);
293 return FatTypeUnknown
;
299 RootDirSectors
= ((FatBpb
->Fat12_16
.BPB_RootEntCnt
* sizeof(FAT_DIRECTORY_ENTRY
)) +
300 (FatBpb
->Fat12_16
.BPB_BytsPerSec
- 1)) /
301 FatBpb
->Fat12_16
.BPB_BytsPerSec
;
303 if (FatBpb
->Fat12_16
.BPB_FATSz16
!= 0) {
304 FATSz
= FatBpb
->Fat12_16
.BPB_FATSz16
;
306 FATSz
= FatBpb
->Fat32
.BPB_FATSz32
;
309 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_FATSz16, BPB_FATSz32 - 0, expected: Non-Zero Value");
310 return FatTypeUnknown
;
313 if (FatBpb
->Fat12_16
.BPB_TotSec16
!= 0) {
314 TotSec
= FatBpb
->Fat12_16
.BPB_TotSec16
;
316 TotSec
= FatBpb
->Fat12_16
.BPB_TotSec32
;
319 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_TotSec16, BPB_TotSec32 - 0, expected: Non-Zero Value");
320 return FatTypeUnknown
;
324 FatBpb
->Fat12_16
.BPB_RsvdSecCnt
+
325 FatBpb
->Fat12_16
.BPB_NumFATs
* FATSz
+
329 CountOfClusters
= DataSec
/ FatBpb
->Fat12_16
.BPB_SecPerClus
;
331 if (CountOfClusters
< FAT_MAX_FAT12_CLUSTER
) {
332 FatType
= FatTypeFat12
;
333 } else if (CountOfClusters
< FAT_MAX_FAT16_CLUSTER
) {
334 FatType
= FatTypeFat16
;
336 FatType
= FatTypeFat32
;
339 // Check according to FAT spec
341 if (((FatType
== FatTypeFat12
) || (FatType
== FatTypeFat16
)) &&
342 (FatBpb
->Fat12_16
.BPB_RsvdSecCnt
!= 1)) {
343 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT12_16 - BPB_RsvdSecCnt - %04x, expected: %04x",
344 FatBpb
->Fat12_16
.BPB_RsvdSecCnt
, 1);
346 if ((FatType
== FatTypeFat32
) &&
347 (FatBpb
->Fat12_16
.BPB_RsvdSecCnt
!= 32)) {
348 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT32 - BPB_RsvdSecCnt - %04x, expected: %04x",
349 FatBpb
->Fat12_16
.BPB_RsvdSecCnt
, 32);
351 if ((FatType
== FatTypeFat16
) &&
352 (FatBpb
->Fat12_16
.BPB_RootEntCnt
!= 512)) {
353 printf ("WARNING: FAT16: BPB_RootEntCnt - %04x, expected - %04x\n",
354 FatBpb
->Fat12_16
.BPB_RootEntCnt
, 512);
356 if ((FatType
== FatTypeFat32
) &&
357 (FatBpb
->Fat12_16
.BPB_RootEntCnt
!= 0)) {
358 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BPB_RootEntCnt - %04x, expected: %04x",
359 FatBpb
->Fat12_16
.BPB_RootEntCnt
, 0);
360 return FatTypeUnknown
;
362 if ((FatType
== FatTypeFat32
) &&
363 (FatBpb
->Fat12_16
.BPB_TotSec16
!= 0)) {
364 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BPB_TotSec16 - %04x, expected: %04x",
365 FatBpb
->Fat12_16
.BPB_TotSec16
, 0);
366 return FatTypeUnknown
;
368 if ((FatType
== FatTypeFat32
) &&
369 (FatBpb
->Fat12_16
.BPB_FATSz16
!= 0)) {
370 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BPB_FATSz16 - %04x, expected: %04x",
371 FatBpb
->Fat12_16
.BPB_FATSz16
, 0);
372 return FatTypeUnknown
;
374 if ((FatType
== FatTypeFat32
) &&
375 (FatBpb
->Fat12_16
.BPB_TotSec32
== 0)) {
376 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BPB_TotSec32 - %04x, expected: Non-Zero",
377 (unsigned) FatBpb
->Fat12_16
.BPB_TotSec32
);
378 return FatTypeUnknown
;
380 if ((FatType
== FatTypeFat32
) &&
381 (FatBpb
->Fat32
.BPB_FATSz32
== 0)) {
382 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BPB_FATSz32 - %08x, expected: Non-Zero",
383 (unsigned) FatBpb
->Fat32
.BPB_FATSz32
);
384 return FatTypeUnknown
;
386 if ((FatType
== FatTypeFat32
) &&
387 (FatBpb
->Fat32
.BPB_FSVer
!= 0)) {
388 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT32 - BPB_FSVer - %08x, expected: %04x",
389 FatBpb
->Fat32
.BPB_FSVer
, 0);
391 if ((FatType
== FatTypeFat32
) &&
392 (FatBpb
->Fat32
.BPB_RootClus
!= 2)) {
393 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT32 - BPB_RootClus - %08x, expected: %04x",
394 (unsigned) FatBpb
->Fat32
.BPB_RootClus
, 2);
396 if ((FatType
== FatTypeFat32
) &&
397 (FatBpb
->Fat32
.BPB_FSInfo
!= 1)) {
398 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT32 - BPB_FSInfo - %08x, expected: %04x",
399 FatBpb
->Fat32
.BPB_FSInfo
, 1);
401 if ((FatType
== FatTypeFat32
) &&
402 (FatBpb
->Fat32
.BPB_BkBootSec
!= 6)) {
403 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT32 - BPB_BkBootSec - %08x, expected: %04x",
404 FatBpb
->Fat32
.BPB_BkBootSec
, 6);
406 if ((FatType
== FatTypeFat32
) &&
407 ((*(UINT32
*)FatBpb
->Fat32
.BPB_Reserved
!= 0) ||
408 (*((UINT32
*)FatBpb
->Fat32
.BPB_Reserved
+ 1) != 0) ||
409 (*((UINT32
*)FatBpb
->Fat32
.BPB_Reserved
+ 2) != 0))) {
410 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",
411 FatBpb
->Fat32
.BPB_Reserved
[0],
412 FatBpb
->Fat32
.BPB_Reserved
[1],
413 FatBpb
->Fat32
.BPB_Reserved
[2],
414 FatBpb
->Fat32
.BPB_Reserved
[3],
415 FatBpb
->Fat32
.BPB_Reserved
[4],
416 FatBpb
->Fat32
.BPB_Reserved
[5],
417 FatBpb
->Fat32
.BPB_Reserved
[6],
418 FatBpb
->Fat32
.BPB_Reserved
[7],
419 FatBpb
->Fat32
.BPB_Reserved
[8],
420 FatBpb
->Fat32
.BPB_Reserved
[9],
421 FatBpb
->Fat32
.BPB_Reserved
[10],
422 FatBpb
->Fat32
.BPB_Reserved
[11]);
423 return FatTypeUnknown
;
425 if (((FatType
== FatTypeFat12
) || (FatType
== FatTypeFat16
)) &&
426 (FatBpb
->Fat12_16
.BS_Reserved1
!= 0)) {
427 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT12_16 - BS_Reserved1 - %02x, expected: 0\n",
428 FatBpb
->Fat12_16
.BS_Reserved1
);
429 return FatTypeUnknown
;
431 if ((FatType
== FatTypeFat32
) &&
432 (FatBpb
->Fat32
.BS_Reserved1
!= 0)) {
433 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BS_Reserved1 - %02x, expected: 0\n",
434 FatBpb
->Fat32
.BS_Reserved1
);
435 return FatTypeUnknown
;
437 if (((FatType
== FatTypeFat12
) || (FatType
== FatTypeFat16
)) &&
438 (FatBpb
->Fat12_16
.BS_BootSig
!= FAT_BS_BOOTSIG
)) {
439 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT12_16 - BS_BootSig - %02x, expected: %02x\n",
440 FatBpb
->Fat12_16
.BS_BootSig
, FAT_BS_BOOTSIG
);
441 return FatTypeUnknown
;
443 if ((FatType
== FatTypeFat32
) &&
444 (FatBpb
->Fat32
.BS_BootSig
!= FAT_BS_BOOTSIG
)) {
445 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT32 - BS_BootSig - %02x, expected: %02x\n",
446 FatBpb
->Fat32
.BS_BootSig
, FAT_BS_BOOTSIG
);
447 return FatTypeUnknown
;
450 if ((FatType
== FatTypeFat12
) || (FatType
== FatTypeFat16
)) {
451 memcpy (FilSysType
, FatBpb
->Fat12_16
.BS_FilSysType
, 8);
453 if ((FatType
== FatTypeFat12
) &&
454 (strcmp (FilSysType
, FAT12_FILSYSTYPE
) != 0) &&
455 (strcmp (FilSysType
, FAT_FILSYSTYPE
) != 0)) {
456 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT12 - BS_FilSysType - %s, expected: %s, or %s\n",
457 FilSysType
, FAT12_FILSYSTYPE
, FAT_FILSYSTYPE
);
459 if ((FatType
== FatTypeFat16
) &&
460 (strcmp (FilSysType
, FAT16_FILSYSTYPE
) != 0) &&
461 (strcmp (FilSysType
, FAT_FILSYSTYPE
) != 0)) {
462 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT16 - BS_FilSysType - %s, expected: %s, or %s\n",
463 FilSysType
, FAT16_FILSYSTYPE
, FAT_FILSYSTYPE
);
466 if (FatType
== FatTypeFat32
) {
467 memcpy (FilSysType
, FatBpb
->Fat32
.BS_FilSysType
, 8);
469 if (strcmp (FilSysType
, FAT32_FILSYSTYPE
) != 0) {
470 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3003: FAT32 - BS_FilSysType - %s, expected: %s\n",
471 FilSysType
, FAT32_FILSYSTYPE
);
476 // pass all check, get FAT type
487 FAT_BPB_STRUCT FatBpb
;
490 if (ReadFromFile ((void *)&FatBpb
, FileName
) == 0) {
494 FatType
= GetFatType (&FatBpb
);
495 if (FatType
<= FatTypeUnknown
|| FatType
>= FatTypeMax
) {
496 printf ("ERROR: E3002: Unknown FAT Type!\n");
500 printf ("\nBoot Sector %s:\n", FatTypeToString (FatType
));
502 printf (" Offset Title Data\n");
503 printf ("==================================================================\n");
504 printf (" 0 JMP instruction %02x %02x %02x\n",
505 FatBpb
.Fat12_16
.BS_jmpBoot
[0],
506 FatBpb
.Fat12_16
.BS_jmpBoot
[1],
507 FatBpb
.Fat12_16
.BS_jmpBoot
[2]);
508 printf (" 3 OEM %c%c%c%c%c%c%c%c\n",
509 FatBpb
.Fat12_16
.BS_OEMName
[0],
510 FatBpb
.Fat12_16
.BS_OEMName
[1],
511 FatBpb
.Fat12_16
.BS_OEMName
[2],
512 FatBpb
.Fat12_16
.BS_OEMName
[3],
513 FatBpb
.Fat12_16
.BS_OEMName
[4],
514 FatBpb
.Fat12_16
.BS_OEMName
[5],
515 FatBpb
.Fat12_16
.BS_OEMName
[6],
516 FatBpb
.Fat12_16
.BS_OEMName
[7]);
518 printf ("BIOS Parameter Block\n");
519 printf (" B Bytes per sector %04x\n", FatBpb
.Fat12_16
.BPB_BytsPerSec
);
520 printf (" D Sectors per cluster %02x\n", FatBpb
.Fat12_16
.BPB_SecPerClus
);
521 printf (" E Reserved sectors %04x\n", FatBpb
.Fat12_16
.BPB_RsvdSecCnt
);
522 printf (" 10 Number of FATs %02x\n", FatBpb
.Fat12_16
.BPB_NumFATs
);
523 printf (" 11 Root entries %04x\n", FatBpb
.Fat12_16
.BPB_RootEntCnt
);
524 printf (" 13 Sectors (under 32MB) %04x\n", FatBpb
.Fat12_16
.BPB_TotSec16
);
525 printf (" 15 Media descriptor %02x\n", FatBpb
.Fat12_16
.BPB_Media
);
526 printf (" 16 Sectors per FAT (small vol.) %04x\n", FatBpb
.Fat12_16
.BPB_FATSz16
);
527 printf (" 18 Sectors per track %04x\n", FatBpb
.Fat12_16
.BPB_SecPerTrk
);
528 printf (" 1A Heads %04x\n", FatBpb
.Fat12_16
.BPB_NumHeads
);
529 printf (" 1C Hidden sectors %08x\n", (unsigned) FatBpb
.Fat12_16
.BPB_HiddSec
);
530 printf (" 20 Sectors (over 32MB) %08x\n", (unsigned) FatBpb
.Fat12_16
.BPB_TotSec32
);
532 if (FatType
!= FatTypeFat32
) {
533 printf (" 24 BIOS drive %02x\n", FatBpb
.Fat12_16
.BS_DrvNum
);
534 printf (" 25 (Unused) %02x\n", FatBpb
.Fat12_16
.BS_Reserved1
);
535 printf (" 26 Ext. boot signature %02x\n", FatBpb
.Fat12_16
.BS_BootSig
);
536 printf (" 27 Volume serial number %08x\n", (unsigned) FatBpb
.Fat12_16
.BS_VolID
);
537 printf (" 2B Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",
538 FatBpb
.Fat12_16
.BS_VolLab
[0],
539 FatBpb
.Fat12_16
.BS_VolLab
[1],
540 FatBpb
.Fat12_16
.BS_VolLab
[2],
541 FatBpb
.Fat12_16
.BS_VolLab
[3],
542 FatBpb
.Fat12_16
.BS_VolLab
[4],
543 FatBpb
.Fat12_16
.BS_VolLab
[5],
544 FatBpb
.Fat12_16
.BS_VolLab
[6],
545 FatBpb
.Fat12_16
.BS_VolLab
[7],
546 FatBpb
.Fat12_16
.BS_VolLab
[8],
547 FatBpb
.Fat12_16
.BS_VolLab
[9],
548 FatBpb
.Fat12_16
.BS_VolLab
[10]);
549 printf (" 36 File system %c%c%c%c%c%c%c%c\n",
550 FatBpb
.Fat12_16
.BS_FilSysType
[0],
551 FatBpb
.Fat12_16
.BS_FilSysType
[1],
552 FatBpb
.Fat12_16
.BS_FilSysType
[2],
553 FatBpb
.Fat12_16
.BS_FilSysType
[3],
554 FatBpb
.Fat12_16
.BS_FilSysType
[4],
555 FatBpb
.Fat12_16
.BS_FilSysType
[5],
556 FatBpb
.Fat12_16
.BS_FilSysType
[6],
557 FatBpb
.Fat12_16
.BS_FilSysType
[7]);
560 printf ("FAT32 Section\n");
561 printf (" 24 Sectors per FAT (large vol.) %08x\n", (unsigned) FatBpb
.Fat32
.BPB_FATSz32
);
562 printf (" 28 Flags %04x\n", FatBpb
.Fat32
.BPB_ExtFlags
);
563 printf (" 2A Version %04x\n", FatBpb
.Fat32
.BPB_FSVer
);
564 printf (" 2C Root dir 1st cluster %08x\n", (unsigned) FatBpb
.Fat32
.BPB_RootClus
);
565 printf (" 30 FSInfo sector %04x\n", FatBpb
.Fat32
.BPB_FSInfo
);
566 printf (" 32 Backup boot sector %04x\n", FatBpb
.Fat32
.BPB_BkBootSec
);
567 printf (" 34 (Reserved) %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
568 FatBpb
.Fat32
.BPB_Reserved
[0],
569 FatBpb
.Fat32
.BPB_Reserved
[1],
570 FatBpb
.Fat32
.BPB_Reserved
[2],
571 FatBpb
.Fat32
.BPB_Reserved
[3],
572 FatBpb
.Fat32
.BPB_Reserved
[4],
573 FatBpb
.Fat32
.BPB_Reserved
[5],
574 FatBpb
.Fat32
.BPB_Reserved
[6],
575 FatBpb
.Fat32
.BPB_Reserved
[7],
576 FatBpb
.Fat32
.BPB_Reserved
[8],
577 FatBpb
.Fat32
.BPB_Reserved
[9],
578 FatBpb
.Fat32
.BPB_Reserved
[10],
579 FatBpb
.Fat32
.BPB_Reserved
[11]);
581 printf (" 40 BIOS drive %02x\n", FatBpb
.Fat32
.BS_DrvNum
);
582 printf (" 41 (Unused) %02x\n", FatBpb
.Fat32
.BS_Reserved1
);
583 printf (" 42 Ext. boot signature %02x\n", FatBpb
.Fat32
.BS_BootSig
);
584 printf (" 43 Volume serial number %08x\n", (unsigned) FatBpb
.Fat32
.BS_VolID
);
585 printf (" 47 Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",
586 FatBpb
.Fat32
.BS_VolLab
[0],
587 FatBpb
.Fat32
.BS_VolLab
[1],
588 FatBpb
.Fat32
.BS_VolLab
[2],
589 FatBpb
.Fat32
.BS_VolLab
[3],
590 FatBpb
.Fat32
.BS_VolLab
[4],
591 FatBpb
.Fat32
.BS_VolLab
[5],
592 FatBpb
.Fat32
.BS_VolLab
[6],
593 FatBpb
.Fat32
.BS_VolLab
[7],
594 FatBpb
.Fat32
.BS_VolLab
[8],
595 FatBpb
.Fat32
.BS_VolLab
[9],
596 FatBpb
.Fat32
.BS_VolLab
[10]);
597 printf (" 52 File system %c%c%c%c%c%c%c%c\n",
598 FatBpb
.Fat32
.BS_FilSysType
[0],
599 FatBpb
.Fat32
.BS_FilSysType
[1],
600 FatBpb
.Fat32
.BS_FilSysType
[2],
601 FatBpb
.Fat32
.BS_FilSysType
[3],
602 FatBpb
.Fat32
.BS_FilSysType
[4],
603 FatBpb
.Fat32
.BS_FilSysType
[5],
604 FatBpb
.Fat32
.BS_FilSysType
[6],
605 FatBpb
.Fat32
.BS_FilSysType
[7]);
608 printf (" 1FE Signature %04x\n", FatBpb
.Fat12_16
.Signature
);
618 char *SourceFileName
,
623 Patch destination file according to the information from source file.
624 Only patch BPB data but leave boot code un-touched.
627 DestFileName - Destination file to patch
628 SourceFileName - Source file where patch from
631 FAT_BPB_STRUCT DestFatBpb
;
632 FAT_BPB_STRUCT SourceFatBpb
;
633 FAT_TYPE DestFatType
;
634 FAT_TYPE SourceFatType
;
638 if (ReadFromFile ((void *)&DestFatBpb
, DestFileName
) == 0) {
641 if (ReadFromFile ((void *)&SourceFatBpb
, SourceFileName
) == 0) {
645 DestFatType
= GetFatType (&DestFatBpb
);
646 SourceFatType
= GetFatType (&SourceFatBpb
);
648 if (DestFatType
!= SourceFatType
) {
653 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s",
654 FatTypeToString(SourceFatType
), FatTypeToString(DestFatType
));
656 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s",
657 FatTypeToString(SourceFatType
), FatTypeToString(DestFatType
));
662 if (SourceFatType
<= FatTypeUnknown
|| SourceFatType
>= FatTypeMax
) {
663 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3002: Unknown FAT Type!\n");
668 // Copy BPB/boot data (excluding BS_jmpBoot, BS_OEMName, BootCode and Signature) from SourceFatBpb to DestFatBpb
670 printf ("Patching %s BPB: ", FatTypeToString (SourceFatType
));
671 if (SourceFatType
!= FatTypeFat32
) {
673 &DestFatBpb
.Fat12_16
.BPB_BytsPerSec
,
674 &SourceFatBpb
.Fat12_16
.BPB_BytsPerSec
,
675 ((UINTN
)&DestFatBpb
.Fat12_16
.Reserved
- (UINTN
)&DestFatBpb
.Fat12_16
.BPB_BytsPerSec
)
679 &DestFatBpb
.Fat32
.BPB_BytsPerSec
,
680 &SourceFatBpb
.Fat32
.BPB_BytsPerSec
,
681 ((UINTN
)&DestFatBpb
.Fat32
.Reserved
- (UINTN
)&DestFatBpb
.Fat32
.BPB_BytsPerSec
)
686 // Set BS_VolLab and BS_FilSysType of DestFatBpb
688 // BS_VolLab BS_FilSysType
689 // FAT12: EFI FAT12 FAT12
690 // FAT16: EFI FAT16 FAT16
691 // FAT32: EFI FAT32 FAT32
693 if (SourceFatType
== FatTypeFat32
) {
694 memcpy (VolLab
, "EFI FAT32 ", sizeof(VolLab
));
695 memcpy (FilSysType
, FAT32_FILSYSTYPE
, sizeof(FilSysType
));
696 } else if (SourceFatType
== FatTypeFat16
) {
697 memcpy (VolLab
, "EFI FAT16 ", sizeof(VolLab
));
698 memcpy (FilSysType
, FAT16_FILSYSTYPE
, sizeof(FilSysType
));
700 memcpy (VolLab
, "EFI FAT12 ", sizeof(VolLab
));
701 memcpy (FilSysType
, FAT12_FILSYSTYPE
, sizeof(FilSysType
));
703 if (SourceFatType
!= FatTypeFat32
) {
704 memcpy (DestFatBpb
.Fat12_16
.BS_VolLab
, VolLab
, sizeof(VolLab
));
705 memcpy (DestFatBpb
.Fat12_16
.BS_FilSysType
, FilSysType
, sizeof(FilSysType
));
707 memcpy (DestFatBpb
.Fat32
.BS_VolLab
, VolLab
, sizeof(VolLab
));
708 memcpy (DestFatBpb
.Fat32
.BS_FilSysType
, FilSysType
, sizeof(FilSysType
));
712 // Set Signature of DestFatBpb to 55AA
714 DestFatBpb
.Fat12_16
.Signature
= FAT_BS_SIGNATURE
;
719 if (WriteToFile ((void *)&DestFatBpb
, DestFileName
)) {
720 printf ("successful!\n");
722 printf ("failed!\n");
733 MASTER_BOOT_RECORD Mbr
;
735 if (ReadFromFile ((void *)&Mbr
, FileName
) == 0) {
739 printf ("\nMaster Boot Record:\n");
741 printf (" Offset Title Value\n");
742 printf ("==================================================================\n");
743 printf (" 0 Master bootstrap loader code (not list)\n");
744 printf (" 1B8 Windows disk signature %08x\n", (unsigned) Mbr
.UniqueMbrSignature
);
746 printf ("Partition Table Entry #1\n");
747 printf (" 1BE 80 = active partition %02x\n", Mbr
.PartitionRecord
[0].BootIndicator
);
748 printf (" 1BF Start head %02x\n", Mbr
.PartitionRecord
[0].StartHead
);
749 printf (" 1C0 Start sector %02x\n", Mbr
.PartitionRecord
[0].StartSector
);
750 printf (" 1C1 Start cylinder %02x\n", Mbr
.PartitionRecord
[0].StartTrack
);
751 printf (" 1C2 Partition type indicator %02x\n", Mbr
.PartitionRecord
[0].OSType
);
752 printf (" 1C3 End head %02x\n", Mbr
.PartitionRecord
[0].EndHead
);
753 printf (" 1C4 End sector %02x\n", Mbr
.PartitionRecord
[0].EndSector
);
754 printf (" 1C5 End cylinder %02x\n", Mbr
.PartitionRecord
[0].EndTrack
);
755 printf (" 1C6 Sectors preceding partition %08x\n", (unsigned) Mbr
.PartitionRecord
[0].StartingLBA
);
756 printf (" 1CA Sectors in partition %08x\n", (unsigned) Mbr
.PartitionRecord
[0].SizeInLBA
);
758 printf ("Partition Table Entry #2\n");
759 printf (" 1CE 80 = active partition %02x\n", Mbr
.PartitionRecord
[1].BootIndicator
);
760 printf (" 1CF Start head %02x\n", Mbr
.PartitionRecord
[1].StartHead
);
761 printf (" 1D0 Start sector %02x\n", Mbr
.PartitionRecord
[1].StartSector
);
762 printf (" 1D1 Start cylinder %02x\n", Mbr
.PartitionRecord
[1].StartTrack
);
763 printf (" 1D2 Partition type indicator %02x\n", Mbr
.PartitionRecord
[1].OSType
);
764 printf (" 1D3 End head %02x\n", Mbr
.PartitionRecord
[1].EndHead
);
765 printf (" 1D4 End sector %02x\n", Mbr
.PartitionRecord
[1].EndSector
);
766 printf (" 1D5 End cylinder %02x\n", Mbr
.PartitionRecord
[1].EndTrack
);
767 printf (" 1D6 Sectors preceding partition %08x\n", (unsigned) Mbr
.PartitionRecord
[1].StartingLBA
);
768 printf (" 1DA Sectors in partition %08x\n", (unsigned) Mbr
.PartitionRecord
[1].SizeInLBA
);
770 printf ("Partition Table Entry #3\n");
771 printf (" 1DE 80 = active partition %02x\n", Mbr
.PartitionRecord
[2].BootIndicator
);
772 printf (" 1DF Start head %02x\n", Mbr
.PartitionRecord
[2].StartHead
);
773 printf (" 1E0 Start sector %02x\n", Mbr
.PartitionRecord
[2].StartSector
);
774 printf (" 1E1 Start cylinder %02x\n", Mbr
.PartitionRecord
[2].StartTrack
);
775 printf (" 1E2 Partition type indicator %02x\n", Mbr
.PartitionRecord
[2].OSType
);
776 printf (" 1E3 End head %02x\n", Mbr
.PartitionRecord
[2].EndHead
);
777 printf (" 1E4 End sector %02x\n", Mbr
.PartitionRecord
[2].EndSector
);
778 printf (" 1E5 End cylinder %02x\n", Mbr
.PartitionRecord
[2].EndTrack
);
779 printf (" 1E6 Sectors preceding partition %08x\n", (unsigned) Mbr
.PartitionRecord
[2].StartingLBA
);
780 printf (" 1EA Sectors in partition %08x\n", (unsigned) Mbr
.PartitionRecord
[2].SizeInLBA
);
782 printf ("Partition Table Entry #4\n");
783 printf (" 1EE 80 = active partition %02x\n", Mbr
.PartitionRecord
[3].BootIndicator
);
784 printf (" 1EF Start head %02x\n", Mbr
.PartitionRecord
[3].StartHead
);
785 printf (" 1F0 Start sector %02x\n", Mbr
.PartitionRecord
[3].StartSector
);
786 printf (" 1F1 Start cylinder %02x\n", Mbr
.PartitionRecord
[3].StartTrack
);
787 printf (" 1F2 Partition type indicator %02x\n", Mbr
.PartitionRecord
[3].OSType
);
788 printf (" 1F3 End head %02x\n", Mbr
.PartitionRecord
[3].EndHead
);
789 printf (" 1F4 End sector %02x\n", Mbr
.PartitionRecord
[3].EndSector
);
790 printf (" 1F5 End cylinder %02x\n", Mbr
.PartitionRecord
[3].EndTrack
);
791 printf (" 1F6 Sectors preceding partition %08x\n", (unsigned) Mbr
.PartitionRecord
[3].StartingLBA
);
792 printf (" 1FA Sectors in partition %08x\n", (unsigned) Mbr
.PartitionRecord
[3].SizeInLBA
);
794 printf (" 1FE Signature %04x\n", Mbr
.Signature
);
806 MASTER_BOOT_RECORD DestMbr
;
807 MASTER_BOOT_RECORD SourceMbr
;
809 if (ReadFromFile ((void *)&DestMbr
, DestFileName
) == 0) {
812 if (ReadFromFile ((void *)&SourceMbr
, SourceFileName
) == 0) {
816 if (SourceMbr
.Signature
!= MBR_SIGNATURE
) {
817 printf ("ERROR: E3000: Invalid MBR!\n");
821 printf ("Patching MBR:\n");
823 &DestMbr
.PartitionRecord
[0],
824 &SourceMbr
.PartitionRecord
[0],
825 sizeof(DestMbr
.PartitionRecord
)
828 DestMbr
.Signature
= MBR_SIGNATURE
;
831 if (WriteToFile ((void *)&DestMbr
, DestFileName
)) {
832 printf ("\tsuccessful!\n");
847 BOOLEAN ForcePatch
; // -f
848 BOOLEAN ProcessMbr
; // -m
849 BOOLEAN DoParse
; // -p SrcImage or -g SrcImage DstImage
850 BOOLEAN Verbose
; // -v
852 EFI_STATUS EfiStatus
;
854 SrcImage
= DstImage
= NULL
;
860 SetUtilityName ("bootsectimage");
870 if (strcmp (*argv
, "-f") == 0 || strcmp (*argv
, "--force") == 0) {
872 } else if (strcmp (*argv
, "-p") == 0 || strcmp (*argv
, "--parse") == 0) {
880 } else if (strcmp (*argv
, "-g") == 0 || strcmp (*argv
, "--patch") == 0) {
890 } else if (strcmp (*argv
, "-m") == 0 || strcmp (*argv
, "--mbr") == 0) {
892 } else if (strcmp (*argv
, "-v") == 0 || strcmp (*argv
, "--verbose") == 0) {
894 } else if (strcmp (*argv
, "--version") == 0) {
897 } else if ((stricmp (*argv
, "-d") == 0) || (stricmp (*argv
, "--debug") == 0)) {
903 EfiStatus
= AsciiStringToUint64 (*argv
, FALSE
, &LogLevel
);
904 if (EFI_ERROR (EfiStatus
)) {
905 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", "--debug", *argv
);
909 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) LogLevel
);
912 SetPrintLevel (LogLevel
);
913 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", *argv
);
922 if (ForcePatch
&& DoParse
) {
923 printf ("ERROR: E1002: Conflicting options: -f, -p. Cannot apply force(-f) to parse(-p)!\n");
927 if (ForcePatch
&& !DoParse
&& ProcessMbr
) {
928 printf ("ERROR: E1002: Conflicting options: -f, -g -m. Cannot apply force(-f) to processing MBR (-g -m)!\n");
934 SetPrintLevel (VERBOSE_LOG_LEVEL
);
936 SetPrintLevel (KEY_LOG_LEVEL
);
943 ParseBootSector (SrcImage
);
947 PatchMbr (DstImage
, SrcImage
);
949 PatchBootSector (DstImage
, SrcImage
, ForcePatch
);