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 const char* GUID2String(void *guid
, char *buf
, int len
)
78 GUID
* pGUID
= (GUID
*)guid
;
79 sprintf_s(buf
, len
, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
80 pGUID
->Data1
, pGUID
->Data2
, pGUID
->Data3
,
81 pGUID
->Data4
[0], pGUID
->Data4
[1],
82 pGUID
->Data4
[2], pGUID
->Data4
[3], pGUID
->Data4
[4], pGUID
->Data4
[5], pGUID
->Data4
[6], pGUID
->Data4
[7]
87 BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...)
92 CHAR FilePath
[MAX_PATH
];
95 vsnprintf_s(FilePath
, sizeof(FilePath
), sizeof(FilePath
), Fmt
, Arg
);
98 hFile
= CreateFileA(FilePath
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
99 if (INVALID_HANDLE_VALUE
== hFile
)
106 Attr
= GetFileAttributesA(FilePath
);
110 if ((Attr
& FILE_ATTRIBUTE_DIRECTORY
) == 0)
117 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
126 int SaveBufToFile(const CHAR
*FileName
, const void *Buffer
, int BufLen
)
131 fopen_s(&File
, FileName
, "wb");
134 Log("Failed to open file %s", FileName
);
138 fwrite(Buffer
, 1, BufLen
, File
);
143 int ReadWholeFileToBuf(const CHAR
*FileName
, int ExtLen
, void **Bufer
, int *BufLen
)
149 fopen_s(&File
, FileName
, "rb");
152 Log("Failed to open file %s", FileName
);
156 fseek(File
, 0, SEEK_END
);
157 FileSize
= (int)ftell(File
);
159 Data
= malloc(FileSize
+ ExtLen
);
166 fseek(File
, 0, SEEK_SET
);
167 fread(Data
, 1, FileSize
, File
);
177 const CHAR
* GetLocalVentoyVersion(void)
183 static CHAR LocalVersion
[64] = { 0 };
185 if (LocalVersion
[0] == 0)
187 rc
= ReadWholeFileToBuf(VENTOY_FILE_VERSION
, 1, (void **)&Buf
, &FileSize
);
194 for (Pos
= Buf
; *Pos
; Pos
++)
196 if (*Pos
== '\r' || *Pos
== '\n')
203 safe_sprintf(LocalVersion
, "%s", Buf
);
210 const CHAR
* ParseVentoyVersionFromString(CHAR
*Buf
)
214 static CHAR LocalVersion
[64] = { 0 };
216 Pos
= strstr(Buf
, "VENTOY_VERSION=");
219 Pos
+= strlen("VENTOY_VERSION=");
226 while (*End
!= 0 && *End
!= '"' && *End
!= '\r' && *End
!= '\n')
233 safe_sprintf(LocalVersion
, "%s", Pos
);
242 typedef BOOL(WINAPI
*LPFN_ISWOW64PROCESS
)(HANDLE
, PBOOL
);
243 LPFN_ISWOW64PROCESS fnIsWow64Process
;
244 BOOL bIsWow64
= FALSE
;
245 CHAR Wow64Dir
[MAX_PATH
];
247 fnIsWow64Process
= (LPFN_ISWOW64PROCESS
)GetProcAddress(GetModuleHandleA("kernel32"), "IsWow64Process");
248 if (NULL
!= fnIsWow64Process
)
250 fnIsWow64Process(GetCurrentProcess(), &bIsWow64
);
255 if (GetSystemWow64DirectoryA(Wow64Dir
, sizeof(Wow64Dir
)))
257 Log("GetSystemWow64DirectoryA=<%s>", Wow64Dir
);
266 * Some code and functions in the file are copied from rufus.
267 * https://github.com/pbatard/rufus
270 /* Windows versions */
271 enum WindowsVersion
{
272 WINDOWS_UNDEFINED
= -1,
273 WINDOWS_UNSUPPORTED
= 0,
275 WINDOWS_2003
= 0x52, // Also XP_64
276 WINDOWS_VISTA
= 0x60, // Also Server 2008
277 WINDOWS_7
= 0x61, // Also Server 2008_R2
278 WINDOWS_8
= 0x62, // Also Server 2012
279 WINDOWS_8_1
= 0x63, // Also Server 2012_R2
280 WINDOWS_10_PREVIEW1
= 0x64,
281 WINDOWS_10
= 0xA0, // Also Server 2016, also Server 2019
282 WINDOWS_11
= 0xB0, // Also Server 2022
286 static const char* GetEdition(DWORD ProductType
)
288 // From: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getproductinfo
289 // These values can be found in the winnt.h header.
290 switch (ProductType
) {
291 case 0x00000000: return ""; // Undefined
292 case 0x00000001: return "Ultimate";
293 case 0x00000002: return "Home Basic";
294 case 0x00000003: return "Home Premium";
295 case 0x00000004: return "Enterprise";
296 case 0x00000005: return "Home Basic N";
297 case 0x00000006: return "Business";
298 case 0x00000007: return "Server Standard";
299 case 0x00000008: return "Server Datacenter";
300 case 0x00000009: return "Smallbusiness Server";
301 case 0x0000000A: return "Server Enterprise";
302 case 0x0000000B: return "Starter";
303 case 0x0000000C: return "Server Datacenter (Core)";
304 case 0x0000000D: return "Server Standard (Core)";
305 case 0x0000000E: return "Server Enterprise (Core)";
306 case 0x00000010: return "Business N";
307 case 0x00000011: return "Web Server";
308 case 0x00000012: return "HPC Edition";
309 case 0x00000013: return "Storage Server (Essentials)";
310 case 0x0000001A: return "Home Premium N";
311 case 0x0000001B: return "Enterprise N";
312 case 0x0000001C: return "Ultimate N";
313 case 0x00000022: return "Home Server";
314 case 0x00000024: return "Server Standard without Hyper-V";
315 case 0x00000025: return "Server Datacenter without Hyper-V";
316 case 0x00000026: return "Server Enterprise without Hyper-V";
317 case 0x00000027: return "Server Datacenter without Hyper-V (Core)";
318 case 0x00000028: return "Server Standard without Hyper-V (Core)";
319 case 0x00000029: return "Server Enterprise without Hyper-V (Core)";
320 case 0x0000002A: return "Hyper-V Server";
321 case 0x0000002F: return "Starter N";
322 case 0x00000030: return "Pro";
323 case 0x00000031: return "Pro N";
324 case 0x00000034: return "Server Solutions Premium";
325 case 0x00000035: return "Server Solutions Premium (Core)";
326 case 0x00000040: return "Server Hyper Core V";
327 case 0x00000042: return "Starter E";
328 case 0x00000043: return "Home Basic E";
329 case 0x00000044: return "Premium E";
330 case 0x00000045: return "Pro E";
331 case 0x00000046: return "Enterprise E";
332 case 0x00000047: return "Ultimate E";
333 case 0x00000048: return "Enterprise (Eval)";
334 case 0x0000004F: return "Server Standard (Eval)";
335 case 0x00000050: return "Server Datacenter (Eval)";
336 case 0x00000054: return "Enterprise N (Eval)";
337 case 0x00000057: return "Thin PC";
338 case 0x00000058: case 0x00000059: case 0x0000005A: case 0x0000005B: case 0x0000005C: return "Embedded";
339 case 0x00000062: return "Home N";
340 case 0x00000063: return "Home China";
341 case 0x00000064: return "Home Single Language";
342 case 0x00000065: return "Home";
343 case 0x00000067: return "Pro with Media Center";
344 case 0x00000069: case 0x0000006A: case 0x0000006B: case 0x0000006C: return "Embedded";
345 case 0x0000006F: return "Home Connected";
346 case 0x00000070: return "Pro Student";
347 case 0x00000071: return "Home Connected N";
348 case 0x00000072: return "Pro Student N";
349 case 0x00000073: return "Home Connected Single Language";
350 case 0x00000074: return "Home Connected China";
351 case 0x00000079: return "Education";
352 case 0x0000007A: return "Education N";
353 case 0x0000007D: return "Enterprise LTSB";
354 case 0x0000007E: return "Enterprise LTSB N";
355 case 0x0000007F: return "Pro S";
356 case 0x00000080: return "Pro S N";
357 case 0x00000081: return "Enterprise LTSB (Eval)";
358 case 0x00000082: return "Enterprise LTSB N (Eval)";
359 case 0x0000008A: return "Pro Single Language";
360 case 0x0000008B: return "Pro China";
361 case 0x0000008C: return "Enterprise Subscription";
362 case 0x0000008D: return "Enterprise Subscription N";
363 case 0x00000091: return "Server Datacenter SA (Core)";
364 case 0x00000092: return "Server Standard SA (Core)";
365 case 0x00000095: return "Utility VM";
366 case 0x000000A1: return "Pro for Workstations";
367 case 0x000000A2: return "Pro for Workstations N";
368 case 0x000000A4: return "Pro for Education";
369 case 0x000000A5: return "Pro for Education N";
370 case 0x000000AB: return "Enterprise G"; // I swear Microsoft are just making up editions...
371 case 0x000000AC: return "Enterprise G N";
372 case 0x000000B6: return "Home OS";
373 case 0x000000B7: return "Cloud E";
374 case 0x000000B8: return "Cloud E N";
375 case 0x000000BD: return "Lite";
376 case 0xABCDABCD: return "(Unlicensed)";
377 default: return "(Unknown Edition)";
381 #define is_x64 IsWow64
382 #define static_strcpy safe_strcpy
383 #define REGKEY_HKCU HKEY_CURRENT_USER
384 #define REGKEY_HKLM HKEY_LOCAL_MACHINE
385 static int nWindowsVersion
= WINDOWS_UNDEFINED
;
386 static int nWindowsBuildNumber
= -1;
387 static char WindowsVersionStr
[128] = "";
389 /* Helpers for 32 bit registry operations */
392 * Read a generic registry key value. If a short key_name is used, assume that
393 * it belongs to the application and create the app subkey if required
395 static __inline BOOL
_GetRegistryKey(HKEY key_root
, const char* key_name
, DWORD reg_type
,
396 LPBYTE dest
, DWORD dest_size
)
398 const char software_prefix
[] = "SOFTWARE\\";
399 char long_key_name
[MAX_PATH
] = { 0 };
403 HKEY hSoftware
= NULL
, hApp
= NULL
;
404 DWORD dwType
= -1, dwSize
= dest_size
;
406 memset(dest
, 0, dest_size
);
408 if (key_name
== NULL
)
411 for (i
= strlen(key_name
); i
>0; i
--) {
412 if (key_name
[i
] == '\\')
417 // Prefix with "SOFTWARE" if needed
418 if (_strnicmp(key_name
, software_prefix
, sizeof(software_prefix
)-1) != 0) {
419 if (i
+ sizeof(software_prefix
) >= sizeof(long_key_name
))
421 strcpy_s(long_key_name
, sizeof(long_key_name
), software_prefix
);
422 strcat_s(long_key_name
, sizeof(long_key_name
), key_name
);
423 long_key_name
[sizeof(software_prefix
)+i
- 1] = 0;
426 if (i
>= sizeof(long_key_name
))
428 static_strcpy(long_key_name
, key_name
);
429 long_key_name
[i
] = 0;
432 if (RegOpenKeyExA(key_root
, long_key_name
, 0, KEY_READ
, &hApp
) != ERROR_SUCCESS
) {
438 if (RegOpenKeyExA(key_root
, "SOFTWARE", 0, KEY_READ
| KEY_CREATE_SUB_KEY
, &hSoftware
) != ERROR_SUCCESS
) {
444 s
= RegQueryValueExA(hApp
, &key_name
[i
], NULL
, &dwType
, (LPBYTE
)dest
, &dwSize
);
445 // No key means default value of 0 or empty string
446 if ((s
== ERROR_FILE_NOT_FOUND
) || ((s
== ERROR_SUCCESS
) && (dwType
== reg_type
) && (dwSize
> 0))) {
450 if (hSoftware
!= NULL
)
451 RegCloseKey(hSoftware
);
457 #define GetRegistryKey32(root, key, pval) _GetRegistryKey(root, key, REG_DWORD, (LPBYTE)pval, sizeof(DWORD))
458 static __inline INT32
ReadRegistryKey32(HKEY root
, const char* key
) {
460 GetRegistryKey32(root
, key
, &val
);
465 * Modified from smartmontools' os_win32.cpp
467 void GetWindowsVersion(void)
469 OSVERSIONINFOEXA vi
, vi2
;
472 const char* w64
= "32 bit";
475 unsigned major
, minor
;
476 ULONGLONG major_equal
, minor_equal
;
479 nWindowsVersion
= WINDOWS_UNDEFINED
;
480 static_strcpy(WindowsVersionStr
, "Windows Undefined");
482 // suppress the C4996 warning for GetVersionExA
483 #pragma warning(push)
484 #pragma warning(disable:4996)
486 memset(&vi
, 0, sizeof(vi
));
487 vi
.dwOSVersionInfoSize
= sizeof(vi
);
488 if (!GetVersionExA((OSVERSIONINFOA
*)&vi
)) {
489 memset(&vi
, 0, sizeof(vi
));
490 vi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOA
);
491 if (!GetVersionExA((OSVERSIONINFOA
*)&vi
))
497 if (vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
499 if (vi
.dwMajorVersion
> 6 || (vi
.dwMajorVersion
== 6 && vi
.dwMinorVersion
>= 2)) {
500 // Starting with Windows 8.1 Preview, GetVersionEx() does no longer report the actual OS version
501 // See: http://msdn.microsoft.com/en-us/library/windows/desktop/dn302074.aspx
502 // And starting with Windows 10 Preview 2, Windows enforces the use of the application/supportedOS
503 // manifest in order for VerSetConditionMask() to report the ACTUAL OS major and minor...
505 major_equal
= VerSetConditionMask(0, VER_MAJORVERSION
, VER_EQUAL
);
506 for (major
= vi
.dwMajorVersion
; major
<= 9; major
++) {
507 memset(&vi2
, 0, sizeof(vi2
));
508 vi2
.dwOSVersionInfoSize
= sizeof(vi2
); vi2
.dwMajorVersion
= major
;
509 if (!VerifyVersionInfoA(&vi2
, VER_MAJORVERSION
, major_equal
))
511 if (vi
.dwMajorVersion
< major
) {
512 vi
.dwMajorVersion
= major
; vi
.dwMinorVersion
= 0;
515 minor_equal
= VerSetConditionMask(0, VER_MINORVERSION
, VER_EQUAL
);
516 for (minor
= vi
.dwMinorVersion
; minor
<= 9; minor
++) {
517 memset(&vi2
, 0, sizeof(vi2
)); vi2
.dwOSVersionInfoSize
= sizeof(vi2
);
518 vi2
.dwMinorVersion
= minor
;
519 if (!VerifyVersionInfoA(&vi2
, VER_MINORVERSION
, minor_equal
))
521 vi
.dwMinorVersion
= minor
;
529 if (vi
.dwMajorVersion
<= 0xf && vi
.dwMinorVersion
<= 0xf) {
530 ws
= (vi
.wProductType
<= VER_NT_WORKSTATION
);
531 nWindowsVersion
= vi
.dwMajorVersion
<< 4 | vi
.dwMinorVersion
;
532 switch (nWindowsVersion
) {
533 case WINDOWS_XP
: w
= "XP";
535 case WINDOWS_2003
: w
= (ws
? "XP_64" : (!GetSystemMetrics(89) ? "Server 2003" : "Server 2003_R2"));
537 case WINDOWS_VISTA
: w
= (ws
? "Vista" : "Server 2008");
539 case WINDOWS_7
: w
= (ws
? "7" : "Server 2008_R2");
541 case WINDOWS_8
: w
= (ws
? "8" : "Server 2012");
543 case WINDOWS_8_1
: w
= (ws
? "8.1" : "Server 2012_R2");
545 case WINDOWS_10_PREVIEW1
: w
= (ws
? "10 (Preview 1)" : "Server 10 (Preview 1)");
547 // Starting with Windows 10 Preview 2, the major is the same as the public-facing version
549 if (vi
.dwBuildNumber
< 20000) {
550 w
= (ws
? "10" : ((vi
.dwBuildNumber
< 17763) ? "Server 2016" : "Server 2019"));
553 nWindowsVersion
= WINDOWS_11
;
555 case WINDOWS_11
: w
= (ws
? "11" : "Server 2022");
558 if (nWindowsVersion
< WINDOWS_XP
)
559 nWindowsVersion
= WINDOWS_UNSUPPORTED
;
570 GetProductInfo(vi
.dwMajorVersion
, vi
.dwMinorVersion
, vi
.wServicePackMajor
, vi
.wServicePackMinor
, &dwProductType
);
571 vptr
= WindowsVersionStr
;
572 vlen
= sizeof(WindowsVersionStr
) - 1;
575 sprintf_s(vptr
, vlen
, "%s %u.%u %s", (vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
? "NT" : "??"),
576 (unsigned)vi
.dwMajorVersion
, (unsigned)vi
.dwMinorVersion
, w64
);
577 else if (vi
.wServicePackMinor
)
578 sprintf_s(vptr
, vlen
, "%s SP%u.%u %s", w
, vi
.wServicePackMajor
, vi
.wServicePackMinor
, w64
);
579 else if (vi
.wServicePackMajor
)
580 sprintf_s(vptr
, vlen
, "%s SP%u %s", w
, vi
.wServicePackMajor
, w64
);
582 sprintf_s(vptr
, vlen
, "%s%s%s, %s",
583 w
, (dwProductType
!= PRODUCT_UNDEFINED
) ? " " : "", GetEdition(dwProductType
), w64
);
585 // Add the build number (including UBR if available) for Windows 8.0 and later
586 nWindowsBuildNumber
= vi
.dwBuildNumber
;
587 if (nWindowsVersion
>= 0x62) {
588 int nUbr
= ReadRegistryKey32(REGKEY_HKLM
, "Software\\Microsoft\\Windows NT\\CurrentVersion\\UBR");
589 vptr
= WindowsVersionStr
+ strlen(WindowsVersionStr
);
590 vlen
= sizeof(WindowsVersionStr
) - strlen(WindowsVersionStr
) - 1;
592 sprintf_s(vptr
, vlen
, " (Build %d.%d)", nWindowsBuildNumber
, nUbr
);
594 sprintf_s(vptr
, vlen
, " (Build %d)", nWindowsBuildNumber
);
600 void DumpWindowsVersion(void)
603 Log("Windows Version: <<Windows %s>>", WindowsVersionStr
);
608 BOOL
IsVentoyLogicalDrive(CHAR DriveLetter
)
611 CONST CHAR
*Files
[] =
613 "EFI\\BOOT\\BOOTX64.EFI",
614 "grub\\themes\\ventoy\\theme.txt",
615 "ventoy\\ventoy.cpio",
618 for (i
= 0; i
< sizeof(Files
) / sizeof(Files
[0]); i
++)
620 if (!IsFileExist("%C:\\%s", DriveLetter
, Files
[i
]))
630 int VentoyFillMBRLocation(UINT64 DiskSizeInBytes
, UINT32 StartSectorId
, UINT32 SectorCount
, PART_TABLE
*Table
)
639 while (nHead
!= 0 && (DiskSizeInBytes
/ 512 / nSector
/ nHead
) > 1024)
641 nHead
= (BYTE
)nHead
* 2;
649 Cylinder
= StartSectorId
/ nSector
/ nHead
;
650 Head
= StartSectorId
/ nSector
% nHead
;
651 Sector
= StartSectorId
% nSector
+ 1;
653 Table
->StartHead
= Head
;
654 Table
->StartSector
= Sector
;
655 Table
->StartCylinder
= Cylinder
;
657 EndSectorId
= StartSectorId
+ SectorCount
- 1;
658 Cylinder
= EndSectorId
/ nSector
/ nHead
;
659 Head
= EndSectorId
/ nSector
% nHead
;
660 Sector
= EndSectorId
% nSector
+ 1;
662 Table
->EndHead
= Head
;
663 Table
->EndSector
= Sector
;
664 Table
->EndCylinder
= Cylinder
;
666 Table
->StartSectorId
= StartSectorId
;
667 Table
->SectorCount
= SectorCount
;
672 int VentoyFillMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
, int PartStyle
, UINT8 FsFlag
)
676 UINT32 DiskSignature
;
677 UINT32 DiskSectorCount
;
678 UINT32 PartSectorCount
;
679 UINT32 PartStartSector
;
680 UINT32 ReservedSector
;
682 VentoyGetLocalBootImg(pMBR
);
686 memcpy(&DiskSignature
, &Guid
, sizeof(UINT32
));
688 Log("Disk signature: 0x%08x", DiskSignature
);
690 *((UINT32
*)(pMBR
->BootCode
+ 0x1B8)) = DiskSignature
;
691 memcpy(pMBR
->BootCode
+ 0x180, &Guid
, 16);
693 if (DiskSizeBytes
/ 512 > 0xFFFFFFFF)
695 DiskSectorCount
= 0xFFFFFFFF;
699 DiskSectorCount
= (UINT32
)(DiskSizeBytes
/ 512);
702 ReservedValue
= GetReservedSpaceInMB();
703 if (ReservedValue
<= 0)
709 ReservedSector
= (UINT32
)(ReservedValue
* 2048);
714 ReservedSector
+= 33; // backup GPT part table
717 // check aligned with 4KB
718 if (IsPartNeed4KBAlign())
720 UINT64 sectors
= DiskSizeBytes
/ 512;
723 Log("Disk need to align with 4KB %u", (UINT32
)(sectors
% 8));
724 ReservedSector
+= (UINT32
)(sectors
% 8);
728 Log("ReservedSector: %u", ReservedSector
);
731 PartStartSector
= VENTOY_PART1_START_SECTOR
;
732 PartSectorCount
= DiskSectorCount
- ReservedSector
- VENTOY_EFI_PART_SIZE
/ 512 - PartStartSector
;
733 VentoyFillMBRLocation(DiskSizeBytes
, PartStartSector
, PartSectorCount
, pMBR
->PartTbl
);
735 pMBR
->PartTbl
[0].Active
= 0x80; // bootable
736 pMBR
->PartTbl
[0].FsFlag
= FsFlag
; // File system flag 07:exFAT/NTFS/HPFS 0C:FAT32
739 PartStartSector
+= PartSectorCount
;
740 PartSectorCount
= VENTOY_EFI_PART_SIZE
/ 512;
741 VentoyFillMBRLocation(DiskSizeBytes
, PartStartSector
, PartSectorCount
, pMBR
->PartTbl
+ 1);
743 pMBR
->PartTbl
[1].Active
= 0x00;
744 pMBR
->PartTbl
[1].FsFlag
= 0xEF; // EFI System Partition
753 static int VentoyFillProtectMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
)
756 UINT32 DiskSignature
;
757 UINT64 DiskSectorCount
;
759 VentoyGetLocalBootImg(pMBR
);
763 memcpy(&DiskSignature
, &Guid
, sizeof(UINT32
));
765 Log("Disk signature: 0x%08x", DiskSignature
);
767 *((UINT32
*)(pMBR
->BootCode
+ 0x1B8)) = DiskSignature
;
768 memcpy(pMBR
->BootCode
+ 0x180, &Guid
, 16);
770 DiskSectorCount
= DiskSizeBytes
/ 512 - 1;
771 if (DiskSectorCount
> 0xFFFFFFFF)
773 DiskSectorCount
= 0xFFFFFFFF;
776 memset(pMBR
->PartTbl
, 0, sizeof(pMBR
->PartTbl
));
778 pMBR
->PartTbl
[0].Active
= 0x00;
779 pMBR
->PartTbl
[0].FsFlag
= 0xee; // EE
781 pMBR
->PartTbl
[0].StartHead
= 0;
782 pMBR
->PartTbl
[0].StartSector
= 1;
783 pMBR
->PartTbl
[0].StartCylinder
= 0;
784 pMBR
->PartTbl
[0].EndHead
= 254;
785 pMBR
->PartTbl
[0].EndSector
= 63;
786 pMBR
->PartTbl
[0].EndCylinder
= 1023;
788 pMBR
->PartTbl
[0].StartSectorId
= 1;
789 pMBR
->PartTbl
[0].SectorCount
= (UINT32
)DiskSectorCount
;
794 pMBR
->BootCode
[92] = 0x22;
799 int VentoyFillWholeGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
)
801 UINT64 Part1SectorCount
= 0;
802 UINT64 DiskSectorCount
= DiskSizeBytes
/ 512;
803 VTOY_GPT_HDR
*Head
= &pInfo
->Head
;
804 VTOY_GPT_PART_TBL
*Table
= pInfo
->PartTbl
;
805 static GUID WindowsDataPartType
= { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
807 VentoyFillProtectMBR(DiskSizeBytes
, &pInfo
->MBR
);
809 Part1SectorCount
= DiskSectorCount
- 33 - 2048;
811 memcpy(Head
->Signature
, "EFI PART", 8);
812 Head
->Version
[2] = 0x01;
815 Head
->EfiStartLBA
= 1;
816 Head
->EfiBackupLBA
= DiskSectorCount
- 1;
817 Head
->PartAreaStartLBA
= 34;
818 Head
->PartAreaEndLBA
= DiskSectorCount
- 34;
819 CoCreateGuid(&Head
->DiskGuid
);
820 Head
->PartTblStartLBA
= 2;
821 Head
->PartTblTotNum
= 128;
822 Head
->PartTblEntryLen
= 128;
825 memcpy(&(Table
[0].PartType
), &WindowsDataPartType
, sizeof(GUID
));
826 CoCreateGuid(&(Table
[0].PartGuid
));
827 Table
[0].StartLBA
= 2048;
828 Table
[0].LastLBA
= 2048 + Part1SectorCount
- 1;
830 memcpy(Table
[0].Name
, L
"Data", 4 * 2);
833 Head
->PartTblCrc
= VentoyCrc32(Table
, sizeof(pInfo
->PartTbl
));
834 Head
->Crc
= VentoyCrc32(Head
, Head
->Length
);
839 int VentoyFillGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
)
841 INT64 ReservedValue
= 0;
842 UINT64 ModSectorCount
= 0;
843 UINT64 ReservedSector
= 33;
844 UINT64 Part1SectorCount
= 0;
845 UINT64 DiskSectorCount
= DiskSizeBytes
/ 512;
846 VTOY_GPT_HDR
*Head
= &pInfo
->Head
;
847 VTOY_GPT_PART_TBL
*Table
= pInfo
->PartTbl
;
848 static GUID WindowsDataPartType
= { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
849 static GUID EspPartType
= { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
850 static GUID BiosGrubPartType
= { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
852 VentoyFillProtectMBR(DiskSizeBytes
, &pInfo
->MBR
);
854 ReservedValue
= GetReservedSpaceInMB();
855 if (ReservedValue
> 0)
857 ReservedSector
+= ReservedValue
* 2048;
860 Part1SectorCount
= DiskSectorCount
- ReservedSector
- (VENTOY_EFI_PART_SIZE
/ 512) - 2048;
862 ModSectorCount
= (Part1SectorCount
% 8);
865 Log("Part1SectorCount:%llu is not aligned by 4KB (%llu)", (ULONGLONG
)Part1SectorCount
, (ULONGLONG
)ModSectorCount
);
868 // check aligned with 4KB
869 if (IsPartNeed4KBAlign())
873 Log("Disk need to align with 4KB %u", (UINT32
)ModSectorCount
);
874 Part1SectorCount
-= ModSectorCount
;
878 Log("no need to align with 4KB");
882 memcpy(Head
->Signature
, "EFI PART", 8);
883 Head
->Version
[2] = 0x01;
886 Head
->EfiStartLBA
= 1;
887 Head
->EfiBackupLBA
= DiskSectorCount
- 1;
888 Head
->PartAreaStartLBA
= 34;
889 Head
->PartAreaEndLBA
= DiskSectorCount
- 34;
890 CoCreateGuid(&Head
->DiskGuid
);
891 Head
->PartTblStartLBA
= 2;
892 Head
->PartTblTotNum
= 128;
893 Head
->PartTblEntryLen
= 128;
896 memcpy(&(Table
[0].PartType
), &WindowsDataPartType
, sizeof(GUID
));
897 CoCreateGuid(&(Table
[0].PartGuid
));
898 Table
[0].StartLBA
= 2048;
899 Table
[0].LastLBA
= 2048 + Part1SectorCount
- 1;
901 memcpy(Table
[0].Name
, L
"Ventoy", 6 * 2);
903 // to fix windows issue
904 //memcpy(&(Table[1].PartType), &EspPartType, sizeof(GUID));
905 memcpy(&(Table
[1].PartType
), &WindowsDataPartType
, sizeof(GUID
));
906 CoCreateGuid(&(Table
[1].PartGuid
));
907 Table
[1].StartLBA
= Table
[0].LastLBA
+ 1;
908 Table
[1].LastLBA
= Table
[1].StartLBA
+ VENTOY_EFI_PART_SIZE
/ 512 - 1;
909 Table
[1].Attr
= 0xC000000000000001ULL
;
910 memcpy(Table
[1].Name
, L
"VTOYEFI", 7 * 2);
913 memcpy(&(Table
[2].PartType
), &BiosGrubPartType
, sizeof(GUID
));
914 CoCreateGuid(&(Table
[2].PartGuid
));
915 Table
[2].StartLBA
= 34;
916 Table
[2].LastLBA
= 2047;
921 Head
->PartTblCrc
= VentoyCrc32(Table
, sizeof(pInfo
->PartTbl
));
922 Head
->Crc
= VentoyCrc32(Head
, Head
->Length
);
927 int VentoyFillBackupGptHead(VTOY_GPT_INFO
*pInfo
, VTOY_GPT_HDR
*pHead
)
932 memcpy(pHead
, &pInfo
->Head
, sizeof(VTOY_GPT_HDR
));
934 LBA
= pHead
->EfiStartLBA
;
935 BackupLBA
= pHead
->EfiBackupLBA
;
937 pHead
->EfiStartLBA
= BackupLBA
;
938 pHead
->EfiBackupLBA
= LBA
;
939 pHead
->PartTblStartLBA
= BackupLBA
+ 1 - 33;
942 pHead
->Crc
= VentoyCrc32(pHead
, pHead
->Length
);
947 CHAR
GetFirstUnusedDriveLetter(void)
950 DWORD Drives
= GetLogicalDrives();
962 const CHAR
* GetBusTypeString(STORAGE_BUS_TYPE Type
)
966 case BusTypeUnknown
: return "unknown";
967 case BusTypeScsi
: return "SCSI";
968 case BusTypeAtapi
: return "Atapi";
969 case BusTypeAta
: return "ATA";
970 case BusType1394
: return "1394";
971 case BusTypeSsa
: return "SSA";
972 case BusTypeFibre
: return "Fibre";
973 case BusTypeUsb
: return "USB";
974 case BusTypeRAID
: return "RAID";
975 case BusTypeiScsi
: return "iSCSI";
976 case BusTypeSas
: return "SAS";
977 case BusTypeSata
: return "SATA";
978 case BusTypeSd
: return "SD";
979 case BusTypeMmc
: return "MMC";
980 case BusTypeVirtual
: return "Virtual";
981 case BusTypeFileBackedVirtual
: return "FileBackedVirtual";
982 case BusTypeSpaces
: return "Spaces";
983 case BusTypeNvme
: return "Nvme";
988 int VentoyGetLocalBootImg(MBR_HEAD
*pMBR
)
992 static int Loaded
= 0;
997 memcpy(pMBR
, &MBR
, 512);
1001 if (0 == ReadWholeFileToBuf(VENTOY_FILE_BOOT_IMG
, 0, (void **)&ImgBuf
, &Len
))
1003 Log("Copy boot img success");
1004 memcpy(pMBR
, ImgBuf
, 512);
1007 CoCreateGuid((GUID
*)(pMBR
->BootCode
+ 0x180));
1009 memcpy(&MBR
, pMBR
, 512);
1016 Log("Copy boot img failed");
1021 int GetHumanReadableGBSize(UINT64 SizeBytes
)
1026 double GB
= SizeBytes
* 1.0 / 1000 / 1000 / 1000;
1028 if ((SizeBytes
% 1073741824) == 0)
1030 return (int)(SizeBytes
/ 1073741824);
1033 for (i
= 0; i
< 12; i
++)
1037 Delta
= (Pow2
- GB
) / Pow2
;
1041 Delta
= (GB
- Pow2
) / Pow2
;
1055 void TrimString(CHAR
*String
)
1057 CHAR
*Pos1
= String
;
1058 CHAR
*Pos2
= String
;
1059 size_t Len
= strlen(String
);
1063 if (String
[Len
- 1] != ' ' && String
[Len
- 1] != '\t')
1067 String
[Len
- 1] = 0;
1071 while (*Pos1
== ' ' || *Pos1
== '\t')
1085 int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
)
1093 lRet
= RegOpenKeyExA(Key
, SubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1094 Log("RegOpenKeyExA <%s> Ret:%ld", SubKey
, lRet
);
1096 if (ERROR_SUCCESS
== lRet
)
1098 Size
= sizeof(Value
);
1099 lRet
= RegQueryValueExA(hKey
, ValueName
, NULL
, &Type
, (LPBYTE
)&Value
, &Size
);
1100 Log("RegQueryValueExA <%s> ret:%u Size:%u Value:%u", ValueName
, lRet
, Size
, Value
);
1113 int GetPhysicalDriveCount(void)
1118 if (GetRegDwordValue(HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Services\\disk\\Enum", "Count", &Value
) == 0)
1123 Log("GetPhysicalDriveCount: %d", Count
);
1127 void VentoyStringToUpper(CHAR
* str
)
1131 if (*str
>= 'a' && *str
<= 'z')
1133 *str
= toupper(*str
);