]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.c
1.1.07 release
[Ventoy.git] / EDK2 / edk2_mod / edk2-edk2-stable201911 / MdeModulePkg / Application / Ventoy / Ventoy.c
1 /******************************************************************************
2 * Ventoy.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include <Uefi.h>
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>
38 #include <Ventoy.h>
39
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;
58
59 ventoy_grub_param_file_replace *g_file_replace_list = NULL;
60 ventoy_efi_file_replace g_efi_file_replace;
61
62 ventoy_grub_param_file_replace *g_img_replace_list = NULL;
63 ventoy_efi_file_replace g_img_file_replace[VTOY_MAX_CONF_REPLACE];
64
65 CONST CHAR16 gIso9660EfiDriverPath[] = ISO9660_EFI_DRIVER_PATH;
66 CONST CHAR16 gUdfEfiDriverPath[] = UDF_EFI_DRIVER_PATH;
67
68 BOOLEAN g_fix_windows_1st_cdrom_issue = FALSE;
69
70 STATIC BOOLEAN g_hook_keyboard = FALSE;
71
72 CHAR16 gFirstTryBootFile[256] = {0};
73
74 STATIC EFI_GET_VARIABLE g_org_get_variable = NULL;
75 STATIC EFI_EXIT_BOOT_SERVICES g_org_exit_boot_service = NULL;
76
77 /* Boot filename */
78 UINTN gBootFileStartIndex = 1;
79 CONST CHAR16 *gEfiBootFileName[] =
80 {
81 L"@",
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",
103 #endif
104
105 };
106
107 VOID EFIAPI VtoyDebug(IN CONST CHAR8 *Format, ...)
108 {
109 VA_LIST Marker;
110 CHAR16 Buffer[512];
111
112 VA_START (Marker, Format);
113 UnicodeVSPrintAsciiFormat(Buffer, sizeof(Buffer), Format, Marker);
114 VA_END (Marker);
115
116 gST->ConOut->OutputString(gST->ConOut, Buffer);
117 }
118
119 VOID EFIAPI ventoy_clear_input(VOID)
120 {
121 EFI_INPUT_KEY Key;
122
123 gST->ConIn->Reset(gST->ConIn, FALSE);
124 while (EFI_SUCCESS == gST->ConIn->ReadKeyStroke(gST->ConIn, &Key))
125 {
126 ;
127 }
128 gST->ConIn->Reset(gST->ConIn, FALSE);
129 }
130
131 static void EFIAPI ventoy_dump_img_chunk(ventoy_chain_head *chain)
132 {
133 UINT32 i;
134 int errcnt = 0;
135 UINT64 img_sec = 0;
136 ventoy_img_chunk *chunk;
137
138 chunk = (ventoy_img_chunk *)((char *)chain + chain->img_chunk_offset);
139
140 debug("##################### ventoy_dump_img_chunk #######################");
141
142 for (i = 0; i < chain->img_chunk_num; i++)
143 {
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);
147
148 if (i > 0 && (chunk[i].img_start_sector != chunk[i - 1].img_end_sector + 1))
149 {
150 errcnt++;
151 }
152
153 img_sec += chunk[i].img_end_sector - chunk[i].img_start_sector + 1;
154 }
155
156 if (errcnt == 0 && (img_sec * 2048 == g_chain->real_img_size_in_bytes))
157 {
158 debug("image chunk size check success");
159 }
160 else
161 {
162 debug("image chunk size check failed %d", errcnt);
163 }
164
165 ventoy_debug_pause();
166 }
167
168 static void EFIAPI ventoy_dump_override_chunk(ventoy_chain_head *chain)
169 {
170 UINT32 i;
171 ventoy_override_chunk *chunk;
172
173 chunk = (ventoy_override_chunk *)((char *)chain + chain->override_chunk_offset);
174
175 debug("##################### ventoy_dump_override_chunk #######################");
176
177 for (i = 0; i < g_override_chunk_num; i++)
178 {
179 debug("%2u: [ %llu, %u ]", i, chunk[i].img_offset, chunk[i].override_size);
180 }
181
182 ventoy_debug_pause();
183 }
184
185 static void EFIAPI ventoy_dump_virt_chunk(ventoy_chain_head *chain)
186 {
187 UINT32 i;
188 ventoy_virt_chunk *node;
189
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);
193
194 node = (ventoy_virt_chunk *)((char *)chain + chain->virt_chunk_offset);
195 for (i = 0; i < chain->virt_chunk_num; i++, node++)
196 {
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);
204 }
205
206 ventoy_debug_pause();
207 }
208
209 static void EFIAPI ventoy_dump_chain(ventoy_chain_head *chain)
210 {
211 UINT32 i = 0;
212 UINT8 chksum = 0;
213 UINT8 *guid;
214
215 guid = chain->os_param.vtoy_disk_guid;
216 for (i = 0; i < sizeof(ventoy_os_param); i++)
217 {
218 chksum += *((UINT8 *)(&(chain->os_param)) + i);
219 }
220
221 debug("##################### ventoy_dump_chain #######################");
222
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]
240 );
241
242 ventoy_debug_pause();
243
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);
253
254 ventoy_debug_pause();
255
256 ventoy_dump_img_chunk(chain);
257 ventoy_dump_override_chunk(chain);
258 ventoy_dump_virt_chunk(chain);
259 }
260
261 static int ventoy_update_image_location(ventoy_os_param *param)
262 {
263 EFI_STATUS Status = EFI_SUCCESS;
264 UINT8 chksum = 0;
265 unsigned int i;
266 unsigned int length;
267 UINTN address = 0;
268 void *buffer = NULL;
269 ventoy_image_location *location = NULL;
270 ventoy_image_disk_region *region = NULL;
271 ventoy_img_chunk *chunk = g_chunk;
272
273 length = sizeof(ventoy_image_location) + (g_img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
274
275 Status = gBS->AllocatePool(EfiRuntimeServicesData, length + 4096 * 2, &buffer);
276 if (EFI_ERROR(Status) || NULL == buffer)
277 {
278 debug("Failed to allocate runtime pool %r\n", Status);
279 return 1;
280 }
281
282 address = (UINTN)buffer;
283 g_vtoy_img_location_buf = buffer;
284
285 if (address % 4096)
286 {
287 address += 4096 - (address % 4096);
288 }
289
290 param->chksum = 0;
291 param->vtoy_img_location_addr = address;
292 param->vtoy_img_location_len = length;
293
294 /* update check sum */
295 for (i = 0; i < sizeof(ventoy_os_param); i++)
296 {
297 chksum += *((UINT8 *)param + i);
298 }
299 param->chksum = (chksum == 0) ? 0 : (UINT8)(0x100 - chksum);
300
301 location = (ventoy_image_location *)(unsigned long)(param->vtoy_img_location_addr);
302 if (NULL == location)
303 {
304 return 0;
305 }
306
307 CopyMem(&location->guid, &param->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;
311
312 region = location->regions;
313
314 if (gSector512Mode)
315 {
316 for (i = 0; i < g_img_chunk_num; i++)
317 {
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;
321 region++;
322 chunk++;
323 }
324 }
325 else
326 {
327 for (i = 0; i < g_img_chunk_num; i++)
328 {
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;
332 region++;
333 chunk++;
334 }
335 }
336
337 return 0;
338 }
339
340 EFI_HANDLE EFIAPI ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL *pDevPath)
341 {
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;
347
348 pTmpDevPath = DuplicateDevicePath(pDevPath);
349 if (!pTmpDevPath)
350 {
351 return NULL;
352 }
353
354 pCurNode = pTmpDevPath;
355 while (!IsDevicePathEnd(pCurNode))
356 {
357 pLastNode = pCurNode;
358 pCurNode = NextDevicePathNode(pCurNode);
359 }
360 if (pLastNode)
361 {
362 CopyMem(pLastNode, pCurNode, sizeof(EFI_DEVICE_PATH_PROTOCOL));
363 }
364
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));
368
369 FreePool(pTmpDevPath);
370
371 return Handle;
372 }
373
374 STATIC ventoy_ram_disk g_backup_ramdisk_param;
375 STATIC ventoy_os_param g_backup_os_param_var;
376
377
378 EFI_STATUS EFIAPI ventoy_save_ramdisk_param(VOID)
379 {
380 UINTN DataSize;
381 EFI_STATUS Status = EFI_SUCCESS;
382 EFI_GUID VarGuid = VENTOY_GUID;
383
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))
387 {
388 debug("find previous ramdisk variable <%llu>", g_backup_ramdisk_param.DiskSize);
389 }
390
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);
395
396 return Status;
397 }
398
399 EFI_STATUS EFIAPI ventoy_delete_ramdisk_param(VOID)
400 {
401 EFI_STATUS Status = EFI_SUCCESS;
402 EFI_GUID VarGuid = VENTOY_GUID;
403
404 if (g_backup_ramdisk_param.DiskSize > 0 && g_backup_ramdisk_param.PhyAddr > 0)
405 {
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);
410 }
411 else
412 {
413 Status = gRT->SetVariable(L"VentoyRamDisk", &VarGuid,
414 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
415 0, NULL);
416 debug("delete ramdisk variable %r", Status);
417 }
418
419 return Status;
420 }
421
422 EFI_STATUS EFIAPI ventoy_save_variable(VOID)
423 {
424 UINTN DataSize;
425 EFI_STATUS Status = EFI_SUCCESS;
426 EFI_GUID VarGuid = VENTOY_GUID;
427
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))
431 {
432 debug("find previous efi variable <%a>", g_backup_os_param_var.vtoy_img_path);
433 }
434
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);
439
440 return Status;
441 }
442
443 EFI_STATUS EFIAPI ventoy_delete_variable(VOID)
444 {
445 EFI_STATUS Status = EFI_SUCCESS;
446 EFI_GUID VarGuid = VENTOY_GUID;
447
448 if (0 == CompareMem(&(g_backup_os_param_var.guid), &VarGuid, sizeof(EFI_GUID)))
449 {
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);
454 }
455 else
456 {
457 Status = gRT->SetVariable(L"VentoyOsParam", &VarGuid,
458 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
459 0, NULL);
460 debug("delete efi variable %r", Status);
461 }
462
463 return Status;
464 }
465
466 #if (VENTOY_DEVICE_WARN != 0)
467 STATIC VOID ventoy_warn_invalid_device(VOID)
468 {
469 STATIC BOOLEAN flag = FALSE;
470
471 if (flag)
472 {
473 return;
474 }
475
476 flag = TRUE;
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");
481
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");
484
485 gST->ConOut->OutputString(gST->ConOut, L"\r\n\r\nWill exit after 10 seconds ...... ");
486
487 sleep(10);
488 }
489 #else
490 STATIC VOID ventoy_warn_invalid_device(VOID)
491 {
492
493 }
494 #endif
495
496 STATIC EFI_STATUS EFIAPI ventoy_load_image
497 (
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
503 )
504 {
505 EFI_STATUS Status = EFI_SUCCESS;
506 CHAR16 TmpBuf[256] = {0};
507 FILEPATH_DEVICE_PATH *pFilePath = NULL;
508 EFI_DEVICE_PATH_PROTOCOL *pImgPath = NULL;
509
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);
516
517 pImgPath = AppendDevicePathNode(pDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)pFilePath);
518 if (!pImgPath)
519 {
520 return EFI_NOT_FOUND;
521 }
522
523 Status = gBS->LoadImage(FALSE, ImageHandle, pImgPath, NULL, 0, Image);
524
525 debug("Load Image File %r DP: <%s>", Status, ConvertDevicePathToText(pImgPath, FALSE, FALSE));
526
527 FreePool(pImgPath);
528
529 return Status;
530 }
531
532
533 STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle)
534 {
535 UINTN i = 0;
536 UINTN Count = 0;
537 UINT64 DiskSize = 0;
538 MBR_HEAD *pMBR = NULL;
539 UINT8 *pBuffer = NULL;
540 EFI_HANDLE *Handles;
541 EFI_STATUS Status = EFI_SUCCESS;
542 EFI_BLOCK_IO_PROTOCOL *pBlockIo;
543
544 pBuffer = AllocatePool(2048);
545 if (!pBuffer)
546 {
547 return EFI_OUT_OF_RESOURCES;
548 }
549
550 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiBlockIoProtocolGuid,
551 NULL, &Count, &Handles);
552 if (EFI_ERROR(Status))
553 {
554 FreePool(pBuffer);
555 return Status;
556 }
557
558 for (i = 0; i < Count; i++)
559 {
560 Status = gBS->HandleProtocol(Handles[i], &gEfiBlockIoProtocolGuid, (VOID **)&pBlockIo);
561 if (EFI_ERROR(Status))
562 {
563 continue;
564 }
565
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)
569 {
570 continue;
571 }
572
573 Status = pBlockIo->ReadBlocks(pBlockIo, pBlockIo->Media->MediaId, 0, 512, pBuffer);
574 if (EFI_ERROR(Status))
575 {
576 debug("ReadBlocks filed %r", Status);
577 continue;
578 }
579
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)
582 {
583 pMBR = (MBR_HEAD *)pBuffer;
584 if (g_os_param_reserved[6] == 0 && pMBR->PartTbl[0].FsFlag != 0xEE)
585 {
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)
589 {
590 debug("Failed to check disk part table");
591 ventoy_warn_invalid_device();
592 }
593 }
594
595 gBlockData.RawBlockIoHandle = Handles[i];
596 gBlockData.pRawBlockIo = pBlockIo;
597 gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
598 (VOID **)&(gBlockData.pDiskDevPath),
599 ImageHandle,
600 Handles[i],
601 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
602
603 debug("Find Ventoy Disk Sig Handle:%p DP:%s", Handles[i],
604 ConvertDevicePathToText(gBlockData.pDiskDevPath, FALSE, FALSE));
605 break;
606 }
607 }
608
609 FreePool(Handles);
610
611 if (i >= Count)
612 {
613 return EFI_NOT_FOUND;
614 }
615 else
616 {
617 return EFI_SUCCESS;
618 }
619 }
620
621
622 STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle)
623 {
624 UINTN i = 0;
625 UINTN Count = 0;
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;
631
632 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
633 NULL, &Count, &Handles);
634 if (EFI_ERROR(Status))
635 {
636 return Status;
637 }
638
639 debug("ventoy_find_iso_disk_fs fs count:%u", Count);
640
641 for (i = 0; i < Count; i++)
642 {
643 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
644 if (EFI_ERROR(Status))
645 {
646 continue;
647 }
648
649 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
650 (VOID **)&pDevPath,
651 ImageHandle,
652 Handles[i],
653 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
654 if (EFI_ERROR(Status))
655 {
656 debug("Failed to open device path protocol %r", Status);
657 continue;
658 }
659
660 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
661 Parent = ventoy_get_parent_handle(pDevPath);
662
663 if (Parent == gBlockData.RawBlockIoHandle)
664 {
665 debug("Find ventoy disk fs");
666 gBlockData.DiskFsHandle = Handles[i];
667 gBlockData.pDiskFs = pFile;
668 gBlockData.pDiskFsDevPath = pDevPath;
669 break;
670 }
671 }
672
673 FreePool(Handles);
674
675 return EFI_SUCCESS;
676 }
677
678 STATIC EFI_STATUS EFIAPI ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle)
679 {
680 EFI_HANDLE Image = NULL;
681 EFI_STATUS Status = EFI_SUCCESS;
682 CHAR16 LogVar[4] = L"5";
683
684 if (gIsoUdf)
685 {
686 Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
687 gUdfEfiDriverPath,
688 sizeof(gUdfEfiDriverPath),
689 &Image);
690 debug("load iso UDF efi driver status:%r", Status);
691 }
692 else
693 {
694 Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
695 gIso9660EfiDriverPath,
696 sizeof(gIso9660EfiDriverPath),
697 &Image);
698 debug("load iso 9660 efi driver status:%r", Status);
699 }
700
701 if (gDebugPrint)
702 {
703 gRT->SetVariable(L"FS_LOGGING", &gShellVariableGuid,
704 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
705 sizeof(LogVar), LogVar);
706 }
707
708 gRT->SetVariable(L"FS_NAME_NOCASE", &gShellVariableGuid,
709 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
710 sizeof(LogVar), LogVar);
711
712 gBlockData.IsoDriverImage = Image;
713 Status = gBS->StartImage(Image, NULL, NULL);
714 debug("Start iso efi driver status:%r", Status);
715
716 return EFI_SUCCESS;
717 }
718
719 STATIC EFI_STATUS ventoy_proc_img_replace_name(ventoy_grub_param_file_replace *replace)
720 {
721 UINT32 i;
722 char tmp[256];
723
724 if (replace->magic != GRUB_IMG_REPLACE_MAGIC)
725 {
726 return EFI_SUCCESS;
727 }
728
729 if (replace->old_file_name[0][0] == 0)
730 {
731 return EFI_SUCCESS;
732 }
733
734 AsciiStrCpyS(tmp, sizeof(tmp), replace->old_file_name[0]);
735
736 for (i = 0; i < 256 && tmp[i]; i++)
737 {
738 if (tmp[i] == '/')
739 {
740 tmp[i] = '\\';
741 }
742 }
743
744 AsciiStrCpyS(replace->old_file_name[0], 256, tmp);
745 return EFI_SUCCESS;
746 }
747
748 EFI_STATUS EFIAPI ventoy_get_variable_wrapper
749 (
750 IN CHAR16 *VariableName,
751 IN EFI_GUID *VendorGuid,
752 OUT UINT32 *Attributes, OPTIONAL
753 IN OUT UINTN *DataSize,
754 OUT VOID *Data OPTIONAL
755 )
756 {
757 EFI_STATUS Status = EFI_SUCCESS;
758
759 Status = g_org_get_variable(VariableName, VendorGuid, Attributes, DataSize, Data);
760 if (StrCmp(VariableName, L"SecureBoot") == 0)
761 {
762 if ((*DataSize == 1) && Data)
763 {
764 *(UINT8 *)Data = 0;
765 }
766 }
767
768 return Status;
769 }
770
771 EFI_STATUS EFIAPI ventoy_exit_boot_service_wrapper
772 (
773 IN EFI_HANDLE ImageHandle,
774 IN UINTN MapKey
775 )
776 {
777 if (g_org_get_variable)
778 {
779 gRT->GetVariable = g_org_get_variable;
780 g_org_get_variable = NULL;
781 }
782
783 return g_org_exit_boot_service(ImageHandle, MapKey);
784 }
785
786 STATIC EFI_STATUS EFIAPI ventoy_disable_secure_boot(IN EFI_HANDLE ImageHandle)
787 {
788 UINT8 Value = 0;
789 UINTN DataSize = 1;
790 EFI_STATUS Status = EFI_SUCCESS;
791
792 Status = gRT->GetVariable(L"SecureBoot", &gEfiGlobalVariableGuid, NULL, &DataSize, &Value);
793 if (!EFI_ERROR(Status))
794 {
795 if (DataSize == 1 && Value == 0)
796 {
797 debug("Current secure boot is off, no need to disable");
798 return EFI_SUCCESS;
799 }
800 }
801
802 debug("ventoy_disable_secure_boot");
803
804 /* step1: wrapper security protocol. */
805 /* Do we still need it since we have been loaded ? */
806
807
808 /* step2: fake SecureBoot variable */
809 g_org_exit_boot_service = gBS->ExitBootServices;
810 gBS->ExitBootServices = ventoy_exit_boot_service_wrapper;
811
812 g_org_get_variable = gRT->GetVariable;
813 gRT->GetVariable = ventoy_get_variable_wrapper;
814
815 return EFI_SUCCESS;
816 }
817
818
819 STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
820 {
821 UINT32 i = 0;
822 UINT32 old_cnt = 0;
823 UINTN size = 0;
824 UINT8 chksum = 0;
825 const char *pEnv = NULL;
826 CHAR16 *pPos = 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;
833
834 Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
835 if (EFI_ERROR(Status))
836 {
837 VtoyDebug("Failed to handle load image protocol %r", Status);
838 return Status;
839 }
840
841 pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
842 SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
843 CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
844
845 if (StrStr(pCmdLine, L"debug"))
846 {
847 gDebugPrint = TRUE;
848 }
849
850 if (StrStr(pCmdLine, L"fallback"))
851 {
852 gBootFallBack = TRUE;
853 }
854
855 if (StrStr(pCmdLine, L"dotefi"))
856 {
857 gDotEfiBoot = TRUE;
858 }
859
860 if (StrStr(pCmdLine, L"isoefi=on"))
861 {
862 gLoadIsoEfi = TRUE;
863 }
864
865 if (StrStr(pCmdLine, L"iso_udf"))
866 {
867 gIsoUdf = TRUE;
868 }
869
870 pPos = StrStr(pCmdLine, L"FirstTry=@");
871 if (pPos)
872 {
873 pPos += StrLen(L"FirstTry=");
874 for (i = 0; i < ARRAY_SIZE(gFirstTryBootFile); i++, pPos++)
875 {
876 if (*pPos != L' ' && *pPos != L'\t' && *pPos)
877 {
878 gFirstTryBootFile[i] = (*pPos == '@') ? '\\' : *pPos;
879 }
880 else
881 {
882 break;
883 }
884 }
885
886 gEfiBootFileName[0] = gFirstTryBootFile;
887 gBootFileStartIndex = 0;
888 }
889
890 debug("cmdline:<%s>", pCmdLine);
891
892 if (gFirstTryBootFile[0])
893 {
894 debug("First Try:<%s>", gFirstTryBootFile);
895 }
896
897 pPos = StrStr(pCmdLine, L"env_param=");
898 if (!pPos)
899 {
900 return EFI_INVALID_PARAMETER;
901 }
902
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");
907 if (!pEnv)
908 {
909 return EFI_INVALID_PARAMETER;
910 }
911
912 if (pEnv[0] != '0' || pEnv[1] != 0)
913 {
914 ventoy_warn_invalid_device();
915 return EFI_INVALID_PARAMETER;
916 }
917
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,
923 old_cnt,
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] : ""
928 );
929
930
931 for (i = 0; i < VTOY_MAX_CONF_REPLACE; i++)
932 {
933 replace = pGrubParam->img_replace + i;
934 if (replace->magic == GRUB_IMG_REPLACE_MAGIC)
935 {
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>",
939 i, replace->magic,
940 replace->new_file_virtual_id,
941 old_cnt,
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] : ""
946 );
947 g_img_replace_list = pGrubParam->img_replace;
948 }
949 }
950
951
952 pPos = StrStr(pCmdLine, L"mem:");
953 chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
954
955 pPos = StrStr(pPos, L"size:");
956 size = StrDecimalToUintn(pPos + 5);
957
958 debug("memory addr:%p size:%lu", chain, size);
959
960 if (StrStr(pCmdLine, L"sector512"))
961 {
962 gSector512Mode = TRUE;
963 }
964
965 if (StrStr(pCmdLine, L"memdisk"))
966 {
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);
970
971 g_chain = chain;
972 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
973 gMemdiskMode = TRUE;
974 }
975 else
976 {
977 debug("This is normal mode");
978 g_chain = AllocatePool(size);
979 CopyMem(g_chain, chain, size);
980
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;
987
988 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
989
990 /* Workaround for Windows & ISO9660 */
991 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[3] == 0)
992 {
993 g_fixup_iso9660_secover_enable = TRUE;
994 }
995
996 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[4] != 1)
997 {
998 g_hook_keyboard = TRUE;
999 }
1000
1001 if (g_os_param_reserved[5] == 1 && g_os_param_reserved[2] == ventoy_chain_linux)
1002 {
1003 ventoy_disable_secure_boot(ImageHandle);
1004 }
1005
1006 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable, g_hook_keyboard);
1007
1008 for (i = 0; i < sizeof(ventoy_os_param); i++)
1009 {
1010 chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
1011 }
1012
1013 if (gDebugPrint)
1014 {
1015 debug("os param checksum: 0x%x %a", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
1016 }
1017
1018 ventoy_update_image_location(&(g_chain->os_param));
1019
1020 if (gDebugPrint)
1021 {
1022 ventoy_dump_chain(g_chain);
1023 }
1024 }
1025
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])
1029 {
1030 if (ventoy_is_cdrom_dp_exist())
1031 {
1032 debug("fixup the 1st cdrom influences when boot windows ...");
1033 g_fix_windows_1st_cdrom_issue = TRUE;
1034 }
1035 }
1036
1037 ventoy_debug_pause();
1038
1039 FreePool(pCmdLine);
1040 return EFI_SUCCESS;
1041 }
1042
1043 EFI_STATUS EFIAPI ventoy_clean_env(VOID)
1044 {
1045 FreePool(g_sector_flag);
1046 g_sector_flag_num = 0;
1047
1048 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1049 {
1050 gBS->UnloadImage(gBlockData.IsoDriverImage);
1051 }
1052
1053 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1054
1055 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1056 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1057 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1058 NULL);
1059
1060 ventoy_delete_variable();
1061
1062 if (g_vtoy_img_location_buf)
1063 {
1064 FreePool(g_vtoy_img_location_buf);
1065 }
1066
1067 if (!gMemdiskMode)
1068 {
1069 FreePool(g_chain);
1070 }
1071
1072 return EFI_SUCCESS;
1073 }
1074
1075 STATIC EFI_STATUS ventoy_hook_start(VOID)
1076 {
1077 /* don't add debug print in this function */
1078
1079 if (g_fix_windows_1st_cdrom_issue)
1080 {
1081 ventoy_hook_1st_cdrom_start();
1082 }
1083
1084 /* let this the last */
1085 if (g_hook_keyboard)
1086 {
1087 ventoy_hook_keyboard_start();
1088 }
1089
1090 return EFI_SUCCESS;
1091 }
1092
1093 STATIC EFI_STATUS ventoy_hook_stop(VOID)
1094 {
1095 /* don't add debug print in this function */
1096
1097 if (g_fix_windows_1st_cdrom_issue)
1098 {
1099 ventoy_hook_1st_cdrom_stop();
1100 }
1101
1102 /* let this the last */
1103 if (g_hook_keyboard)
1104 {
1105 ventoy_hook_keyboard_stop();
1106 }
1107
1108 return EFI_SUCCESS;
1109 }
1110
1111 EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
1112 {
1113 UINTN t = 0;
1114 UINTN i = 0;
1115 UINTN j = 0;
1116 UINTN Find = 0;
1117 UINTN Count = 0;
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;
1123
1124 for (t = 0; t < 3; t++)
1125 {
1126 Count = 0;
1127 Handles = NULL;
1128
1129 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
1130 NULL, &Count, &Handles);
1131 if (EFI_ERROR(Status))
1132 {
1133 return Status;
1134 }
1135
1136 debug("ventoy_boot fs count:%u", Count);
1137
1138 for (i = 0; i < Count; i++)
1139 {
1140 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
1141 if (EFI_ERROR(Status))
1142 {
1143 continue;
1144 }
1145
1146 debug("FS:%u Protocol:%p OpenVolume:%p", i, pFile, pFile->OpenVolume);
1147
1148 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
1149 (VOID **)&pDevPath,
1150 ImageHandle,
1151 Handles[i],
1152 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1153 if (EFI_ERROR(Status))
1154 {
1155 debug("Failed to open device path protocol %r", Status);
1156 continue;
1157 }
1158
1159 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
1160 if (CompareMem(gBlockData.Path, pDevPath, gBlockData.DevicePathCompareLen))
1161 {
1162 debug("Not ventoy disk file system");
1163 continue;
1164 }
1165
1166 for (j = gBootFileStartIndex; j < ARRAY_SIZE(gEfiBootFileName); j++)
1167 {
1168 Status = ventoy_load_image(ImageHandle, pDevPath, gEfiBootFileName[j],
1169 StrSize(gEfiBootFileName[j]), &Image);
1170 if (EFI_SUCCESS == Status)
1171 {
1172 break;
1173 }
1174 debug("Failed to load image %r <%s>", Status, gEfiBootFileName[j]);
1175 }
1176
1177 if (j >= ARRAY_SIZE(gEfiBootFileName))
1178 {
1179 continue;
1180 }
1181
1182 Find++;
1183 debug("Find boot file, now try to boot .....");
1184 ventoy_debug_pause();
1185
1186 if (gDebugPrint)
1187 {
1188 gST->ConIn->Reset(gST->ConIn, FALSE);
1189 }
1190
1191 if ((g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC) || g_img_replace_list)
1192 {
1193 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
1194 pFile->OpenVolume = ventoy_wrapper_open_volume;
1195 }
1196
1197 ventoy_hook_start();
1198 /* can't add debug print here */
1199 //ventoy_wrapper_system();
1200 Status = gBS->StartImage(Image, NULL, NULL);
1201 ventoy_hook_stop();
1202
1203 if (EFI_ERROR(Status))
1204 {
1205 debug("Failed to start image %r", Status);
1206 sleep(3);
1207 gBS->UnloadImage(Image);
1208 break;
1209 }
1210 }
1211
1212 FreePool(Handles);
1213
1214 if (Find == 0)
1215 {
1216 if (gDotEfiBoot)
1217 {
1218 break;
1219 }
1220
1221 debug("Fs not found, now wait and retry...");
1222 sleep(1);
1223 }
1224 }
1225
1226 if (Find == 0)
1227 {
1228 return EFI_NOT_FOUND;
1229 }
1230
1231 return EFI_SUCCESS;
1232 }
1233
1234 EFI_STATUS EFIAPI VentoyEfiMain
1235 (
1236 IN EFI_HANDLE ImageHandle,
1237 IN EFI_SYSTEM_TABLE *SystemTable
1238 )
1239 {
1240 EFI_STATUS Status = EFI_SUCCESS;
1241 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *Protocol;
1242
1243 g_sector_flag_num = 512; /* initial value */
1244
1245 g_sector_flag = AllocatePool(g_sector_flag_num * sizeof(ventoy_sector_flag));
1246 if (NULL == g_sector_flag)
1247 {
1248 return EFI_OUT_OF_RESOURCES;
1249 }
1250
1251 Status = gBS->HandleProtocol(gST->ConsoleInHandle, &gEfiSimpleTextInputExProtocolGuid, (VOID **)&Protocol);
1252 if (EFI_SUCCESS == Status)
1253 {
1254 g_con_simple_input_ex = Protocol;
1255 }
1256
1257 gST->ConOut->ClearScreen(gST->ConOut);
1258 ventoy_clear_input();
1259
1260 Status = ventoy_parse_cmdline(ImageHandle);
1261 if (EFI_ERROR(Status))
1262 {
1263 return Status;
1264 }
1265
1266 ventoy_disable_ex_filesystem();
1267
1268 if (gMemdiskMode)
1269 {
1270 g_ramdisk_param.PhyAddr = (UINT64)(UINTN)g_iso_data_buf;
1271 g_ramdisk_param.DiskSize = (UINT64)g_iso_buf_size;
1272
1273 ventoy_save_ramdisk_param();
1274
1275 if (gLoadIsoEfi)
1276 {
1277 ventoy_find_iso_disk(ImageHandle);
1278 ventoy_find_iso_disk_fs(ImageHandle);
1279 ventoy_load_isoefi_driver(ImageHandle);
1280 }
1281
1282 ventoy_install_blockio(ImageHandle, g_iso_buf_size);
1283 ventoy_debug_pause();
1284
1285 Status = ventoy_boot(ImageHandle);
1286
1287 ventoy_delete_ramdisk_param();
1288
1289 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1290 {
1291 gBS->UnloadImage(gBlockData.IsoDriverImage);
1292 }
1293
1294 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1295 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1296 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1297 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1298 NULL);
1299 }
1300 else
1301 {
1302 ventoy_save_variable();
1303 Status = ventoy_find_iso_disk(ImageHandle);
1304 if (!EFI_ERROR(Status))
1305 {
1306 if (gLoadIsoEfi)
1307 {
1308 ventoy_find_iso_disk_fs(ImageHandle);
1309 ventoy_load_isoefi_driver(ImageHandle);
1310 }
1311
1312 ventoy_debug_pause();
1313
1314 ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
1315
1316 ventoy_debug_pause();
1317
1318 Status = ventoy_boot(ImageHandle);
1319 }
1320
1321 ventoy_clean_env();
1322 }
1323
1324 if (FALSE == gDotEfiBoot && FALSE == gBootFallBack)
1325 {
1326 if (EFI_NOT_FOUND == Status)
1327 {
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");
1330 sleep(30);
1331 }
1332 }
1333
1334 ventoy_clear_input();
1335 gST->ConOut->ClearScreen(gST->ConOut);
1336
1337 if (gDotEfiBoot && (EFI_NOT_FOUND == Status))
1338 {
1339 grub_env_set("vtoy_dotefi_retry", "YES");
1340 }
1341
1342 ventoy_enable_ex_filesystem();
1343
1344 return EFI_SUCCESS;
1345 }
1346