Optimization for Ventoy2Disk.exe

This commit is contained in:
longpanda 2021-10-23 15:46:28 +08:00
parent df87cafdfb
commit d151e74336
3 changed files with 43 additions and 20 deletions

Binary file not shown.

View File

@ -887,7 +887,7 @@ STATIC BOOL VDS_CallBack_CleanDisk(void *pInterface, VDS_DISK_PROP *pDiskProp, U
BOOL VDS_CleanDisk(int DriveIndex)
{
BOOL ret = VDS_DiskCommProc(INTF_ADVANCEDDISK, DriveIndex, VDS_CallBack_CleanDisk, 0);
Log("VDS_CleanDisk %d ret:%d", DriveIndex, ret);
Log("VDS_CleanDisk %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
@ -959,14 +959,14 @@ STATIC BOOL VDS_CallBack_DeletePartition(void *pInterface, VDS_DISK_PROP *pDiskP
BOOL VDS_DeleteAllPartitions(int DriveIndex)
{
BOOL ret = VDS_DiskCommProc(INTF_ADVANCEDDISK, DriveIndex, VDS_CallBack_DeletePartition, 0);
Log("VDS_DeleteAllPartitions %d ret:%d", DriveIndex, ret);
Log("VDS_DeleteAllPartitions %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
BOOL VDS_DeleteVtoyEFIPartition(int DriveIndex)
{
BOOL ret = VDS_DiskCommProc(INTF_ADVANCEDDISK, DriveIndex, VDS_CallBack_DeletePartition, 2);
Log("VDS_DeleteVtoyEFIPartition %d ret:%d", DriveIndex, ret);
Log("VDS_DeleteVtoyEFIPartition %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
@ -1030,7 +1030,7 @@ STATIC BOOL VDS_CallBack_ChangeEFIAttr(void *pInterface, VDS_DISK_PROP *pDiskPro
BOOL VDS_ChangeVtoyEFIAttr(int DriveIndex, UINT64 Attr)
{
BOOL ret = VDS_DiskCommProc(INTF_ADVANCEDDISK, DriveIndex, VDS_CallBack_ChangeEFIAttr, Attr);
Log("VDS_ChangeVtoyEFIAttr %d ret:%d", DriveIndex, ret);
Log("VDS_ChangeVtoyEFIAttr %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
@ -1070,7 +1070,7 @@ BOOL VDS_ChangeVtoyEFI2ESP(int DriveIndex, UINT64 Offset)
Para.Offset = Offset;
BOOL ret = VDS_DiskCommProc(INTF_ADVANCEDDISK2, DriveIndex, VDS_CallBack_ChangeEFIType, (ULONG)&Para);
Log("VDS_ChangeVtoyEFI2ESP %d ret:%d", DriveIndex, ret);
Log("VDS_ChangeVtoyEFI2ESP %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
@ -1083,7 +1083,7 @@ BOOL VDS_ChangeVtoyEFI2Basic(int DriveIndex, UINT64 Offset)
Para.Offset = Offset;
BOOL ret = VDS_DiskCommProc(INTF_ADVANCEDDISK2, DriveIndex, VDS_CallBack_ChangeEFIType, (ULONG)&Para);
Log("VDS_ChangeVtoyEFI2ESP %d ret:%d", DriveIndex, ret);
Log("VDS_ChangeVtoyEFI2Basic %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
@ -1158,7 +1158,7 @@ BOOL VDS_CreateVtoyEFIPart(int DriveIndex, UINT64 Offset)
CoCreateGuid(&(Para.Id));
BOOL ret = VDS_DiskCommProc(INTF_CREATEPARTITIONEX, DriveIndex, VDS_CallBack_CreateVtoyEFI, (ULONG)&Para);
Log("VDS_CreateVtoyEFIPart %d ret:%d", DriveIndex, ret);
Log("VDS_CreateVtoyEFIPart %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}
@ -1218,6 +1218,7 @@ BOOL VDS_FormatVtoyEFIPart(int DriveIndex, UINT64 Offset)
memcpy(Para.Name, L"VTOYEFI", 7 * 2);
BOOL ret = VDS_DiskCommProc(INTF_PARTITIONMF, DriveIndex, VDS_CallBack_FormatVtoyEFI, (ULONG)&Para);
Log("VDS_FormatVtoyEFIPart %d ret:%d", DriveIndex, ret);
Log("VDS_FormatVtoyEFIPart %d ret:%d (%s)", DriveIndex, ret, ret ? "SUCCESS" : "FAIL");
return ret;
}

View File

@ -1837,9 +1837,11 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
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);
@ -1870,6 +1872,18 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
goto out;
}
pGPT = (VTOY_GPT_INFO *)backup;
offset = pGPT->Head.EfiBackupLBA * 512;
if (offset >= (DiskSize - SIZE_2MB) && offset < DiskSize)
{
Log("EFI partition table check success");
}
else
{
Log("Backup EFI LBA not in last 2MB range: %llu", pGPT->Head.EfiBackupLBA);
goto out;
}
//read last 2MB
liCurPosition.QuadPart = DiskSize - SIZE_2MB;
liNewPosition.QuadPart = 0;
@ -2111,6 +2125,13 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
{
bWriteBack = FALSE;
}
//write the first 2MB except parttable
if (!WriteBackupDataToDisk(hDrive, 34 * 512, pBackup + 34 * 512, SIZE_2MB - 34 * 512))
{
bWriteBack = FALSE;
}
Status = ERROR_NOT_FOUND;
}
else if (Esp2Basic)
@ -2288,12 +2309,7 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
if (CleanDisk)
{
if (!WriteBackupDataToDisk(hDrive, 4 * 512, pBackup + 4 * 512, SIZE_2MB - 4 * 512))
{
bWriteBack = FALSE;
}
if (!WriteBackupDataToDisk(hDrive, 0, pBackup, 4 * 512))
if (!WriteBackupDataToDisk(hDrive, 0, pBackup, 34 * 512))
{
bWriteBack = FALSE;
}
@ -2302,9 +2318,15 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
if (bWriteBack)
{
Log("Write success, now delete %s", BackBinFile);
Log("Write backup data success, now delete %s", BackBinFile);
DeleteFileA(BackBinFile);
}
else
{
Log("Write backup data failed");
}
Sleep(1000);
}
//Refresh Drive Layout