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 | ␉␉␉␉␉error("Could not load %s\n", gMKextName);␊ |
246 | ␉␉␉␉␉return -1;␊ |
247 | ␉␉␉␉}␊ |
248 | ␉␉␉}␊ |
249 | ␉␉␉else␊ |
250 | ␉␉␉{␊ |
251 | ␉␉␉␉if (MacOSVerCurrent >= MacOSVer2Int("10.9")) { // issue 352␊ |
252 | ␉␉␉␉␉strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */␊ |
253 | ␉␉␉␉␉strcat(gExtensionsSpec, "Library/");␊ |
254 | ␉␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
255 | ␉␉␉␉}␊ |
256 | ␉␉␉␉strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */␊ |
257 | ␉␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
258 | ␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
259 | ␉␉␉}␊ |
260 | ␊ |
261 | ␉␉}␊ |
262 | ␉}␊ |
263 | ␉else␊ |
264 | ␉{␊ |
265 | ␉␉return 0;␊ |
266 | ␉}␊ |
267 | ␊ |
268 | ␉MatchPersonalities();␊ |
269 | ␊ |
270 | ␉MatchLibraries();␊ |
271 | ␊ |
272 | ␉LoadMatchedModules();␊ |
273 | ␊ |
274 | ␉return 0;␊ |
275 | }␊ |
276 | ␊ |
277 | //==========================================================================␊ |
278 | // FileLoadMKext␊ |
279 | ␊ |
280 | static long FileLoadMKext( const char * dirSpec, const char * extDirSpec )␊ |
281 | {␊ |
282 | ␉long␉␉ret, flags;␊ |
283 | ␉u_int32_t␉time, time2;␊ |
284 | ␉char␉␉altDirSpec[512];␊ |
285 | ␉␊ |
286 | ␉snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);␊ |
287 | ␉ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);␊ |
288 | ␊ |
289 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
290 | ␉{␊ |
291 | ␉␉ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
292 | ␊ |
293 | ␉␉if ((ret != 0)␊ |
294 | ␉␉␉|| ((flags & kFileTypeMask) != kFileTypeDirectory)␊ |
295 | ␉␉␉|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
296 | ␉␉{␊ |
297 | ␉␉␉snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);␊ |
298 | ␉␉␉verbose("LoadDrivers: Loading from '%s'\n", gDriverSpec);␊ |
299 | ␊ |
300 | ␉␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
301 | ␉␉␉{␊ |
302 | ␉␉␉␉return 0;␊ |
303 | ␉␉␉}␊ |
304 | ␉␉}␊ |
305 | ␉}␊ |
306 | ␉return -1;␊ |
307 | }␊ |
308 | ␊ |
309 | //==========================================================================␊ |
310 | // FileLoadDrivers␊ |
311 | ␊ |
312 | long FileLoadDrivers( char * dirSpec, long plugin )␊ |
313 | {␊ |
314 | ␉long long␉index;␊ |
315 | ␉long␉␉ret, length, flags, bundleType;␊ |
316 | ␉long␉␉result = -1;␊ |
317 | ␉u_int32_t␉time;␊ |
318 | ␉const char␉* name;␊ |
319 | ␊ |
320 | ␉if ( !plugin )␊ |
321 | ␉{␊ |
322 | ␉␉// First try 10.6's path for loading Extensions.mkext.␊ |
323 | ␉␉if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {␊ |
324 | ␉␉␉return 0;␊ |
325 | ␉␉}␊ |
326 | ␊ |
327 | ␉␉// Next try the legacy path.␊ |
328 | ␉␉else if (FileLoadMKext(dirSpec, "") == 0)␊ |
329 | ␉␉{␊ |
330 | ␉␉␉return 0;␊ |
331 | ␉␉}␊ |
332 | ␊ |
333 | ␉␉strcat(dirSpec, "Extensions");␊ |
334 | ␉}␊ |
335 | ␊ |
336 | ␉index = 0;␊ |
337 | ␉while (1)␊ |
338 | ␉{␊ |
339 | ␉␉ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
340 | ␉␉if (ret == -1) {␊ |
341 | ␉␉␉break;␊ |
342 | ␉␉}␊ |
343 | ␊ |
344 | ␉␉// Make sure this is a directory.␊ |
345 | ␉␉if ((flags & kFileTypeMask) != kFileTypeDirectory)␊ |
346 | ␉␉{␊ |
347 | ␉␉␉continue;␊ |
348 | ␉␉}␊ |
349 | ␊ |
350 | ␉␉// Make sure this is a kext.␊ |
351 | ␉␉length = strlen(name);␊ |
352 | ␉␉if (strcmp(name + length - 5, ".kext"))␊ |
353 | ␉␉{␊ |
354 | ␉␉␉continue;␊ |
355 | ␉␉}␊ |
356 | ␊ |
357 | ␉␉// Save the file name.␊ |
358 | ␉␉strlcpy(gFileName, name, 4096);␊ |
359 | ␊ |
360 | ␉␉// Determine the bundle type.␊ |
361 | ␉␉snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);␊ |
362 | ␉␉ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
363 | ␉␉if (ret == 0)␊ |
364 | ␉␉{␊ |
365 | ␉␉␉bundleType = kCFBundleType2;␊ |
366 | ␉␉}␊ |
367 | ␉␉else␊ |
368 | ␉␉{␊ |
369 | ␉␉␉bundleType = kCFBundleType3;␊ |
370 | ␉␉}␊ |
371 | ␊ |
372 | ␉␉if (!plugin)␊ |
373 | ␉␉{␊ |
374 | ␉␉␉snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
375 | ␉␉}␊ |
376 | ␊ |
377 | ␉␉ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
378 | ␊ |
379 | ␉␉if (result != 0)␊ |
380 | ␉␉{␊ |
381 | ␉␉␉result = ret;␊ |
382 | ␉␉}␊ |
383 | ␊ |
384 | ␉␉if (!plugin)␊ |
385 | ␉␉{␊ |
386 | ␉␉␉FileLoadDrivers(gDriverSpec, 1);␊ |
387 | ␉␉}␊ |
388 | ␉}␊ |
389 | ␊ |
390 | ␉return result;␊ |
391 | }␊ |
392 | ␊ |
393 | ␊ |
394 | //==========================================================================␊ |
395 | // ␊ |
396 | ␊ |
397 | long NetLoadDrivers( char * dirSpec )␊ |
398 | {␊ |
399 | ␉long tries;␊ |
400 | ␊ |
401 | #if NODEF␊ |
402 | ␉long cnt;␊ |
403 | ␊ |
404 | ␉// Get the name of the kernel␊ |
405 | ␉cnt = strlen(gBootFile);␊ |
406 | ␉while (cnt--) {␊ |
407 | ␉␉if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ','))␊ |
408 | ␉␉{␊ |
409 | ␉␉␉cnt++;␊ |
410 | ␉␉␉break;␊ |
411 | ␉␉}␊ |
412 | ␉}␊ |
413 | #endif␊ |
414 | ␊ |
415 | ␉// INTEL modification␊ |
416 | ␉snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
417 | ␊ |
418 | ␉verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
419 | ␊ |
420 | ␉tries = 3;␊ |
421 | ␉while (tries--)␊ |
422 | ␉{␊ |
423 | ␉␉if (LoadDriverMKext(gDriverSpec) == 0)␊ |
424 | ␉␉{␊ |
425 | ␉␉␉break;␊ |
426 | ␉␉}␊ |
427 | ␉}␊ |
428 | ␉if (tries == -1)␊ |
429 | ␉{␊ |
430 | ␉␉return -1;␊ |
431 | ␉}␊ |
432 | ␊ |
433 | ␉return 0;␊ |
434 | }␊ |
435 | ␊ |
436 | //==========================================================================␊ |
437 | // loadDriverMKext␊ |
438 | ␊ |
439 | long LoadDriverMKext( char * fileSpec )␊ |
440 | {␊ |
441 | ␉unsigned long driversAddr, driversLength;␊ |
442 | ␉long length;␊ |
443 | ␉char segName[32];␊ |
444 | ␉DriversPackage * package;␊ |
445 | ␊ |
446 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
447 | ␊ |
448 | ␉// Load the MKext.␊ |
449 | ␉length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
450 | ␉if (length < sizeof (DriversPackage))␊ |
451 | ␉{␊ |
452 | ␉␉return -1;␊ |
453 | ␉}␊ |
454 | ␊ |
455 | ␉// call hook to notify modules that the mkext has been loaded␊ |
456 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);␊ |
457 | ␊ |
458 | ␉␊ |
459 | ␉// Verify the MKext.␊ |
460 | ␉if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
461 | ␉␉( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
462 | ␉␉( GetPackageElement(length) > kLoadSize ) ||␊ |
463 | ␉␉( GetPackageElement(adler32) !=␊ |
464 | ␉␉Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
465 | ␉{␊ |
466 | ␉␉return -1;␊ |
467 | ␉}␊ |
468 | ␊ |
469 | ␉// Make space for the MKext.␊ |
470 | ␉driversLength = GetPackageElement(length);␊ |
471 | ␉driversAddr = AllocateKernelMemory(driversLength);␊ |
472 | ␊ |
473 | ␉// Copy the MKext.␊ |
474 | ␉memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
475 | ␊ |
476 | ␉// Add the MKext to the memory map.␊ |
477 | ␉snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);␊ |
478 | ␉AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);␊ |
479 | ␊ |
480 | ␉return 0;␊ |
481 | }␊ |
482 | ␊ |
483 | //==========================================================================␊ |
484 | // LoadDriverPList␊ |
485 | ␊ |
486 | long␊ |
487 | LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
488 | {␊ |
489 | ␉long length, executablePathLength, bundlePathLength;␊ |
490 | ␉ModulePtr module;␊ |
491 | ␉TagPtr personalities;␊ |
492 | ␉char * buffer = 0;␊ |
493 | ␉char * tmpExecutablePath = 0;␊ |
494 | ␉char * tmpBundlePath = 0;␊ |
495 | ␉long ret = -1;␊ |
496 | ␊ |
497 | ␉do{␊ |
498 | ␉// Save the driver path.␊ |
499 | ␊ |
500 | ␉if(name) {␊ |
501 | ␉␉snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
502 | ␉} else {␊ |
503 | ␉␉snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
504 | ␉}␊ |
505 | ␉executablePathLength = strlen(gFileSpec) + 1;␊ |
506 | ␊ |
507 | ␉tmpExecutablePath = malloc(executablePathLength);␊ |
508 | ␉if (tmpExecutablePath == 0) {␊ |
509 | ␉␉break;␊ |
510 | ␉}␊ |
511 | ␉strcpy(tmpExecutablePath, gFileSpec);␊ |
512 | ␊ |
513 | ␉if(name) {␊ |
514 | ␉␉snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);␊ |
515 | ␉} else ␉{␊ |
516 | ␉␉snprintf(gFileSpec, 4096, "%s", dirSpec);␊ |
517 | ␉}␊ |
518 | ␉bundlePathLength = strlen(gFileSpec) + 1;␊ |
519 | ␊ |
520 | ␉tmpBundlePath = malloc(bundlePathLength);␊ |
521 | ␉if (tmpBundlePath == 0) {␊ |
522 | ␉␉break;␊ |
523 | ␉}␊ |
524 | ␊ |
525 | ␉strcpy(tmpBundlePath, gFileSpec);␊ |
526 | ␊ |
527 | ␉// Construct the file spec to the plist, then load it.␊ |
528 | ␊ |
529 | ␉if(name) {␊ |
530 | ␉␉snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
531 | ␉} else {␊ |
532 | ␉␉snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
533 | ␉}␊ |
534 | ␊ |
535 | ␉length = LoadFile(gFileSpec);␊ |
536 | ␉if (length == -1) {␊ |
537 | ␉␉break;␊ |
538 | ␉}␊ |
539 | ␉length = length + 1;␊ |
540 | ␉buffer = malloc(length);␊ |
541 | ␉if (buffer == 0) {␊ |
542 | ␉␉break;␊ |
543 | ␉}␊ |
544 | ␉strlcpy(buffer, (char *)kLoadAddr, length);␊ |
545 | ␊ |
546 | ␉// Parse the plist.␊ |
547 | ␊ |
548 | ␉ret = ParseXML(buffer, &module, &personalities);␊ |
549 | ␊ |
550 | ␉if (ret != 0) {␊ |
551 | ␉␉break;␊ |
552 | ␉}␊ |
553 | ␊ |
554 | ␉if (!module) // cparm␊ |
555 | ␉{␊ |
556 | ␉␉ret = -1;␊ |
557 | ␉␉break;␊ |
558 | ␉} // Should never happen but it will make the compiler happy␊ |
559 | ␊ |
560 | ␉// Allocate memory for the driver path and the plist.␊ |
561 | ␊ |
562 | ␉module->executablePath = tmpExecutablePath;␊ |
563 | ␉module->bundlePath = tmpBundlePath;␊ |
564 | ␉module->bundlePathLength = bundlePathLength;␊ |
565 | ␉module->plistAddr = malloc(length);␊ |
566 | ␊ |
567 | ␉if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0)) {␊ |
568 | ␉␉break;␊ |
569 | ␉}␊ |
570 | ␊ |
571 | ␉// Save the driver path in the module.␊ |
572 | ␉//strcpy(module->driverPath, tmpDriverPath);␊ |
573 | ␉tmpExecutablePath = 0;␊ |
574 | ␉tmpBundlePath = 0;␊ |
575 | ␊ |
576 | ␉// Add the plist to the module.␊ |
577 | ␊ |
578 | ␉strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
579 | ␉module->plistLength = length;␊ |
580 | ␊ |
581 | ␉// Add the module to the end of the module list.␊ |
582 | ␊ |
583 | ␉if (gModuleHead == 0) {␊ |
584 | ␉␉gModuleHead = module;␊ |
585 | ␉} else ␉{␊ |
586 | ␉␉gModuleTail->nextModule = module;␊ |
587 | ␉}␊ |
588 | ␉gModuleTail = module;␊ |
589 | ␊ |
590 | ␉// Add the persionalities to the personality list.␊ |
591 | ␊ |
592 | ␉if (personalities) {␊ |
593 | ␉␉personalities = personalities->tag;␊ |
594 | ␉}␊ |
595 | ␉while (personalities != 0)␊ |
596 | ␉{␊ |
597 | ␉␉if (gPersonalityHead == 0) {␊ |
598 | ␉␉␉gPersonalityHead = personalities->tag;␊ |
599 | ␉␉} else {␊ |
600 | ␉␉␉gPersonalityTail->tagNext = personalities->tag;␊ |
601 | ␉␉}␊ |
602 | ␊ |
603 | ␉␉gPersonalityTail = personalities->tag;␊ |
604 | ␉␉personalities = personalities->tagNext;␊ |
605 | ␉}␊ |
606 | ␊ |
607 | ␉ret = 0;␊ |
608 | ␉}␊ |
609 | ␉while (0);␊ |
610 | ␊ |
611 | ␉if ( buffer ) {␊ |
612 | ␉␉free( buffer );␊ |
613 | ␉}␊ |
614 | ␉if ( tmpExecutablePath ) {␊ |
615 | ␉␉free( tmpExecutablePath );␊ |
616 | ␉}␊ |
617 | ␉if ( tmpBundlePath ) {␊ |
618 | ␉␉free( tmpBundlePath );␊ |
619 | ␉}␊ |
620 | ␉return ret;␊ |
621 | }␊ |
622 | ␊ |
623 | ␊ |
624 | //==========================================================================␊ |
625 | // LoadMatchedModules␊ |
626 | ␊ |
627 | long␊ |
628 | LoadMatchedModules( void )␊ |
629 | {␊ |
630 | ␉TagPtr␉␉ prop;␊ |
631 | ␉ModulePtr␉ module;␊ |
632 | ␉char␉␉ *fileName, segName[32];␊ |
633 | ␉DriverInfoPtr driver;␊ |
634 | ␉long␉␉ length, driverAddr, driverLength;␊ |
635 | ␉void␉␉ *executableAddr = 0;␊ |
636 | ␊ |
637 | ␉module = gModuleHead;␊ |
638 | ␊ |
639 | ␉while (module != 0)␊ |
640 | ␉{␊ |
641 | ␉␉if (module->willLoad)␊ |
642 | ␉␉{␊ |
643 | ␉␉␉prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
644 | ␊ |
645 | ␉␉␉if (prop != 0)␊ |
646 | ␉␉␉{␊ |
647 | ␉␉␉␉fileName = prop->string;␊ |
648 | ␉␉␉␉snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);␊ |
649 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
650 | ␉␉␉␉if (length == 0)␊ |
651 | ␉␉␉␉{␊ |
652 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
653 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
654 | ␉␉␉␉}␊ |
655 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
656 | ␉␉␉}␊ |
657 | ␉␉␉else␊ |
658 | ␉␉␉␉length = 0;␊ |
659 | ␊ |
660 | ␉␉␉if (length != -1)␊ |
661 | ␉␉␉{␊ |
662 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
663 | //␉␉␉␉if (length != 0)␊ |
664 | //␉␉␉␉{␊ |
665 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
666 | //␉␉␉␉}␊ |
667 | ␊ |
668 | ␉␉␉␉// Make make in the image area.␊ |
669 | ␊ |
670 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
671 | ␊ |
672 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
673 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
674 | ␊ |
675 | ␉␉␉␉// Set up the DriverInfo.␊ |
676 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
677 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
678 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
679 | ␉␉␉␉if (length != 0)␊ |
680 | ␉␉␉␉{␊ |
681 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
682 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
683 | ␉␉␉␉␉driver->executableLength = length;␊ |
684 | ␉␉␉␉}␊ |
685 | ␉␉␉␉else␊ |
686 | ␉␉␉␉{␊ |
687 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
688 | ␉␉␉␉␉driver->executableLength = 0;␊ |
689 | ␉␉␉␉}␊ |
690 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
691 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
692 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
693 | ␊ |
694 | ␉␉␉␉// Save the plist, module and bundle.␊ |
695 | ␉␉␉␉strcpy(driver->plistAddr, module->plistAddr);␊ |
696 | ␉␉␉␉if (length != 0)␊ |
697 | ␉␉␉␉{␊ |
698 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
699 | ␉␉␉␉}␊ |
700 | ␉␉␉␉strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
701 | ␊ |
702 | ␉␉␉␉// Add an entry to the memory map.␊ |
703 | ␉␉␉␉snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);␊ |
704 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
705 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
706 | ␉␉␉}␊ |
707 | ␉␉}␊ |
708 | ␉␉module = module->nextModule;␊ |
709 | ␉}␊ |
710 | ␊ |
711 | ␉return 0;␊ |
712 | }␊ |
713 | ␊ |
714 | //==========================================================================␊ |
715 | // MatchPersonalities␊ |
716 | ␊ |
717 | static long␊ |
718 | MatchPersonalities( void )␊ |
719 | {␊ |
720 | ␉/* IONameMatch support not implemented */␊ |
721 | ␉return 0;␊ |
722 | }␊ |
723 | ␊ |
724 | //==========================================================================␊ |
725 | // MatchLibraries␊ |
726 | ␊ |
727 | static long␊ |
728 | MatchLibraries( void )␊ |
729 | {␊ |
730 | ␉TagPtr prop, prop2;␊ |
731 | ␉ModulePtr module, module2;␊ |
732 | ␉long done;␊ |
733 | ␊ |
734 | ␉do {␊ |
735 | ␉␉done = 1;␊ |
736 | ␉␉module = gModuleHead;␊ |
737 | ␊ |
738 | ␉␉while (module != 0)␊ |
739 | ␉␉{␊ |
740 | ␉␉␉if (module->willLoad == 1)␊ |
741 | ␉␉␉{␊ |
742 | ␉␉␉␉prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
743 | ␊ |
744 | ␉␉␉␉if (prop != 0)␊ |
745 | ␉␉␉␉{␊ |
746 | ␉␉␉␉␉prop = prop->tag;␊ |
747 | ␊ |
748 | ␉␉␉␉␉while (prop != 0)␊ |
749 | ␉␉␉␉␉{␊ |
750 | ␉␉␉␉␉␉module2 = gModuleHead;␊ |
751 | ␊ |
752 | ␉␉␉␉␉␉while (module2 != 0)␊ |
753 | ␉␉␉␉␉␉{␊ |
754 | ␉␉␉␉␉␉␉prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
755 | ␊ |
756 | ␉␉␉␉␉␉␉if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
757 | ␉␉␉␉␉␉␉{␊ |
758 | ␉␉␉␉␉␉␉␉if (module2->willLoad == 0)␊ |
759 | ␉␉␉␉␉␉␉␉{␊ |
760 | ␉␉␉␉␉␉␉␉␉module2->willLoad = 1;␊ |
761 | ␉␉␉␉␉␉␉␉}␊ |
762 | ␉␉␉␉␉␉␉␉break;␊ |
763 | ␉␉␉␉␉␉␉}␊ |
764 | ␉␉␉␉␉␉␉module2 = module2->nextModule;␊ |
765 | ␉␉␉␉␉␉}␊ |
766 | ␉␉␉␉␉␉prop = prop->tagNext;␊ |
767 | ␉␉␉␉␉}␊ |
768 | ␉␉␉␉}␊ |
769 | ␉␉␉␉module->willLoad = 2;␊ |
770 | ␉␉␉␉done = 0;␊ |
771 | ␉␉␉}␊ |
772 | ␉␉␉module = module->nextModule;␊ |
773 | ␉␉}␊ |
774 | ␉}␊ |
775 | ␉while (!done);␊ |
776 | ␊ |
777 | ␉return 0;␊ |
778 | }␊ |
779 | ␊ |
780 | ␊ |
781 | //==========================================================================␊ |
782 | // FindModule␊ |
783 | ␊ |
784 | #if NOTDEF␊ |
785 | static ModulePtr␊ |
786 | FindModule( char * name )␊ |
787 | {␊ |
788 | ␉ModulePtr module;␊ |
789 | ␉TagPtr prop;␊ |
790 | ␊ |
791 | ␉module = gModuleHead;␊ |
792 | ␊ |
793 | ␉while (module != 0)␊ |
794 | ␉{␊ |
795 | ␉␉prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
796 | ␊ |
797 | ␉␉if ((prop != 0) && !strcmp(name, prop->string))␊ |
798 | ␉␉{␊ |
799 | ␉␉␉break;␊ |
800 | ␉␉}␊ |
801 | ␊ |
802 | ␉␉module = module->nextModule;␊ |
803 | ␉}␊ |
804 | ␊ |
805 | ␉return module;␊ |
806 | }␊ |
807 | #endif /* NOTDEF */␊ |
808 | ␊ |
809 | //==========================================================================␊ |
810 | // ParseXML␊ |
811 | ␊ |
812 | static long␊ |
813 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
814 | {␊ |
815 | ␉long length, pos;␊ |
816 | ␉TagPtr moduleDict, required;␊ |
817 | ␉ModulePtr tmpModule;␊ |
818 | ␊ |
819 | ␉pos = 0;␊ |
820 | ␊ |
821 | ␉while (1)␊ |
822 | ␉{␊ |
823 | ␉␉length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
824 | ␉␉if (length == -1)␊ |
825 | ␉␉{␊ |
826 | ␉␉␉break;␊ |
827 | ␉␉}␊ |
828 | ␊ |
829 | ␉␉pos += length;␊ |
830 | ␊ |
831 | ␉␉if (moduleDict == 0)␊ |
832 | ␉␉{␊ |
833 | ␉␉␉continue;␊ |
834 | ␉␉}␊ |
835 | ␉␉if (moduleDict->type == kTagTypeDict)␊ |
836 | ␉␉{␊ |
837 | ␉␉␉break;␊ |
838 | ␉␉}␊ |
839 | ␉␉XMLFreeTag(moduleDict);␊ |
840 | ␉}␊ |
841 | ␊ |
842 | ␉if (length == -1)␊ |
843 | ␉{␊ |
844 | ␉␉return -1;␊ |
845 | ␉}␊ |
846 | ␊ |
847 | ␉required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
848 | ␊ |
849 | ␉if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))␊ |
850 | ␉{␊ |
851 | ␉␉XMLFreeTag(moduleDict);␊ |
852 | ␉␉return -2;␊ |
853 | ␉}␊ |
854 | ␊ |
855 | ␉tmpModule = malloc(sizeof(Module));␊ |
856 | ␉if (tmpModule == 0)␊ |
857 | ␉{␊ |
858 | ␉␉XMLFreeTag(moduleDict);␊ |
859 | ␉␉return -1;␊ |
860 | ␉}␊ |
861 | ␉tmpModule->dict = moduleDict;␊ |
862 | ␊ |
863 | ␉// For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
864 | ␊ |
865 | ␉tmpModule->willLoad = 1;␊ |
866 | ␊ |
867 | ␉*module = tmpModule;␊ |
868 | ␊ |
869 | ␉// Get the personalities.␊ |
870 | ␊ |
871 | ␉*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
872 | ␊ |
873 | ␉return 0;␊ |
874 | }␊ |
875 | ␊ |
876 | #if NOTDEF␊ |
877 | static char gPlatformName[64];␊ |
878 | #endif␊ |
879 | ␊ |
880 | long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
881 | {␊ |
882 | ␉long ret = 0;␊ |
883 | ␉compressed_kernel_header *kernel_header = (compressed_kernel_header *)binary;␊ |
884 | ␉u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;␊ |
885 | ␉//void *buffer = NULL;␊ |
886 | ␉unsigned long len = 0;␊ |
887 | ␉␊ |
888 | /*#if 0␊ |
889 | ␉printf("kernel header:\n");␊ |
890 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
891 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
892 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
893 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
894 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
895 | ␉getchar();␊ |
896 | #endif*/␊ |
897 | ␊ |
898 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
899 | ␉{␊ |
900 | ␉␉verbose("Decompressing Kernel Cache");␊ |
901 | /*␊ |
902 | ␉␉if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&␊ |
903 | (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))␊ |
904 | ␉␉{␊ |
905 | ␉␉␉error("ERROR: kernel compression is bad!\n");␊ |
906 | ␉␉␉return -1;␊ |
907 | ␉␉}␊ |
908 | */␊ |
909 | #if NOTDEF␊ |
910 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
911 | ␉␉{␊ |
912 | ␉␉␉return -1;␊ |
913 | ␉␉}␊ |
914 | ␊ |
915 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
916 | ␉␉{␊ |
917 | ␉␉␉return -1;␊ |
918 | ␉␉}␊ |
919 | #endif␊ |
920 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
921 | ␉␉binary /* = buffer */ = malloc(uncompressed_size);␊ |
922 | ␊ |
923 | ␉␉// MinusZwei␊ |
924 | ␉␉size = 0;␊ |
925 | ␉␉switch (kernel_header->compress_type)␊ |
926 | ␉␉{␊ |
927 | ␉␉␉case OSSwapBigToHostConstInt32('lzvn'):␊ |
928 | verbose(" using 'lzvn': ");␊ |
929 | ␉␉␉␉size = lzvn_decode((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
930 | ␉␉␉␉if (uncompressed_size != size)␊ |
931 | ␉␉␉␉{␊ |
932 | error("ERROR! Size mismatch from 'lzvn' (found: %d, expected: %d).\n", size, uncompressed_size);␊ |
933 | return -1;␊ |
934 | }␊ |
935 | ␉␉␉␉break;␊ |
936 | ␊ |
937 | ␉␉␉case OSSwapBigToHostConstInt32('lzss'):␊ |
938 | verbose(" using 'lzss': ");␊ |
939 | ␉␉␉␉size = decompress_lzss((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
940 | ␉␉␉␉if (uncompressed_size != size)␊ |
941 | ␉␉␉␉{␊ |
942 | error("ERROR! Size mismatch from 'lzss' (found: %d, expected: %d).\n", size, uncompressed_size);␊ |
943 | return -1;␊ |
944 | }␊ |
945 | ␉␉␉␉break;␊ |
946 | ␊ |
947 | ␉␉␉default:␊ |
948 | verbose(": ");␊ |
949 | uint32_t compress_type = OSSwapBigToHostInt32(kernel_header->compress_type);␊ |
950 | 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]);␊ |
951 | return -1;␊ |
952 | ␉␉}␊ |
953 | ␉␉// MinusZwei␊ |
954 | ␊ |
955 | ␉␉adler32 = Adler32(binary, uncompressed_size);␊ |
956 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)␊ |
957 | ␉␉{␊ |
958 | ␉␉␉error("ERROR! Adler mismatch (found: %X, expected: %X).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));␊ |
959 | ␉␉␉return -1;␊ |
960 | ␉␉}␊ |
961 | verbose("OK.\n");␊ |
962 | ␉}␊ |
963 | ␉␊ |
964 | ␉ret = ThinFatFile(&binary, &len);␊ |
965 | ␉if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
966 | ␉{␊ |
967 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
968 | ␉␉ret = ThinFatFile(&binary, &len);␊ |
969 | ␉}␊ |
970 | ␊ |
971 | // Bungo: scan binary for Darwin Kernel Version string␊ |
972 | uint32_t offset = 0;␊ |
973 | //strncpy(gDarwinBuildVerStr, "Darwin Kernel Version", sizeof(gDarwinBuildVerStr));␊ |
974 | ␉while ((offset < 0xFFFFFFFF - (uint32_t)binary - 256) && memcmp(binary + offset, gDarwinBuildVerStr, 21))␊ |
975 | ␉{␊ |
976 | offset++;␊ |
977 | }␊ |
978 | if (offset < 0xFFFFFFFF - (uint32_t)binary - 256)␊ |
979 | ␉{␊ |
980 | strncpy(gDarwinBuildVerStr, (char *)(binary + offset), sizeof(gDarwinBuildVerStr));␊ |
981 | ␉}␊ |
982 | else␊ |
983 | ␉{␊ |
984 | strcat(gDarwinBuildVerStr, ": Unknown");␊ |
985 | ␉}␊ |
986 | ␊ |
987 | ␉// Notify modules that the kernel has been decompressed, thinned and is about to be decoded␊ |
988 | ␉execute_hook("DecodeKernel", (void *)binary, NULL, NULL, NULL);␊ |
989 | ␊ |
990 | ␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
991 | ␉if (ret < 0 && archCpuType == CPU_TYPE_X86_64)␊ |
992 | ␉{␊ |
993 | ␉␉archCpuType = CPU_TYPE_I386;␊ |
994 | ␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
995 | ␉}␊ |
996 | ␊ |
997 | ␉return ret;␊ |
998 | }␊ |
999 | |