]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.c
VentoyVlnk add cmdline mode.
[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;
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 {
582 pMBR = (MBR_HEAD *)pBuffer;
583 if (g_os_param_reserved[6] == 0 && pMBR->PartTbl[0].FsFlag != 0xEE)
584 {
585 if (pMBR->PartTbl[0].StartSectorId != 2048 ||
586 pMBR->PartTbl[1].SectorCount != 65536 ||
587 pMBR->PartTbl[1].StartSectorId != pMBR->PartTbl[0].StartSectorId + pMBR->PartTbl[0].SectorCount)
588 {
589 debug("Failed to check disk part table");
590 ventoy_warn_invalid_device();
591 }
592 }
593
594 gBlockData.RawBlockIoHandle = Handles[i];
595 gBlockData.pRawBlockIo = pBlockIo;
596 gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
597 (VOID **)&(gBlockData.pDiskDevPath),
598 ImageHandle,
599 Handles[i],
600 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
601
602 debug("Find Ventoy Disk Handle:%p DP:%s", Handles[i],
603 ConvertDevicePathToText(gBlockData.pDiskDevPath, FALSE, FALSE));
604 break;
605 }
606 }
607
608 FreePool(Handles);
609
610 if (i >= Count)
611 {
612 return EFI_NOT_FOUND;
613 }
614 else
615 {
616 return EFI_SUCCESS;
617 }
618 }
619
620
621 STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle)
622 {
623 UINTN i = 0;
624 UINTN Count = 0;
625 EFI_HANDLE Parent = NULL;
626 EFI_HANDLE *Handles = NULL;
627 EFI_STATUS Status = EFI_SUCCESS;
628 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
629 EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
630
631 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
632 NULL, &Count, &Handles);
633 if (EFI_ERROR(Status))
634 {
635 return Status;
636 }
637
638 debug("ventoy_find_iso_disk_fs fs count:%u", Count);
639
640 for (i = 0; i < Count; i++)
641 {
642 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
643 if (EFI_ERROR(Status))
644 {
645 continue;
646 }
647
648 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
649 (VOID **)&pDevPath,
650 ImageHandle,
651 Handles[i],
652 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
653 if (EFI_ERROR(Status))
654 {
655 debug("Failed to open device path protocol %r", Status);
656 continue;
657 }
658
659 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
660 Parent = ventoy_get_parent_handle(pDevPath);
661
662 if (Parent == gBlockData.RawBlockIoHandle)
663 {
664 debug("Find ventoy disk fs");
665 gBlockData.DiskFsHandle = Handles[i];
666 gBlockData.pDiskFs = pFile;
667 gBlockData.pDiskFsDevPath = pDevPath;
668 break;
669 }
670 }
671
672 FreePool(Handles);
673
674 return EFI_SUCCESS;
675 }
676
677 STATIC EFI_STATUS EFIAPI ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle)
678 {
679 EFI_HANDLE Image = NULL;
680 EFI_STATUS Status = EFI_SUCCESS;
681 CHAR16 LogVar[4] = L"5";
682
683 if (gIsoUdf)
684 {
685 Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
686 gUdfEfiDriverPath,
687 sizeof(gUdfEfiDriverPath),
688 &Image);
689 debug("load iso UDF efi driver status:%r", Status);
690 }
691 else
692 {
693 Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
694 gIso9660EfiDriverPath,
695 sizeof(gIso9660EfiDriverPath),
696 &Image);
697 debug("load iso 9660 efi driver status:%r", Status);
698 }
699
700 if (gDebugPrint)
701 {
702 gRT->SetVariable(L"FS_LOGGING", &gShellVariableGuid,
703 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
704 sizeof(LogVar), LogVar);
705 }
706
707 gRT->SetVariable(L"FS_NAME_NOCASE", &gShellVariableGuid,
708 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
709 sizeof(LogVar), LogVar);
710
711 gBlockData.IsoDriverImage = Image;
712 Status = gBS->StartImage(Image, NULL, NULL);
713 debug("Start iso efi driver status:%r", Status);
714
715 return EFI_SUCCESS;
716 }
717
718 STATIC EFI_STATUS ventoy_proc_img_replace_name(ventoy_grub_param_file_replace *replace)
719 {
720 UINT32 i;
721 char tmp[256];
722
723 if (replace->magic != GRUB_IMG_REPLACE_MAGIC)
724 {
725 return EFI_SUCCESS;
726 }
727
728 if (replace->old_file_name[0][0] == 0)
729 {
730 return EFI_SUCCESS;
731 }
732
733 AsciiStrCpyS(tmp, sizeof(tmp), replace->old_file_name[0]);
734
735 for (i = 0; i < 256 && tmp[i]; i++)
736 {
737 if (tmp[i] == '/')
738 {
739 tmp[i] = '\\';
740 }
741 }
742
743 AsciiStrCpyS(replace->old_file_name[0], 256, tmp);
744 return EFI_SUCCESS;
745 }
746
747 EFI_STATUS EFIAPI ventoy_get_variable_wrapper
748 (
749 IN CHAR16 *VariableName,
750 IN EFI_GUID *VendorGuid,
751 OUT UINT32 *Attributes, OPTIONAL
752 IN OUT UINTN *DataSize,
753 OUT VOID *Data OPTIONAL
754 )
755 {
756 EFI_STATUS Status = EFI_SUCCESS;
757
758 Status = g_org_get_variable(VariableName, VendorGuid, Attributes, DataSize, Data);
759 if (StrCmp(VariableName, L"SecureBoot") == 0)
760 {
761 if ((*DataSize == 1) && Data)
762 {
763 *(UINT8 *)Data = 0;
764 }
765 }
766
767 return Status;
768 }
769
770 EFI_STATUS EFIAPI ventoy_exit_boot_service_wrapper
771 (
772 IN EFI_HANDLE ImageHandle,
773 IN UINTN MapKey
774 )
775 {
776 if (g_org_get_variable)
777 {
778 gRT->GetVariable = g_org_get_variable;
779 g_org_get_variable = NULL;
780 }
781
782 return g_org_exit_boot_service(ImageHandle, MapKey);
783 }
784
785 STATIC EFI_STATUS EFIAPI ventoy_disable_secure_boot(IN EFI_HANDLE ImageHandle)
786 {
787 UINT8 Value = 0;
788 UINTN DataSize = 1;
789 EFI_STATUS Status = EFI_SUCCESS;
790
791 Status = gRT->GetVariable(L"SecureBoot", &gEfiGlobalVariableGuid, NULL, &DataSize, &Value);
792 if (!EFI_ERROR(Status))
793 {
794 if (DataSize == 1 && Value == 0)
795 {
796 debug("Current secure boot is off, no need to disable");
797 return EFI_SUCCESS;
798 }
799 }
800
801 debug("ventoy_disable_secure_boot");
802
803 /* step1: wrapper security protocol. */
804 /* Do we still need it since we have been loaded ? */
805
806
807 /* step2: fake SecureBoot variable */
808 g_org_exit_boot_service = gBS->ExitBootServices;
809 gBS->ExitBootServices = ventoy_exit_boot_service_wrapper;
810
811 g_org_get_variable = gRT->GetVariable;
812 gRT->GetVariable = ventoy_get_variable_wrapper;
813
814 return EFI_SUCCESS;
815 }
816
817
818 STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
819 {
820 UINT32 i = 0;
821 UINT32 old_cnt = 0;
822 UINTN size = 0;
823 UINT8 chksum = 0;
824 const char *pEnv = NULL;
825 CHAR16 *pPos = NULL;
826 CHAR16 *pCmdLine = NULL;
827 EFI_STATUS Status = EFI_SUCCESS;
828 ventoy_grub_param *pGrubParam = NULL;
829 EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
830 ventoy_chain_head *chain = NULL;
831
832 Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
833 if (EFI_ERROR(Status))
834 {
835 VtoyDebug("Failed to handle load image protocol %r", Status);
836 return Status;
837 }
838
839 pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
840 SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
841 CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
842
843 if (StrStr(pCmdLine, L"debug"))
844 {
845 gDebugPrint = TRUE;
846 }
847
848 if (StrStr(pCmdLine, L"fallback"))
849 {
850 gBootFallBack = TRUE;
851 }
852
853 if (StrStr(pCmdLine, L"dotefi"))
854 {
855 gDotEfiBoot = TRUE;
856 }
857
858 if (StrStr(pCmdLine, L"isoefi=on"))
859 {
860 gLoadIsoEfi = TRUE;
861 }
862
863 if (StrStr(pCmdLine, L"iso_udf"))
864 {
865 gIsoUdf = TRUE;
866 }
867
868 pPos = StrStr(pCmdLine, L"FirstTry=@");
869 if (pPos)
870 {
871 pPos += StrLen(L"FirstTry=");
872 for (i = 0; i < ARRAY_SIZE(gFirstTryBootFile); i++, pPos++)
873 {
874 if (*pPos != L' ' && *pPos != L'\t' && *pPos)
875 {
876 gFirstTryBootFile[i] = (*pPos == '@') ? '\\' : *pPos;
877 }
878 else
879 {
880 break;
881 }
882 }
883
884 gEfiBootFileName[0] = gFirstTryBootFile;
885 gBootFileStartIndex = 0;
886 }
887
888 debug("cmdline:<%s>", pCmdLine);
889
890 if (gFirstTryBootFile[0])
891 {
892 debug("First Try:<%s>", gFirstTryBootFile);
893 }
894
895 pPos = StrStr(pCmdLine, L"env_param=");
896 if (!pPos)
897 {
898 return EFI_INVALID_PARAMETER;
899 }
900
901 pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
902 grub_env_set = pGrubParam->grub_env_set;
903 grub_env_get = pGrubParam->grub_env_get;
904 pEnv = grub_env_get("VTOY_CHKDEV_RESULT_STRING");
905 if (!pEnv)
906 {
907 return EFI_INVALID_PARAMETER;
908 }
909
910 if (pEnv[0] != '0' || pEnv[1] != 0)
911 {
912 ventoy_warn_invalid_device();
913 return EFI_INVALID_PARAMETER;
914 }
915
916 g_file_replace_list = &pGrubParam->file_replace;
917 old_cnt = g_file_replace_list->old_file_cnt;
918 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
919 g_file_replace_list->magic,
920 g_file_replace_list->new_file_virtual_id,
921 old_cnt,
922 old_cnt > 0 ? g_file_replace_list->old_file_name[0] : "",
923 old_cnt > 1 ? g_file_replace_list->old_file_name[1] : "",
924 old_cnt > 2 ? g_file_replace_list->old_file_name[2] : "",
925 old_cnt > 3 ? g_file_replace_list->old_file_name[3] : ""
926 );
927
928 g_img_replace_list = &pGrubParam->img_replace;
929 ventoy_proc_img_replace_name(g_img_replace_list);
930 old_cnt = g_img_replace_list->old_file_cnt;
931 debug("img replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
932 g_img_replace_list->magic,
933 g_img_replace_list->new_file_virtual_id,
934 old_cnt,
935 old_cnt > 0 ? g_img_replace_list->old_file_name[0] : "",
936 old_cnt > 1 ? g_img_replace_list->old_file_name[1] : "",
937 old_cnt > 2 ? g_img_replace_list->old_file_name[2] : "",
938 old_cnt > 3 ? g_img_replace_list->old_file_name[3] : ""
939 );
940
941 pPos = StrStr(pCmdLine, L"mem:");
942 chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
943
944 pPos = StrStr(pPos, L"size:");
945 size = StrDecimalToUintn(pPos + 5);
946
947 debug("memory addr:%p size:%lu", chain, size);
948
949 if (StrStr(pCmdLine, L"sector512"))
950 {
951 gSector512Mode = TRUE;
952 }
953
954 if (StrStr(pCmdLine, L"memdisk"))
955 {
956 g_iso_data_buf = (UINT8 *)chain + sizeof(ventoy_chain_head);
957 g_iso_buf_size = size - sizeof(ventoy_chain_head);
958 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size);
959
960 g_chain = chain;
961 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
962 gMemdiskMode = TRUE;
963 }
964 else
965 {
966 debug("This is normal mode");
967 g_chain = AllocatePool(size);
968 CopyMem(g_chain, chain, size);
969
970 g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
971 g_img_chunk_num = g_chain->img_chunk_num;
972 g_override_chunk = (ventoy_override_chunk *)((char *)g_chain + g_chain->override_chunk_offset);
973 g_override_chunk_num = g_chain->override_chunk_num;
974 g_virt_chunk = (ventoy_virt_chunk *)((char *)g_chain + g_chain->virt_chunk_offset);
975 g_virt_chunk_num = g_chain->virt_chunk_num;
976
977 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
978
979 /* Workaround for Windows & ISO9660 */
980 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[3] == 0)
981 {
982 g_fixup_iso9660_secover_enable = TRUE;
983 }
984
985 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[4] != 1)
986 {
987 g_hook_keyboard = TRUE;
988 }
989
990 if (g_os_param_reserved[5] == 1 && g_os_param_reserved[2] == ventoy_chain_linux)
991 {
992 ventoy_disable_secure_boot(ImageHandle);
993 }
994
995 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable, g_hook_keyboard);
996
997 for (i = 0; i < sizeof(ventoy_os_param); i++)
998 {
999 chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
1000 }
1001
1002 if (gDebugPrint)
1003 {
1004 debug("os param checksum: 0x%x %a", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
1005 }
1006
1007 ventoy_update_image_location(&(g_chain->os_param));
1008
1009 if (gDebugPrint)
1010 {
1011 ventoy_dump_chain(g_chain);
1012 }
1013 }
1014
1015 g_fix_windows_1st_cdrom_issue = FALSE;
1016 if (ventoy_chain_windows == g_os_param_reserved[2] ||
1017 ventoy_chain_wim == g_os_param_reserved[2])
1018 {
1019 if (ventoy_is_cdrom_dp_exist())
1020 {
1021 debug("fixup the 1st cdrom influences when boot windows ...");
1022 g_fix_windows_1st_cdrom_issue = TRUE;
1023 }
1024 }
1025
1026 ventoy_debug_pause();
1027
1028 FreePool(pCmdLine);
1029 return EFI_SUCCESS;
1030 }
1031
1032 EFI_STATUS EFIAPI ventoy_clean_env(VOID)
1033 {
1034 FreePool(g_sector_flag);
1035 g_sector_flag_num = 0;
1036
1037 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1038 {
1039 gBS->UnloadImage(gBlockData.IsoDriverImage);
1040 }
1041
1042 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1043
1044 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1045 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1046 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1047 NULL);
1048
1049 ventoy_delete_variable();
1050
1051 if (g_vtoy_img_location_buf)
1052 {
1053 FreePool(g_vtoy_img_location_buf);
1054 }
1055
1056 if (!gMemdiskMode)
1057 {
1058 FreePool(g_chain);
1059 }
1060
1061 return EFI_SUCCESS;
1062 }
1063
1064 STATIC EFI_STATUS ventoy_hook_start(VOID)
1065 {
1066 /* don't add debug print in this function */
1067
1068 if (g_fix_windows_1st_cdrom_issue)
1069 {
1070 ventoy_hook_1st_cdrom_start();
1071 }
1072
1073 /* let this the last */
1074 if (g_hook_keyboard)
1075 {
1076 ventoy_hook_keyboard_start();
1077 }
1078
1079 return EFI_SUCCESS;
1080 }
1081
1082 STATIC EFI_STATUS ventoy_hook_stop(VOID)
1083 {
1084 /* don't add debug print in this function */
1085
1086 if (g_fix_windows_1st_cdrom_issue)
1087 {
1088 ventoy_hook_1st_cdrom_stop();
1089 }
1090
1091 /* let this the last */
1092 if (g_hook_keyboard)
1093 {
1094 ventoy_hook_keyboard_stop();
1095 }
1096
1097 return EFI_SUCCESS;
1098 }
1099
1100 EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
1101 {
1102 UINTN t = 0;
1103 UINTN i = 0;
1104 UINTN j = 0;
1105 UINTN Find = 0;
1106 UINTN Count = 0;
1107 EFI_HANDLE Image = NULL;
1108 EFI_HANDLE *Handles = NULL;
1109 EFI_STATUS Status = EFI_SUCCESS;
1110 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
1111 EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
1112
1113 for (t = 0; t < 3; t++)
1114 {
1115 Count = 0;
1116 Handles = NULL;
1117
1118 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
1119 NULL, &Count, &Handles);
1120 if (EFI_ERROR(Status))
1121 {
1122 return Status;
1123 }
1124
1125 debug("ventoy_boot fs count:%u", Count);
1126
1127 for (i = 0; i < Count; i++)
1128 {
1129 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
1130 if (EFI_ERROR(Status))
1131 {
1132 continue;
1133 }
1134
1135 debug("FS:%u Protocol:%p OpenVolume:%p", i, pFile, pFile->OpenVolume);
1136
1137 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
1138 (VOID **)&pDevPath,
1139 ImageHandle,
1140 Handles[i],
1141 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1142 if (EFI_ERROR(Status))
1143 {
1144 debug("Failed to open device path protocol %r", Status);
1145 continue;
1146 }
1147
1148 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
1149 if (CompareMem(gBlockData.Path, pDevPath, gBlockData.DevicePathCompareLen))
1150 {
1151 debug("Not ventoy disk file system");
1152 continue;
1153 }
1154
1155 for (j = gBootFileStartIndex; j < ARRAY_SIZE(gEfiBootFileName); j++)
1156 {
1157 Status = ventoy_load_image(ImageHandle, pDevPath, gEfiBootFileName[j],
1158 StrSize(gEfiBootFileName[j]), &Image);
1159 if (EFI_SUCCESS == Status)
1160 {
1161 break;
1162 }
1163 debug("Failed to load image %r <%s>", Status, gEfiBootFileName[j]);
1164 }
1165
1166 if (j >= ARRAY_SIZE(gEfiBootFileName))
1167 {
1168 continue;
1169 }
1170
1171 Find++;
1172 debug("Find boot file, now try to boot .....");
1173 ventoy_debug_pause();
1174
1175 if (gDebugPrint)
1176 {
1177 gST->ConIn->Reset(gST->ConIn, FALSE);
1178 }
1179
1180 if ((g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC) ||
1181 (g_img_replace_list && g_img_replace_list->magic == GRUB_IMG_REPLACE_MAGIC))
1182 {
1183 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
1184 pFile->OpenVolume = ventoy_wrapper_open_volume;
1185 }
1186
1187 ventoy_hook_start();
1188 /* can't add debug print here */
1189 //ventoy_wrapper_system();
1190 Status = gBS->StartImage(Image, NULL, NULL);
1191 ventoy_hook_stop();
1192
1193 if (EFI_ERROR(Status))
1194 {
1195 debug("Failed to start image %r", Status);
1196 sleep(3);
1197 gBS->UnloadImage(Image);
1198 break;
1199 }
1200 }
1201
1202 FreePool(Handles);
1203
1204 if (Find == 0)
1205 {
1206 if (gDotEfiBoot)
1207 {
1208 break;
1209 }
1210
1211 debug("Fs not found, now wait and retry...");
1212 sleep(1);
1213 }
1214 }
1215
1216 if (Find == 0)
1217 {
1218 return EFI_NOT_FOUND;
1219 }
1220
1221 return EFI_SUCCESS;
1222 }
1223
1224 EFI_STATUS EFIAPI VentoyEfiMain
1225 (
1226 IN EFI_HANDLE ImageHandle,
1227 IN EFI_SYSTEM_TABLE *SystemTable
1228 )
1229 {
1230 EFI_STATUS Status = EFI_SUCCESS;
1231 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *Protocol;
1232
1233 g_sector_flag_num = 512; /* initial value */
1234
1235 g_sector_flag = AllocatePool(g_sector_flag_num * sizeof(ventoy_sector_flag));
1236 if (NULL == g_sector_flag)
1237 {
1238 return EFI_OUT_OF_RESOURCES;
1239 }
1240
1241 Status = gBS->HandleProtocol(gST->ConsoleInHandle, &gEfiSimpleTextInputExProtocolGuid, (VOID **)&Protocol);
1242 if (EFI_SUCCESS == Status)
1243 {
1244 g_con_simple_input_ex = Protocol;
1245 }
1246
1247 gST->ConOut->ClearScreen(gST->ConOut);
1248 ventoy_clear_input();
1249
1250 Status = ventoy_parse_cmdline(ImageHandle);
1251 if (EFI_ERROR(Status))
1252 {
1253 return Status;
1254 }
1255
1256 ventoy_disable_ex_filesystem();
1257
1258 if (gMemdiskMode)
1259 {
1260 g_ramdisk_param.PhyAddr = (UINT64)(UINTN)g_iso_data_buf;
1261 g_ramdisk_param.DiskSize = (UINT64)g_iso_buf_size;
1262
1263 ventoy_save_ramdisk_param();
1264
1265 if (gLoadIsoEfi)
1266 {
1267 ventoy_find_iso_disk(ImageHandle);
1268 ventoy_find_iso_disk_fs(ImageHandle);
1269 ventoy_load_isoefi_driver(ImageHandle);
1270 }
1271
1272 ventoy_install_blockio(ImageHandle, g_iso_buf_size);
1273 ventoy_debug_pause();
1274
1275 Status = ventoy_boot(ImageHandle);
1276
1277 ventoy_delete_ramdisk_param();
1278
1279 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1280 {
1281 gBS->UnloadImage(gBlockData.IsoDriverImage);
1282 }
1283
1284 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1285 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1286 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1287 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1288 NULL);
1289 }
1290 else
1291 {
1292 ventoy_save_variable();
1293 Status = ventoy_find_iso_disk(ImageHandle);
1294 if (!EFI_ERROR(Status))
1295 {
1296 if (gLoadIsoEfi)
1297 {
1298 ventoy_find_iso_disk_fs(ImageHandle);
1299 ventoy_load_isoefi_driver(ImageHandle);
1300 }
1301
1302 ventoy_debug_pause();
1303
1304 ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
1305
1306 ventoy_debug_pause();
1307
1308 Status = ventoy_boot(ImageHandle);
1309 }
1310
1311 ventoy_clean_env();
1312 }
1313
1314 if (FALSE == gDotEfiBoot && FALSE == gBootFallBack)
1315 {
1316 if (EFI_NOT_FOUND == Status)
1317 {
1318 gST->ConOut->OutputString(gST->ConOut, L"No bootfile found for UEFI!\r\n");
1319 gST->ConOut->OutputString(gST->ConOut, L"Maybe the image does not support " VENTOY_UEFI_DESC L"!\r\n");
1320 sleep(30);
1321 }
1322 }
1323
1324 ventoy_clear_input();
1325 gST->ConOut->ClearScreen(gST->ConOut);
1326
1327 if (gDotEfiBoot && (EFI_NOT_FOUND == Status))
1328 {
1329 grub_env_set("vtoy_dotefi_retry", "YES");
1330 }
1331
1332 ventoy_enable_ex_filesystem();
1333
1334 return EFI_SUCCESS;
1335 }
1336