]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.c
Support Stratodesk NoTouch OS (#1652)
[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 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
833 Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
834 if (EFI_ERROR(Status))
835 {
836 VtoyDebug("Failed to handle load image protocol %r", Status);
837 return Status;
838 }
839
840 pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
841 SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
842 CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
843
844 if (StrStr(pCmdLine, L"debug"))
845 {
846 gDebugPrint = TRUE;
847 }
848
849 if (StrStr(pCmdLine, L"fallback"))
850 {
851 gBootFallBack = TRUE;
852 }
853
854 if (StrStr(pCmdLine, L"dotefi"))
855 {
856 gDotEfiBoot = TRUE;
857 }
858
859 if (StrStr(pCmdLine, L"isoefi=on"))
860 {
861 gLoadIsoEfi = TRUE;
862 }
863
864 if (StrStr(pCmdLine, L"iso_udf"))
865 {
866 gIsoUdf = TRUE;
867 }
868
869 pPos = StrStr(pCmdLine, L"FirstTry=@");
870 if (pPos)
871 {
872 pPos += StrLen(L"FirstTry=");
873 for (i = 0; i < ARRAY_SIZE(gFirstTryBootFile); i++, pPos++)
874 {
875 if (*pPos != L' ' && *pPos != L'\t' && *pPos)
876 {
877 gFirstTryBootFile[i] = (*pPos == '@') ? '\\' : *pPos;
878 }
879 else
880 {
881 break;
882 }
883 }
884
885 gEfiBootFileName[0] = gFirstTryBootFile;
886 gBootFileStartIndex = 0;
887 }
888
889 debug("cmdline:<%s>", pCmdLine);
890
891 if (gFirstTryBootFile[0])
892 {
893 debug("First Try:<%s>", gFirstTryBootFile);
894 }
895
896 pPos = StrStr(pCmdLine, L"env_param=");
897 if (!pPos)
898 {
899 return EFI_INVALID_PARAMETER;
900 }
901
902 pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
903 grub_env_set = pGrubParam->grub_env_set;
904 grub_env_get = pGrubParam->grub_env_get;
905 pEnv = grub_env_get("VTOY_CHKDEV_RESULT_STRING");
906 if (!pEnv)
907 {
908 return EFI_INVALID_PARAMETER;
909 }
910
911 if (pEnv[0] != '0' || pEnv[1] != 0)
912 {
913 ventoy_warn_invalid_device();
914 return EFI_INVALID_PARAMETER;
915 }
916
917 g_file_replace_list = &pGrubParam->file_replace;
918 old_cnt = g_file_replace_list->old_file_cnt;
919 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
920 g_file_replace_list->magic,
921 g_file_replace_list->new_file_virtual_id,
922 old_cnt,
923 old_cnt > 0 ? g_file_replace_list->old_file_name[0] : "",
924 old_cnt > 1 ? g_file_replace_list->old_file_name[1] : "",
925 old_cnt > 2 ? g_file_replace_list->old_file_name[2] : "",
926 old_cnt > 3 ? g_file_replace_list->old_file_name[3] : ""
927 );
928
929 g_img_replace_list = &pGrubParam->img_replace;
930 ventoy_proc_img_replace_name(g_img_replace_list);
931 old_cnt = g_img_replace_list->old_file_cnt;
932 debug("img replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
933 g_img_replace_list->magic,
934 g_img_replace_list->new_file_virtual_id,
935 old_cnt,
936 old_cnt > 0 ? g_img_replace_list->old_file_name[0] : "",
937 old_cnt > 1 ? g_img_replace_list->old_file_name[1] : "",
938 old_cnt > 2 ? g_img_replace_list->old_file_name[2] : "",
939 old_cnt > 3 ? g_img_replace_list->old_file_name[3] : ""
940 );
941
942 pPos = StrStr(pCmdLine, L"mem:");
943 chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
944
945 pPos = StrStr(pPos, L"size:");
946 size = StrDecimalToUintn(pPos + 5);
947
948 debug("memory addr:%p size:%lu", chain, size);
949
950 if (StrStr(pCmdLine, L"sector512"))
951 {
952 gSector512Mode = TRUE;
953 }
954
955 if (StrStr(pCmdLine, L"memdisk"))
956 {
957 g_iso_data_buf = (UINT8 *)chain + sizeof(ventoy_chain_head);
958 g_iso_buf_size = size - sizeof(ventoy_chain_head);
959 debug("memdisk mode iso_buf_size:%u", g_iso_buf_size);
960
961 g_chain = chain;
962 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
963 gMemdiskMode = TRUE;
964 }
965 else
966 {
967 debug("This is normal mode");
968 g_chain = AllocatePool(size);
969 CopyMem(g_chain, chain, size);
970
971 g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
972 g_img_chunk_num = g_chain->img_chunk_num;
973 g_override_chunk = (ventoy_override_chunk *)((char *)g_chain + g_chain->override_chunk_offset);
974 g_override_chunk_num = g_chain->override_chunk_num;
975 g_virt_chunk = (ventoy_virt_chunk *)((char *)g_chain + g_chain->virt_chunk_offset);
976 g_virt_chunk_num = g_chain->virt_chunk_num;
977
978 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
979
980 /* Workaround for Windows & ISO9660 */
981 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[3] == 0)
982 {
983 g_fixup_iso9660_secover_enable = TRUE;
984 }
985
986 if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[4] != 1)
987 {
988 g_hook_keyboard = TRUE;
989 }
990
991 if (g_os_param_reserved[5] == 1 && g_os_param_reserved[2] == ventoy_chain_linux)
992 {
993 ventoy_disable_secure_boot(ImageHandle);
994 }
995
996 debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable, g_hook_keyboard);
997
998 for (i = 0; i < sizeof(ventoy_os_param); i++)
999 {
1000 chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
1001 }
1002
1003 if (gDebugPrint)
1004 {
1005 debug("os param checksum: 0x%x %a", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
1006 }
1007
1008 ventoy_update_image_location(&(g_chain->os_param));
1009
1010 if (gDebugPrint)
1011 {
1012 ventoy_dump_chain(g_chain);
1013 }
1014 }
1015
1016 g_fix_windows_1st_cdrom_issue = FALSE;
1017 if (ventoy_chain_windows == g_os_param_reserved[2] ||
1018 ventoy_chain_wim == g_os_param_reserved[2])
1019 {
1020 if (ventoy_is_cdrom_dp_exist())
1021 {
1022 debug("fixup the 1st cdrom influences when boot windows ...");
1023 g_fix_windows_1st_cdrom_issue = TRUE;
1024 }
1025 }
1026
1027 ventoy_debug_pause();
1028
1029 FreePool(pCmdLine);
1030 return EFI_SUCCESS;
1031 }
1032
1033 EFI_STATUS EFIAPI ventoy_clean_env(VOID)
1034 {
1035 FreePool(g_sector_flag);
1036 g_sector_flag_num = 0;
1037
1038 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1039 {
1040 gBS->UnloadImage(gBlockData.IsoDriverImage);
1041 }
1042
1043 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1044
1045 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1046 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1047 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1048 NULL);
1049
1050 ventoy_delete_variable();
1051
1052 if (g_vtoy_img_location_buf)
1053 {
1054 FreePool(g_vtoy_img_location_buf);
1055 }
1056
1057 if (!gMemdiskMode)
1058 {
1059 FreePool(g_chain);
1060 }
1061
1062 return EFI_SUCCESS;
1063 }
1064
1065 STATIC EFI_STATUS ventoy_hook_start(VOID)
1066 {
1067 /* don't add debug print in this function */
1068
1069 if (g_fix_windows_1st_cdrom_issue)
1070 {
1071 ventoy_hook_1st_cdrom_start();
1072 }
1073
1074 /* let this the last */
1075 if (g_hook_keyboard)
1076 {
1077 ventoy_hook_keyboard_start();
1078 }
1079
1080 return EFI_SUCCESS;
1081 }
1082
1083 STATIC EFI_STATUS ventoy_hook_stop(VOID)
1084 {
1085 /* don't add debug print in this function */
1086
1087 if (g_fix_windows_1st_cdrom_issue)
1088 {
1089 ventoy_hook_1st_cdrom_stop();
1090 }
1091
1092 /* let this the last */
1093 if (g_hook_keyboard)
1094 {
1095 ventoy_hook_keyboard_stop();
1096 }
1097
1098 return EFI_SUCCESS;
1099 }
1100
1101 EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
1102 {
1103 UINTN t = 0;
1104 UINTN i = 0;
1105 UINTN j = 0;
1106 UINTN Find = 0;
1107 UINTN Count = 0;
1108 EFI_HANDLE Image = NULL;
1109 EFI_HANDLE *Handles = NULL;
1110 EFI_STATUS Status = EFI_SUCCESS;
1111 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
1112 EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
1113
1114 for (t = 0; t < 3; t++)
1115 {
1116 Count = 0;
1117 Handles = NULL;
1118
1119 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
1120 NULL, &Count, &Handles);
1121 if (EFI_ERROR(Status))
1122 {
1123 return Status;
1124 }
1125
1126 debug("ventoy_boot fs count:%u", Count);
1127
1128 for (i = 0; i < Count; i++)
1129 {
1130 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
1131 if (EFI_ERROR(Status))
1132 {
1133 continue;
1134 }
1135
1136 debug("FS:%u Protocol:%p OpenVolume:%p", i, pFile, pFile->OpenVolume);
1137
1138 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
1139 (VOID **)&pDevPath,
1140 ImageHandle,
1141 Handles[i],
1142 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1143 if (EFI_ERROR(Status))
1144 {
1145 debug("Failed to open device path protocol %r", Status);
1146 continue;
1147 }
1148
1149 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
1150 if (CompareMem(gBlockData.Path, pDevPath, gBlockData.DevicePathCompareLen))
1151 {
1152 debug("Not ventoy disk file system");
1153 continue;
1154 }
1155
1156 for (j = gBootFileStartIndex; j < ARRAY_SIZE(gEfiBootFileName); j++)
1157 {
1158 Status = ventoy_load_image(ImageHandle, pDevPath, gEfiBootFileName[j],
1159 StrSize(gEfiBootFileName[j]), &Image);
1160 if (EFI_SUCCESS == Status)
1161 {
1162 break;
1163 }
1164 debug("Failed to load image %r <%s>", Status, gEfiBootFileName[j]);
1165 }
1166
1167 if (j >= ARRAY_SIZE(gEfiBootFileName))
1168 {
1169 continue;
1170 }
1171
1172 Find++;
1173 debug("Find boot file, now try to boot .....");
1174 ventoy_debug_pause();
1175
1176 if (gDebugPrint)
1177 {
1178 gST->ConIn->Reset(gST->ConIn, FALSE);
1179 }
1180
1181 if ((g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC) ||
1182 (g_img_replace_list && g_img_replace_list->magic == GRUB_IMG_REPLACE_MAGIC))
1183 {
1184 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
1185 pFile->OpenVolume = ventoy_wrapper_open_volume;
1186 }
1187
1188 ventoy_hook_start();
1189 /* can't add debug print here */
1190 //ventoy_wrapper_system();
1191 Status = gBS->StartImage(Image, NULL, NULL);
1192 ventoy_hook_stop();
1193
1194 if (EFI_ERROR(Status))
1195 {
1196 debug("Failed to start image %r", Status);
1197 sleep(3);
1198 gBS->UnloadImage(Image);
1199 break;
1200 }
1201 }
1202
1203 FreePool(Handles);
1204
1205 if (Find == 0)
1206 {
1207 if (gDotEfiBoot)
1208 {
1209 break;
1210 }
1211
1212 debug("Fs not found, now wait and retry...");
1213 sleep(1);
1214 }
1215 }
1216
1217 if (Find == 0)
1218 {
1219 return EFI_NOT_FOUND;
1220 }
1221
1222 return EFI_SUCCESS;
1223 }
1224
1225 EFI_STATUS EFIAPI VentoyEfiMain
1226 (
1227 IN EFI_HANDLE ImageHandle,
1228 IN EFI_SYSTEM_TABLE *SystemTable
1229 )
1230 {
1231 EFI_STATUS Status = EFI_SUCCESS;
1232 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *Protocol;
1233
1234 g_sector_flag_num = 512; /* initial value */
1235
1236 g_sector_flag = AllocatePool(g_sector_flag_num * sizeof(ventoy_sector_flag));
1237 if (NULL == g_sector_flag)
1238 {
1239 return EFI_OUT_OF_RESOURCES;
1240 }
1241
1242 Status = gBS->HandleProtocol(gST->ConsoleInHandle, &gEfiSimpleTextInputExProtocolGuid, (VOID **)&Protocol);
1243 if (EFI_SUCCESS == Status)
1244 {
1245 g_con_simple_input_ex = Protocol;
1246 }
1247
1248 gST->ConOut->ClearScreen(gST->ConOut);
1249 ventoy_clear_input();
1250
1251 Status = ventoy_parse_cmdline(ImageHandle);
1252 if (EFI_ERROR(Status))
1253 {
1254 return Status;
1255 }
1256
1257 ventoy_disable_ex_filesystem();
1258
1259 if (gMemdiskMode)
1260 {
1261 g_ramdisk_param.PhyAddr = (UINT64)(UINTN)g_iso_data_buf;
1262 g_ramdisk_param.DiskSize = (UINT64)g_iso_buf_size;
1263
1264 ventoy_save_ramdisk_param();
1265
1266 if (gLoadIsoEfi)
1267 {
1268 ventoy_find_iso_disk(ImageHandle);
1269 ventoy_find_iso_disk_fs(ImageHandle);
1270 ventoy_load_isoefi_driver(ImageHandle);
1271 }
1272
1273 ventoy_install_blockio(ImageHandle, g_iso_buf_size);
1274 ventoy_debug_pause();
1275
1276 Status = ventoy_boot(ImageHandle);
1277
1278 ventoy_delete_ramdisk_param();
1279
1280 if (gLoadIsoEfi && gBlockData.IsoDriverImage)
1281 {
1282 gBS->UnloadImage(gBlockData.IsoDriverImage);
1283 }
1284
1285 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
1286 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
1287 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
1288 &gEfiDevicePathProtocolGuid, gBlockData.Path,
1289 NULL);
1290 }
1291 else
1292 {
1293 ventoy_save_variable();
1294 Status = ventoy_find_iso_disk(ImageHandle);
1295 if (!EFI_ERROR(Status))
1296 {
1297 if (gLoadIsoEfi)
1298 {
1299 ventoy_find_iso_disk_fs(ImageHandle);
1300 ventoy_load_isoefi_driver(ImageHandle);
1301 }
1302
1303 ventoy_debug_pause();
1304
1305 ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
1306
1307 ventoy_debug_pause();
1308
1309 Status = ventoy_boot(ImageHandle);
1310 }
1311
1312 ventoy_clean_env();
1313 }
1314
1315 if (FALSE == gDotEfiBoot && FALSE == gBootFallBack)
1316 {
1317 if (EFI_NOT_FOUND == Status)
1318 {
1319 gST->ConOut->OutputString(gST->ConOut, L"No bootfile found for UEFI!\r\n");
1320 gST->ConOut->OutputString(gST->ConOut, L"Maybe the image does not support " VENTOY_UEFI_DESC L"!\r\n");
1321 sleep(30);
1322 }
1323 }
1324
1325 ventoy_clear_input();
1326 gST->ConOut->ClearScreen(gST->ConOut);
1327
1328 if (gDotEfiBoot && (EFI_NOT_FOUND == Status))
1329 {
1330 grub_env_set("vtoy_dotefi_retry", "YES");
1331 }
1332
1333 ventoy_enable_ex_filesystem();
1334
1335 return EFI_SUCCESS;
1336 }
1337