|
| 1 | +// |
| 2 | +// IntelBTPatcher.cpp |
| 3 | +// IntelBTPatcher |
| 4 | +// |
| 5 | +// Created by qcwap on 2021/2/8. |
| 6 | +// |
| 7 | + |
| 8 | +#include <Headers/kern_api.hpp> |
| 9 | +#include <Headers/kern_util.hpp> |
| 10 | +#include <Headers/plugin_start.hpp> |
| 11 | + |
| 12 | +#include "IntelBTPatcher.hpp" |
| 13 | + |
| 14 | +static CIntelBTPatcher ibtPatcher; |
| 15 | +static CIntelBTPatcher *callbackIBTPatcher = nullptr; |
| 16 | + |
| 17 | +static const char *bootargOff[] { |
| 18 | + "-ibtcompatoff" |
| 19 | +}; |
| 20 | + |
| 21 | +static const char *bootargDebug[] { |
| 22 | + "-ibtcompatdbg" |
| 23 | +}; |
| 24 | + |
| 25 | +static const char *bootargBeta[] { |
| 26 | + "-ibtcompatbeta" |
| 27 | +}; |
| 28 | + |
| 29 | +PluginConfiguration ADDPR(config) { |
| 30 | + xStringify(PRODUCT_NAME), |
| 31 | + parseModuleVersion(xStringify(MODULE_VERSION)), |
| 32 | + LiluAPI::AllowNormal | LiluAPI::AllowInstallerRecovery | LiluAPI::AllowSafeMode, |
| 33 | + bootargOff, |
| 34 | + arrsize(bootargOff), |
| 35 | + bootargDebug, |
| 36 | + arrsize(bootargDebug), |
| 37 | + bootargBeta, |
| 38 | + arrsize(bootargBeta), |
| 39 | + KernelVersion::MountainLion, |
| 40 | + KernelVersion::Monterey, |
| 41 | + []() { |
| 42 | + ibtPatcher.init(); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +static const char *IntelBTPatcher_IOBluetoothFamily[] { "/System/Library/Extensions/IOBluetoothFamily.kext/Contents/MacOS/IOBluetoothFamily" }; |
| 47 | + |
| 48 | +static KernelPatcher::KextInfo IntelBTPatcher_IOBluetoothInfo { |
| 49 | + "com.apple.iokit.IOBluetoothFamily", |
| 50 | + IntelBTPatcher_IOBluetoothFamily, |
| 51 | + 1, |
| 52 | + {true, true}, |
| 53 | + {}, |
| 54 | + KernelPatcher::KextInfo::Unloaded |
| 55 | +}; |
| 56 | + |
| 57 | +bool CIntelBTPatcher::init() |
| 58 | +{ |
| 59 | + DBGLOG(DRV_NAME, "%s", __PRETTY_FUNCTION__); |
| 60 | + callbackIBTPatcher = this; |
| 61 | + lilu.onKextLoadForce(&IntelBTPatcher_IOBluetoothInfo, 1, |
| 62 | + [](void *user, KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) { |
| 63 | + callbackIBTPatcher->processKext(patcher, index, address, size); |
| 64 | + }, this); |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +void CIntelBTPatcher::free() |
| 69 | +{ |
| 70 | + DBGLOG(DRV_NAME, "%s", __PRETTY_FUNCTION__); |
| 71 | +} |
| 72 | + |
| 73 | +void CIntelBTPatcher::processKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) |
| 74 | +{ |
| 75 | + DBGLOG(DRV_NAME, "%s", __PRETTY_FUNCTION__); |
| 76 | + if (getKernelVersion() < KernelVersion::Monterey) { |
| 77 | + if (IntelBTPatcher_IOBluetoothInfo.loadIndex == index) { |
| 78 | + DBGLOG(DRV_NAME, "%s", IntelBTPatcher_IOBluetoothInfo.id); |
| 79 | + |
| 80 | + KernelPatcher::RouteRequest findQueueRequestRequest { |
| 81 | + "__ZN25IOBluetoothHostController17FindQueuedRequestEtP22BluetoothDeviceAddresstbPP21IOBluetoothHCIRequest", |
| 82 | + newFindQueueRequest, |
| 83 | + oldFindQueueRequest |
| 84 | + }; |
| 85 | + patcher.routeMultiple(index, &findQueueRequestRequest, 1, address, size); |
| 86 | + if (patcher.getError() == KernelPatcher::Error::NoError) { |
| 87 | + DBGLOG(DRV_NAME, "routed %s", findQueueRequestRequest.symbol); |
| 88 | + } else { |
| 89 | + SYSLOG(DRV_NAME, "failed to resolve %s, error = %d", findQueueRequestRequest.symbol, patcher.getError()); |
| 90 | + patcher.clearError(); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +IOReturn CIntelBTPatcher::newFindQueueRequest(void *that, unsigned short arg1, void *addr, unsigned short arg2, bool arg3, void **hciRequestPtr) |
| 98 | +{ |
| 99 | + IOReturn ret = FunctionCast(newFindQueueRequest, callbackIBTPatcher->oldFindQueueRequest)(that, arg1, addr, arg2, arg3, hciRequestPtr); |
| 100 | + if (ret != 0 && arg1 == 0x2019) { |
| 101 | + ret = FunctionCast(newFindQueueRequest, callbackIBTPatcher->oldFindQueueRequest)(that, arg1, addr, 0xffff, arg3, hciRequestPtr); |
| 102 | + DBGLOG(DRV_NAME, "%s ret: %d arg1: 0x%04x arg2: 0x%04x arg3: %d ptr: %p", __FUNCTION__, ret, arg1, arg2, arg3, *hciRequestPtr); |
| 103 | + } |
| 104 | + return ret; |
| 105 | +} |
0 commit comments