Chameleon

Chameleon Svn Source Tree

Root/trunk/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.
206strlcpy(dirSpecExtra, "/Extra/", sizeof(dirSpecExtra));
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[0]);
216if (FileLoadDrivers(dirSpecExtra, 0) != 0) {
217// Next we'll try the base
218strlcpy(dirSpecExtra, "bt(0,0)/Extra/", sizeof(dirSpecExtra));
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)
232{
233strlcpy(dirSpecExtra, "/com.apple.boot.P/System/Library/", sizeof(dirSpecExtra));
234if (FileLoadDrivers(dirSpecExtra, 0) != 0)
235{
236strlcpy(dirSpecExtra, "/com.apple.boot.R/System/Library/", sizeof(dirSpecExtra));
237if (FileLoadDrivers(dirSpecExtra, 0) != 0)
238{
239strlcpy(dirSpecExtra, "/com.apple.boot.S/System/Library/", sizeof(dirSpecExtra));
240FileLoadDrivers(dirSpecExtra, 0);
241}
242}
243}
244
245if (gMKextName[0] != '\0')
246{
247verbose("LoadDrivers: Loading from '%s'\n", gMKextName);
248if ( LoadDriverMKext(gMKextName) != 0 )
249{
250error("Could not load %s\n", gMKextName);
251return -1;
252}
253}
254else
255{
256if ( MAVERICKS || YOSEMITE ) // issue 352
257{
258strlcpy(gExtensionsSpec, dirSpec, 4087); /* 4096 - sizeof("Library/") */
259strcat(gExtensionsSpec, "Library/");
260FileLoadDrivers(gExtensionsSpec, 0);
261}
262strlcpy(gExtensionsSpec, dirSpec, 4080); /* 4096 - sizeof("System/Library/") */
263strcat(gExtensionsSpec, "System/Library/");
264FileLoadDrivers(gExtensionsSpec, 0);
265}
266
267}
268}
269else
270{
271return 0;
272}
273
274MatchPersonalities();
275
276MatchLibraries();
277
278LoadMatchedModules();
279
280return 0;
281}
282
283//==========================================================================
284// FileLoadMKext
285
286static long FileLoadMKext( const char * dirSpec, const char * extDirSpec )
287{
288longret, flags;
289u_int32_ttime, time2;
290charaltDirSpec[512];
291
292snprintf(altDirSpec, sizeof(altDirSpec), "%s%s", dirSpec, extDirSpec);
293ret = GetFileInfo(altDirSpec, "Extensions.mkext", &flags, &time);
294
295if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
296{
297ret = GetFileInfo(dirSpec, "Extensions", &flags, &time2);
298
299if ((ret != 0)
300|| ((flags & kFileTypeMask) != kFileTypeDirectory)
301|| (((gBootMode & kBootModeSafe) == 0) && (time == (time2 + 1))))
302{
303snprintf(gDriverSpec, sizeof(altDirSpec) + 18, "%sExtensions.mkext", altDirSpec);
304verbose("LoadDrivers: Loading from '%s'\n", gDriverSpec);
305
306if (LoadDriverMKext(gDriverSpec) == 0)
307{
308return 0;
309}
310}
311}
312return -1;
313}
314
315//==========================================================================
316// FileLoadDrivers
317
318long FileLoadDrivers( char * dirSpec, long plugin )
319{
320long longindex;
321longret, length, flags, bundleType;
322longresult = -1;
323u_int32_ttime;
324const char* name;
325
326if ( !plugin )
327{
328// First try 10.6's path for loading Extensions.mkext.
329if (FileLoadMKext(dirSpec, "Caches/com.apple.kext.caches/Startup/") == 0) {
330return 0;
331}
332
333// Next try the legacy path.
334else if (FileLoadMKext(dirSpec, "") == 0)
335{
336return 0;
337}
338
339strcat(dirSpec, "Extensions");
340}
341
342index = 0;
343while (1)
344{
345ret = GetDirEntry(dirSpec, &index, &name, &flags, &time);
346if (ret == -1) {
347break;
348}
349
350// Make sure this is a directory.
351if ((flags & kFileTypeMask) != kFileTypeDirectory)
352{
353continue;
354}
355
356// Make sure this is a kext.
357length = strlen(name);
358if (strncmp(name + length - 5, ".kext", sizeof(".kext") ))
359{
360continue;
361}
362
363// Save the file name.
364strlcpy(gFileName, name, 4096);
365
366// Determine the bundle type.
367snprintf(gTempSpec, 4096, "%s/%s", dirSpec, gFileName);
368ret = GetFileInfo(gTempSpec, "Contents", &flags, &time);
369if (ret == 0)
370{
371bundleType = kCFBundleType2;
372}
373else
374{
375bundleType = kCFBundleType3;
376}
377
378if (!plugin)
379{
380snprintf(gDriverSpec, 4096, "%s/%s/%sPlugIns", dirSpec, gFileName, (bundleType == kCFBundleType2) ? "Contents/" : "");
381}
382
383ret = LoadDriverPList(dirSpec, gFileName, bundleType);
384
385if (result != 0)
386{
387result = ret;
388}
389
390if (!plugin)
391{
392FileLoadDrivers(gDriverSpec, 1);
393}
394}
395
396return result;
397}
398
399
400//==========================================================================
401//
402
403long NetLoadDrivers( char * dirSpec )
404{
405long tries;
406
407#if NODEF
408long cnt;
409
410// Get the name of the kernel
411cnt = strlen(gBootFile);
412while (cnt--) {
413if ((gBootFile[cnt] == '\\') || (gBootFile[cnt] == ','))
414{
415cnt++;
416break;
417}
418}
419#endif
420
421// INTEL modification
422snprintf(gDriverSpec, 4096, "%s%s.mkext", dirSpec, bootInfo->bootFile);
423
424verbose("NetLoadDrivers: Loading from [%s]\n", gDriverSpec);
425
426tries = 3;
427while (tries--)
428{
429if (LoadDriverMKext(gDriverSpec) == 0)
430{
431break;
432}
433}
434if (tries == -1)
435{
436return -1;
437}
438
439return 0;
440}
441
442//==========================================================================
443// loadDriverMKext
444
445long LoadDriverMKext( char * fileSpec )
446{
447unsigned long driversAddr, driversLength;
448long length;
449char segName[32];
450DriversPackage * package;
451
452#define GetPackageElement(e) OSSwapBigToHostInt32(package->e)
453
454// Load the MKext.
455length = LoadThinFatFile(fileSpec, (void **)&package);
456if (length < sizeof (DriversPackage))
457{
458return -1;
459}
460
461// call hook to notify modules that the mkext has been loaded
462execute_hook("LoadDriverMKext", (void*)fileSpec, (void*)package, (void*) &length, NULL);
463
464
465// Verify the MKext.
466if (( GetPackageElement(signature1) != kDriverPackageSignature1) ||
467( GetPackageElement(signature2) != kDriverPackageSignature2) ||
468( GetPackageElement(length) > kLoadSize ) ||
469( GetPackageElement(adler32) !=
470Adler32((unsigned char *)&package->version, GetPackageElement(length) - 0x10) ) )
471{
472return -1;
473}
474
475// Make space for the MKext.
476driversLength = GetPackageElement(length);
477driversAddr = AllocateKernelMemory(driversLength);
478
479// Copy the MKext.
480memcpy((void *)driversAddr, (void *)package, driversLength);
481
482// Add the MKext to the memory map.
483snprintf(segName, sizeof(segName), "DriversPackage-%lx", driversAddr);
484AllocateMemoryRange(segName, driversAddr, driversLength, kBootDriverTypeMKEXT);
485
486return 0;
487}
488
489//==========================================================================
490// LoadDriverPList
491
492long
493LoadDriverPList( char * dirSpec, char * name, long bundleType )
494{
495long length, executablePathLength, bundlePathLength;
496ModulePtr module;
497TagPtr personalities;
498char * buffer = 0;
499char * tmpExecutablePath = 0;
500char * tmpBundlePath = 0;
501long ret = -1;
502
503do{
504// Save the driver path.
505
506if(name)
507{
508snprintf(gFileSpec, 4096, "%s/%s/%s", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
509}
510else
511{
512snprintf(gFileSpec, 4096, "%s/%s", dirSpec, (bundleType == kCFBundleType2) ? "Contents/MacOS/" : "");
513}
514executablePathLength = strlen(gFileSpec) + 1;
515
516tmpExecutablePath = malloc(executablePathLength);
517if (tmpExecutablePath == 0) {
518break;
519}
520strlcpy(tmpExecutablePath, gFileSpec, executablePathLength);
521
522if(name)
523{
524snprintf(gFileSpec, 4096, "%s/%s", dirSpec, name);
525}
526else
527{
528strncpy(gFileSpec, dirSpec, 4096);
529}
530bundlePathLength = strlen(gFileSpec) + 1;
531
532tmpBundlePath = malloc(bundlePathLength);
533if (tmpBundlePath == 0)
534{
535break;
536}
537
538strlcpy(tmpBundlePath, gFileSpec, bundlePathLength);
539
540// Construct the file spec to the plist, then load it.
541
542if(name)
543{
544snprintf(gFileSpec, 4096, "%s/%s/%sInfo.plist", dirSpec, name, (bundleType == kCFBundleType2) ? "Contents/" : "");
545}
546else
547{
548snprintf(gFileSpec, 4096, "%s/%sInfo.plist", dirSpec, (bundleType == kCFBundleType2) ? "Contents/" : "");
549}
550
551length = LoadFile(gFileSpec);
552
553if (length == -1)
554{
555break;
556}
557
558length = length + 1;
559buffer = malloc(length);
560
561if (buffer == 0)
562{
563break;
564}
565
566strlcpy(buffer, (char *)kLoadAddr, length);
567
568// Parse the plist.
569
570ret = ParseXML(buffer, &module, &personalities);
571
572if (ret != 0) {
573break;
574}
575
576if (!module) // cparm
577{
578ret = -1;
579break;
580} // Should never happen but it will make the compiler happy
581
582// Allocate memory for the driver path and the plist.
583
584module->executablePath = tmpExecutablePath;
585module->bundlePath = tmpBundlePath;
586module->bundlePathLength = bundlePathLength;
587module->plistAddr = malloc(length);
588
589if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))
590{
591break;
592}
593
594// Save the driver path in the module.
595//strcpy(module->driverPath, tmpDriverPath);
596tmpExecutablePath = 0;
597tmpBundlePath = 0;
598
599// Add the plist to the module.
600
601strlcpy(module->plistAddr, (char *)kLoadAddr, length);
602module->plistLength = length;
603
604// Add the module to the end of the module list.
605
606if (gModuleHead == 0) {
607gModuleHead = module;
608} else {
609gModuleTail->nextModule = module;
610}
611gModuleTail = module;
612
613// Add the persionalities to the personality list.
614
615if (personalities) {
616personalities = personalities->tag;
617}
618while (personalities != 0)
619{
620if (gPersonalityHead == 0) {
621gPersonalityHead = personalities->tag;
622} else {
623gPersonalityTail->tagNext = personalities->tag;
624}
625
626gPersonalityTail = personalities->tag;
627personalities = personalities->tagNext;
628}
629
630ret = 0;
631}
632while (0);
633
634if ( buffer ) {
635free( buffer );
636}
637if ( tmpExecutablePath ) {
638free( tmpExecutablePath );
639}
640if ( tmpBundlePath ) {
641free( tmpBundlePath );
642}
643return ret;
644}
645
646
647//==========================================================================
648// LoadMatchedModules
649
650long
651LoadMatchedModules( void )
652{
653TagPtr prop;
654ModulePtr module;
655char *fileName, segName[32];
656DriverInfoPtr driver;
657long length, driverAddr, driverLength;
658void *executableAddr = 0;
659
660module = gModuleHead;
661
662while (module != 0)
663{
664if (module->willLoad)
665{
666prop = XMLGetProperty(module->dict, kPropCFBundleExecutable);
667
668if (prop != 0)
669{
670fileName = prop->string;
671snprintf(gFileSpec, 4096, "%s%s", module->executablePath, fileName);
672length = LoadThinFatFile(gFileSpec, &executableAddr);
673if (length == 0)
674{
675length = LoadFile(gFileSpec);
676executableAddr = (void *)kLoadAddr;
677}
678//printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();
679}
680else
681length = 0;
682
683if (length != -1)
684{
685//driverModuleAddr = (void *)kLoadAddr;
686//if (length != 0)
687//{
688//ThinFatFile(&driverModuleAddr, &length);
689//}
690
691// Make make in the image area.
692
693execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);
694
695driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;
696driverAddr = AllocateKernelMemory(driverLength);
697
698// Set up the DriverInfo.
699driver = (DriverInfoPtr)driverAddr;
700driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));
701driver->plistLength = module->plistLength;
702if (length != 0)
703{
704driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +
705 module->plistLength);
706driver->executableLength = length;
707}
708else
709{
710driver->executableAddr = 0;
711driver->executableLength = 0;
712}
713driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +
714 module->plistLength + driver->executableLength);
715driver->bundlePathLength = module->bundlePathLength;
716
717// Save the plist, module and bundle.
718strlcpy(driver->plistAddr, module->plistAddr, driver->plistLength);
719if (length != 0)
720{
721memcpy(driver->executableAddr, executableAddr, length);
722}
723strlcpy(driver->bundlePathAddr, module->bundlePath, module->bundlePathLength);
724
725// Add an entry to the memory map.
726snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);
727AllocateMemoryRange(segName, driverAddr, driverLength,
728kBootDriverTypeKEXT);
729}
730}
731module = module->nextModule;
732}
733
734return 0;
735}
736
737//==========================================================================
738// MatchPersonalities
739
740static long
741MatchPersonalities( void )
742{
743/* IONameMatch support not implemented */
744return 0;
745}
746
747//==========================================================================
748// MatchLibraries
749
750static long MatchLibraries( void )
751{
752TagPtr prop;
753TagPtr prop2;
754ModulePtr module;
755ModulePtr module2;
756long done;
757
758do {
759done = 1;
760module = gModuleHead;
761
762while (module != 0)
763{
764if (module->willLoad == 1)
765{
766prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
767
768if (prop != 0)
769{
770prop = prop->tag;
771
772while (prop != 0)
773{
774module2 = gModuleHead;
775
776while (module2 != 0)
777{
778prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
779
780if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))
781{
782if (module2->willLoad == 0)
783{
784module2->willLoad = 1;
785}
786break;
787}
788module2 = module2->nextModule;
789}
790prop = prop->tagNext;
791}
792}
793module->willLoad = 2;
794done = 0;
795}
796module = module->nextModule;
797}
798}
799while (!done);
800
801return 0;
802}
803
804
805//==========================================================================
806// FindModule
807
808#if NOTDEF
809static ModulePtr FindModule( char *name )
810{
811ModulePtr module;
812TagPtr prop;
813
814module = gModuleHead;
815
816while (module != 0)
817{
818prop = GetProperty(module->dict, kPropCFBundleIdentifier);
819
820if ((prop != 0) && !strcmp(name, prop->string))
821{
822break;
823}
824
825module = module->nextModule;
826}
827
828return module;
829}
830#endif /* NOTDEF */
831
832//==========================================================================
833// ParseXML
834
835static long ParseXML( char *buffer, ModulePtr *module, TagPtr *personalities )
836{
837longlength;
838longpos = 0;
839TagPtrmoduleDict;
840TagPtrrequired;
841ModulePtrtmpModule;
842
843while (1)
844{
845length = XMLParseNextTag(buffer + pos, &moduleDict);
846if (length == -1)
847{
848break;
849}
850
851pos += length;
852
853if (moduleDict == 0)
854{
855continue;
856}
857if (moduleDict->type == kTagTypeDict)
858{
859break;
860}
861XMLFreeTag(moduleDict);
862}
863
864if (length == -1)
865{
866return -1;
867}
868
869required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
870
871if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))
872{
873XMLFreeTag(moduleDict);
874return -2;
875}
876
877tmpModule = malloc(sizeof(Module));
878if (tmpModule == 0)
879{
880XMLFreeTag(moduleDict);
881return -1;
882}
883tmpModule->dict = moduleDict;
884
885// For now, load any module that has OSBundleRequired != "Safe Boot".
886
887tmpModule->willLoad = 1;
888
889*module = tmpModule;
890
891// Get the personalities.
892
893*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
894
895return 0;
896}
897
898#if NOTDEF
899static char gPlatformName[64];
900#endif
901
902long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
903{
904long ret = 0;
905compressed_kernel_header *kernel_header = (compressed_kernel_header *)binary;
906u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;
907void *buffer = NULL;
908unsigned long len = 0;
909
910/*#if 0
911printf("kernel header:\n");
912printf("signature: 0x%x\n", kernel_header->signature);
913printf("compress_type: 0x%x\n", kernel_header->compress_type);
914printf("adler32: 0x%x\n", kernel_header->adler32);
915printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
916printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
917getchar();
918#endif*/
919
920if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))
921{
922DBG("Decompressing Kernel Cache");
923
924if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&
925(kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))
926{
927error("ERROR: kernel compression is bad!\n");
928return -1;
929}
930
931if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))
932{
933verbose ("Decompressing Kernel Using lzss\n");
934}
935
936if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))
937{
938verbose ("Decompressing Kernel Using lzvn\n");
939}
940
941#if NOTDEF
942if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
943{
944return -1;
945}
946
947if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
948{
949return -1;
950}
951#endif
952uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
953binary = buffer = malloc(uncompressed_size);
954
955// MinusZwei
956size = 0;
957switch (kernel_header->compress_type)
958{
959case OSSwapBigToHostConstInt32('lzvn'):
960size = lzvn_decode(binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
961break;
962
963case OSSwapBigToHostConstInt32('lzss'):
964size = decompress_lzss((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
965break;
966
967default:
968break;
969}
970// MinusZwei
971
972if (uncompressed_size != size)
973{
974if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))
975{
976error("ERROR! Size mismatch from lzvn (found: %x, expected: %x).\n", size, uncompressed_size);
977}
978
979if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))
980{
981error("ERROR! size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);
982}
983
984return -1;
985}
986
987adler32 = Adler32(binary, uncompressed_size);
988if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
989{
990error("ERROR! Adler mismatch (found: %X, expected: %X).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
991return -1;
992}
993
994DBG("OK.\n");
995}
996
997ret = ThinFatFile(&binary, &len);
998if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
999{
1000archCpuType=CPU_TYPE_I386;
1001ret = ThinFatFile(&binary, &len);
1002}
1003
1004// Bungo: scan binary for Darwin Kernel Version string
1005uint32_t offset = 0;
1006strncpy(gDarwinBuildVerStr, "Darwin Kernel Version", sizeof(gDarwinBuildVerStr));
1007while ((offset < 0xFFFFFFFF - (uint32_t)binary - 256) && memcmp(binary + offset, gDarwinBuildVerStr, 21))
1008{
1009offset++;
1010}
1011if (offset < 0xFFFFFFFF - (uint32_t)binary - 256)
1012{
1013strncpy(gDarwinBuildVerStr, (char *)(binary + offset), sizeof(gDarwinBuildVerStr));
1014}
1015else
1016{
1017strcat(gDarwinBuildVerStr, ": Unknown");
1018}
1019
1020// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
1021execute_hook("DecodeKernel", (void *)binary, NULL, NULL, NULL);
1022
1023ret = DecodeMachO(binary, rentry, raddr, rsize);
1024if (ret < 0 && archCpuType == CPU_TYPE_X86_64)
1025{
1026archCpuType = CPU_TYPE_I386;
1027ret = DecodeMachO(binary, rentry, raddr, rsize);
1028}
1029
1030return ret;
1031}
1032

Archive Download this file

Revision: 2634