OpenCoreKernel: Replaced MatchKernel with MinKernel and MaxKernel

This commit is contained in:
vit9696 2019-09-25 13:50:21 +03:00
parent cd481d011b
commit fec17e6339
4 changed files with 230 additions and 67 deletions

View File

@ -4,6 +4,7 @@ OpenCore Changelog
#### v0.5.1
- Added support of kernel resource kext injection
- Added support for 0.25% clock slowdown on Xeon Scalable CPUs (thx @mrmiller)
- Replaced `MatchKernel` with `MinKernel` and `MaxKernel`
#### v0.5.0
- Added builtin firmware versions for new models 2019

View File

@ -249,7 +249,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/Lilu</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -263,7 +265,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/VirtualSMC</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -277,7 +281,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/WhateverGreen</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -291,7 +297,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/AppleALC</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -305,7 +313,9 @@
<false/>
<key>ExecutablePath</key>
<string>Contents/MacOS/IntelMausi</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -319,7 +329,9 @@
<false/>
<key>ExecutablePath</key>
<string></string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -341,7 +353,9 @@
<false/>
<key>Identifier</key>
<string>com.apple.driver.AppleTyMCEDriver</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
</dict>
</array>
@ -364,7 +378,9 @@
<integer>0</integer>
<key>Mask</key>
<data></data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>Replace</key>
<data>6y4Ptg==</data>
@ -390,7 +406,9 @@
<integer>0</integer>
<key>Mask</key>
<data></data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string>18.5.0</string>
<key>MinKernel</key>
<string>18.5.0</string>
<key>Replace</key>
<data>6zMPtw==</data>
@ -416,7 +434,9 @@
<integer>0</integer>
<key>Mask</key>
<data></data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string>18.5.0</string>
<key>MinKernel</key>
<string>18.5.0</string>
<key>Replace</key>
<data>ww==</data>
@ -442,7 +462,9 @@
<integer>0</integer>
<key>Mask</key>
<data>/wAA////////////</data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>Replace</key>
<data>uLQWAABmiYMABQAA</data>

View File

@ -249,7 +249,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/Lilu</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -263,7 +265,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/VirtualSMC</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -277,7 +281,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/WhateverGreen</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -291,7 +297,9 @@
<true/>
<key>ExecutablePath</key>
<string>Contents/MacOS/AppleALC</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -305,7 +313,9 @@
<false/>
<key>ExecutablePath</key>
<string>Contents/MacOS/IntelMausi</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -319,7 +329,9 @@
<false/>
<key>ExecutablePath</key>
<string></string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>PlistPath</key>
<string>Contents/Info.plist</string>
@ -341,7 +353,9 @@
<false/>
<key>Identifier</key>
<string>com.apple.driver.AppleTyMCEDriver</string>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
</dict>
</array>
@ -364,7 +378,9 @@
<integer>0</integer>
<key>Mask</key>
<data></data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>Replace</key>
<data>6y4Ptg==</data>
@ -390,7 +406,9 @@
<integer>0</integer>
<key>Mask</key>
<data></data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string>18.5.0</string>
<key>MinKernel</key>
<string>18.5.0</string>
<key>Replace</key>
<data>6zMPtw==</data>
@ -416,7 +434,9 @@
<integer>0</integer>
<key>Mask</key>
<data></data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string>18.5.0</string>
<key>MinKernel</key>
<string>18.5.0</string>
<key>Replace</key>
<data>ww==</data>
@ -442,7 +462,9 @@
<integer>0</integer>
<key>Mask</key>
<data>/wAA////////////</data>
<key>MatchKernel</key>
<key>MaxKernel</key>
<string></string>
<key>MinKernel</key>
<string></string>
<key>Replace</key>
<data>uLQWAABmiYMABQAA</data>

View File

