]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.c
1.0.13 release
[Ventoy.git] / EDK2 / edk2_mod / edk2-edk2-stable201911 / MdeModulePkg / Application / Ventoy / Ventoy.c
1 /******************************************************************************
2 * Ventoy.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <Uefi.h>
22 #include <Library/DebugLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DevicePathLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiApplicationEntryPoint.h>
31 #include <Protocol/LoadedImage.h>
32 #include <Guid/FileInfo.h>
33 #include <Guid/FileSystemInfo.h>
34 #include <Protocol/BlockIo.h>
35 #include <Protocol/RamDisk.h>
36 #include <Protocol/SimpleFileSystem.h>
37 #include <Ventoy.h>
38
39 BOOLEAN gDebugPrint = FALSE;
40 ventoy_ram_disk g_ramdisk_param;
41 ventoy_chain_head *g_chain;
42 ventoy_img_chunk *g_chunk;
43 UINT8 *g_os_param_reserved;
44 UINT32 g_img_chunk_num;
45 ventoy_override_chunk *g_override_chunk;
46 UINT32 g_override_chunk_num;
47 ventoy_virt_chunk *g_virt_chunk;
48 UINT32 g_virt_chunk_num;
49 vtoy_block_data gBlockData;
50 static grub_env_get_pf grub_env_get = NULL;
51
52 ventoy_grub_param_file_replace *g_file_replace_list = NULL;
53 ventoy_efi_file_replace g_efi_file_replace;
54
55 CHAR16 gFirstTryBootFile[256] = {0};
56
57 /* Boot filename */
58 UINTN gBootFileStartIndex = 1;
59 CONST CHAR16 *gEfiBootFileName[] =
60 {
61 L"@",
62 EFI_REMOVABLE_MEDIA_FILE_NAME,
63 L"\\EFI\\BOOT\\GRUBX64.EFI",
64 L"\\EFI\\BOOT\\BOOTx64.EFI",
65 L"\\EFI\\BOOT\\bootx64.efi",
66 L"\\efi\\boot\\bootx64.efi",
67 };
68
69 VOID EFIAPI VtoyDebug(IN CONST CHAR8 *Format, ...)
70 {
71 VA_LIST Marker;
72 CHAR16 Buffer[512];
73
74 VA_START (Marker, Format);
75 UnicodeVSPrintAsciiFormat(Buffer, sizeof(Buffer), Format, Marker);
76 VA_END (Marker);
77
78 gST->ConOut->OutputString(gST->ConOut, Buffer);
79 }
80
81 VOID EFIAPI ventoy_clear_input(VOID)
82 {
83 EFI_INPUT_KEY Key;
84
85 gST->ConIn->Reset(gST->ConIn, FALSE);
86 while (EFI_SUCCESS == gST->ConIn->ReadKeyStroke(gST->ConIn, &Key))
87 {
88 ;
89 }
90 gST->ConIn->Reset(gST->ConIn, FALSE);
91 }
92
93 static void EFIAPI ventoy_dump_img_chunk(ventoy_chain_head *chain)
94 {
95 UINT32 i;
96 int errcnt = 0;
97 UINT64 img_sec = 0;
98 ventoy_img_chunk *chunk;
99
100 chunk = (ventoy_img_chunk *)((char *)chain + chain->img_chunk_offset);
101
102 debug("##################### ventoy_dump_img_chunk #######################");
103
104 for (i = 0; i < chain->img_chunk_num; i++)
105 {
106 debug("%2u: [ %u - %u ] <==> [ %llu - %llu ]",
107 i, chunk[i].img_start_sector, chunk[i].img_end_sector,
108 chunk[i].disk_start_sector, chunk[i].disk_end_sector);
109
110 if (i > 0 && (chunk[i].img_start_sector != chunk[i - 1].img_end_sector + 1))
111 {
112 errcnt++;
113 }
114
115 img_sec += chunk[i].img_end_sector - chunk[i].img_start_sector + 1;
116 }
117
118 if (errcnt == 0 && (img_sec * 2048 == g_chain->real_img_size_in_bytes))
119 {
120 debug("image chunk size check success");
121 }
122 else
123 {
124 debug("image chunk size check failed %d", errcnt);
125 }
126
127 ventoy_debug_pause();
128 }
129
130 static void EFIAPI ventoy_dump_override_chunk(ventoy_chain_head *chain)
131 {
132 UINT32 i;
133 ventoy_override_chunk *chunk;
134
135 chunk = (ventoy_override_chunk *)((char *)chain + chain->override_chunk_offset);
136
137 debug("##################### ventoy_dump_override_chunk #######################");
138
139 for (i = 0; i < g_override_chunk_num; i++)
140 {
141 debug("%2u: [ %llu, %u ]", i, chunk[i].img_offset, chunk[i].override_size);
142 }
143
144 ventoy_debug_pause();
145 }
146
147 static void EFIAPI ventoy_dump_virt_chunk(ventoy_chain_head *chain)
148 {
149 UINT32 i;
150 ventoy_virt_chunk *node;
151
152 debug("##################### ventoy_dump_virt_chunk #######################");
153 debug("virt_chunk_offset=%u", chain->virt_chunk_offset);
154 debug("virt_chunk_num=%u", chain->virt_chunk_num);
155
156 node = (ventoy_virt_chunk *)((char *)chain + chain->virt_chunk_offset);
157 for (i = 0; i < chain->virt_chunk_num; i++, node++)
158 {
159 debug("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]", i,
160 node->mem_sector_start,
161 node->mem_sector_end,
162 node->mem_sector_offset,
163 node->remap_sector_start,
164 node->remap_sector_end,
165 node->org_sector_start);
166 }
167
168 ventoy_debug_pause();
169 }
170
171 static void EFIAPI ventoy_dump_chain(ventoy_chain_head *chain)
172 {
173 UINT32 i = 0;
174 UINT8 chksum = 0;
175 UINT8 *guid;
176
177 guid = chain->os_param.vtoy_disk_guid;
178 for (i = 0; i < sizeof(ventoy_os_param); i++)
179 {
180 chksum += *((UINT8 *)(&(chain->os_param)) + i);
181 }
182
183 debug("##################### ventoy_dump_chain #######################");
184
185 debug("os_param->chksum=0x%x (%a)", chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
186 debug("os_param->vtoy_disk_guid=%02x%02x%02x%02x", guid[0], guid[1], guid[2], guid[3]);
187 debug("os_param->vtoy_disk_size=%llu", chain->os_param.vtoy_disk_size);
188 debug("os_param->vtoy_disk_part_id=%u", chain->os_param.vtoy_disk_part_id);
189 debug("os_param->vtoy_disk_part_type=%u", chain->os_param.vtoy_disk_part_type);
190 debug("os_param->vtoy_img_path=<%a>", chain->os_param.vtoy_img_path);
191 debug("os_param->vtoy_img_size=<%llu>", chain->os_param.vtoy_img_size);
192 debug("os_param->vtoy_img_location_addr=<0x%llx>", chain->os_param.vtoy_img_location_addr);
193 debug("os_param->vtoy_img_location_len=<%u>", chain->os_param.vtoy_img_location_len);
194 debug("os_param->vtoy_reserved=<%u %u %u %u>",
195 g_os_param_reserved[0],
196 g_os_param_reserved[1],
197 g_os_param_reserved[2],
198 g_os_param_reserved[3]
199 );
200
201 ventoy_debug_pause();
202
203 debug("chain->disk_drive=0x%x", chain->disk_drive);
204 debug("chain->disk_sector_size=%u", chain->disk_sector_size);
205 debug("chain->real_img_size_in_bytes=%llu", chain->real_img_size_in_bytes);
206 debug("chain->virt_img_size_in_bytes=%llu", chain->virt_img_size_in_bytes);
207 debug("chain->boot_catalog=%u", chain->boot_catalog);
208 debug("chain->img_chunk_offset=%u", chain->img_chunk_offset);
209 debug("chain->img_chunk_num=%u", chain->img_chunk_num);
210 debug("chain->override_chunk_offset=%u", chain->override_chunk_offset);
211 debug("chain->override_chunk_num=%u", chain->override_chunk_num);
212
213 ventoy_debug_pause();
214
215 ventoy_dump_img_chunk(chain);
216 ventoy_dump_override_chunk(chain);
217 ventoy_dump_virt_chunk(chain);
218 }
219
220 static int ventoy_update_image_location(ventoy_os_param *param)
221 {
222 EFI_STATUS Status = EFI_SUCCESS;
223 UINT8 chksum = 0;
224 unsigned int i;
225 unsigned int length;
226 UINTN address = 0;
227 void *buffer = NULL;
228 ventoy_image_location *location = NULL;
229 ventoy_image_disk_region *region = NULL;
230 ventoy_img_chunk *chunk = g_chunk;
231
232 length = sizeof(ventoy_image_location) + (g_img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
233
234 Status = gBS->AllocatePool(EfiRuntimeServicesData, length + 4096 * 2, &buffer);
235 if (EFI_ERROR(Status) || NULL == buffer)
236 {
237 debug("Failed to allocate runtime pool %r\n", Status);
238 return 1;
239 }
240
241 address = (UINTN)buffer;
242
243 if (address % 4096)
244 {
245 address += 4096 - (address % 4096);
246 }
247
248 param->chksum = 0;
249 param->vtoy_img_location_addr = address;
250 param->vtoy_img_location_len = length;
251
252 /* update check sum */
253 for (i = 0; i < sizeof(ventoy_os_param); i++)
254 {
255 chksum += *((UINT8 *)param + i);
256 }
257 param->chksum = (chksum == 0) ? 0 : (UINT8)(0x100 - chksum);
258
259 location = (ventoy_image_location *)(unsigned long)(param->vtoy_img_location_addr);
260 if (NULL == location)
261 {
262 return 0;
263 }
264
265 CopyMem(&location->guid, &param->guid, sizeof(ventoy_guid));
266 location->image_sector_size = 2048;
267 location->disk_sector_size = g_chain->disk_sector_size;
268 location->region_count = g_img_chunk_num;
269
270 region = location->regions;
271
272 for (i = 0; i < g_img_chunk_num; i++)
273 {
274 region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
275 region->image_start_sector = chunk->img_start_sector;
276 region->disk_start_sector = chunk->disk_start_sector;
277 region++;
278 chunk++;
279 }
280
281 return 0;
282 }
283
284 EFI_HANDLE EFIAPI ventoy_get_parent_handle(IN EFI_DEVICE_PATH_PROTOCOL *pDevPath)
285 {
286 EFI_HANDLE Handle = NULL;
287 EFI_STATUS Status = EFI_SUCCESS;
288 EFI_DEVICE_PATH_PROTOCOL *pLastNode = NULL;
289 EFI_DEVICE_PATH_PROTOCOL *pCurNode = NULL;
290 EFI_DEVICE_PATH_PROTOCOL *pTmpDevPath = NULL;
291
292 pTmpDevPath = DuplicateDevicePath(pDevPath);
293 if (!pTmpDevPath)
294 {
295 return NULL;
296 }
297
298 pCurNode = pTmpDevPath;
299 while (!IsDevicePathEnd(pCurNode))
300 {
301 pLastNode = pCurNode;
302 pCurNode = NextDevicePathNode(pCurNode);
303 }
304 if (pLastNode)
305 {
306 CopyMem(pLastNode, pCurNode, sizeof(EFI_DEVICE_PATH_PROTOCOL));
307 }
308
309 pCurNode = pTmpDevPath;
310 Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid, &pCurNode, &Handle);
311 debug("Status:%r Parent Handle:%p DP:%s", Status, Handle, ConvertDevicePathToText(pTmpDevPath, FALSE, FALSE));
312
313 FreePool(pTmpDevPath);
314
315 return Handle;
316 }
317
318 EFI_STATUS EFIAPI ventoy_save_ramdisk_param(VOID)
319 {
320 EFI_STATUS Status = EFI_SUCCESS;
321 EFI_GUID VarGuid = VENTOY_GUID;
322
323 Status = gRT->SetVariable(L"VentoyRamDisk", &VarGuid,
324 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
325 sizeof(g_ramdisk_param), &(g_ramdisk_param));
326 debug("set efi variable %r", Status);
327
328 return Status;
329 }
330
331 EFI_STATUS EFIAPI ventoy_delete_ramdisk_param(VOID)
332 {
333 EFI_STATUS Status = EFI_SUCCESS;
334 EFI_GUID VarGuid = VENTOY_GUID;
335
336 Status = gRT->SetVariable(L"VentoyRamDisk", &VarGuid,
337 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
338 0, NULL);
339 debug("delete efi variable %r", Status);
340
341 return Status;
342 }
343
344
345 EFI_STATUS EFIAPI ventoy_save_variable(VOID)
346 {
347 EFI_STATUS Status = EFI_SUCCESS;
348 EFI_GUID VarGuid = VENTOY_GUID;
349
350 Status = gRT->SetVariable(L"VentoyOsParam", &VarGuid,
351 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
352 sizeof(g_chain->os_param), &(g_chain->os_param));
353 debug("set efi variable %r", Status);
354
355 return Status;
356 }
357
358 EFI_STATUS EFIAPI ventoy_delete_variable(VOID)
359 {
360 EFI_STATUS Status = EFI_SUCCESS;
361 EFI_GUID VarGuid = VENTOY_GUID;
362
363 Status = gRT->SetVariable(L"VentoyOsParam", &VarGuid,
364 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
365 0, NULL);
366 debug("delete efi variable %r", Status);
367
368 return Status;
369 }
370
371 STATIC EFI_STATUS EFIAPI ventoy_load_image
372 (
373 IN EFI_HANDLE ImageHandle,
374 IN EFI_DEVICE_PATH_PROTOCOL *pDevicePath,
375 IN CONST CHAR16 *FileName,
376 IN UINTN FileNameLen,
377 OUT EFI_HANDLE *Image
378 )
379 {
380 EFI_STATUS Status = EFI_SUCCESS;
381 CHAR16 TmpBuf[256] = {0};
382 FILEPATH_DEVICE_PATH *pFilePath = NULL;
383 EFI_DEVICE_PATH_PROTOCOL *pImgPath = NULL;
384
385 pFilePath = (FILEPATH_DEVICE_PATH *)TmpBuf;
386 pFilePath->Header.Type = MEDIA_DEVICE_PATH;
387 pFilePath->Header.SubType = MEDIA_FILEPATH_DP;
388 pFilePath->Header.Length[0] = FileNameLen + sizeof(EFI_DEVICE_PATH_PROTOCOL);
389 pFilePath->Header.Length[1] = 0;
390 CopyMem(pFilePath->PathName, FileName, FileNameLen);
391
392 pImgPath = AppendDevicePathNode(pDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)pFilePath);
393 if (!pImgPath)
394 {
395 return EFI_NOT_FOUND;
396 }
397
398 Status = gBS->LoadImage(FALSE, ImageHandle, pImgPath, NULL, 0, Image);
399
400 debug("Load Image File %r DP: <%s>", Status, ConvertDevicePathToText(pImgPath, FALSE, FALSE));
401
402 FreePool(pImgPath);
403
404 return Status;
405 }
406
407
408 STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle)
409 {
410 UINTN i = 0;
411 UINTN Count = 0;
412 UINT64 DiskSize = 0;
413 UINT8 *pBuffer = NULL;
414 EFI_HANDLE *Handles;
415 EFI_STATUS Status = EFI_SUCCESS;
416 EFI_BLOCK_IO_PROTOCOL *pBlockIo;
417
418 pBuffer = AllocatePool(2048);
419 if (!pBuffer)
420 {
421 return EFI_OUT_OF_RESOURCES;
422 }
423
424 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiBlockIoProtocolGuid,
425 NULL, &Count, &Handles);
426 if (EFI_ERROR(Status))
427 {
428 FreePool(pBuffer);
429 return Status;
430 }
431
432 for (i = 0; i < Count; i++)
433 {
434 Status = gBS->HandleProtocol(Handles[i], &gEfiBlockIoProtocolGuid, (VOID **)&pBlockIo);
435 if (EFI_ERROR(Status))
436 {
437 continue;
438 }
439
440 DiskSize = (pBlockIo->Media->LastBlock + 1) * pBlockIo->Media->BlockSize;
441 debug("This Disk size: %llu", DiskSize);
442 if (g_chain->os_param.vtoy_disk_size != DiskSize)
443 {
444 continue;
445 }
446
447 Status = pBlockIo->ReadBlocks(pBlockIo, pBlockIo->Media->MediaId, 0, 512, pBuffer);
448 if (EFI_ERROR(Status))
449 {
450 debug("ReadBlocks filed %r", Status);
451 continue;
452 }
453
454 if (CompareMem(g_chain->os_param.vtoy_disk_guid, pBuffer + 0x180, 16) == 0)
455 {
456 gBlockData.RawBlockIoHandle = Handles[i];
457 gBlockData.pRawBlockIo = pBlockIo;
458 gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
459 (VOID **)&(gBlockData.pDiskDevPath),
460 ImageHandle,
461 Handles[i],
462 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
463
464 debug("Find Ventoy Disk Handle:%p DP:%s", Handles[i],
465 ConvertDevicePathToText(gBlockData.pDiskDevPath, FALSE, FALSE));
466 break;
467 }
468 }
469
470 FreePool(Handles);
471
472 if (i >= Count)
473 {
474 return EFI_NOT_FOUND;
475 }
476 else
477 {
478 return EFI_SUCCESS;
479 }
480 }
481
482 STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
483 {
484 UINT32 i = 0;
485 UINT32 old_cnt = 0;
486 UINTN size = 0;
487 UINT8 chksum = 0;
488 CHAR16 *pPos = NULL;
489 CHAR16 *pCmdLine = NULL;
490 EFI_STATUS Status = EFI_SUCCESS;
491 ventoy_grub_param *pGrubParam = NULL;
492 EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
493
494 Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
495 if (EFI_ERROR(Status))
496 {
497 VtoyDebug("Failed to handle load image protocol %r", Status);
498 return Status;
499 }
500
501 pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
502 SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
503 CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
504
505 if (StrStr(pCmdLine, L"debug"))
506 {
507 gDebugPrint = TRUE;
508 }
509
510 pPos = StrStr(pCmdLine, L"FirstTry=@");
511 if (pPos)
512 {
513 pPos += StrLen(L"FirstTry=");
514 for (i = 0; i < ARRAY_SIZE(gFirstTryBootFile); i++, pPos++)
515 {
516 if (*pPos != L' ' && *pPos != L'\t' && *pPos)
517 {
518 gFirstTryBootFile[i] = (*pPos == '@') ? '\\' : *pPos;
519 }
520 else
521 {
522 break;
523 }
524 }
525
526 gEfiBootFileName[0] = gFirstTryBootFile;
527 gBootFileStartIndex = 0;
528 }
529
530 debug("cmdline:<%s>", pCmdLine);
531
532 if (gFirstTryBootFile[0])
533 {
534 debug("First Try:<%s>", gFirstTryBootFile);
535 }
536
537 pPos = StrStr(pCmdLine, L"env_param=");
538 if (!pPos)
539 {
540 return EFI_INVALID_PARAMETER;
541 }
542
543 pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
544 grub_env_get = pGrubParam->grub_env_get;
545
546 g_file_replace_list = &pGrubParam->file_replace;
547 old_cnt = g_file_replace_list->old_file_cnt;
548 debug("file replace: magic:0x%x virtid:%u name count:%u <%a> <%a> <%a> <%a>",
549 g_file_replace_list->magic,
550 g_file_replace_list->new_file_virtual_id,
551 old_cnt,
552 old_cnt > 0 ? g_file_replace_list->old_file_name[0] : "",
553 old_cnt > 1 ? g_file_replace_list->old_file_name[1] : "",
554 old_cnt > 2 ? g_file_replace_list->old_file_name[2] : "",
555 old_cnt > 3 ? g_file_replace_list->old_file_name[3] : ""
556 );
557
558 pPos = StrStr(pCmdLine, L"mem:");
559 g_chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
560
561 pPos = StrStr(pPos, L"size:");
562 size = StrDecimalToUintn(pPos + 5);
563
564 debug("memory addr:%p size:%lu", g_chain, size);
565
566 if (StrStr(pCmdLine, L"memdisk"))
567 {
568 g_iso_buf_size = size;
569 gMemdiskMode = TRUE;
570 }
571 else
572 {
573 g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
574 g_img_chunk_num = g_chain->img_chunk_num;
575 g_override_chunk = (ventoy_override_chunk *)((char *)g_chain + g_chain->override_chunk_offset);
576 g_override_chunk_num = g_chain->override_chunk_num;
577 g_virt_chunk = (ventoy_virt_chunk *)((char *)g_chain + g_chain->virt_chunk_offset);
578 g_virt_chunk_num = g_chain->virt_chunk_num;
579
580 g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
581
582 /* Workaround for Windows & ISO9660 */
583 if (g_os_param_reserved[2] == 1 && g_os_param_reserved[3] == 0)
584 {
585 g_fixup_iso9660_secover_enable = TRUE;
586 }
587
588 for (i = 0; i < sizeof(ventoy_os_param); i++)
589 {
590 chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
591 }
592
593 if (gDebugPrint)
594 {
595 debug("os param checksum: 0x%x %a", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
596 }
597
598 ventoy_update_image_location(&(g_chain->os_param));
599
600 if (gDebugPrint)
601 {
602 ventoy_dump_chain(g_chain);
603 }
604 }
605
606 FreePool(pCmdLine);
607 return EFI_SUCCESS;
608 }
609
610 EFI_STATUS EFIAPI ventoy_clean_env(VOID)
611 {
612 FreePool(g_sector_flag);
613 g_sector_flag_num = 0;
614
615 gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
616
617 gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
618 &gEfiBlockIoProtocolGuid, &gBlockData.BlockIo,
619 &gEfiDevicePathProtocolGuid, gBlockData.Path,
620 NULL);
621
622 ventoy_delete_variable();
623
624 if (g_chain->os_param.vtoy_img_location_addr)
625 {
626 FreePool((VOID *)(UINTN)g_chain->os_param.vtoy_img_location_addr);
627 }
628
629 return EFI_SUCCESS;
630 }
631
632 EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
633 {
634 UINTN t = 0;
635 UINTN i = 0;
636 UINTN j = 0;
637 UINTN Find = 0;
638 UINTN Count = 0;
639 EFI_HANDLE Image = NULL;
640 EFI_HANDLE *Handles = NULL;
641 EFI_STATUS Status = EFI_SUCCESS;
642 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
643 EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
644
645 for (t = 0; t < 3; t++)
646 {
647 Count = 0;
648 Handles = NULL;
649
650 Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
651 NULL, &Count, &Handles);
652 if (EFI_ERROR(Status))
653 {
654 return Status;
655 }
656
657 debug("ventoy_boot fs count:%u", Count);
658
659 for (i = 0; i < Count; i++)
660 {
661 Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
662 if (EFI_ERROR(Status))
663 {
664 continue;
665 }
666
667 debug("FS:%u Protocol:%p OpenVolume:%p", i, pFile, pFile->OpenVolume);
668
669 Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
670 (VOID **)&pDevPath,
671 ImageHandle,
672 Handles[i],
673 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
674 if (EFI_ERROR(Status))
675 {
676 debug("Failed to open device path protocol %r", Status);
677 continue;
678 }
679
680 debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
681 if (CompareMem(gBlockData.Path, pDevPath, gBlockData.DevicePathCompareLen))
682 {
683 debug("Not ventoy disk file system");
684 continue;
685 }
686
687 for (j = gBootFileStartIndex; j < ARRAY_SIZE(gEfiBootFileName); j++)
688 {
689 Status = ventoy_load_image(ImageHandle, pDevPath, gEfiBootFileName[j],
690 StrSize(gEfiBootFileName[j]), &Image);
691 if (EFI_SUCCESS == Status)
692 {
693 break;
694 }
695 debug("Failed to load image %r <%s>", Status, gEfiBootFileName[j]);
696 }
697
698 if (j >= ARRAY_SIZE(gEfiBootFileName))
699 {
700 continue;
701 }
702
703 Find++;
704 debug("Find boot file, now try to boot .....");
705 ventoy_debug_pause();
706
707 if (gDebugPrint)
708 {
709 gST->ConIn->Reset(gST->ConIn, FALSE);
710 //ventoy_wrapper_system();
711 }
712
713 if (g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC)
714 {
715 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
716 pFile->OpenVolume = ventoy_wrapper_open_volume;
717 }
718
719 Status = gBS->StartImage(Image, NULL, NULL);
720 if (EFI_ERROR(Status))
721 {
722 debug("Failed to start image %r", Status);
723 sleep(3);
724 gBS->UnloadImage(Image);
725 break;
726 }
727 }
728
729 FreePool(Handles);
730
731 if (Find == 0)
732 {
733 debug("Fs not found, now wait and retry...");
734 sleep(2);
735 }
736 }
737
738 if (Find == 0)
739 {
740 return EFI_NOT_FOUND;
741 }
742
743 return EFI_SUCCESS;
744 }
745
746
747 EFI_STATUS EFIAPI VentoyEfiMain
748 (
749 IN EFI_HANDLE ImageHandle,
750 IN EFI_SYSTEM_TABLE *SystemTable
751 )
752 {
753 EFI_STATUS Status = EFI_SUCCESS;
754
755 g_sector_flag_num = 512; /* initial value */
756
757 g_sector_flag = AllocatePool(g_sector_flag_num * sizeof(ventoy_sector_flag));
758 if (NULL == g_sector_flag)
759 {
760 return EFI_OUT_OF_RESOURCES;
761 }
762
763 gST->ConOut->ClearScreen(gST->ConOut);
764 ventoy_clear_input();
765
766 ventoy_parse_cmdline(ImageHandle);
767
768 if (gMemdiskMode)
769 {
770 g_ramdisk_param.PhyAddr = (UINT64)(UINTN)g_chain;
771 g_ramdisk_param.DiskSize = (UINT64)g_iso_buf_size;
772
773 ventoy_save_ramdisk_param();
774
775 ventoy_install_blockio(ImageHandle, g_iso_buf_size);
776 Status = ventoy_boot(ImageHandle);
777
778 ventoy_delete_ramdisk_param();
779 }
780 else
781 {
782 ventoy_save_variable();
783 ventoy_find_iso_disk(ImageHandle);
784
785 ventoy_debug_pause();
786
787 ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
788
789 ventoy_debug_pause();
790
791 Status = ventoy_boot(ImageHandle);
792
793 ventoy_clean_env();
794 }
795
796 if (EFI_NOT_FOUND == Status)
797 {
798 gST->ConOut->OutputString(gST->ConOut, L"No bootfile found for UEFI!\r\n");
799 gST->ConOut->OutputString(gST->ConOut, L"Maybe the image does not support " VENTOY_UEFI_DESC L"!\r\n");
800 sleep(30);
801 }
802
803 ventoy_clear_input();
804 gST->ConOut->ClearScreen(gST->ConOut);
805
806 return EFI_SUCCESS;
807 }
808