From e2162d3f3d0c7595dbec5a70979862c9a8adeb61 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 19 Oct 2019 17:42:53 -0700 Subject: [PATCH] libstrat: namespace remaining non-namespaced code. more new-ipc updates --- Makefile | 2 +- include/stratosphere.hpp | 9 +- include/stratosphere/ams.hpp | 5 +- .../{on_crash.hpp => ams/ams_emummc_api.hpp} | 18 +- .../ams_environment.hpp} | 13 +- .../stratosphere/ams/ams_exosphere_api.hpp | 61 ++ include/stratosphere/ams/ams_types.hpp | 65 ++ .../stratosphere/dd.hpp | 8 +- include/stratosphere/dd/dd_io_mappings.hpp | 36 + include/stratosphere/defines.hpp | 1 + include/stratosphere/os/os_common_types.hpp | 12 +- include/stratosphere/services/bpc_ams.h | 66 -- include/stratosphere/sm.hpp | 1 + include/stratosphere/sm/sm_api.hpp | 13 + include/stratosphere/sm/sm_scoped_holder.hpp | 86 +++ include/stratosphere/sm/sm_types.hpp | 63 -- include/stratosphere/spl/smc/spl_smc.hpp | 18 + include/stratosphere/spl/spl_types.hpp | 13 + include/stratosphere/utilities.hpp | 128 ---- include/stratosphere/version_check.hpp | 75 -- source/ams/ams_bpc.c | 46 ++ source/ams/ams_bpc.h | 34 + source/ams/ams_environment.cpp | 153 +++++ source/ams/ams_exosphere_api.cpp | 75 ++ source/ams/ams_hos_version_api.cpp | 14 +- source/bpc_ams.c | 76 -- source/dd/dd_io_mappings.cpp | 55 ++ source/dmnt/dmntcht.c | 166 +++++ .../services => source/dmnt}/dmntcht.h | 7 +- source/dmntcht.c | 650 ------------------ source/emummc_utilities.cpp | 141 ---- source/ldr/ldr_ams.c | 41 +- source/on_crash.cpp | 142 ---- source/pm/pm_ams.c | 169 +---- source/service_guard.h | 64 ++ source/sm/sm_ams.c | 351 ++-------- source/sm/sm_ams.h | 5 +- source/sm/sm_api.cpp | 11 + source/sm/smm_ams.c | 119 +--- source/spl/smc/spl_smc.cpp | 55 ++ source/updater/updater_api.cpp | 10 +- 41 files changed, 1096 insertions(+), 1981 deletions(-) rename include/stratosphere/{on_crash.hpp => ams/ams_emummc_api.hpp} (68%) rename include/stratosphere/{emummc_utilities.hpp => ams/ams_environment.hpp} (72%) create mode 100644 include/stratosphere/ams/ams_exosphere_api.hpp rename source/utilities.cpp => include/stratosphere/dd.hpp (80%) create mode 100644 include/stratosphere/dd/dd_io_mappings.hpp delete mode 100644 include/stratosphere/services/bpc_ams.h create mode 100644 include/stratosphere/sm/sm_scoped_holder.hpp delete mode 100644 include/stratosphere/utilities.hpp delete mode 100644 include/stratosphere/version_check.hpp create mode 100644 source/ams/ams_bpc.c create mode 100644 source/ams/ams_bpc.h create mode 100644 source/ams/ams_environment.cpp create mode 100644 source/ams/ams_exosphere_api.cpp delete mode 100644 source/bpc_ams.c create mode 100644 source/dd/dd_io_mappings.cpp create mode 100644 source/dmnt/dmntcht.c rename {include/stratosphere/services => source/dmnt}/dmntcht.h (96%) delete mode 100644 source/dmntcht.c delete mode 100644 source/emummc_utilities.cpp delete mode 100644 source/on_crash.cpp create mode 100644 source/service_guard.h diff --git a/Makefile b/Makefile index 0fd81506..58169122 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ include $(DEVKITPRO)/libnx/switch_rules # INCLUDES is a list of directories containing header files #--------------------------------------------------------------------------------- TARGET := $(notdir $(CURDIR)) -SOURCES := source source/ams source/os source/os/impl source/sf source/sf/cmif source/sf/hipc source/spl source/spl/smc source/updater source/patcher source/map source/rnd source/util source/sm source/cfg source/pm source/hid source/ldr source/kvdb source/boot2 +SOURCES := source source/ams source/os source/os/impl source/dd source/sf source/sf/cmif source/sf/hipc source/dmnt source/spl source/spl/smc source/updater source/patcher source/map source/rnd source/util source/sm source/cfg source/pm source/hid source/ldr source/kvdb source/boot2 DATA := data INCLUDES := include diff --git a/include/stratosphere.hpp b/include/stratosphere.hpp index b1a36f45..58e7ffe7 100644 --- a/include/stratosphere.hpp +++ b/include/stratosphere.hpp @@ -17,20 +17,13 @@ #pragma once #include "stratosphere/defines.hpp" - -#include "stratosphere/utilities.hpp" -#include "stratosphere/emummc_utilities.hpp" - -#include "stratosphere/version_check.hpp" - #include "stratosphere/results.hpp" -#include "stratosphere/on_crash.hpp" - #include "stratosphere/util.hpp" #include "stratosphere/svc.hpp" #include "stratosphere/ams.hpp" #include "stratosphere/os.hpp" +#include "stratosphere/dd.hpp" #include "stratosphere/cfg.hpp" #include "stratosphere/fatal.hpp" #include "stratosphere/hid.hpp" diff --git a/include/stratosphere/ams.hpp b/include/stratosphere/ams.hpp index 9af4ad13..666633bd 100644 --- a/include/stratosphere/ams.hpp +++ b/include/stratosphere/ams.hpp @@ -17,4 +17,7 @@ #pragma once #include "ams/ams_types.hpp" -#include "ams/ams_hos_version_api.hpp" \ No newline at end of file +#include "ams/ams_hos_version_api.hpp" +#include "ams/ams_exosphere_api.hpp" +#include "ams/ams_emummc_api.hpp" +#include "ams/ams_environment.hpp" diff --git a/include/stratosphere/on_crash.hpp b/include/stratosphere/ams/ams_emummc_api.hpp similarity index 68% rename from include/stratosphere/on_crash.hpp rename to include/stratosphere/ams/ams_emummc_api.hpp index 2f66f60e..5a666140 100644 --- a/include/stratosphere/on_crash.hpp +++ b/include/stratosphere/ams/ams_emummc_api.hpp @@ -15,13 +15,17 @@ */ #pragma once -#include -#include -#include "services/bpc_ams.h" +#include "ams_types.hpp" -static constexpr size_t AtmosphereFatalErrorNumGprs = 29; +namespace sts::ams::emummc { -static constexpr u32 AtmosphereFatalErrorMagic = 0x31454641; /* "AFE1" */ + /* Get whether emummc is active. */ + bool IsActive(); -/* Will be called by libstratosphere on crash. */ -void StratosphereCrashHandler(ThreadExceptionDump *ctx); \ No newline at end of file + /* Get Nintendo redirection path. */ + const char *GetNintendoDirPath(); + + /* Get Emummc folderpath, NULL if not file-based. */ + const char *GetFilePath(); + +} diff --git a/include/stratosphere/emummc_utilities.hpp b/include/stratosphere/ams/ams_environment.hpp similarity index 72% rename from include/stratosphere/emummc_utilities.hpp rename to include/stratosphere/ams/ams_environment.hpp index d2b52e04..55265ee3 100644 --- a/include/stratosphere/emummc_utilities.hpp +++ b/include/stratosphere/ams/ams_environment.hpp @@ -15,14 +15,11 @@ */ #pragma once -#include -#include +#include "ams_types.hpp" -/* Get whether emummc is active. */ -bool IsEmummc(); +namespace sts::ams { -/* Get Nintendo redirection path. */ -const char *GetEmummcNintendoDirPath(); + /* Will be called by libstratosphere on crash. */ + void CrashHandler(ThreadExceptionDump *ctx); -/* Get Emummc folderpath, NULL if not file-based. */ -const char *GetEmummcFilePath(); +} diff --git a/include/stratosphere/ams/ams_exosphere_api.hpp b/include/stratosphere/ams/ams_exosphere_api.hpp new file mode 100644 index 00000000..7dbcbedf --- /dev/null +++ b/include/stratosphere/ams/ams_exosphere_api.hpp @@ -0,0 +1,61 @@ +/* + * 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 . + */ + +#pragma once +#include "ams_types.hpp" + +namespace sts::ams { + + ApiInfo GetApiInfo(); + + void ForceRebootToRcm(); + void ForceRebootToIramPayload(); + void ForceShutdown(); + + bool IsRcmBugPatched(); + + void CopyToIram(uintptr_t iram_dst, const void *dram_src, size_t size); + void CopyFromIram(void *dram_dst, uintptr_t iram_src, size_t size); + + /* Version checking utility. */ +#ifdef ATMOSPHERE_RELEASE_VERSION_MAJOR + +#define ATMOSPHERE_RELEASE_VERSION ATMOSPHERE_RELEASE_VERSION_MAJOR, ATMOSPHERE_RELEASE_VERSION_MINOR, ATMOSPHERE_RELEASE_VERSION_MICRO + + inline void CheckApiVersion() { + const u32 runtime_version = GetApiInfo().GetVersion(); + const u32 build_version = GetVersion(ATMOSPHERE_RELEASE_VERSION); + + if (runtime_version < build_version) { + R_ASSERT(ResultAtmosphereVersionMismatch); + } + } + +#endif + +#ifdef ATMOSPHERE_GIT_BRANCH + NX_CONSTEXPR const char *GetGitBranch() { + return ATMOSPHERE_GIT_BRANCH; + } +#endif + +#ifdef ATMOSPHERE_GIT_REV + NX_CONSTEXPR const char *GetGitRevision() { + return ATMOSPHERE_GIT_REV; + } +#endif + +} diff --git a/include/stratosphere/ams/ams_types.hpp b/include/stratosphere/ams/ams_types.hpp index 9c0e2436..15e29808 100644 --- a/include/stratosphere/ams/ams_types.hpp +++ b/include/stratosphere/ams/ams_types.hpp @@ -17,6 +17,8 @@ #pragma once #include #include "../defines.hpp" +#include "../results.hpp" +#include "../sf/sf_buffer_tags.hpp" /* Define firmware version in global namespace, for convenience. */ namespace sts { @@ -59,4 +61,67 @@ namespace sts::ams { TargetFirmware_900 = 11, }; + constexpr inline u32 GetVersion(u32 major, u32 minor, u32 micro) { + return (major << 16) | (minor << 8) | (micro); + } + + struct ApiInfo { + u32 major_version; + u32 minor_version; + u32 micro_version; + TargetFirmware target_firmware; + u32 master_key_revision; + + constexpr u32 GetVersion() const { + return ::sts::ams::GetVersion(this->major_version, this->minor_version, this->micro_version); + } + + constexpr TargetFirmware GetTargetFirmware() const { + return this->target_firmware; + } + + constexpr u32 GetMasterKeyRevision() const { + return this->master_key_revision; + } + }; + + struct FatalErrorContext : sf::LargeData, sf::PrefersMapAliasTransferMode { + static constexpr size_t MaxStackTrace = 0x20; + static constexpr size_t MaxStackDumpSize = 0x100; + static constexpr size_t NumGprs = 29; + static constexpr uintptr_t StdAbortMagicAddress = 0x8; + static constexpr u64 StdAbortMagicValue = 0xA55AF00DDEADCAFEul; + static constexpr u32 StdAbortErrorDesc = 0xFFE; + static constexpr u32 DataAbortErrorDesc = 0x101; + static constexpr u32 Magic = 0x31454641; + + u32 magic; + u32 error_desc; + u64 title_id; + union { + u64 gprs[32]; + struct { + u64 _gprs[29]; + u64 fp; + u64 lr; + u64 sp; + }; + }; + u64 pc; + u64 module_base; + u32 pstate; + u32 afsr0; + u32 afsr1; + u32 esr; + u64 far; + u64 report_identifier; /* Normally just system tick. */ + u64 stack_trace_size; + u64 stack_dump_size; + u64 stack_trace[MaxStackTrace]; + u8 stack_dump[MaxStackDumpSize]; + }; + + static_assert(sizeof(FatalErrorContext) == 0x350, "sizeof(FatalErrorContext)"); + static_assert(std::is_pod::value, "FatalErrorContext"); + } diff --git a/source/utilities.cpp b/include/stratosphere/dd.hpp similarity index 80% rename from source/utilities.cpp rename to include/stratosphere/dd.hpp index af8d221d..a4ba4aac 100644 --- a/source/utilities.cpp +++ b/include/stratosphere/dd.hpp @@ -14,11 +14,7 @@ * along with this program. If not, see . */ +#pragma once #include -#include -static sts::os::RecursiveMutex g_sm_session_lock; - -sts::os::RecursiveMutex &GetSmSessionMutex() { - return g_sm_session_lock; -} +#include "dd/dd_io_mappings.hpp" diff --git a/include/stratosphere/dd/dd_io_mappings.hpp b/include/stratosphere/dd/dd_io_mappings.hpp new file mode 100644 index 00000000..a0ba2fd9 --- /dev/null +++ b/include/stratosphere/dd/dd_io_mappings.hpp @@ -0,0 +1,36 @@ +/* + * 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 . + */ + +#pragma once +#include +#include "../defines.hpp" + +namespace sts::dd { + + uintptr_t QueryIoMapping(uintptr_t phys_addr, size_t size); + + u32 ReadRegister(uintptr_t phys_addr); + void WriteRegister(uintptr_t phys_addr, u32 value); + u32 ReadWriteRegister(uintptr_t phys_addr, u32 value, u32 mask); + + /* Convenience Helper. */ + + inline uintptr_t GetIoMapping(uintptr_t phys_addr, size_t size) { + const uintptr_t io_mapping = QueryIoMapping(phys_addr, size); + STS_ASSERT(io_mapping); + return io_mapping; + } +} diff --git a/include/stratosphere/defines.hpp b/include/stratosphere/defines.hpp index 3cd70b67..2f381820 100644 --- a/include/stratosphere/defines.hpp +++ b/include/stratosphere/defines.hpp @@ -15,6 +15,7 @@ */ #pragma once +#include #include /* Any broadly useful language defines should go here. */ diff --git a/include/stratosphere/os/os_common_types.hpp b/include/stratosphere/os/os_common_types.hpp index 8740faed..9ed48405 100644 --- a/include/stratosphere/os/os_common_types.hpp +++ b/include/stratosphere/os/os_common_types.hpp @@ -46,14 +46,18 @@ namespace sts::os { inline constexpr const ProcessId InvalidProcessId = ProcessId::Invalid; - NX_INLINE Result GetProcessId(os::ProcessId *out, ::Handle process_handle) { + NX_INLINE Result TryGetProcessId(os::ProcessId *out, ::Handle process_handle) { return svcGetProcessId(&out->value, process_handle); } + NX_INLINE os::ProcessId GetProcessId(::Handle process_handle) { + os::ProcessId process_id; + R_ASSERT(TryGetProcessId(&process_id, process_handle)); + return process_id; + } + NX_INLINE ProcessId GetCurrentProcessId() { - os::ProcessId current_process_id; - R_ASSERT(GetProcessId(¤t_process_id, CUR_PROCESS_HANDLE)); - return current_process_id; + return GetProcessId(CUR_PROCESS_HANDLE); } inline constexpr bool operator==(const ProcessId &lhs, const ProcessId &rhs) { diff --git a/include/stratosphere/services/bpc_ams.h b/include/stratosphere/services/bpc_ams.h deleted file mode 100644 index c8e11a73..00000000 --- a/include/stratosphere/services/bpc_ams.h +++ /dev/null @@ -1,66 +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 . - */ - -#pragma once -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define AMS_FATAL_ERROR_MAX_STACKTRACE 0x20 -#define AMS_FATAL_ERROR_MAX_STACKDUMP 0x100 - -#define STD_ABORT_ADDR_MAGIC (0x8) -#define STD_ABORT_VALUE_MAGIC (0xA55AF00DDEADCAFEul) -#define DATA_ABORT_ERROR_DESC (0x101) -#define STD_ABORT_ERROR_DESC (0xFFE) - -typedef struct { - u32 magic; - u32 error_desc; - u64 title_id; - union { - u64 gprs[32]; - struct { - u64 _gprs[29]; - u64 fp; - u64 lr; - u64 sp; - }; - }; - u64 pc; - u64 module_base; - u32 pstate; - u32 afsr0; - u32 afsr1; - u32 esr; - u64 far; - u64 report_identifier; /* Normally just system tick. */ - u64 stack_trace_size; - u64 stack_dump_size; - u64 stack_trace[AMS_FATAL_ERROR_MAX_STACKTRACE]; - u8 stack_dump[AMS_FATAL_ERROR_MAX_STACKDUMP]; -} AtmosphereFatalErrorContext; - -Result bpcAmsInitialize(void); -void bpcAmsExit(void); - -Result bpcAmsRebootToFatalError(AtmosphereFatalErrorContext *ctx); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/stratosphere/sm.hpp b/include/stratosphere/sm.hpp index a334419a..5c6ac9f3 100644 --- a/include/stratosphere/sm.hpp +++ b/include/stratosphere/sm.hpp @@ -20,3 +20,4 @@ #include "sm/sm_types.hpp" #include "sm/sm_api.hpp" #include "sm/sm_mitm_api.hpp" +#include "sm/sm_scoped_holder.hpp" diff --git a/include/stratosphere/sm/sm_api.hpp b/include/stratosphere/sm/sm_api.hpp index 80e12cb7..2fb4f938 100644 --- a/include/stratosphere/sm/sm_api.hpp +++ b/include/stratosphere/sm/sm_api.hpp @@ -29,4 +29,17 @@ namespace sts::sm { Result HasService(bool *out, ServiceName name); Result WaitService(ServiceName name); + /* Scoped session access. */ + namespace impl { + + void DoWithSessionImpl(void (*Invoker)(void *), void *Function); + + } + + template + NX_CONSTEXPR void DoWithSession(F f) { + auto invoker = +[](void *func) { (*(F *)func)(); }; + impl::DoWithSessionImpl(invoker, &f); + } + } diff --git a/include/stratosphere/sm/sm_scoped_holder.hpp b/include/stratosphere/sm/sm_scoped_holder.hpp new file mode 100644 index 00000000..a7ffb724 --- /dev/null +++ b/include/stratosphere/sm/sm_scoped_holder.hpp @@ -0,0 +1,86 @@ +/* + * 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 . + */ + +#pragma once + +#include "sm_api.hpp" + +namespace sts::sm { + + /* Utility, for scoped access to libnx services. */ + template + class ScopedServiceHolder { + NON_COPYABLE(ScopedServiceHolder); + private: + Result result; + bool has_initialized; + public: + ScopedServiceHolder(bool initialize = true) : result(ResultSuccess), has_initialized(false) { + if (initialize) { + this->Initialize(); + } + } + + ~ScopedServiceHolder() { + if (this->has_initialized) { + this->Finalize(); + } + } + + ScopedServiceHolder(ScopedServiceHolder&& rhs) { + this->result = rhs.result; + this->has_initialized = rhs.has_initialized; + rhs.result = ResultSuccess; + rhs.has_initialized = false; + } + + ScopedServiceHolder& operator=(ScopedServiceHolder&& rhs) { + rhs.Swap(*this); + return *this; + } + + void Swap(ScopedServiceHolder& rhs) { + std::swap(this->result, rhs.result); + std::swap(this->has_initialized, rhs.has_initialized); + } + + explicit operator bool() const { + return this->has_initialized; + } + + Result Initialize() { + STS_ASSERT(!this->has_initialized); + + sm::DoWithSession([&]() { + this->result = Initializer(); + }); + + this->has_initialized = R_SUCCEEDED(this->result); + return this->result; + } + + void Finalize() { + STS_ASSERT(this->has_initialized); + Finalizer(); + this->has_initialized = false; + } + + Result GetResult() const { + return this->result; + } + }; + +} diff --git a/include/stratosphere/sm/sm_types.hpp b/include/stratosphere/sm/sm_types.hpp index 812cc2f7..30d97b3b 100644 --- a/include/stratosphere/sm/sm_types.hpp +++ b/include/stratosphere/sm/sm_types.hpp @@ -70,67 +70,4 @@ namespace sts::sm { }; static_assert(sizeof(ServiceRecord) == 0x30, "ServiceRecord definition!"); - /* Utility, for scoped access to libnx services. */ - template - class ScopedServiceHolder { - NON_COPYABLE(ScopedServiceHolder); - private: - Result result; - bool has_initialized; - public: - ScopedServiceHolder(bool initialize = true) : result(ResultSuccess), has_initialized(false) { - if (initialize) { - this->Initialize(); - } - } - - ~ScopedServiceHolder() { - if (this->has_initialized) { - this->Finalize(); - } - } - - ScopedServiceHolder(ScopedServiceHolder&& rhs) { - this->result = rhs.result; - this->has_initialized = rhs.has_initialized; - rhs.result = ResultSuccess; - rhs.has_initialized = false; - } - - ScopedServiceHolder& operator=(ScopedServiceHolder&& rhs) { - rhs.Swap(*this); - return *this; - } - - void Swap(ScopedServiceHolder& rhs) { - std::swap(this->result, rhs.result); - std::swap(this->has_initialized, rhs.has_initialized); - } - - explicit operator bool() const { - return this->has_initialized; - } - - Result Initialize() { - STS_ASSERT(!this->has_initialized); - - DoWithSmSession([&]() { - this->result = Initializer(); - }); - - this->has_initialized = R_SUCCEEDED(this->result); - return this->result; - } - - void Finalize() { - STS_ASSERT(this->has_initialized); - Finalizer(); - this->has_initialized = false; - } - - Result GetResult() const { - return this->result; - } - }; - } diff --git a/include/stratosphere/spl/smc/spl_smc.hpp b/include/stratosphere/spl/smc/spl_smc.hpp index 46a011a3..fe39687b 100644 --- a/include/stratosphere/spl/smc/spl_smc.hpp +++ b/include/stratosphere/spl/smc/spl_smc.hpp @@ -16,6 +16,7 @@ #pragma once #include +#include #include "../spl_types.hpp" @@ -54,4 +55,21 @@ namespace sts::spl::smc { Result DecryptRsaPrivateKey(size_t *out_size, void *data, size_t size, const AccessKey &access_key, const KeySource &source, u32 option); Result ImportSecureExpModKey(const void *data, size_t size, const AccessKey &access_key, const KeySource &source, u32 option); + /* Atmosphere functions. */ + Result AtmosphereCopyToIram(uintptr_t iram_dst, const void *dram_src, size_t size); + Result AtmosphereCopyFromIram(void *dram_dst, uintptr_t iram_src, size_t size); + Result AtmosphereReadWriteRegister(uint64_t address, uint32_t mask, uint32_t value, uint32_t *out_value); + Result AtmosphereWriteAddress(void *dst, const void *src, size_t size); + + /* Helpers. */ + inline Result SetConfig(SplConfigItem which, const u64 value) { + return SetConfig(which, &value, 1); + } + + template + inline Result AtmosphereWriteAddress(void *dst, const T value) { + static_assert(std::is_integral::value && sizeof(T) <= 8 && (sizeof(T) & (sizeof(T) - 1)) == 0, "AtmosphereWriteAddress requires integral type."); + return AtmosphereWriteAddress(dst, &value, sizeof(T)); + } + } diff --git a/include/stratosphere/spl/spl_types.hpp b/include/stratosphere/spl/spl_types.hpp index 56ce8fdd..664811e2 100644 --- a/include/stratosphere/spl/spl_types.hpp +++ b/include/stratosphere/spl/spl_types.hpp @@ -46,6 +46,12 @@ namespace sts::spl { ImportEsKey = 0xC300100C, DecryptRsaPrivateKey = 0xC300100D, ImportSecureExpModKey = 0xC300100E, + + /* Atmosphere functions. */ + AtmosphereIramCopy = 0xF0000201, + AtmosphereReadWriteRegister = 0xF0000002, + AtmosphereWriteAddress = 0xF0000003, + AtmosphereGetEmummcConfig = 0xF0000404, }; enum class Result { @@ -177,3 +183,10 @@ namespace sts::spl { #pragma pack(pop) } + +/* Extensions to libnx spl config item enum. */ +constexpr inline SplConfigItem SplConfigItem_ExosphereApiVersion = static_cast(65000); +constexpr inline SplConfigItem SplConfigItem_ExosphereNeedsReboot = static_cast(65001); +constexpr inline SplConfigItem SplConfigItem_ExosphereNeedsShutdown = static_cast(65002); +constexpr inline SplConfigItem SplConfigItem_ExosphereGitCommitHash = static_cast(65003); +constexpr inline SplConfigItem SplConfigItem_ExosphereHasRcmBugPatch = static_cast(65004); diff --git a/include/stratosphere/utilities.hpp b/include/stratosphere/utilities.hpp deleted file mode 100644 index 4de68dc1..00000000 --- a/include/stratosphere/utilities.hpp +++ /dev/null @@ -1,128 +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 . - */ - -#pragma once -#include -#include "defines.hpp" -#include "results.hpp" -#include "os.hpp" - -static inline uintptr_t GetIoMapping(const u64 io_addr, const u64 io_size) { - u64 vaddr; - const u64 aligned_addr = (io_addr & ~0xFFFul); - const u64 aligned_size = io_size + (io_addr - aligned_addr); - R_ASSERT(svcQueryIoMapping(&vaddr, aligned_addr, aligned_size)); - return static_cast(vaddr + (io_addr - aligned_addr)); -} - -static inline void RebootToRcm() { - SecmonArgs args = {0}; - args.X[0] = 0xC3000401; /* smcSetConfig */ - args.X[1] = 65001; /* Exosphere reboot */ - args.X[3] = 1; /* Perform reboot to RCM. */ - svcCallSecureMonitor(&args); -} - -static inline void RebootToIramPayload() { - SecmonArgs args = {0}; - args.X[0] = 0xC3000401; /* smcSetConfig */ - args.X[1] = 65001; /* Exosphere reboot */ - args.X[3] = 2; /* Perform reboot to payload at 0x40010000 in IRAM. */ - svcCallSecureMonitor(&args); -} - -static inline void PerformShutdownSmc() { - SecmonArgs args = {0}; - args.X[0] = 0xC3000401; /* smcSetConfig */ - args.X[1] = 65002; /* Exosphere shutdown */ - args.X[3] = 1; /* Perform shutdown. */ - svcCallSecureMonitor(&args); -} - -static inline void CopyToIram(uintptr_t iram_addr, void *src_addr, size_t size) { - SecmonArgs args = {0}; - args.X[0] = 0xF0000201; /* smcAmsIramCopy */ - args.X[1] = (u64)src_addr; /* DRAM address */ - args.X[2] = (u64)iram_addr; /* IRAM address */ - args.X[3] = size; /* Amount to copy */ - args.X[4] = 1; /* 1 = Write */ - svcCallSecureMonitor(&args); -} - -static inline void CopyFromIram(void *dst_addr, uintptr_t iram_addr, size_t size) { - SecmonArgs args = {0}; - args.X[0] = 0xF0000201; /* smcAmsIramCopy */ - args.X[1] = (u64)dst_addr; /* DRAM address */ - args.X[2] = (u64)iram_addr; /* IRAM address */ - args.X[3] = size; /* Amount to copy */ - args.X[4] = 0; /* 0 = Read */ - svcCallSecureMonitor(&args); -} - -static inline Result SmcGetConfig(SplConfigItem config_item, u64 *out_config) { - SecmonArgs args = {0}; - args.X[0] = 0xC3000002; /* smcGetConfig */ - args.X[1] = (u64)config_item; /* config item */ - - R_TRY(svcCallSecureMonitor(&args)); - if (args.X[0] != 0) { - /* SPL result n = SMC result n */ - return MAKERESULT(26, args.X[0]); - } - - if (out_config) { - *out_config = args.X[1]; - } - return ResultSuccess; -} - -static inline Result GetRcmBugPatched(bool *out) { - u64 tmp = 0; - R_TRY(SmcGetConfig((SplConfigItem)65004, &tmp)); - *out = (tmp != 0); - return ResultSuccess; -} - -static inline bool IsRcmBugPatched() { - bool rcm_bug_patched; - R_ASSERT(GetRcmBugPatched(&rcm_bug_patched)); - return rcm_bug_patched; -} - -static inline Result GetShouldBlankProdInfo(bool *out) { - u64 tmp = 0; - R_TRY(SmcGetConfig((SplConfigItem)65005, &tmp)); - *out = (tmp != 0); - return ResultSuccess; -} - -static inline bool ShouldBlankProdInfo() { - bool should_blank_prodinfo; - R_ASSERT(GetShouldBlankProdInfo(&should_blank_prodinfo)); - return should_blank_prodinfo; -} - -sts::os::RecursiveMutex &GetSmSessionMutex(); - -template -static void DoWithSmSession(F f) { - std::scoped_lock lk(GetSmSessionMutex()); - { - R_ASSERT(smInitialize()); - f(); - smExit(); - } -} diff --git a/include/stratosphere/version_check.hpp b/include/stratosphere/version_check.hpp deleted file mode 100644 index f9f3dea1..00000000 --- a/include/stratosphere/version_check.hpp +++ /dev/null @@ -1,75 +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 . - */ - -#pragma once -#include - -#include "results.hpp" - -static inline void GetAtmosphereApiVersion(u32 *major, u32 *minor, u32 *micro, u32 *target_fw, u32 *mkey_rev) { - /* Check for exosphere API compatibility. */ - u64 exosphere_cfg; - if (R_FAILED(SmcGetConfig((SplConfigItem)65000, &exosphere_cfg))) { - fatalSimple(ResultAtmosphereExosphereNotPresent); - } - - if (mkey_rev) { - *mkey_rev = (u32)((exosphere_cfg >> 0x00) & 0xFF); - } - - if (target_fw) { - *target_fw = (u32)((exosphere_cfg >> 0x08) & 0xFF); - } - - if (micro) { - *micro = (u32)((exosphere_cfg >> 0x10) & 0xFF); - } - - if (minor) { - *minor = (u32)((exosphere_cfg >> 0x18) & 0xFF); - } - - if (major) { - *major = (u32)((exosphere_cfg >> 0x20) & 0xFF); - } -} - -static inline u32 MakeAtmosphereVersion(u32 major, u32 minor, u32 micro) { - return (major << 16) | (minor << 8) | micro; -} - -static inline void CheckAtmosphereVersion(u32 expected_major, u32 expected_minor, u32 expected_micro) { - u32 major, minor, micro; - GetAtmosphereApiVersion(&major, &minor, µ, nullptr, nullptr); - - if (MakeAtmosphereVersion(major, minor, micro) < MakeAtmosphereVersion(expected_major, expected_minor, expected_micro)) { - fatalSimple(ResultAtmosphereVersionMismatch); - } -} - -#define CURRENT_ATMOSPHERE_VERSION ATMOSPHERE_RELEASE_VERSION_MAJOR, ATMOSPHERE_RELEASE_VERSION_MINOR, ATMOSPHERE_RELEASE_VERSION_MICRO - -#ifdef ATMOSPHERE_GIT_BRANCH -static inline const char *GetAtmosphereGitBranch() { - return ATMOSPHERE_GIT_BRANCH; -} -#endif - -#ifdef ATMOSPHERE_GIT_REV -static inline const char *GetAtmosphereGitRevision() { - return ATMOSPHERE_GIT_REV; -} -#endif diff --git a/source/ams/ams_bpc.c b/source/ams/ams_bpc.c new file mode 100644 index 00000000..84c1d476 --- /dev/null +++ b/source/ams/ams_bpc.c @@ -0,0 +1,46 @@ +/* + * 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 . + */ +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "../service_guard.h" +#include "ams_bpc.h" + +static Service g_amsBpcSrv; + +NX_GENERATE_SERVICE_GUARD(amsBpc); + +Result _amsBpcInitialize(void) { + Handle h; + Result rc = svcConnectToNamedPort(&h, "bpc:ams"); /* TODO: ams:bpc */ + if (R_SUCCEEDED(rc)) serviceCreate(&g_amsBpcSrv, h); + return rc; +} + +void _amsBpcCleanup(void) { + serviceClose(&g_amsBpcSrv); +} + +Service *amsBpcGetServiceSession(void) { + return &g_amsBpcSrv; +} + +Result amsBpcRebootToFatalError(void *ctx) { + /* Note: this takes in an sts::ams::FatalErrorContext. */ + /* static_assert(sizeof() == 0x350) is done at type definition. */ + return serviceDispatch(&g_amsBpcSrv, 65000, + .buffer_attrs = { SfBufferAttr_Out | SfBufferAttr_HipcMapAlias | SfBufferAttr_FixedSize }, + .buffers = { { ctx, 0x350 } }, + ); +} diff --git a/source/ams/ams_bpc.h b/source/ams/ams_bpc.h new file mode 100644 index 00000000..91dfd1e4 --- /dev/null +++ b/source/ams/ams_bpc.h @@ -0,0 +1,34 @@ +/* + * 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 . + */ + +#pragma once +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +Result amsBpcInitialize(void); +void amsBpcExit(void); +Service *amsBpcGetServiceSession(void); + +Result amsBpcRebootToFatalError(void *ctx); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/source/ams/ams_environment.cpp b/source/ams/ams_environment.cpp new file mode 100644 index 00000000..77d3d8e6 --- /dev/null +++ b/source/ams/ams_environment.cpp @@ -0,0 +1,153 @@ +/* + * 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 . + */ + +#include +#include "ams_bpc.h" + +namespace sts::ams { + + namespace { + + inline u64 GetPc() { + u64 pc; + __asm__ __volatile__ ("adr %[pc], ." : [pc]"=&r"(pc) :: ); + return pc; + } + + struct StackFrame { + u64 fp; + u64 lr; + }; + + } + + extern ncm::TitleId StratosphereTitleId; + + void WEAK ExceptionHandler(FatalErrorContext *ctx) { + R_ASSERT(amsBpcInitialize()); + R_ASSERT(amsBpcRebootToFatalError(ctx)); + while (1) { /* ... */ } + } + + void CrashHandler(ThreadExceptionDump *ctx) { + FatalErrorContext ams_ctx; + + /* Convert thread dump to atmosphere dump. */ + { + ams_ctx.magic = FatalErrorContext::Magic; + ams_ctx.error_desc = ctx->error_desc; + ams_ctx.title_id = static_cast(StratosphereTitleId); + for (size_t i = 0; i < FatalErrorContext::NumGprs; i++) { + ams_ctx.gprs[i] = ctx->cpu_gprs[i].x; + } + if (ams_ctx.error_desc == FatalErrorContext::DataAbortErrorDesc && + ams_ctx.gprs[27] == FatalErrorContext::StdAbortMagicAddress && + ams_ctx.gprs[28] == FatalErrorContext::StdAbortMagicValue) + { + /* Detect std::abort(). */ + ams_ctx.error_desc = FatalErrorContext::StdAbortErrorDesc; + } + + ams_ctx.fp = ctx->fp.x; + ams_ctx.lr = ctx->lr.x; + ams_ctx.sp = ctx->sp.x; + ams_ctx.pc = ctx->pc.x; + ams_ctx.pstate = ctx->pstate; + ams_ctx.afsr0 = ctx->afsr0; + ams_ctx.afsr1 = ctx->afsr1; + ams_ctx.far = ctx->far.x; + ams_ctx.report_identifier = armGetSystemTick(); + /* Grab module base. */ + { + MemoryInfo mem_info; + u32 page_info; + if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, GetPc()))) { + ams_ctx.module_base = mem_info.addr; + } else { + ams_ctx.module_base = 0; + } + } + ams_ctx.stack_trace_size = 0; + u64 cur_fp = ams_ctx.fp; + for (size_t i = 0; i < FatalErrorContext::MaxStackTrace; i++) { + /* Validate current frame. */ + if (cur_fp == 0 || (cur_fp & 0xF)) { + break; + } + + /* Read a new frame. */ + StackFrame cur_frame; + MemoryInfo mem_info; + u32 page_info; + if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, cur_fp)) && (mem_info.perm & Perm_R) == Perm_R) { + std::memcpy(&cur_frame, reinterpret_cast(cur_fp), sizeof(cur_frame)); + } else { + break; + } + + /* Advance to the next frame. */ + ams_ctx.stack_trace[ams_ctx.stack_trace_size++] = cur_frame.lr; + cur_fp = cur_frame.fp; + } + /* Clear unused parts of stack trace. */ + for (size_t i = ams_ctx.stack_trace_size; i < FatalErrorContext::MaxStackTrace; i++) { + ams_ctx.stack_trace[i] = 0; + } + + /* Grab up to 0x100 of stack. */ + { + MemoryInfo mem_info; + u32 page_info; + if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, ams_ctx.sp)) && (mem_info.perm & Perm_R) == Perm_R) { + size_t copy_size = std::min(FatalErrorContext::MaxStackDumpSize, static_cast(mem_info.addr + mem_info.size - ams_ctx.sp)); + ams_ctx.stack_dump_size = copy_size; + std::memcpy(ams_ctx.stack_dump, reinterpret_cast(ams_ctx.sp), copy_size); + } else { + ams_ctx.stack_dump_size = 0; + } + } + } + + /* Just call the user exception handler. */ + ::sts::ams::ExceptionHandler(&ams_ctx); + } + + inline __attribute((noreturn)) void AbortImpl() { + /* Just perform a data abort. */ + register u64 addr __asm__("x27") = FatalErrorContext::StdAbortMagicAddress; + register u64 val __asm__("x28") = FatalErrorContext::StdAbortMagicValue; + while (true) { + __asm__ __volatile__ ( + "str %[val], [%[addr]]" + : + : [val]"r"(val), [addr]"r"(addr) + ); + } + } + +} + +extern "C" { + + /* Redefine abort to trigger these handlers. */ + void abort(); + +} + +/* Custom abort handler, so that std::abort will trigger these. */ +void abort() { + sts::ams::AbortImpl(); +} \ No newline at end of file diff --git a/source/ams/ams_exosphere_api.cpp b/source/ams/ams_exosphere_api.cpp new file mode 100644 index 00000000..39c8f60a --- /dev/null +++ b/source/ams/ams_exosphere_api.cpp @@ -0,0 +1,75 @@ +/* + * 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 . + */ + +#include +#include +#include + +namespace sts::ams { + + ApiInfo GetApiInfo() { + u64 exosphere_cfg; + if (spl::smc::GetConfig(&exosphere_cfg, 1, SplConfigItem_ExosphereApiVersion) != spl::smc::Result::Success) { + R_ASSERT(ResultAtmosphereExosphereNotPresent); + } + + return ApiInfo{ + .major_version = static_cast((exosphere_cfg >> 0x20) & 0xFF), + .minor_version = static_cast((exosphere_cfg >> 0x18) & 0xFF), + .micro_version = static_cast((exosphere_cfg >> 0x10) & 0xFF), + .target_firmware = static_cast((exosphere_cfg >> 0x08) & 0xFF), + .master_key_revision = static_cast((exosphere_cfg >> 0x00) & 0xFF), + }; + } + + void ForceRebootToRcm() { + R_ASSERT(spl::smc::ConvertResult(spl::smc::SetConfig(SplConfigItem_ExosphereNeedsReboot, 1))); + } + + void ForceRebootToIramPayload() { + R_ASSERT(spl::smc::ConvertResult(spl::smc::SetConfig(SplConfigItem_ExosphereNeedsReboot, 2))); + } + + void ForceShutdown() { + R_ASSERT(spl::smc::ConvertResult(spl::smc::SetConfig(SplConfigItem_ExosphereNeedsShutdown, 1))); + } + + void CopyToIram(uintptr_t iram_dst, const void *dram_src, size_t size) { + spl::smc::AtmosphereCopyToIram(iram_dst, dram_src, size); + } + + void CopyFromIram(void *dram_dst, uintptr_t iram_src, size_t size) { + spl::smc::AtmosphereCopyFromIram(dram_dst, iram_src, size); + } + + namespace { + + inline Result GetRcmBugPatched(bool *out) { + u64 tmp; + R_TRY(spl::smc::ConvertResult(spl::smc::GetConfig(&tmp, 1, SplConfigItem_ExosphereHasRcmBugPatch))); + *out = (tmp != 0); + return ResultSuccess; + } + + } + + bool IsRcmBugPatched() { + bool rcm_bug_patched; + R_ASSERT(GetRcmBugPatched(&rcm_bug_patched)); + return rcm_bug_patched; + } + +} diff --git a/source/ams/ams_hos_version_api.cpp b/source/ams/ams_hos_version_api.cpp index 7a0404e0..5f6ac7b3 100644 --- a/source/ams/ams_hos_version_api.cpp +++ b/source/ams/ams_hos_version_api.cpp @@ -35,19 +35,7 @@ namespace sts::hos { return; } - /* TODO: spl::smc:: */ - u32 target_fw = 0; - { - SecmonArgs args = {0}; - args.X[0] = 0xC3000002; /* smcGetConfig */ - args.X[1] = 65000; /* ConfigItem_ExosphereVersion */ - R_ASSERT(svcCallSecureMonitor(&args)); - STS_ASSERT(args.X[0] == 0); - - target_fw = (args.X[1] >> 0x08) & 0xFF; - } - - switch (static_cast(target_fw)) { + switch (ams::GetApiInfo().GetTargetFirmware()) { case ams::TargetFirmware_100: g_hos_version = hos::Version_100; break; diff --git a/source/bpc_ams.c b/source/bpc_ams.c deleted file mode 100644 index 0728e091..00000000 --- a/source/bpc_ams.c +++ /dev/null @@ -1,76 +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 . - */ - -#include -#include -#include - -static Service g_bpcAmsSrv; -static u64 g_bpcAmsAmsRefcnt; - -Result bpcAmsInitialize(void) { - atomicIncrement64(&g_bpcAmsAmsRefcnt); - - if (serviceIsActive(&g_bpcAmsSrv)) { - return 0; - } - - Handle h; - Result rc = svcConnectToNamedPort(&h, "bpc:ams"); - if (R_SUCCEEDED(rc)) { - serviceCreate(&g_bpcAmsSrv, h); - } - - return rc; -} - -void bpcAmsExit(void) { - if (atomicDecrement64(&g_bpcAmsAmsRefcnt) == 0) - serviceClose(&g_bpcAmsSrv); -} - -Result bpcAmsRebootToFatalError(AtmosphereFatalErrorContext *ctx) { - IpcCommand c; - ipcInitialize(&c); - ipcAddSendBuffer(&c, ctx, sizeof(*ctx), BufferType_Normal); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_bpcAmsSrv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - - Result rc = serviceIpcDispatch(&g_bpcAmsSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_bpcAmsSrv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} diff --git a/source/dd/dd_io_mappings.cpp b/source/dd/dd_io_mappings.cpp new file mode 100644 index 00000000..5bb6b2ae --- /dev/null +++ b/source/dd/dd_io_mappings.cpp @@ -0,0 +1,55 @@ +/* + * 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 . + */ +#include + +namespace sts::dd { + + uintptr_t QueryIoMapping(uintptr_t phys_addr, size_t size) { + u64 virtual_addr; + const u64 aligned_addr = util::AlignDown(phys_addr, 0x1000); + const size_t offset = phys_addr - aligned_addr; + const u64 aligned_size = size + offset; + R_TRY_CATCH(svcQueryIoMapping(&virtual_addr, aligned_addr, aligned_size)) { + /* Official software handles this by returning 0. */ + R_CATCH(ResultKernelNotFound) { return 0; } + } R_END_TRY_CATCH_WITH_ASSERT; + + return static_cast(virtual_addr + offset); + } + + namespace { + + inline u32 ReadWriteRegisterImpl(uintptr_t phys_addr, u32 value, u32 mask) { + u32 out_value; + R_ASSERT(svcReadWriteRegister(&out_value, phys_addr, mask, value)); + return out_value; + } + + } + + u32 ReadRegister(uintptr_t phys_addr) { + return ReadWriteRegisterImpl(phys_addr, 0, 0); + } + + void WriteRegister(uintptr_t phys_addr, u32 value) { + ReadWriteRegisterImpl(phys_addr, value, ~u32()); + } + + u32 ReadWriteRegister(uintptr_t phys_addr, u32 value, u32 mask) { + return ReadWriteRegisterImpl(phys_addr, value, mask); + } + +} diff --git a/source/dmnt/dmntcht.c b/source/dmnt/dmntcht.c new file mode 100644 index 00000000..86ed9be2 --- /dev/null +++ b/source/dmnt/dmntcht.c @@ -0,0 +1,166 @@ +/* + * 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 . + */ +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "../service_guard.h" +#include "dmntcht.h" + +static Service g_dmntchtSrv; + +NX_GENERATE_SERVICE_GUARD(dmntcht); + +Result _dmntchtInitialize(void) { + return smGetService(&g_dmntchtSrv, "dmnt:cht"); +} + +void _dmntchtCleanup(void) { + serviceClose(&g_dmntchtSrv); +} + +Service* dmntchtGetServiceSession(void) { + return &g_dmntchtSrv; +} + +Result dmntchtHasCheatProcess(bool *out) { + u8 tmp; + Result rc = serviceDispatchOut(&g_dmntchtSrv, 65000, tmp); + if (R_SUCCEEDED(rc) && out) *out = tmp & 1; +} + +Result dmntchtGetCheatProcessEvent(Event *event) { + Handle evt_handle; + Result rc = serviceDispatch(&g_dmntchtSrv, 65001, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = &evt_handle, + ); + + if (R_SUCCEEDED(rc)) { + eventLoadRemote(&g_dmntchtSrv, evt_handle, true); + } + + return rc; +} + +Result dmntchtGetCheatProcessMetadata(DmntCheatProcessMetadata *out_metadata) { + return serviceDispatchOut(&g_dmntchtSrv, 65002, *out_metadata); +} + +Result dmntchtForceOpenCheatProcess(void) { + return serviceDispatch(&g_dmntchtSrv, 65003); +} + +static Result _dmntchtGetCount(u64 *out_count, u32 cmd_id) { + return serviceDispatchOut(&g_dmntchtSrv, cmd_id, *out_count); +} + +static Result _dmntchtGetEntries(void *buffer, u64 buffer_size, u64 offset, u64 *out_count, u32 cmd_id) { + return serviceDispatchInOut(&g_dmntchtSrv, cmd_id, offset, *out_count, + .buffer_attrs = { SfBufferAttr_Out | SfBufferAttr_HipcMapAlias }, + .buffers = { { buffer, buffer_size } }, + ); +} + +static Result _dmntchtCmdInU32NoOut(u32 in, u32 cmd_id) { + return serviceDispatchIn(&g_dmntchtSrv, cmd_id, in); +} + +Result dmntchtGetCheatProcessMappingCount(u64 *out_count) { + return _dmntchtGetCount(65100, out_count); +} + +Result dmntchtGetCheatProcessMappings(MemoryInfo *buffer, u64 max_count, u64 offset, u64 *out_count) { + return _dmntchtGetEntries(65101, buffer, sizeof(*buffer) * max_count, offset, out_count); +} + +Result dmntchtReadCheatProcessMemory(u64 address, void *buffer, size_t size) { + const struct { + u64 address; + u64 size; + } in = { address, size }; + return serviceDispatchIn(&g_dmntchtSrv, 65102, in, + .buffer_attrs = { SfBufferAttr_Out | SfBufferAttr_HipcMapAlias }, + .buffers = { { buffer, size } }, + ); +} + +Result dmntchtWriteCheatProcessMemory(u64 address, const void *buffer, size_t size) { + const struct { + u64 address; + u64 size; + } in = { address, size }; + return serviceDispatchIn(&g_dmntchtSrv, 65103, in, + .buffer_attrs = { SfBufferAttr_In | SfBufferAttr_HipcMapAlias }, + .buffers = { { buffer, size } }, + ); +} + +Result dmntchtQueryCheatProcessMemory(MemoryInfo *mem_info, u64 address){ + return serviceDispatchInOut(&g_dmntchtSrv, 65104, address, *mem_info); +} + +Result dmntchtGetCheatCount(u64 *out_count) { + return _dmntchtGetCount(65200, out_count); +} + +Result dmntchtGetCheats(DmntCheatEntry *buffer, u64 max_count, u64 offset, u64 *out_count) { + return _dmntchtGetEntries(65201, buffer, sizeof(*buffer) * max_count, offset, out_count); +} + +Result dmntchtGetCheatById(DmntCheatEntry *out, u32 cheat_id) { + return serviceDispatchIn(&g_dmntchtSrv, 65202, cheat_id, + .buffer_attrs = { SfBufferAttr_Out | SfBufferAttr_HipcMapAlias | SfBufferAttr_FixedSize }, + .buffers = { { out, sizeof(*out) } }, + ); +} + +Result dmntchtToggleCheat(u32 cheat_id) { + return _dmntchtCmdInU32NoOut(cheat_id, 65203); +} + +Result dmntchtAddCheat(DmntCheatDefinition *cheat_def, bool enabled, u32 *out_cheat_id) { + const u8 in = enabled != 0; + return serviceDispatchInOut(&g_dmntchtSrv, 65204, in, *out_cheat_id, + .buffer_attrs = { SfBufferAttr_In | SfBufferAttr_HipcMapAlias | SfBufferAttr_FixedSize }, + .buffers = { { cheat_def, sizeof(*cheat_def) } }, + ); +} + +Result dmntchtRemoveCheat(u32 cheat_id) { + return _dmntchtCmdInU32NoOut(cheat_id, 65205); +} + +Result dmntchtGetFrozenAddressCount(u64 *out_count) { + return _dmntchtGetCount(65300, out_count); +} + +Result dmntchtGetFrozenAddresses(DmntFrozenAddressEntry *buffer, u64 max_count, u64 offset, u64 *out_count) { + return _dmntchtGetEntries(65301, buffer, sizeof(*buffer) * max_count, offset, out_count); +} + +Result dmntchtGetFrozenAddress(DmntFrozenAddressEntry *out, u64 address) { + return serviceDispatchInOut(&g_dmntchtSrv, 65302, address, *out); +} + +Result dmntchtEnableFrozenAddress(u64 address, u64 width, u64 *out_value) { + const struct { + u64 address; + u64 width; + } in = { address, width }; + return serviceDispatchInOut(&g_dmntchtSrv, 65303, in, *out_value); +} + +Result dmntchtDisableFrozenAddress(u64 address) { + return serviceDispatchIn(&g_dmntchtSrv, 65304, address); +} diff --git a/include/stratosphere/services/dmntcht.h b/source/dmnt/dmntcht.h similarity index 96% rename from include/stratosphere/services/dmntcht.h rename to source/dmnt/dmntcht.h index 6f8d5ddc..c1dc6f71 100644 --- a/include/stratosphere/services/dmntcht.h +++ b/source/dmnt/dmntcht.h @@ -15,7 +15,9 @@ */ #pragma once -#include +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -86,7 +88,6 @@ Result dmntchtGetFrozenAddress(DmntFrozenAddressEntry *out, u64 address); Result dmntchtEnableFrozenAddress(u64 address, u64 width, u64 *out_value); Result dmntchtDisableFrozenAddress(u64 address); - #ifdef __cplusplus } -#endif \ No newline at end of file +#endif diff --git a/source/dmntcht.c b/source/dmntcht.c deleted file mode 100644 index eb6476f0..00000000 --- a/source/dmntcht.c +++ /dev/null @@ -1,650 +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 . - */ - -#include -#include -#include - -static Service g_dmntchtService; -static u64 g_refCnt; - -static Result _dmntchtGetCount(u64 cmd_id, u64 *out_count); -static Result _dmntchtGetEntries(u64 cmd_id, void *buffer, u64 buffer_size, u64 offset, u64 *out_count); - -Result dmntchtInitialize(void) { - atomicIncrement64(&g_refCnt); - - if (serviceIsActive(&g_dmntchtService)) { - return 0; - } - - return smGetService(&g_dmntchtService, "dmnt:cht"); -} - -void dmntchtExit(void) { - if (atomicIncrement64(&g_refCnt) == 0) { - serviceClose(&g_dmntchtService); - } -} - -Service* dmntchtGetServiceSession(void) { - return &g_dmntchtService; -} - -Result dmntchtHasCheatProcess(bool *out) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - bool out; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (out) *out = resp->out; - } - } - - return rc; -} - -Result dmntchtGetCheatProcessEvent(Event *event) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65001; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - eventLoadRemote(event, r.Handles[0], true); - } - } - - return rc; -} - -Result dmntchtGetCheatProcessMetadata(DmntCheatProcessMetadata *out_metadata) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65002; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - DmntCheatProcessMetadata metadata; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (out_metadata) *out_metadata = resp->metadata; - } - } - - return rc; -} - -Result dmntchtForceOpenCheatProcess(void) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65003; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -static Result _dmntchtGetCount(u64 cmd_id, u64 *out_count) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 count; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - *out_count = resp->count; - } - - return rc; -} - -static Result _dmntchtGetEntries(u64 cmd_id, void *buffer, u64 buffer_size, u64 offset, u64 *out_count) { - IpcCommand c; - ipcInitialize(&c); - ipcAddRecvBuffer(&c, buffer, buffer_size, 0); - - struct { - u64 magic; - u64 cmd_id; - u64 offset; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = cmd_id; - raw->offset = offset; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 count; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (out_count) *out_count = resp->count; - } - } - - return rc; -} - -Result dmntchtGetCheatProcessMappingCount(u64 *out_count) { - return _dmntchtGetCount(65100, out_count); -} - -Result dmntchtGetCheatProcessMappings(MemoryInfo *buffer, u64 max_count, u64 offset, u64 *out_count) { - return _dmntchtGetEntries(65101, buffer, sizeof(*buffer) * max_count, offset, out_count); -} - -Result dmntchtReadCheatProcessMemory(u64 address, void *buffer, size_t size) { - IpcCommand c; - ipcInitialize(&c); - ipcAddRecvBuffer(&c, buffer, size, 0); - - struct { - u64 magic; - u64 cmd_id; - u64 address; - u64 size; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65102; - raw->address = address; - raw->size = size; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -Result dmntchtWriteCheatProcessMemory(u64 address, const void *buffer, size_t size) { - IpcCommand c; - ipcInitialize(&c); - ipcAddSendBuffer(&c, buffer, size, 0); - - struct { - u64 magic; - u64 cmd_id; - u64 address; - u64 size; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65103; - raw->address = address; - raw->size = size; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -Result dmntchtQueryCheatProcessMemory(MemoryInfo *mem_info, u64 address){ - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 address; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65104; - raw->address = address; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - MemoryInfo mem_info; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (mem_info) *mem_info = resp->mem_info; - } - } - - return rc; -} - -Result dmntchtGetCheatCount(u64 *out_count) { - return _dmntchtGetCount(65200, out_count); -} - -Result dmntchtGetCheats(DmntCheatEntry *buffer, u64 max_count, u64 offset, u64 *out_count) { - return _dmntchtGetEntries(65201, buffer, sizeof(*buffer) * max_count, offset, out_count); -} - -Result dmntchtGetCheatById(DmntCheatEntry *buffer, u32 cheat_id) { - IpcCommand c; - ipcInitialize(&c); - ipcAddRecvBuffer(&c, buffer, sizeof(*buffer), 0); - - struct { - u64 magic; - u64 cmd_id; - u32 cheat_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65202; - raw->cheat_id = cheat_id; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -Result dmntchtToggleCheat(u32 cheat_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 cheat_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65203; - raw->cheat_id = cheat_id; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - -Result dmntchtAddCheat(DmntCheatDefinition *buffer, bool enabled, u32 *out_cheat_id) { - IpcCommand c; - ipcInitialize(&c); - ipcAddSendBuffer(&c, buffer, sizeof(*buffer), 0); - - struct { - u64 magic; - u64 cmd_id; - u8 enabled; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65204; - raw->enabled = enabled; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u32 cheat_id; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (out_cheat_id) *out_cheat_id = resp->cheat_id; - } - } - - return rc; -} - -Result dmntchtRemoveCheat(u32 cheat_id) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 cheat_id; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65205; - raw->cheat_id = cheat_id; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} - - -Result dmntchtGetFrozenAddressCount(u64 *out_count) { - return _dmntchtGetCount(65300, out_count); -} - -Result dmntchtGetFrozenAddresses(DmntFrozenAddressEntry *buffer, u64 max_count, u64 offset, u64 *out_count) { - return _dmntchtGetEntries(65301, buffer, sizeof(*buffer) * max_count, offset, out_count); -} - -Result dmntchtGetFrozenAddress(DmntFrozenAddressEntry *out, u64 address) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 address; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65302; - raw->address = address; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - DmntFrozenAddressEntry entry; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (out) *out = resp->entry; - } - } - - return rc; -} - -Result dmntchtEnableFrozenAddress(u64 address, u64 width, u64 *out_value) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 address; - u64 width; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65303; - raw->address = address; - raw->width = width; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 value; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - if (out_value) *out_value = resp->value; - } - } - - return rc; -} - -Result dmntchtDisableFrozenAddress(u64 address) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 address; - } *raw; - - raw = serviceIpcPrepareHeader(&g_dmntchtService, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65304; - raw->address = address; - - Result rc = serviceIpcDispatch(&g_dmntchtService); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 value; - } *resp; - - serviceIpcParse(&g_dmntchtService, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; -} diff --git a/source/emummc_utilities.cpp b/source/emummc_utilities.cpp deleted file mode 100644 index 59c4267c..00000000 --- a/source/emummc_utilities.cpp +++ /dev/null @@ -1,141 +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 . - */ - -#include -#include - -/* EFS0 */ -static constexpr u32 EmummcStorageMagic = 0x30534645; -static constexpr size_t EmummcMaxDirLength = 0x7F; - -struct EmummcBaseConfig { - u32 magic; - u32 type; - u32 id; - u32 fs_version; -}; - -struct EmummcPartitionConfig { - u64 start_sector; -}; - -struct EmummcFileConfig { - char path[EmummcMaxDirLength+1]; -}; - -struct ExoEmummcConfig { - EmummcBaseConfig base_cfg; - union { - EmummcPartitionConfig partition_cfg; - EmummcFileConfig file_cfg; - }; - char emu_dir_path[EmummcMaxDirLength+1]; -}; - -enum EmummcType { - EmummcType_Emmc = 0, - EmummcType_Sd, - EmummcType_SdFile, - - EmummcType_Max, -}; - -static bool g_IsEmummc = false; -static bool g_HasCached = false; -static Mutex g_Mutex; -static ExoEmummcConfig g_exo_emummc_config; - -static void _CacheValues(void) -{ - if (__atomic_load_n(&g_HasCached, __ATOMIC_SEQ_CST)) - return; - - mutexLock(&g_Mutex); - - if (g_HasCached) { - mutexUnlock(&g_Mutex); - return; - } - - static struct { - char file_path[EmummcMaxDirLength+1]; - char nintendo_path[EmummcMaxDirLength+1]; - } __attribute__((aligned(0x1000))) paths; - - { - SecmonArgs args = {0}; - args.X[0] = 0xF0000404; /* smcAmsGetEmunandConfig */ - args.X[1] = 0; /* NAND */ - args.X[2] = reinterpret_cast(&paths); /* path output */ - R_ASSERT(svcCallSecureMonitor(&args)); - STS_ASSERT(args.X[0] == 0); - std::memcpy(&g_exo_emummc_config, &args.X[1], sizeof(args) - sizeof(args.X[0])); - } - - const EmummcType emummc_type = static_cast(g_exo_emummc_config.base_cfg.type); - -/* Ignore format warnings. */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-truncation" - switch (emummc_type) { - case EmummcType_SdFile: - std::snprintf(g_exo_emummc_config.file_cfg.path, sizeof(g_exo_emummc_config.file_cfg.path), "/%s", paths.file_path); - break; - default: - break; - } - - std::snprintf(g_exo_emummc_config.emu_dir_path, sizeof(g_exo_emummc_config.emu_dir_path), "/%s", paths.nintendo_path); - - g_IsEmummc = g_exo_emummc_config.base_cfg.magic == EmummcStorageMagic && emummc_type != EmummcType_Emmc; - - /* Default Nintendo redirection path. */ - if (g_IsEmummc) { - if (std::strcmp(g_exo_emummc_config.emu_dir_path, "/") == 0) { - std::snprintf(g_exo_emummc_config.emu_dir_path, sizeof(g_exo_emummc_config.emu_dir_path), "/emummc/Nintendo_%04x", g_exo_emummc_config.base_cfg.id); - } - } -#pragma GCC diagnostic pop - - __atomic_store_n(&g_HasCached, true, __ATOMIC_SEQ_CST); - - mutexUnlock(&g_Mutex); -} - - -/* Get whether emummc is active. */ -bool IsEmummc() { - _CacheValues(); - return g_IsEmummc; -} - -/* Get Nintendo redirection path. */ -const char *GetEmummcNintendoDirPath() { - _CacheValues(); - if (!g_IsEmummc) { - return nullptr; - } - return g_exo_emummc_config.emu_dir_path; -} - -/* Get Emummc folderpath, NULL if not file-based. */ -const char *GetEmummcFilePath() { - _CacheValues(); - if (!g_IsEmummc || g_exo_emummc_config.base_cfg.type != EmummcType_SdFile) { - return nullptr; - } - return g_exo_emummc_config.file_cfg.path; -} diff --git a/source/ldr/ldr_ams.c b/source/ldr/ldr_ams.c index 94c042e1..59ec1324 100644 --- a/source/ldr/ldr_ams.c +++ b/source/ldr/ldr_ams.c @@ -18,44 +18,9 @@ #include "ldr_ams.h" static Result _ldrAtmosphereHasLaunchedTitle(Service *srv, bool *out, u64 tid) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 title_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - raw->title_id = tid; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 has_launched_title; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out = resp->has_launched_title != 0; - } else { - rc = 0x666; - } - } else { - rc = 0x555; - } - + u8 tmp; + Result rc = serviceDispatchInOut(srv, 65000, tid, tmp); + if (R_SUCCEEDED(rc) && out) *out = tmp & 1; return rc; } diff --git a/source/on_crash.cpp b/source/on_crash.cpp deleted file mode 100644 index 2621a51a..00000000 --- a/source/on_crash.cpp +++ /dev/null @@ -1,142 +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 . - */ - -#include -#include -#include - -WEAK sts::ncm::TitleId __stratosphere_title_id = sts::ncm::TitleId::Invalid; - -extern "C" { - void WEAK __libstratosphere_exception_handler(AtmosphereFatalErrorContext *ctx); - - /* Redefine abort, so that it triggers these handlers. */ - void abort(); -}; - -static inline u64 GetPc() { - u64 pc; - __asm__ __volatile__ ("adr %[pc], ." : [pc]"=&r"(pc) :: ); - return pc; -} - -struct StackFrame { - u64 fp; - u64 lr; -}; - -void StratosphereCrashHandler(ThreadExceptionDump *ctx) { - AtmosphereFatalErrorContext ams_ctx; - /* Convert thread dump to atmosphere dump. */ - { - ams_ctx.magic = AtmosphereFatalErrorMagic; - ams_ctx.error_desc = ctx->error_desc; - ams_ctx.title_id = static_cast(__stratosphere_title_id); - for (size_t i = 0; i < AtmosphereFatalErrorNumGprs; i++) { - ams_ctx.gprs[i] = ctx->cpu_gprs[i].x; - } - if (ams_ctx.error_desc == DATA_ABORT_ERROR_DESC && - ams_ctx.gprs[2] == STD_ABORT_ADDR_MAGIC && - ams_ctx.gprs[3] == STD_ABORT_VALUE_MAGIC) { - /* Detect std::abort(). */ - ams_ctx.error_desc = STD_ABORT_ERROR_DESC; - } - - ams_ctx.fp = ctx->fp.x; - ams_ctx.lr = ctx->lr.x; - ams_ctx.sp = ctx->sp.x; - ams_ctx.pc = ctx->pc.x; - ams_ctx.pstate = ctx->pstate; - ams_ctx.afsr0 = ctx->afsr0; - ams_ctx.afsr1 = ctx->afsr1; - ams_ctx.far = ctx->far.x; - ams_ctx.report_identifier = armGetSystemTick(); - /* Grab module base. */ - { - MemoryInfo mem_info; - u32 page_info; - if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, GetPc()))) { - ams_ctx.module_base = mem_info.addr; - } else { - ams_ctx.module_base = 0; - } - } - ams_ctx.stack_trace_size = 0; - u64 cur_fp = ams_ctx.fp; - for (size_t i = 0; i < AMS_FATAL_ERROR_MAX_STACKTRACE; i++) { - /* Validate current frame. */ - if (cur_fp == 0 || (cur_fp & 0xF)) { - break; - } - - /* Read a new frame. */ - StackFrame cur_frame; - MemoryInfo mem_info; - u32 page_info; - if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, cur_fp)) && (mem_info.perm & Perm_R) == Perm_R) { - std::memcpy(&cur_frame, reinterpret_cast(cur_fp), sizeof(cur_frame)); - } else { - break; - } - - /* Advance to the next frame. */ - ams_ctx.stack_trace[ams_ctx.stack_trace_size++] = cur_frame.lr; - cur_fp = cur_frame.fp; - } - /* Clear unused parts of stack trace. */ - for (size_t i = ams_ctx.stack_trace_size; i < AMS_FATAL_ERROR_MAX_STACKTRACE; i++) { - ams_ctx.stack_trace[i] = 0; - } - - /* Grab up to 0x100 of stack. */ - { - MemoryInfo mem_info; - u32 page_info; - if (R_SUCCEEDED(svcQueryMemory(&mem_info, &page_info, ams_ctx.sp)) && (mem_info.perm & Perm_R) == Perm_R) { - size_t copy_size = std::min(static_cast(AMS_FATAL_ERROR_MAX_STACKDUMP), static_cast(mem_info.addr + mem_info.size - ams_ctx.sp)); - ams_ctx.stack_dump_size = copy_size; - std::memcpy(ams_ctx.stack_dump, reinterpret_cast(ams_ctx.sp), copy_size); - } else { - ams_ctx.stack_dump_size = 0; - } - } - } - - /* Just call the user exception handler. */ - __libstratosphere_exception_handler(&ams_ctx); -} - -/* Default exception handler behavior. */ -void WEAK __libstratosphere_exception_handler(AtmosphereFatalErrorContext *ctx) { - R_ASSERT(bpcAmsInitialize()); - R_ASSERT(bpcAmsRebootToFatalError(ctx)); - bpcAmsExit(); - while (1) { } -} - -/* Custom abort handler, so that std::abort will trigger these. */ -void abort() { - /* Just perform a data abort. */ - register u64 addr __asm__("x2") = STD_ABORT_ADDR_MAGIC; - register u64 val __asm__("x3") = STD_ABORT_VALUE_MAGIC; - while (true) { - __asm__ __volatile__ ( - "str %[val], [%[addr]]" - : - : [val]"r"(val), [addr]"r"(addr) - ); - } -} \ No newline at end of file diff --git a/source/pm/pm_ams.c b/source/pm/pm_ams.c index 2e1c1988..aeb6bf1c 100644 --- a/source/pm/pm_ams.c +++ b/source/pm/pm_ams.c @@ -18,124 +18,35 @@ #include "pm_ams.h" Result pminfoAtmosphereGetProcessId(u64 *out_pid, u64 tid) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = pminfoGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 title_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - raw->title_id = tid; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 pid; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out_pid = resp->pid; - } - } - - return rc; + return serviceDispatchInOut(pminfoGetServiceSession(), 65000, tid, *out_pid); } Result pminfoAtmosphereHasLaunchedTitle(bool *out, u64 tid) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = pminfoGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 title_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65001; - raw->title_id = tid; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 has_launched_title; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out = resp->has_launched_title != 0; - } - } - + u8 tmp; + Result rc = serviceDispatchInOut(pminfoGetServiceSession(), 65001, tid, tmp); + if (R_SUCCEEDED(rc) && out) *out = tmp & 1; return rc; } -Result pmdmntAtmosphereGetProcessInfo(Handle* out, u64 *tid_out, u8 *sid_out, u64 pid) { - IpcCommand c; - ipcInitialize(&c); - Service *s = pmdmntGetServiceSession(); - +Result pmdmntAtmosphereGetProcessInfo(Handle* handle_out, u64 *tid_out, u8 *sid_out, u64 pid) { struct { - u64 magic; - u64 cmd_id; - u64 pid; - } *raw; + u64 title_id; + u8 storage_id; + } out; + Handle tmp_handle; - raw = serviceIpcPrepareHeader(s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - raw->pid = pid; - - Result rc = serviceIpcDispatch(s); + Result rc = serviceDispatchInOut(pmdmntGetServiceSession(), 65000, pid, out, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = &tmp_handle, + ); if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 title_id; - FsStorageId storage_id; - } *resp; - - serviceIpcParse(s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - if (out) { - *out = r.Handles[0]; - } else { - svcCloseHandle(r.Handles[0]); - } - if (tid_out) *tid_out = resp->title_id; - if (sid_out) *sid_out = resp->storage_id; + if (tid_out) *tid_out = out.title_id; + if (sid_out) *sid_out = out.storage_id; + if (handle_out) { + *handle_out = tmp_handle; + } else { + svcCloseHandle(tmp_handle); } } @@ -143,44 +54,20 @@ Result pmdmntAtmosphereGetProcessInfo(Handle* out, u64 *tid_out, u8 *sid_out, u6 } Result pmdmntAtmosphereGetCurrentLimitInfo(u64 *out_cur, u64 *out_lim, u32 group, u32 resource) { - IpcCommand c; - ipcInitialize(&c); - Service *s = pmdmntGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 group; u32 resource; - } *raw; + } in = { group, resource }; + struct { + u64 cur; + u64 lim; + } out; - raw = serviceIpcPrepareHeader(s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65001; - raw->group = group; - raw->resource = resource; - - Result rc = serviceIpcDispatch(s); + Result rc = serviceDispatchInOut(pmdmntGetServiceSession(), 65001, in, out); if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 cur_value; - u64 lim_value; - } *resp; - - serviceIpcParse(s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - if (out_cur) *out_cur = resp->cur_value; - if (out_lim) *out_lim = resp->lim_value; - } + if (out_cur) *out_cur = out.cur; + if (out_lim) *out_lim = out.lim; } return rc; diff --git a/source/service_guard.h b/source/service_guard.h new file mode 100644 index 00000000..588a0c99 --- /dev/null +++ b/source/service_guard.h @@ -0,0 +1,64 @@ +#pragma once +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ServiceGuard { + Mutex mutex; + u32 refCount; +} ServiceGuard; + +NX_INLINE bool serviceGuardBeginInit(ServiceGuard* g) +{ + mutexLock(&g->mutex); + return (g->refCount++) == 0; +} + +NX_INLINE Result serviceGuardEndInit(ServiceGuard* g, Result rc, void (*cleanupFunc)(void)) +{ + if (R_FAILED(rc)) { + cleanupFunc(); + --g->refCount; + } + mutexUnlock(&g->mutex); + return rc; +} + +NX_INLINE void serviceGuardExit(ServiceGuard* g, void (*cleanupFunc)(void)) +{ + mutexLock(&g->mutex); + if (g->refCount && (--g->refCount) == 0) + cleanupFunc(); + mutexUnlock(&g->mutex); +} + +#define NX_GENERATE_SERVICE_GUARD_PARAMS(name, _paramdecl, _parampass) \ +\ +static ServiceGuard g_##name##Guard; \ +NX_INLINE Result _##name##Initialize _paramdecl; \ +static void _##name##Cleanup(void); \ +\ +Result name##Initialize _paramdecl \ +{ \ + Result rc = 0; \ + if (serviceGuardBeginInit(&g_##name##Guard)) \ + rc = _##name##Initialize _parampass; \ + return serviceGuardEndInit(&g_##name##Guard, rc, _##name##Cleanup); \ +} \ +\ +void name##Exit(void) \ +{ \ + serviceGuardExit(&g_##name##Guard, _##name##Cleanup); \ +} + +#define NX_GENERATE_SERVICE_GUARD(name) NX_GENERATE_SERVICE_GUARD_PARAMS(name, (void), ()) + +#ifdef __cplusplus +} +#endif diff --git a/source/sm/sm_ams.c b/source/sm/sm_ams.c index 628ff2fb..b345e42a 100644 --- a/source/sm/sm_ams.c +++ b/source/sm/sm_ams.c @@ -13,166 +13,42 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - -#include -#include +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "../service_guard.h" #include "sm_ams.h" -static Service g_smMitmSrv; -static u64 g_mitmRefCnt; +static Result _smAtmosphereCmdHas(bool *out, u64 service_name, u32 cmd_id) { + u8 tmp; + Result rc = serviceDispatchInOut(smGetServiceSession(), cmd_id, service_name, tmp); + if (R_SUCCEEDED(rc) && out) *out = tmp & 1; + return rc; +} + +static Result _smAtmosphereCmdInServiceNameNoOut(u64 service_name, Service *srv, u32 cmd_id) { + return serviceDispatchIn(srv, cmd_id, service_name); +} Result smAtmosphereHasService(bool *out, const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = smGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65100; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 has_service; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out = resp->has_service != 0; - } - } - - return rc; + return _smAtmosphereCmdHas(out, smEncodeName(name), 65100); } Result smAtmosphereWaitService(const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = smGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65101; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _smAtmosphereCmdInServiceNameNoOut(smEncodeName(name), smGetServiceSession(), 65101); } Result smAtmosphereHasMitm(bool *out, const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = smGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65004; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 has_mitm; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out = resp->has_mitm != 0; - } - } - - return rc; + return _smAtmosphereCmdHas(out, smEncodeName(name), 65004); } Result smAtmosphereWaitMitm(const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = smGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65005; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _smAtmosphereCmdInServiceNameNoOut(smEncodeName(name), smGetServiceSession(), 65005); } -Result smAtmosphereMitmInitialize(void) { - atomicIncrement64(&g_mitmRefCnt); +static Service g_smAtmosphereMitmSrv; - if (serviceIsActive(&g_smMitmSrv)) - return 0; +NX_GENERATE_SERVICE_GUARD(smAtmosphereMitm); +Result _smAtmosphereMitmInitialize(void) { Handle sm_handle; Result rc = svcConnectToNamedPort(&sm_handle, "sm:"); while (R_VALUE(rc) == KERNELRESULT(NotFound)) { @@ -181,195 +57,64 @@ Result smAtmosphereMitmInitialize(void) { } if (R_SUCCEEDED(rc)) { - serviceCreate(&g_smMitmSrv, sm_handle); + serviceCreate(&g_smAtmosphereMitmSrv, sm_handle); } if (R_SUCCEEDED(rc)) { - IpcCommand c; - ipcInitialize(&c); - ipcSendPid(&c); - - struct { - u64 magic; - u64 cmd_id; - u64 zero; - u64 reserved[2]; - } *raw; - - raw = serviceIpcPrepareHeader(&g_smMitmSrv, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->zero = 0; - - rc = serviceIpcDispatch(&g_smMitmSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - - struct { - u64 magic; - u64 result; - } *resp; - serviceIpcParse(&g_smMitmSrv, &r, sizeof(*resp)); - - resp = r.Raw; - rc = resp->result; - } + const u64 pid_placeholder = 0; + rc = serviceDispatchIn(&g_smAtmosphereMitmSrv, 0, pid_placeholder, .in_send_pid = true); } - if (R_FAILED(rc)) - smAtmosphereMitmExit(); - return rc; } -void smAtmosphereMitmExit(void) { - if (atomicDecrement64(&g_mitmRefCnt) == 0) { - serviceClose(&g_smMitmSrv); - } +void _smAtmosphereMitmCleanup(void) { + serviceClose(&g_smAtmosphereMitmSrv); +} + +Service* smAtmosphereMitmGetServiceSession(void) { + return &g_smAtmosphereMitmSrv; } Result smAtmosphereMitmInstall(Handle *handle_out, Handle *query_out, const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = &g_smMitmSrv; - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); + const u64 in = smEncodeName(name); + Handle tmp_handles[2]; + Result rc = serviceDispatchIn(&g_smAtmosphereMitmSrv, 65000, in, + .out_handle_attrs = { SfOutHandleAttr_HipcMove, SfOutHandleAttr_HipcMove }, + .out_handles = tmp_handles, + ); if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; - *query_out = r.Handles[1]; - } + *handle_out = tmp_handles[0]; + *query_out = tmp_handles[1]; } return rc; } Result smAtmosphereMitmUninstall(const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = &g_smMitmSrv; - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65001; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - rc = resp->result; - } - - return rc; + return _smAtmosphereCmdInServiceNameNoOut(smEncodeName(name), &g_smAtmosphereMitmSrv, 65001); } Result smAtmosphereMitmDeclareFuture(const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = &g_smMitmSrv; - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65006; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - rc = resp->result; - } - - return rc; + return _smAtmosphereCmdInServiceNameNoOut(smEncodeName(name), &g_smAtmosphereMitmSrv, 65006); } Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, u64 *tid_out, const char *name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = &g_smMitmSrv; - + const u64 in = smEncodeName(name); struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; + u64 pid; + u64 tid; + } out; - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65003; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); + Result rc = serviceDispatchInOut(&g_smAtmosphereMitmSrv, 65003, in, out, + .out_num_objects = 1, + .out_objects = srv_out, + ); if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u64 pid; - u64 tid; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - *pid_out = resp->pid; - *tid_out = resp->tid; - serviceCreate(srv_out, r.Handles[0]); - } + if (pid_out) *pid_out = out.pid; + if (tid_out) *tid_out = out.tid; } return rc; diff --git a/source/sm/sm_ams.h b/source/sm/sm_ams.h index 67c9b889..7ab27f64 100644 --- a/source/sm/sm_ams.h +++ b/source/sm/sm_ams.h @@ -5,7 +5,9 @@ * @copyright libnx Authors */ #pragma once -#include +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -18,6 +20,7 @@ Result smAtmosphereWaitMitm(const char *name); Result smAtmosphereMitmInitialize(void); void smAtmosphereMitmExit(void); +Service *smAtmosphereMitmGetServiceSession(); Result smAtmosphereMitmInstall(Handle *handle_out, Handle *query_out, const char *name); Result smAtmosphereMitmUninstall(const char *name); diff --git a/source/sm/sm_api.cpp b/source/sm/sm_api.cpp index a6b2be97..c12806e3 100644 --- a/source/sm/sm_api.cpp +++ b/source/sm/sm_api.cpp @@ -55,4 +55,15 @@ namespace sts::sm { }); } + namespace impl { + + void DoWithSessionImpl(void (*Invoker)(void *), void *Function) { + impl::DoWithUserSession([&]() { + Invoker(Function); + return ResultSuccess; + }); + } + + } + } diff --git a/source/sm/smm_ams.c b/source/sm/smm_ams.c index f5ea6517..e9a1995e 100644 --- a/source/sm/smm_ams.c +++ b/source/sm/smm_ams.c @@ -13,117 +13,36 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - -#include #include "smm_ams.h" Result smManagerAtmosphereEndInitialDefers(void) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = smManagerGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65000; - - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; - + return serviceDispatch(smManagerGetServiceSession(), 65000); } Result smManagerAtmosphereRegisterProcess(u64 pid, u64 tid, const void *acid_sac, size_t acid_sac_size, const void *aci_sac, size_t aci_sac_size) { - IpcCommand c; - ipcInitialize(&c); - ipcAddSendBuffer(&c, acid_sac, acid_sac_size, BufferType_Normal); - ipcAddSendBuffer(&c, aci_sac, aci_sac_size, BufferType_Normal); - Service *srv = smManagerGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; + const struct { u64 pid; u64 tid; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65002; - raw->pid = pid; - raw->tid = tid; - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } + } in = { pid, tid }; + return serviceDispatchIn(smManagerGetServiceSession(), 65002, in, + .buffer_attrs = { + SfBufferAttr_In | SfBufferAttr_HipcMapAlias, + SfBufferAttr_In | SfBufferAttr_HipcMapAlias, + }, + .buffers = { + { acid_sac, acid_sac_size }, + { aci_sac, aci_sac_size }, + }, + ); +} +static Result _smManagerAtmosphereCmdHas(bool *out, u64 service_name, u32 cmd_id) { + u8 tmp; + Result rc = serviceDispatchInOut(smManagerGetServiceSession(), cmd_id, service_name, tmp); + if (R_SUCCEEDED(rc) && out) *out = tmp & 1; return rc; } Result smManagerAtmosphereHasMitm(bool *out, const char* name) { - IpcCommand c; - ipcInitialize(&c); - Service *srv = smManagerGetServiceSession(); - - struct { - u64 magic; - u64 cmd_id; - u64 service_name; - } *raw; - - raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); - raw->magic = SFCI_MAGIC; - raw->cmd_id = 65001; - raw->service_name = smEncodeName(name); - - Result rc = serviceIpcDispatch(srv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u8 has_mitm; - } *resp; - - serviceIpcParse(srv, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out = resp->has_mitm != 0; - } - } - - return rc; + return _smManagerAtmosphereCmdHas(out, smEncodeName(name), 65001); } diff --git a/source/spl/smc/spl_smc.cpp b/source/spl/smc/spl_smc.cpp index 89e35067..8b3c9733 100644 --- a/source/spl/smc/spl_smc.cpp +++ b/source/spl/smc/spl_smc.cpp @@ -309,4 +309,59 @@ namespace sts::spl::smc { return static_cast(args.X[0]); } + /* Atmosphere functions. */ + namespace { + + enum class IramCopyDirection { + FromIram = 0, + ToIram = 1, + }; + + inline Result AtmosphereIramCopy(uintptr_t dram_address, uintptr_t iram_address, size_t size, IramCopyDirection direction) { + SecmonArgs args; + args.X[0] = static_cast(FunctionId::AtmosphereIramCopy); + args.X[1] = dram_address; + args.X[2] = iram_address; + args.X[3] = size; + args.X[4] = static_cast(direction); + svcCallSecureMonitor(&args); + + return static_cast(args.X[0]); + } + + } + + Result AtmosphereCopyToIram(uintptr_t iram_dst, const void *dram_src, size_t size) { + return AtmosphereIramCopy(reinterpret_cast(dram_src), iram_dst, size, IramCopyDirection::ToIram); + } + + Result AtmosphereCopyFromIram(void *dram_dst, uintptr_t iram_src, size_t size) { + return AtmosphereIramCopy(reinterpret_cast(dram_dst), iram_src, size, IramCopyDirection::FromIram); + } + + Result AtmosphereReadWriteRegister(uint64_t address, uint32_t mask, uint32_t value, uint32_t *out_value) { + SecmonArgs args; + args.X[0] = static_cast(FunctionId::AtmosphereReadWriteRegister); + args.X[1] = address; + args.X[2] = mask; + args.X[3] = value; + svcCallSecureMonitor(&args); + + *out_value = static_cast(args.X[1]); + return static_cast(args.X[0]); + } + + Result AtmosphereWriteAddress(void *dst, const void *src, size_t size) { + STS_ASSERT(size <= sizeof(u64)); + + SecmonArgs args; + args.X[0] = static_cast(FunctionId::AtmosphereWriteAddress); + args.X[1] = reinterpret_cast(dst); + __builtin_memcpy(&args.X[1], src, size); + args.X[3] = size; + svcCallSecureMonitor(&args); + + return static_cast(args.X[0]); + } + } diff --git a/source/updater/updater_api.cpp b/source/updater/updater_api.cpp index f4f93852..ab619c10 100644 --- a/source/updater/updater_api.cpp +++ b/source/updater/updater_api.cpp @@ -346,7 +346,7 @@ namespace sts::updater { } /* Only preserve autorcm if on a unit with unpatched rcm bug. */ - if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) { + if (HasAutoRcmPreserve(boot_image_update_type) && !ams::IsRcmBugPatched()) { R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalSub)); R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)); R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalMain)); @@ -407,7 +407,7 @@ namespace sts::updater { R_TRY(boot0_accessor.UpdateEks(bct, work)); } /* Only preserve autorcm if on a unit with unpatched rcm bug. */ - if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) { + if (HasAutoRcmPreserve(boot_image_update_type) && !ams::IsRcmBugPatched()) { R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeSub)); R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)); R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeMain)); @@ -522,10 +522,8 @@ namespace sts::updater { } /* Get a session to ncm. */ - DoWithSmSession([&]() { - R_ASSERT(ncmInitialize()); - }); - ON_SCOPE_EXIT { ncmExit(); }; + sm::ScopedServiceHolder ncm_holder; + R_ASSERT(ncm_holder.GetResult()); /* Verify normal, verify safe as needed. */ if (verification_state.needs_verify_normal) {