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//==========================================================================
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// Allocate memory for the driver path and the plist.
533
534module->executablePath = tmpExecutablePath;
535module->bundlePath = tmpBundlePath;
536module->bundlePathLength = bundlePathLength;
537module->plistAddr = malloc(length);
538
539if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0)) {
540break;
541}
542
543// Save the driver path in the module.
544//strcpy(module->driverPath, tmpDriverPath);
545tmpExecutablePath = 0;
546tmpBundlePath = 0;
547
548// Add the plist to the module.
549
550strlcpy(module->plistAddr, (char *)kLoadAddr, length);
551module->plistLength = length;
552
553// Add the module to the end of the module list.
554
555if (gModuleHead == 0) {
556gModuleHead = module;
557} else {
558gModuleTail->nextModule = module;
559}
560gModuleTail = module;
561
562// Add the persionalities to the personality list.
563
564if (personalities) {
565personalities = personalities->tag;
566}
567while (personalities != 0)
568{
569if (gPersonalityHead == 0) {
570gPersonalityHead = personalities->tag;
571} else {
572gPersonalityTail->tagNext = personalities->tag;
573}
574
575gPersonalityTail = personalities->tag;
576personalities = personalities->tagNext;
577}
578
579ret = 0;
580}
581while (0);
582
583if ( buffer ) {
584free( buffer );
585}
586if ( tmpExecutablePath ) {
587free( tmpExecutablePath );
588}
589if ( tmpBundlePath ) {
590free( tmpBundlePath );
591}
592return ret;
593}
594
595
596//==========================================================================
597// LoadMatchedModules
598
599long
600LoadMatchedModules( void )
601{
602TagPtr prop;
603ModulePtr module;
604char *fileName, segName[32];
605DriverInfoPtr driver;
606long length, driverAddr, driverLength;
607void *executableAddr = 0;
608
609module = gModuleHead;
610
611while (module != 0)
612{
613if (module->willLoad)
614{
615prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);
616
617if (prop != 0)
618{
619fileName = prop->string;
620snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);
621length = LoadThinFatFile(gFileSpec, &executableAddr);
622if (length == 0)
623{
624length = LoadFile(gFileSpec);
625executableAddr = (void *)kLoadAddr;
626}
627//printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();
628}
629else
630length = 0;
631
632if (length != -1)
633{
634//driverModuleAddr = (void *)kLoadAddr;
635//if (length != 0)
636//{
637//ThinFatFile(&driverModuleAddr, &length);
638//}
639
640// Make make in the image area.
641
642execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);
643
644driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;
645driverAddr = AllocateKernelMemory(driverLength);
646
647// Set up the DriverInfo.
648driver = (DriverInfoPtr)driverAddr;
649driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));
650driver->plistLength = module->plistLength;
651if (length != 0)
652{
653driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +
654 module->plistLength);
655driver->executableLength = length;
656}
657else
658{
659driver->executableAddr = 0;
660driver->executableLength = 0;
661}
662driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +
663 module->plistLength + driver->executableLength);
664driver->bundlePathLength = module->bundlePathLength;
665
666// Save the plist, module and bundle.
667strcpy(driver->plistAddr, module->plistAddr);
668if (length != 0)
669{
670memcpy(driver->executableAddr, executableAddr, length);
671}
672strcpy(driver->bundlePathAddr, module->bundlePath);
673
674// Add an entry to the memory map.
675snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);
676AllocateMemoryRange(segName, driverAddr, driverLength,
677kBootDriverTypeKEXT);
678}
679}
680module = module->nextModule;
681}
682
683return 0;
684}
685
686//==========================================================================
687// MatchPersonalities
688
689static long
690MatchPersonalities( void )
691{
692/* IONameMatch support not implemented */
693return 0;
694}
695
696//==========================================================================
697// MatchLibraries
698
699static long
700MatchLibraries( void )
701{
702TagPtr prop, prop2;
703ModulePtr module, module2;
704long done;
705
706do {
707done = 1;
708module = gModuleHead;
709
710while (module != 0)
711{
712if (module->willLoad == 1)
713{
714prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
715
716if (prop != 0)
717{
718prop = prop->tag;
719
720while (prop != 0)
721{
722module2 = gModuleHead;
723
724while (module2 != 0)
725{
726prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
727
728if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))
729{
730if (module2->willLoad == 0)
731{
732module2->willLoad = 1;
733}
734break;
735}
736module2 = module2->nextModule;
737}
738prop = prop->tagNext;
739}
740}
741module->willLoad = 2;
742done = 0;
743}
744module = module->nextModule;
745}
746}
747while (!done);
748
749return 0;
750}
751
752
753//==========================================================================
754// FindModule
755
756#if NOTDEF
757static ModulePtr
758FindModule( char * name )
759{
760ModulePtr module;
761TagPtr prop;
762
763module = gModuleHead;
764
765while (module != 0)
766{
767prop = GetProperty(module->dict, kPropCFBundleIdentifier);
768
769if ((prop != 0) && !strcmp(name, prop->string)) {
770break;
771}
772
773module = module->nextModule;
774}
775
776return module;
777}
778#endif /* NOTDEF */
779
780//==========================================================================
781// ParseXML
782
783static long
784ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )
785{
786long length, pos;
787TagPtr moduleDict, required;
788ModulePtr tmpModule;
789
790pos = 0;
791
792while (1)
793{
794length = XMLParseNextTag(buffer + pos, &moduleDict);
795if (length == -1) {
796break;
797}
798
799pos += length;
800
801if (moduleDict == 0) {
802continue;
803}
804if (moduleDict->type == kTagTypeDict) {
805break;
806}
807XMLFreeTag(moduleDict);
808}
809
810if (length == -1) {
811return -1;
812}
813
814required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
815
816if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))
817{
818XMLFreeTag(moduleDict);
819return -2;
820}
821
822tmpModule = malloc(sizeof(Module));
823if (tmpModule == 0) {
824XMLFreeTag(moduleDict);
825return -1;
826}
827tmpModule->dict = moduleDict;
828
829// For now, load any module that has OSBundleRequired != "Safe Boot".
830
831tmpModule->willLoad = 1;
832
833*module = tmpModule;
834
835// Get the personalities.
836
837*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
838
839return 0;
840}
841
842#if NOTDEF
843static char gPlatformName[64];
844#endif
845
846char *gDarwinBuildVerStr = "Darwin Kernel Version"; // Bungo
847
848long
849DecodeKernel(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;
854void *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{
869 DBG("Decompressing Kernel: ");
870
871if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss'))
872{
873error("ERROR: kernel compression is bad!\n");
874return -1;
875}
876#if NOTDEF
877if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
878{
879return -1;
880}
881if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
882{
883return -1;
884}
885#endif
886uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
887binary = buffer = malloc(uncompressed_size);
888
889size = decompress_lzss((u_int8_t *)binary, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
890if (uncompressed_size != size) {
891error("ERROR: size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);
892return -1;
893}
894
895 adler32 = Adler32(binary, uncompressed_size);
896if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
897{
898error("ERROR: adler mismatch (found: %x, expected: %x).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
899return -1;
900}
901
902 DBG("OK.\n");
903}
904
905ret = ThinFatFile(&binary, &len);
906if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
907{
908archCpuType=CPU_TYPE_I386;
909ret = ThinFatFile(&binary, &len);
910}
911
912 // Bungo: no range checking, sorry
913 size = 0;
914 while (memcmp((uint8_t *)binary + size, (uint8_t *)gDarwinBuildVerStr, 21)) {
915 size++;
916 }
917 gDarwinBuildVerStr = (char *)binary + size;
918
919// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
920execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);
921
922ret = DecodeMachO(binary, rentry, raddr, rsize);
923if (ret<0 && archCpuType==CPU_TYPE_X86_64)
924{
925archCpuType=CPU_TYPE_I386;
926ret = DecodeMachO(binary, rentry, raddr, rsize);
927}
928
929return ret;
930}
931

Archive Download this file

Revision: 2379