]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
c1ebe5e0a58ec9cf4e0a60bb0453ffe54187b63c
[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
735 file = fl_fopen("/EFI/BOOT/BOOTX64.EFI", "wb");
736 Log("Open bootx64 efi file %p ", file);
737 if (file)
738 {
739 if (filebuf)
740 {
741 fl_fwrite(filebuf, 1, size, file);
742 }
743
744 fl_fflush(file);
745 fl_fclose(file);
746 }
747
748 if (filebuf)
749 {
750 free(filebuf);
751 }
752 }
753
754 file = fl_fopen("/EFI/BOOT/grubia32_real.efi", "rb");
755 Log("Open ventoy efi file %p ", file);
756 if (file)
757 {
758 fl_fseek(file, 0, SEEK_END);
759 size = (int)fl_ftell(file);
760 fl_fseek(file, 0, SEEK_SET);
761
762 Log("ventoy efi file size %d ...", size);
763
764 filebuf = (char *)malloc(size);
765 if (filebuf)
766 {
767 fl_fread(filebuf, 1, size, file);
768 }
769
770 fl_fclose(file);
771
772 Log("Now delete all efi files ...");
773 fl_remove("/EFI/BOOT/BOOTIA32.EFI");
774 fl_remove("/EFI/BOOT/grubia32.efi");
775 fl_remove("/EFI/BOOT/grubia32_real.efi");
776 fl_remove("/EFI/BOOT/mmia32.efi");
777
778 file = fl_fopen("/EFI/BOOT/BOOTIA32.EFI", "wb");
779 Log("Open bootia32 efi file %p ", file);
780 if (file)
781 {
782 if (filebuf)
783 {
784 fl_fwrite(filebuf, 1, size, file);
785 }
786
787 fl_fflush(file);
788 fl_fclose(file);
789 }
790
791 if (filebuf)
792 {
793 free(filebuf);
794 }
795 }
796
797 }
798 else
799 {
800 rc = 1;
801 }
802
803 fl_shutdown();
804
805 return rc;
806 }
807
808
809
810 static int disk_xz_flush(void *src, unsigned int size)
811 {
812 unsigned int i;
813 BYTE *buf = (BYTE *)src;
814
815 for (i = 0; i < size; i++)
816 {
817 *g_part_img_pos = *buf++;
818
819 g_disk_unxz_len++;
820 if ((g_disk_unxz_len % SIZE_1MB) == 0)
821 {
822 g_part_img_pos = g_part_img_buf[g_disk_unxz_len / SIZE_1MB];
823 }
824 else
825 {
826 g_part_img_pos++;
827 }
828 }
829
830 return (int)size;
831 }
832
833 static void unxz_error(char *x)
834 {
835 Log("%s", x);
836 }
837
838 static BOOL TryWritePart2(HANDLE hDrive, UINT64 StartSectorId)
839 {
840 BOOL bRet;
841 DWORD TrySize = 16 * 1024;
842 DWORD dwSize;
843 BYTE *Buffer = NULL;
844 unsigned char *data = NULL;
845 LARGE_INTEGER liCurrentPosition;
846
847 liCurrentPosition.QuadPart = StartSectorId * 512;
848 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
849
850 Buffer = malloc(TrySize);
851
852 bRet = WriteFile(hDrive, Buffer, TrySize, &dwSize, NULL);
853
854 free(Buffer);
855
856 Log("Try write part2 bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
857
858 if (bRet && dwSize == TrySize)
859 {
860 return TRUE;
861 }
862
863 return FALSE;
864 }
865
866 static int FormatPart2Fat(HANDLE hDrive, UINT64 StartSectorId)
867 {
868 int i;
869 int rc = 0;
870 int len = 0;
871 int writelen = 0;
872 int partwrite = 0;
873 int Pos = PT_WRITE_VENTOY_START;
874 DWORD dwSize = 0;
875 BOOL bRet;
876 unsigned char *data = NULL;
877 LARGE_INTEGER liCurrentPosition;
878 LARGE_INTEGER liNewPosition;
879 BYTE *CheckBuf = NULL;
880
881 Log("FormatPart2Fat %llu...", (ULONGLONG)StartSectorId);
882
883 CheckBuf = malloc(SIZE_1MB);
884 if (!CheckBuf)
885 {
886 Log("Failed to malloc check buf");
887 return 1;
888 }
889
890 rc = ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG, 0, (void **)&data, &len);
891 if (rc)
892 {
893 Log("Failed to read img file %p %u", data, len);
894 free(CheckBuf);
895 return 1;
896 }
897
898 liCurrentPosition.QuadPart = StartSectorId * 512;
899 SetFilePointerEx(hDrive, liCurrentPosition, &liNewPosition, FILE_BEGIN);
900
901 memset(g_part_img_buf, 0, sizeof(g_part_img_buf));
902
903 g_part_img_buf[0] = (BYTE *)malloc(VENTOY_EFI_PART_SIZE);
904 if (g_part_img_buf[0])
905 {
906 Log("Malloc whole img buffer success, now decompress ...");
907 unxz(data, len, NULL, NULL, g_part_img_buf[0], &writelen, unxz_error);
908
909 if (len == writelen)
910 {
911 Log("decompress finished success");
912
913 VentoyProcSecureBoot(g_SecureBoot);
914
915 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
916 {
917 dwSize = 0;
918 bRet = WriteFile(hDrive, g_part_img_buf[0] + i * SIZE_1MB, SIZE_1MB, &dwSize, NULL);
919 Log("Write part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
920
921 if (!bRet)
922 {
923 rc = 1;
924 goto End;
925 }
926
927 PROGRESS_BAR_SET_POS(Pos);
928 if (i % 2 == 0)
929 {
930 Pos++;
931 }
932 }
933
934 //Read and check the data
935 liCurrentPosition.QuadPart = StartSectorId * 512;
936 SetFilePointerEx(hDrive, liCurrentPosition, &liNewPosition, FILE_BEGIN);
937
938 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
939 {
940 bRet = ReadFile(hDrive, CheckBuf, SIZE_1MB, &dwSize, NULL);
941 Log("Read part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
942
943 if (!bRet || memcmp(CheckBuf, g_part_img_buf[0] + i * SIZE_1MB, SIZE_1MB))
944 {
945 Log("### [Check Fail] The data write and read does not match");
946 rc = 1;
947 goto End;
948 }
949
950 PROGRESS_BAR_SET_POS(Pos);
951 if (i % 2 == 0)
952 {
953 Pos++;
954 }
955 }
956 }
957 else
958 {
959 rc = 1;
960 Log("decompress finished failed");
961 goto End;
962 }
963 }
964 else
965 {
966 Log("Failed to malloc whole img size %u, now split it", VENTOY_EFI_PART_SIZE);
967
968 partwrite = 1;
969 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
970 {
971 g_part_img_buf[i] = (BYTE *)malloc(SIZE_1MB);
972 if (g_part_img_buf[i] == NULL)
973 {
974 rc = 1;
975 goto End;
976 }
977 }
978
979 Log("Malloc part img buffer success, now decompress ...");
980
981 g_part_img_pos = g_part_img_buf[0];
982
983 unxz(data, len, NULL, disk_xz_flush, NULL, NULL, unxz_error);
984
985 if (g_disk_unxz_len == VENTOY_EFI_PART_SIZE)
986 {
987 Log("decompress finished success");
988
989 VentoyProcSecureBoot(g_SecureBoot);
990
991 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
992 {
993 dwSize = 0;
994 bRet = WriteFile(hDrive, g_part_img_buf[i], SIZE_1MB, &dwSize, NULL);
995 Log("Write part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
996
997 if (!bRet)
998 {
999 rc = 1;
1000 goto End;
1001 }
1002
1003 PROGRESS_BAR_SET_POS(Pos);
1004 if (i % 2 == 0)
1005 {
1006 Pos++;
1007 }
1008 }
1009
1010 //Read and check the data
1011 liCurrentPosition.QuadPart = StartSectorId * 512;
1012 SetFilePointerEx(hDrive, liCurrentPosition, &liNewPosition, FILE_BEGIN);
1013
1014 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1015 {
1016 bRet = ReadFile(hDrive, CheckBuf, SIZE_1MB, &dwSize, NULL);
1017 Log("Read part data bRet:%u dwSize:%u code:%u", bRet, dwSize, LASTERR);
1018
1019 if (!bRet || memcmp(CheckBuf, g_part_img_buf[i], SIZE_1MB))
1020 {
1021 Log("### [Check Fail] The data write and read does not match");
1022 rc = 1;
1023 goto End;
1024 }
1025
1026 PROGRESS_BAR_SET_POS(Pos);
1027 if (i % 2 == 0)
1028 {
1029 Pos++;
1030 }
1031 }
1032 }
1033 else
1034 {
1035 rc = 1;
1036 Log("decompress finished failed");
1037 goto End;
1038 }
1039 }
1040
1041 End:
1042
1043 if (data) free(data);
1044 if (CheckBuf)free(CheckBuf);
1045
1046 if (partwrite)
1047 {
1048 for (i = 0; i < VENTOY_EFI_PART_SIZE / SIZE_1MB; i++)
1049 {
1050 if (g_part_img_buf[i]) free(g_part_img_buf[i]);
1051 }
1052 }
1053 else
1054 {
1055 if (g_part_img_buf[0]) free(g_part_img_buf[0]);
1056 }
1057
1058 return rc;
1059 }
1060
1061 static int WriteGrubStage1ToPhyDrive(HANDLE hDrive, int PartStyle)
1062 {
1063 int Len = 0;
1064 int readLen = 0;
1065 BOOL bRet;
1066 DWORD dwSize;
1067 BYTE *ImgBuf = NULL;
1068 BYTE *RawBuf = NULL;
1069
1070 Log("WriteGrubStage1ToPhyDrive ...");
1071
1072 RawBuf = (BYTE *)malloc(SIZE_1MB);
1073 if (!RawBuf)
1074 {
1075 return 1;
1076 }
1077
1078 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG, 0, (void **)&ImgBuf, &Len))
1079 {
1080 Log("Failed to read stage1 img");
1081 free(RawBuf);
1082 return 1;
1083 }
1084
1085 unxz(ImgBuf, Len, NULL, NULL, RawBuf, &readLen, unxz_error);
1086
1087 if (PartStyle)
1088 {
1089 Log("Write GPT stage1 ...");
1090 RawBuf[500] = 35;//update blocklist
1091 SetFilePointer(hDrive, 512 * 34, NULL, FILE_BEGIN);
1092 bRet = WriteFile(hDrive, RawBuf, SIZE_1MB - 512 * 34, &dwSize, NULL);
1093 }
1094 else
1095 {
1096 Log("Write MBR stage1 ...");
1097 SetFilePointer(hDrive, 512, NULL, FILE_BEGIN);
1098 bRet = WriteFile(hDrive, RawBuf, SIZE_1MB - 512, &dwSize, NULL);
1099 }
1100
1101 Log("WriteFile Ret:%u dwSize:%u ErrCode:%u", bRet, dwSize, GetLastError());
1102
1103 free(RawBuf);
1104 free(ImgBuf);
1105 return 0;
1106 }
1107
1108
1109
1110 static int FormatPart1exFAT(UINT64 DiskSizeBytes)
1111 {
1112 MKFS_PARM Option;
1113 FRESULT Ret;
1114
1115 Option.fmt = FM_EXFAT;
1116 Option.n_fat = 1;
1117 Option.align = 8;
1118 Option.n_root = 1;
1119
1120 // < 32GB select 32KB as cluster size
1121 // > 32GB select 128KB as cluster size
1122 if (DiskSizeBytes / 1024 / 1024 / 1024 <= 32)
1123 {
1124 Option.au_size = 32768;
1125 }
1126 else
1127 {
1128 Option.au_size = 131072;
1129 }
1130
1131 Log("Formatting Part1 exFAT ...");
1132
1133 disk_io_reset_write_error();
1134
1135 Ret = f_mkfs(TEXT("0:"), &Option, 0, 8 * 1024 * 1024);
1136 if (FR_OK == Ret)
1137 {
1138 if (disk_io_is_write_error())
1139 {
1140 Log("Formatting Part1 exFAT failed, write error.");
1141 return 1;
1142 }
1143
1144 Log("Formatting Part1 exFAT success");
1145 return 0;
1146 }
1147 else
1148 {
1149 Log("Formatting Part1 exFAT failed");
1150 return 1;
1151 }
1152 }
1153
1154
1155
1156 int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLetter)
1157 {
1158 int i;
1159 int rc = 0;
1160 int state = 0;
1161 HANDLE hDrive;
1162 DWORD dwSize;
1163 BOOL bRet;
1164 CHAR MountDrive;
1165 CHAR DriveName[] = "?:\\";
1166 CHAR DriveLetters[MAX_PATH] = { 0 };
1167 LARGE_INTEGER liCurrentPosition;
1168 char *pTmpBuf = NULL;
1169 MBR_HEAD MBR;
1170
1171 *pDrvLetter = 0;
1172
1173 Log("ClearVentoyFromPhyDrive PhyDrive%d <<%s %s %dGB>>",
1174 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1175 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1176
1177 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1178
1179 Log("Lock disk for clean ............................. ");
1180
1181 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1182 if (hDrive == INVALID_HANDLE_VALUE)
1183 {
1184 Log("Failed to open physical disk");
1185 return 1;
1186 }
1187
1188 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1189
1190 if (DriveLetters[0] == 0)
1191 {
1192 Log("No drive letter was assigned...");
1193 DriveName[0] = GetFirstUnusedDriveLetter();
1194 Log("GetFirstUnusedDriveLetter %C: ...", DriveName[0]);
1195 }
1196 else
1197 {
1198 // Unmount all mounted volumes that belong to this drive
1199 // Do it in reverse so that we always end on the first volume letter
1200 for (i = (int)strlen(DriveLetters); i > 0; i--)
1201 {
1202 DriveName[0] = DriveLetters[i - 1];
1203 bRet = DeleteVolumeMountPointA(DriveName);
1204 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, GetLastError());
1205 }
1206 }
1207
1208 MountDrive = DriveName[0];
1209 Log("Will use '%C:' as volume mountpoint", DriveName[0]);
1210
1211 // It kind of blows, but we have to relinquish access to the physical drive
1212 // for VDS to be able to delete the partitions that reside on it...
1213 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1214 CHECK_CLOSE_HANDLE(hDrive);
1215
1216 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
1217
1218 if (!VDS_DeleteAllPartitions(pPhyDrive->PhyDrive))
1219 {
1220 Log("Notice: Could not delete partitions: %u", GetLastError());
1221 }
1222
1223 Log("Deleting all partitions ......................... OK");
1224
1225 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1226
1227 Log("Lock disk for write ............................. ");
1228 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1229 if (hDrive == INVALID_HANDLE_VALUE)
1230 {
1231 Log("Failed to GetPhysicalHandle for write.");
1232 rc = 1;
1233 goto End;
1234 }
1235
1236 // clear first and last 2MB space
1237 pTmpBuf = malloc(SIZE_2MB);
1238 if (!pTmpBuf)
1239 {
1240 Log("Failed to alloc memory.");
1241 rc = 1;
1242 goto End;
1243 }
1244 memset(pTmpBuf, 0, SIZE_2MB);
1245
1246 SET_FILE_POS(512);
1247 bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB - 512, &dwSize, NULL);
1248 Log("Write fisrt 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1249 if (!bRet)
1250 {
1251 rc = 1;
1252 goto End;
1253 }
1254
1255 SET_FILE_POS(pPhyDrive->SizeInBytes - SIZE_2MB);
1256 bRet = WriteFile(hDrive, pTmpBuf, SIZE_2MB, &dwSize, NULL);
1257 Log("Write 2nd 1MB ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1258 if (!bRet)
1259 {
1260 rc = 1;
1261 goto End;
1262 }
1263
1264 SET_FILE_POS(0);
1265
1266 if (pPhyDrive->SizeInBytes > 2199023255552ULL)
1267 {
1268 VTOY_GPT_INFO *pGptInfo;
1269 VTOY_GPT_HDR BackupHead;
1270 LARGE_INTEGER liCurrentPosition;
1271
1272 pGptInfo = (VTOY_GPT_INFO *)pTmpBuf;
1273
1274 VentoyFillWholeGpt(pPhyDrive->SizeInBytes, pGptInfo);
1275
1276 SET_FILE_POS(pPhyDrive->SizeInBytes - 512);
1277 VentoyFillBackupGptHead(pGptInfo, &BackupHead);
1278 if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))
1279 {
1280 rc = 1;
1281 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1282 goto End;
1283 }
1284
1285 SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);
1286 if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))
1287 {
1288 rc = 1;
1289 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1290 goto End;
1291 }
1292
1293 SET_FILE_POS(0);
1294 if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))
1295 {
1296 rc = 1;
1297 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1298 goto End;
1299 }
1300
1301 Log("Write GPT Info OK ...");
1302 }
1303 else
1304 {
1305 bRet = ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
1306 Log("Read MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1307 if (!bRet)
1308 {
1309 rc = 1;
1310 goto End;
1311 }
1312
1313 //clear boot code and partition table (reserved disk signature)
1314 memset(MBR.BootCode, 0, 440);
1315 memset(MBR.PartTbl, 0, sizeof(MBR.PartTbl));
1316
1317 VentoyFillMBRLocation(pPhyDrive->SizeInBytes, 2048, (UINT32)(pPhyDrive->SizeInBytes / 512 - 2048), MBR.PartTbl);
1318
1319 MBR.PartTbl[0].Active = 0x00; // bootable
1320 MBR.PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS
1321
1322 SET_FILE_POS(0);
1323 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
1324 Log("Write MBR ret:%d size:%u err:%d", bRet, dwSize, LASTERR);
1325 if (!bRet)
1326 {
1327 rc = 1;
1328 goto End;
1329 }
1330 }
1331
1332 Log("Clear Ventoy successfully finished");
1333
1334 //Refresh Drive Layout
1335 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1336
1337 End:
1338
1339 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1340 PROGRESS_BAR_SET_POS(PT_REFORMAT_FINISH);
1341
1342 if (pTmpBuf)
1343 {
1344 free(pTmpBuf);
1345 }
1346
1347 if (rc == 0)
1348 {
1349 Log("Mounting Ventoy Partition ....................... ");
1350 Sleep(1000);
1351
1352 state = 0;
1353 memset(DriveLetters, 0, sizeof(DriveLetters));
1354 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1355 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1356
1357 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1358 {
1359 DriveName[0] = DriveLetters[i];
1360 Log("%s is ventoy part1, already mounted", DriveName);
1361 state = 1;
1362 }
1363
1364 if (state != 1)
1365 {
1366 Log("need to mount ventoy part1...");
1367 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, 2048, DriveLetters, sizeof(DriveLetters), FALSE))
1368 {
1369 DriveName[0] = MountDrive;
1370 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1371 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1372
1373 *pDrvLetter = MountDrive;
1374 }
1375 else
1376 {
1377 Log("Failed to find ventoy volume");
1378 }
1379 }
1380
1381 Log("OK\n");
1382 }
1383 else
1384 {
1385 FindProcessOccupyDisk(hDrive, pPhyDrive);
1386 }
1387
1388 CHECK_CLOSE_HANDLE(hDrive);
1389 return rc;
1390 }
1391
1392 int InstallVentoy2FileImage(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
1393 {
1394 int i;
1395 int rc = 1;
1396 int Len = 0;
1397 int dataLen = 0;
1398 UINT size = 0;
1399 UINT segnum = 0;
1400 UINT32 chksum = 0;
1401 UINT64 data_offset = 0;
1402 UINT64 Part2StartSector = 0;
1403 UINT64 Part1StartSector = 0;
1404 UINT64 Part1SectorCount = 0;
1405 UINT8 *pData = NULL;
1406 UINT8 *pBkGptPartTbl = NULL;
1407 BYTE *ImgBuf = NULL;
1408 MBR_HEAD *pMBR = NULL;
1409 VTSI_FOOTER *pImgFooter = NULL;
1410 VTSI_SEGMENT *pSegment = NULL;
1411 VTOY_GPT_INFO *pGptInfo = NULL;
1412 VTOY_GPT_HDR *pBkGptHdr = NULL;
1413 FILE *fp = NULL;
1414
1415 Log("InstallVentoy2FileImage %s PhyDrive%d <<%s %s %dGB>>",
1416 PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1417 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1418
1419 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1420
1421 size = SIZE_1MB + VENTOY_EFI_PART_SIZE + 33 * 512 + VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT) + sizeof(VTSI_FOOTER);
1422
1423 pData = (UINT8 *)malloc(size);
1424 if (!pData)
1425 {
1426 Log("malloc image buffer failed %d.", size);
1427 goto End;
1428 }
1429
1430 pImgFooter = (VTSI_FOOTER *)(pData + size - sizeof(VTSI_FOOTER));
1431 pSegment = (VTSI_SEGMENT *)((UINT8 *)pImgFooter - VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT));
1432 memset(pImgFooter, 0, sizeof(VTSI_FOOTER));
1433 memset(pSegment, 0, VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT));
1434
1435 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START);
1436
1437 Log("Writing Boot Image ............................. ");
1438 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG, 0, (void **)&ImgBuf, &Len))
1439 {
1440 Log("Failed to read stage1 img");
1441 goto End;
1442 }
1443
1444 unxz(ImgBuf, Len, NULL, NULL, pData, &dataLen, unxz_error);
1445 SAFE_FREE(ImgBuf);
1446
1447 Log("decompress %s len:%d", VENTOY_FILE_STG1_IMG, dataLen);
1448
1449 if (PartStyle)
1450 {
1451 pData[500] = 35;//update blocklist
1452 memmove(pData + 34 * 512, pData, SIZE_1MB - 512 * 34);
1453 memset(pData, 0, 34 * 512);
1454
1455 pGptInfo = (VTOY_GPT_INFO *)pData;
1456 memset(pGptInfo, 0, sizeof(VTOY_GPT_INFO));
1457 VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);
1458
1459 pBkGptPartTbl = pData + SIZE_1MB + VENTOY_EFI_PART_SIZE;
1460 memset(pBkGptPartTbl, 0, 33 * 512);
1461
1462 memcpy(pBkGptPartTbl, pGptInfo->PartTbl, 32 * 512);
1463 pBkGptHdr = (VTOY_GPT_HDR *)(pBkGptPartTbl + 32 * 512);
1464 VentoyFillBackupGptHead(pGptInfo, pBkGptHdr);
1465
1466 Part1StartSector = pGptInfo->PartTbl[0].StartLBA;
1467 Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;
1468 Part2StartSector = pGptInfo->PartTbl[1].StartLBA;
1469
1470 Log("Write GPT Info OK ...");
1471 }
1472 else
1473 {
1474 memmove(pData + 512, pData, SIZE_1MB - 512);
1475 memset(pData, 0, 512);
1476
1477 pMBR = (MBR_HEAD *)pData;
1478 VentoyFillMBR(pPhyDrive->SizeInBytes, pMBR, PartStyle);
1479 Part1StartSector = pMBR->PartTbl[0].StartSectorId;
1480 Part1SectorCount = pMBR->PartTbl[0].SectorCount;
1481 Part2StartSector = pMBR->PartTbl[1].StartSectorId;
1482
1483 Log("Write MBR OK ...");
1484 }
1485
1486 Log("Writing EFI part Image ............................. ");
1487 rc = ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG, 0, (void **)&ImgBuf, &Len);
1488 if (rc)
1489 {
1490 Log("Failed to read img file %p %u", ImgBuf, Len);
1491 goto End;
1492 }
1493
1494 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START + 28);
1495 memset(g_part_img_buf, 0, sizeof(g_part_img_buf));
1496 unxz(ImgBuf, Len, NULL, NULL, pData + SIZE_1MB, &dataLen, unxz_error);
1497 if (dataLen == Len)
1498 {
1499 Log("decompress finished success");
1500 g_part_img_buf[0] = pData + SIZE_1MB;
1501
1502 VentoyProcSecureBoot(g_SecureBoot);
1503 }
1504 else
1505 {
1506 Log("decompress finished failed");
1507 goto End;
1508 }
1509
1510 fopen_s(&fp, "VentoySparseImg.vtsi", "wb+");
1511 if (!fp)
1512 {
1513 Log("Failed to create Ventoy img file");
1514 goto End;
1515 }
1516
1517 Log("Writing stage1 data ............................. ");
1518
1519 fwrite(pData, 1, SIZE_1MB, fp);
1520
1521 pSegment[0].disk_start_sector = 0;
1522 pSegment[0].sector_num = SIZE_1MB / 512;
1523 pSegment[0].data_offset = data_offset;
1524 data_offset += pSegment[0].sector_num * 512;
1525
1526 disk_io_set_param(INVALID_HANDLE_VALUE, Part1StartSector + Part1SectorCount);// include the 2048 sector gap
1527 disk_io_set_imghook(fp, pSegment + 1, VTSI_IMG_MAX_SEG - 1, data_offset);
1528
1529 Log("Formatting part1 exFAT ...");
1530 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
1531 {
1532 Log("FormatPart1exFAT failed.");
1533 disk_io_reset_imghook(&segnum, &data_offset);
1534 goto End;
1535 }
1536
1537 disk_io_reset_imghook(&segnum, &data_offset);
1538 segnum++;
1539
1540 Log("current segment number:%d dataoff:%ld", segnum, (long)data_offset);
1541
1542 //write data
1543 Log("Writing part2 data ............................. ");
1544 fwrite(pData + SIZE_1MB, 1, VENTOY_EFI_PART_SIZE, fp);
1545 pSegment[segnum].disk_start_sector = Part2StartSector;
1546 pSegment[segnum].sector_num = VENTOY_EFI_PART_SIZE / 512;
1547 pSegment[segnum].data_offset = data_offset;
1548 data_offset += pSegment[segnum].sector_num * 512;
1549 segnum++;
1550
1551 if (PartStyle)
1552 {
1553 Log("Writing backup gpt table ............................. ");
1554 fwrite(pBkGptPartTbl, 1, 33 * 512, fp);
1555 pSegment[segnum].disk_start_sector = pPhyDrive->SizeInBytes / 512 - 33;
1556 pSegment[segnum].sector_num = 33;
1557 pSegment[segnum].data_offset = data_offset;
1558 data_offset += pSegment[segnum].sector_num * 512;
1559 segnum++;
1560 }
1561
1562 Log("Writing segment metadata ............................. ");
1563
1564 for (i = 0; i < (int)segnum; i++)
1565 {
1566 Log("SEG[%d]: PhySector:%llu SectorNum:%llu DataOffset:%llu(sector:%llu)", i, pSegment[i].disk_start_sector, pSegment[i].sector_num,
1567 pSegment[i].data_offset, pSegment[i].data_offset / 512);
1568 }
1569
1570 dataLen = segnum * sizeof(VTSI_SEGMENT);
1571 fwrite(pSegment, 1, dataLen, fp);
1572
1573 if (dataLen % 512)
1574 {
1575 //pData + SIZE_1MB - 8192 is a temp data buffer with zero
1576 fwrite(pData + SIZE_1MB - 8192, 1, 512 - (dataLen % 512), fp);
1577 }
1578
1579 //Fill footer
1580 pImgFooter->magic = VTSI_IMG_MAGIC;
1581 pImgFooter->version = 1;
1582 pImgFooter->disk_size = pPhyDrive->SizeInBytes;
1583 memcpy(&pImgFooter->disk_signature, pPhyDrive->MBR.BootCode + 0x1b8, 4);
1584 pImgFooter->segment_num = segnum;
1585 pImgFooter->segment_offset = data_offset;
1586
1587 for (i = 0, chksum = 0; i < (int)(segnum * sizeof(VTSI_SEGMENT)); i++)
1588 {
1589 chksum += *((UINT8 *)pSegment + i);
1590 }
1591 pImgFooter->segment_chksum = ~chksum;
1592
1593 for (i = 0, chksum = 0; i < sizeof(VTSI_FOOTER); i++)
1594 {
1595 chksum += *((UINT8 *)pImgFooter + i);
1596 }
1597 pImgFooter->foot_chksum = ~chksum;
1598
1599 Log("Writing footer segnum(%u) segoffset(%llu) ......................", segnum, data_offset);
1600 Log("disk_size=%llu disk_signature=%lx segment_offset=%llu", pImgFooter->disk_size, pImgFooter->disk_signature, pImgFooter->segment_offset);
1601
1602 fwrite(pImgFooter, 1, sizeof(VTSI_FOOTER), fp);
1603 fclose(fp);
1604
1605 Log("Writing Ventoy image file finished, the file size should be %llu .", data_offset + 512 + ((dataLen + 511) / 512 * 512));
1606
1607 rc = 0;
1608
1609 End:
1610
1611 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1612 PROGRESS_BAR_SET_POS(PT_REFORMAT_FINISH);
1613
1614 Log("retcode:%d\n", rc);
1615
1616 SAFE_FREE(pData);
1617 SAFE_FREE(ImgBuf);
1618
1619 return rc;
1620 }
1621
1622
1623 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
1624 {
1625 int i;
1626 int rc = 0;
1627 int state = 0;
1628 HANDLE hDrive;
1629 DWORD dwSize;
1630 BOOL bRet;
1631 CHAR MountDrive;
1632 CHAR DriveName[] = "?:\\";
1633 CHAR DriveLetters[MAX_PATH] = { 0 };
1634 MBR_HEAD MBR;
1635 VTOY_GPT_INFO *pGptInfo = NULL;
1636 UINT64 Part1StartSector = 0;
1637 UINT64 Part1SectorCount = 0;
1638 UINT64 Part2StartSector = 0;
1639
1640 Log("#####################################################");
1641 Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
1642 PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1643 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1644 Log("#####################################################");
1645
1646 if (PartStyle)
1647 {
1648 pGptInfo = malloc(sizeof(VTOY_GPT_INFO));
1649 memset(pGptInfo, 0, sizeof(VTOY_GPT_INFO));
1650 }
1651
1652 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1653
1654 if (PartStyle)
1655 {
1656 VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);
1657 Part1StartSector = pGptInfo->PartTbl[0].StartLBA;
1658 Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;
1659 Part2StartSector = pGptInfo->PartTbl[1].StartLBA;
1660 }
1661 else
1662 {
1663 VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);
1664 Part1StartSector = MBR.PartTbl[0].StartSectorId;
1665 Part1SectorCount = MBR.PartTbl[0].SectorCount;
1666 Part2StartSector = MBR.PartTbl[1].StartSectorId;
1667 }
1668
1669 Log("Lock disk for clean ............................. ");
1670
1671 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1672 if (hDrive == INVALID_HANDLE_VALUE)
1673 {
1674 Log("Failed to open physical disk");
1675 free(pGptInfo);
1676 return 1;
1677 }
1678
1679 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1680
1681 if (DriveLetters[0] == 0)
1682 {
1683 Log("No drive letter was assigned...");
1684 DriveName[0] = GetFirstUnusedDriveLetter();
1685 Log("GetFirstUnusedDriveLetter %C: ...", DriveName[0]);
1686 }
1687 else
1688 {
1689 // Unmount all mounted volumes that belong to this drive
1690 // Do it in reverse so that we always end on the first volume letter
1691 for (i = (int)strlen(DriveLetters); i > 0; i--)
1692 {
1693 DriveName[0] = DriveLetters[i - 1];
1694 bRet = DeleteVolumeMountPointA(DriveName);
1695 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, GetLastError());
1696 }
1697 }
1698
1699 MountDrive = DriveName[0];
1700 Log("Will use '%C:' as volume mountpoint", DriveName[0]);
1701
1702 // It kind of blows, but we have to relinquish access to the physical drive
1703 // for VDS to be able to delete the partitions that reside on it...
1704 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1705 CHECK_CLOSE_HANDLE(hDrive);
1706
1707 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
1708
1709 if (!VDS_DeleteAllPartitions(pPhyDrive->PhyDrive))
1710 {
1711 Log("Notice: Could not delete partitions: 0x%x, but we continue.", GetLastError());
1712 }
1713
1714 Log("Deleting all partitions ......................... OK");
1715
1716 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1717
1718 Log("Lock disk for write ............................. ");
1719 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1720 if (hDrive == INVALID_HANDLE_VALUE)
1721 {
1722 Log("Failed to GetPhysicalHandle for write.");
1723 rc = 1;
1724 goto End;
1725 }
1726
1727 //Refresh Drive Layout
1728 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1729
1730 disk_io_set_param(hDrive, Part1StartSector + Part1SectorCount);// include the 2048 sector gap
1731
1732 PROGRESS_BAR_SET_POS(PT_FORMAT_PART1);
1733
1734 if (PartStyle == 1 && pPhyDrive->PartStyle == 0)
1735 {
1736 Log("Wait for format part1 ...");
1737 Sleep(1000 * 5);
1738 }
1739
1740 Log("Formatting part1 exFAT ...");
1741 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
1742 {
1743 Log("FormatPart1exFAT failed.");
1744 rc = 1;
1745 goto End;
1746 }
1747
1748 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
1749 Log("Writing part2 FAT img ...");
1750
1751 if (0 != FormatPart2Fat(hDrive, Part2StartSector))
1752 {
1753 Log("FormatPart2Fat failed.");
1754 rc = 1;
1755 goto End;
1756 }
1757
1758 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
1759 Log("Writing Boot Image ............................. ");
1760 if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)
1761 {
1762 Log("WriteGrubStage1ToPhyDrive failed.");
1763 rc = 1;
1764 goto End;
1765 }
1766
1767 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
1768 Log("Writing Partition Table ........................ ");
1769 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
1770
1771 if (PartStyle)
1772 {
1773 VTOY_GPT_HDR BackupHead;
1774 LARGE_INTEGER liCurrentPosition;
1775
1776 SET_FILE_POS(pPhyDrive->SizeInBytes - 512);
1777 VentoyFillBackupGptHead(pGptInfo, &BackupHead);
1778 if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))
1779 {
1780 rc = 1;
1781 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1782 goto End;
1783 }
1784
1785 SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);
1786 if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))
1787 {
1788 rc = 1;
1789 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1790 goto End;
1791 }
1792
1793 SET_FILE_POS(0);
1794 if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))
1795 {
1796 rc = 1;
1797 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1798 goto End;
1799 }
1800
1801 Log("Write GPT Info OK ...");
1802 memcpy(&(pPhyDrive->MBR), &(pGptInfo->MBR), 512);
1803 }
1804 else
1805 {
1806 if (!WriteFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL))
1807 {
1808 rc = 1;
1809 Log("Write MBR Failed, dwSize:%u ErrCode:%u", dwSize, GetLastError());
1810 goto End;
1811 }
1812 Log("Write MBR OK ...");
1813 memcpy(&(pPhyDrive->MBR), &MBR, 512);
1814 }
1815
1816 //Refresh Drive Layout
1817 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1818
1819 End:
1820
1821 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1822
1823 if (rc == 0)
1824 {
1825 Log("Mounting Ventoy Partition ....................... ");
1826 Sleep(1000);
1827
1828 state = 0;
1829 memset(DriveLetters, 0, sizeof(DriveLetters));
1830 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1831 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1832
1833 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1834 {
1835 DriveName[0] = DriveLetters[i];
1836 if (IsVentoyLogicalDrive(DriveName[0]))
1837 {
1838 Log("%s is ventoy part2, delete mountpoint", DriveName);
1839 DeleteVolumeMountPointA(DriveName);
1840 }
1841 else
1842 {
1843 Log("%s is ventoy part1, already mounted", DriveName);
1844 MountDrive = DriveName[0];
1845 state = 1;
1846 }
1847 }
1848
1849 if (state != 1)
1850 {
1851 Log("need to mount ventoy part1...");
1852
1853 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, Part1StartSector, DriveLetters, sizeof(DriveLetters), FALSE))
1854 {
1855 DriveName[0] = MountDrive;
1856 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1857 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1858
1859 if (bRet)
1860 {
1861 state = 1;
1862 }
1863 }
1864 else
1865 {
1866 Log("Failed to find ventoy volume");
1867 }
1868 }
1869
1870 if (GetVentoyFsType() > 0)
1871 {
1872 if (state)
1873 {
1874 Log("Reformat %C:\\ to %s", MountDrive, GetVentoyFsName());
1875 DISK_FormatVolume(MountDrive, GetVentoyFsType());
1876 }
1877 else
1878 {
1879 Log("Can not reformat %s to %s", DriveName, GetVentoyFsName());
1880 }
1881 }
1882 else
1883 {
1884 Log("No need to reformat ventoy partition");
1885 }
1886
1887 Log("OK\n");
1888 }
1889 else
1890 {
1891 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1892
1893 FindProcessOccupyDisk(hDrive, pPhyDrive);
1894
1895 if (!VDS_IsLastAvaliable())
1896 {
1897 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1898 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1899 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1900 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1901 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1902 }
1903 }
1904
1905 if (pGptInfo)
1906 {
1907 free(pGptInfo);
1908 }
1909
1910 CHECK_CLOSE_HANDLE(hDrive);
1911 return rc;
1912 }
1913
1914
1915 int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive)
1916 {
1917 int i, j;
1918 int rc = 1;
1919 int PhyDrive;
1920 int PartStyle;
1921 INT64 ReservedValue;
1922 UINT64 RecudeBytes;
1923 GUID Guid;
1924 MBR_HEAD MBR;
1925 VTOY_GPT_INFO *pGPT;
1926 MBR_HEAD *pMBR;
1927 DWORD dwSize = 0;
1928 VTOY_GPT_HDR BackupHead;
1929 HANDLE hDrive = INVALID_HANDLE_VALUE;
1930 GUID ZeroGuid = { 0 };
1931 static GUID WindowsDataPartType = { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
1932 static GUID EspPartType = { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
1933 static GUID BiosGrubPartType = { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
1934
1935 Log("#####################################################");
1936 Log("PartitionResizeForVentoy PhyDrive%d <<%s %s %dGB>>",
1937 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1938 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1939 Log("#####################################################");
1940
1941 pGPT = &(pPhyDrive->Gpt);
1942 pMBR = &(pPhyDrive->Gpt.MBR);
1943 Log("Disksize:%llu Part2Start:%llu", pPhyDrive->SizeInBytes, pPhyDrive->ResizePart2StartSector * 512);
1944
1945 if (pMBR->PartTbl[0].FsFlag == 0xEE && memcmp(pGPT->Head.Signature, "EFI PART", 8) == 0)
1946 {
1947 PartStyle = 1;
1948 }
1949 else
1950 {
1951 PartStyle = 0;
1952 }
1953
1954 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1955
1956 RecudeBytes = VENTOY_EFI_PART_SIZE;
1957 ReservedValue = GetReservedSpaceInMB();
1958 if (ReservedValue > 0)
1959 {
1960 Log("Reduce add reserved space %lldMB", (LONGLONG)ReservedValue);
1961 RecudeBytes += (UINT64)(ReservedValue * SIZE_1MB);
1962 }
1963
1964
1965 if (pPhyDrive->ResizeNoShrink == FALSE)
1966 {
1967 Log("Need to shrink the volume");
1968 if (DISK_ShrinkVolume(pPhyDrive->PhyDrive, pPhyDrive->ResizeVolumeGuid, pPhyDrive->Part1DriveLetter, pPhyDrive->ResizeOldPart1Size, RecudeBytes))
1969 {
1970 Log("Shrink volume success, now check again");
1971
1972 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1973 if (hDrive == INVALID_HANDLE_VALUE)
1974 {
1975 Log("Failed to GetPhysicalHandle for update.");
1976 goto End;
1977 }
1978
1979 //Refresh Drive Layout
1980 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1981
1982 CHECK_CLOSE_HANDLE(hDrive);
1983
1984
1985 if (PartResizePreCheck(NULL) && pPhyDrive->ResizeNoShrink)
1986 {
1987 Log("Recheck after Shrink volume success");
1988 Log("After shrink Disksize:%llu Part2Start:%llu", pPhyDrive->SizeInBytes, pPhyDrive->ResizePart2StartSector * 512);
1989 }
1990 else
1991 {
1992 Log("Recheck after Shrink volume failed %u", pPhyDrive->ResizeNoShrink);
1993 goto End;
1994 }
1995 }
1996 else
1997 {
1998 Log("Shrink volume failed");
1999 goto End;
2000 }
2001 }
2002
2003
2004 //Now try write data
2005 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2006 if (hDrive == INVALID_HANDLE_VALUE)
2007 {
2008 Log("Failed to GetPhysicalHandle for update.");
2009 goto End;
2010 }
2011
2012
2013 //Write partition 2 data
2014 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
2015 if (0 != FormatPart2Fat(hDrive, pPhyDrive->ResizePart2StartSector))
2016 {
2017 Log("FormatPart2Fat failed.");
2018 goto End;
2019 }
2020
2021 //Write grub stage2 gap
2022 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
2023 Log("Writing Boot Image ............................. ");
2024 if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)
2025 {
2026 Log("WriteGrubStage1ToPhyDrive failed.");
2027 goto End;
2028 }
2029
2030
2031 //Write partition table
2032 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
2033 Log("Writing partition table ............................. ");
2034
2035 VentoyGetLocalBootImg(&MBR);
2036 CoCreateGuid(&Guid);
2037 memcpy(MBR.BootCode + 0x180, &Guid, 16);
2038 memcpy(pMBR->BootCode, MBR.BootCode, 440);
2039
2040 if (PartStyle == 0)
2041 {
2042 for (i = 1; i < 4; i++)
2043 {
2044 if (pMBR->PartTbl[i].SectorCount == 0)
2045 {
2046 break;
2047 }
2048 }
2049
2050 if (i >= 4)
2051 {
2052 Log("Can not find MBR free partition table");
2053 goto End;
2054 }
2055
2056 for (j = i - 1; j > 0; j--)
2057 {
2058 Log("Move MBR partition table %d --> %d", j + 1, j + 2);
2059 memcpy(pMBR->PartTbl + (j + 1), pMBR->PartTbl + j, sizeof(PART_TABLE));
2060 }
2061
2062 memset(pMBR->PartTbl + 1, 0, sizeof(PART_TABLE));
2063 VentoyFillMBRLocation(pPhyDrive->SizeInBytes, (UINT32)pPhyDrive->ResizePart2StartSector, VENTOY_EFI_PART_SIZE / 512, pMBR->PartTbl + 1);
2064 pMBR->PartTbl[0].Active = 0x80; // bootable
2065 pMBR->PartTbl[1].Active = 0x00;
2066 pMBR->PartTbl[1].FsFlag = 0xEF; // EFI System Partition
2067
2068 if (!WriteDataToPhyDisk(hDrive, 0, pMBR, 512))
2069 {
2070 Log("Legacy BIOS write MBR failed");
2071 goto End;
2072 }
2073 }
2074 else
2075 {
2076 for (i = 1; i < 128; i++)
2077 {
2078 if (memcmp(&(pGPT->PartTbl[i].PartGuid), &ZeroGuid, sizeof(GUID)) == 0)
2079 {
2080 break;
2081 }
2082 }
2083
2084 if (i >= 128)
2085 {
2086 Log("Can not find GPT free partition table");
2087 goto End;
2088 }
2089
2090 for (j = i - 1; j > 0; j--)
2091 {
2092 Log("Move GPT partition table %d --> %d", j + 1, j + 2);
2093 memcpy(pGPT->PartTbl + (j + 1), pGPT->PartTbl + j, sizeof(VTOY_GPT_PART_TBL));
2094 }
2095
2096
2097 pMBR->BootCode[92] = 0x22;
2098
2099 // to fix windows issue
2100 memset(pGPT->PartTbl + 1, 0, sizeof(VTOY_GPT_PART_TBL));
2101 memcpy(&(pGPT->PartTbl[1].PartType), &WindowsDataPartType, sizeof(GUID));
2102 CoCreateGuid(&(pGPT->PartTbl[1].PartGuid));
2103
2104 pGPT->PartTbl[1].StartLBA = pGPT->PartTbl[0].LastLBA + 1;
2105 pGPT->PartTbl[1].LastLBA = pGPT->PartTbl[1].StartLBA + VENTOY_EFI_PART_SIZE / 512 - 1;
2106 pGPT->PartTbl[1].Attr = 0xC000000000000001ULL;
2107 memcpy(pGPT->PartTbl[1].Name, L"VTOYEFI", 7 * 2);
2108
2109 //Update CRC
2110 pGPT->Head.PartTblCrc = VentoyCrc32(pGPT->PartTbl, sizeof(pGPT->PartTbl));
2111 pGPT->Head.Crc = 0;
2112 pGPT->Head.Crc = VentoyCrc32(&(pGPT->Head), pGPT->Head.Length);
2113
2114 Log("pGPT->Head.EfiStartLBA=%llu", (ULONGLONG)pGPT->Head.EfiStartLBA);
2115 Log("pGPT->Head.EfiBackupLBA=%llu", (ULONGLONG)pGPT->Head.EfiBackupLBA);
2116
2117 VentoyFillBackupGptHead(pGPT, &BackupHead);
2118 if (!WriteDataToPhyDisk(hDrive, pGPT->Head.EfiBackupLBA * 512, &BackupHead, 512))
2119 {
2120 Log("UEFI write backup head failed");
2121 goto End;
2122 }
2123
2124 if (!WriteDataToPhyDisk(hDrive, (pGPT->Head.EfiBackupLBA - 32) * 512, pGPT->PartTbl, 512 * 32))
2125 {
2126 Log("UEFI write backup partition table failed");
2127 goto End;
2128 }
2129
2130 if (!WriteDataToPhyDisk(hDrive, 0, pGPT, 512 * 34))
2131 {
2132 Log("UEFI write MBR & Main partition table failed");
2133 goto End;
2134 }
2135 }
2136
2137
2138
2139 //Refresh Drive Layout
2140 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2141
2142 //We must close handle here, because it will block the refresh bellow
2143 CHECK_CLOSE_HANDLE(hDrive);
2144
2145 Sleep(2000);
2146
2147 //Refresh disk list
2148 PhyDrive = pPhyDrive->PhyDrive;
2149
2150 Log("#### Now Refresh PhyDrive ####");
2151 Ventoy2DiskDestroy();
2152 Ventoy2DiskInit();
2153
2154 pPhyDrive = GetPhyDriveInfoByPhyDrive(PhyDrive);
2155 if (pPhyDrive)
2156 {
2157 if (pPhyDrive->VentoyVersion[0] == 0)
2158 {
2159 Log("After process the Ventoy version is still invalid");
2160 goto End;
2161 }
2162
2163 Log("### Ventoy non-destructive installation successfully finished <%s>", pPhyDrive->VentoyVersion);
2164 }
2165 else
2166 {
2167 Log("### Ventoy non-destructive installation successfully finished <not found>");
2168 }
2169
2170 InitComboxCtrl(g_DialogHwnd, PhyDrive);
2171
2172 rc = 0;
2173
2174 End:
2175 CHECK_CLOSE_HANDLE(hDrive);
2176 return rc;
2177 }
2178
2179
2180 static BOOL DiskCheckWriteAccess(HANDLE hDrive)
2181 {
2182 DWORD dwSize;
2183 BOOL ret = FALSE;
2184 BOOL bRet = FALSE;
2185 BYTE Buffer[512];
2186 LARGE_INTEGER liCurPosition;
2187 LARGE_INTEGER liNewPosition;
2188
2189 liCurPosition.QuadPart = 2039 * 512;
2190 liNewPosition.QuadPart = 0;
2191 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2192 liNewPosition.QuadPart != liCurPosition.QuadPart)
2193 {
2194 Log("SetFilePointer1 Failed %u", LASTERR);
2195 goto out;
2196 }
2197
2198
2199 dwSize = 0;
2200 ret = ReadFile(hDrive, Buffer, 512, &dwSize, NULL);
2201 if ((!ret) || (dwSize != 512))
2202 {
2203 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2204 goto out;
2205 }
2206
2207
2208 liCurPosition.QuadPart = 2039 * 512;
2209 liNewPosition.QuadPart = 0;
2210 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2211 liNewPosition.QuadPart != liCurPosition.QuadPart)
2212 {
2213 Log("SetFilePointer2 Failed %u", LASTERR);
2214 goto out;
2215 }
2216
2217 dwSize = 0;
2218 ret = WriteFile(hDrive, Buffer, 512, &dwSize, NULL);
2219 if ((!ret) || dwSize != 512)
2220 {
2221 Log("Failed to write %d %u %u", ret, dwSize, LASTERR);
2222 goto out;
2223 }
2224
2225 bRet = TRUE;
2226
2227 out:
2228
2229 return bRet;
2230 }
2231
2232 static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBackup)
2233 {
2234 DWORD dwSize;
2235 DWORD dwStatus;
2236 BOOL Return = FALSE;
2237 BOOL ret = FALSE;
2238 BYTE *backup = NULL;
2239 UINT64 offset;
2240 HANDLE hDrive = INVALID_HANDLE_VALUE;
2241 LARGE_INTEGER liCurPosition;
2242 LARGE_INTEGER liNewPosition;
2243 VTOY_GPT_INFO *pGPT = NULL;
2244
2245 Log("BackupDataBeforeCleanDisk %d", PhyDrive);
2246
2247 // step1: check write access
2248 hDrive = GetPhysicalHandle(PhyDrive, TRUE, TRUE, FALSE);
2249 if (hDrive == INVALID_HANDLE_VALUE)
2250 {
2251 Log("Failed to GetPhysicalHandle for write.");
2252 goto out;
2253 }
2254
2255 if (DiskCheckWriteAccess(hDrive))
2256 {
2257 Log("DiskCheckWriteAccess success");
2258 CHECK_CLOSE_HANDLE(hDrive);
2259 }
2260 else
2261 {
2262 Log("DiskCheckWriteAccess failed");
2263 goto out;
2264 }
2265
2266 //step2 backup 4MB data
2267 backup = malloc(SIZE_1MB * 4);
2268 if (!backup)
2269 {
2270 goto out;
2271 }
2272
2273 hDrive = GetPhysicalHandle(PhyDrive, FALSE, FALSE, FALSE);
2274 if (hDrive == INVALID_HANDLE_VALUE)
2275 {
2276 goto out;
2277 }
2278
2279 //read first 2MB
2280 dwStatus = SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2281 if (dwStatus != 0)
2282 {
2283 goto out;
2284 }
2285
2286 dwSize = 0;
2287 ret = ReadFile(hDrive, backup, SIZE_2MB, &dwSize, NULL);
2288 if ((!ret) || (dwSize != SIZE_2MB))
2289 {
2290 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2291 goto out;
2292 }
2293
2294 pGPT = (VTOY_GPT_INFO *)backup;
2295 offset = pGPT->Head.EfiBackupLBA * 512;
2296 if (offset >= (DiskSize - SIZE_2MB) && offset < DiskSize)
2297 {
2298 Log("EFI partition table check success");
2299 }
2300 else
2301 {
2302 Log("Backup EFI LBA not in last 2MB range: %llu", pGPT->Head.EfiBackupLBA);
2303 goto out;
2304 }
2305
2306 //read last 2MB
2307 liCurPosition.QuadPart = DiskSize - SIZE_2MB;
2308 liNewPosition.QuadPart = 0;
2309 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2310 liNewPosition.QuadPart != liCurPosition.QuadPart)
2311 {
2312 goto out;
2313 }
2314
2315 dwSize = 0;
2316 ret = ReadFile(hDrive, backup + SIZE_2MB, SIZE_2MB, &dwSize, NULL);
2317 if ((!ret) || (dwSize != SIZE_2MB))
2318 {
2319 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2320 goto out;
2321 }
2322
2323 *pBackup = backup;
2324 backup = NULL; //For don't free later
2325 Return = TRUE;
2326
2327 out:
2328 CHECK_CLOSE_HANDLE(hDrive);
2329 if (backup)
2330 free(backup);
2331
2332 return Return;
2333 }
2334
2335
2336 static BOOL WriteBackupDataToDisk(HANDLE hDrive, UINT64 Offset, BYTE *Data, DWORD Length)
2337 {
2338 DWORD dwSize = 0;
2339 BOOL ret = FALSE;
2340 LARGE_INTEGER liCurPosition;
2341 LARGE_INTEGER liNewPosition;
2342
2343 Log("WriteBackupDataToDisk %llu %p %u", Offset, Data, Length);
2344
2345 liCurPosition.QuadPart = Offset;
2346 liNewPosition.QuadPart = 0;
2347 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2348 liNewPosition.QuadPart != liCurPosition.QuadPart)
2349 {
2350 return FALSE;
2351 }
2352
2353 ret = WriteFile(hDrive, Data, Length, &dwSize, NULL);
2354 if ((!ret) || dwSize != Length)
2355 {
2356 Log("Failed to write %d %u %u", ret, dwSize, LASTERR);
2357 return FALSE;
2358 }
2359
2360 Log("WriteBackupDataToDisk %llu %p %u success", Offset, Data, Length);
2361 return TRUE;
2362 }
2363
2364
2365 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
2366 {
2367 int i;
2368 int rc = 0;
2369 int MaxRetry = 4;
2370 BOOL ForceMBR = FALSE;
2371 BOOL Esp2Basic = FALSE;
2372 BOOL ChangeAttr = FALSE;
2373 BOOL CleanDisk = FALSE;
2374 BOOL DelEFI = FALSE;
2375 BOOL bWriteBack = TRUE;
2376 HANDLE hVolume;
2377 HANDLE hDrive;
2378 DWORD Status;
2379 DWORD dwSize;
2380 BOOL bRet;
2381 CHAR DriveName[] = "?:\\";
2382 CHAR DriveLetters[MAX_PATH] = { 0 };
2383 CHAR BackBinFile[MAX_PATH];
2384 UINT64 StartSector;
2385 UINT64 ReservedMB = 0;
2386 MBR_HEAD BootImg;
2387 MBR_HEAD MBR;
2388 BYTE *pBackup = NULL;
2389 VTOY_GPT_INFO *pGptInfo = NULL;
2390 VTOY_GPT_INFO *pGptBkup = NULL;
2391 UINT8 ReservedData[4096];
2392
2393 Log("#####################################################");
2394 Log("UpdateVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
2395 pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
2396 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
2397 Log("#####################################################");
2398
2399 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2400
2401 Log("Lock disk for umount ............................ ");
2402
2403 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
2404 if (hDrive == INVALID_HANDLE_VALUE)
2405 {
2406 Log("Failed to open physical disk");
2407 return 1;
2408 }
2409
2410 if (pPhyDrive->PartStyle)
2411 {
2412 pGptInfo = malloc(2 * sizeof(VTOY_GPT_INFO));
2413 if (!pGptInfo)
2414 {
2415 return 1;
2416 }
2417
2418 memset(pGptInfo, 0, 2 * sizeof(VTOY_GPT_INFO));
2419 pGptBkup = pGptInfo + 1;
2420
2421 // Read GPT Info
2422 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2423 ReadFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL);
2424 memcpy(pGptBkup, pGptInfo, sizeof(VTOY_GPT_INFO));
2425
2426 //MBR will be used to compare with local boot image
2427 memcpy(&MBR, &pGptInfo->MBR, sizeof(MBR_HEAD));
2428
2429 StartSector = pGptInfo->PartTbl[1].StartLBA;
2430 Log("GPT StartSector in PartTbl:%llu", (ULONGLONG)StartSector);
2431
2432 ReservedMB = (pPhyDrive->SizeInBytes / 512 - (StartSector + VENTOY_EFI_PART_SIZE / 512) - 33) / 2048;
2433 Log("GPT Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);
2434 }
2435 else
2436 {
2437 // Read MBR
2438 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2439 ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
2440
2441 StartSector = MBR.PartTbl[1].StartSectorId;
2442 Log("MBR StartSector in PartTbl:%llu", (ULONGLONG)StartSector);
2443
2444 ReservedMB = (pPhyDrive->SizeInBytes / 512 - (StartSector + VENTOY_EFI_PART_SIZE / 512)) / 2048;
2445 Log("MBR Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);
2446 }
2447
2448 //Read Reserved Data
2449 SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);
2450 ReadFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);
2451
2452 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
2453
2454 if (DriveLetters[0] == 0)
2455 {
2456 Log("No drive letter was assigned...");
2457 }
2458 else
2459 {
2460 // Unmount all mounted volumes that belong to this drive
2461 // Do it in reverse so that we always end on the first volume letter
2462 for (i = (int)strlen(DriveLetters); i > 0; i--)
2463 {
2464 DriveName[0] = DriveLetters[i - 1];
2465 if (IsVentoyLogicalDrive(DriveName[0]))
2466 {
2467 Log("%s is ventoy logical drive", DriveName);
2468 bRet = DeleteVolumeMountPointA(DriveName);
2469 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, LASTERR);
2470 break;
2471 }
2472 }
2473 }
2474
2475 // It kind of blows, but we have to relinquish access to the physical drive
2476 // for VDS to be able to delete the partitions that reside on it...
2477 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2478 CHECK_CLOSE_HANDLE(hDrive);
2479
2480 if (pPhyDrive->PartStyle == 1)
2481 {
2482 Log("TryId=%d EFI GPT partition type is 0x%llx", TryId, pPhyDrive->Part2GPTAttr);
2483 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
2484
2485 if (TryId == 1)
2486 {
2487 Log("Change GPT partition type to ESP");
2488 if (DISK_ChangeVtoyEFI2ESP(pPhyDrive->PhyDrive, StartSector * 512ULL))
2489 {
2490 Esp2Basic = TRUE;
2491 Sleep(3000);
2492 }
2493 }
2494 else if (TryId == 2)
2495 {
2496 Log("Change GPT partition attribute");
2497 if (DISK_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, StartSector * 512ULL, 0x8000000000000001))
2498 {
2499 ChangeAttr = TRUE;
2500 Sleep(2000);
2501 }
2502 }
2503 else if (TryId == 3)
2504 {
2505 DISK_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive, StartSector * 512ULL);
2506 DelEFI = TRUE;
2507 }
2508 else if (TryId == 4)
2509 {
2510 Log("Clean disk GPT partition table");
2511 if (BackupDataBeforeCleanDisk(pPhyDrive->PhyDrive, pPhyDrive->SizeInBytes, &pBackup))
2512 {
2513 sprintf_s(BackBinFile, sizeof(BackBinFile), ".\\ventoy\\phydrive%d_%u_%d.bin",
2514 pPhyDrive->PhyDrive, GetCurrentProcessId(), g_backup_bin_index++);
2515 SaveBufToFile(BackBinFile, pBackup, 4 * SIZE_1MB);
2516 Log("Save backup data to %s", BackBinFile);
2517
2518 Log("Success to backup data before clean");
2519 CleanDisk = TRUE;
2520 DISK_CleanDisk(pPhyDrive->PhyDrive);
2521 Sleep(3000);
2522 }
2523 else
2524 {
2525 Log("Failed to backup data before clean");
2526 }
2527 }
2528 }
2529
2530 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
2531
2532 Log("Lock disk for update ............................ ");
2533 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2534 if (hDrive == INVALID_HANDLE_VALUE)
2535 {
2536 Log("Failed to GetPhysicalHandle for write.");
2537 rc = 1;
2538 goto End;
2539 }
2540
2541 PROGRESS_BAR_SET_POS(PT_LOCK_VOLUME);
2542
2543 Log("Lock volume for update .......................... ");
2544 hVolume = INVALID_HANDLE_VALUE;
2545
2546 //If we change VTOYEFI to ESP, it can not have s volume name, so don't try to get it.
2547 if (CleanDisk)
2548 {
2549 //writeback the last 2MB
2550 if (!WriteBackupDataToDisk(hDrive, pPhyDrive->SizeInBytes - SIZE_2MB, pBackup + SIZE_2MB, SIZE_2MB))
2551 {
2552 bWriteBack = FALSE;
2553 }
2554
2555 //write the first 2MB except parttable
2556 if (!WriteBackupDataToDisk(hDrive, 34 * 512, pBackup + 34 * 512, SIZE_2MB - 34 * 512))
2557 {
2558 bWriteBack = FALSE;
2559 }
2560
2561 Status = ERROR_NOT_FOUND;
2562 }
2563 else if (DelEFI)
2564 {
2565 Status = ERROR_NOT_FOUND;
2566 }
2567 else if (Esp2Basic)
2568 {
2569 Status = ERROR_NOT_FOUND;
2570 }
2571 else
2572 {
2573 for (i = 0; i < MaxRetry; i++)
2574 {
2575 Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, StartSector, DriveLetters, sizeof(DriveLetters), TRUE);
2576 if (ERROR_SUCCESS == Status)
2577 {
2578 break;
2579 }
2580 else
2581 {
2582 Log("==== Volume not found, wait and retry %d... ====", i);
2583 Sleep(2);
2584 }
2585 }
2586 }
2587
2588 if (ERROR_SUCCESS == Status)
2589 {
2590 Log("Now lock and dismount volume <%s>", DriveLetters);
2591
2592 for (i = 0; i < MaxRetry; i++)
2593 {
2594 hVolume = CreateFileA(DriveLetters,
2595 GENERIC_READ | GENERIC_WRITE,
2596 FILE_SHARE_READ,
2597 NULL,
2598 OPEN_EXISTING,
2599 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
2600 NULL);
2601
2602 if (hVolume == INVALID_HANDLE_VALUE)
2603 {
2604 Log("Failed to create file volume, errcode:%u, wait and retry ...", LASTERR);
2605 Sleep(2000);
2606 }
2607 else
2608 {
2609 break;
2610 }
2611 }
2612
2613 if (hVolume == INVALID_HANDLE_VALUE)
2614 {
2615 Log("Failed to create file volume, errcode:%u", LASTERR);
2616 }
2617 else
2618 {
2619 bRet = DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2620 Log("FSCTL_LOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2621
2622 bRet = DeviceIoControl(hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2623 Log("FSCTL_DISMOUNT_VOLUME bRet:%u code:%u", bRet, LASTERR);
2624 }
2625 }
2626 else if (ERROR_NOT_FOUND == Status)
2627 {
2628 Log("Volume not found, maybe not supported");
2629 }
2630 else
2631 {
2632 rc = 1;
2633 goto End;
2634 }
2635
2636 bRet = TryWritePart2(hDrive, StartSector);
2637 if (FALSE == bRet && Esp2Basic)
2638 {
2639 Log("TryWritePart2 agagin ...");
2640 Sleep(3000);
2641 bRet = TryWritePart2(hDrive, StartSector);
2642 }
2643
2644 if (!bRet)
2645 {
2646 if (pPhyDrive->PartStyle == 0)
2647 {
2648 if (DiskCheckWriteAccess(hDrive))
2649 {
2650 Log("MBR DiskCheckWriteAccess success");
2651
2652 ForceMBR = TRUE;
2653
2654 Log("Try write failed, now delete partition 2 for MBR...");
2655 CHECK_CLOSE_HANDLE(hDrive);
2656
2657 Log("Now delete partition 2...");
2658 DISK_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive, StartSector * 512ULL);
2659
2660 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2661 if (hDrive == INVALID_HANDLE_VALUE)
2662 {
2663 Log("Failed to GetPhysicalHandle for write.");
2664 rc = 1;
2665 goto End;
2666 }
2667 }
2668 else
2669 {
2670 Log("MBR DiskCheckWriteAccess failed");
2671 }
2672 }
2673 else
2674 {
2675 Log("TryWritePart2 failed ....");
2676 rc = 1;
2677 goto End;
2678 }
2679 }
2680
2681 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
2682
2683 Log("Write Ventoy to disk ............................ ");
2684 if (0 != FormatPart2Fat(hDrive, StartSector))
2685 {
2686 rc = 1;
2687 goto End;
2688 }
2689
2690 if (hVolume != INVALID_HANDLE_VALUE)
2691 {
2692 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2693 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2694 CHECK_CLOSE_HANDLE(hVolume);
2695 }
2696
2697 Log("Updating Boot Image ............................. ");
2698 if (WriteGrubStage1ToPhyDrive(hDrive, pPhyDrive->PartStyle) != 0)
2699 {
2700 rc = 1;
2701 goto End;
2702 }
2703
2704 //write reserved data
2705 SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);
2706 bRet = WriteFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);
2707 Log("Write resv data ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2708
2709 // Boot Image
2710 VentoyGetLocalBootImg(&BootImg);
2711
2712 // Use Old UUID
2713 memcpy(BootImg.BootCode + 0x180, MBR.BootCode + 0x180, 16);
2714 if (pPhyDrive->PartStyle)
2715 {
2716 BootImg.BootCode[92] = 0x22;
2717 }
2718
2719 if (ForceMBR == FALSE && memcmp(BootImg.BootCode, MBR.BootCode, 440) == 0)
2720 {
2721 Log("Boot image has no difference, no need to write.");
2722 }
2723 else
2724 {
2725 Log("Boot image need to write %u.", ForceMBR);
2726
2727 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2728
2729 memcpy(MBR.BootCode, BootImg.BootCode, 440);
2730 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
2731 Log("Write Boot Image ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2732 }
2733
2734 if (pPhyDrive->PartStyle == 0)
2735 {
2736 if (0x00 == MBR.PartTbl[0].Active && 0x80 == MBR.PartTbl[1].Active)
2737 {
2738 Log("Need to chage 1st partition active and 2nd partition inactive.");
2739
2740 MBR.PartTbl[0].Active = 0x80;
2741 MBR.PartTbl[1].Active = 0x00;
2742
2743 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2744 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
2745 Log("Write NEW MBR ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2746 }
2747 }
2748
2749 if (CleanDisk)
2750 {
2751 if (!WriteBackupDataToDisk(hDrive, 0, pBackup, 34 * 512))
2752 {
2753 bWriteBack = FALSE;
2754 }
2755
2756 free(pBackup);
2757
2758 if (bWriteBack)
2759 {
2760 Log("Write backup data success, now delete %s", BackBinFile);
2761 DeleteFileA(BackBinFile);
2762 }
2763 else
2764 {
2765 Log("Write backup data failed");
2766 }
2767
2768 Sleep(1000);
2769 }
2770 else if (DelEFI)
2771 {
2772 VTOY_GPT_HDR BackupHdr;
2773
2774 VentoyFillBackupGptHead(pGptBkup, &BackupHdr);
2775 if (!WriteBackupDataToDisk(hDrive, 512 * pGptBkup->Head.EfiBackupLBA, (BYTE*)(&BackupHdr), 512))
2776 {
2777 bWriteBack = FALSE;
2778 }
2779
2780 if (!WriteBackupDataToDisk(hDrive, 512 * (pGptBkup->Head.EfiBackupLBA - 32), (BYTE*)(pGptBkup->PartTbl), 32 * 512))
2781 {
2782 bWriteBack = FALSE;
2783 }
2784
2785 if (!WriteBackupDataToDisk(hDrive, 512, (BYTE*)pGptBkup + 512, 33 * 512))
2786 {
2787 bWriteBack = FALSE;
2788 }
2789
2790 if (bWriteBack)
2791 {
2792 Log("Write backup partition table success");
2793 }
2794 else
2795 {
2796 Log("Write backup partition table failed");
2797 }
2798
2799 Sleep(1000);
2800 }
2801
2802 //Refresh Drive Layout
2803 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2804
2805 End:
2806
2807 if (hVolume != INVALID_HANDLE_VALUE)
2808 {
2809 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2810 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2811 CHECK_CLOSE_HANDLE(hVolume);
2812 }
2813
2814 if (rc == 0)
2815 {
2816 Log("OK");
2817 }
2818 else
2819 {
2820 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2821 FindProcessOccupyDisk(hDrive, pPhyDrive);
2822 }
2823
2824 CHECK_CLOSE_HANDLE(hDrive);
2825
2826 if (Esp2Basic)
2827 {
2828 Log("Recover GPT partition type to basic");
2829 DISK_ChangeVtoyEFI2Basic(pPhyDrive->PhyDrive, StartSector * 512);
2830 }
2831
2832 if (pPhyDrive->PartStyle == 1)
2833 {
2834 if (ChangeAttr || ((pPhyDrive->Part2GPTAttr >> 56) != 0xC0))
2835 {
2836 Log("Change EFI partition attr %u <0x%llx> to <0x%llx>", ChangeAttr, pPhyDrive->Part2GPTAttr, 0xC000000000000001ULL);
2837 if (DISK_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, StartSector * 512ULL, 0xC000000000000001ULL))
2838 {
2839 Log("Change EFI partition attr success");
2840 pPhyDrive->Part2GPTAttr = 0xC000000000000001ULL;
2841 }
2842 else
2843 {
2844 Log("Change EFI partition attr failed");
2845 }
2846 }
2847 }
2848
2849 if (pGptInfo)
2850 {
2851 free(pGptInfo);
2852 }
2853
2854 return rc;
2855 }
2856
2857