Bemanitools v5.26 release

This commit is contained in:
icex2
2019-09-27 22:36:50 +02:00
commit cbd7720349
718 changed files with 62731 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
avsdlls += sdvxhook
deplibs_sdvxhook := \
avs \
libs_sdvxhook := \
acioemu \
hook \
hooklib \
util \
eamio \
sdvxio \
src_sdvxhook := \
acio.c \
dllmain.c \
gfx.c \
kfca.c \
lcd.c \

94
src/main/sdvxhook/acio.c Normal file
View File

@@ -0,0 +1,94 @@
#include <windows.h>
#include <ntdef.h>
#include <devioctl.h>
#include <ntddser.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include "acioemu/addr.h"
#include "acioemu/emu.h"
#include "acioemu/icca.h"
#include "hook/iohook.h"
#include "imports/avs.h"
#include "sdvxhook/acio.h"
#include "sdvxhook/kfca.h"
#include "util/defs.h"
#include "util/iobuf.h"
#include "util/log.h"
#include "util/str.h"
static struct ac_io_emu ac_io_emu;
static struct ac_io_emu_icca ac_io_emu_icca;
void ac_io_bus_init(void)
{
ac_io_emu_init(&ac_io_emu, L"COM2");
ac_io_emu_icca_init(&ac_io_emu_icca, &ac_io_emu, 0);
kfca_init(&ac_io_emu);
}
void ac_io_bus_fini(void)
{
ac_io_emu_fini(&ac_io_emu);
}
HRESULT ac_io_bus_dispatch_irp(struct irp *irp)
{
const struct ac_io_message *msg;
HRESULT hr;
log_assert(irp != NULL);
if (!ac_io_emu_match_irp(&ac_io_emu, irp)) {
return irp_invoke_next(irp);
}
for (;;) {
hr = ac_io_emu_dispatch_irp(&ac_io_emu, irp);
if (hr != S_OK) {
return hr;
}
msg = ac_io_emu_request_peek(&ac_io_emu);
switch (msg->addr) {
case 0:
ac_io_emu_cmd_assign_addrs(&ac_io_emu, msg, 2);
break;
case 1:
ac_io_emu_icca_dispatch_request(&ac_io_emu_icca, msg);
break;
case 2:
kfca_dispatch_request(msg);
break;
case AC_IO_BROADCAST:
log_warning("Broadcast(?) message on SDVX ACIO bus?");
break;
default:
log_warning(
"ACIO message on unhandled bus address: %d",
msg->addr);
break;
}
ac_io_emu_request_pop(&ac_io_emu);
}
}

12
src/main/sdvxhook/acio.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef IIDXHOOK_AC_IO_H
#define IIDXHOOK_AC_IO_H
#include <windows.h>
#include "hook/iohook.h"
void ac_io_bus_init(void);
void ac_io_bus_fini(void);
HRESULT ac_io_bus_dispatch_irp(struct irp *irp);
#endif

143
src/main/sdvxhook/dllmain.c Normal file
View File

@@ -0,0 +1,143 @@
#include <windows.h>
#include <stdbool.h>
#include <stddef.h>
#include "bemanitools/eamio.h"
#include "bemanitools/sdvxio.h"
#include "hook/iohook.h"
#include "hooklib/app.h"
#include "hooklib/rs232.h"
#include "sdvxhook/acio.h"
#include "sdvxhook/lcd.h"
#include "sdvxhook/gfx.h"
#include "util/cmdline.h"
#include "util/defs.h"
#include "util/log.h"
static bool my_dll_entry_init(char *sidcode, struct property_node *config);
static bool my_dll_entry_main(void);
static const irp_handler_t sdvxhook_handlers[] = {
ac_io_bus_dispatch_irp,
lcd_dispatch_irp,
};
static bool my_dll_entry_init(char *sidcode, struct property_node *config)
{
bool ok;
log_info("--- Begin sdvxhook dll_entry_init ---");
ac_io_bus_init();
log_info("Starting up SDVX IO backend");
sdvx_io_set_loggers(
log_body_misc,
log_body_info,
log_body_warning,
log_body_fatal);
ok = sdvx_io_init(avs_thread_create, avs_thread_join, avs_thread_destroy);
if (!ok) {
goto sdvx_io_fail;
}
log_info("Starting up card reader backend");
eam_io_set_loggers(
log_body_misc,
log_body_info,
log_body_warning,
log_body_fatal);
ok = eam_io_init(avs_thread_create, avs_thread_join, avs_thread_destroy);
if (!ok) {
goto eam_io_fail;
}
log_info("--- End sdvxhook dll_entry_init ---");
return app_hook_invoke_init(sidcode, config);
eam_io_fail:
sdvx_io_fini();
sdvx_io_fail:
ac_io_bus_fini();
return false;
}
static bool my_dll_entry_main(void)
{
bool result;
result = app_hook_invoke_main();
log_info("Shutting down card reader backend");
eam_io_fini();
log_info("Shutting down SDVX IO backend");
sdvx_io_fini();
ac_io_bus_fini();
return result;
}
BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
{
int i;
int argc;
char **argv;
if (reason != DLL_PROCESS_ATTACH) {
return TRUE;
}
log_to_external(
log_body_misc,
log_body_info,
log_body_warning,
log_body_fatal);
args_recover(&argc, &argv);
for (i = 1 ; i < argc ; i++) {
if (argv[i][0] != '-') {
continue;
}
switch (argv[i][1]) {
case 'c':
gfx_set_confined();
break;
case 'w':
gfx_set_windowed();
break;
}
}
args_free(argc, argv);
app_hook_init(my_dll_entry_init, my_dll_entry_main);
iohook_init(sdvxhook_handlers, lengthof(sdvxhook_handlers));
rs232_hook_init();
gfx_init();
lcd_init();
return TRUE;
}

