Skip to content

Commit

Permalink
OcAppleKernelLib: Integrate USB restriction patches (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
PMheart authored Jun 12, 2023
1 parent ba06e05 commit d52fc46
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ OpenCore Changelog
- Fixed `AppleXcpmForceBoost` quirk on macOS 14
- Updated builtin firmware versions for SMBIOS and the rest
- Added `ConsoleFont` option to load custom console font for `Builtin` renderer
- Improved `XhciPortLimit` quirk on macOS 11 to 14

This comment has been minimized.

Copy link
@narcyzzo

narcyzzo Jun 12, 2023

So the patch now works, or its still recommended to Map Ports?

This comment has been minimized.

Copy link
@PMheart

PMheart Jun 12, 2023

Author Member

Recommendation remains the same; i.e., avoid using it wherever possible.

This comment has been minimized.

Copy link
@WaTeZhuangJiDingZhi

WaTeZhuangJiDingZhi Jun 12, 2023

Sorry, the actual test of the patch is effective from 11 to 13, but I haven't finished the 14 beta,I can provide relevant search tutorials

This comment has been minimized.

Copy link
@PMheart

PMheart Jun 12, 2023

Author Member

It should work for 14.0 b1 too, as there are only byte changes which are masked out this way. The new patch here is made to be unique for 11-14.

This comment has been minimized.

Copy link
@WaTeZhuangJiDingZhi

WaTeZhuangJiDingZhi Jun 12, 2023

I'm from China, you may need to translate @PMheart

This comment has been minimized.

Copy link
@PMheart

PMheart Jun 12, 2023

Author Member

Thank you for the guide, but I am afraid that we were talking about the same thing. OpenCore supports mask patching, which wipes out the unnecessary bytes and makes the patch stabler.

This comment has been minimized.

Copy link
@WaTeZhuangJiDingZhi

WaTeZhuangJiDingZhi Jun 12, 2023

So it's best implemented via a separate quirk or patch, reverting to the original XhciPortLimit quirk in。

This comment has been minimized.

Copy link
@PMheart

PMheart Jun 12, 2023

Author Member

Not quite, since we are doing the same thing - increasing the maximum available ports. This behavior remains unchanged, and is exactly what XhciPortLimit does. I see no points on implementing a new quirk; sorry.


#### v0.9.2
- Added `DisableIoMapperMapping` quirk, thx @CaseySJ
Expand Down
74 changes: 64 additions & 10 deletions Library/OcAppleKernelLib/CommonPatches.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,26 +645,65 @@ PATCHER_GENERIC_PATCH

STATIC
CONST UINT8
mRemoveUsbLimitIoP1Find[] = {
mRemoveUsbLimitIoP1Find1[] = {
0x0F, 0x0F, 0x87
};

STATIC
CONST UINT8
mRemoveUsbLimitIoP1Replace[] = {
mRemoveUsbLimitIoP1Replace1[] = {
0x40, 0x0F, 0x87
};

STATIC
PATCHER_GENERIC_PATCH
mRemoveUsbLimitIoP1Patch = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitIoP1"),
mRemoveUsbLimitIoP1Patch1 = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitIoP1 part 1"),
.Base = "__ZN16AppleUSBHostPort15setPortLocationEj",
.Find = mRemoveUsbLimitIoP1Find,
.Find = mRemoveUsbLimitIoP1Find1,
.Mask = NULL,
.Replace = mRemoveUsbLimitIoP1Replace,
.Replace = mRemoveUsbLimitIoP1Replace1,
.ReplaceMask = NULL,
.Size = sizeof (mRemoveUsbLimitIoP1Replace),
.Size = sizeof (mRemoveUsbLimitIoP1Replace1),
.Count = 1,
.Skip = 0,
.Limit = 4096
};

STATIC
CONST UINT8
mRemoveUsbLimitIoP1Find2[] = {
0x41, 0x83, 0x00, 0x0F, ///< and whatever, 0x0Fh
0x41, 0xD3, 0x00, ///< shl whatever, cl
0x00, 0x09, 0x00 ///< or ebx, whatever
};

STATIC
CONST UINT8
mRemoveUsbLimitIoP1Replace2[] = {
0x41, 0x83, 0x00, 0x3F, ///< and whatever, 0x3Fh
0x41, 0xD3, 0x00, ///< shl whatever, cl
0x00, 0x09, 0x00 ///< or ebx, whatever
};

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

STATIC
PATCHER_GENERIC_PATCH
mRemoveUsbLimitIoP1Patch2 = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitIoP1 part 2"),
.Base = "__ZN16AppleUSBHostPort15setPortLocationEj",
.Find = mRemoveUsbLimitIoP1Find2,
.Mask = mRemoveUsbLimitIoP1Mask2,
.Replace = mRemoveUsbLimitIoP1Replace2,
.ReplaceMask = mRemoveUsbLimitIoP1Mask2,
.Size = sizeof (mRemoveUsbLimitIoP1Replace2),
.Count = 1,
.Skip = 0,
.Limit = 4096
Expand Down Expand Up @@ -693,11 +732,26 @@ PatchUsbXhciPortLimit1 (
return EFI_NOT_FOUND;
}

Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitIoP1Patch);
Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitIoP1Patch1);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply port patch com.apple.iokit.IOUSBHostFamily part 1 - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success port com.apple.iokit.IOUSBHostFamily part 1\n"));
}

//
// The following patch is only needed on macOS 11.1 (Darwin 20.2.0) and above; skip it otherwise.
//
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_BIG_SUR, 2, 0), 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping port patch com.apple.iokit.IOUSBHostFamily part 2 on %u\n", KernelVersion));
return Status;
}

Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitIoP1Patch2);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply port patch com.apple.iokit.IOUSBHostFamily - %r\n", Status));
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply port patch com.apple.iokit.IOUSBHostFamily part 2 - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success port com.apple.iokit.IOUSBHostFamily\n"));
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success port com.apple.iokit.IOUSBHostFamily part 2\n"));
}

return Status;
Expand Down

0 comments on commit d52fc46

Please # to comment.