Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2043