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", Sector
, Count
, Buffer
, pRawBlockIo
->Media
->IoAlign
);
160 ReadStart
= Sector
* 2048;
161 ReadEnd
= (Sector
+ Count
) * 2048;
163 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
165 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
167 if (g_chain
->disk_sector_size
== 512)
169 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
171 else if (g_chain
->disk_sector_size
== 1024)
173 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
175 else if (g_chain
->disk_sector_size
== 2048)
177 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
179 else if (g_chain
->disk_sector_size
== 4096)
181 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
184 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
185 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
187 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
188 MapLba
, secRead
* 2048, pCurBuf
);
189 if (EFI_ERROR(Status
))
191 debug("Raw disk read block failed %r LBA:%lu Count:%u %p", Status
, MapLba
, secRead
, pCurBuf
);
197 pCurBuf
+= secRead
* 2048;
201 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
207 pCurBuf
= (UINT8
*)Buffer
;
208 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
210 OverrideStart
= pOverride
->img_offset
;
211 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
213 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
218 if (ReadStart
<= OverrideStart
)
220 if (ReadEnd
<= OverrideEnd
)
222 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
226 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
231 if (ReadEnd
<= OverrideEnd
)
233 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
237 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
241 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
242 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
244 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
245 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
247 g_fixup_iso9660_secover_start
= TRUE
;
248 g_fixup_iso9660_secover_cur_secs
= 0;
253 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
255 if (*(UINT32
*)Buffer
== 0x66676572)
257 g_blockio_bcd_read_done
= TRUE
;
264 STATIC EFI_STATUS EFIAPI ventoy_write_iso_sector
271 EFI_STATUS Status
= EFI_SUCCESS
;
276 UINT64 ReadStart
= 0;
278 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
279 ventoy_img_chunk
*pchunk
= g_chunk
;
280 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
282 debug("write iso sector %lu count %u", Sector
, Count
);
284 ReadStart
= Sector
* 2048;
285 ReadEnd
= (Sector
+ Count
) * 2048;
287 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
289 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
291 if (g_chain
->disk_sector_size
== 512)
293 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
295 else if (g_chain
->disk_sector_size
== 1024)
297 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
299 else if (g_chain
->disk_sector_size
== 2048)
301 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
303 else if (g_chain
->disk_sector_size
== 4096)
305 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
309 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
310 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
312 Status
= pRawBlockIo
->WriteBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
313 MapLba
, secRead
* 2048, pCurBuf
);
314 if (EFI_ERROR(Status
))
316 debug("Raw disk write block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
322 pCurBuf
+= secRead
* 2048;
329 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_write
331 IN EFI_BLOCK_IO_PROTOCOL
*This
,
346 return EFI_WRITE_PROTECTED
;
349 CopyMem(g_iso_data_buf
+ (Lba
* 2048), Buffer
, BufferSize
);
354 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
356 IN EFI_BLOCK_IO_PROTOCOL
*This
,
363 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
368 CopyMem(Buffer
, g_iso_data_buf
+ (Lba
* 2048), BufferSize
);
370 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
372 if (*(UINT32
*)Buffer
== 0x66676572)
374 g_blockio_bcd_read_done
= TRUE
;
381 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
385 if (g_fixup_iso9660_secover_cur_secs
> 0)
387 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
388 g_fixup_iso9660_secover_cur_secs
+= secNum
;
389 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
391 g_fixup_iso9660_secover_start
= FALSE
;
397 ventoy_iso9660_override
*dirent
;
398 ventoy_override_chunk
*pOverride
;
400 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
402 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
403 if (Lba
== dirent
->first_sector
)
405 g_fixup_iso9660_secover_start
= FALSE
;
410 if (g_fixup_iso9660_secover_start
)
412 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
414 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
415 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
417 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
418 g_fixup_iso9660_secover_cur_secs
= secNum
;
419 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
421 g_fixup_iso9660_secover_start
= FALSE
;
423 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
434 EFI_STATUS EFIAPI ventoy_block_io_read_real
436 IN EFI_BLOCK_IO_PROTOCOL
*This
,
453 ventoy_sector_flag
*cur_flag
;
454 ventoy_virt_chunk
*node
;
456 debug("### block_io_read_real sector:%u count:%u Buffer:%p", (UINT32
)Lba
, (UINT32
)BufferSize
/ 2048, Buffer
);
458 secNum
= BufferSize
/ 2048;
460 /* Workaround for SSTR PE loader error */
461 if (g_fixup_iso9660_secover_start
)
463 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
468 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
470 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
472 else if (offset
< g_chain
->real_img_size_in_bytes
)
474 TmpNum
= (g_chain
->real_img_size_in_bytes
- offset
) / 2048;
475 ventoy_read_iso_sector(Lba
, TmpNum
, Buffer
);
479 Buffer
= (UINT8
*)Buffer
+ (g_chain
->real_img_size_in_bytes
- offset
);
483 VirtSec
= g_chain
->virt_img_size_in_bytes
/ 2048;
488 else if (Lba
+ secNum
> VirtSec
)
490 secNum
= VirtSec
- Lba
;
493 debug("XXX block_io_read_real sector:%u count:%u Buffer:%p", (UINT32
)Lba
, (UINT32
)BufferSize
/ 2048, Buffer
);
495 if (secNum
> g_sector_flag_num
)
497 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
498 if (NULL
== cur_flag
)
500 return EFI_OUT_OF_RESOURCES
;
503 FreePool(g_sector_flag
);
504 g_sector_flag
= cur_flag
;
505 g_sector_flag_num
= secNum
;
508 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
511 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
513 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
515 CopyMem((UINT8
*)Buffer
+ j
* 2048,
516 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
521 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
523 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
530 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
532 if (cur_flag
->flag
== 2)
536 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
537 lastlba
= cur_flag
->remap_lba
;
540 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
546 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
547 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
548 lastlba
= cur_flag
->remap_lba
;
556 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
562 EFI_STATUS EFIAPI ventoy_block_io_read
564 IN EFI_BLOCK_IO_PROTOCOL
*This
,
573 EFI_STATUS Status
= EFI_OUT_OF_RESOURCES
;
575 if (gBlockData
.pRawBlockIo
&& gBlockData
.pRawBlockIo
->Media
)
577 IoAlign
= gBlockData
.pRawBlockIo
->Media
->IoAlign
;
580 if ((IoAlign
== 0) || (((UINTN
) Buffer
& (IoAlign
- 1)) == 0))
582 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, Buffer
);
586 NewBuf
= AllocatePages(EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
589 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, NewBuf
);
590 CopyMem(Buffer
, NewBuf
, BufferSize
);
591 FreePages(NewBuf
, EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
598 EFI_STATUS EFIAPI ventoy_block_io_write
600 IN EFI_BLOCK_IO_PROTOCOL
*This
,
615 return EFI_WRITE_PROTECTED
;
618 secNum
= BufferSize
/ 2048;
621 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
624 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
630 STATIC UINTN
ventoy_get_current_device_path_id(VOID
)
636 BOOLEAN Find
= FALSE
;
637 EFI_HANDLE
*Handles
= NULL
;
638 EFI_STATUS Status
= EFI_SUCCESS
;
639 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
640 VENDOR_DEVICE_PATH
*venPath
= NULL
;
642 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiDevicePathProtocolGuid
,
643 NULL
, &Count
, &Handles
);
644 if (EFI_ERROR(Status
))
649 for (i
= 0; i
< Count
; i
++)
651 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
652 if (EFI_ERROR(Status
))
657 if (DevicePath
->Type
== HARDWARE_DEVICE_PATH
&& DevicePath
->SubType
== HW_VENDOR_DP
)
659 venPath
= (VENDOR_DEVICE_PATH
*)DevicePath
;
660 if (CompareGuid(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
))
662 CurId
= StrDecimalToUintn((CHAR16
*)(venPath
+ 1) + StrLen(L
"ventoy_"));
663 MaxId
= MAX(MaxId
, CurId
);
671 return Find
? (MaxId
+ 1) : 0;
674 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
676 UINTN CurVtoyDpId
= 0;
678 UINT8 TmpBuf
[128] = {0};
679 VENDOR_DEVICE_PATH
*venPath
= NULL
;
680 CHAR16 VtoyDpName
[32];
682 CurVtoyDpId
= ventoy_get_current_device_path_id();
683 UnicodeSPrintAsciiFormat(VtoyDpName
, sizeof(VtoyDpName
), "ventoy_%03lu", CurVtoyDpId
);
685 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
686 NameLen
= StrSize(VtoyDpName
);
687 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
688 venPath
->Header
.SubType
= HW_VENDOR_DP
;
689 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
690 venPath
->Header
.Length
[1] = 0;
691 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
692 CopyMem(venPath
+ 1, VtoyDpName
, NameLen
);
694 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
695 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
697 debug("gBlockData.Path=<%lu><%s>\n", CurVtoyDpId
, ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
702 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
706 CHAR16
*DriverName
= NULL
;
707 EFI_HANDLE
*Handles
= NULL
;
708 EFI_HANDLE DrvHandles
[2] = { NULL
};
709 EFI_STATUS Status
= EFI_SUCCESS
;
710 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
711 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
713 debug("ventoy_connect_driver <%s>...", DrvName
);
715 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
716 NULL
, &Count
, &Handles
);
717 if (EFI_ERROR(Status
))
722 for (i
= 0; i
< Count
; i
++)
724 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
725 if (EFI_ERROR(Status
))
730 VENTOY_GET_COMPONENT_NAME(Name2Protocol
, DriverName
);
732 if (StrStr(DriverName
, DrvName
))
734 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
735 DrvHandles
[0] = Handles
[i
];
742 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
743 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
747 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
753 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
754 NULL
, &Count
, &Handles
);
755 if (EFI_ERROR(Status
))
760 for (i
= 0; i
< Count
; i
++)
762 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
763 if (EFI_ERROR(Status
))
768 VENTOY_GET_COMPONENT_NAME(NameProtocol
, DriverName
);
770 if (StrStr(DriverName
, DrvName
))
772 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
773 DrvHandles
[0] = Handles
[i
];
780 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
781 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
785 Status
= EFI_NOT_FOUND
;
794 STATIC BOOLEAN
ventoy_filesystem_need_wrapper(IN CONST CHAR16
*DrvName
)
797 CHAR16 UpperDrvName
[256];
799 StrCpyS(UpperDrvName
, 256, DrvName
);
801 for (i
= 0; i
< 256 && UpperDrvName
[i
]; i
++)
803 if (UpperDrvName
[i
] >= 'a' && UpperDrvName
[i
] <= 'z')
805 UpperDrvName
[i
] = 'A' + (UpperDrvName
[i
] - 'a');
810 * suppress some file system drivers
811 * 1. rEFInd File System Driver
815 if (StrStr(UpperDrvName
, L
"REFIND") && StrStr(UpperDrvName
, L
"FILE SYSTEM"))
823 STATIC VOID ventoy_add_filesystem_wrapper
825 IN EFI_DRIVER_BINDING_PROTOCOL
*DriverBindProtocol
,
826 IN CONST CHAR16
*DriverName
831 if (g_DriverBindWrapperCnt
>= MAX_DRIVER_BIND_WRAPPER
)
833 debug("driver binding wrapper overflow %lu", g_DriverBindWrapperCnt
);
837 if (!ventoy_filesystem_need_wrapper(DriverName
))
842 for (j
= 0; j
< g_DriverBindWrapperCnt
; j
++)
844 if (g_DriverBindWrapperList
[j
].DriverBinding
== DriverBindProtocol
)
846 debug("Duplicate driverbinding <%s> %p %lu %lu", DriverName
, DriverBindProtocol
, j
, g_DriverBindWrapperCnt
);
851 if (j
>= g_DriverBindWrapperCnt
)
853 g_DriverBindWrapperList
[g_DriverBindWrapperCnt
].DriverBinding
= DriverBindProtocol
;
854 g_DriverBindWrapperList
[g_DriverBindWrapperCnt
].pfOldSupport
= DriverBindProtocol
->Supported
;
855 g_DriverBindWrapperCnt
++;
856 debug("Add driverbinding <%s> %p %lu", DriverName
, DriverBindProtocol
, g_DriverBindWrapperCnt
);
860 STATIC EFI_STATUS
ventoy_find_filesystem_driverbind(VOID
)
864 CHAR16
*DriverName
= NULL
;
865 EFI_HANDLE
*Handles
= NULL
;
866 EFI_STATUS Status
= EFI_SUCCESS
;
867 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
868 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
869 EFI_DRIVER_BINDING_PROTOCOL
*DriverBindProtocol
= NULL
;
871 debug("ventoy_find_filesystem_driverbind...");
873 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
874 NULL
, &Count
, &Handles
);
875 if (EFI_ERROR(Status
))
880 for (i
= 0; i
< Count
; i
++)
882 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
883 if (EFI_ERROR(Status
))
888 VENTOY_GET_COMPONENT_NAME(Name2Protocol
, DriverName
);
890 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDriverBindingProtocolGuid
, (VOID
**)&DriverBindProtocol
);
891 if (EFI_ERROR(Status
))
893 debug("### 2 No DriverBind <%s> <%r>", DriverName
, Status
);
897 ventoy_add_filesystem_wrapper(DriverBindProtocol
, DriverName
);
904 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
905 NULL
, &Count
, &Handles
);
906 if (EFI_ERROR(Status
))
911 for (i
= 0; i
< Count
; i
++)
913 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
914 if (EFI_ERROR(Status
))
919 VENTOY_GET_COMPONENT_NAME(NameProtocol
, DriverName
);
921 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDriverBindingProtocolGuid
, (VOID
**)&DriverBindProtocol
);
922 if (EFI_ERROR(Status
))
924 debug("### 1 No DriverBind <%s> <%r>", DriverName
, Status
);
928 ventoy_add_filesystem_wrapper(DriverBindProtocol
, DriverName
);
936 STATIC EFI_STATUS EFIAPI ventoy_wrapper_driver_bind_support
938 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
939 IN EFI_HANDLE ControllerHandle
,
940 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
944 EFI_STATUS Status
= EFI_SUCCESS
;
945 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
946 EFI_DRIVER_BINDING_SUPPORTED pfOldSupport
= NULL
;
948 for (i
= 0; i
< g_DriverBindWrapperCnt
; i
++)
950 if (g_DriverBindWrapperList
[i
].DriverBinding
== This
)
952 pfOldSupport
= g_DriverBindWrapperList
[i
].pfOldSupport
;
957 debug("ventoy_wrapper_driver_bind_support %lu %p", i
, pfOldSupport
);
961 return EFI_UNSUPPORTED
;
964 Status
= gBS
->HandleProtocol(ControllerHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
965 if (EFI_ERROR(Status
))
970 if (0 == CompareMem(gBlockData
.Path
, DevicePath
, gBlockData
.DevicePathCompareLen
))
972 debug("return EFI_UNSUPPORTED for ventoy");
973 return EFI_UNSUPPORTED
;
977 return pfOldSupport(This
, ControllerHandle
, RemainingDevicePath
);
980 EFI_STATUS
ventoy_disable_ex_filesystem(VOID
)
984 ventoy_find_filesystem_driverbind();
986 for (i
= 0; i
< g_DriverBindWrapperCnt
; i
++)
988 g_DriverBindWrapperList
[i
].DriverBinding
->Supported
= ventoy_wrapper_driver_bind_support
;
991 debug("Wrapper Ex Driver Binding %lu", g_DriverBindWrapperCnt
);
992 ventoy_debug_pause();
997 EFI_STATUS
ventoy_enable_ex_filesystem(VOID
)
1001 for (i
= 0; i
< g_DriverBindWrapperCnt
; i
++)
1003 g_DriverBindWrapperList
[i
].DriverBinding
->Supported
= g_DriverBindWrapperList
[i
].pfOldSupport
;
1005 g_DriverBindWrapperCnt
= 0;
1010 EFI_STATUS EFIAPI ventoy_block_io_read_512
1012 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1015 IN UINTN BufferSize
,
1021 UINT8
*CurBuf
= NULL
;
1022 EFI_STATUS Status
= EFI_SUCCESS
;
1024 debug("ventoy_block_io_read_512 %lu %lu Buffer:%p\n", Lba
, BufferSize
/ 512, Buffer
);
1026 CurBuf
= (UINT8
*)Buffer
;
1031 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1033 if (BufferSize
<= (4 - Mod
) * 512)
1035 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
1040 ReadSize
= (4 - Mod
) * 512;
1041 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
1044 BufferSize
-= ReadSize
;
1048 if (BufferSize
>= 2048)
1050 ReadSize
= BufferSize
/ 2048 * 2048;
1052 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
1055 Lba
+= ReadSize
/ 512;
1056 BufferSize
-= ReadSize
;
1061 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1062 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
1068 EFI_STATUS EFIAPI ventoy_block_io_write_512
1070 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1073 IN UINTN BufferSize
,
1079 UINT8
*CurBuf
= NULL
;
1080 EFI_STATUS Status
= EFI_SUCCESS
;
1082 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
1084 CurBuf
= (UINT8
*)Buffer
;
1089 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1091 if (BufferSize
<= (4 - Mod
) * 512)
1093 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
1094 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1098 ReadSize
= (4 - Mod
) * 512;
1099 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
1100 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1104 BufferSize
-= ReadSize
;
1108 if (BufferSize
>= 2048)
1110 ReadSize
= BufferSize
/ 2048 * 2048;
1112 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
1115 Lba
+= ReadSize
/ 512;
1116 BufferSize
-= ReadSize
;
1121 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1123 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
1124 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
1130 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
1132 EFI_STATUS Status
= EFI_SUCCESS
;
1133 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
1135 ventoy_fill_device_path();
1137 debug("install block io protocol %p", ImageHandle
);
1138 ventoy_debug_pause();
1142 gBlockData
.Media
.BlockSize
= 512;
1143 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
1144 gBlockData
.Media
.ReadOnly
= FALSE
;
1148 gBlockData
.Media
.BlockSize
= 2048;
1149 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
1150 gBlockData
.Media
.ReadOnly
= TRUE
;
1153 gBlockData
.Media
.MediaPresent
= 1;
1154 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
1156 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
1157 pBlockIo
->Media
= &(gBlockData
.Media
);
1158 pBlockIo
->Reset
= ventoy_block_io_reset
;
1162 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
1163 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
1164 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
1165 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
1169 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
1170 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
1173 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
1175 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
1176 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1177 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1179 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
1180 if (EFI_ERROR(Status
))
1185 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
1186 debug("Connect disk IO driver %r", Status
);
1188 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
1189 debug("Connect partition driver %r", Status
);
1190 if (EFI_ERROR(Status
))
1192 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
1193 debug("Connect all controller %r", Status
);
1196 ventoy_debug_pause();
1202 /* For file replace */
1205 STATIC EFI_STATUS EFIAPI
1206 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
1216 STATIC EFI_STATUS EFIAPI
1217 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
1219 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
1222 STATIC EFI_STATUS EFIAPI
1223 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
1229 STATIC EFI_STATUS EFIAPI
1230 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
1235 STATIC EFI_STATUS EFIAPI
1236 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
1243 STATIC EFI_STATUS EFIAPI
1244 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
1252 STATIC EFI_STATUS EFIAPI
1253 ventoy_wrapper_file_flush_ex_img(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
1261 STATIC EFI_STATUS EFIAPI
1262 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1268 return EFI_WRITE_PROTECTED
;
1271 STATIC EFI_STATUS EFIAPI
1272 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1274 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
1278 STATIC EFI_STATUS EFIAPI
1279 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
1285 STATIC EFI_STATUS EFIAPI
1286 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
1288 ventoy_efi_file_replace
*replace
= NULL
;
1290 ASSIGN_REPLACE(This
, replace
);
1292 if (Position
<= replace
->FileSizeBytes
)
1294 replace
->CurPos
= Position
;
1298 replace
->CurPos
= replace
->FileSizeBytes
;
1304 STATIC EFI_STATUS EFIAPI
1305 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
1307 ventoy_efi_file_replace
*replace
= NULL
;
1309 ASSIGN_REPLACE(This
, replace
);
1311 *Position
= replace
->CurPos
;
1317 STATIC EFI_STATUS EFIAPI
1318 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
1320 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
1321 ventoy_efi_file_replace
*replace
= NULL
;
1323 ASSIGN_REPLACE(This
, replace
);
1325 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
1327 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
1329 return EFI_INVALID_PARAMETER
;
1335 return EFI_BUFFER_TOO_SMALL
;
1338 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
1340 Info
->Size
= sizeof(EFI_FILE_INFO
);
1341 Info
->FileSize
= replace
->FileSizeBytes
;
1342 Info
->PhysicalSize
= replace
->FileSizeBytes
;
1343 Info
->Attribute
= EFI_FILE_READ_ONLY
;
1344 //Info->FileName = EFI_FILE_READ_ONLY;
1351 STATIC EFI_STATUS EFIAPI
1352 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1355 UINTN ReadLen
= *Len
;
1356 ventoy_efi_file_replace
*replace
= NULL
;
1358 ASSIGN_REPLACE(This
, replace
);
1360 debug("ventoy_wrapper_file_read ... %u", *Len
);
1362 if (replace
->CurPos
+ ReadLen
> replace
->FileSizeBytes
)
1364 ReadLen
= replace
->FileSizeBytes
- replace
->CurPos
;
1367 Lba
= replace
->CurPos
/ 2048 + replace
->BlockIoSectorStart
;
1369 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1373 replace
->CurPos
+= ReadLen
;
1378 STATIC EFI_STATUS EFIAPI
1379 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1381 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1384 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
, BOOLEAN Img
)
1386 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1387 File
->Open
= ventoy_wrapper_fs_open
;
1388 File
->Close
= ventoy_wrapper_file_close
;
1389 File
->Delete
= ventoy_wrapper_file_delete
;
1390 File
->Read
= ventoy_wrapper_file_read
;
1391 File
->Write
= ventoy_wrapper_file_write
;
1392 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1393 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1394 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1395 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1396 File
->Flush
= ventoy_wrapper_file_flush
;
1397 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1398 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1399 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1400 File
->FlushEx
= Img
? ventoy_wrapper_file_flush_ex_img
: ventoy_wrapper_file_flush_ex
;
1405 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1407 EFI_FILE_HANDLE This
,
1408 EFI_FILE_HANDLE
*New
,
1417 EFI_STATUS Status
= EFI_SUCCESS
;
1420 ventoy_virt_chunk
*virt
= NULL
;
1422 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1424 if ((Mode
& EFI_FILE_MODE_WRITE
) > 0 && StrCmp(Name
, L
"\\loader\\random-seed") == 0)
1428 debug("## ventoy_wrapper_file_open return NOT_FOUND for random-seed %lx", Mode
);
1431 return EFI_NOT_FOUND
;
1434 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1435 if (EFI_ERROR(Status
))
1441 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1442 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1444 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1445 for (j
= 0; j
< 4; j
++)
1447 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1449 g_original_fclose(*New
);
1450 *New
= &g_efi_file_replace
.WrapperHandle
;
1451 ventoy_wrapper_file_procotol(*New
, FALSE
);
1453 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1455 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1457 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1458 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1462 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1463 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1471 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1473 (*New
)->Open
= ventoy_wrapper_file_open
;
1479 if (g_img_replace_list
&& g_img_replace_list
->magic
== GRUB_IMG_REPLACE_MAGIC
&&
1480 g_img_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1482 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1483 for (j
= 0; j
< g_img_replace_list
->old_file_cnt
; j
++)
1485 AsciiStrCpyS(OldName
, sizeof(OldName
), g_img_replace_list
[i
].old_file_name
[j
]);
1486 if ((0 == AsciiStrCmp(OldName
, TmpName
)) ||
1487 (AsciiStrnCmp(OldName
, "\\loader\\entries\\", 16) == 0 &&
1488 AsciiStrCmp(OldName
+ 16, TmpName
) == 0
1492 g_original_fclose(*New
);
1493 *New
= &g_img_file_replace
.WrapperHandle
;
1494 ventoy_wrapper_file_procotol(*New
, TRUE
);
1496 virt
= g_virt_chunk
+ g_img_replace_list
->new_file_virtual_id
;
1498 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1500 g_img_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1501 g_img_file_replace
.FileSizeBytes
= Sectors
* 2048;
1505 debug("## ventoy_wrapper_file_open2 <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1506 g_img_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1514 if (StrCmp(Name
, L
"\\loader\\entries") == 0)
1516 (*New
)->Open
= ventoy_wrapper_file_open
;
1523 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1525 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1526 OUT EFI_FILE_PROTOCOL
**Root
1529 EFI_STATUS Status
= EFI_SUCCESS
;
1531 Status
= g_original_open_volume(This
, Root
);
1532 if (!EFI_ERROR(Status
))
1534 g_original_fopen
= (*Root
)->Open
;
1535 g_original_fclose
= (*Root
)->Close
;
1536 (*Root
)->Open
= ventoy_wrapper_file_open
;
1542 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1544 g_original_open_volume
= OpenVolume
;
1549 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1552 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1554 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1555 OUT EFI_KEY_DATA
*KeyData
1558 /* only hook once before BCD file read */
1559 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1561 g_keyboard_hook_count
++;
1563 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1564 KeyData
->Key
.UnicodeChar
= 0;
1565 KeyData
->KeyState
.KeyShiftState
= 0;
1566 KeyData
->KeyState
.KeyToggleState
= 0;
1571 return g_org_read_key_ex(This
, KeyData
);
1574 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1576 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1577 OUT EFI_INPUT_KEY
*Key
1580 /* only hook once before BCD file read */
1581 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1583 g_keyboard_hook_count
++;
1585 Key
->ScanCode
= SCAN_DELETE
;
1586 Key
->UnicodeChar
= 0;
1590 return g_org_read_key(This
, Key
);
1593 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1595 g_blockio_start_record_bcd
= TRUE
;
1596 g_blockio_bcd_read_done
= FALSE
;
1597 g_keyboard_hook_count
= 0;
1599 if (g_con_simple_input_ex
)
1601 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1602 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1605 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1606 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1611 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1613 g_blockio_start_record_bcd
= FALSE
;
1614 g_blockio_bcd_read_done
= FALSE
;
1615 g_keyboard_hook_count
= 0;
1617 if (g_con_simple_input_ex
)
1619 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1622 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1628 /* Fixup the 1st cdrom influnce for Windows boot */
1631 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1633 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1634 IN EFI_GUID
*Protocol
, OPTIONAL
1635 IN VOID
*SearchKey
, OPTIONAL
1636 IN OUT UINTN
*BufferSize
,
1637 OUT EFI_HANDLE
*Buffer
1641 EFI_HANDLE Handle
= NULL
;
1642 EFI_STATUS Status
= EFI_SUCCESS
;
1644 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1646 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1648 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1650 if (Buffer
[i
] == gBlockData
.Handle
)
1653 Buffer
[0] = Buffer
[i
];
1663 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1665 g_org_locate_handle
= gBS
->LocateHandle
;
1666 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1671 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1673 gBS
->LocateHandle
= g_org_locate_handle
;
1674 g_org_locate_handle
= NULL
;