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 BOOLEAN gLoadIsoEfi
= FALSE
;
41 ventoy_ram_disk g_ramdisk_param
;
42 ventoy_chain_head
*g_chain
;
43 ventoy_img_chunk
*g_chunk
;
44 UINT8
*g_os_param_reserved
;
45 UINT32 g_img_chunk_num
;
46 ventoy_override_chunk
*g_override_chunk
;
47 UINT32 g_override_chunk_num
;
48 ventoy_virt_chunk
*g_virt_chunk
;
49 UINT32 g_virt_chunk_num
;
50 vtoy_block_data gBlockData
;
51 static grub_env_get_pf grub_env_get
= NULL
;
53 ventoy_grub_param_file_replace
*g_file_replace_list
= NULL
;
54 ventoy_efi_file_replace g_efi_file_replace
;
56 CONST CHAR16 gIso9660EfiDriverPath
[] = ISO9660_EFI_DRIVER_PATH
;
58 BOOLEAN g_fix_windows_1st_cdrom_issue
= FALSE
;
60 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
62 CHAR16 gFirstTryBootFile
[256] = {0};
65 UINTN gBootFileStartIndex
= 1;
66 CONST CHAR16
*gEfiBootFileName
[] =
69 EFI_REMOVABLE_MEDIA_FILE_NAME
,
70 L
"\\EFI\\BOOT\\GRUBX64.EFI",
71 L
"\\EFI\\BOOT\\BOOTx64.EFI",
72 L
"\\EFI\\BOOT\\bootx64.efi",
73 L
"\\efi\\boot\\bootx64.efi",
76 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
81 VA_START (Marker
, Format
);
82 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
85 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
88 VOID EFIAPI
ventoy_clear_input(VOID
)
92 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
93 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
97 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
100 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
105 ventoy_img_chunk
*chunk
;
107 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
109 debug("##################### ventoy_dump_img_chunk #######################");
111 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
113 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
114 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
115 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
117 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
122 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
125 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
127 debug("image chunk size check success");
131 debug("image chunk size check failed %d", errcnt
);
134 ventoy_debug_pause();
137 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
140 ventoy_override_chunk
*chunk
;
142 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
144 debug("##################### ventoy_dump_override_chunk #######################");
146 for (i
= 0; i
< g_override_chunk_num
; i
++)
148 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
151 ventoy_debug_pause();
154 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
157 ventoy_virt_chunk
*node
;
159 debug("##################### ventoy_dump_virt_chunk #######################");
160 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
161 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
163 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
164 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
166 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
167 node
->mem_sector_start
,
168 node
->mem_sector_end
,
169 node
->mem_sector_offset
,
170 node
->remap_sector_start
,
171 node
->remap_sector_end
,
172 node
->org_sector_start
);
175 ventoy_debug_pause();
178 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
184 guid
= chain
->os_param
.vtoy_disk_guid
;
185 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
187 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
190 debug("##################### ventoy_dump_chain #######################");
192 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
193 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
194 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
195 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
196 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
197 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
198 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
199 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
200 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
201 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
202 g_os_param_reserved
[0],
203 g_os_param_reserved
[1],
204 g_os_param_reserved
[2],
205 g_os_param_reserved
[3],
206 g_os_param_reserved
[4]
209 ventoy_debug_pause();
211 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
212 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
213 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
214 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
215 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
216 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
217 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
218 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
219 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
221 ventoy_debug_pause();
223 ventoy_dump_img_chunk(chain
);
224 ventoy_dump_override_chunk(chain
);
225 ventoy_dump_virt_chunk(chain
);
228 static int ventoy_update_image_location(ventoy_os_param
*param
)
230 EFI_STATUS Status
= EFI_SUCCESS
;
236 ventoy_image_location
*location
= NULL
;
237 ventoy_image_disk_region
*region
= NULL
;
238 ventoy_img_chunk
*chunk
= g_chunk
;
240 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
242 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
243 if (EFI_ERROR(Status
) || NULL
== buffer
)
245 debug("Failed to allocate runtime pool %r\n", Status
);
249 address
= (UINTN
)buffer
;
253 address
+= 4096 - (address
% 4096);
257 param
->vtoy_img_location_addr
= address
;
258 param
->vtoy_img_location_len
= length
;
260 /* update check sum */
261 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
263 chksum
+= *((UINT8
*)param
+ i
);
265 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
267 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
268 if (NULL
== location
)
273 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
274 location
->image_sector_size
= 2048;
275 location
->disk_sector_size
= g_chain
->disk_sector_size
;
276 location
->region_count
= g_img_chunk_num
;
278 region
= location
->regions
;
280 for (i
= 0; i
< g_img_chunk_num
; i
++)
282 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
283 region
->image_start_sector
= chunk
->img_start_sector
;
284 region
->disk_start_sector
= chunk
->disk_start_sector
;
292 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
294 EFI_HANDLE Handle
= NULL
;
295 EFI_STATUS Status
= EFI_SUCCESS
;
296 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
297 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
298 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
300 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
306 pCurNode
= pTmpDevPath
;
307 while (!IsDevicePathEnd(pCurNode
))
309 pLastNode
= pCurNode
;
310 pCurNode
= NextDevicePathNode(pCurNode
);
314 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
317 pCurNode
= pTmpDevPath
;
318 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
319 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
321 FreePool(pTmpDevPath
);
326 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
328 EFI_STATUS Status
= EFI_SUCCESS
;
329 EFI_GUID VarGuid
= VENTOY_GUID
;
331 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
332 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
333 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
334 debug("set efi variable %r", Status
);
339 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
341 EFI_STATUS Status
= EFI_SUCCESS
;
342 EFI_GUID VarGuid
= VENTOY_GUID
;
344 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
345 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
347 debug("delete efi variable %r", Status
);
353 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
355 EFI_STATUS Status
= EFI_SUCCESS
;
356 EFI_GUID VarGuid
= VENTOY_GUID
;
358 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
359 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
360 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
361 debug("set efi variable %r", Status
);
366 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
368 EFI_STATUS Status
= EFI_SUCCESS
;
369 EFI_GUID VarGuid
= VENTOY_GUID
;
371 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
372 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
374 debug("delete efi variable %r", Status
);
379 STATIC EFI_STATUS EFIAPI ventoy_load_image
381 IN EFI_HANDLE ImageHandle
,
382 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
383 IN CONST CHAR16
*FileName
,
384 IN UINTN FileNameLen
,
385 OUT EFI_HANDLE
*Image
388 EFI_STATUS Status
= EFI_SUCCESS
;
389 CHAR16 TmpBuf
[256] = {0};
390 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
391 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
393 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
394 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
395 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
396 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
397 pFilePath
->Header
.Length
[1] = 0;
398 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
400 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
403 return EFI_NOT_FOUND
;
406 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
408 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
416 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
421 UINT8
*pBuffer
= NULL
;
423 EFI_STATUS Status
= EFI_SUCCESS
;
424 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
426 pBuffer
= AllocatePool(2048);
429 return EFI_OUT_OF_RESOURCES
;
432 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
433 NULL
, &Count
, &Handles
);
434 if (EFI_ERROR(Status
))
440 for (i
= 0; i
< Count
; i
++)
442 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
443 if (EFI_ERROR(Status
))
448 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
449 debug("This Disk size: %llu", DiskSize
);
450 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
455 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
456 if (EFI_ERROR(Status
))
458 debug("ReadBlocks filed %r", Status
);
462 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
464 gBlockData
.RawBlockIoHandle
= Handles
[i
];
465 gBlockData
.pRawBlockIo
= pBlockIo
;
466 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
467 (VOID
**)&(gBlockData
.pDiskDevPath
),
470 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
472 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
473 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
482 return EFI_NOT_FOUND
;
491 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
495 EFI_HANDLE Parent
= NULL
;
496 EFI_HANDLE
*Handles
= NULL
;
497 EFI_STATUS Status
= EFI_SUCCESS
;
498 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
499 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
501 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
502 NULL
, &Count
, &Handles
);
503 if (EFI_ERROR(Status
))
508 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
510 for (i
= 0; i
< Count
; i
++)
512 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
513 if (EFI_ERROR(Status
))
518 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
522 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
523 if (EFI_ERROR(Status
))
525 debug("Failed to open device path protocol %r", Status
);
529 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
530 Parent
= ventoy_get_parent_handle(pDevPath
);
532 if (Parent
== gBlockData
.RawBlockIoHandle
)
534 debug("Find ventoy disk fs");
535 gBlockData
.DiskFsHandle
= Handles
[i
];
536 gBlockData
.pDiskFs
= pFile
;
537 gBlockData
.pDiskFsDevPath
= pDevPath
;
547 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
549 EFI_HANDLE Image
= NULL
;
550 EFI_STATUS Status
= EFI_SUCCESS
;
551 CHAR16 LogVar
[4] = L
"5";
553 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
554 gIso9660EfiDriverPath
,
555 sizeof(gIso9660EfiDriverPath
),
557 debug("load iso efi driver status:%r", Status
);
561 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
562 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
563 sizeof(LogVar
), LogVar
);
566 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
567 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
568 sizeof(LogVar
), LogVar
);
570 gBlockData
.IsoDriverImage
= Image
;
571 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
572 debug("Start iso efi driver status:%r", Status
);
577 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
584 CHAR16
*pCmdLine
= NULL
;
585 EFI_STATUS Status
= EFI_SUCCESS
;
586 ventoy_grub_param
*pGrubParam
= NULL
;
587 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
588 ventoy_chain_head
*chain
= NULL
;
590 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
591 if (EFI_ERROR(Status
))
593 VtoyDebug("Failed to handle load image protocol %r", Status
);
597 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
598 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
599 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
601 if (StrStr(pCmdLine
, L
"debug"))
606 if (StrStr(pCmdLine
, L
"isoefi=on"))
611 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
614 pPos
+= StrLen(L
"FirstTry=");
615 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
617 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
619 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
627 gEfiBootFileName
[0] = gFirstTryBootFile
;
628 gBootFileStartIndex
= 0;
631 debug("cmdline:<%s>", pCmdLine
);
633 if (gFirstTryBootFile
[0])
635 debug("First Try:<%s>", gFirstTryBootFile
);
638 pPos
= StrStr(pCmdLine
, L
"env_param=");
641 return EFI_INVALID_PARAMETER
;
644 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
645 grub_env_get
= pGrubParam
->grub_env_get
;
647 g_file_replace_list
= &pGrubParam
->file_replace
;
648 old_cnt
= g_file_replace_list
->old_file_cnt
;
649 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
650 g_file_replace_list
->magic
,
651 g_file_replace_list
->new_file_virtual_id
,
653 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
654 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
655 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
656 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
659 pPos
= StrStr(pCmdLine
, L
"mem:");
660 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
662 pPos
= StrStr(pPos
, L
"size:");
663 size
= StrDecimalToUintn(pPos
+ 5);
665 debug("memory addr:%p size:%lu", chain
, size
);
667 g_chain
= AllocatePool(size
);
668 CopyMem(g_chain
, chain
, size
);
670 if (StrStr(pCmdLine
, L
"memdisk"))
672 g_iso_buf_size
= size
;
677 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
678 g_img_chunk_num
= g_chain
->img_chunk_num
;
679 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
680 g_override_chunk_num
= g_chain
->override_chunk_num
;
681 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
682 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
684 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
686 /* Workaround for Windows & ISO9660 */
687 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
689 g_fixup_iso9660_secover_enable
= TRUE
;
692 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
694 g_hook_keyboard
= TRUE
;
697 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
699 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
701 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
706 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
709 ventoy_update_image_location(&(g_chain
->os_param
));
713 ventoy_dump_chain(g_chain
);
717 g_fix_windows_1st_cdrom_issue
= FALSE
;
718 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
719 ventoy_chain_wim
== g_os_param_reserved
[2])
721 if (ventoy_is_cdrom_dp_exist())
723 debug("fixup the 1st cdrom influences when boot windows ...");
724 g_fix_windows_1st_cdrom_issue
= TRUE
;
732 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
734 FreePool(g_sector_flag
);
735 g_sector_flag_num
= 0;
737 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
739 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
742 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
744 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
745 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
746 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
749 ventoy_delete_variable();
751 if (g_chain
->os_param
.vtoy_img_location_addr
)
753 FreePool((VOID
*)(UINTN
)g_chain
->os_param
.vtoy_img_location_addr
);
761 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
763 /* don't add debug print in this function */
765 if (g_fix_windows_1st_cdrom_issue
)
767 ventoy_hook_1st_cdrom_start();
770 /* let this the last */
773 ventoy_hook_keyboard_start();
779 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
781 /* don't add debug print in this function */
783 if (g_fix_windows_1st_cdrom_issue
)
785 ventoy_hook_1st_cdrom_stop();
788 /* let this the last */
791 ventoy_hook_keyboard_stop();
797 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
804 EFI_HANDLE Image
= NULL
;
805 EFI_HANDLE
*Handles
= NULL
;
806 EFI_STATUS Status
= EFI_SUCCESS
;
807 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
808 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
810 for (t
= 0; t
< 3; t
++)
815 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
816 NULL
, &Count
, &Handles
);
817 if (EFI_ERROR(Status
))
822 debug("ventoy_boot fs count:%u", Count
);
824 for (i
= 0; i
< Count
; i
++)
826 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
827 if (EFI_ERROR(Status
))
832 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
834 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
838 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
839 if (EFI_ERROR(Status
))
841 debug("Failed to open device path protocol %r", Status
);
845 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
846 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
848 debug("Not ventoy disk file system");
852 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
854 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
855 StrSize(gEfiBootFileName
[j
]), &Image
);
856 if (EFI_SUCCESS
== Status
)
860 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
863 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
869 debug("Find boot file, now try to boot .....");
870 ventoy_debug_pause();
874 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
877 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
879 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
880 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
884 /* can't add debug print here */
885 //ventoy_wrapper_system();
886 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
889 if (EFI_ERROR(Status
))
891 debug("Failed to start image %r", Status
);
893 gBS
->UnloadImage(Image
);
902 debug("Fs not found, now wait and retry...");
909 return EFI_NOT_FOUND
;
915 EFI_STATUS EFIAPI VentoyEfiMain
917 IN EFI_HANDLE ImageHandle
,
918 IN EFI_SYSTEM_TABLE
*SystemTable
921 EFI_STATUS Status
= EFI_SUCCESS
;
922 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
924 g_sector_flag_num
= 512; /* initial value */
926 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
927 if (NULL
== g_sector_flag
)
929 return EFI_OUT_OF_RESOURCES
;
932 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
933 if (EFI_SUCCESS
== Status
)
935 g_con_simple_input_ex
= Protocol
;
938 gST
->ConOut
->ClearScreen(gST
->ConOut
);
939 ventoy_clear_input();
941 ventoy_parse_cmdline(ImageHandle
);
945 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_chain
;
946 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
948 ventoy_save_ramdisk_param();
950 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
951 Status
= ventoy_boot(ImageHandle
);
953 ventoy_delete_ramdisk_param();
957 ventoy_save_variable();
958 ventoy_find_iso_disk(ImageHandle
);
962 ventoy_find_iso_disk_fs(ImageHandle
);
963 ventoy_load_isoefi_driver(ImageHandle
);
966 ventoy_debug_pause();
968 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
970 ventoy_debug_pause();
972 Status
= ventoy_boot(ImageHandle
);
977 if (EFI_NOT_FOUND
== Status
)
979 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
980 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
984 ventoy_clear_input();
985 gST
->ConOut
->ClearScreen(gST
->ConOut
);