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 ((checkOSVersion("10.9")) || (checkOSVersion("10.10"))) {␊ |
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 | ␉// Allocate memory for the driver path and the plist.␊ |
528 | ␊ |
529 | ␉module->executablePath = tmpExecutablePath;␊ |
530 | ␉module->bundlePath = tmpBundlePath;␊ |
531 | ␉module->bundlePathLength = bundlePathLength;␊ |
532 | ␉module->plistAddr = malloc(length);␊ |
533 | ␊ |
534 | ␉if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0)) {␊ |
535 | ␉␉break;␊ |
536 | ␉}␊ |
537 | ␊ |
538 | ␉// Save the driver path in the module.␊ |
539 | ␉//strcpy(module->driverPath, tmpDriverPath);␊ |
540 | ␉tmpExecutablePath = 0;␊ |
541 | ␉tmpBundlePath = 0;␊ |
542 | ␊ |
543 | ␉// Add the plist to the module.␊ |
544 | ␊ |
545 | ␉strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
546 | ␉module->plistLength = length;␊ |
547 | ␊ |
548 | ␉// Add the module to the end of the module list.␊ |
549 | ␊ |
550 | ␉if (gModuleHead == 0) {␊ |
551 | ␉␉gModuleHead = module;␊ |
552 | ␉} else ␉{␊ |
553 | ␉␉gModuleTail->nextModule = module;␊ |
554 | ␉}␊ |
555 | ␉gModuleTail = module;␊ |
556 | ␊ |
557 | ␉// Add the persionalities to the personality list.␊ |
558 | ␊ |
559 | ␉if (personalities) {␊ |
560 | ␉␉personalities = personalities->tag;␊ |
561 | ␉}␊ |
562 | ␉while (personalities != 0)␊ |
563 | ␉{␊ |
564 | ␉␉if (gPersonalityHead == 0) {␊ |
565 | ␉␉␉gPersonalityHead = personalities->tag;␊ |
566 | ␉␉} else {␊ |
567 | ␉␉␉gPersonalityTail->tagNext = personalities->tag;␊ |
568 | ␉␉}␊ |
569 | ␊ |
570 | ␉␉gPersonalityTail = personalities->tag;␊ |
571 | ␉␉personalities = personalities->tagNext;␊ |
572 | ␉}␊ |
573 | ␊ |
574 | ␉ret = 0;␊ |
575 | ␉}␊ |
576 | ␉while (0);␊ |
577 | ␊ |
578 | ␉if ( buffer ) {␊ |
579 | ␉␉free( buffer );␊ |
580 | ␉}␊ |
581 | ␉if ( tmpExecutablePath ) {␊ |
582 | ␉␉free( tmpExecutablePath );␊ |
583 | ␉}␊ |
584 | ␉if ( tmpBundlePath ) {␊ |
585 | ␉␉free( tmpBundlePath );␊ |
586 | ␉}␊ |
587 | ␉return ret;␊ |
588 | }␊ |
589 | ␊ |
590 | ␊ |
591 | //==========================================================================␊ |
592 | // LoadMatchedModules␊ |
593 | ␊ |
594 | long␊ |
595 | LoadMatchedModules( void )␊ |
596 | {␊ |
597 | ␉TagPtr␉␉ prop;␊ |
598 | ␉ModulePtr␉ module;␊ |
599 | ␉char␉␉ *fileName, segName[32];␊ |
600 | ␉DriverInfoPtr driver;␊ |
601 | ␉long␉␉ length, driverAddr, driverLength;␊ |
602 | ␉void␉␉ *executableAddr = 0;␊ |
603 | ␊ |
604 | ␉module = gModuleHead;␊ |
605 | ␊ |
606 | ␉while (module != 0)␊ |
607 | ␉{␊ |
608 | ␉␉if (module->willLoad)␊ |
609 | ␉␉{␊ |
610 | ␉␉␉prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
611 | ␊ |
612 | ␉␉␉if (prop != 0)␊ |
613 | ␉␉␉{␊ |
614 | ␉␉␉␉fileName = prop->string;␊ |
615 | ␉␉␉␉snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);␊ |
616 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
617 | ␉␉␉␉if (length == 0)␊ |
618 | ␉␉␉␉{␊ |
619 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
620 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
621 | ␉␉␉␉}␊ |
622 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
623 | ␉␉␉}␊ |
624 | ␉␉␉else␊ |
625 | ␉␉␉␉length = 0;␊ |
626 | ␊ |
627 | ␉␉␉if (length != -1)␊ |
628 | ␉␉␉{␊ |
629 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
630 | //␉␉␉␉if (length != 0)␊ |
631 | //␉␉␉␉{␊ |
632 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
633 | //␉␉␉␉}␊ |
634 | ␊ |
635 | ␉␉␉␉// Make make in the image area.␊ |
636 | ␊ |
637 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
638 | ␊ |
639 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
640 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
641 | ␊ |
642 | ␉␉␉␉// Set up the DriverInfo.␊ |
643 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
644 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
645 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
646 | ␉␉␉␉if (length != 0)␊ |
647 | ␉␉␉␉{␊ |
648 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
649 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
650 | ␉␉␉␉␉driver->executableLength = length;␊ |
651 | ␉␉␉␉}␊ |
652 | ␉␉␉␉else␊ |
653 | ␉␉␉␉{␊ |
654 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
655 | ␉␉␉␉␉driver->executableLength = 0;␊ |
656 | ␉␉␉␉}␊ |
657 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
658 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
659 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
660 | ␊ |
661 | ␉␉␉␉// Save the plist, module and bundle.␊ |
662 | ␉␉␉␉strcpy(driver->plistAddr, module->plistAddr);␊ |
663 | ␉␉␉␉if (length != 0)␊ |
664 | ␉␉␉␉{␊ |
665 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
666 | ␉␉␉␉}␊ |
667 | ␉␉␉␉strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
668 | ␊ |
669 | ␉␉␉␉// Add an entry to the memory map.␊ |
670 | ␉␉␉␉snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);␊ |
671 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
672 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
673 | ␉␉␉}␊ |
674 | ␉␉}␊ |
675 | ␉␉module = module->nextModule;␊ |
676 | ␉}␊ |
677 | ␊ |
678 | ␉return 0;␊ |
679 | }␊ |
680 | ␊ |
681 | //==========================================================================␊ |
682 | // MatchPersonalities␊ |
683 | ␊ |
684 | static long␊ |
685 | MatchPersonalities( void )␊ |
686 | {␊ |
687 | ␉/* IONameMatch support not implemented */␊ |
688 | ␉return 0;␊ |
689 | }␊ |
690 | ␊ |
691 | //==========================================================================␊ |
692 | // MatchLibraries␊ |
693 | ␊ |
694 | static long␊ |
695 | MatchLibraries( void )␊ |
696 | {␊ |
697 | ␉TagPtr prop, prop2;␊ |
698 | ␉ModulePtr module, module2;␊ |
699 | ␉long done;␊ |
700 | ␊ |
701 | ␉do {␊ |
702 | ␉␉done = 1;␊ |
703 | ␉␉module = gModuleHead;␊ |
704 | ␊ |
705 | ␉␉while (module != 0)␊ |
706 | ␉␉{␊ |
707 | ␉␉␉if (module->willLoad == 1)␊ |
708 | ␉␉␉{␊ |
709 | ␉␉␉␉prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
710 | ␊ |
711 | ␉␉␉␉if (prop != 0)␊ |
712 | ␉␉␉␉{␊ |
713 | ␉␉␉␉␉prop = prop->tag;␊ |
714 | ␊ |
715 | ␉␉␉␉␉while (prop != 0)␊ |
716 | ␉␉␉␉␉{␊ |
717 | ␉␉␉␉␉␉module2 = gModuleHead;␊ |
718 | ␊ |
719 | ␉␉␉␉␉␉while (module2 != 0)␊ |
720 | ␉␉␉␉␉␉{␊ |
721 | ␉␉␉␉␉␉␉prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
722 | ␊ |
723 | ␉␉␉␉␉␉␉if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
724 | ␉␉␉␉␉␉␉{␊ |
725 | ␉␉␉␉␉␉␉␉if (module2->willLoad == 0)␊ |
726 | ␉␉␉␉␉␉␉␉{␊ |
727 | ␉␉␉␉␉␉␉␉␉module2->willLoad = 1;␊ |
728 | ␉␉␉␉␉␉␉␉}␊ |
729 | ␉␉␉␉␉␉␉␉break;␊ |
730 | ␉␉␉␉␉␉␉}␊ |
731 | ␉␉␉␉␉␉␉module2 = module2->nextModule;␊ |
732 | ␉␉␉␉␉␉}␊ |
733 | ␉␉␉␉␉␉prop = prop->tagNext;␊ |
734 | ␉␉␉␉␉}␊ |
735 | ␉␉␉␉}␊ |
736 | ␉␉␉␉module->willLoad = 2;␊ |
737 | ␉␉␉␉done = 0;␊ |
738 | ␉␉␉}␊ |
739 | ␉␉␉module = module->nextModule;␊ |
740 | ␉␉}␊ |
741 | ␉}␊ |
742 | ␉while (!done);␊ |
743 | ␊ |
744 | ␉return 0;␊ |
745 | }␊ |
746 | ␊ |
747 | ␊ |
748 | //==========================================================================␊ |
749 | // FindModule␊ |
750 | ␊ |
751 | #if NOTDEF␊ |
752 | static ModulePtr␊ |
753 | FindModule( char * name )␊ |
754 | {␊ |
755 | ␉ModulePtr module;␊ |
756 | ␉TagPtr prop;␊ |
757 | ␊ |
758 | ␉module = gModuleHead;␊ |
759 | ␊ |
760 | ␉while (module != 0)␊ |
761 | ␉{␊ |
762 | ␉␉prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
763 | ␊ |
764 | ␉␉if ((prop != 0) && !strcmp(name, prop->string)) {␊ |
765 | ␉␉␉break;␊ |
766 | ␉␉}␊ |
767 | ␊ |
768 | ␉␉module = module->nextModule;␊ |
769 | ␉}␊ |
770 | ␊ |
771 | ␉return module;␊ |
772 | }␊ |
773 | #endif /* NOTDEF */␊ |
774 | ␊ |
775 | //==========================================================================␊ |
776 | // ParseXML␊ |
777 | ␊ |
778 | static long␊ |
779 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
780 | {␊ |
781 | ␉long length, pos;␊ |
782 | ␉TagPtr moduleDict, required;␊ |
783 | ␉ModulePtr tmpModule;␊ |
784 | ␊ |
785 | ␉pos = 0;␊ |
786 | ␊ |
787 | ␉while (1)␊ |
788 | ␉{␊ |
789 | ␉␉length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
790 | ␉␉if (length == -1) {␊ |
791 | ␉␉␉break;␊ |
792 | ␉␉}␊ |
793 | ␊ |
794 | ␉␉pos += length;␊ |
795 | ␊ |
796 | ␉␉if (moduleDict == 0) {␊ |
797 | ␉␉␉continue;␊ |
798 | ␉␉}␊ |
799 | ␉␉if (moduleDict->type == kTagTypeDict) {␊ |
800 | ␉␉␉break;␊ |
801 | ␉␉}␊ |
802 | ␉␉XMLFreeTag(moduleDict);␊ |
803 | ␉}␊ |
804 | ␊ |
805 | ␉if (length == -1) {␊ |
806 | ␉␉return -1;␊ |
807 | ␉}␊ |
808 | ␊ |
809 | ␉required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
810 | ␊ |
811 | ␉if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))␊ |
812 | ␉{␊ |
813 | ␉␉XMLFreeTag(moduleDict);␊ |
814 | ␉␉return -2;␊ |
815 | ␉}␊ |
816 | ␊ |
817 | ␉tmpModule = malloc(sizeof(Module));␊ |
818 | ␉if (tmpModule == 0) {␊ |
819 | ␉␉XMLFreeTag(moduleDict);␊ |
820 | ␉␉return -1;␊ |
821 | ␉}␊ |
822 | ␉tmpModule->dict = moduleDict;␊ |
823 | ␊ |
824 | ␉// For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
825 | ␊ |
826 | ␉tmpModule->willLoad = 1;␊ |
827 | ␊ |
828 | ␉*module = tmpModule;␊ |
829 | ␊ |
830 | ␉// Get the personalities.␊ |
831 | ␊ |
832 | ␉*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
833 | ␊ |
834 | ␉return 0;␊ |
835 | }␊ |
836 | ␊ |
837 | #if NOTDEF␊ |
838 | static char gPlatformName[64];␊ |
839 | #endif␊ |
840 | ␊ |
841 | char *gDarwinBuildVerStr = "Darwin Kernel Version"; // Bungo␊ |
842 | ␊ |
843 | long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
844 | {␊ |
845 | long ret = 0;␊ |
846 | compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
847 | u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;␊ |
848 | void *buffer = NULL;␊ |
849 | unsigned long len = 0;␊ |
850 | ␉␊ |
851 | #if 0␊ |
852 | ␉printf("kernel header:\n");␊ |
853 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
854 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
855 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
856 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
857 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
858 | ␉getchar();␊ |
859 | #endif␊ |
860 | ␊ |
861 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
862 | ␉{␊ |
863 | if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&␊ |
864 | (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))␊ |
865 | ␉␉{␊ |
866 | verbose ("Decompressing Kernel\n");␊ |
867 | ␉␉␉error("ERROR: Invalid kernel compression\n");␊ |
868 | ␉␉␉return -1;␊ |
869 | ␉␉}␊ |
870 | if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))␊ |
871 | verbose ("Decompressing Kernel Cache Using lzss\n");␊ |
872 | else␊ |
873 | if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))␊ |
874 | verbose ("Decompressing Kernel Cache Using lzvn\n");␊ |
875 | #if NOTDEF␊ |
876 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
877 | ␉␉{␊ |
878 | ␉␉␉return -1;␊ |
879 | ␉␉}␊ |
880 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
881 | ␉␉{␊ |
882 | ␉␉␉return -1;␊ |
883 | ␉␉}␊ |
884 | #endif␊ |
885 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
886 | ␉␉binary = buffer = malloc(uncompressed_size);␊ |
887 | ␉␉␊ |
888 | // MinusZwei␊ |
889 | size = 0;␊ |
890 | switch (kernel_header->compress_type)␊ |
891 | {␊ |
892 | case OSSwapBigToHostConstInt32('lzvn'):␊ |
893 | size = decompress_lzvn( binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
894 | break;␊ |
895 | ␊ |
896 | case OSSwapBigToHostConstInt32('lzss'):␊ |
897 | size = decompress_lzss( (u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
898 | break;␊ |
899 | ␊ |
900 | default:␊ |
901 | break;␊ |
902 | }␊ |
903 | // MinusZwei␊ |
904 | ␊ |
905 | if (uncompressed_size != size) {␊ |
906 | error("ERROR: size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);␊ |
907 | return -1;␊ |
908 | }␊ |
909 | ␊ |
910 | adler32 = Adler32(binary, uncompressed_size);␊ |
911 | if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)␊ |
912 | {␊ |
913 | error("ERROR: adler mismatch (found: %x, expected: %x).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));␊ |
914 | return -1;␊ |
915 | }␊ |
916 | }␊ |
917 | ␉␊ |
918 | ␉ret = ThinFatFile(&binary, &len);␊ |
919 | ␉if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
920 | ␉{␊ |
921 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
922 | ␉␉ret = ThinFatFile(&binary, &len);␊ |
923 | ␉}␊ |
924 | ␊ |
925 | // Bungo: no range checking, sorry␊ |
926 | size = 0;␊ |
927 | while (memcmp((uint8_t *)binary + size, (uint8_t *)gDarwinBuildVerStr, 21)) {␊ |
928 | size++;␊ |
929 | }␊ |
930 | gDarwinBuildVerStr = (char *)binary + size;␊ |
931 | ␊ |
932 | ␉// Notify modules that the kernel has been decompressed, thinned and is about to be decoded␊ |
933 | ␉execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);␊ |
934 | ␊ |
935 | ␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
936 | ␉if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
937 | ␉{␊ |
938 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
939 | ␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
940 | ␉}␊ |
941 | ␊ |
942 | ␉return ret;␊ |
943 | }␊ |
944 | |