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