1.0.77 release

This commit is contained in:
longpanda
2022-06-19 13:56:49 +08:00
parent 8db8e07b61
commit b976923f00
23 changed files with 1456 additions and 209 deletions

View File

@@ -36,6 +36,8 @@ static ventoy_guid g_ventoy_guid = VENTOY_GUID;
static HANDLE g_vtoylog_mutex = NULL;
static HANDLE g_vtoyins_mutex = NULL;
static DWORD g_vtoy_disk_drive;
static CHAR g_prog_full_path[MAX_PATH];
static CHAR g_prog_dir[MAX_PATH];
static CHAR g_prog_name[MAX_PATH];
@@ -760,7 +762,7 @@ static int VentoyFatDiskRead(uint32 Sector, uint8 *Buffer, uint32 SectorCount)
bRet = ReadFile(g_FatPhyDrive, Buffer, ReadSize, &dwSize, NULL);
if (bRet == FALSE || dwSize != ReadSize)
{
Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u\n", bRet, ReadSize, dwSize, GetLastError());
Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet, ReadSize, dwSize, GetLastError());
}
return 1;
@@ -1291,12 +1293,485 @@ End:
return rc;
}
static int UnattendNeedVarExpand(const char *script)
{
FILE *fp = NULL;
char szLine[4096];
fopen_s(&fp, script, "r");
if (!fp)
{
return 0;
}
szLine[0] = szLine[4095] = 0;
while (fgets(szLine, sizeof(szLine) - 1, fp))
{
if (strstr(szLine, "$$VT_"))
{
fclose(fp);
return 1;
}
szLine[0] = szLine[4095] = 0;
}
fclose(fp);
return 0;
}
static int ExpandSingleVar(VarDiskInfo *pDiskInfo, int DiskNum, const char *var, char *value, int len)
{
int i;
int index = -1;
UINT64 uiDst = 0;
UINT64 uiDelta = 0;
UINT64 uiMaxSize = 0;
UINT64 uiMaxDelta = ULLONG_MAX;
value[0] = 0;
if (strcmp(var, "VT_WINDOWS_DISK_1ST_NONVTOY") == 0)
{
for (i = 0; i < DiskNum; i++)
{
if (pDiskInfo[i].Capacity > 0 && i != g_vtoy_disk_drive)
{
Log("%s=<PhyDrive%d>", var, i);
sprintf_s(value, len, "%d", i);
return 0;
}
}
}
else if (strcmp(var, "VT_WINDOWS_DISK_1ST_NONUSB") == 0)
{
for (i = 0; i < DiskNum; i++)
{
if (pDiskInfo[i].Capacity > 0 && pDiskInfo[i].BusType != BusTypeUsb)
{
Log("%s=<PhyDrive%d>", var, i);
sprintf_s(value, len, "%d", i);
return 0;
}
}
}
else if (strcmp(var, "VT_WINDOWS_DISK_MAX_SIZE") == 0)
{
for (i = 0; i < DiskNum; i++)
{
if (pDiskInfo[i].Capacity > 0 && pDiskInfo[i].Capacity > uiMaxSize)
{
index = i;
uiMaxSize = pDiskInfo[i].Capacity;
}
}
Log("%s=<PhyDrive%d>", var, index);
sprintf_s(value, len, "%d", index);
}
else if (strncmp(var, "VT_WINDOWS_DISK_CLOSEST_", 24) == 0)
{
uiDst = strtoul(var + 24, NULL, 10);
uiDst = uiDst * (1024ULL * 1024ULL * 1024ULL);
for (i = 0; i < DiskNum; i++)
{
if (pDiskInfo[i].Capacity == 0)
{
continue;
}
if (pDiskInfo[i].Capacity > uiDst)
{
uiDelta = pDiskInfo[i].Capacity - uiDst;
}
else
{
uiDelta = uiDst - pDiskInfo[i].Capacity;
}
if (uiDelta < uiMaxDelta)
{
uiMaxDelta = uiDelta;
index = i;
}
}
Log("%s=<PhyDrive%d>", var, index);
sprintf_s(value, len, "%d", index);
}
else
{
Log("Invalid var name <%s>", var);
sprintf_s(value, len, "$$%s$$", var);
}
if (value[0] == 0)
{
sprintf_s(value, len, "$$%s$$", var);
}
return 0;
}
static void TrimString(CHAR *String)
{
CHAR *Pos1 = String;
CHAR *Pos2 = String;
size_t Len = strlen(String);
while (Len > 0)
{
if (String[Len - 1] != ' ' && String[Len - 1] != '\t')
{
break;
}
String[Len - 1] = 0;
Len--;
}
while (*Pos1 == ' ' || *Pos1 == '\t')
{
Pos1++;
}
while (*Pos1)
{
*Pos2++ = *Pos1++;
}
*Pos2++ = 0;
return;
}
static int GetRegDwordValue(HKEY Key, LPCSTR SubKey, LPCSTR ValueName, DWORD *pValue)
{
HKEY hKey;
DWORD Type;
DWORD Size;
LSTATUS lRet;
DWORD Value;
lRet = RegOpenKeyExA(Key, SubKey, 0, KEY_QUERY_VALUE, &hKey);
Log("RegOpenKeyExA <%s> Ret:%ld", SubKey, lRet);
if (ERROR_SUCCESS == lRet)
{
Size = sizeof(Value);
lRet = RegQueryValueExA(hKey, ValueName, NULL, &Type, (LPBYTE)&Value, &Size);
Log("RegQueryValueExA <%s> ret:%u Size:%u Value:%u", ValueName, lRet, Size, Value);
*pValue = Value;
RegCloseKey(hKey);
return 0;
}
else
{
return 1;
}
}
static const CHAR * GetBusTypeString(int Type)
{
switch (Type)
{
case BusTypeUnknown: return "unknown";
case BusTypeScsi: return "SCSI";
case BusTypeAtapi: return "Atapi";
case BusTypeAta: return "ATA";
case BusType1394: return "1394";
case BusTypeSsa: return "SSA";
case BusTypeFibre: return "Fibre";
case BusTypeUsb: return "USB";
case BusTypeRAID: return "RAID";
case BusTypeiScsi: return "iSCSI";
case BusTypeSas: return "SAS";
case BusTypeSata: return "SATA";
case BusTypeSd: return "SD";
case BusTypeMmc: return "MMC";
case BusTypeVirtual: return "Virtual";
case BusTypeFileBackedVirtual: return "FileBackedVirtual";
case BusTypeSpaces: return "Spaces";
case BusTypeNvme: return "Nvme";
}
return "unknown";
}
static int GetHumanReadableGBSize(UINT64 SizeBytes)
{
int i;
int Pow2 = 1;
double Delta;
double GB = SizeBytes * 1.0 / 1000 / 1000 / 1000;
if ((SizeBytes % 1073741824) == 0)
{
return (int)(SizeBytes / 1073741824);
}
for (i = 0; i < 12; i++)
{
if (Pow2 > GB)
{
Delta = (Pow2 - GB) / Pow2;
}
else
{
Delta = (GB - Pow2) / Pow2;
}
if (Delta < 0.05)
{
return Pow2;
}
Pow2 <<= 1;
}
return (int)GB;
}
static int EnumerateAllDisk(VarDiskInfo **ppDiskInfo, int *pDiskNum)
{
int i;
DWORD Value;
int DiskNum = 0;
BOOL bRet;
DWORD dwBytes;
VarDiskInfo *pDiskInfo = NULL;
HANDLE Handle = INVALID_HANDLE_VALUE;
CHAR PhyDrive[128];
GET_LENGTH_INFORMATION LengthInfo;
STORAGE_PROPERTY_QUERY Query;
STORAGE_DESCRIPTOR_HEADER DevDescHeader;
STORAGE_DEVICE_DESCRIPTOR *pDevDesc;
if (GetRegDwordValue(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\disk\\Enum", "Count", &Value) == 0)
{
DiskNum = (int)Value;
}
else
{
Log("Failed to read disk count");
return 1;
}
Log("Current phy disk count:%d", DiskNum);
if (DiskNum <= 0)
{
return 1;
}
pDiskInfo = malloc(DiskNum * sizeof(VarDiskInfo));
if (!pDiskInfo)
{
Log("Failed to alloc");
return 1;
}
memset(pDiskInfo, 0, DiskNum * sizeof(VarDiskInfo));
for (i = 0; i < DiskNum; i++)
{
SAFE_CLOSE_HANDLE(Handle);
safe_sprintf(PhyDrive, "\\\\.\\PhysicalDrive%d", i);
Handle = CreateFileA(PhyDrive, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
Log("Create file Handle:%p %s status:%u", Handle, PhyDrive, LASTERR);
if (Handle == INVALID_HANDLE_VALUE)
{
continue;
}
bRet = DeviceIoControl(Handle,
IOCTL_DISK_GET_LENGTH_INFO, NULL,
0,
&LengthInfo,
sizeof(LengthInfo),
&dwBytes,
NULL);
if (!bRet)
{
Log("DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO failed error:%u", LASTERR);
continue;
}
Log("PHYSICALDRIVE%d size %llu bytes", i, (ULONGLONG)LengthInfo.Length.QuadPart);
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
bRet = DeviceIoControl(Handle,
IOCTL_STORAGE_QUERY_PROPERTY,
&Query,
sizeof(Query),
&DevDescHeader,
sizeof(STORAGE_DESCRIPTOR_HEADER),
&dwBytes,
NULL);
if (!bRet)
{
Log("DeviceIoControl1 error:%u dwBytes:%u", LASTERR, dwBytes);
continue;
}
if (DevDescHeader.Size < sizeof(STORAGE_DEVICE_DESCRIPTOR))
{
Log("Invalid DevDescHeader.Size:%u", DevDescHeader.Size);
continue;
}
pDevDesc = (STORAGE_DEVICE_DESCRIPTOR *)malloc(DevDescHeader.Size);
if (!pDevDesc)
{
Log("failed to malloc error:%u len:%u", LASTERR, DevDescHeader.Size);
continue;
}
bRet = DeviceIoControl(Handle,
IOCTL_STORAGE_QUERY_PROPERTY,
&Query,
sizeof(Query),
pDevDesc,
DevDescHeader.Size,
&dwBytes,
NULL);
if (!bRet)
{
Log("DeviceIoControl2 error:%u dwBytes:%u", LASTERR, dwBytes);
free(pDevDesc);
continue;
}
pDiskInfo[i].RemovableMedia = pDevDesc->RemovableMedia;
pDiskInfo[i].BusType = pDevDesc->BusType;
pDiskInfo[i].DeviceType = pDevDesc->DeviceType;
pDiskInfo[i].Capacity = LengthInfo.Length.QuadPart;
if (pDevDesc->VendorIdOffset)
{
safe_strcpy(pDiskInfo[i].VendorId, (char *)pDevDesc + pDevDesc->VendorIdOffset);
TrimString(pDiskInfo[i].VendorId);
}
if (pDevDesc->ProductIdOffset)
{
safe_strcpy(pDiskInfo[i].ProductId, (char *)pDevDesc + pDevDesc->ProductIdOffset);
TrimString(pDiskInfo[i].ProductId);
}
if (pDevDesc->ProductRevisionOffset)
{
safe_strcpy(pDiskInfo[i].ProductRev, (char *)pDevDesc + pDevDesc->ProductRevisionOffset);
TrimString(pDiskInfo[i].ProductRev);
}
if (pDevDesc->SerialNumberOffset)
{
safe_strcpy(pDiskInfo[i].SerialNumber, (char *)pDevDesc + pDevDesc->SerialNumberOffset);
TrimString(pDiskInfo[i].SerialNumber);
}
free(pDevDesc);
SAFE_CLOSE_HANDLE(Handle);
}
Log("########## DUMP DISK BEGIN ##########");
for (i = 0; i < DiskNum; i++)
{
Log("PhyDrv:%d BusType:%-4s Removable:%u Size:%dGB(%llu) Name:%s %s",
i, GetBusTypeString(pDiskInfo[i].BusType), pDiskInfo[i].RemovableMedia,
GetHumanReadableGBSize(pDiskInfo[i].Capacity), pDiskInfo[i].Capacity,
pDiskInfo[i].VendorId, pDiskInfo[i].ProductId);
}
Log("Ventoy disk is PhyDvr%d", g_vtoy_disk_drive);
Log("########## DUMP DISK END ##########");
*ppDiskInfo = pDiskInfo;
*pDiskNum = DiskNum;
return 0;
}
static int UnattendVarExpand(const char *script, const char *tmpfile)
{
FILE *fp = NULL;
FILE *fout = NULL;
char *start = NULL;
char *end = NULL;
char szLine[4096];
char szValue[256];
int DiskNum = 0;
VarDiskInfo *pDiskInfo = NULL;
Log("UnattendVarExpand ...");
if (EnumerateAllDisk(&pDiskInfo, &DiskNum))
{
Log("Failed to EnumerateAllDisk");
return 1;
}
fopen_s(&fp, script, "r");
if (!fp)
{
free(pDiskInfo);
return 0;
}
fopen_s(&fout, tmpfile, "w+");
if (!fout)
{
fclose(fp);
free(pDiskInfo);
return 0;
}
szLine[0] = szLine[4095] = 0;
while (fgets(szLine, sizeof(szLine) - 1, fp))
{
start = strstr(szLine, "$$VT_");
if (start)
{
end = strstr(start + 5, "$$");
}
if (start && end)
{
*start = 0;
fprintf(fout, "%s", szLine);
*end = 0;
ExpandSingleVar(pDiskInfo, DiskNum, start + 2, szValue, sizeof(szValue) - 1);
fprintf(fout, "%s", szValue);
fprintf(fout, "%s", end + 2);
}
else
{
fprintf(fout, "%s", szLine);
}
szLine[0] = szLine[4095] = 0;
}
fclose(fp);
fclose(fout);
free(pDiskInfo);
return 0;
}
//#define VAR_DEBUG 1
static int ProcessUnattendedInstallation(const char *script)
{
DWORD dw;
HKEY hKey;
LSTATUS Ret;
CHAR Letter;
CHAR TmpFile[MAX_PATH];
CHAR CurDir[MAX_PATH];
Log("Copy unattended XML ...");
@@ -1311,16 +1786,34 @@ static int ProcessUnattendedInstallation(const char *script)
{
Letter = 'X';
}
sprintf_s(CurDir, sizeof(CurDir), "%C:\\Autounattend.xml", Letter);
Log("Copy file <%s> --> <%s>", script, CurDir);
CopyFile(script, CurDir, FALSE);
#ifdef VAR_DEBUG
sprintf_s(CurDir, sizeof(CurDir), "%C:\\AutounattendXXX.xml", Letter);
#else
sprintf_s(CurDir, sizeof(CurDir), "%C:\\Autounattend.xml", Letter);
#endif
if (UnattendNeedVarExpand(script))
{
sprintf_s(TmpFile, sizeof(TmpFile), "%C:\\__Autounattend", Letter);
UnattendVarExpand(script, TmpFile);
Log("Expand Copy file <%s> --> <%s>", script, CurDir);
CopyFile(TmpFile, CurDir, FALSE);
}
else
{
Log("No var expand copy file <%s> --> <%s>", script, CurDir);
CopyFile(script, CurDir, FALSE);
}
#ifndef VAR_DEBUG
Ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "System\\Setup", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw);
if (ERROR_SUCCESS == Ret)
{
Ret = RegSetValueEx(hKey, "UnattendFile", 0, REG_SZ, CurDir, (DWORD)(strlen(CurDir) + 1));
}
#endif
return 0;
}
@@ -1493,7 +1986,7 @@ static int VentoyHook(ventoy_os_param *param)
if (IsUTF8Encode(param->vtoy_img_path))
{
Log("This file is UTF8 encoding\n");
Log("This file is UTF8 encoding");
}
for (i = 0; i < 5; i++)
@@ -1622,6 +2115,8 @@ static int VentoyHook(ventoy_os_param *param)
return 1;
}
g_vtoy_disk_drive = VtoyDiskNum;
Drives = GetLogicalDrives();
Log("Drives before mount: 0x%x", Drives);

View File

@@ -1,29 +1,29 @@
/******************************************************************************
* vtoyjump.h
*
* Copyright (c) 2020, longpanda <admin@ventoy.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __VTOYJUMP_H__
#define __VTOYJUMP_H__
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#define SIZE_1MB (1024 * 1024)
#define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
/******************************************************************************
* vtoyjump.h
*
* Copyright (c) 2020, longpanda <admin@ventoy.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __VTOYJUMP_H__
#define __VTOYJUMP_H__
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#define SIZE_1MB (1024 * 1024)
#define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
#define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}
@@ -67,106 +67,124 @@ typedef struct ventoy_os_param
UINT8 reserved[27];
}ventoy_os_param;
typedef struct ventoy_windows_data
{
char auto_install_script[384];
char injection_archive[384];
UINT8 windows11_bypass_check;
UINT32 auto_install_len;
UINT8 reserved[255 - 4];
/* auto install script file data ... + auto_install_len */
/* ...... */
typedef struct ventoy_windows_data
{
char auto_install_script[384];
char injection_archive[384];
UINT8 windows11_bypass_check;
UINT32 auto_install_len;
UINT8 reserved[255 - 4];
/* auto install script file data ... + auto_install_len */
/* ...... */
}ventoy_windows_data;
typedef struct PART_TABLE
{
UINT8 Active; // 0x00 0x80
UINT8 StartHead;
UINT16 StartSector : 6;
UINT16 StartCylinder : 10;
UINT8 FsFlag;
UINT8 EndHead;
UINT16 EndSector : 6;
UINT16 EndCylinder : 10;
UINT32 StartSectorId;
UINT32 SectorCount;
}PART_TABLE;
typedef struct MBR_HEAD
{
UINT8 BootCode[446];
PART_TABLE PartTbl[4];
UINT8 Byte55;
UINT8 ByteAA;
}MBR_HEAD;
typedef struct VTOY_GPT_HDR
{
CHAR Signature[8]; /* EFI PART */
UINT8 Version[4];
UINT32 Length;
UINT32 Crc;
UINT8 Reserved1[4];
UINT64 EfiStartLBA;
UINT64 EfiBackupLBA;
UINT64 PartAreaStartLBA;
UINT64 PartAreaEndLBA;
GUID DiskGuid;
UINT64 PartTblStartLBA;
UINT32 PartTblTotNum;
UINT32 PartTblEntryLen;
UINT32 PartTblCrc;
UINT8 Reserved2[420];
}VTOY_GPT_HDR;
typedef struct VTOY_GPT_PART_TBL
{
GUID PartType;
GUID PartGuid;
UINT64 StartLBA;
UINT64 LastLBA;
UINT64 Attr;
UINT16 Name[36];
}VTOY_GPT_PART_TBL;
typedef struct VTOY_GPT_INFO
{
MBR_HEAD MBR;
VTOY_GPT_HDR Head;
VTOY_GPT_PART_TBL PartTbl[128];
}VTOY_GPT_INFO;
typedef struct PART_TABLE
{
UINT8 Active; // 0x00 0x80
UINT8 StartHead;
UINT16 StartSector : 6;
UINT16 StartCylinder : 10;
UINT8 FsFlag;
UINT8 EndHead;
UINT16 EndSector : 6;
UINT16 EndCylinder : 10;
UINT32 StartSectorId;
UINT32 SectorCount;
}PART_TABLE;
typedef struct MBR_HEAD
{
UINT8 BootCode[446];
PART_TABLE PartTbl[4];
UINT8 Byte55;
UINT8 ByteAA;
}MBR_HEAD;
typedef struct VTOY_GPT_HDR
{
CHAR Signature[8]; /* EFI PART */
UINT8 Version[4];
UINT32 Length;
UINT32 Crc;
UINT8 Reserved1[4];
UINT64 EfiStartLBA;
UINT64 EfiBackupLBA;
UINT64 PartAreaStartLBA;
UINT64 PartAreaEndLBA;
GUID DiskGuid;
UINT64 PartTblStartLBA;
UINT32 PartTblTotNum;
UINT32 PartTblEntryLen;
UINT32 PartTblCrc;
UINT8 Reserved2[420];
}VTOY_GPT_HDR;
typedef struct VTOY_GPT_PART_TBL
{
GUID PartType;
GUID PartGuid;
UINT64 StartLBA;
UINT64 LastLBA;
UINT64 Attr;
UINT16 Name[36];
}VTOY_GPT_PART_TBL;
typedef struct VTOY_GPT_INFO
{
MBR_HEAD MBR;
VTOY_GPT_HDR Head;
VTOY_GPT_PART_TBL PartTbl[128];
}VTOY_GPT_INFO;
#pragma pack()
#define SAFE_CLOSE_HANDLE(handle) \
{\
if (handle != INVALID_HANDLE_VALUE) \
{\
CloseHandle(handle); \
(handle) = INVALID_HANDLE_VALUE; \
}\
}
#define LASTERR GetLastError()
int unxz(unsigned char *in, int in_size,
int(*fill)(void *dest, unsigned int size),
int(*flush)(void *src, unsigned int size),
unsigned char *out, int *in_used,
void(*error)(char *x));
#endif
typedef struct VarDiskInfo
{
UINT64 Capacity;
int BusType;
BOOL RemovableMedia;
BYTE DeviceType;
CHAR VendorId[128];
CHAR ProductId[128];
CHAR ProductRev[128];
CHAR SerialNumber[128];
}VarDiskInfo;
#define SAFE_CLOSE_HANDLE(handle) \
{\
if (handle != INVALID_HANDLE_VALUE) \
{\
CloseHandle(handle); \
(handle) = INVALID_HANDLE_VALUE; \
}\
}
#define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
#define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
#define LASTERR GetLastError()
int unxz(unsigned char *in, int in_size,
int(*fill)(void *dest, unsigned int size),
int(*flush)(void *src, unsigned int size),
unsigned char *out, int *in_used,
void(*error)(char *x));
#endif