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