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