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