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/>.
22 #include <Library/DebugLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DevicePathLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiApplicationEntryPoint.h>
31 #include <Protocol/LoadedImage.h>
32 #include <Guid/FileInfo.h>
33 #include <Guid/FileSystemInfo.h>
34 #include <Protocol/BlockIo.h>
35 #include <Protocol/RamDisk.h>
36 #include <Protocol/SimpleFileSystem.h>
37 #include <Protocol/DriverBinding.h>
40 #define ASSIGN_REPLACE(This, replace) \
41 replace = (This->FlushEx == ventoy_wrapper_file_flush_ex) ? &g_efi_file_replace : &g_img_file_replace
43 UINT8
*g_iso_data_buf
= NULL
;
44 UINTN g_iso_buf_size
= 0;
45 BOOLEAN gMemdiskMode
= FALSE
;
46 BOOLEAN gSector512Mode
= FALSE
;
48 ventoy_sector_flag
*g_sector_flag
= NULL
;
49 UINT32 g_sector_flag_num
= 0;
51 EFI_FILE_OPEN g_original_fopen
= NULL
;
52 EFI_FILE_CLOSE g_original_fclose
= NULL
;
53 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME g_original_open_volume
= NULL
;
55 /* EFI block device vendor device path GUID */
56 EFI_GUID gVtoyBlockDevicePathGuid
= VTOY_BLOCK_DEVICE_PATH_GUID
;
58 #define VENTOY_ISO9660_SECTOR_OVERFLOW 2097152
60 BOOLEAN g_fixup_iso9660_secover_enable
= FALSE
;
61 BOOLEAN g_fixup_iso9660_secover_start
= FALSE
;
62 UINT64 g_fixup_iso9660_secover_1st_secs
= 0;
63 UINT64 g_fixup_iso9660_secover_cur_secs
= 0;
64 UINT64 g_fixup_iso9660_secover_tot_secs
= 0;
66 STATIC UINTN g_keyboard_hook_count
= 0;
67 STATIC BOOLEAN g_blockio_start_record_bcd
= FALSE
;
68 STATIC BOOLEAN g_blockio_bcd_read_done
= FALSE
;
70 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*g_con_simple_input_ex
= NULL
;
71 STATIC EFI_INPUT_READ_KEY_EX g_org_read_key_ex
= NULL
;
72 STATIC EFI_INPUT_READ_KEY g_org_read_key
= NULL
;
74 STATIC EFI_LOCATE_HANDLE g_org_locate_handle
= NULL
;
76 STATIC UINT8 g_sector_buf
[2048];
77 STATIC EFI_BLOCK_READ g_sector_2048_read
= NULL
;
78 STATIC EFI_BLOCK_WRITE g_sector_2048_write
= NULL
;
80 STATIC UINTN g_DriverBindWrapperCnt
= 0;
81 STATIC DRIVER_BIND_WRAPPER g_DriverBindWrapperList
[MAX_DRIVER_BIND_WRAPPER
];
83 BOOLEAN
ventoy_is_cdrom_dp_exist(VOID
)
87 EFI_HANDLE
*Handles
= NULL
;
88 EFI_STATUS Status
= EFI_SUCCESS
;
89 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
91 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiDevicePathProtocolGuid
,
92 NULL
, &Count
, &Handles
);
93 if (EFI_ERROR(Status
))
98 for (i
= 0; i
< Count
; i
++)
100 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
101 if (EFI_ERROR(Status
))
106 while (!IsDevicePathEnd(DevicePath
))
108 if (MEDIA_DEVICE_PATH
== DevicePath
->Type
&& MEDIA_CDROM_DP
== DevicePath
->SubType
)
114 DevicePath
= NextDevicePathNode(DevicePath
);
123 /* Block IO procotol */
126 EFI_STATUS EFIAPI ventoy_block_io_reset
128 IN EFI_BLOCK_IO_PROTOCOL
*This
,
129 IN BOOLEAN ExtendedVerification
133 (VOID
)ExtendedVerification
;
137 STATIC EFI_STATUS EFIAPI ventoy_read_iso_sector
144 EFI_STATUS Status
= EFI_SUCCESS
;
149 UINT64 ReadStart
= 0;
151 UINT64 OverrideStart
= 0;
152 UINT64 OverrideEnd
= 0;
153 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
154 ventoy_img_chunk
*pchunk
= g_chunk
;
155 ventoy_override_chunk
*pOverride
= g_override_chunk
;
156 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
158 debug("read iso sector %lu count %u Buffer:%p Align:%u blk:%u",
159 Sector
, Count
, Buffer
, pRawBlockIo
->Media
->IoAlign
, pRawBlockIo
->Media
->BlockSize
);
161 ReadStart
= Sector
* 2048;
162 ReadEnd
= (Sector
+ Count
) * 2048;
164 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
166 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
168 if (g_chain
->disk_sector_size
== 512)
170 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
172 else if (g_chain
->disk_sector_size
== 1024)
174 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
176 else if (g_chain
->disk_sector_size
== 2048)
178 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
180 else if (g_chain
->disk_sector_size
== 4096)
182 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
185 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
186 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
188 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
189 MapLba
, secRead
* 2048, pCurBuf
);
190 if (EFI_ERROR(Status
))
192 debug("Raw disk read block failed %r LBA:%lu Count:%u %p", Status
, MapLba
, secRead
, pCurBuf
);
198 pCurBuf
+= secRead
* 2048;
202 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
208 pCurBuf
= (UINT8
*)Buffer
;
209 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
211 OverrideStart
= pOverride
->img_offset
;
212 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
214 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
219 if (ReadStart
<= OverrideStart
)
221 if (ReadEnd
<= OverrideEnd
)
223 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
227 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
232 if (ReadEnd
<= OverrideEnd
)
234 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
238 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
242 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
243 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
245 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
246 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
248 g_fixup_iso9660_secover_start
= TRUE
;
249 g_fixup_iso9660_secover_cur_secs
= 0;
254 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
256 if (*(UINT32
*)Buffer
== 0x66676572)
258 g_blockio_bcd_read_done
= TRUE
;
265 STATIC EFI_STATUS EFIAPI ventoy_write_iso_sector
272 EFI_STATUS Status
= EFI_SUCCESS
;
277 UINT64 ReadStart
= 0;
279 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
280 ventoy_img_chunk
*pchunk
= g_chunk
;
281 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
283 debug("write iso sector %lu count %u", Sector
, Count
);
285 ReadStart
= Sector
* 2048;
286 ReadEnd
= (Sector
+ Count
) * 2048;
288 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
290 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
292 if (g_chain
->disk_sector_size
== 512)
294 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
296 else if (g_chain
->disk_sector_size
== 1024)
298 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
300 else if (g_chain
->disk_sector_size
== 2048)
302 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
304 else if (g_chain
->disk_sector_size
== 4096)
306 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
310 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
311 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
313 Status
= pRawBlockIo
->WriteBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
314 MapLba
, secRead
* 2048, pCurBuf
);
315 if (EFI_ERROR(Status
))
317 debug("Raw disk write block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
323 pCurBuf
+= secRead
* 2048;
330 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_write
332 IN EFI_BLOCK_IO_PROTOCOL
*This
,
347 return EFI_WRITE_PROTECTED
;
350 CopyMem(g_iso_data_buf
+ (Lba
* 2048), Buffer
, BufferSize
);
355 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
357 IN EFI_BLOCK_IO_PROTOCOL
*This
,
364 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
369 CopyMem(Buffer
, g_iso_data_buf
+ (Lba
* 2048), BufferSize
);
371 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
373 if (*(UINT32
*)Buffer
== 0x66676572)
375 g_blockio_bcd_read_done
= TRUE
;
382 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
386 if (g_fixup_iso9660_secover_cur_secs
> 0)
388 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
389 g_fixup_iso9660_secover_cur_secs
+= secNum
;
390 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
392 g_fixup_iso9660_secover_start
= FALSE
;
398 ventoy_iso9660_override
*dirent
;
399 ventoy_override_chunk
*pOverride
;
401 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
403 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
404 if (Lba
== dirent
->first_sector
)
406 g_fixup_iso9660_secover_start
= FALSE
;
411 if (g_fixup_iso9660_secover_start
)
413 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
415 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
416 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
418 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
419 g_fixup_iso9660_secover_cur_secs
= secNum
;
420 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
422 g_fixup_iso9660_secover_start
= FALSE
;
424 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
435 EFI_STATUS EFIAPI ventoy_block_io_read_real
437 IN EFI_BLOCK_IO_PROTOCOL
*This
,
454 ventoy_sector_flag
*cur_flag
;
455 ventoy_virt_chunk
*node
;
457 debug("### block_io_read_real sector:%u count:%u Buffer:%p", (UINT32
)Lba
, (UINT32
)BufferSize
/ 2048, Buffer
);
459 secNum
= BufferSize
/ 2048;
461 /* Workaround for SSTR PE loader error */
462 if (g_fixup_iso9660_secover_start
)
464 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
469 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
471 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
473 else if (offset
< g_chain
->real_img_size_in_bytes
)
475 TmpNum
= (g_chain
->real_img_size_in_bytes
- offset
) / 2048;
476 ventoy_read_iso_sector(Lba
, TmpNum
, Buffer
);
480 Buffer
= (UINT8
*)Buffer
+ (g_chain
->real_img_size_in_bytes
- offset
);
484 VirtSec
= g_chain
->virt_img_size_in_bytes
/ 2048;
489 else if (Lba
+ secNum
> VirtSec
)
491 secNum
= VirtSec
- Lba
;
494 debug("XXX block_io_read_real sector:%u count:%u Buffer:%p", (UINT32
)Lba
, (UINT32
)BufferSize
/ 2048, Buffer
);
496 if (secNum
> g_sector_flag_num
)
498 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
499 if (NULL
== cur_flag
)
501 return EFI_OUT_OF_RESOURCES
;
504 FreePool(g_sector_flag
);
505 g_sector_flag
= cur_flag
;
506 g_sector_flag_num
= secNum
;
509 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
512 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
514 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
516 CopyMem((UINT8
*)Buffer
+ j
* 2048,
517 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
522 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
524 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
531 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
533 if (cur_flag
->flag
== 2)
537 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
538 lastlba
= cur_flag
->remap_lba
;
541 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
547 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
548 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
549 lastlba
= cur_flag
->remap_lba
;
557 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
563 EFI_STATUS EFIAPI ventoy_block_io_read
565 IN EFI_BLOCK_IO_PROTOCOL
*This
,
574 EFI_STATUS Status
= EFI_OUT_OF_RESOURCES
;
576 if (gBlockData
.pRawBlockIo
&& gBlockData
.pRawBlockIo
->Media
)
578 IoAlign
= gBlockData
.pRawBlockIo
->Media
->IoAlign
;
581 if ((IoAlign
== 0) || (((UINTN
) Buffer
& (IoAlign
- 1)) == 0))
583 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, Buffer
);
587 NewBuf
= AllocatePages(EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
590 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, NewBuf
);
591 CopyMem(Buffer
, NewBuf
, BufferSize
);
592 FreePages(NewBuf
, EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
599 EFI_STATUS EFIAPI ventoy_block_io_write
601 IN EFI_BLOCK_IO_PROTOCOL
*This
,
616 return EFI_WRITE_PROTECTED
;
619 secNum
= BufferSize
/ 2048;
622 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
625 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
631 STATIC UINTN
ventoy_get_current_device_path_id(VOID
)
637 BOOLEAN Find
= FALSE
;
638 EFI_HANDLE
*Handles
= NULL
;
639 EFI_STATUS Status
= EFI_SUCCESS
;
640 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
641 VENDOR_DEVICE_PATH
*venPath
= NULL
;
643 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiDevicePathProtocolGuid
,
644 NULL
, &Count
, &Handles
);
645 if (EFI_ERROR(Status
))
650 for (i
= 0; i
< Count
; i
++)
652 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
653 if (EFI_ERROR(Status
))
658 if (DevicePath
->Type
== HARDWARE_DEVICE_PATH
&& DevicePath
->SubType
== HW_VENDOR_DP
)
660 venPath
= (VENDOR_DEVICE_PATH
*)DevicePath
;
661 if (CompareGuid(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
))
663 CurId
= StrDecimalToUintn((CHAR16
*)(venPath
+ 1) + StrLen(L
"ventoy_"));
664 MaxId
= MAX(MaxId
, CurId
);
672 return Find
? (MaxId
+ 1) : 0;
675 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
677 UINTN CurVtoyDpId
= 0;
679 UINT8 TmpBuf
[128] = {0};
680 VENDOR_DEVICE_PATH
*venPath
= NULL
;
681 CHAR16 VtoyDpName
[32];
683 CurVtoyDpId
= ventoy_get_current_device_path_id();
684 UnicodeSPrintAsciiFormat(VtoyDpName
, sizeof(VtoyDpName
), "ventoy_%03lu", CurVtoyDpId
);
686 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
687 NameLen
= StrSize(VtoyDpName
);
688 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
689 venPath
->Header
.SubType
= HW_VENDOR_DP
;
690 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
691 venPath
->Header
.Length
[1] = 0;
692 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
693 CopyMem(venPath
+ 1, VtoyDpName
, NameLen
);
695 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
696 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
698 debug("gBlockData.Path=<%lu><%s>\n", CurVtoyDpId
, ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
703 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
707 CHAR16
*DriverName
= NULL
;
708 EFI_HANDLE
*Handles
= NULL
;
709 EFI_HANDLE DrvHandles
[2] = { NULL
};
710 EFI_STATUS Status
= EFI_SUCCESS
;
711 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
712 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
714 debug("ventoy_connect_driver <%s>...", DrvName
);
716 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
717 NULL
, &Count
, &Handles
);
718 if (EFI_ERROR(Status
))
723 for (i
= 0; i
< Count
; i
++)
725 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
726 if (EFI_ERROR(Status
))
731 VENTOY_GET_COMPONENT_NAME(Name2Protocol
, DriverName
);
733 if (StrStr(DriverName
, DrvName
))
735 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
736 DrvHandles
[0] = Handles
[i
];
743 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
744 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
748 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
754 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
755 NULL
, &Count
, &Handles
);
756 if (EFI_ERROR(Status
))
761 for (i
= 0; i
< Count
; i
++)
763 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
764 if (EFI_ERROR(Status
))
769 VENTOY_GET_COMPONENT_NAME(NameProtocol
, DriverName
);
771 if (StrStr(DriverName
, DrvName
))
773 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
774 DrvHandles
[0] = Handles
[i
];
781 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
782 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
786 Status
= EFI_NOT_FOUND
;
795 STATIC BOOLEAN
ventoy_filesystem_need_wrapper(IN CONST CHAR16
*DrvName
)
798 CHAR16 UpperDrvName
[256];
800 StrCpyS(UpperDrvName
, 256, DrvName
);
802 for (i
= 0; i
< 256 && UpperDrvName
[i
]; i
++)
804 if (UpperDrvName
[i
] >= 'a' && UpperDrvName
[i
] <= 'z')
806 UpperDrvName
[i
] = 'A' + (UpperDrvName
[i
] - 'a');
811 * suppress some file system drivers
812 * 1. rEFInd File System Driver
816 if (StrStr(UpperDrvName
, L
"REFIND") && StrStr(UpperDrvName
, L
"FILE SYSTEM"))
824 STATIC VOID ventoy_add_filesystem_wrapper
826 IN EFI_DRIVER_BINDING_PROTOCOL
*DriverBindProtocol
,
827 IN CONST CHAR16
*DriverName
832 if (g_DriverBindWrapperCnt
>= MAX_DRIVER_BIND_WRAPPER
)
834 debug("driver binding wrapper overflow %lu", g_DriverBindWrapperCnt
);
838 if (!ventoy_filesystem_need_wrapper(DriverName
))
843 for (j
= 0; j
< g_DriverBindWrapperCnt
; j
++)
845 if (g_DriverBindWrapperList
[j
].DriverBinding
== DriverBindProtocol
)
847 debug("Duplicate driverbinding <%s> %p %lu %lu", DriverName
, DriverBindProtocol
, j
, g_DriverBindWrapperCnt
);
852 if (j
>= g_DriverBindWrapperCnt
)
854 g_DriverBindWrapperList
[g_DriverBindWrapperCnt
].DriverBinding
= DriverBindProtocol
;
855 g_DriverBindWrapperList
[g_DriverBindWrapperCnt
].pfOldSupport
= DriverBindProtocol
->Supported
;
856 g_DriverBindWrapperCnt
++;
857 debug("Add driverbinding <%s> %p %lu", DriverName
, DriverBindProtocol
, g_DriverBindWrapperCnt
);
861 STATIC EFI_STATUS
ventoy_find_filesystem_driverbind(VOID
)
865 CHAR16
*DriverName
= NULL
;
866 EFI_HANDLE
*Handles
= NULL
;
867 EFI_STATUS Status
= EFI_SUCCESS
;
868 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
869 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
870 EFI_DRIVER_BINDING_PROTOCOL
*DriverBindProtocol
= NULL
;
872 debug("ventoy_find_filesystem_driverbind...");
874 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
875 NULL
, &Count
, &Handles
);
876 if (EFI_ERROR(Status
))
881 for (i
= 0; i
< Count
; i
++)
883 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
884 if (EFI_ERROR(Status
))
889 VENTOY_GET_COMPONENT_NAME(Name2Protocol
, DriverName
);
891 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDriverBindingProtocolGuid
, (VOID
**)&DriverBindProtocol
);
892 if (EFI_ERROR(Status
))
894 debug("### 2 No DriverBind <%s> <%r>", DriverName
, Status
);
898 ventoy_add_filesystem_wrapper(DriverBindProtocol
, DriverName
);
905 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
906 NULL
, &Count
, &Handles
);
907 if (EFI_ERROR(Status
))
912 for (i
= 0; i
< Count
; i
++)
914 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
915 if (EFI_ERROR(Status
))
920 VENTOY_GET_COMPONENT_NAME(NameProtocol
, DriverName
);
922 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDriverBindingProtocolGuid
, (VOID
**)&DriverBindProtocol
);
923 if (EFI_ERROR(Status
))
925 debug("### 1 No DriverBind <%s> <%r>", DriverName
, Status
);
929 ventoy_add_filesystem_wrapper(DriverBindProtocol
, DriverName
);
937 STATIC EFI_STATUS EFIAPI ventoy_wrapper_driver_bind_support
939 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
940 IN EFI_HANDLE ControllerHandle
,
941 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
945 EFI_STATUS Status
= EFI_SUCCESS
;
946 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
947 EFI_DRIVER_BINDING_SUPPORTED pfOldSupport
= NULL
;
949 for (i
= 0; i
< g_DriverBindWrapperCnt
; i
++)
951 if (g_DriverBindWrapperList
[i
].DriverBinding
== This
)
953 pfOldSupport
= g_DriverBindWrapperList
[i
].pfOldSupport
;
958 debug("ventoy_wrapper_driver_bind_support %lu %p", i
, pfOldSupport
);
962 return EFI_UNSUPPORTED
;
965 Status
= gBS
->HandleProtocol(ControllerHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
966 if (EFI_ERROR(Status
))
971 if (0 == CompareMem(gBlockData
.Path
, DevicePath
, gBlockData
.DevicePathCompareLen
))
973 debug("return EFI_UNSUPPORTED for ventoy");
974 return EFI_UNSUPPORTED
;
978 return pfOldSupport(This
, ControllerHandle
, RemainingDevicePath
);
981 EFI_STATUS
ventoy_disable_ex_filesystem(VOID
)
985 ventoy_find_filesystem_driverbind();
987 for (i
= 0; i
< g_DriverBindWrapperCnt
; i
++)
989 g_DriverBindWrapperList
[i
].DriverBinding
->Supported
= ventoy_wrapper_driver_bind_support
;
992 debug("Wrapper Ex Driver Binding %lu", g_DriverBindWrapperCnt
);
993 ventoy_debug_pause();
998 EFI_STATUS
ventoy_enable_ex_filesystem(VOID
)
1002 for (i
= 0; i
< g_DriverBindWrapperCnt
; i
++)
1004 g_DriverBindWrapperList
[i
].DriverBinding
->Supported
= g_DriverBindWrapperList
[i
].pfOldSupport
;
1006 g_DriverBindWrapperCnt
= 0;
1011 EFI_STATUS EFIAPI ventoy_block_io_read_512
1013 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1016 IN UINTN BufferSize
,
1022 UINT8
*CurBuf
= NULL
;
1023 EFI_STATUS Status
= EFI_SUCCESS
;
1025 debug("ventoy_block_io_read_512 %lu %lu Buffer:%p\n", Lba
, BufferSize
/ 512, Buffer
);
1027 CurBuf
= (UINT8
*)Buffer
;
1032 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1034 if (BufferSize
<= (4 - Mod
) * 512)
1036 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
1041 ReadSize
= (4 - Mod
) * 512;
1042 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
1045 BufferSize
-= ReadSize
;
1049 if (BufferSize
>= 2048)
1051 ReadSize
= BufferSize
/ 2048 * 2048;
1053 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
1056 Lba
+= ReadSize
/ 512;
1057 BufferSize
-= ReadSize
;
1062 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1063 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
1069 EFI_STATUS EFIAPI ventoy_block_io_write_512
1071 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1074 IN UINTN BufferSize
,
1080 UINT8
*CurBuf
= NULL
;
1081 EFI_STATUS Status
= EFI_SUCCESS
;
1083 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
1085 CurBuf
= (UINT8
*)Buffer
;
1090 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1092 if (BufferSize
<= (4 - Mod
) * 512)
1094 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
1095 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1099 ReadSize
= (4 - Mod
) * 512;
1100 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
1101 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1105 BufferSize
-= ReadSize
;
1109 if (BufferSize
>= 2048)
1111 ReadSize
= BufferSize
/ 2048 * 2048;
1113 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
1116 Lba
+= ReadSize
/ 512;
1117 BufferSize
-= ReadSize
;
1122 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1124 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
1125 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1131 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
1133 EFI_STATUS Status
= EFI_SUCCESS
;
1134 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
1136 ventoy_fill_device_path();
1138 debug("install block io protocol %p", ImageHandle
);
1139 ventoy_debug_pause();
1143 gBlockData
.Media
.BlockSize
= 512;
1144 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
1145 gBlockData
.Media
.ReadOnly
= FALSE
;
1149 gBlockData
.Media
.BlockSize
= 2048;
1150 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
1151 gBlockData
.Media
.ReadOnly
= TRUE
;
1154 gBlockData
.Media
.MediaPresent
= 1;
1155 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
1157 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
1158 pBlockIo
->Media
= &(gBlockData
.Media
);
1159 pBlockIo
->Reset
= ventoy_block_io_reset
;
1163 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
1164 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
1165 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
1166 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
1170 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
1171 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
1174 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
1176 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
1177 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1178 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1180 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
1181 if (EFI_ERROR(Status
))
1186 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
1187 debug("Connect disk IO driver %r", Status
);
1189 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
1190 debug("Connect partition driver %r", Status
);
1191 if (EFI_ERROR(Status
))
1193 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
1194 debug("Connect all controller %r", Status
);
1197 ventoy_debug_pause();
1203 /* For file replace */
1206 STATIC EFI_STATUS EFIAPI
1207 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
1217 STATIC EFI_STATUS EFIAPI
1218 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
1220 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
1223 STATIC EFI_STATUS EFIAPI
1224 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
1230 STATIC EFI_STATUS EFIAPI
1231 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
1236 STATIC EFI_STATUS EFIAPI
1237 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
1244 STATIC EFI_STATUS EFIAPI
1245 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
1253 STATIC EFI_STATUS EFIAPI
1254 ventoy_wrapper_file_flush_ex_img(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
1262 STATIC EFI_STATUS EFIAPI
1263 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1269 return EFI_WRITE_PROTECTED
;
1272 STATIC EFI_STATUS EFIAPI
1273 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1275 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
1279 STATIC EFI_STATUS EFIAPI
1280 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
1286 STATIC EFI_STATUS EFIAPI
1287 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
1289 ventoy_efi_file_replace
*replace
= NULL
;
1291 ASSIGN_REPLACE(This
, replace
);
1293 if (Position
<= replace
->FileSizeBytes
)
1295 replace
->CurPos
= Position
;
1299 replace
->CurPos
= replace
->FileSizeBytes
;
1305 STATIC EFI_STATUS EFIAPI
1306 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
1308 ventoy_efi_file_replace
*replace
= NULL
;
1310 ASSIGN_REPLACE(This
, replace
);
1312 *Position
= replace
->CurPos
;
1318 STATIC EFI_STATUS EFIAPI
1319 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
1321 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
1322 ventoy_efi_file_replace
*replace
= NULL
;
1324 ASSIGN_REPLACE(This
, replace
);
1326 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
1328 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
1330 return EFI_INVALID_PARAMETER
;
1336 return EFI_BUFFER_TOO_SMALL
;
1339 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
1341 Info
->Size
= sizeof(EFI_FILE_INFO
);
1342 Info
->FileSize
= replace
->FileSizeBytes
;
1343 Info
->PhysicalSize
= replace
->FileSizeBytes
;
1344 Info
->Attribute
= EFI_FILE_READ_ONLY
;
1345 //Info->FileName = EFI_FILE_READ_ONLY;
1352 STATIC EFI_STATUS EFIAPI
1353 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1356 UINTN ReadLen
= *Len
;
1357 ventoy_efi_file_replace
*replace
= NULL
;
1359 ASSIGN_REPLACE(This
, replace
);
1361 debug("ventoy_wrapper_file_read ... %u", *Len
);
1363 if (replace
->CurPos
+ ReadLen
> replace
->FileSizeBytes
)
1365 ReadLen
= replace
->FileSizeBytes
- replace
->CurPos
;
1368 Lba
= replace
->CurPos
/ 2048 + replace
->BlockIoSectorStart
;
1370 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1374 replace
->CurPos
+= ReadLen
;
1379 STATIC EFI_STATUS EFIAPI
1380 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1382 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1385 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
, BOOLEAN Img
)
1387 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1388 File
->Open
= ventoy_wrapper_fs_open
;
1389 File
->Close
= ventoy_wrapper_file_close
;
1390 File
->Delete
= ventoy_wrapper_file_delete
;
1391 File
->Read
= ventoy_wrapper_file_read
;
1392 File
->Write
= ventoy_wrapper_file_write
;
1393 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1394 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1395 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1396 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1397 File
->Flush
= ventoy_wrapper_file_flush
;
1398 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1399 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1400 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1401 File
->FlushEx
= Img
? ventoy_wrapper_file_flush_ex_img
: ventoy_wrapper_file_flush_ex
;
1406 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1408 EFI_FILE_HANDLE This
,
1409 EFI_FILE_HANDLE
*New
,
1418 EFI_STATUS Status
= EFI_SUCCESS
;
1421 ventoy_virt_chunk
*virt
= NULL
;
1423 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1425 if ((Mode
& EFI_FILE_MODE_WRITE
) > 0 && StrCmp(Name
, L
"\\loader\\random-seed") == 0)
1429 debug("## ventoy_wrapper_file_open return NOT_FOUND for random-seed %lx", Mode
);
1432 return EFI_NOT_FOUND
;
1435 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1436 if (EFI_ERROR(Status
))
1442 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1443 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1445 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1446 for (j
= 0; j
< 4; j
++)
1448 if (0 == AsciiStriCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1450 g_original_fclose(*New
);
1451 *New
= &g_efi_file_replace
.WrapperHandle
;
1452 ventoy_wrapper_file_procotol(*New
, FALSE
);
1454 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1456 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1458 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1459 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1463 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1464 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1472 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1474 (*New
)->Open
= ventoy_wrapper_file_open
;
1480 if (g_img_replace_list
&& g_img_replace_list
->magic
== GRUB_IMG_REPLACE_MAGIC
&&
1481 g_img_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1483 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1484 for (j
= 0; j
< g_img_replace_list
->old_file_cnt
; j
++)
1486 AsciiStrCpyS(OldName
, sizeof(OldName
), g_img_replace_list
[i
].old_file_name
[j
]);
1487 if ((0 == AsciiStrCmp(OldName
, TmpName
)) ||
1488 (AsciiStrnCmp(OldName
, "\\loader\\entries\\", 16) == 0 &&
1489 AsciiStrCmp(OldName
+ 16, TmpName
) == 0
1493 g_original_fclose(*New
);
1494 *New
= &g_img_file_replace
.WrapperHandle
;
1495 ventoy_wrapper_file_procotol(*New
, TRUE
);
1497 virt
= g_virt_chunk
+ g_img_replace_list
->new_file_virtual_id
;
1499 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1501 g_img_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1502 g_img_file_replace
.FileSizeBytes
= Sectors
* 2048;
1506 debug("## ventoy_wrapper_file_open2 <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1507 g_img_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1515 if (StrCmp(Name
, L
"\\loader\\entries") == 0)
1517 (*New
)->Open
= ventoy_wrapper_file_open
;
1524 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1526 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1527 OUT EFI_FILE_PROTOCOL
**Root
1530 EFI_STATUS Status
= EFI_SUCCESS
;
1532 Status
= g_original_open_volume(This
, Root
);
1533 if (!EFI_ERROR(Status
))
1535 g_original_fopen
= (*Root
)->Open
;
1536 g_original_fclose
= (*Root
)->Close
;
1537 (*Root
)->Open
= ventoy_wrapper_file_open
;
1543 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1545 g_original_open_volume
= OpenVolume
;
1550 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1553 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1555 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1556 OUT EFI_KEY_DATA
*KeyData
1559 /* only hook once before BCD file read */
1560 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1562 g_keyboard_hook_count
++;
1564 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1565 KeyData
->Key
.UnicodeChar
= 0;
1566 KeyData
->KeyState
.KeyShiftState
= 0;
1567 KeyData
->KeyState
.KeyToggleState
= 0;
1572 return g_org_read_key_ex(This
, KeyData
);
1575 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1577 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1578 OUT EFI_INPUT_KEY
*Key
1581 /* only hook once before BCD file read */
1582 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1584 g_keyboard_hook_count
++;
1586 Key
->ScanCode
= SCAN_DELETE
;
1587 Key
->UnicodeChar
= 0;
1591 return g_org_read_key(This
, Key
);
1594 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1596 g_blockio_start_record_bcd
= TRUE
;
1597 g_blockio_bcd_read_done
= FALSE
;
1598 g_keyboard_hook_count
= 0;
1600 if (g_con_simple_input_ex
)
1602 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1603 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1606 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1607 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1612 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1614 g_blockio_start_record_bcd
= FALSE
;
1615 g_blockio_bcd_read_done
= FALSE
;
1616 g_keyboard_hook_count
= 0;
1618 if (g_con_simple_input_ex
)
1620 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1623 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1629 /* Fixup the 1st cdrom influnce for Windows boot */
1632 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1634 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1635 IN EFI_GUID
*Protocol
, OPTIONAL
1636 IN VOID
*SearchKey
, OPTIONAL
1637 IN OUT UINTN
*BufferSize
,
1638 OUT EFI_HANDLE
*Buffer
1642 EFI_HANDLE Handle
= NULL
;
1643 EFI_STATUS Status
= EFI_SUCCESS
;
1645 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1647 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1649 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1651 if (Buffer
[i
] == gBlockData
.Handle
)
1654 Buffer
[0] = Buffer
[i
];
1664 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1666 g_org_locate_handle
= gBS
->LocateHandle
;
1667 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1672 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1674 gBS
->LocateHandle
= g_org_locate_handle
;
1675 g_org_locate_handle
= NULL
;