1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <Library/DebugLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DevicePathLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiApplicationEntryPoint.h>
31 #include <Protocol/LoadedImage.h>
32 #include <Guid/FileInfo.h>
33 #include <Guid/FileSystemInfo.h>
34 #include <Protocol/BlockIo.h>
35 #include <Protocol/RamDisk.h>
36 #include <Protocol/SimpleFileSystem.h>
39 BOOLEAN gDebugPrint
= FALSE
;
40 BOOLEAN gDotEfiBoot
= FALSE
;
41 BOOLEAN gLoadIsoEfi
= FALSE
;
42 BOOLEAN gIsoUdf
= FALSE
;
43 ventoy_ram_disk g_ramdisk_param
;
44 ventoy_chain_head
*g_chain
;
45 ventoy_img_chunk
*g_chunk
;
46 UINT8
*g_os_param_reserved
;
47 UINT32 g_img_chunk_num
;
48 ventoy_override_chunk
*g_override_chunk
;
49 UINT32 g_override_chunk_num
;
50 ventoy_virt_chunk
*g_virt_chunk
;
51 UINT32 g_virt_chunk_num
;
52 vtoy_block_data gBlockData
;
53 static grub_env_get_pf grub_env_get
= NULL
;
54 static grub_env_set_pf grub_env_set
= NULL
;
56 ventoy_grub_param_file_replace
*g_file_replace_list
= NULL
;
57 ventoy_efi_file_replace g_efi_file_replace
;
59 CONST CHAR16 gIso9660EfiDriverPath
[] = ISO9660_EFI_DRIVER_PATH
;
60 CONST CHAR16 gUdfEfiDriverPath
[] = UDF_EFI_DRIVER_PATH
;
62 BOOLEAN g_fix_windows_1st_cdrom_issue
= FALSE
;
64 STATIC BOOLEAN g_hook_keyboard
= FALSE
;
66 CHAR16 gFirstTryBootFile
[256] = {0};
69 UINTN gBootFileStartIndex
= 1;
70 CONST CHAR16
*gEfiBootFileName
[] =
73 EFI_REMOVABLE_MEDIA_FILE_NAME
,
74 #if defined (MDE_CPU_IA32)
75 L
"\\EFI\\BOOT\\GRUBIA32.EFI",
76 L
"\\EFI\\BOOT\\BOOTia32.EFI",
77 L
"\\EFI\\BOOT\\bootia32.efi",
78 L
"\\efi\\boot\\bootia32.efi",
79 #elif defined (MDE_CPU_X64)
80 L
"\\EFI\\BOOT\\GRUBX64.EFI",
81 L
"\\EFI\\BOOT\\BOOTx64.EFI",
82 L
"\\EFI\\BOOT\\bootx64.efi",
83 L
"\\efi\\boot\\bootx64.efi",
84 #elif defined (MDE_CPU_ARM)
85 L
"\\EFI\\BOOT\\GRUBARM.EFI",
86 L
"\\EFI\\BOOT\\BOOTarm.EFI",
87 L
"\\EFI\\BOOT\\bootarm.efi",
88 L
"\\efi\\boot\\bootarm.efi",
89 #elif defined (MDE_CPU_AARCH64)
90 L
"\\EFI\\BOOT\\GRUBAA64.EFI",
91 L
"\\EFI\\BOOT\\BOOTaa64.EFI",
92 L
"\\EFI\\BOOT\\bootaa64.efi",
93 L
"\\efi\\boot\\bootaa64.efi",
98 VOID EFIAPI
VtoyDebug(IN CONST CHAR8
*Format
, ...)
103 VA_START (Marker
, Format
);
104 UnicodeVSPrintAsciiFormat(Buffer
, sizeof(Buffer
), Format
, Marker
);
107 gST
->ConOut
->OutputString(gST
->ConOut
, Buffer
);
110 VOID EFIAPI
ventoy_clear_input(VOID
)
114 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
115 while (EFI_SUCCESS
== gST
->ConIn
->ReadKeyStroke(gST
->ConIn
, &Key
))
119 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
122 static void EFIAPI
ventoy_dump_img_chunk(ventoy_chain_head
*chain
)
127 ventoy_img_chunk
*chunk
;
129 chunk
= (ventoy_img_chunk
*)((char *)chain
+ chain
->img_chunk_offset
);
131 debug("##################### ventoy_dump_img_chunk #######################");
133 for (i
= 0; i
< chain
->img_chunk_num
; i
++)
135 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
136 i
, chunk
[i
].img_start_sector
, chunk
[i
].img_end_sector
,
137 chunk
[i
].disk_start_sector
, chunk
[i
].disk_end_sector
);
139 if (i
> 0 && (chunk
[i
].img_start_sector
!= chunk
[i
- 1].img_end_sector
+ 1))
144 img_sec
+= chunk
[i
].img_end_sector
- chunk
[i
].img_start_sector
+ 1;
147 if (errcnt
== 0 && (img_sec
* 2048 == g_chain
->real_img_size_in_bytes
))
149 debug("image chunk size check success");
153 debug("image chunk size check failed %d", errcnt
);
156 ventoy_debug_pause();
159 static void EFIAPI
ventoy_dump_override_chunk(ventoy_chain_head
*chain
)
162 ventoy_override_chunk
*chunk
;
164 chunk
= (ventoy_override_chunk
*)((char *)chain
+ chain
->override_chunk_offset
);
166 debug("##################### ventoy_dump_override_chunk #######################");
168 for (i
= 0; i
< g_override_chunk_num
; i
++)
170 debug("%2u: [ %llu, %u ]", i
, chunk
[i
].img_offset
, chunk
[i
].override_size
);
173 ventoy_debug_pause();
176 static void EFIAPI
ventoy_dump_virt_chunk(ventoy_chain_head
*chain
)
179 ventoy_virt_chunk
*node
;
181 debug("##################### ventoy_dump_virt_chunk #######################");
182 debug("virt_chunk_offset=%u", chain
->virt_chunk_offset
);
183 debug("virt_chunk_num=%u", chain
->virt_chunk_num
);
185 node
= (ventoy_virt_chunk
*)((char *)chain
+ chain
->virt_chunk_offset
);
186 for (i
= 0; i
< chain
->virt_chunk_num
; i
++, node
++)
188 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i
,
189 node
->mem_sector_start
,
190 node
->mem_sector_end
,
191 node
->mem_sector_offset
,
192 node
->remap_sector_start
,
193 node
->remap_sector_end
,
194 node
->org_sector_start
);
197 ventoy_debug_pause();
200 static void EFIAPI
ventoy_dump_chain(ventoy_chain_head
*chain
)
206 guid
= chain
->os_param
.vtoy_disk_guid
;
207 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
209 chksum
+= *((UINT8
*)(&(chain
->os_param
)) + i
);
212 debug("##################### ventoy_dump_chain #######################");
214 debug("os_param->chksum=0x%x (%a)", chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
215 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid
[0], guid
[1], guid
[2], guid
[3]);
216 debug("os_param->vtoy_disk_size=%llu", chain
->os_param
.vtoy_disk_size
);
217 debug("os_param->vtoy_disk_part_id=%u", chain
->os_param
.vtoy_disk_part_id
);
218 debug("os_param->vtoy_disk_part_type=%u", chain
->os_param
.vtoy_disk_part_type
);
219 debug("os_param->vtoy_img_path=<%a>", chain
->os_param
.vtoy_img_path
);
220 debug("os_param->vtoy_img_size=<%llu>", chain
->os_param
.vtoy_img_size
);
221 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain
->os_param
.vtoy_img_location_addr
);
222 debug("os_param->vtoy_img_location_len=<%u>", chain
->os_param
.vtoy_img_location_len
);
223 debug("os_param->vtoy_reserved=<%u %u %u %u %u>",
224 g_os_param_reserved
[0],
225 g_os_param_reserved
[1],
226 g_os_param_reserved
[2],
227 g_os_param_reserved
[3],
228 g_os_param_reserved
[4]
231 ventoy_debug_pause();
233 debug("chain->disk_drive=0x%x", chain
->disk_drive
);
234 debug("chain->disk_sector_size=%u", chain
->disk_sector_size
);
235 debug("chain->real_img_size_in_bytes=%llu", chain
->real_img_size_in_bytes
);
236 debug("chain->virt_img_size_in_bytes=%llu", chain
->virt_img_size_in_bytes
);
237 debug("chain->boot_catalog=%u", chain
->boot_catalog
);
238 debug("chain->img_chunk_offset=%u", chain
->img_chunk_offset
);
239 debug("chain->img_chunk_num=%u", chain
->img_chunk_num
);
240 debug("chain->override_chunk_offset=%u", chain
->override_chunk_offset
);
241 debug("chain->override_chunk_num=%u", chain
->override_chunk_num
);
243 ventoy_debug_pause();
245 ventoy_dump_img_chunk(chain
);
246 ventoy_dump_override_chunk(chain
);
247 ventoy_dump_virt_chunk(chain
);
250 static int ventoy_update_image_location(ventoy_os_param
*param
)
252 EFI_STATUS Status
= EFI_SUCCESS
;
258 ventoy_image_location
*location
= NULL
;
259 ventoy_image_disk_region
*region
= NULL
;
260 ventoy_img_chunk
*chunk
= g_chunk
;
262 length
= sizeof(ventoy_image_location
) + (g_img_chunk_num
- 1) * sizeof(ventoy_image_disk_region
);
264 Status
= gBS
->AllocatePool(EfiRuntimeServicesData
, length
+ 4096 * 2, &buffer
);
265 if (EFI_ERROR(Status
) || NULL
== buffer
)
267 debug("Failed to allocate runtime pool %r\n", Status
);
271 address
= (UINTN
)buffer
;
275 address
+= 4096 - (address
% 4096);
279 param
->vtoy_img_location_addr
= address
;
280 param
->vtoy_img_location_len
= length
;
282 /* update check sum */
283 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
285 chksum
+= *((UINT8
*)param
+ i
);
287 param
->chksum
= (chksum
== 0) ? 0 : (UINT8
)(0x100 - chksum
);
289 location
= (ventoy_image_location
*)(unsigned long)(param
->vtoy_img_location_addr
);
290 if (NULL
== location
)
295 CopyMem(&location
->guid
, ¶m
->guid
, sizeof(ventoy_guid
));
296 location
->image_sector_size
= gSector512Mode
? 512 : 2048;
297 location
->disk_sector_size
= g_chain
->disk_sector_size
;
298 location
->region_count
= g_img_chunk_num
;
300 region
= location
->regions
;
304 for (i
= 0; i
< g_img_chunk_num
; i
++)
306 region
->image_sector_count
= chunk
->disk_end_sector
- chunk
->disk_start_sector
+ 1;
307 region
->image_start_sector
= chunk
->img_start_sector
* 4;
308 region
->disk_start_sector
= chunk
->disk_start_sector
;
315 for (i
= 0; i
< g_img_chunk_num
; i
++)
317 region
->image_sector_count
= chunk
->img_end_sector
- chunk
->img_start_sector
+ 1;
318 region
->image_start_sector
= chunk
->img_start_sector
;
319 region
->disk_start_sector
= chunk
->disk_start_sector
;
328 EFI_HANDLE EFIAPI
ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL
*pDevPath
)
330 EFI_HANDLE Handle
= NULL
;
331 EFI_STATUS Status
= EFI_SUCCESS
;
332 EFI_DEVICE_PATH_PROTOCOL
*pLastNode
= NULL
;
333 EFI_DEVICE_PATH_PROTOCOL
*pCurNode
= NULL
;
334 EFI_DEVICE_PATH_PROTOCOL
*pTmpDevPath
= NULL
;
336 pTmpDevPath
= DuplicateDevicePath(pDevPath
);
342 pCurNode
= pTmpDevPath
;
343 while (!IsDevicePathEnd(pCurNode
))
345 pLastNode
= pCurNode
;
346 pCurNode
= NextDevicePathNode(pCurNode
);
350 CopyMem(pLastNode
, pCurNode
, sizeof(EFI_DEVICE_PATH_PROTOCOL
));
353 pCurNode
= pTmpDevPath
;
354 Status
= gBS
->LocateDevicePath(&gEfiDevicePathProtocolGuid
, &pCurNode
, &Handle
);
355 debug("Status:%r Parent Handle:%p DP:%s", Status
, Handle
, ConvertDevicePathToText(pTmpDevPath
, FALSE
, FALSE
));
357 FreePool(pTmpDevPath
);
362 EFI_STATUS EFIAPI
ventoy_save_ramdisk_param(VOID
)
364 EFI_STATUS Status
= EFI_SUCCESS
;
365 EFI_GUID VarGuid
= VENTOY_GUID
;
367 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
368 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
369 sizeof(g_ramdisk_param
), &(g_ramdisk_param
));
370 debug("set ramdisk variable %r", Status
);
375 EFI_STATUS EFIAPI
ventoy_delete_ramdisk_param(VOID
)
377 EFI_STATUS Status
= EFI_SUCCESS
;
378 EFI_GUID VarGuid
= VENTOY_GUID
;
380 Status
= gRT
->SetVariable(L
"VentoyRamDisk", &VarGuid
,
381 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
383 debug("delete efi variable %r", Status
);
389 EFI_STATUS EFIAPI
ventoy_save_variable(VOID
)
391 EFI_STATUS Status
= EFI_SUCCESS
;
392 EFI_GUID VarGuid
= VENTOY_GUID
;
394 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
395 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
396 sizeof(g_chain
->os_param
), &(g_chain
->os_param
));
397 debug("set efi variable %r", Status
);
402 EFI_STATUS EFIAPI
ventoy_delete_variable(VOID
)
404 EFI_STATUS Status
= EFI_SUCCESS
;
405 EFI_GUID VarGuid
= VENTOY_GUID
;
407 Status
= gRT
->SetVariable(L
"VentoyOsParam", &VarGuid
,
408 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
410 debug("delete efi variable %r", Status
);
415 #if (VENTOY_DEVICE_WARN != 0)
416 STATIC VOID
ventoy_warn_invalid_device(VOID
)
418 STATIC BOOLEAN flag
= FALSE
;
426 gST
->ConOut
->ClearScreen(gST
->ConOut
);
427 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
428 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n");
429 gST
->ConOut
->OutputString(gST
->ConOut
, VTOY_WARNING L
"\r\n\r\n\r\n");
431 gST
->ConOut
->OutputString(gST
->ConOut
, L
"This is NOT a standard Ventoy device and is NOT supported.\r\n\r\n");
432 gST
->ConOut
->OutputString(gST
->ConOut
, L
"You should follow the official instructions in https://www.ventoy.net\r\n");
434 gST
->ConOut
->OutputString(gST
->ConOut
, L
"\r\n\r\nWill exit after 10 seconds ...... ");
439 STATIC VOID
ventoy_warn_invalid_device(VOID
)
445 STATIC EFI_STATUS EFIAPI ventoy_load_image
447 IN EFI_HANDLE ImageHandle
,
448 IN EFI_DEVICE_PATH_PROTOCOL
*pDevicePath
,
449 IN CONST CHAR16
*FileName
,
450 IN UINTN FileNameLen
,
451 OUT EFI_HANDLE
*Image
454 EFI_STATUS Status
= EFI_SUCCESS
;
455 CHAR16 TmpBuf
[256] = {0};
456 FILEPATH_DEVICE_PATH
*pFilePath
= NULL
;
457 EFI_DEVICE_PATH_PROTOCOL
*pImgPath
= NULL
;
459 pFilePath
= (FILEPATH_DEVICE_PATH
*)TmpBuf
;
460 pFilePath
->Header
.Type
= MEDIA_DEVICE_PATH
;
461 pFilePath
->Header
.SubType
= MEDIA_FILEPATH_DP
;
462 pFilePath
->Header
.Length
[0] = FileNameLen
+ sizeof(EFI_DEVICE_PATH_PROTOCOL
);
463 pFilePath
->Header
.Length
[1] = 0;
464 CopyMem(pFilePath
->PathName
, FileName
, FileNameLen
);
466 pImgPath
= AppendDevicePathNode(pDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)pFilePath
);
469 return EFI_NOT_FOUND
;
472 Status
= gBS
->LoadImage(FALSE
, ImageHandle
, pImgPath
, NULL
, 0, Image
);
474 debug("Load Image File %r DP: <%s>", Status
, ConvertDevicePathToText(pImgPath
, FALSE
, FALSE
));
482 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle
)
487 MBR_HEAD
*pMBR
= NULL
;
488 UINT8
*pBuffer
= NULL
;
490 EFI_STATUS Status
= EFI_SUCCESS
;
491 EFI_BLOCK_IO_PROTOCOL
*pBlockIo
;
493 pBuffer
= AllocatePool(2048);
496 return EFI_OUT_OF_RESOURCES
;
499 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiBlockIoProtocolGuid
,
500 NULL
, &Count
, &Handles
);
501 if (EFI_ERROR(Status
))
507 for (i
= 0; i
< Count
; i
++)
509 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiBlockIoProtocolGuid
, (VOID
**)&pBlockIo
);
510 if (EFI_ERROR(Status
))
515 DiskSize
= (pBlockIo
->Media
->LastBlock
+ 1) * pBlockIo
->Media
->BlockSize
;
516 debug("This Disk size: %llu", DiskSize
);
517 if (g_chain
->os_param
.vtoy_disk_size
!= DiskSize
)
522 Status
= pBlockIo
->ReadBlocks(pBlockIo
, pBlockIo
->Media
->MediaId
, 0, 512, pBuffer
);
523 if (EFI_ERROR(Status
))
525 debug("ReadBlocks filed %r", Status
);
529 if (CompareMem(g_chain
->os_param
.vtoy_disk_guid
, pBuffer
+ 0x180, 16) == 0)
531 pMBR
= (MBR_HEAD
*)pBuffer
;
532 if (pMBR
->PartTbl
[0].FsFlag
!= 0xEE)
534 if (pMBR
->PartTbl
[0].StartSectorId
!= 2048 ||
535 pMBR
->PartTbl
[1].SectorCount
!= 65536 ||
536 pMBR
->PartTbl
[1].StartSectorId
!= pMBR
->PartTbl
[0].StartSectorId
+ pMBR
->PartTbl
[0].SectorCount
)
538 debug("Failed to check disk part table");
539 ventoy_warn_invalid_device();
543 gBlockData
.RawBlockIoHandle
= Handles
[i
];
544 gBlockData
.pRawBlockIo
= pBlockIo
;
545 gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
546 (VOID
**)&(gBlockData
.pDiskDevPath
),
549 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
551 debug("Find Ventoy Disk Handle:%p DP:%s", Handles
[i
],
552 ConvertDevicePathToText(gBlockData
.pDiskDevPath
, FALSE
, FALSE
));
561 return EFI_NOT_FOUND
;
570 STATIC EFI_STATUS EFIAPI
ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle
)
574 EFI_HANDLE Parent
= NULL
;
575 EFI_HANDLE
*Handles
= NULL
;
576 EFI_STATUS Status
= EFI_SUCCESS
;
577 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
578 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
580 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
581 NULL
, &Count
, &Handles
);
582 if (EFI_ERROR(Status
))
587 debug("ventoy_find_iso_disk_fs fs count:%u", Count
);
589 for (i
= 0; i
< Count
; i
++)
591 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
592 if (EFI_ERROR(Status
))
597 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
601 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
602 if (EFI_ERROR(Status
))
604 debug("Failed to open device path protocol %r", Status
);
608 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
609 Parent
= ventoy_get_parent_handle(pDevPath
);
611 if (Parent
== gBlockData
.RawBlockIoHandle
)
613 debug("Find ventoy disk fs");
614 gBlockData
.DiskFsHandle
= Handles
[i
];
615 gBlockData
.pDiskFs
= pFile
;
616 gBlockData
.pDiskFsDevPath
= pDevPath
;
626 STATIC EFI_STATUS EFIAPI
ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle
)
628 EFI_HANDLE Image
= NULL
;
629 EFI_STATUS Status
= EFI_SUCCESS
;
630 CHAR16 LogVar
[4] = L
"5";
634 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
636 sizeof(gUdfEfiDriverPath
),
638 debug("load iso UDF efi driver status:%r", Status
);
642 Status
= ventoy_load_image(ImageHandle
, gBlockData
.pDiskFsDevPath
,
643 gIso9660EfiDriverPath
,
644 sizeof(gIso9660EfiDriverPath
),
646 debug("load iso 9660 efi driver status:%r", Status
);
651 gRT
->SetVariable(L
"FS_LOGGING", &gShellVariableGuid
,
652 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
653 sizeof(LogVar
), LogVar
);
656 gRT
->SetVariable(L
"FS_NAME_NOCASE", &gShellVariableGuid
,
657 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
658 sizeof(LogVar
), LogVar
);
660 gBlockData
.IsoDriverImage
= Image
;
661 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
662 debug("Start iso efi driver status:%r", Status
);
667 STATIC EFI_STATUS EFIAPI
ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle
)
673 const char *pEnv
= NULL
;
675 CHAR16
*pCmdLine
= NULL
;
676 EFI_STATUS Status
= EFI_SUCCESS
;
677 ventoy_grub_param
*pGrubParam
= NULL
;
678 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
679 ventoy_chain_head
*chain
= NULL
;
681 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
682 if (EFI_ERROR(Status
))
684 VtoyDebug("Failed to handle load image protocol %r", Status
);
688 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
689 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
690 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
692 if (StrStr(pCmdLine
, L
"debug"))
697 if (StrStr(pCmdLine
, L
"dotefi"))
702 if (StrStr(pCmdLine
, L
"isoefi=on"))
707 if (StrStr(pCmdLine
, L
"iso_udf"))
712 pPos
= StrStr(pCmdLine
, L
"FirstTry=@");
715 pPos
+= StrLen(L
"FirstTry=");
716 for (i
= 0; i
< ARRAY_SIZE(gFirstTryBootFile
); i
++, pPos
++)
718 if (*pPos
!= L
' ' && *pPos
!= L
'\t' && *pPos
)
720 gFirstTryBootFile
[i
] = (*pPos
== '@') ? '\\' : *pPos
;
728 gEfiBootFileName
[0] = gFirstTryBootFile
;
729 gBootFileStartIndex
= 0;
732 debug("cmdline:<%s>", pCmdLine
);
734 if (gFirstTryBootFile
[0])
736 debug("First Try:<%s>", gFirstTryBootFile
);
739 pPos
= StrStr(pCmdLine
, L
"env_param=");
742 return EFI_INVALID_PARAMETER
;
745 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
746 grub_env_set
= pGrubParam
->grub_env_set
;
747 grub_env_get
= pGrubParam
->grub_env_get
;
748 pEnv
= grub_env_get("VTOY_CHKDEV_RESULT_STRING");
751 return EFI_INVALID_PARAMETER
;
754 if (pEnv
[0] != '0' || pEnv
[1] != 0)
756 ventoy_warn_invalid_device();
757 return EFI_INVALID_PARAMETER
;
760 g_file_replace_list
= &pGrubParam
->file_replace
;
761 old_cnt
= g_file_replace_list
->old_file_cnt
;
762 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
763 g_file_replace_list
->magic
,
764 g_file_replace_list
->new_file_virtual_id
,
766 old_cnt
> 0 ? g_file_replace_list
->old_file_name
[0] : "",
767 old_cnt
> 1 ? g_file_replace_list
->old_file_name
[1] : "",
768 old_cnt
> 2 ? g_file_replace_list
->old_file_name
[2] : "",
769 old_cnt
> 3 ? g_file_replace_list
->old_file_name
[3] : ""
772 pPos
= StrStr(pCmdLine
, L
"mem:");
773 chain
= (ventoy_chain_head
*)StrHexToUintn(pPos
+ 4);
775 pPos
= StrStr(pPos
, L
"size:");
776 size
= StrDecimalToUintn(pPos
+ 5);
778 debug("memory addr:%p size:%lu", chain
, size
);
780 if (StrStr(pCmdLine
, L
"sector512"))
782 gSector512Mode
= TRUE
;
785 if (StrStr(pCmdLine
, L
"memdisk"))
787 g_iso_data_buf
= (UINT8
*)chain
+ sizeof(ventoy_chain_head
);
788 g_iso_buf_size
= size
- sizeof(ventoy_chain_head
);
789 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size
);
796 debug("This is normal mode");
797 g_chain
= AllocatePool(size
);
798 CopyMem(g_chain
, chain
, size
);
800 g_chunk
= (ventoy_img_chunk
*)((char *)g_chain
+ g_chain
->img_chunk_offset
);
801 g_img_chunk_num
= g_chain
->img_chunk_num
;
802 g_override_chunk
= (ventoy_override_chunk
*)((char *)g_chain
+ g_chain
->override_chunk_offset
);
803 g_override_chunk_num
= g_chain
->override_chunk_num
;
804 g_virt_chunk
= (ventoy_virt_chunk
*)((char *)g_chain
+ g_chain
->virt_chunk_offset
);
805 g_virt_chunk_num
= g_chain
->virt_chunk_num
;
807 g_os_param_reserved
= (UINT8
*)(g_chain
->os_param
.vtoy_reserved
);
809 /* Workaround for Windows & ISO9660 */
810 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[3] == 0)
812 g_fixup_iso9660_secover_enable
= TRUE
;
815 if (g_os_param_reserved
[2] == ventoy_chain_windows
&& g_os_param_reserved
[4] != 1)
817 g_hook_keyboard
= TRUE
;
820 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable
, g_hook_keyboard
);
822 for (i
= 0; i
< sizeof(ventoy_os_param
); i
++)
824 chksum
+= *((UINT8
*)(&(g_chain
->os_param
)) + i
);
829 debug("os param checksum: 0x%x %a", g_chain
->os_param
.chksum
, chksum
? "FAILED" : "SUCCESS");
832 ventoy_update_image_location(&(g_chain
->os_param
));
836 ventoy_dump_chain(g_chain
);
840 g_fix_windows_1st_cdrom_issue
= FALSE
;
841 if (ventoy_chain_windows
== g_os_param_reserved
[2] ||
842 ventoy_chain_wim
== g_os_param_reserved
[2])
844 if (ventoy_is_cdrom_dp_exist())
846 debug("fixup the 1st cdrom influences when boot windows ...");
847 g_fix_windows_1st_cdrom_issue
= TRUE
;
851 ventoy_debug_pause();
857 EFI_STATUS EFIAPI
ventoy_clean_env(VOID
)
859 FreePool(g_sector_flag
);
860 g_sector_flag_num
= 0;
862 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
864 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
867 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
869 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
870 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
871 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
874 ventoy_delete_variable();
876 if (g_chain
->os_param
.vtoy_img_location_addr
)
878 FreePool((VOID
*)(UINTN
)g_chain
->os_param
.vtoy_img_location_addr
);
886 STATIC EFI_STATUS
ventoy_hook_start(VOID
)
888 /* don't add debug print in this function */
890 if (g_fix_windows_1st_cdrom_issue
)
892 ventoy_hook_1st_cdrom_start();
895 /* let this the last */
898 ventoy_hook_keyboard_start();
904 STATIC EFI_STATUS
ventoy_hook_stop(VOID
)
906 /* don't add debug print in this function */
908 if (g_fix_windows_1st_cdrom_issue
)
910 ventoy_hook_1st_cdrom_stop();
913 /* let this the last */
916 ventoy_hook_keyboard_stop();
922 EFI_STATUS EFIAPI
ventoy_boot(IN EFI_HANDLE ImageHandle
)
929 EFI_HANDLE Image
= NULL
;
930 EFI_HANDLE
*Handles
= NULL
;
931 EFI_STATUS Status
= EFI_SUCCESS
;
932 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*pFile
= NULL
;
933 EFI_DEVICE_PATH_PROTOCOL
*pDevPath
= NULL
;
935 for (t
= 0; t
< 3; t
++)
940 Status
= gBS
->LocateHandleBuffer(ByProtocol
, &gEfiSimpleFileSystemProtocolGuid
,
941 NULL
, &Count
, &Handles
);
942 if (EFI_ERROR(Status
))
947 debug("ventoy_boot fs count:%u", Count
);
949 for (i
= 0; i
< Count
; i
++)
951 Status
= gBS
->HandleProtocol(Handles
[i
], &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&pFile
);
952 if (EFI_ERROR(Status
))
957 debug("FS:%u Protocol:%p OpenVolume:%p", i
, pFile
, pFile
->OpenVolume
);
959 Status
= gBS
->OpenProtocol(Handles
[i
], &gEfiDevicePathProtocolGuid
,
963 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
964 if (EFI_ERROR(Status
))
966 debug("Failed to open device path protocol %r", Status
);
970 debug("Handle:%p FS DP: <%s>", Handles
[i
], ConvertDevicePathToText(pDevPath
, FALSE
, FALSE
));
971 if (CompareMem(gBlockData
.Path
, pDevPath
, gBlockData
.DevicePathCompareLen
))
973 debug("Not ventoy disk file system");
977 for (j
= gBootFileStartIndex
; j
< ARRAY_SIZE(gEfiBootFileName
); j
++)
979 Status
= ventoy_load_image(ImageHandle
, pDevPath
, gEfiBootFileName
[j
],
980 StrSize(gEfiBootFileName
[j
]), &Image
);
981 if (EFI_SUCCESS
== Status
)
985 debug("Failed to load image %r <%s>", Status
, gEfiBootFileName
[j
]);
988 if (j
>= ARRAY_SIZE(gEfiBootFileName
))
994 debug("Find boot file, now try to boot .....");
995 ventoy_debug_pause();
999 gST
->ConIn
->Reset(gST
->ConIn
, FALSE
);
1002 if (g_file_replace_list
&& g_file_replace_list
->magic
== GRUB_FILE_REPLACE_MAGIC
)
1004 ventoy_wrapper_push_openvolume(pFile
->OpenVolume
);
1005 pFile
->OpenVolume
= ventoy_wrapper_open_volume
;
1008 ventoy_hook_start();
1009 /* can't add debug print here */
1010 //ventoy_wrapper_system();
1011 Status
= gBS
->StartImage(Image
, NULL
, NULL
);
1014 if (EFI_ERROR(Status
))
1016 debug("Failed to start image %r", Status
);
1018 gBS
->UnloadImage(Image
);
1032 debug("Fs not found, now wait and retry...");
1039 return EFI_NOT_FOUND
;
1045 EFI_STATUS EFIAPI VentoyEfiMain
1047 IN EFI_HANDLE ImageHandle
,
1048 IN EFI_SYSTEM_TABLE
*SystemTable
1051 EFI_STATUS Status
= EFI_SUCCESS
;
1052 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*Protocol
;
1054 g_sector_flag_num
= 512; /* initial value */
1056 g_sector_flag
= AllocatePool(g_sector_flag_num
* sizeof(ventoy_sector_flag
));
1057 if (NULL
== g_sector_flag
)
1059 return EFI_OUT_OF_RESOURCES
;
1062 Status
= gBS
->HandleProtocol(gST
->ConsoleInHandle
, &gEfiSimpleTextInputExProtocolGuid
, (VOID
**)&Protocol
);
1063 if (EFI_SUCCESS
== Status
)
1065 g_con_simple_input_ex
= Protocol
;
1068 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1069 ventoy_clear_input();
1071 Status
= ventoy_parse_cmdline(ImageHandle
);
1072 if (EFI_ERROR(Status
))
1079 g_ramdisk_param
.PhyAddr
= (UINT64
)(UINTN
)g_iso_data_buf
;
1080 g_ramdisk_param
.DiskSize
= (UINT64
)g_iso_buf_size
;
1082 ventoy_save_ramdisk_param();
1086 ventoy_find_iso_disk(ImageHandle
);
1087 ventoy_find_iso_disk_fs(ImageHandle
);
1088 ventoy_load_isoefi_driver(ImageHandle
);
1091 ventoy_install_blockio(ImageHandle
, g_iso_buf_size
);
1092 ventoy_debug_pause();
1094 Status
= ventoy_boot(ImageHandle
);
1096 ventoy_delete_ramdisk_param();
1098 if (gLoadIsoEfi
&& gBlockData
.IsoDriverImage
)
1100 gBS
->UnloadImage(gBlockData
.IsoDriverImage
);
1103 gBS
->DisconnectController(gBlockData
.Handle
, NULL
, NULL
);
1104 gBS
->UninstallMultipleProtocolInterfaces(gBlockData
.Handle
,
1105 &gEfiBlockIoProtocolGuid
, &gBlockData
.BlockIo
,
1106 &gEfiDevicePathProtocolGuid
, gBlockData
.Path
,
1111 ventoy_save_variable();
1112 Status
= ventoy_find_iso_disk(ImageHandle
);
1113 if (!EFI_ERROR(Status
))
1117 ventoy_find_iso_disk_fs(ImageHandle
);
1118 ventoy_load_isoefi_driver(ImageHandle
);
1121 ventoy_debug_pause();
1123 ventoy_install_blockio(ImageHandle
, g_chain
->virt_img_size_in_bytes
);
1125 ventoy_debug_pause();
1127 Status
= ventoy_boot(ImageHandle
);
1133 if (FALSE
== gDotEfiBoot
)
1135 if (EFI_NOT_FOUND
== Status
)
1137 gST
->ConOut
->OutputString(gST
->ConOut
, L
"No bootfile found for UEFI!\r\n");
1138 gST
->ConOut
->OutputString(gST
->ConOut
, L
"Maybe the image does not support " VENTOY_UEFI_DESC L
"!\r\n");
1143 ventoy_clear_input();
1144 gST
->ConOut
->ClearScreen(gST
->ConOut
);
1146 if (gDotEfiBoot
&& (EFI_NOT_FOUND
== Status
))
1148 grub_env_set("vtoy_dotefi_retry", "YES");