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};
75 UINTN gBootFileStartIndex
= 1;
76 CONST CHAR16
*gEfiBootFileName
[] =
79 EFI_REMOVABLE_MEDIA_FILE_NAME
,
80 #if defined (MDE_CPU_IA32)
81 L
"\\EFI\\BOOT\\GRUBIA32.EFI",
82 L
"\\EFI\\BOOT\\BOOTia32.EFI",
83 L
"\\EFI\\BOOT\\bootia32.efi",
84 L
"\\efi\\boot\\bootia32.efi",
85 #elif defined (MDE_CPU_X64)
86 L
"\\EFI\\BOOT\\GRUBX64.EFI",
87 L
"\\EFI\\BOOT\\BOOTx64.EFI",
88 L
"\\EFI\\BOOT\\bootx64.efi",
89 L
"\\efi\\boot\\bootx64.efi",
90 #elif defined (MDE_CPU_ARM)
91 L
"\\EFI\\BOOT\\GRUBARM.EFI",
92 L
"\\EFI\\BOOT\\BOOTarm.EFI",
93 L
"\\EFI\\BOOT\\bootarm.efi",
94 L
"\\efi\\boot\\bootarm.efi",
95 #elif defined (MDE_CPU_AARCH64)
96 L
"\\EFI\\BOOT\\GRUBAA64.EFI",
97 L
"\\EFI\\BOOT\\BOOTaa64.EFI",
98 L
"\\EFI\\BOOT\\bootaa64.efi",
99 L
"\\efi\\boot\\bootaa64.efi",
104 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
109 VA_START (Marker
, Format
);
110 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
113 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
116 VOID EFIAPI
ventoy_clear_input(VOID
)
120 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
121 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
125 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
128 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
133 ventoy_img_chunk
*chunk
;
135 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
137 debug("##################### ventoy_dump_img_chunk #######################");
139 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
141 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
142 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
143 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
145 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
150 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
153 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
155 debug("image chunk size check success");
159 debug("image chunk size check failed %d", errcnt
);
162 ventoy_debug_pause();
165 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
168 ventoy_override_chunk
*chunk
;
170 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
172 debug("##################### ventoy_dump_override_chunk #######################");
174 for (i
= 0; i
< g_override_chunk_num
; i
++)
176 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
179 ventoy_debug_pause();
182 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
185 ventoy_virt_chunk
*node
;
187 debug("##################### ventoy_dump_virt_chunk #######################");
188 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
189 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
191 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
192 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
194 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
195 node
->mem_sector_start
,
196 node
->mem_sector_end
,
197 node
->mem_sector_offset
,
198 node
->remap_sector_start
,
199 node
->remap_sector_end
,
200 node
->org_sector_start
);
203 ventoy_debug_pause();
206 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
212 guid
= chain
->os_param
.vtoy_disk_guid
;
213 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
215 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
218 debug("##################### ventoy_dump_chain #######################");
220 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
221 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
222 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
223 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
224 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
225 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
226 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
227 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
228 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
229 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
230 g_os_param_reserved
[0],
231 g_os_param_reserved
[1],
232 g_os_param_reserved
[2],
233 g_os_param_reserved
[3],
234 g_os_param_reserved
[4]
237 ventoy_debug_pause();
239 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
240 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
241 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
242 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
243 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
244 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
245 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
246 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
247 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
249 ventoy_debug_pause();
251 ventoy_dump_img_chunk(chain
);
252 ventoy_dump_override_chunk(chain
);
253 ventoy_dump_virt_chunk(chain
);
256 static int ventoy_update_image_location(ventoy_os_param
*param
)
258 EFI_STATUS Status
= EFI_SUCCESS
;
264 ventoy_image_location
*location
= NULL
;
265 ventoy_image_disk_region
*region
= NULL
;
266 ventoy_img_chunk
*chunk
= g_chunk
;
268 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
270 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
271 if (EFI_ERROR(Status
) || NULL
== buffer
)
273 debug("Failed to allocate runtime pool %r\n", Status
);
277 address
= (UINTN
)buffer
;
278 g_vtoy_img_location_buf
= buffer
;
282 address
+= 4096 - (address
% 4096);
286 param
->vtoy_img_location_addr
= address
;
287 param
->vtoy_img_location_len
= length
;
289 /* update check sum */
290 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
292 chksum
+= *((UINT8
*)param
+ i
);
294 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
296 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
297 if (NULL
== location
)
302 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
303 location
->image_sector_size
= gSector512Mode
? 512 : 2048;
304 location
->disk_sector_size
= g_chain
->disk_sector_size
;
305 location
->region_count
= g_img_chunk_num
;
307 region
= location
->regions
;
311 for (i
= 0; i
< g_img_chunk_num
; i
++)
313 region
->image_sector_count
= chunk
->disk_end_sector
- chunk
->disk_start_sector
+ 1;
314 region
->image_start_sector
= chunk
->img_start_sector
* 4;
315 region
->disk_start_sector
= chunk
->disk_start_sector
;
322 for (i
= 0; i
< g_img_chunk_num
; i
++)
324 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
325 region
->image_start_sector
= chunk
->img_start_sector
;
326 region
->disk_start_sector
= chunk
->disk_start_sector
;
335 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
337 EFI_HANDLE Handle
= NULL
;
338 EFI_STATUS Status
= EFI_SUCCESS
;
339 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
340 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
341 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
343 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
349 pCurNode
= pTmpDevPath
;
350 while (!IsDevicePathEnd(pCurNode
))
352 pLastNode
= pCurNode
;
353 pCurNode
= NextDevicePathNode(pCurNode
);
357 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
360 pCurNode
= pTmpDevPath
;
361 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
362 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
364 FreePool(pTmpDevPath
);
369 STATIC ventoy_ram_disk g_backup_ramdisk_param
;
370 STATIC ventoy_os_param g_backup_os_param_var
;
373 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
376 EFI_STATUS Status
= EFI_SUCCESS
;
377 EFI_GUID VarGuid
= VENTOY_GUID
;
379 DataSize
= sizeof(g_backup_ramdisk_param
);
380 Status
= gRT
->GetVariable(L
"VentoyRamDisk", &VarGuid
, NULL
, &DataSize
, &g_backup_ramdisk_param
);
381 if (!EFI_ERROR(Status
))
383 debug("find previous ramdisk variable <%llu>", g_backup_ramdisk_param
.DiskSize
);
386 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
387 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
388 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
389 debug("set ramdisk variable %r", Status
);
394 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
396 EFI_STATUS Status
= EFI_SUCCESS
;
397 EFI_GUID VarGuid
= VENTOY_GUID
;
399 if (g_backup_ramdisk_param
.DiskSize
> 0 && g_backup_ramdisk_param
.PhyAddr
> 0)
401 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
402 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
403 sizeof(g_backup_ramdisk_param
), &g_backup_ramdisk_param
);
404 debug("resotre ramdisk variable %r", Status
);
408 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
409 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
411 debug("delete ramdisk variable %r", Status
);
417 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
420 EFI_STATUS Status
= EFI_SUCCESS
;
421 EFI_GUID VarGuid
= VENTOY_GUID
;
423 DataSize
= sizeof(g_backup_os_param_var
);
424 Status
= gRT
->GetVariable(L
"VentoyOsParam", &VarGuid
, NULL
, &DataSize
, &g_backup_os_param_var
);
425 if (!EFI_ERROR(Status
))
427 debug("find previous efi variable <%a>", g_backup_os_param_var
.vtoy_img_path
);
430 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
431 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
432 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
433 debug("set efi variable %r", Status
);
438 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
440 EFI_STATUS Status
= EFI_SUCCESS
;
441 EFI_GUID VarGuid
= VENTOY_GUID
;
443 if (0 == CompareMem(&(g_backup_os_param_var
.guid
), &VarGuid
, sizeof(EFI_GUID
)))
445 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
446 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
447 sizeof(g_backup_os_param_var
), &(g_backup_os_param_var
));
448 debug("restore efi variable %r", Status
);
452 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
453 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
455 debug("delete efi variable %r", Status
);
461 #if (VENTOY_DEVICE_WARN != 0)
462 STATIC VOID
ventoy_warn_invalid_device(VOID
)
464 STATIC BOOLEAN flag
= FALSE
;
472 gST
->ConOut
->ClearScreen(gST
->ConOut
);
473 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
474 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
475 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n\r\n\r\n");
477 gST
->ConOut
->OutputString(gST
->ConOut
, L
"This is NOT a standard Ventoy device and is NOT supported.\r\n\r\n");
478 gST
->ConOut
->OutputString(gST
->ConOut
, L
"You should follow the official instructions in https://www.ventoy.net\r\n");
480 gST
->ConOut
->OutputString(gST
->ConOut
, L
"\r\n\r\nWill exit after 10 seconds ...... ");
485 STATIC VOID
ventoy_warn_invalid_device(VOID
)
491 STATIC EFI_STATUS EFIAPI ventoy_load_image
493 IN EFI_HANDLE ImageHandle
,
494 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
495 IN CONST CHAR16
*FileName
,
496 IN UINTN FileNameLen
,
497 OUT EFI_HANDLE
*Image
500 EFI_STATUS Status
= EFI_SUCCESS
;
501 CHAR16 TmpBuf
[256] = {0};
502 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
503 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
505 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
506 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
507 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
508 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
509 pFilePath
->Header
.Length
[1] = 0;
510 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
512 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
515 return EFI_NOT_FOUND
;
518 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
520 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
528 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
533 MBR_HEAD
*pMBR
= NULL
;
534 UINT8
*pBuffer
= NULL
;
536 EFI_STATUS Status
= EFI_SUCCESS
;
537 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
539 pBuffer
= AllocatePool(2048);
542 return EFI_OUT_OF_RESOURCES
;
545 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
546 NULL
, &Count
, &Handles
);
547 if (EFI_ERROR(Status
))
553 for (i
= 0; i
< Count
; i
++)
555 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
556 if (EFI_ERROR(Status
))
561 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
562 debug("This Disk size: %llu", DiskSize
);
563 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
568 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
569 if (EFI_ERROR(Status
))
571 debug("ReadBlocks filed %r", Status
);
575 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
577 pMBR
= (MBR_HEAD
*)pBuffer
;
578 if (pMBR
->PartTbl
[0].FsFlag
!= 0xEE)
580 if (pMBR
->PartTbl
[0].StartSectorId
!= 2048 ||
581 pMBR
->PartTbl
[1].SectorCount
!= 65536 ||
582 pMBR
->PartTbl
[1].StartSectorId
!= pMBR
->PartTbl
[0].StartSectorId
+ pMBR
->PartTbl
[0].SectorCount
)
584 debug("Failed to check disk part table");
585 ventoy_warn_invalid_device();
589 gBlockData
.RawBlockIoHandle
= Handles
[i
];
590 gBlockData
.pRawBlockIo
= pBlockIo
;
591 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
592 (VOID
**)&(gBlockData
.pDiskDevPath
),
595 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
597 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
598 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
607 return EFI_NOT_FOUND
;
616 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
620 EFI_HANDLE Parent
= NULL
;
621 EFI_HANDLE
*Handles
= NULL
;
622 EFI_STATUS Status
= EFI_SUCCESS
;
623 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
624 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
626 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
627 NULL
, &Count
, &Handles
);
628 if (EFI_ERROR(Status
))
633 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
635 for (i
= 0; i
< Count
; i
++)
637 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
638 if (EFI_ERROR(Status
))
643 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
647 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
648 if (EFI_ERROR(Status
))
650 debug("Failed to open device path protocol %r", Status
);
654 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
655 Parent
= ventoy_get_parent_handle(pDevPath
);
657 if (Parent
== gBlockData
.RawBlockIoHandle
)
659 debug("Find ventoy disk fs");
660 gBlockData
.DiskFsHandle
= Handles
[i
];
661 gBlockData
.pDiskFs
= pFile
;
662 gBlockData
.pDiskFsDevPath
= pDevPath
;
672 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
674 EFI_HANDLE Image
= NULL
;
675 EFI_STATUS Status
= EFI_SUCCESS
;
676 CHAR16 LogVar
[4] = L
"5";
680 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
682 sizeof(gUdfEfiDriverPath
),
684 debug("load iso UDF efi driver status:%r", Status
);
688 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
689 gIso9660EfiDriverPath
,
690 sizeof(gIso9660EfiDriverPath
),
692 debug("load iso 9660 efi driver status:%r", Status
);
697 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
698 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
699 sizeof(LogVar
), LogVar
);
702 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
703 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
704 sizeof(LogVar
), LogVar
);
706 gBlockData
.IsoDriverImage
= Image
;
707 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
708 debug("Start iso efi driver status:%r", Status
);
713 STATIC EFI_STATUS
ventoy_proc_img_replace_name(ventoy_grub_param_file_replace
*replace
)
718 if (replace
->magic
!= GRUB_IMG_REPLACE_MAGIC
)
723 if (replace
->old_file_name
[0][0] == 0)
728 AsciiStrCpyS(tmp
, sizeof(tmp
), replace
->old_file_name
[0]);
730 for (i
= 0; i
< 256 && tmp
[i
]; i
++)
738 AsciiStrCpyS(replace
->old_file_name
[0], 256, tmp
);
742 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
748 const char *pEnv
= NULL
;
750 CHAR16
*pCmdLine
= NULL
;
751 EFI_STATUS Status
= EFI_SUCCESS
;
752 ventoy_grub_param
*pGrubParam
= NULL
;
753 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
754 ventoy_chain_head
*chain
= NULL
;
756 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
757 if (EFI_ERROR(Status
))
759 VtoyDebug("Failed to handle load image protocol %r", Status
);
763 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
764 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
765 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
767 if (StrStr(pCmdLine
, L
"debug"))
772 if (StrStr(pCmdLine
, L
"fallback"))
774 gBootFallBack
= TRUE
;
777 if (StrStr(pCmdLine
, L
"dotefi"))
782 if (StrStr(pCmdLine
, L
"isoefi=on"))
787 if (StrStr(pCmdLine
, L
"iso_udf"))
792 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
795 pPos
+= StrLen(L
"FirstTry=");
796 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
798 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
800 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
808 gEfiBootFileName
[0] = gFirstTryBootFile
;
809 gBootFileStartIndex
= 0;
812 debug("cmdline:<%s>", pCmdLine
);
814 if (gFirstTryBootFile
[0])
816 debug("First Try:<%s>", gFirstTryBootFile
);
819 pPos
= StrStr(pCmdLine
, L
"env_param=");
822 return EFI_INVALID_PARAMETER
;
825 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
826 grub_env_set
= pGrubParam
->grub_env_set
;
827 grub_env_get
= pGrubParam
->grub_env_get
;
828 pEnv
= grub_env_get("VTOY_CHKDEV_RESULT_STRING");
831 return EFI_INVALID_PARAMETER
;
834 if (pEnv
[0] != '0' || pEnv
[1] != 0)
836 ventoy_warn_invalid_device();
837 return EFI_INVALID_PARAMETER
;
840 g_file_replace_list
= &pGrubParam
->file_replace
;
841 old_cnt
= g_file_replace_list
->old_file_cnt
;
842 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
843 g_file_replace_list
->magic
,
844 g_file_replace_list
->new_file_virtual_id
,
846 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
847 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
848 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
849 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
852 g_img_replace_list
= &pGrubParam
->img_replace
;
853 ventoy_proc_img_replace_name(g_img_replace_list
);
854 old_cnt
= g_img_replace_list
->old_file_cnt
;
855 debug("img replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
856 g_img_replace_list
->magic
,
857 g_img_replace_list
->new_file_virtual_id
,
859 old_cnt
> 0 ? g_img_replace_list
->old_file_name
[0] : "",
860 old_cnt
> 1 ? g_img_replace_list
->old_file_name
[1] : "",
861 old_cnt
> 2 ? g_img_replace_list
->old_file_name
[2] : "",
862 old_cnt
> 3 ? g_img_replace_list
->old_file_name
[3] : ""
865 pPos
= StrStr(pCmdLine
, L
"mem:");
866 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
868 pPos
= StrStr(pPos
, L
"size:");
869 size
= StrDecimalToUintn(pPos
+ 5);
871 debug("memory addr:%p size:%lu", chain
, size
);
873 if (StrStr(pCmdLine
, L
"sector512"))
875 gSector512Mode
= TRUE
;
878 if (StrStr(pCmdLine
, L
"memdisk"))
880 g_iso_data_buf
= (UINT8
*)chain
+ sizeof(ventoy_chain_head
);
881 g_iso_buf_size
= size
- sizeof(ventoy_chain_head
);
882 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size
);
889 debug("This is normal mode");
890 g_chain
= AllocatePool(size
);
891 CopyMem(g_chain
, chain
, size
);
893 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
894 g_img_chunk_num
= g_chain
->img_chunk_num
;
895 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
896 g_override_chunk_num
= g_chain
->override_chunk_num
;
897 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
898 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
900 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
902 /* Workaround for Windows & ISO9660 */
903 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
905 g_fixup_iso9660_secover_enable
= TRUE
;
908 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
910 g_hook_keyboard
= TRUE
;
913 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
915 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
917 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
922 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
925 ventoy_update_image_location(&(g_chain
->os_param
));
929 ventoy_dump_chain(g_chain
);
933 g_fix_windows_1st_cdrom_issue
= FALSE
;
934 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
935 ventoy_chain_wim
== g_os_param_reserved
[2])
937 if (ventoy_is_cdrom_dp_exist())
939 debug("fixup the 1st cdrom influences when boot windows ...");
940 g_fix_windows_1st_cdrom_issue
= TRUE
;
944 ventoy_debug_pause();
950 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
952 FreePool(g_sector_flag
);
953 g_sector_flag_num
= 0;
955 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
957 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
960 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
962 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
963 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
964 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
967 ventoy_delete_variable();
969 if (g_vtoy_img_location_buf
)
971 FreePool(g_vtoy_img_location_buf
);
982 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
984 /* don't add debug print in this function */
986 if (g_fix_windows_1st_cdrom_issue
)
988 ventoy_hook_1st_cdrom_start();
991 /* let this the last */
994 ventoy_hook_keyboard_start();
1000 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
1002 /* don't add debug print in this function */
1004 if (g_fix_windows_1st_cdrom_issue
)
1006 ventoy_hook_1st_cdrom_stop();
1009 /* let this the last */
1010 if (g_hook_keyboard
)
1012 ventoy_hook_keyboard_stop();
1018 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
1025 EFI_HANDLE Image
= NULL
;
1026 EFI_HANDLE
*Handles
= NULL
;
1027 EFI_STATUS Status
= EFI_SUCCESS
;
1028 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
1029 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
1031 for (t
= 0; t
< 3; t
++)
1036 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
1037 NULL
, &Count
, &Handles
);
1038 if (EFI_ERROR(Status
))
1043 debug("ventoy_boot fs count:%u", Count
);
1045 for (i
= 0; i
< Count
; i
++)
1047 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
1048 if (EFI_ERROR(Status
))
1053 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
1055 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
1059 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1060 if (EFI_ERROR(Status
))
1062 debug("Failed to open device path protocol %r", Status
);
1066 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
1067 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
1069 debug("Not ventoy disk file system");
1073 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
1075 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
1076 StrSize(gEfiBootFileName
[j
]), &Image
);
1077 if (EFI_SUCCESS
== Status
)
1081 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
1084 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
1090 debug("Find boot file, now try to boot .....");
1091 ventoy_debug_pause();
1095 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
1098 if ((g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
) ||
1099 (g_img_replace_list
&& g_img_replace_list
->magic
== GRUB_IMG_REPLACE_MAGIC
))
1101 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
1102 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
1105 ventoy_hook_start();
1106 /* can't add debug print here */
1107 //ventoy_wrapper_system();
1108 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
1111 if (EFI_ERROR(Status
))
1113 debug("Failed to start image %r", Status
);
1115 gBS
->UnloadImage(Image
);
1129 debug("Fs not found, now wait and retry...");
1136 return EFI_NOT_FOUND
;
1142 EFI_STATUS EFIAPI VentoyEfiMain
1144 IN EFI_HANDLE ImageHandle
,
1145 IN EFI_SYSTEM_TABLE
*SystemTable
1148 EFI_STATUS Status
= EFI_SUCCESS
;
1149 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
1151 g_sector_flag_num
= 512; /* initial value */
1153 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
1154 if (NULL
== g_sector_flag
)
1156 return EFI_OUT_OF_RESOURCES
;
1159 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
1160 if (EFI_SUCCESS
== Status
)
1162 g_con_simple_input_ex
= Protocol
;
1165 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1166 ventoy_clear_input();
1168 Status
= ventoy_parse_cmdline(ImageHandle
);
1169 if (EFI_ERROR(Status
))
1174 ventoy_disable_ex_filesystem();
1178 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_iso_data_buf
;
1179 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
1181 ventoy_save_ramdisk_param();
1185 ventoy_find_iso_disk(ImageHandle
);
1186 ventoy_find_iso_disk_fs(ImageHandle
);
1187 ventoy_load_isoefi_driver(ImageHandle
);
1190 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
1191 ventoy_debug_pause();
1193 Status
= ventoy_boot(ImageHandle
);
1195 ventoy_delete_ramdisk_param();
1197 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1199 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1202 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1203 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1204 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1205 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1210 ventoy_save_variable();
1211 Status
= ventoy_find_iso_disk(ImageHandle
);
1212 if (!EFI_ERROR(Status
))
1216 ventoy_find_iso_disk_fs(ImageHandle
);
1217 ventoy_load_isoefi_driver(ImageHandle
);
1220 ventoy_debug_pause();
1222 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
1224 ventoy_debug_pause();
1226 Status
= ventoy_boot(ImageHandle
);
1232 if (FALSE
== gDotEfiBoot
&& FALSE
== gBootFallBack
)
1234 if (EFI_NOT_FOUND
== Status
)
1236 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
1237 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
1242 ventoy_clear_input();
1243 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1245 if (gDotEfiBoot
&& (EFI_NOT_FOUND
== Status
))
1247 grub_env_set("vtoy_dotefi_retry", "YES");
1250 ventoy_enable_ex_filesystem();