1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the 'License'). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * drivers.c - Driver Loading Functions.␊ |
26 | *␊ |
27 | * Copyright (c) 2000 Apple Computer, Inc.␊ |
28 | *␊ |
29 | * DRI: Josh de Cesare␊ |
30 | */␊ |
31 | ␊ |
32 | #include <mach-o/fat.h>␊ |
33 | #include <libkern/OSByteOrder.h>␊ |
34 | #include <mach/machine.h>␊ |
35 | ␊ |
36 | #include "sl.h"␊ |
37 | #include "boot.h"␊ |
38 | #include "bootstruct.h"␊ |
39 | #include "xml.h"␊ |
40 | #include "ramdisk.h"␊ |
41 | #include "modules.h"␊ |
42 | ␊ |
43 | //extern char gMacOSVersion[8];␊ |
44 | ␊ |
45 | struct Module { ␊ |
46 | struct Module *nextModule;␊ |
47 | long willLoad;␊ |
48 | TagPtr dict;␊ |
49 | char *plistAddr;␊ |
50 | long plistLength;␊ |
51 | char *executablePath;␊ |
52 | char *bundlePath;␊ |
53 | long bundlePathLength;␊ |
54 | };␊ |
55 | typedef struct Module Module, *ModulePtr;␊ |
56 | ␊ |
57 | struct DriverInfo {␊ |
58 | char *plistAddr;␊ |
59 | long plistLength;␊ |
60 | void *executableAddr;␊ |
61 | long executableLength;␊ |
62 | void *bundlePathAddr;␊ |
63 | long bundlePathLength;␊ |
64 | };␊ |
65 | typedef struct DriverInfo DriverInfo, *DriverInfoPtr;␊ |
66 | ␊ |
67 | #define kDriverPackageSignature1 'MKXT'␊ |
68 | #define kDriverPackageSignature2 'MOSX'␊ |
69 | ␊ |
70 | struct DriversPackage {␊ |
71 | unsigned long signature1;␊ |
72 | unsigned long signature2;␊ |
73 | unsigned long length;␊ |
74 | unsigned long adler32;␊ |
75 | unsigned long version;␊ |
76 | unsigned long numDrivers;␊ |
77 | unsigned long reserved1;␊ |
78 | unsigned long reserved2;␊ |
79 | };␊ |
80 | typedef struct DriversPackage DriversPackage;␊ |
81 | ␊ |
82 | enum {␊ |
83 | kCFBundleType2,␊ |
84 | kCFBundleType3␊ |
85 | };␊ |
86 | ␊ |
87 | long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);␊ |
88 | ␊ |
89 | /*static*/ unsigned long Adler32( unsigned char * buffer, long length );␊ |
90 | ␊ |
91 | static long FileLoadDrivers(char *dirSpec, long plugin);␊ |
92 | static long NetLoadDrivers(char *dirSpec);␊ |
93 | static long LoadDriverMKext(char *fileSpec);␊ |
94 | static long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
95 | static long LoadMatchedModules(void);␊ |
96 | static long MatchPersonalities(void);␊ |
97 | static long MatchLibraries(void);␊ |
98 | #ifdef NOTDEF␊ |
99 | static ModulePtr FindModule(char *name);␊ |
100 | static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);␊ |
101 | #endif␊ |
102 | static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);␊ |
103 | static long InitDriverSupport(void);␊ |
104 | ␊ |
105 | static ModulePtr gModuleHead, gModuleTail;␊ |
106 | static TagPtr gPersonalityHead, gPersonalityTail;␊ |
107 | static char * gExtensionsSpec;␊ |
108 | static char * gDriverSpec;␊ |
109 | static char * gFileSpec;␊ |
110 | static char * gTempSpec;␊ |
111 | static char * gFileName;␊ |
112 | ␊ |
113 | /*static*/ unsigned long␊ |
114 | Adler32( unsigned char * buffer, long length )␊ |
115 | {␊ |
116 | long cnt;␊ |
117 | unsigned long result, lowHalf, highHalf;␊ |
118 | ␊ |
119 | lowHalf = 1;␊ |
120 | highHalf = 0;␊ |
121 | ␊ |
122 | ␉for ( cnt = 0; cnt < length; cnt++ )␊ |
123 | {␊ |
124 | if ((cnt % 5000) == 0)␊ |
125 | {␊ |
126 | lowHalf %= 65521L;␊ |
127 | highHalf %= 65521L;␊ |
128 | }␊ |
129 | ␊ |
130 | lowHalf += buffer[cnt];␊ |
131 | highHalf += lowHalf;␊ |
132 | }␊ |
133 | ␊ |
134 | ␉lowHalf %= 65521L;␊ |
135 | ␉highHalf %= 65521L;␊ |
136 | ␊ |
137 | ␉result = (highHalf << 16) | lowHalf;␊ |
138 | ␊ |
139 | ␉return result;␊ |
140 | }␊ |
141 | ␊ |
142 | ␊ |
143 | //==========================================================================␊ |
144 | // InitDriverSupport␊ |
145 | ␊ |
146 | static long␊ |
147 | InitDriverSupport( void )␊ |
148 | {␊ |
149 | gExtensionsSpec = malloc( 4096 );␊ |
150 | gDriverSpec = malloc( 4096 );␊ |
151 | gFileSpec = malloc( 4096 );␊ |
152 | gTempSpec = malloc( 4096 );␊ |
153 | gFileName = malloc( 4096 );␊ |
154 | ␊ |
155 | if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName )␊ |
156 | stop("InitDriverSupport error");␊ |
157 | ␊ |
158 | return 0;␊ |
159 | }␊ |
160 | ␊ |
161 | //==========================================================================␊ |
162 | // LoadDrivers␊ |
163 | ␊ |
164 | long LoadDrivers( char * dirSpec )␊ |
165 | {␊ |
166 | char dirSpecExtra[1024];␊ |
167 | ␊ |
168 | if ( InitDriverSupport() != 0 )␊ |
169 | return 0;␊ |
170 | ␊ |
171 | // Load extra drivers if a hook has been installed.␊ |
172 | if (LoadExtraDrivers_p != NULL)␊ |
173 | {␊ |
174 | (*LoadExtraDrivers_p)(&FileLoadDrivers);␊ |
175 | }␊ |
176 | ␊ |
177 | if ( gBootFileType == kNetworkDeviceType )␊ |
178 | {␊ |
179 | if (NetLoadDrivers(dirSpec) != 0) {␊ |
180 | error("Could not load drivers from the network\n");␊ |
181 | return -1;␊ |
182 | }␊ |
183 | }␊ |
184 | else if ( gBootFileType == kBlockDeviceType )␊ |
185 | ␉{␊ |
186 | // First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).␊ |
187 | if (gRAMDiskVolume && !gRAMDiskBTAliased)␊ |
188 | {␊ |
189 | strcpy(dirSpecExtra, "rd(0,0)/Extra/");␊ |
190 | FileLoadDrivers(dirSpecExtra, 0);␊ |
191 | }␊ |
192 | ␊ |
193 | // Next try to load Extra extensions from the selected root partition.␊ |
194 | strcpy(dirSpecExtra, "/Extra/");␊ |
195 | if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
196 | {␊ |
197 | // If failed, then try to load Extra extensions from the boot partition␊ |
198 | ␉ // in case we have a separate booter partition or a bt(0,0) aliased ramdisk.␊ |
199 | ␉ if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)␊ |
200 | ␉ || (gRAMDiskVolume && gRAMDiskBTAliased) )␊ |
201 | ␉ {␊ |
202 | ␉ // Next try a specfic OS version folder ie 10.5␊ |
203 | sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);␊ |
204 | ␉ if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
205 | ␉ {␉␊ |
206 | ␉ // Next we'll try the base␊ |
207 | ␉ strcpy(dirSpecExtra, "bt(0,0)/Extra/");␊ |
208 | ␉ FileLoadDrivers(dirSpecExtra, 0);␊ |
209 | ␉ }␊ |
210 | ␉ }␊ |
211 | }␊ |
212 | ␊ |
213 | // Also try to load Extensions from boot helper partitions.␊ |
214 | if (gBootVolume->flags & kBVFlagBooter)␊ |
215 | {␊ |
216 | strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
217 | if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
218 | {␊ |
219 | strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
220 | if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
221 | {␊ |
222 | strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
223 | FileLoadDrivers(dirSpecExtra, 0);␊ |
224 | }␊ |
225 | }␊ |
226 | }␊ |
227 | ␊ |
228 | if (gMKextName[0] != '\0')␊ |
229 | {␊ |
230 | verbose("LoadDrivers: Loading from [%s]\n", gMKextName);␊ |
231 | if ( LoadDriverMKext(gMKextName) != 0 )␊ |
232 | {␊ |
233 | error("Could not load %s\n", gMKextName);␊ |
234 | return -1;␊ |
235 | }␊ |
236 | }␊ |
237 | else␊ |
238 | {␊ |
239 | strcpy(gExtensionsSpec, dirSpec);␊ |
240 | strcat(gExtensionsSpec, "System/Library/");␊ |
241 | FileLoadDrivers(gExtensionsSpec, 0);␊ |
242 | }␊ |
243 | }␊ |
244 | else␊ |
245 | {␊ |
246 | return 0;␊ |
247 | }␊ |
248 | ␊ |
249 | MatchPersonalities();␊ |
250 | ␊ |
251 | MatchLibraries();␊ |
252 | ␊ |
253 | LoadMatchedModules();␊ |
254 | ␊ |
255 | return 0;␊ |
256 | }␊ |
257 | ␊ |
258 | //==========================================================================␊ |
259 | // FileLoadMKext␊ |
260 | ␊ |
261 | static long␊ |
262 | FileLoadMKext( const char * dirSpec, const char * extDirSpec )␊ |
263 | {␊ |
264 | ␉long␉ret, flags, time, time2;␊ |
265 | ␉char␉altDirSpec[512];␊ |
266 | ␉␊ |
267 | ␉sprintf (altDirSpec, "%s%s", dirSpec, extDirSpec);␊ |
268 | ␉ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);␊ |
269 | ␉␊ |
270 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
271 | ␉{␊ |
272 | ␉␉ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
273 | ␉␉␊ |
274 | ␉␉if ((ret != 0)␊ |
275 | ␉␉␉|| ((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 | ␉␉␉␊ |
281 | ␉␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
282 | ␉␉␉␉return 0;␊ |
283 | ␉␉}␊ |
284 | ␉}␊ |
285 | ␉return -1;␊ |
286 | }␊ |
287 | ␊ |
288 | //==========================================================================␊ |
289 | // FileLoadDrivers␊ |
290 | ␊ |
291 | static long␊ |
292 | FileLoadDrivers( char * dirSpec, long plugin )␊ |
293 | {␊ |
294 | long ret, length, flags, time, bundleType;␊ |
295 | long long␉ index;␊ |
296 | long result = -1;␊ |
297 | const char * name;␊ |
298 | ␊ |
299 | if ( !plugin )␊ |
300 | {␊ |
301 | // First try 10.6's path for loading Extensions.mkext.␊ |
302 | if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0)␊ |
303 | return 0;␊ |
304 | ␊ |
305 | // Next try the legacy path.␊ |
306 | else if (FileLoadMKext(dirSpec, "") == 0)␊ |
307 | return 0;␊ |
308 | ␊ |
309 | strcat(dirSpec, "Extensions");␊ |
310 | }␊ |
311 | ␊ |
312 | index = 0;␊ |
313 | while (1) {␊ |
314 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
315 | if (ret == -1) break;␊ |
316 | ␊ |
317 | // Make sure this is a directory.␊ |
318 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
319 | ␊ |
320 | // Make sure this is a kext.␊ |
321 | length = strlen(name);␊ |
322 | if (strcmp(name + length - 5, ".kext")) continue;␊ |
323 | ␊ |
324 | // Save the file name.␊ |
325 | strcpy(gFileName, name);␊ |
326 | ␊ |
327 | // Determine the bundle type.␊ |
328 | sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
329 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
330 | if (ret == 0) bundleType = kCFBundleType2;␊ |
331 | else bundleType = kCFBundleType3;␊ |
332 | ␊ |
333 | if (!plugin)␊ |
334 | sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
335 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
336 | ␊ |
337 | ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
338 | ␊ |
339 | if (result != 0)␊ |
340 | result = ret;␊ |
341 | ␊ |
342 | if (!plugin) ␊ |
343 | FileLoadDrivers(gDriverSpec, 1);␊ |
344 | }␊ |
345 | ␊ |
346 | return result;␊ |
347 | }␊ |
348 | ␊ |
349 | //==========================================================================␊ |
350 | // ␊ |
351 | ␊ |
352 | static long␊ |
353 | NetLoadDrivers( char * dirSpec )␊ |
354 | {␊ |
355 | long tries;␊ |
356 | ␊ |
357 | #if NODEF␊ |
358 | long cnt;␊ |
359 | ␊ |
360 | // Get the name of the kernel␊ |
361 | cnt = strlen(gBootFile);␊ |
362 | while (cnt--) {␊ |
363 | if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {␊ |
364 | cnt++;␊ |
365 | break;␊ |
366 | }␊ |
367 | }␊ |
368 | #endif␊ |
369 | ␊ |
370 | // INTEL modification␊ |
371 | sprintf(gDriverSpec, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
372 | ␊ |
373 | verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
374 | ␊ |
375 | tries = 3;␊ |
376 | while (tries--)␊ |
377 | {␊ |
378 | if (LoadDriverMKext(gDriverSpec) == 0) break;␊ |
379 | }␊ |
380 | if (tries == -1) return -1;␊ |
381 | ␊ |
382 | return 0;␊ |
383 | }␊ |
384 | ␊ |
385 | //==========================================================================␊ |
386 | // loadDriverMKext␊ |
387 | ␊ |
388 | static long␊ |
389 | LoadDriverMKext( char * fileSpec )␊ |
390 | {␊ |
391 | unsigned long driversAddr, driversLength;␊ |
392 | long length;␊ |
393 | char segName[32];␊ |
394 | DriversPackage * package;␊ |
395 | ␊ |
396 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
397 | ␊ |
398 | // Load the MKext.␊ |
399 | length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
400 | if (length < sizeof (DriversPackage)) return -1;␊ |
401 | ␊ |
402 | ␉// call hook to notify modules that the mkext has been loaded␊ |
403 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);␊ |
404 | ␊ |
405 | ␉␊ |
406 | // Verify the MKext.␊ |
407 | if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
408 | ( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
409 | ( GetPackageElement(length) > kLoadSize ) ||␊ |
410 | ( GetPackageElement(adler32) !=␊ |
411 | Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
412 | {␊ |
413 | return -1;␊ |
414 | }␊ |
415 | ␊ |
416 | // Make space for the MKext.␊ |
417 | driversLength = GetPackageElement(length);␊ |
418 | driversAddr = AllocateKernelMemory(driversLength);␊ |
419 | ␊ |
420 | // Copy the MKext.␊ |
421 | memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
422 | ␊ |
423 | // Add the MKext to the memory map.␊ |
424 | sprintf(segName, "DriversPackage-%lx", driversAddr);␊ |
425 | AllocateMemoryRange(segName, driversAddr, driversLength,␊ |
426 | kBootDriverTypeMKEXT);␊ |
427 | ␊ |
428 | return 0;␊ |
429 | }␊ |
430 | ␊ |
431 | //==========================================================================␊ |
432 | // LoadDriverPList␊ |
433 | ␊ |
434 | static long␊ |
435 | LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
436 | {␊ |
437 | long length, executablePathLength, bundlePathLength;␊ |
438 | ModulePtr module;␊ |
439 | TagPtr personalities;␊ |
440 | char * buffer = 0;␊ |
441 | char * tmpExecutablePath = 0;␊ |
442 | char * tmpBundlePath = 0;␊ |
443 | long ret = -1;␊ |
444 | ␊ |
445 | do {␊ |
446 | // Save the driver path.␊ |
447 | ␊ |
448 | sprintf(gFileSpec, "%s/%s/%s", dirSpec, name,␊ |
449 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
450 | executablePathLength = strlen(gFileSpec) + 1;␊ |
451 | ␊ |
452 | tmpExecutablePath = malloc(executablePathLength);␊ |
453 | if (tmpExecutablePath == 0) break;␊ |
454 | ␊ |
455 | strcpy(tmpExecutablePath, gFileSpec);␊ |
456 | ␊ |
457 | sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
458 | bundlePathLength = strlen(gFileSpec) + 1;␊ |
459 | ␊ |
460 | tmpBundlePath = malloc(bundlePathLength);␊ |
461 | if (tmpBundlePath == 0) break;␊ |
462 | ␊ |
463 | strcpy(tmpBundlePath, gFileSpec);␊ |
464 | ␊ |
465 | // Construct the file spec to the plist, then load it.␊ |
466 | ␊ |
467 | sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
468 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
469 | ␊ |
470 | length = LoadFile(gFileSpec);␊ |
471 | if (length == -1) break;␊ |
472 | ␊ |
473 | length = length + 1;␊ |
474 | buffer = malloc(length);␊ |
475 | if (buffer == 0) break;␊ |
476 | ␊ |
477 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
478 | ␊ |
479 | // Parse the plist.␊ |
480 | ␊ |
481 | ret = ParseXML(buffer, &module, &personalities);␊ |
482 | if (ret != 0) { break; }␊ |
483 | ␊ |
484 | // Allocate memory for the driver path and the plist.␊ |
485 | ␊ |
486 | module->executablePath = tmpExecutablePath;␊ |
487 | module->bundlePath = tmpBundlePath;␊ |
488 | module->bundlePathLength = bundlePathLength;␊ |
489 | module->plistAddr = malloc(length);␊ |
490 | ␊ |
491 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
492 | break;␊ |
493 | ␊ |
494 | // Save the driver path in the module.␊ |
495 | //strcpy(module->driverPath, tmpDriverPath);␊ |
496 | tmpExecutablePath = 0;␊ |
497 | tmpBundlePath = 0;␊ |
498 | ␊ |
499 | // Add the plist to the module.␊ |
500 | ␊ |
501 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
502 | module->plistLength = length;␊ |
503 | ␊ |
504 | // Add the module to the end of the module list.␊ |
505 | ␊ |
506 | if (gModuleHead == 0)␊ |
507 | gModuleHead = module;␊ |
508 | else␊ |
509 | gModuleTail->nextModule = module;␊ |
510 | gModuleTail = module;␊ |
511 | ␊ |
512 | // Add the persionalities to the personality list.␊ |
513 | ␊ |
514 | if (personalities) personalities = personalities->tag;␊ |
515 | while (personalities != 0)␊ |
516 | {␊ |
517 | if (gPersonalityHead == 0)␊ |
518 | gPersonalityHead = personalities->tag;␊ |
519 | else␊ |
520 | gPersonalityTail->tagNext = personalities->tag;␊ |
521 | ␊ |
522 | gPersonalityTail = personalities->tag;␊ |
523 | personalities = personalities->tagNext;␊ |
524 | }␊ |
525 | ␊ |
526 | ret = 0;␊ |
527 | }␊ |
528 | while (0);␊ |
529 | ␊ |
530 | if ( buffer ) free( buffer );␊ |
531 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
532 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
533 | ␊ |
534 | return ret;␊ |
535 | }␊ |
536 | ␊ |
537 | ␊ |
538 | //==========================================================================␊ |
539 | // LoadMatchedModules␊ |
540 | ␊ |
541 | static long␊ |
542 | LoadMatchedModules( void )␊ |
543 | {␊ |
544 | ␉TagPtr␉␉ prop;␊ |
545 | ␉ModulePtr␉ module;␊ |
546 | ␉char␉␉ *fileName, segName[32];␊ |
547 | ␉DriverInfoPtr driver;␊ |
548 | ␉long␉␉ length, driverAddr, driverLength;␊ |
549 | ␉void␉␉ *executableAddr = 0;␊ |
550 | ␊ |
551 | ␊ |
552 | ␉module = gModuleHead;␊ |
553 | ␊ |
554 | ␉while (module != 0)␊ |
555 | ␉{␊ |
556 | ␉␉if (module->willLoad)␊ |
557 | ␉␉{␊ |
558 | ␉␉␉prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
559 | ␊ |
560 | ␉␉␉if (prop != 0)␊ |
561 | ␉␉␉{␊ |
562 | ␉␉␉␉fileName = prop->string;␊ |
563 | ␉␉␉␉sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
564 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
565 | ␉␉␉␉if (length == 0)␊ |
566 | ␉␉␉␉{␊ |
567 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
568 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
569 | ␉␉␉␉}␊ |
570 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
571 | ␉␉␉}␊ |
572 | ␉␉␉else␊ |
573 | ␉␉␉␉length = 0;␊ |
574 | ␊ |
575 | ␉␉␉if (length != -1)␊ |
576 | ␉␉␉{␊ |
577 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
578 | //␉␉␉␉if (length != 0)␊ |
579 | //␉␉␉␉{␊ |
580 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
581 | //␉␉␉␉}␊ |
582 | ␊ |
583 | ␉␉␉␉// Make make in the image area.␊ |
584 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
585 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
586 | ␊ |
587 | ␉␉␉␉// Set up the DriverInfo.␊ |
588 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
589 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
590 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
591 | ␉␉␉␉if (length != 0)␊ |
592 | ␉␉␉␉{␊ |
593 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
594 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
595 | ␉␉␉␉␉driver->executableLength = length;␊ |
596 | ␉␉␉␉}␊ |
597 | ␉␉␉␉else␊ |
598 | ␉␉␉␉{␊ |
599 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
600 | ␉␉␉␉␉driver->executableLength = 0;␊ |
601 | ␉␉␉␉}␊ |
602 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
603 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
604 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
605 | ␊ |
606 | ␉␉␉␉// Save the plist, module and bundle.␊ |
607 | ␉␉␉␉strcpy(driver->plistAddr, module->plistAddr);␊ |
608 | ␉␉␉␉if (length != 0)␊ |
609 | ␉␉␉␉{␊ |
610 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
611 | ␉␉␉␉}␊ |
612 | ␉␉␉␉strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
613 | ␊ |
614 | ␉␉␉␉// Add an entry to the memory map.␊ |
615 | ␉␉␉␉sprintf(segName, "Driver-%lx", (unsigned long)driver);␊ |
616 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
617 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
618 | ␉␉␉}␊ |
619 | ␉␉}␊ |
620 | ␉␉module = module->nextModule;␊ |
621 | ␉}␊ |
622 | ␊ |
623 | ␉return 0;␊ |
624 | }␊ |
625 | ␊ |
626 | //==========================================================================␊ |
627 | // MatchPersonalities␊ |
628 | ␊ |
629 | static long␊ |
630 | MatchPersonalities( void )␊ |
631 | {␊ |
632 | /* IONameMatch support not implemented */␊ |
633 | return 0;␊ |
634 | }␊ |
635 | ␊ |
636 | //==========================================================================␊ |
637 | // MatchLibraries␊ |
638 | ␊ |
639 | static long␊ |
640 | MatchLibraries( void )␊ |
641 | {␊ |
642 | TagPtr prop, prop2;␊ |
643 | ModulePtr module, module2;␊ |
644 | long done;␊ |
645 | ␊ |
646 | do {␊ |
647 | done = 1;␊ |
648 | module = gModuleHead;␊ |
649 | ␊ |
650 | while (module != 0)␊ |
651 | {␊ |
652 | if (module->willLoad == 1)␊ |
653 | {␊ |
654 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
655 | if (prop != 0)␊ |
656 | {␊ |
657 | prop = prop->tag;␊ |
658 | while (prop != 0)␊ |
659 | {␊ |
660 | module2 = gModuleHead;␊ |
661 | while (module2 != 0)␊ |
662 | {␊ |
663 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
664 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
665 | {␊ |
666 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
667 | break;␊ |
668 | }␊ |
669 | module2 = module2->nextModule;␊ |
670 | }␊ |
671 | prop = prop->tagNext;␊ |
672 | }␊ |
673 | }␊ |
674 | module->willLoad = 2;␊ |
675 | done = 0;␊ |
676 | }␊ |
677 | module = module->nextModule;␊ |
678 | }␊ |
679 | }␊ |
680 | while (!done);␊ |
681 | ␊ |
682 | return 0;␊ |
683 | }␊ |
684 | ␊ |
685 | ␊ |
686 | //==========================================================================␊ |
687 | // FindModule␊ |
688 | ␊ |
689 | #if NOTDEF␊ |
690 | static ModulePtr␊ |
691 | FindModule( char * name )␊ |
692 | {␊ |
693 | ModulePtr module;␊ |
694 | TagPtr prop;␊ |
695 | ␊ |
696 | module = gModuleHead;␊ |
697 | ␊ |
698 | while (module != 0)␊ |
699 | {␊ |
700 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
701 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
702 | module = module->nextModule;␊ |
703 | }␊ |
704 | ␊ |
705 | return module;␊ |
706 | }␊ |
707 | #endif /* NOTDEF */␊ |
708 | ␊ |
709 | //==========================================================================␊ |
710 | // ParseXML␊ |
711 | ␊ |
712 | static long␊ |
713 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
714 | {␊ |
715 | ␉long length, pos;␊ |
716 | ␉TagPtr moduleDict, required;␊ |
717 | ␉ModulePtr tmpModule;␊ |
718 | ␊ |
719 | pos = 0;␊ |
720 | ␊ |
721 | while (1)␊ |
722 | {␊ |
723 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
724 | if (length == -1) break;␊ |
725 | ␊ |
726 | pos += length;␊ |
727 | ␊ |
728 | if (moduleDict == 0) continue;␊ |
729 | if (moduleDict->type == kTagTypeDict) break;␊ |
730 | ␊ |
731 | XMLFreeTag(moduleDict);␊ |
732 | }␊ |
733 | ␊ |
734 | if (length == -1) return -1;␊ |
735 | ␊ |
736 | required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
737 | if ( (required == 0) ||␊ |
738 | (required->type != kTagTypeString) ||␊ |
739 | !strcmp(required->string, "Safe Boot"))␊ |
740 | {␊ |
741 | XMLFreeTag(moduleDict);␊ |
742 | return -2;␊ |
743 | }␊ |
744 | ␊ |
745 | tmpModule = malloc(sizeof(Module));␊ |
746 | if (tmpModule == 0)␊ |
747 | {␊ |
748 | XMLFreeTag(moduleDict);␊ |
749 | return -1;␊ |
750 | }␊ |
751 | tmpModule->dict = moduleDict;␊ |
752 | ␊ |
753 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
754 | ␊ |
755 | tmpModule->willLoad = 1;␊ |
756 | ␊ |
757 | *module = tmpModule;␊ |
758 | ␊ |
759 | // Get the personalities.␊ |
760 | ␊ |
761 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
762 | ␊ |
763 | return 0;␊ |
764 | }␊ |
765 | ␊ |
766 | #if NOTDEF␊ |
767 | static char gPlatformName[64];␊ |
768 | #endif␊ |
769 | ␊ |
770 | long ␊ |
771 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
772 | {␊ |
773 | ␉long ret;␊ |
774 | ␉compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
775 | ␉u_int32_t uncompressed_size, size;␊ |
776 | ␉void *buffer;␊ |
777 | ␉unsigned long len;␊ |
778 | ␉␊ |
779 | #if 0␊ |
780 | ␉printf("kernel header:\n");␊ |
781 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
782 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
783 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
784 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
785 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
786 | ␉getchar();␊ |
787 | #endif␊ |
788 | ␉␊ |
789 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
790 | ␉{␊ |
791 | ␉␉if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))␊ |
792 | ␉␉{␊ |
793 | ␉␉␉error("kernel compression is bad\n");␊ |
794 | ␉␉␉return -1;␊ |
795 | ␉␉}␊ |
796 | #if NOTDEF␊ |
797 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
798 | ␉␉␉return -1;␊ |
799 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
800 | ␉␉␉return -1;␊ |
801 | #endif␊ |
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 | ␉␉␊ |
812 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
813 | ␉␉␉Adler32(binary, uncompressed_size))␊ |
814 | ␉␉{␊ |
815 | ␉␉␉printf("adler mismatch\n");␊ |
816 | ␉␉␉return -1;␊ |
817 | ␉␉}␊ |
818 | ␉}␊ |
819 | ␉␊ |
820 | ␉// Notify modules that the kernel has been decompressed, is about to be decoded␊ |
821 | ␉execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);␊ |
822 | ␉␊ |
823 | ␉ret = ThinFatFile(&binary, &len);␊ |
824 | ␉ if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
825 | ␉ {␊ |
826 | ␉␉ archCpuType=CPU_TYPE_I386;␊ |
827 | ␉␉ ret = ThinFatFile(&binary, &len);␊ |
828 | ␉ }␊ |
829 | ␊ |
830 | ␉ ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
831 | ␊ |
832 | ␉ if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
833 | ␉ {␊ |
834 | ␉␉ archCpuType=CPU_TYPE_I386;␊ |
835 | ␉␉ ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
836 | ␉ }␊ |
837 | ␊ |
838 | ␉ return ret;␊ |
839 | }␊ |
840 | |