]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
b35a2d503296fc7341c8c046463d24f8aa094e7b
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / PhyDrive.c
1 /******************************************************************************
2 * PhyDrive.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 <winternl.h>
23 #include <commctrl.h>
24 #include <initguid.h>
25 #include <vds.h>
26 #include "resource.h"
27 #include "Language.h"
28 #include "Ventoy2Disk.h"
29 #include "fat_filelib.h"
30 #include "ff.h"
31
32 #define VDS_SET_ERROR SetLastError
33 #define IVdsServiceLoader_LoadService(This, pwszMachineName, ppService) (This)->lpVtbl->LoadService(This, pwszMachineName, ppService)
34 #define IVdsServiceLoader_Release(This) (This)->lpVtbl->Release(This)
35 #define IVdsService_QueryProviders(This, masks, ppEnum) (This)->lpVtbl->QueryProviders(This, masks, ppEnum)
36 #define IVdsService_WaitForServiceReady(This) ((This)->lpVtbl->WaitForServiceReady(This))
37 #define IVdsService_CleanupObsoleteMountPoints(This) ((This)->lpVtbl->CleanupObsoleteMountPoints(This))
38 #define IVdsService_Refresh(This) ((This)->lpVtbl->Refresh(This))
39 #define IVdsService_Reenumerate(This) ((This)->lpVtbl->Reenumerate(This))
40 #define IVdsSwProvider_QueryInterface(This, riid, ppvObject) (This)->lpVtbl->QueryInterface(This, riid, ppvObject)
41 #define IVdsProvider_Release(This) (This)->lpVtbl->Release(This)
42 #define IVdsSwProvider_QueryPacks(This, ppEnum) (This)->lpVtbl->QueryPacks(This, ppEnum)
43 #define IVdsSwProvider_Release(This) (This)->lpVtbl->Release(This)
44 #define IVdsPack_QueryDisks(This, ppEnum) (This)->lpVtbl->QueryDisks(This, ppEnum)
45 #define IVdsDisk_GetProperties(This, pDiskProperties) (This)->lpVtbl->GetProperties(This, pDiskProperties)
46 #define IVdsDisk_Release(This) (This)->lpVtbl->Release(This)
47 #define IVdsDisk_QueryInterface(This, riid, ppvObject) (This)->lpVtbl->QueryInterface(This, riid, ppvObject)
48 #define IVdsAdvancedDisk_QueryPartitions(This, ppPartitionPropArray, plNumberOfPartitions) (This)->lpVtbl->QueryPartitions(This, ppPartitionPropArray, plNumberOfPartitions)
49 #define IVdsAdvancedDisk_DeletePartition(This, ullOffset, bForce, bForceProtected) (This)->lpVtbl->DeletePartition(This, ullOffset, bForce, bForceProtected)
50 #define IVdsAdvancedDisk_Clean(This, bForce, bForceOEM, bFullClean, ppAsync) (This)->lpVtbl->Clean(This, bForce, bForceOEM, bFullClean, ppAsync)
51 #define IVdsAdvancedDisk_Release(This) (This)->lpVtbl->Release(This)
52 #define IEnumVdsObject_Next(This, celt, ppObjectArray, pcFetched) (This)->lpVtbl->Next(This, celt, ppObjectArray, pcFetched)
53 #define IVdsPack_QueryVolumes(This, ppEnum) (This)->lpVtbl->QueryVolumes(This, ppEnum)
54 #define IVdsVolume_QueryInterface(This, riid, ppvObject) (This)->lpVtbl->QueryInterface(This, riid, ppvObject)
55 #define IVdsVolume_Release(This) (This)->lpVtbl->Release(This)
56 #define IVdsVolumeMF3_QueryVolumeGuidPathnames(This, pwszPathArray, pulNumberOfPaths) (This)->lpVtbl->QueryVolumeGuidPathnames(This,pwszPathArray,pulNumberOfPaths)
57 #define IVdsVolumeMF3_FormatEx2(This, pwszFileSystemTypeName, usFileSystemRevision, ulDesiredUnitAllocationSize, pwszLabel, Options, ppAsync) (This)->lpVtbl->FormatEx2(This, pwszFileSystemTypeName, usFileSystemRevision, ulDesiredUnitAllocationSize, pwszLabel, Options, ppAsync)
58 #define IVdsVolumeMF3_Release(This) (This)->lpVtbl->Release(This)
59 #define IVdsVolume_GetProperties(This, pVolumeProperties) (This)->lpVtbl->GetProperties(This,pVolumeProperties)
60 #define IVdsAsync_Cancel(This) (This)->lpVtbl->Cancel(This)
61 #define IVdsAsync_QueryStatus(This,pHrResult,pulPercentCompleted) (This)->lpVtbl->QueryStatus(This,pHrResult,pulPercentCompleted)
62 #define IVdsAsync_Wait(This,pHrResult,pAsyncOut) (This)->lpVtbl->Wait(This,pHrResult,pAsyncOut)
63 #define IVdsAsync_Release(This) (This)->lpVtbl->Release(This)
64
65 #define IUnknown_QueryInterface(This, a, b) (This)->lpVtbl->QueryInterface(This,a,b)
66 #define IUnknown_Release(This) (This)->lpVtbl->Release(This)
67
68 /*
69 * Delete all the partitions from a disk, using VDS
70 * Mostly copied from https://social.msdn.microsoft.com/Forums/vstudio/en-US/b90482ae-4e44-4b08-8731-81915030b32a/createpartition-using-vds-interface-throw-error-enointerface-dcom?forum=vcgeneral
71 */
72 BOOL DeletePartitions(DWORD DriveIndex, BOOL OnlyPart2)
73 {
74 BOOL r = FALSE;
75 HRESULT hr;
76 ULONG ulFetched;
77 wchar_t wPhysicalName[48];
78 IVdsServiceLoader *pLoader;
79 IVdsService *pService;
80 IEnumVdsObject *pEnum;
81 IUnknown *pUnk;
82
83 swprintf_s(wPhysicalName, ARRAYSIZE(wPhysicalName), L"\\\\?\\PhysicalDrive%lu", DriveIndex);
84
85 // Initialize COM
86 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
87 CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT,
88 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0, NULL);
89
90 // Create a VDS Loader Instance
91 hr = CoCreateInstance(&CLSID_VdsLoader, NULL, CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER,
92 &IID_IVdsServiceLoader, (void **)&pLoader);
93 if (hr != S_OK) {
94 VDS_SET_ERROR(hr);
95 Log("Could not create VDS Loader Instance: %u", LASTERR);
96 goto out;
97 }
98
99 // Load the VDS Service
100 hr = IVdsServiceLoader_LoadService(pLoader, L"", &pService);
101 IVdsServiceLoader_Release(pLoader);
102 if (hr != S_OK) {
103 VDS_SET_ERROR(hr);
104 Log("Could not load VDS Service: %u", LASTERR);
105 goto out;
106 }
107
108 // Wait for the Service to become ready if needed
109 hr = IVdsService_WaitForServiceReady(pService);
110 if (hr != S_OK) {
111 VDS_SET_ERROR(hr);
112 Log("VDS Service is not ready: %u", LASTERR);
113 goto out;
114 }
115
116 // Query the VDS Service Providers
117 hr = IVdsService_QueryProviders(pService, VDS_QUERY_SOFTWARE_PROVIDERS, &pEnum);
118 if (hr != S_OK) {
119 VDS_SET_ERROR(hr);
120 Log("Could not query VDS Service Providers: %u", LASTERR);
121 goto out;
122 }
123
124 while (IEnumVdsObject_Next(pEnum, 1, &pUnk, &ulFetched) == S_OK) {
125 IVdsProvider *pProvider;
126 IVdsSwProvider *pSwProvider;
127 IEnumVdsObject *pEnumPack;
128 IUnknown *pPackUnk;
129
130 // Get VDS Provider
131 hr = IUnknown_QueryInterface(pUnk, &IID_IVdsProvider, (void **)&pProvider);
132 IUnknown_Release(pUnk);
133 if (hr != S_OK) {
134 VDS_SET_ERROR(hr);
135 Log("Could not get VDS Provider: %u", LASTERR);
136 goto out;
137 }
138
139 // Get VDS Software Provider
140 hr = IVdsSwProvider_QueryInterface(pProvider, &IID_IVdsSwProvider, (void **)&pSwProvider);
141 IVdsProvider_Release(pProvider);
142 if (hr != S_OK) {
143 VDS_SET_ERROR(hr);
144 Log("Could not get VDS Software Provider: %u", LASTERR);
145 goto out;
146 }
147
148 // Get VDS Software Provider Packs
149 hr = IVdsSwProvider_QueryPacks(pSwProvider, &pEnumPack);
150 IVdsSwProvider_Release(pSwProvider);
151 if (hr != S_OK) {
152 VDS_SET_ERROR(hr);
153 Log("Could not get VDS Software Provider Packs: %u", LASTERR);
154 goto out;
155 }
156
157 // Enumerate Provider Packs
158 while (IEnumVdsObject_Next(pEnumPack, 1, &pPackUnk, &ulFetched) == S_OK) {
159 IVdsPack *pPack;
160 IEnumVdsObject *pEnumDisk;
161 IUnknown *pDiskUnk;
162
163 hr = IUnknown_QueryInterface(pPackUnk, &IID_IVdsPack, (void **)&pPack);
164 IUnknown_Release(pPackUnk);
165 if (hr != S_OK) {
166 VDS_SET_ERROR(hr);
167 Log("Could not query VDS Software Provider Pack: %u", LASTERR);
168 goto out;
169 }
170
171 // Use the pack interface to access the disks
172 hr = IVdsPack_QueryDisks(pPack, &pEnumDisk);
173 if (hr != S_OK) {
174 VDS_SET_ERROR(hr);
175 Log("Could not query VDS disks: %u", LASTERR);
176 goto out;
177 }
178
179 // List disks
180 while (IEnumVdsObject_Next(pEnumDisk, 1, &pDiskUnk, &ulFetched) == S_OK) {
181 VDS_DISK_PROP diskprop;
182 VDS_PARTITION_PROP* prop_array;
183 LONG i, prop_array_size;
184 IVdsDisk *pDisk;
185 IVdsAdvancedDisk *pAdvancedDisk;
186
187 // Get the disk interface.
188 hr = IUnknown_QueryInterface(pDiskUnk, &IID_IVdsDisk, (void **)&pDisk);
189 if (hr != S_OK) {
190 VDS_SET_ERROR(hr);
191 Log("Could not query VDS Disk Interface: %u", LASTERR);
192 goto out;
193 }
194
195 // Get the disk properties
196 hr = IVdsDisk_GetProperties(pDisk, &diskprop);
197 if (hr != S_OK) {
198 VDS_SET_ERROR(hr);
199 Log("Could not query VDS Disk Properties: %u", LASTERR);
200 goto out;
201 }
202
203 // Isolate the disk we want
204 if (_wcsicmp(wPhysicalName, diskprop.pwszName) != 0) {
205 IVdsDisk_Release(pDisk);
206 continue;
207 }
208
209 // Instantiate the AdvanceDisk interface for our disk.
210 hr = IVdsDisk_QueryInterface(pDisk, &IID_IVdsAdvancedDisk, (void **)&pAdvancedDisk);
211 IVdsDisk_Release(pDisk);
212 if (hr != S_OK) {
213 VDS_SET_ERROR(hr);
214 Log("Could not access VDS Advanced Disk interface: %u", LASTERR);
215 goto out;
216 }
217
218 // Query the partition data, so we can get the start offset, which we need for deletion
219 hr = IVdsAdvancedDisk_QueryPartitions(pAdvancedDisk, &prop_array, &prop_array_size);
220 if (hr == S_OK) {
221 Log("Deleting ALL partition(s) from disk '%S':", diskprop.pwszName);
222 // Now go through each partition
223 for (i = 0; i < prop_array_size; i++) {
224
225 Log("* Partition %d (offset: %lld, size: %llu)", prop_array[i].ulPartitionNumber,
226 prop_array[i].ullOffset, (ULONGLONG)prop_array[i].ullSize);
227
228 if (OnlyPart2 && prop_array[i].ullOffset == 2048*512)
229 {
230 Log("Skip this partition...");
231 continue;
232 }
233
234
235 hr = IVdsAdvancedDisk_DeletePartition(pAdvancedDisk, prop_array[i].ullOffset, TRUE, TRUE);
236 if (hr != S_OK) {
237 r = FALSE;
238 VDS_SET_ERROR(hr);
239 Log("Could not delete partitions: %u", LASTERR);
240 }
241 }
242 r = TRUE;
243 }
244 else {
245 Log("No partition to delete on disk '%S'", diskprop.pwszName);
246 r = TRUE;
247 }
248 CoTaskMemFree(prop_array);
249
250 #if 0
251 // Issue a Clean while we're at it
252 HRESULT hr2 = E_FAIL;
253 ULONG completed;
254 IVdsAsync* pAsync;
255 hr = IVdsAdvancedDisk_Clean(pAdvancedDisk, TRUE, FALSE, FALSE, &pAsync);
256 while (SUCCEEDED(hr)) {
257 if (IS_ERROR(FormatStatus)) {
258 IVdsAsync_Cancel(pAsync);
259 break;
260 }
261 hr = IVdsAsync_QueryStatus(pAsync, &hr2, &completed);
262 if (SUCCEEDED(hr)) {
263 hr = hr2;
264 if (hr == S_OK)
265 break;
266 if (hr == VDS_E_OPERATION_PENDING)
267 hr = S_OK;
268 }
269 Sleep(500);
270 }
271 if (hr != S_OK) {
272 VDS_SET_ERROR(hr);
273 Log("Could not clean disk: %s", LASTERR);
274 }
275 #endif
276 IVdsAdvancedDisk_Release(pAdvancedDisk);
277 goto out;
278 }
279 }
280 }
281
282 out:
283 return r;
284 }
285
286
287 static DWORD GetVentoyVolumeName(int PhyDrive, UINT32 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)
288 {
289 size_t len;
290 BOOL bRet;
291 DWORD dwSize;
292 HANDLE hDrive;
293 HANDLE hVolume;
294 UINT64 PartOffset;
295 DWORD Status = ERROR_NOT_FOUND;
296 DISK_EXTENT *pExtents = NULL;
297 CHAR VolumeName[MAX_PATH] = { 0 };
298 VOLUME_DISK_EXTENTS DiskExtents;
299
300 PartOffset = 512ULL * StartSectorId;
301
302 Log("GetVentoyVolumeName PhyDrive %d PartOffset:%llu", PhyDrive, (ULONGLONG)PartOffset);
303
304 hVolume = FindFirstVolumeA(VolumeName, sizeof(VolumeName));
305 if (hVolume == INVALID_HANDLE_VALUE)
306 {
307 return 1;
308 }
309
310 do {
311
312 len = strlen(VolumeName);
313 Log("Find volume:%s", VolumeName);
314
315 VolumeName[len - 1] = 0;
316
317 hDrive = CreateFileA(VolumeName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
318 if (hDrive == INVALID_HANDLE_VALUE)
319 {
320 continue;
321 }
322
323 bRet = DeviceIoControl(hDrive,
324 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
325 NULL,
326 0,
327 &DiskExtents,
328 (DWORD)(sizeof(DiskExtents)),
329 (LPDWORD)&dwSize,
330 NULL);
331
332 Log("IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS bRet:%u code:%u", bRet, LASTERR);
333 Log("NumberOfDiskExtents:%u DiskNumber:%u", DiskExtents.NumberOfDiskExtents, DiskExtents.Extents[0].DiskNumber);
334
335 if (bRet && DiskExtents.NumberOfDiskExtents == 1)
336 {
337 pExtents = DiskExtents.Extents;
338
339 Log("This volume DiskNumber:%u offset:%llu", pExtents->DiskNumber, (ULONGLONG)pExtents->StartingOffset.QuadPart);
340 if ((int)pExtents->DiskNumber == PhyDrive && pExtents->StartingOffset.QuadPart == PartOffset)
341 {
342 Log("This volume match");
343
344 if (!DelSlash)
345 {
346 VolumeName[len - 1] = '\\';
347 }
348
349 sprintf_s(NameBuf, BufLen, "%s", VolumeName);
350 Status = ERROR_SUCCESS;
351 CloseHandle(hDrive);
352 break;
353 }
354 }
355
356 CloseHandle(hDrive);
357 } while (FindNextVolumeA(hVolume, VolumeName, sizeof(VolumeName)));
358
359 FindVolumeClose(hVolume);
360
361 Log("GetVentoyVolumeName return %u", Status);
362 return Status;
363 }
364
365 static int GetLettersBelongPhyDrive(int PhyDrive, char *DriveLetters, size_t Length)
366 {
367 int n = 0;
368 DWORD DataSize = 0;
369 CHAR *Pos = NULL;
370 CHAR *StringBuf = NULL;
371
372 DataSize = GetLogicalDriveStringsA(0, NULL);
373 StringBuf = (CHAR *)malloc(DataSize + 1);
374 if (StringBuf == NULL)
375 {
376 return 1;
377 }
378
379 GetLogicalDriveStringsA(DataSize, StringBuf);
380
381 for (Pos = StringBuf; *Pos; Pos += strlen(Pos) + 1)
382 {
383 if (n < (int)Length && PhyDrive == GetPhyDriveByLogicalDrive(Pos[0]))
384 {
385 Log("%C: is belong to phydrive%d", Pos[0], PhyDrive);
386 DriveLetters[n++] = Pos[0];
387 }
388 }
389
390 free(StringBuf);
391 return 0;
392 }
393
394 static HANDLE GetPhysicalHandle(int Drive, BOOLEAN bLockDrive, BOOLEAN bWriteAccess, BOOLEAN bWriteShare)
395 {
396 int i;
397 DWORD dwSize;
398 DWORD LastError;
399 UINT64 EndTime;
400 HANDLE hDrive = INVALID_HANDLE_VALUE;
401 CHAR PhyDrive[128];
402 CHAR DevPath[MAX_PATH] = { 0 };
403
404 safe_sprintf(PhyDrive, "\\\\.\\PhysicalDrive%d", Drive);
405
406 if (0 == QueryDosDeviceA(PhyDrive + 4, DevPath, sizeof(DevPath)))
407 {
408 Log("QueryDosDeviceA failed error:%u", GetLastError());
409 strcpy_s(DevPath, sizeof(DevPath), "???");
410 }
411 else
412 {
413 Log("QueryDosDeviceA success %s", DevPath);
414 }
415
416 for (i = 0; i < DRIVE_ACCESS_RETRIES; i++)
417 {
418 // Try without FILE_SHARE_WRITE (unless specifically requested) so that
419 // we won't be bothered by the OS or other apps when we set up our data.
420 // However this means we might have to wait for an access gap...
421 // We keep FILE_SHARE_READ though, as this shouldn't hurt us any, and is
422 // required for enumeration.
423 hDrive = CreateFileA(PhyDrive,
424 GENERIC_READ | (bWriteAccess ? GENERIC_WRITE : 0),
425 FILE_SHARE_READ | (bWriteShare ? FILE_SHARE_WRITE : 0),
426 NULL,
427 OPEN_EXISTING,
428 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
429 NULL);
430
431 LastError = GetLastError();
432 Log("[%d] CreateFileA %s code:%u %p", i, PhyDrive, LastError, hDrive);
433
434 if (hDrive != INVALID_HANDLE_VALUE)
435 {
436 break;
437 }
438
439 if ((LastError != ERROR_SHARING_VIOLATION) && (LastError != ERROR_ACCESS_DENIED))
440 {
441 break;
442 }
443
444 if (i == 0)
445 {
446 Log("Waiting for access on %s [%s]...", PhyDrive, DevPath);
447 }
448 else if (!bWriteShare && (i > DRIVE_ACCESS_RETRIES / 3))
449 {
450 // If we can't seem to get a hold of the drive for some time, try to enable FILE_SHARE_WRITE...
451 Log("Warning: Could not obtain exclusive rights. Retrying with write sharing enabled...");
452 bWriteShare = TRUE;
453
454 // Try to report the process that is locking the drive
455 // We also use bit 6 as a flag to indicate that SearchProcess was called.
456 //access_mask = SearchProcess(DevPath, SEARCH_PROCESS_TIMEOUT, TRUE, TRUE, FALSE) | 0x40;
457
458 }
459 Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
460 }
461
462 if (hDrive == INVALID_HANDLE_VALUE)
463 {
464 Log("Could not open %s %u", PhyDrive, LASTERR);
465 goto End;
466 }
467
468 if (bWriteAccess)
469 {
470 Log("Opened %s for %s write access", PhyDrive, bWriteShare ? "shared" : "exclusive");
471 }
472
473 if (bLockDrive)
474 {
475 if (DeviceIoControl(hDrive, FSCTL_ALLOW_EXTENDED_DASD_IO, NULL, 0, NULL, 0, &dwSize, NULL))
476 {
477 Log("I/O boundary checks disabled");
478 }
479
480 EndTime = GetTickCount64() + DRIVE_ACCESS_TIMEOUT;
481
482 do {
483 if (DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL))
484 {
485 Log("FSCTL_LOCK_VOLUME success");
486 goto End;
487 }
488 Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
489 } while (GetTickCount64() < EndTime);
490
491 // If we reached this section, either we didn't manage to get a lock or the user cancelled
492 Log("Could not lock access to %s %u", PhyDrive, LASTERR);
493
494 // See if we can report the processes are accessing the drive
495 //if (!IS_ERROR(FormatStatus) && (access_mask == 0))
496 // access_mask = SearchProcess(DevPath, SEARCH_PROCESS_TIMEOUT, TRUE, TRUE, FALSE);
497 // Try to continue if the only access rights we saw were for read-only
498 //if ((access_mask & 0x07) != 0x01)
499 // safe_closehandle(hDrive);
500
501 CHECK_CLOSE_HANDLE(hDrive);
502 }
503
504 End:
505
506 if (hDrive == INVALID_HANDLE_VALUE)
507 {
508 Log("Can get handle of %s, maybe some process control it.", DevPath);
509 }
510
511 return hDrive;
512 }
513
514 int GetPhyDriveByLogicalDrive(int DriveLetter)
515 {
516 BOOL Ret;
517 DWORD dwSize;
518 HANDLE Handle;
519 VOLUME_DISK_EXTENTS DiskExtents;
520 CHAR PhyPath[128];
521
522 safe_sprintf(PhyPath, "\\\\.\\%C:", (CHAR)DriveLetter);
523
524 Handle = CreateFileA(PhyPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
525 if (Handle == INVALID_HANDLE_VALUE)
526 {
527 Log("Could not open the disk<%s>, error:%u", PhyPath, LASTERR);
528 return -1;
529 }
530
531 Ret = DeviceIoControl(Handle,
532 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
533 NULL,
534 0,
535 &DiskExtents,
536 (DWORD)(sizeof(DiskExtents)),
537 (LPDWORD)&dwSize,
538 NULL);
539
540 if (!Ret || DiskExtents.NumberOfDiskExtents == 0)
541 {
542 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath, LASTERR);
543 CHECK_CLOSE_HANDLE(Handle);
544 return -1;
545 }
546 CHECK_CLOSE_HANDLE(Handle);
547
548 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
549 PhyPath,
550 DiskExtents.Extents[0].DiskNumber,
551 DiskExtents.Extents[0].StartingOffset.QuadPart,
552 DiskExtents.Extents[0].ExtentLength.QuadPart
553 );
554
555 return (int)DiskExtents.Extents[0].DiskNumber;
556 }
557
558 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount)
559 {
560 int i;
561 int Count;
562 int id;
563 int Letter = 'A';
564 BOOL bRet;
565 DWORD dwBytes;
566 DWORD DriveCount = 0;
567 HANDLE Handle = INVALID_HANDLE_VALUE;
568 CHAR PhyDrive[128];
569 PHY_DRIVE_INFO *CurDrive = pDriveList;
570 GET_LENGTH_INFORMATION LengthInfo;
571 STORAGE_PROPERTY_QUERY Query;
572 STORAGE_DESCRIPTOR_HEADER DevDescHeader;
573 STORAGE_DEVICE_DESCRIPTOR *pDevDesc;
574 int PhyDriveId[VENTOY_MAX_PHY_DRIVE];
575
576 Count = GetPhysicalDriveCount();
577
578 for (i = 0; i < Count && i < VENTOY_MAX_PHY_DRIVE; i++)
579 {
580 PhyDriveId[i] = i;
581 }
582
583 dwBytes = GetLogicalDrives();
584 Log("Logical Drives: 0x%x", dwBytes);
585 while (dwBytes)
586 {
587 if (dwBytes & 0x01)
588 {
589 id = GetPhyDriveByLogicalDrive(Letter);
590 Log("%C --> %d", Letter, id);
591 if (id >= 0)
592 {
593 for (i = 0; i < Count; i++)
594 {
595 if (PhyDriveId[i] == id)
596 {
597 break;
598 }
599 }
600
601 if (i >= Count)
602 {
603 Log("Add phy%d to list", i);
604 PhyDriveId[Count] = id;
605 Count++;
606 }
607 }
608 }
609
610 Letter++;
611 dwBytes >>= 1;
612 }
613
614 for (i = 0; i < Count && DriveCount < VENTOY_MAX_PHY_DRIVE; i++)
615 {
616 CHECK_CLOSE_HANDLE(Handle);
617
618 safe_sprintf(PhyDrive, "\\\\.\\PhysicalDrive%d", PhyDriveId[i]);
619 Handle = CreateFileA(PhyDrive, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
620 Log("Create file Handle:%p %s status:%u", Handle, PhyDrive, LASTERR);
621
622 if (Handle == INVALID_HANDLE_VALUE)
623 {
624 continue;
625 }
626
627 bRet = DeviceIoControl(Handle,
628 IOCTL_DISK_GET_LENGTH_INFO, NULL,
629 0,
630 &LengthInfo,
631 sizeof(LengthInfo),
632 &dwBytes,
633 NULL);
634 if (!bRet)
635 {
636 Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR);
637 continue;
638 }
639
640 Log("PHYSICALDRIVE%d size %llu bytes", i, (ULONGLONG)LengthInfo.Length.QuadPart);
641
642 Query.PropertyId = StorageDeviceProperty;
643 Query.QueryType = PropertyStandardQuery;
644
645 bRet = DeviceIoControl(Handle,
646 IOCTL_STORAGE_QUERY_PROPERTY,
647 &Query,
648 sizeof(Query),
649 &DevDescHeader,
650 sizeof(STORAGE_DESCRIPTOR_HEADER),
651 &dwBytes,
652 NULL);
653 if (!bRet)
654 {
655 Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR, dwBytes);
656 continue;
657 }
658
659 pDevDesc = (STORAGE_DEVICE_DESCRIPTOR *)malloc(DevDescHeader.Size);
660 if (!pDevDesc)
661 {
662 Log("failed to malloc error:%u len:%u", LASTERR, DevDescHeader.Size);
663 continue;
664 }
665
666 bRet = DeviceIoControl(Handle,
667 IOCTL_STORAGE_QUERY_PROPERTY,
668 &Query,
669 sizeof(Query),
670 pDevDesc,
671 DevDescHeader.Size,
672 &dwBytes,
673 NULL);
674 if (!bRet)
675 {
676 Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR, dwBytes);
677 free(pDevDesc);
678 continue;
679 }
680
681 CurDrive->PhyDrive = i;
682 CurDrive->SizeInBytes = LengthInfo.Length.QuadPart;
683 CurDrive->DeviceType = pDevDesc->DeviceType;
684 CurDrive->RemovableMedia = pDevDesc->RemovableMedia;
685 CurDrive->BusType = pDevDesc->BusType;
686
687 if (pDevDesc->VendorIdOffset)
688 {
689 safe_strcpy(CurDrive->VendorId, (char *)pDevDesc + pDevDesc->VendorIdOffset);
690 TrimString(CurDrive->VendorId);
691 }
692
693 if (pDevDesc->ProductIdOffset)
694 {
695 safe_strcpy(CurDrive->ProductId, (char *)pDevDesc + pDevDesc->ProductIdOffset);
696 TrimString(CurDrive->ProductId);
697 }
698
699 if (pDevDesc->ProductRevisionOffset)
700 {
701 safe_strcpy(CurDrive->ProductRev, (char *)pDevDesc + pDevDesc->ProductRevisionOffset);
702 TrimString(CurDrive->ProductRev);
703 }
704
705 if (pDevDesc->SerialNumberOffset)
706 {
707 safe_strcpy(CurDrive->SerialNumber, (char *)pDevDesc + pDevDesc->SerialNumberOffset);
708 TrimString(CurDrive->SerialNumber);
709 }
710
711 CurDrive++;
712 DriveCount++;
713
714 free(pDevDesc);
715
716 CHECK_CLOSE_HANDLE(Handle);
717 }
718
719 for (i = 0, CurDrive = pDriveList; i < (int)DriveCount; i++, CurDrive++)
720 {
721 Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Name:%s %s",
722 CurDrive->PhyDrive, GetBusTypeString(CurDrive->BusType), CurDrive->RemovableMedia,
723 GetHumanReadableGBSize(CurDrive->SizeInBytes), CurDrive->SizeInBytes,
724 CurDrive->VendorId, CurDrive->ProductId);
725 }
726
727 *pDriveCount = DriveCount;
728
729 return 0;
730 }
731
732
733 static HANDLE g_FatPhyDrive;
734 static UINT64 g_Part2StartSec;
735 static int GetVentoyVersionFromFatFile(CHAR *VerBuf, size_t BufLen)
736 {
737 int rc = 1;
738 int size = 0;
739 char *buf = NULL;
740 void *flfile = NULL;
741
742 flfile = fl_fopen("/grub/grub.cfg", "rb");
743 if (flfile)
744 {
745 fl_fseek(flfile, 0, SEEK_END);
746 size = (int)fl_ftell(flfile);
747
748 fl_fseek(flfile, 0, SEEK_SET);
749
750 buf = (char *)malloc(size + 1);
751 if (buf)
752 {
753 fl_fread(buf, 1, size, flfile);
754 buf[size] = 0;
755
756 rc = 0;
757 sprintf_s(VerBuf, BufLen, "%s", ParseVentoyVersionFromString(buf));
758 free(buf);
759 }
760
761 fl_fclose(flfile);
762 }
763
764 return rc;
765 }
766
767 static int VentoyFatDiskRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
768 {
769 DWORD dwSize;
770 BOOL bRet;
771 DWORD ReadSize;
772 LARGE_INTEGER liCurrentPosition;
773
774 liCurrentPosition.QuadPart = Sector + g_Part2StartSec;
775 liCurrentPosition.QuadPart *= 512;
776 SetFilePointerEx(g_FatPhyDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
777
778 ReadSize = (DWORD)(SectorCount * 512);
779
780 bRet = ReadFile(g_FatPhyDrive, Buffer, ReadSize, &dwSize, NULL);
781 if (bRet == FALSE || dwSize != ReadSize)
782 {
783 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet, ReadSize, dwSize, LASTERR);
784 }
785
786 return 1;
787 }
788
789
790 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, CHAR *VerBuf, size_t BufLen)
791 {
792 int rc = 0;
793 HANDLE hDrive;
794
795 hDrive = GetPhysicalHandle(pDriveInfo->PhyDrive, FALSE, FALSE, FALSE);
796 if (hDrive == INVALID_HANDLE_VALUE)
797 {
798 return 1;
799 }
800
801 g_FatPhyDrive = hDrive;
802 g_Part2StartSec = (pDriveInfo->SizeInBytes - VENTOY_EFI_PART_SIZE) / 512;
803
804 Log("Parse FAT fs...");
805
806 fl_init();
807
808 if (0 == fl_attach_media(VentoyFatDiskRead, NULL))
809 {
810 rc = GetVentoyVersionFromFatFile(VerBuf, BufLen);
811 }
812 else
813 {
814 rc = 1;
815 }
816
817 fl_shutdown();
818
819 CHECK_CLOSE_HANDLE(hDrive);
820
821 return rc;
822 }
823
824
825
826
827
828 static unsigned int g_disk_unxz_len = 0;
829 static BYTE *g_part_img_pos = NULL;
830 static BYTE *g_part_img_buf[VENTOY_EFI_PART_SIZE / SIZE_1MB];
831
832
833 static int VentoyFatMemRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
834 {
835 uint32 i;
836 uint32 offset;
837 BYTE *MbBuf = NULL;
838
839 for (i = 0; i < SectorCount; i++)
840 {
841 offset = (Sector + i) * 512;
842
843 if (g_part_img_buf[1] == NULL)
844 {
845 MbBuf = g_part_img_buf[0] + offset;
846 memcpy(Buffer + i * 512, MbBuf, 512);
847 }
848 else
849 {
850 MbBuf = g_part_img_buf[offset / SIZE_1MB];
851 memcpy(Buffer + i * 512, MbBuf + (offset % SIZE_1MB), 512);
852 }
853 }
854
855 return 1;
856 }
857
858
859 static int VentoyFatMemWrite(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
860 {
861 uint32 i;
862 uint32 offset;
863 BYTE *MbBuf = NULL;
864
865 for (i = 0; i < SectorCount; i++)
866 {
867 offset = (Sector + i) * 512;
868
869 if (g_part_img_buf[1] == NULL)
870 {
871 MbBuf = g_part_img_buf[0] + offset;
872 memcpy(MbBuf, Buffer + i * 512, 512);
873 }
874 else
875 {
876 MbBuf = g_part_img_buf[offset / SIZE_1MB];
877 memcpy(MbBuf + (offset % SIZE_1MB), Buffer + i * 512, 512);
878 }
879 }
880
881 return 1;
882 }
883
884 int VentoyProcSecureBoot(BOOL SecureBoot)
885 {
886 int rc = 0;
887 int size;
888 char *filebuf = NULL;
889 void *file = NULL;
890
891 Log("VentoyProcSecureBoot %d ...", SecureBoot);
892
893 if (SecureBoot)
894 {
895 Log("Secure boot is enabled ...");
896 return 0;
897 }
898
899 fl_init();
900
901 if (0 == fl_attach_media(VentoyFatMemRead, VentoyFatMemWrite))
902 {
903 file = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
904 Log("Open ventoy efi file %p ", file);
905 if (file)
906 {
907 fl_fseek(file, 0, SEEK_END);
908 size = (int)fl_ftell(file);
909 fl_fseek(file, 0, SEEK_SET);
910
911 Log("ventoy efi file size %d ...", size);
912
913 filebuf = (char *)malloc(size);
914 if (filebuf)
915 {
916 fl_fread(filebuf, 1, size, file);
917 }
918
919 fl_fclose(file);
920
921 Log("Now delete all efi files ...");
922 fl_remove("/EFI/BOOT/BOOTX64.EFI");
923 fl_remove("/EFI/BOOT/grubx64.efi");
924 fl_remove("/EFI/BOOT/grubx64_real.efi");
925 fl_remove("/EFI/BOOT/MokManager.efi");
926
927 file = fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb");
928 Log("Open bootx64 efi file %p ", file);
929 if (file)
930 {
931 fl_fwrite(filebuf, 1, size, file);
932 fl_fflush(file);
933 fl_fclose(file);
934 }
935 }
936 }
937 else
938 {
939 rc = 1;
940 }
941
942 fl_shutdown();
943
944 return rc;
945 }
946
947
948
949 static int disk_xz_flush(void *src, unsigned int size)
950 {
951 unsigned int i;
952 BYTE *buf = (BYTE *)src;
953
954 for (i = 0; i < size; i++)
955 {
956 *g_part_img_pos = *buf++;
957
958 g_disk_unxz_len++;
959 if ((g_disk_unxz_len % SIZE_1MB) == 0)
960 {
961 g_part_img_pos = g_part_img_buf[g_disk_unxz_len / SIZE_1MB];
962 }
963 else
964 {
965 g_part_img_pos++;
966 }
967 }
968
969 return (int)size;
970 }
971
972 static void unxz_error(char *x)
973 {
974 Log("%s", x);
975 }
976
977 static BOOL TryWritePart2(HANDLE hDrive, UINT64 StartSectorId)
978 {
979 BOOL bRet;
980 DWORD TrySize = 16 * 1024;
981 DWORD dwSize;
982 BYTE *Buffer = NULL;
983 unsigned char *data = NULL;
984 LARGE_INTEGER liCurrentPosition;
985
986 liCurrentPosition.QuadPart = StartSectorId * 512;
987 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
988
989 Buffer = malloc(TrySize);
990
991 bRet = WriteFile(hDrive, Buffer, TrySize, &dwSize, NULL);
992
993 free(Buffer);
994
995 Log("Try write part2 bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
996
997 if (bRet && dwSize == TrySize)
998 {
999 return TRUE;
1000 }
1001
1002 return FALSE;
1003 }
1004
1005 static int FormatPart2Fat(HANDLE hDrive, UINT64 StartSectorId)
1006 {
1007 int i;
1008 int rc = 0;
1009 int len = 0;
1010 int writelen = 0;
1011 int partwrite = 0;
1012 DWORD dwSize = 0;
1013 BOOL bRet;
1014 unsigned char *data = NULL;
1015 LARGE_INTEGER liCurrentPosition;
1016
1017 Log("FormatPart2Fat ...");
1018
1019 rc = ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG, 0, (void **)&data, &len);
1020 if (rc)
1021 {
1022 Log("Failed to read img file %p %u", data, len);
1023 return 1;
1024 }
1025
1026 liCurrentPosition.QuadPart = StartSectorId * 512;
1027 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
1028
1029 memset(g_part_img_buf, 0, sizeof(g_part_img_buf));
1030
1031 g_part_img_buf[0] = (BYTE *)malloc(VENTOY_EFI_PART_SIZE);
1032 if (g_part_img_buf[0])
1033 {
1034 Log("Malloc whole img buffer success, now decompress ...");
1035 unxz(data, len, NULL, NULL, g_part_img_buf[0], &writelen, unxz_error);
1036
1037 if (len == writelen)
1038 {
1039 Log("decompress finished success");
1040
1041 VentoyProcSecureBoot(g_SecureBoot);
1042
1043 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1044 {
1045 dwSize = 0;
1046 bRet = WriteFile(hDrive, g_part_img_buf[0] + i * SIZE_1MB, SIZE_1MB, &dwSize, NULL);
1047 Log("Write part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
1048
1049 if (!bRet)
1050 {
1051 rc = 1;
1052 goto End;
1053 }
1054
1055 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START + i);
1056 }
1057 }
1058 else
1059 {
1060 rc = 1;
1061 Log("decompress finished failed");
1062 goto End;
1063 }
1064 }
1065 else
1066 {
1067 Log("Failed to malloc whole img size %u, now split it", VENTOY_EFI_PART_SIZE);
1068
1069 partwrite = 1;
1070 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1071 {
1072 g_part_img_buf[i] = (BYTE *)malloc(SIZE_1MB);
1073 if (g_part_img_buf[i] == NULL)
1074 {
1075 rc = 1;
1076 goto End;
1077 }
1078 }
1079
1080 Log("Malloc part img buffer success, now decompress ...");
1081
1082 g_part_img_pos = g_part_img_buf[0];
1083
1084 unxz(data, len, NULL, disk_xz_flush, NULL, NULL, unxz_error);
1085
1086 if (g_disk_unxz_len == VENTOY_EFI_PART_SIZE)
1087 {
1088 Log("decompress finished success");
1089
1090 VentoyProcSecureBoot(g_SecureBoot);
1091
1092 for (int i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1093 {
1094 dwSize = 0;
1095 bRet = WriteFile(hDrive, g_part_img_buf[i], SIZE_1MB, &dwSize, NULL);
1096 Log("Write part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
1097
1098 if (!bRet)
1099 {
1100 rc = 1;
1101 goto End;
1102 }
1103
1104 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START + i);
1105 }
1106 }
1107 else
1108 {
1109 rc = 1;
1110 Log("decompress finished failed");
1111 goto End;
1112 }
1113 }
1114
1115 End:
1116
1117 if (data) free(data);
1118
1119 if (partwrite)
1120 {
1121 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1122 {
1123 if (g_part_img_buf[i]) free(g_part_img_buf[i]);
1124 }
1125 }
1126 else
1127 {
1128 if (g_part_img_buf[0]) free(g_part_img_buf[0]);
1129 }
1130
1131 return rc;
1132 }
1133
1134 static int WriteGrubStage1ToPhyDrive(HANDLE hDrive)
1135 {
1136 int Len = 0;
1137 int readLen = 0;
1138 BOOL bRet;
1139 DWORD dwSize;
1140 BYTE *ImgBuf = NULL;
1141 BYTE *RawBuf = NULL;
1142
1143 Log("WriteGrubStage1ToPhyDrive ...");
1144
1145 RawBuf = (BYTE *)malloc(SIZE_1MB);
1146 if (!RawBuf)
1147 {
1148 return 1;
1149 }
1150
1151 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG, 0, (void **)&ImgBuf, &Len))
1152 {
1153 Log("Failed to read stage1 img");
1154 free(RawBuf);
1155 return 1;
1156 }
1157
1158 unxz(ImgBuf, Len, NULL, NULL, RawBuf, &readLen, unxz_error);
1159
1160 SetFilePointer(hDrive, 512, NULL, FILE_BEGIN);
1161
1162 bRet = WriteFile(hDrive, RawBuf, SIZE_1MB - 512, &dwSize, NULL);
1163 Log("WriteFile Ret:%u dwSize:%u ErrCode:%u", bRet, dwSize, GetLastError());
1164
1165 free(RawBuf);
1166 free(ImgBuf);
1167 return 0;
1168 }
1169
1170
1171
1172 static int FormatPart1exFAT(UINT64 DiskSizeBytes)
1173 {
1174 MKFS_PARM Option;
1175 FRESULT Ret;
1176 FATFS fs;
1177
1178 Option.fmt = FM_EXFAT;
1179 Option.n_fat = 1;
1180 Option.align = 8;
1181 Option.n_root = 1;
1182
1183 // < 32GB select 32KB as cluster size
1184 // > 32GB select 128KB as cluster size
1185 if (DiskSizeBytes / 1024 / 1024 / 1024 <= 32)
1186 {
1187 Option.au_size = 32768;
1188 }
1189 else
1190 {
1191 Option.au_size = 131072;
1192 }
1193
1194 Log("Formatting Part1 exFAT ...");
1195
1196 Ret = f_mkfs(TEXT("0:"), &Option, 0, 8 * 1024 * 1024);
1197
1198 if (FR_OK == Ret)
1199 {
1200 Log("Formatting Part1 exFAT success");
1201
1202 Ret = f_mount(&fs, TEXT("0:"), 1);
1203 Log("mount part %d", Ret);
1204
1205 if (FR_OK == Ret)
1206 {
1207 Ret = f_setlabel(TEXT("Ventoy"));
1208 Log("f_setlabel %d", Ret);
1209
1210 Ret = f_mount(0, TEXT("0:"), 1);
1211 Log("umount part %d", Ret);
1212 }
1213
1214 return 0;
1215 }
1216 else
1217 {
1218 Log("Formatting Part1 exFAT failed");
1219 return 1;
1220 }
1221 }
1222
1223
1224 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
1225 {
1226 int i;
1227 int rc = 0;
1228 int state = 0;
1229 HANDLE hDrive;
1230 DWORD dwSize;
1231 BOOL bRet;
1232 CHAR MountDrive;
1233 CHAR DriveName[] = "?:\\";
1234 CHAR DriveLetters[MAX_PATH] = { 0 };
1235 MBR_HEAD MBR;
1236
1237 Log("InstallVentoy2PhyDrive PhyDrive%d <<%s %s %dGB>>",
1238 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1239 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1240
1241 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1242
1243 VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR);
1244
1245 Log("Lock disk for clean ............................. ");
1246
1247 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1248 if (hDrive == INVALID_HANDLE_VALUE)
1249 {
1250 Log("Failed to open physical disk");
1251 return 1;
1252 }
1253
1254 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1255
1256 if (DriveLetters[0] == 0)
1257 {
1258 Log("No drive letter was assigned...");
1259 DriveName[0] = GetFirstUnusedDriveLetter();
1260 Log("GetFirstUnusedDriveLetter %C: ...", DriveName[0]);
1261 }
1262 else
1263 {
1264 // Unmount all mounted volumes that belong to this drive
1265 // Do it in reverse so that we always end on the first volume letter
1266 for (i = (int)strlen(DriveLetters); i > 0; i--)
1267 {
1268 DriveName[0] = DriveLetters[i - 1];
1269 bRet = DeleteVolumeMountPointA(DriveName);
1270 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, GetLastError());
1271 }
1272 }
1273
1274 MountDrive = DriveName[0];
1275 Log("Will use '%C:' as volume mountpoint", DriveName[0]);
1276
1277 // It kind of blows, but we have to relinquish access to the physical drive
1278 // for VDS to be able to delete the partitions that reside on it...
1279 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1280 CHECK_CLOSE_HANDLE(hDrive);
1281
1282 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
1283
1284 if (!DeletePartitions(pPhyDrive->PhyDrive, FALSE))
1285 {
1286 Log("Notice: Could not delete partitions: %u", GetLastError());
1287 }
1288
1289 Log("Deleting all partitions ......................... OK");
1290
1291 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1292
1293 Log("Lock disk for write ............................. ");
1294 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1295 if (hDrive == INVALID_HANDLE_VALUE)
1296 {
1297 Log("Failed to GetPhysicalHandle for write.");
1298 rc = 1;
1299 goto End;
1300 }
1301
1302 //Refresh Drive Layout
1303 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1304
1305 disk_io_set_param(hDrive, MBR.PartTbl[0].StartSectorId + MBR.PartTbl[0].SectorCount);
1306
1307 PROGRESS_BAR_SET_POS(PT_FORMAT_PART1);
1308
1309 Log("Formatting part1 exFAT ...");
1310 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
1311 {
1312 rc = 1;
1313 goto End;
1314 }
1315
1316 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
1317 Log("Writing part2 FAT img ...");
1318 if (0 != FormatPart2Fat(hDrive, MBR.PartTbl[1].StartSectorId))
1319 {
1320 rc = 1;
1321 goto End;
1322 }
1323
1324 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
1325 Log("Writting Boot Image ............................. ");
1326 if (WriteGrubStage1ToPhyDrive(hDrive) != 0)
1327 {
1328 rc = 1;
1329 goto End;
1330 }
1331
1332
1333 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
1334 Log("Writting Partition Table ........................ ");
1335 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
1336 if (!WriteFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL))
1337 {
1338 rc = 1;
1339 Log("Write MBR Failed, dwSize:%u ErrCode:%u", dwSize, GetLastError());
1340 goto End;
1341 }
1342
1343 Log("Write MBR OK ...");
1344
1345 //Refresh Drive Layout
1346 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1347
1348 End:
1349 CHECK_CLOSE_HANDLE(hDrive);
1350
1351 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1352 Log("Mounting Ventoy Partition ....................... ");
1353 Sleep(1000);
1354
1355 state = 0;
1356 memset(DriveLetters, 0, sizeof(DriveLetters));
1357 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1358 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1359
1360 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1361 {
1362 DriveName[0] = DriveLetters[i];
1363 if (IsVentoyLogicalDrive(DriveName[0]))
1364 {
1365 Log("%s is ventoy part2, delete mountpoint", DriveName);
1366 DeleteVolumeMountPointA(DriveName);
1367 }
1368 else
1369 {
1370 Log("%s is ventoy part1, already mounted", DriveName);
1371 state = 1;
1372 }
1373 }
1374
1375 if (state != 1)
1376 {
1377 Log("need to mount ventoy part1...");
1378 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[0].StartSectorId, DriveLetters, sizeof(DriveLetters), FALSE))
1379 {
1380 DriveName[0] = MountDrive;
1381 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1382 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1383 }
1384 else
1385 {
1386 Log("Failed to find ventoy volume");
1387 }
1388 }
1389
1390 Log("OK\n");
1391
1392 return rc;
1393 }
1394
1395 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive)
1396 {
1397 int i;
1398 int rc = 0;
1399 BOOL ForceMBR = FALSE;
1400 HANDLE hVolume;
1401 HANDLE hDrive;
1402 DWORD Status;
1403 DWORD dwSize;
1404 BOOL bRet;
1405 CHAR DriveName[] = "?:\\";
1406 CHAR DriveLetters[MAX_PATH] = { 0 };
1407 UINT32 StartSector;
1408 MBR_HEAD BootImg;
1409 MBR_HEAD MBR;
1410
1411 StartSector = (UINT32)(pPhyDrive->SizeInBytes / 512 - VENTOY_EFI_PART_SIZE / 512);
1412
1413 Log("UpdateVentoy2PhyDrive PhyDrive%d <<%s %s %dGB>>",
1414 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1415 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1416
1417 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1418
1419 Log("Lock disk for umount ............................ ");
1420
1421 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1422 if (hDrive == INVALID_HANDLE_VALUE)
1423 {
1424 Log("Failed to open physical disk");
1425 return 1;
1426 }
1427
1428 // Read MBR
1429 ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
1430
1431 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1432
1433 if (DriveLetters[0] == 0)
1434 {
1435 Log("No drive letter was assigned...");
1436 }
1437 else
1438 {
1439 // Unmount all mounted volumes that belong to this drive
1440 // Do it in reverse so that we always end on the first volume letter
1441 for (i = (int)strlen(DriveLetters); i > 0; i--)
1442 {
1443 DriveName[0] = DriveLetters[i - 1];
1444 if (IsVentoyLogicalDrive(DriveName[0]))
1445 {
1446 Log("%s is ventoy logical drive", DriveName);
1447 bRet = DeleteVolumeMountPointA(DriveName);
1448 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, LASTERR);
1449 break;
1450 }
1451 }
1452 }
1453
1454 // It kind of blows, but we have to relinquish access to the physical drive
1455 // for VDS to be able to delete the partitions that reside on it...
1456 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1457 CHECK_CLOSE_HANDLE(hDrive);
1458
1459 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1460
1461 Log("Lock disk for update ............................ ");
1462 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1463 if (hDrive == INVALID_HANDLE_VALUE)
1464 {
1465 Log("Failed to GetPhysicalHandle for write.");
1466 rc = 1;
1467 goto End;
1468 }
1469
1470 PROGRESS_BAR_SET_POS(PT_LOCK_VOLUME);
1471
1472 Log("Lock volume for update .......................... ");
1473 hVolume = INVALID_HANDLE_VALUE;
1474 Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, MBR.PartTbl[1].StartSectorId, DriveLetters, sizeof(DriveLetters), TRUE);
1475 if (ERROR_SUCCESS == Status)
1476 {
1477 Log("Now lock and dismount volume <%s>", DriveLetters);
1478 hVolume = CreateFileA(DriveLetters,
1479 GENERIC_READ | GENERIC_WRITE,
1480 FILE_SHARE_READ,
1481 NULL,
1482 OPEN_EXISTING,
1483 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
1484 NULL);
1485
1486 if (hVolume == INVALID_HANDLE_VALUE)
1487 {
1488 Log("Failed to create file volume, errcode:%u", LASTERR);
1489 rc = 1;
1490 goto End;
1491 }
1492
1493 bRet = DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1494 Log("FSCTL_LOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
1495
1496 bRet = DeviceIoControl(hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1497 Log("FSCTL_DISMOUNT_VOLUME bRet:%u code:%u", bRet, LASTERR);
1498 }
1499 else if (ERROR_NOT_FOUND == Status)
1500 {
1501 Log("Volume not found, maybe not supported");
1502 }
1503 else
1504 {
1505 rc = 1;
1506 goto End;
1507 }
1508
1509
1510 if (!TryWritePart2(hDrive, StartSector))
1511 {
1512 ForceMBR = TRUE;
1513 Log("Try write failed, now delete partition 2...");
1514
1515 CHECK_CLOSE_HANDLE(hDrive);
1516
1517 Log("Now delete partition 2...");
1518 DeletePartitions(pPhyDrive->PhyDrive, TRUE);
1519
1520 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1521 if (hDrive == INVALID_HANDLE_VALUE)
1522 {
1523 Log("Failed to GetPhysicalHandle for write.");
1524 rc = 1;
1525 goto End;
1526 }
1527 }
1528
1529
1530 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
1531
1532 Log("Write Ventoy to disk ............................ ");
1533 if (0 != FormatPart2Fat(hDrive, StartSector))
1534 {
1535 rc = 1;
1536 goto End;
1537 }
1538
1539 if (hVolume != INVALID_HANDLE_VALUE)
1540 {
1541 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1542 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
1543 CHECK_CLOSE_HANDLE(hVolume);
1544 }
1545
1546 Log("Updating Boot Image ............................. ");
1547 if (WriteGrubStage1ToPhyDrive(hDrive) != 0)
1548 {
1549 rc = 1;
1550 goto End;
1551 }
1552
1553 // Boot Image
1554 VentoyGetLocalBootImg(&BootImg);
1555
1556 // Use Old UUID
1557 memcpy(BootImg.BootCode + 0x180, MBR.BootCode + 0x180, 16);
1558
1559 if (ForceMBR == FALSE && memcmp(BootImg.BootCode, MBR.BootCode, 440) == 0)
1560 {
1561 Log("Boot image has no difference, no need to write.");
1562 }
1563 else
1564 {
1565 Log("Boot image need to write %u.", ForceMBR);
1566
1567 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
1568
1569 memcpy(MBR.BootCode, BootImg.BootCode, 440);
1570 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
1571 Log("Write Boot Image ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
1572 }
1573
1574 //Refresh Drive Layout
1575 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1576
1577 End:
1578 CHECK_CLOSE_HANDLE(hDrive);
1579
1580 return rc;
1581 }
1582