Skip to content

Commit

Permalink
Do user stuff after graftdmg instead of serial_keyboard_init
Browse files Browse the repository at this point in the history
serial_keyboard_init is too early for Ventura. graftdmg happens soon after serial_keyboard_init and /System/Volumes/Preboot should be mounted at this time so that the dyld shared cache map can be read successfully.
  • Loading branch information
joevt committed Jul 18, 2022
1 parent d78c66c commit ea2c884
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Lilu/PrivateHeaders/kern_config.hpp
Expand Up @@ -126,7 +126,8 @@ class Configuration {
static int wrap_serial_init( void ); // for initializing serial port output in case macOS doesn't do it automatically
static void wrap_console_write(char *str, int size); // for outputing IOLog and printf to kprintf
static void wrap_console_printbuf_putc(int ch, void * arg); // for outputing IOLog and printf to kprintf
static void wrap_serial_keyboard_init(void); // for signaling Lilu when rootvnode is available for reading files
static void wrap_serial_keyboard_init(void); // for signaling Lilu when rootvnode / is available for reading files such as the dyld shared cache map
static int wrap_graftdmg(proc_t p, graftdmg_args* uap, int32_t* retval); // For Ventura, for reading dyld shared cache map - /System/Volumes/Preboot is mounted at this time

#if defined(__x86_64__)
/**
Expand All @@ -151,6 +152,7 @@ class Configuration {
mach_vm_address_t org_console_write {0};
mach_vm_address_t org_console_printbuf_putc {0};
mach_vm_address_t org_serial_keyboard_init {0};
mach_vm_address_t org_graftdmg {0};

#ifdef DEBUG
/**
Expand Down
29 changes: 24 additions & 5 deletions Lilu/Sources/kern_start.cpp
Expand Up @@ -188,7 +188,7 @@ We can affect the start time of Lilu::start by changing IOResourceMatch in Info.
- IOResourceMatch "IOBSD" is too early to have rootvnode (required for UserPatcher::loadFilesForPatching)
- IOResourceMatch "boot-uuid-media" is also too early
- IOResourceMatch "IOConsoleUsers" is too late - WindowServer has already loaded
To solve this, we trap serial_keyboard_init in performCommonInit - it happens very early but not too early; rootvnode will have been initialized by bsd_init by that time.
To solve this, we trap serial_keyboard_init or graftdmg in performCommonInit - it happens very early but not too early; rootvnode will have been initialized by bsd_init by that time.
*/

static bool userReady = false;
Expand All @@ -198,10 +198,26 @@ static void ** rootvnodePtr = NULL; // set before wrap_serial_keyboard_init is c
void Configuration::wrap_serial_keyboard_init(void) {
DBGLOG("config", "[ Configuration::wrap_serial_keyboard_init");
FunctionCast(wrap_serial_keyboard_init, ADDPR(config).org_serial_keyboard_init)();

if (!ADDPR(config).org_graftdmg) {
IOLockLock(ADDPR(config).policyLock);
ADDPR(config).processUserLoadCallbacks();
IOLockUnlock(ADDPR(config).policyLock);
}

DBGLOG("config", "] Configuration::wrap_serial_keyboard_init");
}

int Configuration::wrap_graftdmg(proc_t p, graftdmg_args* uap, int32_t* retval) {
DBGLOG("config", "[ Configuration::wrap_graftdmg");
int result = FunctionCast(wrap_graftdmg, ADDPR(config).org_graftdmg)(p, uap, retval);

IOLockLock(ADDPR(config).policyLock);
ADDPR(config).processUserLoadCallbacks();
IOLockUnlock(ADDPR(config).policyLock);
DBGLOG("config", "] Configuration::wrap_serial_keyboard_init");

DBGLOG("config", "] Configuration::wrap_graftdmg result:%d", result);
return result;
}

void Configuration::processUserLoadCallbacks() {
Expand All @@ -221,9 +237,12 @@ void Configuration::processUserLoadCallbacks() {
bool Configuration::performCommonInit() {
DBGLOG("config", "[ Configuration::performCommonInit");

KernelPatcher::RouteRequest request {"_serial_keyboard_init", wrap_serial_keyboard_init, org_serial_keyboard_init};
if (!kernelPatcher.routeMultiple(KernelPatcher::KernelID, &request, 1, 0, 0, true, false)) {
SYSLOG("config", "failed to patch serial_keyboard_init for user patching");
KernelPatcher::RouteRequest requests[] = {
{"_serial_keyboard_init", wrap_serial_keyboard_init, org_serial_keyboard_init},
{"_graftdmg", wrap_graftdmg, org_graftdmg}
};
if (!kernelPatcher.routeMultiple(KernelPatcher::KernelID, requests, arrsize(requests), 0, 0, true, false)) {
SYSLOG("config", "failed to patch serial_keyboard_init and graftdmg for user patching");
kernelPatcher.clearError();
}

Expand Down

0 comments on commit ea2c884

Please sign in to comment.