]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
Update vietnamese language (#836)
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / PhyDrive.c
index 6eade725ad6ce64bca1412145ede7fe21aba9aef..768169330d5a9b357523ba698b3741c1f15d6017 100644 (file)
@@ -289,7 +289,7 @@ out:
 }\r
 \r
 \r
-static DWORD GetVentoyVolumeName(int PhyDrive, UINT32 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)\r
+static DWORD GetVentoyVolumeName(int PhyDrive, UINT64 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)\r
 {\r
     size_t len;\r
     BOOL bRet;\r
@@ -304,7 +304,7 @@ static DWORD GetVentoyVolumeName(int PhyDrive, UINT32 StartSectorId, CHAR *NameB
 \r
     PartOffset = 512ULL * StartSectorId;\r
 \r
-       Log("GetVentoyVolumeName PhyDrive %d SectorStart:%u PartOffset:%llu", PhyDrive, StartSectorId, (ULONGLONG)PartOffset);\r
+       Log("GetVentoyVolumeName PhyDrive %d SectorStart:%llu PartOffset:%llu", PhyDrive, (ULONGLONG)StartSectorId, (ULONGLONG)PartOffset);\r
 \r
     hVolume = FindFirstVolumeA(VolumeName, sizeof(VolumeName));\r
     if (hVolume == INVALID_HANDLE_VALUE)\r
@@ -798,10 +798,11 @@ static int VentoyFatDiskRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
 }\r
 \r
 \r
-int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen)\r
+int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen, BOOL *pSecureBoot)\r
 {\r
     int rc = 0;\r
     HANDLE hDrive;\r
+    void *flfile;\r
 \r
     hDrive = GetPhysicalHandle(pDriveInfo->PhyDrive, FALSE, FALSE, FALSE);\r
     if (hDrive == INVALID_HANDLE_VALUE)\r
@@ -831,6 +832,13 @@ int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSe
     if (rc == 0)\r
     {\r
         Log("VentoyVerInPhyDrive %d is <%s>...", pDriveInfo->PhyDrive, VerBuf);\r
+\r
+        flfile = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");\r
+        if (flfile)\r
+        {\r
+            *pSecureBoot = TRUE;\r
+            fl_fclose(flfile);\r
+        }\r
     }\r
 \r
     fl_shutdown();\r
@@ -962,6 +970,50 @@ int VentoyProcSecureBoot(BOOL SecureBoot)
                                free(filebuf);\r
                        }\r
                }\r
+\r
+        file = fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");\r
+        Log("Open ventoy efi file %p ", file);\r
+        if (file)\r
+        {\r
+            fl_fseek(file, 0, SEEK_END);\r
+            size = (int)fl_ftell(file);\r
+            fl_fseek(file, 0, SEEK_SET);\r
+\r
+            Log("ventoy efi file size %d ...", size);\r
+\r
+            filebuf = (char *)malloc(size);\r
+            if (filebuf)\r
+            {\r
+                fl_fread(filebuf, 1, size, file);\r
+            }\r
+\r
+            fl_fclose(file);\r
+\r
+            Log("Now delete all efi files ...");\r
+            fl_remove("/EFI/BOOT/BOOTIA32.EFI");\r
+            fl_remove("/EFI/BOOT/grubia32.efi");\r
+            fl_remove("/EFI/BOOT/grubia32_real.efi");\r
+            fl_remove("/EFI/BOOT/mmia32.efi");            \r
+\r
+            file = fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");\r
+            Log("Open bootia32 efi file %p ", file);\r
+            if (file)\r
+            {\r
+                if (filebuf)\r
+                {\r
+                    fl_fwrite(filebuf, 1, size, file);\r
+                }\r
+\r
+                fl_fflush(file);\r
+                fl_fclose(file);\r
+            }\r
+\r
+            if (filebuf)\r
+            {\r
+                free(filebuf);\r
+            }\r
+        }\r
+\r
        }\r
        else\r
        {\r
@@ -1349,18 +1401,18 @@ int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLett
         goto End;\r
     }\r
 \r
