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/>.
21 #ifndef __VENTOY2DISK_H__
22 #define __VENTOY2DISK_H__
26 #define SIZE_1MB (1024 * 1024)
27 #define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
28 #define VENTOY_PART1_START_SECTOR 2048
30 #define VENTOY_FILE_BOOT_IMG "boot\\boot.img"
31 #define VENTOY_FILE_STG1_IMG "boot\\core.img.xz"
32 #define VENTOY_FILE_DISK_IMG "ventoy\\ventoy.disk.img.xz"
33 #define VENTOY_FILE_LOG "log.txt"
34 #define VENTOY_FILE_VERSION "ventoy\\version"
36 #define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
37 #define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
39 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
40 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
42 #define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
43 #define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
45 #define CHECK_CLOSE_HANDLE(Handle) \
47 if (Handle != INVALID_HANDLE_VALUE) \
49 CloseHandle(Handle); \
50 Handle = INVALID_HANDLE_VALUE; \
54 #define LASTERR GetLastError()
57 typedef struct PART_TABLE
59 UINT8 Active
; // 0x00 0x80
62 UINT16 StartSector
: 6;
63 UINT16 StartCylinder
: 10;
69 UINT16 EndCylinder
: 10;
75 typedef struct MBR_HEAD
78 PART_TABLE PartTbl
[4];
83 typedef struct VTOY_GPT_HDR
85 CHAR Signature
[8]; /* EFI PART */
92 UINT64 PartAreaStartLBA
;
93 UINT64 PartAreaEndLBA
;
95 UINT64 PartTblStartLBA
;
97 UINT32 PartTblEntryLen
;
102 typedef struct VTOY_GPT_PART_TBL
112 typedef struct VTOY_GPT_INFO
116 VTOY_GPT_PART_TBL PartTbl
[128];
120 typedef struct ventoy_secure_data
122 UINT8 magic1
[16]; /* VENTOY_GUID */
125 UINT8 adminSHA256
[32];
126 UINT8 reserved
[4000];
127 UINT8 magic2
[16]; /* VENTOY_GUID */
133 #define VENTOY_MAX_PHY_DRIVE 128
135 typedef struct PHY_DRIVE_INFO
139 int PartStyle
;//0:MBR 1:GPT
145 CHAR ProductRev
[128];
146 CHAR SerialNumber
[128];
147 STORAGE_BUS_TYPE BusType
;
149 CHAR DriveLetters
[64];
151 CHAR VentoyVersion
[32];
153 BOOL SecureBootSupport
;
157 typedef enum PROGRESS_POINT
164 PT_LOCK_VOLUME
= PT_FORMAT_PART1
,
167 PT_WRITE_VENTOY_START
,
168 PT_WRITE_VENTOY_FINISH
= PT_WRITE_VENTOY_START
+ 32,
177 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
179 extern PHY_DRIVE_INFO
*g_PhyDriveList
;
180 extern DWORD g_PhyDriveCount
;
181 extern int g_ForceOperation
;
182 extern HWND g_ProgressBarHwnd
;
183 extern HFONT g_language_normal_font
;
184 extern HFONT g_language_bold_font
;
186 void Log(const char *Fmt
, ...);
187 BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...);
188 void DumpWindowsVersion(void);
189 const CHAR
* GetLocalVentoyVersion(void);
190 const CHAR
* ParseVentoyVersionFromString(CHAR
*Buf
);
191 CHAR
GetFirstUnusedDriveLetter(void);
192 const CHAR
* GetBusTypeString(STORAGE_BUS_TYPE Type
);
193 int VentoyGetLocalBootImg(MBR_HEAD
*pMBR
);
194 int GetHumanReadableGBSize(UINT64 SizeBytes
);
195 void TrimString(CHAR
*String
);
196 int VentoyFillMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
, int PartStyle
);
197 int VentoyFillGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
198 BOOL
IsVentoyLogicalDrive(CHAR DriveLetter
);
199 int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
);
200 int GetPhysicalDriveCount(void);
201 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO
*pDriveList
, DWORD
*pDriveCount
);
202 int GetPhyDriveByLogicalDrive(int DriveLetter
);
203 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO
*pDriveInfo
, UINT64 Part2StartSector
, CHAR
*VerBuf
, size_t BufLen
, BOOL
*pSecureBoot
);
204 int Ventoy2DiskInit(void);
205 int Ventoy2DiskDestroy(void);
206 PHY_DRIVE_INFO
* GetPhyDriveInfoById(int Id
);
207 int ParseCmdLineOption(LPSTR lpCmdLine
);
208 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
);
209 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
);
210 int VentoyFillBackupGptHead(VTOY_GPT_INFO
*pInfo
, VTOY_GPT_HDR
*pHead
);
211 void SetProgressBarPos(int Pos
);
212 int ReadWholeFileToBuf(const CHAR
*FileName
, int ExtLen
, void **Bufer
, int *BufLen
);
213 int INIT
unxz(unsigned char *in
, int in_size
,
214 int(*fill
)(void *dest
, unsigned int size
),
215 int(*flush
)(void *src
, unsigned int size
),
216 unsigned char *out
, int *in_used
,
217 void(*error
)(char *x
));
218 void disk_io_set_param(HANDLE Handle
, UINT64 SectorCount
);
219 INT_PTR CALLBACK
PartDialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
);
220 int GetReservedSpaceInMB(void);
221 int FindProcessOccupyDisk(HANDLE hDrive
, PHY_DRIVE_INFO
*pPhyDrive
);
222 int VentoyFillLocation(UINT64 DiskSizeInBytes
, UINT32 StartSectorId
, UINT32 SectorCount
, PART_TABLE
*Table
);
223 int ClearVentoyFromPhyDrive(HWND hWnd
, PHY_DRIVE_INFO
*pPhyDrive
, char *pDrvLetter
);
224 UINT32
VentoyCrc32(void *Buffer
, UINT32 Length
);
226 #define SET_FILE_POS(pos) \
227 liCurrentPosition.QuadPart = pos; \
228 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\