From cc9538964683a4fcca12a855133f96f1f6470b88 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 7 Oct 2020 17:27:23 +0000 Subject: [PATCH] platform: Add basic hwreset implementation for ALLS --- platform/config.c | 11 +++++ platform/config.h | 2 + platform/hwreset.c | 107 +++++++++++++++++++++++++++++++++++++++++++ platform/hwreset.h | 11 +++++ platform/meson.build | 2 + platform/platform.h | 2 + 6 files changed, 135 insertions(+) create mode 100644 platform/hwreset.c create mode 100644 platform/hwreset.h diff --git a/platform/config.c b/platform/config.c index 94b9e40..848cfbd 100644 --- a/platform/config.c +++ b/platform/config.c @@ -14,6 +14,7 @@ #include "platform/config.h" #include "platform/dns.h" #include "platform/hwmon.h" +#include "platform/hwreset.h" #include "platform/misc.h" #include "platform/netenv.h" #include "platform/nusec.h" @@ -30,6 +31,7 @@ void platform_config_load(struct platform_config *cfg, const wchar_t *filename) clock_config_load(&cfg->clock, filename); dns_config_load(&cfg->dns, filename); hwmon_config_load(&cfg->hwmon, filename); + hwreset_config_load(&cfg->hwreset, filename); misc_config_load(&cfg->misc, filename); pcbid_config_load(&cfg->pcbid, filename); netenv_config_load(&cfg->netenv, filename); @@ -117,9 +119,18 @@ void hwmon_config_load(struct hwmon_config *cfg, const wchar_t *filename) cfg->enable = GetPrivateProfileIntW(L"hwmon", L"enable", 1, filename); } +void hwreset_config_load(struct hwreset_config *cfg, const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + cfg->enable = GetPrivateProfileIntW(L"hwreset", L"enable", 1, filename); +} + void misc_config_load(struct misc_config *cfg, const wchar_t *filename) { assert(cfg != NULL); + assert(filename != NULL); cfg->enable = GetPrivateProfileIntW(L"misc", L"enable", 1, filename); } diff --git a/platform/config.h b/platform/config.h index 9aeb5d3..7ece41d 100644 --- a/platform/config.h +++ b/platform/config.h @@ -10,6 +10,7 @@ #include "platform/clock.h" #include "platform/dns.h" #include "platform/hwmon.h" +#include "platform/hwreset.h" #include "platform/misc.h" #include "platform/netenv.h" #include "platform/nusec.h" @@ -25,6 +26,7 @@ void amvideo_config_load(struct amvideo_config *cfg, const wchar_t *filename); void clock_config_load(struct clock_config *cfg, const wchar_t *filename); void dns_config_load(struct dns_config *cfg, const wchar_t *filename); void hwmon_config_load(struct hwmon_config *cfg, const wchar_t *filename); +void hwreset_config_load(struct hwreset_config *cfg, const wchar_t *filename); void misc_config_load(struct misc_config *cfg, const wchar_t *filename); void netenv_config_load(struct netenv_config *cfg, const wchar_t *filename); void nusec_config_load(struct nusec_config *cfg, const wchar_t *filename); diff --git a/platform/hwreset.c b/platform/hwreset.c new file mode 100644 index 0000000..51ec337 --- /dev/null +++ b/platform/hwreset.c @@ -0,0 +1,107 @@ +#include + +#include +#include + +#include "hook/iohook.h" + +#include "platform/hwreset.h" + +#include "util/dprintf.h" +#include "util/str.h" + +enum { + HWRESET_IOCTL_RESTART = 0x80002000, +}; + +static HRESULT hwreset_handle_irp(struct irp *irp); +static HRESULT hwreset_handle_open(struct irp *irp); +static HRESULT hwreset_handle_close(struct irp *irp); +static HRESULT hwreset_handle_ioctl(struct irp *irp); + +static HRESULT hwreset_ioctl_restart(struct irp *irp); + +static HANDLE hwreset_fd; + +HRESULT hwreset_hook_init(const struct hwreset_config *cfg) +{ + HRESULT hr; + + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + hr = iohook_open_nul_fd(&hwreset_fd); + + if (FAILED(hr)) { + return hr; + } + + hr = iohook_push_handler(hwreset_handle_irp); + + if (FAILED(hr)) { + return hr; + } + + return S_OK; +} + +static HRESULT hwreset_handle_irp(struct irp *irp) +{ + assert(irp != NULL); + + if (irp->op != IRP_OP_OPEN && irp->fd != hwreset_fd) { + return iohook_invoke_next(irp); + } + + switch (irp->op) { + case IRP_OP_OPEN: return hwreset_handle_open(irp); + case IRP_OP_CLOSE: return hwreset_handle_close(irp); + case IRP_OP_IOCTL: return hwreset_handle_ioctl(irp); + default: return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION); + } +} + +static HRESULT hwreset_handle_open(struct irp *irp) +{ + if (!wstr_ieq(irp->open_filename, L"\\\\.\\sghwreset")) { + return iohook_invoke_next(irp); + } + + dprintf("Hwreset: Opened device\n"); + irp->fd = hwreset_fd; + + return S_OK; +} + +static HRESULT hwreset_handle_close(struct irp *irp) +{ + dprintf("Hwreset: Closed device\n"); + + return S_OK; +} + +static HRESULT hwreset_handle_ioctl(struct irp *irp) +{ + switch (irp->ioctl) { + case HWRESET_IOCTL_RESTART: + return hwreset_ioctl_restart(irp); + + default: + dprintf("Hwreset: Unknown ioctl %08x, write %i read %i\n", + irp->ioctl, + (int) irp->write.nbytes, + (int) irp->read.nbytes); + + return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION); + } +} + +static HRESULT hwreset_ioctl_restart(struct irp *irp) +{ + dprintf("Hwreset: Reset requested\n"); + + return iobuf_write_le32(&irp->read, 1); +} diff --git a/platform/hwreset.h b/platform/hwreset.h new file mode 100644 index 0000000..9f42615 --- /dev/null +++ b/platform/hwreset.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +#include + +struct hwreset_config { + bool enable; +}; + +HRESULT hwreset_hook_init(const struct hwreset_config *cfg); diff --git a/platform/meson.build b/platform/meson.build index 7e5c544..4f0fbc9 100644 --- a/platform/meson.build +++ b/platform/meson.build @@ -18,6 +18,8 @@ platform_lib = static_library( 'dns.h', 'hwmon.c', 'hwmon.h', + 'hwreset.c', + 'hwreset.h', 'misc.c', 'misc.h', 'netenv.c', diff --git a/platform/platform.h b/platform/platform.h index aaaff51..69c65e2 100644 --- a/platform/platform.h +++ b/platform/platform.h @@ -6,6 +6,7 @@ #include "platform/clock.h" #include "platform/dns.h" #include "platform/hwmon.h" +#include "platform/hwreset.h" #include "platform/misc.h" #include "platform/netenv.h" #include "platform/nusec.h" @@ -17,6 +18,7 @@ struct platform_config { struct clock_config clock; struct dns_config dns; struct hwmon_config hwmon; + struct hwreset_config hwreset; struct misc_config misc; struct pcbid_config pcbid; struct netenv_config netenv;