-STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle)
-{
- UINTN i = 0;
- UINTN Count = 0;
- EFI_HANDLE Parent = NULL;
- EFI_HANDLE *Handles = NULL;
- EFI_STATUS Status = EFI_SUCCESS;
- EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
- EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
-
- Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
- NULL, &Count, &Handles);
- if (EFI_ERROR(Status))
- {
- return Status;
- }
-
- debug("ventoy_find_iso_disk_fs fs count:%u", Count);
-
- for (i = 0; i < Count; i++)
- {
- Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
- if (EFI_ERROR(Status))
- {
- continue;
- }
-
- Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
- (VOID **)&pDevPath,
- ImageHandle,
- Handles[i],
- EFI_OPEN_PROTOCOL_GET_PROTOCOL);
- if (EFI_ERROR(Status))
- {
- debug("Failed to open device path protocol %r", Status);
- continue;
- }
-
- debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
- Parent = ventoy_get_parent_handle(pDevPath);
-
- if (Parent == gBlockData.RawBlockIoHandle)
- {
- debug("Find ventoy disk fs");
- gBlockData.DiskFsHandle = Handles[i];
- gBlockData.pDiskFs = pFile;
- gBlockData.pDiskFsDevPath = pDevPath;
- break;
- }
- }
-
- FreePool(Handles);
-
- return EFI_SUCCESS;
-}
-
-STATIC EFI_STATUS EFIAPI ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle)
-{
- EFI_HANDLE Image = NULL;
- EFI_STATUS Status = EFI_SUCCESS;
- CHAR16 LogVar[4] = L"5";
-
- Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
- gIso9660EfiDriverPath,
- sizeof(gIso9660EfiDriverPath),
- &Image);
- debug("load iso efi driver status:%r", Status);
-
- if (gDebugPrint)
- {
- gRT->SetVariable(L"FS_LOGGING", &gShellVariableGuid,
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
- sizeof(LogVar), LogVar);
- }
-
- gRT->SetVariable(L"FS_NAME_NOCASE", &gShellVariableGuid,
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
- sizeof(LogVar), LogVar);
-
- gBlockData.IsoDriverImage = Image;
- Status = gBS->StartImage(Image, NULL, NULL);
- debug("Start iso efi driver status:%r", Status);
-
- return EFI_SUCCESS;
-}
-
-static int ventoy_update_image_location(ventoy_os_param *param)
-{
- EFI_STATUS Status = EFI_SUCCESS;
- UINT8 chksum = 0;
- unsigned int i;
- unsigned int length;
- UINTN address = 0;
- void *buffer = NULL;
- ventoy_image_location *location = NULL;
- ventoy_image_disk_region *region = NULL;
- ventoy_img_chunk *chunk = g_chunk;
-
- length = sizeof(ventoy_image_location) + (g_img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
-
- Status = gBS->AllocatePool(EfiRuntimeServicesData, length + 4096 * 2, &buffer);
- if (EFI_ERROR(Status) || NULL == buffer)
- {
- debug("Failed to allocate runtime pool %r\n", Status);
- return 1;
- }
-
- address = (UINTN)buffer;
-
- if (address % 4096)
- {
- address += 4096 - (address % 4096);
- }
-
- param->chksum = 0;
- param->vtoy_img_location_addr = address;
- param->vtoy_img_location_len = length;
-
- /* update check sum */
- for (i = 0; i < sizeof(ventoy_os_param); i++)
- {
- chksum += *((UINT8 *)param + i);
- }
- param->chksum = (chksum == 0) ? 0 : (UINT8)(0x100 - chksum);
-
- location = (ventoy_image_location *)(unsigned long)(param->vtoy_img_location_addr);
- if (NULL == location)
- {
- return 0;
- }
-
- CopyMem(&location->guid, ¶m->guid, sizeof(ventoy_guid));
- location->image_sector_size = 2048;
- location->disk_sector_size = g_chain->disk_sector_size;
- location->region_count = g_img_chunk_num;
-
- region = location->regions;
-
- for (i = 0; i < g_img_chunk_num; i++)
- {
- region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
- region->image_start_sector = chunk->img_start_sector;
- region->disk_start_sector = chunk->disk_start_sector;
- region++;
- chunk++;
- }
-
- return 0;
-}
-