1 /******************************************************************************
4 * Copyright (c) 2020, 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 BOOL g_64bit_system
= FALSE
;
35 static ventoy_guid g_ventoy_guid
= VENTOY_GUID
;
37 void Log(const char *Fmt
, ...)
46 Len
+= sprintf_s(szBuf
, sizeof(szBuf
),
47 "[%4d/%02d/%02d %02d:%02d:%02d.%03d] ",
48 Sys
.wYear
, Sys
.wMonth
, Sys
.wDay
,
49 Sys
.wHour
, Sys
.wMinute
, Sys
.wSecond
,
53 Len
+= vsnprintf_s(szBuf
+ Len
, sizeof(szBuf
)-Len
, sizeof(szBuf
)-Len
, Fmt
, Arg
);
56 fopen_s(&File
, "ventoy.log", "a+");
59 fwrite(szBuf
, 1, Len
, File
);
60 fwrite("\n", 1, 1, File
);
66 static int LoadNtDriver(const char *DrvBinPath
)
72 SC_HANDLE hServiceMgr
;
74 char name
[256] = { 0 };
76 for (i
= (int)strlen(DrvBinPath
) - 1; i
>= 0; i
--)
78 if (DrvBinPath
[i
] == '\\' || DrvBinPath
[i
] == '/')
80 sprintf_s(name
, sizeof(name
), "%s", DrvBinPath
+ i
+ 1);
85 Log("Load NT driver: %s %s", DrvBinPath
, name
);
87 hServiceMgr
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
88 if (hServiceMgr
== NULL
)
90 Log("OpenSCManager failed Error:%u", GetLastError());
94 Log("OpenSCManager OK");
96 hService
= CreateServiceA(hServiceMgr
,
100 SERVICE_KERNEL_DRIVER
,
101 SERVICE_DEMAND_START
,
102 SERVICE_ERROR_NORMAL
,
104 NULL
, NULL
, NULL
, NULL
, NULL
);
105 if (hService
== NULL
)
107 Status
= GetLastError();
108 if (Status
!= ERROR_IO_PENDING
&& Status
!= ERROR_SERVICE_EXISTS
)
110 Log("CreateService failed v %u", Status
);
111 CloseServiceHandle(hServiceMgr
);
115 hService
= OpenServiceA(hServiceMgr
, name
, SERVICE_ALL_ACCESS
);
116 if (hService
== NULL
)
118 Log("OpenService failed %u", Status
);
119 CloseServiceHandle(hServiceMgr
);
124 Log("CreateService imdisk OK");
126 Ret
= StartServiceA(hService
, 0, NULL
);
129 Log("StartService OK");
133 Status
= GetLastError();
134 if (Status
== ERROR_SERVICE_ALREADY_RUNNING
)
140 Log("StartService error %u", Status
);
145 CloseServiceHandle(hService
);
146 CloseServiceHandle(hServiceMgr
);
148 Log("Load NT driver %s", rc
? "failed" : "success");
153 static int ReadWholeFile2Buf(const char *Fullpath
, void **Data
, DWORD
*Size
)
161 Log("ReadWholeFile2Buf <%s>", Fullpath
);
163 Handle
= CreateFileA(Fullpath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
164 if (Handle
== INVALID_HANDLE_VALUE
)
166 Log("Could not open the file<%s>, error:%u", Fullpath
, GetLastError());
170 FileSize
= SetFilePointer(Handle
, 0, NULL
, FILE_END
);
172 Buffer
= malloc(FileSize
);
175 Log("Failed to alloc memory size:%u", FileSize
);
179 SetFilePointer(Handle
, 0, NULL
, FILE_BEGIN
);
180 if (!ReadFile(Handle
, Buffer
, FileSize
, &dwSize
, NULL
))
182 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
189 Log("Success read file size:%u", FileSize
);
194 SAFE_CLOSE_HANDLE(Handle
);
199 static BOOL
CheckPeHead(BYTE
*Head
)
203 if (Head
[0] != 'M' || Head
[1] != 'Z')
208 PeOffset
= *(UINT32
*)(Head
+ 60);
209 if (*(UINT32
*)(Head
+ PeOffset
) != 0x00004550)
217 static BOOL
IsPe64(BYTE
*buffer
)
221 if (!CheckPeHead(buffer
))
226 pe_off
= *(UINT32
*)(buffer
+ 60);
227 if (*(UINT16
*)(buffer
+ pe_off
+ 24) == 0x020b)
236 static BOOL
CheckOsParam(ventoy_os_param
*param
)
241 if (memcmp(¶m
->guid
, &g_ventoy_guid
, sizeof(ventoy_guid
)))
246 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
248 Sum
+= *((BYTE
*)param
+ i
);
256 if (param
->vtoy_img_location_addr
% 4096)
264 static int SaveBuffer2File(const char *Fullpath
, void *Buffer
, DWORD Length
)
270 Log("SaveBuffer2File <%s> len:%u", Fullpath
, Length
);
272 Handle
= CreateFileA(Fullpath
, GENERIC_READ
| GENERIC_WRITE
,
273 FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, CREATE_NEW
, 0, 0);
274 if (Handle
== INVALID_HANDLE_VALUE
)
276 Log("Could not create new file, error:%u", GetLastError());
280 WriteFile(Handle
, Buffer
, Length
, &dwSize
, NULL
);
285 SAFE_CLOSE_HANDLE(Handle
);
290 static BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...)
295 CHAR FilePath
[MAX_PATH
];
298 vsnprintf_s(FilePath
, sizeof(FilePath
), sizeof(FilePath
), Fmt
, Arg
);
301 hFile
= CreateFileA(FilePath
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
302 if (INVALID_HANDLE_VALUE
== hFile
)
309 Attr
= GetFileAttributesA(FilePath
);
313 if ((Attr
& FILE_ATTRIBUTE_DIRECTORY
) == 0)
320 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
329 static int GetPhyDiskUUID(const char LogicalDrive
, UINT8
*UUID
, DISK_EXTENT
*DiskExtent
)
334 VOLUME_DISK_EXTENTS DiskExtents
;
336 UINT8 SectorBuf
[512];
338 Log("GetPhyDiskUUID %C", LogicalDrive
);
340 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", LogicalDrive
);
341 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
342 if (Handle
== INVALID_HANDLE_VALUE
)
344 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
348 Ret
= DeviceIoControl(Handle
,
349 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
353 (DWORD
)(sizeof(DiskExtents
)),
356 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
358 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed, error:%u", GetLastError());
364 memcpy(DiskExtent
, DiskExtents
.Extents
, sizeof(DiskExtent
));
365 Log("%C: is in PhysicalDrive%d ", LogicalDrive
, DiskExtents
.Extents
[0].DiskNumber
);
367 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskExtents
.Extents
[0].DiskNumber
);
368 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
369 if (Handle
== INVALID_HANDLE_VALUE
)
371 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
375 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
377 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
382 memcpy(UUID
, SectorBuf
+ 0x180, 16);
387 int VentoyMountISOByAPI(const char *IsoPath
)
391 WCHAR wFilePath
[512] = { 0 };
392 VIRTUAL_STORAGE_TYPE StorageType
;
393 OPEN_VIRTUAL_DISK_PARAMETERS OpenParameters
;
394 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
396 Log("VentoyMountISOByAPI <%s>", IsoPath
);
398 MultiByteToWideChar(CP_ACP
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
400 memset(&StorageType
, 0, sizeof(StorageType
));
401 memset(&OpenParameters
, 0, sizeof(OpenParameters
));
402 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
404 OpenParameters
.Version
= OPEN_VIRTUAL_DISK_VERSION_1
;
405 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
407 Status
= OpenVirtualDisk(&StorageType
, wFilePath
, VIRTUAL_DISK_ACCESS_READ
, 0, &OpenParameters
, &Handle
);
408 if (Status
!= ERROR_SUCCESS
)
410 if (ERROR_VIRTDISK_PROVIDER_NOT_FOUND
== Status
)
412 Log("VirtualDisk for ISO file is not supported in current system");
416 Log("Failed to open virtual disk ErrorCode:%u", Status
);
421 Log("OpenVirtualDisk success");
423 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
424 if (Status
!= ERROR_SUCCESS
)
426 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
436 static HANDLE g_FatPhyDrive
;
437 static UINT64 g_Part2StartSec
;
439 static int CopyFileFromFatDisk(const CHAR
* SrcFile
, const CHAR
*DstFile
)
446 Log("CopyFileFromFatDisk (%s)==>(%s)", SrcFile
, DstFile
);
448 flfile
= fl_fopen(SrcFile
, "rb");
451 fl_fseek(flfile
, 0, SEEK_END
);
452 size
= (int)fl_ftell(flfile
);
453 fl_fseek(flfile
, 0, SEEK_SET
);
455 buf
= (char *)malloc(size
);
458 fl_fread(buf
, 1, size
, flfile
);
461 SaveBuffer2File(DstFile
, buf
, size
);
471 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
476 LARGE_INTEGER liCurrentPosition
;
478 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
479 liCurrentPosition
.QuadPart
*= 512;
480 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
482 ReadSize
= (DWORD
)(SectorCount
* 512);
484 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
485 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
487 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet
, ReadSize
, dwSize
, GetLastError());
493 static CHAR
GetMountLogicalDrive(void)
497 DWORD Mask
= 0x1000000;
499 Drives
= GetLogicalDrives();
500 Log("Drives=0x%x", Drives
);
504 if ((Drives
& Mask
) == 0)
516 int VentoyMountISOByImdisk(const char *IsoPath
, DWORD PhyDrive
)
523 CHAR PhyPath
[MAX_PATH
];
525 PROCESS_INFORMATION Pi
;
526 GET_LENGTH_INFORMATION LengthInfo
;
528 Log("VentoyMountISOByImdisk %s", IsoPath
);
530 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
531 hDrive
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
532 if (hDrive
== INVALID_HANDLE_VALUE
)
534 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
538 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
541 Log("Could not get phy disk %s size, error:%u", PhyPath
, GetLastError());
545 g_FatPhyDrive
= hDrive
;
546 g_Part2StartSec
= (LengthInfo
.Length
.QuadPart
- VENTOY_EFI_PART_SIZE
) / 512;
548 Log("Parse FAT fs...");
552 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
556 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.sys", "ventoy\\imdisk.sys");
557 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.exe", "ventoy\\imdisk.exe");
558 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.cpl", "ventoy\\imdisk.cpl");
562 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.sys", "ventoy\\imdisk.sys");
563 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");
564 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");
567 GetCurrentDirectoryA(sizeof(PhyPath
), PhyPath
);
568 strcat_s(PhyPath
, sizeof(PhyPath
), "\\ventoy\\imdisk.sys");
570 if (LoadNtDriver(PhyPath
) == 0)
574 Letter
= GetMountLogicalDrive();
575 sprintf_s(PhyPath
, sizeof(PhyPath
), "ventoy\\imdisk.exe -a -o ro -f %s -m %C:", IsoPath
, Letter
);
577 Log("mount iso to %C: use imdisk cmd <%s>", Letter
, PhyPath
);
579 GetStartupInfoA(&Si
);
581 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
582 Si
.wShowWindow
= SW_HIDE
;
584 CreateProcessA(NULL
, PhyPath
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
585 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
592 SAFE_CLOSE_HANDLE(hDrive
);
597 static int MountIsoFile(CONST CHAR
*IsoPath
, DWORD PhyDrive
)
599 if (IsWindows8OrGreater())
601 Log("This is Windows 8 or latter...");
602 if (VentoyMountISOByAPI(IsoPath
) == 0)
604 Log("Mount iso by API success");
609 Log("Mount iso by API failed, maybe not supported, try imdisk");
610 return VentoyMountISOByImdisk(IsoPath
, PhyDrive
);
615 Log("This is before Windows 8 ...");
616 if (VentoyMountISOByImdisk(IsoPath
, PhyDrive
) == 0)
618 Log("Mount iso by imdisk success");
623 return VentoyMountISOByAPI(IsoPath
);
628 static int GetPhyDriveByLogicalDrive(int DriveLetter
)
633 VOLUME_DISK_EXTENTS DiskExtents
;
636 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", (CHAR
)DriveLetter
);
638 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
639 if (Handle
== INVALID_HANDLE_VALUE
)
641 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
645 Ret
= DeviceIoControl(Handle
,
646 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
650 (DWORD
)(sizeof(DiskExtents
)),
654 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
656 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, GetLastError());
657 SAFE_CLOSE_HANDLE(Handle
);
660 SAFE_CLOSE_HANDLE(Handle
);
662 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
664 DiskExtents
.Extents
[0].DiskNumber
,
665 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
666 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
669 return (int)DiskExtents
.Extents
[0].DiskNumber
;
673 static int DeleteVentoyPart2MountPoint(DWORD PhyDrive
)
678 CHAR DriveName
[] = "?:\\";
680 Log("DeleteVentoyPart2MountPoint Phy%u ...", PhyDrive
);
682 Drives
= GetLogicalDrives();
685 if ((Drives
& 0x01) && IsPathExist(FALSE
, "%C:\\ventoy\\ventoy.cpio", Letter
))
687 Log("File %C:\\ventoy\\ventoy.cpio exist", Letter
);
689 PhyDisk
= GetPhyDriveByLogicalDrive(Letter
);
690 Log("PhyDisk=%u for %C", PhyDisk
, Letter
);
692 if (PhyDisk
== PhyDrive
)
694 DriveName
[0] = Letter
;
695 DeleteVolumeMountPointA(DriveName
);
707 static BOOL
check_tar_archive(const char *archive
, CHAR
*tarName
)
711 const char *pos
= archive
;
712 const char *slash
= archive
;
716 if (*pos
== '\\' || *pos
== '/')
723 len
= (int)strlen(slash
);
725 if (len
> 7 && (strncmp(slash
+ len
- 7, ".tar.gz", 7) == 0 || strncmp(slash
+ len
- 7, ".tar.xz", 7) == 0))
727 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
728 tarName
[nameLen
- 3] = 0;
731 else if (len
> 8 && strncmp(slash
+ len
- 8, ".tar.bz2", 8) == 0)
733 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
734 tarName
[nameLen
- 4] = 0;
737 else if (len
> 9 && strncmp(slash
+ len
- 9, ".tar.lzma", 9) == 0)
739 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
740 tarName
[nameLen
- 5] = 0;
747 static int DecompressInjectionArchive(const char *archive
, DWORD PhyDrive
)
754 DWORD flags
= CREATE_NO_WINDOW
;
755 CHAR StrBuf
[MAX_PATH
];
756 CHAR tarName
[MAX_PATH
];
758 PROCESS_INFORMATION Pi
;
759 PROCESS_INFORMATION NewPi
;
760 GET_LENGTH_INFORMATION LengthInfo
;
761 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
763 Log("DecompressInjectionArchive %s", archive
);
765 sprintf_s(StrBuf
, sizeof(StrBuf
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
766 hDrive
= CreateFileA(StrBuf
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
767 if (hDrive
== INVALID_HANDLE_VALUE
)
769 Log("Could not open the disk<%s>, error:%u", StrBuf
, GetLastError());
773 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
776 Log("Could not get phy disk %s size, error:%u", StrBuf
, GetLastError());
780 g_FatPhyDrive
= hDrive
;
781 g_Part2StartSec
= (LengthInfo
.Length
.QuadPart
- VENTOY_EFI_PART_SIZE
) / 512;
783 Log("Parse FAT fs...");
787 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
791 CopyFileFromFatDisk("/ventoy/7z/64/7za.exe", "ventoy\\7za.exe");
795 CopyFileFromFatDisk("/ventoy/7z/32/7za.exe", "ventoy\\7za.exe");
798 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", archive
);
800 Log("extract inject to X:");
801 Log("cmdline:<%s>", StrBuf
);
803 GetStartupInfoA(&Si
);
805 hOut
= CreateFileA("ventoy\\7z.log",
807 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
810 FILE_ATTRIBUTE_NORMAL
,
813 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
815 if (hOut
!= INVALID_HANDLE_VALUE
)
818 Si
.hStdOutput
= hOut
;
821 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
822 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
825 // decompress tar archive, for tar.gz/tar.xz/tar.bz2
827 if (check_tar_archive(archive
, tarName
))
829 Log("Decompress tar archive...<%s>", tarName
);
831 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", tarName
);
833 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &NewPi
);
834 WaitForSingleObject(NewPi
.hProcess
, INFINITE
);
836 Log("Now delete %s", tarName
);
837 DeleteFileA(tarName
);
840 SAFE_CLOSE_HANDLE(hOut
);
846 SAFE_CLOSE_HANDLE(hDrive
);
851 static int ProcessUnattendedInstallation(const char *script
)
857 CHAR CurDir
[MAX_PATH
];
859 Log("Copy unattended XML ...");
861 GetCurrentDirectory(sizeof(CurDir
), CurDir
);
863 if ((Letter
>= 'A' && Letter
<= 'Z') || (Letter
>= 'a' && Letter
<= 'z'))
865 Log("Current Drive Letter: %C", Letter
);
872 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\Autounattend.xml", Letter
);
873 Log("Copy file <%s> --> <%s>", script
, CurDir
);
874 CopyFile(script
, CurDir
, FALSE
);
876 Ret
= RegCreateKeyEx(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dw
);
877 if (ERROR_SUCCESS
== Ret
)
879 Ret
= RegSetValueEx(hKey
, "UnattendFile", 0, REG_SZ
, CurDir
, (DWORD
)(strlen(CurDir
) + 1));
885 static int VentoyHook(ventoy_os_param
*param
)
889 DISK_EXTENT DiskExtent
;
890 DWORD Drives
= GetLogicalDrives();
892 CHAR IsoPath
[MAX_PATH
];
894 Log("Logical Drives=0x%x Path:<%s>", Drives
, param
->vtoy_img_path
);
900 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
901 if (IsPathExist(FALSE
, "%s", IsoPath
))
903 Log("File exist under %C:", Letter
);
904 if (GetPhyDiskUUID(Letter
, UUID
, &DiskExtent
) == 0)
906 if (memcmp(UUID
, param
->vtoy_disk_guid
, 16) == 0)
908 Log("Disk UUID match");
915 Log("File NOT exist under %C:", Letter
);
925 Log("Failed to find ISO file");
929 Log("Find ISO file <%s>", IsoPath
);
931 rc
= MountIsoFile(IsoPath
, DiskExtent
.DiskNumber
);
932 Log("Mount ISO FILE: %s", rc
== 0 ? "SUCCESS" : "FAILED");
935 rc
= DeleteVentoyPart2MountPoint(DiskExtent
.DiskNumber
);
936 Log("Delete ventoy mountpoint: %s", rc
== 0 ? "SUCCESS" : "NO NEED");
938 if (g_windows_data
.auto_install_script
[0])
940 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", Letter
, g_windows_data
.auto_install_script
);
941 if (IsPathExist(FALSE
, "%s", IsoPath
))
943 Log("use auto install script %s...", IsoPath
);
944 ProcessUnattendedInstallation(IsoPath
);
948 Log("auto install script %s not exist", IsoPath
);
953 Log("auto install no need");
956 if (g_windows_data
.injection_archive
[0])
958 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", Letter
, g_windows_data
.injection_archive
);
959 if (IsPathExist(FALSE
, "%s", IsoPath
))
961 Log("decompress injection archive %s...", IsoPath
);
962 DecompressInjectionArchive(IsoPath
, DiskExtent
.DiskNumber
);
966 Log("injection archive %s not exist", IsoPath
);
971 Log("no injection archive found");
977 const char * GetFileNameInPath(const char *fullpath
)
980 const char *pos
= NULL
;
982 if (strstr(fullpath
, ":"))
984 for (i
= (int)strlen(fullpath
); i
> 0; i
--)
986 if (fullpath
[i
- 1] == '/' || fullpath
[i
- 1] == '\\')
996 int VentoyJump(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
1002 BYTE
*Buffer
= NULL
;
1003 CHAR ExeFileName
[MAX_PATH
];
1005 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s", argv
[0]);
1006 if (!IsPathExist(FALSE
, "%s", ExeFileName
))
1008 Log("File %s NOT exist, now try %s.exe", ExeFileName
, ExeFileName
);
1009 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s.exe", argv
[0]);
1011 Log("File %s exist ? %s", ExeFileName
, IsPathExist(FALSE
, "%s", ExeFileName
) ? "YES" : "NO");
1014 if (ReadWholeFile2Buf(ExeFileName
, (void **)&Buffer
, &FileSize
))
1019 g_64bit_system
= IsPe64(Buffer
);
1021 if (!IsPathExist(TRUE
, "ventoy"))
1023 if (!CreateDirectoryA("ventoy", NULL
))
1025 Log("Failed to create ventoy directory err:%u", GetLastError());
1030 for (PeStart
= 0; PeStart
< FileSize
; PeStart
+= 16)
1032 if (CheckOsParam((ventoy_os_param
*)(Buffer
+ PeStart
)) &&
1033 CheckPeHead(Buffer
+ PeStart
+ sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
)))
1035 Log("Find os pararm at %u", PeStart
);
1037 memcpy(&g_os_param
, Buffer
+ PeStart
, sizeof(ventoy_os_param
));
1038 memcpy(&g_windows_data
, Buffer
+ PeStart
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
1039 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
1041 if (g_os_param_reserved
[0] == 1)
1043 Log("break here for debug .....");
1048 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
1050 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
1052 g_os_param
.vtoy_img_path
[Pos
] = '\\';
1056 PeStart
+= sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
);
1057 sprintf_s(LunchFile
, MAX_PATH
, "ventoy\\%s", GetFileNameInPath(ExeFileName
));
1058 SaveBuffer2File(LunchFile
, Buffer
+ PeStart
, FileSize
- PeStart
);
1063 if (PeStart
>= FileSize
)
1065 Log("OS param not found");
1069 if (g_os_param_reserved
[0] == 2)
1071 Log("skip hook for debug .....");
1076 rc
= VentoyHook(&g_os_param
);
1088 int main(int argc
, char **argv
)
1093 CHAR CurDir
[MAX_PATH
];
1094 CHAR LunchFile
[MAX_PATH
];
1096 PROCESS_INFORMATION Pi
;
1098 if (argv
[0] && argv
[0][0] && argv
[0][1] == ':')
1100 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
1102 strcpy_s(LunchFile
, sizeof(LunchFile
), argv
[0]);
1103 Pos
= (char *)GetFileNameInPath(LunchFile
);
1105 strcat_s(CurDir
, sizeof(CurDir
), "\\");
1106 strcat_s(CurDir
, sizeof(CurDir
), Pos
);
1108 if (_stricmp(argv
[0], CurDir
) != 0)
1111 SetCurrentDirectoryA(LunchFile
);
1115 Log("######## VentoyJump ##########");
1116 Log("argc = %d argv[0] = <%s>", argc
, argv
[0]);
1118 if (Pos
&& *Pos
== 0)
1120 Log("Old current directory = <%s>", CurDir
);
1121 Log("New current directory = <%s>", LunchFile
);
1125 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
1126 Log("Current directory = <%s>", CurDir
);
1129 GetStartupInfoA(&Si
);
1131 memset(LunchFile
, 0, sizeof(LunchFile
));
1132 rc
= VentoyJump(argc
, argv
, LunchFile
);
1134 if (g_os_param_reserved
[0] == 3)
1136 Log("Open log for debug ...");
1137 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "notepad.exe ventoy.log");
1141 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
1142 Si
.wShowWindow
= SW_HIDE
;
1143 Log("Ventoy jump %s ...", rc
== 0 ? "success" : "failed");
1146 CreateProcessA(NULL
, LunchFile
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1150 Log("Ventoy hook failed, now wait and retry ...");
1153 rc
= VentoyHook(&g_os_param
);
1156 WaitForSingleObject(Pi
.hProcess
, INFINITE
);