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