1
0
mirror of https://gitea.tendokyu.moe/Hay1tsme/segatools.git synced 2024-11-28 11:00:49 +01:00
segatools-hay1tsme/amex/sram.c

161 lines
3.3 KiB
C
Raw Normal View History

2018-11-08 02:03:25 +01:00
#include <windows.h>
2018-11-24 00:24:41 +01:00
#ifdef __GNUC__
#include <ntdef.h>
#else
#include <winnt.h>
#endif
#include <devioctl.h>
#include <ntdddisk.h>
2018-11-23 20:37:42 +01:00
#include <assert.h>
#include "amex/sram.h"
#include "amex/nvram.h"
2018-11-08 02:03:25 +01:00
#include "hook/iohook.h"
2018-11-08 02:03:25 +01:00
#include "hooklib/setupapi.h"
2018-11-08 02:03:25 +01:00
#include "util/dprintf.h"
2019-05-13 22:49:18 +02:00
#include "util/str.h"
2018-11-08 02:03:25 +01:00
2021-09-02 00:52:40 +02:00
enum {
SRAM_IOCTL_GET_ABI_VERSION = 0x80006000,
};
2018-11-08 02:03:25 +01:00
static HRESULT sram_handle_irp(struct irp *irp);
static HRESULT sram_handle_open(struct irp *irp);
static HRESULT sram_handle_close(struct irp *irp);
static HRESULT sram_handle_ioctl(struct irp *irp);
static HRESULT sram_ioctl_get_geometry(struct irp *irp);
2021-09-02 00:52:40 +02:00
static HRESULT sram_ioctl_get_abi_version(struct irp *irp);
2018-11-08 02:03:25 +01:00
static struct sram_config sram_config;
2018-11-08 02:03:25 +01:00
static HANDLE sram_file;
2019-05-04 19:12:20 +02:00
HRESULT sram_hook_init(const struct sram_config *cfg)
2018-11-08 02:03:25 +01:00
{
HRESULT hr;
2019-05-04 19:12:20 +02:00
assert(cfg != NULL);
if (!cfg->enable) {
return S_FALSE;
}
memcpy(&sram_config, cfg, sizeof(*cfg));
2018-11-08 02:03:25 +01:00
hr = iohook_push_handler(sram_handle_irp);
if (FAILED(hr)) {
return hr;
}
hr = setupapi_add_phantom_dev(&sram_guid, L"$sram");
if (FAILED(hr)) {
return hr;
}
return S_OK;
}
static HRESULT sram_handle_irp(struct irp *irp)
{
assert(irp != NULL);
if (irp->op != IRP_OP_OPEN && irp->fd != sram_file) {
return iohook_invoke_next(irp);
}
switch (irp->op) {
case IRP_OP_OPEN: return sram_handle_open(irp);
case IRP_OP_CLOSE: return sram_handle_close(irp);
case IRP_OP_IOCTL: return sram_handle_ioctl(irp);
default: return iohook_invoke_next(irp);
}
}
static HRESULT sram_handle_open(struct irp *irp)
{
HRESULT hr;
2019-05-13 22:49:18 +02:00
if (!wstr_eq(irp->open_filename, L"$sram")) {
2018-11-08 02:03:25 +01:00
return iohook_invoke_next(irp);
}
if (sram_file != NULL) {
dprintf("SRAM: Already open\n");
return HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION);
}
2018-11-08 02:03:25 +01:00
dprintf("SRAM: Open device\n");
hr = nvram_open_file(&sram_file, sram_config.path, 0x80000);
if (FAILED(hr)) {
return hr;
}
2018-11-08 02:03:25 +01:00
irp->fd = sram_file;
return S_OK;
}
static HRESULT sram_handle_close(struct irp *irp)
{
dprintf("SRAM: Close device\n");
sram_file = NULL;
2018-11-08 02:03:25 +01:00
return iohook_invoke_next(irp);
2018-11-08 02:03:25 +01:00
}
static HRESULT sram_handle_ioctl(struct irp *irp)
{
switch (irp->ioctl) {
case IOCTL_DISK_GET_DRIVE_GEOMETRY:
return sram_ioctl_get_geometry(irp);
2021-09-02 00:52:40 +02:00
case SRAM_IOCTL_GET_ABI_VERSION:
return sram_ioctl_get_abi_version(irp);
2018-11-08 02:03:25 +01:00
default:
dprintf("SRAM: Unknown ioctl %x, write %i read %i\n",
irp->ioctl,
2018-12-17 23:34:15 +01:00
(int) irp->write.nbytes,
(int) irp->read.nbytes);
2018-11-08 02:03:25 +01:00
return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
}
}
static HRESULT sram_ioctl_get_geometry(struct irp *irp)
{
DISK_GEOMETRY out;
HRESULT hr;
2018-11-08 02:03:25 +01:00
dprintf("SRAM: Get geometry\n");
memset(&out, 0, sizeof(out));
out.Cylinders.QuadPart = 0x20000;
out.MediaType = 0;
out.TracksPerCylinder = 1;
out.SectorsPerTrack = 1;
out.BytesPerSector = 4;
2018-11-08 02:03:25 +01:00
hr = iobuf_write(&irp->read, &out, sizeof(out));
2018-11-08 02:03:25 +01:00
if (FAILED(hr)) {
dprintf("SRAM: Get geometry failed: %08x\n", (int) hr);
}
return hr;
2018-11-08 02:03:25 +01:00
}
2021-09-02 00:52:40 +02:00
static HRESULT sram_ioctl_get_abi_version(struct irp *irp)
{
return iobuf_write_le16(&irp->read, 256);
}