OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
ServiceOverrides.c File Reference
#include "BootCompatInternal.h"
#include <Guid/AppleVariable.h>
#include <Guid/OcVariable.h>
#include <IndustryStandard/AppleHibernate.h>
#include <IndustryStandard/AppleEfiBootRtInfo.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DevicePathLib.h>
#include <Library/OcBootManagementLib.h>
#include <Library/OcLogAggregatorLib.h>
#include <Library/OcDebugLogLib.h>
#include <Library/OcDevicePathLib.h>
#include <Library/OcDeviceMiscLib.h>
#include <Library/OcMemoryLib.h>
#include <Library/OcMiscLib.h>
#include <Library/OcOSInfoLib.h>
#include <Library/OcStringLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Protocol/OcFirmwareRuntime.h>
#include <Protocol/VMwareMac.h>

Go to the source code of this file.

Functions

STATIC VOID FixRuntimeAttributes (IN BOOT_COMPAT_CONTEXT *BootCompat, IN UINT32 Type)
 
STATIC EFI_STATUS ForceExitBootServices (IN EFI_HANDLE ImageHandle, IN UINTN MapKey, IN EFI_EXIT_BOOT_SERVICES ExitBootServices OPTIONAL, IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL)
 
STATIC VOID ProtectMemoryRegions (IN UINTN MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN DescriptorSize)
 
STATIC VOID DevirtualiseMmio (IN VOID *Context, IN UINTN MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN DescriptorSize)
 
STATIC VOID ApplyBooterPatch (IN OUT UINT8 *ImageBase, IN UINTN ImageSize, IN OC_BOOTER_PATCH *Patch)
 
STATIC VOID ApplyBooterPatches (IN EFI_HANDLE ImageHandle, IN BOOLEAN IsApple, IN OC_BOOTER_PATCH *Patches, IN UINT32 PatchCount)
 
STATIC EFI_STATUS EFIAPI OcAllocatePages (IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN NumberOfPages, IN OUT EFI_PHYSICAL_ADDRESS *Memory)
 
STATIC EFI_STATUS EFIAPI OcFreePages (IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN Pages)
 
STATIC EFI_STATUS EFIAPI OcGetMemoryMap (IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, OUT UINTN *MapKey, OUT UINTN *DescriptorSize, OUT UINT32 *DescriptorVersion)
 
STATIC EFI_STATUS EFIAPI OcAllocatePool (IN EFI_MEMORY_TYPE PoolType, IN UINTN Size, OUT VOID **Buffer)
 
STATIC EFI_STATUS EFIAPI OcFreePool (IN VOID *Buffer)
 
STATIC BOOLEAN InternalIsEfiBootRt (IN EFI_DEVICE_PATH_PROTOCOL *EfiBootRtDevicePath OPTIONAL, IN VOID *SourceBuffer OPTIONAL, IN UINTN SourceSize, IN EFI_LOADED_IMAGE_PROTOCOL *EfiBootLoadedImage)
 
STATIC EFI_STATUS EFIAPI OcLoadImage (IN BOOLEAN BootPolicy, IN EFI_HANDLE ParentImageHandle, IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN VOID *SourceBuffer OPTIONAL, IN UINTN SourceSize, OUT EFI_HANDLE *ImageHandle)
 
STATIC EFI_STATUS EFIAPI OcStartImage (IN EFI_HANDLE ImageHandle, OUT UINTN *ExitDataSize, OUT CHAR16 **ExitData OPTIONAL)
 
STATIC EFI_STATUS EFIAPI OcExitBootServices (IN EFI_HANDLE ImageHandle, IN UINTN MapKey)
 
STATIC EFI_STATUS EFIAPI OcSetVirtualAddressMap (IN UINTN MemoryMapSize, IN UINTN DescriptorSize, IN UINT32 DescriptorVersion, IN EFI_MEMORY_DESCRIPTOR *MemoryMap)
 
STATIC EFI_STATUS EFIAPI OcGetVariable (IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, OUT UINT32 *Attributes OPTIONAL, IN OUT UINTN *DataSize, OUT VOID *Data)
 
STATIC VOID EFIAPI SetGetVariableHookHandler (IN EFI_EVENT Event, IN VOID *Context)
 
VOID InstallServiceOverrides (IN OUT BOOT_COMPAT_CONTEXT *BootCompat)
 

Detailed Description

Copyright (C) 2013, dmazar. All rights reserved. Copyright (C) 2017, vit9696. All rights reserved.

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.

Definition in file ServiceOverrides.c.

Function Documentation

