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_1GB (1024 * 1024 * 1024)
27 #define SIZE_1MB (1024 * 1024)
28 #define SIZE_2MB (2048 * 1024)
29 #define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
30 #define VENTOY_PART1_START_SECTOR 2048
32 #define VENTOY_FILE_BOOT_IMG "boot\\boot.img"
33 #define VENTOY_FILE_STG1_IMG "boot\\core.img.xz"
34 #define VENTOY_FILE_DISK_IMG "ventoy\\ventoy.disk.img.xz"
35 #define VENTOY_FILE_LOG "log.txt"
36 #define VENTOY_FILE_VERSION "ventoy\\version"
38 #define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
39 #define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
41 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
42 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
44 #define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
45 #define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
47 #define CHECK_FREE(p) \
56 #define CHECK_CLOSE_HANDLE(Handle) \
58 if (Handle != INVALID_HANDLE_VALUE) \
60 CloseHandle(Handle); \
61 Handle = INVALID_HANDLE_VALUE; \
65 #define LASTERR GetLastError()
66 #define RET_LASTERR (ret ? 0 : LASTERR)
69 typedef struct PART_TABLE
71 UINT8 Active
; // 0x00 0x80
74 UINT16 StartSector
: 6;
75 UINT16 StartCylinder
: 10;
81 UINT16 EndCylinder
: 10;
87 typedef struct MBR_HEAD
90 PART_TABLE PartTbl
[4];
95 typedef struct VTOY_GPT_HDR
97 CHAR Signature
[8]; /* EFI PART */
104 UINT64 PartAreaStartLBA
;
105 UINT64 PartAreaEndLBA
;
107 UINT64 PartTblStartLBA
;
108 UINT32 PartTblTotNum
;
109 UINT32 PartTblEntryLen
;
111 UINT8 Reserved2
[420];
114 typedef struct VTOY_GPT_PART_TBL
124 typedef struct VTOY_GPT_INFO
128 VTOY_GPT_PART_TBL PartTbl
[128];
132 typedef struct ventoy_secure_data
134 UINT8 magic1
[16]; /* VENTOY_GUID */
137 UINT8 adminSHA256
[32];
138 UINT8 reserved
[4000];
139 UINT8 magic2
[16]; /* VENTOY_GUID */
145 #define VENTOY_MAX_PHY_DRIVE 128
147 typedef struct PHY_DRIVE_INFO
151 int PartStyle
;//0:MBR 1:GPT
157 CHAR ProductRev
[128];
158 CHAR SerialNumber
[128];
159 STORAGE_BUS_TYPE BusType
;
161 CHAR DriveLetters
[64];
163 CHAR VentoyFsType
[16];
164 CHAR VentoyVersion
[32];
166 BOOL SecureBootSupport
;
171 UINT64 ResizeOldPart1Size
;
172 CHAR Part1DriveLetter
;
173 CHAR ResizeVolumeGuid
[64];
175 UINT64 ResizePart2StartSector
;
180 typedef enum PROGRESS_POINT
183 PT_LOCK_FOR_CLEAN
= 8,
187 PT_LOCK_VOLUME
= PT_FORMAT_PART1
,
190 PT_WRITE_VENTOY_START
,
191 PT_WRITE_VENTOY_FINISH
= PT_WRITE_VENTOY_START
+ 32,
198 PT_REFORMAT_FINISH
= PT_REFORMAT_START
+ 2,
203 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
205 extern PHY_DRIVE_INFO
*g_PhyDriveList
;
206 extern DWORD g_PhyDriveCount
;
207 extern int g_ForceOperation
;
208 extern int g_NoNeedInputYes
;
209 extern HWND g_ProgressBarHwnd
;
210 extern HFONT g_language_normal_font
;
211 extern HFONT g_language_bold_font
;
212 extern int g_FilterUSB
;
216 void TraceOut(const char *Fmt
, ...);
217 void Log(const char *Fmt
, ...);
218 BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...);
219 void DumpWindowsVersion(void);
220 const CHAR
* GetLocalVentoyVersion(void);
221 const CHAR
* ParseVentoyVersionFromString(CHAR
*Buf
);
222 CHAR
GetFirstUnusedDriveLetter(void);
223 const CHAR
* GetBusTypeString(STORAGE_BUS_TYPE Type
);
224 int VentoyGetLocalBootImg(MBR_HEAD
*pMBR
);
225 int GetHumanReadableGBSize(UINT64 SizeBytes
);
226 void TrimString(CHAR
*String
);
227 int VentoyFillMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
, int PartStyle
);
228 int VentoyFillGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
229 BOOL
IsVentoyLogicalDrive(CHAR DriveLetter
);
230 int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
);
231 int GetPhysicalDriveCount(void);
232 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO
*pDriveList
, DWORD
*pDriveCount
);
233 int GetPhyDriveByLogicalDrive(int DriveLetter
, UINT64
*Offset
);
234 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO
*pDriveInfo
, UINT64 Part2StartSector
, CHAR
*VerBuf
, size_t BufLen
, BOOL
*pSecureBoot
);
235 int Ventoy2DiskInit(void);
236 int Ventoy2DiskDestroy(void);
237 PHY_DRIVE_INFO
* GetPhyDriveInfoById(int Id
);
238 PHY_DRIVE_INFO
* GetPhyDriveInfoByPhyDrive(int PhyDrive
);
239 int ParseCmdLineOption(LPSTR lpCmdLine
);
240 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
, int TryId
);
241 int PartitionResizeForVentoy(PHY_DRIVE_INFO
*pPhyDrive
);
242 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int TryId
);
243 int VentoyFillBackupGptHead(VTOY_GPT_INFO
*pInfo
, VTOY_GPT_HDR
*pHead
);
244 int VentoyFillWholeGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
245 void SetProgressBarPos(int Pos
);
246 int SaveBufToFile(const CHAR
*FileName
, const void *Buffer
, int BufLen
);
247 int ReadWholeFileToBuf(const CHAR
*FileName
, int ExtLen
, void **Bufer
, int *BufLen
);
248 int INIT
unxz(unsigned char *in
, int in_size
,
249 int(*fill
)(void *dest
, unsigned int size
),
250 int(*flush
)(void *src
, unsigned int size
),
251 unsigned char *out
, int *in_used
,
252 void(*error
)(char *x
));
253 void disk_io_set_param(HANDLE Handle
, UINT64 SectorCount
);
255 extern BOOL g_InputYes
;
256 INT_PTR CALLBACK
YesDialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
);
257 INT_PTR CALLBACK
PartDialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
);
258 int GetReservedSpaceInMB(void);
259 int IsPartNeed4KBAlign(void);
260 int GetVentoyFsType(void);
261 void SetVentoyFsType(int fs
);
262 const char* GetVentoyFsName(void);
263 int FindProcessOccupyDisk(HANDLE hDrive
, PHY_DRIVE_INFO
*pPhyDrive
);
264 int VentoyFillMBRLocation(UINT64 DiskSizeInBytes
, UINT32 StartSectorId
, UINT32 SectorCount
, PART_TABLE
*Table
);
265 int ClearVentoyFromPhyDrive(HWND hWnd
, PHY_DRIVE_INFO
*pPhyDrive
, char *pDrvLetter
);
266 UINT32
VentoyCrc32(void *Buffer
, UINT32 Length
);
267 BOOL
PartResizePreCheck(PHY_DRIVE_INFO
** ppPhyDrive
);
269 #define SET_FILE_POS(pos) \
270 liCurrentPosition.QuadPart = pos; \
271 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
273 #define SECURE_ICON_STRING _UICON(UNICODE_LOCK)
275 extern int g_WriteImage
;
277 #define VTSI_IMG_MAGIC 0x0000594F544E4556ULL // "VENTOY\0\0"
282 +---------------------------------
283 + sector 0 ~ sector N-1
285 +---------------------------------
292 + align data (aligned with 512)
293 +---------------------------------
295 +---------------------------------
297 * All the integers are in little endian
298 * The sector size is fixed 512 for ventoy image file.
302 #define VTSI_IMG_MAX_SEG 128
305 UINT64 disk_start_sector
;
314 UINT32 disk_signature
;
318 UINT32 segment_chksum
;
319 UINT64 segment_offset
;
321 UINT8 reserved
[512 - 44];
324 extern int __static_assert__
[sizeof(VTSI_FOOTER
) == 512 ? 1 : -1];
326 extern HWND g_DialogHwnd
;
328 #define SAFE_FREE(ptr) if (ptr) { free(ptr); (ptr) = NULL; }
329 int InstallVentoy2FileImage(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
);
330 void disk_io_set_imghook(FILE *fp
, VTSI_SEGMENT
*segment
, int maxseg
, UINT64 data_offset
);
331 void disk_io_reset_imghook(int *psegnum
, UINT64
*pDataOffset
);
333 HANDLE
GetPhysicalHandle(int Drive
, BOOLEAN bLockDrive
, BOOLEAN bWriteAccess
, BOOLEAN bWriteShare
);
334 void InitComboxCtrl(HWND hWnd
, int PhyDrive
);
335 int disk_io_is_write_error(void);
336 void disk_io_reset_write_error(void);
337 const char* GUID2String(void* guid
, char* buf
, int len
);
339 #define VTSI_SUPPORT 1