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 BOOLEAN g_fix_windows_1st_cdrom_issue
= FALSE
;
57 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
59 CHAR16 gFirstTryBootFile
[256] = {0};
62 UINTN gBootFileStartIndex
= 1;
63 CONST CHAR16
*gEfiBootFileName
[] =
66 EFI_REMOVABLE_MEDIA_FILE_NAME
,
67 L
"\\EFI\\BOOT\\GRUBX64.EFI",
68 L
"\\EFI\\BOOT\\BOOTx64.EFI",
69 L
"\\EFI\\BOOT\\bootx64.efi",
70 L
"\\efi\\boot\\bootx64.efi",
73 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
78 VA_START (Marker
, Format
);
79 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
82 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
85 VOID EFIAPI
ventoy_clear_input(VOID
)
89 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
90 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
94 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
97 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
102 ventoy_img_chunk
*chunk
;
104 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
106 debug("##################### ventoy_dump_img_chunk #######################");
108 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
110 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
111 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
112 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
114 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
119 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
122 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
124 debug("image chunk size check success");
128 debug("image chunk size check failed %d", errcnt
);
131 ventoy_debug_pause();
134 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
137 ventoy_override_chunk
*chunk
;
139 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
141 debug("##################### ventoy_dump_override_chunk #######################");
143 for (i
= 0; i
< g_override_chunk_num
; i
++)
145 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
148 ventoy_debug_pause();
151 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
154 ventoy_virt_chunk
*node
;
156 debug("##################### ventoy_dump_virt_chunk #######################");
157 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
158 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
160 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
161 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
163 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
164 node
->mem_sector_start
,
165 node
->mem_sector_end
,
166 node
->mem_sector_offset
,
167 node
->remap_sector_start
,
168 node
->remap_sector_end
,
169 node
->org_sector_start
);
172 ventoy_debug_pause();
175 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
181 guid
= chain
->os_param
.vtoy_disk_guid
;
182 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
184 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
187 debug("##################### ventoy_dump_chain #######################");
189 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
190 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
191 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
192 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
193 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
194 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
195 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
196 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
197 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
198 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
199 g_os_param_reserved
[0],
200 g_os_param_reserved
[1],
201 g_os_param_reserved
[2],
202 g_os_param_reserved
[3],
203 g_os_param_reserved
[4]
206 ventoy_debug_pause();
208 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
209 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
210 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
211 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
212 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
213 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
214 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
215 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
216 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
218 ventoy_debug_pause();
220 ventoy_dump_img_chunk(chain
);
221 ventoy_dump_override_chunk(chain
);
222 ventoy_dump_virt_chunk(chain
);
225 static int ventoy_update_image_location(ventoy_os_param
*param
)
227 EFI_STATUS Status
= EFI_SUCCESS
;
233 ventoy_image_location
*location
= NULL
;
234 ventoy_image_disk_region
*region
= NULL
;
235 ventoy_img_chunk
*chunk
= g_chunk
;
237 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
239 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
240 if (EFI_ERROR(Status
) || NULL
== buffer
)
242 debug("Failed to allocate runtime pool %r\n", Status
);
246 address
= (UINTN
)buffer
;
250 address
+= 4096 - (address
% 4096);
254 param
->vtoy_img_location_addr
= address
;
255 param
->vtoy_img_location_len
= length
;
257 /* update check sum */
258 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
260 chksum
+= *((UINT8
*)param
+ i
);
262 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
264 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
265 if (NULL
== location
)
270 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
271 location
->image_sector_size
= 2048;
272 location
->disk_sector_size
= g_chain
->disk_sector_size
;
273 location
->region_count
= g_img_chunk_num
;
275 region
= location
->regions
;
277 for (i
= 0; i
< g_img_chunk_num
; i
++)
279 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
280 region
->image_start_sector
= chunk
->img_start_sector
;
281 region
->disk_start_sector
= chunk
->disk_start_sector
;
289 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
291 EFI_HANDLE Handle
= NULL
;
292 EFI_STATUS Status
= EFI_SUCCESS
;
293 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
294 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
295 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
297 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
303 pCurNode
= pTmpDevPath
;
304 while (!IsDevicePathEnd(pCurNode
))
306 pLastNode
= pCurNode
;
307 pCurNode
= NextDevicePathNode(pCurNode
);
311 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
314 pCurNode
= pTmpDevPath
;
315 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
316 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
318 FreePool(pTmpDevPath
);
323 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
325 EFI_STATUS Status
= EFI_SUCCESS
;
326 EFI_GUID VarGuid
= VENTOY_GUID
;
328 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
329 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
330 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
331 debug("set efi variable %r", Status
);
336 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
338 EFI_STATUS Status
= EFI_SUCCESS
;
339 EFI_GUID VarGuid
= VENTOY_GUID
;
341 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
342 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
344 debug("delete efi variable %r", Status
);
350 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
352 EFI_STATUS Status
= EFI_SUCCESS
;
353 EFI_GUID VarGuid
= VENTOY_GUID
;
355 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
356 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
357 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
358 debug("set efi variable %r", Status
);
363 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
365 EFI_STATUS Status
= EFI_SUCCESS
;
366 EFI_GUID VarGuid
= VENTOY_GUID
;
368 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
369 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
371 debug("delete efi variable %r", Status
);
376 STATIC EFI_STATUS EFIAPI ventoy_load_image
378 IN EFI_HANDLE ImageHandle
,
379 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
380 IN CONST CHAR16
*FileName
,
381 IN UINTN FileNameLen
,
382 OUT EFI_HANDLE
*Image
385 EFI_STATUS Status
= EFI_SUCCESS
;
386 CHAR16 TmpBuf
[256] = {0};
387 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
388 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
390 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
391 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
392 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
393 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
394 pFilePath
->Header
.Length
[1] = 0;
395 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
397 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
400 return EFI_NOT_FOUND
;
403 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
405 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
413 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
418 UINT8
*pBuffer
= NULL
;
420 EFI_STATUS Status
= EFI_SUCCESS
;
421 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
423 pBuffer
= AllocatePool(2048);
426 return EFI_OUT_OF_RESOURCES
;
429 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
430 NULL
, &Count
, &Handles
);
431 if (EFI_ERROR(Status
))
437 for (i
= 0; i
< Count
; i
++)
439 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
440 if (EFI_ERROR(Status
))
445 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
446 debug("This Disk size: %llu", DiskSize
);
447 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
452 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
453 if (EFI_ERROR(Status
))
455 debug("ReadBlocks filed %r", Status
);
459 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
461 gBlockData
.RawBlockIoHandle
= Handles
[i
];
462 gBlockData
.pRawBlockIo
= pBlockIo
;
463 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
464 (VOID
**)&(gBlockData
.pDiskDevPath
),
467 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
469 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
470 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
479 return EFI_NOT_FOUND
;
487 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
494 CHAR16
*pCmdLine
= NULL
;
495 EFI_STATUS Status
= EFI_SUCCESS
;
496 ventoy_grub_param
*pGrubParam
= NULL
;
497 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
498 ventoy_chain_head
*chain
= NULL
;
500 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
501 if (EFI_ERROR(Status
))
503 VtoyDebug("Failed to handle load image protocol %r", Status
);
507 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
508 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
509 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
511 if (StrStr(pCmdLine
, L
"debug"))
516 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
519 pPos
+= StrLen(L
"FirstTry=");
520 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
522 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
524 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
532 gEfiBootFileName
[0] = gFirstTryBootFile
;
533 gBootFileStartIndex
= 0;
536 debug("cmdline:<%s>", pCmdLine
);
538 if (gFirstTryBootFile
[0])
540 debug("First Try:<%s>", gFirstTryBootFile
);
543 pPos
= StrStr(pCmdLine
, L
"env_param=");
546 return EFI_INVALID_PARAMETER
;
549 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
550 grub_env_get
= pGrubParam
->grub_env_get
;
552 g_file_replace_list
= &pGrubParam
->file_replace
;
553 old_cnt
= g_file_replace_list
->old_file_cnt
;
554 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
555 g_file_replace_list
->magic
,
556 g_file_replace_list
->new_file_virtual_id
,
558 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
559 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
560 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
561 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
564 pPos
= StrStr(pCmdLine
, L
"mem:");
565 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
567 pPos
= StrStr(pPos
, L
"size:");
568 size
= StrDecimalToUintn(pPos
+ 5);
570 debug("memory addr:%p size:%lu", chain
, size
);
572 g_chain
= AllocatePool(size
);
573 CopyMem(g_chain
, chain
, size
);
575 if (StrStr(pCmdLine
, L
"memdisk"))
577 g_iso_buf_size
= size
;
582 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
583 g_img_chunk_num
= g_chain
->img_chunk_num
;
584 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
585 g_override_chunk_num
= g_chain
->override_chunk_num
;
586 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
587 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
589 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
591 /* Workaround for Windows & ISO9660 */
592 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
594 g_fixup_iso9660_secover_enable
= TRUE
;
597 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
599 g_hook_keyboard
= TRUE
;
602 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
604 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
606 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
611 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
614 ventoy_update_image_location(&(g_chain
->os_param
));
618 ventoy_dump_chain(g_chain
);
622 g_fix_windows_1st_cdrom_issue
= FALSE
;
623 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
624 ventoy_chain_wim
== g_os_param_reserved
[2])
626 if (ventoy_is_cdrom_dp_exist())
628 debug("fixup the 1st cdrom influences when boot windows ...");
629 g_fix_windows_1st_cdrom_issue
= TRUE
;
637 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
639 FreePool(g_sector_flag
);
640 g_sector_flag_num
= 0;
642 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
644 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
645 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
646 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
649 ventoy_delete_variable();
651 if (g_chain
->os_param
.vtoy_img_location_addr
)
653 FreePool((VOID
*)(UINTN
)g_chain
->os_param
.vtoy_img_location_addr
);
661 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
663 /* don't add debug print in this function */
665 if (g_fix_windows_1st_cdrom_issue
)
667 ventoy_hook_1st_cdrom_start();
670 /* let this the last */
673 ventoy_hook_keyboard_start();
679 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
681 /* don't add debug print in this function */
683 if (g_fix_windows_1st_cdrom_issue
)
685 ventoy_hook_1st_cdrom_stop();
688 /* let this the last */
691 ventoy_hook_keyboard_stop();
697 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
704 EFI_HANDLE Image
= NULL
;
705 EFI_HANDLE
*Handles
= NULL
;
706 EFI_STATUS Status
= EFI_SUCCESS
;
707 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
708 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
710 for (t
= 0; t
< 3; t
++)
715 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
716 NULL
, &Count
, &Handles
);
717 if (EFI_ERROR(Status
))
722 debug("ventoy_boot fs count:%u", Count
);
724 for (i
= 0; i
< Count
; i
++)
726 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
727 if (EFI_ERROR(Status
))
732 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
734 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
738 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
739 if (EFI_ERROR(Status
))
741 debug("Failed to open device path protocol %r", Status
);
745 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
746 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
748 debug("Not ventoy disk file system");
752 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
754 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
755 StrSize(gEfiBootFileName
[j
]), &Image
);
756 if (EFI_SUCCESS
== Status
)
760 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
763 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
769 debug("Find boot file, now try to boot .....");
770 ventoy_debug_pause();
774 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
777 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
779 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
780 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
784 /* can't add debug print here */
785 //ventoy_wrapper_system();
786 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
789 if (EFI_ERROR(Status
))
791 debug("Failed to start image %r", Status
);
793 gBS
->UnloadImage(Image
);
802 debug("Fs not found, now wait and retry...");
809 return EFI_NOT_FOUND
;
815 EFI_STATUS EFIAPI VentoyEfiMain
817 IN EFI_HANDLE ImageHandle
,
818 IN EFI_SYSTEM_TABLE
*SystemTable
821 EFI_STATUS Status
= EFI_SUCCESS
;
822 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
824 g_sector_flag_num
= 512; /* initial value */
826 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
827 if (NULL
== g_sector_flag
)
829 return EFI_OUT_OF_RESOURCES
;
832 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
833 if (EFI_SUCCESS
== Status
)
835 g_con_simple_input_ex
= Protocol
;
838 gST
->ConOut
->ClearScreen(gST
->ConOut
);
839 ventoy_clear_input();
841 ventoy_parse_cmdline(ImageHandle
);
845 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_chain
;
846 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
848 ventoy_save_ramdisk_param();
850 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
851 Status
= ventoy_boot(ImageHandle
);
853 ventoy_delete_ramdisk_param();
857 ventoy_save_variable();
858 ventoy_find_iso_disk(ImageHandle
);
860 ventoy_debug_pause();
862 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
864 ventoy_debug_pause();
866 Status
= ventoy_boot(ImageHandle
);
871 if (EFI_NOT_FOUND
== Status
)
873 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
874 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
878 ventoy_clear_input();
879 gST
->ConOut
->ClearScreen(gST
->ConOut
);