diff --git a/common/defaults/system_settings.ini b/common/defaults/system_settings.ini index d1b7418b9..075c0d454 100644 --- a/common/defaults/system_settings.ini +++ b/common/defaults/system_settings.ini @@ -15,4 +15,10 @@ dmnt_cheats_enabled_by_default = u8!0x1 ; Controls whether dmnt should always save cheat toggle state ; for restoration on new game launch. 1 = always save toggles, ; 0 = only save toggles if toggle file exists. -dmnt_always_save_cheat_toggles = u8!0x0 \ No newline at end of file +dmnt_always_save_cheat_toggles = u8!0x0 +; Controls whether fs.mitm should redirect save files +; to directories on the sd card. +; 0 = Do not redirect, 1 = Redirect. +; NOTE: EXPERIMENTAL +; If you do not know what you are doing, do not touch this yet. +fsmitm_redirect_saves_to_sd = u8!0x0 \ No newline at end of file diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.cpp b/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.cpp index aba5e0369..f1558856c 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.cpp @@ -60,20 +60,20 @@ Result FsDirUtils::CopyFile(IFileSystem *dst_fs, IFileSystem *src_fs, const FsPa offset += read_size; } - + return ResultSuccess; } Result FsDirUtils::CopyDirectoryRecursively(IFileSystem *dst_fs, IFileSystem *src_fs, const FsPath &dst_path, const FsPath &src_path, void *work_buf, size_t work_buf_size) { FsPath work_path = dst_path; - return IterateDirectoryRecursively(src_fs, src_path, + return IterateDirectoryRecursively(src_fs, src_path, [&](const FsPath &path, const FsDirectoryEntry *dir_ent) -> Result { /* On Enter Directory */ /* Update path, create new dir. */ strncat(work_path.str, dir_ent->name, sizeof(work_path) - strnlen(work_path.str, sizeof(work_path) - 1) - 1); strncat(work_path.str, "/", sizeof(work_path) - strnlen(work_path.str, sizeof(work_path) - 1) - 1); return dst_fs->CreateDirectory(work_path); - }, + }, [&](const FsPath &path, const FsDirectoryEntry *dir_ent) -> Result { /* On Exit Directory */ /* Check we have a parent directory. */ const size_t work_path_len = strnlen(work_path.str, sizeof(work_path)); @@ -89,9 +89,45 @@ Result FsDirUtils::CopyDirectoryRecursively(IFileSystem *dst_fs, IFileSystem *sr p[1] = 0; return ResultSuccess; - }, + }, [&](const FsPath &path, const FsDirectoryEntry *dir_ent) -> Result { /* On File */ /* Just copy the file to the new fs. */ return CopyFile(dst_fs, src_fs, work_path, path, dir_ent, work_buf, work_buf_size); }); -} \ No newline at end of file +} + +Result FsDirUtils::EnsureDirectoryExists(IFileSystem *fs, const FsPath &path) { + FsPath normal_path; + size_t normal_path_len; + Result rc; + + /* Normalize the path. */ + if (R_FAILED((rc = FsPathUtils::Normalize(normal_path.str, sizeof(normal_path.str) - 1, path.str, &normal_path_len)))) { + return rc; + } + + /* Repeatedly call CreateDirectory on each directory leading to the target. */ + for (size_t i = 1; i < normal_path_len; i++) { + /* If we detect a separator, we're done. */ + if (normal_path.str[i] == '/') { + normal_path.str[i] = 0; + { + rc = fs->CreateDirectory(normal_path); + if (rc == ResultFsPathAlreadyExists) { + rc = ResultSuccess; + } + if (R_FAILED(rc)) { + return rc; + } + } + normal_path.str[i] = '/'; + } + } + + /* Call CreateDirectory on the final path. */ + rc = fs->CreateDirectory(normal_path); + if (rc == ResultFsPathAlreadyExists) { + rc = ResultSuccess; + } + return rc; +} diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.hpp b/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.hpp index 3656c7348..0587fc676 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.hpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_dir_utils.hpp @@ -132,6 +132,9 @@ class FsDirUtils { static Result CopyDirectoryRecursively(IFileSystem *fs, const FsPath &dst_path, const FsPath &src_path, void *work_buf, size_t work_buf_size) { return CopyDirectoryRecursively(fs, fs, dst_path, src_path, work_buf, work_buf_size); } + + /* Ensure directory existence. */ + static Result EnsureDirectoryExists(IFileSystem *fs, const FsPath &path); /* Other Utility. */ template diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.cpp b/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.cpp index 8ea14b87b..cf0ce4d40 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.cpp @@ -166,6 +166,27 @@ void DirectorySaveDataFileSystem::OnWritableFileClose() { /* TODO: Abort if < 0? N does not. */ } +Result DirectorySaveDataFileSystem::CopySaveFromProxy() { + if (this->proxy_save_fs != nullptr) { + Result rc; + + /* Get a buffer to work with. */ + void *work_buf = nullptr; + size_t work_buf_size = 0; + if (R_FAILED((rc = this->AllocateWorkBuffer(&work_buf, &work_buf_size, IdealWorkBuffersize)))) { + return rc; + } + ON_SCOPE_EXIT { free(work_buf); }; + + rc = FsDirUtils::CopyDirectoryRecursively(this, this->proxy_save_fs.get(), FsPathUtils::RootPath, FsPathUtils::RootPath, work_buf, work_buf_size); + if (R_FAILED(rc)) { + return rc; + } + return this->Commit(); + } + return ResultSuccess; +} + /* ================================================================================================ */ Result DirectorySaveDataFileSystem::CreateFileImpl(const FsPath &path, uint64_t size, int flags) { diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.hpp b/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.hpp index e40521faa..175b9f482 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.hpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_directory_savedata_filesystem.hpp @@ -35,12 +35,13 @@ class DirectorySaveDataFileSystem : public IFileSystem { private: std::shared_ptr shared_fs; std::unique_ptr unique_fs; + std::unique_ptr proxy_save_fs; IFileSystem *fs; HosMutex lock; size_t open_writable_files = 0; public: - DirectorySaveDataFileSystem(IFileSystem *fs) : unique_fs(fs) { + DirectorySaveDataFileSystem(IFileSystem *fs, std::unique_ptr pfs) : unique_fs(fs), proxy_save_fs(std::move(pfs)) { this->fs = this->unique_fs.get(); Result rc = this->Initialize(); if (R_FAILED(rc)) { @@ -48,7 +49,7 @@ class DirectorySaveDataFileSystem : public IFileSystem { } } - DirectorySaveDataFileSystem(std::unique_ptr fs) : unique_fs(std::move(fs)) { + DirectorySaveDataFileSystem(std::unique_ptr fs, std::unique_ptr pfs) : unique_fs(std::move(fs)), proxy_save_fs(std::move(pfs)) { this->fs = this->unique_fs.get(); Result rc = this->Initialize(); if (R_FAILED(rc)) { @@ -56,7 +57,7 @@ class DirectorySaveDataFileSystem : public IFileSystem { } } - DirectorySaveDataFileSystem(std::shared_ptr fs) : shared_fs(fs) { + DirectorySaveDataFileSystem(std::shared_ptr fs, std::unique_ptr pfs) : shared_fs(fs), proxy_save_fs(std::move(pfs)) { this->fs = this->shared_fs.get(); Result rc = this->Initialize(); if (R_FAILED(rc)) { @@ -64,7 +65,6 @@ class DirectorySaveDataFileSystem : public IFileSystem { } } - virtual ~DirectorySaveDataFileSystem() { } private: @@ -79,6 +79,7 @@ class DirectorySaveDataFileSystem : public IFileSystem { } public: void OnWritableFileClose(); + Result CopySaveFromProxy(); public: virtual Result CreateFileImpl(const FsPath &path, uint64_t size, int flags) override; virtual Result DeleteFileImpl(const FsPath &path) override; diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_ifilesystem.hpp b/stratosphere/ams_mitm/source/fs_mitm/fs_ifilesystem.hpp index eb8d250a8..aab0fd4aa 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_ifilesystem.hpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_ifilesystem.hpp @@ -198,13 +198,18 @@ class IFileSystem { class IFileSystemInterface : public IServiceObject { private: - std::unique_ptr base_fs; + std::unique_ptr unique_fs; + std::shared_ptr shared_fs; + IFileSystem *base_fs; public: - IFileSystemInterface(IFileSystem *fs) : base_fs(fs) { - /* ... */ + IFileSystemInterface(IFileSystem *fs) : unique_fs(fs) { + this->base_fs = this->unique_fs.get(); }; - IFileSystemInterface(std::unique_ptr fs) : base_fs(std::move(fs)) { - /* ... */ + IFileSystemInterface(std::unique_ptr fs) : unique_fs(std::move(fs)) { + this->base_fs = this->unique_fs.get(); + }; + IFileSystemInterface(std::shared_ptr fs) : shared_fs(fs) { + this->base_fs = this->shared_fs.get(); }; private: diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_save_utils.cpp b/stratosphere/ams_mitm/source/fs_mitm/fs_save_utils.cpp new file mode 100644 index 000000000..4250dab5c --- /dev/null +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_save_utils.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018 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 + +#include "fs_save_utils.hpp" + +Result FsSaveUtils::GetSaveDataSpaceIdString(const char **out_str, u8 space_id) { + const char *str = nullptr; + + switch (space_id) { + case FsSaveDataSpaceId_NandSystem: + case 100: /* ProperSystem */ + case 101: /* SafeMode */ + str = "sys"; + break; + case FsSaveDataSpaceId_NandUser: + str = "user"; + break; + case FsSaveDataSpaceId_SdCard: + str = "sd_sys"; + break; + case FsSaveDataSpaceId_TemporaryStorage: + str = "temp"; + break; + case 4: /* SdUser */ + str = "sd_user"; + break; + default: /* Unexpected */ + str = nullptr; + break; + } + + if (str == nullptr) { + return ResultFsInvalidSaveDataSpaceId; + } + + if (out_str) { + *out_str = str; + } + + return ResultSuccess; +} + +Result FsSaveUtils::GetSaveDataTypeString(const char **out_str, u8 save_data_type) { + const char *str = nullptr; + + switch (save_data_type) { + case FsSaveDataType_SystemSaveData: + str = "system"; + break; + case FsSaveDataType_SaveData: + str = "account"; + break; + case FsSaveDataType_BcatDeliveryCacheStorage: + str = "bcat"; + break; + case FsSaveDataType_DeviceSaveData: + str = "device"; + break; + case FsSaveDataType_TemporaryStorage: + str = "temp"; + break; + case FsSaveDataType_CacheStorage: + str = "cache"; + break; + case 6: /* System Bcat Save */ + str = "bcat_sys"; + break; + default: /* Unexpected */ + str = nullptr; + break; + } + + if (str == nullptr) { + /* TODO: Better result? */ + return ResultFsInvalidArgument; + } + + if (out_str) { + *out_str = str; + } + + return ResultSuccess; +} + +Result FsSaveUtils::GetSaveDataDirectoryPath(FsPath &out_path, u8 space_id, u8 save_data_type, u64 title_id, u128 user_id, u64 save_id) { + const char *space_id_str, *save_type_str; + Result rc; + + /* Get space_id, save_data_type strings. */ + if (R_FAILED((rc = GetSaveDataSpaceIdString(&space_id_str, space_id))) || R_FAILED((rc = GetSaveDataTypeString(&save_type_str, save_data_type)))) { + return rc; + } + + /* Clear and initialize the path. */ + std::memset(&out_path, 0, sizeof(out_path)); + const bool is_system = (save_id != 0 && user_id == 0); + size_t out_path_len; + if (is_system) { + out_path_len = static_cast(snprintf(out_path.str, sizeof(out_path.str), "/atmosphere/saves/sysnand/%s/%s/%016lx", + space_id_str, save_type_str, save_id)); + } else { + out_path_len = static_cast(snprintf(out_path.str, sizeof(out_path.str), "/atmosphere/saves/sysnand/%s/%s/%016lx/%016lx%016lx", + space_id_str, save_type_str, title_id, static_cast(user_id >> 64ul), static_cast(user_id))); + } + if (out_path_len >= sizeof(out_path)) { + /* TODO: Should we abort here? */ + return ResultFsTooLongPath; + } + + return ResultSuccess; +} diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_save_utils.hpp b/stratosphere/ams_mitm/source/fs_mitm/fs_save_utils.hpp new file mode 100644 index 000000000..b180303aa --- /dev/null +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_save_utils.hpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2018 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 "fs_path_utils.hpp" +#include "fs_ifilesystem.hpp" + +class FsSaveUtils { + private: + static Result GetSaveDataSpaceIdString(const char **out_str, u8 space_id); + static Result GetSaveDataTypeString(const char **out_str, u8 save_data_type); + public: + static Result GetSaveDataDirectoryPath(FsPath &out_path, u8 space_id, u8 save_data_type, u64 title_id, u128 user_id, u64 save_id); +}; diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_shim.c b/stratosphere/ams_mitm/source/fs_mitm/fs_shim.c index 7e4f21753..a5aa80dfd 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_shim.c +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_shim.c @@ -225,6 +225,46 @@ Result fsOpenFileSystemWithIdFwd(Service* s, FsFileSystem* out, u64 titleId, FsF return rc; } +Result fsOpenSaveDataFileSystemFwd(Service *s, FsFileSystem* out, u8 inval, FsSave *save) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + u64 inval;//Actually u8. + FsSave save; + } PACKED *raw; + + raw = serviceIpcPrepareHeader(s, &c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 51; + raw->inval = (u64)inval; + memcpy(&raw->save, save, sizeof(FsSave)); + + Result rc = serviceIpcDispatch(s); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + } *resp; + + serviceIpcParse(s, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc)) { + serviceCreateSubservice(&out->s, s, &r, 0); + } + } + + return rc; +} + /* Missing FS File commands. */ Result fsFileOperateRange(FsFile* f, u32 op_id, u64 off, u64 len, FsRangeInfo *out) { IpcCommand c; diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_shim.h b/stratosphere/ams_mitm/source/fs_mitm/fs_shim.h index cf6b28daf..fa163fe7e 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_shim.h +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_shim.h @@ -22,6 +22,7 @@ Result fsOpenDataStorageByCurrentProcessFwd(Service* s, FsStorage* out); Result fsOpenDataStorageByDataIdFwd(Service* s, FsStorageId storage_id, u64 data_id, FsStorage* out); Result fsOpenFileSystemWithPatchFwd(Service* s, FsFileSystem* out, u64 titleId, FsFileSystemType fsType); Result fsOpenFileSystemWithIdFwd(Service* s, FsFileSystem* out, u64 titleId, FsFileSystemType fsType, const char* contentPath); +Result fsOpenSaveDataFileSystemFwd(Service* s, FsFileSystem* out, u8 inval, FsSave *save); /* Missing FS File commands. */ Result fsFileOperateRange(FsFile* f, u32 op_id, u64 off, u64 len, FsRangeInfo *out); diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_service.cpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_service.cpp index e52940b05..bca3655bd 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_service.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_service.cpp @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + #include #include #include @@ -28,6 +28,8 @@ #include "fsmitm_romstorage.hpp" #include "fsmitm_layeredrom.hpp" +#include "fs_dir_utils.hpp" +#include "fs_save_utils.hpp" #include "fs_subdirectory_filesystem.hpp" #include "fs_directory_savedata_filesystem.hpp" @@ -41,7 +43,7 @@ static bool StorageCacheGetEntry(u64 title_id, std::shared_ptr *ptr) { std::scoped_lock lock(g_StorageCacheLock); - + /* Ensure we always use the cached copy if present. */ if (g_StorageCache.find(title_id) != g_StorageCache.end()) { auto intf = g_StorageCache[title_id].lock(); @@ -60,7 +62,7 @@ static void StorageCacheSetEntry(u64 title_id, std::shared_ptr fs = nullptr; u32 out_domain_id = 0; Result rc = ResultSuccess; - + ON_SCOPE_EXIT { if (R_SUCCEEDED(rc)) { out_fs.SetValue(std::move(fs)); @@ -96,12 +98,13 @@ Result FsMitmService::OpenHblWebContentFileSystem(Out(std::make_unique(std::make_shared(sd_fs), AtmosphereHblWebContentDir)); + std::unique_ptr web_ifs = std::make_unique(std::make_shared(sd_fs), AtmosphereHblWebContentDir); + fs = std::make_shared(std::move(web_ifs)); if (out_fs.IsDomain()) { out_domain_id = sd_fs.s.object_id; } @@ -130,7 +133,7 @@ Result FsMitmService::OpenFileSystemWithPatch(OutOpenHblWebContentFileSystem(out_fs); } @@ -138,7 +141,7 @@ Result FsMitmService::OpenFileSystemWithId(Outtitle_id) || filesystem_type != FsFileSystemType_ContentManual || !Utils::IsHblTid(title_id) || + if (!Utils::IsWebAppletTid(this->title_id) || filesystem_type != FsFileSystemType_ContentManual || !Utils::IsHblTid(title_id) || R_FAILED(Utils::OpenSdDir(AtmosphereHblWebContentDir, &d))) { return ResultAtmosphereMitmShouldForwardToSession; } @@ -158,12 +161,90 @@ Result FsMitmService::OpenFileSystemWithId(OutOpenHblWebContentFileSystem(out_fs); } +Result FsMitmService::OpenSaveDataFileSystem(Out> out_fs, u8 space_id, FsSave save_struct) { + bool should_redirect_saves = false; + if (R_FAILED(Utils::GetSettingsItemBooleanValue("atmosphere", "fsmitm_redirect_saves_to_sd", &should_redirect_saves))) { + return ResultAtmosphereMitmShouldForwardToSession; + } + + /* For now, until we're sure this is robust, only intercept normal savedata. */ + if (!should_redirect_saves || save_struct.SaveDataType != FsSaveDataType_SaveData) { + return ResultAtmosphereMitmShouldForwardToSession; + } + + /* Verify we can open the save. */ + FsFileSystem save_fs; + if (R_FAILED(fsOpenSaveDataFileSystemFwd(this->forward_service.get(), &save_fs, space_id, &save_struct))) { + return ResultAtmosphereMitmShouldForwardToSession; + } + std::unique_ptr save_ifs = std::make_unique(save_fs); + + { + std::shared_ptr fs = nullptr; + u32 out_domain_id = 0; + Result rc = ResultSuccess; + + ON_SCOPE_EXIT { + if (R_SUCCEEDED(rc)) { + out_fs.SetValue(std::move(fs)); + if (out_fs.IsDomain()) { + out_fs.ChangeObjectId(out_domain_id); + } + } + }; + + /* Mount the SD card using fs.mitm's session. */ + FsFileSystem sd_fs; + if (R_FAILED((rc = fsMountSdcard(&sd_fs)))) { + return rc; + } + std::shared_ptr sd_ifs = std::make_shared(sd_fs); + + + /* Verify that we can open the save directory, and that it exists. */ + const u64 target_tid = save_struct.titleID == 0 ? this->title_id : save_struct.titleID; + FsPath save_dir_path; + if (R_FAILED((rc = FsSaveUtils::GetSaveDataDirectoryPath(save_dir_path, space_id, save_struct.SaveDataType, target_tid, save_struct.userID, save_struct.saveID)))) { + return rc; + } + + /* Check if this is the first time we're making the save. */ + bool is_new_save = false; + { + DirectoryEntryType ent; + if (sd_ifs->GetEntryType(&ent, save_dir_path) == ResultFsPathNotFound) { + is_new_save = true; + } + } + + /* Ensure the directory exists. */ + if (R_FAILED((rc = FsDirUtils::EnsureDirectoryExists(sd_ifs.get(), save_dir_path)))) { + return rc; + } + + std::shared_ptr dirsave_ifs = std::make_shared(new SubDirectoryFileSystem(sd_ifs, save_dir_path.str), std::move(save_ifs)); + + /* If it's the first time we're making the save, copy existing savedata over. */ + if (is_new_save) { + /* TODO: Check error? */ + dirsave_ifs->CopySaveFromProxy(); + } + + fs = std::make_shared(static_cast>(dirsave_ifs)); + if (out_fs.IsDomain()) { + out_domain_id = sd_fs.s.object_id; + } + + return rc; + } +} + /* Gate access to the BIS partitions. */ Result FsMitmService::OpenBisStorage(Out> out_storage, u32 bis_partition_id) { std::shared_ptr storage = nullptr; u32 out_domain_id = 0; Result rc = ResultSuccess; - + ON_SCOPE_EXIT { if (R_SUCCEEDED(rc)) { out_storage.SetValue(std::move(storage)); @@ -172,7 +253,7 @@ Result FsMitmService::OpenBisStorage(Out> out } } }; - + { FsStorage bis_storage; rc = fsOpenBisStorageFwd(this->forward_service.get(), &bis_storage, bis_partition_id); @@ -195,7 +276,7 @@ Result FsMitmService::OpenBisStorage(Out> out if (is_sysmodule || has_bis_write_flag) { /* Sysmodules should still be allowed to read and write. */ storage = std::make_shared(new ProxyStorage(bis_storage)); - } else if (Utils::IsHblTid(this->title_id) && + } else if (Utils::IsHblTid(this->title_id) && ((BisStorageId_BcPkg2_1 <= bis_partition_id && bis_partition_id <= BisStorageId_BcPkg2_6) || bis_partition_id == BisStorageId_Boot1)) { /* Allow HBL to write to boot1 (safe firm) + package2. */ /* This is needed to not break compatibility with ChoiDujourNX, which does not check for write access before beginning an update. */ @@ -211,7 +292,7 @@ Result FsMitmService::OpenBisStorage(Out> out } } } - + return rc; } @@ -220,27 +301,27 @@ Result FsMitmService::OpenDataStorageByCurrentProcess(Out storage = nullptr; u32 out_domain_id = 0; Result rc = ResultSuccess; - + if (!this->should_override_contents) { return ResultAtmosphereMitmShouldForwardToSession; } - + bool has_cache = StorageCacheGetEntry(this->title_id, &storage); - + ON_SCOPE_EXIT { if (R_SUCCEEDED(rc)) { if (!has_cache) { StorageCacheSetEntry(this->title_id, &storage); } - + out_storage.SetValue(std::move(storage)); if (out_storage.IsDomain()) { out_storage.ChangeObjectId(out_domain_id); } } }; - - + + if (has_cache) { if (out_storage.IsDomain()) { FsStorage s = {0}; @@ -257,7 +338,7 @@ Result FsMitmService::OpenDataStorageByCurrentProcess(Outforward_service.get(), &data_storage); Log(armGetTls(), 0x100); @@ -279,7 +360,7 @@ Result FsMitmService::OpenDataStorageByCurrentProcess(Outshould_override_contents) { return ResultAtmosphereMitmShouldForwardToSession; } - + std::shared_ptr storage = nullptr; u32 out_domain_id = 0; Result rc = ResultSuccess; - + bool has_cache = StorageCacheGetEntry(data_id, &storage); - + ON_SCOPE_EXIT { if (R_SUCCEEDED(rc)) { if (!has_cache) { StorageCacheSetEntry(data_id, &storage); } - + out_storage.SetValue(std::move(storage)); if (out_storage.IsDomain()) { out_storage.ChangeObjectId(out_domain_id); } } }; - + if (has_cache) { if (out_storage.IsDomain()) { FsStorage s = {0}; @@ -346,6 +427,6 @@ Result FsMitmService::OpenDataStorageByDataId(Out. */ - + #pragma once #include #include @@ -23,12 +23,14 @@ enum FspSrvCmd : u32 { FspSrvCmd_OpenFileSystemDeprecated = 0, - + FspSrvCmd_SetCurrentProcess = 1, - + FspSrvCmd_OpenFileSystemWithPatch = 7, FspSrvCmd_OpenFileSystemWithId = 8, - + + FspSrvCmd_OpenSaveDataFileSystem = 51, + FspSrvCmd_OpenBisStorage = 12, FspSrvCmd_OpenDataStorageByCurrentProcess = 200, FspSrvCmd_OpenDataStorageByDataId = 202, @@ -48,13 +50,13 @@ class FsMitmService : public IMitmServiceObject { this->should_override_contents = (this->title_id >= TitleId_ApplicationStart || Utils::HasSdMitMFlag(this->title_id)) && Utils::HasOverrideButton(this->title_id); } } - + static bool ShouldMitm(u64 pid, u64 tid) { /* Don't Mitm KIPs */ if (pid < 0x50) { return false; } - + static std::atomic_bool has_launched_qlaunch = false; /* TODO: intercepting everything seems to cause issues with sleep mode, for some reason. */ @@ -62,10 +64,10 @@ class FsMitmService : public IMitmServiceObject { if (tid == TitleId_AppletQlaunch) { has_launched_qlaunch = true; } - + return has_launched_qlaunch || tid == TitleId_Ns || tid >= TitleId_ApplicationStart || Utils::HasSdMitMFlag(tid); } - + static void PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx); private: Result OpenHblWebContentFileSystem(Out> &out); @@ -73,6 +75,7 @@ class FsMitmService : public IMitmServiceObject { /* Overridden commands. */ Result OpenFileSystemWithPatch(Out> out, u64 title_id, u32 filesystem_type); Result OpenFileSystemWithId(Out> out, InPointer path, u64 title_id, u32 filesystem_type); + Result OpenSaveDataFileSystem(Out> out, u8 space_id, FsSave save_struct); Result OpenBisStorage(Out> out, u32 bis_partition_id); Result OpenDataStorageByCurrentProcess(Out> out); Result OpenDataStorageByDataId(Out> out, u64 data_id, u8 storage_id); @@ -81,6 +84,7 @@ class FsMitmService : public IMitmServiceObject { /* TODO MakeServiceCommandMeta(), */ MakeServiceCommandMeta(), MakeServiceCommandMeta(), + MakeServiceCommandMeta(), MakeServiceCommandMeta(), MakeServiceCommandMeta(), MakeServiceCommandMeta(), diff --git a/stratosphere/ams_mitm/source/utils.cpp b/stratosphere/ams_mitm/source/utils.cpp index e490d70b8..753a8521c 100644 --- a/stratosphere/ams_mitm/source/utils.cpp +++ b/stratosphere/ams_mitm/source/utils.cpp @@ -640,3 +640,15 @@ Result Utils::GetSettingsItemValueSize(const char *name, const char *key, u64 *o Result Utils::GetSettingsItemValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size) { return SettingsItemManager::GetValue(name, key, out, max_size, out_size); } + +Result Utils::GetSettingsItemBooleanValue(const char *name, const char *key, bool *out) { + u8 val = 0; + u64 out_size; + Result rc = Utils::GetSettingsItemValue(name, key, &val, sizeof(val), &out_size); + if (R_SUCCEEDED(rc)) { + if (out) { + *out = val != 0; + } + } + return rc; +} diff --git a/stratosphere/ams_mitm/source/utils.hpp b/stratosphere/ams_mitm/source/utils.hpp index bebdf974b..c95218072 100644 --- a/stratosphere/ams_mitm/source/utils.hpp +++ b/stratosphere/ams_mitm/source/utils.hpp @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + #pragma once #include #include @@ -46,45 +46,47 @@ class Utils { public: static bool IsSdInitialized(); static void WaitSdInitialized(); - + static Result OpenSdFile(const char *fn, int flags, FsFile *out); static Result OpenSdFileForAtmosphere(u64 title_id, const char *fn, int flags, FsFile *out); 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 SaveSdFileForAtmosphere(u64 title_id, const char *fn, void *data, size_t size); - + static bool HasSdRomfsContent(u64 title_id); - + /* Delayed Initialization + MitM detection. */ static void InitializeThreadFunc(void *args); - + static bool IsHblTid(u64 tid); static bool IsWebAppletTid(u64 tid); - + static bool HasTitleFlag(u64 tid, const char *flag); static bool HasHblFlag(const char *flag); static bool HasGlobalFlag(const char *flag); static bool HasFlag(u64 tid, const char *flag); - + static bool HasSdMitMFlag(u64 tid); static bool HasSdDisableMitMFlag(u64 tid); - - + + static bool IsHidAvailable(); static Result GetKeysHeld(u64 *keys); - + static OverrideKey GetTitleOverrideKey(u64 tid); static bool HasOverrideButton(u64 tid); - + /* Settings! */ static Result GetSettingsItemValueSize(const char *name, const char *key, u64 *out_size); static Result GetSettingsItemValue(const char *name, const char *key, void *out, size_t max_size, u64 *out_size); + + static Result GetSettingsItemBooleanValue(const char *name, const char *key, bool *out); private: static void RefreshConfiguration(); }; \ No newline at end of file diff --git a/stratosphere/libstratosphere b/stratosphere/libstratosphere index b6ca2d89a..8021b07eb 160000 --- a/stratosphere/libstratosphere +++ b/stratosphere/libstratosphere @@ -1 +1 @@ -Subproject commit b6ca2d89ae5106ea21454f332f07076159ad2e92 +Subproject commit 8021b07eb82b9941c1820b211bbe6d898589e59b