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