2018-11-08 01:04:54 +00:00
|
|
|
#include <windows.h>
|
2018-11-23 23:24:41 +00:00
|
|
|
#include <devioctl.h>
|
|
|
|
#include <ntdddisk.h>
|
2018-11-08 01:04:54 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-05-16 00:08:35 +00:00
|
|
|
#include "amex/config.h"
|
2019-03-04 22:22:38 +00:00
|
|
|
#include "amex/ds.h"
|
|
|
|
#include "amex/nvram.h"
|
|
|
|
|
2018-11-08 01:04:54 +00:00
|
|
|
#include "hook/iobuf.h"
|
|
|
|
#include "hook/iohook.h"
|
|
|
|
|
2019-05-14 15:15:20 +00:00
|
|
|
#include "hooklib/setupapi.h"
|
|
|
|
|
2018-11-08 01:04:54 +00:00
|
|
|
#include "util/crc.h"
|
|
|
|
#include "util/dprintf.h"
|
2019-05-13 20:49:18 +00:00
|
|
|
#include "util/str.h"
|
2018-11-08 01:04:54 +00:00
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
enum {
|
|
|
|
DS_IOCTL_SETUP = 0x80006004,
|
|
|
|
DS_IOCTL_READ_SECTOR = 0x80006010,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ds_eeprom {
|
|
|
|
uint32_t crc32;
|
2019-03-04 23:38:37 +00:00
|
|
|
uint8_t unk_04[4];
|
|
|
|
uint8_t region;
|
2018-11-08 01:04:54 +00:00
|
|
|
char serial_no[17];
|
|
|
|
uint8_t unk_1A[6];
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(struct ds_eeprom) == 0x20, "DS EEPROM size");
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
static HRESULT ds_handle_irp(struct irp *irp);
|
|
|
|
static HRESULT ds_handle_open(struct irp *irp);
|
|
|
|
static HRESULT ds_handle_close(struct irp *irp);
|
|
|
|
static HRESULT ds_handle_ioctl(struct irp *irp);
|
|
|
|
|
|
|
|
static HRESULT ds_ioctl_get_geometry(struct irp *irp);
|
|
|
|
static HRESULT ds_ioctl_setup(struct irp *irp);
|
|
|
|
static HRESULT ds_ioctl_read_sector(struct irp *irp);
|
|
|
|
|
|
|
|
static struct ds_eeprom ds_eeprom;
|
|
|
|
static HANDLE ds_fd;
|
|
|
|
|
2019-05-04 17:12:20 +00:00
|
|
|
HRESULT ds_hook_init(const struct ds_config *cfg)
|
2018-11-08 01:04:54 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
2019-05-04 17:12:20 +00:00
|
|
|
assert(cfg != NULL);
|
2018-11-08 01:04:54 +00:00
|
|
|
|
2019-05-04 17:12:20 +00:00
|
|
|
if (!cfg->enable) {
|
|
|
|
return S_FALSE;
|
2018-11-08 01:04:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:12:20 +00:00
|
|
|
memset(&ds_eeprom, 0, sizeof(ds_eeprom));
|
|
|
|
|
|
|
|
wcstombs_s(
|
|
|
|
NULL,
|
|
|
|
ds_eeprom.serial_no,
|
|
|
|
_countof(ds_eeprom.serial_no),
|
|
|
|
cfg->serial_no,
|
|
|
|
_countof(cfg->serial_no) - 1);
|
|
|
|
|
|
|
|
ds_eeprom.region = cfg->region;
|
2018-11-08 01:04:54 +00:00
|
|
|
ds_eeprom.crc32 = crc32(&ds_eeprom.unk_04, 0x1C, 0);
|
|
|
|
|
|
|
|
hr = iohook_push_handler(ds_handle_irp);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = setupapi_add_phantom_dev(&ds_guid, L"$ds");
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ds_fd = iohook_open_dummy_fd();
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_handle_irp(struct irp *irp)
|
|
|
|
{
|
|
|
|
assert(irp != NULL);
|
|
|
|
|
|
|
|
if (irp->op != IRP_OP_OPEN && irp->fd != ds_fd) {
|
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (irp->op) {
|
|
|
|
case IRP_OP_OPEN: return ds_handle_open(irp);
|
|
|
|
case IRP_OP_CLOSE: return ds_handle_close(irp);
|
|
|
|
case IRP_OP_IOCTL: return ds_handle_ioctl(irp);
|
|
|
|
default: return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_handle_open(struct irp *irp)
|
|
|
|
{
|
2019-05-13 20:49:18 +00:00
|
|
|
if (!wstr_eq(irp->open_filename, L"$ds")) {
|
2018-11-08 01:04:54 +00:00
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("DS: Open device\n");
|
|
|
|
irp->fd = ds_fd;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_handle_close(struct irp *irp)
|
|
|
|
{
|
|
|
|
dprintf("DS: Close device\n");
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_handle_ioctl(struct irp *irp)
|
|
|
|
{
|
|
|
|
switch (irp->ioctl) {
|
|
|
|
case IOCTL_DISK_GET_DRIVE_GEOMETRY:
|
|
|
|
return ds_ioctl_get_geometry(irp);
|
|
|
|
|
|
|
|
case DS_IOCTL_SETUP:
|
|
|
|
return ds_ioctl_setup(irp);
|
|
|
|
|
|
|
|
case DS_IOCTL_READ_SECTOR:
|
|
|
|
return ds_ioctl_read_sector(irp);
|
|
|
|
|
|
|
|
default:
|
2018-12-17 22:34:15 +00:00
|
|
|
dprintf("DS: Unknown ioctl %08x, write %i read %i\n",
|
2018-11-08 01:04:54 +00:00
|
|
|
irp->ioctl,
|
2018-12-17 22:34:15 +00:00
|
|
|
(int) irp->write.nbytes,
|
|
|
|
(int) irp->read.nbytes);
|
2018-11-08 01:04:54 +00:00
|
|
|
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_ioctl_get_geometry(struct irp *irp)
|
|
|
|
{
|
2019-10-13 18:48:10 +00:00
|
|
|
DISK_GEOMETRY out;
|
|
|
|
HRESULT hr;
|
2018-11-08 01:04:54 +00:00
|
|
|
|
|
|
|
dprintf("DS: Get geometry\n");
|
|
|
|
|
2019-10-13 18:48:10 +00:00
|
|
|
memset(&out, 0, sizeof(out));
|
|
|
|
out.Cylinders.QuadPart = 1;
|
|
|
|
out.MediaType = 0;
|
|
|
|
out.TracksPerCylinder = 1;
|
|
|
|
out.SectorsPerTrack = 2;
|
|
|
|
out.BytesPerSector = 32;
|
2018-11-08 01:04:54 +00:00
|
|
|
|
2019-10-13 18:48:10 +00:00
|
|
|
hr = iobuf_write(&irp->read, &out, sizeof(out));
|
2018-11-08 01:04:54 +00:00
|
|
|
|
2019-10-13 18:48:10 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("DS: Get geometry failed: %08x\n", (int) hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
2018-11-08 01:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_ioctl_setup(struct irp *irp)
|
|
|
|
{
|
|
|
|
dprintf("DS: Setup IOCTL\n");
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT ds_ioctl_read_sector(struct irp *irp)
|
|
|
|
{
|
|
|
|
struct const_iobuf src;
|
|
|
|
uint32_t sector_no;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
hr = iobuf_read_le32(&irp->write, §or_no);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("DS: Read sector %08x\n", sector_no);
|
|
|
|
|
|
|
|
src.bytes = (const uint8_t *) &ds_eeprom;
|
|
|
|
src.nbytes = sizeof(ds_eeprom);
|
|
|
|
src.pos = 0;
|
|
|
|
|
|
|
|
iobuf_move(&irp->read, &src);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|