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