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 debug("XXX block_io_read_real sector:%u count:%u Buffer:%p", (UINT32
)Lba
, (UINT32
)BufferSize
/ 2048, Buffer
);
488 if (secNum
> g_sector_flag_num
)
490 cur_flag
= AllocatePool(secNum
* sizeof(ventoy_sector_flag
));
491 if (NULL
== cur_flag
)
493 return EFI_OUT_OF_RESOURCES
;
496 FreePool(g_sector_flag
);
497 g_sector_flag
= cur_flag
;
498 g_sector_flag_num
= secNum
;
501 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
504 for (node
= g_virt_chunk
, i
= 0; i
< g_virt_chunk_num
; i
++, node
++)
506 if (curlba
>= node
->mem_sector_start
&& curlba
< node
->mem_sector_end
)
508 CopyMem((UINT8
*)Buffer
+ j
* 2048,
509 (char *)g_virt_chunk
+ node
->mem_sector_offset
+ (curlba
- node
->mem_sector_start
) * 2048,
514 else if (curlba
>= node
->remap_sector_start
&& curlba
< node
->remap_sector_end
)
516 cur_flag
->remap_lba
= node
->org_sector_start
+ curlba
- node
->remap_sector_start
;
523 for (curlba
= Lba
, cur_flag
= g_sector_flag
, j
= 0; j
< secNum
; j
++, curlba
++, cur_flag
++)
525 if (cur_flag
->flag
== 2)
529 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
530 lastlba
= cur_flag
->remap_lba
;
533 else if (lastlba
+ lbacount
== cur_flag
->remap_lba
)
539 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
540 lastbuffer
= (UINT8
*)Buffer
+ j
* 2048;
541 lastlba
= cur_flag
->remap_lba
;
549 ventoy_read_iso_sector(lastlba
, lbacount
, lastbuffer
);
555 EFI_STATUS EFIAPI ventoy_block_io_read
557 IN EFI_BLOCK_IO_PROTOCOL
*This
,
566 EFI_STATUS Status
= EFI_OUT_OF_RESOURCES
;
568 if (gBlockData
.pRawBlockIo
&& gBlockData
.pRawBlockIo
->Media
)
570 IoAlign
= gBlockData
.pRawBlockIo
->Media
->IoAlign
;
573 if ((IoAlign
== 0) || (((UINTN
) Buffer
& (IoAlign
- 1)) == 0))
575 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, Buffer
);
579 NewBuf
= AllocatePages(EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
582 Status
= ventoy_block_io_read_real(This
, MediaId
, Lba
, BufferSize
, NewBuf
);
583 CopyMem(Buffer
, NewBuf
, BufferSize
);
584 FreePages(NewBuf
, EFI_SIZE_TO_PAGES(BufferSize
+ IoAlign
));
591 EFI_STATUS EFIAPI ventoy_block_io_write
593 IN EFI_BLOCK_IO_PROTOCOL
*This
,
608 return EFI_WRITE_PROTECTED
;
611 secNum
= BufferSize
/ 2048;
614 return ventoy_write_iso_sector(Lba
, secNum
, Buffer
);
617 EFI_STATUS EFIAPI
ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL
*This
)
624 EFI_STATUS EFIAPI
ventoy_fill_device_path(VOID
)
627 UINT8 TmpBuf
[128] = {0};
628 VENDOR_DEVICE_PATH
*venPath
= NULL
;
630 venPath
= (VENDOR_DEVICE_PATH
*)TmpBuf
;
631 NameLen
= StrSize(VTOY_BLOCK_DEVICE_PATH_NAME
);
632 venPath
->Header
.Type
= HARDWARE_DEVICE_PATH
;
633 venPath
->Header
.SubType
= HW_VENDOR_DP
;
634 venPath
->Header
.Length
[0] = sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
635 venPath
->Header
.Length
[1] = 0;
636 CopyMem(&venPath
->Guid
, &gVtoyBlockDevicePathGuid
, sizeof(EFI_GUID
));
637 CopyMem(venPath
+ 1, VTOY_BLOCK_DEVICE_PATH_NAME
, NameLen
);
639 gBlockData
.Path
= AppendDevicePathNode(NULL
, (EFI_DEVICE_PATH_PROTOCOL
*)TmpBuf
);
640 gBlockData
.DevicePathCompareLen
= sizeof(VENDOR_DEVICE_PATH
) + NameLen
;
642 debug("gBlockData.Path=<%s>\n", ConvertDevicePathToText(gBlockData
.Path
, FALSE
, FALSE
));
647 EFI_STATUS EFIAPI
ventoy_connect_driver(IN EFI_HANDLE ControllerHandle
, IN CONST CHAR16
*DrvName
)
651 CHAR16
*DriverName
= NULL
;
652 EFI_HANDLE
*Handles
= NULL
;
653 EFI_HANDLE DrvHandles
[2] = { NULL
};
654 EFI_STATUS Status
= EFI_SUCCESS
;
655 EFI_COMPONENT_NAME_PROTOCOL
*NameProtocol
= NULL
;
656 EFI_COMPONENT_NAME2_PROTOCOL
*Name2Protocol
= NULL
;
658 debug("ventoy_connect_driver <%s>...", DrvName
);
660 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentName2ProtocolGuid
,
661 NULL
, &Count
, &Handles
);
662 if (EFI_ERROR(Status
))
667 for (i
= 0; i
< Count
; i
++)
669 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentName2ProtocolGuid
, (VOID
**)&Name2Protocol
);
670 if (EFI_ERROR(Status
))
675 Status
= Name2Protocol
->GetDriverName(Name2Protocol
, "en", &DriverName
);
676 if (EFI_ERROR(Status
) || NULL
== DriverName
)
681 if (StrStr(DriverName
, DrvName
))
683 debug("Find driver name2:<%s>: <%s>", DriverName
, DrvName
);
684 DrvHandles
[0] = Handles
[i
];
691 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
692 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
696 debug("%s NOT found, now try COMPONENT_NAME", DrvName
);
702 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiComponentNameProtocolGuid
,
703 NULL
, &Count
, &Handles
);
704 if (EFI_ERROR(Status
))
709 for (i
= 0; i
< Count
; i
++)
711 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiComponentNameProtocolGuid
, (VOID
**)&NameProtocol
);
712 if (EFI_ERROR(Status
))
717 Status
= NameProtocol
->GetDriverName(NameProtocol
, "en", &DriverName
);
718 if (EFI_ERROR(Status
))
723 if (StrStr(DriverName
, DrvName
))
725 debug("Find driver name:<%s>: <%s>", DriverName
, DrvName
);
726 DrvHandles
[0] = Handles
[i
];
733 Status
= gBS
->ConnectController(ControllerHandle
, DrvHandles
, NULL
, TRUE
);
734 debug("ventoy_connect_driver:<%s> <%r>", DrvName
, Status
);
738 Status
= EFI_NOT_FOUND
;
746 EFI_STATUS EFIAPI ventoy_block_io_read_512
748 IN EFI_BLOCK_IO_PROTOCOL
*This
,
757 UINT8
*CurBuf
= NULL
;
758 EFI_STATUS Status
= EFI_SUCCESS
;
760 debug("ventoy_block_io_read_512 %lu %lu Buffer:%p\n", Lba
, BufferSize
/ 512, Buffer
);
762 CurBuf
= (UINT8
*)Buffer
;
767 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
769 if (BufferSize
<= (4 - Mod
) * 512)
771 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, BufferSize
);
776 ReadSize
= (4 - Mod
) * 512;
777 CopyMem(CurBuf
, g_sector_buf
+ Mod
* 512, ReadSize
);
780 BufferSize
-= ReadSize
;
784 if (BufferSize
>= 2048)
786 ReadSize
= BufferSize
/ 2048 * 2048;
788 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
791 Lba
+= ReadSize
/ 512;
792 BufferSize
-= ReadSize
;
797 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
798 CopyMem(CurBuf
, g_sector_buf
, BufferSize
);
804 EFI_STATUS EFIAPI ventoy_block_io_write_512
806 IN EFI_BLOCK_IO_PROTOCOL
*This
,
815 UINT8
*CurBuf
= NULL
;
816 EFI_STATUS Status
= EFI_SUCCESS
;
818 debug("ventoy_block_io_write_512 %lu %lu\n", Lba
, BufferSize
/ 512);
820 CurBuf
= (UINT8
*)Buffer
;
825 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
827 if (BufferSize
<= (4 - Mod
) * 512)
829 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, BufferSize
);
830 return g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
834 ReadSize
= (4 - Mod
) * 512;
835 CopyMem(g_sector_buf
+ Mod
* 512, CurBuf
, ReadSize
);
836 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
840 BufferSize
-= ReadSize
;
844 if (BufferSize
>= 2048)
846 ReadSize
= BufferSize
/ 2048 * 2048;
848 Status
|= g_sector_2048_write(This
, MediaId
, Lba
/ 4, ReadSize
, CurBuf
);
851 Lba
+= ReadSize
/ 512;
852 BufferSize
-= ReadSize
;
857 Status
|= g_sector_2048_read(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
859 CopyMem(g_sector_buf
, CurBuf
, BufferSize
);
860 g_sector_2048_write(This
, MediaId
, Lba
/ 4, 2048, g_sector_buf
);
866 EFI_STATUS EFIAPI
ventoy_install_blockio(IN EFI_HANDLE ImageHandle
, IN UINT64 ImgSize
)
868 EFI_STATUS Status
= EFI_SUCCESS
;
869 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
= &(gBlockData
.BlockIo
);
871 ventoy_fill_device_path();
873 debug("install block io protocol %p", ImageHandle
);
874 ventoy_debug_pause();
878 gBlockData
.Media
.BlockSize
= 512;
879 gBlockData
.Media
.LastBlock
= ImgSize
/ 512 - 1;
883 gBlockData
.Media
.BlockSize
= 2048;
884 gBlockData
.Media
.LastBlock
= ImgSize
/ 2048 - 1;
887 gBlockData
.Media
.ReadOnly
= TRUE
;
888 gBlockData
.Media
.MediaPresent
= 1;
889 gBlockData
.Media
.LogicalBlocksPerPhysicalBlock
= 1;
891 pBlockIo
->Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
892 pBlockIo
->Media
= &(gBlockData
.Media
);
893 pBlockIo
->Reset
= ventoy_block_io_reset
;
897 g_sector_2048_read
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
898 g_sector_2048_write
= gMemdiskMode
? ventoy_block_io_ramdisk_write
: ventoy_block_io_write
;
899 pBlockIo
->ReadBlocks
= ventoy_block_io_read_512
;
900 pBlockIo
->WriteBlocks
= ventoy_block_io_write_512
;
904 pBlockIo
->ReadBlocks
= gMemdiskMode
? ventoy_block_io_ramdisk_read
: ventoy_block_io_read
;
905 pBlockIo
->WriteBlocks
= ventoy_block_io_write
;
908 pBlockIo
->FlushBlocks
= ventoy_block_io_flush
;
910 Status
= gBS
->InstallMultipleProtocolInterfaces(&gBlockData
.Handle
,
911 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
912 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
914 debug("Install protocol %r %p", Status
, gBlockData
.Handle
);
915 if (EFI_ERROR(Status
))
920 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Disk I/O Driver");
921 debug("Connect disk IO driver %r", Status
);
923 Status
= ventoy_connect_driver(gBlockData
.Handle
, L
"Partition Driver");
924 debug("Connect partition driver %r", Status
);
925 if (EFI_ERROR(Status
))
927 Status
= gBS
->ConnectController(gBlockData
.Handle
, NULL
, NULL
, TRUE
);
928 debug("Connect all controller %r", Status
);
931 ventoy_debug_pause();
937 /* For file replace */
940 STATIC EFI_STATUS EFIAPI
941 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
)
951 STATIC EFI_STATUS EFIAPI
952 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This
, EFI_FILE_HANDLE
*New
, CHAR16
*Name
, UINT64 Mode
, UINT64 Attributes
, EFI_FILE_IO_TOKEN
*Token
)
954 return ventoy_wrapper_fs_open(This
, New
, Name
, Mode
, Attributes
);
957 STATIC EFI_STATUS EFIAPI
958 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This
)
964 STATIC EFI_STATUS EFIAPI
965 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN Len
, VOID
*Data
)
970 STATIC EFI_STATUS EFIAPI
971 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This
)
978 STATIC EFI_STATUS EFIAPI
979 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This
, EFI_FILE_IO_TOKEN
*Token
)
987 STATIC EFI_STATUS EFIAPI
988 ventoy_wrapper_file_write(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
994 return EFI_WRITE_PROTECTED
;
997 STATIC EFI_STATUS EFIAPI
998 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1000 return ventoy_wrapper_file_write(This
, &(Token
->BufferSize
), Token
->Buffer
);
1004 STATIC EFI_STATUS EFIAPI
1005 ventoy_wrapper_file_close(EFI_FILE_HANDLE This
)
1012 STATIC EFI_STATUS EFIAPI
1013 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This
, UINT64 Position
)
1017 if (Position
<= g_efi_file_replace
.FileSizeBytes
)
1019 g_efi_file_replace
.CurPos
= Position
;
1023 g_efi_file_replace
.CurPos
= g_efi_file_replace
.FileSizeBytes
;
1029 STATIC EFI_STATUS EFIAPI
1030 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This
, UINT64
*Position
)
1034 *Position
= g_efi_file_replace
.CurPos
;
1040 STATIC EFI_STATUS EFIAPI
1041 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This
, EFI_GUID
*Type
, UINTN
*Len
, VOID
*Data
)
1043 EFI_FILE_INFO
*Info
= (EFI_FILE_INFO
*) Data
;
1045 debug("ventoy_wrapper_file_get_info ... %u", *Len
);
1047 if (!CompareGuid(Type
, &gEfiFileInfoGuid
))
1049 return EFI_INVALID_PARAMETER
;
1055 return EFI_BUFFER_TOO_SMALL
;
1058 ZeroMem(Data
, sizeof(EFI_FILE_INFO
));
1060 Info
->Size
= sizeof(EFI_FILE_INFO
);
1061 Info
->FileSize
= g_efi_file_replace
.FileSizeBytes
;
1062 Info
->PhysicalSize
= g_efi_file_replace
.FileSizeBytes
;
1063 Info
->Attribute
= EFI_FILE_READ_ONLY
;
1064 //Info->FileName = EFI_FILE_READ_ONLY;
1071 STATIC EFI_STATUS EFIAPI
1072 ventoy_wrapper_file_read(EFI_FILE_HANDLE This
, UINTN
*Len
, VOID
*Data
)
1075 UINTN ReadLen
= *Len
;
1079 debug("ventoy_wrapper_file_read ... %u", *Len
);
1081 if (g_efi_file_replace
.CurPos
+ ReadLen
> g_efi_file_replace
.FileSizeBytes
)
1083 ReadLen
= g_efi_file_replace
.FileSizeBytes
- g_efi_file_replace
.CurPos
;
1086 Lba
= g_efi_file_replace
.CurPos
/ 2048 + g_efi_file_replace
.BlockIoSectorStart
;
1088 ventoy_block_io_read(NULL
, 0, Lba
, ReadLen
, Data
);
1092 g_efi_file_replace
.CurPos
+= ReadLen
;
1097 STATIC EFI_STATUS EFIAPI
1098 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL
*This
, IN OUT EFI_FILE_IO_TOKEN
*Token
)
1100 return ventoy_wrapper_file_read(This
, &(Token
->BufferSize
), Token
->Buffer
);
1103 STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL
*File
)
1105 File
->Revision
= EFI_FILE_PROTOCOL_REVISION2
;
1106 File
->Open
= ventoy_wrapper_fs_open
;
1107 File
->Close
= ventoy_wrapper_file_close
;
1108 File
->Delete
= ventoy_wrapper_file_delete
;
1109 File
->Read
= ventoy_wrapper_file_read
;
1110 File
->Write
= ventoy_wrapper_file_write
;
1111 File
->GetPosition
= ventoy_wrapper_file_get_pos
;
1112 File
->SetPosition
= ventoy_wrapper_file_set_pos
;
1113 File
->GetInfo
= ventoy_wrapper_file_get_info
;
1114 File
->SetInfo
= ventoy_wrapper_file_set_info
;
1115 File
->Flush
= ventoy_wrapper_file_flush
;
1116 File
->OpenEx
= ventoy_wrapper_file_open_ex
;
1117 File
->ReadEx
= ventoy_wrapper_file_read_ex
;
1118 File
->WriteEx
= ventoy_wrapper_file_write_ex
;
1119 File
->FlushEx
= ventoy_wrapper_file_flush_ex
;
1124 STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
1126 EFI_FILE_HANDLE This
,
1127 EFI_FILE_HANDLE
*New
,
1136 EFI_STATUS Status
= EFI_SUCCESS
;
1138 ventoy_virt_chunk
*virt
= NULL
;
1140 debug("## ventoy_wrapper_file_open <%s> ", Name
);
1142 Status
= g_original_fopen(This
, New
, Name
, Mode
, Attributes
);
1143 if (EFI_ERROR(Status
))
1148 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
&&
1149 g_file_replace_list
->new_file_virtual_id
< g_virt_chunk_num
)
1151 AsciiSPrint(TmpName
, sizeof(TmpName
), "%s", Name
);
1152 for (j
= 0; j
< 4; j
++)
1154 if (0 == AsciiStrCmp(g_file_replace_list
[i
].old_file_name
[j
], TmpName
))
1156 g_original_fclose(*New
);
1157 *New
= &g_efi_file_replace
.WrapperHandle
;
1158 ventoy_wrapper_file_procotol(*New
);
1160 virt
= g_virt_chunk
+ g_file_replace_list
->new_file_virtual_id
;
1162 Sectors
= (virt
->mem_sector_end
- virt
->mem_sector_start
) + (virt
->remap_sector_end
- virt
->remap_sector_start
);
1164 g_efi_file_replace
.BlockIoSectorStart
= virt
->mem_sector_start
;
1165 g_efi_file_replace
.FileSizeBytes
= Sectors
* 2048;
1169 debug("## ventoy_wrapper_file_open <%s> BlockStart:%lu Sectors:%lu Bytes:%lu", Name
,
1170 g_efi_file_replace
.BlockIoSectorStart
, Sectors
, Sectors
* 2048);
1178 if (StrCmp(Name
, L
"\\EFI\\BOOT") == 0)
1180 (*New
)->Open
= ventoy_wrapper_file_open
;
1187 EFI_STATUS EFIAPI ventoy_wrapper_open_volume
1189 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
1190 OUT EFI_FILE_PROTOCOL
**Root
1193 EFI_STATUS Status
= EFI_SUCCESS
;
1195 Status
= g_original_open_volume(This
, Root
);
1196 if (!EFI_ERROR(Status
))
1198 g_original_fopen
= (*Root
)->Open
;
1199 g_original_fclose
= (*Root
)->Close
;
1200 (*Root
)->Open
= ventoy_wrapper_file_open
;
1206 EFI_STATUS EFIAPI
ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume
)
1208 g_original_open_volume
= OpenVolume
;
1213 /* For auto skip Windows 'Press any key to boot from CD or DVD ...' */
1216 STATIC EFI_STATUS EFIAPI ventoy_wrapper_read_key_ex
1218 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1219 OUT EFI_KEY_DATA
*KeyData
1222 /* only hook once before BCD file read */
1223 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1225 g_keyboard_hook_count
++;
1227 KeyData
->Key
.ScanCode
= SCAN_DELETE
;
1228 KeyData
->Key
.UnicodeChar
= 0;
1229 KeyData
->KeyState
.KeyShiftState
= 0;
1230 KeyData
->KeyState
.KeyToggleState
= 0;
1235 return g_org_read_key_ex(This
, KeyData
);
1238 EFI_STATUS EFIAPI ventoy_wrapper_read_key
1240 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1241 OUT EFI_INPUT_KEY
*Key
1244 /* only hook once before BCD file read */
1245 if (g_keyboard_hook_count
== 0 && g_blockio_bcd_read_done
== FALSE
)
1247 g_keyboard_hook_count
++;
1249 Key
->ScanCode
= SCAN_DELETE
;
1250 Key
->UnicodeChar
= 0;
1254 return g_org_read_key(This
, Key
);
1257 EFI_STATUS
ventoy_hook_keyboard_start(VOID
)
1259 g_blockio_start_record_bcd
= TRUE
;
1260 g_blockio_bcd_read_done
= FALSE
;
1261 g_keyboard_hook_count
= 0;
1263 if (g_con_simple_input_ex
)
1265 g_org_read_key_ex
= g_con_simple_input_ex
->ReadKeyStrokeEx
;
1266 g_con_simple_input_ex
->ReadKeyStrokeEx
= ventoy_wrapper_read_key_ex
;
1269 g_org_read_key
= gST
->ConIn
->ReadKeyStroke
;
1270 gST
->ConIn
->ReadKeyStroke
= ventoy_wrapper_read_key
;
1275 EFI_STATUS
ventoy_hook_keyboard_stop(VOID
)
1277 g_blockio_start_record_bcd
= FALSE
;
1278 g_blockio_bcd_read_done
= FALSE
;
1279 g_keyboard_hook_count
= 0;
1281 if (g_con_simple_input_ex
)
1283 g_con_simple_input_ex
->ReadKeyStrokeEx
= g_org_read_key_ex
;
1286 gST
->ConIn
->ReadKeyStroke
= g_org_read_key
;
1292 /* Fixup the 1st cdrom influnce for Windows boot */
1295 STATIC EFI_STATUS EFIAPI ventoy_wrapper_locate_handle
1297 IN EFI_LOCATE_SEARCH_TYPE SearchType
,
1298 IN EFI_GUID
*Protocol
, OPTIONAL
1299 IN VOID
*SearchKey
, OPTIONAL
1300 IN OUT UINTN
*BufferSize
,
1301 OUT EFI_HANDLE
*Buffer
1305 EFI_HANDLE Handle
= NULL
;
1306 EFI_STATUS Status
= EFI_SUCCESS
;
1308 Status
= g_org_locate_handle(SearchType
, Protocol
, SearchKey
, BufferSize
, Buffer
);
1310 if (EFI_SUCCESS
== Status
&& Protocol
&& CompareGuid(&gEfiBlockIoProtocolGuid
, Protocol
))
1312 for (i
= 0; i
< (*BufferSize
) / sizeof(EFI_HANDLE
); i
++)
1314 if (Buffer
[i
] == gBlockData
.Handle
)
1317 Buffer
[0] = Buffer
[i
];
1327 EFI_STATUS
ventoy_hook_1st_cdrom_start(VOID
)
1329 g_org_locate_handle
= gBS
->LocateHandle
;
1330 gBS
->LocateHandle
= ventoy_wrapper_locate_handle
;
1335 EFI_STATUS
ventoy_hook_1st_cdrom_stop(VOID
)
1337 gBS
->LocateHandle
= g_org_locate_handle
;
1338 g_org_locate_handle
= NULL
;