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