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