ams_mitm: begin skeleton refactor

This commit is contained in:
Michael Scire
2019-11-20 23:58:18 -08:00
committed by SciresM
parent 02d4c97c6d
commit 393596ef9a
105 changed files with 1541 additions and 352 deletions

View File

@@ -1,69 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unordered_map>
#include <switch.h>
#include <stratosphere.hpp>
#include "hid_shim.h"
#include "hid_mitm_service.hpp"
namespace {
std::unordered_map<u64, u64> g_hbl_map;
bool ShouldSetSystemExt(u64 process_id) {
return g_hbl_map.find(process_id) != g_hbl_map.end();
}
void AddSession(u64 process_id) {
if (g_hbl_map.find(process_id) != g_hbl_map.end()) {
g_hbl_map[process_id]++;
} else {
g_hbl_map[process_id] = 0;
}
}
void RemoveSession(u64 process_id) {
if ((--g_hbl_map[process_id]) == 0) {
g_hbl_map.erase(process_id);
}
}
}
HidMitmService::~HidMitmService() {
if (this->should_set_system_ext) {
RemoveSession(this->process_id);
}
}
void HidMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
/* Nothing to do here */
}
Result HidMitmService::SetSupportedNpadStyleSet(u64 applet_resource_user_id, u32 style_set, PidDescriptor pid_desc) {
if (!this->should_set_system_ext && (style_set & (TYPE_SYSTEM | TYPE_SYSTEM_EXT))) {
/* Guaranteed: hbmenu 3.1.1 will cause this to be true. */
/* This prevents setting this for non-HBL. */
this->should_set_system_ext = true;
AddSession(this->process_id);
}
if (ShouldSetSystemExt(this->process_id)) {
style_set |= TYPE_SYSTEM | TYPE_SYSTEM_EXT;
}
return hidSetSupportedNpadStyleSetFwd(this->forward_service.get(), this->process_id, applet_resource_user_id, static_cast<HidControllerType>(style_set));
}

View File

@@ -15,40 +15,28 @@
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "../utils.hpp"
namespace ams::mitm::hid {
class HidMitmService : public IMitmServiceObject {
private:
enum class CommandId {
SetSupportedNpadStyleSet = 100,
};
private:
bool should_set_system_ext = false;
public:
HidMitmService(std::shared_ptr<Service> s, u64 pid, ams::ncm::TitleId tid) : IMitmServiceObject(s, pid, tid) {
/* ... */
}
class HidMitmService : public sf::IMitmServiceObject {
private:
enum class CommandId {
/* TODO */
};
public:
static bool ShouldMitm(os::ProcessId process_id, ncm::ProgramId program_id) {
/* TODO */
return false;
}
public:
SF_MITM_SERVICE_OBJECT_CTOR(HidMitmService) { /* ... */ }
protected:
/* TODO */
public:
DEFINE_SERVICE_DISPATCH_TABLE {
/* TODO */
};
};
~HidMitmService();
static bool ShouldMitm(u64 pid, ams::ncm::TitleId tid) {
/* TODO: Consider removing in Atmosphere 0.10.0/1.0.0. */
/* We will mitm:
* - hbl, to help homebrew not need to be recompiled.
*/
return Utils::IsHblTid(static_cast<u64>(tid));
}
static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx);
protected:
/* Overridden commands. */
Result SetSupportedNpadStyleSet(u64 applet_resource_user_id, u32 style_set, PidDescriptor pid_desc);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(HidMitmService, SetSupportedNpadStyleSet),
};
};
}

View File

@@ -1,64 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <switch.h>
#include "hid_shim.h"
/* Command forwarders. */
Result hidSetSupportedNpadStyleSetFwd(Service* s, u64 process_id, u64 aruid, HidControllerType type) {
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
u32 type;
u64 AppletResourceUserId;
} *raw;
ipcSendPid(&c);
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 100;
raw->type = type;
raw->AppletResourceUserId = aruid;
/* Override Process ID. */
{
u32 *cmd_buf = (u32 *)armGetTls();
cmd_buf[3] = (u32)(process_id >> 0);
cmd_buf[4] = (u32)((process_id >> 32) & 0xFFFF) | 0xFFFE0000ul;
}
Result rc = serviceIpcDispatch(s);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
} *resp = r.Raw;
rc = resp->result;
}
return rc;
}

View File

@@ -1,19 +0,0 @@
/**
* @file hid_shim.h
* @brief Human Interface Devices Services (hid) IPC wrapper.
* @author SciresM
* @copyright libnx Authors
*/
#pragma once
#include <switch.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Command forwarders. */
Result hidSetSupportedNpadStyleSetFwd(Service* s, u64 process_id, u64 aruid, HidControllerType type);
#ifdef __cplusplus
}
#endif

View File

@@ -1,56 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <malloc.h>
#include <switch.h>
#include <atmosphere.h>
#include <stratosphere.hpp>
#include "hidmitm_main.hpp"
#include "hid_mitm_service.hpp"
#include "../utils.hpp"
struct HidMitmManagerOptions {
static const size_t PointerBufferSize = 0x200;
static const size_t MaxDomains = 0x0;
static const size_t MaxDomainObjects = 0x0;
};
using HidMitmManager = WaitableManager<HidMitmManagerOptions>;
void HidMitmMain(void *arg) {
/* This is only necessary on 9.x+ */
if (GetRuntimeFirmwareVersion() < FirmwareVersion_900) {
return;
}
/* Ensure we can talk to HID. */
Utils::WaitHidAvailable();
/* Create server manager */
static auto s_server_manager = HidMitmManager(1);
/* Create hid mitm. */
/* Note: official HID passes 100 as sessions, despite this being > 0x40. */
AddMitmServerToManager<HidMitmService>(&s_server_manager, "hid", 100);
/* Loop forever, servicing our services. */
s_server_manager.Process();
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hidmitm_module.hpp"
#include "hid_mitm_service.hpp"
namespace ams::mitm::hid {
namespace {
constexpr sm::ServiceName MitmServiceName = sm::ServiceName::Encode("hid");
struct ServerOptions {
static constexpr size_t PointerBufferSize = 0x200;
static constexpr size_t MaxDomains = 0;
static constexpr size_t MaxDomainObjects = 0;
};
constexpr size_t MaxServers = 1;
sf::hipc::ServerManager<MaxServers, ServerOptions> g_server_manager;
}
void MitmModule::ThreadFunction(void *arg) {
/* This is only necessary on 9.x+ */
if (hos::GetVersion() < hos::Version_900) {
return;
}
/* Create hid mitm. */
R_ASSERT(g_server_manager.RegisterMitmServer<HidMitmService>(MitmServiceName));
/* Loop forever, servicing our services. */
g_server_manager.LoopProcess();
}
}

View File

@@ -13,12 +13,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../amsmitm_module.hpp"
#include <switch.h>
namespace ams::mitm::hid {
constexpr u32 HidMitmPriority = 47;
constexpr u32 HidMitmStackSize = 0x8000;
DEFINE_MITM_MODULE_CLASS(0x8000, 47);
void HidMitmMain(void *arg);
}