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 BOOLEAN gDebugPrint
= FALSE
;
40 ventoy_ram_disk g_ramdisk_param
;
41 ventoy_chain_head
*g_chain
;
42 ventoy_img_chunk
*g_chunk
;
43 UINT8
*g_os_param_reserved
;
44 UINT32 g_img_chunk_num
;
45 ventoy_override_chunk
*g_override_chunk
;
46 UINT32 g_override_chunk_num
;
47 ventoy_virt_chunk
*g_virt_chunk
;
48 UINT32 g_virt_chunk_num
;
49 vtoy_block_data gBlockData
;
50 static grub_env_get_pf grub_env_get
= NULL
;
52 ventoy_grub_param_file_replace
*g_file_replace_list
= NULL
;
53 ventoy_efi_file_replace g_efi_file_replace
;
55 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
57 CHAR16 gFirstTryBootFile
[256] = {0};
60 UINTN gBootFileStartIndex
= 1;
61 CONST CHAR16
*gEfiBootFileName
[] =
64 EFI_REMOVABLE_MEDIA_FILE_NAME
,
65 L
"\\EFI\\BOOT\\GRUBX64.EFI",
66 L
"\\EFI\\BOOT\\BOOTx64.EFI",
67 L
"\\EFI\\BOOT\\bootx64.efi",
68 L
"\\efi\\boot\\bootx64.efi",
71 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
76 VA_START (Marker
, Format
);
77 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
80 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
83 VOID EFIAPI
ventoy_clear_input(VOID
)
87 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
88 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
92 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
95 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
100 ventoy_img_chunk
*chunk
;
102 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
104 debug("##################### ventoy_dump_img_chunk #######################");
106 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
108 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
109 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
110 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
112 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
117 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
120 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
122 debug("image chunk size check success");
126 debug("image chunk size check failed %d", errcnt
);
129 ventoy_debug_pause();
132 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
135 ventoy_override_chunk
*chunk
;
137 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
139 debug("##################### ventoy_dump_override_chunk #######################");
141 for (i
= 0; i
< g_override_chunk_num
; i
++)
143 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
146 ventoy_debug_pause();
149 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
152 ventoy_virt_chunk
*node
;
154 debug("##################### ventoy_dump_virt_chunk #######################");
155 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
156 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
158 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
159 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
161 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
162 node
->mem_sector_start
,
163 node
->mem_sector_end
,
164 node
->mem_sector_offset
,
165 node
->remap_sector_start
,
166 node
->remap_sector_end
,
167 node
->org_sector_start
);
170 ventoy_debug_pause();
173 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
179 guid
= chain
->os_param
.vtoy_disk_guid
;
180 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
182 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
185 debug("##################### ventoy_dump_chain #######################");
187 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
188 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
189 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
190 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
191 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
192 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
193 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
194 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
195 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
196 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
197 g_os_param_reserved
[0],
198 g_os_param_reserved
[1],
199 g_os_param_reserved
[2],
200 g_os_param_reserved
[3],
201 g_os_param_reserved
[4]
204 ventoy_debug_pause();
206 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
207 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
208 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
209 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
210 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
211 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
212 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
213 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
214 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
216 ventoy_debug_pause();
218 ventoy_dump_img_chunk(chain
);
219 ventoy_dump_override_chunk(chain
);
220 ventoy_dump_virt_chunk(chain
);
223 static int ventoy_update_image_location(ventoy_os_param
*param
)
225 EFI_STATUS Status
= EFI_SUCCESS
;
231 ventoy_image_location
*location
= NULL
;
232 ventoy_image_disk_region
*region
= NULL
;
233 ventoy_img_chunk
*chunk
= g_chunk
;
235 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
237 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
238 if (EFI_ERROR(Status
) || NULL
== buffer
)
240 debug("Failed to allocate runtime pool %r\n", Status
);
244 address
= (UINTN
)buffer
;
248 address
+= 4096 - (address
% 4096);
252 param
->vtoy_img_location_addr
= address
;
253 param
->vtoy_img_location_len
= length
;
255 /* update check sum */
256 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
258 chksum
+= *((UINT8
*)param
+ i
);
260 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
262 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
263 if (NULL
== location
)
268 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
269 location
->image_sector_size
= 2048;
270 location
->disk_sector_size
= g_chain
->disk_sector_size
;
271 location
->region_count
= g_img_chunk_num
;
273 region
= location
->regions
;
275 for (i
= 0; i
< g_img_chunk_num
; i
++)
277 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
278 region
->image_start_sector
= chunk
->img_start_sector
;
279 region
->disk_start_sector
= chunk
->disk_start_sector
;
287 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
289 EFI_HANDLE Handle
= NULL
;
290 EFI_STATUS Status
= EFI_SUCCESS
;
291 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
292 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
293 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
295 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
301 pCurNode
= pTmpDevPath
;
302 while (!IsDevicePathEnd(pCurNode
))
304 pLastNode
= pCurNode
;
305 pCurNode
= NextDevicePathNode(pCurNode
);
309 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
312 pCurNode
= pTmpDevPath
;
313 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
314 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
316 FreePool(pTmpDevPath
);
321 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
323 EFI_STATUS Status
= EFI_SUCCESS
;
324 EFI_GUID VarGuid
= VENTOY_GUID
;
326 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
327 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
328 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
329 debug("set efi variable %r", Status
);
334 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
336 EFI_STATUS Status
= EFI_SUCCESS
;
337 EFI_GUID VarGuid
= VENTOY_GUID
;
339 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
340 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
342 debug("delete efi variable %r", Status
);
348 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
350 EFI_STATUS Status
= EFI_SUCCESS
;
351 EFI_GUID VarGuid
= VENTOY_GUID
;
353 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
354 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
355 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
356 debug("set efi variable %r", Status
);
361 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
363 EFI_STATUS Status
= EFI_SUCCESS
;
364 EFI_GUID VarGuid
= VENTOY_GUID
;
366 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
367 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
369 debug("delete efi variable %r", Status
);
374 STATIC EFI_STATUS EFIAPI ventoy_load_image
376 IN EFI_HANDLE ImageHandle
,
377 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
378 IN CONST CHAR16
*FileName
,
379 IN UINTN FileNameLen
,
380 OUT EFI_HANDLE
*Image
383 EFI_STATUS Status
= EFI_SUCCESS
;
384 CHAR16 TmpBuf
[256] = {0};
385 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
386 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
388 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
389 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
390 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
391 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
392 pFilePath
->Header
.Length
[1] = 0;
393 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
395 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
398 return EFI_NOT_FOUND
;
401 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
403 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
411 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
416 UINT8
*pBuffer
= NULL
;
418 EFI_STATUS Status
= EFI_SUCCESS
;
419 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
421 pBuffer
= AllocatePool(2048);
424 return EFI_OUT_OF_RESOURCES
;
427 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
428 NULL
, &Count
, &Handles
);
429 if (EFI_ERROR(Status
))
435 for (i
= 0; i
< Count
; i
++)
437 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
438 if (EFI_ERROR(Status
))
443 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
444 debug("This Disk size: %llu", DiskSize
);
445 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
450 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
451 if (EFI_ERROR(Status
))
453 debug("ReadBlocks filed %r", Status
);
457 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
459 gBlockData
.RawBlockIoHandle
= Handles
[i
];
460 gBlockData
.pRawBlockIo
= pBlockIo
;
461 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
462 (VOID
**)&(gBlockData
.pDiskDevPath
),
465 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
467 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
468 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
477 return EFI_NOT_FOUND
;
485 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
492 CHAR16
*pCmdLine
= NULL
;
493 EFI_STATUS Status
= EFI_SUCCESS
;
494 ventoy_grub_param
*pGrubParam
= NULL
;
495 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
497 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
498 if (EFI_ERROR(Status
))
500 VtoyDebug("Failed to handle load image protocol %r", Status
);
504 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
505 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
506 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
508 if (StrStr(pCmdLine
, L
"debug"))
513 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
516 pPos
+= StrLen(L
"FirstTry=");
517 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
519 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
521 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
529 gEfiBootFileName
[0] = gFirstTryBootFile
;
530 gBootFileStartIndex
= 0;
533 debug("cmdline:<%s>", pCmdLine
);
535 if (gFirstTryBootFile
[0])
537 debug("First Try:<%s>", gFirstTryBootFile
);
540 pPos
= StrStr(pCmdLine
, L
"env_param=");
543 return EFI_INVALID_PARAMETER
;
546 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
547 grub_env_get
= pGrubParam
->grub_env_get
;
549 g_file_replace_list
= &pGrubParam
->file_replace
;
550 old_cnt
= g_file_replace_list
->old_file_cnt
;
551 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
552 g_file_replace_list
->magic
,
553 g_file_replace_list
->new_file_virtual_id
,
555 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
556 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
557 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
558 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
561 pPos
= StrStr(pCmdLine
, L
"mem:");
562 g_chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
564 pPos
= StrStr(pPos
, L
"size:");
565 size
= StrDecimalToUintn(pPos
+ 5);
567 debug("memory addr:%p size:%lu", g_chain
, size
);
569 if (StrStr(pCmdLine
, L
"memdisk"))
571 g_iso_buf_size
= size
;
576 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
577 g_img_chunk_num
= g_chain
->img_chunk_num
;
578 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
579 g_override_chunk_num
= g_chain
->override_chunk_num
;
580 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
581 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
583 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
585 /* Workaround for Windows & ISO9660 */
586 if (g_os_param_reserved
[2] == 1 && g_os_param_reserved
[3] == 0)
588 g_fixup_iso9660_secover_enable
= TRUE
;
591 if (g_os_param_reserved
[2] == 1 && g_os_param_reserved
[4] != 1)
593 g_hook_keyboard
= TRUE
;
596 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
598 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
600 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
605 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
608 ventoy_update_image_location(&(g_chain
->os_param
));
612 ventoy_dump_chain(g_chain
);
620 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
622 FreePool(g_sector_flag
);
623 g_sector_flag_num
= 0;
625 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
627 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
628 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
629 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
632 ventoy_delete_variable();
634 if (g_chain
->os_param
.vtoy_img_location_addr
)
636 FreePool((VOID
*)(UINTN
)g_chain
->os_param
.vtoy_img_location_addr
);
642 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
649 EFI_HANDLE Image
= NULL
;
650 EFI_HANDLE
*Handles
= NULL
;
651 EFI_STATUS Status
= EFI_SUCCESS
;
652 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
653 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
655 for (t
= 0; t
< 3; t
++)
660 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
661 NULL
, &Count
, &Handles
);
662 if (EFI_ERROR(Status
))
667 debug("ventoy_boot fs count:%u", Count
);
669 for (i
= 0; i
< Count
; i
++)
671 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
672 if (EFI_ERROR(Status
))
677 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
679 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
683 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
684 if (EFI_ERROR(Status
))
686 debug("Failed to open device path protocol %r", Status
);
690 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
691 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
693 debug("Not ventoy disk file system");
697 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
699 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
700 StrSize(gEfiBootFileName
[j
]), &Image
);
701 if (EFI_SUCCESS
== Status
)
705 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
708 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
714 debug("Find boot file, now try to boot .....");
715 ventoy_debug_pause();
719 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
722 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
724 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
725 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
730 ventoy_hook_keyboard_start();
732 /* can't add debug print here */
733 //ventoy_wrapper_system();
734 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
737 ventoy_hook_keyboard_stop();
740 if (EFI_ERROR(Status
))
742 debug("Failed to start image %r", Status
);
744 gBS
->UnloadImage(Image
);
753 debug("Fs not found, now wait and retry...");
760 return EFI_NOT_FOUND
;
766 EFI_STATUS EFIAPI VentoyEfiMain
768 IN EFI_HANDLE ImageHandle
,
769 IN EFI_SYSTEM_TABLE
*SystemTable
772 EFI_STATUS Status
= EFI_SUCCESS
;
773 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
775 g_sector_flag_num
= 512; /* initial value */
777 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
778 if (NULL
== g_sector_flag
)
780 return EFI_OUT_OF_RESOURCES
;
783 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
784 if (EFI_SUCCESS
== Status
)
786 g_con_simple_input_ex
= Protocol
;
789 gST
->ConOut
->ClearScreen(gST
->ConOut
);
790 ventoy_clear_input();
792 ventoy_parse_cmdline(ImageHandle
);
796 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_chain
;
797 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
799 ventoy_save_ramdisk_param();
801 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
802 Status
= ventoy_boot(ImageHandle
);
804 ventoy_delete_ramdisk_param();
808 ventoy_save_variable();
809 ventoy_find_iso_disk(ImageHandle
);
811 ventoy_debug_pause();
813 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
815 ventoy_debug_pause();
817 Status
= ventoy_boot(ImageHandle
);
822 if (EFI_NOT_FOUND
== Status
)
824 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
825 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
829 ventoy_clear_input();
830 gST
->ConOut
->ClearScreen(gST
->ConOut
);