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 || ELCAPITAN ) // 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 | ␊ |
673 | ␉␉␉␉length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
674 | ␉␉␉␉if (length == 0)␊ |
675 | ␉␉␉␉{␊ |
676 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
677 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
678 | ␉␉␉␉}␊ |
679 | //␉␉␉␉printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();␊ |
680 | ␉␉␉}␊ |
681 | ␉␉␉else␊ |
682 | ␉␉␉{␊ |
683 | ␉␉␉␉length = 0;␊ |
684 | ␉␉␉}␊ |
685 | ␊ |
686 | ␉␉␉if (length != -1)␊ |
687 | ␉␉␉{␊ |
688 | //␉␉␉␉driverModuleAddr = (void *)kLoadAddr;␊ |
689 | //␉␉␉␉if (length != 0)␊ |
690 | //␉␉␉␉{␊ |
691 | //␉␉␉␉␉ThinFatFile(&driverModuleAddr, &length);␊ |
692 | //␉␉␉␉}␊ |
693 | ␊ |
694 | ␉␉␉␉// Make make in the image area.␊ |
695 | ␊ |
696 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
697 | ␊ |
698 | ␉␉␉␉driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
699 | ␉␉␉␉driverAddr = AllocateKernelMemory(driverLength);␊ |
700 | ␊ |
701 | ␉␉␉␉// Set up the DriverInfo.␊ |
702 | ␉␉␉␉driver = (DriverInfoPtr)driverAddr;␊ |
703 | ␉␉␉␉driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
704 | ␉␉␉␉driver->plistLength = module->plistLength;␊ |
705 | ␊ |
706 | ␉␉␉␉if (length != 0)␊ |
707 | ␉␉␉␉{␊ |
708 | ␉␉␉␉␉driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
709 | ␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
710 | ␉␉␉␉␉driver->executableLength = length;␊ |
711 | ␉␉␉␉}␊ |
712 | ␉␉␉␉else␊ |
713 | ␉␉␉␉{␊ |
714 | ␉␉␉␉␉driver->executableAddr␉ = 0;␊ |
715 | ␉␉␉␉␉driver->executableLength = 0;␊ |
716 | ␉␉␉␉}␊ |
717 | ␊ |
718 | ␉␉␉␉driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
719 | ␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
720 | ␉␉␉␉driver->bundlePathLength = module->bundlePathLength;␊ |
721 | ␊ |
722 | ␉␉␉␉// Save the plist, module and bundle.␊ |
723 | ␉␉␉␉strlcpy(driver->plistAddr, module->plistAddr, driver->plistLength);␊ |
724 | ␊ |
725 | ␉␉␉␉if (length != 0)␊ |
726 | ␉␉␉␉{␊ |
727 | ␉␉␉␉␉memcpy(driver->executableAddr, executableAddr, length);␊ |
728 | ␉␉␉␉}␊ |
729 | ␊ |
730 | ␉␉␉␉strlcpy(driver->bundlePathAddr, module->bundlePath, module->bundlePathLength);␊ |
731 | ␊ |
732 | ␉␉␉␉// Add an entry to the memory map.␊ |
733 | ␉␉␉␉snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);␊ |
734 | ␉␉␉␉AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
735 | ␉␉␉␉␉␉␉␉␉kBootDriverTypeKEXT);␊ |
736 | ␉␉␉}␊ |
737 | ␉␉}␊ |
738 | ␉␉module = module->nextModule;␊ |
739 | ␉}␊ |
740 | ␊ |
741 | ␉return 0;␊ |
742 | }␊ |
743 | ␊ |
744 | //==========================================================================␊ |
745 | // MatchPersonalities␊ |
746 | ␊ |
747 | static long␊ |
748 | MatchPersonalities( void )␊ |
749 | {␊ |
750 | ␉/* IONameMatch support not implemented */␊ |
751 | ␉return 0;␊ |
752 | }␊ |
753 | ␊ |
754 | //==========================================================================␊ |
755 | // MatchLibraries␊ |
756 | ␊ |
757 | static long MatchLibraries( void )␊ |
758 | {␊ |
759 | ␉TagPtr prop;␊ |
760 | ␉TagPtr prop2;␊ |
761 | ␉ModulePtr module;␊ |
762 | ␉ModulePtr module2;␊ |
763 | ␉long done;␊ |
764 | ␊ |
765 | ␉do {␊ |
766 | ␉␉done = 1;␊ |
767 | ␉␉module = gModuleHead;␊ |
768 | ␊ |
769 | ␉␉while (module != 0)␊ |
770 | ␉␉{␊ |
771 | ␉␉␉if (module->willLoad == 1)␊ |
772 | ␉␉␉{␊ |
773 | ␉␉␉␉prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
774 | ␊ |
775 | ␉␉␉␉if (prop != 0)␊ |
776 | ␉␉␉␉{␊ |
777 | ␉␉␉␉␉prop = prop->tag;␊ |
778 | ␊ |
779 | ␉␉␉␉␉while (prop != 0)␊ |
780 | ␉␉␉␉␉{␊ |
781 | ␉␉␉␉␉␉module2 = gModuleHead;␊ |
782 | ␊ |
783 | ␉␉␉␉␉␉while (module2 != 0)␊ |
784 | ␉␉␉␉␉␉{␊ |
785 | ␉␉␉␉␉␉␉prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
786 | ␊ |
787 | ␉␉␉␉␉␉␉if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
788 | ␉␉␉␉␉␉␉{␊ |
789 | ␉␉␉␉␉␉␉␉if (module2->willLoad == 0)␊ |
790 | ␉␉␉␉␉␉␉␉{␊ |
791 | ␉␉␉␉␉␉␉␉␉module2->willLoad = 1;␊ |
792 | ␉␉␉␉␉␉␉␉}␊ |
793 | ␉␉␉␉␉␉␉␉break;␊ |
794 | ␉␉␉␉␉␉␉}␊ |
795 | ␉␉␉␉␉␉␉module2 = module2->nextModule;␊ |
796 | ␉␉␉␉␉␉}␊ |
797 | ␉␉␉␉␉␉prop = prop->tagNext;␊ |
798 | ␉␉␉␉␉}␊ |
799 | ␉␉␉␉}␊ |
800 | ␉␉␉␉module->willLoad = 2;␊ |
801 | ␉␉␉␉done = 0;␊ |
802 | ␉␉␉}␊ |
803 | ␉␉␉module = module->nextModule;␊ |
804 | ␉␉}␊ |
805 | ␉}␊ |
806 | ␉while (!done);␊ |
807 | ␊ |
808 | ␉return 0;␊ |
809 | }␊ |
810 | ␊ |
811 | ␊ |
812 | //==========================================================================␊ |
813 | // FindModule␊ |
814 | ␊ |
815 | #if NOTDEF␊ |
816 | static ModulePtr FindModule( char *name )␊ |
817 | {␊ |
818 | ␉ModulePtr module;␊ |
819 | ␉TagPtr prop;␊ |
820 | ␊ |
821 | ␉module = gModuleHead;␊ |
822 | ␊ |
823 | ␉while (module != 0)␊ |
824 | ␉{␊ |
825 | ␉␉prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
826 | ␊ |
827 | ␉␉if ((prop != 0) && !strcmp(name, prop->string))␊ |
828 | ␉␉{␊ |
829 | ␉␉␉break;␊ |
830 | ␉␉}␊ |
831 | ␊ |
832 | ␉␉module = module->nextModule;␊ |
833 | ␉}␊ |
834 | ␊ |
835 | ␉return module;␊ |
836 | }␊ |
837 | #endif /* NOTDEF */␊ |
838 | ␊ |
839 | //==========================================================================␊ |
840 | // ParseXML␊ |
841 | ␊ |
842 | static long ParseXML( char *buffer, ModulePtr *module, TagPtr *personalities )␊ |
843 | {␊ |
844 | ␉long␉␉length;␊ |
845 | ␉long␉␉pos = 0;␊ |
846 | ␉TagPtr␉␉moduleDict;␊ |
847 | ␉TagPtr␉␉required;␊ |
848 | ␉ModulePtr␉tmpModule;␊ |
849 | ␊ |
850 | ␉while (1)␊ |
851 | ␉{␊ |
852 | ␉␉length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
853 | ␉␉if (length == -1)␊ |
854 | ␉␉{␊ |
855 | ␉␉␉break;␊ |
856 | ␉␉}␊ |
857 | ␊ |
858 | ␉␉pos += length;␊ |
859 | ␊ |
860 | ␉␉if (moduleDict == 0)␊ |
861 | ␉␉{␊ |
862 | ␉␉␉continue;␊ |
863 | ␉␉}␊ |
864 | ␉␉if (moduleDict->type == kTagTypeDict)␊ |
865 | ␉␉{␊ |
866 | ␉␉␉break;␊ |
867 | ␉␉}␊ |
868 | ␉␉XMLFreeTag(moduleDict);␊ |
869 | ␉}␊ |
870 | ␊ |
871 | ␉if (length == -1)␊ |
872 | ␉{␊ |
873 | ␉␉return -1;␊ |
874 | ␉}␊ |
875 | ␊ |
876 | ␉required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
877 | ␊ |
878 | ␉if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))␊ |
879 | ␉{␊ |
880 | ␉␉XMLFreeTag(moduleDict);␊ |
881 | ␉␉return -2;␊ |
882 | ␉}␊ |
883 | ␊ |
884 | ␉tmpModule = malloc(sizeof(Module));␊ |
885 | ␉if (tmpModule == 0)␊ |
886 | ␉{␊ |
887 | ␉␉XMLFreeTag(moduleDict);␊ |
888 | ␉␉return -1;␊ |
889 | ␉}␊ |
890 | ␉tmpModule->dict = moduleDict;␊ |
891 | ␊ |
892 | ␉// For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
893 | ␊ |
894 | ␉tmpModule->willLoad = 1;␊ |
895 | ␊ |
896 | ␉*module = tmpModule;␊ |
897 | ␊ |
898 | ␉// Get the personalities.␊ |
899 | ␊ |
900 | ␉*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
901 | ␊ |
902 | ␉return 0;␊ |
903 | }␊ |
904 | ␊ |
905 | #if NOTDEF␊ |
906 | static char gPlatformName[64];␊ |
907 | #endif␊ |
908 | ␊ |
909 | long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
910 | {␊ |
911 | ␉long ret = 0;␊ |
912 | ␉compressed_kernel_header *kernel_header = (compressed_kernel_header *)binary;␊ |
913 | ␉u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;␊ |
914 | ␉void *buffer = NULL;␊ |
915 | ␉unsigned long len = 0;␊ |
916 | ␉␊ |
917 | /*#if 0␊ |
918 | ␉printf("kernel header:\n");␊ |
919 | ␉printf("signature: 0x%x\n", kernel_header->signature);␊ |
920 | ␉printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
921 | ␉printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
922 | ␉printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
923 | ␉printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
924 | ␉getchar();␊ |
925 | #endif*/␊ |
926 | ␊ |
927 | ␉if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
928 | ␉{␊ |
929 | ␉␉DBG("Decompressing Kernel Cache");␊ |
930 | ␊ |
931 | ␉␉if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&␊ |
932 | ␉␉␉(kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))␊ |
933 | ␉␉{␊ |
934 | ␉␉␉error("ERROR: kernel compression is bad!\n");␊ |
935 | ␉␉␉return -1;␊ |
936 | ␉␉}␊ |
937 | ␊ |
938 | ␉␉if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))␊ |
939 | ␉␉{␊ |
940 | ␉␉␉verbose ("Decompressing Kernel Using lzss\n");␊ |
941 | ␉␉}␊ |
942 | ␊ |
943 | ␉␉if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))␊ |
944 | ␉␉{␊ |
945 | ␉␉␉verbose ("Decompressing Kernel Using lzvn\n");␊ |
946 | ␉␉}␊ |
947 | ␊ |
948 | #if NOTDEF␊ |
949 | ␉␉if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
950 | ␉␉{␊ |
951 | ␉␉␉return -1;␊ |
952 | ␉␉}␊ |
953 | ␊ |
954 | ␉␉if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
955 | ␉␉{␊ |
956 | ␉␉␉return -1;␊ |
957 | ␉␉}␊ |
958 | #endif␊ |
959 | ␉␉uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
960 | ␉␉binary = buffer = malloc(uncompressed_size);␊ |
961 | ␊ |
962 | ␉␉// MinusZwei␊ |
963 | ␉␉size = 0;␊ |
964 | ␉␉switch (kernel_header->compress_type)␊ |
965 | ␉␉{␊ |
966 | ␉␉␉case OSSwapBigToHostConstInt32('lzvn'):␊ |
967 | ␉␉␉␉size = lzvn_decode(binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
968 | ␉␉␉␉break;␊ |
969 | ␊ |
970 | ␉␉␉case OSSwapBigToHostConstInt32('lzss'):␊ |
971 | ␉␉␉␉size = decompress_lzss((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
972 | ␉␉␉␉break;␊ |
973 | ␊ |
974 | ␉␉␉default:␊ |
975 | ␉␉␉␉break;␊ |
976 | ␉␉}␊ |
977 | ␉␉// MinusZwei␊ |
978 | ␊ |
979 | ␉␉if (uncompressed_size != size)␊ |
980 | ␉␉{␊ |
981 | ␉␉␉if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))␊ |
982 | ␉␉␉{␊ |
983 | ␉␉␉␉error("ERROR! Size mismatch from lzvn (found: %x, expected: %x).\n", size, uncompressed_size);␊ |
984 | ␉␉␉}␊ |
985 | ␊ |
986 | ␉␉␉if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))␊ |
987 | ␉␉␉{␊ |
988 | ␉␉␉␉error("ERROR! size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);␊ |
989 | ␉␉␉}␊ |
990 | ␊ |
991 | ␉␉␉return -1;␊ |
992 | ␉␉}␊ |
993 | ␊ |
994 | ␉␉adler32 = Adler32(binary, uncompressed_size);␊ |
995 | ␉␉if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)␊ |
996 | ␉␉{␊ |
997 | ␉␉␉error("ERROR! Adler mismatch (found: %X, expected: %X).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));␊ |
998 | ␉␉␉return -1;␊ |
999 | ␉␉}␊ |
1000 | ␊ |
1001 | ␉␉DBG("OK.\n");␊ |
1002 | ␉}␊ |
1003 | ␉␊ |
1004 | ␉ret = ThinFatFile(&binary, &len);␊ |
1005 | ␉if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
1006 | ␉{␊ |
1007 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
1008 | ␉␉ret = ThinFatFile(&binary, &len);␊ |
1009 | ␉}␊ |
1010 | ␊ |
1011 | ␉// Bungo: scan binary for Darwin Kernel Version string␊ |
1012 | ␉uint32_t offset = 0;␊ |
1013 | ␉strncpy(gDarwinBuildVerStr, "Darwin Kernel Version", sizeof(gDarwinBuildVerStr));␊ |
1014 | ␊ |
1015 | ␉while ((offset < 0xFFFFFFFF - (uint32_t)binary - 256) && memcmp(binary + offset, gDarwinBuildVerStr, 21))␊ |
1016 | ␉{␊ |
1017 | ␉␉offset++;␊ |
1018 | ␉}␊ |
1019 | ␉if (offset < 0xFFFFFFFF - (uint32_t)binary - 256)␊ |
1020 | ␉{␊ |
1021 | ␉␉strncpy(gDarwinBuildVerStr, (char *)(binary + offset), sizeof(gDarwinBuildVerStr));␊ |
1022 | ␉}␊ |
1023 | ␉else␊ |
1024 | ␉{␊ |
1025 | ␉␉strcat(gDarwinBuildVerStr, ": Unknown");␊ |
1026 | ␉}␊ |
1027 | ␊ |
1028 | ␉// Notify modules that the kernel has been decompressed, thinned and is about to be decoded␊ |
1029 | ␉execute_hook("DecodeKernel", (void *)binary, NULL, NULL, NULL);␊ |
1030 | ␊ |
1031 | /* ================================================================ */␊ |
1032 | ␊ |
1033 | ␉// Entry point␊ |
1034 | ␊ |
1035 | /* ================================================================ */␊ |
1036 | ␊ |
1037 | ␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
1038 | ␉if (ret < 0 && archCpuType == CPU_TYPE_X86_64)␊ |
1039 | ␉{␊ |
1040 | ␉␉archCpuType = CPU_TYPE_I386;␊ |
1041 | ␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
1042 | ␉}␊ |
1043 | ␊ |
1044 | ␉return ret;␊ |
1045 | }␊ |
1046 | |