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