1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the 'License'). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * drivers.c - Driver Loading Functions.␊ |
26 | *␊ |
27 | * Copyright (c) 2000 Apple Computer, Inc.␊ |
28 | *␊ |
29 | * DRI: Josh de Cesare␊ |
30 | */␊ |
31 | ␊ |
32 | #include <mach-o/fat.h>␊ |
33 | #include <libkern/OSByteOrder.h>␊ |
34 | #include <mach/machine.h>␊ |
35 | ␊ |
36 | #include "sl.h"␊ |
37 | #include "boot.h"␊ |
38 | #include "bootstruct.h"␊ |
39 | #include "xml.h"␊ |
40 | #include "ramdisk.h"␊ |
41 | ␊ |
42 | struct Module { ␊ |
43 | struct Module *nextModule;␊ |
44 | long willLoad;␊ |
45 | TagPtr dict;␊ |
46 | char *plistAddr;␊ |
47 | long plistLength;␊ |
48 | char *executablePath;␊ |
49 | char *bundlePath;␊ |
50 | long bundlePathLength;␊ |
51 | };␊ |
52 | typedef struct Module Module, *ModulePtr;␊ |
53 | ␊ |
54 | struct DriverInfo {␊ |
55 | char *plistAddr;␊ |
56 | long plistLength;␊ |
57 | void *executableAddr;␊ |
58 | long executableLength;␊ |
59 | void *bundlePathAddr;␊ |
60 | long bundlePathLength;␊ |
61 | };␊ |
62 | typedef struct DriverInfo DriverInfo, *DriverInfoPtr;␊ |
63 | ␊ |
64 | #define kDriverPackageSignature1 'MKXT'␊ |
65 | #define kDriverPackageSignature2 'MOSX'␊ |
66 | ␊ |
67 | struct DriversPackage {␊ |
68 | unsigned long signature1;␊ |
69 | unsigned long signature2;␊ |
70 | unsigned long length;␊ |
71 | unsigned long alder32;␊ |
72 | unsigned long version;␊ |
73 | unsigned long numDrivers;␊ |
74 | unsigned long reserved1;␊ |
75 | unsigned long reserved2;␊ |
76 | };␊ |
77 | typedef struct DriversPackage DriversPackage;␊ |
78 | ␊ |
79 | enum {␊ |
80 | kCFBundleType2,␊ |
81 | kCFBundleType3␊ |
82 | };␊ |
83 | ␊ |
84 | long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);␊ |
85 | ␊ |
86 | static unsigned long Alder32( unsigned char * buffer, long length );␊ |
87 | ␊ |
88 | static long FileLoadDrivers(char *dirSpec, long plugin);␊ |
89 | static long NetLoadDrivers(char *dirSpec);␊ |
90 | static long LoadDriverMKext(char *fileSpec);␊ |
91 | static long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
92 | static long LoadMatchedModules(void);␊ |
93 | static long MatchPersonalities(void);␊ |
94 | static long MatchLibraries(void);␊ |
95 | #ifdef NOTDEF␊ |
96 | static ModulePtr FindModule(char *name);␊ |
97 | static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);␊ |
98 | #endif␊ |
99 | static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);␊ |
100 | static long InitDriverSupport(void);␊ |
101 | ␊ |
102 | static ModulePtr gModuleHead, gModuleTail;␊ |
103 | static TagPtr gPersonalityHead, gPersonalityTail;␊ |
104 | static char * gExtensionsSpec;␊ |
105 | static char * gDriverSpec;␊ |
106 | static char * gFileSpec;␊ |
107 | static char * gTempSpec;␊ |
108 | static char * gFileName;␊ |
109 | ␊ |
110 | static unsigned long␊ |
111 | Alder32( unsigned char * buffer, long length )␊ |
112 | {␊ |
113 | long cnt;␊ |
114 | unsigned long result, lowHalf, highHalf;␊ |
115 | ␊ |
116 | lowHalf = 1;␊ |
117 | highHalf = 0;␊ |
118 | ␊ |
119 | ␉for ( cnt = 0; cnt < length; cnt++ )␊ |
120 | {␊ |
121 | if ((cnt % 5000) == 0)␊ |
122 | {␊ |
123 | lowHalf %= 65521L;␊ |
124 | highHalf %= 65521L;␊ |
125 | }␊ |
126 | ␊ |
127 | lowHalf += buffer[cnt];␊ |
128 | highHalf += lowHalf;␊ |
129 | }␊ |
130 | ␊ |
131 | ␉lowHalf %= 65521L;␊ |
132 | ␉highHalf %= 65521L;␊ |
133 | ␊ |
134 | ␉result = (highHalf << 16) | lowHalf;␊ |
135 | ␊ |
136 | ␉return result;␊ |
137 | }␊ |
138 | ␊ |
139 | ␊ |
140 | //==========================================================================␊ |
141 | // InitDriverSupport␊ |
142 | ␊ |
143 | static long␊ |
144 | InitDriverSupport( void )␊ |
145 | {␊ |
146 | gExtensionsSpec = malloc( 4096 );␊ |
147 | gDriverSpec = malloc( 4096 );␊ |
148 | gFileSpec = malloc( 4096 );␊ |
149 | gTempSpec = malloc( 4096 );␊ |
150 | gFileName = malloc( 4096 );␊ |
151 | ␊ |
152 | if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName )␊ |
153 | stop("InitDriverSupport error");␊ |
154 | ␊ |
155 | return 0;␊ |
156 | }␊ |
157 | ␊ |
158 | //==========================================================================␊ |
159 | // LoadDrivers␊ |
160 | ␊ |
161 | long LoadDrivers( char * dirSpec )␊ |
162 | {␊ |
163 | ␉char dirSpecExtra[128];␊ |
164 | ␉const char *override_pathfolder = NULL; // full path to a folder.␊ |
165 | ␉int␉␉␉fd = 0, len = 0;␊ |
166 | ␉extern char gMacOSVersion;␊ |
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.␊ |
188 | ␉␉if (getValueForKey(kExtensionsKey, &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 apply.␊ |
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 | ␉␉␊ |
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␊ |
245 | ␉␉{␊ |
246 | ␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
247 | ␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
248 | ␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
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 | if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
277 | {␊ |
278 | ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
279 | if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeDirectory) ||␊ |
280 | (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
281 | {␊ |
282 | sprintf(gDriverSpec, "%sExtensions.mkext", altDirSpec);␊ |
283 | verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
284 | if (LoadDriverMKext(gDriverSpec) == 0) return 0;␊ |
285 | }␊ |
286 | }␊ |
287 | return -1;␊ |
288 | }␊ |
289 | ␊ |
290 | //==========================================================================␊ |
291 | // FileLoadDrivers␊ |
292 | ␊ |
293 | static long␊ |
294 | FileLoadDrivers( char * dirSpec, long plugin )␊ |
295 | {␊ |
296 | long ret, length, flags, time, bundleType;␊ |
297 | long long␉ index;␊ |
298 | long result = -1;␊ |
299 | const char * name;␊ |
300 | ␊ |
301 | if ( !plugin )␊ |
302 | {␊ |
303 | // First try 10.6's path for loading Extensions.mkext.␊ |
304 | if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0)␊ |
305 | return 0;␊ |
306 | ␊ |
307 | // Next try the legacy path.␊ |
308 | else if (FileLoadMKext(dirSpec, "") == 0)␊ |
309 | return 0;␊ |
310 | ␊ |
311 | strcat(dirSpec, "Extensions");␊ |
312 | }␊ |
313 | ␊ |
314 | index = 0;␊ |
315 | while (1) {␊ |
316 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
317 | if (ret == -1) break;␊ |
318 | ␊ |
319 | // Make sure this is a directory.␊ |
320 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
321 | ␊ |
322 | // Make sure this is a kext.␊ |
323 | length = strlen(name);␊ |
324 | if (strcmp(name + length - 5, ".kext")) continue;␊ |
325 | ␊ |
326 | // Save the file name.␊ |
327 | strcpy(gFileName, name);␊ |
328 | ␊ |
329 | // Determine the bundle type.␊ |
330 | sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
331 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
332 | if (ret == 0) bundleType = kCFBundleType2;␊ |
333 | else bundleType = kCFBundleType3;␊ |
334 | ␊ |
335 | if (!plugin)␊ |
336 | sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
337 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
338 | ␊ |
339 | ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
340 | ␊ |
341 | if (result != 0)␊ |
342 | result = ret;␊ |
343 | ␊ |
344 | if (!plugin) ␊ |
345 | FileLoadDrivers(gDriverSpec, 1);␊ |
346 | }␊ |
347 | ␊ |
348 | return result;␊ |
349 | }␊ |
350 | ␊ |
351 | //==========================================================================␊ |
352 | // ␊ |
353 | ␊ |
354 | static long␊ |
355 | NetLoadDrivers( char * dirSpec )␊ |
356 | {␊ |
357 | long tries;␊ |
358 | ␊ |
359 | #if NODEF␊ |
360 | long cnt;␊ |
361 | ␊ |
362 | // Get the name of the kernel␊ |
363 | cnt = strlen(gBootFile);␊ |
364 | while (cnt--) {␊ |
365 | if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {␊ |
366 | cnt++;␊ |
367 | break;␊ |
368 | }␊ |
369 | }␊ |
370 | #endif␊ |
371 | ␊ |
372 | // INTEL modification␊ |
373 | sprintf(gDriverSpec, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
374 | ␊ |
375 | verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
376 | ␊ |
377 | tries = 3;␊ |
378 | while (tries--)␊ |
379 | {␊ |
380 | if (LoadDriverMKext(gDriverSpec) == 0) break;␊ |
381 | }␊ |
382 | if (tries == -1) return -1;␊ |
383 | ␊ |
384 | return 0;␊ |
385 | }␊ |
386 | ␊ |
387 | //==========================================================================␊ |
388 | // loadDriverMKext␊ |
389 | ␊ |
390 | static long␊ |
391 | LoadDriverMKext( char * fileSpec )␊ |
392 | {␊ |
393 | unsigned long driversAddr, driversLength;␊ |
394 | long length;␊ |
395 | char segName[32];␊ |
396 | DriversPackage * package;␊ |
397 | ␊ |
398 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
399 | ␊ |
400 | // Load the MKext.␊ |
401 | length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
402 | if (length < sizeof (DriversPackage)) return -1;␊ |
403 | ␊ |
404 | // Verify the MKext.␊ |
405 | if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
406 | ( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
407 | ( GetPackageElement(length) > kLoadSize ) ||␊ |
408 | ( GetPackageElement(alder32) !=␊ |
409 | Alder32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
410 | {␊ |
411 | return -1;␊ |
412 | }␊ |
413 | ␊ |
414 | // Make space for the MKext.␊ |
415 | driversLength = GetPackageElement(length);␊ |
416 | driversAddr = AllocateKernelMemory(driversLength);␊ |
417 | ␊ |
418 | // Copy the MKext.␊ |
419 | memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
420 | ␊ |
421 | // Add the MKext to the memory map.␊ |
422 | sprintf(segName, "DriversPackage-%lx", driversAddr);␊ |
423 | AllocateMemoryRange(segName, driversAddr, driversLength,␊ |
424 | kBootDriverTypeMKEXT);␊ |
425 | ␊ |
426 | return 0;␊ |
427 | }␊ |
428 | ␊ |
429 | //==========================================================================␊ |
430 | // LoadDriverPList␊ |
431 | ␊ |
432 | static long␊ |
433 | LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
434 | {␊ |
435 | long length, executablePathLength, bundlePathLength;␊ |
436 | ModulePtr module;␊ |
437 | TagPtr personalities;␊ |
438 | char * buffer = 0;␊ |
439 | char * tmpExecutablePath = 0;␊ |
440 | char * tmpBundlePath = 0;␊ |
441 | long ret = -1;␊ |
442 | ␊ |
443 | do {␊ |
444 | // Save the driver path.␊ |
445 | ␊ |
446 | sprintf(gFileSpec, "%s/%s/%s", dirSpec, name,␊ |
447 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
448 | executablePathLength = strlen(gFileSpec) + 1;␊ |
449 | ␊ |
450 | tmpExecutablePath = malloc(executablePathLength);␊ |
451 | if (tmpExecutablePath == 0) break;␊ |
452 | ␊ |
453 | strcpy(tmpExecutablePath, gFileSpec);␊ |
454 | ␊ |
455 | sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
456 | bundlePathLength = strlen(gFileSpec) + 1;␊ |
457 | ␊ |
458 | tmpBundlePath = malloc(bundlePathLength);␊ |
459 | if (tmpBundlePath == 0) break;␊ |
460 | ␊ |
461 | strcpy(tmpBundlePath, gFileSpec);␊ |
462 | ␊ |
463 | // Construct the file spec to the plist, then load it.␊ |
464 | ␊ |
465 | sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
466 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
467 | ␊ |
468 | length = LoadFile(gFileSpec);␊ |
469 | if (length == -1) break;␊ |
470 | ␊ |
471 | length = length + 1;␊ |
472 | buffer = malloc(length);␊ |
473 | if (buffer == 0) break;␊ |
474 | ␊ |
475 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
476 | ␊ |
477 | // Parse the plist.␊ |
478 | ␊ |
479 | ret = ParseXML(buffer, &module, &personalities);␊ |
480 | if (ret != 0) { break; }␊ |
481 | ␊ |
482 | // Allocate memory for the driver path and the plist.␊ |
483 | ␊ |
484 | module->executablePath = tmpExecutablePath;␊ |
485 | module->bundlePath = tmpBundlePath;␊ |
486 | module->bundlePathLength = bundlePathLength;␊ |
487 | module->plistAddr = malloc(length);␊ |
488 | ␊ |
489 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
490 | break;␊ |
491 | ␊ |
492 | // Save the driver path in the module.␊ |
493 | //strcpy(module->driverPath, tmpDriverPath);␊ |
494 | tmpExecutablePath = 0;␊ |
495 | tmpBundlePath = 0;␊ |
496 | ␊ |
497 | // Add the plist to the module.␊ |
498 | ␊ |
499 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
500 | module->plistLength = length;␊ |
501 | ␊ |
502 | // Add the module to the end of the module list.␊ |
503 | ␊ |
504 | if (gModuleHead == 0)␊ |
505 | gModuleHead = module;␊ |
506 | else␊ |
507 | gModuleTail->nextModule = module;␊ |
508 | gModuleTail = module;␊ |
509 | ␊ |
510 | // Add the persionalities to the personality list.␊ |
511 | ␊ |
512 | if (personalities) personalities = personalities->tag;␊ |
513 | while (personalities != 0)␊ |
514 | {␊ |
515 | if (gPersonalityHead == 0)␊ |
516 | gPersonalityHead = personalities->tag;␊ |
517 | else␊ |
518 | gPersonalityTail->tagNext = personalities->tag;␊ |
519 | ␊ |
520 | gPersonalityTail = personalities->tag;␊ |
521 | personalities = personalities->tagNext;␊ |
522 | }␊ |
523 | ␊ |
524 | ret = 0;␊ |
525 | }␊ |
526 | while (0);␊ |
527 | ␊ |
528 | if ( buffer ) free( buffer );␊ |
529 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
530 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
531 | ␊ |
532 | return ret;␊ |
533 | }␊ |
534 | ␊ |
535 | ␊ |
536 | //==========================================================================␊ |
537 | // LoadMatchedModules␊ |
538 | ␊ |
539 | static long␊ |
540 | LoadMatchedModules( void )␊ |
541 | {␊ |
542 | TagPtr prop;␊ |
543 | ModulePtr module;␊ |
544 | char *fileName, segName[32];␊ |
545 | DriverInfoPtr driver;␊ |
546 | long length, driverAddr, driverLength;␊ |
547 | void *executableAddr = 0;␊ |
548 | ␊ |
549 | ␊ |
550 | module = gModuleHead;␊ |
551 | ␊ |
552 | while (module != 0)␊ |
553 | {␊ |
554 | if (module->willLoad)␊ |
555 | {␊ |
556 | prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
557 | ␊ |
558 | if (prop != 0)␊ |
559 | {␊ |
560 | fileName = prop->string;␊ |
561 | sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
562 | length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
563 | ␉␉␉␉if (length == 0)␊ |
564 | ␉␉␉␉{␊ |
565 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
566 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
567 | ␉␉␉␉}␊ |
568 | //printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getc();␊ |
569 | }␊ |
570 | else␊ |
571 | length = 0;␊ |
572 | ␊ |
573 | if (length != -1)␊ |
574 | {␊ |
575 | ␉␉//driverModuleAddr = (void *)kLoadAddr;␊ |
576 | //if (length != 0)␊ |
577 | //{␊ |
578 | ␉␉// ThinFatFile(&driverModuleAddr, &length);␊ |
579 | ␉␉//}␊ |
580 | ␊ |
581 | // Make make in the image area.␊ |
582 | driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
583 | driverAddr = AllocateKernelMemory(driverLength);␊ |
584 | ␊ |
585 | // Set up the DriverInfo.␊ |
586 | driver = (DriverInfoPtr)driverAddr;␊ |
587 | driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
588 | driver->plistLength = module->plistLength;␊ |
589 | if (length != 0)␊ |
590 | {␊ |
591 | driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
592 | ␉␉␉␉␉ module->plistLength);␊ |
593 | driver->executableLength = length;␊ |
594 | }␊ |
595 | else␊ |
596 | {␊ |
597 | driver->executableAddr = 0;␊ |
598 | driver->executableLength = 0;␊ |
599 | }␊ |
600 | driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
601 | ␉␉␉␉ module->plistLength + driver->executableLength);␊ |
602 | driver->bundlePathLength = module->bundlePathLength;␊ |
603 | ␊ |
604 | // Save the plist, module and bundle.␊ |
605 | strcpy(driver->plistAddr, module->plistAddr);␊ |
606 | if (length != 0)␊ |
607 | {␊ |
608 | memcpy(driver->executableAddr, executableAddr, length);␊ |
609 | }␊ |
610 | strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
611 | ␊ |
612 | // Add an entry to the memory map.␊ |
613 | sprintf(segName, "Driver-%lx", (unsigned long)driver);␊ |
614 | AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
615 | kBootDriverTypeKEXT);␊ |
616 | }␊ |
617 | }␊ |
618 | module = module->nextModule;␊ |
619 | }␊ |
620 | ␊ |
621 | return 0;␊ |
622 | }␊ |
623 | ␊ |
624 | //==========================================================================␊ |
625 | // MatchPersonalities␊ |
626 | ␊ |
627 | static long␊ |
628 | MatchPersonalities( void )␊ |
629 | {␊ |
630 | /* IONameMatch support not implemented */␊ |
631 | return 0;␊ |
632 | }␊ |
633 | ␊ |
634 | //==========================================================================␊ |
635 | // MatchLibraries␊ |
636 | ␊ |
637 | static long␊ |
638 | MatchLibraries( void )␊ |
639 | {␊ |
640 | TagPtr prop, prop2;␊ |
641 | ModulePtr module, module2;␊ |
642 | long done;␊ |
643 | ␊ |
644 | do {␊ |
645 | done = 1;␊ |
646 | module = gModuleHead;␊ |
647 | ␊ |
648 | while (module != 0)␊ |
649 | {␊ |
650 | if (module->willLoad == 1)␊ |
651 | {␊ |
652 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
653 | if (prop != 0)␊ |
654 | {␊ |
655 | prop = prop->tag;␊ |
656 | while (prop != 0)␊ |
657 | {␊ |
658 | module2 = gModuleHead;␊ |
659 | while (module2 != 0)␊ |
660 | {␊ |
661 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
662 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
663 | {␊ |
664 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
665 | break;␊ |
666 | }␊ |
667 | module2 = module2->nextModule;␊ |
668 | }␊ |
669 | prop = prop->tagNext;␊ |
670 | }␊ |
671 | }␊ |
672 | module->willLoad = 2;␊ |
673 | done = 0;␊ |
674 | }␊ |
675 | module = module->nextModule;␊ |
676 | }␊ |
677 | }␊ |
678 | while (!done);␊ |
679 | ␊ |
680 | return 0;␊ |
681 | }␊ |
682 | ␊ |
683 | ␊ |
684 | //==========================================================================␊ |
685 | // FindModule␊ |
686 | ␊ |
687 | #if NOTDEF␊ |
688 | static ModulePtr␊ |
689 | FindModule( char * name )␊ |
690 | {␊ |
691 | ModulePtr module;␊ |
692 | TagPtr prop;␊ |
693 | ␊ |
694 | module = gModuleHead;␊ |
695 | ␊ |
696 | while (module != 0)␊ |
697 | {␊ |
698 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
699 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
700 | module = module->nextModule;␊ |
701 | }␊ |
702 | ␊ |
703 | return module;␊ |
704 | }␊ |
705 | #endif /* NOTDEF */␊ |
706 | ␊ |
707 | //==========================================================================␊ |
708 | // ParseXML␊ |
709 | ␊ |
710 | static long␊ |
711 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
712 | {␊ |
713 | ␉long length, pos;␊ |
714 | ␉TagPtr moduleDict, required;␊ |
715 | ␉ModulePtr tmpModule;␊ |
716 | ␊ |
717 | pos = 0;␊ |
718 | ␊ |
719 | while (1)␊ |
720 | {␊ |
721 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
722 | if (length == -1) break;␊ |
723 | ␊ |
724 | pos += length;␊ |
725 | ␊ |
726 | if (moduleDict == 0) continue;␊ |
727 | if (moduleDict->type == kTagTypeDict) break;␊ |
728 | ␊ |
729 | XMLFreeTag(moduleDict);␊ |
730 | }␊ |
731 | ␊ |
732 | if (length == -1) return -1;␊ |
733 | ␊ |
734 | required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
735 | if ( (required == 0) ||␊ |
736 | (required->type != kTagTypeString) ||␊ |
737 | !strcmp(required->string, "Safe Boot"))␊ |
738 | {␊ |
739 | XMLFreeTag(moduleDict);␊ |
740 | return -2;␊ |
741 | }␊ |
742 | ␊ |
743 | tmpModule = malloc(sizeof(Module));␊ |
744 | if (tmpModule == 0)␊ |
745 | {␊ |
746 | XMLFreeTag(moduleDict);␊ |
747 | return -1;␊ |
748 | }␊ |
749 | tmpModule->dict = moduleDict;␊ |
750 | ␊ |
751 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
752 | ␊ |
753 | tmpModule->willLoad = 1;␊ |
754 | ␊ |
755 | *module = tmpModule;␊ |
756 | ␊ |
757 | // Get the personalities.␊ |
758 | ␊ |
759 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
760 | ␊ |
761 | return 0;␊ |
762 | }␊ |
763 | ␊ |
764 | #if NOTDEF␊ |
765 | static char gPlatformName[64];␊ |
766 | #endif␊ |
767 | ␊ |
768 | long ␊ |
769 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
770 | {␊ |
771 | long ret;␊ |
772 | compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
773 | u_int32_t uncompressed_size, size;␊ |
774 | void *buffer;␊ |
775 | ␉unsigned long len;␊ |
776 | ␊ |
777 | #if 0␊ |
778 | printf("kernel header:\n");␊ |
779 | printf("signature: 0x%x\n", kernel_header->signature);␊ |
780 | printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
781 | printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
782 | printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
783 | printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
784 | getc();␊ |
785 | #endif␊ |
786 | ␊ |
787 | if (kernel_header->signature == OSSwapBigToHostConstInt32('comp')) {␊ |
788 | if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) {␊ |
789 | error("kernel compression is bad\n");␊ |
790 | return -1;␊ |
791 | }␊ |
792 | #if NOTDEF␊ |
793 | if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
794 | return -1;␊ |
795 | if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
796 | return -1;␊ |
797 | #endif␊ |
798 | ␊ |
799 | uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
800 | binary = buffer = malloc(uncompressed_size);␊ |
801 | ␊ |
802 | size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
803 | OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
804 | if (uncompressed_size != size) {␊ |
805 | error("size mismatch from lzss: %x\n", size);␊ |
806 | return -1;␊ |
807 | }␊ |
808 | if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
809 | Alder32(binary, uncompressed_size)) {␊ |
810 | printf("adler mismatch\n");␊ |
811 | return -1;␊ |
812 | }␊ |
813 | }␊ |
814 | ␊ |
815 | ret = ThinFatFile(&binary, &len);␊ |
816 | if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
817 | {␊ |
818 | ␉ archCpuType=CPU_TYPE_I386;␊ |
819 | ␉ ret = ThinFatFile(&binary, &len);␊ |
820 | }␊ |
821 | ␊ |
822 | ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
823 | ␉␊ |
824 | if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
825 | {␊ |
826 | ␉ archCpuType=CPU_TYPE_I386;␊ |
827 | ␉ ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
828 | }␊ |
829 | ␊ |
830 | return ret;␊ |
831 | }␊ |
832 | |