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/>.
24 #define COMPILE_ASSERT(expr) extern char __compile_assert[(expr) ? 1 : -1]
26 #define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}
28 typedef enum ventoy_chain_type
30 ventoy_chain_linux
= 0, /* 0: linux */
31 ventoy_chain_windows
, /* 1: windows */
32 ventoy_chain_wim
, /* 2: wim */
39 typedef struct ventoy_guid
47 typedef struct ventoy_image_disk_region
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
;
54 typedef struct ventoy_image_location
58 /* image sector size, currently this value is always 2048 */
59 UINT32 image_sector_size
;
61 /* disk sector size, normally the value is 512 */
62 UINT32 disk_sector_size
;
68 * If the image file has more than one fragments in disk,
69 * there will be more than one region data here.
72 ventoy_image_disk_region regions
[1];
74 /* ventoy_image_disk_region regions[2~region_count-1] */
75 }ventoy_image_location
;
77 typedef struct ventoy_os_param
79 ventoy_guid guid
; // VENTOY_GUID
80 UINT8 chksum
; // checksum
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
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.
96 UINT64 vtoy_img_location_addr
;
97 UINT32 vtoy_img_location_len
;
99 UINT64 vtoy_reserved
[4]; // Internal use by ventoy
106 // compile assert to check that size of ventoy_os_param must be 512
107 COMPILE_ASSERT(sizeof(ventoy_os_param
) == 512);
113 typedef struct ventoy_chain_head
115 ventoy_os_param os_param
;
119 UINT32 disk_sector_size
;
121 UINT64 real_img_size_in_bytes
;
122 UINT64 virt_img_size_in_bytes
;
124 UINT8 boot_catalog_sector
[2048];
126 UINT32 img_chunk_offset
;
127 UINT32 img_chunk_num
;
129 UINT32 override_chunk_offset
;
130 UINT32 override_chunk_num
;
132 UINT32 virt_chunk_offset
;
133 UINT32 virt_chunk_num
;
137 typedef struct ventoy_img_chunk
139 UINT32 img_start_sector
; //2KB
140 UINT32 img_end_sector
;
142 UINT64 disk_start_sector
; // in disk_sector_size
143 UINT64 disk_end_sector
;
147 typedef struct ventoy_override_chunk
150 UINT32 override_size
;
151 UINT8 override_data
[512];
152 }ventoy_override_chunk
;
154 typedef struct ventoy_virt_chunk
156 UINT32 mem_sector_start
;
157 UINT32 mem_sector_end
;
158 UINT32 mem_sector_offset
;
159 UINT32 remap_sector_start
;
160 UINT32 remap_sector_end
;
161 UINT32 org_sector_start
;
168 #define VTOY_BLOCK_DEVICE_PATH_GUID \
169 { 0x37b87ac6, 0xc180, 0x4583, { 0xa7, 0x05, 0x41, 0x4d, 0xa8, 0xf7, 0x7e, 0xd2 }}
171 #define VTOY_BLOCK_DEVICE_PATH_NAME L"ventoy"
173 #if defined (MDE_CPU_IA32)
174 #define VENTOY_UEFI_DESC L"IA32 UEFI"
175 #elif defined (MDE_CPU_X64)
176 #define VENTOY_UEFI_DESC L"X64 UEFI"
177 #elif defined (MDE_CPU_EBC)
178 #elif defined (MDE_CPU_ARM)
179 #define VENTOY_UEFI_DESC L"ARM UEFI"
180 #elif defined (MDE_CPU_AARCH64)
181 #define VENTOY_UEFI_DESC L"ARM64 UEFI"
183 #error Unknown Processor Type
186 typedef struct ventoy_sector_flag
188 UINT8 flag
; // 0:init 1:mem 2:remap
193 typedef struct vtoy_block_data
196 EFI_BLOCK_IO_MEDIA Media
; /* Media descriptor */
197 EFI_BLOCK_IO_PROTOCOL BlockIo
; /* Block I/O protocol */
199 UINTN DevicePathCompareLen
;
200 EFI_DEVICE_PATH_PROTOCOL
*Path
; /* Device path protocol */
202 EFI_HANDLE RawBlockIoHandle
;
203 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
;
204 EFI_DEVICE_PATH_PROTOCOL
*pDiskDevPath
;
206 /* ventoy disk part2 ESP */
207 EFI_HANDLE DiskFsHandle
;
208 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pDiskFs
;
209 EFI_DEVICE_PATH_PROTOCOL
*pDiskFsDevPath
;
214 #define debug(expr, ...) if (gDebugPrint) VtoyDebug("[VTOY] "expr"\r\n", ##__VA_ARGS__)
215 #define trace(expr, ...) VtoyDebug("[VTOY] "expr"\r\n", ##__VA_ARGS__)
216 #define sleep(sec) gBS->Stall(1000000 * (sec))
218 #define ventoy_debug_pause() \
222 gST->ConOut->OutputString(gST->ConOut, L"[VTOY] ###### Press Enter to continue... ######\r\n");\
223 gST->ConIn->Reset(gST->ConIn, FALSE); \
224 gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &__Index);\
227 typedef const char * (*grub_env_get_pf
)(const char *name
);
228 typedef int (*grub_env_printf_pf
)(const char *fmt
, ...);
232 #define GRUB_FILE_REPLACE_MAGIC 0x1258BEEF
234 typedef struct ventoy_efi_file_replace
236 UINT64 BlockIoSectorStart
;
239 UINT64 FileSizeBytes
;
241 EFI_FILE_PROTOCOL WrapperHandle
;
242 }ventoy_efi_file_replace
;
244 typedef struct ventoy_grub_param_file_replace
247 char old_file_name
[4][256];
249 UINT32 new_file_virtual_id
;
250 }ventoy_grub_param_file_replace
;
252 typedef struct ventoy_grub_param
254 grub_env_get_pf grub_env_get
;
255 ventoy_grub_param_file_replace file_replace
;
256 grub_env_printf_pf grub_env_printf
;
259 typedef struct ventoy_ram_disk
265 typedef struct ventoy_iso9660_override
268 UINT32 first_sector_be
;
271 }ventoy_iso9660_override
;
276 typedef struct well_known_guid
282 typedef struct ventoy_system_wrapper
284 EFI_LOCATE_PROTOCOL NewLocateProtocol
;
285 EFI_LOCATE_PROTOCOL OriLocateProtocol
;
287 EFI_HANDLE_PROTOCOL NewHandleProtocol
;
288 EFI_HANDLE_PROTOCOL OriHandleProtocol
;
290 EFI_OPEN_PROTOCOL NewOpenProtocol
;
291 EFI_OPEN_PROTOCOL OriOpenProtocol
;
293 EFI_LOCATE_HANDLE_BUFFER NewLocateHandleBuffer
;
294 EFI_LOCATE_HANDLE_BUFFER OriLocateHandleBuffer
;
296 EFI_PROTOCOLS_PER_HANDLE NewProtocolsPerHandle
;
297 EFI_PROTOCOLS_PER_HANDLE OriProtocolsPerHandle
;
299 EFI_LOCATE_HANDLE NewLocateHandle
;
300 EFI_LOCATE_HANDLE OriLocateHandle
;
302 EFI_LOCATE_DEVICE_PATH NewLocateDevicePath
;
303 EFI_LOCATE_DEVICE_PATH OriLocateDevicePath
;
304 } ventoy_system_wrapper
;
306 #define ventoy_wrapper(bs, wrapper, func, newfunc) \
308 wrapper.Ori##func = bs->func;\
309 wrapper.New##func = newfunc;\
310 bs->func = wrapper.New##func;\
313 extern BOOLEAN gDebugPrint
;
314 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...);
315 EFI_STATUS EFIAPI
ventoy_wrapper_system(VOID
);
316 EFI_STATUS EFIAPI ventoy_block_io_read
318 IN EFI_BLOCK_IO_PROTOCOL
*This
,
326 extern ventoy_chain_head
*g_chain
;
327 extern ventoy_img_chunk
*g_chunk
;
328 extern UINT32 g_img_chunk_num
;
329 extern ventoy_override_chunk
*g_override_chunk
;
330 extern UINT32 g_override_chunk_num
;
331 extern ventoy_virt_chunk
*g_virt_chunk
;
332 extern UINT32 g_virt_chunk_num
;
333 extern vtoy_block_data gBlockData
;
334 extern ventoy_efi_file_replace g_efi_file_replace
;
335 extern ventoy_sector_flag
*g_sector_flag
;
336 extern UINT32 g_sector_flag_num
;
337 extern BOOLEAN gMemdiskMode
;
338 extern UINTN g_iso_buf_size
;
339 extern ventoy_grub_param_file_replace
*g_file_replace_list
;
340 extern BOOLEAN g_fixup_iso9660_secover_enable
;
341 extern EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*g_con_simple_input_ex
;
342 extern BOOLEAN g_fix_windows_1st_cdrom_issue
;
344 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
346 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
347 OUT EFI_FILE_PROTOCOL
**Root
349 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
);
350 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
);
351 EFI_STATUS
ventoy_hook_keyboard_start(VOID
);
352 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
);
353 BOOLEAN
ventoy_is_cdrom_dp_exist(VOID
);
354 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
);
355 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
);