fs.mitm: WIP LayeredFS impl (NOTE: UNUSABLE ATM)

Also greatly refactors libstratosphere, and does a lot of other things.
There is a lot of code in this one.
This commit is contained in:
Michael Scire
2018-06-14 17:50:01 -06:00
parent 82b248aeac
commit c2d9ac8f5c
56 changed files with 1615 additions and 243 deletions

View File

@@ -5,21 +5,28 @@
#include "fsmitm_worker.hpp"
#include "fsmitm_utils.hpp"
#include "fsmitm_romstorage.hpp"
#include "fsmitm_layeredrom.hpp"
#include "mitm_query_service.hpp"
#include "debug.hpp"
Result FsMitMService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
Result rc = 0xF601;
switch (cmd_id) {
case FspSrv_Cmd_SetCurrentProcess:
if (!this->has_initialized && r.HasPid) {
this->process_id = r.Pid;
if (this->has_initialized) {
switch (cmd_id) {
case FspSrv_Cmd_OpenDataStorageByCurrentProcess:
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_current_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case FspSrv_Cmd_OpenDataStorageByDataId:
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_data_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
}
} else {
if (cmd_id == FspSrv_Cmd_SetCurrentProcess) {
if (r.HasPid) {
this->init_pid = r.Pid;
}
break;
/*case FspSrv_Cmd_OpenDataStorageByDataId:
rc = WrapIpcCommandImpl<&FsMitMService::open_data_storage_by_data_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break; */
}
}
return rc;
}
@@ -41,24 +48,15 @@ void FsMitMService::postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_
case FspSrv_Cmd_SetCurrentProcess:
if (R_SUCCEEDED(rc)) {
this->has_initialized = true;
rc = pminfoGetTitleId(&this->title_id, this->process_id);
if (R_FAILED(rc)) {
if (rc == 0x20F) {
this->title_id = this->process_id;
rc = 0x0;
} else {
fatalSimple(rc);
}
}
}
Log(&this->process_id, 8);
Log(&this->title_id, 8);
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. */
}
for (unsigned int i = 0; i < sizeof(backup_tls)/sizeof(u64); i++) {
tls[i] = backup_tls[i];
}
if (this->title_id >= 0x0100000000001000) {
Reboot();
}
break;
}
resp->result = rc;
@@ -69,13 +67,46 @@ Result FsMitMService::handle_deferred() {
return 0;
}
/* 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;
FsStorage data_storage;
FsFile data_file;
u32 out_domain_id = 0;
Result rc;
if (this->get_owner() == NULL) {
rc = fsOpenDataStorageByCurrentProcessFwd(this->forward_service, &data_storage);
} else {
rc = fsOpenDataStorageByCurrentProcessFromDomainFwd(this->forward_service, &out_domain_id);
if (R_SUCCEEDED(rc)) {
rc = ipcCopyFromDomain(this->forward_service->handle, out_domain_id, &data_storage.s);
}
}
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_session = new IPCSession<IStorageInterface>(std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), std::make_shared<RomFileStorage>(data_file), this->title_id)));
} else {
out_session = new IPCSession<IStorageInterface>(std::make_shared<IStorageInterface>(new LayeredRomFS(std::make_shared<RomInterfaceStorage>(data_storage), nullptr, this->title_id)));
}
if (this->get_owner() == NULL) {
FsMitMWorker::AddWaitable(out_session);
}
}
OutSession out_s = OutSession(out_session);
out_s.domain_id = out_domain_id;
return {rc, out_s};
}
/* Add redirection for System Data Archives to the SD card. */
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;
FsStorage data_storage;
FsFile data_file;
u32 out_domain_id;
u32 out_domain_id = 0;
Result rc;
if (this->get_owner() == NULL) {
rc = fsOpenDataStorageByDataId(this->forward_service, storage_id, data_id, &data_storage);
@@ -86,17 +117,14 @@ std::tuple<Result, OutSession<IStorageInterface>> FsMitMService::open_data_stora
}
}
if (R_SUCCEEDED(rc)) {
char path[FS_MAX_PATH] = {0};
/* TODO: Is there a sensible path that ends in ".romfs" we can use?" */
snprintf(path, sizeof(path), "/atmosphere/titles/%016lx/romfs.bin", data_id);
if (R_SUCCEEDED(Utils::OpenSdFile(path, FS_OPEN_READ, &data_file))) {
fsStorageClose(&data_storage);
out_session = new IPCSession<IStorageInterface>(new IStorageInterface(new RomFileStorage(data_file)));
} else {
out_session = new IPCSession<IStorageInterface>(new IStorageInterface(new RomInterfaceStorage(data_storage)));
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)));
}
if (this->get_owner() == NULL) {
FsMitmWorker::AddWaitable(out_session);
FsMitMWorker::AddWaitable(out_session);
}
}