]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/Ventoy2Disk.h
f305ea03caadeeec33cf58478d4f6640caf4260b
[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 VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
28 #define VENTOY_PART1_START_SECTOR 2048
29
30 #define VENTOY_FILE_BOOT_IMG "boot\\boot.img"
31 #define VENTOY_FILE_STG1_IMG "boot\\core.img.xz"
32 #define VENTOY_FILE_DISK_IMG "ventoy\\ventoy.disk.img.xz"
33 #define VENTOY_FILE_LOG "log.txt"
34 #define VENTOY_FILE_VERSION "ventoy\\version"
35
36 #define DRIVE_ACCESS_TIMEOUT 15000 // How long we should retry drive access (in ms)
37 #define DRIVE_ACCESS_RETRIES 150 // How many times we should retry
38
39 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
40 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
41
42 #define safe_sprintf(dst, fmt, ...) sprintf_s(dst, sizeof(dst), fmt, __VA_ARGS__)
43 #define safe_strcpy(dst, src) strcpy_s(dst, sizeof(dst), src)
44
45 #define CHECK_CLOSE_HANDLE(Handle) \
46 {\
47 if (Handle != INVALID_HANDLE_VALUE) \
48 {\
49 CloseHandle(Handle); \
50 Handle = INVALID_HANDLE_VALUE; \
51 }\
52 }
53
54 #define LASTERR GetLastError()
55
56 #pragma pack(1)
57 typedef struct PART_TABLE
58 {
59 UINT8 Active; // 0x00 0x80
60
61 UINT8 StartHead;
62 UINT16 StartSector : 6;
63 UINT16 StartCylinder : 10;
64
65 UINT8 FsFlag;
66
67 UINT8 EndHead;
68 UINT16 EndSector : 6;
69 UINT16 EndCylinder : 10;
70
71 UINT32 StartSectorId;
72 UINT32 SectorCount;
73 }PART_TABLE;
74
75 typedef struct MBR_HEAD
76 {
77 UINT8 BootCode[446];
78 PART_TABLE PartTbl[4];
79 UINT8 Byte55;
80 UINT8 ByteAA;
81 }MBR_HEAD;
82
83 typedef struct VTOY_GPT_HDR
84 {
85 CHAR Signature[8]; /* EFI PART */
86 UINT8 Version[4];
87 UINT32 Length;
88 UINT32 Crc;
89 UINT8 Reserved1[4];
90 UINT64 EfiStartLBA;
91 UINT64 EfiBackupLBA;
92 UINT64 PartAreaStartLBA;
93 UINT64 PartAreaEndLBA;
94 GUID DiskGuid;
95 UINT64 PartTblStartLBA;
96 UINT32 PartTblTotNum;
97 UINT32 PartTblEntryLen;
98 UINT32 PartTblCrc;
99 UINT8 Reserved2[420];
100 }VTOY_GPT_HDR;
101
102 typedef struct VTOY_GPT_PART_TBL
103 {
104 GUID PartType;
105 GUID PartGuid;
106 UINT64 StartLBA;
107 UINT64 LastLBA;
108 UINT64 Attr;
109 UINT16 Name[36];
110 }VTOY_GPT_PART_TBL;
111
112 typedef struct VTOY_GPT_INFO
113 {
114 MBR_HEAD MBR;
115 VTOY_GPT_HDR Head;
116 VTOY_GPT_PART_TBL PartTbl[128];
117 }VTOY_GPT_INFO;
118
119 #pragma pack()
120
121 #define VENTOY_MAX_PHY_DRIVE 128
122
123 typedef struct PHY_DRIVE_INFO
124 {
125 int Id;
126 int PhyDrive;
127 int PartStyle;//0:MBR 1:GPT
128 UINT64 SizeInBytes;
129 BYTE DeviceType;
130 BOOL RemovableMedia;
131 CHAR VendorId[128];
132 CHAR ProductId[128];
133 CHAR ProductRev[128];
134 CHAR SerialNumber[128];
135 STORAGE_BUS_TYPE BusType;
136
137 int FirstDriveLetter;
138 CHAR VentoyVersion[32];
139
140 }PHY_DRIVE_INFO;
141
142 typedef enum PROGRESS_POINT
143 {
144 PT_START = 0,
145 PT_LOCK_FOR_CLEAN,
146 PT_DEL_ALL_PART,
147 PT_LOCK_FOR_WRITE,
148 PT_FORMAT_PART1,
149 PT_LOCK_VOLUME = PT_FORMAT_PART1,
150 PT_FORMAT_PART2,
151
152 PT_WRITE_VENTOY_START,
153 PT_WRITE_VENTOY_FINISH = PT_WRITE_VENTOY_START + 32,
154
155 PT_WRITE_STG1_IMG,
156 PT_WRITE_PART_TABLE,
157 PT_MOUNT_VOLUME,
158
159 PT_FINISH
160 }PROGRESS_POINT;
161
162 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
163
164 extern PHY_DRIVE_INFO *g_PhyDriveList;
165 extern DWORD g_PhyDriveCount;
166 extern int g_ForceOperation;
167 extern HWND g_ProgressBarHwnd;
168 extern HFONT g_language_normal_font;
169 extern HFONT g_language_bold_font;
170
171 void Log(const char *Fmt, ...);
172 BOOL IsPathExist(BOOL Dir, const char *Fmt, ...);
173 void DumpWindowsVersion(void);
174 const CHAR* GetLocalVentoyVersion(void);
175 const CHAR* ParseVentoyVersionFromString(CHAR *Buf);
176 CHAR GetFirstUnusedDriveLetter(void);
177 const CHAR * GetBusTypeString(STORAGE_BUS_TYPE Type);
178 int VentoyGetLocalBootImg(MBR_HEAD *pMBR);
179 int GetHumanReadableGBSize(UINT64 SizeBytes);
180 void TrimString(CHAR *String);
181 int VentoyFillMBR(UINT64 DiskSizeBytes, MBR_HEAD *pMBR, int PartStyle);
182 int VentoyFillGpt(UINT64 DiskSizeBytes, VTOY_GPT_INFO *pInfo);
183 BOOL IsVentoyLogicalDrive(CHAR DriveLetter);
184 int GetRegDwordValue(HKEY Key, LPCSTR SubKey, LPCSTR ValueName, DWORD *pValue);
185 int GetPhysicalDriveCount(void);
186 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount);
187 int GetPhyDriveByLogicalDrive(int DriveLetter);
188 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, UINT64 Part2StartSector, CHAR *VerBuf, size_t BufLen);
189 int Ventoy2DiskInit(void);
190 int Ventoy2DiskDestroy(void);
191 PHY_DRIVE_INFO * GetPhyDriveInfoById(int Id);
192 int ParseCmdLineOption(LPSTR lpCmdLine);
193 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive, int PartStyle);
194 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive);
195 int VentoyFillBackupGptHead(VTOY_GPT_INFO *pInfo, VTOY_GPT_HDR *pHead);
196 void SetProgressBarPos(int Pos);
197 int ReadWholeFileToBuf(const CHAR *FileName, int ExtLen, void **Bufer, int *BufLen);
198 int INIT unxz(unsigned char *in, int in_size,
199 int(*fill)(void *dest, unsigned int size),
200 int(*flush)(void *src, unsigned int size),
201 unsigned char *out, int *in_used,
202 void(*error)(char *x));
203 void disk_io_set_param(HANDLE Handle, UINT64 SectorCount);
204 INT_PTR CALLBACK PartDialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
205 int GetReservedSpaceInMB(void);
206 int FindProcessOccupyDisk(HANDLE hDrive, PHY_DRIVE_INFO *pPhyDrive);
207 int VentoyFillLocation(UINT64 DiskSizeInBytes, UINT32 StartSectorId, UINT32 SectorCount, PART_TABLE *Table);
208 int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLetter);
209 UINT32 VentoyCrc32(void *Buffer, UINT32 Length);
210
211 #define SET_FILE_POS(pos) \
212 liCurrentPosition.QuadPart = pos; \
213 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
214
215
216 #endif