Root/
Source at commit 543 created 13 years 6 months ago. By meklort, Chameleon code shrinkage. Also moved a few things to modules. | |
---|---|
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 | ␊ |
44 | extern char gMacOSVersion;␊ |
45 | ␊ |
46 | struct Module { ␊ |
47 | ␉struct Module *nextModule;␊ |
48 | ␉long willLoad;␊ |
49 | ␉TagPtr dict;␊ |
50 | ␉char *plistAddr;␊ |
51 | ␉long plistLength;␊ |
52 | ␉char *executablePath;␊ |
53 | ␉char *bundlePath;␊ |
54 | ␉long bundlePathLength;␊ |
55 | };␊ |
56 | typedef struct Module Module, *ModulePtr;␊ |
57 | ␊ |
58 | struct DriverInfo {␊ |
59 | ␉char *plistAddr;␊ |
60 | ␉long plistLength;␊ |
61 | ␉void *executableAddr;␊ |
62 | ␉long executableLength;␊ |
63 | ␉void *bundlePathAddr;␊ |
64 | ␉long bundlePathLength;␊ |
65 | };␊ |
66 | typedef struct DriverInfo DriverInfo, *DriverInfoPtr;␊ |
67 | ␊ |
68 | #define kDriverPackageSignature1 'MKXT'␊ |
69 | #define kDriverPackageSignature2 'MOSX'␊ |
70 | ␊ |
71 | struct DriversPackage {␊ |
72 | ␉unsigned long signature1;␊ |
73 | ␉unsigned long signature2;␊ |
74 | ␉unsigned long length;␊ |
75 | ␉unsigned long alder32;␊ |
76 | ␉unsigned long version;␊ |
77 | ␉unsigned long numDrivers;␊ |
78 | ␉unsigned long reserved1;␊ |
79 | ␉unsigned long reserved2;␊ |
80 | };␊ |
81 | typedef struct DriversPackage DriversPackage;␊ |
82 | ␊ |
83 | enum {␊ |
84 | ␉kCFBundleType2,␊ |
85 | ␉kCFBundleType3␊ |
86 | };␊ |
87 | ␊ |
88 | #ifndef OPTION_ROM␊ |
89 | long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);␊ |
90 | #endif␊ |
91 | ␊ |
92 | static unsigned long Alder32( unsigned char * buffer, long length );␊ |
93 | ␊ |
94 | static long FileLoadDrivers(char *dirSpec, long plugin);␊ |
95 | #ifndef OPTION_ROM␊ |
96 | static long NetLoadDrivers(char *dirSpec);␊ |
97 | #endif␊ |
98 | static long LoadDriverMKext(char *fileSpec);␊ |
99 | static long LoadDriverPList(char *dirSpec, char *name, long bundleType);␊ |
100 | static long LoadMatchedModules(void);␊ |
101 | static long MatchPersonalities(void);␊ |
102 | static long MatchLibraries(void);␊ |
103 | #ifdef NOTDEF␊ |
104 | static ModulePtr FindModule(char *name);␊ |
105 | static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);␊ |
106 | #endif␊ |
107 | static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);␊ |
108 | static long InitDriverSupport(void);␊ |
109 | ␊ |
110 | static ModulePtr gModuleHead, gModuleTail;␊ |
111 | static TagPtr gPersonalityHead, gPersonalityTail;␊ |
112 | static char * gExtensionsSpec;␊ |
113 | static char * gDriverSpec;␊ |
114 | static char * gFileSpec;␊ |
115 | static char * gTempSpec;␊ |
116 | static char * gFileName;␊ |
117 | ␊ |
118 | static unsigned long␊ |
119 | Alder32( unsigned char * buffer, long length )␊ |
120 | {␊ |
121 | long cnt;␊ |
122 | unsigned long result, lowHalf, highHalf;␊ |
123 | ␊ |
124 | lowHalf = 1;␊ |
125 | highHalf = 0;␊ |
126 | ␉␊ |
127 | ␉for ( cnt = 0; cnt < length; cnt++ )␊ |
128 | {␊ |
129 | if ((cnt % 5000) == 0)␊ |
130 | {␊ |
131 | lowHalf %= 65521L;␊ |
132 | highHalf %= 65521L;␊ |
133 | }␊ |
134 | ␉␉␊ |
135 | lowHalf += buffer[cnt];␊ |
136 | highHalf += lowHalf;␊ |
137 | }␊ |
138 | ␉␊ |
139 | ␉lowHalf %= 65521L;␊ |
140 | ␉highHalf %= 65521L;␊ |
141 | ␉␊ |
142 | ␉result = (highHalf << 16) | lowHalf;␊ |
143 | ␉␊ |
144 | ␉return result;␊ |
145 | }␊ |
146 | ␊ |
147 | ␊ |
148 | //==========================================================================␊ |
149 | // InitDriverSupport␊ |
150 | ␊ |
151 | static long␊ |
152 | InitDriverSupport( void )␊ |
153 | {␊ |
154 | gExtensionsSpec = malloc( 4096 );␊ |
155 | gDriverSpec = malloc( 4096 );␊ |
156 | gFileSpec = malloc( 4096 );␊ |
157 | gTempSpec = malloc( 4096 );␊ |
158 | gFileName = malloc( 4096 );␊ |
159 | ␉␊ |
160 | if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName )␊ |
161 | stop("InitDriverSupport error");␊ |
162 | ␉␊ |
163 | return 0;␊ |
164 | }␊ |
165 | ␊ |
166 | //==========================================================================␊ |
167 | // LoadDrivers␊ |
168 | ␊ |
169 | long LoadDrivers( char * dirSpec )␊ |
170 | {␊ |
171 | char dirSpecExtra[1024];␊ |
172 | ␉␊ |
173 | if ( InitDriverSupport() != 0 )␊ |
174 | return 0;␊ |
175 | ␉␊ |
176 | #ifndef OPTION_ROM␊ |
177 | // Load extra drivers if a hook has been installed.␊ |
178 | if (LoadExtraDrivers_p != NULL)␊ |
179 | {␊ |
180 | (*LoadExtraDrivers_p)(&FileLoadDrivers);␊ |
181 | }␊ |
182 | ␉␊ |
183 | if ( gBootFileType == kNetworkDeviceType )␊ |
184 | {␊ |
185 | if (NetLoadDrivers(dirSpec) != 0) {␊ |
186 | error("Could not load drivers from the network\n");␊ |
187 | return -1;␊ |
188 | }␊ |
189 | }␊ |
190 | else␊ |
191 | #endif␊ |
192 | ␉␉if ( gBootFileType == kBlockDeviceType )␊ |
193 | ␉␉{␊ |
194 | ␉␉␉// First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).␊ |
195 | #ifndef OPTION_ROM␊ |
196 | ␉␉␉if (gRAMDiskVolume && !gRAMDiskBTAliased)␊ |
197 | ␉␉␉{␊ |
198 | ␉␉␉␉strcpy(dirSpecExtra, "rd(0,0)/Extra/");␊ |
199 | ␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
200 | ␉␉␉}␊ |
201 | #endif␊ |
202 | ␉␉␉// Next try to load Extra extensions from the selected root partition.␊ |
203 | ␉␉␉strcpy(dirSpecExtra, "/Extra/");␊ |
204 | ␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
205 | ␉␉␉{␊ |
206 | ␉␉␉␉// If failed, then try to load Extra extensions from the boot partition␊ |
207 | ␉␉␉␉// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.␊ |
208 | ␉␉␉␉if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)␊ |
209 | #ifndef OPTION_ROM␊ |
210 | ␉␉␉␉␉|| (gRAMDiskVolume && gRAMDiskBTAliased) )␊ |
211 | #else␊ |
212 | ␉␉␉␉␉)␊ |
213 | #endif␊ |
214 | ␉␉␉␉{␊ |
215 | ␉␉␉␉␉// First try a specfic OS version folder ie 10.5␊ |
216 | ␉␉␉␉␉sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);␊ |
217 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
218 | ␉␉␉␉␉{␉␊ |
219 | ␉␉␉␉␉␉// Next we'll try the base␊ |
220 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "bt(0,0)/Extra/");␊ |
221 | ␉␉␉␉␉␉FileLoadDrivers(dirSpecExtra, 0);␊ |
222 | ␉␉␉␉␉}␊ |
223 | ␉␉␉␉}␊ |
224 | ␉␉␉}␊ |
225 | ␉␉␉␊ |
226 | ␉␉␉// TODO: fix this, the order does matter, and it's not correct now.␊ |
227 | ␉␉␉// Also try to load Extensions from boot helper partitions.␊ |
228 | ␉␉␉if (gBootVolume->flags & kBVFlagBooter)␊ |
229 | ␉␉␉{␊ |
230 | ␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");␊ |
231 | ␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
232 | ␉␉␉␉{␊ |
233 | ␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");␊ |
234 | ␉␉␉␉␉if (FileLoadDrivers(dirSpecExtra, 0) != 0)␊ |
235 | ␉␉␉␉␉{␊ |
236 | ␉␉␉␉␉␉strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");␊ |
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 | ␉␉␉␉strcpy(gExtensionsSpec, dirSpec);␊ |
254 | ␉␉␉␉strcat(gExtensionsSpec, "System/Library/");␊ |
255 | ␉␉␉␉FileLoadDrivers(gExtensionsSpec, 0);␊ |
256 | ␉␉␉}␊ |
257 | ␉␉}␊ |
258 | ␉␉else␊ |
259 | ␉␉{␊ |
260 | ␉␉␉return 0;␊ |
261 | ␉␉}␊ |
262 | ␉␊ |
263 | MatchPersonalities();␊ |
264 | ␉␊ |
265 | MatchLibraries();␊ |
266 | ␉␊ |
267 | LoadMatchedModules();␊ |
268 | ␉␊ |
269 | return 0;␊ |
270 | }␊ |
271 | ␊ |
272 | //==========================================================================␊ |
273 | // FileLoadMKext␊ |
274 | ␊ |
275 | static long␊ |
276 | FileLoadMKext( const char * dirSpec, const char * extDirSpec )␊ |
277 | {␊ |
278 | ␉long ret, flags, time, time2;␊ |
279 | ␉char altDirSpec[512];␊ |
280 | ␉␊ |
281 | ␉sprintf (altDirSpec, "%s%s", dirSpec, extDirSpec);␊ |
282 | ␉ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);␊ |
283 | ␉if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))␊ |
284 | ␉{␊ |
285 | ␉␉ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);␊ |
286 | ␉␉if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeDirectory) ||␊ |
287 | ␉␉␉(((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))␊ |
288 | ␉␉{␊ |
289 | ␉␉␉sprintf(gDriverSpec, "%sExtensions.mkext", altDirSpec);␊ |
290 | ␉␉␉verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
291 | ␉␉␉if (LoadDriverMKext(gDriverSpec) == 0) return 0;␊ |
292 | ␉␉}␊ |
293 | ␉}␊ |
294 | ␉return -1;␊ |
295 | }␊ |
296 | ␊ |
297 | //==========================================================================␊ |
298 | // FileLoadDrivers␊ |
299 | ␊ |
300 | static long␊ |
301 | FileLoadDrivers( char * dirSpec, long plugin )␊ |
302 | {␊ |
303 | long ret, length, flags, time, bundleType;␊ |
304 | long long␉ index;␊ |
305 | long result = -1;␊ |
306 | const char * name;␊ |
307 | ␉␊ |
308 | if ( !plugin )␊ |
309 | {␊ |
310 | // First try 10.6's path for loading Extensions.mkext.␊ |
311 | if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0)␊ |
312 | ␉␉␉return 0;␊ |
313 | ␉␉␊ |
314 | // Next try the legacy path.␊ |
315 | else if (FileLoadMKext(dirSpec, "") == 0)␊ |
316 | ␉␉␉return 0;␊ |
317 | ␉␉␊ |
318 | strcat(dirSpec, "Extensions");␊ |
319 | }␊ |
320 | ␉␊ |
321 | index = 0;␊ |
322 | while (1) {␊ |
323 | ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);␊ |
324 | if (ret == -1) break;␊ |
325 | ␉␉␊ |
326 | // Make sure this is a directory.␊ |
327 | if ((flags & kFileTypeMask) != kFileTypeDirectory) continue;␊ |
328 | ␊ |
329 | // Make sure this is a kext.␊ |
330 | length = strlen(name);␊ |
331 | if (strcmp(name + length - 5, ".kext")) continue;␊ |
332 | ␉␉␊ |
333 | // Save the file name.␊ |
334 | strcpy(gFileName, name);␊ |
335 | ␉␉␊ |
336 | // Determine the bundle type.␊ |
337 | sprintf(gTempSpec, "%s/%s", dirSpec, gFileName);␊ |
338 | ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);␊ |
339 | if (ret == 0) bundleType = kCFBundleType2;␊ |
340 | else bundleType = kCFBundleType3;␊ |
341 | ␉␉␊ |
342 | if (!plugin)␊ |
343 | sprintf(gDriverSpec, "%s/%s/%sPlugIns", dirSpec, gFileName,␊ |
344 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
345 | ␉␉␊ |
346 | ret = LoadDriverPList(dirSpec, gFileName, bundleType);␊ |
347 | ␉␉␊ |
348 | if (result != 0)␊ |
349 | ␉␉␉result = ret;␊ |
350 | ␉␉␊ |
351 | if (!plugin) ␊ |
352 | ␉␉␉FileLoadDrivers(gDriverSpec, 1);␊ |
353 | }␊ |
354 | ␉␊ |
355 | return result;␊ |
356 | }␊ |
357 | ␊ |
358 | //==========================================================================␊ |
359 | // ␊ |
360 | #ifndef OPTION_ROM␊ |
361 | static long␊ |
362 | NetLoadDrivers( char * dirSpec )␊ |
363 | {␊ |
364 | long tries;␊ |
365 | ␉␊ |
366 | #if NODEF␊ |
367 | long cnt;␊ |
368 | ␉␊ |
369 | // Get the name of the kernel␊ |
370 | cnt = strlen(gBootFile);␊ |
371 | while (cnt--) {␊ |
372 | if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {␊ |
373 | ␉␉␉cnt++;␊ |
374 | ␉␉␉break;␊ |
375 | }␊ |
376 | }␊ |
377 | #endif␊ |
378 | ␉␊ |
379 | // INTEL modification␊ |
380 | sprintf(gDriverSpec, "%s%s.mkext", dirSpec, bootInfo->bootFile);␊ |
381 | ␊ |
382 | verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);␊ |
383 | ␊ |
384 | tries = 3;␊ |
385 | while (tries--)␊ |
386 | {␊ |
387 | if (LoadDriverMKext(gDriverSpec) == 0) break;␊ |
388 | }␊ |
389 | if (tries == -1) return -1;␊ |
390 | ␉␊ |
391 | return 0;␊ |
392 | }␊ |
393 | #endif␊ |
394 | //==========================================================================␊ |
395 | // loadDriverMKext␊ |
396 | ␊ |
397 | static long␊ |
398 | LoadDriverMKext( char * fileSpec )␊ |
399 | {␊ |
400 | unsigned long driversAddr, driversLength;␊ |
401 | long length;␊ |
402 | char segName[32];␊ |
403 | DriversPackage * package;␊ |
404 | ␉␊ |
405 | #define GetPackageElement(e) OSSwapBigToHostInt32(package->e)␊ |
406 | ␉␊ |
407 | // Load the MKext.␊ |
408 | length = LoadThinFatFile(fileSpec, (void **)&package);␊ |
409 | if (length < sizeof (DriversPackage)) return -1;␊ |
410 | ␉␊ |
411 | ␉// call hook to notify modules that the mkext has been loaded␊ |
412 | ␉execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) length, NULL);␊ |
413 | ␉␊ |
414 | ␉␊ |
415 | // Verify the MKext.␊ |
416 | if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||␊ |
417 | ( GetPackageElement(signature2) != kDriverPackageSignature2) ||␊ |
418 | ( GetPackageElement(length) > kLoadSize ) ||␊ |
419 | ( GetPackageElement(alder32) !=␊ |
420 | ␉␉ Alder32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )␊ |
421 | {␊ |
422 | return -1;␊ |
423 | }␊ |
424 | ␉␊ |
425 | ␉␊ |
426 | // Make space for the MKext.␊ |
427 | driversLength = GetPackageElement(length);␊ |
428 | driversAddr = AllocateKernelMemory(driversLength);␊ |
429 | ␉␊ |
430 | // Copy the MKext.␊ |
431 | memcpy((void *)driversAddr, (void *)package, driversLength);␊ |
432 | ␉␊ |
433 | // Add the MKext to the memory map.␊ |
434 | sprintf(segName, "DriversPackage-%lx", driversAddr);␊ |
435 | AllocateMemoryRange(segName, driversAddr, driversLength,␊ |
436 | kBootDriverTypeMKEXT);␊ |
437 | ␉␊ |
438 | return 0;␊ |
439 | }␊ |
440 | ␊ |
441 | //==========================================================================␊ |
442 | // LoadDriverPList␊ |
443 | ␊ |
444 | static long␊ |
445 | LoadDriverPList( char * dirSpec, char * name, long bundleType )␊ |
446 | {␊ |
447 | long length, executablePathLength, bundlePathLength;␊ |
448 | ModulePtr module;␊ |
449 | TagPtr personalities;␊ |
450 | char * buffer = 0;␊ |
451 | char * tmpExecutablePath = 0;␊ |
452 | char * tmpBundlePath = 0;␊ |
453 | long ret = -1;␊ |
454 | ␉␊ |
455 | do {␊ |
456 | // Save the driver path.␊ |
457 | ␊ |
458 | sprintf(gFileSpec, "%s/%s/%s", dirSpec, name,␊ |
459 | (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");␊ |
460 | executablePathLength = strlen(gFileSpec) + 1;␊ |
461 | ␉␉␊ |
462 | tmpExecutablePath = malloc(executablePathLength);␊ |
463 | if (tmpExecutablePath == 0) break;␊ |
464 | ␉␉␊ |
465 | strcpy(tmpExecutablePath, gFileSpec);␊ |
466 | ␉␉␊ |
467 | sprintf(gFileSpec, "%s/%s", dirSpec, name);␊ |
468 | bundlePathLength = strlen(gFileSpec) + 1;␊ |
469 | ␉␉␊ |
470 | tmpBundlePath = malloc(bundlePathLength);␊ |
471 | if (tmpBundlePath == 0) break;␊ |
472 | ␉␉␊ |
473 | strcpy(tmpBundlePath, gFileSpec);␊ |
474 | ␉␉␊ |
475 | // Construct the file spec to the plist, then load it.␊ |
476 | ␉␉␊ |
477 | sprintf(gFileSpec, "%s/%s/%sInfo.plist", dirSpec, name,␊ |
478 | (bundleType == kCFBundleType2) ? "Contents/" : "");␊ |
479 | ␉␉␊ |
480 | length = LoadFile(gFileSpec);␊ |
481 | if (length == -1) break;␊ |
482 | ␉␉␊ |
483 | length = length + 1;␊ |
484 | buffer = malloc(length);␊ |
485 | if (buffer == 0) break;␊ |
486 | ␉␉␊ |
487 | strlcpy(buffer, (char *)kLoadAddr, length);␊ |
488 | ␉␉␊ |
489 | // Parse the plist.␊ |
490 | ␉␉␊ |
491 | ret = ParseXML(buffer, &module, &personalities);␊ |
492 | if (ret != 0) { break; }␊ |
493 | ␉␉␊ |
494 | // Allocate memory for the driver path and the plist.␊ |
495 | ␉␉␊ |
496 | module->executablePath = tmpExecutablePath;␊ |
497 | module->bundlePath = tmpBundlePath;␊ |
498 | module->bundlePathLength = bundlePathLength;␊ |
499 | module->plistAddr = malloc(length);␊ |
500 | ␉␉␊ |
501 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
502 | break;␊ |
503 | ␉␉␊ |
504 | // Save the driver path in the module.␊ |
505 | //strcpy(module->driverPath, tmpDriverPath);␊ |
506 | tmpExecutablePath = 0;␊ |
507 | tmpBundlePath = 0;␊ |
508 | ␉␉␊ |
509 | // Add the plist to the module.␊ |
510 | ␉␉␊ |
511 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
512 | module->plistLength = length;␊ |
513 | ␉␉␊ |
514 | // Add the module to the end of the module list.␊ |
515 | ␊ |
516 | if (gModuleHead == 0)␊ |
517 | gModuleHead = module;␊ |
518 | else␊ |
519 | gModuleTail->nextModule = module;␊ |
520 | gModuleTail = module;␊ |
521 | ␉␉␊ |
522 | // Add the persionalities to the personality list.␊ |
523 | ␉␉␊ |
524 | if (personalities) personalities = personalities->tag;␊ |
525 | while (personalities != 0)␊ |
526 | {␊ |
527 | if (gPersonalityHead == 0)␊ |
528 | gPersonalityHead = personalities->tag;␊ |
529 | else␊ |
530 | gPersonalityTail->tagNext = personalities->tag;␊ |
531 | ␊ |
532 | gPersonalityTail = personalities->tag;␊ |
533 | personalities = personalities->tagNext;␊ |
534 | }␊ |
535 | ␊ |
536 | ret = 0;␊ |
537 | }␊ |
538 | while (0);␊ |
539 | ␊ |
540 | if ( buffer ) free( buffer );␊ |
541 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
542 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
543 | ␉␊ |
544 | return ret;␊ |
545 | }␊ |
546 | ␊ |
547 | ␊ |
548 | //==========================================================================␊ |
549 | // LoadMatchedModules␊ |
550 | ␊ |
551 | static long␊ |
552 | LoadMatchedModules( void )␊ |
553 | {␊ |
554 | TagPtr prop;␊ |
555 | ModulePtr module;␊ |
556 | char *fileName, segName[32];␊ |
557 | DriverInfoPtr driver;␊ |
558 | long length, driverAddr, driverLength;␊ |
559 | void *executableAddr = 0;␊ |
560 | ␉␊ |
561 | ␉␊ |
562 | module = gModuleHead;␊ |
563 | ␉␊ |
564 | while (module != 0)␊ |
565 | {␊ |
566 | if (module->willLoad)␊ |
567 | {␊ |
568 | prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
569 | ␉␉␉␊ |
570 | if (prop != 0)␊ |
571 | {␊ |
572 | fileName = prop->string;␊ |
573 | sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
574 | length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
575 | ␉␉␉␉if (length == 0)␊ |
576 | ␉␉␉␉{␊ |
577 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
578 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
579 | ␉␉␉␉}␊ |
580 | //printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getc();␊ |
581 | }␊ |
582 | else␊ |
583 | length = 0;␊ |
584 | ␉␉␉␊ |
585 | if (length != -1)␊ |
586 | {␊ |
587 | ␉␉␉␉//driverModuleAddr = (void *)kLoadAddr;␊ |
588 | //if (length != 0)␊ |
589 | //{␊ |
590 | ␉␉␉␉// ThinFatFile(&driverModuleAddr, &length);␊ |
591 | ␉␉␉␉//}␊ |
592 | ␉␉␉␉␊ |
593 | // Make make in the image area.␊ |
594 | ␉␉␉␉␊ |
595 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);␊ |
596 | ␉␉␉␉␊ |
597 | driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
598 | driverAddr = AllocateKernelMemory(driverLength);␊ |
599 | ␉␉␉␉␊ |
600 | // Set up the DriverInfo.␊ |
601 | driver = (DriverInfoPtr)driverAddr;␊ |
602 | driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
603 | driver->plistLength = module->plistLength;␊ |
604 | if (length != 0)␊ |
605 | {␊ |
606 | driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
607 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
608 | driver->executableLength = length;␊ |
609 | }␊ |
610 | else␊ |
611 | {␊ |
612 | driver->executableAddr = 0;␊ |
613 | driver->executableLength = 0;␊ |
614 | }␊ |
615 | driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
616 | ␉␉␉␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
617 | driver->bundlePathLength = module->bundlePathLength;␊ |
618 | ␉␉␉␉␊ |
619 | // Save the plist, module and bundle.␊ |
620 | strcpy(driver->plistAddr, module->plistAddr);␊ |
621 | if (length != 0)␊ |
622 | {␊ |
623 | memcpy(driver->executableAddr, executableAddr, length);␊ |
624 | }␊ |
625 | strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
626 | ␉␉␉␉␊ |
627 | // Add an entry to the memory map.␊ |
628 | sprintf(segName, "Driver-%lx", (unsigned long)driver);␊ |
629 | AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
630 | kBootDriverTypeKEXT);␊ |
631 | }␊ |
632 | }␊ |
633 | module = module->nextModule;␊ |
634 | }␊ |
635 | ␉␊ |
636 | return 0;␊ |
637 | }␊ |
638 | ␊ |
639 | //==========================================================================␊ |
640 | // MatchPersonalities␊ |
641 | ␊ |
642 | static long␊ |
643 | MatchPersonalities( void )␊ |
644 | {␊ |
645 | /* IONameMatch support not implemented */␊ |
646 | return 0;␊ |
647 | }␊ |
648 | ␊ |
649 | //==========================================================================␊ |
650 | // MatchLibraries␊ |
651 | ␊ |
652 | static long␊ |
653 | MatchLibraries( void )␊ |
654 | {␊ |
655 | TagPtr prop, prop2;␊ |
656 | ModulePtr module, module2;␊ |
657 | long done;␊ |
658 | ␉␊ |
659 | do {␊ |
660 | done = 1;␊ |
661 | module = gModuleHead;␊ |
662 | ␊ |
663 | while (module != 0)␊ |
664 | {␊ |
665 | if (module->willLoad == 1)␊ |
666 | {␊ |
667 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
668 | if (prop != 0)␊ |
669 | {␊ |
670 | prop = prop->tag;␊ |
671 | while (prop != 0)␊ |
672 | {␊ |
673 | module2 = gModuleHead;␊ |
674 | while (module2 != 0)␊ |
675 | {␊ |
676 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
677 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
678 | {␊ |
679 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
680 | break;␊ |
681 | }␊ |
682 | module2 = module2->nextModule;␊ |
683 | }␊ |
684 | prop = prop->tagNext;␊ |
685 | }␊ |
686 | }␊ |
687 | module->willLoad = 2;␊ |
688 | done = 0;␊ |
689 | }␊ |
690 | module = module->nextModule;␊ |
691 | }␊ |
692 | }␊ |
693 | while (!done);␊ |
694 | ␉␊ |
695 | return 0;␊ |
696 | }␊ |
697 | ␊ |
698 | ␊ |
699 | //==========================================================================␊ |
700 | // FindModule␊ |
701 | ␊ |
702 | #if NOTDEF␊ |
703 | static ModulePtr␊ |
704 | FindModule( char * name )␊ |
705 | {␊ |
706 | ModulePtr module;␊ |
707 | TagPtr prop;␊ |
708 | ␊ |
709 | module = gModuleHead;␊ |
710 | ␊ |
711 | while (module != 0)␊ |
712 | {␊ |
713 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
714 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
715 | module = module->nextModule;␊ |
716 | }␊ |
717 | ␊ |
718 | return module;␊ |
719 | }␊ |
720 | #endif /* NOTDEF */␊ |
721 | ␊ |
722 | //==========================================================================␊ |
723 | // ParseXML␊ |
724 | ␊ |
725 | static long␊ |
726 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
727 | {␊ |
728 | ␉long length, pos;␊ |
729 | ␉TagPtr moduleDict, required;␊ |
730 | ␉ModulePtr tmpModule;␊ |
731 | ␉␊ |
732 | pos = 0;␊ |
733 | ␉␊ |
734 | while (1)␊ |
735 | {␊ |
736 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
737 | if (length == -1) break;␊ |
738 | ␉␉␊ |
739 | pos += length;␊ |
740 | ␉␉␊ |
741 | if (moduleDict == 0) continue;␊ |
742 | if (moduleDict->type == kTagTypeDict) break;␊ |
743 | ␉␉␊ |
744 | XMLFreeTag(moduleDict);␊ |
745 | }␊ |
746 | ␉␊ |
747 | if (length == -1) return -1;␊ |
748 | ␉␊ |
749 | required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
750 | if ( (required == 0) ||␊ |
751 | ␉␉(required->type != kTagTypeString) ||␊ |
752 | ␉␉!strcmp(required->string, "Safe Boot"))␊ |
753 | {␊ |
754 | XMLFreeTag(moduleDict);␊ |
755 | return -2;␊ |
756 | }␊ |
757 | ␉␊ |
758 | tmpModule = malloc(sizeof(Module));␊ |
759 | if (tmpModule == 0)␊ |
760 | {␊ |
761 | XMLFreeTag(moduleDict);␊ |
762 | return -1;␊ |
763 | }␊ |
764 | tmpModule->dict = moduleDict;␊ |
765 | ␉␊ |
766 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
767 | ␉␊ |
768 | tmpModule->willLoad = 1;␊ |
769 | ␉␊ |
770 | *module = tmpModule;␊ |
771 | ␉␊ |
772 | // Get the personalities.␊ |
773 | ␉␊ |
774 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
775 | ␉␊ |
776 | return 0;␊ |
777 | }␊ |
778 | ␊ |
779 | #if NOTDEF␊ |
780 | static char gPlatformName[64];␊ |
781 | #endif␊ |
782 | ␊ |
783 | long ␊ |
784 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
785 | {␊ |
786 | long ret;␊ |
787 | compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
788 | u_int32_t uncompressed_size, size;␊ |
789 | void *buffer;␊ |
790 | ␉unsigned long len;␊ |
791 | ␉␊ |
792 | #if 0␊ |
793 | printf("kernel header:\n");␊ |
794 | printf("signature: 0x%x\n", kernel_header->signature);␊ |
795 | printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
796 | printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
797 | printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
798 | printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
799 | getc();␊ |
800 | #endif␊ |
801 | ␉␊ |
802 | if (kernel_header->signature == OSSwapBigToHostConstInt32('comp')) {␊ |
803 | if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) {␊ |
804 | error("kernel compression is bad\n");␊ |
805 | return -1;␊ |
806 | }␊ |
807 | #if NOTDEF␊ |
808 | if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))␊ |
809 | return -1;␊ |
810 | if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))␊ |
811 | return -1;␊ |
812 | #endif␊ |
813 | ␉␉␊ |
814 | uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
815 | binary = buffer = malloc(uncompressed_size);␊ |
816 | ␉␉␊ |
817 | size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
818 | OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
819 | if (uncompressed_size != size) {␊ |
820 | error("size mismatch from lzss: %x\n", size);␊ |
821 | return -1;␊ |
822 | }␊ |
823 | if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
824 | Alder32(binary, uncompressed_size)) {␊ |
825 | printf("adler mismatch\n");␊ |
826 | return -1;␊ |
827 | }␊ |
828 | }␊ |
829 | ␉␊ |
830 | ␉ret = ThinFatFile(&binary, &len);␊ |
831 | ␉if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
832 | ␉{␊ |
833 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
834 | ␉␉ret = ThinFatFile(&binary, &len);␊ |
835 | ␉}␊ |
836 | ␉␊ |
837 | ␉//patch_kernel(binary);␊ |
838 | ␉␊ |
839 | ␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
840 | ␉␊ |
841 | ␉if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
842 | ␉{␊ |
843 | ␉␉archCpuType=CPU_TYPE_I386;␊ |
844 | ␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
845 | ␉}␊ |
846 | ␉␊ |
847 | ␉␊ |
848 | ␉return ret;␊ |
849 | }␊ |
850 |