Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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{
274longret, flags, time, time2;
275charaltDirSpec[512];
276
277snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);
278ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);
279
280if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
281{
282ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);
283
284if ((ret != 0)
285|| ((flags & kFileTypeMask) != kFileTypeDirectory)
286|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))
287{
288snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);
289verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);
290
291if (LoadDriverMKext(gDriverSpec) == 0) {
292return 0;
293}
294}
295}
296return -1;
297}
298
299//==========================================================================
300// FileLoadDrivers
301
302long
303FileLoadDrivers( char * dirSpec, long plugin )
304{
305long ret, length, flags, time, bundleType;
306long long index;
307long result = -1;
308const char * name;
309
310if ( !plugin )
311{
312// First try 10.6's path for loading Extensions.mkext.
313if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {
314return 0;
315}
316
317// Next try the legacy path.
318else if (FileLoadMKext(dirSpec, "") == 0) {
319return 0;
320}
321
322strcat(dirSpec, "Extensions");
323}
324
325index = 0;
326while (1)
327{
328ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
329if (ret == -1) {
330break;
331}
332
333// Make sure this is a directory.
334if ((flags & kFileTypeMask) != kFileTypeDirectory) {
335continue;
336}
337
338// Make sure this is a kext.
339length = strlen(name);
340if (strcmp(name + length - 5, ".kext")) {
341continue;
342}
343
344// Save the file name.
345strlcpy(gFileName, name, 4096);
346
347// Determine the bundle type.
348snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);
349ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);
350if (ret == 0) {
351bundleType = kCFBundleType2;
352} else {
353bundleType = kCFBundleType3;
354}
355
356if (!plugin) {
357snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");
358}
359
360ret = LoadDriverPList(dirSpec, gFileName, bundleType);
361
362if (result != 0) {
363result = ret;
364}
365
366if (!plugin) {
367FileLoadDrivers(gDriverSpec, 1);
368}
369}
370
371return result;
372}
373
374
375//==========================================================================
376//
377
378long
379NetLoadDrivers( char * dirSpec )
380{
381long tries;
382
383#if NODEF
384long cnt;
385
386// Get the name of the kernel
387cnt = strlen(gBootFile);
388while (cnt--) {
389if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {
390cnt++;
391break;
392}
393}
394#endif
395
396// INTEL modification
397snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);
398
399verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);
400
401tries = 3;
402while (tries--)
403{
404if (LoadDriverMKext(gDriverSpec) == 0) {
405break;
406}
407}
408if (tries == -1) {
409return -1;
410}
411
412return 0;
413}
414
415//==========================================================================
416// loadDriverMKext
417
418long
419LoadDriverMKext( char * fileSpec )
420{
421unsigned long driversAddr, driversLength;
422long length;
423char segName[32];
424DriversPackage * package;
425
426#define GetPackageElement(e) OSSwapBigToHostInt32(package->e)
427
428// Load the MKext.
429length = LoadThinFatFile(fileSpec, (void **)&package);
430if (length < sizeof (DriversPackage)) {
431return -1;
432}
433
434// call hook to notify modules that the mkext has been loaded
435execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);
436
437
438// Verify the MKext.
439if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||
440( GetPackageElement(signature2) != kDriverPackageSignature2) ||
441( GetPackageElement(length) > kLoadSize ) ||
442( GetPackageElement(adler32) !=
443Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )
444{
445return -1;
446}
447
448// Make space for the MKext.
449driversLength = GetPackageElement(length);
450driversAddr = AllocateKernelMemory(driversLength);
451
452// Copy the MKext.
453memcpy((void *)driversAddr, (void *)package, driversLength);
454
455// Add the MKext to the memory map.
456snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);
457AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);
458
459return 0;
460}
461
462//==========================================================================
463// LoadDriverPList
464
465long
466LoadDriverPList( char * dirSpec, char * name, long bundleType )
467{
468long length, executablePathLength, bundlePathLength;
469ModulePtr module;
470TagPtr personalities;
471char * buffer = 0;
472char * tmpExecutablePath = 0;
473char * tmpBundlePath = 0;
474long ret = -1;
475
476do{
477// Save the driver path.
478
479if(name) {
480snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
481} else {
482snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
483}
484executablePathLength = strlen(gFileSpec) + 1;
485
486tmpExecutablePath = malloc(executablePathLength);
487if (tmpExecutablePath == 0) {
488break;
489}
490strcpy(tmpExecutablePath, gFileSpec);
491
492if(name) {
493snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);
494} else {
495snprintf(gFileSpec, 4096, "%s", dirSpec);
496}
497bundlePathLength = strlen(gFileSpec) + 1;
498
499tmpBundlePath = malloc(bundlePathLength);
500if (tmpBundlePath == 0) {
501break;
502}
503
504strcpy(tmpBundlePath, gFileSpec);
505
506// Construct the file spec to the plist, then load it.
507
508if(name) {
509snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");
510} else {
511snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");
512}
513
514length = LoadFile(gFileSpec);
515if (length == -1) {
516break;
517}
518length = length + 1;
519buffer = malloc(length);
520if (buffer == 0) {
521break;
522}
523strlcpy(buffer, (char *)kLoadAddr, length);
524
525// Parse the plist.
526
527ret = ParseXML(buffer, &module, &personalities);
528
529if (ret != 0) {
530break;
531}
532
533if (!module)
534{
535ret = -1;
536break;
537} // Should never happen but it will make the compiler happy
538
539// Allocate memory for the driver path and the plist.
540
541module->executablePath = tmpExecutablePath;
542module->bundlePath = tmpBundlePath;
543module->bundlePathLength = bundlePathLength;
544module->plistAddr = malloc(length);
545
546if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0)) {
547break;
548}
549
550// Save the driver path in the module.
551//strcpy(module->driverPath, tmpDriverPath);
552tmpExecutablePath = 0;
553tmpBundlePath = 0;
554
555// Add the plist to the module.
556
557strlcpy(module->plistAddr, (char *)kLoadAddr, length);
558module->plistLength = length;
559
560// Add the module to the end of the module list.
561
562if (gModuleHead == 0) {
563gModuleHead = module;
564} else {
565gModuleTail->nextModule = module;
566}
567gModuleTail = module;
568
569// Add the persionalities to the personality list.
570
571if (personalities) {
572personalities = personalities->tag;
573}
574while (personalities != 0)
575{
576if (gPersonalityHead == 0) {
577gPersonalityHead = personalities->tag;
578} else {
579gPersonalityTail->tagNext = personalities->tag;
580}
581
582gPersonalityTail = personalities->tag;
583personalities = personalities->tagNext;
584}
585
586ret = 0;
587}
588while (0);
589
590if ( buffer ) {
591free( buffer );
592}
593if ( tmpExecutablePath ) {
594free( tmpExecutablePath );
595}
596if ( tmpBundlePath ) {
597free( tmpBundlePath );
598}
599return ret;
600}
601
602
603//==========================================================================
604// LoadMatchedModules
605
606long
607LoadMatchedModules( void )
608{
609TagPtr prop;
610ModulePtr module;
611char *fileName, segName[32];
612DriverInfoPtr driver;
613long length, driverAddr, driverLength;
614void *executableAddr = 0;
615
616module = gModuleHead;
617
618while (module != 0)
619{
620if (module->willLoad)
621{
622prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);
623
624if (prop != 0)
625{
626fileName = prop->string;
627snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);
628length = LoadThinFatFile(gFileSpec, &executableAddr);
629if (length == 0)
630{
631length = LoadFile(gFileSpec);
632executableAddr = (void *)kLoadAddr;
633}
634//printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();
635}
636else
637length = 0;
638
639if (length != -1)
640{
641//driverModuleAddr = (void *)kLoadAddr;
642//if (length != 0)
643//{
644//ThinFatFile(&driverModuleAddr, &length);
645//}
646
647// Make make in the image area.
648
649execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);
650
651driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;
652driverAddr = AllocateKernelMemory(driverLength);
653
654// Set up the DriverInfo.
655driver = (DriverInfoPtr)driverAddr;
656driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));
657driver->plistLength = module->plistLength;
658if (length != 0)
659{
660driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +
661 module->plistLength);
662driver->executableLength = length;
663}
664else
665{
666driver->executableAddr = 0;
667driver->executableLength = 0;
668}
669driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +
670 module->plistLength + driver->executableLength);
671driver->bundlePathLength = module->bundlePathLength;
672
673// Save the plist, module and bundle.
674strcpy(driver->plistAddr, module->plistAddr);
675if (length != 0)
676{
677memcpy(driver->executableAddr, executableAddr, length);
678}
679strcpy(driver->bundlePathAddr, module->bundlePath);
680
681// Add an entry to the memory map.
682snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);
683AllocateMemoryRange(segName, driverAddr, driverLength,
684kBootDriverTypeKEXT);
685}
686}
687module = module->nextModule;
688}
689
690return 0;
691}
692
693//==========================================================================
694// MatchPersonalities
695
696static long
697MatchPersonalities( void )
698{
699/* IONameMatch support not implemented */
700return 0;
701}
702
703//==========================================================================
704// MatchLibraries
705
706static long
707MatchLibraries( void )
708{
709TagPtr prop, prop2;
710ModulePtr module, module2;
711long done;
712
713do {
714done = 1;
715module = gModuleHead;
716
717while (module != 0)
718{
719if (module->willLoad == 1)
720{
721prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
722
723if (prop != 0)
724{
725prop = prop->tag;
726
727while (prop != 0)
728{
729module2 = gModuleHead;
730
731while (module2 != 0)
732{
733prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
734
735if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))
736{
737if (module2->willLoad == 0)
738{
739module2->willLoad = 1;
740}
741break;
742}
743module2 = module2->nextModule;
744}
745prop = prop->tagNext;
746}
747}
748module->willLoad = 2;
749done = 0;
750}
751module = module->nextModule;
752}
753}
754while (!done);
755
756return 0;
757}
758
759
760//==========================================================================
761// FindModule
762
763#if NOTDEF
764static ModulePtr
765FindModule( char * name )
766{
767ModulePtr module;
768TagPtr prop;
769
770module = gModuleHead;
771
772while (module != 0)
773{
774prop = GetProperty(module->dict, kPropCFBundleIdentifier);
775
776if ((prop != 0) && !strcmp(name, prop->string)) {
777break;
778}
779
780module = module->nextModule;
781}
782
783return module;
784}
785#endif /* NOTDEF */
786
787//==========================================================================
788// ParseXML
789
790static long
791ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )
792{
793long length, pos;
794TagPtr moduleDict, required;
795ModulePtr tmpModule;
796
797pos = 0;
798
799while (1)
800{
801length = XMLParseNextTag(buffer + pos, &moduleDict);
802if (length == -1) {
803break;
804}
805
806pos += length;
807
808if (moduleDict == 0) {
809continue;
810}
811if (moduleDict->type == kTagTypeDict) {
812break;
813}
814XMLFreeTag(moduleDict);
815}
816
817if (length == -1) {
818return -1;
819}
820
821required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
822
823if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))
824{
825XMLFreeTag(moduleDict);
826return -2;
827}
828
829tmpModule = malloc(sizeof(Module));
830if (tmpModule == 0) {
831XMLFreeTag(moduleDict);
832return -1;
833}
834tmpModule->dict = moduleDict;
835
836// For now, load any module that has OSBundleRequired != "Safe Boot".
837
838tmpModule->willLoad = 1;
839
840*module = tmpModule;
841
842// Get the personalities.
843
844*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
845
846return 0;
847}
848
849#if NOTDEF
850static char gPlatformName[64];
851#endif
852
853char *gDarwinBuildVerStr = "Darwin Kernel Version"; // Bungo
854
855long
856DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
857{
858long ret = 0;
859compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;
860u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;
861void *buffer = NULL;
862unsigned long len = 0;
863
864/*#if 0
865printf("kernel header:\n");
866printf("signature: 0x%x\n", kernel_header->signature);
867printf("compress_type: 0x%x\n", kernel_header->compress_type);
868printf("adler32: 0x%x\n", kernel_header->adler32);
869printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
870printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
871getchar();
872#endif*/
873
874if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))
875{
876DBG("Decompressing Kernel: ");
877
878if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))
879{
880error("ERROR: kernel compression is bad!\n");
881return -1;
882}
883#if NOTDEF
884if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
885{
886return -1;
887}
888if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
889{
890return -1;
891}
892#endif
893uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
894binary = buffer = malloc(uncompressed_size);
895
896size = decompress_lzss((u_int8_t *)binary, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
897if (uncompressed_size != size) {
898error("ERROR: size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);
899return -1;
900}
901
902adler32 = Adler32(binary, uncompressed_size);
903if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
904{
905error("ERROR: adler mismatch (found: %x, expected: %x).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
906return -1;
907}
908
909DBG("OK.\n");
910}
911
912ret = ThinFatFile(&binary, &len);
913if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
914{
915archCpuType=CPU_TYPE_I386;
916ret = ThinFatFile(&binary, &len);
917}
918
919// Bungo: no range checking, sorry
920size = 0;
921while (memcmp((uint8_t *)binary + size, (uint8_t *)gDarwinBuildVerStr, 21)) {
922size++;
923}
924gDarwinBuildVerStr = (char *)binary + size;
925
926// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
927execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);
928
929ret = DecodeMachO(binary, rentry, raddr, rsize);
930if (ret<0 && archCpuType==CPU_TYPE_X86_64)
931{
932archCpuType=CPU_TYPE_I386;
933ret = DecodeMachO(binary, rentry, raddr, rsize);
934}
935
936return ret;
937}
938

Archive Download this file

Revision: 2385