Root/
Source at commit 629 created 13 years 4 months ago. By meklort, 3150 changes. preperation for work on the IOAccelerator kext / QE. | |
---|---|
1 | ␉/*␊ |
2 | * Copyright (c) 2010 Evan Lojewski. All rights reserved.␊ |
3 | *␉␊ |
4 | *␉KextPather␊ |
5 | *␉This is an experimental module that I'm looking into implimenting.␊ |
6 | *␉The main purpose is to replace the need for programs such as ␊ |
7 | * NetbookInstaller's kext patching routines. THis way, Apple's kexts can be␊ |
8 | * patched whe loaded instead. (eg: GMA950 kext, Bluetooth + Wifi kexts)␊ |
9 | */␊ |
10 | ␊ |
11 | #include "libsaio.h"␊ |
12 | #include "zlib.h"␊ |
13 | #include "kext_patcher.h"␊ |
14 | #include "boot.h"␊ |
15 | #include "bootstruct.h"␊ |
16 | #include "pci.h"␊ |
17 | #include "drivers.h"␊ |
18 | #include "mkext.h"␊ |
19 | #include "modules.h"␊ |
20 | #include "hex_editor.h"␊ |
21 | ␊ |
22 | ␊ |
23 | #define kHDACodec␉␉␉␉"HDACodec"␊ |
24 | ␊ |
25 | ␊ |
26 | #ifndef DEBUG_KEXT_PATCHER␊ |
27 | #define DEBUG_KEXT_PATCHER 0␊ |
28 | #endif␊ |
29 | ␊ |
30 | #if DEBUG_KEXT_PATCHER␊ |
31 | #define DBG(x...)␉printf(x)␊ |
32 | #else␊ |
33 | #define DBG(x...)␊ |
34 | #endif␊ |
35 | ␊ |
36 | ␊ |
37 | bool patch_kext(TagPtr plist, char* plistbuffer, void* start);␊ |
38 | bool patch_gma_kexts(TagPtr plist, char* plistbuffer, void* start);␊ |
39 | bool patch_bcm_kext(TagPtr plist, char* plistbuffer, void* start);␊ |
40 | bool patch_hda_kext(TagPtr plist, char* plistbuffer, void* start);␊ |
41 | bool patch_hda_controller(TagPtr plist, char* plistbuffer, void* start);␊ |
42 | ␊ |
43 | int chartohex(char c);␊ |
44 | ␊ |
45 | static void * z_alloc(void *, u_int items, u_int size);␊ |
46 | static void z_free(void *, void *ptr);␊ |
47 | ␊ |
48 | uint16_t patch_gma_deviceid = 0;␊ |
49 | uint16_t patch_bcm_deviceid = 0;␊ |
50 | // TODO: add detection code / a method for users to enter the id␊ |
51 | uint16_t patch_hda_codec = 0;␊ |
52 | ␊ |
53 | #define NEEDS_PATCHING␉␉(patch_bcm_deviceid || patch_gma_deviceid || patch_hda_codec)␊ |
54 | ␊ |
55 | typedef struct z_mem {␊ |
56 | uint32_t alloc_size;␊ |
57 | uint8_t data[0];␊ |
58 | } z_mem;␊ |
59 | ␊ |
60 | /*␊ |
61 | * Space allocation and freeing routines for use by zlib routines.␊ |
62 | */␊ |
63 | void *␊ |
64 | z_alloc(void * notused __unused, u_int num_items, u_int size)␊ |
65 | {␊ |
66 | void * result = NULL;␊ |
67 | z_mem * zmem = NULL;␊ |
68 | uint32_t total = num_items * size;␊ |
69 | uint32_t allocSize = total + sizeof(zmem);␊ |
70 | ␊ |
71 | zmem = (z_mem *)malloc(allocSize);␊ |
72 | if (!zmem) {␊ |
73 | goto finish;␊ |
74 | }␊ |
75 | zmem->alloc_size = allocSize;␊ |
76 | result = (void *)&(zmem->data);␊ |
77 | finish:␊ |
78 | return result;␊ |
79 | }␊ |
80 | ␊ |
81 | void␊ |
82 | z_free(void * notused __unused, void * ptr)␊ |
83 | {␊ |
84 | uint32_t * skipper = (uint32_t *)ptr - 1;␊ |
85 | z_mem * zmem = (z_mem *)skipper;␊ |
86 | free((void *)zmem);␊ |
87 | return;␊ |
88 | }␊ |
89 | ␊ |
90 | ␊ |
91 | unsigned long Mkext_Alder32( unsigned char * buffer, long length );␊ |
92 | ␊ |
93 | void KextPatcher_hook(void* current, void* arg2, void* arg3, void* arg4);␊ |
94 | ␊ |
95 | /**␊ |
96 | ** KextPatcher_start -> module start␊ |
97 | **␉␉Notified the module system that this module will hook into the ␊ |
98 | **␉␉LoadMatchedModules and LoadDriverMKext functions␊ |
99 | **/␊ |
100 | void KextPatcher_start()␊ |
101 | {␉␉␊ |
102 | ␉// Hooks into the following:␊ |
103 | ␉//␉execute_hook("LoadDriverMKext", (void*)package, (void*) length, NULL, NULL);␊ |
104 | ␉// execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
105 | ␊ |
106 | ␉register_hook_callback("PCIDevice", &KextPatcher_hook);␊ |
107 | ␉//register_hook_callback("LoadMatchedModules", &kext_loaded); ␊ |
108 | ␉register_hook_callback("LoadDriverMKext", &mkext_loaded); ␊ |
109 | ␊ |
110 | }␊ |
111 | ␊ |
112 | /**␊ |
113 | ** kext_loaded -> Called whenever a kext is in read into memory␊ |
114 | **␉␉This function will be used to patch kexts ( eg AppleInteIntegratedFramebuffer)␊ |
115 | **␉␉and their plists when they are loaded into memmory␊ |
116 | **/␊ |
117 | void kext_loaded(void* moduletmp, void* lengthprt, void* executableAddr, void* arg3)␊ |
118 | {␊ |
119 | ␉␊ |
120 | ␉//ModulePtr module = moduletmp;␊ |
121 | ␉//long length = *(long*)lengthprt;␊ |
122 | ␉//long length2 = strlen(module->plistAddr);␊ |
123 | ␉// *(long*)lengthprt = length2 + 5 * 1024 * 1024;␊ |
124 | ␊ |
125 | ␉//printf("Loading %s, lenght is %d, executable is 0x%X\n", module->plistAddr, length, executableAddr);␊ |
126 | ␉//getc();␊ |
127 | }␊ |
128 | ␊ |
129 | /**␊ |
130 | ** mkext_loaded -> Called whenever an mkext is in read into memory␊ |
131 | **␉␉This function will be used to patch mkext. Matching kexts will be␊ |
132 | **␉␉Extracted, modified, and then compressed again. Note: I need to determine␊ |
133 | **␉␉what sort of slowdown this will cause and if it's worth implimenting.␊ |
134 | **/␊ |
135 | ␊ |
136 | void mkext_loaded(void* filespec, void* packagetmp, void* lengthtmp, void* arg3)␊ |
137 | {␊ |
138 | ␉const char* hda_codec;␊ |
139 | ␉int len = 0;␊ |
140 | ␉␊ |
141 | ␉if (getValueForKey(kHDACodec, &hda_codec, &len, &bootInfo->bootConfig))␊ |
142 | ␉{␊ |
143 | ␉␉int index = 0;␊ |
144 | ␉␉while(len)␊ |
145 | ␉␉{␊ |
146 | ␉␉␉patch_hda_codec <<= 4;␊ |
147 | ␉␉␉patch_hda_codec |= chartohex(hda_codec[index]);␊ |
148 | ␉␉␉len--;␊ |
149 | ␉␉␉index++;␊ |
150 | ␉␉}␊ |
151 | ␉}␊ |
152 | ␉␉␊ |
153 | ␉if(!NEEDS_PATCHING) return;␉// No need to apply a patch, hardware doesn't need it␊ |
154 | ␉␊ |
155 | ␉int version = 0;␊ |
156 | ␉//int length = *((int*)lengthtmp);␊ |
157 | ␉mkext_basic_header* package = packagetmp;␊ |
158 | ␊ |
159 | ␉// Verify the MKext.␊ |
160 | if (( MKEXT_GET_MAGIC(package)␉␉!= MKEXT_MAGIC ) ||␊ |
161 | ( MKEXT_GET_SIGNATURE(package)␉!= MKEXT_SIGN ) ||␊ |
162 | ( MKEXT_GET_LENGTH(package)␉␉> kLoadSize )␉ ||␊ |
163 | ( MKEXT_GET_CHECKSUM(package) !=␊ |
164 | ␉␉ Mkext_Alder32((unsigned char *)&package->version, MKEXT_GET_LENGTH(package) - 0x10) ) )␊ |
165 | {␊ |
166 | return;␊ |
167 | ␉␉// Don't try to patch a b␊ |
168 | }␉␊ |
169 | ␉␊ |
170 | ␉/*␊ |
171 | ␉if(strcmp(filespec, "/System/Library/Caches/com.apple.kext.caches/Startup/Extensions.mkext") == 0)␊ |
172 | ␉{␊ |
173 | ␉␉printf("Invalidating mkext %s\n", filespec);␊ |
174 | ␉␉// 10.6 cache folder. Doesn't contain certain extensions we need, so invalidate it.␊ |
175 | ␉␉//package->adler32++;␊ |
176 | ␉␉// NOTE: double check that this is needed␊ |
177 | ␉␉package->magic = 0x00;␊ |
178 | ␉␉return;␊ |
179 | ␉}*/␊ |
180 | ␉␊ |
181 | ␉␊ |
182 | ␉version = MKEXT_GET_VERSION(package);␊ |
183 | ␉␊ |
184 | ␉if(version == 0x01008000) // mkext1␊ |
185 | ␉{␊ |
186 | ␉␉// mkext1 uses lzss␊ |
187 | ␉␉mkext1_header* package = packagetmp;␊ |
188 | ␉␉int i;␊ |
189 | ␉␉for(i = 0; i < MKEXT_GET_COUNT(package); i++)␊ |
190 | ␉␉{␊ |
191 | ␉␉␉DBG("Parsing kext %d\n", i);␊ |
192 | ␉␉␉//mkext_kext* kext = MKEXT1_GET_KEXT(package, i);␊ |
193 | ␉␉␉// uses decompress_lzss␊ |
194 | ␉␉␉// TODO: handle kext␊ |
195 | ␊ |
196 | ␉␉}␊ |
197 | ␉}␊ |
198 | ␉else if((version & 0xFFFF0000) == 0x02000000) // mkext2␊ |
199 | ␉{␊ |
200 | ␉␉DBG("Mkext2 package located at 0x%X\n", package);␊ |
201 | ␊ |
202 | ␉␉// mkext2 uses zlib␉␉␊ |
203 | ␉␉mkext2_header* package = packagetmp;␊ |
204 | ␉␉z_stream zstream;␊ |
205 | ␉␉bool zstream_inited = false;␊ |
206 | ␉␉int zlib_result;␊ |
207 | ␉␉int plist_offset = MKEXT2_GET_PLIST(package);␊ |
208 | ␉␉␊ |
209 | ␉␉char* plist = malloc(MKEXT2_GET_PLIST_FULLSIZE(package));␊ |
210 | ␉␉␊ |
211 | ␉␉bzero(&zstream, sizeof(zstream));␉␉␊ |
212 | ␉␉zstream.next_in = (UInt8*)((char*)package + plist_offset);␊ |
213 | ␉␉zstream.avail_in = MKEXT2_GET_PLIST_COMPSIZE(package);␊ |
214 | ␉␉␊ |
215 | ␉␉zstream.next_out = (UInt8*)plist;␊ |
216 | ␉␉zstream.avail_out = MKEXT2_GET_PLIST_FULLSIZE(package);␊ |
217 | ␉␉␊ |
218 | ␉␉zstream.zalloc = z_alloc;␊ |
219 | ␉␉zstream.zfree = z_free;␊ |
220 | ␉␉␉␉␊ |
221 | ␉␉zlib_result = inflateInit(&zstream);␊ |
222 | ␉␉if (Z_OK != zlib_result)␊ |
223 | ␉␉{␊ |
224 | ␉␉␉printf("ZLIB Error: %s\n", zstream.msg);␊ |
225 | ␉␉␉getc();␊ |
226 | ␉␉}␊ |
227 | ␉␉else ␊ |
228 | ␉␉{␊ |
229 | ␉␉␉zstream_inited = true;␊ |
230 | ␉␉}␊ |
231 | ␊ |
232 | ␉␉␊ |
233 | ␉␉zlib_result = inflate(&zstream, Z_FINISH);␊ |
234 | ␉␉if (zstream_inited) inflateEnd(&zstream);␊ |
235 | ␊ |
236 | ␉␉DBG("Inflated result is %d, in: %d bytes, out: %d bytes\n", zlib_result, zstream.total_in, zstream.total_out);␊ |
237 | ␉␉if (zlib_result == Z_STREAM_END || zlib_result == Z_OK)␊ |
238 | ␉␉{␉␉␉␊ |
239 | ␉␉␉config_file_t plistData;␊ |
240 | ␉␉␉config_file_t allDicts;␊ |
241 | ␉␉␉bzero(&plistData, sizeof(plistData));␊ |
242 | ␉␉␉bzero(&allDicts, sizeof(allDicts));␊ |
243 | ␉␉␉␊ |
244 | ␉␉␉XMLParseFile( plist, &plistData.dictionary );␊ |
245 | ␊ |
246 | ␉␉␉int count = 0;␊ |
247 | ␊ |
248 | ␉␉␉allDicts.dictionary = XMLGetProperty(plistData.dictionary, kMKEXTInfoDictionariesKey);␊ |
249 | ␉␉␉count = XMLTagCount(allDicts.dictionary);␊ |
250 | ␊ |
251 | ␉␉␉DBG("Plist contains %d kexts\n", count);␊ |
252 | ␉␉␉␊ |
253 | ␉␉␉␊ |
254 | ␉␉␉bool patched = false;␊ |
255 | ␉␉␉for(; count--; count > 0)␊ |
256 | ␉␉␉{␊ |
257 | ␉␉␉␉TagPtr kextEntry = XMLGetElement(allDicts.dictionary, count);␊ |
258 | ␉␉␉␉patched |= patch_kext(kextEntry, plist, package);␊ |
259 | ␉␉␉}␊ |
260 | ␉␉␉␊ |
261 | ␊ |
262 | ␉␉␉if(patched)␊ |
263 | ␉␉␉{␊ |
264 | ␉␉␉␉zstream_inited = false;␊ |
265 | ␉␉␉␉// Recompress the plist␊ |
266 | ␉␉␉␉bzero(&zstream, sizeof(zstream));␉␉␊ |
267 | ␉␉␉␉zstream.next_in = (UInt8*)plist;␊ |
268 | ␉␉␉␉zstream.next_out = (UInt8*)package + plist_offset;␊ |
269 | ␉␉␉␉zstream.avail_in = MKEXT2_GET_PLIST_FULLSIZE(package);␊ |
270 | ␉␉␉␉zstream.avail_out = MKEXT2_GET_PLIST_FULLSIZE(package)<<2;␉// Give us some extra free space, just in case␊ |
271 | ␉␉␉␉zstream.zalloc = Z_NULL;␊ |
272 | ␉␉␉␉zstream.zfree = Z_NULL;␊ |
273 | ␉␉␉␉zstream.opaque = Z_NULL;␊ |
274 | ␉␉␉␉␊ |
275 | ␉␉␉␉␊ |
276 | ␉␉␉␉zlib_result = deflateInit2(&zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,15, 8 /* memLevel */, Z_DEFAULT_STRATEGY);␊ |
277 | ␉␉␉␉if (Z_OK != zlib_result) {␊ |
278 | ␉␉␉␉␉printf("ZLIB Deflate Error: %s\n", zstream.msg);␊ |
279 | ␉␉␉␉␉getc();␊ |
280 | ␉␉␉␉}␊ |
281 | ␉␉␉␉else ␊ |
282 | ␉␉␉␉{␊ |
283 | ␉␉␉␉␉zstream_inited = true;␊ |
284 | ␉␉␉␉}␊ |
285 | ␉␉␉␉␊ |
286 | ␉␉␉␉zlib_result = deflate(&zstream, Z_FINISH);␊ |
287 | ␉␉␉␉␊ |
288 | ␉␉␉␉if (zlib_result == Z_STREAM_END)␊ |
289 | ␉␉␉␉{␊ |
290 | ␉␉␉␉␉DBG("Deflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, MKEXT2_GET_PLIST_FULLSIZE(package));␊ |
291 | ␉␉␉␉} ␊ |
292 | ␉␉␉␉else if (zlib_result == Z_OK)␊ |
293 | ␉␉␉␉{␊ |
294 | ␉␉␉␉␉/* deflate filled output buffer, meaning the data doesn't compress.␊ |
295 | ␉␉␉␉␉ */␊ |
296 | ␉␉␉␉␉DBG("Deflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, MKEXT2_GET_PLIST_FULLSIZE(package));␊ |
297 | ␉␉␉␉␉␊ |
298 | ␉␉␉␉} ␊ |
299 | ␉␉␉␉else if (zlib_result != Z_STREAM_ERROR)␊ |
300 | ␉␉␉␉{␊ |
301 | ␉␉␉␉␉printf("ZLIB Deflate Error: %s\n", zstream.msg);␊ |
302 | ␉␉␉␉␉getc();␊ |
303 | ␉␉␉␉}␊ |
304 | ␉␉␉␉␊ |
305 | ␉␉␉␉if(zstream.total_out != MKEXT2_GET_PLIST_COMPSIZE(package))␊ |
306 | ␉␉␉␉{␊ |
307 | ␉␉␉␉␉// Update the mkext length␊ |
308 | ␉␉␉␉␉MKEXT2_HDR_CAST(package)->length = MKEXT_SWAP(MKEXT_GET_LENGTH(package) - MKEXT2_GET_PLIST_COMPSIZE(package) + zstream.total_out);␊ |
309 | ␉␉␉␉␉MKEXT2_HDR_CAST(package)->plist_compressed_size = MKEXT_SWAP(zstream.total_out);␊ |
310 | ␉␉␉␉␉*((int*)lengthtmp) -= MKEXT2_GET_PLIST_COMPSIZE(package);␊ |
311 | ␉␉␉␉␉*((int*)lengthtmp) += zstream.total_out;␊ |
312 | ␉␉␉␉}␊ |
313 | ␉␉␉␉␉␉␉␉␊ |
314 | ␉␉␉␉if (zstream_inited) deflateEnd(&zstream);␊ |
315 | ␉␉␉␉␊ |
316 | ␉␉␉␉␊ |
317 | ␉␉␉␉␊ |
318 | ␉␉␉␉// re alder32 the new mkext2 package␊ |
319 | ␉␉␉␉MKEXT_HDR_CAST(package)->adler32 = ␊ |
320 | ␉␉␉␉␉MKEXT_SWAP(Mkext_Alder32((unsigned char *)&package->version,␊ |
321 | ␉␉␉␉␉␉␉␉␉␉␉ MKEXT_GET_LENGTH(package) - 0x10));␊ |
322 | ␉␉␉}␊ |
323 | ␉␉}␊ |
324 | ␉␉else␊ |
325 | ␉␉{␊ |
326 | ␉␉␉printf("ZLIB Error: %s\n", zstream.msg);␊ |
327 | ␉␉␉getc();␊ |
328 | ␉␉}␊ |
329 | ␊ |
330 | ␉␉//config_file_t mkextPlist;␊ |
331 | ␉␉//ParseXMLFile((char*) plist, &mkextPlist.dictionary);␊ |
332 | ␉␉␊ |
333 | ␉␉␊ |
334 | ␉␉␊ |
335 | ␉␉␊ |
336 | ␉␉␊ |
337 | ␉␉/*␉␉int i;␊ |
338 | ␉␉for(i = 0; i < MKEXT_GET_COUNT(package); i++)␊ |
339 | ␉␉{␊ |
340 | ␉␉␉printf("Parsing kext %d\n", i);␊ |
341 | ␉␉}␊ |
342 | ␉␉*/␊ |
343 | ␉␉␊ |
344 | ␊ |
345 | ␉}␊ |
346 | ␊ |
347 | ␉␊ |
348 | ␉DBG("Loading %s, version 0x%x\n", filespec, version);␊ |
349 | ␉//getc();␊ |
350 | }␊ |
351 | ␊ |
352 | // FIXME: only handles mkext2 entries␊ |
353 | bool patch_kext(TagPtr plist, char* plistbuffer, void* start)␊ |
354 | {␊ |
355 | ␉char* bundleID;␊ |
356 | ␉␊ |
357 | ␉if(XMLGetProperty(plist, kMKEXTExecutableKey) == NULL) return false;␉// Kext is a plist only kext, don't patch␊ |
358 | ␉␊ |
359 | ␉bundleID = XMLCastString(XMLGetProperty(plist, kPropCFBundleIdentifier));␊ |
360 | ␉␊ |
361 | ␉␊ |
362 | ␉if(patch_gma_deviceid &&␊ |
363 | ␉ (␊ |
364 | ␉␉␉(strcmp(bundleID, "com.apple.driver.AppleIntelGMA950") == 0) ||␊ |
365 | ␉␉␉(strcmp(bundleID, "com.apple.driver.AppleIntelIntegratedFramebuffer") == 0)␊ |
366 | ␉␉ )␊ |
367 | ␉ )␊ |
368 | ␉{␊ |
369 | ␉␉if(strcmp(bundleID, "com.apple.driver.AppleIntelIntegratedFramebuffer") == 0 || patch_gma_deviceid == 0x27ae)␊ |
370 | ␉␉{␊ |
371 | ␉␉␉return patch_gma_kexts(plist, plistbuffer, start);␊ |
372 | ␉␉}␊ |
373 | ␉␉else␊ |
374 | ␉␉{␊ |
375 | ␉␉␉␊ |
376 | ␉␉}␊ |
377 | ␊ |
378 | ␉}␊ |
379 | ␉else if(patch_bcm_deviceid && (strcmp(bundleID, "com.apple.driver.AirPortBrcm43xx") == 0))␊ |
380 | ␉{␊ |
381 | ␉␉return patch_bcm_kext(plist, plistbuffer, start);␊ |
382 | ␊ |
383 | ␉}␊ |
384 | ␉else if(patch_hda_codec && strcmp(bundleID, "com.apple.driver.AppleHDA") == 0)␊ |
385 | ␉{␊ |
386 | ␉␉//return patch_hda_kext(plist, plistbuffer, start);␊ |
387 | ␊ |
388 | ␉}␊ |
389 | ␉/*␊ |
390 | ␉else if(patch_hda_codec && strcmp(bundleID, "com.apple.driver.AppleHDAController") == 0)␊ |
391 | ␉{␊ |
392 | ␉␉return patch_hda_controller(plist, plistbuffer, start);␊ |
393 | ␊ |
394 | ␉}␊ |
395 | ␉*/␊ |
396 | ␉return false;␊ |
397 | }␊ |
398 | ␊ |
399 | void KextPatcher_hook(void* arg1, void* arg2, void* arg3, void* arg4)␊ |
400 | {␊ |
401 | ␉pci_dt_t* current = arg1;␊ |
402 | ␉if(current)␊ |
403 | ␉{␊ |
404 | ␉␉switch(current->class_id)␊ |
405 | ␉␉{␊ |
406 | ␉␉␉case PCI_CLASS_DISPLAY_VGA:␊ |
407 | ␉␉␉␉if(current->vendor_id == 0x8086 && ␊ |
408 | ␉␉␉␉ (␊ |
409 | ␉␉␉␉␉current->device_id == 0x27AE ||␊ |
410 | ␉␉␉␉␉current->device_id == 0xA001 ||␊ |
411 | ␉␉␉␉␉current->device_id == 0xA002 ||␊ |
412 | ␉␉␉␉␉current->device_id == 0xA011 ||␊ |
413 | ␉␉␉␉␉current->device_id == 0xA012␊ |
414 | ␊ |
415 | ␉␉␉␉␉)␊ |
416 | ␉␉␉␉ )␊ |
417 | ␉␉␉␉{␊ |
418 | ␉␉␉␉␉patch_gma_deviceid = current->device_id;␊ |
419 | ␉␉␉␉}␊ |
420 | ␉␉␉␉break;␊ |
421 | ␉␉␉␉␊ |
422 | ␉␉␉case PCI_CLASS_NETWORK_OTHER:␊ |
423 | ␉␉␉␉␊ |
424 | ␉␉␉␉// Patch BCM43xx␊ |
425 | ␉␉␉␉if(current->vendor_id == 0x14E4 && ((current->device_id & 0xFF00) == 0x4300))␊ |
426 | ␉␉␉␉{␊ |
427 | ␉␉␉␉␉patch_bcm_deviceid = current->device_id;␊ |
428 | ␉␉␉␉}␊ |
429 | ␉␉␉␉break;␊ |
430 | ␉␉}␊ |
431 | ␉}␊ |
432 | }␊ |
433 | ␊ |
434 | ␊ |
435 | bool patch_hda_controller(TagPtr plist, char* plistbuffer, void* start)␊ |
436 | {␊ |
437 | ␉return false;␊ |
438 | ␉// change the PCI class code to match to. Note: A LegacyHDA plist should do this on it's own␊ |
439 | ␉// As such, it's disabled␊ |
440 | ␊ |
441 | ␉// TODO: read class code␊ |
442 | ␉TagPtr personality;␊ |
443 | ␉personality =␉␉XMLCastDict(XMLGetProperty(plist, kPropIOKitPersonalities));␊ |
444 | ␉personality =␉␉XMLGetProperty(personality, (const char*)"BuiltInHDA");␉␊ |
445 | ␉TagPtr match_class =XMLCastArray(XMLGetProperty(personality, (const char*)"IOPCIClassMatch"));␊ |
446 | ␉␊ |
447 | ␉␊ |
448 | ␉char* new_str = malloc(strlen("0xXXXX000&0xFFFE0000")+1);␊ |
449 | ␉sprintf(new_str, "0x04030000&0xFFFE0000"); // todo, pass in actual class id␊ |
450 | ␉␊ |
451 | ␉␊ |
452 | ␉char* orig_string = "0x04020000&0xFFFE0000"; //XMLCastString(match_class);␊ |
453 | ␉␊ |
454 | ␉DBG("Attemting to replace '%s' with '%s'\n", orig_string, new_str);␊ |
455 | ␉␊ |
456 | ␉// TODO: verify string doesn't exist first.␊ |
457 | ␉␊ |
458 | ␉replace_string(orig_string, new_str, plistbuffer + XMLCastStringOffset(match_class), 10240);␊ |
459 | ␉␊ |
460 | ␉return true;␊ |
461 | ␉␊ |
462 | }␊ |
463 | ␊ |
464 | ␊ |
465 | bool patch_hda_kext(TagPtr plist, char* plistbuffer, void* start)␊ |
466 | {␊ |
467 | ␉uint16_t find_codec = 0;␊ |
468 | ␉int full_size, compressed_size, executable_offset;␊ |
469 | ␉void* compressed_data;␊ |
470 | ␉mkext2_file_entry* kext;␊ |
471 | ␉int zlib_result;␊ |
472 | ␉z_stream zstream;␊ |
473 | ␉bool zstream_inited = false;␊ |
474 | ␊ |
475 | ␉switch(0xFF00 & patch_hda_codec)␊ |
476 | ␉{␊ |
477 | ␉␉case 0x0200:␊ |
478 | ␉␉␉find_codec = 0x0262;␊ |
479 | ␉␉␉break;␊ |
480 | ␊ |
481 | ␉␉case 0x0800:␊ |
482 | ␉␉␉find_codec = 0x0885;␊ |
483 | ␉␉␉break;␊ |
484 | ␉␉␉␊ |
485 | ␉␉case 0x0600:␉// specificaly the 662␊ |
486 | ␉␉␉find_codec = 0x0885;␊ |
487 | ␉␉␉break;␊ |
488 | ␉}␊ |
489 | ␉␊ |
490 | ␉if(!find_codec) return false;␉// notify caller that we aren't patching the kext␊ |
491 | ␊ |
492 | ␉␉␊ |
493 | ␉executable_offset = XMLCastInteger(XMLGetProperty(plist, kMKEXTExecutableKey));␊ |
494 | ␉kext = (void*)((char*)start + executable_offset);␊ |
495 | ␊ |
496 | ␉full_size = MKEXT2_GET_ENTRY_FULLSIZE(kext);␊ |
497 | ␉compressed_size = MKEXT2_GET_ENTRY_COMPSIZE(kext);␊ |
498 | ␉compressed_data = MKEXT2_GET_ENTRY_DATA(kext);␉␊ |
499 | ␉executable_offset = XMLCastInteger(XMLGetProperty(plist, kMKEXTExecutableKey));␊ |
500 | ␉␊ |
501 | ␉␊ |
502 | ␉char* executable = malloc(full_size);␊ |
503 | ␉␊ |
504 | ␉bzero(&zstream, sizeof(zstream));␉␉␊ |
505 | ␉zstream.next_in = (UInt8*)compressed_data;␊ |
506 | ␉zstream.avail_in = compressed_size;␊ |
507 | ␉␊ |
508 | ␉zstream.next_out = (UInt8*)executable;␊ |
509 | ␉zstream.avail_out = full_size;␊ |
510 | ␉␊ |
511 | ␉zstream.zalloc = z_alloc;␊ |
512 | ␉zstream.zfree = z_free;␊ |
513 | ␉␊ |
514 | ␉zlib_result = inflateInit(&zstream);␊ |
515 | ␉if (Z_OK != zlib_result)␊ |
516 | ␉{␊ |
517 | ␉␉printf("ZLIB Inflate Error: %s\n", zstream.msg);␊ |
518 | ␉␉getc();␊ |
519 | ␉}␊ |
520 | ␉else ␊ |
521 | ␉{␊ |
522 | ␉␉zstream_inited = true;␊ |
523 | ␉}␊ |
524 | ␉␊ |
525 | ␉␊ |
526 | ␉zlib_result = inflate(&zstream, Z_FINISH);␊ |
527 | ␉␊ |
528 | ␉DBG("Inflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, full_size);␊ |
529 | ␉␉␊ |
530 | ␉replace_word(0x10EC | (find_codec << 16), 0xE10EC | (patch_hda_codec << 16), executable, zstream.total_out);␊ |
531 | ␉␊ |
532 | ␉if (zstream_inited) inflateEnd(&zstream);␊ |
533 | ␉␊ |
534 | ␉␊ |
535 | ␉zstream.next_in = (UInt8*)executable;␊ |
536 | ␉zstream.next_out = (UInt8*)compressed_data;␊ |
537 | ␉␊ |
538 | ␉zstream.avail_in = full_size;␊ |
539 | ␉zstream.avail_out = compressed_size;␊ |
540 | ␉zstream.zalloc = Z_NULL;␊ |
541 | ␉zstream.zfree = Z_NULL;␊ |
542 | ␉zstream.opaque = Z_NULL;␊ |
543 | ␉␊ |
544 | ␉␊ |
545 | ␉␊ |
546 | ␉// Recompress the eecutable␊ |
547 | ␉zlib_result = deflateInit2(&zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,15, 8 /* memLevel */, Z_DEFAULT_STRATEGY);␊ |
548 | ␉if (Z_OK != zlib_result) {␊ |
549 | ␉␉printf("ZLIB Deflate Error: %s\n", zstream.msg);␊ |
550 | ␉␉getc();␊ |
551 | ␉}␊ |
552 | ␉else ␊ |
553 | ␉{␊ |
554 | ␉␉zstream_inited = true;␊ |
555 | ␉}␊ |
556 | ␉␊ |
557 | ␉zlib_result = deflate(&zstream, Z_FINISH);␊ |
558 | ␉␊ |
559 | ␉if (zlib_result == Z_STREAM_END)␊ |
560 | ␉{␊ |
561 | ␉␉DBG("Deflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, full_size);␊ |
562 | ␉} ␊ |
563 | ␉else if (zlib_result == Z_OK)␊ |
564 | ␉{␊ |
565 | ␉␉/* deflate filled output buffer, meaning the data doesn't compress.␊ |
566 | ␉␉ */␊ |
567 | ␉␉DBG("Deflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, full_size);␊ |
568 | ␉␉␊ |
569 | ␉} ␊ |
570 | ␉else if (zlib_result != Z_STREAM_ERROR)␊ |
571 | ␉{␊ |
572 | ␉␉printf("ZLIB Deflate Error: %s\n", zstream.msg);␊ |
573 | ␉␉getc();␊ |
574 | ␉}␊ |
575 | ␉␊ |
576 | ␉if (zstream_inited) deflateEnd(&zstream);␊ |
577 | ␉␊ |
578 | ␉free(executable);␊ |
579 | ␉␊ |
580 | ␉return true;␉␊ |
581 | ␊ |
582 | }␊ |
583 | ␊ |
584 | bool patch_bcm_kext(TagPtr plist, char* plistbuffer, void* start)␊ |
585 | {␊ |
586 | ␉TagPtr personality;␊ |
587 | ␉personality =␉␉XMLCastDict(XMLGetProperty(plist, kPropIOKitPersonalities));␊ |
588 | ␉personality =␉␉XMLGetProperty(personality, (const char*)"Broadcom 802.11 PCI");␉␊ |
589 | ␉TagPtr match_names =XMLCastArray(XMLGetProperty(personality, (const char*)"IONameMatch"));␊ |
590 | ␊ |
591 | ␉␊ |
592 | ␉char* new_str = malloc(strlen("pci14e4,xxxx")+1);␊ |
593 | ␉sprintf(new_str, "pci14e4,%02x", patch_bcm_deviceid);␊ |
594 | ␊ |
595 | ␉// Check to see if we *really* need to modify the plist, if not, return false␊ |
596 | ␉// so that *if* this were going ot be the only modified kext, the repacking code␊ |
597 | ␉// won't need to be executed.␊ |
598 | ␉int count = XMLTagCount(match_names);␊ |
599 | ␉while(count)␊ |
600 | ␉{␊ |
601 | ␉␉count--;␊ |
602 | ␉␉TagPtr replace =␉XMLGetElement(match_names, count);␉// Modify the second entry␊ |
603 | ␉␉char* orig_string = XMLCastString(replace);␊ |
604 | ␉␉if(strcmp(orig_string, new_str) == 0) return false;␊ |
605 | ␉}␊ |
606 | ␊ |
607 | ␉␊ |
608 | ␉TagPtr replace =␉XMLGetElement(match_names, 1);␉// Modify the second entry␊ |
609 | ␉char* orig_string = XMLCastString(replace);␊ |
610 | ␉␊ |
611 | ␉␊ |
612 | ␉// TODO: verify string doesn't exist first.␊ |
613 | ␉␊ |
614 | ␉replace_string(orig_string, new_str, plistbuffer + XMLCastStringOffset(replace), 10240);␊ |
615 | ␊ |
616 | ␉return true;␊ |
617 | }␊ |
618 | ␊ |
619 | bool patch_gma_kexts(TagPtr plist, char* plistbuffer, void* start)␊ |
620 | {␊ |
621 | ␉// TODO: clean up this function / split into two / etc␊ |
622 | ␉int exeutable_offset, full_size, compressed_size;␊ |
623 | ␉TagPtr personality;␊ |
624 | ␉long offset;␊ |
625 | ␉int zlib_result;␊ |
626 | ␉z_stream zstream;␊ |
627 | ␉bool zstream_inited = false;␊ |
628 | ␉mkext2_file_entry* kext;␊ |
629 | ␉void* compressed_data;␊ |
630 | ␊ |
631 | ␉exeutable_offset = XMLCastInteger(XMLGetProperty(plist, kMKEXTExecutableKey));␊ |
632 | ␉kext = (void*)((char*)start + exeutable_offset);␊ |
633 | ␊ |
634 | ␉full_size = MKEXT2_GET_ENTRY_FULLSIZE(kext);␊ |
635 | ␉compressed_size = MKEXT2_GET_ENTRY_COMPSIZE(kext);␊ |
636 | ␉compressed_data = MKEXT2_GET_ENTRY_DATA(kext);␊ |
637 | ␉␊ |
638 | ␉personality =␉␉XMLCastDict(XMLGetProperty(plist, kPropIOKitPersonalities));␊ |
639 | ␉␊ |
640 | ␉␊ |
641 | ␉␊ |
642 | ␉char* executable = malloc(full_size);␊ |
643 | ␉␊ |
644 | ␉bzero(&zstream, sizeof(zstream));␉␉␊ |
645 | ␉zstream.next_in = (UInt8*)compressed_data;␊ |
646 | ␉zstream.avail_in = compressed_size;␊ |
647 | ␉␊ |
648 | ␉zstream.next_out = (UInt8*)executable;␊ |
649 | ␉zstream.avail_out = full_size;␊ |
650 | ␉␊ |
651 | ␉zstream.zalloc = z_alloc;␊ |
652 | ␉zstream.zfree = z_free;␊ |
653 | ␉␊ |
654 | ␉zlib_result = inflateInit(&zstream);␊ |
655 | ␉if (Z_OK != zlib_result)␊ |
656 | ␉{␊ |
657 | ␉␉printf("ZLIB Inflate Error: %s\n", zstream.msg);␊ |
658 | ␉␉getc();␊ |
659 | ␉}␊ |
660 | ␉else ␊ |
661 | ␉{␊ |
662 | ␉␉zstream_inited = true;␊ |
663 | ␉}␊ |
664 | ␉␊ |
665 | ␉␊ |
666 | ␉zlib_result = inflate(&zstream, Z_FINISH);␊ |
667 | ␉␊ |
668 | ␉DBG("Inflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, full_size);␊ |
669 | ␉␊ |
670 | ␉␊ |
671 | ␉␊ |
672 | ␉if(XMLGetProperty(personality, (const char*)"Intel915"))␊ |
673 | ␉{␊ |
674 | ␉␉personality =␉␉XMLGetProperty(personality, (const char*)"Intel915");␊ |
675 | ␉␉// IOAccelerator kext␊ |
676 | ␉}␊ |
677 | ␉else␊ |
678 | ␉{␊ |
679 | ␉␉personality =␉␉XMLGetProperty(personality, (const char*)"AppleIntelIntegratedFramebuffer");␊ |
680 | ␉␉// Framebuffer Kext␊ |
681 | ␉␉␊ |
682 | ␉␉if((patch_gma_deviceid & 0xFF00) == 0xA000)␉// GMA3150␊ |
683 | ␉␉{␊ |
684 | ␉␉␉// Cursor corruption fix.␊ |
685 | ␉␉␉// This patch changes the cursor address from␊ |
686 | ␉␉␉// a physical address (used in the gma950) to an offset (used in the gma3150).␊ |
687 | ␉␉␉␊ |
688 | ␉␉␉char find_bytes[] = {0x8b, 0x55, 0x08, 0x83, 0xba, 0xb0, 0x00, 0x00, 0x00, 0x01, 0x7e, 0x36, 0x89, 0x04, 0x24, 0xe8, 0x32, 0xbb, 0xff, 0xff};␉// getPhysicalAddress() and more␊ |
689 | ␉␉␉char new_bytes[] = {0xb8, 0x00, 0x00, 0x00, 0x02, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xEB, 0x04, 0x00, 0x00, 0x00, 0x00};␉// jump past getPhysicalAddress binding. NOTE: last six bytes are unusable␊ |
690 | ␉␉␉replace_bytes(find_bytes, sizeof(find_bytes), new_bytes, sizeof(new_bytes), executable, zstream.total_out);␊ |
691 | ␉␉}␊ |
692 | ␉␉␊ |
693 | ␉}␊ |
694 | ␉␊ |
695 | ␉␊ |
696 | #if DEBUG_KEXT_PATCHER␊ |
697 | ␉char* pcimatch =␉XMLCastString(XMLGetProperty(personality, (const char*)"IOPCIPrimaryMatch"));␊ |
698 | #endif␊ |
699 | ␉offset =␉␉XMLCastStringOffset(XMLGetProperty(personality, (const char*)"IOPCIPrimaryMatch"));␊ |
700 | ␉␊ |
701 | ␉char* newstring = malloc(strlen("0x00008086") + 1);␊ |
702 | ␉sprintf(newstring, "0x%04x", 0x8086 | (patch_gma_deviceid << 16));␊ |
703 | ␉␊ |
704 | ␉DBG("Replacing %s with %s\n", "0x00008086", newstring); ␊ |
705 | ␉replace_string("0x27A28086", newstring, plistbuffer + offset, 10240);␊ |
706 | ␉replace_word(0x27A28086, 0x8086 | (patch_gma_deviceid << 16), executable, zstream.total_out);␊ |
707 | ␊ |
708 | ␉␊ |
709 | ␉␊ |
710 | ␉if (zstream_inited) inflateEnd(&zstream);␊ |
711 | ␉␊ |
712 | ␉␊ |
713 | ␉zstream.next_in = (UInt8*)executable;␊ |
714 | ␉zstream.next_out = (UInt8*)compressed_data;␊ |
715 | ␉␊ |
716 | ␉zstream.avail_in = full_size;␊ |
717 | ␉zstream.avail_out = compressed_size;␊ |
718 | ␉zstream.zalloc = Z_NULL;␊ |
719 | ␉zstream.zfree = Z_NULL;␊ |
720 | ␉zstream.opaque = Z_NULL;␊ |
721 | ␉␊ |
722 | ␉␊ |
723 | ␉␊ |
724 | ␉// Recompress the eecutable␊ |
725 | ␉zlib_result = deflateInit2(&zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,15, 8 /* memLevel */, Z_DEFAULT_STRATEGY);␊ |
726 | ␉if (Z_OK != zlib_result) {␊ |
727 | ␉␉printf("ZLIB Deflate Error: %s\n", zstream.msg);␊ |
728 | ␉␉getc();␊ |
729 | ␉}␊ |
730 | ␉else ␊ |
731 | ␉{␊ |
732 | ␉␉zstream_inited = true;␊ |
733 | ␉}␊ |
734 | ␉␊ |
735 | ␉zlib_result = deflate(&zstream, Z_FINISH);␊ |
736 | ␉␊ |
737 | ␉if (zlib_result == Z_STREAM_END)␊ |
738 | ␉{␊ |
739 | ␉␉DBG("Deflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, full_size);␊ |
740 | ␉} ␊ |
741 | ␉else if (zlib_result == Z_OK)␊ |
742 | ␉{␊ |
743 | ␉␉/* deflate filled output buffer, meaning the data doesn't compress.␊ |
744 | ␉␉ */␊ |
745 | ␉␉printf("Deflated result is %d, in: %d bytes, out: %d bytes, full: %d\n", zlib_result, zstream.total_in, zstream.total_out, full_size);␊ |
746 | ␉␉printf("ERROR: Unable to compress patched kext, not enough room.\n");␊ |
747 | ␉␉pause();␊ |
748 | ␉␉␊ |
749 | ␉} ␊ |
750 | ␉else if (zlib_result != Z_STREAM_ERROR)␊ |
751 | ␉{␊ |
752 | ␉␉printf("ZLIB Deflate Error: %s\n", zstream.msg);␊ |
753 | ␉␉getc();␊ |
754 | ␉}␊ |
755 | ␉//kext->compressed_size = MKEXT_SWAP(zstream.total_out);␊ |
756 | ␊ |
757 | ␉␊ |
758 | ␉␊ |
759 | ␉␊ |
760 | ␉if (zstream_inited) deflateEnd(&zstream);␊ |
761 | ␉␊ |
762 | ␉free(executable);␊ |
763 | ␉␊ |
764 | ␉return true;␉␊ |
765 | }␊ |
766 | ␊ |
767 | int chartohex(char c)␊ |
768 | {␊ |
769 | ␉if(c <= '9' && c >= '0')␊ |
770 | ␉{␊ |
771 | ␉␉return c - '0';␉// c is between 0 and 9␊ |
772 | ␉}␊ |
773 | ␉else if(c <= 'F' && c >= 'A')␊ |
774 | ␉{␊ |
775 | ␉␉return c - 'A' + 10; // c = 10 - 15;␊ |
776 | ␉}␊ |
777 | ␉else if(c <= 'f' && c >= 'a')␊ |
778 | ␉{␊ |
779 | ␉␉return c - 'a' + 10; // c = 10 - 15;␊ |
780 | ␉}␊ |
781 | ␉return 0;␊ |
782 | } |