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 DWORD g_vtoy_disk_drive
;
41 static CHAR g_prog_full_path
[MAX_PATH
];
42 static CHAR g_prog_dir
[MAX_PATH
];
43 static CHAR g_prog_name
[MAX_PATH
];
45 #define VTOY_PECMD_PATH "X:\\Windows\\system32\\ventoy\\PECMD.EXE"
46 #define ORG_PECMD_PATH "X:\\Windows\\system32\\PECMD.EXE"
47 #define ORG_PECMD_BK_PATH "X:\\Windows\\system32\\PECMD.EXE_BACK.EXE"
49 #define AUTO_RUN_BAT "X:\\VentoyAutoRun.bat"
50 #define AUTO_RUN_LOG "X:\\VentoyAutoRun.log"
52 #define VTOY_AUTO_FILE "X:\\_vtoy_auto_install"
54 #define LOG_FILE "X:\\Windows\\system32\\ventoy.log"
55 #define MUTEX_LOCK(hmutex) if (hmutex != NULL) LockStatus = WaitForSingleObject(hmutex, INFINITE)
56 #define MUTEX_UNLOCK(hmutex) if (hmutex != NULL && WAIT_OBJECT_0 == LockStatus) ReleaseMutex(hmutex)
58 static const char * GetFileNameInPath(const char *fullpath
)
62 if (strstr(fullpath
, ":"))
64 for (i
= (int)strlen(fullpath
); i
> 0; i
--)
66 if (fullpath
[i
- 1] == '/' || fullpath
[i
- 1] == '\\')
76 static int split_path_name(char *fullpath
, char *dir
, char *name
)
81 Pos
= (CHAR
*)GetFileNameInPath(fullpath
);
83 strcpy_s(name
, MAX_PATH
, Pos
);
87 strcpy_s(dir
, MAX_PATH
, fullpath
);
94 void Log(const char *Fmt
, ...)
101 DWORD LockStatus
= 0;
102 DWORD PID
= GetCurrentProcessId();
105 Len
+= sprintf_s(szBuf
, sizeof(szBuf
),
106 "[%4d/%02d/%02d %02d:%02d:%02d.%03d] [%u] ",
107 Sys
.wYear
, Sys
.wMonth
, Sys
.wDay
,
108 Sys
.wHour
, Sys
.wMinute
, Sys
.wSecond
,
109 Sys
.wMilliseconds
, PID
);
112 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
115 MUTEX_LOCK(g_vtoylog_mutex
);
117 fopen_s(&File
, LOG_FILE
, "a+");
120 fwrite(szBuf
, 1, Len
, File
);
121 fwrite("\n", 1, 1, File
);
125 MUTEX_UNLOCK(g_vtoylog_mutex
);
129 static int LoadNtDriver(const char *DrvBinPath
)
135 SC_HANDLE hServiceMgr
;
137 char name
[256] = { 0 };
139 for (i
= (int)strlen(DrvBinPath
) - 1; i
>= 0; i
--)
141 if (DrvBinPath
[i
] == '\\' || DrvBinPath
[i
] == '/')
143 sprintf_s(name
, sizeof(name
), "%s", DrvBinPath
+ i
+ 1);
148 Log("Load NT driver: %s %s", DrvBinPath
, name
);
150 hServiceMgr
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
151 if (hServiceMgr
== NULL
)
153 Log("OpenSCManager failed Error:%u", GetLastError());
157 Log("OpenSCManager OK");
159 hService
= CreateServiceA(hServiceMgr
,
163 SERVICE_KERNEL_DRIVER
,
164 SERVICE_DEMAND_START
,
165 SERVICE_ERROR_NORMAL
,
167 NULL
, NULL
, NULL
, NULL
, NULL
);
168 if (hService
== NULL
)
170 Status
= GetLastError();
171 if (Status
!= ERROR_IO_PENDING
&& Status
!= ERROR_SERVICE_EXISTS
)
173 Log("CreateService failed v %u", Status
);
174 CloseServiceHandle(hServiceMgr
);
178 hService
= OpenServiceA(hServiceMgr
, name
, SERVICE_ALL_ACCESS
);
179 if (hService
== NULL
)
181 Log("OpenService failed %u", Status
);
182 CloseServiceHandle(hServiceMgr
);
187 Log("CreateService imdisk OK");
189 Ret
= StartServiceA(hService
, 0, NULL
);
192 Log("StartService OK");
196 Status
= GetLastError();
197 if (Status
== ERROR_SERVICE_ALREADY_RUNNING
)
203 Log("StartService error %u", Status
);
208 CloseServiceHandle(hService
);
209 CloseServiceHandle(hServiceMgr
);
211 Log("Load NT driver %s", rc
? "failed" : "success");
216 static int ReadWholeFile2Buf(const char *Fullpath
, void **Data
, DWORD
*Size
)
224 Log("ReadWholeFile2Buf <%s>", Fullpath
);
226 Handle
= CreateFileA(Fullpath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
227 if (Handle
== INVALID_HANDLE_VALUE
)
229 Log("Could not open the file<%s>, error:%u", Fullpath
, GetLastError());
233 FileSize
= SetFilePointer(Handle
, 0, NULL
, FILE_END
);
235 Buffer
= malloc(FileSize
);
238 Log("Failed to alloc memory size:%u", FileSize
);
242 SetFilePointer(Handle
, 0, NULL
, FILE_BEGIN
);
243 if (!ReadFile(Handle
, Buffer
, FileSize
, &dwSize
, NULL
))
245 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
252 Log("Success read file size:%u", FileSize
);
257 SAFE_CLOSE_HANDLE(Handle
);
262 static BOOL
CheckPeHead(BYTE
*Buffer
, DWORD Size
, DWORD Offset
)
267 ventoy_windows_data
*pdata
= NULL
;
269 Head
= Buffer
+ Offset
;
270 pdata
= (ventoy_windows_data
*)Head
;
271 Head
+= sizeof(ventoy_windows_data
);
273 if (pdata
->auto_install_script
[0] && pdata
->auto_install_len
> 0)
275 End
= Offset
+ sizeof(ventoy_windows_data
) + pdata
->auto_install_len
+ 60;
278 Head
+= pdata
->auto_install_len
;
282 if (Head
[0] != 'M' || Head
[1] != 'Z')
287 PeOffset
= *(UINT32
*)(Head
+ 60);
288 if (*(UINT32
*)(Head
+ PeOffset
) != 0x00004550)
297 static BOOL
CheckOsParam(ventoy_os_param
*param
)
302 if (memcmp(¶m
->guid
, &g_ventoy_guid
, sizeof(ventoy_guid
)))
307 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
309 Sum
+= *((BYTE
*)param
+ i
);
317 if (param
->vtoy_img_location_addr
% 4096)
325 static int SaveBuffer2File(const char *Fullpath
, void *Buffer
, DWORD Length
)
331 Log("SaveBuffer2File <%s> len:%u", Fullpath
, Length
);
333 Handle
= CreateFileA(Fullpath
, GENERIC_READ
| GENERIC_WRITE
,
334 FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, CREATE_NEW
, 0, 0);
335 if (Handle
== INVALID_HANDLE_VALUE
)
337 Log("Could not create new file, error:%u", GetLastError());
341 WriteFile(Handle
, Buffer
, Length
, &dwSize
, NULL
);
346 SAFE_CLOSE_HANDLE(Handle
);
351 static int IsUTF8Encode(const char *src
)
354 const UCHAR
*Byte
= (const UCHAR
*)src
;
356 for (i
= 0; i
< MAX_PATH
&& Byte
[i
]; i
++)
367 static int Utf8ToUtf16(const char* src
, WCHAR
* dst
)
369 int size
= MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, 0);
370 return MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, size
+ 1);
373 static BOOL
IsDirExist(const char *Fmt
, ...)
378 CHAR FilePathA
[MAX_PATH
];
379 WCHAR FilePathW
[MAX_PATH
];
382 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
385 UTF8
= IsUTF8Encode(FilePathA
);
389 Utf8ToUtf16(FilePathA
, FilePathW
);
390 Attr
= GetFileAttributesW(FilePathW
);
394 Attr
= GetFileAttributesA(FilePathA
);
397 if (Attr
!= INVALID_FILE_ATTRIBUTES
&& (Attr
& FILE_ATTRIBUTE_DIRECTORY
))
405 static BOOL
IsFileExist(const char *Fmt
, ...)
412 CHAR FilePathA
[MAX_PATH
];
413 WCHAR FilePathW
[MAX_PATH
];
416 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
419 UTF8
= IsUTF8Encode(FilePathA
);
423 Utf8ToUtf16(FilePathA
, FilePathW
);
424 hFile
= CreateFileW(FilePathW
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
428 hFile
= CreateFileA(FilePathA
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
430 if (INVALID_HANDLE_VALUE
== hFile
)
439 Attr
= GetFileAttributesW(FilePathW
);
443 Attr
= GetFileAttributesA(FilePathA
);
446 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
454 Log("File <%s> %s", FilePathA
, (bRet
? "exist" : "NOT exist"));
458 static int GetPhyDiskUUID(const char LogicalDrive
, UINT8
*UUID
, UINT32
*DiskSig
, DISK_EXTENT
*DiskExtent
)
463 VOLUME_DISK_EXTENTS DiskExtents
;
465 UINT8 SectorBuf
[512];
467 Log("GetPhyDiskUUID %C", LogicalDrive
);
469 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", LogicalDrive
);
470 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
471 if (Handle
== INVALID_HANDLE_VALUE
)
473 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
477 Ret
= DeviceIoControl(Handle
,
478 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
482 (DWORD
)(sizeof(DiskExtents
)),
485 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
487 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed, error:%u", GetLastError());
493 memcpy(DiskExtent
, DiskExtents
.Extents
, sizeof(DISK_EXTENT
));
494 Log("%C: is in PhysicalDrive%d Offset:%llu", LogicalDrive
, DiskExtents
.Extents
[0].DiskNumber
,
495 (ULONGLONG
)(DiskExtents
.Extents
[0].StartingOffset
.QuadPart
));
497 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskExtents
.Extents
[0].DiskNumber
);
498 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
499 if (Handle
== INVALID_HANDLE_VALUE
)
501 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
505 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
507 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
512 memcpy(UUID
, SectorBuf
+ 0x180, 16);
515 memcpy(DiskSig
, SectorBuf
+ 0x1B8, 4);
522 static int VentoyMountAnywhere(HANDLE Handle
)
525 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
527 Log("VentoyMountAnywhere");
529 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
530 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
532 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
533 if (Status
!= ERROR_SUCCESS
)
535 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
542 int VentoyMountY(HANDLE Handle
)
547 DWORD physicalDriveNameSize
;
549 WCHAR physicalDriveName
[MAX_PATH
];
550 CHAR physicalDriveNameA
[MAX_PATH
];
551 CHAR cdromDriveName
[MAX_PATH
];
552 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
556 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
557 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
559 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
);
560 if (Status
!= ERROR_SUCCESS
)
562 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
566 memset(physicalDriveName
, 0, sizeof(physicalDriveName
));
567 memset(physicalDriveNameA
, 0, sizeof(physicalDriveNameA
));
569 physicalDriveNameSize
= MAX_PATH
;
570 Status
= GetVirtualDiskPhysicalPath(Handle
, &physicalDriveNameSize
, physicalDriveName
);
571 if (Status
!= ERROR_SUCCESS
)
573 Log("Failed GetVirtualDiskPhysicalPath ErrorCode:%u", Status
);
577 for (i
= 0; physicalDriveName
[i
]; i
++)
579 physicalDriveNameA
[i
] = (CHAR
)toupper((CHAR
)(physicalDriveName
[i
]));
582 Log("physicalDriveNameA=<%s>", physicalDriveNameA
);
584 Pos
= strstr(physicalDriveNameA
, "CDROM");
587 Log("Not cdrom phy drive");
591 sprintf_s(cdromDriveName
, sizeof(cdromDriveName
), "\\Device\\%s", Pos
);
592 Log("cdromDriveName=<%s>", cdromDriveName
);
594 for (i
= 0; i
< 3 && (bRet
== FALSE
); i
++)
597 bRet
= DefineDosDeviceA(DDD_RAW_TARGET_PATH
, "Y:", cdromDriveName
);
598 Log("DefineDosDeviceA %s", bRet
? "success" : "failed");
604 static BOOL
VentoyAPINeedMountY(const char *IsoPath
)
612 static int VentoyAttachVirtualDisk(HANDLE Handle
, const char *IsoPath
)
617 Drives
= GetLogicalDrives();
618 if ((1 << 24) & Drives
)
620 Log("Y: is occupied");
625 Log("Y: is free now");
629 if (DriveYFree
&& VentoyAPINeedMountY(IsoPath
))
631 return VentoyMountY(Handle
);
635 return VentoyMountAnywhere(Handle
);
639 int VentoyMountISOByAPI(const char *IsoPath
)
644 WCHAR wFilePath
[512] = { 0 };
645 VIRTUAL_STORAGE_TYPE StorageType
;
646 OPEN_VIRTUAL_DISK_PARAMETERS OpenParameters
;
648 Log("VentoyMountISOByAPI <%s>", IsoPath
);
650 if (IsUTF8Encode(IsoPath
))
652 Log("This is UTF8 encoding");
653 MultiByteToWideChar(CP_UTF8
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
657 Log("This is ANSI encoding");
658 MultiByteToWideChar(CP_ACP
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
661 memset(&StorageType
, 0, sizeof(StorageType
));
662 memset(&OpenParameters
, 0, sizeof(OpenParameters
));
664 OpenParameters
.Version
= OPEN_VIRTUAL_DISK_VERSION_1
;
666 for (i
= 0; i
< 10; i
++)
668 Status
= OpenVirtualDisk(&StorageType
, wFilePath
, VIRTUAL_DISK_ACCESS_READ
, 0, &OpenParameters
, &Handle
);
669 if (ERROR_FILE_NOT_FOUND
== Status
|| ERROR_PATH_NOT_FOUND
== Status
)
671 Log("OpenVirtualDisk ErrorCode:%u, now wait and retry...", Status
);
676 if (ERROR_SUCCESS
== Status
)
678 Log("OpenVirtualDisk success");
680 else if (ERROR_VIRTDISK_PROVIDER_NOT_FOUND
== Status
)
682 Log("VirtualDisk for ISO file is not supported in current system");
686 Log("Failed to open virtual disk ErrorCode:%u", Status
);
692 if (Status
!= ERROR_SUCCESS
)
697 Log("OpenVirtualDisk success");
699 Status
= VentoyAttachVirtualDisk(Handle
, IsoPath
);
700 if (Status
!= ERROR_SUCCESS
)
702 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
707 Log("VentoyAttachVirtualDisk success");
714 static HANDLE g_FatPhyDrive
;
715 static UINT64 g_Part2StartSec
;
717 static int CopyFileFromFatDisk(const CHAR
* SrcFile
, const CHAR
*DstFile
)
724 Log("CopyFileFromFatDisk (%s)==>(%s)", SrcFile
, DstFile
);
726 flfile
= fl_fopen(SrcFile
, "rb");
729 fl_fseek(flfile
, 0, SEEK_END
);
730 size
= (int)fl_ftell(flfile
);
731 fl_fseek(flfile
, 0, SEEK_SET
);
733 buf
= (char *)malloc(size
);
736 fl_fread(buf
, 1, size
, flfile
);
739 SaveBuffer2File(DstFile
, buf
, size
);
749 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
754 LARGE_INTEGER liCurrentPosition
;
756 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
757 liCurrentPosition
.QuadPart
*= 512;
758 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
760 ReadSize
= (DWORD
)(SectorCount
* 512);
762 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
763 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
765 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet
, ReadSize
, dwSize
, GetLastError());
771 static BOOL
Is2K10PE(void)
777 fopen_s(&fp
, "X:\\Windows\\System32\\PECMD.INI", "r");
783 memset(szLine
, 0, sizeof(szLine
));
784 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
786 if (strstr(szLine
, "2k10\\"))
797 static CHAR
GetIMDiskMountLogicalDrive(void)
801 DWORD Mask
= 0x1000000;
803 // fixed use M as mountpoint for 2K10 PE
806 Log("Use M: for 2K10 PE");
810 Drives
= GetLogicalDrives();
811 Log("Drives=0x%x", Drives
);
815 if ((Drives
& Mask
) == 0)
827 UINT64
GetVentoyEfiPartStartSector(HANDLE hDrive
)
832 VTOY_GPT_INFO
*pGpt
= NULL
;
833 UINT64 StartSector
= 0;
835 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
837 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
838 Log("Read MBR Ret:%u Size:%u code:%u", bRet
, dwSize
, LASTERR
);
840 if ((!bRet
) || (dwSize
!= sizeof(MBR
)))
845 if (MBR
.PartTbl
[0].FsFlag
== 0xEE)
847 Log("GPT partition style");
849 pGpt
= malloc(sizeof(VTOY_GPT_INFO
));
855 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
856 bRet
= ReadFile(hDrive
, pGpt
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
857 if ((!bRet
) || (dwSize
!= sizeof(VTOY_GPT_INFO
)))
859 Log("Failed to read gpt info %d %u %d", bRet
, dwSize
, LASTERR
);
863 StartSector
= pGpt
->PartTbl
[1].StartLBA
;
868 Log("MBR partition style");
869 StartSector
= MBR
.PartTbl
[1].StartSectorId
;
872 Log("GetVentoyEfiPart StartSector: %llu", StartSector
);
876 static int VentoyRunImdisk(const char *IsoPath
, const char *imdiskexe
)
881 PROCESS_INFORMATION Pi
;
883 Log("VentoyRunImdisk <%s> <%s>", IsoPath
, imdiskexe
);
885 Letter
= GetIMDiskMountLogicalDrive();
886 sprintf_s(Cmdline
, sizeof(Cmdline
), "%s -a -o ro -f \"%s\" -m %C:", imdiskexe
, IsoPath
, Letter
);
887 Log("mount iso to %C: use imdisk cmd <%s>", Letter
, Cmdline
);
889 if (IsUTF8Encode(IsoPath
))
892 GetStartupInfoW(&Si
);
893 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
894 Si
.wShowWindow
= SW_HIDE
;
896 Utf8ToUtf16(Cmdline
, CmdlineW
);
897 CreateProcessW(NULL
, CmdlineW
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
899 Log("This is UTF8 encoding");
904 GetStartupInfoA(&Si
);
905 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
906 Si
.wShowWindow
= SW_HIDE
;
908 CreateProcessA(NULL
, Cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
910 Log("This is ANSI encoding");
913 Log("Wait for imdisk process ...");
914 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
915 Log("imdisk process finished");
920 int VentoyMountISOByImdisk(const char *IsoPath
, DWORD PhyDrive
)
926 CHAR PhyPath
[MAX_PATH
];
927 GET_LENGTH_INFORMATION LengthInfo
;
929 Log("VentoyMountISOByImdisk %s", IsoPath
);
931 if (IsFileExist("X:\\Windows\\System32\\imdisk.exe"))
933 Log("imdisk.exe exist, use it directly...");
934 VentoyRunImdisk(IsoPath
, "imdisk.exe");
938 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
939 hDrive
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
940 if (hDrive
== INVALID_HANDLE_VALUE
)
942 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
946 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
949 Log("Could not get phy disk %s size, error:%u", PhyPath
, GetLastError());
953 g_FatPhyDrive
= hDrive
;
954 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
956 Log("Parse FAT fs...");
960 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
962 if (g_system_bit
== 64)
964 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.sys", "ventoy\\imdisk.sys");
965 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.exe", "ventoy\\imdisk.exe");
966 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.cpl", "ventoy\\imdisk.cpl");
970 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.sys", "ventoy\\imdisk.sys");
971 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");
972 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");
975 GetCurrentDirectoryA(sizeof(PhyPath
), PhyPath
);
976 strcat_s(PhyPath
, sizeof(PhyPath
), "\\ventoy\\imdisk.sys");
978 if (LoadNtDriver(PhyPath
) == 0)
980 VentoyRunImdisk(IsoPath
, "ventoy\\imdisk.exe");
988 SAFE_CLOSE_HANDLE(hDrive
);
993 static int MountIsoFile(CONST CHAR
*IsoPath
, DWORD PhyDrive
)
995 if (IsWindows8OrGreater())
997 Log("This is Windows 8 or latter...");
998 if (VentoyMountISOByAPI(IsoPath
) == 0)
1000 Log("Mount iso by API success");
1005 Log("Mount iso by API failed, maybe not supported, try imdisk");
1006 return VentoyMountISOByImdisk(IsoPath
, PhyDrive
);
1011 Log("This is before Windows 8 ...");
1012 if (VentoyMountISOByImdisk(IsoPath
, PhyDrive
) == 0)
1014 Log("Mount iso by imdisk success");
1019 return VentoyMountISOByAPI(IsoPath
);
1024 static int GetPhyDriveByLogicalDrive(int DriveLetter
)
1029 VOLUME_DISK_EXTENTS DiskExtents
;
1032 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", (CHAR
)DriveLetter
);
1034 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1035 if (Handle
== INVALID_HANDLE_VALUE
)
1037 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1041 Ret
= DeviceIoControl(Handle
,
1042 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
1046 (DWORD
)(sizeof(DiskExtents
)),
1050 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
1052 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, GetLastError());
1053 SAFE_CLOSE_HANDLE(Handle
);
1056 SAFE_CLOSE_HANDLE(Handle
);
1058 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
1060 DiskExtents
.Extents
[0].DiskNumber
,
1061 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
1062 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
1065 return (int)DiskExtents
.Extents
[0].DiskNumber
;
1069 static int DeleteVentoyPart2MountPoint(DWORD PhyDrive
)
1074 CHAR DriveName
[] = "?:\\";
1076 Log("DeleteVentoyPart2MountPoint Phy%u ...", PhyDrive
);
1078 Drives
= GetLogicalDrives();
1081 if ((Drives
& 0x01) && IsFileExist("%C:\\ventoy\\ventoy.cpio", Letter
))
1083 Log("File %C:\\ventoy\\ventoy.cpio exist", Letter
);
1085 PhyDisk
= GetPhyDriveByLogicalDrive(Letter
);
1086 Log("PhyDisk=%u for %C", PhyDisk
, Letter
);
1088 if (PhyDisk
== PhyDrive
)
1090 DriveName
[0] = Letter
;
1091 DeleteVolumeMountPointA(DriveName
);
1103 static BOOL
check_tar_archive(const char *archive
, CHAR
*tarName
)
1107 const char *pos
= archive
;
1108 const char *slash
= archive
;
1112 if (*pos
== '\\' || *pos
== '/')
1119 len
= (int)strlen(slash
);
1121 if (len
> 7 && (strncmp(slash
+ len
- 7, ".tar.gz", 7) == 0 || strncmp(slash
+ len
- 7, ".tar.xz", 7) == 0))
1123 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1124 tarName
[nameLen
- 3] = 0;
1127 else if (len
> 8 && strncmp(slash
+ len
- 8, ".tar.bz2", 8) == 0)
1129 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1130 tarName
[nameLen
- 4] = 0;
1133 else if (len
> 9 && strncmp(slash
+ len
- 9, ".tar.lzma", 9) == 0)
1135 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
1136 tarName
[nameLen
- 5] = 0;
1143 static UCHAR
*g_unxz_buffer
= NULL
;
1144 static int g_unxz_len
= 0;
1146 static void unxz_error(char *x
)
1151 static int unxz_flush(void *src
, unsigned int size
)
1153 memcpy(g_unxz_buffer
+ g_unxz_len
, src
, size
);
1154 g_unxz_len
+= (int)size
;
1159 static int DecompressInjectionArchive(const char *archive
, DWORD PhyDrive
)
1163 UCHAR
*Buffer
= NULL
;
1164 UCHAR
*RawBuffer
= NULL
;
1170 DWORD flags
= CREATE_NO_WINDOW
;
1171 CHAR StrBuf
[MAX_PATH
];
1172 CHAR tarName
[MAX_PATH
];
1174 PROCESS_INFORMATION Pi
;
1175 PROCESS_INFORMATION NewPi
;
1176 GET_LENGTH_INFORMATION LengthInfo
;
1177 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
1179 Log("DecompressInjectionArchive %s", archive
);
1181 sprintf_s(StrBuf
, sizeof(StrBuf
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
1182 hDrive
= CreateFileA(StrBuf
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1183 if (hDrive
== INVALID_HANDLE_VALUE
)
1185 Log("Could not open the disk<%s>, error:%u", StrBuf
, GetLastError());
1189 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
1192 Log("Could not get phy disk %s size, error:%u", StrBuf
, GetLastError());
1196 g_FatPhyDrive
= hDrive
;
1197 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
1199 Log("Parse FAT fs...");
1203 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
1205 if (g_system_bit
== 64)
1207 CopyFileFromFatDisk("/ventoy/7z/64/7za.xz", "ventoy\\7za.xz");
1211 CopyFileFromFatDisk("/ventoy/7z/32/7za.xz", "ventoy\\7za.xz");
1214 ReadWholeFile2Buf("ventoy\\7za.xz", &Buffer
, &dwSize
);
1215 Log("7za.xz file size:%u", dwSize
);
1217 RawBuffer
= malloc(SIZE_1MB
* 4);
1220 g_unxz_buffer
= RawBuffer
;
1222 unxz(Buffer
, (int)dwSize
, NULL
, unxz_flush
, NULL
, &writelen
, unxz_error
);
1223 if (writelen
== (int)dwSize
)
1225 Log("Decompress success 7za.xz(%u) ---> 7za.exe(%d)", dwSize
, g_unxz_len
);
1229 Log("Decompress failed 7za.xz(%u) ---> 7za.exe(%u)", dwSize
, dwSize
);
1232 SaveBuffer2File("ventoy\\7za.exe", RawBuffer
, (DWORD
)g_unxz_len
);
1234 g_unxz_buffer
= NULL
;
1240 Log("Failed to alloc 4MB memory");
1243 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", archive
);
1245 Log("extract inject to X:");
1246 Log("cmdline:<%s>", StrBuf
);
1248 GetStartupInfoA(&Si
);
1250 hOut
= CreateFileA("ventoy\\7z.log",
1252 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
1255 FILE_ATTRIBUTE_NORMAL
,
1258 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
1260 if (hOut
!= INVALID_HANDLE_VALUE
)
1262 Si
.hStdError
= hOut
;
1263 Si
.hStdOutput
= hOut
;
1266 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
1267 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
1270 // decompress tar archive, for tar.gz/tar.xz/tar.bz2
1272 if (check_tar_archive(archive
, tarName
))
1274 Log("Decompress tar archive...<%s>", tarName
);
1276 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", tarName
);
1278 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &NewPi
);
1279 WaitForSingleObject(NewPi
.hProcess
, INFINITE
);
1281 Log("Now delete %s", tarName
);
1282 DeleteFileA(tarName
);
1285 SAFE_CLOSE_HANDLE(hOut
);
1291 SAFE_CLOSE_HANDLE(hDrive
);
1296 static int UnattendNeedVarExpand(const char *script
)
1301 fopen_s(&fp
, script
, "r");
1307 szLine
[0] = szLine
[4095] = 0;
1309 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
1311 if (strstr(szLine
, "$$VT_"))
1317 szLine
[0] = szLine
[4095] = 0;
1324 static int ExpandSingleVar(VarDiskInfo
*pDiskInfo
, int DiskNum
, const char *var
, char *value
, int len
)
1330 UINT64 uiMaxSize
= 0;
1331 UINT64 uiMaxDelta
= ULLONG_MAX
;
1335 if (strcmp(var
, "VT_WINDOWS_DISK_1ST_NONVTOY") == 0)
1337 for (i
= 0; i
< DiskNum
; i
++)
1339 if (pDiskInfo
[i
].Capacity
> 0 && i
!= g_vtoy_disk_drive
)
1341 Log("%s=<PhyDrive%d>", var
, i
);
1342 sprintf_s(value
, len
, "%d", i
);
1347 else if (strcmp(var
, "VT_WINDOWS_DISK_1ST_NONUSB") == 0)
1349 for (i
= 0; i
< DiskNum
; i
++)
1351 if (pDiskInfo
[i
].Capacity
> 0 && pDiskInfo
[i
].BusType
!= BusTypeUsb
)
1353 Log("%s=<PhyDrive%d>", var
, i
);
1354 sprintf_s(value
, len
, "%d", i
);
1359 else if (strcmp(var
, "VT_WINDOWS_DISK_MAX_SIZE") == 0)
1361 for (i
= 0; i
< DiskNum
; i
++)
1363 if (pDiskInfo
[i
].Capacity
> 0 && pDiskInfo
[i
].Capacity
> uiMaxSize
)
1366 uiMaxSize
= pDiskInfo
[i
].Capacity
;
1370 Log("%s=<PhyDrive%d>", var
, index
);
1371 sprintf_s(value
, len
, "%d", index
);
1373 else if (strncmp(var
, "VT_WINDOWS_DISK_CLOSEST_", 24) == 0)
1375 uiDst
= strtoul(var
+ 24, NULL
, 10);
1376 uiDst
= uiDst
* (1024ULL * 1024ULL * 1024ULL);
1378 for (i
= 0; i
< DiskNum
; i
++)
1380 if (pDiskInfo
[i
].Capacity
== 0)
1385 if (pDiskInfo
[i
].Capacity
> uiDst
)
1387 uiDelta
= pDiskInfo
[i
].Capacity
- uiDst
;
1391 uiDelta
= uiDst
- pDiskInfo
[i
].Capacity
;
1394 if (uiDelta
< uiMaxDelta
)
1396 uiMaxDelta
= uiDelta
;
1401 Log("%s=<PhyDrive%d>", var
, index
);
1402 sprintf_s(value
, len
, "%d", index
);
1406 Log("Invalid var name <%s>", var
);
1407 sprintf_s(value
, len
, "$$%s$$", var
);
1412 sprintf_s(value
, len
, "$$%s$$", var
);
1418 static void TrimString(CHAR
*String
)
1420 CHAR
*Pos1
= String
;
1421 CHAR
*Pos2
= String
;
1422 size_t Len
= strlen(String
);
1426 if (String
[Len
- 1] != ' ' && String
[Len
- 1] != '\t')
1430 String
[Len
- 1] = 0;
1434 while (*Pos1
== ' ' || *Pos1
== '\t')
1448 static int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
)
1456 lRet
= RegOpenKeyExA(Key
, SubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
1457 Log("RegOpenKeyExA <%s> Ret:%ld", SubKey
, lRet
);
1459 if (ERROR_SUCCESS
== lRet
)
1461 Size
= sizeof(Value
);
1462 lRet
= RegQueryValueExA(hKey
, ValueName
, NULL
, &Type
, (LPBYTE
)&Value
, &Size
);
1463 Log("RegQueryValueExA <%s> ret:%u Size:%u Value:%u", ValueName
, lRet
, Size
, Value
);
1476 static const CHAR
* GetBusTypeString(int Type
)
1480 case BusTypeUnknown
: return "unknown";
1481 case BusTypeScsi
: return "SCSI";
1482 case BusTypeAtapi
: return "Atapi";
1483 case BusTypeAta
: return "ATA";
1484 case BusType1394
: return "1394";
1485 case BusTypeSsa
: return "SSA";
1486 case BusTypeFibre
: return "Fibre";
1487 case BusTypeUsb
: return "USB";
1488 case BusTypeRAID
: return "RAID";
1489 case BusTypeiScsi
: return "iSCSI";
1490 case BusTypeSas
: return "SAS";
1491 case BusTypeSata
: return "SATA";
1492 case BusTypeSd
: return "SD";
1493 case BusTypeMmc
: return "MMC";
1494 case BusTypeVirtual
: return "Virtual";
1495 case BusTypeFileBackedVirtual
: return "FileBackedVirtual";
1496 case BusTypeSpaces
: return "Spaces";
1497 case BusTypeNvme
: return "Nvme";
1502 static int GetHumanReadableGBSize(UINT64 SizeBytes
)
1507 double GB
= SizeBytes
* 1.0 / 1000 / 1000 / 1000;
1509 if ((SizeBytes
% 1073741824) == 0)
1511 return (int)(SizeBytes
/ 1073741824);
1514 for (i
= 0; i
< 12; i
++)
1518 Delta
= (Pow2
- GB
) / Pow2
;
1522 Delta
= (GB
- Pow2
) / Pow2
;
1536 static int EnumerateAllDisk(VarDiskInfo
**ppDiskInfo
, int *pDiskNum
)
1543 VarDiskInfo
*pDiskInfo
= NULL
;
1544 HANDLE Handle
= INVALID_HANDLE_VALUE
;
1546 GET_LENGTH_INFORMATION LengthInfo
;
1547 STORAGE_PROPERTY_QUERY Query
;
1548 STORAGE_DESCRIPTOR_HEADER DevDescHeader
;
1549 STORAGE_DEVICE_DESCRIPTOR
*pDevDesc
;
1551 if (GetRegDwordValue(HKEY_LOCAL_MACHINE
, "SYSTEM\\CurrentControlSet\\Services\\disk\\Enum", "Count", &Value
) == 0)
1553 DiskNum
= (int)Value
;
1557 Log("Failed to read disk count");
1561 Log("Current phy disk count:%d", DiskNum
);
1567 pDiskInfo
= malloc(DiskNum
* sizeof(VarDiskInfo
));
1570 Log("Failed to alloc");
1573 memset(pDiskInfo
, 0, DiskNum
* sizeof(VarDiskInfo
));
1575 for (i
= 0; i
< DiskNum
; i
++)
1577 SAFE_CLOSE_HANDLE(Handle
);
1579 safe_sprintf(PhyDrive
, "\\\\.\\PhysicalDrive%d", i
);
1580 Handle
= CreateFileA(PhyDrive
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
1581 Log("Create file Handle:%p %s status:%u", Handle
, PhyDrive
, LASTERR
);
1583 if (Handle
== INVALID_HANDLE_VALUE
)
1588 bRet
= DeviceIoControl(Handle
,
1589 IOCTL_DISK_GET_LENGTH_INFO
, NULL
,
1597 Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR
);
1601 Log("PHYSICALDRIVE%d size %llu bytes", i
, (ULONGLONG
)LengthInfo
.Length
.QuadPart
);
1603 Query
.PropertyId
= StorageDeviceProperty
;
1604 Query
.QueryType
= PropertyStandardQuery
;
1606 bRet
= DeviceIoControl(Handle
,
1607 IOCTL_STORAGE_QUERY_PROPERTY
,
1611 sizeof(STORAGE_DESCRIPTOR_HEADER
),
1616 Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR
, dwBytes
);
1620 if (DevDescHeader
.Size
< sizeof(STORAGE_DEVICE_DESCRIPTOR
))
1622 Log("Invalid DevDescHeader.Size:%u", DevDescHeader
.Size
);
1626 pDevDesc
= (STORAGE_DEVICE_DESCRIPTOR
*)malloc(DevDescHeader
.Size
);
1629 Log("failed to malloc error:%u len:%u", LASTERR
, DevDescHeader
.Size
);
1633 bRet
= DeviceIoControl(Handle
,
1634 IOCTL_STORAGE_QUERY_PROPERTY
,
1643 Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR
, dwBytes
);
1648 pDiskInfo
[i
].RemovableMedia
= pDevDesc
->RemovableMedia
;
1649 pDiskInfo
[i
].BusType
= pDevDesc
->BusType
;
1650 pDiskInfo
[i
].DeviceType
= pDevDesc
->DeviceType
;
1651 pDiskInfo
[i
].Capacity
= LengthInfo
.Length
.QuadPart
;
1653 if (pDevDesc
->VendorIdOffset
)
1655 safe_strcpy(pDiskInfo
[i
].VendorId
, (char *)pDevDesc
+ pDevDesc
->VendorIdOffset
);
1656 TrimString(pDiskInfo
[i
].VendorId
);
1659 if (pDevDesc
->ProductIdOffset
)
1661 safe_strcpy(pDiskInfo
[i
].ProductId
, (char *)pDevDesc
+ pDevDesc
->ProductIdOffset
);
1662 TrimString(pDiskInfo
[i
].ProductId
);
1665 if (pDevDesc
->ProductRevisionOffset
)
1667 safe_strcpy(pDiskInfo
[i
].ProductRev
, (char *)pDevDesc
+ pDevDesc
->ProductRevisionOffset
);
1668 TrimString(pDiskInfo
[i
].ProductRev
);
1671 if (pDevDesc
->SerialNumberOffset
)
1673 safe_strcpy(pDiskInfo
[i
].SerialNumber
, (char *)pDevDesc
+ pDevDesc
->SerialNumberOffset
);
1674 TrimString(pDiskInfo
[i
].SerialNumber
);
1678 SAFE_CLOSE_HANDLE(Handle
);
1681 Log("########## DUMP DISK BEGIN ##########");
1682 for (i
= 0; i
< DiskNum
; i
++)
1684 Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Name:%s %s",
1685 i
, GetBusTypeString(pDiskInfo
[i
].BusType
), pDiskInfo
[i
].RemovableMedia
,
1686 GetHumanReadableGBSize(pDiskInfo
[i
].Capacity
), pDiskInfo
[i
].Capacity
,
1687 pDiskInfo
[i
].VendorId
, pDiskInfo
[i
].ProductId
);
1689 Log("Ventoy disk is PhyDvr%d", g_vtoy_disk_drive
);
1690 Log("########## DUMP DISK END ##########");
1692 *ppDiskInfo
= pDiskInfo
;
1693 *pDiskNum
= DiskNum
;
1697 static int UnattendVarExpand(const char *script
, const char *tmpfile
)
1706 VarDiskInfo
*pDiskInfo
= NULL
;
1708 Log("UnattendVarExpand ...");
1710 if (EnumerateAllDisk(&pDiskInfo
, &DiskNum
))
1712 Log("Failed to EnumerateAllDisk");
1716 fopen_s(&fp
, script
, "r");
1723 fopen_s(&fout
, tmpfile
, "w+");
1731 szLine
[0] = szLine
[4095] = 0;
1733 while (fgets(szLine
, sizeof(szLine
) - 1, fp
))
1735 start
= strstr(szLine
, "$$VT_");
1738 end
= strstr(start
+ 5, "$$");
1744 fprintf(fout
, "%s", szLine
);
1747 ExpandSingleVar(pDiskInfo
, DiskNum
, start
+ 2, szValue
, sizeof(szValue
) - 1);
1748 fprintf(fout
, "%s", szValue
);
1750 fprintf(fout
, "%s", end
+ 2);
1754 fprintf(fout
, "%s", szLine
);
1757 szLine
[0] = szLine
[4095] = 0;
1766 //#define VAR_DEBUG 1
1768 static int ProcessUnattendedInstallation(const char *script
)
1774 CHAR TmpFile
[MAX_PATH
];
1775 CHAR CurDir
[MAX_PATH
];
1777 Log("Copy unattended XML ...");
1779 GetCurrentDirectory(sizeof(CurDir
), CurDir
);
1781 if ((Letter
>= 'A' && Letter
<= 'Z') || (Letter
>= 'a' && Letter
<= 'z'))
1783 Log("Current Drive Letter: %C", Letter
);
1791 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\AutounattendXXX.xml", Letter
);
1793 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\Autounattend.xml", Letter
);
1796 if (UnattendNeedVarExpand(script
))
1798 sprintf_s(TmpFile
, sizeof(TmpFile
), "%C:\\__Autounattend", Letter
);
1799 UnattendVarExpand(script
, TmpFile
);
1801 Log("Expand Copy file <%s> --> <%s>", script
, CurDir
);
1802 CopyFile(TmpFile
, CurDir
, FALSE
);
1806 Log("No var expand copy file <%s> --> <%s>", script
, CurDir
);
1807 CopyFile(script
, CurDir
, FALSE
);
1811 Ret
= RegCreateKeyEx(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dw
);
1812 if (ERROR_SUCCESS
== Ret
)
1814 Ret
= RegSetValueEx(hKey
, "UnattendFile", 0, REG_SZ
, CurDir
, (DWORD
)(strlen(CurDir
) + 1));
1821 static int Windows11BypassCheck(const char *isofile
, const char MntLetter
)
1828 CHAR
*Buffer
= NULL
;
1829 VS_FIXEDFILEINFO
* VerInfo
= NULL
;
1830 CHAR CheckFile
[MAX_PATH
];
1831 UINT16 Major
, Minor
, Build
, Revision
;
1833 Log("Windows11BypassCheck for <%s> %C:", isofile
, MntLetter
);
1835 if (FALSE
== IsFileExist("%C:\\sources\\boot.wim", MntLetter
) ||
1836 FALSE
== IsFileExist("%C:\\sources\\compatresources.dll", MntLetter
))
1838 Log("boot.wim/compatresources.dll not exist, this is not a windows install media.");
1842 if (FALSE
== IsFileExist("%C:\\sources\\install.wim", MntLetter
) &&
1843 FALSE
== IsFileExist("%C:\\sources\\install.esd", MntLetter
))
1845 Log("install.wim/install.esd not exist, this is not a windows install media.");
1849 sprintf_s(CheckFile
, sizeof(CheckFile
), "%C:\\sources\\compatresources.dll", MntLetter
);
1850 dwSize
= GetFileVersionInfoSizeA(CheckFile
, &dwHandle
);
1853 Log("Failed to get file version info size: %u", LASTERR
);
1857 Buffer
= malloc(dwSize
);
1863 if (FALSE
== GetFileVersionInfoA(CheckFile
, dwHandle
, dwSize
, Buffer
))
1865 Log("Failed to get file version info : %u", LASTERR
);
1869 if (VerQueryValueA(Buffer
, "\\", (LPVOID
)&VerInfo
, &VerLen
) && VerLen
!= 0)
1871 if (VerInfo
->dwSignature
== VS_FFI_SIGNATURE
)
1873 Major
= HIWORD(VerInfo
->dwFileVersionMS
);
1874 Minor
= LOWORD(VerInfo
->dwFileVersionMS
);
1875 Build
= HIWORD(VerInfo
->dwFileVersionLS
);
1876 Revision
= LOWORD(VerInfo
->dwFileVersionLS
);
1878 Log("FileVersionze: <%u %u %u %u>", Major
, Minor
, Build
, Revision
);
1880 if (Major
== 10 && Build
> 20000)
1887 Log("This is not Windows 11, not need to bypass.", Major
);
1893 //Now we really need to bypass windows 11 check. create registry
1895 HKEY hSubKey
= NULL
;
1898 Status
= RegCreateKeyExA(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dwSize
);
1899 if (ERROR_SUCCESS
!= Status
)
1901 Log("Failed to create reg key System\\Setup %u %u", LASTERR
, Status
);
1905 Status
= RegCreateKeyExA(hKey
, "LabConfig", 0, NULL
, 0, KEY_SET_VALUE
| KEY_QUERY_VALUE
| KEY_CREATE_SUB_KEY
, NULL
, &hSubKey
, &dwSize
);
1906 if (ERROR_SUCCESS
!= Status
)
1908 Log("Failed to create LabConfig reg %u %u", LASTERR
, Status
);
1913 Status
+= RegSetValueExA(hSubKey
, "BypassRAMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1914 Status
+= RegSetValueExA(hSubKey
, "BypassTPMCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1915 Status
+= RegSetValueExA(hSubKey
, "BypassSecureBootCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1916 Status
+= RegSetValueExA(hSubKey
, "BypassStorageCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1917 Status
+= RegSetValueExA(hSubKey
, "BypassCPUCheck", 0, REG_DWORD
, (LPBYTE
)&dwValue
, sizeof(DWORD
));
1919 Log("Create bypass registry %s %u", (Status
== ERROR_SUCCESS
) ? "SUCCESS" : "FAILED", Status
);
1932 static BOOL
CheckVentoyDisk(DWORD DiskNum
)
1936 UINT8 SectorBuf
[512];
1938 UINT8 check
[8] = { 0x56, 0x54, 0x00, 0x47, 0x65, 0x00, 0x48, 0x44 };
1940 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskNum
);
1941 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
1942 if (Handle
== INVALID_HANDLE_VALUE
)
1944 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
1948 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
1950 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
1951 CloseHandle(Handle
);
1955 CloseHandle(Handle
);
1957 if (memcmp(SectorBuf
+ 0x190, check
, 8) == 0)
1966 static int VentoyHook(ventoy_os_param
*param
)
1971 BOOL vtoyfind
= FALSE
;
1980 DISK_EXTENT DiskExtent
;
1981 DISK_EXTENT VtoyDiskExtent
;
1983 CHAR IsoPath
[MAX_PATH
];
1985 Log("VentoyHook Path:<%s>", param
->vtoy_img_path
);
1987 if (IsUTF8Encode(param
->vtoy_img_path
))
1989 Log("This file is UTF8 encoding");
1992 for (i
= 0; i
< 5; i
++)
1995 Drives
= GetLogicalDrives();
1996 Log("Logic Drives: 0x%x", Drives
);
2002 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
2003 if (IsFileExist("%s", IsoPath
))
2005 Log("File exist under %C:", Letter
);
2006 memset(UUID
, 0, sizeof(UUID
));
2007 memset(&DiskExtent
, 0, sizeof(DiskExtent
));
2008 if (GetPhyDiskUUID(Letter
, UUID
, NULL
, &DiskExtent
) == 0)
2010 if (memcmp(UUID
, param
->vtoy_disk_guid
, 16) == 0)
2012 Log("Disk UUID match");
2020 Log("File NOT exist under %C:", Letter
);
2034 Log("Now wait and retry ...");
2041 Log("Failed to find ISO file");
2045 Log("Find ISO file <%s>", IsoPath
);
2047 //Find VtoyLetter in Vlnk Mode
2048 if (g_os_param_reserved
[6] == 1)
2050 memcpy(&VtoySig
, g_os_param_reserved
+ 7, 4);
2051 for (i
= 0; i
< 5; i
++)
2054 Drives
= GetLogicalDrives();
2055 Log("Logic Drives: 0x%x VentoySig:%08X", Drives
, VtoySig
);
2061 memset(UUID
, 0, sizeof(UUID
));
2062 memset(&VtoyDiskExtent
, 0, sizeof(VtoyDiskExtent
));
2064 if (GetPhyDiskUUID(VtoyLetter
, UUID
, &DiskSig
, &VtoyDiskExtent
) == 0)
2066 Log("DiskSig=%08X PartStart=%lld", DiskSig
, VtoyDiskExtent
.StartingOffset
.QuadPart
);
2067 if (DiskSig
== VtoySig
&& VtoyDiskExtent
.StartingOffset
.QuadPart
== SIZE_1MB
)
2069 Log("Ventoy Disk Sig match");
2082 Log("Find Ventoy Letter: %C", VtoyLetter
);
2087 Log("Now wait and retry ...");
2092 if (vtoyfind
== FALSE
)
2094 Log("Failed to find ventoy disk");
2098 VtoyDiskNum
= VtoyDiskExtent
.DiskNumber
;
2102 VtoyLetter
= Letter
;
2103 Log("No vlnk mode %C", Letter
);
2105 VtoyDiskNum
= DiskExtent
.DiskNumber
;
2108 if (CheckVentoyDisk(VtoyDiskNum
))
2110 Log("Disk check OK %C: %u", VtoyLetter
, VtoyDiskNum
);
2114 Log("Failed to check ventoy disk %u", VtoyDiskNum
);
2118 g_vtoy_disk_drive
= VtoyDiskNum
;
2120 Drives
= GetLogicalDrives();
2121 Log("Drives before mount: 0x%x", Drives
);
2123 rc
= MountIsoFile(IsoPath
, VtoyDiskNum
);
2125 NewDrives
= GetLogicalDrives();
2126 Log("Drives after mount: 0x%x (0x%x)", NewDrives
, (NewDrives
^ Drives
));
2129 NewDrives
= (NewDrives
^ Drives
);
2132 if (NewDrives
& 0x01)
2134 if ((NewDrives
>> 1) == 0)
2136 Log("The ISO file is mounted at %C:", MntLetter
);
2140 Log("Maybe the ISO file is mounted at %C:", MntLetter
);
2149 Log("Mount ISO FILE: %s", rc
== 0 ? "SUCCESS" : "FAILED");
2151 //Windows 11 bypass check
2152 if (g_windows_data
.windows11_bypass_check
== 1)
2154 Windows11BypassCheck(IsoPath
, MntLetter
);
2158 rc
= DeleteVentoyPart2MountPoint(VtoyDiskNum
);
2159 Log("Delete ventoy mountpoint: %s", rc
== 0 ? "SUCCESS" : "NO NEED");
2161 if (g_windows_data
.auto_install_script
[0])
2163 if (IsFileExist("%s", VTOY_AUTO_FILE
))
2165 Log("use auto install script %s...", VTOY_AUTO_FILE
);
2166 ProcessUnattendedInstallation(VTOY_AUTO_FILE
);
2170 Log("auto install script %s not exist", IsoPath
);
2175 Log("auto install no need");
2178 if (g_windows_data
.injection_archive
[0])
2180 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", VtoyLetter
, g_windows_data
.injection_archive
);
2181 if (IsFileExist("%s", IsoPath
))
2183 Log("decompress injection archive %s...", IsoPath
);
2184 DecompressInjectionArchive(IsoPath
, VtoyDiskNum
);
2186 if (IsFileExist("%s", AUTO_RUN_BAT
))
2189 DWORD flags
= CREATE_NO_WINDOW
;
2192 PROCESS_INFORMATION Pi
;
2193 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
2195 Log("%s exist, now run it...", AUTO_RUN_BAT
);
2197 GetStartupInfoA(&Si
);
2199 hOut
= CreateFileA(AUTO_RUN_LOG
,
2201 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
2204 FILE_ATTRIBUTE_NORMAL
,
2207 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
2208 if (hOut
!= INVALID_HANDLE_VALUE
)
2210 Si
.hStdError
= hOut
;
2211 Si
.hStdOutput
= hOut
;
2214 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
2215 sprintf_s(StrBuf
, sizeof(StrBuf
), "cmd.exe /c %s \"%s\" %C", AUTO_RUN_BAT
, IsoPath
, MntLetter
);
2216 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
2217 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2219 SAFE_CLOSE_HANDLE(hOut
);
2223 Log("%s not exist...", AUTO_RUN_BAT
);
2228 Log("injection archive %s not exist", IsoPath
);
2233 Log("no injection archive found");
2239 static int ExtractWindowsDataFile(char *databuf
)
2242 char *filedata
= NULL
;
2243 ventoy_windows_data
*pdata
= (ventoy_windows_data
*)databuf
;
2245 Log("ExtractWindowsDataFile: auto install <%s:%d>", pdata
->auto_install_script
, pdata
->auto_install_len
);
2247 filedata
= databuf
+ sizeof(ventoy_windows_data
);
2249 if (pdata
->auto_install_script
[0] && pdata
->auto_install_len
> 0)
2251 SaveBuffer2File(VTOY_AUTO_FILE
, filedata
, pdata
->auto_install_len
);
2252 filedata
+= pdata
->auto_install_len
;
2253 len
= pdata
->auto_install_len
;
2259 int VentoyJumpWimboot(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
2266 Log("VentoyJumpWimboot %dbit", g_system_bit
);
2268 sprintf_s(LunchFile
, MAX_PATH
, "X:\\setup.exe");
2270 ReadWholeFile2Buf("wimboot.data", &buf
, &size
);
2271 Log("wimboot.data size:%d", size
);
2273 memcpy(&g_os_param
, buf
, sizeof(ventoy_os_param
));
2274 memcpy(&g_windows_data
, buf
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
2275 ExtractWindowsDataFile(buf
+ sizeof(ventoy_os_param
));
2276 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
2278 if (g_os_param_reserved
[0] == 1)
2280 Log("break here for debug .....");
2285 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
2287 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
2289 g_os_param
.vtoy_img_path
[Pos
] = '\\';
2293 if (g_os_param_reserved
[0] == 2)
2295 Log("skip hook for debug .....");
2300 rc
= VentoyHook(&g_os_param
);
2312 static int ventoy_check_create_directory(void)
2314 if (IsDirExist("ventoy"))
2316 Log("ventoy directory already exist");
2320 Log("ventoy directory not exist, now create it.");
2321 if (!CreateDirectoryA("ventoy", NULL
))
2323 Log("Failed to create ventoy directory err:%u", GetLastError());
2331 int VentoyJump(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
2339 DWORD LockStatus
= 0;
2340 BYTE
*Buffer
= NULL
;
2341 CHAR ExeFileName
[MAX_PATH
];
2343 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s", argv
[0]);
2344 if (!IsFileExist("%s", ExeFileName
))
2346 Log("File %s NOT exist, now try %s.exe", ExeFileName
, ExeFileName
);
2347 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s.exe", argv
[0]);
2349 Log("File %s exist ? %s", ExeFileName
, IsFileExist("%s", ExeFileName
) ? "YES" : "NO");
2352 if (ReadWholeFile2Buf(ExeFileName
, (void **)&Buffer
, &FileSize
))
2357 Log("VentoyJump %dbit", g_system_bit
);
2359 MUTEX_LOCK(g_vtoyins_mutex
);
2360 stat
= ventoy_check_create_directory();
2361 MUTEX_UNLOCK(g_vtoyins_mutex
);
2368 for (PeStart
= 0; PeStart
< FileSize
; PeStart
+= 16)
2370 if (CheckOsParam((ventoy_os_param
*)(Buffer
+ PeStart
)) &&
2371 CheckPeHead(Buffer
, FileSize
, PeStart
+ sizeof(ventoy_os_param
)))
2373 Log("Find os pararm at %u", PeStart
);
2375 memcpy(&g_os_param
, Buffer
+ PeStart
, sizeof(ventoy_os_param
));
2376 memcpy(&g_windows_data
, Buffer
+ PeStart
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
2377 exlen
= ExtractWindowsDataFile(Buffer
+ PeStart
+ sizeof(ventoy_os_param
));
2378 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
2380 if (g_os_param_reserved
[0] == 1)
2382 Log("break here for debug .....");
2387 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
2389 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
2391 g_os_param
.vtoy_img_path
[Pos
] = '\\';
2395 PeStart
+= sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
) + exlen
;
2396 sprintf_s(LunchFile
, MAX_PATH
, "ventoy\\%s", GetFileNameInPath(ExeFileName
));
2398 MUTEX_LOCK(g_vtoyins_mutex
);
2399 if (IsFileExist("%s", LunchFile
))
2401 Log("vtoyjump multiple call ...");
2403 MUTEX_UNLOCK(g_vtoyins_mutex
);
2407 SaveBuffer2File(LunchFile
, Buffer
+ PeStart
, FileSize
- PeStart
);
2408 MUTEX_UNLOCK(g_vtoyins_mutex
);
2414 if (PeStart
>= FileSize
)
2416 Log("OS param not found");
2420 if (g_os_param_reserved
[0] == 2)
2422 Log("skip hook for debug .....");
2427 rc
= VentoyHook(&g_os_param
);
2440 int real_main(int argc
, char **argv
)
2444 CHAR NewFile
[MAX_PATH
];
2445 CHAR LunchFile
[MAX_PATH
];
2446 CHAR CallParam
[1024] = { 0 };
2448 PROCESS_INFORMATION Pi
;
2450 Log("#### real_main #### argc = %d", argc
);
2451 Log("program full path: <%s>", g_prog_full_path
);
2452 Log("program dir: <%s>", g_prog_dir
);
2453 Log("program name:: <%s>", g_prog_name
);
2455 Log("argc = %d", argc
);
2456 for (i
= 0; i
< argc
; i
++)
2458 Log("argv[%d]=<%s>", i
, argv
[i
]);
2461 strcat_s(CallParam
, sizeof(CallParam
), " ");
2462 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
2466 GetStartupInfoA(&Si
);
2467 memset(LunchFile
, 0, sizeof(LunchFile
));
2469 if (strstr(argv
[0], "vtoyjump.exe"))
2471 rc
= VentoyJumpWimboot(argc
, argv
, LunchFile
);
2475 rc
= VentoyJump(argc
, argv
, LunchFile
);
2478 Log("LunchFile=<%s> CallParam=<%s>", LunchFile
, CallParam
);
2480 if (_stricmp(g_prog_name
, "winpeshl.exe") != 0 && IsFileExist("ventoy\\%s", g_prog_name
))
2482 sprintf_s(NewFile
, sizeof(NewFile
), "%s_BACK.EXE", g_prog_full_path
);
2483 MoveFileA(g_prog_full_path
, NewFile
);
2484 Log("Move <%s> to <%s>", g_prog_full_path
, NewFile
);
2486 sprintf_s(NewFile
, sizeof(NewFile
), "ventoy\\%s", g_prog_name
);
2487 CopyFileA(NewFile
, g_prog_full_path
, TRUE
);
2488 Log("Copy <%s> to <%s>", NewFile
, g_prog_full_path
);
2490 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", g_prog_full_path
);
2491 Log("Final lunchFile is <%s>", LunchFile
);
2495 Log("We don't need to recover original <%s>", g_prog_name
);
2498 if (g_os_param_reserved
[0] == 3)
2500 Log("Open log for debug ...");
2501 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "notepad.exe ventoy.log");
2507 strcat_s(LunchFile
, sizeof(LunchFile
), CallParam
);
2509 else if (NULL
== strstr(LunchFile
, "setup.exe"))
2511 Log("Not setup.exe, hide windows.");
2512 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
2513 Si
.wShowWindow
= SW_HIDE
;
2516 Log("Ventoy jump %s ...", rc
== 0 ? "success" : "failed");
2519 Log("Now launch <%s> ...", LunchFile
);
2521 if (g_os_param_reserved
[0] == 4)
2523 Log("Open cmd for debug ...");
2524 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "cmd.exe");
2527 Log("Backup log at this point");
2528 CopyFileA(LOG_FILE
, "X:\\Windows\\ventoy.backup", TRUE
);
2530 CreateProcessA(NULL
, LunchFile
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
2532 for (i
= 0; rc
&& i
< 1800; i
++)
2534 Log("Ventoy hook failed, now wait and retry ...");
2536 rc
= VentoyHook(&g_os_param
);
2539 Log("Wait process...");
2540 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2542 Log("vtoyjump finished");
2546 static void VentoyToUpper(CHAR
*str
)
2549 for (i
= 0; str
[i
]; i
++)
2551 str
[i
] = (CHAR
)toupper(str
[i
]);
2555 int main(int argc
, char **argv
)
2559 PROCESS_INFORMATION Pi
;
2560 CHAR CurDir
[MAX_PATH
];
2561 CHAR NewArgv0
[MAX_PATH
];
2562 CHAR CallParam
[1024] = { 0 };
2564 g_vtoylog_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYLOG_LOCK");
2565 g_vtoyins_mutex
= CreateMutexA(NULL
, FALSE
, "VTOYINS_LOCK");
2567 Log("######## VentoyJump %dbit ##########", g_system_bit
);
2569 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
2570 Log("Current directory is <%s>", CurDir
);
2572 GetModuleFileNameA(NULL
, g_prog_full_path
, MAX_PATH
);
2573 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
2575 Log("EXE path: <%s> dir:<%s> name:<%s>", g_prog_full_path
, g_prog_dir
, g_prog_name
);
2577 if (_stricmp(g_prog_name
, "WinLogon.exe") == 0)
2579 Log("This time is rejump back ...");
2581 strcpy_s(g_prog_full_path
, sizeof(g_prog_full_path
), argv
[1]);
2582 split_path_name(g_prog_full_path
, g_prog_dir
, g_prog_name
);
2584 return real_main(argc
- 1, argv
+ 1);
2586 else if (_stricmp(g_prog_name
, "PECMD.exe") == 0)
2588 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_dir
);
2589 VentoyToUpper(NewArgv0
);
2591 if (NULL
== strstr(NewArgv0
, "SYSTEM32") && IsFileExist(ORG_PECMD_BK_PATH
))
2593 Log("Just call original pecmd.exe");
2594 strcpy_s(CallParam
, sizeof(CallParam
), ORG_PECMD_PATH
);
2598 Log("We need to rejump for pecmd ...");
2600 ventoy_check_create_directory();
2601 CopyFileA(g_prog_full_path
, "ventoy\\WinLogon.exe", TRUE
);
2603 sprintf_s(CallParam
, sizeof(CallParam
), "ventoy\\WinLogon.exe %s", g_prog_full_path
);
2606 for (i
= 1; i
< argc
; i
++)
2608 strcat_s(CallParam
, sizeof(CallParam
), " ");
2609 strcat_s(CallParam
, sizeof(CallParam
), argv
[i
]);
2612 Log("Now rejump to <%s> ...", CallParam
);
2613 GetStartupInfoA(&Si
);
2614 CreateProcessA(NULL
, CallParam
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
2616 Log("Wait rejump process...");
2617 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
2618 Log("rejump finished");
2623 Log("We don't need to rejump ...");
2625 strcpy_s(NewArgv0
, sizeof(NewArgv0
), g_prog_full_path
);
2628 return real_main(argc
, argv
);