From 257dbc1c2200ff5d5b88850b7b832c59735387f0 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Thu, 16 Mar 2023 01:45:39 -0400 Subject: [PATCH] push work on reader --- board/bpreader.c | 144 +++++++++++++++++++++++++++++++++++- board/bpreader.h | 7 +- dist/ferrum/bananatools.ini | 4 + dist/taiko/bananatools.ini | 4 + ferrumhook/config.c | 12 +-- ferrumhook/config.h | 8 +- ferrumhook/dllmain.c | 4 +- ferrumhook/meson.build | 3 +- ferrumhook/network.c | 66 ----------------- ferrumhook/network.h | 10 --- platform/jvs.c | 12 +-- taikohook/bngrw.c | 0 taikohook/bngrw.h | 0 taikohook/config.c | 1 + taikohook/config.h | 2 + taikohook/meson.build | 3 + 16 files changed, 170 insertions(+), 110 deletions(-) delete mode 100644 ferrumhook/network.c delete mode 100644 ferrumhook/network.h create mode 100644 taikohook/bngrw.c create mode 100644 taikohook/bngrw.h diff --git a/board/bpreader.c b/board/bpreader.c index e87c582..2fa61e2 100644 --- a/board/bpreader.c +++ b/board/bpreader.c @@ -2,16 +2,152 @@ #include #include +#include "hook/iobuf.h" +#include "hook/iohook.h" + +#include "hooklib/uart.h" +#include "hooklib/fdshark.h" + +#include "util/dprintf.h" +#include "util/dump.h" + #include "board/bpreader.h" -static struct bpreader_config config; +static HRESULT bp_handle_irp(struct irp *irp); +static HRESULT bp_handle_irp_locked(struct irp *irp); -HRESULT bpreader_init(uint16_t port) +static struct bpreader_config *config; +static struct uart bp_uart; +static CRITICAL_SECTION bp_lock; +static uint8_t bp_written_bytes[520]; +static uint8_t bp_readable_bytes[520]; +static uint8_t last_cmd = 0; +static uint16_t write_ct = 0; + +HRESULT bpreader_init(struct bpreader_config *cfg, uint16_t port) { - return S_OK; + config = cfg; + if (!config->enable) { + return S_OK; + } + if (cfg->port < 0) { + port = cfg->port; + } + + uart_init(&bp_uart, port); + bp_uart.written.bytes = bp_written_bytes; + bp_uart.written.nbytes = sizeof(bp_written_bytes); + bp_uart.readable.bytes = bp_readable_bytes; + bp_uart.readable.nbytes = sizeof(bp_readable_bytes); + InitializeCriticalSection(&bp_lock); + + dprintf("Reader: Init\n"); + return iohook_push_handler(bp_handle_irp); } -void bpreader_congif_init(struct bpreader_config *cfg) +void bpreader_congif_load(struct bpreader_config *cfg, const wchar_t *filename) { + assert(cfg != NULL); + assert(filename != NULL); + cfg->enable = GetPrivateProfileIntW(L"reader", L"enable", 1, filename); + cfg->port = GetPrivateProfileIntW(L"reader", L"port", 0, filename); + GetPrivateProfileStringW( + L"reader", + L"access_code", + L"", + cfg->access_code, + _countof(cfg->access_code), + filename); +} + +static HRESULT bp_handle_irp(struct irp *irp) +{ + HRESULT hr; + + assert(irp != NULL); + + if (uart_match_irp(&bp_uart, irp)) { + EnterCriticalSection(&bp_lock); + hr = bp_handle_irp_locked(irp); + LeaveCriticalSection(&bp_lock); + } + else { + return iohook_invoke_next(irp); + } + + return hr; +} + +static HRESULT bp_handle_irp_locked(struct irp *irp) +{ + HRESULT hr; + if (irp->op == IRP_OP_OPEN) { + dprintf("BNG Reader: Starting backend\n"); + dprintf("Reader: Baudrate %ld\n", bp_uart.baud.BaudRate); + } + + hr = uart_handle_irp(&bp_uart, irp); + if (FAILED(hr)) { + return hr; + } +#if 0 + if (irp->op == IRP_OP_WRITE) { + dprintf("WRITE:\n"); + dump_iobuf(&bp_uart.written); + } + if (irp->op == IRP_OP_READ) { + dprintf("READ:\n"); + dump_iobuf(&bp_uart.readable); + } +#endif + if (irp->op == IRP_OP_WRITE) { + write_ct = 0; + if (bp_uart.written.bytes[0] == 0x55) { + dprintf("Reader: Hello\n"); + return hr; + } + else if (bp_uart.written.bytes[3] == 0x00) { + dprintf("Reader: Wait Next Cmd\n"); + last_cmd = 0x00; + return hr; + } + else { + last_cmd = bp_uart.written.bytes[3]; + dump_iobuf(&bp_uart.written); + return hr; + } + } + if (irp->op == IRP_OP_READ) { + dprintf("Reader: last_cmd %d write_ct %d\n", last_cmd, write_ct); + switch (last_cmd) { + case 0x03: + dprintf("Reader: Initalize Reader\n"); + uint8_t buff_init[] = { 00, 00, 0xFF, 00, 0xFF, 00, 00, 00, 0xFF, 0x02, 0xFE, 0xD5, 0x19, 0x12, 0x00}; + iobuf_write(&bp_uart.readable, buff_init, sizeof(buff_init)); + bp_uart.written.pos = 0; + break; + + case 0x06: + dprintf("Reader: Unknown 0x06\n"); + uint8_t buff_unk6_r1[] = { 00, 00, 0xff, 00, 0xff, 00, 00, 00 }; + uint8_t buff_unk6_r2[] = {0xFF, 02, 0xFE, 0xd5, 0x33, 0xf8, 0x00}; + + if (!write_ct) + iobuf_write(&bp_uart.readable, buff_unk6_r1, sizeof(buff_unk6_r1)); + else + iobuf_write(&bp_uart.readable, buff_unk6_r2, sizeof(buff_unk6_r2)); + + break; + + default: + dprintf("Reader: Unknown Command %02X\n", last_cmd); + dump_iobuf(&bp_uart.written); + break; + } + write_ct++; + } + + bp_uart.written.pos = 0; + return hr; } \ No newline at end of file diff --git a/board/bpreader.h b/board/bpreader.h index ad096d2..419f5bd 100644 --- a/board/bpreader.h +++ b/board/bpreader.h @@ -5,8 +5,9 @@ struct bpreader_config { bool enable; - char access_code[21]; + uint16_t port; + wchar_t access_code[21]; }; -HRESULT bpreader_init(uint16_t port); -void bpreader_congif_init(struct bpreader_config *cfg); \ No newline at end of file +HRESULT bpreader_init(struct bpreader_config *cfg, uint16_t port); +void bpreader_congif_load(struct bpreader_config *cfg, const wchar_t *filename); \ No newline at end of file diff --git a/dist/ferrum/bananatools.ini b/dist/ferrum/bananatools.ini index 7d09aa5..f8e60cd 100644 --- a/dist/ferrum/bananatools.ini +++ b/dist/ferrum/bananatools.ini @@ -36,6 +36,10 @@ server_host=localhost [xinput] enable=1 +[reader] +enable=1 +access_code=00000000000000000000 + ; JVS config [jvs] enable=1 diff --git a/dist/taiko/bananatools.ini b/dist/taiko/bananatools.ini index a8105b5..3e48d46 100644 --- a/dist/taiko/bananatools.ini +++ b/dist/taiko/bananatools.ini @@ -21,6 +21,10 @@ windowed=1 framed=0 monitor=0 +[reader] +enable=1 +access_code=00000000000000000000 + ; Control the AMCUS replacement class [amcus] enable=1 diff --git a/ferrumhook/config.c b/ferrumhook/config.c index b8c5d76..37cb16d 100644 --- a/ferrumhook/config.c +++ b/ferrumhook/config.c @@ -31,16 +31,6 @@ void ferrum_xinput_config_load( cfg->enable = GetPrivateProfileIntW(L"xinput", L"enable", 1, filename); } -void ferrum_network_config_load( - struct ferrum_network_config *cfg, - const wchar_t *filename) -{ - assert(cfg != NULL); - assert(filename != NULL); - - cfg->enable = GetPrivateProfileIntW(L"network", L"enable", 1, filename); -} - void ferrum_hook_config_load( struct ferrum_hook_config *cfg, @@ -53,5 +43,5 @@ void ferrum_hook_config_load( ferrum_dll_config_load(&cfg->dll, filename); ferrum_xinput_config_load(&cfg->xinput, filename); gfx_config_load(&cfg->gfx, filename); - ferrum_network_config_load(&cfg->network, filename); + bpreader_congif_load(&cfg->reader, filename); } diff --git a/ferrumhook/config.h b/ferrumhook/config.h index 4aeddd2..6b72f83 100644 --- a/ferrumhook/config.h +++ b/ferrumhook/config.h @@ -5,7 +5,7 @@ #include "ferrumhook/ferrum-dll.h" #include "ferrumhook/xinput.h" #include "ferrumhook/jvs.h" -#include "ferrumhook/network.h" +#include "board/bpreader.h" #include "platform/config.h" #include "gfxhook/config.h" @@ -17,7 +17,7 @@ struct ferrum_hook_config { struct ferrum_xinput_config xinput; struct gfx_config gfx; struct amcus_config amcus; - struct ferrum_network_config network; + struct bpreader_config reader; }; void ferrum_dll_config_load( @@ -28,10 +28,6 @@ void ferrum_xinput_config_load( struct ferrum_xinput_config *cfg, const wchar_t *filename); -void ferrum_network_config_load( - struct ferrum_network_config *cfg, - const wchar_t *filename); - void ferrum_hook_config_load( struct ferrum_hook_config *cfg, const wchar_t *filename); diff --git a/ferrumhook/dllmain.c b/ferrumhook/dllmain.c index 9e54b27..d08087c 100644 --- a/ferrumhook/dllmain.c +++ b/ferrumhook/dllmain.c @@ -5,7 +5,6 @@ #include "ferrumhook/ferrum-dll.h" #include "ferrumhook/xinput.h" #include "ferrumhook/jvs.h" -#include "ferrumhook/network.h" #include "amcus/amcus.h" @@ -18,6 +17,7 @@ #include "gfxhook/gfx.h" #include "gfxhook/dxgi.h" #include "gfxhook/d3d11.h" +#include "board/bpreader.h" #include "util/dprintf.h" @@ -65,7 +65,7 @@ static DWORD CALLBACK ferrum_pre_startup(void) ExitProcess(EXIT_FAILURE); } - hr = network_hook_init(&ferrum_hook_cfg.network); + hr = bpreader_init(&ferrum_hook_cfg.reader, 4); if (FAILED(hr)) { ExitProcess(EXIT_FAILURE); diff --git a/ferrumhook/meson.build b/ferrumhook/meson.build index db12847..daff604 100644 --- a/ferrumhook/meson.build +++ b/ferrumhook/meson.build @@ -18,6 +18,7 @@ shared_library( hooklib_lib, gfxhook_lib, jvs_lib, + board_lib, ], sources : [ 'dllmain.c', @@ -29,7 +30,5 @@ shared_library( 'xinput.h', 'jvs.c', 'jvs.h', - 'network.c', - 'network.h', ], ) diff --git a/ferrumhook/network.c b/ferrumhook/network.c deleted file mode 100644 index 36cca83..0000000 --- a/ferrumhook/network.c +++ /dev/null @@ -1,66 +0,0 @@ -#include - -#include "taikohook/network.h" - -#include "hook/table.h" - -#include "util/dprintf.h" - -void network_insert_hooks(HMODULE target); - -static uint64_t my_TLSv1_method(); -static uint64_t my_SSL_CTX_new(void *method); -static uint64_t (*next_TLSv1_2_method)(); -static uint64_t (*next_TLSv1_method)(); -static uint64_t (*next_SSL_CTX_new)(void *method); - -static const struct hook_symbol nethook_syms[] = { - { - .link = (void *) &next_TLSv1_2_method, - .ordinal = 350 - }, - { - .patch = my_TLSv1_method, - .link = (void *) &next_TLSv1_method, - .ordinal = 170 - }, - { - .patch = my_SSL_CTX_new, - .link = (void *) &next_SSL_CTX_new, - .ordinal = 12 - }, -}; - -HRESULT network_hook_init(const struct ferrum_network_config *cfg) -{ - if (!cfg->enable) { - return S_FALSE; - } - - dprintf("Nethook: Init\n"); - network_insert_hooks(NULL); - - return S_OK; -} - -void network_insert_hooks(HMODULE target) -{ - hook_table_apply( - target, - "ssleay32.dll", - nethook_syms, - _countof(nethook_syms) - ); -} - -static uint64_t my_TLSv1_method() -{ - dprintf("Nethook: Redirect TLS v1.0 to v1.2\n"); - return next_TLSv1_2_method(); -} - -static uint64_t my_SSL_CTX_new(void *method) -{ - dprintf("Nethook: my_SSL_CTX_new\n"); - return next_SSL_CTX_new(method); -} \ No newline at end of file diff --git a/ferrumhook/network.h b/ferrumhook/network.h deleted file mode 100644 index 2924e7f..0000000 --- a/ferrumhook/network.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include -#include -struct ferrum_network_config { - bool enable; -}; - -HRESULT network_hook_init(const struct ferrum_network_config *cfg); \ No newline at end of file diff --git a/platform/jvs.c b/platform/jvs.c index e19070c..f2cd60d 100644 --- a/platform/jvs.c +++ b/platform/jvs.c @@ -23,8 +23,8 @@ static HRESULT jvs_handle_irp_locked(struct irp *irp); static struct uart jvs_uart; static CRITICAL_SECTION jvs_lock; -static uint8_t jvs_written_bytes[520]; -static uint8_t jvs_readable_bytes[520]; +static uint8_t bp_written_bytes[520]; +static uint8_t bp_readable_bytes[520]; static struct jvs_node *jvs_root; static jvs_provider_t jvs_provider; @@ -40,10 +40,10 @@ HRESULT jvs_hook_init(const struct jvs_config *cfg, jvs_provider_t provider) dprintf("JVS I/O: init\n"); uart_init(&jvs_uart, cfg->port); - jvs_uart.written.bytes = jvs_written_bytes; - jvs_uart.written.nbytes = sizeof(jvs_written_bytes); - jvs_uart.readable.bytes = jvs_readable_bytes; - jvs_uart.readable.nbytes = sizeof(jvs_readable_bytes); + jvs_uart.written.bytes = bp_written_bytes; + jvs_uart.written.nbytes = sizeof(bp_written_bytes); + jvs_uart.readable.bytes = bp_readable_bytes; + jvs_uart.readable.nbytes = sizeof(bp_readable_bytes); jvs_provider = provider; diff --git a/taikohook/bngrw.c b/taikohook/bngrw.c new file mode 100644 index 0000000..e69de29 diff --git a/taikohook/bngrw.h b/taikohook/bngrw.h new file mode 100644 index 0000000..e69de29 diff --git a/taikohook/config.c b/taikohook/config.c index a4c816f..9dac33c 100644 --- a/taikohook/config.c +++ b/taikohook/config.c @@ -64,4 +64,5 @@ void taiko_hook_config_load( gfx_config_load(&cfg->gfx, filename); qr_config_load(&cfg->qr, filename); network_config_load(&cfg->network, filename); + bpreader_congif_load(&cfg->reader, filename); } diff --git a/taikohook/config.h b/taikohook/config.h index e552a0d..6958e52 100644 --- a/taikohook/config.h +++ b/taikohook/config.h @@ -10,6 +10,7 @@ #include "platform/config.h" #include "gfxhook/config.h" #include "amcus/config.h" +#include "board/bpreader.h" struct taiko_hook_config { struct platform_config platform; @@ -19,6 +20,7 @@ struct taiko_hook_config { struct amcus_config amcus; struct qr_config qr; struct ferrum_network_config network; + struct bpreader_config reader; }; void taiko_dll_config_load( diff --git a/taikohook/meson.build b/taikohook/meson.build index 6bc87f3..b6de754 100644 --- a/taikohook/meson.build +++ b/taikohook/meson.build @@ -18,6 +18,7 @@ shared_library( hooklib_lib, gfxhook_lib, jvs_lib, + board_lib ], sources : [ 'dllmain.c', @@ -33,5 +34,7 @@ shared_library( 'qr.h', 'network.c', 'network.h', + 'bngrw.c', + 'bngrw.h', ], )