debug kext patching

Signed-off-by: SergeySlice <sergey.slice@gmail.com>
This commit is contained in:
SergeySlice 2020-07-21 12:17:02 +03:00
parent dddceaae36
commit 69e0e0327c
5 changed files with 47 additions and 35 deletions

View File

@ -2298,7 +2298,6 @@
A5456D2A23FC5AFC000BF18C /* spd.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = spd.cpp; sourceTree = "<group>"; };
A5456D2B23FC5AFC000BF18C /* nvidia.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = nvidia.cpp; sourceTree = "<group>"; };
A5456D2C23FC5AFC000BF18C /* ati.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ati.h; sourceTree = "<group>"; };
A5456D2D23FC5AFD000BF18C /* LoaderUefi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoaderUefi.h; sourceTree = "<group>"; };
A5456D2E23FC5AFD000BF18C /* Net.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Net.cpp; sourceTree = "<group>"; };
A5456D2F23FC5AFE000BF18C /* gma.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gma.h; sourceTree = "<group>"; };
A5456D3023FC5AFE000BF18C /* BdsConnect.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BdsConnect.cpp; sourceTree = "<group>"; };
@ -7809,7 +7808,6 @@
A5456D0E23FC5AF0000BF18C /* LegacyBiosThunk.h */,
A5456D1E23FC5AF7000BF18C /* LegacyBoot.cpp */,
A523132E2448B34000421FB2 /* LegacyBoot.h */,
A5456D2D23FC5AFD000BF18C /* LoaderUefi.h */,
A5456D4123FC5B05000BF18C /* memvendors.h */,
A5456D2E23FC5AFD000BF18C /* Net.cpp */,
A52313302448B35F00421FB2 /* Net.h */,

View File

