Chameleon

Chameleon Svn Source Tree

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

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

Archive Download this file

Revision: 2238