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

Archive Download this file

Revision: 2436