2018-11-07 22:45:48 +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>
|
|
|
|
|
2019-05-16 02:08:35 +02:00
|
|
|
#include "amex/config.h"
|
2019-03-04 23:22:38 +01:00
|
|
|
#include "amex/eeprom.h"
|
|
|
|
#include "amex/nvram.h"
|
2018-11-07 22:45:48 +01:00
|
|
|
|
2019-03-04 23:22:38 +01:00
|
|
|
#include "hook/iohook.h"
|
2018-11-07 22:45:48 +01:00
|
|
|
|
2019-05-14 17:15:20 +02:00
|
|
|
#include "hooklib/setupapi.h"
|
|
|
|
|
2018-11-07 22:45:48 +01:00
|
|
|
#include "util/dprintf.h"
|
2019-05-13 22:49:18 +02:00
|
|
|
#include "util/str.h"
|
2018-11-07 22:45:48 +01:00
|
|
|
|
|
|
|
static HRESULT eeprom_handle_irp(struct irp *irp);
|
|
|
|
static HRESULT eeprom_handle_open(struct irp *irp);
|
|
|
|
static HRESULT eeprom_handle_close(struct irp *irp);
|
|
|
|
static HRESULT eeprom_handle_ioctl(struct irp *irp);
|
|
|
|
static HRESULT eeprom_handle_read(struct irp *irp);
|
|
|
|
static HRESULT eeprom_handle_write(struct irp *irp);
|
|
|
|
|
|
|
|
static HRESULT eeprom_ioctl_get_geometry(struct irp *irp);
|
|
|
|
|
|
|
|
static HANDLE eeprom_file;
|
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
HRESULT eeprom_hook_init(const struct eeprom_config *cfg)
|
2018-11-07 22:45:48 +01:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
assert(cfg != NULL);
|
|
|
|
|
|
|
|
if (!cfg->enable) {
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = nvram_open_file(&eeprom_file, cfg->path, 0x2000);
|
2018-11-07 22:45:48 +01:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = iohook_push_handler(eeprom_handle_irp);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = setupapi_add_phantom_dev(&eeprom_guid, L"$eeprom");
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_handle_irp(struct irp *irp)
|
|
|
|
{
|
|
|
|
assert(irp != NULL);
|
|
|
|
|
|
|
|
if (irp->op != IRP_OP_OPEN && irp->fd != eeprom_file) {
|
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (irp->op) {
|
|
|
|
case IRP_OP_OPEN: return eeprom_handle_open(irp);
|
|
|
|
case IRP_OP_CLOSE: return eeprom_handle_close(irp);
|
|
|
|
case IRP_OP_IOCTL: return eeprom_handle_ioctl(irp);
|
|
|
|
case IRP_OP_READ: return eeprom_handle_read(irp);
|
|
|
|
case IRP_OP_WRITE: return eeprom_handle_write(irp);
|
|
|
|
default: return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_handle_open(struct irp *irp)
|
|
|
|
{
|
2019-05-13 22:49:18 +02:00
|
|
|
if (!wstr_eq(irp->open_filename, L"$eeprom") != 0) {
|
2018-11-07 22:45:48 +01:00
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("EEPROM: Open device\n");
|
|
|
|
irp->fd = eeprom_file;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_handle_close(struct irp *irp)
|
|
|
|
{
|
|
|
|
dprintf("EEPROM: Close device\n");
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_handle_ioctl(struct irp *irp)
|
|
|
|
{
|
|
|
|
switch (irp->ioctl) {
|
|
|
|
case IOCTL_DISK_GET_DRIVE_GEOMETRY:
|
|
|
|
return eeprom_ioctl_get_geometry(irp);
|
|
|
|
|
|
|
|
default:
|
|
|
|
dprintf("EEPROM: 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-07 22:45:48 +01:00
|
|
|
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_ioctl_get_geometry(struct irp *irp)
|
|
|
|
{
|
2019-10-13 20:48:10 +02:00
|
|
|
DISK_GEOMETRY out;
|
|
|
|
HRESULT hr;
|
2018-11-07 22:45:48 +01:00
|
|
|
|
|
|
|
dprintf("EEPROM: Get geometry\n");
|
|
|
|
|
|
|
|
/* Not the real values, just bullshitting something for now */
|
|
|
|
|
2019-10-13 20:48:10 +02:00
|
|
|
memset(&out, 0, sizeof(out));
|
|
|
|
out.Cylinders.QuadPart = 0x800;
|
|
|
|
out.MediaType = 0;
|
|
|
|
out.TracksPerCylinder = 1;
|
|
|
|
out.SectorsPerTrack = 2;
|
|
|
|
out.BytesPerSector = 4;
|
2018-11-07 22:45:48 +01:00
|
|
|
|
2019-10-13 20:48:10 +02:00
|
|
|
hr = iobuf_write(&irp->read, &out, sizeof(out));
|
2018-11-07 22:45:48 +01:00
|
|
|
|
2019-10-13 20:48:10 +02:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("EEPROM: Get geometry failed: %08x\n", (int) hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
2018-11-07 22:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_handle_read(struct irp *irp)
|
|
|
|
{
|
|
|
|
if (irp->ovl == NULL) {
|
|
|
|
dprintf("EEPROM: Synchronous read..?\n");
|
|
|
|
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("EEPROM: Read off %x len %x\n",
|
|
|
|
(int) irp->ovl->Offset,
|
2018-12-17 23:34:15 +01:00
|
|
|
(int) irp->read.nbytes);
|
2018-11-07 22:45:48 +01:00
|
|
|
|
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT eeprom_handle_write(struct irp *irp)
|
|
|
|
{
|
|
|
|
if (irp->ovl == NULL) {
|
|
|
|
dprintf("EEPROM: Synchronous write..?\n");
|
|
|
|
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("EEPROM: Write off %x len %x\n",
|
|
|
|
(int) irp->ovl->Offset,
|
2018-12-17 23:34:15 +01:00
|
|
|
(int) irp->write.nbytes);
|
2018-11-07 22:45:48 +01:00
|
|
|
|
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|