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