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
;
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)
582 pMBR
= (MBR_HEAD
*)pBuffer
;
583 if (g_os_param_reserved
[6] == 0 && pMBR
->PartTbl
[0].FsFlag
!= 0xEE)
585 if (pMBR
->PartTbl
[0].StartSectorId
!= 2048 ||
586 pMBR
->PartTbl
[1].SectorCount
!= 65536 ||
587 pMBR
->PartTbl
[1].StartSectorId
!= pMBR
->PartTbl
[0].StartSectorId
+ pMBR
->PartTbl
[0].SectorCount
)
589 debug("Failed to check disk part table");
590 ventoy_warn_invalid_device();
594 gBlockData
.RawBlockIoHandle
= Handles
[i
];
595 gBlockData
.pRawBlockIo
= pBlockIo
;
596 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
597 (VOID
**)&(gBlockData
.pDiskDevPath
),
600 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
602 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
603 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
612 return EFI_NOT_FOUND
;
621 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
625 EFI_HANDLE Parent
= NULL
;
626 EFI_HANDLE
*Handles
= NULL
;
627 EFI_STATUS Status
= EFI_SUCCESS
;
628 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
629 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
631 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
632 NULL
, &Count
, &Handles
);
633 if (EFI_ERROR(Status
))
638 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
640 for (i
= 0; i
< Count
; i
++)
642 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
643 if (EFI_ERROR(Status
))
648 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
652 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
653 if (EFI_ERROR(Status
))
655 debug("Failed to open device path protocol %r", Status
);
659 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
660 Parent
= ventoy_get_parent_handle(pDevPath
);
662 if (Parent
== gBlockData
.RawBlockIoHandle
)
664 debug("Find ventoy disk fs");
665 gBlockData
.DiskFsHandle
= Handles
[i
];
666 gBlockData
.pDiskFs
= pFile
;
667 gBlockData
.pDiskFsDevPath
= pDevPath
;
677 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
679 EFI_HANDLE Image
= NULL
;
680 EFI_STATUS Status
= EFI_SUCCESS
;
681 CHAR16 LogVar
[4] = L
"5";
685 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
687 sizeof(gUdfEfiDriverPath
),
689 debug("load iso UDF efi driver status:%r", Status
);
693 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
694 gIso9660EfiDriverPath
,
695 sizeof(gIso9660EfiDriverPath
),
697 debug("load iso 9660 efi driver status:%r", Status
);
702 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
703 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
704 sizeof(LogVar
), LogVar
);
707 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
708 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
709 sizeof(LogVar
), LogVar
);
711 gBlockData
.IsoDriverImage
= Image
;
712 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
713 debug("Start iso efi driver status:%r", Status
);
718 STATIC EFI_STATUS
ventoy_proc_img_replace_name(ventoy_grub_param_file_replace
*replace
)
723 if (replace
->magic
!= GRUB_IMG_REPLACE_MAGIC
)
728 if (replace
->old_file_name
[0][0] == 0)
733 AsciiStrCpyS(tmp
, sizeof(tmp
), replace
->old_file_name
[0]);
735 for (i
= 0; i
< 256 && tmp
[i
]; i
++)
743 AsciiStrCpyS(replace
->old_file_name
[0], 256, tmp
);
747 EFI_STATUS EFIAPI ventoy_get_variable_wrapper
749 IN CHAR16
*VariableName
,
750 IN EFI_GUID
*VendorGuid
,
751 OUT UINT32
*Attributes
, OPTIONAL
752 IN OUT UINTN
*DataSize
,
753 OUT VOID
*Data OPTIONAL
756 EFI_STATUS Status
= EFI_SUCCESS
;
758 Status
= g_org_get_variable(VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
759 if (StrCmp(VariableName
, L
"SecureBoot") == 0)
761 if ((*DataSize
== 1) && Data
)
770 EFI_STATUS EFIAPI ventoy_exit_boot_service_wrapper
772 IN EFI_HANDLE ImageHandle
,
776 if (g_org_get_variable
)
778 gRT
->GetVariable
= g_org_get_variable
;
779 g_org_get_variable
= NULL
;
782 return g_org_exit_boot_service(ImageHandle
, MapKey
);
785 STATIC EFI_STATUS EFIAPI
ventoy_disable_secure_boot(IN EFI_HANDLE ImageHandle
)
789 EFI_STATUS Status
= EFI_SUCCESS
;
791 Status
= gRT
->GetVariable(L
"SecureBoot", &gEfiGlobalVariableGuid
, NULL
, &DataSize
, &Value
);
792 if (!EFI_ERROR(Status
))
794 if (DataSize
== 1 && Value
== 0)
796 debug("Current secure boot is off, no need to disable");
801 debug("ventoy_disable_secure_boot");
803 /* step1: wrapper security protocol. */
804 /* Do we still need it since we have been loaded ? */
807 /* step2: fake SecureBoot variable */
808 g_org_exit_boot_service
= gBS
->ExitBootServices
;
809 gBS
->ExitBootServices
= ventoy_exit_boot_service_wrapper
;
811 g_org_get_variable
= gRT
->GetVariable
;
812 gRT
->GetVariable
= ventoy_get_variable_wrapper
;
818 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
824 const char *pEnv
= NULL
;
826 CHAR16
*pCmdLine
= NULL
;
827 EFI_STATUS Status
= EFI_SUCCESS
;
828 ventoy_grub_param
*pGrubParam
= NULL
;
829 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
830 ventoy_chain_head
*chain
= NULL
;
832 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
833 if (EFI_ERROR(Status
))
835 VtoyDebug("Failed to handle load image protocol %r", Status
);
839 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
840 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
841 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
843 if (StrStr(pCmdLine
, L
"debug"))
848 if (StrStr(pCmdLine
, L
"fallback"))
850 gBootFallBack
= TRUE
;
853 if (StrStr(pCmdLine
, L
"dotefi"))
858 if (StrStr(pCmdLine
, L
"isoefi=on"))
863 if (StrStr(pCmdLine
, L
"iso_udf"))
868 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
871 pPos
+= StrLen(L
"FirstTry=");
872 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
874 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
876 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
884 gEfiBootFileName
[0] = gFirstTryBootFile
;
885 gBootFileStartIndex
= 0;
888 debug("cmdline:<%s>", pCmdLine
);
890 if (gFirstTryBootFile
[0])
892 debug("First Try:<%s>", gFirstTryBootFile
);
895 pPos
= StrStr(pCmdLine
, L
"env_param=");
898 return EFI_INVALID_PARAMETER
;
901 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
902 grub_env_set
= pGrubParam
->grub_env_set
;
903 grub_env_get
= pGrubParam
->grub_env_get
;
904 pEnv
= grub_env_get("VTOY_CHKDEV_RESULT_STRING");
907 return EFI_INVALID_PARAMETER
;
910 if (pEnv
[0] != '0' || pEnv
[1] != 0)
912 ventoy_warn_invalid_device();
913 return EFI_INVALID_PARAMETER
;
916 g_file_replace_list
= &pGrubParam
->file_replace
;
917 old_cnt
= g_file_replace_list
->old_file_cnt
;
918 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
919 g_file_replace_list
->magic
,
920 g_file_replace_list
->new_file_virtual_id
,
922 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
923 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
924 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
925 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
928 g_img_replace_list
= &pGrubParam
->img_replace
;
929 ventoy_proc_img_replace_name(g_img_replace_list
);
930 old_cnt
= g_img_replace_list
->old_file_cnt
;
931 debug("img replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
932 g_img_replace_list
->magic
,
933 g_img_replace_list
->new_file_virtual_id
,
935 old_cnt
> 0 ? g_img_replace_list
->old_file_name
[0] : "",
936 old_cnt
> 1 ? g_img_replace_list
->old_file_name
[1] : "",
937 old_cnt
> 2 ? g_img_replace_list
->old_file_name
[2] : "",
938 old_cnt
> 3 ? g_img_replace_list
->old_file_name
[3] : ""
941 pPos
= StrStr(pCmdLine
, L
"mem:");
942 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
944 pPos
= StrStr(pPos
, L
"size:");
945 size
= StrDecimalToUintn(pPos
+ 5);
947 debug("memory addr:%p size:%lu", chain
, size
);
949 if (StrStr(pCmdLine
, L
"sector512"))
951 gSector512Mode
= TRUE
;
954 if (StrStr(pCmdLine
, L
"memdisk"))
956 g_iso_data_buf
= (UINT8
*)chain
+ sizeof(ventoy_chain_head
);
957 g_iso_buf_size
= size
- sizeof(ventoy_chain_head
);
958 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size
);
961 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
966 debug("This is normal mode");
967 g_chain
= AllocatePool(size
);
968 CopyMem(g_chain
, chain
, size
);
970 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
971 g_img_chunk_num
= g_chain
->img_chunk_num
;
972 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
973 g_override_chunk_num
= g_chain
->override_chunk_num
;
974 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
975 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
977 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
979 /* Workaround for Windows & ISO9660 */
980 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
982 g_fixup_iso9660_secover_enable
= TRUE
;
985 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
987 g_hook_keyboard
= TRUE
;
990 if (g_os_param_reserved
[5] == 1 && g_os_param_reserved
[2] == ventoy_chain_linux
)
992 ventoy_disable_secure_boot(ImageHandle
);
995 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
997 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
999 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
1004 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
1007 ventoy_update_image_location(&(g_chain
->os_param
));
1011 ventoy_dump_chain(g_chain
);
1015 g_fix_windows_1st_cdrom_issue
= FALSE
;
1016 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
1017 ventoy_chain_wim
== g_os_param_reserved
[2])
1019 if (ventoy_is_cdrom_dp_exist())
1021 debug("fixup the 1st cdrom influences when boot windows ...");
1022 g_fix_windows_1st_cdrom_issue
= TRUE
;
1026 ventoy_debug_pause();
1032 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
1034 FreePool(g_sector_flag
);
1035 g_sector_flag_num
= 0;
1037 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1039 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1042 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1044 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1045 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1046 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1049 ventoy_delete_variable();
1051 if (g_vtoy_img_location_buf
)
1053 FreePool(g_vtoy_img_location_buf
);
1064 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
1066 /* don't add debug print in this function */
1068 if (g_fix_windows_1st_cdrom_issue
)
1070 ventoy_hook_1st_cdrom_start();
1073 /* let this the last */
1074 if (g_hook_keyboard
)
1076 ventoy_hook_keyboard_start();
1082 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
1084 /* don't add debug print in this function */
1086 if (g_fix_windows_1st_cdrom_issue
)
1088 ventoy_hook_1st_cdrom_stop();
1091 /* let this the last */
1092 if (g_hook_keyboard
)
1094 ventoy_hook_keyboard_stop();
1100 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
1107 EFI_HANDLE Image
= NULL
;
1108 EFI_HANDLE
*Handles
= NULL
;
1109 EFI_STATUS Status
= EFI_SUCCESS
;
1110 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
1111 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
1113 for (t
= 0; t
< 3; t
++)
1118 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
1119 NULL
, &Count
, &Handles
);
1120 if (EFI_ERROR(Status
))
1125 debug("ventoy_boot fs count:%u", Count
);
1127 for (i
= 0; i
< Count
; i
++)
1129 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
1130 if (EFI_ERROR(Status
))
1135 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
1137 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
1141 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1142 if (EFI_ERROR(Status
))
1144 debug("Failed to open device path protocol %r", Status
);
1148 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
1149 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
1151 debug("Not ventoy disk file system");
1155 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
1157 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
1158 StrSize(gEfiBootFileName
[j
]), &Image
);
1159 if (EFI_SUCCESS
== Status
)
1163 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
1166 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
1172 debug("Find boot file, now try to boot .....");
1173 ventoy_debug_pause();
1177 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
1180 if ((g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
) ||
1181 (g_img_replace_list
&& g_img_replace_list
->magic
== GRUB_IMG_REPLACE_MAGIC
))
1183 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
1184 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
1187 ventoy_hook_start();
1188 /* can't add debug print here */
1189 //ventoy_wrapper_system();
1190 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
1193 if (EFI_ERROR(Status
))
1195 debug("Failed to start image %r", Status
);
1197 gBS
->UnloadImage(Image
);
1211 debug("Fs not found, now wait and retry...");
1218 return EFI_NOT_FOUND
;
1224 EFI_STATUS EFIAPI VentoyEfiMain
1226 IN EFI_HANDLE ImageHandle
,
1227 IN EFI_SYSTEM_TABLE
*SystemTable
1230 EFI_STATUS Status
= EFI_SUCCESS
;
1231 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
1233 g_sector_flag_num
= 512; /* initial value */
1235 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
1236 if (NULL
== g_sector_flag
)
1238 return EFI_OUT_OF_RESOURCES
;
1241 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
1242 if (EFI_SUCCESS
== Status
)
1244 g_con_simple_input_ex
= Protocol
;
1247 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1248 ventoy_clear_input();
1250 Status
= ventoy_parse_cmdline(ImageHandle
);
1251 if (EFI_ERROR(Status
))
1256 ventoy_disable_ex_filesystem();
1260 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_iso_data_buf
;
1261 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
1263 ventoy_save_ramdisk_param();
1267 ventoy_find_iso_disk(ImageHandle
);
1268 ventoy_find_iso_disk_fs(ImageHandle
);
1269 ventoy_load_isoefi_driver(ImageHandle
);
1272 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
1273 ventoy_debug_pause();
1275 Status
= ventoy_boot(ImageHandle
);
1277 ventoy_delete_ramdisk_param();
1279 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1281 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1284 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1285 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1286 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1287 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1292 ventoy_save_variable();
1293 Status
= ventoy_find_iso_disk(ImageHandle
);
1294 if (!EFI_ERROR(Status
))
1298 ventoy_find_iso_disk_fs(ImageHandle
);
1299 ventoy_load_isoefi_driver(ImageHandle
);
1302 ventoy_debug_pause();
1304 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
1306 ventoy_debug_pause();
1308 Status
= ventoy_boot(ImageHandle
);
1314 if (FALSE
== gDotEfiBoot
&& FALSE
== gBootFallBack
)
1316 if (EFI_NOT_FOUND
== Status
)
1318 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
1319 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
1324 ventoy_clear_input();
1325 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1327 if (gDotEfiBoot
&& (EFI_NOT_FOUND
== Status
))
1329 grub_env_set("vtoy_dotefi_retry", "YES");
1332 ventoy_enable_ex_filesystem();