Bemanitools v5.26 release
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
avsdlls += unicorntail
|
||||
|
||||
deplibs_unicorntail := \
|
||||
avs \
|
||||
|
||||
libs_unicorntail := \
|
||||
p3io \
|
||||
p3ioemu \
|
||||
hook \
|
||||
hooklib \
|
||||
util \
|
||||
|
||||
src_unicorntail := \
|
||||
dllmain.c \
|
||||
p3io.c \
|
||||
usbmem.c \
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
!include ../Common.inc
|
||||
|
||||
TARGETNAME = unicorntail
|
||||
TARGETTYPE = DYNLINK
|
||||
|
||||
SOURCES = \
|
||||
unicorntail.c \
|
||||
|
||||
TARGETLIBS = $(SDK_LIB_PATH)/kernel32.lib \
|
||||
../libhook/$(OBJDIR)/$(O)/libhook.lib \
|
||||
../libp3io/$(OBJDIR)/$(O)/libp3io.lib \
|
||||
../libutil/$(OBJDIR)/$(O)/libutil.lib \
|
||||
../imports/$(O)/libavs.lib \
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "ddrhook/p3io.h"
|
||||
#include "ddrhook/usbmem.h"
|
||||
|
||||
#include "hook/iohook.h"
|
||||
|
||||
#include "hooklib/app.h"
|
||||
|
||||
#include "imports/avs.h"
|
||||
|
||||
#include "unicorntail/p3io.h"
|
||||
#include "unicorntail/usbmem.h"
|
||||
|
||||
#include "util/defs.h"
|
||||
#include "util/log.h"
|
||||
|
||||
static bool my_dll_entry_init(char *sidcode, struct property_node *param);
|
||||
static bool my_dll_entry_main(void);
|
||||
|
||||
static const irp_handler_t unicorntail_handlers[] = {
|
||||
p3io_filter_dispatch_irp,
|
||||
usbmem_dispatch_irp,
|
||||
};
|
||||
|
||||
static bool my_dll_entry_init(char *sidcode, struct property_node *param)
|
||||
{
|
||||
log_info("--- Begin unicorntail dll_entry_init ---");
|
||||
|
||||
iohook_init(unicorntail_handlers, lengthof(unicorntail_handlers));
|
||||
p3io_filter_init();
|
||||
usbmem_init();
|
||||
|
||||
log_info("--- End unicorntail dll_entry_init ---");
|
||||
|
||||
return app_hook_invoke_init(sidcode, param);
|
||||
}
|
||||
|
||||
static bool my_dll_entry_main(void)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = app_hook_invoke_main();
|
||||
|
||||
usbmem_fini();
|
||||
p3io_filter_fini();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
|
||||
{
|
||||
if (reason != DLL_PROCESS_ATTACH) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
log_to_external(
|
||||
log_body_misc,
|
||||
log_body_info,
|
||||
log_body_warning,
|
||||
log_body_fatal);
|
||||
|
||||
app_hook_init(my_dll_entry_init, my_dll_entry_main);
|
||||
|
||||
end:
|
||||
return TRUE;
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "p3io/cmd.h"
|
||||
#include "p3io/frame.h"
|
||||
|
||||
#include "p3ioemu/uart.h"
|
||||
|
||||
#include "unicorntail/p3io.h"
|
||||
|
||||
#include "util/array.h"
|
||||
#include "util/log.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static bool p3io_match_irp_locked(const struct irp *irp);
|
||||
|
||||
static HRESULT p3io_handle_open(struct irp *irp);
|
||||
static HRESULT p3io_handle_close(struct irp *irp);
|
||||
static HRESULT p3io_handle_write(struct irp *irp);
|
||||
static HRESULT p3io_handle_read(struct irp *irp);
|
||||
|
||||
static CRITICAL_SECTION p3io_handle_lock;
|
||||
static struct array p3io_handles;
|
||||
static CRITICAL_SECTION p3io_cmd_lock;
|
||||
static uint8_t p3io_resp_bytes[260];
|
||||
static struct iobuf p3io_resp;
|
||||
|
||||
void p3io_filter_init(void)
|
||||
{
|
||||
InitializeCriticalSection(&p3io_handle_lock);
|
||||
InitializeCriticalSection(&p3io_cmd_lock);
|
||||
|
||||
p3io_uart_set_path(0, L"COM4");
|
||||
|
||||
p3io_resp.bytes = p3io_resp_bytes;
|
||||
p3io_resp.nbytes = sizeof(p3io_resp_bytes);
|
||||
p3io_resp.pos = 0;
|
||||
}
|
||||
|
||||
void p3io_filter_fini(void)
|
||||
{
|
||||
DeleteCriticalSection(&p3io_cmd_lock);
|
||||
DeleteCriticalSection(&p3io_handle_lock);
|
||||
}
|
||||
|
||||
HRESULT p3io_filter_dispatch_irp(struct irp *irp)
|
||||
{
|
||||
bool match;
|
||||
|
||||
EnterCriticalSection(&p3io_handle_lock);
|
||||
match = p3io_match_irp_locked(irp);
|
||||
LeaveCriticalSection(&p3io_handle_lock);
|
||||
|
||||
if (!match) {
|
||||
return irp_invoke_next(irp);
|
||||
}
|
||||
|
||||
switch (irp->op) {
|
||||
case IRP_OP_OPEN: return p3io_handle_open(irp);
|
||||
case IRP_OP_CLOSE: return p3io_handle_close(irp);
|
||||
case IRP_OP_WRITE: return p3io_handle_write(irp);
|
||||
case IRP_OP_READ: return p3io_handle_read(irp);
|
||||
default: return irp_invoke_next(irp);
|
||||
}
|
||||
}
|
||||
|
||||
static bool p3io_match_irp_locked(const struct irp *irp)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (irp->op == IRP_OP_OPEN) {
|
||||
return wstr_ends_with(irp->open_filename, L"\\p3io");
|
||||
} else {
|
||||
for (i = 0 ; i < p3io_handles.nitems ; i++) {
|
||||
if (irp->fd == *array_item(HANDLE, &p3io_handles, i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static HRESULT p3io_handle_open(struct irp *irp)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
hr = irp_invoke_next(irp);
|
||||
|
||||
if (FAILED(hr)) {
|
||||
return hr;
|
||||
}
|
||||
|
||||
log_misc("Got p3io fd %p", irp->fd);
|
||||
|
||||
EnterCriticalSection(&p3io_handle_lock);
|
||||
*array_append(HANDLE, &p3io_handles) = irp->fd;
|
||||
LeaveCriticalSection(&p3io_handle_lock);
|
||||
|
||||
return hr;
|
||||
}
|
||||
|
||||
static HRESULT p3io_handle_close(struct irp *irp)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
log_misc("Releasing p3io fd %p", irp->fd);
|
||||
|
||||
EnterCriticalSection(&p3io_handle_lock);
|
||||
|
||||
for (i = 0 ; i < p3io_handles.nitems ; i++) {
|
||||
if (irp->fd == array_item(HANDLE, &p3io_handles, i)) {
|
||||
array_remove(HANDLE, &p3io_handles, i);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
LeaveCriticalSection(&p3io_handle_lock);
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static HRESULT p3io_handle_write(struct irp *irp)
|
||||
{
|
||||
union p3io_req_any req;
|
||||
union p3io_resp_any resp;
|
||||
struct iobuf desc;
|
||||
HRESULT hr;
|
||||
|
||||
desc.bytes = req.raw;
|
||||
desc.nbytes = sizeof(req);
|
||||
desc.pos = 0;
|
||||
|
||||
hr = p3io_frame_decode(&desc, &irp->write);
|
||||
|
||||
if (FAILED(hr)) {
|
||||
return hr;
|
||||
}
|
||||
|
||||
switch (p3io_req_cmd(&req)) {
|
||||
case P3IO_CMD_RS232_OPEN_CLOSE:
|
||||
EnterCriticalSection(&p3io_cmd_lock);
|
||||
p3io_uart_cmd_open_close(&req.rs232_open_close, &resp.u8);
|
||||
|
||||
break;
|
||||
|
||||
case P3IO_CMD_RS232_WRITE:
|
||||
EnterCriticalSection(&p3io_cmd_lock);
|
||||
p3io_uart_cmd_write(&req.rs232_write, &resp.rs232_write);
|
||||
|
||||
break;
|
||||
|
||||
case P3IO_CMD_RS232_READ:
|
||||
EnterCriticalSection(&p3io_cmd_lock);
|
||||
p3io_uart_cmd_read(&req.rs232_read, &resp.rs232_read);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Non-UART command, break out here. */
|
||||
irp->write.pos = 0;
|
||||
|
||||
return irp_invoke_next(irp);
|
||||
}
|
||||
|
||||
/* Frame up and queue a response packet */
|
||||
|
||||
hr = p3io_frame_encode(&p3io_resp, &resp.hdr, resp.hdr.nbytes + 1);
|
||||
LeaveCriticalSection(&p3io_cmd_lock);
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static HRESULT p3io_handle_read(struct irp *irp)
|
||||
{
|
||||
struct const_iobuf tmp;
|
||||
|
||||
EnterCriticalSection(&p3io_cmd_lock);
|
||||
|
||||
if (p3io_resp.pos == 0) {
|
||||
LeaveCriticalSection(&p3io_cmd_lock);
|
||||
|
||||
return irp_invoke_next(irp);
|
||||
} else {
|
||||
iobuf_flip(&tmp, &p3io_resp);
|
||||
iobuf_move(&irp->read, &tmp);
|
||||
p3io_resp.pos = 0;
|
||||
|
||||
LeaveCriticalSection(&p3io_cmd_lock);
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef UNICORNTAIL_P3IO_H
|
||||
#define UNICORNTAIL_P3IO_H
|
||||
|
||||
#include "hook/iohook.h"
|
||||
|
||||
void p3io_filter_init(void);
|
||||
void p3io_filter_fini(void);
|
||||
HRESULT p3io_filter_dispatch_irp(struct irp *irp);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,4 @@
|
||||
LIBRARY unicorntail
|
||||
|
||||
EXPORTS
|
||||
DllMain@12 @1 NONAME
|
||||
@@ -0,0 +1,185 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "hook/iohook.h"
|
||||
|
||||
#include "util/log.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static bool usbmem_match_irp(const struct irp *irp);
|
||||
static HRESULT usbmem_dispatch_irp_locked(struct irp *irp);
|
||||
static HRESULT usbmem_handle_open(struct irp *irp);
|
||||
static HRESULT usbmem_handle_close(struct irp *irp);
|
||||
static HRESULT usbmem_handle_write(struct irp *irp);
|
||||
static HRESULT usbmem_handle_read(struct irp *irp);
|
||||
|
||||
static CRITICAL_SECTION usbmem_lock;
|
||||
static HANDLE usbmem_fd;
|
||||
static bool usbmem_pending;
|
||||
static char usbmem_response[64];
|
||||
|
||||
void usbmem_init(void)
|
||||
{
|
||||
InitializeCriticalSection(&usbmem_lock);
|
||||
usbmem_pending = false;
|
||||
}
|
||||
|
||||
void usbmem_fini(void)
|
||||
{
|
||||
struct irp irp;
|
||||
|
||||
if (usbmem_fd != NULL && usbmem_fd != INVALID_HANDLE_VALUE) {
|
||||
memset(&irp, 0, sizeof(irp));
|
||||
irp.op = IRP_OP_CLOSE;
|
||||
irp.fd = usbmem_fd;
|
||||
|
||||
irp_invoke_next(&irp);
|
||||
}
|
||||
|
||||
DeleteCriticalSection(&usbmem_lock);
|
||||
}
|
||||
|
||||
HRESULT usbmem_dispatch_irp(struct irp *irp)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
/* usbmem is not performance-critical in any way so we can use coarse
|
||||
locking. */
|
||||
|
||||
EnterCriticalSection(&usbmem_lock);
|
||||
|
||||
if (!usbmem_match_irp(irp)) {
|
||||
LeaveCriticalSection(&usbmem_lock);
|
||||
|
||||
return irp_invoke_next(irp);
|
||||
} else {
|
||||
hr = usbmem_dispatch_irp_locked(irp);
|
||||
LeaveCriticalSection(&usbmem_lock);
|
||||
|
||||
return hr;
|
||||
}
|
||||
}
|
||||
|
||||
static bool usbmem_match_irp(const struct irp *irp)
|
||||
{
|
||||
if (irp->op == IRP_OP_OPEN) {
|
||||
return wstr_eq(irp->open_filename, L"COM3");
|
||||
} else {
|
||||
/* This will not match any IRPs at all unless IRP_OP_OPEN failed and
|
||||
we set usbmem_fd to a non-NULL HANDLE (i.e. we started emulation). */
|
||||
return irp->fd == usbmem_fd;
|
||||
}
|
||||
}
|
||||
|
||||
static HRESULT usbmem_dispatch_irp_locked(struct irp *irp)
|
||||
{
|
||||
switch (irp->op) {
|
||||
case IRP_OP_OPEN: return usbmem_handle_open(irp);
|
||||
case IRP_OP_CLOSE: return usbmem_handle_close(irp);
|
||||
case IRP_OP_WRITE: return usbmem_handle_write(irp);
|
||||
case IRP_OP_READ: return usbmem_handle_read(irp);
|
||||
default: return E_NOTIMPL;
|
||||
}
|
||||
}
|
||||
|
||||
static HRESULT usbmem_handle_open(struct irp *irp)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
hr = irp_invoke_next(irp);
|
||||
|
||||
if (SUCCEEDED(hr)) {
|
||||
log_info("Opened a real usbmem port");
|
||||
|
||||
return hr;
|
||||
}
|
||||
|
||||
log_info("Failed to open real usbmem, will emulate one instead");
|
||||
|
||||
/* Opening the real COM3 port failed. Open a fake FD, return that, and
|
||||
start emulating a usbmem unit. */
|
||||
|
||||
if (usbmem_fd == NULL || usbmem_fd == INVALID_HANDLE_VALUE) {
|
||||
usbmem_fd = iohook_open_dummy_fd();
|
||||
}
|
||||
|
||||
irp->fd = usbmem_fd;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static HRESULT usbmem_handle_close(struct irp *irp)
|
||||
{
|
||||
usbmem_fd = NULL;
|
||||
|
||||
return irp_invoke_next(irp);
|
||||
}
|
||||
|
||||
static HRESULT usbmem_handle_write(struct irp *irp)
|
||||
{
|
||||
char request[64];
|
||||
size_t nbytes;
|
||||
|
||||
nbytes = irp->write.nbytes - irp->write.pos;
|
||||
|
||||
if (nbytes > sizeof(request)) {
|
||||
nbytes = sizeof(request);
|
||||
}
|
||||
|
||||
memcpy(request, &irp->write.bytes[irp->write.pos], nbytes);
|
||||
irp->write.pos += nbytes;
|
||||
|
||||
if (nbytes > 0) {
|
||||
request[nbytes - 1] = '\0'; /* This is always a CR. */
|
||||
} else {
|
||||
request[0] = '\0'; /* Shouldn't ever happen but w/e */
|
||||
}
|
||||
|
||||
log_misc(">%s", request);
|
||||
|
||||
if (request[0] != '\0') {
|
||||
if (str_eq(request, "sver")) {
|
||||
str_cpy(usbmem_response,
|
||||
sizeof(usbmem_response),
|
||||
"done GQHDXJAA UNKNOWN");
|
||||
} else if (
|
||||
str_eq(request, "on_a") ||
|
||||
str_eq(request, "on_b") ||
|
||||
str_eq(request, "offa") ||
|
||||
str_eq(request, "offb") ||
|
||||
strncmp(request, "lma ", 4) == 0 ||
|
||||
strncmp(request, "lmb ", 4) == 0 ) {
|
||||
str_cpy(usbmem_response, sizeof(usbmem_response), "done");
|
||||
} else {
|
||||
str_cpy(usbmem_response, sizeof(usbmem_response), "not connected");
|
||||
}
|
||||
}
|
||||
|
||||
usbmem_pending = true;
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
static HRESULT usbmem_handle_read(struct irp *irp)
|
||||
{
|
||||
size_t rlength;
|
||||
|
||||
if (usbmem_pending) {
|
||||
if (strlen(usbmem_response) > 0) {
|
||||
log_misc("%s", usbmem_response);
|
||||
}
|
||||
|
||||
str_cat(usbmem_response, sizeof(usbmem_response), "\r>");
|
||||
usbmem_pending = false;
|
||||
}
|
||||
|
||||
rlength = strlen(usbmem_response);
|
||||
memcpy(&irp->read.bytes[irp->read.pos], usbmem_response, rlength);
|
||||
irp->read.pos += rlength;
|
||||
|
||||
memset(usbmem_response, 0, sizeof(usbmem_response));
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef DDRHOOK_USBMEM_H
|
||||
#define DDRHOOK_USBMEM_H
|
||||
|
||||
#include "hook/iohook.h"
|
||||
|
||||
void usbmem_init(void);
|
||||
void usbmem_fini(void);
|
||||
HRESULT usbmem_dispatch_irp(struct irp *irp);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user