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 | //␉␉msglog("(%s) Extensions time +1 = %d\n", __FUNCTION__, time2 + 1); //Azi: don't delete the one below, you...!␊ |
283 | ␉␉␊ |
284 | ␉␉if ((ret != 0)␊ |
285 | ␉␉␉|| ((flags & kFileTypeMask) != kFileTypeDirectory)␊ |
286 | ␉␉␉|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
287 | ␉␉{␊ |
288 | ␉␉␉sprintf(gDriverSpec, "%sExtensions.mkext", altDirSpec);␊ |
289 | ␉␉␉msglog("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
290 | ␉␉␉␊ |
291 | ␉␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
292 | ␉␉␉␉return 0;␊ |
293 | ␉␉}␊ |
294 | ␉}␊ |
295 | ␉return -1;␊ |
296 | }␊ |
297 | ␊ |
298 | //==========================================================================␊ |
299 | // FileLoadDrivers␊ |
300 | ␊ |
301 | static long␊ |
302 | FileLoadDrivers( char * dirSpec, long plugin )␊ |
303 | {␊ |
304 | long ret, length, flags, time, bundleType;␊ |
305 | long long␉ index;␊ |
306 | long result = -1;␊ |
307 | const char * name;␊ |
308 | ␊ |
309 | if ( !plugin )␊ |
310 | {␊ |
311 | // First try 10.6's path for loading Extensions.mkext.␊ |
312 | if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0)␊ |
313 | ␉␉␉return 0; // "bt(0,0)/Extra/10.6/"␊ |
314 | ␊ |
315 | // Next try the legacy path.␊ |
316 | else if (FileLoadMKext(dirSpec, "") == 0)␊ |
317 | return 0;␊ |
318 | ␊ |
319 | strcat(dirSpec, "Extensions");␊ |
320 | }␊ |
321 | ␊ |
322 | index = 0;␊ |
323 | while (1) {␊ |
324 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
325 | if (ret == -1) break;␊ |
326 | ␊ |
327 | // Make sure this is a directory.␊ |
328 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
329 | ␊ |
330 | // Make sure this is a kext.␊ |
331 | length = strlen(name);␊ |
332 | if (strcmp(name + length - 5, ".kext")) continue;␊ |
333 | ␊ |
334 | // Save the file name.␊ |
335 | strcpy(gFileName, name);␊ |
336 | ␊ |
337 | // Determine the bundle type.␊ |
338 | sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
339 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
340 | if (ret == 0) bundleType = kCFBundleType2;␊ |
341 | else bundleType = kCFBundleType3;␊ |
342 | ␊ |
343 | if (!plugin)␊ |
344 | sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
345 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
346 | ␊ |
347 | ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
348 | ␊ |
349 | if (result != 0)␊ |
350 | result = ret;␊ |
351 | ␊ |
352 | if (!plugin) ␊ |
353 | FileLoadDrivers(gDriverSpec, 1);␊ |
354 | }␊ |
355 | ␊ |
356 | return result;␊ |
357 | }␊ |
358 | ␊ |
359 | //==========================================================================␊ |
360 | // ␊ |
361 | ␊ |
362 | static long␊ |
363 | NetLoadDrivers( char * dirSpec )␊ |
364 | {␊ |
365 | long tries;␊ |
366 | ␊ |
367 | #if NODEF␊ |
368 | long cnt;␊ |
369 | ␊ |
370 | // Get the name of the kernel␊ |
371 | cnt = strlen(gBootFile);␊ |
372 | while (cnt--) {␊ |
373 | if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {␊ |
374 | cnt++;␊ |
375 | break;␊ |
376 | }␊ |
377 | }␊ |
378 | #endif␊ |
379 | ␊ |
380 | // INTEL modification␊ |
381 | sprintf(gDriverSpec, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
382 | ␊ |
383 | verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
384 | ␊ |
385 | tries = 3;␊ |
386 | while (tries--)␊ |
387 | {␊ |
388 | if (LoadDriverMKext(gDriverSpec) == 0) break;␊ |
389 | }␊ |
390 | if (tries == -1) return -1;␊ |
391 | ␊ |
392 | return 0;␊ |
393 | }␊ |
394 | ␊ |
395 | //==========================================================================␊ |
396 | // loadDriverMKext␊ |
397 | ␊ |
398 | static long␊ |
399 | LoadDriverMKext( char * fileSpec )␊ |
400 | {␊ |
401 | unsigned long driversAddr, driversLength;␊ |
402 | long length;␊ |
403 | char segName[32];␊ |
404 | DriversPackage * package;␊ |
405 | ␊ |
406 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
407 | ␊ |
408 | // Load the MKext.␊ |
409 | length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
410 | if (length < sizeof (DriversPackage)) return -1;␊ |
411 | ␊ |
412 | ␉// call hook to notify modules that the mkext has been loaded␊ |
413 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);␊ |
414 | ␊ |
415 | ␉␊ |
416 | // Verify the MKext.␊ |
417 | if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
418 | ( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
419 | ( GetPackageElement(length) > kLoadSize ) ||␊ |
420 | ( GetPackageElement(adler32) !=␊ |
421 | Adler32((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); getchar();␊ |
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 | ␉getchar();␊ |
797 | #endif␊ |
798 | ␉␊ |
799 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
800 | ␉{␊ |
801 | ␉␉if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))␊ |
802 | ␉␉{␊ |
803 | ␉␉␉error("kernel compression is bad\n");␊ |
804 | ␉␉␉return -1;␊ |
805 | ␉␉}␊ |
806 | #if NOTDEF␊ |
807 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
808 | ␉␉␉return -1;␊ |
809 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
810 | ␉␉␉return -1;␊ |
811 | #endif␊ |
812 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
813 | ␉␉binary = buffer = malloc(uncompressed_size);␊ |
814 | ␉␉␊ |
815 | ␉␉size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
816 | ␉␉␉␉␉␉␉ OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
817 | ␉␉if (uncompressed_size != size) {␊ |
818 | ␉␉␉error("size mismatch from lzss: %x\n", size);␊ |
819 | ␉␉␉return -1;␊ |
820 | ␉␉}␊ |
821 | ␉␉␊ |
822 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
823 | ␉␉␉Adler32(binary, uncompressed_size))␊ |
824 | ␉␉{␊ |
825 | ␉␉␉printf("adler mismatch\n");␊ |
826 | ␉␉␉return -1;␊ |
827 | ␉␉}␊ |
828 | ␉}␊ |
829 | ␉␊ |
830 | ␉// Notify modules that the kernel has been decompressed, is about to be decoded␊ |
831 | ␉execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);␊ |
832 | ␉␊ |
833 | ␉ret = ThinFatFile(&binary, &len);␊ |
834 | ␉ if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
835 | ␉ {␊ |
836 | ␉␉ archCpuType=CPU_TYPE_I386;␊ |
837 | ␉␉ ret = ThinFatFile(&binary, &len);␊ |
838 | ␉ }␊ |
839 | ␊ |
840 | ␉ ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
841 | ␊ |
842 | ␉ if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
843 | ␉ {␊ |
844 | ␉␉ archCpuType=CPU_TYPE_I386;␊ |
845 | ␉␉ ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
846 | ␉ }␊ |
847 | ␊ |
848 | ␉ return ret;␊ |
849 | }␊ |
850 | |