]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
1. Add new options for Windows CLI mode.
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / PhyDrive.c
index 256a9cf28706b63d03f4e2486f18237e0fea17af..42d74d065274c0ac0b170023447910f4185faf82 100644 (file)
@@ -139,7 +139,7 @@ static DWORD GetVentoyVolumeName(int PhyDrive, UINT64 StartSectorId, CHAR *NameB
     return Status;\r
 }\r
 \r
-static int GetLettersBelongPhyDrive(int PhyDrive, char *DriveLetters, size_t Length)\r
+int GetLettersBelongPhyDrive(int PhyDrive, char *DriveLetters, size_t Length)\r
 {\r
     int n = 0;\r
     DWORD DataSize = 0;\r
@@ -355,6 +355,7 @@ int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount)
     STORAGE_PROPERTY_QUERY Query;\r
     STORAGE_DESCRIPTOR_HEADER DevDescHeader;\r
     STORAGE_DEVICE_DESCRIPTOR *pDevDesc;\r
+    STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR diskAlignment;\r
     int PhyDriveId[VENTOY_MAX_PHY_DRIVE];\r
 \r
     Count = GetPhysicalDriveCount();\r
@@ -468,12 +469,35 @@ int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount)
             continue;\r
         }\r
 \r
+\r
+\r
+        memset(&Query, 0, sizeof(STORAGE_PROPERTY_QUERY));\r
+        Query.PropertyId = StorageAccessAlignmentProperty;\r
+        Query.QueryType = PropertyStandardQuery;\r
+        memset(&diskAlignment, 0, sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR));\r
+\r
+        bRet = DeviceIoControl(Handle,\r
+                               IOCTL_STORAGE_QUERY_PROPERTY,\r
+                               &Query,\r
+                               sizeof(STORAGE_PROPERTY_QUERY),\r
+                               &diskAlignment,\r
+                               sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR),\r
+                               &dwBytes,\r
+                               NULL);\r
+        if (!bRet)\r
+        {\r
+            Log("DeviceIoControl3 error:%u dwBytes:%u", LASTERR, dwBytes);            \r
+        }\r
+\r
         CurDrive->PhyDrive = i;\r
         CurDrive->SizeInBytes = LengthInfo.Length.QuadPart;\r
         CurDrive->DeviceType = pDevDesc->DeviceType;\r
         CurDrive->RemovableMedia = pDevDesc->RemovableMedia;\r
         CurDrive->BusType = pDevDesc->BusType;\r
 \r
+        CurDrive->BytesPerLogicalSector = diskAlignment.BytesPerLogicalSector;\r
+        CurDrive->BytesPerPhysicalSector = diskAlignment.BytesPerPhysicalSector;\r
+\r
         if (pDevDesc->VendorIdOffset)\r
         {\r
             safe_strcpy(CurDrive->VendorId, (char *)pDevDesc + pDevDesc->VendorIdOffset);\r
@@ -508,9 +532,10 @@ int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount)
 \r
     for (i = 0, CurDrive = pDriveList; i < (int)DriveCount; i++, CurDrive++)\r
     {\r
-        Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Name:%s %s",\r
+        Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Sector:%u/%u Name:%s %s",\r
             CurDrive->PhyDrive, GetBusTypeString(CurDrive->BusType), CurDrive->RemovableMedia,\r
             GetHumanReadableGBSize(CurDrive->SizeInBytes), CurDrive->SizeInBytes,\r
+            CurDrive->BytesPerLogicalSector, CurDrive->BytesPerPhysicalSector,\r
             CurDrive->VendorId, CurDrive->ProductId);\r
     }\r
 \r
@@ -1225,7 +1250,45 @@ static int FormatPart1exFAT(UINT64 DiskSizeBytes)
     }\r
 }\r
 \r
