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 | ␉␉␉␉if (gMacOSVersion[3] == '9') {␊ |
247 | strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */␊ |
248 | ␉␉␉␉␉strcat(gExtensionsSpec, "Library/");␊ |
249 | ␉␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
250 | ␉␉␉␉}␊ |
251 | strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */␊ |
252 | strcat(gExtensionsSpec, "System/Library/");␊ |
253 | FileLoadDrivers(gExtensionsSpec, 0);␊ |
254 | }␊ |
255 | ␊ |
256 | }␊ |
257 | }␊ |
258 | else␊ |
259 | {␊ |
260 | return 0;␊ |
261 | }␊ |
262 | ␊ |
263 | MatchPersonalities();␊ |
264 | ␊ |
265 | MatchLibraries();␊ |
266 | ␊ |
267 | LoadMatchedModules();␊ |
268 | ␊ |
269 | return 0;␊ |
270 | }␊ |
271 | ␊ |
272 | //==========================================================================␊ |
273 | // FileLoadMKext␊ |
274 | ␊ |
275 | static long␊ |
276 | FileLoadMKext( const char * dirSpec, const char * extDirSpec )␊ |
277 | {␊ |
278 | ␉long␉ret, flags, time, time2;␊ |
279 | ␉char␉altDirSpec[512];␊ |
280 | ␉␊ |
281 | ␉snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);␊ |
282 | ␉ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);␊ |
283 | ␊ |
284 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
285 | ␉{␊ |
286 | ␉␉ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
287 | ␊ |
288 | ␉␉if ((ret != 0)␊ |
289 | ␉␉␉|| ((flags & kFileTypeMask) != kFileTypeDirectory)␊ |
290 | ␉␉␉|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
291 | ␉␉{␊ |
292 | snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);␊ |
293 | ␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
294 | ␊ |
295 | ␉␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
296 | ␉␉␉␉return 0;␊ |
297 | ␉␉}␊ |
298 | ␉}␊ |
299 | ␉return -1;␊ |
300 | }␊ |
301 | ␊ |
302 | //==========================================================================␊ |
303 | // FileLoadDrivers␊ |
304 | ␊ |
305 | long␊ |
306 | FileLoadDrivers( char * dirSpec, long plugin )␊ |
307 | {␊ |
308 | long ret, length, flags, time, bundleType;␊ |
309 | long long␉ index;␊ |
310 | long result = -1;␊ |
311 | const char * name;␊ |
312 | ␊ |
313 | if ( !plugin )␊ |
314 | {␊ |
315 | // First try 10.6's path for loading Extensions.mkext.␊ |
316 | if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0)␊ |
317 | return 0;␊ |
318 | ␊ |
319 | // Next try the legacy path.␊ |
320 | else if (FileLoadMKext(dirSpec, "") == 0)␊ |
321 | return 0;␊ |
322 | ␊ |
323 | strcat(dirSpec, "Extensions");␊ |
324 | }␊ |
325 | ␊ |
326 | index = 0;␊ |
327 | while (1) {␊ |
328 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
329 | if (ret == -1) break;␊ |
330 | ␊ |
331 | // Make sure this is a directory.␊ |
332 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
333 | ␊ |
334 | // Make sure this is a kext.␊ |
335 | length = strlen(name);␊ |
336 | if (strcmp(name + length - 5, ".kext")) continue;␊ |
337 | ␊ |
338 | // Save the file name.␊ |
339 | strlcpy(gFileName, name, 4096);␊ |
340 | ␊ |
341 | // Determine the bundle type.␊ |
342 | snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);␊ |
343 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
344 | if (ret == 0) bundleType = kCFBundleType2;␊ |
345 | else bundleType = kCFBundleType3;␊ |
346 | ␊ |
347 | if (!plugin)␊ |
348 | snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
349 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
350 | ␊ |
351 | ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
352 | ␊ |
353 | if (result != 0)␊ |
354 | result = ret;␊ |
355 | ␊ |
356 | if (!plugin) ␊ |
357 | FileLoadDrivers(gDriverSpec, 1);␊ |
358 | }␊ |
359 | ␊ |
360 | return result;␊ |
361 | }␊ |
362 | ␊ |
363 | ␊ |
364 | //==========================================================================␊ |
365 | // ␊ |
366 | ␊ |
367 | long␊ |
368 | NetLoadDrivers( char * dirSpec )␊ |
369 | {␊ |
370 | long tries;␊ |
371 | ␊ |
372 | #if NODEF␊ |
373 | long cnt;␊ |
374 | ␊ |
375 | // Get the name of the kernel␊ |
376 | cnt = strlen(gBootFile);␊ |
377 | while (cnt--) {␊ |
378 | if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {␊ |
379 | cnt++;␊ |
380 | break;␊ |
381 | }␊ |
382 | }␊ |
383 | #endif␊ |
384 | ␊ |
385 | // INTEL modification␊ |
386 | snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
387 | ␊ |
388 | verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
389 | ␊ |
390 | tries = 3;␊ |
391 | while (tries--)␊ |
392 | {␊ |
393 | if (LoadDriverMKext(gDriverSpec) == 0) break;␊ |
394 | }␊ |
395 | if (tries == -1) return -1;␊ |
396 | ␊ |
397 | ␉return 0;␊ |
398 | }␊ |
399 | ␊ |
400 | //==========================================================================␊ |
401 | // loadDriverMKext␊ |
402 | ␊ |
403 | long␊ |
404 | LoadDriverMKext( char * fileSpec )␊ |
405 | {␊ |
406 | unsigned long driversAddr, driversLength;␊ |
407 | long length;␊ |
408 | char segName[32];␊ |
409 | DriversPackage * package;␊ |
410 | ␊ |
411 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
412 | ␊ |
413 | // Load the MKext.␊ |
414 | length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
415 | if (length < sizeof (DriversPackage)) return -1;␊ |
416 | ␊ |
417 | ␉// call hook to notify modules that the mkext has been loaded␊ |
418 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);␊ |
419 | ␊ |
420 | ␉␊ |
421 | // Verify the MKext.␊ |
422 | if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
423 | ( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
424 | ( GetPackageElement(length) > kLoadSize ) ||␊ |
425 | ( GetPackageElement(adler32) !=␊ |
426 | Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
427 | {␊ |
428 | return -1;␊ |
429 | }␊ |
430 | ␊ |
431 | // Make space for the MKext.␊ |
432 | driversLength = GetPackageElement(length);␊ |
433 | driversAddr = AllocateKernelMemory(driversLength);␊ |
434 | ␊ |
435 | // Copy the MKext.␊ |
436 | memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
437 | ␊ |
438 | // Add the MKext to the memory map.␊ |
439 | snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);␊ |
440 | AllocateMemoryRange(segName, driversAddr, driversLength,␊ |
441 | kBootDriverTypeMKEXT);␊ |
442 | ␊ |
443 | return 0;␊ |
444 | }␊ |
445 | ␊ |
446 | //==========================================================================␊ |
447 | // LoadDriverPList␊ |
448 | ␊ |
449 | long␊ |
450 | LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
451 | {␊ |
452 | long length, executablePathLength, bundlePathLength;␊ |
453 | ModulePtr module;␊ |
454 | TagPtr personalities;␊ |
455 | char * buffer = 0;␊ |
456 | char * tmpExecutablePath = 0;␊ |
457 | char * tmpBundlePath = 0;␊ |
458 | long ret = -1;␊ |
459 | ␊ |
460 | do {␊ |
461 | // Save the driver path.␊ |
462 | ␊ |
463 | if(name)␊ |
464 | snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name,␊ |
465 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
466 | else␊ |
467 | snprintf(gFileSpec, 4096, "%s/%s", dirSpec,␊ |
468 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
469 | executablePathLength = strlen(gFileSpec) + 1;␊ |
470 | ␊ |
471 | tmpExecutablePath = malloc(executablePathLength);␊ |
472 | if (tmpExecutablePath == 0) break;␊ |
473 | strcpy(tmpExecutablePath, gFileSpec);␊ |
474 | ␊ |
475 | if(name) ␊ |
476 | snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);␊ |
477 | else␊ |
478 | snprintf(gFileSpec, 4096, "%s", dirSpec);␊ |
479 | bundlePathLength = strlen(gFileSpec) + 1;␊ |
480 | ␊ |
481 | tmpBundlePath = malloc(bundlePathLength);␊ |
482 | if (tmpBundlePath == 0) break;␊ |
483 | ␊ |
484 | strcpy(tmpBundlePath, gFileSpec);␊ |
485 | ␊ |
486 | // Construct the file spec to the plist, then load it.␊ |
487 | ␊ |
488 | if(name)␊ |
489 | snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
490 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
491 | else␊ |
492 | snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec,␊ |
493 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
494 | ␊ |
495 | length = LoadFile(gFileSpec);␊ |
496 | if (length == -1) break;␊ |
497 | length = length + 1;␊ |
498 | buffer = malloc(length);␊ |
499 | if (buffer == 0) break;␊ |
500 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
501 | ␊ |
502 | // Parse the plist.␊ |
503 | ␊ |
504 | ret = ParseXML(buffer, &module, &personalities);␊ |
505 | if (ret != 0) { break; }␊ |
506 | // Allocate memory for the driver path and the plist.␊ |
507 | ␊ |
508 | module->executablePath = tmpExecutablePath;␊ |
509 | module->bundlePath = tmpBundlePath;␊ |
510 | module->bundlePathLength = bundlePathLength;␊ |
511 | module->plistAddr = malloc(length);␊ |
512 | ␊ |
513 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
514 | break;␊ |
515 | // Save the driver path in the module.␊ |
516 | //strcpy(module->driverPath, tmpDriverPath);␊ |
517 | tmpExecutablePath = 0;␊ |
518 | tmpBundlePath = 0;␊ |
519 | ␊ |
520 | // Add the plist to the module.␊ |
521 | ␊ |
522 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
523 | module->plistLength = length;␊ |
524 | ␊ |
525 | // Add the module to the end of the module list.␊ |
526 | ␊ |
527 | if (gModuleHead == 0)␊ |
528 | gModuleHead = module;␊ |
529 | else␊ |
530 | gModuleTail->nextModule = module;␊ |
531 | gModuleTail = module;␊ |
532 | ␊ |
533 | // Add the persionalities to the personality list.␊ |
534 | ␊ |
535 | if (personalities) personalities = personalities->tag;␊ |
536 | while (personalities != 0)␊ |
537 | {␊ |
538 | if (gPersonalityHead == 0)␊ |
539 | gPersonalityHead = personalities->tag;␊ |
540 | else␊ |
541 | gPersonalityTail->tagNext = personalities->tag;␊ |
542 | ␊ |
543 | gPersonalityTail = personalities->tag;␊ |
544 | personalities = personalities->tagNext;␊ |
545 | }␊ |
546 | ␊ |
547 | ret = 0;␊ |
548 | }␊ |
549 | while (0);␊ |
550 | ␊ |
551 | if ( buffer ) free( buffer );␊ |
552 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
553 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
554 | ␊ |
555 | return ret;␊ |
556 | }␊ |
557 | ␊ |
558 | ␊ |
559 | //==========================================================================␊ |
560 | // LoadMatchedModules␊ |
561 | ␊ |
562 | long␊ |
563 | LoadMatchedModules( void )␊ |
564 | {␊ |
565 | ␉TagPtr␉␉ prop;␊ |
566 | ␉ModulePtr␉ module;␊ |
567 | ␉char␉␉ *fileName, segName[32];␊ |
568 | ␉DriverInfoPtr driver;␊ |
569 | ␉long␉␉ length, driverAddr, driverLength;␊ |
570 | ␉void␉␉ *executableAddr = 0;␊ |
571 | ␊ |
572 | ␉module = gModuleHead;␊ |
573 | ␊ |
574 | ␉while (module != 0)␊ |
575 | ␉{␊ |
576 | ␉␉if (module->willLoad)␊ |
577 | ␉␉{␊ |
578 | ␉␉␉prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
579 | ␊ |
580 | ␉␉␉if (prop != 0)␊ |
581 | ␉␉␉{␊ |
582 | ␉␉␉␉fileName = prop->string;␊ |
583 | ␉␉␉␉snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);␊ |
584 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
585 | ␉␉␉␉if (length == 0)␊ |
586 | ␉␉␉␉{␊ |
587 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
588 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
589 | ␉␉␉␉}␊ |
590 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
591 | ␉␉␉}␊ |
592 | ␉␉␉else␊ |
593 | ␉␉␉␉length = 0;␊ |
594 | ␊ |
595 | ␉␉␉if (length != -1)␊ |
596 | ␉␉␉{␊ |
597 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
598 | //␉␉␉␉if (length != 0)␊ |
599 | //␉␉␉␉{␊ |
600 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
601 | //␉␉␉␉}␊ |
602 | ␊ |
603 | ␉␉␉␉// Make make in the image area.␊ |
604 | ␊ |
605 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
606 | ␊ |
607 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
608 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
609 | ␊ |
610 | ␉␉␉␉// Set up the DriverInfo.␊ |
611 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
612 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
613 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
614 | ␉␉␉␉if (length != 0)␊ |
615 | ␉␉␉␉{␊ |
616 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
617 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
618 | ␉␉␉␉␉driver->executableLength = length;␊ |
619 | ␉␉␉␉}␊ |
620 | ␉␉␉␉else␊ |
621 | ␉␉␉␉{␊ |
622 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
623 | ␉␉␉␉␉driver->executableLength = 0;␊ |
624 | ␉␉␉␉}␊ |
625 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
626 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
627 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
628 | ␊ |
629 | ␉␉␉␉// Save the plist, module and bundle.␊ |
630 | ␉␉␉␉strcpy(driver->plistAddr, module->plistAddr);␊ |
631 | ␉␉␉␉if (length != 0)␊ |
632 | ␉␉␉␉{␊ |
633 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
634 | ␉␉␉␉}␊ |
635 | ␉␉␉␉strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
636 | ␊ |
637 | ␉␉␉␉// Add an entry to the memory map.␊ |
638 | ␉␉␉␉snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);␊ |
639 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
640 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
641 | ␉␉␉}␊ |
642 | ␉␉}␊ |
643 | ␉␉module = module->nextModule;␊ |
644 | ␉}␊ |
645 | ␊ |
646 | ␉return 0;␊ |
647 | }␊ |
648 | ␊ |
649 | //==========================================================================␊ |
650 | // MatchPersonalities␊ |
651 | ␊ |
652 | static long␊ |
653 | MatchPersonalities( void )␊ |
654 | {␊ |
655 | /* IONameMatch support not implemented */␊ |
656 | return 0;␊ |
657 | }␊ |
658 | ␊ |
659 | //==========================================================================␊ |
660 | // MatchLibraries␊ |
661 | ␊ |
662 | static long␊ |
663 | MatchLibraries( void )␊ |
664 | {␊ |
665 | TagPtr prop, prop2;␊ |
666 | ModulePtr module, module2;␊ |
667 | long done;␊ |
668 | ␊ |
669 | do {␊ |
670 | done = 1;␊ |
671 | module = gModuleHead;␊ |
672 | ␊ |
673 | while (module != 0)␊ |
674 | {␊ |
675 | if (module->willLoad == 1)␊ |
676 | {␊ |
677 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
678 | if (prop != 0)␊ |
679 | {␊ |
680 | prop = prop->tag;␊ |
681 | while (prop != 0)␊ |
682 | {␊ |
683 | module2 = gModuleHead;␊ |
684 | while (module2 != 0)␊ |
685 | {␊ |
686 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
687 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
688 | {␊ |
689 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
690 | break;␊ |
691 | }␊ |
692 | module2 = module2->nextModule;␊ |
693 | }␊ |
694 | prop = prop->tagNext;␊ |
695 | }␊ |
696 | }␊ |
697 | module->willLoad = 2;␊ |
698 | done = 0;␊ |
699 | }␊ |
700 | module = module->nextModule;␊ |
701 | }␊ |
702 | }␊ |
703 | while (!done);␊ |
704 | ␊ |
705 | return 0;␊ |
706 | }␊ |
707 | ␊ |
708 | ␊ |
709 | //==========================================================================␊ |
710 | // FindModule␊ |
711 | ␊ |
712 | #if NOTDEF␊ |
713 | static ModulePtr␊ |
714 | FindModule( char * name )␊ |
715 | {␊ |
716 | ModulePtr module;␊ |
717 | TagPtr prop;␊ |
718 | ␊ |
719 | module = gModuleHead;␊ |
720 | ␊ |
721 | while (module != 0)␊ |
722 | {␊ |
723 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
724 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
725 | module = module->nextModule;␊ |
726 | }␊ |
727 | ␊ |
728 | return module;␊ |
729 | }␊ |
730 | #endif /* NOTDEF */␊ |
731 | ␊ |
732 | //==========================================================================␊ |
733 | // ParseXML␊ |
734 | ␊ |
735 | static long␊ |
736 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
737 | {␊ |
738 | ␉long length, pos;␊ |
739 | ␉TagPtr moduleDict, required;␊ |
740 | ␉ModulePtr tmpModule;␊ |
741 | ␊ |
742 | pos = 0;␊ |
743 | ␊ |
744 | while (1)␊ |
745 | {␊ |
746 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
747 | if (length == -1) break;␊ |
748 | ␊ |
749 | pos += length;␊ |
750 | ␊ |
751 | if (moduleDict == 0) continue;␊ |
752 | if (moduleDict->type == kTagTypeDict) break;␊ |
753 | ␊ |
754 | XMLFreeTag(moduleDict);␊ |
755 | }␊ |
756 | ␊ |
757 | if (length == -1) return -1;␊ |
758 | ␊ |
759 | ␉required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
760 | ␊ |
761 | ␉if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))␊ |
762 | ␉{␊ |
763 | ␉␉XMLFreeTag(moduleDict);␊ |
764 | ␉␉return -2;␊ |
765 | ␉}␊ |
766 | ␊ |
767 | tmpModule = malloc(sizeof(Module));␊ |
768 | if (tmpModule == 0)␊ |
769 | {␊ |
770 | XMLFreeTag(moduleDict);␊ |
771 | return -1;␊ |
772 | }␊ |
773 | tmpModule->dict = moduleDict;␊ |
774 | ␊ |
775 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
776 | ␊ |
777 | tmpModule->willLoad = 1;␊ |
778 | ␊ |
779 | *module = tmpModule;␊ |
780 | ␊ |
781 | // Get the personalities.␊ |
782 | ␊ |
783 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
784 | ␊ |
785 | return 0;␊ |
786 | }␊ |
787 | ␊ |
788 | #if NOTDEF␊ |
789 | static char gPlatformName[64];␊ |
790 | #endif␊ |
791 | ␊ |
792 | long ␊ |
793 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
794 | {␊ |
795 | ␉long ret;␊ |
796 | ␉compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
797 | ␉u_int32_t uncompressed_size, size;␊ |
798 | ␉void *buffer;␊ |
799 | ␉unsigned long len;␊ |
800 | ␉␊ |
801 | #if 0␊ |
802 | ␉printf("kernel header:\n");␊ |
803 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
804 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
805 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
806 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
807 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
808 | ␉getchar();␊ |
809 | #endif␊ |
810 | ␊ |
811 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
812 | ␉{␊ |
813 | ␉␉if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))␊ |
814 | ␉␉{␊ |
815 | ␉␉␉error("kernel compression is bad\n");␊ |
816 | ␉␉␉return -1;␊ |
817 | ␉␉}␊ |
818 | #if NOTDEF␊ |
819 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
820 | ␉␉{␊ |
821 | ␉␉␉return -1;␊ |
822 | ␉␉}␊ |
823 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
824 | ␉␉{␊ |
825 | ␉␉␉return -1;␊ |
826 | ␉␉}␊ |
827 | #endif␊ |
828 | ␊ |
829 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
830 | ␉␉binary = buffer = malloc(uncompressed_size);␊ |
831 | ␉␉␊ |
832 | ␉␉size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
833 | ␉␉␉␉␉␉␉ OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
834 | ␉␉if (uncompressed_size != size) {␊ |
835 | ␉␉␉error("size mismatch from lzss: %x\n", size);␊ |
836 | ␉␉␉return -1;␊ |
837 | ␉␉}␊ |
838 | ␉␉␊ |
839 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
840 | ␉␉␉Adler32(binary, uncompressed_size))␊ |
841 | ␉␉{␊ |
842 | ␉␉␉printf("adler mismatch\n");␊ |
843 | ␉␉␉return -1;␊ |
844 | ␉␉}␊ |
845 | ␉}␊ |
846 | ␉␊ |
847 | ␉ret = ThinFatFile(&binary, &len);␊ |
848 | if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
849 | {␊ |
850 | archCpuType=CPU_TYPE_I386;␊ |
851 | ret = ThinFatFile(&binary, &len);␊ |
852 | }␊ |
853 | ␊ |
854 | // Notify modules that the kernel has been decompressed, thinned and is about to be decoded␊ |
855 | ␉execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);␊ |
856 | ␊ |
857 | ␊ |
858 | ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
859 | if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
860 | {␊ |
861 | archCpuType=CPU_TYPE_I386;␊ |
862 | ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
863 | }␊ |
864 | ␊ |
865 | return ret;␊ |
866 | }␊ |
867 | |