From aef7d363005a458e995fa18d218941026e075879 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 29 Oct 2018 17:30:39 -0700 Subject: [PATCH] fs.mitm: update for libstratosphere refactor --- stratosphere/fs_mitm/fs_mitm.json | 1 + stratosphere/fs_mitm/source/debug.cpp | 2 - stratosphere/fs_mitm/source/fs_istorage.hpp | 103 +++----- .../fs_mitm/source/fsmitm_layeredrom.hpp | 10 +- stratosphere/fs_mitm/source/fsmitm_main.cpp | 46 ++-- .../fs_mitm/source/fsmitm_romfsbuild.cpp | 4 +- .../fs_mitm/source/fsmitm_romstorage.hpp | 8 - .../fs_mitm/source/fsmitm_service.cpp | 140 ++++------ .../fs_mitm/source/fsmitm_service.hpp | 45 ++-- stratosphere/fs_mitm/source/fsmitm_utils.cpp | 1 - stratosphere/fs_mitm/source/fsmitm_worker.cpp | 53 ---- stratosphere/fs_mitm/source/fsmitm_worker.hpp | 27 -- .../fs_mitm/source/imitmserviceobject.hpp | 45 ---- .../fs_mitm/source/mitm_query_service.cpp | 43 --- .../fs_mitm/source/mitm_query_service.hpp | 75 ------ stratosphere/fs_mitm/source/mitm_server.hpp | 69 ----- stratosphere/fs_mitm/source/mitm_session.hpp | 246 ------------------ .../fs_mitm/source/setsys_mitm_service.cpp | 45 +--- .../fs_mitm/source/setsys_mitm_service.hpp | 41 ++- stratosphere/fs_mitm/source/sm_mitm.c | 190 -------------- stratosphere/fs_mitm/source/sm_mitm.h | 24 -- 21 files changed, 154 insertions(+), 1064 deletions(-) delete mode 100644 stratosphere/fs_mitm/source/fsmitm_worker.cpp delete mode 100644 stratosphere/fs_mitm/source/fsmitm_worker.hpp delete mode 100644 stratosphere/fs_mitm/source/imitmserviceobject.hpp delete mode 100644 stratosphere/fs_mitm/source/mitm_query_service.cpp delete mode 100644 stratosphere/fs_mitm/source/mitm_query_service.hpp delete mode 100644 stratosphere/fs_mitm/source/mitm_server.hpp delete mode 100644 stratosphere/fs_mitm/source/mitm_session.hpp delete mode 100644 stratosphere/fs_mitm/source/sm_mitm.c delete mode 100644 stratosphere/fs_mitm/source/sm_mitm.h diff --git a/stratosphere/fs_mitm/fs_mitm.json b/stratosphere/fs_mitm/fs_mitm.json index a6775155b..aac437f20 100644 --- a/stratosphere/fs_mitm/fs_mitm.json +++ b/stratosphere/fs_mitm/fs_mitm.json @@ -58,6 +58,7 @@ "svcReplyAndReceiveWithUserBuffer": "0x44", "svcCreateEvent": "0x45", "svcCreateInterruptEvent": "0x53", + "svcReadWriteRegister": "0x4E", "svcQueryIoMapping": "0x55", "svcCreateDeviceAddressSpace": "0x56", "svcAttachDeviceAddressSpace": "0x57", diff --git a/stratosphere/fs_mitm/source/debug.cpp b/stratosphere/fs_mitm/source/debug.cpp index 35844b65e..1531eff48 100644 --- a/stratosphere/fs_mitm/source/debug.cpp +++ b/stratosphere/fs_mitm/source/debug.cpp @@ -24,7 +24,5 @@ void Reboot() { } void Log(const void *data, int size) { - (void)(data); - (void)(size); /* ... */ } \ No newline at end of file diff --git a/stratosphere/fs_mitm/source/fs_istorage.hpp b/stratosphere/fs_mitm/source/fs_istorage.hpp index d6827839f..2f21b6b7e 100644 --- a/stratosphere/fs_mitm/source/fs_istorage.hpp +++ b/stratosphere/fs_mitm/source/fs_istorage.hpp @@ -21,21 +21,19 @@ #include "debug.hpp" -enum class FsIStorageCmd { - Read = 0, - Write = 1, - Flush = 2, - SetSize = 3, - GetSize = 4, - OperateRange = 5, +enum FsIStorageCmd : u32 { + FsIStorageCmd_Read = 0, + FsIStorageCmd_Write = 1, + FsIStorageCmd_Flush = 2, + FsIStorageCmd_SetSize = 3, + FsIStorageCmd_GetSize = 4, + FsIStorageCmd_OperateRange = 5, }; class IStorage { public: virtual ~IStorage(); - - virtual IStorage *Clone() = 0; - + virtual Result Read(void *buffer, size_t size, u64 offset) = 0; virtual Result Write(void *buffer, size_t size, u64 offset) = 0; virtual Result Flush() = 0; @@ -51,88 +49,59 @@ class IStorageInterface : public IServiceObject { IStorageInterface(IStorage *s) : base_storage(s) { /* ... */ }; - - IStorageInterface *clone() override { - return new IStorageInterface(this->base_storage->Clone()); - } - + ~IStorageInterface() { delete base_storage; }; - Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) final { - Result rc = 0xF601; - switch ((FsIStorageCmd)cmd_id) { - case FsIStorageCmd::Read: - rc = WrapIpcCommandImpl<&IStorageInterface::read>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - case FsIStorageCmd::Write: - rc = WrapIpcCommandImpl<&IStorageInterface::write>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - case FsIStorageCmd::Flush: - rc = WrapIpcCommandImpl<&IStorageInterface::flush>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - case FsIStorageCmd::SetSize: - rc = WrapIpcCommandImpl<&IStorageInterface::set_size>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - case FsIStorageCmd::GetSize: - rc = WrapIpcCommandImpl<&IStorageInterface::get_size>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - case FsIStorageCmd::OperateRange: - if (kernelAbove400()) { - rc = WrapIpcCommandImpl<&IStorageInterface::operate_range>(this, r, out_c, pointer_buffer, pointer_buffer_size); - } - break; - default: - break; - } - return rc; - }; - - Result handle_deferred() final { - /* TODO: Panic, we can never defer. */ - return 0; - }; private: /* Actual command API. */ - virtual std::tuple read(OutBuffer buffer, u64 offset, u64 size) final { - return {this->base_storage->Read(buffer.buffer, std::min(buffer.num_elements, size), offset)}; + virtual Result Read(OutBuffer buffer, u64 offset, u64 size) final { + return this->base_storage->Read(buffer.buffer, std::min(buffer.num_elements, size), offset); }; - virtual std::tuple write(InBuffer buffer, u64 offset, u64 size) final { - return {this->base_storage->Write(buffer.buffer, std::min(buffer.num_elements, size), offset)}; + virtual Result Write(InBuffer buffer, u64 offset, u64 size) final { + return this->base_storage->Write(buffer.buffer, std::min(buffer.num_elements, size), offset); }; - virtual std::tuple flush() final { - return {this->base_storage->Flush()}; + virtual Result Flush() final { + return this->base_storage->Flush(); }; - virtual std::tuple set_size(u64 size) final { - return {this->base_storage->SetSize(size)}; + virtual Result SetSize(u64 size) final { + return this->base_storage->SetSize(size); }; - virtual std::tuple get_size() final { - u64 out_size = 0; - Result rc = this->base_storage->GetSize(&out_size); - return {rc, out_size}; + virtual Result GetSize(Out size) final { + return this->base_storage->GetSize(size.GetPointer()); }; - virtual std::tuple operate_range(u32 operation_type, u64 offset, u64 size) final { - FsRangeInfo out_range_info = {0}; - Result rc = this->base_storage->OperateRange(operation_type, offset, size, &out_range_info); - return {rc, out_range_info}; + virtual Result OperateRange(Out range_info, u32 operation_type, u64 offset, u64 size) final { + return this->base_storage->OperateRange(operation_type, offset, size, range_info.GetPointer()); + }; + public: + DEFINE_SERVICE_DISPATCH_TABLE { + /* 1.0.0- */ + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + + /* 4.0.0- */ + MakeServiceCommandMeta(), }; }; class IROStorage : public IStorage { public: virtual Result Read(void *buffer, size_t size, u64 offset) = 0; - Result Write(void *buffer, size_t size, u64 offset) final { + virtual Result Write(void *buffer, size_t size, u64 offset) final { (void)(buffer); (void)(offset); (void)(size); return 0x313802; }; - Result Flush() final { + virtual Result Flush() final { return 0x0; }; - Result SetSize(u64 size) final { + virtual Result SetSize(u64 size) final { (void)(size); return 0x313802; }; diff --git a/stratosphere/fs_mitm/source/fsmitm_layeredrom.hpp b/stratosphere/fs_mitm/source/fsmitm_layeredrom.hpp index 3e6061651..01be3af01 100644 --- a/stratosphere/fs_mitm/source/fsmitm_layeredrom.hpp +++ b/stratosphere/fs_mitm/source/fsmitm_layeredrom.hpp @@ -32,16 +32,12 @@ class LayeredRomFS : public IROStorage { /* Information about the merged RomFS. */ u64 title_id; std::shared_ptr> p_source_infos; - - LayeredRomFS *Clone() override { - return new LayeredRomFS(*this); - }; public: LayeredRomFS(std::shared_ptr s_r, std::shared_ptr f_r, u64 tid); virtual ~LayeredRomFS() = default; - Result Read(void *buffer, size_t size, u64 offset) override; - Result GetSize(u64 *out_size) override; - Result OperateRange(u32 operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) override; + virtual Result Read(void *buffer, size_t size, u64 offset) override; + virtual Result GetSize(u64 *out_size) override; + virtual Result OperateRange(u32 operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) override; }; diff --git a/stratosphere/fs_mitm/source/fsmitm_main.cpp b/stratosphere/fs_mitm/source/fsmitm_main.cpp index 12b2e75c3..2d96ce78e 100644 --- a/stratosphere/fs_mitm/source/fsmitm_main.cpp +++ b/stratosphere/fs_mitm/source/fsmitm_main.cpp @@ -22,13 +22,7 @@ #include #include -#include "sm_mitm.h" - -#include "mitm_server.hpp" #include "fsmitm_service.hpp" -#include "fsmitm_worker.hpp" - -#include "mitm_query_service.hpp" #include "fsmitm_utils.hpp" @@ -88,37 +82,34 @@ void __appExit(void) { smExit(); } +struct FsMitmManagerOptions { + static const size_t PointerBufferSize = 0x800; + static const size_t MaxDomains = 0x10; + static const size_t MaxDomainObjects = 0x4000; +}; + +using FsMitmManager = WaitableManager; + void CreateSettingsMitMServer(void *arg) { - MultiThreadedWaitableManager *server_manager = (MultiThreadedWaitableManager *)arg; + auto server_manager = (FsMitmManager *)arg; Result rc; if (R_FAILED((rc = setsysInitialize()))) { fatalSimple(rc); } - ISession> *setsys_query_srv = NULL; - MitMServer *setsys_srv = new MitMServer(&setsys_query_srv, "set:sys", 60); - server_manager->add_waitable(setsys_srv); - server_manager->add_waitable(setsys_query_srv); + AddMitmServerToManager(server_manager, "set:sys", 5); svcExitThread(); } int main(int argc, char **argv) { - Thread worker_thread = {0}; Thread sd_initializer_thread = {0}; Thread hid_initializer_thread = {0}; Thread set_mitm_setup_thread = {0}; consoleDebugInit(debugDevice_SVC); - if (R_FAILED(threadCreate(&worker_thread, &FsMitMWorker::Main, NULL, 0x20000, 45, 0))) { - /* TODO: Panic. */ - } - if (R_FAILED(threadStart(&worker_thread))) { - /* TODO: Panic. */ - } - if (R_FAILED(threadCreate(&sd_initializer_thread, &Utils::InitializeSdThreadFunc, NULL, 0x4000, 0x15, 0))) { /* TODO: Panic. */ } @@ -134,24 +125,21 @@ int main(int argc, char **argv) } /* TODO: What's a good timeout value to use here? */ - MultiThreadedWaitableManager *server_manager = new MultiThreadedWaitableManager(5, U64_MAX, 0x20000); + auto server_manager = new FsMitmManager(1); /* Create fsp-srv mitm. */ - ISession> *fs_query_srv = NULL; - MitMServer *fs_srv = new MitMServer(&fs_query_srv, "fsp-srv", 61); - server_manager->add_waitable(fs_srv); - server_manager->add_waitable(fs_query_srv); + AddMitmServerToManager(server_manager, "fsp-srv", 61); /* Create set:sys mitm server, delayed until set:sys is available. */ - if (R_FAILED(threadCreate(&set_mitm_setup_thread, &CreateSettingsMitMServer, server_manager, 0x4000, 0x15, 0))) { + //if (R_FAILED(threadCreate(&set_mitm_setup_thread, &CreateSettingsMitMServer, server_manager, 0x4000, 0x15, 0))) { /* TODO: Panic. */ - } - if (R_FAILED(threadStart(&set_mitm_setup_thread))) { + //} + //if (R_FAILED(threadStart(&set_mitm_setup_thread))) { /* TODO: Panic. */ - } + //} /* Loop forever, servicing our services. */ - server_manager->process(); + server_manager->Process(); delete server_manager; diff --git a/stratosphere/fs_mitm/source/fsmitm_romfsbuild.cpp b/stratosphere/fs_mitm/source/fsmitm_romfsbuild.cpp index 3a1afb780..4a01bff77 100644 --- a/stratosphere/fs_mitm/source/fsmitm_romfsbuild.cpp +++ b/stratosphere/fs_mitm/source/fsmitm_romfsbuild.cpp @@ -400,7 +400,7 @@ void RomFSBuildContext::Build(std::vector *out_infos) { header->file_table_ofs = header->file_hash_table_ofs + header->file_hash_table_size; /* For debugging, uncomment this to get a log of the generated metadata tables. */ - /* + { FsFileSystem sd_fs; if (R_SUCCEEDED(fsMountSdcard(&sd_fs))) { @@ -415,7 +415,7 @@ void RomFSBuildContext::Build(std::vector *out_infos) { fsFsClose(&sd_fs); } } - */ + out_infos->emplace_back(header->dir_hash_table_ofs, this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size, metadata, RomFSDataSource::Memory); } diff --git a/stratosphere/fs_mitm/source/fsmitm_romstorage.hpp b/stratosphere/fs_mitm/source/fsmitm_romstorage.hpp index cc378cd1f..c421a2147 100644 --- a/stratosphere/fs_mitm/source/fsmitm_romstorage.hpp +++ b/stratosphere/fs_mitm/source/fsmitm_romstorage.hpp @@ -35,10 +35,6 @@ class RomFileStorage : public IROStorage { fsFileClose(base_file); delete base_file; }; - - RomFileStorage *Clone() override { - return new RomFileStorage(this->base_file); - }; public: Result Read(void *buffer, size_t size, u64 offset) override { size_t out_sz = 0; @@ -72,10 +68,6 @@ class RomInterfaceStorage : public IROStorage { fsStorageClose(base_storage); delete base_storage; }; - - RomInterfaceStorage *Clone() override { - return new RomInterfaceStorage(this->base_storage); - }; public: Result Read(void *buffer, size_t size, u64 offset) override { return fsStorageRead(this->base_storage, offset, buffer, size); diff --git a/stratosphere/fs_mitm/source/fsmitm_service.cpp b/stratosphere/fs_mitm/source/fsmitm_service.cpp index d139cc328..da19d5778 100644 --- a/stratosphere/fs_mitm/source/fsmitm_service.cpp +++ b/stratosphere/fs_mitm/source/fsmitm_service.cpp @@ -18,81 +18,41 @@ #include "fsmitm_service.hpp" #include "fs_shim.h" -#include "fsmitm_worker.hpp" #include "fsmitm_utils.hpp" #include "fsmitm_romstorage.hpp" #include "fsmitm_layeredrom.hpp" -#include "mitm_query_service.hpp" #include "debug.hpp" -Result FsMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { - Result rc = 0xF601; - if (this->has_initialized) { - switch (static_cast(cmd_id)) { - case FspSrvCmd::OpenDataStorageByCurrentProcess: - rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_current_process>(this, r, out_c, pointer_buffer, pointer_buffer_size); +void FsMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) { + auto this_ptr = static_cast(obj); + switch ((FspSrvCmd)ctx->cmd_id) { + case FspSrvCmd_SetCurrentProcess: + if (R_SUCCEEDED(ctx->rc)) { + this_ptr->has_initialized = true; + this_ptr->process_id = ctx->request.Pid; + this_ptr->title_id = this_ptr->process_id; + if (R_FAILED(MitmQueryUtils::GetAssociatedTidForPid(this_ptr->process_id, &this_ptr->title_id))) { + /* Log here, if desired. */ + } break; - case FspSrvCmd::OpenDataStorageByDataId: - rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_data_id>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - default: - break; - } - } else { - if (static_cast(cmd_id) == FspSrvCmd::SetCurrentProcess) { - if (r.HasPid) { - this->init_pid = r.Pid; } - } - } - return rc; -} - -void FsMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { - struct { - u64 magic; - u64 result; - } *resp = (decltype(resp))r.Raw; - - u64 *tls = (u64 *)armGetTls(); - std::array backup_tls; - std::copy(tls, tls + backup_tls.size(), backup_tls.begin()); - - Result rc = (Result)resp->result; - switch (static_cast(cmd_id)) { - case FspSrvCmd::SetCurrentProcess: - if (R_SUCCEEDED(rc)) { - this->has_initialized = true; - } - this->process_id = this->init_pid; - this->title_id = this->process_id; - if (R_FAILED(MitMQueryUtils::get_associated_tid_for_pid(this->process_id, &this->title_id))) { - /* Log here, if desired. */ - } - std::copy(backup_tls.begin(), backup_tls.end(), tls); break; default: break; } - resp->result = rc; -} - -Result FsMitMService::handle_deferred() { - /* This service is never deferrable. */ - return 0; } /* Add redirection for RomFS to the SD card. */ -std::tuple> FsMitMService::open_data_storage_by_current_process() { - IPCSession *out_session = NULL; - std::shared_ptr out_storage = nullptr; +Result FsMitmService::OpenDataStorageByCurrentProcess(Out> out_storage) { + std::shared_ptr storage = nullptr; u32 out_domain_id = 0; - Result rc; + Result rc = 0; + if (this->romfs_storage != nullptr) { - if (this->get_owner() != NULL) { + if (out_storage.IsDomain()) { FsStorage s = {0}; - rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &s); + rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service.get(), &s); if (R_SUCCEEDED(rc)) { out_domain_id = s.s.object_id; } @@ -100,64 +60,78 @@ std::tuple> FsMitMService::open_data_stora rc = 0; } if (R_SUCCEEDED(rc)) { - out_storage = this->romfs_storage; - out_session = new IPCSession(out_storage); + storage = this->romfs_storage; } } else { FsStorage data_storage; FsFile data_file; - rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &data_storage); + rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service.get(), &data_storage); Log(armGetTls(), 0x100); if (R_SUCCEEDED(rc)) { /* TODO: Is there a sensible path that ends in ".romfs" we can use?" */ if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(this->title_id, "romfs.bin", FS_OPEN_READ, &data_file))) { - out_storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), this->title_id)); + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), this->title_id)); } else { - out_storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, this->title_id)); + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, this->title_id)); } - this->romfs_storage = out_storage; - out_session = new IPCSession(out_storage); - if (this->get_owner() == NULL) { - FsMitMWorker::AddWaitable(out_session); - } else { + this->romfs_storage = storage; + if (out_storage.IsDomain()) { out_domain_id = data_storage.s.object_id; } } } - OutSession out_s = OutSession(out_session); - out_s.domain_id = out_domain_id; - return {rc, out_s}; + if (R_SUCCEEDED(rc)) { + out_storage.SetValue(std::move(storage)); + if (out_storage.IsDomain()) { + out_storage.ChangeObjectId(out_domain_id); + } + } + + return rc; } /* Add redirection for System Data Archives to the SD card. */ -std::tuple> FsMitMService::open_data_storage_by_data_id(u64 sid, u64 data_id) { +Result FsMitmService::OpenDataStorageByDataId(Out> out_storage, u64 sid, u64 data_id) { FsStorageId storage_id = (FsStorageId)sid; - IPCSession *out_session = NULL; FsStorage data_storage; FsFile data_file; - u32 out_domain_id = 0; - Result rc; - rc = fsOpenDataStorageByDataIdFwd(this->forward_service, storage_id, data_id, &data_storage); + std::shared_ptr storage = nullptr; + u32 out_domain_id = 0; + Result rc = 0; + + ON_SCOPE_EXIT { + if (R_SUCCEEDED(rc)) { + out_storage.SetValue(std::move(storage)); + if (out_storage.IsDomain()) { + out_storage.ChangeObjectId(out_domain_id); + } + } + }; + + rc = fsOpenDataStorageByDataIdFwd(this->forward_service.get(), storage_id, data_id, &data_storage); if (R_SUCCEEDED(rc)) { /* TODO: Is there a sensible path that ends in ".romfs" we can use?" */ if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(data_id, "romfs.bin", FS_OPEN_READ, &data_file))) { - out_session = new IPCSession(std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), data_id))); + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), data_id)); } else { - out_session = new IPCSession(std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, data_id))); + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, data_id)); } - if (this->get_owner() == NULL) { - FsMitMWorker::AddWaitable(out_session); - } else { + if (out_storage.IsDomain()) { out_domain_id = data_storage.s.object_id; } } - OutSession out_s = OutSession(out_session); - out_s.domain_id = out_domain_id; - return {rc, out_s}; + if (R_SUCCEEDED(rc)) { + out_storage.SetValue(std::move(storage)); + if (out_storage.IsDomain()) { + out_storage.ChangeObjectId(out_domain_id); + } + } + + return rc; } \ No newline at end of file diff --git a/stratosphere/fs_mitm/source/fsmitm_service.hpp b/stratosphere/fs_mitm/source/fsmitm_service.hpp index 0dfa441e3..c1bf4e9a8 100644 --- a/stratosphere/fs_mitm/source/fsmitm_service.hpp +++ b/stratosphere/fs_mitm/source/fsmitm_service.hpp @@ -16,52 +16,41 @@ #pragma once #include -#include -#include "imitmserviceobject.hpp" +#include #include "fs_istorage.hpp" #include "fsmitm_utils.hpp" -enum class FspSrvCmd { - SetCurrentProcess = 1, - OpenDataStorageByCurrentProcess = 200, - OpenDataStorageByDataId = 202, +enum FspSrvCmd : u32 { + FspSrvCmd_SetCurrentProcess = 1, + FspSrvCmd_OpenDataStorageByCurrentProcess = 200, + FspSrvCmd_OpenDataStorageByDataId = 202, }; -class FsMitMService : public IMitMServiceObject { +class FsMitmService : public IMitmServiceObject { private: bool has_initialized = false; - u64 init_pid = 0; std::shared_ptr romfs_storage; public: - FsMitMService(Service *s) : IMitMServiceObject(s) { + FsMitmService(std::shared_ptr s) : IMitmServiceObject(s) { /* ... */ } - static bool should_mitm(u64 pid, u64 tid) { + static bool ShouldMitm(u64 pid, u64 tid) { if (Utils::HasSdDisableMitMFlag(tid)) { return false; } return (tid >= 0x0100000000010000ULL || Utils::HasSdMitMFlag(tid)) && Utils::HasOverrideButton(tid); } - FsMitMService *clone() override { - auto new_srv = new FsMitMService((Service *)&this->forward_service); - this->clone_to(new_srv); - return new_srv; - } - - void clone_to(void *o) override { - FsMitMService *other = (FsMitMService *)o; - other->has_initialized = has_initialized; - other->init_pid = init_pid; - } - - virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size); - virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size); - virtual Result handle_deferred(); - + static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx); + protected: /* Overridden commands. */ - std::tuple> open_data_storage_by_current_process(); - std::tuple> open_data_storage_by_data_id(u64 storage_id, u64 data_id); + Result OpenDataStorageByCurrentProcess(Out> out); + Result OpenDataStorageByDataId(Out> out, u64 storage_id, u64 data_id); + public: + DEFINE_SERVICE_DISPATCH_TABLE { + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + }; }; diff --git a/stratosphere/fs_mitm/source/fsmitm_utils.cpp b/stratosphere/fs_mitm/source/fsmitm_utils.cpp index 127fa7a24..aa22bb5c3 100644 --- a/stratosphere/fs_mitm/source/fsmitm_utils.cpp +++ b/stratosphere/fs_mitm/source/fsmitm_utils.cpp @@ -20,7 +20,6 @@ #include #include -#include "sm_mitm.h" #include "debug.hpp" #include "fsmitm_utils.hpp" #include "ini.h" diff --git a/stratosphere/fs_mitm/source/fsmitm_worker.cpp b/stratosphere/fs_mitm/source/fsmitm_worker.cpp deleted file mode 100644 index 24cba1168..000000000 --- a/stratosphere/fs_mitm/source/fsmitm_worker.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#include "fsmitm_worker.hpp" - -static SystemEvent *g_new_waitable_event = NULL; - -static HosMutex g_new_waitable_mutex; -static HosSemaphore g_sema_new_waitable_finish; - -static std::unique_ptr g_worker_waiter; - -Result FsMitMWorker::AddWaitableCallback(void *arg, Handle *handles, size_t num_handles, u64 timeout) { - (void)arg; - svcClearEvent(handles[0]); - g_sema_new_waitable_finish.Signal(); - return 0; -} - -void FsMitMWorker::AddWaitable(IWaitable *waitable) { - g_worker_waiter->add_waitable(waitable); - std::scoped_lock lk{g_new_waitable_mutex}; - g_new_waitable_event->signal_event(); - g_sema_new_waitable_finish.Wait(); -} - -void FsMitMWorker::Main(void *arg) { - /* Initialize waitable event. */ - g_new_waitable_event = new SystemEvent(NULL, &FsMitMWorker::AddWaitableCallback); - - /* Make a new waitable manager. */ - g_worker_waiter = std::make_unique(U64_MAX); - g_worker_waiter->add_waitable(g_new_waitable_event); - - /* Service processes. */ - g_worker_waiter->process(); -} diff --git a/stratosphere/fs_mitm/source/fsmitm_worker.hpp b/stratosphere/fs_mitm/source/fsmitm_worker.hpp deleted file mode 100644 index 68c5ea4e4..000000000 --- a/stratosphere/fs_mitm/source/fsmitm_worker.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 . - */ - -#pragma once -#include -#include - -class FsMitMWorker { - private: - static Result AddWaitableCallback(void *arg, Handle *handles, size_t num_handles, u64 timeout); - public: - static void Main(void *arg); - static void AddWaitable(IWaitable *waitable); -}; \ No newline at end of file diff --git a/stratosphere/fs_mitm/source/imitmserviceobject.hpp b/stratosphere/fs_mitm/source/imitmserviceobject.hpp deleted file mode 100644 index 8952903e9..000000000 --- a/stratosphere/fs_mitm/source/imitmserviceobject.hpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 . - */ - -#pragma once -#include -#include - -#include - -#include "debug.hpp" - -class IMitMServiceObject : public IServiceObject { - protected: - Service *forward_service; - u64 process_id = 0; - u64 title_id = 0; - public: - IMitMServiceObject(Service *s) : forward_service(s) { - - } - - static bool should_mitm(u64 pid, u64 tid) { - return true; - } - - virtual void clone_to(void *o) = 0; - protected: - virtual ~IMitMServiceObject() = default; - virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0; - virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0; - virtual Result handle_deferred() = 0; -}; diff --git a/stratosphere/fs_mitm/source/mitm_query_service.cpp b/stratosphere/fs_mitm/source/mitm_query_service.cpp deleted file mode 100644 index e4d88d076..000000000 --- a/stratosphere/fs_mitm/source/mitm_query_service.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#include "mitm_query_service.hpp" - -static std::vector g_known_pids; -static std::vector g_known_tids; -static HosMutex g_pid_tid_mutex; - -Result MitMQueryUtils::get_associated_tid_for_pid(u64 pid, u64 *tid) { - Result rc = 0xCAFE; - std::scoped_lock lk{g_pid_tid_mutex}; - for (unsigned int i = 0; i < g_known_pids.size(); i++) { - if (g_known_pids[i] == pid) { - *tid = g_known_tids[i]; - rc = 0x0; - break; - } - } - return rc; -} - -void MitMQueryUtils::associate_pid_to_tid(u64 pid, u64 tid) { - std::scoped_lock lk{g_pid_tid_mutex}; - g_known_pids.push_back(pid); - g_known_tids.push_back(tid); -} diff --git a/stratosphere/fs_mitm/source/mitm_query_service.hpp b/stratosphere/fs_mitm/source/mitm_query_service.hpp deleted file mode 100644 index a1153c496..000000000 --- a/stratosphere/fs_mitm/source/mitm_query_service.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 . - */ - -#pragma once -#include -#include - -#include "debug.hpp" - -enum MitMQueryServiceCommand { - MQS_Cmd_ShouldMitm = 65000, - MQS_Cmd_AssociatePidTid = 65001 -}; - -namespace MitMQueryUtils { - Result get_associated_tid_for_pid(u64 pid, u64 *tid); - - void associate_pid_to_tid(u64 pid, u64 tid); -} - - -template -class MitMQueryService : public IServiceObject { - public: - MitMQueryService *clone() override { - return new MitMQueryService(); - } - Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) override { - Log(armGetTls(), 0x100); - switch (cmd_id) { - case MQS_Cmd_ShouldMitm: - return WrapIpcCommandImpl<&MitMQueryService::should_mitm>(this, r, out_c, pointer_buffer, pointer_buffer_size); - case MQS_Cmd_AssociatePidTid: - return WrapIpcCommandImpl<&MitMQueryService::associate_pid_tid>(this, r, out_c, pointer_buffer, pointer_buffer_size); - default: - return 0xF601; - } - if (cmd_id == 65000) { - - } else { - return 0xF601; - } - } - Result handle_deferred() override { - /* This service is never deferrable. */ - return 0; - } - - protected: - std::tuple should_mitm(u64 pid) { - u64 should_mitm = 0; - u64 tid = 0; - if (R_SUCCEEDED(MitMQueryUtils::get_associated_tid_for_pid(pid, &tid))) { - should_mitm = T::should_mitm(pid, tid); - } - return {0, should_mitm}; - } - std::tuple associate_pid_tid(u64 pid, u64 tid) { - MitMQueryUtils::associate_pid_to_tid(pid, tid); - return {0x0}; - } -}; \ No newline at end of file diff --git a/stratosphere/fs_mitm/source/mitm_server.hpp b/stratosphere/fs_mitm/source/mitm_server.hpp deleted file mode 100644 index 4054dd9ac..000000000 --- a/stratosphere/fs_mitm/source/mitm_server.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 . - */ - -#pragma once -#include -#include - -#include "mitm_query_service.hpp" -#include "sm_mitm.h" -#include "mitm_session.hpp" - -#include "debug.hpp" - -template -class MitMSession; - -template -class MitMServer final : public IServer { - static_assert(std::is_base_of::value, "Service Objects must derive from IServiceObject"); - private: - char mitm_name[9]; - - public: - MitMServer(ISession> **out_query_session, const char *service_name, unsigned int max_s, bool s_d = false) : IServer(service_name, max_s, s_d) { - Handle tmp_hnd; - Handle out_query_h; - Result rc; - - if (R_SUCCEEDED((rc = smGetServiceOriginal(&tmp_hnd, smEncodeName(service_name))))) { - svcCloseHandle(tmp_hnd); - } else { - fatalSimple(rc); - } - strncpy(mitm_name, service_name, 8); - mitm_name[8] = '\x00'; - if (R_FAILED((rc = smMitMInstall(&this->port_handle, &out_query_h, mitm_name)))) { - fatalSimple(rc); - } - *out_query_session = new ServiceSession>(NULL, out_query_h, 0); - } - - virtual ~MitMServer() { - if (this->port_handle) { - if (R_FAILED(smMitMUninstall(this->mitm_name))) { - /* TODO: Panic. */ - } - /* svcCloseHandle(port_handle); was called by ~IServer. */ - } - } - - ISession *get_new_session(Handle session_h) override { - return new MitMSession(this, session_h, 0, mitm_name); - } -}; - - diff --git a/stratosphere/fs_mitm/source/mitm_session.hpp b/stratosphere/fs_mitm/source/mitm_session.hpp deleted file mode 100644 index c49915e69..000000000 --- a/stratosphere/fs_mitm/source/mitm_session.hpp +++ /dev/null @@ -1,246 +0,0 @@ -/* - * 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 . - */ - -#pragma once -#include -#include -#include "imitmserviceobject.hpp" - -#include "mitm_query_service.hpp" -#include "mitm_server.hpp" -#include "fsmitm_worker.hpp" - -#include "debug.hpp" - -template -class MitMServer; - -template -class MitMSession final : public ISession { - static_assert(std::is_base_of::value, "MitM Service Objects must derive from IMitMServiceObject"); - - /* This will be for the actual session. */ - Service forward_service; - IpcParsedCommand cur_out_r; - u32 mitm_domain_id = 0; - bool got_first_message; - - public: - MitMSession(MitMServer *s, Handle s_h, Handle c_h, const char *srv) : ISession(s, s_h, c_h, NULL, 0), got_first_message(false) { - this->server = s; - this->server_handle = s_h; - this->client_handle = c_h; - if (R_FAILED(smMitMGetService(&forward_service, srv))) { - /* TODO: Panic. */ - } - size_t pointer_buffer_size = 0; - if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) { - /* TODO: Panic. */ - } - this->service_object = std::make_shared(&forward_service); - this->pointer_buffer.resize(pointer_buffer_size); - } - MitMSession(MitMServer *s, Handle s_h, Handle c_h, Handle f_h) : ISession(s, s_h, c_h, NULL, 0), got_first_message(true) { - this->server = s; - this->server_handle = s_h; - this->client_handle = c_h; - serviceCreate(&this->forward_service, f_h); - size_t pointer_buffer_size = 0; - if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) { - /* TODO: Panic. */ - } - this->service_object = std::make_shared(&forward_service); - this->pointer_buffer.resize(pointer_buffer_size); - } - - virtual ~MitMSession() { - serviceClose(&forward_service); - } - - Result handle_message(IpcParsedCommand &r) override { - IpcCommand c; - ipcInitialize(&c); - u64 cmd_id = ((u32 *)r.Raw)[2]; - Result retval = 0xF601; - - cur_out_r.NumHandles = 0; - - Log(armGetTls(), 0x100); - - u32 *cmdbuf = (u32 *)armGetTls(); - if (r.CommandType == IpcCommandType_Close) { - Reboot(); - } - - if (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext) { - std::shared_ptr obj; - if (r.IsDomainRequest) { - obj = this->domain->get_domain_object(r.InThisObjectId); - if (obj != nullptr && r.InMessageType == DomainMessageType_Close) { - if (r.InThisObjectId == this->mitm_domain_id) { - Reboot(); - } - this->domain->delete_object(r.InThisObjectId); - struct { - u64 magic; - u64 result; - } *o_resp; - - o_resp = (decltype(o_resp)) ipcPrepareHeaderForDomain(&c, sizeof(*o_resp), 0); - *(DomainResponseHeader *)((uintptr_t)o_resp - sizeof(DomainResponseHeader)) = {0}; - o_resp->magic = SFCO_MAGIC; - o_resp->result = 0x0; - Log(armGetTls(), 0x100); - return o_resp->result; - } - } else { - obj = this->service_object; - } - if (obj != nullptr) { - retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size()); - if (R_SUCCEEDED(retval)) { - if (r.IsDomainRequest) { - /* We never work with out object ids, so this should be fine. */ - ipcParseDomainResponse(&cur_out_r, 0); - } else { - ipcParse(&cur_out_r); - } - return retval; - } - } - } else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) { - /* Ipc Clone Current Object. */ - retval = serviceIpcDispatch(&forward_service); - Log(armGetTls(), 0x100); - if (R_SUCCEEDED(retval)) { - ipcParse(&cur_out_r); - struct { - u64 magic; - u64 result; - } *resp = (decltype(resp))cur_out_r.Raw; - retval = resp->result; - if ((cmd_id == IpcCtrl_Cmd_CloneCurrentObject || cmd_id == IpcCtrl_Cmd_CloneCurrentObjectEx)) { - if (R_SUCCEEDED(retval)) { - Handle s_h; - Handle c_h; - Result rc; - if (R_FAILED((rc = svcCreateSession(&s_h, &c_h, 0, 0)))) { - fatalSimple(rc); - } - - if (cur_out_r.NumHandles != 1) { - Reboot(); - } - - MitMSession *new_sess = new MitMSession((MitMServer *)this->server, s_h, c_h, cur_out_r.Handles[0]); - new_sess->service_object = this->service_object; - - if (this->is_domain) { - new_sess->is_domain = true; - new_sess->domain = this->domain; - new_sess->mitm_domain_id = this->mitm_domain_id; - new_sess->forward_service.type = this->forward_service.type; - new_sess->forward_service.object_id = this->forward_service.object_id; - } - this->get_manager()->add_waitable(new_sess); - ipcSendHandleMove(&c, c_h); - struct { - u64 magic; - u64 result; - } *o_resp; - - o_resp = (decltype(o_resp)) ipcPrepareHeader(&c, sizeof(*o_resp)); - o_resp->magic = SFCO_MAGIC; - o_resp->result = 0x0; - } - } - } - Log(armGetTls(), 0x100); - return retval; - } - - /* 0xF601 --> Dispatch onwards. */ - if (retval == 0xF601) { - /* Patch PID Descriptor, if relevant. */ - if (r.HasPid) { - /* [ctrl 0] [ctrl 1] [handle desc 0] [pid low] [pid high] */ - cmdbuf[4] = 0xFFFE0000UL | (cmdbuf[4] & 0xFFFFUL); - } - - Log(armGetTls(), 0x100); - retval = serviceIpcDispatch(&forward_service); - if (R_SUCCEEDED(retval)) { - if (r.IsDomainRequest) { - /* We never work with out object ids, so this should be fine. */ - ipcParseDomainResponse(&cur_out_r, 0); - } else { - ipcParse(&cur_out_r); - } - - struct { - u64 magic; - u64 result; - } *resp = (decltype(resp))cur_out_r.Raw; - - retval = resp->result; - } - } - - Log(armGetTls(), 0x100); - Log(&cmd_id, sizeof(u64)); - u64 retval_for_log = retval; - Log(&retval_for_log, sizeof(u64)); - if (R_FAILED(retval)) { - //Reboot(); - } - - return retval; - } - - void postprocess(IpcParsedCommand &r, u64 cmd_id) override { - if (this->active_object == this->service_object && (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext)) { - IpcCommand c; - ipcInitialize(&c); - this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size()); - } else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) { - if (cmd_id == IpcCtrl_Cmd_ConvertCurrentObjectToDomain) { - this->is_domain = true; - this->domain = std::make_shared(); - struct { - u64 magic; - u64 result; - u32 domain_id; - } *resp = (decltype(resp))cur_out_r.Raw; - Result rc; - if (R_FAILED((rc = this->domain->set_object(this->service_object, resp->domain_id)))) { - fatalSimple(rc); - } - this->mitm_domain_id = resp->domain_id; - this->forward_service.type = ServiceType_Domain; - this->forward_service.object_id = resp->domain_id; - } - } - } - - void cleanup() override { - /* Clean up copy handles. */ - for (unsigned int i = 0; i < cur_out_r.NumHandles; i++) { - if (cur_out_r.WasHandleCopied[i]) { - svcCloseHandle(cur_out_r.Handles[i]); - } - } - } -}; diff --git a/stratosphere/fs_mitm/source/setsys_mitm_service.cpp b/stratosphere/fs_mitm/source/setsys_mitm_service.cpp index 2d702f93c..a8bb5624a 100644 --- a/stratosphere/fs_mitm/source/setsys_mitm_service.cpp +++ b/stratosphere/fs_mitm/source/setsys_mitm_service.cpp @@ -18,7 +18,6 @@ #include #include "setsys_mitm_service.hpp" -#include "mitm_query_service.hpp" #include "debug.hpp" static HosMutex g_version_mutex; @@ -51,54 +50,20 @@ static Result _GetFirmwareVersion(SetSysFirmwareVersion *out) { return 0; } -Result SetSysMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { - Result rc = 0xF601; - - switch (static_cast(cmd_id)) { - case SetSysCmd::GetFirmwareVersion: - rc = WrapIpcCommandImpl<&SetSysMitMService::get_firmware_version>(this, r, out_c, pointer_buffer, pointer_buffer_size); - break; - case SetSysCmd::GetFirmwareVersion2: - if (kernelAbove300()) { - rc = WrapIpcCommandImpl<&SetSysMitMService::get_firmware_version2>(this, r, out_c, pointer_buffer, pointer_buffer_size); - } - break; - default: - break; - } - - return rc; -} - -void SetSysMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { +void SetSysMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) { /* No commands need postprocessing. */ } -Result SetSysMitMService::handle_deferred() { - /* This service is never deferrable. */ - return 0; -} - -std::tuple SetSysMitMService::get_firmware_version(OutPointerWithServerSize out) { - if (out.num_elements != 1) { - return {0xF601}; - } - +Result SetSysMitmService::GetFirmwareVersion(OutPointerWithServerSize out) { Result rc = _GetFirmwareVersion(out.pointer); /* GetFirmwareVersion sanitizes these fields. */ out.pointer->revision_major = 0; out.pointer->revision_minor = 0; - return {rc}; + return rc; } -std::tuple SetSysMitMService::get_firmware_version2(OutPointerWithServerSize out) { - if (out.num_elements != 1) { - return {0xF601}; - } - - Result rc = _GetFirmwareVersion(out.pointer); - - return {rc}; +Result SetSysMitmService::GetFirmwareVersion2(OutPointerWithServerSize out) { + return _GetFirmwareVersion(out.pointer); } \ No newline at end of file diff --git a/stratosphere/fs_mitm/source/setsys_mitm_service.hpp b/stratosphere/fs_mitm/source/setsys_mitm_service.hpp index 8c8877881..b62bdcb69 100644 --- a/stratosphere/fs_mitm/source/setsys_mitm_service.hpp +++ b/stratosphere/fs_mitm/source/setsys_mitm_service.hpp @@ -16,43 +16,34 @@ #pragma once #include -#include -#include "imitmserviceobject.hpp" +#include #include "fsmitm_utils.hpp" -enum class SetSysCmd { - GetFirmwareVersion = 3, - GetFirmwareVersion2 = 4, +enum SetSysCmd : u32 { + SetSysCmd_GetFirmwareVersion = 3, + SetSysCmd_GetFirmwareVersion2 = 4, }; -class SetSysMitMService : public IMitMServiceObject { - private: +class SetSysMitmService : public IMitmServiceObject { public: - SetSysMitMService(Service *s) : IMitMServiceObject(s) { + SetSysMitmService(std::shared_ptr s) : IMitmServiceObject(s) { /* ... */ } - static bool should_mitm(u64 pid, u64 tid) { + static bool ShouldMitm(u64 pid, u64 tid) { /* Only MitM qlaunch, maintenance. */ return tid == 0x0100000000001000ULL || tid == 0x0100000000001015ULL; } - SetSysMitMService *clone() override { - auto new_srv = new SetSysMitMService((Service *)&this->forward_service); - this->clone_to(new_srv); - return new_srv; - } - - void clone_to(void *o) override { - /* ... */ - } - - virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size); - virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size); - virtual Result handle_deferred(); - + static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx); + protected: /* Overridden commands. */ - std::tuple get_firmware_version(OutPointerWithServerSize out); - std::tuple get_firmware_version2(OutPointerWithServerSize out); + Result GetFirmwareVersion(OutPointerWithServerSize out); + Result GetFirmwareVersion2(OutPointerWithServerSize out); + public: + DEFINE_SERVICE_DISPATCH_TABLE { + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + }; }; diff --git a/stratosphere/fs_mitm/source/sm_mitm.c b/stratosphere/fs_mitm/source/sm_mitm.c deleted file mode 100644 index 0ab1c5d55..000000000 --- a/stratosphere/fs_mitm/source/sm_mitm.c +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include "sm_mitm.h" - -static Handle g_smMitmHandle = INVALID_HANDLE; -static u64 g_refCnt; - -Result smMitMInitialize(void) { - atomicIncrement64(&g_refCnt); - - if (g_smMitmHandle != INVALID_HANDLE) - return 0; - - Result rc = svcConnectToNamedPort(&g_smMitmHandle, "sm:"); - - if (R_SUCCEEDED(rc)) { - IpcCommand c; - ipcInitialize(&c); - ipcSendPid(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 zero; - u64 reserved[2]; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->zero = 0; - - rc = ipcDispatch(g_smMitmHandle); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - } - - if (R_FAILED(rc)) - smExit(); - - return rc; -} - -void smMitMExit(void) { - if (atomicDecrement64(&g_refCnt) == 0) { - svcCloseHandle(g_smMitmHandle); - g_smMitmHandle = INVALID_HANDLE; - } -} - -Result smMitMGetService(Service* service_out, const char *name_str) -{ - u64 name = smEncodeName(name_str); - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - u64 reserved[2]; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 1; - raw->service_name = name; - - Result rc = ipcDispatch(g_smMitmHandle); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - service_out->type = ServiceType_Normal; - service_out->handle = r.Handles[0]; - } - } - - return rc; -} - - -Result smMitMInstall(Handle *handle_out, Handle *query_out, const char *name) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - raw->service_name = smEncodeName(name); - - Result rc = ipcDispatch(g_smMitmHandle); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; - *query_out = r.Handles[1]; - } - } - - return rc; -} - -Result smMitMUninstall(const char *name) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - u64 reserved; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65001; - raw->service_name = smEncodeName(name); - - Result rc = ipcDispatch(g_smMitmHandle); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; -} \ No newline at end of file diff --git a/stratosphere/fs_mitm/source/sm_mitm.h b/stratosphere/fs_mitm/source/sm_mitm.h deleted file mode 100644 index 4b521c88c..000000000 --- a/stratosphere/fs_mitm/source/sm_mitm.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @file sm_mitm.h - * @brief Service manager (sm) IPC wrapper for Atmosphere extensions. - * @author SciresM - * @copyright libnx Authors - */ -#pragma once -#include - -#ifdef __cplusplus -extern "C" { -#endif - -Result smMitMInitialize(void); -void smMitMExit(void); -Result smMitMGetService(Service* service_out, const char *name); -Result smMitMInstall(Handle *handle_out, Handle *query_out, const char *name); -Result smMitMUninstall(const char *name); - -Result smMitMIsRegistered(const char *name); - -#ifdef __cplusplus -} -#endif \ No newline at end of file