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 UINT8
*g_iso_data_buf
= NULL
;
40 UINTN g_iso_buf_size
= 0;
41 BOOLEAN gMemdiskMode
= FALSE
;
42 BOOLEAN gSector512Mode
= FALSE
;
44 ventoy_sector_flag
*g_sector_flag
= NULL
;
45 UINT32 g_sector_flag_num
= 0;
47 EFI_FILE_OPEN g_original_fopen
= NULL
;
48 EFI_FILE_CLOSE g_original_fclose
= NULL
;
49 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME g_original_open_volume
= NULL
;
51 /* EFI block device vendor device path GUID */
52 EFI_GUID gVtoyBlockDevicePathGuid
= VTOY_BLOCK_DEVICE_PATH_GUID
;
54 #define VENTOY_ISO9660_SECTOR_OVERFLOW 2097152
56 BOOLEAN g_fixup_iso9660_secover_enable
= FALSE
;
57 BOOLEAN g_fixup_iso9660_secover_start
= FALSE
;
58 UINT64 g_fixup_iso9660_secover_1st_secs
= 0;
59 UINT64 g_fixup_iso9660_secover_cur_secs
= 0;
60 UINT64 g_fixup_iso9660_secover_tot_secs
= 0;
62 STATIC UINTN g_keyboard_hook_count
= 0;
63 STATIC BOOLEAN g_blockio_start_record_bcd
= FALSE
;
64 STATIC BOOLEAN g_blockio_bcd_read_done
= FALSE
;
66 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*g_con_simple_input_ex
= NULL
;
67 STATIC EFI_INPUT_READ_KEY_EX g_org_read_key_ex
= NULL
;
68 STATIC EFI_INPUT_READ_KEY g_org_read_key
= NULL
;
70 STATIC EFI_LOCATE_HANDLE g_org_locate_handle
= NULL
;
72 STATIC UINT8 g_sector_buf
[2048];
73 STATIC EFI_BLOCK_READ g_sector_2048_read
= NULL
;
75 BOOLEAN
ventoy_is_cdrom_dp_exist(VOID
)
79 EFI_HANDLE
*Handles
= NULL
;
80 EFI_STATUS Status
= EFI_SUCCESS
;
81 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
83 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiDevicePathProtocolGuid
,
84 NULL
, &Count
, &Handles
);
85 if (EFI_ERROR(Status
))
90 for (i
= 0; i
< Count
; i
++)
92 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
93 if (EFI_ERROR(Status
))
98 while (!IsDevicePathEnd(DevicePath
))
100 if (MEDIA_DEVICE_PATH
== DevicePath
->Type
&& MEDIA_CDROM_DP
== DevicePath
->SubType
)
106 DevicePath
= NextDevicePathNode(DevicePath
);
115 /* Block IO procotol */
118 EFI_STATUS EFIAPI ventoy_block_io_reset
120 IN EFI_BLOCK_IO_PROTOCOL
*This
,
121 IN BOOLEAN ExtendedVerification
125 (VOID
)ExtendedVerification
;
129 STATIC EFI_STATUS EFIAPI ventoy_read_iso_sector
136 EFI_STATUS Status
= EFI_SUCCESS
;
141 UINT64 ReadStart
= 0;
143 UINT64 OverrideStart
= 0;
144 UINT64 OverrideEnd
= 0;
145 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
146 ventoy_img_chunk
*pchunk
= g_chunk
;
147 ventoy_override_chunk
*pOverride
= g_override_chunk
;
148 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
150 debug("read iso sector %lu count %u", Sector
, Count
);
152 ReadStart
= Sector
* 2048;
153 ReadEnd
= (Sector
+ Count
) * 2048;
155 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
157 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
159 if (g_chain
->disk_sector_size
== 512)
161 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
165 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2048 / g_chain
->disk_sector_size
+ pchunk
->disk_start_sector
;
168 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
169 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
171 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
172 MapLba
, secRead
* 2048, pCurBuf
);
173 if (EFI_ERROR(Status
))
175 debug("Raw disk read block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
181 pCurBuf
+= secRead
* 2048;
185 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
191 pCurBuf
= (UINT8
*)Buffer
;
192 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
194 OverrideStart
= pOverride
->img_offset
;
195 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
197 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
202 if (ReadStart
<= OverrideStart
)
204 if (ReadEnd
<= OverrideEnd
)
206 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
210 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
215 if (ReadEnd
<= OverrideEnd
)
217 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
221 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
225 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
226 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
228 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
229 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
231 g_fixup_iso9660_secover_start
= TRUE
;
232 g_fixup_iso9660_secover_cur_secs
= 0;
237 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
239 if (*(UINT32
*)Buffer
== 0x66676572)
241 g_blockio_bcd_read_done
= TRUE
;
248 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
250 IN EFI_BLOCK_IO_PROTOCOL
*This
,
257 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
262 CopyMem(Buffer
, g_iso_data_buf
+ (Lba
* 2048), BufferSize
);
264 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
266 if (*(UINT32
*)Buffer
== 0x66676572)
268 g_blockio_bcd_read_done
= TRUE
;
275 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
279 if (g_fixup_iso9660_secover_cur_secs
> 0)
281 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
282 g_fixup_iso9660_secover_cur_secs
+= secNum
;
283 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
285 g_fixup_iso9660_secover_start
= FALSE
;
291 ventoy_iso9660_override
*dirent
;
292 ventoy_override_chunk
*pOverride
;
294 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
296 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
297 if (Lba
== dirent
->first_sector
)
299 g_fixup_iso9660_secover_start
= FALSE
;
304 if (g_fixup_iso9660_secover_start
)
306 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
308 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
309 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
311 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
312 g_fixup_iso9660_secover_cur_secs
= secNum
;
313 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
315 g_fixup_iso9660_secover_start
= FALSE
;
317 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
328 EFI_STATUS EFIAPI ventoy_block_io_read
330 IN EFI_BLOCK_IO_PROTOCOL
*This
,
345 ventoy_sector_flag
*cur_flag
;
346 ventoy_virt_chunk
*node
;
348 //debug("### ventoy_block_io_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
350 secNum
= BufferSize
/ 2048;
352 /* Workaround for SSTR PE loader error */
353 if (g_fixup_iso9660_secover_start
)
355 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
360 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
362 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
365 if (secNum
> g_sector_flag_num
)
367 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
368 if (NULL
== cur_flag
)
370 return EFI_OUT_OF_RESOURCES
;
373 FreePool(g_sector_flag
);
374 g_sector_flag
= cur_flag
;
375 g_sector_flag_num
= secNum
;
378 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
381 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
383 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
385 CopyMem((UINT8
*)Buffer
+ j
* 2048,
386 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
391 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
393 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
400 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
402 if (cur_flag
->flag
== 2)
406 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
407 lastlba
= cur_flag
->remap_lba
;
410 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
416 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
417 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
418 lastlba
= cur_flag
->remap_lba
;
426 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
432 EFI_STATUS EFIAPI ventoy_block_io_write
434 IN EFI_BLOCK_IO_PROTOCOL
*This
,
446 return EFI_WRITE_PROTECTED
;
449 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
456 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
459 UINT8 TmpBuf
[128] = {0};
460 VENDOR_DEVICE_PATH
*venPath
= NULL
;
462 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
463 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
464 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
465 venPath
->Header
.SubType
= HW_VENDOR_DP
;
466 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
467 venPath
->Header
.Length
[1] = 0;
468 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
469 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
471 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
472 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
474 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
479 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
483 CHAR16
*DriverName
= NULL
;
484 EFI_HANDLE
*Handles
= NULL
;
485 EFI_HANDLE DrvHandles
[2] = { NULL
};
486 EFI_STATUS Status
= EFI_SUCCESS
;
487 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
488 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
490 debug("ventoy_connect_driver <%s>...", DrvName
);
492 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
493 NULL
, &Count
, &Handles
);
494 if (EFI_ERROR(Status
))
499 for (i
= 0; i
< Count
; i
++)
501 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
502 if (EFI_ERROR(Status
))
507 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
508 if (EFI_ERROR(Status
) || NULL
== DriverName
)
513 if (StrStr(DriverName
, DrvName
))
515 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
516 DrvHandles
[0] = Handles
[i
];
523 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
524 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
528 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
534 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
535 NULL
, &Count
, &Handles
);
536 if (EFI_ERROR(Status
))
541 for (i
= 0; i
< Count
; i
++)
543 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
544 if (EFI_ERROR(Status
))
549 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
550 if (EFI_ERROR(Status
))
555 if (StrStr(DriverName
, DrvName
))
557 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
558 DrvHandles
[0] = Handles
[i
];
565 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
566 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
570 Status
= EFI_NOT_FOUND
;
578 EFI_STATUS EFIAPI ventoy_block_io_read_512
580 IN EFI_BLOCK_IO_PROTOCOL
*This
,
589 UINT8
*CurBuf
= NULL
;
590 EFI_STATUS Status
= EFI_SUCCESS
;
592 debug("ventoy_block_io_read_512 %lu %lu\n", Lba
, BufferSize
/ 512);
594 CurBuf
= (UINT8
*)Buffer
;
599 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
601 if (BufferSize
<= (4 - Mod
) * 512)
603 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
608 ReadSize
= (4 - Mod
) * 512;
609 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
612 BufferSize
-= ReadSize
;
616 if (BufferSize
>= 2048)
618 ReadSize
= BufferSize
/ 2048 * 2048;
620 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
623 Lba
+= ReadSize
/ 512;
624 BufferSize
-= ReadSize
;
629 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
630 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
636 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
638 EFI_STATUS Status
= EFI_SUCCESS
;
639 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
641 ventoy_fill_device_path();
643 debug("install block io protocol %p", ImageHandle
);
644 ventoy_debug_pause();
648 gBlockData
.Media
.BlockSize
= 512;
649 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
653 gBlockData
.Media
.BlockSize
= 2048;
654 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
657 gBlockData
.Media
.ReadOnly
= TRUE
;
658 gBlockData
.Media
.MediaPresent
= 1;
659 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
661 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
662 pBlockIo
->Media
= &(gBlockData
.Media
);
663 pBlockIo
->Reset
= ventoy_block_io_reset
;
667 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
668 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
672 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
675 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
676 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
678 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
679 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
680 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
682 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
683 if (EFI_ERROR(Status
))
688 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
689 debug("Connect disk IO driver %r", Status
);
691 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
692 debug("Connect partition driver %r", Status
);
693 if (EFI_ERROR(Status
))
695 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
696 debug("Connect all controller %r", Status
);
699 ventoy_debug_pause();
705 /* For file replace */
708 STATIC EFI_STATUS EFIAPI
709 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
719 STATIC EFI_STATUS EFIAPI
720 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
722 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
725 STATIC EFI_STATUS EFIAPI
726 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
732 STATIC EFI_STATUS EFIAPI
733 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
738 STATIC EFI_STATUS EFIAPI
739 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
746 STATIC EFI_STATUS EFIAPI
747 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
755 STATIC EFI_STATUS EFIAPI
756 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
762 return EFI_WRITE_PROTECTED
;
765 STATIC EFI_STATUS EFIAPI
766 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
768 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
772 STATIC EFI_STATUS EFIAPI
773 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
780 STATIC EFI_STATUS EFIAPI
781 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
785 if (Position
<= g_efi_file_replace
.FileSizeBytes
)
787 g_efi_file_replace
.CurPos
= Position
;
791 g_efi_file_replace
.CurPos
= g_efi_file_replace
.FileSizeBytes
;
797 STATIC EFI_STATUS EFIAPI
798 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
802 *Position
= g_efi_file_replace
.CurPos
;
808 STATIC EFI_STATUS EFIAPI
809 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
811 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
813 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
815 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
817 return EFI_INVALID_PARAMETER
;
823 return EFI_BUFFER_TOO_SMALL
;
826 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
828 Info
->Size
= sizeof(EFI_FILE_INFO
);
829 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
830 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
831 Info
->Attribute
= EFI_FILE_READ_ONLY
;
832 //Info->FileName = EFI_FILE_READ_ONLY;
839 STATIC EFI_STATUS EFIAPI
840 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
843 UINTN ReadLen
= *Len
;
847 debug("ventoy_wrapper_file_read ... %u", *Len
);
849 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
851 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
854 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
856 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
860 g_efi_file_replace
.CurPos
+= ReadLen
;
865 STATIC EFI_STATUS EFIAPI
866 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
868 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
871 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
873 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
874 File
->Open
= ventoy_wrapper_fs_open
;
875 File
->Close
= ventoy_wrapper_file_close
;
876 File
->Delete
= ventoy_wrapper_file_delete
;
877 File
->Read
= ventoy_wrapper_file_read
;
878 File
->Write
= ventoy_wrapper_file_write
;
879 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
880 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
881 File
->GetInfo
= ventoy_wrapper_file_get_info
;
882 File
->SetInfo
= ventoy_wrapper_file_set_info
;
883 File
->Flush
= ventoy_wrapper_file_flush
;
884 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
885 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
886 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
887 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
892 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
894 EFI_FILE_HANDLE This
,
895 EFI_FILE_HANDLE
*New
,
904 EFI_STATUS Status
= EFI_SUCCESS
;
906 ventoy_virt_chunk
*virt
= NULL
;
908 debug("## ventoy_wrapper_file_open <%s> ", Name
);
910 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
911 if (EFI_ERROR(Status
))
916 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
917 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
919 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
920 for (j
= 0; j
< 4; j
++)
922 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
924 g_original_fclose(*New
);
925 *New
= &g_efi_file_replace
.WrapperHandle
;
926 ventoy_wrapper_file_procotol(*New
);
928 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
930 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
932 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
933 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
937 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
938 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
946 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
948 (*New
)->Open
= ventoy_wrapper_file_open
;
955 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
957 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
958 OUT EFI_FILE_PROTOCOL
**Root
961 EFI_STATUS Status
= EFI_SUCCESS
;
963 Status
= g_original_open_volume(This
, Root
);
964 if (!EFI_ERROR(Status
))
966 g_original_fopen
= (*Root
)->Open
;
967 g_original_fclose
= (*Root
)->Close
;
968 (*Root
)->Open
= ventoy_wrapper_file_open
;
974 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
976 g_original_open_volume
= OpenVolume
;
981 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
984 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
986 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
987 OUT EFI_KEY_DATA
*KeyData
990 /* only hook once before BCD file read */
991 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
993 g_keyboard_hook_count
++;
995 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
996 KeyData
->Key
.UnicodeChar
= 0;
997 KeyData
->KeyState
.KeyShiftState
= 0;
998 KeyData
->KeyState
.KeyToggleState
= 0;
1003 return g_org_read_key_ex(This
, KeyData
);
1006 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1008 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1009 OUT EFI_INPUT_KEY
*Key
1012 /* only hook once before BCD file read */
1013 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1015 g_keyboard_hook_count
++;
1017 Key
->ScanCode
= SCAN_DELETE
;
1018 Key
->UnicodeChar
= 0;
1022 return g_org_read_key(This
, Key
);
1025 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1027 g_blockio_start_record_bcd
= TRUE
;
1028 g_blockio_bcd_read_done
= FALSE
;
1029 g_keyboard_hook_count
= 0;
1031 if (g_con_simple_input_ex
)
1033 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1034 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1037 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1038 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1043 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1045 g_blockio_start_record_bcd
= FALSE
;
1046 g_blockio_bcd_read_done
= FALSE
;
1047 g_keyboard_hook_count
= 0;
1049 if (g_con_simple_input_ex
)
1051 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1054 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1060 /* Fixup the 1st cdrom influnce for Windows boot */
1063 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1065 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1066 IN EFI_GUID
*Protocol
, OPTIONAL
1067 IN VOID
*SearchKey
, OPTIONAL
1068 IN OUT UINTN
*BufferSize
,
1069 OUT EFI_HANDLE
*Buffer
1073 EFI_HANDLE Handle
= NULL
;
1074 EFI_STATUS Status
= EFI_SUCCESS
;
1076 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1078 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1080 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1082 if (Buffer
[i
] == gBlockData
.Handle
)
1085 Buffer
[0] = Buffer
[i
];
1095 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1097 g_org_locate_handle
= gBS
->LocateHandle
;
1098 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1103 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1105 gBS
->LocateHandle
= g_org_locate_handle
;
1106 g_org_locate_handle
= NULL
;