@ -29,18 +29,95 @@ STATIC OC_GLOBAL_CONFIG *mOcConfiguration;
STATIC OC_CPU_INFO *mOcCpuInfo;
STATIC
VOID
UINT32
OcParseDarwinVersion (
IN CONST CHAR8 *String
)
{
UINT32 Version;
UINT32 Index;
UINT32 Index2;
if (*String == '\0' || *String < '0' || *String > '9') {
return 0;
}
Version = 0;
for (Index = 0; Index < 3; ++Index) {
for (Index2 = 0; Index2 < 2; ++Index2) {
Version *= 10;
if (*String >= '0' && *String <= '9') {
Version += *String++ - '0';
} else if (*String != '.' && *String != '\0') {
return 0;
}
}
if (*String == '.') {
++String;
}
}
return Version;
}
STATIC
BOOLEAN
OcMatchDarwinVersion (
IN UINT32 CurrentVersion,
IN UINT32 MinVersion,
IN UINT32 MaxVersion
)
{
//
// Check against min <= curr <= max.
// curr=0 -> curr=inf, max=0 -> max=inf
//
//
// Replace max inf with max known version.
//
if (MaxVersion == 0) {
MaxVersion = CurrentVersion;
}
//
// Handle curr=inf <= max=inf(?) case.
//
if (CurrentVersion == 0) {
return MaxVersion == 0;
}
//
// Handle curr=num > max=num case.
//
if (CurrentVersion > MaxVersion) {
return FALSE;
}
//
// Handle min=num > curr=num case.
//
if (CurrentVersion < MinVersion) {
return FALSE;
}
return TRUE;
}
STATIC
UINT32
OcKernelReadDarwinVersion (
IN CONST UINT8 *Kernel,
IN UINT32 KernelSize,
OUT CHAR8 *DarwinVersion,
OUT UINT32 DarwinVersionSize
IN UINT32 KernelSize
)
{
INT32 Offset;
UINT32 Index;
CHAR8 DarwinVersion[32];
CHAR8 DarwinVersionInteger;
ASSERT (DarwinVersion > 0);
Offset = FindPattern (
(CONST UINT8 *) "Darwin Kernel Version ",
@ -53,21 +130,28 @@ OcKernelReadDarwinVersion (
if (Offset < 0) {
DEBUG ((DEBUG_WARN, "OC: Failed to determine kernel version\n"));
DarwinVersion[0] = '\0';
return;
return 0;
}
Offset += L_STR_LEN ("Darwin Kernel Version ");
for (Index = 0; Index < DarwinVersionSize - 1; ++Index, ++Offset) {
for (Index = 0; Index < ARRAY_SIZE (DarwinVersion) - 1; ++Index, ++Offset) {
if ((UINT32) Offset >= KernelSize || Kernel[Offset] == ':') {
break;
}
DarwinVersion[Index] = (CHAR8) Kernel[Offset];
}
DarwinVersion[Index] = '\0';
DarwinVersionInteger = OcParseDarwinVersion (DarwinVersion);
DEBUG ((DEBUG_INFO, "OC: Read kernel version %a\n", DarwinVersion));
DEBUG ((
DEBUG_INFO,
"OC: Read kernel version %a (%u)\n",
DarwinVersion,
DarwinVersionInteger
));
return DarwinVersionInteger;
}
STATIC
@ -80,6 +164,7 @@ OcKernelLoadKextsAndReserve (
UINT32 Index;
UINT32 ReserveSize;
CHAR8 *BundlePath;
CHAR8 *Comment;
CHAR8 *PlistPath;
CHAR8 *ExecutablePath;
CHAR16 FullPath[128];
@ -96,6 +181,7 @@ OcKernelLoadKextsAndReserve (
if (Kext->PlistDataSize == 0) {
BundlePath = OC_BLOB_GET (&Kext->BundlePath);
Comment = OC_BLOB_GET (&Kext->Comment);
PlistPath = OC_BLOB_GET (&Kext->PlistPath);
if (BundlePath[0] == '\0' || PlistPath[0] == '\0') {
DEBUG ((DEBUG_ERROR, "OC: Your config has improper for kext info\n"));
@ -120,7 +206,13 @@ OcKernelLoadKextsAndReserve (
);
if (Kext->PlistData == NULL) {
DEBUG ((DEBUG_ERROR, "OC: Plist %s is missing for kext %a\n", FullPath, BundlePath));
DEBUG ((
DEBUG_ERROR,
"OC: Plist %s is missing for kext %a (%a)\n",
FullPath,
BundlePath,
Comment
));
Kext->Enabled = FALSE;
continue;
}
@ -144,7 +236,13 @@ OcKernelLoadKextsAndReserve (
);
if (Kext->ImageData == NULL) {
DEBUG ((DEBUG_ERROR, "OC: Image %s is missing for kext %a\n", FullPath, BundlePath));
DEBUG ((
DEBUG_ERROR,
"OC: Image %s is missing for kext %a (%a)\n",
FullPath,
BundlePath,
Comment
));
Kext->Enabled = FALSE;
continue;
}
@ -168,7 +266,7 @@ STATIC
VOID
OcKernelApplyPatches (
IN OC_GLOBAL_CONFIG *Config,
IN CONST CHAR8 *DarwinVersion,
IN UINT32 DarwinVersion,
IN PRELINKED_CONTEXT *Context,
IN OUT UINT8 *Kernel,
IN UINT32 Size
@ -180,7 +278,9 @@ OcKernelApplyPatches (
PATCHER_GENERIC_PATCH Patch;
OC_KERNEL_PATCH_ENTRY *UserPatch;
CONST CHAR8 *Target;
CONST CHAR8 *MatchKernel;
CONST CHAR8 *Comment;
UINT32 MaxKernel;
UINT32 MinKernel;
BOOLEAN IsKernelPatch;
IsKernelPatch = Context == NULL;
@ -203,22 +303,25 @@ OcKernelApplyPatches (
for (Index = 0; Index < Config->Kernel.Patch.Count; ++Index) {
UserPatch = Config->Kernel.Patch.Values[Index];
Target = OC_BLOB_GET (&UserPatch->Identifier);
Comment = OC_BLOB_GET (&UserPatch->Comment);
if (!UserPatch->Enabled
|| (AsciiStrCmp (Target, "kernel") == 0) != IsKernelPatch) {
if (!UserPatch->Enabled || (AsciiStrCmp (Target, "kernel") == 0) != IsKernelPatch) {
continue;
}
MatchKernel = OC_BLOB_GET (&UserPatch->MatchKernel);
MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&UserPatch->MaxKernel));
MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&UserPatch->MaxKernel));
if (AsciiStrnCmp (DarwinVersion, MatchKernel, AsciiStrLen (MatchKernel)) != 0) {
if (!OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
DEBUG ((
DEBUG_INFO,
"OC: Kernel patcher skips %a patch at %u due to version %a vs %a",
"OC: Kernel patcher skips %a (%a) patch at %u due to version %u <= %u <= %u",
Target,
Comment,
Index,
MatchKernel,
DarwinVersion
MinKernel,
DarwinVersion,
MaxKernel
));
continue;
}
@ -231,10 +334,10 @@ OcKernelApplyPatches (
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OC: Kernel patcher %a init failure - %r\n", Target, Status));
DEBUG ((DEBUG_WARN, "OC: Kernel patcher %a (%a) init failure - %r\n", Target, Comment, Status));
continue;
} else {
DEBUG ((DEBUG_INFO, "OC: Kernel patcher %a init succeed\n", Target));
DEBUG ((DEBUG_INFO, "OC: Kernel patcher %a (%a) init succeed\n", Target, Comment));
}
}
@ -249,7 +352,7 @@ OcKernelApplyPatches (
|| (OC_BLOB_GET (&UserPatch->Base)[0] == '\0' && UserPatch->Find.Size != UserPatch->Replace.Size)
|| (UserPatch->Mask.Size > 0 && UserPatch->Find.Size != UserPatch->Mask.Size)
|| (UserPatch->ReplaceMask.Size > 0 && UserPatch->Find.Size != UserPatch->ReplaceMask.Size)) {
DEBUG ((DEBUG_ERROR, "OC: Kernel patch %u for %a is borked\n", Index, Target));
DEBUG ((DEBUG_ERROR, "OC: Kernel patch %u for %a (%a) is borked\n", Index, Target, Comment));
continue;
}
@ -285,9 +388,10 @@ OcKernelApplyPatches (
Status = PatcherApplyGenericPatch (&Patcher, &Patch);
DEBUG ((
EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_INFO,
"OC: Kernel patcher result %u for %a - %r\n",
"OC: Kernel patcher result %u for %a (%a) - %r\n",
Index,
Target,
Comment,
Status
));
}
@ -351,7 +455,7 @@ STATIC
VOID
OcKernelBlockKexts (
IN OC_GLOBAL_CONFIG *Config,
IN CONST CHAR8 *DarwinVersion,
IN UINT32 DarwinVersion,
IN PRELINKED_CONTEXT *Context
)
{
@ -360,26 +464,32 @@ OcKernelBlockKexts (
UINT32 Index;
OC_KERNEL_BLOCK_ENTRY *Kext;
CONST CHAR8 *Target;
CONST CHAR8 *MatchKernel;
CONST CHAR8 *Comment;
UINT32 MaxKernel;
UINT32 MinKernel;
for (Index = 0; Index < Config->Kernel.Block.Count; ++Index) {
Kext = Config->Kernel.Block.Values[Index];
Target = OC_BLOB_GET (&Kext->Identifier);
Kext = Config->Kernel.Block.Values[Index];
Target = OC_BLOB_GET (&Kext->Identifier);
Comment = OC_BLOB_GET (&Kext->Comment);
if (!Kext->Enabled) {
continue;
}
MatchKernel = OC_BLOB_GET (&Kext->MatchKernel);
MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&Kext->MaxKernel));
MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&Kext->MaxKernel));
if (AsciiStrnCmp (DarwinVersion, MatchKernel, AsciiStrLen (MatchKernel)) != 0) {
if (!OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
DEBUG ((
DEBUG_INFO,
"OC: Prelink blocker skips %a block at %u due to version %a vs %a",
"OC: Prelink blocker skips %a (%a) block at %u due to version %u <= %u <= %u",
Target,
Comment,
Index,
MatchKernel,
DarwinVersion
MinKernel,
DarwinVersion,
MaxKernel
));
continue;
}
@ -391,7 +501,7 @@ OcKernelBlockKexts (
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OC: Prelink blocker %a init failure - %r\n", Target, Status));
DEBUG ((DEBUG_WARN, "OC: Prelink blocker %a (%a) init failure - %r\n", Target, Comment, Status));
continue;
}
@ -399,8 +509,9 @@ OcKernelBlockKexts (
DEBUG ((
EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_INFO,
"OC: Prelink blocker %a - %r\n",
"OC: Prelink blocker %a (%a) - %r\n",
Target,
Comment,
Status
));
}
@ -410,7 +521,7 @@ STATIC
EFI_STATUS
OcKernelProcessPrelinked (
IN OC_GLOBAL_CONFIG *Config,
IN CONST CHAR8 *DarwinVersion,
IN UINT32 DarwinVersion,
IN OUT UINT8 *Kernel,
IN UINT32 *KernelSize,
IN UINT32 AllocatedSize
@ -420,10 +531,12 @@ OcKernelProcessPrelinked (
PRELINKED_CONTEXT Context;
CHAR8 *BundlePath;
CHAR8 *ExecutablePath;
CHAR8 *Comment;
UINT32 Index;
CHAR8 FullPath[128];
OC_KERNEL_ADD_ENTRY *Kext;
CONST CHAR8 *MatchKernel;
UINT32 MaxKernel;
UINT32 MinKernel;
Status = PrelinkedContextInit (&Context, Kernel, *KernelSize, AllocatedSize);
@ -443,16 +556,20 @@ OcKernelProcessPrelinked (
}
BundlePath = OC_BLOB_GET (&Kext->BundlePath);
MatchKernel = OC_BLOB_GET (&Kext->MatchKernel);
Comment = OC_BLOB_GET (&Kext->Comment);
MaxKernel = OcParseDarwinVersion (OC_BLOB_GET (&Kext->MaxKernel));
MinKernel = OcParseDarwinVersion (OC_BLOB_GET (&Kext->MaxKernel));
if (AsciiStrnCmp (DarwinVersion, MatchKernel, AsciiStrLen (MatchKernel)) != 0) {
if (!OcMatchDarwinVersion (DarwinVersion, MinKernel, MaxKernel)) {
DEBUG ((
DEBUG_INFO,
"OC: Prelink injection skips %a kext at %u due to version %a vs %a",
"OC: Prelink injection skips %a (%a) kext at %u due to version %u <= %u <= %u",
BundlePath,
Comment,
Index,
MatchKernel,
DarwinVersion
MinKernel,
DarwinVersion,
MaxKernel
));
continue;
}
@ -476,8 +593,9 @@ OcKernelProcessPrelinked (
DEBUG ((
EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_INFO,
"OC: Prelink injection %a - %r\n",
"OC: Prelink injection %a (%a) - %r\n",
BundlePath,
Comment,
Status
));
}
@ -517,7 +635,7 @@ OcKernelFileOpen (
EFI_FILE_PROTOCOL *VirtualFileHandle;
EFI_STATUS PrelinkedStatus;
EFI_TIME ModificationTime;
CHAR8 DarwinVersion[16];
UINT32 DarwinVersion;
Status = This->Open (This, NewHandle, FileName, OpenMode, Attributes);
@ -556,7 +674,7 @@ OcKernelFileOpen (
// This is not Apple kernel, just return the original file.
//
if (!EFI_ERROR (Status)) {
OcKernelReadDarwinVersion (Kernel, KernelSize, DarwinVersion, sizeof (DarwinVersion));
DarwinVersion = OcKernelReadDarwinVersion (Kernel, KernelSize);
OcKernelApplyPatches (mOcConfiguration, DarwinVersion, NULL, Kernel, KernelSize);
PrelinkedStatus = OcKernelProcessPrelinked (