2019-09-03 11:58:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2010 Frank peng. All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LIBSAIO_KERNEL_PATCHER_H
|
|
|
|
#define __LIBSAIO_KERNEL_PATCHER_H
|
|
|
|
|
2020-04-16 11:09:22 +02:00
|
|
|
#include "boot.h"
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
#define CPUFAMILY_INTEL_6_13 0xaa33392b
|
|
|
|
#define CPUFAMILY_INTEL_YONAH 0x73d67300
|
|
|
|
#define CPUFAMILY_INTEL_MEROM 0x426f69ef
|
|
|
|
#define CPUFAMILY_INTEL_PENRYN 0x78ea4fbc
|
|
|
|
#define CPUFAMILY_INTEL_NEHALEM 0x6b5a4cd2
|
|
|
|
#define CPUFAMILY_INTEL_WESTMERE 0x573b5eec
|
|
|
|
|
|
|
|
#define CPUIDFAMILY_DEFAULT 6
|
|
|
|
|
|
|
|
#define CPUID_MODEL_6_13 13
|
|
|
|
#define CPUID_MODEL_YONAH 14
|
|
|
|
#define CPUID_MODEL_MEROM 15
|
2020-04-29 06:58:43 +02:00
|
|
|
#define CPUID_MODEL_PENRYN 23
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
#define MACH_GET_MAGIC(hdr) (((struct mach_header_64*)(hdr))->magic)
|
|
|
|
#define MACH_GET_NCMDS(hdr) (((struct mach_header_64*)(hdr))->ncmds)
|
|
|
|
#define MACH_GET_CPU(hdr) (((struct mach_header_64*)(hdr))->cputype)
|
|
|
|
#define MACH_GET_FLAGS(hdr) (((struct mach_header_64*)(hdr))->flags)
|
|
|
|
#define SC_GET_CMD(hdr) (((struct segment_command_64*)(hdr))->cmd)
|
|
|
|
|
|
|
|
|
|
|
|
#define kPrelinkTextSegment "__PRELINK_TEXT"
|
|
|
|
#define kPrelinkTextSection "__text"
|
|
|
|
|
|
|
|
#define kPrelinkLinkStateSegment "__PRELINK_STATE"
|
|
|
|
#define kPrelinkKernelLinkStateSection "__kernel"
|
|
|
|
#define kPrelinkKextsLinkStateSection "__kexts"
|
|
|
|
|
|
|
|
#define kPrelinkInfoSegment "__PRELINK_INFO"
|
|
|
|
#define kPrelinkInfoSection "__info"
|
|
|
|
|
2020-04-27 17:16:43 +02:00
|
|
|
#define kLinkEditSegment "__LINKEDIT"
|
2020-04-29 10:06:44 +02:00
|
|
|
#define kTextSegment "__TEXT"
|
2020-04-29 17:07:10 +02:00
|
|
|
#define ID_SEG_TEXT 0x010f
|
2020-04-29 10:06:44 +02:00
|
|
|
#define kDataSegment "__DATA"
|
2020-04-29 17:07:10 +02:00
|
|
|
#define ID_SEG_DATA 0x0f0f
|
2020-04-29 10:06:44 +02:00
|
|
|
#define kDataConstSegment "__DATA_CONST"
|
2020-04-29 17:07:10 +02:00
|
|
|
#define ID_SEG_DATA_CONST 0x110f
|
2020-04-29 10:06:44 +02:00
|
|
|
#define kKldSegment "__KLD"
|
2020-04-29 17:07:10 +02:00
|
|
|
#define ID_SEG_KLD 0x180f
|
|
|
|
#define ID_SEG_KLD2 0x1a0f
|
2020-04-27 17:16:43 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
#define kPrelinkBundlePathKey "_PrelinkBundlePath"
|
|
|
|
#define kPrelinkExecutableRelativePathKey "_PrelinkExecutableRelativePath"
|
|
|
|
#define kPrelinkExecutableLoadKey "_PrelinkExecutableLoadAddr"
|
|
|
|
#define kPrelinkExecutableSourceKey "_PrelinkExecutableSourceAddr"
|
|
|
|
#define kPrelinkExecutableSizeKey "_PrelinkExecutableSize"
|
|
|
|
#define kPrelinkInfoDictionaryKey "_PrelinkInfoDictionary"
|
|
|
|
#define kPrelinkInterfaceUUIDKey "_PrelinkInterfaceUUID"
|
|
|
|
#define kPrelinkKmodInfoKey "_PrelinkKmodInfo"
|
|
|
|
#define kPrelinkLinkStateKey "_PrelinkLinkState"
|
|
|
|
#define kPrelinkLinkStateSizeKey "_PrelinkLinkStateSize"
|
|
|
|
|
|
|
|
#define kPropCFBundleIdentifier ("CFBundleIdentifier")
|
|
|
|
#define kPropCFBundleExecutable ("CFBundleExecutable")
|
|
|
|
#define kPropOSBundleRequired ("OSBundleRequired")
|
|
|
|
#define kPropOSBundleLibraries ("OSBundleLibraries")
|
|
|
|
#define kPropIOKitPersonalities ("IOKitPersonalities")
|
|
|
|
#define kPropIONameMatch ("IONameMatch")
|
|
|
|
|
|
|
|
typedef struct _BooterKextFileInfo {
|
|
|
|
UINT32 infoDictPhysAddr;
|
|
|
|
UINT32 infoDictLength;
|
|
|
|
UINT32 executablePhysAddr;
|
|
|
|
UINT32 executableLength;
|
|
|
|
UINT32 bundlePathPhysAddr;
|
|
|
|
UINT32 bundlePathLength;
|
|
|
|
} _BooterKextFileInfo;
|
|
|
|
|
|
|
|
typedef struct _DeviceTreeBuffer {
|
|
|
|
uint32_t paddr;
|
|
|
|
uint32_t length;
|
|
|
|
} _DeviceTreeBuffer;
|
|
|
|
|
2020-04-27 17:16:43 +02:00
|
|
|
typedef struct VTABLE {
|
|
|
|
UINT32 NameOffset;
|
2020-04-29 10:06:44 +02:00
|
|
|
UINT32 Seg;
|
2020-04-27 17:16:43 +02:00
|
|
|
UINT64 ProcAddr;
|
|
|
|
} VTABLE;
|
|
|
|
|
|
|
|
typedef struct SEGMENT {
|
|
|
|
CHAR8 Name[16]; //0
|
2020-04-29 06:58:43 +02:00
|
|
|
UINT64 SegAddress; //16 0x10
|
|
|
|
UINT64 vmsize; //0x18 0x16FB60
|
|
|
|
UINT64 fileoff; //0x20 0xDDA000
|
|
|
|
UINT64 filesize; //0x28 0x16FB60
|
|
|
|
UINT32 maxprot; //0x30 01-Cat 07-Moj
|
|
|
|
UINT32 initprot; //0x34 01
|
|
|
|
UINT32 NumSects; //0x38 00
|
|
|
|
UINT32 Flags; //0x3C 00
|
|
|
|
UINT32 Cmd[2]; //0x40 02, 18
|
|
|
|
UINT32 AddrVtable; //0x48
|
|
|
|
UINT32 SizeVtable; //0x4C
|
|
|
|
UINT32 AddrNames; //0x50
|
2020-04-27 17:16:43 +02:00
|
|
|
} SEGMENT;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
extern EFI_PHYSICAL_ADDRESS KernelRelocBase;
|
|
|
|
extern BootArgs1 *bootArgs1;
|
|
|
|
extern BootArgs2 *bootArgs2;
|
|
|
|
extern CHAR8 *dtRoot;
|
2019-12-18 18:34:26 +01:00
|
|
|
extern UINT32 *dtLength;
|
2019-12-27 18:01:40 +01:00
|
|
|
extern UINT8 *KernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
extern UINT32 KernelSlide;
|
|
|
|
extern BOOLEAN isKernelcache;
|
|
|
|
extern BOOLEAN is64BitKernel;
|
|
|
|
extern BOOLEAN gSNBEAICPUFixRequire; // SandyBridge-E AppleIntelCpuPowerManagement patch require or not
|
|
|
|
extern BOOLEAN gBDWEIOPCIFixRequire; // Broadwell-E IOPCIFamily fix require or not
|
|
|
|
|
|
|
|
// notes:
|
|
|
|
// - 64bit segCmd64->vmaddr is 0xffffff80xxxxxxxx and we are taking
|
|
|
|
// only lower 32bit part into PrelinkTextAddr
|
|
|
|
// - PrelinkTextAddr is segCmd64->vmaddr + KernelRelocBase
|
|
|
|
extern UINT32 PrelinkTextLoadCmdAddr;
|
|
|
|
extern UINT32 PrelinkTextAddr;
|
|
|
|
extern UINT32 PrelinkTextSize;
|
|
|
|
|
|
|
|
// notes:
|
|
|
|
// - 64bit sect->addr is 0xffffff80xxxxxxxx and we are taking
|
|
|
|
// only lower 32bit part into PrelinkInfoAddr
|
|
|
|
// - PrelinkInfoAddr is sect->addr + KernelRelocBase
|
|
|
|
extern UINT32 PrelinkInfoLoadCmdAddr;
|
|
|
|
extern UINT32 PrelinkInfoAddr;
|
|
|
|
extern UINT32 PrelinkInfoSize;
|
|
|
|
|
|
|
|
extern UINT32 DisplayVendor[];
|
|
|
|
//VOID findCPUfamily();
|
|
|
|
|
2020-04-16 09:15:26 +02:00
|
|
|
extern BOOLEAN SSSE3;
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//UINT64 kernelsize;
|
|
|
|
|
|
|
|
VOID Patcher_SSE3_5(VOID* kernelData);
|
|
|
|
VOID Patcher_SSE3_6(VOID* kernelData);
|
|
|
|
VOID Patcher_SSE3_7(VOID* kernelData);
|
|
|
|
|
2020-02-29 08:30:21 +01:00
|
|
|
#include "../gui/menu_items/menu_items.h" // for LOADER_ENTRY
|
2020-03-02 13:00:24 +01:00
|
|
|
class LOADER_ENTRY;
|
2019-09-03 11:58:42 +02:00
|
|
|
VOID KernelAndKextsPatcherStart(IN LOADER_ENTRY *Entry);
|
|
|
|
|
|
|
|
//VOID register_kernel_symbol(CONST CHAR8* name);
|
|
|
|
//UINT64 symbol_handler(CHAR8* symbolName, UINT64 addr);
|
|
|
|
//INTN locate_symbols(VOID* kernelData);
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
//
|
|
|
|
// kext_patcher.c
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Called from SetFSInjection(), before boot.efi is started,
|
|
|
|
// to allow patchers to prepare FSInject to force load needed kexts.
|
|
|
|
//
|
|
|
|
VOID KextPatcherRegisterKexts(FSINJECTION_PROTOCOL *FSInject, FSI_STRING_LIST *ForceLoadKexts, LOADER_ENTRY *Entry);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Entry for all kext patches.
|
|
|
|
// Will iterate through kext in prelinked kernel (kernelcache)
|
|
|
|
// or DevTree (drivers boot) and do patches.
|
|
|
|
//
|
|
|
|
VOID KextPatcherStart(LOADER_ENTRY *Entry);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Searches Source for Search pattern of size SearchSize
|
|
|
|
// and returns the number of occurences.
|
|
|
|
//
|
|
|
|
UINTN SearchAndCount(UINT8 *Source, UINT64 SourceSize, UINT8 *Search, UINTN SearchSize);
|
|
|
|
|
2020-04-25 18:18:18 +02:00
|
|
|
BOOLEAN CompareMemMask(UINT8 *Source, UINT8 *Search, UINT8 *Mask, UINTN SearchSize);
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// Searches Source for Search pattern of size SearchSize
|
|
|
|
// and replaces it with Replace up to MaxReplaces times.
|
|
|
|
// If MaxReplaces <= 0, then there is no restriction on number of replaces.
|
|
|
|
// Replace should have the same size as Search.
|
|
|
|
// Returns number of replaces done.
|
|
|
|
//
|
|
|
|
UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, UINT8 *Search, UINTN SearchSize, UINT8 *Replace, INTN MaxReplaces);
|
|
|
|
|
2019-10-08 22:57:18 +02:00
|
|
|
UINTN SearchAndReplaceMask(UINT8 *Source, UINT64 SourceSize, UINT8 *Search, UINT8 *MaskSearch, UINTN SearchSize, UINT8 *Replace, UINT8 *MaskReplace, INTN MaxReplaces);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-04-29 17:07:10 +02:00
|
|
|
UINTN searchProc(unsigned char * kernel, const char *procedure, UINTN *procLen);
|
2020-04-27 17:16:43 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
#endif /* !__LIBSAIO_KERNEL_PATCHER_H */
|