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