+ if (!ventoy_filesystem_need_wrapper(DriverName))
+ {
+ return;
+ }
+
+ for (j = 0; j < g_DriverBindWrapperCnt; j++)
+ {
+ if (g_DriverBindWrapperList[j].DriverBinding == DriverBindProtocol)
+ {
+ debug("Duplicate driverbinding <%s> %p %lu %lu", DriverName, DriverBindProtocol, j, g_DriverBindWrapperCnt);
+ break;
+ }
+ }
+
+ if (j >= g_DriverBindWrapperCnt)
+ {
+ g_DriverBindWrapperList[g_DriverBindWrapperCnt].DriverBinding = DriverBindProtocol;
+ g_DriverBindWrapperList[g_DriverBindWrapperCnt].pfOldSupport = DriverBindProtocol->Supported;
+ g_DriverBindWrapperCnt++;
+ debug("Add driverbinding <%s> %p %lu", DriverName, DriverBindProtocol, g_DriverBindWrapperCnt);
+ }
+}
+
+STATIC EFI_STATUS ventoy_find_filesystem_driverbind(VOID)
+{
+ UINTN i = 0;
+ UINTN Count = 0;
+ CHAR16 *DriverName = NULL;
+ EFI_HANDLE *Handles = NULL;
+ EFI_STATUS Status = EFI_SUCCESS;
+ EFI_COMPONENT_NAME_PROTOCOL *NameProtocol = NULL;
+ EFI_COMPONENT_NAME2_PROTOCOL *Name2Protocol = NULL;
+ EFI_DRIVER_BINDING_PROTOCOL *DriverBindProtocol = NULL;
+
+ debug("ventoy_find_filesystem_driverbind...");
+
+ Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiComponentName2ProtocolGuid,
+ NULL, &Count, &Handles);
+ if (EFI_ERROR(Status))
+ {
+ return Status;
+ }
+
+ for (i = 0; i < Count; i++)
+ {
+ Status = gBS->HandleProtocol(Handles[i], &gEfiComponentName2ProtocolGuid, (VOID **)&Name2Protocol);
+ if (EFI_ERROR(Status))
+ {
+ continue;
+ }
+
+ VENTOY_GET_COMPONENT_NAME(Name2Protocol, DriverName);
+
+ Status = gBS->HandleProtocol(Handles[i], &gEfiDriverBindingProtocolGuid, (VOID **)&DriverBindProtocol);
+ if (EFI_ERROR(Status))
+ {
+ debug("### 2 No DriverBind <%s> <%r>", DriverName, Status);
+ continue;
+ }
+
+ ventoy_add_filesystem_wrapper(DriverBindProtocol, DriverName);
+ }
+
+ Count = 0;
+ FreePool(Handles);
+ Handles = NULL;
+
+ Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiComponentNameProtocolGuid,
+ NULL, &Count, &Handles);
+ if (EFI_ERROR(Status))
+ {
+ return Status;
+ }
+
+ for (i = 0; i < Count; i++)
+ {
+ Status = gBS->HandleProtocol(Handles[i], &gEfiComponentNameProtocolGuid, (VOID **)&NameProtocol);
+ if (EFI_ERROR(Status))
+ {
+ continue;
+ }
+
+ VENTOY_GET_COMPONENT_NAME(NameProtocol, DriverName);
+
+ Status = gBS->HandleProtocol(Handles[i], &gEfiDriverBindingProtocolGuid, (VOID **)&DriverBindProtocol);
+ if (EFI_ERROR(Status))
+ {
+ debug("### 1 No DriverBind <%s> <%r>", DriverName, Status);
+ continue;
+ }
+
+ ventoy_add_filesystem_wrapper(DriverBindProtocol, DriverName);
+ }
+
+ FreePool(Handles);
+
+ return EFI_SUCCESS;
+}
+
+STATIC EFI_STATUS EFIAPI ventoy_wrapper_driver_bind_support
+(
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,
+ IN EFI_HANDLE ControllerHandle,
+ IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
+)
+{
+ UINTN i;
+ EFI_STATUS Status = EFI_SUCCESS;
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath = NULL;
+ EFI_DRIVER_BINDING_SUPPORTED pfOldSupport = NULL;
+
+ for (i = 0; i < g_DriverBindWrapperCnt; i++)
+ {
+ if (g_DriverBindWrapperList[i].DriverBinding == This)
+ {
+ pfOldSupport = g_DriverBindWrapperList[i].pfOldSupport;
+ break;
+ }
+ }
+
+ debug("ventoy_wrapper_driver_bind_support %lu %p", i, pfOldSupport);
+
+ if (!pfOldSupport)
+ {
+ return EFI_UNSUPPORTED;
+ }
+
+ Status = gBS->HandleProtocol(ControllerHandle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePath);
+ if (EFI_ERROR(Status))
+ {
+ goto out;
+ }
+
+ if (0 == CompareMem(gBlockData.Path, DevicePath, gBlockData.DevicePathCompareLen))
+ {
+ debug("return EFI_UNSUPPORTED for ventoy");
+ return EFI_UNSUPPORTED;
+ }
+
+out:
+ return pfOldSupport(This, ControllerHandle, RemainingDevicePath);
+}
+
+EFI_STATUS ventoy_disable_ex_filesystem(VOID)
+{
+ UINTN i;
+
+ ventoy_find_filesystem_driverbind();
+
+ for (i = 0; i < g_DriverBindWrapperCnt; i++)
+ {
+ g_DriverBindWrapperList[i].DriverBinding->Supported = ventoy_wrapper_driver_bind_support;
+ }
+
+ debug("Wrapper Ex Driver Binding %lu", g_DriverBindWrapperCnt);
+ ventoy_debug_pause();
+
+ return EFI_SUCCESS;
+}
+
+EFI_STATUS ventoy_enable_ex_filesystem(VOID)
+{
+ UINTN i;
+
+ for (i = 0; i < g_DriverBindWrapperCnt; i++)
+ {
+ g_DriverBindWrapperList[i].DriverBinding->Supported = g_DriverBindWrapperList[i].pfOldSupport;
+ }
+ g_DriverBindWrapperCnt = 0;
+
+ return EFI_SUCCESS;
+}
+
+EFI_STATUS EFIAPI ventoy_block_io_read_512
+(
+ IN EFI_BLOCK_IO_PROTOCOL *This,
+ IN UINT32 MediaId,
+ IN EFI_LBA Lba,
+ IN UINTN BufferSize,
+ OUT VOID *Buffer
+)
+{
+ EFI_LBA Mod;
+ UINTN ReadSize;
+ UINT8 *CurBuf = NULL;
+ EFI_STATUS Status = EFI_SUCCESS;
+
+ debug("ventoy_block_io_read_512 %lu %lu Buffer:%p\n", Lba, BufferSize / 512, Buffer);
+
+ CurBuf = (UINT8 *)Buffer;
+
+ Mod = Lba % 4;
+ if (Mod > 0)
+ {
+ Status |= g_sector_2048_read(This, MediaId, Lba / 4, 2048, g_sector_buf);
+
+ if (BufferSize <= (4 - Mod) * 512)
+ {
+ CopyMem(CurBuf, g_sector_buf + Mod * 512, BufferSize);
+ return EFI_SUCCESS;
+ }
+ else
+ {
+ ReadSize = (4 - Mod) * 512;
+ CopyMem(CurBuf, g_sector_buf + Mod * 512, ReadSize);
+ CurBuf += ReadSize;
+ Lba += (4 - Mod);
+ BufferSize -= ReadSize;
+ }
+ }
+
+ if (BufferSize >= 2048)
+ {
+ ReadSize = BufferSize / 2048 * 2048;
+
+ Status |= g_sector_2048_read(This, MediaId, Lba / 4, ReadSize, CurBuf);
+ CurBuf += ReadSize;
+
+ Lba += ReadSize / 512;
+ BufferSize -= ReadSize;
+ }
+
+ if (BufferSize > 0)
+ {
+ Status |= g_sector_2048_read(This, MediaId, Lba / 4, 2048, g_sector_buf);
+ CopyMem(CurBuf, g_sector_buf, BufferSize);
+ }
+
+ return Status;
+}
+
+EFI_STATUS EFIAPI ventoy_block_io_write_512
+(
+ IN EFI_BLOCK_IO_PROTOCOL *This,
+ IN UINT32 MediaId,
+ IN EFI_LBA Lba,
+ IN UINTN BufferSize,
+ IN VOID *Buffer
+)
+{
+ EFI_LBA Mod;
+ UINTN ReadSize;
+ UINT8 *CurBuf = NULL;
+ EFI_STATUS Status = EFI_SUCCESS;
+
+ debug("ventoy_block_io_write_512 %lu %lu\n", Lba, BufferSize / 512);
+
+ CurBuf = (UINT8 *)Buffer;
+
+ Mod = Lba % 4;
+ if (Mod > 0)
+ {
+ Status |= g_sector_2048_read(This, MediaId, Lba / 4, 2048, g_sector_buf);
+
+ if (BufferSize <= (4 - Mod) * 512)
+ {
+ CopyMem(g_sector_buf + Mod * 512, CurBuf, BufferSize);
+ return g_sector_2048_write(This, MediaId, Lba / 4, 2048, g_sector_buf);
+ }
+ else
+ {
+ ReadSize = (4 - Mod) * 512;
+ CopyMem(g_sector_buf + Mod * 512, CurBuf, ReadSize);
+ g_sector_2048_write(This, MediaId, Lba / 4, 2048, g_sector_buf);
+
+ CurBuf += ReadSize;
+ Lba += (4 - Mod);
+ BufferSize -= ReadSize;
+ }
+ }
+
+ if (BufferSize >= 2048)
+ {
+ ReadSize = BufferSize / 2048 * 2048;
+
+ Status |= g_sector_2048_write(This, MediaId, Lba / 4, ReadSize, CurBuf);
+ CurBuf += ReadSize;
+
+ Lba += ReadSize / 512;
+ BufferSize -= ReadSize;
+ }
+
+ if (BufferSize > 0)
+ {
+ Status |= g_sector_2048_read(This, MediaId, Lba / 4, 2048, g_sector_buf);
+
+ CopyMem(g_sector_buf, CurBuf, BufferSize);
+ g_sector_2048_write(This, MediaId, Lba / 4, 2048, g_sector_buf);
+ }
+
+ return Status;
+}
+
+EFI_STATUS EFIAPI ventoy_install_blockio(IN EFI_HANDLE ImageHandle, IN UINT64 ImgSize)
+{
+ EFI_STATUS Status = EFI_SUCCESS;
+ EFI_BLOCK_IO_PROTOCOL *pBlockIo = &(gBlockData.BlockIo);
+
+ ventoy_fill_device_path();
+
+ debug("install block io protocol %p", ImageHandle);
+ ventoy_debug_pause();
+
+ if (gSector512Mode)
+ {
+ gBlockData.Media.BlockSize = 512;
+ gBlockData.Media.LastBlock = ImgSize / 512 - 1;
+ gBlockData.Media.ReadOnly = FALSE;
+ }
+ else
+ {
+ gBlockData.Media.BlockSize = 2048;
+ gBlockData.Media.LastBlock = ImgSize / 2048 - 1;
+ gBlockData.Media.ReadOnly = TRUE;
+ }
+
+ gBlockData.Media.MediaPresent = 1;
+ gBlockData.Media.LogicalBlocksPerPhysicalBlock = 1;
+
+ pBlockIo->Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
+ pBlockIo->Media = &(gBlockData.Media);
+ pBlockIo->Reset = ventoy_block_io_reset;
+
+ if (gSector512Mode)
+ {
+ g_sector_2048_read = gMemdiskMode ? ventoy_block_io_ramdisk_read : ventoy_block_io_read;
+ g_sector_2048_write = gMemdiskMode ? ventoy_block_io_ramdisk_write : ventoy_block_io_write;
+ pBlockIo->ReadBlocks = ventoy_block_io_read_512;
+ pBlockIo->WriteBlocks = ventoy_block_io_write_512;
+ }
+ else
+ {
+ pBlockIo->ReadBlocks = gMemdiskMode ? ventoy_block_io_ramdisk_read : ventoy_block_io_read;
+ pBlockIo->WriteBlocks = ventoy_block_io_write;
+ }
+
+ pBlockIo->FlushBlocks = ventoy_block_io_flush;
+
+ Status = gBS->InstallMultipleProtocolInterfaces(&gBlockData.Handle,
+ &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
+ &gEfiDevicePathProtocolGuid, gBlockData.Path,
+ NULL);
+ debug("Install protocol %r %p", Status, gBlockData.Handle);
+ if (EFI_ERROR(Status))
+ {
+ return Status;
+ }
+
+ Status = ventoy_connect_driver(gBlockData.Handle, L"Disk I/O Driver");
+ debug("Connect disk IO driver %r", Status);
+
+ Status = ventoy_connect_driver(gBlockData.Handle, L"Partition Driver");
+ debug("Connect partition driver %r", Status);
+ if (EFI_ERROR(Status))
+ {
+ Status = gBS->ConnectController(gBlockData.Handle, NULL, NULL, TRUE);
+ debug("Connect all controller %r", Status);
+ }
+
+ ventoy_debug_pause();
+
+ return EFI_SUCCESS;
+}
+
+#if 0
+/* For file replace */
+#endif
+
+STATIC EFI_STATUS EFIAPI
+ventoy_wrapper_fs_open(EFI_FILE_HANDLE This, EFI_FILE_HANDLE *New, CHAR16 *Name, UINT64 Mode, UINT64 Attributes)
+{
+ (VOID)This;
+ (VOID)New;
+ (VOID)Name;
+ (VOID)Mode;
+ (VOID)Attributes;
+ return EFI_SUCCESS;
+}
+
+STATIC EFI_STATUS EFIAPI
+ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This, EFI_FILE_HANDLE *New, CHAR16 *Name, UINT64 Mode, UINT64 Attributes, EFI_FILE_IO_TOKEN *Token)
+{
+ return ventoy_wrapper_fs_open(This, New, Name, Mode, Attributes);
+}
+
+STATIC EFI_STATUS EFIAPI
+ventoy_wrapper_file_delete(EFI_FILE_HANDLE This)
+{