◆ ApplyBooterPatch()

STATIC VOID ApplyBooterPatch ( IN OUT UINT8 * ImageBase,
IN UINTN ImageSize,
IN OC_BOOTER_PATCH * Patch )

Apply single booter patch.

Parameters
[in,out]ImageBaseBooter image to be patched.
[in]ImageSizeSize of booter image.
[in]PatchSingle patch to be applied to booter.

Definition at line 334 of file ServiceOverrides.c.

◆ ApplyBooterPatches()

STATIC VOID ApplyBooterPatches ( IN EFI_HANDLE ImageHandle,
IN BOOLEAN IsApple,
IN OC_BOOTER_PATCH * Patches,
IN UINT32 PatchCount )

Iterate through user booter patches and apply them.

Parameters
[in]ImageHandleLoaded image handle to patch.
[in]IsAppleWhether the booter is Apple-made.
[in]PatchesArray of patches to be applied.
[in]PatchesCountSize of patches to be applied.

Definition at line 392 of file ServiceOverrides.c.

◆ DevirtualiseMmio()

STATIC VOID DevirtualiseMmio ( IN VOID * Context,
IN UINTN MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR * MemoryMap,
IN UINTN DescriptorSize )

Mark MMIO virtual memory regions as non-runtime to reduce the amount of virtual memory required by boot.efi.

Parameters
[in]ContextBoot compatibility context.
[in,out]MemoryMapSizeMemory map size in bytes, updated on devirtualisation.
[in,out]MemoryMapMemory map to devirtualise.
[in]DescriptorSizeMemory map descriptor size in bytes.

Definition at line 245 of file ServiceOverrides.c.

◆ FixRuntimeAttributes()

STATIC VOID FixRuntimeAttributes ( IN BOOT_COMPAT_CONTEXT * BootCompat,
IN UINT32 Type )

Helper function to mark OpenRuntime as executable with proper permissions.

Parameters
[in]BootCompatBoot compatibility context.

Definition at line 51 of file ServiceOverrides.c.

◆ ForceExitBootServices()

STATIC EFI_STATUS ForceExitBootServices ( IN EFI_HANDLE ImageHandle,
IN UINTN MapKey,
IN EFI_EXIT_BOOT_SERVICES ExitBootServices OPTIONAL,
IN EFI_GET_MEMORY_MAP GetMemoryMap OPTIONAL )

Helper function to call ExitBootServices that can handle outdated MapKey issues.

Parameters
[in]ExitBootServicesExitBootServices function pointer, optional.
[in]GetMemoryMapGetMemoryMap function pointer, optional.
[in]ImageHandleImage handle to call ExitBootServices on.
[in]MapKeyMapKey to call ExitBootServices on.
Return values
EFI_SUCCESSon success.

Definition at line 95 of file ServiceOverrides.c.

◆ InstallServiceOverrides()

VOID InstallServiceOverrides ( IN OUT BOOT_COMPAT_CONTEXT * BootCompat)

Install UEFI services overrides as necessary.

Parameters
[in,out]BootCompatBoot compatibility context.

Definition at line 1626 of file ServiceOverrides.c.

◆ InternalIsEfiBootRt()

STATIC BOOLEAN InternalIsEfiBootRt ( IN EFI_DEVICE_PATH_PROTOCOL *EfiBootRtDevicePath OPTIONAL,
IN VOID *SourceBuffer OPTIONAL,
IN UINTN SourceSize,
IN EFI_LOADED_IMAGE_PROTOCOL * EfiBootLoadedImage )

Definition at line 764 of file ServiceOverrides.c.

◆ OcAllocatePages()

STATIC EFI_STATUS EFIAPI OcAllocatePages ( IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN NumberOfPages,
IN OUT EFI_PHYSICAL_ADDRESS * Memory )

UEFI Boot Services AllocatePages override. Returns pages from free memory block to boot.efi for kernel boot image.

Definition at line 451 of file ServiceOverrides.c.

◆ OcAllocatePool()

STATIC EFI_STATUS EFIAPI OcAllocatePool ( IN EFI_MEMORY_TYPE PoolType,
IN UINTN Size,
OUT VOID ** Buffer )

UEFI Boot Services AllocatePool override. Ensures synchronised memory attribute table.

Definition at line 701 of file ServiceOverrides.c.

◆ OcExitBootServices()

STATIC EFI_STATUS EFIAPI OcExitBootServices ( IN EFI_HANDLE ImageHandle,
IN UINTN MapKey )