+static int ZeroPart1FileSystem(HANDLE hDrive, UINT64 Part2StartSector)\r
+{\r
+    int i;\r
+    DWORD dwSize = 0;\r
+    LARGE_INTEGER liCurPos;\r
+    LARGE_INTEGER liNewPos;\r
+    CHAR TmpBuffer[1024] = { 0 };\r
+\r
+    liCurPos.QuadPart = VENTOY_PART1_START_SECTOR * 512;\r
+    liNewPos.QuadPart = 0;\r
+    if (0 == SetFilePointerEx(hDrive, liCurPos, &liNewPos, FILE_BEGIN) ||\r
+        liNewPos.QuadPart != liCurPos.QuadPart)\r
+    {\r
+        Log("SetFilePointerEx Failed %u %llu %llu", LASTERR, (ULONGLONG)liCurPos.QuadPart, (ULONGLONG)liNewPos.QuadPart);\r
+        return 1;\r
+    }\r
+\r
+    for (i = 0; i < 1024; i++)\r
+    {\r
+        WriteFile(hDrive, TmpBuffer, 1024, &dwSize, NULL);\r
+    }\r
+\r
+    liCurPos.QuadPart = (Part2StartSector * 512) - (1024 * 1024);\r
+    liNewPos.QuadPart = 0;\r
+    if (0 == SetFilePointerEx(hDrive, liCurPos, &liNewPos, FILE_BEGIN) ||\r
+        liNewPos.QuadPart != liCurPos.QuadPart)\r
+    {\r
+        Log("SetFilePointerEx Failed %u %llu %llu", LASTERR, (ULONGLONG)liCurPos.QuadPart, (ULONGLONG)liNewPos.QuadPart);\r
+        return 1;\r
+    }\r
 \r
+    for (i = 0; i < 1024; i++)\r
+    {\r
+        WriteFile(hDrive, TmpBuffer, 1024, &dwSize, NULL);\r
+    }\r
+\r
+    Log("Zero Part1 SUCCESS");\r
+    return 0;\r
+}\r
 \r
 int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLetter)\r
 {\r
@@ -1549,7 +1612,7 @@ int InstallVentoy2FileImage(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
         memset(pData, 0, 512);\r
 \r
         pMBR = (MBR_HEAD *)pData;\r
-        VentoyFillMBR(pPhyDrive->SizeInBytes, pMBR, PartStyle);\r
+        VentoyFillMBR(pPhyDrive->SizeInBytes, pMBR, PartStyle, 0x07);\r
         Part1StartSector = pMBR->PartTbl[0].StartSectorId;\r
         Part1SectorCount = pMBR->PartTbl[0].SectorCount;\r
         Part2StartSector = pMBR->PartTbl[1].StartSectorId;\r
@@ -1712,6 +1775,8 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
     UINT64 Part1SectorCount = 0;\r
     UINT64 Part2StartSector = 0;\r
     BOOL LargeFAT32 = FALSE;\r
+    BOOL DefaultExFAT = FALSE;\r
+    UINT8 FsFlag = 0x07;\r
 \r
        Log("#####################################################");\r
     Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,\r
@@ -1736,7 +1801,12 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
     }\r
     else\r
     {\r
-        VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);\r
+        if (GetVentoyFsType() == VTOY_FS_FAT32)\r
+        {\r
+            FsFlag = 0x0C;\r
+        }\r
+\r
+        VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle, FsFlag);\r
         Part1StartSector = MBR.PartTbl[0].StartSectorId;\r
         Part1SectorCount = MBR.PartTbl[0].SectorCount;\r
         Part2StartSector = MBR.PartTbl[1].StartSectorId;\r
@@ -1824,15 +1894,25 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
             goto End;\r
         }\r
     }\r
+    else if (GetVentoyFsType() == VTOY_FS_EXFAT && GetClusterSize() == 0)\r
+    {\r
+        Log("Formatting part1 exFAT ...");\r
+        DefaultExFAT = TRUE;\r
+        if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))\r
+        {\r
+            Log("FormatPart1exFAT failed.");\r
+            rc = 1;\r
+            goto End;\r
+        }\r
+    }\r
     else\r
     {\r
-        CHAR TmpBuffer[512] = { 0 };\r
-\r
         Log("Zero part1 file system ...");\r
-        SetFilePointer(hDrive, VENTOY_PART1_START_SECTOR * 512, NULL, FILE_BEGIN);\r
-        for (i = 0; i < 32; i++)\r
+        if (0 != ZeroPart1FileSystem(hDrive, Part2StartSector))\r
         {\r
-            WriteFile(hDrive, TmpBuffer, 512, &dwSize, NULL);\r
+            Log("ZeroPart1FileSystem failed.");\r
+            rc = 1;\r
+            goto End;\r
         }\r
     }\r
 \r
