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