Optimization for Ventoy2Disk.exe

This commit is contained in:
longpanda 2021-10-26 09:18:41 +08:00
parent e47e4484ce
commit 5fcb3971bc
3 changed files with 69 additions and 33 deletions

Binary file not shown.

View File

@ -1591,9 +1591,11 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
UINT64 Part1SectorCount = 0; UINT64 Part1SectorCount = 0;
UINT64 Part2StartSector = 0; UINT64 Part2StartSector = 0;
Log("#####################################################");
Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId, Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId, PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
GetHumanReadableGBSize(pPhyDrive->SizeInBytes)); GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
Log("#####################################################");
if (PartStyle) if (PartStyle)
{ {
@ -1828,29 +1830,14 @@ End:
return rc; return rc;
} }
static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBackup) static BOOL DiskCheckWriteAccess(HANDLE hDrive)
{ {
DWORD dwSize; DWORD dwSize;
DWORD dwStatus;
BOOL Return = FALSE;
BOOL ret = FALSE; BOOL ret = FALSE;
BYTE *backup = NULL; BOOL bRet = FALSE;
UINT64 offset;
BYTE Buffer[512]; BYTE Buffer[512];
HANDLE hDrive = INVALID_HANDLE_VALUE;
LARGE_INTEGER liCurPosition; LARGE_INTEGER liCurPosition;
LARGE_INTEGER liNewPosition; LARGE_INTEGER liNewPosition;
VTOY_GPT_INFO *pGPT = NULL;
Log("BackupDataBeforeCleanDisk %d", PhyDrive);
// step1: check write access
hDrive = GetPhysicalHandle(PhyDrive, TRUE, TRUE, FALSE);
if (hDrive == INVALID_HANDLE_VALUE)
{
Log("Failed to GetPhysicalHandle for write.");
goto out;
}
liCurPosition.QuadPart = 2039 * 512; liCurPosition.QuadPart = 2039 * 512;
liNewPosition.QuadPart = 0; liNewPosition.QuadPart = 0;
@ -1860,7 +1847,7 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
Log("SetFilePointer1 Failed %u", LASTERR); Log("SetFilePointer1 Failed %u", LASTERR);
goto out; goto out;
} }
dwSize = 0; dwSize = 0;
ret = ReadFile(hDrive, Buffer, 512, &dwSize, NULL); ret = ReadFile(hDrive, Buffer, 512, &dwSize, NULL);
@ -1880,6 +1867,7 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
goto out; goto out;
} }
dwSize = 0;
ret = WriteFile(hDrive, Buffer, 512, &dwSize, NULL); ret = WriteFile(hDrive, Buffer, 512, &dwSize, NULL);
if ((!ret) || dwSize != 512) if ((!ret) || dwSize != 512)
{ {
@ -1887,9 +1875,46 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
goto out; goto out;
} }
CHECK_CLOSE_HANDLE(hDrive); bRet = TRUE;
Log("Write access check success");
out:
return bRet;
}
static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBackup)
{
DWORD dwSize;
DWORD dwStatus;
BOOL Return = FALSE;
BOOL ret = FALSE;
BYTE *backup = NULL;
UINT64 offset;
HANDLE hDrive = INVALID_HANDLE_VALUE;
LARGE_INTEGER liCurPosition;
LARGE_INTEGER liNewPosition;
VTOY_GPT_INFO *pGPT = NULL;
Log("BackupDataBeforeCleanDisk %d", PhyDrive);
// step1: check write access
hDrive = GetPhysicalHandle(PhyDrive, TRUE, TRUE, FALSE);
if (hDrive == INVALID_HANDLE_VALUE)
{
Log("Failed to GetPhysicalHandle for write.");
goto out;
}
if (DiskCheckWriteAccess(hDrive))
{
Log("DiskCheckWriteAccess success");
CHECK_CLOSE_HANDLE(hDrive);
}
else
{
Log("DiskCheckWriteAccess failed");
goto out;
}
//step2 backup 4MB data //step2 backup 4MB data
backup = malloc(SIZE_1MB * 4); backup = malloc(SIZE_1MB * 4);
@ -2016,9 +2041,11 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
VTOY_GPT_INFO *pGptInfo = NULL; VTOY_GPT_INFO *pGptInfo = NULL;
UINT8 ReservedData[4096]; UINT8 ReservedData[4096];
Log("#####################################################");
Log("UpdateVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId, Log("UpdateVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId, pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
GetHumanReadableGBSize(pPhyDrive->SizeInBytes)); GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
Log("#####################################################");
PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN); PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
@ -2262,20 +2289,29 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
{ {
if (pPhyDrive->PartStyle == 0) if (pPhyDrive->PartStyle == 0)
{ {
ForceMBR = TRUE; if (DiskCheckWriteAccess(hDrive))
Log("Try write failed, now delete partition 2 for MBR...");
CHECK_CLOSE_HANDLE(hDrive);
Log("Now delete partition 2...");
VDS_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive);
hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
if (hDrive == INVALID_HANDLE_VALUE)
{ {
Log("Failed to GetPhysicalHandle for write."); Log("MBR DiskCheckWriteAccess success");
rc = 1;
goto End; ForceMBR = TRUE;
Log("Try write failed, now delete partition 2 for MBR...");
CHECK_CLOSE_HANDLE(hDrive);
Log("Now delete partition 2...");
VDS_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive);
hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
if (hDrive == INVALID_HANDLE_VALUE)
{
Log("Failed to GetPhysicalHandle for write.");
rc = 1;
goto End;
}
}
else
{
Log("MBR DiskCheckWriteAccess failed");
} }
} }
else else

Binary file not shown.