-    // clear first and last 1MB space\r
-    pTmpBuf = malloc(SIZE_1MB);\r
+    // clear first and last 2MB space\r
+    pTmpBuf = malloc(SIZE_2MB);\r
     if (!pTmpBuf)\r
     {\r
         Log("Failed to alloc memory.");\r
         rc = 1;\r
         goto End;\r
     }\r
-    memset(pTmpBuf, 0, SIZE_1MB);   \r
+    memset(pTmpBuf, 0, SIZE_2MB);\r
 \r
     SET_FILE_POS(512);\r
-    bRet = WriteFile(hDrive, pTmpBuf, SIZE_1MB - 512, &dwSize, NULL);\r
+    bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB - 512, &dwSize, NULL);\r
     Log("Write fisrt 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
     if (!bRet)\r
     {\r
@@ -1368,8 +1420,8 @@ int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLett
         goto End;\r
     }\r
 \r
-    SET_FILE_POS(SIZE_1MB);\r
-    bRet = WriteFile(hDrive, pTmpBuf, SIZE_1MB, &dwSize, NULL);\r
+    SET_FILE_POS(pPhyDrive->SizeInBytes - SIZE_2MB);\r
+    bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB, &dwSize, NULL);\r
     Log("Write 2nd 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
     if (!bRet)\r
     {\r
@@ -1378,30 +1430,71 @@ int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLett
     }\r
 \r
     SET_FILE_POS(0);\r
-    bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);\r
-    Log("Read MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
-    if (!bRet)\r
+\r
+    if (pPhyDrive->SizeInBytes > 2199023255552ULL)\r
     {\r
-        rc = 1;\r
-        goto End;\r
-    }\r
+        VTOY_GPT_INFO *pGptInfo;\r
+        VTOY_GPT_HDR BackupHead;\r
+        LARGE_INTEGER liCurrentPosition;\r
+\r
+        pGptInfo = (VTOY_GPT_INFO *)pTmpBuf;\r
 \r
-    //clear boot code and partition table (reserved disk signature)\r
-    memset(MBR.BootCode, 0, 440);\r
-    memset(MBR.PartTbl, 0, sizeof(MBR.PartTbl));\r
+        VentoyFillWholeGpt(pPhyDrive->SizeInBytes, pGptInfo);\r
 \r
-    VentoyFillLocation(pPhyDrive->SizeInBytes, 2048, (UINT32)(pPhyDrive->SizeInBytes / 512 - 2048), MBR.PartTbl);\r
+        SET_FILE_POS(pPhyDrive->SizeInBytes - 512);\r
+        VentoyFillBackupGptHead(pGptInfo, &BackupHead);\r
+        if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))\r
+        {\r
+            rc = 1;\r
+            Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());\r
+            goto End;\r
+        }\r
 \r
-    MBR.PartTbl[0].Active = 0x00; // bootable\r
-    MBR.PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS\r
+        SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);\r
+        if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))\r
+        {\r
+            rc = 1;\r
+            Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());\r
+            goto End;\r
+        }\r
 \r
-    SET_FILE_POS(0);\r
-    bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);\r
-    Log("Write MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
-    if (!bRet)\r
+        SET_FILE_POS(0);\r
+        if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))\r
+        {\r
+            rc = 1;\r
+            Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());\r
+            goto End;\r
+        }\r
+\r
+        Log("Write GPT Info OK ...");\r
+    }\r
+    else\r
     {\r
-        rc = 1;\r
-        goto End;\r
+        bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);\r
+        Log("Read MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
+        if (!bRet)\r
+        {\r
+            rc = 1;\r
+            goto End;\r
+        }\r
+\r
+        //clear boot code and partition table (reserved disk signature)\r
+        memset(MBR.BootCode, 0, 440);\r
+        memset(MBR.PartTbl, 0, sizeof(MBR.PartTbl));\r
+\r
+        VentoyFillMBRLocation(pPhyDrive->SizeInBytes, 2048, (UINT32)(pPhyDrive->SizeInBytes / 512 - 2048), MBR.PartTbl);\r
+\r
+        MBR.PartTbl[0].Active = 0x00; // bootable\r
+        MBR.PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS\r
+\r
+        SET_FILE_POS(0);\r
+        bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);\r
+        Log("Write MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);\r
+        if (!bRet)\r
+        {\r
+            rc = 1;\r
+            goto End;\r
+        }\r
     }\r
 \r
     Log("Clear Ventoy successfully finished");\r
