X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FC%2FGenBootSector%2FGenBootSector.c;h=c02de49ba2ad6e3e67e59f68853e8655cf572e68;hp=4a9a0e1b57d472f3f7dfa7fbcb20766ab8549868;hb=1bdd9465c12e53246e88dc91cd22879ceb269f5c;hpb=40d841f6a8f84e75409178e19e69b95e01bada0f diff --git a/BaseTools/Source/C/GenBootSector/GenBootSector.c b/BaseTools/Source/C/GenBootSector/GenBootSector.c index 4a9a0e1b57..c02de49ba2 100644 --- a/BaseTools/Source/C/GenBootSector/GenBootSector.c +++ b/BaseTools/Source/C/GenBootSector/GenBootSector.c @@ -1,6 +1,10 @@ /** @file - -Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
+Reading/writing MBR/DBR. + NOTE: + If we write MBR to disk, we just update the MBR code and the partition table wouldn't be over written. + If we process DBR, we will patch MBR to set first partition active if no active partition exists. + +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
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 @@ -9,16 +13,6 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - genbootsector.c - -Abstract: - Reading/writing MBR/DBR. - NOTE: - If we write MBR to disk, we just update the MBR code and the partition table wouldn't be over written. - If we process DBR, we will patch MBR to set first partition active if no active partition exists. - **/ #include @@ -28,6 +22,7 @@ Abstract: #include "ParseInf.h" #include "EfiUtilityMsgs.h" +#include "CommonLib.h" // // Utility Name @@ -38,7 +33,7 @@ Abstract: // Utility version information // #define UTILITY_MAJOR_VERSION 0 -#define UTILITY_MINOR_VERSION 1 +#define UTILITY_MINOR_VERSION 2 #define MAX_DRIVE 26 #define PARTITION_TABLE_OFFSET 0x1BE @@ -172,7 +167,7 @@ Return: if (VolumeHandle == INVALID_HANDLE_VALUE) { fprintf ( stderr, - "error E0005: CreateFile failed: Volume = %s, LastError = 0x%x\n", + "error E0005: CreateFile failed: Volume = %s, LastError = 0x%lx\n", VolumeAccessPath, GetLastError () ); @@ -206,6 +201,7 @@ Return: // // Only care about the disk. // + CloseHandle(VolumeHandle); return FALSE; } else{ DriveInfo->DiskNumber = StorageDeviceNumber.DeviceNumber; @@ -283,7 +279,7 @@ GetBootSectorOffset ( Description: Get the offset of boot sector. For non-MBR disk, offset is just 0 - for disk with MBR, offset needs to be caculated by parsing MBR + for disk with MBR, offset needs to be calculated by parsing MBR NOTE: if no one is active, we will patch MBR to select first partition as active. @@ -442,8 +438,8 @@ ProcessBsOrMbr ( BYTE DiskPartitionBackup[0x200] = {0}; DWORD BytesReturn; INT DrvNumOffset; - HANDLE InputHandle; - HANDLE OutputHandle; + HANDLE InputHandle = INVALID_HANDLE_VALUE; + HANDLE OutputHandle = INVALID_HANDLE_VALUE; ERROR_STATUS Status; DWORD InputDbrOffset; DWORD OutputDbrOffset; @@ -453,7 +449,7 @@ ProcessBsOrMbr ( // Status = GetFileHandle(InputInfo, ProcessMbr, &InputHandle, &InputDbrOffset); if (Status != ErrorSuccess) { - return Status; + goto Done; } // @@ -461,14 +457,15 @@ ProcessBsOrMbr ( // Status = GetFileHandle(OutputInfo, ProcessMbr, &OutputHandle, &OutputDbrOffset); if (Status != ErrorSuccess) { - return Status; + goto Done; } // // Read boot sector from source disk/file // if (!ReadFile (InputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) { - return ErrorFileReadWrite; + Status = ErrorFileReadWrite; + goto Done; } if (InputInfo->Type == PathUsb) { @@ -478,7 +475,8 @@ ProcessBsOrMbr ( // DrvNumOffset = GetDrvNumOffset (DiskPartition); if (DrvNumOffset == -1) { - return ErrorFatType; + Status = ErrorFatType; + goto Done; } // // Some legacy BIOS require 0x80 discarding MBR. @@ -500,7 +498,8 @@ ProcessBsOrMbr ( // Use original partition table // if (!ReadFile (OutputHandle, DiskPartitionBackup, 0x200, &BytesReturn, NULL)) { - return ErrorFileReadWrite; + Status = ErrorFileReadWrite; + goto Done; } memcpy (DiskPartition + 0x1BE, DiskPartitionBackup + 0x1BE, 0x40); SetFilePointer (OutputHandle, 0, NULL, FILE_BEGIN); @@ -512,13 +511,19 @@ ProcessBsOrMbr ( // Write boot sector to taget disk/file // if (!WriteFile (OutputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) { - return ErrorFileReadWrite; + Status = ErrorFileReadWrite; + goto Done; } - CloseHandle (InputHandle); - CloseHandle (OutputHandle); +Done: + if (InputHandle != INVALID_HANDLE_VALUE) { + CloseHandle (InputHandle); + } + if (OutputHandle != INVALID_HANDLE_VALUE) { + CloseHandle (OutputHandle); + } - return ErrorSuccess; + return Status; } void @@ -541,8 +546,7 @@ Returns: --*/ { - printf ("%s v%d.%d -Utility to retrieve and update the boot sector or MBR.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION); - printf ("Copyright (c) 2009 - 2010 Intel Corporation. All rights reserved.\n"); + printf ("%s Version %d.%d %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION); } VOID @@ -550,18 +554,27 @@ PrintUsage ( void ) { - Version(); - printf ("\nUsage: \n\ - GenBootSector\n\ - [-l, --list list disks]\n\ - [-i, --input Filename]\n\ - [-o, --output Filename]\n\ - [-m, --mbr process the MBR also]\n\ - [-v, --verbose]\n\ - [--version]\n\ - [-q, --quiet disable all messages except fatal errors]\n\ - [-d, --debug[#]\n\ - [-h, --help]\n"); + printf ("Usage: GenBootSector [options] --cfg-file CFG_FILE\n\n\ +Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.\n\n\ + Utility to retrieve and update the boot sector or MBR.\n\n\ +optional arguments:\n\ + -h, --help Show this help message and exit\n\ + --version Show program's version number and exit\n\ + -d [DEBUG], --debug [DEBUG]\n\ + Output DEBUG statements, where DEBUG_LEVEL is 0 (min)\n\ + - 9 (max)\n\ + -v, --verbose Print informational statements\n\ + -q, --quiet Returns the exit code, error messages will be\n\ + displayed\n\ + -s, --silent Returns only the exit code; informational and error\n\ + messages are not displayed\n\ + -l, --list List disk drives\n\ + -i INPUT_FILENAME, --input INPUT_FILENAME\n\ + Input file name\n\ + -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\ + Output file name\n\ + -m, --mbr Also process the MBR\n\ + --sfo Reserved for future use\n"); } @@ -596,7 +609,7 @@ GetPathInfo ( } if (!GetDriveInfo(VolumeLetter, &DriveInfo)) { - fprintf (stderr, "ERROR: GetDriveInfo - 0x%x\n", GetLastError ()); + fprintf (stderr, "ERROR: GetDriveInfo - 0x%lx\n", GetLastError ()); return ErrorPath; } @@ -618,19 +631,33 @@ GetPathInfo ( return ErrorSuccess; } + // + // Check the path length + // + if (strlen (PathInfo->Path) >= (sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]))) { + fprintf (stderr, "ERROR, Path is too long for - %s", PathInfo->Path); + return ErrorPath; + } + PathInfo->Type = PathFile; if (PathInfo->Input) { // // If path is file path, check whether file is valid. // - f = fopen (PathInfo->Path, "r"); + f = fopen (LongFilePath (PathInfo->Path), "r"); if (f == NULL) { fprintf (stderr, "error E2003: File was not provided!\n"); return ErrorPath; - } + } + fclose (f); } PathInfo->Type = PathFile; - strcpy(PathInfo->PhysicalPath, PathInfo->Path); + strncpy( + PathInfo->PhysicalPath, + PathInfo->Path, + sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]) - 1 + ); + PathInfo->PhysicalPath[sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]) - 1] = 0; return ErrorSuccess; } @@ -784,7 +811,7 @@ main ( } else { fprintf ( stderr, - "%s: %s %s: failed - %s (LastError: 0x%x)!\n", + "%s: %s %s: failed - %s (LastError: 0x%lx)!\n", (Status == ErrorNoMbr) ? "WARNING" : "ERROR", (OutputPathInfo.Type != PathFile) ? "Write" : "Read", ProcessMbr ? "MBR" : "DBR",