1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 * Copyright (c) 2011-2020, Pete Batard <pete@akeo.ie>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include "Ventoy2Disk.h"
30 #include "fat_filelib.h"
32 #include "DiskService.h"
34 static int g_backup_bin_index
= 0;
37 static BOOL
WriteDataToPhyDisk(HANDLE hDrive
, UINT64 Offset
, VOID
*buffer
, DWORD len
)
41 LARGE_INTEGER liCurPosition
;
42 LARGE_INTEGER liNewPosition
;
44 liCurPosition
.QuadPart
= (LONGLONG
)Offset
;
45 liNewPosition
.QuadPart
= 0;
46 if (0 == SetFilePointerEx(hDrive
, liCurPosition
, &liNewPosition
, FILE_BEGIN
) ||
47 liNewPosition
.QuadPart
!= liCurPosition
.QuadPart
)
49 Log("SetFilePointerEx Failed %u", LASTERR
);
53 bRet
= WriteFile(hDrive
, buffer
, len
, &dwSize
, NULL
);
54 if (bRet
== FALSE
|| dwSize
!= len
)
56 Log("Write file error %u %u", dwSize
, LASTERR
);
64 static DWORD
GetVentoyVolumeName(int PhyDrive
, UINT64 StartSectorId
, CHAR
*NameBuf
, UINT32 BufLen
, BOOL DelSlash
)
72 DWORD Status
= ERROR_NOT_FOUND
;
73 DISK_EXTENT
*pExtents
= NULL
;
74 CHAR VolumeName
[MAX_PATH
] = { 0 };
75 VOLUME_DISK_EXTENTS DiskExtents
;
77 PartOffset
= 512ULL * StartSectorId
;
79 Log("GetVentoyVolumeName PhyDrive %d SectorStart:%llu PartOffset:%llu", PhyDrive
, (ULONGLONG
)StartSectorId
, (ULONGLONG
)PartOffset
);
81 hVolume
= FindFirstVolumeA(VolumeName
, sizeof(VolumeName
));
82 if (hVolume
== INVALID_HANDLE_VALUE
)
89 len
= strlen(VolumeName
);
90 Log("Find volume:%s", VolumeName
);
92 VolumeName
[len
- 1] = 0;
94 hDrive
= CreateFileA(VolumeName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
95 if (hDrive
== INVALID_HANDLE_VALUE
)
100 bRet
= DeviceIoControl(hDrive
,
101 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
105 (DWORD
)(sizeof(DiskExtents
)),
109 Log("IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS bRet:%u code:%u", bRet
, LASTERR
);
110 Log("NumberOfDiskExtents:%u DiskNumber:%u", DiskExtents
.NumberOfDiskExtents
, DiskExtents
.Extents
[0].DiskNumber
);
112 if (bRet
&& DiskExtents
.NumberOfDiskExtents
== 1)
114 pExtents
= DiskExtents
.Extents
;
116 Log("This volume DiskNumber:%u offset:%llu", pExtents
->DiskNumber
, (ULONGLONG
)pExtents
->StartingOffset
.QuadPart
);
117 if ((int)pExtents
->DiskNumber
== PhyDrive
&& pExtents
->StartingOffset
.QuadPart
== PartOffset
)
119 Log("This volume match");
123 VolumeName
[len
- 1] = '\\';
126 sprintf_s(NameBuf
, BufLen
, "%s", VolumeName
);
127 Status
= ERROR_SUCCESS
;
134 } while (FindNextVolumeA(hVolume
, VolumeName
, sizeof(VolumeName
)));
136 FindVolumeClose(hVolume
);
138 Log("GetVentoyVolumeName return %u", Status
);
142 int GetLettersBelongPhyDrive(int PhyDrive
, char *DriveLetters
, size_t Length
)
147 CHAR
*StringBuf
= NULL
;
149 DataSize
= GetLogicalDriveStringsA(0, NULL
);
150 StringBuf
= (CHAR
*)malloc(DataSize
+ 1);
151 if (StringBuf
== NULL
)
156 GetLogicalDriveStringsA(DataSize
, StringBuf
);
158 for (Pos
= StringBuf
; *Pos
; Pos
+= strlen(Pos
) + 1)
160 if (n
< (int)Length
&& PhyDrive
== GetPhyDriveByLogicalDrive(Pos
[0], NULL
))
162 Log("%C: is belong to phydrive%d", Pos
[0], PhyDrive
);
163 DriveLetters
[n
++] = Pos
[0];
171 HANDLE
GetPhysicalHandle(int Drive
, BOOLEAN bLockDrive
, BOOLEAN bWriteAccess
, BOOLEAN bWriteShare
)
177 HANDLE hDrive
= INVALID_HANDLE_VALUE
;
179 CHAR DevPath
[MAX_PATH
] = { 0 };
181 safe_sprintf(PhyDrive
, "\\\\.\\PhysicalDrive%d", Drive
);
183 if (0 == QueryDosDeviceA(PhyDrive
+ 4, DevPath
, sizeof(DevPath
)))
185 Log("QueryDosDeviceA failed error:%u", GetLastError());
186 strcpy_s(DevPath
, sizeof(DevPath
), "???");
190 Log("QueryDosDeviceA success %s", DevPath
);
193 for (i
= 0; i
< DRIVE_ACCESS_RETRIES
; i
++)
195 // Try without FILE_SHARE_WRITE (unless specifically requested) so that
196 // we won't be bothered by the OS or other apps when we set up our data.
197 // However this means we might have to wait for an access gap...
198 // We keep FILE_SHARE_READ though, as this shouldn't hurt us any, and is
199 // required for enumeration.
200 hDrive
= CreateFileA(PhyDrive
,
201 GENERIC_READ
| (bWriteAccess
? GENERIC_WRITE
: 0),
202 FILE_SHARE_READ
| (bWriteShare
? FILE_SHARE_WRITE
: 0),
205 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
,
208 LastError
= GetLastError();
209 Log("[%d] CreateFileA %s code:%u %p", i
, PhyDrive
, LastError
, hDrive
);
211 if (hDrive
!= INVALID_HANDLE_VALUE
)
216 if ((LastError
!= ERROR_SHARING_VIOLATION
) && (LastError
!= ERROR_ACCESS_DENIED
))
223 Log("Waiting for access on %s [%s]...", PhyDrive
, DevPath
);
225 else if (!bWriteShare
&& (i
> DRIVE_ACCESS_RETRIES
/ 3))
227 // If we can't seem to get a hold of the drive for some time, try to enable FILE_SHARE_WRITE...
228 Log("Warning: Could not obtain exclusive rights. Retrying with write sharing enabled...");
231 // Try to report the process that is locking the drive
232 // We also use bit 6 as a flag to indicate that SearchProcess was called.
233 //access_mask = SearchProcess(DevPath, SEARCH_PROCESS_TIMEOUT, TRUE, TRUE, FALSE) | 0x40;
236 Sleep(DRIVE_ACCESS_TIMEOUT
/ DRIVE_ACCESS_RETRIES
);
239 if (hDrive
== INVALID_HANDLE_VALUE
)
241 Log("Could not open %s %u", PhyDrive
, LASTERR
);
247 Log("Opened %s for %s write access", PhyDrive
, bWriteShare
? "shared" : "exclusive");
252 if (DeviceIoControl(hDrive
, FSCTL_ALLOW_EXTENDED_DASD_IO
, NULL
, 0, NULL
, 0, &dwSize
, NULL
))
254 Log("I/O boundary checks disabled");
257 EndTime
= GetTickCount64() + DRIVE_ACCESS_TIMEOUT
;
260 if (DeviceIoControl(hDrive
, FSCTL_LOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
))
262 Log("FSCTL_LOCK_VOLUME success");
265 Sleep(DRIVE_ACCESS_TIMEOUT
/ DRIVE_ACCESS_RETRIES
);
266 } while (GetTickCount64() < EndTime
);
268 // If we reached this section, either we didn't manage to get a lock or the user cancelled
269 Log("Could not lock access to %s %u", PhyDrive
, LASTERR
);
271 // See if we can report the processes are accessing the drive
272 //if (!IS_ERROR(FormatStatus) && (access_mask == 0))
273 // access_mask = SearchProcess(DevPath, SEARCH_PROCESS_TIMEOUT, TRUE, TRUE, FALSE);
274 // Try to continue if the only access rights we saw were for read-only
275 //if ((access_mask & 0x07) != 0x01)
276 // safe_closehandle(hDrive);
278 CHECK_CLOSE_HANDLE(hDrive
);
283 if (hDrive
== INVALID_HANDLE_VALUE
)
285 Log("Can get handle of %s, maybe some process control it.", DevPath
);
291 int GetPhyDriveByLogicalDrive(int DriveLetter
, UINT64
*Offset
)
295 HANDLE Handle
= INVALID_HANDLE_VALUE
;
296 VOLUME_DISK_EXTENTS DiskExtents
;
299 safe_sprintf(PhyPath
, "\\\\.\\%C:", (CHAR
)DriveLetter
);
301 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
302 if (Handle
== INVALID_HANDLE_VALUE
)
304 Log("Could not open the disk<%s>, error:%u", PhyPath
, LASTERR
);
308 memset(&DiskExtents
, 0, sizeof(DiskExtents
));
309 Ret
= DeviceIoControl(Handle
,
310 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
314 (DWORD
)(sizeof(DiskExtents
)),
318 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
320 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, LASTERR
);
321 CHECK_CLOSE_HANDLE(Handle
);
324 CHECK_CLOSE_HANDLE(Handle
);
326 Log("LogicalDrive:%s PhyDrive:%d Num:%d Offset:%llu ExtentLength:%llu",
328 DiskExtents
.Extents
[0].DiskNumber
,
329 DiskExtents
.NumberOfDiskExtents
,
330 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
331 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
336 *Offset
= (UINT64
)(DiskExtents
.Extents
[0].StartingOffset
.QuadPart
);
339 return (int)DiskExtents
.Extents
[0].DiskNumber
;
342 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO
*pDriveList
, DWORD
*pDriveCount
)
350 DWORD DriveCount
= 0;
351 HANDLE Handle
= INVALID_HANDLE_VALUE
;
353 PHY_DRIVE_INFO
*CurDrive
= pDriveList
;
354 GET_LENGTH_INFORMATION LengthInfo
;
355 STORAGE_PROPERTY_QUERY Query
;
356 STORAGE_DESCRIPTOR_HEADER DevDescHeader
;
357 STORAGE_DEVICE_DESCRIPTOR
*pDevDesc
;
358 STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR diskAlignment
;
359 int PhyDriveId
[VENTOY_MAX_PHY_DRIVE
];
361 Count
= GetPhysicalDriveCount();
363 for (i
= 0; i
< Count
&& i
< VENTOY_MAX_PHY_DRIVE
; i
++)
368 dwBytes
= GetLogicalDrives();
369 Log("Logical Drives: 0x%x", dwBytes
);
374 id
= GetPhyDriveByLogicalDrive(Letter
, NULL
);
375 Log("%C --> %d", Letter
, id
);
378 for (i
= 0; i
< Count
; i
++)
380 if (PhyDriveId
[i
] == id
)
388 Log("Add phy%d to list", i
);
389 PhyDriveId
[Count
] = id
;
399 for (i
= 0; i
< Count
&& DriveCount
< VENTOY_MAX_PHY_DRIVE
; i
++)
401 CHECK_CLOSE_HANDLE(Handle
);
403 safe_sprintf(PhyDrive
, "\\\\.\\PhysicalDrive%d", PhyDriveId
[i
]);
404 Handle
= CreateFileA(PhyDrive
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
405 Log("Create file Handle:%p %s status:%u", Handle
, PhyDrive
, LASTERR
);
407 if (Handle
== INVALID_HANDLE_VALUE
)
412 bRet
= DeviceIoControl(Handle
,
413 IOCTL_DISK_GET_LENGTH_INFO
, NULL
,
421 Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR
);
425 Log("PHYSICALDRIVE%d size %llu bytes", i
, (ULONGLONG
)LengthInfo
.Length
.QuadPart
);
427 Query
.PropertyId
= StorageDeviceProperty
;
428 Query
.QueryType
= PropertyStandardQuery
;
430 bRet
= DeviceIoControl(Handle
,
431 IOCTL_STORAGE_QUERY_PROPERTY
,
435 sizeof(STORAGE_DESCRIPTOR_HEADER
),
440 Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR
, dwBytes
);
444 if (DevDescHeader
.Size
< sizeof(STORAGE_DEVICE_DESCRIPTOR
))
446 Log("Invalid DevDescHeader.Size:%u", DevDescHeader
.Size
);
450 pDevDesc
= (STORAGE_DEVICE_DESCRIPTOR
*)malloc(DevDescHeader
.Size
);
453 Log("failed to malloc error:%u len:%u", LASTERR
, DevDescHeader
.Size
);
457 bRet
= DeviceIoControl(Handle
,
458 IOCTL_STORAGE_QUERY_PROPERTY
,
467 Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR
, dwBytes
);
474 memset(&Query
, 0, sizeof(STORAGE_PROPERTY_QUERY
));
475 Query
.PropertyId
= StorageAccessAlignmentProperty
;
476 Query
.QueryType
= PropertyStandardQuery
;
477 memset(&diskAlignment
, 0, sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR
));
479 bRet
= DeviceIoControl(Handle
,
480 IOCTL_STORAGE_QUERY_PROPERTY
,
482 sizeof(STORAGE_PROPERTY_QUERY
),
484 sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR
),
489 Log("DeviceIoControl3 error:%u dwBytes:%u", LASTERR
, dwBytes
);
492 CurDrive
->PhyDrive
= i
;
493 CurDrive
->SizeInBytes
= LengthInfo
.Length
.QuadPart
;
494 CurDrive
->DeviceType
= pDevDesc
->DeviceType
;
495 CurDrive
->RemovableMedia
= pDevDesc
->RemovableMedia
;
496 CurDrive
->BusType
= pDevDesc
->BusType
;
498 CurDrive
->BytesPerLogicalSector
= diskAlignment
.BytesPerLogicalSector
;
499 CurDrive
->BytesPerPhysicalSector
= diskAlignment
.BytesPerPhysicalSector
;
501 if (pDevDesc
->VendorIdOffset
)
503 safe_strcpy(CurDrive
->VendorId
, (char *)pDevDesc
+ pDevDesc
->VendorIdOffset
);
504 TrimString(CurDrive
->VendorId
);
507 if (pDevDesc
->ProductIdOffset
)
509 safe_strcpy(CurDrive
->ProductId
, (char *)pDevDesc
+ pDevDesc
->ProductIdOffset
);
510 TrimString(CurDrive
->ProductId
);
513 if (pDevDesc
->ProductRevisionOffset
)
515 safe_strcpy(CurDrive
->ProductRev
, (char *)pDevDesc
+ pDevDesc
->ProductRevisionOffset
);
516 TrimString(CurDrive
->ProductRev
);
519 if (pDevDesc
->SerialNumberOffset
)
521 safe_strcpy(CurDrive
->SerialNumber
, (char *)pDevDesc
+ pDevDesc
->SerialNumberOffset
);
522 TrimString(CurDrive
->SerialNumber
);
530 CHECK_CLOSE_HANDLE(Handle
);
533 for (i
= 0, CurDrive
= pDriveList
; i
< (int)DriveCount
; i
++, CurDrive
++)
535 Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Sector:%u/%u Name:%s %s",
536 CurDrive
->PhyDrive
, GetBusTypeString(CurDrive
->BusType
), CurDrive
->RemovableMedia
,
537 GetHumanReadableGBSize(CurDrive
->SizeInBytes
), CurDrive
->SizeInBytes
,
538 CurDrive
->BytesPerLogicalSector
, CurDrive
->BytesPerPhysicalSector
,
539 CurDrive
->VendorId
, CurDrive
->ProductId
);
542 *pDriveCount
= DriveCount
;
548 static HANDLE g_FatPhyDrive
;
549 static UINT64 g_Part2StartSec
;
550 static int GetVentoyVersionFromFatFile(CHAR
*VerBuf
, size_t BufLen
)
557 flfile
= fl_fopen("/grub/grub.cfg", "rb");
560 fl_fseek(flfile
, 0, SEEK_END
);
561 size
= (int)fl_ftell(flfile
);
563 fl_fseek(flfile
, 0, SEEK_SET
);
565 buf
= (char *)malloc(size
+ 1);
568 fl_fread(buf
, 1, size
, flfile
);
572 sprintf_s(VerBuf
, BufLen
, "%s", ParseVentoyVersionFromString(buf
));
582 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
587 LARGE_INTEGER liCurrentPosition
;
589 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
590 liCurrentPosition
.QuadPart
*= 512;
591 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
593 ReadSize
= (DWORD
)(SectorCount
* 512);
595 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
596 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
598 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet
, ReadSize
, dwSize
, LASTERR
);
605 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO
*pDriveInfo
, UINT64 Part2StartSector
, CHAR
*VerBuf
, size_t BufLen
, BOOL
*pSecureBoot
)
611 hDrive
= GetPhysicalHandle(pDriveInfo
->PhyDrive
, FALSE
, FALSE
, FALSE
);
612 if (hDrive
== INVALID_HANDLE_VALUE
)
617 g_FatPhyDrive
= hDrive
;
618 g_Part2StartSec
= Part2StartSector
;
620 Log("Parse FAT fs...");
624 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
626 Log("attach media success...");
627 rc
= GetVentoyVersionFromFatFile(VerBuf
, BufLen
);
631 Log("attach media failed...");
635 Log("GetVentoyVerInPhyDrive rc=%d...", rc
);
638 Log("VentoyVerInPhyDrive %d is <%s>...", pDriveInfo
->PhyDrive
, VerBuf
);
640 flfile
= fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
650 CHECK_CLOSE_HANDLE(hDrive
);
659 static unsigned int g_disk_unxz_len
= 0;
660 static BYTE
*g_part_img_pos
= NULL
;
661 static BYTE
*g_part_img_buf
[VENTOY_EFI_PART_SIZE
/ SIZE_1MB
];
664 static int VentoyFatMemRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
670 for (i
= 0; i
< SectorCount
; i
++)
672 offset
= (Sector
+ i
) * 512;
674 if (g_part_img_buf
[1] == NULL
)
676 MbBuf
= g_part_img_buf
[0] + offset
;
677 memcpy(Buffer
+ i
* 512, MbBuf
, 512);
681 MbBuf
= g_part_img_buf
[offset
/ SIZE_1MB
];
682 memcpy(Buffer
+ i
* 512, MbBuf
+ (offset
% SIZE_1MB
), 512);
690 static int VentoyFatMemWrite(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
696 for (i
= 0; i
< SectorCount
; i
++)
698 offset
= (Sector
+ i
) * 512;
700 if (g_part_img_buf
[1] == NULL
)
702 MbBuf
= g_part_img_buf
[0] + offset
;
703 memcpy(MbBuf
, Buffer
+ i
* 512, 512);
707 MbBuf
= g_part_img_buf
[offset
/ SIZE_1MB
];
708 memcpy(MbBuf
+ (offset
% SIZE_1MB
), Buffer
+ i
* 512, 512);
715 int VentoyProcSecureBoot(BOOL SecureBoot
)
719 char *filebuf
= NULL
;
722 Log("VentoyProcSecureBoot %d ...", SecureBoot
);
726 Log("Secure boot is enabled ...");
732 if (0 == fl_attach_media(VentoyFatMemRead
, VentoyFatMemWrite
))
734 file
= fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
735 Log("Open ventoy efi file %p ", file
);
738 fl_fseek(file
, 0, SEEK_END
);
739 size
= (int)fl_ftell(file
);
740 fl_fseek(file
, 0, SEEK_SET
);
742 Log("ventoy efi file size %d ...", size
);
744 filebuf
= (char *)malloc(size
);
747 fl_fread(filebuf
, 1, size
, file
);
752 Log("Now delete all efi files ...");
753 fl_remove("/EFI/BOOT/BOOTX64.EFI");
754 fl_remove("/EFI/BOOT/grubx64.efi");
755 fl_remove("/EFI/BOOT/grubx64_real.efi");
756 fl_remove("/EFI/BOOT/MokManager.efi");
757 fl_remove("/EFI/BOOT/mmx64.efi");
758 fl_remove("/ENROLL_THIS_KEY_IN_MOKMANAGER.cer");
759 fl_remove("/EFI/BOOT/grub.efi");
761 file
= fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb");
762 Log("Open bootx64 efi file %p ", file
);
767 fl_fwrite(filebuf
, 1, size
, file
);
780 file
= fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");
781 Log("Open ventoy efi file %p ", file
);
784 fl_fseek(file
, 0, SEEK_END
);
785 size
= (int)fl_ftell(file
);
786 fl_fseek(file
, 0, SEEK_SET
);
788 Log("ventoy efi file size %d ...", size
);
790 filebuf
= (char *)malloc(size
);
793 fl_fread(filebuf
, 1, size
, file
);
798 Log("Now delete all efi files ...");
799 fl_remove("/EFI/BOOT/BOOTIA32.EFI");
800 fl_remove("/EFI/BOOT/grubia32.efi");
801 fl_remove("/EFI/BOOT/grubia32_real.efi");
802 fl_remove("/EFI/BOOT/mmia32.efi");
804 file
= fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");
805 Log("Open bootia32 efi file %p ", file
);
810 fl_fwrite(filebuf
, 1, size
, file
);
836 static int disk_xz_flush(void *src
, unsigned int size
)
839 BYTE
*buf
= (BYTE
*)src
;
841 for (i
= 0; i
< size
; i
++)
843 *g_part_img_pos
= *buf
++;
846 if ((g_disk_unxz_len
% SIZE_1MB
) == 0)
848 g_part_img_pos
= g_part_img_buf
[g_disk_unxz_len
/ SIZE_1MB
];
859 static void unxz_error(char *x
)
864 static BOOL
TryWritePart2(HANDLE hDrive
, UINT64 StartSectorId
)
867 DWORD TrySize
= 16 * 1024;
870 unsigned char *data
= NULL
;
871 LARGE_INTEGER liCurrentPosition
;
873 liCurrentPosition
.QuadPart
= StartSectorId
* 512;
874 SetFilePointerEx(hDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
876 Buffer
= malloc(TrySize
);
878 bRet
= WriteFile(hDrive
, Buffer
, TrySize
, &dwSize
, NULL
);
882 Log("Try write part2 bRet:%u dwSize:%u code:%u", bRet
, dwSize
, LASTERR
);
884 if (bRet
&& dwSize
== TrySize
)
892 static int FormatPart2Fat(HANDLE hDrive
, UINT64 StartSectorId
)
899 int Pos
= PT_WRITE_VENTOY_START
;
902 unsigned char *data
= NULL
;
903 LARGE_INTEGER liCurrentPosition
;
904 LARGE_INTEGER liNewPosition
;
905 BYTE
*CheckBuf
= NULL
;
907 Log("FormatPart2Fat %llu...", (ULONGLONG
)StartSectorId
);
909 CheckBuf
= malloc(SIZE_1MB
);
912 Log("Failed to malloc check buf");
916 rc
= ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG
, 0, (void **)&data
, &len
);
919 Log("Failed to read img file %p %u", data
, len
);
924 liCurrentPosition
.QuadPart
= StartSectorId
* 512;
925 SetFilePointerEx(hDrive
, liCurrentPosition
, &liNewPosition
, FILE_BEGIN
);
927 memset(g_part_img_buf
, 0, sizeof(g_part_img_buf
));
929 g_part_img_buf
[0] = (BYTE
*)malloc(VENTOY_EFI_PART_SIZE
);
930 if (g_part_img_buf
[0])
932 Log("Malloc whole img buffer success, now decompress ...");
933 unxz(data
, len
, NULL
, NULL
, g_part_img_buf
[0], &writelen
, unxz_error
);
937 Log("decompress finished success");
939 VentoyProcSecureBoot(g_SecureBoot
);
941 for (i
= 0; i
< VENTOY_EFI_PART_SIZE
/ SIZE_1MB
; i
++)
944 bRet
= WriteFile(hDrive
, g_part_img_buf
[0] + i
* SIZE_1MB
, SIZE_1MB
, &dwSize
, NULL
);
945 Log("Write part data bRet:%u dwSize:%u code:%u", bRet
, dwSize
, LASTERR
);
953 PROGRESS_BAR_SET_POS(Pos
);
960 //Read and check the data
961 liCurrentPosition
.QuadPart
= StartSectorId
* 512;
962 SetFilePointerEx(hDrive
, liCurrentPosition
, &liNewPosition
, FILE_BEGIN
);
964 for (i
= 0; i
< VENTOY_EFI_PART_SIZE
/ SIZE_1MB
; i
++)
966 bRet
= ReadFile(hDrive
, CheckBuf
, SIZE_1MB
, &dwSize
, NULL
);
967 Log("Read part data bRet:%u dwSize:%u code:%u", bRet
, dwSize
, LASTERR
);
969 if (!bRet
|| memcmp(CheckBuf
, g_part_img_buf
[0] + i
* SIZE_1MB
, SIZE_1MB
))
971 Log("### [Check Fail] The data write and read does not match");
976 PROGRESS_BAR_SET_POS(Pos
);
986 Log("decompress finished failed");
992 Log("Failed to malloc whole img size %u, now split it", VENTOY_EFI_PART_SIZE
);
995 for (i
= 0; i
< VENTOY_EFI_PART_SIZE
/ SIZE_1MB
; i
++)
997 g_part_img_buf
[i
] = (BYTE
*)malloc(SIZE_1MB
);
998 if (g_part_img_buf
[i
] == NULL
)
1005 Log("Malloc part img buffer success, now decompress ...");
1007 g_part_img_pos
= g_part_img_buf
[0];
1009 unxz(data
, len
, NULL
, disk_xz_flush
, NULL
, NULL
, unxz_error
);
1011 if (g_disk_unxz_len
== VENTOY_EFI_PART_SIZE
)
1013 Log("decompress finished success");
1015 VentoyProcSecureBoot(g_SecureBoot
);
1017 for (i
= 0; i
< VENTOY_EFI_PART_SIZE
/ SIZE_1MB
; i
++)
1020 bRet
= WriteFile(hDrive
, g_part_img_buf
[i
], SIZE_1MB
, &dwSize
, NULL
);
1021 Log("Write part data bRet:%u dwSize:%u code:%u", bRet
, dwSize
, LASTERR
);
1029 PROGRESS_BAR_SET_POS(Pos
);
1036 //Read and check the data
1037 liCurrentPosition
.QuadPart
= StartSectorId
* 512;
1038 SetFilePointerEx(hDrive
, liCurrentPosition
, &liNewPosition
, FILE_BEGIN
);
1040 for (i
= 0; i
< VENTOY_EFI_PART_SIZE
/ SIZE_1MB
; i
++)
1042 bRet
= ReadFile(hDrive
, CheckBuf
, SIZE_1MB
, &dwSize
, NULL
);
1043 Log("Read part data bRet:%u dwSize:%u code:%u", bRet
, dwSize
, LASTERR
);
1045 if (!bRet
|| memcmp(CheckBuf
, g_part_img_buf
[i
], SIZE_1MB
))
1047 Log("### [Check Fail] The data write and read does not match");
1052 PROGRESS_BAR_SET_POS(Pos
);
1062 Log("decompress finished failed");
1069 if (data
) free(data
);
1070 if (CheckBuf
)free(CheckBuf
);
1074 for (i
= 0; i
< VENTOY_EFI_PART_SIZE
/ SIZE_1MB
; i
++)
1076 if (g_part_img_buf
[i
]) free(g_part_img_buf
[i
]);
1081 if (g_part_img_buf
[0]) free(g_part_img_buf
[0]);
1087 static int WriteGrubStage1ToPhyDrive(HANDLE hDrive
, int PartStyle
)
1093 BYTE
*ImgBuf
= NULL
;
1094 BYTE
*RawBuf
= NULL
;
1096 Log("WriteGrubStage1ToPhyDrive ...");
1098 RawBuf
= (BYTE
*)malloc(SIZE_1MB
);
1104 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG
, 0, (void **)&ImgBuf
, &Len
))
1106 Log("Failed to read stage1 img");
1111 unxz(ImgBuf
, Len
, NULL
, NULL
, RawBuf
, &readLen
, unxz_error
);
1115 Log("Write GPT stage1 ...");
1116 RawBuf
[500] = 35;//update blocklist
1117 SetFilePointer(hDrive
, 512 * 34, NULL
, FILE_BEGIN
);
1118 bRet
= WriteFile(hDrive
, RawBuf
, SIZE_1MB
- 512 * 34, &dwSize
, NULL
);
1122 Log("Write MBR stage1 ...");
1123 SetFilePointer(hDrive
, 512, NULL
, FILE_BEGIN
);
1124 bRet
= WriteFile(hDrive
, RawBuf
, SIZE_1MB
- 512, &dwSize
, NULL
);
1127 Log("WriteFile Ret:%u dwSize:%u ErrCode:%u", bRet
, dwSize
, GetLastError());
1135 static int FormatPart1LargeFAT32(UINT64 DiskSizeBytes
, int CluserSize
)
1141 Option
.fmt
= FM_FAT32
;
1146 if (CluserSize
== 0)
1148 // < 32GB select 32KB as cluster size
1149 // > 32GB select 128KB as cluster size
1150 if (DiskSizeBytes
/ 1024 / 1024 / 1024 <= 32)
1152 Option
.au_size
= 32768;
1156 Option
.au_size
= 131072;
1161 Option
.au_size
= CluserSize
;
1164 Log("Formatting Part1 large FAT32 ClusterSize:%u(%uKB) ...", CluserSize
, CluserSize
/ 1024);
1166 disk_io_reset_write_error();
1168 Ret
= f_mkfs(TEXT("0:"), &Option
, 0, 8 * 1024 * 1024);
1171 if (disk_io_is_write_error())
1173 Log("Formatting Part1 large FAT32 failed, write error.");
1177 Log("Formatting Part1 large FAT32 success, now set label");
1179 Ret
= f_mount(&FS
, TEXT("0:"), 1);
1182 Log("f_mount SUCCESS");
1183 Ret
= f_setlabel(TEXT("0:Ventoy"));
1186 Log("f_setlabel SUCCESS");
1187 Ret
= f_unmount(TEXT("0:"));
1188 Log("f_unmount %d %s", Ret
, (FR_OK
== Ret
) ? "SUCCESS" : "FAILED");
1192 Log("f_setlabel failed %d", Ret
);
1197 Log("f_mount failed %d", Ret
);
1204 Log("Formatting Part1 large FAT32 failed");
1209 static int FormatPart1exFAT(UINT64 DiskSizeBytes
)
1214 Option
.fmt
= FM_EXFAT
;
1219 // < 32GB select 32KB as cluster size
1220 // > 32GB select 128KB as cluster size
1221 if (DiskSizeBytes
/ 1024 / 1024 / 1024 <= 32)
1223 Option
.au_size
= 32768;
1227 Option
.au_size
= 131072;
1230 Log("Formatting Part1 exFAT ...");
1232 disk_io_reset_write_error();
1234 Ret
= f_mkfs(TEXT("0:"), &Option
, 0, 8 * 1024 * 1024);
1237 if (disk_io_is_write_error())
1239 Log("Formatting Part1 exFAT failed, write error.");
1243 Log("Formatting Part1 exFAT success");
1248 Log("Formatting Part1 exFAT failed");
1253 static int ZeroPart1FileSystem(HANDLE hDrive
, UINT64 Part2StartSector
)
1257 LARGE_INTEGER liCurPos
;
1258 LARGE_INTEGER liNewPos
;
1259 CHAR TmpBuffer
[1024] = { 0 };
1261 liCurPos
.QuadPart
= VENTOY_PART1_START_SECTOR
* 512;
1262 liNewPos
.QuadPart
= 0;
1263 if (0 == SetFilePointerEx(hDrive
, liCurPos
, &liNewPos
, FILE_BEGIN
) ||
1264 liNewPos
.QuadPart
!= liCurPos
.QuadPart
)
1266 Log("SetFilePointerEx Failed %u %llu %llu", LASTERR
, (ULONGLONG
)liCurPos
.QuadPart
, (ULONGLONG
)liNewPos
.QuadPart
);
1270 for (i
= 0; i
< 1024; i
++)
1272 WriteFile(hDrive
, TmpBuffer
, 1024, &dwSize
, NULL
);
1275 liCurPos
.QuadPart
= (Part2StartSector
* 512) - (1024 * 1024);
1276 liNewPos
.QuadPart
= 0;
1277 if (0 == SetFilePointerEx(hDrive
, liCurPos
, &liNewPos
, FILE_BEGIN
) ||
1278 liNewPos
.QuadPart
!= liCurPos
.QuadPart
)
1280 Log("SetFilePointerEx Failed %u %llu %llu", LASTERR
, (ULONGLONG
)liCurPos
.QuadPart
, (ULONGLONG
)liNewPos
.QuadPart
);
1284 for (i
= 0; i
< 1024; i
++)
1286 WriteFile(hDrive
, TmpBuffer
, 1024, &dwSize
, NULL
);
1289 Log("Zero Part1 SUCCESS");
1293 int ClearVentoyFromPhyDrive(HWND hWnd
, PHY_DRIVE_INFO
*pPhyDrive
, char *pDrvLetter
)
1302 CHAR DriveName
[] = "?:\\";
1303 CHAR DriveLetters
[MAX_PATH
] = { 0 };
1304 LARGE_INTEGER liCurrentPosition
;
1305 char *pTmpBuf
= NULL
;
1310 Log("ClearVentoyFromPhyDrive PhyDrive%d <<%s %s %dGB>>",
1311 pPhyDrive
->PhyDrive
, pPhyDrive
->VendorId
, pPhyDrive
->ProductId
,
1312 GetHumanReadableGBSize(pPhyDrive
->SizeInBytes
));
1314 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
1316 Log("Lock disk for clean ............................. ");
1318 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, FALSE
, FALSE
);
1319 if (hDrive
== INVALID_HANDLE_VALUE
)
1321 Log("Failed to open physical disk");
1325 GetLettersBelongPhyDrive(pPhyDrive
->PhyDrive
, DriveLetters
, sizeof(DriveLetters
));
1327 if (DriveLetters
[0] == 0)
1329 Log("No drive letter was assigned...");
1330 DriveName
[0] = GetFirstUnusedDriveLetter();
1331 Log("GetFirstUnusedDriveLetter %C: ...", DriveName
[0]);
1335 // Unmount all mounted volumes that belong to this drive
1336 // Do it in reverse so that we always end on the first volume letter
1337 for (i
= (int)strlen(DriveLetters
); i
> 0; i
--)
1339 DriveName
[0] = DriveLetters
[i
- 1];
1340 bRet
= DeleteVolumeMountPointA(DriveName
);
1341 Log("Delete mountpoint %s ret:%u code:%u", DriveName
, bRet
, GetLastError());
1345 MountDrive
= DriveName
[0];
1346 Log("Will use '%C:' as volume mountpoint", DriveName
[0]);
1348 // It kind of blows, but we have to relinquish access to the physical drive
1349 // for VDS to be able to delete the partitions that reside on it...
1350 DeviceIoControl(hDrive
, FSCTL_UNLOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
1351 CHECK_CLOSE_HANDLE(hDrive
);
1353 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART
);
1355 if (!VDS_DeleteAllPartitions(pPhyDrive
->PhyDrive
))
1357 Log("Notice: Could not delete partitions: %u", GetLastError());
1360 Log("Deleting all partitions ......................... OK");
1362 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE
);
1364 Log("Lock disk for write ............................. ");
1365 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
1366 if (hDrive
== INVALID_HANDLE_VALUE
)
1368 Log("Failed to GetPhysicalHandle for write.");
1373 // clear first and last 2MB space
1374 pTmpBuf
= malloc(SIZE_2MB
);
1377 Log("Failed to alloc memory.");
1381 memset(pTmpBuf
, 0, SIZE_2MB
);
1384 bRet
= WriteFile(hDrive
, pTmpBuf
, SIZE_2MB
- 512, &dwSize
, NULL
);
1385 Log("Write fisrt 1MB ret:%d size:%u err:%d", bRet
, dwSize
, LASTERR
);
1392 SET_FILE_POS(pPhyDrive
->SizeInBytes
- SIZE_2MB
);
1393 bRet
= WriteFile(hDrive
, pTmpBuf
, SIZE_2MB
, &dwSize
, NULL
);
1394 Log("Write 2nd 1MB ret:%d size:%u err:%d", bRet
, dwSize
, LASTERR
);
1403 if (pPhyDrive
->SizeInBytes
> 2199023255552ULL)
1405 VTOY_GPT_INFO
*pGptInfo
;
1406 VTOY_GPT_HDR BackupHead
;
1407 LARGE_INTEGER liCurrentPosition
;
1409 pGptInfo
= (VTOY_GPT_INFO
*)pTmpBuf
;
1411 VentoyFillWholeGpt(pPhyDrive
->SizeInBytes
, pGptInfo
);
1413 SET_FILE_POS(pPhyDrive
->SizeInBytes
- 512);
1414 VentoyFillBackupGptHead(pGptInfo
, &BackupHead
);
1415 if (!WriteFile(hDrive
, &BackupHead
, sizeof(VTOY_GPT_HDR
), &dwSize
, NULL
))
1418 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize
, sizeof(VTOY_GPT_INFO
), GetLastError());
1422 SET_FILE_POS(pPhyDrive
->SizeInBytes
- 512 * 33);
1423 if (!WriteFile(hDrive
, pGptInfo
->PartTbl
, sizeof(pGptInfo
->PartTbl
), &dwSize
, NULL
))
1426 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize
, sizeof(VTOY_GPT_INFO
), GetLastError());
1431 if (!WriteFile(hDrive
, pGptInfo
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
))
1434 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize
, sizeof(VTOY_GPT_INFO
), GetLastError());
1438 Log("Write GPT Info OK ...");
1442 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
1443 Log("Read MBR ret:%d size:%u err:%d", bRet
, dwSize
, LASTERR
);
1450 //clear boot code and partition table (reserved disk signature)
1451 memset(MBR
.BootCode
, 0, 440);
1452 memset(MBR
.PartTbl
, 0, sizeof(MBR
.PartTbl
));
1454 VentoyFillMBRLocation(pPhyDrive
->SizeInBytes
, 2048, (UINT32
)(pPhyDrive
->SizeInBytes
/ 512 - 2048), MBR
.PartTbl
);
1456 MBR
.PartTbl
[0].Active
= 0x00; // bootable
1457 MBR
.PartTbl
[0].FsFlag
= 0x07; // exFAT/NTFS/HPFS
1460 bRet
= WriteFile(hDrive
, &MBR
, 512, &dwSize
, NULL
);
1461 Log("Write MBR ret:%d size:%u err:%d", bRet
, dwSize
, LASTERR
);
1469 Log("Clear Ventoy successfully finished");
1471 //Refresh Drive Layout
1472 DeviceIoControl(hDrive
, IOCTL_DISK_UPDATE_PROPERTIES
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
1476 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME
);
1477 PROGRESS_BAR_SET_POS(PT_REFORMAT_FINISH
);
1486 Log("Mounting Ventoy Partition ....................... ");
1490 memset(DriveLetters
, 0, sizeof(DriveLetters
));
1491 GetLettersBelongPhyDrive(pPhyDrive
->PhyDrive
, DriveLetters
, sizeof(DriveLetters
));
1492 Log("Logical drive letter after write ventoy: <%s>", DriveLetters
);
1494 for (i
= 0; i
< sizeof(DriveLetters
) && DriveLetters
[i
]; i
++)
1496 DriveName
[0] = DriveLetters
[i
];
1497 Log("%s is ventoy part1, already mounted", DriveName
);
1503 Log("need to mount ventoy part1...");
1504 if (0 == GetVentoyVolumeName(pPhyDrive
->PhyDrive
, 2048, DriveLetters
, sizeof(DriveLetters
), FALSE
))
1506 DriveName
[0] = MountDrive
;
1507 bRet
= SetVolumeMountPointA(DriveName
, DriveLetters
);
1508 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName
, DriveLetters
, bRet
, GetLastError());
1510 *pDrvLetter
= MountDrive
;
1514 Log("Failed to find ventoy volume");
1522 FindProcessOccupyDisk(hDrive
, pPhyDrive
);
1525 CHECK_CLOSE_HANDLE(hDrive
);
1529 int InstallVentoy2FileImage(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
)
1538 UINT64 data_offset
= 0;
1539 UINT64 Part2StartSector
= 0;
1540 UINT64 Part1StartSector
= 0;
1541 UINT64 Part1SectorCount
= 0;
1542 UINT8
*pData
= NULL
;
1543 UINT8
*pBkGptPartTbl
= NULL
;
1544 BYTE
*ImgBuf
= NULL
;
1545 MBR_HEAD
*pMBR
= NULL
;
1546 VTSI_FOOTER
*pImgFooter
= NULL
;
1547 VTSI_SEGMENT
*pSegment
= NULL
;
1548 VTOY_GPT_INFO
*pGptInfo
= NULL
;
1549 VTOY_GPT_HDR
*pBkGptHdr
= NULL
;
1552 Log("InstallVentoy2FileImage %s PhyDrive%d <<%s %s %dGB>>",
1553 PartStyle
? "GPT" : "MBR", pPhyDrive
->PhyDrive
, pPhyDrive
->VendorId
, pPhyDrive
->ProductId
,
1554 GetHumanReadableGBSize(pPhyDrive
->SizeInBytes
));
1556 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
1558 size
= SIZE_1MB
+ VENTOY_EFI_PART_SIZE
+ 33 * 512 + VTSI_IMG_MAX_SEG
* sizeof(VTSI_SEGMENT
) + sizeof(VTSI_FOOTER
);
1560 pData
= (UINT8
*)malloc(size
);
1563 Log("malloc image buffer failed %d.", size
);
1567 pImgFooter
= (VTSI_FOOTER
*)(pData
+ size
- sizeof(VTSI_FOOTER
));
1568 pSegment
= (VTSI_SEGMENT
*)((UINT8
*)pImgFooter
- VTSI_IMG_MAX_SEG
* sizeof(VTSI_SEGMENT
));
1569 memset(pImgFooter
, 0, sizeof(VTSI_FOOTER
));
1570 memset(pSegment
, 0, VTSI_IMG_MAX_SEG
* sizeof(VTSI_SEGMENT
));
1572 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START
);
1574 Log("Writing Boot Image ............................. ");
1575 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG
, 0, (void **)&ImgBuf
, &Len
))
1577 Log("Failed to read stage1 img");
1581 unxz(ImgBuf
, Len
, NULL
, NULL
, pData
, &dataLen
, unxz_error
);
1584 Log("decompress %s len:%d", VENTOY_FILE_STG1_IMG
, dataLen
);
1588 pData
[500] = 35;//update blocklist
1589 memmove(pData
+ 34 * 512, pData
, SIZE_1MB
- 512 * 34);
1590 memset(pData
, 0, 34 * 512);
1592 pGptInfo
= (VTOY_GPT_INFO
*)pData
;
1593 memset(pGptInfo
, 0, sizeof(VTOY_GPT_INFO
));
1594 VentoyFillGpt(pPhyDrive
->SizeInBytes
, pGptInfo
);
1596 pBkGptPartTbl
= pData
+ SIZE_1MB
+ VENTOY_EFI_PART_SIZE
;
1597 memset(pBkGptPartTbl
, 0, 33 * 512);
1599 memcpy(pBkGptPartTbl
, pGptInfo
->PartTbl
, 32 * 512);
1600 pBkGptHdr
= (VTOY_GPT_HDR
*)(pBkGptPartTbl
+ 32 * 512);
1601 VentoyFillBackupGptHead(pGptInfo
, pBkGptHdr
);
1603 Part1StartSector
= pGptInfo
->PartTbl
[0].StartLBA
;
1604 Part1SectorCount
= pGptInfo
->PartTbl
[0].LastLBA
- Part1StartSector
+ 1;
1605 Part2StartSector
= pGptInfo
->PartTbl
[1].StartLBA
;
1607 Log("Write GPT Info OK ...");
1611 memmove(pData
+ 512, pData
, SIZE_1MB
- 512);
1612 memset(pData
, 0, 512);
1614 pMBR
= (MBR_HEAD
*)pData
;
1615 VentoyFillMBR(pPhyDrive
->SizeInBytes
, pMBR
, PartStyle
, 0x07);
1616 Part1StartSector
= pMBR
->PartTbl
[0].StartSectorId
;
1617 Part1SectorCount
= pMBR
->PartTbl
[0].SectorCount
;
1618 Part2StartSector
= pMBR
->PartTbl
[1].StartSectorId
;
1620 Log("Write MBR OK ...");
1623 Log("Writing EFI part Image ............................. ");
1624 rc
= ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG
, 0, (void **)&ImgBuf
, &Len
);
1627 Log("Failed to read img file %p %u", ImgBuf
, Len
);
1631 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START
+ 28);
1632 memset(g_part_img_buf
, 0, sizeof(g_part_img_buf
));
1633 unxz(ImgBuf
, Len
, NULL
, NULL
, pData
+ SIZE_1MB
, &dataLen
, unxz_error
);
1636 Log("decompress finished success");
1637 g_part_img_buf
[0] = pData
+ SIZE_1MB
;
1639 VentoyProcSecureBoot(g_SecureBoot
);
1643 Log("decompress finished failed");
1647 fopen_s(&fp
, "VentoySparseImg.vtsi", "wb+");
1650 Log("Failed to create Ventoy img file");
1654 Log("Writing stage1 data ............................. ");
1656 fwrite(pData
, 1, SIZE_1MB
, fp
);
1658 pSegment
[0].disk_start_sector
= 0;
1659 pSegment
[0].sector_num
= SIZE_1MB
/ 512;
1660 pSegment
[0].data_offset
= data_offset
;
1661 data_offset
+= pSegment
[0].sector_num
* 512;
1663 disk_io_set_param(INVALID_HANDLE_VALUE
, Part1StartSector
+ Part1SectorCount
);// include the 2048 sector gap
1664 disk_io_set_imghook(fp
, pSegment
+ 1, VTSI_IMG_MAX_SEG
- 1, data_offset
);
1666 Log("Formatting part1 exFAT ...");
1667 if (0 != FormatPart1exFAT(pPhyDrive
->SizeInBytes
))
1669 Log("FormatPart1exFAT failed.");
1670 disk_io_reset_imghook(&segnum
, &data_offset
);
1674 disk_io_reset_imghook(&segnum
, &data_offset
);
1677 Log("current segment number:%d dataoff:%ld", segnum
, (long)data_offset
);
1680 Log("Writing part2 data ............................. ");
1681 fwrite(pData
+ SIZE_1MB
, 1, VENTOY_EFI_PART_SIZE
, fp
);
1682 pSegment
[segnum
].disk_start_sector
= Part2StartSector
;
1683 pSegment
[segnum
].sector_num
= VENTOY_EFI_PART_SIZE
/ 512;
1684 pSegment
[segnum
].data_offset
= data_offset
;
1685 data_offset
+= pSegment
[segnum
].sector_num
* 512;
1690 Log("Writing backup gpt table ............................. ");
1691 fwrite(pBkGptPartTbl
, 1, 33 * 512, fp
);
1692 pSegment
[segnum
].disk_start_sector
= pPhyDrive
->SizeInBytes
/ 512 - 33;
1693 pSegment
[segnum
].sector_num
= 33;
1694 pSegment
[segnum
].data_offset
= data_offset
;
1695 data_offset
+= pSegment
[segnum
].sector_num
* 512;
1699 Log("Writing segment metadata ............................. ");
1701 for (i
= 0; i
< (int)segnum
; i
++)
1703 Log("SEG[%d]: PhySector:%llu SectorNum:%llu DataOffset:%llu(sector:%llu)", i
, pSegment
[i
].disk_start_sector
, pSegment
[i
].sector_num
,
1704 pSegment
[i
].data_offset
, pSegment
[i
].data_offset
/ 512);
1707 dataLen
= segnum
* sizeof(VTSI_SEGMENT
);
1708 fwrite(pSegment
, 1, dataLen
, fp
);
1712 //pData + SIZE_1MB - 8192 is a temp data buffer with zero
1713 fwrite(pData
+ SIZE_1MB
- 8192, 1, 512 - (dataLen
% 512), fp
);
1717 pImgFooter
->magic
= VTSI_IMG_MAGIC
;
1718 pImgFooter
->version
= 1;
1719 pImgFooter
->disk_size
= pPhyDrive
->SizeInBytes
;
1720 memcpy(&pImgFooter
->disk_signature
, pPhyDrive
->MBR
.BootCode
+ 0x1b8, 4);
1721 pImgFooter
->segment_num
= segnum
;
1722 pImgFooter
->segment_offset
= data_offset
;
1724 for (i
= 0, chksum
= 0; i
< (int)(segnum
* sizeof(VTSI_SEGMENT
)); i
++)
1726 chksum
+= *((UINT8
*)pSegment
+ i
);
1728 pImgFooter
->segment_chksum
= ~chksum
;
1730 for (i
= 0, chksum
= 0; i
< sizeof(VTSI_FOOTER
); i
++)
1732 chksum
+= *((UINT8
*)pImgFooter
+ i
);
1734 pImgFooter
->foot_chksum
= ~chksum
;
1736 Log("Writing footer segnum(%u) segoffset(%llu) ......................", segnum
, data_offset
);
1737 Log("disk_size=%llu disk_signature=%lx segment_offset=%llu", pImgFooter
->disk_size
, pImgFooter
->disk_signature
, pImgFooter
->segment_offset
);
1739 fwrite(pImgFooter
, 1, sizeof(VTSI_FOOTER
), fp
);
1742 Log("Writing Ventoy image file finished, the file size should be %llu .", data_offset
+ 512 + ((dataLen
+ 511) / 512 * 512));
1748 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME
);
1749 PROGRESS_BAR_SET_POS(PT_REFORMAT_FINISH
);
1751 Log("retcode:%d\n", rc
);
1760 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
, int TryId
)
1770 CHAR DriveName
[] = "?:\\";
1771 CHAR DriveLetters
[MAX_PATH
] = { 0 };
1773 VTOY_GPT_INFO
*pGptInfo
= NULL
;
1774 UINT64 Part1StartSector
= 0;
1775 UINT64 Part1SectorCount
= 0;
1776 UINT64 Part2StartSector
= 0;
1777 BOOL LargeFAT32
= FALSE
;
1778 BOOL DefaultExFAT
= FALSE
;
1779 UINT8 FsFlag
= 0x07;
1781 Log("#####################################################");
1782 Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId
,
1783 PartStyle
? "GPT" : "MBR", pPhyDrive
->PhyDrive
, pPhyDrive
->VendorId
, pPhyDrive
->ProductId
,
1784 GetHumanReadableGBSize(pPhyDrive
->SizeInBytes
));
1785 Log("#####################################################");
1789 pGptInfo
= malloc(sizeof(VTOY_GPT_INFO
));
1790 memset(pGptInfo
, 0, sizeof(VTOY_GPT_INFO
));
1793 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
1797 VentoyFillGpt(pPhyDrive
->SizeInBytes
, pGptInfo
);
1798 Part1StartSector
= pGptInfo
->PartTbl
[0].StartLBA
;
1799 Part1SectorCount
= pGptInfo
->PartTbl
[0].LastLBA
- Part1StartSector
+ 1;
1800 Part2StartSector
= pGptInfo
->PartTbl
[1].StartLBA
;
1804 if (GetVentoyFsType() == VTOY_FS_FAT32
)
1809 VentoyFillMBR(pPhyDrive
->SizeInBytes
, &MBR
, PartStyle
, FsFlag
);
1810 Part1StartSector
= MBR
.PartTbl
[0].StartSectorId
;
1811 Part1SectorCount
= MBR
.PartTbl
[0].SectorCount
;
1812 Part2StartSector
= MBR
.PartTbl
[1].StartSectorId
;
1815 Log("Lock disk for clean ............................. ");
1817 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, FALSE
, FALSE
);
1818 if (hDrive
== INVALID_HANDLE_VALUE
)
1820 Log("Failed to open physical disk");
1825 GetLettersBelongPhyDrive(pPhyDrive
->PhyDrive
, DriveLetters
, sizeof(DriveLetters
));
1827 if (DriveLetters
[0] == 0)
1829 Log("No drive letter was assigned...");
1830 DriveName
[0] = GetFirstUnusedDriveLetter();
1831 Log("GetFirstUnusedDriveLetter %C: ...", DriveName
[0]);
1835 // Unmount all mounted volumes that belong to this drive
1836 // Do it in reverse so that we always end on the first volume letter
1837 for (i
= (int)strlen(DriveLetters
); i
> 0; i
--)
1839 DriveName
[0] = DriveLetters
[i
- 1];
1840 bRet
= DeleteVolumeMountPointA(DriveName
);
1841 Log("Delete mountpoint %s ret:%u code:%u", DriveName
, bRet
, GetLastError());
1845 MountDrive
= DriveName
[0];
1846 Log("Will use '%C:' as volume mountpoint", DriveName
[0]);
1848 // It kind of blows, but we have to relinquish access to the physical drive
1849 // for VDS to be able to delete the partitions that reside on it...
1850 DeviceIoControl(hDrive
, FSCTL_UNLOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
1851 CHECK_CLOSE_HANDLE(hDrive
);
1853 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART
);
1855 if (!VDS_DeleteAllPartitions(pPhyDrive
->PhyDrive
))
1857 Log("Notice: Could not delete partitions: 0x%x, but we continue.", GetLastError());
1860 Log("Deleting all partitions ......................... OK");
1862 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE
);
1864 Log("Lock disk for write ............................. ");
1865 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
1866 if (hDrive
== INVALID_HANDLE_VALUE
)
1868 Log("Failed to GetPhysicalHandle for write.");
1873 //Refresh Drive Layout
1874 DeviceIoControl(hDrive
, IOCTL_DISK_UPDATE_PROPERTIES
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
1876 disk_io_set_param(hDrive
, Part1StartSector
+ Part1SectorCount
);// include the 2048 sector gap
1878 PROGRESS_BAR_SET_POS(PT_FORMAT_PART1
);
1880 if (PartStyle
== 1 && pPhyDrive
->PartStyle
== 0)
1882 Log("Wait for format part1 ...");
1886 if (GetVentoyFsType() == VTOY_FS_FAT32
&& (Part1SectorCount
* 512 >= FAT32_MAX_LIMIT
))
1888 Log("Formatting part1 large FAT32 ...");
1890 if (0 != FormatPart1LargeFAT32(pPhyDrive
->SizeInBytes
, GetClusterSize()))
1892 Log("FormatPart1LargeFAT32 failed.");
1897 else if (GetVentoyFsType() == VTOY_FS_EXFAT
&& GetClusterSize() == 0)
1899 Log("Formatting part1 exFAT ...");
1900 DefaultExFAT
= TRUE
;
1901 if (0 != FormatPart1exFAT(pPhyDrive
->SizeInBytes
))
1903 Log("FormatPart1exFAT failed.");
1910 Log("Zero part1 file system ...");
1911 if (0 != ZeroPart1FileSystem(hDrive
, Part2StartSector
))
1913 Log("ZeroPart1FileSystem failed.");
1919 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2
);
1920 Log("Writing part2 FAT img ...");
1922 if (0 != FormatPart2Fat(hDrive
, Part2StartSector
))
1924 Log("FormatPart2Fat failed.");
1929 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG
);
1930 Log("Writing Boot Image ............................. ");
1931 if (WriteGrubStage1ToPhyDrive(hDrive
, PartStyle
) != 0)
1933 Log("WriteGrubStage1ToPhyDrive failed.");
1938 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE
);
1939 Log("Writing Partition Table ........................ ");
1940 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
1944 VTOY_GPT_HDR BackupHead
;
1945 LARGE_INTEGER liCurrentPosition
;
1947 SET_FILE_POS(pPhyDrive
->SizeInBytes
- 512);
1948 VentoyFillBackupGptHead(pGptInfo
, &BackupHead
);
1949 if (!WriteFile(hDrive
, &BackupHead
, sizeof(VTOY_GPT_HDR
), &dwSize
, NULL
))
1952 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize
, sizeof(VTOY_GPT_INFO
), GetLastError());
1956 SET_FILE_POS(pPhyDrive
->SizeInBytes
- 512 * 33);
1957 if (!WriteFile(hDrive
, pGptInfo
->PartTbl
, sizeof(pGptInfo
->PartTbl
), &dwSize
, NULL
))
1960 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize
, sizeof(VTOY_GPT_INFO
), GetLastError());
1965 if (!WriteFile(hDrive
, pGptInfo
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
))
1968 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize
, sizeof(VTOY_GPT_INFO
), GetLastError());
1972 Log("Write GPT Info OK ...");
1973 memcpy(&(pPhyDrive
->MBR
), &(pGptInfo
->MBR
), 512);
1977 if (!WriteFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
))
1980 Log("Write MBR Failed, dwSize:%u ErrCode:%u", dwSize
, GetLastError());
1983 Log("Write MBR OK ...");
1984 memcpy(&(pPhyDrive
->MBR
), &MBR
, 512);
1987 //Refresh Drive Layout
1988 DeviceIoControl(hDrive
, IOCTL_DISK_UPDATE_PROPERTIES
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
1992 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME
);
1996 Log("Mounting Ventoy Partition ....................... ");
2000 memset(DriveLetters
, 0, sizeof(DriveLetters
));
2001 GetLettersBelongPhyDrive(pPhyDrive
->PhyDrive
, DriveLetters
, sizeof(DriveLetters
));
2002 Log("Logical drive letter after write ventoy: <%s>", DriveLetters
);
2004 for (i
= 0; i
< sizeof(DriveLetters
) && DriveLetters
[i
]; i
++)
2006 DriveName
[0] = DriveLetters
[i
];
2007 if (IsVentoyLogicalDrive(DriveName
[0]))
2009 Log("%s is ventoy part2, delete mountpoint", DriveName
);
2010 DeleteVolumeMountPointA(DriveName
);
2014 Log("%s is ventoy part1, already mounted", DriveName
);
2015 MountDrive
= DriveName
[0];
2022 Log("need to mount ventoy part1...");
2024 if (0 == GetVentoyVolumeName(pPhyDrive
->PhyDrive
, Part1StartSector
, DriveLetters
, sizeof(DriveLetters
), FALSE
))
2026 DriveName
[0] = MountDrive
;
2027 bRet
= SetVolumeMountPointA(DriveName
, DriveLetters
);
2028 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName
, DriveLetters
, bRet
, GetLastError());
2037 Log("Failed to find ventoy volume");
2041 // close handle, or it will deny reformat
2042 Log("Close handle ...");
2043 CHECK_CLOSE_HANDLE(hDrive
);
2051 Log("No need to reformat for large FAT32");
2052 pPhyDrive
->VentoyFsClusterSize
= GetVolumeClusterSize(MountDrive
);
2054 else if (DefaultExFAT
)
2056 Log("No need to reformat for default exfat");
2057 pPhyDrive
->VentoyFsClusterSize
= GetVolumeClusterSize(MountDrive
);
2061 bRet
= DISK_FormatVolume(MountDrive
, GetVentoyFsType(), Part1SectorCount
* 512);
2062 for (i
= 0; bRet
== FALSE
&& i
< 2; i
++)
2064 Log("Wait and retry reformat ...");
2066 bRet
= DISK_FormatVolume(MountDrive
, GetVentoyFsType(), Part1SectorCount
* 512);
2071 Log("Reformat %C:\\ to %s SUCCESS", MountDrive
, GetVentoyFsName());
2072 pPhyDrive
->VentoyFsClusterSize
= GetVolumeClusterSize(MountDrive
);
2074 if ((GetVentoyFsType() != VTOY_FS_UDF
) && (pPhyDrive
->VentoyFsClusterSize
< 2048))
2076 for (i
= 0; i
< 10; i
++)
2078 Log("### Invalid cluster size %d ###", pPhyDrive
->VentoyFsClusterSize
);
2085 Log("Reformat %C:\\ to %s FAILED", MountDrive
, GetVentoyFsName());
2091 Log("Can not reformat %s to %s", DriveName
, GetVentoyFsName());
2096 Log("Format to exfat with built-in algorithm");
2098 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
2099 if (hDrive
== INVALID_HANDLE_VALUE
)
2101 Log("Failed to GetPhysicalHandle for write.");
2105 if (0 != FormatPart1exFAT(pPhyDrive
->SizeInBytes
))
2107 Log("FormatPart1exFAT SUCCESS.");
2111 Log("FormatPart1exFAT FAILED.");
2114 CHECK_CLOSE_HANDLE(hDrive
);
2122 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
2124 FindProcessOccupyDisk(hDrive
, pPhyDrive
);
2126 if (!VDS_IsLastAvaliable())
2128 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2129 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2130 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2131 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2132 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2135 CHECK_CLOSE_HANDLE(hDrive
);
2147 int PartitionResizeForVentoy(PHY_DRIVE_INFO
*pPhyDrive
)
2153 INT64 ReservedValue
;
2157 VTOY_GPT_INFO
*pGPT
;
2160 VTOY_GPT_HDR BackupHead
;
2161 HANDLE hDrive
= INVALID_HANDLE_VALUE
;
2162 GUID ZeroGuid
= { 0 };
2163 static GUID WindowsDataPartType
= { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
2164 static GUID EspPartType
= { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
2165 static GUID BiosGrubPartType
= { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
2167 Log("#####################################################");
2168 Log("PartitionResizeForVentoy PhyDrive%d <<%s %s %dGB>>",
2169 pPhyDrive
->PhyDrive
, pPhyDrive
->VendorId
, pPhyDrive
->ProductId
,
2170 GetHumanReadableGBSize(pPhyDrive
->SizeInBytes
));
2171 Log("#####################################################");
2173 pGPT
= &(pPhyDrive
->Gpt
);
2174 pMBR
= &(pPhyDrive
->Gpt
.MBR
);
2175 Log("Disksize:%llu Part2Start:%llu", pPhyDrive
->SizeInBytes
, pPhyDrive
->ResizePart2StartSector
* 512);
2177 if (pMBR
->PartTbl
[0].FsFlag
== 0xEE && memcmp(pGPT
->Head
.Signature
, "EFI PART", 8) == 0)
2186 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
2188 RecudeBytes
= VENTOY_EFI_PART_SIZE
;
2189 ReservedValue
= GetReservedSpaceInMB();
2190 if (ReservedValue
> 0)
2192 Log("Reduce add reserved space %lldMB", (LONGLONG
)ReservedValue
);
2193 RecudeBytes
+= (UINT64
)(ReservedValue
* SIZE_1MB
);
2197 if (pPhyDrive
->ResizeNoShrink
== FALSE
)
2199 Log("Need to shrink the volume");
2200 if (DISK_ShrinkVolume(pPhyDrive
->PhyDrive
, pPhyDrive
->ResizeVolumeGuid
, pPhyDrive
->Part1DriveLetter
, pPhyDrive
->ResizeOldPart1Size
, RecudeBytes
))
2202 Log("Shrink volume success, now check again");
2204 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
2205 if (hDrive
== INVALID_HANDLE_VALUE
)
2207 Log("Failed to GetPhysicalHandle for update.");
2211 //Refresh Drive Layout
2212 DeviceIoControl(hDrive
, IOCTL_DISK_UPDATE_PROPERTIES
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
2214 CHECK_CLOSE_HANDLE(hDrive
);
2217 if (PartResizePreCheck(NULL
) && pPhyDrive
->ResizeNoShrink
)
2219 Log("Recheck after Shrink volume success");
2220 Log("After shrink Disksize:%llu Part2Start:%llu", pPhyDrive
->SizeInBytes
, pPhyDrive
->ResizePart2StartSector
* 512);
2224 Log("Recheck after Shrink volume failed %u", pPhyDrive
->ResizeNoShrink
);
2230 Log("Shrink volume failed");
2236 //Now try write data
2237 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
2238 if (hDrive
== INVALID_HANDLE_VALUE
)
2240 Log("Failed to GetPhysicalHandle for update.");
2245 //Write partition 2 data
2246 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2
);
2247 if (0 != FormatPart2Fat(hDrive
, pPhyDrive
->ResizePart2StartSector
))
2249 Log("FormatPart2Fat failed.");
2253 //Write grub stage2 gap
2254 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG
);
2255 Log("Writing Boot Image ............................. ");
2256 if (WriteGrubStage1ToPhyDrive(hDrive
, PartStyle
) != 0)
2258 Log("WriteGrubStage1ToPhyDrive failed.");
2263 //Write partition table
2264 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE
);
2265 Log("Writing partition table ............................. ");
2267 VentoyGetLocalBootImg(&MBR
);
2268 CoCreateGuid(&Guid
);
2269 memcpy(MBR
.BootCode
+ 0x180, &Guid
, 16);
2270 memcpy(pMBR
->BootCode
, MBR
.BootCode
, 440);
2274 for (i
= 1; i
< 4; i
++)
2276 if (pMBR
->PartTbl
[i
].SectorCount
== 0)
2284 Log("Can not find MBR free partition table");
2288 for (j
= i
- 1; j
> 0; j
--)
2290 Log("Move MBR partition table %d --> %d", j
+ 1, j
+ 2);
2291 memcpy(pMBR
->PartTbl
+ (j
+ 1), pMBR
->PartTbl
+ j
, sizeof(PART_TABLE
));
2294 memset(pMBR
->PartTbl
+ 1, 0, sizeof(PART_TABLE
));
2295 VentoyFillMBRLocation(pPhyDrive
->SizeInBytes
, (UINT32
)pPhyDrive
->ResizePart2StartSector
, VENTOY_EFI_PART_SIZE
/ 512, pMBR
->PartTbl
+ 1);
2296 pMBR
->PartTbl
[0].Active
= 0x80; // bootable
2297 pMBR
->PartTbl
[1].Active
= 0x00;
2298 pMBR
->PartTbl
[1].FsFlag
= 0xEF; // EFI System Partition
2300 if (!WriteDataToPhyDisk(hDrive
, 0, pMBR
, 512))
2302 Log("Legacy BIOS write MBR failed");
2308 for (i
= 1; i
< 128; i
++)
2310 if (memcmp(&(pGPT
->PartTbl
[i
].PartGuid
), &ZeroGuid
, sizeof(GUID
)) == 0)
2318 Log("Can not find GPT free partition table");
2322 for (j
= i
- 1; j
> 0; j
--)
2324 Log("Move GPT partition table %d --> %d", j
+ 1, j
+ 2);
2325 memcpy(pGPT
->PartTbl
+ (j
+ 1), pGPT
->PartTbl
+ j
, sizeof(VTOY_GPT_PART_TBL
));
2329 pMBR
->BootCode
[92] = 0x22;
2331 // to fix windows issue
2332 memset(pGPT
->PartTbl
+ 1, 0, sizeof(VTOY_GPT_PART_TBL
));
2333 memcpy(&(pGPT
->PartTbl
[1].PartType
), &WindowsDataPartType
, sizeof(GUID
));
2334 CoCreateGuid(&(pGPT
->PartTbl
[1].PartGuid
));
2336 pGPT
->PartTbl
[1].StartLBA
= pGPT
->PartTbl
[0].LastLBA
+ 1;
2337 pGPT
->PartTbl
[1].LastLBA
= pGPT
->PartTbl
[1].StartLBA
+ VENTOY_EFI_PART_SIZE
/ 512 - 1;
2338 pGPT
->PartTbl
[1].Attr
= 0xC000000000000001ULL
;
2339 memcpy(pGPT
->PartTbl
[1].Name
, L
"VTOYEFI", 7 * 2);
2342 pGPT
->Head
.PartTblCrc
= VentoyCrc32(pGPT
->PartTbl
, sizeof(pGPT
->PartTbl
));
2344 pGPT
->Head
.Crc
= VentoyCrc32(&(pGPT
->Head
), pGPT
->Head
.Length
);
2346 Log("pGPT->Head.EfiStartLBA=%llu", (ULONGLONG
)pGPT
->Head
.EfiStartLBA
);
2347 Log("pGPT->Head.EfiBackupLBA=%llu", (ULONGLONG
)pGPT
->Head
.EfiBackupLBA
);
2349 VentoyFillBackupGptHead(pGPT
, &BackupHead
);
2350 if (!WriteDataToPhyDisk(hDrive
, pGPT
->Head
.EfiBackupLBA
* 512, &BackupHead
, 512))
2352 Log("UEFI write backup head failed");
2356 if (!WriteDataToPhyDisk(hDrive
, (pGPT
->Head
.EfiBackupLBA
- 32) * 512, pGPT
->PartTbl
, 512 * 32))
2358 Log("UEFI write backup partition table failed");
2362 if (!WriteDataToPhyDisk(hDrive
, 0, pGPT
, 512 * 34))
2364 Log("UEFI write MBR & Main partition table failed");
2371 //Refresh Drive Layout
2372 DeviceIoControl(hDrive
, IOCTL_DISK_UPDATE_PROPERTIES
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
2374 //We must close handle here, because it will block the refresh bellow
2375 CHECK_CLOSE_HANDLE(hDrive
);
2381 Log("### Ventoy non-destructive CLI installation successfully finished.");
2386 PhyDrive
= pPhyDrive
->PhyDrive
;
2388 Log("#### Now Refresh PhyDrive ####");
2389 Ventoy2DiskDestroy();
2392 pPhyDrive
= GetPhyDriveInfoByPhyDrive(PhyDrive
);
2395 if (pPhyDrive
->VentoyVersion
[0] == 0)
2397 Log("After process the Ventoy version is still invalid");
2401 Log("### Ventoy non-destructive installation successfully finished <%s>", pPhyDrive
->VentoyVersion
);
2405 Log("### Ventoy non-destructive installation successfully finished <not found>");
2408 InitComboxCtrl(g_DialogHwnd
, PhyDrive
);
2414 CHECK_CLOSE_HANDLE(hDrive
);
2419 static BOOL
DiskCheckWriteAccess(HANDLE hDrive
)
2425 LARGE_INTEGER liCurPosition
;
2426 LARGE_INTEGER liNewPosition
;
2428 liCurPosition
.QuadPart
= 2039 * 512;
2429 liNewPosition
.QuadPart
= 0;
2430 if (0 == SetFilePointerEx(hDrive
, liCurPosition
, &liNewPosition
, FILE_BEGIN
) ||
2431 liNewPosition
.QuadPart
!= liCurPosition
.QuadPart
)
2433 Log("SetFilePointer1 Failed %u", LASTERR
);
2439 ret
= ReadFile(hDrive
, Buffer
, 512, &dwSize
, NULL
);
2440 if ((!ret
) || (dwSize
!= 512))
2442 Log("Failed to read %d %u 0x%x", ret
, dwSize
, LASTERR
);
2447 liCurPosition
.QuadPart
= 2039 * 512;
2448 liNewPosition
.QuadPart
= 0;
2449 if (0 == SetFilePointerEx(hDrive
, liCurPosition
, &liNewPosition
, FILE_BEGIN
) ||
2450 liNewPosition
.QuadPart
!= liCurPosition
.QuadPart
)
2452 Log("SetFilePointer2 Failed %u", LASTERR
);
2457 ret
= WriteFile(hDrive
, Buffer
, 512, &dwSize
, NULL
);
2458 if ((!ret
) || dwSize
!= 512)
2460 Log("Failed to write %d %u %u", ret
, dwSize
, LASTERR
);
2471 static BOOL
BackupDataBeforeCleanDisk(int PhyDrive
, UINT64 DiskSize
, BYTE
**pBackup
)
2475 BOOL Return
= FALSE
;
2477 BYTE
*backup
= NULL
;
2479 HANDLE hDrive
= INVALID_HANDLE_VALUE
;
2480 LARGE_INTEGER liCurPosition
;
2481 LARGE_INTEGER liNewPosition
;
2482 VTOY_GPT_INFO
*pGPT
= NULL
;
2484 Log("BackupDataBeforeCleanDisk %d", PhyDrive
);
2486 // step1: check write access
2487 hDrive
= GetPhysicalHandle(PhyDrive
, TRUE
, TRUE
, FALSE
);
2488 if (hDrive
== INVALID_HANDLE_VALUE
)
2490 Log("Failed to GetPhysicalHandle for write.");
2494 if (DiskCheckWriteAccess(hDrive
))
2496 Log("DiskCheckWriteAccess success");
2497 CHECK_CLOSE_HANDLE(hDrive
);
2501 Log("DiskCheckWriteAccess failed");
2505 //step2 backup 4MB data
2506 backup
= malloc(SIZE_1MB
* 4);
2512 hDrive
= GetPhysicalHandle(PhyDrive
, FALSE
, FALSE
, FALSE
);
2513 if (hDrive
== INVALID_HANDLE_VALUE
)
2519 dwStatus
= SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
2526 ret
= ReadFile(hDrive
, backup
, SIZE_2MB
, &dwSize
, NULL
);
2527 if ((!ret
) || (dwSize
!= SIZE_2MB
))
2529 Log("Failed to read %d %u 0x%x", ret
, dwSize
, LASTERR
);
2533 pGPT
= (VTOY_GPT_INFO
*)backup
;
2534 offset
= pGPT
->Head
.EfiBackupLBA
* 512;
2535 if (offset
>= (DiskSize
- SIZE_2MB
) && offset
< DiskSize
)
2537 Log("EFI partition table check success");
2541 Log("Backup EFI LBA not in last 2MB range: %llu", pGPT
->Head
.EfiBackupLBA
);
2546 liCurPosition
.QuadPart
= DiskSize
- SIZE_2MB
;
2547 liNewPosition
.QuadPart
= 0;
2548 if (0 == SetFilePointerEx(hDrive
, liCurPosition
, &liNewPosition
, FILE_BEGIN
) ||
2549 liNewPosition
.QuadPart
!= liCurPosition
.QuadPart
)
2555 ret
= ReadFile(hDrive
, backup
+ SIZE_2MB
, SIZE_2MB
, &dwSize
, NULL
);
2556 if ((!ret
) || (dwSize
!= SIZE_2MB
))
2558 Log("Failed to read %d %u 0x%x", ret
, dwSize
, LASTERR
);
2563 backup
= NULL
; //For don't free later
2567 CHECK_CLOSE_HANDLE(hDrive
);
2575 static BOOL
WriteBackupDataToDisk(HANDLE hDrive
, UINT64 Offset
, BYTE
*Data
, DWORD Length
)
2579 LARGE_INTEGER liCurPosition
;
2580 LARGE_INTEGER liNewPosition
;
2582 Log("WriteBackupDataToDisk %llu %p %u", Offset
, Data
, Length
);
2584 liCurPosition
.QuadPart
= Offset
;
2585 liNewPosition
.QuadPart
= 0;
2586 if (0 == SetFilePointerEx(hDrive
, liCurPosition
, &liNewPosition
, FILE_BEGIN
) ||
2587 liNewPosition
.QuadPart
!= liCurPosition
.QuadPart
)
2592 ret
= WriteFile(hDrive
, Data
, Length
, &dwSize
, NULL
);
2593 if ((!ret
) || dwSize
!= Length
)
2595 Log("Failed to write %d %u %u", ret
, dwSize
, LASTERR
);
2599 Log("WriteBackupDataToDisk %llu %p %u success", Offset
, Data
, Length
);
2604 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int TryId
)
2609 BOOL ForceMBR
= FALSE
;
2610 BOOL Esp2Basic
= FALSE
;
2611 BOOL ChangeAttr
= FALSE
;
2612 BOOL CleanDisk
= FALSE
;
2613 BOOL DelEFI
= FALSE
;
2614 BOOL bWriteBack
= TRUE
;
2620 CHAR DriveName
[] = "?:\\";
2621 CHAR DriveLetters
[MAX_PATH
] = { 0 };
2622 CHAR BackBinFile
[MAX_PATH
];
2624 UINT64 ReservedMB
= 0;
2627 BYTE
*pBackup
= NULL
;
2628 VTOY_GPT_INFO
*pGptInfo
= NULL
;
2629 VTOY_GPT_INFO
*pGptBkup
= NULL
;
2630 UINT8 ReservedData
[4096];
2632 Log("#####################################################");
2633 Log("UpdateVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId
,
2634 pPhyDrive
->PartStyle
? "GPT" : "MBR", pPhyDrive
->PhyDrive
, pPhyDrive
->VendorId
, pPhyDrive
->ProductId
,
2635 GetHumanReadableGBSize(pPhyDrive
->SizeInBytes
));
2636 Log("#####################################################");
2638 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
2640 Log("Lock disk for umount ............................ ");
2642 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, FALSE
, FALSE
);
2643 if (hDrive
== INVALID_HANDLE_VALUE
)
2645 Log("Failed to open physical disk");
2649 if (pPhyDrive
->PartStyle
)
2651 pGptInfo
= malloc(2 * sizeof(VTOY_GPT_INFO
));
2657 memset(pGptInfo
, 0, 2 * sizeof(VTOY_GPT_INFO
));
2658 pGptBkup
= pGptInfo
+ 1;
2661 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
2662 ReadFile(hDrive
, pGptInfo
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
2663 memcpy(pGptBkup
, pGptInfo
, sizeof(VTOY_GPT_INFO
));
2665 //MBR will be used to compare with local boot image
2666 memcpy(&MBR
, &pGptInfo
->MBR
, sizeof(MBR_HEAD
));
2668 StartSector
= pGptInfo
->PartTbl
[1].StartLBA
;
2669 Log("GPT StartSector in PartTbl:%llu", (ULONGLONG
)StartSector
);
2671 ReservedMB
= (pPhyDrive
->SizeInBytes
/ 512 - (StartSector
+ VENTOY_EFI_PART_SIZE
/ 512) - 33) / 2048;
2672 Log("GPT Reserved Disk Space:%llu MB", (ULONGLONG
)ReservedMB
);
2677 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
2678 ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
2680 StartSector
= MBR
.PartTbl
[1].StartSectorId
;
2681 Log("MBR StartSector in PartTbl:%llu", (ULONGLONG
)StartSector
);
2683 ReservedMB
= (pPhyDrive
->SizeInBytes
/ 512 - (StartSector
+ VENTOY_EFI_PART_SIZE
/ 512)) / 2048;
2684 Log("MBR Reserved Disk Space:%llu MB", (ULONGLONG
)ReservedMB
);
2687 //Read Reserved Data
2688 SetFilePointer(hDrive
, 512 * 2040, NULL
, FILE_BEGIN
);
2689 ReadFile(hDrive
, ReservedData
, sizeof(ReservedData
), &dwSize
, NULL
);
2691 GetLettersBelongPhyDrive(pPhyDrive
->PhyDrive
, DriveLetters
, sizeof(DriveLetters
));
2693 if (DriveLetters
[0] == 0)
2695 Log("No drive letter was assigned...");
2699 // Unmount all mounted volumes that belong to this drive
2700 // Do it in reverse so that we always end on the first volume letter
2701 for (i
= (int)strlen(DriveLetters
); i
> 0; i
--)
2703 DriveName
[0] = DriveLetters
[i
- 1];
2704 if (IsVentoyLogicalDrive(DriveName
[0]))
2706 Log("%s is ventoy logical drive", DriveName
);
2707 bRet
= DeleteVolumeMountPointA(DriveName
);
2708 Log("Delete mountpoint %s ret:%u code:%u", DriveName
, bRet
, LASTERR
);
2714 // It kind of blows, but we have to relinquish access to the physical drive
2715 // for VDS to be able to delete the partitions that reside on it...
2716 DeviceIoControl(hDrive
, FSCTL_UNLOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
2717 CHECK_CLOSE_HANDLE(hDrive
);
2719 if (pPhyDrive
->PartStyle
== 1)
2721 Log("TryId=%d EFI GPT partition type is 0x%llx", TryId
, pPhyDrive
->Part2GPTAttr
);
2722 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART
);
2726 Log("Change GPT partition type to ESP");
2727 if (DISK_ChangeVtoyEFI2ESP(pPhyDrive
->PhyDrive
, StartSector
* 512ULL))
2733 else if (TryId
== 2)
2735 Log("Change GPT partition attribute");
2736 if (DISK_ChangeVtoyEFIAttr(pPhyDrive
->PhyDrive
, StartSector
* 512ULL, 0x8000000000000001))
2742 else if (TryId
== 3)
2744 DISK_DeleteVtoyEFIPartition(pPhyDrive
->PhyDrive
, StartSector
* 512ULL);
2747 else if (TryId
== 4)
2749 Log("Clean disk GPT partition table");
2750 if (BackupDataBeforeCleanDisk(pPhyDrive
->PhyDrive
, pPhyDrive
->SizeInBytes
, &pBackup
))
2752 sprintf_s(BackBinFile
, sizeof(BackBinFile
), ".\\ventoy\\phydrive%d_%u_%d.bin",
2753 pPhyDrive
->PhyDrive
, GetCurrentProcessId(), g_backup_bin_index
++);
2754 SaveBufToFile(BackBinFile
, pBackup
, 4 * SIZE_1MB
);
2755 Log("Save backup data to %s", BackBinFile
);
2757 Log("Success to backup data before clean");
2759 DISK_CleanDisk(pPhyDrive
->PhyDrive
);
2764 Log("Failed to backup data before clean");
2769 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE
);
2771 Log("Lock disk for update ............................ ");
2772 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
2773 if (hDrive
== INVALID_HANDLE_VALUE
)
2775 Log("Failed to GetPhysicalHandle for write.");
2780 PROGRESS_BAR_SET_POS(PT_LOCK_VOLUME
);
2782 Log("Lock volume for update .......................... ");
2783 hVolume
= INVALID_HANDLE_VALUE
;
2785 //If we change VTOYEFI to ESP, it can not have s volume name, so don't try to get it.
2788 //writeback the last 2MB
2789 if (!WriteBackupDataToDisk(hDrive
, pPhyDrive
->SizeInBytes
- SIZE_2MB
, pBackup
+ SIZE_2MB
, SIZE_2MB
))
2794 //write the first 2MB except parttable
2795 if (!WriteBackupDataToDisk(hDrive
, 34 * 512, pBackup
+ 34 * 512, SIZE_2MB
- 34 * 512))
2800 Status
= ERROR_NOT_FOUND
;
2804 Status
= ERROR_NOT_FOUND
;
2808 Status
= ERROR_NOT_FOUND
;
2812 for (i
= 0; i
< MaxRetry
; i
++)
2814 Status
= GetVentoyVolumeName(pPhyDrive
->PhyDrive
, StartSector
, DriveLetters
, sizeof(DriveLetters
), TRUE
);
2815 if (ERROR_SUCCESS
== Status
)
2821 Log("==== Volume not found, wait and retry %d... ====", i
);
2827 if (ERROR_SUCCESS
== Status
)
2829 Log("Now lock and dismount volume <%s>", DriveLetters
);
2831 for (i
= 0; i
< MaxRetry
; i
++)
2833 hVolume
= CreateFileA(DriveLetters
,
2834 GENERIC_READ
| GENERIC_WRITE
,
2838 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
,
2841 if (hVolume
== INVALID_HANDLE_VALUE
)
2843 Log("Failed to create file volume, errcode:%u, wait and retry ...", LASTERR
);
2852 if (hVolume
== INVALID_HANDLE_VALUE
)
2854 Log("Failed to create file volume, errcode:%u", LASTERR
);
2858 bRet
= DeviceIoControl(hVolume
, FSCTL_LOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
2859 Log("FSCTL_LOCK_VOLUME bRet:%u code:%u", bRet
, LASTERR
);
2861 bRet
= DeviceIoControl(hVolume
, FSCTL_DISMOUNT_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
2862 Log("FSCTL_DISMOUNT_VOLUME bRet:%u code:%u", bRet
, LASTERR
);
2865 else if (ERROR_NOT_FOUND
== Status
)
2867 Log("Volume not found, maybe not supported");
2875 bRet
= TryWritePart2(hDrive
, StartSector
);
2876 if (FALSE
== bRet
&& Esp2Basic
)
2878 Log("TryWritePart2 agagin ...");
2880 bRet
= TryWritePart2(hDrive
, StartSector
);
2885 if (pPhyDrive
->PartStyle
== 0)
2887 if (DiskCheckWriteAccess(hDrive
))
2889 Log("MBR DiskCheckWriteAccess success");
2893 Log("Try write failed, now delete partition 2 for MBR...");
2894 CHECK_CLOSE_HANDLE(hDrive
);
2896 Log("Now delete partition 2...");
2897 DISK_DeleteVtoyEFIPartition(pPhyDrive
->PhyDrive
, StartSector
* 512ULL);
2899 hDrive
= GetPhysicalHandle(pPhyDrive
->PhyDrive
, TRUE
, TRUE
, FALSE
);
2900 if (hDrive
== INVALID_HANDLE_VALUE
)
2902 Log("Failed to GetPhysicalHandle for write.");
2909 Log("MBR DiskCheckWriteAccess failed");
2914 Log("TryWritePart2 failed ....");
2920 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2
);
2922 Log("Write Ventoy to disk ............................ ");
2923 if (0 != FormatPart2Fat(hDrive
, StartSector
))
2929 if (hVolume
!= INVALID_HANDLE_VALUE
)
2931 bRet
= DeviceIoControl(hVolume
, FSCTL_UNLOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
2932 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet
, LASTERR
);
2933 CHECK_CLOSE_HANDLE(hVolume
);
2936 Log("Updating Boot Image ............................. ");
2937 if (WriteGrubStage1ToPhyDrive(hDrive
, pPhyDrive
->PartStyle
) != 0)
2943 //write reserved data
2944 SetFilePointer(hDrive
, 512 * 2040, NULL
, FILE_BEGIN
);
2945 bRet
= WriteFile(hDrive
, ReservedData
, sizeof(ReservedData
), &dwSize
, NULL
);
2946 Log("Write resv data ret:%u dwSize:%u Error:%u", bRet
, dwSize
, LASTERR
);
2949 VentoyGetLocalBootImg(&BootImg
);
2952 memcpy(BootImg
.BootCode
+ 0x180, MBR
.BootCode
+ 0x180, 16);
2953 if (pPhyDrive
->PartStyle
)
2955 BootImg
.BootCode
[92] = 0x22;
2958 if (ForceMBR
== FALSE
&& memcmp(BootImg
.BootCode
, MBR
.BootCode
, 440) == 0)
2960 Log("Boot image has no difference, no need to write.");
2964 Log("Boot image need to write %u.", ForceMBR
);
2966 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
2968 memcpy(MBR
.BootCode
, BootImg
.BootCode
, 440);
2969 bRet
= WriteFile(hDrive
, &MBR
, 512, &dwSize
, NULL
);
2970 Log("Write Boot Image ret:%u dwSize:%u Error:%u", bRet
, dwSize
, LASTERR
);
2973 if (pPhyDrive
->PartStyle
== 0)
2975 if (0x00 == MBR
.PartTbl
[0].Active
&& 0x80 == MBR
.PartTbl
[1].Active
)
2977 Log("Need to chage 1st partition active and 2nd partition inactive.");
2979 MBR
.PartTbl
[0].Active
= 0x80;
2980 MBR
.PartTbl
[1].Active
= 0x00;
2982 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
2983 bRet
= WriteFile(hDrive
, &MBR
, 512, &dwSize
, NULL
);
2984 Log("Write NEW MBR ret:%u dwSize:%u Error:%u", bRet
, dwSize
, LASTERR
);
2990 if (!WriteBackupDataToDisk(hDrive
, 0, pBackup
, 34 * 512))
2999 Log("Write backup data success, now delete %s", BackBinFile
);
3000 DeleteFileA(BackBinFile
);
3004 Log("Write backup data failed");
3011 VTOY_GPT_HDR BackupHdr
;
3013 VentoyFillBackupGptHead(pGptBkup
, &BackupHdr
);
3014 if (!WriteBackupDataToDisk(hDrive
, 512 * pGptBkup
->Head
.EfiBackupLBA
, (BYTE
*)(&BackupHdr
), 512))
3019 if (!WriteBackupDataToDisk(hDrive
, 512 * (pGptBkup
->Head
.EfiBackupLBA
- 32), (BYTE
*)(pGptBkup
->PartTbl
), 32 * 512))
3024 if (!WriteBackupDataToDisk(hDrive
, 512, (BYTE
*)pGptBkup
+ 512, 33 * 512))
3031 Log("Write backup partition table success");
3035 Log("Write backup partition table failed");
3041 //Refresh Drive Layout
3042 DeviceIoControl(hDrive
, IOCTL_DISK_UPDATE_PROPERTIES
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
3046 if (hVolume
!= INVALID_HANDLE_VALUE
)
3048 bRet
= DeviceIoControl(hVolume
, FSCTL_UNLOCK_VOLUME
, NULL
, 0, NULL
, 0, &dwSize
, NULL
);
3049 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet
, LASTERR
);
3050 CHECK_CLOSE_HANDLE(hVolume
);
3059 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN
);
3060 FindProcessOccupyDisk(hDrive
, pPhyDrive
);
3063 CHECK_CLOSE_HANDLE(hDrive
);
3067 Log("Recover GPT partition type to basic");
3068 DISK_ChangeVtoyEFI2Basic(pPhyDrive
->PhyDrive
, StartSector
* 512);
3071 if (pPhyDrive
->PartStyle
== 1)
3073 if (ChangeAttr
|| ((pPhyDrive
->Part2GPTAttr
>> 56) != 0xC0))
3075 Log("Change EFI partition attr %u <0x%llx> to <0x%llx>", ChangeAttr
, pPhyDrive
->Part2GPTAttr
, 0xC000000000000001ULL
);
3076 if (DISK_ChangeVtoyEFIAttr(pPhyDrive
->PhyDrive
, StartSector
* 512ULL, 0xC000000000000001ULL
))
3078 Log("Change EFI partition attr success");
3079 pPhyDrive
->Part2GPTAttr
= 0xC000000000000001ULL
;
3083 Log("Change EFI partition attr failed");