]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.c
update tip message for VTOY_DEFAULT_SEARCH_ROOT (#1373)
[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>",
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 );
239
240 ventoy_debug_pause();
241
242 debug("chain->disk_drive=0x%x", chain->disk_drive);
243 debug("chain->disk_sector_size=%u", chain->disk_sector_size);
244 debug("chain->real_img_size_in_bytes=%llu", chain->real_img_size_in_bytes);
245 debug("chain->virt_img_size_in_bytes=%llu", chain->virt_img_size_in_bytes);
246 debug("chain->boot_catalog=%u", chain->boot_catalog);
247 debug("chain->img_chunk_offset=%u", chain->img_chunk_offset);
248 debug("chain->img_chunk_num=%u", chain->img_chunk_num);
249 debug("chain->override_chunk_offset=%u", chain->override_chunk_offset);
250 debug("chain->override_chunk_num=%u", chain->override_chunk_num);
251
252 ventoy_debug_pause();
253
254 ventoy_dump_img_chunk(chain);
255 ventoy_dump_override_chunk(chain);
256 ventoy_dump_virt_chunk(chain);
257 }
258
259 static int ventoy_update_image_location(ventoy_os_param *param)
260 {
261 EFI_STATUS Status = EFI_SUCCESS;
262 UINT8 chksum = 0;
263 unsigned int i;
264 unsigned int length;
265 UINTN address = 0;
266 void *buffer = NULL;
267 ventoy_image_location *location = NULL;
268 ventoy_image_disk_region *region = NULL;
269 ventoy_img_chunk *chunk = g_chunk;
270
271 length = sizeof(ventoy_image_location) + (g_img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
272
273 Status = gBS->AllocatePool(EfiRuntimeServicesData, length + 4096 * 2, &buffer);
274 if (EFI_ERROR(Status) || NULL == buffer)
275 {
276 debug("Failed to allocate runtime pool %r\n", Status);
277 return 1;
278 }
279
280 address = (UINTN)buffer;
281 g_vtoy_img_location_buf = buffer;
282
283 if (address % 4096)
284 {
285 address += 4096 - (address % 4096);
286 }
287
288 param->chksum = 0;
289 param->vtoy_img_location_addr = address;
290 param->vtoy_img_location_len = length;
291
292 /* update check sum */
293 for (i = 0; i < sizeof(ventoy_os_param); i++)
294 {
295 chksum += *((UINT8 *)param + i);
296 }
297 param->chksum = (chksum == 0) ? 0 : (UINT8)(0x100 - chksum);
298
299 location = (ventoy_image_location *)(unsigned long)(param->vtoy_img_location_addr);
300 if (NULL == location)
301 {
302 return 0;
303 }
304
305 CopyMem(&location->guid, &param->guid, sizeof(ventoy_guid));
306 location->image_sector_size = gSector512Mode ? 512 : 2048;
307 location->disk_sector_size = g_chain->disk_sector_size;
308 location->region_count = g_img_chunk_num;
309
310 region = location->regions;
311
312 if (gSector512Mode)
313 {
314 for (i = 0; i < g_img_chunk_num; i++)
315 {
316 region->image_sector_count = chunk->disk_end_sector - chunk->disk_start_sector + 1;
317 region->image_start_sector = chunk->img_start_sector * 4;
318 region->disk_start_sector = chunk->disk_start_sector;
319 region++;
320 chunk++;
321 }
322 }
323 else
324 {
325 for (i = 0; i < g_img_chunk_num; i++)
326 {
327 region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
328 region->image_start_sector = chunk->img_start_sector;
329 region->disk_start_sector = chunk->disk_start_sector;
330 region++;
331 chunk++;
332 }
333 }
334
335 return 0;
336 }
337
338 EFI_HANDLE EFIAPI ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL *pDevPath)
339 {
340 EFI_HANDLE Handle = NULL;
341 EFI_STATUS Status = EFI_SUCCESS;
342 EFI_DEVICE_PATH_PROTOCOL *pLastNode = NULL;
343 EFI_DEVICE_PATH_PROTOCOL *pCurNode = NULL;
344 EFI_DEVICE_PATH_PROTOCOL *pTmpDevPath = NULL;
345
346 pTmpDevPath = DuplicateDevicePath(pDevPath);
347 if (!pTmpDevPath)
348 {
349 return NULL;
350 }
351
352 pCurNode = pTmpDevPath;
353 while (!IsDevicePathEnd(pCurNode))
354 {
355 pLastNode = pCurNode;
356 pCurNode = NextDevicePathNode(pCurNode);
357 }
358 if (pLastNode)
359 {
360 CopyMem(pLastNode, pCurNode, sizeof(EFI_DEVICE_PATH_PROTOCOL));
361 }
362
363 pCurNode = pTmpDevPath;
364 Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid, &pCurNode, &Handle);
365 debug("Status:%r Parent Handle:%p DP:%s", Status, Handle, ConvertDevicePathToText(pTmpDevPath, FALSE, FALSE));
366
367 FreePool(pTmpDevPath);
368
369 return Handle;
370 }
371
372 STATIC ventoy_ram_disk g_backup_ramdisk_param;
373 STATIC ventoy_os_param g_backup_os_param_var;
374
375
376 EFI_STATUS EFIAPI ventoy_save_ramdisk_param(VOID)
377 {
378 UINTN DataSize;
379 EFI_STATUS Status = EFI_SUCCESS;
380 EFI_GUID VarGuid = VENTOY_GUID;
381
382 DataSize = sizeof(g_backup_ramdisk_param);
383 Status = gRT->GetVariable(L"VentoyRamDisk", &VarGuid, NULL, &DataSize, &g_backup_ramdisk_param);
384 if (!EFI_ERROR(Status))
385 {
386 debug("find previous ramdisk variable <%llu>", g_backup_ramdisk_param.DiskSize);
387 }
388
389 Status = gRT->SetVariable(L"VentoyRamDisk", &VarGuid,
390 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
391 sizeof(g_ramdisk_param), &(g_ramdisk_param));
392 debug("set ramdisk variable %r", Status);
393
394 return Status;
395 }
396
397 EFI_STATUS EFIAPI ventoy_delete_ramdisk_param(VOID)
398 {
399 EFI_STATUS Status = EFI_SUCCESS;
400 EFI_GUID VarGuid = VENTOY_GUID;
401
402 if (g_backup_ramdisk_param.DiskSize > 0 && g_backup_ramdisk_param.PhyAddr > 0)
403 {
404 Status = gRT->SetVariable(L"VentoyRamDisk", &VarGuid,
405 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
406 sizeof(g_backup_ramdisk_param), &g_backup_ramdisk_param);
407 debug("resotre ramdisk variable %r", Status);
408 }
409 else
410 {
411 Status = gRT->SetVariable(L"VentoyRamDisk", &VarGuid,
412 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
413 0, NULL);
414 debug("delete ramdisk variable %r", Status);
415 }
416
417 return Status;
418 }
419
420 EFI_STATUS EFIAPI ventoy_save_variable(VOID)
421 {
422 UINTN DataSize;
423 EFI_STATUS Status = EFI_SUCCESS;
424 EFI_GUID VarGuid = VENTOY_GUID;
425
426 DataSize = sizeof(g_backup_os_param_var);
427 Status = gRT->GetVariable(L"VentoyOsParam", &VarGuid, NULL, &DataSize, &g_backup_os_param_var);
428 if (!EFI_ERROR(Status))
429 {
430 debug("find previous efi variable <%a>", g_backup_os_param_var.vtoy_img_path);
431 }
432
433 Status = gRT->SetVariable(L"VentoyOsParam", &VarGuid,
434 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
435 sizeof(g_chain->os_param), &(g_chain->os_param));
436 debug("set efi variable %r", Status);
437
438 return Status;
439 }
440
441 EFI_STATUS EFIAPI ventoy_delete_variable(VOID)
442 {
443 EFI_STATUS Status = EFI_SUCCESS;
444 EFI_GUID VarGuid = VENTOY_GUID;
445
446 if (0 == CompareMem(&(g_backup_os_param_var.guid), &VarGuid, sizeof(EFI_GUID)))
447 {
448 Status = gRT->SetVariable(L"VentoyOsParam", &VarGuid,
449 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
450 sizeof(g_backup_os_param_var), &(g_backup_os_param_var));
451 debug("restore efi variable %r", Status);
452 }
453 else
454 {
455 Status = gRT->SetVariable(L"VentoyOsParam", &VarGuid,
456 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
457 0, NULL);
458 debug("delete efi variable %r", Status);
459 }
460
461 return Status;
462 }
463
464 #if (VENTOY_DEVICE_WARN != 0)
465 STATIC VOID ventoy_warn_invalid_device(VOID)
466 {
467 STATIC BOOLEAN flag = FALSE;
468
469 if (flag)
470 {
471 return;
472 }
473
474 flag = TRUE;
475 gST->ConOut->ClearScreen(gST->ConOut);
476 gST->ConOut->OutputString(gST->ConOut, VTOY_WARNING L"\r\n");
477 gST->ConOut->OutputString(gST->ConOut, VTOY_WARNING L"\r\n");
478 gST->ConOut->OutputString(gST->ConOut, VTOY_WARNING L"\r\n\r\n\r\n");
479
480 gST->ConOut->OutputString(gST->ConOut, L"This is NOT a standard Ventoy device and is NOT supported.\r\n\r\n");
481 gST->ConOut->OutputString(gST->ConOut, L"You should follow the official instructions in https://www.ventoy.net\r\n");
482
483 gST->ConOut->OutputString(gST->ConOut, L"\r\n\r\nWill exit after 10 seconds ...... ");
484
485 sleep(10);
486 }
487 #else
488 STATIC VOID ventoy_warn_invalid_device(VOID)
489 {
490
491 }
492 #endif
493
494 STATIC EFI_STATUS EFIAPI ventoy_load_image
495 (
496 IN EFI_HANDLE ImageHandle,
497 IN EFI_DEVICE_PATH_PROTOCOL *pDevicePath,
498 IN CONST CHAR16 *FileName,
499 IN UINTN FileNameLen,
500 OUT EFI_HANDLE *Image
501 )
502 {
503 EFI_STATUS Status = EFI_SUCCESS;
504 CHAR16 TmpBuf[256] = {0};
505 FILEPATH_DEVICE_PATH *pFilePath = NULL;
506 EFI_DEVICE_PATH_PROTOCOL *pImgPath = NULL;
507
508 pFilePath = (FILEPATH_DEVICE_PATH *)TmpBuf;
509 pFilePath->Header.Type = MEDIA_DEVICE_PATH;
510 pFilePath->Header.SubType = MEDIA_FILEPATH_DP;
511 pFilePath->Header.Length[0] = FileNameLen + sizeof(EFI_DEVICE_PATH_PROTOCOL);
512 pFilePath->Header.Length[1] = 0;
513 CopyMem(pFilePath->PathName, FileName, FileNameLen);
514
515 pImgPath = AppendDevicePathNode(pDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)pFilePath);
516 if (!pImgPath)
517 {
518 return EFI_NOT_FOUND;
519 }
520
521 Status = gBS->LoadImage(FALSE, ImageHandle, pImgPath, NULL, 0, Image);
522
523 debug("Load Image File %r DP: <%s>", Status, ConvertDevicePathToText(pImgPath, FALSE, FALSE));
524
525 FreePool(pImgPath);
526
527 return Status;
528 }
529
530
531 STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle)
532 {
533 UINTN i = 0;
534 UINTN Count = 0;
535 UINT64 DiskSize = 0;
536 MBR_HEAD *pMBR = NULL;
537 UINT8 *pBuffer = NULL;
538 EFI_HANDLE *Handles;
539 EFI_STATUS Status = EFI_SUCCESS;
540 EFI_BLOCK_IO_PROTOCOL *pBlockIo;
541
542 pBuffer = AllocatePool(2048);
543 if (!pBuffer)
544 {
545 return EFI_OUT_OF_RESOURCES;
546 }
547
548 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiBlockIoProtocolGuid,
549 NULL, &Count, &Handles);
550 if (EFI_ERROR(Status))
551 {
552 FreePool(pBuffer);
553 return Status;
554 }
555
556 for (i = 0; i < Count; i++)
557 {
558 Status = gBS->HandleProtocol(Handles[i], &gEfiBlockIoProtocolGuid, (VOID **)&pBlockIo);
559 if (EFI_ERROR(Status))
560 {
561 continue;
562 }
563
564 DiskSize = (pBlockIo->Media->LastBlock + 1) * pBlockIo->Media->BlockSize;
565 debug("This Disk size: %llu", DiskSize);
566 if (g_chain->os_param.vtoy_disk_size != DiskSize)
567 {
568 continue;
569 }
570
571 Status = pBlockIo->ReadBlocks(pBlockIo, pBlockIo->Media->MediaId, 0, 512, pBuffer);
572 if (EFI_ERROR(Status))
573 {
574 debug("ReadBlocks filed %r", Status);
575 continue;
576 }
577
578 if (CompareMem(g_chain->os_param.vtoy_disk_guid, pBuffer + 0x180, 16) == 0)
579 {
580 pMBR = (MBR_HEAD *)pBuffer;
581 if (pMBR->PartTbl[0].FsFlag != 0xEE)
582 {
583 if (pMBR->PartTbl[0].StartSectorId != 2048 ||
584 pMBR->PartTbl[1].SectorCount != 65536 ||
585 pMBR->PartTbl[1].StartSectorId != pMBR->PartTbl[0].StartSectorId + pMBR->PartTbl[0].SectorCount)
586 {
587 debug("Failed to check disk part table");
588 ventoy_warn_invalid_device();
589 }
590 }
591
592 gBlockData.RawBlockIoHandle = Handles[i];
593 gBlockData.pRawBlockIo = pBlockIo;
594 gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
595 (VOID **)&(gBlockData.pDiskDevPath),
596 ImageHandle,
597 Handles[i],
598 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
599
600 debug("Find Ventoy Disk Handle:%p DP:%s", Handles[i],
601 ConvertDevicePathToText(gBlockData.pDiskDevPath, FALSE, FALSE));
602 break;
603 }
604 }
605
606 FreePool(Handles);
607
608 if (i >= Count)
609 {
610 return EFI_NOT_FOUND;
611 }
612 else
613 {
614 return EFI_SUCCESS;
615 }
616 }
617
618
619 STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle)
620 {
621 UINTN i = 0;
622 UINTN Count = 0;
623 EFI_HANDLE Parent = NULL;
624 EFI_HANDLE *Handles = NULL;
625 EFI_STATUS Status = EFI_SUCCESS;
626 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
627 EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
628
629 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
630 NULL, &Count, &Handles);
631 if (EFI_ERROR(Status))
632 {
633 return Status;
634 }
635
636 debug("ventoy_find_iso_disk_fs fs count:%u", Count);
637
638 for (i = 0; i < Count; i++)
639 {
640 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
641 if (EFI_ERROR(Status))
642 {
643 continue;
644 }
645
646 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
647 (VOID **)&pDevPath,
648 ImageHandle,
649 Handles[i],
650 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
651 if (EFI_ERROR(Status))
652 {
653 debug("Failed to open device path protocol %r", Status);
654 continue;
655 }
656
657 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
658 Parent = ventoy_get_parent_handle(pDevPath);
659
660 if (Parent == gBlockData.RawBlockIoHandle)
661 {
662 debug("Find ventoy disk fs");
663 gBlockData.DiskFsHandle = Handles[i];
664 gBlockData.pDiskFs = pFile;
665 gBlockData.pDiskFsDevPath = pDevPath;
666 break;
667 }
668 }
669
670 FreePool(Handles);
671
672 return EFI_SUCCESS;
673 }
674
675 STATIC EFI_STATUS EFIAPI ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle)
676 {
677 EFI_HANDLE Image = NULL;
678 EFI_STATUS Status = EFI_SUCCESS;
679 CHAR16 LogVar[4] = L"5";
680
681 if (gIsoUdf)
682 {
683 Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
684 gUdfEfiDriverPath,
685 sizeof(gUdfEfiDriverPath),
686 &Image);
687 debug("load iso UDF efi driver status:%r", Status);
688 }
689 else
690 {
691 Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
692 gIso9660EfiDriverPath,
693 sizeof(gIso9660EfiDriverPath),
694 &Image);
695 debug("load iso 9660 efi driver status:%r", Status);
696 }
697
698 if (gDebugPrint)
699 {
700 gRT->SetVariable(L"FS_LOGGING", &gShellVariableGuid,
701 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
702 sizeof(LogVar), LogVar);
703 }
704
705 gRT->SetVariable(L"FS_NAME_NOCASE", &gShellVariableGuid,
706 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
707 sizeof(LogVar), LogVar);
708
709 gBlockData.IsoDriverImage = Image;
710 Status = gBS->StartImage(Image, NULL, NULL);
711 debug("Start iso efi driver status:%r", Status);
712
713 return EFI_SUCCESS;
714 }
715
716 STATIC EFI_STATUS ventoy_proc_img_replace_name(ventoy_grub_param_file_replace *replace)
717 {
718 UINT32 i;
719 char tmp[256];
720
721 if (replace->magic != GRUB_IMG_REPLACE_MAGIC)
722 {
723 return EFI_SUCCESS;
724 }
725
726 if (replace->old_file_name[0][0] == 0)
727 {
728 return EFI_SUCCESS;
729 }
730
731 AsciiStrCpyS(tmp, sizeof(tmp), replace->old_file_name[0]);
732
733 for (i = 0; i < 256 && tmp[i]; i++)
734 {
735 if (tmp[i] == '/')
736 {
737 tmp[i] = '\\';
738 }
739 }
740
741 AsciiStrCpyS(replace->old_file_name[0], 256, tmp);
742 return EFI_SUCCESS;
743 }
744
745 EFI_STATUS EFIAPI ventoy_get_variable_wrapper
746 (
747 IN CHAR16 *VariableName,
748 IN EFI_GUID *VendorGuid,
749 OUT UINT32 *Attributes, OPTIONAL
750 IN OUT UINTN *DataSize,
751 OUT VOID *Data OPTIONAL
752 )
753 {
754 EFI_STATUS Status = EFI_SUCCESS;
755
756 Status = g_org_get_variable(VariableName, VendorGuid, Attributes, DataSize, Data);
757 if (StrCmp(VariableName, L"SecureBoot") == 0)
758 {
759 if ((*DataSize == 1) && Data)
760 {
761 *(UINT8 *)Data = 0;
762 }
763 }
764
765 return Status;
766 }
767
768 EFI_STATUS EFIAPI ventoy_exit_boot_service_wrapper
769 (
770 IN EFI_HANDLE ImageHandle,
771 IN UINTN MapKey
772 )
773 {
774 if (g_org_get_variable)
775 {
776 gRT->GetVariable = g_org_get_variable;
777 g_org_get_variable = NULL;
778 }
779
780 return g_org_exit_boot_service(ImageHandle, MapKey);
781 }
782
783 STATIC EFI_STATUS EFIAPI ventoy_disable_secure_boot(IN EFI_HANDLE ImageHandle)
784 {
785 UINT8 Value = 0;
786 UINTN DataSize = 1;
787 EFI_STATUS Status = EFI_SUCCESS;
788
789 Status = gRT->GetVariable(L"SecureBoot", &gEfiGlobalVariableGuid, NULL, &DataSize, &Value);
790 if (!EFI_ERROR(Status))
791 {
792 if (DataSize == 1 && Value == 0)
793 {
794 debug("Current secure boot is off, no need to disable");
795 return EFI_SUCCESS;
796 }
797 }
798
799 debug("ventoy_disable_secure_boot");
800
801 /* step1: wrapper security protocol. */
802 /* Do we still need it since we have been loaded ? */
803
804
805 /* step2: fake SecureBoot variable */
806 g_org_exit_boot_service = gBS->ExitBootServices;
807 gBS->ExitBootServices = ventoy_exit_boot_service_wrapper;
808
809 g_org_get_variable = gRT->GetVariable;
810 gRT->GetVariable = ventoy_get_variable_wrapper;
811
812 return EFI_SUCCESS;
813 }
814
815
816 STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
817 {
818 UINT32 i = 0;
819 UINT32 old_cnt = 0;
820 UINTN size = 0;
821 UINT8 chksum = 0;
822 const char *pEnv = NULL;
823 CHAR16 *pPos = NULL;
824 CHAR16 *pCmdLine = NULL;
825 EFI_STATUS Status = EFI_SUCCESS;
826 ventoy_grub_param *pGrubParam = NULL;
827 EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
828 ventoy_chain_head *chain = NULL;
829
830 Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
831 if (EFI_ERROR(Status))
832 {
833 VtoyDebug("Failed to handle load image protocol %r", Status);
834 return Status;
835 }
836
837 pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
838 SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
839 CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
840
841 if (StrStr(pCmdLine, L"debug"))
842 {
843 gDebugPrint = TRUE;
844 }
845
846 if (StrStr(pCmdLine, L"fallback"))
847 {
848 gBootFallBack = TRUE;
849 }
850
851 if (StrStr(pCmdLine, L"dotefi"))
852 {
853 gDotEfiBoot = TRUE;
854 }
855
856 if (StrStr(pCmdLine, L"isoefi=on"))
857 {
858 gLoadIsoEfi = TRUE;
859 }
860
861 if (StrStr(pCmdLine, L"iso_udf"))
862 {
863 gIsoUdf = TRUE;
864 }
865
866 pPos = StrStr(pCmdLine, L"FirstTry=@");
867 if (pPos)
868 {
869 pPos += StrLen(L"FirstTry=");
870 for (i = 0; i < ARRAY_SIZE(gFirstTryBootFile); i++, pPos++)
871 {
872 if (*pPos != L' ' && *pPos != L'\t' && *pPos)
873 {
874 gFirstTryBootFile[i] = (*pPos == '@') ? '\\' : *pPos;
875 }
876 else
877 {
878 break;
879 }
880 }
881
882 gEfiBootFileName[0] = gFirstTryBootFile;
883 gBootFileStartIndex = 0;
884 }
885
886 debug("cmdline:<%s>", pCmdLine);
887
888 if (gFirstTryBootFile[0])
889 {
890 debug("First Try:<%s>", gFirstTryBootFile);
891 }
892
893 pPos = StrStr(pCmdLine, L"env_param=");
894 if (!pPos)
895 {
896 return EFI_INVALID_PARAMETER;
897 }
898
899 pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
900 grub_env_set = pGrubParam->grub_env_set;
901 grub_env_get = pGrubParam->grub_env_get;
902 pEnv = grub_env_get("VTOY_CHKDEV_RESULT_STRING");
903 if (!pEnv)
904 {
905 return EFI_INVALID_PARAMETER;
906 }
907
908 if (pEnv[0] != '0' || pEnv[1] != 0)
909 {
910 ventoy_warn_invalid_device();
911 return EFI_INVALID_PARAMETER;
912 }
913
914 g_file_replace_list = &pGrubParam->file_replace;
915 old_cnt = g_file_replace_list->old_file_cnt;
916 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
917 g_file_replace_list->magic,
918 g_file_replace_list->new_file_virtual_id,
919 old_cnt,
920 old_cnt > 0 ? g_file_replace_list->old_file_name[0] : "",
921 old_cnt > 1 ? g_file_replace_list->old_file_name[1] : "",
922 old_cnt > 2 ? g_file_replace_list->old_file_name[2] : "",
923 old_cnt > 3 ? g_file_replace_list->old_file_name[3] : ""
924 );
925
926 g_img_replace_list = &pGrubParam->img_replace;
927 ventoy_proc_img_replace_name(g_img_replace_list);
928 old_cnt = g_img_replace_list->old_file_cnt;
929 debug("img replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
930 g_img_replace_list->magic,
931 g_img_replace_list->new_file_virtual_id,
932 old_cnt,
933 old_cnt > 0 ? g_img_replace_list->old_file_name[0] : "",
934 old_cnt > 1 ? g_img_replace_list->old_file_name[1] : "",
935 old_cnt > 2 ? g_img_replace_list->old_file_name[2] : "",
936 old_cnt > 3 ? g_img_replace_list->old_file_name[3] : ""
937 );
938
939 pPos = StrStr(pCmdLine, L"mem:");
940 chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
941
942 pPos = StrStr(pPos, L"size:");
943 size = StrDecimalToUintn(pPos + 5);
944
945 debug("memory addr:%p size:%lu", chain, size);
946
947 if (StrStr(pCmdLine, L"sector512"))
948 {
949 gSector512Mode = TRUE;
950 }
951
952 if (StrStr(pCmdLine, L"memdisk"))
953 {
954 g_iso_data_buf = (UINT8 *)chain + sizeof(ventoy_chain_head);
955 g_iso_buf_size = size - sizeof(ventoy_chain_head);
956 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size);
957
958 g_chain = chain;
959 gMemdiskMode = TRUE;
960 }
961 else
962 {
963 debug("This is normal mode");
964 g_chain = AllocatePool(size);
965 CopyMem(g_chain, chain, size);
966
967 g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
968 g_img_chunk_num = g_chain->img_chunk_num;
969 g_override_chunk = (ventoy_override_chunk *)((char *)g_chain + g_chain->override_chunk_offset);
970 g_override_chunk_num = g_chain->override_chunk_num;
971 g_virt_chunk = (ventoy_virt_chunk *)((char *)g_chain + g_chain->virt_chunk_offset);
972 g_virt_chunk_num = g_chain->virt_chunk_num;
973
974 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
975
976 /* Workaround for Windows & ISO9660 */
977 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[3] == 0)
978 {
979 g_fixup_iso9660_secover_enable = TRUE;
980 }
981
982 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[4] != 1)
983 {
984 g_hook_keyboard = TRUE;
985 }
986
987 if (g_os_param_reserved[5] == 1 && g_os_param_reserved[2] == ventoy_chain_linux)
988 {
989 ventoy_disable_secure_boot(ImageHandle);
990 }
991
992 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable, g_hook_keyboard);
993
994 for (i = 0; i < sizeof(ventoy_os_param); i++)
995 {
996 chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
997 }
998
999 if (gDebugPrint)
1000 {
1001 debug("os param checksum: 0x%x %a", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
1002 }
1003
1004 ventoy_update_image_location(&(g_chain->os_param));
1005
1006 if (gDebugPrint)
1007 {
1008 ventoy_dump_chain(g_chain);
1009 }
1010 }
1011
1012 g_fix_windows_1st_cdrom_issue = FALSE;
1013 if (ventoy_chain_windows == g_os_param_reserved[2] ||
1014 ventoy_chain_wim == g_os_param_reserved[2])
1015 {
1016 if (ventoy_is_cdrom_dp_exist())
1017 {
1018 debug("fixup the 1st cdrom influences when boot windows ...");
1019 g_fix_windows_1st_cdrom_issue = TRUE;
1020 }
1021 }
1022
1023 ventoy_debug_pause();
1024
1025 FreePool(pCmdLine);
1026 return EFI_SUCCESS;
1027 }
1028
1029 EFI_STATUS EFIAPI ventoy_clean_env(VOID)
1030 {
1031 FreePool(g_sector_flag);
1032 g_sector_flag_num = 0;
1033
1034 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1035 {
1036 gBS->UnloadImage(gBlockData.IsoDriverImage);
1037 }
1038
1039 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1040
1041 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1042 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1043 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1044 NULL);
1045
1046 ventoy_delete_variable();
1047
1048 if (g_vtoy_img_location_buf)
1049 {
1050 FreePool(g_vtoy_img_location_buf);
1051 }
1052
1053 if (!gMemdiskMode)
1054 {
1055 FreePool(g_chain);
1056 }
1057
1058 return EFI_SUCCESS;
1059 }
1060
1061 STATIC EFI_STATUS ventoy_hook_start(VOID)
1062 {
1063 /* don't add debug print in this function */
1064
1065 if (g_fix_windows_1st_cdrom_issue)
1066 {
1067 ventoy_hook_1st_cdrom_start();
1068 }
1069
1070 /* let this the last */
1071 if (g_hook_keyboard)
1072 {
1073 ventoy_hook_keyboard_start();
1074 }
1075
1076 return EFI_SUCCESS;
1077 }
1078
1079 STATIC EFI_STATUS ventoy_hook_stop(VOID)
1080 {
1081 /* don't add debug print in this function */
1082
1083 if (g_fix_windows_1st_cdrom_issue)
1084 {
1085 ventoy_hook_1st_cdrom_stop();
1086 }
1087
1088 /* let this the last */
1089 if (g_hook_keyboard)
1090 {
1091 ventoy_hook_keyboard_stop();
1092 }
1093
1094 return EFI_SUCCESS;
1095 }
1096
1097 EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
1098 {
1099 UINTN t = 0;
1100 UINTN i = 0;
1101 UINTN j = 0;
1102 UINTN Find = 0;
1103 UINTN Count = 0;
1104 EFI_HANDLE Image = NULL;
1105 EFI_HANDLE *Handles = NULL;
1106 EFI_STATUS Status = EFI_SUCCESS;
1107 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
1108 EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
1109
1110 for (t = 0; t < 3; t++)
1111 {
1112 Count = 0;
1113 Handles = NULL;
1114
1115 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
1116 NULL, &Count, &Handles);
1117 if (EFI_ERROR(Status))
1118 {
1119 return Status;
1120 }
1121
1122 debug("ventoy_boot fs count:%u", Count);
1123
1124 for (i = 0; i < Count; i++)
1125 {
1126 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
1127 if (EFI_ERROR(Status))
1128 {
1129 continue;
1130 }
1131
1132 debug("FS:%u Protocol:%p OpenVolume:%p", i, pFile, pFile->OpenVolume);
1133
1134 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
1135 (VOID **)&pDevPath,
1136 ImageHandle,
1137 Handles[i],
1138 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1139 if (EFI_ERROR(Status))
1140 {
1141 debug("Failed to open device path protocol %r", Status);
1142 continue;
1143 }
1144
1145 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
1146 if (CompareMem(gBlockData.Path, pDevPath, gBlockData.DevicePathCompareLen))
1147 {
1148 debug("Not ventoy disk file system");
1149 continue;
1150 }
1151
1152 for (j = gBootFileStartIndex; j < ARRAY_SIZE(gEfiBootFileName); j++)
1153 {
1154 Status = ventoy_load_image(ImageHandle, pDevPath, gEfiBootFileName[j],
1155 StrSize(gEfiBootFileName[j]), &Image);
1156 if (EFI_SUCCESS == Status)
1157 {
1158 break;
1159 }
1160 debug("Failed to load image %r <%s>", Status, gEfiBootFileName[j]);
1161 }
1162
1163 if (j >= ARRAY_SIZE(gEfiBootFileName))
1164 {
1165 continue;
1166 }
1167
1168 Find++;
1169 debug("Find boot file, now try to boot .....");
1170 ventoy_debug_pause();
1171
1172 if (gDebugPrint)
1173 {
1174 gST->ConIn->Reset(gST->ConIn, FALSE);
1175 }
1176
1177 if ((g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC) ||
1178 (g_img_replace_list && g_img_replace_list->magic == GRUB_IMG_REPLACE_MAGIC))
1179 {
1180 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
1181 pFile->OpenVolume = ventoy_wrapper_open_volume;
1182 }
1183
1184 ventoy_hook_start();
1185 /* can't add debug print here */
1186 //ventoy_wrapper_system();
1187 Status = gBS->StartImage(Image, NULL, NULL);
1188 ventoy_hook_stop();
1189
1190 if (EFI_ERROR(Status))
1191 {
1192 debug("Failed to start image %r", Status);
1193 sleep(3);
1194 gBS->UnloadImage(Image);
1195 break;
1196 }
1197 }
1198
1199 FreePool(Handles);
1200
1201 if (Find == 0)
1202 {
1203 if (gDotEfiBoot)
1204 {
1205 break;
1206 }
1207
1208 debug("Fs not found, now wait and retry...");
1209 sleep(1);
1210 }
1211 }
1212
1213 if (Find == 0)
1214 {
1215 return EFI_NOT_FOUND;
1216 }
1217
1218 return EFI_SUCCESS;
1219 }
1220
1221 EFI_STATUS EFIAPI VentoyEfiMain
1222 (
1223 IN EFI_HANDLE ImageHandle,
1224 IN EFI_SYSTEM_TABLE *SystemTable
1225 )
1226 {
1227 EFI_STATUS Status = EFI_SUCCESS;
1228 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *Protocol;
1229
1230 g_sector_flag_num = 512; /* initial value */
1231
1232 g_sector_flag = AllocatePool(g_sector_flag_num * sizeof(ventoy_sector_flag));
1233 if (NULL == g_sector_flag)
1234 {
1235 return EFI_OUT_OF_RESOURCES;
1236 }
1237
1238 Status = gBS->HandleProtocol(gST->ConsoleInHandle, &gEfiSimpleTextInputExProtocolGuid, (VOID **)&Protocol);
1239 if (EFI_SUCCESS == Status)
1240 {
1241 g_con_simple_input_ex = Protocol;
1242 }
1243
1244 gST->ConOut->ClearScreen(gST->ConOut);
1245 ventoy_clear_input();
1246
1247 Status = ventoy_parse_cmdline(ImageHandle);
1248 if (EFI_ERROR(Status))
1249 {
1250 return Status;
1251 }
1252
1253 ventoy_disable_ex_filesystem();
1254
1255 if (gMemdiskMode)
1256 {
1257 g_ramdisk_param.PhyAddr = (UINT64)(UINTN)g_iso_data_buf;
1258 g_ramdisk_param.DiskSize = (UINT64)g_iso_buf_size;
1259
1260 ventoy_save_ramdisk_param();
1261
1262 if (gLoadIsoEfi)
1263 {
1264 ventoy_find_iso_disk(ImageHandle);
1265 ventoy_find_iso_disk_fs(ImageHandle);
1266 ventoy_load_isoefi_driver(ImageHandle);
1267 }
1268
1269 ventoy_install_blockio(ImageHandle, g_iso_buf_size);
1270 ventoy_debug_pause();
1271
1272 Status = ventoy_boot(ImageHandle);
1273
1274 ventoy_delete_ramdisk_param();
1275
1276 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1277 {
1278 gBS->UnloadImage(gBlockData.IsoDriverImage);
1279 }
1280
1281 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1282 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1283 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1284 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1285 NULL);
1286 }
1287 else
1288 {
1289 ventoy_save_variable();
1290 Status = ventoy_find_iso_disk(ImageHandle);
1291 if (!EFI_ERROR(Status))
1292 {
1293 if (gLoadIsoEfi)
1294 {
1295 ventoy_find_iso_disk_fs(ImageHandle);
1296 ventoy_load_isoefi_driver(ImageHandle);
1297 }
1298
1299 ventoy_debug_pause();
1300
1301 ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
1302
1303 ventoy_debug_pause();
1304
1305 Status = ventoy_boot(ImageHandle);
1306 }
1307
1308 ventoy_clean_env();
1309 }
1310
1311 if (FALSE == gDotEfiBoot && FALSE == gBootFallBack)
1312 {
1313 if (EFI_NOT_FOUND == Status)
1314 {
1315 gST->ConOut->OutputString(gST->ConOut, L"No bootfile found for UEFI!\r\n");
1316 gST->ConOut->OutputString(gST->ConOut, L"Maybe the image does not support " VENTOY_UEFI_DESC L"!\r\n");
1317 sleep(30);
1318 }
1319 }
1320
1321 ventoy_clear_input();
1322 gST->ConOut->ClearScreen(gST->ConOut);
1323
1324 if (gDotEfiBoot && (EFI_NOT_FOUND == Status))
1325 {
1326 grub_env_set("vtoy_dotefi_retry", "YES");
1327 }
1328
1329 ventoy_enable_ex_filesystem();
1330
1331 return EFI_SUCCESS;
1332 }
1333