]> glassweightruler.freedombox.rocks Git - Ventoy.git/commitdiff
Optimization for Ventoy2Disk.exe
authorlongpanda <admin@ventoy.net>
Fri, 22 Oct 2021 14:30:38 +0000 (22:30 +0800)
committerlongpanda <admin@ventoy.net>
Fri, 22 Oct 2021 14:30:38 +0000 (22:30 +0800)
INSTALL/Ventoy2Disk.exe
Ventoy2Disk/Ventoy2Disk/PhyDrive.c
Ventoy2Disk/Ventoy2Disk/WinDialog.c

index 9c439a33811ff2acc8bc26488fdc65ad3060bf4b..b18eed6b1da8c55964ca31f8a25aa2b16ee1d7df 100644 (file)
Binary files a/INSTALL/Ventoy2Disk.exe and b/INSTALL/Ventoy2Disk.exe differ
index 5aaa0dbdf872dde77d631bd1af03cefaf984e1b2..d36f51dd848598f8f2a4cedd144ae949df1817fb 100644 (file)
@@ -31,6 +31,8 @@
 #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
@@ -1841,7 +1843,7 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
 \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
@@ -1853,7 +1855,7 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
                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
@@ -1861,14 +1863,15 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
        }\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
@@ -1877,8 +1880,8 @@ static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBac
        }\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
@@ -1930,11 +1933,12 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
 {\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
@@ -1942,6 +1946,7 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
        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
@@ -2044,7 +2049,7 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
                        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
@@ -2053,7 +2058,7 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
                        if (VDS_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, 0x8000000000000001))\r
                        {\r
                                ChangeAttr = TRUE;\r
-                               Sleep(1000);\r
+                               Sleep(2000);\r
                        }\r
                }\r
                else if (TryId == 3)\r
@@ -2061,10 +2066,19 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
                        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
@@ -2092,7 +2106,11 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
        //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
@@ -2164,7 +2182,15 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
         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
@@ -2262,9 +2288,23 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
 \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
@@ -2291,20 +2331,20 @@ End:
 \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
index 0e5852c889efb0127202e28c4de63c2f56bcc3dc..564978d1ee510d1544140f3a8b755d8853977beb 100644 (file)
Binary files a/Ventoy2Disk/Ventoy2Disk/WinDialog.c and b/Ventoy2Disk/Ventoy2Disk/WinDialog.c differ