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
;
69 /* Block IO procotol */
72 EFI_STATUS EFIAPI ventoy_block_io_reset
74 IN EFI_BLOCK_IO_PROTOCOL
*This
,
75 IN BOOLEAN ExtendedVerification
79 (VOID
)ExtendedVerification
;
83 STATIC EFI_STATUS EFIAPI ventoy_read_iso_sector
90 EFI_STATUS Status
= EFI_SUCCESS
;
97 UINT64 OverrideStart
= 0;
98 UINT64 OverrideEnd
= 0;
99 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
100 ventoy_img_chunk
*pchunk
= g_chunk
;
101 ventoy_override_chunk
*pOverride
= g_override_chunk
;
102 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
104 debug("read iso sector %lu count %u", Sector
, Count
);
106 ReadStart
= Sector
* 2048;
107 ReadEnd
= (Sector
+ Count
) * 2048;
109 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
111 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
113 if (g_chain
->disk_sector_size
== 512)
115 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
119 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2048 / g_chain
->disk_sector_size
+ pchunk
->disk_start_sector
;
122 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
123 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
125 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
126 MapLba
, secRead
* 2048, pCurBuf
);
127 if (EFI_ERROR(Status
))
129 debug("Raw disk read block failed %r", Status
);
135 pCurBuf
+= secRead
* 2048;
139 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
145 pCurBuf
= (UINT8
*)Buffer
;
146 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
148 OverrideStart
= pOverride
->img_offset
;
149 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
151 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
156 if (ReadStart
<= OverrideStart
)
158 if (ReadEnd
<= OverrideEnd
)
160 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
164 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
169 if (ReadEnd
<= OverrideEnd
)
171 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
175 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
179 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
180 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
182 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
183 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
185 g_fixup_iso9660_secover_start
= TRUE
;
186 g_fixup_iso9660_secover_cur_secs
= 0;
191 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
193 if (*(UINT32
*)Buffer
== 0x66676572)
195 g_blockio_bcd_read_done
= TRUE
;
202 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
204 IN EFI_BLOCK_IO_PROTOCOL
*This
,
211 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
216 CopyMem(Buffer
, (char *)g_chain
+ (Lba
* 2048), BufferSize
);
218 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
220 if (*(UINT32
*)Buffer
== 0x66676572)
222 g_blockio_bcd_read_done
= TRUE
;
229 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
233 if (g_fixup_iso9660_secover_cur_secs
> 0)
235 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
236 g_fixup_iso9660_secover_cur_secs
+= secNum
;
237 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
239 g_fixup_iso9660_secover_start
= FALSE
;
245 ventoy_iso9660_override
*dirent
;
246 ventoy_override_chunk
*pOverride
;
248 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
250 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
251 if (Lba
== dirent
->first_sector
)
253 g_fixup_iso9660_secover_start
= FALSE
;
258 if (g_fixup_iso9660_secover_start
)
260 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
262 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
263 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
265 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
266 g_fixup_iso9660_secover_cur_secs
= secNum
;
267 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
269 g_fixup_iso9660_secover_start
= FALSE
;
271 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
282 EFI_STATUS EFIAPI ventoy_block_io_read
284 IN EFI_BLOCK_IO_PROTOCOL
*This
,
299 ventoy_sector_flag
*cur_flag
;
300 ventoy_virt_chunk
*node
;
302 //debug("### ventoy_block_io_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
304 secNum
= BufferSize
/ 2048;
306 /* Workaround for SSTR PE loader error */
307 if (g_fixup_iso9660_secover_start
)
309 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
314 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
316 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
319 if (secNum
> g_sector_flag_num
)
321 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
322 if (NULL
== cur_flag
)
324 return EFI_OUT_OF_RESOURCES
;
327 FreePool(g_sector_flag
);
328 g_sector_flag
= cur_flag
;
329 g_sector_flag_num
= secNum
;
332 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
335 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
337 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
339 CopyMem((UINT8
*)Buffer
+ j
* 2048,
340 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
345 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
347 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
354 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
356 if (cur_flag
->flag
== 2)
360 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
361 lastlba
= cur_flag
->remap_lba
;
364 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
370 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
371 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
372 lastlba
= cur_flag
->remap_lba
;
380 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
386 EFI_STATUS EFIAPI ventoy_block_io_write
388 IN EFI_BLOCK_IO_PROTOCOL
*This
,
400 return EFI_WRITE_PROTECTED
;
403 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
410 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
413 UINT8 TmpBuf
[128] = {0};
414 VENDOR_DEVICE_PATH
*venPath
= NULL
;
416 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
417 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
418 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
419 venPath
->Header
.SubType
= HW_VENDOR_DP
;
420 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
421 venPath
->Header
.Length
[1] = 0;
422 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
423 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
425 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
426 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
428 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
433 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
437 CHAR16
*DriverName
= NULL
;
438 EFI_HANDLE
*Handles
= NULL
;
439 EFI_HANDLE DrvHandles
[2] = { NULL
};
440 EFI_STATUS Status
= EFI_SUCCESS
;
441 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
442 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
444 debug("ventoy_connect_driver <%s>...", DrvName
);
446 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
447 NULL
, &Count
, &Handles
);
448 if (EFI_ERROR(Status
))
453 for (i
= 0; i
< Count
; i
++)
455 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
456 if (EFI_ERROR(Status
))
461 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
462 if (EFI_ERROR(Status
) || NULL
== DriverName
)
467 if (StrStr(DriverName
, DrvName
))
469 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
470 DrvHandles
[0] = Handles
[i
];
477 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
478 debug("Connect partition driver:<%r>", Status
);
482 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
488 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
489 NULL
, &Count
, &Handles
);
490 if (EFI_ERROR(Status
))
495 for (i
= 0; i
< Count
; i
++)
497 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
498 if (EFI_ERROR(Status
))
503 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
504 if (EFI_ERROR(Status
))
509 if (StrStr(DriverName
, DrvName
))
511 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
512 DrvHandles
[0] = Handles
[i
];
519 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
520 debug("Connect partition driver:<%r>", Status
);
524 Status
= EFI_NOT_FOUND
;
532 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
534 EFI_STATUS Status
= EFI_SUCCESS
;
535 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
537 ventoy_fill_device_path();
539 gBlockData
.Media
.BlockSize
= 2048;
540 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
541 gBlockData
.Media
.ReadOnly
= TRUE
;
542 gBlockData
.Media
.MediaPresent
= 1;
543 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
545 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
546 pBlockIo
->Media
= &(gBlockData
.Media
);
547 pBlockIo
->Reset
= ventoy_block_io_reset
;
548 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
549 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
550 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
552 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
553 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
554 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
556 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
557 if (EFI_ERROR(Status
))
562 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
563 debug("Connect disk IO driver %r", Status
);
564 ventoy_debug_pause();
566 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
567 debug("Connect partition driver %r", Status
);
568 if (EFI_ERROR(Status
))
570 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
571 debug("Connect all controller %r", Status
);
574 ventoy_debug_pause();
580 /* For file replace */
583 STATIC EFI_STATUS EFIAPI
584 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
594 STATIC EFI_STATUS EFIAPI
595 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
597 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
600 STATIC EFI_STATUS EFIAPI
601 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
607 STATIC EFI_STATUS EFIAPI
608 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
613 STATIC EFI_STATUS EFIAPI
614 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
621 STATIC EFI_STATUS EFIAPI
622 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
630 STATIC EFI_STATUS EFIAPI
631 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
637 return EFI_WRITE_PROTECTED
;
640 STATIC EFI_STATUS EFIAPI
641 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
643 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
647 STATIC EFI_STATUS EFIAPI
648 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
655 STATIC EFI_STATUS EFIAPI
656 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
660 g_efi_file_replace
.CurPos
= Position
;
664 STATIC EFI_STATUS EFIAPI
665 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
669 *Position
= g_efi_file_replace
.CurPos
;
675 STATIC EFI_STATUS EFIAPI
676 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
678 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
680 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
682 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
684 return EFI_INVALID_PARAMETER
;
690 return EFI_BUFFER_TOO_SMALL
;
693 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
695 Info
->Size
= sizeof(EFI_FILE_INFO
);
696 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
697 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
698 Info
->Attribute
= EFI_FILE_READ_ONLY
;
699 //Info->FileName = EFI_FILE_READ_ONLY;
706 STATIC EFI_STATUS EFIAPI
707 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
710 UINTN ReadLen
= *Len
;
714 debug("ventoy_wrapper_file_read ... %u", *Len
);
716 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
718 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
721 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
723 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
727 g_efi_file_replace
.CurPos
+= ReadLen
;
732 STATIC EFI_STATUS EFIAPI
733 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
735 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
738 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
740 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
741 File
->Open
= ventoy_wrapper_fs_open
;
742 File
->Close
= ventoy_wrapper_file_close
;
743 File
->Delete
= ventoy_wrapper_file_delete
;
744 File
->Read
= ventoy_wrapper_file_read
;
745 File
->Write
= ventoy_wrapper_file_write
;
746 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
747 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
748 File
->GetInfo
= ventoy_wrapper_file_get_info
;
749 File
->SetInfo
= ventoy_wrapper_file_set_info
;
750 File
->Flush
= ventoy_wrapper_file_flush
;
751 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
752 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
753 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
754 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
759 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
761 EFI_FILE_HANDLE This
,
762 EFI_FILE_HANDLE
*New
,
771 EFI_STATUS Status
= EFI_SUCCESS
;
773 ventoy_virt_chunk
*virt
= NULL
;
775 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
776 if (EFI_ERROR(Status
))
781 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
782 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
784 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
785 for (j
= 0; j
< 4; j
++)
787 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
789 g_original_fclose(*New
);
790 *New
= &g_efi_file_replace
.WrapperHandle
;
791 ventoy_wrapper_file_procotol(*New
);
793 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
795 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
797 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
798 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
802 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
803 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
815 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
817 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
818 OUT EFI_FILE_PROTOCOL
**Root
821 EFI_STATUS Status
= EFI_SUCCESS
;
823 Status
= g_original_open_volume(This
, Root
);
824 if (!EFI_ERROR(Status
))
826 g_original_fopen
= (*Root
)->Open
;
827 g_original_fclose
= (*Root
)->Close
;
828 (*Root
)->Open
= ventoy_wrapper_file_open
;
834 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
836 g_original_open_volume
= OpenVolume
;
841 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
844 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
846 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
847 OUT EFI_KEY_DATA
*KeyData
850 /* only hook once before BCD file read */
851 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
853 g_keyboard_hook_count
++;
855 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
856 KeyData
->Key
.UnicodeChar
= 0;
857 KeyData
->KeyState
.KeyShiftState
= 0;
858 KeyData
->KeyState
.KeyToggleState
= 0;
863 return g_org_read_key_ex(This
, KeyData
);
866 EFI_STATUS EFIAPI ventoy_wrapper_read_key
868 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
869 OUT EFI_INPUT_KEY
*Key
872 /* only hook once before BCD file read */
873 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
875 g_keyboard_hook_count
++;
877 Key
->ScanCode
= SCAN_DELETE
;
878 Key
->UnicodeChar
= 0;
882 return g_org_read_key(This
, Key
);
885 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
887 g_blockio_start_record_bcd
= TRUE
;
888 g_blockio_bcd_read_done
= FALSE
;
889 g_keyboard_hook_count
= 0;
891 if (g_con_simple_input_ex
)
893 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
894 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
897 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
898 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
903 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
905 g_blockio_start_record_bcd
= FALSE
;
906 g_blockio_bcd_read_done
= FALSE
;
907 g_keyboard_hook_count
= 0;
909 if (g_con_simple_input_ex
)
911 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
914 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;