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