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 BOOLEAN gIsoUdf
= FALSE
;
43 ventoy_ram_disk g_ramdisk_param
;
44 ventoy_chain_head
*g_chain
;
45 void *g_vtoy_img_location_buf
;
46 ventoy_img_chunk
*g_chunk
;
47 UINT8
*g_os_param_reserved
;
48 UINT32 g_img_chunk_num
;
49 ventoy_override_chunk
*g_override_chunk
;
50 UINT32 g_override_chunk_num
;
51 ventoy_virt_chunk
*g_virt_chunk
;
52 UINT32 g_virt_chunk_num
;
53 vtoy_block_data gBlockData
;
54 static grub_env_get_pf grub_env_get
= NULL
;
55 static grub_env_set_pf grub_env_set
= NULL
;
57 ventoy_grub_param_file_replace
*g_file_replace_list
= NULL
;
58 ventoy_efi_file_replace g_efi_file_replace
;
60 CONST CHAR16 gIso9660EfiDriverPath
[] = ISO9660_EFI_DRIVER_PATH
;
61 CONST CHAR16 gUdfEfiDriverPath
[] = UDF_EFI_DRIVER_PATH
;
63 BOOLEAN g_fix_windows_1st_cdrom_issue
= FALSE
;
65 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
67 CHAR16 gFirstTryBootFile
[256] = {0};
70 UINTN gBootFileStartIndex
= 1;
71 CONST CHAR16
*gEfiBootFileName
[] =
74 EFI_REMOVABLE_MEDIA_FILE_NAME
,
75 #if defined (MDE_CPU_IA32)
76 L
"\\EFI\\BOOT\\GRUBIA32.EFI",
77 L
"\\EFI\\BOOT\\BOOTia32.EFI",
78 L
"\\EFI\\BOOT\\bootia32.efi",
79 L
"\\efi\\boot\\bootia32.efi",
80 #elif defined (MDE_CPU_X64)
81 L
"\\EFI\\BOOT\\GRUBX64.EFI",
82 L
"\\EFI\\BOOT\\BOOTx64.EFI",
83 L
"\\EFI\\BOOT\\bootx64.efi",
84 L
"\\efi\\boot\\bootx64.efi",
85 #elif defined (MDE_CPU_ARM)
86 L
"\\EFI\\BOOT\\GRUBARM.EFI",
87 L
"\\EFI\\BOOT\\BOOTarm.EFI",
88 L
"\\EFI\\BOOT\\bootarm.efi",
89 L
"\\efi\\boot\\bootarm.efi",
90 #elif defined (MDE_CPU_AARCH64)
91 L
"\\EFI\\BOOT\\GRUBAA64.EFI",
92 L
"\\EFI\\BOOT\\BOOTaa64.EFI",
93 L
"\\EFI\\BOOT\\bootaa64.efi",
94 L
"\\efi\\boot\\bootaa64.efi",
99 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
104 VA_START (Marker
, Format
);
105 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
108 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
111 VOID EFIAPI
ventoy_clear_input(VOID
)
115 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
116 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
120 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
123 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
128 ventoy_img_chunk
*chunk
;
130 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
132 debug("##################### ventoy_dump_img_chunk #######################");
134 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
136 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
137 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
138 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
140 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
145 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
148 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
150 debug("image chunk size check success");
154 debug("image chunk size check failed %d", errcnt
);
157 ventoy_debug_pause();
160 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
163 ventoy_override_chunk
*chunk
;
165 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
167 debug("##################### ventoy_dump_override_chunk #######################");
169 for (i
= 0; i
< g_override_chunk_num
; i
++)
171 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
174 ventoy_debug_pause();
177 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
180 ventoy_virt_chunk
*node
;
182 debug("##################### ventoy_dump_virt_chunk #######################");
183 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
184 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
186 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
187 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
189 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
190 node
->mem_sector_start
,
191 node
->mem_sector_end
,
192 node
->mem_sector_offset
,
193 node
->remap_sector_start
,
194 node
->remap_sector_end
,
195 node
->org_sector_start
);
198 ventoy_debug_pause();
201 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
207 guid
= chain
->os_param
.vtoy_disk_guid
;
208 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
210 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
213 debug("##################### ventoy_dump_chain #######################");
215 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
216 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
217 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
218 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
219 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
220 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
221 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
222 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
223 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
224 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
225 g_os_param_reserved
[0],
226 g_os_param_reserved
[1],
227 g_os_param_reserved
[2],
228 g_os_param_reserved
[3],
229 g_os_param_reserved
[4]
232 ventoy_debug_pause();
234 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
235 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
236 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
237 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
238 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
239 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
240 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
241 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
242 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
244 ventoy_debug_pause();
246 ventoy_dump_img_chunk(chain
);
247 ventoy_dump_override_chunk(chain
);
248 ventoy_dump_virt_chunk(chain
);
251 static int ventoy_update_image_location(ventoy_os_param
*param
)
253 EFI_STATUS Status
= EFI_SUCCESS
;
259 ventoy_image_location
*location
= NULL
;
260 ventoy_image_disk_region
*region
= NULL
;
261 ventoy_img_chunk
*chunk
= g_chunk
;
263 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
265 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
266 if (EFI_ERROR(Status
) || NULL
== buffer
)
268 debug("Failed to allocate runtime pool %r\n", Status
);
272 address
= (UINTN
)buffer
;
273 g_vtoy_img_location_buf
= buffer
;
277 address
+= 4096 - (address
% 4096);
281 param
->vtoy_img_location_addr
= address
;
282 param
->vtoy_img_location_len
= length
;
284 /* update check sum */
285 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
287 chksum
+= *((UINT8
*)param
+ i
);
289 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
291 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
292 if (NULL
== location
)
297 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
298 location
->image_sector_size
= gSector512Mode
? 512 : 2048;
299 location
->disk_sector_size
= g_chain
->disk_sector_size
;
300 location
->region_count
= g_img_chunk_num
;
302 region
= location
->regions
;
306 for (i
= 0; i
< g_img_chunk_num
; i
++)
308 region
->image_sector_count
= chunk
->disk_end_sector
- chunk
->disk_start_sector
+ 1;
309 region
->image_start_sector
= chunk
->img_start_sector
* 4;
310 region
->disk_start_sector
= chunk
->disk_start_sector
;
317 for (i
= 0; i
< g_img_chunk_num
; i
++)
319 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
320 region
->image_start_sector
= chunk
->img_start_sector
;
321 region
->disk_start_sector
= chunk
->disk_start_sector
;
330 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
332 EFI_HANDLE Handle
= NULL
;
333 EFI_STATUS Status
= EFI_SUCCESS
;
334 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
335 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
336 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
338 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
344 pCurNode
= pTmpDevPath
;
345 while (!IsDevicePathEnd(pCurNode
))
347 pLastNode
= pCurNode
;
348 pCurNode
= NextDevicePathNode(pCurNode
);
352 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
355 pCurNode
= pTmpDevPath
;
356 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
357 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
359 FreePool(pTmpDevPath
);
364 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
366 EFI_STATUS Status
= EFI_SUCCESS
;
367 EFI_GUID VarGuid
= VENTOY_GUID
;
369 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
370 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
371 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
372 debug("set ramdisk variable %r", Status
);
377 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
379 EFI_STATUS Status
= EFI_SUCCESS
;
380 EFI_GUID VarGuid
= VENTOY_GUID
;
382 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
383 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
385 debug("delete efi variable %r", Status
);
391 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
393 EFI_STATUS Status
= EFI_SUCCESS
;
394 EFI_GUID VarGuid
= VENTOY_GUID
;
396 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
397 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
398 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
399 debug("set efi variable %r", Status
);
404 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
406 EFI_STATUS Status
= EFI_SUCCESS
;
407 EFI_GUID VarGuid
= VENTOY_GUID
;
409 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
410 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
412 debug("delete efi variable %r", Status
);
417 #if (VENTOY_DEVICE_WARN != 0)
418 STATIC VOID
ventoy_warn_invalid_device(VOID
)
420 STATIC BOOLEAN flag
= FALSE
;
428 gST
->ConOut
->ClearScreen(gST
->ConOut
);
429 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
430 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
431 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n\r\n\r\n");
433 gST
->ConOut
->OutputString(gST
->ConOut
, L
"This is NOT a standard Ventoy device and is NOT supported.\r\n\r\n");
434 gST
->ConOut
->OutputString(gST
->ConOut
, L
"You should follow the official instructions in https://www.ventoy.net\r\n");
436 gST
->ConOut
->OutputString(gST
->ConOut
, L
"\r\n\r\nWill exit after 10 seconds ...... ");
441 STATIC VOID
ventoy_warn_invalid_device(VOID
)
447 STATIC EFI_STATUS EFIAPI ventoy_load_image
449 IN EFI_HANDLE ImageHandle
,
450 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
451 IN CONST CHAR16
*FileName
,
452 IN UINTN FileNameLen
,
453 OUT EFI_HANDLE
*Image
456 EFI_STATUS Status
= EFI_SUCCESS
;
457 CHAR16 TmpBuf
[256] = {0};
458 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
459 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
461 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
462 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
463 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
464 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
465 pFilePath
->Header
.Length
[1] = 0;
466 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
468 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
471 return EFI_NOT_FOUND
;
474 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
476 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
484 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
489 MBR_HEAD
*pMBR
= NULL
;
490 UINT8
*pBuffer
= NULL
;
492 EFI_STATUS Status
= EFI_SUCCESS
;
493 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
495 pBuffer
= AllocatePool(2048);
498 return EFI_OUT_OF_RESOURCES
;
501 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
502 NULL
, &Count
, &Handles
);
503 if (EFI_ERROR(Status
))
509 for (i
= 0; i
< Count
; i
++)
511 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
512 if (EFI_ERROR(Status
))
517 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
518 debug("This Disk size: %llu", DiskSize
);
519 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
524 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
525 if (EFI_ERROR(Status
))
527 debug("ReadBlocks filed %r", Status
);
531 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
533 pMBR
= (MBR_HEAD
*)pBuffer
;
534 if (pMBR
->PartTbl
[0].FsFlag
!= 0xEE)
536 if (pMBR
->PartTbl
[0].StartSectorId
!= 2048 ||
537 pMBR
->PartTbl
[1].SectorCount
!= 65536 ||
538 pMBR
->PartTbl
[1].StartSectorId
!= pMBR
->PartTbl
[0].StartSectorId
+ pMBR
->PartTbl
[0].SectorCount
)
540 debug("Failed to check disk part table");
541 ventoy_warn_invalid_device();
545 gBlockData
.RawBlockIoHandle
= Handles
[i
];
546 gBlockData
.pRawBlockIo
= pBlockIo
;
547 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
548 (VOID
**)&(gBlockData
.pDiskDevPath
),
551 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
553 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
554 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
563 return EFI_NOT_FOUND
;
572 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
576 EFI_HANDLE Parent
= NULL
;
577 EFI_HANDLE
*Handles
= NULL
;
578 EFI_STATUS Status
= EFI_SUCCESS
;
579 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
580 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
582 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
583 NULL
, &Count
, &Handles
);
584 if (EFI_ERROR(Status
))
589 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
591 for (i
= 0; i
< Count
; i
++)
593 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
594 if (EFI_ERROR(Status
))
599 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
603 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
604 if (EFI_ERROR(Status
))
606 debug("Failed to open device path protocol %r", Status
);
610 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
611 Parent
= ventoy_get_parent_handle(pDevPath
);
613 if (Parent
== gBlockData
.RawBlockIoHandle
)
615 debug("Find ventoy disk fs");
616 gBlockData
.DiskFsHandle
= Handles
[i
];
617 gBlockData
.pDiskFs
= pFile
;
618 gBlockData
.pDiskFsDevPath
= pDevPath
;
628 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
630 EFI_HANDLE Image
= NULL
;
631 EFI_STATUS Status
= EFI_SUCCESS
;
632 CHAR16 LogVar
[4] = L
"5";
636 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
638 sizeof(gUdfEfiDriverPath
),
640 debug("load iso UDF efi driver status:%r", Status
);
644 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
645 gIso9660EfiDriverPath
,
646 sizeof(gIso9660EfiDriverPath
),
648 debug("load iso 9660 efi driver status:%r", Status
);
653 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
654 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
655 sizeof(LogVar
), LogVar
);
658 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
659 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
660 sizeof(LogVar
), LogVar
);
662 gBlockData
.IsoDriverImage
= Image
;
663 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
664 debug("Start iso efi driver status:%r", Status
);
669 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
675 const char *pEnv
= NULL
;
677 CHAR16
*pCmdLine
= NULL
;
678 EFI_STATUS Status
= EFI_SUCCESS
;
679 ventoy_grub_param
*pGrubParam
= NULL
;
680 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
681 ventoy_chain_head
*chain
= NULL
;
683 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
684 if (EFI_ERROR(Status
))
686 VtoyDebug("Failed to handle load image protocol %r", Status
);
690 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
691 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
692 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
694 if (StrStr(pCmdLine
, L
"debug"))
699 if (StrStr(pCmdLine
, L
"dotefi"))
704 if (StrStr(pCmdLine
, L
"isoefi=on"))
709 if (StrStr(pCmdLine
, L
"iso_udf"))
714 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
717 pPos
+= StrLen(L
"FirstTry=");
718 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
720 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
722 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
730 gEfiBootFileName
[0] = gFirstTryBootFile
;
731 gBootFileStartIndex
= 0;
734 debug("cmdline:<%s>", pCmdLine
);
736 if (gFirstTryBootFile
[0])
738 debug("First Try:<%s>", gFirstTryBootFile
);
741 pPos
= StrStr(pCmdLine
, L
"env_param=");
744 return EFI_INVALID_PARAMETER
;
747 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
748 grub_env_set
= pGrubParam
->grub_env_set
;
749 grub_env_get
= pGrubParam
->grub_env_get
;
750 pEnv
= grub_env_get("VTOY_CHKDEV_RESULT_STRING");
753 return EFI_INVALID_PARAMETER
;
756 if (pEnv
[0] != '0' || pEnv
[1] != 0)
758 ventoy_warn_invalid_device();
759 return EFI_INVALID_PARAMETER
;
762 g_file_replace_list
= &pGrubParam
->file_replace
;
763 old_cnt
= g_file_replace_list
->old_file_cnt
;
764 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
765 g_file_replace_list
->magic
,
766 g_file_replace_list
->new_file_virtual_id
,
768 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
769 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
770 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
771 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
774 pPos
= StrStr(pCmdLine
, L
"mem:");
775 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
777 pPos
= StrStr(pPos
, L
"size:");
778 size
= StrDecimalToUintn(pPos
+ 5);
780 debug("memory addr:%p size:%lu", chain
, size
);
782 if (StrStr(pCmdLine
, L
"sector512"))
784 gSector512Mode
= TRUE
;
787 if (StrStr(pCmdLine
, L
"memdisk"))
789 g_iso_data_buf
= (UINT8
*)chain
+ sizeof(ventoy_chain_head
);
790 g_iso_buf_size
= size
- sizeof(ventoy_chain_head
);
791 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size
);
798 debug("This is normal mode");
799 g_chain
= AllocatePool(size
);
800 CopyMem(g_chain
, chain
, size
);
802 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
803 g_img_chunk_num
= g_chain
->img_chunk_num
;
804 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
805 g_override_chunk_num
= g_chain
->override_chunk_num
;
806 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
807 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
809 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
811 /* Workaround for Windows & ISO9660 */
812 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
814 g_fixup_iso9660_secover_enable
= TRUE
;
817 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
819 g_hook_keyboard
= TRUE
;
822 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
824 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
826 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
831 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
834 ventoy_update_image_location(&(g_chain
->os_param
));
838 ventoy_dump_chain(g_chain
);
842 g_fix_windows_1st_cdrom_issue
= FALSE
;
843 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
844 ventoy_chain_wim
== g_os_param_reserved
[2])
846 if (ventoy_is_cdrom_dp_exist())
848 debug("fixup the 1st cdrom influences when boot windows ...");
849 g_fix_windows_1st_cdrom_issue
= TRUE
;
853 ventoy_debug_pause();
859 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
861 FreePool(g_sector_flag
);
862 g_sector_flag_num
= 0;
864 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
866 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
869 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
871 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
872 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
873 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
876 ventoy_delete_variable();
878 if (g_vtoy_img_location_buf
)
880 FreePool(g_vtoy_img_location_buf
);
891 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
893 /* don't add debug print in this function */
895 if (g_fix_windows_1st_cdrom_issue
)
897 ventoy_hook_1st_cdrom_start();
900 /* let this the last */
903 ventoy_hook_keyboard_start();
909 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
911 /* don't add debug print in this function */
913 if (g_fix_windows_1st_cdrom_issue
)
915 ventoy_hook_1st_cdrom_stop();
918 /* let this the last */
921 ventoy_hook_keyboard_stop();
927 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
934 EFI_HANDLE Image
= NULL
;
935 EFI_HANDLE
*Handles
= NULL
;
936 EFI_STATUS Status
= EFI_SUCCESS
;
937 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
938 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
940 for (t
= 0; t
< 3; t
++)
945 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
946 NULL
, &Count
, &Handles
);
947 if (EFI_ERROR(Status
))
952 debug("ventoy_boot fs count:%u", Count
);
954 for (i
= 0; i
< Count
; i
++)
956 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
957 if (EFI_ERROR(Status
))
962 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
964 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
968 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
969 if (EFI_ERROR(Status
))
971 debug("Failed to open device path protocol %r", Status
);
975 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
976 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
978 debug("Not ventoy disk file system");
982 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
984 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
985 StrSize(gEfiBootFileName
[j
]), &Image
);
986 if (EFI_SUCCESS
== Status
)
990 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
993 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
999 debug("Find boot file, now try to boot .....");
1000 ventoy_debug_pause();
1004 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
1007 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
1009 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
1010 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
1013 ventoy_hook_start();
1014 /* can't add debug print here */
1015 //ventoy_wrapper_system();
1016 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
1019 if (EFI_ERROR(Status
))
1021 debug("Failed to start image %r", Status
);
1023 gBS
->UnloadImage(Image
);
1037 debug("Fs not found, now wait and retry...");
1044 return EFI_NOT_FOUND
;
1050 EFI_STATUS EFIAPI VentoyEfiMain
1052 IN EFI_HANDLE ImageHandle
,
1053 IN EFI_SYSTEM_TABLE
*SystemTable
1056 EFI_STATUS Status
= EFI_SUCCESS
;
1057 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
1059 g_sector_flag_num
= 512; /* initial value */
1061 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
1062 if (NULL
== g_sector_flag
)
1064 return EFI_OUT_OF_RESOURCES
;
1067 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
1068 if (EFI_SUCCESS
== Status
)
1070 g_con_simple_input_ex
= Protocol
;
1073 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1074 ventoy_clear_input();
1076 Status
= ventoy_parse_cmdline(ImageHandle
);
1077 if (EFI_ERROR(Status
))
1084 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_iso_data_buf
;
1085 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
1087 ventoy_save_ramdisk_param();
1091 ventoy_find_iso_disk(ImageHandle
);
1092 ventoy_find_iso_disk_fs(ImageHandle
);
1093 ventoy_load_isoefi_driver(ImageHandle
);
1096 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
1097 ventoy_debug_pause();
1099 Status
= ventoy_boot(ImageHandle
);
1101 ventoy_delete_ramdisk_param();
1103 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1105 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1108 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1109 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1110 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1111 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1116 ventoy_save_variable();
1117 Status
= ventoy_find_iso_disk(ImageHandle
);
1118 if (!EFI_ERROR(Status
))
1122 ventoy_find_iso_disk_fs(ImageHandle
);
1123 ventoy_load_isoefi_driver(ImageHandle
);
1126 ventoy_debug_pause();
1128 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
1130 ventoy_debug_pause();
1132 Status
= ventoy_boot(ImageHandle
);
1138 if (FALSE
== gDotEfiBoot
)
1140 if (EFI_NOT_FOUND
== Status
)
1142 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
1143 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
1148 ventoy_clear_input();
1149 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1151 if (gDotEfiBoot
&& (EFI_NOT_FOUND
== Status
))
1153 grub_env_set("vtoy_dotefi_retry", "YES");