]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
Japanese translation Fixed. (#2808)
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / PhyDrive.c
index 11e3d966f7d6f21235bfef01edec7b874a439176..d64916e5989771d09ec108151331ea28217b6c58 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
@@ -519,6 +544,199 @@ int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount)
     return 0;\r
 }\r
 \r
+BOOL VentoyPhydriveMatch(PHY_DRIVE_INFO* pPhyDrive)\r
+{\r
+    BOOL  bRet = FALSE;\r
+    DWORD dwBytes;\r
+    HANDLE Handle = INVALID_HANDLE_VALUE;\r
+    CHAR PhyDrive[128];\r
+    GET_LENGTH_INFORMATION LengthInfo;\r
+    STORAGE_PROPERTY_QUERY Query;\r
+    STORAGE_DESCRIPTOR_HEADER DevDescHeader;\r
+    STORAGE_DEVICE_DESCRIPTOR* pDevDesc = NULL;\r
+    STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR diskAlignment;\r
+    CHAR VendorId[128] = { 0 };\r
+    CHAR ProductId[128] = { 0 };\r
+    CHAR ProductRev[128] = { 0 };\r
+    CHAR SerialNumber[128] = { 0 };\r
+\r
+\r
+    safe_sprintf(PhyDrive, "\\\\.\\PhysicalDrive%d", pPhyDrive->PhyDrive);\r
+    Handle = CreateFileA(PhyDrive, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);\r
+    if (Handle == INVALID_HANDLE_VALUE)\r
+    {\r
+        Log("Create file Handle:%p %s status:%u", Handle, PhyDrive, LASTERR);\r
+        return FALSE;\r
+    }\r
+\r
+    bRet = DeviceIoControl(Handle,\r
+        IOCTL_DISK_GET_LENGTH_INFO, NULL,\r
+        0,\r
+        &LengthInfo,\r
+        sizeof(LengthInfo),\r
+        &dwBytes,\r
+        NULL);\r
+    if (!bRet)\r
+    {\r
+        Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR);\r
+        return FALSE;\r
+    }\r
+\r
+    if (pPhyDrive->SizeInBytes != (ULONGLONG)LengthInfo.Length.QuadPart)\r
+    {\r
+        Log("PHYSICALDRIVE%d size not match %llu %llu", pPhyDrive->PhyDrive, (ULONGLONG)LengthInfo.Length.QuadPart,\r
+            (ULONGLONG)pPhyDrive->SizeInBytes);\r
+        CHECK_CLOSE_HANDLE(Handle);\r
+        return FALSE;\r
+    }\r
+\r
+    Query.PropertyId = StorageDeviceProperty;\r
+    Query.QueryType = PropertyStandardQuery;\r
+\r
+    bRet = DeviceIoControl(Handle,\r
+        IOCTL_STORAGE_QUERY_PROPERTY,\r
+        &Query,\r
+        sizeof(Query),\r
+        &DevDescHeader,\r
+        sizeof(STORAGE_DESCRIPTOR_HEADER),\r
+        &dwBytes,\r
+        NULL);\r
+    if (!bRet)\r
+    {\r
+        Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR, dwBytes);\r
+        CHECK_CLOSE_HANDLE(Handle);\r
+        return FALSE;\r
+    }\r
+\r
+    if (DevDescHeader.Size < sizeof(STORAGE_DEVICE_DESCRIPTOR))\r
+    {\r
+        Log("Invalid DevDescHeader.Size:%u", DevDescHeader.Size);\r
+        CHECK_CLOSE_HANDLE(Handle);\r
+        return FALSE;\r
+    }\r
+\r
+    pDevDesc = (STORAGE_DEVICE_DESCRIPTOR*)malloc(DevDescHeader.Size);\r
+    if (!pDevDesc)\r
+    {\r
+        Log("failed to malloc error:%u len:%u", LASTERR, DevDescHeader.Size);\r
+        CHECK_CLOSE_HANDLE(Handle);\r
+        return FALSE;\r
+    }\r
+\r
+    bRet = DeviceIoControl(Handle,\r
+        IOCTL_STORAGE_QUERY_PROPERTY,\r
+        &Query,\r
+        sizeof(Query),\r
+        pDevDesc,\r
+        DevDescHeader.Size,\r
+        &dwBytes,\r
+        NULL);\r
+    if (!bRet)\r
+    {\r
+        Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR, dwBytes);\r
+        free(pDevDesc);\r
+        goto out;\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
+    if (pPhyDrive->DeviceType != pDevDesc->DeviceType ||\r
+        pPhyDrive->RemovableMedia != pDevDesc->RemovableMedia ||\r
+        pPhyDrive->BusType != pDevDesc->BusType ||\r
+        pPhyDrive->BytesPerLogicalSector != diskAlignment.BytesPerLogicalSector ||\r
+        pPhyDrive->BytesPerPhysicalSector != diskAlignment.BytesPerPhysicalSector\r
+        )\r
+    {\r
+        Log("Some properties not match DeviceType[%u %u] Removable[%u %u] BusType[%u %u] LogSec[%u %u] PhySec[%u %u]", \r
+            pPhyDrive->DeviceType, pDevDesc->DeviceType,\r
+            pPhyDrive->RemovableMedia, pDevDesc->RemovableMedia,\r
+            pPhyDrive->BusType, pDevDesc->BusType,\r
+            pPhyDrive->BytesPerLogicalSector, diskAlignment.BytesPerLogicalSector,\r
+            pPhyDrive->BytesPerPhysicalSector, diskAlignment.BytesPerPhysicalSector\r
+            );\r
+        goto out;\r
+    }\r
+\r
+    if (pDevDesc->VendorIdOffset)\r
+    {\r
+        safe_strcpy(VendorId, (char*)pDevDesc + pDevDesc->VendorIdOffset);\r
+        TrimString(VendorId);\r
+\r
+        if (strcmp(pPhyDrive->VendorId, VendorId))\r
+        {\r
+            Log("VendorId not match <%s %s>", pPhyDrive->VendorId, VendorId);\r
+            goto out;\r
+        }\r
+    }\r
+\r
+    if (pDevDesc->ProductIdOffset)\r
+    {\r
+        safe_strcpy(ProductId, (char*)pDevDesc + pDevDesc->ProductIdOffset);\r
+        TrimString(ProductId);\r
+\r
+        if (strcmp(pPhyDrive->ProductId, ProductId))\r
+        {\r
+            Log("ProductId not match <%s %s>", pPhyDrive->ProductId, ProductId);\r
+            goto out;\r
+        }\r
+    }\r
+\r
+    if (pDevDesc->ProductRevisionOffset)\r
+    {\r
+        safe_strcpy(ProductRev, (char*)pDevDesc + pDevDesc->ProductRevisionOffset);\r
+        TrimString(ProductRev);\r
+\r
+        if (strcmp(pPhyDrive->ProductRev, ProductRev))\r
+        {\r
+            Log("ProductRev not match <%s %s>", pPhyDrive->ProductRev, ProductRev);\r
+            goto out;\r
+        }\r
+    }\r
+\r
+    if (pDevDesc->SerialNumberOffset)\r
+    {\r
+        safe_strcpy(SerialNumber, (char*)pDevDesc + pDevDesc->SerialNumberOffset);\r
+        TrimString(SerialNumber);\r
+\r
+        if (strcmp(pPhyDrive->SerialNumber, SerialNumber))\r
+        {\r
+            Log("ProductRev not match <%s %s>", pPhyDrive->SerialNumber, SerialNumber);\r
+            goto out;\r
+        }\r
+    }\r
+\r
+    Log("PhyDrive%d ALL match, now continue", pPhyDrive->PhyDrive);\r
+\r
+    bRet = TRUE;\r
+\r
+out:\r
+    if (pDevDesc)\r
+    {\r
+        free(pDevDesc);\r
+    }\r
+\r
+    CHECK_CLOSE_HANDLE(Handle);\r
+\r
+    return bRet;\r
+}\r
 \r
 static HANDLE g_FatPhyDrive;\r
 static UINT64 g_Part2StartSec;\r
@@ -1587,7 +1805,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
@@ -1750,6 +1968,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
@@ -1774,7 +1994,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
@@ -1862,6 +2087,17 @@ 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
         Log("Zero part1 file system ...");\r
@@ -2008,6 +2244,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
@@ -2102,7 +2343,6 @@ int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive)
        int rc = 1;\r
        int PhyDrive;\r
        int PartStyle;\r
-       INT64 ReservedValue;\r
        UINT64 RecudeBytes;\r
        GUID Guid;\r
        MBR_HEAD MBR;\r
@@ -2138,13 +2378,6 @@ int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive)
        PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);\r
 \r
        RecudeBytes = VENTOY_EFI_PART_SIZE;\r
-       ReservedValue = GetReservedSpaceInMB();\r
-       if (ReservedValue > 0)\r
-       {\r
-               Log("Reduce add reserved space %lldMB", (LONGLONG)ReservedValue);\r
-               RecudeBytes += (UINT64)(ReservedValue * SIZE_1MB);\r
-       }\r
-\r
 \r
        if (pPhyDrive->ResizeNoShrink == FALSE)\r
        {\r
@@ -2328,30 +2561,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