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