]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/PhyDrive.c
485d9ff29a8114ff9e51c229dd8614288e785cf0
[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
1341 if (pTmpBuf)
1342 {
1343 free(pTmpBuf);
1344 }
1345
1346 if (rc == 0)
1347 {
1348 Log("Mounting Ventoy Partition ....................... ");
1349 Sleep(1000);
1350
1351 state = 0;
1352 memset(DriveLetters, 0, sizeof(DriveLetters));
1353 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1354 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1355
1356 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1357 {
1358 DriveName[0] = DriveLetters[i];
1359 Log("%s is ventoy part1, already mounted", DriveName);
1360 state = 1;
1361 }
1362
1363 if (state != 1)
1364 {
1365 Log("need to mount ventoy part1...");
1366 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, 2048, DriveLetters, sizeof(DriveLetters), FALSE))
1367 {
1368 DriveName[0] = MountDrive;
1369 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1370 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1371
1372 *pDrvLetter = MountDrive;
1373 }
1374 else
1375 {
1376 Log("Failed to find ventoy volume");
1377 }
1378 }
1379
1380 Log("OK\n");
1381 }
1382 else
1383 {
1384 FindProcessOccupyDisk(hDrive, pPhyDrive);
1385 }
1386
1387 CHECK_CLOSE_HANDLE(hDrive);
1388 return rc;
1389 }
1390
1391 int InstallVentoy2FileImage(PHY_DRIVE_INFO *pPhyDrive, int PartStyle)
1392 {
1393 int i;
1394 int rc = 1;
1395 int Len = 0;
1396 int dataLen = 0;
1397 UINT size = 0;
1398 UINT segnum = 0;
1399 UINT32 chksum = 0;
1400 UINT64 data_offset = 0;
1401 UINT64 Part2StartSector = 0;
1402 UINT64 Part1StartSector = 0;
1403 UINT64 Part1SectorCount = 0;
1404 UINT8 *pData = NULL;
1405 UINT8 *pBkGptPartTbl = NULL;
1406 BYTE *ImgBuf = NULL;
1407 MBR_HEAD *pMBR = NULL;
1408 VTSI_FOOTER *pImgFooter = NULL;
1409 VTSI_SEGMENT *pSegment = NULL;
1410 VTOY_GPT_INFO *pGptInfo = NULL;
1411 VTOY_GPT_HDR *pBkGptHdr = NULL;
1412 FILE *fp = NULL;
1413
1414 Log("InstallVentoy2FileImage %s PhyDrive%d <<%s %s %dGB>>",
1415 PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1416 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1417
1418 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1419
1420 size = SIZE_1MB + VENTOY_EFI_PART_SIZE + 33 * 512 + VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT) + sizeof(VTSI_FOOTER);
1421
1422 pData = (UINT8 *)malloc(size);
1423 if (!pData)
1424 {
1425 Log("malloc image buffer failed %d.", size);
1426 goto End;
1427 }
1428
1429 pImgFooter = (VTSI_FOOTER *)(pData + size - sizeof(VTSI_FOOTER));
1430 pSegment = (VTSI_SEGMENT *)((UINT8 *)pImgFooter - VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT));
1431 memset(pImgFooter, 0, sizeof(VTSI_FOOTER));
1432 memset(pSegment, 0, VTSI_IMG_MAX_SEG * sizeof(VTSI_SEGMENT));
1433
1434 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START);
1435
1436 Log("Writing Boot Image ............................. ");
1437 if (ReadWholeFileToBuf(VENTOY_FILE_STG1_IMG, 0, (void **)&ImgBuf, &Len))
1438 {
1439 Log("Failed to read stage1 img");
1440 goto End;
1441 }
1442
1443 unxz(ImgBuf, Len, NULL, NULL, pData, &dataLen, unxz_error);
1444 SAFE_FREE(ImgBuf);
1445
1446 Log("decompress %s len:%d", VENTOY_FILE_STG1_IMG, dataLen);
1447
1448 if (PartStyle)
1449 {
1450 pData[500] = 35;//update blocklist
1451 memmove(pData + 34 * 512, pData, SIZE_1MB - 512 * 34);
1452 memset(pData, 0, 34 * 512);
1453
1454 pGptInfo = (VTOY_GPT_INFO *)pData;
1455 memset(pGptInfo, 0, sizeof(VTOY_GPT_INFO));
1456 VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);
1457
1458 pBkGptPartTbl = pData + SIZE_1MB + VENTOY_EFI_PART_SIZE;
1459 memset(pBkGptPartTbl, 0, 33 * 512);
1460
1461 memcpy(pBkGptPartTbl, pGptInfo->PartTbl, 32 * 512);
1462 pBkGptHdr = (VTOY_GPT_HDR *)(pBkGptPartTbl + 32 * 512);
1463 VentoyFillBackupGptHead(pGptInfo, pBkGptHdr);
1464
1465 Part1StartSector = pGptInfo->PartTbl[0].StartLBA;
1466 Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;
1467 Part2StartSector = pGptInfo->PartTbl[1].StartLBA;
1468
1469 Log("Write GPT Info OK ...");
1470 }
1471 else
1472 {
1473 memmove(pData + 512, pData, SIZE_1MB - 512);
1474 memset(pData, 0, 512);
1475
1476 pMBR = (MBR_HEAD *)pData;
1477 VentoyFillMBR(pPhyDrive->SizeInBytes, pMBR, PartStyle);
1478 Part1StartSector = pMBR->PartTbl[0].StartSectorId;
1479 Part1SectorCount = pMBR->PartTbl[0].SectorCount;
1480 Part2StartSector = pMBR->PartTbl[1].StartSectorId;
1481
1482 Log("Write MBR OK ...");
1483 }
1484
1485 Log("Writing EFI part Image ............................. ");
1486 rc = ReadWholeFileToBuf(VENTOY_FILE_DISK_IMG, 0, (void **)&ImgBuf, &Len);
1487 if (rc)
1488 {
1489 Log("Failed to read img file %p %u", ImgBuf, Len);
1490 goto End;
1491 }
1492
1493 PROGRESS_BAR_SET_POS(PT_WRITE_VENTOY_START + 28);
1494 memset(g_part_img_buf, 0, sizeof(g_part_img_buf));
1495 unxz(ImgBuf, Len, NULL, NULL, pData + SIZE_1MB, &dataLen, unxz_error);
1496 if (dataLen == Len)
1497 {
1498 Log("decompress finished success");
1499 g_part_img_buf[0] = pData + SIZE_1MB;
1500
1501 VentoyProcSecureBoot(g_SecureBoot);
1502 }
1503 else
1504 {
1505 Log("decompress finished failed");
1506 goto End;
1507 }
1508
1509 fopen_s(&fp, "VentoySparseImg.vtsi", "wb+");
1510 if (!fp)
1511 {
1512 Log("Failed to create Ventoy img file");
1513 goto End;
1514 }
1515
1516 Log("Writing stage1 data ............................. ");
1517
1518 fwrite(pData, 1, SIZE_1MB, fp);
1519
1520 pSegment[0].disk_start_sector = 0;
1521 pSegment[0].sector_num = SIZE_1MB / 512;
1522 pSegment[0].data_offset = data_offset;
1523 data_offset += pSegment[0].sector_num * 512;
1524
1525 disk_io_set_param(INVALID_HANDLE_VALUE, Part1StartSector + Part1SectorCount);// include the 2048 sector gap
1526 disk_io_set_imghook(fp, pSegment + 1, VTSI_IMG_MAX_SEG - 1, data_offset);
1527
1528 Log("Formatting part1 exFAT ...");
1529 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
1530 {
1531 Log("FormatPart1exFAT failed.");
1532 disk_io_reset_imghook(&segnum, &data_offset);
1533 goto End;
1534 }
1535
1536 disk_io_reset_imghook(&segnum, &data_offset);
1537 segnum++;
1538
1539 Log("current segment number:%d dataoff:%ld", segnum, (long)data_offset);
1540
1541 //write data
1542 Log("Writing part2 data ............................. ");
1543 fwrite(pData + SIZE_1MB, 1, VENTOY_EFI_PART_SIZE, fp);
1544 pSegment[segnum].disk_start_sector = Part2StartSector;
1545 pSegment[segnum].sector_num = VENTOY_EFI_PART_SIZE / 512;
1546 pSegment[segnum].data_offset = data_offset;
1547 data_offset += pSegment[segnum].sector_num * 512;
1548 segnum++;
1549
1550 if (PartStyle)
1551 {
1552 Log("Writing backup gpt table ............................. ");
1553 fwrite(pBkGptPartTbl, 1, 33 * 512, fp);
1554 pSegment[segnum].disk_start_sector = pPhyDrive->SizeInBytes / 512 - 33;
1555 pSegment[segnum].sector_num = 33;
1556 pSegment[segnum].data_offset = data_offset;
1557 data_offset += pSegment[segnum].sector_num * 512;
1558 segnum++;
1559 }
1560
1561 Log("Writing segment metadata ............................. ");
1562
1563 for (i = 0; i < (int)segnum; i++)
1564 {
1565 Log("SEG[%d]: PhySector:%llu SectorNum:%llu DataOffset:%llu(sector:%llu)", i, pSegment[i].disk_start_sector, pSegment[i].sector_num,
1566 pSegment[i].data_offset, pSegment[i].data_offset / 512);
1567 }
1568
1569 dataLen = segnum * sizeof(VTSI_SEGMENT);
1570 fwrite(pSegment, 1, dataLen, fp);
1571
1572 if (dataLen % 512)
1573 {
1574 //pData + SIZE_1MB - 8192 is a temp data buffer with zero
1575 fwrite(pData + SIZE_1MB - 8192, 1, 512 - (dataLen % 512), fp);
1576 }
1577
1578 //Fill footer
1579 pImgFooter->magic = VTSI_IMG_MAGIC;
1580 pImgFooter->version = 1;
1581 pImgFooter->disk_size = pPhyDrive->SizeInBytes;
1582 memcpy(&pImgFooter->disk_signature, pPhyDrive->MBR.BootCode + 0x1b8, 4);
1583 pImgFooter->segment_num = segnum;
1584 pImgFooter->segment_offset = data_offset;
1585
1586 for (i = 0, chksum = 0; i < (int)(segnum * sizeof(VTSI_SEGMENT)); i++)
1587 {
1588 chksum += *((UINT8 *)pSegment + i);
1589 }
1590 pImgFooter->segment_chksum = ~chksum;
1591
1592 for (i = 0, chksum = 0; i < sizeof(VTSI_FOOTER); i++)
1593 {
1594 chksum += *((UINT8 *)pImgFooter + i);
1595 }
1596 pImgFooter->foot_chksum = ~chksum;
1597
1598 Log("Writing footer segnum(%u) segoffset(%llu) ......................", segnum, data_offset);
1599 Log("disk_size=%llu disk_signature=%lx segment_offset=%llu", pImgFooter->disk_size, pImgFooter->disk_signature, pImgFooter->segment_offset);
1600
1601 fwrite(pImgFooter, 1, sizeof(VTSI_FOOTER), fp);
1602 fclose(fp);
1603
1604 Log("Writing Ventoy image file finished, the file size should be %llu .", data_offset + 512 + ((dataLen + 511) / 512 * 512));
1605
1606 rc = 0;
1607
1608 End:
1609
1610 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1611
1612 Log("retcode:%d\n", rc);
1613
1614 SAFE_FREE(pData);
1615 SAFE_FREE(ImgBuf);
1616
1617 return rc;
1618 }
1619
1620
1621 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId)
1622 {
1623 int i;
1624 int rc = 0;
1625 int state = 0;
1626 HANDLE hDrive;
1627 DWORD dwSize;
1628 BOOL bRet;
1629 CHAR MountDrive;
1630 CHAR DriveName[] = "?:\\";
1631 CHAR DriveLetters[MAX_PATH] = { 0 };
1632 MBR_HEAD MBR;
1633 VTOY_GPT_INFO *pGptInfo = NULL;
1634 UINT64 Part1StartSector = 0;
1635 UINT64 Part1SectorCount = 0;
1636 UINT64 Part2StartSector = 0;
1637
1638 Log("#####################################################");
1639 Log("InstallVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
1640 PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1641 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1642 Log("#####################################################");
1643
1644 if (PartStyle)
1645 {
1646 pGptInfo = malloc(sizeof(VTOY_GPT_INFO));
1647 memset(pGptInfo, 0, sizeof(VTOY_GPT_INFO));
1648 }
1649
1650 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1651
1652 if (PartStyle)
1653 {
1654 VentoyFillGpt(pPhyDrive->SizeInBytes, pGptInfo);
1655 Part1StartSector = pGptInfo->PartTbl[0].StartLBA;
1656 Part1SectorCount = pGptInfo->PartTbl[0].LastLBA - Part1StartSector + 1;
1657 Part2StartSector = pGptInfo->PartTbl[1].StartLBA;
1658 }
1659 else
1660 {
1661 VentoyFillMBR(pPhyDrive->SizeInBytes, &MBR, PartStyle);
1662 Part1StartSector = MBR.PartTbl[0].StartSectorId;
1663 Part1SectorCount = MBR.PartTbl[0].SectorCount;
1664 Part2StartSector = MBR.PartTbl[1].StartSectorId;
1665 }
1666
1667 Log("Lock disk for clean ............................. ");
1668
1669 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
1670 if (hDrive == INVALID_HANDLE_VALUE)
1671 {
1672 Log("Failed to open physical disk");
1673 free(pGptInfo);
1674 return 1;
1675 }
1676
1677 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1678
1679 if (DriveLetters[0] == 0)
1680 {
1681 Log("No drive letter was assigned...");
1682 DriveName[0] = GetFirstUnusedDriveLetter();
1683 Log("GetFirstUnusedDriveLetter %C: ...", DriveName[0]);
1684 }
1685 else
1686 {
1687 // Unmount all mounted volumes that belong to this drive
1688 // Do it in reverse so that we always end on the first volume letter
1689 for (i = (int)strlen(DriveLetters); i > 0; i--)
1690 {
1691 DriveName[0] = DriveLetters[i - 1];
1692 bRet = DeleteVolumeMountPointA(DriveName);
1693 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, GetLastError());
1694 }
1695 }
1696
1697 MountDrive = DriveName[0];
1698 Log("Will use '%C:' as volume mountpoint", DriveName[0]);
1699
1700 // It kind of blows, but we have to relinquish access to the physical drive
1701 // for VDS to be able to delete the partitions that reside on it...
1702 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
1703 CHECK_CLOSE_HANDLE(hDrive);
1704
1705 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
1706
1707 if (!VDS_DeleteAllPartitions(pPhyDrive->PhyDrive))
1708 {
1709 Log("Notice: Could not delete partitions: 0x%x, but we continue.", GetLastError());
1710 }
1711
1712 Log("Deleting all partitions ......................... OK");
1713
1714 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
1715
1716 Log("Lock disk for write ............................. ");
1717 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1718 if (hDrive == INVALID_HANDLE_VALUE)
1719 {
1720 Log("Failed to GetPhysicalHandle for write.");
1721 rc = 1;
1722 goto End;
1723 }
1724
1725 //Refresh Drive Layout
1726 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1727
1728 disk_io_set_param(hDrive, Part1StartSector + Part1SectorCount);// include the 2048 sector gap
1729
1730 PROGRESS_BAR_SET_POS(PT_FORMAT_PART1);
1731
1732 if (PartStyle == 1 && pPhyDrive->PartStyle == 0)
1733 {
1734 Log("Wait for format part1 ...");
1735 Sleep(1000 * 5);
1736 }
1737
1738 Log("Formatting part1 exFAT ...");
1739 if (0 != FormatPart1exFAT(pPhyDrive->SizeInBytes))
1740 {
1741 Log("FormatPart1exFAT failed.");
1742 rc = 1;
1743 goto End;
1744 }
1745
1746 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
1747 Log("Writing part2 FAT img ...");
1748
1749 if (0 != FormatPart2Fat(hDrive, Part2StartSector))
1750 {
1751 Log("FormatPart2Fat failed.");
1752 rc = 1;
1753 goto End;
1754 }
1755
1756 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
1757 Log("Writing Boot Image ............................. ");
1758 if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)
1759 {
1760 Log("WriteGrubStage1ToPhyDrive failed.");
1761 rc = 1;
1762 goto End;
1763 }
1764
1765 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
1766 Log("Writing Partition Table ........................ ");
1767 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
1768
1769 if (PartStyle)
1770 {
1771 VTOY_GPT_HDR BackupHead;
1772 LARGE_INTEGER liCurrentPosition;
1773
1774 SET_FILE_POS(pPhyDrive->SizeInBytes - 512);
1775 VentoyFillBackupGptHead(pGptInfo, &BackupHead);
1776 if (!WriteFile(hDrive, &BackupHead, sizeof(VTOY_GPT_HDR), &dwSize, NULL))
1777 {
1778 rc = 1;
1779 Log("Write GPT Backup Head Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1780 goto End;
1781 }
1782
1783 SET_FILE_POS(pPhyDrive->SizeInBytes - 512 * 33);
1784 if (!WriteFile(hDrive, pGptInfo->PartTbl, sizeof(pGptInfo->PartTbl), &dwSize, NULL))
1785 {
1786 rc = 1;
1787 Log("Write GPT Backup Part Table Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1788 goto End;
1789 }
1790
1791 SET_FILE_POS(0);
1792 if (!WriteFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL))
1793 {
1794 rc = 1;
1795 Log("Write GPT Info Failed, dwSize:%u (%u) ErrCode:%u", dwSize, sizeof(VTOY_GPT_INFO), GetLastError());
1796 goto End;
1797 }
1798
1799 Log("Write GPT Info OK ...");
1800 memcpy(&(pPhyDrive->MBR), &(pGptInfo->MBR), 512);
1801 }
1802 else
1803 {
1804 if (!WriteFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL))
1805 {
1806 rc = 1;
1807 Log("Write MBR Failed, dwSize:%u ErrCode:%u", dwSize, GetLastError());
1808 goto End;
1809 }
1810 Log("Write MBR OK ...");
1811 memcpy(&(pPhyDrive->MBR), &MBR, 512);
1812 }
1813
1814 //Refresh Drive Layout
1815 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1816
1817 End:
1818
1819 PROGRESS_BAR_SET_POS(PT_MOUNT_VOLUME);
1820
1821 if (rc == 0)
1822 {
1823 Log("Mounting Ventoy Partition ....................... ");
1824 Sleep(1000);
1825
1826 state = 0;
1827 memset(DriveLetters, 0, sizeof(DriveLetters));
1828 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
1829 Log("Logical drive letter after write ventoy: <%s>", DriveLetters);
1830
1831 for (i = 0; i < sizeof(DriveLetters) && DriveLetters[i]; i++)
1832 {
1833 DriveName[0] = DriveLetters[i];
1834 if (IsVentoyLogicalDrive(DriveName[0]))
1835 {
1836 Log("%s is ventoy part2, delete mountpoint", DriveName);
1837 DeleteVolumeMountPointA(DriveName);
1838 }
1839 else
1840 {
1841 Log("%s is ventoy part1, already mounted", DriveName);
1842 state = 1;
1843 }
1844 }
1845
1846 if (state != 1)
1847 {
1848 Log("need to mount ventoy part1...");
1849
1850 if (0 == GetVentoyVolumeName(pPhyDrive->PhyDrive, Part1StartSector, DriveLetters, sizeof(DriveLetters), FALSE))
1851 {
1852 DriveName[0] = MountDrive;
1853 bRet = SetVolumeMountPointA(DriveName, DriveLetters);
1854 Log("SetVolumeMountPoint <%s> <%s> bRet:%u code:%u", DriveName, DriveLetters, bRet, GetLastError());
1855 }
1856 else
1857 {
1858 Log("Failed to find ventoy volume");
1859 }
1860 }
1861 Log("OK\n");
1862 }
1863 else
1864 {
1865 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1866
1867 FindProcessOccupyDisk(hDrive, pPhyDrive);
1868
1869 if (!VDS_IsLastAvaliable())
1870 {
1871 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1872 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1873 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1874 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1875 Log("###### [Error:] Virtual Disk Service (VDS) Unavailable ######");
1876 }
1877 }
1878
1879 if (pGptInfo)
1880 {
1881 free(pGptInfo);
1882 }
1883
1884 CHECK_CLOSE_HANDLE(hDrive);
1885 return rc;
1886 }
1887
1888
1889 int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive)
1890 {
1891 int i, j;
1892 int rc = 1;
1893 int PhyDrive;
1894 int PartStyle;
1895 INT64 ReservedValue;
1896 UINT64 RecudeBytes;
1897 GUID Guid;
1898 MBR_HEAD MBR;
1899 VTOY_GPT_INFO *pGPT;
1900 MBR_HEAD *pMBR;
1901 DWORD dwSize = 0;
1902 VTOY_GPT_HDR BackupHead;
1903 HANDLE hDrive = INVALID_HANDLE_VALUE;
1904 GUID ZeroGuid = { 0 };
1905 static GUID WindowsDataPartType = { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
1906 static GUID EspPartType = { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
1907 static GUID BiosGrubPartType = { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
1908
1909 Log("#####################################################");
1910 Log("PartitionResizeForVentoy PhyDrive%d <<%s %s %dGB>>",
1911 pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
1912 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
1913 Log("#####################################################");
1914
1915 pGPT = &(pPhyDrive->Gpt);
1916 pMBR = &(pPhyDrive->Gpt.MBR);
1917 Log("Disksize:%llu Part2Start:%llu", pPhyDrive->SizeInBytes, pPhyDrive->ResizePart2StartSector * 512);
1918
1919 if (pMBR->PartTbl[0].FsFlag == 0xEE && memcmp(pGPT->Head.Signature, "EFI PART", 8) == 0)
1920 {
1921 PartStyle = 1;
1922 }
1923 else
1924 {
1925 PartStyle = 0;
1926 }
1927
1928 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
1929
1930 RecudeBytes = VENTOY_EFI_PART_SIZE;
1931 ReservedValue = GetReservedSpaceInMB();
1932 if (ReservedValue > 0)
1933 {
1934 Log("Reduce add reserved space %lldMB", (LONGLONG)ReservedValue);
1935 RecudeBytes += (UINT64)(ReservedValue * SIZE_1MB);
1936 }
1937
1938
1939 if (pPhyDrive->ResizeNoShrink == FALSE)
1940 {
1941 Log("Need to shrink the volume");
1942 if (DISK_ShrinkVolume(pPhyDrive->PhyDrive, pPhyDrive->ResizeVolumeGuid, pPhyDrive->Part1DriveLetter, pPhyDrive->ResizeOldPart1Size, RecudeBytes))
1943 {
1944 Log("Shrink volume success, now check again");
1945
1946 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1947 if (hDrive == INVALID_HANDLE_VALUE)
1948 {
1949 Log("Failed to GetPhysicalHandle for update.");
1950 goto End;
1951 }
1952
1953 //Refresh Drive Layout
1954 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
1955
1956 CHECK_CLOSE_HANDLE(hDrive);
1957
1958
1959 if (PartResizePreCheck(NULL) && pPhyDrive->ResizeNoShrink)
1960 {
1961 Log("Recheck after Shrink volume success");
1962 Log("After shrink Disksize:%llu Part2Start:%llu", pPhyDrive->SizeInBytes, pPhyDrive->ResizePart2StartSector * 512);
1963 }
1964 else
1965 {
1966 Log("Recheck after Shrink volume failed %u", pPhyDrive->ResizeNoShrink);
1967 goto End;
1968 }
1969 }
1970 else
1971 {
1972 Log("Shrink volume failed");
1973 goto End;
1974 }
1975 }
1976
1977
1978 //Now try write data
1979 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
1980 if (hDrive == INVALID_HANDLE_VALUE)
1981 {
1982 Log("Failed to GetPhysicalHandle for update.");
1983 goto End;
1984 }
1985
1986
1987 //Write partition 2 data
1988 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
1989 if (0 != FormatPart2Fat(hDrive, pPhyDrive->ResizePart2StartSector))
1990 {
1991 Log("FormatPart2Fat failed.");
1992 goto End;
1993 }
1994
1995 //Write grub stage2 gap
1996 PROGRESS_BAR_SET_POS(PT_WRITE_STG1_IMG);
1997 Log("Writing Boot Image ............................. ");
1998 if (WriteGrubStage1ToPhyDrive(hDrive, PartStyle) != 0)
1999 {
2000 Log("WriteGrubStage1ToPhyDrive failed.");
2001 goto End;
2002 }
2003
2004
2005 //Write partition table
2006 PROGRESS_BAR_SET_POS(PT_WRITE_PART_TABLE);
2007 Log("Writing partition table ............................. ");
2008
2009 VentoyGetLocalBootImg(&MBR);
2010 CoCreateGuid(&Guid);
2011 memcpy(MBR.BootCode + 0x180, &Guid, 16);
2012 memcpy(pMBR->BootCode, MBR.BootCode, 440);
2013
2014 if (PartStyle == 0)
2015 {
2016 for (i = 1; i < 4; i++)
2017 {
2018 if (pMBR->PartTbl[i].SectorCount == 0)
2019 {
2020 break;
2021 }
2022 }
2023
2024 if (i >= 4)
2025 {
2026 Log("Can not find MBR free partition table");
2027 goto End;
2028 }
2029
2030 for (j = i - 1; j > 0; j--)
2031 {
2032 Log("Move MBR partition table %d --> %d", j + 1, j + 2);
2033 memcpy(pMBR->PartTbl + (j + 1), pMBR->PartTbl + j, sizeof(PART_TABLE));
2034 }
2035
2036 memset(pMBR->PartTbl + 1, 0, sizeof(PART_TABLE));
2037 VentoyFillMBRLocation(pPhyDrive->SizeInBytes, (UINT32)pPhyDrive->ResizePart2StartSector, VENTOY_EFI_PART_SIZE / 512, pMBR->PartTbl + 1);
2038 pMBR->PartTbl[0].Active = 0x80; // bootable
2039 pMBR->PartTbl[1].Active = 0x00;
2040 pMBR->PartTbl[1].FsFlag = 0xEF; // EFI System Partition
2041
2042 if (!WriteDataToPhyDisk(hDrive, 0, pMBR, 512))
2043 {
2044 Log("Legacy BIOS write MBR failed");
2045 goto End;
2046 }
2047 }
2048 else
2049 {
2050 for (i = 1; i < 128; i++)
2051 {
2052 if (memcmp(&(pGPT->PartTbl[i].PartGuid), &ZeroGuid, sizeof(GUID)) == 0)
2053 {
2054 break;
2055 }
2056 }
2057
2058 if (i >= 128)
2059 {
2060 Log("Can not find GPT free partition table");
2061 goto End;
2062 }
2063
2064 for (j = i - 1; j > 0; j--)
2065 {
2066 Log("Move GPT partition table %d --> %d", j + 1, j + 2);
2067 memcpy(pGPT->PartTbl + (j + 1), pGPT->PartTbl + j, sizeof(VTOY_GPT_PART_TBL));
2068 }
2069
2070
2071 pMBR->BootCode[92] = 0x22;
2072
2073 // to fix windows issue
2074 memset(pGPT->PartTbl + 1, 0, sizeof(VTOY_GPT_PART_TBL));
2075 memcpy(&(pGPT->PartTbl[1].PartType), &WindowsDataPartType, sizeof(GUID));
2076 CoCreateGuid(&(pGPT->PartTbl[1].PartGuid));
2077
2078 pGPT->PartTbl[1].StartLBA = pGPT->PartTbl[0].LastLBA + 1;
2079 pGPT->PartTbl[1].LastLBA = pGPT->PartTbl[1].StartLBA + VENTOY_EFI_PART_SIZE / 512 - 1;
2080 pGPT->PartTbl[1].Attr = 0xC000000000000001ULL;
2081 memcpy(pGPT->PartTbl[1].Name, L"VTOYEFI", 7 * 2);
2082
2083 //Update CRC
2084 pGPT->Head.PartTblCrc = VentoyCrc32(pGPT->PartTbl, sizeof(pGPT->PartTbl));
2085 pGPT->Head.Crc = 0;
2086 pGPT->Head.Crc = VentoyCrc32(&(pGPT->Head), pGPT->Head.Length);
2087
2088 Log("pGPT->Head.EfiStartLBA=%llu", (ULONGLONG)pGPT->Head.EfiStartLBA);
2089 Log("pGPT->Head.EfiBackupLBA=%llu", (ULONGLONG)pGPT->Head.EfiBackupLBA);
2090
2091 VentoyFillBackupGptHead(pGPT, &BackupHead);
2092 if (!WriteDataToPhyDisk(hDrive, pGPT->Head.EfiBackupLBA * 512, &BackupHead, 512))
2093 {
2094 Log("UEFI write backup head failed");
2095 goto End;
2096 }
2097
2098 if (!WriteDataToPhyDisk(hDrive, (pGPT->Head.EfiBackupLBA - 32) * 512, pGPT->PartTbl, 512 * 32))
2099 {
2100 Log("UEFI write backup partition table failed");
2101 goto End;
2102 }
2103
2104 if (!WriteDataToPhyDisk(hDrive, 0, pGPT, 512 * 34))
2105 {
2106 Log("UEFI write MBR & Main partition table failed");
2107 goto End;
2108 }
2109 }
2110
2111
2112
2113 //Refresh Drive Layout
2114 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2115
2116 //We must close handle here, because it will block the refresh bellow
2117 CHECK_CLOSE_HANDLE(hDrive);
2118
2119 Sleep(2000);
2120
2121 //Refresh disk list
2122 PhyDrive = pPhyDrive->PhyDrive;
2123
2124 Log("#### Now Refresh PhyDrive ####");
2125 Ventoy2DiskDestroy();
2126 Ventoy2DiskInit();
2127
2128 pPhyDrive = GetPhyDriveInfoByPhyDrive(PhyDrive);
2129 if (pPhyDrive)
2130 {
2131 if (pPhyDrive->VentoyVersion[0] == 0)
2132 {
2133 Log("After process the Ventoy version is still invalid");
2134 goto End;
2135 }
2136
2137 Log("### Ventoy non-destructive installation successfully finished <%s>", pPhyDrive->VentoyVersion);
2138 }
2139 else
2140 {
2141 Log("### Ventoy non-destructive installation successfully finished <not found>");
2142 }
2143
2144 InitComboxCtrl(g_DialogHwnd, PhyDrive);
2145
2146 rc = 0;
2147
2148 End:
2149 CHECK_CLOSE_HANDLE(hDrive);
2150 return rc;
2151 }
2152
2153
2154 static BOOL DiskCheckWriteAccess(HANDLE hDrive)
2155 {
2156 DWORD dwSize;
2157 BOOL ret = FALSE;
2158 BOOL bRet = FALSE;
2159 BYTE Buffer[512];
2160 LARGE_INTEGER liCurPosition;
2161 LARGE_INTEGER liNewPosition;
2162
2163 liCurPosition.QuadPart = 2039 * 512;
2164 liNewPosition.QuadPart = 0;
2165 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2166 liNewPosition.QuadPart != liCurPosition.QuadPart)
2167 {
2168 Log("SetFilePointer1 Failed %u", LASTERR);
2169 goto out;
2170 }
2171
2172
2173 dwSize = 0;
2174 ret = ReadFile(hDrive, Buffer, 512, &dwSize, NULL);
2175 if ((!ret) || (dwSize != 512))
2176 {
2177 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2178 goto out;
2179 }
2180
2181
2182 liCurPosition.QuadPart = 2039 * 512;
2183 liNewPosition.QuadPart = 0;
2184 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2185 liNewPosition.QuadPart != liCurPosition.QuadPart)
2186 {
2187 Log("SetFilePointer2 Failed %u", LASTERR);
2188 goto out;
2189 }
2190
2191 dwSize = 0;
2192 ret = WriteFile(hDrive, Buffer, 512, &dwSize, NULL);
2193 if ((!ret) || dwSize != 512)
2194 {
2195 Log("Failed to write %d %u %u", ret, dwSize, LASTERR);
2196 goto out;
2197 }
2198
2199 bRet = TRUE;
2200
2201 out:
2202
2203 return bRet;
2204 }
2205
2206 static BOOL BackupDataBeforeCleanDisk(int PhyDrive, UINT64 DiskSize, BYTE **pBackup)
2207 {
2208 DWORD dwSize;
2209 DWORD dwStatus;
2210 BOOL Return = FALSE;
2211 BOOL ret = FALSE;
2212 BYTE *backup = NULL;
2213 UINT64 offset;
2214 HANDLE hDrive = INVALID_HANDLE_VALUE;
2215 LARGE_INTEGER liCurPosition;
2216 LARGE_INTEGER liNewPosition;
2217 VTOY_GPT_INFO *pGPT = NULL;
2218
2219 Log("BackupDataBeforeCleanDisk %d", PhyDrive);
2220
2221 // step1: check write access
2222 hDrive = GetPhysicalHandle(PhyDrive, TRUE, TRUE, FALSE);
2223 if (hDrive == INVALID_HANDLE_VALUE)
2224 {
2225 Log("Failed to GetPhysicalHandle for write.");
2226 goto out;
2227 }
2228
2229 if (DiskCheckWriteAccess(hDrive))
2230 {
2231 Log("DiskCheckWriteAccess success");
2232 CHECK_CLOSE_HANDLE(hDrive);
2233 }
2234 else
2235 {
2236 Log("DiskCheckWriteAccess failed");
2237 goto out;
2238 }
2239
2240 //step2 backup 4MB data
2241 backup = malloc(SIZE_1MB * 4);
2242 if (!backup)
2243 {
2244 goto out;
2245 }
2246
2247 hDrive = GetPhysicalHandle(PhyDrive, FALSE, FALSE, FALSE);
2248 if (hDrive == INVALID_HANDLE_VALUE)
2249 {
2250 goto out;
2251 }
2252
2253 //read first 2MB
2254 dwStatus = SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2255 if (dwStatus != 0)
2256 {
2257 goto out;
2258 }
2259
2260 dwSize = 0;
2261 ret = ReadFile(hDrive, backup, SIZE_2MB, &dwSize, NULL);
2262 if ((!ret) || (dwSize != SIZE_2MB))
2263 {
2264 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2265 goto out;
2266 }
2267
2268 pGPT = (VTOY_GPT_INFO *)backup;
2269 offset = pGPT->Head.EfiBackupLBA * 512;
2270 if (offset >= (DiskSize - SIZE_2MB) && offset < DiskSize)
2271 {
2272 Log("EFI partition table check success");
2273 }
2274 else
2275 {
2276 Log("Backup EFI LBA not in last 2MB range: %llu", pGPT->Head.EfiBackupLBA);
2277 goto out;
2278 }
2279
2280 //read last 2MB
2281 liCurPosition.QuadPart = DiskSize - SIZE_2MB;
2282 liNewPosition.QuadPart = 0;
2283 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2284 liNewPosition.QuadPart != liCurPosition.QuadPart)
2285 {
2286 goto out;
2287 }
2288
2289 dwSize = 0;
2290 ret = ReadFile(hDrive, backup + SIZE_2MB, SIZE_2MB, &dwSize, NULL);
2291 if ((!ret) || (dwSize != SIZE_2MB))
2292 {
2293 Log("Failed to read %d %u 0x%x", ret, dwSize, LASTERR);
2294 goto out;
2295 }
2296
2297 *pBackup = backup;
2298 backup = NULL; //For don't free later
2299 Return = TRUE;
2300
2301 out:
2302 CHECK_CLOSE_HANDLE(hDrive);
2303 if (backup)
2304 free(backup);
2305
2306 return Return;
2307 }
2308
2309
2310 static BOOL WriteBackupDataToDisk(HANDLE hDrive, UINT64 Offset, BYTE *Data, DWORD Length)
2311 {
2312 DWORD dwSize = 0;
2313 BOOL ret = FALSE;
2314 LARGE_INTEGER liCurPosition;
2315 LARGE_INTEGER liNewPosition;
2316
2317 Log("WriteBackupDataToDisk %llu %p %u", Offset, Data, Length);
2318
2319 liCurPosition.QuadPart = Offset;
2320 liNewPosition.QuadPart = 0;
2321 if (0 == SetFilePointerEx(hDrive, liCurPosition, &liNewPosition, FILE_BEGIN) ||
2322 liNewPosition.QuadPart != liCurPosition.QuadPart)
2323 {
2324 return FALSE;
2325 }
2326
2327 ret = WriteFile(hDrive, Data, Length, &dwSize, NULL);
2328 if ((!ret) || dwSize != Length)
2329 {
2330 Log("Failed to write %d %u %u", ret, dwSize, LASTERR);
2331 return FALSE;
2332 }
2333
2334 Log("WriteBackupDataToDisk %llu %p %u success", Offset, Data, Length);
2335 return TRUE;
2336 }
2337
2338
2339 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId)
2340 {
2341 int i;
2342 int rc = 0;
2343 int MaxRetry = 4;
2344 BOOL ForceMBR = FALSE;
2345 BOOL Esp2Basic = FALSE;
2346 BOOL ChangeAttr = FALSE;
2347 BOOL CleanDisk = FALSE;
2348 BOOL DelEFI = FALSE;
2349 BOOL bWriteBack = TRUE;
2350 HANDLE hVolume;
2351 HANDLE hDrive;
2352 DWORD Status;
2353 DWORD dwSize;
2354 BOOL bRet;
2355 CHAR DriveName[] = "?:\\";
2356 CHAR DriveLetters[MAX_PATH] = { 0 };
2357 CHAR BackBinFile[MAX_PATH];
2358 UINT64 StartSector;
2359 UINT64 ReservedMB = 0;
2360 MBR_HEAD BootImg;
2361 MBR_HEAD MBR;
2362 BYTE *pBackup = NULL;
2363 VTOY_GPT_INFO *pGptInfo = NULL;
2364 VTOY_GPT_INFO *pGptBkup = NULL;
2365 UINT8 ReservedData[4096];
2366
2367 Log("#####################################################");
2368 Log("UpdateVentoy2PhyDrive try%d %s PhyDrive%d <<%s %s %dGB>>", TryId,
2369 pPhyDrive->PartStyle ? "GPT" : "MBR", pPhyDrive->PhyDrive, pPhyDrive->VendorId, pPhyDrive->ProductId,
2370 GetHumanReadableGBSize(pPhyDrive->SizeInBytes));
2371 Log("#####################################################");
2372
2373 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2374
2375 Log("Lock disk for umount ............................ ");
2376
2377 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, FALSE, FALSE);
2378 if (hDrive == INVALID_HANDLE_VALUE)
2379 {
2380 Log("Failed to open physical disk");
2381 return 1;
2382 }
2383
2384 if (pPhyDrive->PartStyle)
2385 {
2386 pGptInfo = malloc(2 * sizeof(VTOY_GPT_INFO));
2387 if (!pGptInfo)
2388 {
2389 return 1;
2390 }
2391
2392 memset(pGptInfo, 0, 2 * sizeof(VTOY_GPT_INFO));
2393 pGptBkup = pGptInfo + 1;
2394
2395 // Read GPT Info
2396 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2397 ReadFile(hDrive, pGptInfo, sizeof(VTOY_GPT_INFO), &dwSize, NULL);
2398 memcpy(pGptBkup, pGptInfo, sizeof(VTOY_GPT_INFO));
2399
2400 //MBR will be used to compare with local boot image
2401 memcpy(&MBR, &pGptInfo->MBR, sizeof(MBR_HEAD));
2402
2403 StartSector = pGptInfo->PartTbl[1].StartLBA;
2404 Log("GPT StartSector in PartTbl:%llu", (ULONGLONG)StartSector);
2405
2406 ReservedMB = (pPhyDrive->SizeInBytes / 512 - (StartSector + VENTOY_EFI_PART_SIZE / 512) - 33) / 2048;
2407 Log("GPT Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);
2408 }
2409 else
2410 {
2411 // Read MBR
2412 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2413 ReadFile(hDrive, &MBR, sizeof(MBR), &dwSize, NULL);
2414
2415 StartSector = MBR.PartTbl[1].StartSectorId;
2416 Log("MBR StartSector in PartTbl:%llu", (ULONGLONG)StartSector);
2417
2418 ReservedMB = (pPhyDrive->SizeInBytes / 512 - (StartSector + VENTOY_EFI_PART_SIZE / 512)) / 2048;
2419 Log("MBR Reserved Disk Space:%llu MB", (ULONGLONG)ReservedMB);
2420 }
2421
2422 //Read Reserved Data
2423 SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);
2424 ReadFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);
2425
2426 GetLettersBelongPhyDrive(pPhyDrive->PhyDrive, DriveLetters, sizeof(DriveLetters));
2427
2428 if (DriveLetters[0] == 0)
2429 {
2430 Log("No drive letter was assigned...");
2431 }
2432 else
2433 {
2434 // Unmount all mounted volumes that belong to this drive
2435 // Do it in reverse so that we always end on the first volume letter
2436 for (i = (int)strlen(DriveLetters); i > 0; i--)
2437 {
2438 DriveName[0] = DriveLetters[i - 1];
2439 if (IsVentoyLogicalDrive(DriveName[0]))
2440 {
2441 Log("%s is ventoy logical drive", DriveName);
2442 bRet = DeleteVolumeMountPointA(DriveName);
2443 Log("Delete mountpoint %s ret:%u code:%u", DriveName, bRet, LASTERR);
2444 break;
2445 }
2446 }
2447 }
2448
2449 // It kind of blows, but we have to relinquish access to the physical drive
2450 // for VDS to be able to delete the partitions that reside on it...
2451 DeviceIoControl(hDrive, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2452 CHECK_CLOSE_HANDLE(hDrive);
2453
2454 if (pPhyDrive->PartStyle == 1)
2455 {
2456 Log("TryId=%d EFI GPT partition type is 0x%llx", TryId, pPhyDrive->Part2GPTAttr);
2457 PROGRESS_BAR_SET_POS(PT_DEL_ALL_PART);
2458
2459 if (TryId == 1)
2460 {
2461 Log("Change GPT partition type to ESP");
2462 if (DISK_ChangeVtoyEFI2ESP(pPhyDrive->PhyDrive, StartSector * 512ULL))
2463 {
2464 Esp2Basic = TRUE;
2465 Sleep(3000);
2466 }
2467 }
2468 else if (TryId == 2)
2469 {
2470 Log("Change GPT partition attribute");
2471 if (DISK_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, StartSector * 512ULL, 0x8000000000000001))
2472 {
2473 ChangeAttr = TRUE;
2474 Sleep(2000);
2475 }
2476 }
2477 else if (TryId == 3)
2478 {
2479 DISK_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive, StartSector * 512ULL);
2480 DelEFI = TRUE;
2481 }
2482 else if (TryId == 4)
2483 {
2484 Log("Clean disk GPT partition table");
2485 if (BackupDataBeforeCleanDisk(pPhyDrive->PhyDrive, pPhyDrive->SizeInBytes, &pBackup))
2486 {
2487 sprintf_s(BackBinFile, sizeof(BackBinFile), ".\\ventoy\\phydrive%d_%u_%d.bin",
2488 pPhyDrive->PhyDrive, GetCurrentProcessId(), g_backup_bin_index++);
2489 SaveBufToFile(BackBinFile, pBackup, 4 * SIZE_1MB);
2490 Log("Save backup data to %s", BackBinFile);
2491
2492 Log("Success to backup data before clean");
2493 CleanDisk = TRUE;
2494 DISK_CleanDisk(pPhyDrive->PhyDrive);
2495 Sleep(3000);
2496 }
2497 else
2498 {
2499 Log("Failed to backup data before clean");
2500 }
2501 }
2502 }
2503
2504 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_WRITE);
2505
2506 Log("Lock disk for update ............................ ");
2507 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2508 if (hDrive == INVALID_HANDLE_VALUE)
2509 {
2510 Log("Failed to GetPhysicalHandle for write.");
2511 rc = 1;
2512 goto End;
2513 }
2514
2515 PROGRESS_BAR_SET_POS(PT_LOCK_VOLUME);
2516
2517 Log("Lock volume for update .......................... ");
2518 hVolume = INVALID_HANDLE_VALUE;
2519
2520 //If we change VTOYEFI to ESP, it can not have s volume name, so don't try to get it.
2521 if (CleanDisk)
2522 {
2523 //writeback the last 2MB
2524 if (!WriteBackupDataToDisk(hDrive, pPhyDrive->SizeInBytes - SIZE_2MB, pBackup + SIZE_2MB, SIZE_2MB))
2525 {
2526 bWriteBack = FALSE;
2527 }
2528
2529 //write the first 2MB except parttable
2530 if (!WriteBackupDataToDisk(hDrive, 34 * 512, pBackup + 34 * 512, SIZE_2MB - 34 * 512))
2531 {
2532 bWriteBack = FALSE;
2533 }
2534
2535 Status = ERROR_NOT_FOUND;
2536 }
2537 else if (DelEFI)
2538 {
2539 Status = ERROR_NOT_FOUND;
2540 }
2541 else if (Esp2Basic)
2542 {
2543 Status = ERROR_NOT_FOUND;
2544 }
2545 else
2546 {
2547 for (i = 0; i < MaxRetry; i++)
2548 {
2549 Status = GetVentoyVolumeName(pPhyDrive->PhyDrive, StartSector, DriveLetters, sizeof(DriveLetters), TRUE);
2550 if (ERROR_SUCCESS == Status)
2551 {
2552 break;
2553 }
2554 else
2555 {
2556 Log("==== Volume not found, wait and retry %d... ====", i);
2557 Sleep(2);
2558 }
2559 }
2560 }
2561
2562 if (ERROR_SUCCESS == Status)
2563 {
2564 Log("Now lock and dismount volume <%s>", DriveLetters);
2565
2566 for (i = 0; i < MaxRetry; i++)
2567 {
2568 hVolume = CreateFileA(DriveLetters,
2569 GENERIC_READ | GENERIC_WRITE,
2570 FILE_SHARE_READ,
2571 NULL,
2572 OPEN_EXISTING,
2573 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
2574 NULL);
2575
2576 if (hVolume == INVALID_HANDLE_VALUE)
2577 {
2578 Log("Failed to create file volume, errcode:%u, wait and retry ...", LASTERR);
2579 Sleep(2000);
2580 }
2581 else
2582 {
2583 break;
2584 }
2585 }
2586
2587 if (hVolume == INVALID_HANDLE_VALUE)
2588 {
2589 Log("Failed to create file volume, errcode:%u", LASTERR);
2590 }
2591 else
2592 {
2593 bRet = DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2594 Log("FSCTL_LOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2595
2596 bRet = DeviceIoControl(hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2597 Log("FSCTL_DISMOUNT_VOLUME bRet:%u code:%u", bRet, LASTERR);
2598 }
2599 }
2600 else if (ERROR_NOT_FOUND == Status)
2601 {
2602 Log("Volume not found, maybe not supported");
2603 }
2604 else
2605 {
2606 rc = 1;
2607 goto End;
2608 }
2609
2610 bRet = TryWritePart2(hDrive, StartSector);
2611 if (FALSE == bRet && Esp2Basic)
2612 {
2613 Log("TryWritePart2 agagin ...");
2614 Sleep(3000);
2615 bRet = TryWritePart2(hDrive, StartSector);
2616 }
2617
2618 if (!bRet)
2619 {
2620 if (pPhyDrive->PartStyle == 0)
2621 {
2622 if (DiskCheckWriteAccess(hDrive))
2623 {
2624 Log("MBR DiskCheckWriteAccess success");
2625
2626 ForceMBR = TRUE;
2627
2628 Log("Try write failed, now delete partition 2 for MBR...");
2629 CHECK_CLOSE_HANDLE(hDrive);
2630
2631 Log("Now delete partition 2...");
2632 DISK_DeleteVtoyEFIPartition(pPhyDrive->PhyDrive, StartSector * 512ULL);
2633
2634 hDrive = GetPhysicalHandle(pPhyDrive->PhyDrive, TRUE, TRUE, FALSE);
2635 if (hDrive == INVALID_HANDLE_VALUE)
2636 {
2637 Log("Failed to GetPhysicalHandle for write.");
2638 rc = 1;
2639 goto End;
2640 }
2641 }
2642 else
2643 {
2644 Log("MBR DiskCheckWriteAccess failed");
2645 }
2646 }
2647 else
2648 {
2649 Log("TryWritePart2 failed ....");
2650 rc = 1;
2651 goto End;
2652 }
2653 }
2654
2655 PROGRESS_BAR_SET_POS(PT_FORMAT_PART2);
2656
2657 Log("Write Ventoy to disk ............................ ");
2658 if (0 != FormatPart2Fat(hDrive, StartSector))
2659 {
2660 rc = 1;
2661 goto End;
2662 }
2663
2664 if (hVolume != INVALID_HANDLE_VALUE)
2665 {
2666 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2667 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2668 CHECK_CLOSE_HANDLE(hVolume);
2669 }
2670
2671 Log("Updating Boot Image ............................. ");
2672 if (WriteGrubStage1ToPhyDrive(hDrive, pPhyDrive->PartStyle) != 0)
2673 {
2674 rc = 1;
2675 goto End;
2676 }
2677
2678 //write reserved data
2679 SetFilePointer(hDrive, 512 * 2040, NULL, FILE_BEGIN);
2680 bRet = WriteFile(hDrive, ReservedData, sizeof(ReservedData), &dwSize, NULL);
2681 Log("Write resv data ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2682
2683 // Boot Image
2684 VentoyGetLocalBootImg(&BootImg);
2685
2686 // Use Old UUID
2687 memcpy(BootImg.BootCode + 0x180, MBR.BootCode + 0x180, 16);
2688 if (pPhyDrive->PartStyle)
2689 {
2690 BootImg.BootCode[92] = 0x22;
2691 }
2692
2693 if (ForceMBR == FALSE && memcmp(BootImg.BootCode, MBR.BootCode, 440) == 0)
2694 {
2695 Log("Boot image has no difference, no need to write.");
2696 }
2697 else
2698 {
2699 Log("Boot image need to write %u.", ForceMBR);
2700
2701 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2702
2703 memcpy(MBR.BootCode, BootImg.BootCode, 440);
2704 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
2705 Log("Write Boot Image ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2706 }
2707
2708 if (pPhyDrive->PartStyle == 0)
2709 {
2710 if (0x00 == MBR.PartTbl[0].Active && 0x80 == MBR.PartTbl[1].Active)
2711 {
2712 Log("Need to chage 1st partition active and 2nd partition inactive.");
2713
2714 MBR.PartTbl[0].Active = 0x80;
2715 MBR.PartTbl[1].Active = 0x00;
2716
2717 SetFilePointer(hDrive, 0, NULL, FILE_BEGIN);
2718 bRet = WriteFile(hDrive, &MBR, 512, &dwSize, NULL);
2719 Log("Write NEW MBR ret:%u dwSize:%u Error:%u", bRet, dwSize, LASTERR);
2720 }
2721 }
2722
2723 if (CleanDisk)
2724 {
2725 if (!WriteBackupDataToDisk(hDrive, 0, pBackup, 34 * 512))
2726 {
2727 bWriteBack = FALSE;
2728 }
2729
2730 free(pBackup);
2731
2732 if (bWriteBack)
2733 {
2734 Log("Write backup data success, now delete %s", BackBinFile);
2735 DeleteFileA(BackBinFile);
2736 }
2737 else
2738 {
2739 Log("Write backup data failed");
2740 }
2741
2742 Sleep(1000);
2743 }
2744 else if (DelEFI)
2745 {
2746 VTOY_GPT_HDR BackupHdr;
2747
2748 VentoyFillBackupGptHead(pGptBkup, &BackupHdr);
2749 if (!WriteBackupDataToDisk(hDrive, 512 * pGptBkup->Head.EfiBackupLBA, (BYTE*)(&BackupHdr), 512))
2750 {
2751 bWriteBack = FALSE;
2752 }
2753
2754 if (!WriteBackupDataToDisk(hDrive, 512 * (pGptBkup->Head.EfiBackupLBA - 32), (BYTE*)(pGptBkup->PartTbl), 32 * 512))
2755 {
2756 bWriteBack = FALSE;
2757 }
2758
2759 if (!WriteBackupDataToDisk(hDrive, 512, (BYTE*)pGptBkup + 512, 33 * 512))
2760 {
2761 bWriteBack = FALSE;
2762 }
2763
2764 if (bWriteBack)
2765 {
2766 Log("Write backup partition table success");
2767 }
2768 else
2769 {
2770 Log("Write backup partition table failed");
2771 }
2772
2773 Sleep(1000);
2774 }
2775
2776 //Refresh Drive Layout
2777 DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwSize, NULL);
2778
2779 End:
2780
2781 if (hVolume != INVALID_HANDLE_VALUE)
2782 {
2783 bRet = DeviceIoControl(hVolume, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL);
2784 Log("FSCTL_UNLOCK_VOLUME bRet:%u code:%u", bRet, LASTERR);
2785 CHECK_CLOSE_HANDLE(hVolume);
2786 }
2787
2788 if (rc == 0)
2789 {
2790 Log("OK");
2791 }
2792 else
2793 {
2794 PROGRESS_BAR_SET_POS(PT_LOCK_FOR_CLEAN);
2795 FindProcessOccupyDisk(hDrive, pPhyDrive);
2796 }
2797
2798 CHECK_CLOSE_HANDLE(hDrive);
2799
2800 if (Esp2Basic)
2801 {
2802 Log("Recover GPT partition type to basic");
2803 DISK_ChangeVtoyEFI2Basic(pPhyDrive->PhyDrive, StartSector * 512);
2804 }
2805
2806 if (pPhyDrive->PartStyle == 1)
2807 {
2808 if (ChangeAttr || ((pPhyDrive->Part2GPTAttr >> 56) != 0xC0))
2809 {
2810 Log("Change EFI partition attr %u <0x%llx> to <0x%llx>", ChangeAttr, pPhyDrive->Part2GPTAttr, 0xC000000000000001ULL);
2811 if (DISK_ChangeVtoyEFIAttr(pPhyDrive->PhyDrive, StartSector * 512ULL, 0xC000000000000001ULL))
2812 {
2813 Log("Change EFI partition attr success");
2814 pPhyDrive->Part2GPTAttr = 0xC000000000000001ULL;
2815 }
2816 else
2817 {
2818 Log("Change EFI partition attr failed");
2819 }
2820 }
2821 }
2822
2823 if (pGptInfo)
2824 {
2825 free(pGptInfo);
2826 }
2827
2828 return rc;
2829 }
2830
2831