]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/Ventoy2Disk.h
6106520d52cf3f9d37cfffc1088863cc295e3d0b
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / Ventoy2Disk.h
1 /******************************************************************************
2 * Ventoy2Disk.h
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #ifndef __VENTOY2DISK_H__
22 #define __VENTOY2DISK_H__
23
24 #include <stdio.h>
25
26 typedef enum VTOY_FS
27 {
28 VTOY_FS_EXFAT = 0,
29 VTOY_FS_NTFS,
30 VTOY_FS_FAT32,
31 VTOY_FS_UDF,
32 VTOY_FS_BUTT
33 }VTOY_FS;
34
35 #define FAT32_MAX_LIMIT (32 * 1073741824ULL)
36
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
44
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"
50
51 #define VENTOY_CLI_LOG "cli_log.txt"
52 #define VENTOY_CLI_PERCENT "cli_percent.txt"
53 #define VENTOY_CLI_DONE "cli_done.txt"
54
55
56 #define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
57 #define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
58
59 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
60 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
61
62 #define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
63 #define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
64
65 #define CHECK_FREE(p) \
66 {\
67 if (p)\
68 {\
69 free(p); \
70 (p) = NULL; \
71 }\
72 }
73
74 #define CHECK_CLOSE_HANDLE(Handle) \
75 {\
76 if (Handle != INVALID_HANDLE_VALUE) \
77 {\
78 CloseHandle(Handle); \
79 Handle = INVALID_HANDLE_VALUE; \
80 }\
81 }
82
83 #define LASTERR GetLastError()
84 #define RET_LASTERR (ret ? 0 : LASTERR)
85
86 #pragma pack(1)
87 typedef struct PART_TABLE
88 {
89 UINT8 Active; // 0x00 0x80
90
91 UINT8 StartHead;
92 UINT16 StartSector : 6;
93 UINT16 StartCylinder : 10;
94
95 UINT8 FsFlag;
96
97 UINT8 EndHead;
98 UINT16 EndSector : 6;
99 UINT16 EndCylinder : 10;
100
101 UINT32 StartSectorId;
102 UINT32 SectorCount;
103 }PART_TABLE;
104
105 typedef struct MBR_HEAD
106 {
107 UINT8 BootCode[446];
108 PART_TABLE PartTbl[4];
109 UINT8 Byte55;
110 UINT8 ByteAA;
111 }MBR_HEAD;
112
113 typedef struct VTOY_GPT_HDR
114 {
115 CHAR Signature[8]; /* EFI PART */
116 UINT8 Version[4];
117 UINT32 Length;
118 UINT32 Crc;
119 UINT8 Reserved1[4];
120 UINT64 EfiStartLBA;
121 UINT64 EfiBackupLBA;
122 UINT64 PartAreaStartLBA;
123 UINT64 PartAreaEndLBA;
124 GUID DiskGuid;
125 UINT64 PartTblStartLBA;
126 UINT32 PartTblTotNum;
127 UINT32 PartTblEntryLen;
128 UINT32 PartTblCrc;
129 UINT8 Reserved2[420];
130 }VTOY_GPT_HDR;
131
132 typedef struct VTOY_GPT_PART_TBL
133 {
134 GUID PartType;
135 GUID PartGuid;
136 UINT64 StartLBA;
137 UINT64 LastLBA;
138 UINT64 Attr;
139 UINT16 Name[36];
140 }VTOY_GPT_PART_TBL;
141
142 typedef struct VTOY_GPT_INFO
143 {
144 MBR_HEAD MBR;
145 VTOY_GPT_HDR Head;
146 VTOY_GPT_PART_TBL PartTbl[128];
147 }VTOY_GPT_INFO;
148
149
150 typedef struct ventoy_secure_data
151 {
152 UINT8 magic1[16]; /* VENTOY_GUID */
153 UINT8 diskuuid[16];
154 UINT8 Checksum[16];
155 UINT8 adminSHA256[32];
156 UINT8 reserved[4000];
157 UINT8 magic2[16]; /* VENTOY_GUID */
158 }ventoy_secure_data;
159
160
161 #pragma pack()
162
163 #define VENTOY_MAX_PHY_DRIVE 128
164
165 typedef struct PHY_DRIVE_INFO
166 {
167 int Id;
168 int PhyDrive;
169 int PartStyle;//0:MBR 1:GPT
170 UINT64 SizeInBytes;
171 BYTE DeviceType;
172 BOOL RemovableMedia;
173 CHAR VendorId[128];
174 CHAR ProductId[128];
175 CHAR ProductRev[128];
176 CHAR SerialNumber[128];
177 STORAGE_BUS_TYPE BusType;
178
179 DWORD BytesPerLogicalSector;
180 DWORD BytesPerPhysicalSector;
181
182 CHAR DriveLetters[64];
183
184 int VentoyFsClusterSize;
185 CHAR VentoyFsType[16];
186 CHAR VentoyVersion[32];
187
188 BOOL SecureBootSupport;
189 MBR_HEAD MBR;
190 UINT64 Part2GPTAttr;
191
192 BOOL ResizeNoShrink;
193 UINT64 ResizeOldPart1Size;
194 CHAR Part1DriveLetter;
195 CHAR ResizeVolumeGuid[64];
196 CHAR FsName[64];
197 UINT64 ResizePart2StartSector;
198 VTOY_GPT_INFO Gpt;
199
200 }PHY_DRIVE_INFO;
201
202 typedef enum PROGRESS_POINT
203 {
204 PT_START = 0,
205 PT_LOCK_FOR_CLEAN = 8,
206 PT_DEL_ALL_PART,
207 PT_LOCK_FOR_WRITE,
208 PT_FORMAT_PART1,
209 PT_LOCK_VOLUME = PT_FORMAT_PART1,
210 PT_FORMAT_PART2,
211
212 PT_WRITE_VENTOY_START,
213 PT_WRITE_VENTOY_FINISH = PT_WRITE_VENTOY_START + 32,
214
215 PT_WRITE_STG1_IMG,
216 PT_WRITE_PART_TABLE,
217 PT_MOUNT_VOLUME,
218
219 PT_REFORMAT_START,
220 PT_REFORMAT_FINISH = PT_REFORMAT_START + 16,
221
222 PT_FINISH
223 }PROGRESS_POINT;
224
225 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
226
227 extern PHY_DRIVE_INFO *g_PhyDriveList;
228 extern DWORD g_PhyDriveCount;
229 extern int g_ForceOperation;
230 extern int g_NoNeedInputYes;
231 extern HWND g_ProgressBarHwnd;
232 extern HFONT g_language_normal_font;
233 extern HFONT g_language_bold_font;
234 extern int g_FilterUSB;
235
236
237
238 void TraceOut(const char *Fmt, ...);
239 void Log(const char *Fmt, ...);
240 void LogCache(BOOL cache);
241 void LogFlush(void);
242 BOOL IsPathExist(BOOL Dir, const char *Fmt, ...);
243 void DumpWindowsVersion(void);
244 const CHAR* GetLocalVentoyVersion(void);
245 const CHAR* ParseVentoyVersionFromString(CHAR *Buf);
246 CHAR GetFirstUnusedDriveLetter(void);
247 const CHAR * GetBusTypeString(STORAGE_BUS_TYPE Type);
248 int VentoyGetLocalBootImg(MBR_HEAD *pMBR);
249 int GetHumanReadableGBSize(UINT64 SizeBytes);
250 void TrimString(CHAR *String);
251 int VentoyFillMBR(UINT64 DiskSizeBytes, MBR_HEAD *pMBR, int PartStyle, UINT8 FsFlag);
252 int VentoyFillGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo);
253 BOOL IsVentoyLogicalDrive(CHAR DriveLetter);
254 int GetRegDwordValue(HKEY Key, LPCSTR SubKey, LPCSTR ValueName, DWORD *pValue);
255 int GetPhysicalDriveCount(void);
256 BOOL VentoyPhydriveMatch(PHY_DRIVE_INFO* pPhyDrive);
257 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount);
258 int GetPhyDriveByLogicalDrive(int DriveLetter, UINT64*Offset);
259 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen, BOOL *pSecureBoot);
260 int Ventoy2DiskInit(void);
261 int Ventoy2DiskDestroy(void);
262 PHY_DRIVE_INFO * GetPhyDriveInfoById(int Id);
263 PHY_DRIVE_INFO * GetPhyDriveInfoByPhyDrive(int PhyDrive);
264 int ParseCmdLineOption(LPSTR lpCmdLine);
265 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle, int TryId);
266 int PartitionResizeForVentoy(PHY_DRIVE_INFO *pPhyDrive);
267 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int TryId);
268 int VentoyFillBackupGptHead(VTOY_GPT_INFO *pInfo, VTOY_GPT_HDR *pHead);
269 int VentoyFillWholeGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo);
270 void SetProgressBarPos(int Pos);
271 int SaveBufToFile(const CHAR *FileName, const void *Buffer, int BufLen);
272 int ReadWholeFileToBuf(const CHAR *FileName, int ExtLen, void **Bufer, int *BufLen);
273 int INIT unxz(unsigned char *in, int in_size,
274 int(*fill)(void *dest, unsigned int size),
275 int(*flush)(void *src, unsigned int size),
276 unsigned char *out, int *in_used,
277 void(*error)(char *x));
278 void disk_io_set_param(HANDLE Handle, UINT64 SectorCount);
279 int GetVolumeClusterSize(char Drive);
280
281 extern BOOL g_InputYes;
282 INT_PTR CALLBACK YesDialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
283 INT_PTR CALLBACK PartDialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
284 int GetReservedSpaceInMB(void);
285 int IsPartNeed4KBAlign(void);
286 int GetVentoyFsType(void);
287 void SetVentoyFsType(int fs);
288 int GetClusterSize(void);
289 void SetClusterSize(int ClusterSize);
290 WCHAR* GetClusterSizeTip(void);
291 void FormatClusterSizeTip(int Size, WCHAR* pBuf, size_t len);
292 const char* GetVentoyFsName(void);
293 const char* GetVentoyFsNameByType(int fs);
294 CHAR* GetVentoyFsFmtNameByTypeA(int fs);
295 WCHAR* GetVentoyFsFmtNameByTypeW(int fs);
296 int FindProcessOccupyDisk(HANDLE hDrive, PHY_DRIVE_INFO *pPhyDrive);
297 int VentoyFillMBRLocation(UINT64 DiskSizeInBytes, UINT32 StartSectorId, UINT32 SectorCount, PART_TABLE *Table);
298 int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLetter);
299 UINT32 VentoyCrc32(void *Buffer, UINT32 Length);
300 BOOL PartResizePreCheck(PHY_DRIVE_INFO** ppPhyDrive);
301
302 #define SET_FILE_POS(pos) \
303 liCurrentPosition.QuadPart = pos; \
304 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
305
306 extern int g_WriteImage;
307
308 #define VTSI_IMG_MAGIC 0x0000594F544E4556ULL // "VENTOY\0\0"
309
310 #pragma pack(1)
311
312 /*
313 +---------------------------------
314 + sector 0 ~ sector N-1
315 + data area
316 +---------------------------------
317 + sector N ~
318 + segment[0]
319 + segment[1]
320 + segment[2]
321 + ......
322 + segment[M-1]
323 + align data (aligned with 512)
324 +---------------------------------
325 + footer
326 +---------------------------------
327 *
328 * All the integers are in little endian
329 * The sector size is fixed 512 for ventoy image file.
330 *
331 */
332
333 #define VTSI_IMG_MAX_SEG 128
334
335 typedef struct {
336 UINT64 disk_start_sector;
337 UINT64 sector_num;
338 UINT64 data_offset;
339 }VTSI_SEGMENT;
340
341 typedef struct {
342 UINT64 magic;
343 UINT32 version;
344 UINT64 disk_size;
345 UINT32 disk_signature;
346 UINT32 foot_chksum;
347
348 UINT32 segment_num;
349 UINT32 segment_chksum;
350 UINT64 segment_offset;
351
352 UINT8 reserved[512 - 44];
353 }VTSI_FOOTER;
354 #pragma pack()
355 extern int __static_assert__[sizeof(VTSI_FOOTER) == 512 ? 1 : -1];
356
357 extern HWND g_DialogHwnd;
358
359 extern BOOL g_CLI_Mode;
360
361 #define SAFE_FREE(ptr) if (ptr) { free(ptr); (ptr) = NULL; }
362 int InstallVentoy2FileImage(PHY_DRIVE_INFO *pPhyDrive, int PartStyle);
363 void disk_io_set_imghook(FILE *fp, VTSI_SEGMENT *segment, int maxseg, UINT64 data_offset);
364 void disk_io_reset_imghook(int *psegnum, UINT64 *pDataOffset);
365
366 HANDLE GetPhysicalHandle(int Drive, BOOLEAN bLockDrive, BOOLEAN bWriteAccess, BOOLEAN bWriteShare);
367 void InitComboxCtrl(HWND hWnd, int PhyDrive);
368 int disk_io_is_write_error(void);
369 void disk_io_reset_write_error(void);
370 const char* GUID2String(void* guid, char* buf, int len);
371 void VentoyStringToUpper(CHAR* str);
372 BOOL AlertSuppressInit(void);
373 void SetAlertPromptHookEnable(BOOL enable);
374 int VentoyCLIMain(int argc, char** argv);
375 BOOL IsVentoyPhyDrive(int PhyDrive, UINT64 SizeBytes, MBR_HEAD* pMBR, UINT64* Part2StartSector, UINT64* GptPart2Attr);
376 int GetVentoyFsNameInPhyDrive(PHY_DRIVE_INFO* CurDrive);
377 void CLISetReserveSpace(int MB);
378 void CLI_UpdatePercent(int Pos);
379 int GetLettersBelongPhyDrive(int PhyDrive, char* DriveLetters, size_t Length);
380 PHY_DRIVE_INFO* CLI_PhyDrvInfo(void);
381
382 #define UTF8_Log(fmt, wstr) \
383 {\
384 memset(TmpPathA, 0, sizeof(TmpPathA));\
385 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, TmpPathA, sizeof(TmpPathA), NULL, NULL);\
386 Log(fmt, TmpPathA);\
387 }
388
389 #define VTSI_SUPPORT 1
390
391
392 #define WM_OFFSET (WM_USER + 40)
393 #define WM_WIDTH_CHANGE (WM_OFFSET + 1)
394
395
396 int ExpandDlg(HWND hParent, UINT uiID, int WidthDelta);
397 int MoveDlg(HWND hParent, UINT uiID, int WidthDelta);
398
399 #endif