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 CHAR16 gFirstTryBootFile
[256] = {0};
58 UINTN gBootFileStartIndex
= 1;
59 CONST CHAR16
*gEfiBootFileName
[] =
62 EFI_REMOVABLE_MEDIA_FILE_NAME
,
63 L
"\\EFI\\BOOT\\GRUBX64.EFI",
64 L
"\\EFI\\BOOT\\BOOTx64.EFI",
65 L
"\\EFI\\BOOT\\bootx64.efi",
66 L
"\\efi\\boot\\bootx64.efi",
69 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
74 VA_START (Marker
, Format
);
75 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
78 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
81 VOID EFIAPI
ventoy_clear_input(VOID
)
85 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
86 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
90 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
93 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
98 ventoy_img_chunk
*chunk
;
100 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
102 debug("##################### ventoy_dump_img_chunk #######################");
104 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
106 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
107 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
108 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
110 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
115 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
118 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
120 debug("image chunk size check success");
124 debug("image chunk size check failed %d", errcnt
);
127 ventoy_debug_pause();
130 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
133 ventoy_override_chunk
*chunk
;
135 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
137 debug("##################### ventoy_dump_override_chunk #######################");
139 for (i
= 0; i
< g_override_chunk_num
; i
++)
141 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
144 ventoy_debug_pause();
147 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
150 ventoy_virt_chunk
*node
;
152 debug("##################### ventoy_dump_virt_chunk #######################");
153 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
154 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
156 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
157 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
159 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
160 node
->mem_sector_start
,
161 node
->mem_sector_end
,
162 node
->mem_sector_offset
,
163 node
->remap_sector_start
,
164 node
->remap_sector_end
,
165 node
->org_sector_start
);
168 ventoy_debug_pause();
171 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
177 guid
= chain
->os_param
.vtoy_disk_guid
;
178 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
180 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
183 debug("##################### ventoy_dump_chain #######################");
185 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
186 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
187 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
188 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
189 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
190 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
191 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
192 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
193 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
194 debug("os_param->vtoy_reserved=<%u %u %u %u>",
195 g_os_param_reserved
[0],
196 g_os_param_reserved
[1],
197 g_os_param_reserved
[2],
198 g_os_param_reserved
[3]
201 ventoy_debug_pause();
203 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
204 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
205 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
206 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
207 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
208 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
209 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
210 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
211 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
213 ventoy_debug_pause();
215 ventoy_dump_img_chunk(chain
);
216 ventoy_dump_override_chunk(chain
);
217 ventoy_dump_virt_chunk(chain
);
220 static int ventoy_update_image_location(ventoy_os_param
*param
)
222 EFI_STATUS Status
= EFI_SUCCESS
;
228 ventoy_image_location
*location
= NULL
;
229 ventoy_image_disk_region
*region
= NULL
;
230 ventoy_img_chunk
*chunk
= g_chunk
;
232 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
234 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
235 if (EFI_ERROR(Status
) || NULL
== buffer
)
237 debug("Failed to allocate runtime pool %r\n", Status
);
241 address
= (UINTN
)buffer
;
245 address
+= 4096 - (address
% 4096);
249 param
->vtoy_img_location_addr
= address
;
250 param
->vtoy_img_location_len
= length
;
252 /* update check sum */
253 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
255 chksum
+= *((UINT8
*)param
+ i
);
257 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
259 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
260 if (NULL
== location
)
265 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
266 location
->image_sector_size
= 2048;
267 location
->disk_sector_size
= g_chain
->disk_sector_size
;
268 location
->region_count
= g_img_chunk_num
;
270 region
= location
->regions
;
272 for (i
= 0; i
< g_img_chunk_num
; i
++)
274 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
275 region
->image_start_sector
= chunk
->img_start_sector
;
276 region
->disk_start_sector
= chunk
->disk_start_sector
;
284 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
286 EFI_HANDLE Handle
= NULL
;
287 EFI_STATUS Status
= EFI_SUCCESS
;
288 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
289 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
290 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
292 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
298 pCurNode
= pTmpDevPath
;
299 while (!IsDevicePathEnd(pCurNode
))
301 pLastNode
= pCurNode
;
302 pCurNode
= NextDevicePathNode(pCurNode
);
306 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
309 pCurNode
= pTmpDevPath
;
310 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
311 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
313 FreePool(pTmpDevPath
);
318 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
320 EFI_STATUS Status
= EFI_SUCCESS
;
321 EFI_GUID VarGuid
= VENTOY_GUID
;
323 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
324 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
325 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
326 debug("set efi variable %r", Status
);
331 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
333 EFI_STATUS Status
= EFI_SUCCESS
;
334 EFI_GUID VarGuid
= VENTOY_GUID
;
336 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
337 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
339 debug("delete efi variable %r", Status
);
345 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
347 EFI_STATUS Status
= EFI_SUCCESS
;
348 EFI_GUID VarGuid
= VENTOY_GUID
;
350 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
351 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
352 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
353 debug("set efi variable %r", Status
);
358 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
360 EFI_STATUS Status
= EFI_SUCCESS
;
361 EFI_GUID VarGuid
= VENTOY_GUID
;
363 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
364 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
366 debug("delete efi variable %r", Status
);
371 STATIC EFI_STATUS EFIAPI ventoy_load_image
373 IN EFI_HANDLE ImageHandle
,
374 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
375 IN CONST CHAR16
*FileName
,
376 IN UINTN FileNameLen
,
377 OUT EFI_HANDLE
*Image
380 EFI_STATUS Status
= EFI_SUCCESS
;
381 CHAR16 TmpBuf
[256] = {0};
382 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
383 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
385 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
386 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
387 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
388 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
389 pFilePath
->Header
.Length
[1] = 0;
390 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
392 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
395 return EFI_NOT_FOUND
;
398 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
400 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
408 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
413 UINT8
*pBuffer
= NULL
;
415 EFI_STATUS Status
= EFI_SUCCESS
;
416 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
418 pBuffer
= AllocatePool(2048);
421 return EFI_OUT_OF_RESOURCES
;
424 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
425 NULL
, &Count
, &Handles
);
426 if (EFI_ERROR(Status
))
432 for (i
= 0; i
< Count
; i
++)
434 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
435 if (EFI_ERROR(Status
))
440 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
441 debug("This Disk size: %llu", DiskSize
);
442 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
447 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
448 if (EFI_ERROR(Status
))
450 debug("ReadBlocks filed %r", Status
);
454 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
456 gBlockData
.RawBlockIoHandle
= Handles
[i
];
457 gBlockData
.pRawBlockIo
= pBlockIo
;
458 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
459 (VOID
**)&(gBlockData
.pDiskDevPath
),
462 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
464 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
465 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
474 return EFI_NOT_FOUND
;
482 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
489 CHAR16
*pCmdLine
= NULL
;
490 EFI_STATUS Status
= EFI_SUCCESS
;
491 ventoy_grub_param
*pGrubParam
= NULL
;
492 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
494 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
495 if (EFI_ERROR(Status
))
497 VtoyDebug("Failed to handle load image protocol %r", Status
);
501 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
502 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
503 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
505 if (StrStr(pCmdLine
, L
"debug"))
510 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
513 pPos
+= StrLen(L
"FirstTry=");
514 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
516 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
518 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
526 gEfiBootFileName
[0] = gFirstTryBootFile
;
527 gBootFileStartIndex
= 0;
530 debug("cmdline:<%s>", pCmdLine
);
532 if (gFirstTryBootFile
[0])
534 debug("First Try:<%s>", gFirstTryBootFile
);
537 pPos
= StrStr(pCmdLine
, L
"env_param=");
540 return EFI_INVALID_PARAMETER
;
543 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
544 grub_env_get
= pGrubParam
->grub_env_get
;
546 g_file_replace_list
= &pGrubParam
->file_replace
;
547 old_cnt
= g_file_replace_list
->old_file_cnt
;
548 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
549 g_file_replace_list
->magic
,
550 g_file_replace_list
->new_file_virtual_id
,
552 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
553 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
554 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
555 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
558 pPos
= StrStr(pCmdLine
, L
"mem:");
559 g_chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
561 pPos
= StrStr(pPos
, L
"size:");
562 size
= StrDecimalToUintn(pPos
+ 5);
564 debug("memory addr:%p size:%lu", g_chain
, size
);
566 if (StrStr(pCmdLine
, L
"memdisk"))
568 g_iso_buf_size
= size
;
573 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
574 g_img_chunk_num
= g_chain
->img_chunk_num
;
575 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
576 g_override_chunk_num
= g_chain
->override_chunk_num
;
577 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
578 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
580 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
582 /* Workaround for Windows & ISO9660 */
583 if (g_os_param_reserved
[2] == 1 && g_os_param_reserved
[3] == 0)
585 g_fixup_iso9660_secover_enable
= TRUE
;
588 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
590 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
595 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
598 ventoy_update_image_location(&(g_chain
->os_param
));
602 ventoy_dump_chain(g_chain
);
610 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
612 FreePool(g_sector_flag
);
613 g_sector_flag_num
= 0;
615 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
617 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
618 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
619 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
622 ventoy_delete_variable();
624 if (g_chain
->os_param
.vtoy_img_location_addr
)
626 FreePool((VOID
*)(UINTN
)g_chain
->os_param
.vtoy_img_location_addr
);
632 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
639 EFI_HANDLE Image
= NULL
;
640 EFI_HANDLE
*Handles
= NULL
;
641 EFI_STATUS Status
= EFI_SUCCESS
;
642 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
643 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
645 for (t
= 0; t
< 3; t
++)
650 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
651 NULL
, &Count
, &Handles
);
652 if (EFI_ERROR(Status
))
657 debug("ventoy_boot fs count:%u", Count
);
659 for (i
= 0; i
< Count
; i
++)
661 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
662 if (EFI_ERROR(Status
))
667 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
669 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
673 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
674 if (EFI_ERROR(Status
))
676 debug("Failed to open device path protocol %r", Status
);
680 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
681 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
683 debug("Not ventoy disk file system");
687 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
689 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
690 StrSize(gEfiBootFileName
[j
]), &Image
);
691 if (EFI_SUCCESS
== Status
)
695 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
698 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
704 debug("Find boot file, now try to boot .....");
705 ventoy_debug_pause();
709 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
710 //ventoy_wrapper_system();
713 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
715 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
716 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
719 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
720 if (EFI_ERROR(Status
))
722 debug("Failed to start image %r", Status
);
724 gBS
->UnloadImage(Image
);
733 debug("Fs not found, now wait and retry...");
740 return EFI_NOT_FOUND
;
747 EFI_STATUS EFIAPI VentoyEfiMain
749 IN EFI_HANDLE ImageHandle
,
750 IN EFI_SYSTEM_TABLE
*SystemTable
753 EFI_STATUS Status
= EFI_SUCCESS
;
755 g_sector_flag_num
= 512; /* initial value */
757 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
758 if (NULL
== g_sector_flag
)
760 return EFI_OUT_OF_RESOURCES
;
763 gST
->ConOut
->ClearScreen(gST
->ConOut
);
764 ventoy_clear_input();
766 ventoy_parse_cmdline(ImageHandle
);
770 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_chain
;
771 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
773 ventoy_save_ramdisk_param();
775 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
776 Status
= ventoy_boot(ImageHandle
);
778 ventoy_delete_ramdisk_param();
782 ventoy_save_variable();
783 ventoy_find_iso_disk(ImageHandle
);
785 ventoy_debug_pause();
787 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
789 ventoy_debug_pause();
791 Status
= ventoy_boot(ImageHandle
);
796 if (EFI_NOT_FOUND
== Status
)
798 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
799 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
803 ventoy_clear_input();
804 gST
->ConOut
->ClearScreen(gST
->ConOut
);