Chameleon

Chameleon Commit Details

Date:2017-03-25 19:39:15 (2 years 2 months ago)
Author:ErmaC
Commit:2872
Parents: 2871
Message:Add embedded KextsPatcher (Micky1979)
Changes:
M/branches/ErmaC/Enoch/i386/boot2/config.h
M/branches/ErmaC/Enoch/i386/boot2/drivers.c
M/branches/ErmaC/Enoch/i386/boot2/boot.c

File differences

branches/ErmaC/Enoch/i386/boot2/config.h
1717
1818
1919
20
20
2121
2222
2323
////////////////////////////////// kernel_patcher.c
#define DEBUG_KERNEL 0// 0 mean NO show verbose message, 1 mean YES show verbose message
#define KEXTPATCH_SUPPORT 0// 1 enabled by default
#define KEXTPATCH_SUPPORT 1 // 1 enabled by default
////////////////////////////////// hpet.c
#define DEBUG_HPET 0
branches/ErmaC/Enoch/i386/boot2/drivers.c
100100
101101
102102
103
104
105
103106
104
107
105108
106109
107110
......
109112
110113
111114
112
115
116
117
113118
114119
115120
......
313318
314319
315320
321
322
323
316324
317325
318326
......
528536
529537
530538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
531747
532748
533749
......
606822
607823
608824
609
825
610826
611827
612828
......
627843
628844
629845
846
630847
631848
632849
......
8681085
8691086
8701087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
8711123
872
1124
8731125
8741126
8751127
......
9051157
9061158
9071159
1160
1161
9081162
9091163
9101164
9111165
9121166
1167
9131168
9141169
9151170
long NetLoadDrivers(char *dirSpec);
long LoadDriverMKext(char *fileSpec);
long LoadDriverPList(char *dirSpec, char *name, long bundleType);
long LoadEmbeddedKext( char *InjectorPlist, size_t length, char *name );
long LoadMatchedModules(void);
void buildAndLoadInjectorPList(void);
static long MatchPersonalities(void);
static long MatchLibraries(void);
static ModulePtr FindModule(char *name);
static void ThinFatFile(void **loadAddrP, unsigned long *lengthP);
#endif
static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities);
bool isKextForcedToLoad(char *kext);
static long ParseXML(char *buffer, ModulePtr *module, TagPtr *personalities, bool forceKextToLoad);
static long InitDriverSupport(void);
ModulePtr gModuleHead, gModuleTail;
return 0;
}
buildAndLoadInjectorPList();
MatchPersonalities();
MatchLibraries();
}
//==========================================================================
// buildAndLoadInjectorPList
void buildAndLoadInjectorPList(void)
{
TagPtr PersTag = NULL;
if ((PersTag = XMLCastDict(XMLGetProperty(bootInfo->kextConfig.dictionary, (const char *)"PersonalitiesInjector"))))
{
unsigned long count = XMLTagCount(PersTag);
if (count)
{
verbose("Attempting to generate and load custom injectors kexts:\n");
}
while(count)
{
bool canLoad = false;
//int length = 0;
char *kextName = strdup(XMLCastString(XMLGetKey(PersTag, count)));
TagPtr sub = XMLGetProperty(PersTag, kextName);
if (sub && XMLIsDict(sub))
{
TagPtr IOKPTag = NULL;
char *MatchOS = NULL;
char *Comment = NULL;
char *OSBundleRequired = NULL;
IOKPTag = XMLGetProperty(sub, (const char*)"IOKitPersonalities");
Comment = XMLCastString(XMLGetProperty(sub, (const char*)"Comment"));
MatchOS = XMLCastString(XMLGetProperty(sub, (const char*)"MatchOS"));
OSBundleRequired = XMLCastString(XMLGetProperty(sub, (const char*)"OSBundleRequired"));
Comment = (Comment != NULL && strlen(Comment) >0) ? Comment : "untitled";
MatchOS = (MatchOS != NULL && strlen(MatchOS) >0) ? MatchOS : "";
OSBundleRequired = (OSBundleRequired != NULL && strlen(OSBundleRequired) >0) ? OSBundleRequired : "";
canLoad = true;
if (strlen(MatchOS))
{
// check MatchOS and disable this patch if does not match
canLoad = IsPatchEnabled(MatchOS, (char *)gMacOSVersion);
}
else
{
MatchOS = "not set";
}
if (canLoad)
{
if (XMLIsDict(IOKPTag))
{
char *inj= XMLGenerateKextInjectorFromTag(IOKPTag, kextName, OSBundleRequired);
if (inj != NULL && strlen(inj))
{
verbose("\t- Loading %s.kext (%s) MatchOS[ %s ]: ", kextName, Comment, MatchOS);
verbose("%s!\n", (LoadEmbeddedKext( inj, strlen(inj)+1, kextName ) == 0) ? "success" : "failed");
}
else
{
verbose("\t- Error building %s personality, skipped\n", kextName);
}
}
else
{
verbose("\t- skipping %s personality (%s) MatchOS[ %s ]: not a dictionary\n", kextName, Comment, MatchOS);
}
}
else
{
verbose("\t- skipping %s personality (%s) MatchOS[ %s ]\n", kextName, Comment, MatchOS);
}
}
count--;
}
}
}
//==========================================================================
// LoadEmbeddedKext
long LoadEmbeddedKext( char *InjectorPlist, size_t length, char *name )
{
longexecutablePathLength, bundlePathLength;
ModulePtrmodule;
TagPtrpersonalities;
char*tmpExecutablePath = 0;
char*tmpBundlePath = 0;
char*realAddr = NULL;
longret = -1;
do {
snprintf(gFileSpec, 4096, "%s/%s.kext/%s", "/System/Library/Extensions", name, "Contents/MacOS/");
executablePathLength = strlen(gFileSpec) + 1;
tmpExecutablePath = malloc(executablePathLength);
if (tmpExecutablePath == 0)
{
break;
}
strlcpy(tmpExecutablePath, gFileSpec, executablePathLength);
snprintf(gFileSpec, 4096, "%s/%s.kext", "/System/Library/Extensions", name);
bundlePathLength = strlen(gFileSpec) + 1;
tmpBundlePath = malloc(bundlePathLength);
if (tmpBundlePath == 0)
{
break;
}
strlcpy(tmpBundlePath, gFileSpec, bundlePathLength);
// Construct the file spec to the plist, then load it.
snprintf(gFileSpec, 4096, "%s/%s.kext/Contents/Info.plist", "/System/Library/Extensions", name);
// bug, after ParseXML something weired happened to InjectorPlist. Making a copy
realAddr = malloc(length);
strlcpy(realAddr, InjectorPlist, length);
ret = ParseXML((char *)InjectorPlist, &module, &personalities, true);
if (ret != 0)
{
break;
}
if (!module) // cparm
{
ret = -1;
break;
} // Should never happen but it will make the compiler happy
// Allocate memory for the driver path and the plist.
module->executablePath = tmpExecutablePath;
module->bundlePath = tmpBundlePath;
module->bundlePathLength = bundlePathLength;
module->plistAddr = malloc(length);
if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))
{
verbose("(executablePath or bundlePath or plistAddr are 0) ");
ret = -1;
break;
}
tmpExecutablePath = 0;
tmpBundlePath = 0;
strlcpy(module->plistAddr, realAddr, length);
module->plistLength = length;
// Add the module to the end of the module list.
if (gModuleHead == 0)
{
gModuleHead = module;
}
else
{
gModuleTail->nextModule = module;
}
gModuleTail = module;
// Add the persionalities to the personality list.
if (personalities)
{
personalities = personalities->tag;
}
while (personalities != 0)
{
if (gPersonalityHead == 0)
{
gPersonalityHead = personalities->tag;
}
else
{
gPersonalityTail->tagNext = personalities->tag;
}
gPersonalityTail = personalities->tag;
personalities = personalities->tagNext;
}
ret = 0;
}while (0);
if (realAddr)
{
free(realAddr);
}
if ( strlen(InjectorPlist) )
{
free( InjectorPlist );
}
if ( tmpExecutablePath )
{
free( tmpExecutablePath );
}
if ( tmpBundlePath )
{
free( tmpBundlePath );
}
return ret;
}
//==========================================================================
// LoadDriverPList
long LoadDriverPList( char *dirSpec, char *name, long bundleType )
{
// Parse the plist.
ret = ParseXML(buffer, &module, &personalities);
ret = ParseXML(buffer, &module, &personalities, isKextForcedToLoad(name));
if (ret != 0) {
break;
if ((module->executablePath == 0) || (module->bundlePath == 0) || (module->plistAddr == 0))
{
verbose("%s: executablePath or bundlePath or plistAddr are 0\n", name);
break;
}
#endif /* NOTDEF */
//==========================================================================
// try to force loading kext.
// this not assure the kext will be loaded by the kernel for sure,
// but at least is forced loaded by the bootloader
bool isKextForcedToLoad(char *kext)
{
TagPtr ForceTag = NULL;
if ((ForceTag = XMLCastArray(XMLGetProperty(bootInfo->kextConfig.dictionary, (const char *)"ForceToLoad"))))
{
unsigned long count = XMLTagCount(ForceTag);
if (count) {
for (unsigned i = count ; i-- > 0 ;) /* reversed iteration since xml.c add it reversed */
{
TagPtr index = XMLGetElement( ForceTag, i );
if(XMLIsString(index))
{
char *forced = XMLCastString(index);
char buffer[strlen(forced) + strlen(".kext") +1];
//snprintf(buffer, strlen(forced) + strlen(".kext") +1, "%s%s", forced, ".kext");
sprintf(buffer, "%s%s", forced, ".kext");
if (!strcmp(buffer, kext))
{
verbose("- %s forced loaded\n", kext);
return true;
}
}
}
}
}
return false;
}
//==========================================================================
// ParseXML
static long ParseXML( char *buffer, ModulePtr *module, TagPtr *personalities )
static long ParseXML( char *buffer, ModulePtr *module, TagPtr *personalities, bool forceKextToLoad )
{
longlength;
longpos = 0;
required = XMLGetProperty(moduleDict, kPropOSBundleRequired);
// if forceKextToLoad is true, the kext will be force to load even if OSBundleRequired has value set to "Safe Boot" instead of "Root"
if (!forceKextToLoad) {
if ( (required == 0) || (required->type != kTagTypeString) || !strncmp(required->string, "Safe Boot", sizeof("Safe Boot")))
{
XMLFreeTag(moduleDict);
return -2;
}
}
tmpModule = malloc(sizeof(Module));
if (tmpModule == 0)
branches/ErmaC/Enoch/i386/boot2/boot.c
178178
179179
180180
181
182
183
184
185
186
187
188
181189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
182221
183222
184223
......
909948
910949
911950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
9121000
9131001
9141002
}
}
#if KEXTPATCH_SUPPORT
// =========================================================================
// Load the Kext.plist override config file if any
static void setupKextConfigFile(const char *filename)
{
chardirSpec[128];
const char*override_pathname = NULL;
intlen = 0, err = 0;
AppleRTCPatch = false;
AICPMPatch = false;
OrangeIconFixSata = false;
NVIDIAWebDrv = false;
TrimEnablerSata = false;
// Take in account user overriding
if (getValueForKey(kKEXTKey, &override_pathname, &len, &bootInfo->chameleonConfig) && len > 0)
{
// Specify a path to a file, e.g. KEXTPlist=/Extra/Kext2.plist
strcpy(dirSpec, override_pathname);
err = loadConfigFile(dirSpec, &bootInfo->kextConfig);
} else {
// Check selected volume's Extra.
sprintf(dirSpec, "/Extra/%s", filename);
err = loadConfigFile(dirSpec, &bootInfo->kextConfig);
}
if (!err)
{
getBoolForKey(kAppleRTCPatch, &AppleRTCPatch, KEXTPlist);
getBoolForKey(kAICPMPatch, &AICPMPatch, KEXTPlist);
getBoolForKey(kNVIDIAWebDrv, &NVIDIAWebDrv, KEXTPlist);
getBoolForKey(kOrangeIconFixSata, &OrangeIconFixSata, KEXTPlist);
getBoolForKey(kTrimEnablerSata, &TrimEnablerSata, KEXTPlist);
} else {
verbose("No %s replacement found.\n", filename);
}
}
#endif
//==========================================================================
// ExecKernel - Load the kernel image (mach-o) and jump to its entry point.
}
}
#if KEXTPATCH_SUPPORT
//-------------------------------------- kext patcher
getBoolForKey(kSkipKextsPatcher, &skipKextsPatcher, &bootInfo->chameleonConfig);
// kext patcher
if (!skipKextsPatcher)
{
const char*key = NULL;
intlen = 0;
setupKextConfigFile("kexts.plist");
// looking at command line for kext patcher setting and override where needed
if (getValueForKey(kAICPMPatch, &key, &len, &bootInfo->chameleonConfig) && len > 0)
{
getBoolForKey(kAICPMPatch, &AICPMPatch, &bootInfo->chameleonConfig);
}
key = NULL;
len = 0;
if (getValueForKey(kNVIDIAWebDrv, &key, &len, &bootInfo->chameleonConfig) && len > 0)
{
getBoolForKey(kNVIDIAWebDrv, &NVIDIAWebDrv, &bootInfo->chameleonConfig);
}
key = NULL;
len = 0;
if (getValueForKey(kOrangeIconFixSata, &key, &len, &bootInfo->chameleonConfig) && len > 0)
{
getBoolForKey(kOrangeIconFixSata, &OrangeIconFixSata, &bootInfo->chameleonConfig);
}
key = NULL;
len = 0;
if (getValueForKey(kTrimEnablerSata, &key, &len, &bootInfo->chameleonConfig) && len > 0)
{
getBoolForKey(kTrimEnablerSata, &TrimEnablerSata, &bootInfo->chameleonConfig);
}
key = NULL;
len = 0;
if (getValueForKey(kAppleRTCPatch, &key, &len, &bootInfo->chameleonConfig) && len > 0)
{
getBoolForKey(kAppleRTCPatch, &AppleRTCPatch, &bootInfo->chameleonConfig);
}
}
// -------------------------------------- kext patcher
#endif
if (gBootVolume->OSisInstaller)
{
isInstaller = true;

Archive Download the corresponding diff file

Revision: 2872