set.mitm: Split out from fs.mitm

This commit is contained in:
Michael Scire
2018-10-29 22:18:04 -07:00
committed by SciresM
parent 4cdd9aa8f1
commit bbffbd654f
6 changed files with 476 additions and 1 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright (c) 2018 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 <stratosphere.hpp>
#include "setsys_mitm_service.hpp"
extern "C" {
extern u32 __start__;
u32 __nx_applet_type = AppletType_None;
#define INNER_HEAP_SIZE 0x20000
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];
void __libnx_initheap(void);
void __appInit(void);
void __appExit(void);
}
void __libnx_initheap(void) {
void* addr = nx_inner_heap;
size_t size = nx_inner_heap_size;
/* Newlib */
extern char* fake_heap_start;
extern char* fake_heap_end;
fake_heap_start = (char*)addr;
fake_heap_end = (char*)addr + size;
}
void __appInit(void) {
Result rc;
rc = smInitialize();
if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
}
rc = setsysInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
CheckAtmosphereVersion();
}
void __appExit(void) {
/* Cleanup services. */
setsysExit();
smExit();
}
struct SetSysManagerOptions {
static const size_t PointerBufferSize = 0x100;
static const size_t MaxDomains = 4;
static const size_t MaxDomainObjects = 0x100;
};
using SetMitmManager = WaitableManager<SetSysManagerOptions>;
int main(int argc, char **argv)
{
consoleDebugInit(debugDevice_SVC);
/* TODO: What's a good timeout value to use here? */
auto server_manager = new SetMitmManager(1);
/* Create fsp-srv mitm. */
AddMitmServerToManager<SetSysMitmService>(server_manager, "set:sys", 4);
/* Loop forever, servicing our services. */
server_manager->Process();
delete server_manager;
return 0;
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018 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 <mutex>
#include <switch.h>
#include "setsys_mitm_service.hpp"
static HosMutex g_version_mutex;
static bool g_got_version = false;
static SetSysFirmwareVersion g_fw_version = {0};
static Result _GetFirmwareVersion(SetSysFirmwareVersion *out) {
std::scoped_lock<HosMutex> lock(g_version_mutex);
if (!g_got_version) {
Result rc = setsysGetFirmwareVersion(&g_fw_version);
if (R_FAILED(rc)) {
return rc;
}
/* Modify the output firmware version. */
{
u32 major, minor, micro;
char display_version[sizeof(g_fw_version.display_version)] = {0};
GetAtmosphereApiVersion(&major, &minor, &micro, nullptr, nullptr);
snprintf(display_version, sizeof(display_version), "%s (AMS %u.%u.%u)", g_fw_version.display_version, major, minor, micro);
memcpy(g_fw_version.display_version, display_version, sizeof(g_fw_version.display_version));
}
g_got_version = true;
}
*out = g_fw_version;
return 0;
}
void SetSysMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
/* No commands need postprocessing. */
}
Result SetSysMitmService::GetFirmwareVersion(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out) {
Result rc = _GetFirmwareVersion(out.pointer);
/* GetFirmwareVersion sanitizes these fields. */
if (R_SUCCEEDED(rc)) {
out.pointer->revision_major = 0;
out.pointer->revision_minor = 0;
}
return rc;
}
Result SetSysMitmService::GetFirmwareVersion2(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out) {
return _GetFirmwareVersion(out.pointer);
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2018 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/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
enum SetSysCmd : u32 {
SetSysCmd_GetFirmwareVersion = 3,
SetSysCmd_GetFirmwareVersion2 = 4,
};
class SetSysMitmService : public IMitmServiceObject {
public:
SetSysMitmService(std::shared_ptr<Service> s) : IMitmServiceObject(s) {
/* ... */
}
static bool ShouldMitm(u64 pid, u64 tid) {
/* Only MitM qlaunch, maintenance. */
return tid == 0x0100000000001000ULL || tid == 0x0100000000001015ULL;
}
static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx);
protected:
/* Overridden commands. */
Result GetFirmwareVersion(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out);
Result GetFirmwareVersion2(OutPointerWithServerSize<SetSysFirmwareVersion, 0x1> out);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<SetSysCmd_GetFirmwareVersion, &SetSysMitmService::GetFirmwareVersion>(),
MakeServiceCommandMeta<SetSysCmd_GetFirmwareVersion2, &SetSysMitmService::GetFirmwareVersion2>(),
};
};