Chameleon

Chameleon Svn Source Tree

Root/branches/chucko/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//==========================================================================
151// InitDriverSupport
152
153static long
154InitDriverSupport( void )
155{
156gExtensionsSpec = malloc( 4096 );
157gDriverSpec = malloc( 4096 );
158gFileSpec = malloc( 4096 );
159gTempSpec = malloc( 4096 );
160gFileName = malloc( 4096 );
161
162if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName ) {
163stop("InitDriverSupport error");
164}
165
166return 0;
167}
168
169//==========================================================================
170// LoadDrivers
171
172long LoadDrivers( char * dirSpec )
173{
174char dirSpecExtra[1024];
175
176if ( InitDriverSupport() != 0 ) {
177return 0;
178}
179
180// Load extra drivers if a hook has been installed.
181if (LoadExtraDrivers_p != NULL)
182{
183(*LoadExtraDrivers_p)(&FileLoadDrivers);
184}
185
186if ( gBootFileType == kNetworkDeviceType )
187{
188if (NetLoadDrivers(dirSpec) != 0)
189{
190error("Could not load drivers from the network\n");
191return -1;
192}
193}
194else if ( gBootFileType == kBlockDeviceType )
195{
196// First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).
197if (gRAMDiskVolume && !gRAMDiskBTAliased)
198{
199strcpy(dirSpecExtra, "rd(0,0)/Extra/");
200FileLoadDrivers(dirSpecExtra, 0);
201}
202
203// Next try to load Extra extensions from the selected root partition.
204strcpy(dirSpecExtra, "/Extra/");
205if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
206// If failed, then try to load Extra extensions from the boot partition
207// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.
208if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)
209|| (gRAMDiskVolume && gRAMDiskBTAliased) ) {
210// Next try a specfic OS version folder ie 10.5
211sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);
212if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
213// Next we'll try the base
214strcpy(dirSpecExtra, "bt(0,0)/Extra/");
215FileLoadDrivers(dirSpecExtra, 0);
216}
217}
218}
219if(!gHaveKernelCache) {
220// Don't load main driver (from /System/Library/Extentions) if gHaveKernelCache is set.
221// since these drivers will already be in the kernel cache.
222// NOTE: when gHaveKernelCache, xnu cannot (by default) load *any* extra kexts from the bootloader.
223// The /Extra code is not disabled in this case due to a kernel patch that allows for this to happen.
224
225// Also try to load Extensions from boot helper partitions.
226if (gBootVolume->flags & kBVFlagBooter) {
227strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
228if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
229strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
230if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
231strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
232FileLoadDrivers(dirSpecExtra, 0);
233}
234}
235}
236
237if (gMKextName[0] != '\0') {
238verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
239if ( LoadDriverMKext(gMKextName) != 0 ) {
240error("Could not load %s\n", gMKextName);
241return -1;
242}
243} else {
244if (gMacOSVersion[3] == '9') {
245strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */
246strcat(gExtensionsSpec, "Library/");
247FileLoadDrivers(gExtensionsSpec, 0);
248}
249strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */
250strcat(gExtensionsSpec, "System/Library/");
251FileLoadDrivers(gExtensionsSpec, 0);
252}
253
254}
255} else {
256return 0;
257}
258
259MatchPersonalities();
260
261MatchLibraries();
262
263LoadMatchedModules();
264
265return 0;
266}
267
268//==========================================================================
269// FileLoadMKext
270
271static long
272FileLoadMKext( const char * dirSpec, const char * extDirSpec )
273{
274 long ret, flags;
275 u_int32_t time, time2;
276charaltDirSpec[512];
277
278snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);
279ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);
280
281if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
282{
283ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);
284
285if ((ret != 0)
286|| ((flags & kFileTypeMask) != kFileTypeDirectory)
287|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))
288{
289snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);
290verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);
291
292if (LoadDriverMKext(gDriverSpec) == 0) {
293return 0;
294}
295}
296}
297return -1;
298}
299
300//==========================================================================
301// FileLoadDrivers
302
303long
304FileLoadDrivers( char * dirSpec, long plugin )
305{
306long long index;
307long ret, length, flags, bundleType;
308long result = -1;
309 u_int32_t time;
310const char * name;
311
312if ( !plugin )
313{
314// First try 10.6's path for loading Extensions.mkext.
315if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {
316return 0;
317}
318
319// Next try the legacy path.
320else if (FileLoadMKext(dirSpec, "") == 0) {
321return 0;
322}
323
324strcat(dirSpec, "Extensions");
325}
326
327index = 0;
328while (1)
329{
330ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
331if (ret == -1) {
332break;
333}
334
335// Make sure this is a directory.
336if ((flags & kFileTypeMask) != kFileTypeDirectory) {
337continue;
338}
339
340// Make sure this is a kext.
341length = strlen(name);
342if (strcmp(name + length - 5, ".kext")) {
343continue;
344}
345
346// Save the file name.
347strlcpy(gFileName, name, 4096);
348
349// Determine the bundle type.
350snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);
351ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);
352if (ret == 0) {
353bundleType = kCFBundleType2;
354} else {
355bundleType = kCFBundleType3;
356}
357
358if (!plugin) {
359snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");
360}
361
362ret = LoadDriverPList(dirSpec, gFileName, bundleType);
363
364if (result != 0) {
365result = ret;
366}
367
368if (!plugin) {
369FileLoadDrivers(gDriverSpec, 1);
370}
371}
372
373return result;
374}
375
376
377//==========================================================================
378//
379
380long
381NetLoadDrivers( char * dirSpec )
382{
383long tries;
384
385#if NODEF
386long cnt;
387
388// Get the name of the kernel
389cnt = strlen(gBootFile);
390while (cnt--) {
391if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {
392cnt++;
393break;
394}
395}
396#endif
397
398// INTEL modification
399snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);
400
401verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);
402
403tries = 3;
404while (tries--)
405{
406if (LoadDriverMKext(gDriverSpec) == 0) {
407break;
408}
409}
410if (tries == -1) {
411return -1;
412}
413
414return 0;
415}
416
417//==========================================================================
418// loadDriverMKext
419
420long
421LoadDriverMKext( char * fileSpec )
422{
423unsigned long driversAddr, driversLength;
424long length;
425char segName[32];
426DriversPackage * package;
427
428#define GetPackageElement(e) OSSwapBigToHostInt32(package->e)
429
430// Load the MKext.
431length = LoadThinFatFile(fileSpec, (void **)&package);
432if (length < sizeof (DriversPackage)) {
433return -1;
434}
435
436// call hook to notify modules that the mkext has been loaded
437execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);
438
439
440// Verify the MKext.
441if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||
442( GetPackageElement(signature2) != kDriverPackageSignature2) ||
443( GetPackageElement(length) > kLoadSize ) ||
444( GetPackageElement(adler32) !=
445Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )
446{
447return -1;
448}
449
450// Make space for the MKext.
451driversLength = GetPackageElement(length);
452driversAddr = AllocateKernelMemory(driversLength);
453
454// Copy the MKext.
455memcpy((void *)driversAddr, (void *)package, driversLength);
456
457// Add the MKext to the memory map.
458snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);
459AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);
460
461return 0;
462}
463
464//==========================================================================
465// LoadDriverPList
466
467long
468LoadDriverPList( char * dirSpec, char * name, long bundleType )
469{
470long length, executablePathLength, bundlePathLength;
471ModulePtr module;
472TagPtr personalities;
473char * buffer = 0;
474char * tmpExecutablePath = 0;
475char * tmpBundlePath = 0;
476long ret = -1;
477
478do{
479// Save the driver path.
480
481if(name) {
482snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
483} else {
484snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
485}
486executablePathLength = strlen(gFileSpec) + 1;
487
488tmpExecutablePath = malloc(executablePathLength);
489if (tmpExecutablePath == 0) {
490break;
491}
492strcpy(tmpExecutablePath, gFileSpec);
493
494if(name) {
495snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);
496} else {
497snprintf(gFileSpec, 4096, "%s", dirSpec);
498}
499bundlePathLength = strlen(gFileSpec) + 1;
500
501tmpBundlePath = malloc(bundlePathLength);
502if (tmpBundlePath == 0) {
503break;
504}
505
506strcpy(tmpBundlePath, gFileSpec);
507
508// Construct the file spec to the plist, then load it.
509
510if(name) {
511snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");
512} else {
513snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");
514}
515
516length = LoadFile(gFileSpec);
517if (length == -1) {
518break;
519}
520length = length + 1;
521buffer = malloc(length);
522if (buffer == 0) {
523break;
524}
525strlcpy(buffer, (char *)kLoadAddr, length);
526
527// Parse the plist.
528
529ret = ParseXML(buffer, &module, &personalities);
530
531if (ret != 0) {
532break;
533}
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
849char *gDarwinBuildVerStr = "Darwin Kernel Version"; // Bungo
850
851long
852DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
853{
854long ret = 0;
855compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;
856u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;
857void *buffer = NULL;
858unsigned long len = 0;
859
860/*#if 0
861printf("kernel header:\n");
862printf("signature: 0x%x\n", kernel_header->signature);
863printf("compress_type: 0x%x\n", kernel_header->compress_type);
864printf("adler32: 0x%x\n", kernel_header->adler32);
865printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
866printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
867getchar();
868#endif*/
869
870if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))
871{
872DBG("Decompressing Kernel: ");
873
874if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&
875(kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))
876{
877error("ERROR: kernel compression is bad!\n");
878return -1;
879}
880#if NOTDEF
881if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
882{
883return -1;
884}
885if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
886{
887return -1;
888}
889#endif
890uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
891binary = buffer = malloc(uncompressed_size);
892
893// MinusZwei
894size = 0;
895switch (kernel_header->compress_type)
896{
897case OSSwapBigToHostConstInt32('lzvn'):
898size = decompress_lzvn( binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
899break;
900
901case OSSwapBigToHostConstInt32('lzss'):
902size = decompress_lzss( (u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
903break;
904
905default:
906break;
907}
908// MinusZwei
909
910if (uncompressed_size != size) {
911error("ERROR: size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);
912return -1;
913}
914
915adler32 = Adler32(binary, uncompressed_size);
916if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
917{
918error("ERROR: adler mismatch (found: %x, expected: %x).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
919return -1;
920}
921
922DBG("OK.\n");
923}
924
925ret = ThinFatFile(&binary, &len);
926if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
927{
928archCpuType=CPU_TYPE_I386;
929ret = ThinFatFile(&binary, &len);
930}
931
932// Bungo: no range checking, sorry
933size = 0;
934while (memcmp((uint8_t *)binary + size, (uint8_t *)gDarwinBuildVerStr, 21)) {
935size++;
936}
937gDarwinBuildVerStr = (char *)binary + size;
938
939// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
940execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);
941
942ret = DecodeMachO(binary, rentry, raddr, rsize);
943if (ret < 0 && archCpuType == CPU_TYPE_X86_64)
944{
945archCpuType = CPU_TYPE_I386;
946ret = DecodeMachO(binary, rentry, raddr, rsize);
947}
948
949return ret;
950}
951

Archive Download this file

Revision: 2453