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 = 0;␊ |
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 | ␉␉if (!module) {ret = -1;break;} // Should never happen but it will make the compiler happy␊ |
484 | ␊ |
485 | // Allocate memory for the driver path and the plist.␊ |
486 | ␉␉␊ |
487 | module->executablePath = tmpExecutablePath;␊ |
488 | module->bundlePath = tmpBundlePath;␊ |
489 | module->bundlePathLength = bundlePathLength;␊ |
490 | module->plistAddr = malloc(length);␊ |
491 | ␉␉␊ |
492 | if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))␊ |
493 | {␊ |
494 | if ( module->plistAddr ) free(module->plistAddr);␊ |
495 | ␉␉␉ret = -1;␊ |
496 | break;␊ |
497 | }␊ |
498 | ␉␉␊ |
499 | // Save the driver path in the module.␊ |
500 | //strcpy(module->driverPath, tmpDriverPath);␊ |
501 | //tmpExecutablePath = 0;␊ |
502 | //tmpBundlePath = 0;␊ |
503 | ␉␉␊ |
504 | // Add the plist to the module.␊ |
505 | ␉␉␊ |
506 | strlcpy(module->plistAddr, (char *)kLoadAddr, length);␊ |
507 | module->plistLength = length;␊ |
508 | ␉␉␊ |
509 | // Add the module to the end of the module list.␊ |
510 | ␊ |
511 | if (gModuleHead == 0)␊ |
512 | gModuleHead = module;␊ |
513 | else␊ |
514 | gModuleTail->nextModule = module;␊ |
515 | gModuleTail = module;␊ |
516 | ␉␉␊ |
517 | // Add the persionalities to the personality list.␊ |
518 | ␉␉␊ |
519 | if (personalities) personalities = personalities->tag;␊ |
520 | while (personalities != 0)␊ |
521 | {␊ |
522 | if (gPersonalityHead == 0)␊ |
523 | gPersonalityHead = personalities->tag;␊ |
524 | else␊ |
525 | gPersonalityTail->tagNext = personalities->tag;␊ |
526 | ␊ |
527 | gPersonalityTail = personalities->tag;␊ |
528 | personalities = personalities->tagNext;␊ |
529 | }␊ |
530 | ␊ |
531 | ret = 0;␊ |
532 | }␊ |
533 | while (0);␊ |
534 | ␊ |
535 | if ( buffer ) free( buffer );␊ |
536 | if (ret != 0)␊ |
537 | {␊ |
538 | if ( tmpExecutablePath ) free( tmpExecutablePath );␊ |
539 | if ( tmpBundlePath ) free( tmpBundlePath );␊ |
540 | if ( module ) free( module );␊ |
541 | }␉␊ |
542 | return ret;␊ |
543 | }␊ |
544 | ␊ |
545 | ␊ |
546 | //==========================================================================␊ |
547 | // LoadMatchedModules␊ |
548 | ␊ |
549 | long LoadMatchedModules( void )␊ |
550 | {␊ |
551 | TagPtr prop;␊ |
552 | ModulePtr module;␊ |
553 | char *fileName, segName[32];␊ |
554 | DriverInfoPtr driver;␊ |
555 | long length, driverAddr, driverLength;␊ |
556 | void *executableAddr = 0;␊ |
557 | ␉␊ |
558 | ␉␊ |
559 | module = gModuleHead;␊ |
560 | ␉␊ |
561 | while (module != 0)␊ |
562 | {␊ |
563 | if (module->willLoad)␊ |
564 | {␊ |
565 | prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);␊ |
566 | ␉␉␉␊ |
567 | if (prop != 0)␊ |
568 | {␊ |
569 | fileName = prop->string;␊ |
570 | sprintf(gFileSpec, "%s%s", module->executablePath, fileName);␊ |
571 | length = LoadThinFatFile(gFileSpec, &executableAddr);␊ |
572 | ␉␉␉␉if (length == 0)␊ |
573 | ␉␉␉␉{␊ |
574 | ␉␉␉␉␉length = LoadFile(gFileSpec);␊ |
575 | ␉␉␉␉␉executableAddr = (void *)kLoadAddr;␊ |
576 | ␉␉␉␉}␊ |
577 | //printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getc();␊ |
578 | ␊ |
579 | }␊ |
580 | else␊ |
581 | length = 0;␊ |
582 | ␉␉␉␊ |
583 | if ((length != -1) && executableAddr)␊ |
584 | {␊ |
585 | ␉␉␉␉//driverModuleAddr = (void *)kLoadAddr;␊ |
586 | //if (length != 0)␊ |
587 | //{␊ |
588 | ␉␉␉␉// ThinFatFile(&driverModuleAddr, &length);␊ |
589 | ␉␉␉␉//}␊ |
590 | ␉␉␉␉␊ |
591 | // Make make in the image area.␊ |
592 | ␉␉␉␉␊ |
593 | ␉␉␉␉execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL, NULL, NULL);␊ |
594 | ␉␉␉␉␊ |
595 | driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;␊ |
596 | driverAddr = AllocateKernelMemory(driverLength);␊ |
597 | ␉␉␉␉␊ |
598 | // Set up the DriverInfo.␊ |
599 | driver = (DriverInfoPtr)driverAddr;␊ |
600 | driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));␊ |
601 | driver->plistLength = module->plistLength;␊ |
602 | if (length != 0)␊ |
603 | {␊ |
604 | driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
605 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ module->plistLength);␊ |
606 | driver->executableLength = length;␊ |
607 | }␊ |
608 | else␊ |
609 | {␊ |
610 | driver->executableAddr = 0;␊ |
611 | driver->executableLength = 0;␊ |
612 | }␊ |
613 | driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +␊ |
614 | ␉␉␉␉␉␉␉␉␉␉␉␉ module->plistLength + driver->executableLength);␊ |
615 | driver->bundlePathLength = module->bundlePathLength;␊ |
616 | ␉␉␉␉␊ |
617 | // Save the plist, module and bundle.␊ |
618 | strcpy(driver->plistAddr, module->plistAddr);␊ |
619 | if (length != 0)␊ |
620 | {␊ |
621 | memcpy(driver->executableAddr, executableAddr, length);␊ |
622 | }␊ |
623 | strcpy(driver->bundlePathAddr, module->bundlePath);␊ |
624 | ␉␉␉␉␊ |
625 | // Add an entry to the memory map.␊ |
626 | sprintf(segName, "Driver-%lx", (unsigned long)driver); ␊ |
627 | #if UNUSED␊ |
628 | AllocateMemoryRange(segName, driverAddr, driverLength,␊ |
629 | kBootDriverTypeKEXT);␊ |
630 | #else␊ |
631 | AllocateMemoryRange(segName, driverAddr, driverLength);␊ |
632 | #endif␊ |
633 | ␊ |
634 | }␊ |
635 | }␊ |
636 | module = module->nextModule;␊ |
637 | }␊ |
638 | ␉␊ |
639 | return 0;␊ |
640 | }␊ |
641 | ␊ |
642 | #if UNUSED␊ |
643 | //==========================================================================␊ |
644 | // MatchPersonalities␊ |
645 | ␊ |
646 | long MatchPersonalities( void )␊ |
647 | {␊ |
648 | /* IONameMatch support not implemented */␊ |
649 | return 0;␊ |
650 | }␊ |
651 | #endif␊ |
652 | ␊ |
653 | //==========================================================================␊ |
654 | // MatchLibraries␊ |
655 | ␊ |
656 | long MatchLibraries( void )␊ |
657 | {␊ |
658 | TagPtr prop, prop2;␊ |
659 | ModulePtr module, module2;␊ |
660 | long done;␊ |
661 | ␉␊ |
662 | do {␊ |
663 | done = 1;␊ |
664 | module = gModuleHead;␊ |
665 | ␊ |
666 | while (module != 0)␊ |
667 | {␊ |
668 | if (module->willLoad == 1)␊ |
669 | {␊ |
670 | prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);␊ |
671 | if (prop != 0)␊ |
672 | {␊ |
673 | prop = prop->tag;␊ |
674 | while (prop != 0)␊ |
675 | {␊ |
676 | module2 = gModuleHead;␊ |
677 | while (module2 != 0)␊ |
678 | {␊ |
679 | prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);␊ |
680 | if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))␊ |
681 | {␊ |
682 | if (module2->willLoad == 0) module2->willLoad = 1;␊ |
683 | break;␊ |
684 | }␊ |
685 | module2 = module2->nextModule;␊ |
686 | }␊ |
687 | prop = prop->tagNext;␊ |
688 | }␊ |
689 | }␊ |
690 | module->willLoad = 2;␊ |
691 | done = 0;␊ |
692 | }␊ |
693 | module = module->nextModule;␊ |
694 | }␊ |
695 | }␊ |
696 | while (!done);␊ |
697 | ␉␊ |
698 | return 0;␊ |
699 | }␊ |
700 | ␊ |
701 | ␊ |
702 | //==========================================================================␊ |
703 | // FindModule␊ |
704 | ␊ |
705 | #if UNUSED␊ |
706 | static ModulePtr␊ |
707 | FindModule( char * name )␊ |
708 | {␊ |
709 | ModulePtr module;␊ |
710 | TagPtr prop;␊ |
711 | ␊ |
712 | module = gModuleHead;␊ |
713 | ␊ |
714 | while (module != 0)␊ |
715 | {␊ |
716 | prop = GetProperty(module->dict, kPropCFBundleIdentifier);␊ |
717 | if ((prop != 0) && !strcmp(name, prop->string)) break;␊ |
718 | module = module->nextModule;␊ |
719 | }␊ |
720 | ␊ |
721 | return module;␊ |
722 | }␊ |
723 | #endif␊ |
724 | ␊ |
725 | //==========================================================================␊ |
726 | // ParseXML␊ |
727 | ␊ |
728 | static long␊ |
729 | ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )␊ |
730 | {␊ |
731 | ␉long length, pos;␊ |
732 | ␉TagPtr moduleDict, required;␊ |
733 | ␉ModulePtr tmpModule;␊ |
734 | ␉␊ |
735 | pos = 0;␊ |
736 | ␉␊ |
737 | while (1)␊ |
738 | {␊ |
739 | length = XMLParseNextTag(buffer + pos, &moduleDict);␊ |
740 | if (length == -1) break;␊ |
741 | ␉␉␊ |
742 | pos += length;␊ |
743 | ␉␉␊ |
744 | if (moduleDict == 0) continue;␊ |
745 | if (moduleDict->type == kTagTypeDict) break;␊ |
746 | ␉␉␊ |
747 | XMLFreeTag(moduleDict);␊ |
748 | }␊ |
749 | ␉␊ |
750 | if (length == -1) return -1;␊ |
751 | ␉␊ |
752 | required = XMLGetProperty(moduleDict, kPropOSBundleRequired);␊ |
753 | if ( (required == 0) ||␊ |
754 | ␉␉(required->type != kTagTypeString) ||␊ |
755 | ␉␉!strcmp(required->string, "Safe Boot"))␊ |
756 | {␊ |
757 | XMLFreeTag(moduleDict);␊ |
758 | return -2;␊ |
759 | }␊ |
760 | ␉␊ |
761 | tmpModule = malloc(sizeof(Module));␊ |
762 | if (tmpModule == 0)␊ |
763 | {␊ |
764 | XMLFreeTag(moduleDict);␊ |
765 | return -1;␊ |
766 | }␊ |
767 | tmpModule->dict = moduleDict;␊ |
768 | ␉␊ |
769 | // For now, load any module that has OSBundleRequired != "Safe Boot".␊ |
770 | ␉␊ |
771 | tmpModule->willLoad = 1;␊ |
772 | ␉␊ |
773 | *module = tmpModule;␊ |
774 | ␉␊ |
775 | // Get the personalities.␊ |
776 | ␉␊ |
777 | *personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);␊ |
778 | ␉␊ |
779 | return 0;␊ |
780 | }␊ |
781 | ␊ |
782 | ␊ |
783 | long ␊ |
784 | DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)␊ |
785 | {␊ |
786 | long ret;␊ |
787 | compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;␊ |
788 | ␉␊ |
789 | #if 0␊ |
790 | printf("kernel header:\n");␊ |
791 | printf("signature: 0x%x\n", kernel_header->signature);␊ |
792 | printf("compress_type: 0x%x\n", kernel_header->compress_type);␊ |
793 | printf("adler32: 0x%x\n", kernel_header->adler32);␊ |
794 | printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);␊ |
795 | printf("compressed_size: 0x%x\n", kernel_header->compressed_size);␊ |
796 | getc();␊ |
797 | #endif␊ |
798 | ␉␊ |
799 | if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))␊ |
800 | ␉{␊ |
801 | if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))␊ |
802 | ␉␉{␊ |
803 | error("kernel compression is bad\n");␊ |
804 | return -1;␊ |
805 | }␊ |
806 | ␉␉␊ |
807 | ␉␉u_int32_t uncompressed_size, size;␊ |
808 | ␉␉␊ |
809 | uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);␊ |
810 | binary = malloc(uncompressed_size);␊ |
811 | ␉␉if (!binary) {␊ |
812 | ␉␉␉printf("Unable to allocate memory for uncompressed kernel\n");␊ |
813 | return -1;␊ |
814 | ␉␉}␊ |
815 | ␉␉␊ |
816 | size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],␊ |
817 | OSSwapBigToHostInt32(kernel_header->compressed_size));␊ |
818 | if (uncompressed_size != size)␊ |
819 | ␉␉{␊ |
820 | error("size mismatch from lzss: %x\n", size);␊ |
821 | return -1;␊ |
822 | }␊ |
823 | if (OSSwapBigToHostInt32(kernel_header->adler32) !=␊ |
824 | adler32(binary, uncompressed_size))␊ |
825 | ␉␉{␊ |
826 | printf("adler mismatch\n");␊ |
827 | return -1;␊ |
828 | }␊ |
829 | char* BootKernelCacheFile = (char*)(uint32_t)get_env(envkCache);␊ |
830 | ␉␉if (((get_env(envgBootMode) & kBootModeSafe) == 0) && (BootKernelCacheFile[0] != '\0') && gBootVolume->OSVersion[3] > '6') ␊ |
831 | ␉␉␉safe_set_env(envAdler32, kernel_header->adler32);␊ |
832 | }␊ |
833 | ␉␊ |
834 | ␉{␊ |
835 | ␉␉unsigned long len;␊ |
836 | ␉␉ret = ThinFatFile(&binary, &len);␊ |
837 | ␉␉if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)␊ |
838 | ␉␉{␊ |
839 | ␉␉␉archCpuType=CPU_TYPE_I386;␊ |
840 | ␉␉␉/*ret =*/ ThinFatFile(&binary, &len);␊ |
841 | ␉␉}␊ |
842 | ␉␉␊ |
843 | ␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
844 | ␉␉␊ |
845 | ␉␉if (ret<0 && archCpuType==CPU_TYPE_X86_64)␊ |
846 | ␉␉{␊ |
847 | ␉␉␉archCpuType=CPU_TYPE_I386;␊ |
848 | ␉␉␉ret = DecodeMachO(binary, rentry, raddr, rsize);␊ |
849 | ␉␉}␊ |
850 | ␉}␊ |
851 | ␉␊ |
852 | ␉return ret;␊ |
853 | } |