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
;
74 STATIC EFI_BLOCK_WRITE g_sector_2048_write
= NULL
;
76 BOOLEAN
ventoy_is_cdrom_dp_exist(VOID
)
80 EFI_HANDLE
*Handles
= NULL
;
81 EFI_STATUS Status
= EFI_SUCCESS
;
82 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
= NULL
;
84 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiDevicePathProtocolGuid
,
85 NULL
, &Count
, &Handles
);
86 if (EFI_ERROR(Status
))
91 for (i
= 0; i
< Count
; i
++)
93 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
, (VOID
**)&DevicePath
);
94 if (EFI_ERROR(Status
))
99 while (!IsDevicePathEnd(DevicePath
))
101 if (MEDIA_DEVICE_PATH
== DevicePath
->Type
&& MEDIA_CDROM_DP
== DevicePath
->SubType
)
107 DevicePath
= NextDevicePathNode(DevicePath
);
116 /* Block IO procotol */
119 EFI_STATUS EFIAPI ventoy_block_io_reset
121 IN EFI_BLOCK_IO_PROTOCOL
*This
,
122 IN BOOLEAN ExtendedVerification
126 (VOID
)ExtendedVerification
;
130 STATIC EFI_STATUS EFIAPI ventoy_read_iso_sector
137 EFI_STATUS Status
= EFI_SUCCESS
;
142 UINT64 ReadStart
= 0;
144 UINT64 OverrideStart
= 0;
145 UINT64 OverrideEnd
= 0;
146 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
147 ventoy_img_chunk
*pchunk
= g_chunk
;
148 ventoy_override_chunk
*pOverride
= g_override_chunk
;
149 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
151 debug("read iso sector %lu count %u", Sector
, Count
);
153 ReadStart
= Sector
* 2048;
154 ReadEnd
= (Sector
+ Count
) * 2048;
156 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
158 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
160 if (g_chain
->disk_sector_size
== 512)
162 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
166 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2048 / g_chain
->disk_sector_size
+ pchunk
->disk_start_sector
;
169 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
170 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
172 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
173 MapLba
, secRead
* 2048, pCurBuf
);
174 if (EFI_ERROR(Status
))
176 debug("Raw disk read block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
182 pCurBuf
+= secRead
* 2048;
186 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
192 pCurBuf
= (UINT8
*)Buffer
;
193 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
195 OverrideStart
= pOverride
->img_offset
;
196 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
198 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
203 if (ReadStart
<= OverrideStart
)
205 if (ReadEnd
<= OverrideEnd
)
207 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
211 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
216 if (ReadEnd
<= OverrideEnd
)
218 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
222 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
226 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
227 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
229 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
230 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
232 g_fixup_iso9660_secover_start
= TRUE
;
233 g_fixup_iso9660_secover_cur_secs
= 0;
238 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
240 if (*(UINT32
*)Buffer
== 0x66676572)
242 g_blockio_bcd_read_done
= TRUE
;
249 STATIC EFI_STATUS EFIAPI ventoy_write_iso_sector
256 EFI_STATUS Status
= EFI_SUCCESS
;
261 UINT64 ReadStart
= 0;
263 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
264 ventoy_img_chunk
*pchunk
= g_chunk
;
265 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
267 debug("write iso sector %lu count %u", Sector
, Count
);
269 ReadStart
= Sector
* 2048;
270 ReadEnd
= (Sector
+ Count
) * 2048;
272 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
274 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
276 if (g_chain
->disk_sector_size
== 512)
278 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
282 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2048 / g_chain
->disk_sector_size
+ pchunk
->disk_start_sector
;
285 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
286 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
288 Status
= pRawBlockIo
->WriteBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
289 MapLba
, secRead
* 2048, pCurBuf
);
290 if (EFI_ERROR(Status
))
292 debug("Raw disk write block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
298 pCurBuf
+= secRead
* 2048;
305 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_write
307 IN EFI_BLOCK_IO_PROTOCOL
*This
,
322 return EFI_WRITE_PROTECTED
;
325 CopyMem(g_iso_data_buf
+ (Lba
* 2048), Buffer
, BufferSize
);
330 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
332 IN EFI_BLOCK_IO_PROTOCOL
*This
,
339 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
344 CopyMem(Buffer
, g_iso_data_buf
+ (Lba
* 2048), BufferSize
);
346 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
348 if (*(UINT32
*)Buffer
== 0x66676572)
350 g_blockio_bcd_read_done
= TRUE
;
357 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
361 if (g_fixup_iso9660_secover_cur_secs
> 0)
363 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
364 g_fixup_iso9660_secover_cur_secs
+= secNum
;
365 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
367 g_fixup_iso9660_secover_start
= FALSE
;
373 ventoy_iso9660_override
*dirent
;
374 ventoy_override_chunk
*pOverride
;
376 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
378 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
379 if (Lba
== dirent
->first_sector
)
381 g_fixup_iso9660_secover_start
= FALSE
;
386 if (g_fixup_iso9660_secover_start
)
388 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
390 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
391 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
393 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
394 g_fixup_iso9660_secover_cur_secs
= secNum
;
395 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
397 g_fixup_iso9660_secover_start
= FALSE
;
399 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
410 EFI_STATUS EFIAPI ventoy_block_io_read
412 IN EFI_BLOCK_IO_PROTOCOL
*This
,
427 ventoy_sector_flag
*cur_flag
;
428 ventoy_virt_chunk
*node
;
430 //debug("### ventoy_block_io_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
432 secNum
= BufferSize
/ 2048;
434 /* Workaround for SSTR PE loader error */
435 if (g_fixup_iso9660_secover_start
)
437 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
442 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
444 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
447 if (secNum
> g_sector_flag_num
)
449 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
450 if (NULL
== cur_flag
)
452 return EFI_OUT_OF_RESOURCES
;
455 FreePool(g_sector_flag
);
456 g_sector_flag
= cur_flag
;
457 g_sector_flag_num
= secNum
;
460 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
463 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
465 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
467 CopyMem((UINT8
*)Buffer
+ j
* 2048,
468 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
473 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
475 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
482 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
484 if (cur_flag
->flag
== 2)
488 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
489 lastlba
= cur_flag
->remap_lba
;
492 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
498 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
499 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
500 lastlba
= cur_flag
->remap_lba
;
508 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
514 EFI_STATUS EFIAPI ventoy_block_io_write
516 IN EFI_BLOCK_IO_PROTOCOL
*This
,
531 return EFI_WRITE_PROTECTED
;
534 secNum
= BufferSize
/ 2048;
537 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
540 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
547 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
550 UINT8 TmpBuf
[128] = {0};
551 VENDOR_DEVICE_PATH
*venPath
= NULL
;
553 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
554 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
555 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
556 venPath
->Header
.SubType
= HW_VENDOR_DP
;
557 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
558 venPath
->Header
.Length
[1] = 0;
559 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
560 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
562 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
563 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
565 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
570 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
574 CHAR16
*DriverName
= NULL
;
575 EFI_HANDLE
*Handles
= NULL
;
576 EFI_HANDLE DrvHandles
[2] = { NULL
};
577 EFI_STATUS Status
= EFI_SUCCESS
;
578 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
579 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
581 debug("ventoy_connect_driver <%s>...", DrvName
);
583 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
584 NULL
, &Count
, &Handles
);
585 if (EFI_ERROR(Status
))
590 for (i
= 0; i
< Count
; i
++)
592 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
593 if (EFI_ERROR(Status
))
598 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
599 if (EFI_ERROR(Status
) || NULL
== DriverName
)
604 if (StrStr(DriverName
, DrvName
))
606 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
607 DrvHandles
[0] = Handles
[i
];
614 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
615 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
619 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
625 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
626 NULL
, &Count
, &Handles
);
627 if (EFI_ERROR(Status
))
632 for (i
= 0; i
< Count
; i
++)
634 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
635 if (EFI_ERROR(Status
))
640 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
641 if (EFI_ERROR(Status
))
646 if (StrStr(DriverName
, DrvName
))
648 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
649 DrvHandles
[0] = Handles
[i
];
656 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
657 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
661 Status
= EFI_NOT_FOUND
;
669 EFI_STATUS EFIAPI ventoy_block_io_read_512
671 IN EFI_BLOCK_IO_PROTOCOL
*This
,
680 UINT8
*CurBuf
= NULL
;
681 EFI_STATUS Status
= EFI_SUCCESS
;
683 debug("ventoy_block_io_read_512 %lu %lu\n", Lba
, BufferSize
/ 512);
685 CurBuf
= (UINT8
*)Buffer
;
690 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
692 if (BufferSize
<= (4 - Mod
) * 512)
694 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
699 ReadSize
= (4 - Mod
) * 512;
700 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
703 BufferSize
-= ReadSize
;
707 if (BufferSize
>= 2048)
709 ReadSize
= BufferSize
/ 2048 * 2048;
711 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
714 Lba
+= ReadSize
/ 512;
715 BufferSize
-= ReadSize
;
720 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
721 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
727 EFI_STATUS EFIAPI ventoy_block_io_write_512
729 IN EFI_BLOCK_IO_PROTOCOL
*This
,
738 UINT8
*CurBuf
= NULL
;
739 EFI_STATUS Status
= EFI_SUCCESS
;
741 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
743 CurBuf
= (UINT8
*)Buffer
;
748 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
750 if (BufferSize
<= (4 - Mod
) * 512)
752 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
753 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
757 ReadSize
= (4 - Mod
) * 512;
758 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
759 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
763 BufferSize
-= ReadSize
;
767 if (BufferSize
>= 2048)
769 ReadSize
= BufferSize
/ 2048 * 2048;
771 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
774 Lba
+= ReadSize
/ 512;
775 BufferSize
-= ReadSize
;
780 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
782 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
783 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
789 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
791 EFI_STATUS Status
= EFI_SUCCESS
;
792 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
794 ventoy_fill_device_path();
796 debug("install block io protocol %p", ImageHandle
);
797 ventoy_debug_pause();
801 gBlockData
.Media
.BlockSize
= 512;
802 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
806 gBlockData
.Media
.BlockSize
= 2048;
807 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
810 gBlockData
.Media
.ReadOnly
= TRUE
;
811 gBlockData
.Media
.MediaPresent
= 1;
812 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
814 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
815 pBlockIo
->Media
= &(gBlockData
.Media
);
816 pBlockIo
->Reset
= ventoy_block_io_reset
;
820 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
821 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
822 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
823 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
827 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
828 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
831 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
833 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
834 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
835 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
837 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
838 if (EFI_ERROR(Status
))
843 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
844 debug("Connect disk IO driver %r", Status
);
846 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
847 debug("Connect partition driver %r", Status
);
848 if (EFI_ERROR(Status
))
850 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
851 debug("Connect all controller %r", Status
);
854 ventoy_debug_pause();
860 /* For file replace */
863 STATIC EFI_STATUS EFIAPI
864 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
874 STATIC EFI_STATUS EFIAPI
875 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
877 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
880 STATIC EFI_STATUS EFIAPI
881 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
887 STATIC EFI_STATUS EFIAPI
888 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
893 STATIC EFI_STATUS EFIAPI
894 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
901 STATIC EFI_STATUS EFIAPI
902 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
910 STATIC EFI_STATUS EFIAPI
911 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
917 return EFI_WRITE_PROTECTED
;
920 STATIC EFI_STATUS EFIAPI
921 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
923 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
927 STATIC EFI_STATUS EFIAPI
928 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
935 STATIC EFI_STATUS EFIAPI
936 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
940 if (Position
<= g_efi_file_replace
.FileSizeBytes
)
942 g_efi_file_replace
.CurPos
= Position
;
946 g_efi_file_replace
.CurPos
= g_efi_file_replace
.FileSizeBytes
;
952 STATIC EFI_STATUS EFIAPI
953 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
957 *Position
= g_efi_file_replace
.CurPos
;
963 STATIC EFI_STATUS EFIAPI
964 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
966 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
968 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
970 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
972 return EFI_INVALID_PARAMETER
;
978 return EFI_BUFFER_TOO_SMALL
;
981 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
983 Info
->Size
= sizeof(EFI_FILE_INFO
);
984 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
985 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
986 Info
->Attribute
= EFI_FILE_READ_ONLY
;
987 //Info->FileName = EFI_FILE_READ_ONLY;
994 STATIC EFI_STATUS EFIAPI
995 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
998 UINTN ReadLen
= *Len
;
1002 debug("ventoy_wrapper_file_read ... %u", *Len
);
1004 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
1006 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
1009 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
1011 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1015 g_efi_file_replace
.CurPos
+= ReadLen
;
1020 STATIC EFI_STATUS EFIAPI
1021 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1023 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1026 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
1028 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1029 File
->Open
= ventoy_wrapper_fs_open
;
1030 File
->Close
= ventoy_wrapper_file_close
;
1031 File
->Delete
= ventoy_wrapper_file_delete
;
1032 File
->Read
= ventoy_wrapper_file_read
;
1033 File
->Write
= ventoy_wrapper_file_write
;
1034 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1035 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1036 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1037 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1038 File
->Flush
= ventoy_wrapper_file_flush
;
1039 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1040 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1041 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1042 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
1047 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1049 EFI_FILE_HANDLE This
,
1050 EFI_FILE_HANDLE
*New
,
1059 EFI_STATUS Status
= EFI_SUCCESS
;
1061 ventoy_virt_chunk
*virt
= NULL
;
1063 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1065 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1066 if (EFI_ERROR(Status
))
1071 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1072 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1074 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1075 for (j
= 0; j
< 4; j
++)
1077 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1079 g_original_fclose(*New
);
1080 *New
= &g_efi_file_replace
.WrapperHandle
;
1081 ventoy_wrapper_file_procotol(*New
);
1083 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1085 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1087 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1088 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1092 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1093 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1101 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1103 (*New
)->Open
= ventoy_wrapper_file_open
;
1110 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1112 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1113 OUT EFI_FILE_PROTOCOL
**Root
1116 EFI_STATUS Status
= EFI_SUCCESS
;
1118 Status
= g_original_open_volume(This
, Root
);
1119 if (!EFI_ERROR(Status
))
1121 g_original_fopen
= (*Root
)->Open
;
1122 g_original_fclose
= (*Root
)->Close
;
1123 (*Root
)->Open
= ventoy_wrapper_file_open
;
1129 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1131 g_original_open_volume
= OpenVolume
;
1136 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1139 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1141 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1142 OUT EFI_KEY_DATA
*KeyData
1145 /* only hook once before BCD file read */
1146 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1148 g_keyboard_hook_count
++;
1150 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1151 KeyData
->Key
.UnicodeChar
= 0;
1152 KeyData
->KeyState
.KeyShiftState
= 0;
1153 KeyData
->KeyState
.KeyToggleState
= 0;
1158 return g_org_read_key_ex(This
, KeyData
);
1161 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1163 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1164 OUT EFI_INPUT_KEY
*Key
1167 /* only hook once before BCD file read */
1168 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1170 g_keyboard_hook_count
++;
1172 Key
->ScanCode
= SCAN_DELETE
;
1173 Key
->UnicodeChar
= 0;
1177 return g_org_read_key(This
, Key
);
1180 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1182 g_blockio_start_record_bcd
= TRUE
;
1183 g_blockio_bcd_read_done
= FALSE
;
1184 g_keyboard_hook_count
= 0;
1186 if (g_con_simple_input_ex
)
1188 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1189 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1192 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1193 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1198 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1200 g_blockio_start_record_bcd
= FALSE
;
1201 g_blockio_bcd_read_done
= FALSE
;
1202 g_keyboard_hook_count
= 0;
1204 if (g_con_simple_input_ex
)
1206 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1209 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1215 /* Fixup the 1st cdrom influnce for Windows boot */
1218 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1220 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1221 IN EFI_GUID
*Protocol
, OPTIONAL
1222 IN VOID
*SearchKey
, OPTIONAL
1223 IN OUT UINTN
*BufferSize
,
1224 OUT EFI_HANDLE
*Buffer
1228 EFI_HANDLE Handle
= NULL
;
1229 EFI_STATUS Status
= EFI_SUCCESS
;
1231 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1233 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1235 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1237 if (Buffer
[i
] == gBlockData
.Handle
)
1240 Buffer
[0] = Buffer
[i
];
1250 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1252 g_org_locate_handle
= gBS
->LocateHandle
;
1253 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1258 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1260 gBS
->LocateHandle
= g_org_locate_handle
;
1261 g_org_locate_handle
= NULL
;