1 /******************************************************************************
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <VersionHelpers.h>
29 #include "fat_filelib.h"
31 static ventoy_os_param g_os_param
;
32 static ventoy_windows_data g_windows_data
;
33 static UINT8 g_os_param_reserved
[32];
34 static INT g_system_bit
= VTOY_BIT
;
35 static ventoy_guid g_ventoy_guid
= VENTOY_GUID
;
36 static HANDLE g_vtoylog_mutex
= NULL
;
37 static HANDLE g_vtoyins_mutex
= NULL
;
39 static BOOL g_wimboot_mode
= FALSE
;
41 static DWORD g_vtoy_disk_drive
;
43 static CHAR g_prog_full_path
[MAX_PATH
];
44 static CHAR g_prog_dir
[MAX_PATH
];
45 static CHAR g_prog_name
[MAX_PATH
];
47 #define VTOY_PECMD_PATH "X:\\Windows\\system32\\ventoy\\PECMD.EXE"
48 #define ORG_PECMD_PATH "X:\\Windows\\system32\\PECMD.EXE"
49 #define ORG_PECMD_BK_PATH "X:\\Windows\\system32\\VTOYJUMP.EXE"
51 #define WIMBOOT_FILE "X:\\Windows\\system32\\vtoy_wimboot"
52 #define WIMBOOT_DONE "X:\\Windows\\system32\\vtoy_wimboot_done"
54 #define AUTO_RUN_BAT "X:\\VentoyAutoRun.bat"
55 #define AUTO_RUN_LOG "X:\\VentoyAutoRun.log"
57 #define VTOY_AUTO_FILE "X:\\_vtoy_auto_install"
59 #define LOG_FILE "X:\\Windows\\system32\\ventoy.log"
60 #define MUTEX_LOCK(hmutex) if (hmutex != NULL) LockStatus = WaitForSingleObject(hmutex, INFINITE)
61 #define MUTEX_UNLOCK(hmutex) if (hmutex != NULL && WAIT_OBJECT_0 == LockStatus) ReleaseMutex(hmutex)
63 static const char * GetFileNameInPath(const char *fullpath
)
67 if (strstr(fullpath
, ":"))
69 for (i
= (int)strlen(fullpath
); i
> 0; i
--)
71 if (fullpath
[i
- 1] == '/' || fullpath
[i
- 1] == '\\')
81 static int split_path_name(char *fullpath
, char *dir
, char *name
)
86 Pos
= (CHAR
*)GetFileNameInPath(fullpath
);
88 strcpy_s(name
, MAX_PATH
, Pos
);
92 strcpy_s(dir
, MAX_PATH
, fullpath
);
98 static void TrimString(CHAR
*String
, BOOL TrimLeft
)
102 size_t Len
= strlen(String
);
106 if (String
[Len
- 1] != ' ' && String
[Len
- 1] != '\t')
116 while (*Pos1
== ' ' || *Pos1
== '\t')
131 static int VentoyProcessRunCmd(const char *Fmt
, ...)
136 PROCESS_INFORMATION Pi
;
137 char szBuf
[1024] = { 0 };
140 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
143 GetStartupInfoA(&Si
);
144 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
145 Si
.wShowWindow
= SW_HIDE
;
147 Log("Process Run: <%s>", szBuf
);
148 CreateProcessA(NULL
, szBuf
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
149 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
154 static CHAR
VentoyGetFirstFreeDriveLetter(BOOL Reverse
)
160 Drives
= GetLogicalDrives();
164 for (i
= 25; i
>= 2; i
--)
166 if (0 == (Drives
& (1 << i
)))
175 for (i
= 2; i
< 26; i
++)
177 if (0 == (Drives
& (1 << i
)))
185 Log("FirstFreeDriveLetter %u %C:", Reverse
, Letter
);
189 void Log(const char *Fmt
, ...)
196 DWORD LockStatus
= 0;
197 DWORD PID
= GetCurrentProcessId();
200 Len
+= sprintf_s(szBuf
, sizeof(szBuf
),
201 "[%4d/%02d/%02d %02d:%02d:%02d.%03d] [%u] ",
202 Sys
.wYear
, Sys
.wMonth
, Sys
.wDay
,
203 Sys
.wHour
, Sys
.wMinute
, Sys
.wSecond
,
204 Sys
.wMilliseconds
, PID
);
207 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
210 MUTEX_LOCK(g_vtoylog_mutex
);
212 fopen_s(&File
, LOG_FILE
, "a+");
215 fwrite(szBuf
, 1, Len
, File
);
216 fwrite("\n", 1, 1, File
);
220 MUTEX_UNLOCK(g_vtoylog_mutex
);
224 static int LoadNtDriver(const char *DrvBinPath
)
230 SC_HANDLE hServiceMgr
;
232 char name
[256] = { 0 };
234 for (i
= (int)strlen(DrvBinPath
) - 1; i
>= 0; i
--)
236 if (DrvBinPath
[i
] == '\\' || DrvBinPath
[i
] == '/')
238 sprintf_s(name
, sizeof(name
), "%s", DrvBinPath
+ i
+ 1);
243 Log("Load NT driver: %s %s", DrvBinPath
, name
);
245 hServiceMgr
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
246 if (hServiceMgr
== NULL
)
248 Log("OpenSCManager failed Error:%u", GetLastError());
252 Log("OpenSCManager OK");
254 hService
= CreateServiceA(hServiceMgr
,
258 SERVICE_KERNEL_DRIVER
,
259 SERVICE_DEMAND_START
,
260 SERVICE_ERROR_NORMAL
,
262 NULL
, NULL
, NULL
, NULL
, NULL
);
263 if (hService
== NULL
)
265 Status
= GetLastError();
266 if (Status
!= ERROR_IO_PENDING
&& Status
!= ERROR_SERVICE_EXISTS
)
268 Log("CreateService failed v %u", Status
);
269 CloseServiceHandle(hServiceMgr
);
273 hService
= OpenServiceA(hServiceMgr
, name
, SERVICE_ALL_ACCESS
);
274 if (hService
== NULL
)
276 Log("OpenService failed %u", Status
);
277 CloseServiceHandle(hServiceMgr
);
282 Log("CreateService imdisk OK");
284 Ret
= StartServiceA(hService
, 0, NULL
);
287 Log("StartService OK");
291 Status
= GetLastError();
292 if (Status
== ERROR_SERVICE_ALREADY_RUNNING
)
298 Log("StartService error %u", Status
);
303 CloseServiceHandle(hService
);
304 CloseServiceHandle(hServiceMgr
);
306 Log("Load NT driver %s", rc
? "failed" : "success");
311 static int ReadWholeFile2Buf(const char *Fullpath
, void **Data
, DWORD
*Size
)
319 Log("ReadWholeFile2Buf <%s>", Fullpath
);
321 Handle
= CreateFileA(Fullpath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
322 if (Handle
== INVALID_HANDLE_VALUE
)
324 Log("Could not open the file<%s>, error:%u", Fullpath
, GetLastError());
328 FileSize
= SetFilePointer(Handle
, 0, NULL
, FILE_END
);
330 Buffer
= malloc(FileSize
);
333 Log("Failed to alloc memory size:%u", FileSize
);
337 SetFilePointer(Handle
, 0, NULL
, FILE_BEGIN
);
338 if (!ReadFile(Handle
, Buffer
, FileSize
, &dwSize
, NULL
))
340 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
347 Log("Success read file size:%u", FileSize
);
352 SAFE_CLOSE_HANDLE(Handle
);
357 static BOOL
CheckPeHead(BYTE
*Buffer
, DWORD Size
, DWORD Offset
)
362 ventoy_windows_data
*pdata
= NULL
;
364 Head
= Buffer
+ Offset
;
365 pdata
= (ventoy_windows_data
*)Head
;
366 Head
+= sizeof(ventoy_windows_data
);
368 if (pdata
->auto_install_script
[0] && pdata
->auto_install_len
> 0)
370 End
= Offset
+ sizeof(ventoy_windows_data
) + pdata
->auto_install_len
+ 60;
373 Head
+= pdata
->auto_install_len
;
377 if (Head
[0] != 'M' || Head
[1] != 'Z')
382 PeOffset
= *(UINT32
*)(Head
+ 60);
383 if (*(UINT32
*)(Head
+ PeOffset
) != 0x00004550)
392 static BOOL
CheckOsParam(ventoy_os_param
*param
)
397 if (memcmp(¶m
->guid
, &g_ventoy_guid
, sizeof(ventoy_guid
)))
402 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
404 Sum
+= *((BYTE
*)param
+ i
);
412 if (param
->vtoy_img_location_addr
% 4096)
420 static int SaveBuffer2File(const char *Fullpath
, void *Buffer
, DWORD Length
)
426 Log("SaveBuffer2File <%s> len:%u", Fullpath
, Length
);
428 Handle
= CreateFileA(Fullpath
, GENERIC_READ
| GENERIC_WRITE
,
429 FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, CREATE_NEW
, 0, 0);
430 if (Handle
== INVALID_HANDLE_VALUE
)
432 Log("Could not create new file, error:%u", GetLastError());
436 WriteFile(Handle
, Buffer
, Length
, &dwSize
, NULL
);
441 SAFE_CLOSE_HANDLE(Handle
);
446 static int IsUTF8Encode(const char *src
)
449 const UCHAR
*Byte
= (const UCHAR
*)src
;
451 for (i
= 0; i
< MAX_PATH
&& Byte
[i
]; i
++)
462 static int Utf8ToUtf16(const char* src
, WCHAR
* dst
)
464 int size
= MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, 0);
465 return MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, size
+ 1);
468 static BOOL
IsDirExist(const char *Fmt
, ...)
473 CHAR FilePathA
[MAX_PATH
];
474 WCHAR FilePathW
[MAX_PATH
];
477 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
480 UTF8
= IsUTF8Encode(FilePathA
);
484 Utf8ToUtf16(FilePathA
, FilePathW
);
485 Attr
= GetFileAttributesW(FilePathW
);
489 Attr
= GetFileAttributesA(FilePathA
);
492 if (Attr
!= INVALID_FILE_ATTRIBUTES
&& (Attr
& FILE_ATTRIBUTE_DIRECTORY
))
500 static BOOL
IsFileExist(const char *Fmt
, ...)
507 CHAR FilePathA
[MAX_PATH
];
508 WCHAR FilePathW
[MAX_PATH
];
511 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
514 UTF8
= IsUTF8Encode(FilePathA
);
518 Utf8ToUtf16(FilePathA
, FilePathW
);
519 hFile
= CreateFileW(FilePathW
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
523 hFile
= CreateFileA(FilePathA
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
525 if (INVALID_HANDLE_VALUE
== hFile
)
534 Attr
= GetFileAttributesW(FilePathW
);
538 Attr
= GetFileAttributesA(FilePathA
);
541 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
549 Log("File <%s> %s", FilePathA
, (bRet
? "exist" : "NOT exist"));
553 static int GetPhyDiskUUID(const char LogicalDrive
, UINT8
*UUID
, UINT32
*DiskSig
, DISK_EXTENT
*DiskExtent
)
558 VOLUME_DISK_EXTENTS DiskExtents
;
560 UINT8 SectorBuf
[512];
562 Log("GetPhyDiskUUID %C", LogicalDrive
);
564 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", LogicalDrive
);
565 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
566 if (Handle
== INVALID_HANDLE_VALUE
)
568 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
572 Ret
= DeviceIoControl(Handle
,
573 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
577 (DWORD
)(sizeof(DiskExtents
)),
580 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
582 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed, error:%u", GetLastError());
588 memcpy(DiskExtent
, DiskExtents
.Extents
, sizeof(DISK_EXTENT
));
589 Log("%C: is in PhysicalDrive%d Offset:%llu", LogicalDrive
, DiskExtents
.Extents
[0].DiskNumber
,
590 (ULONGLONG
)(DiskExtents
.Extents
[0].StartingOffset
.QuadPart
));
592 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskExtents
.Extents
[0].DiskNumber
);
593 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
594 if (Handle
== INVALID_HANDLE_VALUE
)
596 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
600 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
602 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
607 memcpy(UUID
, SectorBuf
+ 0x180, 16);
610 memcpy(DiskSig
, SectorBuf
+ 0x1B8, 4);
617 static int VentoyMountAnywhere(HANDLE Handle
)
620 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
622 Log("VentoyMountAnywhere");
624 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
625 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
627 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
628 if (Status
!= ERROR_SUCCESS
)
630 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
637 int VentoyMountY(HANDLE Handle
)
642 DWORD physicalDriveNameSize
;
644 WCHAR physicalDriveName
[MAX_PATH
];
645 CHAR physicalDriveNameA
[MAX_PATH
];
646 CHAR cdromDriveName
[MAX_PATH
];
647 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
651 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
652 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
654 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_NO_DRIVE_LETTER
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
655 if (Status
!= ERROR_SUCCESS
)
657 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
661 memset(physicalDriveName
, 0, sizeof(physicalDriveName
));
662 memset(physicalDriveNameA
, 0, sizeof(physicalDriveNameA
));
664 physicalDriveNameSize
= MAX_PATH
;
665 Status
= GetVirtualDiskPhysicalPath(Handle
, &physicalDriveNameSize
, physicalDriveName
);
666 if (Status
!= ERROR_SUCCESS
)
668 Log("Failed GetVirtualDiskPhysicalPath ErrorCode:%u", Status
);
672 for (i
= 0; physicalDriveName
[i
]; i
++)
674 physicalDriveNameA
[i
] = (CHAR
)toupper((CHAR
)(physicalDriveName
[i
]));
677 Log("physicalDriveNameA=<%s>", physicalDriveNameA
);
679 Pos
= strstr(physicalDriveNameA
, "CDROM");
682 Log("Not cdrom phy drive");
686 sprintf_s(cdromDriveName
, sizeof(cdromDriveName
), "\\Device\\%s", Pos
);
687 Log("cdromDriveName=<%s>", cdromDriveName
);
689 for (i
= 0; i
< 3 && (bRet
== FALSE
); i
++)
692 bRet
= DefineDosDeviceA(DDD_RAW_TARGET_PATH
, "Y:", cdromDriveName
);
693 Log("DefineDosDeviceA %s", bRet
? "success" : "failed");
699 static BOOL
VentoyAPINeedMountY(const char *IsoPath
)
707 static int VentoyAttachVirtualDisk(HANDLE Handle
, const char *IsoPath
)
712 Drives
= GetLogicalDrives();
713 if ((1 << 24) & Drives
)
715 Log("Y: is occupied");
720 Log("Y: is free now");
724 if (DriveYFree
&& VentoyAPINeedMountY(IsoPath
))
726 return VentoyMountY(Handle
);
730 return VentoyMountAnywhere(Handle
);
734 int VentoyMountISOByAPI(const char *IsoPath
)
739 WCHAR wFilePath
[512] = { 0 };
740 VIRTUAL_STORAGE_TYPE StorageType
;
741 OPEN_VIRTUAL_DISK_PARAMETERS OpenParameters
;
743 Log("VentoyMountISOByAPI <%s>", IsoPath
);
745 if (IsUTF8Encode(IsoPath
))
747 Log("This is UTF8 encoding");
748 MultiByteToWideChar(CP_UTF8
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
752 Log("This is ANSI encoding");
753 MultiByteToWideChar(CP_ACP
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
756 memset(&StorageType
, 0, sizeof(StorageType
));
757 memset(&OpenParameters
, 0, sizeof(OpenParameters
));
759 OpenParameters
.Version
= OPEN_VIRTUAL_DISK_VERSION_1
;
761 for (i
= 0; i
< 10; i
++)
763 Status
= OpenVirtualDisk(&StorageType
, wFilePath
, VIRTUAL_DISK_ACCESS_READ
, 0, &OpenParameters
, &Handle
);
764 if (ERROR_FILE_NOT_FOUND
== Status
|| ERROR_PATH_NOT_FOUND
== Status
)
766 Log("OpenVirtualDisk ErrorCode:%u, now wait and retry...", Status
);
771 if (ERROR_SUCCESS
== Status
)
773 Log("OpenVirtualDisk success");
775 else if (ERROR_VIRTDISK_PROVIDER_NOT_FOUND
== Status
)
777 Log("VirtualDisk for ISO file is not supported in current system");
781 Log("Failed to open virtual disk ErrorCode:%u", Status
);
787 if (Status
!= ERROR_SUCCESS
)
792 Log("OpenVirtualDisk success");
794 Status
= VentoyAttachVirtualDisk(Handle
, IsoPath
);
795 if (Status
!= ERROR_SUCCESS
)
797 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
802 Log("VentoyAttachVirtualDisk success");
809 static HANDLE g_FatPhyDrive
;
810 static UINT64 g_Part2StartSec
;
812 static int CopyFileFromFatDisk(const CHAR
* SrcFile
, const CHAR
*DstFile
)
819 Log("CopyFileFromFatDisk (%s)==>(%s)", SrcFile
, DstFile
);
821 flfile
= fl_fopen(SrcFile
, "rb");
824 fl_fseek(flfile
, 0, SEEK_END
);
825 size
= (int)fl_ftell(flfile
);
826 fl_fseek(flfile
, 0, SEEK_SET
);
828 buf
= (char *)malloc(size
);
831 fl_fread(buf
, 1, size
, flfile
);
834 SaveBuffer2File(DstFile
, buf
, size
);
844 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
849 LARGE_INTEGER liCurrentPosition
;
851 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
852 liCurrentPosition
.QuadPart
*= 512;
853 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
855 ReadSize
= (DWORD
)(SectorCount
* 512);
857 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
858 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
860 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet
, ReadSize
, dwSize
, GetLastError());
866 static BOOL
Is2K10PE(void)
872 fopen_s(&fp
, "X:\\Windows\\System32\\PECMD.INI", "r");
878 memset(szLine
, 0, sizeof(szLine
));
879 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
881 if (strstr(szLine
, "2k10\\"))
892 static CHAR
GetIMDiskMountLogicalDrive(void)
896 DWORD Mask
= 0x1000000;
898 // fixed use M as mountpoint for 2K10 PE
901 Log("Use M: for 2K10 PE");
905 Drives
= GetLogicalDrives();
906 Log("Drives=0x%x", Drives
);
910 if ((Drives
& Mask
) == 0)
922 UINT64
GetVentoyEfiPartStartSector(HANDLE hDrive
)
927 VTOY_GPT_INFO
*pGpt
= NULL
;
928 UINT64 StartSector
= 0;
930 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
932 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
933 Log("Read MBR Ret:%u Size:%u code:%u", bRet
, dwSize
, LASTERR
);
935 if ((!bRet
) || (dwSize
!= sizeof(MBR
)))
940 if (MBR
.PartTbl
[0].FsFlag
== 0xEE)
942 Log("GPT partition style");
944 pGpt
= malloc(sizeof(VTOY_GPT_INFO
));
950 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
951 bRet
= ReadFile(hDrive
, pGpt
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
952 if ((!bRet
) || (dwSize
!= sizeof(VTOY_GPT_INFO
)))
954 Log("Failed to read gpt info %d %u %d", bRet
, dwSize
, LASTERR
);
958 StartSector
= pGpt
->PartTbl
[1].StartLBA
;
963 Log("MBR partition style");
964 StartSector
= MBR
.PartTbl
[1].StartSectorId
;
967 Log("GetVentoyEfiPart StartSector: %llu", StartSector
);
971 static int VentoyCopyImdisk(DWORD PhyDrive
, CHAR
*ImPath
)
977 CHAR PhyPath
[MAX_PATH
];
978 GET_LENGTH_INFORMATION LengthInfo
;
980 if (IsFileExist("X:\\Windows\\System32\\imdisk.exe"))
982 Log("imdisk.exe already exist, no need to copy...");
983 strcpy_s(ImPath
, MAX_PATH
, "imdisk.exe");
987 if (IsFileExist("X:\\Windows\\System32\\ventoy\\imdisk.exe"))
989 Log("imdisk.exe already copied, no need to copy...");
990 strcpy_s(ImPath
, MAX_PATH
, "ventoy\\imdisk.exe");
994 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
995 hDrive
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
996 if (hDrive
== INVALID_HANDLE_VALUE
)
998 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1002 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
1005 Log("Could not get phy disk %s size, error:%u", PhyPath
, GetLastError());
1009 g_FatPhyDrive
= hDrive
;
1010 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
1012 Log("Parse FAT fs...");
1016 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
1018 if (g_system_bit
== 64)
1020 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.sys", "ventoy\\imdisk.sys");
1021 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.exe", "ventoy\\imdisk.exe");
1022 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.cpl", "ventoy\\imdisk.cpl");
1026 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.sys", "ventoy\\imdisk.sys");
1027 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");
1028 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");
1031 GetCurrentDirectoryA(sizeof(PhyPath
), PhyPath
);
1032 strcat_s(PhyPath
, sizeof(PhyPath
), "\\ventoy\\imdisk.sys");
1034 if (LoadNtDriver(PhyPath
) == 0)
1036 strcpy_s(ImPath
, MAX_PATH
, "ventoy\\imdisk.exe");
1044 SAFE_CLOSE_HANDLE(hDrive
);
1049 static int VentoyRunImdisk(const char *IsoPath
, const char *imdiskexe
)
1053 WCHAR CmdlineW
[512];
1054 PROCESS_INFORMATION Pi
;
1056 Log("VentoyRunImdisk <%s> <%s>", IsoPath
, imdiskexe
);
1058 Letter
= GetIMDiskMountLogicalDrive();
1059 sprintf_s(Cmdline
, sizeof(Cmdline
), "%s -a -o ro -f \"%s\" -m %C:", imdiskexe
, IsoPath
, Letter
);
1060 Log("mount iso to %C: use imdisk cmd <%s>", Letter
, Cmdline
);
1062 if (IsUTF8Encode(IsoPath
))
1065 GetStartupInfoW(&Si
);
1066 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
1067 Si
.wShowWindow
= SW_HIDE
;
1069 Utf8ToUtf16(Cmdline
, CmdlineW
);
1070 CreateProcessW(NULL
, CmdlineW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1072 Log("This is UTF8 encoding");
1077 GetStartupInfoA(&Si
);
1078 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
1079 Si
.wShowWindow
= SW_HIDE
;
1081 CreateProcessA(NULL
, Cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1083 Log("This is ANSI encoding");
1086 Log("Wait for imdisk process ...");
1087 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1088 Log("imdisk process finished");
1093 int VentoyMountISOByImdisk(const char *IsoPath
, DWORD PhyDrive
)
1096 CHAR ImPath
[MAX_PATH
];
1098 Log("VentoyMountISOByImdisk %s", IsoPath
);
1100 if (0 == VentoyCopyImdisk(PhyDrive
, ImPath
))
1102 VentoyRunImdisk(IsoPath
, ImPath
);
1109 static int GetIsoId(CONST CHAR
*IsoPath
, IsoId
*ids
)
1117 hFile
= CreateFileA(IsoPath
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1118 if (hFile
== INVALID_HANDLE_VALUE
)
1123 SetFilePointer(hFile
, 2048 * 16 + 8, NULL
, FILE_BEGIN
);
1124 bRet
[n
++] = ReadFile(hFile
, ids
->SystemId
, 32, &dwSize
, NULL
);
1126 SetFilePointer(hFile
, 2048 * 16 + 40, NULL
, FILE_BEGIN
);
1127 bRet
[n
++] = ReadFile(hFile
, ids
->VolumeId
, 32, &dwSize
, NULL
);
1129 SetFilePointer(hFile
, 2048 * 16 + 318, NULL
, FILE_BEGIN
);
1130 bRet
[n
++] = ReadFile(hFile
, ids
->PulisherId
, 128, &dwSize
, NULL
);
1132 SetFilePointer(hFile
, 2048 * 16 + 446, NULL
, FILE_BEGIN
);
1133 bRet
[n
++] = ReadFile(hFile
, ids
->PreparerId
, 128, &dwSize
, NULL
);
1138 for (i
= 0; i
< n
; i
++)
1140 if (bRet
[i
] == FALSE
)
1147 TrimString(ids
->SystemId
, FALSE
);
1148 TrimString(ids
->VolumeId
, FALSE
);
1149 TrimString(ids
->PulisherId
, FALSE
);
1150 TrimString(ids
->PreparerId
, FALSE
);
1152 Log("ISO ID: System<%s> Volume<%s> Pulisher<%s> Preparer<%s>",
1153 ids
->SystemId
, ids
->VolumeId
, ids
->PulisherId
, ids
->PreparerId
);
1158 static int CheckSkipMountIso(CONST CHAR
*IsoPath
)
1160 BOOL InRoot
= FALSE
;
1162 CONST CHAR
*p
= NULL
;
1166 for (p
= IsoPath
; *p
; p
++)
1168 if (*p
== '\\' || *p
== '/')
1179 memset(&ID
, 0, sizeof(ID
));
1180 if (GetIsoId(IsoPath
, &ID
))
1185 //Bob.Ombs.Modified.Win10PEx64.iso will auto find ISO file in root, so we can skip the mount
1186 if (InRoot
&& strcmp(ID
.VolumeId
, "Modified-Win10PEx64") == 0)
1194 static int MountIsoFile(CONST CHAR
*IsoPath
, DWORD PhyDrive
)
1196 if (CheckSkipMountIso(IsoPath
))
1198 Log("Skip mount ISO file for <%s>", IsoPath
);
1202 if (IsWindows8OrGreater())
1204 Log("This is Windows 8 or latter...");
1205 if (VentoyMountISOByAPI(IsoPath
) == 0)
1207 Log("Mount iso by API success");
1212 Log("Mount iso by API failed, maybe not supported, try imdisk");
1213 return VentoyMountISOByImdisk(IsoPath
, PhyDrive
);
1218 Log("This is before Windows 8 ...");
1219 if (VentoyMountISOByImdisk(IsoPath
, PhyDrive
) == 0)
1221 Log("Mount iso by imdisk success");
1226 return VentoyMountISOByAPI(IsoPath
);
1231 static int GetPhyDriveByLogicalDrive(int DriveLetter
)
1236 VOLUME_DISK_EXTENTS DiskExtents
;
1239 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", (CHAR
)DriveLetter
);
1241 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1242 if (Handle
== INVALID_HANDLE_VALUE
)
1244 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1248 Ret
= DeviceIoControl(Handle
,
1249 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
1253 (DWORD
)(sizeof(DiskExtents
)),
1257 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
1259 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, GetLastError());
1260 SAFE_CLOSE_HANDLE(Handle
);
1263 SAFE_CLOSE_HANDLE(Handle
);
1265 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
1267 DiskExtents
.Extents
[0].DiskNumber
,
1268 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
1269 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
1272 return (int)DiskExtents
.Extents
[0].DiskNumber
;
1276 static int DeleteVentoyPart2MountPoint(DWORD PhyDrive
)
1281 CHAR DriveName
[] = "?:\\";
1283 Log("DeleteVentoyPart2MountPoint Phy%u ...", PhyDrive
);
1285 Drives
= GetLogicalDrives();
1288 if ((Drives
& 0x01) && IsFileExist("%C:\\ventoy\\ventoy.cpio", Letter
))
1290 Log("File %C:\\ventoy\\ventoy.cpio exist", Letter
);
1292 PhyDisk
= GetPhyDriveByLogicalDrive(Letter
);
1293 Log("PhyDisk=%u for %C", PhyDisk
, Letter
);
1295 if (PhyDisk
== PhyDrive
)
1297 DriveName
[0] = Letter
;
1298 DeleteVolumeMountPointA(DriveName
);
1310 static BOOL
check_tar_archive(const char *archive
, CHAR
*tarName
)
1314 const char *pos
= archive
;
1315 const char *slash
= archive
;
1319 if (*pos
== '\\' || *pos
== '/')
1326 len
= (int)strlen(slash
);
1328 if (len
> 7 && (strncmp(slash
+ len
- 7, ".tar.gz", 7) == 0 || strncmp(slash
+ len
- 7, ".tar.xz", 7) == 0))
1330 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1331 tarName
[nameLen
- 3] = 0;
1334 else if (len
> 8 && strncmp(slash
+ len
- 8, ".tar.bz2", 8) == 0)
1336 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1337 tarName
[nameLen
- 4] = 0;
1340 else if (len
> 9 && strncmp(slash
+ len
- 9, ".tar.lzma", 9) == 0)
1342 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1343 tarName
[nameLen
- 5] = 0;
1350 static UCHAR
*g_unxz_buffer
= NULL
;
1351 static int g_unxz_len
= 0;
1353 static void unxz_error(char *x
)
1358 static int unxz_flush(void *src
, unsigned int size
)
1360 memcpy(g_unxz_buffer
+ g_unxz_len
, src
, size
);
1361 g_unxz_len
+= (int)size
;
1366 static int DecompressInjectionArchive(const char *archive
, DWORD PhyDrive
)
1370 UCHAR
*Buffer
= NULL
;
1371 UCHAR
*RawBuffer
= NULL
;
1377 DWORD flags
= CREATE_NO_WINDOW
;
1378 CHAR StrBuf
[MAX_PATH
];
1379 CHAR tarName
[MAX_PATH
];
1381 PROCESS_INFORMATION Pi
;
1382 PROCESS_INFORMATION NewPi
;
1383 GET_LENGTH_INFORMATION LengthInfo
;
1384 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
1386 Log("DecompressInjectionArchive %s", archive
);
1388 sprintf_s(StrBuf
, sizeof(StrBuf
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
1389 hDrive
= CreateFileA(StrBuf
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1390 if (hDrive
== INVALID_HANDLE_VALUE
)
1392 Log("Could not open the disk<%s>, error:%u", StrBuf
, GetLastError());
1396 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
1399 Log("Could not get phy disk %s size, error:%u", StrBuf
, GetLastError());
1403 g_FatPhyDrive
= hDrive
;
1404 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
1406 Log("Parse FAT fs...");
1410 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
1412 if (g_system_bit
== 64)
1414 CopyFileFromFatDisk("/ventoy/7z/64/7za.xz", "ventoy\\7za.xz");
1418 CopyFileFromFatDisk("/ventoy/7z/32/7za.xz", "ventoy\\7za.xz");
1421 ReadWholeFile2Buf("ventoy\\7za.xz", &Buffer
, &dwSize
);
1422 Log("7za.xz file size:%u", dwSize
);
1424 RawBuffer
= malloc(SIZE_1MB
* 4);
1427 g_unxz_buffer
= RawBuffer
;
1429 unxz(Buffer
, (int)dwSize
, NULL
, unxz_flush
, NULL
, &writelen
, unxz_error
);
1430 if (writelen
== (int)dwSize
)
1432 Log("Decompress success 7za.xz(%u) ---> 7za.exe(%d)", dwSize
, g_unxz_len
);
1436 Log("Decompress failed 7za.xz(%u) ---> 7za.exe(%u)", dwSize
, dwSize
);
1439 SaveBuffer2File("ventoy\\7za.exe", RawBuffer
, (DWORD
)g_unxz_len
);
1441 g_unxz_buffer
= NULL
;
1447 Log("Failed to alloc 4MB memory");
1450 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", archive
);
1452 Log("extract inject to X:");
1453 Log("cmdline:<%s>", StrBuf
);
1455 GetStartupInfoA(&Si
);
1457 hOut
= CreateFileA("ventoy\\7z.log",
1459 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1462 FILE_ATTRIBUTE_NORMAL
,
1465 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
1467 if (hOut
!= INVALID_HANDLE_VALUE
)
1469 Si
.hStdError
= hOut
;
1470 Si
.hStdOutput
= hOut
;
1473 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
1474 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1477 // decompress tar archive, for tar.gz/tar.xz/tar.bz2
1479 if (check_tar_archive(archive
, tarName
))
1481 Log("Decompress tar archive...<%s>", tarName
);
1483 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", tarName
);
1485 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &NewPi
);
1486 WaitForSingleObject(NewPi
.hProcess
, INFINITE
);
1488 Log("Now delete %s", tarName
);
1489 DeleteFileA(tarName
);
1492 SAFE_CLOSE_HANDLE(hOut
);
1498 SAFE_CLOSE_HANDLE(hDrive
);
1503 static int UnattendNeedVarExpand(const char *script
)
1508 fopen_s(&fp
, script
, "r");
1514 szLine
[0] = szLine
[4095] = 0;
1516 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
1518 if (strstr(szLine
, "$$VT_"))
1524 szLine
[0] = szLine
[4095] = 0;
1531 static int ExpandSingleVar(VarDiskInfo
*pDiskInfo
, int DiskNum
, const char *var
, char *value
, int len
)
1537 UINT64 uiMaxSize
= 0;
1538 UINT64 uiMaxDelta
= ULLONG_MAX
;
1542 if (strcmp(var
, "VT_WINDOWS_DISK_1ST_NONVTOY") == 0)
1544 for (i
= 0; i
< DiskNum
; i
++)
1546 if (pDiskInfo
[i
].Capacity
> 0 && i
!= g_vtoy_disk_drive
)
1548 Log("%s=<PhyDrive%d>", var
, i
);
1549 sprintf_s(value
, len
, "%d", i
);
1554 else if (strcmp(var
, "VT_WINDOWS_DISK_1ST_NONUSB") == 0)
1556 for (i
= 0; i
< DiskNum
; i
++)
1558 if (pDiskInfo
[i
].Capacity
> 0 && pDiskInfo
[i
].BusType
!= BusTypeUsb
)
1560 Log("%s=<PhyDrive%d>", var
, i
);
1561 sprintf_s(value
, len
, "%d", i
);
1566 else if (strcmp(var
, "VT_WINDOWS_DISK_MAX_SIZE") == 0)
1568 for (i
= 0; i
< DiskNum
; i
++)
1570 if (pDiskInfo
[i
].Capacity
> 0 && pDiskInfo
[i
].Capacity
> uiMaxSize
)
1573 uiMaxSize
= pDiskInfo
[i
].Capacity
;
1577 Log("%s=<PhyDrive%d>", var
, index
);
1578 sprintf_s(value
, len
, "%d", index
);
1580 else if (strncmp(var
, "VT_WINDOWS_DISK_CLOSEST_", 24) == 0)
1582 uiDst
= strtoul(var
+ 24, NULL
, 10);
1583 uiDst
= uiDst
* (1024ULL * 1024ULL * 1024ULL);
1585 for (i
= 0; i
< DiskNum
; i
++)
1587 if (pDiskInfo
[i
].Capacity
== 0)
1592 if (pDiskInfo
[i
].Capacity
> uiDst
)
1594 uiDelta
= pDiskInfo
[i
].Capacity
- uiDst
;
1598 uiDelta
= uiDst
- pDiskInfo
[i
].Capacity
;
1601 if (uiDelta
< uiMaxDelta
)
1603 uiMaxDelta
= uiDelta
;
1608 Log("%s=<PhyDrive%d>", var
, index
);
1609 sprintf_s(value
, len
, "%d", index
);
1613 Log("Invalid var name <%s>", var
);
1614 sprintf_s(value
, len
, "$$%s$$", var
);
1619 sprintf_s(value
, len
, "$$%s$$", var
);
1625 static int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
)
1633 lRet
= RegOpenKeyExA(Key
, SubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1634 Log("RegOpenKeyExA <%s> Ret:%ld", SubKey
, lRet
);
1636 if (ERROR_SUCCESS
== lRet
)
1638 Size
= sizeof(Value
);
1639 lRet
= RegQueryValueExA(hKey
, ValueName
, NULL
, &Type
, (LPBYTE
)&Value
, &Size
);
1640 Log("RegQueryValueExA <%s> ret:%u Size:%u Value:%u", ValueName
, lRet
, Size
, Value
);
1653 static const CHAR
* GetBusTypeString(int Type
)
1657 case BusTypeUnknown
: return "unknown";
1658 case BusTypeScsi
: return "SCSI";
1659 case BusTypeAtapi
: return "Atapi";
1660 case BusTypeAta
: return "ATA";
1661 case BusType1394
: return "1394";
1662 case BusTypeSsa
: return "SSA";
1663 case BusTypeFibre
: return "Fibre";
1664 case BusTypeUsb
: return "USB";
1665 case BusTypeRAID
: return "RAID";
1666 case BusTypeiScsi
: return "iSCSI";
1667 case BusTypeSas
: return "SAS";
1668 case BusTypeSata
: return "SATA";
1669 case BusTypeSd
: return "SD";
1670 case BusTypeMmc
: return "MMC";
1671 case BusTypeVirtual
: return "Virtual";
1672 case BusTypeFileBackedVirtual
: return "FileBackedVirtual";
1673 case BusTypeSpaces
: return "Spaces";
1674 case BusTypeNvme
: return "Nvme";
1679 static int GetHumanReadableGBSize(UINT64 SizeBytes
)
1684 double GB
= SizeBytes
* 1.0 / 1000 / 1000 / 1000;
1686 if ((SizeBytes
% 1073741824) == 0)
1688 return (int)(SizeBytes
/ 1073741824);
1691 for (i
= 0; i
< 12; i
++)
1695 Delta
= (Pow2
- GB
) / Pow2
;
1699 Delta
= (GB
- Pow2
) / Pow2
;
1713 static int EnumerateAllDisk(VarDiskInfo
**ppDiskInfo
, int *pDiskNum
)
1720 VarDiskInfo
*pDiskInfo
= NULL
;
1721 HANDLE Handle
= INVALID_HANDLE_VALUE
;
1723 GET_LENGTH_INFORMATION LengthInfo
;
1724 STORAGE_PROPERTY_QUERY Query
;
1725 STORAGE_DESCRIPTOR_HEADER DevDescHeader
;
1726 STORAGE_DEVICE_DESCRIPTOR
*pDevDesc
;
1728 if (GetRegDwordValue(HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Services\\disk\\Enum", "Count", &Value
) == 0)
1730 DiskNum
= (int)Value
;
1734 Log("Failed to read disk count");
1738 Log("Current phy disk count:%d", DiskNum
);
1744 pDiskInfo
= malloc(DiskNum
* sizeof(VarDiskInfo
));
1747 Log("Failed to alloc");
1750 memset(pDiskInfo
, 0, DiskNum
* sizeof(VarDiskInfo
));
1752 for (i
= 0; i
< DiskNum
; i
++)
1754 SAFE_CLOSE_HANDLE(Handle
);
1756 safe_sprintf(PhyDrive
, "\\\\.\\PhysicalDrive%d", i
);
1757 Handle
= CreateFileA(PhyDrive
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1758 Log("Create file Handle:%p %s status:%u", Handle
, PhyDrive
, LASTERR
);
1760 if (Handle
== INVALID_HANDLE_VALUE
)
1765 bRet
= DeviceIoControl(Handle
,
1766 IOCTL_DISK_GET_LENGTH_INFO
, NULL
,
1774 Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR
);
1778 Log("PHYSICALDRIVE%d size %llu bytes", i
, (ULONGLONG
)LengthInfo
.Length
.QuadPart
);
1780 Query
.PropertyId
= StorageDeviceProperty
;
1781 Query
.QueryType
= PropertyStandardQuery
;
1783 bRet
= DeviceIoControl(Handle
,
1784 IOCTL_STORAGE_QUERY_PROPERTY
,
1788 sizeof(STORAGE_DESCRIPTOR_HEADER
),
1793 Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR
, dwBytes
);
1797 if (DevDescHeader
.Size
< sizeof(STORAGE_DEVICE_DESCRIPTOR
))
1799 Log("Invalid DevDescHeader.Size:%u", DevDescHeader
.Size
);
1803 pDevDesc
= (STORAGE_DEVICE_DESCRIPTOR
*)malloc(DevDescHeader
.Size
);
1806 Log("failed to malloc error:%u len:%u", LASTERR
, DevDescHeader
.Size
);
1810 bRet
= DeviceIoControl(Handle
,
1811 IOCTL_STORAGE_QUERY_PROPERTY
,
1820 Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR
, dwBytes
);
1825 pDiskInfo
[i
].RemovableMedia
= pDevDesc
->RemovableMedia
;
1826 pDiskInfo
[i
].BusType
= pDevDesc
->BusType
;
1827 pDiskInfo
[i
].DeviceType
= pDevDesc
->DeviceType
;
1828 pDiskInfo
[i
].Capacity
= LengthInfo
.Length
.QuadPart
;
1830 if (pDevDesc
->VendorIdOffset
)
1832 safe_strcpy(pDiskInfo
[i
].VendorId
, (char *)pDevDesc
+ pDevDesc
->VendorIdOffset
);
1833 TrimString(pDiskInfo
[i
].VendorId
, TRUE
);
1836 if (pDevDesc
->ProductIdOffset
)
1838 safe_strcpy(pDiskInfo
[i
].ProductId
, (char *)pDevDesc
+ pDevDesc
->ProductIdOffset
);
1839 TrimString(pDiskInfo
[i
].ProductId
, TRUE
);
1842 if (pDevDesc
->ProductRevisionOffset
)
1844 safe_strcpy(pDiskInfo
[i
].ProductRev
, (char *)pDevDesc
+ pDevDesc
->ProductRevisionOffset
);
1845 TrimString(pDiskInfo
[i
].ProductRev
, TRUE
);
1848 if (pDevDesc
->SerialNumberOffset
)
1850 safe_strcpy(pDiskInfo
[i
].SerialNumber
, (char *)pDevDesc
+ pDevDesc
->SerialNumberOffset
);
1851 TrimString(pDiskInfo
[i
].SerialNumber
, TRUE
);
1855 SAFE_CLOSE_HANDLE(Handle
);
1858 Log("########## DUMP DISK BEGIN ##########");
1859 for (i
= 0; i
< DiskNum
; i
++)
1861 Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Name:%s %s",
1862 i
, GetBusTypeString(pDiskInfo
[i
].BusType
), pDiskInfo
[i
].RemovableMedia
,
1863 GetHumanReadableGBSize(pDiskInfo
[i
].Capacity
), pDiskInfo
[i
].Capacity
,
1864 pDiskInfo
[i
].VendorId
, pDiskInfo
[i
].ProductId
);
1866 Log("Ventoy disk is PhyDvr%d", g_vtoy_disk_drive
);
1867 Log("########## DUMP DISK END ##########");
1869 *ppDiskInfo
= pDiskInfo
;
1870 *pDiskNum
= DiskNum
;
1874 static int UnattendVarExpand(const char *script
, const char *tmpfile
)
1883 VarDiskInfo
*pDiskInfo
= NULL
;
1885 Log("UnattendVarExpand ...");
1887 if (EnumerateAllDisk(&pDiskInfo
, &DiskNum
))
1889 Log("Failed to EnumerateAllDisk");
1893 fopen_s(&fp
, script
, "r");
1900 fopen_s(&fout
, tmpfile
, "w+");
1908 szLine
[0] = szLine
[4095] = 0;
1910 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
1912 start
= strstr(szLine
, "$$VT_");
1915 end
= strstr(start
+ 5, "$$");
1921 fprintf(fout
, "%s", szLine
);
1924 ExpandSingleVar(pDiskInfo
, DiskNum
, start
+ 2, szValue
, sizeof(szValue
) - 1);
1925 fprintf(fout
, "%s", szValue
);
1927 fprintf(fout
, "%s", end
+ 2);
1931 fprintf(fout
, "%s", szLine
);
1934 szLine
[0] = szLine
[4095] = 0;
1943 //#define VAR_DEBUG 1
1945 static int CreateUnattendRegKey(const char *file
)
1952 Ret
= RegCreateKeyEx(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dw
);
1953 if (ERROR_SUCCESS
== Ret
)
1955 Ret
= RegSetValueEx(hKey
, "UnattendFile", 0, REG_SZ
, file
, (DWORD
)(strlen(file
) + 1));
1962 static int ProcessUnattendedInstallation(const char *script
, DWORD PhyDrive
)
1966 CHAR TmpFile
[MAX_PATH
];
1967 CHAR CurDir
[MAX_PATH
];
1968 CHAR ImPath
[MAX_PATH
];
1970 Log("Copy unattended XML ...");
1972 GetCurrentDirectory(sizeof(CurDir
), CurDir
);
1974 if ((Letter
>= 'A' && Letter
<= 'Z') || (Letter
>= 'a' && Letter
<= 'z'))
1976 Log("Current Drive Letter: %C", Letter
);
1984 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\AutounattendXXX.xml", Letter
);
1986 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\Unattend.xml", Letter
);
1989 if (UnattendNeedVarExpand(script
))
1991 sprintf_s(TmpFile
, sizeof(TmpFile
), "%C:\\__Autounattend", Letter
);
1992 UnattendVarExpand(script
, TmpFile
);
1994 Log("Expand Copy file <%s> --> <%s>", script
, CurDir
);
1995 CopyFileA(TmpFile
, CurDir
, FALSE
);
1999 Log("No var expand copy file <%s> --> <%s>", script
, CurDir
);
2000 CopyFileA(script
, CurDir
, FALSE
);
2003 VentoyCopyImdisk(PhyDrive
, ImPath
);
2004 DrvLetter
= VentoyGetFirstFreeDriveLetter(FALSE
);
2005 VentoyProcessRunCmd("%s -a -s 64M -m %C: -p \"/fs:FAT32 /q /y\"", ImPath
, DrvLetter
);
2009 sprintf_s(TmpFile
, sizeof(TmpFile
), "%C:\\Unattend.xml", DrvLetter
);
2010 if (CopyFileA(CurDir
, TmpFile
, FALSE
))
2012 DeleteFileA(CurDir
);
2013 Log("Move file <%s> ==> <%s>, use the later as unattend XML", CurDir
, TmpFile
);
2014 CreateUnattendRegKey(TmpFile
);
2018 Log("Failed to copy file <%s> ==> <%s>, use OLD", CurDir
, TmpFile
);
2019 CreateUnattendRegKey(CurDir
);
2025 static int Windows11BypassCheck(const char *isofile
, const char MntLetter
)
2032 CHAR
*Buffer
= NULL
;
2033 VS_FIXEDFILEINFO
* VerInfo
= NULL
;
2034 CHAR CheckFile
[MAX_PATH
];
2035 UINT16 Major
, Minor
, Build
, Revision
;
2037 Log("Windows11BypassCheck for <%s> %C:", isofile
, MntLetter
);
2039 if (FALSE
== IsFileExist("%C:\\sources\\boot.wim", MntLetter
) ||
2040 FALSE
== IsFileExist("%C:\\sources\\compatresources.dll", MntLetter
))
2042 Log("boot.wim/compatresources.dll not exist, this is not a windows install media.");
2046 if (FALSE
== IsFileExist("%C:\\sources\\install.wim", MntLetter
) &&
2047 FALSE
== IsFileExist("%C:\\sources\\install.esd", MntLetter
))
2049 Log("install.wim/install.esd not exist, this is not a windows install media.");
2053 sprintf_s(CheckFile
, sizeof(CheckFile
), "%C:\\sources\\compatresources.dll", MntLetter
);
2054 dwSize
= GetFileVersionInfoSizeA(CheckFile
, &dwHandle
);
2057 Log("Failed to get file version info size: %u", LASTERR
);
2061 Buffer
= malloc(dwSize
);
2067 if (FALSE
== GetFileVersionInfoA(CheckFile
, dwHandle
, dwSize
, Buffer
))
2069 Log("Failed to get file version info : %u", LASTERR
);
2073 if (VerQueryValueA(Buffer
, "\\", (LPVOID
)&VerInfo
, &VerLen
) && VerLen
!= 0)
2075 if (VerInfo
->dwSignature
== VS_FFI_SIGNATURE
)
2077 Major
= HIWORD(VerInfo
->dwFileVersionMS
);
2078 Minor
= LOWORD(VerInfo
->dwFileVersionMS
);
2079 Build
= HIWORD(VerInfo
->dwFileVersionLS
);
2080 Revision
= LOWORD(VerInfo
->dwFileVersionLS
);
2082 Log("FileVersionze: <%u %u %u %u>", Major
, Minor
, Build
, Revision
);
2084 if (Major
== 10 && Build
> 20000)
2091 Log("This is not Windows 11, not need to bypass.", Major
);
2097 //Now we really need to bypass windows 11 check. create registry
2099 HKEY hSubKey
= NULL
;
2102 Status
= RegCreateKeyExA(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dwSize
);
2103 if (ERROR_SUCCESS
!= Status
)
2105 Log("Failed to create reg key System\\Setup %u %u", LASTERR
, Status
);
2109 Status
= RegCreateKeyExA(hKey
, "LabConfig", 0, NULL
, 0, KEY_SET_VALUE
| KEY_QUERY_VALUE
| KEY_CREATE_SUB_KEY
, NULL
, &hSubKey
, &dwSize
);
2110 if (ERROR_SUCCESS
!= Status
)
2112 Log("Failed to create LabConfig reg %u %u", LASTERR
, Status
);
2117 Status
+= RegSetValueExA(hSubKey
, "BypassRAMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
2118 Status
+= RegSetValueExA(hSubKey
, "BypassTPMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
2119 Status
+= RegSetValueExA(hSubKey
, "BypassSecureBootCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
2120 Status
+= RegSetValueExA(hSubKey
, "BypassStorageCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
2121 Status
+= RegSetValueExA(hSubKey
, "BypassCPUCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
2123 Log("Create bypass registry %s %u", (Status
== ERROR_SUCCESS
) ? "SUCCESS" : "FAILED", Status
);
2136 static BOOL
CheckVentoyDisk(DWORD DiskNum
)
2140 UINT8 SectorBuf
[512];
2142 UINT8 check
[8] = { 0x56, 0x54, 0x00, 0x47, 0x65, 0x00, 0x48, 0x44 };
2144 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskNum
);
2145 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
2146 if (Handle
== INVALID_HANDLE_VALUE
)
2148 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
2152 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
2154 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
2155 CloseHandle(Handle
);
2159 CloseHandle(Handle
);
2161 if (memcmp(SectorBuf
+ 0x190, check
, 8) == 0)
2170 static int VentoyHook(ventoy_os_param
*param
)
2175 BOOL vtoyfind
= FALSE
;
2184 DISK_EXTENT DiskExtent
;
2185 DISK_EXTENT VtoyDiskExtent
;
2187 CHAR IsoPath
[MAX_PATH
];
2189 Log("VentoyHook Path:<%s>", param
->vtoy_img_path
);
2191 if (IsUTF8Encode(param
->vtoy_img_path
))
2193 Log("This file is UTF8 encoding");
2196 for (i
= 0; i
< 5; i
++)
2199 Drives
= GetLogicalDrives();
2200 Log("Logic Drives: 0x%x", Drives
);
2206 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
2207 if (IsFileExist("%s", IsoPath
))
2209 Log("File exist under %C:", Letter
);
2210 memset(UUID
, 0, sizeof(UUID
));
2211 memset(&DiskExtent
, 0, sizeof(DiskExtent
));
2212 if (GetPhyDiskUUID(Letter
, UUID
, NULL
, &DiskExtent
) == 0)
2214 if (memcmp(UUID
, param
->vtoy_disk_guid
, 16) == 0)
2216 Log("Disk UUID match");
2224 Log("File NOT exist under %C:", Letter
);
2238 Log("Now wait and retry ...");
2245 Log("Failed to find ISO file");
2249 Log("Find ISO file <%s>", IsoPath
);
2251 //Find VtoyLetter in Vlnk Mode
2252 if (g_os_param_reserved
[6] == 1)
2254 memcpy(&VtoySig
, g_os_param_reserved
+ 7, 4);
2255 for (i
= 0; i
< 5; i
++)
2258 Drives
= GetLogicalDrives();
2259 Log("Logic Drives: 0x%x VentoySig:%08X", Drives
, VtoySig
);
2265 memset(UUID
, 0, sizeof(UUID
));
2266 memset(&VtoyDiskExtent
, 0, sizeof(VtoyDiskExtent
));
2268 if (GetPhyDiskUUID(VtoyLetter
, UUID
, &DiskSig
, &VtoyDiskExtent
) == 0)
2270 Log("DiskSig=%08X PartStart=%lld", DiskSig
, VtoyDiskExtent
.StartingOffset
.QuadPart
);
2271 if (DiskSig
== VtoySig
&& VtoyDiskExtent
.StartingOffset
.QuadPart
== SIZE_1MB
)
2273 Log("Ventoy Disk Sig match");
2286 Log("Find Ventoy Letter: %C", VtoyLetter
);
2291 Log("Now wait and retry ...");
2296 if (vtoyfind
== FALSE
)
2298 Log("Failed to find ventoy disk");
2302 VtoyDiskNum
= VtoyDiskExtent
.DiskNumber
;
2306 VtoyLetter
= Letter
;
2307 Log("No vlnk mode %C", Letter
);
2309 VtoyDiskNum
= DiskExtent
.DiskNumber
;
2312 if (CheckVentoyDisk(VtoyDiskNum
))
2314 Log("Disk check OK %C: %u", VtoyLetter
, VtoyDiskNum
);
2318 Log("Failed to check ventoy disk %u", VtoyDiskNum
);
2322 g_vtoy_disk_drive
= VtoyDiskNum
;
2324 Drives
= GetLogicalDrives();
2325 Log("Drives before mount: 0x%x", Drives
);
2327 rc
= MountIsoFile(IsoPath
, VtoyDiskNum
);
2329 NewDrives
= GetLogicalDrives();
2330 Log("Drives after mount: 0x%x (0x%x)", NewDrives
, (NewDrives
^ Drives
));
2333 NewDrives
= (NewDrives
^ Drives
);
2336 if (NewDrives
& 0x01)
2338 if ((NewDrives
>> 1) == 0)
2340 Log("The ISO file is mounted at %C:", MntLetter
);
2344 Log("Maybe the ISO file is mounted at %C:", MntLetter
);
2353 Log("Mount ISO FILE: %s", rc
== 0 ? "SUCCESS" : "FAILED");
2355 //Windows 11 bypass check
2356 if (g_windows_data
.windows11_bypass_check
== 1)
2358 Windows11BypassCheck(IsoPath
, MntLetter
);
2362 rc
= DeleteVentoyPart2MountPoint(VtoyDiskNum
);
2363 Log("Delete ventoy mountpoint: %s", rc
== 0 ? "SUCCESS" : "NO NEED");
2365 if (g_windows_data
.auto_install_script
[0])
2367 if (IsFileExist("%s", VTOY_AUTO_FILE
))
2369 Log("use auto install script %s...", VTOY_AUTO_FILE
);
2370 ProcessUnattendedInstallation(VTOY_AUTO_FILE
, VtoyDiskNum
);
2374 Log("auto install script %s not exist", IsoPath
);
2379 Log("auto install no need");
2382 if (g_windows_data
.injection_archive
[0])
2384 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", VtoyLetter
, g_windows_data
.injection_archive
);
2385 if (IsFileExist("%s", IsoPath
))
2387 Log("decompress injection archive %s...", IsoPath
);
2388 DecompressInjectionArchive(IsoPath
, VtoyDiskNum
);
2390 if (IsFileExist("%s", AUTO_RUN_BAT
))
2393 DWORD flags
= CREATE_NO_WINDOW
;
2396 PROCESS_INFORMATION Pi
;
2397 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
2399 Log("%s exist, now run it...", AUTO_RUN_BAT
);
2401 GetStartupInfoA(&Si
);
2403 hOut
= CreateFileA(AUTO_RUN_LOG
,
2405 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
2408 FILE_ATTRIBUTE_NORMAL
,
2411 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
2412 if (hOut
!= INVALID_HANDLE_VALUE
)
2414 Si
.hStdError
= hOut
;
2415 Si
.hStdOutput
= hOut
;
2418 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
2419 sprintf_s(StrBuf
, sizeof(StrBuf
), "cmd.exe /c %s \"%s\" %C", AUTO_RUN_BAT
, IsoPath
, MntLetter
);
2420 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
2421 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2423 SAFE_CLOSE_HANDLE(hOut
);
2427 Log("%s not exist...", AUTO_RUN_BAT
);
2432 Log("injection archive %s not exist", IsoPath
);
2437 Log("no injection archive found");
2443 static int ExtractWindowsDataFile(char *databuf
)
2446 char *filedata
= NULL
;
2447 ventoy_windows_data
*pdata
= (ventoy_windows_data
*)databuf
;
2449 Log("ExtractWindowsDataFile: auto install <%s:%d>", pdata
->auto_install_script
, pdata
->auto_install_len
);
2451 filedata
= databuf
+ sizeof(ventoy_windows_data
);
2453 if (pdata
->auto_install_script
[0] && pdata
->auto_install_len
> 0)
2455 SaveBuffer2File(VTOY_AUTO_FILE
, filedata
, pdata
->auto_install_len
);
2456 filedata
+= pdata
->auto_install_len
;
2457 len
= pdata
->auto_install_len
;
2463 static int ventoy_check_create_directory(void)
2465 if (IsDirExist("ventoy"))
2467 Log("ventoy directory already exist");
2471 Log("ventoy directory not exist, now create it.");
2472 if (!CreateDirectoryA("ventoy", NULL
))
2474 Log("Failed to create ventoy directory err:%u", GetLastError());
2482 int VentoyJump(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
2490 DWORD LockStatus
= 0;
2491 BYTE
*Buffer
= NULL
;
2492 CHAR ExeFileName
[MAX_PATH
];
2494 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s", argv
[0]);
2495 if (!IsFileExist("%s", ExeFileName
))
2497 Log("File %s NOT exist, now try %s.exe", ExeFileName
, ExeFileName
);
2498 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s.exe", argv
[0]);
2500 Log("File %s exist ? %s", ExeFileName
, IsFileExist("%s", ExeFileName
) ? "YES" : "NO");
2503 if (ReadWholeFile2Buf(ExeFileName
, (void **)&Buffer
, &FileSize
))
2508 Log("VentoyJump %dbit", g_system_bit
);
2510 MUTEX_LOCK(g_vtoyins_mutex
);
2511 stat
= ventoy_check_create_directory();
2512 MUTEX_UNLOCK(g_vtoyins_mutex
);
2519 for (PeStart
= 0; PeStart
< FileSize
; PeStart
+= 16)
2521 if (CheckOsParam((ventoy_os_param
*)(Buffer
+ PeStart
)) &&
2522 CheckPeHead(Buffer
, FileSize
, PeStart
+ sizeof(ventoy_os_param
)))
2524 Log("Find os pararm at %u", PeStart
);
2526 memcpy(&g_os_param
, Buffer
+ PeStart
, sizeof(ventoy_os_param
));
2527 memcpy(&g_windows_data
, Buffer
+ PeStart
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
2528 exlen
= ExtractWindowsDataFile(Buffer
+ PeStart
+ sizeof(ventoy_os_param
));
2529 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
2531 if (g_os_param_reserved
[0] == 1)
2533 Log("break here for debug .....");
2538 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
2540 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
2542 g_os_param
.vtoy_img_path
[Pos
] = '\\';
2546 PeStart
+= sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
) + exlen
;
2547 sprintf_s(LunchFile
, MAX_PATH
, "ventoy\\%s", GetFileNameInPath(ExeFileName
));
2549 MUTEX_LOCK(g_vtoyins_mutex
);
2550 if (IsFileExist("%s", LunchFile
))
2552 Log("vtoyjump multiple call ...");
2554 MUTEX_UNLOCK(g_vtoyins_mutex
);
2558 SaveBuffer2File(LunchFile
, Buffer
+ PeStart
, FileSize
- PeStart
);
2559 MUTEX_UNLOCK(g_vtoyins_mutex
);
2565 if (PeStart
>= FileSize
)
2567 Log("OS param not found");
2571 if (g_os_param_reserved
[0] == 2)
2573 Log("skip hook for debug .....");
2578 rc
= VentoyHook(&g_os_param
);
2591 int real_main(int argc
, char **argv
)
2595 CHAR NewFile
[MAX_PATH
];
2596 CHAR LunchFile
[MAX_PATH
];
2597 CHAR CallParam
[1024] = { 0 };
2599 PROCESS_INFORMATION Pi
;
2601 Log("#### real_main #### argc = %d", argc
);
2602 Log("program full path: <%s>", g_prog_full_path
);
2603 Log("program dir: <%s>", g_prog_dir
);
2604 Log("program name: <%s>", g_prog_name
);
2606 Log("argc = %d", argc
);
2607 for (i
= 0; i
< argc
; i
++)
2609 Log("argv[%d]=<%s>", i
, argv
[i
]);
2612 strcat_s(CallParam
, sizeof(CallParam
), " ");
2613 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
2617 GetStartupInfoA(&Si
);
2618 memset(LunchFile
, 0, sizeof(LunchFile
));
2620 rc
= VentoyJump(argc
, argv
, LunchFile
);
2622 Log("LunchFile=<%s> CallParam=<%s>", LunchFile
, CallParam
);
2624 if (_stricmp(g_prog_name
, "winpeshl.exe") != 0 && IsFileExist("ventoy\\%s", g_prog_name
))
2626 sprintf_s(NewFile
, sizeof(NewFile
), "%s\\VTOYJUMP.EXE", g_prog_dir
);
2627 MoveFileA(g_prog_full_path
, NewFile
);
2628 Log("Move <%s> to <%s>", g_prog_full_path
, NewFile
);
2630 sprintf_s(NewFile
, sizeof(NewFile
), "ventoy\\%s", g_prog_name
);
2631 CopyFileA(NewFile
, g_prog_full_path
, TRUE
);
2632 Log("Copy <%s> to <%s>", NewFile
, g_prog_full_path
);
2634 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", g_prog_full_path
);
2635 Log("Final lunchFile is <%s>", LunchFile
);
2639 Log("We don't need to recover original <%s>", g_prog_name
);
2642 if (g_os_param_reserved
[0] == 3)
2644 Log("Open log for debug ...");
2645 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "notepad.exe ventoy.log");
2651 strcat_s(LunchFile
, sizeof(LunchFile
), CallParam
);
2653 else if (NULL
== strstr(LunchFile
, "setup.exe"))
2655 Log("Not setup.exe, hide windows.");
2656 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
2657 Si
.wShowWindow
= SW_HIDE
;
2660 Log("Ventoy jump %s ...", rc
== 0 ? "success" : "failed");
2663 Log("Now launch <%s> ...", LunchFile
);
2665 if (g_os_param_reserved
[0] == 4)
2667 Log("Open cmd for debug ...");
2668 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
2669 Si
.wShowWindow
= SW_NORMAL
;
2670 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "cmd.exe");
2673 Log("Backup log at this point");
2674 CopyFileA(LOG_FILE
, "X:\\Windows\\ventoy.backup", TRUE
);
2676 CreateProcessA(NULL
, LunchFile
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
2678 for (i
= 0; rc
&& i
< 1800; i
++)
2680 Log("Ventoy hook failed, now wait and retry ...");
2682 rc
= VentoyHook(&g_os_param
);
2685 Log("Wait process...");
2686 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2688 Log("vtoyjump finished");
2692 static void VentoyToUpper(CHAR
*str
)
2695 for (i
= 0; str
[i
]; i
++)
2697 str
[i
] = (CHAR
)toupper(str
[i
]);
2701 static int vtoy_remove_duplicate_file(char *File
)
2703 CHAR szCmd
[MAX_PATH
];
2704 CHAR NewFile
[MAX_PATH
];
2706 PROCESS_INFORMATION Pi
;
2708 Log("<1> Copy New file", File
);
2709 sprintf_s(NewFile
, sizeof(NewFile
), "%s_NEW", File
);
2710 CopyFileA(File
, NewFile
, FALSE
);
2712 Log("<2> Remove file <%s>", File
);
2713 GetStartupInfoA(&Si
);
2714 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
2715 Si
.wShowWindow
= SW_HIDE
;
2716 sprintf_s(szCmd
, sizeof(szCmd
), "cmd.exe /c del /F /Q %s", File
);
2717 CreateProcessA(NULL
, szCmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
2718 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2720 Log("<3> Copy back file <%s>", File
);
2721 MoveFileA(NewFile
, File
);
2726 int main(int argc
, char **argv
)
2730 PROCESS_INFORMATION Pi
;
2731 CHAR CurDir
[MAX_PATH
];
2732 CHAR NewArgv0
[MAX_PATH
];
2733 CHAR CallParam
[1024] = { 0 };
2735 g_vtoylog_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYLOG_LOCK");
2736 g_vtoyins_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYINS_LOCK");
2738 Log("######## VentoyJump %dbit ##########", g_system_bit
);
2740 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
2741 Log("Current directory is <%s>", CurDir
);
2743 GetModuleFileNameA(NULL
, g_prog_full_path
, MAX_PATH
);
2744 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
2746 Log("EXE path: <%s> dir:<%s> name:<%s>", g_prog_full_path
, g_prog_dir
, g_prog_name
);
2748 if (IsFileExist(WIMBOOT_FILE
))
2750 Log("This is wimboot mode ...");
2751 g_wimboot_mode
= TRUE
;
2753 if (!IsFileExist(WIMBOOT_DONE
))
2755 vtoy_remove_duplicate_file(g_prog_full_path
);
2756 SaveBuffer2File(WIMBOOT_DONE
, g_prog_full_path
, 1);
2761 Log("This is normal mode ...");
2764 if (_stricmp(g_prog_name
, "WinLogon.exe") == 0)
2766 Log("This time is rejump back ...");
2768 strcpy_s(g_prog_full_path
, sizeof(g_prog_full_path
), argv
[1]);
2769 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
2771 return real_main(argc
- 1, argv
+ 1);
2773 else if (_stricmp(g_prog_name
, "PECMD.exe") == 0)
2775 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_dir
);
2776 VentoyToUpper(NewArgv0
);
2778 if (NULL
== strstr(NewArgv0
, "SYSTEM32") && IsFileExist(ORG_PECMD_BK_PATH
))
2780 Log("Just call original pecmd.exe");
2781 strcpy_s(CallParam
, sizeof(CallParam
), ORG_PECMD_PATH
);
2785 Log("We need to rejump for pecmd ...");
2787 ventoy_check_create_directory();
2788 CopyFileA(g_prog_full_path
, "ventoy\\WinLogon.exe", TRUE
);
2790 sprintf_s(CallParam
, sizeof(CallParam
), "ventoy\\WinLogon.exe %s", g_prog_full_path
);
2793 for (i
= 1; i
< argc
; i
++)
2795 strcat_s(CallParam
, sizeof(CallParam
), " ");
2796 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
2799 Log("Now rejump to <%s> ...", CallParam
);
2800 GetStartupInfoA(&Si
);
2801 CreateProcessA(NULL
, CallParam
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
2803 Log("Wait rejump process...");
2804 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2805 Log("rejump finished");
2810 Log("We don't need to rejump ...");
2812 ventoy_check_create_directory();
2813 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_full_path
);
2816 return real_main(argc
, argv
);