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