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