Skip to content

Commit

Permalink
OcAppleKernelLib: Disabled XcpmExtraMsrs MSR_MISC_PWR_MGMT patch on…
Browse files Browse the repository at this point in the history
… macOS 12+; fix IOAHCIBlockStorage patch on macOS 14.4+ thanks @vit9696 (acidanthera#545)
  • Loading branch information
PMheart authored Jun 15, 2024
1 parent 55bed88 commit 485db4c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ OpenCore Changelog
- Updated code and added progress bar to macrecovery, thx @soyeonswife63
- Bundled fat binary i386/x64 10.6+ compatible `nvramdump` with LogoutHook release
- Added support for manual build of i386/x64 10.6+ versions of userspace tools via `FATBIN32=1 make`
- Disabled `XcpmExtraMsrs MSR_MISC_PWR_MGMT` patch on macOS 12+ due to non-existence
- Fixed `ThirdPartyDrives` quirk on macOS 14.4 and above

#### v1.0.0
- Updated builtin firmware versions for SMBIOS and the rest
Expand Down
6 changes: 6 additions & 0 deletions Include/Acidanthera/Library/OcAppleKernelLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ typedef enum KERNEL_CACHE_TYPE_ {
#define KERNEL_VERSION_BIG_SUR 20
#define KERNEL_VERSION_MONTEREY 21
#define KERNEL_VERSION_VENTURA 22
#define KERNEL_VERSION_SONOMA 23
#define KERNEL_VERSION_SEQUOIA 24

//
// Minimum kernel versions for each release.
Expand All @@ -148,6 +150,8 @@ typedef enum KERNEL_CACHE_TYPE_ {
#define KERNEL_VERSION_BIG_SUR_MIN KERNEL_VERSION (KERNEL_VERSION_BIG_SUR, 0, 0)
#define KERNEL_VERSION_MONTEREY_MIN KERNEL_VERSION (KERNEL_VERSION_MONTEREY, 0, 0)
#define KERNEL_VERSION_VENTURA_MIN KERNEL_VERSION (KERNEL_VERSION_VENTURA, 0, 0)
#define KERNEL_VERSION_SONOMA_MIN KERNEL_VERSION (KERNEL_VERSION_SONOMA, 0, 0)
#define KERNEL_VERSION_SEQUOIA_MIN KERNEL_VERSION (KERNEL_VERSION_SEQUOIA, 0, 0)

//
// Maximum kernel versions for each release.
Expand All @@ -166,6 +170,8 @@ typedef enum KERNEL_CACHE_TYPE_ {
#define KERNEL_VERSION_CATALINA_MAX (KERNEL_VERSION_BIG_SUR_MIN - 1)
#define KERNEL_VERSION_BIG_SUR_MAX (KERNEL_VERSION_MONTEREY_MIN - 1)
#define KERNEL_VERSION_MONTEREY_MAX (KERNEL_VERSION_VENTURA_MIN - 1)
#define KERNEL_VERSION_VENTURA_MAX (KERNEL_VERSION_SONOMA_MIN - 1)
#define KERNEL_VERSION_SONOMA_MAX (KERNEL_VERSION_SEQUOIA_MIN - 1)

//
// Prelinked context used for kernel modification.
Expand Down
80 changes: 75 additions & 5 deletions Library/OcAppleKernelLib/CommonPatches.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,18 @@ PatchAppleXcpmExtraMsrs (

//
// Now patch writes to MSR_MISC_PWR_MGMT.
// On macOS Monterey (12) and above, this no longer exists.
//
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtRelPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: Failed to patch writes to MSR_MISC_PWR_MGMT - %r, trying dbg\n", Status));
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtDbgPatch);
if (OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_MONTEREY_MIN, 0)) {
DEBUG ((DEBUG_INFO, "OCAK: Skipping XcpmExtraMsrs MSR_MISC_PWR_MGMT patch on %u\n", KernelVersion));
} else {
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtRelPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: Failed to patch writes to MSR_MISC_PWR_MGMT - %r\n", Status));
DEBUG ((DEBUG_INFO, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT - %r, trying dbg\n", Status));
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtDbgPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT - %r\n", Status));
}
}
}

Expand Down Expand Up @@ -988,6 +993,57 @@ PATCHER_GENERIC_PATCH
.Limit = 4096
};

STATIC
CONST UINT8
mIOAHCIBlockStoragePatch144Find[] = {
0x4C, 0x8D, 0x2D, 0x00, 0x00, 0x00, 0x00, ///< lea r13, qword ("APPLE" and "APPLE SSD")
0x4C, 0x89, 0xEF, ///< mov rdi, r13
0xE8, 0x00, 0x00, 0x00, 0x00, ///< call strlen
0x4C, 0x89, 0xEF ///< mov rdi, r13
};

STATIC
CONST UINT8
mIOAHCIBlockStoragePatch144FindMask[] = {
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF
};

STATIC
CONST UINT8
mIOAHCIBlockStoragePatch144Replace[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x31, 0xC0, 0x90, 0x90, 0x90, ///< xor eax, eax ; nop
0x00, 0x00, 0x00
};

STATIC
CONST UINT8
mIOAHCIBlockStoragePatch144ReplaceMask[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00
};

STATIC
PATCHER_GENERIC_PATCH
mIOAHCIBlockStoragePatch144 = {
.Comment = DEBUG_POINTER ("IOAHCIBlockStorage trim 14.4+"),
.Base = "__ZN24IOAHCIBlockStorageDriver23DetermineDeviceFeaturesEPt",
.Find = mIOAHCIBlockStoragePatch144Find,
.Mask = mIOAHCIBlockStoragePatch144FindMask,
.Replace = mIOAHCIBlockStoragePatch144Replace,
.ReplaceMask = mIOAHCIBlockStoragePatch144ReplaceMask,
.Size = sizeof (mIOAHCIBlockStoragePatch144Find),
.Count = 2,
.Skip = 0,
.Limit = 4096
};

STATIC
EFI_STATUS
PatchThirdPartyDriveSupport (
Expand All @@ -1002,6 +1058,20 @@ PatchThirdPartyDriveSupport (
return EFI_NOT_FOUND;
}

//
// macOS 14.4+ (Darwin 23.4.0) adopted different patch patterns similar to 13.3+, as below.
//
if (OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_SONOMA, 4, 0), 0)) {
Status = PatcherApplyGenericPatch (Patcher, &mIOAHCIBlockStoragePatch144);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply patch 14.4+ com.apple.iokit.IOAHCIBlockStorage - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success 14.4+ com.apple.iokit.IOAHCIBlockStorage\n"));
}

return Status;
}

//
// Starting with macOS 13.3 (Darwin 22.4.0), a new set of patches are required, discovered by @vit9696.
//
Expand Down

0 comments on commit 485db4c

Please sign in to comment.