]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/Ventoy2Disk.c
2e0ef2046be66164cf0c69e906d143835872d25b
[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_NoNeedInputYes = 0;
32 int g_WriteImage = 0;
33
34 int ParseCmdLineOption(LPSTR lpCmdLine)
35 {
36 int i;
37 char cfgfile[MAX_PATH];
38
39 if (lpCmdLine && lpCmdLine[0])
40 {
41 Log("CmdLine:<%s>", lpCmdLine);
42 }
43
44 for (i = 0; i < __argc; i++)
45 {
46 if (strncmp(__argv[i], "-U", 2) == 0 ||
47 strncmp(__argv[i], "-u", 2) == 0)
48 {
49 g_FilterUSB = 0;
50 }
51 else if (strncmp(__argv[i], "-F", 2) == 0)
52 {
53 g_ForceOperation = 1;
54 }
55 else if (strncmp(__argv[i], "-Y", 2) == 0 || strncmp(__argv[i], "-y", 2) == 0)
56 {
57 g_NoNeedInputYes = 1;
58 }
59 }
60
61 GetCurrentDirectoryA(sizeof(cfgfile), cfgfile);
62 strcat_s(cfgfile, sizeof(cfgfile), "\\Ventoy2Disk.ini");
63
64 if (0 == GetPrivateProfileIntA("Filter", "USB", 1, cfgfile))
65 {
66 g_FilterUSB = 0;
67 }
68
69 if (1 == GetPrivateProfileIntA("Operation", "Force", 0, cfgfile))
70 {
71 g_ForceOperation = 1;
72 }
73
74 Log("Control Flag: %d %d %d", g_FilterRemovable, g_FilterUSB, g_ForceOperation);
75
76 return 0;
77 }
78
79 static BOOL IsVentoyPhyDrive(int PhyDrive, UINT64 SizeBytes, MBR_HEAD *pMBR, UINT64 *Part2StartSector, UINT64 *GptPart2Attr)
80 {
81 int i;
82 BOOL bRet;
83 DWORD dwSize;
84 HANDLE hDrive;
85 MBR_HEAD MBR;
86 UINT32 PartStartSector;
87 UINT32 PartSectorCount;
88 CHAR PhyDrivePath[128];
89 CHAR GUIDStr[128];
90 GUID ZeroGuid = { 0 };
91 VTOY_GPT_INFO *pGpt = NULL;
92
93 safe_sprintf(PhyDrivePath, "\\\\.\\PhysicalDrive%d", PhyDrive);
94 hDrive = CreateFileA(PhyDrivePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
95 Log("Create file Handle:%p %s status:%u", hDrive, PhyDrivePath, LASTERR);
96
97 if (hDrive == INVALID_HANDLE_VALUE)
98 {
99 return FALSE;
100 }
101
102 bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
103 Log("Read MBR Ret:%u Size:%u code:%u", bRet, dwSize, LASTERR);
104
105 if ((!bRet) || (dwSize != sizeof(MBR)))
106 {
107 CHECK_CLOSE_HANDLE(hDrive);
108 return FALSE;
109 }
110
111 if (MBR.Byte55 != 0x55 || MBR.ByteAA != 0xAA)
112 {
113 Log("Byte55 ByteAA not match 0x%x 0x%x", MBR.Byte55, MBR.ByteAA);
114 CHECK_CLOSE_HANDLE(hDrive);
115 return FALSE;
116 }
117
118
119
120 if (MBR.PartTbl[0].FsFlag == 0xEE)
121 {
122 pGpt = malloc(sizeof(VTOY_GPT_INFO));
123 if (!pGpt)
124 {
125 CHECK_CLOSE_HANDLE(hDrive);
126 return FALSE;
127 }
128
129 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
130 bRet = ReadFile(hDrive, pGpt, sizeof(VTOY_GPT_INFO), &dwSize, NULL);
131 CHECK_CLOSE_HANDLE(hDrive);
132 if ((!bRet) || (dwSize != sizeof(VTOY_GPT_INFO)))
133 {
134 Log("Failed to read gpt info %d %u %d", bRet, dwSize, LASTERR);
135 return FALSE;
136 }
137
138 if (memcmp(pGpt->Head.Signature, "EFI PART", 8))
139 {
140 Log("Invalid GPT signature");
141 return FALSE;
142 }
143
144 for (i = 0; i < 128; i++)
145 {
146 if (memcmp(&(pGpt->PartTbl[i].PartGuid), &ZeroGuid, sizeof(GUID)) == 0)
147 {
148 continue;
149 }
150
151 Log("=========== Disk%d GPT Partition %d ============", PhyDrive, i + 1);
152
153 Log("PartTbl.PartType = %s", GUID2String(&pGpt->PartTbl[i].PartType, GUIDStr, sizeof(GUIDStr)));
154 Log("PartTbl.PartGuid = %s", GUID2String(&pGpt->PartTbl[i].PartGuid, GUIDStr, sizeof(GUIDStr)));
155 Log("PartTbl.StartLBA = %llu", (ULONGLONG)pGpt->PartTbl[i].StartLBA);
156 Log("PartTbl.LastLBA = %llu", (ULONGLONG)pGpt->PartTbl[i].LastLBA);
157 Log("PartTbl.Attribute = 0x%llx", pGpt->PartTbl[i].Attr);
158 Log("PartTbl.Name = %S", pGpt->PartTbl[i].Name);
159 }
160
161 if (memcmp(pGpt->PartTbl[1].Name, L"VTOYEFI", 7 * 2))
162 {
163 if (pGpt->PartTbl[1].Name[0])
164 {
165 Log("Invalid ventoy efi part name <%S>", pGpt->PartTbl[1].Name);
166 }
167 else
168 {
169 Log("Invalid ventoy efi part name <null>");
170 }
171
172 return FALSE;
173 }
174
175 if (pGpt->PartTbl[0].StartLBA != 2048)
176 {
177 Log("Part1 not match %llu", pGpt->PartTbl[0].StartLBA);
178 return FALSE;
179 }
180
181 PartSectorCount = VENTOY_EFI_PART_SIZE / 512;
182
183 if (pGpt->PartTbl[1].StartLBA != pGpt->PartTbl[0].LastLBA + 1 ||
184 (UINT32)(pGpt->PartTbl[1].LastLBA + 1 - pGpt->PartTbl[1].StartLBA) != PartSectorCount)
185 {
186 Log("Part2 not match [%llu %llu] [%llu %llu]",
187 pGpt->PartTbl[0].StartLBA, pGpt->PartTbl[0].LastLBA,
188 pGpt->PartTbl[1].StartLBA, pGpt->PartTbl[1].LastLBA);
189 return FALSE;
190 }
191
192 *GptPart2Attr = pGpt->PartTbl[1].Attr;
193 *Part2StartSector = pGpt->PartTbl[1].StartLBA;
194
195 memcpy(pMBR, &(pGpt->MBR), sizeof(MBR_HEAD));
196 }
197 else
198 {
199 CHECK_CLOSE_HANDLE(hDrive);
200
201 for (i = 0; i < 4; i++)
202 {
203 Log("=========== Disk%d MBR Partition %d ============", PhyDrive, i + 1);
204 Log("PartTbl.Active = 0x%x", MBR.PartTbl[i].Active);
205 Log("PartTbl.FsFlag = 0x%x", MBR.PartTbl[i].FsFlag);
206 Log("PartTbl.StartSectorId = %u", MBR.PartTbl[i].StartSectorId);
207 Log("PartTbl.SectorCount = %u", MBR.PartTbl[i].SectorCount);
208 Log("PartTbl.StartHead = %u", MBR.PartTbl[i].StartHead);
209 Log("PartTbl.StartSector = %u", MBR.PartTbl[i].StartSector);
210 Log("PartTbl.StartCylinder = %u", MBR.PartTbl[i].StartCylinder);
211 Log("PartTbl.EndHead = %u", MBR.PartTbl[i].EndHead);
212 Log("PartTbl.EndSector = %u", MBR.PartTbl[i].EndSector);
213 Log("PartTbl.EndCylinder = %u", MBR.PartTbl[i].EndCylinder);
214 }
215
216 if (MBR.PartTbl[0].StartSectorId != 2048)
217 {
218 Log("Part1 not match %u", MBR.PartTbl[0].StartSectorId);
219 return FALSE;
220 }
221
222 PartStartSector = MBR.PartTbl[0].StartSectorId + MBR.PartTbl[0].SectorCount;
223 PartSectorCount = VENTOY_EFI_PART_SIZE / 512;
224
225 if (MBR.PartTbl[1].StartSectorId != PartStartSector ||
226 MBR.PartTbl[1].SectorCount != PartSectorCount)
227 {
228 Log("Part2 not match [0x%x 0x%x] [%u %u] [%u %u]",
229 MBR.PartTbl[1].FsFlag, 0xEF,
230 MBR.PartTbl[1].StartSectorId, PartStartSector,
231 MBR.PartTbl[1].SectorCount, PartSectorCount);
232 return FALSE;
233 }
234
235 if (MBR.PartTbl[0].Active != 0x80 && MBR.PartTbl[1].Active != 0x80)
236 {
237 Log("Part1 and Part2 are both NOT active 0x%x 0x%x", MBR.PartTbl[0].Active, MBR.PartTbl[1].Active);
238 if (MBR.PartTbl[2].Active != 0x80 && MBR.PartTbl[3].Active != 0x80)
239 {
240 Log("Part3 and Part4 are both NOT active 0x%x 0x%x", MBR.PartTbl[2].Active, MBR.PartTbl[3].Active);
241 //return FALSE;
242 }
243 }
244
245 *Part2StartSector = MBR.PartTbl[1].StartSectorId;
246
247 memcpy(pMBR, &MBR, sizeof(MBR_HEAD));
248 }
249
250 Log("PhysicalDrive%d is ventoy disk", PhyDrive);
251 return TRUE;
252 }
253
254 static int GetVentoyFsNameInPhyDrive(PHY_DRIVE_INFO* CurDrive)
255 {
256 int i = 0;
257 UINT64 Offset;
258 CHAR Volume[128] = { 0 };
259 CHAR FsName[MAX_PATH] = { 0 };
260
261 while (CurDrive->DriveLetters[i])
262 {
263 if (GetPhyDriveByLogicalDrive(CurDrive->DriveLetters[i], &Offset) >= 0)
264 {
265 if (Offset == SIZE_1MB)
266 {
267 sprintf_s(Volume, sizeof(Volume), "%C:\\", CurDrive->DriveLetters[i]);
268 Log("Find the partition 1 logical drive is %s", Volume);
269 break;
270 }
271 }
272 i++;
273 }
274
275 sprintf_s(CurDrive->VentoyFsType, sizeof(CurDrive->VentoyFsType), "??");
276
277 if (Volume[0])
278 {
279 if (GetVolumeInformationA(Volume, NULL, 0, NULL, NULL, NULL, FsName, MAX_PATH))
280 {
281 if (_stricmp(FsName, "exFAT") == 0)
282 {
283 sprintf_s(CurDrive->VentoyFsType, sizeof(CurDrive->VentoyFsType), "exFAT");
284 }
285 else if (_stricmp(FsName, "NTFS") == 0)
286 {
287 sprintf_s(CurDrive->VentoyFsType, sizeof(CurDrive->VentoyFsType), "NTFS");
288 }
289 else if (_stricmp(FsName, "FAT") == 0 || _stricmp(FsName, "FAT32") == 0)
290 {
291 sprintf_s(CurDrive->VentoyFsType, sizeof(CurDrive->VentoyFsType), "FAT32");
292 }
293 else
294 {
295 sprintf_s(CurDrive->VentoyFsType, sizeof(CurDrive->VentoyFsType), "%s", FsName);
296 }
297
298 Log("GetVentoyFsNameInPhyDrive %d %s <%s> <%s>", CurDrive->PhyDrive, Volume, FsName, CurDrive->VentoyFsType);
299 }
300 else
301 {
302 Log("GetVolumeInformationA %s failed %u", Volume, LASTERR);
303 }
304 }
305 else
306 {
307 Log("GetVentoyFsNameInPhyDrive %s not found", Volume);
308 }
309
310 return 0;
311 }
312
313 static int FilterPhysicalDrive(PHY_DRIVE_INFO *pDriveList, DWORD DriveCount)
314 {
315 DWORD i;
316 DWORD LogDrive;
317 int Count = 0;
318 int Letter = 'A';
319 int Id = 0;
320 int LetterCount = 0;
321 UINT64 Part2GPTAttr = 0;
322 UINT64 Part2StartSector = 0;
323 PHY_DRIVE_INFO *CurDrive;
324 MBR_HEAD MBR;
325 int LogLetter[VENTOY_MAX_PHY_DRIVE];
326 int PhyDriveId[VENTOY_MAX_PHY_DRIVE];
327
328 for (LogDrive = GetLogicalDrives(); LogDrive > 0; LogDrive >>= 1)
329 {
330 if (LogDrive & 0x01)
331 {
332 LogLetter[LetterCount] = Letter;
333 PhyDriveId[LetterCount] = GetPhyDriveByLogicalDrive(Letter, NULL);
334
335 Log("Logical Drive:%C ===> PhyDrive:%d", LogLetter[LetterCount], PhyDriveId[LetterCount]);
336 LetterCount++;
337 }
338
339 Letter++;
340 }
341
342 for (i = 0; i < DriveCount; i++)
343 {
344 Part2GPTAttr = 0;
345 CurDrive = pDriveList + i;
346
347 CurDrive->Id = -1;
348 memset(CurDrive->DriveLetters, 0, sizeof(CurDrive->DriveLetters));
349
350 if (g_FilterRemovable && (!CurDrive->RemovableMedia))
351 {
352 Log("<%s %s> is filtered for not removable.", CurDrive->VendorId, CurDrive->ProductId);
353 continue;
354 }
355
356 if (g_FilterUSB && CurDrive->BusType != BusTypeUsb)
357 {
358 Log("<%s %s> is filtered for not USB type.", CurDrive->VendorId, CurDrive->ProductId);
359 continue;
360 }
361
362 CurDrive->Id = Id++;
363
364 for (Count = 0, Letter = 0; Letter < LetterCount; Letter++)
365 {
366 if (PhyDriveId[Letter] == CurDrive->PhyDrive)
367 {
368 if (Count + 1 < sizeof(CurDrive->DriveLetters) / sizeof(CHAR))
369 {
370 CurDrive->DriveLetters[Count] = LogLetter[Letter];
371 }
372 Count++;
373 }
374 }
375
376 if (IsVentoyPhyDrive(CurDrive->PhyDrive, CurDrive->SizeInBytes, &MBR, &Part2StartSector, &Part2GPTAttr))
377 {
378 memcpy(&(CurDrive->MBR), &MBR, sizeof(MBR));
379 CurDrive->PartStyle = (MBR.PartTbl[0].FsFlag == 0xEE) ? 1 : 0;
380 CurDrive->Part2GPTAttr = Part2GPTAttr;
381 GetVentoyVerInPhyDrive(CurDrive, Part2StartSector, CurDrive->VentoyVersion, sizeof(CurDrive->VentoyVersion), &(CurDrive->SecureBootSupport));
382 Log("PhyDrive %d is Ventoy Disk ver:%s SecureBoot:%u", CurDrive->PhyDrive, CurDrive->VentoyVersion, CurDrive->SecureBootSupport);
383
384 GetVentoyFsNameInPhyDrive(CurDrive);
385
386 if (CurDrive->VentoyVersion[0] == 0)
387 {
388 CurDrive->VentoyVersion[0] = '?';
389 Log("Unknown Ventoy Version");
390 }
391 }
392 }
393
394 // for safe
395 for (i = 0; i < DriveCount; i++)
396 {
397 CurDrive = pDriveList + i;
398 if (CurDrive->Id < 0)
399 {
400 CurDrive->PhyDrive = 0x00FFFFFF;
401 }
402 }
403
404 return Id;
405 }
406
407 PHY_DRIVE_INFO * GetPhyDriveInfoById(int Id)
408 {
409 DWORD i;
410 for (i = 0; i < g_PhyDriveCount; i++)
411 {
412 if (g_PhyDriveList[i].Id >= 0 && g_PhyDriveList[i].Id == Id)
413 {
414 return g_PhyDriveList + i;
415 }
416 }
417
418 return NULL;
419 }
420
421
422 PHY_DRIVE_INFO * GetPhyDriveInfoByPhyDrive(int PhyDrive)
423 {
424 DWORD i;
425 for (i = 0; i < g_PhyDriveCount; i++)
426 {
427 if (g_PhyDriveList[i].PhyDrive == PhyDrive)
428 {
429 return g_PhyDriveList + i;
430 }
431 }
432
433 return NULL;
434 }
435
436
437
438 int SortPhysicalDrive(PHY_DRIVE_INFO *pDriveList, DWORD DriveCount)
439 {
440 DWORD i, j;
441 BOOL flag;
442 PHY_DRIVE_INFO TmpDriveInfo;
443
444 for (i = 0; i < DriveCount; i++)
445 {
446 for (j = i + 1; j < DriveCount; j++)
447 {
448 flag = FALSE;
449
450 if (pDriveList[i].BusType == BusTypeUsb && pDriveList[j].BusType == BusTypeUsb)
451 {
452 if (pDriveList[i].RemovableMedia == FALSE && pDriveList[j].RemovableMedia == TRUE)
453 {
454 flag = TRUE;
455 }
456 }
457 else if (pDriveList[j].BusType == BusTypeUsb)
458 {
459 flag = TRUE;
460 }
461 else
462 {
463 if (pDriveList[j].PhyDrive < pDriveList[i].PhyDrive)
464 {
465 flag = TRUE;
466 }
467 }
468
469 if (flag)
470 {
471 memcpy(&TmpDriveInfo, pDriveList + i, sizeof(PHY_DRIVE_INFO));
472 memcpy(pDriveList + i, pDriveList + j, sizeof(PHY_DRIVE_INFO));
473 memcpy(pDriveList + j, &TmpDriveInfo, sizeof(PHY_DRIVE_INFO));
474 }
475 }
476 }
477
478 return 0;
479 }
480
481 int Ventoy2DiskInit(void)
482 {
483 Log("\n===================== Enum All PhyDrives =====================");
484 g_PhyDriveList = (PHY_DRIVE_INFO *)malloc(sizeof(PHY_DRIVE_INFO)* VENTOY_MAX_PHY_DRIVE);
485 if (NULL == g_PhyDriveList)
486 {
487 Log("Failed to alloc phy drive memory");
488 return FALSE;
489 }
490 memset(g_PhyDriveList, 0, sizeof(PHY_DRIVE_INFO)* VENTOY_MAX_PHY_DRIVE);
491
492 GetAllPhysicalDriveInfo(g_PhyDriveList, &g_PhyDriveCount);
493
494 SortPhysicalDrive(g_PhyDriveList, g_PhyDriveCount);
495
496 FilterPhysicalDrive(g_PhyDriveList, g_PhyDriveCount);
497
498 return 0;
499 }
500
501 int Ventoy2DiskDestroy(void)
502 {
503 free(g_PhyDriveList);
504 g_PhyDriveList = NULL;
505 g_PhyDriveCount = 0;
506
507 return 0;
508 }