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 CHAR g_prog_full_path
[MAX_PATH
];
40 static CHAR g_prog_dir
[MAX_PATH
];
41 static CHAR g_prog_name
[MAX_PATH
];
43 #define VTOY_PECMD_PATH "X:\\Windows\\system32\\ventoy\\PECMD.EXE"
44 #define ORG_PECMD_PATH "X:\\Windows\\system32\\PECMD.EXE"
45 #define ORG_PECMD_BK_PATH "X:\\Windows\\system32\\PECMD.EXE_BACK.EXE"
47 #define AUTO_RUN_BAT "X:\\VentoyAutoRun.bat"
48 #define AUTO_RUN_LOG "X:\\VentoyAutoRun.log"
50 #define LOG_FILE "X:\\Windows\\system32\\ventoy.log"
51 #define MUTEX_LOCK(hmutex) if (hmutex != NULL) LockStatus = WaitForSingleObject(hmutex, INFINITE)
52 #define MUTEX_UNLOCK(hmutex) if (hmutex != NULL && WAIT_OBJECT_0 == LockStatus) ReleaseMutex(hmutex)
54 static const char * GetFileNameInPath(const char *fullpath
)
58 if (strstr(fullpath
, ":"))
60 for (i
= (int)strlen(fullpath
); i
> 0; i
--)
62 if (fullpath
[i
- 1] == '/' || fullpath
[i
- 1] == '\\')
72 static int split_path_name(char *fullpath
, char *dir
, char *name
)
77 Pos
= (CHAR
*)GetFileNameInPath(fullpath
);
79 strcpy_s(name
, MAX_PATH
, Pos
);
83 strcpy_s(dir
, MAX_PATH
, fullpath
);
90 void Log(const char *Fmt
, ...)
98 DWORD PID
= GetCurrentProcessId();
101 Len
+= sprintf_s(szBuf
, sizeof(szBuf
),
102 "[%4d/%02d/%02d %02d:%02d:%02d.%03d] [%u] ",
103 Sys
.wYear
, Sys
.wMonth
, Sys
.wDay
,
104 Sys
.wHour
, Sys
.wMinute
, Sys
.wSecond
,
105 Sys
.wMilliseconds
, PID
);
108 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
111 MUTEX_LOCK(g_vtoylog_mutex
);
113 fopen_s(&File
, LOG_FILE
, "a+");
116 fwrite(szBuf
, 1, Len
, File
);
117 fwrite("\n", 1, 1, File
);
121 MUTEX_UNLOCK(g_vtoylog_mutex
);
125 static int LoadNtDriver(const char *DrvBinPath
)
131 SC_HANDLE hServiceMgr
;
133 char name
[256] = { 0 };
135 for (i
= (int)strlen(DrvBinPath
) - 1; i
>= 0; i
--)
137 if (DrvBinPath
[i
] == '\\' || DrvBinPath
[i
] == '/')
139 sprintf_s(name
, sizeof(name
), "%s", DrvBinPath
+ i
+ 1);
144 Log("Load NT driver: %s %s", DrvBinPath
, name
);
146 hServiceMgr
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
147 if (hServiceMgr
== NULL
)
149 Log("OpenSCManager failed Error:%u", GetLastError());
153 Log("OpenSCManager OK");
155 hService
= CreateServiceA(hServiceMgr
,
159 SERVICE_KERNEL_DRIVER
,
160 SERVICE_DEMAND_START
,
161 SERVICE_ERROR_NORMAL
,
163 NULL
, NULL
, NULL
, NULL
, NULL
);
164 if (hService
== NULL
)
166 Status
= GetLastError();
167 if (Status
!= ERROR_IO_PENDING
&& Status
!= ERROR_SERVICE_EXISTS
)
169 Log("CreateService failed v %u", Status
);
170 CloseServiceHandle(hServiceMgr
);
174 hService
= OpenServiceA(hServiceMgr
, name
, SERVICE_ALL_ACCESS
);
175 if (hService
== NULL
)
177 Log("OpenService failed %u", Status
);
178 CloseServiceHandle(hServiceMgr
);
183 Log("CreateService imdisk OK");
185 Ret
= StartServiceA(hService
, 0, NULL
);
188 Log("StartService OK");
192 Status
= GetLastError();
193 if (Status
== ERROR_SERVICE_ALREADY_RUNNING
)
199 Log("StartService error %u", Status
);
204 CloseServiceHandle(hService
);
205 CloseServiceHandle(hServiceMgr
);
207 Log("Load NT driver %s", rc
? "failed" : "success");
212 static int ReadWholeFile2Buf(const char *Fullpath
, void **Data
, DWORD
*Size
)
220 Log("ReadWholeFile2Buf <%s>", Fullpath
);
222 Handle
= CreateFileA(Fullpath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
223 if (Handle
== INVALID_HANDLE_VALUE
)
225 Log("Could not open the file<%s>, error:%u", Fullpath
, GetLastError());
229 FileSize
= SetFilePointer(Handle
, 0, NULL
, FILE_END
);
231 Buffer
= malloc(FileSize
);
234 Log("Failed to alloc memory size:%u", FileSize
);
238 SetFilePointer(Handle
, 0, NULL
, FILE_BEGIN
);
239 if (!ReadFile(Handle
, Buffer
, FileSize
, &dwSize
, NULL
))
241 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
248 Log("Success read file size:%u", FileSize
);
253 SAFE_CLOSE_HANDLE(Handle
);
258 static BOOL
CheckPeHead(BYTE
*Head
)
262 if (Head
[0] != 'M' || Head
[1] != 'Z')
267 PeOffset
= *(UINT32
*)(Head
+ 60);
268 if (*(UINT32
*)(Head
+ PeOffset
) != 0x00004550)
277 static BOOL
CheckOsParam(ventoy_os_param
*param
)
282 if (memcmp(¶m
->guid
, &g_ventoy_guid
, sizeof(ventoy_guid
)))
287 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
289 Sum
+= *((BYTE
*)param
+ i
);
297 if (param
->vtoy_img_location_addr
% 4096)
305 static int SaveBuffer2File(const char *Fullpath
, void *Buffer
, DWORD Length
)
311 Log("SaveBuffer2File <%s> len:%u", Fullpath
, Length
);
313 Handle
= CreateFileA(Fullpath
, GENERIC_READ
| GENERIC_WRITE
,
314 FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, CREATE_NEW
, 0, 0);
315 if (Handle
== INVALID_HANDLE_VALUE
)
317 Log("Could not create new file, error:%u", GetLastError());
321 WriteFile(Handle
, Buffer
, Length
, &dwSize
, NULL
);
326 SAFE_CLOSE_HANDLE(Handle
);
331 static int IsUTF8Encode(const char *src
)
334 const UCHAR
*Byte
= (const UCHAR
*)src
;
336 for (i
= 0; i
< MAX_PATH
&& Byte
[i
]; i
++)
347 static int Utf8ToUtf16(const char* src
, WCHAR
* dst
)
349 int size
= MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, 0);
350 return MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, size
+ 1);
353 static BOOL
IsDirExist(const char *Fmt
, ...)
358 CHAR FilePathA
[MAX_PATH
];
359 WCHAR FilePathW
[MAX_PATH
];
362 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
365 UTF8
= IsUTF8Encode(FilePathA
);
369 Utf8ToUtf16(FilePathA
, FilePathW
);
370 Attr
= GetFileAttributesW(FilePathW
);
374 Attr
= GetFileAttributesA(FilePathA
);
377 if (Attr
!= INVALID_FILE_ATTRIBUTES
&& (Attr
& FILE_ATTRIBUTE_DIRECTORY
))
385 static BOOL
IsFileExist(const char *Fmt
, ...)
392 CHAR FilePathA
[MAX_PATH
];
393 WCHAR FilePathW
[MAX_PATH
];
396 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
399 UTF8
= IsUTF8Encode(FilePathA
);
403 Utf8ToUtf16(FilePathA
, FilePathW
);
404 hFile
= CreateFileW(FilePathW
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
408 hFile
= CreateFileA(FilePathA
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
410 if (INVALID_HANDLE_VALUE
== hFile
)
419 Attr
= GetFileAttributesW(FilePathW
);
423 Attr
= GetFileAttributesA(FilePathA
);
426 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
434 Log("File <%s> %s", FilePathA
, (bRet
? "exist" : "NOT exist"));
438 static int GetPhyDiskUUID(const char LogicalDrive
, UINT8
*UUID
, UINT32
*DiskSig
, DISK_EXTENT
*DiskExtent
)
443 VOLUME_DISK_EXTENTS DiskExtents
;
445 UINT8 SectorBuf
[512];
447 Log("GetPhyDiskUUID %C", LogicalDrive
);
449 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", LogicalDrive
);
450 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
451 if (Handle
== INVALID_HANDLE_VALUE
)
453 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
457 Ret
= DeviceIoControl(Handle
,
458 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
462 (DWORD
)(sizeof(DiskExtents
)),
465 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
467 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed, error:%u", GetLastError());
473 memcpy(DiskExtent
, DiskExtents
.Extents
, sizeof(DISK_EXTENT
));
474 Log("%C: is in PhysicalDrive%d Offset:%llu", LogicalDrive
, DiskExtents
.Extents
[0].DiskNumber
,
475 (ULONGLONG
)(DiskExtents
.Extents
[0].StartingOffset
.QuadPart
));
477 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskExtents
.Extents
[0].DiskNumber
);
478 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
479 if (Handle
== INVALID_HANDLE_VALUE
)
481 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
485 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
487 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
492 memcpy(UUID
, SectorBuf
+ 0x180, 16);
495 memcpy(DiskSig
, SectorBuf
+ 0x1B8, 4);
502 static int VentoyMountAnywhere(HANDLE Handle
)
505 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
507 Log("VentoyMountAnywhere");
509 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
510 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
512 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
513 if (Status
!= ERROR_SUCCESS
)
515 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
522 int VentoyMountY(HANDLE Handle
)
527 DWORD physicalDriveNameSize
;
529 WCHAR physicalDriveName
[MAX_PATH
];
530 CHAR physicalDriveNameA
[MAX_PATH
];
531 CHAR cdromDriveName
[MAX_PATH
];
532 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
536 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
537 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
539 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
);
540 if (Status
!= ERROR_SUCCESS
)
542 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
546 memset(physicalDriveName
, 0, sizeof(physicalDriveName
));
547 memset(physicalDriveNameA
, 0, sizeof(physicalDriveNameA
));
549 physicalDriveNameSize
= MAX_PATH
;
550 Status
= GetVirtualDiskPhysicalPath(Handle
, &physicalDriveNameSize
, physicalDriveName
);
551 if (Status
!= ERROR_SUCCESS
)
553 Log("Failed GetVirtualDiskPhysicalPath ErrorCode:%u", Status
);
557 for (i
= 0; physicalDriveName
[i
]; i
++)
559 physicalDriveNameA
[i
] = (CHAR
)toupper((CHAR
)(physicalDriveName
[i
]));
562 Log("physicalDriveNameA=<%s>", physicalDriveNameA
);
564 Pos
= strstr(physicalDriveNameA
, "CDROM");
567 Log("Not cdrom phy drive");
571 sprintf_s(cdromDriveName
, sizeof(cdromDriveName
), "\\Device\\%s", Pos
);
572 Log("cdromDriveName=<%s>", cdromDriveName
);
574 for (i
= 0; i
< 3 && (bRet
== FALSE
); i
++)
577 bRet
= DefineDosDeviceA(DDD_RAW_TARGET_PATH
, "Y:", cdromDriveName
);
578 Log("DefineDosDeviceA %s", bRet
? "success" : "failed");
584 static BOOL
VentoyAPINeedMountY(const char *IsoPath
)
592 static int VentoyAttachVirtualDisk(HANDLE Handle
, const char *IsoPath
)
597 Drives
= GetLogicalDrives();
598 if ((1 << 24) & Drives
)
600 Log("Y: is occupied");
605 Log("Y: is free now");
609 if (DriveYFree
&& VentoyAPINeedMountY(IsoPath
))
611 return VentoyMountY(Handle
);
615 return VentoyMountAnywhere(Handle
);
619 int VentoyMountISOByAPI(const char *IsoPath
)
624 WCHAR wFilePath
[512] = { 0 };
625 VIRTUAL_STORAGE_TYPE StorageType
;
626 OPEN_VIRTUAL_DISK_PARAMETERS OpenParameters
;
628 Log("VentoyMountISOByAPI <%s>", IsoPath
);
630 if (IsUTF8Encode(IsoPath
))
632 Log("This is UTF8 encoding");
633 MultiByteToWideChar(CP_UTF8
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
637 Log("This is ANSI encoding");
638 MultiByteToWideChar(CP_ACP
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
641 memset(&StorageType
, 0, sizeof(StorageType
));
642 memset(&OpenParameters
, 0, sizeof(OpenParameters
));
644 OpenParameters
.Version
= OPEN_VIRTUAL_DISK_VERSION_1
;
646 for (i
= 0; i
< 10; i
++)
648 Status
= OpenVirtualDisk(&StorageType
, wFilePath
, VIRTUAL_DISK_ACCESS_READ
, 0, &OpenParameters
, &Handle
);
649 if (ERROR_FILE_NOT_FOUND
== Status
|| ERROR_PATH_NOT_FOUND
== Status
)
651 Log("OpenVirtualDisk ErrorCode:%u, now wait and retry...", Status
);
656 if (ERROR_SUCCESS
== Status
)
658 Log("OpenVirtualDisk success");
660 else if (ERROR_VIRTDISK_PROVIDER_NOT_FOUND
== Status
)
662 Log("VirtualDisk for ISO file is not supported in current system");
666 Log("Failed to open virtual disk ErrorCode:%u", Status
);
672 if (Status
!= ERROR_SUCCESS
)
677 Log("OpenVirtualDisk success");
679 Status
= VentoyAttachVirtualDisk(Handle
, IsoPath
);
680 if (Status
!= ERROR_SUCCESS
)
682 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
687 Log("VentoyAttachVirtualDisk success");
694 static HANDLE g_FatPhyDrive
;
695 static UINT64 g_Part2StartSec
;
697 static int CopyFileFromFatDisk(const CHAR
* SrcFile
, const CHAR
*DstFile
)
704 Log("CopyFileFromFatDisk (%s)==>(%s)", SrcFile
, DstFile
);
706 flfile
= fl_fopen(SrcFile
, "rb");
709 fl_fseek(flfile
, 0, SEEK_END
);
710 size
= (int)fl_ftell(flfile
);
711 fl_fseek(flfile
, 0, SEEK_SET
);
713 buf
= (char *)malloc(size
);
716 fl_fread(buf
, 1, size
, flfile
);
719 SaveBuffer2File(DstFile
, buf
, size
);
729 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
734 LARGE_INTEGER liCurrentPosition
;
736 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
737 liCurrentPosition
.QuadPart
*= 512;
738 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
740 ReadSize
= (DWORD
)(SectorCount
* 512);
742 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
743 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
745 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet
, ReadSize
, dwSize
, GetLastError());
751 static BOOL
Is2K10PE(void)
757 fopen_s(&fp
, "X:\\Windows\\System32\\PECMD.INI", "r");
763 memset(szLine
, 0, sizeof(szLine
));
764 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
766 if (strstr(szLine
, "2k10\\"))
777 static CHAR
GetIMDiskMountLogicalDrive(void)
781 DWORD Mask
= 0x1000000;
783 // fixed use M as mountpoint for 2K10 PE
786 Log("Use M: for 2K10 PE");
790 Drives
= GetLogicalDrives();
791 Log("Drives=0x%x", Drives
);
795 if ((Drives
& Mask
) == 0)
807 UINT64
GetVentoyEfiPartStartSector(HANDLE hDrive
)
812 VTOY_GPT_INFO
*pGpt
= NULL
;
813 UINT64 StartSector
= 0;
815 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
817 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
818 Log("Read MBR Ret:%u Size:%u code:%u", bRet
, dwSize
, LASTERR
);
820 if ((!bRet
) || (dwSize
!= sizeof(MBR
)))
825 if (MBR
.PartTbl
[0].FsFlag
== 0xEE)
827 Log("GPT partition style");
829 pGpt
= malloc(sizeof(VTOY_GPT_INFO
));
835 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
836 bRet
= ReadFile(hDrive
, pGpt
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
837 if ((!bRet
) || (dwSize
!= sizeof(VTOY_GPT_INFO
)))
839 Log("Failed to read gpt info %d %u %d", bRet
, dwSize
, LASTERR
);
843 StartSector
= pGpt
->PartTbl
[1].StartLBA
;
848 Log("MBR partition style");
849 StartSector
= MBR
.PartTbl
[1].StartSectorId
;
852 Log("GetVentoyEfiPart StartSector: %llu", StartSector
);
856 static int VentoyRunImdisk(const char *IsoPath
, const char *imdiskexe
)
861 PROCESS_INFORMATION Pi
;
863 Log("VentoyRunImdisk <%s> <%s>", IsoPath
, imdiskexe
);
865 Letter
= GetIMDiskMountLogicalDrive();
866 sprintf_s(Cmdline
, sizeof(Cmdline
), "%s -a -o ro -f \"%s\" -m %C:", imdiskexe
, IsoPath
, Letter
);
867 Log("mount iso to %C: use imdisk cmd <%s>", Letter
, Cmdline
);
869 if (IsUTF8Encode(IsoPath
))
872 GetStartupInfoW(&Si
);
873 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
874 Si
.wShowWindow
= SW_HIDE
;
876 Utf8ToUtf16(Cmdline
, CmdlineW
);
877 CreateProcessW(NULL
, CmdlineW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
879 Log("This is UTF8 encoding");
884 GetStartupInfoA(&Si
);
885 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
886 Si
.wShowWindow
= SW_HIDE
;
888 CreateProcessA(NULL
, Cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
890 Log("This is ANSI encoding");
893 Log("Wait for imdisk process ...");
894 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
895 Log("imdisk process finished");
900 int VentoyMountISOByImdisk(const char *IsoPath
, DWORD PhyDrive
)
906 CHAR PhyPath
[MAX_PATH
];
907 GET_LENGTH_INFORMATION LengthInfo
;
909 Log("VentoyMountISOByImdisk %s", IsoPath
);
911 if (IsFileExist("X:\\Windows\\System32\\imdisk.exe"))
913 Log("imdisk.exe exist, use it directly...");
914 VentoyRunImdisk(IsoPath
, "imdisk.exe");
918 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
919 hDrive
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
920 if (hDrive
== INVALID_HANDLE_VALUE
)
922 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
926 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
929 Log("Could not get phy disk %s size, error:%u", PhyPath
, GetLastError());
933 g_FatPhyDrive
= hDrive
;
934 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
936 Log("Parse FAT fs...");
940 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
942 if (g_system_bit
== 64)
944 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.sys", "ventoy\\imdisk.sys");
945 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.exe", "ventoy\\imdisk.exe");
946 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.cpl", "ventoy\\imdisk.cpl");
950 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.sys", "ventoy\\imdisk.sys");
951 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");
952 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");
955 GetCurrentDirectoryA(sizeof(PhyPath
), PhyPath
);
956 strcat_s(PhyPath
, sizeof(PhyPath
), "\\ventoy\\imdisk.sys");
958 if (LoadNtDriver(PhyPath
) == 0)
960 VentoyRunImdisk(IsoPath
, "ventoy\\imdisk.exe");
968 SAFE_CLOSE_HANDLE(hDrive
);
973 static int MountIsoFile(CONST CHAR
*IsoPath
, DWORD PhyDrive
)
975 if (IsWindows8OrGreater())
977 Log("This is Windows 8 or latter...");
978 if (VentoyMountISOByAPI(IsoPath
) == 0)
980 Log("Mount iso by API success");
985 Log("Mount iso by API failed, maybe not supported, try imdisk");
986 return VentoyMountISOByImdisk(IsoPath
, PhyDrive
);
991 Log("This is before Windows 8 ...");
992 if (VentoyMountISOByImdisk(IsoPath
, PhyDrive
) == 0)
994 Log("Mount iso by imdisk success");
999 return VentoyMountISOByAPI(IsoPath
);
1004 static int GetPhyDriveByLogicalDrive(int DriveLetter
)
1009 VOLUME_DISK_EXTENTS DiskExtents
;
1012 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", (CHAR
)DriveLetter
);
1014 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1015 if (Handle
== INVALID_HANDLE_VALUE
)
1017 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1021 Ret
= DeviceIoControl(Handle
,
1022 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
1026 (DWORD
)(sizeof(DiskExtents
)),
1030 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
1032 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, GetLastError());
1033 SAFE_CLOSE_HANDLE(Handle
);
1036 SAFE_CLOSE_HANDLE(Handle
);
1038 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
1040 DiskExtents
.Extents
[0].DiskNumber
,
1041 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
1042 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
1045 return (int)DiskExtents
.Extents
[0].DiskNumber
;
1049 static int DeleteVentoyPart2MountPoint(DWORD PhyDrive
)
1054 CHAR DriveName
[] = "?:\\";
1056 Log("DeleteVentoyPart2MountPoint Phy%u ...", PhyDrive
);
1058 Drives
= GetLogicalDrives();
1061 if ((Drives
& 0x01) && IsFileExist("%C:\\ventoy\\ventoy.cpio", Letter
))
1063 Log("File %C:\\ventoy\\ventoy.cpio exist", Letter
);
1065 PhyDisk
= GetPhyDriveByLogicalDrive(Letter
);
1066 Log("PhyDisk=%u for %C", PhyDisk
, Letter
);
1068 if (PhyDisk
== PhyDrive
)
1070 DriveName
[0] = Letter
;
1071 DeleteVolumeMountPointA(DriveName
);
1083 static BOOL
check_tar_archive(const char *archive
, CHAR
*tarName
)
1087 const char *pos
= archive
;
1088 const char *slash
= archive
;
1092 if (*pos
== '\\' || *pos
== '/')
1099 len
= (int)strlen(slash
);
1101 if (len
> 7 && (strncmp(slash
+ len
- 7, ".tar.gz", 7) == 0 || strncmp(slash
+ len
- 7, ".tar.xz", 7) == 0))
1103 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1104 tarName
[nameLen
- 3] = 0;
1107 else if (len
> 8 && strncmp(slash
+ len
- 8, ".tar.bz2", 8) == 0)
1109 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1110 tarName
[nameLen
- 4] = 0;
1113 else if (len
> 9 && strncmp(slash
+ len
- 9, ".tar.lzma", 9) == 0)
1115 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1116 tarName
[nameLen
- 5] = 0;
1123 static UCHAR
*g_unxz_buffer
= NULL
;
1124 static int g_unxz_len
= 0;
1126 static void unxz_error(char *x
)
1131 static int unxz_flush(void *src
, unsigned int size
)
1133 memcpy(g_unxz_buffer
+ g_unxz_len
, src
, size
);
1134 g_unxz_len
+= (int)size
;
1139 static int DecompressInjectionArchive(const char *archive
, DWORD PhyDrive
)
1143 UCHAR
*Buffer
= NULL
;
1144 UCHAR
*RawBuffer
= NULL
;
1150 DWORD flags
= CREATE_NO_WINDOW
;
1151 CHAR StrBuf
[MAX_PATH
];
1152 CHAR tarName
[MAX_PATH
];
1154 PROCESS_INFORMATION Pi
;
1155 PROCESS_INFORMATION NewPi
;
1156 GET_LENGTH_INFORMATION LengthInfo
;
1157 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
1159 Log("DecompressInjectionArchive %s", archive
);
1161 sprintf_s(StrBuf
, sizeof(StrBuf
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
1162 hDrive
= CreateFileA(StrBuf
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1163 if (hDrive
== INVALID_HANDLE_VALUE
)
1165 Log("Could not open the disk<%s>, error:%u", StrBuf
, GetLastError());
1169 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
1172 Log("Could not get phy disk %s size, error:%u", StrBuf
, GetLastError());
1176 g_FatPhyDrive
= hDrive
;
1177 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
1179 Log("Parse FAT fs...");
1183 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
1185 if (g_system_bit
== 64)
1187 CopyFileFromFatDisk("/ventoy/7z/64/7za.xz", "ventoy\\7za.xz");
1191 CopyFileFromFatDisk("/ventoy/7z/32/7za.xz", "ventoy\\7za.xz");
1194 ReadWholeFile2Buf("ventoy\\7za.xz", &Buffer
, &dwSize
);
1195 Log("7za.xz file size:%u", dwSize
);
1197 RawBuffer
= malloc(SIZE_1MB
* 4);
1200 g_unxz_buffer
= RawBuffer
;
1202 unxz(Buffer
, (int)dwSize
, NULL
, unxz_flush
, NULL
, &writelen
, unxz_error
);
1203 if (writelen
== (int)dwSize
)
1205 Log("Decompress success 7za.xz(%u) ---> 7za.exe(%d)", dwSize
, g_unxz_len
);
1209 Log("Decompress failed 7za.xz(%u) ---> 7za.exe(%u)", dwSize
, dwSize
);
1212 SaveBuffer2File("ventoy\\7za.exe", RawBuffer
, (DWORD
)g_unxz_len
);
1214 g_unxz_buffer
= NULL
;
1220 Log("Failed to alloc 4MB memory");
1223 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", archive
);
1225 Log("extract inject to X:");
1226 Log("cmdline:<%s>", StrBuf
);
1228 GetStartupInfoA(&Si
);
1230 hOut
= CreateFileA("ventoy\\7z.log",
1232 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1235 FILE_ATTRIBUTE_NORMAL
,
1238 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
1240 if (hOut
!= INVALID_HANDLE_VALUE
)
1242 Si
.hStdError
= hOut
;
1243 Si
.hStdOutput
= hOut
;
1246 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
1247 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1250 // decompress tar archive, for tar.gz/tar.xz/tar.bz2
1252 if (check_tar_archive(archive
, tarName
))
1254 Log("Decompress tar archive...<%s>", tarName
);
1256 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", tarName
);
1258 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &NewPi
);
1259 WaitForSingleObject(NewPi
.hProcess
, INFINITE
);
1261 Log("Now delete %s", tarName
);
1262 DeleteFileA(tarName
);
1265 SAFE_CLOSE_HANDLE(hOut
);
1271 SAFE_CLOSE_HANDLE(hDrive
);
1276 static int ProcessUnattendedInstallation(const char *script
)
1282 CHAR CurDir
[MAX_PATH
];
1284 Log("Copy unattended XML ...");
1286 GetCurrentDirectory(sizeof(CurDir
), CurDir
);
1288 if ((Letter
>= 'A' && Letter
<= 'Z') || (Letter
>= 'a' && Letter
<= 'z'))
1290 Log("Current Drive Letter: %C", Letter
);
1297 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\Autounattend.xml", Letter
);
1298 Log("Copy file <%s> --> <%s>", script
, CurDir
);
1299 CopyFile(script
, CurDir
, FALSE
);
1301 Ret
= RegCreateKeyEx(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dw
);
1302 if (ERROR_SUCCESS
== Ret
)
1304 Ret
= RegSetValueEx(hKey
, "UnattendFile", 0, REG_SZ
, CurDir
, (DWORD
)(strlen(CurDir
) + 1));
1310 static int Windows11BypassCheck(const char *isofile
, const char MntLetter
)
1317 CHAR
*Buffer
= NULL
;
1318 VS_FIXEDFILEINFO
* VerInfo
= NULL
;
1319 CHAR CheckFile
[MAX_PATH
];
1320 UINT16 Major
, Minor
, Build
, Revision
;
1322 Log("Windows11BypassCheck for <%s> %C:", isofile
, MntLetter
);
1324 if (FALSE
== IsFileExist("%C:\\sources\\boot.wim", MntLetter
) ||
1325 FALSE
== IsFileExist("%C:\\sources\\compatresources.dll", MntLetter
))
1327 Log("boot.wim/compatresources.dll not exist, this is not a windows install media.");
1331 if (FALSE
== IsFileExist("%C:\\sources\\install.wim", MntLetter
) &&
1332 FALSE
== IsFileExist("%C:\\sources\\install.esd", MntLetter
))
1334 Log("install.wim/install.esd not exist, this is not a windows install media.");
1338 sprintf_s(CheckFile
, sizeof(CheckFile
), "%C:\\sources\\compatresources.dll", MntLetter
);
1339 dwSize
= GetFileVersionInfoSizeA(CheckFile
, &dwHandle
);
1342 Log("Failed to get file version info size: %u", LASTERR
);
1346 Buffer
= malloc(dwSize
);
1352 if (FALSE
== GetFileVersionInfoA(CheckFile
, dwHandle
, dwSize
, Buffer
))
1354 Log("Failed to get file version info : %u", LASTERR
);
1358 if (VerQueryValueA(Buffer
, "\\", (LPVOID
)&VerInfo
, &VerLen
) && VerLen
!= 0)
1360 if (VerInfo
->dwSignature
== VS_FFI_SIGNATURE
)
1362 Major
= HIWORD(VerInfo
->dwFileVersionMS
);
1363 Minor
= LOWORD(VerInfo
->dwFileVersionMS
);
1364 Build
= HIWORD(VerInfo
->dwFileVersionLS
);
1365 Revision
= LOWORD(VerInfo
->dwFileVersionLS
);
1367 Log("FileVersionze: <%u %u %u %u>", Major
, Minor
, Build
, Revision
);
1369 if (Major
== 10 && Build
> 20000)
1376 Log("This is not Windows 11, not need to bypass.", Major
);
1382 //Now we really need to bypass windows 11 check. create registry
1384 HKEY hSubKey
= NULL
;
1387 Status
= RegCreateKeyExA(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dwSize
);
1388 if (ERROR_SUCCESS
!= Status
)
1390 Log("Failed to create reg key System\\Setup %u %u", LASTERR
, Status
);
1394 Status
= RegCreateKeyExA(hKey
, "LabConfig", 0, NULL
, 0, KEY_SET_VALUE
| KEY_QUERY_VALUE
| KEY_CREATE_SUB_KEY
, NULL
, &hSubKey
, &dwSize
);
1395 if (ERROR_SUCCESS
!= Status
)
1397 Log("Failed to create LabConfig reg %u %u", LASTERR
, Status
);
1402 Status
+= RegSetValueExA(hSubKey
, "BypassRAMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1403 Status
+= RegSetValueExA(hSubKey
, "BypassTPMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1404 Status
+= RegSetValueExA(hSubKey
, "BypassSecureBootCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1405 Status
+= RegSetValueExA(hSubKey
, "BypassStorageCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1406 Status
+= RegSetValueExA(hSubKey
, "BypassCPUCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1408 Log("Create bypass registry %s %u", (Status
== ERROR_SUCCESS
) ? "SUCCESS" : "FAILED", Status
);
1421 static BOOL
CheckVentoyDisk(DWORD DiskNum
)
1425 UINT8 SectorBuf
[512];
1427 UINT8 check
[8] = { 0x56, 0x54, 0x00, 0x47, 0x65, 0x00, 0x48, 0x44 };
1429 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskNum
);
1430 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1431 if (Handle
== INVALID_HANDLE_VALUE
)
1433 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1437 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
1439 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
1440 CloseHandle(Handle
);
1444 CloseHandle(Handle
);
1446 if (memcmp(SectorBuf
+ 0x190, check
, 8) == 0)
1455 static int VentoyHook(ventoy_os_param
*param
)
1460 BOOL vtoyfind
= FALSE
;
1469 DISK_EXTENT DiskExtent
;
1470 DISK_EXTENT VtoyDiskExtent
;
1472 CHAR IsoPath
[MAX_PATH
];
1474 Log("VentoyHook Path:<%s>", param
->vtoy_img_path
);
1476 if (IsUTF8Encode(param
->vtoy_img_path
))
1478 Log("This file is UTF8 encoding\n");
1481 for (i
= 0; i
< 5; i
++)
1484 Drives
= GetLogicalDrives();
1485 Log("Logic Drives: 0x%x", Drives
);
1491 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
1492 if (IsFileExist("%s", IsoPath
))
1494 Log("File exist under %C:", Letter
);
1495 memset(UUID
, 0, sizeof(UUID
));
1496 memset(&DiskExtent
, 0, sizeof(DiskExtent
));
1497 if (GetPhyDiskUUID(Letter
, UUID
, NULL
, &DiskExtent
) == 0)
1499 if (memcmp(UUID
, param
->vtoy_disk_guid
, 16) == 0)
1501 Log("Disk UUID match");
1509 Log("File NOT exist under %C:", Letter
);
1523 Log("Now wait and retry ...");
1530 Log("Failed to find ISO file");
1534 Log("Find ISO file <%s>", IsoPath
);
1536 //Find VtoyLetter in Vlnk Mode
1537 if (g_os_param_reserved
[6] == 1)
1539 memcpy(&VtoySig
, g_os_param_reserved
+ 7, 4);
1540 for (i
= 0; i
< 5; i
++)
1543 Drives
= GetLogicalDrives();
1544 Log("Logic Drives: 0x%x VentoySig:%08X", Drives
, VtoySig
);
1550 memset(UUID
, 0, sizeof(UUID
));
1551 memset(&VtoyDiskExtent
, 0, sizeof(VtoyDiskExtent
));
1553 if (GetPhyDiskUUID(VtoyLetter
, UUID
, &DiskSig
, &VtoyDiskExtent
) == 0)
1555 Log("DiskSig=%08X PartStart=%lld", DiskSig
, VtoyDiskExtent
.StartingOffset
.QuadPart
);
1556 if (DiskSig
== VtoySig
&& VtoyDiskExtent
.StartingOffset
.QuadPart
== SIZE_1MB
)
1558 Log("Ventoy Disk Sig match");
1571 Log("Find Ventoy Letter: %C", VtoyLetter
);
1576 Log("Now wait and retry ...");
1581 if (vtoyfind
== FALSE
)
1583 Log("Failed to find ventoy disk");
1587 VtoyDiskNum
= VtoyDiskExtent
.DiskNumber
;
1591 VtoyLetter
= Letter
;
1592 Log("No vlnk mode %C", Letter
);
1594 VtoyDiskNum
= DiskExtent
.DiskNumber
;
1597 if (CheckVentoyDisk(VtoyDiskNum
))
1599 Log("Disk check OK %C: %u", VtoyLetter
, VtoyDiskNum
);
1603 Log("Failed to check ventoy disk %u", VtoyDiskNum
);
1607 Drives
= GetLogicalDrives();
1608 Log("Drives before mount: 0x%x", Drives
);
1610 rc
= MountIsoFile(IsoPath
, VtoyDiskNum
);
1612 NewDrives
= GetLogicalDrives();
1613 Log("Drives after mount: 0x%x (0x%x)", NewDrives
, (NewDrives
^ Drives
));
1616 NewDrives
= (NewDrives
^ Drives
);
1619 if (NewDrives
& 0x01)
1621 if ((NewDrives
>> 1) == 0)
1623 Log("The ISO file is mounted at %C:", MntLetter
);
1627 Log("Maybe the ISO file is mounted at %C:", MntLetter
);
1636 Log("Mount ISO FILE: %s", rc
== 0 ? "SUCCESS" : "FAILED");
1638 //Windows 11 bypass check
1639 if (g_windows_data
.windows11_bypass_check
== 1)
1641 Windows11BypassCheck(IsoPath
, MntLetter
);
1645 rc
= DeleteVentoyPart2MountPoint(VtoyDiskNum
);
1646 Log("Delete ventoy mountpoint: %s", rc
== 0 ? "SUCCESS" : "NO NEED");
1648 if (g_windows_data
.auto_install_script
[0])
1650 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", VtoyLetter
, g_windows_data
.auto_install_script
);
1651 if (IsFileExist("%s", IsoPath
))
1653 Log("use auto install script %s...", IsoPath
);
1654 ProcessUnattendedInstallation(IsoPath
);
1658 Log("auto install script %s not exist", IsoPath
);
1663 Log("auto install no need");
1666 if (g_windows_data
.injection_archive
[0])
1668 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", VtoyLetter
, g_windows_data
.injection_archive
);
1669 if (IsFileExist("%s", IsoPath
))
1671 Log("decompress injection archive %s...", IsoPath
);
1672 DecompressInjectionArchive(IsoPath
, VtoyDiskNum
);
1674 if (IsFileExist("%s", AUTO_RUN_BAT
))
1677 DWORD flags
= CREATE_NO_WINDOW
;
1680 PROCESS_INFORMATION Pi
;
1681 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
1683 Log("%s exist, now run it...", AUTO_RUN_BAT
);
1685 GetStartupInfoA(&Si
);
1687 hOut
= CreateFileA(AUTO_RUN_LOG
,
1689 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1692 FILE_ATTRIBUTE_NORMAL
,
1695 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
1696 if (hOut
!= INVALID_HANDLE_VALUE
)
1698 Si
.hStdError
= hOut
;
1699 Si
.hStdOutput
= hOut
;
1702 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
1703 sprintf_s(StrBuf
, sizeof(StrBuf
), "cmd.exe /c %s \"%s\" %C", AUTO_RUN_BAT
, IsoPath
, MntLetter
);
1704 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
1705 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1707 SAFE_CLOSE_HANDLE(hOut
);
1711 Log("%s not exist...", AUTO_RUN_BAT
);
1716 Log("injection archive %s not exist", IsoPath
);
1721 Log("no injection archive found");
1728 int VentoyJumpWimboot(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
1735 Log("VentoyJumpWimboot %dbit", g_system_bit
);
1737 sprintf_s(LunchFile
, MAX_PATH
, "X:\\setup.exe");
1739 ReadWholeFile2Buf("wimboot.data", &buf
, &size
);
1740 Log("wimboot.data size:%d", size
);
1742 memcpy(&g_os_param
, buf
, sizeof(ventoy_os_param
));
1743 memcpy(&g_windows_data
, buf
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
1744 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
1746 if (g_os_param_reserved
[0] == 1)
1748 Log("break here for debug .....");
1753 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
1755 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
1757 g_os_param
.vtoy_img_path
[Pos
] = '\\';
1761 if (g_os_param_reserved
[0] == 2)
1763 Log("skip hook for debug .....");
1768 rc
= VentoyHook(&g_os_param
);
1780 static int ventoy_check_create_directory(void)
1782 if (IsDirExist("ventoy"))
1784 Log("ventoy directory already exist");
1788 Log("ventoy directory not exist, now create it.");
1789 if (!CreateDirectoryA("ventoy", NULL
))
1791 Log("Failed to create ventoy directory err:%u", GetLastError());
1799 int VentoyJump(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
1806 DWORD LockStatus
= 0;
1807 BYTE
*Buffer
= NULL
;
1808 CHAR ExeFileName
[MAX_PATH
];
1810 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s", argv
[0]);
1811 if (!IsFileExist("%s", ExeFileName
))
1813 Log("File %s NOT exist, now try %s.exe", ExeFileName
, ExeFileName
);
1814 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s.exe", argv
[0]);
1816 Log("File %s exist ? %s", ExeFileName
, IsFileExist("%s", ExeFileName
) ? "YES" : "NO");
1819 if (ReadWholeFile2Buf(ExeFileName
, (void **)&Buffer
, &FileSize
))
1824 Log("VentoyJump %dbit", g_system_bit
);
1826 MUTEX_LOCK(g_vtoyins_mutex
);
1827 stat
= ventoy_check_create_directory();
1828 MUTEX_UNLOCK(g_vtoyins_mutex
);
1835 for (PeStart
= 0; PeStart
< FileSize
; PeStart
+= 16)
1837 if (CheckOsParam((ventoy_os_param
*)(Buffer
+ PeStart
)) &&
1838 CheckPeHead(Buffer
+ PeStart
+ sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
)))
1840 Log("Find os pararm at %u", PeStart
);
1842 memcpy(&g_os_param
, Buffer
+ PeStart
, sizeof(ventoy_os_param
));
1843 memcpy(&g_windows_data
, Buffer
+ PeStart
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
1844 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
1846 if (g_os_param_reserved
[0] == 1)
1848 Log("break here for debug .....");
1853 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
1855 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
1857 g_os_param
.vtoy_img_path
[Pos
] = '\\';
1861 PeStart
+= sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
);
1862 sprintf_s(LunchFile
, MAX_PATH
, "ventoy\\%s", GetFileNameInPath(ExeFileName
));
1864 MUTEX_LOCK(g_vtoyins_mutex
);
1865 if (IsFileExist("%s", LunchFile
))
1867 Log("vtoyjump multiple call ...");
1869 MUTEX_UNLOCK(g_vtoyins_mutex
);
1873 SaveBuffer2File(LunchFile
, Buffer
+ PeStart
, FileSize
- PeStart
);
1874 MUTEX_UNLOCK(g_vtoyins_mutex
);
1880 if (PeStart
>= FileSize
)
1882 Log("OS param not found");
1886 if (g_os_param_reserved
[0] == 2)
1888 Log("skip hook for debug .....");
1893 rc
= VentoyHook(&g_os_param
);
1906 int real_main(int argc
, char **argv
)
1910 CHAR NewFile
[MAX_PATH
];
1911 CHAR LunchFile
[MAX_PATH
];
1912 CHAR CallParam
[1024] = { 0 };
1914 PROCESS_INFORMATION Pi
;
1916 Log("#### real_main #### argc = %d", argc
);
1917 Log("program full path: <%s>", g_prog_full_path
);
1918 Log("program dir: <%s>", g_prog_dir
);
1919 Log("program name:: <%s>", g_prog_name
);
1921 Log("argc = %d", argc
);
1922 for (i
= 0; i
< argc
; i
++)
1924 Log("argv[%d]=<%s>", i
, argv
[i
]);
1927 strcat_s(CallParam
, sizeof(CallParam
), " ");
1928 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
1932 GetStartupInfoA(&Si
);
1933 memset(LunchFile
, 0, sizeof(LunchFile
));
1935 if (strstr(argv
[0], "vtoyjump.exe"))
1937 rc
= VentoyJumpWimboot(argc
, argv
, LunchFile
);
1941 rc
= VentoyJump(argc
, argv
, LunchFile
);
1944 Log("LunchFile=<%s> CallParam=<%s>", LunchFile
, CallParam
);
1946 if (_stricmp(g_prog_name
, "winpeshl.exe") != 0 && IsFileExist("ventoy\\%s", g_prog_name
))
1948 sprintf_s(NewFile
, sizeof(NewFile
), "%s_BACK.EXE", g_prog_full_path
);
1949 MoveFileA(g_prog_full_path
, NewFile
);
1950 Log("Move <%s> to <%s>", g_prog_full_path
, NewFile
);
1952 sprintf_s(NewFile
, sizeof(NewFile
), "ventoy\\%s", g_prog_name
);
1953 CopyFileA(NewFile
, g_prog_full_path
, TRUE
);
1954 Log("Copy <%s> to <%s>", NewFile
, g_prog_full_path
);
1956 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", g_prog_full_path
);
1957 Log("Final lunchFile is <%s>", LunchFile
);
1961 Log("We don't need to recover original <%s>", g_prog_name
);
1964 if (g_os_param_reserved
[0] == 3)
1966 Log("Open log for debug ...");
1967 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "notepad.exe ventoy.log");
1973 strcat_s(LunchFile
, sizeof(LunchFile
), CallParam
);
1975 else if (NULL
== strstr(LunchFile
, "setup.exe"))
1977 Log("Not setup.exe, hide windows.");
1978 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
1979 Si
.wShowWindow
= SW_HIDE
;
1982 Log("Ventoy jump %s ...", rc
== 0 ? "success" : "failed");
1985 Log("Now launch <%s> ...", LunchFile
);
1987 if (g_os_param_reserved
[0] == 4)
1989 Log("Open cmd for debug ...");
1990 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "cmd.exe");
1993 Log("Backup log at this point");
1994 CopyFileA(LOG_FILE
, "X:\\Windows\\ventoy.backup", TRUE
);
1996 CreateProcessA(NULL
, LunchFile
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1998 for (i
= 0; rc
&& i
< 1800; i
++)
2000 Log("Ventoy hook failed, now wait and retry ...");
2002 rc
= VentoyHook(&g_os_param
);
2005 Log("Wait process...");
2006 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2008 Log("vtoyjump finished");
2012 static void VentoyToUpper(CHAR
*str
)
2015 for (i
= 0; str
[i
]; i
++)
2017 str
[i
] = (CHAR
)toupper(str
[i
]);
2021 int main(int argc
, char **argv
)
2025 PROCESS_INFORMATION Pi
;
2026 CHAR CurDir
[MAX_PATH
];
2027 CHAR NewArgv0
[MAX_PATH
];
2028 CHAR CallParam
[1024] = { 0 };
2030 g_vtoylog_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYLOG_LOCK");
2031 g_vtoyins_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYINS_LOCK");
2033 Log("######## VentoyJump %dbit ##########", g_system_bit
);
2035 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
2036 Log("Current directory is <%s>", CurDir
);
2038 GetModuleFileNameA(NULL
, g_prog_full_path
, MAX_PATH
);
2039 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
2041 Log("EXE path: <%s> dir:<%s> name:<%s>", g_prog_full_path
, g_prog_dir
, g_prog_name
);
2043 if (_stricmp(g_prog_name
, "WinLogon.exe") == 0)
2045 Log("This time is rejump back ...");
2047 strcpy_s(g_prog_full_path
, sizeof(g_prog_full_path
), argv
[1]);
2048 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
2050 return real_main(argc
- 1, argv
+ 1);
2052 else if (_stricmp(g_prog_name
, "PECMD.exe") == 0)
2054 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_dir
);
2055 VentoyToUpper(NewArgv0
);
2057 if (NULL
== strstr(NewArgv0
, "SYSTEM32") && IsFileExist(ORG_PECMD_BK_PATH
))
2059 Log("Just call original pecmd.exe");
2060 strcpy_s(CallParam
, sizeof(CallParam
), ORG_PECMD_PATH
);
2064 Log("We need to rejump for pecmd ...");
2066 ventoy_check_create_directory();
2067 CopyFileA(g_prog_full_path
, "ventoy\\WinLogon.exe", TRUE
);
2069 sprintf_s(CallParam
, sizeof(CallParam
), "ventoy\\WinLogon.exe %s", g_prog_full_path
);
2072 for (i
= 1; i
< argc
; i
++)
2074 strcat_s(CallParam
, sizeof(CallParam
), " ");
2075 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
2078 Log("Now rejump to <%s> ...", CallParam
);
2079 GetStartupInfoA(&Si
);
2080 CreateProcessA(NULL
, CallParam
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
2082 Log("Wait rejump process...");
2083 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2084 Log("rejump finished");
2089 Log("We don't need to rejump ...");
2091 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_full_path
);
2094 return real_main(argc
, argv
);