4 Patch the BPB information in boot sector image file.
5 Patch the MBR code in MBR image file.
7 Copyright (c) 2006 - 2014, 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 0
37 #define UTILITY_MINOR_VERSION 1
47 Displays the standard utility information to SDTOUT
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");
71 GC_TODO: Add function description
78 GC_TODO: add return values
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\
92 [-q, --quiet disable all messages except fatal errors]\n\
104 Write 512 bytes boot sector to file.
107 BootSector - point to a buffer containing 512 bytes boot sector to write
108 FileName - file to write to
111 int - number of bytes wrote,
112 512 indicates write successful
113 0 indicates write failure
119 FileHandle
= fopen (LongFilePath (FileName
), "r+b");
120 if (FileHandle
== NULL
) {
121 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "Open file: %s", FileName
);
124 fseek (FileHandle
, 0, SEEK_SET
);
126 result
= fwrite (BootSector
, 1, 512, FileHandle
);
128 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "Write file: %s", FileName
);
142 Read first 512 bytes from file.
145 BootSector - point to a buffer receiving the first 512 bytes data from file
146 FileName - file to read from
149 int - number of bytes read,
150 512 indicates read successful
151 0 indicates read failure
157 FileHandle
= fopen (LongFilePath (FileName
), "rb");
158 if (FileHandle
== NULL
) {
159 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E0001: Error opening file: %s", FileName
);
163 result
= fread (BootSector
, 1, 512, FileHandle
);
165 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E0004: Error reading file: %s", FileName
);
179 Convert enum type of FatType to string
192 return "FAT Unknown";
197 IN FAT_BPB_STRUCT
*FatBpb
201 Determine the FAT type according to BIOS Paramater Block (BPB) data
204 FatBpb - BIOS Parameter Block (BPB) data, 512 Bytes
207 FatTypeUnknown - Cannot determine the FAT type
214 UINTN RootDirSectors
;
218 UINTN CountOfClusters
;
221 FatType
= FatTypeUnknown
;
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
;
233 // Check according to FAT spec
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
;
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
;
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);
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
;
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
;
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
;
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);
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
;
297 RootDirSectors
= ((FatBpb
->Fat12_16
.BPB_RootEntCnt
* sizeof(FAT_DIRECTORY_ENTRY
)) +
298 (FatBpb
->Fat12_16
.BPB_BytsPerSec
- 1)) /
299 FatBpb
->Fat12_16
.BPB_BytsPerSec
;
301 if (FatBpb
->Fat12_16
.BPB_FATSz16
!= 0) {
302 FATSz
= FatBpb
->Fat12_16
.BPB_FATSz16
;
304 FATSz
= FatBpb
->Fat32
.BPB_FATSz32
;
307 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_FATSz16, BPB_FATSz32 - 0, expected: Non-Zero Value");
308 return FatTypeUnknown
;
311 if (FatBpb
->Fat12_16
.BPB_TotSec16
!= 0) {
312 TotSec
= FatBpb
->Fat12_16
.BPB_TotSec16
;
314 TotSec
= FatBpb
->Fat12_16
.BPB_TotSec32
;
317 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3003: FAT - BPB_TotSec16, BPB_TotSec32 - 0, expected: Non-Zero Value");
318 return FatTypeUnknown
;
322 FatBpb
->Fat12_16
.BPB_RsvdSecCnt
+
323 FatBpb
->Fat12_16
.BPB_NumFATs
* FATSz
+
327 CountOfClusters
= DataSec
/ FatBpb
->Fat12_16
.BPB_SecPerClus
;
329 if (CountOfClusters
< FAT_MAX_FAT12_CLUSTER
) {
330 FatType
= FatTypeFat12
;
331 } else if (CountOfClusters
< FAT_MAX_FAT16_CLUSTER
) {
332 FatType
= FatTypeFat16
;
334 FatType
= FatTypeFat32
;
337 // Check according to FAT spec
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);
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);
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);
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
;
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
;
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
;
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
;
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
;
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);
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);
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);
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);
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
;
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
;
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
;
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
;
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
;
448 if ((FatType
== FatTypeFat12
) || (FatType
== FatTypeFat16
)) {
449 memcpy (FilSysType
, FatBpb
->Fat12_16
.BS_FilSysType
, 8);
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
);
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
);
464 if (FatType
== FatTypeFat32
) {
465 memcpy (FilSysType
, FatBpb
->Fat32
.BS_FilSysType
, 8);
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
);
474 // pass all check, get FAT type
485 FAT_BPB_STRUCT FatBpb
;
488 if (ReadFromFile ((void *)&FatBpb
, FileName
) == 0) {
492 FatType
= GetFatType (&FatBpb
);
493 if (FatType
<= FatTypeUnknown
|| FatType
>= FatTypeMax
) {
494 printf ("ERROR: E3002: Unknown FAT Type!\n");
498 printf ("\nBoot Sector %s:\n", FatTypeToString (FatType
));
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]);
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
);
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]);
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]);
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]);
606 printf (" 1FE Signature %04x\n", FatBpb
.Fat12_16
.Signature
);
616 char *SourceFileName
,
621 Patch destination file according to the information from source file.
622 Only patch BPB data but leave boot code un-touched.
625 DestFileName - Destination file to patch
626 SourceFileName - Source file where patch from
629 FAT_BPB_STRUCT DestFatBpb
;
630 FAT_BPB_STRUCT SourceFatBpb
;
631 FAT_TYPE DestFatType
;
632 FAT_TYPE SourceFatType
;
636 if (ReadFromFile ((void *)&DestFatBpb
, DestFileName
) == 0) {
639 if (ReadFromFile ((void *)&SourceFatBpb
, SourceFileName
) == 0) {
643 DestFatType
= GetFatType (&DestFatBpb
);
644 SourceFatType
= GetFatType (&SourceFatBpb
);
646 if (DestFatType
!= SourceFatType
) {
651 DebugMsg (NULL
, 0, DEBUG_WARN
, NULL
, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s",
652 FatTypeToString(SourceFatType
), FatTypeToString(DestFatType
));
654 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s",
655 FatTypeToString(SourceFatType
), FatTypeToString(DestFatType
));
660 if (SourceFatType
<= FatTypeUnknown
|| SourceFatType
>= FatTypeMax
) {
661 DebugMsg (NULL
, 0, DEBUG_ERROR
, NULL
, "ERROR: E3002: Unknown FAT Type!\n");
666 // Copy BPB/boot data (excluding BS_jmpBoot, BS_OEMName, BootCode and Signature) from SourceFatBpb to DestFatBpb
668 printf ("Patching %s BPB: ", FatTypeToString (SourceFatType
));
669 if (SourceFatType
!= FatTypeFat32
) {
671 &DestFatBpb
.Fat12_16
.BPB_BytsPerSec
,
672 &SourceFatBpb
.Fat12_16
.BPB_BytsPerSec
,
673 ((UINTN
)&DestFatBpb
.Fat12_16
.Reserved
- (UINTN
)&DestFatBpb
.Fat12_16
.BPB_BytsPerSec
)
677 &DestFatBpb
.Fat32
.BPB_BytsPerSec
,
678 &SourceFatBpb
.Fat32
.BPB_BytsPerSec
,
679 ((UINTN
)&DestFatBpb
.Fat32
.Reserved
- (UINTN
)&DestFatBpb
.Fat32
.BPB_BytsPerSec
)
684 // Set BS_VolLab and BS_FilSysType of DestFatBpb
686 // BS_VolLab BS_FilSysType
687 // FAT12: EFI FAT12 FAT12
688 // FAT16: EFI FAT16 FAT16
689 // FAT32: EFI FAT32 FAT32
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
));
698 memcpy (VolLab
, "EFI FAT12 ", sizeof(VolLab
));
699 memcpy (FilSysType
, FAT12_FILSYSTYPE
, sizeof(FilSysType
));
701 if (SourceFatType
!= FatTypeFat32
) {
702 memcpy (DestFatBpb
.Fat12_16
.BS_VolLab
, VolLab
, sizeof(VolLab
));
703 memcpy (DestFatBpb
.Fat12_16
.BS_FilSysType
, FilSysType
, sizeof(FilSysType
));
705 memcpy (DestFatBpb
.Fat32
.BS_VolLab
, VolLab
, sizeof(VolLab
));
706 memcpy (DestFatBpb
.Fat32
.BS_FilSysType
, FilSysType
, sizeof(FilSysType
));
710 // Set Signature of DestFatBpb to 55AA
712 DestFatBpb
.Fat12_16
.Signature
= FAT_BS_SIGNATURE
;
717 if (WriteToFile ((void *)&DestFatBpb
, DestFileName
)) {
718 printf ("successful!\n");
720 printf ("failed!\n");
731 MASTER_BOOT_RECORD Mbr
;
733 if (ReadFromFile ((void *)&Mbr
, FileName
) == 0) {
737 printf ("\nMaster Boot Record:\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
);
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
);
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
);
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
);
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
);
792 printf (" 1FE Signature %04x\n", Mbr
.Signature
);
804 MASTER_BOOT_RECORD DestMbr
;
805 MASTER_BOOT_RECORD SourceMbr
;
807 if (ReadFromFile ((void *)&DestMbr
, DestFileName
) == 0) {
810 if (ReadFromFile ((void *)&SourceMbr
, SourceFileName
) == 0) {
814 if (SourceMbr
.Signature
!= MBR_SIGNATURE
) {
815 printf ("ERROR: E3000: Invalid MBR!\n");
819 printf ("Patching MBR:\n");
821 &DestMbr
.PartitionRecord
[0],
822 &SourceMbr
.PartitionRecord
[0],
823 sizeof(DestMbr
.PartitionRecord
)
826 DestMbr
.Signature
= MBR_SIGNATURE
;
829 if (WriteToFile ((void *)&DestMbr
, DestFileName
)) {
830 printf ("\tsuccessful!\n");
845 BOOLEAN ForcePatch
; // -f
846 BOOLEAN ProcessMbr
; // -m
847 BOOLEAN DoParse
; // -p SrcImage or -g SrcImage DstImage
848 BOOLEAN Verbose
; // -v
850 EFI_STATUS EfiStatus
;
852 SrcImage
= DstImage
= NULL
;
858 SetUtilityName ("bootsectimage");
868 if (strcmp (*argv
, "-f") == 0 || strcmp (*argv
, "--force") == 0) {
870 } else if (strcmp (*argv
, "-p") == 0 || strcmp (*argv
, "--parse") == 0) {
878 } else if (strcmp (*argv
, "-g") == 0 || strcmp (*argv
, "--patch") == 0) {
888 } else if (strcmp (*argv
, "-m") == 0 || strcmp (*argv
, "--mbr") == 0) {
890 } else if (strcmp (*argv
, "-v") == 0 || strcmp (*argv
, "--verbose") == 0) {
892 } else if ((stricmp (*argv
, "-d") == 0) || (stricmp (*argv
, "--debug") == 0)) {
898 EfiStatus
= AsciiStringToUint64 (*argv
, FALSE
, &LogLevel
);
899 if (EFI_ERROR (EfiStatus
)) {
900 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", "--debug", *argv
);
904 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) LogLevel
);
907 SetPrintLevel (LogLevel
);
908 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", *argv
);
917 if (ForcePatch
&& DoParse
) {
918 printf ("ERROR: E1002: Conflicting options: -f, -p. Cannot apply force(-f) to parse(-p)!\n");
922 if (ForcePatch
&& !DoParse
&& ProcessMbr
) {
923 printf ("ERROR: E1002: Conflicting options: -f, -g -m. Cannot apply force(-f) to processing MBR (-g -m)!\n");
929 SetPrintLevel (VERBOSE_LOG_LEVEL
);
931 SetPrintLevel (KEY_LOG_LEVEL
);
938 ParseBootSector (SrcImage
);
942 PatchMbr (DstImage
, SrcImage
);
944 PatchBootSector (DstImage
, SrcImage
, ForcePatch
);