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