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