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

Archive Download this file

Revision: 2329