2018-09-07 17:00:13 +02:00
|
|
|
/*
|
2019-04-08 04:00:49 +02:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 17:00:13 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
2018-06-10 03:33:22 +02:00
|
|
|
#include <switch.h>
|
2018-11-04 22:56:07 +01:00
|
|
|
#include <stratosphere.hpp>
|
2018-06-10 03:33:22 +02:00
|
|
|
#include "fsmitm_service.hpp"
|
2018-06-10 19:11:05 +02:00
|
|
|
#include "fs_shim.h"
|
|
|
|
|
2019-01-29 11:29:29 +01:00
|
|
|
#include "../utils.hpp"
|
2018-11-15 04:40:46 +01:00
|
|
|
#include "fsmitm_boot0storage.hpp"
|
2018-06-10 19:11:05 +02:00
|
|
|
#include "fsmitm_romstorage.hpp"
|
2018-06-15 01:50:01 +02:00
|
|
|
#include "fsmitm_layeredrom.hpp"
|
2018-06-10 19:11:05 +02:00
|
|
|
|
2019-04-05 22:36:38 +02:00
|
|
|
#include "fs_dir_utils.hpp"
|
|
|
|
#include "fs_save_utils.hpp"
|
2019-03-22 19:28:09 +01:00
|
|
|
#include "fs_subdirectory_filesystem.hpp"
|
2019-03-28 18:43:33 +01:00
|
|
|
#include "fs_directory_savedata_filesystem.hpp"
|
2019-03-22 16:49:10 +01:00
|
|
|
|
2019-01-29 11:29:29 +01:00
|
|
|
#include "../debug.hpp"
|
2018-06-10 03:33:22 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
static HosMutex g_StorageCacheLock;
|
|
|
|
static std::unordered_map<u64, std::weak_ptr<IStorageInterface>> g_StorageCache;
|
|
|
|
|
|
|
|
static bool StorageCacheGetEntry(u64 title_id, std::shared_ptr<IStorageInterface> *out) {
|
|
|
|
std::scoped_lock<HosMutex> lock(g_StorageCacheLock);
|
|
|
|
if (g_StorageCache.find(title_id) == g_StorageCache.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
auto intf = g_StorageCache[title_id].lock();
|
|
|
|
if (intf != nullptr) {
|
|
|
|
*out = intf;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StorageCacheSetEntry(u64 title_id, std::shared_ptr<IStorageInterface> *ptr) {
|
|
|
|
std::scoped_lock<HosMutex> lock(g_StorageCacheLock);
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
/* 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();
|
|
|
|
if (intf != nullptr) {
|
|
|
|
*ptr = intf;
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
g_StorageCache[title_id] = *ptr;
|
|
|
|
}
|
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
void FsMitmService::PostProcess(IMitmServiceObject *obj, IpcResponseContext *ctx) {
|
|
|
|
auto this_ptr = static_cast<FsMitmService *>(obj);
|
|
|
|
switch ((FspSrvCmd)ctx->cmd_id) {
|
|
|
|
case FspSrvCmd_SetCurrentProcess:
|
|
|
|
if (R_SUCCEEDED(ctx->rc)) {
|
|
|
|
this_ptr->has_initialized = true;
|
|
|
|
this_ptr->process_id = ctx->request.Pid;
|
|
|
|
this_ptr->title_id = this_ptr->process_id;
|
|
|
|
if (R_FAILED(MitmQueryUtils::GetAssociatedTidForPid(this_ptr->process_id, &this_ptr->title_id))) {
|
|
|
|
/* Log here, if desired. */
|
|
|
|
}
|
2018-06-15 01:50:01 +02:00
|
|
|
break;
|
2018-06-10 11:06:50 +02:00
|
|
|
}
|
2018-06-19 20:07:31 +02:00
|
|
|
break;
|
|
|
|
default:
|
2018-06-10 11:06:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-10 19:11:05 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 20:38:23 +01:00
|
|
|
Result FsMitmService::OpenHblWebContentFileSystem(Out<std::shared_ptr<IFileSystemInterface>> &out_fs) {
|
|
|
|
std::shared_ptr<IFileSystemInterface> fs = nullptr;
|
|
|
|
u32 out_domain_id = 0;
|
2019-03-29 06:39:39 +01:00
|
|
|
Result rc = ResultSuccess;
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2019-03-22 20:38:23 +01:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
out_fs.SetValue(std::move(fs));
|
|
|
|
if (out_fs.IsDomain()) {
|
|
|
|
out_fs.ChangeObjectId(out_domain_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2019-03-22 20:38:23 +01:00
|
|
|
/* Mount the SD card using fs.mitm's session. */
|
|
|
|
FsFileSystem sd_fs;
|
|
|
|
rc = fsMountSdcard(&sd_fs);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-04-05 22:36:38 +02:00
|
|
|
std::unique_ptr<IFileSystem> web_ifs = std::make_unique<SubDirectoryFileSystem>(std::make_shared<ProxyFileSystem>(sd_fs), AtmosphereHblWebContentDir);
|
|
|
|
fs = std::make_shared<IFileSystemInterface>(std::move(web_ifs));
|
2019-03-22 20:38:23 +01:00
|
|
|
if (out_fs.IsDomain()) {
|
|
|
|
out_domain_id = sd_fs.s.object_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result FsMitmService::OpenFileSystemWithPatch(Out<std::shared_ptr<IFileSystemInterface>> out_fs, u64 title_id, u32 filesystem_type) {
|
2019-03-26 19:53:30 +01:00
|
|
|
/* Check for eligibility. */
|
|
|
|
{
|
|
|
|
FsDir d;
|
|
|
|
if (!Utils::IsWebAppletTid(this->title_id) || filesystem_type != FsFileSystemType_ContentManual || !Utils::IsHblTid(title_id) ||
|
|
|
|
R_FAILED(Utils::OpenSdDir(AtmosphereHblWebContentDir, &d))) {
|
2019-03-29 04:19:38 +01:00
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
2019-03-26 19:53:30 +01:00
|
|
|
}
|
|
|
|
fsDirClose(&d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's an existing filesystem, don't override. */
|
|
|
|
/* TODO: Multiplex, overriding existing content with HBL content. */
|
|
|
|
{
|
|
|
|
FsFileSystem fs;
|
|
|
|
if (R_SUCCEEDED(fsOpenFileSystemWithPatchFwd(this->forward_service.get(), &fs, title_id, static_cast<FsFileSystemType>(filesystem_type)))) {
|
|
|
|
fsFsClose(&fs);
|
2019-03-29 04:19:38 +01:00
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
2019-03-26 19:53:30 +01:00
|
|
|
}
|
2019-03-22 20:38:23 +01:00
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2019-03-22 20:38:23 +01:00
|
|
|
return this->OpenHblWebContentFileSystem(out_fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result FsMitmService::OpenFileSystemWithId(Out<std::shared_ptr<IFileSystemInterface>> out_fs, InPointer<char> path, u64 title_id, u32 filesystem_type) {
|
2019-03-26 19:53:30 +01:00
|
|
|
/* Check for eligibility. */
|
|
|
|
{
|
|
|
|
FsDir d;
|
2019-04-05 22:36:38 +02:00
|
|
|
if (!Utils::IsWebAppletTid(this->title_id) || filesystem_type != FsFileSystemType_ContentManual || !Utils::IsHblTid(title_id) ||
|
2019-03-26 19:53:30 +01:00
|
|
|
R_FAILED(Utils::OpenSdDir(AtmosphereHblWebContentDir, &d))) {
|
2019-03-29 04:19:38 +01:00
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
2019-03-26 19:53:30 +01:00
|
|
|
}
|
|
|
|
fsDirClose(&d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's an existing filesystem, don't override. */
|
|
|
|
/* TODO: Multiplex, overriding existing content with HBL content. */
|
|
|
|
{
|
|
|
|
FsFileSystem fs;
|
|
|
|
if (R_SUCCEEDED(fsOpenFileSystemWithIdFwd(this->forward_service.get(), &fs, title_id, static_cast<FsFileSystemType>(filesystem_type), path.pointer))) {
|
|
|
|
fsFsClose(&fs);
|
2019-03-29 04:19:38 +01:00
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
2019-03-26 19:53:30 +01:00
|
|
|
}
|
2019-03-22 20:38:23 +01:00
|
|
|
}
|
2019-03-26 17:05:19 +01:00
|
|
|
|
2019-03-22 20:38:23 +01:00
|
|
|
return this->OpenHblWebContentFileSystem(out_fs);
|
|
|
|
}
|
|
|
|
|
2019-04-05 22:36:38 +02:00
|
|
|
Result FsMitmService::OpenSaveDataFileSystem(Out<std::shared_ptr<IFileSystemInterface>> out_fs, u8 space_id, FsSave save_struct) {
|
2019-05-17 04:28:24 +02:00
|
|
|
const bool has_sd_save_flags = Utils::HasFlag(this->title_id, "sd_save");
|
2019-04-05 22:36:38 +02:00
|
|
|
bool should_redirect_saves = false;
|
|
|
|
if (R_FAILED(Utils::GetSettingsItemBooleanValue("atmosphere", "fsmitm_redirect_saves_to_sd", &should_redirect_saves))) {
|
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
|
|
|
}
|
2019-05-17 04:28:24 +02:00
|
|
|
|
|
|
|
if (!has_sd_save_flags) {
|
|
|
|
should_redirect_saves = false;
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
|
|
|
/* 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<IFileSystem> save_ifs = std::make_unique<ProxyFileSystem>(save_fs);
|
|
|
|
|
|
|
|
{
|
|
|
|
std::shared_ptr<IFileSystemInterface> 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<IFileSystem> sd_ifs = std::make_shared<ProxyFileSystem>(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<DirectorySaveDataFileSystem> dirsave_ifs = std::make_shared<DirectorySaveDataFileSystem>(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<IFileSystemInterface>(static_cast<std::shared_ptr<IFileSystem>>(dirsave_ifs));
|
|
|
|
if (out_fs.IsDomain()) {
|
|
|
|
out_domain_id = sd_fs.s.object_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:39:48 +01:00
|
|
|
/* Gate access to the BIS partitions. */
|
|
|
|
Result FsMitmService::OpenBisStorage(Out<std::shared_ptr<IStorageInterface>> out_storage, u32 bis_partition_id) {
|
|
|
|
std::shared_ptr<IStorageInterface> storage = nullptr;
|
|
|
|
u32 out_domain_id = 0;
|
2019-03-29 06:39:39 +01:00
|
|
|
Result rc = ResultSuccess;
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-15 03:39:48 +01:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
out_storage.SetValue(std::move(storage));
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
out_storage.ChangeObjectId(out_domain_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-15 03:39:48 +01:00
|
|
|
{
|
|
|
|
FsStorage bis_storage;
|
|
|
|
rc = fsOpenBisStorageFwd(this->forward_service.get(), &bis_storage, bis_partition_id);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-03-29 05:36:08 +01:00
|
|
|
const bool is_sysmodule = TitleIdIsSystem(this->title_id);
|
2018-11-29 21:30:32 +01:00
|
|
|
const bool has_bis_write_flag = Utils::HasFlag(this->title_id, "bis_write");
|
|
|
|
const bool has_cal0_read_flag = Utils::HasFlag(this->title_id, "cal_read");
|
2018-11-15 04:40:46 +01:00
|
|
|
if (bis_partition_id == BisStorageId_Boot0) {
|
2018-11-15 12:57:55 +01:00
|
|
|
storage = std::make_shared<IStorageInterface>(new Boot0Storage(bis_storage, this->title_id));
|
2018-11-15 15:23:44 +01:00
|
|
|
} else if (bis_partition_id == BisStorageId_Prodinfo) {
|
|
|
|
/* PRODINFO should *never* be writable. */
|
2018-11-29 21:30:32 +01:00
|
|
|
if (is_sysmodule || has_cal0_read_flag) {
|
2018-11-16 00:29:02 +01:00
|
|
|
storage = std::make_shared<IStorageInterface>(new ROProxyStorage(bis_storage));
|
|
|
|
} else {
|
|
|
|
/* Do not allow non-sysmodules to read *or* write CAL0. */
|
|
|
|
fsStorageClose(&bis_storage);
|
2019-04-22 12:42:20 +02:00
|
|
|
rc = ResultFsPermissionDenied;
|
|
|
|
return rc;
|
2018-11-16 00:29:02 +01:00
|
|
|
}
|
2018-11-15 03:39:48 +01:00
|
|
|
} else {
|
2018-11-29 21:30:32 +01:00
|
|
|
if (is_sysmodule || has_bis_write_flag) {
|
2018-11-15 04:40:46 +01:00
|
|
|
/* Sysmodules should still be allowed to read and write. */
|
|
|
|
storage = std::make_shared<IStorageInterface>(new ProxyStorage(bis_storage));
|
2019-04-05 22:36:38 +02:00
|
|
|
} else if (Utils::IsHblTid(this->title_id) &&
|
2019-03-24 00:33:49 +01:00
|
|
|
((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. */
|
2019-03-22 20:38:23 +01:00
|
|
|
/* TODO: get fixed so that this can be turned off without causing bricks :/ */
|
|
|
|
storage = std::make_shared<IStorageInterface>(new ProxyStorage(bis_storage));
|
2018-11-29 21:30:32 +01:00
|
|
|
} else {
|
|
|
|
/* Non-sysmodules should be allowed to read. */
|
|
|
|
storage = std::make_shared<IStorageInterface>(new ROProxyStorage(bis_storage));
|
2018-11-15 04:40:46 +01:00
|
|
|
}
|
2018-11-15 03:39:48 +01:00
|
|
|
}
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
out_domain_id = bis_storage.s.object_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-15 03:39:48 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-06-15 01:50:01 +02:00
|
|
|
/* Add redirection for RomFS to the SD card. */
|
2018-10-30 01:30:39 +01:00
|
|
|
Result FsMitmService::OpenDataStorageByCurrentProcess(Out<std::shared_ptr<IStorageInterface>> out_storage) {
|
|
|
|
std::shared_ptr<IStorageInterface> storage = nullptr;
|
2018-06-15 01:50:01 +02:00
|
|
|
u32 out_domain_id = 0;
|
2019-03-29 06:39:39 +01:00
|
|
|
Result rc = ResultSuccess;
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-15 23:59:47 +01:00
|
|
|
if (!this->should_override_contents) {
|
2019-03-29 04:19:38 +01:00
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
2018-11-15 23:59:47 +01:00
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
bool has_cache = StorageCacheGetEntry(this->title_id, &storage);
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 06:14:34 +01:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2018-11-04 22:56:07 +01:00
|
|
|
if (!has_cache) {
|
|
|
|
StorageCacheSetEntry(this->title_id, &storage);
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 06:14:34 +01:00
|
|
|
out_storage.SetValue(std::move(storage));
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
out_storage.ChangeObjectId(out_domain_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-05 22:36:38 +02:00
|
|
|
|
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
if (has_cache) {
|
2018-10-30 01:30:39 +01:00
|
|
|
if (out_storage.IsDomain()) {
|
2018-10-16 22:33:45 +02:00
|
|
|
FsStorage s = {0};
|
2018-10-30 01:30:39 +01:00
|
|
|
rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service.get(), &s);
|
2018-10-16 22:33:45 +02:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
out_domain_id = s.s.object_id;
|
|
|
|
}
|
2018-06-28 07:02:06 +02:00
|
|
|
} else {
|
2019-03-29 06:39:39 +01:00
|
|
|
rc = ResultSuccess;
|
2018-06-28 07:02:06 +02:00
|
|
|
}
|
2018-11-04 22:56:07 +01:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
storage.reset();
|
2018-06-15 01:50:01 +02:00
|
|
|
}
|
2018-06-28 07:02:06 +02:00
|
|
|
} else {
|
|
|
|
FsStorage data_storage;
|
|
|
|
FsFile data_file;
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service.get(), &data_storage);
|
2018-06-28 07:02:06 +02:00
|
|
|
|
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2018-11-04 21:45:29 +01:00
|
|
|
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<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), this->title_id));
|
|
|
|
} else {
|
|
|
|
storage = std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, this->title_id));
|
|
|
|
}
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
out_domain_id = data_storage.s.object_id;
|
|
|
|
}
|
2018-06-28 07:02:06 +02:00
|
|
|
} else {
|
2018-11-04 21:45:29 +01:00
|
|
|
/* If we don't have anything to modify, there's no sense in maintaining a copy of the metadata tables. */
|
|
|
|
fsStorageClose(&data_storage);
|
2019-03-29 04:19:38 +01:00
|
|
|
rc = ResultAtmosphereMitmShouldForwardToSession;
|
2018-06-28 07:02:06 +02:00
|
|
|
}
|
2018-06-15 01:50:01 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
return rc;
|
2018-06-15 01:50:01 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 19:11:05 +02:00
|
|
|
/* Add redirection for System Data Archives to the SD card. */
|
2018-10-30 14:29:30 +01:00
|
|
|
Result FsMitmService::OpenDataStorageByDataId(Out<std::shared_ptr<IStorageInterface>> out_storage, u64 data_id, u8 sid) {
|
2018-06-13 00:00:09 +02:00
|
|
|
FsStorageId storage_id = (FsStorageId)sid;
|
2018-06-10 19:11:05 +02:00
|
|
|
FsStorage data_storage;
|
|
|
|
FsFile data_file;
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-15 23:59:47 +01:00
|
|
|
if (!this->should_override_contents) {
|
2019-03-29 04:19:38 +01:00
|
|
|
return ResultAtmosphereMitmShouldForwardToSession;
|
2018-11-15 23:59:47 +01:00
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
std::shared_ptr<IStorageInterface> storage = nullptr;
|
2018-06-15 01:50:01 +02:00
|
|
|
u32 out_domain_id = 0;
|
2019-03-29 06:39:39 +01:00
|
|
|
Result rc = ResultSuccess;
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
bool has_cache = StorageCacheGetEntry(data_id, &storage);
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2018-11-04 22:56:07 +01:00
|
|
|
if (!has_cache) {
|
|
|
|
StorageCacheSetEntry(data_id, &storage);
|
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
out_storage.SetValue(std::move(storage));
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
out_storage.ChangeObjectId(out_domain_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
if (has_cache) {
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
FsStorage s = {0};
|
|
|
|
rc = fsOpenDataStorageByDataIdFwd(this->forward_service.get(), storage_id, data_id, &s);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
out_domain_id = s.s.object_id;
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-29 06:39:39 +01:00
|
|
|
rc = ResultSuccess;
|
2018-11-04 22:56:07 +01:00
|
|
|
}
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
storage.reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = fsOpenDataStorageByDataIdFwd(this->forward_service.get(), storage_id, data_id, &data_storage);
|
2018-10-16 22:33:45 +02:00
|
|
|
|
2018-11-04 22:56:07 +01:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
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<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), data_id));
|
|
|
|
} else {
|
|
|
|
storage = std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, data_id));
|
|
|
|
}
|
|
|
|
if (out_storage.IsDomain()) {
|
|
|
|
out_domain_id = data_storage.s.object_id;
|
|
|
|
}
|
2018-11-04 21:45:29 +01:00
|
|
|
} else {
|
2018-11-04 22:56:07 +01:00
|
|
|
/* If we don't have anything to modify, there's no sense in maintaining a copy of the metadata tables. */
|
|
|
|
fsStorageClose(&data_storage);
|
2019-03-29 04:19:38 +01:00
|
|
|
rc = ResultAtmosphereMitmShouldForwardToSession;
|
2018-11-04 21:45:29 +01:00
|
|
|
}
|
2018-06-13 00:00:09 +02:00
|
|
|
}
|
2018-06-10 19:11:05 +02:00
|
|
|
}
|
2019-04-05 22:36:38 +02:00
|
|
|
|
2018-10-30 01:30:39 +01:00
|
|
|
return rc;
|
2019-05-17 04:28:24 +02:00
|
|
|
}
|