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 SIZE_2MB (2048 * 1024)
28 #define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
29 #define VENTOY_PART1_START_SECTOR 2048
31 #define VENTOY_FILE_BOOT_IMG "boot\\boot.img"
32 #define VENTOY_FILE_STG1_IMG "boot\\core.img.xz"
33 #define VENTOY_FILE_DISK_IMG "ventoy\\ventoy.disk.img.xz"
34 #define VENTOY_FILE_LOG "log.txt"
35 #define VENTOY_FILE_VERSION "ventoy\\version"
37 #define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
38 #define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
40 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
41 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
43 #define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
44 #define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
46 #define CHECK_CLOSE_HANDLE(Handle) \
48 if (Handle != INVALID_HANDLE_VALUE) \
50 CloseHandle(Handle); \
51 Handle = INVALID_HANDLE_VALUE; \
55 #define LASTERR GetLastError()
56 #define RET_LASTERR (ret ? 0 : LASTERR)
59 typedef struct PART_TABLE
61 UINT8 Active
; // 0x00 0x80
64 UINT16 StartSector
: 6;
65 UINT16 StartCylinder
: 10;
71 UINT16 EndCylinder
: 10;
77 typedef struct MBR_HEAD
80 PART_TABLE PartTbl
[4];
85 typedef struct VTOY_GPT_HDR
87 CHAR Signature
[8]; /* EFI PART */
94 UINT64 PartAreaStartLBA
;
95 UINT64 PartAreaEndLBA
;
97 UINT64 PartTblStartLBA
;
99 UINT32 PartTblEntryLen
;
101 UINT8 Reserved2
[420];
104 typedef struct VTOY_GPT_PART_TBL
114 typedef struct VTOY_GPT_INFO
118 VTOY_GPT_PART_TBL PartTbl
[128];
122 typedef struct ventoy_secure_data
124 UINT8 magic1
[16]; /* VENTOY_GUID */
127 UINT8 adminSHA256
[32];
128 UINT8 reserved
[4000];
129 UINT8 magic2
[16]; /* VENTOY_GUID */
135 #define VENTOY_MAX_PHY_DRIVE 128
137 typedef struct PHY_DRIVE_INFO
141 int PartStyle
;//0:MBR 1:GPT
147 CHAR ProductRev
[128];
148 CHAR SerialNumber
[128];
149 STORAGE_BUS_TYPE BusType
;
151 CHAR DriveLetters
[64];
153 CHAR VentoyVersion
[32];
155 BOOL SecureBootSupport
;
160 typedef enum PROGRESS_POINT
163 PT_LOCK_FOR_CLEAN
= 8,
167 PT_LOCK_VOLUME
= PT_FORMAT_PART1
,
170 PT_WRITE_VENTOY_START
,
171 PT_WRITE_VENTOY_FINISH
= PT_WRITE_VENTOY_START
+ 32,
180 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
182 extern PHY_DRIVE_INFO
*g_PhyDriveList
;
183 extern DWORD g_PhyDriveCount
;
184 extern int g_ForceOperation
;
185 extern HWND g_ProgressBarHwnd
;
186 extern HFONT g_language_normal_font
;
187 extern HFONT g_language_bold_font
;
188 extern int g_FilterUSB
;
192 void TraceOut(const char *Fmt
, ...);
193 void Log(const char *Fmt
, ...);
194 BOOL
IsPathExist(BOOL Dir
, const char *Fmt
, ...);
195 void DumpWindowsVersion(void);
196 const CHAR
* GetLocalVentoyVersion(void);
197 const CHAR
* ParseVentoyVersionFromString(CHAR
*Buf
);
198 CHAR
GetFirstUnusedDriveLetter(void);
199 const CHAR
* GetBusTypeString(STORAGE_BUS_TYPE Type
);
200 int VentoyGetLocalBootImg(MBR_HEAD
*pMBR
);
201 int GetHumanReadableGBSize(UINT64 SizeBytes
);
202 void TrimString(CHAR
*String
);
203 int VentoyFillMBR(UINT64 DiskSizeBytes
, MBR_HEAD
*pMBR
, int PartStyle
);
204 int VentoyFillGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
205 BOOL
IsVentoyLogicalDrive(CHAR DriveLetter
);
206 int GetRegDwordValue(HKEY Key
, LPCSTR SubKey
, LPCSTR ValueName
, DWORD
*pValue
);
207 int GetPhysicalDriveCount(void);
208 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO
*pDriveList
, DWORD
*pDriveCount
);
209 int GetPhyDriveByLogicalDrive(int DriveLetter
);
210 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO
*pDriveInfo
, UINT64 Part2StartSector
, CHAR
*VerBuf
, size_t BufLen
, BOOL
*pSecureBoot
);
211 int Ventoy2DiskInit(void);
212 int Ventoy2DiskDestroy(void);
213 PHY_DRIVE_INFO
* GetPhyDriveInfoById(int Id
);
214 int ParseCmdLineOption(LPSTR lpCmdLine
);
215 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
, int TryId
);
216 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO
*pPhyDrive
, int TryId
);
217 int VentoyFillBackupGptHead(VTOY_GPT_INFO
*pInfo
, VTOY_GPT_HDR
*pHead
);
218 int VentoyFillWholeGpt(UINT64 DiskSizeBytes
, VTOY_GPT_INFO
*pInfo
);
219 void SetProgressBarPos(int Pos
);
220 int SaveBufToFile(const CHAR
*FileName
, const void *Buffer
, int BufLen
);
221 int ReadWholeFileToBuf(const CHAR
*FileName
, int ExtLen
, void **Bufer
, int *BufLen
);
222 int INIT
unxz(unsigned char *in
, int in_size
,
223 int(*fill
)(void *dest
, unsigned int size
),
224 int(*flush
)(void *src
, unsigned int size
),
225 unsigned char *out
, int *in_used
,
226 void(*error
)(char *x
));
227 void disk_io_set_param(HANDLE Handle
, UINT64 SectorCount
);
228 INT_PTR CALLBACK
PartDialogProc(HWND hWnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
);
229 int GetReservedSpaceInMB(void);
230 int IsPartNeed4KBAlign(void);
231 int FindProcessOccupyDisk(HANDLE hDrive
, PHY_DRIVE_INFO
*pPhyDrive
);
232 int VentoyFillMBRLocation(UINT64 DiskSizeInBytes
, UINT32 StartSectorId
, UINT32 SectorCount
, PART_TABLE
*Table
);
233 int ClearVentoyFromPhyDrive(HWND hWnd
, PHY_DRIVE_INFO
*pPhyDrive
, char *pDrvLetter
);
234 UINT32
VentoyCrc32(void *Buffer
, UINT32 Length
);
236 #define SET_FILE_POS(pos) \
237 liCurrentPosition.QuadPart = pos; \
238 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
240 #define SECURE_ICON_STRING _UICON(UNICODE_LOCK)
242 extern int g_WriteImage
;
244 #define VTSI_IMG_MAGIC 0x0000594F544E4556ULL // "VENTOY\0\0"
249 +---------------------------------
250 + sector 0 ~ sector N-1
252 +---------------------------------
259 + align data (aligned with 512)
260 +---------------------------------
262 +---------------------------------
264 * All the integers are in little endian
265 * The sector size is fixed 512 for ventoy image file.
269 #define VTSI_IMG_MAX_SEG 128
272 UINT64 disk_start_sector
;
281 UINT32 disk_signature
;
285 UINT32 segment_chksum
;
286 UINT64 segment_offset
;
288 UINT8 reserved
[512 - 44];
291 extern int __static_assert__
[sizeof(VTSI_FOOTER
) == 512 ? 1 : -1];
294 #define SAFE_FREE(ptr) if (ptr) { free(ptr); (ptr) = NULL; }
295 int InstallVentoy2FileImage(PHY_DRIVE_INFO
*pPhyDrive
, int PartStyle
);
296 void disk_io_set_imghook(FILE *fp
, VTSI_SEGMENT
*segment
, int maxseg
, UINT64 data_offset
);
297 void disk_io_reset_imghook(int *psegnum
, UINT64
*pDataOffset
);
300 #define VTSI_SUPPORT 1