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//extern char gMacOSVersion[8];
44
45struct Module {
46struct Module *nextModule;
47long willLoad;
48TagPtr dict;
49char *plistAddr;
50long plistLength;
51char *executablePath;
52char *bundlePath;
53long bundlePathLength;
54};
55typedef struct Module Module, *ModulePtr;
56
57struct DriverInfo {
58char *plistAddr;
59long plistLength;
60void *executableAddr;
61long executableLength;
62void *bundlePathAddr;
63long bundlePathLength;
64};
65typedef struct DriverInfo DriverInfo, *DriverInfoPtr;
66
67#define kDriverPackageSignature1 'MKXT'
68#define kDriverPackageSignature2 'MOSX'
69
70struct DriversPackage {
71 unsigned long signature1;
72 unsigned long signature2;
73 unsigned long length;
74 unsigned long adler32;
75 unsigned long version;
76 unsigned long numDrivers;
77 unsigned long reserved1;
78 unsigned long reserved2;
79};
80typedef struct DriversPackage DriversPackage;
81
82enum {
83kCFBundleType2,
84kCFBundleType3
85};
86
87long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);
88
89/*static*/ unsigned long Adler32( unsigned char * buffer, long length );
90
91long FileLoadDrivers(char *dirSpec, long plugin);
92long NetLoadDrivers(char *dirSpec);
93long LoadDriverMKext(char *fileSpec);
94long LoadDriverPList(char *dirSpec, char *name, long bundleType);
95long LoadMatchedModules(void);
96
97static long MatchPersonalities(void);
98static long MatchLibraries(void);
99#ifdef NOTDEF
100static ModulePtr FindModule(char *name);
101static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);
102#endif
103static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);
104static long InitDriverSupport(void);
105
106ModulePtr gModuleHead, gModuleTail;
107static TagPtr gPersonalityHead, gPersonalityTail;
108static char * gExtensionsSpec;
109static char * gDriverSpec;
110static char * gFileSpec;
111static char * gTempSpec;
112static char * gFileName;
113
114/*static*/ unsigned long
115Adler32( unsigned char * buffer, long length )
116{
117long cnt;
118unsigned long result, lowHalf, highHalf;
119
120lowHalf = 1;
121highHalf = 0;
122
123for (cnt = 0; cnt < length; cnt++)
124{
125if ((cnt % 5000) == 0)
126{
127lowHalf %= 65521L;
128highHalf %= 65521L;
129}
130
131lowHalf += buffer[cnt];
132highHalf += lowHalf;
133}
134
135lowHalf %= 65521L;
136highHalf %= 65521L;
137
138result = (highHalf << 16) | lowHalf;
139
140return result;
141}
142
143
144//==========================================================================
145// InitDriverSupport
146
147static long
148InitDriverSupport( void )
149{
150gExtensionsSpec = malloc( 4096 );
151gDriverSpec = malloc( 4096 );
152gFileSpec = malloc( 4096 );
153gTempSpec = malloc( 4096 );
154gFileName = malloc( 4096 );
155
156if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName ) {
157stop("InitDriverSupport error");
158}
159
160return 0;
161}
162
163//==========================================================================
164// LoadDrivers
165
166long LoadDrivers( char * dirSpec )
167{
168char dirSpecExtra[1024];
169
170if ( InitDriverSupport() != 0 ) {
171return 0;
172}
173
174// Load extra drivers if a hook has been installed.
175if (LoadExtraDrivers_p != NULL)
176{
177(*LoadExtraDrivers_p)(&FileLoadDrivers);
178}
179
180if ( gBootFileType == kNetworkDeviceType )
181{
182if (NetLoadDrivers(dirSpec) != 0)
183{
184error("Could not load drivers from the network\n");
185return -1;
186}
187}
188else if ( gBootFileType == kBlockDeviceType )
189{
190// First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).
191if (gRAMDiskVolume && !gRAMDiskBTAliased)
192{
193strcpy(dirSpecExtra, "rd(0,0)/Extra/");
194FileLoadDrivers(dirSpecExtra, 0);
195}
196
197// Next try to load Extra extensions from the selected root partition.
198strcpy(dirSpecExtra, "/Extra/");
199if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
200// If failed, then try to load Extra extensions from the boot partition
201// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.
202if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)
203|| (gRAMDiskVolume && gRAMDiskBTAliased) ) {
204// Next try a specfic OS version folder ie 10.5
205sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);
206if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
207// Next we'll try the base
208strcpy(dirSpecExtra, "bt(0,0)/Extra/");
209FileLoadDrivers(dirSpecExtra, 0);
210}
211}
212}
213if(!gHaveKernelCache) {
214// Don't load main driver (from /System/Library/Extentions) if gHaveKernelCache is set.
215// since these drivers will already be in the kernel cache.
216// NOTE: when gHaveKernelCache, xnu cannot (by default) load *any* extra kexts from the bootloader.
217// The /Extra code is not disabled in this case due to a kernel patch that allows for this to happen.
218
219// Also try to load Extensions from boot helper partitions.
220if (gBootVolume->flags & kBVFlagBooter) {
221strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
222if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
223strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
224if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
225strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
226FileLoadDrivers(dirSpecExtra, 0);
227}
228}
229}
230
231if (gMKextName[0] != '\0') {
232verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
233if ( LoadDriverMKext(gMKextName) != 0 ) {
234error("Could not load %s\n", gMKextName);
235return -1;
236}
237} else {
238if (gMacOSVersion[3] == '9') {
239strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */
240strcat(gExtensionsSpec, "Library/");
241FileLoadDrivers(gExtensionsSpec, 0);
242}
243strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */
244strcat(gExtensionsSpec, "System/Library/");
245FileLoadDrivers(gExtensionsSpec, 0);
246}
247
248}
249} else {
250return 0;
251}
252
253MatchPersonalities();
254
255MatchLibraries();
256
257LoadMatchedModules();
258
259return 0;
260}
261
262//==========================================================================
263// FileLoadMKext
264
265static long
266FileLoadMKext( const char * dirSpec, const char * extDirSpec )
267{
268longret, flags, time, time2;
269charaltDirSpec[512];
270
271snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);
272ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);
273
274if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
275{
276ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);
277
278if ((ret != 0)
279|| ((flags & kFileTypeMask) != kFileTypeDirectory)
280|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))
281{
282snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);
283verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);
284
285if (LoadDriverMKext(gDriverSpec) == 0) {
286return 0;
287}
288}
289}
290return -1;
291}
292
293//==========================================================================
294// FileLoadDrivers
295
296long
297FileLoadDrivers( char * dirSpec, long plugin )
298{
299long ret, length, flags, time, bundleType;
300long long index;
301long result = -1;
302const char * name;
303
304if ( !plugin )
305{
306// First try 10.6's path for loading Extensions.mkext.
307if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {
308return 0;
309}
310
311// Next try the legacy path.
312else if (FileLoadMKext(dirSpec, "") == 0) {
313return 0;
314}
315
316strcat(dirSpec, "Extensions");
317}
318
319index = 0;
320while (1)
321{
322ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
323if (ret == -1) {
324break;
325}
326
327// Make sure this is a directory.
328if ((flags & kFileTypeMask) != kFileTypeDirectory) {
329continue;
330}
331
332// Make sure this is a kext.
333length = strlen(name);
334if (strcmp(name + length - 5, ".kext")) {
335continue;
336}
337
338// Save the file name.
339strlcpy(gFileName, name, 4096);
340
341// Determine the bundle type.
342snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);
343ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);
344if (ret == 0) {
345bundleType = kCFBundleType2;
346} else {
347bundleType = kCFBundleType3;
348}
349
350if (!plugin) {
351snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");
352}
353
354ret = LoadDriverPList(dirSpec, gFileName, bundleType);
355
356if (result != 0) {
357result = ret;
358}
359
360if (!plugin) {
361FileLoadDrivers(gDriverSpec, 1);
362}
363}
364
365return result;
366}
367
368
369//==========================================================================
370//
371
372long
373NetLoadDrivers( char * dirSpec )
374{
375long tries;
376
377#if NODEF
378long cnt;
379
380// Get the name of the kernel
381cnt = strlen(gBootFile);
382while (cnt--) {
383if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {
384cnt++;
385break;
386}
387}
388#endif
389
390// INTEL modification
391snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);
392
393verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);
394
395tries = 3;
396while (tries--)
397{
398if (LoadDriverMKext(gDriverSpec) == 0) {
399break;
400}
401}
402if (tries == -1) {
403return -1;
404}
405
406return 0;
407}
408
409//==========================================================================
410// loadDriverMKext
411
412long
413LoadDriverMKext( char * fileSpec )
414{
415unsigned long driversAddr, driversLength;
416long length;
417char segName[32];
418DriversPackage * package;
419
420#define GetPackageElement(e) OSSwapBigToHostInt32(package->e)
421
422// Load the MKext.
423length = LoadThinFatFile(fileSpec, (void **)&package);
424if (length < sizeof (DriversPackage)) {
425return -1;
426}
427
428// call hook to notify modules that the mkext has been loaded
429execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);
430
431
432// Verify the MKext.
433if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||
434( GetPackageElement(signature2) != kDriverPackageSignature2) ||
435( GetPackageElement(length) > kLoadSize ) ||
436( GetPackageElement(adler32) !=
437Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )
438{
439return -1;
440}
441
442// Make space for the MKext.
443driversLength = GetPackageElement(length);
444driversAddr = AllocateKernelMemory(driversLength);
445
446// Copy the MKext.
447memcpy((void *)driversAddr, (void *)package, driversLength);
448
449// Add the MKext to the memory map.
450snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);
451AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);
452
453return 0;
454}
455
456//==========================================================================
457// LoadDriverPList
458
459long
460LoadDriverPList( char * dirSpec, char * name, long bundleType )
461{
462long length, executablePathLength, bundlePathLength;
463ModulePtr module;
464TagPtr personalities;
465char * buffer = 0;
466char * tmpExecutablePath = 0;
467char * tmpBundlePath = 0;
468long ret = -1;
469
470do{
471// Save the driver path.
472
473if(name) {
474snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
475} else {
476snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
477}
478executablePathLength = strlen(gFileSpec) + 1;
479
480tmpExecutablePath = malloc(executablePathLength);
481if (tmpExecutablePath == 0) {
482break;
483}
484strcpy(tmpExecutablePath, gFileSpec);
485
486if(name) {
487snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);
488} else {
489snprintf(gFileSpec, 4096, "%s", dirSpec);
490}
491bundlePathLength = strlen(gFileSpec) + 1;
492
493tmpBundlePath = malloc(bundlePathLength);
494if (tmpBundlePath == 0) {
495break;
496}
497
498strcpy(tmpBundlePath, gFileSpec);
499
500// Construct the file spec to the plist, then load it.
501
502if(name) {
503snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");
504} else {
505snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");
506}
507
508length = LoadFile(gFileSpec);
509if (length == -1) {
510break;
511}
512length = length + 1;
513buffer = malloc(length);
514if (buffer == 0) {
515break;
516}
517strlcpy(buffer, (char *)kLoadAddr, length);
518
519// Parse the plist.
520
521ret = ParseXML(buffer, &module, &personalities);
522
523if (ret != 0) {
524break;
525}
526
527if (!module) {
528ret = -1;
529break;
530} // Should never happen but it will make the compiler happy
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) {
711if (module->willLoad == 1) {
712prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
713
714if (prop != 0) {
715prop = prop->tag;
716
717while (prop != 0) {
718module2 = gModuleHead;
719
720while (module2 != 0) {
721prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
722
723if ((prop2 != 0) && (!strcmp(prop->string, prop2->string))) {
724if (module2->willLoad == 0) {
725module2->willLoad = 1;
726}
727break;
728}
729module2 = module2->nextModule;
730}
731prop = prop->tagNext;
732}
733}
734module->willLoad = 2;
735done = 0;
736}
737module = module->nextModule;
738}
739}
740while (!done);
741
742return 0;
743}
744
745
746//==========================================================================
747// FindModule
748
749#if NOTDEF
750static ModulePtr
751FindModule( char * name )
752{
753ModulePtr module;
754TagPtr prop;
755
756module = gModuleHead;
757
758while (module != 0) {
759prop = GetProperty(module->dict, kPropCFBundleIdentifier);
760
761if ((prop != 0) && !strcmp(name, prop->string)) {
762break;
763}
764
765module = module->nextModule;
766}
767
768return module;
769}
770#endif /* NOTDEF */
771
772//==========================================================================
773// ParseXML
774
775static long
776ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )
777{
778long length, pos;
779TagPtr moduleDict, required;
780ModulePtr tmpModule;
781
782pos = 0;
783
784while (1)
785{
786length = XMLParseNextTag(buffer + pos, &moduleDict);
787if (length == -1) {
788break;
789}
790
791pos += length;
792
793if (moduleDict == 0) {
794continue;
795}
796if (moduleDict->type == kTagTypeDict) {
797break;
798}
799XMLFreeTag(moduleDict);
800}
801
802if (length == -1) {
803return -1;
804}
805
806required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
807
808if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot")) {
809XMLFreeTag(moduleDict);
810return -2;
811}
812
813tmpModule = malloc(sizeof(Module));
814if (tmpModule == 0) {
815XMLFreeTag(moduleDict);
816return -1;
817}
818tmpModule->dict = moduleDict;
819
820// For now, load any module that has OSBundleRequired != "Safe Boot".
821
822tmpModule->willLoad = 1;
823
824*module = tmpModule;
825
826// Get the personalities.
827
828*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
829
830return 0;
831}
832
833#if NOTDEF
834static char gPlatformName[64];
835#endif
836
837long
838DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
839{
840long ret;
841compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;
842u_int32_t uncompressed_size, size;
843void *buffer;
844unsigned long len;
845
846#if 0
847printf("kernel header:\n");
848printf("signature: 0x%x\n", kernel_header->signature);
849printf("compress_type: 0x%x\n", kernel_header->compress_type);
850printf("adler32: 0x%x\n", kernel_header->adler32);
851printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
852printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
853getchar();
854#endif
855
856if (kernel_header->signature == OSSwapBigToHostConstInt32('comp')) {
857if (kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) {
858error("kernel compression is bad\n");
859return -1;
860}
861#if NOTDEF
862if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name)) {
863return -1;
864}
865if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path)) {
866return -1;
867}
868#endif
869
870uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
871binary = buffer = malloc(uncompressed_size);
872
873size = decompress_lzss((u_int8_t *) binary, &kernel_header->data[0],
874 OSSwapBigToHostInt32(kernel_header->compressed_size));
875if (uncompressed_size != size) {
876error("size mismatch from lzss: %x\n", size);
877return -1;
878}
879
880if (OSSwapBigToHostInt32(kernel_header->adler32) !=
881Adler32(binary, uncompressed_size)) {
882printf("adler mismatch\n");
883return -1;
884}
885}
886
887ret = ThinFatFile(&binary, &len);
888if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64) {
889archCpuType=CPU_TYPE_I386;
890ret = ThinFatFile(&binary, &len);
891}
892
893// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
894execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);
895
896ret = DecodeMachO(binary, rentry, raddr, rsize);
897if (ret<0 && archCpuType==CPU_TYPE_X86_64) {
898archCpuType=CPU_TYPE_I386;
899ret = DecodeMachO(binary, rentry, raddr, rsize);
900}
901
902return ret;
903}
904

Archive Download this file

Revision: 2341