diff --git a/libexosphere/include/exosphere/secmon/secmon_configuration_context.arch.arm64.hpp b/libexosphere/include/exosphere/secmon/secmon_configuration_context.arch.arm64.hpp index 2008f7e4..9eacd856 100644 --- a/libexosphere/include/exosphere/secmon/secmon_configuration_context.arch.arm64.hpp +++ b/libexosphere/include/exosphere/secmon/secmon_configuration_context.arch.arm64.hpp @@ -70,6 +70,10 @@ namespace ams::secmon { GetConfigurationContext().secmon_cfg.key_generation = generation; } + ALWAYS_INLINE void SetTargetFirmware(ams::TargetFirmware target_firmware) { + GetConfigurationContext().secmon_cfg.target_firmware = target_firmware; + } + ALWAYS_INLINE pkg1::BootConfig *GetBootConfigStorage() { return std::addressof(GetConfigurationContext().boot_config); } diff --git a/libstratosphere/include/stratosphere/ncm/ncm_i_content_manager.hpp b/libstratosphere/include/stratosphere/ncm/ncm_i_content_manager.hpp index cf51c691..101f42c5 100644 --- a/libstratosphere/include/stratosphere/ncm/ncm_i_content_manager.hpp +++ b/libstratosphere/include/stratosphere/ncm/ncm_i_content_manager.hpp @@ -33,6 +33,6 @@ AMS_SF_METHOD_INFO(C, H, 11, Result, ActivateContentMetaDatabase, (ncm::StorageId storage_id), (storage_id), hos::Version_2_0_0) \ AMS_SF_METHOD_INFO(C, H, 12, Result, InactivateContentMetaDatabase, (ncm::StorageId storage_id), (storage_id), hos::Version_2_0_0) \ AMS_SF_METHOD_INFO(C, H, 13, Result, InvalidateRightsIdCache, (), (), hos::Version_9_0_0) \ - AMS_SF_METHOD_INFO(C, H, 14, Result, GetMemoryReport, (sf::Out out), (out), hos::Version_10_0_0) + AMS_SF_METHOD_INFO(C, H, 14, Result, GetMemoryReport, (sf::Out out), (out), hos::Version_10_0_0) AMS_SF_DEFINE_INTERFACE(ams::ncm, IContentManager, AMS_NCM_I_CONTENT_MANAGER_INTERFACE_INFO); diff --git a/libstratosphere/include/stratosphere/spl/spl_api.hpp b/libstratosphere/include/stratosphere/spl/spl_api.hpp index 345d771b..3db4ed3d 100644 --- a/libstratosphere/include/stratosphere/spl/spl_api.hpp +++ b/libstratosphere/include/stratosphere/spl/spl_api.hpp @@ -39,6 +39,7 @@ namespace ams::spl { Result DecryptAesKey(void *dst, size_t dst_size, const void *src, size_t src_size, s32 generation, u32 option); Result GetConfig(u64 *out, ConfigItem item); + Result SetConfig(ConfigItem item, u64 v); bool IsDevelopment(); MemoryArrangement GetMemoryArrangement(); diff --git a/libstratosphere/include/stratosphere/spl/spl_types.hpp b/libstratosphere/include/stratosphere/spl/spl_types.hpp index 5b1b459b..3accdc62 100644 --- a/libstratosphere/include/stratosphere/spl/spl_types.hpp +++ b/libstratosphere/include/stratosphere/spl/spl_types.hpp @@ -231,18 +231,19 @@ namespace ams::spl { Package2Hash = 17, /* Extension config items for exosphere. */ - ExosphereApiVersion = 65000, - ExosphereNeedsReboot = 65001, - ExosphereNeedsShutdown = 65002, - ExosphereGitCommitHash = 65003, - ExosphereHasRcmBugPatch = 65004, - ExosphereBlankProdInfo = 65005, - ExosphereAllowCalWrites = 65006, - ExosphereEmummcType = 65007, - ExospherePayloadAddress = 65008, - ExosphereLogConfiguration = 65009, - ExosphereForceEnableUsb30 = 65010, - ExosphereSupportedHosVersion = 65011, + ExosphereApiVersion = 65000, + ExosphereNeedsReboot = 65001, + ExosphereNeedsShutdown = 65002, + ExosphereGitCommitHash = 65003, + ExosphereHasRcmBugPatch = 65004, + ExosphereBlankProdInfo = 65005, + ExosphereAllowCalWrites = 65006, + ExosphereEmummcType = 65007, + ExospherePayloadAddress = 65008, + ExosphereLogConfiguration = 65009, + ExosphereForceEnableUsb30 = 65010, + ExosphereSupportedHosVersion = 65011, + ExosphereApproximateApiVersion = 65012, /* NOTE: Internal use only. */ }; } diff --git a/libstratosphere/source/hos/hos_stratosphere_api.cpp b/libstratosphere/source/hos/hos_stratosphere_api.cpp index 2cd3ce05..cf714570 100644 --- a/libstratosphere/source/hos/hos_stratosphere_api.cpp +++ b/libstratosphere/source/hos/hos_stratosphere_api.cpp @@ -14,7 +14,6 @@ * along with this program. If not, see . */ #include -#include "hos_version_api_private.hpp" #include "../os/impl/os_rng_manager.hpp" namespace ams::os { @@ -34,23 +33,29 @@ extern "C" { namespace ams::hos { + namespace { + + bool CanAllowTemporaryApproximateVersion() { + /* Check if we're a program that can use a temporary approximate version. */ + const auto program_id = os::GetCurrentProgramId(); + + return program_id == ncm::SystemProgramId::Pm || /* Needed so that boot has permissions to use fsp-srv. */ + program_id == ncm::SystemProgramId::Sm || /* Needed so that boot can acquire fsp-srv. */ + program_id == ncm::SystemProgramId::Boot || /* Needed so that boot can set the true target firmware. */ + program_id == ncm::SystemProgramId::Spl || /* Needed so that FS can complete initialization. */ + program_id == ncm::SystemProgramId::Ncm; /* Needed so that FS can determine where SystemVersion is located. */ + } + + } + + void InitializeVersionInternal(bool allow_approximate); + void InitializeForStratosphere() { /* Initialize the global os resource managers. This *must* be done before anything else in stratosphere. */ os::InitializeForStratosphereInternal(); /* Initialize hos::Version API. */ - hos::SetVersionForLibnxInternal(); - - /* Check that we're running under mesosphere. */ - AMS_ABORT_UNLESS(svc::IsKernelMesosphere()); - } - - void InitializeForStratosphereDebug(hos::Version debug_version) { - /* Initialize the global os resource managers. This *must* be done before anything else in stratosphere. */ - os::InitializeForStratosphereInternal(); - - /* Initialize hos::Version API. */ - hos::SetVersionForLibnxInternalDebug(debug_version); + hos::InitializeVersionInternal(CanAllowTemporaryApproximateVersion()); /* Check that we're running under mesosphere. */ AMS_ABORT_UNLESS(svc::IsKernelMesosphere()); diff --git a/libstratosphere/source/hos/hos_version_api.cpp b/libstratosphere/source/hos/hos_version_api.cpp index 1ae7f8d7..e2487ddb 100644 --- a/libstratosphere/source/hos/hos_version_api.cpp +++ b/libstratosphere/source/hos/hos_version_api.cpp @@ -14,69 +14,139 @@ * along with this program. If not, see . */ #include -#include "hos_version_api_private.hpp" namespace ams::hos { namespace { - hos::Version g_hos_version; - bool g_has_cached; - os::SdkMutex g_mutex; + constinit os::SdkMutex g_hos_init_lock; + constinit hos::Version g_hos_version; + constinit bool g_set_hos_version; - void CacheValues() { - if (__atomic_load_n(&g_has_cached, __ATOMIC_SEQ_CST)) { - return; + Result GetExosphereApiInfo(exosphere::ApiInfo *out) { + u64 exosphere_cfg; + R_TRY_CATCH(spl::impl::GetConfig(std::addressof(exosphere_cfg), spl::ConfigItem::ExosphereApiVersion)) { + R_CATCH_RETHROW(spl::ResultSecureMonitorNotInitialized) + } R_END_TRY_CATCH_WITH_ABORT_UNLESS; + + *out = { exosphere_cfg }; + return ResultSuccess(); + } + + Result GetApproximateExosphereApiInfo(exosphere::ApiInfo *out) { + u64 exosphere_cfg; + + R_TRY_CATCH(spl::impl::GetConfig(std::addressof(exosphere_cfg), spl::ConfigItem::ExosphereApproximateApiVersion)) { + R_CATCH_RETHROW(spl::ResultSecureMonitorBusy) + } R_END_TRY_CATCH_WITH_ABORT_UNLESS; + + *out = { exosphere_cfg }; + return ResultSuccess(); + } + + exosphere::ApiInfo GetExosphereApiInfo(bool allow_approximate) { + exosphere::ApiInfo info; + while (true) { + if (R_SUCCEEDED(GetExosphereApiInfo(std::addressof(info)))) { + return info; + } + + if (allow_approximate && R_SUCCEEDED(GetApproximateExosphereApiInfo(std::addressof(info)))) { + return info; + } + + svc::SleepThread(TimeSpan::FromMilliSeconds(25).GetNanoSeconds()); } + } - std::scoped_lock lk(g_mutex); + settings::FirmwareVersion GetSettingsFirmwareVersion() { + /* Mount the system version title. */ + R_ABORT_UNLESS(ams::fs::MountSystemData("sysver", ncm::SystemDataId::SystemVersion)); + ON_SCOPE_EXIT { ams::fs::Unmount("sysver"); }; - if (g_has_cached) { - return; - } + /* Read the firmware version file. */ + ams::fs::FileHandle file; + R_ABORT_UNLESS(ams::fs::OpenFile(std::addressof(file), "sysver:/file", fs::OpenMode_Read)); + ON_SCOPE_EXIT { ams::fs::CloseFile(file); }; - /* Hos version is a direct copy of target firmware, just renamed. */ - g_hos_version = static_cast(exosphere::GetApiInfo().GetTargetFirmware()); + /* Must be possible to read firmware version from file. */ + settings::FirmwareVersion firmware_version; + R_ABORT_UNLESS(ams::fs::ReadFile(file, 0, std::addressof(firmware_version), sizeof(firmware_version))); - /* Ensure that this is a hos version we can sanely *try* to run. */ - /* To be friendly, we will only require that we recognize the major and minor versions. */ - /* We can consider only recognizing major in the future, but micro seems safe to ignore as */ - /* there are no breaking IPC changes in minor updates. */ - { - constexpr u32 MaxMajor = (static_cast(hos::Version_Max) >> 24) & 0xFF; - constexpr u32 MaxMinor = (static_cast(hos::Version_Max) >> 16) & 0xFF; - - const u32 major = (static_cast(g_hos_version) >> 24) & 0xFF; - const u32 minor = (static_cast(g_hos_version) >> 16) & 0xFF; - - const bool is_safely_tryable_version = (g_hos_version <= hos::Version_Max) || (major == MaxMajor && minor <= MaxMinor); - AMS_ABORT_UNLESS(is_safely_tryable_version); - } - - __atomic_store_n(&g_has_cached, true, __ATOMIC_SEQ_CST); + return firmware_version; } } + void InitializeVersionInternal(bool allow_approximate) { + /* Get the current (and previous approximation of) target firmware. */ + hos::Version prev, current; + bool has_prev = false; + { + /* Acquire exclusive access to set hos version. */ + std::scoped_lock lk(g_hos_init_lock); + + /* Save the previous value of g_hos_version. */ + prev = g_hos_version; + has_prev = g_set_hos_version; + + /* Set hos version = exosphere api version target firmware. */ + g_hos_version = static_cast(GetExosphereApiInfo(allow_approximate).GetTargetFirmware()); + + /* Save the current value of g_hos_version. */ + current = g_hos_version; + + /* Note that we've set a previous hos version. */ + g_set_hos_version = true; + } + + /* Ensure that this is a hos version we can sanely *try* to run. */ + /* To be friendly, we will only require that we recognize the major and minor versions. */ + /* We can consider only recognizing major in the future, but micro seems safe to ignore as */ + /* there are no breaking IPC changes in minor updates. */ + { + constexpr u32 MaxMajor = (static_cast(hos::Version_Max) >> 24) & 0xFF; + constexpr u32 MaxMinor = (static_cast(hos::Version_Max) >> 16) & 0xFF; + + const u32 major = (static_cast(current) >> 24) & 0xFF; + const u32 minor = (static_cast(current) >> 16) & 0xFF; + + const bool is_safely_tryable_version = (current <= hos::Version_Max) || (major == MaxMajor && minor <= MaxMinor); + AMS_ABORT_UNLESS(is_safely_tryable_version); + } + + /* Ensure that this is a hos version compatible with previous approximations. */ + if (has_prev) { + AMS_ABORT_UNLESS(current >= prev); + + const u32 current_major = (static_cast(current) >> 24) & 0xFF; + const u32 prev_major = (static_cast(prev) >> 24) & 0xFF; + + AMS_ABORT_UNLESS(current_major == prev_major); + } + + /* Set the version for libnx. */ + { + const u32 major = (static_cast(current) >> 24) & 0xFF; + const u32 minor = (static_cast(current) >> 16) & 0xFF; + const u32 micro = (static_cast(current) >> 8) & 0xFF; + hosversionSet((BIT(31)) | (MAKEHOSVERSION(major, minor, micro))); + } + } + + void SetNonApproximateVersionInternal() { + /* Get the settings . */ + const auto firmware_version = GetSettingsFirmwareVersion(); + + /* Set the exosphere api version. */ + R_ABORT_UNLESS(spl::SetConfig(spl::ConfigItem::ExosphereApiVersion, (static_cast(firmware_version.major) << 24) | (static_cast(firmware_version.minor) << 16) | (static_cast(firmware_version.micro) << 8))); + + /* Update our own version value. */ + InitializeVersionInternal(false); + } + ::ams::hos::Version GetVersion() { - CacheValues(); return g_hos_version; } - - void SetVersionForLibnxInternalDebug(hos::Version debug_version) { - std::scoped_lock lk(g_mutex); - g_hos_version = debug_version; - __atomic_store_n(&g_has_cached, true, __ATOMIC_SEQ_CST); - SetVersionForLibnxInternal(); - } - - void SetVersionForLibnxInternal() { - const u32 hos_version_val = static_cast(hos::GetVersion()); - const u32 major = (hos_version_val >> 24) & 0xFF; - const u32 minor = (hos_version_val >> 16) & 0xFF; - const u32 micro = (hos_version_val >> 8) & 0xFF; - hosversionSet((BIT(31)) | (MAKEHOSVERSION(major, minor, micro))); - } - } diff --git a/libstratosphere/source/hos/hos_version_api_private.hpp b/libstratosphere/source/hos/hos_version_api_private.hpp deleted file mode 100644 index a3c18da2..00000000 --- a/libstratosphere/source/hos/hos_version_api_private.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 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 - -namespace ams::hos { - - void SetVersionForLibnxInternal(); - void SetVersionForLibnxInternalDebug(hos::Version debug_version); - -} diff --git a/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_battery_driver.cpp b/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_battery_driver.cpp index 5171c976..7542971f 100644 --- a/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_battery_driver.cpp +++ b/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_battery_driver.cpp @@ -35,6 +35,7 @@ namespace ams::powctl::impl::board::nintendo::nx { if (AMS_LIKELY(!g_constructed_max17050_driver)) { util::ConstructAt(g_max17050_driver); + g_constructed_max17050_driver = true; } } diff --git a/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_charger_driver.cpp b/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_charger_driver.cpp index 25e25a08..f5b8227b 100644 --- a/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_charger_driver.cpp +++ b/libstratosphere/source/powctl/impl/board/nintendo/nx/powctl_charger_driver.cpp @@ -35,6 +35,7 @@ namespace ams::powctl::impl::board::nintendo::nx { if (AMS_LIKELY(!g_constructed_bq24193_driver)) { util::ConstructAt(g_bq24193_driver); + g_constructed_bq24193_driver = true; } } diff --git a/libstratosphere/source/spl/impl/spl_api_impl.cpp b/libstratosphere/source/spl/impl/spl_api_impl.cpp index a2a1681c..0504e2bc 100644 --- a/libstratosphere/source/spl/impl/spl_api_impl.cpp +++ b/libstratosphere/source/spl/impl/spl_api_impl.cpp @@ -18,6 +18,12 @@ #include "spl_device_address_mapper.hpp" #include "spl_key_slot_cache.hpp" +namespace ams::hos { + + void InitializeVersionInternal(bool allow_approximate); + +} + namespace ams::spl::impl { namespace { @@ -536,7 +542,13 @@ namespace ams::spl::impl { } Result SetConfig(ConfigItem key, u64 value) { - return smc::ConvertResult(smc::SetConfig(key, value)); + R_TRY(smc::ConvertResult(smc::SetConfig(key, value))); + + /* Work around for temporary version. */ + if (key == ConfigItem::ExosphereApiVersion) { + hos::InitializeVersionInternal(false); + } + return ResultSuccess(); } Result GenerateRandomBytes(void *out, size_t size) { diff --git a/libstratosphere/source/spl/spl_api.cpp b/libstratosphere/source/spl/spl_api.cpp index 9e5ec476..e296579c 100644 --- a/libstratosphere/source/spl/spl_api.cpp +++ b/libstratosphere/source/spl/spl_api.cpp @@ -216,6 +216,10 @@ namespace ams::spl { return splGetConfig(static_cast<::SplConfigItem>(item), out); } + Result SetConfig(ConfigItem item, u64 v) { + return splSetConfig(static_cast<::SplConfigItem>(item), v); + } + bool IsDevelopment() { bool is_dev; R_ABORT_UNLESS(splIsDevelopment(std::addressof(is_dev)));