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__
35 #define FAT32_MAX_LIMIT (32 * 1073741824ULL)
37 #define SIZE_1KB (1024)
38 #define SIZE_1GB (1024 * 1024 * 1024)
39 #define SIZE_1TB (1024ULL * 1024ULL * 1024ULL * 1024ULL)
40 #define SIZE_1MB (1024 * 1024)
41 #define SIZE_2MB (2048 * 1024)
42 #define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
43 #define VENTOY_PART1_START_SECTOR 2048
45 #define VENTOY_FILE_BOOT_IMG "boot\\boot.img"
46 #define VENTOY_FILE_STG1_IMG "boot\\core.img.xz"
47 #define VENTOY_FILE_DISK_IMG "ventoy\\ventoy.disk.img.xz"
48 #define VENTOY_FILE_LOG "log.txt"
49 #define VENTOY_FILE_VERSION "ventoy\\version"
51 #define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
52 #define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
54 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
55 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
57 #define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
58 #define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
60 #define CHECK_FREE(p) \
69 #define CHECK_CLOSE_HANDLE(Handle) \
71 if (Handle != INVALID_HANDLE_VALUE) \
73 CloseHandle(Handle); \
74 Handle = INVALID_HANDLE_VALUE; \
78 #define LASTERR GetLastError()
79 #define RET_LASTERR (ret ? 0 : LASTERR)
82 typedef struct PART_TABLE
84 UINT8 Active
; // 0x00 0x80
87 UINT16 StartSector
: 6;
88 UINT16 StartCylinder
: 10;
94 UINT16 EndCylinder
: 10;
100 typedef struct MBR_HEAD
103 PART_TABLE PartTbl
[4];
108 typedef struct VTOY_GPT_HDR
110 CHAR Signature
[8]; /* EFI PART */
117 UINT64 PartAreaStartLBA
;
118 UINT64 PartAreaEndLBA
;
120 UINT64 PartTblStartLBA
;
121 UINT32 PartTblTotNum
;
122 UINT32 PartTblEntryLen
;
124 UINT8 Reserved2
[420];
127 typedef struct VTOY_GPT_PART_TBL
137 typedef struct VTOY_GPT_INFO
141 VTOY_GPT_PART_TBL PartTbl
[128];
145 typedef struct ventoy_secure_data
147 UINT8 magic1
[16]; /* VENTOY_GUID */
150 UINT8 adminSHA256
[32];
151 UINT8 reserved
[4000];
152 UINT8 magic2
[16]; /* VENTOY_GUID */
158 #define VENTOY_MAX_PHY_DRIVE 128
160 typedef struct PHY_DRIVE_INFO
164 int PartStyle
;//0:MBR 1:GPT
170 CHAR ProductRev
[128];
171 CHAR SerialNumber
[128];
172 STORAGE_BUS_TYPE BusType
;
174 CHAR DriveLetters
[64];
176 int VentoyFsClusterSize
;
177 CHAR VentoyFsType
[16];
178 CHAR VentoyVersion
[32];
180 BOOL SecureBootSupport
;
185 UINT64 ResizeOldPart1Size
;
186 CHAR Part1DriveLetter
;
187 CHAR ResizeVolumeGuid
[64];
189 UINT64 ResizePart2StartSector
;
194 typedef enum PROGRESS_POINT
197 PT_LOCK_FOR_CLEAN
= 8,
201 PT_LOCK_VOLUME
= PT_FORMAT_PART1
,
204 PT_WRITE_VENTOY_START
,
205 PT_WRITE_VENTOY_FINISH
= PT_WRITE_VENTOY_START
+ 32,
212 PT_REFORMAT_FINISH
= PT_REFORMAT_START
+ 16,
217 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
219 extern PHY_DRIVE_INFO
*g_PhyDriveList
;
220 extern DWORD g_PhyDriveCount
;
221 extern int g_ForceOperation
;
222 extern int g_NoNeedInputYes
;
223 extern HWND g_ProgressBarHwnd
;
224 extern HFONT g_language_normal_font
;
225 extern HFONT g_language_bold_font
;
226 extern int g_FilterUSB
;
230 void TraceOut(const char *Fmt
, ...);
231 void Log(const char *Fmt
, ...);
232 BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...);
233 void DumpWindowsVersion(void);
234 const CHAR
* GetLocalVentoyVersion(void);
235 const CHAR
* ParseVentoyVersionFromString(CHAR
*Buf
);
236 CHAR
GetFirstUnusedDriveLetter(void);
237 const CHAR
* GetBusTypeString(STORAGE_BUS_TYPE Type
);
238 int VentoyGetLocalBootImg(MBR_HEAD
*pMBR
);
239 int GetHumanReadableGBSize(UINT64 SizeBytes
);
240 void TrimString(CHAR
*String
);
241 int VentoyFillMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
, int PartStyle
, UINT8 FsFlag
);
242 int VentoyFillGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
243 BOOL
IsVentoyLogicalDrive(CHAR DriveLetter
);
244 int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
);
245 int GetPhysicalDriveCount(void);
246 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO
*pDriveList
, DWORD
*pDriveCount
);
247 int GetPhyDriveByLogicalDrive(int DriveLetter
, UINT64
*Offset
);
248 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO
*pDriveInfo
, UINT64 Part2StartSector
, CHAR
*VerBuf
, size_t BufLen
, BOOL
*pSecureBoot
);
249 int Ventoy2DiskInit(void);
250 int Ventoy2DiskDestroy(void);
251 PHY_DRIVE_INFO
* GetPhyDriveInfoById(int Id
);
252 PHY_DRIVE_INFO
* GetPhyDriveInfoByPhyDrive(int PhyDrive
);
253 int ParseCmdLineOption(LPSTR lpCmdLine
);
254 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
, int TryId
);
255 int PartitionResizeForVentoy(PHY_DRIVE_INFO
*pPhyDrive
);
256 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int TryId
);
257 int VentoyFillBackupGptHead(VTOY_GPT_INFO
*pInfo
, VTOY_GPT_HDR
*pHead
);
258 int VentoyFillWholeGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
259 void SetProgressBarPos(int Pos
);
260 int SaveBufToFile(const CHAR
*FileName
, const void *Buffer
, int BufLen
);
261 int ReadWholeFileToBuf(const CHAR
*FileName
, int ExtLen
, void **Bufer
, int *BufLen
);
262 int INIT
unxz(unsigned char *in
, int in_size
,
263 int(*fill
)(void *dest
, unsigned int size
),
264 int(*flush
)(void *src
, unsigned int size
),
265 unsigned char *out
, int *in_used
,
266 void(*error
)(char *x
));
267 void disk_io_set_param(HANDLE Handle
, UINT64 SectorCount
);
268 int GetVolumeClusterSize(char Drive
);
270 extern BOOL g_InputYes
;
271 INT_PTR CALLBACK
YesDialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
);
272 INT_PTR CALLBACK
PartDialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
);
273 int GetReservedSpaceInMB(void);
274 int IsPartNeed4KBAlign(void);
275 int GetVentoyFsType(void);
276 void SetVentoyFsType(int fs
);
277 int GetClusterSize(void);
278 void SetClusterSize(int ClusterSize
);
279 WCHAR
* GetClusterSizeTip(void);
280 void FormatClusterSizeTip(int Size
, WCHAR
* pBuf
, size_t len
);
281 const char* GetVentoyFsName(void);
282 const char* GetVentoyFsNameByType(int fs
);
283 CHAR
* GetVentoyFsFmtNameByTypeA(int fs
);
284 WCHAR
* GetVentoyFsFmtNameByTypeW(int fs
);
285 int FindProcessOccupyDisk(HANDLE hDrive
, PHY_DRIVE_INFO
*pPhyDrive
);
286 int VentoyFillMBRLocation(UINT64 DiskSizeInBytes
, UINT32 StartSectorId
, UINT32 SectorCount
, PART_TABLE
*Table
);
287 int ClearVentoyFromPhyDrive(HWND hWnd
, PHY_DRIVE_INFO
*pPhyDrive
, char *pDrvLetter
);
288 UINT32
VentoyCrc32(void *Buffer
, UINT32 Length
);
289 BOOL
PartResizePreCheck(PHY_DRIVE_INFO
** ppPhyDrive
);
291 #define SET_FILE_POS(pos) \
292 liCurrentPosition.QuadPart = pos; \
293 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
295 extern int g_WriteImage;
297 #define VTSI_IMG_MAGIC 0x0000594F544E4556ULL // "VENTOY\0\0"
302 +---------------------------------
303 + sector 0 ~ sector N-1
305 +---------------------------------
312 + align data (aligned with 512)
313 +---------------------------------
315 +---------------------------------
317 * All the integers are in little endian
318 * The sector size is fixed 512 for ventoy image file.
322 #define VTSI_IMG_MAX_SEG 128
325 UINT64 disk_start_sector
;
334 UINT32 disk_signature
;
338 UINT32 segment_chksum
;
339 UINT64 segment_offset
;
341 UINT8 reserved
[512 - 44];
344 extern int __static_assert__
[sizeof(VTSI_FOOTER
) == 512 ? 1 : -1];
346 extern HWND g_DialogHwnd
;
348 #define SAFE_FREE(ptr) if (ptr) { free(ptr); (ptr) = NULL; }
349 int InstallVentoy2FileImage(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
);
350 void disk_io_set_imghook(FILE *fp
, VTSI_SEGMENT
*segment
, int maxseg
, UINT64 data_offset
);
351 void disk_io_reset_imghook(int *psegnum
, UINT64
*pDataOffset
);
353 HANDLE
GetPhysicalHandle(int Drive
, BOOLEAN bLockDrive
, BOOLEAN bWriteAccess
, BOOLEAN bWriteShare
);
354 void InitComboxCtrl(HWND hWnd
, int PhyDrive
);
355 int disk_io_is_write_error(void);
356 void disk_io_reset_write_error(void);
357 const char* GUID2String(void* guid
, char* buf
, int len
);
358 void VentoyStringToUpper(CHAR
* str
);
359 BOOL
AlertSuppressInit(void);
360 void SetAlertPromptHookEnable(BOOL enable
);
362 #define VTSI_SUPPORT 1