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