1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
21 #ifndef __VENTOY2DISK_H__
22 #define __VENTOY2DISK_H__
26 #define SIZE_1MB (1024 * 1024)
27 #define VENTOY_EFI_PART_SIZE (32 * SIZE_1MB)
28 #define VENTOY_PART1_START_SECTOR 2048
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"
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
39 #define IsFileExist(Fmt, ...) IsPathExist(FALSE, Fmt, __VA_ARGS__)
40 #define IsDirExist(Fmt, ...) IsPathExist(TRUE, Fmt, __VA_ARGS__)
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)
45 #define CHECK_CLOSE_HANDLE(Handle) \
47 if (Handle != INVALID_HANDLE_VALUE) \
49 CloseHandle(Handle); \
50 Handle = INVALID_HANDLE_VALUE; \
54 #define LASTERR GetLastError()
57 typedef struct PART_TABLE
59 UINT8 Active
; // 0x00 0x80
62 UINT16 StartSector
: 6;
63 UINT16 StartCylinder
: 10;
69 UINT16 EndCylinder
: 10;
75 typedef struct MBR_HEAD
78 PART_TABLE PartTbl
[4];
83 typedef struct VTOY_GPT_HDR
85 CHAR Signature
[8]; /* EFI PART */
92 UINT64 PartAreaStartLBA
;
93 UINT64 PartAreaEndLBA
;
95 UINT64 PartTblStartLBA
;
97 UINT32 PartTblEntryLen
;
102 typedef struct VTOY_GPT_PART_TBL
112 typedef struct VTOY_GPT_INFO
116 VTOY_GPT_PART_TBL PartTbl
[128];
121 #define VENTOY_MAX_PHY_DRIVE 128
123 typedef struct PHY_DRIVE_INFO
127 int PartStyle
;//0:MBR 1:GPT
133 CHAR ProductRev
[128];
134 CHAR SerialNumber
[128];
135 STORAGE_BUS_TYPE BusType
;
137 int FirstDriveLetter
;
138 CHAR VentoyVersion
[32];
142 typedef enum PROGRESS_POINT
149 PT_LOCK_VOLUME
= PT_FORMAT_PART1
,
152 PT_WRITE_VENTOY_START
,
153 PT_WRITE_VENTOY_FINISH
= PT_WRITE_VENTOY_START
+ 32,
162 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
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
;
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
);
211 #define SET_FILE_POS(pos) \
212 liCurrentPosition.QuadPart = pos; \
213 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\