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 AUTO_RUN_BAT "X:\\VentoyAutoRun.bat"
44 #define AUTO_RUN_LOG "X:\\VentoyAutoRun.log"
46 #define LOG_FILE "X:\\Windows\\system32\\ventoy.log"
47 #define MUTEX_LOCK(hmutex) if (hmutex != NULL) LockStatus = WaitForSingleObject(hmutex, INFINITE)
48 #define MUTEX_UNLOCK(hmutex) if (hmutex != NULL && WAIT_OBJECT_0 == LockStatus) ReleaseMutex(hmutex)
50 static const char * GetFileNameInPath(const char *fullpath
)
54 if (strstr(fullpath
, ":"))
56 for (i
= (int)strlen(fullpath
); i
> 0; i
--)
58 if (fullpath
[i
- 1] == '/' || fullpath
[i
- 1] == '\\')
68 static int split_path_name(char *fullpath
, char *dir
, char *name
)
73 Pos
= (CHAR
*)GetFileNameInPath(fullpath
);
75 strcpy_s(name
, MAX_PATH
, Pos
);
79 strcpy_s(dir
, MAX_PATH
, fullpath
);
86 void Log(const char *Fmt
, ...)
94 DWORD PID
= GetCurrentProcessId();
97 Len
+= sprintf_s(szBuf
, sizeof(szBuf
),
98 "[%4d/%02d/%02d %02d:%02d:%02d.%03d] [%u] ",
99 Sys
.wYear
, Sys
.wMonth
, Sys
.wDay
,
100 Sys
.wHour
, Sys
.wMinute
, Sys
.wSecond
,
101 Sys
.wMilliseconds
, PID
);
104 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
107 MUTEX_LOCK(g_vtoylog_mutex
);
109 fopen_s(&File
, LOG_FILE
, "a+");
112 fwrite(szBuf
, 1, Len
, File
);
113 fwrite("\n", 1, 1, File
);
117 MUTEX_UNLOCK(g_vtoylog_mutex
);
121 static int LoadNtDriver(const char *DrvBinPath
)
127 SC_HANDLE hServiceMgr
;
129 char name
[256] = { 0 };
131 for (i
= (int)strlen(DrvBinPath
) - 1; i
>= 0; i
--)
133 if (DrvBinPath
[i
] == '\\' || DrvBinPath
[i
] == '/')
135 sprintf_s(name
, sizeof(name
), "%s", DrvBinPath
+ i
+ 1);
140 Log("Load NT driver: %s %s", DrvBinPath
, name
);
142 hServiceMgr
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
143 if (hServiceMgr
== NULL
)
145 Log("OpenSCManager failed Error:%u", GetLastError());
149 Log("OpenSCManager OK");
151 hService
= CreateServiceA(hServiceMgr
,
155 SERVICE_KERNEL_DRIVER
,
156 SERVICE_DEMAND_START
,
157 SERVICE_ERROR_NORMAL
,
159 NULL
, NULL
, NULL
, NULL
, NULL
);
160 if (hService
== NULL
)
162 Status
= GetLastError();
163 if (Status
!= ERROR_IO_PENDING
&& Status
!= ERROR_SERVICE_EXISTS
)
165 Log("CreateService failed v %u", Status
);
166 CloseServiceHandle(hServiceMgr
);
170 hService
= OpenServiceA(hServiceMgr
, name
, SERVICE_ALL_ACCESS
);
171 if (hService
== NULL
)
173 Log("OpenService failed %u", Status
);
174 CloseServiceHandle(hServiceMgr
);
179 Log("CreateService imdisk OK");
181 Ret
= StartServiceA(hService
, 0, NULL
);
184 Log("StartService OK");
188 Status
= GetLastError();
189 if (Status
== ERROR_SERVICE_ALREADY_RUNNING
)
195 Log("StartService error %u", Status
);
200 CloseServiceHandle(hService
);
201 CloseServiceHandle(hServiceMgr
);
203 Log("Load NT driver %s", rc
? "failed" : "success");
208 static int ReadWholeFile2Buf(const char *Fullpath
, void **Data
, DWORD
*Size
)
216 Log("ReadWholeFile2Buf <%s>", Fullpath
);
218 Handle
= CreateFileA(Fullpath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
219 if (Handle
== INVALID_HANDLE_VALUE
)
221 Log("Could not open the file<%s>, error:%u", Fullpath
, GetLastError());
225 FileSize
= SetFilePointer(Handle
, 0, NULL
, FILE_END
);
227 Buffer
= malloc(FileSize
);
230 Log("Failed to alloc memory size:%u", FileSize
);
234 SetFilePointer(Handle
, 0, NULL
, FILE_BEGIN
);
235 if (!ReadFile(Handle
, Buffer
, FileSize
, &dwSize
, NULL
))
237 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
244 Log("Success read file size:%u", FileSize
);
249 SAFE_CLOSE_HANDLE(Handle
);
254 static BOOL
CheckPeHead(BYTE
*Head
)
258 if (Head
[0] != 'M' || Head
[1] != 'Z')
263 PeOffset
= *(UINT32
*)(Head
+ 60);
264 if (*(UINT32
*)(Head
+ PeOffset
) != 0x00004550)
273 static BOOL
CheckOsParam(ventoy_os_param
*param
)
278 if (memcmp(¶m
->guid
, &g_ventoy_guid
, sizeof(ventoy_guid
)))
283 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
285 Sum
+= *((BYTE
*)param
+ i
);
293 if (param
->vtoy_img_location_addr
% 4096)
301 static int SaveBuffer2File(const char *Fullpath
, void *Buffer
, DWORD Length
)
307 Log("SaveBuffer2File <%s> len:%u", Fullpath
, Length
);
309 Handle
= CreateFileA(Fullpath
, GENERIC_READ
| GENERIC_WRITE
,
310 FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, CREATE_NEW
, 0, 0);
311 if (Handle
== INVALID_HANDLE_VALUE
)
313 Log("Could not create new file, error:%u", GetLastError());
317 WriteFile(Handle
, Buffer
, Length
, &dwSize
, NULL
);
322 SAFE_CLOSE_HANDLE(Handle
);
327 static int IsUTF8Encode(const char *src
)
330 const UCHAR
*Byte
= (const UCHAR
*)src
;
332 for (i
= 0; i
< MAX_PATH
&& Byte
[i
]; i
++)
343 static int Utf8ToUtf16(const char* src
, WCHAR
* dst
)
345 int size
= MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, 0);
346 return MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, size
+ 1);
349 static BOOL
IsDirExist(const char *Fmt
, ...)
354 CHAR FilePathA
[MAX_PATH
];
355 WCHAR FilePathW
[MAX_PATH
];
358 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
361 UTF8
= IsUTF8Encode(FilePathA
);
365 Utf8ToUtf16(FilePathA
, FilePathW
);
366 Attr
= GetFileAttributesW(FilePathW
);
370 Attr
= GetFileAttributesA(FilePathA
);
373 if (Attr
!= INVALID_FILE_ATTRIBUTES
&& (Attr
& FILE_ATTRIBUTE_DIRECTORY
))
381 static BOOL
IsFileExist(const char *Fmt
, ...)
388 CHAR FilePathA
[MAX_PATH
];
389 WCHAR FilePathW
[MAX_PATH
];
392 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
395 UTF8
= IsUTF8Encode(FilePathA
);
399 Utf8ToUtf16(FilePathA
, FilePathW
);
400 hFile
= CreateFileW(FilePathW
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
404 hFile
= CreateFileA(FilePathA
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
406 if (INVALID_HANDLE_VALUE
== hFile
)
415 Attr
= GetFileAttributesW(FilePathW
);
419 Attr
= GetFileAttributesA(FilePathA
);
422 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
430 Log("File <%s> %s", FilePathA
, (bRet
? "exist" : "NOT exist"));
434 static int GetPhyDiskUUID(const char LogicalDrive
, UINT8
*UUID
, DISK_EXTENT
*DiskExtent
)
439 VOLUME_DISK_EXTENTS DiskExtents
;
441 UINT8 SectorBuf
[512];
443 Log("GetPhyDiskUUID %C", LogicalDrive
);
445 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", LogicalDrive
);
446 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
447 if (Handle
== INVALID_HANDLE_VALUE
)
449 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
453 Ret
= DeviceIoControl(Handle
,
454 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
458 (DWORD
)(sizeof(DiskExtents
)),
461 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
463 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed, error:%u", GetLastError());
469 memcpy(DiskExtent
, DiskExtents
.Extents
, sizeof(DiskExtent
));
470 Log("%C: is in PhysicalDrive%d ", LogicalDrive
, DiskExtents
.Extents
[0].DiskNumber
);
472 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskExtents
.Extents
[0].DiskNumber
);
473 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
474 if (Handle
== INVALID_HANDLE_VALUE
)
476 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
480 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
482 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
487 memcpy(UUID
, SectorBuf
+ 0x180, 16);
492 static int VentoyMountAnywhere(HANDLE Handle
)
495 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
497 Log("VentoyMountAnywhere");
499 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
500 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
502 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
503 if (Status
!= ERROR_SUCCESS
)
505 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
512 int VentoyMountY(HANDLE Handle
)
517 DWORD physicalDriveNameSize
;
519 WCHAR physicalDriveName
[MAX_PATH
];
520 CHAR physicalDriveNameA
[MAX_PATH
];
521 CHAR cdromDriveName
[MAX_PATH
];
522 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
526 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
527 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
529 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
);
530 if (Status
!= ERROR_SUCCESS
)
532 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
536 memset(physicalDriveName
, 0, sizeof(physicalDriveName
));
537 memset(physicalDriveNameA
, 0, sizeof(physicalDriveNameA
));
539 physicalDriveNameSize
= MAX_PATH
;
540 Status
= GetVirtualDiskPhysicalPath(Handle
, &physicalDriveNameSize
, physicalDriveName
);
541 if (Status
!= ERROR_SUCCESS
)
543 Log("Failed GetVirtualDiskPhysicalPath ErrorCode:%u", Status
);
547 for (i
= 0; physicalDriveName
[i
]; i
++)
549 physicalDriveNameA
[i
] = (CHAR
)toupper((CHAR
)(physicalDriveName
[i
]));
552 Log("physicalDriveNameA=<%s>", physicalDriveNameA
);
554 Pos
= strstr(physicalDriveNameA
, "CDROM");
557 Log("Not cdrom phy drive");
561 sprintf_s(cdromDriveName
, sizeof(cdromDriveName
), "\\Device\\%s", Pos
);
562 Log("cdromDriveName=<%s>", cdromDriveName
);
564 for (i
= 0; i
< 3 && (bRet
== FALSE
); i
++)
567 bRet
= DefineDosDeviceA(DDD_RAW_TARGET_PATH
, "Y:", cdromDriveName
);
568 Log("DefineDosDeviceA %s", bRet
? "success" : "failed");
574 static BOOL
VentoyAPINeedMountY(const char *IsoPath
)
582 static int VentoyAttachVirtualDisk(HANDLE Handle
, const char *IsoPath
)
587 Drives
= GetLogicalDrives();
588 if ((1 << 24) & Drives
)
590 Log("Y: is occupied");
595 Log("Y: is free now");
599 if (DriveYFree
&& VentoyAPINeedMountY(IsoPath
))
601 return VentoyMountY(Handle
);
605 return VentoyMountAnywhere(Handle
);
609 int VentoyMountISOByAPI(const char *IsoPath
)
614 WCHAR wFilePath
[512] = { 0 };
615 VIRTUAL_STORAGE_TYPE StorageType
;
616 OPEN_VIRTUAL_DISK_PARAMETERS OpenParameters
;
618 Log("VentoyMountISOByAPI <%s>", IsoPath
);
620 if (IsUTF8Encode(IsoPath
))
622 Log("This is UTF8 encoding");
623 MultiByteToWideChar(CP_UTF8
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
627 Log("This is ANSI encoding");
628 MultiByteToWideChar(CP_ACP
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
631 memset(&StorageType
, 0, sizeof(StorageType
));
632 memset(&OpenParameters
, 0, sizeof(OpenParameters
));
634 OpenParameters
.Version
= OPEN_VIRTUAL_DISK_VERSION_1
;
636 for (i
= 0; i
< 10; i
++)
638 Status
= OpenVirtualDisk(&StorageType
, wFilePath
, VIRTUAL_DISK_ACCESS_READ
, 0, &OpenParameters
, &Handle
);
639 if (ERROR_FILE_NOT_FOUND
== Status
|| ERROR_PATH_NOT_FOUND
== Status
)
641 Log("OpenVirtualDisk ErrorCode:%u, now wait and retry...", Status
);
646 if (ERROR_SUCCESS
== Status
)
648 Log("OpenVirtualDisk success");
650 else if (ERROR_VIRTDISK_PROVIDER_NOT_FOUND
== Status
)
652 Log("VirtualDisk for ISO file is not supported in current system");
656 Log("Failed to open virtual disk ErrorCode:%u", Status
);
662 if (Status
!= ERROR_SUCCESS
)
667 Log("OpenVirtualDisk success");
669 Status
= VentoyAttachVirtualDisk(Handle
, IsoPath
);
670 if (Status
!= ERROR_SUCCESS
)
672 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
677 Log("VentoyAttachVirtualDisk success");
684 static HANDLE g_FatPhyDrive
;
685 static UINT64 g_Part2StartSec
;
687 static int CopyFileFromFatDisk(const CHAR
* SrcFile
, const CHAR
*DstFile
)
694 Log("CopyFileFromFatDisk (%s)==>(%s)", SrcFile
, DstFile
);
696 flfile
= fl_fopen(SrcFile
, "rb");
699 fl_fseek(flfile
, 0, SEEK_END
);
700 size
= (int)fl_ftell(flfile
);
701 fl_fseek(flfile
, 0, SEEK_SET
);
703 buf
= (char *)malloc(size
);
706 fl_fread(buf
, 1, size
, flfile
);
709 SaveBuffer2File(DstFile
, buf
, size
);
719 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
724 LARGE_INTEGER liCurrentPosition
;
726 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
727 liCurrentPosition
.QuadPart
*= 512;
728 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
730 ReadSize
= (DWORD
)(SectorCount
* 512);
732 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
733 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
735 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet
, ReadSize
, dwSize
, GetLastError());
741 static BOOL
Is2K10PE(void)
747 fopen_s(&fp
, "X:\\Windows\\System32\\PECMD.INI", "r");
753 memset(szLine
, 0, sizeof(szLine
));
754 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
756 if (strstr(szLine
, "2k10\\"))
767 static CHAR
GetIMDiskMountLogicalDrive(void)
771 DWORD Mask
= 0x1000000;
773 // fixed use M as mountpoint for 2K10 PE
776 Log("Use M: for 2K10 PE");
780 Drives
= GetLogicalDrives();
781 Log("Drives=0x%x", Drives
);
785 if ((Drives
& Mask
) == 0)
797 UINT64
GetVentoyEfiPartStartSector(HANDLE hDrive
)
802 VTOY_GPT_INFO
*pGpt
= NULL
;
803 UINT64 StartSector
= 0;
805 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
807 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
808 Log("Read MBR Ret:%u Size:%u code:%u", bRet
, dwSize
, LASTERR
);
810 if ((!bRet
) || (dwSize
!= sizeof(MBR
)))
815 if (MBR
.PartTbl
[0].FsFlag
== 0xEE)
817 Log("GPT partition style");
819 pGpt
= malloc(sizeof(VTOY_GPT_INFO
));
825 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
826 bRet
= ReadFile(hDrive
, pGpt
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
827 if ((!bRet
) || (dwSize
!= sizeof(VTOY_GPT_INFO
)))
829 Log("Failed to read gpt info %d %u %d", bRet
, dwSize
, LASTERR
);
833 StartSector
= pGpt
->PartTbl
[1].StartLBA
;
838 Log("MBR partition style");
839 StartSector
= MBR
.PartTbl
[1].StartSectorId
;
842 Log("GetVentoyEfiPart StartSector: %llu", StartSector
);
846 static int VentoyRunImdisk(const char *IsoPath
, const char *imdiskexe
)
851 PROCESS_INFORMATION Pi
;
853 Log("VentoyRunImdisk <%s> <%s>", IsoPath
, imdiskexe
);
855 Letter
= GetIMDiskMountLogicalDrive();
856 sprintf_s(Cmdline
, sizeof(Cmdline
), "%s -a -o ro -f \"%s\" -m %C:", imdiskexe
, IsoPath
, Letter
);
857 Log("mount iso to %C: use imdisk cmd <%s>", Letter
, Cmdline
);
859 if (IsUTF8Encode(IsoPath
))
862 GetStartupInfoW(&Si
);
863 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
864 Si
.wShowWindow
= SW_HIDE
;
866 Utf8ToUtf16(Cmdline
, CmdlineW
);
867 CreateProcessW(NULL
, CmdlineW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
869 Log("This is UTF8 encoding");
874 GetStartupInfoA(&Si
);
875 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
876 Si
.wShowWindow
= SW_HIDE
;
878 CreateProcessA(NULL
, Cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
880 Log("This is ANSI encoding");
883 Log("Wait for imdisk process ...");
884 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
885 Log("imdisk process finished");
890 int VentoyMountISOByImdisk(const char *IsoPath
, DWORD PhyDrive
)
896 CHAR PhyPath
[MAX_PATH
];
897 GET_LENGTH_INFORMATION LengthInfo
;
899 Log("VentoyMountISOByImdisk %s", IsoPath
);
901 if (IsFileExist("X:\\Windows\\System32\\imdisk.exe"))
903 Log("imdisk.exe exist, use it directly...");
904 VentoyRunImdisk(IsoPath
, "imdisk.exe");
908 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
909 hDrive
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
910 if (hDrive
== INVALID_HANDLE_VALUE
)
912 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
916 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
919 Log("Could not get phy disk %s size, error:%u", PhyPath
, GetLastError());
923 g_FatPhyDrive
= hDrive
;
924 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
926 Log("Parse FAT fs...");
930 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
932 if (g_system_bit
== 64)
934 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.sys", "ventoy\\imdisk.sys");
935 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.exe", "ventoy\\imdisk.exe");
936 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.cpl", "ventoy\\imdisk.cpl");
940 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.sys", "ventoy\\imdisk.sys");
941 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");
942 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");
945 GetCurrentDirectoryA(sizeof(PhyPath
), PhyPath
);
946 strcat_s(PhyPath
, sizeof(PhyPath
), "\\ventoy\\imdisk.sys");
948 if (LoadNtDriver(PhyPath
) == 0)
950 VentoyRunImdisk(IsoPath
, "ventoy\\imdisk.exe");
958 SAFE_CLOSE_HANDLE(hDrive
);
963 static int MountIsoFile(CONST CHAR
*IsoPath
, DWORD PhyDrive
)
965 if (IsWindows8OrGreater())
967 Log("This is Windows 8 or latter...");
968 if (VentoyMountISOByAPI(IsoPath
) == 0)
970 Log("Mount iso by API success");
975 Log("Mount iso by API failed, maybe not supported, try imdisk");
976 return VentoyMountISOByImdisk(IsoPath
, PhyDrive
);
981 Log("This is before Windows 8 ...");
982 if (VentoyMountISOByImdisk(IsoPath
, PhyDrive
) == 0)
984 Log("Mount iso by imdisk success");
989 return VentoyMountISOByAPI(IsoPath
);
994 static int GetPhyDriveByLogicalDrive(int DriveLetter
)
999 VOLUME_DISK_EXTENTS DiskExtents
;
1002 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", (CHAR
)DriveLetter
);
1004 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1005 if (Handle
== INVALID_HANDLE_VALUE
)
1007 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1011 Ret
= DeviceIoControl(Handle
,
1012 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
1016 (DWORD
)(sizeof(DiskExtents
)),
1020 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
1022 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, GetLastError());
1023 SAFE_CLOSE_HANDLE(Handle
);
1026 SAFE_CLOSE_HANDLE(Handle
);
1028 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
1030 DiskExtents
.Extents
[0].DiskNumber
,
1031 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
1032 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
1035 return (int)DiskExtents
.Extents
[0].DiskNumber
;
1039 static int DeleteVentoyPart2MountPoint(DWORD PhyDrive
)
1044 CHAR DriveName
[] = "?:\\";
1046 Log("DeleteVentoyPart2MountPoint Phy%u ...", PhyDrive
);
1048 Drives
= GetLogicalDrives();
1051 if ((Drives
& 0x01) && IsFileExist("%C:\\ventoy\\ventoy.cpio", Letter
))
1053 Log("File %C:\\ventoy\\ventoy.cpio exist", Letter
);
1055 PhyDisk
= GetPhyDriveByLogicalDrive(Letter
);
1056 Log("PhyDisk=%u for %C", PhyDisk
, Letter
);
1058 if (PhyDisk
== PhyDrive
)
1060 DriveName
[0] = Letter
;
1061 DeleteVolumeMountPointA(DriveName
);
1073 static BOOL
check_tar_archive(const char *archive
, CHAR
*tarName
)
1077 const char *pos
= archive
;
1078 const char *slash
= archive
;
1082 if (*pos
== '\\' || *pos
== '/')
1089 len
= (int)strlen(slash
);
1091 if (len
> 7 && (strncmp(slash
+ len
- 7, ".tar.gz", 7) == 0 || strncmp(slash
+ len
- 7, ".tar.xz", 7) == 0))
1093 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1094 tarName
[nameLen
- 3] = 0;
1097 else if (len
> 8 && strncmp(slash
+ len
- 8, ".tar.bz2", 8) == 0)
1099 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1100 tarName
[nameLen
- 4] = 0;
1103 else if (len
> 9 && strncmp(slash
+ len
- 9, ".tar.lzma", 9) == 0)
1105 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1106 tarName
[nameLen
- 5] = 0;
1113 static UCHAR
*g_unxz_buffer
= NULL
;
1114 static int g_unxz_len
= 0;
1116 static void unxz_error(char *x
)
1121 static int unxz_flush(void *src
, unsigned int size
)
1123 memcpy(g_unxz_buffer
+ g_unxz_len
, src
, size
);
1124 g_unxz_len
+= (int)size
;
1129 static int DecompressInjectionArchive(const char *archive
, DWORD PhyDrive
)
1133 UCHAR
*Buffer
= NULL
;
1134 UCHAR
*RawBuffer
= NULL
;
1140 DWORD flags
= CREATE_NO_WINDOW
;
1141 CHAR StrBuf
[MAX_PATH
];
1142 CHAR tarName
[MAX_PATH
];
1144 PROCESS_INFORMATION Pi
;
1145 PROCESS_INFORMATION NewPi
;
1146 GET_LENGTH_INFORMATION LengthInfo
;
1147 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
1149 Log("DecompressInjectionArchive %s", archive
);
1151 sprintf_s(StrBuf
, sizeof(StrBuf
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
1152 hDrive
= CreateFileA(StrBuf
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1153 if (hDrive
== INVALID_HANDLE_VALUE
)
1155 Log("Could not open the disk<%s>, error:%u", StrBuf
, GetLastError());
1159 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
1162 Log("Could not get phy disk %s size, error:%u", StrBuf
, GetLastError());
1166 g_FatPhyDrive
= hDrive
;
1167 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
1169 Log("Parse FAT fs...");
1173 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
1175 if (g_system_bit
== 64)
1177 CopyFileFromFatDisk("/ventoy/7z/64/7za.xz", "ventoy\\7za.xz");
1181 CopyFileFromFatDisk("/ventoy/7z/32/7za.xz", "ventoy\\7za.xz");
1184 ReadWholeFile2Buf("ventoy\\7za.xz", &Buffer
, &dwSize
);
1185 Log("7za.xz file size:%u", dwSize
);
1187 RawBuffer
= malloc(SIZE_1MB
* 4);
1190 g_unxz_buffer
= RawBuffer
;
1192 unxz(Buffer
, (int)dwSize
, NULL
, unxz_flush
, NULL
, &writelen
, unxz_error
);
1193 if (writelen
== (int)dwSize
)
1195 Log("Decompress success 7za.xz(%u) ---> 7za.exe(%d)", dwSize
, g_unxz_len
);
1199 Log("Decompress failed 7za.xz(%u) ---> 7za.exe(%u)", dwSize
, dwSize
);
1202 SaveBuffer2File("ventoy\\7za.exe", RawBuffer
, (DWORD
)g_unxz_len
);
1204 g_unxz_buffer
= NULL
;
1210 Log("Failed to alloc 4MB memory");
1213 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", archive
);
1215 Log("extract inject to X:");
1216 Log("cmdline:<%s>", StrBuf
);
1218 GetStartupInfoA(&Si
);
1220 hOut
= CreateFileA("ventoy\\7z.log",
1222 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1225 FILE_ATTRIBUTE_NORMAL
,
1228 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
1230 if (hOut
!= INVALID_HANDLE_VALUE
)
1232 Si
.hStdError
= hOut
;
1233 Si
.hStdOutput
= hOut
;
1236 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
1237 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1240 // decompress tar archive, for tar.gz/tar.xz/tar.bz2
1242 if (check_tar_archive(archive
, tarName
))
1244 Log("Decompress tar archive...<%s>", tarName
);
1246 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", tarName
);
1248 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &NewPi
);
1249 WaitForSingleObject(NewPi
.hProcess
, INFINITE
);
1251 Log("Now delete %s", tarName
);
1252 DeleteFileA(tarName
);
1255 SAFE_CLOSE_HANDLE(hOut
);
1261 SAFE_CLOSE_HANDLE(hDrive
);
1266 static int ProcessUnattendedInstallation(const char *script
)
1272 CHAR CurDir
[MAX_PATH
];
1274 Log("Copy unattended XML ...");
1276 GetCurrentDirectory(sizeof(CurDir
), CurDir
);
1278 if ((Letter
>= 'A' && Letter
<= 'Z') || (Letter
>= 'a' && Letter
<= 'z'))
1280 Log("Current Drive Letter: %C", Letter
);
1287 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\Autounattend.xml", Letter
);
1288 Log("Copy file <%s> --> <%s>", script
, CurDir
);
1289 CopyFile(script
, CurDir
, FALSE
);
1291 Ret
= RegCreateKeyEx(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dw
);
1292 if (ERROR_SUCCESS
== Ret
)
1294 Ret
= RegSetValueEx(hKey
, "UnattendFile", 0, REG_SZ
, CurDir
, (DWORD
)(strlen(CurDir
) + 1));
1300 static int Windows11BypassCheck(const char *isofile
, const char MntLetter
)
1307 CHAR
*Buffer
= NULL
;
1308 VS_FIXEDFILEINFO
* VerInfo
= NULL
;
1309 CHAR CheckFile
[MAX_PATH
];
1310 UINT16 Major
, Minor
, Build
, Revision
;
1312 Log("Windows11BypassCheck for <%s> %C:", isofile
, MntLetter
);
1314 if (FALSE
== IsFileExist("%C:\\sources\\boot.wim", MntLetter
) ||
1315 FALSE
== IsFileExist("%C:\\sources\\compatresources.dll", MntLetter
))
1317 Log("boot.wim/compatresources.dll not exist, this is not a windows install media.");
1321 if (FALSE
== IsFileExist("%C:\\sources\\install.wim", MntLetter
) &&
1322 FALSE
== IsFileExist("%C:\\sources\\install.esd", MntLetter
))
1324 Log("install.wim/install.esd not exist, this is not a windows install media.");
1328 sprintf_s(CheckFile
, sizeof(CheckFile
), "%C:\\sources\\compatresources.dll", MntLetter
);
1329 dwSize
= GetFileVersionInfoSizeA(CheckFile
, &dwHandle
);
1332 Log("Failed to get file version info size: %u", LASTERR
);
1336 Buffer
= malloc(dwSize
);
1342 if (FALSE
== GetFileVersionInfoA(CheckFile
, dwHandle
, dwSize
, Buffer
))
1344 Log("Failed to get file version info : %u", LASTERR
);
1348 if (VerQueryValueA(Buffer
, "\\", (LPVOID
)&VerInfo
, &VerLen
) && VerLen
!= 0)
1350 if (VerInfo
->dwSignature
== VS_FFI_SIGNATURE
)
1352 Major
= HIWORD(VerInfo
->dwFileVersionMS
);
1353 Minor
= LOWORD(VerInfo
->dwFileVersionMS
);
1354 Build
= HIWORD(VerInfo
->dwFileVersionLS
);
1355 Revision
= LOWORD(VerInfo
->dwFileVersionLS
);
1357 Log("FileVersionze: <%u %u %u %u>", Major
, Minor
, Build
, Revision
);
1359 if (Major
== 10 && Build
> 20000)
1366 Log("This is not Windows 11, not need to bypass.", Major
);
1372 //Now we really need to bypass windows 11 check. create registry
1374 HKEY hSubKey
= NULL
;
1377 Status
= RegCreateKeyExA(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dwSize
);
1378 if (ERROR_SUCCESS
!= Status
)
1380 Log("Failed to create reg key System\\Setup %u %u", LASTERR
, Status
);
1384 Status
= RegCreateKeyExA(hKey
, "LabConfig", 0, NULL
, 0, KEY_SET_VALUE
| KEY_QUERY_VALUE
| KEY_CREATE_SUB_KEY
, NULL
, &hSubKey
, &dwSize
);
1385 if (ERROR_SUCCESS
!= Status
)
1387 Log("Failed to create LabConfig reg %u %u", LASTERR
, Status
);
1392 Status
+= RegSetValueExA(hSubKey
, "BypassRAMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1393 Status
+= RegSetValueExA(hSubKey
, "BypassTPMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1394 Status
+= RegSetValueExA(hSubKey
, "BypassSecureBootCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1395 Status
+= RegSetValueExA(hSubKey
, "BypassStorageCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1396 Status
+= RegSetValueExA(hSubKey
, "BypassCPUCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1398 Log("Create bypass registry %s %u", (Status
== ERROR_SUCCESS
) ? "SUCCESS" : "FAILED", Status
);
1411 static int VentoyHook(ventoy_os_param
*param
)
1420 DISK_EXTENT DiskExtent
;
1422 CHAR IsoPath
[MAX_PATH
];
1424 Log("VentoyHook Path:<%s>", param
->vtoy_img_path
);
1426 if (IsUTF8Encode(param
->vtoy_img_path
))
1428 Log("This file is UTF8 encoding\n");
1431 for (i
= 0; i
< 5; i
++)
1434 Drives
= GetLogicalDrives();
1435 Log("Logic Drives: 0x%x", Drives
);
1441 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
1442 if (IsFileExist("%s", IsoPath
))
1444 Log("File exist under %C:", Letter
);
1445 if (GetPhyDiskUUID(Letter
, UUID
, &DiskExtent
) == 0)
1447 if (memcmp(UUID
, param
->vtoy_disk_guid
, 16) == 0)
1449 Log("Disk UUID match");
1457 Log("File NOT exist under %C:", Letter
);
1471 Log("Now wait and retry ...");
1478 Log("Failed to find ISO file");
1482 Log("Find ISO file <%s>", IsoPath
);
1484 Drives
= GetLogicalDrives();
1485 Log("Drives before mount: 0x%x", Drives
);
1487 rc
= MountIsoFile(IsoPath
, DiskExtent
.DiskNumber
);
1489 NewDrives
= GetLogicalDrives();
1490 Log("Drives after mount: 0x%x (0x%x)", NewDrives
, (NewDrives
^ Drives
));
1493 NewDrives
= (NewDrives
^ Drives
);
1496 if (NewDrives
& 0x01)
1498 if ((NewDrives
>> 1) == 0)
1500 Log("The ISO file is mounted at %C:", MntLetter
);
1504 Log("Maybe the ISO file is mounted at %C:", MntLetter
);
1513 Log("Mount ISO FILE: %s", rc
== 0 ? "SUCCESS" : "FAILED");
1515 //Windows 11 bypass check
1516 if (g_windows_data
.windows11_bypass_check
== 1)
1518 Windows11BypassCheck(IsoPath
, MntLetter
);
1522 rc
= DeleteVentoyPart2MountPoint(DiskExtent
.DiskNumber
);
1523 Log("Delete ventoy mountpoint: %s", rc
== 0 ? "SUCCESS" : "NO NEED");
1525 if (g_windows_data
.auto_install_script
[0])
1527 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", Letter
, g_windows_data
.auto_install_script
);
1528 if (IsFileExist("%s", IsoPath
))
1530 Log("use auto install script %s...", IsoPath
);
1531 ProcessUnattendedInstallation(IsoPath
);
1535 Log("auto install script %s not exist", IsoPath
);
1540 Log("auto install no need");
1543 if (g_windows_data
.injection_archive
[0])
1545 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", Letter
, g_windows_data
.injection_archive
);
1546 if (IsFileExist("%s", IsoPath
))
1548 Log("decompress injection archive %s...", IsoPath
);
1549 DecompressInjectionArchive(IsoPath
, DiskExtent
.DiskNumber
);
1551 if (IsFileExist("%s", AUTO_RUN_BAT
))
1554 DWORD flags
= CREATE_NO_WINDOW
;
1557 PROCESS_INFORMATION Pi
;
1558 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
1560 Log("%s exist, now run it...", AUTO_RUN_BAT
);
1562 GetStartupInfoA(&Si
);
1564 hOut
= CreateFileA(AUTO_RUN_LOG
,
1566 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1569 FILE_ATTRIBUTE_NORMAL
,
1572 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
1573 if (hOut
!= INVALID_HANDLE_VALUE
)
1575 Si
.hStdError
= hOut
;
1576 Si
.hStdOutput
= hOut
;
1579 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
1580 sprintf_s(StrBuf
, sizeof(StrBuf
), "cmd.exe /c %s \"%s\" %C", AUTO_RUN_BAT
, IsoPath
, MntLetter
);
1581 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
1582 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1584 SAFE_CLOSE_HANDLE(hOut
);
1588 Log("%s not exist...", AUTO_RUN_BAT
);
1593 Log("injection archive %s not exist", IsoPath
);
1598 Log("no injection archive found");
1605 int VentoyJumpWimboot(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
1612 Log("VentoyJumpWimboot %dbit", g_system_bit
);
1614 sprintf_s(LunchFile
, MAX_PATH
, "X:\\setup.exe");
1616 ReadWholeFile2Buf("wimboot.data", &buf
, &size
);
1617 Log("wimboot.data size:%d", size
);
1619 memcpy(&g_os_param
, buf
, sizeof(ventoy_os_param
));
1620 memcpy(&g_windows_data
, buf
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
1621 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
1623 if (g_os_param_reserved
[0] == 1)
1625 Log("break here for debug .....");
1630 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
1632 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
1634 g_os_param
.vtoy_img_path
[Pos
] = '\\';
1638 if (g_os_param_reserved
[0] == 2)
1640 Log("skip hook for debug .....");
1645 rc
= VentoyHook(&g_os_param
);
1657 static int ventoy_check_create_directory(void)
1659 if (IsDirExist("ventoy"))
1661 Log("ventoy directory already exist");
1665 Log("ventoy directory not exist, now create it.");
1666 if (!CreateDirectoryA("ventoy", NULL
))
1668 Log("Failed to create ventoy directory err:%u", GetLastError());
1676 int VentoyJump(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
1683 DWORD LockStatus
= 0;
1684 BYTE
*Buffer
= NULL
;
1685 CHAR ExeFileName
[MAX_PATH
];
1687 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s", argv
[0]);
1688 if (!IsFileExist("%s", ExeFileName
))
1690 Log("File %s NOT exist, now try %s.exe", ExeFileName
, ExeFileName
);
1691 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s.exe", argv
[0]);
1693 Log("File %s exist ? %s", ExeFileName
, IsFileExist("%s", ExeFileName
) ? "YES" : "NO");
1696 if (ReadWholeFile2Buf(ExeFileName
, (void **)&Buffer
, &FileSize
))
1701 Log("VentoyJump %dbit", g_system_bit
);
1703 MUTEX_LOCK(g_vtoyins_mutex
);
1704 stat
= ventoy_check_create_directory();
1705 MUTEX_UNLOCK(g_vtoyins_mutex
);
1712 for (PeStart
= 0; PeStart
< FileSize
; PeStart
+= 16)
1714 if (CheckOsParam((ventoy_os_param
*)(Buffer
+ PeStart
)) &&
1715 CheckPeHead(Buffer
+ PeStart
+ sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
)))
1717 Log("Find os pararm at %u", PeStart
);
1719 memcpy(&g_os_param
, Buffer
+ PeStart
, sizeof(ventoy_os_param
));
1720 memcpy(&g_windows_data
, Buffer
+ PeStart
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
1721 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
1723 if (g_os_param_reserved
[0] == 1)
1725 Log("break here for debug .....");
1730 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
1732 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
1734 g_os_param
.vtoy_img_path
[Pos
] = '\\';
1738 PeStart
+= sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
);
1739 sprintf_s(LunchFile
, MAX_PATH
, "ventoy\\%s", GetFileNameInPath(ExeFileName
));
1741 MUTEX_LOCK(g_vtoyins_mutex
);
1742 if (IsFileExist("%s", LunchFile
))
1744 Log("vtoyjump multiple call ...");
1746 MUTEX_UNLOCK(g_vtoyins_mutex
);
1750 SaveBuffer2File(LunchFile
, Buffer
+ PeStart
, FileSize
- PeStart
);
1751 MUTEX_UNLOCK(g_vtoyins_mutex
);
1757 if (PeStart
>= FileSize
)
1759 Log("OS param not found");
1763 if (g_os_param_reserved
[0] == 2)
1765 Log("skip hook for debug .....");
1770 rc
= VentoyHook(&g_os_param
);
1783 int real_main(int argc
, char **argv
)
1787 CHAR NewFile
[MAX_PATH
];
1788 CHAR LunchFile
[MAX_PATH
];
1789 CHAR CallParam
[1024] = { 0 };
1791 PROCESS_INFORMATION Pi
;
1793 Log("#### real_main #### argc = %d", argc
);
1794 Log("program full path: <%s>", g_prog_full_path
);
1795 Log("program dir: <%s>", g_prog_dir
);
1796 Log("program name:: <%s>", g_prog_name
);
1798 Log("argc = %d", argc
);
1799 for (i
= 0; i
< argc
; i
++)
1801 Log("argv[%d]=<%s>", i
, argv
[i
]);
1804 strcat_s(CallParam
, sizeof(CallParam
), " ");
1805 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
1809 GetStartupInfoA(&Si
);
1810 memset(LunchFile
, 0, sizeof(LunchFile
));
1812 if (strstr(argv
[0], "vtoyjump.exe"))
1814 rc
= VentoyJumpWimboot(argc
, argv
, LunchFile
);
1818 rc
= VentoyJump(argc
, argv
, LunchFile
);
1821 Log("LunchFile=<%s> CallParam=<%s>", LunchFile
, CallParam
);
1823 if (_stricmp(g_prog_name
, "winpeshl.exe") != 0 && IsFileExist("ventoy\\%s", g_prog_name
))
1825 sprintf_s(NewFile
, sizeof(NewFile
), "%s_BACK.EXE", g_prog_full_path
);
1826 MoveFileA(g_prog_full_path
, NewFile
);
1827 Log("Move <%s> to <%s>", g_prog_full_path
, NewFile
);
1829 sprintf_s(NewFile
, sizeof(NewFile
), "ventoy\\%s", g_prog_name
);
1830 CopyFileA(NewFile
, g_prog_full_path
, TRUE
);
1831 Log("Copy <%s> to <%s>", NewFile
, g_prog_full_path
);
1833 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", g_prog_full_path
);
1834 Log("Final lunchFile is <%s>", LunchFile
);
1838 Log("We don't need to recover original <%s>", g_prog_name
);
1841 if (g_os_param_reserved
[0] == 3)
1843 Log("Open log for debug ...");
1844 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "notepad.exe ventoy.log");
1850 strcat_s(LunchFile
, sizeof(LunchFile
), CallParam
);
1852 else if (NULL
== strstr(LunchFile
, "setup.exe"))
1854 Log("Not setup.exe, hide windows.");
1855 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
1856 Si
.wShowWindow
= SW_HIDE
;
1859 Log("Ventoy jump %s ...", rc
== 0 ? "success" : "failed");
1862 Log("Now launch <%s> ...", LunchFile
);
1864 if (g_os_param_reserved
[0] == 4)
1866 Log("Open cmd for debug ...");
1867 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "cmd.exe");
1870 Log("Backup log at this point");
1871 CopyFileA(LOG_FILE
, "X:\\Windows\\ventoy.backup", TRUE
);
1873 CreateProcessA(NULL
, LunchFile
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1875 for (i
= 0; rc
&& i
< 1800; i
++)
1877 Log("Ventoy hook failed, now wait and retry ...");
1879 rc
= VentoyHook(&g_os_param
);
1882 Log("Wait process...");
1883 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1885 Log("vtoyjump finished");
1890 int main(int argc
, char **argv
)
1894 PROCESS_INFORMATION Pi
;
1895 CHAR CurDir
[MAX_PATH
];
1896 CHAR NewArgv0
[MAX_PATH
];
1897 CHAR CallParam
[1024] = { 0 };
1899 g_vtoylog_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYLOG_LOCK");
1900 g_vtoyins_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYINS_LOCK");
1902 Log("######## VentoyJump %dbit ##########", g_system_bit
);
1904 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
1905 Log("Current directory is <%s>", CurDir
);
1907 GetModuleFileNameA(NULL
, g_prog_full_path
, MAX_PATH
);
1908 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
1910 Log("EXE path: <%s> dir:<%s> name:<%s>", g_prog_full_path
, g_prog_dir
, g_prog_name
);
1912 if (_stricmp(g_prog_name
, "WinLogon.exe") == 0)
1914 Log("This time is rejump back ...");
1916 strcpy_s(g_prog_full_path
, sizeof(g_prog_full_path
), argv
[1]);
1917 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
1919 return real_main(argc
- 1, argv
+ 1);
1921 else if (_stricmp(g_prog_name
, "PECMD.exe") == 0)
1923 Log("We need to rejump for pecmd ...");
1925 ventoy_check_create_directory();
1926 CopyFileA(g_prog_full_path
, "ventoy\\WinLogon.exe", TRUE
);
1928 sprintf_s(CallParam
, sizeof(CallParam
), "ventoy\\WinLogon.exe %s", g_prog_full_path
);
1929 for (i
= 1; i
< argc
; i
++)
1931 strcat_s(CallParam
, sizeof(CallParam
), " ");
1932 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
1935 Log("Now rejump to <%s> ...", CallParam
);
1936 GetStartupInfoA(&Si
);
1937 CreateProcessA(NULL
, CallParam
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1939 Log("Wait rejump process...");
1940 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1941 Log("rejump finished");
1946 Log("We don't need to rejump ...");
1948 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_full_path
);
1951 return real_main(argc
, argv
);