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