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>
39 UINTN g_iso_buf_size
= 0;
40 BOOLEAN gMemdiskMode
= FALSE
;
42 ventoy_sector_flag
*g_sector_flag
= NULL
;
43 UINT32 g_sector_flag_num
= 0;
45 EFI_FILE_OPEN g_original_fopen
= NULL
;
46 EFI_FILE_CLOSE g_original_fclose
= NULL
;
47 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME g_original_open_volume
= NULL
;
49 /* EFI block device vendor device path GUID */
50 EFI_GUID gVtoyBlockDevicePathGuid
= VTOY_BLOCK_DEVICE_PATH_GUID
;
52 #define VENTOY_ISO9660_SECTOR_OVERFLOW 2097152
54 BOOLEAN g_fixup_iso9660_secover_enable
= FALSE
;
55 BOOLEAN g_fixup_iso9660_secover_start
= FALSE
;
56 UINT64 g_fixup_iso9660_secover_1st_secs
= 0;
57 UINT64 g_fixup_iso9660_secover_cur_secs
= 0;
58 UINT64 g_fixup_iso9660_secover_tot_secs
= 0;
60 STATIC UINTN g_keyboard_hook_count
= 0;
61 STATIC BOOLEAN g_blockio_start_record_bcd
= FALSE
;
62 STATIC BOOLEAN g_blockio_bcd_read_done
= FALSE
;
64 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*g_con_simple_input_ex
= NULL
;
65 STATIC EFI_INPUT_READ_KEY_EX g_org_read_key_ex
= NULL
;
66 STATIC EFI_INPUT_READ_KEY g_org_read_key
= NULL
;
68 STATIC EFI_LOCATE_HANDLE g_org_locate_handle
= NULL
;
70 BOOLEAN
ventoy_is_cdrom_dp_exist(VOID
)
74 EFI_HANDLE
*Handles
= NULL
;
75 EFI_STATUS Status
= EFI_SUCCESS
;
76 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
78 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiDevicePathProtocolGuid
,
79 NULL
, &Count
, &Handles
);
80 if (EFI_ERROR(Status
))
85 for (i
= 0; i
< Count
; i
++)
87 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
88 if (EFI_ERROR(Status
))
93 while (!IsDevicePathEnd(DevicePath
))
95 if (MEDIA_DEVICE_PATH
== DevicePath
->Type
&& MEDIA_CDROM_DP
== DevicePath
->SubType
)
101 DevicePath
= NextDevicePathNode(DevicePath
);
110 /* Block IO procotol */
113 EFI_STATUS EFIAPI ventoy_block_io_reset
115 IN EFI_BLOCK_IO_PROTOCOL
*This
,
116 IN BOOLEAN ExtendedVerification
120 (VOID
)ExtendedVerification
;
124 STATIC EFI_STATUS EFIAPI ventoy_read_iso_sector
131 EFI_STATUS Status
= EFI_SUCCESS
;
136 UINT64 ReadStart
= 0;
138 UINT64 OverrideStart
= 0;
139 UINT64 OverrideEnd
= 0;
140 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
141 ventoy_img_chunk
*pchunk
= g_chunk
;
142 ventoy_override_chunk
*pOverride
= g_override_chunk
;
143 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
145 debug("read iso sector %lu count %u", Sector
, Count
);
147 ReadStart
= Sector
* 2048;
148 ReadEnd
= (Sector
+ Count
) * 2048;
150 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
152 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
154 if (g_chain
->disk_sector_size
== 512)
156 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
160 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2048 / g_chain
->disk_sector_size
+ pchunk
->disk_start_sector
;
163 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
164 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
166 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
167 MapLba
, secRead
* 2048, pCurBuf
);
168 if (EFI_ERROR(Status
))
170 debug("Raw disk read block failed %r", Status
);
176 pCurBuf
+= secRead
* 2048;
180 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
186 pCurBuf
= (UINT8
*)Buffer
;
187 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
189 OverrideStart
= pOverride
->img_offset
;
190 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
192 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
197 if (ReadStart
<= OverrideStart
)
199 if (ReadEnd
<= OverrideEnd
)
201 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
205 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
210 if (ReadEnd
<= OverrideEnd
)
212 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
216 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
220 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
221 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
223 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
224 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
226 g_fixup_iso9660_secover_start
= TRUE
;
227 g_fixup_iso9660_secover_cur_secs
= 0;
232 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
234 if (*(UINT32
*)Buffer
== 0x66676572)
236 g_blockio_bcd_read_done
= TRUE
;
243 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
245 IN EFI_BLOCK_IO_PROTOCOL
*This
,
252 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
257 CopyMem(Buffer
, (char *)g_chain
+ (Lba
* 2048), BufferSize
);
259 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
261 if (*(UINT32
*)Buffer
== 0x66676572)
263 g_blockio_bcd_read_done
= TRUE
;
270 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
274 if (g_fixup_iso9660_secover_cur_secs
> 0)
276 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
277 g_fixup_iso9660_secover_cur_secs
+= secNum
;
278 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
280 g_fixup_iso9660_secover_start
= FALSE
;
286 ventoy_iso9660_override
*dirent
;
287 ventoy_override_chunk
*pOverride
;
289 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
291 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
292 if (Lba
== dirent
->first_sector
)
294 g_fixup_iso9660_secover_start
= FALSE
;
299 if (g_fixup_iso9660_secover_start
)
301 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
303 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
304 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
306 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
307 g_fixup_iso9660_secover_cur_secs
= secNum
;
308 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
310 g_fixup_iso9660_secover_start
= FALSE
;
312 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
323 EFI_STATUS EFIAPI ventoy_block_io_read
325 IN EFI_BLOCK_IO_PROTOCOL
*This
,
340 ventoy_sector_flag
*cur_flag
;
341 ventoy_virt_chunk
*node
;
343 //debug("### ventoy_block_io_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
345 secNum
= BufferSize
/ 2048;
347 /* Workaround for SSTR PE loader error */
348 if (g_fixup_iso9660_secover_start
)
350 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
355 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
357 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
360 if (secNum
> g_sector_flag_num
)
362 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
363 if (NULL
== cur_flag
)
365 return EFI_OUT_OF_RESOURCES
;
368 FreePool(g_sector_flag
);
369 g_sector_flag
= cur_flag
;
370 g_sector_flag_num
= secNum
;
373 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
376 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
378 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
380 CopyMem((UINT8
*)Buffer
+ j
* 2048,
381 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
386 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
388 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
395 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
397 if (cur_flag
->flag
== 2)
401 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
402 lastlba
= cur_flag
->remap_lba
;
405 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
411 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
412 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
413 lastlba
= cur_flag
->remap_lba
;
421 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
427 EFI_STATUS EFIAPI ventoy_block_io_write
429 IN EFI_BLOCK_IO_PROTOCOL
*This
,
441 return EFI_WRITE_PROTECTED
;
444 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
451 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
454 UINT8 TmpBuf
[128] = {0};
455 VENDOR_DEVICE_PATH
*venPath
= NULL
;
457 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
458 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
459 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
460 venPath
->Header
.SubType
= HW_VENDOR_DP
;
461 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
462 venPath
->Header
.Length
[1] = 0;
463 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
464 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
466 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
467 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
469 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
474 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
478 CHAR16
*DriverName
= NULL
;
479 EFI_HANDLE
*Handles
= NULL
;
480 EFI_HANDLE DrvHandles
[2] = { NULL
};
481 EFI_STATUS Status
= EFI_SUCCESS
;
482 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
483 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
485 debug("ventoy_connect_driver <%s>...", DrvName
);
487 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
488 NULL
, &Count
, &Handles
);
489 if (EFI_ERROR(Status
))
494 for (i
= 0; i
< Count
; i
++)
496 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
497 if (EFI_ERROR(Status
))
502 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
503 if (EFI_ERROR(Status
) || NULL
== DriverName
)
508 if (StrStr(DriverName
, DrvName
))
510 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
511 DrvHandles
[0] = Handles
[i
];
518 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
519 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
523 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
529 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
530 NULL
, &Count
, &Handles
);
531 if (EFI_ERROR(Status
))
536 for (i
= 0; i
< Count
; i
++)
538 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
539 if (EFI_ERROR(Status
))
544 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
545 if (EFI_ERROR(Status
))
550 if (StrStr(DriverName
, DrvName
))
552 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
553 DrvHandles
[0] = Handles
[i
];
560 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
561 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
565 Status
= EFI_NOT_FOUND
;
573 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
575 EFI_STATUS Status
= EFI_SUCCESS
;
576 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
578 ventoy_fill_device_path();
580 debug("install block io protocol %p", ImageHandle
);
581 ventoy_debug_pause();
583 gBlockData
.Media
.BlockSize
= 2048;
584 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
585 gBlockData
.Media
.ReadOnly
= TRUE
;
586 gBlockData
.Media
.MediaPresent
= 1;
587 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
589 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
590 pBlockIo
->Media
= &(gBlockData
.Media
);
591 pBlockIo
->Reset
= ventoy_block_io_reset
;
592 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
593 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
594 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
596 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
597 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
598 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
600 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
601 if (EFI_ERROR(Status
))
606 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
607 debug("Connect disk IO driver %r", Status
);
609 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
610 debug("Connect partition driver %r", Status
);
611 if (EFI_ERROR(Status
))
613 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
614 debug("Connect all controller %r", Status
);
617 ventoy_debug_pause();
623 /* For file replace */
626 STATIC EFI_STATUS EFIAPI
627 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
637 STATIC EFI_STATUS EFIAPI
638 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
640 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
643 STATIC EFI_STATUS EFIAPI
644 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
650 STATIC EFI_STATUS EFIAPI
651 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
656 STATIC EFI_STATUS EFIAPI
657 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
664 STATIC EFI_STATUS EFIAPI
665 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
673 STATIC EFI_STATUS EFIAPI
674 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
680 return EFI_WRITE_PROTECTED
;
683 STATIC EFI_STATUS EFIAPI
684 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
686 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
690 STATIC EFI_STATUS EFIAPI
691 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
698 STATIC EFI_STATUS EFIAPI
699 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
703 g_efi_file_replace
.CurPos
= Position
;
707 STATIC EFI_STATUS EFIAPI
708 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
712 *Position
= g_efi_file_replace
.CurPos
;
718 STATIC EFI_STATUS EFIAPI
719 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
721 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
723 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
725 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
727 return EFI_INVALID_PARAMETER
;
733 return EFI_BUFFER_TOO_SMALL
;
736 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
738 Info
->Size
= sizeof(EFI_FILE_INFO
);
739 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
740 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
741 Info
->Attribute
= EFI_FILE_READ_ONLY
;
742 //Info->FileName = EFI_FILE_READ_ONLY;
749 STATIC EFI_STATUS EFIAPI
750 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
753 UINTN ReadLen
= *Len
;
757 debug("ventoy_wrapper_file_read ... %u", *Len
);
759 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
761 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
764 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
766 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
770 g_efi_file_replace
.CurPos
+= ReadLen
;
775 STATIC EFI_STATUS EFIAPI
776 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
778 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
781 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
783 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
784 File
->Open
= ventoy_wrapper_fs_open
;
785 File
->Close
= ventoy_wrapper_file_close
;
786 File
->Delete
= ventoy_wrapper_file_delete
;
787 File
->Read
= ventoy_wrapper_file_read
;
788 File
->Write
= ventoy_wrapper_file_write
;
789 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
790 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
791 File
->GetInfo
= ventoy_wrapper_file_get_info
;
792 File
->SetInfo
= ventoy_wrapper_file_set_info
;
793 File
->Flush
= ventoy_wrapper_file_flush
;
794 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
795 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
796 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
797 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
802 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
804 EFI_FILE_HANDLE This
,
805 EFI_FILE_HANDLE
*New
,
814 EFI_STATUS Status
= EFI_SUCCESS
;
816 ventoy_virt_chunk
*virt
= NULL
;
818 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
819 if (EFI_ERROR(Status
))
824 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
825 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
827 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
828 for (j
= 0; j
< 4; j
++)
830 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
832 g_original_fclose(*New
);
833 *New
= &g_efi_file_replace
.WrapperHandle
;
834 ventoy_wrapper_file_procotol(*New
);
836 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
838 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
840 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
841 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
845 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
846 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
858 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
860 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
861 OUT EFI_FILE_PROTOCOL
**Root
864 EFI_STATUS Status
= EFI_SUCCESS
;
866 Status
= g_original_open_volume(This
, Root
);
867 if (!EFI_ERROR(Status
))
869 g_original_fopen
= (*Root
)->Open
;
870 g_original_fclose
= (*Root
)->Close
;
871 (*Root
)->Open
= ventoy_wrapper_file_open
;
877 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
879 g_original_open_volume
= OpenVolume
;
884 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
887 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
889 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
890 OUT EFI_KEY_DATA
*KeyData
893 /* only hook once before BCD file read */
894 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
896 g_keyboard_hook_count
++;
898 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
899 KeyData
->Key
.UnicodeChar
= 0;
900 KeyData
->KeyState
.KeyShiftState
= 0;
901 KeyData
->KeyState
.KeyToggleState
= 0;
906 return g_org_read_key_ex(This
, KeyData
);
909 EFI_STATUS EFIAPI ventoy_wrapper_read_key
911 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
912 OUT EFI_INPUT_KEY
*Key
915 /* only hook once before BCD file read */
916 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
918 g_keyboard_hook_count
++;
920 Key
->ScanCode
= SCAN_DELETE
;
921 Key
->UnicodeChar
= 0;
925 return g_org_read_key(This
, Key
);
928 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
930 g_blockio_start_record_bcd
= TRUE
;
931 g_blockio_bcd_read_done
= FALSE
;
932 g_keyboard_hook_count
= 0;
934 if (g_con_simple_input_ex
)
936 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
937 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
940 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
941 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
946 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
948 g_blockio_start_record_bcd
= FALSE
;
949 g_blockio_bcd_read_done
= FALSE
;
950 g_keyboard_hook_count
= 0;
952 if (g_con_simple_input_ex
)
954 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
957 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
963 /* Fixup the 1st cdrom influnce for Windows boot */
966 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
968 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
969 IN EFI_GUID
*Protocol
, OPTIONAL
970 IN VOID
*SearchKey
, OPTIONAL
971 IN OUT UINTN
*BufferSize
,
972 OUT EFI_HANDLE
*Buffer
976 EFI_HANDLE Handle
= NULL
;
977 EFI_STATUS Status
= EFI_SUCCESS
;
979 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
981 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
983 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
985 if (Buffer
[i
] == gBlockData
.Handle
)
988 Buffer
[0] = Buffer
[i
];
998 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1000 g_org_locate_handle
= gBS
->LocateHandle
;
1001 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1006 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1008 gBS
->LocateHandle
= g_org_locate_handle
;
1009 g_org_locate_handle
= NULL
;