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