-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathIntelBTPatcher.cpp
More file actions
314 lines (279 loc) · 13.9 KB
/
Copy pathIntelBTPatcher.cpp
File metadata and controls
314 lines (279 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// IntelBTPatcher.cpp
// IntelBTPatcher
//
// Created by zxystd <zxystd@foxmail.com> on 2021/2/8.
//
#include <Headers/kern_api.hpp>
#include <Headers/kern_util.hpp>
#include <Headers/plugin_start.hpp>
#include "IntelBTPatcher.hpp"
static CIntelBTPatcher ibtPatcher;
static CIntelBTPatcher *callbackIBTPatcher = nullptr;
static const char *bootargOff[] {
"-ibtcompatoff"
};
static const char *bootargDebug[] {
"-ibtcompatdbg"
};
static const char *bootargBeta[] {
"-ibtcompatbeta"
};
PluginConfiguration ADDPR(config) {
xStringify(PRODUCT_NAME),
parseModuleVersion(xStringify(MODULE_VERSION)),
LiluAPI::AllowNormal | LiluAPI::AllowInstallerRecovery | LiluAPI::AllowSafeMode,
bootargOff,
arrsize(bootargOff),
bootargDebug,
arrsize(bootargDebug),
bootargBeta,
arrsize(bootargBeta),
KernelVersion::MountainLion,
KernelVersion::Sonoma,
[]() {
ibtPatcher.init();
}
};
static const char *IntelBTPatcher_IOBluetoothFamily[] { "/System/Library/Extensions/IOBluetoothFamily.kext/Contents/MacOS/IOBluetoothFamily" };
static KernelPatcher::KextInfo IntelBTPatcher_IOBluetoothInfo {
"com.apple.iokit.IOBluetoothFamily",
IntelBTPatcher_IOBluetoothFamily,
1,
{true, true},
{},
KernelPatcher::KextInfo::Unloaded
};
static const char *IntelBTPatcher_IOUSBHostFamily[] {
"/System/Library/Extensions/IOUSBHostFamily.kext/Contents/MacOS/IOUSBHostFamily" };
static KernelPatcher::KextInfo IntelBTPatcher_IOUsbHostInfo {
"com.apple.iokit.IOUSBHostFamily",
IntelBTPatcher_IOUSBHostFamily,
1,
{true, true},
{},
KernelPatcher::KextInfo::Unloaded
};
void *CIntelBTPatcher::_hookPipeInstance = nullptr;
AsyncOwnerData *CIntelBTPatcher::_interruptPipeAsyncOwner = nullptr;
bool CIntelBTPatcher::_randomAddressInit = false;
bool CIntelBTPatcher::init()
{
DBGLOG(DRV_NAME, "%s", __PRETTY_FUNCTION__);
callbackIBTPatcher = this;
if (getKernelVersion() < KernelVersion::Monterey) {
lilu.onKextLoadForce(&IntelBTPatcher_IOBluetoothInfo, 1,
[](void *user, KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
callbackIBTPatcher->processKext(patcher, index, address, size);
}, this);
} else {
lilu.onKextLoadForce(&IntelBTPatcher_IOUsbHostInfo, 1,
[](void *user, KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
callbackIBTPatcher->processKext(patcher, index, address, size);
}, this);
}
return true;
}
void CIntelBTPatcher::free()
{
DBGLOG(DRV_NAME, "%s", __PRETTY_FUNCTION__);
}
void CIntelBTPatcher::processKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size)
{
DBGLOG(DRV_NAME, "%s", __PRETTY_FUNCTION__);
if (getKernelVersion() < KernelVersion::Monterey) {
if (IntelBTPatcher_IOBluetoothInfo.loadIndex == index) {
DBGLOG(DRV_NAME, "%s", IntelBTPatcher_IOBluetoothInfo.id);
KernelPatcher::RouteRequest findQueueRequestRequest {
"__ZN25IOBluetoothHostController17FindQueuedRequestEtP22BluetoothDeviceAddresstbPP21IOBluetoothHCIRequest",
newFindQueueRequest,
oldFindQueueRequest
};
patcher.routeMultiple(index, &findQueueRequestRequest, 1, address, size);
if (patcher.getError() == KernelPatcher::Error::NoError) {
DBGLOG(DRV_NAME, "routed %s", findQueueRequestRequest.symbol);
} else {
SYSLOG(DRV_NAME, "failed to resolve %s, error = %d", findQueueRequestRequest.symbol, patcher.getError());
patcher.clearError();
}
}
} else {
if (IntelBTPatcher_IOUsbHostInfo.loadIndex == index) {
SYSLOG(DRV_NAME, "%s", IntelBTPatcher_IOUsbHostInfo.id);
KernelPatcher::RouteRequest hostDeviceRequest {
"__ZN15IOUSBHostDevice13deviceRequestEP9IOServiceRN11StandardUSB13DeviceRequestEPvP18IOMemoryDescriptorRjP19IOUSBHostCompletionj",
newHostDeviceRequest,
oldHostDeviceRequest
};
patcher.routeMultiple(index, &hostDeviceRequest, 1, address, size);
if (patcher.getError() == KernelPatcher::Error::NoError) {
SYSLOG(DRV_NAME, "routed %s", hostDeviceRequest.symbol);
} else {
SYSLOG(DRV_NAME, "failed to resolve %s, error = %d", hostDeviceRequest.symbol, patcher.getError());
patcher.clearError();
}
KernelPatcher::RouteRequest asyncIORequest {
"__ZN13IOUSBHostPipe2ioEP18IOMemoryDescriptorjP19IOUSBHostCompletionj",
newAsyncIO,
oldAsyncIO
};
patcher.routeMultiple(index, &asyncIORequest, 1, address, size);
if (patcher.getError() == KernelPatcher::Error::NoError) {
SYSLOG(DRV_NAME, "routed %s", asyncIORequest.symbol);
} else {
SYSLOG(DRV_NAME, "failed to resolve %s, error = %d", asyncIORequest.symbol, patcher.getError());
patcher.clearError();
}
KernelPatcher::RouteRequest initPipeRequest {
"__ZN13IOUSBHostPipe28initWithDescriptorsAndOwnersEPKN11StandardUSB18EndpointDescriptorEPKNS0_37SuperSpeedEndpointCompanionDescriptorEP22AppleUSBHostControllerP15IOUSBHostDeviceP18IOUSBHostInterfaceht",
newInitPipe,
oldInitPipe
};
patcher.routeMultiple(index, &initPipeRequest, 1, address, size);
if (patcher.getError() == KernelPatcher::Error::NoError) {
SYSLOG(DRV_NAME, "routed %s", initPipeRequest.symbol);
} else {
SYSLOG(DRV_NAME, "failed to resolve %s, error = %d", initPipeRequest.symbol, patcher.getError());
patcher.clearError();
}
}
}
}
#pragma mark - For Bigsur-, patch unhandled 0x2019 opcode
#define HCI_OP_LE_START_ENCRYPTION 0x2019
IOReturn CIntelBTPatcher::newFindQueueRequest(void *that, unsigned short arg1, void *addr, unsigned short arg2, bool arg3, void **hciRequestPtr)
{
IOReturn ret = FunctionCast(newFindQueueRequest, callbackIBTPatcher->oldFindQueueRequest)(that, arg1, addr, arg2, arg3, hciRequestPtr);
if (ret != 0 && arg1 == HCI_OP_LE_START_ENCRYPTION) {
ret = FunctionCast(newFindQueueRequest, callbackIBTPatcher->oldFindQueueRequest)(that, arg1, addr, 0xffff, arg3, hciRequestPtr);
DBGLOG(DRV_NAME, "%s ret: %d arg1: 0x%04x arg2: 0x%04x arg3: %d ptr: %p", __FUNCTION__, ret, arg1, arg2, arg3, *hciRequestPtr);
}
return ret;
}
#pragma mark - For Monterey+ patch for intercept HCI REQ and RESP
StandardUSB::DeviceRequest randomAddressRequest;
// Hardcoded Random Address HCI Command
const uint8_t randomAddressHci[9] = {0x05, 0x20, 0x06, 0x94, 0x50, 0x64, 0xD0, 0x78, 0x6B};
IOBufferMemoryDescriptor *writeHCIDescriptor = nullptr;
#define MAX_HCI_BUF_LEN 255
#define HCI_OP_RESET 0x0c03
#define HCI_OP_LE_SET_SCAN_PARAM 0x200B
#define HCI_OP_LE_SET_SCAN_ENABLE 0x200C
#define HCI_OP_LE_READ_REMOTE_FEATURES 0x2016
IOReturn CIntelBTPatcher::newHostDeviceRequest(void *that, IOService *provider, StandardUSB::DeviceRequest &request, void *data, IOMemoryDescriptor *descriptor, unsigned int &length, IOUSBHostCompletion *completion, unsigned int timeout)
{
HciCommandHdr *hdr = nullptr;
uint32_t hdrLen = 0;
char hciBuf[MAX_HCI_BUF_LEN] = {0};
if (data == nullptr) {
if (descriptor != nullptr && descriptor->getLength() > 0) {
descriptor->readBytes(0, hciBuf, min(descriptor->getLength(), MAX_HCI_BUF_LEN));
hdrLen = (uint32_t)min(descriptor->getLength(), MAX_HCI_BUF_LEN);
}
hdr = (HciCommandHdr *)hciBuf;
if (hdr->opcode == HCI_OP_LE_SET_SCAN_PARAM) {
if (!_randomAddressInit) {
randomAddressRequest.bmRequestType = makeDeviceRequestbmRequestType(kRequestDirectionOut, kRequestTypeClass, kRequestRecipientInterface);
randomAddressRequest.bRequest = 0xE0;
randomAddressRequest.wIndex = 0;
randomAddressRequest.wValue = 0;
randomAddressRequest.wLength = 9;
length = 9;
if (writeHCIDescriptor == nullptr)
writeHCIDescriptor = IOBufferMemoryDescriptor::withBytes(randomAddressHci, 9, kIODirectionOut);
writeHCIDescriptor->prepare(kIODirectionOut);
IOReturn ret = FunctionCast(newHostDeviceRequest, callbackIBTPatcher->oldHostDeviceRequest)(that, provider, randomAddressRequest, nullptr, writeHCIDescriptor, length, nullptr, timeout);
writeHCIDescriptor->complete();
const char *randAddressDump = _hexDumpHCIData((uint8_t *)randomAddressHci, 9);
if (randAddressDump) {
SYSLOG(DRV_NAME, "[PATCH] Sending Random Address HCI %d %s", ret, randAddressDump);
IOFree((void *)randAddressDump, 9 * 3 + 1);
}
_randomAddressInit = true;
SYSLOG(DRV_NAME, "[PATCH] Resend LE SCAN PARAM HCI %d", ret);
}
} else if (hdr->opcode == HCI_OP_LE_READ_REMOTE_FEATURES) {
IOReturn ret = FunctionCast(newHostDeviceRequest, callbackIBTPatcher->oldHostDeviceRequest)(that, provider, request, nullptr, descriptor, length, nullptr, timeout);
SYSLOG(DRV_NAME, "[PATCH] Sending extra LE Read Remote Features command %d", ret);
}
} else {
hdr = (HciCommandHdr *)data;
hdrLen = request.wLength - 3;
}
if (hdr) {
// HCI reset, we need to send Random address again
if (hdr->opcode == HCI_OP_RESET)
_randomAddressInit = false;
#if DEBUG
DBGLOG(DRV_NAME, "[%s] bRequest: 0x%x direction: %s type: %s recipient: %s wValue: 0x%02x wIndex: 0x%02x opcode: 0x%04x len: %d length: %d async: %d", provider->getName(), request.bRequest, requestDirectionNames[(request.bmRequestType & kDeviceRequestDirectionMask) >> kDeviceRequestDirectionPhase], requestRecipientNames[(request.bmRequestType & kDeviceRequestRecipientMask) >> kDeviceRequestRecipientPhase], requestTypeNames[(request.bmRequestType & kDeviceRequestTypeMask) >> kDeviceRequestTypePhase], request.wValue, request.wIndex, hdr->opcode, hdr->len, request.wLength, completion != nullptr);
if (hdrLen) {
const char *dump = _hexDumpHCIData((uint8_t *)hdr, hdrLen);
if (dump) {
DBGLOG(DRV_NAME, "[Request]: %s", dump);
IOFree((void *)dump, hdrLen * 3 + 1);
}
}
#endif
}
return FunctionCast(newHostDeviceRequest, callbackIBTPatcher->oldHostDeviceRequest)(that, provider, request, data, descriptor, length, completion, timeout);
}
#define HCI_EVT_LE_META 0x3E
#define HCI_EVT_LE_META_READ_REMOTE_FEATURES_COMPLETE 0x04
uint8_t fakePhyUpdateCompleteEvent[8] = {0x3E, 0x06, 0x0C, 0x00, 0x00, 0x00, 0x02, 0x02};
static void asyncIOCompletion(void* owner, void* parameter, IOReturn status, uint32_t bytesTransferred)
{
AsyncOwnerData *asyncOwner = (AsyncOwnerData *)owner;
IOMemoryDescriptor* dataBuffer = asyncOwner->dataBuffer;
static bool skipExtraReadRemoteFeaturesComplete = true;
if (dataBuffer && bytesTransferred) {
void *buffer = IOMalloc(bytesTransferred);
dataBuffer->readBytes(0, buffer, bytesTransferred);
HciEventHdr *hdr = (HciEventHdr *)buffer;
if (hdr->evt == HCI_EVT_LE_META && hdr->data[0] == HCI_EVT_LE_META_READ_REMOTE_FEATURES_COMPLETE) {
if (skipExtraReadRemoteFeaturesComplete) {
skipExtraReadRemoteFeaturesComplete = false;
} else {
// Copy Connection Handle
fakePhyUpdateCompleteEvent[4] = hdr->data[2];
fakePhyUpdateCompleteEvent[5] = hdr->data[3];
dataBuffer->writeBytes(0, fakePhyUpdateCompleteEvent, 8);
skipExtraReadRemoteFeaturesComplete = true;
}
}
IOFree(buffer, bytesTransferred);
}
if (asyncOwner->action)
asyncOwner->action(asyncOwner->owner, parameter, status, bytesTransferred);
}
IOReturn CIntelBTPatcher::
newAsyncIO(void *that, IOMemoryDescriptor* dataBuffer, uint32_t bytesTransferred, IOUSBHostCompletion* completion, uint32_t completionTimeoutMs)
{
if (that == _hookPipeInstance && completion) {
_interruptPipeAsyncOwner->action = completion->action;
_interruptPipeAsyncOwner->owner = completion->owner;
_interruptPipeAsyncOwner->dataBuffer = dataBuffer;
completion->action = asyncIOCompletion;
completion->owner = _interruptPipeAsyncOwner;
}
return FunctionCast(newAsyncIO, callbackIBTPatcher->oldAsyncIO)(that, dataBuffer, bytesTransferred, completion, completionTimeoutMs);
}
#define VENDOR_USB_INTEL 0x8087
int CIntelBTPatcher::
newInitPipe(void *that, StandardUSB::EndpointDescriptor const *descriptor, StandardUSB::SuperSpeedEndpointCompanionDescriptor const *superDescriptor, AppleUSBHostController *controller, IOUSBHostDevice *device, IOUSBHostInterface *interface, unsigned char a7, unsigned short a8)
{
int ret = FunctionCast(newInitPipe, callbackIBTPatcher->oldInitPipe)(that, descriptor, superDescriptor, controller, device, interface, a7, a8);
if (device) {
const StandardUSB::DeviceDescriptor *deviceDescriptor = device->getDeviceDescriptor();
if (deviceDescriptor &&
deviceDescriptor->idVendor == VENDOR_USB_INTEL) {
uint8_t epType = StandardUSB::getEndpointType(descriptor);
if (epType == kIOUSBEndpointTypeInterrupt) {
CIntelBTPatcher::_hookPipeInstance = that;
if (!CIntelBTPatcher::_interruptPipeAsyncOwner)
CIntelBTPatcher::_interruptPipeAsyncOwner = new AsyncOwnerData;
CIntelBTPatcher::_randomAddressInit = false;
}
}
}
return ret;
}