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 Buffer:%p Align:%u", Sector
, Count
, Buffer
, pRawBlockIo
->Media
->IoAlign
);
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
;
177 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
178 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
180 Status
= pRawBlockIo
->ReadBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
181 MapLba
, secRead
* 2048, pCurBuf
);
182 if (EFI_ERROR(Status
))
184 debug("Raw disk read block failed %r LBA:%lu Count:%u %p", Status
, MapLba
, secRead
, pCurBuf
);
190 pCurBuf
+= secRead
* 2048;
194 if (ReadStart
> g_chain
->real_img_size_in_bytes
)
200 pCurBuf
= (UINT8
*)Buffer
;
201 for (i
= 0; i
< g_override_chunk_num
; i
++, pOverride
++)
203 OverrideStart
= pOverride
->img_offset
;
204 OverrideEnd
= pOverride
->img_offset
+ pOverride
->override_size
;
206 if (OverrideStart
>= ReadEnd
|| ReadStart
>= OverrideEnd
)
211 if (ReadStart
<= OverrideStart
)
213 if (ReadEnd
<= OverrideEnd
)
215 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, ReadEnd
- OverrideStart
);
219 CopyMem(pCurBuf
+ OverrideStart
- ReadStart
, pOverride
->override_data
, pOverride
->override_size
);
224 if (ReadEnd
<= OverrideEnd
)
226 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, ReadEnd
- ReadStart
);
230 CopyMem(pCurBuf
, pOverride
->override_data
+ ReadStart
- OverrideStart
, OverrideEnd
- ReadStart
);
234 if (g_fixup_iso9660_secover_enable
&& (!g_fixup_iso9660_secover_start
) &&
235 pOverride
->override_size
== sizeof(ventoy_iso9660_override
))
237 ventoy_iso9660_override
*dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
238 if (dirent
->first_sector
>= VENTOY_ISO9660_SECTOR_OVERFLOW
)
240 g_fixup_iso9660_secover_start
= TRUE
;
241 g_fixup_iso9660_secover_cur_secs
= 0;
246 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
248 if (*(UINT32
*)Buffer
== 0x66676572)
250 g_blockio_bcd_read_done
= TRUE
;
257 STATIC EFI_STATUS EFIAPI ventoy_write_iso_sector
264 EFI_STATUS Status
= EFI_SUCCESS
;
269 UINT64 ReadStart
= 0;
271 UINT8
*pCurBuf
= (UINT8
*)Buffer
;
272 ventoy_img_chunk
*pchunk
= g_chunk
;
273 EFI_BLOCK_IO_PROTOCOL
*pRawBlockIo
= gBlockData
.pRawBlockIo
;
275 debug("write iso sector %lu count %u", Sector
, Count
);
277 ReadStart
= Sector
* 2048;
278 ReadEnd
= (Sector
+ Count
) * 2048;
280 for (i
= 0; Count
> 0 && i
< g_img_chunk_num
; i
++, pchunk
++)
282 if (Sector
>= pchunk
->img_start_sector
&& Sector
<= pchunk
->img_end_sector
)
284 if (g_chain
->disk_sector_size
== 512)
286 MapLba
= (Sector
- pchunk
->img_start_sector
) * 4 + pchunk
->disk_start_sector
;
288 else if (g_chain
->disk_sector_size
== 1024)
290 MapLba
= (Sector
- pchunk
->img_start_sector
) * 2 + pchunk
->disk_start_sector
;
292 else if (g_chain
->disk_sector_size
== 2048)
294 MapLba
= (Sector
- pchunk
->img_start_sector
) + pchunk
->disk_start_sector
;
296 else if (g_chain
->disk_sector_size
== 4096)
298 MapLba
= ((Sector
- pchunk
->img_start_sector
) >> 1) + pchunk
->disk_start_sector
;
302 secLeft
= pchunk
->img_end_sector
+ 1 - Sector
;
303 secRead
= (Count
< secLeft
) ? Count
: secLeft
;
305 Status
= pRawBlockIo
->WriteBlocks(pRawBlockIo
, pRawBlockIo
->Media
->MediaId
,
306 MapLba
, secRead
* 2048, pCurBuf
);
307 if (EFI_ERROR(Status
))
309 debug("Raw disk write block failed %r LBA:%lu Count:%u", Status
, MapLba
, secRead
);
315 pCurBuf
+= secRead
* 2048;
322 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_write
324 IN EFI_BLOCK_IO_PROTOCOL
*This
,
339 return EFI_WRITE_PROTECTED
;
342 CopyMem(g_iso_data_buf
+ (Lba
* 2048), Buffer
, BufferSize
);
347 EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
349 IN EFI_BLOCK_IO_PROTOCOL
*This
,
356 //debug("### ventoy_block_io_ramdisk_read sector:%u count:%u", (UINT32)Lba, (UINT32)BufferSize / 2048);
361 CopyMem(Buffer
, g_iso_data_buf
+ (Lba
* 2048), BufferSize
);
363 if (g_blockio_start_record_bcd
&& FALSE
== g_blockio_bcd_read_done
)
365 if (*(UINT32
*)Buffer
== 0x66676572)
367 g_blockio_bcd_read_done
= TRUE
;
374 EFI_LBA EFIAPI
ventoy_fixup_iso9660_sector(IN EFI_LBA Lba
, UINT32 secNum
)
378 if (g_fixup_iso9660_secover_cur_secs
> 0)
380 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
381 g_fixup_iso9660_secover_cur_secs
+= secNum
;
382 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
384 g_fixup_iso9660_secover_start
= FALSE
;
390 ventoy_iso9660_override
*dirent
;
391 ventoy_override_chunk
*pOverride
;
393 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
395 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
396 if (Lba
== dirent
->first_sector
)
398 g_fixup_iso9660_secover_start
= FALSE
;
403 if (g_fixup_iso9660_secover_start
)
405 for (i
= 0, pOverride
= g_override_chunk
; i
< g_override_chunk_num
; i
++, pOverride
++)
407 dirent
= (ventoy_iso9660_override
*)pOverride
->override_data
;
408 if (Lba
+ VENTOY_ISO9660_SECTOR_OVERFLOW
== dirent
->first_sector
)
410 g_fixup_iso9660_secover_tot_secs
= (dirent
->size
+ 2047) / 2048;
411 g_fixup_iso9660_secover_cur_secs
= secNum
;
412 if (g_fixup_iso9660_secover_cur_secs
>= g_fixup_iso9660_secover_tot_secs
)
414 g_fixup_iso9660_secover_start
= FALSE
;
416 Lba
+= VENTOY_ISO9660_SECTOR_OVERFLOW
;
427 EFI_STATUS EFIAPI ventoy_block_io_read_real
429 IN EFI_BLOCK_IO_PROTOCOL
*This
,
446 ventoy_sector_flag
*cur_flag
;
447 ventoy_virt_chunk
*node
;
449 debug("### block_io_read_real sector:%u count:%u Buffer:%p", (UINT32
)Lba
, (UINT32
)BufferSize
/ 2048, Buffer
);
451 secNum
= BufferSize
/ 2048;
453 /* Workaround for SSTR PE loader error */
454 if (g_fixup_iso9660_secover_start
)
456 Lba
= ventoy_fixup_iso9660_sector(Lba
, secNum
);
461 if (offset
+ BufferSize
<= g_chain
->real_img_size_in_bytes
)
463 return ventoy_read_iso_sector(Lba
, secNum
, Buffer
);
465 else if (offset
< g_chain
->real_img_size_in_bytes
)
467 TmpNum
= (g_chain
->real_img_size_in_bytes
- offset
) / 2048;
468 ventoy_read_iso_sector(Lba
, TmpNum
, Buffer
);
472 Buffer
= (UINT8
*)Buffer
+ (g_chain
->real_img_size_in_bytes
- offset
);
476 VirtSec
= g_chain
->virt_img_size_in_bytes
/ 2048;
481 else if (Lba
+ secNum
> VirtSec
)
483 secNum
= VirtSec
- Lba
;
486 if (secNum
> g_sector_flag_num
)
488 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
489 if (NULL
== cur_flag
)
491 return EFI_OUT_OF_RESOURCES
;
494 FreePool(g_sector_flag
);
495 g_sector_flag
= cur_flag
;
496 g_sector_flag_num
= secNum
;
499 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
502 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
504 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
506 CopyMem((UINT8
*)Buffer
+ j
* 2048,
507 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
512 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
514 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
521 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
523 if (cur_flag
->flag
== 2)
527 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
528 lastlba
= cur_flag
->remap_lba
;
531 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
537 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
538 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
539 lastlba
= cur_flag
->remap_lba
;
547 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
553 EFI_STATUS EFIAPI ventoy_block_io_read
555 IN EFI_BLOCK_IO_PROTOCOL
*This
,
564 EFI_STATUS Status
= EFI_OUT_OF_RESOURCES
;
566 if (gBlockData
.pRawBlockIo
&& gBlockData
.pRawBlockIo
->Media
)
568 IoAlign
= gBlockData
.pRawBlockIo
->Media
->IoAlign
;
571 if ((IoAlign
== 0) || (((UINTN
) Buffer
& (IoAlign
- 1)) == 0))
573 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, Buffer
);
577 NewBuf
= AllocatePages(EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
580 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, NewBuf
);
581 CopyMem(Buffer
, NewBuf
, BufferSize
);
582 FreePages(NewBuf
, EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
589 EFI_STATUS EFIAPI ventoy_block_io_write
591 IN EFI_BLOCK_IO_PROTOCOL
*This
,
606 return EFI_WRITE_PROTECTED
;
609 secNum
= BufferSize
/ 2048;
612 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
615 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
622 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
625 UINT8 TmpBuf
[128] = {0};
626 VENDOR_DEVICE_PATH
*venPath
= NULL
;
628 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
629 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
630 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
631 venPath
->Header
.SubType
= HW_VENDOR_DP
;
632 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
633 venPath
->Header
.Length
[1] = 0;
634 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
635 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
637 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
638 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
640 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
645 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
649 CHAR16
*DriverName
= NULL
;
650 EFI_HANDLE
*Handles
= NULL
;
651 EFI_HANDLE DrvHandles
[2] = { NULL
};
652 EFI_STATUS Status
= EFI_SUCCESS
;
653 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
654 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
656 debug("ventoy_connect_driver <%s>...", DrvName
);
658 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
659 NULL
, &Count
, &Handles
);
660 if (EFI_ERROR(Status
))
665 for (i
= 0; i
< Count
; i
++)
667 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
668 if (EFI_ERROR(Status
))
673 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
674 if (EFI_ERROR(Status
) || NULL
== DriverName
)
679 if (StrStr(DriverName
, DrvName
))
681 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
682 DrvHandles
[0] = Handles
[i
];
689 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
690 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
694 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
700 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
701 NULL
, &Count
, &Handles
);
702 if (EFI_ERROR(Status
))
707 for (i
= 0; i
< Count
; i
++)
709 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
710 if (EFI_ERROR(Status
))
715 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
716 if (EFI_ERROR(Status
))
721 if (StrStr(DriverName
, DrvName
))
723 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
724 DrvHandles
[0] = Handles
[i
];
731 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
732 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
736 Status
= EFI_NOT_FOUND
;
744 EFI_STATUS EFIAPI ventoy_block_io_read_512
746 IN EFI_BLOCK_IO_PROTOCOL
*This
,
755 UINT8
*CurBuf
= NULL
;
756 EFI_STATUS Status
= EFI_SUCCESS
;
758 debug("ventoy_block_io_read_512 %lu %lu Buffer:%p\n", Lba
, BufferSize
/ 512, Buffer
);
760 CurBuf
= (UINT8
*)Buffer
;
765 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
767 if (BufferSize
<= (4 - Mod
) * 512)
769 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
774 ReadSize
= (4 - Mod
) * 512;
775 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
778 BufferSize
-= ReadSize
;
782 if (BufferSize
>= 2048)
784 ReadSize
= BufferSize
/ 2048 * 2048;
786 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
789 Lba
+= ReadSize
/ 512;
790 BufferSize
-= ReadSize
;
795 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
796 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
802 EFI_STATUS EFIAPI ventoy_block_io_write_512
804 IN EFI_BLOCK_IO_PROTOCOL
*This
,
813 UINT8
*CurBuf
= NULL
;
814 EFI_STATUS Status
= EFI_SUCCESS
;
816 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
818 CurBuf
= (UINT8
*)Buffer
;
823 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
825 if (BufferSize
<= (4 - Mod
) * 512)
827 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
828 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
832 ReadSize
= (4 - Mod
) * 512;
833 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
834 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
838 BufferSize
-= ReadSize
;
842 if (BufferSize
>= 2048)
844 ReadSize
= BufferSize
/ 2048 * 2048;
846 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
849 Lba
+= ReadSize
/ 512;
850 BufferSize
-= ReadSize
;
855 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
857 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
858 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
864 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
866 EFI_STATUS Status
= EFI_SUCCESS
;
867 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
869 ventoy_fill_device_path();
871 debug("install block io protocol %p", ImageHandle
);
872 ventoy_debug_pause();
876 gBlockData
.Media
.BlockSize
= 512;
877 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
881 gBlockData
.Media
.BlockSize
= 2048;
882 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
885 gBlockData
.Media
.ReadOnly
= TRUE
;
886 gBlockData
.Media
.MediaPresent
= 1;
887 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
889 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
890 pBlockIo
->Media
= &(gBlockData
.Media
);
891 pBlockIo
->Reset
= ventoy_block_io_reset
;
895 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
896 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
897 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
898 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
902 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
903 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
906 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
908 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
909 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
910 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
912 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
913 if (EFI_ERROR(Status
))
918 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
919 debug("Connect disk IO driver %r", Status
);
921 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
922 debug("Connect partition driver %r", Status
);
923 if (EFI_ERROR(Status
))
925 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
926 debug("Connect all controller %r", Status
);
929 ventoy_debug_pause();
935 /* For file replace */
938 STATIC EFI_STATUS EFIAPI
939 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
949 STATIC EFI_STATUS EFIAPI
950 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
952 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
955 STATIC EFI_STATUS EFIAPI
956 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
962 STATIC EFI_STATUS EFIAPI
963 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
968 STATIC EFI_STATUS EFIAPI
969 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
976 STATIC EFI_STATUS EFIAPI
977 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
985 STATIC EFI_STATUS EFIAPI
986 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
992 return EFI_WRITE_PROTECTED
;
995 STATIC EFI_STATUS EFIAPI
996 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
998 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
1002 STATIC EFI_STATUS EFIAPI
1003 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
1010 STATIC EFI_STATUS EFIAPI
1011 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
1015 if (Position
<= g_efi_file_replace
.FileSizeBytes
)
1017 g_efi_file_replace
.CurPos
= Position
;
1021 g_efi_file_replace
.CurPos
= g_efi_file_replace
.FileSizeBytes
;
1027 STATIC EFI_STATUS EFIAPI
1028 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
1032 *Position
= g_efi_file_replace
.CurPos
;
1038 STATIC EFI_STATUS EFIAPI
1039 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
1041 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
1043 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
1045 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
1047 return EFI_INVALID_PARAMETER
;
1053 return EFI_BUFFER_TOO_SMALL
;
1056 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
1058 Info
->Size
= sizeof(EFI_FILE_INFO
);
1059 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
1060 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
1061 Info
->Attribute
= EFI_FILE_READ_ONLY
;
1062 //Info->FileName = EFI_FILE_READ_ONLY;
1069 STATIC EFI_STATUS EFIAPI
1070 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1073 UINTN ReadLen
= *Len
;
1077 debug("ventoy_wrapper_file_read ... %u", *Len
);
1079 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
1081 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
1084 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
1086 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1090 g_efi_file_replace
.CurPos
+= ReadLen
;
1095 STATIC EFI_STATUS EFIAPI
1096 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1098 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1101 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
1103 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1104 File
->Open
= ventoy_wrapper_fs_open
;
1105 File
->Close
= ventoy_wrapper_file_close
;
1106 File
->Delete
= ventoy_wrapper_file_delete
;
1107 File
->Read
= ventoy_wrapper_file_read
;
1108 File
->Write
= ventoy_wrapper_file_write
;
1109 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1110 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1111 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1112 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1113 File
->Flush
= ventoy_wrapper_file_flush
;
1114 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1115 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1116 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1117 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
1122 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1124 EFI_FILE_HANDLE This
,
1125 EFI_FILE_HANDLE
*New
,
1134 EFI_STATUS Status
= EFI_SUCCESS
;
1136 ventoy_virt_chunk
*virt
= NULL
;
1138 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1140 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1141 if (EFI_ERROR(Status
))
1146 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1147 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1149 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1150 for (j
= 0; j
< 4; j
++)
1152 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1154 g_original_fclose(*New
);
1155 *New
= &g_efi_file_replace
.WrapperHandle
;
1156 ventoy_wrapper_file_procotol(*New
);
1158 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1160 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1162 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1163 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1167 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1168 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1176 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1178 (*New
)->Open
= ventoy_wrapper_file_open
;
1185 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1187 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1188 OUT EFI_FILE_PROTOCOL
**Root
1191 EFI_STATUS Status
= EFI_SUCCESS
;
1193 Status
= g_original_open_volume(This
, Root
);
1194 if (!EFI_ERROR(Status
))
1196 g_original_fopen
= (*Root
)->Open
;
1197 g_original_fclose
= (*Root
)->Close
;
1198 (*Root
)->Open
= ventoy_wrapper_file_open
;
1204 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1206 g_original_open_volume
= OpenVolume
;
1211 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1214 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1216 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1217 OUT EFI_KEY_DATA
*KeyData
1220 /* only hook once before BCD file read */
1221 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1223 g_keyboard_hook_count
++;
1225 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1226 KeyData
->Key
.UnicodeChar
= 0;
1227 KeyData
->KeyState
.KeyShiftState
= 0;
1228 KeyData
->KeyState
.KeyToggleState
= 0;
1233 return g_org_read_key_ex(This
, KeyData
);
1236 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1238 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1239 OUT EFI_INPUT_KEY
*Key
1242 /* only hook once before BCD file read */
1243 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1245 g_keyboard_hook_count
++;
1247 Key
->ScanCode
= SCAN_DELETE
;
1248 Key
->UnicodeChar
= 0;
1252 return g_org_read_key(This
, Key
);
1255 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1257 g_blockio_start_record_bcd
= TRUE
;
1258 g_blockio_bcd_read_done
= FALSE
;
1259 g_keyboard_hook_count
= 0;
1261 if (g_con_simple_input_ex
)
1263 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1264 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1267 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1268 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1273 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1275 g_blockio_start_record_bcd
= FALSE
;
1276 g_blockio_bcd_read_done
= FALSE
;
1277 g_keyboard_hook_count
= 0;
1279 if (g_con_simple_input_ex
)
1281 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1284 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1290 /* Fixup the 1st cdrom influnce for Windows boot */
1293 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1295 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1296 IN EFI_GUID
*Protocol
, OPTIONAL
1297 IN VOID
*SearchKey
, OPTIONAL
1298 IN OUT UINTN
*BufferSize
,
1299 OUT EFI_HANDLE
*Buffer
1303 EFI_HANDLE Handle
= NULL
;
1304 EFI_STATUS Status
= EFI_SUCCESS
;
1306 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1308 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1310 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1312 if (Buffer
[i
] == gBlockData
.Handle
)
1315 Buffer
[0] = Buffer
[i
];
1325 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1327 g_org_locate_handle
= gBS
->LocateHandle
;
1328 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1333 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1335 gBS
->LocateHandle
= g_org_locate_handle
;
1336 g_org_locate_handle
= NULL
;