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