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