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