From 2ec0ee47941f81bf263fad79d4661daa57408462 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Sun, 10 Dec 2023 20:47:43 -0500 Subject: [PATCH] update procaddr hook --- cxbhook/led.c | 4 +- cxbhook/revio.c | 4 +- hooklib/meson.build | 2 - hooklib/procaddr.c | 130 -------------------------------------- hooklib/procaddr.h | 20 ------ hooklib/reg.c | 3 +- mai2hook/dllmain.c | 2 +- mai2hook/touch.c | 61 ++++++++++-------- mai2hook/unity.c | 2 +- subprojects/capnhook.wrap | 2 +- 10 files changed, 42 insertions(+), 188 deletions(-) delete mode 100644 hooklib/procaddr.c delete mode 100644 hooklib/procaddr.h diff --git a/cxbhook/led.c b/cxbhook/led.c index 39d6d4f..ae35aed 100644 --- a/cxbhook/led.c +++ b/cxbhook/led.c @@ -5,7 +5,7 @@ #include "cxbhook/led.h" #include "cxbhook/cxb-dll.h" -#include "hooklib/procaddr.h" +#include "hook/procaddr.h" #include "hook/table.h" @@ -50,7 +50,7 @@ static struct hook_symbol lamp_syms[] = { HRESULT led_hook_init(struct led_config *cfg) { dprintf("LED: Init\n"); - return proc_addr_table_push("CommLamp.dll", lamp_syms, _countof(lamp_syms)); + return proc_addr_table_push(NULL, "CommLamp.dll", lamp_syms, _countof(lamp_syms)); } static int my_cCommLamp_Open(char *port) diff --git a/cxbhook/revio.c b/cxbhook/revio.c index e72158d..a398bfa 100644 --- a/cxbhook/revio.c +++ b/cxbhook/revio.c @@ -6,7 +6,7 @@ #include "cxbhook/revio.h" #include "cxbhook/cxb-dll.h" -#include "hooklib/procaddr.h" +#include "hook/procaddr.h" #include "hook/table.h" @@ -83,7 +83,7 @@ static struct hook_symbol revio_syms[] = { HRESULT revio_hook_init(struct revio_config *cfg) { dprintf("Revio: Init\n"); - return proc_addr_table_push("CommIo.dll", revio_syms, _countof(revio_syms)); + return proc_addr_table_push(NULL, "CommIo.dll", revio_syms, _countof(revio_syms)); } static int my_cCommIo_Open(char *port) diff --git a/hooklib/meson.build b/hooklib/meson.build index 4ff4734..0a69e9f 100644 --- a/hooklib/meson.build +++ b/hooklib/meson.build @@ -23,8 +23,6 @@ hooklib_lib = static_library( 'fdshark.h', 'path.c', 'path.h', - 'procaddr.c', - 'procaddr.h', 'reg.c', 'reg.h', 'setupapi.c', diff --git a/hooklib/procaddr.c b/hooklib/procaddr.c deleted file mode 100644 index c29471f..0000000 --- a/hooklib/procaddr.c +++ /dev/null @@ -1,130 +0,0 @@ -#include -#include -#include -#include - -#include "hooklib/procaddr.h" - -#include "hook/table.h" - -#include "util/dprintf.h" - -static struct proc_addr_table *proc_addr_hook_list; -static size_t proc_addr_hook_count; -static CRITICAL_SECTION proc_addr_hook_lock; -static bool proc_addr_hook_initted; - -static FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name); -static FARPROC (WINAPI *next_GetProcAddress)(HMODULE hModule, const char *name); -static void proc_addr_hook_init(void); - -static const struct hook_symbol win32_hooks[] = { - { - .name = "GetProcAddress", - .patch = my_GetProcAddress, - .link = (void **) &next_GetProcAddress - } -}; - -HRESULT proc_addr_table_push( - const char *target, - struct hook_symbol *syms, - size_t nsyms -) -{ - HRESULT hr; - struct proc_addr_table *new_item; - struct proc_addr_table *new_mem; - - proc_addr_hook_init(); - - EnterCriticalSection(&proc_addr_hook_lock); - - new_mem = realloc( - proc_addr_hook_list, - (proc_addr_hook_count + 1) * sizeof(struct proc_addr_table)); - - if (new_mem == NULL) { - hr = E_OUTOFMEMORY; - - LeaveCriticalSection(&proc_addr_hook_lock); - return hr; - } - - new_item = &new_mem[proc_addr_hook_count]; - new_item->name = target; - new_item->nsyms = nsyms; - new_item->syms = syms; - - proc_addr_hook_list = new_mem; - proc_addr_hook_count++; - hr = S_OK; - - LeaveCriticalSection(&proc_addr_hook_lock); - - return hr; -} - -static void proc_addr_hook_init(void) -{ - if (proc_addr_hook_initted) { - return; - } - - dprintf("ProcAddr: Hook init\n"); - proc_addr_hook_initted = true; - - InitializeCriticalSection(&proc_addr_hook_lock); - - proc_addr_insert_hooks(NULL); -} - -void proc_addr_insert_hooks(HMODULE target) -{ - hook_table_apply( - target, - "kernel32.dll", - win32_hooks, - _countof(win32_hooks)); -} - -FARPROC WINAPI my_GetProcAddress(HMODULE hModule, const char *name) -{ - uintptr_t ordinal = (uintptr_t) name; - char mod_path[PATH_MAX]; - char *mod_name; - const struct hook_symbol *sym; - FARPROC result = next_GetProcAddress(hModule, name); - - GetModuleFileNameA(hModule, mod_path, PATH_MAX); - mod_name = basename(mod_path); - - for (int i = 0; i < proc_addr_hook_count; i++) { - - if (strcmp(proc_addr_hook_list[i].name, mod_name) == 0) { - - for (int j = 0; j < proc_addr_hook_list[i].nsyms; j++) { - sym = &proc_addr_hook_list[i].syms[j]; - - if (ordinal > 0xFFFF) { - - if (strcmp(sym->name, name) == 0) { - - dprintf("ProcAddr: Hooking %s from %s\n", name, mod_name); - result = (FARPROC) sym->patch; - } - } - - else { - if (sym->ordinal == ordinal) { - - dprintf("ProcAddr: Hooking Ord %p from %s\n", (void *)ordinal, mod_name); - result = (FARPROC) sym->patch; - } - } - } - } - } - - return result; -} \ No newline at end of file diff --git a/hooklib/procaddr.h b/hooklib/procaddr.h deleted file mode 100644 index ce72940..0000000 --- a/hooklib/procaddr.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include -#include -#include - -#include "hook/table.h" - -struct proc_addr_table { - const char *name; - size_t nsyms; - struct hook_symbol *syms; -}; - -HRESULT proc_addr_table_push( - const char *target, - struct hook_symbol *syms, - size_t nsyms -); - -void proc_addr_insert_hooks(HMODULE target); diff --git a/hooklib/reg.c b/hooklib/reg.c index ddda6e9..bc18dbb 100644 --- a/hooklib/reg.c +++ b/hooklib/reg.c @@ -7,7 +7,7 @@ #include "hook/table.h" #include "hooklib/reg.h" -#include "hooklib/procaddr.h" +#include "hook/procaddr.h" #include "util/dprintf.h" #include "util/str.h" @@ -313,6 +313,7 @@ static void reg_hook_init(void) reg_hook_insert_hooks(NULL); proc_addr_table_push( + NULL, "ADVAPI32.dll", (struct hook_symbol *) reg_hook_syms, _countof(reg_hook_syms)); diff --git a/mai2hook/dllmain.c b/mai2hook/dllmain.c index c93873b..58abd61 100644 --- a/mai2hook/dllmain.c +++ b/mai2hook/dllmain.c @@ -18,7 +18,7 @@ #include "hooklib/spike.h" #include "hooklib/path.h" #include "hooklib/reg.h" -#include "hooklib/procaddr.h" +#include "hook/procaddr.h" #include "hooklib/serial.h" #include "mai2hook/config.h" diff --git a/mai2hook/touch.c b/mai2hook/touch.c index 9a5131c..3241655 100644 --- a/mai2hook/touch.c +++ b/mai2hook/touch.c @@ -10,6 +10,7 @@ #include "mai2hook/mai2-dll.h" #include "util/dprintf.h" +#include "util/dump.h" const char CMD_START = '{'; const char CMD_END = '}'; @@ -26,6 +27,7 @@ const char CMD_SENS_CHECK[2] = "th"; const char CMD_RESET[7] = "{RSET}"; // Reset board to default state const char CMD_STAT[7] = "{STAT}"; // Start sending touch state const char CMD_HALT[7] = "{HALT}"; // Stop sending touch state +const char RSP_ANY[6] = "(0000)"; // Stop sending touch state static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes); static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes); @@ -33,6 +35,7 @@ static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes); static HRESULT touch_handle_irp(struct irp *irp); static HRESULT touch0_handle_irp_locked(struct irp *irp); static HRESULT touch1_handle_irp_locked(struct irp *irp); +static HRESULT touch_cmd_dispatch(char* cmd, struct iobuf *dest, uint8_t side); bool touch0_auto = false; bool touch1_auto = false; @@ -149,7 +152,7 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp) //hr = mai2_dll.touch_init(); if (FAILED(hr)) { - dprintf("Mai2 touch: Backend error: %x\n", (int) hr); + dprintf("Mai2 touch0: Backend error: %x\n", (int) hr); return hr; } @@ -163,24 +166,16 @@ static HRESULT touch0_handle_irp_locked(struct irp *irp) for (;;) { #if 0 - dprintf("TX0 Buffer:\n"); + dprintf("touch0 Buffer:\n"); dump_iobuf(&touch0_uart.written); #endif - //hr = touch_frame_decode(&req, &touch0_uart.written, 0); - - if (hr != S_OK) { - if (FAILED(hr)) { - dprintf("Mai2 touch: Deframe error: %x\n", (int) hr); - } - + hr = touch_cmd_dispatch((char*)touch0_uart.written.bytes, &touch0_uart.readable, 0); + if (FAILED(hr)) { + dprintf("Mai2 touch0: Dispatch failed %08lX\n", hr); return hr; } - //hr = touch_req_dispatch(&req); - - if (FAILED(hr)) { - dprintf("Mai2 touch: Processing error: %x\n", (int) hr); - } + touch0_uart.written.pos = 0; return hr; } @@ -195,13 +190,13 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp) //hr = mai2_dll.touch_init(); if (FAILED(hr)) { - dprintf("Mai2 touch: Backend error: %x\n", (int) hr); + dprintf("Mai2 touch1: Backend error: %x\n", (int) hr); return hr; } } - hr = uart_handle_irp(&touch0_uart, irp); + hr = uart_handle_irp(&touch1_uart, irp); if (FAILED(hr) || irp->op != IRP_OP_WRITE) { return hr; @@ -209,25 +204,35 @@ static HRESULT touch1_handle_irp_locked(struct irp *irp) for (;;) { #if 0 - dprintf("TX0 Buffer:\n"); + dprintf("touch1 Buffer:\n"); dump_iobuf(&touch0_uart.written); #endif - //hr = touch_frame_decode(&req, &touch0_uart.written, 0); - - if (hr != S_OK) { - if (FAILED(hr)) { - dprintf("Mai2 touch: Deframe error: %x\n", (int) hr); - } + hr = touch_cmd_dispatch((char*)touch1_uart.written.bytes, &touch1_uart.readable, 1); + if (FAILED(hr)) { + dprintf("Mai2 touch1: Dispatch failed %08lX\n", hr); return hr; } - //hr = touch_req_dispatch(&req); - - if (FAILED(hr)) { - dprintf("Mai2 touch: Processing error: %x\n", (int) hr); - } + touch1_uart.written.pos = 0; return hr; } +} + +static HRESULT touch_cmd_dispatch(char* cmd, struct iobuf *dest, uint8_t side) +{ + if (!strcmp(cmd, CMD_RESET)) { + dprintf("Mai2 touch%d: Reset\n", side); + return S_OK; + } + + else if (!strcmp(cmd, CMD_HALT)) { + dprintf("Mai2 touch%d: Halt\n", side); + Sleep(1001); // ? + return S_OK; + } + + dprintf("Mai2 touch%d: Unknow %s\n", side, cmd); + return S_OK; } \ No newline at end of file diff --git a/mai2hook/unity.c b/mai2hook/unity.c index 1f6137b..4c82b07 100644 --- a/mai2hook/unity.c +++ b/mai2hook/unity.c @@ -8,7 +8,7 @@ #include "hooklib/dll.h" #include "hooklib/path.h" #include "hooklib/reg.h" -#include "hooklib/procaddr.h" +#include "hook/procaddr.h" #include "hooklib/serial.h" #include "util/dprintf.h" diff --git a/subprojects/capnhook.wrap b/subprojects/capnhook.wrap index c62f130..b2687a2 100644 --- a/subprojects/capnhook.wrap +++ b/subprojects/capnhook.wrap @@ -1,4 +1,4 @@ [wrap-git] directory = capnhook url = https://github.com/Hay1tsme/capnhook -revision = 888d068d58e68cf702e0cee872959a71413a7b55 +revision = dbdcd61b3a3043b08f86f959bd45df4967503a77