@ -2230,16 +2230,16 @@ LOADER_ENTRY::KernelUserPatch()
// how to resolve it?
for (; i < KernelAndKextPatches->NrKernels; ++i) {
DBG_RT( "Patch[%lld]: %s\n", i, KernelAndKextPatches->KernelPatches[i].Label);
DBG( "Patch[%lld]: %s\n", i, KernelAndKextPatches->KernelPatches[i].Label);
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);
DBG_RT( "==> disabled\n");
DBG( "==> disabled\n");
continue;
}
bool once = false;
UINTN procLen = 0;
UINTN procAddr = searchProc(KernelAndKextPatches->KernelPatches[i].ProcedureName);
DBG_RT("procedure %s found at 0x%llx\n", KernelAndKextPatches->KernelPatches[i].ProcedureName, procAddr);
DBG("procedure %s found at 0x%llx\n", KernelAndKextPatches->KernelPatches[i].ProcedureName, procAddr);
if (SearchLen == 0) {
SearchLen = KERNEL_MAX_SIZE;
procLen = KERNEL_MAX_SIZE - procAddr;
@ -2256,7 +2256,7 @@ LOADER_ENTRY::KernelUserPatch()
KernelAndKextPatches->KernelPatches[i].StartPatternLen,
(const UINT8*)KernelAndKextPatches->KernelPatches[i].StartMask,
KernelAndKextPatches->KernelPatches[i].StartPatternLen)) {
DBG_RT( " StartPattern found\n");
DBG( " StartPattern found\n");
Num = SearchAndReplaceMask(curs,
procLen,
(const UINT8*)KernelAndKextPatches->KernelPatches[i].Data,
@ -2272,7 +2272,7 @@ LOADER_ENTRY::KernelUserPatch()
curs += SearchLen - 1;
j += SearchLen - 1;
}
DBG_RT( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
DBG( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
if (once ||
!KernelAndKextPatches->KernelPatches[i].StartPattern ||
!KernelAndKextPatches->KernelPatches[i].StartPatternLen) {
@ -2301,9 +2301,9 @@ LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
SearchLen = BooterSize;
}
for (; i < KernelAndKextPatches->NrBoots; ++i) {
DBG_RT( "Patch[%lld]: %s\n", i, KernelAndKextPatches->BootPatches[i].Label);
DBG( "Patch[%lld]: %s\n", i, KernelAndKextPatches->BootPatches[i].Label);
if (!KernelAndKextPatches->BootPatches[i].MenuItem.BValue) {
DBG_RT( "==> disabled\n");
DBG( "==> disabled\n");
continue;
}
UINT8 * curs = BooterData;
@ -2315,7 +2315,7 @@ LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
KernelAndKextPatches->BootPatches[i].StartPatternLen,
(const UINT8*)KernelAndKextPatches->BootPatches[i].StartMask,
KernelAndKextPatches->BootPatches[i].StartPatternLen)) {
DBG_RT( " StartPattern found\n");
DBG( " StartPattern found\n");
Num = SearchAndReplaceMask(curs,
SearchLen,
@ -2332,7 +2332,7 @@ LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
j += SearchLen - 1;
}
DBG_RT( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
DBG( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num);
if (!KernelAndKextPatches->BootPatches[i].StartPattern ||
!KernelAndKextPatches->BootPatches[i].StartPatternLen) {
break;
@ -2341,10 +2341,10 @@ LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize)
j++; curs++;
}
}
if (KernelAndKextPatches->KPDebug) {
gBS->Stall(2000000);
}
// if (KernelAndKextPatches->KPDebug) {
// gBS->Stall(2000000);
// }
Stall(2000000);
return (y != 0);
}
@ -2379,15 +2379,16 @@ LOADER_ENTRY::KernelAndKextPatcherInit()
// UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
DBG("os_version=%s\n", OSVersion);
// if (os_version < AsciiOSVersionToUint64("10.6")) {
// KernelData = (UINT8*)(UINTN)(KernelSlide + KernelRelocBase + 0x00111000);
// } else {
KernelData = (UINT8*)(UINTN)(KernelSlide + KernelRelocBase + 0x00200000);
KernelData = (UINT8*)(UINTN)(KernelSlide + KernelRelocBase + 0x00200000);
// }
// check that it is Mach-O header and detect architecture
if(MACH_GET_MAGIC(KernelData) == MH_MAGIC || MACH_GET_MAGIC(KernelData) == MH_CIGAM) {
DBG_RT("Found 32 bit kernel at 0x%llx\n", (UINTN)KernelData);
DBG("Found 32 bit kernel at 0x%llx\n", (UINTN)KernelData);
is64BitKernel = FALSE;
} else if (MACH_GET_MAGIC(KernelData) == MH_MAGIC_64 || MACH_GET_MAGIC(KernelData) == MH_CIGAM_64) {
DBG( "Found 64 bit kernel at 0x%llx\n", (UINTN)KernelData);
@ -2433,11 +2434,11 @@ LOADER_ENTRY::KernelAndKextPatcherInit()
}
*/
if (EFI_ERROR(getVTable())) {
DBG_RT("error getting vtable: \n");
DBG("error getting vtable: \n");
}
isKernelcache = (PrelinkTextSize > 0) && (PrelinkInfoSize > 0);
DBG_RT( "isKernelcache: %ls\n", isKernelcache ? L"Yes" : L"No");
DBG( "isKernelcache: %ls\n", isKernelcache ? L"Yes" : L"No");
}
VOID
@ -2463,10 +2464,10 @@ LOADER_ENTRY::KernelAndKextsPatcherStart()
((KernelAndKextPatches->NrKexts > 0) && (KernelAndKextPatches->KextPatches != NULL))
);
DBG_RT("\nKernelToPatch: ");
DBG_RT("Kernels patches: %d\n", KernelAndKextPatches->NrKernels);
// DBG_RT("\nKernelToPatch: ");
// DBG_RT("Kernels patches: %d\n", KernelAndKextPatches->NrKernels);
if (gSettings.KernelPatchesAllowed && (KernelAndKextPatches->KernelPatches != NULL) && KernelAndKextPatches->NrKernels) {
DBG_RT("Enabled: \n");
// DBG_RT("Enabled: \n");
DBG("Kernels patches: enabled \n");
// KernelAndKextPatcherInit();
// if (KernelData == NULL) goto NoKernelData;
@ -2475,10 +2476,10 @@ LOADER_ENTRY::KernelAndKextsPatcherStart()
goto NoKernelData;
}
patchedOk = KernelUserPatch();
DBG_RT(patchedOk ? " OK\n" : " FAILED!\n");
// DBG_RT(patchedOk ? " OK\n" : " FAILED!\n");
// gBS->Stall(5000000);
} else {
DBG_RT("Disabled\n");
// DBG_RT("Disabled\n");
}
/*
DBG_RT( "\nKernelCpu patch: ");

View File

@ -148,10 +148,11 @@ EFI_STATUS LOADER_ENTRY::LoadKext(IN EFI_FILE *RootDir, IN CHAR16 *FileName, IN
//try to find a planar kext, without Contents
TempName = PoolPrint(L"%s\\%s", FileName, L"Info.plist");
// snwprintf(TempName, 512, L"%s\\%s", FileName, "Info.plist");
infoDictBufferLength = 0;
Status = egLoadFile(RootDir, TempName, &infoDictBuffer, &infoDictBufferLength);
FreePool(TempName);
if (EFI_ERROR(Status)) {
MsgLog("Failed to load extra kext (Info.plist not found): %ls\n", FileName);
MsgLog("Failed to load extra kext : %ls status=%s\n", TempName, strerror(Status));
return EFI_NOT_FOUND;
}
NoContents = TRUE;

View File

@ -3,29 +3,39 @@
*
*/
//#include <IndustryStardard/MachO-loader.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#ifdef __cplusplus
}
#endif
#include <UefiLoader.h>
#include "Platform.h"
//#include "LoaderUefi.h"
//#include "device_tree.h"
#include "kernel_patcher.h"
#define OLD_METHOD 0
#ifndef DEBUG_ALL
#define KEXT_DEBUG 0
#define KEXT_DEBUG 1
#else
#define KEXT_DEBUG DEBUG_ALL
#endif
#if KEXT_DEBUG
#define DBG(...) printf(__VA_ARGS__);
#if KEXT_DEBUG == 2
#define DBG(...) printf(__VA_ARGS__);
#elif KEXT_DEBUG == 1
#define DBG(...) DebugLog(KEXT_DEBUG, __VA_ARGS__)
#else
#define DBG(...)
#endif
// runtime debug
#define DBG_RT(...) if ((KernelAndKextPatches != NULL) && KernelAndKextPatches->KPDebug) { printf(__VA_ARGS__); }
@ -619,6 +629,7 @@ VOID LOADER_ENTRY::AppleRTCPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPl
//RodionS
UINTN procLocation = searchProcInDriver(Driver, DriverSize, "updateChecksum");
DBG("updateChecksum at 0x%llx", procLocation);
if (procLocation != 0) {
Driver[procLocation] = 0xC3;
DBG_RT("AppleRTC: patched\n");
@ -639,6 +650,7 @@ VOID LOADER_ENTRY::AppleRTCPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPl
// disable kext injection InjectKexts()
//
// not used since 4242
#if 0
VOID LOADER_ENTRY::CheckForFakeSMC(CHAR8 *InfoPlist)
{
if (OSFLAG_ISSET(Flags, OSFLAG_CHECKFAKESMC) &&
@ -653,7 +665,7 @@ VOID LOADER_ENTRY::CheckForFakeSMC(CHAR8 *InfoPlist)
}
}
}
#endif
////////////////////////////////////
@ -1456,7 +1468,7 @@ VOID LOADER_ENTRY::PatchPrelinkedKexts()
//Slice
// I see no reason to disable kext injection if FakeSMC found in cache
//since rev4240 we have manual kext inject disable
CheckForFakeSMC(WholePlist);
// CheckForFakeSMC(WholePlist);
DictPtr = WholePlist;
while ((DictPtr = AsciiStrStr(DictPtr, "dict>")) != NULL) {

View File

@ -452,7 +452,7 @@ class REFIT_ABSTRACT_MENU_ENTRY
void ATIConnectorsPatchRegisterKexts(void *FSInject_v, void *ForceLoadKexts_v);
void AppleIntelCPUPMPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPlist, UINT32 InfoPlistSize);
void AppleRTCPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPlist, UINT32 InfoPlistSize);
void CheckForFakeSMC(CHAR8 *InfoPlist);
// void CheckForFakeSMC(CHAR8 *InfoPlist);
void DellSMBIOSPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPlist, UINT32 InfoPlistSize);
void SNBE_AICPUPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPlist, UINT32 InfoPlistSize);
void BDWE_IOPCIPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPlist, UINT32 InfoPlistSize);