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 int IsUTF8Encode(const char *src
)
293 const UCHAR
*Byte
= (const UCHAR
*)src
;
295 for (i
= 0; i
< MAX_PATH
&& Byte
[i
]; i
++)
306 static int Utf8ToUtf16(const char* src
, WCHAR
* dst
)
308 int size
= MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, 0);
309 return MultiByteToWideChar(CP_UTF8
, 0, src
, -1, dst
, size
+ 1);
312 static BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...)
318 CHAR FilePathA
[MAX_PATH
];
319 WCHAR FilePathW
[MAX_PATH
];
322 vsnprintf_s(FilePathA
, sizeof(FilePathA
), sizeof(FilePathA
), Fmt
, Arg
);
325 UTF8
= IsUTF8Encode(FilePathA
);
329 Utf8ToUtf16(FilePathA
, FilePathW
);
330 hFile
= CreateFileW(FilePathW
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
334 hFile
= CreateFileA(FilePathA
, FILE_READ_EA
, FILE_SHARE_READ
, 0, OPEN_EXISTING
, 0, 0);
336 if (INVALID_HANDLE_VALUE
== hFile
)
345 Attr
= GetFileAttributesW(FilePathW
);
349 Attr
= GetFileAttributesA(FilePathA
);
354 if ((Attr
& FILE_ATTRIBUTE_DIRECTORY
) == 0)
361 if (Attr
& FILE_ATTRIBUTE_DIRECTORY
)
370 static int GetPhyDiskUUID(const char LogicalDrive
, UINT8
*UUID
, DISK_EXTENT
*DiskExtent
)
375 VOLUME_DISK_EXTENTS DiskExtents
;
377 UINT8 SectorBuf
[512];
379 Log("GetPhyDiskUUID %C", LogicalDrive
);
381 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", LogicalDrive
);
382 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
383 if (Handle
== INVALID_HANDLE_VALUE
)
385 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
389 Ret
= DeviceIoControl(Handle
,
390 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
394 (DWORD
)(sizeof(DiskExtents
)),
397 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
399 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed, error:%u", GetLastError());
405 memcpy(DiskExtent
, DiskExtents
.Extents
, sizeof(DiskExtent
));
406 Log("%C: is in PhysicalDrive%d ", LogicalDrive
, DiskExtents
.Extents
[0].DiskNumber
);
408 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", DiskExtents
.Extents
[0].DiskNumber
);
409 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
410 if (Handle
== INVALID_HANDLE_VALUE
)
412 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
416 if (!ReadFile(Handle
, SectorBuf
, sizeof(SectorBuf
), &dwSize
, NULL
))
418 Log("ReadFile failed, dwSize:%u error:%u", dwSize
, GetLastError());
423 memcpy(UUID
, SectorBuf
+ 0x180, 16);
428 int VentoyMountISOByAPI(const char *IsoPath
)
432 WCHAR wFilePath
[512] = { 0 };
433 VIRTUAL_STORAGE_TYPE StorageType
;
434 OPEN_VIRTUAL_DISK_PARAMETERS OpenParameters
;
435 ATTACH_VIRTUAL_DISK_PARAMETERS AttachParameters
;
437 Log("VentoyMountISOByAPI <%s>", IsoPath
);
439 if (IsUTF8Encode(IsoPath
))
441 MultiByteToWideChar(CP_UTF8
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
445 MultiByteToWideChar(CP_ACP
, 0, IsoPath
, (int)strlen(IsoPath
), wFilePath
, (int)(sizeof(wFilePath
) / sizeof(WCHAR
)));
448 memset(&StorageType
, 0, sizeof(StorageType
));
449 memset(&OpenParameters
, 0, sizeof(OpenParameters
));
450 memset(&AttachParameters
, 0, sizeof(AttachParameters
));
452 OpenParameters
.Version
= OPEN_VIRTUAL_DISK_VERSION_1
;
453 AttachParameters
.Version
= ATTACH_VIRTUAL_DISK_VERSION_1
;
455 Status
= OpenVirtualDisk(&StorageType
, wFilePath
, VIRTUAL_DISK_ACCESS_READ
, 0, &OpenParameters
, &Handle
);
456 if (Status
!= ERROR_SUCCESS
)
458 if (ERROR_VIRTDISK_PROVIDER_NOT_FOUND
== Status
)
460 Log("VirtualDisk for ISO file is not supported in current system");
464 Log("Failed to open virtual disk ErrorCode:%u", Status
);
469 Log("OpenVirtualDisk success");
471 Status
= AttachVirtualDisk(Handle
, NULL
, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY
| ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME
, 0, &AttachParameters
, NULL
);
472 if (Status
!= ERROR_SUCCESS
)
474 Log("Failed to attach virtual disk ErrorCode:%u", Status
);
484 static HANDLE g_FatPhyDrive
;
485 static UINT64 g_Part2StartSec
;
487 static int CopyFileFromFatDisk(const CHAR
* SrcFile
, const CHAR
*DstFile
)
494 Log("CopyFileFromFatDisk (%s)==>(%s)", SrcFile
, DstFile
);
496 flfile
= fl_fopen(SrcFile
, "rb");
499 fl_fseek(flfile
, 0, SEEK_END
);
500 size
= (int)fl_ftell(flfile
);
501 fl_fseek(flfile
, 0, SEEK_SET
);
503 buf
= (char *)malloc(size
);
506 fl_fread(buf
, 1, size
, flfile
);
509 SaveBuffer2File(DstFile
, buf
, size
);
519 static int VentoyFatDiskRead(uint32 Sector
, uint8
*Buffer
, uint32 SectorCount
)
524 LARGE_INTEGER liCurrentPosition
;
526 liCurrentPosition
.QuadPart
= Sector
+ g_Part2StartSec
;
527 liCurrentPosition
.QuadPart
*= 512;
528 SetFilePointerEx(g_FatPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
530 ReadSize
= (DWORD
)(SectorCount
* 512);
532 bRet
= ReadFile(g_FatPhyDrive
, Buffer
, ReadSize
, &dwSize
, NULL
);
533 if (bRet
== FALSE
|| dwSize
!= ReadSize
)
535 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet
, ReadSize
, dwSize
, GetLastError());
541 static CHAR
GetMountLogicalDrive(void)
545 DWORD Mask
= 0x1000000;
547 Drives
= GetLogicalDrives();
548 Log("Drives=0x%x", Drives
);
552 if ((Drives
& Mask
) == 0)
564 UINT64
GetVentoyEfiPartStartSector(HANDLE hDrive
)
569 VTOY_GPT_INFO
*pGpt
= NULL
;
570 UINT64 StartSector
= 0;
572 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
574 bRet
= ReadFile(hDrive
, &MBR
, sizeof(MBR
), &dwSize
, NULL
);
575 Log("Read MBR Ret:%u Size:%u code:%u", bRet
, dwSize
, LASTERR
);
577 if ((!bRet
) || (dwSize
!= sizeof(MBR
)))
582 if (MBR
.PartTbl
[0].FsFlag
== 0xEE)
584 Log("GPT partition style");
586 pGpt
= malloc(sizeof(VTOY_GPT_INFO
));
592 SetFilePointer(hDrive
, 0, NULL
, FILE_BEGIN
);
593 bRet
= ReadFile(hDrive
, pGpt
, sizeof(VTOY_GPT_INFO
), &dwSize
, NULL
);
594 if ((!bRet
) || (dwSize
!= sizeof(VTOY_GPT_INFO
)))
596 Log("Failed to read gpt info %d %u %d", bRet
, dwSize
, LASTERR
);
600 StartSector
= pGpt
->PartTbl
[1].StartLBA
;
605 Log("MBR partition style");
606 StartSector
= MBR
.PartTbl
[1].StartSectorId
;
609 Log("GetVentoyEfiPart StartSector: %llu", StartSector
);
613 int VentoyMountISOByImdisk(const char *IsoPath
, DWORD PhyDrive
)
620 CHAR PhyPath
[MAX_PATH
];
621 WCHAR PhyPathW
[MAX_PATH
];
623 PROCESS_INFORMATION Pi
;
624 GET_LENGTH_INFORMATION LengthInfo
;
626 Log("VentoyMountISOByImdisk %s", IsoPath
);
628 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
629 if (IsUTF8Encode(PhyPath
))
631 Utf8ToUtf16(PhyPath
, PhyPathW
);
632 hDrive
= CreateFileW(PhyPathW
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
636 hDrive
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
639 if (hDrive
== INVALID_HANDLE_VALUE
)
641 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
645 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
648 Log("Could not get phy disk %s size, error:%u", PhyPath
, GetLastError());
652 g_FatPhyDrive
= hDrive
;
653 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
655 Log("Parse FAT fs...");
659 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
663 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.sys", "ventoy\\imdisk.sys");
664 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.exe", "ventoy\\imdisk.exe");
665 CopyFileFromFatDisk("/ventoy/imdisk/64/imdisk.cpl", "ventoy\\imdisk.cpl");
669 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.sys", "ventoy\\imdisk.sys");
670 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");
671 CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");
674 GetCurrentDirectoryA(sizeof(PhyPath
), PhyPath
);
675 strcat_s(PhyPath
, sizeof(PhyPath
), "\\ventoy\\imdisk.sys");
677 if (LoadNtDriver(PhyPath
) == 0)
681 Letter
= GetMountLogicalDrive();
682 sprintf_s(PhyPath
, sizeof(PhyPath
), "ventoy\\imdisk.exe -a -o ro -f %s -m %C:", IsoPath
, Letter
);
684 Log("mount iso to %C: use imdisk cmd <%s>", Letter
, PhyPath
);
686 GetStartupInfoA(&Si
);
688 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
689 Si
.wShowWindow
= SW_HIDE
;
691 CreateProcessA(NULL
, PhyPath
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
692 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
699 SAFE_CLOSE_HANDLE(hDrive
);
704 static int MountIsoFile(CONST CHAR
*IsoPath
, DWORD PhyDrive
)
706 if (IsWindows8OrGreater())
708 Log("This is Windows 8 or latter...");
709 if (VentoyMountISOByAPI(IsoPath
) == 0)
711 Log("Mount iso by API success");
716 Log("Mount iso by API failed, maybe not supported, try imdisk");
717 return VentoyMountISOByImdisk(IsoPath
, PhyDrive
);
722 Log("This is before Windows 8 ...");
723 if (VentoyMountISOByImdisk(IsoPath
, PhyDrive
) == 0)
725 Log("Mount iso by imdisk success");
730 return VentoyMountISOByAPI(IsoPath
);
735 static int GetPhyDriveByLogicalDrive(int DriveLetter
)
740 VOLUME_DISK_EXTENTS DiskExtents
;
743 sprintf_s(PhyPath
, sizeof(PhyPath
), "\\\\.\\%C:", (CHAR
)DriveLetter
);
745 Handle
= CreateFileA(PhyPath
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
746 if (Handle
== INVALID_HANDLE_VALUE
)
748 Log("Could not open the disk<%s>, error:%u", PhyPath
, GetLastError());
752 Ret
= DeviceIoControl(Handle
,
753 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
,
757 (DWORD
)(sizeof(DiskExtents
)),
761 if (!Ret
|| DiskExtents
.NumberOfDiskExtents
== 0)
763 Log("DeviceIoControl IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed %s, error:%u", PhyPath
, GetLastError());
764 SAFE_CLOSE_HANDLE(Handle
);
767 SAFE_CLOSE_HANDLE(Handle
);
769 Log("LogicalDrive:%s PhyDrive:%d Offset:%llu ExtentLength:%llu",
771 DiskExtents
.Extents
[0].DiskNumber
,
772 DiskExtents
.Extents
[0].StartingOffset
.QuadPart
,
773 DiskExtents
.Extents
[0].ExtentLength
.QuadPart
776 return (int)DiskExtents
.Extents
[0].DiskNumber
;
780 static int DeleteVentoyPart2MountPoint(DWORD PhyDrive
)
785 CHAR DriveName
[] = "?:\\";
787 Log("DeleteVentoyPart2MountPoint Phy%u ...", PhyDrive
);
789 Drives
= GetLogicalDrives();
792 if ((Drives
& 0x01) && IsPathExist(FALSE
, "%C:\\ventoy\\ventoy.cpio", Letter
))
794 Log("File %C:\\ventoy\\ventoy.cpio exist", Letter
);
796 PhyDisk
= GetPhyDriveByLogicalDrive(Letter
);
797 Log("PhyDisk=%u for %C", PhyDisk
, Letter
);
799 if (PhyDisk
== PhyDrive
)
801 DriveName
[0] = Letter
;
802 DeleteVolumeMountPointA(DriveName
);
814 static BOOL
check_tar_archive(const char *archive
, CHAR
*tarName
)
818 const char *pos
= archive
;
819 const char *slash
= archive
;
823 if (*pos
== '\\' || *pos
== '/')
830 len
= (int)strlen(slash
);
832 if (len
> 7 && (strncmp(slash
+ len
- 7, ".tar.gz", 7) == 0 || strncmp(slash
+ len
- 7, ".tar.xz", 7) == 0))
834 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
835 tarName
[nameLen
- 3] = 0;
838 else if (len
> 8 && strncmp(slash
+ len
- 8, ".tar.bz2", 8) == 0)
840 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
841 tarName
[nameLen
- 4] = 0;
844 else if (len
> 9 && strncmp(slash
+ len
- 9, ".tar.lzma", 9) == 0)
846 nameLen
= (int)sprintf_s(tarName
, MAX_PATH
, "X:%s", slash
);
847 tarName
[nameLen
- 5] = 0;
854 static int DecompressInjectionArchive(const char *archive
, DWORD PhyDrive
)
861 DWORD flags
= CREATE_NO_WINDOW
;
862 CHAR StrBuf
[MAX_PATH
];
863 CHAR tarName
[MAX_PATH
];
865 PROCESS_INFORMATION Pi
;
866 PROCESS_INFORMATION NewPi
;
867 GET_LENGTH_INFORMATION LengthInfo
;
868 SECURITY_ATTRIBUTES Sa
= { sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
870 Log("DecompressInjectionArchive %s", archive
);
872 sprintf_s(StrBuf
, sizeof(StrBuf
), "\\\\.\\PhysicalDrive%d", PhyDrive
);
873 hDrive
= CreateFileA(StrBuf
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
874 if (hDrive
== INVALID_HANDLE_VALUE
)
876 Log("Could not open the disk<%s>, error:%u", StrBuf
, GetLastError());
880 bRet
= DeviceIoControl(hDrive
, IOCTL_DISK_GET_LENGTH_INFO
, NULL
, 0, &LengthInfo
, sizeof(LengthInfo
), &dwBytes
, NULL
);
883 Log("Could not get phy disk %s size, error:%u", StrBuf
, GetLastError());
887 g_FatPhyDrive
= hDrive
;
888 g_Part2StartSec
= GetVentoyEfiPartStartSector(hDrive
);
890 Log("Parse FAT fs...");
894 if (0 == fl_attach_media(VentoyFatDiskRead
, NULL
))
898 CopyFileFromFatDisk("/ventoy/7z/64/7za.exe", "ventoy\\7za.exe");
902 CopyFileFromFatDisk("/ventoy/7z/32/7za.exe", "ventoy\\7za.exe");
905 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", archive
);
907 Log("extract inject to X:");
908 Log("cmdline:<%s>", StrBuf
);
910 GetStartupInfoA(&Si
);
912 hOut
= CreateFileA("ventoy\\7z.log",
914 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
917 FILE_ATTRIBUTE_NORMAL
,
920 Si
.dwFlags
|= STARTF_USESTDHANDLES
;
922 if (hOut
!= INVALID_HANDLE_VALUE
)
925 Si
.hStdOutput
= hOut
;
928 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &Pi
);
929 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
932 // decompress tar archive, for tar.gz/tar.xz/tar.bz2
934 if (check_tar_archive(archive
, tarName
))
936 Log("Decompress tar archive...<%s>", tarName
);
938 sprintf_s(StrBuf
, sizeof(StrBuf
), "ventoy\\7za.exe x -y -aoa -oX:\\ %s", tarName
);
940 CreateProcessA(NULL
, StrBuf
, NULL
, NULL
, TRUE
, flags
, NULL
, NULL
, &Si
, &NewPi
);
941 WaitForSingleObject(NewPi
.hProcess
, INFINITE
);
943 Log("Now delete %s", tarName
);
944 DeleteFileA(tarName
);
947 SAFE_CLOSE_HANDLE(hOut
);
953 SAFE_CLOSE_HANDLE(hDrive
);
958 static int ProcessUnattendedInstallation(const char *script
)
964 CHAR CurDir
[MAX_PATH
];
966 Log("Copy unattended XML ...");
968 GetCurrentDirectory(sizeof(CurDir
), CurDir
);
970 if ((Letter
>= 'A' && Letter
<= 'Z') || (Letter
>= 'a' && Letter
<= 'z'))
972 Log("Current Drive Letter: %C", Letter
);
979 sprintf_s(CurDir
, sizeof(CurDir
), "%C:\\Autounattend.xml", Letter
);
980 Log("Copy file <%s> --> <%s>", script
, CurDir
);
981 CopyFile(script
, CurDir
, FALSE
);
983 Ret
= RegCreateKeyEx(HKEY_LOCAL_MACHINE
, "System\\Setup", 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hKey
, &dw
);
984 if (ERROR_SUCCESS
== Ret
)
986 Ret
= RegSetValueEx(hKey
, "UnattendFile", 0, REG_SZ
, CurDir
, (DWORD
)(strlen(CurDir
) + 1));
992 static int VentoyHook(ventoy_os_param
*param
)
996 DISK_EXTENT DiskExtent
;
997 DWORD Drives
= GetLogicalDrives();
999 CHAR IsoPath
[MAX_PATH
];
1001 Log("Logical Drives=0x%x Path:<%s>", Drives
, param
->vtoy_img_path
);
1003 if (IsUTF8Encode(param
->vtoy_img_path
))
1005 Log("This file is UTF8 encoding\n");
1012 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:\\%s", Letter
, param
->vtoy_img_path
);
1013 if (IsPathExist(FALSE
, "%s", IsoPath
))
1015 Log("File exist under %C:", Letter
);
1016 if (GetPhyDiskUUID(Letter
, UUID
, &DiskExtent
) == 0)
1018 if (memcmp(UUID
, param
->vtoy_disk_guid
, 16) == 0)
1020 Log("Disk UUID match");
1027 Log("File NOT exist under %C:", Letter
);
1037 Log("Failed to find ISO file");
1041 Log("Find ISO file <%s>", IsoPath
);
1043 rc
= MountIsoFile(IsoPath
, DiskExtent
.DiskNumber
);
1044 Log("Mount ISO FILE: %s", rc
== 0 ? "SUCCESS" : "FAILED");
1047 rc
= DeleteVentoyPart2MountPoint(DiskExtent
.DiskNumber
);
1048 Log("Delete ventoy mountpoint: %s", rc
== 0 ? "SUCCESS" : "NO NEED");
1050 if (g_windows_data
.auto_install_script
[0])
1052 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", Letter
, g_windows_data
.auto_install_script
);
1053 if (IsPathExist(FALSE
, "%s", IsoPath
))
1055 Log("use auto install script %s...", IsoPath
);
1056 ProcessUnattendedInstallation(IsoPath
);
1060 Log("auto install script %s not exist", IsoPath
);
1065 Log("auto install no need");
1068 if (g_windows_data
.injection_archive
[0])
1070 sprintf_s(IsoPath
, sizeof(IsoPath
), "%C:%s", Letter
, g_windows_data
.injection_archive
);
1071 if (IsPathExist(FALSE
, "%s", IsoPath
))
1073 Log("decompress injection archive %s...", IsoPath
);
1074 DecompressInjectionArchive(IsoPath
, DiskExtent
.DiskNumber
);
1078 Log("injection archive %s not exist", IsoPath
);
1083 Log("no injection archive found");
1089 const char * GetFileNameInPath(const char *fullpath
)
1092 const char *pos
= NULL
;
1094 if (strstr(fullpath
, ":"))
1096 for (i
= (int)strlen(fullpath
); i
> 0; i
--)
1098 if (fullpath
[i
- 1] == '/' || fullpath
[i
- 1] == '\\')
1100 return fullpath
+ i
;
1108 int VentoyJump(INT argc
, CHAR
**argv
, CHAR
*LunchFile
)
1114 BYTE
*Buffer
= NULL
;
1115 CHAR ExeFileName
[MAX_PATH
];
1117 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s", argv
[0]);
1118 if (!IsPathExist(FALSE
, "%s", ExeFileName
))
1120 Log("File %s NOT exist, now try %s.exe", ExeFileName
, ExeFileName
);
1121 sprintf_s(ExeFileName
, sizeof(ExeFileName
), "%s.exe", argv
[0]);
1123 Log("File %s exist ? %s", ExeFileName
, IsPathExist(FALSE
, "%s", ExeFileName
) ? "YES" : "NO");
1126 if (ReadWholeFile2Buf(ExeFileName
, (void **)&Buffer
, &FileSize
))
1131 g_64bit_system
= IsPe64(Buffer
);
1133 if (!IsPathExist(TRUE
, "ventoy"))
1135 if (!CreateDirectoryA("ventoy", NULL
))
1137 Log("Failed to create ventoy directory err:%u", GetLastError());
1142 for (PeStart
= 0; PeStart
< FileSize
; PeStart
+= 16)
1144 if (CheckOsParam((ventoy_os_param
*)(Buffer
+ PeStart
)) &&
1145 CheckPeHead(Buffer
+ PeStart
+ sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
)))
1147 Log("Find os pararm at %u", PeStart
);
1149 memcpy(&g_os_param
, Buffer
+ PeStart
, sizeof(ventoy_os_param
));
1150 memcpy(&g_windows_data
, Buffer
+ PeStart
+ sizeof(ventoy_os_param
), sizeof(ventoy_windows_data
));
1151 memcpy(g_os_param_reserved
, g_os_param
.vtoy_reserved
, sizeof(g_os_param_reserved
));
1153 if (g_os_param_reserved
[0] == 1)
1155 Log("break here for debug .....");
1160 for (Pos
= 0; Pos
< sizeof(g_os_param
.vtoy_img_path
) && g_os_param
.vtoy_img_path
[Pos
]; Pos
++)
1162 if (g_os_param
.vtoy_img_path
[Pos
] == '/')
1164 g_os_param
.vtoy_img_path
[Pos
] = '\\';
1168 PeStart
+= sizeof(ventoy_os_param
) + sizeof(ventoy_windows_data
);
1169 sprintf_s(LunchFile
, MAX_PATH
, "ventoy\\%s", GetFileNameInPath(ExeFileName
));
1170 SaveBuffer2File(LunchFile
, Buffer
+ PeStart
, FileSize
- PeStart
);
1175 if (PeStart
>= FileSize
)
1177 Log("OS param not found");
1181 if (g_os_param_reserved
[0] == 2)
1183 Log("skip hook for debug .....");
1188 rc
= VentoyHook(&g_os_param
);
1200 int main(int argc
, char **argv
)
1205 CHAR CurDir
[MAX_PATH
];
1206 CHAR LunchFile
[MAX_PATH
];
1208 PROCESS_INFORMATION Pi
;
1210 if (argv
[0] && argv
[0][0] && argv
[0][1] == ':')
1212 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
1214 strcpy_s(LunchFile
, sizeof(LunchFile
), argv
[0]);
1215 Pos
= (char *)GetFileNameInPath(LunchFile
);
1217 strcat_s(CurDir
, sizeof(CurDir
), "\\");
1218 strcat_s(CurDir
, sizeof(CurDir
), Pos
);
1220 if (_stricmp(argv
[0], CurDir
) != 0)
1223 SetCurrentDirectoryA(LunchFile
);
1227 Log("######## VentoyJump ##########");
1228 Log("argc = %d argv[0] = <%s>", argc
, argv
[0]);
1230 if (Pos
&& *Pos
== 0)
1232 Log("Old current directory = <%s>", CurDir
);
1233 Log("New current directory = <%s>", LunchFile
);
1237 GetCurrentDirectoryA(sizeof(CurDir
), CurDir
);
1238 Log("Current directory = <%s>", CurDir
);
1241 GetStartupInfoA(&Si
);
1243 memset(LunchFile
, 0, sizeof(LunchFile
));
1244 rc
= VentoyJump(argc
, argv
, LunchFile
);
1246 if (g_os_param_reserved
[0] == 3)
1248 Log("Open log for debug ...");
1249 sprintf_s(LunchFile
, sizeof(LunchFile
), "%s", "notepad.exe ventoy.log");
1253 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
1254 Si
.wShowWindow
= SW_HIDE
;
1255 Log("Ventoy jump %s ...", rc
== 0 ? "success" : "failed");
1258 CreateProcessA(NULL
, LunchFile
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
1262 Log("Ventoy hook failed, now wait and retry ...");
1265 rc
= VentoyHook(&g_os_param
);
1268 WaitForSingleObject(Pi
.hProcess
, INFINITE
);