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