@@ -1962,7 +2042,7 @@ End:
         Log("Close handle ...");\r
         CHECK_CLOSE_HANDLE(hDrive);\r
 \r
-        ReformatOK = FALSE;\r
+        ReformatOK = TRUE;\r
 \r
         if (state)\r
         {\r
@@ -1971,6 +2051,11 @@ End:
                 Log("No need to reformat for large FAT32");\r
                 pPhyDrive->VentoyFsClusterSize = GetVolumeClusterSize(MountDrive);\r
             }\r
+            else if (DefaultExFAT)\r
+            {\r
+                Log("No need to reformat for default exfat");\r
+                pPhyDrive->VentoyFsClusterSize = GetVolumeClusterSize(MountDrive);\r
+            }\r
             else\r
             {\r
                 bRet = DISK_FormatVolume(MountDrive, GetVentoyFsType(), Part1SectorCount * 512);\r
@@ -1983,7 +2068,6 @@ End:
 \r
                 if (bRet)\r
                 {\r
-                    ReformatOK = TRUE;\r
                     Log("Reformat %C:\\ to %s SUCCESS", MountDrive, GetVentoyFsName());\r
                     pPhyDrive->VentoyFsClusterSize = GetVolumeClusterSize(MountDrive);\r
 \r
@@ -1997,6 +2081,7 @@ End:
                 }\r
                 else\r
                 {\r
+                    ReformatOK = FALSE;\r
                     Log("Reformat %C:\\ to %s FAILED", MountDrive, GetVentoyFsName());\r
                 }\r
             }\r
@@ -2291,30 +2376,37 @@ int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive)
 \r
        Sleep(2000);\r
 \r
-       //Refresh disk list\r
-       PhyDrive = pPhyDrive->PhyDrive;\r
+    if (g_CLI_Mode)\r
+    {\r
+        Log("### Ventoy non-destructive CLI installation successfully finished.");\r
+    }\r
+    else\r
+    {\r
+        //Refresh disk list\r
+        PhyDrive = pPhyDrive->PhyDrive;\r
 \r
-       Log("#### Now Refresh PhyDrive ####");\r
-       Ventoy2DiskDestroy();\r
-       Ventoy2DiskInit();\r
-       \r
-       pPhyDrive = GetPhyDriveInfoByPhyDrive(PhyDrive);\r
-       if (pPhyDrive)\r
-       {\r
-               if (pPhyDrive->VentoyVersion[0] == 0)\r
-               {\r
-                       Log("After process the Ventoy version is still invalid");\r
-                       goto End;\r
-               }\r
+        Log("#### Now Refresh PhyDrive ####");\r
+        Ventoy2DiskDestroy();\r
+        Ventoy2DiskInit();\r
 \r
-               Log("### Ventoy non-destructive installation successfully finished <%s>", pPhyDrive->VentoyVersion);\r
-       }\r
-       else\r
-       {\r
-               Log("### Ventoy non-destructive installation successfully finished <not found>");\r
-       }\r
+        pPhyDrive = GetPhyDriveInfoByPhyDrive(PhyDrive);\r
+        if (pPhyDrive)\r
+        {\r
+            if (pPhyDrive->VentoyVersion[0] == 0)\r
+            {\r
+                Log("After process the Ventoy version is still invalid");\r
+                goto End;\r
+            }\r
+\r
+            Log("### Ventoy non-destructive installation successfully finished <%s>", pPhyDrive->VentoyVersion);\r
+        }\r
+        else\r
+        {\r
+            Log("### Ventoy non-destructive installation successfully finished <not found>");\r
+        }\r
 \r
-       InitComboxCtrl(g_DialogHwnd, PhyDrive);\r
+        InitComboxCtrl(g_DialogHwnd, PhyDrive);\r
+    }\r
 \r
        rc = 0;\r
 \r