]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.h
1.0.07 release
[Ventoy.git] / EDK2 / edk2-edk2-stable201911 / MdeModulePkg / Application / Ventoy / Ventoy.h
1 /******************************************************************************
2 * Ventoy.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 __VENTOY_H__
22 #define __VENTOY_H__
23
24 #define COMPILE_ASSERT(expr) extern char __compile_assert[(expr) ? 1 : -1]
25
26 #define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}
27
28 #pragma pack(1)
29
30 typedef struct ventoy_guid
31 {
32 UINT32 data1;
33 UINT16 data2;
34 UINT16 data3;
35 UINT8 data4[8];
36 }ventoy_guid;
37
38 typedef struct ventoy_image_disk_region
39 {
40 UINT32 image_sector_count; /* image sectors contained in this region */
41 UINT32 image_start_sector; /* image sector start */
42 UINT64 disk_start_sector; /* disk sector start */
43 }ventoy_image_disk_region;
44
45 typedef struct ventoy_image_location
46 {
47 ventoy_guid guid;
48
49 /* image sector size, currently this value is always 2048 */
50 UINT32 image_sector_size;
51
52 /* disk sector size, normally the value is 512 */
53 UINT32 disk_sector_size;
54
55 UINT32 region_count;
56
57 /*
58 * disk region data
59 * If the image file has more than one fragments in disk,
60 * there will be more than one region data here.
61 *
62 */
63 ventoy_image_disk_region regions[1];
64
65 /* ventoy_image_disk_region regions[2~region_count-1] */
66 }ventoy_image_location;
67
68 typedef struct ventoy_os_param
69 {
70 ventoy_guid guid; // VENTOY_GUID
71 UINT8 chksum; // checksum
72
73 UINT8 vtoy_disk_guid[16];
74 UINT64 vtoy_disk_size; // disk size in bytes
75 UINT16 vtoy_disk_part_id; // begin with 1
76 UINT16 vtoy_disk_part_type; // 0:exfat 1:ntfs other: reserved
77 char vtoy_img_path[384]; // It seems to be enough, utf-8 format
78 UINT64 vtoy_img_size; // image file size in bytes
79
80 /*
81 * Ventoy will write a copy of ventoy_image_location data into runtime memory
82 * this is the physically address and length of that memory.
83 * Address 0 means no such data exist.
84 * Address will be aligned by 4KB.
85 *
86 */
87 UINT64 vtoy_img_location_addr;
88 UINT32 vtoy_img_location_len;
89
90 UINT64 vtoy_reserved[4]; // Internal use by ventoy
91
92 UINT8 reserved[31];
93 }ventoy_os_param;
94
95 #pragma pack()
96
97 // compile assert to check that size of ventoy_os_param must be 512
98 COMPILE_ASSERT(sizeof(ventoy_os_param) == 512);
99
100
101
102 #pragma pack(4)
103
104 typedef struct ventoy_chain_head
105 {
106 ventoy_os_param os_param;
107
108 UINT32 disk_drive;
109 UINT32 drive_map;
110 UINT32 disk_sector_size;
111
112 UINT64 real_img_size_in_bytes;
113 UINT64 virt_img_size_in_bytes;
114 UINT32 boot_catalog;
115 UINT8 boot_catalog_sector[2048];
116
117 UINT32 img_chunk_offset;
118 UINT32 img_chunk_num;
119
120 UINT32 override_chunk_offset;
121 UINT32 override_chunk_num;
122
123 UINT32 virt_chunk_offset;
124 UINT32 virt_chunk_num;
125 }ventoy_chain_head;
126
127
128 typedef struct ventoy_img_chunk
129 {
130 UINT32 img_start_sector; //2KB
131 UINT32 img_end_sector;
132
133 UINT64 disk_start_sector; // in disk_sector_size
134 UINT64 disk_end_sector;
135 }ventoy_img_chunk;
136
137
138 typedef struct ventoy_override_chunk
139 {
140 UINT64 img_offset;
141 UINT32 override_size;
142 UINT8 override_data[512];
143 }ventoy_override_chunk;
144
145 typedef struct ventoy_virt_chunk
146 {
147 UINT32 mem_sector_start;
148 UINT32 mem_sector_end;
149 UINT32 mem_sector_offset;
150 UINT32 remap_sector_start;
151 UINT32 remap_sector_end;
152 UINT32 org_sector_start;
153 }ventoy_virt_chunk;
154
155
156 #pragma pack()
157
158
159 #define VTOY_BLOCK_DEVICE_PATH_GUID \
160 { 0x37b87ac6, 0xc180, 0x4583, { 0xa7, 0x05, 0x41, 0x4d, 0xa8, 0xf7, 0x7e, 0xd2 }}
161
162 #define VTOY_BLOCK_DEVICE_PATH_NAME L"ventoy"
163
164 #if defined (MDE_CPU_IA32)
165 #define VENTOY_UEFI_DESC L"IA32 UEFI"
166 #elif defined (MDE_CPU_X64)
167 #define VENTOY_UEFI_DESC L"X64 UEFI"
168 #elif defined (MDE_CPU_EBC)
169 #elif defined (MDE_CPU_ARM)
170 #define VENTOY_UEFI_DESC L"ARM UEFI"
171 #elif defined (MDE_CPU_AARCH64)
172 #define VENTOY_UEFI_DESC L"ARM64 UEFI"
173 #else
174 #error Unknown Processor Type
175 #endif
176
177 typedef struct ventoy_sector_flag
178 {
179 UINT8 flag; // 0:init 1:mem 2:remap
180 UINT64 remap_lba;
181 }ventoy_sector_flag;
182
183
184 typedef struct vtoy_block_data
185 {
186 EFI_HANDLE Handle;
187 EFI_BLOCK_IO_MEDIA Media; /* Media descriptor */
188 EFI_BLOCK_IO_PROTOCOL BlockIo; /* Block I/O protocol */
189
190 UINTN DevicePathCompareLen;
191 EFI_DEVICE_PATH_PROTOCOL *Path; /* Device path protocol */
192
193 EFI_HANDLE RawBlockIoHandle;
194 EFI_BLOCK_IO_PROTOCOL *pRawBlockIo;
195 EFI_DEVICE_PATH_PROTOCOL *pDiskDevPath;
196
197 /* ventoy disk part2 ESP */
198 EFI_HANDLE DiskFsHandle;
199 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pDiskFs;
200 EFI_DEVICE_PATH_PROTOCOL *pDiskFsDevPath;
201
202 EFI_HANDLE IsoDriverImage;
203 }vtoy_block_data;
204
205 #define ISO9660_EFI_DRIVER_PATH L"\\ventoy\\iso9660_x64.efi"
206
207 #define debug(expr, ...) if (gDebugPrint) VtoyDebug("[VTOY] "expr"\r\n", ##__VA_ARGS__)
208 #define trace(expr, ...) VtoyDebug("[VTOY] "expr"\r\n", ##__VA_ARGS__)
209 #define sleep(sec) gBS->Stall(1000000 * (sec))
210
211 #define ventoy_debug_pause() \
212 if (gDebugPrint) \
213 { \
214 UINTN __Index = 0; \
215 gST->ConOut->OutputString(gST->ConOut, L"[VTOY] ###### Press Enter to continue... ######\r\n");\
216 gST->ConIn->Reset(gST->ConIn, FALSE); \
217 gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &__Index);\
218 }
219
220 typedef const char * (*grub_env_get_pf)(const char *name);
221
222 #pragma pack(1)
223
224 #define GRUB_FILE_REPLACE_MAGIC 0x1258BEEF
225
226 typedef struct ventoy_efi_file_replace
227 {
228 UINT64 BlockIoSectorStart;
229
230 UINT64 CurPos;
231 UINT64 FileSizeBytes;
232
233 EFI_FILE_PROTOCOL WrapperHandle;
234 }ventoy_efi_file_replace;
235
236 typedef struct ventoy_grub_param_file_replace
237 {
238 UINT32 magic;
239 char old_file_name[4][256];
240 UINT32 old_file_cnt;
241 UINT32 new_file_virtual_id;
242 }ventoy_grub_param_file_replace;
243
244 typedef struct ventoy_grub_param
245 {
246 grub_env_get_pf grub_env_get;
247
248 ventoy_grub_param_file_replace file_replace;
249 }ventoy_grub_param;
250
251 typedef struct ventoy_ram_disk
252 {
253 UINT64 PhyAddr;
254 UINT64 DiskSize;
255 }ventoy_ram_disk;
256
257 #pragma pack()
258
259
260 typedef struct well_known_guid
261 {
262 EFI_GUID *guid;
263 const char *name;
264 }well_known_guid;
265
266 typedef struct ventoy_system_wrapper
267 {
268 EFI_LOCATE_PROTOCOL NewLocateProtocol;
269 EFI_LOCATE_PROTOCOL OriLocateProtocol;
270
271 EFI_HANDLE_PROTOCOL NewHandleProtocol;
272 EFI_HANDLE_PROTOCOL OriHandleProtocol;
273
274 EFI_OPEN_PROTOCOL NewOpenProtocol;
275 EFI_OPEN_PROTOCOL OriOpenProtocol;
276 } ventoy_system_wrapper;
277
278 #define ventoy_wrapper(bs, wrapper, func, newfunc) \
279 {\
280 wrapper.Ori##func = bs->func;\
281 wrapper.New##func = newfunc;\
282 bs->func = wrapper.New##func;\
283 }
284
285 extern ventoy_efi_file_replace g_efi_file_replace;
286 extern BOOLEAN gDebugPrint;
287 VOID EFIAPI VtoyDebug(IN CONST CHAR8 *Format, ...);
288 EFI_STATUS EFIAPI ventoy_wrapper_system(VOID);
289 EFI_STATUS EFIAPI ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL *File);
290 EFI_STATUS EFIAPI ventoy_block_io_read
291 (
292 IN EFI_BLOCK_IO_PROTOCOL *This,
293 IN UINT32 MediaId,
294 IN EFI_LBA Lba,
295 IN UINTN BufferSize,
296 OUT VOID *Buffer
297 );
298
299 #endif
300