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