]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/Ventoy2Disk.h
Update hi_HI.json (#2081)
[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 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
53
54 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
55 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
56
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)
59
60 #define CHECK_FREE(p) \
61 {\
62 if (p)\
63 {\
64 free(p); \
65 (p) = NULL; \
66 }\
67 }
68
69 #define CHECK_CLOSE_HANDLE(Handle) \
70 {\
71 if (Handle != INVALID_HANDLE_VALUE) \
72 {\
73 CloseHandle(Handle); \
74 Handle = INVALID_HANDLE_VALUE; \
75 }\
76 }
77
78 #define LASTERR GetLastError()
79 #define RET_LASTERR (ret ? 0 : LASTERR)
80
81 #pragma pack(1)
82 typedef struct PART_TABLE
83 {
84 UINT8 Active; // 0x00 0x80
85
86 UINT8 StartHead;
87 UINT16 StartSector : 6;
88 UINT16 StartCylinder : 10;
89
90 UINT8 FsFlag;
91
92 UINT8 EndHead;
93 UINT16 EndSector : 6;
94 UINT16 EndCylinder : 10;
95
96 UINT32 StartSectorId;
97 UINT32 SectorCount;
98 }PART_TABLE;
99
100 typedef struct MBR_HEAD
101 {
102 UINT8 BootCode[446];
103 PART_TABLE PartTbl[4];
104 UINT8 Byte55;
105 UINT8 ByteAA;
106 }MBR_HEAD;
107
108 typedef struct VTOY_GPT_HDR
109 {
110 CHAR Signature[8]; /* EFI PART */
111 UINT8 Version[4];
112 UINT32 Length;
113 UINT32 Crc;
114 UINT8 Reserved1[4];
115 UINT64 EfiStartLBA;
116 UINT64 EfiBackupLBA;
117 UINT64 PartAreaStartLBA;
118 UINT64 PartAreaEndLBA;
119 GUID DiskGuid;
120 UINT64 PartTblStartLBA;
121 UINT32 PartTblTotNum;
122 UINT32 PartTblEntryLen;
123 UINT32 PartTblCrc;
124 UINT8 Reserved2[420];
125 }VTOY_GPT_HDR;
126
127 typedef struct VTOY_GPT_PART_TBL
128 {
129 GUID PartType;
130 GUID PartGuid;
131 UINT64 StartLBA;
132 UINT64 LastLBA;
133 UINT64 Attr;
134 UINT16 Name[36];
135 }VTOY_GPT_PART_TBL;
136
137 typedef struct VTOY_GPT_INFO
138 {
139 MBR_HEAD MBR;
140 VTOY_GPT_HDR Head;
141 VTOY_GPT_PART_TBL PartTbl[128];
142 }VTOY_GPT_INFO;
143
144
145 typedef struct ventoy_secure_data
146 {
147 UINT8 magic1[16]; /* VENTOY_GUID */
148 UINT8 diskuuid[16];
149 UINT8 Checksum[16];
150 UINT8 adminSHA256[32];
151 UINT8 reserved[4000];
152 UINT8 magic2[16]; /* VENTOY_GUID */
153 }ventoy_secure_data;
154
155
156 #pragma pack()
157
158 #define VENTOY_MAX_PHY_DRIVE 128
159
160 typedef struct PHY_DRIVE_INFO
161 {
162 int Id;
163 int PhyDrive;
164 int PartStyle;//0:MBR 1:GPT
165 UINT64 SizeInBytes;
166 BYTE DeviceType;
167 BOOL RemovableMedia;
168 CHAR VendorId[128];
169 CHAR ProductId[128];
170 CHAR ProductRev[128];
171 CHAR SerialNumber[128];
172 STORAGE_BUS_TYPE BusType;
173
174 CHAR DriveLetters[64];
175
176 int VentoyFsClusterSize;
177 CHAR VentoyFsType[16];
178 CHAR VentoyVersion[32];
179
180 BOOL SecureBootSupport;
181 MBR_HEAD MBR;
182 UINT64 Part2GPTAttr;
183
184 BOOL ResizeNoShrink;
185 UINT64 ResizeOldPart1Size;
186 CHAR Part1DriveLetter;
187 CHAR ResizeVolumeGuid[64];
188 CHAR FsName[64];
189 UINT64 ResizePart2StartSector;
190 VTOY_GPT_INFO Gpt;
191
192 }PHY_DRIVE_INFO;
193
194 typedef enum PROGRESS_POINT
195 {
196 PT_START = 0,
197 PT_LOCK_FOR_CLEAN = 8,
198 PT_DEL_ALL_PART,
199 PT_LOCK_FOR_WRITE,
200 PT_FORMAT_PART1,
201 PT_LOCK_VOLUME = PT_FORMAT_PART1,
202 PT_FORMAT_PART2,
203
204 PT_WRITE_VENTOY_START,
205 PT_WRITE_VENTOY_FINISH = PT_WRITE_VENTOY_START + 32,
206
207 PT_WRITE_STG1_IMG,
208 PT_WRITE_PART_TABLE,
209 PT_MOUNT_VOLUME,
210
211 PT_REFORMAT_START,
212 PT_REFORMAT_FINISH = PT_REFORMAT_START + 16,
213
214 PT_FINISH
215 }PROGRESS_POINT;
216
217 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
218
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;
227
228
229
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);
269
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);
290
291 #define SET_FILE_POS(pos) \
292 liCurrentPosition.QuadPart = pos; \
293 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
294
295 extern int g_WriteImage;
296
297 #define VTSI_IMG_MAGIC 0x0000594F544E4556ULL // "VENTOY\0\0"
298
299 #pragma pack(1)
300
301 /*
302 +---------------------------------
303 + sector 0 ~ sector N-1
304 + data area
305 +---------------------------------
306 + sector N ~
307 + segment[0]
308 + segment[1]
309 + segment[2]
310 + ......
311 + segment[M-1]
312 + align data (aligned with 512)
313 +---------------------------------
314 + footer
315 +---------------------------------
316 *
317 * All the integers are in little endian
318 * The sector size is fixed 512 for ventoy image file.
319 *
320 */
321
322 #define VTSI_IMG_MAX_SEG 128
323
324 typedef struct {
325 UINT64 disk_start_sector;
326 UINT64 sector_num;
327 UINT64 data_offset;
328 }VTSI_SEGMENT;
329
330 typedef struct {
331 UINT64 magic;
332 UINT32 version;
333 UINT64 disk_size;
334 UINT32 disk_signature;
335 UINT32 foot_chksum;
336
337 UINT32 segment_num;
338 UINT32 segment_chksum;
339 UINT64 segment_offset;
340
341 UINT8 reserved[512 - 44];
342 }VTSI_FOOTER;
343 #pragma pack()
344 extern int __static_assert__[sizeof(VTSI_FOOTER) == 512 ? 1 : -1];
345
346 extern HWND g_DialogHwnd;
347
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);
352
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);
361
362 #define VTSI_SUPPORT 1
363
364
365 #endif