1 /******************************************************************************
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 * Copyright (c) 2011-2020, Pete Batard <pete@akeo.ie>
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.
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.
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/>.
22 #include "Ventoy2Disk.h"
24 void TraceOut(const char *Fmt
, ...)
32 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
35 fopen_s(&File
, VENTOY_FILE_LOG
, "a+");
38 fwrite(szBuf
, 1, Len
, File
);
43 void Log(const char *Fmt
, ...)
52 Len
+= safe_sprintf(szBuf
,
53 "[%4d/%02d/%02d %02d:%02d:%02d.%03d] ",
54 Sys
.wYear
, Sys
.wMonth
, Sys
.wDay
,
55 Sys
.wHour
, Sys
.wMinute
, Sys
.wSecond
,
59 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
62 //printf("%s\n", szBuf);
65 fopen_s(&File
, VENTOY_FILE_LOG
, "a+");
68 fwrite(szBuf
, 1, Len
, File
);
69 fwrite("\n", 1, 1, File
);
76 BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...)
81 CHAR FilePath
[MAX_PATH
];
84 vsnprintf_s(FilePath
, sizeof(FilePath
), sizeof(FilePath
), Fmt
, Arg
);
87 hFile
= CreateFileA(FilePath
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
88 if (INVALID_HANDLE_VALUE
== hFile
)
95 Attr
= GetFileAttributesA(FilePath
);
99 if ((Attr
& FILE_ATTRIBUTE_DIRECTORY
) == 0)
106 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
115 int SaveBufToFile(const CHAR
*FileName
, const void *Buffer
, int BufLen
)
120 fopen_s(&File
, FileName
, "wb");
123 Log("Failed to open file %s", FileName
);
127 fwrite(Buffer
, 1, BufLen
, File
);
132 int ReadWholeFileToBuf(const CHAR
*FileName
, int ExtLen
, void **Bufer
, int *BufLen
)
138 fopen_s(&File
, FileName
, "rb");
141 Log("Failed to open file %s", FileName
);
145 fseek(File
, 0, SEEK_END
);
146 FileSize
= (int)ftell(File
);
148 Data
= malloc(FileSize
+ ExtLen
);
155 fseek(File
, 0, SEEK_SET
);
156 fread(Data
, 1, FileSize
, File
);
166 const CHAR
* GetLocalVentoyVersion(void)
172 static CHAR LocalVersion
[64] = { 0 };
174 if (LocalVersion
[0] == 0)
176 rc
= ReadWholeFileToBuf(VENTOY_FILE_VERSION
, 1, (void **)&Buf
, &FileSize
);
183 for (Pos
= Buf
; *Pos
; Pos
++)
185 if (*Pos
== '\r' || *Pos
== '\n')
192 safe_sprintf(LocalVersion
, "%s", Buf
);
199 const CHAR
* ParseVentoyVersionFromString(CHAR
*Buf
)
203 static CHAR LocalVersion
[64] = { 0 };
205 Pos
= strstr(Buf
, "VENTOY_VERSION=");
208 Pos
+= strlen("VENTOY_VERSION=");
215 while (*End
!= 0 && *End
!= '"' && *End
!= '\r' && *End
!= '\n')
222 safe_sprintf(LocalVersion
, "%s", Pos
);
231 typedef BOOL(WINAPI
*LPFN_ISWOW64PROCESS
)(HANDLE
, PBOOL
);
232 LPFN_ISWOW64PROCESS fnIsWow64Process
;
233 BOOL bIsWow64
= FALSE
;
235 fnIsWow64Process
= (LPFN_ISWOW64PROCESS
)GetProcAddress(GetModuleHandleA("kernel32"), "IsWow64Process");
236 if (NULL
!= fnIsWow64Process
)
238 fnIsWow64Process(GetCurrentProcess(), &bIsWow64
);
245 * Some code and functions in the file are copied from rufus.
246 * https://github.com/pbatard/rufus
249 /* Windows versions */
250 enum WindowsVersion
{
251 WINDOWS_UNDEFINED
= -1,
252 WINDOWS_UNSUPPORTED
= 0,
254 WINDOWS_2003
= 0x52, // Also XP_64
255 WINDOWS_VISTA
= 0x60, // Also Server 2008
256 WINDOWS_7
= 0x61, // Also Server 2008_R2
257 WINDOWS_8
= 0x62, // Also Server 2012
258 WINDOWS_8_1
= 0x63, // Also Server 2012_R2
259 WINDOWS_10_PREVIEW1
= 0x64,
260 WINDOWS_10
= 0xA0, // Also Server 2016, also Server 2019
261 WINDOWS_11
= 0xB0, // Also Server 2022
265 static const char* GetEdition(DWORD ProductType
)
267 // From: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getproductinfo
268 // These values can be found in the winnt.h header.
269 switch (ProductType
) {
270 case 0x00000000: return ""; // Undefined
271 case 0x00000001: return "Ultimate";
272 case 0x00000002: return "Home Basic";
273 case 0x00000003: return "Home Premium";
274 case 0x00000004: return "Enterprise";
275 case 0x00000005: return "Home Basic N";
276 case 0x00000006: return "Business";
277 case 0x00000007: return "Standard Server";
278 case 0x00000008: return "Datacenter Server";
279 case 0x00000009: return "Smallbusiness Server";
280 case 0x0000000A: return "Enterprise Server";
281 case 0x0000000B: return "Starter";
282 case 0x00000010: return "Business N";
283 case 0x00000011: return "Web Server";
284 case 0x00000012: return "Cluster Server";
285 case 0x00000013: return "Home Server";
286 case 0x0000001A: return "Home Premium N";
287 case 0x0000001B: return "Enterprise N";
288 case 0x0000001C: return "Ultimate N";
289 case 0x00000022: return "Home Premium Server";
290 case 0x0000002F: return "Starter N";
291 case 0x00000030: return "Pro";
292 case 0x00000031: return "Pro N";
293 case 0x00000042: return "Starter E";
294 case 0x00000043: return "Home Basic E";
295 case 0x00000044: return "Premium E";
296 case 0x00000045: return "Pro E";
297 case 0x00000046: return "Enterprise E";
298 case 0x00000047: return "Ultimate E";
299 case 0x00000048: return "Enterprise Eval";
300 case 0x00000054: return "Enterprise N Eval";
301 case 0x00000057: return "Thin PC";
302 case 0x0000006F: return "Core Connected";
303 case 0x00000070: return "Pro Student";
304 case 0x00000071: return "Core Connected N";
305 case 0x00000072: return "Pro Student N";
306 case 0x00000073: return "Core Connected Single Language";
307 case 0x00000074: return "Core Connected China";
308 case 0x00000079: return "Edu";
309 case 0x0000007A: return "Edu N";
310 case 0x0000007D: return "Enterprise S";
311 case 0x0000007E: return "Enterprise S N";
312 case 0x0000007F: return "Pro S";
313 case 0x00000080: return "Pro S N";
314 case 0x00000081: return "Enterprise S Eval";
315 case 0x00000082: return "Enterprise S N Eval";
316 case 0x0000008A: return "Pro Single Language";
317 case 0x0000008B: return "Pro China";
318 case 0x0000008C: return "Enterprise Subscription";
319 case 0x0000008D: return "Enterprise Subscription N";
320 case 0x00000095: return "Utility VM";
321 case 0x000000A1: return "Pro Workstation";
322 case 0x000000A2: return "Pro Workstation N";
323 case 0x000000A4: return "Pro for Education";
324 case 0x000000A5: return "Pro for Education N";
325 case 0x000000AB: return "Enterprise G"; // I swear Microsoft are just making up editions...
326 case 0x000000AC: return "Enterprise G N";
327 case 0x000000B6: return "Core OS";
328 case 0x000000B7: return "Cloud E";
329 case 0x000000B8: return "Cloud E N";
330 case 0x000000BD: return "Lite";
331 case 0xABCDABCD: return "(Unlicensed)";
332 default: return "(Unknown Edition)";
336 #define is_x64 IsWow64
337 #define static_strcpy safe_strcpy
338 #define REGKEY_HKCU HKEY_CURRENT_USER
339 #define REGKEY_HKLM HKEY_LOCAL_MACHINE
340 static int nWindowsVersion
= WINDOWS_UNDEFINED
;
341 static int nWindowsBuildNumber
= -1;
342 static char WindowsVersionStr
[128] = "";
344 /* Helpers for 32 bit registry operations */
347 * Read a generic registry key value. If a short key_name is used, assume that
348 * it belongs to the application and create the app subkey if required
350 static __inline BOOL
_GetRegistryKey(HKEY key_root
, const char* key_name
, DWORD reg_type
,
351 LPBYTE dest
, DWORD dest_size
)
353 const char software_prefix
[] = "SOFTWARE\\";
354 char long_key_name
[MAX_PATH
] = { 0 };
358 HKEY hSoftware
= NULL
, hApp
= NULL
;
359 DWORD dwType
= -1, dwSize
= dest_size
;
361 memset(dest
, 0, dest_size
);
363 if (key_name
== NULL
)
366 for (i
= strlen(key_name
); i
>0; i
--) {
367 if (key_name
[i
] == '\\')
372 // Prefix with "SOFTWARE" if needed
373 if (_strnicmp(key_name
, software_prefix
, sizeof(software_prefix
)-1) != 0) {
374 if (i
+ sizeof(software_prefix
) >= sizeof(long_key_name
))
376 strcpy_s(long_key_name
, sizeof(long_key_name
), software_prefix
);
377 strcat_s(long_key_name
, sizeof(long_key_name
), key_name
);
378 long_key_name
[sizeof(software_prefix
)+i
- 1] = 0;
381 if (i
>= sizeof(long_key_name
))
383 static_strcpy(long_key_name
, key_name
);
384 long_key_name
[i
] = 0;
387 if (RegOpenKeyExA(key_root
, long_key_name
, 0, KEY_READ
, &hApp
) != ERROR_SUCCESS
) {
393 if (RegOpenKeyExA(key_root
, "SOFTWARE", 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &hSoftware
) != ERROR_SUCCESS
) {
399 s
= RegQueryValueExA(hApp
, &key_name
[i
], NULL
, &dwType
, (LPBYTE
)dest
, &dwSize
);
400 // No key means default value of 0 or empty string
401 if ((s
== ERROR_FILE_NOT_FOUND
) || ((s
== ERROR_SUCCESS
) && (dwType
== reg_type
) && (dwSize
> 0))) {
405 if (hSoftware
!= NULL
)
406 RegCloseKey(hSoftware
);
412 #define GetRegistryKey32(root, key, pval) _GetRegistryKey(root, key, REG_DWORD, (LPBYTE)pval, sizeof(DWORD))
413 static __inline INT32
ReadRegistryKey32(HKEY root
, const char* key
) {
415 GetRegistryKey32(root
, key
, &val
);
420 * Modified from smartmontools' os_win32.cpp
422 void GetWindowsVersion(void)
424 OSVERSIONINFOEXA vi
, vi2
;
427 const char* w64
= "32 bit";
430 unsigned major
, minor
;
431 ULONGLONG major_equal
, minor_equal
;
434 nWindowsVersion
= WINDOWS_UNDEFINED
;
435 static_strcpy(WindowsVersionStr
, "Windows Undefined");
437 // suppress the C4996 warning for GetVersionExA
438 #pragma warning(push)
439 #pragma warning(disable:4996)
441 memset(&vi
, 0, sizeof(vi
));
442 vi
.dwOSVersionInfoSize
= sizeof(vi
);
443 if (!GetVersionExA((OSVERSIONINFOA
*)&vi
)) {
444 memset(&vi
, 0, sizeof(vi
));
445 vi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOA
);
446 if (!GetVersionExA((OSVERSIONINFOA
*)&vi
))
452 if (vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
454 if (vi
.dwMajorVersion
> 6 || (vi
.dwMajorVersion
== 6 && vi
.dwMinorVersion
>= 2)) {
455 // Starting with Windows 8.1 Preview, GetVersionEx() does no longer report the actual OS version
456 // See: http://msdn.microsoft.com/en-us/library/windows/desktop/dn302074.aspx
457 // And starting with Windows 10 Preview 2, Windows enforces the use of the application/supportedOS
458 // manifest in order for VerSetConditionMask() to report the ACTUAL OS major and minor...
460 major_equal
= VerSetConditionMask(0, VER_MAJORVERSION
, VER_EQUAL
);
461 for (major
= vi
.dwMajorVersion
; major
<= 9; major
++) {
462 memset(&vi2
, 0, sizeof(vi2
));
463 vi2
.dwOSVersionInfoSize
= sizeof(vi2
); vi2
.dwMajorVersion
= major
;
464 if (!VerifyVersionInfoA(&vi2
, VER_MAJORVERSION
, major_equal
))
466 if (vi
.dwMajorVersion
< major
) {
467 vi
.dwMajorVersion
= major
; vi
.dwMinorVersion
= 0;
470 minor_equal
= VerSetConditionMask(0, VER_MINORVERSION
, VER_EQUAL
);
471 for (minor
= vi
.dwMinorVersion
; minor
<= 9; minor
++) {
472 memset(&vi2
, 0, sizeof(vi2
)); vi2
.dwOSVersionInfoSize
= sizeof(vi2
);
473 vi2
.dwMinorVersion
= minor
;
474 if (!VerifyVersionInfoA(&vi2
, VER_MINORVERSION
, minor_equal
))
476 vi
.dwMinorVersion
= minor
;
484 if (vi
.dwMajorVersion
<= 0xf && vi
.dwMinorVersion
<= 0xf) {
485 ws
= (vi
.wProductType
<= VER_NT_WORKSTATION
);
486 nWindowsVersion
= vi
.dwMajorVersion
<< 4 | vi
.dwMinorVersion
;
487 switch (nWindowsVersion
) {
488 case WINDOWS_XP
: w
= "XP";
490 case WINDOWS_2003
: w
= (ws
? "XP_64" : (!GetSystemMetrics(89) ? "Server 2003" : "Server 2003_R2"));
492 case WINDOWS_VISTA
: w
= (ws
? "Vista" : "Server 2008");
494 case WINDOWS_7
: w
= (ws
? "7" : "Server 2008_R2");
496 case WINDOWS_8
: w
= (ws
? "8" : "Server 2012");
498 case WINDOWS_8_1
: w
= (ws
? "8.1" : "Server 2012_R2");
500 case WINDOWS_10_PREVIEW1
: w
= (ws
? "10 (Preview 1)" : "Server 10 (Preview 1)");
502 // Starting with Windows 10 Preview 2, the major is the same as the public-facing version
504 if (vi
.dwBuildNumber
< 20000) {
505 w
= (ws
? "10" : ((vi
.dwBuildNumber
< 17763) ? "Server 2016" : "Server 2019"));
508 nWindowsVersion
= WINDOWS_11
;
510 case WINDOWS_11
: w
= (ws
? "11" : "Server 2022");
513 if (nWindowsVersion
< WINDOWS_XP
)
514 nWindowsVersion
= WINDOWS_UNSUPPORTED
;
525 GetProductInfo(vi
.dwMajorVersion
, vi
.dwMinorVersion
, vi
.wServicePackMajor
, vi
.wServicePackMinor
, &dwProductType
);
526 vptr
= WindowsVersionStr
;
527 vlen
= sizeof(WindowsVersionStr
) - 1;
530 sprintf_s(vptr
, vlen
, "%s %u.%u %s", (vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
? "NT" : "??"),
531 (unsigned)vi
.dwMajorVersion
, (unsigned)vi
.dwMinorVersion
, w64
);
532 else if (vi
.wServicePackMinor
)
533 sprintf_s(vptr
, vlen
, "%s SP%u.%u %s", w
, vi
.wServicePackMajor
, vi
.wServicePackMinor
, w64
);
534 else if (vi
.wServicePackMajor
)
535 sprintf_s(vptr
, vlen
, "%s SP%u %s", w
, vi
.wServicePackMajor
, w64
);
537 sprintf_s(vptr
, vlen
, "%s%s%s, %s",
538 w
, (dwProductType
!= PRODUCT_UNDEFINED
) ? " " : "", GetEdition(dwProductType
), w64
);
540 // Add the build number (including UBR if available) for Windows 8.0 and later
541 nWindowsBuildNumber
= vi
.dwBuildNumber
;
542 if (nWindowsVersion
>= 0x62) {
543 int nUbr
= ReadRegistryKey32(REGKEY_HKLM
, "Software\\Microsoft\\Windows NT\\CurrentVersion\\UBR");
544 vptr
= WindowsVersionStr
+ strlen(WindowsVersionStr
);
545 vlen
= sizeof(WindowsVersionStr
) - strlen(WindowsVersionStr
) - 1;
547 sprintf_s(vptr
, vlen
, " (Build %d.%d)", nWindowsBuildNumber
, nUbr
);
549 sprintf_s(vptr
, vlen
, " (Build %d)", nWindowsBuildNumber
);
555 void DumpWindowsVersion(void)
558 Log("Windows Version: <<Windows %s>>", WindowsVersionStr
);
562 BOOL
IsVentoyLogicalDrive(CHAR DriveLetter
)
565 CONST CHAR
*Files
[] =
567 "EFI\\BOOT\\BOOTX64.EFI",
568 "grub\\themes\\ventoy\\theme.txt",
569 "ventoy\\ventoy.cpio",
572 for (i
= 0; i
< sizeof(Files
) / sizeof(Files
[0]); i
++)
574 if (!IsFileExist("%C:\\%s", DriveLetter
, Files
[i
]))
584 int VentoyFillMBRLocation(UINT64 DiskSizeInBytes
, UINT32 StartSectorId
, UINT32 SectorCount
, PART_TABLE
*Table
)
593 while (nHead
!= 0 && (DiskSizeInBytes
/ 512 / nSector
/ nHead
) > 1024)
595 nHead
= (BYTE
)nHead
* 2;
603 Cylinder
= StartSectorId
/ nSector
/ nHead
;
604 Head
= StartSectorId
/ nSector
% nHead
;
605 Sector
= StartSectorId
% nSector
+ 1;
607 Table
->StartHead
= Head
;
608 Table
->StartSector
= Sector
;
609 Table
->StartCylinder
= Cylinder
;
611 EndSectorId
= StartSectorId
+ SectorCount
- 1;
612 Cylinder
= EndSectorId
/ nSector
/ nHead
;
613 Head
= EndSectorId
/ nSector
% nHead
;
614 Sector
= EndSectorId
% nSector
+ 1;
616 Table
->EndHead
= Head
;
617 Table
->EndSector
= Sector
;
618 Table
->EndCylinder
= Cylinder
;
620 Table
->StartSectorId
= StartSectorId
;
621 Table
->SectorCount
= SectorCount
;
626 int VentoyFillMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
, int PartStyle
)
630 UINT32 DiskSignature
;
631 UINT32 DiskSectorCount
;
632 UINT32 PartSectorCount
;
633 UINT32 PartStartSector
;
634 UINT32 ReservedSector
;
636 VentoyGetLocalBootImg(pMBR
);
640 memcpy(&DiskSignature
, &Guid
, sizeof(UINT32
));
642 Log("Disk signature: 0x%08x", DiskSignature
);
644 *((UINT32
*)(pMBR
->BootCode
+ 0x1B8)) = DiskSignature
;
646 if (DiskSizeBytes
/ 512 > 0xFFFFFFFF)
648 DiskSectorCount
= 0xFFFFFFFF;
652 DiskSectorCount
= (UINT32
)(DiskSizeBytes
/ 512);
655 ReservedValue
= GetReservedSpaceInMB();
656 if (ReservedValue
<= 0)
662 ReservedSector
= (UINT32
)(ReservedValue
* 2048);
667 ReservedSector
+= 33; // backup GPT part table
670 // check aligned with 4KB
671 if (IsPartNeed4KBAlign())
673 UINT64 sectors
= DiskSizeBytes
/ 512;
676 Log("Disk need to align with 4KB %u", (UINT32
)(sectors
% 8));
677 ReservedSector
+= (UINT32
)(sectors
% 8);
681 Log("ReservedSector: %u", ReservedSector
);
684 PartStartSector
= VENTOY_PART1_START_SECTOR
;
685 PartSectorCount
= DiskSectorCount
- ReservedSector
- VENTOY_EFI_PART_SIZE
/ 512 - PartStartSector
;
686 VentoyFillMBRLocation(DiskSizeBytes
, PartStartSector
, PartSectorCount
, pMBR
->PartTbl
);
688 pMBR
->PartTbl
[0].Active
= 0x80; // bootable
689 pMBR
->PartTbl
[0].FsFlag
= 0x07; // exFAT/NTFS/HPFS
692 PartStartSector
+= PartSectorCount
;
693 PartSectorCount
= VENTOY_EFI_PART_SIZE
/ 512;
694 VentoyFillMBRLocation(DiskSizeBytes
, PartStartSector
, PartSectorCount
, pMBR
->PartTbl
+ 1);
696 pMBR
->PartTbl
[1].Active
= 0x00;
697 pMBR
->PartTbl
[1].FsFlag
= 0xEF; // EFI System Partition
706 static int VentoyFillProtectMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
)
709 UINT32 DiskSignature
;
710 UINT64 DiskSectorCount
;
712 VentoyGetLocalBootImg(pMBR
);
716 memcpy(&DiskSignature
, &Guid
, sizeof(UINT32
));
718 Log("Disk signature: 0x%08x", DiskSignature
);
720 *((UINT32
*)(pMBR
->BootCode
+ 0x1B8)) = DiskSignature
;
722 DiskSectorCount
= DiskSizeBytes
/ 512 - 1;
723 if (DiskSectorCount
> 0xFFFFFFFF)
725 DiskSectorCount
= 0xFFFFFFFF;
728 memset(pMBR
->PartTbl
, 0, sizeof(pMBR
->PartTbl
));
730 pMBR
->PartTbl
[0].Active
= 0x00;
731 pMBR
->PartTbl
[0].FsFlag
= 0xee; // EE
733 pMBR
->PartTbl
[0].StartHead
= 0;
734 pMBR
->PartTbl
[0].StartSector
= 1;
735 pMBR
->PartTbl
[0].StartCylinder
= 0;
736 pMBR
->PartTbl
[0].EndHead
= 254;
737 pMBR
->PartTbl
[0].EndSector
= 63;
738 pMBR
->PartTbl
[0].EndCylinder
= 1023;
740 pMBR
->PartTbl
[0].StartSectorId
= 1;
741 pMBR
->PartTbl
[0].SectorCount
= (UINT32
)DiskSectorCount
;
746 pMBR
->BootCode
[92] = 0x22;
751 int VentoyFillWholeGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
)
753 UINT64 Part1SectorCount
= 0;
754 UINT64 DiskSectorCount
= DiskSizeBytes
/ 512;
755 VTOY_GPT_HDR
*Head
= &pInfo
->Head
;
756 VTOY_GPT_PART_TBL
*Table
= pInfo
->PartTbl
;
757 static GUID WindowsDataPartType
= { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
759 VentoyFillProtectMBR(DiskSizeBytes
, &pInfo
->MBR
);
761 Part1SectorCount
= DiskSectorCount
- 33 - 2048;
763 memcpy(Head
->Signature
, "EFI PART", 8);
764 Head
->Version
[2] = 0x01;
767 Head
->EfiStartLBA
= 1;
768 Head
->EfiBackupLBA
= DiskSectorCount
- 1;
769 Head
->PartAreaStartLBA
= 34;
770 Head
->PartAreaEndLBA
= DiskSectorCount
- 34;
771 CoCreateGuid(&Head
->DiskGuid
);
772 Head
->PartTblStartLBA
= 2;
773 Head
->PartTblTotNum
= 128;
774 Head
->PartTblEntryLen
= 128;
777 memcpy(&(Table
[0].PartType
), &WindowsDataPartType
, sizeof(GUID
));
778 CoCreateGuid(&(Table
[0].PartGuid
));
779 Table
[0].StartLBA
= 2048;
780 Table
[0].LastLBA
= 2048 + Part1SectorCount
- 1;
782 memcpy(Table
[0].Name
, L
"Data", 4 * 2);
785 Head
->PartTblCrc
= VentoyCrc32(Table
, sizeof(pInfo
->PartTbl
));
786 Head
->Crc
= VentoyCrc32(Head
, Head
->Length
);
791 int VentoyFillGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
)
793 INT64 ReservedValue
= 0;
794 UINT64 ModSectorCount
= 0;
795 UINT64 ReservedSector
= 33;
796 UINT64 Part1SectorCount
= 0;
797 UINT64 DiskSectorCount
= DiskSizeBytes
/ 512;
798 VTOY_GPT_HDR
*Head
= &pInfo
->Head
;
799 VTOY_GPT_PART_TBL
*Table
= pInfo
->PartTbl
;
800 static GUID WindowsDataPartType
= { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
801 static GUID EspPartType
= { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
802 static GUID BiosGrubPartType
= { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
804 VentoyFillProtectMBR(DiskSizeBytes
, &pInfo
->MBR
);
806 ReservedValue
= GetReservedSpaceInMB();
807 if (ReservedValue
> 0)
809 ReservedSector
+= ReservedValue
* 2048;
812 Part1SectorCount
= DiskSectorCount
- ReservedSector
- (VENTOY_EFI_PART_SIZE
/ 512) - 2048;
814 ModSectorCount
= (Part1SectorCount
% 8);
817 Log("Part1SectorCount:%llu is not aligned by 4KB (%llu)", (ULONGLONG
)Part1SectorCount
, (ULONGLONG
)ModSectorCount
);
820 // check aligned with 4KB
821 if (IsPartNeed4KBAlign())
825 Log("Disk need to align with 4KB %u", (UINT32
)ModSectorCount
);
826 Part1SectorCount
-= ModSectorCount
;
830 Log("no need to align with 4KB");
834 memcpy(Head
->Signature
, "EFI PART", 8);
835 Head
->Version
[2] = 0x01;
838 Head
->EfiStartLBA
= 1;
839 Head
->EfiBackupLBA
= DiskSectorCount
- 1;
840 Head
->PartAreaStartLBA
= 34;
841 Head
->PartAreaEndLBA
= DiskSectorCount
- 34;
842 CoCreateGuid(&Head
->DiskGuid
);
843 Head
->PartTblStartLBA
= 2;
844 Head
->PartTblTotNum
= 128;
845 Head
->PartTblEntryLen
= 128;
848 memcpy(&(Table
[0].PartType
), &WindowsDataPartType
, sizeof(GUID
));
849 CoCreateGuid(&(Table
[0].PartGuid
));
850 Table
[0].StartLBA
= 2048;
851 Table
[0].LastLBA
= 2048 + Part1SectorCount
- 1;
853 memcpy(Table
[0].Name
, L
"Ventoy", 6 * 2);
855 // to fix windows issue
856 //memcpy(&(Table[1].PartType), &EspPartType, sizeof(GUID));
857 memcpy(&(Table
[1].PartType
), &WindowsDataPartType
, sizeof(GUID
));
858 CoCreateGuid(&(Table
[1].PartGuid
));
859 Table
[1].StartLBA
= Table
[0].LastLBA
+ 1;
860 Table
[1].LastLBA
= Table
[1].StartLBA
+ VENTOY_EFI_PART_SIZE
/ 512 - 1;
861 Table
[1].Attr
= 0xC000000000000001ULL
;
862 memcpy(Table
[1].Name
, L
"VTOYEFI", 7 * 2);
865 memcpy(&(Table
[2].PartType
), &BiosGrubPartType
, sizeof(GUID
));
866 CoCreateGuid(&(Table
[2].PartGuid
));
867 Table
[2].StartLBA
= 34;
868 Table
[2].LastLBA
= 2047;
873 Head
->PartTblCrc
= VentoyCrc32(Table
, sizeof(pInfo
->PartTbl
));
874 Head
->Crc
= VentoyCrc32(Head
, Head
->Length
);
879 int VentoyFillBackupGptHead(VTOY_GPT_INFO
*pInfo
, VTOY_GPT_HDR
*pHead
)
884 memcpy(pHead
, &pInfo
->Head
, sizeof(VTOY_GPT_HDR
));
886 LBA
= pHead
->EfiStartLBA
;
887 BackupLBA
= pHead
->EfiBackupLBA
;
889 pHead
->EfiStartLBA
= BackupLBA
;
890 pHead
->EfiBackupLBA
= LBA
;
891 pHead
->PartTblStartLBA
= BackupLBA
+ 1 - 33;
894 pHead
->Crc
= VentoyCrc32(pHead
, pHead
->Length
);
899 CHAR
GetFirstUnusedDriveLetter(void)
902 DWORD Drives
= GetLogicalDrives();
914 const CHAR
* GetBusTypeString(STORAGE_BUS_TYPE Type
)
918 case BusTypeUnknown
: return "unknown";
919 case BusTypeScsi
: return "SCSI";
920 case BusTypeAtapi
: return "Atapi";
921 case BusTypeAta
: return "ATA";
922 case BusType1394
: return "1394";
923 case BusTypeSsa
: return "SSA";
924 case BusTypeFibre
: return "Fibre";
925 case BusTypeUsb
: return "USB";
926 case BusTypeRAID
: return "RAID";
927 case BusTypeiScsi
: return "iSCSI";
928 case BusTypeSas
: return "SAS";
929 case BusTypeSata
: return "SATA";
930 case BusTypeSd
: return "SD";
931 case BusTypeMmc
: return "MMC";
932 case BusTypeVirtual
: return "Virtual";
933 case BusTypeFileBackedVirtual
: return "FileBackedVirtual";
934 case BusTypeSpaces
: return "Spaces";
935 case BusTypeNvme
: return "Nvme";
940 int VentoyGetLocalBootImg(MBR_HEAD
*pMBR
)
944 static int Loaded
= 0;
949 memcpy(pMBR
, &MBR
, 512);
953 if (0 == ReadWholeFileToBuf(VENTOY_FILE_BOOT_IMG
, 0, (void **)&ImgBuf
, &Len
))
955 Log("Copy boot img success");
956 memcpy(pMBR
, ImgBuf
, 512);
959 CoCreateGuid((GUID
*)(pMBR
->BootCode
+ 0x180));
961 memcpy(&MBR
, pMBR
, 512);
968 Log("Copy boot img failed");
973 int GetHumanReadableGBSize(UINT64 SizeBytes
)
978 double GB
= SizeBytes
* 1.0 / 1000 / 1000 / 1000;
980 if ((SizeBytes
% 1073741824) == 0)
982 return (int)(SizeBytes
/ 1073741824);
985 for (i
= 0; i
< 12; i
++)
989 Delta
= (Pow2
- GB
) / Pow2
;
993 Delta
= (GB
- Pow2
) / Pow2
;
1007 void TrimString(CHAR
*String
)
1009 CHAR
*Pos1
= String
;
1010 CHAR
*Pos2
= String
;
1011 size_t Len
= strlen(String
);
1015 if (String
[Len
- 1] != ' ' && String
[Len
- 1] != '\t')
1019 String
[Len
- 1] = 0;
1023 while (*Pos1
== ' ' || *Pos1
== '\t')
1037 int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
)
1045 lRet
= RegOpenKeyExA(Key
, SubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1046 Log("RegOpenKeyExA <%s> Ret:%ld", SubKey
, lRet
);
1048 if (ERROR_SUCCESS
== lRet
)
1050 Size
= sizeof(Value
);
1051 lRet
= RegQueryValueExA(hKey
, ValueName
, NULL
, &Type
, (LPBYTE
)&Value
, &Size
);
1052 Log("RegQueryValueExA <%s> ret:%u Size:%u Value:%u", ValueName
, lRet
, Size
, Value
);
1065 int GetPhysicalDriveCount(void)
1070 if (GetRegDwordValue(HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Services\\disk\\Enum", "Count", &Value
) == 0)
1075 Log("GetPhysicalDriveCount: %d", Count
);