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