Root/
Source at commit 502 created 13 years 10 months ago. By meklort, Typo fix, current status of kernel patcher. NOTE: 64bit doesn't work fully yet. This is just a placeholder untill I sync with trunk. | |
---|---|
1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the 'License'). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * drivers.c - Driver Loading Functions.␊ |
26 | *␊ |
27 | * Copyright (c) 2000 Apple Computer, Inc.␊ |
28 | *␊ |
29 | * DRI: Josh de Cesare␊ |
30 | */␊ |
31 | ␊ |
32 | #include <mach-o/fat.h>␊ |
33 | #include <libkern/OSByteOrder.h>␊ |
34 | #include <mach/machine.h>␊ |
35 | ␊ |
36 | #include "sl.h"␊ |
37 | #include "boot.h"␊ |
38 | #include "bootstruct.h"␊ |
39 | #include "xml.h"␊ |
40 | #include "ramdisk.h"␊ |
41 | #include "modules.h"␊ |
42 | ␊ |
43 | ␊ |
44 | extern char gMacOSVersion;␊ |
45 | extern int␉recoveryMode;␊ |
46 | ␊ |
47 | ␊ |
48 | struct Module { ␊ |
49 | struct Module *nextModule;␊ |
50 | long willLoad;␊ |
51 | TagPtr dict;␊ |
52 | char *plistAddr;␊ |
53 | long plistLength;␊ |
54 | char *executablePath;␊ |
55 | char *bundlePath;␊ |
56 | long bundlePathLength;␊ |
57 | };␊ |
58 | typedef struct Module Module, *ModulePtr;␊ |
59 | ␊ |
60 | struct DriverInfo {␊ |
61 | char *plistAddr;␊ |
62 | long plistLength;␊ |
63 | void *executableAddr;␊ |
64 | long executableLength;␊ |
65 | void *bundlePathAddr;␊ |
66 | long bundlePathLength;␊ |
67 | };␊ |
68 | typedef struct DriverInfo DriverInfo, *DriverInfoPtr;␊ |
69 | ␊ |
70 | #define kDriverPackageSignature1 'MKXT'␊ |
71 | #define kDriverPackageSignature2 'MOSX'␊ |
72 | ␊ |
73 | struct DriversPackage {␊ |
74 | unsigned long signature1;␊ |
75 | unsigned long signature2;␊ |
76 | unsigned long length;␊ |
77 | unsigned long alder32;␊ |
78 | unsigned long version;␊ |
79 | unsigned long numDrivers;␊ |
80 | unsigned long reserved1;␊ |
81 | unsigned long reserved2;␊ |
82 | };␊ |
83 | typedef struct DriversPackage DriversPackage;␊ |
84 | ␊ |
85 | enum {␊ |
86 | kCFBundleType2,␊ |
87 | kCFBundleType3␊ |
88 | };␊ |
89 | ␊ |
90 | long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);␊ |
91 | ␊ |
92 | static unsigned long Alder32( unsigned char * buffer, long length );␊ |
93 | ␊ |
94 | static long FileLoadDrivers(char *dirSpec, long plugin);␊ |
95 | static long NetLoadDrivers(char *dirSpec);␊ |
96 | static long LoadDriverMKext(char *fileSpec);␊ |
97 | static long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
98 | static long LoadMatchedModules(void);␊ |
99 | static long MatchPersonalities(void);␊ |
100 | static long MatchLibraries(void);␊ |
101 | #ifdef NOTDEF␊ |
102 | static ModulePtr FindModule(char *name);␊ |
103 | static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);␊ |
104 | #endif␊ |
105 | static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);␊ |
106 | static long InitDriverSupport(void);␊ |
107 | ␊ |
108 | static ModulePtr gModuleHead, gModuleTail;␊ |
109 | static TagPtr gPersonalityHead, gPersonalityTail;␊ |
110 | static char * gExtensionsSpec;␊ |
111 | static char * gDriverSpec;␊ |
112 | static char * gFileSpec;␊ |
113 | static char * gTempSpec;␊ |
114 | static char * gFileName;␊ |
115 | ␊ |
116 | static unsigned long␊ |
117 | Alder32( unsigned char * buffer, long length )␊ |
118 | {␊ |
119 | long cnt;␊ |
120 | unsigned long result, lowHalf, highHalf;␊ |
121 | ␊ |
122 | lowHalf = 1;␊ |
123 | highHalf = 0;␊ |
124 | ␊ |
125 | ␉for ( cnt = 0; cnt < length; cnt++ )␊ |
126 | {␊ |
127 | if ((cnt % 5000) == 0)␊ |
128 | {␊ |
129 | lowHalf %= 65521L;␊ |
130 | highHalf %= 65521L;␊ |
131 | }␊ |
132 | ␊ |
133 | lowHalf += buffer[cnt];␊ |
134 | highHalf += lowHalf;␊ |
135 | }␊ |
136 | ␊ |
137 | ␉lowHalf %= 65521L;␊ |
138 | ␉highHalf %= 65521L;␊ |
139 | ␊ |
140 | ␉result = (highHalf << 16) | lowHalf;␊ |
141 | ␊ |
142 | ␉return result;␊ |
143 | }␊ |
144 | ␊ |
145 | ␊ |
146 | //==========================================================================␊ |
147 | // InitDriverSupport␊ |
148 | ␊ |
149 | static long␊ |
150 | InitDriverSupport( void )␊ |
151 | {␊ |
152 | gExtensionsSpec = malloc( 4096 );␊ |
153 | gDriverSpec = malloc( 4096 );␊ |
154 | gFileSpec = malloc( 4096 );␊ |
155 | gTempSpec = malloc( 4096 );␊ |
156 | gFileName = malloc( 4096 );␊ |
157 | ␊ |
158 | if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName )␊ |
159 | stop("InitDriverSupport error");␊ |
160 | ␊ |
161 | return 0;␊ |
162 | }␊ |
163 | ␊ |
164 | //==========================================================================␊ |
165 | // LoadDrivers␊ |
166 | ␊ |
167 | long LoadDrivers( char * dirSpec )␊ |
168 | {␊ |
169 | char dirSpecExtra[1024];␊ |
170 | ␊ |
171 | if ( InitDriverSupport() != 0 )␊ |
172 | return 0;␊ |
173 | ␊ |
174 | // Load extra drivers if a hook has been installed.␊ |
175 | if (LoadExtraDrivers_p != NULL)␊ |
176 | {␊ |
177 | (*LoadExtraDrivers_p)(&FileLoadDrivers);␊ |
178 | }␊ |
179 | ␊ |
180 | if ( gBootFileType == kNetworkDeviceType )␊ |
181 | {␊ |
182 | if (NetLoadDrivers(dirSpec) != 0) {␊ |
183 | error("Could not load drivers from the network\n");␊ |
184 | return -1;␊ |
185 | }␊ |
186 | }␊ |
187 | else if ( gBootFileType == kBlockDeviceType )␊ |
188 | {␊ |
189 | // First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).␊ |
190 | ␉␉if(recoveryMode) ␊ |
191 | ␉␉{␊ |
192 | ␉␉␉verbose("Loading recovery extensions\n");␊ |
193 | ␊ |
194 | ␉␉␉const char* recoveryFolder;␊ |
195 | ␉␉␉int len;␊ |
196 | ␉␉␉if (getValueForKey(kWakeImage, &recoveryFolder, &len, &bootInfo->bootConfig))␊ |
197 | ␉␉␉{␊ |
198 | ␉␉␉␉sprintf(dirSpecExtra, "/Extra/%s/", &recoveryFolder);␊ |
199 | ␉␉␉␉if(FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
200 | ␉␉␉␉{␊ |
201 | ␉␉␉␉␉verbose("Unable to locate recovery extensions\n");␊ |
202 | ␉␉␉␉}␊ |
203 | ␉␉␉}␊ |
204 | ␉␉␉else {␊ |
205 | ␉␉␉␉verbose("Unable to locate recovery extensions\n");␊ |
206 | ␉␉␉}␊ |
207 | ␊ |
208 | ␉␉}␊ |
209 | ␉␉else␊ |
210 | ␉␉{␊ |
211 | ␉␉␉if (gRAMDiskVolume && !gRAMDiskBTAliased)␊ |
212 | ␉␉␉{␊ |
213 | ␉␉␉␉strcpy(dirSpecExtra, "rd(0,0)/Extra/");␊ |
214 | ␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
215 | ␉␉␉}␊ |
216 | ␉␉␉␊ |
217 | ␉␉␉// Next try to load Extra extensions from the selected root partition.␊ |
218 | ␉␉␉strcpy(dirSpecExtra, "/Extra/");␊ |
219 | ␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
220 | ␉␉␉{␊ |
221 | ␉␉␉␉// If failed, then try to load Extra extensions from the boot partition␊ |
222 | ␉␉␉␉// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.␊ |
223 | ␉␉␉␉if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)␊ |
224 | ␉␉␉␉␉|| (gRAMDiskVolume && gRAMDiskBTAliased) )␊ |
225 | ␉␉␉␉{␊ |
226 | ␉␉␉␉␉// First try a specfic OS version folder ie 10.5␊ |
227 | ␉␉␉␉␉sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);␊ |
228 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
229 | ␉␉␉␉␉{␉␊ |
230 | ␉␉␉␉␉␉// Next we'll try the base␊ |
231 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "bt(0,0)/Extra/");␊ |
232 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
233 | ␉␉␉␉␉}␊ |
234 | ␉␉␉␉}␊ |
235 | ␉␉␉}␊ |
236 | ␉␉␉␊ |
237 | ␉␉}␊ |
238 | ␊ |
239 | // Also try to load Extensions from boot helper partitions.␊ |
240 | strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
241 | if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
242 | {␊ |
243 | strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
244 | if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
245 | {␊ |
246 | strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
247 | FileLoadDrivers(dirSpecExtra, 0);␊ |
248 | }␊ |
249 | }␊ |
250 | ␊ |
251 | if (gMKextName[0] != '\0')␊ |
252 | {␊ |
253 | verbose("LoadDrivers: Loading from [%s]\n", gMKextName);␊ |
254 | if ( LoadDriverMKext(gMKextName) != 0 )␊ |
255 | {␊ |
256 | error("Could not load %s\n", gMKextName);␊ |
257 | return -1;␊ |
258 | }␊ |
259 | }␊ |
260 | else␊ |
261 | {␊ |
262 | strcpy(gExtensionsSpec, dirSpec);␊ |
263 | strcat(gExtensionsSpec, "System/Library/");␊ |
264 | FileLoadDrivers(gExtensionsSpec, 0);␊ |
265 | }␊ |
266 | }␊ |
267 | else␊ |
268 | {␊ |
269 | return 0;␊ |
270 | }␊ |
271 | ␊ |
272 | MatchPersonalities();␊ |
273 | ␊ |
274 | MatchLibraries();␊ |
275 | ␊ |
276 | LoadMatchedModules();␊ |
277 | ␊ |
278 | return 0;␊ |
279 | }␊ |
280 | ␊ |
281 | //==========================================================================␊ |
282 | // FileLoadDrivers␊ |
283 | ␊ |
284 | static long␊ |
285 | FileLoadDrivers( char * dirSpec, long plugin )␊ |
286 | {␊ |
287 | long ret, length, index, flags, time, bundleType;␊ |
288 | long result = -1;␊ |
289 | const char * name;␊ |
290 | ␊ |
291 | if ( !plugin )␊ |
292 | {␊ |
293 | long time2;␊ |
294 | ␊ |
295 | // TODO: refactor this part of code.␊ |
296 | char altDirSpec[4500];␊ |
297 | sprintf (altDirSpec,"%sCaches/com.apple.kext.caches/Startup/",dirSpec);␊ |
298 | ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);␊ |
299 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
300 | {␊ |
301 | ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
302 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeDirectory) ||␊ |
303 | (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
304 | {␊ |
305 | sprintf(gDriverSpec, "%sExtensions.mkext", altDirSpec);␊ |
306 | verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
307 | if (LoadDriverMKext(gDriverSpec) == 0) return 0;␊ |
308 | }␊ |
309 | }␊ |
310 | //␊ |
311 | ␊ |
312 | ret = GetFileInfo(dirSpec, "Extensions.mkext", &flags, &time);␊ |
313 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
314 | {␊ |
315 | ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
316 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeDirectory) ||␊ |
317 | (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
318 | {␊ |
319 | sprintf(gDriverSpec, "%sExtensions.mkext", dirSpec);␊ |
320 | verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
321 | if (LoadDriverMKext(gDriverSpec) == 0) return 0;␊ |
322 | }␊ |
323 | }␊ |
324 | ␊ |
325 | strcat(dirSpec, "Extensions");␊ |
326 | }␊ |
327 | ␊ |
328 | index = 0;␊ |
329 | while (1) {␊ |
330 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
331 | if (ret == -1) break;␊ |
332 | ␊ |
333 | // Make sure this is a directory.␊ |
334 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
335 | ␊ |
336 | // Make sure this is a kext.␊ |
337 | length = strlen(name);␊ |
338 | if (strcmp(name + length - 5, ".kext")) continue;␊ |
339 | ␊ |
340 | // Save the file name.␊ |
341 | strcpy(gFileName, name);␊ |
342 | ␊ |
343 | // Determine the bundle type.␊ |
344 | sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
345 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
346 | if (ret == 0) bundleType = kCFBundleType2;␊ |
347 | else bundleType = kCFBundleType3;␊ |
348 | ␊ |
349 | if (!plugin)␊ |
350 | sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
351 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
352 | ␊ |
353 | ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
354 | ␊ |
355 | if (result != 0)␊ |
356 | result = ret;␊ |
357 | ␊ |
358 | if (!plugin) ␊ |
359 | result = FileLoadDrivers(gDriverSpec, 1);␊ |
360 | }␊ |
361 | ␊ |
362 | return result;␊ |
363 | }␊ |
364 | ␊ |
365 | //==========================================================================␊ |
366 | // ␊ |
367 | ␊ |
368 | static long␊ |
369 | NetLoadDrivers( char * dirSpec )␊ |
370 | {␊ |
371 | long tries;␊ |
372 | ␊ |
373 | #if NODEF␊ |
374 | long cnt;␊ |
375 | ␊ |
376 | // Get the name of the kernel␊ |
377 | cnt = strlen(gBootFile);␊ |
378 | while (cnt--) {␊ |
379 | if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {␊ |
380 | cnt++;␊ |
381 | break;␊ |
382 | }␊ |
383 | }␊ |
384 | #endif␊ |
385 | ␊ |
386 | // INTEL modification␊ |
387 | sprintf(gDriverSpec, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
388 | ␊ |
389 | verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
390 | ␊ |
391 | tries = 3;␊ |
392 | while (tries--)␊ |
393 | {␊ |
394 | if (LoadDriverMKext(gDriverSpec) == 0) break;␊ |
395 | }␊ |
396 | if (tries == -1) return -1;␊ |
397 | ␊ |
398 | return 0;␊ |
399 | }␊ |
400 | ␊ |
401 | //==========================================================================␊ |
402 | // loadDriverMKext␊ |
403 | ␊ |
404 | static long␊ |
405 | LoadDriverMKext( char * fileSpec )␊ |
406 | {␊ |
407 | unsigned long driversAddr, driversLength;␊ |
408 | long length;␊ |
409 | char segName[32];␊ |
410 | DriversPackage * package;␊ |
411 | ␊ |
412 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
413 | ␊ |
414 | // Load the MKext.␊ |
415 | length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
416 | if (length < sizeof (DriversPackage)) return -1;␊ |
417 | ␊ |
418 | ␉// call hook to notify modules that the mkext has been loaded␊ |
419 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) length, NULL);␊ |
420 | ␊ |
421 | ␉␊ |
422 | // Verify the MKext.␊ |
423 | if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
424 | ( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
425 | ( GetPackageElement(length) > kLoadSize ) ||␊ |
426 | ( GetPackageElement(alder32) !=␊ |
427 | Alder32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
428 | {␊ |
429 | return -1;␊ |
430 | }␊ |
431 | ␉␊ |
432 | ␊ |
433 | // Make space for the MKext.␊ |
434 | driversLength = GetPackageElement(length);␊ |
435 | driversAddr = AllocateKernelMemory(driversLength);␊ |
436 | ␊ |
437 | // Copy the MKext.␊ |
438 | memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
439 | ␊ |
440 | // Add the MKext to the memory map.␊ |
441 | sprintf(segName, "DriversPackage-%lx", driversAddr);␊ |
442 | AllocateMemoryRange(segName, driversAddr, driversLength,␊ |
443 | kBootDriverTypeMKEXT);␊ |
444 | ␊ |
445 | return 0;␊ |
446 | }␊ |
447 | ␊ |
448 | //==========================================================================␊ |
449 | // LoadDriverPList␊ |
450 | ␊ |
451 | static long␊ |
452 | LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
453 | {␊ |
454 | long length, executablePathLength, bundlePathLength;␊ |
455 | ModulePtr module;␊ |
456 | TagPtr personalities;␊ |
457 | char * buffer = 0;␊ |
458 | char * tmpExecutablePath = 0;␊ |
459 | char * tmpBundlePath = 0;␊ |
460 | long ret = -1;␊ |
461 | ␊ |
462 | do {␊ |
463 | // Save the driver path.␊ |
464 | ␊ |
465 | sprintf(gFileSpec, "%s/%s/%s", dirSpec, name,␊ |
466 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
467 | executablePathLength = strlen(gFileSpec) + 1;␊ |
468 | ␊ |
469 | tmpExecutablePath = malloc(executablePathLength);␊ |
470 | if (tmpExecutablePath == 0) break;␊ |
471 | ␊ |
472 | strcpy(tmpExecutablePath, gFileSpec);␊ |
473 | ␊ |
474 | sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
475 | bundlePathLength = strlen(gFileSpec) + 1;␊ |
476 | ␊ |
477 | tmpBundlePath = malloc(bundlePathLength);␊ |
478 | if (tmpBundlePath == 0) break;␊ |
479 | ␊ |
480 | strcpy(tmpBundlePath, gFileSpec);␊ |
481 | ␊ |
482 | // Construct the file spec to the plist, then load it.␊ |
483 | ␊ |
484 | sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
485 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
486 | ␊ |
487 | length = LoadFile(gFileSpec);␊ |
488 | if (length == -1) break;␊ |
489 | ␊ |
490 | length = length + 1;␊ |
491 | buffer = malloc(length);␊ |
492 | if (buffer == 0) break;␊ |
493 | ␊ |
494 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
495 | ␊ |
496 | // Parse the plist.␊ |
497 | ␊ |
498 | ret = ParseXML(buffer, &module, &personalities);␊ |
499 | if (ret != 0) { break; }␊ |
500 | ␊ |
501 | // Allocate memory for the driver path and the plist.␊ |
502 | ␊ |
503 | module->executablePath = tmpExecutablePath;␊ |
504 | module->bundlePath = tmpBundlePath;␊ |
505 | module->bundlePathLength = bundlePathLength;␊ |
506 | module->plistAddr = malloc(length);␊ |
507 | ␊ |
508 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
509 | break;␊ |
510 | ␊ |
511 | // Save the driver path in the module.␊ |
512 | //strcpy(module->driverPath, tmpDriverPath);␊ |
513 | tmpExecutablePath = 0;␊ |
514 | tmpBundlePath = 0;␊ |
515 | ␊ |
516 | // Add the plist to the module.␊ |
517 | ␊ |
518 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
519 | module->plistLength = length;␊ |
520 | ␊ |
521 | // Add the module to the end of the module list.␊ |
522 | ␊ |
523 | if (gModuleHead == 0)␊ |
524 | gModuleHead = module;␊ |
525 | else␊ |
526 | gModuleTail->nextModule = module;␊ |
527 | gModuleTail = module;␊ |
528 | ␊ |
529 | // Add the persionalities to the personality list.␊ |
530 | ␊ |
531 | if (personalities) personalities = personalities->tag;␊ |
532 | while (personalities != 0)␊ |
533 | {␊ |
534 | if (gPersonalityHead == 0)␊ |
535 | gPersonalityHead = personalities->tag;␊ |
536 | else␊ |
537 | gPersonalityTail->tagNext = personalities->tag;␊ |
538 | ␊ |
539 | gPersonalityTail = personalities->tag;␊ |
540 | personalities = personalities->tagNext;␊ |
541 | }␊ |
542 | ␊ |
543 | ret = 0;␊ |
544 | }␊ |
545 | while (0);␊ |
546 | ␊ |
547 | if ( buffer ) free( buffer );␊ |
548 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
549 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
550 | ␊ |
551 | return ret;␊ |
552 | }␊ |
553 | ␊ |
554 | ␊ |
555 | //==========================================================================␊ |
556 | // LoadMatchedModules␊ |
557 | ␊ |
558 | static long␊ |
559 | LoadMatchedModules( void )␊ |
560 | {␊ |
561 | TagPtr prop;␊ |
562 | ModulePtr module;␊ |
563 | char *fileName, segName[32];␊ |
564 | DriverInfoPtr driver;␊ |
565 | long length, driverAddr, driverLength;␊ |
566 | void *executableAddr = 0;␊ |
567 | ␊ |
568 | ␊ |
569 | module = gModuleHead;␊ |
570 | ␊ |
571 | while (module != 0)␊ |
572 | {␊ |
573 | if (module->willLoad)␊ |
574 | {␊ |
575 | prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
576 | ␊ |
577 | if (prop != 0)␊ |
578 | {␊ |
579 | fileName = prop->string;␊ |
580 | sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
581 | length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
582 | ␉␉␉␉if (length == 0)␊ |
583 | ␉␉␉␉{␊ |
584 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
585 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
586 | ␉␉␉␉}␊ |
587 | //printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getc();␊ |
588 | }␊ |
589 | else␊ |
590 | length = 0;␊ |
591 | ␊ |
592 | if (length != -1)␊ |
593 | {␊ |
594 | ␉␉//driverModuleAddr = (void *)kLoadAddr;␊ |
595 | //if (length != 0)␊ |
596 | //{␊ |
597 | ␉␉// ThinFatFile(&driverModuleAddr, &length);␊ |
598 | ␉␉//}␊ |
599 | ␊ |
600 | // Make make in the image area.␊ |
601 | ␉␉␉␉␊ |
602 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
603 | ␊ |
604 | driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
605 | driverAddr = AllocateKernelMemory(driverLength);␊ |
606 | ␊ |
607 | // Set up the DriverInfo.␊ |
608 | driver = (DriverInfoPtr)driverAddr;␊ |
609 | driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
610 | driver->plistLength = module->plistLength;␊ |
611 | if (length != 0)␊ |
612 | {␊ |
613 | driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
614 | ␉␉␉␉␉ module->plistLength);␊ |
615 | driver->executableLength = length;␊ |
616 | }␊ |
617 | else␊ |
618 | {␊ |
619 | driver->executableAddr = 0;␊ |
620 | driver->executableLength = 0;␊ |
621 | }␊ |
622 | driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
623 | ␉␉␉␉ module->plistLength + driver->executableLength);␊ |
624 | driver->bundlePathLength = module->bundlePathLength;␊ |
625 | ␊ |
626 | // Save the plist, module and bundle.␊ |
627 | strcpy(driver->plistAddr, module->plistAddr);␊ |
628 | if (length != 0)␊ |
629 | {␊ |
630 | memcpy(driver->executableAddr, executableAddr, length);␊ |
631 | }␊ |
632 | strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
633 | ␊ |
634 | // Add an entry to the memory map.␊ |
635 | sprintf(segName, "Driver-%lx", (unsigned long)driver);␊ |
636 | AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
637 | kBootDriverTypeKEXT);␊ |
638 | }␊ |
639 | }␊ |
640 | module = module->nextModule;␊ |
641 | }␊ |
642 | ␊ |
643 | return 0;␊ |
644 | }␊ |
645 | ␊ |
646 | //==========================================================================␊ |
647 | // MatchPersonalities␊ |
648 | ␊ |
649 | static long␊ |
650 | MatchPersonalities( void )␊ |
651 | {␊ |
652 | /* IONameMatch support not implemented */␊ |
653 | return 0;␊ |
654 | }␊ |
655 | ␊ |
656 | //==========================================================================␊ |
657 | // MatchLibraries␊ |
658 | ␊ |
659 | static long␊ |
660 | MatchLibraries( void )␊ |
661 | {␊ |
662 | TagPtr prop, prop2;␊ |
663 | ModulePtr module, module2;␊ |
664 | long done;␊ |
665 | ␊ |
666 | do {␊ |
667 | done = 1;␊ |
668 | module = gModuleHead;␊ |
669 | ␊ |
670 | while (module != 0)␊ |
671 | {␊ |
672 | if (module->willLoad == 1)␊ |
673 | {␊ |
674 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
675 | if (prop != 0)␊ |
676 | {␊ |
677 | prop = prop->tag;␊ |
678 | while (prop != 0)␊ |
679 | {␊ |
680 | module2 = gModuleHead;␊ |
681 | while (module2 != 0)␊ |
682 | {␊ |
683 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
684 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
685 | {␊ |
686 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
687 | break;␊ |
688 | }␊ |
689 | module2 = module2->nextModule;␊ |
690 | }␊ |
691 | prop = prop->tagNext;␊ |
692 | }␊ |
693 | }␊ |
694 | module->willLoad = 2;␊ |
695 | done = 0;␊ |
696 | }␊ |
697 | module = module->nextModule;␊ |
698 | }␊ |
699 | }␊ |
700 | while (!done);␊ |
701 | ␊ |
702 | return 0;␊ |
703 | }␊ |
704 | ␊ |
705 | ␊ |
706 | //==========================================================================␊ |
707 | // FindModule␊ |
708 | ␊ |
709 | #if NOTDEF␊ |
710 | static ModulePtr␊ |
711 | FindModule( char * name )␊ |
712 | {␊ |
713 | ModulePtr module;␊ |
714 | TagPtr prop;␊ |
715 | ␊ |
716 | module = gModuleHead;␊ |
717 | ␊ |
718 | while (module != 0)␊ |
719 | {␊ |
720 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
721 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
722 | module = module->nextModule;␊ |
723 | }␊ |
724 | ␊ |
725 | return module;␊ |
726 | }␊ |
727 | #endif /* NOTDEF */␊ |
728 | ␊ |
729 | //==========================================================================␊ |
730 | // ParseXML␊ |
731 | ␊ |
732 | static long␊ |
733 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
734 | {␊ |
735 | ␉long length, pos;␊ |
736 | ␉TagPtr moduleDict, required;␊ |
737 | ␉ModulePtr tmpModule;␊ |
738 | ␊ |
739 | pos = 0;␊ |
740 | ␊ |
741 | while (1)␊ |
742 | {␊ |
743 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
744 | if (length == -1) break;␊ |
745 | ␊ |
746 | pos += length;␊ |
747 | ␊ |
748 | if (moduleDict == 0) continue;␊ |
749 | if (moduleDict->type == kTagTypeDict) break;␊ |
750 | ␊ |
751 | XMLFreeTag(moduleDict);␊ |
752 | }␊ |
753 | ␊ |
754 | if (length == -1) return -1;␊ |
755 | ␊ |
756 | required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
757 | if ( (required == 0) ||␊ |
758 | (required->type != kTagTypeString) ||␊ |
759 | !strcmp(required->string, "Safe Boot"))␊ |
760 | {␊ |
761 | XMLFreeTag(moduleDict);␊ |
762 | return -2;␊ |
763 | }␊ |
764 | ␊ |
765 | tmpModule = malloc(sizeof(Module));␊ |
766 | if (tmpModule == 0)␊ |
767 | {␊ |
768 | XMLFreeTag(moduleDict);␊ |
769 | return -1;␊ |
770 | }␊ |
771 | tmpModule->dict = moduleDict;␊ |
772 | ␊ |
773 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
774 | ␊ |
775 | tmpModule->willLoad = 1;␊ |
776 | ␊ |
777 | *module = tmpModule;␊ |
778 | ␊ |
779 | // Get the personalities.␊ |
780 | ␊ |
781 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
782 | ␊ |
783 | return 0;␊ |
784 | }␊ |
785 | ␊ |
786 | #if NOTDEF␊ |
787 | static char gPlatformName[64];␊ |
788 | #endif␊ |
789 | ␊ |
790 | long ␊ |
791 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
792 | {␊ |
793 | long ret;␊ |
794 | compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
795 | u_int32_t uncompressed_size, size;␊ |
796 | void *buffer;␊ |
797 | ␉unsigned long len;␊ |
798 | ␊ |
799 | #if 0␊ |
800 | printf("kernel header:\n");␊ |
801 | printf("signature: 0x%x\n", kernel_header->signature);␊ |
802 | printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
803 | printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
804 | printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
805 | printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
806 | getc();␊ |
807 | #endif␊ |
808 | ␊ |
809 | if (kernel_header->signature == OSSwapBigToHostConstInt32('comp')) {␊ |
810 | if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) {␊ |
811 | error("kernel compression is bad\n");␊ |
812 | return -1;␊ |
813 | }␊ |
814 | #if NOTDEF␊ |
815 | if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
816 | return -1;␊ |
817 | if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
818 | return -1;␊ |
819 | #endif␊ |
820 | ␊ |
821 | uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
822 | binary = buffer = malloc(uncompressed_size);␊ |
823 | ␊ |
824 | size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
825 | OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
826 | if (uncompressed_size != size) {␊ |
827 | error("size mismatch from lzss: %x\n", size);␊ |
828 | return -1;␊ |
829 | }␊ |
830 | if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
831 | Alder32(binary, uncompressed_size)) {␊ |
832 | printf("adler mismatch\n");␊ |
833 | return -1;␊ |
834 | }␊ |
835 | }␊ |
836 | ␊ |
837 | ret = ThinFatFile(&binary, &len);␊ |
838 | if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
839 | {␊ |
840 | ␉ archCpuType=CPU_TYPE_I386;␊ |
841 | ␉ ret = ThinFatFile(&binary, &len);␊ |
842 | }␊ |
843 | ␊ |
844 | ␉//patch_kernel(binary);␊ |
845 | ␉␊ |
846 | ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
847 | ␉␊ |
848 | if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
849 | {␊ |
850 | ␉ archCpuType=CPU_TYPE_I386;␊ |
851 | ␉ ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
852 | }␊ |
853 | ␉␊ |
854 | ␊ |
855 | return ret;␊ |
856 | }␊ |
857 |