1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * ␊ |
5 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
6 | * Reserved. This file contains Original Code and/or Modifications of␊ |
7 | * Original Code as defined in and that are subject to the Apple Public␊ |
8 | * Source License Version 2.0 (the 'License'). You may not use this file␊ |
9 | * except in compliance with the License. Please obtain a copy of the␊ |
10 | * License at http://www.apple.com/publicsource and read it before using␊ |
11 | * this file.␊ |
12 | * ␊ |
13 | * The Original Code and all software distributed under the License are␊ |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
17 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
18 | * License for the specific language governing rights and limitations␊ |
19 | * under the License.␊ |
20 | * ␊ |
21 | * drivers.c - Driver Loading Functions.␊ |
22 | *␊ |
23 | * Copyright (c) 2000 Apple Computer, Inc.␊ |
24 | *␊ |
25 | * DRI: Josh de Cesare␊ |
26 | */␊ |
27 | ␊ |
28 | #include <mach-o/fat.h>␊ |
29 | #include <libkern/OSByteOrder.h>␊ |
30 | #include <mach/machine.h>␊ |
31 | ␊ |
32 | #include "sl.h"␊ |
33 | #include "boot.h"␊ |
34 | #include "bootstruct.h"␊ |
35 | #include "xml.h"␊ |
36 | #include "ramdisk.h"␊ |
37 | #include "modules.h"␊ |
38 | ␊ |
39 | //extern char gMacOSVersion[8];␊ |
40 | ␊ |
41 | struct Module { ␊ |
42 | ␉struct Module *nextModule;␊ |
43 | ␉long willLoad;␊ |
44 | ␉TagPtr dict;␊ |
45 | ␉char *plistAddr;␊ |
46 | ␉long plistLength;␊ |
47 | ␉char *executablePath;␊ |
48 | ␉char *bundlePath;␊ |
49 | ␉long bundlePathLength;␊ |
50 | };␊ |
51 | typedef struct Module Module, *ModulePtr;␊ |
52 | ␊ |
53 | struct DriverInfo {␊ |
54 | ␉char *plistAddr;␊ |
55 | ␉long plistLength;␊ |
56 | ␉void *executableAddr;␊ |
57 | ␉long executableLength;␊ |
58 | ␉void *bundlePathAddr;␊ |
59 | ␉long bundlePathLength;␊ |
60 | };␊ |
61 | typedef struct DriverInfo DriverInfo, *DriverInfoPtr;␊ |
62 | ␊ |
63 | #define kDriverPackageSignature1 'MKXT'␊ |
64 | #define kDriverPackageSignature2 'MOSX'␊ |
65 | ␊ |
66 | struct DriversPackage {␊ |
67 | unsigned long signature1;␊ |
68 | unsigned long signature2;␊ |
69 | unsigned long length;␊ |
70 | unsigned long adler32;␊ |
71 | unsigned long version;␊ |
72 | unsigned long numDrivers;␊ |
73 | unsigned long reserved1;␊ |
74 | unsigned long reserved2;␊ |
75 | };␊ |
76 | typedef struct DriversPackage DriversPackage;␊ |
77 | ␊ |
78 | enum␊ |
79 | {␊ |
80 | ␉kCFBundleType2,␊ |
81 | ␉kCFBundleType3␊ |
82 | };␊ |
83 | ␊ |
84 | long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);␊ |
85 | ␊ |
86 | /*static*/ unsigned long Adler32( unsigned char * buffer, long length );␊ |
87 | ␊ |
88 | long FileLoadDrivers(char *dirSpec, long plugin);␊ |
89 | long NetLoadDrivers(char *dirSpec);␊ |
90 | long LoadDriverMKext(char *fileSpec);␊ |
91 | long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
92 | long LoadMatchedModules(void);␊ |
93 | ␊ |
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 | 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 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 | ␉{␊ |
154 | ␉␉stop("InitDriverSupport error");␊ |
155 | ␉}␊ |
156 | ␊ |
157 | ␉return 0;␊ |
158 | }␊ |
159 | ␊ |
160 | //==========================================================================␊ |
161 | // LoadDrivers␊ |
162 | ␊ |
163 | long LoadDrivers( char * dirSpec )␊ |
164 | {␊ |
165 | ␉char dirSpecExtra[1024];␊ |
166 | ␊ |
167 | ␉if ( InitDriverSupport() != 0 )␊ |
168 | ␉{␊ |
169 | ␉␉return 0;␊ |
170 | ␉}␊ |
171 | ␊ |
172 | ␉// Load extra drivers if a hook has been installed.␊ |
173 | ␉if (LoadExtraDrivers_p != NULL)␊ |
174 | ␉{␊ |
175 | ␉␉(*LoadExtraDrivers_p)(&FileLoadDrivers);␊ |
176 | ␉}␊ |
177 | ␊ |
178 | ␉if ( gBootFileType == kNetworkDeviceType )␊ |
179 | ␉{␊ |
180 | ␉␉if (NetLoadDrivers(dirSpec) != 0)␊ |
181 | ␉␉{␊ |
182 | ␉␉␉error("Could not load drivers from the network\n");␊ |
183 | ␉␉␉return -1;␊ |
184 | ␉␉}␊ |
185 | ␉}␊ |
186 | ␉else if ( gBootFileType == kBlockDeviceType )␊ |
187 | ␉{␊ |
188 | ␉␉// First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).␊ |
189 | ␉␉if (gRAMDiskVolume && !gRAMDiskBTAliased)␊ |
190 | ␉␉{␊ |
191 | ␉␉␉strcpy(dirSpecExtra, "rd(0,0)/Extra/");␊ |
192 | ␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
193 | ␉␉}␊ |
194 | ␊ |
195 | ␉␉// Next try to load Extra extensions from the selected root partition.␊ |
196 | ␉␉strcpy(dirSpecExtra, "/Extra/");␊ |
197 | ␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
198 | ␉␉{␊ |
199 | ␉␉␉// If failed, then try to load Extra extensions from the boot partition␊ |
200 | ␉␉␉// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.␊ |
201 | ␉␉␉if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)␊ |
202 | ␉␉␉␉|| (gRAMDiskVolume && gRAMDiskBTAliased) )␊ |
203 | ␉␉␉{␊ |
204 | ␉␉␉␉// Next try a specfic OS version folder ie 10.5␊ |
205 | ␉␉␉␉sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);␊ |
206 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
207 | ␉␉␉␉{␉␊ |
208 | ␉␉␉␉␉// Next we'll try the base␊ |
209 | ␉␉␉␉␉strcpy(dirSpecExtra, "bt(0,0)/Extra/");␊ |
210 | ␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
211 | ␉␉␉␉}␊ |
212 | ␉␉␉}␊ |
213 | ␉␉}␊ |
214 | ␉␉if(!gHaveKernelCache)␊ |
215 | ␉␉{␊ |
216 | ␉␉␉// Don't load main driver (from /System/Library/Extentions) if gHaveKernelCache is set.␊ |
217 | ␉␉␉// since these drivers will already be in the kernel cache.␊ |
218 | ␉␉␉// NOTE: when gHaveKernelCache, xnu cannot (by default) load *any* extra kexts from the bootloader.␊ |
219 | ␉␉␉// The /Extra code is not disabled in this case due to a kernel patch that allows for this to happen.␊ |
220 | ␊ |
221 | ␉␉␉// Also try to load Extensions from boot helper partitions.␊ |
222 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
223 | ␉␉␉{␊ |
224 | ␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
225 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
226 | ␉␉␉␉{␊ |
227 | ␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
228 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
229 | ␉␉␉␉␉{␊ |
230 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
231 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
232 | ␉␉␉␉␉}␊ |
233 | ␉␉␉␉}␊ |
234 | ␉␉␉}␊ |
235 | ␊ |
236 | ␉␉␉if (gMKextName[0] != '\0')␊ |
237 | ␉␉␉{␊ |
238 | ␉␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gMKextName);␊ |
239 | ␉␉␉␉if ( LoadDriverMKext(gMKextName) != 0 )␊ |
240 | ␉␉␉␉{␊ |
241 | ␉␉␉␉␉error("Could not load %s\n", gMKextName);␊ |
242 | ␉␉␉␉␉return -1;␊ |
243 | ␉␉␉␉}␊ |
244 | ␉␉␉}␊ |
245 | ␉␉␉else␊ |
246 | ␉␉␉{␊ |
247 | ␉␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
248 | ␉␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
249 | ␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
250 | ␉␉␉}␊ |
251 | ␊ |
252 | ␉␉}␊ |
253 | ␉}␊ |
254 | ␉else␊ |
255 | ␉{␊ |
256 | ␉␉return 0;␊ |
257 | ␉}␊ |
258 | ␊ |
259 | ␉MatchPersonalities();␊ |
260 | ␊ |
261 | ␉MatchLibraries();␊ |
262 | ␊ |
263 | ␉LoadMatchedModules();␊ |
264 | ␊ |
265 | ␉return 0;␊ |
266 | }␊ |
267 | ␊ |
268 | //==========================================================================␊ |
269 | // FileLoadMKext␊ |
270 | ␊ |
271 | static long FileLoadMKext( const char * dirSpec, const char * extDirSpec )␊ |
272 | {␊ |
273 | ␉long␉ret, flags, time, time2;␊ |
274 | ␉char␉altDirSpec[512];␊ |
275 | ␉␊ |
276 | ␉sprintf (altDirSpec, "%s%s", dirSpec, extDirSpec);␊ |
277 | ␉ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);␊ |
278 | ␉␊ |
279 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
280 | ␉{␊ |
281 | ␉␉ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
282 | ␉␉␊ |
283 | ␉␉if ((ret != 0)␊ |
284 | ␉␉␉|| ((flags & kFileTypeMask) != kFileTypeDirectory)␊ |
285 | ␉␉␉|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
286 | ␉␉{␊ |
287 | ␉␉␉sprintf(gDriverSpec, "%sExtensions.mkext", altDirSpec);␊ |
288 | ␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
289 | ␉␉␉␊ |
290 | ␉␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
291 | ␉␉␉{␊ |
292 | ␉␉␉␉return 0;␊ |
293 | ␉␉␉}␊ |
294 | ␉␉}␊ |
295 | ␉}␊ |
296 | ␉return -1;␊ |
297 | }␊ |
298 | ␊ |
299 | //==========================================================================␊ |
300 | // FileLoadDrivers␊ |
301 | ␊ |
302 | long 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 | ␉␉{␊ |
314 | ␉␉␉return 0;␊ |
315 | ␉␉}␊ |
316 | ␊ |
317 | ␉␉// Next try the legacy path.␊ |
318 | ␉␉else if (FileLoadMKext(dirSpec, "") == 0)␊ |
319 | ␉␉{␊ |
320 | ␉␉␉return 0;␊ |
321 | ␉␉}␊ |
322 | ␊ |
323 | ␉␉strcat(dirSpec, "Extensions");␊ |
324 | ␉}␊ |
325 | ␊ |
326 | ␉index = 0;␊ |
327 | ␉while (1)␊ |
328 | ␉{␊ |
329 | ␉␉ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
330 | ␉␉if (ret == -1)␊ |
331 | ␉␉{␊ |
332 | ␉␉␉break;␊ |
333 | ␉␉}␊ |
334 | ␊ |
335 | ␉␉// Make sure this is a directory.␊ |
336 | ␉␉if ((flags & kFileTypeMask) != kFileTypeDirectory)␊ |
337 | ␉␉{␊ |
338 | ␉␉␉continue;␊ |
339 | ␉␉}␊ |
340 | ␊ |
341 | ␉␉// Make sure this is a kext.␊ |
342 | ␉␉length = strlen(name);␊ |
343 | ␉␉if (strcmp(name + length - 5, ".kext"))␊ |
344 | ␉␉{␊ |
345 | ␉␉␉continue;␊ |
346 | ␉␉}␊ |
347 | ␊ |
348 | ␉␉// Save the file name.␊ |
349 | ␉␉strcpy(gFileName, name);␊ |
350 | ␊ |
351 | ␉␉// Determine the bundle type.␊ |
352 | ␉␉sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
353 | ␉␉ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
354 | ␉␉if (ret == 0)␊ |
355 | ␉␉{␊ |
356 | ␉␉␉bundleType = kCFBundleType2;␊ |
357 | ␉␉}␊ |
358 | ␉␉else␊ |
359 | ␉␉{␊ |
360 | ␉␉␉bundleType = kCFBundleType3;␊ |
361 | ␉␉}␊ |
362 | ␊ |
363 | ␉␉if (!plugin)␊ |
364 | ␉␉{␊ |
365 | ␉␉␉sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
366 | ␉␉}␊ |
367 | ␊ |
368 | ␉␉ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
369 | ␊ |
370 | ␉␉if (result != 0)␊ |
371 | ␉␉{␊ |
372 | ␉␉␉result = ret;␊ |
373 | ␉␉}␊ |
374 | ␊ |
375 | ␉␉if (!plugin)␊ |
376 | ␉␉{␊ |
377 | ␉␉␉FileLoadDrivers(gDriverSpec, 1);␊ |
378 | ␉␉}␊ |
379 | ␉}␊ |
380 | ␊ |
381 | ␉return result;␊ |
382 | }␊ |
383 | ␊ |
384 | ␊ |
385 | //==========================================================================␊ |
386 | // ␊ |
387 | ␊ |
388 | long␊ |
389 | NetLoadDrivers( char * dirSpec )␊ |
390 | {␊ |
391 | ␉long tries;␊ |
392 | ␊ |
393 | #if NODEF␊ |
394 | ␉long cnt;␊ |
395 | ␊ |
396 | ␉// Get the name of the kernel␊ |
397 | ␉cnt = strlen(gBootFile);␊ |
398 | ␉while (cnt--)␊ |
399 | ␉{␊ |
400 | ␉␉if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ','))␊ |
401 | ␉␉{␊ |
402 | ␉␉␉cnt++;␊ |
403 | ␉␉␉break;␊ |
404 | ␉␉}␊ |
405 | ␉}␊ |
406 | #endif␊ |
407 | ␊ |
408 | ␉// INTEL modification␊ |
409 | ␉sprintf(gDriverSpec, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
410 | ␊ |
411 | ␉verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
412 | ␊ |
413 | ␉tries = 3;␊ |
414 | ␉while (tries--)␊ |
415 | ␉{␊ |
416 | ␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
417 | ␉␉{␊ |
418 | ␉␉␉break;␊ |
419 | ␉␉}␊ |
420 | ␉}␊ |
421 | ␉if (tries == -1)␊ |
422 | ␉{␊ |
423 | ␉␉return -1;␊ |
424 | ␉}␊ |
425 | ␊ |
426 | ␉return 0;␊ |
427 | }␊ |
428 | ␊ |
429 | //==========================================================================␊ |
430 | // loadDriverMKext␊ |
431 | ␊ |
432 | long LoadDriverMKext( char * fileSpec )␊ |
433 | {␊ |
434 | ␉unsigned long driversAddr, driversLength;␊ |
435 | ␉long length;␊ |
436 | ␉char segName[32];␊ |
437 | ␉DriversPackage * package;␊ |
438 | ␊ |
439 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
440 | ␊ |
441 | ␉// Load the MKext.␊ |
442 | ␉length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
443 | ␉if (length < sizeof (DriversPackage))␊ |
444 | ␉{␊ |
445 | ␉␉return -1;␊ |
446 | ␉}␊ |
447 | ␊ |
448 | ␉// call hook to notify modules that the mkext has been loaded␊ |
449 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);␊ |
450 | ␊ |
451 | ␉␊ |
452 | ␉// Verify the MKext.␊ |
453 | ␉if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
454 | ␉␉( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
455 | ␉␉( GetPackageElement(length) > kLoadSize ) ||␊ |
456 | ␉␉( GetPackageElement(adler32) !=␊ |
457 | ␉␉Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
458 | ␉{␊ |
459 | ␉␉return -1;␊ |
460 | ␉}␊ |
461 | ␊ |
462 | ␉// Make space for the MKext.␊ |
463 | ␉driversLength = GetPackageElement(length);␊ |
464 | ␉driversAddr = AllocateKernelMemory(driversLength);␊ |
465 | ␊ |
466 | ␉// Copy the MKext.␊ |
467 | ␉memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
468 | ␊ |
469 | ␉// Add the MKext to the memory map.␊ |
470 | ␉sprintf(segName, "DriversPackage-%lx", driversAddr);␊ |
471 | ␉AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);␊ |
472 | ␊ |
473 | ␉return 0;␊ |
474 | }␊ |
475 | ␊ |
476 | //==========================================================================␊ |
477 | // LoadDriverPList␊ |
478 | ␊ |
479 | long LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
480 | {␊ |
481 | ␉long length, executablePathLength, bundlePathLength;␊ |
482 | ␉ModulePtr module;␊ |
483 | ␉TagPtr personalities;␊ |
484 | ␉char * buffer = 0;␊ |
485 | ␉char * tmpExecutablePath = 0;␊ |
486 | ␉char * tmpBundlePath = 0;␊ |
487 | ␉long ret = -1;␊ |
488 | ␊ |
489 | ␉do␊ |
490 | ␉{␊ |
491 | ␉// Save the driver path.␊ |
492 | ␊ |
493 | ␉if(name)␊ |
494 | ␉{␊ |
495 | ␉␉sprintf(gFileSpec, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
496 | ␉}␊ |
497 | ␉else␊ |
498 | ␉{␊ |
499 | ␉␉sprintf(gFileSpec, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
500 | ␉}␊ |
501 | ␉executablePathLength = strlen(gFileSpec) + 1;␊ |
502 | ␊ |
503 | ␉tmpExecutablePath = malloc(executablePathLength);␊ |
504 | ␉if (tmpExecutablePath == 0)␊ |
505 | ␉{␊ |
506 | ␉␉break;␊ |
507 | ␉}␊ |
508 | ␉strcpy(tmpExecutablePath, gFileSpec);␊ |
509 | ␊ |
510 | ␉if(name)␊ |
511 | ␉{␊ |
512 | ␉␉sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
513 | ␉}␊ |
514 | else␊ |
515 | ␉{␊ |
516 | ␉␉sprintf(gFileSpec, "%s", dirSpec);␊ |
517 | ␉}␊ |
518 | ␉bundlePathLength = strlen(gFileSpec) + 1;␊ |
519 | ␊ |
520 | ␉tmpBundlePath = malloc(bundlePathLength);␊ |
521 | ␉if (tmpBundlePath == 0)␊ |
522 | ␉{␊ |
523 | ␉␉break;␊ |
524 | ␉}␊ |
525 | ␊ |
526 | ␉strcpy(tmpBundlePath, gFileSpec);␊ |
527 | ␊ |
528 | ␉// Construct the file spec to the plist, then load it.␊ |
529 | ␊ |
530 | ␉if(name)␊ |
531 | ␉{␊ |
532 | ␉␉sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
533 | ␉}␊ |
534 | ␉else␊ |
535 | ␉{␊ |
536 | ␉␉sprintf(gFileSpec, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
537 | ␉}␊ |
538 | ␊ |
539 | ␉length = LoadFile(gFileSpec);␊ |
540 | ␉if (length == -1)␊ |
541 | ␉{␊ |
542 | ␉␉break;␊ |
543 | ␉}␊ |
544 | ␉length = length + 1;␊ |
545 | ␉buffer = malloc(length);␊ |
546 | ␉if (buffer == 0)␊ |
547 | ␉{␊ |
548 | ␉␉break;␊ |
549 | ␉}␊ |
550 | ␉strlcpy(buffer, (char *)kLoadAddr, length);␊ |
551 | ␊ |
552 | ␉// Parse the plist.␊ |
553 | ␊ |
554 | ␉ret = ParseXML(buffer, &module, &personalities);␊ |
555 | ␉if (ret != 0)␊ |
556 | ␉{␊ |
557 | ␉␉break;␊ |
558 | ␉}␊ |
559 | ␊ |
560 | ␉if (!module)␊ |
561 | ␉{␊ |
562 | ␉␉ret = -1;␊ |
563 | ␉␉break;␊ |
564 | ␉} // Should never happen but it will make the compiler happy␊ |
565 | ␊ |
566 | ␉// Allocate memory for the driver path and the plist.␊ |
567 | ␊ |
568 | ␉module->executablePath = tmpExecutablePath;␊ |
569 | ␉module->bundlePath = tmpBundlePath;␊ |
570 | ␉module->bundlePathLength = bundlePathLength;␊ |
571 | ␉module->plistAddr = malloc(length);␊ |
572 | ␊ |
573 | ␉if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
574 | ␉{␊ |
575 | ␉␉break;␊ |
576 | ␉}␊ |
577 | ␊ |
578 | ␉// Save the driver path in the module.␊ |
579 | ␉//strcpy(module->driverPath, tmpDriverPath);␊ |
580 | ␉tmpExecutablePath = 0;␊ |
581 | ␉tmpBundlePath = 0;␊ |
582 | ␊ |
583 | ␉// Add the plist to the module.␊ |
584 | ␊ |
585 | ␉strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
586 | ␉module->plistLength = length;␊ |
587 | ␊ |
588 | ␉// Add the module to the end of the module list.␊ |
589 | ␊ |
590 | ␉if (gModuleHead == 0)␊ |
591 | ␉{␊ |
592 | ␉␉gModuleHead = module;␊ |
593 | ␉}␊ |
594 | ␉else␊ |
595 | ␉{␊ |
596 | ␉␉gModuleTail->nextModule = module;␊ |
597 | ␉}␊ |
598 | ␉gModuleTail = module;␊ |
599 | ␊ |
600 | ␉// Add the persionalities to the personality list.␊ |
601 | ␊ |
602 | ␉if (personalities)␊ |
603 | ␉{␊ |
604 | ␉␉personalities = personalities->tag;␊ |
605 | ␉}␊ |
606 | ␉while (personalities != 0)␊ |
607 | ␉{␊ |
608 | ␉␉if (gPersonalityHead == 0)␊ |
609 | ␉␉{␊ |
610 | ␉␉␉gPersonalityHead = personalities->tag;␊ |
611 | ␉␉}␊ |
612 | ␉␉else␊ |
613 | ␉␉{␊ |
614 | ␉␉␉gPersonalityTail->tagNext = personalities->tag;␊ |
615 | ␉␉}␊ |
616 | ␊ |
617 | ␉␉gPersonalityTail = personalities->tag;␊ |
618 | ␉␉personalities = personalities->tagNext;␊ |
619 | ␉}␊ |
620 | ␊ |
621 | ␉ret = 0;␊ |
622 | ␉}␊ |
623 | ␉while (0);␊ |
624 | ␊ |
625 | ␉if ( buffer )␊ |
626 | ␉{␊ |
627 | ␉␉free( buffer );␊ |
628 | ␉}␊ |
629 | ␉if ( tmpExecutablePath )␊ |
630 | ␉{␊ |
631 | ␉␉free( tmpExecutablePath );␊ |
632 | ␉}␊ |
633 | ␉if ( tmpBundlePath )␊ |
634 | ␉{␊ |
635 | ␉␉free( tmpBundlePath );␊ |
636 | ␉}␊ |
637 | ␉return ret;␊ |
638 | }␊ |
639 | ␊ |
640 | ␊ |
641 | //==========================================================================␊ |
642 | // LoadMatchedModules␊ |
643 | ␊ |
644 | long LoadMatchedModules( void )␊ |
645 | {␊ |
646 | ␉TagPtr␉␉ prop;␊ |
647 | ␉ModulePtr␉ module;␊ |
648 | ␉char␉␉ *fileName, segName[32];␊ |
649 | ␉DriverInfoPtr driver;␊ |
650 | ␉long␉␉ length, driverAddr, driverLength;␊ |
651 | ␉void␉␉ *executableAddr = 0;␊ |
652 | ␊ |
653 | ␊ |
654 | ␉module = gModuleHead;␊ |
655 | ␊ |
656 | ␉while (module != 0)␊ |
657 | ␉{␊ |
658 | ␉␉if (module->willLoad)␊ |
659 | ␉␉{␊ |
660 | ␉␉␉prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
661 | ␊ |
662 | ␉␉␉if (prop != 0)␊ |
663 | ␉␉␉{␊ |
664 | ␉␉␉␉fileName = prop->string;␊ |
665 | ␉␉␉␉sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
666 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
667 | ␉␉␉␉if (length == 0)␊ |
668 | ␉␉␉␉{␊ |
669 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
670 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
671 | ␉␉␉␉}␊ |
672 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
673 | ␉␉␉}␊ |
674 | ␉␉␉else␊ |
675 | ␉␉␉␉length = 0;␊ |
676 | ␊ |
677 | ␉␉␉if (length != -1)␊ |
678 | ␉␉␉{␊ |
679 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
680 | //␉␉␉␉if (length != 0)␊ |
681 | //␉␉␉␉{␊ |
682 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
683 | //␉␉␉␉}␊ |
684 | ␊ |
685 | ␉␉␉␉// Make make in the image area.␊ |
686 | ␊ |
687 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
688 | ␊ |
689 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
690 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
691 | ␊ |
692 | ␉␉␉␉// Set up the DriverInfo.␊ |
693 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
694 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
695 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
696 | ␉␉␉␉if (length != 0)␊ |
697 | ␉␉␉␉{␊ |
698 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
699 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
700 | ␉␉␉␉␉driver->executableLength = length;␊ |
701 | ␉␉␉␉}␊ |
702 | ␉␉␉␉else␊ |
703 | ␉␉␉␉{␊ |
704 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
705 | ␉␉␉␉␉driver->executableLength = 0;␊ |
706 | ␉␉␉␉}␊ |
707 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
708 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
709 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
710 | ␊ |
711 | ␉␉␉␉// Save the plist, module and bundle.␊ |
712 | ␉␉␉␉strcpy(driver->plistAddr, module->plistAddr);␊ |
713 | ␉␉␉␉if (length != 0)␊ |
714 | ␉␉␉␉{␊ |
715 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
716 | ␉␉␉␉}␊ |
717 | ␉␉␉␉strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
718 | ␊ |
719 | ␉␉␉␉// Add an entry to the memory map.␊ |
720 | ␉␉␉␉sprintf(segName, "Driver-%lx", (unsigned long)driver);␊ |
721 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
722 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
723 | ␉␉␉}␊ |
724 | ␉␉}␊ |
725 | ␉␉module = module->nextModule;␊ |
726 | ␉}␊ |
727 | ␊ |
728 | ␉return 0;␊ |
729 | }␊ |
730 | ␊ |
731 | //==========================================================================␊ |
732 | // MatchPersonalities␊ |
733 | ␊ |
734 | static long MatchPersonalities( void )␊ |
735 | {␊ |
736 | ␉/* IONameMatch support not implemented */␊ |
737 | ␉return 0;␊ |
738 | }␊ |
739 | ␊ |
740 | //==========================================================================␊ |
741 | // MatchLibraries␊ |
742 | ␊ |
743 | static long MatchLibraries( void )␊ |
744 | {␊ |
745 | ␉TagPtr prop, prop2;␊ |
746 | ␉ModulePtr module, module2;␊ |
747 | ␉long done;␊ |
748 | ␊ |
749 | ␉do␊ |
750 | ␉{␊ |
751 | ␉␉done = 1;␊ |
752 | ␉␉module = gModuleHead;␊ |
753 | ␊ |
754 | ␉␉while (module != 0)␊ |
755 | ␉␉{␊ |
756 | ␉␉␉if (module->willLoad == 1)␊ |
757 | ␉␉␉{␊ |
758 | ␉␉␉␉prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
759 | ␊ |
760 | ␉␉␉␉if (prop != 0)␊ |
761 | ␉␉␉␉{␊ |
762 | ␉␉␉␉␉prop = prop->tag;␊ |
763 | ␊ |
764 | ␉␉␉␉␉while (prop != 0)␊ |
765 | ␉␉␉␉␉{␊ |
766 | ␉␉␉␉␉␉module2 = gModuleHead;␊ |
767 | ␊ |
768 | ␉␉␉␉␉␉while (module2 != 0)␊ |
769 | ␉␉␉␉␉␉{␊ |
770 | ␉␉␉␉␉␉␉prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
771 | ␊ |
772 | ␉␉␉␉␉␉␉if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
773 | ␉␉␉␉␉␉␉{␊ |
774 | ␉␉␉␉␉␉␉␉if (module2->willLoad == 0)␊ |
775 | ␉␉␉␉␉␉␉␉{␊ |
776 | ␉␉␉␉␉␉␉␉␉module2->willLoad = 1;␊ |
777 | ␉␉␉␉␉␉␉␉}␊ |
778 | ␉␉␉␉␉␉␉␉break;␊ |
779 | ␉␉␉␉␉␉␉}␊ |
780 | ␉␉␉␉␉␉␉module2 = module2->nextModule;␊ |
781 | ␉␉␉␉␉␉}␊ |
782 | ␉␉␉␉␉␉prop = prop->tagNext;␊ |
783 | ␉␉␉␉␉}␊ |
784 | ␉␉␉␉}␊ |
785 | ␉␉␉␉module->willLoad = 2;␊ |
786 | ␉␉␉␉done = 0;␊ |
787 | ␉␉␉}␊ |
788 | ␉␉␉module = module->nextModule;␊ |
789 | ␉␉}␊ |
790 | ␉}␊ |
791 | ␉while (!done);␊ |
792 | ␊ |
793 | ␉return 0;␊ |
794 | }␊ |
795 | ␊ |
796 | ␊ |
797 | //==========================================================================␊ |
798 | // FindModule␊ |
799 | ␊ |
800 | #if NOTDEF␊ |
801 | static ModulePtr FindModule( char * name )␊ |
802 | {␊ |
803 | ␉ModulePtr module;␊ |
804 | ␉TagPtr prop;␊ |
805 | ␊ |
806 | ␉module = gModuleHead;␊ |
807 | ␊ |
808 | ␉while (module != 0)␊ |
809 | ␉{␊ |
810 | ␉␉prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
811 | ␊ |
812 | ␉␉if ((prop != 0) && !strcmp(name, prop->string))␊ |
813 | ␉␉{␊ |
814 | ␉␉␉break;␊ |
815 | ␉␉}␊ |
816 | ␊ |
817 | ␉␉module = module->nextModule;␊ |
818 | ␉}␊ |
819 | ␊ |
820 | ␉return module;␊ |
821 | }␊ |
822 | #endif /* NOTDEF */␊ |
823 | ␊ |
824 | //==========================================================================␊ |
825 | // ParseXML␊ |
826 | ␊ |
827 | static long ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
828 | {␊ |
829 | ␉long length, pos;␊ |
830 | ␉TagPtr moduleDict, required;␊ |
831 | ␉ModulePtr tmpModule;␊ |
832 | ␊ |
833 | ␉pos = 0;␊ |
834 | ␊ |
835 | ␉while (1)␊ |
836 | ␉{␊ |
837 | ␉␉length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
838 | ␉␉if (length == -1)␊ |
839 | ␉␉{␊ |
840 | ␉␉␉break;␊ |
841 | ␉␉}␊ |
842 | ␊ |
843 | ␉␉pos += length;␊ |
844 | ␊ |
845 | ␉␉if (moduleDict == 0)␊ |
846 | ␉␉{␊ |
847 | ␉␉␉continue;␊ |
848 | ␉␉}␊ |
849 | ␉␉if (moduleDict->type == kTagTypeDict)␊ |
850 | ␉␉{␊ |
851 | ␉␉␉break;␊ |
852 | ␉␉}␊ |
853 | ␉␉XMLFreeTag(moduleDict);␊ |
854 | ␉}␊ |
855 | ␊ |
856 | ␉if (length == -1)␊ |
857 | ␉{␊ |
858 | ␉␉return -1;␊ |
859 | ␉}␊ |
860 | ␊ |
861 | ␉required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
862 | ␊ |
863 | ␉if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))␊ |
864 | ␉{␊ |
865 | ␉␉XMLFreeTag(moduleDict);␊ |
866 | ␉␉return -2;␊ |
867 | ␉}␊ |
868 | ␊ |
869 | ␉tmpModule = malloc(sizeof(Module));␊ |
870 | ␉if (tmpModule == 0)␊ |
871 | ␉{␊ |
872 | ␉␉XMLFreeTag(moduleDict);␊ |
873 | ␉␉return -1;␊ |
874 | ␉}␊ |
875 | ␉tmpModule->dict = moduleDict;␊ |
876 | ␊ |
877 | ␉// For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
878 | ␊ |
879 | ␉tmpModule->willLoad = 1;␊ |
880 | ␊ |
881 | ␉*module = tmpModule;␊ |
882 | ␊ |
883 | ␉// Get the personalities.␊ |
884 | ␊ |
885 | ␉*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
886 | ␊ |
887 | ␉return 0;␊ |
888 | }␊ |
889 | ␊ |
890 | #if NOTDEF␊ |
891 | static char gPlatformName[64];␊ |
892 | #endif␊ |
893 | ␊ |
894 | long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
895 | {␊ |
896 | ␉long ret;␊ |
897 | ␉compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
898 | ␉u_int32_t uncompressed_size, size;␊ |
899 | ␉void *buffer;␊ |
900 | ␉unsigned long len;␊ |
901 | ␉␊ |
902 | #if 0␊ |
903 | ␉printf("kernel header:\n");␊ |
904 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
905 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
906 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
907 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
908 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
909 | ␉getchar();␊ |
910 | #endif␊ |
911 | ␊ |
912 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
913 | ␉{␊ |
914 | ␉␉if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))␊ |
915 | ␉␉{␊ |
916 | ␉␉␉error("kernel compression is bad\n");␊ |
917 | ␉␉␉return -1;␊ |
918 | ␉␉}␊ |
919 | #if NOTDEF␊ |
920 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
921 | ␉␉{␊ |
922 | ␉␉␉return -1;␊ |
923 | ␉␉}␊ |
924 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
925 | ␉␉{␊ |
926 | ␉␉␉return -1;␊ |
927 | ␉␉}␊ |
928 | #endif␊ |
929 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
930 | ␉␉binary = buffer = malloc(uncompressed_size);␊ |
931 | ␉␉␊ |
932 | ␉␉size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
933 | ␉␉␉␉␉␉␉ OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
934 | ␉␉if (uncompressed_size != size)␊ |
935 | ␉␉{␊ |
936 | ␉␉␉error("size mismatch from lzss: %x\n", size);␊ |
937 | ␉␉␉return -1;␊ |
938 | ␉␉}␊ |
939 | ␉␉␊ |
940 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) != Adler32(binary, uncompressed_size))␊ |
941 | ␉␉{␊ |
942 | ␉␉␉printf("adler mismatch\n");␊ |
943 | ␉␉␉return -1;␊ |
944 | ␉␉}␊ |
945 | ␉}␊ |
946 | ␉␊ |
947 | ␉ret = ThinFatFile(&binary, &len);␊ |
948 | ␉if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
949 | ␉{␊ |
950 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
951 | ␉␉ret = ThinFatFile(&binary, &len);␊ |
952 | ␉}␊ |
953 | ␊ |
954 | ␉// Notify modules that the kernel has been decompressed, thinned and is about to be decoded␊ |
955 | ␉execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);␊ |
956 | ␊ |
957 | ␊ |
958 | ␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
959 | ␉if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
960 | ␉{␊ |
961 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
962 | ␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
963 | ␉}␊ |
964 | ␊ |
965 | ␉return ret;␊ |
966 | }␊ |
967 | |