If we write MBR to disk, we just update the MBR code and the partition table wouldn't be over written.\r
If we process DBR, we will patch MBR to set first partition active if no active partition exists.\r
\r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
This program and the accompanying materials \r
are licensed and made available under the terms and conditions of the BSD License \r
which accompanies this distribution. The full text of the license may be found at \r
if (VolumeHandle == INVALID_HANDLE_VALUE) {\r
fprintf (\r
stderr, \r
- "error E0005: CreateFile failed: Volume = %s, LastError = 0x%x\n", \r
+ "error E0005: CreateFile failed: Volume = %s, LastError = 0x%lx\n", \r
VolumeAccessPath, \r
GetLastError ()\r
);\r
//\r
// Only care about the disk.\r
//\r
+ CloseHandle(VolumeHandle);\r
return FALSE;\r
} else{\r
DriveInfo->DiskNumber = StorageDeviceNumber.DeviceNumber;\r
BYTE DiskPartitionBackup[0x200] = {0};\r
DWORD BytesReturn;\r
INT DrvNumOffset;\r
- HANDLE InputHandle;\r
- HANDLE OutputHandle;\r
+ HANDLE InputHandle = INVALID_HANDLE_VALUE;\r
+ HANDLE OutputHandle = INVALID_HANDLE_VALUE;\r
ERROR_STATUS Status;\r
DWORD InputDbrOffset;\r
DWORD OutputDbrOffset;\r
//\r
Status = GetFileHandle(InputInfo, ProcessMbr, &InputHandle, &InputDbrOffset);\r
if (Status != ErrorSuccess) {\r
- return Status;\r
+ goto Done;\r
}\r
\r
//\r
//\r
Status = GetFileHandle(OutputInfo, ProcessMbr, &OutputHandle, &OutputDbrOffset);\r
if (Status != ErrorSuccess) {\r
- return Status;\r
+ goto Done;\r
}\r
\r
//\r
// Read boot sector from source disk/file\r
// \r
if (!ReadFile (InputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) {\r
- return ErrorFileReadWrite;\r
+ Status = ErrorFileReadWrite;\r
+ goto Done;\r
}\r
\r
if (InputInfo->Type == PathUsb) {\r
//\r
DrvNumOffset = GetDrvNumOffset (DiskPartition);\r
if (DrvNumOffset == -1) {\r
- return ErrorFatType;\r
+ Status = ErrorFatType;\r
+ goto Done;\r
}\r
//\r
// Some legacy BIOS require 0x80 discarding MBR.\r
// Use original partition table\r
//\r
if (!ReadFile (OutputHandle, DiskPartitionBackup, 0x200, &BytesReturn, NULL)) {\r
- return ErrorFileReadWrite;\r
+ Status = ErrorFileReadWrite;\r
+ goto Done;\r
}\r
memcpy (DiskPartition + 0x1BE, DiskPartitionBackup + 0x1BE, 0x40);\r
SetFilePointer (OutputHandle, 0, NULL, FILE_BEGIN);\r
// Write boot sector to taget disk/file\r
// \r
if (!WriteFile (OutputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) {\r
- return ErrorFileReadWrite;\r
+ Status = ErrorFileReadWrite;\r
+ goto Done;\r
}\r
\r
- CloseHandle (InputHandle);\r
- CloseHandle (OutputHandle);\r
+Done:\r
+ if (InputHandle != INVALID_HANDLE_VALUE) {\r
+ CloseHandle (InputHandle);\r
+ }\r
+ if (OutputHandle != INVALID_HANDLE_VALUE) {\r
+ CloseHandle (OutputHandle);\r
+ }\r
\r
- return ErrorSuccess;\r
+ return Status;\r
}\r
\r
void\r
}\r
\r
if (!GetDriveInfo(VolumeLetter, &DriveInfo)) {\r
- fprintf (stderr, "ERROR: GetDriveInfo - 0x%x\n", GetLastError ());\r
+ fprintf (stderr, "ERROR: GetDriveInfo - 0x%lx\n", GetLastError ());\r
return ErrorPath;\r
}\r
\r
return ErrorSuccess;\r
} \r
\r
+ //\r
+ // Check the path length\r
+ //\r
+ if (strlen (PathInfo->Path) >= (sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]))) {\r
+ fprintf (stderr, "ERROR, Path is too long for - %s", PathInfo->Path);\r
+ return ErrorPath;\r
+ }\r
+\r
PathInfo->Type = PathFile;\r
if (PathInfo->Input) {\r
//\r
if (f == NULL) {\r
fprintf (stderr, "error E2003: File was not provided!\n");\r
return ErrorPath;\r
- } \r
+ }\r
+ fclose (f);\r
}\r
PathInfo->Type = PathFile;\r
- strcpy(PathInfo->PhysicalPath, PathInfo->Path);\r
+ strncpy(\r
+ PathInfo->PhysicalPath,\r
+ PathInfo->Path,\r
+ sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]) - 1\r
+ );\r
+ PathInfo->PhysicalPath[sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]) - 1] = 0;\r
\r
return ErrorSuccess;\r
} \r
} else {\r
fprintf (\r
stderr, \r
- "%s: %s %s: failed - %s (LastError: 0x%x)!\n",\r
+ "%s: %s %s: failed - %s (LastError: 0x%lx)!\n",\r
(Status == ErrorNoMbr) ? "WARNING" : "ERROR",\r
(OutputPathInfo.Type != PathFile) ? "Write" : "Read", \r
ProcessMbr ? "MBR" : "DBR", \r