From e786bc7e9a3c0b9b0424b12f243d5f8debd2c85d Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 4 Nov 2018 12:45:29 -0800 Subject: [PATCH] fs.mitm: Only create storage interface when needed. --- .../fs_mitm/source/fsmitm_service.cpp | 42 ++++++++++++------- stratosphere/fs_mitm/source/fsmitm_utils.cpp | 16 ++++--- stratosphere/fs_mitm/source/fsmitm_utils.hpp | 5 +-- .../stratosphere/mitm/mitm_session.hpp | 4 +- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/stratosphere/fs_mitm/source/fsmitm_service.cpp b/stratosphere/fs_mitm/source/fsmitm_service.cpp index a9f1e30a5..7cb91f1ca 100644 --- a/stratosphere/fs_mitm/source/fsmitm_service.cpp +++ b/stratosphere/fs_mitm/source/fsmitm_service.cpp @@ -79,15 +79,21 @@ Result FsMitmService::OpenDataStorageByCurrentProcess(Outtitle_id, "romfs.bin", FS_OPEN_READ, &data_file))) { - storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), this->title_id)); + if (Utils::HasSdRomfsContent(this->title_id)) { + /* TODO: Is there a sensible path that ends in ".romfs" we can use?" */ + if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(this->title_id, "romfs.bin", FS_OPEN_READ, &data_file))) { + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), this->title_id)); + } else { + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, this->title_id)); + } + this->romfs_storage = storage; + if (out_storage.IsDomain()) { + out_domain_id = data_storage.s.object_id; + } } else { - storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, this->title_id)); - } - this->romfs_storage = storage; - if (out_storage.IsDomain()) { - out_domain_id = data_storage.s.object_id; + /* If we don't have anything to modify, there's no sense in maintaining a copy of the metadata tables. */ + fsStorageClose(&data_storage); + rc = RESULT_FORWARD_TO_SESSION; } } } @@ -117,14 +123,20 @@ Result FsMitmService::OpenDataStorageByDataId(Outforward_service.get(), storage_id, data_id, &data_storage); if (R_SUCCEEDED(rc)) { - /* TODO: Is there a sensible path that ends in ".romfs" we can use?" */ - if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(data_id, "romfs.bin", FS_OPEN_READ, &data_file))) { - storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), data_id)); + if (Utils::HasSdRomfsContent(data_id)) { + /* TODO: Is there a sensible path that ends in ".romfs" we can use?" */ + if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(data_id, "romfs.bin", FS_OPEN_READ, &data_file))) { + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), std::make_shared(data_file), data_id)); + } else { + storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, data_id)); + } + if (out_storage.IsDomain()) { + out_domain_id = data_storage.s.object_id; + } } else { - storage = std::make_shared(new LayeredRomFS(std::make_shared(data_storage), nullptr, data_id)); - } - if (out_storage.IsDomain()) { - out_domain_id = data_storage.s.object_id; + /* If we don't have anything to modify, there's no sense in maintaining a copy of the metadata tables. */ + fsStorageClose(&data_storage); + rc = RESULT_FORWARD_TO_SESSION; } } diff --git a/stratosphere/fs_mitm/source/fsmitm_utils.cpp b/stratosphere/fs_mitm/source/fsmitm_utils.cpp index aa22bb5c3..2210f4cfe 100644 --- a/stratosphere/fs_mitm/source/fsmitm_utils.cpp +++ b/stratosphere/fs_mitm/source/fsmitm_utils.cpp @@ -201,20 +201,18 @@ Result Utils::OpenRomFSDir(FsFileSystem *fs, u64 title_id, const char *path, FsD return fsFsOpenDirectory(fs, safe_path, FS_DIROPEN_DIRECTORY | FS_DIROPEN_FILE, out); } -Result Utils::HasSdRomfsContent(u64 title_id, bool *out) { - Result rc; +bool Utils::HasSdRomfsContent(u64 title_id) { FsDir dir; - if (R_FAILED((rc = Utils::OpenRomFSSdDir(title_id, "", &dir)))) { - return rc; + if (R_FAILED(Utils::OpenRomFSSdDir(title_id, "", &dir))) { + return false; } + ON_SCOPE_EXIT { + fsDirClose(&dir); + }; FsDirectoryEntry dir_entry; u64 read_entries; - if (R_SUCCEEDED((rc = fsDirRead(&dir, 0, &read_entries, 1, &dir_entry)))) { - *out = (read_entries == 1); - } - fsDirClose(&dir); - return rc; + return R_SUCCEEDED(fsDirRead(&dir, 0, &read_entries, 1, &dir_entry)) && read_entries == 1; } bool Utils::HasSdMitMFlag(u64 tid) { diff --git a/stratosphere/fs_mitm/source/fsmitm_utils.hpp b/stratosphere/fs_mitm/source/fsmitm_utils.hpp index 42d547481..5c27b6b14 100644 --- a/stratosphere/fs_mitm/source/fsmitm_utils.hpp +++ b/stratosphere/fs_mitm/source/fsmitm_utils.hpp @@ -26,13 +26,12 @@ class Utils { static Result OpenRomFSSdFile(u64 title_id, const char *fn, int flags, FsFile *out); static Result OpenSdDir(const char *path, FsDir *out); static Result OpenSdDirForAtmosphere(u64 title_id, const char *path, FsDir *out); - static Result OpenRomFSSdDir(u64 title_id, const char *path, FsDir *out); - + static Result OpenRomFSSdDir(u64 title_id, const char *path, FsDir *out); static Result OpenRomFSFile(FsFileSystem *fs, u64 title_id, const char *fn, int flags, FsFile *out); static Result OpenRomFSDir(FsFileSystem *fs, u64 title_id, const char *path, FsDir *out); - static Result HasSdRomfsContent(u64 title_id, bool *out); + static bool HasSdRomfsContent(u64 title_id); /* SD card Initialization + MitM detection. */ static void InitializeSdThreadFunc(void *args); diff --git a/stratosphere/libstratosphere/include/stratosphere/mitm/mitm_session.hpp b/stratosphere/libstratosphere/include/stratosphere/mitm/mitm_session.hpp index f4df504f1..3a4032161 100644 --- a/stratosphere/libstratosphere/include/stratosphere/mitm/mitm_session.hpp +++ b/stratosphere/libstratosphere/include/stratosphere/mitm/mitm_session.hpp @@ -21,6 +21,8 @@ #include "mitm_query_service.hpp" +#define RESULT_FORWARD_TO_SESSION 0xCAFEFC + class MitmSession final : public ServiceSession { private: /* This will be for the actual session. */ @@ -149,7 +151,7 @@ class MitmSession final : public ServiceSession { } } - if (!found_entry) { + if (!found_entry || rc == RESULT_FORWARD_TO_SESSION) { memcpy(armGetTls(), this->backup_tls, sizeof(this->backup_tls)); rc = ForwardRequest(ctx); }