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