@@ -1438,7 +1531,7 @@ End:
         if (state != 1)\r
         {\r
             Log("need to mount ventoy part1...");\r
-            if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[0].StartSectorId, DriveLetters, sizeof(DriveLetters), FALSE))\r
+            if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, 2048, DriveLetters, sizeof(DriveLetters), FALSE))\r
             {\r
                 DriveName[0] = MountDrive;\r
                 bRet = SetVolumeMountPointA(DriveName, DriveLetters);\r
@@ -1476,6 +1569,10 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
     CHAR DriveLetters[MAX_PATH] = { 0 };\r
     MBR_HEAD MBR;\r
     VTOY_GPT_INFO *pGptInfo = NULL;\r
+    UINT64 Part1StartSector = 0;\r
+    UINT64 Part1SectorCount = 0;\r
+    UINT64 Part2StartSector = 0;\r
+\r
 \r
     Log("InstallVentoy2PhyDrive %s PhyDrive%d <<%s %s %dGB>>",\r
         PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,\r
@@ -1489,10 +1586,19 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
 \r
     PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);\r
 \r
-    VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);//also used to format 1st partition in GPT mode\r
     if (PartStyle)\r
     {\r
         VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);\r
+        Part1StartSector = pGptInfo->PartTbl[0].StartLBA;\r
+        Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;\r
+        Part2StartSector = pGptInfo->PartTbl[1].StartLBA;\r
+    }\r
+    else\r
+    {\r
+        VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);\r
+        Part1StartSector = MBR.PartTbl[0].StartSectorId;\r
+        Part1SectorCount = MBR.PartTbl[0].SectorCount;\r
+        Part2StartSector = MBR.PartTbl[1].StartSectorId;\r
     }\r
 \r
     Log("Lock disk for clean ............................. ");\r
@@ -1556,7 +1662,7 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
     //Refresh Drive Layout\r
     DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);\r
 \r
-    disk_io_set_param(hDrive, MBR.PartTbl[0].StartSectorId + MBR.PartTbl[0].SectorCount);\r
+    disk_io_set_param(hDrive, Part1StartSector + Part1SectorCount);// include the 2048 sector gap\r
 \r
     PROGRESS_BAR_SET_POS(PT_FORMAT_PART1);\r
 \r
@@ -1574,9 +1680,12 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
         goto End;\r
     }\r
 \r
+    \r
+\r
     PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);\r
     Log("Writing part2 FAT img ...");\r
-    if (0 != FormatPart2Fat(hDrive, MBR.PartTbl[1].StartSectorId))\r
+    \r
+    if (0 != FormatPart2Fat(hDrive, Part2StartSector))\r
     {\r
         Log("FormatPart2Fat failed.");\r
         rc = 1;\r
@@ -1584,7 +1693,7 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
     }\r
 \r
     PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);\r
-    Log("Writting Boot Image ............................. ");\r
+    Log("Writing Boot Image ............................. ");\r
     if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)\r
     {\r
         Log("WriteGrubStage1ToPhyDrive failed.");\r
@@ -1593,7 +1702,7 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
     }\r
 \r
     PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);\r
-    Log("Writting Partition Table ........................ ");\r
+    Log("Writing Partition Table ........................ ");\r
     SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);\r
 \r
     if (PartStyle)\r
