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
;
164 else if (g_chain
->disk_sector_size
== 1024)
166 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
168 else if (g_chain
->disk_sector_size
== 2048)
170 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
172 else if (g_chain
->disk_sector_size
== 4096)
174 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
178 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
179 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
181 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
182 MapLba
, secRead
* 2048, pCurBuf
);
183 if (EFI_ERROR(Status
))
185 debug("Raw disk read block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
191 pCurBuf
+= secRead
* 2048;
195 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
201 pCurBuf
= (UINT8
*)Buffer
;
202 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
204 OverrideStart
= pOverride
->img_offset
;
205 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
207 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
212 if (ReadStart
<= OverrideStart
)
214 if (ReadEnd
<= OverrideEnd
)
216 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
220 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
225 if (ReadEnd
<= OverrideEnd
)
227 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
231 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
235 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
236 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
238 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
239 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
241 g_fixup_iso9660_secover_start
= TRUE
;
242 g_fixup_iso9660_secover_cur_secs
= 0;
247 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
249 if (*(UINT32
*)Buffer
== 0x66676572)
251 g_blockio_bcd_read_done
= TRUE
;
258 STATIC EFI_STATUS EFIAPI ventoy_write_iso_sector
265 EFI_STATUS Status
= EFI_SUCCESS
;
270 UINT64 ReadStart
= 0;
272 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
273 ventoy_img_chunk
*pchunk
= g_chunk
;
274 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
276 debug("write iso sector %lu count %u", Sector
, Count
);
278 ReadStart
= Sector
* 2048;
279 ReadEnd
= (Sector
+ Count
) * 2048;
281 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
283 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
285 if (g_chain
->disk_sector_size
== 512)
287 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
289 else if (g_chain
->disk_sector_size
== 1024)
291 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
293 else if (g_chain
->disk_sector_size
== 2048)
295 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
297 else if (g_chain
->disk_sector_size
== 4096)
299 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
303 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
304 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
306 Status
= pRawBlockIo
->WriteBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
307 MapLba
, secRead
* 2048, pCurBuf
);
308 if (EFI_ERROR(Status
))
310 debug("Raw disk write block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
316 pCurBuf
+= secRead
* 2048;
323 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_write
325 IN EFI_BLOCK_IO_PROTOCOL
*This
,
340 return EFI_WRITE_PROTECTED
;
343 CopyMem(g_iso_data_buf
+ (Lba
* 2048), Buffer
, BufferSize
);
348 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
350 IN EFI_BLOCK_IO_PROTOCOL
*This
,
357 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
362 CopyMem(Buffer
, g_iso_data_buf
+ (Lba
* 2048), BufferSize
);
364 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
366 if (*(UINT32
*)Buffer
== 0x66676572)
368 g_blockio_bcd_read_done
= TRUE
;
375 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
379 if (g_fixup_iso9660_secover_cur_secs
> 0)
381 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
382 g_fixup_iso9660_secover_cur_secs
+= secNum
;
383 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
385 g_fixup_iso9660_secover_start
= FALSE
;
391 ventoy_iso9660_override
*dirent
;
392 ventoy_override_chunk
*pOverride
;
394 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
396 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
397 if (Lba
== dirent
->first_sector
)
399 g_fixup_iso9660_secover_start
= FALSE
;
404 if (g_fixup_iso9660_secover_start
)
406 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
408 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
409 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
411 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
412 g_fixup_iso9660_secover_cur_secs
= secNum
;
413 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
415 g_fixup_iso9660_secover_start
= FALSE
;
417 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
428 EFI_STATUS EFIAPI ventoy_block_io_read
430 IN EFI_BLOCK_IO_PROTOCOL
*This
,
447 ventoy_sector_flag
*cur_flag
;
448 ventoy_virt_chunk
*node
;
450 //debug("### ventoy_block_io_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
452 secNum
= BufferSize
/ 2048;
454 /* Workaround for SSTR PE loader error */
455 if (g_fixup_iso9660_secover_start
)
457 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
462 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
464 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
466 else if (offset
< g_chain
->real_img_size_in_bytes
)
468 TmpNum
= (g_chain
->real_img_size_in_bytes
- offset
) / 2048;
469 ventoy_read_iso_sector(Lba
, TmpNum
, Buffer
);
473 Buffer
= (UINT8
*)Buffer
+ (g_chain
->real_img_size_in_bytes
- offset
);
477 VirtSec
= g_chain
->virt_img_size_in_bytes
/ 2048;
478 if (Lba
+ secNum
> VirtSec
)
480 secNum
= VirtSec
- Lba
;
483 if (secNum
> g_sector_flag_num
)
485 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
486 if (NULL
== cur_flag
)
488 return EFI_OUT_OF_RESOURCES
;
491 FreePool(g_sector_flag
);
492 g_sector_flag
= cur_flag
;
493 g_sector_flag_num
= secNum
;
496 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
499 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
501 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
503 CopyMem((UINT8
*)Buffer
+ j
* 2048,
504 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
509 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
511 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
518 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
520 if (cur_flag
->flag
== 2)
524 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
525 lastlba
= cur_flag
->remap_lba
;
528 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
534 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
535 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
536 lastlba
= cur_flag
->remap_lba
;
544 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
550 EFI_STATUS EFIAPI ventoy_block_io_write
552 IN EFI_BLOCK_IO_PROTOCOL
*This
,
567 return EFI_WRITE_PROTECTED
;
570 secNum
= BufferSize
/ 2048;
573 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
576 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
583 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
586 UINT8 TmpBuf
[128] = {0};
587 VENDOR_DEVICE_PATH
*venPath
= NULL
;
589 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
590 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
591 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
592 venPath
->Header
.SubType
= HW_VENDOR_DP
;
593 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
594 venPath
->Header
.Length
[1] = 0;
595 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
596 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
598 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
599 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
601 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
606 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
610 CHAR16
*DriverName
= NULL
;
611 EFI_HANDLE
*Handles
= NULL
;
612 EFI_HANDLE DrvHandles
[2] = { NULL
};
613 EFI_STATUS Status
= EFI_SUCCESS
;
614 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
615 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
617 debug("ventoy_connect_driver <%s>...", DrvName
);
619 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
620 NULL
, &Count
, &Handles
);
621 if (EFI_ERROR(Status
))
626 for (i
= 0; i
< Count
; i
++)
628 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
629 if (EFI_ERROR(Status
))
634 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
635 if (EFI_ERROR(Status
) || NULL
== DriverName
)
640 if (StrStr(DriverName
, DrvName
))
642 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
643 DrvHandles
[0] = Handles
[i
];
650 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
651 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
655 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
661 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
662 NULL
, &Count
, &Handles
);
663 if (EFI_ERROR(Status
))
668 for (i
= 0; i
< Count
; i
++)
670 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
671 if (EFI_ERROR(Status
))
676 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
677 if (EFI_ERROR(Status
))
682 if (StrStr(DriverName
, DrvName
))
684 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
685 DrvHandles
[0] = Handles
[i
];
692 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
693 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
697 Status
= EFI_NOT_FOUND
;
705 EFI_STATUS EFIAPI ventoy_block_io_read_512
707 IN EFI_BLOCK_IO_PROTOCOL
*This
,
716 UINT8
*CurBuf
= NULL
;
717 EFI_STATUS Status
= EFI_SUCCESS
;
719 debug("ventoy_block_io_read_512 %lu %lu\n", Lba
, BufferSize
/ 512);
721 CurBuf
= (UINT8
*)Buffer
;
726 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
728 if (BufferSize
<= (4 - Mod
) * 512)
730 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
735 ReadSize
= (4 - Mod
) * 512;
736 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
739 BufferSize
-= ReadSize
;
743 if (BufferSize
>= 2048)
745 ReadSize
= BufferSize
/ 2048 * 2048;
747 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
750 Lba
+= ReadSize
/ 512;
751 BufferSize
-= ReadSize
;
756 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
757 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
763 EFI_STATUS EFIAPI ventoy_block_io_write_512
765 IN EFI_BLOCK_IO_PROTOCOL
*This
,
774 UINT8
*CurBuf
= NULL
;
775 EFI_STATUS Status
= EFI_SUCCESS
;
777 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
779 CurBuf
= (UINT8
*)Buffer
;
784 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
786 if (BufferSize
<= (4 - Mod
) * 512)
788 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
789 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
793 ReadSize
= (4 - Mod
) * 512;
794 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
795 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
799 BufferSize
-= ReadSize
;
803 if (BufferSize
>= 2048)
805 ReadSize
= BufferSize
/ 2048 * 2048;
807 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
810 Lba
+= ReadSize
/ 512;
811 BufferSize
-= ReadSize
;
816 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
818 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
819 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
825 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
827 EFI_STATUS Status
= EFI_SUCCESS
;
828 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
830 ventoy_fill_device_path();
832 debug("install block io protocol %p", ImageHandle
);
833 ventoy_debug_pause();
837 gBlockData
.Media
.BlockSize
= 512;
838 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
842 gBlockData
.Media
.BlockSize
= 2048;
843 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
846 gBlockData
.Media
.ReadOnly
= TRUE
;
847 gBlockData
.Media
.MediaPresent
= 1;
848 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
850 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
851 pBlockIo
->Media
= &(gBlockData
.Media
);
852 pBlockIo
->Reset
= ventoy_block_io_reset
;
856 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
857 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
858 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
859 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
863 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
864 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
867 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
869 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
870 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
871 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
873 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
874 if (EFI_ERROR(Status
))
879 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
880 debug("Connect disk IO driver %r", Status
);
882 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
883 debug("Connect partition driver %r", Status
);
884 if (EFI_ERROR(Status
))
886 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
887 debug("Connect all controller %r", Status
);
890 ventoy_debug_pause();
896 /* For file replace */
899 STATIC EFI_STATUS EFIAPI
900 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
910 STATIC EFI_STATUS EFIAPI
911 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
913 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
916 STATIC EFI_STATUS EFIAPI
917 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
923 STATIC EFI_STATUS EFIAPI
924 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
929 STATIC EFI_STATUS EFIAPI
930 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
937 STATIC EFI_STATUS EFIAPI
938 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
946 STATIC EFI_STATUS EFIAPI
947 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
953 return EFI_WRITE_PROTECTED
;
956 STATIC EFI_STATUS EFIAPI
957 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
959 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
963 STATIC EFI_STATUS EFIAPI
964 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
971 STATIC EFI_STATUS EFIAPI
972 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
976 if (Position
<= g_efi_file_replace
.FileSizeBytes
)
978 g_efi_file_replace
.CurPos
= Position
;
982 g_efi_file_replace
.CurPos
= g_efi_file_replace
.FileSizeBytes
;
988 STATIC EFI_STATUS EFIAPI
989 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
993 *Position
= g_efi_file_replace
.CurPos
;
999 STATIC EFI_STATUS EFIAPI
1000 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
1002 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
1004 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
1006 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
1008 return EFI_INVALID_PARAMETER
;
1014 return EFI_BUFFER_TOO_SMALL
;
1017 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
1019 Info
->Size
= sizeof(EFI_FILE_INFO
);
1020 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
1021 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
1022 Info
->Attribute
= EFI_FILE_READ_ONLY
;
1023 //Info->FileName = EFI_FILE_READ_ONLY;
1030 STATIC EFI_STATUS EFIAPI
1031 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1034 UINTN ReadLen
= *Len
;
1038 debug("ventoy_wrapper_file_read ... %u", *Len
);
1040 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
1042 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
1045 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
1047 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1051 g_efi_file_replace
.CurPos
+= ReadLen
;
1056 STATIC EFI_STATUS EFIAPI
1057 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1059 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1062 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
1064 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1065 File
->Open
= ventoy_wrapper_fs_open
;
1066 File
->Close
= ventoy_wrapper_file_close
;
1067 File
->Delete
= ventoy_wrapper_file_delete
;
1068 File
->Read
= ventoy_wrapper_file_read
;
1069 File
->Write
= ventoy_wrapper_file_write
;
1070 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1071 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1072 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1073 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1074 File
->Flush
= ventoy_wrapper_file_flush
;
1075 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1076 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1077 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1078 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
1083 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1085 EFI_FILE_HANDLE This
,
1086 EFI_FILE_HANDLE
*New
,
1095 EFI_STATUS Status
= EFI_SUCCESS
;
1097 ventoy_virt_chunk
*virt
= NULL
;
1099 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1101 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1102 if (EFI_ERROR(Status
))
1107 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1108 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1110 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1111 for (j
= 0; j
< 4; j
++)
1113 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1115 g_original_fclose(*New
);
1116 *New
= &g_efi_file_replace
.WrapperHandle
;
1117 ventoy_wrapper_file_procotol(*New
);
1119 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1121 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1123 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1124 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1128 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1129 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1137 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1139 (*New
)->Open
= ventoy_wrapper_file_open
;
1146 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1148 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1149 OUT EFI_FILE_PROTOCOL
**Root
1152 EFI_STATUS Status
= EFI_SUCCESS
;
1154 Status
= g_original_open_volume(This
, Root
);
1155 if (!EFI_ERROR(Status
))
1157 g_original_fopen
= (*Root
)->Open
;
1158 g_original_fclose
= (*Root
)->Close
;
1159 (*Root
)->Open
= ventoy_wrapper_file_open
;
1165 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1167 g_original_open_volume
= OpenVolume
;
1172 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1175 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1177 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1178 OUT EFI_KEY_DATA
*KeyData
1181 /* only hook once before BCD file read */
1182 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1184 g_keyboard_hook_count
++;
1186 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1187 KeyData
->Key
.UnicodeChar
= 0;
1188 KeyData
->KeyState
.KeyShiftState
= 0;
1189 KeyData
->KeyState
.KeyToggleState
= 0;
1194 return g_org_read_key_ex(This
, KeyData
);
1197 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1199 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1200 OUT EFI_INPUT_KEY
*Key
1203 /* only hook once before BCD file read */
1204 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1206 g_keyboard_hook_count
++;
1208 Key
->ScanCode
= SCAN_DELETE
;
1209 Key
->UnicodeChar
= 0;
1213 return g_org_read_key(This
, Key
);
1216 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1218 g_blockio_start_record_bcd
= TRUE
;
1219 g_blockio_bcd_read_done
= FALSE
;
1220 g_keyboard_hook_count
= 0;
1222 if (g_con_simple_input_ex
)
1224 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1225 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1228 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1229 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1234 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1236 g_blockio_start_record_bcd
= FALSE
;
1237 g_blockio_bcd_read_done
= FALSE
;
1238 g_keyboard_hook_count
= 0;
1240 if (g_con_simple_input_ex
)
1242 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1245 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1251 /* Fixup the 1st cdrom influnce for Windows boot */
1254 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1256 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1257 IN EFI_GUID
*Protocol
, OPTIONAL
1258 IN VOID
*SearchKey
, OPTIONAL
1259 IN OUT UINTN
*BufferSize
,
1260 OUT EFI_HANDLE
*Buffer
1264 EFI_HANDLE Handle
= NULL
;
1265 EFI_STATUS Status
= EFI_SUCCESS
;
1267 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1269 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1271 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1273 if (Buffer
[i
] == gBlockData
.Handle
)
1276 Buffer
[0] = Buffer
[i
];
1286 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1288 g_org_locate_handle
= gBS
->LocateHandle
;
1289 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1294 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1296 gBS
->LocateHandle
= g_org_locate_handle
;
1297 g_org_locate_handle
= NULL
;