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 | ␉␉␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
248 | ␉␉␉␉␉strcat(gExtensionsSpec, "Library/");␊ |
249 | ␉␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
250 | ␉␉␉␉}␊ |
251 | strcpy(gExtensionsSpec, dirSpec);␊ |
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 | ␉sprintf (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 | ␉␉␉sprintf(gDriverSpec, "%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 | strcpy(gFileName, name);␊ |
340 | ␊ |
341 | // Determine the bundle type.␊ |
342 | sprintf(gTempSpec, "%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 | sprintf(gDriverSpec, "%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 | sprintf(gDriverSpec, "%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 | sprintf(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) sprintf(gFileSpec, "%s/%s/%s", dirSpec, name,␊ |
464 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
465 | else sprintf(gFileSpec, "%s/%s", dirSpec,␊ |
466 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
467 | executablePathLength = strlen(gFileSpec) + 1;␊ |
468 | ␊ |
469 | tmpExecutablePath = malloc(executablePathLength);␊ |
470 | if (tmpExecutablePath == 0) break;␊ |
471 | strcpy(tmpExecutablePath, gFileSpec);␊ |
472 | ␊ |
473 | if(name) sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
474 | else sprintf(gFileSpec, "%s", dirSpec);␊ |
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 | if(name) sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
485 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
486 | else sprintf(gFileSpec, "%s/%sInfo.plist", dirSpec,␊ |
487 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
488 | ␊ |
489 | length = LoadFile(gFileSpec);␊ |
490 | if (length == -1) break;␊ |
491 | length = length + 1;␊ |
492 | buffer = malloc(length);␊ |
493 | if (buffer == 0) break;␊ |
494 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
495 | ␊ |
496 | // Parse the plist.␊ |
497 | ␊ |
498 | ret = ParseXML(buffer, &module, &personalities);␊ |
499 | if (ret != 0) { break; }␊ |
500 | // Allocate memory for the driver path and the plist.␊ |
501 | ␊ |
502 | module->executablePath = tmpExecutablePath;␊ |
503 | module->bundlePath = tmpBundlePath;␊ |
504 | module->bundlePathLength = bundlePathLength;␊ |
505 | module->plistAddr = malloc(length);␊ |
506 | ␊ |
507 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
508 | break;␊ |
509 | // Save the driver path in the module.␊ |
510 | //strcpy(module->driverPath, tmpDriverPath);␊ |
511 | tmpExecutablePath = 0;␊ |
512 | tmpBundlePath = 0;␊ |
513 | ␊ |
514 | // Add the plist to the module.␊ |
515 | ␊ |
516 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
517 | module->plistLength = length;␊ |
518 | ␊ |
519 | // Add the module to the end of the module list.␊ |
520 | ␊ |
521 | if (gModuleHead == 0)␊ |
522 | gModuleHead = module;␊ |
523 | else␊ |
524 | gModuleTail->nextModule = module;␊ |
525 | gModuleTail = module;␊ |
526 | ␊ |
527 | // Add the persionalities to the personality list.␊ |
528 | ␊ |
529 | if (personalities) personalities = personalities->tag;␊ |
530 | while (personalities != 0)␊ |
531 | {␊ |
532 | if (gPersonalityHead == 0)␊ |
533 | gPersonalityHead = personalities->tag;␊ |
534 | else␊ |
535 | gPersonalityTail->tagNext = personalities->tag;␊ |
536 | ␊ |
537 | gPersonalityTail = personalities->tag;␊ |
538 | personalities = personalities->tagNext;␊ |
539 | }␊ |
540 | ␊ |
541 | ret = 0;␊ |
542 | }␊ |
543 | while (0);␊ |
544 | ␊ |
545 | if ( buffer ) free( buffer );␊ |
546 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
547 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
548 | ␊ |
549 | return ret;␊ |
550 | }␊ |
551 | ␊ |
552 | ␊ |
553 | //==========================================================================␊ |
554 | // LoadMatchedModules␊ |
555 | ␊ |
556 | long␊ |
557 | LoadMatchedModules( void )␊ |
558 | {␊ |
559 | ␉TagPtr␉␉ prop;␊ |
560 | ␉ModulePtr␉ module;␊ |
561 | ␉char␉␉ *fileName, segName[32];␊ |
562 | ␉DriverInfoPtr driver;␊ |
563 | ␉long␉␉ length, driverAddr, driverLength;␊ |
564 | ␉void␉␉ *executableAddr = 0;␊ |
565 | ␊ |
566 | ␉module = gModuleHead;␊ |
567 | ␊ |
568 | ␉while (module != 0)␊ |
569 | ␉{␊ |
570 | ␉␉if (module->willLoad)␊ |
571 | ␉␉{␊ |
572 | ␉␉␉prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
573 | ␊ |
574 | ␉␉␉if (prop != 0)␊ |
575 | ␉␉␉{␊ |
576 | ␉␉␉␉fileName = prop->string;␊ |
577 | ␉␉␉␉sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
578 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
579 | ␉␉␉␉if (length == 0)␊ |
580 | ␉␉␉␉{␊ |
581 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
582 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
583 | ␉␉␉␉}␊ |
584 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
585 | ␉␉␉}␊ |
586 | ␉␉␉else␊ |
587 | ␉␉␉␉length = 0;␊ |
588 | ␊ |
589 | ␉␉␉if (length != -1)␊ |
590 | ␉␉␉{␊ |
591 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
592 | //␉␉␉␉if (length != 0)␊ |
593 | //␉␉␉␉{␊ |
594 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
595 | //␉␉␉␉}␊ |
596 | ␊ |
597 | ␉␉␉␉// Make make in the image area.␊ |
598 | ␊ |
599 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
600 | ␊ |
601 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
602 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
603 | ␊ |
604 | ␉␉␉␉// Set up the DriverInfo.␊ |
605 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
606 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
607 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
608 | ␉␉␉␉if (length != 0)␊ |
609 | ␉␉␉␉{␊ |
610 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
611 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
612 | ␉␉␉␉␉driver->executableLength = length;␊ |
613 | ␉␉␉␉}␊ |
614 | ␉␉␉␉else␊ |
615 | ␉␉␉␉{␊ |
616 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
617 | ␉␉␉␉␉driver->executableLength = 0;␊ |
618 | ␉␉␉␉}␊ |
619 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
620 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
621 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
622 | ␊ |
623 | ␉␉␉␉// Save the plist, module and bundle.␊ |
624 | ␉␉␉␉strcpy(driver->plistAddr, module->plistAddr);␊ |
625 | ␉␉␉␉if (length != 0)␊ |
626 | ␉␉␉␉{␊ |
627 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
628 | ␉␉␉␉}␊ |
629 | ␉␉␉␉strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
630 | ␊ |
631 | ␉␉␉␉// Add an entry to the memory map.␊ |
632 | ␉␉␉␉sprintf(segName, "Driver-%lx", (unsigned long)driver);␊ |
633 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
634 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
635 | ␉␉␉}␊ |
636 | ␉␉}␊ |
637 | ␉␉module = module->nextModule;␊ |
638 | ␉}␊ |
639 | ␊ |
640 | ␉return 0;␊ |
641 | }␊ |
642 | ␊ |
643 | //==========================================================================␊ |
644 | // MatchPersonalities␊ |
645 | ␊ |
646 | static long␊ |
647 | MatchPersonalities( void )␊ |
648 | {␊ |
649 | /* IONameMatch support not implemented */␊ |
650 | return 0;␊ |
651 | }␊ |
652 | ␊ |
653 | //==========================================================================␊ |
654 | // MatchLibraries␊ |
655 | ␊ |
656 | static long␊ |
657 | MatchLibraries( void )␊ |
658 | {␊ |
659 | TagPtr prop, prop2;␊ |
660 | ModulePtr module, module2;␊ |
661 | long done;␊ |
662 | ␊ |
663 | do {␊ |
664 | done = 1;␊ |
665 | module = gModuleHead;␊ |
666 | ␊ |
667 | while (module != 0)␊ |
668 | {␊ |
669 | if (module->willLoad == 1)␊ |
670 | {␊ |
671 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
672 | if (prop != 0)␊ |
673 | {␊ |
674 | prop = prop->tag;␊ |
675 | while (prop != 0)␊ |
676 | {␊ |
677 | module2 = gModuleHead;␊ |
678 | while (module2 != 0)␊ |
679 | {␊ |
680 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
681 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
682 | {␊ |
683 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
684 | break;␊ |
685 | }␊ |
686 | module2 = module2->nextModule;␊ |
687 | }␊ |
688 | prop = prop->tagNext;␊ |
689 | }␊ |
690 | }␊ |
691 | module->willLoad = 2;␊ |
692 | done = 0;␊ |
693 | }␊ |
694 | module = module->nextModule;␊ |
695 | }␊ |
696 | }␊ |
697 | while (!done);␊ |
698 | ␊ |
699 | return 0;␊ |
700 | }␊ |
701 | ␊ |
702 | ␊ |
703 | //==========================================================================␊ |
704 | // FindModule␊ |
705 | ␊ |
706 | #if NOTDEF␊ |
707 | static ModulePtr␊ |
708 | FindModule( char * name )␊ |
709 | {␊ |
710 | ModulePtr module;␊ |
711 | TagPtr prop;␊ |
712 | ␊ |
713 | module = gModuleHead;␊ |
714 | ␊ |
715 | while (module != 0)␊ |
716 | {␊ |
717 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
718 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
719 | module = module->nextModule;␊ |
720 | }␊ |
721 | ␊ |
722 | return module;␊ |
723 | }␊ |
724 | #endif /* NOTDEF */␊ |
725 | ␊ |
726 | //==========================================================================␊ |
727 | // ParseXML␊ |
728 | ␊ |
729 | static long␊ |
730 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
731 | {␊ |
732 | ␉long length, pos;␊ |
733 | ␉TagPtr moduleDict, required;␊ |
734 | ␉ModulePtr tmpModule;␊ |
735 | ␊ |
736 | pos = 0;␊ |
737 | ␊ |
738 | while (1)␊ |
739 | {␊ |
740 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
741 | if (length == -1) break;␊ |
742 | ␊ |
743 | pos += length;␊ |
744 | ␊ |
745 | if (moduleDict == 0) continue;␊ |
746 | if (moduleDict->type == kTagTypeDict) break;␊ |
747 | ␊ |
748 | XMLFreeTag(moduleDict);␊ |
749 | }␊ |
750 | ␊ |
751 | if (length == -1) return -1;␊ |
752 | ␊ |
753 | ␉required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
754 | ␊ |
755 | ␉if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))␊ |
756 | ␉{␊ |
757 | ␉␉XMLFreeTag(moduleDict);␊ |
758 | ␉␉return -2;␊ |
759 | ␉}␊ |
760 | ␊ |
761 | tmpModule = malloc(sizeof(Module));␊ |
762 | if (tmpModule == 0)␊ |
763 | {␊ |
764 | XMLFreeTag(moduleDict);␊ |
765 | return -1;␊ |
766 | }␊ |
767 | tmpModule->dict = moduleDict;␊ |
768 | ␊ |
769 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
770 | ␊ |
771 | tmpModule->willLoad = 1;␊ |
772 | ␊ |
773 | *module = tmpModule;␊ |
774 | ␊ |
775 | // Get the personalities.␊ |
776 | ␊ |
777 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
778 | ␊ |
779 | return 0;␊ |
780 | }␊ |
781 | ␊ |
782 | #if NOTDEF␊ |
783 | static char gPlatformName[64];␊ |
784 | #endif␊ |
785 | ␊ |
786 | long ␊ |
787 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
788 | {␊ |
789 | ␉long ret;␊ |
790 | ␉compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
791 | ␉u_int32_t uncompressed_size, size;␊ |
792 | ␉void *buffer;␊ |
793 | ␉unsigned long len;␊ |
794 | ␉␊ |
795 | #if 0␊ |
796 | ␉printf("kernel header:\n");␊ |
797 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
798 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
799 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
800 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
801 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
802 | ␉getchar();␊ |
803 | #endif␊ |
804 | ␊ |
805 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
806 | ␉{␊ |
807 | ␉␉if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))␊ |
808 | ␉␉{␊ |
809 | ␉␉␉error("kernel compression is bad\n");␊ |
810 | ␉␉␉return -1;␊ |
811 | ␉␉}␊ |
812 | #if NOTDEF␊ |
813 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
814 | ␉␉{␊ |
815 | ␉␉␉return -1;␊ |
816 | ␉␉}␊ |
817 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
818 | ␉␉{␊ |
819 | ␉␉␉return -1;␊ |
820 | ␉␉}␊ |
821 | #endif␊ |
822 | ␊ |
823 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
824 | ␉␉binary = buffer = malloc(uncompressed_size);␊ |
825 | ␉␉␊ |
826 | ␉␉size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
827 | ␉␉␉␉␉␉␉ OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
828 | ␉␉if (uncompressed_size != size) {␊ |
829 | ␉␉␉error("size mismatch from lzss: %x\n", size);␊ |
830 | ␉␉␉return -1;␊ |
831 | ␉␉}␊ |
832 | ␉␉␊ |
833 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
834 | ␉␉␉Adler32(binary, uncompressed_size))␊ |
835 | ␉␉{␊ |
836 | ␉␉␉printf("adler mismatch\n");␊ |
837 | ␉␉␉return -1;␊ |
838 | ␉␉}␊ |
839 | ␉}␊ |
840 | ␉␊ |
841 | ␉ret = ThinFatFile(&binary, &len);␊ |
842 | if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
843 | {␊ |
844 | archCpuType=CPU_TYPE_I386;␊ |
845 | ret = ThinFatFile(&binary, &len);␊ |
846 | }␊ |
847 | ␊ |
848 | // Notify modules that the kernel has been decompressed, thinned and is about to be decoded␊ |
849 | ␉execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);␊ |
850 | ␊ |
851 | ␊ |
852 | ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
853 | if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
854 | {␊ |
855 | archCpuType=CPU_TYPE_I386;␊ |
856 | ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
857 | }␊ |
858 | ␊ |
859 | return ret;␊ |
860 | }␊ |
861 | |