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