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