2018-09-07 16:00:13 +01:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-06-10 02:33:22 +01:00
|
|
|
#include <switch.h>
|
|
|
|
#include "fsmitm_service.hpp"
|
2018-06-10 18:11:05 +01:00
|
|
|
#include "fs_shim.h"
|
|
|
|
|
|
|
|
#include "fsmitm_worker.hpp"
|
|
|
|
#include "fsmitm_utils.hpp"
|
|
|
|
#include "fsmitm_romstorage.hpp"
|
2018-06-15 00:50:01 +01:00
|
|
|
#include "fsmitm_layeredrom.hpp"
|
2018-06-10 18:11:05 +01:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
#include "mitm_query_service.hpp"
|
2018-06-10 18:11:05 +01:00
|
|
|
#include "debug.hpp"
|
2018-06-10 02:33:22 +01:00
|
|
|
|
|
|
|
Result FsMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
|
|
|
Result rc = 0xF601;
|
2018-06-15 00:50:01 +01:00
|
|
|
if (this->has_initialized) {
|
2018-06-19 19:07:31 +01:00
|
|
|
switch (static_cast<FspSrvCmd>(cmd_id)) {
|
|
|
|
case FspSrvCmd::OpenDataStorageByCurrentProcess:
|
2018-06-15 00:50:01 +01:00
|
|
|
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_current_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
2018-06-19 19:07:31 +01:00
|
|
|
case FspSrvCmd::OpenDataStorageByDataId:
|
2018-06-29 03:42:27 +01:00
|
|
|
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_data_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
2018-06-15 00:50:01 +01:00
|
|
|
break;
|
2018-06-19 19:07:31 +01:00
|
|
|
default:
|
|
|
|
break;
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-19 19:07:31 +01:00
|
|
|
if (static_cast<FspSrvCmd>(cmd_id) == FspSrvCmd::SetCurrentProcess) {
|
2018-06-15 00:50:01 +01:00
|
|
|
if (r.HasPid) {
|
|
|
|
this->init_pid = r.Pid;
|
2018-06-10 10:06:50 +01:00
|
|
|
}
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2018-06-10 10:06:50 +01:00
|
|
|
}
|
2018-06-10 08:17:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
void FsMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
2018-06-10 08:19:21 +01:00
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
|
|
|
} *resp = (decltype(resp))r.Raw;
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
u64 *tls = (u64 *)armGetTls();
|
2018-06-19 19:07:31 +01:00
|
|
|
std::array<u64, 0x100/sizeof(u64)> backup_tls;
|
|
|
|
std::copy(tls, tls + backup_tls.size(), backup_tls.begin());
|
2018-06-12 23:00:09 +01:00
|
|
|
|
2018-06-10 08:19:21 +01:00
|
|
|
Result rc = (Result)resp->result;
|
2018-06-19 19:07:31 +01:00
|
|
|
switch (static_cast<FspSrvCmd>(cmd_id)) {
|
|
|
|
case FspSrvCmd::SetCurrentProcess:
|
2018-06-10 10:06:50 +01:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
this->has_initialized = true;
|
2018-06-12 23:00:09 +01:00
|
|
|
}
|
2018-06-15 00:50:01 +01:00
|
|
|
this->process_id = this->init_pid;
|
|
|
|
this->title_id = this->process_id;
|
|
|
|
if (R_FAILED(MitMQueryUtils::get_associated_tid_for_pid(this->process_id, &this->title_id))) {
|
|
|
|
/* Log here, if desired. */
|
|
|
|
}
|
2018-06-19 19:07:31 +01:00
|
|
|
std::copy(backup_tls.begin(), backup_tls.end(), tls);
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-10 10:06:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
resp->result = rc;
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FsMitMService::handle_deferred() {
|
|
|
|
/* This service is never deferrable. */
|
|
|
|
return 0;
|
2018-06-10 18:11:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
/* Add redirection for RomFS to the SD card. */
|
|
|
|
std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_storage_by_current_process() {
|
|
|
|
IPCSession<IStorageInterface> *out_session = NULL;
|
2018-06-28 06:02:06 +01:00
|
|
|
std::shared_ptr<IStorageInterface> out_storage = nullptr;
|
2018-06-15 00:50:01 +01:00
|
|
|
u32 out_domain_id = 0;
|
|
|
|
Result rc;
|
2018-06-28 06:02:06 +01:00
|
|
|
if (this->romfs_storage != nullptr) {
|
|
|
|
if (this->get_owner() != NULL) {
|
2018-10-16 21:33:45 +01:00
|
|
|
FsStorage s = {0};
|
|
|
|
rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &s);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
out_domain_id = s.s.object_id;
|
|
|
|
}
|
2018-06-28 06:02:06 +01:00
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
}
|
2018-06-15 00:50:01 +01:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
2018-06-28 06:02:06 +01:00
|
|
|
out_storage = this->romfs_storage;
|
|
|
|
out_session = new IPCSession<IStorageInterface>(out_storage);
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
2018-06-28 06:02:06 +01:00
|
|
|
} else {
|
|
|
|
FsStorage data_storage;
|
|
|
|
FsFile data_file;
|
2018-10-16 21:33:45 +01:00
|
|
|
|
|
|
|
rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &data_storage);
|
2018-06-28 06:02:06 +01:00
|
|
|
|
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
/* 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))) {
|
|
|
|
out_storage = std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), this->title_id));
|
|
|
|
} else {
|
|
|
|
out_storage = std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, this->title_id));
|
|
|
|
}
|
|
|
|
this->romfs_storage = out_storage;
|
|
|
|
out_session = new IPCSession<IStorageInterface>(out_storage);
|
|
|
|
if (this->get_owner() == NULL) {
|
|
|
|
FsMitMWorker::AddWaitable(out_session);
|
2018-10-16 21:33:45 +01:00
|
|
|
} else {
|
|
|
|
out_domain_id = data_storage.s.object_id;
|
2018-06-28 06:02:06 +01:00
|
|
|
}
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OutSession out_s = OutSession(out_session);
|
|
|
|
out_s.domain_id = out_domain_id;
|
|
|
|
return {rc, out_s};
|
|
|
|
}
|
|
|
|
|
2018-06-10 18:11:05 +01:00
|
|
|
/* Add redirection for System Data Archives to the SD card. */
|
2018-06-12 23:00:09 +01:00
|
|
|
std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_storage_by_data_id(u64 sid, u64 data_id) {
|
|
|
|
FsStorageId storage_id = (FsStorageId)sid;
|
|
|
|
IPCSession<IStorageInterface> *out_session = NULL;
|
2018-06-10 18:11:05 +01:00
|
|
|
FsStorage data_storage;
|
|
|
|
FsFile data_file;
|
2018-06-15 00:50:01 +01:00
|
|
|
u32 out_domain_id = 0;
|
2018-06-12 23:00:09 +01:00
|
|
|
Result rc;
|
2018-10-16 21:33:45 +01:00
|
|
|
|
|
|
|
rc = fsOpenDataStorageByDataIdFwd(this->forward_service, storage_id, data_id, &data_storage);
|
|
|
|
|
2018-06-10 18:11:05 +01:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
/* TODO: Is there a sensible path that ends in ".romfs" we can use?" */
|
2018-06-15 00:50:01 +01:00
|
|
|
if (R_SUCCEEDED(Utils::OpenSdFileForAtmosphere(data_id, "romfs.bin", FS_OPEN_READ, &data_file))) {
|
|
|
|
out_session = new IPCSession<IStorageInterface>(std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), data_id)));
|
|
|
|
} else {
|
|
|
|
out_session = new IPCSession<IStorageInterface>(std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, data_id)));
|
2018-06-10 18:11:05 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
if (this->get_owner() == NULL) {
|
2018-06-15 00:50:01 +01:00
|
|
|
FsMitMWorker::AddWaitable(out_session);
|
2018-10-16 21:33:45 +01:00
|
|
|
} else {
|
|
|
|
out_domain_id = data_storage.s.object_id;
|
2018-06-12 23:00:09 +01:00
|
|
|
}
|
2018-06-10 18:11:05 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
|
|
|
|
OutSession out_s = OutSession(out_session);
|
|
|
|
out_s.domain_id = out_domain_id;
|
|
|
|
return {rc, out_s};
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|