Index: branches/meklort/i386/libsaio/stringTable.c =================================================================== --- branches/meklort/i386/libsaio/stringTable.c (revision 363) +++ branches/meklort/i386/libsaio/stringTable.c (revision 364) @@ -610,9 +610,6 @@ "/Extra/com.apple.Boot.plist", "bt(0,0)/Extra/com.apple.Boot.plist", "/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", - "/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", - "/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", - "/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist" }; int i, fd, count, ret=-1; @@ -636,6 +633,11 @@ break; } } + if(ret == -1) + { + ret = loadHelperConfig(config); + } + return ret; } @@ -650,9 +652,6 @@ "rd(0,0)/Extra/com.apple.Boot.plist", "/Extra/com.apple.Boot.plist", "/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", - "/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", - "/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", - "/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist" }; int i, fd, count, ret=-1; @@ -672,6 +671,11 @@ break; } } + + if(ret == -1) + { + ret = loadHelperConfig(config); + } return ret; } @@ -682,29 +686,99 @@ */ int loadHelperConfig(config_file_t *config) { + int rfd, pfd, sfd, count, ret=-1; + char *dirspec[] = { "/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", "/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist", "/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist" }; - - int i, fd, count, ret=-1; - - for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) + + // This is a simple rock - paper scissors algo. R beats S, P beats R, S beats P + // If all three, S is used for now. This should be change dto something else (say, timestamp?) + + pfd = open(dirspec[0], 0); + if(pfd >= 0) // com.apple.boot.P exists { - if ((fd = open(dirspec[i], 0)) >= 0) + sfd = open(dirspec[2], 0); // com.apple.boot.S takes precidence if it also exists + if(sfd >= 0) { - // read file - count = read(fd, config->plist, IO_CONFIG_DATA_SIZE); - close(fd); + // Use sfd + count = read(sfd, config->plist, IO_CONFIG_DATA_SIZE); + close(sfd); + close(pfd); // build xml dictionary ParseXMLFile(config->plist, &config->dictionary); sysConfigValid = true; ret=0; - break; + } + else + { + // used pfd + count = read(pfd, config->plist, IO_CONFIG_DATA_SIZE); + close(pfd); + + // build xml dictionary + ParseXMLFile(config->plist, &config->dictionary); + sysConfigValid = true; + ret=0; + } + } + else + { + rfd = open(dirspec[1], 0); // com.apple.boot.R exists + if(rfd >= 0) + { + pfd = open(dirspec[2], 0); // com.apple.boot.P takes recidence if it exists + if(pfd >= 0) + { + // use sfd + count = read(pfd, config->plist, IO_CONFIG_DATA_SIZE); + close(pfd); + close(rfd); + + // build xml dictionary + ParseXMLFile(config->plist, &config->dictionary); + sysConfigValid = true; + ret=0; + + } + else + { + // use rfd + count = read(rfd, config->plist, IO_CONFIG_DATA_SIZE); + close(rfd); + + // build xml dictionary + ParseXMLFile(config->plist, &config->dictionary); + sysConfigValid = true; + ret=0; + + } + + } + else + { + sfd = open(dirspec[2], 0); // com.apple.boot.S exists, but nothing else does + if(sfd >= 0) + { + // use sfd + count = read(sfd, config->plist, IO_CONFIG_DATA_SIZE); + close(sfd); + + // build xml dictionary + ParseXMLFile(config->plist, &config->dictionary); + sysConfigValid = true; + ret=0; + + } + } + + } + return ret; } Index: branches/meklort/i386/boot2/boot.c =================================================================== --- branches/meklort/i386/boot2/boot.c (revision 363) +++ branches/meklort/i386/boot2/boot.c (revision 364) @@ -286,12 +286,11 @@ loadPrebootRAMDisk(); // Intialize module system - load_module(SYMBOLS_MODULE); - lookup_symbol = (void*)lookup_all_symbols("_lookup_symbol"); - - // Load a module - load_module("KernelPatcher"); - + if(init_module_system()) + { + load_all_modules(); + } + // Disable rescan option by default gEnableCDROMRescan = false; Index: branches/meklort/i386/modules/KernelPatcher/kernel_patcher.c =================================================================== --- branches/meklort/i386/modules/KernelPatcher/kernel_patcher.c (revision 363) +++ branches/meklort/i386/modules/KernelPatcher/kernel_patcher.c (revision 364) @@ -1,4 +1,4 @@ -/* + /* * Copyright (c) 2009 Evan Lojewski. All rights reserved. * */ @@ -6,81 +6,116 @@ #include "libsaio.h" #include "kernel_patcher.h" #include "platform.h" - extern PlatformInfo_t Platform; +patchRoutine_t* patches = NULL; +kernSymbols_t* kernelSymbols = NULL; -#define SYMBOL_CPUID_SET_INFO 0 -#define SYMBOL_PANIC 1 -#define SYMBOL_PMCPUEXITHALTTOOFF 2 -#define SYMBOL_LAPIC_INIT 3 -#define SYMBOL_COMMPAGE_STUFF_ROUTINE 4 -#define NUM_SYMBOLS 5 -#define SYMBOL_CPUID_SET_INFO_STRING "_cpuid_set_info" -#define SYMBOL_PANIC_STRING "_panic" -#define SYMBOL_PMCPUEXITHALTTOOFF_STRING "_pmCPUExitHaltToOff" -#define SYMBOL_LAPIC_INIT_STRING "_lapic_init" -#define SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING "_commpage_stuff_routine" -char* kernelSymbols[NUM_SYMBOLS] = { - SYMBOL_CPUID_SET_INFO_STRING, - SYMBOL_PANIC_STRING, - SYMBOL_PMCPUEXITHALTTOOFF_STRING, - SYMBOL_LAPIC_INIT_STRING, - SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING -}; - -UInt32 kernelSymbolAddresses[NUM_SYMBOLS] = { - 0, - 0, - 0, - 0, - 0 -}; - - UInt32 textSection = 0; UInt32 textAddress = 0; -void HelloWorld_start(); - void KernelPatcher_start() { - printf("KernelPatcher(), about to call HelloWorld_start()\n"); - getc(); - HelloWorld_start(); + register_kernel_patch(patch_cpuid_set_info, KERNEL_32, CPUID_MODEL_ATOM); + register_kernel_patch(patch_cpuid_set_info, KERNEL_32, CPUID_MODEL_UNKNOWN); + register_kernel_patch(patch_commpage_stuff_routine, KERNEL_32, CPUID_MODEL_ANY); + + register_kernel_patch(patch_lapic_init, KERNEL_32, CPUID_MODEL_ANY); + + // TODO: register needed symbols + + + // TODO: Hook main kernel patcher loop into chameleon } +/* + * Register a kerenl patch + * TODO: chang efunction prototype to include patch argument + */ +void register_kernel_patch(void* patch, int arch, int cpus) +{ + // TODO: only insert valid patches based on current cpuid and architecture + // AKA, don't at 64bit patches if it's a 32bit only machine + patchRoutine_t* entry; + + // TODO: verify Platform.CPU.Model is populated this early in bootup + if(cpus != Platform.CPU.Model) + { + if(cpus != CPUID_MODEL_ANY) + { + if(cpus == CPUID_MODEL_UNKNOWN) + { + switch(Platform.CPU.Model) + { + case 13: + case CPUID_MODEL_YONAH: + case CPUID_MODEL_MEROM: + case CPUID_MODEL_PENRYN: + case CPUID_MODEL_NEHALEM: + case CPUID_MODEL_FIELDS: + case CPUID_MODEL_DALES: + case CPUID_MODEL_NEHALEM_EX: + break; + default: + // CPU not in supported list.s + return; + + } + } + else + { + // Incalid cpuid for current cpu. Ignoring patch + return; + } + } + } + + // Check arch + + if(patches == NULL) + { + patches = entry = malloc(sizeof(patchRoutine_t)); + } + else + { + entry = patches; + while(entry->next) + { + entry = entry->next; + } + + entry->next = malloc(sizeof(patchRoutine_t)); + entry = entry->next; + } + + entry->next = NULL; + entry->patchRoutine = patch; + entry->validArchs = arch; + entry->validCpu = cpus; +} + +void* lookup_kernel_symbol(const char* name) +{ + return NULL; +} + + void patch_kernel(void* kernelData) { switch (locate_symbols((void*)kernelData)) { case KERNEL_32: patch_kernel_32((void*)kernelData); break; - - case KERNEL_64: - default: - patch_kernel_64((void*)kernelData); - break; } } -// patches a 64bit kernel. -void patch_kernel_64(void* kernelData) -{ - // At the moment, the kernel patching code fails when used - // in 64bit mode, so we don't patch it. This is due to 32bit vs 64bit - // pointers as well as changes in structure sizes - printf("Unable to patch 64bit kernel. Please use arch=i386.\n"); -} - - /** ** patch_kernel_32 ** patches kernel based on cpu info determined earlier in the boot process. @@ -135,11 +170,10 @@ **/ int locate_symbols(void* kernelData) { - UInt16 symbolIndexes[NUM_SYMBOLS]; struct load_command *loadCommand; struct symtab_command *symtableData; - struct nlist *symbolEntry; + // struct nlist *symbolEntry; char* symbolString; @@ -179,10 +213,10 @@ symbolString = kernelData + symtableData->stroff; - UInt16 symbolIndex = 0; - UInt8 numSymbolsFound = 0; + //UInt16 symbolIndex = 0; + //UInt8 numSymbolsFound = 0; - while(symbolIndex < symtableData->nsyms && numSymbolsFound < NUM_SYMBOLS) // TODO: for loop + /*while(symbolIndex < symtableData->nsyms && numSymbolsFound < NUM_SYMBOLS) // TODO: for loop { int i = 0; while(i < NUM_SYMBOLS) @@ -220,7 +254,8 @@ } symbolIndex ++; - } + } + */ // Load commands should be anded with 0x7FFFFFFF to ignore the LC_REQ_DYLD flag } else if((loadCommand->cmd & 0x7FFFFFFF) == LC_SEGMENT) // We only care about the __TEXT segment, any other load command can be ignored { @@ -274,16 +309,19 @@ void patch_cpuid_set_info(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel) { UInt8* bytes = (UInt8*)kernelData; - UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] - textAddress + textSection); + UInt32 patchLocation = (UInt32)lookup_kernel_symbol("_cpuid_set_info"); + + // (kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] - textAddress + textSection); UInt32 jumpLocation = 0; - UInt32 panicAddr = kernelSymbolAddresses[SYMBOL_PANIC] - textAddress; - if(kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] == 0) + UInt32 panicAddr = (UInt32)lookup_kernel_symbol("_panic"); + //kernelSymbolAddresses[SYMBOL_PANIC] - textAddress; + if(patchLocation == 0) { printf("Unable to locate _cpuid_set_info\n"); return; } - if(kernelSymbolAddresses[SYMBOL_PANIC] == 0) + if(panicAddr == 0) { printf("Unable to locate _panic\n"); return; @@ -445,9 +483,11 @@ void patch_pmCPUExitHaltToOff(void* kernelData) { UInt8* bytes = (UInt8*)kernelData; - UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_PMCPUEXITHALTTOOFF] - textAddress + textSection); + UInt32 patchLocation = lookup_kernel_symbol("_PmCpuExitHaltToOff"); // verify - if(kernelSymbolAddresses[SYMBOL_PMCPUEXITHALTTOOFF] == 0) + //(kernelSymbolAddresses[SYMBOL_PMCPUEXITHALTTOOFF] - textAddress + textSection); + + if(patchLocation == 0) { printf("Unable to locate _pmCPUExitHaltToOff\n"); return; @@ -469,18 +509,18 @@ { UInt8 panicIndex = 0; UInt8* bytes = (UInt8*)kernelData; - UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_LAPIC_INIT] - textAddress + textSection); - UInt32 panicAddr = kernelSymbolAddresses[SYMBOL_PANIC] - textAddress; + UInt32 patchLocation = 0x00; // (kernelSymbolAddresses[SYMBOL_LAPIC_INIT] - textAddress + textSection); + UInt32 panicAddr = 0x00; //kernelSymbolAddresses[SYMBOL_PANIC] - textAddress; - if(kernelSymbolAddresses[SYMBOL_LAPIC_INIT] == 0) + //if(kernelSymbolAddresses[SYMBOL_LAPIC_INIT] == 0) { - printf("Unable to locate %s\n", SYMBOL_LAPIC_INIT_STRING); + //printf("Unable to locate %s\n", SYMBOL_LAPIC_INIT_STRING); return; } - if(kernelSymbolAddresses[SYMBOL_PANIC] == 0) + //if(kernelSymbolAddresses[SYMBOL_PANIC] == 0) { - printf("Unable to locate %s\n", SYMBOL_PANIC_STRING); + //printf("Unable to locate %s\n", SYMBOL_PANIC_STRING); return; } @@ -517,18 +557,18 @@ void patch_commpage_stuff_routine(void* kernelData) { UInt8* bytes = (UInt8*)kernelData; - UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_COMMPAGE_STUFF_ROUTINE] - textAddress + textSection); - UInt32 panicAddr = kernelSymbolAddresses[SYMBOL_PANIC] - textAddress; + UInt32 patchLocation = 0x00; // (kernelSymbolAddresses[SYMBOL_COMMPAGE_STUFF_ROUTINE] - textAddress + textSection); + UInt32 panicAddr = 0x00;// kernelSymbolAddresses[SYMBOL_PANIC] - textAddress; - if(kernelSymbolAddresses[SYMBOL_COMMPAGE_STUFF_ROUTINE] == 0) + //if(kernelSymbolAddresses[SYMBOL_COMMPAGE_STUFF_ROUTINE] == 0) { - printf("Unable to locate %s\n", SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING); + // printf("Unable to locate %s\n", SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING); return; } - if(kernelSymbolAddresses[SYMBOL_PANIC] == 0) + //if(kernelSymbolAddresses[SYMBOL_PANIC] == 0) { - printf("Unable to locate %s\n", SYMBOL_PANIC_STRING); + // printf("Unable to locate %s\n", SYMBOL_PANIC_STRING); return; } Index: branches/meklort/i386/modules/KernelPatcher/kernel_patcher.h =================================================================== --- branches/meklort/i386/modules/KernelPatcher/kernel_patcher.h (revision 363) +++ branches/meklort/i386/modules/KernelPatcher/kernel_patcher.h (revision 364) @@ -9,6 +9,8 @@ #ifndef __BOOT2_KERNEL_PATCHER_H #define __BOOT2_KERNEL_PATCHER_H +#define CPUID_MODEL_ANY 0x00 +#define CPUID_MODEL_UNKNOWN 0x01 #define CPUID_MODEL_YONAH 14 #define CPUID_MODEL_MEROM 15 @@ -19,16 +21,35 @@ #define CPUID_MODEL_DALES 31 /* Havendale, Auburndale */ #define CPUID_MODEL_NEHALEM_EX 46 +#define KERNEL_ANY 0x00 +#define KERNEL_64 0x01 +#define KERNEL_32 0x02 + +typedef struct patchRoutine_t +{ + void* patchRoutine; + int validArchs; + int validCpu; + struct patchRoutine_t* next; +} patchRoutine_t; + + +typedef struct kernSymbols_t +{ + char* symbol; + void* symbolAddress; + struct kernSymbols_t* next; +} kernSymbols_t; + +void* lookup_kernel_symbol(const char* name); + void patch_kernel(void* kernelData); +void register_kernel_patch(void* patch, int arch, int cpus); -#define KERNEL_64 1 -#define KERNEL_32 2 - int locate_symbols(void* kernelData); void patch_kernel_32(void* kernelData); -void patch_kernel_64(void* kernelData);