]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
256a9cf28706b63d03f4e2486f18237e0fea17af
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / PhyDrive.c
1 /******************************************************************************
2 * PhyDrive.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 * Copyright (c) 2011-2020, Pete Batard <pete@akeo.ie>
6 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 #include <Windows.h>
23 #include <time.h>
24 #include <winternl.h>
25 #include <commctrl.h>
26 #include <initguid.h>
27 #include "resource.h"
28 #include "Language.h"
29 #include "Ventoy2Disk.h"
30 #include "fat_filelib.h"
31 #include "ff.h"
32 #include "DiskService.h"
33
34 static int g_backup_bin_index = 0;
35
36
37 static BOOL WriteDataToPhyDisk(HANDLE hDrive, UINT64 Offset, VOID *buffer, DWORD len)
38 {
39 BOOL bRet;
40 DWORD dwSize = 0;
41 LARGE_INTEGER liCurPosition;
42 LARGE_INTEGER liNewPosition;
43
44 liCurPosition.QuadPart = (LONGLONG)Offset;
45 liNewPosition.QuadPart = 0;
46 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
47 liNewPosition.QuadPart != liCurPosition.QuadPart)
48 {
49 Log("SetFilePointerEx Failed %u", LASTERR);
50 return FALSE;
51 }
52
53 bRet = WriteFile(hDrive, buffer, len, &dwSize, NULL);
54 if (bRet == FALSE || dwSize != len)
55 {
56 Log("Write file error %u %u", dwSize, LASTERR);
57 return FALSE;
58 }
59
60 return TRUE;
61 }
62
63
64 static DWORD GetVentoyVolumeName(int PhyDrive, UINT64 StartSectorId, CHAR *NameBuf, UINT32 BufLen, BOOL DelSlash)
65 {
66 size_t len;
67 BOOL bRet;
68 DWORD dwSize;
69 HANDLE hDrive;
70 HANDLE hVolume;
71 UINT64 PartOffset;
72 DWORD Status = ERROR_NOT_FOUND;
73 DISK_EXTENT *pExtents = NULL;
74 CHAR VolumeName[MAX_PATH] = { 0 };
75 VOLUME_DISK_EXTENTS DiskExtents;
76
77 PartOffset = 512ULL * StartSectorId;
78
79 Log("GetVentoyVolumeName PhyDrive %d SectorStart:%llu PartOffset:%llu", PhyDrive, (ULONGLONG)StartSectorId, (ULONGLONG)PartOffset);
80
81 hVolume = FindFirstVolumeA(VolumeName, sizeof(VolumeName));
82 if (hVolume == INVALID_HANDLE_VALUE)
83 {
84 return 1;
85 }
86
87 do {
88
89 len = strlen(VolumeName);
90 Log("Find volume:%s", VolumeName);
91
92 VolumeName[len - 1] = 0;
93
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)
96 {
97 continue;
98 }
99
100 bRet = DeviceIoControl(hDrive,
101 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
102 NULL,
103 0,
104 &DiskExtents,
105 (DWORD)(sizeof(DiskExtents)),
106 (LPDWORD)&dwSize,
107 NULL);
108
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);
111
112 if (bRet && DiskExtents.NumberOfDiskExtents == 1)
113 {
114 pExtents = DiskExtents.Extents;
115
116 Log("This volume DiskNumber:%u offset:%llu", pExtents->DiskNumber, (ULONGLONG)pExtents->StartingOffset.QuadPart);
117 if ((int)pExtents->DiskNumber == PhyDrive && pExtents->StartingOffset.QuadPart == PartOffset)
118 {
119 Log("This volume match");
120
121 if (!DelSlash)
122 {
123 VolumeName[len - 1] = '\\';
124 }
125
126 sprintf_s(NameBuf, BufLen, "%s", VolumeName);
127 Status = ERROR_SUCCESS;
128 CloseHandle(hDrive);
129 break;
130 }
131 }
132
133 CloseHandle(hDrive);
134 } while (FindNextVolumeA(hVolume, VolumeName, sizeof(VolumeName)));
135
136 FindVolumeClose(hVolume);
137
138 Log("GetVentoyVolumeName return %u", Status);
139 return Status;
140 }
141
142 static int GetLettersBelongPhyDrive(int PhyDrive, char *DriveLetters, size_t Length)
143 {
144 int n = 0;
145 DWORD DataSize = 0;
146 CHAR *Pos = NULL;
147 CHAR *StringBuf = NULL;
148
149 DataSize = GetLogicalDriveStringsA(0, NULL);
150 StringBuf = (CHAR *)malloc(DataSize + 1);
151 if (StringBuf == NULL)
152 {
153 return 1;
154 }
155
156 GetLogicalDriveStringsA(DataSize, StringBuf);
157
158 for (Pos = StringBuf; *Pos; Pos += strlen(Pos) + 1)
159 {
160 if (n < (int)Length && PhyDrive == GetPhyDriveByLogicalDrive(Pos[0], NULL))
161 {
162 Log("%C: is belong to phydrive%d", Pos[0], PhyDrive);
163 DriveLetters[n++] = Pos[0];
164 }
165 }
166
167 free(StringBuf);
168 return 0;
169 }
170
171 HANDLE GetPhysicalHandle(int Drive, BOOLEAN bLockDrive, BOOLEAN bWriteAccess, BOOLEAN bWriteShare)
172 {
173 int i;
174 DWORD dwSize;
175 DWORD LastError;
176 UINT64 EndTime;
177 HANDLE hDrive = INVALID_HANDLE_VALUE;
178 CHAR PhyDrive[128];
179 CHAR DevPath[MAX_PATH] = { 0 };
180
181 safe_sprintf(PhyDrive, "\\\\.\\PhysicalDrive%d", Drive);
182
183 if (0 == QueryDosDeviceA(PhyDrive + 4, DevPath, sizeof(DevPath)))
184 {
185 Log("QueryDosDeviceA failed error:%u", GetLastError());
186 strcpy_s(DevPath, sizeof(DevPath), "???");
187 }
188 else
189 {
190 Log("QueryDosDeviceA success %s", DevPath);
191 }
192
193 for (i = 0; i < DRIVE_ACCESS_RETRIES; i++)
194 {
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),
203 NULL,
204 OPEN_EXISTING,
205 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
206 NULL);
207
208 LastError = GetLastError();
209 Log("[%d] CreateFileA %s code:%u %p", i, PhyDrive, LastError, hDrive);
210
211 if (hDrive != INVALID_HANDLE_VALUE)
212 {
213 break;
214 }
215
216 if ((LastError != ERROR_SHARING_VIOLATION) && (LastError != ERROR_ACCESS_DENIED))
217 {
218 break;
219 }
220
221 if (i == 0)
222 {
223 Log("Waiting for access on %s [%s]...", PhyDrive, DevPath);
224 }
225 else if (!bWriteShare && (i > DRIVE_ACCESS_RETRIES / 3))
226 {
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...");
229 bWriteShare = TRUE;
230
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;
234
235 }
236 Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
237 }
238
239 if (hDrive == INVALID_HANDLE_VALUE)
240 {
241 Log("Could not open %s %u", PhyDrive, LASTERR);
242 goto End;
243 }
244
245 if (bWriteAccess)
246 {
247 Log("Opened %s for %s write access", PhyDrive, bWriteShare ? "shared" : "exclusive");
248 }
249
250 if (bLockDrive)
251 {
252 if (DeviceIoControl(hDrive, FSCTL_ALLOW_EXTENDED_DASD_IO, NULL, 0, NULL, 0, &dwSize, NULL))
253 {
254 Log("I/O boundary checks disabled");
255 }
256
257 EndTime = GetTickCount64() + DRIVE_ACCESS_TIMEOUT;
258
259 do {
260 if (DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL))
261 {
262 Log("FSCTL_LOCK_VOLUME success");
263 goto End;
264 }
265 Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
266 } while (GetTickCount64() < EndTime);
267
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);
270
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);
277
278 CHECK_CLOSE_HANDLE(hDrive);
279 }
280
281 End:
282
283 if (hDrive == INVALID_HANDLE_VALUE)
284 {
285 Log("Can get handle of %s, maybe some process control it.", DevPath);
286 }
287
288 return hDrive;
289 }
290
291 int GetPhyDriveByLogicalDrive(int DriveLetter, UINT64 *Offset)
292 {
293 BOOL Ret = FALSE;
294 DWORD dwSize = 0;
295 HANDLE Handle = INVALID_HANDLE_VALUE;
296 VOLUME_DISK_EXTENTS DiskExtents;
297 CHAR PhyPath[128];
298
299 safe_sprintf(PhyPath, "\\\\.\\%C:", (CHAR)DriveLetter);
300
301 Handle = CreateFileA(PhyPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
302 if (Handle == INVALID_HANDLE_VALUE)
303 {
304 Log("Could not open the disk<%s>, error:%u", PhyPath, LASTERR);
305 return -1;
306 }
307
308 memset(&DiskExtents, 0, sizeof(DiskExtents));
309 Ret = DeviceIoControl(Handle,
310 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
311 NULL,
312 0,
313 &DiskExtents,
314 (DWORD)(sizeof(DiskExtents)),
315 (LPDWORD)&dwSize,
316 NULL);
317
318 if (!Ret || DiskExtents.NumberOfDiskExtents == 0)
319 {
320 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath, LASTERR);
321 CHECK_CLOSE_HANDLE(Handle);
322 return -1;
323 }
324 CHECK_CLOSE_HANDLE(Handle);
325
326 Log("LogicalDrive:%s PhyDrive:%d Num:%d Offset:%llu ExtentLength:%llu",
327 PhyPath,
328 DiskExtents.Extents[0].DiskNumber,
329 DiskExtents.NumberOfDiskExtents,
330 DiskExtents.Extents[0].StartingOffset.QuadPart,
331 DiskExtents.Extents[0].ExtentLength.QuadPart
332 );
333
334 if (Offset)
335 {
336 *Offset = (UINT64)(DiskExtents.Extents[0].StartingOffset.QuadPart);
337 }
338
339 return (int)DiskExtents.Extents[0].DiskNumber;
340 }
341
342 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount)
343 {
344 int i;
345 int Count;
346 int id;
347 int Letter = 'A';
348 BOOL bRet;
349 DWORD dwBytes;
350 DWORD DriveCount = 0;
351 HANDLE Handle = INVALID_HANDLE_VALUE;
352 CHAR PhyDrive[128];
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 int PhyDriveId[VENTOY_MAX_PHY_DRIVE];
359
360 Count = GetPhysicalDriveCount();
361
362 for (i = 0; i < Count && i < VENTOY_MAX_PHY_DRIVE; i++)
363 {
364 PhyDriveId[i] = i;
365 }
366
367 dwBytes = GetLogicalDrives();
368 Log("Logical Drives: 0x%x", dwBytes);
369 while (dwBytes)
370 {
371 if (dwBytes & 0x01)
372 {
373 id = GetPhyDriveByLogicalDrive(Letter, NULL);
374 Log("%C --> %d", Letter, id);
375 if (id >= 0)
376 {
377 for (i = 0; i < Count; i++)
378 {
379 if (PhyDriveId[i] == id)
380 {
381 break;
382 }
383 }
384
385 if (i >= Count)
386 {
387 Log("Add phy%d to list", i);
388 PhyDriveId[Count] = id;
389 Count++;
390 }
391 }
392 }
393
394 Letter++;
395 dwBytes >>= 1;
396 }
397
398 for (i = 0; i < Count && DriveCount < VENTOY_MAX_PHY_DRIVE; i++)
399 {
400 CHECK_CLOSE_HANDLE(Handle);
401
402 safe_sprintf(PhyDrive, "\\\\.\\PhysicalDrive%d", PhyDriveId[i]);
403 Handle = CreateFileA(PhyDrive, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
404 Log("Create file Handle:%p %s status:%u", Handle, PhyDrive, LASTERR);
405
406 if (Handle == INVALID_HANDLE_VALUE)
407 {
408 continue;
409 }
410
411 bRet = DeviceIoControl(Handle,
412 IOCTL_DISK_GET_LENGTH_INFO, NULL,
413 0,
414 &LengthInfo,
415 sizeof(LengthInfo),
416 &dwBytes,
417 NULL);
418 if (!bRet)
419 {
420 Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR);
421 continue;
422 }
423
424 Log("PHYSICALDRIVE%d size %llu bytes", i, (ULONGLONG)LengthInfo.Length.QuadPart);
425
426 Query.PropertyId = StorageDeviceProperty;
427 Query.QueryType = PropertyStandardQuery;
428
429 bRet = DeviceIoControl(Handle,
430 IOCTL_STORAGE_QUERY_PROPERTY,
431 &Query,
432 sizeof(Query),
433 &DevDescHeader,
434 sizeof(STORAGE_DESCRIPTOR_HEADER),
435 &dwBytes,
436 NULL);
437 if (!bRet)
438 {
439 Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR, dwBytes);
440 continue;
441 }
442
443 if (DevDescHeader.Size < sizeof(STORAGE_DEVICE_DESCRIPTOR))
444 {
445 Log("Invalid DevDescHeader.Size:%u", DevDescHeader.Size);
446 continue;
447 }
448
449 pDevDesc = (STORAGE_DEVICE_DESCRIPTOR *)malloc(DevDescHeader.Size);
450 if (!pDevDesc)
451 {
452 Log("failed to malloc error:%u len:%u", LASTERR, DevDescHeader.Size);
453 continue;
454 }
455
456 bRet = DeviceIoControl(Handle,
457 IOCTL_STORAGE_QUERY_PROPERTY,
458 &Query,
459 sizeof(Query),
460 pDevDesc,
461 DevDescHeader.Size,
462 &dwBytes,
463 NULL);
464 if (!bRet)
465 {
466 Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR, dwBytes);
467 free(pDevDesc);
468 continue;
469 }
470
471 CurDrive->PhyDrive = i;
472 CurDrive->SizeInBytes = LengthInfo.Length.QuadPart;
473 CurDrive->DeviceType = pDevDesc->DeviceType;
474 CurDrive->RemovableMedia = pDevDesc->RemovableMedia;
475 CurDrive->BusType = pDevDesc->BusType;
476
477 if (pDevDesc->VendorIdOffset)
478 {
479 safe_strcpy(CurDrive->VendorId, (char *)pDevDesc + pDevDesc->VendorIdOffset);
480 TrimString(CurDrive->VendorId);
481 }
482
483 if (pDevDesc->ProductIdOffset)
484 {
485 safe_strcpy(CurDrive->ProductId, (char *)pDevDesc + pDevDesc->ProductIdOffset);
486 TrimString(CurDrive->ProductId);
487 }
488
489 if (pDevDesc->ProductRevisionOffset)
490 {
491 safe_strcpy(CurDrive->ProductRev, (char *)pDevDesc + pDevDesc->ProductRevisionOffset);
492 TrimString(CurDrive->ProductRev);
493 }
494
495 if (pDevDesc->SerialNumberOffset)
496 {
497 safe_strcpy(CurDrive->SerialNumber, (char *)pDevDesc + pDevDesc->SerialNumberOffset);
498 TrimString(CurDrive->SerialNumber);
499 }
500
501 CurDrive++;
502 DriveCount++;
503
504 free(pDevDesc);
505
506 CHECK_CLOSE_HANDLE(Handle);
507 }
508
509 for (i = 0, CurDrive = pDriveList; i < (int)DriveCount; i++, CurDrive++)
510 {
511 Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Name:%s %s",
512 CurDrive->PhyDrive, GetBusTypeString(CurDrive->BusType), CurDrive->RemovableMedia,
513 GetHumanReadableGBSize(CurDrive->SizeInBytes), CurDrive->SizeInBytes,
514 CurDrive->VendorId, CurDrive->ProductId);
515 }
516
517 *pDriveCount = DriveCount;
518
519 return 0;
520 }
521
522
523 static HANDLE g_FatPhyDrive;
524 static UINT64 g_Part2StartSec;
525 static int GetVentoyVersionFromFatFile(CHAR *VerBuf, size_t BufLen)
526 {
527 int rc = 1;
528 int size = 0;
529 char *buf = NULL;
530 void *flfile = NULL;
531
532 flfile = fl_fopen("/grub/grub.cfg", "rb");
533 if (flfile)
534 {
535 fl_fseek(flfile, 0, SEEK_END);
536 size = (int)fl_ftell(flfile);
537
538 fl_fseek(flfile, 0, SEEK_SET);
539
540 buf = (char *)malloc(size + 1);
541 if (buf)
542 {
543 fl_fread(buf, 1, size, flfile);
544 buf[size] = 0;
545
546 rc = 0;
547 sprintf_s(VerBuf, BufLen, "%s", ParseVentoyVersionFromString(buf));
548 free(buf);
549 }
550
551 fl_fclose(flfile);
552 }
553
554 return rc;
555 }
556
557 static int VentoyFatDiskRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
558 {
559 DWORD dwSize;
560 BOOL bRet;
561 DWORD ReadSize;
562 LARGE_INTEGER liCurrentPosition;
563
564 liCurrentPosition.QuadPart = Sector + g_Part2StartSec;
565 liCurrentPosition.QuadPart *= 512;
566 SetFilePointerEx(g_FatPhyDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
567
568 ReadSize = (DWORD)(SectorCount * 512);
569
570 bRet = ReadFile(g_FatPhyDrive, Buffer, ReadSize, &dwSize, NULL);
571 if (bRet == FALSE || dwSize != ReadSize)
572 {
573 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet, ReadSize, dwSize, LASTERR);
574 }
575
576 return 1;
577 }
578
579
580 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen, BOOL *pSecureBoot)
581 {
582 int rc = 0;
583 HANDLE hDrive;
584 void *flfile;
585
586 hDrive = GetPhysicalHandle(pDriveInfo->PhyDrive, FALSE, FALSE, FALSE);
587 if (hDrive == INVALID_HANDLE_VALUE)
588 {
589 return 1;
590 }
591
592 g_FatPhyDrive = hDrive;
593 g_Part2StartSec = Part2StartSector;
594
595 Log("Parse FAT fs...");
596
597 fl_init();
598
599 if (0 == fl_attach_media(VentoyFatDiskRead, NULL))
600 {
601 Log("attach media success...");
602 rc = GetVentoyVersionFromFatFile(VerBuf, BufLen);
603 }
604 else
605 {
606 Log("attach media failed...");
607 rc = 1;
608 }
609
610 Log("GetVentoyVerInPhyDrive rc=%d...", rc);
611 if (rc == 0)
612 {
613 Log("VentoyVerInPhyDrive %d is <%s>...", pDriveInfo->PhyDrive, VerBuf);
614
615 flfile = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
616 if (flfile)
617 {
618 *pSecureBoot = TRUE;
619 fl_fclose(flfile);
620 }
621 }
622
623 fl_shutdown();
624
625 CHECK_CLOSE_HANDLE(hDrive);
626
627 return rc;
628 }
629
630
631
632
633
634 static unsigned int g_disk_unxz_len = 0;
635 static BYTE *g_part_img_pos = NULL;
636 static BYTE *g_part_img_buf[VENTOY_EFI_PART_SIZE / SIZE_1MB];
637
638
639 static int VentoyFatMemRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
640 {
641 uint32 i;
642 uint32 offset;
643 BYTE *MbBuf = NULL;
644
645 for (i = 0; i < SectorCount; i++)
646 {
647 offset = (Sector + i) * 512;
648
649 if (g_part_img_buf[1] == NULL)
650 {
651 MbBuf = g_part_img_buf[0] + offset;
652 memcpy(Buffer + i * 512, MbBuf, 512);
653 }
654 else
655 {
656 MbBuf = g_part_img_buf[offset / SIZE_1MB];
657 memcpy(Buffer + i * 512, MbBuf + (offset % SIZE_1MB), 512);
658 }
659 }
660
661 return 1;
662 }
663
664
665 static int VentoyFatMemWrite(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
666 {
667 uint32 i;
668 uint32 offset;
669 BYTE *MbBuf = NULL;
670
671 for (i = 0; i < SectorCount; i++)
672 {
673 offset = (Sector + i) * 512;
674
675 if (g_part_img_buf[1] == NULL)
676 {
677 MbBuf = g_part_img_buf[0] + offset;
678 memcpy(MbBuf, Buffer + i * 512, 512);
679 }
680 else
681 {
682 MbBuf = g_part_img_buf[offset / SIZE_1MB];
683 memcpy(MbBuf + (offset % SIZE_1MB), Buffer + i * 512, 512);
684 }
685 }
686
687 return 1;
688 }
689
690 int VentoyProcSecureBoot(BOOL SecureBoot)
691 {
692 int rc = 0;
693 int size;
694 char *filebuf = NULL;
695 void *file = NULL;
696
697 Log("VentoyProcSecureBoot %d ...", SecureBoot);
698
699 if (SecureBoot)
700 {
701 Log("Secure boot is enabled ...");
702 return 0;
703 }
704
705 fl_init();
706
707 if (0 == fl_attach_media(VentoyFatMemRead, VentoyFatMemWrite))
708 {
709 file = fl_fopen("/EFI/BOOT/grubx64_real.efi", "rb");
710 Log("Open ventoy efi file %p ", file);
711 if (file)
712 {
713 fl_fseek(file, 0, SEEK_END);
714 size = (int)fl_ftell(file);
715 fl_fseek(file, 0, SEEK_SET);
716
717 Log("ventoy efi file size %d ...", size);
718
719 filebuf = (char *)malloc(size);
720 if (filebuf)
721 {
722 fl_fread(filebuf, 1, size, file);
723 }
724
725 fl_fclose(file);
726
727 Log("Now delete all efi files ...");
728 fl_remove("/EFI/BOOT/BOOTX64.EFI");
729 fl_remove("/EFI/BOOT/grubx64.efi");
730 fl_remove("/EFI/BOOT/grubx64_real.efi");
731 fl_remove("/EFI/BOOT/MokManager.efi");
732 fl_remove("/EFI/BOOT/mmx64.efi");
733 fl_remove("/ENROLL_THIS_KEY_IN_MOKMANAGER.cer");
734 fl_remove("/EFI/BOOT/grub.efi");
735
736 file = fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb");
737 Log("Open bootx64 efi file %p ", file);
738 if (file)
739 {
740 if (filebuf)
741 {
742 fl_fwrite(filebuf, 1, size, file);
743 }
744
745 fl_fflush(file);
746 fl_fclose(file);
747 }
748
749 if (filebuf)
750 {
751 free(filebuf);
752 }
753 }
754
755 file = fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");
756 Log("Open ventoy efi file %p ", file);
757 if (file)
758 {
759 fl_fseek(file, 0, SEEK_END);
760 size = (int)fl_ftell(file);
761 fl_fseek(file, 0, SEEK_SET);
762
763 Log("ventoy efi file size %d ...", size);
764
765 filebuf = (char *)malloc(size);
766 if (filebuf)
767 {
768 fl_fread(filebuf, 1, size, file);
769 }
770
771 fl_fclose(file);
772
773 Log("Now delete all efi files ...");
774 fl_remove("/EFI/BOOT/BOOTIA32.EFI");
775 fl_remove("/EFI/BOOT/grubia32.efi");
776 fl_remove("/EFI/BOOT/grubia32_real.efi");
777 fl_remove("/EFI/BOOT/mmia32.efi");
778
779 file = fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");
780 Log("Open bootia32 efi file %p ", file);
781 if (file)
782 {
783 if (filebuf)
784 {
785 fl_fwrite(filebuf, 1, size, file);
786 }
787
788 fl_fflush(file);
789 fl_fclose(file);
790 }
791
792 if (filebuf)
793 {
794 free(filebuf);
795 }
796 }
797
798 }
799 else
800 {
801 rc = 1;
802 }
803
804 fl_shutdown();
805
806 return rc;
807 }
808
809
810
811 static int disk_xz_flush(void *src, unsigned int size)
812 {
813 unsigned int i;
814 BYTE *buf = (BYTE *)src;
815
816 for (i = 0; i < size; i++)
817 {
818 *g_part_img_pos = *buf++;
819
820 g_disk_unxz_len++;
821 if ((g_disk_unxz_len % SIZE_1MB) == 0)
822 {
823 g_part_img_pos = g_part_img_buf[g_disk_unxz_len / SIZE_1MB];
824 }
825 else
826 {
827 g_part_img_pos++;
828 }
829 }
830
831 return (int)size;
832 }
833
834 static void unxz_error(char *x)
835 {
836 Log("%s", x);
837 }
838
839 static BOOL TryWritePart2(HANDLE hDrive, UINT64 StartSectorId)
840 {
841 BOOL bRet;
842 DWORD TrySize = 16 * 1024;
843 DWORD dwSize;
844 BYTE *Buffer = NULL;
845 unsigned char *data = NULL;
846 LARGE_INTEGER liCurrentPosition;
847
848 liCurrentPosition.QuadPart = StartSectorId * 512;
849 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
850
851 Buffer = malloc(TrySize);
852
853 bRet = WriteFile(hDrive, Buffer, TrySize, &dwSize, NULL);
854
855 free(Buffer);
856
857 Log("Try write part2 bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
858
859 if (bRet && dwSize == TrySize)
860 {
861 return TRUE;
862 }
863
864 return FALSE;
865 }
866
867 static int FormatPart2Fat(HANDLE hDrive, UINT64 StartSectorId)
868 {
869 int i;
870 int rc = 0;
871 int len = 0;
872 int writelen = 0;
873 int partwrite = 0;
874 int Pos = PT_WRITE_VENTOY_START;
875 DWORD dwSize = 0;
876 BOOL bRet;
877 unsigned char *data = NULL;
878 LARGE_INTEGER liCurrentPosition;
879 LARGE_INTEGER liNewPosition;
880 BYTE *CheckBuf = NULL;
881
882 Log("FormatPart2Fat %llu...", (ULONGLONG)StartSectorId);
883
884 CheckBuf = malloc(SIZE_1MB);
885 if (!CheckBuf)
886 {
887 Log("Failed to malloc check buf");
888 return 1;
889 }
890
891 rc = ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG, 0, (void **)&data, &len);
892 if (rc)
893 {
894 Log("Failed to read img file %p %u", data, len);
895 free(CheckBuf);
896 return 1;
897 }
898
899 liCurrentPosition.QuadPart = StartSectorId * 512;
900 SetFilePointerEx(hDrive, liCurrentPosition, &liNewPosition, FILE_BEGIN);
901
902 memset(g_part_img_buf, 0, sizeof(g_part_img_buf));
903
904 g_part_img_buf[0] = (BYTE *)malloc(VENTOY_EFI_PART_SIZE);
905 if (g_part_img_buf[0])
906 {
907 Log("Malloc whole img buffer success, now decompress ...");
908 unxz(data, len, NULL, NULL, g_part_img_buf[0], &writelen, unxz_error);
909
910 if (len == writelen)
911 {
912 Log("decompress finished success");
913
914 VentoyProcSecureBoot(g_SecureBoot);
915
916 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
917 {
918 dwSize = 0;
919 bRet = WriteFile(hDrive, g_part_img_buf[0] + i * SIZE_1MB, SIZE_1MB, &dwSize, NULL);
920 Log("Write part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
921
922 if (!bRet)
923 {
924 rc = 1;
925 goto End;
926 }
927
928 PROGRESS_BAR_SET_POS(Pos);
929 if (i % 2 == 0)
930 {
931 Pos++;
932 }
933 }
934
935 //Read and check the data
936 liCurrentPosition.QuadPart = StartSectorId * 512;
937 SetFilePointerEx(hDrive, liCurrentPosition, &liNewPosition, FILE_BEGIN);
938
939 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
940 {
941 bRet = ReadFile(hDrive, CheckBuf, SIZE_1MB, &dwSize, NULL);
942 Log("Read part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
943
944 if (!bRet || memcmp(CheckBuf, g_part_img_buf[0] + i * SIZE_1MB, SIZE_1MB))
945 {
946 Log("### [Check Fail] The data write and read does not match");
947 rc = 1;
948 goto End;
949 }
950
951 PROGRESS_BAR_SET_POS(Pos);
952 if (i % 2 == 0)
953 {
954 Pos++;
955 }
956 }
957 }
958 else
959 {
960 rc = 1;
961 Log("decompress finished failed");
962 goto End;
963 }
964 }
965 else
966 {
967 Log("Failed to malloc whole img size %u, now split it", VENTOY_EFI_PART_SIZE);
968
969 partwrite = 1;
970 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
971 {
972 g_part_img_buf[i] = (BYTE *)malloc(SIZE_1MB);
973 if (g_part_img_buf[i] == NULL)
974 {
975 rc = 1;
976 goto End;
977 }
978 }
979
980 Log("Malloc part img buffer success, now decompress ...");
981
982 g_part_img_pos = g_part_img_buf[0];
983
984 unxz(data, len, NULL, disk_xz_flush, NULL, NULL, unxz_error);
985
986 if (g_disk_unxz_len == VENTOY_EFI_PART_SIZE)
987 {
988 Log("decompress finished success");
989
990 VentoyProcSecureBoot(g_SecureBoot);
991
992 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
993 {
994 dwSize = 0;
995 bRet = WriteFile(hDrive, g_part_img_buf[i], SIZE_1MB, &dwSize, NULL);
996 Log("Write part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
997
998 if (!bRet)
999 {
1000 rc = 1;
1001 goto End;
1002 }
1003
1004 PROGRESS_BAR_SET_POS(Pos);
1005 if (i % 2 == 0)
1006 {
1007 Pos++;
1008 }
1009 }
1010
1011 //Read and check the data
1012 liCurrentPosition.QuadPart = StartSectorId * 512;
1013 SetFilePointerEx(hDrive, liCurrentPosition, &liNewPosition, FILE_BEGIN);
1014
1015 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1016 {
1017 bRet = ReadFile(hDrive, CheckBuf, SIZE_1MB, &dwSize, NULL);
1018 Log("Read part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
1019
1020 if (!bRet || memcmp(CheckBuf, g_part_img_buf[i], SIZE_1MB))
1021 {
1022 Log("### [Check Fail] The data write and read does not match");
1023 rc = 1;
1024 goto End;
1025 }
1026
1027 PROGRESS_BAR_SET_POS(Pos);
1028 if (i % 2 == 0)
1029 {
1030 Pos++;
1031 }
1032 }
1033 }
1034 else
1035 {
1036 rc = 1;
1037 Log("decompress finished failed");
1038 goto End;
1039 }
1040 }
1041
1042 End:
1043
1044 if (data) free(data);
1045 if (CheckBuf)free(CheckBuf);
1046
1047 if (partwrite)
1048 {
1049 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1050 {
1051 if (g_part_img_buf[i]) free(g_part_img_buf[i]);
1052 }
1053 }
1054 else
1055 {
1056 if (g_part_img_buf[0]) free(g_part_img_buf[0]);
1057 }
1058
1059 return rc;
1060 }
1061
1062 static int WriteGrubStage1ToPhyDrive(HANDLE hDrive, int PartStyle)
1063 {
1064 int Len = 0;
1065 int readLen = 0;
1066 BOOL bRet;
1067 DWORD dwSize;
1068 BYTE *ImgBuf = NULL;
1069 BYTE *RawBuf = NULL;
1070
1071 Log("WriteGrubStage1ToPhyDrive ...");
1072
1073 RawBuf = (BYTE *)malloc(SIZE_1MB);
1074 if (!RawBuf)
1075 {
1076 return 1;
1077 }
1078
1079 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG, 0, (void **)&ImgBuf, &Len))
1080 {
1081 Log("Failed to read stage1 img");
1082 free(RawBuf);
1083 return 1;
1084 }
1085
1086 unxz(ImgBuf, Len, NULL, NULL, RawBuf, &readLen, unxz_error);
1087
1088 if (PartStyle)
1089 {
1090 Log("Write GPT stage1 ...");
1091 RawBuf[500] = 35;//update blocklist
1092 SetFilePointer(hDrive, 512 * 34, NULL, FILE_BEGIN);
1093 bRet = WriteFile(hDrive, RawBuf, SIZE_1MB - 512 * 34, &dwSize, NULL);
1094 }
1095 else
1096 {
1097 Log("Write MBR stage1 ...");
1098 SetFilePointer(hDrive, 512, NULL, FILE_BEGIN);
1099 bRet = WriteFile(hDrive, RawBuf, SIZE_1MB - 512, &dwSize, NULL);
1100 }
1101
1102 Log("WriteFile Ret:%u dwSize:%u ErrCode:%u", bRet, dwSize, GetLastError());
1103
1104 free(RawBuf);
1105 free(ImgBuf);
1106 return 0;
1107 }
1108
1109
1110 static int FormatPart1LargeFAT32(UINT64 DiskSizeBytes, int CluserSize)
1111 {
1112 MKFS_PARM Option;
1113 FRESULT Ret;
1114 FATFS FS;
1115
1116 Option.fmt = FM_FAT32;
1117 Option.n_fat = 1;
1118 Option.align = 8;
1119 Option.n_root = 1;
1120
1121 if (CluserSize == 0)
1122 {
1123 // < 32GB select 32KB as cluster size
1124 // > 32GB select 128KB as cluster size
1125 if (DiskSizeBytes / 1024 / 1024 / 1024 <= 32)
1126 {
1127 Option.au_size = 32768;
1128 }
1129 else
1130 {
1131 Option.au_size = 131072;
1132 }
1133 }
1134 else
1135 {
1136 Option.au_size = CluserSize;
1137 }
1138
1139 Log("Formatting Part1 large FAT32 ClusterSize:%u(%uKB) ...", CluserSize, CluserSize / 1024);
1140
1141 disk_io_reset_write_error();
1142
1143 Ret = f_mkfs(TEXT("0:"), &Option, 0, 8 * 1024 * 1024);
1144 if (FR_OK == Ret)
1145 {
1146 if (disk_io_is_write_error())
1147 {
1148 Log("Formatting Part1 large FAT32 failed, write error.");
1149 return 1;
1150 }
1151
1152 Log("Formatting Part1 large FAT32 success, now set label");
1153
1154 Ret = f_mount(&FS, TEXT("0:"), 1);
1155 if (FR_OK == Ret)
1156 {
1157 Log("f_mount SUCCESS");
1158 Ret = f_setlabel(TEXT("0:Ventoy"));
1159 if (FR_OK == Ret)
1160 {
1161 Log("f_setlabel SUCCESS");
1162 Ret = f_unmount(TEXT("0:"));
1163 Log("f_unmount %d %s", Ret, (FR_OK == Ret) ? "SUCCESS" : "FAILED");
1164 }
1165 else
1166 {
1167 Log("f_setlabel failed %d", Ret);
1168 }
1169 }
1170 else
1171 {
1172 Log("f_mount failed %d", Ret);
1173 }
1174
1175 return 0;
1176 }
1177 else
1178 {
1179 Log("Formatting Part1 large FAT32 failed");
1180 return 1;
1181 }
1182 }
1183
1184 static int FormatPart1exFAT(UINT64 DiskSizeBytes)
1185 {
1186 MKFS_PARM Option;
1187 FRESULT Ret;
1188
1189 Option.fmt = FM_EXFAT;
1190 Option.n_fat = 1;
1191 Option.align = 8;
1192 Option.n_root = 1;
1193
1194 // < 32GB select 32KB as cluster size
1195 // > 32GB select 128KB as cluster size
1196 if (DiskSizeBytes / 1024 / 1024 / 1024 <= 32)
1197 {
1198 Option.au_size = 32768;
1199 }
1200 else
1201 {
1202 Option.au_size = 131072;
1203 }
1204
1205 Log("Formatting Part1 exFAT ...");
1206
1207 disk_io_reset_write_error();
1208
1209 Ret = f_mkfs(TEXT("0:"), &Option, 0, 8 * 1024 * 1024);
1210 if (FR_OK == Ret)
1211 {
1212 if (disk_io_is_write_error())
1213 {
1214 Log("Formatting Part1 exFAT failed, write error.");
1215 return 1;
1216 }
1217
1218 Log("Formatting Part1 exFAT success");
1219 return 0;
1220 }
1221 else
1222 {
1223 Log("Formatting Part1 exFAT failed");
1224 return 1;
1225 }
1226 }
1227
1228
1229
1230 int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLetter)
1231 {
1232 int i;
1233 int rc = 0;
1234 int state = 0;
1235 HANDLE hDrive;
1236 DWORD dwSize;
1237 BOOL bRet;
1238 CHAR MountDrive;
1239 CHAR DriveName[] = "?:\\";
1240 CHAR DriveLetters[MAX_PATH] = { 0 };
1241 LARGE_INTEGER liCurrentPosition;
1242 char *pTmpBuf = NULL;
1243 MBR_HEAD MBR;
1244
1245 *pDrvLetter = 0;
1246
1247 Log("ClearVentoyFromPhyDrive PhyDrive%d <<%s %s %dGB>>",
1248 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1249 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1250
1251 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1252
1253 Log("Lock disk for clean ............................. ");
1254
1255 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1256 if (hDrive == INVALID_HANDLE_VALUE)
1257 {
1258 Log("Failed to open physical disk");
1259 return 1;
1260 }
1261
1262 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1263
1264 if (DriveLetters[0] == 0)
1265 {
1266 Log("No drive letter was assigned...");
1267 DriveName[0] = GetFirstUnusedDriveLetter();
1268 Log("GetFirstUnusedDriveLetter %C: ...", DriveName[0]);
1269 }
1270 else
1271 {
1272 // Unmount all mounted volumes that belong to this drive
1273 // Do it in reverse so that we always end on the first volume letter
1274 for (i = (int)strlen(DriveLetters); i > 0; i--)
1275 {
1276 DriveName[0] = DriveLetters[i - 1];
1277 bRet = DeleteVolumeMountPointA(DriveName);
1278 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, GetLastError());
1279 }
1280 }
1281
1282 MountDrive = DriveName[0];
1283 Log("Will use '%C:' as volume mountpoint", DriveName[0]);
1284
1285 // It kind of blows, but we have to relinquish access to the physical drive
1286 // for VDS to be able to delete the partitions that reside on it...
1287 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1288 CHECK_CLOSE_HANDLE(hDrive);
1289
1290 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
1291
1292 if (!VDS_DeleteAllPartitions(pPhyDrive->PhyDrive))
1293 {
1294 Log("Notice: Could not delete partitions: %u", GetLastError());
1295 }
1296
1297 Log("Deleting all partitions ......................... OK");
1298
1299 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1300
1301 Log("Lock disk for write ............................. ");
1302 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1303 if (hDrive == INVALID_HANDLE_VALUE)
1304 {
1305 Log("Failed to GetPhysicalHandle for write.");
1306 rc = 1;
1307 goto End;
1308 }
1309
1310 // clear first and last 2MB space
1311 pTmpBuf = malloc(SIZE_2MB);
1312 if (!pTmpBuf)
1313 {
1314 Log("Failed to alloc memory.");
1315 rc = 1;
1316 goto End;
1317 }
1318 memset(pTmpBuf, 0, SIZE_2MB);
1319
1320 SET_FILE_POS(512);
1321 bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB - 512, &dwSize, NULL);
1322 Log("Write fisrt 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1323 if (!bRet)
1324 {
1325 rc = 1;
1326 goto End;
1327 }
1328
1329 SET_FILE_POS(pPhyDrive->SizeInBytes - SIZE_2MB);
1330 bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB, &dwSize, NULL);
1331 Log("Write 2nd 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1332 if (!bRet)
1333 {
1334 rc = 1;
1335 goto End;
1336 }
1337
1338 SET_FILE_POS(0);
1339
1340 if (pPhyDrive->SizeInBytes > 2199023255552ULL)
1341 {
1342 VTOY_GPT_INFO *pGptInfo;
1343 VTOY_GPT_HDR BackupHead;
1344 LARGE_INTEGER liCurrentPosition;
1345
1346 pGptInfo = (VTOY_GPT_INFO *)pTmpBuf;
1347
1348 VentoyFillWholeGpt(pPhyDrive->SizeInBytes, pGptInfo);
1349
1350 SET_FILE_POS(pPhyDrive->SizeInBytes - 512);
1351 VentoyFillBackupGptHead(pGptInfo, &BackupHead);
1352 if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))
1353 {
1354 rc = 1;
1355 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1356 goto End;
1357 }
1358
1359 SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);
1360 if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))
1361 {
1362 rc = 1;
1363 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1364 goto End;
1365 }
1366
1367 SET_FILE_POS(0);
1368 if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))
1369 {
1370 rc = 1;
1371 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1372 goto End;
1373 }
1374
1375 Log("Write GPT Info OK ...");
1376 }
1377 else
1378 {
1379 bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
1380 Log("Read MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1381 if (!bRet)
1382 {
1383 rc = 1;
1384 goto End;
1385 }
1386
1387 //clear boot code and partition table (reserved disk signature)
1388 memset(MBR.BootCode, 0, 440);
1389 memset(MBR.PartTbl, 0, sizeof(MBR.PartTbl));
1390
1391 VentoyFillMBRLocation(pPhyDrive->SizeInBytes, 2048, (UINT32)(pPhyDrive->SizeInBytes / 512 - 2048), MBR.PartTbl);
1392
1393 MBR.PartTbl[0].Active = 0x00; // bootable
1394 MBR.PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS
1395
1396 SET_FILE_POS(0);
1397 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
1398 Log("Write MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1399 if (!bRet)
1400 {
1401 rc = 1;
1402 goto End;
1403 }
1404 }
1405
1406 Log("Clear Ventoy successfully finished");
1407
1408 //Refresh Drive Layout
1409 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1410
1411 End:
1412
1413 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1414 PROGRESS_BAR_SET_POS(PT_REFORMAT_FINISH);
1415
1416 if (pTmpBuf)
1417 {
1418 free(pTmpBuf);
1419 }
1420
1421 if (rc == 0)
1422 {
1423 Log("Mounting Ventoy Partition ....................... ");
1424 Sleep(1000);
1425
1426 state = 0;
1427 memset(DriveLetters, 0, sizeof(DriveLetters));
1428 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1429 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1430
1431 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1432 {
1433 DriveName[0] = DriveLetters[i];
1434 Log("%s is ventoy part1, already mounted", DriveName);
1435 state = 1;
1436 }
1437
1438 if (state != 1)
1439 {
1440 Log("need to mount ventoy part1...");
1441 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, 2048, DriveLetters, sizeof(DriveLetters), FALSE))
1442 {
1443 DriveName[0] = MountDrive;
1444 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1445 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1446
1447 *pDrvLetter = MountDrive;
1448 }
1449 else
1450 {
1451 Log("Failed to find ventoy volume");
1452 }
1453 }
1454
1455 Log("OK\n");
1456 }
1457 else
1458 {
1459 FindProcessOccupyDisk(hDrive, pPhyDrive);
1460 }
1461
1462 CHECK_CLOSE_HANDLE(hDrive);
1463 return rc;
1464 }
1465
1466 int InstallVentoy2FileImage(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
1467 {
1468 int i;
1469 int rc = 1;
1470 int Len = 0;
1471 int dataLen = 0;
1472 UINT size = 0;
1473 UINT segnum = 0;
1474 UINT32 chksum = 0;
1475 UINT64 data_offset = 0;
1476 UINT64 Part2StartSector = 0;
1477 UINT64 Part1StartSector = 0;
1478 UINT64 Part1SectorCount = 0;
1479 UINT8 *pData = NULL;
1480 UINT8 *pBkGptPartTbl = NULL;
1481 BYTE *ImgBuf = NULL;
1482 MBR_HEAD *pMBR = NULL;
1483 VTSI_FOOTER *pImgFooter = NULL;
1484 VTSI_SEGMENT *pSegment = NULL;
1485 VTOY_GPT_INFO *pGptInfo = NULL;
1486 VTOY_GPT_HDR *pBkGptHdr = NULL;
1487 FILE *fp = NULL;
1488
1489 Log("InstallVentoy2FileImage %s PhyDrive%d <<%s %s %dGB>>",
1490 PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1491 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1492
1493 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1494
1495 size = SIZE_1MB + VENTOY_EFI_PART_SIZE + 33 * 512 + VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT) + sizeof(VTSI_FOOTER);
1496
1497 pData = (UINT8 *)malloc(size);
1498 if (!pData)
1499 {
1500 Log("malloc image buffer failed %d.", size);
1501 goto End;
1502 }
1503
1504 pImgFooter = (VTSI_FOOTER *)(pData + size - sizeof(VTSI_FOOTER));
1505 pSegment = (VTSI_SEGMENT *)((UINT8 *)pImgFooter - VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT));
1506 memset(pImgFooter, 0, sizeof(VTSI_FOOTER));
1507 memset(pSegment, 0, VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT));
1508
1509 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START);
1510
1511 Log("Writing Boot Image ............................. ");
1512 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG, 0, (void **)&ImgBuf, &Len))
1513 {
1514 Log("Failed to read stage1 img");
1515 goto End;
1516 }
1517
1518 unxz(ImgBuf, Len, NULL, NULL, pData, &dataLen, unxz_error);
1519 SAFE_FREE(ImgBuf);
1520
1521 Log("decompress %s len:%d", VENTOY_FILE_STG1_IMG, dataLen);
1522
1523 if (PartStyle)
1524 {
1525 pData[500] = 35;//update blocklist
1526 memmove(pData + 34 * 512, pData, SIZE_1MB - 512 * 34);
1527 memset(pData, 0, 34 * 512);
1528
1529 pGptInfo = (VTOY_GPT_INFO *)pData;
1530 memset(pGptInfo, 0, sizeof(VTOY_GPT_INFO));
1531 VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);
1532
1533 pBkGptPartTbl = pData + SIZE_1MB + VENTOY_EFI_PART_SIZE;
1534 memset(pBkGptPartTbl, 0, 33 * 512);
1535
1536 memcpy(pBkGptPartTbl, pGptInfo->PartTbl, 32 * 512);
1537 pBkGptHdr = (VTOY_GPT_HDR *)(pBkGptPartTbl + 32 * 512);
1538 VentoyFillBackupGptHead(pGptInfo, pBkGptHdr);
1539
1540 Part1StartSector = pGptInfo->PartTbl[0].StartLBA;
1541 Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;
1542 Part2StartSector = pGptInfo->PartTbl[1].StartLBA;
1543
1544 Log("Write GPT Info OK ...");
1545 }
1546 else
1547 {
1548 memmove(pData + 512, pData, SIZE_1MB - 512);
1549 memset(pData, 0, 512);
1550
1551 pMBR = (MBR_HEAD *)pData;
1552 VentoyFillMBR(pPhyDrive->SizeInBytes, pMBR, PartStyle);
1553 Part1StartSector = pMBR->PartTbl[0].StartSectorId;
1554 Part1SectorCount = pMBR->PartTbl[0].SectorCount;
1555 Part2StartSector = pMBR->PartTbl[1].StartSectorId;
1556
1557 Log("Write MBR OK ...");
1558 }
1559
1560 Log("Writing EFI part Image ............................. ");
1561 rc = ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG, 0, (void **)&ImgBuf, &Len);
1562 if (rc)
1563 {
1564 Log("Failed to read img file %p %u", ImgBuf, Len);
1565 goto End;
1566 }
1567
1568 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START + 28);
1569 memset(g_part_img_buf, 0, sizeof(g_part_img_buf));
1570 unxz(ImgBuf, Len, NULL, NULL, pData + SIZE_1MB, &dataLen, unxz_error);
1571 if (dataLen == Len)
1572 {
1573 Log("decompress finished success");
1574 g_part_img_buf[0] = pData + SIZE_1MB;
1575
1576 VentoyProcSecureBoot(g_SecureBoot);
1577 }
1578 else
1579 {
1580 Log("decompress finished failed");
1581 goto End;
1582 }
1583
1584 fopen_s(&fp, "VentoySparseImg.vtsi", "wb+");
1585 if (!fp)
1586 {
1587 Log("Failed to create Ventoy img file");
1588 goto End;
1589 }
1590
1591 Log("Writing stage1 data ............................. ");
1592
1593 fwrite(pData, 1, SIZE_1MB, fp);
1594
1595 pSegment[0].disk_start_sector = 0;
1596 pSegment[0].sector_num = SIZE_1MB / 512;
1597 pSegment[0].data_offset = data_offset;
1598 data_offset += pSegment[0].sector_num * 512;
1599
1600 disk_io_set_param(INVALID_HANDLE_VALUE, Part1StartSector + Part1SectorCount);// include the 2048 sector gap
1601 disk_io_set_imghook(fp, pSegment + 1, VTSI_IMG_MAX_SEG - 1, data_offset);
1602
1603 Log("Formatting part1 exFAT ...");
1604 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
1605 {
1606 Log("FormatPart1exFAT failed.");
1607 disk_io_reset_imghook(&segnum, &data_offset);
1608 goto End;
1609 }
1610
1611 disk_io_reset_imghook(&segnum, &data_offset);
1612 segnum++;
1613
1614 Log("current segment number:%d dataoff:%ld", segnum, (long)data_offset);
1615
1616 //write data
1617 Log("Writing part2 data ............................. ");
1618 fwrite(pData + SIZE_1MB, 1, VENTOY_EFI_PART_SIZE, fp);
1619 pSegment[segnum].disk_start_sector = Part2StartSector;
1620 pSegment[segnum].sector_num = VENTOY_EFI_PART_SIZE / 512;
1621 pSegment[segnum].data_offset = data_offset;
1622 data_offset += pSegment[segnum].sector_num * 512;
1623 segnum++;
1624
1625 if (PartStyle)
1626 {
1627 Log("Writing backup gpt table ............................. ");
1628 fwrite(pBkGptPartTbl, 1, 33 * 512, fp);
1629 pSegment[segnum].disk_start_sector = pPhyDrive->SizeInBytes / 512 - 33;
1630 pSegment[segnum].sector_num = 33;
1631 pSegment[segnum].data_offset = data_offset;
1632 data_offset += pSegment[segnum].sector_num * 512;
1633 segnum++;
1634 }
1635
1636 Log("Writing segment metadata ............................. ");
1637
1638 for (i = 0; i < (int)segnum; i++)
1639 {
1640 Log("SEG[%d]: PhySector:%llu SectorNum:%llu DataOffset:%llu(sector:%llu)", i, pSegment[i].disk_start_sector, pSegment[i].sector_num,
1641 pSegment[i].data_offset, pSegment[i].data_offset / 512);
1642 }
1643
1644 dataLen = segnum * sizeof(VTSI_SEGMENT);
1645 fwrite(pSegment, 1, dataLen, fp);
1646
1647 if (dataLen % 512)
1648 {
1649 //pData + SIZE_1MB - 8192 is a temp data buffer with zero
1650 fwrite(pData + SIZE_1MB - 8192, 1, 512 - (dataLen % 512), fp);
1651 }
1652
1653 //Fill footer
1654 pImgFooter->magic = VTSI_IMG_MAGIC;
1655 pImgFooter->version = 1;
1656 pImgFooter->disk_size = pPhyDrive->SizeInBytes;
1657 memcpy(&pImgFooter->disk_signature, pPhyDrive->MBR.BootCode + 0x1b8, 4);
1658 pImgFooter->segment_num = segnum;
1659 pImgFooter->segment_offset = data_offset;
1660
1661 for (i = 0, chksum = 0; i < (int)(segnum * sizeof(VTSI_SEGMENT)); i++)
1662 {
1663 chksum += *((UINT8 *)pSegment + i);
1664 }
1665 pImgFooter->segment_chksum = ~chksum;
1666
1667 for (i = 0, chksum = 0; i < sizeof(VTSI_FOOTER); i++)
1668 {
1669 chksum += *((UINT8 *)pImgFooter + i);
1670 }
1671 pImgFooter->foot_chksum = ~chksum;
1672
1673 Log("Writing footer segnum(%u) segoffset(%llu) ......................", segnum, data_offset);
1674 Log("disk_size=%llu disk_signature=%lx segment_offset=%llu", pImgFooter->disk_size, pImgFooter->disk_signature, pImgFooter->segment_offset);
1675
1676 fwrite(pImgFooter, 1, sizeof(VTSI_FOOTER), fp);
1677 fclose(fp);
1678
1679 Log("Writing Ventoy image file finished, the file size should be %llu .", data_offset + 512 + ((dataLen + 511) / 512 * 512));
1680
1681 rc = 0;
1682
1683 End:
1684
1685 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1686 PROGRESS_BAR_SET_POS(PT_REFORMAT_FINISH);
1687
1688 Log("retcode:%d\n", rc);
1689
1690 SAFE_FREE(pData);
1691 SAFE_FREE(ImgBuf);
1692
1693 return rc;
1694 }
1695
1696
1697 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
1698 {
1699 int i;
1700 int rc = 0;
1701 int state = 0;
1702 BOOL ReformatOK;
1703 HANDLE hDrive;
1704 DWORD dwSize;
1705 BOOL bRet;
1706 CHAR MountDrive;
1707 CHAR DriveName[] = "?:\\";
1708 CHAR DriveLetters[MAX_PATH] = { 0 };
1709 MBR_HEAD MBR;
1710 VTOY_GPT_INFO *pGptInfo = NULL;
1711 UINT64 Part1StartSector = 0;
1712 UINT64 Part1SectorCount = 0;
1713 UINT64 Part2StartSector = 0;
1714 BOOL LargeFAT32 = FALSE;
1715
1716 Log("#####################################################");
1717 Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
1718 PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1719 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1720 Log("#####################################################");
1721
1722 if (PartStyle)
1723 {
1724 pGptInfo = malloc(sizeof(VTOY_GPT_INFO));
1725 memset(pGptInfo, 0, sizeof(VTOY_GPT_INFO));
1726 }
1727
1728 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1729
1730 if (PartStyle)
1731 {
1732 VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);
1733 Part1StartSector = pGptInfo->PartTbl[0].StartLBA;
1734 Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;
1735 Part2StartSector = pGptInfo->PartTbl[1].StartLBA;
1736 }
1737 else
1738 {
1739 VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);
1740 Part1StartSector = MBR.PartTbl[0].StartSectorId;
1741 Part1SectorCount = MBR.PartTbl[0].SectorCount;
1742 Part2StartSector = MBR.PartTbl[1].StartSectorId;
1743 }
1744
1745 Log("Lock disk for clean ............................. ");
1746
1747 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1748 if (hDrive == INVALID_HANDLE_VALUE)
1749 {
1750 Log("Failed to open physical disk");
1751 free(pGptInfo);
1752 return 1;
1753 }
1754
1755 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1756
1757 if (DriveLetters[0] == 0)
1758 {
1759 Log("No drive letter was assigned...");
1760 DriveName[0] = GetFirstUnusedDriveLetter();
1761 Log("GetFirstUnusedDriveLetter %C: ...", DriveName[0]);
1762 }
1763 else
1764 {
1765 // Unmount all mounted volumes that belong to this drive
1766 // Do it in reverse so that we always end on the first volume letter
1767 for (i = (int)strlen(DriveLetters); i > 0; i--)
1768 {
1769 DriveName[0] = DriveLetters[i - 1];
1770 bRet = DeleteVolumeMountPointA(DriveName);
1771 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, GetLastError());
1772 }
1773 }
1774
1775 MountDrive = DriveName[0];
1776 Log("Will use '%C:' as volume mountpoint", DriveName[0]);
1777
1778 // It kind of blows, but we have to relinquish access to the physical drive
1779 // for VDS to be able to delete the partitions that reside on it...
1780 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1781 CHECK_CLOSE_HANDLE(hDrive);
1782
1783 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
1784
1785 if (!VDS_DeleteAllPartitions(pPhyDrive->PhyDrive))
1786 {
1787 Log("Notice: Could not delete partitions: 0x%x, but we continue.", GetLastError());
1788 }
1789
1790 Log("Deleting all partitions ......................... OK");
1791
1792 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1793
1794 Log("Lock disk for write ............................. ");
1795 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1796 if (hDrive == INVALID_HANDLE_VALUE)
1797 {
1798 Log("Failed to GetPhysicalHandle for write.");
1799 rc = 1;
1800 goto End;
1801 }
1802
1803 //Refresh Drive Layout
1804 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1805
1806 disk_io_set_param(hDrive, Part1StartSector + Part1SectorCount);// include the 2048 sector gap
1807
1808 PROGRESS_BAR_SET_POS(PT_FORMAT_PART1);
1809
1810 if (PartStyle == 1 && pPhyDrive->PartStyle == 0)
1811 {
1812 Log("Wait for format part1 ...");
1813 Sleep(1000 * 5);
1814 }
1815
1816 if (GetVentoyFsType() == VTOY_FS_FAT32 && (Part1SectorCount * 512 >= FAT32_MAX_LIMIT))
1817 {
1818 Log("Formatting part1 large FAT32 ...");
1819 LargeFAT32 = TRUE;
1820 if (0 != FormatPart1LargeFAT32(pPhyDrive->SizeInBytes, GetClusterSize()))
1821 {
1822 Log("FormatPart1LargeFAT32 failed.");
1823 rc = 1;
1824 goto End;
1825 }
1826 }
1827 else
1828 {
1829 CHAR TmpBuffer[512] = { 0 };
1830
1831 Log("Zero part1 file system ...");
1832 SetFilePointer(hDrive, VENTOY_PART1_START_SECTOR * 512, NULL, FILE_BEGIN);
1833 for (i = 0; i < 32; i++)
1834 {
1835 WriteFile(hDrive, TmpBuffer, 512, &dwSize, NULL);
1836 }
1837 }
1838
1839 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
1840 Log("Writing part2 FAT img ...");
1841
1842 if (0 != FormatPart2Fat(hDrive, Part2StartSector))
1843 {
1844 Log("FormatPart2Fat failed.");
1845 rc = 1;
1846 goto End;
1847 }
1848
1849 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
1850 Log("Writing Boot Image ............................. ");
1851 if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)
1852 {
1853 Log("WriteGrubStage1ToPhyDrive failed.");
1854 rc = 1;
1855 goto End;
1856 }
1857
1858 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
1859 Log("Writing Partition Table ........................ ");
1860 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
1861
1862 if (PartStyle)
1863 {
1864 VTOY_GPT_HDR BackupHead;
1865 LARGE_INTEGER liCurrentPosition;
1866
1867 SET_FILE_POS(pPhyDrive->SizeInBytes - 512);
1868 VentoyFillBackupGptHead(pGptInfo, &BackupHead);
1869 if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))
1870 {
1871 rc = 1;
1872 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1873 goto End;
1874 }
1875
1876 SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);
1877 if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))
1878 {
1879 rc = 1;
1880 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1881 goto End;
1882 }
1883
1884 SET_FILE_POS(0);
1885 if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))
1886 {
1887 rc = 1;
1888 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1889 goto End;
1890 }
1891
1892 Log("Write GPT Info OK ...");
1893 memcpy(&(pPhyDrive->MBR), &(pGptInfo->MBR), 512);
1894 }
1895 else
1896 {
1897 if (!WriteFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL))
1898 {
1899 rc = 1;
1900 Log("Write MBR Failed, dwSize:%u ErrCode:%u", dwSize, GetLastError());
1901 goto End;
1902 }
1903 Log("Write MBR OK ...");
1904 memcpy(&(pPhyDrive->MBR), &MBR, 512);
1905 }
1906
1907 //Refresh Drive Layout
1908 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1909
1910 End:
1911
1912 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1913
1914 if (rc == 0)
1915 {
1916 Log("Mounting Ventoy Partition ....................... ");
1917 Sleep(1000);
1918
1919 state = 0;
1920 memset(DriveLetters, 0, sizeof(DriveLetters));
1921 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1922 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1923
1924 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1925 {
1926 DriveName[0] = DriveLetters[i];
1927 if (IsVentoyLogicalDrive(DriveName[0]))
1928 {
1929 Log("%s is ventoy part2, delete mountpoint", DriveName);
1930 DeleteVolumeMountPointA(DriveName);
1931 }
1932 else
1933 {
1934 Log("%s is ventoy part1, already mounted", DriveName);
1935 MountDrive = DriveName[0];
1936 state = 1;
1937 }
1938 }
1939
1940 if (state != 1)
1941 {
1942 Log("need to mount ventoy part1...");
1943
1944 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, Part1StartSector, DriveLetters, sizeof(DriveLetters), FALSE))
1945 {
1946 DriveName[0] = MountDrive;
1947 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1948 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1949
1950 if (bRet)
1951 {
1952 state = 1;
1953 }
1954 }
1955 else
1956 {
1957 Log("Failed to find ventoy volume");
1958 }
1959 }
1960
1961 // close handle, or it will deny reformat
1962 Log("Close handle ...");
1963 CHECK_CLOSE_HANDLE(hDrive);
1964
1965 ReformatOK = FALSE;
1966
1967 if (state)
1968 {
1969 if (LargeFAT32)
1970 {
1971 Log("No need to reformat for large FAT32");
1972 pPhyDrive->VentoyFsClusterSize = GetVolumeClusterSize(MountDrive);
1973 }
1974 else
1975 {
1976 bRet = DISK_FormatVolume(MountDrive, GetVentoyFsType(), Part1SectorCount * 512);
1977 for (i = 0; bRet == FALSE && i < 2; i++)
1978 {
1979 Log("Wait and retry reformat ...");
1980 Sleep(1000);
1981 bRet = DISK_FormatVolume(MountDrive, GetVentoyFsType(), Part1SectorCount * 512);
1982 }
1983
1984 if (bRet)
1985 {
1986 ReformatOK = TRUE;
1987 Log("Reformat %C:\\ to %s SUCCESS", MountDrive, GetVentoyFsName());
1988 pPhyDrive->VentoyFsClusterSize = GetVolumeClusterSize(MountDrive);
1989
1990 if ((GetVentoyFsType() != VTOY_FS_UDF) && (pPhyDrive->VentoyFsClusterSize < 2048))
1991 {
1992 for (i = 0; i < 10; i++)
1993 {
1994 Log("### Invalid cluster size %d ###", pPhyDrive->VentoyFsClusterSize);
1995 }
1996 }
1997 }
1998 else
1999 {
2000 Log("Reformat %C:\\ to %s FAILED", MountDrive, GetVentoyFsName());
2001 }
2002 }
2003 }
2004 else
2005 {
2006 Log("Can not reformat %s to %s", DriveName, GetVentoyFsName());
2007 }
2008
2009 if (!ReformatOK)
2010 {
2011 Log("Format to exfat with built-in algorithm");
2012
2013 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2014 if (hDrive == INVALID_HANDLE_VALUE)
2015 {
2016 Log("Failed to GetPhysicalHandle for write.");
2017 }
2018 else
2019 {
2020 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
2021 {
2022 Log("FormatPart1exFAT SUCCESS.");
2023 }
2024 else
2025 {
2026 Log("FormatPart1exFAT FAILED.");
2027 }
2028
2029 CHECK_CLOSE_HANDLE(hDrive);
2030 }
2031 }
2032
2033 Log("OK\n");
2034 }
2035 else
2036 {
2037 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2038
2039 FindProcessOccupyDisk(hDrive, pPhyDrive);
2040
2041 if (!VDS_IsLastAvaliable())
2042 {
2043 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2044 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2045 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2046 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2047 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
2048 }
2049
2050 CHECK_CLOSE_HANDLE(hDrive);
2051 }
2052
2053 if (pGptInfo)
2054 {
2055 free(pGptInfo);
2056 }
2057
2058 return rc;
2059 }
2060
2061
2062 int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive)
2063 {
2064 int i, j;
2065 int rc = 1;
2066 int PhyDrive;
2067 int PartStyle;
2068 INT64 ReservedValue;
2069 UINT64 RecudeBytes;
2070 GUID Guid;
2071 MBR_HEAD MBR;
2072 VTOY_GPT_INFO *pGPT;
2073 MBR_HEAD *pMBR;
2074 DWORD dwSize = 0;
2075 VTOY_GPT_HDR BackupHead;
2076 HANDLE hDrive = INVALID_HANDLE_VALUE;
2077 GUID ZeroGuid = { 0 };
2078 static GUID WindowsDataPartType = { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
2079 static GUID EspPartType = { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
2080 static GUID BiosGrubPartType = { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
2081
2082 Log("#####################################################");
2083 Log("PartitionResizeForVentoy PhyDrive%d <<%s %s %dGB>>",
2084 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
2085 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
2086 Log("#####################################################");
2087
2088 pGPT = &(pPhyDrive->Gpt);
2089 pMBR = &(pPhyDrive->Gpt.MBR);
2090 Log("Disksize:%llu Part2Start:%llu", pPhyDrive->SizeInBytes, pPhyDrive->ResizePart2StartSector * 512);
2091
2092 if (pMBR->PartTbl[0].FsFlag == 0xEE && memcmp(pGPT->Head.Signature, "EFI PART", 8) == 0)
2093 {
2094 PartStyle = 1;
2095 }
2096 else
2097 {
2098 PartStyle = 0;
2099 }
2100
2101 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2102
2103 RecudeBytes = VENTOY_EFI_PART_SIZE;
2104 ReservedValue = GetReservedSpaceInMB();
2105 if (ReservedValue > 0)
2106 {
2107 Log("Reduce add reserved space %lldMB", (LONGLONG)ReservedValue);
2108 RecudeBytes += (UINT64)(ReservedValue * SIZE_1MB);
2109 }
2110
2111
2112 if (pPhyDrive->ResizeNoShrink == FALSE)
2113 {
2114 Log("Need to shrink the volume");
2115 if (DISK_ShrinkVolume(pPhyDrive->PhyDrive, pPhyDrive->ResizeVolumeGuid, pPhyDrive->Part1DriveLetter, pPhyDrive->ResizeOldPart1Size, RecudeBytes))
2116 {
2117 Log("Shrink volume success, now check again");
2118
2119 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2120 if (hDrive == INVALID_HANDLE_VALUE)
2121 {
2122 Log("Failed to GetPhysicalHandle for update.");
2123 goto End;
2124 }
2125
2126 //Refresh Drive Layout
2127 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2128
2129 CHECK_CLOSE_HANDLE(hDrive);
2130
2131
2132 if (PartResizePreCheck(NULL) && pPhyDrive->ResizeNoShrink)
2133 {
2134 Log("Recheck after Shrink volume success");
2135 Log("After shrink Disksize:%llu Part2Start:%llu", pPhyDrive->SizeInBytes, pPhyDrive->ResizePart2StartSector * 512);
2136 }
2137 else
2138 {
2139 Log("Recheck after Shrink volume failed %u", pPhyDrive->ResizeNoShrink);
2140 goto End;
2141 }
2142 }
2143 else
2144 {
2145 Log("Shrink volume failed");
2146 goto End;
2147 }
2148 }
2149
2150
2151 //Now try write data
2152 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2153 if (hDrive == INVALID_HANDLE_VALUE)
2154 {
2155 Log("Failed to GetPhysicalHandle for update.");
2156 goto End;
2157 }
2158
2159
2160 //Write partition 2 data
2161 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
2162 if (0 != FormatPart2Fat(hDrive, pPhyDrive->ResizePart2StartSector))
2163 {
2164 Log("FormatPart2Fat failed.");
2165 goto End;
2166 }
2167
2168 //Write grub stage2 gap
2169 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
2170 Log("Writing Boot Image ............................. ");
2171 if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)
2172 {
2173 Log("WriteGrubStage1ToPhyDrive failed.");
2174 goto End;
2175 }
2176
2177
2178 //Write partition table
2179 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
2180 Log("Writing partition table ............................. ");
2181
2182 VentoyGetLocalBootImg(&MBR);
2183 CoCreateGuid(&Guid);
2184 memcpy(MBR.BootCode + 0x180, &Guid, 16);
2185 memcpy(pMBR->BootCode, MBR.BootCode, 440);
2186
2187 if (PartStyle == 0)
2188 {
2189 for (i = 1; i < 4; i++)
2190 {
2191 if (pMBR->PartTbl[i].SectorCount == 0)
2192 {
2193 break;
2194 }
2195 }
2196
2197 if (i >= 4)
2198 {
2199 Log("Can not find MBR free partition table");
2200 goto End;
2201 }
2202
2203 for (j = i - 1; j > 0; j--)
2204 {
2205 Log("Move MBR partition table %d --> %d", j + 1, j + 2);
2206 memcpy(pMBR->PartTbl + (j + 1), pMBR->PartTbl + j, sizeof(PART_TABLE));
2207 }
2208
2209 memset(pMBR->PartTbl + 1, 0, sizeof(PART_TABLE));
2210 VentoyFillMBRLocation(pPhyDrive->SizeInBytes, (UINT32)pPhyDrive->ResizePart2StartSector, VENTOY_EFI_PART_SIZE / 512, pMBR->PartTbl + 1);
2211 pMBR->PartTbl[0].Active = 0x80; // bootable
2212 pMBR->PartTbl[1].Active = 0x00;
2213 pMBR->PartTbl[1].FsFlag = 0xEF; // EFI System Partition
2214
2215 if (!WriteDataToPhyDisk(hDrive, 0, pMBR, 512))
2216 {
2217 Log("Legacy BIOS write MBR failed");
2218 goto End;
2219 }
2220 }
2221 else
2222 {
2223 for (i = 1; i < 128; i++)
2224 {
2225 if (memcmp(&(pGPT->PartTbl[i].PartGuid), &ZeroGuid, sizeof(GUID)) == 0)
2226 {
2227 break;
2228 }
2229 }
2230
2231 if (i >= 128)
2232 {
2233 Log("Can not find GPT free partition table");
2234 goto End;
2235 }
2236
2237 for (j = i - 1; j > 0; j--)
2238 {
2239 Log("Move GPT partition table %d --> %d", j + 1, j + 2);
2240 memcpy(pGPT->PartTbl + (j + 1), pGPT->PartTbl + j, sizeof(VTOY_GPT_PART_TBL));
2241 }
2242
2243
2244 pMBR->BootCode[92] = 0x22;
2245
2246 // to fix windows issue
2247 memset(pGPT->PartTbl + 1, 0, sizeof(VTOY_GPT_PART_TBL));
2248 memcpy(&(pGPT->PartTbl[1].PartType), &WindowsDataPartType, sizeof(GUID));
2249 CoCreateGuid(&(pGPT->PartTbl[1].PartGuid));
2250
2251 pGPT->PartTbl[1].StartLBA = pGPT->PartTbl[0].LastLBA + 1;
2252 pGPT->PartTbl[1].LastLBA = pGPT->PartTbl[1].StartLBA + VENTOY_EFI_PART_SIZE / 512 - 1;
2253 pGPT->PartTbl[1].Attr = 0xC000000000000001ULL;
2254 memcpy(pGPT->PartTbl[1].Name, L"VTOYEFI", 7 * 2);
2255
2256 //Update CRC
2257 pGPT->Head.PartTblCrc = VentoyCrc32(pGPT->PartTbl, sizeof(pGPT->PartTbl));
2258 pGPT->Head.Crc = 0;
2259 pGPT->Head.Crc = VentoyCrc32(&(pGPT->Head), pGPT->Head.Length);
2260
2261 Log("pGPT->Head.EfiStartLBA=%llu", (ULONGLONG)pGPT->Head.EfiStartLBA);
2262 Log("pGPT->Head.EfiBackupLBA=%llu", (ULONGLONG)pGPT->Head.EfiBackupLBA);
2263
2264 VentoyFillBackupGptHead(pGPT, &BackupHead);
2265 if (!WriteDataToPhyDisk(hDrive, pGPT->Head.EfiBackupLBA * 512, &BackupHead, 512))
2266 {
2267 Log("UEFI write backup head failed");
2268 goto End;
2269 }
2270
2271 if (!WriteDataToPhyDisk(hDrive, (pGPT->Head.EfiBackupLBA - 32) * 512, pGPT->PartTbl, 512 * 32))
2272 {
2273 Log("UEFI write backup partition table failed");
2274 goto End;
2275 }
2276
2277 if (!WriteDataToPhyDisk(hDrive, 0, pGPT, 512 * 34))
2278 {
2279 Log("UEFI write MBR & Main partition table failed");
2280 goto End;
2281 }
2282 }
2283
2284
2285
2286 //Refresh Drive Layout
2287 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2288
2289 //We must close handle here, because it will block the refresh bellow
2290 CHECK_CLOSE_HANDLE(hDrive);
2291
2292 Sleep(2000);
2293
2294 //Refresh disk list
2295 PhyDrive = pPhyDrive->PhyDrive;
2296
2297 Log("#### Now Refresh PhyDrive ####");
2298 Ventoy2DiskDestroy();
2299 Ventoy2DiskInit();
2300
2301 pPhyDrive = GetPhyDriveInfoByPhyDrive(PhyDrive);
2302 if (pPhyDrive)
2303 {
2304 if (pPhyDrive->VentoyVersion[0] == 0)
2305 {
2306 Log("After process the Ventoy version is still invalid");
2307 goto End;
2308 }
2309
2310 Log("### Ventoy non-destructive installation successfully finished <%s>", pPhyDrive->VentoyVersion);
2311 }
2312 else
2313 {
2314 Log("### Ventoy non-destructive installation successfully finished <not found>");
2315 }
2316
2317 InitComboxCtrl(g_DialogHwnd, PhyDrive);
2318
2319 rc = 0;
2320
2321 End:
2322 CHECK_CLOSE_HANDLE(hDrive);
2323 return rc;
2324 }
2325
2326
2327 static BOOL DiskCheckWriteAccess(HANDLE hDrive)
2328 {
2329 DWORD dwSize;
2330 BOOL ret = FALSE;
2331 BOOL bRet = FALSE;
2332 BYTE Buffer[512];
2333 LARGE_INTEGER liCurPosition;
2334 LARGE_INTEGER liNewPosition;
2335
2336 liCurPosition.QuadPart = 2039 * 512;
2337 liNewPosition.QuadPart = 0;
2338 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2339 liNewPosition.QuadPart != liCurPosition.QuadPart)
2340 {
2341 Log("SetFilePointer1 Failed %u", LASTERR);
2342 goto out;
2343 }
2344
2345
2346 dwSize = 0;
2347 ret = ReadFile(hDrive, Buffer, 512, &dwSize, NULL);
2348 if ((!ret) || (dwSize != 512))
2349 {
2350 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2351 goto out;
2352 }
2353
2354
2355 liCurPosition.QuadPart = 2039 * 512;
2356 liNewPosition.QuadPart = 0;
2357 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2358 liNewPosition.QuadPart != liCurPosition.QuadPart)
2359 {
2360 Log("SetFilePointer2 Failed %u", LASTERR);
2361 goto out;
2362 }
2363
2364 dwSize = 0;
2365 ret = WriteFile(hDrive, Buffer, 512, &dwSize, NULL);
2366 if ((!ret) || dwSize != 512)
2367 {
2368 Log("Failed to write %d %u %u", ret, dwSize, LASTERR);
2369 goto out;
2370 }
2371
2372 bRet = TRUE;
2373
2374 out:
2375
2376 return bRet;
2377 }
2378
2379 static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBackup)
2380 {
2381 DWORD dwSize;
2382 DWORD dwStatus;
2383 BOOL Return = FALSE;
2384 BOOL ret = FALSE;
2385 BYTE *backup = NULL;
2386 UINT64 offset;
2387 HANDLE hDrive = INVALID_HANDLE_VALUE;
2388 LARGE_INTEGER liCurPosition;
2389 LARGE_INTEGER liNewPosition;
2390 VTOY_GPT_INFO *pGPT = NULL;
2391
2392 Log("BackupDataBeforeCleanDisk %d", PhyDrive);
2393
2394 // step1: check write access
2395 hDrive = GetPhysicalHandle(PhyDrive, TRUE, TRUE, FALSE);
2396 if (hDrive == INVALID_HANDLE_VALUE)
2397 {
2398 Log("Failed to GetPhysicalHandle for write.");
2399 goto out;
2400 }
2401
2402 if (DiskCheckWriteAccess(hDrive))
2403 {
2404 Log("DiskCheckWriteAccess success");
2405 CHECK_CLOSE_HANDLE(hDrive);
2406 }
2407 else
2408 {
2409 Log("DiskCheckWriteAccess failed");
2410 goto out;
2411 }
2412
2413 //step2 backup 4MB data
2414 backup = malloc(SIZE_1MB * 4);
2415 if (!backup)
2416 {
2417 goto out;
2418 }
2419
2420 hDrive = GetPhysicalHandle(PhyDrive, FALSE, FALSE, FALSE);
2421 if (hDrive == INVALID_HANDLE_VALUE)
2422 {
2423 goto out;
2424 }
2425
2426 //read first 2MB
2427 dwStatus = SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2428 if (dwStatus != 0)
2429 {
2430 goto out;
2431 }
2432
2433 dwSize = 0;
2434 ret = ReadFile(hDrive, backup, SIZE_2MB, &dwSize, NULL);
2435 if ((!ret) || (dwSize != SIZE_2MB))
2436 {
2437 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2438 goto out;
2439 }
2440
2441 pGPT = (VTOY_GPT_INFO *)backup;
2442 offset = pGPT->Head.EfiBackupLBA * 512;
2443 if (offset >= (DiskSize - SIZE_2MB) && offset < DiskSize)
2444 {
2445 Log("EFI partition table check success");
2446 }
2447 else
2448 {
2449 Log("Backup EFI LBA not in last 2MB range: %llu", pGPT->Head.EfiBackupLBA);
2450 goto out;
2451 }
2452
2453 //read last 2MB
2454 liCurPosition.QuadPart = DiskSize - SIZE_2MB;
2455 liNewPosition.QuadPart = 0;
2456 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2457 liNewPosition.QuadPart != liCurPosition.QuadPart)
2458 {
2459 goto out;
2460 }
2461
2462 dwSize = 0;
2463 ret = ReadFile(hDrive, backup + SIZE_2MB, SIZE_2MB, &dwSize, NULL);
2464 if ((!ret) || (dwSize != SIZE_2MB))
2465 {
2466 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2467 goto out;
2468 }
2469
2470 *pBackup = backup;
2471 backup = NULL; //For don't free later
2472 Return = TRUE;
2473
2474 out:
2475 CHECK_CLOSE_HANDLE(hDrive);
2476 if (backup)
2477 free(backup);
2478
2479 return Return;
2480 }
2481
2482
2483 static BOOL WriteBackupDataToDisk(HANDLE hDrive, UINT64 Offset, BYTE *Data, DWORD Length)
2484 {
2485 DWORD dwSize = 0;
2486 BOOL ret = FALSE;
2487 LARGE_INTEGER liCurPosition;
2488 LARGE_INTEGER liNewPosition;
2489
2490 Log("WriteBackupDataToDisk %llu %p %u", Offset, Data, Length);
2491
2492 liCurPosition.QuadPart = Offset;
2493 liNewPosition.QuadPart = 0;
2494 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2495 liNewPosition.QuadPart != liCurPosition.QuadPart)
2496 {
2497 return FALSE;
2498 }
2499
2500 ret = WriteFile(hDrive, Data, Length, &dwSize, NULL);
2501 if ((!ret) || dwSize != Length)
2502 {
2503 Log("Failed to write %d %u %u", ret, dwSize, LASTERR);
2504 return FALSE;
2505 }
2506
2507 Log("WriteBackupDataToDisk %llu %p %u success", Offset, Data, Length);
2508 return TRUE;
2509 }
2510
2511
2512 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
2513 {
2514 int i;
2515 int rc = 0;
2516 int MaxRetry = 4;
2517 BOOL ForceMBR = FALSE;
2518 BOOL Esp2Basic = FALSE;
2519 BOOL ChangeAttr = FALSE;
2520 BOOL CleanDisk = FALSE;
2521 BOOL DelEFI = FALSE;
2522 BOOL bWriteBack = TRUE;
2523 HANDLE hVolume;
2524 HANDLE hDrive;
2525 DWORD Status;
2526 DWORD dwSize;
2527 BOOL bRet;
2528 CHAR DriveName[] = "?:\\";
2529 CHAR DriveLetters[MAX_PATH] = { 0 };
2530 CHAR BackBinFile[MAX_PATH];
2531 UINT64 StartSector;
2532 UINT64 ReservedMB = 0;
2533 MBR_HEAD BootImg;
2534 MBR_HEAD MBR;
2535 BYTE *pBackup = NULL;
2536 VTOY_GPT_INFO *pGptInfo = NULL;
2537 VTOY_GPT_INFO *pGptBkup = NULL;
2538 UINT8 ReservedData[4096];
2539
2540 Log("#####################################################");
2541 Log("UpdateVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
2542 pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
2543 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
2544 Log("#####################################################");
2545
2546 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2547
2548 Log("Lock disk for umount ............................ ");
2549
2550 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
2551 if (hDrive == INVALID_HANDLE_VALUE)
2552 {
2553 Log("Failed to open physical disk");
2554 return 1;
2555 }
2556
2557 if (pPhyDrive->PartStyle)
2558 {
2559 pGptInfo = malloc(2 * sizeof(VTOY_GPT_INFO));
2560 if (!pGptInfo)
2561 {
2562 return 1;
2563 }
2564
2565 memset(pGptInfo, 0, 2 * sizeof(VTOY_GPT_INFO));
2566 pGptBkup = pGptInfo + 1;
2567
2568 // Read GPT Info
2569 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2570 ReadFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL);
2571 memcpy(pGptBkup, pGptInfo, sizeof(VTOY_GPT_INFO));
2572
2573 //MBR will be used to compare with local boot image
2574 memcpy(&MBR, &pGptInfo->MBR, sizeof(MBR_HEAD));
2575
2576 StartSector = pGptInfo->PartTbl[1].StartLBA;
2577 Log("GPT StartSector in PartTbl:%llu", (ULONGLONG)StartSector);
2578
2579 ReservedMB = (pPhyDrive->SizeInBytes / 512 - (StartSector + VENTOY_EFI_PART_SIZE / 512) - 33) / 2048;
2580 Log("GPT Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);
2581 }
2582 else
2583 {
2584 // Read MBR
2585 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2586 ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
2587
2588 StartSector = MBR.PartTbl[1].StartSectorId;
2589 Log("MBR StartSector in PartTbl:%llu", (ULONGLONG)StartSector);
2590
2591 ReservedMB = (pPhyDrive->SizeInBytes / 512 - (StartSector + VENTOY_EFI_PART_SIZE / 512)) / 2048;
2592 Log("MBR Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);
2593 }
2594
2595 //Read Reserved Data
2596 SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);
2597 ReadFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);
2598
2599 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
2600
2601 if (DriveLetters[0] == 0)
2602 {
2603 Log("No drive letter was assigned...");
2604 }
2605 else
2606 {
2607 // Unmount all mounted volumes that belong to this drive
2608 // Do it in reverse so that we always end on the first volume letter
2609 for (i = (int)strlen(DriveLetters); i > 0; i--)
2610 {
2611 DriveName[0] = DriveLetters[i - 1];
2612 if (IsVentoyLogicalDrive(DriveName[0]))
2613 {
2614 Log("%s is ventoy logical drive", DriveName);
2615 bRet = DeleteVolumeMountPointA(DriveName);
2616 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, LASTERR);
2617 break;
2618 }
2619 }
2620 }
2621
2622 // It kind of blows, but we have to relinquish access to the physical drive
2623 // for VDS to be able to delete the partitions that reside on it...
2624 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2625 CHECK_CLOSE_HANDLE(hDrive);
2626
2627 if (pPhyDrive->PartStyle == 1)
2628 {
2629 Log("TryId=%d EFI GPT partition type is 0x%llx", TryId, pPhyDrive->Part2GPTAttr);
2630 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
2631
2632 if (TryId == 1)
2633 {
2634 Log("Change GPT partition type to ESP");
2635 if (DISK_ChangeVtoyEFI2ESP(pPhyDrive->PhyDrive, StartSector * 512ULL))
2636 {
2637 Esp2Basic = TRUE;
2638 Sleep(3000);
2639 }
2640 }
2641 else if (TryId == 2)
2642 {
2643 Log("Change GPT partition attribute");
2644 if (DISK_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, StartSector * 512ULL, 0x8000000000000001))
2645 {
2646 ChangeAttr = TRUE;
2647 Sleep(2000);
2648 }
2649 }
2650 else if (TryId == 3)
2651 {
2652 DISK_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive, StartSector * 512ULL);
2653 DelEFI = TRUE;
2654 }
2655 else if (TryId == 4)
2656 {
2657 Log("Clean disk GPT partition table");
2658 if (BackupDataBeforeCleanDisk(pPhyDrive->PhyDrive, pPhyDrive->SizeInBytes, &pBackup))
2659 {
2660 sprintf_s(BackBinFile, sizeof(BackBinFile), ".\\ventoy\\phydrive%d_%u_%d.bin",
2661 pPhyDrive->PhyDrive, GetCurrentProcessId(), g_backup_bin_index++);
2662 SaveBufToFile(BackBinFile, pBackup, 4 * SIZE_1MB);
2663 Log("Save backup data to %s", BackBinFile);
2664
2665 Log("Success to backup data before clean");
2666 CleanDisk = TRUE;
2667 DISK_CleanDisk(pPhyDrive->PhyDrive);
2668 Sleep(3000);
2669 }
2670 else
2671 {
2672 Log("Failed to backup data before clean");
2673 }
2674 }
2675 }
2676
2677 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
2678
2679 Log("Lock disk for update ............................ ");
2680 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2681 if (hDrive == INVALID_HANDLE_VALUE)
2682 {
2683 Log("Failed to GetPhysicalHandle for write.");
2684 rc = 1;
2685 goto End;
2686 }
2687
2688 PROGRESS_BAR_SET_POS(PT_LOCK_VOLUME);
2689
2690 Log("Lock volume for update .......................... ");
2691 hVolume = INVALID_HANDLE_VALUE;
2692
2693 //If we change VTOYEFI to ESP, it can not have s volume name, so don't try to get it.
2694 if (CleanDisk)
2695 {
2696 //writeback the last 2MB
2697 if (!WriteBackupDataToDisk(hDrive, pPhyDrive->SizeInBytes - SIZE_2MB, pBackup + SIZE_2MB, SIZE_2MB))
2698 {
2699 bWriteBack = FALSE;
2700 }
2701
2702 //write the first 2MB except parttable
2703 if (!WriteBackupDataToDisk(hDrive, 34 * 512, pBackup + 34 * 512, SIZE_2MB - 34 * 512))
2704 {
2705 bWriteBack = FALSE;
2706 }
2707
2708 Status = ERROR_NOT_FOUND;
2709 }
2710 else if (DelEFI)
2711 {
2712 Status = ERROR_NOT_FOUND;
2713 }
2714 else if (Esp2Basic)
2715 {
2716 Status = ERROR_NOT_FOUND;
2717 }
2718 else
2719 {
2720 for (i = 0; i < MaxRetry; i++)
2721 {
2722 Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, StartSector, DriveLetters, sizeof(DriveLetters), TRUE);
2723 if (ERROR_SUCCESS == Status)
2724 {
2725 break;
2726 }
2727 else
2728 {
2729 Log("==== Volume not found, wait and retry %d... ====", i);
2730 Sleep(2);
2731 }
2732 }
2733 }
2734
2735 if (ERROR_SUCCESS == Status)
2736 {
2737 Log("Now lock and dismount volume <%s>", DriveLetters);
2738
2739 for (i = 0; i < MaxRetry; i++)
2740 {
2741 hVolume = CreateFileA(DriveLetters,
2742 GENERIC_READ | GENERIC_WRITE,
2743 FILE_SHARE_READ,
2744 NULL,
2745 OPEN_EXISTING,
2746 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
2747 NULL);
2748
2749 if (hVolume == INVALID_HANDLE_VALUE)
2750 {
2751 Log("Failed to create file volume, errcode:%u, wait and retry ...", LASTERR);
2752 Sleep(2000);
2753 }
2754 else
2755 {
2756 break;
2757 }
2758 }
2759
2760 if (hVolume == INVALID_HANDLE_VALUE)
2761 {
2762 Log("Failed to create file volume, errcode:%u", LASTERR);
2763 }
2764 else
2765 {
2766 bRet = DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2767 Log("FSCTL_LOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2768
2769 bRet = DeviceIoControl(hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2770 Log("FSCTL_DISMOUNT_VOLUME bRet:%u code:%u", bRet, LASTERR);
2771 }
2772 }
2773 else if (ERROR_NOT_FOUND == Status)
2774 {
2775 Log("Volume not found, maybe not supported");
2776 }
2777 else
2778 {
2779 rc = 1;
2780 goto End;
2781 }
2782
2783 bRet = TryWritePart2(hDrive, StartSector);
2784 if (FALSE == bRet && Esp2Basic)
2785 {
2786 Log("TryWritePart2 agagin ...");
2787 Sleep(3000);
2788 bRet = TryWritePart2(hDrive, StartSector);
2789 }
2790
2791 if (!bRet)
2792 {
2793 if (pPhyDrive->PartStyle == 0)
2794 {
2795 if (DiskCheckWriteAccess(hDrive))
2796 {
2797 Log("MBR DiskCheckWriteAccess success");
2798
2799 ForceMBR = TRUE;
2800
2801 Log("Try write failed, now delete partition 2 for MBR...");
2802 CHECK_CLOSE_HANDLE(hDrive);
2803
2804 Log("Now delete partition 2...");
2805 DISK_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive, StartSector * 512ULL);
2806
2807 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2808 if (hDrive == INVALID_HANDLE_VALUE)
2809 {
2810 Log("Failed to GetPhysicalHandle for write.");
2811 rc = 1;
2812 goto End;
2813 }
2814 }
2815 else
2816 {
2817 Log("MBR DiskCheckWriteAccess failed");
2818 }
2819 }
2820 else
2821 {
2822 Log("TryWritePart2 failed ....");
2823 rc = 1;
2824 goto End;
2825 }
2826 }
2827
2828 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
2829
2830 Log("Write Ventoy to disk ............................ ");
2831 if (0 != FormatPart2Fat(hDrive, StartSector))
2832 {
2833 rc = 1;
2834 goto End;
2835 }
2836
2837 if (hVolume != INVALID_HANDLE_VALUE)
2838 {
2839 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2840 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2841 CHECK_CLOSE_HANDLE(hVolume);
2842 }
2843
2844 Log("Updating Boot Image ............................. ");
2845 if (WriteGrubStage1ToPhyDrive(hDrive, pPhyDrive->PartStyle) != 0)
2846 {
2847 rc = 1;
2848 goto End;
2849 }
2850
2851 //write reserved data
2852 SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);
2853 bRet = WriteFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);
2854 Log("Write resv data ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2855
2856 // Boot Image
2857 VentoyGetLocalBootImg(&BootImg);
2858
2859 // Use Old UUID
2860 memcpy(BootImg.BootCode + 0x180, MBR.BootCode + 0x180, 16);
2861 if (pPhyDrive->PartStyle)
2862 {
2863 BootImg.BootCode[92] = 0x22;
2864 }
2865
2866 if (ForceMBR == FALSE && memcmp(BootImg.BootCode, MBR.BootCode, 440) == 0)
2867 {
2868 Log("Boot image has no difference, no need to write.");
2869 }
2870 else
2871 {
2872 Log("Boot image need to write %u.", ForceMBR);
2873
2874 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2875
2876 memcpy(MBR.BootCode, BootImg.BootCode, 440);
2877 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
2878 Log("Write Boot Image ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2879 }
2880
2881 if (pPhyDrive->PartStyle == 0)
2882 {
2883 if (0x00 == MBR.PartTbl[0].Active && 0x80 == MBR.PartTbl[1].Active)
2884 {
2885 Log("Need to chage 1st partition active and 2nd partition inactive.");
2886
2887 MBR.PartTbl[0].Active = 0x80;
2888 MBR.PartTbl[1].Active = 0x00;
2889
2890 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2891 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
2892 Log("Write NEW MBR ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2893 }
2894 }
2895
2896 if (CleanDisk)
2897 {
2898 if (!WriteBackupDataToDisk(hDrive, 0, pBackup, 34 * 512))
2899 {
2900 bWriteBack = FALSE;
2901 }
2902
2903 free(pBackup);
2904
2905 if (bWriteBack)
2906 {
2907 Log("Write backup data success, now delete %s", BackBinFile);
2908 DeleteFileA(BackBinFile);
2909 }
2910 else
2911 {
2912 Log("Write backup data failed");
2913 }
2914
2915 Sleep(1000);
2916 }
2917 else if (DelEFI)
2918 {
2919 VTOY_GPT_HDR BackupHdr;
2920
2921 VentoyFillBackupGptHead(pGptBkup, &BackupHdr);
2922 if (!WriteBackupDataToDisk(hDrive, 512 * pGptBkup->Head.EfiBackupLBA, (BYTE*)(&BackupHdr), 512))
2923 {
2924 bWriteBack = FALSE;
2925 }
2926
2927 if (!WriteBackupDataToDisk(hDrive, 512 * (pGptBkup->Head.EfiBackupLBA - 32), (BYTE*)(pGptBkup->PartTbl), 32 * 512))
2928 {
2929 bWriteBack = FALSE;
2930 }
2931
2932 if (!WriteBackupDataToDisk(hDrive, 512, (BYTE*)pGptBkup + 512, 33 * 512))
2933 {
2934 bWriteBack = FALSE;
2935 }
2936
2937 if (bWriteBack)
2938 {
2939 Log("Write backup partition table success");
2940 }
2941 else
2942 {
2943 Log("Write backup partition table failed");
2944 }
2945
2946 Sleep(1000);
2947 }
2948
2949 //Refresh Drive Layout
2950 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2951
2952 End:
2953
2954 if (hVolume != INVALID_HANDLE_VALUE)
2955 {
2956 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2957 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2958 CHECK_CLOSE_HANDLE(hVolume);
2959 }
2960
2961 if (rc == 0)
2962 {
2963 Log("OK");
2964 }
2965 else
2966 {
2967 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2968 FindProcessOccupyDisk(hDrive, pPhyDrive);
2969 }
2970
2971 CHECK_CLOSE_HANDLE(hDrive);
2972
2973 if (Esp2Basic)
2974 {
2975 Log("Recover GPT partition type to basic");
2976 DISK_ChangeVtoyEFI2Basic(pPhyDrive->PhyDrive, StartSector * 512);
2977 }
2978
2979 if (pPhyDrive->PartStyle == 1)
2980 {
2981 if (ChangeAttr || ((pPhyDrive->Part2GPTAttr >> 56) != 0xC0))
2982 {
2983 Log("Change EFI partition attr %u <0x%llx> to <0x%llx>", ChangeAttr, pPhyDrive->Part2GPTAttr, 0xC000000000000001ULL);
2984 if (DISK_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, StartSector * 512ULL, 0xC000000000000001ULL))
2985 {
2986 Log("Change EFI partition attr success");
2987 pPhyDrive->Part2GPTAttr = 0xC000000000000001ULL;
2988 }
2989 else
2990 {
2991 Log("Change EFI partition attr failed");
2992 }
2993 }
2994 }
2995
2996 if (pGptInfo)
2997 {
2998 free(pGptInfo);
2999 }
3000
3001 return rc;
3002 }
3003
3004