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