]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/Ventoy2Disk.c
5ba9e71ae7f1adbb0fe1d8f72bc942462e4aa29b
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / Ventoy2Disk.c
1 /******************************************************************************
2 * Ventoy2Disk.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include <Windows.h>
22 #include "resource.h"
23 #include "Language.h"
24 #include "Ventoy2Disk.h"
25
26 PHY_DRIVE_INFO *g_PhyDriveList = NULL;
27 DWORD g_PhyDriveCount = 0;
28 static int g_FilterRemovable = 0;
29 int g_FilterUSB = 1;
30 int g_ForceOperation = 1;
31 int g_WriteImage = 0;
32
33 int ParseCmdLineOption(LPSTR lpCmdLine)
34 {
35 int i;
36 char cfgfile[MAX_PATH];
37
38 if (lpCmdLine && lpCmdLine[0])
39 {
40 Log("CmdLine:<%s>", lpCmdLine);
41 }
42
43 for (i = 0; i < __argc; i++)
44 {
45 if (strncmp(__argv[i], "-U", 2) == 0 ||
46 strncmp(__argv[i], "-u", 2) == 0)
47 {
48 g_FilterUSB = 0;
49 }
50 else if (strncmp(__argv[i], "-F", 2) == 0)
51 {
52 g_ForceOperation = 1;
53 }
54 }
55
56 GetCurrentDirectoryA(sizeof(cfgfile), cfgfile);
57 strcat_s(cfgfile, sizeof(cfgfile), "\\Ventoy2Disk.ini");
58
59 if (0 == GetPrivateProfileIntA("Filter", "USB", 1, cfgfile))
60 {
61 g_FilterUSB = 0;
62 }
63
64 if (1 == GetPrivateProfileIntA("Operation", "Force", 0, cfgfile))
65 {
66 g_ForceOperation = 1;
67 }
68
69 Log("Control Flag: %d %d %d", g_FilterRemovable, g_FilterUSB, g_ForceOperation);
70
71 return 0;
72 }
73
74 static BOOL IsVentoyPhyDrive(int PhyDrive, UINT64 SizeBytes, MBR_HEAD *pMBR, UINT64 *Part2StartSector, UINT64 *GptPart2Attr)
75 {
76 int i;
77 BOOL bRet;
78 DWORD dwSize;
79 HANDLE hDrive;
80 MBR_HEAD MBR;
81 UINT32 PartStartSector;
82 UINT32 PartSectorCount;
83 CHAR PhyDrivePath[128];
84 VTOY_GPT_INFO *pGpt = NULL;
85
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);
89
90 if (hDrive == INVALID_HANDLE_VALUE)
91 {
92 return FALSE;
93 }
94
95 bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
96 Log("Read MBR Ret:%u Size:%u code:%u", bRet, dwSize, LASTERR);
97
98 if ((!bRet) || (dwSize != sizeof(MBR)))
99 {
100 CHECK_CLOSE_HANDLE(hDrive);
101 return FALSE;
102 }
103
104 if (MBR.Byte55 != 0x55 || MBR.ByteAA != 0xAA)
105 {
106 Log("Byte55 ByteAA not match 0x%x 0x%x", MBR.Byte55, MBR.ByteAA);
107 CHECK_CLOSE_HANDLE(hDrive);
108 return FALSE;
109 }
110
111 for (i = 0; i < 4; i++)
112 {
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);
124 }
125
126 if (MBR.PartTbl[0].FsFlag == 0xEE)
127 {
128 pGpt = malloc(sizeof(VTOY_GPT_INFO));
129 if (!pGpt)
130 {
131 CHECK_CLOSE_HANDLE(hDrive);
132 return FALSE;
133 }
134
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)))
139 {
140 Log("Failed to read gpt info %d %u %d", bRet, dwSize, LASTERR);
141 return FALSE;
142 }
143
144 if (memcmp(pGpt->Head.Signature, "EFI PART", 8))
145 {
146 Log("Invalid GPT signature");
147 return FALSE;
148 }
149
150 if (memcmp(pGpt->PartTbl[1].Name, L"VTOYEFI", 7 * 2))
151 {
152 if (pGpt->PartTbl[1].Name[0])
153 {
154 Log("Invalid ventoy efi part name <%S>", pGpt->PartTbl[1].Name);
155 }
156 else
157 {
158 Log("Invalid ventoy efi part name <null>");
159 }
160
161 return FALSE;
162 }
163
164 if (pGpt->PartTbl[0].StartLBA != 2048)
165 {
166 Log("Part1 not match %llu", pGpt->PartTbl[0].StartLBA);
167 return FALSE;
168 }
169
170 PartSectorCount = VENTOY_EFI_PART_SIZE / 512;
171
172 if (pGpt->PartTbl[1].StartLBA != pGpt->PartTbl[0].LastLBA + 1 ||
173 (UINT32)(pGpt->PartTbl[1].LastLBA + 1 - pGpt->PartTbl[1].StartLBA) != PartSectorCount)
174 {
175 Log("Part2 not match [%llu %llu] [%llu %llu]",
176 pGpt->PartTbl[0].StartLBA, pGpt->PartTbl[0].LastLBA,
177 pGpt->PartTbl[1].StartLBA, pGpt->PartTbl[1].LastLBA);
178 return FALSE;
179 }
180
181 *GptPart2Attr = pGpt->PartTbl[1].Attr;
182 *Part2StartSector = pGpt->PartTbl[1].StartLBA;
183
184 memcpy(pMBR, &(pGpt->MBR), sizeof(MBR_HEAD));
185 }
186 else
187 {
188 CHECK_CLOSE_HANDLE(hDrive);
189
190 if (MBR.PartTbl[0].StartSectorId != 2048)
191 {
192 Log("Part1 not match %u", MBR.PartTbl[0].StartSectorId);
193 return FALSE;
194 }
195
196 PartStartSector = MBR.PartTbl[0].StartSectorId + MBR.PartTbl[0].SectorCount;
197 PartSectorCount = VENTOY_EFI_PART_SIZE / 512;
198
199 if (MBR.PartTbl[1].StartSectorId != PartStartSector ||
200 MBR.PartTbl[1].SectorCount != PartSectorCount)
201 {
202 Log("Part2 not match [0x%x 0x%x] [%u %u] [%u %u]",
203 MBR.PartTbl[1].FsFlag, 0xEF,
204 MBR.PartTbl[1].StartSectorId, PartStartSector,
205 MBR.PartTbl[1].SectorCount, PartSectorCount);
206 return FALSE;
207 }
208
209 if (MBR.PartTbl[0].Active != 0x80 && MBR.PartTbl[1].Active != 0x80)
210 {
211 Log("Part1 and Part2 are both NOT active 0x%x 0x%x", MBR.PartTbl[0].Active, MBR.PartTbl[1].Active);
212 if (MBR.PartTbl[2].Active != 0x80 && MBR.PartTbl[3].Active != 0x80)
213 {
214 Log("Part3 and Part4 are both NOT active 0x%x 0x%x", MBR.PartTbl[2].Active, MBR.PartTbl[3].Active);
215 //return FALSE;
216 }
217 }
218
219 *Part2StartSector = MBR.PartTbl[1].StartSectorId;
220
221 memcpy(pMBR, &MBR, sizeof(MBR_HEAD));
222 }
223
224 Log("PhysicalDrive%d is ventoy disk", PhyDrive);
225 return TRUE;
226 }
227
228
229 static int FilterPhysicalDrive(PHY_DRIVE_INFO *pDriveList, DWORD DriveCount)
230 {
231 DWORD i;
232 DWORD LogDrive;
233 int Count = 0;
234 int Letter = 'A';
235 int Id = 0;
236 int LetterCount = 0;
237 UINT64 Part2GPTAttr = 0;
238 UINT64 Part2StartSector = 0;
239 PHY_DRIVE_INFO *CurDrive;
240 MBR_HEAD MBR;
241 int LogLetter[VENTOY_MAX_PHY_DRIVE];
242 int PhyDriveId[VENTOY_MAX_PHY_DRIVE];
243
244 for (LogDrive = GetLogicalDrives(); LogDrive > 0; LogDrive >>= 1)
245 {
246 if (LogDrive & 0x01)
247 {
248 LogLetter[LetterCount] = Letter;
249 PhyDriveId[LetterCount] = GetPhyDriveByLogicalDrive(Letter);
250
251 Log("Logical Drive:%C ===> PhyDrive:%d", LogLetter[LetterCount], PhyDriveId[LetterCount]);
252 LetterCount++;
253 }
254
255 Letter++;
256 }
257
258 for (i = 0; i < DriveCount; i++)
259 {
260 Part2GPTAttr = 0;
261 CurDrive = pDriveList + i;
262
263 CurDrive->Id = -1;
264 memset(CurDrive->DriveLetters, 0, sizeof(CurDrive->DriveLetters));
265
266 if (g_FilterRemovable && (!CurDrive->RemovableMedia))
267 {
268 Log("<%s %s> is filtered for not removable.", CurDrive->VendorId, CurDrive->ProductId);
269 continue;
270 }
271
272 if (g_FilterUSB && CurDrive->BusType != BusTypeUsb)
273 {
274 Log("<%s %s> is filtered for not USB type.", CurDrive->VendorId, CurDrive->ProductId);
275 continue;
276 }
277
278 CurDrive->Id = Id++;
279
280 for (Count = 0, Letter = 0; Letter < LetterCount; Letter++)
281 {
282 if (PhyDriveId[Letter] == CurDrive->PhyDrive)
283 {
284 if (Count + 1 < sizeof(CurDrive->DriveLetters) / sizeof(CHAR))
285 {
286 CurDrive->DriveLetters[Count] = LogLetter[Letter];
287 }
288 Count++;
289 }
290 }
291
292 if (IsVentoyPhyDrive(CurDrive->PhyDrive, CurDrive->SizeInBytes, &MBR, &Part2StartSector, &Part2GPTAttr))
293 {
294 memcpy(&(CurDrive->MBR), &MBR, sizeof(MBR));
295 CurDrive->PartStyle = (MBR.PartTbl[0].FsFlag == 0xEE) ? 1 : 0;
296 CurDrive->Part2GPTAttr = Part2GPTAttr;
297 GetVentoyVerInPhyDrive(CurDrive, Part2StartSector, CurDrive->VentoyVersion, sizeof(CurDrive->VentoyVersion), &(CurDrive->SecureBootSupport));
298 Log("PhyDrive %d is Ventoy Disk ver:%s SecureBoot:%u", CurDrive->PhyDrive, CurDrive->VentoyVersion, CurDrive->SecureBootSupport);
299
300 if (CurDrive->VentoyVersion[0] == 0)
301 {
302 CurDrive->VentoyVersion[0] = '?';
303 Log("Unknown Ventoy Version");
304 }
305 }
306 }
307
308 // for safe
309 for (i = 0; i < DriveCount; i++)
310 {
311 CurDrive = pDriveList + i;
312 if (CurDrive->Id < 0)
313 {
314 CurDrive->PhyDrive = 0x00FFFFFF;
315 }
316 }
317
318 return Id;
319 }
320
321 PHY_DRIVE_INFO * GetPhyDriveInfoById(int Id)
322 {
323 DWORD i;
324 for (i = 0; i < g_PhyDriveCount; i++)
325 {
326 if (g_PhyDriveList[i].Id >= 0 && g_PhyDriveList[i].Id == Id)
327 {
328 return g_PhyDriveList + i;
329 }
330 }
331
332 return NULL;
333 }
334
335 int SortPhysicalDrive(PHY_DRIVE_INFO *pDriveList, DWORD DriveCount)
336 {
337 DWORD i, j;
338 BOOL flag;
339 PHY_DRIVE_INFO TmpDriveInfo;
340
341 for (i = 0; i < DriveCount; i++)
342 {
343 for (j = i + 1; j < DriveCount; j++)
344 {
345 flag = FALSE;
346
347 if (pDriveList[i].BusType == BusTypeUsb && pDriveList[j].BusType == BusTypeUsb)
348 {
349 if (pDriveList[i].RemovableMedia == FALSE && pDriveList[j].RemovableMedia == TRUE)
350 {
351 flag = TRUE;
352 }
353 }
354 else if (pDriveList[j].BusType == BusTypeUsb)
355 {
356 flag = TRUE;
357 }
358 else
359 {
360 if (pDriveList[j].PhyDrive < pDriveList[i].PhyDrive)
361 {
362 flag = TRUE;
363 }
364 }
365
366 if (flag)
367 {
368 memcpy(&TmpDriveInfo, pDriveList + i, sizeof(PHY_DRIVE_INFO));
369 memcpy(pDriveList + i, pDriveList + j, sizeof(PHY_DRIVE_INFO));
370 memcpy(pDriveList + j, &TmpDriveInfo, sizeof(PHY_DRIVE_INFO));
371 }
372 }
373 }
374
375 return 0;
376 }
377
378 int Ventoy2DiskInit(void)
379 {
380 Log("\n===================== Enum All PhyDrives =====================");
381 g_PhyDriveList = (PHY_DRIVE_INFO *)malloc(sizeof(PHY_DRIVE_INFO)* VENTOY_MAX_PHY_DRIVE);
382 if (NULL == g_PhyDriveList)
383 {
384 Log("Failed to alloc phy drive memory");
385 return FALSE;
386 }
387 memset(g_PhyDriveList, 0, sizeof(PHY_DRIVE_INFO)* VENTOY_MAX_PHY_DRIVE);
388
389 GetAllPhysicalDriveInfo(g_PhyDriveList, &g_PhyDriveCount);
390
391 SortPhysicalDrive(g_PhyDriveList, g_PhyDriveCount);
392
393 FilterPhysicalDrive(g_PhyDriveList, g_PhyDriveCount);
394
395 return 0;
396 }
397
398 int Ventoy2DiskDestroy(void)
399 {
400 free(g_PhyDriveList);
401 return 0;
402 }