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