From 2b6719ec255088a457fdda904efc8de98965e455 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 21 Feb 2022 14:57:03 -0800 Subject: [PATCH] ams_mitm: update to use new NcaFileSystemDriver intf --- .../fssystem/fssystem_asynchronous_access.hpp | 2 +- .../fssystem/fssystem_compressed_storage.hpp | 9 ++- .../fssystem_file_system_proxy_api.hpp | 2 + .../fssystem_file_system_proxy_api.cpp | 67 +++++++++++++++++++ .../fssystem_nca_file_system_driver.cpp | 2 +- stratosphere/ams_mitm/source/amsmitm_main.cpp | 3 +- .../source/sysupdater/sysupdater_fs_utils.cpp | 7 +- 7 files changed, 82 insertions(+), 10 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp index 7eba3db59..466707968 100644 --- a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp +++ b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp @@ -28,7 +28,7 @@ namespace ams::fssystem { Result QueryNextOffset(s64 *out, s64 start_offset, s64 end_offset, s64 access_size, s64 alignment_size); public: virtual Result QueryAppropriateOffset(s64 *out, s64 offset, s64 access_size, s64 alignment_size) = 0; - virtual Result QueryInvocationCount(s64 *out, s64 start_offset, s64 end_offset, s64 access_size, s64 alignment_size); + virtual Result QueryInvocationCount(s64 *out, s64 start_offset, s64 end_offset, s64 access_size, s64 alignment_size) { AMS_UNUSED(out, start_offset, end_offset, access_size, alignment_size); AMS_ABORT("TODO"); } }; class DefaultAsynchronousAccessSplitter final : public IAsynchronousAccessSplitter { diff --git a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp index 327087959..a9e5461b7 100644 --- a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp +++ b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp @@ -56,7 +56,10 @@ namespace ams::fssystem { CompressedStorage() { /* ... */ } virtual ~CompressedStorage() { this->Finalize(); } - Result Initialize(MemoryResource *bktr_allocator, IBufferManager *cache_allocator, fs::SubStorage data_storage, fs::SubStorage node_storage, fs::SubStorage entry_storage, s32 bktr_entry_count, size_t block_size_max, size_t continuous_reading_size_max, GetDecompressorFunction get_decompressor, size_t cache_size_0, size_t cache_size_1, s32 max_cache_entries); + Result Initialize(MemoryResource *bktr_allocator, IBufferManager *cache_allocator, fs::SubStorage data_storage, fs::SubStorage node_storage, fs::SubStorage entry_storage, s32 bktr_entry_count, size_t block_size_max, size_t continuous_reading_size_max, GetDecompressorFunction get_decompressor, size_t cache_size_0, size_t cache_size_1, s32 max_cache_entries) { + AMS_UNUSED(bktr_allocator, cache_allocator, data_storage, node_storage, entry_storage, bktr_entry_count, block_size_max, continuous_reading_size_max, get_decompressor, cache_size_0, cache_size_1, max_cache_entries); + AMS_ABORT("TODO"); + } void Finalize() { AMS_ABORT("TODO"); @@ -70,8 +73,8 @@ namespace ams::fssystem { /* return m_core.QueryAppropriateOffsetForAsynchronousAccess(out, offset, access_size, alignment_size); */ } public: - virtual Result Read(s64 offset, void *buffer, size_t size) override; - virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override; + virtual Result Read(s64 offset, void *buffer, size_t size) override { AMS_UNUSED(offset, buffer, size); AMS_ABORT("TODO"); } + virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override { AMS_UNUSED(dst, dst_size, op_id, offset, size, src, src_size); AMS_ABORT("TODO"); } virtual Result GetSize(s64 *out) override { AMS_ABORT("TODO"); diff --git a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp index ae19b4e2f..63e306451 100644 --- a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp @@ -28,6 +28,8 @@ namespace ams::fssystem { /* This should be re-examined when FS is reimplemented. */ void InitializeForFileSystemProxy(); + void InitializeForAtmosphereMitm(); + const ::ams::fssrv::fscreator::FileSystemCreatorInterfaces *GetFileSystemCreatorInterfaces(); } diff --git a/libraries/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp b/libraries/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp index cca546939..5697c1416 100644 --- a/libraries/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp +++ b/libraries/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp @@ -185,6 +185,73 @@ namespace ams::fssystem { /* TODO FS-REIMPL: spl::Finalize(); */ } + void InitializeForAtmosphereMitm() { + /* Initialize spl library. */ + spl::InitializeForFs(); + + /* TODO FS-REIMPL: spl::SetIsAvailableAccessKeyHandler(fssrv::IsAvailableAccessKey) */ + + /* Determine whether we're prod or dev. */ + bool is_prod = !spl::IsDevelopment(); + bool is_development_function_enabled = spl::IsDevelopmentFunctionEnabled(); + + /* Set debug flags. */ + fssrv::SetDebugFlagEnabled(is_development_function_enabled); + + /* Setup our crypto configuration. */ + SetUpKekAccessKeys(is_prod); + + /* Setup our heap. */ + InitializeExpHeap(); + + /* Initialize buffer allocator. */ + util::ConstructAt(g_buffer_allocator, g_buffer_pool, BufferPoolSize); + util::ConstructAt(g_allocator, GetPointer(g_buffer_allocator)); + + /* Set allocators. */ + /* TODO FS-REIMPL: sf::SetGlobalDefaultMemoryResource() */ + fs::SetAllocator(AllocateForFileSystemProxy, DeallocateForFileSystemProxy); + fssystem::InitializeAllocator(AllocateForFileSystemProxy, DeallocateForFileSystemProxy); + fssystem::InitializeAllocatorForSystem(AllocateForFileSystemProxy, DeallocateForFileSystemProxy); + + /* Initialize the buffer manager. */ + /* TODO FS-REIMPL: os::AllocateMemoryBlock(...); */ + util::ConstructAt(g_buffer_manager); + GetReference(g_buffer_manager).Initialize(MaxCacheCount, reinterpret_cast(g_buffer_manager_heap), BufferManagerHeapSize, BlockSize); + + /* TODO FS-REIMPL: os::AllocateMemoryBlock(...); */ + /* TODO FS-REIMPL: fssrv::storage::CreateDeviceAddressSpace(...); */ + fssystem::InitializeBufferPool(reinterpret_cast(g_device_buffer), DeviceBufferSize); + + /* TODO FS-REIMPL: Create Pooled Threads/Stack Usage Reporter, fssystem::RegisterThreadPool. */ + + /* TODO FS-REIMPL: fssrv::GetFileSystemProxyServices(), some service creation. */ + + /* Initialize fs creators. */ + /* TODO FS-REIMPL: Revise for accuracy. */ + util::ConstructAt(g_rom_fs_creator, GetPointer(g_allocator)); + util::ConstructAt(g_partition_fs_creator); + util::ConstructAt(g_storage_on_nca_creator, GetPointer(g_allocator), *GetNcaCryptoConfiguration(is_prod), *GetNcaCompressionConfiguration(), GetPointer(g_buffer_manager), fs::impl::GetNcaHashGeneratorFactorySelector()); + + /* TODO FS-REIMPL: Initialize other creators. */ + g_fs_creator_interfaces = { + .rom_fs_creator = GetPointer(g_rom_fs_creator), + .partition_fs_creator = GetPointer(g_partition_fs_creator), + .storage_on_nca_creator = GetPointer(g_storage_on_nca_creator), + }; + + /* Initialize fssrv. TODO FS-REIMPL: More arguments, more actions taken. */ + fssrv::InitializeForFileSystemProxy(std::addressof(g_fs_creator_interfaces), GetPointer(g_buffer_manager), is_development_function_enabled); + + /* Disable auto-abort in fs library code. */ + fs::SetEnabledAutoAbort(false); + + /* Quick sanity check, before we leave. */ + #if defined(ATMOSPHERE_OS_HORIZON) + AMS_ABORT_UNLESS(os::GetCurrentProgramId() == ncm::AtmosphereProgramId::Mitm); + #endif + } + const ::ams::fssrv::fscreator::FileSystemCreatorInterfaces *GetFileSystemCreatorInterfaces() { return std::addressof(g_fs_creator_interfaces); } diff --git a/libraries/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp b/libraries/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp index 14d975243..b5060f138 100644 --- a/libraries/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp +++ b/libraries/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp @@ -1022,7 +1022,7 @@ namespace ams::fssystem { R_TRY(buffer_hold_storage->GetSize(std::addressof(base_size))); /* Check that we're within range. */ - R_UNLESS(data_region.offset + data_region.size <= base_size, fs::ResultNcaBaseStorageOutOfRangeC()); + R_UNLESS(hash_region.offset + hash_region.size <= base_size, fs::ResultNcaBaseStorageOutOfRangeC()); R_UNLESS(data_region.offset + data_region.size <= base_size, fs::ResultNcaBaseStorageOutOfRangeC()); /* Create the master hash storage. */ diff --git a/stratosphere/ams_mitm/source/amsmitm_main.cpp b/stratosphere/ams_mitm/source/amsmitm_main.cpp index 2bcfde694..d52b6c595 100644 --- a/stratosphere/ams_mitm/source/amsmitm_main.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_main.cpp @@ -43,7 +43,6 @@ namespace ams { R_ABORT_UNLESS(pmdmntInitialize()); R_ABORT_UNLESS(pminfoInitialize()); ncm::Initialize(); - spl::InitializeForFs(); /* Verify that we can sanely execute. */ ams::CheckApiVersion(); @@ -79,7 +78,7 @@ namespace ams { } /* Initialize fssystem library. */ - fssystem::InitializeForFileSystemProxy(); + fssystem::InitializeForAtmosphereMitm(); /* Configure ncm to use fssystem library to mount content from the sd card. */ ncm::SetMountContentMetaFunction(mitm::sysupdater::MountSdCardContentMeta); diff --git a/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp b/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp index 18ae3bd89..1d9f5f07b 100644 --- a/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp +++ b/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp @@ -151,7 +151,7 @@ namespace ams::mitm::sysupdater { return ResultSuccess(); } - Result OpenMetaStorage(std::shared_ptr *out, std::shared_ptr nca_reader, fssystem::NcaFsHeader::FsType *out_fs_type) { + Result OpenMetaStorage(std::shared_ptr *out, std::shared_ptr *out_splitter, std::shared_ptr nca_reader, fssystem::NcaFsHeader::FsType *out_fs_type) { /* Ensure the nca is a meta nca. */ R_UNLESS(nca_reader->GetContentType() == fssystem::NcaHeader::ContentType::Meta, fs::ResultPreconditionViolation()); @@ -163,7 +163,7 @@ namespace ams::mitm::sysupdater { /* Open fs header reader. */ fssystem::NcaFsHeaderReader fs_header_reader; - R_TRY(fssystem::GetFileSystemCreatorInterfaces()->storage_on_nca_creator->Create(out, std::addressof(fs_header_reader), std::move(nca_reader), MetaPartitionIndex, false)); + R_TRY(fssystem::GetFileSystemCreatorInterfaces()->storage_on_nca_creator->Create(out, out_splitter, std::addressof(fs_header_reader), std::move(nca_reader), MetaPartitionIndex)); /* Set the output fs type. */ *out_fs_type = fs_header_reader.GetFsType(); @@ -195,8 +195,9 @@ namespace ams::mitm::sysupdater { /* Open meta storage. */ std::shared_ptr storage; + std::shared_ptr splitter; fssystem::NcaFsHeader::FsType fs_type; - R_TRY(OpenMetaStorage(std::addressof(storage), std::move(nca_reader), std::addressof(fs_type))); + R_TRY(OpenMetaStorage(std::addressof(storage), std::addressof(splitter), std::move(nca_reader), std::addressof(fs_type))); /* Open the appropriate interface. */ const auto * const creator_intfs = fssystem::GetFileSystemCreatorInterfaces();