@@ -1627,6 +1736,7 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
         }\r
 \r
         Log("Write GPT Info OK ...");\r
+        memcpy(&(pPhyDrive->MBR), &(pGptInfo->MBR), 512);\r
     }\r
     else\r
     {\r
@@ -1637,8 +1747,8 @@ int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
             goto End;\r
         }\r
         Log("Write MBR OK ...");\r
+        memcpy(&(pPhyDrive->MBR), &MBR, 512);\r
     }\r
-    \r
 \r
     //Refresh Drive Layout\r
     DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);\r
@@ -1675,7 +1785,8 @@ End:
         if (state != 1)\r
         {\r
             Log("need to mount ventoy part1...");\r
-            if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[0].StartSectorId, DriveLetters, sizeof(DriveLetters), FALSE))\r
+            \r
+            if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, Part1StartSector, DriveLetters, sizeof(DriveLetters), FALSE))\r
             {\r
                 DriveName[0] = MountDrive;\r
                 bRet = SetVolumeMountPointA(DriveName, DriveLetters);\r
@@ -1719,6 +1830,7 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
     MBR_HEAD BootImg;\r
     MBR_HEAD MBR;\r
     VTOY_GPT_INFO *pGptInfo = NULL;\r
+    UINT8 ReservedData[4096];\r
 \r
     Log("UpdateVentoy2PhyDrive %s PhyDrive%d <<%s %s %dGB>>",\r
         pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,\r
@@ -1771,6 +1883,10 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
         Log("MBR Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);\r
     }\r
 \r
+    //Read Reserved Data\r
+    SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);\r
+    ReadFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);\r
+\r
     GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));\r
 \r
     if (DriveLetters[0] == 0)\r
@@ -1814,7 +1930,7 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
 \r
     Log("Lock volume for update .......................... ");\r
     hVolume = INVALID_HANDLE_VALUE;\r
-    Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[1].StartSectorId, DriveLetters, sizeof(DriveLetters), TRUE);\r
+       Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, StartSector, DriveLetters, sizeof(DriveLetters), TRUE);\r
     if (ERROR_SUCCESS == Status)\r
     {\r
         Log("Now lock and dismount volume <%s>", DriveLetters);\r
@@ -1852,24 +1968,26 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
 \r
     if (!TryWritePart2(hDrive, StartSector))\r
     {\r
-        ForceMBR = TRUE;\r
-        Log("Try write failed, now delete partition 2...");\r
+               if (pPhyDrive->PartStyle == 0)\r
+               {\r
+                       ForceMBR = TRUE;\r
+                       Log("Try write failed, now delete partition 2...");\r
 \r
-        CHECK_CLOSE_HANDLE(hDrive);\r
+                       CHECK_CLOSE_HANDLE(hDrive);\r
 \r
-        Log("Now delete partition 2...");\r
-        DeletePartitions(pPhyDrive->PhyDrive, TRUE);\r
+                       Log("Now delete partition 2...");\r
+                       DeletePartitions(pPhyDrive->PhyDrive, TRUE);\r
 \r
-        hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);\r
-        if (hDrive == INVALID_HANDLE_VALUE)\r
-        {\r
-            Log("Failed to GetPhysicalHandle for write.");\r
-            rc = 1;\r
-            goto End;\r
-        }\r
+                       hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);\r
+                       if (hDrive == INVALID_HANDLE_VALUE)\r
+                       {\r
+                               Log("Failed to GetPhysicalHandle for write.");\r
+                               rc = 1;\r
+                               goto End;\r
+                       }\r
+               }\r
     }\r
 \r
-\r
     PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);\r
 \r
     Log("Write Ventoy to disk ............................ ");\r
@@ -1893,6 +2011,11 @@ int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
         goto End;\r
     }\r
 \r
+    //write reserved data\r
+    SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);    \r
+    bRet = WriteFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);\r
+    Log("Write resv data ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);\r
+\r
     // Boot Image\r
     VentoyGetLocalBootImg(&BootImg);\r
 \r