Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2309