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 || ELCAPITAN ) // 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);
672
673length = LoadThinFatFile(gFileSpec, &executableAddr);
674if (length == 0)
675{
676length = LoadFile(gFileSpec);
677executableAddr = (void *)kLoadAddr;
678}
679//printf("%s length = %d addr = 0x%x\n", gFileSpec, length, driverModuleAddr); getchar();
680}
681else
682{
683length = 0;
684}
685
686if (length != -1)
687{
688//driverModuleAddr = (void *)kLoadAddr;
689//if (length != 0)
690//{
691//ThinFatFile(&driverModuleAddr, &length);
692//}
693
694// Make make in the image area.
695
696execute_hook("LoadMatchedModules", module, &length, executableAddr, NULL);
697
698driverLength = sizeof(DriverInfo) + module->plistLength + length + module->bundlePathLength;
699driverAddr = AllocateKernelMemory(driverLength);
700
701// Set up the DriverInfo.
702driver = (DriverInfoPtr)driverAddr;
703driver->plistAddr = (char *)(driverAddr + sizeof(DriverInfo));
704driver->plistLength = module->plistLength;
705
706if (length != 0)
707{
708driver->executableAddr = (void *)(driverAddr + sizeof(DriverInfo) +
709 module->plistLength);
710driver->executableLength = length;
711}
712else
713{
714driver->executableAddr = 0;
715driver->executableLength = 0;
716}
717
718driver->bundlePathAddr = (void *)(driverAddr + sizeof(DriverInfo) +
719 module->plistLength + driver->executableLength);
720driver->bundlePathLength = module->bundlePathLength;
721
722// Save the plist, module and bundle.
723strlcpy(driver->plistAddr, module->plistAddr, driver->plistLength);
724
725if (length != 0)
726{
727memcpy(driver->executableAddr, executableAddr, length);
728}
729
730strlcpy(driver->bundlePathAddr, module->bundlePath, module->bundlePathLength);
731
732// Add an entry to the memory map.
733snprintf(segName, sizeof(segName), "Driver-%lx", (unsigned long)driver);
734AllocateMemoryRange(segName, driverAddr, driverLength,
735kBootDriverTypeKEXT);
736}
737}
738module = module->nextModule;
739}
740
741return 0;
742}
743
744//==========================================================================
745// MatchPersonalities
746
747static long
748MatchPersonalities( void )
749{
750/* IONameMatch support not implemented */
751return 0;
752}
753
754//==========================================================================
755// MatchLibraries
756
757static long MatchLibraries( void )
758{
759TagPtr prop;
760TagPtr prop2;
761ModulePtr module;
762ModulePtr module2;
763long done;
764
765do {
766done = 1;
767module = gModuleHead;
768
769while (module != 0)
770{
771if (module->willLoad == 1)
772{
773prop = XMLGetProperty(module->dict, kPropOSBundleLibraries);
774
775if (prop != 0)
776{
777prop = prop->tag;
778
779while (prop != 0)
780{
781module2 = gModuleHead;
782
783while (module2 != 0)
784{
785prop2 = XMLGetProperty(module2->dict, kPropCFBundleIdentifier);
786
787if ((prop2 != 0) && (!strcmp(prop->string, prop2->string)))
788{
789if (module2->willLoad == 0)
790{
791module2->willLoad = 1;
792}
793break;
794}
795module2 = module2->nextModule;
796}
797prop = prop->tagNext;
798}
799}
800module->willLoad = 2;
801done = 0;
802}
803module = module->nextModule;
804}
805}
806while (!done);
807
808return 0;
809}
810
811
812//==========================================================================
813// FindModule
814
815#if NOTDEF
816static ModulePtr FindModule( char *name )
817{
818ModulePtr module;
819TagPtr prop;
820
821module = gModuleHead;
822
823while (module != 0)
824{
825prop = GetProperty(module->dict, kPropCFBundleIdentifier);
826
827if ((prop != 0) && !strcmp(name, prop->string))
828{
829break;
830}
831
832module = module->nextModule;
833}
834
835return module;
836}
837#endif /* NOTDEF */
838
839//==========================================================================
840// ParseXML
841
842static long ParseXML( char *buffer, ModulePtr *module, TagPtr *personalities )
843{
844longlength;
845longpos = 0;
846TagPtrmoduleDict;
847TagPtrrequired;
848ModulePtrtmpModule;
849
850while (1)
851{
852length = XMLParseNextTag(buffer + pos, &moduleDict);
853if (length == -1)
854{
855break;
856}
857
858pos += length;
859
860if (moduleDict == 0)
861{
862continue;
863}
864if (moduleDict->type == kTagTypeDict)
865{
866break;
867}
868XMLFreeTag(moduleDict);
869}
870
871if (length == -1)
872{
873return -1;
874}
875
876required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
877
878if ( (required == 0) || (required->type != kTagTypeString) || !strcmp(required->string, "Safe Boot"))
879{
880XMLFreeTag(moduleDict);
881return -2;
882}
883
884tmpModule = malloc(sizeof(Module));
885if (tmpModule == 0)
886{
887XMLFreeTag(moduleDict);
888return -1;
889}
890tmpModule->dict = moduleDict;
891
892// For now, load any module that has OSBundleRequired != "Safe Boot".
893
894tmpModule->willLoad = 1;
895
896*module = tmpModule;
897
898// Get the personalities.
899
900*personalities = XMLGetProperty(moduleDict, kPropIOKitPersonalities);
901
902return 0;
903}
904
905#if NOTDEF
906static char gPlatformName[64];
907#endif
908
909long DecodeKernel(void *binary, entry_t *rentry, char **raddr, int *rsize)
910{
911long ret = 0;
912compressed_kernel_header *kernel_header = (compressed_kernel_header *)binary;
913u_int32_t uncompressed_size = 0, size = 0, adler32 = 0;
914void *buffer = NULL;
915unsigned long len = 0;
916
917/*#if 0
918printf("kernel header:\n");
919printf("signature: 0x%x\n", kernel_header->signature);
920printf("compress_type: 0x%x\n", kernel_header->compress_type);
921printf("adler32: 0x%x\n", kernel_header->adler32);
922printf("uncompressed_size: 0x%x\n", kernel_header->uncompressed_size);
923printf("compressed_size: 0x%x\n", kernel_header->compressed_size);
924getchar();
925#endif*/
926
927if (kernel_header->signature == OSSwapBigToHostConstInt32('comp'))
928{
929DBG("Decompressing Kernel Cache");
930
931if ((kernel_header->compress_type != OSSwapBigToHostConstInt32('lzss')) &&
932(kernel_header->compress_type != OSSwapBigToHostConstInt32('lzvn')))
933{
934error("ERROR: kernel compression is bad!\n");
935return -1;
936}
937
938if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))
939{
940verbose ("Decompressing Kernel Using lzss\n");
941}
942
943if (kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))
944{
945verbose ("Decompressing Kernel Using lzvn\n");
946}
947
948#if NOTDEF
949if (kernel_header->platform_name[0] && strcmp(gPlatformName, kernel_header->platform_name))
950{
951return -1;
952}
953
954if (kernel_header->root_path[0] && strcmp(gBootFile, kernel_header->root_path))
955{
956return -1;
957}
958#endif
959uncompressed_size = OSSwapBigToHostInt32(kernel_header->uncompressed_size);
960binary = buffer = malloc(uncompressed_size);
961
962// MinusZwei
963size = 0;
964switch (kernel_header->compress_type)
965{
966case OSSwapBigToHostConstInt32('lzvn'):
967size = lzvn_decode(binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
968break;
969
970case OSSwapBigToHostConstInt32('lzss'):
971size = decompress_lzss((u_int8_t *)binary, uncompressed_size, &kernel_header->data[0], OSSwapBigToHostInt32(kernel_header->compressed_size));
972break;
973
974default:
975break;
976}
977// MinusZwei
978
979if (uncompressed_size != size)
980{
981if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzvn'))
982{
983error("ERROR! Size mismatch from lzvn (found: %x, expected: %x).\n", size, uncompressed_size);
984}
985
986if ( kernel_header->compress_type == OSSwapBigToHostConstInt32('lzss'))
987{
988error("ERROR! size mismatch from lzss (found: %x, expected: %x).\n", size, uncompressed_size);
989}
990
991return -1;
992}
993
994adler32 = Adler32(binary, uncompressed_size);
995if (OSSwapBigToHostInt32(kernel_header->adler32) != adler32)
996{
997error("ERROR! Adler mismatch (found: %X, expected: %X).\n", adler32, OSSwapBigToHostInt32(kernel_header->adler32));
998return -1;
999}
1000
1001DBG("OK.\n");
1002}
1003
1004ret = ThinFatFile(&binary, &len);
1005if (ret == 0 && len == 0 && archCpuType==CPU_TYPE_X86_64)
1006{
1007archCpuType=CPU_TYPE_I386;
1008ret = ThinFatFile(&binary, &len);
1009}
1010
1011// Bungo: scan binary for Darwin Kernel Version string
1012uint32_t offset = 0;
1013strncpy(gDarwinBuildVerStr, "Darwin Kernel Version", sizeof(gDarwinBuildVerStr));
1014
1015while ((offset < 0xFFFFFFFF - (uint32_t)binary - 256) && memcmp(binary + offset, gDarwinBuildVerStr, 21))
1016{
1017offset++;
1018}
1019if (offset < 0xFFFFFFFF - (uint32_t)binary - 256)
1020{
1021strncpy(gDarwinBuildVerStr, (char *)(binary + offset), sizeof(gDarwinBuildVerStr));
1022}
1023else
1024{
1025strcat(gDarwinBuildVerStr, ": Unknown");
1026}
1027
1028// Notify modules that the kernel has been decompressed, thinned and is about to be decoded
1029execute_hook("DecodeKernel", (void *)binary, NULL, NULL, NULL);
1030
1031/* ================================================================ */
1032
1033// Entry point
1034
1035/* ================================================================ */
1036
1037ret = DecodeMachO(binary, rentry, raddr, rsize);
1038if (ret < 0 && archCpuType == CPU_TYPE_X86_64)
1039{
1040archCpuType = CPU_TYPE_I386;
1041ret = DecodeMachO(binary, rentry, raddr, rsize);
1042}
1043
1044return ret;
1045}
1046

Archive Download this file

Revision: 2729