139
src/main/sdvxhook/gfx.c Normal file
View File

@@ -0,0 +1,139 @@
#include <windows.h>
#include <d3d9.h>
#include <stdbool.h>
#include "hook/com-proxy.h"
#include "hook/pe.h"
#include "hook/table.h"
#include "sdvxhook/gfx.h"
#include "util/defs.h"
#include "util/log.h"
static LRESULT CALLBACK my_WndProc(HWND hwnd, UINT msg, WPARAM wparam,
LPARAM lparam);
static HRESULT STDCALL my_CreateDevice(
IDirect3D9 *self, UINT adapter, D3DDEVTYPE type, HWND hwnd, DWORD flags,
D3DPRESENT_PARAMETERS *pp, IDirect3DDevice9 **pdev);
static IDirect3D9 *STDCALL my_Direct3DCreate9(UINT sdk_ver);
static WNDPROC real_WndProc;
static IDirect3D9 * (STDCALL *real_Direct3DCreate9)(UINT sdk_ver);
static const struct hook_symbol gfx_hooks[] = {
{
.name = "Direct3DCreate9",
.patch = my_Direct3DCreate9,
.link = (void **) &real_Direct3DCreate9
},
};
static bool gfx_confined;
static bool gfx_windowed;
static LRESULT gfx_confine(HWND hwnd)
{
POINT p;
RECT r;
log_misc("Confining mouse (ALT-TAB to release)");
p.x = 0;
p.y = 0;
ClientToScreen(hwnd, &p);
r.left = p.x;
r.top = p.y;
r.right = p.x + 100;
r.bottom = p.y + 100;
ClipCursor(&r);
return TRUE;
}
static LRESULT gfx_unconfine(HWND hwnd)
{
log_misc("Un-confining mouse");
ClipCursor(NULL);
return TRUE;
}
static LRESULT CALLBACK my_WndProc(HWND hwnd, UINT msg, WPARAM wparam,
LPARAM lparam)
{
switch (msg) {
case WM_SETFOCUS:
return gfx_confine(hwnd);
case WM_KILLFOCUS:
return gfx_unconfine(hwnd);
default:
return CallWindowProc(real_WndProc, hwnd, msg, wparam, lparam);
}
}
static HRESULT STDCALL my_CreateDevice(
IDirect3D9 *self, UINT adapter, D3DDEVTYPE type, HWND hwnd, DWORD flags,
D3DPRESENT_PARAMETERS *pp, IDirect3DDevice9 **pdev)
{
IDirect3D9 *real = COM_PROXY_UNWRAP(self);
HRESULT hr;
log_misc("IDirect3D9::CreateDevice hook hit");
if (gfx_windowed) {
pp->Windowed = TRUE;
pp->FullScreen_RefreshRateInHz = 0;
}
hr = IDirect3D9_CreateDevice(real, adapter, type, hwnd, flags, pp, pdev);
if (SUCCEEDED(hr) && gfx_confined) {
real_WndProc = (void *) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (uintptr_t) my_WndProc);
}
return hr;
}
static IDirect3D9 *STDCALL my_Direct3DCreate9(UINT sdk_ver)
{
IDirect3D9 *api;
IDirect3D9Vtbl *api_vtbl;
struct com_proxy *api_proxy;
log_info("Direct3DCreate9 hook hit");
api = real_Direct3DCreate9(sdk_ver);
api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl));
api_vtbl = api_proxy->vptr;
api_vtbl->CreateDevice = my_CreateDevice;
return (IDirect3D9 *) api_proxy;
}
void gfx_init(void)
{
hook_table_apply(NULL, "d3d9.dll", gfx_hooks, lengthof(gfx_hooks));
log_info("Inserted graphics hooks");
}
void gfx_set_confined(void)
{
gfx_confined = true;
}
void gfx_set_windowed(void)
{
gfx_windowed = true;
}

