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