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