8
src/main/sdvxhook/gfx.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef SDVXHOOK_GFX_H
#define SDVXHOOK_GFX_H
void gfx_init(void);
void gfx_set_confined(void);
void gfx_set_windowed(void);
#endif

195
src/main/sdvxhook/kfca.c Normal file
View File

@@ -0,0 +1,195 @@
#include <windows.h>
#include <stdint.h>
#include <string.h>
#include "acio/acio.h"
#include "acioemu/emu.h"
#include "bemanitools/sdvxio.h"
#include "util/defs.h"
#include "util/log.h"
#include "util/time.h"
static void kfca_send_version(const struct ac_io_message *req);
static void kfca_report_status(const struct ac_io_message *req, uint8_t status);
static void kfca_report_0128(const struct ac_io_message *req);
static void kfca_poll(const struct ac_io_message *req);
static void kfca_poll_thunk(void *ctx_ptr, struct ac_io_message *resp);
static struct ac_io_emu *kfca_ac_io_emu;
void kfca_init(struct ac_io_emu *emu)
{
log_assert(emu != NULL);
kfca_ac_io_emu = emu;
}
void kfca_dispatch_request(const struct ac_io_message *req)
{
uint16_t cmd_code;
cmd_code = ac_io_u16(req->cmd.code);
switch (cmd_code) {
case AC_IO_CMD_GET_VERSION:
log_misc("AC_IO_CMD_GET_VERSION(%d)", req->addr);
kfca_send_version(req);
break;
case AC_IO_CMD_START_UP:
log_misc("AC_IO_CMD_START_UP(%d)", req->addr);
kfca_report_status(req, 0x00);
break;
case AC_IO_CMD_KFCA_POLL:
kfca_poll(req);
break;
case AC_IO_CMD_KFCA_UNK_0120:
log_misc("AC_IO_CMD_KFCA_UNK_%04X(%d)", cmd_code, req->addr);
kfca_report_status(req, 0x00);
break;
case AC_IO_CMD_KFCA_UNK_0128:
log_misc("AC_IO_CMD_KFCA_UNK_0128(%d)", req->addr);
kfca_report_0128(req);
break;
default:
log_warning("Unknown ACIO message %04x on KFCA mode, addr=%d",
cmd_code, req->addr);
break;
}
}
static void kfca_send_version(const struct ac_io_message *req)
{
struct ac_io_message resp;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = sizeof(resp.cmd.version);
resp.cmd.version.type = ac_io_u32(AC_IO_NODE_TYPE_KFCA);
resp.cmd.version.flag = 0x00;
resp.cmd.version.major = 0x01;
resp.cmd.version.minor = 0x01;
resp.cmd.version.revision = 0x00;
memcpy(resp.cmd.version.product_code, "KFCA",
sizeof(resp.cmd.version.product_code));
strncpy(resp.cmd.version.date, __DATE__, sizeof(resp.cmd.version.date));
strncpy(resp.cmd.version.time, __TIME__, sizeof(resp.cmd.version.time));
ac_io_emu_response_push(kfca_ac_io_emu, &resp, 0);
}
static void kfca_report_status(const struct ac_io_message *req, uint8_t status)
{
struct ac_io_message resp;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = sizeof(resp.cmd.status);
resp.cmd.status = status;
ac_io_emu_response_push(kfca_ac_io_emu, &resp, 0);
}
static void kfca_report_0128(const struct ac_io_message *req) {
struct ac_io_message resp;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = req->cmd.nbytes;
memcpy(resp.cmd.raw, req->cmd.raw, req->cmd.nbytes);
ac_io_emu_response_push(kfca_ac_io_emu, &resp, 0);
}
static void kfca_poll(const struct ac_io_message *req)
{
static uint32_t last_poll = 0;
uint64_t delay;
const struct ac_io_kfca_poll_out *pout;
uintptr_t ctx;
size_t i;
/* Handle lighting output immediately */
pout = &req->cmd.kfca_poll_out;
sdvx_io_set_gpio_lights(ac_io_u32(pout->gpio));
for (i = 0 ; i < lengthof(pout->pwm) ; i++) {
sdvx_io_set_pwm_light(i, pout->pwm[i]);
}
sdvx_io_write_output();
/* SDVX expects only one poll response per frame.
We use a thunk here to defer polling until right before the response
is due. */
delay = time_get_elapsed_ms(last_poll) < 16 ? 16000 : 0;
last_poll = time_get_counter();
ctx = req->addr | (req->cmd.code << 8) | (req->cmd.seq_no << 24);
ac_io_emu_response_push_thunk(
kfca_ac_io_emu,
kfca_poll_thunk,
(void *) ctx,
delay);
}
static void kfca_poll_thunk(void *ctx_ptr, struct ac_io_message *resp)
{
struct ac_io_kfca_poll_in *pin;
uintptr_t ctx;
uint8_t req_addr;
uint16_t req_code;
uint8_t req_seq_no;
/* Unpack context "pointer" for relevant response information */
ctx = (uintptr_t) ctx_ptr;
req_addr = ctx;
req_code = ctx >> 8;
req_seq_no = ctx >> 24;
/* Poll input now and construct an immediate response */
pin = &resp->cmd.kfca_poll_in;
resp->addr = req_addr | AC_IO_RESPONSE_FLAG;
resp->cmd.code = req_code;
resp->cmd.seq_no = req_seq_no;
resp->cmd.nbytes = sizeof(*pin);
sdvx_io_read_input();
memset(pin, 0, sizeof(*pin));
pin->adc[0] = sdvx_io_get_spinner_pos(0) << 6;
pin->adc[1] = sdvx_io_get_spinner_pos(1) << 6;
pin->gpio_sys |= sdvx_io_get_input_gpio_sys() & 0x3F;
pin->adc[0] = ac_io_u16(pin->adc[0]);
pin->adc[1] = ac_io_u16(pin->adc[1]);
pin->gpio[0] = ac_io_u16(sdvx_io_get_input_gpio(0));
pin->gpio[1] = ac_io_u16(sdvx_io_get_input_gpio(1));
}

