Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/i386/boot2/drivers.c

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

Archive Download this file

Revision: 2587