2020-08-17 21:40:52 +02:00
|
|
|
#include <Platform.h> // Only use angled for Platform, else, xcode project won't compile
|
2020-04-16 11:09:22 +02:00
|
|
|
#include "kext_inject.h"
|
2020-04-16 09:15:26 +02:00
|
|
|
#include "DataHubCpu.h"
|
2020-08-25 17:35:19 +02:00
|
|
|
#include "../Platform/plist/plist.h"
|
2020-08-17 21:40:52 +02:00
|
|
|
#include "../Platform/Settings.h"
|
2020-08-25 17:35:19 +02:00
|
|
|
#include "../Platform/guid.h"
|
2020-03-26 13:59:20 +01:00
|
|
|
|
|
|
|
#ifndef DEBUG_ALL
|
2020-07-27 15:09:30 +02:00
|
|
|
#define KEXT_INJECT_DEBUG 1
|
2020-03-26 13:59:20 +01:00
|
|
|
#else
|
|
|
|
#define KEXT_INJECT_DEBUG DEBUG_ALL
|
|
|
|
#endif
|
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
#if KEXT_INJECT_DEBUG == 2
|
2020-07-20 10:52:36 +02:00
|
|
|
#define DBG(...) printf(__VA_ARGS__);
|
2019-09-03 11:58:42 +02:00
|
|
|
#elif KEXT_INJECT_DEBUG == 1
|
2020-07-20 10:52:36 +02:00
|
|
|
#define DBG(...) DebugLog(KEXT_INJECT_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
|
|
|
//
|
2020-05-02 05:38:38 +02:00
|
|
|
#define OLD_EXTRA_KEXT_PATCH 0
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
////////////////////
|
|
|
|
// globals
|
|
|
|
////////////////////
|
|
|
|
LIST_ENTRY gKextList = INITIALIZE_LIST_HEAD_VARIABLE (gKextList);
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
// before booting
|
|
|
|
////////////////////
|
|
|
|
EFI_STATUS EFIAPI ThinFatFile(IN OUT UINT8 **binary, IN OUT UINTN *length, IN cpu_type_t archCpuType)
|
|
|
|
{
|
|
|
|
UINT32 nfat, swapped, size = 0;
|
|
|
|
FAT_HEADER *fhp = (FAT_HEADER *)*binary;
|
|
|
|
FAT_ARCH *fap = (FAT_ARCH *)(*binary + sizeof(FAT_HEADER));
|
|
|
|
cpu_type_t fapcputype;
|
|
|
|
UINT32 fapoffset;
|
|
|
|
UINT32 fapsize;
|
|
|
|
|
|
|
|
swapped = 0;
|
|
|
|
if (fhp->magic == FAT_MAGIC) {
|
|
|
|
nfat = fhp->nfat_arch;
|
|
|
|
} else if (fhp->magic == FAT_CIGAM) {
|
|
|
|
nfat = SwapBytes32(fhp->nfat_arch);
|
|
|
|
swapped = 1;
|
|
|
|
//already thin
|
|
|
|
} else if (fhp->magic == THIN_X64){
|
|
|
|
if (archCpuType == CPU_TYPE_X86_64) {
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
} else if (fhp->magic == THIN_IA32){
|
|
|
|
if (archCpuType == CPU_TYPE_I386) {
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
MsgLog("Thinning fails\n");
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; nfat > 0; nfat--, fap++) {
|
|
|
|
if (swapped) {
|
|
|
|
fapcputype = SwapBytes32(fap->cputype);
|
|
|
|
fapoffset = SwapBytes32(fap->offset);
|
|
|
|
fapsize = SwapBytes32(fap->size);
|
|
|
|
} else {
|
|
|
|
fapcputype = fap->cputype;
|
|
|
|
fapoffset = fap->offset;
|
|
|
|
fapsize = fap->size;
|
|
|
|
}
|
|
|
|
if (fapcputype == archCpuType) {
|
|
|
|
*binary = (*binary + fapoffset);
|
|
|
|
size = fapsize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (length != 0) *length = size;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-08-15 15:47:56 +02:00
|
|
|
void toLowerStr(CHAR8 *tstr, IN CONST CHAR8 *str) {
|
2019-09-30 10:29:31 +02:00
|
|
|
UINT16 cnt = 0;
|
|
|
|
|
|
|
|
for (cnt = 0; *str != '\0' && cnt <= 0xFF; cnt++, str++, tstr++) {
|
|
|
|
if (*str >= 'A' && *str <= 'Z')
|
|
|
|
*tstr = 'a' + (*str - 'A');
|
|
|
|
else
|
|
|
|
*tstr = *str;
|
|
|
|
}
|
|
|
|
*tstr = '\0';
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:35:19 +02:00
|
|
|
BOOLEAN checkOSBundleRequired(UINT8 loaderType, const TagDict* dict)
|
2019-09-30 10:29:31 +02:00
|
|
|
{
|
|
|
|
BOOLEAN inject = TRUE;
|
2020-08-19 14:50:26 +02:00
|
|
|
const TagStruct* osBundleRequiredTag;
|
2020-08-15 15:47:56 +02:00
|
|
|
XString8 osbundlerequired;
|
2019-09-30 10:29:31 +02:00
|
|
|
|
2020-08-25 17:35:19 +02:00
|
|
|
osBundleRequiredTag = dict->propertyForKey("OSBundleRequired");
|
2020-08-15 15:47:56 +02:00
|
|
|
if (osBundleRequiredTag) {
|
2020-08-25 17:35:19 +02:00
|
|
|
osbundlerequired = osBundleRequiredTag->getString()->stringValue();
|
2020-08-15 15:47:56 +02:00
|
|
|
osbundlerequired.lowerAscii();
|
|
|
|
}
|
2019-09-30 10:29:31 +02:00
|
|
|
|
2020-05-16 21:30:29 +02:00
|
|
|
if (OSTYPE_IS_OSX_RECOVERY(loaderType) ||
|
|
|
|
OSTYPE_IS_OSX_INSTALLER(loaderType)) {
|
2020-08-15 15:47:56 +02:00
|
|
|
if ( osbundlerequired != "root"_XS8 &&
|
|
|
|
osbundlerequired != "local"_XS8 &&
|
|
|
|
osbundlerequired != "console"_XS8 &&
|
|
|
|
osbundlerequired != "network-root"_XS8 ) {
|
2019-12-19 09:48:27 +01:00
|
|
|
inject = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 10:29:31 +02:00
|
|
|
|
|
|
|
return inject;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
//extern VOID KernelAndKextPatcherInit(IN LOADER_ENTRY *Entry);
|
|
|
|
//extern VOID AnyKextPatch(UINT8 *Driver, UINT32 DriverSize, CHAR8 *InfoPlist, UINT32 InfoPlistSize, INT32 N, LOADER_ENTRY *Entry);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
EFI_STATUS LOADER_ENTRY::LoadKext(IN EFI_FILE *RootDir, IN CONST CHAR16 *FileName, IN cpu_type_t archCpuType, IN OUT VOID *kext_v)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT8* infoDictBuffer = NULL;
|
|
|
|
UINTN infoDictBufferLength = 0;
|
|
|
|
UINT8* executableFatBuffer = NULL;
|
|
|
|
UINT8* executableBuffer = NULL;
|
|
|
|
UINTN executableBufferLength = 0;
|
|
|
|
CHAR8* bundlePathBuffer = NULL;
|
|
|
|
UINTN bundlePathBufferLength = 0;
|
2020-08-09 17:55:30 +02:00
|
|
|
XStringW TempName;
|
|
|
|
XStringW Executable;
|
2020-08-25 17:35:19 +02:00
|
|
|
TagDict* dict = NULL;
|
2020-08-19 14:50:26 +02:00
|
|
|
const TagStruct* prop = NULL;
|
2019-09-03 11:58:42 +02:00
|
|
|
BOOLEAN NoContents = FALSE;
|
2019-09-30 10:29:31 +02:00
|
|
|
BOOLEAN inject = FALSE;
|
2019-09-03 11:58:42 +02:00
|
|
|
_BooterKextFileInfo *infoAddr = NULL;
|
2020-05-01 18:26:28 +02:00
|
|
|
_DeviceTreeBuffer *kext = (_DeviceTreeBuffer *)kext_v;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
TempName = SWPrintf("%ls\\%ls", FileName, L"Contents\\Info.plist");
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(TempName, 512, L"%s\\%s", FileName, "Contents\\Info.plist");
|
2020-08-09 17:55:30 +02:00
|
|
|
Status = egLoadFile(RootDir, TempName.wc_str(), &infoDictBuffer, &infoDictBufferLength);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
//try to find a planar kext, without Contents
|
2020-08-09 17:55:30 +02:00
|
|
|
TempName = SWPrintf("%ls\\%ls", FileName, L"Info.plist");
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(TempName, 512, L"%s\\%s", FileName, "Info.plist");
|
2020-07-21 11:17:02 +02:00
|
|
|
infoDictBufferLength = 0;
|
2020-08-09 17:55:30 +02:00
|
|
|
Status = egLoadFile(RootDir, TempName.wc_str(), &infoDictBuffer, &infoDictBufferLength);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (EFI_ERROR(Status)) {
|
2020-08-25 17:35:19 +02:00
|
|
|
MsgLog("Failed to load extra kext : %ls status=%s\n", TempName.wc_str(), efiStrError(Status));
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
NoContents = TRUE;
|
|
|
|
}
|
2020-08-25 17:35:19 +02:00
|
|
|
if( ParseXML((CHAR8*)infoDictBuffer, &dict,infoDictBufferLength)!=0 ) {
|
2019-09-03 11:58:42 +02:00
|
|
|
FreePool(infoDictBuffer);
|
2020-03-25 19:32:44 +01:00
|
|
|
MsgLog("Failed to load extra kext (failed to parse Info.plist): %ls\n", FileName);
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
2019-09-30 10:29:31 +02:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
inject = checkOSBundleRequired(LoaderType, dict);
|
2019-09-30 10:29:31 +02:00
|
|
|
if(!inject) {
|
2020-03-25 19:32:44 +01:00
|
|
|
MsgLog("Skipping kext injection by OSBundleRequired : %ls\n", FileName);
|
2019-09-30 10:29:31 +02:00
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:35:19 +02:00
|
|
|
prop = dict->propertyForKey("CFBundleExecutable");
|
|
|
|
if( prop != NULL && prop->isString() && prop->getString()->stringValue().notEmpty() ) {
|
|
|
|
Executable.takeValueFrom(prop->getString()->stringValue());
|
|
|
|
// AsciiStrToUnicodeStrS(prop->getString()->stringValue(), Executable, 256);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (NoContents) {
|
2020-08-09 17:55:30 +02:00
|
|
|
TempName = SWPrintf("%ls\\%ls", FileName, Executable.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(TempName, 512, "%s\\%s", FileName, Executable);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-08-09 17:55:30 +02:00
|
|
|
TempName = SWPrintf("%ls\\%ls\\%ls", FileName, L"Contents\\MacOS", Executable.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(TempName, 512, L"%s\\%s\\%s", FileName, "Contents\\MacOS",Executable);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-08-09 17:55:30 +02:00
|
|
|
Status = egLoadFile(RootDir, TempName.wc_str(), &executableFatBuffer, &executableBufferLength);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (EFI_ERROR(Status)) {
|
|
|
|
FreePool(infoDictBuffer);
|
2020-03-25 19:32:44 +01:00
|
|
|
MsgLog("Failed to load extra kext (executable not found): %ls\n", FileName);
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
executableBuffer = executableFatBuffer;
|
|
|
|
if (ThinFatFile(&executableBuffer, &executableBufferLength, archCpuType)) {
|
|
|
|
FreePool(infoDictBuffer);
|
|
|
|
FreePool(executableBuffer);
|
2020-03-25 19:32:44 +01:00
|
|
|
MsgLog("Thinning failed: %ls\n", FileName);
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bundlePathBufferLength = StrLen(FileName) + 1;
|
2020-08-15 15:47:56 +02:00
|
|
|
bundlePathBuffer = (__typeof__(bundlePathBuffer))AllocateZeroPool(bundlePathBufferLength);
|
2019-09-03 11:58:42 +02:00
|
|
|
UnicodeStrToAsciiStrS(FileName, bundlePathBuffer, bundlePathBufferLength);
|
|
|
|
|
|
|
|
kext->length = (UINT32)(sizeof(_BooterKextFileInfo) + infoDictBufferLength + executableBufferLength + bundlePathBufferLength);
|
|
|
|
infoAddr = (_BooterKextFileInfo *)AllocatePool(kext->length);
|
|
|
|
infoAddr->infoDictPhysAddr = sizeof(_BooterKextFileInfo);
|
|
|
|
infoAddr->infoDictLength = (UINT32)infoDictBufferLength;
|
|
|
|
infoAddr->executablePhysAddr = (UINT32)(sizeof(_BooterKextFileInfo) + infoDictBufferLength);
|
|
|
|
infoAddr->executableLength = (UINT32)executableBufferLength;
|
|
|
|
infoAddr->bundlePathPhysAddr = (UINT32)(sizeof(_BooterKextFileInfo) + infoDictBufferLength + executableBufferLength);
|
|
|
|
infoAddr->bundlePathLength = (UINT32)bundlePathBufferLength;
|
|
|
|
kext->paddr = (UINT32)(UINTN)infoAddr; // Note that we cannot free infoAddr because of this
|
|
|
|
CopyMem((CHAR8 *)infoAddr + sizeof(_BooterKextFileInfo), infoDictBuffer, infoDictBufferLength);
|
|
|
|
CopyMem((CHAR8 *)infoAddr + sizeof(_BooterKextFileInfo) + infoDictBufferLength, executableBuffer, executableBufferLength);
|
|
|
|
CopyMem((CHAR8 *)infoAddr + sizeof(_BooterKextFileInfo) + infoDictBufferLength + executableBufferLength, bundlePathBuffer, bundlePathBufferLength);
|
|
|
|
FreePool(infoDictBuffer);
|
|
|
|
FreePool(executableFatBuffer);
|
|
|
|
FreePool(bundlePathBuffer);
|
2020-08-22 15:39:24 +02:00
|
|
|
dict->FreeTag();
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
EFI_STATUS LOADER_ENTRY::AddKext(IN EFI_FILE *RootDir, IN CONST CHAR16 *FileName, IN cpu_type_t archCpuType)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
KEXT_ENTRY *KextEntry;
|
|
|
|
|
2019-12-21 01:31:49 +01:00
|
|
|
KextEntry = (__typeof__(KextEntry))AllocatePool (sizeof(KEXT_ENTRY));
|
2019-09-03 11:58:42 +02:00
|
|
|
KextEntry->Signature = KEXT_SIGNATURE;
|
2020-05-01 18:26:28 +02:00
|
|
|
Status = LoadKext(RootDir, FileName, archCpuType, &KextEntry->kext);
|
2019-09-03 11:58:42 +02:00
|
|
|
if(EFI_ERROR(Status)) {
|
|
|
|
FreePool(KextEntry);
|
|
|
|
} else {
|
|
|
|
InsertTailList (&gKextList, &KextEntry->Link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 GetListCount(LIST_ENTRY const* List)
|
|
|
|
{
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
UINT32 Count=0;
|
|
|
|
|
|
|
|
if(!IsListEmpty(List)) {
|
|
|
|
for (Link = List->ForwardLink; Link != List; Link = Link->ForwardLink)
|
|
|
|
Count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 GetKextCount()
|
|
|
|
{
|
|
|
|
return (UINT32)GetListCount(&gKextList);
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 GetKextsSize()
|
|
|
|
{
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
KEXT_ENTRY *KextEntry;
|
|
|
|
UINT32 kextsSize=0;
|
|
|
|
|
|
|
|
if(!IsListEmpty(&gKextList)) {
|
|
|
|
for (Link = gKextList.ForwardLink; Link != &gKextList; Link = Link->ForwardLink) {
|
|
|
|
KextEntry = CR(Link, KEXT_ENTRY, Link, KEXT_SIGNATURE);
|
|
|
|
kextsSize += RoundPage(KextEntry->kext.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kextsSize;
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
VOID LOADER_ENTRY::LoadPlugInKexts(IN EFI_FILE *RootDir, IN CONST CHAR16 *DirName, IN cpu_type_t archCpuType, IN BOOLEAN Force)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
|
|
|
REFIT_DIR_ITER PlugInIter;
|
|
|
|
EFI_FILE_INFO *PlugInFile;
|
2020-08-09 17:55:30 +02:00
|
|
|
XStringW FileName;
|
2020-05-01 18:26:28 +02:00
|
|
|
if ((RootDir == NULL) || (DirName == NULL)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DirIterOpen(RootDir, DirName, &PlugInIter);
|
|
|
|
while (DirIterNext(&PlugInIter, 1, L"*.kext", &PlugInFile)) {
|
2019-12-19 17:51:21 +01:00
|
|
|
if (PlugInFile->FileName[0] == '.' || StrStr(PlugInFile->FileName, L".kext") == NULL)
|
|
|
|
continue; // skip this
|
2020-08-09 17:55:30 +02:00
|
|
|
FileName = SWPrintf("%ls\\%ls", DirName, PlugInFile->FileName);
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(FileName, 512, "%s\\%s", DirName, PlugInFile->FileName);
|
2020-08-09 17:55:30 +02:00
|
|
|
MsgLog(" %ls PlugIn kext: %ls\n", Force ? L"Force" : L"Extra", FileName.wc_str());
|
|
|
|
AddKext( RootDir, FileName.wc_str(), archCpuType);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
DirIterClose(&PlugInIter);
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
VOID LOADER_ENTRY::AddKexts(const XStringW& SrcDir, const XStringW& Path, cpu_type_t archCpuType)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-08-09 17:55:30 +02:00
|
|
|
XStringW FileName;
|
|
|
|
XStringW PlugInName;
|
2019-09-03 11:58:42 +02:00
|
|
|
SIDELOAD_KEXT *CurrentKext;
|
|
|
|
SIDELOAD_KEXT *CurrentPlugInKext;
|
2019-09-30 10:29:31 +02:00
|
|
|
EFI_STATUS Status;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
MsgLog("Preparing kexts injection from %ls\n", SrcDir.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
CurrentKext = InjectKextList;
|
|
|
|
while (CurrentKext) {
|
2020-06-27 04:57:27 +02:00
|
|
|
// DBG(" current kext name=%ls path=%ls, match against=%ls\n", CurrentKext->FileName, CurrentKext->KextDirNameUnderOEMPath, Path);
|
2020-08-12 17:15:47 +02:00
|
|
|
if ( CurrentKext->KextDirNameUnderOEMPath == Path ) {
|
|
|
|
FileName = SWPrintf("%ls\\%ls", SrcDir.wc_str(), CurrentKext->FileName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(FileName, 512, "%s\\%s", SrcDir, CurrentKext->FileName);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (!(CurrentKext->MenuItem.BValue)) {
|
|
|
|
// inject require
|
2020-08-09 17:55:30 +02:00
|
|
|
MsgLog("->Extra kext: %ls (v.%ls)\n", FileName.wc_str(), CurrentKext->Version.wc_str());
|
|
|
|
Status = AddKext(SelfVolume->RootDir, FileName.wc_str(), archCpuType);
|
2019-09-30 10:29:31 +02:00
|
|
|
if(!EFI_ERROR(Status)) {
|
2019-12-19 17:51:21 +01:00
|
|
|
// decide which plugins to inject
|
|
|
|
CurrentPlugInKext = CurrentKext->PlugInList;
|
|
|
|
while (CurrentPlugInKext) {
|
2020-08-09 17:55:30 +02:00
|
|
|
PlugInName = SWPrintf("%ls\\%ls\\%ls", FileName.wc_str(), L"Contents\\PlugIns", CurrentPlugInKext->FileName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(PlugInName, 512, L"%s\\%s\\%s", FileName, "Contents\\PlugIns", CurrentPlugInKext->FileName);
|
2019-12-19 17:51:21 +01:00
|
|
|
if (!(CurrentPlugInKext->MenuItem.BValue)) {
|
|
|
|
// inject PlugIn require
|
2020-08-09 17:55:30 +02:00
|
|
|
MsgLog(" |-- PlugIn kext: %ls (v.%ls)\n", PlugInName.wc_str(), CurrentPlugInKext->Version.wc_str());
|
|
|
|
AddKext(SelfVolume->RootDir, PlugInName.wc_str(), archCpuType);
|
2019-12-19 17:51:21 +01:00
|
|
|
} else {
|
2020-08-09 17:55:30 +02:00
|
|
|
MsgLog(" |-- Disabled plug-in kext: %ls (v.%ls)\n", PlugInName.wc_str(), CurrentPlugInKext->Version.wc_str());
|
2019-12-19 17:51:21 +01:00
|
|
|
}
|
|
|
|
CurrentPlugInKext = CurrentPlugInKext->Next;
|
|
|
|
} // end of plug-in kext injection
|
2019-09-30 10:29:31 +02:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
|
|
|
// disable current kext injection
|
2020-08-12 17:15:47 +02:00
|
|
|
if ( SrcDir.containsIC(L"Off") ) {
|
2020-08-09 17:55:30 +02:00
|
|
|
MsgLog("Disabled kext: %ls (v.%ls)\n", FileName.wc_str(), CurrentKext->Version.wc_str());
|
2020-03-15 14:42:45 +01:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrentKext = CurrentKext->Next;
|
|
|
|
} // end of kext injection
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
EFI_STATUS LOADER_ENTRY::LoadKexts()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-08-09 17:55:30 +02:00
|
|
|
XStringW SrcDir;
|
2019-09-03 11:58:42 +02:00
|
|
|
REFIT_DIR_ITER PlugInIter;
|
2020-08-09 17:55:30 +02:00
|
|
|
EFI_FILE_INFO *PlugInFile;
|
|
|
|
XStringW FileName;
|
|
|
|
XStringW PlugIns;
|
2020-05-02 21:57:41 +02:00
|
|
|
// CONST CHAR16 *Arch = NULL;
|
2020-04-05 14:25:39 +02:00
|
|
|
// CONST CHAR16 *Ptr = NULL;
|
2019-09-03 11:58:42 +02:00
|
|
|
#if defined(MDE_CPU_X64)
|
|
|
|
cpu_type_t archCpuType=CPU_TYPE_X86_64;
|
|
|
|
#else
|
|
|
|
cpu_type_t archCpuType=CPU_TYPE_I386;
|
|
|
|
#endif
|
|
|
|
UINTN mm_extra_size;
|
|
|
|
VOID *mm_extra;
|
|
|
|
UINTN extra_size;
|
|
|
|
VOID *extra;
|
|
|
|
|
|
|
|
|
|
|
|
// Make Arch point to the last appearance of "arch=" in LoadOptions (which is what boot.efi will use).
|
2020-05-02 21:57:41 +02:00
|
|
|
// if (LoadOptions.notEmpty()) {
|
2020-05-01 18:26:28 +02:00
|
|
|
// for (Ptr = StrStr(LoadOptions, L"arch="); Ptr != NULL; Arch = Ptr + StrLen(L"arch="), Ptr = StrStr(Arch, L"arch="));
|
2020-05-02 21:57:41 +02:00
|
|
|
// }
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-02 21:57:41 +02:00
|
|
|
// if (Arch != NULL && StrnCmp(Arch,L"x86_64",StrLen(L"x86_64")) == 0) {
|
2020-08-06 20:53:01 +02:00
|
|
|
if (LoadOptions.contains("arch=x86_64")) {
|
2019-09-03 11:58:42 +02:00
|
|
|
archCpuType = CPU_TYPE_X86_64;
|
2020-05-02 21:57:41 +02:00
|
|
|
// } else if (Arch != NULL && StrnCmp(Arch,L"i386",StrLen(L"i386")) == 0) {
|
2020-08-06 20:53:01 +02:00
|
|
|
} else if (LoadOptions.contains("arch=i386")) {
|
2019-09-03 11:58:42 +02:00
|
|
|
archCpuType = CPU_TYPE_I386;
|
2020-08-12 17:15:47 +02:00
|
|
|
} else if (OSVersion.notEmpty()) {
|
2020-05-01 18:26:28 +02:00
|
|
|
UINT64 os_version = AsciiOSVersionToUint64(OSVersion);
|
2020-08-12 17:15:47 +02:00
|
|
|
if (os_version >= AsciiOSVersionToUint64("10.8"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
archCpuType = CPU_TYPE_X86_64; // For OSVersion >= 10.8, only x86_64 exists
|
2020-08-12 17:15:47 +02:00
|
|
|
} else if (os_version < AsciiOSVersionToUint64("10.7"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
archCpuType = CPU_TYPE_I386; // For OSVersion < 10.7, use default of i386
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force kexts to load
|
2020-08-12 17:15:47 +02:00
|
|
|
if ( KernelAndKextPatches.ForceKexts.notEmpty() ) {
|
|
|
|
for (size_t i = 0; i < KernelAndKextPatches.ForceKexts.size(); ++i) {
|
|
|
|
MsgLog(" Force kext: %ls\n", KernelAndKextPatches.ForceKexts[i].wc_str());
|
2020-05-01 18:26:28 +02:00
|
|
|
if (Volume && Volume->RootDir) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// Check if the entry is a directory
|
2020-08-12 17:15:47 +02:00
|
|
|
if (StrStr(KernelAndKextPatches.ForceKexts[i].wc_str(), L".kext") == NULL) {
|
|
|
|
DirIterOpen(Volume->RootDir, KernelAndKextPatches.ForceKexts[i].wc_str(), &PlugInIter);
|
2019-09-03 11:58:42 +02:00
|
|
|
while (DirIterNext(&PlugInIter, 1, L"*.kext", &PlugInFile)) {
|
|
|
|
if (PlugInFile->FileName[0] == '.' || StrStr(PlugInFile->FileName, L".kext") == NULL)
|
|
|
|
continue; // skip this
|
2020-08-12 17:15:47 +02:00
|
|
|
FileName = SWPrintf("%ls\\%ls", KernelAndKextPatches.ForceKexts[i].wc_str(), PlugInFile->FileName);
|
2020-08-11 14:43:53 +02:00
|
|
|
// snwprintf(FileName, 512, "%s\\%s", KernelAndKextPatches.ForceKexts[i], PlugInFile->FileName);
|
2020-08-09 17:55:30 +02:00
|
|
|
MsgLog(" Force kext: %ls\n", FileName.wc_str());
|
|
|
|
AddKext( Volume->RootDir, FileName.wc_str(), archCpuType);
|
|
|
|
PlugIns = SWPrintf("%ls\\Contents\\PlugIns", FileName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(PlugIns, 512, "%s\\Contents\\PlugIns", FileName);
|
2020-08-09 17:55:30 +02:00
|
|
|
LoadPlugInKexts(Volume->RootDir, PlugIns.wc_str(), archCpuType, TRUE);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
DirIterClose(&PlugInIter);
|
|
|
|
} else {
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKext( Volume->RootDir, KernelAndKextPatches.ForceKexts[i].wc_str(), archCpuType);
|
|
|
|
PlugIns = SWPrintf("%ls\\Contents\\PlugIns", KernelAndKextPatches.ForceKexts[i].wc_str());
|
2020-08-11 14:43:53 +02:00
|
|
|
// snwprintf(PlugIns, 512, "%s\\Contents\\PlugIns", KernelAndKextPatches.ForceKexts[i]);
|
2020-08-09 17:55:30 +02:00
|
|
|
LoadPlugInKexts(Volume->RootDir, PlugIns.wc_str(), archCpuType, TRUE);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
XStringW UniOSVersion;
|
|
|
|
UniOSVersion = OSVersion;
|
|
|
|
DBG("UniOSVersion == %ls\n", UniOSVersion.wc_str());
|
2019-12-09 11:22:15 +01:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
XStringW UniShortOSVersion;
|
|
|
|
// XString8 ShortOSVersion;
|
|
|
|
if (AsciiOSVersionToUint64(OSVersion) < AsciiOSVersionToUint64("10.10"_XS8)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// OSVersion that are earlier than 10.10(form: 10.x.y)
|
2020-08-12 17:15:47 +02:00
|
|
|
// ShortOSVersion.strncpy(OSVersion.c_str(), 4);
|
|
|
|
UniShortOSVersion.strncpy(OSVersion.c_str(), 4);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-08-12 17:15:47 +02:00
|
|
|
// ShortOSVersion.strncpy(OSVersion.c_str(), 5);
|
|
|
|
UniShortOSVersion.strncpy(OSVersion.c_str(), 5);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-08-12 17:15:47 +02:00
|
|
|
// DBG("ShortOSVersion == %s\n", ShortOSVersion.c_str());
|
|
|
|
DBG("UniShortOSVersion == %ls\n", UniShortOSVersion.wc_str());
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// syscl - allow specific load inject kext
|
|
|
|
// Clover/Kexts/Other is for general injection thus we need to scan both Other and OSVersion folder
|
2020-08-09 17:55:30 +02:00
|
|
|
SrcDir = GetOtherKextsDir(TRUE);
|
|
|
|
if ( SrcDir.notEmpty() ) {
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(SrcDir, L"Other"_XSW, archCpuType);
|
2019-12-23 12:36:26 +01:00
|
|
|
} else {
|
|
|
|
DBG("GetOtherKextsDir(TRUE) return NULL\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
// slice: CLOVER/kexts/Off keep disabled kext which can be allowed
|
2020-08-09 17:55:30 +02:00
|
|
|
SrcDir = GetOtherKextsDir(FALSE);
|
|
|
|
if ( SrcDir.notEmpty() ) {
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(SrcDir, L"Off"_XSW, archCpuType);
|
2019-12-23 12:36:26 +01:00
|
|
|
} else {
|
|
|
|
DBG("GetOtherKextsDir(FALSE) return NULL\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-13 12:56:57 +01:00
|
|
|
// Add kext from 10
|
2019-12-23 12:36:26 +01:00
|
|
|
{
|
2020-08-09 17:55:30 +02:00
|
|
|
XStringW OSAllVersionKextsDir;
|
|
|
|
XStringW OSShortVersionKextsDir;
|
|
|
|
XStringW OSVersionKextsDirName;
|
|
|
|
XStringW DirName;
|
|
|
|
XStringW DirPath;
|
|
|
|
OSAllVersionKextsDir = SWPrintf("%ls\\kexts\\10", OEMPath.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(OSAllVersionKextsDir, sizeof(OSAllVersionKextsDir), "%s\\kexts\\10", OEMPath);
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(OSAllVersionKextsDir, L"10"_XSW, archCpuType);
|
2019-12-11 13:52:26 +01:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
if (OSTYPE_IS_OSX_INSTALLER(LoaderType)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DirName = SWPrintf("10_install");
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "10_install");
|
2020-05-01 18:26:28 +02:00
|
|
|
} else if (OSTYPE_IS_OSX_RECOVERY(LoaderType)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DirName = SWPrintf("10_recovery");
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "10_recovery");
|
2019-12-23 12:36:26 +01:00
|
|
|
} else {
|
2020-08-09 17:55:30 +02:00
|
|
|
DirName = SWPrintf("10_normal");
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "10_normal");
|
2019-12-23 12:36:26 +01:00
|
|
|
}
|
2020-08-09 17:55:30 +02:00
|
|
|
DirPath = SWPrintf("%ls\\kexts\\%ls", OEMPath.wc_str(), DirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirPath, sizeof(DirPath), "%s\\kexts\\%s", OEMPath, DirName);
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(DirPath, DirName, archCpuType);
|
2019-12-11 13:52:26 +01:00
|
|
|
|
2019-12-09 11:22:15 +01:00
|
|
|
|
2019-12-23 12:36:26 +01:00
|
|
|
// Add kext from 10.{version}
|
2019-12-19 17:51:21 +01:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
OSShortVersionKextsDir = SWPrintf("%ls\\kexts\\%ls", OEMPath.wc_str(), UniShortOSVersion.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(OSShortVersionKextsDir, sizeof(OSShortVersionKextsDir), "%s\\kexts\\%s", OEMPath, UniShortOSVersion);
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts( OSShortVersionKextsDir, UniShortOSVersion, archCpuType);
|
2019-12-11 13:52:26 +01:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
if (OSTYPE_IS_OSX_INSTALLER(LoaderType)) {
|
2020-08-12 17:15:47 +02:00
|
|
|
DirName = SWPrintf("%ls_install", UniShortOSVersion.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "%s_install", UniShortOSVersion);
|
2020-05-01 18:26:28 +02:00
|
|
|
} else if (OSTYPE_IS_OSX_RECOVERY(LoaderType)) {
|
2020-08-12 17:15:47 +02:00
|
|
|
DirName = SWPrintf("%ls_recovery", UniShortOSVersion.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "%s_recovery", UniShortOSVersion);
|
2019-12-23 12:36:26 +01:00
|
|
|
} else {
|
2020-08-12 17:15:47 +02:00
|
|
|
DirName = SWPrintf("%ls_normal", UniShortOSVersion.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "%s_normal", UniShortOSVersion);
|
2019-12-23 12:36:26 +01:00
|
|
|
}
|
2020-08-09 17:55:30 +02:00
|
|
|
DirPath = SWPrintf("%ls\\kexts\\%ls", OEMPath.wc_str(), DirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirPath, sizeof(DirPath), "%s\\kexts\\%s", OEMPath, DirName);
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(DirPath, DirName, archCpuType);
|
2019-12-09 11:22:15 +01:00
|
|
|
|
2019-12-19 17:51:21 +01:00
|
|
|
|
2019-12-23 12:36:26 +01:00
|
|
|
// Add kext from :
|
|
|
|
// 10.{version}.0 if NO minor version
|
|
|
|
// 10.{version}.{minor version} if minor version is > 0
|
2019-12-19 17:51:21 +01:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
if ( UniShortOSVersion == OSVersion ) {
|
|
|
|
OSVersionKextsDirName = SWPrintf("%s.0", OSVersion.c_str());
|
2020-05-01 18:26:28 +02:00
|
|
|
// snwprintf(OSVersionKextsDirName, sizeof(OSVersionKextsDirName), "%a.0", OSVersion);
|
2019-12-23 12:36:26 +01:00
|
|
|
} else {
|
2020-08-12 17:15:47 +02:00
|
|
|
OSVersionKextsDirName = SWPrintf("%s", OSVersion.c_str());
|
2020-05-01 18:26:28 +02:00
|
|
|
// snwprintf(OSVersionKextsDirName, sizeof(OSVersionKextsDirName), "%a", OSVersion);
|
2019-12-23 12:36:26 +01:00
|
|
|
}
|
2019-12-11 13:52:26 +01:00
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
DirPath = SWPrintf("%ls\\kexts\\%ls", OEMPath.wc_str(), OSVersionKextsDirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirPath, sizeof(DirPath), "%s\\kexts\\%s", OEMPath, OSVersionKextsDirName);
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(DirPath, OSVersionKextsDirName, archCpuType);
|
2019-12-11 13:52:26 +01:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
if ( OSTYPE_IS_OSX_INSTALLER(LoaderType)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DirName = SWPrintf("%ls_install", OSVersionKextsDirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "%s_install", OSVersionKextsDirName);
|
2020-05-01 18:26:28 +02:00
|
|
|
} else if (OSTYPE_IS_OSX_RECOVERY(LoaderType)) {
|
2020-08-09 17:55:30 +02:00
|
|
|
DirName = SWPrintf("%ls_recovery", OSVersionKextsDirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "%s_recovery", OSVersionKextsDirName);
|
2019-12-23 12:36:26 +01:00
|
|
|
} else {
|
2020-08-09 17:55:30 +02:00
|
|
|
DirName = SWPrintf("%ls_normal", OSVersionKextsDirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirName, sizeof(DirName), "%s_normal", OSVersionKextsDirName);
|
2019-12-23 12:36:26 +01:00
|
|
|
}
|
2020-08-09 17:55:30 +02:00
|
|
|
DirPath = SWPrintf("%ls\\kexts\\%ls", OEMPath.wc_str(), DirName.wc_str());
|
2020-04-04 15:50:13 +02:00
|
|
|
// snwprintf(DirPath, sizeof(DirPath), "%s\\kexts\\%s", OEMPath, DirName);
|
2020-08-12 17:15:47 +02:00
|
|
|
AddKexts(DirPath, DirName, archCpuType);
|
2019-12-23 12:36:26 +01:00
|
|
|
}
|
2019-12-09 11:22:15 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// reserve space in the device tree
|
|
|
|
if (GetKextCount() > 0) {
|
|
|
|
mm_extra_size = GetKextCount() * (sizeof(DeviceTreeNodeProperty) + sizeof(_DeviceTreeBuffer));
|
2020-08-15 15:47:56 +02:00
|
|
|
mm_extra = (__typeof__(mm_extra))AllocateZeroPool(mm_extra_size - sizeof(DeviceTreeNodeProperty));
|
2019-09-03 11:58:42 +02:00
|
|
|
/*Status = */LogDataHub(&gEfiMiscSubClassGuid, L"mm_extra", mm_extra, (UINT32)(mm_extra_size - sizeof(DeviceTreeNodeProperty)));
|
|
|
|
extra_size = GetKextsSize();
|
2020-08-15 15:47:56 +02:00
|
|
|
extra = (__typeof__(extra))AllocateZeroPool(extra_size - sizeof(DeviceTreeNodeProperty) + EFI_PAGE_SIZE);
|
2019-09-03 11:58:42 +02:00
|
|
|
/*Status = */LogDataHub(&gEfiMiscSubClassGuid, L"extra", extra, (UINT32)(extra_size - sizeof(DeviceTreeNodeProperty) + EFI_PAGE_SIZE));
|
|
|
|
// MsgLog("count: %d \n", GetKextCount());
|
|
|
|
// MsgLog("mm_extra_size: %d \n", mm_extra_size);
|
|
|
|
// MsgLog("extra_size: %d \n", extra_size);
|
|
|
|
// MsgLog("offset: %d \n", extra_size - sizeof(DeviceTreeNodeProperty) + EFI_PAGE_SIZE);
|
|
|
|
//no more needed
|
|
|
|
FreePool(mm_extra);
|
|
|
|
FreePool(extra);
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:55:30 +02:00
|
|
|
// SIDELOAD_KEXT *CurrentKext = NULL;
|
|
|
|
// SIDELOAD_KEXT *CurrentPlugInKext = NULL;
|
|
|
|
// SIDELOAD_KEXT *Next = NULL;
|
|
|
|
// //No more InjectKextList needed. Will free the list
|
|
|
|
// while (InjectKextList) {
|
|
|
|
// CurrentKext = InjectKextList->Next;
|
|
|
|
// CurrentPlugInKext = InjectKextList->PlugInList;
|
|
|
|
// while (CurrentPlugInKext) {
|
|
|
|
// Next = CurrentPlugInKext->Next;
|
|
|
|
// FreePool(CurrentPlugInKext->FileName);
|
|
|
|
// FreePool(CurrentPlugInKext->KextDirNameUnderOEMPath);
|
|
|
|
// FreePool(CurrentPlugInKext->Version);
|
|
|
|
// FreePool(CurrentPlugInKext);
|
|
|
|
// CurrentPlugInKext = Next;
|
|
|
|
// }
|
|
|
|
// FreePool(InjectKextList->FileName);
|
|
|
|
// FreePool(InjectKextList->KextDirNameUnderOEMPath);
|
|
|
|
// FreePool(InjectKextList->Version);
|
|
|
|
// FreePool(InjectKextList);
|
|
|
|
// InjectKextList = CurrentKext;
|
|
|
|
// }
|
|
|
|
delete InjectKextList;
|
|
|
|
InjectKextList = NULL;
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-31 18:46:52 +01:00
|
|
|
|
2019-12-27 13:54:58 +01:00
|
|
|
/*
|
2020-07-20 10:52:36 +02:00
|
|
|
* Adler32 from Chameleon, not used
|
2019-12-27 13:54:58 +01:00
|
|
|
*/
|
|
|
|
#define BASE 65521L /* largest prime smaller than 65536 */
|
|
|
|
#define NMAX 5000
|
|
|
|
// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
|
|
|
|
|
|
|
|
#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
|
|
|
|
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
|
|
|
|
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
|
|
|
|
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
|
|
|
|
#define DO16(buf) DO8(buf,0); DO8(buf,8);
|
2020-07-20 10:52:36 +02:00
|
|
|
#if 0
|
2020-03-11 15:23:58 +01:00
|
|
|
static UINT32 Adler32(unsigned char *buf, long len)
|
2019-12-27 13:54:58 +01:00
|
|
|
{
|
|
|
|
unsigned long s1 = 1; // adler & 0xffff;
|
|
|
|
unsigned long s2 = 0; // (adler >> 16) & 0xffff;
|
|
|
|
unsigned long result;
|
2020-03-11 15:23:58 +01:00
|
|
|
long k;
|
2019-12-27 13:54:58 +01:00
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
k = len < NMAX ? len : NMAX;
|
|
|
|
len -= k;
|
|
|
|
while (k >= 16) {
|
|
|
|
DO16(buf);
|
|
|
|
buf += 16;
|
|
|
|
k -= 16;
|
|
|
|
}
|
|
|
|
if (k != 0) do {
|
|
|
|
s1 += *buf++;
|
|
|
|
s2 += s1;
|
|
|
|
} while (--k);
|
|
|
|
s1 %= BASE;
|
|
|
|
s2 %= BASE;
|
|
|
|
}
|
|
|
|
result = (s2 << 16) | s1;
|
|
|
|
// result is in big endian
|
2020-03-11 15:23:58 +01:00
|
|
|
return (UINT32)result;
|
2019-12-27 13:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-27 17:01:27 +01:00
|
|
|
typedef struct {
|
|
|
|
UINT32 Magic;
|
|
|
|
UINT32 Signature;
|
|
|
|
UINT32 Length;
|
|
|
|
UINT32 Adler32;
|
|
|
|
UINT32 Version;
|
|
|
|
UINT32 NumKexts;
|
|
|
|
UINT32 CpuType;
|
|
|
|
UINT32 CpuSubtype;
|
|
|
|
} MKextHeader;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
UINT32 PlistOffset;
|
|
|
|
UINT32 PlistCompressedSize;
|
|
|
|
UINT32 PlistFullSize;
|
|
|
|
UINT32 PlistModifiedSeconds;
|
|
|
|
UINT32 BinaryOffset;
|
|
|
|
UINT32 BinaryCompressedSize;
|
|
|
|
UINT32 BinaryFullSize;
|
|
|
|
UINT32 BinaryModifiedSeconds;
|
|
|
|
} MKextFile;
|
|
|
|
|
|
|
|
#define MKEXT_MAGIC 0x54584b4d
|
|
|
|
#define MKEXT_SIGNATURE 0x58534f4d
|
|
|
|
#define MKEXT_VERSION_1 0x00800001
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
int LOADER_ENTRY::is_mkext_v1(UINT8* drvPtr)
|
2019-12-31 18:44:47 +01:00
|
|
|
{
|
2019-12-27 13:54:58 +01:00
|
|
|
_DeviceTreeBuffer *dtb = (_DeviceTreeBuffer*) (((UINT8*)drvPtr) + sizeof(DeviceTreeNodeProperty));
|
2019-12-27 17:01:27 +01:00
|
|
|
MKextHeader* mkext_ptr = (MKextHeader*)(UINTN)(dtb->paddr);
|
2019-12-27 13:54:58 +01:00
|
|
|
|
2019-12-27 17:01:27 +01:00
|
|
|
if (mkext_ptr->Magic == MKEXT_MAGIC
|
|
|
|
&& mkext_ptr->Signature == MKEXT_SIGNATURE
|
|
|
|
&& mkext_ptr->Version == MKEXT_VERSION_1) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("MKext_v1 found at paddr=0x%08x, length=0x%08x\n", dtb->paddr, dtb->length);
|
2019-12-27 13:54:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
void LOADER_ENTRY::patch_mkext_v1(UINT8 *drvPtr)
|
2019-12-31 18:44:47 +01:00
|
|
|
{
|
2019-12-27 13:54:58 +01:00
|
|
|
_DeviceTreeBuffer *dtb = (_DeviceTreeBuffer*) (((UINT8*)drvPtr) + sizeof(DeviceTreeNodeProperty));
|
2019-12-27 17:01:27 +01:00
|
|
|
MKextHeader* mkext_ptr = (MKextHeader*)(UINTN)dtb->paddr;
|
2019-12-27 13:54:58 +01:00
|
|
|
|
2019-12-27 17:01:27 +01:00
|
|
|
UINT32 mkext_len = SwapBytes32(mkext_ptr->Length);
|
|
|
|
UINT32 mkext_numKexts = SwapBytes32(mkext_ptr->NumKexts);
|
2019-12-27 13:54:58 +01:00
|
|
|
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
KEXT_ENTRY *KextEntry;
|
|
|
|
if(!IsListEmpty(&gKextList)) {
|
|
|
|
for (Link = gKextList.ForwardLink; Link != &gKextList; Link = Link->ForwardLink) {
|
|
|
|
KextEntry = CR(Link, KEXT_ENTRY, Link, KEXT_SIGNATURE);
|
2019-12-27 17:01:27 +01:00
|
|
|
MKextFile *mkext_insert = (MKextFile*)((UINT8*)mkext_ptr + sizeof(MKextHeader) + mkext_numKexts * sizeof(MKextFile));
|
2019-12-27 13:54:58 +01:00
|
|
|
|
|
|
|
// free some space
|
2019-12-27 17:01:27 +01:00
|
|
|
CopyMem((UINT8*)mkext_insert + sizeof(MKextFile),
|
|
|
|
(UINT8*)mkext_insert,
|
|
|
|
mkext_len - (sizeof(MKextHeader) + mkext_numKexts * sizeof(MKextFile)));
|
|
|
|
mkext_len += sizeof(MKextFile);
|
2019-12-27 13:54:58 +01:00
|
|
|
|
|
|
|
// update the offsets to reflect 0x20 bytes moved above
|
2019-12-27 17:01:27 +01:00
|
|
|
for (UINT32 i = 0; i < mkext_numKexts; i++) {
|
|
|
|
MKextFile *kext_base = (MKextFile*)((UINT8*)mkext_ptr + sizeof(MKextHeader) + i * sizeof(MKextFile));
|
|
|
|
UINT32 plist_offset = SwapBytes32(kext_base->PlistOffset) + sizeof(MKextFile);
|
|
|
|
UINT32 binary_offset = SwapBytes32(kext_base->BinaryOffset) + sizeof(MKextFile);
|
|
|
|
kext_base->PlistOffset = SwapBytes32(plist_offset);
|
|
|
|
kext_base->BinaryOffset = SwapBytes32(binary_offset);
|
2019-12-27 13:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy kext data (plist+binary)
|
2019-12-27 17:01:27 +01:00
|
|
|
CopyMem((UINT8*)mkext_ptr + mkext_len,
|
|
|
|
(UINT8*)(KextEntry->kext.paddr + sizeof(_BooterKextFileInfo)),
|
2019-12-27 13:54:58 +01:00
|
|
|
(UINT32)((_BooterKextFileInfo*)(UINTN)(KextEntry->kext.paddr))->infoDictLength
|
|
|
|
+ (UINT32)((_BooterKextFileInfo*)(UINTN)(KextEntry->kext.paddr))->executableLength);
|
|
|
|
|
|
|
|
// insert kext offsets
|
2019-12-27 17:01:27 +01:00
|
|
|
mkext_insert->PlistOffset = SwapBytes32(mkext_len);
|
2019-12-27 13:54:58 +01:00
|
|
|
mkext_len += ((_BooterKextFileInfo*)(UINTN)(KextEntry->kext.paddr))->infoDictLength;
|
2019-12-27 17:01:27 +01:00
|
|
|
mkext_insert->PlistCompressedSize = 0;
|
|
|
|
mkext_insert->PlistFullSize = SwapBytes32((UINT32)((_BooterKextFileInfo*)(UINTN)(KextEntry->kext.paddr))->infoDictLength);
|
|
|
|
mkext_insert->PlistModifiedSeconds = 0;
|
|
|
|
mkext_insert->BinaryOffset = SwapBytes32(mkext_len);
|
2019-12-27 13:54:58 +01:00
|
|
|
mkext_len += ((_BooterKextFileInfo*)(UINTN)(KextEntry->kext.paddr))->executableLength;
|
2019-12-27 17:01:27 +01:00
|
|
|
mkext_insert->BinaryCompressedSize = 0;
|
|
|
|
mkext_insert->BinaryFullSize = SwapBytes32((UINT32)((_BooterKextFileInfo*)(UINTN)(KextEntry->kext.paddr))->executableLength);
|
|
|
|
mkext_insert->BinaryModifiedSeconds = 0;
|
2019-12-27 13:54:58 +01:00
|
|
|
mkext_numKexts ++;
|
|
|
|
|
|
|
|
// update the header
|
2019-12-27 17:01:27 +01:00
|
|
|
mkext_ptr->Length = SwapBytes32(mkext_len);
|
|
|
|
mkext_ptr->NumKexts = SwapBytes32(mkext_numKexts);
|
2019-12-27 13:54:58 +01:00
|
|
|
|
2019-12-27 17:01:27 +01:00
|
|
|
// update the checksum
|
|
|
|
mkext_ptr->Adler32 = SwapBytes32(Adler32((UINT8*)mkext_ptr + 0x10, mkext_len - 0x10));
|
2019-12-27 13:54:58 +01:00
|
|
|
|
2019-12-27 17:01:27 +01:00
|
|
|
// update the memory-map reference
|
2019-12-27 13:54:58 +01:00
|
|
|
dtb->length = mkext_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 10:52:36 +02:00
|
|
|
#endif
|
2019-12-27 13:54:58 +01:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
////////////////////
|
|
|
|
// OnExitBootServices
|
|
|
|
////////////////////
|
2020-05-01 18:26:28 +02:00
|
|
|
EFI_STATUS LOADER_ENTRY::InjectKexts(IN UINT32 deviceTreeP, IN UINT32* deviceTreeLength)
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2019-12-18 19:41:07 +01:00
|
|
|
UINT8 *dtEntry = (UINT8*)(UINTN) deviceTreeP;
|
|
|
|
UINTN dtLen = (UINTN) *deviceTreeLength;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2019-12-18 19:41:07 +01:00
|
|
|
DTEntry platformEntry;
|
|
|
|
DTEntry memmapEntry;
|
2019-09-03 11:58:42 +02:00
|
|
|
CHAR8 *ptr;
|
2019-12-18 19:41:07 +01:00
|
|
|
OpaqueDTPropertyIterator OPropIter;
|
2019-09-03 11:58:42 +02:00
|
|
|
DTPropertyIterator iter = &OPropIter;
|
|
|
|
DeviceTreeNodeProperty *prop = NULL;
|
|
|
|
|
2019-12-18 19:41:07 +01:00
|
|
|
UINT8 *infoPtr = 0;
|
|
|
|
UINT8 *extraPtr = 0;
|
|
|
|
UINT8 *drvPtr = 0;
|
|
|
|
UINTN offset = 0;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
KEXT_ENTRY *KextEntry;
|
2019-12-18 19:41:07 +01:00
|
|
|
UINTN KextBase = 0;
|
|
|
|
_DeviceTreeBuffer *mm;
|
|
|
|
_BooterKextFileInfo *drvinfo;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
UINT32 KextCount;
|
2019-12-18 19:41:07 +01:00
|
|
|
UINTN Index;
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("\nInjectKexts: ");
|
2020-07-20 10:52:36 +02:00
|
|
|
DBG("\nInjectKexts: ");
|
2019-09-03 11:58:42 +02:00
|
|
|
KextCount = GetKextCount();
|
|
|
|
if (KextCount == 0) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("no kexts to inject.\nPausing 5 secs ...\n");
|
2020-08-11 14:43:53 +02:00
|
|
|
if (KernelAndKextPatches.KPDebug) {
|
2019-09-03 11:58:42 +02:00
|
|
|
gBS->Stall(5000000);
|
|
|
|
}
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("%d kexts ...\n", KextCount);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// kextsBase = Desc->PhysicalStart + (((UINTN) Desc->NumberOfPages) * EFI_PAGE_SIZE);
|
|
|
|
// kextsPages = EFI_SIZE_TO_PAGES(kext.length);
|
|
|
|
// Status = gBS->AllocatePages(AllocateAddress, EfiLoaderData, kextsPages, &kextsBase);
|
|
|
|
// if (EFI_ERROR(Status)) { MsgLog("Kext inject: could not allocate memory\n"); return Status; }
|
|
|
|
// Desc->NumberOfPages += kextsPages;
|
|
|
|
// CopyMem((VOID*)kextsBase, (VOID*)(UINTN)kext.paddr, kext.length);
|
|
|
|
// drvinfo = (_BooterKextFileInfo*) kextsBase;
|
|
|
|
// drvinfo->infoDictPhysAddr += (UINT32)kextsBase;
|
|
|
|
// drvinfo->executablePhysAddr += (UINT32)kextsBase;
|
|
|
|
// drvinfo->bundlePathPhysAddr += (UINT32)kextsBase;
|
|
|
|
|
2019-12-18 18:34:26 +01:00
|
|
|
DTInit(dtEntry, deviceTreeLength);
|
2019-12-18 19:41:07 +01:00
|
|
|
if(!EFI_ERROR(DTLookupEntry(NULL,"/chosen/memory-map", &memmapEntry))) {
|
|
|
|
if(!EFI_ERROR(DTCreatePropertyIterator(memmapEntry, iter))) {
|
|
|
|
while(!EFI_ERROR(DTIterateProperties(iter, &ptr))) {
|
|
|
|
prop = iter->CurrentProperty;
|
2019-09-03 11:58:42 +02:00
|
|
|
drvPtr = (UINT8*) prop;
|
2020-05-16 21:30:29 +02:00
|
|
|
if(strncmp(prop->Name, "Driver-", 7)==0 || strncmp(prop->Name, "DriversPackage-", 15)==0) {
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 19:41:07 +01:00
|
|
|
if(!EFI_ERROR(DTLookupEntry(NULL, "/efi/platform", &platformEntry))) {
|
|
|
|
if(!EFI_ERROR(DTCreatePropertyIterator(platformEntry, iter))) {
|
|
|
|
while(!EFI_ERROR(DTIterateProperties(iter, &ptr))) {
|
|
|
|
prop = iter->CurrentProperty;
|
2020-05-16 21:30:29 +02:00
|
|
|
if(strncmp(prop->Name, "mm_extra", 8)==0) {
|
2019-12-18 19:41:07 +01:00
|
|
|
infoPtr = (UINT8*)prop;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-16 21:30:29 +02:00
|
|
|
if(strncmp(prop->Name, "extra", 5)==0) {
|
2019-12-18 19:41:07 +01:00
|
|
|
extraPtr = (UINT8*)prop;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drvPtr == 0 || infoPtr == 0 || extraPtr == 0 || drvPtr > infoPtr || drvPtr > extraPtr || infoPtr > extraPtr) {
|
2020-03-28 07:36:07 +01:00
|
|
|
printf("\nInvalid device tree for kext injection\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
gBS->Stall(5000000);
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make space for memory map entries
|
2019-12-18 19:41:07 +01:00
|
|
|
platformEntry->NumProperties -= 2;
|
|
|
|
offset = sizeof(DeviceTreeNodeProperty) + ((DeviceTreeNodeProperty*) infoPtr)->Length;
|
2019-09-03 11:58:42 +02:00
|
|
|
CopyMem(drvPtr+offset, drvPtr, infoPtr-drvPtr);
|
|
|
|
|
|
|
|
// make space behind device tree
|
|
|
|
// platformEntry->nProperties--;
|
2019-12-18 19:41:07 +01:00
|
|
|
offset = sizeof(DeviceTreeNodeProperty)+((DeviceTreeNodeProperty*) extraPtr)->Length;
|
2019-12-18 18:34:26 +01:00
|
|
|
CopyMem(extraPtr, extraPtr+offset, dtLen-(UINTN)(extraPtr-dtEntry)-offset);
|
2019-09-03 11:58:42 +02:00
|
|
|
*deviceTreeLength -= (UINT32)offset;
|
|
|
|
|
|
|
|
KextBase = RoundPage(dtEntry + *deviceTreeLength);
|
|
|
|
if(!IsListEmpty(&gKextList)) {
|
|
|
|
Index = 1;
|
|
|
|
for (Link = gKextList.ForwardLink; Link != &gKextList; Link = Link->ForwardLink) {
|
|
|
|
KextEntry = CR(Link, KEXT_ENTRY, Link, KEXT_SIGNATURE);
|
|
|
|
|
|
|
|
CopyMem((VOID*) KextBase, (VOID*)(UINTN) KextEntry->kext.paddr, KextEntry->kext.length);
|
|
|
|
drvinfo = (_BooterKextFileInfo*) KextBase;
|
|
|
|
drvinfo->infoDictPhysAddr += (UINT32) KextBase;
|
|
|
|
drvinfo->executablePhysAddr += (UINT32) KextBase;
|
|
|
|
drvinfo->bundlePathPhysAddr += (UINT32) KextBase;
|
|
|
|
|
2019-12-18 19:41:07 +01:00
|
|
|
memmapEntry->NumProperties++;
|
2019-09-03 11:58:42 +02:00
|
|
|
prop = ((DeviceTreeNodeProperty*) drvPtr);
|
2019-12-18 19:41:07 +01:00
|
|
|
prop->Length = sizeof(_DeviceTreeBuffer);
|
2019-09-03 11:58:42 +02:00
|
|
|
mm = (_DeviceTreeBuffer*) (((UINT8*)prop) + sizeof(DeviceTreeNodeProperty));
|
|
|
|
mm->paddr = (UINT32)KextBase;
|
|
|
|
mm->length = KextEntry->kext.length;
|
2020-04-10 15:52:49 +02:00
|
|
|
snprintf(prop->Name, 31, "Driver-%X", (UINT32)KextBase);
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
drvPtr += sizeof(DeviceTreeNodeProperty) + sizeof(_DeviceTreeBuffer);
|
|
|
|
KextBase = RoundPage(KextBase + KextEntry->kext.length);
|
2020-07-20 10:52:36 +02:00
|
|
|
DBG_RT(" %llu - %s\n", Index, (CHAR8 *)(UINTN)drvinfo->bundlePathPhysAddr);
|
|
|
|
DBG(" %llu - %s\n", Index, (CHAR8 *)(UINTN)drvinfo->bundlePathPhysAddr);
|
2019-09-03 11:58:42 +02:00
|
|
|
if (gSettings.KextPatchesAllowed) {
|
|
|
|
CHAR8 SavedValue;
|
|
|
|
CHAR8 *InfoPlist = (CHAR8*)(UINTN)drvinfo->infoDictPhysAddr;
|
|
|
|
SavedValue = InfoPlist[drvinfo->infoDictLength];
|
|
|
|
InfoPlist[drvinfo->infoDictLength] = '\0';
|
2020-07-20 10:52:36 +02:00
|
|
|
// KernelAndKextPatcherInit();
|
2020-08-12 17:15:47 +02:00
|
|
|
for (size_t i = 0; i < KernelAndKextPatches.KextPatches.size(); i++) {
|
|
|
|
if ((KernelAndKextPatches.KextPatches[i].Data.size() > 0) &&
|
|
|
|
(AsciiStrStr(InfoPlist, KernelAndKextPatches.KextPatches[i].Name.c_str()) != NULL)) {
|
2019-09-03 11:58:42 +02:00
|
|
|
AnyKextPatch(
|
|
|
|
(UINT8*)(UINTN)drvinfo->executablePhysAddr,
|
|
|
|
drvinfo->executableLength,
|
|
|
|
InfoPlist,
|
|
|
|
drvinfo->infoDictLength,
|
2020-05-01 18:26:28 +02:00
|
|
|
i
|
2019-09-03 11:58:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InfoPlist[drvinfo->infoDictLength] = SavedValue;
|
|
|
|
}
|
|
|
|
Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("Done.\n");
|
|
|
|
Stall(5000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
//
|
|
|
|
// KernelBooterExtensionsPatch to load extra kexts besides kernelcache
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
// Snow Leopard i386
|
2020-05-02 18:00:31 +02:00
|
|
|
const UINT8 KBESnowSearchEXT_i386[] = { 0xE8, 0xED, 0xF9, 0xFF, 0xFF, 0xEB, 0x08, 0x89, 0x1C, 0x24 };
|
|
|
|
const UINT8 KBESnowReplaceEXT_i386[] = { 0xE8, 0xED, 0xF9, 0xFF, 0xFF, 0x90, 0x90, 0x89, 0x1C, 0x24 };
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Snow Leopard X64
|
2020-05-02 18:00:31 +02:00
|
|
|
const UINT8 KBESnowSearchEXT_X64[] = { 0xE8, 0x5A, 0xFB, 0xFF, 0xFF, 0xEB, 0x08, 0x48, 0x89, 0xDF };
|
|
|
|
const UINT8 KBESnowReplaceEXT_X64[] = { 0xE8, 0x5A, 0xFB, 0xFF, 0xFF, 0x90, 0x90, 0x48, 0x89, 0xDF };
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Lion i386
|
2020-05-02 18:00:31 +02:00
|
|
|
const UINT8 KBELionSearchEXT_i386[] = { 0xE8, 0xAA, 0xFB, 0xFF, 0xFF, 0xEB, 0x08, 0x89, 0x34, 0x24 };
|
|
|
|
const UINT8 KBELionReplaceEXT_i386[] = { 0xE8, 0xAA, 0xFB, 0xFF, 0xFF, 0x90, 0x90, 0x89, 0x34, 0x24 };
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
// Lion X64
|
2020-05-02 18:00:31 +02:00
|
|
|
const UINT8 KBELionSearchEXT_X64[] = { 0xE8, 0x0C, 0xFD, 0xFF, 0xFF, 0xEB, 0x08, 0x48, 0x89, 0xDF };
|
|
|
|
const UINT8 KBELionReplaceEXT_X64[] = { 0xE8, 0x0C, 0xFD, 0xFF, 0xFF, 0x90, 0x90, 0x48, 0x89, 0xDF };
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// We can not rely on OSVersion global variable for OS version detection,
|
|
|
|
// since in some cases it is not correct (install of ML from Lion, for example).
|
|
|
|
// So, we'll use "brute-force" method - just try to patch.
|
|
|
|
// Actually, we'll at least check that if we can find only one instance of code that
|
|
|
|
// we are planning to patch.
|
|
|
|
//
|
|
|
|
// Fully reworked by Sherlocks. 2019.06.23
|
|
|
|
//
|
|
|
|
|
2020-04-29 22:09:59 +02:00
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
VOID EFIAPI LOADER_ENTRY::KernelBooterExtensionsPatch()
|
2019-09-03 11:58:42 +02:00
|
|
|
{
|
2020-05-15 05:23:33 +02:00
|
|
|
// UINTN Num = 0;
|
2019-09-03 11:58:42 +02:00
|
|
|
UINTN NumSnow_i386_EXT = 0;
|
|
|
|
UINTN NumSnow_X64_EXT = 0;
|
|
|
|
UINTN NumLion_i386_EXT = 0;
|
|
|
|
UINTN NumLion_X64_EXT = 0;
|
2020-05-11 17:44:00 +02:00
|
|
|
UINTN patchLocation2 = 0, patchLocation3 = 0;
|
2020-05-02 12:12:53 +02:00
|
|
|
|
2019-09-03 11:58:42 +02:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("\nPatching kernel for injected kexts...\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
|
|
|
|
if (is64BitKernel) {
|
2020-05-09 09:56:30 +02:00
|
|
|
NumSnow_X64_EXT = SearchAndCount(KernelData, KERNEL_MAX_SIZE, KBESnowSearchEXT_X64, sizeof(KBESnowSearchEXT_X64));
|
|
|
|
NumLion_X64_EXT = SearchAndCount(KernelData, KERNEL_MAX_SIZE, KBELionSearchEXT_X64, sizeof(KBELionSearchEXT_X64));
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-09 09:56:30 +02:00
|
|
|
NumSnow_i386_EXT = SearchAndCount(KernelData, KERNEL_MAX_SIZE, KBESnowSearchEXT_i386, sizeof(KBESnowSearchEXT_i386));
|
|
|
|
NumLion_i386_EXT = SearchAndCount(KernelData, KERNEL_MAX_SIZE, KBELionSearchEXT_i386, sizeof(KBELionSearchEXT_i386));
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NumSnow_i386_EXT + NumSnow_X64_EXT + NumLion_i386_EXT + NumLion_X64_EXT > 1) {
|
|
|
|
// more then one pattern found - we do not know what to do with it
|
|
|
|
// and we'll skipp it
|
2020-05-02 12:12:53 +02:00
|
|
|
// DBG_RT("\nERROR patching kernel for injected kexts:\nmultiple patterns found (Snowi386: %llu, SnowX64: %llu, Lioni386: %llu, LionX64: %llu) - skipping patching!\n", NumSnow_i386_EXT, NumSnow_X64_EXT, NumLion_i386_EXT, NumLion_X64_EXT);
|
2020-05-15 05:23:33 +02:00
|
|
|
// Stall(10000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// X64
|
|
|
|
if (is64BitKernel) {
|
|
|
|
if (NumSnow_X64_EXT == 1) {
|
2020-05-15 05:23:33 +02:00
|
|
|
/*Num=*/ SearchAndReplace(KernelData, KERNEL_MAX_SIZE, KBESnowSearchEXT_X64, sizeof(KBESnowSearchEXT_X64), KBESnowReplaceEXT_X64, 1);
|
2020-05-02 12:12:53 +02:00
|
|
|
// DBG_RT("==> kernel Snow Leopard X64: %llu replaces done.\n", Num);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (NumLion_X64_EXT == 1) {
|
2020-05-15 05:23:33 +02:00
|
|
|
/*Num=*/ SearchAndReplace(KernelData, KERNEL_MAX_SIZE, KBELionSearchEXT_X64, sizeof(KBELionSearchEXT_X64), KBELionReplaceEXT_X64, 1);
|
2020-05-02 12:12:53 +02:00
|
|
|
// DBG_RT("==> kernel Lion X64: %llu replaces done.\n", Num);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
|
|
|
// EXT - load extra kexts besides kernelcache.
|
2020-04-29 10:06:44 +02:00
|
|
|
#if OLD_EXTRA_KEXT_PATCH
|
2020-04-29 22:09:59 +02:00
|
|
|
UINT32 patchLocation1 = 0;
|
2020-05-02 12:12:53 +02:00
|
|
|
for (UINT32 i = 0; i < 0x1000000; i++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 01 00 31 FF BE 14 00 05
|
|
|
|
if (Kernel[i+0] == 0x01 && Kernel[i+1] == 0x00 && Kernel[i+2] == 0x31 &&
|
|
|
|
Kernel[i+3] == 0xFF && Kernel[i+4] == 0xBE && Kernel[i+5] == 0x14 &&
|
|
|
|
Kernel[i+6] == 0x00 && Kernel[i+7] == 0x05) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> found EXT Base (10.8 - recent macOS)\n");
|
2020-05-02 12:12:53 +02:00
|
|
|
for (UINT32 y = i; y < 0x1000000; y++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// E8 XX 00 00 00 EB XX XX
|
|
|
|
if (Kernel[y+0] == 0xE8 && Kernel[y+2] == 0x00 && Kernel[y+3] == 0x00 &&
|
|
|
|
Kernel[y+4] == 0x00 && Kernel[y+5] == 0xEB) {
|
|
|
|
//(Kernel[y+7] == 0x48 || Kernel[y+7] == 0xE8)) { // 48:10.8-10.9/E8:10.10+
|
|
|
|
patchLocation1 = y;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> found EXT (10.8 - recent macOS) at 0x%08x\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 EXT (10.8 - recent macOS), kernel patch aborted.\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
gBS->Stall(3000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patchLocation1) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> patched EXT (10.8 - recent macOS) location=%x\n", patchLocation1);
|
2019-09-03 11:58:42 +02:00
|
|
|
for (i = 5; i < 7; i++) {
|
|
|
|
// E8 XX 00 00 00 EB XX XX
|
|
|
|
// E8 XX 00 00 00 90 90 XX
|
|
|
|
Kernel[patchLocation1 + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 10:06:44 +02:00
|
|
|
#else
|
2020-05-02 05:38:38 +02:00
|
|
|
//Capitan
|
|
|
|
// procedure at 950950, len = fffffffffffff3f0
|
|
|
|
// proclen=256, end=256 startLen=0
|
|
|
|
// found start at 0x950950
|
|
|
|
// found pattern: 1
|
|
|
|
// address: 0095098b
|
|
|
|
// bytes:eb05
|
2020-07-17 05:28:17 +02:00
|
|
|
|
|
|
|
// BS
|
|
|
|
// E8 ?? 00 00 00 EB 05 E8 -->
|
|
|
|
// E8 ?? 00 00 00 90 90 E8.
|
2020-05-02 05:38:38 +02:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN procLocation = searchProc("readStartupExtensions"_XS8);
|
2020-05-02 05:38:38 +02:00
|
|
|
const UINT8 findJmp[] = {0xEB, 0x05};
|
|
|
|
const UINT8 patchJmp[] = {0x90, 0x90};
|
2020-07-17 05:28:17 +02:00
|
|
|
DBG("==> readStartupExtensions at %llx\n", procLocation);
|
2020-05-09 09:56:30 +02:00
|
|
|
if (!SearchAndReplace(&KernelData[procLocation], 0x100, findJmp, 2, patchJmp, 1)) {
|
2020-07-17 05:28:17 +02:00
|
|
|
DBG("load kexts not patched\n");
|
|
|
|
for (UINTN j=procLocation+0x2b; j<procLocation+0x4b; ++j) {
|
|
|
|
DBG("%02x ", KernelData[j]);
|
|
|
|
}
|
|
|
|
DBG("\n");
|
2020-05-02 15:30:33 +02:00
|
|
|
// Stall(10000000);
|
2020-06-24 06:20:45 +02:00
|
|
|
//second attempt brute force for 10.16
|
2020-07-20 10:52:36 +02:00
|
|
|
// const UINT8 findJmp2[] = {0xEB, 0x05, 0xE8, 0x7D, 0x03};
|
|
|
|
// const UINT8 patchJmp2[] = {0x90, 0x90, 0xE8, 0x7D, 0x03};
|
|
|
|
// if (!SearchAndReplace(&KernelData[0], KERNEL_MAX_SIZE, findJmp2, 5, patchJmp2, 1)) {
|
|
|
|
// DBG("load kexts 2 not patched\n");
|
|
|
|
// } else {
|
|
|
|
// DBG("load kexts 2 patched !!!\n");
|
|
|
|
// }
|
2020-05-01 18:26:28 +02:00
|
|
|
} else {
|
2020-07-20 10:52:36 +02:00
|
|
|
DBG("load kexts patched \n");
|
2020-05-02 12:12:53 +02:00
|
|
|
// for (UINTN j=procLocation+0x3b; j<procLocation+0x5b; ++j) {
|
|
|
|
// DBG_RT("%02x", Kernel[j]);
|
|
|
|
// }
|
2020-05-02 15:30:33 +02:00
|
|
|
// DBG_RT("\n");
|
2020-04-29 17:07:10 +02:00
|
|
|
}
|
2020-05-02 15:30:33 +02:00
|
|
|
Stall(12000000);
|
2020-04-29 10:06:44 +02:00
|
|
|
#endif
|
2019-09-03 11:58:42 +02:00
|
|
|
// SIP - bypass kext check by System Integrity Protection.
|
2020-05-01 18:26:28 +02:00
|
|
|
//the pattern found in __ZN6OSKext14loadExecutableEv: // OSKext::loadExecutable()
|
|
|
|
// iMac2017:Catalina sergey$ ./FindMask kernel -p loadExecutable -e 1000 -f 488500740048000048,FFFF00FF00FF0000FF
|
|
|
|
// descending
|
|
|
|
// procedure at 7a1ed0, len = ffffffffffff4b50
|
|
|
|
// proclen=4096, end=4096 startLen=0
|
|
|
|
// found start at 0x7a1ed0
|
|
|
|
// found pattern: 1
|
|
|
|
// address: 007a29b7
|
|
|
|
// bytes:4885c074224889c348
|
2020-07-17 05:28:17 +02:00
|
|
|
|
2020-05-01 18:26:28 +02:00
|
|
|
|
2020-05-02 05:38:38 +02:00
|
|
|
//Capitan
|
|
|
|
// ffffff800084897b 4885DB test rbx, rbx
|
|
|
|
// ffffff800084897e 7470 je 0xffffff80008489f0 -> patch to not jump
|
|
|
|
// 7412 jmp ffffff8000848992
|
|
|
|
// ; Basic Block Input Regs: rbx - Killed Regs: rax rdi
|
|
|
|
// ffffff8000848980 488B03 mov rax, qword [ds:rbx]
|
|
|
|
// ffffff8000848983 4889DF mov rdi, rbx
|
|
|
|
// ffffff8000848986 FF5028 call qword [ds:rax+0x28]
|
|
|
|
// ffffff8000848989 483B1DE04F2B00 cmp rbx, qword [ds:0xffffff8000afd970]
|
|
|
|
// ffffff8000848990 745E je 0xffffff80008489f0 -> patch to not jump
|
|
|
|
// ; Basic Block Input Regs: r13 - Killed Regs: rax rbx rdi
|
|
|
|
// ffffff8000848992 498B4500 mov rax, qword [ds:r13+0x0]
|
|
|
|
// procedure at 6487f0, len = 7250
|
|
|
|
// proclen=512, end=512 startLen=0
|
|
|
|
// found start at 0x6487f0
|
|
|
|
// found pattern: 1
|
|
|
|
// address: 0064897b
|
|
|
|
// bytes:4885db7470
|
|
|
|
|
|
|
|
#if OLD_EXTRA_KEXT_PATCH
|
2020-05-02 12:12:53 +02:00
|
|
|
for (UINT32 i = 0; i < 0x1000000; i++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 45 31 FF 41 XX 01 00 00 DC 48
|
|
|
|
if (Kernel[i+0] == 0x45 && Kernel[i+1] == 0x31 && Kernel[i+3] == 0x41 &&
|
|
|
|
//(Kernel[i+4] == 0xBF || Kernel[i+4] == 0xBE) && // BF:10.11/BE:10.12+
|
|
|
|
Kernel[i+5] == 0x01 && Kernel[i+6] == 0x00 && Kernel[i+7] == 0x00 &&
|
|
|
|
Kernel[i+8] == 0xDC && Kernel[i+9] == 0x48) {
|
2020-05-02 05:38:38 +02:00
|
|
|
|
|
|
|
DBG_RT("==> found loadExecutable (10.11 - recent macOS) at %x\n", i);
|
2020-05-02 12:12:53 +02:00
|
|
|
for (UINT32 y = i; y < 0x100000; y++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 48 85 XX 74 XX 48 XX XX 48
|
|
|
|
if (Kernel[y+0] == 0x48 && Kernel[y+1] == 0x85 && Kernel[y+3] == 0x74 &&
|
|
|
|
Kernel[y+5] == 0x48 && Kernel[y+8] == 0x48) {
|
|
|
|
patchLocation2 = y;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> found SIP (10.11 - 10.14) at 0x%08x\n", patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
2020-05-01 18:26:28 +02:00
|
|
|
// 00 85 C0 0F 84 XX 00 00 00 49 //???? - not found in Catalina
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (Kernel[y+0] == 0x00 && Kernel[y+1] == 0x85 && Kernel[y+2] == 0xC0 &&
|
|
|
|
Kernel[y+3] == 0x0F && Kernel[y+4] == 0x84 && Kernel[y+9] == 0x49) {
|
|
|
|
patchLocation2 = y;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> found SIP (10.15 - recent macOS) at 0x%08x\n", patchLocation2);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 05:38:38 +02:00
|
|
|
#else
|
2020-07-17 05:28:17 +02:00
|
|
|
// BS
|
|
|
|
// E8 ?? ?? ?? 00 85 C0 0F 84 ?? 00 00 00 49 8B 45 -->
|
|
|
|
// E8 ?? ?? ?? 00 85 C0 90 90 90 90 90 90 49 8B 45.
|
|
|
|
|
2020-05-02 12:12:53 +02:00
|
|
|
const UINT8 find3[] = {0x48, 0x85, 00, 0x74, 00, 0x48, 00, 00, 0x48 };
|
|
|
|
const UINT8 mask3[] = {0xFF, 0xFF, 00, 0xFF, 00, 0xFF, 00, 00, 0xFF };
|
2020-05-02 05:38:38 +02:00
|
|
|
#endif
|
2020-05-02 12:12:53 +02:00
|
|
|
|
2020-05-03 06:10:02 +02:00
|
|
|
//ffffff80009a2267 488D35970D2400 lea rsi, qword [ds:0xffffff8000be3005] ; "com.apple.private.security.kext-management"
|
|
|
|
//ffffff80009a226e E89D780D00 call _IOTaskHasEntitlement
|
|
|
|
//ffffff80009a2273 85C0 test eax, eax =>change to eb06 -> jmp .+6
|
|
|
|
//ffffff80009a2275 0F843C010000 je 0xffffff80009a23b7
|
|
|
|
//ffffff80009a227b
|
2020-08-12 17:15:47 +02:00
|
|
|
UINTN taskLocation = searchProc("IOTaskHasEntitlement"_XS8);
|
|
|
|
procLocation = searchProc("loadExecutable"_XS8);
|
2020-07-06 06:04:01 +02:00
|
|
|
patchLocation2 = FindMemMask(&KernelData[procLocation], 0x500, find3, sizeof(find3), mask3, sizeof(mask3));
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("IOTaskHasEntitlement at 0x%llx, loadExecutable at 0x%llx\n", taskLocation, procLocation);
|
|
|
|
DBG("find3 at 0x%llx\n", patchLocation2);
|
2020-05-03 06:10:02 +02:00
|
|
|
if (patchLocation2 != KERNEL_MAX_SIZE) {
|
|
|
|
DBG_RT("=> patch SIP applied\n");
|
|
|
|
patchLocation2 += procLocation;
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation2 + 3] = 0xEB;
|
|
|
|
if (KernelData[patchLocation2 + 4] == 0x6C) {
|
|
|
|
KernelData[patchLocation2 + 4] = 0x15;
|
2020-05-03 06:10:02 +02:00
|
|
|
} else {
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation2 + 4] = 0x12;
|
2020-05-03 06:10:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-17 05:28:17 +02:00
|
|
|
patchLocation2 = FindRelative32(KernelData, procLocation, 0x700, taskLocation);
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("else search relative at 0x%llx\n", patchLocation2);
|
2020-05-03 06:10:02 +02:00
|
|
|
if (patchLocation2 != 0) {
|
|
|
|
DBG_RT("=> patch2 SIP applied\n");
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[patchLocation2] = 0xEB;
|
|
|
|
KernelData[patchLocation2 + 1] = 0x06;
|
2020-06-24 06:20:45 +02:00
|
|
|
} else {
|
|
|
|
DBG_RT("=> patch2 SIP not applied\n");
|
2020-07-17 05:28:17 +02:00
|
|
|
const UINT8 find7[] = {0xE8, 0x00, 0x00, 0x00, 0x00, 0x85, 0xC0, 0x0F, 0x84, 0xFF, 0x00, 0x00, 0x00, 0x49, 0x8B, 0x45 };
|
|
|
|
const UINT8 mask7[] = {0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
patchLocation2 = FindMemMask(&KernelData[0], KERNEL_MAX_SIZE, find7, sizeof(find7), mask7, sizeof(mask7));
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("found call to TE at 0x%llx\n", patchLocation2);
|
2020-07-17 05:28:17 +02:00
|
|
|
KernelData[0 + patchLocation2 + 7] = 0xEB;
|
|
|
|
KernelData[0 + patchLocation2 + 8] = 0x04;
|
|
|
|
|
2020-05-02 12:12:53 +02:00
|
|
|
}
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
2020-05-03 06:10:02 +02:00
|
|
|
Stall(10000000);
|
2020-05-02 12:12:53 +02:00
|
|
|
|
|
|
|
/*
|
2020-05-02 05:38:38 +02:00
|
|
|
//Capitan: 48 85 db 74 70 48 8b 03 48
|
2019-09-03 11:58:42 +02:00
|
|
|
if (patchLocation2) {
|
2020-05-02 12:12:53 +02:00
|
|
|
if (taskFound) {
|
|
|
|
UINT8 jmp = Kernel[patchLocation2 + 4];
|
|
|
|
const UINT8 repl4[] = {0xB8, 0x01, 0x00, 0x00, 0x00, 0xEB};
|
|
|
|
CopyMem(&Kernel[patchLocation2], repl4, sizeof(repl4));
|
|
|
|
Kernel[patchLocation2 + 6] = jmp;
|
|
|
|
DBG_RT("=> mojave SIP applyed\n");
|
|
|
|
} else
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Kernel[patchLocation2 + 0] == 0x48 && Kernel[patchLocation2 + 1] == 0x85) {
|
2020-05-02 05:38:38 +02:00
|
|
|
|
|
|
|
Kernel[patchLocation2 + 3] = 0xEB;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> patched SIP (10.11 - 10.14)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Kernel[patchLocation2 + 4] == 0x6C) {
|
|
|
|
// 48 85 XX 74 6C 48 XX XX 48
|
|
|
|
// 48 85 XX EB 15 48 XX XX 48
|
|
|
|
Kernel[patchLocation2 + 4] = 0x15; // 10.14.4-10.14.6
|
|
|
|
} else {
|
|
|
|
// 48 85 XX 74 XX 48 XX XX 48
|
|
|
|
// 48 85 XX EB 12 48 XX XX 48
|
|
|
|
Kernel[patchLocation2 + 4] = 0x12; // 10.11-10.14.3
|
|
|
|
}
|
|
|
|
// PMheart
|
|
|
|
} else if (Kernel[patchLocation2 + 0] == 0x00 && Kernel[patchLocation2 + 1] == 0x85) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> patched SIP (10.15 - recent macOS)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
for (i = 3; i < 9; i++) {
|
|
|
|
// 00 85 C0 0F 84 XX 00 00 00 49
|
|
|
|
// 00 85 C0 90 90 90 90 90 90 49
|
|
|
|
Kernel[patchLocation2 + i] = 0x90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 18:26:28 +02:00
|
|
|
Stall(9000000);
|
2020-05-02 12:12:53 +02:00
|
|
|
*/
|
|
|
|
//Slice - hope this patch useful for some system that I have no.
|
2019-09-03 11:58:42 +02:00
|
|
|
// KxldUnmap by vit9696
|
|
|
|
// Avoid race condition in OSKext::removeKextBootstrap when using booter kexts without keepsyms=1.
|
2020-08-12 17:15:47 +02:00
|
|
|
procLocation = searchProc("removeKextBootstrap"_XS8);
|
2020-05-02 12:12:53 +02:00
|
|
|
const UINT8 find5[] = {0x00, 0x0F, 0x85, 00, 00, 0x00, 0x00, 0x48 };
|
|
|
|
const UINT8 mask5[] = {0xFF, 0xFF, 0xFF, 00, 00, 0xFF, 0xFF, 0xFF };
|
2020-07-20 10:52:36 +02:00
|
|
|
patchLocation3 = FindMemMask(&KernelData[procLocation], 0x300, find5, sizeof(find5), mask5, sizeof(mask5));
|
2020-07-07 08:19:59 +02:00
|
|
|
DBG("removeKextBootstrap at 0x%llx\n", patchLocation3);
|
2020-05-02 12:12:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
for (UINT32 i = 0; i < 0x1000000; i++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 55 48 89 E5 41 57 41 56 41 54 53 //10
|
|
|
|
// 48 83 EC 30 48 C7 45 B8 XX XX XX //21
|
|
|
|
// XX XX XX XX XX XX XX XX XX XX XX //32
|
|
|
|
// XX XX XX XX XX XX XX XX XX XX XX //43
|
2020-03-23 14:31:04 +01:00
|
|
|
// XX XX XX XX XX XX XX XX XX FF XX //54
|
|
|
|
// XX XX XX XX XX XX XX XX XX FF FF //65
|
2019-09-03 11:58:42 +02:00
|
|
|
if (Kernel[i+0] == 0x55 && Kernel[i+1] == 0x48 && Kernel[i+2] == 0x89 &&
|
|
|
|
Kernel[i+3] == 0xE5 && Kernel[i+4] == 0x41 && Kernel[i+5] == 0x57 &&
|
|
|
|
Kernel[i+6] == 0x41 && Kernel[i+7] == 0x56 && Kernel[i+8] == 0x41 &&
|
|
|
|
Kernel[i+9] == 0x54 && Kernel[i+10] == 0x53 && Kernel[i+11] == 0x48 &&
|
|
|
|
Kernel[i+12] == 0x83 && Kernel[i+13] == 0xEC && Kernel[i+14] == 0x30 &&
|
|
|
|
Kernel[i+15] == 0x48 && Kernel[i+16] == 0xC7 && Kernel[i+17] == 0x45 &&
|
2020-03-23 14:31:04 +01:00
|
|
|
Kernel[i+18] == 0xB8 && Kernel[i+53] == 0xFF && Kernel[i+64] == 0xFF && Kernel[i+65] == 0xFF) {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> found KxldUnmap Base (10.14 - recent macOS)\n");
|
2020-05-02 12:12:53 +02:00
|
|
|
for (UINT32 y = i; y < 0x1000000; y++) {
|
2019-09-03 11:58:42 +02:00
|
|
|
// 00 0F 85 XX XX 00 00 48
|
|
|
|
if (Kernel[y+0] == 0x00 && Kernel[y+1] == 0x0F && Kernel[y+2] == 0x85 &&
|
|
|
|
Kernel[y+5] == 0x00 && Kernel[y+6] == 0x00 && Kernel[y+7] == 0x48) {
|
|
|
|
patchLocation3 = y;
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> found KxldUnmap (10.14 - recent macOS) at 0x%08x\n", patchLocation3);
|
2019-09-03 11:58:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 12:12:53 +02:00
|
|
|
*/
|
2020-07-17 05:28:17 +02:00
|
|
|
//BS
|
|
|
|
//FF 80 3D ?? ?? ?? 00 00 0F 85 ?? 01 00 00 41 -->
|
|
|
|
//FF 80 3D ?? ?? ?? 00 00 90 E9 ?? 01 00 00 41.
|
2020-07-20 10:52:36 +02:00
|
|
|
/*
|
2020-05-02 12:12:53 +02:00
|
|
|
if (patchLocation3 == KERNEL_MAX_SIZE) {
|
2020-07-17 05:28:17 +02:00
|
|
|
DBG_RT("==> can't find KxldUnmap (10.14 - 10.15)\n");
|
2020-05-01 18:26:28 +02:00
|
|
|
Stall(3000000);
|
2020-07-17 05:28:17 +02:00
|
|
|
const UINT8 find6[] = {0xFF, 0x80, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x85, 0x00, 0x01, 0x00, 0x00, 0x41 };
|
|
|
|
const UINT8 mask6[] = {0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
patchLocation3 = FindMemMask(&KernelData[0], KERNEL_MAX_SIZE, find6, sizeof(find6), mask6, sizeof(mask6));
|
2020-07-18 20:06:10 +02:00
|
|
|
DBG("find mask 6 at 0x%llx\n", patchLocation3);
|
2020-07-17 05:28:17 +02:00
|
|
|
if (patchLocation3 != KERNEL_MAX_SIZE) {
|
|
|
|
KernelData[0 + patchLocation3 + 8] = 0x90;
|
|
|
|
KernelData[0 + patchLocation3 + 9] = 0xE9;
|
|
|
|
}
|
2020-05-02 12:12:53 +02:00
|
|
|
} else {
|
2020-07-20 10:52:36 +02:00
|
|
|
*/
|
|
|
|
//The patch is not needed for bigsur
|
|
|
|
if (patchLocation3 != KERNEL_MAX_SIZE) {
|
|
|
|
DBG("==> patched KxldUnmap (10.14 - 10.15)\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
// 00 0F 85 XX XX 00 00 48
|
|
|
|
// 00 90 E9 XX XX 00 00 48
|
2020-05-09 09:56:30 +02:00
|
|
|
KernelData[procLocation + patchLocation3 + 1] = 0x90;
|
|
|
|
KernelData[procLocation + patchLocation3 + 2] = 0xE9;
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// i386
|
|
|
|
if (NumSnow_i386_EXT == 1) {
|
2020-05-15 05:23:33 +02:00
|
|
|
/*Num=*/ SearchAndReplace(KernelData, KERNEL_MAX_SIZE, KBESnowSearchEXT_i386, sizeof(KBESnowSearchEXT_i386), KBESnowReplaceEXT_i386, 1);
|
2020-05-02 12:12:53 +02:00
|
|
|
// DBG_RT("==> kernel Snow Leopard i386: %llu replaces done.\n", Num);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else if (NumLion_i386_EXT == 1) {
|
2020-05-15 05:23:33 +02:00
|
|
|
/*Num=*/ SearchAndReplace(KernelData, KERNEL_MAX_SIZE, KBELionSearchEXT_i386, sizeof(KBELionSearchEXT_i386), KBELionReplaceEXT_i386, 1);
|
2020-05-02 12:12:53 +02:00
|
|
|
// DBG_RT("==> kernel Lion i386: %llu replaces done.\n", Num);
|
2019-09-03 11:58:42 +02:00
|
|
|
} else {
|
2020-05-01 18:26:28 +02:00
|
|
|
DBG_RT("==> ERROR: NOT patched - unknown kernel.\n");
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
DBG_RT("Pausing 5 secs ...\n");
|
|
|
|
Stall(5000000);
|
2019-09-03 11:58:42 +02:00
|
|
|
}
|