]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Ventoy2Disk/Ventoy2Disk/Utility.c
issue template
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / Utility.c
index ff77db5564313299802d925a59dc3a706fc72442..6b4416076ace18a21b78293f7aae38bba3d03168 100644 (file)
@@ -382,7 +382,7 @@ BOOL IsVentoyLogicalDrive(CHAR DriveLetter)
 }\r
 \r
 \r
-int VentoyFillLocation(UINT64 DiskSizeInBytes, UINT32 StartSectorId, UINT32 SectorCount, PART_TABLE *Table)\r
+int VentoyFillMBRLocation(UINT64 DiskSizeInBytes, UINT32 StartSectorId, UINT32 SectorCount, PART_TABLE *Table)\r
 {\r
     BYTE Head;\r
     BYTE Sector;\r
@@ -468,12 +468,23 @@ int VentoyFillMBR(UINT64 DiskSizeBytes, MBR_HEAD *pMBR, int PartStyle)
         ReservedSector += 33; // backup GPT part table\r
     }\r
 \r
+    // check aligned with 4KB\r
+    if (IsPartNeed4KBAlign())\r
+    {\r
+        UINT64 sectors = DiskSizeBytes / 512;\r
+        if (sectors % 8)\r
+        {\r
+            Log("Disk need to align with 4KB %u", (UINT32)(sectors % 8));\r
+            ReservedSector += (UINT32)(sectors % 8);\r
+        }\r
+    }\r
+\r
        Log("ReservedSector: %u", ReservedSector);\r
 \r
     //Part1\r
     PartStartSector = VENTOY_PART1_START_SECTOR;\r
        PartSectorCount = DiskSectorCount - ReservedSector - VENTOY_EFI_PART_SIZE / 512 - PartStartSector;\r
-    VentoyFillLocation(DiskSizeBytes, PartStartSector, PartSectorCount, pMBR->PartTbl);\r
+    VentoyFillMBRLocation(DiskSizeBytes, PartStartSector, PartSectorCount, pMBR->PartTbl);\r
 \r
     pMBR->PartTbl[0].Active = 0x80; // bootable\r
     pMBR->PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS\r
@@ -481,7 +492,7 @@ int VentoyFillMBR(UINT64 DiskSizeBytes, MBR_HEAD *pMBR, int PartStyle)
     //Part2\r
     PartStartSector += PartSectorCount;\r
     PartSectorCount = VENTOY_EFI_PART_SIZE / 512;\r
-    VentoyFillLocation(DiskSizeBytes, PartStartSector, PartSectorCount, pMBR->PartTbl + 1);\r
+    VentoyFillMBRLocation(DiskSizeBytes, PartStartSector, PartSectorCount, pMBR->PartTbl + 1);\r
 \r
     pMBR->PartTbl[1].Active = 0x00; \r
     pMBR->PartTbl[1].FsFlag = 0xEF; // EFI System Partition\r
@@ -538,10 +549,50 @@ static int VentoyFillProtectMBR(UINT64 DiskSizeBytes, MBR_HEAD *pMBR)
     return 0;\r
 }\r
 \r
+int VentoyFillWholeGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo)\r
+{\r
+    UINT64 Part1SectorCount = 0;\r
+    UINT64 DiskSectorCount = DiskSizeBytes / 512;\r
+    VTOY_GPT_HDR *Head = &pInfo->Head;\r
+    VTOY_GPT_PART_TBL *Table = pInfo->PartTbl;\r
+    static GUID WindowsDataPartType = { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };\r
+\r
+    VentoyFillProtectMBR(DiskSizeBytes, &pInfo->MBR);\r
+\r
+    Part1SectorCount = DiskSectorCount - 33 - 2048;\r
+\r
+    memcpy(Head->Signature, "EFI PART", 8);\r
+    Head->Version[2] = 0x01;\r
+    Head->Length = 92;\r
+    Head->Crc = 0;\r
+    Head->EfiStartLBA = 1;\r
+    Head->EfiBackupLBA = DiskSectorCount - 1;\r
+    Head->PartAreaStartLBA = 34;\r
+    Head->PartAreaEndLBA = DiskSectorCount - 34;\r
+    CoCreateGuid(&Head->DiskGuid);\r
+    Head->PartTblStartLBA = 2;\r
+    Head->PartTblTotNum = 128;\r
+    Head->PartTblEntryLen = 128;\r
+\r
+\r
+    memcpy(&(Table[0].PartType), &WindowsDataPartType, sizeof(GUID));\r
+    CoCreateGuid(&(Table[0].PartGuid));\r
+    Table[0].StartLBA = 2048;\r
+    Table[0].LastLBA = 2048 + Part1SectorCount - 1;\r
+    Table[0].Attr = 0;\r
+    memcpy(Table[0].Name, L"Data", 4 * 2);\r
+\r
+    //Update CRC\r
+    Head->PartTblCrc = VentoyCrc32(Table, sizeof(pInfo->PartTbl));\r
+    Head->Crc = VentoyCrc32(Head, Head->Length);\r
+\r
+    return 0;\r
+}\r
 \r
 int VentoyFillGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo)\r
 {\r
     INT64 ReservedValue = 0;\r
+    UINT64 ModSectorCount = 0;\r
     UINT64 ReservedSector = 33;\r
     UINT64 Part1SectorCount = 0;\r
     UINT64 DiskSectorCount = DiskSizeBytes / 512;\r
@@ -561,6 +612,26 @@ int VentoyFillGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo)
 \r
     Part1SectorCount = DiskSectorCount - ReservedSector - (VENTOY_EFI_PART_SIZE / 512) - 2048;\r
 \r
+    ModSectorCount = (Part1SectorCount % 8);\r
+    if (ModSectorCount)\r
+    {\r
+        Log("Part1SectorCount:%llu is not aligned by 4KB (%llu)", (ULONGLONG)Part1SectorCount, (ULONGLONG)ModSectorCount);\r
+    }\r
+\r
+    // check aligned with 4KB\r
+    if (IsPartNeed4KBAlign())\r
+    {\r
+        if (ModSectorCount)\r
+        {\r
+            Log("Disk need to align with 4KB %u", (UINT32)ModSectorCount);\r
+            Part1SectorCount -= ModSectorCount;\r
+        }\r
+        else\r
+        {\r
+            Log("no need to align with 4KB");\r
+        }\r
+    }\r
+\r
     memcpy(Head->Signature, "EFI PART", 8);\r
     Head->Version[2] = 0x01;\r
     Head->Length = 92;\r
@@ -588,7 +659,7 @@ int VentoyFillGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo)
     CoCreateGuid(&(Table[1].PartGuid));\r
     Table[1].StartLBA = Table[0].LastLBA + 1;\r
     Table[1].LastLBA = Table[1].StartLBA + VENTOY_EFI_PART_SIZE / 512 - 1;\r
-    Table[1].Attr = 0x8000000000000001ULL;\r
+    Table[1].Attr = 0xC000000000000001ULL;\r
     memcpy(Table[1].Name, L"VTOYEFI", 7 * 2);\r
 \r
 #if 0\r
@@ -707,6 +778,11 @@ int GetHumanReadableGBSize(UINT64 SizeBytes)
     double Delta;\r
     double GB = SizeBytes * 1.0 / 1000 / 1000 / 1000;\r
 \r
+    if ((SizeBytes % 1073741824) == 0)\r
+    {\r
+        return (int)(SizeBytes / 1073741824);\r
+    }\r
+\r
     for (i = 0; i < 12; i++)\r
     {\r
         if (Pow2 > GB)\r