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