#include "ff.h"\r
#include "DiskService.h"\r
\r
+static int g_backup_bin_index = 0;\r
+\r
static DWORD GetVentoyVolumeName(int PhyDrive, UINT64 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)\r
{\r
size_t len;\r
\r
Log("BackupDataBeforeCleanDisk %d", PhyDrive);\r
\r
- backup = malloc(SIZE_1MB * 3);\r
+ backup = malloc(SIZE_1MB * 4);\r
if (!backup)\r
{\r
goto out;\r
goto out;\r
}\r
\r
- //read first 1MB\r
+ //read first 2MB\r
dwStatus = SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);\r
if (dwStatus != 0)\r
{\r
}\r
\r
dwSize = 0;\r
- ret = ReadFile(hDrive, backup, SIZE_1MB, &dwSize, NULL);\r
- if ((!ret) || (dwSize != SIZE_1MB))\r
+ ret = ReadFile(hDrive, backup, SIZE_2MB, &dwSize, NULL);\r
+ if ((!ret) || (dwSize != SIZE_2MB))\r
{\r
Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);\r
goto out;\r
}\r
\r
- liCurPosition.QuadPart = DiskSize - (SIZE_1MB * 2);\r
+ //read last 2MB\r
+ liCurPosition.QuadPart = DiskSize - SIZE_2MB;\r
liNewPosition.QuadPart = 0;\r
if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||\r
liNewPosition.QuadPart != liCurPosition.QuadPart)\r
}\r
\r
dwSize = 0;\r
- ret = ReadFile(hDrive, backup + SIZE_1MB, 2 * SIZE_1MB, &dwSize, NULL);\r
- if ((!ret) || (dwSize != 2 * SIZE_1MB))\r
+ ret = ReadFile(hDrive, backup + SIZE_2MB, SIZE_2MB, &dwSize, NULL);\r
+ if ((!ret) || (dwSize != SIZE_2MB))\r
{\r
Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);\r
goto out;\r
{\r
int i;\r
int rc = 0;\r
- int MaxRetry = 3;\r
+ int MaxRetry = 4;\r
BOOL ForceMBR = FALSE;\r
BOOL Esp2Basic = FALSE;\r
BOOL ChangeAttr = FALSE;\r
BOOL CleanDisk = FALSE;\r
+ BOOL bWriteBack = TRUE;\r
HANDLE hVolume;\r
HANDLE hDrive;\r
DWORD Status;\r
BOOL bRet;\r
CHAR DriveName[] = "?:\\";\r
CHAR DriveLetters[MAX_PATH] = { 0 };\r
+ CHAR BackBinFile[MAX_PATH];\r
UINT64 StartSector;\r
UINT64 ReservedMB = 0;\r
MBR_HEAD BootImg;\r
if (VDS_ChangeVtoyEFI2ESP(pPhyDrive->PhyDrive, StartSector * 512))\r
{\r
Esp2Basic = TRUE;\r
- Sleep(1000);\r
+ Sleep(3000);\r
}\r
}\r
else if (TryId == 2)\r
if (VDS_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, 0x8000000000000001))\r
{\r
ChangeAttr = TRUE;\r
- Sleep(1000);\r
+ Sleep(2000);\r
}\r
}\r
else if (TryId == 3)\r
Log("Clean disk GPT partition table");\r
if (BackupDataBeforeCleanDisk(pPhyDrive->PhyDrive, pPhyDrive->SizeInBytes, &pBackup))\r
{\r
+ sprintf_s(BackBinFile, sizeof(BackBinFile), ".\\ventoy\\phydrive%d_%u_%d.bin",\r
+ pPhyDrive->PhyDrive, GetCurrentProcessId(), g_backup_bin_index++);\r
+ SaveBufToFile(BackBinFile, pBackup, 4 * SIZE_1MB);\r
+ Log("Save backup data to %s", BackBinFile);\r
+\r
Log("Success to backup data before clean");\r
CleanDisk = TRUE;\r
- VDS_CleanDisk(pPhyDrive->PhyDrive);\r
- Sleep(1000);\r
+ if (!VDS_CleanDisk(pPhyDrive->PhyDrive))\r
+ {\r
+ Sleep(3000);\r
+ DSPT_CleanDisk(pPhyDrive->PhyDrive);\r
+ }\r
+ Sleep(3000);\r
}\r
else\r
{\r
//If we change VTOYEFI to ESP, it can not have s volume name, so don't try to get it.\r
if (CleanDisk)\r
{\r
- WriteBackupDataToDisk(hDrive, pPhyDrive->SizeInBytes - (2 * SIZE_1MB), pBackup + SIZE_1MB, 2 * SIZE_1MB);\r
+ //writeback the last 2MB\r
+ if (!WriteBackupDataToDisk(hDrive, pPhyDrive->SizeInBytes - SIZE_2MB, pBackup + SIZE_2MB, SIZE_2MB))\r
+ {\r
+ bWriteBack = FALSE;\r
+ }\r
Status = ERROR_NOT_FOUND;\r
}\r
else if (Esp2Basic)\r
goto End;\r
}\r
\r
- if (!TryWritePart2(hDrive, StartSector))\r
+ bRet = TryWritePart2(hDrive, StartSector);\r
+ if (FALSE == bRet && Esp2Basic)\r
+ {\r
+ Log("TryWritePart2 agagin ...");\r
+ Sleep(3000);\r
+ bRet = TryWritePart2(hDrive, StartSector);\r
+ }\r
+\r
+ if (!bRet)\r
{\r
if (pPhyDrive->PartStyle == 0)\r
{\r
\r
if (CleanDisk)\r
{\r
- WriteBackupDataToDisk(hDrive, 4 * 512, pBackup + 4 * 512, SIZE_1MB - 4 * 512);\r
- WriteBackupDataToDisk(hDrive, 0, pBackup, 4 * 512);\r
+ if (!WriteBackupDataToDisk(hDrive, 4 * 512, pBackup + 4 * 512, SIZE_2MB - 4 * 512))\r
+ {\r
+ bWriteBack = FALSE;\r
+ }\r
+\r
+ if (!WriteBackupDataToDisk(hDrive, 0, pBackup, 4 * 512))\r
+ {\r
+ bWriteBack = FALSE;\r
+ }\r
+\r
free(pBackup);\r
+\r
+ if (bWriteBack)\r
+ {\r
+ Log("Write success, now delete %s", BackBinFile);\r
+ DeleteFileA(BackBinFile);\r
+ }\r
}\r
\r
//Refresh Drive Layout\r
\r
if (pPhyDrive->PartStyle == 1)\r
{\r
- if (ChangeAttr || ((pPhyDrive->Part2GPTAttr >> 56) != 0xC0))\r
- {\r
- Log("Change EFI partition attr %u <0x%llx> to <0x%llx>", ChangeAttr, pPhyDrive->Part2GPTAttr, 0xC000000000000001ULL);\r
- if (VDS_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, 0xC000000000000001ULL))\r
- {\r
- Log("Change EFI partition attr success");\r
- pPhyDrive->Part2GPTAttr = 0xC000000000000001ULL;\r
- }\r
- else\r
+ if (ChangeAttr || ((pPhyDrive->Part2GPTAttr >> 56) != 0xC0))\r
{\r
- Log("Change EFI partition attr failed");\r
+ Log("Change EFI partition attr %u <0x%llx> to <0x%llx>", ChangeAttr, pPhyDrive->Part2GPTAttr, 0xC000000000000001ULL);\r
+ if (VDS_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, 0xC000000000000001ULL))\r
+ {\r
+ Log("Change EFI partition attr success");\r
+ pPhyDrive->Part2GPTAttr = 0xC000000000000001ULL;\r
+ }\r
+ else\r
+ {\r
+ Log("Change EFI partition attr failed");\r
+ }\r
}\r
}\r
- }\r
\r
if (pGptInfo)\r
{\r