11
src/main/sdvxhook/kfca.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef SDVXHOOK_KFCA_H
#define SDVXHOOK_KFCA_H
#include "acio/acio.h"
#include "acioemu/emu.h"
void kfca_init(struct ac_io_emu *emu);
void kfca_dispatch_request(const struct ac_io_message *req);
#endif

105
src/main/sdvxhook/lcd.c Normal file
View File

@@ -0,0 +1,105 @@
#define LOG_MODULE "lcd"
#include <windows.h>
#include <ntdef.h>
#include <devioctl.h>
#include <ntddser.h>
#include <stdbool.h>
#include <string.h>
#include <wchar.h>
#include "hook/iohook.h"
#include "sdvxhook/lcd.h"
#include "util/hex.h"
#include "util/iobuf.h"
#include "util/log.h"
#include "util/str.h"
static HRESULT lcd_open(struct irp *irp);
static HRESULT lcd_close(struct irp *irp);
static HRESULT lcd_write(struct irp *irp);
static HANDLE lcd_fd;
void lcd_init(void)
{
log_assert(lcd_fd == NULL);
lcd_fd = iohook_open_dummy_fd();
}
void lcd_fini(void)
{
if (lcd_fd != NULL) {
CloseHandle(lcd_fd);
}
lcd_fd = NULL;
}
HRESULT lcd_dispatch_irp(struct irp *irp)
{
log_assert(irp != NULL);
if (irp->op != IRP_OP_OPEN && irp->fd != lcd_fd) {
return irp_invoke_next(irp);
}
switch (irp->op) {
case IRP_OP_OPEN: return lcd_open(irp);
case IRP_OP_CLOSE: return lcd_close(irp);
case IRP_OP_READ: return S_OK;
case IRP_OP_WRITE: return lcd_write(irp);
case IRP_OP_IOCTL: return S_OK;
default: return E_NOTIMPL;
}
}
static HRESULT lcd_open(struct irp *irp)
{
log_assert(irp != NULL);
if (!wstr_eq(irp->open_filename, L"COM1")) {
return irp_invoke_next(irp);
}
irp->fd = lcd_fd;
log_info("\"LCD\" port opened");
return S_OK;
}
static HRESULT lcd_close(struct irp *irp)
{
log_info("\"LCD\" port closed");
return S_OK;
}
static HRESULT lcd_write(struct irp *irp)
{
char str[128];
size_t nbytes;
log_assert(irp != NULL);
if (irp->write.nbytes < sizeof(str)) {
nbytes = irp->write.nbytes;
} else {
nbytes = sizeof(str) - 1;
}
memcpy(str, irp->write.bytes, nbytes);
str[nbytes] = '\0';
log_misc("-> %s", str);
irp->write.pos = irp->write.nbytes;
return S_OK;
}

12
src/main/sdvxhook/lcd.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef SDVXHOOK_LCD_H
#define SDVXHOOK_LCD_H
#include <windows.h>
#include "hook/iohook.h"
void lcd_init(void);
void lcd_fini(void);
HRESULT lcd_dispatch_irp(struct irp *irp);
#endif

View File

@@ -0,0 +1,4 @@
LIBRARY sdvxhook
EXPORTS
DllMain@12 @1 NONAME