1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "Ventoy2Disk.h"
26 PHY_DRIVE_INFO
*g_PhyDriveList
= NULL
;
27 DWORD g_PhyDriveCount
= 0;
28 static int g_FilterRemovable
= 0;
30 int g_ForceOperation
= 1;
33 int ParseCmdLineOption(LPSTR lpCmdLine
)
36 char cfgfile
[MAX_PATH
];
38 if (lpCmdLine
&& lpCmdLine
[0])
40 Log("CmdLine:<%s>", lpCmdLine
);
43 for (i
= 0; i
< __argc
; i
++)
45 if (strncmp(__argv
[i
], "-U", 2) == 0 ||
46 strncmp(__argv
[i
], "-u", 2) == 0)
50 else if (strncmp(__argv
[i
], "-F", 2) == 0)
56 GetCurrentDirectoryA(sizeof(cfgfile
), cfgfile
);
57 strcat_s(cfgfile
, sizeof(cfgfile
), "\\Ventoy2Disk.ini");
59 if (0 == GetPrivateProfileIntA("Filter", "USB", 1, cfgfile
))
64 if (1 == GetPrivateProfileIntA("Operation", "Force", 0, cfgfile
))
69 Log("Control Flag: %d %d %d", g_FilterRemovable
, g_FilterUSB
, g_ForceOperation
);
74 static BOOL
IsVentoyPhyDrive(int PhyDrive
, UINT64 SizeBytes
, MBR_HEAD
*pMBR
, UINT64
*Part2StartSector
)
81 UINT32 PartStartSector
;
82 UINT32 PartSectorCount
;
83 CHAR PhyDrivePath
[128];
84 VTOY_GPT_INFO
*pGpt
= NULL
;
86 safe_sprintf(PhyDrivePath
, "\\\\.\\PhysicalDrive%d", PhyDrive
);
87 hDrive
= CreateFileA(PhyDrivePath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
88 Log("Create file Handle:%p %s status:%u", hDrive
, PhyDrivePath
, LASTERR
);
90 if (hDrive
== INVALID_HANDLE_VALUE
)
95 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
96 Log("Read MBR Ret:%u Size:%u code:%u", bRet
, dwSize
, LASTERR
);
98 if ((!bRet
) || (dwSize
!= sizeof(MBR
)))
100 CHECK_CLOSE_HANDLE(hDrive
);
104 if (MBR
.Byte55
!= 0x55 || MBR
.ByteAA
!= 0xAA)
106 Log("Byte55 ByteAA not match 0x%x 0x%x", MBR
.Byte55
, MBR
.ByteAA
);
107 CHECK_CLOSE_HANDLE(hDrive
);
111 for (i
= 0; i
< 4; i
++)
113 Log("=========== Partition Table %d ============", i
+ 1);
114 Log("PartTbl.Active = 0x%x", MBR
.PartTbl
[i
].Active
);
115 Log("PartTbl.FsFlag = 0x%x", MBR
.PartTbl
[i
].FsFlag
);
116 Log("PartTbl.StartSectorId = %u", MBR
.PartTbl
[i
].StartSectorId
);
117 Log("PartTbl.SectorCount = %u", MBR
.PartTbl
[i
].SectorCount
);
118 Log("PartTbl.StartHead = %u", MBR
.PartTbl
[i
].StartHead
);
119 Log("PartTbl.StartSector = %u", MBR
.PartTbl
[i
].StartSector
);
120 Log("PartTbl.StartCylinder = %u", MBR
.PartTbl
[i
].StartCylinder
);
121 Log("PartTbl.EndHead = %u", MBR
.PartTbl
[i
].EndHead
);
122 Log("PartTbl.EndSector = %u", MBR
.PartTbl
[i
].EndSector
);
123 Log("PartTbl.EndCylinder = %u", MBR
.PartTbl
[i
].EndCylinder
);
126 if (MBR
.PartTbl
[0].FsFlag
== 0xEE)
128 pGpt
= malloc(sizeof(VTOY_GPT_INFO
));
131 CHECK_CLOSE_HANDLE(hDrive
);
135 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
136 bRet
= ReadFile(hDrive
, pGpt
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
137 CHECK_CLOSE_HANDLE(hDrive
);
138 if ((!bRet
) || (dwSize
!= sizeof(VTOY_GPT_INFO
)))
140 Log("Failed to read gpt info %d %u %d", bRet
, dwSize
, LASTERR
);
144 if (memcmp(pGpt
->Head
.Signature
, "EFI PART", 8))
146 Log("Invalid GPT signature");
150 if (memcmp(pGpt
->PartTbl
[1].Name
, L
"VTOYEFI", 7 * 2))
152 Log("Invalid ventoy efi part name");
156 if (pGpt
->PartTbl
[0].StartLBA
!= 2048)
158 Log("Part1 not match %llu", pGpt
->PartTbl
[0].StartLBA
);
162 PartSectorCount
= VENTOY_EFI_PART_SIZE
/ 512;
164 if (pGpt
->PartTbl
[1].StartLBA
!= pGpt
->PartTbl
[0].LastLBA
+ 1 ||
165 (UINT32
)(pGpt
->PartTbl
[1].LastLBA
+ 1 - pGpt
->PartTbl
[1].StartLBA
) != PartSectorCount
)
167 Log("Part2 not match [%llu %llu] [%llu %llu]",
168 pGpt
->PartTbl
[0].StartLBA
, pGpt
->PartTbl
[0].LastLBA
,
169 pGpt
->PartTbl
[1].StartLBA
, pGpt
->PartTbl
[1].LastLBA
);
173 *Part2StartSector
= pGpt
->PartTbl
[1].StartLBA
;
175 memcpy(pMBR
, &(pGpt
->MBR
), sizeof(MBR_HEAD
));
179 CHECK_CLOSE_HANDLE(hDrive
);
181 if (MBR
.PartTbl
[0].StartSectorId
!= 2048)
183 Log("Part1 not match %u", MBR
.PartTbl
[0].StartSectorId
);
187 PartStartSector
= MBR
.PartTbl
[0].StartSectorId
+ MBR
.PartTbl
[0].SectorCount
;
188 PartSectorCount
= VENTOY_EFI_PART_SIZE
/ 512;
190 if (MBR
.PartTbl
[1].StartSectorId
!= PartStartSector
||
191 MBR
.PartTbl
[1].SectorCount
!= PartSectorCount
)
193 Log("Part2 not match [0x%x 0x%x] [%u %u] [%u %u]",
194 MBR
.PartTbl
[1].FsFlag
, 0xEF,
195 MBR
.PartTbl
[1].StartSectorId
, PartStartSector
,
196 MBR
.PartTbl
[1].SectorCount
, PartSectorCount
);
200 if (MBR
.PartTbl
[0].Active
!= 0x80 && MBR
.PartTbl
[1].Active
!= 0x80)
202 Log("Part1 and Part2 are both NOT active 0x%x 0x%x", MBR
.PartTbl
[0].Active
, MBR
.PartTbl
[1].Active
);
203 if (MBR
.PartTbl
[2].Active
!= 0x80 && MBR
.PartTbl
[3].Active
!= 0x80)
205 Log("Part3 and Part4 are both NOT active 0x%x 0x%x", MBR
.PartTbl
[2].Active
, MBR
.PartTbl
[3].Active
);
210 *Part2StartSector
= MBR
.PartTbl
[1].StartSectorId
;
212 memcpy(pMBR
, &MBR
, sizeof(MBR_HEAD
));
215 Log("PhysicalDrive%d is ventoy disk", PhyDrive
);
220 static int FilterPhysicalDrive(PHY_DRIVE_INFO
*pDriveList
, DWORD DriveCount
)
228 UINT64 Part2StartSector
= 0;
229 PHY_DRIVE_INFO
*CurDrive
;
231 int LogLetter
[VENTOY_MAX_PHY_DRIVE
];
232 int PhyDriveId
[VENTOY_MAX_PHY_DRIVE
];
234 for (LogDrive
= GetLogicalDrives(); LogDrive
> 0; LogDrive
>>= 1)
238 LogLetter
[LetterCount
] = Letter
;
239 PhyDriveId
[LetterCount
] = GetPhyDriveByLogicalDrive(Letter
);
241 Log("Logical Drive:%C ===> PhyDrive:%d", LogLetter
[LetterCount
], PhyDriveId
[LetterCount
]);
248 for (i
= 0; i
< DriveCount
; i
++)
250 CurDrive
= pDriveList
+ i
;
253 memset(CurDrive
->DriveLetters
, 0, sizeof(CurDrive
->DriveLetters
));
255 if (g_FilterRemovable
&& (!CurDrive
->RemovableMedia
))
257 Log("<%s %s> is filtered for not removable.", CurDrive
->VendorId
, CurDrive
->ProductId
);
261 if (g_FilterUSB
&& CurDrive
->BusType
!= BusTypeUsb
)
263 Log("<%s %s> is filtered for not USB type.", CurDrive
->VendorId
, CurDrive
->ProductId
);
269 for (Count
= 0, Letter
= 0; Letter
< LetterCount
; Letter
++)
271 if (PhyDriveId
[Letter
] == CurDrive
->PhyDrive
)
273 if (Count
+ 1 < sizeof(CurDrive
->DriveLetters
) / sizeof(CHAR
))
275 CurDrive
->DriveLetters
[Count
] = LogLetter
[Letter
];
281 if (IsVentoyPhyDrive(CurDrive
->PhyDrive
, CurDrive
->SizeInBytes
, &MBR
, &Part2StartSector
))
283 memcpy(&(CurDrive
->MBR
), &MBR
, sizeof(MBR
));
284 CurDrive
->PartStyle
= (MBR
.PartTbl
[0].FsFlag
== 0xEE) ? 1 : 0;
285 GetVentoyVerInPhyDrive(CurDrive
, Part2StartSector
, CurDrive
->VentoyVersion
, sizeof(CurDrive
->VentoyVersion
), &(CurDrive
->SecureBootSupport
));
286 Log("PhyDrive %d is Ventoy Disk ver:%s SecureBoot:%u", CurDrive
->PhyDrive
, CurDrive
->VentoyVersion
, CurDrive
->SecureBootSupport
);
288 if (CurDrive
->VentoyVersion
[0] == 0)
290 CurDrive
->VentoyVersion
[0] = '?';
291 Log("Unknown Ventoy Version");
297 for (i
= 0; i
< DriveCount
; i
++)
299 CurDrive
= pDriveList
+ i
;
300 if (CurDrive
->Id
< 0)
302 CurDrive
->PhyDrive
= 0x00FFFFFF;
309 PHY_DRIVE_INFO
* GetPhyDriveInfoById(int Id
)
312 for (i
= 0; i
< g_PhyDriveCount
; i
++)
314 if (g_PhyDriveList
[i
].Id
>= 0 && g_PhyDriveList
[i
].Id
== Id
)
316 return g_PhyDriveList
+ i
;
323 int SortPhysicalDrive(PHY_DRIVE_INFO
*pDriveList
, DWORD DriveCount
)
327 PHY_DRIVE_INFO TmpDriveInfo
;
329 for (i
= 0; i
< DriveCount
; i
++)
331 for (j
= i
+ 1; j
< DriveCount
; j
++)
335 if (pDriveList
[i
].BusType
== BusTypeUsb
&& pDriveList
[j
].BusType
== BusTypeUsb
)
337 if (pDriveList
[i
].RemovableMedia
== FALSE
&& pDriveList
[j
].RemovableMedia
== TRUE
)
342 else if (pDriveList
[j
].BusType
== BusTypeUsb
)
348 if (pDriveList
[j
].PhyDrive
< pDriveList
[i
].PhyDrive
)
356 memcpy(&TmpDriveInfo
, pDriveList
+ i
, sizeof(PHY_DRIVE_INFO
));
357 memcpy(pDriveList
+ i
, pDriveList
+ j
, sizeof(PHY_DRIVE_INFO
));
358 memcpy(pDriveList
+ j
, &TmpDriveInfo
, sizeof(PHY_DRIVE_INFO
));
366 int Ventoy2DiskInit(void)
368 Log("\n===================== Enum All PhyDrives =====================");
369 g_PhyDriveList
= (PHY_DRIVE_INFO
*)malloc(sizeof(PHY_DRIVE_INFO
)* VENTOY_MAX_PHY_DRIVE
);
370 if (NULL
== g_PhyDriveList
)
372 Log("Failed to alloc phy drive memory");
375 memset(g_PhyDriveList
, 0, sizeof(PHY_DRIVE_INFO
)* VENTOY_MAX_PHY_DRIVE
);
377 GetAllPhysicalDriveInfo(g_PhyDriveList
, &g_PhyDriveCount
);
379 SortPhysicalDrive(g_PhyDriveList
, g_PhyDriveCount
);
381 FilterPhysicalDrive(g_PhyDriveList
, g_PhyDriveCount
);
386 int Ventoy2DiskDestroy(void)
388 free(g_PhyDriveList
);