/** @file OpenCore driver. Copyright (c) 2019, vit9696. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ #include #include #include #include #include #include #include STATIC VOID LoadDrivers ( IN OC_STORAGE_CONTEXT *Storage, IN OC_GLOBAL_CONFIG *Config ) { EFI_STATUS Status; VOID *Driver; UINT32 DriverSize; UINT32 Index; CHAR16 DriverPath[64]; EFI_HANDLE ImageHandle; DEBUG ((DEBUG_INFO, "OC: Got %u drivers\n", Config->Uefi.Drivers.Count)); for (Index = 0; Index < Config->Uefi.Drivers.Count; ++Index) { DEBUG (( DEBUG_INFO, "OC: Driver %a at %u is being loaded...\n", Index, OC_BLOB_GET (Config->Uefi.Drivers.Values[Index]) )); UnicodeSPrint ( DriverPath, sizeof (DriverPath), OPEN_CORE_UEFI_DRIVER_PATH "%a", OC_BLOB_GET (Config->Uefi.Drivers.Values[Index]) ); Driver = OcStorageReadFileUnicode (Storage, DriverPath, &DriverSize); if (Driver == NULL) { DEBUG (( DEBUG_ERROR, "OC: Driver %a at %u cannot be found!\n", Index, OC_BLOB_GET (Config->Uefi.Drivers.Values[Index]) )); // // TODO: This should cause security violation if configured! // continue; } // // TODO: Use AppleLoadedImage!! // ImageHandle = NULL; Status = gBS->LoadImage ( FALSE, gImageHandle, NULL, Driver, DriverSize, &ImageHandle ); if (EFI_ERROR (Status)) { DEBUG (( DEBUG_ERROR, "OC: Driver %a at %u cannot be loaded - %r!\n", Index, OC_BLOB_GET (Config->Uefi.Drivers.Values[Index]), Status )); FreePool (Driver); continue; } Status = gBS->StartImage ( ImageHandle, NULL, NULL ); if (EFI_ERROR (Status)) { DEBUG (( DEBUG_ERROR, "OC: Driver %a at %u cannot be started - %r!\n", Index, OC_BLOB_GET (Config->Uefi.Drivers.Values[Index]), Status )); gBS->UnloadImage (ImageHandle); } FreePool (Driver); } } STATIC VOID ConnectDrivers ( VOID ) { EFI_STATUS Status; UINTN HandleCount; EFI_HANDLE *HandleBuffer; UINTN Index; VOID *DriverBinding; Status = gBS->LocateHandleBuffer ( AllHandles, NULL, NULL, &HandleCount, &HandleBuffer ); if (EFI_ERROR (Status)) { return; } for (Index = 0; Index < HandleCount; ++Index) { Status = gBS->HandleProtocol ( HandleBuffer[Index], &gEfiDevicePathProtocolGuid, &DriverBinding ); if (EFI_ERROR (Status)) { // // Calling ConnectController on non-driver results in freezes on APTIO IV. // continue; } gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE); } FreePool (HandleBuffer); } STATIC VOID ProvideConsoleGop ( VOID ) { EFI_STATUS Status; VOID *Gop; Gop = NULL; Status = gBS->HandleProtocol (gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, &Gop); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_WARN, "Missing GOP on ConsoleOutHandle - %r\n", Status)); Status = gBS->LocateProtocol (&gEfiGraphicsOutputProtocolGuid, NULL, &Gop); if (!EFI_ERROR (Status)) { Status = gBS->InstallMultipleProtocolInterfaces ( &gST->ConsoleOutHandle, &gEfiGraphicsOutputProtocolGuid, Gop, NULL ); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_WARN, "Failed to install GOP on ConsoleOutHandle - %r\n", Status)); } } else { DEBUG ((DEBUG_WARN, "Missing GOP entirely - %r\n", Status)); } } } VOID OcLoadUefiSupport ( IN OC_STORAGE_CONTEXT *Storage, IN OC_GLOBAL_CONFIG *Config, IN OC_CPU_INFO *CpuInfo ) { if (Config->Uefi.Quirks.DisableWatchDog) { // // boot.efi kills watchdog only in FV2 UI. // gBS->SetWatchdogTimer (0, 0, 0, NULL); } if (Config->Uefi.Quirks.IgnoreInvalidFlexRatio) { OcCpuCorrectFlexRatio (CpuInfo); } if (Config->Uefi.Quirks.ProvideConsoleGop) { ProvideConsoleGop (); } LoadDrivers (Storage, Config); if (Config->Uefi.ConnectDrivers) { ConnectDrivers (); } }