2019-09-03 11:58:42 +02:00
|
|
|
/*
|
|
|
|
* Original idea of patching kernel by Evan Lojewsky, 2009
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011-2012 Frank Peng. All rights reserved.
|
|
|
|
*
|
2020-07-17 13:36:51 +02:00
|
|
|
* 2012 - 2020 Correction and improvements by Clover team
|
2019-09-03 11:58:42 +02:00
|
|
|
*/
|
|
|
|
|
2020-07-21 08:33:43 +02:00
|
|
|
//#include <IndustryStardard/MachO-loader.h>
|
2020-07-24 20:59:51 +02:00
|
|
|
|
|
|
|
|
2020-07-21 08:33:43 +02:00
|
|
|
#include <UefiLoader.h>
|
|
|
|
|
2020-08-17 21:40:52 +02:00
|
|
|
#include <Platform.h> // Only use angled for Platform, else, xcode project won't compile
|
2020-07-21 08:33:43 +02:00
|
|
|
//#include "LoaderUefi.h"
|
2020-04-16 09:15:26 +02:00
|
|
|
#include "Nvram.h"
|
|
|
|
#include "FixBiosDsdt.h"
|
|
|
|
#include "cpu.h"
|
2020-04-16 11:09:22 +02:00
|
|
|
#include "kext_inject.h"
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
#include "kernel_patcher.h"
|
2020-05-18 21:40:47 +02:00
|
|
|
//#include "sse3_patcher.h"
|
|
|
|
//#include "sse3_5_patcher.h"
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-03-26 13:59:20 +01:00
|
|
|
#ifndef DEBUG_ALL
|
2020-07-27 15:09:30 +02:00
|
|
|
#define KERNEL_DEBUG 1
|
2020-03-26 13:59:20 +01:00
|
|
|
#else
|
|
|
|
#define KERNEL_DEBUG DEBUG_ALL
|
|
|
|
#endif
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-06-27 04:57:27 +02:00
|
|
|
#if KERNEL_DEBUG == 2
|
2020-03-26 13:59:20 +01:00
|
|
|
#define DBG(...) printf(__VA_ARGS__);
|
2020-06-27 04:57:27 +02:00
|
|
|
#elif KERNEL_DEBUG == 1
|
|
|
|
#define DBG(...) DebugLog(KERNEL_DEBUG, __VA_ARGS__)
|
2019-09-03 11:58:42 +02:00
|
|
|
#else
|
|
|
|
#define DBG(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// runtime debug
|
2020-05-01 18:26:28 +02:00
|
|
|
//make it a member of LOADER_ENTRY class entry.DBG_RT(...)
|
2020-08-11 14:43:53 +02:00
|
|
|
//#define DBG_RT( ...) if ((KernelAndKextPatches != NULL) && KernelAndKextPatches.KPDebug) { printf(__VA_ARGS__); }
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
|
2020-05-11 17:44:00 +02:00
|
|
|
//EFI_PHYSICAL_ADDRESS KernelRelocBase = 0;
|
|
|
|
//BootArgs1 *bootArgs1 = NULL;
|
|
|
|
//BootArgs2 *bootArgs2 = NULL;
|
|
|
|
//CHAR8 *dtRoot = NULL;
|
|
|
|
//UINT32 *dtLength;
|
2020-05-09 09:56:30 +02:00
|
|
|
//UINT8 *KernelData = NULL;
|
2020-05-11 17:44:00 +02:00
|
|
|
//UINT32 KernelSlide = 0;
|
|
|
|
//BOOLEAN isKernelcache = FALSE;
|
|
|
|
//BOOLEAN is64BitKernel = FALSE;
|
|
|
|
//BOOLEAN SSSE3;
|
|
|
|
|
|
|
|
//BOOLEAN PatcherInited = FALSE;
|
|
|
|
//BOOLEAN gSNBEAICPUFixRequire = FALSE; // SandyBridge-E AppleIntelCpuPowerManagement patch require or not
|
|
|
|
//BOOLEAN gBDWEIOPCIFixRequire = FALSE; // Broadwell-E IOPCIFamily fix require or not
|
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
extern EFI_GUID gEfiAppleBootGuid;
|
|
|
|
|
2020-05-11 17:44:00 +02:00
|
|
|
/*
|
|
|
|
* the driver OsxAptioFixDrv is old and mostly not used in favour of its successors.
|
|
|
|
* anyway we will keep it for new investigations.
|
|
|
|
*/
|
|
|
|
VOID LOADER_ENTRY::SetKernelRelocBase()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
// EFI_STATUS Status;
|
|
|
|
UINTN DataSize = sizeof(KernelRelocBase);
|
|
|
|
|
|
|
|
KernelRelocBase = 0;
|
|
|
|
// OsxAptioFixDrv will set this
|
|
|
|
/*Status = */gRT->GetVariable(L"OsxAptioFixDrv-RelocBase", &gEfiAppleBootGuid, NULL, &DataSize, &KernelRelocBase);
|
|
|
|
DeleteNvramVariable(L"OsxAptioFixDrv-RelocBase", &gEfiAppleBootGuid); // clean up the temporary variable
|
|
|
|
// KernelRelocBase is now either read or 0
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
//Slice
|
2020-07-23 21:14:20 +02:00
|
|
|
// the purpose of the procedure is to find a table of symbols in the shifted kernel
|
2020-05-09 09:56:30 +02:00
|
|
|
EFI_STATUS LOADER_ENTRY::getVTable()
|
2020-05-01 18:26:28 +02:00
|
|
|
{
|
2020-06-27 04:57:27 +02:00
|
|
|
DBG("kernel at 0x%llx\n", (UINTN)KernelData);
|
2020-07-18 14:27:17 +02:00
|
|
|
// INT32 LinkAdr = FindBin(KernelData, 0x3000, (const UINT8 *)kLinkEditSegment, (UINT32)strlen(kLinkEditSegment));
|
|
|
|
// if (LinkAdr == -1) {
|
|
|
|
// DBG("no LinkEdit\n");
|
|
|
|
// return EFI_NOT_FOUND;
|
|
|
|
// }
|
2020-05-02 05:38:38 +02:00
|
|
|
// const UINT8 vtable[] = {0x04, 00,00,00, 0x0F, 0x08, 00, 00};
|
2020-06-27 04:57:27 +02:00
|
|
|
// const UINT8 vtableSur[] = {0x25, 00,00,00, 0x0F, 0x06, 00, 00};
|
|
|
|
//25000000 0F060000 940BFF00 80FFFFFF
|
|
|
|
//00FFFFFF FF0FFFFF 00000000 FFFFFFFF
|
2020-05-01 18:26:28 +02:00
|
|
|
|
2020-06-27 04:57:27 +02:00
|
|
|
// INT32 Tabble = FindBin(KernelData, 0x5000000, vtableSur, 8);
|
2020-07-17 22:45:01 +02:00
|
|
|
INT32 NTabble = FindBin(KernelData, KERNEL_MAX_SIZE, (const UINT8 *)ctor_used, (UINT32)strlen(ctor_used));
|
2020-07-20 10:52:36 +02:00
|
|
|
DBG("ctor_used found at 0x%x\n", NTabble);
|
2020-05-01 18:26:28 +02:00
|
|
|
if (NTabble < 0) {
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
2020-06-27 04:57:27 +02:00
|
|
|
while (KernelData[NTabble] || KernelData[NTabble-1]) --NTabble;
|
|
|
|
NTabble &= ~0x03; //align, may be 0x07?
|
|
|
|
// NTabble -=4;
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG(" NTabble=%x\n", NTabble);
|
2020-06-27 04:57:27 +02:00
|
|
|
// DBG("LinkAdr=%x NTabble=%x Tabble=%x\n",LinkAdr, NTabble, Tabble);
|
2020-07-17 22:45:01 +02:00
|
|
|
// SEGMENT *LinkSeg = (SEGMENT*)&KernelData[LinkAdr];
|
|
|
|
// AddrVtable = LinkSeg->AddrVtable;
|
|
|
|
// SizeVtable = LinkSeg->SizeVtable;
|
|
|
|
// NamesTable = LinkSeg->AddrNames;
|
2020-05-01 18:26:28 +02:00
|
|
|
//TODO find an origin of the shift
|
2020-07-20 10:52:36 +02:00
|
|
|
shift = NamesTable - NTabble;
|
2020-05-02 05:38:38 +02:00
|
|
|
// DBG_RT("AddrVtable=%x Size=%x AddrNames=%x shift=%x\n", AddrVtable, SizeVtable, NamesTable, shift);
|
2020-05-01 18:26:28 +02:00
|
|
|
NamesTable = NTabble;
|
2020-07-20 10:52:36 +02:00
|
|
|
AddrVtable -= shift;
|
2020-06-27 04:57:27 +02:00
|
|
|
// AddrVtable = Tabble;
|
|
|
|
DBG("AddrVtable=%x Size=%x AddrNames=%x shift=%x\n", AddrVtable, SizeVtable, NamesTable, shift);
|
2020-07-17 22:45:01 +02:00
|
|
|
SegVAddr = FindBin(KernelData+KernelOffset, 0x600, (const UINT8 *)kTextSegment, (UINT32)strlen(kTextSegment));
|
|
|
|
SegVAddr += KernelOffset;
|
2020-06-27 04:57:27 +02:00
|
|
|
DBG("SegVAddr=0x%x\n", SegVAddr);
|
2020-05-01 18:26:28 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-23 21:14:20 +02:00
|
|
|
UINT32 LOADER_ENTRY::searchSectionByNum(UINT8 * binary, UINT32 Num)
|
|
|
|
{
|
|
|
|
UINT32 ncmds, cmdsize;
|
|
|
|
UINT32 binaryIndex;
|
|
|
|
UINT32 currsect = 0;
|
|
|
|
UINT32 nsect;
|
2020-08-09 17:55:30 +02:00
|
|
|
UINT32 textAddr = 0; // Init to avoid warning
|
2020-07-23 21:14:20 +02:00
|
|
|
struct segment_command_64 *loadCommand;
|
|
|
|
// struct symtab_command *symCmd;
|
|
|
|
if (!Num) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ncmds = MACH_GET_NCMDS(binary);
|
2020-07-25 08:36:00 +02:00
|
|
|
binaryIndex = sizeof(struct mach_header_64); //20
|
|
|
|
DBG("segSize=0x%lx secsize=0x%lx\n", sizeof(struct segment_command_64), sizeof(struct section_64)); //48, 50
|
2020-07-23 21:14:20 +02:00
|
|
|
|
|
|
|
for (UINTN cnt = 0; cnt < ncmds; cnt++) {
|
2020-07-25 08:36:00 +02:00
|
|
|
loadCommand = (struct segment_command_64 *)(binary + binaryIndex); //20, 158
|
|
|
|
cmdsize = loadCommand->cmdsize; //138, 278
|
2020-07-23 21:14:20 +02:00
|
|
|
|
|
|
|
switch (loadCommand->cmd) {
|
|
|
|
case LC_SEGMENT_64:
|
2020-07-25 08:36:00 +02:00
|
|
|
nsect = loadCommand->nsects; //3, 7,
|
2020-07-23 21:14:20 +02:00
|
|
|
if (currsect == 0) {
|
2020-07-25 08:36:00 +02:00
|
|
|
textAddr = binaryIndex + sizeof(struct segment_command_64); //20+48=68
|
2020-07-23 21:14:20 +02:00
|
|
|
}
|
2020-07-25 08:36:00 +02:00
|
|
|
if (currsect + nsect >= Num - 1) { //3+7 >= 9
|
|
|
|
UINT32 sectAddr = binaryIndex + sizeof(struct segment_command_64) + sizeof(struct section_64) * (Num - currsect - 1);
|
|
|
|
//158+48+50*
|
2020-07-23 21:14:20 +02:00
|
|
|
if (*(UINT32*)(binary + sectAddr) == 0x73625F5F) { //special case for __bss
|
|
|
|
DBG("__bss will be used as __text\n");
|
|
|
|
return textAddr;
|
|
|
|
}
|
|
|
|
return sectAddr;
|
|
|
|
}
|
2020-07-25 08:36:00 +02:00
|
|
|
currsect += nsect; //3
|
2020-07-23 21:14:20 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-07-25 08:36:00 +02:00
|
|
|
binaryIndex += cmdsize; //20+138=158,
|
2020-07-23 21:14:20 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN LOADER_ENTRY::searchProcInDriver(UINT8 * driver, UINT32 driverLen, const XString8& procedure)
|
2020-05-03 19:16:50 +02:00
|
|
|
{
|
2020-08-12 17:15:47 +02:00
|
|
|
if (procedure.isEmpty()) {
|
2020-05-03 19:16:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-22 22:23:38 +02:00
|
|
|
/*
|
2020-05-03 19:16:50 +02:00
|
|
|
INT32 LinkAdr = FindBin(driver, driverLen, (const UINT8 *)kLinkEditSegment, (UINT32)strlen(kLinkEditSegment));
|
|
|
|
if (LinkAdr == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SEGMENT *LinkSeg = (SEGMENT*)&driver[LinkAdr];
|
|
|
|
// INT32 lAddrVtable = LinkSeg->AddrVtable;
|
|
|
|
INT32 lSizeVtable = LinkSeg->SizeVtable;
|
|
|
|
// INT32 lNamesTable = LinkSeg->AddrNames;
|
|
|
|
const char* Names = (const char*)(&driver[LinkSeg->AddrNames]);
|
|
|
|
VTABLE * vArray = (VTABLE*)(&driver[LinkSeg->AddrVtable]);
|
2020-07-23 21:14:20 +02:00
|
|
|
struct nlist_64 {
|
|
|
|
union {
|
|
|
|
uint32_t n_strx; // index into the string table //str_adr=stroff+n_strx
|
|
|
|
} n_un;
|
|
|
|
uint8_t n_type; // type flag, see below
|
|
|
|
uint8_t n_sect; // section number or NO_SECT
|
|
|
|
uint16_t n_desc; // see <mach-o/stab.h>
|
|
|
|
uint64_t n_value; // value of this symbol (or stab offset)
|
|
|
|
};
|
|
|
|
|
2020-07-22 22:23:38 +02:00
|
|
|
*/
|
2020-08-12 17:15:47 +02:00
|
|
|
DBG("search procedure %s\n", procedure.c_str());
|
2020-07-23 21:14:20 +02:00
|
|
|
struct nlist_64 * vArray = NULL;
|
2020-07-22 22:23:38 +02:00
|
|
|
INT32 lSizeVtable = 0;
|
|
|
|
const char* Names = NULL;
|
|
|
|
struct symtab_command *symCmd = NULL;
|
2020-07-24 20:59:51 +02:00
|
|
|
UINT32 symCmdOffset = Get_Symtab(driver);
|
2020-07-27 10:45:17 +02:00
|
|
|
DBG("symCmdOffset=0x%X\n", symCmdOffset); //0x418
|
2020-07-23 21:14:20 +02:00
|
|
|
if (symCmdOffset != 0) {
|
2020-07-26 15:44:38 +02:00
|
|
|
if ((((struct mach_header_64*)KernelData)->filetype) == MH_KERNEL_COLLECTION) {
|
2020-07-27 10:45:17 +02:00
|
|
|
symCmd = (struct symtab_command *)&driver[symCmdOffset];
|
|
|
|
vArray = (struct nlist_64*)(&KernelData[symCmd->symoff - shift]);
|
2020-07-26 15:44:38 +02:00
|
|
|
lSizeVtable = symCmd->nsyms;
|
2020-07-27 10:45:17 +02:00
|
|
|
Names = (const char*)(&KernelData[symCmd->stroff - shift]);
|
|
|
|
DBG("driverKC: AddrVtable=0x%x SizeVtable=0x%x NamesTable=0x%x\n",
|
|
|
|
symCmd->symoff - shift, lSizeVtable, symCmd->stroff - shift);
|
2020-07-26 15:44:38 +02:00
|
|
|
} else {
|
|
|
|
symCmd = (struct symtab_command *)&driver[symCmdOffset];
|
|
|
|
vArray = (struct nlist_64*)(&driver[symCmd->symoff]);
|
|
|
|
lSizeVtable = symCmd->nsyms;
|
|
|
|
Names = (const char*)(&driver[symCmd->stroff]);
|
|
|
|
DBG("driver: AddrVtable=0x%x SizeVtable=0x%x NamesTable=0x%x\n", symCmd->symoff, lSizeVtable, symCmd->stroff);
|
|
|
|
}
|
2020-07-22 22:23:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!vArray || !lSizeVtable || !Names) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-03 19:16:50 +02:00
|
|
|
INT32 i;
|
2020-08-09 17:55:30 +02:00
|
|
|
UINT32 Offset = 0; // Init to avoid warning
|
2020-05-03 19:16:50 +02:00
|
|
|
bool found = false;
|
|
|
|
for (i = 0; i < lSizeVtable; ++i) {
|
2020-07-23 21:14:20 +02:00
|
|
|
Offset = vArray[i].n_un.n_strx;
|
2020-08-12 17:15:47 +02:00
|
|
|
if (strstr(&Names[Offset], procedure.c_str())) {
|
2020-05-03 19:16:50 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2020-08-12 17:15:47 +02:00
|
|
|
DBG("%s not found\n", procedure.c_str());
|
2020-05-03 19:16:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-23 21:14:20 +02:00
|
|
|
DBG("found section %d at pos=%d\n", vArray[i].n_sect, i);
|
2020-07-27 15:09:30 +02:00
|
|
|
DBG("name offset=0x%x vtable_off=0x%lx\n", symCmd->stroff + Offset, symCmd->symoff + i * sizeof(struct nlist_64));
|
2020-07-25 08:36:00 +02:00
|
|
|
// INT32 textAddr = searchSectionByNum(driver, 1);
|
2020-07-23 21:14:20 +02:00
|
|
|
INT32 lSegVAddr = searchSectionByNum(driver, vArray[i].n_sect);
|
2020-07-27 15:09:30 +02:00
|
|
|
// DBG("section begin:\n");
|
|
|
|
// for (int j=0; j<20; ++j) {
|
|
|
|
// DBG("%02X", driver[lSegVAddr+j]);
|
|
|
|
// }
|
|
|
|
// DBG("\n");
|
2020-07-25 08:36:00 +02:00
|
|
|
|
2020-07-23 21:14:20 +02:00
|
|
|
/*
|
2020-05-03 19:16:50 +02:00
|
|
|
switch (vArray[i].Seg) {
|
|
|
|
case ID_SEG_DATA:
|
2020-05-14 12:39:50 +02:00
|
|
|
lSegVAddr = FindBin(driver, 0x1600, (const UINT8 *)kDataSegment, (UINT32)strlen(kDataSegment));
|
2020-05-03 19:16:50 +02:00
|
|
|
break;
|
|
|
|
case ID_SEG_DATA_CONST:
|
2020-05-30 18:49:12 +02:00
|
|
|
case ID_SEC_CONST:
|
2020-05-27 20:04:31 +02:00
|
|
|
lSegVAddr = FindSection(driver, 0x1600, (const UINT8 *)kDataSegment, (const UINT8 *)kConstSection);
|
|
|
|
break;
|
|
|
|
case ID_SEG_TEXT_CONST:
|
|
|
|
lSegVAddr = FindSection(driver, 0x1600, (const UINT8 *)kTextSegment, (const UINT8 *)kConstSection);
|
|
|
|
break;
|
|
|
|
case ID_SEG_DATA_COMMON:
|
|
|
|
lSegVAddr = FindSection(driver, 0x1600, (const UINT8 *)kDataSegment, (const UINT8 *)kCommonSection);
|
|
|
|
break;
|
|
|
|
case ID_SEG_DATA_DATA2:
|
|
|
|
case ID_SEG_DATA_DATA:
|
|
|
|
lSegVAddr = FindSection(driver, 0x1600, (const UINT8 *)kDataSegment, (const UINT8 *)kDataSection);
|
2020-05-03 19:16:50 +02:00
|
|
|
break;
|
|
|
|
case ID_SEG_KLD:
|
|
|
|
case ID_SEG_KLD2:
|
2020-07-17 22:45:01 +02:00
|
|
|
case ID_SEG_KLD3:
|
2020-05-14 12:39:50 +02:00
|
|
|
lSegVAddr = FindBin(driver, 0x2000, (const UINT8 *)kKldSegment, (UINT32)strlen(kKldSegment));
|
2020-05-03 19:16:50 +02:00
|
|
|
break;
|
2020-05-27 20:04:31 +02:00
|
|
|
// case ID_SEC_BSS:
|
|
|
|
// lSegVAddr = FindSection(driver, 0x1600, (const UINT8 *)kDataSegment, (const UINT8 *)kBssSection);
|
|
|
|
// break;
|
|
|
|
case ID_SEC_BSS: //it works this way
|
2020-05-03 19:16:50 +02:00
|
|
|
case ID_SEG_TEXT:
|
2020-07-22 22:23:38 +02:00
|
|
|
case ID_SEG_TEXT2:
|
2020-05-27 20:04:31 +02:00
|
|
|
lSegVAddr = FindSection(driver, 0x600, (const UINT8 *)kTextSegment, (const UINT8 *)kPrelinkTextSection);
|
2020-05-03 19:16:50 +02:00
|
|
|
break;
|
2020-07-20 10:52:36 +02:00
|
|
|
case ID_SEG_HIB:
|
|
|
|
lSegVAddr = FindBin(driver, 0x2000, (const UINT8 *)kHibSegment, (UINT32)strlen(kHibSegment));
|
|
|
|
break;
|
2020-05-27 20:04:31 +02:00
|
|
|
|
|
|
|
// lSegVAddr = FindBin(driver, 0x600, (const UINT8 *)kTextSegment, (UINT32)strlen(kTextSegment));
|
|
|
|
// break;
|
|
|
|
default:
|
|
|
|
return vArray[i].ProcAddr;
|
|
|
|
|
2020-05-03 19:16:50 +02:00
|
|
|
}
|
2020-05-14 12:39:50 +02:00
|
|
|
if (lSegVAddr == 0) {
|
|
|
|
lSegVAddr = 0x38;
|
2020-05-03 19:16:50 +02:00
|
|
|
}
|
2020-05-14 12:39:50 +02:00
|
|
|
SEGMENT *TextSeg = (SEGMENT*)&driver[lSegVAddr];
|
2020-07-23 21:14:20 +02:00
|
|
|
*/
|
|
|
|
struct section_64 *TextSeg = (struct section_64*)&driver[lSegVAddr];
|
|
|
|
UINT64 Absolut = TextSeg->addr;
|
|
|
|
UINT64 FileOff = TextSeg->offset;
|
2020-07-22 22:23:38 +02:00
|
|
|
DBG("Absolut=0x%llx Fileoff=0x%llx\n", Absolut, FileOff);
|
2020-07-23 21:14:20 +02:00
|
|
|
UINTN procAddr = vArray[i].n_value - Absolut + FileOff;
|
2020-07-25 08:36:00 +02:00
|
|
|
// UINT32 procAddr32 = (UINT32)(vArray[i].n_value); //it is not work
|
2020-07-27 15:09:30 +02:00
|
|
|
if ((((struct mach_header_64*)KernelData)->filetype) == MH_KERNEL_COLLECTION) {
|
|
|
|
procAddr -= shift;
|
|
|
|
}
|
2020-07-23 21:14:20 +02:00
|
|
|
DBG("procAddr=0x%llx\n", procAddr);
|
2020-07-25 08:36:00 +02:00
|
|
|
#if KERNEL_DEBUG
|
|
|
|
if (Absolut != 0) {
|
2020-07-27 15:09:30 +02:00
|
|
|
UINT8 *procVM;
|
|
|
|
if ((((struct mach_header_64*)KernelData)->filetype) == MH_KERNEL_COLLECTION) {
|
|
|
|
procVM = (UINT8*)&KernelData[procAddr];
|
|
|
|
} else {
|
|
|
|
procVM = (UINT8*)&driver[procAddr];
|
|
|
|
}
|
2020-07-25 08:36:00 +02:00
|
|
|
DBG("procedure begin:\n");
|
|
|
|
for (int j=0; j<30; ++j) {
|
|
|
|
DBG("%02X", procVM[j]);
|
|
|
|
}
|
|
|
|
DBG("\n");
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-03 19:16:50 +02:00
|
|
|
return procAddr;
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:52:36 +02:00
|
|
|
//static int N = 0;
|
2020-04-27 17:16:43 +02:00
|
|
|
//search a procedure by Name and return its offset in the kernel
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN LOADER_ENTRY::searchProc(const XString8& procedure)
|
2020-04-27 17:16:43 +02:00
|
|
|
{
|
2020-08-12 17:15:47 +02:00
|
|
|
if (procedure.isEmpty()) {
|
2020-04-27 17:16:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-12 17:15:47 +02:00
|
|
|
DBG("search name in kernel: %s\n", procedure.c_str());
|
2020-05-09 09:56:30 +02:00
|
|
|
const char* Names = (const char*)(&KernelData[NamesTable]);
|
|
|
|
VTABLE * vArray = (VTABLE*)(&KernelData[AddrVtable]);
|
2020-04-27 17:16:43 +02:00
|
|
|
//search for the name
|
2020-05-01 18:26:28 +02:00
|
|
|
// gBS->Stall(9000000);
|
2020-04-27 17:16:43 +02:00
|
|
|
size_t i;
|
|
|
|
bool found = false;
|
|
|
|
for (i=0; i<SizeVtable; ++i) {
|
|
|
|
size_t Offset = vArray[i].NameOffset;
|
2020-06-24 06:20:45 +02:00
|
|
|
if (Offset == 0) break;
|
2020-07-20 10:52:36 +02:00
|
|
|
// if (N < 10) {
|
|
|
|
// DBG("Offset %lx Seg=%x\n", Offset, vArray[i].Seg);
|
|
|
|
// DBG("Name to compare %s\n", &Names[Offset]);
|
|
|
|
// N++;
|
|
|
|
// }
|
2020-05-02 05:38:38 +02:00
|
|
|
// DBG_RT("Offset %lx Seg=%x\n", Offset, vArray[i].Seg);
|
|
|
|
// DBG_RT("Name to compare %s\n", &Names[Offset]);
|
|
|
|
// Stall(3000000);
|
2020-08-12 17:15:47 +02:00
|
|
|
if (AsciiStrStr(&Names[Offset], procedure.c_str()) != NULL) {
|
2020-04-27 17:16:43 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-02 05:38:38 +02:00
|
|
|
// INT32 SegVAddr;
|
|
|
|
// DBG_RT(" segment %x \n", vArray[i].Seg);
|
|
|
|
|
|
|
|
/*
|
2020-04-29 10:06:44 +02:00
|
|
|
switch (vArray[i].Seg) {
|
2020-04-29 17:07:10 +02:00
|
|
|
case ID_SEG_TEXT:
|
2020-05-01 18:26:28 +02:00
|
|
|
SegVAddr = FindBin(kernel, 0x600, (const UINT8 *)kTextSegment, (UINT32)strlen(kTextSegment));
|
2020-04-29 10:06:44 +02:00
|
|
|
break;
|
2020-04-29 17:07:10 +02:00
|
|
|
case ID_SEG_DATA:
|
2020-05-01 18:26:28 +02:00
|
|
|
SegVAddr = FindBin(kernel, 0x1600, (const UINT8 *)kDataSegment, (UINT32)strlen(kDataSegment));
|
2020-04-29 10:06:44 +02:00
|
|
|
break;
|
2020-04-29 17:07:10 +02:00
|
|
|
case ID_SEG_DATA_CONST:
|
2020-05-01 18:26:28 +02:00
|
|
|
SegVAddr = FindBin(kernel, 0x2000, (const UINT8 *)kDataConstSegment, (UINT32)strlen(kDataConstSegment));
|
2020-04-29 10:06:44 +02:00
|
|
|
break;
|
2020-04-29 17:07:10 +02:00
|
|
|
case ID_SEG_KLD:
|
|
|
|
case ID_SEG_KLD2:
|
2020-05-01 18:26:28 +02:00
|
|
|
SegVAddr = FindBin(kernel, 0x2000, (const UINT8 *)kKldSegment, (UINT32)strlen(kKldSegment));
|
2020-04-29 10:06:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-05-02 05:38:38 +02:00
|
|
|
return 0;
|
2020-04-28 12:49:24 +02:00
|
|
|
}
|
2020-05-02 05:38:38 +02:00
|
|
|
*/
|
2020-05-09 09:56:30 +02:00
|
|
|
SEGMENT *TextSeg = (SEGMENT*)&KernelData[SegVAddr];
|
2020-05-02 05:38:38 +02:00
|
|
|
UINT64 Absolut = TextSeg->SegAddress; //KLD=C70000
|
|
|
|
UINT64 FileOff = TextSeg->fileoff; //950000
|
2020-04-29 10:06:44 +02:00
|
|
|
UINT64 procAddr = vArray[i].ProcAddr - Absolut + FileOff;
|
2020-05-05 21:15:59 +02:00
|
|
|
/*
|
2020-04-27 17:16:43 +02:00
|
|
|
UINT64 prevAddr;
|
|
|
|
if (i == 0) {
|
|
|
|
prevAddr = Absolut;
|
|
|
|
} else {
|
|
|
|
prevAddr = vArray[i-1].ProcAddr;
|
|
|
|
}
|
2020-04-29 10:06:44 +02:00
|
|
|
*procLen = vArray[i].ProcAddr - prevAddr; //never worked
|
2020-05-05 21:15:59 +02:00
|
|
|
*/
|
2020-07-23 21:14:20 +02:00
|
|
|
DBG("kernel: procAddr=0x%llx\n", procAddr);
|
2020-04-27 17:16:43 +02:00
|
|
|
return procAddr;
|
|
|
|
}
|
|
|
|
|
2020-05-09 10:55:33 +02:00
|
|
|
#if 0
|
2019-09-03 11:58:42 +02:00
|
|
|
//TimeWalker - extended and corrected for systems up to Yosemite
|
2020-05-01 18:26:28 +02:00
|
|
|
//TODO - Slice: no more needed
|
2020-05-09 09:56:30 +02:00
|
|
|
VOID LOADER_ENTRY::KernelPatcher_64()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8 *bytes = KernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 patchLocation=0, patchLocation1=0;
|
|
|
|
UINT32 i;
|
|
|
|
UINT32 switchaddr=0;
|
|
|
|
UINT32 mask_family=0, mask_model=0;
|
|
|
|
UINT32 cpuid_family_addr=0, cpuid_model_addr=0;
|
|
|
|
UINT64 os_version;
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "Looking for _cpuid_set_info _panic ...\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Determine location of _cpuid_set_info _panic call for reference
|
|
|
|
// basically looking for info_p->cpuid_model = bitfield32(reg[eax], 7, 4);
|
|
|
|
for (i=0; i<0x1000000; i++) {
|
|
|
|
if (bytes[i+ 0] == 0xC7 && bytes[i+ 1] == 0x05 && bytes[i+ 5] == 0x00 &&
|
|
|
|
bytes[i+ 6] == 0x07 && bytes[i+ 7] == 0x00 && bytes[i+ 8] == 0x00 && bytes[i+ 9] == 0x00 &&
|
|
|
|
bytes[i-5] == 0xE8) { // matching 0xE8 for _panic call start
|
|
|
|
patchLocation = i-5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation) {
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "_cpuid_set_info Unsupported CPU _panic not found \n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
os_version = AsciiOSVersionToUint64(OSVersion);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// make sure only kernels for OSX 10.6.0 to 10.7.3 are being patched by this approach
|
|
|
|
if (os_version >= AsciiOSVersionToUint64("10.6") && os_version <= AsciiOSVersionToUint64("10.7.3")) {
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "will patch kernel for macOS 10.6.0 to 10.7.3\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// remove tsc_init: unknown CPU family panic for kernels prior to 10.6.2 which still had Atom support
|
|
|
|
if (os_version < AsciiOSVersionToUint64("10.6.2")) {
|
|
|
|
for (i=0; i<0x1000000; i++) {
|
|
|
|
// find _tsc_init panic address by byte sequence 488d3df4632a00
|
|
|
|
if (bytes[i] == 0x48 && bytes[i+1] == 0x8D && bytes[i+2] == 0x3D && bytes[i+3] == 0xF4 &&
|
|
|
|
bytes[i+4] == 0x63 && bytes[i+5] == 0x2A && bytes[i+6] == 0x00) {
|
|
|
|
patchLocation1 = i+9;
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "Found _tsc_init _panic address at 0x%08x\n",patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOP _panic call
|
|
|
|
if (patchLocation1) {
|
|
|
|
bytes[patchLocation1 + 0] = 0x90;
|
|
|
|
bytes[patchLocation1 + 1] = 0x90;
|
|
|
|
bytes[patchLocation1 + 2] = 0x90;
|
|
|
|
bytes[patchLocation1 + 3] = 0x90;
|
|
|
|
bytes[patchLocation1 + 4] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // assume patching logic for OSX 10.6.2 to 10.7.3
|
|
|
|
|
|
|
|
/*
|
|
|
|
Here is our case from CPUID switch statement, it sets CPUFAMILY_UNKNOWN
|
|
|
|
C7051C2C5F0000000000 mov dword [ds:0xffffff80008a22c0], 0x0 (example from 10.7)
|
|
|
|
*/
|
|
|
|
switchaddr = patchLocation - 19;
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "switch statement patch location is 0x%08x\n", (switchaddr+6));
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (bytes[switchaddr + 0] == 0xC7 && bytes[switchaddr + 1] == 0x05 &&
|
|
|
|
bytes[switchaddr + 5] == 0x00 && bytes[switchaddr + 6] == 0x00 &&
|
|
|
|
bytes[switchaddr + 7] == 0x00 && bytes[switchaddr + 8] == 0x00) {
|
|
|
|
|
|
|
|
// Determine cpuid_family address from above mov operation
|
|
|
|
cpuid_family_addr =
|
|
|
|
bytes[switchaddr + 2] << 0 |
|
|
|
|
bytes[switchaddr + 3] << 8 |
|
|
|
|
bytes[switchaddr + 4] << 16 |
|
|
|
|
bytes[switchaddr + 5] << 24;
|
|
|
|
cpuid_family_addr = cpuid_family_addr + (switchaddr + 10);
|
|
|
|
|
|
|
|
if (cpuid_family_addr) {
|
|
|
|
|
|
|
|
// Determine cpuid_model address
|
|
|
|
// for 10.6.2 kernels it's offset by 299 bytes from cpuid_family address
|
|
|
|
if (os_version == AsciiOSVersionToUint64("10.6.2")) {
|
|
|
|
cpuid_model_addr = cpuid_family_addr - 0X12B;
|
|
|
|
}
|
|
|
|
// for 10.6.3 to 10.6.7 it's offset by 303 bytes
|
|
|
|
else if (os_version <= AsciiOSVersionToUint64("10.6.7")) {
|
|
|
|
cpuid_model_addr = cpuid_family_addr - 0X12F;
|
|
|
|
}
|
|
|
|
// for 10.6.8 to 10.7.3 kernels - by 339 bytes
|
|
|
|
else {
|
|
|
|
cpuid_model_addr = cpuid_family_addr - 0X153;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "cpuid_family address: 0x%08x\n", cpuid_family_addr);
|
|
|
|
// DBG_RT( "cpuid_model address: 0x%08x\n", cpuid_model_addr);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
switchaddr += 6; // offset 6 bytes in mov operation to write a dword instead of zero
|
|
|
|
|
|
|
|
// calculate mask for patching, cpuid_family mask not needed as we offset on a valid mask
|
|
|
|
mask_model = cpuid_model_addr - (switchaddr+14);
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "model mask 0x%08x\n", mask_model);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "overriding cpuid_family and cpuid_model as CPUID_INTEL_PENRYN\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
bytes[switchaddr+0] = (CPUFAMILY_INTEL_PENRYN & 0x000000FF) >> 0;
|
|
|
|
bytes[switchaddr+1] = (CPUFAMILY_INTEL_PENRYN & 0x0000FF00) >> 8;
|
|
|
|
bytes[switchaddr+2] = (CPUFAMILY_INTEL_PENRYN & 0x00FF0000) >> 16;
|
|
|
|
bytes[switchaddr+3] = (CPUFAMILY_INTEL_PENRYN & 0xFF000000) >> 24;
|
|
|
|
|
|
|
|
// mov dword [ds:0xffffff80008a216d], 0x2000117
|
|
|
|
bytes[switchaddr+4] = 0xC7;
|
|
|
|
bytes[switchaddr+5] = 0x05;
|
|
|
|
bytes[switchaddr+6] = (UINT8)((mask_model & 0x000000FF) >> 0);
|
|
|
|
bytes[switchaddr+7] = (UINT8)((mask_model & 0x0000FF00) >> 8);
|
|
|
|
bytes[switchaddr+8] = (UINT8)((mask_model & 0x00FF0000) >> 16);
|
|
|
|
bytes[switchaddr+9] = (UINT8)((mask_model & 0xFF000000) >> 24);
|
|
|
|
bytes[switchaddr+10] = 0x17; // cpuid_model (Penryn)
|
|
|
|
bytes[switchaddr+11] = 0x01; // cpuid_extmodel
|
|
|
|
bytes[switchaddr+12] = 0x00; // cpuid_extfamily
|
|
|
|
bytes[switchaddr+13] = 0x02; // cpuid_stepping
|
|
|
|
|
|
|
|
// fill remainder with 4 NOPs
|
|
|
|
for (i=14; i<18; i++) {
|
|
|
|
bytes[switchaddr+i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "Unable to determine cpuid_family address, patching aborted\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// patch ssse3
|
2020-05-01 18:26:28 +02:00
|
|
|
if (!SSSE3 && (AsciiStrnCmp(OSVersion,"10.6",4)==0)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
Patcher_SSE3_6((VOID*)bytes);
|
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
if (!SSSE3 && (AsciiStrnCmp(OSVersion,"10.7",4)==0)) {
|
2020-05-09 09:56:30 +02:00
|
|
|
Patcher_SSE3_7();
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all 10.7.4+ kernels share common CPUID switch statement logic,
|
|
|
|
// it needs to be exploited in diff manner due to the lack of space
|
|
|
|
else if (os_version >= AsciiOSVersionToUint64("10.7.4")) {
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "will patch kernel for macOS 10.7.4+\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Here is our switchaddress location ... it should be case 20 from CPUID switch statement
|
|
|
|
833D78945F0000 cmp dword [ds:0xffffff80008a21d0], 0x0;
|
|
|
|
7417 je 0xffffff80002a8d71
|
|
|
|
*/
|
|
|
|
switchaddr = patchLocation-45;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "switch statement patch location is 0x%08x\n", switchaddr);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if(bytes[switchaddr + 0] == 0x83 && bytes[switchaddr + 1] == 0x3D &&
|
|
|
|
bytes[switchaddr + 5] == 0x00 && bytes[switchaddr + 6] == 0x00 &&
|
|
|
|
bytes[switchaddr + 7] == 0x74) {
|
|
|
|
|
|
|
|
// Determine cpuid_family address
|
|
|
|
// 891D4F945F00 mov dword [ds:0xffffff80008a21a0], ebx
|
|
|
|
cpuid_family_addr =
|
|
|
|
bytes[switchaddr - 4] << 0 |
|
|
|
|
bytes[switchaddr - 3] << 8 |
|
|
|
|
bytes[switchaddr - 2] << 16 |
|
|
|
|
bytes[switchaddr - 1] << 24;
|
|
|
|
cpuid_family_addr = cpuid_family_addr + switchaddr;
|
|
|
|
|
|
|
|
if (cpuid_family_addr) {
|
|
|
|
|
|
|
|
// Determine cpuid_model address
|
|
|
|
// for 10.6.8+ kernels it's 339 bytes apart from cpuid_family address
|
|
|
|
cpuid_model_addr = cpuid_family_addr - 0X153;
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "cpuid_family address: 0x%08x\n", cpuid_family_addr);
|
|
|
|
DBG_RT( "cpuid_model address: 0x%08x\n", cpuid_model_addr);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Calculate masks for patching
|
|
|
|
mask_family = cpuid_family_addr - (switchaddr +15);
|
|
|
|
mask_model = cpuid_model_addr - (switchaddr +25);
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nfamily mask: 0x%08x \nmodel mask: 0x%08x\n", mask_family, mask_model);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// retain original
|
|
|
|
// test ebx, ebx
|
|
|
|
bytes[switchaddr+0] = bytes[patchLocation-13];
|
|
|
|
bytes[switchaddr+1] = bytes[patchLocation-12];
|
|
|
|
// retain original, but move jump offset by 20 bytes forward
|
|
|
|
// jne for above test
|
|
|
|
bytes[switchaddr+2] = bytes[patchLocation-11];
|
|
|
|
bytes[switchaddr+3] = bytes[patchLocation-10]+0x20;
|
|
|
|
// mov ebx, 0x78ea4fbc
|
|
|
|
bytes[switchaddr+4] = 0xBB;
|
|
|
|
bytes[switchaddr+5] = (CPUFAMILY_INTEL_PENRYN & 0x000000FF) >> 0;
|
|
|
|
bytes[switchaddr+6] = (CPUFAMILY_INTEL_PENRYN & 0x0000FF00) >> 8;
|
|
|
|
bytes[switchaddr+7] = (CPUFAMILY_INTEL_PENRYN & 0x00FF0000) >> 16;
|
|
|
|
bytes[switchaddr+8] = (CPUFAMILY_INTEL_PENRYN & 0xFF000000) >> 24;
|
|
|
|
|
|
|
|
// mov dword, ebx
|
|
|
|
bytes[switchaddr+9] = 0x89;
|
|
|
|
bytes[switchaddr+10] = 0x1D;
|
|
|
|
// cpuid_cpufamily address 0xffffff80008a21a0
|
|
|
|
bytes[switchaddr+11] = (UINT8)((mask_family & 0x000000FF) >> 0);
|
|
|
|
bytes[switchaddr+12] = (UINT8)((mask_family & 0x0000FF00) >> 8);
|
|
|
|
bytes[switchaddr+13] = (UINT8)((mask_family & 0x00FF0000) >> 16);
|
|
|
|
bytes[switchaddr+14] = (UINT8)((mask_family & 0xFF000000) >> 24);
|
|
|
|
|
|
|
|
// mov dword
|
|
|
|
bytes[switchaddr+15] = 0xC7;
|
|
|
|
bytes[switchaddr+16] = 0x05;
|
|
|
|
// cpuid_model address 0xffffff80008b204d
|
|
|
|
bytes[switchaddr+17] = (UINT8)((mask_model & 0x000000FF) >> 0);
|
|
|
|
bytes[switchaddr+18] = (UINT8)((mask_model & 0x0000FF00) >> 8);
|
|
|
|
bytes[switchaddr+19] = (UINT8)((mask_model & 0x00FF0000) >> 16);
|
|
|
|
bytes[switchaddr+20] = (UINT8)((mask_model & 0xFF000000) >> 24);
|
|
|
|
|
|
|
|
bytes[switchaddr+21] = 0x17; // cpuid_model
|
|
|
|
bytes[switchaddr+22] = 0x01; // cpuid_extmodel
|
|
|
|
bytes[switchaddr+23] = 0x00; // cpuid_extfamily
|
|
|
|
bytes[switchaddr+24] = 0x02; // cpuid_stepping
|
|
|
|
|
|
|
|
// fill remainder with 25 NOPs
|
|
|
|
for (i=25; i<25+25; i++) {
|
|
|
|
bytes[switchaddr+i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Unable to determine cpuid_family address, patching aborted\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
VOID LOADER_ENTRY::KernelPatcher_32()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8* bytes = KernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 patchLocation=0, patchLocation1=0;
|
|
|
|
UINT32 i;
|
|
|
|
UINT32 jumpaddr;
|
|
|
|
|
|
|
|
DBG("Found _cpuid_set_info _panic Start\n");
|
|
|
|
// _cpuid_set_info _panic address
|
|
|
|
for (i=0; i<0x1000000; i++) {
|
|
|
|
if (bytes[i] == 0xC7 && bytes[i+1] == 0x05 && bytes[i+6] == 0x07 && bytes[i+7] == 0x00 &&
|
|
|
|
bytes[i+8] == 0x00 && bytes[i+9] == 0x00 && bytes[i+10] == 0xC7 && bytes[i+11] == 0x05 &&
|
|
|
|
bytes[i-5] == 0xE8) {
|
|
|
|
patchLocation = i-5;
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("Found _cpuid_set_info _panic address at 0x%08X\n",patchLocation);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation) {
|
2020-03-26 13:59:20 +01:00
|
|
|
DBG("Can't find _cpuid_set_info _panic address, patch kernel abort.\n"/*,i*/);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this for 10.6.0 and 10.6.1 kernel and remove tsc.c unknow cpufamily panic
|
|
|
|
// c70424540e5900
|
|
|
|
// find _tsc_init panic address
|
|
|
|
for (i=0; i<0x1000000; i++) {
|
|
|
|
// _cpuid_set_info _panic address
|
|
|
|
if (bytes[i] == 0xC7 && bytes[i+1] == 0x04 && bytes[i+2] == 0x24 &&
|
|
|
|
bytes[i+3] == 0x54 && bytes[i+4] == 0x0E && bytes[i+5] == 0x59 &&
|
|
|
|
bytes[i+6] == 0x00) {
|
|
|
|
patchLocation1 = i+7;
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("Found _tsc_init _panic address at 0x%08X\n",patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// found _tsc_init panic addres and patch it
|
|
|
|
if (patchLocation1) {
|
|
|
|
bytes[patchLocation1 + 0] = 0x90;
|
|
|
|
bytes[patchLocation1 + 1] = 0x90;
|
|
|
|
bytes[patchLocation1 + 2] = 0x90;
|
|
|
|
bytes[patchLocation1 + 3] = 0x90;
|
|
|
|
bytes[patchLocation1 + 4] = 0x90;
|
|
|
|
}
|
|
|
|
// end tsc.c panic
|
|
|
|
|
|
|
|
//first move panic code total 5 bytes, if patch cpuid fail still can boot with kernel
|
|
|
|
bytes[patchLocation + 0] = 0x90;
|
|
|
|
bytes[patchLocation + 1] = 0x90;
|
|
|
|
bytes[patchLocation + 2] = 0x90;
|
|
|
|
bytes[patchLocation + 3] = 0x90;
|
|
|
|
bytes[patchLocation + 4] = 0x90;
|
|
|
|
|
|
|
|
jumpaddr = patchLocation;
|
|
|
|
|
|
|
|
for (i=0;i<500;i++) {
|
|
|
|
if (bytes[jumpaddr-i-3] == 0x85 && bytes[jumpaddr-i-2] == 0xC0 &&
|
|
|
|
bytes[jumpaddr-i-1] == 0x75 ) {
|
|
|
|
jumpaddr -= i;
|
|
|
|
bytes[jumpaddr-1] = 0x77;
|
|
|
|
if(bytes[patchLocation - 17] == 0xC7)
|
|
|
|
bytes[jumpaddr] -=10;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jumpaddr == patchLocation) {
|
|
|
|
DBG("Can't Found jumpaddr address.\n");
|
|
|
|
return; //can't find jump location
|
|
|
|
}
|
|
|
|
// patch info_p->cpufamily to CPUFAMILY_INTEL_MEROM
|
|
|
|
|
|
|
|
if (bytes[patchLocation - 17] == 0xC7) {
|
|
|
|
bytes[patchLocation - 11] = (CPUFAMILY_INTEL_MEROM & 0x000000FF) >> 0;
|
|
|
|
bytes[patchLocation - 10] = (CPUFAMILY_INTEL_MEROM & 0x0000FF00) >> 8;
|
|
|
|
bytes[patchLocation - 9] = (CPUFAMILY_INTEL_MEROM & 0x00FF0000) >> 16;
|
|
|
|
bytes[patchLocation - 8] = (CPUFAMILY_INTEL_MEROM & 0xFF000000) >> 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
//patch info->cpuid_cpufamily
|
|
|
|
bytes[patchLocation - 7] = 0xC7;
|
|
|
|
bytes[patchLocation - 6] = 0x05;
|
|
|
|
bytes[patchLocation - 5] = bytes[jumpaddr + 3];
|
|
|
|
bytes[patchLocation - 4] = bytes[jumpaddr + 4];
|
|
|
|
bytes[patchLocation - 3] = bytes[jumpaddr + 5];
|
|
|
|
bytes[patchLocation - 2] = bytes[jumpaddr + 6];
|
|
|
|
|
|
|
|
bytes[patchLocation - 1] = CPUIDFAMILY_DEFAULT; //cpuid_family need alway set 0x06
|
2020-05-01 18:26:28 +02:00
|
|
|
bytes[patchLocation + 0] = CPU_MODEL_MEROM; //cpuid_model set CPU_MODEL_MEROM
|
2019-09-03 11:58:42 +02:00
|
|
|
bytes[patchLocation + 1] = 0x01; //cpuid_extmodel alway set 0x01
|
|
|
|
bytes[patchLocation + 2] = 0x00; //cpuid_extfamily alway set 0x00
|
|
|
|
bytes[patchLocation + 3] = 0x90;
|
|
|
|
bytes[patchLocation + 4] = 0x90;
|
|
|
|
|
|
|
|
if (OSVersion) {
|
|
|
|
if (AsciiStrnCmp(OSVersion,"10.7",4)==0) return;
|
|
|
|
|
|
|
|
if (!SSSE3 && (AsciiStrnCmp(OSVersion,"10.6",4)==0)) {
|
|
|
|
Patcher_SSE3_6((VOID*)bytes);
|
|
|
|
}
|
|
|
|
if (!SSSE3 && (AsciiStrnCmp(OSVersion,"10.5",4)==0)) {
|
|
|
|
Patcher_SSE3_5((VOID*)bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 10:55:33 +02:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
//Slice - FakeCPUID substitution, (c)2014
|
2020-05-06 08:21:30 +02:00
|
|
|
// _cpuid_set_info
|
2020-05-01 18:26:28 +02:00
|
|
|
//TODO remake to patterns
|
2019-09-03 11:58:42 +02:00
|
|
|
//procedure location
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 StrCpuid1_tigLeo[] = {0xb9, 0x01, 0x00, 0x00, 0x00, 0x89, 0xc8, 0x0f, 0xa2};
|
|
|
|
const UINT8 StrCpuid1_snowLeo[] = {0xb8, 0x01, 0x00, 0x00, 0x00, 0x31, 0xdb, 0x89, 0xd9, 0x89, 0xda, 0x0f, 0xa2};
|
|
|
|
const UINT8 StrMsr8b[] = {0xb9, 0x8b, 0x00, 0x00, 0x00, 0x0f, 0x32};
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2019-12-22 12:45:50 +01:00
|
|
|
// Tiger/Leopard/Snow Leopard
|
2019-09-03 11:58:42 +02:00
|
|
|
/*
|
|
|
|
This patch searches
|
|
|
|
and eax, 0xf0 || and eax, 0x0f0000
|
|
|
|
shr eax, 0x04 || shr eax, 0x10
|
|
|
|
and replaces to
|
|
|
|
mov eax, FakeModel | mov eax, FakeExt
|
|
|
|
*/
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 TigLeoSLSearchModel[] = {0x25, 0xf0, 0x00, 0x00, 0x00, 0xc1, 0xe8, 0x04};
|
|
|
|
const UINT8 TigLeoSLSearchExt[] = {0x25, 0x00, 0x00, 0x0f, 0x00, 0xc1, 0xe8, 0x10};
|
|
|
|
const UINT8 TigLeoSLReplaceModel[] = {0xb8, 0x07, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90};
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Lion
|
|
|
|
/*
|
|
|
|
This patch searches
|
|
|
|
mov ecx, eax
|
|
|
|
shr ecx, 0x04 || shr ecx, 0x10
|
|
|
|
and replaces to
|
|
|
|
mov ecx, FakeModel || mov ecx, FakeExt
|
|
|
|
*/
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 LionSearchModel[] = {0x89, 0xc1, 0xc1, 0xe9, 0x04};
|
|
|
|
const UINT8 LionSearchExt[] = {0x89, 0xc1, 0xc1, 0xe9, 0x10};
|
|
|
|
const UINT8 LionReplaceModel[] = {0xb9, 0x07, 0x00, 0x00, 0x00};
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Mountain Lion/Mavericks
|
|
|
|
/*
|
|
|
|
This patch searches
|
|
|
|
mov bl, al || shr eax, 0x10
|
|
|
|
shr bl, 0x04 || and al,0x0f
|
|
|
|
and replaces to
|
|
|
|
mov ebx, FakeModel || mov eax, FakeExt
|
|
|
|
*/
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 MLMavSearchModel[] = {0x88, 0xc3, 0xc0, 0xeb, 0x04};
|
|
|
|
const UINT8 MLMavSearchExt[] = {0xc1, 0xe8, 0x10, 0x24, 0x0f};
|
|
|
|
const UINT8 MLMavReplaceModel[] = {0xbb, 0x0a, 0x00, 0x00, 0x00};
|
|
|
|
const UINT8 MLMavReplaceExt[] = {0xb8, 0x02, 0x00, 0x00, 0x00};
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Yosemite/El Capitan/Sierra
|
|
|
|
/*
|
|
|
|
This patch searches
|
|
|
|
mov cl, al || mov ecx, eax
|
|
|
|
shr cl, 0x04 || shr ecx, 0x10
|
|
|
|
and replaces to
|
|
|
|
mov ecx, FakeModel || mov ecx, FakeExt
|
|
|
|
*/
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 YosECSieSearchModel[] = {0x88, 0xc1, 0xc0, 0xe9, 0x04};
|
|
|
|
const UINT8 YosECSieSearchExt[] = {0x89, 0xc1, 0xc1, 0xe9, 0x10};
|
2019-09-03 11:58:42 +02:00
|
|
|
// Need to use LionReplaceModel
|
|
|
|
|
2020-05-06 08:21:30 +02:00
|
|
|
// High Sierra/Mojave @2c4baa {89 c1 c0 e9 04}
|
2019-09-03 11:58:42 +02:00
|
|
|
/*
|
|
|
|
This patch searches
|
|
|
|
mov ecx, ecx || mov ecx, eax
|
|
|
|
shr cl, 0x04 || shr ecx, 0x10
|
|
|
|
and replaces to
|
|
|
|
mov ecx, FakeModel || mov ecx, FakeExt
|
|
|
|
*/
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 HSieMojSearchModel[] = {0x89, 0xc1, 0xc0, 0xe9, 0x04};
|
2019-09-03 11:58:42 +02:00
|
|
|
// Need to use YosECSieSearchExt, LionReplaceModel
|
|
|
|
|
|
|
|
// Catalina
|
|
|
|
/*
|
|
|
|
This patch searches
|
|
|
|
mov eax, r12 || mov eax, r12
|
|
|
|
shr al, 0x4 || shr eax, 0x10
|
|
|
|
and replaces to
|
|
|
|
mov eax, FakeModel || mov eax, FakeExt
|
|
|
|
nop || nop
|
|
|
|
*/
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8 CataSearchModel[] = {0x44, 0x89, 0xE0, 0xC0, 0xE8, 0x04};
|
|
|
|
const UINT8 CataSearchExt[] = {0x44, 0x89, 0xE0, 0xC1, 0xE8, 0x10};
|
|
|
|
const UINT8 CataReplaceMovEax[] = {0xB8, 0x00, 0x00, 0x00, 0x00, 0x90}; // mov eax, val || nop
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-09 06:59:38 +02:00
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::PatchCPUID(const UINT8* Location, INT32 LenLoc,
|
2020-05-06 08:21:30 +02:00
|
|
|
const UINT8* Search4, const UINT8* Search10, const UINT8* ReplaceModel,
|
|
|
|
const UINT8* ReplaceExt, INT32 Len)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
INT32 patchLocation=0, patchLocation1=0;
|
|
|
|
INT32 Adr = 0, Num;
|
|
|
|
BOOLEAN Patched = FALSE;
|
2020-08-11 14:43:53 +02:00
|
|
|
UINT8 FakeModel = (KernelAndKextPatches.FakeCPUID >> 4) & 0x0f;
|
|
|
|
UINT8 FakeExt = (KernelAndKextPatches.FakeCPUID >> 0x10) & 0x0f;
|
2019-09-03 11:58:42 +02:00
|
|
|
for (Num = 0; Num < 2; Num++) {
|
2020-05-09 09:56:30 +02:00
|
|
|
Adr = FindBin(&KernelData[Adr], 0x800000 - Adr, Location, (UINT32)LenLoc);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Adr < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "found location at %x\n", Adr);
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation = FindBin(&KernelData[Adr], 0x100, Search4, (UINT32)Len);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (patchLocation > 0 && patchLocation < 70) {
|
|
|
|
//found
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "found Model location at %x\n", Adr + patchLocation);
|
2020-05-09 09:56:30 +02:00
|
|
|
CopyMem(&KernelData[Adr + patchLocation], ReplaceModel, Len);
|
|
|
|
KernelData[Adr + patchLocation + 1] = FakeModel;
|
|
|
|
patchLocation1 = FindBin(&KernelData[Adr], 0x100, Search10, (UINT32)Len);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (patchLocation1 > 0 && patchLocation1 < 100) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "found ExtModel location at %x\n", Adr + patchLocation1);
|
2020-05-09 09:56:30 +02:00
|
|
|
CopyMem(&KernelData[Adr + patchLocation1], ReplaceExt, Len);
|
|
|
|
KernelData[Adr + patchLocation1 + 1] = FakeExt;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
Patched = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Patched;
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
VOID LOADER_ENTRY::KernelCPUIDPatch()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2019-12-22 12:45:50 +01:00
|
|
|
// Tiger/Leopard patterns
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try Tiger/Leopard patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrCpuid1_tigLeo[0], sizeof(StrCpuid1_tigLeo), &TigLeoSLSearchModel[0],
|
2019-12-22 12:45:50 +01:00
|
|
|
&TigLeoSLSearchExt[0], &TigLeoSLReplaceModel[0], &TigLeoSLReplaceModel[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(TigLeoSLSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-12-22 12:45:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
// Snow Leopard patterns
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try Snow Leopard patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrCpuid1_snowLeo[0], sizeof(StrCpuid1_snowLeo), &TigLeoSLSearchModel[0],
|
2019-12-22 12:45:50 +01:00
|
|
|
&TigLeoSLSearchExt[0], &TigLeoSLReplaceModel[0], &TigLeoSLReplaceModel[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(TigLeoSLSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Lion patterns
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try Lion patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrMsr8b[0], sizeof(StrMsr8b), &LionSearchModel[0],
|
2019-09-03 11:58:42 +02:00
|
|
|
&LionSearchExt[0], &LionReplaceModel[0], &LionReplaceModel[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(LionSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Mountain Lion/Mavericks patterns
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try Mountain Lion/Mavericks patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrMsr8b[0], sizeof(StrMsr8b), &MLMavSearchModel[0],
|
2019-09-03 11:58:42 +02:00
|
|
|
&MLMavSearchExt[0], &MLMavReplaceModel[0], &MLMavReplaceExt[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(MLMavSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Yosemite/El Capitan/Sierra patterns
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try Yosemite/El Capitan/Sierra patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrMsr8b[0], sizeof(StrMsr8b), &YosECSieSearchModel[0],
|
2019-09-03 11:58:42 +02:00
|
|
|
&YosECSieSearchExt[0], &LionReplaceModel[0], &LionReplaceModel[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(YosECSieSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// High Sierra/Mojave patterns
|
|
|
|
// Sherlocks: 10.13/10.14
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try High Sierra/Mojave patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrMsr8b[0], sizeof(StrMsr8b), &HSieMojSearchModel[0],
|
2019-09-03 11:58:42 +02:00
|
|
|
&YosECSieSearchExt[0], &LionReplaceModel[0], &LionReplaceModel[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(HSieMojSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Catalina patterns
|
|
|
|
// PMheart: 10.15.DP1
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "CPUID: try Catalina patch...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
if (PatchCPUID(&StrMsr8b[0], sizeof(StrMsr8b), &CataSearchModel[0],
|
2019-09-03 11:58:42 +02:00
|
|
|
&CataSearchExt[0], &CataReplaceMovEax[0], &CataReplaceMovEax[0],
|
2020-05-01 18:26:28 +02:00
|
|
|
sizeof(CataSearchModel))) {
|
|
|
|
DBG_RT( "...done!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:12:01 +02:00
|
|
|
#define NEW_PM 1
|
2020-05-05 08:09:48 +02:00
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::KernelPatchPm()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-05-04 21:31:12 +02:00
|
|
|
DBG_RT("Patching kernel power management...\n");
|
2020-05-05 01:52:00 +02:00
|
|
|
#if NEW_PM
|
2020-05-09 09:56:30 +02:00
|
|
|
|
2020-05-04 21:31:12 +02:00
|
|
|
//Slice
|
|
|
|
//1. procedure xcpm_idle
|
|
|
|
// wrmsr 0xe2 twice
|
|
|
|
// B9E2000000 0F30 replace to eb05
|
2020-05-05 21:15:59 +02:00
|
|
|
// UINTN procLen = 0;
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN procLocation = searchProc("xcpm_idle"_XS8);
|
2020-05-04 21:31:12 +02:00
|
|
|
const UINT8 findJmp[] = {0xB9, 0xE2, 0x00, 0x00, 0x00, 0x0F, 0x30};
|
|
|
|
const UINT8 patchJmp[] = {0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90};
|
|
|
|
DBG_RT("==> xcpm_idle at %llx\n", procLocation);
|
2020-05-09 09:56:30 +02:00
|
|
|
INTN Num = SearchAndReplace(&KernelData[procLocation], 0x400, findJmp, sizeof(findJmp), patchJmp, 0);
|
2020-05-05 08:09:48 +02:00
|
|
|
DBG_RT("==> found %lld patterns\n", Num);
|
2020-05-04 21:31:12 +02:00
|
|
|
//2. procedure xcpm_init
|
2020-05-09 06:59:38 +02:00
|
|
|
// indirect call to _xcpm_core_scope_msrs and to _xcpm_SMT_scope_msrs
|
2020-05-05 21:15:59 +02:00
|
|
|
// 488D3DDA317600 lea rdi, qword [ds:_xcpm_core_scope_msrs]
|
2020-05-04 21:31:12 +02:00
|
|
|
// BE0B000000 mov esi, 0xb => replace to eb0a
|
|
|
|
// 31D2 xor edx, edx
|
|
|
|
// E87EFCFFFF call sub_ffffff80004fa610 => check e8?
|
2020-05-05 21:15:59 +02:00
|
|
|
// there are other occurence of _xcpm_core_scope_msrs so check 488D3D or E8 at .+7
|
2020-05-04 21:31:12 +02:00
|
|
|
// or restrict len = 0x200
|
2020-08-12 17:15:47 +02:00
|
|
|
procLocation = searchProc("xcpm_init"_XS8);
|
|
|
|
UINTN symbol1 = searchProc("xcpm_core_scope_msrs"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
UINTN patchLocation1 = FindRelative32(KernelData, procLocation, 0x200, symbol1);
|
2020-05-04 21:31:12 +02:00
|
|
|
if (patchLocation1 != 0) {
|
|
|
|
DBG_RT("=> xcpm_core_scope_msrs found at %llx\n", patchLocation1);
|
2020-05-09 09:56:30 +02:00
|
|
|
if (KernelData[patchLocation1 + 7] == 0xE8) {
|
2020-05-04 21:31:12 +02:00
|
|
|
DBG_RT("=> patch applied\n");
|
2020-05-09 06:59:38 +02:00
|
|
|
// for (int i=0; i < 0x10; ++i) {
|
2020-05-09 09:56:30 +02:00
|
|
|
// DBG_RT("%02x", KernelData[patchLocation1 + i]);
|
2020-05-09 06:59:38 +02:00
|
|
|
// }
|
|
|
|
// DBG_RT("\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation1] = 0xEB;
|
|
|
|
KernelData[patchLocation1 + 1] = 0x0A;
|
2020-05-09 06:59:38 +02:00
|
|
|
} else {
|
|
|
|
DBG_RT("=> pattern not good\n");
|
|
|
|
// for (int i=0; i < 0x10; ++i) {
|
2020-05-09 09:56:30 +02:00
|
|
|
// DBG_RT("%02x", KernelData[patchLocation1 + i]);
|
2020-05-09 06:59:38 +02:00
|
|
|
// }
|
|
|
|
// DBG_RT("\n");
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN symbol2 = searchProc("xcpm_SMT_scope_msrs"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation1 = FindRelative32(KernelData, procLocation, 0x200, symbol2);
|
2020-05-09 06:59:38 +02:00
|
|
|
if (patchLocation1 != 0) {
|
|
|
|
DBG_RT("=> xcpm_SMT_scope_msrs found at %llx\n", patchLocation1);
|
2020-05-09 09:56:30 +02:00
|
|
|
if (KernelData[patchLocation1 + 7] == 0xE8) {
|
2020-05-09 06:59:38 +02:00
|
|
|
DBG_RT("=> SMT patch applied\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation1] = 0xEB;
|
|
|
|
KernelData[patchLocation1 + 1] = 0x0A;
|
2020-05-04 21:31:12 +02:00
|
|
|
} else {
|
2020-05-05 14:12:01 +02:00
|
|
|
DBG_RT("=> pattern not good\n");
|
2020-05-04 21:31:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 14:12:01 +02:00
|
|
|
|
|
|
|
Stall(10000000);
|
2020-05-04 21:31:12 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
// Credits to RehabMan for the kernel patch information
|
|
|
|
// new way by RehabMan 2017-08-13
|
|
|
|
// cleanup by Sherlocks 2020-03-23
|
|
|
|
#define CompareWithMask(x,m,c) (((x) & (m)) == (c))
|
|
|
|
//TODO - remake using CompareMemMask
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT64* Ptr = (UINT64*)KernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT64* End = Ptr + 0x1000000/sizeof(UINT64);
|
|
|
|
if (Ptr == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-03-23 14:31:04 +01:00
|
|
|
|
2020-05-04 21:31:12 +02:00
|
|
|
|
2020-03-24 14:23:05 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
for (; Ptr < End; Ptr += 2) {
|
2020-03-23 14:31:04 +01:00
|
|
|
// check for xcpm_scope_msr common 0xE2 prologue
|
|
|
|
// E2000000 XX000000 00000000 00000000 00040000 00000000
|
|
|
|
// 10.8/10.9: 02,0C,10
|
|
|
|
// E2000000 XXXX0000 00000000 00000000 0F040000 00000000
|
2020-03-24 14:23:05 +01:00
|
|
|
// 10.10: 0200,4C00,9001, 10.11: 0200,4C00,9013, 10.12: 4C00,9033, 10.13-10.15.3: 4C00,9033,0040
|
2020-03-23 14:31:04 +01:00
|
|
|
// E2000000 XXXXXX00 00000000 00000000 0F040000 00000000
|
|
|
|
// 10.15.4+: 4C0000,903306,004000
|
|
|
|
|
|
|
|
// E2000000 XXXXXXXX 00000000 00000000 XX040000 00000000
|
|
|
|
// safe pattern for next macOS
|
|
|
|
if (CompareWithMask(Ptr[0], 0x00000000FFFFFFFF, 0x00000000000000E2) && 0 == Ptr[1] &&
|
2019-09-03 11:58:42 +02:00
|
|
|
CompareWithMask(Ptr[2], 0xFFFFFFFFFFFFFF00, 0x0000000000000400)) {
|
2020-03-23 14:31:04 +01:00
|
|
|
// 10.8 - 10.12
|
|
|
|
// 0700001E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
// 0500001E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
// 0800007E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
// 10.13+
|
|
|
|
// 0500001E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
// 0800007E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
// 0300007E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
|
2020-03-23 14:49:06 +01:00
|
|
|
// XX00001E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
if (CompareWithMask(Ptr[3], 0xFFFFFFFFFFFFFF00, 0x000000001E000000) && 0 == Ptr[4] && 0 == Ptr[5]) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// zero out 0xE2 MSR and CPU mask
|
|
|
|
Ptr[0] = 0;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("Kernel power management: entry 1E found and patched\n");
|
2020-03-23 14:49:06 +01:00
|
|
|
// XX00007E 00000000 00000000 00000000 00000000 00000000
|
|
|
|
} else if (CompareWithMask(Ptr[3], 0xFFFFFFFFFFFFFF00, 0x000000007E000000) && 0 == Ptr[4] && 0 == Ptr[5]) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// zero out 0xE2 MSR and CPU mask
|
|
|
|
Ptr[0] = 0;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("Kernel power management: entry 7E found and patched\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 13:55:35 +01:00
|
|
|
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPDebug) {
|
2020-03-24 13:55:35 +01:00
|
|
|
gBS->Stall(3000000);
|
|
|
|
}
|
2020-05-04 21:31:12 +02:00
|
|
|
#endif
|
2020-03-23 15:11:27 +01:00
|
|
|
return TRUE;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:16:43 +02:00
|
|
|
const UINT8 PanicNoKextDumpFind[] = {0x00, 0x25, 0x2E, 0x2A, 0x73, 0x00};
|
2019-09-03 11:58:42 +02:00
|
|
|
//STATIC UINT8 PanicNoKextDumpReplace[6] = {0x00, 0x00, 0x2E, 0x2A, 0x73, 0x00};
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::KernelPanicNoKextDump()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
INT32 patchLocation;
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation = FindBin(KernelData, 0xF00000, PanicNoKextDumpFind, 6);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (patchLocation > 0) {
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation + 1] = 0;
|
2019-09-03 11:58:42 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::KernelLapicPatch_64()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
// Credits to donovan6000 and Sherlocks for providing the lapic kernel patch source used to build this function
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8 *bytes = KernelData;
|
2020-05-14 12:39:50 +02:00
|
|
|
UINTN patchLocation1 = 0, patchLocation2 = 0;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 i, y;
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Looking for Lapic panic call (64-bit) Start\n");
|
2020-05-05 21:15:59 +02:00
|
|
|
//Slice - symbolic method
|
|
|
|
//start at lapic_interrupt ffffff80004e4950 => @2e4950
|
|
|
|
// found pattern: 1
|
|
|
|
// address: 002e4a2f
|
|
|
|
// bytes:658b04251c0000003b058bb97b00
|
|
|
|
// call _panic -> change to nop {90,90,90,90,90}
|
2020-08-12 17:15:47 +02:00
|
|
|
if (AsciiOSVersionToUint64(OSVersion) >= AsciiOSVersionToUint64("10.10"_XS8)) {
|
|
|
|
UINTN procAddr = searchProc("lapic_interrupt"_XS8);
|
|
|
|
patchLocation1 = searchProc("_panic"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation2 = FindRelative32(KernelData, procAddr, 0x140, patchLocation1);
|
2020-05-05 21:15:59 +02:00
|
|
|
if (patchLocation2 != 0) {
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation2 - 5] = 0xEB;
|
|
|
|
KernelData[patchLocation2 - 4] = 0x03;
|
2020-05-05 21:15:59 +02:00
|
|
|
DBG_RT( "Lapic panic patched\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//else old method
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 0x1000000; i++) {
|
2020-05-09 09:56:30 +02:00
|
|
|
if (KernelData[i+0] == 0x65 && KernelData[i+1] == 0x8B && KernelData[i+2] == 0x04 && KernelData[i+3] == 0x25 &&
|
|
|
|
KernelData[i+4] == 0x3C && KernelData[i+5] == 0x00 && KernelData[i+6] == 0x00 && KernelData[i+7] == 0x00 &&
|
|
|
|
KernelData[i+45] == 0x65 && KernelData[i+46] == 0x8B && KernelData[i+47] == 0x04 && KernelData[i+48] == 0x25 &&
|
|
|
|
KernelData[i+49] == 0x3C && KernelData[i+50] == 0x00 && KernelData[i+51] == 0x00 && KernelData[i+52] == 0x00) {
|
2019-09-03 11:58:42 +02:00
|
|
|
patchLocation1 = i+40;
|
2020-05-14 12:39:50 +02:00
|
|
|
DBG_RT( "Found Lapic panic (10.6) at 0x%08llx\n", patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
2020-05-09 09:56:30 +02:00
|
|
|
} else if (KernelData[i+0] == 0x65 && KernelData[i+1] == 0x8B && KernelData[i+2] == 0x04 && KernelData[i+3] == 0x25 &&
|
|
|
|
KernelData[i+4] == 0x14 && KernelData[i+5] == 0x00 && KernelData[i+6] == 0x00 && KernelData[i+7] == 0x00 &&
|
|
|
|
KernelData[i+35] == 0x65 && KernelData[i+36] == 0x8B && KernelData[i+37] == 0x04 && KernelData[i+38] == 0x25 &&
|
|
|
|
KernelData[i+39] == 0x14 && KernelData[i+40] == 0x00 && KernelData[i+41] == 0x00 && KernelData[i+42] == 0x00) {
|
2019-09-03 11:58:42 +02:00
|
|
|
patchLocation1 = i+30;
|
2020-05-14 12:39:50 +02:00
|
|
|
DBG_RT( "Found Lapic panic (10.7 - 10.8) at 0x%08llx\n", patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
2020-05-09 09:56:30 +02:00
|
|
|
} else if (KernelData[i+0] == 0x65 && KernelData[i+1] == 0x8B && KernelData[i+2] == 0x04 && KernelData[i+3] == 0x25 &&
|
|
|
|
KernelData[i+4] == 0x1C && KernelData[i+5] == 0x00 && KernelData[i+6] == 0x00 && KernelData[i+7] == 0x00 &&
|
|
|
|
KernelData[i+36] == 0x65 && KernelData[i+37] == 0x8B && KernelData[i+38] == 0x04 && KernelData[i+39] == 0x25 &&
|
|
|
|
KernelData[i+40] == 0x1C && KernelData[i+41] == 0x00 && KernelData[i+42] == 0x00 && KernelData[i+43] == 0x00) {
|
2019-09-03 11:58:42 +02:00
|
|
|
patchLocation1 = i+31;
|
2020-05-14 12:39:50 +02:00
|
|
|
DBG_RT( "Found Lapic panic (10.9) at 0x%08llx\n", patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
// 00 29 C7 78 XX 31 DB 8D 47 FA 83
|
2020-05-09 09:56:30 +02:00
|
|
|
} else if (KernelData[i+0] == 0x00 && KernelData[i+1] == 0x29 && KernelData[i+2] == 0xC7 && KernelData[i+3] == 0x78 &&
|
2019-09-03 11:58:42 +02:00
|
|
|
//(bytes[i+4] == 0x3F || bytes[i+4] == 0x4F) && // 3F:10.10-10.12/4F:10.13+
|
|
|
|
bytes[i+5] == 0x31 && bytes[i+6] == 0xDB && bytes[i+7] == 0x8D && bytes[i+8] == 0x47 &&
|
|
|
|
bytes[i+9] == 0xFA && bytes[i+10] == 0x83) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Found Lapic panic Base (10.10 - recent macOS)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
for (y = i; y < 0x1000000; y++) {
|
|
|
|
// Lapic panic patch, by vit9696
|
2020-02-23 04:33:39 +01:00
|
|
|
// mov eax, gs:XX
|
2019-09-03 11:58:42 +02:00
|
|
|
// cmp eax, cs:_master_cpu
|
2020-02-23 04:33:39 +01:00
|
|
|
// 65 8B 04 25 XX 00 00 00 3B 05 XX XX XX 00
|
2019-09-03 11:58:42 +02:00
|
|
|
if (bytes[y+0] == 0x65 && bytes[y+1] == 0x8B && bytes[y+2] == 0x04 && bytes[y+3] == 0x25 &&
|
2020-02-23 04:33:39 +01:00
|
|
|
//(bytes[y+4] == 0x1C || bytes[y+4] == 0x18) && // 1C:10.10-10.15.3/18:10.15.4+
|
|
|
|
bytes[y+5] == 0x00 && bytes[y+6] == 0x00 && bytes[y+7] == 0x00 &&
|
2019-09-03 11:58:42 +02:00
|
|
|
bytes[y+8] == 0x3B && bytes[y+9] == 0x05 && bytes[y+13] == 0x00) {
|
|
|
|
patchLocation1 = y;
|
2020-05-14 12:39:50 +02:00
|
|
|
DBG_RT( "Found Lapic panic (10.10 - recent macOS) at 0x%08llx\n", patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation1) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Can't find Lapic panic, kernel patch aborted.\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-05-05 21:15:59 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
// Already patched? May be running a non-vanilla kernel already?
|
|
|
|
if (bytes[patchLocation1 + 0] == 0x90 && bytes[patchLocation1 + 1] == 0x90 &&
|
|
|
|
bytes[patchLocation1 + 2] == 0x90 && bytes[patchLocation1 + 3] == 0x90 &&
|
|
|
|
bytes[patchLocation1 + 4] == 0x90) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Lapic panic already patched, kernel file (10.6 - 10.9) manually patched?\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
} else if (bytes[patchLocation1 + 0] == 0x31 && bytes[patchLocation1 + 1] == 0xC0 &&
|
|
|
|
bytes[patchLocation1 + 2] == 0x90 && bytes[patchLocation1 + 3] == 0x90) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Lapic panic already patched, kernel file (10.10 - recent macOS) manually patched?\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
if (bytes[patchLocation1 + 8] == 0x3B && bytes[patchLocation1 + 9] == 0x05 &&
|
|
|
|
bytes[patchLocation1 + 13] == 0x00) {
|
2020-03-07 14:35:01 +01:00
|
|
|
// 65 8B 04 25 XX 00 00 00 3B 05 XX XX XX 00
|
2019-09-03 11:58:42 +02:00
|
|
|
// 31 C0 90 90 90 90 90 90 90 90 90 90 90 90
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Patched Lapic panic (10.10 - recent macOS)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
bytes[patchLocation1 + 0] = 0x31;
|
|
|
|
bytes[patchLocation1 + 1] = 0xC0;
|
|
|
|
for (i = 2; i < 14; i++) {
|
|
|
|
bytes[patchLocation1 + i] = 0x90;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 0x1000000; i++) {
|
|
|
|
// 00 29 C7 78 XX 31 DB 8D 47 FA 83
|
|
|
|
if (bytes[i+0] == 0x00 && bytes[i+1] == 0x29 && bytes[i+2] == 0xC7 && bytes[i+3] == 0x78 &&
|
|
|
|
//(bytes[i+4] == 0x3F || bytes[i+4] == 0x4F) && // 3F:10.10-10.12/4F:10.13+
|
|
|
|
bytes[i+5] == 0x31 && bytes[i+6] == 0xDB && bytes[i+7] == 0x8D && bytes[i+8] == 0x47 &&
|
|
|
|
bytes[i+9] == 0xFA && bytes[i+10] == 0x83) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Found Lapic panic master Base (10.10 - recent macOS)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
for (y = i; y < 0x1000000; y++) {
|
|
|
|
// Lapic panic master patch, by vit9696
|
|
|
|
// cmp cs:_debug_boot_arg, 0
|
|
|
|
// E8 XX XX FF FF 83 XX XX XX XX 00 00
|
|
|
|
if (bytes[y+0] == 0xE8 && bytes[y+3] == 0xFF && bytes[y+4] == 0xFF &&
|
|
|
|
bytes[y+5] == 0x83 && bytes[y+10] == 0x00 && bytes[y+11] == 0x00) {
|
|
|
|
patchLocation2 = y;
|
2020-05-14 12:39:50 +02:00
|
|
|
DBG_RT( "Found Lapic panic master (10.10 - recent macOS) at 0x%08llx\n", patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation2) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Can't find Lapic panic master (10.10 - recent macOS), kernel patch aborted.\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already patched? May be running a non-vanilla kernel already?
|
|
|
|
if (bytes[patchLocation2 + 5] == 0x31 && bytes[patchLocation2 + 6] == 0xC0) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Lapic panic master already patched, kernel file (10.10 - recent macOS) manually patched?\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Patched Lapic panic master (10.10 - recent macOS)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
// E8 XX XX FF FF 83 XX XX XX XX 00 00
|
2020-04-25 18:18:18 +02:00
|
|
|
// E8 XX XX FF FF 31 C0 90 90 90 90 90 xor eax,eax; nop; nop;....
|
2019-09-03 11:58:42 +02:00
|
|
|
bytes[patchLocation2 + 5] = 0x31;
|
|
|
|
bytes[patchLocation2 + 6] = 0xC0;
|
|
|
|
for (i = 7; i < 12; i++) {
|
|
|
|
bytes[patchLocation2 + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Patched Lapic panic (10.6 - 10.9)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
bytes[patchLocation1 + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 13:55:35 +01:00
|
|
|
|
2020-08-11 14:43:53 +02:00
|
|
|
// if (KernelAndKextPatches.KPDebug) {
|
2020-05-09 07:51:12 +02:00
|
|
|
Stall(3000000);
|
|
|
|
// }
|
2020-03-24 13:55:35 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::KernelLapicPatch_32()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
// Credits to donovan6000 and Sherlocks for providing the lapic kernel patch source used to build this function
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8 *bytes = KernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 patchLocation = 0;
|
|
|
|
UINT32 i;
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Looking for Lapic panic call (32-bit) Start\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 0x1000000; i++) {
|
|
|
|
if (bytes[i+0] == 0x65 && bytes[i+1] == 0xA1 && bytes[i+2] == 0x0C && bytes[i+3] == 0x00 &&
|
|
|
|
bytes[i+4] == 0x00 && bytes[i+5] == 0x00 &&
|
|
|
|
bytes[i+30] == 0x65 && bytes[i+31] == 0xA1 && bytes[i+32] == 0x0C && bytes[i+33] == 0x00 &&
|
|
|
|
bytes[i+34] == 0x00 && bytes[i+35] == 0x00) {
|
|
|
|
patchLocation = i+25;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Found Lapic panic at 0x%08x\n", patchLocation);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Can't find Lapic panic, kernel patch aborted.\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already patched? May be running a non-vanilla kernel already?
|
|
|
|
|
|
|
|
if (bytes[patchLocation + 0] == 0x90 && bytes[patchLocation + 1] == 0x90 &&
|
|
|
|
bytes[patchLocation + 2] == 0x90 && bytes[patchLocation + 3] == 0x90 &&
|
|
|
|
bytes[patchLocation + 4] == 0x90) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Lapic panic already patched, kernel file manually patched?\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Patched Lapic panic (32-bit)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
bytes[patchLocation + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 13:55:35 +01:00
|
|
|
|
2020-08-11 14:43:53 +02:00
|
|
|
// if (KernelAndKextPatches.KPDebug) {
|
2020-05-09 07:51:12 +02:00
|
|
|
Stall(3000000);
|
|
|
|
// }
|
2020-03-24 13:55:35 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// syscl - EnableExtCpuXCPM(): enable extra(unsupport) Cpu XCPM function
|
|
|
|
// PowerManagement that will be enabled on:
|
|
|
|
// SandyBridge-E, Ivy Bridge, Ivy Bridge-E, Haswell Celeron/Pentium, Haswell-E, Broadwell-E, ...
|
|
|
|
// credit Pike R.Alpha, stinga11, syscl
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
//BOOLEAN (*EnableExtCpuXCPM)(VOID *kernelData);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// syscl - applyKernPatch a wrapper for SearchAndReplace() to make the CpuPM patch tidy and clean
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
VOID LOADER_ENTRY::applyKernPatch(const UINT8 *find, UINTN size, const UINT8 *repl, const CHAR8 *comment)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("Searching %s...\n", comment);
|
2020-05-09 09:56:30 +02:00
|
|
|
if (SearchAndReplace(KernelData, KERNEL_MAX_SIZE, find, size, repl, 0)) {
|
2020-05-06 08:21:30 +02:00
|
|
|
DBG("Found %s\nApplied patch\n", comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("%s no found, patched already?\n", comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PMHeart
|
|
|
|
// Global XCPM patches compatibility
|
|
|
|
// Currently 10.8.5 - 10.15
|
|
|
|
//
|
|
|
|
static inline BOOLEAN IsXCPMOSVersionCompat(UINT64 os_version)
|
|
|
|
{
|
2020-08-12 17:15:47 +02:00
|
|
|
return (os_version >= AsciiOSVersionToUint64("10.8.5"_XS8)) && (os_version < AsciiOSVersionToUint64("11.1.0"_XS8));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Enable Unsupported CPU PowerManagement
|
|
|
|
//
|
|
|
|
// syscl - SandyBridgeEPM(): enable PowerManagement on SandyBridge-E
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::SandyBridgeEPM()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
// note: a dummy function that made patches consistency
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// syscl - Enable Haswell-E XCPM
|
|
|
|
// Hex data provided and polished (c) PMheart, idea (c) Pike R.Alpha
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::HaswellEXCPM()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
DBG("HaswellEXCPM() ===>\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8 *kern = KernelData;
|
2020-08-12 17:15:47 +02:00
|
|
|
XString8 comment;
|
2020-05-14 12:39:50 +02:00
|
|
|
// UINT32 i;
|
2020-05-09 09:56:30 +02:00
|
|
|
UINTN patchLocation;
|
|
|
|
UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// check OS version suit for patches
|
|
|
|
if (!IsXCPMOSVersionCompat(os_version)) {
|
|
|
|
DBG("HaswellEXCPM(): Unsupported macOS.\n");
|
|
|
|
DBG("HaswellEXCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// _cpuid_set_info
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_cpuid_set_info"_XS8;
|
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.8.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xF8, 0x3C, 0x74, 0x2D };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xF8, 0x3F, 0x74, 0x2D };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.10"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.9.x
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xF8, 0x3C, 0x75, 0x07 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xF8, 0x3F, 0x75, 0x07 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.10.1"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.10 - 10.10.1
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x74, 0x11, 0x83, 0xF8, 0x3C };
|
|
|
|
const UINT8 repl[] = { 0x74, 0x11, 0x83, 0xF8, 0x3F };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
} // 10.10.2+: native support reached, no need to patch
|
|
|
|
|
|
|
|
// _xcpm_bootstrap
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_xcpm_bootstrap"_XS8;
|
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.8.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xFB, 0x3C, 0x75, 0x54 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xFB, 0x3F, 0x75, 0x54 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.10"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.9.x
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xFB, 0x3C, 0x75, 0x68 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xFB, 0x3F, 0x75, 0x68 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.10.2"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.10 - 10.10.2
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xFB, 0x3C, 0x75, 0x63 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xFB, 0x3F, 0x75, 0x63 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.10.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.10.3 - 10.10.5
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC6, 0x83, 0xFB, 0x0D };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC3, 0x83, 0xFB, 0x0D };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.11"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.11 DB/PB - 10.11.0
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC6, 0x83, 0xFB, 0x0D };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC3, 0x83, 0xFB, 0x0D };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.11.6"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.11.1 - 10.11.6
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xBB, 0x83, 0xFB, 0x09 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xB8, 0x83, 0xFB, 0x09 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2020-05-09 10:36:20 +02:00
|
|
|
} else {
|
|
|
|
UINTN procLocation = searchProc(comment);
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN featureCall = searchProc("_cpuid_features"_XS8);
|
2020-05-09 10:36:20 +02:00
|
|
|
UINTN place = FindRelative32(KernelData, procLocation, 0x100, featureCall);
|
|
|
|
for (UINTN i = 10; i < 20; ++i) {
|
|
|
|
if (KernelData[place + i] == 0xC4) {
|
|
|
|
KernelData[place + i] = 0xC1;
|
2020-05-10 20:21:51 +02:00
|
|
|
if (KernelData[(place + i) - 5] == 0x3B) {
|
|
|
|
KernelData[(place + i) - 5] = 0x00;
|
|
|
|
}
|
2020-05-09 10:36:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if (os_version <= AsciiOSVersionToUint64("10.12.5")) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.12 - 10.12.5
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC4, 0x83, 0xFB, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC1, 0x83, 0xFB, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.13")) {
|
|
|
|
// 10.12.6
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x83, 0xF8, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC1, 0x83, 0xF8, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.14 compatibility
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15")) {
|
|
|
|
// 10.13/10.14
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x89, 0xD8, 0x04, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x89, 0xD8, 0x04, 0xC1, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.15 compatibility
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15.4")) {
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC1, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.16")) {
|
2020-04-27 17:39:35 +02:00
|
|
|
// vector sigma: 10.15.5 Beta 2 build 19F62f and 10.15.4 build 19E287
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0x3B, 0x7E, 0x2E, 0x80, 0xC3, 0xC4, 0x80, 0xFB, 0x42 };
|
|
|
|
const UINT8 repl[] = { 0x00, 0x7E, 0x2E, 0x80, 0xC3, 0xC1, 0x80, 0xFB, 0x42 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2020-05-09 10:36:20 +02:00
|
|
|
*/
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG("Searching _xcpm_pkg_scope_msr ...\n");
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_xcpm_pkg_scope_msrs"_XS8;
|
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.8.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = {
|
2019-09-03 11:58:42 +02:00
|
|
|
0x48, 0x8D, 0x3D, 0x02, 0x71, 0x55, 0x00, 0xBE,
|
|
|
|
0x07, 0x00, 0x00, 0x00, 0xEB, 0x1F, 0x48, 0x8D,
|
|
|
|
0x3D, 0xF4, 0x70, 0x55, 0x00, 0xBE, 0x07, 0x00,
|
|
|
|
0x00, 0x00, 0x31, 0xD2, 0xE8, 0x28, 0x02, 0x00, 0x00
|
|
|
|
};
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 repl[] = {
|
2019-09-03 11:58:42 +02:00
|
|
|
0x48, 0x8D, 0x3D, 0x02, 0x71, 0x55, 0x00, 0xBE,
|
|
|
|
0x07, 0x00, 0x00, 0x00, 0x90, 0x90, 0x48, 0x8D,
|
|
|
|
0x3D, 0xF4, 0x70, 0x55, 0x00, 0xBE, 0x07, 0x00,
|
|
|
|
0x00, 0x00, 0x31, 0xD2, 0x90, 0x90, 0x90, 0x90, 0x90
|
|
|
|
};
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.10"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.9.x
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 find[] = { 0xBE, 0x07, 0x00, 0x00, 0x00, 0x74, 0x13, 0x31, 0xD2, 0xE8, 0x5F, 0x02, 0x00, 0x00 };
|
|
|
|
const UINT8 repl[] = { 0xBE, 0x07, 0x00, 0x00, 0x00, 0x90, 0x90, 0x31, 0xD2, 0x90, 0x90, 0x90, 0x90, 0x90 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
|
|
|
// 10.10+
|
2020-05-09 09:56:30 +02:00
|
|
|
/* patchLocation = 0; // clean out the value just in case
|
2019-09-03 11:58:42 +02:00
|
|
|
for (i = 0; i < 0x1000000; i++) {
|
|
|
|
if (kern[i+0] == 0xBE && kern[i+1] == 0x07 && kern[i+2] == 0x00 && kern[i+3] == 0x00 &&
|
|
|
|
kern[i+4] == 0x00 && kern[i+5] == 0x31 && kern[i+6] == 0xD2 && kern[i+7] == 0xE8) {
|
|
|
|
patchLocation = i+7;
|
|
|
|
DBG("Found _xcpm_pkg_scope_msr\n");
|
|
|
|
break;
|
|
|
|
}
|
2020-05-09 09:56:30 +02:00
|
|
|
} */
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN procLocation = searchProc("xcpm_init"_XS8);
|
|
|
|
UINTN symbol1 = searchProc("_xcpm_pkg_scope_msrs"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation = FindRelative32(KernelData, procLocation, 0x100, symbol1);
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (patchLocation) {
|
2020-05-14 12:39:50 +02:00
|
|
|
for (UINTN i = 7; i < 12; i++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
kern[patchLocation+i] = 0x90;
|
|
|
|
}
|
|
|
|
DBG("Applied _xcpm_pkg_scope_msr patch\n");
|
|
|
|
} else {
|
|
|
|
DBG("_xcpm_pkg_scope_msr not found, patch aborted\n");
|
|
|
|
DBG("HaswellEXCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG("HaswellEXCPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Enable Broadwell-E/EP PowerManagement on 10.12+ by syscl
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::BroadwellEPM()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
DBG("BroadwellEPM() ===>\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 i;
|
2020-05-09 09:56:30 +02:00
|
|
|
UINTN patchLocation;
|
2020-05-01 18:26:28 +02:00
|
|
|
UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// check OS version suit for patches
|
|
|
|
if (!IsXCPMOSVersionCompat(os_version)) {
|
|
|
|
DBG("BroadwellEPM(): Unsupported macOS.\n");
|
|
|
|
DBG("BroadwellEPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
KernelAndKextPatches.FakeCPUID = (UINT32)(os_version < AsciiOSVersionToUint64("10.10.3"_XS8) ? 0x0306C0 : 0x040674);
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelCPUIDPatch();
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
DBG("Searching _xcpm_pkg_scope_msr ...\n");
|
2020-05-09 07:51:12 +02:00
|
|
|
// proc: _xcpm_init @4687b0
|
2020-05-09 06:59:38 +02:00
|
|
|
// ffffff8000468825 488D3D54527F00 lea rdi, qword [ds:_xcpm_pkg_scope_msrs]
|
|
|
|
// ffffff800046882c BE07000000 mov esi, 0x7
|
|
|
|
// ffffff8000468831 31D2 xor edx, edx
|
|
|
|
// ffffff8000468833 E838FDFFFF call sub_ffffff8000468570
|
2020-08-12 17:15:47 +02:00
|
|
|
if (os_version >= AsciiOSVersionToUint64("10.12"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.12+
|
2020-05-09 07:51:12 +02:00
|
|
|
// patchLocation = 0; // clean out the value just in case
|
|
|
|
// for (i = 0; i < 0x1000000; i++) {
|
|
|
|
// if (kern[i+0] == 0xBE && kern[i+1] == 0x07 && kern[i+2] == 0x00 && kern[i+3] == 0x00 &&
|
|
|
|
// kern[i+4] == 0x00 && kern[i+5] == 0x31 && kern[i+6] == 0xD2 && kern[i+7] == 0xE8) {
|
|
|
|
// patchLocation = i+7;
|
|
|
|
// DBG("Found _xcpm_pkg_scope_msr\n");
|
|
|
|
// break;
|
|
|
|
// }
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN procLocation = searchProc("xcpm_init"_XS8);
|
|
|
|
UINTN symbol1 = searchProc("_xcpm_pkg_scope_msrs"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation = FindRelative32(KernelData, procLocation, 0x100, symbol1);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-09 07:51:12 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
if (patchLocation) {
|
2020-05-09 07:51:12 +02:00
|
|
|
for (i = 7; i < 12; i++) {
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation+i] = 0x90;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
DBG("Applied _xcpm_pkg_scope_msr patch\n");
|
|
|
|
} else {
|
|
|
|
DBG("_xcpm_pkg_scope_msr not found, patch aborted\n");
|
|
|
|
DBG("BroadwellEPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG("BroadwellEPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// syscl - this patch provides XCPM support for Haswell low-end(HSWLowEnd) and platforms later than Haswell
|
|
|
|
// implemented by syscl
|
|
|
|
// credit also Pike R.Alpha, stinga11, Sherlocks, vit9696
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::HaswellLowEndXCPM()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
DBG("HaswellLowEndXCPM() ===>\n");
|
2020-05-01 18:26:28 +02:00
|
|
|
UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2020-08-12 17:15:47 +02:00
|
|
|
XString8 comment;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// check OS version suit for patches
|
|
|
|
if (!IsXCPMOSVersionCompat(os_version)) {
|
|
|
|
DBG("HaswellLowEndXCPM(): Unsupported macOS.\n");
|
|
|
|
DBG("HaswellLowEndXCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:43:53 +02:00
|
|
|
KernelAndKextPatches.FakeCPUID = (UINT32)(0x0306A0); // correct FakeCPUID
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelCPUIDPatch();
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// 10.8.5 - 10.11.x no need the following kernel patches on Haswell Celeron/Pentium
|
2020-08-12 17:15:47 +02:00
|
|
|
if (os_version >= AsciiOSVersionToUint64("10.8.5"_XS8) && os_version < AsciiOSVersionToUint64("10.12"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
DBG("HaswellLowEndXCPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:59:38 +02:00
|
|
|
// _xcpm_idle //this is a part of KernelPM
|
|
|
|
/* if (use_xcpm_idle) {
|
2019-09-03 11:58:42 +02:00
|
|
|
DBG("HWPEnable - ON.\n");
|
|
|
|
comment = "_xcpm_idle";
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0xB9, 0xE2, 0x00, 0x00, 0x00, 0x0F, 0x30 };
|
|
|
|
const UINT8 repl[] = { 0xB9, 0xE2, 0x00, 0x00, 0x00, 0x90, 0x90 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-09 06:59:38 +02:00
|
|
|
*/
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_xcpm_bootstrap"_XS8;
|
2020-05-09 10:36:20 +02:00
|
|
|
UINTN procLocation = searchProc(comment);
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN featureCall = searchProc("_cpuid_features"_XS8);
|
2020-05-09 10:36:20 +02:00
|
|
|
UINTN place = FindRelative32(KernelData, procLocation, 0x100, featureCall);
|
|
|
|
for (UINTN i = 10; i < 20; ++i) {
|
|
|
|
if (KernelData[place + i] == 0xC4) {
|
|
|
|
KernelData[place + i] = 0xC6;
|
2020-05-10 20:21:51 +02:00
|
|
|
if (KernelData[(place + i) - 5] == 0x3B) {
|
|
|
|
KernelData[(place + i) - 5] = 0x00;
|
|
|
|
}
|
2020-05-09 10:36:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2019-09-03 11:58:42 +02:00
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.12.5")) {
|
|
|
|
// 10.12 - 10.12.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC4, 0x83, 0xFB, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC6, 0x83, 0xFB, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.13")) {
|
|
|
|
// 10.12.6
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x83, 0xF8, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC6, 0x83, 0xF8, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15")) {
|
|
|
|
// 10.13/10.14
|
2020-05-09 10:36:20 +02:00
|
|
|
// ; Basic Block Input Regs: rbx - Killed Regs: rax
|
|
|
|
// ffffff80004fa0f7 89D8 mov eax, ebx
|
|
|
|
// ffffff80004fa0f9 04C4 add al, 0xc4
|
|
|
|
// ffffff80004fa0fb 3C22 cmp al, 0x22
|
|
|
|
// ffffff80004fa0fd 7722 jnbe 0xffffff80004fa121
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x89, 0xD8, 0x04, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x89, 0xD8, 0x04, 0xC6, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.15 compatibility
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15.4")) {
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC6, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.16")) {
|
2020-04-27 17:39:35 +02:00
|
|
|
// vector sigma: 10.15.5 Beta 2 build 19F62f and 10.15.4 build 19E287
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x3B, 0x7E, 0x2E, 0x80, 0xC3, 0xC4, 0x80, 0xFB, 0x42 };
|
|
|
|
const UINT8 repl[] = { 0x00, 0x7E, 0x2E, 0x80, 0xC3, 0xC6, 0x80, 0xFB, 0x42 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-09 10:36:20 +02:00
|
|
|
*/
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_cpuid_set_info_rdmsr"_XS8;
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: bytes seem stable as of 10.12
|
2020-08-12 17:15:47 +02:00
|
|
|
if (os_version >= AsciiOSVersionToUint64("10.12"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.12+
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0xB9, 0xA0, 0x01, 0x00, 0x00, 0x0F, 0x32 };
|
|
|
|
const UINT8 repl[] = { 0xB9, 0xA0, 0x01, 0x00, 0x00, 0x31, 0xC0 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG("HaswellLowEndXCPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// this patch provides XCPM support for Ivy Bridge. by PMheart
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::KernelIvyBridgeXCPM()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-08-12 17:15:47 +02:00
|
|
|
XString8 comment;
|
2020-05-14 12:39:50 +02:00
|
|
|
// UINT32 i;
|
2020-05-09 09:56:30 +02:00
|
|
|
UINTN patchLocation;
|
2020-05-01 18:26:28 +02:00
|
|
|
UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// check whether Ivy Bridge
|
|
|
|
if (gCPUStructure.Model != CPU_MODEL_IVY_BRIDGE) {
|
|
|
|
DBG("KernelIvyBridgeXCPM(): Unsupported platform.\nRequires Ivy Bridge, aborted\n");
|
|
|
|
DBG("KernelIvyBridgeXCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check OS version suit for patches
|
|
|
|
// PMheart: attempt to add 10.14 compatibility
|
|
|
|
if (!IsXCPMOSVersionCompat(os_version)) {
|
|
|
|
DBG("KernelIvyBridgeXCPM():Unsupported macOS.\n");
|
|
|
|
DBG("KernelIvyBridgeXCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
2020-08-12 17:15:47 +02:00
|
|
|
} else if (os_version >= AsciiOSVersionToUint64("10.8.5"_XS8) && os_version < AsciiOSVersionToUint64("10.12"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5 - 10.11.x no need the following kernel patches on Ivy Bridge - we just use -xcpm boot-args
|
|
|
|
DBG("KernelIvyBridgeXCPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG("Searching _xcpm_pkg_scope_msr ...\n");
|
2020-08-12 17:15:47 +02:00
|
|
|
if (os_version >= AsciiOSVersionToUint64("10.12"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.12+
|
2020-05-09 09:56:30 +02:00
|
|
|
/* patchLocation = 0; // clean out the value just in case
|
2019-09-03 11:58:42 +02:00
|
|
|
for (i = 0; i < 0x1000000; i++) {
|
|
|
|
if (kern[i+0] == 0xBE && kern[i+1] == 0x07 && kern[i+2] == 0x00 && kern[i+3] == 0x00 &&
|
|
|
|
kern[i+4] == 0x00 && kern[i+5] == 0x31 && kern[i+6] == 0xD2 && kern[i+7] == 0xE8) {
|
|
|
|
patchLocation = i+7;
|
|
|
|
DBG("Found _xcpm_pkg_scope_msr\n");
|
|
|
|
break;
|
|
|
|
}
|
2020-05-09 09:56:30 +02:00
|
|
|
} */
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN procLocation = searchProc("xcpm_init"_XS8);
|
|
|
|
UINTN symbol1 = searchProc("_xcpm_pkg_scope_msrs"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
patchLocation = FindRelative32(KernelData, procLocation, 0x100, symbol1);
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (patchLocation) {
|
2020-05-14 12:39:50 +02:00
|
|
|
for (int i = 7; i < 12; i++) {
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation+i] = 0x90;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
DBG("Applied _xcpm_pkg_scope_msr patch\n");
|
|
|
|
} else {
|
|
|
|
DBG("_xcpm_pkg_scope_msr not found, patch aborted\n");
|
|
|
|
DBG("KernelIvyBridgeXCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_xcpm_bootstrap"_XS8;
|
2020-05-09 10:36:20 +02:00
|
|
|
UINTN procLocation = searchProc(comment);
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN featureCall = searchProc("_cpuid_features"_XS8);
|
2020-05-09 10:36:20 +02:00
|
|
|
UINTN place = FindRelative32(KernelData, procLocation, 0x100, featureCall);
|
|
|
|
for (UINTN i = 10; i < 20; ++i) {
|
|
|
|
if (KernelData[place + i] == 0xC4) {
|
|
|
|
KernelData[place + i] = 0xC6;
|
2020-05-10 20:21:51 +02:00
|
|
|
if (KernelData[(place + i) - 5] == 0x3B) {
|
|
|
|
KernelData[(place + i) - 5] = 0x00;
|
|
|
|
}
|
2020-05-09 10:36:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2019-09-03 11:58:42 +02:00
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.12.5")) {
|
|
|
|
// 10.12 - 10.12.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC4, 0x83, 0xFB, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC6, 0x83, 0xFB, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.13")) {
|
|
|
|
// 10.12.6
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x83, 0xF8, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC6, 0x83, 0xF8, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.14 compatibility
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15")) {
|
|
|
|
// 10.13/10.14
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x89, 0xD8, 0x04, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x89, 0xD8, 0x04, 0xC6, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.15 compatibility
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15.4")) {
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC6, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.16")) {
|
2020-04-27 17:39:35 +02:00
|
|
|
// vector sigma: 10.15.5 Beta 2 build 19F62f and 10.15.4 build 19E287
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x3B, 0x7E, 0x2E, 0x80, 0xC3, 0xC4, 0x80, 0xFB, 0x42 };
|
|
|
|
const UINT8 repl[] = { 0x00, 0x7E, 0x2E, 0x80, 0xC3, 0xC6, 0x80, 0xFB, 0x42 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-09 10:36:20 +02:00
|
|
|
*/
|
2019-09-03 11:58:42 +02:00
|
|
|
DBG("KernelIvyBridgeXCPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// this patch provides XCPM support for Ivy Bridge-E. by PMheart
|
|
|
|
// attempt to enable XCPM for Ivy-E, still need to test further
|
|
|
|
//
|
2020-05-09 09:56:30 +02:00
|
|
|
BOOLEAN LOADER_ENTRY::KernelIvyE5XCPM()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8 *kern = (UINT8*)KernelData;
|
2020-08-12 17:15:47 +02:00
|
|
|
XString8 comment;
|
2020-05-14 12:39:50 +02:00
|
|
|
// UINT32 i;
|
2020-05-09 09:56:30 +02:00
|
|
|
UINTN patchLocation;
|
2020-05-01 18:26:28 +02:00
|
|
|
UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// check whether Ivy Bridge-E5
|
|
|
|
if (gCPUStructure.Model != CPU_MODEL_IVY_BRIDGE_E5) {
|
|
|
|
DBG("KernelIvyE5XCPM(): Unsupported platform.\nRequires Ivy Bridge-E, aborted\n");
|
|
|
|
DBG("KernelIvyE5XCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check OS version suit for patches
|
|
|
|
// PMheart: attempt to add 10.15 compatibility
|
|
|
|
if (!IsXCPMOSVersionCompat(os_version)) {
|
|
|
|
DBG("KernelIvyE5XCPM(): Unsupported macOS.\n");
|
|
|
|
DBG("KernelIvyE5XCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// _cpuid_set_info
|
2020-05-09 09:56:30 +02:00
|
|
|
// TODO: should we use FakeCPUID instead?
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_cpuid_set_info"_XS8;
|
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.8.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xF8, 0x3C, 0x74, 0x2D };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xF8, 0x3E, 0x74, 0x2D };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version == AsciiOSVersionToUint64("10.9"_XS8) || os_version == AsciiOSVersionToUint64("10.9.1"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.9.0 - 10.9.1
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xF8, 0x3C, 0x75, 0x07 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xF8, 0x3E, 0x75, 0x07 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
} // 10.9.2+: native support reached, no need to patch
|
|
|
|
|
|
|
|
// _xcpm_pkg_scope_msrs
|
|
|
|
DBG("Searching _xcpm_pkg_scope_msrs ...\n");
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_xcpm_pkg_scope_msrs"_XS8;
|
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.8.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = {
|
2019-09-03 11:58:42 +02:00
|
|
|
0x48, 0x8D, 0x3D, 0x02, 0x71, 0x55, 0x00, 0xBE,
|
|
|
|
0x07, 0x00, 0x00, 0x00, 0xEB, 0x1F, 0x48, 0x8D,
|
|
|
|
0x3D, 0xF4, 0x70, 0x55, 0x00, 0xBE, 0x07, 0x00,
|
|
|
|
0x00, 0x00, 0x31, 0xD2, 0xE8, 0x28, 0x02, 0x00, 0x00
|
|
|
|
};
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 repl[] = {
|
2019-09-03 11:58:42 +02:00
|
|
|
0x48, 0x8D, 0x3D, 0x02, 0x71, 0x55, 0x00, 0xBE,
|
|
|
|
0x07, 0x00, 0x00, 0x00, 0x90, 0x90, 0x48, 0x8D,
|
|
|
|
0x3D, 0xF4, 0x70, 0x55, 0x00, 0xBE, 0x07, 0x00,
|
|
|
|
0x00, 0x00, 0x31, 0xD2, 0x90, 0x90, 0x90, 0x90, 0x90
|
|
|
|
};
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.10"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.9.x
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0xBE, 0x07, 0x00, 0x00, 0x00, 0x74, 0x13, 0x31, 0xD2, 0xE8, 0x5F, 0x02, 0x00, 0x00 };
|
|
|
|
const UINT8 repl[] = { 0xBE, 0x07, 0x00, 0x00, 0x00, 0x90, 0x90, 0x31, 0xD2, 0x90, 0x90, 0x90, 0x90, 0x90 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
|
|
|
// 10.10+
|
2020-05-09 07:51:12 +02:00
|
|
|
// patchLocation = 0; // clean out the value just in case
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN procLocation = searchProc("xcpm_init"_XS8);
|
2020-05-09 09:56:30 +02:00
|
|
|
UINTN symbol1 = searchProc(comment);
|
2020-05-09 07:51:12 +02:00
|
|
|
patchLocation = FindRelative32(kern, procLocation, 0x100, symbol1);
|
|
|
|
|
|
|
|
/* for (i = 0; i < 0x1000000; i++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
if (kern[i+0] == 0xBE && kern[i+1] == 0x07 && kern[i+2] == 0x00 && kern[i+3] == 0x00 &&
|
|
|
|
kern[i+4] == 0x00 && kern[i+5] == 0x31 && kern[i+6] == 0xD2 && kern[i+7] == 0xE8) {
|
|
|
|
patchLocation = i+7;
|
|
|
|
DBG("Found _xcpm_pkg_scope_msr\n");
|
|
|
|
break;
|
|
|
|
}
|
2020-05-09 07:51:12 +02:00
|
|
|
} */
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (patchLocation) {
|
2020-05-14 12:39:50 +02:00
|
|
|
for (int i = 7; i < 12; i++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
kern[patchLocation+i] = 0x90;
|
|
|
|
}
|
|
|
|
DBG("Applied _xcpm_pkg_scope_msr patch\n");
|
|
|
|
} else {
|
2020-05-08 09:11:47 +02:00
|
|
|
// DBG("_xcpm_pkg_scope_msr not found, patch aborted\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
DBG("KernelIvyE5XCPM() <===FALSE\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// _xcpm_bootstrap
|
2020-08-12 17:15:47 +02:00
|
|
|
comment = "_xcpm_bootstrap"_XS8;
|
|
|
|
if (os_version <= AsciiOSVersionToUint64("10.8.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.8.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xFB, 0x3C, 0x75, 0x54 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xFB, 0x3E, 0x75, 0x54 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.10"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.9.x
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xFB, 0x3C, 0x75, 0x68 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xFB, 0x3E, 0x75, 0x68 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.10.2"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.10 - 10.10.2
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xFB, 0x3C, 0x75, 0x63 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xFB, 0x3E, 0x75, 0x63 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.10.5"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.10.3 - 10.10.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC6, 0x83, 0xFB, 0x0D };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC4, 0x83, 0xFB, 0x0D };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.11"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.11 DB/PB - 10.11.0
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC6, 0x83, 0xFB, 0x0D };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC4, 0x83, 0xFB, 0x0D };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
|
|
|
} else if (os_version <= AsciiOSVersionToUint64("10.11.6"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.11.1 - 10.11.6
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xBB, 0x83, 0xFB, 0x09 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xB9, 0x83, 0xFB, 0x09 };
|
2020-08-12 17:15:47 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment.c_str());
|
2020-05-09 10:36:20 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
UINTN procLocation = searchProc(comment);
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN featureCall = searchProc("_cpuid_features"_XS8);
|
2020-05-09 10:36:20 +02:00
|
|
|
UINTN place = FindRelative32(KernelData, procLocation, 0x100, featureCall);
|
|
|
|
for (UINTN i = 10; i < 20; ++i) {
|
|
|
|
if (KernelData[place + i] == 0xC4) {
|
|
|
|
KernelData[place + i] = 0xC1;
|
2020-05-10 20:21:51 +02:00
|
|
|
if (KernelData[(place + i) - 5] == 0x3B) {
|
|
|
|
KernelData[(place + i) - 5] = 0x00;
|
|
|
|
}
|
2020-05-09 10:36:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if (os_version <= AsciiOSVersionToUint64("10.12.5")) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 10.12 - 10.12.5
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x83, 0xC3, 0xC4, 0x83, 0xFB, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x83, 0xC3, 0xC2, 0x83, 0xFB, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.13")) {
|
|
|
|
// 10.12.6
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x83, 0xF8, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC2, 0x83, 0xF8, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.14 compatibility
|
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15")) {
|
|
|
|
// 10.13/10.14
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x89, 0xD8, 0x04, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x89, 0xD8, 0x04, 0xC1, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2019-09-03 11:58:42 +02:00
|
|
|
// PMheart: attempt to add 10.15 compatibility
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.15.4")) {
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x8D, 0x43, 0xC4, 0x3C, 0x22 };
|
|
|
|
const UINT8 repl[] = { 0x8D, 0x43, 0xC1, 0x3C, 0x22 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2020-03-30 16:11:18 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.16")) {
|
2020-04-27 17:39:35 +02:00
|
|
|
// vector sigma: 10.15.5 Beta 2 build 19F62f and 10.15.4 build 19E287
|
2020-05-08 09:11:47 +02:00
|
|
|
const UINT8 find[] = { 0x3B, 0x7E, 0x2E, 0x80, 0xC3, 0xC4, 0x80, 0xFB, 0x42 };
|
|
|
|
const UINT8 repl[] = { 0x00, 0x7E, 0x2E, 0x80, 0xC3, 0xC1, 0x80, 0xFB, 0x42 };
|
2020-05-09 09:56:30 +02:00
|
|
|
applyKernPatch(find, sizeof(find), repl, comment);
|
2020-05-09 10:36:20 +02:00
|
|
|
*/
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG("KernelIvyE5XCPM() <===\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
2020-05-09 10:55:33 +02:00
|
|
|
#if 0
|
2019-09-03 11:58:42 +02:00
|
|
|
VOID Patcher_SSE3_6(VOID* kernelData)
|
|
|
|
{
|
|
|
|
UINT8* bytes = (UINT8*)kernelData;
|
|
|
|
UINT32 patchLocation1 = 0;
|
|
|
|
UINT32 patchLocation2 = 0;
|
|
|
|
UINT32 patchlast = 0;
|
|
|
|
UINT32 i;
|
|
|
|
//UINT32 Length = sizeof(kernelData);
|
|
|
|
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Start find SSE3 address\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
i=0;
|
|
|
|
//for (i=0;i<Length;i++)
|
|
|
|
while(TRUE) {
|
|
|
|
if (bytes[i] == 0x66 && bytes[i+1] == 0x0F && bytes[i+2] == 0x6F &&
|
|
|
|
bytes[i+3] == 0x44 && bytes[i+4] == 0x0E && bytes[i+5] == 0xF1 &&
|
|
|
|
bytes[i-1664-32] == 0x55
|
|
|
|
) {
|
|
|
|
patchLocation1 = i-1664-32;
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 data address at 0x%08X\n",patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// hasSSE2+..... title
|
|
|
|
if (bytes[i] == 0xE3 && bytes[i+1] == 0x07 && bytes[i+2] == 0x00 &&
|
|
|
|
bytes[i+3] == 0x00 && bytes[i+4] == 0x80 && bytes[i+5] == 0x07 &&
|
|
|
|
bytes[i+6] == 0xFF && bytes[i+7] == 0xFF && bytes[i+8] == 0x24 &&
|
|
|
|
bytes[i+9] == 0x01) {
|
|
|
|
patchLocation2 = i;
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 Title address at 0x%08X\n",patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation1 || !patchLocation2) {
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Can't found SSE3 data addres or Title address at 0x%08X 0x%08X\n", patchLocation1, patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 last data addres Start\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
i = patchLocation1 + 1500;
|
|
|
|
//for (i=(patchLocation1+1500); i<(patchLocation1+3000); i++)
|
|
|
|
while(TRUE) {
|
|
|
|
if (bytes[i] == 0x90 && bytes[i+1] == 0x90 && bytes[i+2] == 0x55 ) {
|
|
|
|
patchlast = (i+1) - patchLocation1;
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 last data addres at 0x%08X\n", patchlast);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchlast) {
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Can't found SSE3 data last addres at 0x%08X\n", patchlast);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// patch sse3_64 data
|
|
|
|
|
|
|
|
for (i=0; i<patchlast; i++) {
|
|
|
|
if (i<sizeof(sse3_patcher)) {
|
|
|
|
bytes[patchLocation1 + i] = sse3_patcher[i];
|
|
|
|
} else {
|
|
|
|
bytes[patchLocation1 + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// patch kHasSSE3 title
|
|
|
|
bytes[patchLocation2 + 0] = 0xFC;
|
|
|
|
bytes[patchLocation2 + 1] = 0x05;
|
|
|
|
bytes[patchLocation2 + 8] = 0x2C;
|
|
|
|
bytes[patchLocation2 + 9] = 0x00;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID Patcher_SSE3_5(VOID* kernelData)
|
|
|
|
{
|
|
|
|
UINT8* bytes = (UINT8*)kernelData;
|
|
|
|
UINT32 patchLocation1 = 0;
|
|
|
|
UINT32 patchLocation2 = 0;
|
|
|
|
UINT32 patchlast=0;
|
|
|
|
UINT32 Length = sizeof(kernelData);
|
|
|
|
UINT32 i;
|
|
|
|
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Start find SSE3 address\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
for (i=256; i<(Length-256); i++) {
|
|
|
|
if (bytes[i] == 0x66 && bytes[i+1] == 0x0F && bytes[i+2] == 0x6F &&
|
|
|
|
bytes[i+3] == 0x44 && bytes[i+4] == 0x0E && bytes[i+5] == 0xF1 &&
|
|
|
|
bytes[i-1680-32] == 0x55) {
|
|
|
|
patchLocation1 = i-1680-32;
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 data address at 0x%08X\n",patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// khasSSE2+..... title
|
|
|
|
if (bytes[i] == 0xF3 && bytes[i+1] == 0x07 && bytes[i+2] == 0x00 &&
|
|
|
|
bytes[i+3] == 0x00 && bytes[i+4] == 0x80 && bytes[i+5] == 0x07 &&
|
|
|
|
bytes[i+6] == 0xFF && bytes[i+7] == 0xFF && bytes[i+8] == 0x24 &&
|
|
|
|
bytes[i+9] == 0x01) {
|
|
|
|
patchLocation2 = i;
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 Title address at 0x%08X\n",patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchLocation1 || !patchLocation2) {
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Can't found SSE3 data addres or Title address at 0x%08X 0x%08X\n", patchLocation1, patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 last data addres Start\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
for (i=(patchLocation1+1500);i<Length;i++) {
|
|
|
|
if (bytes[i] == 0x90 && bytes[i+1] == 0x90 && bytes[i+2] == 0x55) {
|
|
|
|
patchlast = (i+1) - patchLocation1;
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Found SSE3 last data addres at 0x%08X\n", patchlast);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patchlast) {
|
2020-04-25 18:18:18 +02:00
|
|
|
// DBG("Can't found SSE3 data last addres at 0x%08X\n", patchlast);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// patech sse3_64 data
|
|
|
|
|
|
|
|
for (i=0; i<patchlast; i++) {
|
|
|
|
if (i<sizeof(sse3_5_patcher)) {
|
|
|
|
bytes[patchLocation1 + i] = sse3_5_patcher[i];
|
|
|
|
} else {
|
|
|
|
bytes[patchLocation1 + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// patch kHasSSE3 title
|
|
|
|
bytes[patchLocation2 + 0] = 0x0C;
|
|
|
|
bytes[patchLocation2 + 1] = 0x06;
|
|
|
|
bytes[patchLocation2 + 8] = 0x2C;
|
|
|
|
bytes[patchLocation2 + 9] = 0x00;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
VOID Patcher_SSE3_7()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
// not support yet
|
|
|
|
return;
|
|
|
|
}
|
2020-05-09 10:55:33 +02:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
UINT32 LOADER_ENTRY::Get_Symtab(UINT8* binary)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
UINT32 ncmds, cmdsize;
|
|
|
|
UINT32 binaryIndex;
|
|
|
|
UINTN cnt;
|
2020-07-22 22:23:38 +02:00
|
|
|
// UINT8* binary = &KernelData[KernelOffset];
|
2019-09-03 11:58:42 +02:00
|
|
|
struct load_command *loadCommand;
|
2020-07-22 22:23:38 +02:00
|
|
|
// struct symtab_command *symCmd;
|
2020-07-20 18:05:18 +02:00
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
if (MACH_GET_MAGIC(binary) != MH_MAGIC_64 ) {
|
|
|
|
DBG("wrong binary at 0x%llX\n", (UINTN)binary);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-20 18:05:18 +02:00
|
|
|
ncmds = MACH_GET_NCMDS(binary);
|
|
|
|
binaryIndex = sizeof(struct mach_header_64);
|
2020-07-24 20:59:51 +02:00
|
|
|
DBG("ncmd = %d\n", ncmds);
|
|
|
|
for (int j=0; j<20; ++j) {
|
|
|
|
DBG("%02x", binary[j]);
|
|
|
|
}
|
|
|
|
DBG("\n");
|
|
|
|
if (ncmds > 1000) ncmds = 0;
|
2020-07-20 18:05:18 +02:00
|
|
|
|
|
|
|
for (cnt = 0; cnt < ncmds; cnt++) {
|
|
|
|
loadCommand = (struct load_command *)(binary + binaryIndex);
|
|
|
|
cmdsize = loadCommand->cmdsize;
|
|
|
|
|
|
|
|
switch (loadCommand->cmd) {
|
|
|
|
case LC_SYMTAB:
|
2020-07-24 20:59:51 +02:00
|
|
|
// *symCmd = binaryIndex;
|
2020-07-20 18:05:18 +02:00
|
|
|
// struct symtab_command {
|
|
|
|
// uint32_t cmd; /* LC_SYMTAB == 2 */
|
|
|
|
// uint32_t cmdsize; /* sizeof(struct symtab_command) */
|
|
|
|
// uint32_t symoff; /* symbol table offset */
|
|
|
|
// uint32_t nsyms; /* number of symbol table entries */
|
|
|
|
// uint32_t stroff; /* string table offset */
|
|
|
|
// uint32_t strsize; /* string table size in bytes */
|
|
|
|
// };
|
2020-07-22 22:23:38 +02:00
|
|
|
//AddrVtable = symCmd->symoff;
|
|
|
|
//SizeVtable = symCmd->nsyms;
|
|
|
|
//NamesTable = symCmd->stroff;
|
|
|
|
//DBG("SymTab: AddrVtable=0x%x SizeVtable=0x%x NamesTable=0x%x\n", AddrVtable, SizeVtable, NamesTable);
|
2020-07-24 20:59:51 +02:00
|
|
|
return binaryIndex; //continue search for last command? or return here?
|
2020-07-20 18:05:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
binaryIndex += cmdsize;
|
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
return 0;
|
2020-07-20 18:05:18 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
void DumpSeg(struct segment_command_64 *segCmd64)
|
2020-07-22 22:23:38 +02:00
|
|
|
{
|
|
|
|
DBG("segCmd64->segname = %s\n",segCmd64->segname);
|
|
|
|
DBG("segCmd64->vmaddr = 0x%08llX\n",segCmd64->vmaddr);
|
|
|
|
DBG("segCmd64->vmsize = 0x%08llX\n",segCmd64->vmsize);
|
|
|
|
|
|
|
|
DBG("fileoff = 0x%08llX\n",segCmd64->fileoff);
|
|
|
|
DBG("filesize = 0x%08llX\n",segCmd64->filesize);
|
|
|
|
//DBG("maxprot = 0x%08X\n",segCmd64->maxprot);
|
|
|
|
//DBG("initprot = 0x%08X\n",segCmd64->initprot);
|
|
|
|
//DBG("nsects = 0x%08X\n",segCmd64->nsects);
|
|
|
|
DBG("flags = 0x%08X\n",segCmd64->flags);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
UINT32 LOADER_ENTRY::GetTextExec()
|
|
|
|
{
|
|
|
|
UINT32 ncmds, cmdsize;
|
|
|
|
UINT32 binaryIndex;
|
|
|
|
struct segment_command_64 *segCmd64;
|
|
|
|
|
|
|
|
ncmds = MACH_GET_NCMDS(KernelData);
|
|
|
|
binaryIndex = sizeof(struct mach_header_64);
|
|
|
|
|
|
|
|
for (UINTN cnt = 0; cnt < ncmds; cnt++) {
|
|
|
|
segCmd64 = (struct segment_command_64 *)(KernelData + binaryIndex);
|
|
|
|
cmdsize = segCmd64->cmdsize;
|
|
|
|
|
|
|
|
switch (segCmd64->cmd) {
|
|
|
|
case LC_SEGMENT_64: //19
|
|
|
|
if (strcmp(segCmd64->segname, kTextExecSegment) == 0) {
|
|
|
|
return (UINT32)(segCmd64->fileoff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
binaryIndex += cmdsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOADER_ENTRY::Get_PreLink()
|
2020-07-20 18:05:18 +02:00
|
|
|
{
|
|
|
|
UINT32 ncmds, cmdsize;
|
|
|
|
UINT32 binaryIndex;
|
|
|
|
UINT8* binary = &KernelData[0];
|
|
|
|
struct load_command *loadCommand;
|
2020-07-24 20:59:51 +02:00
|
|
|
// struct segment_command *segCmd;
|
2019-09-03 11:58:42 +02:00
|
|
|
struct segment_command_64 *segCmd64;
|
2020-07-20 18:05:18 +02:00
|
|
|
// struct symtab_command *symCmd;
|
2020-07-22 22:23:38 +02:00
|
|
|
UINTN kernelvmAddr = 0;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (is64BitKernel) {
|
|
|
|
binaryIndex = sizeof(struct mach_header_64);
|
|
|
|
} else {
|
|
|
|
binaryIndex = sizeof(struct mach_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
ncmds = MACH_GET_NCMDS(binary);
|
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
for (UINTN cnt = 0; cnt < ncmds; cnt++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
loadCommand = (struct load_command *)(binary + binaryIndex);
|
|
|
|
cmdsize = loadCommand->cmdsize;
|
|
|
|
|
|
|
|
switch (loadCommand->cmd) {
|
2020-07-17 22:45:01 +02:00
|
|
|
case LC_SEGMENT_64: //19
|
2019-09-03 11:58:42 +02:00
|
|
|
segCmd64 = (struct segment_command_64 *)loadCommand;
|
2020-07-24 20:59:51 +02:00
|
|
|
if (strcmp(segCmd64->segname, kTextSegment) == 0) {
|
|
|
|
kernelvmAddr = segCmd64->vmaddr;
|
|
|
|
}
|
|
|
|
if (strcmp(segCmd64->segname, kPrelinkTextSegment) == 0) {
|
2020-07-22 22:23:38 +02:00
|
|
|
// DBG("Found PRELINK_TEXT, 64bit\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
if (segCmd64->vmsize > 0) {
|
|
|
|
// 64bit segCmd64->vmaddr is 0xffffff80xxxxxxxx
|
|
|
|
// PrelinkTextAddr = xxxxxxxx + KernelRelocBase
|
2020-07-22 22:23:38 +02:00
|
|
|
//? PrelinkTextAddr = (UINT32)(segCmd64->vmaddr ? (segCmd64->vmaddr - kernelvmAddr) + KernelRelocBase : 0);
|
2019-09-03 11:58:42 +02:00
|
|
|
PrelinkTextAddr = (UINT32)(segCmd64->vmaddr ? segCmd64->vmaddr + KernelRelocBase : 0);
|
2020-07-24 20:59:51 +02:00
|
|
|
PrelinkTextSize = (UINT32)(segCmd64->filesize);
|
|
|
|
PrelinkTextLoadCmdAddr = binaryIndex; //(UINT32)(UINTN)segCmd64;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-07-25 08:36:00 +02:00
|
|
|
#if KERNEL_DEBUG
|
2020-07-24 20:59:51 +02:00
|
|
|
DumpSeg(segCmd64);
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("PrelinkTextLoadCmdAddr = 0x%X, PrelinkTextAddr = 0x%X, PrelinkTextSize = 0x%X\n",
|
2019-09-03 11:58:42 +02:00
|
|
|
PrelinkTextLoadCmdAddr, PrelinkTextAddr, PrelinkTextSize);
|
2020-07-24 20:59:51 +02:00
|
|
|
UINT32 PrelinkTextAddr32 = (UINT32)(segCmd64->vmaddr - kernelvmAddr);
|
2020-08-06 20:53:01 +02:00
|
|
|
for (int j=0; j<30; ++j) {
|
2020-07-22 22:23:38 +02:00
|
|
|
DBG("%02x", binary[PrelinkTextAddr32+j]);
|
2020-07-20 18:05:18 +02:00
|
|
|
}
|
|
|
|
DBG("\n");
|
2020-07-25 08:36:00 +02:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
if (strcmp(segCmd64->segname, kPrelinkInfoSegment) == 0) {
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 sectionIndex;
|
|
|
|
struct section_64 *sect;
|
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
DumpSeg(segCmd64);
|
2019-09-03 11:58:42 +02:00
|
|
|
sectionIndex = sizeof(struct segment_command_64);
|
|
|
|
|
|
|
|
while(sectionIndex < segCmd64->cmdsize) {
|
|
|
|
sect = (struct section_64 *)((UINT8*)segCmd64 + sectionIndex);
|
|
|
|
|
2020-07-24 20:59:51 +02:00
|
|
|
if(strcmp(sect->sectname, kPrelinkInfoSection) == 0 && strcmp(sect->segname, kPrelinkInfoSegment) == 0) {
|
2019-09-03 11:58:42 +02:00
|
|
|
if (sect->size > 0) {
|
|
|
|
// 64bit sect->addr is 0xffffff80xxxxxxxx
|
|
|
|
// PrelinkInfoAddr = xxxxxxxx + KernelRelocBase
|
2020-07-24 20:59:51 +02:00
|
|
|
PrelinkInfoLoadCmdAddr = binaryIndex + sectionIndex; //(UINT32)(UINTN)sect;
|
2019-09-03 11:58:42 +02:00
|
|
|
PrelinkInfoAddr = (UINT32)(sect->addr ? sect->addr + KernelRelocBase : 0);
|
|
|
|
PrelinkInfoSize = (UINT32)sect->size;
|
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
DBG("__info found at 0x%llx: addr = 0x%x, size = 0x%llx\n", (UINTN)sect, sect->offset, sect->size);
|
2020-03-25 19:32:44 +01:00
|
|
|
DBG("PrelinkInfoLoadCmdAddr = 0x%X, PrelinkInfoAddr = 0x%X, PrelinkInfoSize = 0x%X\n",
|
2019-09-03 11:58:42 +02:00
|
|
|
PrelinkInfoLoadCmdAddr, PrelinkInfoAddr, PrelinkInfoSize);
|
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
sectionIndex += sizeof(struct section_64);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-07-24 20:59:51 +02:00
|
|
|
#if 0 //exclude 32bit
|
2019-09-03 11:58:42 +02:00
|
|
|
case LC_SEGMENT:
|
|
|
|
segCmd = (struct segment_command *)loadCommand;
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("segCmd->segname = %s\n",segCmd->segname);
|
|
|
|
//DBG("segCmd->vmaddr = 0x%08X\n",segCmd->vmaddr)
|
|
|
|
//DBG("segCmd->vmsize = 0x%08X\n",segCmd->vmsize);
|
2020-07-24 20:59:51 +02:00
|
|
|
if (strcmp(segCmd->segname, kPrelinkTextSegment) == 0) {
|
2020-07-22 22:23:38 +02:00
|
|
|
//DBG("Found PRELINK_TEXT, 32bit\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
if (segCmd->vmsize > 0) {
|
|
|
|
// PrelinkTextAddr = vmaddr + KernelRelocBase
|
|
|
|
PrelinkTextAddr = (UINT32)(segCmd->vmaddr ? segCmd->vmaddr + KernelRelocBase : 0);
|
|
|
|
PrelinkTextSize = (UINT32)segCmd->vmsize;
|
|
|
|
PrelinkTextLoadCmdAddr = (UINT32)(UINTN)segCmd;
|
|
|
|
}
|
2020-07-22 22:23:38 +02:00
|
|
|
//DBG("at 0x%llx: vmaddr = 0x%x, vmsize = 0x%x\n", (UINTN)segCmd, segCmd->vmaddr, segCmd->vmsize);
|
|
|
|
//DBG("PrelinkTextLoadCmdAddr = 0x%X, PrelinkTextAddr = 0x%X, PrelinkTextSize = 0x%X\n",
|
|
|
|
// PrelinkTextLoadCmdAddr, PrelinkTextAddr, PrelinkTextSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
//gBS->Stall(30*1000000);
|
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
if (strcmp(segCmd->segname, kPrelinkInfoSegment) == 0) {
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 sectionIndex;
|
|
|
|
struct section *sect;
|
|
|
|
|
2020-07-22 22:23:38 +02:00
|
|
|
//DBG("Found PRELINK_INFO, 32bit\n");
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("cmd = 0x%08X\n",segCmd->cmd);
|
|
|
|
//DBG("cmdsize = 0x%08X\n",segCmd->cmdsize);
|
2020-07-22 22:23:38 +02:00
|
|
|
//DBG("vmaddr = 0x%08X\n",segCmd->vmaddr);
|
|
|
|
//DBG("vmsize = 0x%08X\n",segCmd->vmsize);
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("fileoff = 0x%08X\n",segCmd->fileoff);
|
|
|
|
//DBG("filesize = 0x%08X\n",segCmd->filesize);
|
|
|
|
//DBG("maxprot = 0x%08X\n",segCmd->maxprot);
|
|
|
|
//DBG("initprot = 0x%08X\n",segCmd->initprot);
|
|
|
|
//DBG("nsects = 0x%08X\n",segCmd->nsects);
|
|
|
|
//DBG("flags = 0x%08X\n",segCmd->flags);
|
2019-09-03 11:58:42 +02:00
|
|
|
sectionIndex = sizeof(struct segment_command);
|
|
|
|
|
|
|
|
while(sectionIndex < segCmd->cmdsize) {
|
|
|
|
sect = (struct section *)((UINT8*)segCmd + sectionIndex);
|
|
|
|
sectionIndex += sizeof(struct section);
|
|
|
|
|
2020-08-06 20:53:01 +02:00
|
|
|
if(strcmp(sect->sectname, kPrelinkInfoSection) == 0 && strcmp(sect->segname, kPrelinkInfoSegment) == 0) {
|
2019-09-03 11:58:42 +02:00
|
|
|
if (sect->size > 0) {
|
|
|
|
// PrelinkInfoAddr = sect->addr + KernelRelocBase
|
|
|
|
PrelinkInfoLoadCmdAddr = (UINT32)(UINTN)sect;
|
|
|
|
PrelinkInfoAddr = (UINT32)(sect->addr ? sect->addr + KernelRelocBase : 0);
|
|
|
|
PrelinkInfoSize = (UINT32)sect->size;
|
|
|
|
}
|
2020-07-22 22:23:38 +02:00
|
|
|
//DBG("__info found at 0x%llx: addr = 0x%x, size = 0x%x\n", (UINTN)sect, sect->addr, sect->size);
|
|
|
|
//DBG("PrelinkInfoLoadCmdAddr = 0x%X, PrelinkInfoAddr = 0x%X, PrelinkInfoSize = 0x%X\n",
|
|
|
|
// PrelinkInfoLoadCmdAddr, PrelinkInfoAddr, PrelinkInfoSize);
|
2019-09-03 11:58:42 +02:00
|
|
|
//gBS->Stall(30*1000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-07-24 20:59:51 +02:00
|
|
|
|
2020-07-20 18:05:18 +02:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
binaryIndex += cmdsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
//gBS->Stall(20*1000000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2020-05-01 18:26:28 +02:00
|
|
|
LOADER_ENTRY::FindBootArgs()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
|
|
|
UINT8 archMode = sizeof(UINTN) * 8;
|
|
|
|
|
|
|
|
// start searching from 0x200000.
|
2020-05-01 18:26:28 +02:00
|
|
|
ptr = (UINT8*)0x200000ull;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
while(TRUE) {
|
|
|
|
|
|
|
|
// check bootargs for 10.7 and up
|
|
|
|
bootArgs2 = (BootArgs2*)ptr;
|
|
|
|
|
2020-06-24 06:20:45 +02:00
|
|
|
if (bootArgs2->Version==2 && (bootArgs2->Revision==0 || bootArgs2->Revision==1)
|
2019-09-03 11:58:42 +02:00
|
|
|
// plus additional checks - some values are not inited by boot.efi yet
|
|
|
|
&& bootArgs2->efiMode == archMode
|
|
|
|
&& bootArgs2->kaddr == 0 && bootArgs2->ksize == 0
|
|
|
|
&& bootArgs2->efiSystemTable == 0
|
|
|
|
) {
|
|
|
|
// set vars
|
|
|
|
dtRoot = (CHAR8*)(UINTN)bootArgs2->deviceTreeP;
|
2019-12-18 19:41:07 +01:00
|
|
|
dtLength = &bootArgs2->deviceTreeLength;
|
2019-09-03 11:58:42 +02:00
|
|
|
KernelSlide = bootArgs2->kslide;
|
|
|
|
|
2020-05-11 17:44:00 +02:00
|
|
|
DBG_RT( "Found bootArgs2 at 0x%llX, DevTree at 0x%llX\n", (UINTN)ptr, (UINTN)bootArgs2->deviceTreeP);
|
2020-06-27 04:57:27 +02:00
|
|
|
//DBG_RT("bootArgs2->kaddr = 0x%llX and bootArgs2->ksize = 0x%llX\n", bootArgs2->kaddr, bootArgs2->ksize);
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("bootArgs2->efiMode = 0x%02X\n", bootArgs2->efiMode);
|
2020-05-11 17:44:00 +02:00
|
|
|
DBG_RT( "bootArgs2->CommandLine = %s\n", bootArgs2->CommandLine);
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "bootArgs2->flags = 0x%hx\n", bootArgs2->flags);
|
|
|
|
DBG_RT( "bootArgs2->kslide = 0x%x\n", bootArgs2->kslide);
|
2020-05-11 17:44:00 +02:00
|
|
|
DBG_RT( "bootArgs2->bootMemStart = 0x%llx\n", bootArgs2->bootMemStart);
|
2020-08-11 14:43:53 +02:00
|
|
|
// if (KernelAndKextPatches && KernelAndKextPatches.KPDebug)
|
2020-07-24 20:59:51 +02:00
|
|
|
Stall(5000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// disable other pointer
|
|
|
|
bootArgs1 = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check bootargs for 10.4 - 10.6.x
|
2020-06-24 06:20:45 +02:00
|
|
|
/*
|
2019-09-03 11:58:42 +02:00
|
|
|
bootArgs1 = (BootArgs1*)ptr;
|
|
|
|
|
|
|
|
if (bootArgs1->Version==1
|
|
|
|
&& (bootArgs1->Revision==6 || bootArgs1->Revision==5 || bootArgs1->Revision==4)
|
|
|
|
// plus additional checks - some values are not inited by boot.efi yet
|
|
|
|
&& bootArgs1->efiMode == archMode
|
|
|
|
&& bootArgs1->kaddr == 0 && bootArgs1->ksize == 0
|
|
|
|
&& bootArgs1->efiSystemTable == 0
|
|
|
|
) {
|
|
|
|
// set vars
|
|
|
|
dtRoot = (CHAR8*)(UINTN)bootArgs1->deviceTreeP;
|
2019-12-18 19:41:07 +01:00
|
|
|
dtLength = &bootArgs1->deviceTreeLength;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-11 17:44:00 +02:00
|
|
|
DBG_RT( "Found bootArgs1 at 0x%8s, DevTree at %p\n", ptr, dtRoot);
|
2020-03-25 19:32:44 +01:00
|
|
|
//DBG("bootArgs1->kaddr = 0x%08X and bootArgs1->ksize = 0x%08X\n", bootArgs1->kaddr, bootArgs1->ksize);
|
|
|
|
//DBG("bootArgs1->efiMode = 0x%02X\n", bootArgs1->efiMode);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// disable other pointer
|
|
|
|
bootArgs2 = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2020-06-24 06:20:45 +02:00
|
|
|
*/
|
2019-09-03 11:58:42 +02:00
|
|
|
ptr += 0x1000;
|
2020-07-19 15:41:52 +02:00
|
|
|
if ((UINTN)ptr > 0x70000000ull) {
|
2020-06-24 06:20:45 +02:00
|
|
|
DBG_RT("bootArgs not found\n");
|
|
|
|
bootArgs2 = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
2020-05-09 09:56:30 +02:00
|
|
|
LOADER_ENTRY::KernelUserPatch()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-08-12 17:15:47 +02:00
|
|
|
INTN Num, y = 0;
|
2020-05-03 09:03:06 +02:00
|
|
|
|
|
|
|
|
2020-03-16 05:27:02 +01:00
|
|
|
// old confuse
|
2020-05-01 18:26:28 +02:00
|
|
|
// We are using KernelAndKextPatches as set by Custom Entries.
|
2020-04-25 18:18:18 +02:00
|
|
|
// while config patches go to gSettings.KernelAndKextPatches
|
|
|
|
// how to resolve it?
|
2020-05-01 18:26:28 +02:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
for (size_t i = 0 ; i < KernelAndKextPatches.KernelPatches.size(); ++i) {
|
|
|
|
DBG( "Patch[%zu]: %s\n", i, KernelAndKextPatches.KernelPatches[i].Label.c_str());
|
2020-08-11 14:43:53 +02:00
|
|
|
if (!KernelAndKextPatches.KernelPatches[i].MenuItem.BValue) {
|
|
|
|
//DBG_RT( "Patch[%d]: %a :: is not allowed for booted OS %a\n", i, KernelAndKextPatches.KernelPatches[i].Label, OSVersion);
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( "==> disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-12 17:15:47 +02:00
|
|
|
// if we modify directly KernelAndKextPatches.KernelPatches[i].SearchLen, it will wrong for next driver
|
|
|
|
UINTN SearchLen = KernelAndKextPatches.KernelPatches[i].SearchLen;
|
2020-04-27 17:16:43 +02:00
|
|
|
bool once = false;
|
|
|
|
UINTN procLen = 0;
|
2020-08-11 14:43:53 +02:00
|
|
|
UINTN procAddr = searchProc(KernelAndKextPatches.KernelPatches[i].ProcedureName);
|
2020-08-12 17:15:47 +02:00
|
|
|
DBG("procedure %s found at 0x%llx\n", KernelAndKextPatches.KernelPatches[i].ProcedureName.c_str(), procAddr);
|
2020-05-03 09:03:06 +02:00
|
|
|
if (SearchLen == 0) {
|
|
|
|
SearchLen = KERNEL_MAX_SIZE;
|
2020-05-05 21:15:59 +02:00
|
|
|
procLen = KERNEL_MAX_SIZE - procAddr;
|
|
|
|
once = true;
|
2020-04-27 17:16:43 +02:00
|
|
|
} else {
|
2020-05-03 09:03:06 +02:00
|
|
|
procLen = SearchLen;
|
2020-04-25 18:18:18 +02:00
|
|
|
}
|
2020-05-09 09:56:30 +02:00
|
|
|
UINT8 * curs = &KernelData[procAddr];
|
2020-04-25 18:18:18 +02:00
|
|
|
UINTN j = 0;
|
|
|
|
while (j < KERNEL_MAX_SIZE) {
|
2020-08-30 21:53:40 +02:00
|
|
|
if (KernelAndKextPatches.KernelPatches[i].StartPattern.isEmpty() || //old behavior
|
2020-05-02 05:38:38 +02:00
|
|
|
CompareMemMask((const UINT8*)curs,
|
2020-08-12 17:15:47 +02:00
|
|
|
KernelAndKextPatches.KernelPatches[i].StartPattern.data(),
|
|
|
|
KernelAndKextPatches.KernelPatches[i].StartPattern.size(),
|
|
|
|
KernelAndKextPatches.KernelPatches[i].StartMask.data(),
|
|
|
|
KernelAndKextPatches.KernelPatches[i].StartPattern.size())) {
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( " StartPattern found\n");
|
2020-04-25 18:18:18 +02:00
|
|
|
Num = SearchAndReplaceMask(curs,
|
2020-04-27 17:16:43 +02:00
|
|
|
procLen,
|
2020-08-12 17:15:47 +02:00
|
|
|
(const UINT8*)KernelAndKextPatches.KernelPatches[i].Data.data(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.KernelPatches[i].MaskFind.data(),
|
|
|
|
KernelAndKextPatches.KernelPatches[i].Data.size(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.KernelPatches[i].Patch.data(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.KernelPatches[i].MaskReplace.data(),
|
2020-08-11 14:43:53 +02:00
|
|
|
KernelAndKextPatches.KernelPatches[i].Count
|
2020-04-25 18:18:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (Num) {
|
|
|
|
y++;
|
2020-05-03 09:03:06 +02:00
|
|
|
curs += SearchLen - 1;
|
|
|
|
j += SearchLen - 1;
|
2020-04-25 18:18:18 +02:00
|
|
|
}
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
|
2020-08-30 21:53:40 +02:00
|
|
|
if ( once || KernelAndKextPatches.KernelPatches[i].StartPattern.isEmpty() ) {
|
2020-04-25 18:18:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j++; curs++;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPDebug) {
|
2019-09-03 11:58:42 +02:00
|
|
|
gBS->Stall(2000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (y != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
2020-05-01 18:26:28 +02:00
|
|
|
LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-08-12 17:15:47 +02:00
|
|
|
INTN Num, y = 0;
|
2020-05-03 09:03:06 +02:00
|
|
|
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
for (size_t i = 0 ; i < KernelAndKextPatches.BootPatches.size(); ++i)
|
|
|
|
{
|
|
|
|
// if we modify directly KernelAndKextPatches.BootPatches[i].SearchLen, it will wrong for next driver
|
|
|
|
UINTN SearchLen = KernelAndKextPatches.BootPatches[i].SearchLen;
|
|
|
|
if (!SearchLen) {
|
|
|
|
SearchLen = BooterSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG( "Patch[%zu]: %s\n", i, KernelAndKextPatches.BootPatches[i].Label.c_str());
|
2020-08-11 14:43:53 +02:00
|
|
|
if (!KernelAndKextPatches.BootPatches[i].MenuItem.BValue) {
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( "==> disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-25 18:18:18 +02:00
|
|
|
UINT8 * curs = BooterData;
|
|
|
|
UINTN j = 0;
|
|
|
|
while (j < BooterSize) {
|
2020-08-30 21:53:40 +02:00
|
|
|
if (KernelAndKextPatches.BootPatches[i].StartPattern.isEmpty() || //old behavior
|
2020-05-02 05:38:38 +02:00
|
|
|
CompareMemMask((const UINT8*)curs,
|
2020-08-12 17:15:47 +02:00
|
|
|
(const UINT8*)KernelAndKextPatches.BootPatches[i].StartPattern.data(),
|
|
|
|
KernelAndKextPatches.BootPatches[i].StartPattern.size(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.BootPatches[i].StartMask.data(),
|
|
|
|
KernelAndKextPatches.BootPatches[i].StartPattern.size())) {
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( " StartPattern found\n");
|
2020-04-25 18:18:18 +02:00
|
|
|
|
2020-05-03 19:16:50 +02:00
|
|
|
Num = SearchAndReplaceMask(curs,
|
2020-05-03 09:03:06 +02:00
|
|
|
SearchLen,
|
2020-08-12 17:15:47 +02:00
|
|
|
(const UINT8*)KernelAndKextPatches.BootPatches[i].Data.data(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.BootPatches[i].MaskFind.data(),
|
|
|
|
KernelAndKextPatches.BootPatches[i].Data.size(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.BootPatches[i].Patch.data(),
|
|
|
|
(const UINT8*)KernelAndKextPatches.BootPatches[i].MaskReplace.data(),
|
2020-08-11 14:43:53 +02:00
|
|
|
KernelAndKextPatches.BootPatches[i].Count
|
2020-04-25 18:18:18 +02:00
|
|
|
);
|
|
|
|
if (Num) {
|
|
|
|
y++;
|
2020-05-03 09:03:06 +02:00
|
|
|
curs += SearchLen - 1;
|
|
|
|
j += SearchLen - 1;
|
2020-04-25 18:18:18 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
|
2020-08-30 21:53:40 +02:00
|
|
|
if ( KernelAndKextPatches.BootPatches[i].StartPattern.isEmpty() ) {
|
2020-04-25 18:18:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j++; curs++;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 14:43:53 +02:00
|
|
|
// if (KernelAndKextPatches.KPDebug) {
|
2020-07-21 11:17:02 +02:00
|
|
|
// gBS->Stall(2000000);
|
|
|
|
// }
|
|
|
|
Stall(2000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
return (y != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2020-05-01 18:26:28 +02:00
|
|
|
LOADER_ENTRY::KernelAndKextPatcherInit()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
if (PatcherInited) {
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("patcher inited\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PatcherInited = TRUE;
|
|
|
|
|
|
|
|
// KernelRelocBase will normally be 0
|
|
|
|
// but if OsxAptioFixDrv is used, then it will be > 0
|
|
|
|
SetKernelRelocBase();
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("KernelRelocBase = %llx\n", KernelRelocBase);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Find bootArgs - we need then for proper detection
|
|
|
|
// of kernel Mach-O header
|
2020-05-01 18:26:28 +02:00
|
|
|
FindBootArgs();
|
2019-09-03 11:58:42 +02:00
|
|
|
if (bootArgs1 == NULL && bootArgs2 == NULL) {
|
2020-06-24 06:20:45 +02:00
|
|
|
DBG_RT("BootArgs not found - skipping patches!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find kernel Mach-O header:
|
2019-12-22 12:45:50 +01:00
|
|
|
// for 10.4 - 10.5: 0x00111000
|
|
|
|
// for 10.6 - 10.7: 0x00200000
|
2019-09-03 11:58:42 +02:00
|
|
|
// for ML: bootArgs2->kslide + 0x00200000
|
2019-12-21 11:29:57 +01:00
|
|
|
// for AptioFix booting - it's always at KernelRelocBase + 0x00200000
|
2019-12-22 12:45:50 +01:00
|
|
|
|
2020-06-27 04:57:27 +02:00
|
|
|
// UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2020-08-12 17:15:47 +02:00
|
|
|
DBG("os_version=%s\n", OSVersion.c_str());
|
2020-07-21 11:17:02 +02:00
|
|
|
|
2020-06-27 04:57:27 +02:00
|
|
|
// if (os_version < AsciiOSVersionToUint64("10.6")) {
|
|
|
|
// KernelData = (UINT8*)(UINTN)(KernelSlide + KernelRelocBase + 0x00111000);
|
|
|
|
// } else {
|
2020-07-23 22:42:29 +02:00
|
|
|
KernelData = (UINT8*)(UINTN)(KernelSlide + KernelRelocBase + 0x00200000);
|
2020-06-27 04:57:27 +02:00
|
|
|
// }
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// check that it is Mach-O header and detect architecture
|
|
|
|
if(MACH_GET_MAGIC(KernelData) == MH_MAGIC || MACH_GET_MAGIC(KernelData) == MH_CIGAM) {
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG("Found 32 bit kernel at 0x%llx\n", (UINTN)KernelData);
|
2019-09-03 11:58:42 +02:00
|
|
|
is64BitKernel = FALSE;
|
|
|
|
} else if (MACH_GET_MAGIC(KernelData) == MH_MAGIC_64 || MACH_GET_MAGIC(KernelData) == MH_CIGAM_64) {
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG( "Found 64 bit kernel at 0x%llx\n", (UINTN)KernelData);
|
2020-07-17 22:45:01 +02:00
|
|
|
// DBG_RT("text section is: %s\n", (const char*)&KernelData[0x28]);
|
2020-07-24 20:59:51 +02:00
|
|
|
/*
|
2020-07-17 22:45:01 +02:00
|
|
|
KernelOffset = 0;
|
|
|
|
while (KernelOffset < KERNEL_MAX_SIZE) {
|
2020-07-20 10:52:36 +02:00
|
|
|
if ((MACH_GET_MAGIC(KernelData+KernelOffset) == MH_MAGIC_64 ) || (MACH_GET_MAGIC(KernelData+KernelOffset) == MH_CIGAM_64)) {
|
|
|
|
DBG("dump at offset 0x%x\n", KernelOffset);
|
|
|
|
for (int j = 0; j<20; ++j) {
|
|
|
|
DBG("%02x ", KernelData[KernelOffset+j]);
|
|
|
|
}
|
|
|
|
DBG("\n");
|
|
|
|
if ((((struct mach_header_64*)(KernelData+KernelOffset))->filetype) == MH_EXECUTE) {
|
|
|
|
DBG("execute found\n");
|
|
|
|
break;
|
|
|
|
}
|
2020-07-17 22:45:01 +02:00
|
|
|
}
|
2020-07-18 20:06:10 +02:00
|
|
|
KernelOffset += 4;
|
2020-07-17 22:45:01 +02:00
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
*/
|
|
|
|
if ((((struct mach_header_64*)KernelData)->filetype) == MH_KERNEL_COLLECTION) {
|
|
|
|
// BigSur
|
|
|
|
KernelOffset = GetTextExec();
|
|
|
|
// DBG("BigSur: KernelOffset =0x%X\n", KernelOffset);
|
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
is64BitKernel = TRUE;
|
|
|
|
} else {
|
|
|
|
// not valid Mach-O header - exiting
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG( "Kernel not found at 0x%llx - skipping patches!\n", (UINTN)KernelData);
|
2019-09-03 11:58:42 +02:00
|
|
|
KernelData = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2020-07-24 20:59:51 +02:00
|
|
|
DBG( " kernel offset at 0x%x\n", KernelOffset);
|
2019-09-03 11:58:42 +02:00
|
|
|
// find __PRELINK_TEXT and __PRELINK_INFO
|
2020-07-24 20:59:51 +02:00
|
|
|
if ((((struct mach_header_64*)KernelData)->filetype) == MH_KERNEL_COLLECTION) {
|
|
|
|
Get_PreLink(); // BigSur
|
|
|
|
} else {
|
|
|
|
Get_PreLink();
|
|
|
|
}
|
2020-07-20 18:05:18 +02:00
|
|
|
//find symbol tables
|
2020-07-23 21:14:20 +02:00
|
|
|
struct symtab_command *symCmd = NULL;
|
2020-07-24 20:59:51 +02:00
|
|
|
UINT32 symCmdOffset = Get_Symtab(&KernelData[KernelOffset]);
|
2020-07-23 21:14:20 +02:00
|
|
|
if (symCmdOffset != 0) {
|
2020-07-23 22:42:29 +02:00
|
|
|
symCmd = (struct symtab_command *)&KernelData[KernelOffset + symCmdOffset];
|
|
|
|
AddrVtable = symCmd->symoff; //this offset relative to KernelData+0
|
2020-07-22 22:23:38 +02:00
|
|
|
SizeVtable = symCmd->nsyms;
|
|
|
|
NamesTable = symCmd->stroff;
|
|
|
|
DBG("Kernel: AddrVtable=0x%x SizeVtable=0x%x NamesTable=0x%x\n", AddrVtable, SizeVtable, NamesTable);
|
|
|
|
}
|
2020-07-17 22:45:01 +02:00
|
|
|
/*
|
2020-06-27 04:57:27 +02:00
|
|
|
for (UINTN i=0x00200000; i<0x30000000; i+=4) {
|
|
|
|
UINT32 *KD = (UINT32 *)i;
|
|
|
|
if ((KD[0] == MH_MAGIC_64) && (KD[0x0a] == 0x45545F5F)){
|
|
|
|
DBG_RT( "Found MAGIC at %llx, text=%s\n", i, (const char*)&KD[0x0a]);
|
|
|
|
DBG( "Found MAGIC at %llx, text=%s\n", i, (const char*)&KD[0x0a]);
|
|
|
|
KernelData = (UINT8*)KD;
|
|
|
|
DBG( "Found new kernel at 0x%llx\n", (UINTN)KernelData);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 22:45:01 +02:00
|
|
|
*/
|
2020-05-09 09:56:30 +02:00
|
|
|
if (EFI_ERROR(getVTable())) {
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG("error getting vtable: \n");
|
2020-05-02 05:38:38 +02:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-02 05:38:38 +02:00
|
|
|
isKernelcache = (PrelinkTextSize > 0) && (PrelinkInfoSize > 0);
|
2020-07-21 11:17:02 +02:00
|
|
|
DBG( "isKernelcache: %ls\n", isKernelcache ? L"Yes" : L"No");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2020-05-01 18:26:28 +02:00
|
|
|
LOADER_ENTRY::KernelAndKextsPatcherStart()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
BOOLEAN KextPatchesNeeded, patchedOk;
|
2020-05-11 17:44:00 +02:00
|
|
|
/*
|
|
|
|
* it was intended for custom entries but not work if no custom entries used
|
|
|
|
* so set common until better solution invented
|
|
|
|
*/
|
2020-08-11 14:43:53 +02:00
|
|
|
//KernelAndKextPatches = (KERNEL_AND_KEXT_PATCHES *)(((UINTN)&gSettings) + OFFSET_OF(SETTINGS_DATA, KernelAndKextPatches));
|
2020-08-12 17:15:47 +02:00
|
|
|
// CopyKernelAndKextPatches(&KernelAndKextPatches, &gSettings.KernelAndKextPatches);
|
|
|
|
KernelAndKextPatches = gSettings.KernelAndKextPatches;
|
2020-07-19 10:50:00 +02:00
|
|
|
|
|
|
|
PatcherInited = false;
|
2020-07-20 10:52:36 +02:00
|
|
|
KernelAndKextPatcherInit();
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
KextPatchesNeeded = (
|
2020-08-11 14:43:53 +02:00
|
|
|
KernelAndKextPatches.KPAppleIntelCPUPM ||
|
|
|
|
KernelAndKextPatches.KPAppleRTC ||
|
|
|
|
KernelAndKextPatches.EightApple ||
|
|
|
|
KernelAndKextPatches.KPDELLSMBIOS ||
|
2020-08-30 21:53:40 +02:00
|
|
|
KernelAndKextPatches.KPATIConnectorsPatch.notEmpty() ||
|
2020-08-12 17:15:47 +02:00
|
|
|
KernelAndKextPatches.KextPatches.size() > 0
|
2019-09-03 11:58:42 +02:00
|
|
|
);
|
|
|
|
|
2020-07-21 11:17:02 +02:00
|
|
|
// DBG_RT("\nKernelToPatch: ");
|
2020-08-12 17:15:47 +02:00
|
|
|
// DBG_RT("Kernels patches: %d\n", KernelAndKextPatches.KernelPatches.size());
|
|
|
|
if (gSettings.KernelPatchesAllowed && KernelAndKextPatches.KernelPatches.notEmpty()) {
|
2020-07-21 11:17:02 +02:00
|
|
|
// DBG_RT("Enabled: \n");
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("Kernels patches: enabled \n");
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2020-05-09 09:56:30 +02:00
|
|
|
if (EFI_ERROR(getVTable())) {
|
2020-05-27 20:04:31 +02:00
|
|
|
// DBG_RT("error getting vtable: \n");
|
2020-05-01 18:26:28 +02:00
|
|
|
goto NoKernelData;
|
|
|
|
}
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelUserPatch();
|
2020-07-21 11:17:02 +02:00
|
|
|
// DBG_RT(patchedOk ? " OK\n" : " FAILED!\n");
|
2020-03-16 05:27:02 +01:00
|
|
|
// gBS->Stall(5000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-07-21 11:17:02 +02:00
|
|
|
// DBG_RT("Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-09 10:55:33 +02:00
|
|
|
/*
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nKernelCpu patch: ");
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPKernelCpu) {
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// Kernel patches
|
|
|
|
//
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Enabled: \n");
|
|
|
|
KernelAndKextPatcherInit();
|
2019-09-03 11:58:42 +02:00
|
|
|
if (KernelData == NULL) goto NoKernelData;
|
|
|
|
if(is64BitKernel) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "64 bit patch ...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelPatcher_64();
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "32 bit patch ...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelPatcher_32();
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( " OK\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-09 10:55:33 +02:00
|
|
|
*/
|
2019-09-03 11:58:42 +02:00
|
|
|
//other method for KernelCPU patch is FakeCPUID
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nFakeCPUID patch: ");
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.FakeCPUID) {
|
|
|
|
DBG_RT( "Enabled: 0x%06x\n", KernelAndKextPatches.FakeCPUID);
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelCPUIDPatch();
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
// CPU power management patch for CPU with locked msr
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nKernelPm patch: ");
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPKernelPm || KernelAndKextPatches.KPKernelXCPM) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Enabled: \n");
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG( "KernelPm patch: Enabled\n");
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
patchedOk = FALSE;
|
|
|
|
if (is64BitKernel) {
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelPatchPm();
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( patchedOk ? " OK\n" : " FAILED!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Patch to not dump kext at panic (c)vit9696
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nPanicNoKextDump patch: ");
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPPanicNoKextDump) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Enabled: \n");
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelPanicNoKextDump();
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( patchedOk ? " OK\n" : " FAILED!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Lapic Panic Kernel Patch
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nKernelLapic patch: ");
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPKernelLapic) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Enabled: \n");
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
if(is64BitKernel) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "64-bit patch ...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelLapicPatch_64();
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "32-bit patch ...\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelLapicPatch_32();
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( patchedOk ? " OK\n" : " FAILED!\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPKernelXCPM) {
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// syscl - EnableExtCpuXCPM: Enable unsupported CPU's PowerManagement
|
|
|
|
//
|
2020-05-01 18:26:28 +02:00
|
|
|
// EnableExtCpuXCPM = NULL;
|
2019-09-03 11:58:42 +02:00
|
|
|
patchedOk = FALSE;
|
2020-05-09 07:22:31 +02:00
|
|
|
// BOOLEAN apply_idle_patch = (gCPUStructure.Model >= CPU_MODEL_SKYLAKE_U) && gSettings.HWP;
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2020-05-01 18:26:28 +02:00
|
|
|
|
|
|
|
// syscl - now enable extra Cpu's PowerManagement
|
|
|
|
// only Intel support this feature till now
|
|
|
|
// move below code outside the if condition if AMD supports
|
|
|
|
// XCPM later on
|
2020-05-09 07:22:31 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
if (gCPUStructure.Vendor == CPU_VENDOR_INTEL) {
|
|
|
|
switch (gCPUStructure.Model) {
|
|
|
|
case CPU_MODEL_JAKETOWN:
|
|
|
|
// SandyBridge-E LGA2011
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = SandyBridgeEPM();
|
2019-09-03 11:58:42 +02:00
|
|
|
gSNBEAICPUFixRequire = TRUE; // turn on SandyBridge-E AppleIntelCPUPowerManagement Fix
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU_MODEL_IVY_BRIDGE:
|
|
|
|
// IvyBridge
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelIvyBridgeXCPM();
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU_MODEL_IVY_BRIDGE_E5:
|
|
|
|
// IvyBridge-E
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = KernelIvyE5XCPM();
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU_MODEL_HASWELL_E:
|
|
|
|
// Haswell-E
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = HaswellEXCPM();
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CPU_MODEL_BROADWELL_E5:
|
|
|
|
case CPU_MODEL_BROADWELL_DE:
|
|
|
|
// Broadwell-E/EP
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = BroadwellEPM();
|
2019-09-03 11:58:42 +02:00
|
|
|
gBDWEIOPCIFixRequire = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (gCPUStructure.Model >= CPU_MODEL_HASWELL &&
|
2020-05-01 18:26:28 +02:00
|
|
|
(AsciiStrStr(gCPUStructure.BrandString, "Celeron") ||
|
|
|
|
AsciiStrStr(gCPUStructure.BrandString, "Pentium"))) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// Haswell+ low-end CPU
|
2020-05-09 09:56:30 +02:00
|
|
|
patchedOk = HaswellLowEndXCPM();
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "EnableExtCpuXCPM - %s!\n", patchedOk? "OK" : "FAILED");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
Stall(2000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
//
|
|
|
|
// Kext patches
|
|
|
|
//
|
|
|
|
|
|
|
|
// we need to scan kexts if "InjectKexts true and CheckFakeSMC"
|
2020-05-01 18:26:28 +02:00
|
|
|
if (/*OSFLAG_ISSET(Flags, OSFLAG_WITHKEXTS) || */
|
|
|
|
OSFLAG_ISSET(Flags, OSFLAG_CHECKFAKESMC)) {
|
|
|
|
DBG_RT( "\nAllowing kext patching to check if FakeSMC is present\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
gSettings.KextPatchesAllowed = TRUE;
|
|
|
|
KextPatchesNeeded = TRUE;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nKextPatches Needed: %c, Allowed: %c ... ",
|
2019-09-03 11:58:42 +02:00
|
|
|
(KextPatchesNeeded ? L'Y' : L'n'),
|
|
|
|
(gSettings.KextPatchesAllowed ? L'Y' : L'n')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (KextPatchesNeeded && gSettings.KextPatchesAllowed) {
|
2020-07-20 10:52:36 +02:00
|
|
|
// DBG_RT( "\nKext patching INIT\n");
|
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "\nKext patching STARTED\n");
|
|
|
|
KextPatcherStart(); //is FakeSMC found in cache then inject will be disabled
|
|
|
|
DBG_RT( "\nKext patching ENDED\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT( "Disabled\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 04:57:27 +02:00
|
|
|
Stall(1000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Kext add
|
|
|
|
//
|
2020-08-11 14:43:53 +02:00
|
|
|
// if (KernelAndKextPatches.KPDebug) {
|
2020-04-25 18:18:18 +02:00
|
|
|
// if (OSFLAG_ISSET(Entry->Flags, OSFLAG_CHECKFAKESMC) &&
|
|
|
|
// OSFLAG_ISUNSET(Entry->Flags, OSFLAG_WITHKEXTS)) {
|
|
|
|
// disabled kext injection if FakeSMC is already present
|
|
|
|
// Entry->Flags = OSFLAG_UNSET(Entry->Flags, OSFLAG_WITHKEXTS); //Slice - we are already here
|
|
|
|
//
|
2020-05-01 18:26:28 +02:00
|
|
|
// DBG_RT( "\nInjectKexts: disabled because FakeSMC is already present and InjectKexts option set to Detect\n");
|
2020-04-25 18:18:18 +02:00
|
|
|
// gBS->Stall(500000);
|
|
|
|
// }
|
|
|
|
// }
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
if (OSFLAG_ISSET(Flags, OSFLAG_WITHKEXTS)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
UINT32 deviceTreeP;
|
2020-05-01 18:26:28 +02:00
|
|
|
UINT32 *deviceTreeLength;
|
2019-09-03 11:58:42 +02:00
|
|
|
EFI_STATUS Status;
|
|
|
|
UINTN DataSize;
|
|
|
|
|
|
|
|
// check if FSInject already injected kexts
|
|
|
|
DataSize = 0;
|
|
|
|
Status = gRT->GetVariable (L"FSInject.KextsInjected", &gEfiGlobalVariableGuid, NULL, &DataSize, NULL);
|
|
|
|
if (Status == EFI_BUFFER_TOO_SMALL) {
|
|
|
|
// var exists - just exit
|
2020-05-01 18:26:28 +02:00
|
|
|
|
|
|
|
DBG_RT( "\nInjectKexts: skipping, FSInject already injected them\n");
|
|
|
|
Stall(500000);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
|
|
|
// if (KernelData == NULL) goto NoKernelData;
|
2019-09-03 11:58:42 +02:00
|
|
|
if (bootArgs1 != NULL) {
|
|
|
|
deviceTreeP = bootArgs1->deviceTreeP;
|
2020-05-01 18:26:28 +02:00
|
|
|
deviceTreeLength = &bootArgs1->deviceTreeLength;
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (bootArgs2 != NULL) {
|
|
|
|
deviceTreeP = bootArgs2->deviceTreeP;
|
2020-05-01 18:26:28 +02:00
|
|
|
deviceTreeLength = &bootArgs2->deviceTreeLength;
|
2019-09-03 11:58:42 +02:00
|
|
|
} else return;
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
Status = InjectKexts(deviceTreeP, deviceTreeLength);
|
2020-06-27 04:57:27 +02:00
|
|
|
DBG_RT("Inject kexts done at 0x%llx\n", (UINTN)deviceTreeP);
|
2020-05-09 09:56:30 +02:00
|
|
|
if (!EFI_ERROR(Status)) KernelBooterExtensionsPatch();
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
NoKernelData:
|
2020-05-01 18:26:28 +02:00
|
|
|
Stall(5000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|