From 936b36fc6e3e8075fbba7eabc739429aea57e1fe 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 +- 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp b/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp index 7eba3db5..46670796 100644 --- a/libstratosphere/include/stratosphere/fssystem/fssystem_asynchronous_access.hpp +++ b/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/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp b/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp index 32708795..a9e5461b 100644 --- a/libstratosphere/include/stratosphere/fssystem/fssystem_compressed_storage.hpp +++ b/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/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp b/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp index ae19b4e2..63e30645 100644 --- a/libstratosphere/include/stratosphere/fssystem/fssystem_file_system_proxy_api.hpp +++ b/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/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp b/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp index cca54693..5697c141 100644 --- a/libstratosphere/source/fssystem/fssystem_file_system_proxy_api.cpp +++ b/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/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp b/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp index 14d97524..b5060f13 100644 --- a/libstratosphere/source/fssystem/fssystem_nca_file_system_driver.cpp +++ b/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. */