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