/** @file
Abstract:
Patch the BPB information in boot sector image file.
Patch the MBR code in MBR image file.
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <stdio.h>
#include <string.h>
#include "fat.h"
#include "mbr.h"
#include "EfiUtilityMsgs.h"
#include "ParseInf.h"
//
// Utility Name
//
//
// Utility version information
//
#define UTILITY_MAJOR_VERSION 0
void
Version (
void
)
/*++
Routine Description:
Displays the standard utility information to SDTOUT
Arguments:
None
Returns:
None
--*/
{
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);
printf ("Copyright (c) 1999-2010 Intel Corporation. All rights reserved.\n");
}
void
Usage (
void
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
Returns:
GC_TODO: add return values
--*/
{
Version();
printf ("\nUsage: \n\
BootSectImage\n\
[-f, --force force patch even if the FAT type of SrcImage and DstImage mismatch]\n\
[-m, --mbr process MBR instead of boot sector]\n\
[-p, --parse parse SrcImageFile]\n\
[-o, --output DstImage]\n\
[-g, --patch patch DstImage using data from SrcImageFile]\n\
[-v, --verbose]\n\
[--version]\n\
[-q, --quiet disable all messages except fatal errors]\n\
[-d, --debug[#]\n\
[-h, --help]\n\
[SrcImageFile]\n");
}
int WriteToFile (
void *BootSector,
char *FileName
)
/*++
Routine Description:
Write 512 bytes boot sector to file.
Arguments:
BootSector - point to a buffer containing 512 bytes boot sector to write
FileName - file to write to
Return:
int - number of bytes wrote,
512 indicates write successful
0 indicates write failure
--*/
{
int result;
if (FileHandle == NULL) {
return 0;
}
if (result != 512) {
result = 0;
}
fclose (FileHandle);
return result;
}
int ReadFromFile (
void *BootSector,
char *FileName
)
/*++
Routine Description:
Read first 512 bytes from file.
Arguments:
BootSector - point to a buffer receiving the first 512 bytes data from file
FileName - file to read from
Return:
int - number of bytes read,
512 indicates read successful
0 indicates read failure
--*/
{
int result;
if (FileHandle == NULL) {
return 0;
}
if (result != 512) {
result = 0;
}
fclose (FileHandle);
return result;
}
char *
)
/*++
Routine Description:
Convert enum type of FatType to string
--*/
{
switch (FatType) {
case FatTypeFat12:
return "FAT12";
case FatTypeFat16:
return "FAT16";
case FatTypeFat32:
return "FAT32";
default:
break;
}
return "FAT Unknown";
}
)
/*++
Routine Description:
Determine the FAT type according to BIOS Paramater Block (BPB) data
Arguments:
FatBpb - BIOS Parameter Block (BPB) data, 512 Bytes
Return:
FatTypeUnknown - Cannot determine the FAT type
FatTypeFat12 - FAT12
FatTypeFat16 - FAT16
FatTypeFat32 - FAT32
--*/
{
//
// Simple check
//
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - Signature Invalid - %04x, expected: %04x",
return FatTypeUnknown;
}
//
// Check according to FAT spec
//
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BS_jmpBoot - %02x, expected: %02x or %02x",
return FatTypeUnknown;
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x, %04x, %04x, or %04x",
return FatTypeUnknown;
}
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_SecPerClus - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",
return FatTypeUnknown;
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec * BPB_SecPerClus - %08x, expected: <= %08x",
return FatTypeUnknown;
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_RsvdSecCnt - %04x, expected: Non-Zero Value",
return FatTypeUnknown;
}
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_Media - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",
return FatTypeUnknown;
}
//
// Algo in FAT spec
//
} else {
}
if (FATSz == 0) {
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_FATSz16, BPB_FATSz32 - 0, expected: Non-Zero Value");
return FatTypeUnknown;
}
} else {
}
if (TotSec == 0) {
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_TotSec16, BPB_TotSec32 - 0, expected: Non-Zero Value");
return FatTypeUnknown;
}
);
if (CountOfClusters < FAT_MAX_FAT12_CLUSTER) {
} else if (CountOfClusters < FAT_MAX_FAT16_CLUSTER) {
} else {
}
//
// Check according to FAT spec
//
DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT12_16 - BPB_RsvdSecCnt - %04x, expected: %04x",
}
if ((FatType == FatTypeFat32) &&
}
if ((FatType == FatTypeFat16) &&
printf ("WARNING: FAT16: BPB_RootEntCnt - %04x, expected - %04x\n",
}
if ((FatType == FatTypeFat32) &&
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_RootEntCnt - %04x, expected: %04x",
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_TotSec32 - %04x, expected: Non-Zero",
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_FATSz32 - %08x, expected: Non-Zero",
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
}
if ((FatType == FatTypeFat32) &&
}
if ((FatType == FatTypeFat32) &&
}
if ((FatType == FatTypeFat32) &&
}
if ((FatType == FatTypeFat32) &&
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",
return FatTypeUnknown;
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT12_16 - BS_Reserved1 - %02x, expected: 0\n",
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
return FatTypeUnknown;
}
DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT12_16 - BS_BootSig - %02x, expected: %02x\n",
return FatTypeUnknown;
}
if ((FatType == FatTypeFat32) &&
return FatTypeUnknown;
}
FilSysType[8] = 0;
if ((FatType == FatTypeFat12) &&
DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT12 - BS_FilSysType - %s, expected: %s, or %s\n",
}
if ((FatType == FatTypeFat16) &&
DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT16 - BS_FilSysType - %s, expected: %s, or %s\n",
}
}
if (FatType == FatTypeFat32) {
FilSysType[8] = 0;
}
}
//
// pass all check, get FAT type
//
return FatType;
}
void
char *FileName
)
{
return ;
}
printf ("ERROR: E3002: Unknown FAT Type!\n");
return;
}
printf ("\n");
printf (" Offset Title Data\n");
printf ("==================================================================\n");
printf (" 0 JMP instruction %02x %02x %02x\n",
printf (" 3 OEM %c%c%c%c%c%c%c%c\n",
printf ("\n");
printf ("BIOS Parameter Block\n");
printf ("\n");
if (FatType != FatTypeFat32) {
printf (" 2B Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",
printf (" 36 File system %c%c%c%c%c%c%c%c\n",
printf ("\n");
} else {
printf ("FAT32 Section\n");
printf (" 34 (Reserved) %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
printf ("\n");
printf (" 47 Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",
printf (" 52 File system %c%c%c%c%c%c%c%c\n",
printf ("\n");
}
printf ("\n");
return ;
}
void
char *DestFileName,
char *SourceFileName,
)
/*++
Routine Description:
Patch destination file according to the information from source file.
Only patch BPB data but leave boot code un-touched.
Arguments:
DestFileName - Destination file to patch
SourceFileName - Source file where patch from
--*/
{
return ;
}
return ;
}
if (DestFatType != SourceFatType) {
//
// FAT type mismatch
//
if (ForcePatch) {
} else {
return ;
}
}
return;
}
//
// Copy BPB/boot data (excluding BS_jmpBoot, BS_OEMName, BootCode and Signature) from SourceFatBpb to DestFatBpb
//
if (SourceFatType != FatTypeFat32) {
memcpy (
);
} else {
memcpy (
);
}
//
// Set BS_VolLab and BS_FilSysType of DestFatBpb
//
// BS_VolLab BS_FilSysType
// FAT12: EFI FAT12 FAT12
// FAT16: EFI FAT16 FAT16
// FAT32: EFI FAT32 FAT32
//
if (SourceFatType == FatTypeFat32) {
} else if (SourceFatType == FatTypeFat16) {
} else {
}
if (SourceFatType != FatTypeFat32) {
} else {
}
//
// Set Signature of DestFatBpb to 55AA
//
//
// Write DestFatBpb
//
printf ("successful!\n");
} else {
printf ("failed!\n");
}
return ;
}
void
ParseMbr (
char *FileName
)
{
return ;
}
printf ("\nMaster Boot Record:\n");
printf ("\n");
printf (" Offset Title Value\n");
printf ("==================================================================\n");
printf (" 0 Master bootstrap loader code (not list)\n");
printf ("\n");
printf ("Partition Table Entry #1\n");
printf ("\n");
printf ("Partition Table Entry #2\n");
printf ("\n");
printf ("Partition Table Entry #3\n");
printf ("\n");
printf ("Partition Table Entry #4\n");
printf ("\n");
printf ("\n");
return ;
}
void
PatchMbr (
char *DestFileName,
char *SourceFileName
)
{
return ;
}
return ;
}
printf ("ERROR: E3000: Invalid MBR!\n");
return;
}
printf ("Patching MBR:\n");
memcpy (
&DestMbr.PartitionRecord[0],
&SourceMbr.PartitionRecord[0],
sizeof(DestMbr.PartitionRecord)
);
printf ("\tsuccessful!\n");
}
return ;
}
int
main (
int argc,
char *argv[]
)
{
char *SrcImage;
char *DstImage;
ForcePatch = FALSE;
ProcessMbr = FALSE;
SetUtilityName ("bootsectimage");
if (argc == 0) {
Usage ();
return -1;
}
while (argc != 0) {
ForcePatch = TRUE;
if (argc < 1) {
Usage ();
return -1;
}
if (argc < 2) {
Usage ();
return -1;
}
ProcessMbr = TRUE;
if (argc < 1) {
Usage ();
return -1;
}
return 1;
}
if (LogLevel > 9) {
Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) LogLevel);
return 1;
}
} else {
Usage ();
return -1;
}
}
if (ForcePatch && DoParse) {
printf ("ERROR: E1002: Conflicting options: -f, -p. Cannot apply force(-f) to parse(-p)!\n");
Usage ();
return -1;
}
printf ("ERROR: E1002: Conflicting options: -f, -g -m. Cannot apply force(-f) to processing MBR (-g -m)!\n");
Usage ();
return -1;
}
if (Verbose) {
} else {
}
if (DoParse) {
if (ProcessMbr) {
} else {
}
} else {
if (ProcessMbr) {
} else {
}
}
return 0;
}