Chameleon

Chameleon Svn Source Tree

Root/branches/Bungo/i386/boot2/drivers.c

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#include "modules.h"
42
43#if DEBUG
44#define DBG(x...)printf(x)
45#else
46#define DBG(x...)msglog(x)
47#endif
48
49// extern char gMacOSVersion[8];
50
51struct Module {
52struct Module *nextModule;
53long willLoad;
54TagPtr dict;
55char *plistAddr;
56long plistLength;
57char *executablePath;
58char *bundlePath;
59long bundlePathLength;
60};
61typedef struct Module Module, *ModulePtr;
62
63struct DriverInfo {
64char *plistAddr;
65long plistLength;
66void *executableAddr;
67long executableLength;
68void *bundlePathAddr;
69long bundlePathLength;
70};
71typedef struct DriverInfo DriverInfo, *DriverInfoPtr;
72
73#define kDriverPackageSignature1 'MKXT'
74#define kDriverPackageSignature2 'MOSX'
75
76struct DriversPackage {
77 unsigned long signature1;
78 unsigned long signature2;
79 unsigned long length;
80 unsigned long adler32;
81 unsigned long version;
82 unsigned long numDrivers;
83 unsigned long reserved1;
84 unsigned long reserved2;
85};
86typedef struct DriversPackage DriversPackage;
87
88enum {
89kCFBundleType2,
90kCFBundleType3
91};
92
93long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);
94
95/*static*/ unsigned long Adler32( unsigned char * buffer, long length );
96
97long FileLoadDrivers(char *dirSpec, long plugin);
98long NetLoadDrivers(char *dirSpec);
99long LoadDriverMKext(char *fileSpec);
100long LoadDriverPList(char *dirSpec, char *name, long bundleType);
101long LoadMatchedModules(void);
102
103static long MatchPersonalities(void);
104static long MatchLibraries(void);
105#ifdef NOTDEF
106static ModulePtr FindModule(char *name);
107static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);
108#endif
109static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);
110static long InitDriverSupport(void);
111
112ModulePtr gModuleHead, gModuleTail;
113static TagPtr gPersonalityHead, gPersonalityTail;
114static char * gExtensionsSpec;
115static char * gDriverSpec;
116static char * gFileSpec;
117static char * gTempSpec;
118static char * gFileName;
119
120/*static*/ unsigned long
121Adler32( unsigned char * buffer, long length )
122{
123long cnt;
124unsigned long result, lowHalf, highHalf;
125
126lowHalf = 1;
127highHalf = 0;
128
129for (cnt = 0; cnt < length; cnt++)
130{
131if ((cnt % 5000) == 0)
132{
133lowHalf %= 65521L;
134highHalf %= 65521L;
135}
136
137lowHalf += buffer[cnt];
138highHalf += lowHalf;
139}
140
141lowHalf %= 65521L;
142highHalf %= 65521L;
143
144result = (highHalf << 16) | lowHalf;
145
146return result;
147}
148
149//==========================================================================
150// InitDriverSupport
151
152static long InitDriverSupport( void )
153{
154gExtensionsSpec = malloc( 4096 );
155gDriverSpec = malloc( 4096 );
156gFileSpec = malloc( 4096 );
157gTempSpec = malloc( 4096 );
158gFileName = malloc( 4096 );
159
160if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName ) {
161stop("InitDriverSupport error");
162}
163
164return 0;
165}
166
167//==========================================================================
168// LoadDrivers
169
170long LoadDrivers( char * dirSpec )
171{
172char dirSpecExtra[1024];
173
174if ( InitDriverSupport() != 0 ) {
175return 0;
176}
177
178// Load extra drivers if a hook has been installed.
179if (LoadExtraDrivers_p != NULL)
180{
181(*LoadExtraDrivers_p)(&FileLoadDrivers);
182}
183
184if ( gBootFileType == kNetworkDeviceType )
185{
186if (NetLoadDrivers(dirSpec) != 0)
187{
188error("LoadDrivers: Could not load drivers from the network\n");
189return -1;
190}
191}
192else if ( gBootFileType == kBlockDeviceType )
193{
194// First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).
195if (gRAMDiskVolume && !gRAMDiskBTAliased)
196{
197strcpy(dirSpecExtra, "rd(0,0)/Extra/");
198FileLoadDrivers(dirSpecExtra, 0);
199}
200
201// Next try to load Extra extensions from the selected root partition.
202strcpy(dirSpecExtra, "/Extra/");
203if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
204// If failed, then try to load Extra extensions from the boot partition
205// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.
206if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)
207|| (gRAMDiskVolume && gRAMDiskBTAliased) ) {
208// Next try a specfic OS version folder ie 10.5
209sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);
210if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
211// Next we'll try the base
212strcpy(dirSpecExtra, "bt(0,0)/Extra/");
213FileLoadDrivers(dirSpecExtra, 0);
214}
215}
216}
217if(!gHaveKernelCache) {
218// Don't load main driver (from /System/Library/Extentions) if gHaveKernelCache is set.
219// since these drivers will already be in the kernel cache.
220// NOTE: when gHaveKernelCache, xnu cannot (by default) load *any* extra kexts from the bootloader.
221// The /Extra code is not disabled in this case due to a kernel patch that allows for this to happen.
222
223// Also try to load Extensions from boot helper partitions.
224if (gBootVolume->flags & kBVFlagBooter) {
225strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
226if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
227strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
228if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
229strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
230FileLoadDrivers(dirSpecExtra, 0);
231}
232}
233}
234
235if (gMKextName[0] != '\0') {
236verbose("LoadDrivers: Loading from '%s'\n", gMKextName);
237if ( LoadDriverMKext(gMKextName) != 0 ) {
238error("Could not load %s\n", gMKextName);
239return -1;
240}
241} else {
242if (MacOSVerCurrent >= MacOSVer2Int("10.9")) { // issue 352
243strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */
244strcat(gExtensionsSpec, "Library/");
245FileLoadDrivers(gExtensionsSpec, 0);
246}
247strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */
248strcat(gExtensionsSpec, "System/Library/");
249FileLoadDrivers(gExtensionsSpec, 0);
250}
251
252}
253} else {
254return 0;
255}
256
257MatchPersonalities();
258
259MatchLibraries();
260
261LoadMatchedModules();
262
263return 0;
264}
265
266//==========================================================================
267// FileLoadMKext
268
269static long FileLoadMKext( const char * dirSpec, const char * extDirSpec )
270{
271longret, flags;
272u_int32_ttime, time2;
273charaltDirSpec[512];
274
275snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);
276ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);
277
278if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
279{
280ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);
281
282if ((ret != 0)
283|| ((flags & kFileTypeMask) != kFileTypeDirectory)
284|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))
285{
286snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);
287verbose("LoadDrivers: Loading from '%s'\n", gDriverSpec);
288
289if (LoadDriverMKext(gDriverSpec) == 0) {
290return 0;
291}
292}
293}
294return -1;
295}
296
297//==========================================================================
298// FileLoadDrivers
299
300long FileLoadDrivers( char * dirSpec, long plugin )
301{
302long longindex;
303longret, length, flags, bundleType;
304longresult = -1;
305u_int32_ttime;
306const char* name;
307
308if ( !plugin )
309{
310// First try 10.6's path for loading Extensions.mkext.
311if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {
312return 0;
313}
314
315// Next try the legacy path.
316else if (FileLoadMKext(dirSpec, "") == 0) {
317return 0;
318}
319
320strcat(dirSpec, "Extensions");
321}
322
323index = 0;
324while (1)
325{
326ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
327if (ret == -1) {
328break;
329}
330
331// Make sure this is a directory.
332if ((flags & kFileTypeMask) != kFileTypeDirectory) {
333continue;
334}
335
336// Make sure this is a kext.
337length = strlen(name);
338if (strcmp(name + length - 5, ".kext")) {
339continue;
340}
341
342// Save the file name.
343strlcpy(gFileName, name, 4096);
344
345// Determine the bundle type.
346snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);
347ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);
348if (ret == 0) {
349bundleType = kCFBundleType2;
350} else {
351bundleType = kCFBundleType3;
352}
353
354if (!plugin) {
355snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");
356}
357
358ret = LoadDriverPList(dirSpec, gFileName, bundleType);
359
360if (result != 0) {
361result = ret;
362}
363
364if (!plugin) {
365FileLoadDrivers(gDriverSpec, 1);
366}
367}
368
369return result;
370}
371
372
373//==========================================================================
374//
375
376long NetLoadDrivers( char * dirSpec )
377{
378long tries;
379
380#if NODEF
381long cnt;
382
383// Get the name of the kernel
384cnt = strlen(gBootFile);
385while (cnt--) {
386if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {
387cnt++;
388break;
389}
390}
391#endif
392
393// INTEL modification
394snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);
395
396verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);
397
398tries = 3;
399while (tries--)
400{
401if (LoadDriverMKext(gDriverSpec) == 0) {
402break;
403}
404}
405if (tries == -1) {
406return -1;
407}
408
409return 0;
410}
411
412//==========================================================================
413// loadDriverMKext
414
415long LoadDriverMKext( char * fileSpec )
416{
417unsigned long driversAddr, driversLength;
418long length;
419char segName[32];
420DriversPackage * package;
421
422#define GetPackageElement(e) OSSwapBigToHostInt32(package->e)
423
424// Load the MKext.
425length = LoadThinFatFile(fileSpec, (void **)&package);
426if (length < sizeof (DriversPackage)) {
427return -1;
428}
429
430// call hook to notify modules that the mkext has been loaded
431execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);
432
433
434// Verify the MKext.
435if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||
436( GetPackageElement(signature2) != kDriverPackageSignature2) ||
437( GetPackageElement(length) > kLoadSize ) ||
438( GetPackageElement(adler32) !=
439Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )
440{
441return -1;
442}
443
444// Make space for the MKext.
445driversLength = GetPackageElement(length);
446driversAddr = AllocateKernelMemory(driversLength);
447
448// Copy the MKext.
449memcpy((void *)driversAddr, (void *)package, driversLength);
450
451// Add the MKext to the memory map.
452snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);
453AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);
454
455return 0;
456}
457
458//==========================================================================
459// LoadDriverPList
460
461long
462LoadDriverPList( char * dirSpec, char * name, long bundleType )
463{
464long length, executablePathLength, bundlePathLength;
465ModulePtr module;
466TagPtr personalities;
467char * buffer = 0;
468char * tmpExecutablePath = 0;
469char * tmpBundlePath = 0;
470long ret = -1;
471
472do{
473// Save the driver path.
474
475if(name) {
476snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
477} else {
478snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
479}
480executablePathLength = strlen(gFileSpec) + 1;
481
482tmpExecutablePath = malloc(executablePathLength);
483if (tmpExecutablePath == 0) {
484break;
485}
486strcpy(tmpExecutablePath, gFileSpec);
487
488if(name) {
489snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);
490} else {
491snprintf(gFileSpec, 4096, "%s", dirSpec);
492}
493bundlePathLength = strlen(gFileSpec) + 1;
494
495tmpBundlePath = malloc(bundlePathLength);
496if (tmpBundlePath == 0) {
497break;
498}
499
500strcpy(tmpBundlePath, gFileSpec);
501
502// Construct the file spec to the plist, then load it.
503
504if(name) {
505snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");
506} else {
507snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");
508}
509
510length = LoadFile(gFileSpec);
511if (length == -1) {
512break;
513}
514length = length + 1;
515buffer = malloc(length);
516if (buffer == 0) {
517break;
518}
519strlcpy(buffer, (char *)kLoadAddr, length);
520
521// Parse the plist.
522
523ret = ParseXML(buffer, &module, &personalities);
524
525if (ret != 0) {
526break;
527}
528
529if (!module) // cparm
530{
531ret = -1;
532break;
533} // Should never happen but it will make the compiler happy
534
535// Allocate memory for the driver path and the plist.
536
537module->executablePath = tmpExecutablePath;
538module->bundlePath = tmpBundlePath;
539module->bundlePathLength = bundlePathLength;
540module->plistAddr = malloc(length);
541
542if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0)) {
543break;
544}
545
546// Save the driver path in the module.
547//strcpy(module->driverPath, tmpDriverPath);
548tmpExecutablePath = 0;
549tmpBundlePath = 0;
550
551// Add the plist to the module.
552
553strlcpy(module->plistAddr, (char *)kLoadAddr, length);
554module->plistLength = length;
555
556// Add the module to the end of the module list.
557
558if (gModuleHead == 0) {
559gModuleHead = module;
560} else {
561gModuleTail->nextModule = module;
562}
563gModuleTail = module;
564
565// Add the persionalities to the personality list.
566
567if (personalities) {
568personalities = personalities->tag;
569}
570while (personalities != 0)
571{
572if (gPersonalityHead == 0) {
573gPersonalityHead = personalities->tag;
574} else {
575gPersonalityTail->tagNext = personalities->tag;
576}
577
578gPersonalityTail = personalities->tag;
579personalities = personalities->tagNext;
580}
581
582ret = 0;
583}
584while (0);
585
586if ( buffer ) {
587free( buffer );
588}
589if ( tmpExecutablePath ) {
590free( tmpExecutablePath );
591}
592if ( tmpBundlePath ) {
593free( tmpBundlePath );
594}
595return ret;
596}
597
598
599//==========================================================================
600// LoadMatchedModules
601
602long
603LoadMatchedModules( void )
604{
605TagPtr prop;
606ModulePtr module;
607char *fileName, segName[32];
608DriverInfoPtr driver;
609long length, driverAddr, driverLength;
610void *executableAddr = 0;
611
612module = gModuleHead;
613
614while (module != 0)
615{
616if (module->willLoad)
617{
618prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);
619
620if (prop != 0)
621{
622fileName = prop->string;
623snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);
624length = LoadThinFatFile(gFileSpec, &executableAddr);
625if (length == 0)
626{
627length = LoadFile(gFileSpec);
628executableAddr = (void *)kLoadAddr;
629}
630//printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();
631}
632else
633length = 0;
634
635if (length != -1)
636{
637//driverModuleAddr = (void *)kLoadAddr;
638//if (length != 0)
639//{
640//ThinFatFile(&driverModuleAddr, &length);
641//}
642
643// Make make in the image area.
644
645execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);
646
647driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;
648driverAddr = AllocateKernelMemory(driverLength);
649
650// Set up the DriverInfo.
651driver = (DriverInfoPtr)driverAddr;
652driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));
653driver->plistLength = module->plistLength;
654if (length != 0)
655{
656driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +
657 module->plistLength);
658driver->executableLength = length;
659}
660else
661{
662driver->executableAddr = 0;
663driver->executableLength = 0;
664}
665driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +
666 module->plistLength + driver->executableLength);
667driver->bundlePathLength = module->bundlePathLength;
668
669// Save the plist, module and bundle.
670strcpy(driver->plistAddr, module->plistAddr);
671if (length != 0)
672{
673memcpy(driver->executableAddr, executableAddr, length);
674}
675strcpy(driver->bundlePathAddr, module->bundlePath);
676
677// Add an entry to the memory map.
678snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);
679AllocateMemoryRange(segName, driverAddr, driverLength,
680kBootDriverTypeKEXT);
681}
682}
683module = module->nextModule;
684}
685
686return 0;
687}
688
689//==========================================================================
690// MatchPersonalities
691
692static long
693MatchPersonalities( void )
694{
695/* IONameMatch support not implemented */
696return 0;
697}
698
699//==========================================================================
700// MatchLibraries
701
702static long
703MatchLibraries( void )
704{
705TagPtr prop, prop2;
706ModulePtr module, module2;
707long done;
708
709do {
710done = 1;
711module = gModuleHead;
712
713while (module != 0)
714{
715if (module->willLoad == 1)
716{
717prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
718
719if (prop != 0)
720{
721prop = prop->tag;
722
723while (prop != 0)
724{
725module2 = gModuleHead;
726
727while (module2 != 0)
728{
729prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
730
731if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))
732{
733if (module2->willLoad == 0)
734{
735module2->willLoad = 1;
736}
737break;
738}
739module2 = module2->nextModule;
740}
741prop = prop->tagNext;
742}
743}
744module->willLoad = 2;
745done = 0;
746}
747module = module->nextModule;
748}
749}
750while (!done);
751
752return 0;
753}
754
755
756//==========================================================================
757// FindModule
758
759#if NOTDEF
760static ModulePtr
761FindModule( char * name )
762{
763ModulePtr module;
764TagPtr prop;
765
766module = gModuleHead;
767
768while (module != 0)
769{
770prop = GetProperty(module->dict, kPropCFBundleIdentifier);
771
772if ((prop != 0) && !strcmp(name, prop->string)) {
773break;
774}
775
776module = module->nextModule;
777}
778
779return module;
780}
781#endif /* NOTDEF */
782
783//==========================================================================
784// ParseXML
785
786static long
787ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )
788{
789long length, pos;
790TagPtr moduleDict, required;
791ModulePtr tmpModule;
792
793pos = 0;
794
795while (1)
796{
797length = XMLParseNextTag(buffer + pos, &moduleDict);
798if (length == -1) {
799break;
800}
801
802pos += length;
803
804if (moduleDict == 0) {
805continue;
806}
807if (moduleDict->type == kTagTypeDict) {
808break;
809}
810XMLFreeTag(moduleDict);
811}
812
813if (length == -1) {
814return -1;
815}
816
817required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
818
819if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))
820{
821XMLFreeTag(moduleDict);
822return -2;
823}
824
825tmpModule = malloc(sizeof(Module));
826if (tmpModule == 0) {
827XMLFreeTag(moduleDict);
828return -1;
829}
830tmpModule->dict = moduleDict;
831
832// For now, load any module that has OSBundleRequired != "Safe Boot".
833
834tmpModule->willLoad = 1;
835
836*module = tmpModule;
837
838// Get the personalities.
839
840*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
841
842return 0;
843}
844
845#if NOTDEF
846static char gPlatformName[64];
847#endif
848
849long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
850{
851long ret = 0;
852compressed_kernel_header *kernel_header = (compressed_kernel_header *)binary;
853u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;
854//void *buffer = NULL;
855unsigned long len = 0;
856
857/*#if 0
858printf("kernel header:\n");
859printf("signature: 0x%x\n", kernel_header->signature);
860printf("compress_type: 0x%x\n", kernel_header->compress_type);
861printf("adler32: 0x%x\n", kernel_header->adler32);
862printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
863printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
864getchar();
865#endif*/
866
867if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))
868{
869verbose("Decompressing Kernel Cache");
870/*
871if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&
872 (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))
873{
874error("ERROR: kernel compression is bad!\n");
875return -1;
876}
877*/
878#if NOTDEF
879if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
880{
881return -1;
882}
883
884if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
885{
886return -1;
887}
888#endif
889uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
890binary /* = buffer */ = malloc(uncompressed_size);
891
892// MinusZwei
893size = 0;
894switch (kernel_header->compress_type)
895{
896case OSSwapBigToHostConstInt32('lzvn'):
897 verbose(" using 'lzvn': ");
898size = decompress_lzvn((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
899 if (uncompressed_size != size) {
900 error("ERROR! Size mismatch from 'lzvn' (found: %d, expected: %d).\n", size, uncompressed_size);
901 return -1;
902 }
903break;
904
905case OSSwapBigToHostConstInt32('lzss'):
906 verbose(" using 'lzss': ");
907size = decompress_lzss((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
908 if (uncompressed_size != size) {
909 error("ERROR! Size mismatch from 'lzss' (found: %d, expected: %d).\n", size, uncompressed_size);
910 return -1;
911 }
912break;
913
914default:
915 verbose(": ");
916 uint32_t compress_type = OSSwapBigToHostInt32(kernel_header->compress_type);
917 error("ERROR! Unknown kernel compression type: '%c%c%c%c'!\n", ((char *)&compress_type)[0], ((char *)&compress_type)[1], ((char *)&compress_type)[2], ((char *)&compress_type)[3]);
918 return -1;
919}
920// MinusZwei
921
922adler32 = Adler32(binary, uncompressed_size);
923if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
924{
925error("ERROR! Adler mismatch (found: %X, expected: %X).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
926return -1;
927}
928 verbose("OK.\n");
929}
930
931ret = ThinFatFile(&binary, &len);
932if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
933{
934archCpuType=CPU_TYPE_I386;
935ret = ThinFatFile(&binary, &len);
936}
937
938 // Bungo: scan binary for Darwin Kernel Version string
939 uint32_t offset = 0;
940 strncpy(gDarwinBuildVerStr, "Darwin Kernel Version", sizeof(gDarwinBuildVerStr));
941 while ((offset < 0xFFFFFFFF - (uint32_t)binary - 256) && memcmp(binary + offset, gDarwinBuildVerStr, 21)) {
942 offset++;
943 }
944 if (offset < 0xFFFFFFFF - (uint32_t)binary - 256)
945 strncpy(gDarwinBuildVerStr, (char *)(binary + offset), sizeof(gDarwinBuildVerStr));
946 else
947 strncpy(gDarwinBuildVerStr, "Unknown", sizeof(gDarwinBuildVerStr));
948
949// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
950execute_hook("DecodeKernel", (void *)binary, NULL, NULL, NULL);
951
952ret = DecodeMachO(binary, rentry, raddr, rsize);
953if (ret < 0 && archCpuType == CPU_TYPE_X86_64)
954{
955archCpuType = CPU_TYPE_I386;
956ret = DecodeMachO(binary, rentry, raddr, rsize);
957}
958
959return ret;
960}
961

Archive Download this file

Revision: 2531