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>
37 #include <Protocol/DriverBinding.h>
40 BOOLEAN gDebugPrint
= FALSE
;
41 BOOLEAN gBootFallBack
= FALSE
;
42 BOOLEAN gDotEfiBoot
= FALSE
;
43 BOOLEAN gLoadIsoEfi
= FALSE
;
44 BOOLEAN gIsoUdf
= FALSE
;
45 ventoy_ram_disk g_ramdisk_param
;
46 ventoy_chain_head
*g_chain
;
47 void *g_vtoy_img_location_buf
;
48 ventoy_img_chunk
*g_chunk
;
49 UINT8
*g_os_param_reserved
;
50 UINT32 g_img_chunk_num
;
51 ventoy_override_chunk
*g_override_chunk
;
52 UINT32 g_override_chunk_num
;
53 ventoy_virt_chunk
*g_virt_chunk
;
54 UINT32 g_virt_chunk_num
;
55 vtoy_block_data gBlockData
;
56 static grub_env_get_pf grub_env_get
= NULL
;
57 static grub_env_set_pf grub_env_set
= NULL
;
59 ventoy_grub_param_file_replace
*g_file_replace_list
= NULL
;
60 ventoy_efi_file_replace g_efi_file_replace
;
62 ventoy_grub_param_file_replace
*g_img_replace_list
= NULL
;
63 ventoy_efi_file_replace g_img_file_replace
[VTOY_MAX_CONF_REPLACE
];
65 CONST CHAR16 gIso9660EfiDriverPath
[] = ISO9660_EFI_DRIVER_PATH
;
66 CONST CHAR16 gUdfEfiDriverPath
[] = UDF_EFI_DRIVER_PATH
;
68 BOOLEAN g_fix_windows_1st_cdrom_issue
= FALSE
;
70 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
72 CHAR16 gFirstTryBootFile
[256] = {0};
74 STATIC EFI_GET_VARIABLE g_org_get_variable
= NULL
;
75 STATIC EFI_EXIT_BOOT_SERVICES g_org_exit_boot_service
= NULL
;
78 UINTN gBootFileStartIndex
= 1;
79 CONST CHAR16
*gEfiBootFileName
[] =
82 EFI_REMOVABLE_MEDIA_FILE_NAME
,
83 #if defined (MDE_CPU_IA32)
84 L
"\\EFI\\BOOT\\GRUBIA32.EFI",
85 L
"\\EFI\\BOOT\\BOOTia32.EFI",
86 L
"\\EFI\\BOOT\\bootia32.efi",
87 L
"\\efi\\boot\\bootia32.efi",
88 #elif defined (MDE_CPU_X64)
89 L
"\\EFI\\BOOT\\GRUBX64.EFI",
90 L
"\\EFI\\BOOT\\BOOTx64.EFI",
91 L
"\\EFI\\BOOT\\bootx64.efi",
92 L
"\\efi\\boot\\bootx64.efi",
93 #elif defined (MDE_CPU_ARM)
94 L
"\\EFI\\BOOT\\GRUBARM.EFI",
95 L
"\\EFI\\BOOT\\BOOTarm.EFI",
96 L
"\\EFI\\BOOT\\bootarm.efi",
97 L
"\\efi\\boot\\bootarm.efi",
98 #elif defined (MDE_CPU_AARCH64)
99 L
"\\EFI\\BOOT\\GRUBAA64.EFI",
100 L
"\\EFI\\BOOT\\BOOTaa64.EFI",
101 L
"\\EFI\\BOOT\\bootaa64.efi",
102 L
"\\efi\\boot\\bootaa64.efi",
107 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
112 VA_START (Marker
, Format
);
113 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
116 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
119 VOID EFIAPI
ventoy_clear_input(VOID
)
123 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
124 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
128 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
131 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
136 ventoy_img_chunk
*chunk
;
138 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
140 debug("##################### ventoy_dump_img_chunk #######################");
142 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
144 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
145 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
146 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
148 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
153 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
156 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
158 debug("image chunk size check success");
162 debug("image chunk size check failed %d", errcnt
);
165 ventoy_debug_pause();
168 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
171 ventoy_override_chunk
*chunk
;
173 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
175 debug("##################### ventoy_dump_override_chunk #######################");
177 for (i
= 0; i
< g_override_chunk_num
; i
++)
179 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
182 ventoy_debug_pause();
185 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
188 ventoy_virt_chunk
*node
;
190 debug("##################### ventoy_dump_virt_chunk #######################");
191 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
192 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
194 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
195 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
197 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
198 node
->mem_sector_start
,
199 node
->mem_sector_end
,
200 node
->mem_sector_offset
,
201 node
->remap_sector_start
,
202 node
->remap_sector_end
,
203 node
->org_sector_start
);
206 ventoy_debug_pause();
209 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
215 guid
= chain
->os_param
.vtoy_disk_guid
;
216 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
218 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
221 debug("##################### ventoy_dump_chain #######################");
223 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
224 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
225 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
226 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
227 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
228 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
229 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
230 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
231 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
232 debug("os_param->vtoy_reserved=<%u %u %u %u %u %u %u>",
233 g_os_param_reserved
[0],
234 g_os_param_reserved
[1],
235 g_os_param_reserved
[2],
236 g_os_param_reserved
[3],
237 g_os_param_reserved
[4],
238 g_os_param_reserved
[5],
239 g_os_param_reserved
[6]
242 ventoy_debug_pause();
244 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
245 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
246 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
247 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
248 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
249 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
250 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
251 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
252 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
254 ventoy_debug_pause();
256 ventoy_dump_img_chunk(chain
);
257 ventoy_dump_override_chunk(chain
);
258 ventoy_dump_virt_chunk(chain
);
261 static int ventoy_update_image_location(ventoy_os_param
*param
)
263 EFI_STATUS Status
= EFI_SUCCESS
;
269 ventoy_image_location
*location
= NULL
;
270 ventoy_image_disk_region
*region
= NULL
;
271 ventoy_img_chunk
*chunk
= g_chunk
;
273 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
275 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
276 if (EFI_ERROR(Status
) || NULL
== buffer
)
278 debug("Failed to allocate runtime pool %r\n", Status
);
282 address
= (UINTN
)buffer
;
283 g_vtoy_img_location_buf
= buffer
;
287 address
+= 4096 - (address
% 4096);
291 param
->vtoy_img_location_addr
= address
;
292 param
->vtoy_img_location_len
= length
;
294 /* update check sum */
295 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
297 chksum
+= *((UINT8
*)param
+ i
);
299 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
301 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
302 if (NULL
== location
)
307 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
308 location
->image_sector_size
= gSector512Mode
? 512 : 2048;
309 location
->disk_sector_size
= g_chain
->disk_sector_size
;
310 location
->region_count
= g_img_chunk_num
;
312 region
= location
->regions
;
316 for (i
= 0; i
< g_img_chunk_num
; i
++)
318 region
->image_sector_count
= chunk
->disk_end_sector
- chunk
->disk_start_sector
+ 1;
319 region
->image_start_sector
= chunk
->img_start_sector
* 4;
320 region
->disk_start_sector
= chunk
->disk_start_sector
;
327 for (i
= 0; i
< g_img_chunk_num
; i
++)
329 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
330 region
->image_start_sector
= chunk
->img_start_sector
;
331 region
->disk_start_sector
= chunk
->disk_start_sector
;
340 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
342 EFI_HANDLE Handle
= NULL
;
343 EFI_STATUS Status
= EFI_SUCCESS
;
344 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
345 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
346 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
348 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
354 pCurNode
= pTmpDevPath
;
355 while (!IsDevicePathEnd(pCurNode
))
357 pLastNode
= pCurNode
;
358 pCurNode
= NextDevicePathNode(pCurNode
);
362 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
365 pCurNode
= pTmpDevPath
;
366 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
367 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
369 FreePool(pTmpDevPath
);
374 STATIC ventoy_ram_disk g_backup_ramdisk_param
;
375 STATIC ventoy_os_param g_backup_os_param_var
;
378 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
381 EFI_STATUS Status
= EFI_SUCCESS
;
382 EFI_GUID VarGuid
= VENTOY_GUID
;
384 DataSize
= sizeof(g_backup_ramdisk_param
);
385 Status
= gRT
->GetVariable(L
"VentoyRamDisk", &VarGuid
, NULL
, &DataSize
, &g_backup_ramdisk_param
);
386 if (!EFI_ERROR(Status
))
388 debug("find previous ramdisk variable <%llu>", g_backup_ramdisk_param
.DiskSize
);
391 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
392 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
393 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
394 debug("set ramdisk variable %r", Status
);
399 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
401 EFI_STATUS Status
= EFI_SUCCESS
;
402 EFI_GUID VarGuid
= VENTOY_GUID
;
404 if (g_backup_ramdisk_param
.DiskSize
> 0 && g_backup_ramdisk_param
.PhyAddr
> 0)
406 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
407 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
408 sizeof(g_backup_ramdisk_param
), &g_backup_ramdisk_param
);
409 debug("resotre ramdisk variable %r", Status
);
413 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
414 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
416 debug("delete ramdisk variable %r", Status
);
422 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
425 EFI_STATUS Status
= EFI_SUCCESS
;
426 EFI_GUID VarGuid
= VENTOY_GUID
;
428 DataSize
= sizeof(g_backup_os_param_var
);
429 Status
= gRT
->GetVariable(L
"VentoyOsParam", &VarGuid
, NULL
, &DataSize
, &g_backup_os_param_var
);
430 if (!EFI_ERROR(Status
))
432 debug("find previous efi variable <%a>", g_backup_os_param_var
.vtoy_img_path
);
435 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
436 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
437 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
438 debug("set efi variable %r", Status
);
443 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
445 EFI_STATUS Status
= EFI_SUCCESS
;
446 EFI_GUID VarGuid
= VENTOY_GUID
;
448 if (0 == CompareMem(&(g_backup_os_param_var
.guid
), &VarGuid
, sizeof(EFI_GUID
)))
450 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
451 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
452 sizeof(g_backup_os_param_var
), &(g_backup_os_param_var
));
453 debug("restore efi variable %r", Status
);
457 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
458 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
460 debug("delete efi variable %r", Status
);
466 #if (VENTOY_DEVICE_WARN != 0)
467 STATIC VOID
ventoy_warn_invalid_device(VOID
)
469 STATIC BOOLEAN flag
= FALSE
;
477 gST
->ConOut
->ClearScreen(gST
->ConOut
);
478 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
479 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
480 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n\r\n\r\n");
482 gST
->ConOut
->OutputString(gST
->ConOut
, L
"This is NOT a standard Ventoy device and is NOT supported.\r\n\r\n");
483 gST
->ConOut
->OutputString(gST
->ConOut
, L
"You should follow the official instructions in https://www.ventoy.net\r\n");
485 gST
->ConOut
->OutputString(gST
->ConOut
, L
"\r\n\r\nWill exit after 10 seconds ...... ");
490 STATIC VOID
ventoy_warn_invalid_device(VOID
)
496 STATIC EFI_STATUS EFIAPI ventoy_load_image
498 IN EFI_HANDLE ImageHandle
,
499 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
500 IN CONST CHAR16
*FileName
,
501 IN UINTN FileNameLen
,
502 OUT EFI_HANDLE
*Image
505 EFI_STATUS Status
= EFI_SUCCESS
;
506 CHAR16 TmpBuf
[256] = {0};
507 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
508 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
510 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
511 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
512 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
513 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
514 pFilePath
->Header
.Length
[1] = 0;
515 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
517 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
520 return EFI_NOT_FOUND
;
523 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
525 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
533 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
538 MBR_HEAD
*pMBR
= NULL
;
539 UINT8
*pBuffer
= NULL
;
541 EFI_STATUS Status
= EFI_SUCCESS
;
542 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
544 pBuffer
= AllocatePool(2048);
547 return EFI_OUT_OF_RESOURCES
;
550 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
551 NULL
, &Count
, &Handles
);
552 if (EFI_ERROR(Status
))
558 for (i
= 0; i
< Count
; i
++)
560 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
561 if (EFI_ERROR(Status
))
566 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
567 debug("This Disk size: %llu", DiskSize
);
568 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
573 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
574 if (EFI_ERROR(Status
))
576 debug("ReadBlocks filed %r", Status
);
580 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0 &&
581 CompareMem(g_chain
->os_param
.vtoy_disk_signature
, pBuffer
+ 0x1b8, 4) == 0)
583 pMBR
= (MBR_HEAD
*)pBuffer
;
584 if (g_os_param_reserved
[6] == 0 && pMBR
->PartTbl
[0].FsFlag
!= 0xEE)
586 if (pMBR
->PartTbl
[0].StartSectorId
!= 2048 ||
587 pMBR
->PartTbl
[1].SectorCount
!= 65536 ||
588 pMBR
->PartTbl
[1].StartSectorId
!= pMBR
->PartTbl
[0].StartSectorId
+ pMBR
->PartTbl
[0].SectorCount
)
590 debug("Failed to check disk part table");
591 ventoy_warn_invalid_device();
595 gBlockData
.RawBlockIoHandle
= Handles
[i
];
596 gBlockData
.pRawBlockIo
= pBlockIo
;
597 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
598 (VOID
**)&(gBlockData
.pDiskDevPath
),
601 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
603 debug("Find Ventoy Disk Sig Handle:%p DP:%s", Handles
[i
],
604 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
613 return EFI_NOT_FOUND
;
622 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
626 EFI_HANDLE Parent
= NULL
;
627 EFI_HANDLE
*Handles
= NULL
;
628 EFI_STATUS Status
= EFI_SUCCESS
;
629 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
630 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
632 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
633 NULL
, &Count
, &Handles
);
634 if (EFI_ERROR(Status
))
639 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
641 for (i
= 0; i
< Count
; i
++)
643 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
644 if (EFI_ERROR(Status
))
649 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
653 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
654 if (EFI_ERROR(Status
))
656 debug("Failed to open device path protocol %r", Status
);
660 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
661 Parent
= ventoy_get_parent_handle(pDevPath
);
663 if (Parent
== gBlockData
.RawBlockIoHandle
)
665 debug("Find ventoy disk fs");
666 gBlockData
.DiskFsHandle
= Handles
[i
];
667 gBlockData
.pDiskFs
= pFile
;
668 gBlockData
.pDiskFsDevPath
= pDevPath
;
678 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
680 EFI_HANDLE Image
= NULL
;
681 EFI_STATUS Status
= EFI_SUCCESS
;
682 CHAR16 LogVar
[4] = L
"5";
686 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
688 sizeof(gUdfEfiDriverPath
),
690 debug("load iso UDF efi driver status:%r", Status
);
694 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
695 gIso9660EfiDriverPath
,
696 sizeof(gIso9660EfiDriverPath
),
698 debug("load iso 9660 efi driver status:%r", Status
);
703 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
704 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
705 sizeof(LogVar
), LogVar
);
708 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
709 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
710 sizeof(LogVar
), LogVar
);
712 gBlockData
.IsoDriverImage
= Image
;
713 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
714 debug("Start iso efi driver status:%r", Status
);
719 STATIC EFI_STATUS
ventoy_proc_img_replace_name(ventoy_grub_param_file_replace
*replace
)
724 if (replace
->magic
!= GRUB_IMG_REPLACE_MAGIC
)
729 if (replace
->old_file_name
[0][0] == 0)
734 AsciiStrCpyS(tmp
, sizeof(tmp
), replace
->old_file_name
[0]);
736 for (i
= 0; i
< 256 && tmp
[i
]; i
++)
744 AsciiStrCpyS(replace
->old_file_name
[0], 256, tmp
);
748 EFI_STATUS EFIAPI ventoy_get_variable_wrapper
750 IN CHAR16
*VariableName
,
751 IN EFI_GUID
*VendorGuid
,
752 OUT UINT32
*Attributes
, OPTIONAL
753 IN OUT UINTN
*DataSize
,
754 OUT VOID
*Data OPTIONAL
757 EFI_STATUS Status
= EFI_SUCCESS
;
759 Status
= g_org_get_variable(VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
760 if (StrCmp(VariableName
, L
"SecureBoot") == 0)
762 if ((*DataSize
== 1) && Data
)
771 EFI_STATUS EFIAPI ventoy_exit_boot_service_wrapper
773 IN EFI_HANDLE ImageHandle
,
777 if (g_org_get_variable
)
779 gRT
->GetVariable
= g_org_get_variable
;
780 g_org_get_variable
= NULL
;
783 return g_org_exit_boot_service(ImageHandle
, MapKey
);
786 STATIC EFI_STATUS EFIAPI
ventoy_disable_secure_boot(IN EFI_HANDLE ImageHandle
)
790 EFI_STATUS Status
= EFI_SUCCESS
;
792 Status
= gRT
->GetVariable(L
"SecureBoot", &gEfiGlobalVariableGuid
, NULL
, &DataSize
, &Value
);
793 if (!EFI_ERROR(Status
))
795 if (DataSize
== 1 && Value
== 0)
797 debug("Current secure boot is off, no need to disable");
802 debug("ventoy_disable_secure_boot");
804 /* step1: wrapper security protocol. */
805 /* Do we still need it since we have been loaded ? */
808 /* step2: fake SecureBoot variable */
809 g_org_exit_boot_service
= gBS
->ExitBootServices
;
810 gBS
->ExitBootServices
= ventoy_exit_boot_service_wrapper
;
812 g_org_get_variable
= gRT
->GetVariable
;
813 gRT
->GetVariable
= ventoy_get_variable_wrapper
;
819 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
825 const char *pEnv
= NULL
;
827 CHAR16
*pCmdLine
= NULL
;
828 EFI_STATUS Status
= EFI_SUCCESS
;
829 ventoy_grub_param
*pGrubParam
= NULL
;
830 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
831 ventoy_chain_head
*chain
= NULL
;
832 ventoy_grub_param_file_replace
*replace
= NULL
;
834 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
835 if (EFI_ERROR(Status
))
837 VtoyDebug("Failed to handle load image protocol %r", Status
);
841 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
842 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
843 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
845 if (StrStr(pCmdLine
, L
"debug"))
850 if (StrStr(pCmdLine
, L
"fallback"))
852 gBootFallBack
= TRUE
;
855 if (StrStr(pCmdLine
, L
"dotefi"))
860 if (StrStr(pCmdLine
, L
"isoefi=on"))
865 if (StrStr(pCmdLine
, L
"iso_udf"))
870 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
873 pPos
+= StrLen(L
"FirstTry=");
874 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
876 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
878 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
886 gEfiBootFileName
[0] = gFirstTryBootFile
;
887 gBootFileStartIndex
= 0;
890 debug("cmdline:<%s>", pCmdLine
);
892 if (gFirstTryBootFile
[0])
894 debug("First Try:<%s>", gFirstTryBootFile
);
897 pPos
= StrStr(pCmdLine
, L
"env_param=");
900 return EFI_INVALID_PARAMETER
;
903 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
904 grub_env_set
= pGrubParam
->grub_env_set
;
905 grub_env_get
= pGrubParam
->grub_env_get
;
906 pEnv
= grub_env_get("VTOY_CHKDEV_RESULT_STRING");
909 return EFI_INVALID_PARAMETER
;
912 if (pEnv
[0] != '0' || pEnv
[1] != 0)
914 ventoy_warn_invalid_device();
915 return EFI_INVALID_PARAMETER
;
918 g_file_replace_list
= &pGrubParam
->file_replace
;
919 old_cnt
= g_file_replace_list
->old_file_cnt
;
920 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
921 g_file_replace_list
->magic
,
922 g_file_replace_list
->new_file_virtual_id
,
924 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
925 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
926 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
927 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
931 for (i
= 0; i
< VTOY_MAX_CONF_REPLACE
; i
++)
933 replace
= pGrubParam
->img_replace
+ i
;
934 if (replace
->magic
== GRUB_IMG_REPLACE_MAGIC
)
936 ventoy_proc_img_replace_name(replace
);
937 old_cnt
= replace
->old_file_cnt
;
938 debug("img replace[%d]: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
940 replace
->new_file_virtual_id
,
942 old_cnt
> 0 ? replace
->old_file_name
[0] : "",
943 old_cnt
> 1 ? replace
->old_file_name
[1] : "",
944 old_cnt
> 2 ? replace
->old_file_name
[2] : "",
945 old_cnt
> 3 ? replace
->old_file_name
[3] : ""
947 g_img_replace_list
= pGrubParam
->img_replace
;
952 pPos
= StrStr(pCmdLine
, L
"mem:");
953 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
955 pPos
= StrStr(pPos
, L
"size:");
956 size
= StrDecimalToUintn(pPos
+ 5);
958 debug("memory addr:%p size:%lu", chain
, size
);
960 if (StrStr(pCmdLine
, L
"sector512"))
962 gSector512Mode
= TRUE
;
965 if (StrStr(pCmdLine
, L
"memdisk"))
967 g_iso_data_buf
= (UINT8
*)chain
+ sizeof(ventoy_chain_head
);
968 g_iso_buf_size
= size
- sizeof(ventoy_chain_head
);
969 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size
);
972 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
977 debug("This is normal mode");
978 g_chain
= AllocatePool(size
);
979 CopyMem(g_chain
, chain
, size
);
981 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
982 g_img_chunk_num
= g_chain
->img_chunk_num
;
983 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
984 g_override_chunk_num
= g_chain
->override_chunk_num
;
985 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
986 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
988 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
990 /* Workaround for Windows & ISO9660 */
991 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
993 g_fixup_iso9660_secover_enable
= TRUE
;
996 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
998 g_hook_keyboard
= TRUE
;
1001 if (g_os_param_reserved
[5] == 1 && g_os_param_reserved
[2] == ventoy_chain_linux
)
1003 ventoy_disable_secure_boot(ImageHandle
);
1006 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
1008 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
1010 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
1015 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
1018 ventoy_update_image_location(&(g_chain
->os_param
));
1022 ventoy_dump_chain(g_chain
);
1026 g_fix_windows_1st_cdrom_issue
= FALSE
;
1027 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
1028 ventoy_chain_wim
== g_os_param_reserved
[2])
1030 if (ventoy_is_cdrom_dp_exist())
1032 debug("fixup the 1st cdrom influences when boot windows ...");
1033 g_fix_windows_1st_cdrom_issue
= TRUE
;
1037 ventoy_debug_pause();
1043 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
1045 FreePool(g_sector_flag
);
1046 g_sector_flag_num
= 0;
1048 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1050 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1053 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1055 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1056 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1057 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1060 ventoy_delete_variable();
1062 if (g_vtoy_img_location_buf
)
1064 FreePool(g_vtoy_img_location_buf
);
1075 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
1077 /* don't add debug print in this function */
1079 if (g_fix_windows_1st_cdrom_issue
)
1081 ventoy_hook_1st_cdrom_start();
1084 /* let this the last */
1085 if (g_hook_keyboard
)
1087 ventoy_hook_keyboard_start();
1093 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
1095 /* don't add debug print in this function */
1097 if (g_fix_windows_1st_cdrom_issue
)
1099 ventoy_hook_1st_cdrom_stop();
1102 /* let this the last */
1103 if (g_hook_keyboard
)
1105 ventoy_hook_keyboard_stop();
1111 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
1118 EFI_HANDLE Image
= NULL
;
1119 EFI_HANDLE
*Handles
= NULL
;
1120 EFI_STATUS Status
= EFI_SUCCESS
;
1121 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
1122 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
1124 for (t
= 0; t
< 3; t
++)
1129 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
1130 NULL
, &Count
, &Handles
);
1131 if (EFI_ERROR(Status
))
1136 debug("ventoy_boot fs count:%u", Count
);
1138 for (i
= 0; i
< Count
; i
++)
1140 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
1141 if (EFI_ERROR(Status
))
1146 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
1148 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
1152 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1153 if (EFI_ERROR(Status
))
1155 debug("Failed to open device path protocol %r", Status
);
1159 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
1160 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
1162 debug("Not ventoy disk file system");
1166 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
1168 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
1169 StrSize(gEfiBootFileName
[j
]), &Image
);
1170 if (EFI_SUCCESS
== Status
)
1174 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
1177 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
1183 debug("Find boot file, now try to boot .....");
1184 ventoy_debug_pause();
1188 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
1191 if ((g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
) || g_img_replace_list
)
1193 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
1194 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
1197 ventoy_hook_start();
1198 /* can't add debug print here */
1199 //ventoy_wrapper_system();
1200 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
1203 if (EFI_ERROR(Status
))
1205 debug("Failed to start image %r", Status
);
1207 gBS
->UnloadImage(Image
);
1221 debug("Fs not found, now wait and retry...");
1228 return EFI_NOT_FOUND
;
1234 EFI_STATUS EFIAPI VentoyEfiMain
1236 IN EFI_HANDLE ImageHandle
,
1237 IN EFI_SYSTEM_TABLE
*SystemTable
1240 EFI_STATUS Status
= EFI_SUCCESS
;
1241 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
1243 g_sector_flag_num
= 512; /* initial value */
1245 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
1246 if (NULL
== g_sector_flag
)
1248 return EFI_OUT_OF_RESOURCES
;
1251 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
1252 if (EFI_SUCCESS
== Status
)
1254 g_con_simple_input_ex
= Protocol
;
1257 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1258 ventoy_clear_input();
1260 Status
= ventoy_parse_cmdline(ImageHandle
);
1261 if (EFI_ERROR(Status
))
1266 ventoy_disable_ex_filesystem();
1270 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_iso_data_buf
;
1271 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
1273 ventoy_save_ramdisk_param();
1277 ventoy_find_iso_disk(ImageHandle
);
1278 ventoy_find_iso_disk_fs(ImageHandle
);
1279 ventoy_load_isoefi_driver(ImageHandle
);
1282 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
1283 ventoy_debug_pause();
1285 Status
= ventoy_boot(ImageHandle
);
1287 ventoy_delete_ramdisk_param();
1289 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1291 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1294 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1295 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1296 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1297 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1302 ventoy_save_variable();
1303 Status
= ventoy_find_iso_disk(ImageHandle
);
1304 if (!EFI_ERROR(Status
))
1308 ventoy_find_iso_disk_fs(ImageHandle
);
1309 ventoy_load_isoefi_driver(ImageHandle
);
1312 ventoy_debug_pause();
1314 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
1316 ventoy_debug_pause();
1318 Status
= ventoy_boot(ImageHandle
);
1324 if (FALSE
== gDotEfiBoot
&& FALSE
== gBootFallBack
)
1326 if (EFI_NOT_FOUND
== Status
)
1328 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
1329 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
1334 ventoy_clear_input();
1335 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1337 if (gDotEfiBoot
&& (EFI_NOT_FOUND
== Status
))
1339 grub_env_set("vtoy_dotefi_retry", "YES");
1342 ventoy_enable_ex_filesystem();