Skip to content

Commit

Permalink
OcAppleKernelLib: More progress with 11.0, many thx to @Download-Fritz
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Jul 3, 2020
1 parent a83f08d commit 0271bac
Show file tree
Hide file tree
Showing 15 changed files with 29,737 additions and 13,322 deletions.
106 changes: 100 additions & 6 deletions Include/Acidanthera/Library/OcAppleKernelLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
#define KC_LINKEDIT_SEGMENT "__LINKEDIT"
#define KC_MOSCOW_SEGMENT "__MOSCOW101"

//
// As PageCount is UINT16, we can only index 2^16 * 4096 Bytes with one chain.
//
#define PRELINKED_KEXTS_MAX_SIZE (BIT16 * MACHO_PAGE_SIZE)

//
// Failsafe default for plist reserve allocation.
//
Expand Down Expand Up @@ -159,6 +164,14 @@ typedef struct {
// Whether this kernel is a kernel collection (used by macOS 11.0+).
//
BOOLEAN IsKernelCollection;

UINT32 KextsFileOffset;

UINT64 KextsVmAddress;

MACH_DYLD_CHAINED_STARTS_IN_SEGMENT *KextsFixupChains;

UINT64 VirtualBase;
} PRELINKED_CONTEXT;

//
Expand Down Expand Up @@ -304,7 +317,8 @@ PrelinkedDependencyInsert (
EFI_STATUS
PrelinkedInjectPrepare (
IN OUT PRELINKED_CONTEXT *Context,
IN UINT32 LinkedExpansion
IN UINT32 LinkedExpansion,
IN UINT32 ReservedExeSize
);

/**
Expand All @@ -322,16 +336,18 @@ PrelinkedInjectComplete (
/**
Updated required reserve size to inject this kext.
@param[in,out] ReservedSize Current reserved size, updated.
@param[in] InfoPlistSize Kext Info.plist size.
@param[in] Executable Kext executable, optional.
@param[in] ExecutableSize Kext executable size, optional.
@param[in,out] ReservedInfoSize Current reserved PLIST size, updated.
@param[in,out] ReservedExeSize Current reserved KEXT size, updated.
@param[in] InfoPlistSize Kext Info.plist size.
@param[in] Executable Kext executable, optional.
@param[in] ExecutableSize Kext executable size, optional.
@return EFI_SUCCESS on success.
**/
EFI_STATUS
PrelinkedReserveKextSize (
IN OUT UINT32 *ReservedSize,
IN OUT UINT32 *ReservedInfoSize,
IN OUT UINT32 *ReservedExeSize,
IN UINT32 InfoPlistSize,
IN UINT8 *Executable OPTIONAL,
IN UINT32 ExecutableSize OPTIONAL
Expand Down Expand Up @@ -361,6 +377,84 @@ PrelinkedInjectKext (
IN UINT32 ExecutableSize OPTIONAL
);

EFI_STATUS
KcRebuildMachHeader (
IN OUT PRELINKED_CONTEXT *Context
);

/*
Returns the size required to store a segment's fixup chains information.
@param[in] SegmentSize The size, in bytes, of the segment to index.
@retval 0 The segment is too large to index with a single structure.
@retval other The size, in bytes, required to store a segment's fixup chain
information.
*/
UINT32
KcGetSegmentFixupChainsSize (
IN UINT32 SegmentSize
);

/*
Initialises a structure that stores a segments's fixup chains information.
@param[out] SegChain The information structure to initialise.
@param[in] SegChainSize The size, in bytes, available to SegChain.
@param[in] VmAddress The virtual address of the segment to index.
@param[in] VmSize The virtual size of the segment to index.
*/
EFI_STATUS
KcInitKextFixupChains (
IN OUT PRELINKED_CONTEXT *Context,
IN UINT32 SegChainSize,
IN UINT32 ReservedSize
);

/*
Indexes all relocations of MachContext into the kernel described by Context.
@param[in,out] Context Prelinked context.
@param[in] MachContext The context of the Mach-O to index. It must have
been prelinked by OcAppleKernelLib. The image
must reside in Segment.
*/
VOID
KcKextIndexFixups (
IN OUT PRELINKED_CONTEXT *Context,
IN OC_MACHO_CONTEXT *MachContext
);

/*
Retrieves a KC KEXT's virtual size.
@param[in] Context Prelinked context.
@param[in] SourceAddress The virtual address within the KC image of the KEXT.
@retval 0 An error has occured.
@retval other The virtual size, in bytes, of the KEXT at SourceAddress.
*/
UINT32
KcGetKextSize (
IN PRELINKED_CONTEXT *Context,
IN UINT64 SourceAddress
);

/*
Apply the delta from KC header to the file's offsets.
@param[in,out] Context The context of the KEXT to rebase.
@param[in] Delta The offset from KC header the KEXT starts at.
@retval EFI_SUCCESS The file has beem rebased successfully.
@retval other An error has occured.
*/
EFI_STATUS
KcKextApplyFileDelta (
IN OUT OC_MACHO_CONTEXT *Context,
IN UINT32 Delta
);

/**
Initialize patcher from prelinked context for kext patching.
Expand Down
22 changes: 22 additions & 0 deletions Include/Apple/IndustryStandard/AppleMachoImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,27 @@ typedef struct {
IsAuth : 1; ///< 0 -> not authenticated. 1 -> authenticated
} MACH_DYKD_CHAINED_PTR_64_KERNEL_CACHE_REBASE;

// header of the LC_DYLD_CHAINED_FIXUPS payload
typedef struct {
UINT32 FixupsVersion; ///< 0
UINT32 StartsOffset; ///< offset of dyld_chained_starts_in_image in chain_data
UINT32 ImportsOffset; ///< offset of imports table in chain_data
UINT32 SymbolsOffset; ///< offset of symbol strings in chain_data
UINT32 ImportsCount; ///< number of imported symbol names
UINT32 ImportsFormat; ///< DYLD_CHAINED_IMPORT*
UINT32 SymbolsFormat; ///< 0 => uncompressed, 1 => zlib compressed
} MACHO_DYLD_CHAINED_FIXUPS_HEADER;

///
/// This struct is embedded in LC_DYLD_CHAINED_FIXUPS payload
///
typedef struct {
UINT32 NumSegments;
UINT32 SegInfoOffset[]; ///< each entry is offset into this struct for that
///< segment followed by pool of
///< dyld_chain_starts_in_segment data
} MACH_DYLD_CHAINED_STARTS_IN_IMAGE;

typedef struct {
UINT32 Size; ///< size of this (amount kernel needs to copy)
UINT16 PageSize; ///< 0x1000 or 0x4000
Expand Down Expand Up @@ -1995,6 +2016,7 @@ typedef UINT32 MACH_HEADER_FILE_TYPE;
#define MACH_HEADER_FLAG_APP_EXTENSION_SAFE BIT25
#define MACH_HEADER_FLAG_NLIST_OUTOFSYNC_WITH_DYLDINFO BIT26
#define MACH_HEADER_FLAG_SIM_SUPPORT BIT27
#define MACH_HEADER_FLAG_DYLIB_IN_CACHE BIT31

typedef UINT32 MACH_HEADER_FLAGS;

Expand Down
2 changes: 1 addition & 1 deletion Include/Intel/Protocol/LegacyBios.h
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ BOOLEAN
the Stack argument
@param[in] This The protocol instance pointer.
@param[in] Segment The segemnt of 16-bit mode call.
@param[in] Segment The segment of 16-bit mode call.
@param[in] Offset The offset of 16-bit mdoe call.
@param[in] Reg Register contexted passed into (and returned) from thunk to
16-bit mode.
Expand Down
4 changes: 2 additions & 2 deletions Legacy/BootPlatform/PciBusNoEnumerationDxe/PciIo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1435,14 +1435,14 @@ Routine Description:
if (IS_PCI_GFX(&PciIoDevice->Pci)) {

//
//Get the boot VGA on the same segement
//Get the boot VGA on the same segment
//
Temp = ActiveVGADeviceOnTheSameSegment (PciIoDevice);

if (!Temp) {

//
// If there is no VGA device on the segement, set
// If there is no VGA device on the segment, set
// this graphics card to decode the palette range
//
DisableCommand |= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP;
Expand Down
Loading

0 comments on commit 0271bac

Please sign in to comment.