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