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;
482 else if (Lba
+ secNum
> VirtSec
)
484 secNum
= VirtSec
- Lba
;
487 if (secNum
> g_sector_flag_num
)
489 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
490 if (NULL
== cur_flag
)
492 return EFI_OUT_OF_RESOURCES
;
495 FreePool(g_sector_flag
);
496 g_sector_flag
= cur_flag
;
497 g_sector_flag_num
= secNum
;
500 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
503 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
505 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
507 CopyMem((UINT8
*)Buffer
+ j
* 2048,
508 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
513 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
515 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
522 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
524 if (cur_flag
->flag
== 2)
528 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
529 lastlba
= cur_flag
->remap_lba
;
532 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
538 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
539 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
540 lastlba
= cur_flag
->remap_lba
;
548 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
554 EFI_STATUS EFIAPI ventoy_block_io_write
556 IN EFI_BLOCK_IO_PROTOCOL
*This
,
571 return EFI_WRITE_PROTECTED
;
574 secNum
= BufferSize
/ 2048;
577 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
580 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
587 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
590 UINT8 TmpBuf
[128] = {0};
591 VENDOR_DEVICE_PATH
*venPath
= NULL
;
593 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
594 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
595 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
596 venPath
->Header
.SubType
= HW_VENDOR_DP
;
597 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
598 venPath
->Header
.Length
[1] = 0;
599 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
600 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
602 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
603 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
605 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
610 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
614 CHAR16
*DriverName
= NULL
;
615 EFI_HANDLE
*Handles
= NULL
;
616 EFI_HANDLE DrvHandles
[2] = { NULL
};
617 EFI_STATUS Status
= EFI_SUCCESS
;
618 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
619 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
621 debug("ventoy_connect_driver <%s>...", DrvName
);
623 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
624 NULL
, &Count
, &Handles
);
625 if (EFI_ERROR(Status
))
630 for (i
= 0; i
< Count
; i
++)
632 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
633 if (EFI_ERROR(Status
))
638 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
639 if (EFI_ERROR(Status
) || NULL
== DriverName
)
644 if (StrStr(DriverName
, DrvName
))
646 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
647 DrvHandles
[0] = Handles
[i
];
654 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
655 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
659 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
665 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
666 NULL
, &Count
, &Handles
);
667 if (EFI_ERROR(Status
))
672 for (i
= 0; i
< Count
; i
++)
674 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
675 if (EFI_ERROR(Status
))
680 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
681 if (EFI_ERROR(Status
))
686 if (StrStr(DriverName
, DrvName
))
688 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
689 DrvHandles
[0] = Handles
[i
];
696 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
697 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
701 Status
= EFI_NOT_FOUND
;
709 EFI_STATUS EFIAPI ventoy_block_io_read_512
711 IN EFI_BLOCK_IO_PROTOCOL
*This
,
720 UINT8
*CurBuf
= NULL
;
721 EFI_STATUS Status
= EFI_SUCCESS
;
723 debug("ventoy_block_io_read_512 %lu %lu\n", Lba
, BufferSize
/ 512);
725 CurBuf
= (UINT8
*)Buffer
;
730 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
732 if (BufferSize
<= (4 - Mod
) * 512)
734 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
739 ReadSize
= (4 - Mod
) * 512;
740 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
743 BufferSize
-= ReadSize
;
747 if (BufferSize
>= 2048)
749 ReadSize
= BufferSize
/ 2048 * 2048;
751 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
754 Lba
+= ReadSize
/ 512;
755 BufferSize
-= ReadSize
;
760 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
761 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
767 EFI_STATUS EFIAPI ventoy_block_io_write_512
769 IN EFI_BLOCK_IO_PROTOCOL
*This
,
778 UINT8
*CurBuf
= NULL
;
779 EFI_STATUS Status
= EFI_SUCCESS
;
781 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
783 CurBuf
= (UINT8
*)Buffer
;
788 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
790 if (BufferSize
<= (4 - Mod
) * 512)
792 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
793 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
797 ReadSize
= (4 - Mod
) * 512;
798 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
799 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
803 BufferSize
-= ReadSize
;
807 if (BufferSize
>= 2048)
809 ReadSize
= BufferSize
/ 2048 * 2048;
811 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
814 Lba
+= ReadSize
/ 512;
815 BufferSize
-= ReadSize
;
820 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
822 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
823 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
829 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
831 EFI_STATUS Status
= EFI_SUCCESS
;
832 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
834 ventoy_fill_device_path();
836 debug("install block io protocol %p", ImageHandle
);
837 ventoy_debug_pause();
841 gBlockData
.Media
.BlockSize
= 512;
842 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
846 gBlockData
.Media
.BlockSize
= 2048;
847 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
850 gBlockData
.Media
.ReadOnly
= TRUE
;
851 gBlockData
.Media
.MediaPresent
= 1;
852 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
854 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
855 pBlockIo
->Media
= &(gBlockData
.Media
);
856 pBlockIo
->Reset
= ventoy_block_io_reset
;
860 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
861 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
862 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
863 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
867 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
868 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
871 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
873 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
874 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
875 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
877 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
878 if (EFI_ERROR(Status
))
883 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
884 debug("Connect disk IO driver %r", Status
);
886 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
887 debug("Connect partition driver %r", Status
);
888 if (EFI_ERROR(Status
))
890 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
891 debug("Connect all controller %r", Status
);
894 ventoy_debug_pause();
900 /* For file replace */
903 STATIC EFI_STATUS EFIAPI
904 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
914 STATIC EFI_STATUS EFIAPI
915 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
917 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
920 STATIC EFI_STATUS EFIAPI
921 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
927 STATIC EFI_STATUS EFIAPI
928 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
933 STATIC EFI_STATUS EFIAPI
934 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
941 STATIC EFI_STATUS EFIAPI
942 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
950 STATIC EFI_STATUS EFIAPI
951 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
957 return EFI_WRITE_PROTECTED
;
960 STATIC EFI_STATUS EFIAPI
961 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
963 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
967 STATIC EFI_STATUS EFIAPI
968 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
975 STATIC EFI_STATUS EFIAPI
976 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
980 if (Position
<= g_efi_file_replace
.FileSizeBytes
)
982 g_efi_file_replace
.CurPos
= Position
;
986 g_efi_file_replace
.CurPos
= g_efi_file_replace
.FileSizeBytes
;
992 STATIC EFI_STATUS EFIAPI
993 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
997 *Position
= g_efi_file_replace
.CurPos
;
1003 STATIC EFI_STATUS EFIAPI
1004 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
1006 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
1008 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
1010 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
1012 return EFI_INVALID_PARAMETER
;
1018 return EFI_BUFFER_TOO_SMALL
;
1021 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
1023 Info
->Size
= sizeof(EFI_FILE_INFO
);
1024 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
1025 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
1026 Info
->Attribute
= EFI_FILE_READ_ONLY
;
1027 //Info->FileName = EFI_FILE_READ_ONLY;
1034 STATIC EFI_STATUS EFIAPI
1035 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1038 UINTN ReadLen
= *Len
;
1042 debug("ventoy_wrapper_file_read ... %u", *Len
);
1044 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
1046 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
1049 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
1051 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1055 g_efi_file_replace
.CurPos
+= ReadLen
;
1060 STATIC EFI_STATUS EFIAPI
1061 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1063 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1066 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
1068 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1069 File
->Open
= ventoy_wrapper_fs_open
;
1070 File
->Close
= ventoy_wrapper_file_close
;
1071 File
->Delete
= ventoy_wrapper_file_delete
;
1072 File
->Read
= ventoy_wrapper_file_read
;
1073 File
->Write
= ventoy_wrapper_file_write
;
1074 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1075 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1076 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1077 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1078 File
->Flush
= ventoy_wrapper_file_flush
;
1079 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1080 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1081 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1082 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
1087 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1089 EFI_FILE_HANDLE This
,
1090 EFI_FILE_HANDLE
*New
,
1099 EFI_STATUS Status
= EFI_SUCCESS
;
1101 ventoy_virt_chunk
*virt
= NULL
;
1103 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1105 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1106 if (EFI_ERROR(Status
))
1111 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1112 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1114 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1115 for (j
= 0; j
< 4; j
++)
1117 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1119 g_original_fclose(*New
);
1120 *New
= &g_efi_file_replace
.WrapperHandle
;
1121 ventoy_wrapper_file_procotol(*New
);
1123 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1125 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1127 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1128 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1132 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1133 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1141 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1143 (*New
)->Open
= ventoy_wrapper_file_open
;
1150 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1152 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1153 OUT EFI_FILE_PROTOCOL
**Root
1156 EFI_STATUS Status
= EFI_SUCCESS
;
1158 Status
= g_original_open_volume(This
, Root
);
1159 if (!EFI_ERROR(Status
))
1161 g_original_fopen
= (*Root
)->Open
;
1162 g_original_fclose
= (*Root
)->Close
;
1163 (*Root
)->Open
= ventoy_wrapper_file_open
;
1169 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1171 g_original_open_volume
= OpenVolume
;
1176 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1179 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1181 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1182 OUT EFI_KEY_DATA
*KeyData
1185 /* only hook once before BCD file read */
1186 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1188 g_keyboard_hook_count
++;
1190 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1191 KeyData
->Key
.UnicodeChar
= 0;
1192 KeyData
->KeyState
.KeyShiftState
= 0;
1193 KeyData
->KeyState
.KeyToggleState
= 0;
1198 return g_org_read_key_ex(This
, KeyData
);
1201 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1203 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1204 OUT EFI_INPUT_KEY
*Key
1207 /* only hook once before BCD file read */
1208 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1210 g_keyboard_hook_count
++;
1212 Key
->ScanCode
= SCAN_DELETE
;
1213 Key
->UnicodeChar
= 0;
1217 return g_org_read_key(This
, Key
);
1220 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1222 g_blockio_start_record_bcd
= TRUE
;
1223 g_blockio_bcd_read_done
= FALSE
;
1224 g_keyboard_hook_count
= 0;
1226 if (g_con_simple_input_ex
)
1228 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1229 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1232 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1233 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1238 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1240 g_blockio_start_record_bcd
= FALSE
;
1241 g_blockio_bcd_read_done
= FALSE
;
1242 g_keyboard_hook_count
= 0;
1244 if (g_con_simple_input_ex
)
1246 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1249 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1255 /* Fixup the 1st cdrom influnce for Windows boot */
1258 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1260 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1261 IN EFI_GUID
*Protocol
, OPTIONAL
1262 IN VOID
*SearchKey
, OPTIONAL
1263 IN OUT UINTN
*BufferSize
,
1264 OUT EFI_HANDLE
*Buffer
1268 EFI_HANDLE Handle
= NULL
;
1269 EFI_STATUS Status
= EFI_SUCCESS
;
1271 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1273 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1275 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1277 if (Buffer
[i
] == gBlockData
.Handle
)
1280 Buffer
[0] = Buffer
[i
];
1290 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1292 g_org_locate_handle
= gBS
->LocateHandle
;
1293 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1298 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1300 gBS
->LocateHandle
= g_org_locate_handle
;
1301 g_org_locate_handle
= NULL
;