Chameleon

Chameleon Svn Source Tree

Root/branches/chucko/i386/boot2/drivers.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * drivers.c - Driver Loading Functions.
26 *
27 * Copyright (c) 2000 Apple Computer, Inc.
28 *
29 * DRI: Josh de Cesare
30 */
31
32#include <mach-o/fat.h>
33#include <libkern/OSByteOrder.h>
34#include <mach/machine.h>
35
36#include "sl.h"
37#include "boot.h"
38#include "bootstruct.h"
39#include "xml.h"
40#include "ramdisk.h"
41#include "modules.h"
42
43#if DEBUG
44#define DBG(x...)printf(x)
45#else
46#define DBG(x...)msglog(x)
47#endif
48
49// extern char gMacOSVersion[8];
50
51struct Module {
52struct Module *nextModule;
53long willLoad;
54TagPtr dict;
55char *plistAddr;
56long plistLength;
57char *executablePath;
58char *bundlePath;
59long bundlePathLength;
60};
61typedef struct Module Module, *ModulePtr;
62
63struct DriverInfo {
64char *plistAddr;
65long plistLength;
66void *executableAddr;
67long executableLength;
68void *bundlePathAddr;
69long bundlePathLength;
70};
71typedef struct DriverInfo DriverInfo, *DriverInfoPtr;
72
73#define kDriverPackageSignature1 'MKXT'
74#define kDriverPackageSignature2 'MOSX'
75
76struct DriversPackage {
77 unsigned long signature1;
78 unsigned long signature2;
79 unsigned long length;
80 unsigned long adler32;
81 unsigned long version;
82 unsigned long numDrivers;
83 unsigned long reserved1;
84 unsigned long reserved2;
85};
86typedef struct DriversPackage DriversPackage;
87
88enum {
89kCFBundleType2,
90kCFBundleType3
91};
92
93long (*LoadExtraDrivers_p)(FileLoadDrivers_t FileLoadDrivers_p);
94
95/*static*/ unsigned long Adler32( unsigned char * buffer, long length );
96
97long FileLoadDrivers(char *dirSpec, long plugin);
98long NetLoadDrivers(char *dirSpec);
99long LoadDriverMKext(char *fileSpec);
100long LoadDriverPList(char *dirSpec, char *name, long bundleType);
101long LoadMatchedModules(void);
102
103static long MatchPersonalities(void);
104static long MatchLibraries(void);
105#ifdef NOTDEF
106static ModulePtr FindModule(char *name);
107static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);
108#endif
109static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);
110static long InitDriverSupport(void);
111
112ModulePtr gModuleHead, gModuleTail;
113static TagPtr gPersonalityHead, gPersonalityTail;
114static char * gExtensionsSpec;
115static char * gDriverSpec;
116static char * gFileSpec;
117static char * gTempSpec;
118static char * gFileName;
119
120/*static*/ unsigned long
121Adler32( unsigned char * buffer, long length )
122{
123long cnt;
124unsigned long result, lowHalf, highHalf;
125
126lowHalf = 1;
127highHalf = 0;
128
129for (cnt = 0; cnt < length; cnt++)
130{
131if ((cnt % 5000) == 0)
132{
133lowHalf %= 65521L;
134highHalf %= 65521L;
135}
136
137lowHalf += buffer[cnt];
138highHalf += lowHalf;
139}
140
141lowHalf %= 65521L;
142highHalf %= 65521L;
143
144result = (highHalf << 16) | lowHalf;
145
146return result;
147}
148
149
150//==========================================================================
151// InitDriverSupport
152
153static long
154InitDriverSupport( void )
155{
156gExtensionsSpec = malloc( 4096 );
157gDriverSpec = malloc( 4096 );
158gFileSpec = malloc( 4096 );
159gTempSpec = malloc( 4096 );
160gFileName = malloc( 4096 );
161
162if ( !gExtensionsSpec || !gDriverSpec || !gFileSpec || !gTempSpec || !gFileName ) {
163stop("InitDriverSupport error");
164}
165
166return 0;
167}
168
169//==========================================================================
170// LoadDrivers
171
172long LoadDrivers( char * dirSpec )
173{
174char dirSpecExtra[1024];
175
176if ( InitDriverSupport() != 0 ) {
177return 0;
178}
179
180// Load extra drivers if a hook has been installed.
181if (LoadExtraDrivers_p != NULL)
182{
183(*LoadExtraDrivers_p)(&FileLoadDrivers);
184}
185
186if ( gBootFileType == kNetworkDeviceType )
187{
188if (NetLoadDrivers(dirSpec) != 0)
189{
190error("Could not load drivers from the network\n");
191return -1;
192}
193}
194else if ( gBootFileType == kBlockDeviceType )
195{
196// First try to load Extra extensions from the ramdisk if isn't aliased as bt(0,0).
197if (gRAMDiskVolume && !gRAMDiskBTAliased)
198{
199strcpy(dirSpecExtra, "rd(0,0)/Extra/");
200FileLoadDrivers(dirSpecExtra, 0);
201}
202
203// Next try to load Extra extensions from the selected root partition.
204strcpy(dirSpecExtra, "/Extra/");
205if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
206// If failed, then try to load Extra extensions from the boot partition
207// in case we have a separate booter partition or a bt(0,0) aliased ramdisk.
208if ( !(gBIOSBootVolume->biosdev == gBootVolume->biosdev && gBIOSBootVolume->part_no == gBootVolume->part_no)
209|| (gRAMDiskVolume && gRAMDiskBTAliased) ) {
210// Next try a specfic OS version folder ie 10.5
211sprintf(dirSpecExtra, "bt(0,0)/Extra/%s/", &gMacOSVersion);
212if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
213// Next we'll try the base
214strcpy(dirSpecExtra, "bt(0,0)/Extra/");
215FileLoadDrivers(dirSpecExtra, 0);
216}
217}
218}
219if(!gHaveKernelCache) {
220// Don't load main driver (from /System/Library/Extentions) if gHaveKernelCache is set.
221// since these drivers will already be in the kernel cache.
222// NOTE: when gHaveKernelCache, xnu cannot (by default) load *any* extra kexts from the bootloader.
223// The /Extra code is not disabled in this case due to a kernel patch that allows for this to happen.
224
225// Also try to load Extensions from boot helper partitions.
226if (gBootVolume->flags & kBVFlagBooter) {
227strcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/");
228if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
229strcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/");
230if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
231strcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/");
232FileLoadDrivers(dirSpecExtra, 0);
233}
234}
235}
236
237if (gMKextName[0] != '\0') {
238verbose("LoadDrivers: Loading from [%s]\n", gMKextName);
239if ( LoadDriverMKext(gMKextName) != 0 ) {
240error("Could not load %s\n", gMKextName);
241return -1;
242}
243}
244else
245{
246if ( MAVERICKS || YOSEMITE ) // issue 352
247{
248strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */
249strcat(gExtensionsSpec, "Library/");
250FileLoadDrivers(gExtensionsSpec, 0);
251}
252strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */
253strcat(gExtensionsSpec, "System/Library/");
254FileLoadDrivers(gExtensionsSpec, 0);
255}
256
257}
258} else {
259return 0;
260}
261
262MatchPersonalities();
263
264MatchLibraries();
265
266LoadMatchedModules();
267
268return 0;
269}
270
271//==========================================================================
272// FileLoadMKext
273
274static long
275FileLoadMKext( const char * dirSpec, const char * extDirSpec )
276{
277 long ret, flags;
278 u_int32_t time, time2;
279charaltDirSpec[512];
280
281snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);
282ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);
283
284if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
285{
286ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);
287
288if ((ret != 0)
289|| ((flags & kFileTypeMask) != kFileTypeDirectory)
290|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))
291{
292snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);
293verbose("LoadDrivers: Loading from [%s]\n", gDriverSpec);
294
295if (LoadDriverMKext(gDriverSpec) == 0) {
296return 0;
297}
298}
299}
300return -1;
301}
302
303//==========================================================================
304// FileLoadDrivers
305
306long
307FileLoadDrivers( char * dirSpec, long plugin )
308{
309long long index;
310long ret, length, flags, bundleType;
311long result = -1;
312 u_int32_t time;
313const char * name;
314
315if ( !plugin )
316{
317// First try 10.6's path for loading Extensions.mkext.
318if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {
319return 0;
320}
321
322// Next try the legacy path.
323else if (FileLoadMKext(dirSpec, "") == 0) {
324return 0;
325}
326
327strcat(dirSpec, "Extensions");
328}
329
330index = 0;
331while (1)
332{
333ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
334if (ret == -1) {
335break;
336}
337
338// Make sure this is a directory.
339if ((flags & kFileTypeMask) != kFileTypeDirectory) {
340continue;
341}
342
343// Make sure this is a kext.
344length = strlen(name);
345if (strcmp(name + length - 5, ".kext")) {
346continue;
347}
348
349// Save the file name.
350strlcpy(gFileName, name, 4096);
351
352// Determine the bundle type.
353snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);
354ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);
355if (ret == 0) {
356bundleType = kCFBundleType2;
357} else {
358bundleType = kCFBundleType3;
359}
360
361if (!plugin) {
362snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");
363}
364
365ret = LoadDriverPList(dirSpec, gFileName, bundleType);
366
367if (result != 0) {
368result = ret;
369}
370
371if (!plugin) {
372FileLoadDrivers(gDriverSpec, 1);
373}
374}
375
376return result;
377}
378
379
380//==========================================================================
381//
382
383long
384NetLoadDrivers( char * dirSpec )
385{
386long tries;
387
388#if NODEF
389long cnt;
390
391// Get the name of the kernel
392cnt = strlen(gBootFile);
393while (cnt--) {
394if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ',')) {
395cnt++;
396break;
397}
398}
399#endif
400
401// INTEL modification
402snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);
403
404verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);
405
406tries = 3;
407while (tries--)
408{
409if (LoadDriverMKext(gDriverSpec) == 0) {
410break;
411}
412}
413if (tries == -1) {
414return -1;
415}
416
417return 0;
418}
419
420//==========================================================================
421// loadDriverMKext
422
423long
424LoadDriverMKext( char * fileSpec )
425{
426unsigned long driversAddr, driversLength;
427long length;
428char segName[32];
429DriversPackage * package;
430
431#define GetPackageElement(e) OSSwapBigToHostInt32(package->e)
432
433// Load the MKext.
434length = LoadThinFatFile(fileSpec, (void **)&package);
435if (length < sizeof (DriversPackage)) {
436return -1;
437}
438
439// call hook to notify modules that the mkext has been loaded
440execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);
441
442
443// Verify the MKext.
444if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||
445( GetPackageElement(signature2) != kDriverPackageSignature2) ||
446( GetPackageElement(length) > kLoadSize ) ||
447( GetPackageElement(adler32) !=
448Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )
449{
450return -1;
451}
452
453// Make space for the MKext.
454driversLength = GetPackageElement(length);
455driversAddr = AllocateKernelMemory(driversLength);
456
457// Copy the MKext.
458memcpy((void *)driversAddr, (void *)package, driversLength);
459
460// Add the MKext to the memory map.
461snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);
462AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);
463
464return 0;
465}
466
467//==========================================================================
468// LoadDriverPList
469
470long
471LoadDriverPList( char * dirSpec, char * name, long bundleType )
472{
473long length, executablePathLength, bundlePathLength;
474ModulePtr module;
475TagPtr personalities;
476char * buffer = 0;
477char * tmpExecutablePath = 0;
478char * tmpBundlePath = 0;
479long ret = -1;
480
481do{
482// Save the driver path.
483
484if(name) {
485snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
486} else {
487snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
488}
489executablePathLength = strlen(gFileSpec) + 1;
490
491tmpExecutablePath = malloc(executablePathLength);
492if (tmpExecutablePath == 0) {
493break;
494}
495strcpy(tmpExecutablePath, gFileSpec);
496
497if(name) {
498snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);
499} else {
500snprintf(gFileSpec, 4096, "%s", dirSpec);
501}
502bundlePathLength = strlen(gFileSpec) + 1;
503
504tmpBundlePath = malloc(bundlePathLength);
505if (tmpBundlePath == 0) {
506break;
507}
508
509strcpy(tmpBundlePath, gFileSpec);
510
511// Construct the file spec to the plist, then load it.
512
513if(name) {
514snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");
515} else {
516snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");
517}
518
519length = LoadFile(gFileSpec);
520if (length == -1) {
521break;
522}
523length = length + 1;
524buffer = malloc(length);
525if (buffer == 0) {
526break;
527}
528strlcpy(buffer, (char *)kLoadAddr, length);
529
530// Parse the plist.
531
532ret = ParseXML(buffer, &module, &personalities);
533
534if (ret != 0) {
535break;
536}
537
538if (!module) // cparm
539{
540ret = -1;
541break;
542} // Should never happen but it will make the compiler happy
543
544// Allocate memory for the driver path and the plist.
545
546module->executablePath = tmpExecutablePath;
547module->bundlePath = tmpBundlePath;
548module->bundlePathLength = bundlePathLength;
549module->plistAddr = malloc(length);
550
551if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0)) {
552break;
553}
554
555// Save the driver path in the module.
556//strcpy(module->driverPath, tmpDriverPath);
557tmpExecutablePath = 0;
558tmpBundlePath = 0;
559
560// Add the plist to the module.
561
562strlcpy(module->plistAddr, (char *)kLoadAddr, length);
563module->plistLength = length;
564
565// Add the module to the end of the module list.
566
567if (gModuleHead == 0) {
568gModuleHead = module;
569} else {
570gModuleTail->nextModule = module;
571}
572gModuleTail = module;
573
574// Add the persionalities to the personality list.
575
576if (personalities) {
577personalities = personalities->tag;
578}
579while (personalities != 0)
580{
581if (gPersonalityHead == 0) {
582gPersonalityHead = personalities->tag;
583} else {
584gPersonalityTail->tagNext = personalities->tag;
585}
586
587gPersonalityTail = personalities->tag;
588personalities = personalities->tagNext;
589}
590
591ret = 0;
592}
593while (0);
594
595if ( buffer ) {
596free( buffer );
597}
598if ( tmpExecutablePath ) {
599free( tmpExecutablePath );
600}
601if ( tmpBundlePath ) {
602free( tmpBundlePath );
603}
604return ret;
605}
606
607
608//==========================================================================
609// LoadMatchedModules
610
611long
612LoadMatchedModules( void )
613{
614TagPtr prop;
615ModulePtr module;
616char *fileName, segName[32];
617DriverInfoPtr driver;
618long length, driverAddr, driverLength;
619void *executableAddr = 0;
620
621module = gModuleHead;
622
623while (module != 0)
624{
625if (module->willLoad)
626{
627prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);
628
629if (prop != 0)
630{
631fileName = prop->string;
632snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);
633length = LoadThinFatFile(gFileSpec, &executableAddr);
634if (length == 0)
635{
636length = LoadFile(gFileSpec);
637executableAddr = (void *)kLoadAddr;
638}
639//printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();
640}
641else
642length = 0;
643
644if (length != -1)
645{
646//driverModuleAddr = (void *)kLoadAddr;
647//if (length != 0)
648//{
649//ThinFatFile(&driverModuleAddr, &length);
650//}
651
652// Make make in the image area.
653
654execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);
655
656driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;
657driverAddr = AllocateKernelMemory(driverLength);
658
659// Set up the DriverInfo.
660driver = (DriverInfoPtr)driverAddr;
661driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));
662driver->plistLength = module->plistLength;
663if (length != 0)
664{
665driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +
666 module->plistLength);
667driver->executableLength = length;
668}
669else
670{
671driver->executableAddr = 0;
672driver->executableLength = 0;
673}
674driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +
675 module->plistLength + driver->executableLength);
676driver->bundlePathLength = module->bundlePathLength;
677
678// Save the plist, module and bundle.
679strcpy(driver->plistAddr, module->plistAddr);
680if (length != 0)
681{
682memcpy(driver->executableAddr, executableAddr, length);
683}
684strcpy(driver->bundlePathAddr, module->bundlePath);
685
686// Add an entry to the memory map.
687snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);
688AllocateMemoryRange(segName, driverAddr, driverLength,
689kBootDriverTypeKEXT);
690}
691}
692module = module->nextModule;
693}
694
695return 0;
696}
697
698//==========================================================================
699// MatchPersonalities
700
701static long
702MatchPersonalities( void )
703{
704/* IONameMatch support not implemented */
705return 0;
706}
707
708//==========================================================================
709// MatchLibraries
710
711static long
712MatchLibraries( void )
713{
714TagPtr prop, prop2;
715ModulePtr module, module2;
716long done;
717
718do {
719done = 1;
720module = gModuleHead;
721
722while (module != 0)
723{
724if (module->willLoad == 1)
725{
726prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
727
728if (prop != 0)
729{
730prop = prop->tag;
731
732while (prop != 0)
733{
734module2 = gModuleHead;
735
736while (module2 != 0)
737{
738prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
739
740if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))
741{
742if (module2->willLoad == 0)
743{
744module2->willLoad = 1;
745}
746break;
747}
748module2 = module2->nextModule;
749}
750prop = prop->tagNext;
751}
752}
753module->willLoad = 2;
754done = 0;
755}
756module = module->nextModule;
757}
758}
759while (!done);
760
761return 0;
762}
763
764
765//==========================================================================
766// FindModule
767
768#if NOTDEF
769static ModulePtr
770FindModule( char * name )
771{
772ModulePtr module;
773TagPtr prop;
774
775module = gModuleHead;
776
777while (module != 0)
778{
779prop = GetProperty(module->dict, kPropCFBundleIdentifier);
780
781if ((prop != 0) && !strcmp(name, prop->string))
782{
783break;
784}
785
786module = module->nextModule;
787}
788
789return module;
790}
791#endif /* NOTDEF */
792
793//==========================================================================
794// ParseXML
795
796static long
797ParseXML( char * buffer, ModulePtr * module, TagPtr * personalities )
798{
799long length, pos;
800TagPtr moduleDict, required;
801ModulePtr tmpModule;
802
803pos = 0;
804
805while (1)
806{
807length = XMLParseNextTag(buffer + pos, &moduleDict);
808if (length == -1)
809{
810break;
811}
812
813pos += length;
814
815if (moduleDict == 0)
816{
817continue;
818}
819if (moduleDict->type == kTagTypeDict)
820{
821break;
822}
823XMLFreeTag(moduleDict);
824}
825
826if (length == -1)
827{
828return -1;
829}
830
831required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
832
833if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))
834{
835XMLFreeTag(moduleDict);
836return -2;
837}
838
839tmpModule = malloc(sizeof(Module));
840if (tmpModule == 0)
841{
842XMLFreeTag(moduleDict);
843return -1;
844}
845tmpModule->dict = moduleDict;
846
847// For now, load any module that has OSBundleRequired != "Safe Boot".
848
849tmpModule->willLoad = 1;
850
851*module = tmpModule;
852
853// Get the personalities.
854
855*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
856
857return 0;
858}
859
860#if NOTDEF
861static char gPlatformName[64];
862#endif
863
864char *gDarwinBuildVerStr = "Darwin Kernel Version"; // Bungo
865
866long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
867{
868long ret = 0;
869compressed_kernel_header * kernel_header = (compressed_kernel_header *) binary;
870u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;
871void *buffer = NULL;
872unsigned long len = 0;
873
874/*#if 0
875printf("kernel header:\n");
876printf("signature: 0x%x\n", kernel_header->signature);
877printf("compress_type: 0x%x\n", kernel_header->compress_type);
878printf("adler32: 0x%x\n", kernel_header->adler32);
879printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
880printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
881getchar();
882#endif*/
883
884if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))
885{
886DBG("Decompressing Kernel: ");
887
888if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&
889(kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))
890{
891error("ERROR: kernel compression is bad!\n");
892return -1;
893}
894#if NOTDEF
895if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
896{
897return -1;
898}
899
900if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
901{
902return -1;
903}
904#endif
905uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
906binary = buffer = malloc(uncompressed_size);
907
908// MinusZwei
909size = 0;
910switch (kernel_header->compress_type)
911{
912case OSSwapBigToHostConstInt32('lzvn'):
913size = decompress_lzvn( binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
914break;
915
916case OSSwapBigToHostConstInt32('lzss'):
917size = decompress_lzss( (u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
918break;
919
920default:
921break;
922}
923// MinusZwei
924
925if (uncompressed_size != size)
926{
927if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))
928{
929error("ERROR: size mismatch from lzvn (found: %x, expected: %x).\n", size, uncompressed_size);
930}
931
932if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))
933{
934error("ERROR: size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);
935}
936
937return -1;
938}
939
940adler32 = Adler32(binary, uncompressed_size);
941if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
942{
943error("ERROR: adler mismatch (found: %x, expected: %x).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
944return -1;
945}
946
947DBG("OK.\n");
948}
949
950ret = ThinFatFile(&binary, &len);
951if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
952{
953archCpuType=CPU_TYPE_I386;
954ret = ThinFatFile(&binary, &len);
955}
956
957// Bungo: no range checking, sorry
958size = 0;
959while (memcmp((uint8_t *)binary + size, (uint8_t *)gDarwinBuildVerStr, 21)) {
960size++;
961}
962gDarwinBuildVerStr = (char *)binary + size;
963
964// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
965execute_hook("DecodeKernel", (void*)binary, NULL, NULL, NULL);
966
967ret = DecodeMachO(binary, rentry, raddr, rsize);
968if (ret < 0 && archCpuType == CPU_TYPE_X86_64)
969{
970archCpuType = CPU_TYPE_I386;
971ret = DecodeMachO(binary, rentry, raddr, rsize);
972}
973
974return ret;
975}
976

Archive Download this file

Revision: 2454