Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2469