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 gDotEfiBoot
= FALSE
;
41 BOOLEAN gLoadIsoEfi
= FALSE
;
42 ventoy_ram_disk g_ramdisk_param
;
43 ventoy_chain_head
*g_chain
;
44 ventoy_img_chunk
*g_chunk
;
45 UINT8
*g_os_param_reserved
;
46 UINT32 g_img_chunk_num
;
47 ventoy_override_chunk
*g_override_chunk
;
48 UINT32 g_override_chunk_num
;
49 ventoy_virt_chunk
*g_virt_chunk
;
50 UINT32 g_virt_chunk_num
;
51 vtoy_block_data gBlockData
;
52 static grub_env_get_pf grub_env_get
= NULL
;
53 static grub_env_set_pf grub_env_set
= NULL
;
55 ventoy_grub_param_file_replace
*g_file_replace_list
= NULL
;
56 ventoy_efi_file_replace g_efi_file_replace
;
58 CONST CHAR16 gIso9660EfiDriverPath
[] = ISO9660_EFI_DRIVER_PATH
;
60 BOOLEAN g_fix_windows_1st_cdrom_issue
= FALSE
;
62 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
64 CHAR16 gFirstTryBootFile
[256] = {0};
67 UINTN gBootFileStartIndex
= 1;
68 CONST CHAR16
*gEfiBootFileName
[] =
71 EFI_REMOVABLE_MEDIA_FILE_NAME
,
72 L
"\\EFI\\BOOT\\GRUBX64.EFI",
73 L
"\\EFI\\BOOT\\BOOTx64.EFI",
74 L
"\\EFI\\BOOT\\bootx64.efi",
75 L
"\\efi\\boot\\bootx64.efi",
78 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
83 VA_START (Marker
, Format
);
84 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
87 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
90 VOID EFIAPI
ventoy_clear_input(VOID
)
94 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
95 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
99 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
102 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
107 ventoy_img_chunk
*chunk
;
109 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
111 debug("##################### ventoy_dump_img_chunk #######################");
113 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
115 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
116 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
117 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
119 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
124 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
127 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
129 debug("image chunk size check success");
133 debug("image chunk size check failed %d", errcnt
);
136 ventoy_debug_pause();
139 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
142 ventoy_override_chunk
*chunk
;
144 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
146 debug("##################### ventoy_dump_override_chunk #######################");
148 for (i
= 0; i
< g_override_chunk_num
; i
++)
150 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
153 ventoy_debug_pause();
156 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
159 ventoy_virt_chunk
*node
;
161 debug("##################### ventoy_dump_virt_chunk #######################");
162 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
163 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
165 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
166 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
168 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
169 node
->mem_sector_start
,
170 node
->mem_sector_end
,
171 node
->mem_sector_offset
,
172 node
->remap_sector_start
,
173 node
->remap_sector_end
,
174 node
->org_sector_start
);
177 ventoy_debug_pause();
180 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
186 guid
= chain
->os_param
.vtoy_disk_guid
;
187 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
189 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
192 debug("##################### ventoy_dump_chain #######################");
194 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
195 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
196 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
197 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
198 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
199 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
200 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
201 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
202 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
203 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
204 g_os_param_reserved
[0],
205 g_os_param_reserved
[1],
206 g_os_param_reserved
[2],
207 g_os_param_reserved
[3],
208 g_os_param_reserved
[4]
211 ventoy_debug_pause();
213 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
214 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
215 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
216 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
217 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
218 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
219 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
220 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
221 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
223 ventoy_debug_pause();
225 ventoy_dump_img_chunk(chain
);
226 ventoy_dump_override_chunk(chain
);
227 ventoy_dump_virt_chunk(chain
);
230 static int ventoy_update_image_location(ventoy_os_param
*param
)
232 EFI_STATUS Status
= EFI_SUCCESS
;
238 ventoy_image_location
*location
= NULL
;
239 ventoy_image_disk_region
*region
= NULL
;
240 ventoy_img_chunk
*chunk
= g_chunk
;
242 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
244 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
245 if (EFI_ERROR(Status
) || NULL
== buffer
)
247 debug("Failed to allocate runtime pool %r\n", Status
);
251 address
= (UINTN
)buffer
;
255 address
+= 4096 - (address
% 4096);
259 param
->vtoy_img_location_addr
= address
;
260 param
->vtoy_img_location_len
= length
;
262 /* update check sum */
263 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
265 chksum
+= *((UINT8
*)param
+ i
);
267 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
269 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
270 if (NULL
== location
)
275 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
276 location
->image_sector_size
= gSector512Mode
? 512 : 2048;
277 location
->disk_sector_size
= g_chain
->disk_sector_size
;
278 location
->region_count
= g_img_chunk_num
;
280 region
= location
->regions
;
284 for (i
= 0; i
< g_img_chunk_num
; i
++)
286 region
->image_sector_count
= chunk
->disk_end_sector
- chunk
->disk_start_sector
+ 1;
287 region
->image_start_sector
= chunk
->img_start_sector
* 4;
288 region
->disk_start_sector
= chunk
->disk_start_sector
;
295 for (i
= 0; i
< g_img_chunk_num
; i
++)
297 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
298 region
->image_start_sector
= chunk
->img_start_sector
;
299 region
->disk_start_sector
= chunk
->disk_start_sector
;
308 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
310 EFI_HANDLE Handle
= NULL
;
311 EFI_STATUS Status
= EFI_SUCCESS
;
312 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
313 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
314 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
316 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
322 pCurNode
= pTmpDevPath
;
323 while (!IsDevicePathEnd(pCurNode
))
325 pLastNode
= pCurNode
;
326 pCurNode
= NextDevicePathNode(pCurNode
);
330 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
333 pCurNode
= pTmpDevPath
;
334 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
335 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
337 FreePool(pTmpDevPath
);
342 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
344 EFI_STATUS Status
= EFI_SUCCESS
;
345 EFI_GUID VarGuid
= VENTOY_GUID
;
347 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
348 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
349 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
350 debug("set ramdisk variable %r", Status
);
355 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
357 EFI_STATUS Status
= EFI_SUCCESS
;
358 EFI_GUID VarGuid
= VENTOY_GUID
;
360 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
361 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
363 debug("delete efi variable %r", Status
);
369 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
371 EFI_STATUS Status
= EFI_SUCCESS
;
372 EFI_GUID VarGuid
= VENTOY_GUID
;
374 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
375 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
376 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
377 debug("set efi variable %r", Status
);
382 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
384 EFI_STATUS Status
= EFI_SUCCESS
;
385 EFI_GUID VarGuid
= VENTOY_GUID
;
387 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
388 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
390 debug("delete efi variable %r", Status
);
395 #if (VENTOY_DEVICE_WARN != 0)
396 STATIC VOID
ventoy_warn_invalid_device(VOID
)
398 STATIC BOOLEAN flag
= FALSE
;
406 gST
->ConOut
->ClearScreen(gST
->ConOut
);
407 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
408 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
409 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n\r\n\r\n");
411 gST
->ConOut
->OutputString(gST
->ConOut
, L
"This is NOT a standard Ventoy device and is NOT supported.\r\n\r\n");
412 gST
->ConOut
->OutputString(gST
->ConOut
, L
"You should follow the official instructions in https://www.ventoy.net\r\n");
414 gST
->ConOut
->OutputString(gST
->ConOut
, L
"\r\n\r\nWill exit after 10 seconds ...... ");
419 STATIC VOID
ventoy_warn_invalid_device(VOID
)
425 STATIC EFI_STATUS EFIAPI ventoy_load_image
427 IN EFI_HANDLE ImageHandle
,
428 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
429 IN CONST CHAR16
*FileName
,
430 IN UINTN FileNameLen
,
431 OUT EFI_HANDLE
*Image
434 EFI_STATUS Status
= EFI_SUCCESS
;
435 CHAR16 TmpBuf
[256] = {0};
436 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
437 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
439 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
440 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
441 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
442 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
443 pFilePath
->Header
.Length
[1] = 0;
444 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
446 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
449 return EFI_NOT_FOUND
;
452 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
454 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
462 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
467 MBR_HEAD
*pMBR
= NULL
;
468 UINT8
*pBuffer
= NULL
;
470 EFI_STATUS Status
= EFI_SUCCESS
;
471 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
473 pBuffer
= AllocatePool(2048);
476 return EFI_OUT_OF_RESOURCES
;
479 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
480 NULL
, &Count
, &Handles
);
481 if (EFI_ERROR(Status
))
487 for (i
= 0; i
< Count
; i
++)
489 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
490 if (EFI_ERROR(Status
))
495 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
496 debug("This Disk size: %llu", DiskSize
);
497 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
502 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
503 if (EFI_ERROR(Status
))
505 debug("ReadBlocks filed %r", Status
);
509 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
511 pMBR
= (MBR_HEAD
*)pBuffer
;
512 if (pMBR
->PartTbl
[0].FsFlag
!= 0xEE)
514 if (pMBR
->PartTbl
[0].StartSectorId
!= 2048 ||
515 pMBR
->PartTbl
[1].SectorCount
!= 65536 ||
516 pMBR
->PartTbl
[1].StartSectorId
!= pMBR
->PartTbl
[0].StartSectorId
+ pMBR
->PartTbl
[0].SectorCount
)
518 debug("Failed to check disk part table");
519 ventoy_warn_invalid_device();
523 gBlockData
.RawBlockIoHandle
= Handles
[i
];
524 gBlockData
.pRawBlockIo
= pBlockIo
;
525 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
526 (VOID
**)&(gBlockData
.pDiskDevPath
),
529 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
531 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
532 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
541 return EFI_NOT_FOUND
;
550 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
554 EFI_HANDLE Parent
= NULL
;
555 EFI_HANDLE
*Handles
= NULL
;
556 EFI_STATUS Status
= EFI_SUCCESS
;
557 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
558 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
560 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
561 NULL
, &Count
, &Handles
);
562 if (EFI_ERROR(Status
))
567 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
569 for (i
= 0; i
< Count
; i
++)
571 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
572 if (EFI_ERROR(Status
))
577 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
581 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
582 if (EFI_ERROR(Status
))
584 debug("Failed to open device path protocol %r", Status
);
588 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
589 Parent
= ventoy_get_parent_handle(pDevPath
);
591 if (Parent
== gBlockData
.RawBlockIoHandle
)
593 debug("Find ventoy disk fs");
594 gBlockData
.DiskFsHandle
= Handles
[i
];
595 gBlockData
.pDiskFs
= pFile
;
596 gBlockData
.pDiskFsDevPath
= pDevPath
;
606 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
608 EFI_HANDLE Image
= NULL
;
609 EFI_STATUS Status
= EFI_SUCCESS
;
610 CHAR16 LogVar
[4] = L
"5";
612 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
613 gIso9660EfiDriverPath
,
614 sizeof(gIso9660EfiDriverPath
),
616 debug("load iso efi driver status:%r", Status
);
620 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
621 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
622 sizeof(LogVar
), LogVar
);
625 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
626 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
627 sizeof(LogVar
), LogVar
);
629 gBlockData
.IsoDriverImage
= Image
;
630 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
631 debug("Start iso efi driver status:%r", Status
);
636 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
642 const char *pEnv
= NULL
;
644 CHAR16
*pCmdLine
= NULL
;
645 EFI_STATUS Status
= EFI_SUCCESS
;
646 ventoy_grub_param
*pGrubParam
= NULL
;
647 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
648 ventoy_chain_head
*chain
= NULL
;
650 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
651 if (EFI_ERROR(Status
))
653 VtoyDebug("Failed to handle load image protocol %r", Status
);
657 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
658 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
659 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
661 if (StrStr(pCmdLine
, L
"debug"))
666 if (StrStr(pCmdLine
, L
"dotefi"))
671 if (StrStr(pCmdLine
, L
"isoefi=on"))
676 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
679 pPos
+= StrLen(L
"FirstTry=");
680 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
682 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
684 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
692 gEfiBootFileName
[0] = gFirstTryBootFile
;
693 gBootFileStartIndex
= 0;
696 debug("cmdline:<%s>", pCmdLine
);
698 if (gFirstTryBootFile
[0])
700 debug("First Try:<%s>", gFirstTryBootFile
);
703 pPos
= StrStr(pCmdLine
, L
"env_param=");
706 return EFI_INVALID_PARAMETER
;
709 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
710 grub_env_set
= pGrubParam
->grub_env_set
;
711 grub_env_get
= pGrubParam
->grub_env_get
;
712 pEnv
= grub_env_get("VTOY_CHKDEV_RESULT_STRING");
715 return EFI_INVALID_PARAMETER
;
718 if (pEnv
[0] != '0' || pEnv
[1] != 0)
720 ventoy_warn_invalid_device();
721 return EFI_INVALID_PARAMETER
;
724 g_file_replace_list
= &pGrubParam
->file_replace
;
725 old_cnt
= g_file_replace_list
->old_file_cnt
;
726 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
727 g_file_replace_list
->magic
,
728 g_file_replace_list
->new_file_virtual_id
,
730 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
731 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
732 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
733 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
736 pPos
= StrStr(pCmdLine
, L
"mem:");
737 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
739 pPos
= StrStr(pPos
, L
"size:");
740 size
= StrDecimalToUintn(pPos
+ 5);
742 debug("memory addr:%p size:%lu", chain
, size
);
744 if (StrStr(pCmdLine
, L
"sector512"))
746 gSector512Mode
= TRUE
;
749 if (StrStr(pCmdLine
, L
"memdisk"))
751 g_iso_data_buf
= (UINT8
*)chain
+ sizeof(ventoy_chain_head
);
752 g_iso_buf_size
= size
- sizeof(ventoy_chain_head
);
753 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size
);
760 debug("This is normal mode");
761 g_chain
= AllocatePool(size
);
762 CopyMem(g_chain
, chain
, size
);
764 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
765 g_img_chunk_num
= g_chain
->img_chunk_num
;
766 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
767 g_override_chunk_num
= g_chain
->override_chunk_num
;
768 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
769 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
771 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
773 /* Workaround for Windows & ISO9660 */
774 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
776 g_fixup_iso9660_secover_enable
= TRUE
;
779 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
781 g_hook_keyboard
= TRUE
;
784 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
786 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
788 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
793 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
796 ventoy_update_image_location(&(g_chain
->os_param
));
800 ventoy_dump_chain(g_chain
);
804 g_fix_windows_1st_cdrom_issue
= FALSE
;
805 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
806 ventoy_chain_wim
== g_os_param_reserved
[2])
808 if (ventoy_is_cdrom_dp_exist())
810 debug("fixup the 1st cdrom influences when boot windows ...");
811 g_fix_windows_1st_cdrom_issue
= TRUE
;
815 ventoy_debug_pause();
821 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
823 FreePool(g_sector_flag
);
824 g_sector_flag_num
= 0;
826 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
828 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
831 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
833 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
834 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
835 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
838 ventoy_delete_variable();
840 if (g_chain
->os_param
.vtoy_img_location_addr
)
842 FreePool((VOID
*)(UINTN
)g_chain
->os_param
.vtoy_img_location_addr
);
850 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
852 /* don't add debug print in this function */
854 if (g_fix_windows_1st_cdrom_issue
)
856 ventoy_hook_1st_cdrom_start();
859 /* let this the last */
862 ventoy_hook_keyboard_start();
868 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
870 /* don't add debug print in this function */
872 if (g_fix_windows_1st_cdrom_issue
)
874 ventoy_hook_1st_cdrom_stop();
877 /* let this the last */
880 ventoy_hook_keyboard_stop();
886 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
893 EFI_HANDLE Image
= NULL
;
894 EFI_HANDLE
*Handles
= NULL
;
895 EFI_STATUS Status
= EFI_SUCCESS
;
896 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
897 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
899 for (t
= 0; t
< 3; t
++)
904 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
905 NULL
, &Count
, &Handles
);
906 if (EFI_ERROR(Status
))
911 debug("ventoy_boot fs count:%u", Count
);
913 for (i
= 0; i
< Count
; i
++)
915 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
916 if (EFI_ERROR(Status
))
921 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
923 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
927 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
928 if (EFI_ERROR(Status
))
930 debug("Failed to open device path protocol %r", Status
);
934 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
935 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
937 debug("Not ventoy disk file system");
941 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
943 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
944 StrSize(gEfiBootFileName
[j
]), &Image
);
945 if (EFI_SUCCESS
== Status
)
949 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
952 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
958 debug("Find boot file, now try to boot .....");
959 ventoy_debug_pause();
963 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
966 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
968 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
969 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
973 /* can't add debug print here */
974 //ventoy_wrapper_system();
975 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
978 if (EFI_ERROR(Status
))
980 debug("Failed to start image %r", Status
);
982 gBS
->UnloadImage(Image
);
996 debug("Fs not found, now wait and retry...");
1003 return EFI_NOT_FOUND
;
1009 EFI_STATUS EFIAPI VentoyEfiMain
1011 IN EFI_HANDLE ImageHandle
,
1012 IN EFI_SYSTEM_TABLE
*SystemTable
1015 EFI_STATUS Status
= EFI_SUCCESS
;
1016 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
1018 g_sector_flag_num
= 512; /* initial value */
1020 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
1021 if (NULL
== g_sector_flag
)
1023 return EFI_OUT_OF_RESOURCES
;
1026 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
1027 if (EFI_SUCCESS
== Status
)
1029 g_con_simple_input_ex
= Protocol
;
1032 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1033 ventoy_clear_input();
1035 Status
= ventoy_parse_cmdline(ImageHandle
);
1036 if (EFI_ERROR(Status
))
1043 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_iso_data_buf
;
1044 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
1046 ventoy_save_ramdisk_param();
1050 ventoy_find_iso_disk(ImageHandle
);
1051 ventoy_find_iso_disk_fs(ImageHandle
);
1052 ventoy_load_isoefi_driver(ImageHandle
);
1055 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
1056 ventoy_debug_pause();
1058 Status
= ventoy_boot(ImageHandle
);
1060 ventoy_delete_ramdisk_param();
1062 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1064 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1067 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1068 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1069 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1070 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1075 ventoy_save_variable();
1076 Status
= ventoy_find_iso_disk(ImageHandle
);
1077 if (!EFI_ERROR(Status
))
1081 ventoy_find_iso_disk_fs(ImageHandle
);
1082 ventoy_load_isoefi_driver(ImageHandle
);
1085 ventoy_debug_pause();
1087 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
1089 ventoy_debug_pause();
1091 Status
= ventoy_boot(ImageHandle
);
1097 if (FALSE
== gDotEfiBoot
)
1099 if (EFI_NOT_FOUND
== Status
)
1101 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
1102 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
1107 ventoy_clear_input();
1108 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1110 if (gDotEfiBoot
&& (EFI_NOT_FOUND
== Status
))
1112 grub_env_set("vtoy_dotefi_retry", "YES");