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