]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/Ventoy2Disk.h
update
[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 #pragma pack()
83
84 #define VENTOY_MAX_PHY_DRIVE 128
85
86 typedef struct PHY_DRIVE_INFO
87 {
88 int Id;
89 int PhyDrive;
90 UINT64 SizeInBytes;
91 BYTE DeviceType;
92 BOOL RemovableMedia;
93 CHAR VendorId[128];
94 CHAR ProductId[128];
95 CHAR ProductRev[128];
96 CHAR SerialNumber[128];
97 STORAGE_BUS_TYPE BusType;
98
99 int FirstDriveLetter;
100 CHAR VentoyVersion[32];
101
102 }PHY_DRIVE_INFO;
103
104 typedef enum PROGRESS_POINT
105 {
106 PT_START = 0,
107 PT_LOCK_FOR_CLEAN,
108 PT_DEL_ALL_PART,
109 PT_LOCK_FOR_WRITE,
110 PT_FORMAT_PART1,
111 PT_LOCK_VOLUME = PT_FORMAT_PART1,
112 PT_FORMAT_PART2,
113
114 PT_WRITE_VENTOY_START,
115 PT_WRITE_VENTOY_FINISH = PT_WRITE_VENTOY_START + 32,
116
117 PT_WRITE_STG1_IMG,
118 PT_WRITE_PART_TABLE,
119 PT_MOUNT_VOLUME,
120
121 PT_FINISH
122 }PROGRESS_POINT;
123
124 #define PROGRESS_BAR_SET_POS(pos) SetProgressBarPos(pos)
125
126 extern PHY_DRIVE_INFO *g_PhyDriveList;
127 extern DWORD g_PhyDriveCount;
128 extern int g_ForceOperation;
129 extern HWND g_ProgressBarHwnd;
130
131 void Log(const char *Fmt, ...);
132 BOOL IsPathExist(BOOL Dir, const char *Fmt, ...);
133 void DumpWindowsVersion(void);
134 const CHAR* GetLocalVentoyVersion(void);
135 const CHAR* ParseVentoyVersionFromString(CHAR *Buf);
136 CHAR GetFirstUnusedDriveLetter(void);
137 const CHAR * GetBusTypeString(STORAGE_BUS_TYPE Type);
138 int VentoyGetLocalBootImg(MBR_HEAD *pMBR);
139 int GetHumanReadableGBSize(UINT64 SizeBytes);
140 void TrimString(CHAR *String);
141 int VentoyFillMBR(UINT64 DiskSizeBytes, MBR_HEAD *pMBR);
142 BOOL IsVentoyLogicalDrive(CHAR DriveLetter);
143 int GetRegDwordValue(HKEY Key, LPCSTR SubKey, LPCSTR ValueName, DWORD *pValue);
144 int GetPhysicalDriveCount(void);
145 int GetAllPhysicalDriveInfo(PHY_DRIVE_INFO *pDriveList, DWORD *pDriveCount);
146 int GetPhyDriveByLogicalDrive(int DriveLetter);
147 int GetVentoyVerInPhyDrive(const PHY_DRIVE_INFO *pDriveInfo, MBR_HEAD *pMBR, CHAR *VerBuf, size_t BufLen);
148 int Ventoy2DiskInit(void);
149 int Ventoy2DiskDestroy(void);
150 PHY_DRIVE_INFO * GetPhyDriveInfoById(int Id);
151 int ParseCmdLineOption(LPSTR lpCmdLine);
152 int InstallVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive);
153 int UpdateVentoy2PhyDrive(PHY_DRIVE_INFO *pPhyDrive);
154 void SetProgressBarPos(int Pos);
155 int ReadWholeFileToBuf(const CHAR *FileName, int ExtLen, void **Bufer, int *BufLen);
156 int INIT unxz(unsigned char *in, int in_size,
157 int(*fill)(void *dest, unsigned int size),
158 int(*flush)(void *src, unsigned int size),
159 unsigned char *out, int *in_used,
160 void(*error)(char *x));
161 void disk_io_set_param(HANDLE Handle, UINT64 SectorCount);
162 INT_PTR CALLBACK PartDialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
163 int GetReservedSpaceInMB(void);
164 int FindProcessOccupyDisk(HANDLE hDrive, PHY_DRIVE_INFO *pPhyDrive);
165 int VentoyFillLocation(UINT64 DiskSizeInBytes, UINT32 StartSectorId, UINT32 SectorCount, PART_TABLE *Table);
166 int ClearVentoyFromPhyDrive(HWND hWnd, PHY_DRIVE_INFO *pPhyDrive, char *pDrvLetter);
167
168 #define SET_FILE_POS(pos) \
169 liCurrentPosition.QuadPart = pos; \
170 SetFilePointerEx(hDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN)\
171
172
173 #endif