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