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