UEFI Boot Services ExitBootServices override. Patches kernel entry point with jump to our KernelEntryPatchJumpBack().

Notes:

  • Most OSes attempt to call ExitBootServices more than once if it fails initially (similar to OpenCore ForceExitBootServices)
    • Therefore, OcExitBootServices may get called more than once
    • However this should never be relied upon for correct operation
  • Any logging within this call but before original ExitBootServices is attempted (e.g. within a scheduled handler) may cause ExitBootServices to fail (e.g. it may change the memory map by allocating), and should only be done, if at all, in the case of unexpected errors
  • Never log after original ExitBootServices has been attempted, not even on error

Definition at line 1380 of file ServiceOverrides.c.

◆ OcFreePages()

STATIC EFI_STATUS EFIAPI OcFreePages ( IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN Pages )

UEFI Boot Services FreePages override. Ensures synchronised memory attribute table.

Definition at line 553 of file ServiceOverrides.c.

◆ OcFreePool()

STATIC EFI_STATUS EFIAPI OcFreePool ( IN VOID * Buffer)

UEFI Boot Services FreePool override. Ensures synchronised memory attribute table.

Definition at line 732 of file ServiceOverrides.c.

◆ OcGetMemoryMap()

STATIC EFI_STATUS EFIAPI OcGetMemoryMap ( IN OUT UINTN * MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR * MemoryMap,
OUT UINTN * MapKey,
OUT UINTN * DescriptorSize,
OUT UINT32 * DescriptorVersion )

UEFI Boot Services GetMemoryMap override. Returns shrinked memory map as XNU can handle up to PMAP_MEMORY_REGIONS_SIZE (128) entries. Also applies any further memory map alterations as necessary.

Definition at line 583 of file ServiceOverrides.c.

◆ OcGetVariable()

STATIC EFI_STATUS EFIAPI OcGetVariable ( IN CHAR16 * VariableName,
IN EFI_GUID * VendorGuid,
OUT UINT32 *Attributes OPTIONAL,
IN OUT UINTN * DataSize,
OUT VOID * Data )

UEFI Runtime Services GetVariable override. Used to return customised values for boot-args and csr-active-config variables.

Definition at line 1499 of file ServiceOverrides.c.

◆ OcLoadImage()

STATIC EFI_STATUS EFIAPI OcLoadImage ( IN BOOLEAN BootPolicy,
IN EFI_HANDLE ParentImageHandle,
IN EFI_DEVICE_PATH_PROTOCOL * DevicePath,
IN VOID *SourceBuffer OPTIONAL,
IN UINTN SourceSize,
OUT EFI_HANDLE * ImageHandle )

UEFI Boot Services LoadImage override. Called to load an efi image. If this is bootrt.efi, then we patch its kernel call gate.

Definition at line 1030 of file ServiceOverrides.c.

◆ OcSetVirtualAddressMap()

STATIC EFI_STATUS EFIAPI OcSetVirtualAddressMap ( IN UINTN MemoryMapSize,
IN UINTN DescriptorSize,
IN UINT32 DescriptorVersion,
IN EFI_MEMORY_DESCRIPTOR * MemoryMap )

UEFI Runtime Services SetVirtualAddressMap override. Fixes virtualizing of RT services.

Definition at line 1467 of file ServiceOverrides.c.

◆ OcStartImage()

STATIC EFI_STATUS EFIAPI OcStartImage ( IN EFI_HANDLE ImageHandle,
OUT UINTN * ExitDataSize,
OUT CHAR16 **ExitData OPTIONAL )

UEFI Boot Services StartImage override. Called to start an efi image. If this is boot.efi, then our overrides are enabled.

Definition at line 1167 of file ServiceOverrides.c.

◆ ProtectMemoryRegions()

STATIC VOID ProtectMemoryRegions ( IN UINTN MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR * MemoryMap,
IN UINTN DescriptorSize )

Protect regions in memory map.

Parameters
[in,out]MemoryMapSizeMemory map size in bytes, updated on shrink.
[in,out]MemoryMapMemory map to shrink.
[in]DescriptorSizeMemory map descriptor size in bytes.

Definition at line 168 of file ServiceOverrides.c.

◆ SetGetVariableHookHandler()

STATIC VOID EFIAPI SetGetVariableHookHandler ( IN EFI_EVENT Event,
IN VOID * Context )

UEFI Runtime Services GetVariable override event handler. We do not override GetVariable ourselves but let our runtime do that.

Parameters
[in]EventEvent handle.
[in]ContextApple boot compatibility context.

Definition at line 1562 of file ServiceOverrides.c.