]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.h
1.1.07 release
[Ventoy.git] / EDK2 / edk2_mod / 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 typedef enum ventoy_chain_type
29 {
30 ventoy_chain_linux = 0, /* 0: linux */
31 ventoy_chain_windows, /* 1: windows */
32 ventoy_chain_wim, /* 2: wim */
33
34 ventoy_chain_max
35 }ventoy_chain_type;
36
37 #pragma pack(1)
38
39 typedef struct ventoy_guid
40 {
41 UINT32 data1;
42 UINT16 data2;
43 UINT16 data3;
44 UINT8 data4[8];
45 }ventoy_guid;
46
47 typedef struct ventoy_image_disk_region
48 {
49 UINT32 image_sector_count; /* image sectors contained in this region */
50 UINT32 image_start_sector; /* image sector start */
51 UINT64 disk_start_sector; /* disk sector start */
52 }ventoy_image_disk_region;
53
54 typedef struct ventoy_image_location
55 {
56 ventoy_guid guid;
57
58 /* image sector size, currently this value is always 2048 */
59 UINT32 image_sector_size;
60
61 /* disk sector size, normally the value is 512 */
62 UINT32 disk_sector_size;
63
64 UINT32 region_count;
65
66 /*
67 * disk region data
68 * If the image file has more than one fragments in disk,
69 * there will be more than one region data here.
70 *
71 */
72 ventoy_image_disk_region regions[1];
73
74 /* ventoy_image_disk_region regions[2~region_count-1] */
75 }ventoy_image_location;
76
77 typedef struct ventoy_os_param
78 {
79 ventoy_guid guid; // VENTOY_GUID
80 UINT8 chksum; // checksum
81
82 UINT8 vtoy_disk_guid[16];
83 UINT64 vtoy_disk_size; // disk size in bytes
84 UINT16 vtoy_disk_part_id; // begin with 1
85 UINT16 vtoy_disk_part_type; // 0:exfat 1:ntfs other: reserved
86 char vtoy_img_path[384]; // It seems to be enough, utf-8 format
87 UINT64 vtoy_img_size; // image file size in bytes
88
89 /*
90 * Ventoy will write a copy of ventoy_image_location data into runtime memory
91 * this is the physically address and length of that memory.
92 * Address 0 means no such data exist.
93 * Address will be aligned by 4KB.
94 *
95 */
96 UINT64 vtoy_img_location_addr;
97 UINT32 vtoy_img_location_len;
98
99 UINT64 vtoy_reserved[4]; // Internal use by ventoy
100
101 UINT8 vtoy_disk_signature[4];
102
103 UINT8 reserved[27];
104 }ventoy_os_param;
105
106 #pragma pack()
107
108 // compile assert to check that size of ventoy_os_param must be 512
109 COMPILE_ASSERT(sizeof(ventoy_os_param) == 512);
110
111
112
113 #pragma pack(4)
114
115 typedef struct ventoy_chain_head
116 {
117 ventoy_os_param os_param;
118
119 UINT32 disk_drive;
120 UINT32 drive_map;
121 UINT32 disk_sector_size;
122
123 UINT64 real_img_size_in_bytes;
124 UINT64 virt_img_size_in_bytes;
125 UINT32 boot_catalog;
126 UINT8 boot_catalog_sector[2048];
127
128 UINT32 img_chunk_offset;
129 UINT32 img_chunk_num;
130
131 UINT32 override_chunk_offset;
132 UINT32 override_chunk_num;
133
134 UINT32 virt_chunk_offset;
135 UINT32 virt_chunk_num;
136 }ventoy_chain_head;
137
138
139 typedef struct ventoy_img_chunk
140 {
141 UINT32 img_start_sector; //2KB
142 UINT32 img_end_sector;
143
144 UINT64 disk_start_sector; // in disk_sector_size
145 UINT64 disk_end_sector;
146 }ventoy_img_chunk;
147
148
149 typedef struct ventoy_override_chunk
150 {
151 UINT64 img_offset;
152 UINT32 override_size;
153 UINT8 override_data[512];
154 }ventoy_override_chunk;
155
156 typedef struct ventoy_virt_chunk
157 {
158 UINT32 mem_sector_start;
159 UINT32 mem_sector_end;
160 UINT32 mem_sector_offset;
161 UINT32 remap_sector_start;
162 UINT32 remap_sector_end;
163 UINT32 org_sector_start;
164 }ventoy_virt_chunk;
165
166
167 #pragma pack()
168
169
170 #define VTOY_BLOCK_DEVICE_PATH_GUID \
171 { 0x37b87ac6, 0xc180, 0x4583, { 0xa7, 0x05, 0x41, 0x4d, 0xa8, 0xf7, 0x7e, 0xd2 }}
172
173
174
175 #if defined (MDE_CPU_IA32)
176 #define VENTOY_UEFI_DESC L"IA32 UEFI"
177 #define ISO9660_EFI_DRIVER_PATH L"\\ventoy\\iso9660_ia32.efi"
178 #define UDF_EFI_DRIVER_PATH L"\\ventoy\\udf_ia32.efi"
179 #elif defined (MDE_CPU_X64)
180 #define VENTOY_UEFI_DESC L"X64 UEFI"
181 #define ISO9660_EFI_DRIVER_PATH L"\\ventoy\\iso9660_x64.efi"
182 #define UDF_EFI_DRIVER_PATH L"\\ventoy\\udf_x64.efi"
183 #elif defined (MDE_CPU_EBC)
184 #elif defined (MDE_CPU_ARM)
185 #define VENTOY_UEFI_DESC L"ARM UEFI"
186 #define ISO9660_EFI_DRIVER_PATH L"\\ventoy\\iso9660_arm.efi"
187 #define UDF_EFI_DRIVER_PATH L"\\ventoy\\udf_arm.efi"
188 #elif defined (MDE_CPU_AARCH64)
189 #define VENTOY_UEFI_DESC L"ARM64 UEFI"
190 #define ISO9660_EFI_DRIVER_PATH L"\\ventoy\\iso9660_aa64.efi"
191 #define UDF_EFI_DRIVER_PATH L"\\ventoy\\udf_aa64.efi"
192 #else
193 #error Unknown Processor Type
194 #endif
195
196 #define VENTOY_DEVICE_WARN 1
197 #define VTOY_WARNING L"!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!"
198
199 typedef struct ventoy_sector_flag
200 {
201 UINT8 flag; // 0:init 1:mem 2:remap
202 UINT64 remap_lba;
203 }ventoy_sector_flag;
204
205
206 typedef struct vtoy_block_data
207 {
208 EFI_HANDLE Handle;
209 EFI_BLOCK_IO_MEDIA Media; /* Media descriptor */
210 EFI_BLOCK_IO_PROTOCOL BlockIo; /* Block I/O protocol */
211
212 UINTN DevicePathCompareLen;
213 EFI_DEVICE_PATH_PROTOCOL *Path; /* Device path protocol */
214
215 EFI_HANDLE RawBlockIoHandle;
216 EFI_BLOCK_IO_PROTOCOL *pRawBlockIo;
217 EFI_DEVICE_PATH_PROTOCOL *pDiskDevPath;
218
219 /* ventoy disk part2 ESP */
220 EFI_HANDLE DiskFsHandle;
221 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pDiskFs;
222 EFI_DEVICE_PATH_PROTOCOL *pDiskFsDevPath;
223
224 EFI_HANDLE IsoDriverImage;
225 }vtoy_block_data;
226
227
228 #define debug(expr, ...) if (gDebugPrint) VtoyDebug("[VTOY] "expr"\r\n", ##__VA_ARGS__)
229 #define trace(expr, ...) VtoyDebug("[VTOY] "expr"\r\n", ##__VA_ARGS__)
230 #define sleep(sec) gBS->Stall(1000000 * (sec))
231
232 #define ventoy_debug_pause() \
233 if (gDebugPrint) \
234 { \
235 UINTN __Index = 0; \
236 gST->ConOut->OutputString(gST->ConOut, L"[VTOY] ###### Press Enter to continue... ######\r\n");\
237 gST->ConIn->Reset(gST->ConIn, FALSE); \
238 gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &__Index);\
239 }
240
241 typedef int (*grub_env_set_pf)(const char *name, const char *val);
242 typedef const char * (*grub_env_get_pf)(const char *name);
243 typedef int (*grub_env_printf_pf)(const char *fmt, ...);
244
245 #pragma pack(1)
246
247 #define VTOY_MAX_CONF_REPLACE 2
248 #define GRUB_FILE_REPLACE_MAGIC 0x1258BEEF
249 #define GRUB_IMG_REPLACE_MAGIC 0x1259BEEF
250
251 typedef struct ventoy_efi_file_replace
252 {
253 UINT64 BlockIoSectorStart;
254
255 UINT64 CurPos;
256 UINT64 FileSizeBytes;
257
258 EFI_FILE_PROTOCOL WrapperHandle;
259 }ventoy_efi_file_replace;
260
261 typedef struct ventoy_grub_param_file_replace
262 {
263 UINT32 magic;
264 char old_file_name[4][256];
265 UINT32 old_file_cnt;
266 UINT32 new_file_virtual_id;
267 }ventoy_grub_param_file_replace;
268
269 typedef struct ventoy_grub_param
270 {
271 grub_env_get_pf grub_env_get;
272 grub_env_set_pf grub_env_set;
273 ventoy_grub_param_file_replace file_replace;
274 ventoy_grub_param_file_replace img_replace[VTOY_MAX_CONF_REPLACE];
275 grub_env_printf_pf grub_env_printf;
276 }ventoy_grub_param;
277
278 typedef struct ventoy_ram_disk
279 {
280 UINT64 PhyAddr;
281 UINT64 DiskSize;
282 }ventoy_ram_disk;
283
284 typedef struct ventoy_iso9660_override
285 {
286 UINT32 first_sector;
287 UINT32 first_sector_be;
288 UINT32 size;
289 UINT32 size_be;
290 }ventoy_iso9660_override;
291
292 typedef struct PART_TABLE
293 {
294 UINT8 Active; // 0x00 0x80
295
296 UINT8 StartHead;
297 UINT16 StartSector : 6;
298 UINT16 StartCylinder : 10;
299
300 UINT8 FsFlag;
301
302 UINT8 EndHead;
303 UINT16 EndSector : 6;
304 UINT16 EndCylinder : 10;
305
306 UINT32 StartSectorId;
307 UINT32 SectorCount;
308 }PART_TABLE;
309
310 typedef struct MBR_HEAD
311 {
312 UINT8 BootCode[446];
313 PART_TABLE PartTbl[4];
314 UINT8 Byte55;
315 UINT8 ByteAA;
316 }MBR_HEAD;
317
318 #pragma pack()
319
320
321 typedef struct well_known_guid
322 {
323 EFI_GUID *guid;
324 const char *name;
325 }well_known_guid;
326
327 typedef struct ventoy_system_wrapper
328 {
329 EFI_LOCATE_PROTOCOL NewLocateProtocol;
330 EFI_LOCATE_PROTOCOL OriLocateProtocol;
331
332 EFI_HANDLE_PROTOCOL NewHandleProtocol;
333 EFI_HANDLE_PROTOCOL OriHandleProtocol;
334
335 EFI_OPEN_PROTOCOL NewOpenProtocol;
336 EFI_OPEN_PROTOCOL OriOpenProtocol;
337
338 EFI_LOCATE_HANDLE_BUFFER NewLocateHandleBuffer;
339 EFI_LOCATE_HANDLE_BUFFER OriLocateHandleBuffer;
340
341 EFI_PROTOCOLS_PER_HANDLE NewProtocolsPerHandle;
342 EFI_PROTOCOLS_PER_HANDLE OriProtocolsPerHandle;
343
344 EFI_LOCATE_HANDLE NewLocateHandle;
345 EFI_LOCATE_HANDLE OriLocateHandle;
346
347 EFI_LOCATE_DEVICE_PATH NewLocateDevicePath;
348 EFI_LOCATE_DEVICE_PATH OriLocateDevicePath;
349 } ventoy_system_wrapper;
350
351
352 #define MAX_DRIVER_BIND_WRAPPER 64
353 typedef struct DriverBindWrapper
354 {
355 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
356 EFI_DRIVER_BINDING_SUPPORTED pfOldSupport;
357 }DRIVER_BIND_WRAPPER;
358
359 #define ventoy_wrapper(bs, wrapper, func, newfunc) \
360 {\
361 wrapper.Ori##func = bs->func;\
362 wrapper.New##func = newfunc;\
363 bs->func = wrapper.New##func;\
364 }
365
366
367 #define VENTOY_GET_COMPONENT_NAME(Protocol, DriverName) \
368 {\
369 DriverName = NULL;\
370 Status = Protocol->GetDriverName(Protocol, "en", &DriverName);\
371 if (EFI_ERROR(Status) || NULL == DriverName) \
372 {\
373 DriverName = NULL;\
374 Status = Protocol->GetDriverName(Protocol, "eng", &DriverName);\
375 if (EFI_ERROR(Status) || NULL == DriverName) \
376 {\
377 continue;\
378 }\
379 }\
380 }
381
382 extern BOOLEAN gDebugPrint;
383 VOID EFIAPI VtoyDebug(IN CONST CHAR8 *Format, ...);
384 EFI_STATUS EFIAPI ventoy_wrapper_system(VOID);
385 EFI_STATUS EFIAPI ventoy_block_io_read
386 (
387 IN EFI_BLOCK_IO_PROTOCOL *This,
388 IN UINT32 MediaId,
389 IN EFI_LBA Lba,
390 IN UINTN BufferSize,
391 OUT VOID *Buffer
392 );
393
394
395 extern ventoy_chain_head *g_chain;
396 extern ventoy_img_chunk *g_chunk;
397 extern UINT32 g_img_chunk_num;
398 extern ventoy_override_chunk *g_override_chunk;
399 extern UINT32 g_override_chunk_num;
400 extern ventoy_virt_chunk *g_virt_chunk;
401 extern UINT32 g_virt_chunk_num;
402 extern vtoy_block_data gBlockData;
403 extern ventoy_efi_file_replace g_efi_file_replace;
404 extern ventoy_efi_file_replace g_img_file_replace[VTOY_MAX_CONF_REPLACE];
405 extern ventoy_sector_flag *g_sector_flag;
406 extern UINT32 g_sector_flag_num;
407 extern BOOLEAN gMemdiskMode;
408 extern BOOLEAN gSector512Mode;
409 extern UINTN g_iso_buf_size;
410 extern UINT8 *g_iso_data_buf;
411 extern ventoy_grub_param_file_replace *g_file_replace_list;
412 extern ventoy_grub_param_file_replace *g_img_replace_list;
413 extern BOOLEAN g_fixup_iso9660_secover_enable;
414 extern EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *g_con_simple_input_ex;
415 extern BOOLEAN g_fix_windows_1st_cdrom_issue;
416
417 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
418 (
419 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
420 OUT EFI_FILE_PROTOCOL **Root
421 );
422 EFI_STATUS EFIAPI ventoy_install_blockio(IN EFI_HANDLE ImageHandle, IN UINT64 ImgSize);
423 EFI_STATUS EFIAPI ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume);
424 EFI_STATUS ventoy_hook_keyboard_start(VOID);
425 EFI_STATUS ventoy_hook_keyboard_stop(VOID);
426 BOOLEAN ventoy_is_cdrom_dp_exist(VOID);
427 EFI_STATUS ventoy_hook_1st_cdrom_start(VOID);
428 EFI_STATUS ventoy_hook_1st_cdrom_stop(VOID);
429 EFI_STATUS ventoy_disable_ex_filesystem(VOID);
430 EFI_STATUS ventoy_enable_ex_filesystem(VOID);
431
432 #endif
433